]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
666399bc6809f6eeb7cd708bb34e020816a1a11e
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2025 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 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry *, struct elf_info_failed *);
51
52 /* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
54
55 static bool
56 _bfd_elf_link_keep_memory (struct bfd_link_info *info)
57 {
58 #ifdef USE_MMAP
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
61
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
63
64 this is opt-in by each backend. */
65 const struct elf_backend_data *bed
66 = get_elf_backend_data (info->output_bfd);
67 if (bed != NULL && bed->use_mmap)
68 return false;
69 #endif
70 bfd *abfd;
71 bfd_size_type size;
72
73 if (!info->keep_memory)
74 return false;
75
76 if (info->max_cache_size == (bfd_size_type) -1)
77 return true;
78
79 abfd = info->input_bfds;
80 size = info->cache_size;
81 do
82 {
83 if (size >= info->max_cache_size)
84 {
85 /* Over the limit. Reduce the memory usage. */
86 info->keep_memory = false;
87 return false;
88 }
89 if (!abfd)
90 break;
91 size += abfd->alloc_size;
92 abfd = abfd->link.next;
93 }
94 while (1);
95
96 return true;
97 }
98
99 static struct elf_link_hash_entry *
100 get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes,
101 unsigned int symndx,
102 unsigned int ext_sym_start)
103 {
104 if (sym_hashes == NULL
105 /* Guard against corrupt input. See PR 32636 for an example. */
106 || symndx < ext_sym_start)
107 return NULL;
108
109 struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
110
111 /* The hash might be empty. See PR 32641 for an example of this. */
112 if (h == NULL)
113 return NULL;
114
115 while (h->root.type == bfd_link_hash_indirect
116 || h->root.type == bfd_link_hash_warning)
117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
118
119 return h;
120 }
121
122 struct elf_link_hash_entry *
123 _bfd_elf_get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes,
124 unsigned int symndx,
125 Elf_Internal_Shdr * symtab_hdr)
126 {
127 if (symtab_hdr == NULL)
128 return NULL;
129
130 return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info);
131 }
132
133 static struct elf_link_hash_entry *
134 get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx)
135 {
136 if (cookie == NULL || cookie->sym_hashes == NULL)
137 return NULL;
138
139 if (r_symndx >= cookie->locsymcount
140 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
141 return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff);
142
143 return NULL;
144 }
145
146 asection *
147 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
148 unsigned long r_symndx,
149 bool discard)
150 {
151 struct elf_link_hash_entry *h;
152
153 h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
154
155 if (h != NULL)
156 {
157 if ((h->root.type == bfd_link_hash_defined
158 || h->root.type == bfd_link_hash_defweak)
159 && discarded_section (h->root.u.def.section))
160 return h->root.u.def.section;
161 else
162 return NULL;
163 }
164
165 /* It's not a relocation against a global symbol,
166 but it could be a relocation against a local
167 symbol for a discarded section. */
168 asection *isec;
169 Elf_Internal_Sym *isym;
170
171 /* Need to: get the symbol; get the section. */
172 isym = &cookie->locsyms[r_symndx];
173 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
174 if (isec != NULL
175 && discard ? discarded_section (isec) : 1)
176 return isec;
177
178 return NULL;
179 }
180
181 /* Define a symbol in a dynamic linkage section. */
182
183 struct elf_link_hash_entry *
184 _bfd_elf_define_linkage_sym (bfd *abfd,
185 struct bfd_link_info *info,
186 asection *sec,
187 const char *name)
188 {
189 struct elf_link_hash_entry *h;
190 struct bfd_link_hash_entry *bh;
191 const struct elf_backend_data *bed;
192
193 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
194 if (h != NULL)
195 {
196 /* Zap symbol defined in an as-needed lib that wasn't linked.
197 This is a symptom of a larger problem: Absolute symbols
198 defined in shared libraries can't be overridden, because we
199 lose the link to the bfd which is via the symbol section. */
200 h->root.type = bfd_link_hash_new;
201 bh = &h->root;
202 }
203 else
204 bh = NULL;
205
206 bed = get_elf_backend_data (abfd);
207 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
208 sec, 0, NULL, false, bed->collect,
209 &bh))
210 return NULL;
211 h = (struct elf_link_hash_entry *) bh;
212 BFD_ASSERT (h != NULL);
213 h->def_regular = 1;
214 h->non_elf = 0;
215 h->root.linker_def = 1;
216 h->type = STT_OBJECT;
217 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
218 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
219
220 (*bed->elf_backend_hide_symbol) (info, h, true);
221 return h;
222 }
223
224 bool
225 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
226 {
227 flagword flags;
228 asection *s;
229 struct elf_link_hash_entry *h;
230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
231 struct elf_link_hash_table *htab = elf_hash_table (info);
232
233 /* This function may be called more than once. */
234 if (htab->sgot != NULL)
235 return true;
236
237 flags = bed->dynamic_sec_flags;
238
239 s = bfd_make_section_anyway_with_flags (abfd,
240 (bed->rela_plts_and_copies_p
241 ? ".rela.got" : ".rel.got"),
242 flags | SEC_READONLY);
243 if (s == NULL
244 || !bfd_set_section_alignment (s, bed->s->log_file_align))
245 return false;
246 htab->srelgot = s;
247
248 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
249 if (s == NULL
250 || !bfd_set_section_alignment (s, bed->s->log_file_align))
251 return false;
252 htab->sgot = s;
253
254 if (bed->want_got_plt)
255 {
256 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
257 if (s == NULL
258 || !bfd_set_section_alignment (s, bed->s->log_file_align))
259 return false;
260 htab->sgotplt = s;
261 }
262
263 /* The first bit of the global offset table is the header. */
264 s->size += bed->got_header_size;
265
266 if (bed->want_got_sym)
267 {
268 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
269 (or .got.plt) section. We don't do this in the linker script
270 because we don't want to define the symbol if we are not creating
271 a global offset table. */
272 h = _bfd_elf_define_linkage_sym (abfd, info, s,
273 "_GLOBAL_OFFSET_TABLE_");
274 elf_hash_table (info)->hgot = h;
275 if (h == NULL)
276 return false;
277 }
278
279 return true;
280 }
281 \f
282 /* Create a strtab to hold the dynamic symbol names. */
283 static bool
284 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
285 {
286 struct elf_link_hash_table *hash_table;
287
288 hash_table = elf_hash_table (info);
289 if (hash_table->dynobj == NULL)
290 {
291 /* We may not set dynobj, an input file holding linker created
292 dynamic sections to abfd, which may be a dynamic object with
293 its own dynamic sections. We need to find a normal input file
294 to hold linker created sections if possible. */
295 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
296 {
297 bfd *ibfd;
298 asection *s;
299 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
300 if ((ibfd->flags
301 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
302 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
303 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
304 && !((s = ibfd->sections) != NULL
305 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
306 {
307 abfd = ibfd;
308 break;
309 }
310 }
311 hash_table->dynobj = abfd;
312 }
313
314 if (hash_table->dynstr == NULL)
315 {
316 hash_table->dynstr = _bfd_elf_strtab_init ();
317 if (hash_table->dynstr == NULL)
318 return false;
319 }
320 return true;
321 }
322
323 /* Create some sections which will be filled in with dynamic linking
324 information. ABFD is an input file which requires dynamic sections
325 to be created. The dynamic sections take up virtual memory space
326 when the final executable is run, so we need to create them before
327 addresses are assigned to the output sections. We work out the
328 actual contents and size of these sections later. */
329
330 bool
331 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
332 {
333 flagword flags;
334 asection *s;
335 const struct elf_backend_data *bed;
336 struct elf_link_hash_entry *h;
337
338 if (! is_elf_hash_table (info->hash))
339 return false;
340
341 if (elf_hash_table (info)->dynamic_sections_created)
342 return true;
343
344 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
345 return false;
346
347 abfd = elf_hash_table (info)->dynobj;
348 bed = get_elf_backend_data (abfd);
349
350 flags = bed->dynamic_sec_flags;
351
352 /* A dynamically linked executable has a .interp section, but a
353 shared library does not. */
354 if (bfd_link_executable (info) && !info->nointerp)
355 {
356 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
357 flags | SEC_READONLY);
358 if (s == NULL)
359 return false;
360 }
361
362 /* Create sections to hold version informations. These are removed
363 if they are not needed. */
364 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
365 flags | SEC_READONLY);
366 if (s == NULL
367 || !bfd_set_section_alignment (s, bed->s->log_file_align))
368 return false;
369
370 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
371 flags | SEC_READONLY);
372 if (s == NULL
373 || !bfd_set_section_alignment (s, 1))
374 return false;
375
376 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
377 flags | SEC_READONLY);
378 if (s == NULL
379 || !bfd_set_section_alignment (s, bed->s->log_file_align))
380 return false;
381
382 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
383 flags | SEC_READONLY);
384 if (s == NULL
385 || !bfd_set_section_alignment (s, bed->s->log_file_align))
386 return false;
387 elf_hash_table (info)->dynsym = s;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
390 flags | SEC_READONLY);
391 if (s == NULL)
392 return false;
393
394 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
395 if (s == NULL
396 || !bfd_set_section_alignment (s, bed->s->log_file_align))
397 return false;
398 elf_hash_table (info)->dynamic = s;
399
400 /* The special symbol _DYNAMIC is always set to the start of the
401 .dynamic section. We could set _DYNAMIC in a linker script, but we
402 only want to define it if we are, in fact, creating a .dynamic
403 section. We don't want to define it if there is no .dynamic
404 section, since on some ELF platforms the start up code examines it
405 to decide how to initialize the process. */
406 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
407 elf_hash_table (info)->hdynamic = h;
408 if (h == NULL)
409 return false;
410
411 if (info->emit_hash)
412 {
413 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
414 flags | SEC_READONLY);
415 if (s == NULL
416 || !bfd_set_section_alignment (s, bed->s->log_file_align))
417 return false;
418 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
419 }
420
421 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
422 {
423 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
424 flags | SEC_READONLY);
425 if (s == NULL
426 || !bfd_set_section_alignment (s, bed->s->log_file_align))
427 return false;
428 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
429 4 32-bit words followed by variable count of 64-bit words, then
430 variable count of 32-bit words. */
431 if (bed->s->arch_size == 64)
432 elf_section_data (s)->this_hdr.sh_entsize = 0;
433 else
434 elf_section_data (s)->this_hdr.sh_entsize = 4;
435 }
436
437 if (info->enable_dt_relr)
438 {
439 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
440 flags | SEC_READONLY);
441 if (s == NULL
442 || !bfd_set_section_alignment (s, bed->s->log_file_align))
443 return false;
444 elf_hash_table (info)->srelrdyn = s;
445 }
446
447 /* Let the backend create the rest of the sections. This lets the
448 backend set the right flags. The backend will normally create
449 the .got and .plt sections. */
450 if (bed->elf_backend_create_dynamic_sections == NULL
451 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
452 return false;
453
454 elf_hash_table (info)->dynamic_sections_created = true;
455
456 return true;
457 }
458
459 /* Create dynamic sections when linking against a dynamic object. */
460
461 bool
462 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
463 {
464 flagword flags, pltflags;
465 struct elf_link_hash_entry *h;
466 asection *s;
467 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
468 struct elf_link_hash_table *htab = elf_hash_table (info);
469
470 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
471 .rel[a].bss sections. */
472 flags = bed->dynamic_sec_flags;
473
474 pltflags = flags;
475 if (bed->plt_not_loaded)
476 /* We do not clear SEC_ALLOC here because we still want the OS to
477 allocate space for the section; it's just that there's nothing
478 to read in from the object file. */
479 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
480 else
481 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
482 if (bed->plt_readonly)
483 pltflags |= SEC_READONLY;
484
485 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
486 if (s == NULL
487 || !bfd_set_section_alignment (s, bed->plt_alignment))
488 return false;
489 htab->splt = s;
490
491 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
492 .plt section. */
493 if (bed->want_plt_sym)
494 {
495 h = _bfd_elf_define_linkage_sym (abfd, info, s,
496 "_PROCEDURE_LINKAGE_TABLE_");
497 elf_hash_table (info)->hplt = h;
498 if (h == NULL)
499 return false;
500 }
501
502 s = bfd_make_section_anyway_with_flags (abfd,
503 (bed->rela_plts_and_copies_p
504 ? ".rela.plt" : ".rel.plt"),
505 flags | SEC_READONLY);
506 if (s == NULL
507 || !bfd_set_section_alignment (s, bed->s->log_file_align))
508 return false;
509 htab->srelplt = s;
510
511 if (! _bfd_elf_create_got_section (abfd, info))
512 return false;
513
514 if (bed->want_dynbss)
515 {
516 /* The .dynbss section is a place to put symbols which are defined
517 by dynamic objects, are referenced by regular objects, and are
518 not functions. We must allocate space for them in the process
519 image and use a R_*_COPY reloc to tell the dynamic linker to
520 initialize them at run time. The linker script puts the .dynbss
521 section into the .bss section of the final image. */
522 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
523 SEC_ALLOC | SEC_LINKER_CREATED);
524 if (s == NULL)
525 return false;
526 htab->sdynbss = s;
527
528 if (bed->want_dynrelro)
529 {
530 /* Similarly, but for symbols that were originally in read-only
531 sections. This section doesn't really need to have contents,
532 but make it like other .data.rel.ro sections. */
533 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
534 flags);
535 if (s == NULL)
536 return false;
537 htab->sdynrelro = s;
538 }
539
540 /* The .rel[a].bss section holds copy relocs. This section is not
541 normally needed. We need to create it here, though, so that the
542 linker will map it to an output section. We can't just create it
543 only if we need it, because we will not know whether we need it
544 until we have seen all the input files, and the first time the
545 main linker code calls BFD after examining all the input files
546 (size_dynamic_sections) the input sections have already been
547 mapped to the output sections. If the section turns out not to
548 be needed, we can discard it later. We will never need this
549 section when generating a shared object, since they do not use
550 copy relocs. */
551 if (bfd_link_executable (info))
552 {
553 s = bfd_make_section_anyway_with_flags (abfd,
554 (bed->rela_plts_and_copies_p
555 ? ".rela.bss" : ".rel.bss"),
556 flags | SEC_READONLY);
557 if (s == NULL
558 || !bfd_set_section_alignment (s, bed->s->log_file_align))
559 return false;
560 htab->srelbss = s;
561
562 if (bed->want_dynrelro)
563 {
564 s = (bfd_make_section_anyway_with_flags
565 (abfd, (bed->rela_plts_and_copies_p
566 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
567 flags | SEC_READONLY));
568 if (s == NULL
569 || !bfd_set_section_alignment (s, bed->s->log_file_align))
570 return false;
571 htab->sreldynrelro = s;
572 }
573 }
574 }
575
576 return true;
577 }
578 \f
579 /* Record a new dynamic symbol. We record the dynamic symbols as we
580 read the input files, since we need to have a list of all of them
581 before we can determine the final sizes of the output sections.
582 Note that we may actually call this function even though we are not
583 going to output any dynamic symbols; in some cases we know that a
584 symbol should be in the dynamic symbol table, but only if there is
585 one. */
586
587 bool
588 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
589 struct elf_link_hash_entry *h)
590 {
591 if (h->dynindx == -1)
592 {
593 struct elf_strtab_hash *dynstr;
594 char *p;
595 const char *name;
596 size_t indx;
597
598 if (h->root.type == bfd_link_hash_defined
599 || h->root.type == bfd_link_hash_defweak)
600 {
601 /* An IR symbol should not be made dynamic. */
602 if (h->root.u.def.section != NULL
603 && h->root.u.def.section->owner != NULL
604 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
605 return true;
606 }
607
608 /* XXX: The ABI draft says the linker must turn hidden and
609 internal symbols into STB_LOCAL symbols when producing the
610 DSO. However, if ld.so honors st_other in the dynamic table,
611 this would not be necessary. */
612 switch (ELF_ST_VISIBILITY (h->other))
613 {
614 case STV_INTERNAL:
615 case STV_HIDDEN:
616 if (h->root.type != bfd_link_hash_undefined
617 && h->root.type != bfd_link_hash_undefweak)
618 {
619 h->forced_local = 1;
620 return true;
621 }
622
623 default:
624 break;
625 }
626
627 h->dynindx = elf_hash_table (info)->dynsymcount;
628 ++elf_hash_table (info)->dynsymcount;
629
630 dynstr = elf_hash_table (info)->dynstr;
631 if (dynstr == NULL)
632 {
633 /* Create a strtab to hold the dynamic symbol names. */
634 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
635 if (dynstr == NULL)
636 return false;
637 }
638
639 char *unversioned_name = NULL;
640
641 /* We don't put any version information in the dynamic string
642 table. */
643 name = h->root.root.string;
644 p = strchr (name, ELF_VER_CHR);
645 if (p != NULL)
646 {
647 unversioned_name = bfd_malloc (p - name + 1);
648 memcpy (unversioned_name, name, p - name);
649 unversioned_name[p - name] = 0;
650 name = unversioned_name;
651 }
652
653 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
654
655 if (p != NULL)
656 free (unversioned_name);
657
658 if (indx == (size_t) -1)
659 return false;
660 h->dynstr_index = indx;
661 }
662
663 return true;
664 }
665 \f
666 /* Mark a symbol dynamic. */
667
668 static void
669 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
670 struct elf_link_hash_entry *h,
671 Elf_Internal_Sym *sym)
672 {
673 struct bfd_elf_dynamic_list *d = info->dynamic_list;
674
675 /* It may be called more than once on the same H. */
676 if(h->dynamic || bfd_link_relocatable (info))
677 return;
678
679 if ((info->dynamic_data
680 && (h->type == STT_OBJECT
681 || h->type == STT_COMMON
682 || (sym != NULL
683 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
684 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
685 || (d != NULL
686 && h->non_elf
687 && (*d->match) (&d->head, NULL, h->root.root.string)))
688 {
689 h->dynamic = 1;
690 /* NB: If a symbol is made dynamic by --dynamic-list, it has
691 non-IR reference. */
692 h->root.non_ir_ref_dynamic = 1;
693 }
694 }
695
696 /* Record an assignment to a symbol made by a linker script. We need
697 this in case some dynamic object refers to this symbol. */
698
699 bool
700 bfd_elf_record_link_assignment (bfd *output_bfd,
701 struct bfd_link_info *info,
702 const char *name,
703 bool provide,
704 bool hidden)
705 {
706 struct elf_link_hash_entry *h, *hv;
707 struct elf_link_hash_table *htab;
708 const struct elf_backend_data *bed;
709
710 if (!is_elf_hash_table (info->hash))
711 return true;
712
713 htab = elf_hash_table (info);
714 h = elf_link_hash_lookup (htab, name, !provide, true, false);
715 if (h == NULL)
716 return provide;
717
718 if (h->root.type == bfd_link_hash_warning)
719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720
721 if (h->versioned == unknown)
722 {
723 /* Set versioned if symbol version is unknown. */
724 char *version = strrchr (name, ELF_VER_CHR);
725 if (version)
726 {
727 if (version > name && version[-1] != ELF_VER_CHR)
728 h->versioned = versioned_hidden;
729 else
730 h->versioned = versioned;
731 }
732 }
733
734 /* Symbols defined in a linker script but not referenced anywhere
735 else will have non_elf set. */
736 if (h->non_elf)
737 {
738 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
739 h->non_elf = 0;
740 }
741
742 switch (h->root.type)
743 {
744 case bfd_link_hash_defined:
745 case bfd_link_hash_defweak:
746 case bfd_link_hash_common:
747 break;
748 case bfd_link_hash_undefweak:
749 case bfd_link_hash_undefined:
750 /* Since we're defining the symbol, don't let it seem to have not
751 been defined. record_dynamic_symbol and size_dynamic_sections
752 may depend on this. */
753 h->root.type = bfd_link_hash_new;
754 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
755 bfd_link_repair_undef_list (&htab->root);
756 break;
757 case bfd_link_hash_new:
758 break;
759 case bfd_link_hash_indirect:
760 /* We had a versioned symbol in a dynamic library. We make the
761 the versioned symbol point to this one. */
762 bed = get_elf_backend_data (output_bfd);
763 hv = h;
764 while (hv->root.type == bfd_link_hash_indirect
765 || hv->root.type == bfd_link_hash_warning)
766 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
767 /* We don't need to update h->root.u since linker will set them
768 later. */
769 h->root.type = bfd_link_hash_undefined;
770 hv->root.type = bfd_link_hash_indirect;
771 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
772 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
773 break;
774 default:
775 BFD_FAIL ();
776 return false;
777 }
778
779 /* If this symbol is being provided by the linker script, and it is
780 currently defined by a dynamic object, but not by a regular
781 object, then mark it as undefined so that the generic linker will
782 force the correct value. */
783 if (provide
784 && h->def_dynamic
785 && !h->def_regular)
786 h->root.type = bfd_link_hash_undefined;
787
788 /* If this symbol is currently defined by a dynamic object, but not
789 by a regular object, then clear out any version information because
790 the symbol will not be associated with the dynamic object any
791 more. */
792 if (h->def_dynamic && !h->def_regular)
793 h->verinfo.verdef = NULL;
794
795 /* Make sure this symbol is not garbage collected. */
796 h->mark = 1;
797
798 h->def_regular = 1;
799
800 if (hidden)
801 {
802 bed = get_elf_backend_data (output_bfd);
803 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
804 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
805 (*bed->elf_backend_hide_symbol) (info, h, true);
806 }
807
808 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
809 and executables. */
810 if (!bfd_link_relocatable (info)
811 && h->dynindx != -1
812 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
813 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
814 h->forced_local = 1;
815
816 if ((h->def_dynamic
817 || h->ref_dynamic
818 || bfd_link_dll (info))
819 && !h->forced_local
820 && h->dynindx == -1)
821 {
822 if (! bfd_elf_link_record_dynamic_symbol (info, h))
823 return false;
824
825 /* If this is a weak defined symbol, and we know a corresponding
826 real symbol from the same dynamic object, make sure the real
827 symbol is also made into a dynamic symbol. */
828 if (h->is_weakalias)
829 {
830 struct elf_link_hash_entry *def = weakdef (h);
831
832 if (def->dynindx == -1
833 && !bfd_elf_link_record_dynamic_symbol (info, def))
834 return false;
835 }
836 }
837
838 return true;
839 }
840
841 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
842 success, and 2 on a failure caused by attempting to record a symbol
843 in a discarded section, eg. a discarded link-once section symbol. */
844
845 int
846 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
847 bfd *input_bfd,
848 long input_indx)
849 {
850 size_t amt;
851 struct elf_link_local_dynamic_entry *entry;
852 struct elf_link_hash_table *eht;
853 struct elf_strtab_hash *dynstr;
854 size_t dynstr_index;
855 char *name;
856 Elf_External_Sym_Shndx eshndx;
857 char esym[sizeof (Elf64_External_Sym)];
858
859 if (! is_elf_hash_table (info->hash))
860 return 0;
861
862 /* See if the entry exists already. */
863 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
864 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
865 return 1;
866
867 amt = sizeof (*entry);
868 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
869 if (entry == NULL)
870 return 0;
871
872 /* Go find the symbol, so that we can find it's name. */
873 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
874 1, input_indx, &entry->isym, esym, &eshndx))
875 {
876 bfd_release (input_bfd, entry);
877 return 0;
878 }
879
880 if (entry->isym.st_shndx != SHN_UNDEF
881 && entry->isym.st_shndx < SHN_LORESERVE)
882 {
883 asection *s;
884
885 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
886 if (s == NULL || bfd_is_abs_section (s->output_section))
887 {
888 /* We can still bfd_release here as nothing has done another
889 bfd_alloc. We can't do this later in this function. */
890 bfd_release (input_bfd, entry);
891 return 2;
892 }
893 }
894
895 name = (bfd_elf_string_from_elf_section
896 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
897 entry->isym.st_name));
898
899 dynstr = elf_hash_table (info)->dynstr;
900 if (dynstr == NULL)
901 {
902 /* Create a strtab to hold the dynamic symbol names. */
903 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
904 if (dynstr == NULL)
905 return 0;
906 }
907
908 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
909 if (dynstr_index == (size_t) -1)
910 return 0;
911 entry->isym.st_name = dynstr_index;
912
913 eht = elf_hash_table (info);
914
915 entry->next = eht->dynlocal;
916 eht->dynlocal = entry;
917 entry->input_bfd = input_bfd;
918 entry->input_indx = input_indx;
919 eht->dynsymcount++;
920
921 /* Whatever binding the symbol had before, it's now local. */
922 entry->isym.st_info
923 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
924
925 /* The dynindx will be set at the end of size_dynamic_sections. */
926
927 return 1;
928 }
929
930 /* Return the dynindex of a local dynamic symbol. */
931
932 long
933 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
934 bfd *input_bfd,
935 long input_indx)
936 {
937 struct elf_link_local_dynamic_entry *e;
938
939 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
940 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
941 return e->dynindx;
942 return -1;
943 }
944
945 /* This function is used to renumber the dynamic symbols, if some of
946 them are removed because they are marked as local. This is called
947 via elf_link_hash_traverse. */
948
949 static bool
950 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
951 void *data)
952 {
953 size_t *count = (size_t *) data;
954
955 if (h->forced_local)
956 return true;
957
958 if (h->dynindx != -1)
959 h->dynindx = ++(*count);
960
961 return true;
962 }
963
964
965 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
966 STB_LOCAL binding. */
967
968 static bool
969 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
970 void *data)
971 {
972 size_t *count = (size_t *) data;
973
974 if (!h->forced_local)
975 return true;
976
977 if (h->dynindx != -1)
978 h->dynindx = ++(*count);
979
980 return true;
981 }
982
983 /* Return true if the dynamic symbol for a given section should be
984 omitted when creating a shared library. */
985 bool
986 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
987 struct bfd_link_info *info,
988 asection *p)
989 {
990 struct elf_link_hash_table *htab;
991 asection *ip;
992
993 switch (elf_section_data (p)->this_hdr.sh_type)
994 {
995 case SHT_PROGBITS:
996 case SHT_NOBITS:
997 /* If sh_type is yet undecided, assume it could be
998 SHT_PROGBITS/SHT_NOBITS. */
999 case SHT_NULL:
1000 htab = elf_hash_table (info);
1001 if (htab->text_index_section != NULL)
1002 return p != htab->text_index_section && p != htab->data_index_section;
1003
1004 return (htab->dynobj != NULL
1005 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
1006 && ip->output_section == p);
1007
1008 /* There shouldn't be section relative relocations
1009 against any other section. */
1010 default:
1011 return true;
1012 }
1013 }
1014
1015 bool
1016 _bfd_elf_omit_section_dynsym_all
1017 (bfd *output_bfd ATTRIBUTE_UNUSED,
1018 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1019 asection *p ATTRIBUTE_UNUSED)
1020 {
1021 return true;
1022 }
1023
1024 /* Assign dynsym indices. In a shared library we generate a section
1025 symbol for each output section, which come first. Next come symbols
1026 which have been forced to local binding. Then all of the back-end
1027 allocated local dynamic syms, followed by the rest of the global
1028 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1029 (This prevents the early call before elf_backend_init_index_section
1030 and strip_excluded_output_sections setting dynindx for sections
1031 that are stripped.) */
1032
1033 static unsigned long
1034 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
1035 struct bfd_link_info *info,
1036 unsigned long *section_sym_count)
1037 {
1038 unsigned long dynsymcount = 0;
1039 bool do_sec = section_sym_count != NULL;
1040
1041 if (bfd_link_pic (info)
1042 || elf_hash_table (info)->is_relocatable_executable)
1043 {
1044 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1045 asection *p;
1046 for (p = output_bfd->sections; p ; p = p->next)
1047 if ((p->flags & SEC_EXCLUDE) == 0
1048 && (p->flags & SEC_ALLOC) != 0
1049 && elf_hash_table (info)->dynamic_relocs
1050 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1051 {
1052 ++dynsymcount;
1053 if (do_sec)
1054 elf_section_data (p)->dynindx = dynsymcount;
1055 }
1056 else if (do_sec)
1057 elf_section_data (p)->dynindx = 0;
1058 }
1059 if (do_sec)
1060 *section_sym_count = dynsymcount;
1061
1062 elf_link_hash_traverse (elf_hash_table (info),
1063 elf_link_renumber_local_hash_table_dynsyms,
1064 &dynsymcount);
1065
1066 if (elf_hash_table (info)->dynlocal)
1067 {
1068 struct elf_link_local_dynamic_entry *p;
1069 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1070 p->dynindx = ++dynsymcount;
1071 }
1072 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1073
1074 elf_link_hash_traverse (elf_hash_table (info),
1075 elf_link_renumber_hash_table_dynsyms,
1076 &dynsymcount);
1077
1078 /* There is an unused NULL entry at the head of the table which we
1079 must account for in our count even if the table is empty since it
1080 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1081 .dynamic section. */
1082 dynsymcount++;
1083
1084 elf_hash_table (info)->dynsymcount = dynsymcount;
1085 return dynsymcount;
1086 }
1087
1088 /* Merge st_other field. */
1089
1090 static void
1091 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1092 unsigned int st_other, asection *sec,
1093 bool definition, bool dynamic)
1094 {
1095 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1096
1097 /* If st_other has a processor-specific meaning, specific
1098 code might be needed here. */
1099 if (bed->elf_backend_merge_symbol_attribute)
1100 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1101 dynamic);
1102
1103 if (!dynamic)
1104 {
1105 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1106 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1107
1108 /* Keep the most constraining visibility. Leave the remainder
1109 of the st_other field to elf_backend_merge_symbol_attribute. */
1110 if (symvis - 1 < hvis - 1)
1111 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1112 }
1113 else if (definition
1114 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1115 && (sec->flags & SEC_READONLY) == 0)
1116 h->protected_def = 1;
1117 }
1118
1119 /* This function is called when we want to merge a new symbol with an
1120 existing symbol. It handles the various cases which arise when we
1121 find a definition in a dynamic object, or when there is already a
1122 definition in a dynamic object. The new symbol is described by
1123 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1124 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1125 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1126 of an old common symbol. We set OVERRIDE if the old symbol is
1127 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1128 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1129 to change. By OK to change, we mean that we shouldn't warn if the
1130 type or size does change. */
1131
1132 static bool
1133 _bfd_elf_merge_symbol (bfd *abfd,
1134 struct bfd_link_info *info,
1135 const char *name,
1136 Elf_Internal_Sym *sym,
1137 asection **psec,
1138 bfd_vma *pvalue,
1139 struct elf_link_hash_entry **sym_hash,
1140 bfd **poldbfd,
1141 bool *pold_weak,
1142 unsigned int *pold_alignment,
1143 bool *skip,
1144 bfd **override,
1145 bool *type_change_ok,
1146 bool *size_change_ok,
1147 bool *matched)
1148 {
1149 asection *sec, *oldsec;
1150 struct elf_link_hash_entry *h;
1151 struct elf_link_hash_entry *hi;
1152 struct elf_link_hash_entry *flip;
1153 int bind;
1154 bfd *oldbfd;
1155 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1156 bool newweak, oldweak, newfunc, oldfunc;
1157 const struct elf_backend_data *bed;
1158 char *new_version;
1159 bool default_sym = *matched;
1160 struct elf_link_hash_table *htab;
1161
1162 *skip = false;
1163 *override = NULL;
1164
1165 sec = *psec;
1166 bind = ELF_ST_BIND (sym->st_info);
1167
1168 if (! bfd_is_und_section (sec))
1169 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1170 else
1171 h = ((struct elf_link_hash_entry *)
1172 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1173 if (h == NULL)
1174 return false;
1175 *sym_hash = h;
1176
1177 bed = get_elf_backend_data (abfd);
1178
1179 /* NEW_VERSION is the symbol version of the new symbol. */
1180 if (h->versioned != unversioned)
1181 {
1182 /* Symbol version is unknown or versioned. */
1183 new_version = strrchr (name, ELF_VER_CHR);
1184 if (new_version)
1185 {
1186 if (h->versioned == unknown)
1187 {
1188 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1189 h->versioned = versioned_hidden;
1190 else
1191 h->versioned = versioned;
1192 }
1193 new_version += 1;
1194 if (new_version[0] == '\0')
1195 new_version = NULL;
1196 }
1197 else
1198 h->versioned = unversioned;
1199 }
1200 else
1201 new_version = NULL;
1202
1203 /* For merging, we only care about real symbols. But we need to make
1204 sure that indirect symbol dynamic flags are updated. */
1205 hi = h;
1206 while (h->root.type == bfd_link_hash_indirect
1207 || h->root.type == bfd_link_hash_warning)
1208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1209
1210 if (!*matched)
1211 {
1212 if (hi == h || h->root.type == bfd_link_hash_new)
1213 *matched = true;
1214 else
1215 {
1216 /* OLD_HIDDEN is true if the existing symbol is only visible
1217 to the symbol with the same symbol version. NEW_HIDDEN is
1218 true if the new symbol is only visible to the symbol with
1219 the same symbol version. */
1220 bool old_hidden = h->versioned == versioned_hidden;
1221 bool new_hidden = hi->versioned == versioned_hidden;
1222 if (!old_hidden && !new_hidden)
1223 /* The new symbol matches the existing symbol if both
1224 aren't hidden. */
1225 *matched = true;
1226 else
1227 {
1228 /* OLD_VERSION is the symbol version of the existing
1229 symbol. */
1230 char *old_version;
1231
1232 if (h->versioned >= versioned)
1233 old_version = strrchr (h->root.root.string,
1234 ELF_VER_CHR) + 1;
1235 else
1236 old_version = NULL;
1237
1238 /* The new symbol matches the existing symbol if they
1239 have the same symbol version. */
1240 *matched = (old_version == new_version
1241 || (old_version != NULL
1242 && new_version != NULL
1243 && strcmp (old_version, new_version) == 0));
1244 }
1245 }
1246 }
1247
1248 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1249 existing symbol. */
1250
1251 oldbfd = NULL;
1252 oldsec = NULL;
1253 switch (h->root.type)
1254 {
1255 default:
1256 break;
1257
1258 case bfd_link_hash_undefined:
1259 case bfd_link_hash_undefweak:
1260 oldbfd = h->root.u.undef.abfd;
1261 break;
1262
1263 case bfd_link_hash_defined:
1264 case bfd_link_hash_defweak:
1265 oldbfd = h->root.u.def.section->owner;
1266 oldsec = h->root.u.def.section;
1267 break;
1268
1269 case bfd_link_hash_common:
1270 oldbfd = h->root.u.c.p->section->owner;
1271 oldsec = h->root.u.c.p->section;
1272 if (pold_alignment)
1273 *pold_alignment = h->root.u.c.p->alignment_power;
1274 break;
1275 }
1276 if (poldbfd && *poldbfd == NULL)
1277 *poldbfd = oldbfd;
1278
1279 /* Differentiate strong and weak symbols. */
1280 newweak = bind == STB_WEAK;
1281 oldweak = (h->root.type == bfd_link_hash_defweak
1282 || h->root.type == bfd_link_hash_undefweak);
1283 if (pold_weak)
1284 *pold_weak = oldweak;
1285
1286 /* We have to check it for every instance since the first few may be
1287 references and not all compilers emit symbol type for undefined
1288 symbols. */
1289 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1290
1291 htab = elf_hash_table (info);
1292
1293 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1294 respectively, is from a dynamic object. */
1295
1296 newdyn = (abfd->flags & DYNAMIC) != 0;
1297
1298 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1299 syms and defined syms in dynamic libraries respectively.
1300 ref_dynamic on the other hand can be set for a symbol defined in
1301 a dynamic library, and def_dynamic may not be set; When the
1302 definition in a dynamic lib is overridden by a definition in the
1303 executable use of the symbol in the dynamic lib becomes a
1304 reference to the executable symbol. */
1305 if (newdyn)
1306 {
1307 if (bfd_is_und_section (sec))
1308 {
1309 if (bind != STB_WEAK)
1310 {
1311 h->ref_dynamic_nonweak = 1;
1312 hi->ref_dynamic_nonweak = 1;
1313 }
1314 }
1315 else
1316 {
1317 /* Update the existing symbol only if they match. */
1318 if (*matched)
1319 h->dynamic_def = 1;
1320 hi->dynamic_def = 1;
1321 }
1322 }
1323
1324 /* If we just created the symbol, mark it as being an ELF symbol.
1325 Other than that, there is nothing to do--there is no merge issue
1326 with a newly defined symbol--so we just return. */
1327
1328 if (h->root.type == bfd_link_hash_new)
1329 {
1330 h->non_elf = 0;
1331 return true;
1332 }
1333
1334 /* In cases involving weak versioned symbols, we may wind up trying
1335 to merge a symbol with itself. Catch that here, to avoid the
1336 confusion that results if we try to override a symbol with
1337 itself. The additional tests catch cases like
1338 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1339 dynamic object, which we do want to handle here. */
1340 if (abfd == oldbfd
1341 && (newweak || oldweak)
1342 && ((abfd->flags & DYNAMIC) == 0
1343 || !h->def_regular))
1344 return true;
1345
1346 olddyn = false;
1347 if (oldbfd != NULL)
1348 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1349 else if (oldsec != NULL)
1350 {
1351 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1352 indices used by MIPS ELF. */
1353 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1354 }
1355
1356 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1357 if (!htab->handling_dt_needed
1358 && oldbfd != NULL
1359 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1360 {
1361 if (newdyn != olddyn)
1362 {
1363 /* Handle a case where plugin_notice won't be called and thus
1364 won't set the non_ir_ref flags on the first pass over
1365 symbols. */
1366 h->root.non_ir_ref_dynamic = true;
1367 hi->root.non_ir_ref_dynamic = true;
1368 }
1369 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1370 && hi->root.type == bfd_link_hash_indirect)
1371 {
1372 /* Change indirect symbol from IR to undefined. */
1373 hi->root.type = bfd_link_hash_undefined;
1374 hi->root.u.undef.abfd = oldbfd;
1375 }
1376 }
1377
1378 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1379 respectively, appear to be a definition rather than reference. */
1380
1381 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1382
1383 olddef = (h->root.type != bfd_link_hash_undefined
1384 && h->root.type != bfd_link_hash_undefweak
1385 && h->root.type != bfd_link_hash_common);
1386
1387 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1388 respectively, appear to be a function. */
1389
1390 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1391 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1392
1393 oldfunc = (h->type != STT_NOTYPE
1394 && bed->is_function_type (h->type));
1395
1396 if (!(newfunc && oldfunc)
1397 && ELF_ST_TYPE (sym->st_info) != h->type
1398 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1399 && h->type != STT_NOTYPE
1400 && (newdef || bfd_is_com_section (sec))
1401 && (olddef || h->root.type == bfd_link_hash_common))
1402 {
1403 /* If creating a default indirect symbol ("foo" or "foo@") from
1404 a dynamic versioned definition ("foo@@") skip doing so if
1405 there is an existing regular definition with a different
1406 type. We don't want, for example, a "time" variable in the
1407 executable overriding a "time" function in a shared library. */
1408 if (newdyn
1409 && !olddyn)
1410 {
1411 *skip = true;
1412 return true;
1413 }
1414
1415 /* When adding a symbol from a regular object file after we have
1416 created indirect symbols, undo the indirection and any
1417 dynamic state. */
1418 if (hi != h
1419 && !newdyn
1420 && olddyn)
1421 {
1422 h = hi;
1423 (*bed->elf_backend_hide_symbol) (info, h, true);
1424 h->forced_local = 0;
1425 h->ref_dynamic = 0;
1426 h->def_dynamic = 0;
1427 h->dynamic_def = 0;
1428 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1429 {
1430 h->root.type = bfd_link_hash_undefined;
1431 h->root.u.undef.abfd = abfd;
1432 }
1433 else
1434 {
1435 h->root.type = bfd_link_hash_new;
1436 h->root.u.undef.abfd = NULL;
1437 }
1438 return true;
1439 }
1440 }
1441
1442 /* Check TLS symbols. We don't check undefined symbols introduced
1443 by "ld -u" which have no type (and oldbfd NULL), and we don't
1444 check symbols from plugins because they also have no type. */
1445 if (oldbfd != NULL
1446 && (oldbfd->flags & BFD_PLUGIN) == 0
1447 && (abfd->flags & BFD_PLUGIN) == 0
1448 && ELF_ST_TYPE (sym->st_info) != h->type
1449 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1450 {
1451 bfd *ntbfd, *tbfd;
1452 bool ntdef, tdef;
1453 asection *ntsec, *tsec;
1454
1455 if (h->type == STT_TLS)
1456 {
1457 ntbfd = abfd;
1458 ntsec = sec;
1459 ntdef = newdef;
1460 tbfd = oldbfd;
1461 tsec = oldsec;
1462 tdef = olddef;
1463 }
1464 else
1465 {
1466 ntbfd = oldbfd;
1467 ntsec = oldsec;
1468 ntdef = olddef;
1469 tbfd = abfd;
1470 tsec = sec;
1471 tdef = newdef;
1472 }
1473
1474 if (tdef && ntdef)
1475 _bfd_error_handler
1476 /* xgettext:c-format */
1477 (_("%s: TLS definition in %pB section %pA "
1478 "mismatches non-TLS definition in %pB section %pA"),
1479 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1480 else if (!tdef && !ntdef)
1481 _bfd_error_handler
1482 /* xgettext:c-format */
1483 (_("%s: TLS reference in %pB "
1484 "mismatches non-TLS reference in %pB"),
1485 h->root.root.string, tbfd, ntbfd);
1486 else if (tdef)
1487 _bfd_error_handler
1488 /* xgettext:c-format */
1489 (_("%s: TLS definition in %pB section %pA "
1490 "mismatches non-TLS reference in %pB"),
1491 h->root.root.string, tbfd, tsec, ntbfd);
1492 else
1493 _bfd_error_handler
1494 /* xgettext:c-format */
1495 (_("%s: TLS reference in %pB "
1496 "mismatches non-TLS definition in %pB section %pA"),
1497 h->root.root.string, tbfd, ntbfd, ntsec);
1498
1499 bfd_set_error (bfd_error_bad_value);
1500 return false;
1501 }
1502
1503 /* If the old symbol has non-default visibility, we ignore the new
1504 definition from a dynamic object. */
1505 if (newdyn
1506 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1507 && !bfd_is_und_section (sec))
1508 {
1509 *skip = true;
1510 /* Make sure this symbol is dynamic. */
1511 h->ref_dynamic = 1;
1512 hi->ref_dynamic = 1;
1513 /* A protected symbol has external availability. Make sure it is
1514 recorded as dynamic.
1515
1516 FIXME: Should we check type and size for protected symbol? */
1517 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1518 return bfd_elf_link_record_dynamic_symbol (info, h);
1519 else
1520 return true;
1521 }
1522 else if (!newdyn
1523 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1524 && h->def_dynamic)
1525 {
1526 /* If the new symbol with non-default visibility comes from a
1527 relocatable file and the old definition comes from a dynamic
1528 object, we remove the old definition. */
1529 if (hi->root.type == bfd_link_hash_indirect)
1530 {
1531 /* Handle the case where the old dynamic definition is
1532 default versioned. We need to copy the symbol info from
1533 the symbol with default version to the normal one if it
1534 was referenced before. */
1535 if (h->ref_regular)
1536 {
1537 hi->root.type = h->root.type;
1538 h->root.type = bfd_link_hash_indirect;
1539 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1540
1541 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1542 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1543 {
1544 /* If the new symbol is hidden or internal, completely undo
1545 any dynamic link state. */
1546 (*bed->elf_backend_hide_symbol) (info, h, true);
1547 h->forced_local = 0;
1548 h->ref_dynamic = 0;
1549 }
1550 else
1551 h->ref_dynamic = 1;
1552
1553 h->def_dynamic = 0;
1554 /* FIXME: Should we check type and size for protected symbol? */
1555 h->size = 0;
1556 h->type = 0;
1557
1558 h = hi;
1559 }
1560 else
1561 h = hi;
1562 }
1563
1564 /* If the old symbol was undefined before, then it will still be
1565 on the undefs list. If the new symbol is undefined or
1566 common, we can't make it bfd_link_hash_new here, because new
1567 undefined or common symbols will be added to the undefs list
1568 by _bfd_generic_link_add_one_symbol. Symbols may not be
1569 added twice to the undefs list. Also, if the new symbol is
1570 undefweak then we don't want to lose the strong undef. */
1571 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1572 {
1573 h->root.type = bfd_link_hash_undefined;
1574 h->root.u.undef.abfd = abfd;
1575 }
1576 else
1577 {
1578 h->root.type = bfd_link_hash_new;
1579 h->root.u.undef.abfd = NULL;
1580 }
1581
1582 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1583 {
1584 /* If the new symbol is hidden or internal, completely undo
1585 any dynamic link state. */
1586 (*bed->elf_backend_hide_symbol) (info, h, true);
1587 h->forced_local = 0;
1588 h->ref_dynamic = 0;
1589 }
1590 else
1591 h->ref_dynamic = 1;
1592 h->def_dynamic = 0;
1593 /* FIXME: Should we check type and size for protected symbol? */
1594 h->size = 0;
1595 h->type = 0;
1596 return true;
1597 }
1598
1599 /* If a new weak symbol definition comes from a regular file and the
1600 old symbol comes from a dynamic library, we treat the new one as
1601 strong. Similarly, an old weak symbol definition from a regular
1602 file is treated as strong when the new symbol comes from a dynamic
1603 library. Further, an old weak symbol from a dynamic library is
1604 treated as strong if the new symbol is from a dynamic library.
1605 This reflects the way glibc's ld.so works.
1606
1607 Also allow a weak symbol to override a linker script symbol
1608 defined by an early pass over the script. This is done so the
1609 linker knows the symbol is defined in an object file, for the
1610 DEFINED script function.
1611
1612 Do this before setting *type_change_ok or *size_change_ok so that
1613 we warn properly when dynamic library symbols are overridden. */
1614
1615 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1616 newweak = false;
1617 if (olddef && newdyn)
1618 oldweak = false;
1619
1620 /* Allow changes between different types of function symbol. */
1621 if (newfunc && oldfunc)
1622 *type_change_ok = true;
1623
1624 /* It's OK to change the type if either the existing symbol or the
1625 new symbol is weak. A type change is also OK if the old symbol
1626 is undefined and the new symbol is defined. */
1627
1628 if (oldweak
1629 || newweak
1630 || (newdef
1631 && h->root.type == bfd_link_hash_undefined))
1632 *type_change_ok = true;
1633
1634 /* It's OK to change the size if either the existing symbol or the
1635 new symbol is weak, or if the old symbol is undefined. */
1636
1637 if (*type_change_ok
1638 || h->root.type == bfd_link_hash_undefined)
1639 *size_change_ok = true;
1640
1641 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1642 symbol, respectively, appears to be a common symbol in a dynamic
1643 object. If a symbol appears in an uninitialized section, and is
1644 not weak, and is not a function, then it may be a common symbol
1645 which was resolved when the dynamic object was created. We want
1646 to treat such symbols specially, because they raise special
1647 considerations when setting the symbol size: if the symbol
1648 appears as a common symbol in a regular object, and the size in
1649 the regular object is larger, we must make sure that we use the
1650 larger size. This problematic case can always be avoided in C,
1651 but it must be handled correctly when using Fortran shared
1652 libraries.
1653
1654 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1655 likewise for OLDDYNCOMMON and OLDDEF.
1656
1657 Note that this test is just a heuristic, and that it is quite
1658 possible to have an uninitialized symbol in a shared object which
1659 is really a definition, rather than a common symbol. This could
1660 lead to some minor confusion when the symbol really is a common
1661 symbol in some regular object. However, I think it will be
1662 harmless. */
1663
1664 if (newdyn
1665 && newdef
1666 && !newweak
1667 && (sec->flags & SEC_ALLOC) != 0
1668 && (sec->flags & SEC_LOAD) == 0
1669 && sym->st_size > 0
1670 && !newfunc)
1671 newdyncommon = true;
1672 else
1673 newdyncommon = false;
1674
1675 if (olddyn
1676 && olddef
1677 && h->root.type == bfd_link_hash_defined
1678 && h->def_dynamic
1679 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1680 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1681 && h->size > 0
1682 && !oldfunc)
1683 olddyncommon = true;
1684 else
1685 olddyncommon = false;
1686
1687 /* We now know everything about the old and new symbols. We ask the
1688 backend to check if we can merge them. */
1689 if (bed->merge_symbol != NULL)
1690 {
1691 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1692 return false;
1693 sec = *psec;
1694 }
1695
1696 /* There are multiple definitions of a normal symbol. Skip the
1697 default symbol as well as definition from an IR object. */
1698 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1699 && !default_sym && h->def_regular
1700 && !(oldbfd != NULL
1701 && (oldbfd->flags & BFD_PLUGIN) != 0
1702 && (abfd->flags & BFD_PLUGIN) == 0))
1703 {
1704 /* Handle a multiple definition. */
1705 (*info->callbacks->multiple_definition) (info, &h->root,
1706 abfd, sec, *pvalue);
1707 *skip = true;
1708 return true;
1709 }
1710
1711 /* If both the old and the new symbols look like common symbols in a
1712 dynamic object, set the size of the symbol to the larger of the
1713 two. */
1714
1715 if (olddyncommon
1716 && newdyncommon
1717 && sym->st_size != h->size)
1718 {
1719 /* Since we think we have two common symbols, issue a multiple
1720 common warning if desired. Note that we only warn if the
1721 size is different. If the size is the same, we simply let
1722 the old symbol override the new one as normally happens with
1723 symbols defined in dynamic objects. */
1724
1725 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1726 bfd_link_hash_common, sym->st_size);
1727 if (sym->st_size > h->size)
1728 h->size = sym->st_size;
1729
1730 *size_change_ok = true;
1731 }
1732
1733 /* If we are looking at a dynamic object, and we have found a
1734 definition, we need to see if the symbol was already defined by
1735 some other object. If so, we want to use the existing
1736 definition, and we do not want to report a multiple symbol
1737 definition error; we do this by clobbering *PSEC to be
1738 bfd_und_section_ptr.
1739
1740 We treat a common symbol as a definition if the symbol in the
1741 shared library is a function, since common symbols always
1742 represent variables; this can cause confusion in principle, but
1743 any such confusion would seem to indicate an erroneous program or
1744 shared library. We also permit a common symbol in a regular
1745 object to override a weak symbol in a shared object. */
1746
1747 if (newdyn
1748 && newdef
1749 && (olddef
1750 || (h->root.type == bfd_link_hash_common
1751 && (newweak || newfunc))))
1752 {
1753 *override = abfd;
1754 newdef = false;
1755 newdyncommon = false;
1756
1757 *psec = sec = bfd_und_section_ptr;
1758 *size_change_ok = true;
1759
1760 /* If we get here when the old symbol is a common symbol, then
1761 we are explicitly letting it override a weak symbol or
1762 function in a dynamic object, and we don't want to warn about
1763 a type change. If the old symbol is a defined symbol, a type
1764 change warning may still be appropriate. */
1765
1766 if (h->root.type == bfd_link_hash_common)
1767 *type_change_ok = true;
1768 }
1769
1770 /* Handle the special case of an old common symbol merging with a
1771 new symbol which looks like a common symbol in a shared object.
1772 We change *PSEC and *PVALUE to make the new symbol look like a
1773 common symbol, and let _bfd_generic_link_add_one_symbol do the
1774 right thing. */
1775
1776 if (newdyncommon
1777 && h->root.type == bfd_link_hash_common)
1778 {
1779 *override = oldbfd;
1780 newdef = false;
1781 newdyncommon = false;
1782 *pvalue = sym->st_size;
1783 *psec = sec = bed->common_section (oldsec);
1784 *size_change_ok = true;
1785 }
1786
1787 /* Skip weak definitions of symbols that are already defined. */
1788 if (newdef && olddef && newweak)
1789 {
1790 /* Don't skip new non-IR weak syms. */
1791 if (!(oldbfd != NULL
1792 && (oldbfd->flags & BFD_PLUGIN) != 0
1793 && (abfd->flags & BFD_PLUGIN) == 0))
1794 {
1795 newdef = false;
1796 *skip = true;
1797 }
1798
1799 /* Merge st_other. If the symbol already has a dynamic index,
1800 but visibility says it should not be visible, turn it into a
1801 local symbol. */
1802 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1803 if (h->dynindx != -1)
1804 switch (ELF_ST_VISIBILITY (h->other))
1805 {
1806 case STV_INTERNAL:
1807 case STV_HIDDEN:
1808 (*bed->elf_backend_hide_symbol) (info, h, true);
1809 break;
1810 }
1811 }
1812
1813 /* If the old symbol is from a dynamic object, and the new symbol is
1814 a definition which is not from a dynamic object, then the new
1815 symbol overrides the old symbol. Symbols from regular files
1816 always take precedence over symbols from dynamic objects, even if
1817 they are defined after the dynamic object in the link.
1818
1819 As above, we again permit a common symbol in a regular object to
1820 override a definition in a shared object if the shared object
1821 symbol is a function or is weak. */
1822
1823 flip = NULL;
1824 if (!newdyn
1825 && (newdef
1826 || (bfd_is_com_section (sec)
1827 && (oldweak || oldfunc)))
1828 && olddyn
1829 && olddef
1830 && h->def_dynamic)
1831 {
1832 /* Change the hash table entry to undefined, and let
1833 _bfd_generic_link_add_one_symbol do the right thing with the
1834 new definition. */
1835
1836 h->root.type = bfd_link_hash_undefined;
1837 h->root.u.undef.abfd = h->root.u.def.section->owner;
1838 *size_change_ok = true;
1839
1840 olddef = false;
1841 olddyncommon = false;
1842
1843 /* We again permit a type change when a common symbol may be
1844 overriding a function. */
1845
1846 if (bfd_is_com_section (sec))
1847 {
1848 if (oldfunc)
1849 {
1850 /* If a common symbol overrides a function, make sure
1851 that it isn't defined dynamically nor has type
1852 function. */
1853 h->def_dynamic = 0;
1854 h->type = STT_NOTYPE;
1855 }
1856 *type_change_ok = true;
1857 }
1858
1859 if (hi->root.type == bfd_link_hash_indirect)
1860 flip = hi;
1861 else
1862 /* This union may have been set to be non-NULL when this symbol
1863 was seen in a dynamic object. We must force the union to be
1864 NULL, so that it is correct for a regular symbol. */
1865 h->verinfo.vertree = NULL;
1866 }
1867
1868 /* Handle the special case of a new common symbol merging with an
1869 old symbol that looks like it might be a common symbol defined in
1870 a shared object. Note that we have already handled the case in
1871 which a new common symbol should simply override the definition
1872 in the shared library. */
1873
1874 if (! newdyn
1875 && bfd_is_com_section (sec)
1876 && olddyncommon)
1877 {
1878 /* It would be best if we could set the hash table entry to a
1879 common symbol, but we don't know what to use for the section
1880 or the alignment. */
1881 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1882 bfd_link_hash_common, sym->st_size);
1883
1884 /* If the presumed common symbol in the dynamic object is
1885 larger, pretend that the new symbol has its size. */
1886
1887 if (h->size > *pvalue)
1888 *pvalue = h->size;
1889
1890 /* We need to remember the alignment required by the symbol
1891 in the dynamic object. */
1892 BFD_ASSERT (pold_alignment);
1893 *pold_alignment = h->root.u.def.section->alignment_power;
1894
1895 olddef = false;
1896 olddyncommon = false;
1897
1898 h->root.type = bfd_link_hash_undefined;
1899 h->root.u.undef.abfd = h->root.u.def.section->owner;
1900
1901 *size_change_ok = true;
1902 *type_change_ok = true;
1903
1904 if (hi->root.type == bfd_link_hash_indirect)
1905 flip = hi;
1906 else
1907 h->verinfo.vertree = NULL;
1908 }
1909
1910 if (flip != NULL)
1911 {
1912 /* Handle the case where we had a versioned symbol in a dynamic
1913 library and now find a definition in a normal object. In this
1914 case, we make the versioned symbol point to the normal one. */
1915 flip->root.type = h->root.type;
1916 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1917 h->root.type = bfd_link_hash_indirect;
1918 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1919 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1920 if (h->def_dynamic)
1921 {
1922 h->def_dynamic = 0;
1923 flip->ref_dynamic = 1;
1924 }
1925 }
1926
1927 return true;
1928 }
1929
1930 /* This function is called to create an indirect symbol from the
1931 default for the symbol with the default version if needed. The
1932 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1933 set DYNSYM if the new indirect symbol is dynamic. */
1934
1935 static bool
1936 _bfd_elf_add_default_symbol (bfd *abfd,
1937 struct bfd_link_info *info,
1938 struct elf_link_hash_entry *h,
1939 const char *name,
1940 Elf_Internal_Sym *sym,
1941 asection *sec,
1942 bfd_vma value,
1943 bfd **poldbfd,
1944 bool *dynsym)
1945 {
1946 bool type_change_ok;
1947 bool size_change_ok;
1948 bool skip;
1949 char *shortname;
1950 struct elf_link_hash_entry *hi;
1951 struct bfd_link_hash_entry *bh;
1952 const struct elf_backend_data *bed;
1953 bool collect;
1954 bool dynamic;
1955 bfd *override;
1956 char *p;
1957 size_t len, shortlen;
1958 asection *tmp_sec;
1959 bool matched;
1960
1961 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1962 return true;
1963
1964 /* If this symbol has a version, and it is the default version, we
1965 create an indirect symbol from the default name to the fully
1966 decorated name. This will cause external references which do not
1967 specify a version to be bound to this version of the symbol. */
1968 p = strchr (name, ELF_VER_CHR);
1969 if (h->versioned == unknown)
1970 {
1971 if (p == NULL)
1972 {
1973 h->versioned = unversioned;
1974 return true;
1975 }
1976 else
1977 {
1978 if (p[1] != ELF_VER_CHR)
1979 {
1980 h->versioned = versioned_hidden;
1981 return true;
1982 }
1983 else
1984 h->versioned = versioned;
1985 }
1986 }
1987 else
1988 {
1989 /* PR ld/19073: We may see an unversioned definition after the
1990 default version. */
1991 if (p == NULL)
1992 return true;
1993 }
1994
1995 bed = get_elf_backend_data (abfd);
1996 collect = bed->collect;
1997 dynamic = (abfd->flags & DYNAMIC) != 0;
1998
1999 shortlen = p - name;
2000 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
2001 if (shortname == NULL)
2002 return false;
2003 memcpy (shortname, name, shortlen);
2004 shortname[shortlen] = '\0';
2005
2006 /* We are going to create a new symbol. Merge it with any existing
2007 symbol with this name. For the purposes of the merge, act as
2008 though we were defining the symbol we just defined, although we
2009 actually going to define an indirect symbol. */
2010 type_change_ok = false;
2011 size_change_ok = false;
2012 matched = true;
2013 tmp_sec = sec;
2014 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2015 &hi, poldbfd, NULL, NULL, &skip, &override,
2016 &type_change_ok, &size_change_ok, &matched))
2017 return false;
2018
2019 if (skip)
2020 goto nondefault;
2021
2022 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
2023 {
2024 /* If the undecorated symbol will have a version added by a
2025 script different to H, then don't indirect to/from the
2026 undecorated symbol. This isn't ideal because we may not yet
2027 have seen symbol versions, if given by a script on the
2028 command line rather than via --version-script. */
2029 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2030 {
2031 bool hide;
2032
2033 hi->verinfo.vertree
2034 = bfd_find_version_for_sym (info->version_info,
2035 hi->root.root.string, &hide);
2036 if (hi->verinfo.vertree != NULL && hide)
2037 {
2038 (*bed->elf_backend_hide_symbol) (info, hi, true);
2039 goto nondefault;
2040 }
2041 }
2042 if (hi->verinfo.vertree != NULL
2043 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2044 goto nondefault;
2045 }
2046
2047 if (! override)
2048 {
2049 /* Add the default symbol if not performing a relocatable link. */
2050 if (! bfd_link_relocatable (info))
2051 {
2052 bh = &hi->root;
2053 if (bh->type == bfd_link_hash_defined
2054 && bh->u.def.section->owner != NULL
2055 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2056 {
2057 /* Mark the previous definition from IR object as
2058 undefined so that the generic linker will override
2059 it. */
2060 bh->type = bfd_link_hash_undefined;
2061 bh->u.undef.abfd = bh->u.def.section->owner;
2062 }
2063 if (! (_bfd_generic_link_add_one_symbol
2064 (info, abfd, shortname, BSF_INDIRECT,
2065 bfd_ind_section_ptr,
2066 0, name, false, collect, &bh)))
2067 return false;
2068 hi = (struct elf_link_hash_entry *) bh;
2069 }
2070 }
2071 else
2072 {
2073 /* In this case the symbol named SHORTNAME is overriding the
2074 indirect symbol we want to add. We were planning on making
2075 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2076 is the name without a version. NAME is the fully versioned
2077 name, and it is the default version.
2078
2079 Overriding means that we already saw a definition for the
2080 symbol SHORTNAME in a regular object, and it is overriding
2081 the symbol defined in the dynamic object.
2082
2083 When this happens, we actually want to change NAME, the
2084 symbol we just added, to refer to SHORTNAME. This will cause
2085 references to NAME in the shared object to become references
2086 to SHORTNAME in the regular object. This is what we expect
2087 when we override a function in a shared object: that the
2088 references in the shared object will be mapped to the
2089 definition in the regular object. */
2090
2091 while (hi->root.type == bfd_link_hash_indirect
2092 || hi->root.type == bfd_link_hash_warning)
2093 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2094
2095 h->root.type = bfd_link_hash_indirect;
2096 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2097 if (h->def_dynamic)
2098 {
2099 h->def_dynamic = 0;
2100 hi->ref_dynamic = 1;
2101 if (hi->ref_regular
2102 || hi->def_regular)
2103 {
2104 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2105 return false;
2106 }
2107 }
2108
2109 /* Now set HI to H, so that the following code will set the
2110 other fields correctly. */
2111 hi = h;
2112 }
2113
2114 /* Check if HI is a warning symbol. */
2115 if (hi->root.type == bfd_link_hash_warning)
2116 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2117
2118 /* If there is a duplicate definition somewhere, then HI may not
2119 point to an indirect symbol. We will have reported an error to
2120 the user in that case. */
2121
2122 if (hi->root.type == bfd_link_hash_indirect)
2123 {
2124 struct elf_link_hash_entry *ht;
2125
2126 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2127 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2128
2129 /* If we first saw a reference to SHORTNAME with non-default
2130 visibility, merge that visibility to the @@VER symbol. */
2131 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2132
2133 /* A reference to the SHORTNAME symbol from a dynamic library
2134 will be satisfied by the versioned symbol at runtime. In
2135 effect, we have a reference to the versioned symbol. */
2136 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2137 hi->dynamic_def |= ht->dynamic_def;
2138
2139 /* See if the new flags lead us to realize that the symbol must
2140 be dynamic. */
2141 if (! *dynsym)
2142 {
2143 if (! dynamic)
2144 {
2145 if (! bfd_link_executable (info)
2146 || hi->def_dynamic
2147 || hi->ref_dynamic)
2148 *dynsym = true;
2149 }
2150 else
2151 {
2152 if (hi->ref_regular)
2153 *dynsym = true;
2154 }
2155 }
2156 }
2157
2158 /* We also need to define an indirection from the nondefault version
2159 of the symbol. */
2160
2161 nondefault:
2162 len = strlen (name);
2163 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2164 if (shortname == NULL)
2165 return false;
2166 memcpy (shortname, name, shortlen);
2167 memcpy (shortname + shortlen, p + 1, len - shortlen);
2168
2169 /* Once again, merge with any existing symbol. */
2170 type_change_ok = false;
2171 size_change_ok = false;
2172 tmp_sec = sec;
2173 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2174 &hi, poldbfd, NULL, NULL, &skip, &override,
2175 &type_change_ok, &size_change_ok, &matched))
2176 return false;
2177
2178 if (skip)
2179 {
2180 if (!dynamic
2181 && h->root.type == bfd_link_hash_defweak
2182 && hi->root.type == bfd_link_hash_defined)
2183 {
2184 /* We are handling a weak sym@@ver and attempting to define
2185 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2186 new weak sym@ver because there is already a strong sym@ver.
2187 However, sym@ver and sym@@ver are really the same symbol.
2188 The existing strong sym@ver ought to override sym@@ver. */
2189 h->root.type = bfd_link_hash_defined;
2190 h->root.u.def.section = hi->root.u.def.section;
2191 h->root.u.def.value = hi->root.u.def.value;
2192 hi->root.type = bfd_link_hash_indirect;
2193 hi->root.u.i.link = &h->root;
2194 }
2195 else
2196 return true;
2197 }
2198 else if (override)
2199 {
2200 /* Here SHORTNAME is a versioned name, so we don't expect to see
2201 the type of override we do in the case above unless it is
2202 overridden by a versioned definition. */
2203 if (hi->root.type != bfd_link_hash_defined
2204 && hi->root.type != bfd_link_hash_defweak)
2205 _bfd_error_handler
2206 /* xgettext:c-format */
2207 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2208 abfd, shortname);
2209 return true;
2210 }
2211 else
2212 {
2213 bh = &hi->root;
2214 if (! (_bfd_generic_link_add_one_symbol
2215 (info, abfd, shortname, BSF_INDIRECT,
2216 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2217 return false;
2218 hi = (struct elf_link_hash_entry *) bh;
2219 }
2220
2221 /* If there is a duplicate definition somewhere, then HI may not
2222 point to an indirect symbol. We will have reported an error
2223 to the user in that case. */
2224 if (hi->root.type == bfd_link_hash_indirect)
2225 {
2226 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2227 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2228 hi->dynamic_def |= h->dynamic_def;
2229
2230 /* If we first saw a reference to @VER symbol with
2231 non-default visibility, merge that visibility to the
2232 @@VER symbol. */
2233 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2234
2235 /* See if the new flags lead us to realize that the symbol
2236 must be dynamic. */
2237 if (! *dynsym)
2238 {
2239 if (! dynamic)
2240 {
2241 if (! bfd_link_executable (info)
2242 || hi->ref_dynamic)
2243 *dynsym = true;
2244 }
2245 else
2246 {
2247 if (hi->ref_regular)
2248 *dynsym = true;
2249 }
2250 }
2251 }
2252
2253 return true;
2254 }
2255 \f
2256 /* This routine is used to export all defined symbols into the dynamic
2257 symbol table. It is called via elf_link_hash_traverse. */
2258
2259 static bool
2260 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2261 {
2262 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2263
2264 /* Ignore indirect symbols. These are added by the versioning code. */
2265 if (h->root.type == bfd_link_hash_indirect)
2266 return true;
2267
2268 /* Ignore this if we won't export it. */
2269 if (!eif->info->export_dynamic && !h->dynamic)
2270 return true;
2271
2272 if (h->dynindx == -1
2273 && (h->def_regular || h->ref_regular)
2274 && ! bfd_hide_sym_by_version (eif->info->version_info,
2275 h->root.root.string))
2276 {
2277 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2278 {
2279 eif->failed = true;
2280 return false;
2281 }
2282 }
2283
2284 return true;
2285 }
2286 \f
2287 /* Return the glibc version reference if VERSION_DEP is added to the
2288 list of glibc version dependencies successfully. VERSION_DEP will
2289 be put into the .gnu.version_r section. GLIBC_MINOR_BASE is the
2290 pointer to the glibc minor base version. */
2291
2292 static Elf_Internal_Verneed *
2293 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2294 Elf_Internal_Verneed *glibc_verref,
2295 const char *version_dep,
2296 int *glibc_minor_base)
2297 {
2298 Elf_Internal_Verneed *t;
2299 Elf_Internal_Vernaux *a;
2300 size_t amt;
2301 int minor_version = -1;
2302
2303 if (glibc_verref != NULL)
2304 {
2305 t = glibc_verref;
2306
2307 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2308 {
2309 /* Return if VERSION_DEP dependency has been added. */
2310 if (a->vna_nodename == version_dep
2311 || strcmp (a->vna_nodename, version_dep) == 0)
2312 return t;
2313 }
2314 }
2315 else
2316 {
2317 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2318 t != NULL;
2319 t = t->vn_nextref)
2320 {
2321 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2322 if (soname != NULL && startswith (soname, "libc.so."))
2323 break;
2324 }
2325
2326 /* Skip the shared library if it isn't libc.so. */
2327 if (t == NULL)
2328 return t;
2329
2330 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2331 {
2332 /* Return if VERSION_DEP dependency has been added. */
2333 if (a->vna_nodename == version_dep
2334 || strcmp (a->vna_nodename, version_dep) == 0)
2335 return t;
2336
2337 /* Check if libc.so provides GLIBC_2.XX version. */
2338 if (startswith (a->vna_nodename, "GLIBC_2."))
2339 {
2340 minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2341 if (minor_version < *glibc_minor_base)
2342 *glibc_minor_base = minor_version;
2343 }
2344 }
2345
2346 /* Skip if it isn't linked against glibc. */
2347 if (minor_version < 0)
2348 return NULL;
2349 }
2350
2351 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */
2352 if (startswith (version_dep, "GLIBC_2."))
2353 {
2354 minor_version = strtol (version_dep + 8, NULL, 10);
2355 if (minor_version <= *glibc_minor_base)
2356 return NULL;
2357 }
2358
2359 amt = sizeof *a;
2360 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2361 if (a == NULL)
2362 {
2363 rinfo->failed = true;
2364 return NULL;
2365 }
2366
2367 a->vna_nodename = version_dep;
2368 a->vna_flags = 0;
2369 a->vna_nextptr = t->vn_auxptr;
2370 a->vna_other = rinfo->vers + 1;
2371 ++rinfo->vers;
2372
2373 t->vn_auxptr = a;
2374
2375 return t;
2376 }
2377
2378 /* Add VERSION_DEP to the list of version dependencies when linked
2379 against glibc. */
2380
2381 void
2382 _bfd_elf_link_add_glibc_version_dependency
2383 (struct elf_find_verdep_info *rinfo,
2384 const char *version_dep[])
2385 {
2386 Elf_Internal_Verneed *t = NULL;
2387 int glibc_minor_base = INT_MAX;
2388
2389 do
2390 {
2391 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
2392 &glibc_minor_base);
2393 /* Return if there is no glibc version reference. */
2394 if (t == NULL)
2395 return;
2396 version_dep++;
2397 }
2398 while (*version_dep != NULL);
2399 }
2400
2401 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2402 linked against glibc. */
2403
2404 void
2405 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2406 {
2407 if (rinfo->info->enable_dt_relr)
2408 {
2409 const char *version[] =
2410 {
2411 "GLIBC_ABI_DT_RELR",
2412 NULL
2413 };
2414 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2415 }
2416 }
2417
2418 /* Look through the symbols which are defined in other shared
2419 libraries and referenced here. Update the list of version
2420 dependencies. This will be put into the .gnu.version_r section.
2421 This function is called via elf_link_hash_traverse. */
2422
2423 static bool
2424 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2425 void *data)
2426 {
2427 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2428 Elf_Internal_Verneed *t;
2429 Elf_Internal_Vernaux *a;
2430 size_t amt;
2431
2432 /* We only care about symbols defined in shared objects with version
2433 information. */
2434 if (!h->def_dynamic
2435 || h->def_regular
2436 || h->dynindx == -1
2437 || h->verinfo.verdef == NULL
2438 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2439 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2440 return true;
2441
2442 /* See if we already know about this version. */
2443 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2444 t != NULL;
2445 t = t->vn_nextref)
2446 {
2447 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2448 continue;
2449
2450 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2451 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2452 return true;
2453
2454 break;
2455 }
2456
2457 /* This is a new version. Add it to tree we are building. */
2458
2459 if (t == NULL)
2460 {
2461 amt = sizeof *t;
2462 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2463 if (t == NULL)
2464 {
2465 rinfo->failed = true;
2466 return false;
2467 }
2468
2469 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2470 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2471 elf_tdata (rinfo->info->output_bfd)->verref = t;
2472 }
2473
2474 amt = sizeof *a;
2475 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2476 if (a == NULL)
2477 {
2478 rinfo->failed = true;
2479 return false;
2480 }
2481
2482 /* Note that we are copying a string pointer here, and testing it
2483 above. If bfd_elf_string_from_elf_section is ever changed to
2484 discard the string data when low in memory, this will have to be
2485 fixed. */
2486 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2487
2488 a->vna_flags = h->verinfo.verdef->vd_flags;
2489 a->vna_nextptr = t->vn_auxptr;
2490
2491 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2492 ++rinfo->vers;
2493
2494 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2495
2496 t->vn_auxptr = a;
2497
2498 return true;
2499 }
2500
2501 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2502 hidden. Set *T_P to NULL if there is no match. */
2503
2504 static bool
2505 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2506 struct elf_link_hash_entry *h,
2507 const char *version_p,
2508 struct bfd_elf_version_tree **t_p,
2509 bool *hide)
2510 {
2511 struct bfd_elf_version_tree *t;
2512
2513 /* Look for the version. If we find it, it is no longer weak. */
2514 for (t = info->version_info; t != NULL; t = t->next)
2515 {
2516 if (strcmp (t->name, version_p) == 0)
2517 {
2518 size_t len;
2519 char *alc;
2520 struct bfd_elf_version_expr *d;
2521
2522 len = version_p - h->root.root.string;
2523 alc = (char *) bfd_malloc (len);
2524 if (alc == NULL)
2525 return false;
2526 memcpy (alc, h->root.root.string, len - 1);
2527 alc[len - 1] = '\0';
2528 if (alc[len - 2] == ELF_VER_CHR)
2529 alc[len - 2] = '\0';
2530
2531 h->verinfo.vertree = t;
2532 t->used = true;
2533 d = NULL;
2534
2535 if (t->globals.list != NULL)
2536 d = (*t->match) (&t->globals, NULL, alc);
2537
2538 /* See if there is anything to force this symbol to
2539 local scope. */
2540 if (d == NULL && t->locals.list != NULL)
2541 {
2542 d = (*t->match) (&t->locals, NULL, alc);
2543 if (d != NULL
2544 && h->dynindx != -1
2545 && ! info->export_dynamic)
2546 *hide = true;
2547 }
2548
2549 free (alc);
2550 break;
2551 }
2552 }
2553
2554 *t_p = t;
2555
2556 return true;
2557 }
2558
2559 /* Return TRUE if the symbol H is hidden by version script. */
2560
2561 bool
2562 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2563 struct elf_link_hash_entry *h)
2564 {
2565 const char *p;
2566 bool hide = false;
2567 const struct elf_backend_data *bed
2568 = get_elf_backend_data (info->output_bfd);
2569
2570 /* Version script only hides symbols defined in regular objects. */
2571 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2572 return true;
2573
2574 p = strchr (h->root.root.string, ELF_VER_CHR);
2575 if (p != NULL && h->verinfo.vertree == NULL)
2576 {
2577 struct bfd_elf_version_tree *t;
2578
2579 ++p;
2580 if (*p == ELF_VER_CHR)
2581 ++p;
2582
2583 if (*p != '\0'
2584 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2585 && hide)
2586 {
2587 if (hide)
2588 (*bed->elf_backend_hide_symbol) (info, h, true);
2589 return true;
2590 }
2591 }
2592
2593 /* If we don't have a version for this symbol, see if we can find
2594 something. */
2595 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2596 {
2597 h->verinfo.vertree
2598 = bfd_find_version_for_sym (info->version_info,
2599 h->root.root.string, &hide);
2600 if (h->verinfo.vertree != NULL && hide)
2601 {
2602 (*bed->elf_backend_hide_symbol) (info, h, true);
2603 return true;
2604 }
2605 }
2606
2607 return false;
2608 }
2609
2610 /* Figure out appropriate versions for all the symbols. We may not
2611 have the version number script until we have read all of the input
2612 files, so until that point we don't know which symbols should be
2613 local. This function is called via elf_link_hash_traverse. */
2614
2615 static bool
2616 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2617 {
2618 struct elf_info_failed *sinfo;
2619 struct bfd_link_info *info;
2620 const struct elf_backend_data *bed;
2621 struct elf_info_failed eif;
2622 char *p;
2623 bool hide;
2624
2625 sinfo = (struct elf_info_failed *) data;
2626 info = sinfo->info;
2627
2628 /* Fix the symbol flags. */
2629 eif.failed = false;
2630 eif.info = info;
2631 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2632 {
2633 if (eif.failed)
2634 sinfo->failed = true;
2635 return false;
2636 }
2637
2638 bed = get_elf_backend_data (info->output_bfd);
2639
2640 /* We only need version numbers for symbols defined in regular
2641 objects. */
2642 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2643 {
2644 /* Hide symbols defined in discarded input sections. */
2645 if ((h->root.type == bfd_link_hash_defined
2646 || h->root.type == bfd_link_hash_defweak)
2647 && discarded_section (h->root.u.def.section))
2648 (*bed->elf_backend_hide_symbol) (info, h, true);
2649 return true;
2650 }
2651
2652 hide = false;
2653 p = strchr (h->root.root.string, ELF_VER_CHR);
2654 if (p != NULL && h->verinfo.vertree == NULL)
2655 {
2656 struct bfd_elf_version_tree *t;
2657
2658 ++p;
2659 if (*p == ELF_VER_CHR)
2660 ++p;
2661
2662 /* If there is no version string, we can just return out. */
2663 if (*p == '\0')
2664 return true;
2665
2666 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2667 {
2668 sinfo->failed = true;
2669 return false;
2670 }
2671
2672 if (hide)
2673 (*bed->elf_backend_hide_symbol) (info, h, true);
2674
2675 /* If we are building an application, we need to create a
2676 version node for this version. */
2677 if (t == NULL && bfd_link_executable (info))
2678 {
2679 struct bfd_elf_version_tree **pp;
2680 int version_index;
2681
2682 /* If we aren't going to export this symbol, we don't need
2683 to worry about it. */
2684 if (h->dynindx == -1)
2685 return true;
2686
2687 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2688 sizeof *t);
2689 if (t == NULL)
2690 {
2691 sinfo->failed = true;
2692 return false;
2693 }
2694
2695 t->name = p;
2696 t->name_indx = (unsigned int) -1;
2697 t->used = true;
2698
2699 version_index = 1;
2700 /* Don't count anonymous version tag. */
2701 if (sinfo->info->version_info != NULL
2702 && sinfo->info->version_info->vernum == 0)
2703 version_index = 0;
2704 for (pp = &sinfo->info->version_info;
2705 *pp != NULL;
2706 pp = &(*pp)->next)
2707 ++version_index;
2708 t->vernum = version_index;
2709
2710 *pp = t;
2711
2712 h->verinfo.vertree = t;
2713 }
2714 else if (t == NULL)
2715 {
2716 /* We could not find the version for a symbol when
2717 generating a shared archive. Return an error. */
2718 _bfd_error_handler
2719 /* xgettext:c-format */
2720 (_("%pB: version node not found for symbol %s"),
2721 info->output_bfd, h->root.root.string);
2722 bfd_set_error (bfd_error_bad_value);
2723 sinfo->failed = true;
2724 return false;
2725 }
2726 }
2727
2728 /* If we don't have a version for this symbol, see if we can find
2729 something. */
2730 if (!hide
2731 && h->verinfo.vertree == NULL
2732 && sinfo->info->version_info != NULL)
2733 {
2734 h->verinfo.vertree
2735 = bfd_find_version_for_sym (sinfo->info->version_info,
2736 h->root.root.string, &hide);
2737 if (h->verinfo.vertree != NULL && hide)
2738 (*bed->elf_backend_hide_symbol) (info, h, true);
2739 }
2740
2741 return true;
2742 }
2743 \f
2744 /* Read and swap the relocs from the section indicated by SHDR. This
2745 may be either a REL or a RELA section. The relocations are
2746 translated into RELA relocations and stored in INTERNAL_RELOCS,
2747 which should have already been allocated to contain enough space.
2748 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2749 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2750 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2751 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2752 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2753
2754 Returns FALSE if something goes wrong. */
2755
2756 static bool
2757 elf_link_read_relocs_from_section (bfd *abfd,
2758 const asection *sec,
2759 Elf_Internal_Shdr *shdr,
2760 void **external_relocs_addr,
2761 size_t *external_relocs_size,
2762 Elf_Internal_Rela *internal_relocs)
2763 {
2764 const struct elf_backend_data *bed;
2765 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2766 const bfd_byte *erela;
2767 const bfd_byte *erelaend;
2768 Elf_Internal_Rela *irela;
2769 Elf_Internal_Shdr *symtab_hdr;
2770 size_t nsyms;
2771 void *external_relocs = *external_relocs_addr;
2772
2773 /* Position ourselves at the start of the section. */
2774 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2775 return false;
2776
2777 /* Read the relocations. */
2778 *external_relocs_size = shdr->sh_size;
2779 if (!_bfd_mmap_read_temporary (&external_relocs,
2780 external_relocs_size,
2781 external_relocs_addr, abfd, true))
2782 return false;
2783
2784 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2785 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2786
2787 bed = get_elf_backend_data (abfd);
2788
2789 /* Convert the external relocations to the internal format. */
2790 if (shdr->sh_entsize == bed->s->sizeof_rel)
2791 swap_in = bed->s->swap_reloc_in;
2792 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2793 swap_in = bed->s->swap_reloca_in;
2794 else
2795 {
2796 bfd_set_error (bfd_error_wrong_format);
2797 return false;
2798 }
2799
2800 erela = (const bfd_byte *) external_relocs;
2801 /* Setting erelaend like this and comparing with <= handles case of
2802 a fuzzed object with sh_size not a multiple of sh_entsize. */
2803 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2804 irela = internal_relocs;
2805 while (erela <= erelaend)
2806 {
2807 bfd_vma r_symndx;
2808
2809 (*swap_in) (abfd, erela, irela);
2810 r_symndx = ELF32_R_SYM (irela->r_info);
2811 if (bed->s->arch_size == 64)
2812 r_symndx >>= 24;
2813 if (nsyms > 0)
2814 {
2815 if ((size_t) r_symndx >= nsyms)
2816 {
2817 _bfd_error_handler
2818 /* xgettext:c-format */
2819 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2820 " for offset %#" PRIx64 " in section `%pA'"),
2821 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2822 (uint64_t) irela->r_offset, sec);
2823 bfd_set_error (bfd_error_bad_value);
2824 return false;
2825 }
2826 }
2827 else if (r_symndx != STN_UNDEF)
2828 {
2829 _bfd_error_handler
2830 /* xgettext:c-format */
2831 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2832 " for offset %#" PRIx64 " in section `%pA'"
2833 " when the object file has no symbol table"),
2834 abfd, (uint64_t) r_symndx,
2835 (uint64_t) irela->r_offset, sec);
2836 bfd_set_error (bfd_error_bad_value);
2837 return false;
2838 }
2839 irela += bed->s->int_rels_per_ext_rel;
2840 erela += shdr->sh_entsize;
2841 }
2842
2843 return true;
2844 }
2845
2846 /* Read and swap the relocs for a section O. They may have been
2847 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2848 not NULL, they are used as buffers to read into. They are known to
2849 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2850 the return value is allocated using either malloc or bfd_alloc,
2851 according to the KEEP_MEMORY argument. If O has two relocation
2852 sections (both REL and RELA relocations), then the REL_HDR
2853 relocations will appear first in INTERNAL_RELOCS, followed by the
2854 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2855 update cache_size. */
2856
2857 Elf_Internal_Rela *
2858 _bfd_elf_link_info_read_relocs (bfd *abfd,
2859 struct bfd_link_info *info,
2860 const asection *o,
2861 void *external_relocs,
2862 Elf_Internal_Rela *internal_relocs,
2863 bool keep_memory)
2864 {
2865 void *alloc1 = NULL;
2866 size_t alloc1_size;
2867 Elf_Internal_Rela *alloc2 = NULL;
2868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2869 struct bfd_elf_section_data *esdo = elf_section_data (o);
2870 Elf_Internal_Rela *internal_rela_relocs;
2871
2872 if (esdo->relocs != NULL)
2873 return esdo->relocs;
2874
2875 if (o->reloc_count == 0)
2876 return NULL;
2877
2878 if (internal_relocs == NULL)
2879 {
2880 bfd_size_type size;
2881
2882 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2883 if (keep_memory && info)
2884 info->cache_size += size;
2885 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2886 if (internal_relocs == NULL)
2887 return NULL;
2888 }
2889
2890 alloc1 = external_relocs;
2891 internal_rela_relocs = internal_relocs;
2892 if (esdo->rel.hdr)
2893 {
2894 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2895 &alloc1, &alloc1_size,
2896 internal_relocs))
2897 goto error_return;
2898 external_relocs = (((bfd_byte *) external_relocs)
2899 + esdo->rel.hdr->sh_size);
2900 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2901 * bed->s->int_rels_per_ext_rel);
2902 }
2903
2904 if (esdo->rela.hdr
2905 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2906 &alloc1, &alloc1_size,
2907 internal_rela_relocs)))
2908 goto error_return;
2909
2910 /* Cache the results for next time, if we can. */
2911 if (keep_memory)
2912 esdo->relocs = internal_relocs;
2913
2914 _bfd_munmap_temporary (alloc1, alloc1_size);
2915
2916 /* Don't free alloc2, since if it was allocated we are passing it
2917 back (under the name of internal_relocs). */
2918
2919 return internal_relocs;
2920
2921 error_return:
2922 _bfd_munmap_temporary (alloc1, alloc1_size);
2923 free (alloc2);
2924 return NULL;
2925 }
2926
2927 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2928 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2929 struct bfd_link_info. */
2930
2931 Elf_Internal_Rela *
2932 _bfd_elf_link_read_relocs (bfd *abfd,
2933 const asection *o,
2934 void *external_relocs,
2935 Elf_Internal_Rela *internal_relocs,
2936 bool keep_memory)
2937 {
2938 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2939 internal_relocs, keep_memory);
2940
2941 }
2942
2943 /* Compute the size of, and allocate space for, REL_HDR which is the
2944 section header for a section containing relocations for O. */
2945
2946 static bool
2947 _bfd_elf_link_size_reloc_section (bfd *abfd,
2948 struct bfd_elf_section_reloc_data *reldata)
2949 {
2950 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2951
2952 /* That allows us to calculate the size of the section. */
2953 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2954
2955 /* The contents field must last into write_object_contents, so we
2956 allocate it with bfd_alloc rather than malloc. Also since we
2957 cannot be sure that the contents will actually be filled in,
2958 we zero the allocated space. */
2959 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2960 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2961 return false;
2962
2963 if (reldata->hashes == NULL && reldata->count)
2964 {
2965 struct elf_link_hash_entry **p;
2966
2967 p = ((struct elf_link_hash_entry **)
2968 bfd_zmalloc (reldata->count * sizeof (*p)));
2969 if (p == NULL)
2970 return false;
2971
2972 reldata->hashes = p;
2973 }
2974
2975 return true;
2976 }
2977
2978 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2979 originated from the section given by INPUT_REL_HDR) to the
2980 OUTPUT_BFD. */
2981
2982 bool
2983 _bfd_elf_link_output_relocs (bfd *output_bfd,
2984 asection *input_section,
2985 Elf_Internal_Shdr *input_rel_hdr,
2986 Elf_Internal_Rela *internal_relocs,
2987 struct elf_link_hash_entry **rel_hash)
2988 {
2989 Elf_Internal_Rela *irela;
2990 Elf_Internal_Rela *irelaend;
2991 bfd_byte *erel;
2992 struct bfd_elf_section_reloc_data *output_reldata;
2993 asection *output_section;
2994 const struct elf_backend_data *bed;
2995 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2996 struct bfd_elf_section_data *esdo;
2997
2998 output_section = input_section->output_section;
2999
3000 bed = get_elf_backend_data (output_bfd);
3001 esdo = elf_section_data (output_section);
3002 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3003 {
3004 output_reldata = &esdo->rel;
3005 swap_out = bed->s->swap_reloc_out;
3006 }
3007 else if (esdo->rela.hdr
3008 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
3009 {
3010 output_reldata = &esdo->rela;
3011 swap_out = bed->s->swap_reloca_out;
3012 }
3013 else
3014 {
3015 _bfd_error_handler
3016 /* xgettext:c-format */
3017 (_("%pB: relocation size mismatch in %pB section %pA"),
3018 output_bfd, input_section->owner, input_section);
3019 bfd_set_error (bfd_error_wrong_format);
3020 return false;
3021 }
3022
3023 erel = output_reldata->hdr->contents;
3024 erel += output_reldata->count * input_rel_hdr->sh_entsize;
3025 irela = internal_relocs;
3026 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3027 * bed->s->int_rels_per_ext_rel);
3028 while (irela < irelaend)
3029 {
3030 if (rel_hash && *rel_hash)
3031 (*rel_hash)->has_reloc = 1;
3032 (*swap_out) (output_bfd, irela, erel);
3033 irela += bed->s->int_rels_per_ext_rel;
3034 erel += input_rel_hdr->sh_entsize;
3035 if (rel_hash)
3036 rel_hash++;
3037 }
3038
3039 /* Bump the counter, so that we know where to add the next set of
3040 relocations. */
3041 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
3042
3043 return true;
3044 }
3045 \f
3046 /* Make weak undefined symbols in PIE dynamic. */
3047
3048 bool
3049 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3050 struct elf_link_hash_entry *h)
3051 {
3052 if (bfd_link_pie (info)
3053 && h->dynindx == -1
3054 && h->root.type == bfd_link_hash_undefweak)
3055 return bfd_elf_link_record_dynamic_symbol (info, h);
3056
3057 return true;
3058 }
3059
3060 /* Fix up the flags for a symbol. This handles various cases which
3061 can only be fixed after all the input files are seen. This is
3062 currently called by both adjust_dynamic_symbol and
3063 assign_sym_version, which is unnecessary but perhaps more robust in
3064 the face of future changes. */
3065
3066 static bool
3067 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3068 struct elf_info_failed *eif)
3069 {
3070 const struct elf_backend_data *bed;
3071
3072 /* If this symbol was mentioned in a non-ELF file, try to set
3073 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3074 permit a non-ELF file to correctly refer to a symbol defined in
3075 an ELF dynamic object. */
3076 if (h->non_elf)
3077 {
3078 while (h->root.type == bfd_link_hash_indirect)
3079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3080
3081 if (h->root.type != bfd_link_hash_defined
3082 && h->root.type != bfd_link_hash_defweak)
3083 {
3084 h->ref_regular = 1;
3085 h->ref_regular_nonweak = 1;
3086 }
3087 else
3088 {
3089 if (h->root.u.def.section->owner != NULL
3090 && (bfd_get_flavour (h->root.u.def.section->owner)
3091 == bfd_target_elf_flavour))
3092 {
3093 h->ref_regular = 1;
3094 h->ref_regular_nonweak = 1;
3095 }
3096 else
3097 h->def_regular = 1;
3098 }
3099
3100 if (h->dynindx == -1
3101 && (h->def_dynamic
3102 || h->ref_dynamic))
3103 {
3104 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3105 {
3106 eif->failed = true;
3107 return false;
3108 }
3109 }
3110 }
3111 else
3112 {
3113 /* Unfortunately, NON_ELF is only correct if the symbol
3114 was first seen in a non-ELF file. Fortunately, if the symbol
3115 was first seen in an ELF file, we're probably OK unless the
3116 symbol was defined in a non-ELF file. Catch that case here.
3117 FIXME: We're still in trouble if the symbol was first seen in
3118 a dynamic object, and then later in a non-ELF regular object. */
3119 if ((h->root.type == bfd_link_hash_defined
3120 || h->root.type == bfd_link_hash_defweak)
3121 && !h->def_regular
3122 && (h->root.u.def.section->owner != NULL
3123 ? (bfd_get_flavour (h->root.u.def.section->owner)
3124 != bfd_target_elf_flavour)
3125 : (bfd_is_abs_section (h->root.u.def.section)
3126 && !h->def_dynamic)))
3127 h->def_regular = 1;
3128 }
3129
3130 /* Backend specific symbol fixup. */
3131 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3132 if (bed->elf_backend_fixup_symbol
3133 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3134 return false;
3135
3136 /* If this is a final link, and the symbol was defined as a common
3137 symbol in a regular object file, and there was no definition in
3138 any dynamic object, then the linker will have allocated space for
3139 the symbol in a common section but the DEF_REGULAR
3140 flag will not have been set. */
3141 if (h->root.type == bfd_link_hash_defined
3142 && !h->def_regular
3143 && h->ref_regular
3144 && !h->def_dynamic
3145 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3146 h->def_regular = 1;
3147
3148 /* Symbols defined in discarded sections shouldn't be dynamic. */
3149 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3150 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3151
3152 /* If a weak undefined symbol has non-default visibility, we also
3153 hide it from the dynamic linker. */
3154 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3155 && h->root.type == bfd_link_hash_undefweak)
3156 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3157
3158 /* A hidden versioned symbol in executable should be forced local if
3159 it is is locally defined, not referenced by shared library and not
3160 exported. */
3161 else if (bfd_link_executable (eif->info)
3162 && h->versioned == versioned_hidden
3163 && !eif->info->export_dynamic
3164 && !h->dynamic
3165 && !h->ref_dynamic
3166 && h->def_regular)
3167 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3168
3169 /* If -Bsymbolic was used (which means to bind references to global
3170 symbols to the definition within the shared object), and this
3171 symbol was defined in a regular object, then it actually doesn't
3172 need a PLT entry. Likewise, if the symbol has non-default
3173 visibility. If the symbol has hidden or internal visibility, we
3174 will force it local. */
3175 else if (h->needs_plt
3176 && bfd_link_pic (eif->info)
3177 && is_elf_hash_table (eif->info->hash)
3178 && (SYMBOLIC_BIND (eif->info, h)
3179 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3180 && h->def_regular)
3181 {
3182 bool force_local;
3183
3184 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3185 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3186 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3187 }
3188
3189 /* If this is a weak defined symbol in a dynamic object, and we know
3190 the real definition in the dynamic object, copy interesting flags
3191 over to the real definition. */
3192 if (h->is_weakalias)
3193 {
3194 struct elf_link_hash_entry *def = weakdef (h);
3195
3196 /* If the real definition is defined by a regular object file,
3197 don't do anything special. See the longer description in
3198 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3199 bfd_link_hash_defined as it was when put on the alias list
3200 then it must have originally been a versioned symbol (for
3201 which a non-versioned indirect symbol is created) and later
3202 a definition for the non-versioned symbol is found. In that
3203 case the indirection is flipped with the versioned symbol
3204 becoming an indirect pointing at the non-versioned symbol.
3205 Thus, not an alias any more. */
3206 if (def->def_regular
3207 || def->root.type != bfd_link_hash_defined)
3208 {
3209 h = def;
3210 while ((h = h->u.alias) != def)
3211 h->is_weakalias = 0;
3212 }
3213 else
3214 {
3215 while (h->root.type == bfd_link_hash_indirect)
3216 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3217 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3218 || h->root.type == bfd_link_hash_defweak);
3219 BFD_ASSERT (def->def_dynamic);
3220 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3221 }
3222 }
3223
3224 return true;
3225 }
3226
3227 /* Make the backend pick a good value for a dynamic symbol. This is
3228 called via elf_link_hash_traverse, and also calls itself
3229 recursively. */
3230
3231 static bool
3232 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3233 {
3234 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3235 struct elf_link_hash_table *htab;
3236 const struct elf_backend_data *bed;
3237
3238 if (! is_elf_hash_table (eif->info->hash))
3239 return false;
3240
3241 /* Ignore indirect symbols. These are added by the versioning code. */
3242 if (h->root.type == bfd_link_hash_indirect)
3243 return true;
3244
3245 /* Fix the symbol flags. */
3246 if (! _bfd_elf_fix_symbol_flags (h, eif))
3247 return false;
3248
3249 htab = elf_hash_table (eif->info);
3250 bed = get_elf_backend_data (htab->dynobj);
3251
3252 if (h->root.type == bfd_link_hash_undefweak)
3253 {
3254 if (eif->info->dynamic_undefined_weak == 0)
3255 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3256 else if (eif->info->dynamic_undefined_weak > 0
3257 && h->ref_regular
3258 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3259 && !bfd_hide_sym_by_version (eif->info->version_info,
3260 h->root.root.string))
3261 {
3262 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3263 {
3264 eif->failed = true;
3265 return false;
3266 }
3267 }
3268 }
3269
3270 /* If this symbol does not require a PLT entry, and it is not
3271 defined by a dynamic object, or is not referenced by a regular
3272 object, ignore it. We do have to handle a weak defined symbol,
3273 even if no regular object refers to it, if we decided to add it
3274 to the dynamic symbol table. FIXME: Do we normally need to worry
3275 about symbols which are defined by one dynamic object and
3276 referenced by another one? */
3277 if (!h->needs_plt
3278 && h->type != STT_GNU_IFUNC
3279 && (h->def_regular
3280 || !h->def_dynamic
3281 || (!h->ref_regular
3282 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3283 {
3284 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3285 return true;
3286 }
3287
3288 /* If we've already adjusted this symbol, don't do it again. This
3289 can happen via a recursive call. */
3290 if (h->dynamic_adjusted)
3291 return true;
3292
3293 /* Don't look at this symbol again. Note that we must set this
3294 after checking the above conditions, because we may look at a
3295 symbol once, decide not to do anything, and then get called
3296 recursively later after REF_REGULAR is set below. */
3297 h->dynamic_adjusted = 1;
3298
3299 /* If this is a weak definition, and we know a real definition, and
3300 the real symbol is not itself defined by a regular object file,
3301 then get a good value for the real definition. We handle the
3302 real symbol first, for the convenience of the backend routine.
3303
3304 Note that there is a confusing case here. If the real definition
3305 is defined by a regular object file, we don't get the real symbol
3306 from the dynamic object, but we do get the weak symbol. If the
3307 processor backend uses a COPY reloc, then if some routine in the
3308 dynamic object changes the real symbol, we will not see that
3309 change in the corresponding weak symbol. This is the way other
3310 ELF linkers work as well, and seems to be a result of the shared
3311 library model.
3312
3313 I will clarify this issue. Most SVR4 shared libraries define the
3314 variable _timezone and define timezone as a weak synonym. The
3315 tzset call changes _timezone. If you write
3316 extern int timezone;
3317 int _timezone = 5;
3318 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3319 you might expect that, since timezone is a synonym for _timezone,
3320 the same number will print both times. However, if the processor
3321 backend uses a COPY reloc, then actually timezone will be copied
3322 into your process image, and, since you define _timezone
3323 yourself, _timezone will not. Thus timezone and _timezone will
3324 wind up at different memory locations. The tzset call will set
3325 _timezone, leaving timezone unchanged. */
3326
3327 if (h->is_weakalias)
3328 {
3329 struct elf_link_hash_entry *def = weakdef (h);
3330
3331 /* If we get to this point, there is an implicit reference to
3332 the alias by a regular object file via the weak symbol H. */
3333 def->ref_regular = 1;
3334
3335 /* Ensure that the backend adjust_dynamic_symbol function sees
3336 the strong alias before H by recursively calling ourselves. */
3337 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3338 return false;
3339 }
3340
3341 /* If a symbol has no type and no size and does not require a PLT
3342 entry, then we are probably about to do the wrong thing here: we
3343 are probably going to create a COPY reloc for an empty object.
3344 This case can arise when a shared object is built with assembly
3345 code, and the assembly code fails to set the symbol type. */
3346 if (h->size == 0
3347 && h->type == STT_NOTYPE
3348 && !h->needs_plt)
3349 _bfd_error_handler
3350 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3351 h->root.root.string);
3352
3353 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3354 {
3355 eif->failed = true;
3356 return false;
3357 }
3358
3359 return true;
3360 }
3361
3362 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3363 DYNBSS. */
3364
3365 bool
3366 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3367 struct elf_link_hash_entry *h,
3368 asection *dynbss)
3369 {
3370 unsigned int power_of_two;
3371 bfd_vma mask;
3372 asection *sec = h->root.u.def.section;
3373
3374 /* The section alignment of the definition is the maximum alignment
3375 requirement of symbols defined in the section. Since we don't
3376 know the symbol alignment requirement, we start with the
3377 maximum alignment and check low bits of the symbol address
3378 for the minimum alignment. */
3379 power_of_two = bfd_section_alignment (sec);
3380 mask = ((bfd_vma) 1 << power_of_two) - 1;
3381 while ((h->root.u.def.value & mask) != 0)
3382 {
3383 mask >>= 1;
3384 --power_of_two;
3385 }
3386
3387 /* Adjust the section alignment if needed. */
3388 if (!bfd_link_align_section (dynbss, power_of_two))
3389 return false;
3390
3391 /* We make sure that the symbol will be aligned properly. */
3392 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3393
3394 /* Define the symbol as being at this point in DYNBSS. */
3395 h->root.u.def.section = dynbss;
3396 h->root.u.def.value = dynbss->size;
3397
3398 /* Increment the size of DYNBSS to make room for the symbol. */
3399 dynbss->size += h->size;
3400
3401 /* No error if extern_protected_data is true. */
3402 if (h->protected_def
3403 && (!info->extern_protected_data
3404 || (info->extern_protected_data < 0
3405 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3406 info->callbacks->einfo
3407 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3408 h->root.root.string);
3409
3410 return true;
3411 }
3412
3413 /* Adjust all external symbols pointing into SEC_MERGE sections
3414 to reflect the object merging within the sections. */
3415
3416 static bool
3417 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3418 {
3419 asection *sec;
3420
3421 if ((h->root.type == bfd_link_hash_defined
3422 || h->root.type == bfd_link_hash_defweak)
3423 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3424 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3425 {
3426 bfd *output_bfd = (bfd *) data;
3427
3428 h->root.u.def.value =
3429 _bfd_merged_section_offset (output_bfd,
3430 &h->root.u.def.section,
3431 elf_section_data (sec)->sec_info,
3432 h->root.u.def.value);
3433 }
3434
3435 return true;
3436 }
3437
3438 /* Returns false if the symbol referred to by H should be considered
3439 to resolve local to the current module, and true if it should be
3440 considered to bind dynamically. */
3441
3442 bool
3443 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3444 struct bfd_link_info *info,
3445 bool not_local_protected)
3446 {
3447 bool binding_stays_local_p;
3448 const struct elf_backend_data *bed;
3449 struct elf_link_hash_table *hash_table;
3450
3451 if (h == NULL)
3452 return false;
3453
3454 while (h->root.type == bfd_link_hash_indirect
3455 || h->root.type == bfd_link_hash_warning)
3456 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3457
3458 /* If it was forced local, then clearly it's not dynamic. */
3459 if (h->dynindx == -1)
3460 return false;
3461 if (h->forced_local)
3462 return false;
3463
3464 /* Identify the cases where name binding rules say that a
3465 visible symbol resolves locally. */
3466 binding_stays_local_p = (bfd_link_executable (info)
3467 || SYMBOLIC_BIND (info, h));
3468
3469 switch (ELF_ST_VISIBILITY (h->other))
3470 {
3471 case STV_INTERNAL:
3472 case STV_HIDDEN:
3473 return false;
3474
3475 case STV_PROTECTED:
3476 hash_table = elf_hash_table (info);
3477 if (!is_elf_hash_table (&hash_table->root))
3478 return false;
3479
3480 bed = get_elf_backend_data (hash_table->dynobj);
3481
3482 /* Proper resolution for function pointer equality may require
3483 that these symbols perhaps be resolved dynamically, even though
3484 we should be resolving them to the current module. */
3485 if (!not_local_protected || !bed->is_function_type (h->type))
3486 binding_stays_local_p = true;
3487 break;
3488
3489 default:
3490 break;
3491 }
3492
3493 /* If it isn't defined locally, then clearly it's dynamic. */
3494 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3495 return true;
3496
3497 /* Otherwise, the symbol is dynamic if binding rules don't tell
3498 us that it remains local. */
3499 return !binding_stays_local_p;
3500 }
3501
3502 /* Return true if the symbol referred to by H should be considered
3503 to resolve local to the current module, and false otherwise. Differs
3504 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3505 undefined symbols. The two functions are virtually identical except
3506 for the place where dynindx == -1 is tested. If that test is true,
3507 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3508 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3509 defined symbols.
3510 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3511 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3512 treatment of undefined weak symbols. For those that do not make
3513 undefined weak symbols dynamic, both functions may return false. */
3514
3515 bool
3516 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3517 struct bfd_link_info *info,
3518 bool local_protected)
3519 {
3520 const struct elf_backend_data *bed;
3521 struct elf_link_hash_table *hash_table;
3522
3523 /* If it's a local sym, of course we resolve locally. */
3524 if (h == NULL)
3525 return true;
3526
3527 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3528 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3529 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3530 return true;
3531
3532 /* Forced local symbols resolve locally. */
3533 if (h->forced_local)
3534 return true;
3535
3536 /* Common symbols that become definitions don't get the DEF_REGULAR
3537 flag set, so test it first, and don't bail out. */
3538 if (ELF_COMMON_DEF_P (h))
3539 /* Do nothing. */;
3540 /* If we don't have a definition in a regular file, then we can't
3541 resolve locally. The sym is either undefined or dynamic. */
3542 else if (!h->def_regular)
3543 return false;
3544
3545 /* Non-dynamic symbols resolve locally. */
3546 if (h->dynindx == -1)
3547 return true;
3548
3549 /* At this point, we know the symbol is defined and dynamic. In an
3550 executable it must resolve locally, likewise when building symbolic
3551 shared libraries. */
3552 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3553 return true;
3554
3555 /* Now deal with defined dynamic symbols in shared libraries. Ones
3556 with default visibility might not resolve locally. */
3557 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3558 return false;
3559
3560 hash_table = elf_hash_table (info);
3561 if (!is_elf_hash_table (&hash_table->root))
3562 return true;
3563
3564 /* STV_PROTECTED symbols with indirect external access are local. */
3565 if (info->indirect_extern_access > 0)
3566 return true;
3567
3568 bed = get_elf_backend_data (hash_table->dynobj);
3569
3570 /* If extern_protected_data is false, STV_PROTECTED non-function
3571 symbols are local. */
3572 if ((!info->extern_protected_data
3573 || (info->extern_protected_data < 0
3574 && !bed->extern_protected_data))
3575 && !bed->is_function_type (h->type))
3576 return true;
3577
3578 /* Function pointer equality tests may require that STV_PROTECTED
3579 symbols be treated as dynamic symbols. If the address of a
3580 function not defined in an executable is set to that function's
3581 plt entry in the executable, then the address of the function in
3582 a shared library must also be the plt entry in the executable. */
3583 return local_protected;
3584 }
3585
3586 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3587 aligned. Returns the first TLS output section. */
3588
3589 struct bfd_section *
3590 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3591 {
3592 struct bfd_section *sec, *tls;
3593 unsigned int align = 0;
3594
3595 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3596 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3597 break;
3598 tls = sec;
3599
3600 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3601 if (sec->alignment_power > align)
3602 align = sec->alignment_power;
3603
3604 elf_hash_table (info)->tls_sec = tls;
3605
3606 /* Ensure the alignment of the first section (usually .tdata) is the largest
3607 alignment, so that the tls segment starts aligned. */
3608 if (tls != NULL)
3609 (void) bfd_link_align_section (tls, align);
3610
3611 return tls;
3612 }
3613
3614 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3615 static bool
3616 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3617 Elf_Internal_Sym *sym)
3618 {
3619 const struct elf_backend_data *bed;
3620
3621 /* Local symbols do not count, but target specific ones might. */
3622 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3623 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3624 return false;
3625
3626 bed = get_elf_backend_data (abfd);
3627 /* Function symbols do not count. */
3628 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3629 return false;
3630
3631 /* If the section is undefined, then so is the symbol. */
3632 if (sym->st_shndx == SHN_UNDEF)
3633 return false;
3634
3635 /* If the symbol is defined in the common section, then
3636 it is a common definition and so does not count. */
3637 if (bed->common_definition (sym))
3638 return false;
3639
3640 /* If the symbol is in a target specific section then we
3641 must rely upon the backend to tell us what it is. */
3642 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3643 /* FIXME - this function is not coded yet:
3644
3645 return _bfd_is_global_symbol_definition (abfd, sym);
3646
3647 Instead for now assume that the definition is not global,
3648 Even if this is wrong, at least the linker will behave
3649 in the same way that it used to do. */
3650 return false;
3651
3652 return true;
3653 }
3654
3655 /* Search the symbol table of the archive element of the archive ABFD
3656 whose archive map contains a mention of SYMDEF, and determine if
3657 the symbol is defined in this element. */
3658 static bool
3659 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3660 {
3661 Elf_Internal_Shdr * hdr;
3662 size_t symcount;
3663 size_t extsymcount;
3664 size_t extsymoff;
3665 Elf_Internal_Sym *isymbuf;
3666 Elf_Internal_Sym *isym;
3667 Elf_Internal_Sym *isymend;
3668 bool result;
3669
3670 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3671 if (abfd == NULL)
3672 return false;
3673
3674 if (! bfd_check_format (abfd, bfd_object))
3675 return false;
3676
3677 /* Select the appropriate symbol table. If we don't know if the
3678 object file is an IR object, give linker LTO plugin a chance to
3679 get the correct symbol table. */
3680 if (abfd->plugin_format == bfd_plugin_yes
3681 || abfd->plugin_format == bfd_plugin_yes_unused
3682 #if BFD_SUPPORTS_PLUGINS
3683 || (abfd->plugin_format == bfd_plugin_unknown
3684 && bfd_link_plugin_object_p (abfd))
3685 #endif
3686 )
3687 {
3688 /* Use the IR symbol table if the object has been claimed by
3689 plugin. */
3690 abfd = abfd->plugin_dummy_bfd;
3691 hdr = &elf_tdata (abfd)->symtab_hdr;
3692 }
3693 else
3694 {
3695 if (elf_use_dt_symtab_p (abfd))
3696 {
3697 bfd_set_error (bfd_error_wrong_format);
3698 return false;
3699 }
3700
3701 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3702 hdr = &elf_tdata (abfd)->symtab_hdr;
3703 else
3704 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3705 }
3706
3707 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3708
3709 /* The sh_info field of the symtab header tells us where the
3710 external symbols start. We don't care about the local symbols. */
3711 if (elf_bad_symtab (abfd))
3712 {
3713 extsymcount = symcount;
3714 extsymoff = 0;
3715 }
3716 else
3717 {
3718 extsymcount = symcount - hdr->sh_info;
3719 extsymoff = hdr->sh_info;
3720 }
3721
3722 if (extsymcount == 0)
3723 return false;
3724
3725 /* Read in the symbol table. */
3726 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3727 NULL, NULL, NULL);
3728 if (isymbuf == NULL)
3729 return false;
3730
3731 /* Scan the symbol table looking for SYMDEF. */
3732 result = false;
3733 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3734 {
3735 const char *name;
3736
3737 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3738 isym->st_name);
3739 if (name == NULL)
3740 break;
3741
3742 if (strcmp (name, symdef->name) == 0)
3743 {
3744 result = is_global_data_symbol_definition (abfd, isym);
3745 break;
3746 }
3747 }
3748
3749 free (isymbuf);
3750
3751 return result;
3752 }
3753 \f
3754 /* Add an entry to the .dynamic table. */
3755
3756 bool
3757 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3758 bfd_vma tag,
3759 bfd_vma val)
3760 {
3761 struct elf_link_hash_table *hash_table;
3762 const struct elf_backend_data *bed;
3763 asection *s;
3764 bfd_size_type newsize;
3765 bfd_byte *newcontents;
3766 Elf_Internal_Dyn dyn;
3767
3768 hash_table = elf_hash_table (info);
3769 if (! is_elf_hash_table (&hash_table->root))
3770 return false;
3771
3772 if (tag == DT_RELA || tag == DT_REL)
3773 hash_table->dynamic_relocs = true;
3774
3775 bed = get_elf_backend_data (hash_table->dynobj);
3776 s = hash_table->dynamic;
3777 BFD_ASSERT (s != NULL);
3778
3779 newsize = s->size + bed->s->sizeof_dyn;
3780 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3781 if (newcontents == NULL)
3782 return false;
3783
3784 dyn.d_tag = tag;
3785 dyn.d_un.d_val = val;
3786 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3787
3788 s->size = newsize;
3789 s->contents = newcontents;
3790
3791 return true;
3792 }
3793
3794 /* Strip zero-sized dynamic sections. */
3795
3796 bool
3797 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3798 {
3799 struct elf_link_hash_table *hash_table;
3800 const struct elf_backend_data *bed;
3801 asection *s, *sdynamic, **pp;
3802 asection *rela_dyn, *rel_dyn;
3803 Elf_Internal_Dyn dyn;
3804 bfd_byte *extdyn, *next;
3805 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3806 bool strip_zero_sized;
3807 bool strip_zero_sized_plt;
3808
3809 if (bfd_link_relocatable (info))
3810 return true;
3811
3812 hash_table = elf_hash_table (info);
3813 if (!is_elf_hash_table (&hash_table->root))
3814 return false;
3815
3816 if (!hash_table->dynobj)
3817 return true;
3818
3819 sdynamic= hash_table->dynamic;
3820 if (!sdynamic)
3821 return true;
3822
3823 bed = get_elf_backend_data (hash_table->dynobj);
3824 swap_dyn_in = bed->s->swap_dyn_in;
3825
3826 strip_zero_sized = false;
3827 strip_zero_sized_plt = false;
3828
3829 /* Strip zero-sized dynamic sections. */
3830 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3831 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3832 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3833 if (s->size == 0
3834 && (s == rela_dyn
3835 || s == rel_dyn
3836 || s == hash_table->srelplt->output_section
3837 || s == hash_table->splt->output_section))
3838 {
3839 *pp = s->next;
3840 info->output_bfd->section_count--;
3841 strip_zero_sized = true;
3842 if (s == rela_dyn)
3843 s = rela_dyn;
3844 if (s == rel_dyn)
3845 s = rel_dyn;
3846 else if (s == hash_table->splt->output_section)
3847 {
3848 s = hash_table->splt;
3849 strip_zero_sized_plt = true;
3850 }
3851 else
3852 s = hash_table->srelplt;
3853 s->flags |= SEC_EXCLUDE;
3854 s->output_section = bfd_abs_section_ptr;
3855 }
3856 else
3857 pp = &s->next;
3858
3859 if (strip_zero_sized_plt && sdynamic->size != 0)
3860 for (extdyn = sdynamic->contents;
3861 extdyn < sdynamic->contents + sdynamic->size;
3862 extdyn = next)
3863 {
3864 next = extdyn + bed->s->sizeof_dyn;
3865 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3866 switch (dyn.d_tag)
3867 {
3868 default:
3869 break;
3870 case DT_JMPREL:
3871 case DT_PLTRELSZ:
3872 case DT_PLTREL:
3873 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3874 the procedure linkage table (the .plt section) has been
3875 removed. */
3876 memmove (extdyn, next,
3877 sdynamic->size - (next - sdynamic->contents));
3878 next = extdyn;
3879 }
3880 }
3881
3882 if (strip_zero_sized)
3883 {
3884 /* Regenerate program headers. */
3885 elf_seg_map (info->output_bfd) = NULL;
3886 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3887 NULL);
3888 }
3889
3890 return true;
3891 }
3892
3893 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3894 1 if a DT_NEEDED tag already exists, and 0 on success. */
3895
3896 int
3897 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3898 {
3899 struct elf_link_hash_table *hash_table;
3900 size_t strindex;
3901 const char *soname;
3902
3903 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3904 return -1;
3905
3906 hash_table = elf_hash_table (info);
3907 soname = elf_dt_name (abfd);
3908 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3909 if (strindex == (size_t) -1)
3910 return -1;
3911
3912 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3913 {
3914 asection *sdyn;
3915 const struct elf_backend_data *bed;
3916 bfd_byte *extdyn;
3917
3918 bed = get_elf_backend_data (hash_table->dynobj);
3919 sdyn = hash_table->dynamic;
3920 if (sdyn != NULL && sdyn->size != 0)
3921 for (extdyn = sdyn->contents;
3922 extdyn < sdyn->contents + sdyn->size;
3923 extdyn += bed->s->sizeof_dyn)
3924 {
3925 Elf_Internal_Dyn dyn;
3926
3927 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3928 if (dyn.d_tag == DT_NEEDED
3929 && dyn.d_un.d_val == strindex)
3930 {
3931 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3932 return 1;
3933 }
3934 }
3935 }
3936
3937 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3938 return -1;
3939
3940 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3941 return -1;
3942
3943 return 0;
3944 }
3945
3946 /* Return true if SONAME is on the needed list between NEEDED and STOP
3947 (or the end of list if STOP is NULL), and needed by a library that
3948 will be loaded. */
3949
3950 static bool
3951 on_needed_list (const char *soname,
3952 struct bfd_link_needed_list *needed,
3953 struct bfd_link_needed_list *stop)
3954 {
3955 struct bfd_link_needed_list *look;
3956 for (look = needed; look != stop; look = look->next)
3957 if (strcmp (soname, look->name) == 0
3958 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3959 /* If needed by a library that itself is not directly
3960 needed, recursively check whether that library is
3961 indirectly needed. Since we add DT_NEEDED entries to
3962 the end of the list, library dependencies appear after
3963 the library. Therefore search prior to the current
3964 LOOK, preventing possible infinite recursion. */
3965 || on_needed_list (elf_dt_name (look->by), needed, look)))
3966 return true;
3967
3968 return false;
3969 }
3970
3971 /* Sort symbol by value, section, size, and type. */
3972 static int
3973 elf_sort_symbol (const void *arg1, const void *arg2)
3974 {
3975 const struct elf_link_hash_entry *h1;
3976 const struct elf_link_hash_entry *h2;
3977 bfd_signed_vma vdiff;
3978 int sdiff;
3979 const char *n1;
3980 const char *n2;
3981
3982 h1 = *(const struct elf_link_hash_entry **) arg1;
3983 h2 = *(const struct elf_link_hash_entry **) arg2;
3984 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3985 if (vdiff != 0)
3986 return vdiff > 0 ? 1 : -1;
3987
3988 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3989 if (sdiff != 0)
3990 return sdiff;
3991
3992 /* Sort so that sized symbols are selected over zero size symbols. */
3993 vdiff = h1->size - h2->size;
3994 if (vdiff != 0)
3995 return vdiff > 0 ? 1 : -1;
3996
3997 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3998 if (h1->type != h2->type)
3999 return h1->type - h2->type;
4000
4001 /* If symbols are properly sized and typed, and multiple strong
4002 aliases are not defined in a shared library by the user we
4003 shouldn't get here. Unfortunately linker script symbols like
4004 __bss_start sometimes match a user symbol defined at the start of
4005 .bss without proper size and type. We'd like to preference the
4006 user symbol over reserved system symbols. Sort on leading
4007 underscores. */
4008 n1 = h1->root.root.string;
4009 n2 = h2->root.root.string;
4010 while (*n1 == *n2)
4011 {
4012 if (*n1 == 0)
4013 break;
4014 ++n1;
4015 ++n2;
4016 }
4017 if (*n1 == '_')
4018 return -1;
4019 if (*n2 == '_')
4020 return 1;
4021
4022 /* Final sort on name selects user symbols like '_u' over reserved
4023 system symbols like '_Z' and also will avoid qsort instability. */
4024 return *n1 - *n2;
4025 }
4026
4027 /* This function is used to adjust offsets into .dynstr for
4028 dynamic symbols. This is called via elf_link_hash_traverse. */
4029
4030 static bool
4031 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4032 {
4033 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
4034
4035 if (h->dynindx != -1)
4036 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
4037 return true;
4038 }
4039
4040 /* Assign string offsets in .dynstr, update all structures referencing
4041 them. */
4042
4043 static bool
4044 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4045 {
4046 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4047 struct elf_link_local_dynamic_entry *entry;
4048 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4049 bfd *dynobj = hash_table->dynobj;
4050 asection *sdyn;
4051 bfd_size_type size;
4052 const struct elf_backend_data *bed;
4053 bfd_byte *extdyn;
4054
4055 _bfd_elf_strtab_finalize (dynstr);
4056 size = _bfd_elf_strtab_size (dynstr);
4057
4058 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4059
4060 if (info->callbacks->examine_strtab)
4061 info->callbacks->examine_strtab (dynstr);
4062
4063 bed = get_elf_backend_data (dynobj);
4064 sdyn = hash_table->dynamic;
4065 BFD_ASSERT (sdyn != NULL);
4066
4067 /* Update all .dynamic entries referencing .dynstr strings. */
4068 for (extdyn = sdyn->contents;
4069 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4070 extdyn += bed->s->sizeof_dyn)
4071 {
4072 Elf_Internal_Dyn dyn;
4073
4074 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4075 switch (dyn.d_tag)
4076 {
4077 case DT_STRSZ:
4078 dyn.d_un.d_val = size;
4079 break;
4080 case DT_NEEDED:
4081 case DT_SONAME:
4082 case DT_RPATH:
4083 case DT_RUNPATH:
4084 case DT_FILTER:
4085 case DT_AUXILIARY:
4086 case DT_AUDIT:
4087 case DT_DEPAUDIT:
4088 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4089 break;
4090 default:
4091 continue;
4092 }
4093 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4094 }
4095
4096 /* Now update local dynamic symbols. */
4097 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4098 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4099 entry->isym.st_name);
4100
4101 /* And the rest of dynamic symbols. */
4102 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4103
4104 /* Adjust version definitions. */
4105 if (elf_tdata (output_bfd)->cverdefs)
4106 {
4107 asection *s;
4108 bfd_byte *p;
4109 size_t i;
4110 Elf_Internal_Verdef def;
4111 Elf_Internal_Verdaux defaux;
4112
4113 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4114 p = s->contents;
4115 do
4116 {
4117 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4118 &def);
4119 p += sizeof (Elf_External_Verdef);
4120 if (def.vd_aux != sizeof (Elf_External_Verdef))
4121 continue;
4122 for (i = 0; i < def.vd_cnt; ++i)
4123 {
4124 _bfd_elf_swap_verdaux_in (output_bfd,
4125 (Elf_External_Verdaux *) p, &defaux);
4126 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4127 defaux.vda_name);
4128 _bfd_elf_swap_verdaux_out (output_bfd,
4129 &defaux, (Elf_External_Verdaux *) p);
4130 p += sizeof (Elf_External_Verdaux);
4131 }
4132 }
4133 while (def.vd_next);
4134 }
4135
4136 /* Adjust version references. */
4137 if (elf_tdata (output_bfd)->verref)
4138 {
4139 asection *s;
4140 bfd_byte *p;
4141 size_t i;
4142 Elf_Internal_Verneed need;
4143 Elf_Internal_Vernaux needaux;
4144
4145 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4146 p = s->contents;
4147 do
4148 {
4149 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4150 &need);
4151 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4152 _bfd_elf_swap_verneed_out (output_bfd, &need,
4153 (Elf_External_Verneed *) p);
4154 p += sizeof (Elf_External_Verneed);
4155 for (i = 0; i < need.vn_cnt; ++i)
4156 {
4157 _bfd_elf_swap_vernaux_in (output_bfd,
4158 (Elf_External_Vernaux *) p, &needaux);
4159 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4160 needaux.vna_name);
4161 _bfd_elf_swap_vernaux_out (output_bfd,
4162 &needaux,
4163 (Elf_External_Vernaux *) p);
4164 p += sizeof (Elf_External_Vernaux);
4165 }
4166 }
4167 while (need.vn_next);
4168 }
4169
4170 return true;
4171 }
4172 \f
4173 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4174 The default is to only match when the INPUT and OUTPUT are exactly
4175 the same target. */
4176
4177 bool
4178 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4179 const bfd_target *output)
4180 {
4181 return input == output;
4182 }
4183
4184 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4185 This version is used when different targets for the same architecture
4186 are virtually identical. */
4187
4188 bool
4189 _bfd_elf_relocs_compatible (const bfd_target *input,
4190 const bfd_target *output)
4191 {
4192 const struct elf_backend_data *obed, *ibed;
4193
4194 if (input == output)
4195 return true;
4196
4197 ibed = xvec_get_elf_backend_data (input);
4198 obed = xvec_get_elf_backend_data (output);
4199
4200 if (ibed->arch != obed->arch)
4201 return false;
4202
4203 /* If both backends are using this function, deem them compatible. */
4204 return ibed->relocs_compatible == obed->relocs_compatible;
4205 }
4206
4207 /* Make a special call to the linker "notice" function to tell it that
4208 we are about to handle an as-needed lib, or have finished
4209 processing the lib. */
4210
4211 bool
4212 _bfd_elf_notice_as_needed (bfd *ibfd,
4213 struct bfd_link_info *info,
4214 enum notice_asneeded_action act)
4215 {
4216 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4217 }
4218
4219 /* Call ACTION on each relocation in an ELF object file. */
4220
4221 bool
4222 _bfd_elf_link_iterate_on_relocs
4223 (bfd *abfd, struct bfd_link_info *info,
4224 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4225 const Elf_Internal_Rela *))
4226 {
4227 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4228 struct elf_link_hash_table *htab = elf_hash_table (info);
4229
4230 /* If this object is the same format as the output object, and it is
4231 not a shared library, then let the backend look through the
4232 relocs.
4233
4234 This is required to build global offset table entries and to
4235 arrange for dynamic relocs. It is not required for the
4236 particular common case of linking non PIC code, even when linking
4237 against shared libraries, but unfortunately there is no way of
4238 knowing whether an object file has been compiled PIC or not.
4239 Looking through the relocs is not particularly time consuming.
4240 The problem is that we must either (1) keep the relocs in memory,
4241 which causes the linker to require additional runtime memory or
4242 (2) read the relocs twice from the input file, which wastes time.
4243 This would be a good case for using mmap.
4244
4245 I have no idea how to handle linking PIC code into a file of a
4246 different format. It probably can't be done. */
4247 if ((abfd->flags & DYNAMIC) == 0
4248 && is_elf_hash_table (&htab->root)
4249 && elf_object_id (abfd) == elf_hash_table_id (htab)
4250 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4251 {
4252 asection *o;
4253
4254 for (o = abfd->sections; o != NULL; o = o->next)
4255 {
4256 Elf_Internal_Rela *internal_relocs;
4257 bool ok;
4258
4259 /* Don't check relocations in excluded sections. Don't do
4260 anything special with non-loaded, non-alloced sections.
4261 In particular, any relocs in such sections should not
4262 affect GOT and PLT reference counting (ie. we don't
4263 allow them to create GOT or PLT entries), there's no
4264 possibility or desire to optimize TLS relocs, and
4265 there's not much point in propagating relocs to shared
4266 libs that the dynamic linker won't relocate. */
4267 if ((o->flags & SEC_ALLOC) == 0
4268 || (o->flags & SEC_RELOC) == 0
4269 || (o->flags & SEC_EXCLUDE) != 0
4270 || o->reloc_count == 0
4271 || ((info->strip == strip_all || info->strip == strip_debugger)
4272 && (o->flags & SEC_DEBUGGING) != 0)
4273 || bfd_is_abs_section (o->output_section))
4274 continue;
4275
4276 internal_relocs = _bfd_elf_link_info_read_relocs
4277 (abfd, info, o, NULL, NULL,
4278 _bfd_elf_link_keep_memory (info));
4279 if (internal_relocs == NULL)
4280 return false;
4281
4282 ok = action (abfd, info, o, internal_relocs);
4283
4284 if (elf_section_data (o)->relocs != internal_relocs)
4285 free (internal_relocs);
4286
4287 if (! ok)
4288 return false;
4289 }
4290 }
4291
4292 return true;
4293 }
4294
4295 /* Check relocations in an ELF object file. This is called after
4296 all input files have been opened. */
4297
4298 bool
4299 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4300 {
4301 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4302 if (bed->check_relocs != NULL)
4303 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4304 bed->check_relocs);
4305 return true;
4306 }
4307
4308 /* An entry in the first definition hash table. */
4309
4310 struct elf_link_first_hash_entry
4311 {
4312 struct bfd_hash_entry root;
4313 /* The object of the first definition. */
4314 bfd *abfd;
4315 };
4316
4317 /* The function to create a new entry in the first definition hash
4318 table. */
4319
4320 static struct bfd_hash_entry *
4321 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4322 struct bfd_hash_table *table,
4323 const char *string)
4324 {
4325 struct elf_link_first_hash_entry *ret =
4326 (struct elf_link_first_hash_entry *) entry;
4327
4328 /* Allocate the structure if it has not already been allocated by a
4329 subclass. */
4330 if (ret == NULL)
4331 ret = (struct elf_link_first_hash_entry *)
4332 bfd_hash_allocate (table,
4333 sizeof (struct elf_link_first_hash_entry));
4334 if (ret == NULL)
4335 return NULL;
4336
4337 /* Call the allocation method of the superclass. */
4338 ret = ((struct elf_link_first_hash_entry *)
4339 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4340 string));
4341 if (ret != NULL)
4342 ret->abfd = NULL;
4343
4344 return (struct bfd_hash_entry *) ret;
4345 }
4346
4347 /* Add the symbol NAME from ABFD to first hash. */
4348
4349 static void
4350 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4351 const char *name, bool copy)
4352 {
4353 struct elf_link_hash_table *htab = elf_hash_table (info);
4354 /* Skip if there is no first hash. */
4355 if (htab->first_hash == NULL)
4356 return;
4357
4358 struct elf_link_first_hash_entry *e
4359 = ((struct elf_link_first_hash_entry *)
4360 bfd_hash_lookup (htab->first_hash, name, true, copy));
4361 if (e == NULL)
4362 info->callbacks->fatal
4363 (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
4364
4365 if (e->abfd == NULL)
4366 /* Store ABFD in abfd. */
4367 e->abfd = abfd;
4368 }
4369
4370 /* Add symbols from an ELF object file to the linker hash table. */
4371
4372 static bool
4373 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4374 {
4375 Elf_Internal_Ehdr *ehdr;
4376 Elf_Internal_Shdr *hdr;
4377 size_t symcount;
4378 size_t extsymcount;
4379 size_t extsymoff;
4380 struct elf_link_hash_entry **sym_hash;
4381 bool dynamic;
4382 Elf_External_Versym *extversym = NULL;
4383 Elf_External_Versym *extversym_end = NULL;
4384 Elf_External_Versym *ever;
4385 struct elf_link_hash_entry *weaks;
4386 struct elf_link_hash_entry **nondeflt_vers = NULL;
4387 size_t nondeflt_vers_cnt = 0;
4388 Elf_Internal_Sym *isymbuf = NULL;
4389 Elf_Internal_Sym *isym;
4390 Elf_Internal_Sym *isymend;
4391 const struct elf_backend_data *bed;
4392 bool add_needed;
4393 struct elf_link_hash_table *htab;
4394 void *alloc_mark = NULL;
4395 struct bfd_hash_entry **old_table = NULL;
4396 unsigned int old_size = 0;
4397 unsigned int old_count = 0;
4398 void *old_tab = NULL;
4399 void *old_ent;
4400 struct bfd_link_hash_entry *old_undefs = NULL;
4401 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4402 void *old_strtab = NULL;
4403 size_t tabsize = 0;
4404 asection *s;
4405 bool just_syms;
4406
4407 htab = elf_hash_table (info);
4408 bed = get_elf_backend_data (abfd);
4409
4410 if (elf_use_dt_symtab_p (abfd))
4411 {
4412 bfd_set_error (bfd_error_wrong_format);
4413 return false;
4414 }
4415
4416 if ((abfd->flags & DYNAMIC) == 0)
4417 {
4418 dynamic = false;
4419 if ((abfd->flags & BFD_PLUGIN) != 0
4420 && is_elf_hash_table (&htab->root)
4421 && htab->first_hash == NULL)
4422 {
4423 /* Initialize first_hash for an IR input. */
4424 htab->first_hash = (struct bfd_hash_table *)
4425 bfd_malloc (sizeof (struct bfd_hash_table));
4426 if (htab->first_hash == NULL
4427 || !bfd_hash_table_init
4428 (htab->first_hash, elf_link_first_hash_newfunc,
4429 sizeof (struct elf_link_first_hash_entry)))
4430 info->callbacks->fatal
4431 (_("%P: first_hash failed to create: %E\n"));
4432 }
4433 }
4434 else
4435 {
4436 dynamic = true;
4437
4438 /* You can't use -r against a dynamic object. Also, there's no
4439 hope of using a dynamic object which does not exactly match
4440 the format of the output file. */
4441 if (bfd_link_relocatable (info)
4442 || !is_elf_hash_table (&htab->root)
4443 || info->output_bfd->xvec != abfd->xvec)
4444 {
4445 if (bfd_link_relocatable (info))
4446 bfd_set_error (bfd_error_invalid_operation);
4447 else
4448 bfd_set_error (bfd_error_wrong_format);
4449 goto error_return;
4450 }
4451 }
4452
4453 ehdr = elf_elfheader (abfd);
4454 if (info->warn_alternate_em
4455 && bed->elf_machine_code != ehdr->e_machine
4456 && ((bed->elf_machine_alt1 != 0
4457 && ehdr->e_machine == bed->elf_machine_alt1)
4458 || (bed->elf_machine_alt2 != 0
4459 && ehdr->e_machine == bed->elf_machine_alt2)))
4460 _bfd_error_handler
4461 /* xgettext:c-format */
4462 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4463 ehdr->e_machine, abfd, bed->elf_machine_code);
4464
4465 /* As a GNU extension, any input sections which are named
4466 .gnu.warning.SYMBOL are treated as warning symbols for the given
4467 symbol. This differs from .gnu.warning sections, which generate
4468 warnings when they are included in an output file. */
4469 /* PR 12761: Also generate this warning when building shared libraries. */
4470 for (s = abfd->sections; s != NULL; s = s->next)
4471 {
4472 const char *name;
4473
4474 name = bfd_section_name (s);
4475 if (startswith (name, ".gnu.warning."))
4476 {
4477 char *msg;
4478 bfd_size_type sz;
4479
4480 name += sizeof ".gnu.warning." - 1;
4481
4482 /* If this is a shared object, then look up the symbol
4483 in the hash table. If it is there, and it is already
4484 been defined, then we will not be using the entry
4485 from this shared object, so we don't need to warn.
4486 FIXME: If we see the definition in a regular object
4487 later on, we will warn, but we shouldn't. The only
4488 fix is to keep track of what warnings we are supposed
4489 to emit, and then handle them all at the end of the
4490 link. */
4491 if (dynamic)
4492 {
4493 struct elf_link_hash_entry *h;
4494
4495 h = elf_link_hash_lookup (htab, name, false, false, true);
4496
4497 /* FIXME: What about bfd_link_hash_common? */
4498 if (h != NULL
4499 && (h->root.type == bfd_link_hash_defined
4500 || h->root.type == bfd_link_hash_defweak))
4501 continue;
4502 }
4503
4504 sz = s->size;
4505 msg = (char *) bfd_alloc (abfd, sz + 1);
4506 if (msg == NULL)
4507 goto error_return;
4508
4509 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4510 goto error_return;
4511
4512 msg[sz] = '\0';
4513
4514 if (! (_bfd_generic_link_add_one_symbol
4515 (info, abfd, name, BSF_WARNING, s, 0, msg,
4516 false, bed->collect, NULL)))
4517 goto error_return;
4518
4519 if (bfd_link_executable (info))
4520 {
4521 /* Clobber the section size so that the warning does
4522 not get copied into the output file. */
4523 s->size = 0;
4524
4525 /* Also set SEC_EXCLUDE, so that symbols defined in
4526 the warning section don't get copied to the output. */
4527 s->flags |= SEC_EXCLUDE;
4528 }
4529 }
4530 }
4531
4532 just_syms = ((s = abfd->sections) != NULL
4533 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4534
4535 add_needed = true;
4536 if (! dynamic)
4537 {
4538 /* If we are creating a shared library, create all the dynamic
4539 sections immediately. We need to attach them to something,
4540 so we attach them to this BFD, provided it is the right
4541 format and is not from ld --just-symbols. Always create the
4542 dynamic sections for -E/--dynamic-list. FIXME: If there
4543 are no input BFD's of the same format as the output, we can't
4544 make a shared library. */
4545 if (!just_syms
4546 && (bfd_link_pic (info)
4547 || (!bfd_link_relocatable (info)
4548 && info->nointerp
4549 && (info->export_dynamic || info->dynamic)))
4550 && is_elf_hash_table (&htab->root)
4551 && info->output_bfd->xvec == abfd->xvec
4552 && !htab->dynamic_sections_created)
4553 {
4554 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4555 goto error_return;
4556 }
4557 }
4558 else if (!is_elf_hash_table (&htab->root))
4559 goto error_return;
4560 else
4561 {
4562 const char *soname = NULL;
4563 char *audit = NULL;
4564 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4565 const Elf_Internal_Phdr *phdr;
4566 struct elf_link_loaded_list *loaded_lib;
4567
4568 /* ld --just-symbols and dynamic objects don't mix very well.
4569 ld shouldn't allow it. */
4570 if (just_syms)
4571 abort ();
4572
4573 /* If this dynamic lib was specified on the command line with
4574 --as-needed in effect, then we don't want to add a DT_NEEDED
4575 tag unless the lib is actually used. Similary for libs brought
4576 in by another lib's DT_NEEDED. When --no-add-needed is used
4577 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4578 any dynamic library in DT_NEEDED tags in the dynamic lib at
4579 all. */
4580 add_needed = (elf_dyn_lib_class (abfd)
4581 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4582 | DYN_NO_NEEDED)) == 0;
4583
4584 s = bfd_get_section_by_name (abfd, ".dynamic");
4585 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4586 {
4587 bfd_byte *dynbuf;
4588 bfd_byte *extdyn;
4589 unsigned int elfsec;
4590 unsigned long shlink;
4591
4592 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4593 {
4594 error_free_dyn:
4595 _bfd_elf_munmap_section_contents (s, dynbuf);
4596 goto error_return;
4597 }
4598
4599 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4600 if (elfsec == SHN_BAD)
4601 goto error_free_dyn;
4602 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4603
4604 for (extdyn = dynbuf;
4605 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4606 extdyn += bed->s->sizeof_dyn)
4607 {
4608 Elf_Internal_Dyn dyn;
4609
4610 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4611 if (dyn.d_tag == DT_SONAME)
4612 {
4613 unsigned int tagv = dyn.d_un.d_val;
4614 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4615 if (soname == NULL)
4616 goto error_free_dyn;
4617 }
4618 if (dyn.d_tag == DT_NEEDED)
4619 {
4620 struct bfd_link_needed_list *n, **pn;
4621 char *fnm, *anm;
4622 unsigned int tagv = dyn.d_un.d_val;
4623 size_t amt = sizeof (struct bfd_link_needed_list);
4624
4625 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4626 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4627 if (n == NULL || fnm == NULL)
4628 goto error_free_dyn;
4629 amt = strlen (fnm) + 1;
4630 anm = (char *) bfd_alloc (abfd, amt);
4631 if (anm == NULL)
4632 goto error_free_dyn;
4633 memcpy (anm, fnm, amt);
4634 n->name = anm;
4635 n->by = abfd;
4636 n->next = NULL;
4637 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4638 ;
4639 *pn = n;
4640 }
4641 if (dyn.d_tag == DT_RUNPATH)
4642 {
4643 struct bfd_link_needed_list *n, **pn;
4644 char *fnm, *anm;
4645 unsigned int tagv = dyn.d_un.d_val;
4646 size_t amt = sizeof (struct bfd_link_needed_list);
4647
4648 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4649 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4650 if (n == NULL || fnm == NULL)
4651 goto error_free_dyn;
4652 amt = strlen (fnm) + 1;
4653 anm = (char *) bfd_alloc (abfd, amt);
4654 if (anm == NULL)
4655 goto error_free_dyn;
4656 memcpy (anm, fnm, amt);
4657 n->name = anm;
4658 n->by = abfd;
4659 n->next = NULL;
4660 for (pn = & runpath;
4661 *pn != NULL;
4662 pn = &(*pn)->next)
4663 ;
4664 *pn = n;
4665 }
4666 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4667 if (!runpath && dyn.d_tag == DT_RPATH)
4668 {
4669 struct bfd_link_needed_list *n, **pn;
4670 char *fnm, *anm;
4671 unsigned int tagv = dyn.d_un.d_val;
4672 size_t amt = sizeof (struct bfd_link_needed_list);
4673
4674 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4675 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4676 if (n == NULL || fnm == NULL)
4677 goto error_free_dyn;
4678 amt = strlen (fnm) + 1;
4679 anm = (char *) bfd_alloc (abfd, amt);
4680 if (anm == NULL)
4681 goto error_free_dyn;
4682 memcpy (anm, fnm, amt);
4683 n->name = anm;
4684 n->by = abfd;
4685 n->next = NULL;
4686 for (pn = & rpath;
4687 *pn != NULL;
4688 pn = &(*pn)->next)
4689 ;
4690 *pn = n;
4691 }
4692 if (dyn.d_tag == DT_AUDIT)
4693 {
4694 unsigned int tagv = dyn.d_un.d_val;
4695 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4696 }
4697 if (dyn.d_tag == DT_FLAGS_1)
4698 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4699 }
4700
4701 _bfd_elf_munmap_section_contents (s, dynbuf);
4702 }
4703
4704 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4705 frees all more recently bfd_alloc'd blocks as well. */
4706 if (runpath)
4707 rpath = runpath;
4708
4709 if (rpath)
4710 {
4711 struct bfd_link_needed_list **pn;
4712 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4713 ;
4714 *pn = rpath;
4715 }
4716
4717 /* If we have a PT_GNU_RELRO program header, mark as read-only
4718 all sections contained fully therein. This makes relro
4719 shared library sections appear as they will at run-time. */
4720 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4721 while (phdr-- > elf_tdata (abfd)->phdr)
4722 if (phdr->p_type == PT_GNU_RELRO)
4723 {
4724 for (s = abfd->sections; s != NULL; s = s->next)
4725 {
4726 unsigned int opb = bfd_octets_per_byte (abfd, s);
4727
4728 if ((s->flags & SEC_ALLOC) != 0
4729 && s->vma * opb >= phdr->p_vaddr
4730 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4731 s->flags |= SEC_READONLY;
4732 }
4733 break;
4734 }
4735
4736 /* We do not want to include any of the sections in a dynamic
4737 object in the output file. We hack by simply clobbering the
4738 list of sections in the BFD. This could be handled more
4739 cleanly by, say, a new section flag; the existing
4740 SEC_NEVER_LOAD flag is not the one we want, because that one
4741 still implies that the section takes up space in the output
4742 file. */
4743 bfd_section_list_clear (abfd);
4744
4745 /* Find the name to use in a DT_NEEDED entry that refers to this
4746 object. If the object has a DT_SONAME entry, we use it.
4747 Otherwise, if the generic linker stuck something in
4748 elf_dt_name, we use that. Otherwise, we just use the file
4749 name. */
4750 if (soname == NULL || *soname == '\0')
4751 {
4752 soname = elf_dt_name (abfd);
4753 if (soname == NULL || *soname == '\0')
4754 soname = bfd_get_filename (abfd);
4755 }
4756
4757 /* Save the SONAME because sometimes the linker emulation code
4758 will need to know it. */
4759 elf_dt_name (abfd) = soname;
4760
4761 /* If we have already included this dynamic object in the
4762 link, just ignore it. There is no reason to include a
4763 particular dynamic object more than once. */
4764 for (loaded_lib = htab->dyn_loaded;
4765 loaded_lib != NULL;
4766 loaded_lib = loaded_lib->next)
4767 {
4768 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4769 return true;
4770 }
4771
4772 /* Create dynamic sections for backends that require that be done
4773 before setup_gnu_properties. */
4774 if (add_needed
4775 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4776 return false;
4777
4778 /* Save the DT_AUDIT entry for the linker emulation code. */
4779 elf_dt_audit (abfd) = audit;
4780 }
4781
4782 /* If this is a dynamic object, we always link against the .dynsym
4783 symbol table, not the .symtab symbol table. The dynamic linker
4784 will only see the .dynsym symbol table, so there is no reason to
4785 look at .symtab for a dynamic object. */
4786
4787 if (! dynamic || elf_dynsymtab (abfd) == 0)
4788 hdr = &elf_tdata (abfd)->symtab_hdr;
4789 else
4790 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4791
4792 symcount = hdr->sh_size / bed->s->sizeof_sym;
4793
4794 /* The sh_info field of the symtab header tells us where the
4795 external symbols start. We don't care about the local symbols at
4796 this point. */
4797 if (elf_bad_symtab (abfd))
4798 {
4799 extsymcount = symcount;
4800 extsymoff = 0;
4801 }
4802 else
4803 {
4804 extsymcount = symcount - hdr->sh_info;
4805 extsymoff = hdr->sh_info;
4806 }
4807
4808 sym_hash = elf_sym_hashes (abfd);
4809 if (extsymcount != 0)
4810 {
4811 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4812 NULL, NULL, NULL);
4813 if (isymbuf == NULL)
4814 goto error_return;
4815
4816 if (sym_hash == NULL)
4817 {
4818 /* We store a pointer to the hash table entry for each
4819 external symbol. */
4820 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4821 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4822 if (sym_hash == NULL)
4823 goto error_free_sym;
4824 elf_sym_hashes (abfd) = sym_hash;
4825 }
4826 }
4827
4828 if (dynamic)
4829 {
4830 /* Read in any version definitions. */
4831 if (!_bfd_elf_slurp_version_tables (abfd,
4832 info->default_imported_symver))
4833 goto error_free_sym;
4834
4835 /* Read in the symbol versions, but don't bother to convert them
4836 to internal format. */
4837 if (elf_dynversym (abfd) != 0)
4838 {
4839 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4840 bfd_size_type amt = versymhdr->sh_size;
4841
4842 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4843 goto error_free_sym;
4844 extversym = (Elf_External_Versym *)
4845 _bfd_malloc_and_read (abfd, amt, amt);
4846 if (extversym == NULL)
4847 goto error_free_sym;
4848 extversym_end = extversym + amt / sizeof (*extversym);
4849 }
4850 }
4851
4852 /* If we are loading an as-needed shared lib, save the symbol table
4853 state before we start adding symbols. If the lib turns out
4854 to be unneeded, restore the state. */
4855 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4856 {
4857 unsigned int i;
4858 size_t entsize;
4859
4860 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4861 {
4862 struct bfd_hash_entry *p;
4863 struct elf_link_hash_entry *h;
4864
4865 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4866 {
4867 h = (struct elf_link_hash_entry *) p;
4868 entsize += htab->root.table.entsize;
4869 if (h->root.type == bfd_link_hash_warning)
4870 {
4871 entsize += htab->root.table.entsize;
4872 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4873 }
4874 if (h->root.type == bfd_link_hash_common)
4875 entsize += sizeof (*h->root.u.c.p);
4876 }
4877 }
4878
4879 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4880 old_tab = bfd_malloc (tabsize + entsize);
4881 if (old_tab == NULL)
4882 goto error_free_vers;
4883
4884 /* Remember the current objalloc pointer, so that all mem for
4885 symbols added can later be reclaimed. */
4886 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4887 if (alloc_mark == NULL)
4888 goto error_free_vers;
4889
4890 /* Make a special call to the linker "notice" function to
4891 tell it that we are about to handle an as-needed lib. */
4892 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4893 goto error_free_vers;
4894
4895 /* Clone the symbol table. Remember some pointers into the
4896 symbol table, and dynamic symbol count. */
4897 old_ent = (char *) old_tab + tabsize;
4898 memcpy (old_tab, htab->root.table.table, tabsize);
4899 old_undefs = htab->root.undefs;
4900 old_undefs_tail = htab->root.undefs_tail;
4901 old_table = htab->root.table.table;
4902 old_size = htab->root.table.size;
4903 old_count = htab->root.table.count;
4904 old_strtab = NULL;
4905 if (htab->dynstr != NULL)
4906 {
4907 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4908 if (old_strtab == NULL)
4909 goto error_free_vers;
4910 }
4911
4912 for (i = 0; i < htab->root.table.size; i++)
4913 {
4914 struct bfd_hash_entry *p;
4915 struct elf_link_hash_entry *h;
4916
4917 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4918 {
4919 h = (struct elf_link_hash_entry *) p;
4920 memcpy (old_ent, h, htab->root.table.entsize);
4921 old_ent = (char *) old_ent + htab->root.table.entsize;
4922 if (h->root.type == bfd_link_hash_warning)
4923 {
4924 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4925 memcpy (old_ent, h, htab->root.table.entsize);
4926 old_ent = (char *) old_ent + htab->root.table.entsize;
4927 }
4928 if (h->root.type == bfd_link_hash_common)
4929 {
4930 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4931 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4932 }
4933 }
4934 }
4935 }
4936
4937 weaks = NULL;
4938 if (extversym == NULL)
4939 ever = NULL;
4940 else if (extversym + extsymoff < extversym_end)
4941 ever = extversym + extsymoff;
4942 else
4943 {
4944 /* xgettext:c-format */
4945 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4946 abfd, (long) extsymoff,
4947 (long) (extversym_end - extversym) / sizeof (* extversym));
4948 bfd_set_error (bfd_error_bad_value);
4949 goto error_free_vers;
4950 }
4951
4952 if (!bfd_link_relocatable (info)
4953 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4954 {
4955 _bfd_error_handler
4956 (_("%pB: plugin needed to handle lto object"), abfd);
4957 }
4958
4959 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4960 isym < isymend;
4961 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4962 {
4963 int bind;
4964 bfd_vma value;
4965 asection *sec, *new_sec;
4966 flagword flags;
4967 const char *name;
4968 const char *defvername;
4969 bool must_copy_name = false;
4970 struct elf_link_hash_entry *h;
4971 struct elf_link_hash_entry *hi;
4972 bool definition;
4973 bool size_change_ok;
4974 bool type_change_ok;
4975 bool new_weak;
4976 bool old_weak;
4977 bfd *override;
4978 bool common;
4979 bool discarded;
4980 unsigned int old_alignment;
4981 unsigned int shindex;
4982 bfd *old_bfd;
4983 bool matched;
4984
4985 override = NULL;
4986
4987 flags = BSF_NO_FLAGS;
4988 sec = NULL;
4989 value = isym->st_value;
4990 common = bed->common_definition (isym);
4991 if (common && info->inhibit_common_definition)
4992 {
4993 /* Treat common symbol as undefined for --no-define-common. */
4994 isym->st_shndx = SHN_UNDEF;
4995 common = false;
4996 }
4997 discarded = false;
4998
4999 bind = ELF_ST_BIND (isym->st_info);
5000 switch (bind)
5001 {
5002 case STB_LOCAL:
5003 /* This should be impossible, since ELF requires that all
5004 global symbols follow all local symbols, and that sh_info
5005 point to the first global symbol. Unfortunately, Irix 5
5006 screws this up. */
5007 if (elf_bad_symtab (abfd))
5008 continue;
5009
5010 /* If we aren't prepared to handle locals within the globals
5011 then we'll likely segfault on a NULL symbol hash if the
5012 symbol is ever referenced in relocations. */
5013 shindex = elf_elfheader (abfd)->e_shstrndx;
5014 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5015 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5016 " (>= sh_info of %lu)"),
5017 abfd, name, (long) (isym - isymbuf + extsymoff),
5018 (long) extsymoff);
5019
5020 /* Dynamic object relocations are not processed by ld, so
5021 ld won't run into the problem mentioned above. */
5022 if (dynamic)
5023 continue;
5024 bfd_set_error (bfd_error_bad_value);
5025 goto error_free_vers;
5026
5027 case STB_GLOBAL:
5028 if (isym->st_shndx != SHN_UNDEF && !common)
5029 flags = BSF_GLOBAL;
5030 break;
5031
5032 case STB_WEAK:
5033 flags = BSF_WEAK;
5034 break;
5035
5036 case STB_GNU_UNIQUE:
5037 flags = BSF_GNU_UNIQUE;
5038 break;
5039
5040 default:
5041 /* Leave it up to the processor backend. */
5042 break;
5043 }
5044
5045 if (isym->st_shndx == SHN_UNDEF)
5046 sec = bfd_und_section_ptr;
5047 else if (isym->st_shndx == SHN_ABS)
5048 sec = bfd_abs_section_ptr;
5049 else if (isym->st_shndx == SHN_COMMON)
5050 {
5051 sec = bfd_com_section_ptr;
5052 /* What ELF calls the size we call the value. What ELF
5053 calls the value we call the alignment. */
5054 value = isym->st_size;
5055 }
5056 else
5057 {
5058 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5059 if (sec == NULL)
5060 sec = bfd_abs_section_ptr;
5061 else if (discarded_section (sec))
5062 {
5063 /* Symbols from discarded section are undefined. We keep
5064 its visibility. */
5065 sec = bfd_und_section_ptr;
5066 discarded = true;
5067 isym->st_shndx = SHN_UNDEF;
5068 }
5069 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5070 value -= sec->vma;
5071 }
5072
5073 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5074 isym->st_name);
5075 if (name == NULL)
5076 goto error_free_vers;
5077
5078 if (isym->st_shndx == SHN_COMMON
5079 && (abfd->flags & BFD_PLUGIN) != 0)
5080 {
5081 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5082
5083 if (xc == NULL)
5084 {
5085 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5086 | SEC_EXCLUDE);
5087 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5088 if (xc == NULL)
5089 goto error_free_vers;
5090 }
5091 sec = xc;
5092 }
5093 else if (isym->st_shndx == SHN_COMMON
5094 && ELF_ST_TYPE (isym->st_info) == STT_TLS
5095 && !bfd_link_relocatable (info))
5096 {
5097 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5098
5099 if (tcomm == NULL)
5100 {
5101 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5102 | SEC_LINKER_CREATED);
5103 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5104 if (tcomm == NULL)
5105 goto error_free_vers;
5106 }
5107 sec = tcomm;
5108 }
5109 else if (bed->elf_add_symbol_hook)
5110 {
5111 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5112 &sec, &value))
5113 goto error_free_vers;
5114
5115 /* The hook function sets the name to NULL if this symbol
5116 should be skipped for some reason. */
5117 if (name == NULL)
5118 continue;
5119 }
5120
5121 /* Sanity check that all possibilities were handled. */
5122 if (sec == NULL)
5123 abort ();
5124
5125 /* Silently discard TLS symbols from --just-syms. There's
5126 no way to combine a static TLS block with a new TLS block
5127 for this executable. */
5128 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5129 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5130 continue;
5131
5132 if (bfd_is_und_section (sec)
5133 || bfd_is_com_section (sec))
5134 definition = false;
5135 else
5136 definition = true;
5137
5138 size_change_ok = false;
5139 type_change_ok = bed->type_change_ok;
5140 old_weak = false;
5141 matched = false;
5142 old_alignment = 0;
5143 old_bfd = NULL;
5144 new_sec = sec;
5145 defvername = NULL;
5146
5147 if (is_elf_hash_table (&htab->root))
5148 {
5149 Elf_Internal_Versym iver;
5150 unsigned int vernum = 0;
5151 bool skip;
5152
5153 if (ever == NULL)
5154 {
5155 if (info->default_imported_symver)
5156 /* Use the default symbol version created earlier. */
5157 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5158 else
5159 iver.vs_vers = 0;
5160 }
5161 else if (ever >= extversym_end)
5162 {
5163 /* xgettext:c-format */
5164 _bfd_error_handler (_("%pB: not enough version information"),
5165 abfd);
5166 bfd_set_error (bfd_error_bad_value);
5167 goto error_free_vers;
5168 }
5169 else
5170 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5171
5172 vernum = iver.vs_vers & VERSYM_VERSION;
5173
5174 /* If this is a hidden symbol, or if it is not version
5175 1, we append the version name to the symbol name.
5176 However, we do not modify a non-hidden absolute symbol
5177 if it is not a function, because it might be the version
5178 symbol itself. FIXME: What if it isn't? */
5179 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5180 || (vernum > 1
5181 && (!bfd_is_abs_section (sec)
5182 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5183 {
5184 const char *verstr;
5185 size_t namelen, verlen, newlen;
5186 char *newname, *p;
5187
5188 if (isym->st_shndx != SHN_UNDEF)
5189 {
5190 if (vernum > elf_tdata (abfd)->cverdefs)
5191 verstr = NULL;
5192 else if (vernum > 1)
5193 verstr =
5194 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5195 else
5196 verstr = "";
5197
5198 if (verstr == NULL)
5199 {
5200 _bfd_error_handler
5201 /* xgettext:c-format */
5202 (_("%pB: %s: invalid version %u (max %d)"),
5203 abfd, name, vernum,
5204 elf_tdata (abfd)->cverdefs);
5205 bfd_set_error (bfd_error_bad_value);
5206 goto error_free_vers;
5207 }
5208 }
5209 else
5210 {
5211 /* We cannot simply test for the number of
5212 entries in the VERNEED section since the
5213 numbers for the needed versions do not start
5214 at 0. */
5215 Elf_Internal_Verneed *t;
5216
5217 verstr = NULL;
5218 for (t = elf_tdata (abfd)->verref;
5219 t != NULL;
5220 t = t->vn_nextref)
5221 {
5222 Elf_Internal_Vernaux *a;
5223
5224 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5225 {
5226 if (a->vna_other == vernum)
5227 {
5228 verstr = a->vna_nodename;
5229 break;
5230 }
5231 }
5232 if (a != NULL)
5233 break;
5234 }
5235 if (verstr == NULL)
5236 {
5237 _bfd_error_handler
5238 /* xgettext:c-format */
5239 (_("%pB: %s: invalid needed version %d"),
5240 abfd, name, vernum);
5241 bfd_set_error (bfd_error_bad_value);
5242 goto error_free_vers;
5243 }
5244 }
5245
5246 namelen = strlen (name);
5247 verlen = strlen (verstr);
5248 newlen = namelen + verlen + 2;
5249 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5250 && isym->st_shndx != SHN_UNDEF)
5251 ++newlen;
5252
5253 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5254 if (newname == NULL)
5255 goto error_free_vers;
5256 memcpy (newname, name, namelen);
5257 p = newname + namelen;
5258 *p++ = ELF_VER_CHR;
5259 /* If this is a defined non-hidden version symbol,
5260 we add another @ to the name. This indicates the
5261 default version of the symbol. */
5262 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5263 && isym->st_shndx != SHN_UNDEF)
5264 *p++ = ELF_VER_CHR, defvername = name;
5265 memcpy (p, verstr, verlen + 1);
5266
5267 name = newname;
5268 /* Since bfd_hash_alloc is used for "name", the string
5269 must be copied if added to first_hash. The string
5270 memory can be freed when an --as-needed library is
5271 not needed. */
5272 must_copy_name = true;
5273 }
5274
5275 /* If this symbol has default visibility and the user has
5276 requested we not re-export it, then mark it as hidden. */
5277 if (!bfd_is_und_section (sec)
5278 && !dynamic
5279 && abfd->no_export
5280 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5281 isym->st_other = (STV_HIDDEN
5282 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5283
5284 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5285 sym_hash, &old_bfd, &old_weak,
5286 &old_alignment, &skip, &override,
5287 &type_change_ok, &size_change_ok,
5288 &matched))
5289 goto error_free_vers;
5290
5291 if (skip)
5292 continue;
5293
5294 h = *sym_hash;
5295 while (h->root.type == bfd_link_hash_indirect
5296 || h->root.type == bfd_link_hash_warning)
5297 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5298
5299 /* Override a definition only if the new symbol matches the
5300 existing one. */
5301 if (override && matched)
5302 {
5303 definition = false;
5304 if (htab->first_hash != NULL
5305 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5306 && h->root.non_ir_ref_regular)
5307 {
5308 /* When reloading --as-needed shared objects for new
5309 symbols added from IR inputs, if this shared object
5310 has the first definition, use it. */
5311 struct elf_link_first_hash_entry *e
5312 = ((struct elf_link_first_hash_entry *)
5313 bfd_hash_lookup (htab->first_hash, name, false,
5314 false));
5315 if (e != NULL && e->abfd == abfd)
5316 definition = true;
5317 }
5318 }
5319
5320 if (h->versioned != unversioned
5321 && elf_tdata (abfd)->verdef != NULL
5322 && vernum > 1
5323 && definition)
5324 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5325 }
5326
5327 if (! (_bfd_generic_link_add_one_symbol
5328 (info, override ? override : abfd, name, flags, sec, value,
5329 NULL, false, bed->collect,
5330 (struct bfd_link_hash_entry **) sym_hash)))
5331 goto error_free_vers;
5332
5333 h = *sym_hash;
5334 /* We need to make sure that indirect symbol dynamic flags are
5335 updated. */
5336 hi = h;
5337 while (h->root.type == bfd_link_hash_indirect
5338 || h->root.type == bfd_link_hash_warning)
5339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5340
5341 *sym_hash = h;
5342
5343 /* Setting the index to -3 tells elf_link_output_extsym that
5344 this symbol is defined in a discarded section. */
5345 if (discarded && is_elf_hash_table (&htab->root))
5346 h->indx = -3;
5347
5348 new_weak = (flags & BSF_WEAK) != 0;
5349 if (dynamic
5350 && definition
5351 && new_weak
5352 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5353 && is_elf_hash_table (&htab->root)
5354 && h->u.alias == NULL)
5355 {
5356 /* Keep a list of all weak defined non function symbols from
5357 a dynamic object, using the alias field. Later in this
5358 function we will set the alias field to the correct
5359 value. We only put non-function symbols from dynamic
5360 objects on this list, because that happens to be the only
5361 time we need to know the normal symbol corresponding to a
5362 weak symbol, and the information is time consuming to
5363 figure out. If the alias field is not already NULL,
5364 then this symbol was already defined by some previous
5365 dynamic object, and we will be using that previous
5366 definition anyhow. */
5367
5368 h->u.alias = weaks;
5369 weaks = h;
5370 }
5371
5372 /* Set the alignment of a common symbol. */
5373 if ((common || bfd_is_com_section (sec))
5374 && h->root.type == bfd_link_hash_common)
5375 {
5376 unsigned int align;
5377
5378 if (common)
5379 align = bfd_log2 (isym->st_value);
5380 else
5381 {
5382 /* The new symbol is a common symbol in a shared object.
5383 We need to get the alignment from the section. */
5384 align = new_sec->alignment_power;
5385 }
5386 if (align > old_alignment)
5387 h->root.u.c.p->alignment_power = align;
5388 else
5389 h->root.u.c.p->alignment_power = old_alignment;
5390 }
5391
5392 if (is_elf_hash_table (&htab->root))
5393 {
5394 /* Set a flag in the hash table entry indicating the type of
5395 reference or definition we just found. A dynamic symbol
5396 is one which is referenced or defined by both a regular
5397 object and a shared object. */
5398 bool dynsym = false;
5399
5400 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5401 if ((abfd->flags & BFD_PLUGIN) != 0)
5402 {
5403 /* Except for this flag to track nonweak references. */
5404 if (!definition
5405 && bind != STB_WEAK)
5406 h->ref_ir_nonweak = 1;
5407 }
5408 else if (!dynamic)
5409 {
5410 if (! definition)
5411 {
5412 h->ref_regular = 1;
5413 if (bind != STB_WEAK)
5414 h->ref_regular_nonweak = 1;
5415 }
5416 else
5417 {
5418 h->def_regular = 1;
5419 if (h->def_dynamic)
5420 {
5421 h->def_dynamic = 0;
5422 h->ref_dynamic = 1;
5423 }
5424 }
5425 }
5426 else
5427 {
5428 if (! definition)
5429 {
5430 h->ref_dynamic = 1;
5431 hi->ref_dynamic = 1;
5432 }
5433 else
5434 {
5435 h->def_dynamic = 1;
5436 hi->def_dynamic = 1;
5437 }
5438 }
5439
5440 /* If an indirect symbol has been forced local, don't
5441 make the real symbol dynamic. */
5442 if (h != hi && hi->forced_local)
5443 ;
5444 else if (!dynamic)
5445 {
5446 if (bfd_link_dll (info)
5447 || h->def_dynamic
5448 || h->ref_dynamic)
5449 dynsym = true;
5450 }
5451 else
5452 {
5453 if (h->def_regular
5454 || h->ref_regular
5455 || (h->is_weakalias
5456 && weakdef (h)->dynindx != -1))
5457 dynsym = true;
5458 }
5459
5460 /* Check to see if we need to add an indirect symbol for
5461 the default name. */
5462 if ((definition
5463 || (!override && h->root.type == bfd_link_hash_common))
5464 && !(hi != h
5465 && hi->versioned == versioned_hidden))
5466 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5467 sec, value, &old_bfd, &dynsym))
5468 goto error_free_vers;
5469
5470 /* Check the alignment when a common symbol is involved. This
5471 can change when a common symbol is overridden by a normal
5472 definition or a common symbol is ignored due to the old
5473 normal definition. We need to make sure the maximum
5474 alignment is maintained. */
5475 if ((old_alignment || common)
5476 && h->root.type != bfd_link_hash_common)
5477 {
5478 unsigned int common_align;
5479 unsigned int normal_align;
5480 unsigned int symbol_align;
5481 bfd *normal_bfd;
5482 bfd *common_bfd;
5483
5484 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5485 || h->root.type == bfd_link_hash_defweak);
5486
5487 symbol_align = ffs (h->root.u.def.value) - 1;
5488 if (h->root.u.def.section->owner != NULL
5489 && (h->root.u.def.section->owner->flags
5490 & (DYNAMIC | BFD_PLUGIN)) == 0)
5491 {
5492 normal_align = h->root.u.def.section->alignment_power;
5493 if (normal_align > symbol_align)
5494 normal_align = symbol_align;
5495 }
5496 else
5497 normal_align = symbol_align;
5498
5499 if (old_alignment)
5500 {
5501 common_align = old_alignment;
5502 common_bfd = old_bfd;
5503 normal_bfd = abfd;
5504 }
5505 else
5506 {
5507 common_align = bfd_log2 (isym->st_value);
5508 common_bfd = abfd;
5509 normal_bfd = old_bfd;
5510 }
5511
5512 if (normal_align < common_align)
5513 {
5514 /* PR binutils/2735 */
5515 if (normal_bfd == NULL)
5516 _bfd_error_handler
5517 /* xgettext:c-format */
5518 (_("warning: alignment %u of common symbol `%s' in %pB is"
5519 " greater than the alignment (%u) of its section %pA"),
5520 1 << common_align, name, common_bfd,
5521 1 << normal_align, h->root.u.def.section);
5522 else
5523 _bfd_error_handler
5524 /* xgettext:c-format */
5525 (_("warning: alignment %u of normal symbol `%s' in %pB"
5526 " is smaller than %u used by the common definition in %pB"),
5527 1 << normal_align, name, normal_bfd,
5528 1 << common_align, common_bfd);
5529
5530 /* PR 30499: make sure that users understand that this warning is serious. */
5531 _bfd_error_handler
5532 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5533 }
5534 }
5535
5536 /* Remember the symbol size if it isn't undefined. */
5537 if (isym->st_size != 0
5538 && isym->st_shndx != SHN_UNDEF
5539 && (definition || h->size == 0))
5540 {
5541 if (h->size != 0
5542 && h->size != isym->st_size
5543 && ! size_change_ok)
5544 {
5545 _bfd_error_handler
5546 /* xgettext:c-format */
5547 (_("warning: size of symbol `%s' changed"
5548 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5549 name, (uint64_t) h->size, old_bfd,
5550 (uint64_t) isym->st_size, abfd);
5551
5552 /* PR 30499: make sure that users understand that this warning is serious. */
5553 _bfd_error_handler
5554 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5555 }
5556
5557 h->size = isym->st_size;
5558 }
5559
5560 /* If this is a common symbol, then we always want H->SIZE
5561 to be the size of the common symbol. The code just above
5562 won't fix the size if a common symbol becomes larger. We
5563 don't warn about a size change here, because that is
5564 covered by --warn-common. Allow changes between different
5565 function types. */
5566 if (h->root.type == bfd_link_hash_common)
5567 h->size = h->root.u.c.size;
5568
5569 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5570 && ((definition && !new_weak)
5571 || (old_weak && h->root.type == bfd_link_hash_common)
5572 || h->type == STT_NOTYPE))
5573 {
5574 unsigned int type = ELF_ST_TYPE (isym->st_info);
5575
5576 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5577 symbol. */
5578 if (type == STT_GNU_IFUNC
5579 && (abfd->flags & DYNAMIC) != 0)
5580 type = STT_FUNC;
5581
5582 if (h->type != type)
5583 {
5584 if (h->type != STT_NOTYPE && ! type_change_ok)
5585 /* xgettext:c-format */
5586 _bfd_error_handler
5587 (_("warning: type of symbol `%s' changed"
5588 " from %d to %d in %pB"),
5589 name, h->type, type, abfd);
5590
5591 h->type = type;
5592 }
5593 }
5594
5595 /* Merge st_other field. */
5596 elf_merge_st_other (abfd, h, isym->st_other, sec,
5597 definition, dynamic);
5598
5599 /* We don't want to make debug symbol dynamic. */
5600 if (definition
5601 && (sec->flags & SEC_DEBUGGING)
5602 && !bfd_link_relocatable (info))
5603 dynsym = false;
5604
5605 /* Nor should we make plugin symbols dynamic. */
5606 if ((abfd->flags & BFD_PLUGIN) != 0)
5607 dynsym = false;
5608
5609 if (definition)
5610 {
5611 h->target_internal = isym->st_target_internal;
5612 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5613 }
5614
5615 /* Don't add indirect symbols for .symver x, x@FOO aliases
5616 in IR. Since all data or text symbols in IR have the
5617 same type, value and section, we can't tell if a symbol
5618 is an alias of another symbol by their types, values and
5619 sections. */
5620 if (definition
5621 && !dynamic
5622 && (abfd->flags & BFD_PLUGIN) == 0)
5623 {
5624 char *p = strchr (name, ELF_VER_CHR);
5625 if (p != NULL && p[1] != ELF_VER_CHR)
5626 {
5627 /* Queue non-default versions so that .symver x, x@FOO
5628 aliases can be checked. */
5629 if (!nondeflt_vers)
5630 {
5631 size_t amt = ((isymend - isym + 1)
5632 * sizeof (struct elf_link_hash_entry *));
5633 nondeflt_vers
5634 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5635 if (!nondeflt_vers)
5636 goto error_free_vers;
5637 }
5638 nondeflt_vers[nondeflt_vers_cnt++] = h;
5639 }
5640 }
5641
5642 if (dynsym && h->dynindx == -1)
5643 {
5644 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5645 goto error_free_vers;
5646 if (h->is_weakalias
5647 && weakdef (h)->dynindx == -1)
5648 {
5649 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5650 goto error_free_vers;
5651 }
5652 }
5653 else if (h->dynindx != -1)
5654 /* If the symbol already has a dynamic index, but
5655 visibility says it should not be visible, turn it into
5656 a local symbol. */
5657 switch (ELF_ST_VISIBILITY (h->other))
5658 {
5659 case STV_INTERNAL:
5660 case STV_HIDDEN:
5661 (*bed->elf_backend_hide_symbol) (info, h, true);
5662 dynsym = false;
5663 break;
5664 }
5665
5666 if (!add_needed
5667 && matched
5668 && definition
5669 && h->root.type != bfd_link_hash_indirect)
5670 {
5671 if ((dynsym
5672 && h->ref_regular_nonweak)
5673 || (old_bfd != NULL
5674 && (old_bfd->flags & BFD_PLUGIN) != 0
5675 && h->ref_ir_nonweak
5676 && !info->lto_all_symbols_read)
5677 || (h->ref_dynamic_nonweak
5678 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5679 && !on_needed_list (elf_dt_name (abfd),
5680 htab->needed, NULL)))
5681 {
5682 const char *soname = elf_dt_name (abfd);
5683
5684 info->callbacks->minfo ("%!", soname, old_bfd,
5685 h->root.root.string);
5686
5687 /* A symbol from a library loaded via DT_NEEDED of some
5688 other library is referenced by a regular object.
5689 Add a DT_NEEDED entry for it. Issue an error if
5690 --no-add-needed is used and the reference was not
5691 a weak one. */
5692 if (old_bfd != NULL
5693 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5694 {
5695 _bfd_error_handler
5696 /* xgettext:c-format */
5697 (_("%pB: undefined reference to symbol '%s'"),
5698 old_bfd, name);
5699 bfd_set_error (bfd_error_missing_dso);
5700 goto error_free_vers;
5701 }
5702
5703 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5704 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5705
5706 /* Create dynamic sections for backends that require
5707 that be done before setup_gnu_properties. */
5708 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5709 goto error_free_vers;
5710 add_needed = true;
5711 }
5712 else if (dynamic
5713 && h->root.u.def.section->owner == abfd)
5714 {
5715 /* Add this symbol to first hash if this shared
5716 object has the first definition. */
5717 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5718 /* And if it was the default symbol version definition,
5719 also add the short name. */
5720 if (defvername)
5721 elf_link_add_to_first_hash (abfd, info, defvername, false);
5722 }
5723 }
5724 }
5725 }
5726
5727 if (info->lto_plugin_active
5728 && !bfd_link_relocatable (info)
5729 && (abfd->flags & BFD_PLUGIN) == 0
5730 && !just_syms
5731 && extsymcount != 0
5732 && is_elf_hash_table (&htab->root))
5733 {
5734 int r_sym_shift;
5735
5736 if (bed->s->arch_size == 32)
5737 r_sym_shift = 8;
5738 else
5739 r_sym_shift = 32;
5740
5741 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5742 referenced in regular objects so that linker plugin will get
5743 the correct symbol resolution. */
5744
5745 sym_hash = elf_sym_hashes (abfd);
5746 for (s = abfd->sections; s != NULL; s = s->next)
5747 {
5748 Elf_Internal_Rela *internal_relocs;
5749 Elf_Internal_Rela *rel, *relend;
5750
5751 /* Don't check relocations in excluded sections. */
5752 if ((s->flags & SEC_RELOC) == 0
5753 || s->reloc_count == 0
5754 || (s->flags & SEC_EXCLUDE) != 0
5755 || (s->flags & SEC_DEBUGGING) != 0)
5756 continue;
5757
5758 internal_relocs = _bfd_elf_link_info_read_relocs
5759 (abfd, info, s, NULL, NULL,
5760 _bfd_elf_link_keep_memory (info));
5761 if (internal_relocs == NULL)
5762 goto error_free_vers;
5763
5764 rel = internal_relocs;
5765 relend = rel + s->reloc_count;
5766 for ( ; rel < relend; rel++)
5767 {
5768 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5769 struct elf_link_hash_entry *h;
5770
5771 /* Skip local symbols. */
5772 if (r_symndx < extsymoff)
5773 continue;
5774
5775 h = sym_hash[r_symndx - extsymoff];
5776 if (h != NULL)
5777 h->root.non_ir_ref_regular = 1;
5778 }
5779
5780 if (elf_section_data (s)->relocs != internal_relocs)
5781 free (internal_relocs);
5782 }
5783 }
5784
5785 free (extversym);
5786 extversym = NULL;
5787 free (isymbuf);
5788 isymbuf = NULL;
5789
5790 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5791 {
5792 unsigned int i;
5793
5794 /* Restore the symbol table. */
5795 old_ent = (char *) old_tab + tabsize;
5796 memset (elf_sym_hashes (abfd), 0,
5797 extsymcount * sizeof (struct elf_link_hash_entry *));
5798 htab->root.table.table = old_table;
5799 htab->root.table.size = old_size;
5800 htab->root.table.count = old_count;
5801 memcpy (htab->root.table.table, old_tab, tabsize);
5802 htab->root.undefs = old_undefs;
5803 htab->root.undefs_tail = old_undefs_tail;
5804 if (htab->dynstr != NULL)
5805 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5806 free (old_strtab);
5807 old_strtab = NULL;
5808 for (i = 0; i < htab->root.table.size; i++)
5809 {
5810 struct bfd_hash_entry *p;
5811 struct elf_link_hash_entry *h;
5812 unsigned int non_ir_ref_dynamic;
5813
5814 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5815 {
5816 /* Preserve non_ir_ref_dynamic so that this symbol
5817 will be exported when the dynamic lib becomes needed
5818 in the second pass. */
5819 h = (struct elf_link_hash_entry *) p;
5820 if (h->root.type == bfd_link_hash_warning)
5821 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5822 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5823
5824 h = (struct elf_link_hash_entry *) p;
5825 memcpy (h, old_ent, htab->root.table.entsize);
5826 old_ent = (char *) old_ent + htab->root.table.entsize;
5827 if (h->root.type == bfd_link_hash_warning)
5828 {
5829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5830 memcpy (h, old_ent, htab->root.table.entsize);
5831 old_ent = (char *) old_ent + htab->root.table.entsize;
5832 }
5833 if (h->root.type == bfd_link_hash_common)
5834 {
5835 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5836 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5837 }
5838 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5839 }
5840 }
5841
5842 /* Make a special call to the linker "notice" function to
5843 tell it that symbols added for crefs may need to be removed. */
5844 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5845 goto error_free_vers;
5846
5847 free (old_tab);
5848 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5849 alloc_mark);
5850 free (nondeflt_vers);
5851 return true;
5852 }
5853
5854 free (old_strtab);
5855 old_strtab = NULL;
5856 if (old_tab != NULL)
5857 {
5858 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5859 goto error_free_vers;
5860 free (old_tab);
5861 old_tab = NULL;
5862 }
5863
5864 /* Now that all the symbols from this input file are created, if
5865 not performing a relocatable link, handle .symver foo, foo@BAR
5866 such that any relocs against foo become foo@BAR. */
5867 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5868 {
5869 size_t cnt, symidx;
5870
5871 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5872 {
5873 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5874 char *shortname, *p;
5875 size_t amt;
5876
5877 p = strchr (h->root.root.string, ELF_VER_CHR);
5878 if (p == NULL
5879 || (h->root.type != bfd_link_hash_defined
5880 && h->root.type != bfd_link_hash_defweak))
5881 continue;
5882
5883 amt = p - h->root.root.string;
5884 shortname = (char *) bfd_malloc (amt + 1);
5885 if (!shortname)
5886 goto error_free_vers;
5887 memcpy (shortname, h->root.root.string, amt);
5888 shortname[amt] = '\0';
5889
5890 hi = (struct elf_link_hash_entry *)
5891 bfd_link_hash_lookup (&htab->root, shortname,
5892 false, false, false);
5893 if (hi != NULL
5894 && hi->root.type == h->root.type
5895 && hi->root.u.def.value == h->root.u.def.value
5896 && hi->root.u.def.section == h->root.u.def.section)
5897 {
5898 (*bed->elf_backend_hide_symbol) (info, hi, true);
5899 hi->root.type = bfd_link_hash_indirect;
5900 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5901 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5902 sym_hash = elf_sym_hashes (abfd);
5903 if (sym_hash)
5904 for (symidx = 0; symidx < extsymcount; ++symidx)
5905 if (sym_hash[symidx] == hi)
5906 {
5907 sym_hash[symidx] = h;
5908 break;
5909 }
5910 }
5911 free (shortname);
5912 }
5913 }
5914 free (nondeflt_vers);
5915 nondeflt_vers = NULL;
5916
5917 /* Now set the alias field correctly for all the weak defined
5918 symbols we found. The only way to do this is to search all the
5919 symbols. Since we only need the information for non functions in
5920 dynamic objects, that's the only time we actually put anything on
5921 the list WEAKS. We need this information so that if a regular
5922 object refers to a symbol defined weakly in a dynamic object, the
5923 real symbol in the dynamic object is also put in the dynamic
5924 symbols; we also must arrange for both symbols to point to the
5925 same memory location. We could handle the general case of symbol
5926 aliasing, but a general symbol alias can only be generated in
5927 assembler code, handling it correctly would be very time
5928 consuming, and other ELF linkers don't handle general aliasing
5929 either. */
5930 if (weaks != NULL)
5931 {
5932 struct elf_link_hash_entry **hpp;
5933 struct elf_link_hash_entry **hppend;
5934 struct elf_link_hash_entry **sorted_sym_hash;
5935 struct elf_link_hash_entry *h;
5936 size_t sym_count, amt;
5937
5938 /* Since we have to search the whole symbol list for each weak
5939 defined symbol, search time for N weak defined symbols will be
5940 O(N^2). Binary search will cut it down to O(NlogN). */
5941 amt = extsymcount * sizeof (*sorted_sym_hash);
5942 sorted_sym_hash = bfd_malloc (amt);
5943 if (sorted_sym_hash == NULL)
5944 goto error_return;
5945 sym_hash = sorted_sym_hash;
5946 hpp = elf_sym_hashes (abfd);
5947 hppend = hpp + extsymcount;
5948 sym_count = 0;
5949 for (; hpp < hppend; hpp++)
5950 {
5951 h = *hpp;
5952 if (h != NULL
5953 && h->root.type == bfd_link_hash_defined
5954 && !bed->is_function_type (h->type))
5955 {
5956 *sym_hash = h;
5957 sym_hash++;
5958 sym_count++;
5959 }
5960 }
5961
5962 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5963 elf_sort_symbol);
5964
5965 while (weaks != NULL)
5966 {
5967 struct elf_link_hash_entry *hlook;
5968 asection *slook;
5969 bfd_vma vlook;
5970 size_t i, j, idx = 0;
5971
5972 hlook = weaks;
5973 weaks = hlook->u.alias;
5974 hlook->u.alias = NULL;
5975
5976 if (hlook->root.type != bfd_link_hash_defined
5977 && hlook->root.type != bfd_link_hash_defweak)
5978 continue;
5979
5980 slook = hlook->root.u.def.section;
5981 vlook = hlook->root.u.def.value;
5982
5983 i = 0;
5984 j = sym_count;
5985 while (i != j)
5986 {
5987 bfd_signed_vma vdiff;
5988 idx = (i + j) / 2;
5989 h = sorted_sym_hash[idx];
5990 vdiff = vlook - h->root.u.def.value;
5991 if (vdiff < 0)
5992 j = idx;
5993 else if (vdiff > 0)
5994 i = idx + 1;
5995 else
5996 {
5997 int sdiff = slook->id - h->root.u.def.section->id;
5998 if (sdiff < 0)
5999 j = idx;
6000 else if (sdiff > 0)
6001 i = idx + 1;
6002 else
6003 break;
6004 }
6005 }
6006
6007 /* We didn't find a value/section match. */
6008 if (i == j)
6009 continue;
6010
6011 /* With multiple aliases, or when the weak symbol is already
6012 strongly defined, we have multiple matching symbols and
6013 the binary search above may land on any of them. Step
6014 one past the matching symbol(s). */
6015 while (++idx != j)
6016 {
6017 h = sorted_sym_hash[idx];
6018 if (h->root.u.def.section != slook
6019 || h->root.u.def.value != vlook)
6020 break;
6021 }
6022
6023 /* Now look back over the aliases. Since we sorted by size
6024 as well as value and section, we'll choose the one with
6025 the largest size. */
6026 while (idx-- != i)
6027 {
6028 h = sorted_sym_hash[idx];
6029
6030 /* Stop if value or section doesn't match. */
6031 if (h->root.u.def.section != slook
6032 || h->root.u.def.value != vlook)
6033 break;
6034 else if (h != hlook)
6035 {
6036 struct elf_link_hash_entry *t;
6037
6038 hlook->u.alias = h;
6039 hlook->is_weakalias = 1;
6040 t = h;
6041 if (t->u.alias != NULL)
6042 while (t->u.alias != h)
6043 t = t->u.alias;
6044 t->u.alias = hlook;
6045
6046 /* If the weak definition is in the list of dynamic
6047 symbols, make sure the real definition is put
6048 there as well. */
6049 if (hlook->dynindx != -1 && h->dynindx == -1)
6050 {
6051 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6052 {
6053 err_free_sym_hash:
6054 free (sorted_sym_hash);
6055 goto error_return;
6056 }
6057 }
6058
6059 /* If the real definition is in the list of dynamic
6060 symbols, make sure the weak definition is put
6061 there as well. If we don't do this, then the
6062 dynamic loader might not merge the entries for the
6063 real definition and the weak definition. */
6064 if (h->dynindx != -1 && hlook->dynindx == -1)
6065 {
6066 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6067 goto err_free_sym_hash;
6068 }
6069 break;
6070 }
6071 }
6072 }
6073
6074 free (sorted_sym_hash);
6075 }
6076
6077 if (bed->check_directives
6078 && !(*bed->check_directives) (abfd, info))
6079 goto error_return;
6080
6081 /* If this is a non-traditional link, try to optimize the handling
6082 of the .stab/.stabstr sections. */
6083 if (! dynamic
6084 && ! info->traditional_format
6085 && is_elf_hash_table (&htab->root)
6086 && (info->strip != strip_all && info->strip != strip_debugger))
6087 {
6088 asection *stabstr;
6089
6090 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6091 if (stabstr != NULL)
6092 {
6093 bfd_size_type string_offset = 0;
6094 asection *stab;
6095
6096 for (stab = abfd->sections; stab; stab = stab->next)
6097 if (startswith (stab->name, ".stab")
6098 && (!stab->name[5] ||
6099 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6100 && (stab->flags & SEC_MERGE) == 0
6101 && !bfd_is_abs_section (stab->output_section))
6102 {
6103 struct bfd_elf_section_data *secdata;
6104
6105 secdata = elf_section_data (stab);
6106 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6107 stabstr, &secdata->sec_info,
6108 &string_offset))
6109 goto error_return;
6110 if (secdata->sec_info)
6111 stab->sec_info_type = SEC_INFO_TYPE_STABS;
6112 }
6113 }
6114 }
6115
6116 if (dynamic && add_needed)
6117 {
6118 /* Add this bfd to the loaded list. */
6119 struct elf_link_loaded_list *n;
6120
6121 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6122 if (n == NULL)
6123 goto error_return;
6124 n->abfd = abfd;
6125 n->next = htab->dyn_loaded;
6126 htab->dyn_loaded = n;
6127 }
6128 if (dynamic && !add_needed
6129 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6130 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6131
6132 return true;
6133
6134 error_free_vers:
6135 free (old_tab);
6136 free (old_strtab);
6137 free (nondeflt_vers);
6138 free (extversym);
6139 error_free_sym:
6140 free (isymbuf);
6141 error_return:
6142 return false;
6143 }
6144
6145 /* Return the linker hash table entry of a symbol that might be
6146 satisfied by an archive symbol. Return -1 on error. */
6147
6148 struct bfd_link_hash_entry *
6149 _bfd_elf_archive_symbol_lookup (bfd *abfd,
6150 struct bfd_link_info *info,
6151 const char *name)
6152 {
6153 struct bfd_link_hash_entry *h;
6154 char *p, *copy;
6155 size_t len, first;
6156
6157 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6158 if (h != NULL)
6159 return h;
6160
6161 /* If this is a default version (the name contains @@), look up the
6162 symbol again with only one `@' as well as without the version.
6163 The effect is that references to the symbol with and without the
6164 version will be matched by the default symbol in the archive. */
6165
6166 p = strchr (name, ELF_VER_CHR);
6167 if (p == NULL || p[1] != ELF_VER_CHR)
6168 {
6169 /* Add this symbol to first hash if this archive has the first
6170 definition. */
6171 if (is_elf_hash_table (info->hash))
6172 elf_link_add_to_first_hash (abfd, info, name, false);
6173 return h;
6174 }
6175
6176 /* First check with only one `@'. */
6177 len = strlen (name);
6178 copy = (char *) bfd_alloc (abfd, len);
6179 if (copy == NULL)
6180 return (struct bfd_link_hash_entry *) -1;
6181
6182 first = p - name + 1;
6183 memcpy (copy, name, first);
6184 memcpy (copy + first, name + first + 1, len - first);
6185
6186 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6187 if (h == NULL)
6188 {
6189 /* We also need to check references to the symbol without the
6190 version. */
6191 copy[first - 1] = '\0';
6192 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6193 }
6194
6195 bfd_release (abfd, copy);
6196 return h;
6197 }
6198
6199 /* Add symbols from an ELF archive file to the linker hash table. We
6200 don't use _bfd_generic_link_add_archive_symbols because we need to
6201 handle versioned symbols.
6202
6203 Fortunately, ELF archive handling is simpler than that done by
6204 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6205 oddities. In ELF, if we find a symbol in the archive map, and the
6206 symbol is currently undefined, we know that we must pull in that
6207 object file.
6208
6209 Unfortunately, we do have to make multiple passes over the symbol
6210 table until nothing further is resolved. */
6211
6212 static bool
6213 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6214 {
6215 symindex c;
6216 unsigned char *included = NULL;
6217 carsym *symdefs;
6218 bool loop;
6219 size_t amt;
6220 const struct elf_backend_data *bed;
6221 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6222 (bfd *, struct bfd_link_info *, const char *);
6223
6224 if (! bfd_has_map (abfd))
6225 {
6226 /* An empty archive is a special case. */
6227 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6228 return true;
6229 bfd_set_error (bfd_error_no_armap);
6230 return false;
6231 }
6232
6233 /* Keep track of all symbols we know to be already defined, and all
6234 files we know to be already included. This is to speed up the
6235 second and subsequent passes. */
6236 c = bfd_ardata (abfd)->symdef_count;
6237 if (c == 0)
6238 return true;
6239 amt = c * sizeof (*included);
6240 included = (unsigned char *) bfd_zmalloc (amt);
6241 if (included == NULL)
6242 return false;
6243
6244 symdefs = bfd_ardata (abfd)->symdefs;
6245 bed = get_elf_backend_data (abfd);
6246 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6247
6248 do
6249 {
6250 file_ptr last;
6251 symindex i;
6252 carsym *symdef;
6253 carsym *symdefend;
6254
6255 loop = false;
6256 last = -1;
6257
6258 symdef = symdefs;
6259 symdefend = symdef + c;
6260 for (i = 0; symdef < symdefend; symdef++, i++)
6261 {
6262 struct bfd_link_hash_entry *h;
6263 bfd *element;
6264 struct bfd_link_hash_entry *undefs_tail;
6265 symindex mark;
6266
6267 if (included[i])
6268 continue;
6269 if (symdef->file_offset == last)
6270 {
6271 included[i] = true;
6272 continue;
6273 }
6274
6275 h = archive_symbol_lookup (abfd, info, symdef->name);
6276 if (h == (struct bfd_link_hash_entry *) -1)
6277 goto error_return;
6278
6279 if (h == NULL)
6280 continue;
6281
6282 if (h->type == bfd_link_hash_undefined)
6283 {
6284 if (is_elf_hash_table (info->hash))
6285 {
6286 /* If the archive element has already been loaded then one
6287 of the symbols defined by that element might have been
6288 made undefined due to being in a discarded section. */
6289 if (((struct elf_link_hash_entry *) h)->indx == -3)
6290 continue;
6291
6292 /* In the pre-LTO-plugin pass we must not mistakenly
6293 include this archive member if an earlier shared
6294 library defined this symbol. */
6295 struct elf_link_hash_table *htab = elf_hash_table (info);
6296 if (htab->first_hash)
6297 {
6298 struct elf_link_first_hash_entry *e
6299 = ((struct elf_link_first_hash_entry *)
6300 bfd_hash_lookup (htab->first_hash, symdef->name,
6301 false, false));
6302 if (e
6303 && (e->abfd->flags & DYNAMIC) != 0
6304 && e->abfd != abfd)
6305 continue;
6306 }
6307 }
6308 }
6309 else if (h->type == bfd_link_hash_common)
6310 {
6311 /* We currently have a common symbol. The archive map contains
6312 a reference to this symbol, so we may want to include it. We
6313 only want to include it however, if this archive element
6314 contains a definition of the symbol, not just another common
6315 declaration of it.
6316
6317 Unfortunately some archivers (including GNU ar) will put
6318 declarations of common symbols into their archive maps, as
6319 well as real definitions, so we cannot just go by the archive
6320 map alone. Instead we must read in the element's symbol
6321 table and check that to see what kind of symbol definition
6322 this is. */
6323 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6324 continue;
6325 }
6326 else
6327 {
6328 if (h->type != bfd_link_hash_undefweak)
6329 /* Symbol must be defined. Don't check it again. */
6330 included[i] = true;
6331
6332 if (!is_elf_hash_table (info->hash))
6333 continue;
6334 struct elf_link_hash_entry *eh
6335 = (struct elf_link_hash_entry *) h;
6336 /* Ignore the archive if the symbol isn't referenced by a
6337 regular object or isn't defined in a shared object. */
6338 if (!eh->ref_regular || !eh->def_dynamic)
6339 continue;
6340 /* Ignore the dynamic definition if symbol is first
6341 defined in this archive. */
6342 struct elf_link_hash_table *htab = elf_hash_table (info);
6343 if (htab->first_hash == NULL)
6344 continue;
6345 struct elf_link_first_hash_entry *e
6346 = ((struct elf_link_first_hash_entry *)
6347 bfd_hash_lookup (htab->first_hash, symdef->name,
6348 false, false));
6349 if (e == NULL || e->abfd != abfd)
6350 continue;
6351 }
6352
6353 /* We need to include this archive member. */
6354 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6355 info);
6356 if (element == NULL)
6357 goto error_return;
6358
6359 if (! bfd_check_format (element, bfd_object))
6360 goto error_return;
6361
6362 undefs_tail = info->hash->undefs_tail;
6363
6364 if (!(*info->callbacks
6365 ->add_archive_element) (info, element, symdef->name, &element))
6366 continue;
6367 if (!bfd_link_add_symbols (element, info))
6368 goto error_return;
6369
6370 /* If there are any new undefined symbols, we need to make
6371 another pass through the archive in order to see whether
6372 they can be defined. FIXME: This isn't perfect, because
6373 common symbols wind up on undefs_tail and because an
6374 undefined symbol which is defined later on in this pass
6375 does not require another pass. This isn't a bug, but it
6376 does make the code less efficient than it could be. */
6377 if (undefs_tail != info->hash->undefs_tail)
6378 loop = true;
6379
6380 /* Look backward to mark all symbols from this object file
6381 which we have already seen in this pass. */
6382 mark = i;
6383 do
6384 {
6385 included[mark] = true;
6386 if (mark == 0)
6387 break;
6388 --mark;
6389 }
6390 while (symdefs[mark].file_offset == symdef->file_offset);
6391
6392 /* We mark subsequent symbols from this object file as we go
6393 on through the loop. */
6394 last = symdef->file_offset;
6395 }
6396 }
6397 while (loop);
6398
6399 free (included);
6400 return true;
6401
6402 error_return:
6403 free (included);
6404 return false;
6405 }
6406
6407 /* Given an ELF BFD, add symbols to the global hash table as
6408 appropriate. */
6409
6410 bool
6411 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6412 {
6413 switch (bfd_get_format (abfd))
6414 {
6415 case bfd_object:
6416 return elf_link_add_object_symbols (abfd, info);
6417 case bfd_archive:
6418 return elf_link_add_archive_symbols (abfd, info);
6419 default:
6420 bfd_set_error (bfd_error_wrong_format);
6421 return false;
6422 }
6423 }
6424 \f
6425 struct hash_codes_info
6426 {
6427 unsigned long *hashcodes;
6428 bool error;
6429 };
6430
6431 /* This function will be called though elf_link_hash_traverse to store
6432 all hash value of the exported symbols in an array. */
6433
6434 static bool
6435 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6436 {
6437 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6438 const char *name;
6439 unsigned long ha;
6440 char *alc = NULL;
6441
6442 /* Ignore indirect symbols. These are added by the versioning code. */
6443 if (h->dynindx == -1)
6444 return true;
6445
6446 name = h->root.root.string;
6447 if (h->versioned >= versioned)
6448 {
6449 char *p = strchr (name, ELF_VER_CHR);
6450 if (p != NULL)
6451 {
6452 alc = (char *) bfd_malloc (p - name + 1);
6453 if (alc == NULL)
6454 {
6455 inf->error = true;
6456 return false;
6457 }
6458 memcpy (alc, name, p - name);
6459 alc[p - name] = '\0';
6460 name = alc;
6461 }
6462 }
6463
6464 /* Compute the hash value. */
6465 ha = bfd_elf_hash (name);
6466
6467 /* Store the found hash value in the array given as the argument. */
6468 *(inf->hashcodes)++ = ha;
6469
6470 /* And store it in the struct so that we can put it in the hash table
6471 later. */
6472 h->u.elf_hash_value = ha;
6473
6474 free (alc);
6475 return true;
6476 }
6477
6478 struct collect_gnu_hash_codes
6479 {
6480 bfd *output_bfd;
6481 const struct elf_backend_data *bed;
6482 unsigned long int nsyms;
6483 unsigned long int maskbits;
6484 unsigned long int *hashcodes;
6485 unsigned long int *hashval;
6486 unsigned long int *indx;
6487 unsigned long int *counts;
6488 bfd_vma *bitmask;
6489 bfd_byte *contents;
6490 bfd_size_type xlat;
6491 long int min_dynindx;
6492 unsigned long int bucketcount;
6493 unsigned long int symindx;
6494 long int local_indx;
6495 long int shift1, shift2;
6496 unsigned long int mask;
6497 bool error;
6498 };
6499
6500 /* This function will be called though elf_link_hash_traverse to store
6501 all hash value of the exported symbols in an array. */
6502
6503 static bool
6504 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6505 {
6506 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6507 const char *name;
6508 unsigned long ha;
6509 char *alc = NULL;
6510
6511 /* Ignore indirect symbols. These are added by the versioning code. */
6512 if (h->dynindx == -1)
6513 return true;
6514
6515 /* Ignore also local symbols and undefined symbols. */
6516 if (! (*s->bed->elf_hash_symbol) (h))
6517 return true;
6518
6519 name = h->root.root.string;
6520 if (h->versioned >= versioned)
6521 {
6522 char *p = strchr (name, ELF_VER_CHR);
6523 if (p != NULL)
6524 {
6525 alc = (char *) bfd_malloc (p - name + 1);
6526 if (alc == NULL)
6527 {
6528 s->error = true;
6529 return false;
6530 }
6531 memcpy (alc, name, p - name);
6532 alc[p - name] = '\0';
6533 name = alc;
6534 }
6535 }
6536
6537 /* Compute the hash value. */
6538 ha = bfd_elf_gnu_hash (name);
6539
6540 /* Store the found hash value in the array for compute_bucket_count,
6541 and also for .dynsym reordering purposes. */
6542 s->hashcodes[s->nsyms] = ha;
6543 s->hashval[h->dynindx] = ha;
6544 ++s->nsyms;
6545 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6546 s->min_dynindx = h->dynindx;
6547
6548 free (alc);
6549 return true;
6550 }
6551
6552 /* This function will be called though elf_link_hash_traverse to do
6553 final dynamic symbol renumbering in case of .gnu.hash.
6554 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6555 to the translation table. */
6556
6557 static bool
6558 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6559 {
6560 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6561 unsigned long int bucket;
6562 unsigned long int val;
6563
6564 /* Ignore indirect symbols. */
6565 if (h->dynindx == -1)
6566 return true;
6567
6568 /* Ignore also local symbols and undefined symbols. */
6569 if (! (*s->bed->elf_hash_symbol) (h))
6570 {
6571 if (h->dynindx >= s->min_dynindx)
6572 {
6573 if (s->bed->record_xhash_symbol != NULL)
6574 {
6575 (*s->bed->record_xhash_symbol) (h, 0);
6576 s->local_indx++;
6577 }
6578 else
6579 h->dynindx = s->local_indx++;
6580 }
6581 return true;
6582 }
6583
6584 bucket = s->hashval[h->dynindx] % s->bucketcount;
6585 val = (s->hashval[h->dynindx] >> s->shift1)
6586 & ((s->maskbits >> s->shift1) - 1);
6587 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6588 s->bitmask[val]
6589 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6590 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6591 if (s->counts[bucket] == 1)
6592 /* Last element terminates the chain. */
6593 val |= 1;
6594 bfd_put_32 (s->output_bfd, val,
6595 s->contents + (s->indx[bucket] - s->symindx) * 4);
6596 --s->counts[bucket];
6597 if (s->bed->record_xhash_symbol != NULL)
6598 {
6599 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6600
6601 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6602 }
6603 else
6604 h->dynindx = s->indx[bucket]++;
6605 return true;
6606 }
6607
6608 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6609
6610 bool
6611 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6612 {
6613 return !(h->forced_local
6614 || h->root.type == bfd_link_hash_undefined
6615 || h->root.type == bfd_link_hash_undefweak
6616 || ((h->root.type == bfd_link_hash_defined
6617 || h->root.type == bfd_link_hash_defweak)
6618 && h->root.u.def.section->output_section == NULL));
6619 }
6620
6621 /* Array used to determine the number of hash table buckets to use
6622 based on the number of symbols there are. If there are fewer than
6623 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6624 fewer than 37 we use 17 buckets, and so forth. We never use more
6625 than 32771 buckets. */
6626
6627 static const size_t elf_buckets[] =
6628 {
6629 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6630 16411, 32771, 0
6631 };
6632
6633 /* Compute bucket count for hashing table. We do not use a static set
6634 of possible tables sizes anymore. Instead we determine for all
6635 possible reasonable sizes of the table the outcome (i.e., the
6636 number of collisions etc) and choose the best solution. The
6637 weighting functions are not too simple to allow the table to grow
6638 without bounds. Instead one of the weighting factors is the size.
6639 Therefore the result is always a good payoff between few collisions
6640 (= short chain lengths) and table size. */
6641 static size_t
6642 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6643 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6644 unsigned long int nsyms,
6645 int gnu_hash)
6646 {
6647 size_t best_size = 0;
6648 unsigned long int i;
6649
6650 if (info->optimize)
6651 {
6652 size_t minsize;
6653 size_t maxsize;
6654 uint64_t best_chlen = ~((uint64_t) 0);
6655 bfd *dynobj = elf_hash_table (info)->dynobj;
6656 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6657 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6658 unsigned long int *counts;
6659 bfd_size_type amt;
6660 unsigned int no_improvement_count = 0;
6661
6662 /* Possible optimization parameters: if we have NSYMS symbols we say
6663 that the hashing table must at least have NSYMS/4 and at most
6664 2*NSYMS buckets. */
6665 minsize = nsyms / 4;
6666 if (minsize == 0)
6667 minsize = 1;
6668 best_size = maxsize = nsyms * 2;
6669 if (gnu_hash)
6670 {
6671 if (minsize < 2)
6672 minsize = 2;
6673 if ((best_size & 31) == 0)
6674 ++best_size;
6675 }
6676
6677 /* Create array where we count the collisions in. We must use bfd_malloc
6678 since the size could be large. */
6679 amt = maxsize;
6680 amt *= sizeof (unsigned long int);
6681 counts = (unsigned long int *) bfd_malloc (amt);
6682 if (counts == NULL)
6683 return 0;
6684
6685 /* Compute the "optimal" size for the hash table. The criteria is a
6686 minimal chain length. The minor criteria is (of course) the size
6687 of the table. */
6688 for (i = minsize; i < maxsize; ++i)
6689 {
6690 /* Walk through the array of hashcodes and count the collisions. */
6691 uint64_t max;
6692 unsigned long int j;
6693 unsigned long int fact;
6694
6695 if (gnu_hash && (i & 31) == 0)
6696 continue;
6697
6698 memset (counts, '\0', i * sizeof (unsigned long int));
6699
6700 /* Determine how often each hash bucket is used. */
6701 for (j = 0; j < nsyms; ++j)
6702 ++counts[hashcodes[j] % i];
6703
6704 /* For the weight function we need some information about the
6705 pagesize on the target. This is information need not be 100%
6706 accurate. Since this information is not available (so far) we
6707 define it here to a reasonable default value. If it is crucial
6708 to have a better value some day simply define this value. */
6709 # ifndef BFD_TARGET_PAGESIZE
6710 # define BFD_TARGET_PAGESIZE (4096)
6711 # endif
6712
6713 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6714 and the chains. */
6715 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6716
6717 # if 1
6718 /* Variant 1: optimize for short chains. We add the squares
6719 of all the chain lengths (which favors many small chain
6720 over a few long chains). */
6721 for (j = 0; j < i; ++j)
6722 max += counts[j] * counts[j];
6723
6724 /* This adds penalties for the overall size of the table. */
6725 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6726 max *= fact * fact;
6727 # else
6728 /* Variant 2: Optimize a lot more for small table. Here we
6729 also add squares of the size but we also add penalties for
6730 empty slots (the +1 term). */
6731 for (j = 0; j < i; ++j)
6732 max += (1 + counts[j]) * (1 + counts[j]);
6733
6734 /* The overall size of the table is considered, but not as
6735 strong as in variant 1, where it is squared. */
6736 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6737 max *= fact;
6738 # endif
6739
6740 /* Compare with current best results. */
6741 if (max < best_chlen)
6742 {
6743 best_chlen = max;
6744 best_size = i;
6745 no_improvement_count = 0;
6746 }
6747 /* PR 11843: Avoid futile long searches for the best bucket size
6748 when there are a large number of symbols. */
6749 else if (++no_improvement_count == 100)
6750 break;
6751 }
6752
6753 free (counts);
6754 }
6755 else
6756 {
6757 for (i = 0; elf_buckets[i] != 0; i++)
6758 {
6759 best_size = elf_buckets[i];
6760 if (nsyms < elf_buckets[i + 1])
6761 break;
6762 }
6763 if (gnu_hash && best_size < 2)
6764 best_size = 2;
6765 }
6766
6767 return best_size;
6768 }
6769
6770 /* Size any SHT_GROUP section for ld -r. */
6771
6772 bool
6773 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6774 {
6775 bfd *ibfd;
6776 asection *s;
6777
6778 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6779 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6780 && (s = ibfd->sections) != NULL
6781 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6782 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6783 return false;
6784 return true;
6785 }
6786
6787 /* Set a default stack segment size. The value in INFO wins. If it
6788 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6789 undefined it is initialized. */
6790
6791 bool
6792 bfd_elf_stack_segment_size (bfd *output_bfd,
6793 struct bfd_link_info *info,
6794 const char *legacy_symbol,
6795 bfd_vma default_size)
6796 {
6797 struct elf_link_hash_entry *h = NULL;
6798
6799 /* Look for legacy symbol. */
6800 if (legacy_symbol)
6801 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6802 false, false, false);
6803 if (h && (h->root.type == bfd_link_hash_defined
6804 || h->root.type == bfd_link_hash_defweak)
6805 && h->def_regular
6806 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6807 {
6808 /* The symbol has no type if specified on the command line. */
6809 h->type = STT_OBJECT;
6810 if (info->stacksize)
6811 /* xgettext:c-format */
6812 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6813 output_bfd, legacy_symbol);
6814 else if (h->root.u.def.section != bfd_abs_section_ptr)
6815 /* xgettext:c-format */
6816 _bfd_error_handler (_("%pB: %s not absolute"),
6817 output_bfd, legacy_symbol);
6818 else
6819 info->stacksize = h->root.u.def.value;
6820 }
6821
6822 if (!info->stacksize)
6823 /* If the user didn't set a size, or explicitly inhibit the
6824 size, set it now. */
6825 info->stacksize = default_size;
6826
6827 /* Provide the legacy symbol, if it is referenced. */
6828 if (h && (h->root.type == bfd_link_hash_undefined
6829 || h->root.type == bfd_link_hash_undefweak))
6830 {
6831 struct bfd_link_hash_entry *bh = NULL;
6832
6833 if (!(_bfd_generic_link_add_one_symbol
6834 (info, output_bfd, legacy_symbol,
6835 BSF_GLOBAL, bfd_abs_section_ptr,
6836 info->stacksize >= 0 ? info->stacksize : 0,
6837 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6838 return false;
6839
6840 h = (struct elf_link_hash_entry *) bh;
6841 h->def_regular = 1;
6842 h->type = STT_OBJECT;
6843 }
6844
6845 return true;
6846 }
6847
6848 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6849
6850 struct elf_gc_sweep_symbol_info
6851 {
6852 struct bfd_link_info *info;
6853 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6854 bool);
6855 };
6856
6857 static bool
6858 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6859 {
6860 if (!h->mark
6861 && (((h->root.type == bfd_link_hash_defined
6862 || h->root.type == bfd_link_hash_defweak)
6863 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6864 && h->root.u.def.section->gc_mark))
6865 || h->root.type == bfd_link_hash_undefined
6866 || h->root.type == bfd_link_hash_undefweak))
6867 {
6868 struct elf_gc_sweep_symbol_info *inf;
6869
6870 inf = (struct elf_gc_sweep_symbol_info *) data;
6871 (*inf->hide_symbol) (inf->info, h, true);
6872 h->def_regular = 0;
6873 h->ref_regular = 0;
6874 h->ref_regular_nonweak = 0;
6875 }
6876
6877 return true;
6878 }
6879
6880 /* Set up the sizes and contents of the ELF dynamic sections. This is
6881 called by the ELF linker emulation before_allocation routine. We
6882 must set the sizes of the sections before the linker sets the
6883 addresses of the various sections. */
6884
6885 bool
6886 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6887 const char *soname,
6888 const char *rpath,
6889 const char *filter_shlib,
6890 const char *audit,
6891 const char *depaudit,
6892 const char * const *auxiliary_filters,
6893 struct bfd_link_info *info,
6894 asection **sinterpptr)
6895 {
6896 bfd *dynobj;
6897 const struct elf_backend_data *bed;
6898
6899 *sinterpptr = NULL;
6900
6901 if (!is_elf_hash_table (info->hash))
6902 return true;
6903
6904 /* Any syms created from now on start with -1 in
6905 got.refcount/offset and plt.refcount/offset. */
6906 elf_hash_table (info)->init_got_refcount
6907 = elf_hash_table (info)->init_got_offset;
6908 elf_hash_table (info)->init_plt_refcount
6909 = elf_hash_table (info)->init_plt_offset;
6910
6911 bed = get_elf_backend_data (output_bfd);
6912
6913 /* The backend may have to create some sections regardless of whether
6914 we're dynamic or not. */
6915 if (bed->elf_backend_early_size_sections
6916 && !bed->elf_backend_early_size_sections (output_bfd, info))
6917 return false;
6918
6919 dynobj = elf_hash_table (info)->dynobj;
6920
6921 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6922 {
6923 struct bfd_elf_version_tree *verdefs;
6924 struct elf_info_failed asvinfo;
6925 struct bfd_elf_version_tree *t;
6926 struct bfd_elf_version_expr *d;
6927 asection *s;
6928 size_t soname_indx;
6929
6930 /* If we are supposed to export all symbols into the dynamic symbol
6931 table (this is not the normal case), then do so. */
6932 if (info->export_dynamic
6933 || (bfd_link_executable (info) && info->dynamic))
6934 {
6935 struct elf_info_failed eif;
6936
6937 eif.info = info;
6938 eif.failed = false;
6939 elf_link_hash_traverse (elf_hash_table (info),
6940 _bfd_elf_export_symbol,
6941 &eif);
6942 if (eif.failed)
6943 return false;
6944 }
6945
6946 if (soname != NULL)
6947 {
6948 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6949 soname, true);
6950 if (soname_indx == (size_t) -1
6951 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6952 return false;
6953 }
6954 else
6955 soname_indx = (size_t) -1;
6956
6957 /* Make all global versions with definition. */
6958 for (t = info->version_info; t != NULL; t = t->next)
6959 for (d = t->globals.list; d != NULL; d = d->next)
6960 if (!d->symver && d->literal)
6961 {
6962 const char *verstr, *name;
6963 size_t namelen, verlen, newlen;
6964 char *newname, *p, leading_char;
6965 struct elf_link_hash_entry *newh;
6966
6967 leading_char = bfd_get_symbol_leading_char (output_bfd);
6968 name = d->pattern;
6969 namelen = strlen (name) + (leading_char != '\0');
6970 verstr = t->name;
6971 verlen = strlen (verstr);
6972 newlen = namelen + verlen + 3;
6973
6974 newname = (char *) bfd_malloc (newlen);
6975 if (newname == NULL)
6976 return false;
6977 newname[0] = leading_char;
6978 memcpy (newname + (leading_char != '\0'), name, namelen);
6979
6980 /* Check the hidden versioned definition. */
6981 p = newname + namelen;
6982 *p++ = ELF_VER_CHR;
6983 memcpy (p, verstr, verlen + 1);
6984 newh = elf_link_hash_lookup (elf_hash_table (info),
6985 newname, false, false,
6986 false);
6987 if (newh == NULL
6988 || (newh->root.type != bfd_link_hash_defined
6989 && newh->root.type != bfd_link_hash_defweak))
6990 {
6991 /* Check the default versioned definition. */
6992 *p++ = ELF_VER_CHR;
6993 memcpy (p, verstr, verlen + 1);
6994 newh = elf_link_hash_lookup (elf_hash_table (info),
6995 newname, false, false,
6996 false);
6997 }
6998 free (newname);
6999
7000 /* Mark this version if there is a definition and it is
7001 not defined in a shared object. */
7002 if (newh != NULL
7003 && !newh->def_dynamic
7004 && (newh->root.type == bfd_link_hash_defined
7005 || newh->root.type == bfd_link_hash_defweak))
7006 d->symver = 1;
7007 }
7008
7009 /* Attach all the symbols to their version information. */
7010 asvinfo.info = info;
7011 asvinfo.failed = false;
7012
7013 elf_link_hash_traverse (elf_hash_table (info),
7014 _bfd_elf_link_assign_sym_version,
7015 &asvinfo);
7016 if (asvinfo.failed)
7017 return false;
7018
7019 if (!info->allow_undefined_version)
7020 {
7021 /* Check if all global versions have a definition. */
7022 bool all_defined = true;
7023 for (t = info->version_info; t != NULL; t = t->next)
7024 for (d = t->globals.list; d != NULL; d = d->next)
7025 if (d->literal && !d->symver && !d->script)
7026 {
7027 _bfd_error_handler
7028 (_("%s: undefined version: %s"),
7029 d->pattern, t->name);
7030 all_defined = false;
7031 }
7032
7033 if (!all_defined)
7034 {
7035 bfd_set_error (bfd_error_bad_value);
7036 return false;
7037 }
7038 }
7039
7040 /* Set up the version definition section. */
7041 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7042 BFD_ASSERT (s != NULL);
7043
7044 /* We may have created additional version definitions if we are
7045 just linking a regular application. */
7046 verdefs = info->version_info;
7047
7048 /* Skip anonymous version tag. */
7049 if (verdefs != NULL && verdefs->vernum == 0)
7050 verdefs = verdefs->next;
7051
7052 if (verdefs == NULL && !info->create_default_symver)
7053 s->flags |= SEC_EXCLUDE;
7054 else
7055 {
7056 unsigned int cdefs;
7057 bfd_size_type size;
7058 bfd_byte *p;
7059 Elf_Internal_Verdef def;
7060 Elf_Internal_Verdaux defaux;
7061 struct bfd_link_hash_entry *bh;
7062 struct elf_link_hash_entry *h;
7063 const char *name;
7064
7065 cdefs = 0;
7066 size = 0;
7067
7068 /* Make space for the base version. */
7069 size += sizeof (Elf_External_Verdef);
7070 size += sizeof (Elf_External_Verdaux);
7071 ++cdefs;
7072
7073 /* Make space for the default version. */
7074 if (info->create_default_symver)
7075 {
7076 size += sizeof (Elf_External_Verdef);
7077 ++cdefs;
7078 }
7079
7080 for (t = verdefs; t != NULL; t = t->next)
7081 {
7082 struct bfd_elf_version_deps *n;
7083
7084 /* Don't emit base version twice. */
7085 if (t->vernum == 0)
7086 continue;
7087
7088 size += sizeof (Elf_External_Verdef);
7089 size += sizeof (Elf_External_Verdaux);
7090 ++cdefs;
7091
7092 for (n = t->deps; n != NULL; n = n->next)
7093 size += sizeof (Elf_External_Verdaux);
7094 }
7095
7096 s->size = size;
7097 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7098 if (s->contents == NULL && s->size != 0)
7099 return false;
7100 s->alloced = 1;
7101
7102 /* Fill in the version definition section. */
7103
7104 p = s->contents;
7105
7106 def.vd_version = VER_DEF_CURRENT;
7107 def.vd_flags = VER_FLG_BASE;
7108 def.vd_ndx = 1;
7109 def.vd_cnt = 1;
7110 if (info->create_default_symver)
7111 {
7112 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7113 def.vd_next = sizeof (Elf_External_Verdef);
7114 }
7115 else
7116 {
7117 def.vd_aux = sizeof (Elf_External_Verdef);
7118 def.vd_next = (sizeof (Elf_External_Verdef)
7119 + sizeof (Elf_External_Verdaux));
7120 }
7121
7122 if (soname_indx != (size_t) -1)
7123 {
7124 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7125 soname_indx);
7126 def.vd_hash = bfd_elf_hash (soname);
7127 defaux.vda_name = soname_indx;
7128 name = soname;
7129 }
7130 else
7131 {
7132 size_t indx;
7133
7134 name = lbasename (bfd_get_filename (output_bfd));
7135 def.vd_hash = bfd_elf_hash (name);
7136 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7137 name, false);
7138 if (indx == (size_t) -1)
7139 return false;
7140 defaux.vda_name = indx;
7141 }
7142 defaux.vda_next = 0;
7143
7144 _bfd_elf_swap_verdef_out (output_bfd, &def,
7145 (Elf_External_Verdef *) p);
7146 p += sizeof (Elf_External_Verdef);
7147 if (info->create_default_symver)
7148 {
7149 /* Add a symbol representing this version. */
7150 bh = NULL;
7151 if (! (_bfd_generic_link_add_one_symbol
7152 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7153 0, NULL, false,
7154 get_elf_backend_data (dynobj)->collect, &bh)))
7155 return false;
7156 h = (struct elf_link_hash_entry *) bh;
7157 h->non_elf = 0;
7158 h->def_regular = 1;
7159 h->type = STT_OBJECT;
7160 h->verinfo.vertree = NULL;
7161
7162 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7163 return false;
7164
7165 /* Create a duplicate of the base version with the same
7166 aux block, but different flags. */
7167 def.vd_flags = 0;
7168 def.vd_ndx = 2;
7169 def.vd_aux = sizeof (Elf_External_Verdef);
7170 if (verdefs)
7171 def.vd_next = (sizeof (Elf_External_Verdef)
7172 + sizeof (Elf_External_Verdaux));
7173 else
7174 def.vd_next = 0;
7175 _bfd_elf_swap_verdef_out (output_bfd, &def,
7176 (Elf_External_Verdef *) p);
7177 p += sizeof (Elf_External_Verdef);
7178 }
7179 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7180 (Elf_External_Verdaux *) p);
7181 p += sizeof (Elf_External_Verdaux);
7182
7183 for (t = verdefs; t != NULL; t = t->next)
7184 {
7185 unsigned int cdeps;
7186 struct bfd_elf_version_deps *n;
7187
7188 /* Don't emit the base version twice. */
7189 if (t->vernum == 0)
7190 continue;
7191
7192 cdeps = 0;
7193 for (n = t->deps; n != NULL; n = n->next)
7194 ++cdeps;
7195
7196 /* Add a symbol representing this version. */
7197 bh = NULL;
7198 if (! (_bfd_generic_link_add_one_symbol
7199 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7200 0, NULL, false,
7201 get_elf_backend_data (dynobj)->collect, &bh)))
7202 return false;
7203 h = (struct elf_link_hash_entry *) bh;
7204 h->non_elf = 0;
7205 h->def_regular = 1;
7206 h->type = STT_OBJECT;
7207 h->verinfo.vertree = t;
7208
7209 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7210 return false;
7211
7212 def.vd_version = VER_DEF_CURRENT;
7213 def.vd_flags = 0;
7214 if (t->globals.list == NULL
7215 && t->locals.list == NULL
7216 && ! t->used)
7217 def.vd_flags |= VER_FLG_WEAK;
7218 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7219 def.vd_cnt = cdeps + 1;
7220 def.vd_hash = bfd_elf_hash (t->name);
7221 def.vd_aux = sizeof (Elf_External_Verdef);
7222 def.vd_next = 0;
7223
7224 /* If a basever node is next, it *must* be the last node in
7225 the chain, otherwise Verdef construction breaks. */
7226 if (t->next != NULL && t->next->vernum == 0)
7227 BFD_ASSERT (t->next->next == NULL);
7228
7229 if (t->next != NULL && t->next->vernum != 0)
7230 def.vd_next = (sizeof (Elf_External_Verdef)
7231 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7232
7233 _bfd_elf_swap_verdef_out (output_bfd, &def,
7234 (Elf_External_Verdef *) p);
7235 p += sizeof (Elf_External_Verdef);
7236
7237 defaux.vda_name = h->dynstr_index;
7238 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7239 h->dynstr_index);
7240 defaux.vda_next = 0;
7241 if (t->deps != NULL)
7242 defaux.vda_next = sizeof (Elf_External_Verdaux);
7243 t->name_indx = defaux.vda_name;
7244
7245 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7246 (Elf_External_Verdaux *) p);
7247 p += sizeof (Elf_External_Verdaux);
7248
7249 for (n = t->deps; n != NULL; n = n->next)
7250 {
7251 if (n->version_needed == NULL)
7252 {
7253 /* This can happen if there was an error in the
7254 version script. */
7255 defaux.vda_name = 0;
7256 }
7257 else
7258 {
7259 defaux.vda_name = n->version_needed->name_indx;
7260 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7261 defaux.vda_name);
7262 }
7263 if (n->next == NULL)
7264 defaux.vda_next = 0;
7265 else
7266 defaux.vda_next = sizeof (Elf_External_Verdaux);
7267
7268 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7269 (Elf_External_Verdaux *) p);
7270 p += sizeof (Elf_External_Verdaux);
7271 }
7272 }
7273
7274 elf_tdata (output_bfd)->cverdefs = cdefs;
7275 }
7276 }
7277
7278 if (info->gc_sections && bed->can_gc_sections)
7279 {
7280 struct elf_gc_sweep_symbol_info sweep_info;
7281
7282 /* Remove the symbols that were in the swept sections from the
7283 dynamic symbol table. */
7284 sweep_info.info = info;
7285 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7286 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7287 &sweep_info);
7288 }
7289
7290 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7291 {
7292 asection *s;
7293 struct elf_find_verdep_info sinfo;
7294
7295 /* Work out the size of the version reference section. */
7296
7297 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7298 BFD_ASSERT (s != NULL);
7299
7300 sinfo.info = info;
7301 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7302 if (sinfo.vers == 0)
7303 sinfo.vers = 1;
7304 sinfo.failed = false;
7305
7306 elf_link_hash_traverse (elf_hash_table (info),
7307 _bfd_elf_link_find_version_dependencies,
7308 &sinfo);
7309 if (sinfo.failed)
7310 return false;
7311
7312 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7313 if (sinfo.failed)
7314 return false;
7315
7316 if (elf_tdata (output_bfd)->verref == NULL)
7317 s->flags |= SEC_EXCLUDE;
7318 else
7319 {
7320 Elf_Internal_Verneed *vn;
7321 unsigned int size;
7322 unsigned int crefs;
7323 bfd_byte *p;
7324
7325 /* Build the version dependency section. */
7326 size = 0;
7327 crefs = 0;
7328 for (vn = elf_tdata (output_bfd)->verref;
7329 vn != NULL;
7330 vn = vn->vn_nextref)
7331 {
7332 Elf_Internal_Vernaux *a;
7333
7334 size += sizeof (Elf_External_Verneed);
7335 ++crefs;
7336 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7337 size += sizeof (Elf_External_Vernaux);
7338 }
7339
7340 s->size = size;
7341 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7342 if (s->contents == NULL)
7343 return false;
7344 s->alloced = 1;
7345
7346 p = s->contents;
7347 for (vn = elf_tdata (output_bfd)->verref;
7348 vn != NULL;
7349 vn = vn->vn_nextref)
7350 {
7351 unsigned int caux;
7352 Elf_Internal_Vernaux *a;
7353 size_t indx;
7354
7355 caux = 0;
7356 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7357 ++caux;
7358
7359 vn->vn_version = VER_NEED_CURRENT;
7360 vn->vn_cnt = caux;
7361 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7362 elf_dt_name (vn->vn_bfd) != NULL
7363 ? elf_dt_name (vn->vn_bfd)
7364 : lbasename (bfd_get_filename
7365 (vn->vn_bfd)),
7366 false);
7367 if (indx == (size_t) -1)
7368 return false;
7369 vn->vn_file = indx;
7370 vn->vn_aux = sizeof (Elf_External_Verneed);
7371 if (vn->vn_nextref == NULL)
7372 vn->vn_next = 0;
7373 else
7374 vn->vn_next = (sizeof (Elf_External_Verneed)
7375 + caux * sizeof (Elf_External_Vernaux));
7376
7377 _bfd_elf_swap_verneed_out (output_bfd, vn,
7378 (Elf_External_Verneed *) p);
7379 p += sizeof (Elf_External_Verneed);
7380
7381 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7382 {
7383 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7384 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7385 a->vna_nodename, false);
7386 if (indx == (size_t) -1)
7387 return false;
7388 a->vna_name = indx;
7389 if (a->vna_nextptr == NULL)
7390 a->vna_next = 0;
7391 else
7392 a->vna_next = sizeof (Elf_External_Vernaux);
7393
7394 _bfd_elf_swap_vernaux_out (output_bfd, a,
7395 (Elf_External_Vernaux *) p);
7396 p += sizeof (Elf_External_Vernaux);
7397 }
7398 }
7399
7400 elf_tdata (output_bfd)->cverrefs = crefs;
7401 }
7402 }
7403
7404 if (bfd_link_relocatable (info)
7405 && !_bfd_elf_size_group_sections (info))
7406 return false;
7407
7408 /* Determine any GNU_STACK segment requirements, after the backend
7409 has had a chance to set a default segment size. */
7410 if (info->execstack)
7411 {
7412 /* If the user has explicitly requested warnings, then generate one even
7413 though the choice is the result of another command line option. */
7414 if (info->warn_execstack == 1)
7415 {
7416 if (info->error_execstack)
7417 {
7418 _bfd_error_handler
7419 (_("\
7420 error: creating an executable stack because of -z execstack command line option"));
7421 return false;
7422 }
7423
7424 _bfd_error_handler
7425 (_("\
7426 warning: enabling an executable stack because of -z execstack command line option"));
7427 }
7428
7429 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7430 }
7431 else if (info->noexecstack)
7432 elf_stack_flags (output_bfd) = PF_R | PF_W;
7433 else
7434 {
7435 bfd *inputobj;
7436 asection *notesec = NULL;
7437 bfd *noteobj = NULL;
7438 bfd *emptyobj = NULL;
7439 int exec = 0;
7440
7441 for (inputobj = info->input_bfds;
7442 inputobj;
7443 inputobj = inputobj->link.next)
7444 {
7445 asection *s;
7446
7447 if (inputobj->flags
7448 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7449 continue;
7450 s = inputobj->sections;
7451 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7452 continue;
7453
7454 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7455 if (s)
7456 {
7457 notesec = s;
7458 if (s->flags & SEC_CODE)
7459 {
7460 noteobj = inputobj;
7461 exec = PF_X;
7462 /* There is no point in scanning the remaining bfds. */
7463 break;
7464 }
7465 }
7466 else if (bed->default_execstack && info->default_execstack)
7467 {
7468 exec = PF_X;
7469 emptyobj = inputobj;
7470 }
7471 }
7472
7473 if (notesec || info->stacksize > 0)
7474 {
7475 if (exec)
7476 {
7477 if (info->warn_execstack != 0)
7478 {
7479 /* PR 29072: Because an executable stack is a serious
7480 security risk, make sure that the user knows that it is
7481 being enabled despite the fact that it was not requested
7482 on the command line. */
7483 if (noteobj)
7484 {
7485 if (info->error_execstack)
7486 {
7487 _bfd_error_handler (_("\
7488 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7489 bfd_get_filename (noteobj));
7490 return false;
7491 }
7492
7493 _bfd_error_handler (_("\
7494 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7495 bfd_get_filename (noteobj));
7496 }
7497 else if (emptyobj)
7498 {
7499 if (info->error_execstack)
7500 {
7501 _bfd_error_handler (_("\
7502 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7503 bfd_get_filename (emptyobj));
7504 return false;
7505 }
7506
7507 _bfd_error_handler (_("\
7508 warning: %s: missing .note.GNU-stack section implies executable stack"),
7509 bfd_get_filename (emptyobj));
7510 _bfd_error_handler (_("\
7511 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7512 }
7513 }
7514 }
7515 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7516 }
7517
7518 if (notesec && exec && bfd_link_relocatable (info)
7519 && notesec->output_section != bfd_abs_section_ptr)
7520 notesec->output_section->flags |= SEC_CODE;
7521 }
7522
7523 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7524 {
7525 struct elf_info_failed eif;
7526 struct elf_link_hash_entry *h;
7527 asection *dynstr;
7528 asection *s;
7529
7530 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7531 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7532
7533 if (info->symbolic)
7534 {
7535 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7536 return false;
7537 info->flags |= DF_SYMBOLIC;
7538 }
7539
7540 if (rpath != NULL)
7541 {
7542 size_t indx;
7543 bfd_vma tag;
7544
7545 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7546 true);
7547 if (indx == (size_t) -1)
7548 return false;
7549
7550 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7551 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7552 return false;
7553 }
7554
7555 if (filter_shlib != NULL)
7556 {
7557 size_t indx;
7558
7559 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7560 filter_shlib, true);
7561 if (indx == (size_t) -1
7562 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7563 return false;
7564 }
7565
7566 if (auxiliary_filters != NULL)
7567 {
7568 const char * const *p;
7569
7570 for (p = auxiliary_filters; *p != NULL; p++)
7571 {
7572 size_t indx;
7573
7574 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7575 *p, true);
7576 if (indx == (size_t) -1
7577 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7578 return false;
7579 }
7580 }
7581
7582 if (audit != NULL)
7583 {
7584 size_t indx;
7585
7586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7587 true);
7588 if (indx == (size_t) -1
7589 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7590 return false;
7591 }
7592
7593 if (depaudit != NULL)
7594 {
7595 size_t indx;
7596
7597 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7598 true);
7599 if (indx == (size_t) -1
7600 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7601 return false;
7602 }
7603
7604 eif.info = info;
7605 eif.failed = false;
7606
7607 /* Find all symbols which were defined in a dynamic object and make
7608 the backend pick a reasonable value for them. */
7609 elf_link_hash_traverse (elf_hash_table (info),
7610 _bfd_elf_adjust_dynamic_symbol,
7611 &eif);
7612 if (eif.failed)
7613 return false;
7614
7615 /* Add some entries to the .dynamic section. We fill in some of the
7616 values later, in bfd_elf_final_link, but we must add the entries
7617 now so that we know the final size of the .dynamic section. */
7618
7619 /* If there are initialization and/or finalization functions to
7620 call then add the corresponding DT_INIT/DT_FINI entries. */
7621 h = (info->init_function
7622 ? elf_link_hash_lookup (elf_hash_table (info),
7623 info->init_function, false,
7624 false, false)
7625 : NULL);
7626 if (h != NULL
7627 && (h->ref_regular
7628 || h->def_regular))
7629 {
7630 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7631 return false;
7632 }
7633 h = (info->fini_function
7634 ? elf_link_hash_lookup (elf_hash_table (info),
7635 info->fini_function, false,
7636 false, false)
7637 : NULL);
7638 if (h != NULL
7639 && (h->ref_regular
7640 || h->def_regular))
7641 {
7642 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7643 return false;
7644 }
7645
7646 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7647 if (s != NULL && s->linker_has_input)
7648 {
7649 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7650 if (! bfd_link_executable (info))
7651 {
7652 bfd *sub;
7653 asection *o;
7654
7655 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7656 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7657 && (o = sub->sections) != NULL
7658 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7659 for (o = sub->sections; o != NULL; o = o->next)
7660 if (elf_section_data (o)->this_hdr.sh_type
7661 == SHT_PREINIT_ARRAY)
7662 {
7663 _bfd_error_handler
7664 (_("%pB: .preinit_array section is not allowed in DSO"),
7665 sub);
7666 break;
7667 }
7668
7669 bfd_set_error (bfd_error_nonrepresentable_section);
7670 return false;
7671 }
7672
7673 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7674 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7675 return false;
7676 }
7677 s = bfd_get_section_by_name (output_bfd, ".init_array");
7678 if (s != NULL && s->linker_has_input)
7679 {
7680 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7681 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7682 return false;
7683 }
7684 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7685 if (s != NULL && s->linker_has_input)
7686 {
7687 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7688 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7689 return false;
7690 }
7691
7692 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7693 /* If .dynstr is excluded from the link, we don't want any of
7694 these tags. Strictly, we should be checking each section
7695 individually; This quick check covers for the case where
7696 someone does a /DISCARD/ : { *(*) }. */
7697 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7698 {
7699 bfd_size_type strsize;
7700
7701 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7702 if ((info->emit_hash
7703 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7704 || (info->emit_gnu_hash
7705 && (bed->record_xhash_symbol == NULL
7706 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7707 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7708 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7709 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7710 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7711 bed->s->sizeof_sym)
7712 || (info->gnu_flags_1
7713 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7714 info->gnu_flags_1)))
7715 return false;
7716 }
7717 }
7718
7719 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7720 return false;
7721
7722 /* The backend must work out the sizes of all the other dynamic
7723 sections. */
7724 if (bed->elf_backend_late_size_sections != NULL
7725 && !bed->elf_backend_late_size_sections (output_bfd, info))
7726 return false;
7727
7728 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7729 {
7730 if (elf_tdata (output_bfd)->cverdefs)
7731 {
7732 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7733
7734 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7735 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7736 return false;
7737 }
7738
7739 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7740 {
7741 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7742 return false;
7743 }
7744 else if (info->flags & DF_BIND_NOW)
7745 {
7746 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7747 return false;
7748 }
7749
7750 if (info->flags_1)
7751 {
7752 if (bfd_link_executable (info))
7753 info->flags_1 &= ~ (DF_1_INITFIRST
7754 | DF_1_NODELETE
7755 | DF_1_NOOPEN);
7756 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7757 return false;
7758 }
7759
7760 if (elf_tdata (output_bfd)->cverrefs)
7761 {
7762 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7763
7764 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7765 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7766 return false;
7767 }
7768
7769 if ((elf_tdata (output_bfd)->cverrefs == 0
7770 && elf_tdata (output_bfd)->cverdefs == 0)
7771 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7772 {
7773 asection *s;
7774
7775 s = bfd_get_linker_section (dynobj, ".gnu.version");
7776 s->flags |= SEC_EXCLUDE;
7777 }
7778 }
7779 return true;
7780 }
7781
7782 /* Find the first non-excluded output section. We'll use its
7783 section symbol for some emitted relocs. */
7784 void
7785 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7786 {
7787 asection *s;
7788 asection *found = NULL;
7789
7790 for (s = output_bfd->sections; s != NULL; s = s->next)
7791 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7792 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7793 {
7794 found = s;
7795 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7796 break;
7797 }
7798 elf_hash_table (info)->text_index_section = found;
7799 }
7800
7801 /* Find two non-excluded output sections, one for code, one for data.
7802 We'll use their section symbols for some emitted relocs. */
7803 void
7804 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7805 {
7806 asection *s;
7807 asection *found = NULL;
7808
7809 /* Data first, since setting text_index_section changes
7810 _bfd_elf_omit_section_dynsym_default. */
7811 for (s = output_bfd->sections; s != NULL; s = s->next)
7812 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7813 && !(s->flags & SEC_READONLY)
7814 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7815 {
7816 found = s;
7817 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7818 break;
7819 }
7820 elf_hash_table (info)->data_index_section = found;
7821
7822 for (s = output_bfd->sections; s != NULL; s = s->next)
7823 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7824 && (s->flags & SEC_READONLY)
7825 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7826 {
7827 found = s;
7828 break;
7829 }
7830 elf_hash_table (info)->text_index_section = found;
7831 }
7832
7833 #define GNU_HASH_SECTION_NAME(bed) \
7834 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7835
7836 bool
7837 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7838 {
7839 const struct elf_backend_data *bed;
7840 unsigned long section_sym_count;
7841 bfd_size_type dynsymcount = 0;
7842
7843 if (!is_elf_hash_table (info->hash))
7844 return true;
7845
7846 bed = get_elf_backend_data (output_bfd);
7847 (*bed->elf_backend_init_index_section) (output_bfd, info);
7848
7849 /* Assign dynsym indices. In a shared library we generate a section
7850 symbol for each output section, which come first. Next come all
7851 of the back-end allocated local dynamic syms, followed by the rest
7852 of the global symbols.
7853
7854 This is usually not needed for static binaries, however backends
7855 can request to always do it, e.g. the MIPS backend uses dynamic
7856 symbol counts to lay out GOT, which will be produced in the
7857 presence of GOT relocations even in static binaries (holding fixed
7858 data in that case, to satisfy those relocations). */
7859
7860 if (elf_hash_table (info)->dynamic_sections_created
7861 || bed->always_renumber_dynsyms)
7862 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7863 &section_sym_count);
7864
7865 if (elf_hash_table (info)->dynamic_sections_created)
7866 {
7867 bfd *dynobj;
7868 asection *s;
7869 unsigned int dtagcount;
7870
7871 dynobj = elf_hash_table (info)->dynobj;
7872
7873 /* Work out the size of the symbol version section. */
7874 s = bfd_get_linker_section (dynobj, ".gnu.version");
7875 BFD_ASSERT (s != NULL);
7876 if ((s->flags & SEC_EXCLUDE) == 0)
7877 {
7878 s->size = dynsymcount * sizeof (Elf_External_Versym);
7879 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7880 if (s->contents == NULL)
7881 return false;
7882 s->alloced = 1;
7883
7884 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7885 return false;
7886 }
7887
7888 /* Set the size of the .dynsym and .hash sections. We counted
7889 the number of dynamic symbols in elf_link_add_object_symbols.
7890 We will build the contents of .dynsym and .hash when we build
7891 the final symbol table, because until then we do not know the
7892 correct value to give the symbols. We built the .dynstr
7893 section as we went along in elf_link_add_object_symbols. */
7894 s = elf_hash_table (info)->dynsym;
7895 BFD_ASSERT (s != NULL);
7896 s->size = dynsymcount * bed->s->sizeof_sym;
7897
7898 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7899 if (s->contents == NULL)
7900 return false;
7901 s->alloced = 1;
7902
7903 /* The first entry in .dynsym is a dummy symbol. Clear all the
7904 section syms, in case we don't output them all. */
7905 ++section_sym_count;
7906 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7907
7908 elf_hash_table (info)->bucketcount = 0;
7909
7910 /* Compute the size of the hashing table. As a side effect this
7911 computes the hash values for all the names we export. */
7912 if (info->emit_hash)
7913 {
7914 unsigned long int *hashcodes;
7915 struct hash_codes_info hashinf;
7916 bfd_size_type amt;
7917 unsigned long int nsyms;
7918 size_t bucketcount;
7919 size_t hash_entry_size;
7920
7921 /* Compute the hash values for all exported symbols. At the same
7922 time store the values in an array so that we could use them for
7923 optimizations. */
7924 amt = dynsymcount * sizeof (unsigned long int);
7925 hashcodes = (unsigned long int *) bfd_malloc (amt);
7926 if (hashcodes == NULL)
7927 return false;
7928 hashinf.hashcodes = hashcodes;
7929 hashinf.error = false;
7930
7931 /* Put all hash values in HASHCODES. */
7932 elf_link_hash_traverse (elf_hash_table (info),
7933 elf_collect_hash_codes, &hashinf);
7934 if (hashinf.error)
7935 {
7936 free (hashcodes);
7937 return false;
7938 }
7939
7940 nsyms = hashinf.hashcodes - hashcodes;
7941 bucketcount
7942 = compute_bucket_count (info, hashcodes, nsyms, 0);
7943 free (hashcodes);
7944
7945 if (bucketcount == 0 && nsyms > 0)
7946 return false;
7947
7948 elf_hash_table (info)->bucketcount = bucketcount;
7949
7950 s = bfd_get_linker_section (dynobj, ".hash");
7951 BFD_ASSERT (s != NULL);
7952 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7953 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7954 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7955 if (s->contents == NULL)
7956 return false;
7957 s->alloced = 1;
7958
7959 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7960 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7961 s->contents + hash_entry_size);
7962 }
7963
7964 if (info->emit_gnu_hash)
7965 {
7966 size_t i, cnt;
7967 unsigned char *contents;
7968 struct collect_gnu_hash_codes cinfo;
7969 bfd_size_type amt;
7970 size_t bucketcount;
7971
7972 memset (&cinfo, 0, sizeof (cinfo));
7973
7974 /* Compute the hash values for all exported symbols. At the same
7975 time store the values in an array so that we could use them for
7976 optimizations. */
7977 amt = dynsymcount * 2 * sizeof (unsigned long int);
7978 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7979 if (cinfo.hashcodes == NULL)
7980 return false;
7981
7982 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7983 cinfo.min_dynindx = -1;
7984 cinfo.output_bfd = output_bfd;
7985 cinfo.bed = bed;
7986
7987 /* Put all hash values in HASHCODES. */
7988 elf_link_hash_traverse (elf_hash_table (info),
7989 elf_collect_gnu_hash_codes, &cinfo);
7990 if (cinfo.error)
7991 {
7992 free (cinfo.hashcodes);
7993 return false;
7994 }
7995
7996 bucketcount
7997 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7998
7999 if (bucketcount == 0)
8000 {
8001 free (cinfo.hashcodes);
8002 return false;
8003 }
8004
8005 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
8006 BFD_ASSERT (s != NULL);
8007
8008 if (cinfo.nsyms == 0)
8009 {
8010 /* Empty .gnu.hash or .MIPS.xhash section is special. */
8011 BFD_ASSERT (cinfo.min_dynindx == -1);
8012 free (cinfo.hashcodes);
8013 s->size = 5 * 4 + bed->s->arch_size / 8;
8014 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8015 if (contents == NULL)
8016 return false;
8017 s->contents = contents;
8018 s->alloced = 1;
8019 /* 1 empty bucket. */
8020 bfd_put_32 (output_bfd, 1, contents);
8021 /* SYMIDX above the special symbol 0. */
8022 bfd_put_32 (output_bfd, 1, contents + 4);
8023 /* Just one word for bitmask. */
8024 bfd_put_32 (output_bfd, 1, contents + 8);
8025 /* Only hash fn bloom filter. */
8026 bfd_put_32 (output_bfd, 0, contents + 12);
8027 /* No hashes are valid - empty bitmask. */
8028 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
8029 /* No hashes in the only bucket. */
8030 bfd_put_32 (output_bfd, 0,
8031 contents + 16 + bed->s->arch_size / 8);
8032 }
8033 else
8034 {
8035 unsigned long int maskwords, maskbitslog2, x;
8036 BFD_ASSERT (cinfo.min_dynindx != -1);
8037
8038 x = cinfo.nsyms;
8039 maskbitslog2 = 1;
8040 while ((x >>= 1) != 0)
8041 ++maskbitslog2;
8042 if (maskbitslog2 < 3)
8043 maskbitslog2 = 5;
8044 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8045 maskbitslog2 = maskbitslog2 + 3;
8046 else
8047 maskbitslog2 = maskbitslog2 + 2;
8048 if (bed->s->arch_size == 64)
8049 {
8050 if (maskbitslog2 == 5)
8051 maskbitslog2 = 6;
8052 cinfo.shift1 = 6;
8053 }
8054 else
8055 cinfo.shift1 = 5;
8056 cinfo.mask = (1 << cinfo.shift1) - 1;
8057 cinfo.shift2 = maskbitslog2;
8058 cinfo.maskbits = 1 << maskbitslog2;
8059 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8060 amt = bucketcount * sizeof (unsigned long int) * 2;
8061 amt += maskwords * sizeof (bfd_vma);
8062 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
8063 if (cinfo.bitmask == NULL)
8064 {
8065 free (cinfo.hashcodes);
8066 return false;
8067 }
8068
8069 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
8070 cinfo.indx = cinfo.counts + bucketcount;
8071 cinfo.symindx = dynsymcount - cinfo.nsyms;
8072 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8073
8074 /* Determine how often each hash bucket is used. */
8075 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8076 for (i = 0; i < cinfo.nsyms; ++i)
8077 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8078
8079 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8080 if (cinfo.counts[i] != 0)
8081 {
8082 cinfo.indx[i] = cnt;
8083 cnt += cinfo.counts[i];
8084 }
8085 BFD_ASSERT (cnt == dynsymcount);
8086 cinfo.bucketcount = bucketcount;
8087 cinfo.local_indx = cinfo.min_dynindx;
8088
8089 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8090 s->size += cinfo.maskbits / 8;
8091 if (bed->record_xhash_symbol != NULL)
8092 s->size += cinfo.nsyms * 4;
8093 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8094 if (contents == NULL)
8095 {
8096 free (cinfo.bitmask);
8097 free (cinfo.hashcodes);
8098 return false;
8099 }
8100
8101 s->contents = contents;
8102 s->alloced = 1;
8103 bfd_put_32 (output_bfd, bucketcount, contents);
8104 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8105 bfd_put_32 (output_bfd, maskwords, contents + 8);
8106 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8107 contents += 16 + cinfo.maskbits / 8;
8108
8109 for (i = 0; i < bucketcount; ++i)
8110 {
8111 if (cinfo.counts[i] == 0)
8112 bfd_put_32 (output_bfd, 0, contents);
8113 else
8114 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8115 contents += 4;
8116 }
8117
8118 cinfo.contents = contents;
8119
8120 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8121 /* Renumber dynamic symbols, if populating .gnu.hash section.
8122 If using .MIPS.xhash, populate the translation table. */
8123 elf_link_hash_traverse (elf_hash_table (info),
8124 elf_gnu_hash_process_symidx, &cinfo);
8125
8126 contents = s->contents + 16;
8127 for (i = 0; i < maskwords; ++i)
8128 {
8129 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8130 contents);
8131 contents += bed->s->arch_size / 8;
8132 }
8133
8134 free (cinfo.bitmask);
8135 free (cinfo.hashcodes);
8136 }
8137 }
8138
8139 s = bfd_get_linker_section (dynobj, ".dynstr");
8140 BFD_ASSERT (s != NULL);
8141
8142 elf_finalize_dynstr (output_bfd, info);
8143
8144 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8145
8146 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8147 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8148 return false;
8149 }
8150
8151 return true;
8152 }
8153 \f
8154 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8155
8156 static void
8157 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8158 asection *sec)
8159 {
8160 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8161 sec->sec_info_type = SEC_INFO_TYPE_NONE;
8162 }
8163
8164 /* Finish SHF_MERGE section merging. */
8165
8166 bool
8167 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8168 {
8169 bfd *ibfd;
8170 asection *sec;
8171
8172 if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
8173 abort ();
8174
8175 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8176 if ((ibfd->flags & DYNAMIC) == 0
8177 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8178 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8179 == get_elf_backend_data (obfd)->s->elfclass))
8180 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8181 if ((sec->flags & SEC_MERGE) != 0
8182 && !bfd_is_abs_section (sec->output_section))
8183 {
8184 struct bfd_elf_section_data *secdata;
8185
8186 secdata = elf_section_data (sec);
8187 if (! _bfd_add_merge_section (obfd,
8188 &elf_hash_table (info)->merge_info,
8189 sec, &secdata->sec_info))
8190 return false;
8191 else if (secdata->sec_info)
8192 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8193 }
8194
8195 if (elf_hash_table (info)->merge_info != NULL)
8196 return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8197 merge_sections_remove_hook);
8198 return true;
8199 }
8200
8201 /* Create an entry in an ELF linker hash table. */
8202
8203 struct bfd_hash_entry *
8204 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8205 struct bfd_hash_table *table,
8206 const char *string)
8207 {
8208 /* Allocate the structure if it has not already been allocated by a
8209 subclass. */
8210 if (entry == NULL)
8211 {
8212 entry = (struct bfd_hash_entry *)
8213 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8214 if (entry == NULL)
8215 return entry;
8216 }
8217
8218 /* Call the allocation method of the superclass. */
8219 entry = _bfd_link_hash_newfunc (entry, table, string);
8220 if (entry != NULL)
8221 {
8222 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8223 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8224
8225 /* Set local fields. */
8226 ret->indx = -1;
8227 ret->dynindx = -1;
8228 ret->got = htab->init_got_refcount;
8229 ret->plt = htab->init_plt_refcount;
8230 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8231 - offsetof (struct elf_link_hash_entry, size)));
8232 /* Assume that we have been called by a non-ELF symbol reader.
8233 This flag is then reset by the code which reads an ELF input
8234 file. This ensures that a symbol created by a non-ELF symbol
8235 reader will have the flag set correctly. */
8236 ret->non_elf = 1;
8237 }
8238
8239 return entry;
8240 }
8241
8242 /* Copy data from an indirect symbol to its direct symbol, hiding the
8243 old indirect symbol. Also used for copying flags to a weakdef. */
8244
8245 void
8246 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8247 struct elf_link_hash_entry *dir,
8248 struct elf_link_hash_entry *ind)
8249 {
8250 struct elf_link_hash_table *htab;
8251
8252 if (ind->dyn_relocs != NULL)
8253 {
8254 if (dir->dyn_relocs != NULL)
8255 {
8256 struct elf_dyn_relocs **pp;
8257 struct elf_dyn_relocs *p;
8258
8259 /* Add reloc counts against the indirect sym to the direct sym
8260 list. Merge any entries against the same section. */
8261 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8262 {
8263 struct elf_dyn_relocs *q;
8264
8265 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8266 if (q->sec == p->sec)
8267 {
8268 q->pc_count += p->pc_count;
8269 q->count += p->count;
8270 *pp = p->next;
8271 break;
8272 }
8273 if (q == NULL)
8274 pp = &p->next;
8275 }
8276 *pp = dir->dyn_relocs;
8277 }
8278
8279 dir->dyn_relocs = ind->dyn_relocs;
8280 ind->dyn_relocs = NULL;
8281 }
8282
8283 /* Copy down any references that we may have already seen to the
8284 symbol which just became indirect. */
8285
8286 if (dir->versioned != versioned_hidden)
8287 dir->ref_dynamic |= ind->ref_dynamic;
8288 dir->ref_regular |= ind->ref_regular;
8289 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8290 dir->non_got_ref |= ind->non_got_ref;
8291 dir->needs_plt |= ind->needs_plt;
8292 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8293
8294 if (ind->root.type != bfd_link_hash_indirect)
8295 return;
8296
8297 /* Copy over the global and procedure linkage table refcount entries.
8298 These may have been already set up by a check_relocs routine. */
8299 htab = elf_hash_table (info);
8300 if (ind->got.refcount > htab->init_got_refcount.refcount)
8301 {
8302 if (dir->got.refcount < 0)
8303 dir->got.refcount = 0;
8304 dir->got.refcount += ind->got.refcount;
8305 ind->got.refcount = htab->init_got_refcount.refcount;
8306 }
8307
8308 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8309 {
8310 if (dir->plt.refcount < 0)
8311 dir->plt.refcount = 0;
8312 dir->plt.refcount += ind->plt.refcount;
8313 ind->plt.refcount = htab->init_plt_refcount.refcount;
8314 }
8315
8316 if (ind->dynindx != -1)
8317 {
8318 if (dir->dynindx != -1)
8319 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8320 dir->dynindx = ind->dynindx;
8321 dir->dynstr_index = ind->dynstr_index;
8322 ind->dynindx = -1;
8323 ind->dynstr_index = 0;
8324 }
8325 }
8326
8327 void
8328 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8329 struct elf_link_hash_entry *h,
8330 bool force_local)
8331 {
8332 /* STT_GNU_IFUNC symbol must go through PLT. */
8333 if (h->type != STT_GNU_IFUNC)
8334 {
8335 h->plt = elf_hash_table (info)->init_plt_offset;
8336 h->needs_plt = 0;
8337 }
8338 if (force_local)
8339 {
8340 h->forced_local = 1;
8341 if (h->dynindx != -1)
8342 {
8343 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8344 h->dynstr_index);
8345 h->dynindx = -1;
8346 h->dynstr_index = 0;
8347 }
8348 }
8349 }
8350
8351 /* Hide a symbol. */
8352
8353 void
8354 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8355 struct bfd_link_info *info,
8356 struct bfd_link_hash_entry *h)
8357 {
8358 if (is_elf_hash_table (info->hash))
8359 {
8360 const struct elf_backend_data *bed
8361 = get_elf_backend_data (output_bfd);
8362 struct elf_link_hash_entry *eh
8363 = (struct elf_link_hash_entry *) h;
8364 bed->elf_backend_hide_symbol (info, eh, true);
8365 eh->def_dynamic = 0;
8366 eh->ref_dynamic = 0;
8367 eh->dynamic_def = 0;
8368 }
8369 }
8370
8371 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8372 caller. */
8373
8374 bool
8375 _bfd_elf_link_hash_table_init
8376 (struct elf_link_hash_table *table,
8377 bfd *abfd,
8378 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8379 struct bfd_hash_table *,
8380 const char *),
8381 unsigned int entsize)
8382 {
8383 bool ret;
8384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8385 int can_refcount = bed->can_refcount;
8386
8387 table->init_got_refcount.refcount = can_refcount - 1;
8388 table->init_plt_refcount.refcount = can_refcount - 1;
8389 table->init_got_offset.offset = -(bfd_vma) 1;
8390 table->init_plt_offset.offset = -(bfd_vma) 1;
8391 /* The first dynamic symbol is a dummy. */
8392 table->dynsymcount = 1;
8393
8394 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8395
8396 table->root.type = bfd_link_elf_hash_table;
8397 table->hash_table_id = bed->target_id;
8398 table->target_os = bed->target_os;
8399 table->root.hash_table_free = _bfd_elf_link_hash_table_free;
8400
8401 return ret;
8402 }
8403
8404 /* Create an ELF linker hash table. */
8405
8406 struct bfd_link_hash_table *
8407 _bfd_elf_link_hash_table_create (bfd *abfd)
8408 {
8409 struct elf_link_hash_table *ret;
8410 size_t amt = sizeof (struct elf_link_hash_table);
8411
8412 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8413 if (ret == NULL)
8414 return NULL;
8415
8416 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8417 sizeof (struct elf_link_hash_entry)))
8418 {
8419 free (ret);
8420 return NULL;
8421 }
8422
8423 return &ret->root;
8424 }
8425
8426 /* Destroy an ELF linker hash table. */
8427
8428 void
8429 _bfd_elf_link_hash_table_free (bfd *obfd)
8430 {
8431 struct elf_link_hash_table *htab;
8432
8433 htab = (struct elf_link_hash_table *) obfd->link.hash;
8434 if (htab->dynstr != NULL)
8435 _bfd_elf_strtab_free (htab->dynstr);
8436 _bfd_merge_sections_free (htab->merge_info);
8437 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8438 if (htab->dynamic != NULL)
8439 {
8440 free (htab->dynamic->contents);
8441 htab->dynamic->contents = NULL;
8442 }
8443 if (htab->first_hash != NULL)
8444 {
8445 bfd_hash_table_free (htab->first_hash);
8446 free (htab->first_hash);
8447 }
8448 if (htab->eh_info.frame_hdr_is_compact)
8449 free (htab->eh_info.u.compact.entries);
8450 else
8451 free (htab->eh_info.u.dwarf.array);
8452 _bfd_generic_link_hash_table_free (obfd);
8453 }
8454
8455 /* This is a hook for the ELF emulation code in the generic linker to
8456 tell the backend linker what file name to use for the DT_NEEDED
8457 entry for a dynamic object. */
8458
8459 void
8460 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8461 {
8462 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8463 && bfd_get_format (abfd) == bfd_object)
8464 elf_dt_name (abfd) = name;
8465 }
8466
8467 int
8468 bfd_elf_get_dyn_lib_class (bfd *abfd)
8469 {
8470 int lib_class;
8471 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8472 && bfd_get_format (abfd) == bfd_object)
8473 lib_class = elf_dyn_lib_class (abfd);
8474 else
8475 lib_class = 0;
8476 return lib_class;
8477 }
8478
8479 void
8480 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8481 {
8482 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8483 && bfd_get_format (abfd) == bfd_object)
8484 elf_dyn_lib_class (abfd) = lib_class;
8485 }
8486
8487 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8488 the linker ELF emulation code. */
8489
8490 struct bfd_link_needed_list *
8491 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8492 struct bfd_link_info *info)
8493 {
8494 if (! is_elf_hash_table (info->hash))
8495 return NULL;
8496 return elf_hash_table (info)->needed;
8497 }
8498
8499 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8500 hook for the linker ELF emulation code. */
8501
8502 struct bfd_link_needed_list *
8503 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8504 struct bfd_link_info *info)
8505 {
8506 if (! is_elf_hash_table (info->hash))
8507 return NULL;
8508 return elf_hash_table (info)->runpath;
8509 }
8510
8511 /* Get the name actually used for a dynamic object for a link. This
8512 is the SONAME entry if there is one. Otherwise, it is the string
8513 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8514
8515 const char *
8516 bfd_elf_get_dt_soname (bfd *abfd)
8517 {
8518 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8519 && bfd_get_format (abfd) == bfd_object)
8520 return elf_dt_name (abfd);
8521 return NULL;
8522 }
8523
8524 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8525 the ELF linker emulation code. */
8526
8527 bool
8528 bfd_elf_get_bfd_needed_list (bfd *abfd,
8529 struct bfd_link_needed_list **pneeded)
8530 {
8531 asection *s;
8532 bfd_byte *dynbuf = NULL;
8533 unsigned int elfsec;
8534 unsigned long shlink;
8535 bfd_byte *extdyn, *extdynend;
8536 size_t extdynsize;
8537 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8538
8539 *pneeded = NULL;
8540
8541 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8542 || bfd_get_format (abfd) != bfd_object)
8543 return true;
8544
8545 s = bfd_get_section_by_name (abfd, ".dynamic");
8546 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8547 return true;
8548
8549 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8550 goto error_return;
8551
8552 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8553 if (elfsec == SHN_BAD)
8554 goto error_return;
8555
8556 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8557
8558 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8559 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8560
8561 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8562 (size_t) (extdynend - extdyn) >= extdynsize;
8563 extdyn += extdynsize)
8564 {
8565 Elf_Internal_Dyn dyn;
8566
8567 (*swap_dyn_in) (abfd, extdyn, &dyn);
8568
8569 if (dyn.d_tag == DT_NULL)
8570 break;
8571
8572 if (dyn.d_tag == DT_NEEDED)
8573 {
8574 const char *string;
8575 struct bfd_link_needed_list *l;
8576 unsigned int tagv = dyn.d_un.d_val;
8577 size_t amt;
8578
8579 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8580 if (string == NULL)
8581 goto error_return;
8582
8583 amt = sizeof *l;
8584 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8585 if (l == NULL)
8586 goto error_return;
8587
8588 l->by = abfd;
8589 l->name = string;
8590 l->next = *pneeded;
8591 *pneeded = l;
8592 }
8593 }
8594
8595 _bfd_elf_munmap_section_contents (s, dynbuf);
8596
8597 return true;
8598
8599 error_return:
8600 _bfd_elf_munmap_section_contents (s, dynbuf);
8601 return false;
8602 }
8603
8604 struct elf_symbuf_symbol
8605 {
8606 unsigned long st_name; /* Symbol name, index in string tbl */
8607 unsigned char st_info; /* Type and binding attributes */
8608 unsigned char st_other; /* Visibilty, and target specific */
8609 };
8610
8611 struct elf_symbuf_head
8612 {
8613 struct elf_symbuf_symbol *ssym;
8614 size_t count;
8615 unsigned int st_shndx;
8616 };
8617
8618 struct elf_symbol
8619 {
8620 union
8621 {
8622 Elf_Internal_Sym *isym;
8623 struct elf_symbuf_symbol *ssym;
8624 void *p;
8625 } u;
8626 const char *name;
8627 };
8628
8629 /* Sort references to symbols by ascending section number. */
8630
8631 static int
8632 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8633 {
8634 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8635 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8636
8637 if (s1->st_shndx != s2->st_shndx)
8638 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8639 /* Final sort by the address of the sym in the symbuf ensures
8640 a stable sort. */
8641 if (s1 != s2)
8642 return s1 > s2 ? 1 : -1;
8643 return 0;
8644 }
8645
8646 static int
8647 elf_sym_name_compare (const void *arg1, const void *arg2)
8648 {
8649 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8650 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8651 int ret = strcmp (s1->name, s2->name);
8652 if (ret != 0)
8653 return ret;
8654 if (s1->u.p != s2->u.p)
8655 return s1->u.p > s2->u.p ? 1 : -1;
8656 return 0;
8657 }
8658
8659 static struct elf_symbuf_head *
8660 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8661 {
8662 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8663 struct elf_symbuf_symbol *ssym;
8664 struct elf_symbuf_head *ssymbuf, *ssymhead;
8665 size_t i, shndx_count, total_size, amt;
8666
8667 amt = symcount * sizeof (*indbuf);
8668 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8669 if (indbuf == NULL)
8670 return NULL;
8671
8672 for (ind = indbuf, i = 0; i < symcount; i++)
8673 if (isymbuf[i].st_shndx != SHN_UNDEF)
8674 *ind++ = &isymbuf[i];
8675 indbufend = ind;
8676
8677 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8678 elf_sort_elf_symbol);
8679
8680 shndx_count = 0;
8681 if (indbufend > indbuf)
8682 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8683 if (ind[0]->st_shndx != ind[1]->st_shndx)
8684 shndx_count++;
8685
8686 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8687 + (indbufend - indbuf) * sizeof (*ssym));
8688 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8689 if (ssymbuf == NULL)
8690 {
8691 free (indbuf);
8692 return NULL;
8693 }
8694
8695 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8696 ssymbuf->ssym = NULL;
8697 ssymbuf->count = shndx_count;
8698 ssymbuf->st_shndx = 0;
8699 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8700 {
8701 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8702 {
8703 ssymhead++;
8704 ssymhead->ssym = ssym;
8705 ssymhead->count = 0;
8706 ssymhead->st_shndx = (*ind)->st_shndx;
8707 }
8708 ssym->st_name = (*ind)->st_name;
8709 ssym->st_info = (*ind)->st_info;
8710 ssym->st_other = (*ind)->st_other;
8711 ssymhead->count++;
8712 }
8713 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8714 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8715
8716 free (indbuf);
8717 return ssymbuf;
8718 }
8719
8720 /* Check if 2 sections define the same set of local and global
8721 symbols. */
8722
8723 static bool
8724 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8725 struct bfd_link_info *info)
8726 {
8727 bfd *bfd1, *bfd2;
8728 const struct elf_backend_data *bed1, *bed2;
8729 Elf_Internal_Shdr *hdr1, *hdr2;
8730 size_t symcount1, symcount2;
8731 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8732 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8733 Elf_Internal_Sym *isym, *isymend;
8734 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8735 size_t count1, count2, sec_count1, sec_count2, i;
8736 unsigned int shndx1, shndx2;
8737 bool result;
8738 bool ignore_section_symbol_p;
8739
8740 bfd1 = sec1->owner;
8741 bfd2 = sec2->owner;
8742
8743 /* Both sections have to be in ELF. */
8744 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8745 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8746 return false;
8747
8748 if (elf_section_type (sec1) != elf_section_type (sec2))
8749 return false;
8750
8751 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8752 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8753 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8754 return false;
8755
8756 bed1 = get_elf_backend_data (bfd1);
8757 bed2 = get_elf_backend_data (bfd2);
8758 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8759 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8760 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8761 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8762
8763 if (symcount1 == 0 || symcount2 == 0)
8764 return false;
8765
8766 result = false;
8767 isymbuf1 = NULL;
8768 isymbuf2 = NULL;
8769 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8770 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8771
8772 /* Ignore section symbols only when matching non-debugging sections
8773 or linkonce section with comdat section. */
8774 ignore_section_symbol_p
8775 = ((sec1->flags & SEC_DEBUGGING) == 0
8776 || ((elf_section_flags (sec1) & SHF_GROUP)
8777 != (elf_section_flags (sec2) & SHF_GROUP)));
8778
8779 if (ssymbuf1 == NULL)
8780 {
8781 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8782 NULL, NULL, NULL);
8783 if (isymbuf1 == NULL)
8784 goto done;
8785
8786 if (info != NULL && !info->reduce_memory_overheads)
8787 {
8788 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8789 elf_tdata (bfd1)->symbuf = ssymbuf1;
8790 }
8791 }
8792
8793 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8794 {
8795 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8796 NULL, NULL, NULL);
8797 if (isymbuf2 == NULL)
8798 goto done;
8799
8800 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8801 {
8802 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8803 elf_tdata (bfd2)->symbuf = ssymbuf2;
8804 }
8805 }
8806
8807 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8808 {
8809 /* Optimized faster version. */
8810 size_t lo, hi, mid;
8811 struct elf_symbol *symp;
8812 struct elf_symbuf_symbol *ssym, *ssymend;
8813
8814 lo = 0;
8815 hi = ssymbuf1->count;
8816 ssymbuf1++;
8817 count1 = 0;
8818 sec_count1 = 0;
8819 while (lo < hi)
8820 {
8821 mid = (lo + hi) / 2;
8822 if (shndx1 < ssymbuf1[mid].st_shndx)
8823 hi = mid;
8824 else if (shndx1 > ssymbuf1[mid].st_shndx)
8825 lo = mid + 1;
8826 else
8827 {
8828 count1 = ssymbuf1[mid].count;
8829 ssymbuf1 += mid;
8830 break;
8831 }
8832 }
8833 if (ignore_section_symbol_p)
8834 {
8835 for (i = 0; i < count1; i++)
8836 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8837 sec_count1++;
8838 count1 -= sec_count1;
8839 }
8840
8841 lo = 0;
8842 hi = ssymbuf2->count;
8843 ssymbuf2++;
8844 count2 = 0;
8845 sec_count2 = 0;
8846 while (lo < hi)
8847 {
8848 mid = (lo + hi) / 2;
8849 if (shndx2 < ssymbuf2[mid].st_shndx)
8850 hi = mid;
8851 else if (shndx2 > ssymbuf2[mid].st_shndx)
8852 lo = mid + 1;
8853 else
8854 {
8855 count2 = ssymbuf2[mid].count;
8856 ssymbuf2 += mid;
8857 break;
8858 }
8859 }
8860 if (ignore_section_symbol_p)
8861 {
8862 for (i = 0; i < count2; i++)
8863 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8864 sec_count2++;
8865 count2 -= sec_count2;
8866 }
8867
8868 if (count1 == 0 || count2 == 0 || count1 != count2)
8869 goto done;
8870
8871 symtable1
8872 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8873 symtable2
8874 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8875 if (symtable1 == NULL || symtable2 == NULL)
8876 goto done;
8877
8878 symp = symtable1;
8879 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8880 ssym < ssymend; ssym++)
8881 if (sec_count1 == 0
8882 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8883 {
8884 symp->u.ssym = ssym;
8885 symp->name = bfd_elf_string_from_elf_section (bfd1,
8886 hdr1->sh_link,
8887 ssym->st_name);
8888 if (symp->name == NULL)
8889 goto done;
8890 symp++;
8891 }
8892
8893 symp = symtable2;
8894 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8895 ssym < ssymend; ssym++)
8896 if (sec_count2 == 0
8897 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8898 {
8899 symp->u.ssym = ssym;
8900 symp->name = bfd_elf_string_from_elf_section (bfd2,
8901 hdr2->sh_link,
8902 ssym->st_name);
8903 if (symp->name == NULL)
8904 goto done;
8905 symp++;
8906 }
8907
8908 /* Sort symbol by name. */
8909 qsort (symtable1, count1, sizeof (struct elf_symbol),
8910 elf_sym_name_compare);
8911 qsort (symtable2, count1, sizeof (struct elf_symbol),
8912 elf_sym_name_compare);
8913
8914 for (i = 0; i < count1; i++)
8915 /* Two symbols must have the same binding, type and name. */
8916 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8917 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8918 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8919 goto done;
8920
8921 result = true;
8922 goto done;
8923 }
8924
8925 symtable1 = (struct elf_symbol *)
8926 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8927 symtable2 = (struct elf_symbol *)
8928 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8929 if (symtable1 == NULL || symtable2 == NULL)
8930 goto done;
8931
8932 /* Count definitions in the section. */
8933 count1 = 0;
8934 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8935 if (isym->st_shndx == shndx1
8936 && (!ignore_section_symbol_p
8937 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8938 symtable1[count1++].u.isym = isym;
8939
8940 count2 = 0;
8941 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8942 if (isym->st_shndx == shndx2
8943 && (!ignore_section_symbol_p
8944 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8945 symtable2[count2++].u.isym = isym;
8946
8947 if (count1 == 0 || count2 == 0 || count1 != count2)
8948 goto done;
8949
8950 for (i = 0; i < count1; i++)
8951 {
8952 symtable1[i].name
8953 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8954 symtable1[i].u.isym->st_name);
8955 if (symtable1[i].name == NULL)
8956 goto done;
8957 }
8958
8959 for (i = 0; i < count2; i++)
8960 {
8961 symtable2[i].name
8962 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8963 symtable2[i].u.isym->st_name);
8964 if (symtable2[i].name == NULL)
8965 goto done;
8966 }
8967
8968 /* Sort symbol by name. */
8969 qsort (symtable1, count1, sizeof (struct elf_symbol),
8970 elf_sym_name_compare);
8971 qsort (symtable2, count1, sizeof (struct elf_symbol),
8972 elf_sym_name_compare);
8973
8974 for (i = 0; i < count1; i++)
8975 /* Two symbols must have the same binding, type and name. */
8976 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8977 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8978 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8979 goto done;
8980
8981 result = true;
8982
8983 done:
8984 free (symtable1);
8985 free (symtable2);
8986 free (isymbuf1);
8987 free (isymbuf2);
8988
8989 return result;
8990 }
8991
8992 /* Return TRUE if 2 section types are compatible. */
8993
8994 bool
8995 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8996 bfd *bbfd, const asection *bsec)
8997 {
8998 if (asec == NULL
8999 || bsec == NULL
9000 || abfd->xvec->flavour != bfd_target_elf_flavour
9001 || bbfd->xvec->flavour != bfd_target_elf_flavour)
9002 return true;
9003
9004 return elf_section_type (asec) == elf_section_type (bsec);
9005 }
9006 \f
9007 /* Final phase of ELF linker. */
9008
9009 /* A structure we use to avoid passing large numbers of arguments. */
9010
9011 struct elf_final_link_info
9012 {
9013 /* General link information. */
9014 struct bfd_link_info *info;
9015 /* Output BFD. */
9016 bfd *output_bfd;
9017 /* Symbol string table. */
9018 struct elf_strtab_hash *symstrtab;
9019 /* .hash section. */
9020 asection *hash_sec;
9021 /* symbol version section (.gnu.version). */
9022 asection *symver_sec;
9023 /* Buffer large enough to hold contents of any section. */
9024 bfd_byte *contents;
9025 /* Buffer large enough to hold external relocs of any section. */
9026 void *external_relocs;
9027 /* Buffer large enough to hold internal relocs of any section. */
9028 Elf_Internal_Rela *internal_relocs;
9029 /* Buffer large enough to hold external local symbols of any input
9030 BFD. */
9031 bfd_byte *external_syms;
9032 /* And a buffer for symbol section indices. */
9033 Elf_External_Sym_Shndx *locsym_shndx;
9034 /* Buffer large enough to hold internal local symbols of any input
9035 BFD. */
9036 Elf_Internal_Sym *internal_syms;
9037 /* Array large enough to hold a symbol index for each local symbol
9038 of any input BFD. */
9039 long *indices;
9040 /* Array large enough to hold a section pointer for each local
9041 symbol of any input BFD. */
9042 asection **sections;
9043 /* Buffer for SHT_SYMTAB_SHNDX section. */
9044 Elf_External_Sym_Shndx *symshndxbuf;
9045 /* Number of STT_FILE syms seen. */
9046 size_t filesym_count;
9047 /* Local symbol hash table. */
9048 struct bfd_hash_table local_hash_table;
9049 };
9050
9051 struct local_hash_entry
9052 {
9053 /* Base hash table entry structure. */
9054 struct bfd_hash_entry root;
9055 /* Size of the local symbol name. */
9056 size_t size;
9057 /* Number of the duplicated local symbol names. */
9058 long count;
9059 };
9060
9061 /* Create an entry in the local symbol hash table. */
9062
9063 static struct bfd_hash_entry *
9064 local_hash_newfunc (struct bfd_hash_entry *entry,
9065 struct bfd_hash_table *table,
9066 const char *string)
9067 {
9068
9069 /* Allocate the structure if it has not already been allocated by a
9070 subclass. */
9071 if (entry == NULL)
9072 {
9073 entry = bfd_hash_allocate (table,
9074 sizeof (struct local_hash_entry));
9075 if (entry == NULL)
9076 return entry;
9077 }
9078
9079 /* Call the allocation method of the superclass. */
9080 entry = bfd_hash_newfunc (entry, table, string);
9081 if (entry != NULL)
9082 {
9083 ((struct local_hash_entry *) entry)->count = 0;
9084 ((struct local_hash_entry *) entry)->size = 0;
9085 }
9086
9087 return entry;
9088 }
9089
9090 /* This struct is used to pass information to elf_link_output_extsym. */
9091
9092 struct elf_outext_info
9093 {
9094 bool failed;
9095 bool localsyms;
9096 bool file_sym_done;
9097 struct elf_final_link_info *flinfo;
9098 };
9099
9100
9101 /* Support for evaluating a complex relocation.
9102
9103 Complex relocations are generalized, self-describing relocations. The
9104 implementation of them consists of two parts: complex symbols, and the
9105 relocations themselves.
9106
9107 The relocations use a reserved elf-wide relocation type code (R_RELC
9108 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9109 information (start bit, end bit, word width, etc) into the addend. This
9110 information is extracted from CGEN-generated operand tables within gas.
9111
9112 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9113 internal) representing prefix-notation expressions, including but not
9114 limited to those sorts of expressions normally encoded as addends in the
9115 addend field. The symbol mangling format is:
9116
9117 <node> := <literal>
9118 | <unary-operator> ':' <node>
9119 | <binary-operator> ':' <node> ':' <node>
9120 ;
9121
9122 <literal> := 's' <digits=N> ':' <N character symbol name>
9123 | 'S' <digits=N> ':' <N character section name>
9124 | '#' <hexdigits>
9125 ;
9126
9127 <binary-operator> := as in C
9128 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9129
9130 static void
9131 set_symbol_value (bfd *bfd_with_globals,
9132 Elf_Internal_Sym *isymbuf,
9133 size_t locsymcount,
9134 size_t symidx,
9135 bfd_vma val)
9136 {
9137 struct elf_link_hash_entry *h;
9138 size_t extsymoff = locsymcount;
9139
9140 if (symidx < locsymcount)
9141 {
9142 Elf_Internal_Sym *sym;
9143
9144 sym = isymbuf + symidx;
9145 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9146 {
9147 /* It is a local symbol: move it to the
9148 "absolute" section and give it a value. */
9149 sym->st_shndx = SHN_ABS;
9150 sym->st_value = val;
9151 return;
9152 }
9153 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9154 extsymoff = 0;
9155 }
9156
9157 /* It is a global symbol: set its link type
9158 to "defined" and give it a value. */
9159 h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff);
9160 if (h == NULL)
9161 {
9162 /* FIXMEL What should we do ? */
9163 return;
9164 }
9165 h->root.type = bfd_link_hash_defined;
9166 h->root.u.def.value = val;
9167 h->root.u.def.section = bfd_abs_section_ptr;
9168 }
9169
9170 static bool
9171 resolve_symbol (const char *name,
9172 bfd *input_bfd,
9173 struct elf_final_link_info *flinfo,
9174 bfd_vma *result,
9175 Elf_Internal_Sym *isymbuf,
9176 size_t locsymcount)
9177 {
9178 Elf_Internal_Sym *sym;
9179 struct bfd_link_hash_entry *global_entry;
9180 const char *candidate = NULL;
9181 Elf_Internal_Shdr *symtab_hdr;
9182 size_t i;
9183
9184 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9185
9186 for (i = 0; i < locsymcount; ++ i)
9187 {
9188 sym = isymbuf + i;
9189
9190 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9191 continue;
9192
9193 candidate = bfd_elf_string_from_elf_section (input_bfd,
9194 symtab_hdr->sh_link,
9195 sym->st_name);
9196 #ifdef DEBUG
9197 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9198 name, candidate, (unsigned long) sym->st_value);
9199 #endif
9200 if (candidate && strcmp (candidate, name) == 0)
9201 {
9202 asection *sec = flinfo->sections [i];
9203
9204 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9205 *result += sec->output_offset + sec->output_section->vma;
9206 #ifdef DEBUG
9207 printf ("Found symbol with value %8.8lx\n",
9208 (unsigned long) *result);
9209 #endif
9210 return true;
9211 }
9212 }
9213
9214 /* Hmm, haven't found it yet. perhaps it is a global. */
9215 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9216 false, false, true);
9217 if (!global_entry)
9218 return false;
9219
9220 if (global_entry->type == bfd_link_hash_defined
9221 || global_entry->type == bfd_link_hash_defweak)
9222 {
9223 *result = (global_entry->u.def.value
9224 + global_entry->u.def.section->output_section->vma
9225 + global_entry->u.def.section->output_offset);
9226 #ifdef DEBUG
9227 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9228 global_entry->root.string, (unsigned long) *result);
9229 #endif
9230 return true;
9231 }
9232
9233 return false;
9234 }
9235
9236 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9237 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9238 names like "foo.end" which is the end address of section "foo". */
9239
9240 static bool
9241 resolve_section (const char *name,
9242 asection *sections,
9243 bfd_vma *result,
9244 bfd * abfd)
9245 {
9246 asection *curr;
9247 unsigned int len;
9248
9249 for (curr = sections; curr; curr = curr->next)
9250 if (strcmp (curr->name, name) == 0)
9251 {
9252 *result = curr->vma;
9253 return true;
9254 }
9255
9256 /* Hmm. still haven't found it. try pseudo-section names. */
9257 /* FIXME: This could be coded more efficiently... */
9258 for (curr = sections; curr; curr = curr->next)
9259 {
9260 len = strlen (curr->name);
9261 if (len > strlen (name))
9262 continue;
9263
9264 if (strncmp (curr->name, name, len) == 0)
9265 {
9266 if (startswith (name + len, ".end"))
9267 {
9268 *result = (curr->vma
9269 + curr->size / bfd_octets_per_byte (abfd, curr));
9270 return true;
9271 }
9272
9273 /* Insert more pseudo-section names here, if you like. */
9274 }
9275 }
9276
9277 return false;
9278 }
9279
9280 static void
9281 undefined_reference (const char *reftype, const char *name)
9282 {
9283 /* xgettext:c-format */
9284 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9285 reftype, name);
9286 bfd_set_error (bfd_error_bad_value);
9287 }
9288
9289 static bool
9290 eval_symbol (bfd_vma *result,
9291 const char **symp,
9292 bfd *input_bfd,
9293 struct elf_final_link_info *flinfo,
9294 bfd_vma dot,
9295 Elf_Internal_Sym *isymbuf,
9296 size_t locsymcount,
9297 int signed_p)
9298 {
9299 size_t len;
9300 size_t symlen;
9301 bfd_vma a;
9302 bfd_vma b;
9303 char symbuf[4096];
9304 const char *sym = *symp;
9305 const char *symend;
9306 bool symbol_is_section = false;
9307
9308 len = strlen (sym);
9309 symend = sym + len;
9310
9311 if (len < 1 || len > sizeof (symbuf))
9312 {
9313 bfd_set_error (bfd_error_invalid_operation);
9314 return false;
9315 }
9316
9317 switch (* sym)
9318 {
9319 case '.':
9320 *result = dot;
9321 *symp = sym + 1;
9322 return true;
9323
9324 case '#':
9325 ++sym;
9326 *result = strtoul (sym, (char **) symp, 16);
9327 return true;
9328
9329 case 'S':
9330 symbol_is_section = true;
9331 /* Fall through. */
9332 case 's':
9333 ++sym;
9334 symlen = strtol (sym, (char **) symp, 10);
9335 sym = *symp + 1; /* Skip the trailing ':'. */
9336
9337 if (symend < sym || symlen + 1 > sizeof (symbuf))
9338 {
9339 bfd_set_error (bfd_error_invalid_operation);
9340 return false;
9341 }
9342
9343 memcpy (symbuf, sym, symlen);
9344 symbuf[symlen] = '\0';
9345 *symp = sym + symlen;
9346
9347 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9348 the symbol as a section, or vice-versa. so we're pretty liberal in our
9349 interpretation here; section means "try section first", not "must be a
9350 section", and likewise with symbol. */
9351
9352 if (symbol_is_section)
9353 {
9354 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9355 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9356 isymbuf, locsymcount))
9357 {
9358 undefined_reference ("section", symbuf);
9359 return false;
9360 }
9361 }
9362 else
9363 {
9364 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9365 isymbuf, locsymcount)
9366 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9367 result, input_bfd))
9368 {
9369 undefined_reference ("symbol", symbuf);
9370 return false;
9371 }
9372 }
9373
9374 return true;
9375
9376 /* All that remains are operators. */
9377
9378 #define UNARY_OP(op) \
9379 if (startswith (sym, #op)) \
9380 { \
9381 sym += strlen (#op); \
9382 if (*sym == ':') \
9383 ++sym; \
9384 *symp = sym; \
9385 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9386 isymbuf, locsymcount, signed_p)) \
9387 return false; \
9388 if (signed_p) \
9389 *result = op ((bfd_signed_vma) a); \
9390 else \
9391 *result = op a; \
9392 return true; \
9393 }
9394
9395 #define BINARY_OP_HEAD(op) \
9396 if (startswith (sym, #op)) \
9397 { \
9398 sym += strlen (#op); \
9399 if (*sym == ':') \
9400 ++sym; \
9401 *symp = sym; \
9402 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9403 isymbuf, locsymcount, signed_p)) \
9404 return false; \
9405 ++*symp; \
9406 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9407 isymbuf, locsymcount, signed_p)) \
9408 return false;
9409 #define BINARY_OP_TAIL(op) \
9410 if (signed_p) \
9411 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9412 else \
9413 *result = a op b; \
9414 return true; \
9415 }
9416 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9417
9418 default:
9419 UNARY_OP (0-);
9420 BINARY_OP_HEAD (<<);
9421 if (b >= sizeof (a) * CHAR_BIT)
9422 {
9423 *result = 0;
9424 return true;
9425 }
9426 signed_p = 0;
9427 BINARY_OP_TAIL (<<);
9428 BINARY_OP_HEAD (>>);
9429 if (b >= sizeof (a) * CHAR_BIT)
9430 {
9431 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9432 return true;
9433 }
9434 BINARY_OP_TAIL (>>);
9435 BINARY_OP (==);
9436 BINARY_OP (!=);
9437 BINARY_OP (<=);
9438 BINARY_OP (>=);
9439 BINARY_OP (&&);
9440 BINARY_OP (||);
9441 UNARY_OP (~);
9442 UNARY_OP (!);
9443 BINARY_OP (*);
9444 BINARY_OP_HEAD (/);
9445 if (b == 0)
9446 {
9447 _bfd_error_handler (_("division by zero"));
9448 bfd_set_error (bfd_error_bad_value);
9449 return false;
9450 }
9451 BINARY_OP_TAIL (/);
9452 BINARY_OP_HEAD (%);
9453 if (b == 0)
9454 {
9455 _bfd_error_handler (_("division by zero"));
9456 bfd_set_error (bfd_error_bad_value);
9457 return false;
9458 }
9459 BINARY_OP_TAIL (%);
9460 BINARY_OP (^);
9461 BINARY_OP (|);
9462 BINARY_OP (&);
9463 BINARY_OP (+);
9464 BINARY_OP (-);
9465 BINARY_OP (<);
9466 BINARY_OP (>);
9467 #undef UNARY_OP
9468 #undef BINARY_OP
9469 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9470 bfd_set_error (bfd_error_invalid_operation);
9471 return false;
9472 }
9473 }
9474
9475 static void
9476 put_value (bfd_vma size,
9477 unsigned long chunksz,
9478 bfd *input_bfd,
9479 bfd_vma x,
9480 bfd_byte *location)
9481 {
9482 location += (size - chunksz);
9483
9484 for (; size; size -= chunksz, location -= chunksz)
9485 {
9486 switch (chunksz)
9487 {
9488 case 1:
9489 bfd_put_8 (input_bfd, x, location);
9490 x >>= 8;
9491 break;
9492 case 2:
9493 bfd_put_16 (input_bfd, x, location);
9494 x >>= 16;
9495 break;
9496 case 4:
9497 bfd_put_32 (input_bfd, x, location);
9498 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9499 x >>= 16;
9500 x >>= 16;
9501 break;
9502 #ifdef BFD64
9503 case 8:
9504 bfd_put_64 (input_bfd, x, location);
9505 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9506 x >>= 32;
9507 x >>= 32;
9508 break;
9509 #endif
9510 default:
9511 abort ();
9512 break;
9513 }
9514 }
9515 }
9516
9517 static bfd_vma
9518 get_value (bfd_vma size,
9519 unsigned long chunksz,
9520 bfd *input_bfd,
9521 bfd_byte *location)
9522 {
9523 int shift;
9524 bfd_vma x = 0;
9525
9526 /* Sanity checks. */
9527 BFD_ASSERT (chunksz <= sizeof (x)
9528 && size >= chunksz
9529 && chunksz != 0
9530 && (size % chunksz) == 0
9531 && input_bfd != NULL
9532 && location != NULL);
9533
9534 if (chunksz == sizeof (x))
9535 {
9536 BFD_ASSERT (size == chunksz);
9537
9538 /* Make sure that we do not perform an undefined shift operation.
9539 We know that size == chunksz so there will only be one iteration
9540 of the loop below. */
9541 shift = 0;
9542 }
9543 else
9544 shift = 8 * chunksz;
9545
9546 for (; size; size -= chunksz, location += chunksz)
9547 {
9548 switch (chunksz)
9549 {
9550 case 1:
9551 x = (x << shift) | bfd_get_8 (input_bfd, location);
9552 break;
9553 case 2:
9554 x = (x << shift) | bfd_get_16 (input_bfd, location);
9555 break;
9556 case 4:
9557 x = (x << shift) | bfd_get_32 (input_bfd, location);
9558 break;
9559 #ifdef BFD64
9560 case 8:
9561 x = (x << shift) | bfd_get_64 (input_bfd, location);
9562 break;
9563 #endif
9564 default:
9565 abort ();
9566 }
9567 }
9568 return x;
9569 }
9570
9571 static void
9572 decode_complex_addend (unsigned long *start, /* in bits */
9573 unsigned long *oplen, /* in bits */
9574 unsigned long *len, /* in bits */
9575 unsigned long *wordsz, /* in bytes */
9576 unsigned long *chunksz, /* in bytes */
9577 unsigned long *lsb0_p,
9578 unsigned long *signed_p,
9579 unsigned long *trunc_p,
9580 unsigned long encoded)
9581 {
9582 * start = encoded & 0x3F;
9583 * len = (encoded >> 6) & 0x3F;
9584 * oplen = (encoded >> 12) & 0x3F;
9585 * wordsz = (encoded >> 18) & 0xF;
9586 * chunksz = (encoded >> 22) & 0xF;
9587 * lsb0_p = (encoded >> 27) & 1;
9588 * signed_p = (encoded >> 28) & 1;
9589 * trunc_p = (encoded >> 29) & 1;
9590 }
9591
9592 bfd_reloc_status_type
9593 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9594 asection *input_section,
9595 bfd_byte *contents,
9596 Elf_Internal_Rela *rel,
9597 bfd_vma relocation)
9598 {
9599 bfd_vma shift, x, mask;
9600 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9601 bfd_reloc_status_type r;
9602 bfd_size_type octets;
9603
9604 /* Perform this reloc, since it is complex.
9605 (this is not to say that it necessarily refers to a complex
9606 symbol; merely that it is a self-describing CGEN based reloc.
9607 i.e. the addend has the complete reloc information (bit start, end,
9608 word size, etc) encoded within it.). */
9609
9610 decode_complex_addend (&start, &oplen, &len, &wordsz,
9611 &chunksz, &lsb0_p, &signed_p,
9612 &trunc_p, rel->r_addend);
9613
9614 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9615
9616 if (lsb0_p)
9617 shift = (start + 1) - len;
9618 else
9619 shift = (8 * wordsz) - (start + len);
9620
9621 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9622 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9623
9624 #ifdef DEBUG
9625 printf ("Doing complex reloc: "
9626 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9627 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9628 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9629 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9630 oplen, (unsigned long) x, (unsigned long) mask,
9631 (unsigned long) relocation);
9632 #endif
9633
9634 r = bfd_reloc_ok;
9635 if (! trunc_p)
9636 /* Now do an overflow check. */
9637 r = bfd_check_overflow ((signed_p
9638 ? complain_overflow_signed
9639 : complain_overflow_unsigned),
9640 len, 0, (8 * wordsz),
9641 relocation);
9642
9643 /* Do the deed. */
9644 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9645
9646 #ifdef DEBUG
9647 printf (" relocation: %8.8lx\n"
9648 " shifted mask: %8.8lx\n"
9649 " shifted/masked reloc: %8.8lx\n"
9650 " result: %8.8lx\n",
9651 (unsigned long) relocation, (unsigned long) (mask << shift),
9652 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9653 #endif
9654 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9655 return r;
9656 }
9657
9658 /* Functions to read r_offset from external (target order) reloc
9659 entry. Faster than bfd_getl32 et al, because we let the compiler
9660 know the value is aligned. */
9661
9662 static bfd_vma
9663 ext32l_r_offset (const void *p)
9664 {
9665 union aligned32
9666 {
9667 uint32_t v;
9668 unsigned char c[4];
9669 };
9670 const union aligned32 *a
9671 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9672
9673 uint32_t aval = ( (uint32_t) a->c[0]
9674 | (uint32_t) a->c[1] << 8
9675 | (uint32_t) a->c[2] << 16
9676 | (uint32_t) a->c[3] << 24);
9677 return aval;
9678 }
9679
9680 static bfd_vma
9681 ext32b_r_offset (const void *p)
9682 {
9683 union aligned32
9684 {
9685 uint32_t v;
9686 unsigned char c[4];
9687 };
9688 const union aligned32 *a
9689 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9690
9691 uint32_t aval = ( (uint32_t) a->c[0] << 24
9692 | (uint32_t) a->c[1] << 16
9693 | (uint32_t) a->c[2] << 8
9694 | (uint32_t) a->c[3]);
9695 return aval;
9696 }
9697
9698 static bfd_vma
9699 ext64l_r_offset (const void *p)
9700 {
9701 union aligned64
9702 {
9703 uint64_t v;
9704 unsigned char c[8];
9705 };
9706 const union aligned64 *a
9707 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9708
9709 uint64_t aval = ( (uint64_t) a->c[0]
9710 | (uint64_t) a->c[1] << 8
9711 | (uint64_t) a->c[2] << 16
9712 | (uint64_t) a->c[3] << 24
9713 | (uint64_t) a->c[4] << 32
9714 | (uint64_t) a->c[5] << 40
9715 | (uint64_t) a->c[6] << 48
9716 | (uint64_t) a->c[7] << 56);
9717 return aval;
9718 }
9719
9720 static bfd_vma
9721 ext64b_r_offset (const void *p)
9722 {
9723 union aligned64
9724 {
9725 uint64_t v;
9726 unsigned char c[8];
9727 };
9728 const union aligned64 *a
9729 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9730
9731 uint64_t aval = ( (uint64_t) a->c[0] << 56
9732 | (uint64_t) a->c[1] << 48
9733 | (uint64_t) a->c[2] << 40
9734 | (uint64_t) a->c[3] << 32
9735 | (uint64_t) a->c[4] << 24
9736 | (uint64_t) a->c[5] << 16
9737 | (uint64_t) a->c[6] << 8
9738 | (uint64_t) a->c[7]);
9739 return aval;
9740 }
9741
9742 /* When performing a relocatable link, the input relocations are
9743 preserved. But, if they reference global symbols, the indices
9744 referenced must be updated. Update all the relocations found in
9745 RELDATA. */
9746
9747 static bool
9748 elf_link_adjust_relocs (bfd *abfd,
9749 asection *sec,
9750 struct bfd_elf_section_reloc_data *reldata,
9751 bool sort,
9752 struct bfd_link_info *info)
9753 {
9754 unsigned int i;
9755 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9756 bfd_byte *erela;
9757 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9758 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9759 bfd_vma r_type_mask;
9760 int r_sym_shift;
9761 unsigned int count = reldata->count;
9762 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9763
9764 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9765 {
9766 swap_in = bed->s->swap_reloc_in;
9767 swap_out = bed->s->swap_reloc_out;
9768 }
9769 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9770 {
9771 swap_in = bed->s->swap_reloca_in;
9772 swap_out = bed->s->swap_reloca_out;
9773 }
9774 else
9775 abort ();
9776
9777 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9778 abort ();
9779
9780 if (bed->s->arch_size == 32)
9781 {
9782 r_type_mask = 0xff;
9783 r_sym_shift = 8;
9784 }
9785 else
9786 {
9787 r_type_mask = 0xffffffff;
9788 r_sym_shift = 32;
9789 }
9790
9791 erela = reldata->hdr->contents;
9792 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9793 {
9794 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9795 unsigned int j;
9796
9797 if (*rel_hash == NULL)
9798 continue;
9799
9800 if ((*rel_hash)->indx == -2
9801 && info->gc_sections
9802 && ! info->gc_keep_exported)
9803 {
9804 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9805 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9806 abfd, sec,
9807 (*rel_hash)->root.root.string);
9808 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9809 abfd, sec);
9810 bfd_set_error (bfd_error_invalid_operation);
9811 return false;
9812 }
9813 BFD_ASSERT ((*rel_hash)->indx >= 0);
9814
9815 (*swap_in) (abfd, erela, irela);
9816 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9817 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9818 | (irela[j].r_info & r_type_mask));
9819 (*swap_out) (abfd, irela, erela);
9820 }
9821
9822 if (bed->elf_backend_update_relocs)
9823 (*bed->elf_backend_update_relocs) (sec, reldata);
9824
9825 if (sort && count != 0)
9826 {
9827 bfd_vma (*ext_r_off) (const void *);
9828 bfd_vma r_off;
9829 size_t elt_size;
9830 bfd_byte *base, *end, *p, *loc;
9831 bfd_byte *buf = NULL;
9832
9833 if (bed->s->arch_size == 32)
9834 {
9835 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9836 ext_r_off = ext32l_r_offset;
9837 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9838 ext_r_off = ext32b_r_offset;
9839 else
9840 abort ();
9841 }
9842 else
9843 {
9844 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9845 ext_r_off = ext64l_r_offset;
9846 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9847 ext_r_off = ext64b_r_offset;
9848 else
9849 abort ();
9850 }
9851
9852 /* Must use a stable sort here. A modified insertion sort,
9853 since the relocs are mostly sorted already. */
9854 elt_size = reldata->hdr->sh_entsize;
9855 base = reldata->hdr->contents;
9856 end = base + count * elt_size;
9857 if (elt_size > sizeof (Elf64_External_Rela))
9858 abort ();
9859
9860 /* Ensure the first element is lowest. This acts as a sentinel,
9861 speeding the main loop below. */
9862 r_off = (*ext_r_off) (base);
9863 for (p = loc = base; (p += elt_size) < end; )
9864 {
9865 bfd_vma r_off2 = (*ext_r_off) (p);
9866 if (r_off > r_off2)
9867 {
9868 r_off = r_off2;
9869 loc = p;
9870 }
9871 }
9872 if (loc != base)
9873 {
9874 /* Don't just swap *base and *loc as that changes the order
9875 of the original base[0] and base[1] if they happen to
9876 have the same r_offset. */
9877 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9878 memcpy (onebuf, loc, elt_size);
9879 memmove (base + elt_size, base, loc - base);
9880 memcpy (base, onebuf, elt_size);
9881 }
9882
9883 for (p = base + elt_size; (p += elt_size) < end; )
9884 {
9885 /* base to p is sorted, *p is next to insert. */
9886 r_off = (*ext_r_off) (p);
9887 /* Search the sorted region for location to insert. */
9888 loc = p - elt_size;
9889 while (r_off < (*ext_r_off) (loc))
9890 loc -= elt_size;
9891 loc += elt_size;
9892 if (loc != p)
9893 {
9894 /* Chances are there is a run of relocs to insert here,
9895 from one of more input files. Files are not always
9896 linked in order due to the way elf_link_input_bfd is
9897 called. See pr17666. */
9898 size_t sortlen = p - loc;
9899 bfd_vma r_off2 = (*ext_r_off) (loc);
9900 size_t runlen = elt_size;
9901 bfd_vma r_off_runend = r_off;
9902 bfd_vma r_off_runend_next;
9903 size_t buf_size = 96 * 1024;
9904 while (p + runlen < end
9905 && (sortlen <= buf_size
9906 || runlen + elt_size <= buf_size)
9907 /* run must not break the ordering of base..loc+1 */
9908 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9909 /* run must be already sorted */
9910 && r_off_runend_next >= r_off_runend)
9911 {
9912 runlen += elt_size;
9913 r_off_runend = r_off_runend_next;
9914 }
9915 if (buf == NULL)
9916 {
9917 buf = bfd_malloc (buf_size);
9918 if (buf == NULL)
9919 return false;
9920 }
9921 if (runlen < sortlen)
9922 {
9923 memcpy (buf, p, runlen);
9924 memmove (loc + runlen, loc, sortlen);
9925 memcpy (loc, buf, runlen);
9926 }
9927 else
9928 {
9929 memcpy (buf, loc, sortlen);
9930 memmove (loc, p, runlen);
9931 memcpy (loc + runlen, buf, sortlen);
9932 }
9933 p += runlen - elt_size;
9934 }
9935 }
9936 /* Hashes are no longer valid. */
9937 free (reldata->hashes);
9938 reldata->hashes = NULL;
9939 free (buf);
9940 }
9941 return true;
9942 }
9943
9944 struct elf_link_sort_rela
9945 {
9946 union {
9947 bfd_vma offset;
9948 bfd_vma sym_mask;
9949 } u;
9950 enum elf_reloc_type_class type;
9951 /* We use this as an array of size int_rels_per_ext_rel. */
9952 Elf_Internal_Rela rela[1];
9953 };
9954
9955 /* qsort stability here and for cmp2 is only an issue if multiple
9956 dynamic relocations are emitted at the same address. But targets
9957 that apply a series of dynamic relocations each operating on the
9958 result of the prior relocation can't use -z combreloc as
9959 implemented anyway. Such schemes tend to be broken by sorting on
9960 symbol index. That leaves dynamic NONE relocs as the only other
9961 case where ld might emit multiple relocs at the same address, and
9962 those are only emitted due to target bugs. */
9963
9964 static int
9965 elf_link_sort_cmp1 (const void *A, const void *B)
9966 {
9967 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9968 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9969 int relativea, relativeb;
9970
9971 relativea = a->type == reloc_class_relative;
9972 relativeb = b->type == reloc_class_relative;
9973
9974 if (relativea < relativeb)
9975 return 1;
9976 if (relativea > relativeb)
9977 return -1;
9978 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9979 return -1;
9980 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9981 return 1;
9982 if (a->rela->r_offset < b->rela->r_offset)
9983 return -1;
9984 if (a->rela->r_offset > b->rela->r_offset)
9985 return 1;
9986 return 0;
9987 }
9988
9989 static int
9990 elf_link_sort_cmp2 (const void *A, const void *B)
9991 {
9992 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9993 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9994
9995 if (a->type < b->type)
9996 return -1;
9997 if (a->type > b->type)
9998 return 1;
9999 if (a->u.offset < b->u.offset)
10000 return -1;
10001 if (a->u.offset > b->u.offset)
10002 return 1;
10003 if (a->rela->r_offset < b->rela->r_offset)
10004 return -1;
10005 if (a->rela->r_offset > b->rela->r_offset)
10006 return 1;
10007 return 0;
10008 }
10009
10010 static size_t
10011 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
10012 {
10013 asection *dynamic_relocs;
10014 asection *rela_dyn;
10015 asection *rel_dyn;
10016 bfd_size_type count, size;
10017 size_t i, ret, sort_elt, ext_size;
10018 bfd_byte *sort, *s_non_relative, *p;
10019 struct elf_link_sort_rela *sq;
10020 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10021 int i2e = bed->s->int_rels_per_ext_rel;
10022 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
10023 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10024 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10025 struct bfd_link_order *lo;
10026 bfd_vma r_sym_mask;
10027 bool use_rela;
10028
10029 /* Find a dynamic reloc section. */
10030 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
10031 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
10032 if (rela_dyn != NULL && rela_dyn->size > 0
10033 && rel_dyn != NULL && rel_dyn->size > 0)
10034 {
10035 bool use_rela_initialised = false;
10036
10037 /* This is just here to stop gcc from complaining.
10038 Its initialization checking code is not perfect. */
10039 use_rela = true;
10040
10041 /* Both sections are present. Examine the sizes
10042 of the indirect sections to help us choose. */
10043 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10044 if (lo->type == bfd_indirect_link_order)
10045 {
10046 asection *o = lo->u.indirect.section;
10047
10048 if ((o->size % bed->s->sizeof_rela) == 0)
10049 {
10050 if ((o->size % bed->s->sizeof_rel) == 0)
10051 /* Section size is divisible by both rel and rela sizes.
10052 It is of no help to us. */
10053 ;
10054 else
10055 {
10056 /* Section size is only divisible by rela. */
10057 if (use_rela_initialised && !use_rela)
10058 {
10059 _bfd_error_handler (_("%pB: unable to sort relocs - "
10060 "they are in more than one size"),
10061 abfd);
10062 bfd_set_error (bfd_error_invalid_operation);
10063 return 0;
10064 }
10065 else
10066 {
10067 use_rela = true;
10068 use_rela_initialised = true;
10069 }
10070 }
10071 }
10072 else if ((o->size % bed->s->sizeof_rel) == 0)
10073 {
10074 /* Section size is only divisible by rel. */
10075 if (use_rela_initialised && use_rela)
10076 {
10077 _bfd_error_handler (_("%pB: unable to sort relocs - "
10078 "they are in more than one size"),
10079 abfd);
10080 bfd_set_error (bfd_error_invalid_operation);
10081 return 0;
10082 }
10083 else
10084 {
10085 use_rela = false;
10086 use_rela_initialised = true;
10087 }
10088 }
10089 else
10090 {
10091 /* The section size is not divisible by either -
10092 something is wrong. */
10093 _bfd_error_handler (_("%pB: unable to sort relocs - "
10094 "they are of an unknown size"), abfd);
10095 bfd_set_error (bfd_error_invalid_operation);
10096 return 0;
10097 }
10098 }
10099
10100 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10101 if (lo->type == bfd_indirect_link_order)
10102 {
10103 asection *o = lo->u.indirect.section;
10104
10105 if ((o->size % bed->s->sizeof_rela) == 0)
10106 {
10107 if ((o->size % bed->s->sizeof_rel) == 0)
10108 /* Section size is divisible by both rel and rela sizes.
10109 It is of no help to us. */
10110 ;
10111 else
10112 {
10113 /* Section size is only divisible by rela. */
10114 if (use_rela_initialised && !use_rela)
10115 {
10116 _bfd_error_handler (_("%pB: unable to sort relocs - "
10117 "they are in more than one size"),
10118 abfd);
10119 bfd_set_error (bfd_error_invalid_operation);
10120 return 0;
10121 }
10122 else
10123 {
10124 use_rela = true;
10125 use_rela_initialised = true;
10126 }
10127 }
10128 }
10129 else if ((o->size % bed->s->sizeof_rel) == 0)
10130 {
10131 /* Section size is only divisible by rel. */
10132 if (use_rela_initialised && use_rela)
10133 {
10134 _bfd_error_handler (_("%pB: unable to sort relocs - "
10135 "they are in more than one size"),
10136 abfd);
10137 bfd_set_error (bfd_error_invalid_operation);
10138 return 0;
10139 }
10140 else
10141 {
10142 use_rela = false;
10143 use_rela_initialised = true;
10144 }
10145 }
10146 else
10147 {
10148 /* The section size is not divisible by either -
10149 something is wrong. */
10150 _bfd_error_handler (_("%pB: unable to sort relocs - "
10151 "they are of an unknown size"), abfd);
10152 bfd_set_error (bfd_error_invalid_operation);
10153 return 0;
10154 }
10155 }
10156
10157 if (! use_rela_initialised)
10158 /* Make a guess. */
10159 use_rela = true;
10160 }
10161 else if (rela_dyn != NULL && rela_dyn->size > 0)
10162 use_rela = true;
10163 else if (rel_dyn != NULL && rel_dyn->size > 0)
10164 use_rela = false;
10165 else
10166 return 0;
10167
10168 if (use_rela)
10169 {
10170 dynamic_relocs = rela_dyn;
10171 ext_size = bed->s->sizeof_rela;
10172 swap_in = bed->s->swap_reloca_in;
10173 swap_out = bed->s->swap_reloca_out;
10174 }
10175 else
10176 {
10177 dynamic_relocs = rel_dyn;
10178 ext_size = bed->s->sizeof_rel;
10179 swap_in = bed->s->swap_reloc_in;
10180 swap_out = bed->s->swap_reloc_out;
10181 }
10182
10183 size = 0;
10184 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10185 if (lo->type == bfd_indirect_link_order)
10186 size += lo->u.indirect.section->size;
10187
10188 if (size != dynamic_relocs->size)
10189 return 0;
10190
10191 sort_elt = (sizeof (struct elf_link_sort_rela)
10192 + (i2e - 1) * sizeof (Elf_Internal_Rela));
10193
10194 count = dynamic_relocs->size / ext_size;
10195 if (count == 0)
10196 return 0;
10197 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10198
10199 if (sort == NULL)
10200 {
10201 (*info->callbacks->warning)
10202 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10203 return 0;
10204 }
10205
10206 if (bed->s->arch_size == 32)
10207 r_sym_mask = ~(bfd_vma) 0xff;
10208 else
10209 r_sym_mask = ~(bfd_vma) 0xffffffff;
10210
10211 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10212 if (lo->type == bfd_indirect_link_order)
10213 {
10214 bfd_byte *erel, *erelend;
10215 asection *o = lo->u.indirect.section;
10216
10217 if (o->contents == NULL && o->size != 0)
10218 {
10219 /* This is a reloc section that is being handled as a normal
10220 section. See bfd_section_from_shdr. We can't combine
10221 relocs in this case. */
10222 free (sort);
10223 return 0;
10224 }
10225 erel = o->contents;
10226 erelend = o->contents + o->size;
10227 p = sort + o->output_offset * opb / ext_size * sort_elt;
10228
10229 while (erel < erelend)
10230 {
10231 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10232
10233 (*swap_in) (abfd, erel, s->rela);
10234 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10235 s->u.sym_mask = r_sym_mask;
10236 p += sort_elt;
10237 erel += ext_size;
10238 }
10239 }
10240
10241 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10242
10243 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10244 {
10245 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10246 if (s->type != reloc_class_relative)
10247 break;
10248 }
10249 ret = i;
10250 s_non_relative = p;
10251
10252 sq = (struct elf_link_sort_rela *) s_non_relative;
10253 for (; i < count; i++, p += sort_elt)
10254 {
10255 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10256 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10257 sq = sp;
10258 sp->u.offset = sq->rela->r_offset;
10259 }
10260
10261 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10262
10263 struct elf_link_hash_table *htab = elf_hash_table (info);
10264 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10265 {
10266 /* We have plt relocs in .rela.dyn. */
10267 sq = (struct elf_link_sort_rela *) sort;
10268 for (i = 0; i < count; i++)
10269 if (sq[count - i - 1].type != reloc_class_plt)
10270 break;
10271 if (i != 0 && htab->srelplt->size == i * ext_size)
10272 {
10273 struct bfd_link_order **plo;
10274 /* Put srelplt link_order last. This is so the output_offset
10275 set in the next loop is correct for DT_JMPREL. */
10276 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10277 if ((*plo)->type == bfd_indirect_link_order
10278 && (*plo)->u.indirect.section == htab->srelplt)
10279 {
10280 lo = *plo;
10281 *plo = lo->next;
10282 }
10283 else
10284 plo = &(*plo)->next;
10285 *plo = lo;
10286 lo->next = NULL;
10287 dynamic_relocs->map_tail.link_order = lo;
10288 }
10289 }
10290
10291 p = sort;
10292 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10293 if (lo->type == bfd_indirect_link_order)
10294 {
10295 bfd_byte *erel, *erelend;
10296 asection *o = lo->u.indirect.section;
10297
10298 erel = o->contents;
10299 erelend = o->contents + o->size;
10300 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10301 while (erel < erelend)
10302 {
10303 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10304 (*swap_out) (abfd, s->rela, erel);
10305 p += sort_elt;
10306 erel += ext_size;
10307 }
10308 }
10309
10310 free (sort);
10311 *psec = dynamic_relocs;
10312 return ret;
10313 }
10314
10315 /* Add a symbol to the output symbol string table. */
10316
10317 static int
10318 elf_link_output_symstrtab (void *finf,
10319 const char *name,
10320 Elf_Internal_Sym *elfsym,
10321 asection *input_sec,
10322 struct elf_link_hash_entry *h)
10323 {
10324 struct elf_final_link_info *flinfo = finf;
10325 int (*output_symbol_hook)
10326 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10327 struct elf_link_hash_entry *);
10328 struct elf_link_hash_table *hash_table;
10329 const struct elf_backend_data *bed;
10330 bfd_size_type strtabsize;
10331
10332 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10333
10334 bed = get_elf_backend_data (flinfo->output_bfd);
10335 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10336 if (output_symbol_hook != NULL)
10337 {
10338 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10339 if (ret != 1)
10340 return ret;
10341 }
10342
10343 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10344 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10345 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10346 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10347
10348 if (name == NULL || *name == '\0')
10349 elfsym->st_name = (unsigned long) -1;
10350 else
10351 {
10352 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10353 to get the final offset for st_name. */
10354 char *versioned_name = (char *) name;
10355 if (h != NULL)
10356 {
10357 if (h->versioned == versioned && h->def_dynamic)
10358 {
10359 /* Keep only one '@' for versioned symbols defined in
10360 shared objects. */
10361 char *version = strrchr (name, ELF_VER_CHR);
10362 char *base_end = strchr (name, ELF_VER_CHR);
10363 if (version != base_end)
10364 {
10365 size_t base_len;
10366 size_t len = strlen (name);
10367 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10368 if (versioned_name == NULL)
10369 return 0;
10370 base_len = base_end - name;
10371 memcpy (versioned_name, name, base_len);
10372 memcpy (versioned_name + base_len, version,
10373 len - base_len);
10374 }
10375 }
10376 }
10377 else if (flinfo->info->unique_symbol
10378 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10379 {
10380 struct local_hash_entry *lh;
10381 size_t count_len;
10382 size_t base_len;
10383 char buf[30];
10384 switch (ELF_ST_TYPE (elfsym->st_info))
10385 {
10386 case STT_FILE:
10387 case STT_SECTION:
10388 break;
10389 default:
10390 lh = (struct local_hash_entry *) bfd_hash_lookup
10391 (&flinfo->local_hash_table, name, true, false);
10392 if (lh == NULL)
10393 return 0;
10394 /* Always append ".COUNT" to local symbols to avoid
10395 potential conflicts with local symbol "XXX.COUNT". */
10396 sprintf (buf, "%lx", lh->count);
10397 base_len = lh->size;
10398 if (!base_len)
10399 {
10400 base_len = strlen (name);
10401 lh->size = base_len;
10402 }
10403 count_len = strlen (buf);
10404 versioned_name = bfd_alloc (flinfo->output_bfd,
10405 base_len + count_len + 2);
10406 if (versioned_name == NULL)
10407 return 0;
10408 memcpy (versioned_name, name, base_len);
10409 versioned_name[base_len] = '.';
10410 memcpy (versioned_name + base_len + 1, buf,
10411 count_len + 1);
10412 lh->count++;
10413 break;
10414 }
10415 }
10416 elfsym->st_name
10417 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10418 versioned_name, false);
10419 if (elfsym->st_name == (unsigned long) -1)
10420 return 0;
10421 }
10422
10423 hash_table = elf_hash_table (flinfo->info);
10424 strtabsize = hash_table->strtabsize;
10425 if (strtabsize <= flinfo->output_bfd->symcount)
10426 {
10427 strtabsize += strtabsize;
10428 hash_table->strtabsize = strtabsize;
10429 strtabsize *= sizeof (*hash_table->strtab);
10430 hash_table->strtab
10431 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10432 strtabsize);
10433 if (hash_table->strtab == NULL)
10434 return 0;
10435 }
10436 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10437 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10438 = flinfo->output_bfd->symcount;
10439 flinfo->output_bfd->symcount += 1;
10440
10441 return 1;
10442 }
10443
10444 /* Swap symbols out to the symbol table and flush the output symbols to
10445 the file. */
10446
10447 static bool
10448 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10449 {
10450 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10451 size_t amt;
10452 size_t i;
10453 const struct elf_backend_data *bed;
10454 bfd_byte *symbuf;
10455 Elf_Internal_Shdr *hdr;
10456 file_ptr pos;
10457 bool ret;
10458
10459 if (flinfo->output_bfd->symcount == 0)
10460 return true;
10461
10462 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10463
10464 bed = get_elf_backend_data (flinfo->output_bfd);
10465
10466 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10467 symbuf = (bfd_byte *) bfd_malloc (amt);
10468 if (symbuf == NULL)
10469 return false;
10470
10471 if (flinfo->symshndxbuf)
10472 {
10473 amt = sizeof (Elf_External_Sym_Shndx);
10474 amt *= bfd_get_symcount (flinfo->output_bfd);
10475 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10476 if (flinfo->symshndxbuf == NULL)
10477 {
10478 free (symbuf);
10479 return false;
10480 }
10481 }
10482
10483 /* Now swap out the symbols. */
10484 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10485 {
10486 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10487 if (elfsym->sym.st_name == (unsigned long) -1)
10488 elfsym->sym.st_name = 0;
10489 else
10490 elfsym->sym.st_name
10491 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10492 elfsym->sym.st_name);
10493
10494 /* Inform the linker of the addition of this symbol. */
10495
10496 if (flinfo->info->callbacks->ctf_new_symbol)
10497 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10498 &elfsym->sym);
10499
10500 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10501 ((bfd_byte *) symbuf
10502 + (elfsym->dest_index
10503 * bed->s->sizeof_sym)),
10504 NPTR_ADD (flinfo->symshndxbuf,
10505 elfsym->dest_index));
10506 }
10507
10508 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10509 pos = hdr->sh_offset + hdr->sh_size;
10510 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10511 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10512 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10513 {
10514 hdr->sh_size += amt;
10515 ret = true;
10516 }
10517 else
10518 ret = false;
10519
10520 free (symbuf);
10521 return ret;
10522 }
10523
10524 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10525
10526 static bool
10527 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10528 {
10529 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10530 && sym->st_shndx < SHN_LORESERVE)
10531 {
10532 /* The gABI doesn't support dynamic symbols in output sections
10533 beyond 64k. */
10534 _bfd_error_handler
10535 /* xgettext:c-format */
10536 (_("%pB: too many sections: %d (>= %d)"),
10537 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10538 bfd_set_error (bfd_error_nonrepresentable_section);
10539 return false;
10540 }
10541 return true;
10542 }
10543
10544 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10545 allowing an unsatisfied unversioned symbol in the DSO to match a
10546 versioned symbol that would normally require an explicit version.
10547 We also handle the case that a DSO references a hidden symbol
10548 which may be satisfied by a versioned symbol in another DSO. */
10549
10550 static bool
10551 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10552 const struct elf_backend_data *bed,
10553 struct elf_link_hash_entry *h)
10554 {
10555 bfd *abfd;
10556 struct elf_link_loaded_list *loaded;
10557
10558 if (!is_elf_hash_table (info->hash))
10559 return false;
10560
10561 /* Check indirect symbol. */
10562 while (h->root.type == bfd_link_hash_indirect)
10563 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10564
10565 switch (h->root.type)
10566 {
10567 default:
10568 abfd = NULL;
10569 break;
10570
10571 case bfd_link_hash_undefined:
10572 case bfd_link_hash_undefweak:
10573 abfd = h->root.u.undef.abfd;
10574 if (abfd == NULL
10575 || (abfd->flags & DYNAMIC) == 0
10576 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10577 return false;
10578 break;
10579
10580 case bfd_link_hash_defined:
10581 case bfd_link_hash_defweak:
10582 abfd = h->root.u.def.section->owner;
10583 break;
10584
10585 case bfd_link_hash_common:
10586 abfd = h->root.u.c.p->section->owner;
10587 break;
10588 }
10589 BFD_ASSERT (abfd != NULL);
10590
10591 for (loaded = elf_hash_table (info)->dyn_loaded;
10592 loaded != NULL;
10593 loaded = loaded->next)
10594 {
10595 bfd *input;
10596 Elf_Internal_Shdr *hdr;
10597 size_t symcount;
10598 size_t extsymcount;
10599 size_t extsymoff;
10600 Elf_Internal_Shdr *versymhdr;
10601 Elf_Internal_Sym *isym;
10602 Elf_Internal_Sym *isymend;
10603 Elf_Internal_Sym *isymbuf;
10604 Elf_External_Versym *ever;
10605 Elf_External_Versym *extversym;
10606
10607 input = loaded->abfd;
10608
10609 /* We check each DSO for a possible hidden versioned definition. */
10610 if (input == abfd
10611 || elf_dynversym (input) == 0)
10612 continue;
10613
10614 hdr = &elf_tdata (input)->dynsymtab_hdr;
10615
10616 symcount = hdr->sh_size / bed->s->sizeof_sym;
10617 if (elf_bad_symtab (input))
10618 {
10619 extsymcount = symcount;
10620 extsymoff = 0;
10621 }
10622 else
10623 {
10624 extsymcount = symcount - hdr->sh_info;
10625 extsymoff = hdr->sh_info;
10626 }
10627
10628 if (extsymcount == 0)
10629 continue;
10630
10631 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10632 NULL, NULL, NULL);
10633 if (isymbuf == NULL)
10634 return false;
10635
10636 /* Read in any version definitions. */
10637 versymhdr = &elf_tdata (input)->dynversym_hdr;
10638 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10639 || (extversym = (Elf_External_Versym *)
10640 _bfd_malloc_and_read (input, versymhdr->sh_size,
10641 versymhdr->sh_size)) == NULL)
10642 {
10643 free (isymbuf);
10644 return false;
10645 }
10646
10647 ever = extversym + extsymoff;
10648 isymend = isymbuf + extsymcount;
10649 for (isym = isymbuf; isym < isymend; isym++, ever++)
10650 {
10651 const char *name;
10652 Elf_Internal_Versym iver;
10653 unsigned short version_index;
10654
10655 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10656 || isym->st_shndx == SHN_UNDEF)
10657 continue;
10658
10659 name = bfd_elf_string_from_elf_section (input,
10660 hdr->sh_link,
10661 isym->st_name);
10662 if (strcmp (name, h->root.root.string) != 0)
10663 continue;
10664
10665 _bfd_elf_swap_versym_in (input, ever, &iver);
10666
10667 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10668 && !(h->def_regular
10669 && h->forced_local))
10670 {
10671 /* If we have a non-hidden versioned sym, then it should
10672 have provided a definition for the undefined sym unless
10673 it is defined in a non-shared object and forced local.
10674 */
10675 abort ();
10676 }
10677
10678 version_index = iver.vs_vers & VERSYM_VERSION;
10679 if (version_index == 1 || version_index == 2)
10680 {
10681 /* This is the base or first version. We can use it. */
10682 free (extversym);
10683 free (isymbuf);
10684 return true;
10685 }
10686 }
10687
10688 free (extversym);
10689 free (isymbuf);
10690 }
10691
10692 return false;
10693 }
10694
10695 /* Convert ELF common symbol TYPE. */
10696
10697 static int
10698 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10699 {
10700 /* Commom symbol can only appear in relocatable link. */
10701 if (!bfd_link_relocatable (info))
10702 abort ();
10703 switch (info->elf_stt_common)
10704 {
10705 case unchanged:
10706 break;
10707 case elf_stt_common:
10708 type = STT_COMMON;
10709 break;
10710 case no_elf_stt_common:
10711 type = STT_OBJECT;
10712 break;
10713 }
10714 return type;
10715 }
10716
10717 /* Add an external symbol to the symbol table. This is called from
10718 the hash table traversal routine. When generating a shared object,
10719 we go through the symbol table twice. The first time we output
10720 anything that might have been forced to local scope in a version
10721 script. The second time we output the symbols that are still
10722 global symbols. */
10723
10724 static bool
10725 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10726 {
10727 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10728 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10729 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10730 bool strip;
10731 Elf_Internal_Sym sym;
10732 asection *input_sec;
10733 const struct elf_backend_data *bed;
10734 long indx;
10735 int ret;
10736 unsigned int type;
10737
10738 if (h->root.type == bfd_link_hash_warning)
10739 {
10740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10741 if (h->root.type == bfd_link_hash_new)
10742 return true;
10743 }
10744
10745 /* Decide whether to output this symbol in this pass. */
10746 if (eoinfo->localsyms)
10747 {
10748 if (!h->forced_local)
10749 return true;
10750 }
10751 else
10752 {
10753 if (h->forced_local)
10754 return true;
10755 }
10756
10757 bed = get_elf_backend_data (flinfo->output_bfd);
10758
10759 if (h->root.type == bfd_link_hash_undefined)
10760 {
10761 /* If we have an undefined symbol reference here then it must have
10762 come from a shared library that is being linked in. (Undefined
10763 references in regular files have already been handled unless
10764 they are in unreferenced sections which are removed by garbage
10765 collection). */
10766 bool ignore_undef = false;
10767
10768 /* Some symbols may be special in that the fact that they're
10769 undefined can be safely ignored - let backend determine that. */
10770 if (bed->elf_backend_ignore_undef_symbol)
10771 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10772
10773 /* If we are reporting errors for this situation then do so now. */
10774 if (!ignore_undef
10775 && h->ref_dynamic_nonweak
10776 && (!h->ref_regular || flinfo->info->gc_sections)
10777 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10778 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10779 {
10780 flinfo->info->callbacks->undefined_symbol
10781 (flinfo->info, h->root.root.string,
10782 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10783 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10784 && !flinfo->info->warn_unresolved_syms);
10785 }
10786
10787 /* Strip a global symbol defined in a discarded section. */
10788 if (h->indx == -3)
10789 return true;
10790 }
10791
10792 /* We should also warn if a forced local symbol is referenced from
10793 shared libraries. */
10794 if (bfd_link_executable (flinfo->info)
10795 && h->forced_local
10796 && h->ref_dynamic
10797 && h->def_regular
10798 && !h->dynamic_def
10799 && h->ref_dynamic_nonweak
10800 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10801 {
10802 bfd *def_bfd;
10803 const char *msg;
10804 struct elf_link_hash_entry *hi = h;
10805
10806 /* Check indirect symbol. */
10807 while (hi->root.type == bfd_link_hash_indirect)
10808 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10809
10810 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10811 /* xgettext:c-format */
10812 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10813 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10814 /* xgettext:c-format */
10815 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10816 else
10817 /* xgettext:c-format */
10818 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10819 def_bfd = flinfo->output_bfd;
10820 if (hi->root.u.def.section != bfd_abs_section_ptr)
10821 def_bfd = hi->root.u.def.section->owner;
10822 _bfd_error_handler (msg, flinfo->output_bfd,
10823 h->root.root.string, def_bfd);
10824 bfd_set_error (bfd_error_bad_value);
10825 eoinfo->failed = true;
10826 return false;
10827 }
10828
10829 /* We don't want to output symbols that have never been mentioned by
10830 a regular file, or that we have been told to strip. However, if
10831 h->indx is set to -2, the symbol is used by a reloc and we must
10832 output it. */
10833 strip = false;
10834 if (h->indx == -2)
10835 ;
10836 else if ((h->def_dynamic
10837 || h->ref_dynamic
10838 || h->root.type == bfd_link_hash_new)
10839 && !h->def_regular
10840 && !h->ref_regular)
10841 strip = true;
10842 else if (flinfo->info->strip == strip_all)
10843 strip = true;
10844 else if (flinfo->info->strip == strip_some
10845 && bfd_hash_lookup (flinfo->info->keep_hash,
10846 h->root.root.string, false, false) == NULL)
10847 strip = true;
10848 else if ((h->root.type == bfd_link_hash_defined
10849 || h->root.type == bfd_link_hash_defweak)
10850 && ((flinfo->info->strip_discarded
10851 && discarded_section (h->root.u.def.section))
10852 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10853 && h->root.u.def.section->owner != NULL
10854 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10855 strip = true;
10856 else if ((h->root.type == bfd_link_hash_undefined
10857 || h->root.type == bfd_link_hash_undefweak)
10858 && h->root.u.undef.abfd != NULL
10859 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10860 strip = true;
10861
10862 /* Remember if this symbol should be stripped. */
10863 bool should_strip = strip;
10864
10865 /* Strip undefined weak symbols link if they don't have relocation. */
10866 if (!strip)
10867 strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10868
10869 type = h->type;
10870
10871 /* If we're stripping it, and it's not a dynamic symbol, there's
10872 nothing else to do. However, if it is a forced local symbol or
10873 an ifunc symbol we need to give the backend finish_dynamic_symbol
10874 function a chance to make it dynamic. */
10875 if (strip
10876 && h->dynindx == -1
10877 && type != STT_GNU_IFUNC
10878 && !h->forced_local)
10879 return true;
10880
10881 sym.st_value = 0;
10882 sym.st_size = h->size;
10883 sym.st_other = h->other;
10884 switch (h->root.type)
10885 {
10886 default:
10887 case bfd_link_hash_new:
10888 case bfd_link_hash_warning:
10889 abort ();
10890 return false;
10891
10892 case bfd_link_hash_undefined:
10893 case bfd_link_hash_undefweak:
10894 input_sec = bfd_und_section_ptr;
10895 sym.st_shndx = SHN_UNDEF;
10896 break;
10897
10898 case bfd_link_hash_defined:
10899 case bfd_link_hash_defweak:
10900 {
10901 input_sec = h->root.u.def.section;
10902 if (input_sec->output_section != NULL)
10903 {
10904 sym.st_shndx =
10905 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10906 input_sec->output_section);
10907 if (sym.st_shndx == SHN_BAD)
10908 {
10909 _bfd_error_handler
10910 /* xgettext:c-format */
10911 (_("%pB: could not find output section %pA for input section %pA"),
10912 flinfo->output_bfd, input_sec->output_section, input_sec);
10913 bfd_set_error (bfd_error_nonrepresentable_section);
10914 eoinfo->failed = true;
10915 return false;
10916 }
10917
10918 /* ELF symbols in relocatable files are section relative,
10919 but in nonrelocatable files they are virtual
10920 addresses. */
10921 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10922 if (!bfd_link_relocatable (flinfo->info))
10923 {
10924 sym.st_value += input_sec->output_section->vma;
10925 if (h->type == STT_TLS)
10926 {
10927 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10928 if (tls_sec != NULL)
10929 sym.st_value -= tls_sec->vma;
10930 }
10931 }
10932 }
10933 else
10934 {
10935 BFD_ASSERT (input_sec->owner == NULL
10936 || (input_sec->owner->flags & DYNAMIC) != 0);
10937 sym.st_shndx = SHN_UNDEF;
10938 input_sec = bfd_und_section_ptr;
10939 }
10940 }
10941 break;
10942
10943 case bfd_link_hash_common:
10944 input_sec = h->root.u.c.p->section;
10945 sym.st_shndx = bed->common_section_index (input_sec);
10946 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10947 break;
10948
10949 case bfd_link_hash_indirect:
10950 /* These symbols are created by symbol versioning. They point
10951 to the decorated version of the name. For example, if the
10952 symbol foo@@GNU_1.2 is the default, which should be used when
10953 foo is used with no version, then we add an indirect symbol
10954 foo which points to foo@@GNU_1.2. We ignore these symbols,
10955 since the indirected symbol is already in the hash table. */
10956 return true;
10957 }
10958
10959 if (type == STT_COMMON || type == STT_OBJECT)
10960 switch (h->root.type)
10961 {
10962 case bfd_link_hash_common:
10963 type = elf_link_convert_common_type (flinfo->info, type);
10964 break;
10965 case bfd_link_hash_defined:
10966 case bfd_link_hash_defweak:
10967 if (bed->common_definition (&sym))
10968 type = elf_link_convert_common_type (flinfo->info, type);
10969 else
10970 type = STT_OBJECT;
10971 break;
10972 case bfd_link_hash_undefined:
10973 case bfd_link_hash_undefweak:
10974 break;
10975 default:
10976 abort ();
10977 }
10978
10979 if (h->forced_local)
10980 {
10981 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10982 /* Turn off visibility on local symbol. */
10983 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10984 }
10985 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10986 else if (h->unique_global && h->def_regular)
10987 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10988 else if (h->root.type == bfd_link_hash_undefweak
10989 || h->root.type == bfd_link_hash_defweak)
10990 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10991 else
10992 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10993 sym.st_target_internal = h->target_internal;
10994
10995 /* Give the processor backend a chance to tweak the symbol value,
10996 and also to finish up anything that needs to be done for this
10997 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10998 forced local syms when non-shared is due to a historical quirk.
10999 STT_GNU_IFUNC symbol must go through PLT. */
11000 if ((h->type == STT_GNU_IFUNC
11001 && h->def_regular
11002 && !bfd_link_relocatable (flinfo->info))
11003 || ((h->dynindx != -1
11004 || h->forced_local)
11005 && ((bfd_link_pic (flinfo->info)
11006 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11007 || h->root.type != bfd_link_hash_undefweak))
11008 || !h->forced_local)
11009 && elf_hash_table (flinfo->info)->dynamic_sections_created))
11010 {
11011 if (! ((*bed->elf_backend_finish_dynamic_symbol)
11012 (flinfo->output_bfd, flinfo->info, h, &sym)))
11013 {
11014 eoinfo->failed = true;
11015 return false;
11016 }
11017 /* If a symbol is in the dynamic symbol table and isn't a
11018 should-strip symbol, also keep it in the symbol table. */
11019 if (!should_strip)
11020 strip = false;
11021 }
11022
11023 /* If we are marking the symbol as undefined, and there are no
11024 non-weak references to this symbol from a regular object, then
11025 mark the symbol as weak undefined; if there are non-weak
11026 references, mark the symbol as strong. We can't do this earlier,
11027 because it might not be marked as undefined until the
11028 finish_dynamic_symbol routine gets through with it. */
11029 if (sym.st_shndx == SHN_UNDEF
11030 && h->ref_regular
11031 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11032 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11033 {
11034 int bindtype;
11035 type = ELF_ST_TYPE (sym.st_info);
11036
11037 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11038 if (type == STT_GNU_IFUNC)
11039 type = STT_FUNC;
11040
11041 if (h->ref_regular_nonweak)
11042 bindtype = STB_GLOBAL;
11043 else
11044 bindtype = STB_WEAK;
11045 sym.st_info = ELF_ST_INFO (bindtype, type);
11046 }
11047
11048 /* If this is a symbol defined in a dynamic library, don't use the
11049 symbol size from the dynamic library. Relinking an executable
11050 against a new library may introduce gratuitous changes in the
11051 executable's symbols if we keep the size. */
11052 if (sym.st_shndx == SHN_UNDEF
11053 && !h->def_regular
11054 && h->def_dynamic)
11055 sym.st_size = 0;
11056
11057 /* If a non-weak symbol with non-default visibility is not defined
11058 locally, it is a fatal error. */
11059 if (!bfd_link_relocatable (flinfo->info)
11060 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11061 && ELF_ST_BIND (sym.st_info) != STB_WEAK
11062 && h->root.type == bfd_link_hash_undefined
11063 && !h->def_regular)
11064 {
11065 const char *msg;
11066
11067 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
11068 /* xgettext:c-format */
11069 msg = _("%pB: protected symbol `%s' isn't defined");
11070 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
11071 /* xgettext:c-format */
11072 msg = _("%pB: internal symbol `%s' isn't defined");
11073 else
11074 /* xgettext:c-format */
11075 msg = _("%pB: hidden symbol `%s' isn't defined");
11076 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
11077 bfd_set_error (bfd_error_bad_value);
11078 eoinfo->failed = true;
11079 return false;
11080 }
11081
11082 /* If this symbol should be put in the .dynsym section, then put it
11083 there now. We already know the symbol index. We also fill in
11084 the entry in the .hash section. */
11085 if (h->dynindx != -1
11086 && elf_hash_table (flinfo->info)->dynamic_sections_created
11087 && elf_hash_table (flinfo->info)->dynsym != NULL
11088 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
11089 {
11090 bfd_byte *esym;
11091
11092 /* Since there is no version information in the dynamic string,
11093 if there is no version info in symbol version section, we will
11094 have a run-time problem if not linking executable, referenced
11095 by shared library, or not bound locally. */
11096 if (h->verinfo.verdef == NULL
11097 && (!bfd_link_executable (flinfo->info)
11098 || h->ref_dynamic
11099 || !h->def_regular))
11100 {
11101 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11102
11103 if (p && p [1] != '\0')
11104 {
11105 _bfd_error_handler
11106 /* xgettext:c-format */
11107 (_("%pB: no symbol version section for versioned symbol `%s'"),
11108 flinfo->output_bfd, h->root.root.string);
11109 eoinfo->failed = true;
11110 return false;
11111 }
11112 }
11113
11114 sym.st_name = h->dynstr_index;
11115 esym = (elf_hash_table (flinfo->info)->dynsym->contents
11116 + h->dynindx * bed->s->sizeof_sym);
11117 if (!check_dynsym (flinfo->output_bfd, &sym))
11118 {
11119 eoinfo->failed = true;
11120 return false;
11121 }
11122
11123 /* Inform the linker of the addition of this symbol. */
11124
11125 if (flinfo->info->callbacks->ctf_new_dynsym)
11126 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11127
11128 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11129
11130 if (flinfo->hash_sec != NULL)
11131 {
11132 size_t hash_entry_size;
11133 bfd_byte *bucketpos;
11134 bfd_vma chain;
11135 size_t bucketcount;
11136 size_t bucket;
11137
11138 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11139 bucket = h->u.elf_hash_value % bucketcount;
11140
11141 hash_entry_size
11142 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11143 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11144 + (bucket + 2) * hash_entry_size);
11145 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11146 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11147 bucketpos);
11148 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11149 ((bfd_byte *) flinfo->hash_sec->contents
11150 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11151 }
11152
11153 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11154 {
11155 Elf_Internal_Versym iversym;
11156 Elf_External_Versym *eversym;
11157
11158 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11159 {
11160 if (h->verinfo.verdef == NULL
11161 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11162 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11163 iversym.vs_vers = 1;
11164 else
11165 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11166 }
11167 else
11168 {
11169 if (h->verinfo.vertree == NULL)
11170 iversym.vs_vers = 1;
11171 else
11172 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11173 if (flinfo->info->create_default_symver)
11174 iversym.vs_vers++;
11175 }
11176
11177 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11178 defined locally. */
11179 if (h->versioned == versioned_hidden && h->def_regular)
11180 iversym.vs_vers |= VERSYM_HIDDEN;
11181
11182 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11183 eversym += h->dynindx;
11184 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11185 }
11186 }
11187
11188 /* If the symbol is undefined, and we didn't output it to .dynsym,
11189 strip it from .symtab too. Obviously we can't do this for
11190 relocatable output or when needed for --emit-relocs. */
11191 else if (input_sec == bfd_und_section_ptr
11192 && h->indx != -2
11193 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11194 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11195 && !bfd_link_relocatable (flinfo->info))
11196 return true;
11197
11198 /* Also strip others that we couldn't earlier due to dynamic symbol
11199 processing. */
11200 if (strip)
11201 return true;
11202 if ((input_sec->flags & SEC_EXCLUDE) != 0)
11203 return true;
11204
11205 /* Output a FILE symbol so that following locals are not associated
11206 with the wrong input file. We need one for forced local symbols
11207 if we've seen more than one FILE symbol or when we have exactly
11208 one FILE symbol but global symbols are present in a file other
11209 than the one with the FILE symbol. We also need one if linker
11210 defined symbols are present. In practice these conditions are
11211 always met, so just emit the FILE symbol unconditionally. */
11212 if (eoinfo->localsyms
11213 && !eoinfo->file_sym_done
11214 && eoinfo->flinfo->filesym_count != 0)
11215 {
11216 Elf_Internal_Sym fsym;
11217
11218 memset (&fsym, 0, sizeof (fsym));
11219 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11220 fsym.st_shndx = SHN_ABS;
11221 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11222 bfd_und_section_ptr, NULL))
11223 return false;
11224
11225 eoinfo->file_sym_done = true;
11226 }
11227
11228 indx = bfd_get_symcount (flinfo->output_bfd);
11229 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11230 input_sec, h);
11231 if (ret == 0)
11232 {
11233 eoinfo->failed = true;
11234 return false;
11235 }
11236 else if (ret == 1)
11237 h->indx = indx;
11238 else if (h->indx == -2)
11239 abort();
11240
11241 return true;
11242 }
11243
11244 /* Return TRUE if special handling is done for relocs in SEC against
11245 symbols defined in discarded sections. */
11246
11247 static bool
11248 elf_section_ignore_discarded_relocs (asection *sec)
11249 {
11250 const struct elf_backend_data *bed;
11251
11252 switch (sec->sec_info_type)
11253 {
11254 case SEC_INFO_TYPE_STABS:
11255 case SEC_INFO_TYPE_EH_FRAME:
11256 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11257 case SEC_INFO_TYPE_SFRAME:
11258 return true;
11259 default:
11260 break;
11261 }
11262
11263 bed = get_elf_backend_data (sec->owner);
11264 if (bed->elf_backend_ignore_discarded_relocs != NULL
11265 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11266 return true;
11267
11268 return false;
11269 }
11270
11271 /* Return a mask saying how ld should treat relocations in SEC against
11272 symbols defined in discarded sections. If this function returns
11273 COMPLAIN set, ld will issue a warning message. If this function
11274 returns PRETEND set, and the discarded section was link-once and the
11275 same size as the kept link-once section, ld will pretend that the
11276 symbol was actually defined in the kept section. Otherwise ld will
11277 zero the reloc (at least that is the intent, but some cooperation by
11278 the target dependent code is needed, particularly for REL targets). */
11279
11280 unsigned int
11281 _bfd_elf_default_action_discarded (asection *sec)
11282 {
11283 const struct elf_backend_data *bed;
11284 bed = get_elf_backend_data (sec->owner);
11285
11286 if (sec->flags & SEC_DEBUGGING)
11287 return PRETEND;
11288
11289 if (strcmp (".eh_frame", sec->name) == 0)
11290 return 0;
11291
11292 if (bed->elf_backend_can_make_multiple_eh_frame
11293 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11294 return 0;
11295
11296 if (strcmp (".sframe", sec->name) == 0)
11297 return 0;
11298
11299 if (strcmp (".gcc_except_table", sec->name) == 0)
11300 return 0;
11301
11302 return COMPLAIN | PRETEND;
11303 }
11304
11305 /* Find a match between a section and a member of a section group. */
11306
11307 static asection *
11308 match_group_member (asection *sec, asection *group,
11309 struct bfd_link_info *info)
11310 {
11311 asection *first = elf_next_in_group (group);
11312 asection *s = first;
11313
11314 while (s != NULL)
11315 {
11316 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11317 return s;
11318
11319 s = elf_next_in_group (s);
11320 if (s == first)
11321 break;
11322 }
11323
11324 return NULL;
11325 }
11326
11327 /* Check if the kept section of a discarded section SEC can be used
11328 to replace it. Return the replacement if it is OK. Otherwise return
11329 NULL. */
11330
11331 asection *
11332 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11333 {
11334 asection *kept;
11335
11336 kept = sec->kept_section;
11337 if (kept != NULL)
11338 {
11339 if ((kept->flags & SEC_GROUP) != 0)
11340 kept = match_group_member (sec, kept, info);
11341 if (kept != NULL)
11342 {
11343 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11344 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11345 kept = NULL;
11346 else
11347 {
11348 /* Get the real kept section. */
11349 asection *next;
11350 for (next = kept->kept_section;
11351 next != NULL;
11352 next = next->kept_section)
11353 kept = next;
11354 }
11355 }
11356 sec->kept_section = kept;
11357 }
11358 return kept;
11359 }
11360
11361 /* Link an input file into the linker output file. This function
11362 handles all the sections and relocations of the input file at once.
11363 This is so that we only have to read the local symbols once, and
11364 don't have to keep them in memory. */
11365
11366 static bool
11367 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11368 {
11369 int (*relocate_section)
11370 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11371 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11372 bfd *output_bfd;
11373 Elf_Internal_Shdr *symtab_hdr;
11374 size_t locsymcount;
11375 size_t extsymoff;
11376 Elf_Internal_Sym *isymbuf;
11377 Elf_Internal_Sym *isym;
11378 Elf_Internal_Sym *isymend;
11379 long *pindex;
11380 asection **ppsection;
11381 asection *o;
11382 const struct elf_backend_data *bed;
11383 struct elf_link_hash_entry **sym_hashes;
11384 bfd_size_type address_size;
11385 bfd_vma r_type_mask;
11386 int r_sym_shift;
11387 bool have_file_sym = false;
11388
11389 output_bfd = flinfo->output_bfd;
11390 bed = get_elf_backend_data (output_bfd);
11391 relocate_section = bed->elf_backend_relocate_section;
11392
11393 /* If this is a dynamic object, we don't want to do anything here:
11394 we don't want the local symbols, and we don't want the section
11395 contents. */
11396 if ((input_bfd->flags & DYNAMIC) != 0)
11397 return true;
11398
11399 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11400 if (elf_bad_symtab (input_bfd))
11401 {
11402 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11403 extsymoff = 0;
11404 }
11405 else
11406 {
11407 locsymcount = symtab_hdr->sh_info;
11408 extsymoff = symtab_hdr->sh_info;
11409 }
11410
11411 /* Enable GNU OSABI features in the output BFD that are used in the input
11412 BFD. */
11413 if (bed->elf_osabi == ELFOSABI_NONE
11414 || bed->elf_osabi == ELFOSABI_GNU
11415 || bed->elf_osabi == ELFOSABI_FREEBSD)
11416 elf_tdata (output_bfd)->has_gnu_osabi
11417 |= (elf_tdata (input_bfd)->has_gnu_osabi
11418 & (bfd_link_relocatable (flinfo->info)
11419 ? -1 : ~elf_gnu_osabi_retain));
11420
11421 /* Read the local symbols. */
11422 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11423 if (isymbuf == NULL && locsymcount != 0)
11424 {
11425 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11426 flinfo->internal_syms,
11427 flinfo->external_syms,
11428 flinfo->locsym_shndx);
11429 if (isymbuf == NULL)
11430 return false;
11431 }
11432
11433 /* Find local symbol sections and adjust values of symbols in
11434 SEC_MERGE sections. Write out those local symbols we know are
11435 going into the output file. */
11436 isymend = PTR_ADD (isymbuf, locsymcount);
11437 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11438 isym < isymend;
11439 isym++, pindex++, ppsection++)
11440 {
11441 asection *isec;
11442 const char *name;
11443 Elf_Internal_Sym osym;
11444 long indx;
11445 int ret;
11446
11447 *pindex = -1;
11448
11449 if (elf_bad_symtab (input_bfd))
11450 {
11451 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11452 {
11453 *ppsection = NULL;
11454 continue;
11455 }
11456 }
11457
11458 if (isym->st_shndx == SHN_UNDEF)
11459 isec = bfd_und_section_ptr;
11460 else if (isym->st_shndx == SHN_ABS)
11461 isec = bfd_abs_section_ptr;
11462 else if (isym->st_shndx == SHN_COMMON)
11463 isec = bfd_com_section_ptr;
11464 else
11465 {
11466 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11467 if (isec == NULL)
11468 {
11469 /* Don't attempt to output symbols with st_shnx in the
11470 reserved range other than SHN_ABS and SHN_COMMON. */
11471 isec = bfd_und_section_ptr;
11472 }
11473 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11474 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11475 isym->st_value =
11476 _bfd_merged_section_offset (output_bfd, &isec,
11477 elf_section_data (isec)->sec_info,
11478 isym->st_value);
11479 }
11480
11481 *ppsection = isec;
11482
11483 /* Don't output the first, undefined, symbol. In fact, don't
11484 output any undefined local symbol. */
11485 if (isec == bfd_und_section_ptr)
11486 continue;
11487
11488 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11489 {
11490 /* We never output section symbols. Instead, we use the
11491 section symbol of the corresponding section in the output
11492 file. */
11493 continue;
11494 }
11495
11496 /* If we are stripping all symbols, we don't want to output this
11497 one. */
11498 if (flinfo->info->strip == strip_all)
11499 continue;
11500
11501 /* If we are discarding all local symbols, we don't want to
11502 output this one. If we are generating a relocatable output
11503 file, then some of the local symbols may be required by
11504 relocs; we output them below as we discover that they are
11505 needed. */
11506 if (flinfo->info->discard == discard_all)
11507 continue;
11508
11509 /* If this symbol is defined in a section which we are
11510 discarding, we don't need to keep it. */
11511 if (isym->st_shndx < SHN_LORESERVE
11512 && (isec->output_section == NULL
11513 || bfd_section_removed_from_list (output_bfd,
11514 isec->output_section)))
11515 continue;
11516
11517 /* Get the name of the symbol. */
11518 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11519 isym->st_name);
11520 if (name == NULL)
11521 return false;
11522
11523 /* See if we are discarding symbols with this name. */
11524 if ((flinfo->info->strip == strip_some
11525 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11526 == NULL))
11527 || (((flinfo->info->discard == discard_sec_merge
11528 && (isec->flags & SEC_MERGE)
11529 && !bfd_link_relocatable (flinfo->info))
11530 || flinfo->info->discard == discard_l)
11531 && bfd_is_local_label_name (input_bfd, name)))
11532 continue;
11533
11534 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11535 {
11536 if (input_bfd->lto_output)
11537 /* -flto puts a temp file name here. This means builds
11538 are not reproducible. Discard the symbol. */
11539 continue;
11540 have_file_sym = true;
11541 flinfo->filesym_count += 1;
11542 }
11543 if (!have_file_sym)
11544 {
11545 /* In the absence of debug info, bfd_find_nearest_line uses
11546 FILE symbols to determine the source file for local
11547 function symbols. Provide a FILE symbol here if input
11548 files lack such, so that their symbols won't be
11549 associated with a previous input file. It's not the
11550 source file, but the best we can do. */
11551 const char *filename;
11552 have_file_sym = true;
11553 flinfo->filesym_count += 1;
11554 memset (&osym, 0, sizeof (osym));
11555 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11556 osym.st_shndx = SHN_ABS;
11557 if (input_bfd->lto_output)
11558 filename = NULL;
11559 else
11560 filename = lbasename (bfd_get_filename (input_bfd));
11561 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11562 bfd_abs_section_ptr, NULL))
11563 return false;
11564 }
11565
11566 osym = *isym;
11567
11568 /* Adjust the section index for the output file. */
11569 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11570 isec->output_section);
11571 if (osym.st_shndx == SHN_BAD)
11572 return false;
11573
11574 /* ELF symbols in relocatable files are section relative, but
11575 in executable files they are virtual addresses. Note that
11576 this code assumes that all ELF sections have an associated
11577 BFD section with a reasonable value for output_offset; below
11578 we assume that they also have a reasonable value for
11579 output_section. Any special sections must be set up to meet
11580 these requirements. */
11581 osym.st_value += isec->output_offset;
11582 if (!bfd_link_relocatable (flinfo->info))
11583 {
11584 osym.st_value += isec->output_section->vma;
11585 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11586 {
11587 /* STT_TLS symbols are relative to PT_TLS segment base. */
11588 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11589 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11590 else
11591 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11592 STT_NOTYPE);
11593 }
11594 }
11595
11596 indx = bfd_get_symcount (output_bfd);
11597 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11598 if (ret == 0)
11599 return false;
11600 else if (ret == 1)
11601 *pindex = indx;
11602 }
11603
11604 if (bed->s->arch_size == 32)
11605 {
11606 r_type_mask = 0xff;
11607 r_sym_shift = 8;
11608 address_size = 4;
11609 }
11610 else
11611 {
11612 r_type_mask = 0xffffffff;
11613 r_sym_shift = 32;
11614 address_size = 8;
11615 }
11616
11617 /* Relocate the contents of each section. */
11618 sym_hashes = elf_sym_hashes (input_bfd);
11619 for (o = input_bfd->sections; o != NULL; o = o->next)
11620 {
11621 bfd_byte *contents;
11622
11623 if (! o->linker_mark)
11624 {
11625 /* This section was omitted from the link. */
11626 continue;
11627 }
11628
11629 if (!flinfo->info->resolve_section_groups
11630 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11631 {
11632 /* Deal with the group signature symbol. */
11633 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11634 unsigned long symndx = sec_data->this_hdr.sh_info;
11635 asection *osec = o->output_section;
11636
11637 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11638 if (symndx >= locsymcount
11639 || (elf_bad_symtab (input_bfd)
11640 && flinfo->sections[symndx] == NULL))
11641 {
11642 struct elf_link_hash_entry *h;
11643
11644 h = get_link_hash_entry (sym_hashes, symndx, extsymoff);
11645 if (h == NULL)
11646 {
11647 _bfd_error_handler
11648 /* xgettext:c-format */
11649 (_("error: %pB: unable to create group section symbol"),
11650 input_bfd);
11651 bfd_set_error (bfd_error_bad_value);
11652 return false;
11653 }
11654
11655 /* Arrange for symbol to be output. */
11656 h->indx = -2;
11657 elf_section_data (osec)->this_hdr.sh_info = -2;
11658 }
11659 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11660 {
11661 /* We'll use the output section target_index. */
11662 asection *sec = flinfo->sections[symndx]->output_section;
11663 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11664 }
11665 else
11666 {
11667 if (flinfo->indices[symndx] == -1)
11668 {
11669 /* Otherwise output the local symbol now. */
11670 Elf_Internal_Sym sym = isymbuf[symndx];
11671 asection *sec = flinfo->sections[symndx]->output_section;
11672 const char *name;
11673 long indx;
11674 int ret;
11675
11676 name = bfd_elf_string_from_elf_section (input_bfd,
11677 symtab_hdr->sh_link,
11678 sym.st_name);
11679 if (name == NULL)
11680 return false;
11681
11682 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11683 sec);
11684 if (sym.st_shndx == SHN_BAD)
11685 return false;
11686
11687 sym.st_value += o->output_offset;
11688
11689 indx = bfd_get_symcount (output_bfd);
11690 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11691 NULL);
11692 if (ret == 0)
11693 return false;
11694 else if (ret == 1)
11695 flinfo->indices[symndx] = indx;
11696 else
11697 abort ();
11698 }
11699 elf_section_data (osec)->this_hdr.sh_info
11700 = flinfo->indices[symndx];
11701 }
11702 }
11703
11704 if ((o->flags & SEC_HAS_CONTENTS) == 0
11705 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11706 continue;
11707
11708 if ((o->flags & SEC_LINKER_CREATED) != 0)
11709 {
11710 /* Section was created by _bfd_elf_link_create_dynamic_sections
11711 or somesuch. */
11712 continue;
11713 }
11714
11715 /* Get the contents of the section. They have been cached by a
11716 relaxation routine. Note that o is a section in an input
11717 file, so the contents field will not have been set by any of
11718 the routines which work on output files. */
11719 if (elf_section_data (o)->this_hdr.contents != NULL)
11720 {
11721 contents = elf_section_data (o)->this_hdr.contents;
11722 if (bed->caches_rawsize
11723 && o->rawsize != 0
11724 && o->rawsize < o->size)
11725 {
11726 memcpy (flinfo->contents, contents, o->rawsize);
11727 contents = flinfo->contents;
11728 }
11729 }
11730 else if (!(o->flags & SEC_RELOC)
11731 && !bed->elf_backend_write_section
11732 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11733 /* A MERGE section that has no relocations doesn't need the
11734 contents anymore, they have been recorded earlier. Except
11735 if the backend has special provisions for writing sections. */
11736 contents = NULL;
11737 else
11738 {
11739 contents = flinfo->contents;
11740 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11741 &contents))
11742 return false;
11743 }
11744
11745 if ((o->flags & SEC_RELOC) != 0)
11746 {
11747 Elf_Internal_Rela *internal_relocs;
11748 Elf_Internal_Rela *rel, *relend;
11749 int action_discarded;
11750 int ret;
11751
11752 /* Get the swapped relocs. */
11753 internal_relocs
11754 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11755 flinfo->external_relocs,
11756 flinfo->internal_relocs,
11757 false);
11758 if (internal_relocs == NULL
11759 && o->reloc_count > 0)
11760 return false;
11761
11762 action_discarded = -1;
11763 if (!elf_section_ignore_discarded_relocs (o))
11764 action_discarded = (*bed->action_discarded) (o);
11765
11766 /* Run through the relocs evaluating complex reloc symbols and
11767 looking for relocs against symbols from discarded sections
11768 or section symbols from removed link-once sections.
11769 Complain about relocs against discarded sections. Zero
11770 relocs against removed link-once sections. */
11771
11772 rel = internal_relocs;
11773 relend = rel + o->reloc_count;
11774 for ( ; rel < relend; rel++)
11775 {
11776 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11777 unsigned int s_type;
11778 asection **ps, *sec;
11779 struct elf_link_hash_entry *h = NULL;
11780 const char *sym_name;
11781
11782 if (r_symndx == STN_UNDEF)
11783 continue;
11784
11785 if (r_symndx >= locsymcount
11786 || (elf_bad_symtab (input_bfd)
11787 && flinfo->sections[r_symndx] == NULL))
11788 {
11789 h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff);
11790
11791 /* Badly formatted input files can contain relocs that
11792 reference non-existant symbols. Check here so that
11793 we do not seg fault. */
11794 if (h == NULL)
11795 {
11796 _bfd_error_handler
11797 /* xgettext:c-format */
11798 (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
11799 "that references a non-existent global symbol"),
11800 input_bfd, (uint64_t) rel->r_info, o);
11801 bfd_set_error (bfd_error_bad_value);
11802 return false;
11803 }
11804
11805 s_type = h->type;
11806
11807 /* If a plugin symbol is referenced from a non-IR file,
11808 mark the symbol as undefined. Note that the
11809 linker may attach linker created dynamic sections
11810 to the plugin bfd. Symbols defined in linker
11811 created sections are not plugin symbols. */
11812 if ((h->root.non_ir_ref_regular
11813 || h->root.non_ir_ref_dynamic)
11814 && (h->root.type == bfd_link_hash_defined
11815 || h->root.type == bfd_link_hash_defweak)
11816 && (h->root.u.def.section->flags
11817 & SEC_LINKER_CREATED) == 0
11818 && h->root.u.def.section->owner != NULL
11819 && (h->root.u.def.section->owner->flags
11820 & BFD_PLUGIN) != 0)
11821 {
11822 h->root.type = bfd_link_hash_undefined;
11823 h->root.u.undef.abfd = h->root.u.def.section->owner;
11824 }
11825
11826 ps = NULL;
11827 if (h->root.type == bfd_link_hash_defined
11828 || h->root.type == bfd_link_hash_defweak)
11829 ps = &h->root.u.def.section;
11830
11831 sym_name = h->root.root.string;
11832 }
11833 else
11834 {
11835 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11836
11837 s_type = ELF_ST_TYPE (sym->st_info);
11838 ps = &flinfo->sections[r_symndx];
11839 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11840 sym, *ps);
11841 }
11842
11843 if ((s_type == STT_RELC || s_type == STT_SRELC)
11844 && !bfd_link_relocatable (flinfo->info))
11845 {
11846 bfd_vma val;
11847 bfd_vma dot = (rel->r_offset
11848 + o->output_offset + o->output_section->vma);
11849 #ifdef DEBUG
11850 printf ("Encountered a complex symbol!");
11851 printf (" (input_bfd %s, section %s, reloc %ld\n",
11852 bfd_get_filename (input_bfd), o->name,
11853 (long) (rel - internal_relocs));
11854 printf (" symbol: idx %8.8lx, name %s\n",
11855 r_symndx, sym_name);
11856 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11857 (unsigned long) rel->r_info,
11858 (unsigned long) rel->r_offset);
11859 #endif
11860 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11861 isymbuf, locsymcount, s_type == STT_SRELC))
11862 return false;
11863
11864 /* Symbol evaluated OK. Update to absolute value. */
11865 set_symbol_value (input_bfd, isymbuf, locsymcount,
11866 r_symndx, val);
11867 continue;
11868 }
11869
11870 if (action_discarded != -1 && ps != NULL)
11871 {
11872 /* Complain if the definition comes from a
11873 discarded section. */
11874 if ((sec = *ps) != NULL && discarded_section (sec))
11875 {
11876 BFD_ASSERT (r_symndx != STN_UNDEF);
11877 if (action_discarded & COMPLAIN)
11878 (*flinfo->info->callbacks->einfo)
11879 /* xgettext:c-format */
11880 (_("%X`%s' referenced in section `%pA' of %pB: "
11881 "defined in discarded section `%pA' of %pB\n"),
11882 sym_name, o, input_bfd, sec, sec->owner);
11883
11884 /* Try to do the best we can to support buggy old
11885 versions of gcc. Pretend that the symbol is
11886 really defined in the kept linkonce section.
11887 FIXME: This is quite broken. Modifying the
11888 symbol here means we will be changing all later
11889 uses of the symbol, not just in this section. */
11890 if (action_discarded & PRETEND)
11891 {
11892 asection *kept;
11893
11894 kept = _bfd_elf_check_kept_section (sec,
11895 flinfo->info);
11896 if (kept != NULL)
11897 {
11898 *ps = kept;
11899 continue;
11900 }
11901 }
11902 }
11903 }
11904 }
11905
11906 /* Relocate the section by invoking a back end routine.
11907
11908 The back end routine is responsible for adjusting the
11909 section contents as necessary, and (if using Rela relocs
11910 and generating a relocatable output file) adjusting the
11911 reloc addend as necessary.
11912
11913 The back end routine does not have to worry about setting
11914 the reloc address or the reloc symbol index.
11915
11916 The back end routine is given a pointer to the swapped in
11917 internal symbols, and can access the hash table entries
11918 for the external symbols via elf_sym_hashes (input_bfd).
11919
11920 When generating relocatable output, the back end routine
11921 must handle STB_LOCAL/STT_SECTION symbols specially. The
11922 output symbol is going to be a section symbol
11923 corresponding to the output section, which will require
11924 the addend to be adjusted. */
11925
11926 ret = (*relocate_section) (output_bfd, flinfo->info,
11927 input_bfd, o, contents,
11928 internal_relocs,
11929 isymbuf,
11930 flinfo->sections);
11931 if (!ret)
11932 return false;
11933
11934 if (ret == 2
11935 || bfd_link_relocatable (flinfo->info)
11936 || flinfo->info->emitrelocations)
11937 {
11938 Elf_Internal_Rela *irela;
11939 Elf_Internal_Rela *irelaend, *irelamid;
11940 bfd_vma last_offset;
11941 struct elf_link_hash_entry **rel_hash;
11942 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11943 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11944 unsigned int next_erel;
11945 bool rela_normal;
11946 struct bfd_elf_section_data *esdi, *esdo;
11947
11948 esdi = elf_section_data (o);
11949 esdo = elf_section_data (o->output_section);
11950 rela_normal = false;
11951
11952 /* Adjust the reloc addresses and symbol indices. */
11953
11954 irela = internal_relocs;
11955 irelaend = irela + o->reloc_count;
11956 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11957 /* We start processing the REL relocs, if any. When we reach
11958 IRELAMID in the loop, we switch to the RELA relocs. */
11959 irelamid = irela;
11960 if (esdi->rel.hdr != NULL)
11961 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11962 * bed->s->int_rels_per_ext_rel);
11963 rel_hash_list = rel_hash;
11964 rela_hash_list = NULL;
11965 last_offset = o->output_offset;
11966 if (!bfd_link_relocatable (flinfo->info))
11967 last_offset += o->output_section->vma;
11968 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11969 {
11970 unsigned long r_symndx;
11971 asection *sec;
11972 Elf_Internal_Sym sym;
11973
11974 if (next_erel == bed->s->int_rels_per_ext_rel)
11975 {
11976 rel_hash++;
11977 next_erel = 0;
11978 }
11979
11980 if (irela == irelamid)
11981 {
11982 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11983 rela_hash_list = rel_hash;
11984 rela_normal = bed->rela_normal;
11985 }
11986
11987 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11988 flinfo->info, o,
11989 irela->r_offset);
11990 if (irela->r_offset >= (bfd_vma) -2)
11991 {
11992 /* This is a reloc for a deleted entry or somesuch.
11993 Turn it into an R_*_NONE reloc, at the same
11994 offset as the last reloc. elf_eh_frame.c and
11995 bfd_elf_discard_info rely on reloc offsets
11996 being ordered. */
11997 irela->r_offset = last_offset;
11998 irela->r_info = 0;
11999 irela->r_addend = 0;
12000 continue;
12001 }
12002
12003 irela->r_offset += o->output_offset;
12004
12005 /* Relocs in an executable have to be virtual addresses. */
12006 if (!bfd_link_relocatable (flinfo->info))
12007 irela->r_offset += o->output_section->vma;
12008
12009 last_offset = irela->r_offset;
12010
12011 r_symndx = irela->r_info >> r_sym_shift;
12012 if (r_symndx == STN_UNDEF)
12013 continue;
12014
12015 if (r_symndx >= locsymcount
12016 || (elf_bad_symtab (input_bfd)
12017 && flinfo->sections[r_symndx] == NULL))
12018 {
12019 struct elf_link_hash_entry *rh;
12020
12021 /* This is a reloc against a global symbol. We
12022 have not yet output all the local symbols, so
12023 we do not know the symbol index of any global
12024 symbol. We set the rel_hash entry for this
12025 reloc to point to the global hash table entry
12026 for this symbol. The symbol index is then
12027 set at the end of bfd_elf_final_link. */
12028 rh = get_link_hash_entry (elf_sym_hashes (input_bfd),
12029 r_symndx, extsymoff);
12030 if (rh == NULL)
12031 {
12032 /* FIXME: Generate an error ? */
12033 continue;
12034 }
12035
12036 /* Setting the index to -2 tells elf_link_output_extsym
12037 that this symbol is used by a reloc. */
12038 BFD_ASSERT (rh->indx < 0);
12039 rh->indx = -2;
12040 *rel_hash = rh;
12041
12042 continue;
12043 }
12044
12045 /* This is a reloc against a local symbol. */
12046
12047 *rel_hash = NULL;
12048 sym = isymbuf[r_symndx];
12049 sec = flinfo->sections[r_symndx];
12050 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12051 {
12052 /* I suppose the backend ought to fill in the
12053 section of any STT_SECTION symbol against a
12054 processor specific section. */
12055 r_symndx = STN_UNDEF;
12056 if (bfd_is_abs_section (sec))
12057 ;
12058 else if (sec == NULL || sec->owner == NULL)
12059 {
12060 bfd_set_error (bfd_error_bad_value);
12061 return false;
12062 }
12063 else
12064 {
12065 asection *osec = sec->output_section;
12066
12067 /* If we have discarded a section, the output
12068 section will be the absolute section. In
12069 case of discarded SEC_MERGE sections, use
12070 the kept section. relocate_section should
12071 have already handled discarded linkonce
12072 sections. */
12073 if (bfd_is_abs_section (osec)
12074 && sec->kept_section != NULL
12075 && sec->kept_section->output_section != NULL)
12076 {
12077 osec = sec->kept_section->output_section;
12078 irela->r_addend -= osec->vma;
12079 }
12080
12081 if (!bfd_is_abs_section (osec))
12082 {
12083 r_symndx = osec->target_index;
12084 if (r_symndx == STN_UNDEF)
12085 {
12086 irela->r_addend += osec->vma;
12087 osec = _bfd_nearby_section (output_bfd, osec,
12088 osec->vma);
12089 irela->r_addend -= osec->vma;
12090 r_symndx = osec->target_index;
12091 }
12092 }
12093 }
12094
12095 /* Adjust the addend according to where the
12096 section winds up in the output section. */
12097 if (rela_normal)
12098 irela->r_addend += sec->output_offset;
12099 }
12100 else
12101 {
12102 if (flinfo->indices[r_symndx] == -1)
12103 {
12104 unsigned long shlink;
12105 const char *name;
12106 asection *osec;
12107 long indx;
12108
12109 if (flinfo->info->strip == strip_all)
12110 {
12111 /* You can't do ld -r -s. */
12112 bfd_set_error (bfd_error_invalid_operation);
12113 return false;
12114 }
12115
12116 /* This symbol was skipped earlier, but
12117 since it is needed by a reloc, we
12118 must output it now. */
12119 shlink = symtab_hdr->sh_link;
12120 name = (bfd_elf_string_from_elf_section
12121 (input_bfd, shlink, sym.st_name));
12122 if (name == NULL)
12123 return false;
12124
12125 osec = sec->output_section;
12126 sym.st_shndx =
12127 _bfd_elf_section_from_bfd_section (output_bfd,
12128 osec);
12129 if (sym.st_shndx == SHN_BAD)
12130 return false;
12131
12132 sym.st_value += sec->output_offset;
12133 if (!bfd_link_relocatable (flinfo->info))
12134 {
12135 sym.st_value += osec->vma;
12136 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12137 {
12138 struct elf_link_hash_table *htab
12139 = elf_hash_table (flinfo->info);
12140
12141 /* STT_TLS symbols are relative to PT_TLS
12142 segment base. */
12143 if (htab->tls_sec != NULL)
12144 sym.st_value -= htab->tls_sec->vma;
12145 else
12146 sym.st_info
12147 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12148 STT_NOTYPE);
12149 }
12150 }
12151
12152 indx = bfd_get_symcount (output_bfd);
12153 ret = elf_link_output_symstrtab (flinfo, name,
12154 &sym, sec,
12155 NULL);
12156 if (ret == 0)
12157 return false;
12158 else if (ret == 1)
12159 flinfo->indices[r_symndx] = indx;
12160 else
12161 abort ();
12162 }
12163
12164 r_symndx = flinfo->indices[r_symndx];
12165 }
12166
12167 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12168 | (irela->r_info & r_type_mask));
12169 }
12170
12171 /* Swap out the relocs. */
12172 input_rel_hdr = esdi->rel.hdr;
12173 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12174 {
12175 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12176 input_rel_hdr,
12177 internal_relocs,
12178 rel_hash_list))
12179 return false;
12180 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12181 * bed->s->int_rels_per_ext_rel);
12182 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12183 }
12184
12185 input_rela_hdr = esdi->rela.hdr;
12186 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12187 {
12188 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12189 input_rela_hdr,
12190 internal_relocs,
12191 rela_hash_list))
12192 return false;
12193 }
12194 }
12195 }
12196
12197 /* Write out the modified section contents. */
12198 if (bed->elf_backend_write_section
12199 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12200 contents))
12201 {
12202 /* Section written out. */
12203 }
12204 else switch (o->sec_info_type)
12205 {
12206 case SEC_INFO_TYPE_STABS:
12207 if (! (_bfd_write_section_stabs
12208 (output_bfd,
12209 &elf_hash_table (flinfo->info)->stab_info,
12210 o, &elf_section_data (o)->sec_info, contents)))
12211 return false;
12212 break;
12213 case SEC_INFO_TYPE_MERGE:
12214 if (! _bfd_write_merged_section (output_bfd, o,
12215 elf_section_data (o)->sec_info))
12216 return false;
12217 break;
12218 case SEC_INFO_TYPE_EH_FRAME:
12219 {
12220 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12221 o, contents))
12222 return false;
12223 }
12224 break;
12225 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12226 {
12227 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12228 flinfo->info,
12229 o, contents))
12230 return false;
12231 }
12232 break;
12233 case SEC_INFO_TYPE_SFRAME:
12234 {
12235 /* Merge SFrame section into the SFrame encoder context of the
12236 output_bfd's section. The final .sframe output section will
12237 be written out later. */
12238 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12239 o, contents))
12240 return false;
12241 }
12242 break;
12243 default:
12244 {
12245 if (! (o->flags & SEC_EXCLUDE))
12246 {
12247 file_ptr offset = (file_ptr) o->output_offset;
12248 bfd_size_type todo = o->size;
12249
12250 offset *= bfd_octets_per_byte (output_bfd, o);
12251
12252 if ((o->flags & SEC_ELF_REVERSE_COPY)
12253 && o->size > address_size)
12254 {
12255 /* Reverse-copy input section to output. */
12256
12257 if ((o->size & (address_size - 1)) != 0
12258 || (o->reloc_count != 0
12259 && (o->size * bed->s->int_rels_per_ext_rel
12260 != o->reloc_count * address_size)))
12261 {
12262 _bfd_error_handler
12263 /* xgettext:c-format */
12264 (_("error: %pB: size of section %pA is not "
12265 "multiple of address size"),
12266 input_bfd, o);
12267 bfd_set_error (bfd_error_bad_value);
12268 return false;
12269 }
12270
12271 do
12272 {
12273 todo -= address_size;
12274 if (! bfd_set_section_contents (output_bfd,
12275 o->output_section,
12276 contents + todo,
12277 offset,
12278 address_size))
12279 return false;
12280 if (todo == 0)
12281 break;
12282 offset += address_size;
12283 }
12284 while (1);
12285 }
12286 else if (! bfd_set_section_contents (output_bfd,
12287 o->output_section,
12288 contents,
12289 offset, todo))
12290 return false;
12291 }
12292 }
12293 break;
12294 }
12295
12296 /* Munmap the section contents for each input section. */
12297 _bfd_elf_link_munmap_section_contents (o);
12298 }
12299
12300 return true;
12301 }
12302
12303 /* Generate a reloc when linking an ELF file. This is a reloc
12304 requested by the linker, and does not come from any input file. This
12305 is used to build constructor and destructor tables when linking
12306 with -Ur. */
12307
12308 static bool
12309 elf_reloc_link_order (bfd *output_bfd,
12310 struct bfd_link_info *info,
12311 asection *output_section,
12312 struct bfd_link_order *link_order)
12313 {
12314 reloc_howto_type *howto;
12315 long indx;
12316 bfd_vma offset;
12317 bfd_vma addend;
12318 struct bfd_elf_section_reloc_data *reldata;
12319 struct elf_link_hash_entry **rel_hash_ptr;
12320 Elf_Internal_Shdr *rel_hdr;
12321 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12322 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12323 bfd_byte *erel;
12324 unsigned int i;
12325 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12326
12327 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12328 if (howto == NULL)
12329 {
12330 bfd_set_error (bfd_error_bad_value);
12331 return false;
12332 }
12333
12334 addend = link_order->u.reloc.p->addend;
12335
12336 if (esdo->rel.hdr)
12337 reldata = &esdo->rel;
12338 else if (esdo->rela.hdr)
12339 reldata = &esdo->rela;
12340 else
12341 {
12342 reldata = NULL;
12343 BFD_ASSERT (0);
12344 }
12345
12346 /* Figure out the symbol index. */
12347 rel_hash_ptr = reldata->hashes + reldata->count;
12348 if (link_order->type == bfd_section_reloc_link_order)
12349 {
12350 indx = link_order->u.reloc.p->u.section->target_index;
12351 BFD_ASSERT (indx != 0);
12352 *rel_hash_ptr = NULL;
12353 }
12354 else
12355 {
12356 struct elf_link_hash_entry *h;
12357
12358 /* Treat a reloc against a defined symbol as though it were
12359 actually against the section. */
12360 h = ((struct elf_link_hash_entry *)
12361 bfd_wrapped_link_hash_lookup (output_bfd, info,
12362 link_order->u.reloc.p->u.name,
12363 false, false, true));
12364 if (h != NULL
12365 && (h->root.type == bfd_link_hash_defined
12366 || h->root.type == bfd_link_hash_defweak))
12367 {
12368 asection *section;
12369
12370 section = h->root.u.def.section;
12371 indx = section->output_section->target_index;
12372 *rel_hash_ptr = NULL;
12373 /* It seems that we ought to add the symbol value to the
12374 addend here, but in practice it has already been added
12375 because it was passed to constructor_callback. */
12376 addend += section->output_section->vma + section->output_offset;
12377 }
12378 else if (h != NULL)
12379 {
12380 /* Setting the index to -2 tells elf_link_output_extsym that
12381 this symbol is used by a reloc. */
12382 h->indx = -2;
12383 *rel_hash_ptr = h;
12384 indx = 0;
12385 }
12386 else
12387 {
12388 (*info->callbacks->unattached_reloc)
12389 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12390 indx = 0;
12391 }
12392 }
12393
12394 /* If this is an inplace reloc, we must write the addend into the
12395 object file. */
12396 if (howto->partial_inplace && addend != 0)
12397 {
12398 bfd_size_type size;
12399 bfd_reloc_status_type rstat;
12400 bfd_byte *buf;
12401 bool ok;
12402 const char *sym_name;
12403 bfd_size_type octets;
12404
12405 size = (bfd_size_type) bfd_get_reloc_size (howto);
12406 buf = (bfd_byte *) bfd_zmalloc (size);
12407 if (buf == NULL && size != 0)
12408 return false;
12409 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12410 switch (rstat)
12411 {
12412 case bfd_reloc_ok:
12413 break;
12414
12415 default:
12416 case bfd_reloc_outofrange:
12417 abort ();
12418
12419 case bfd_reloc_overflow:
12420 if (link_order->type == bfd_section_reloc_link_order)
12421 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12422 else
12423 sym_name = link_order->u.reloc.p->u.name;
12424 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12425 howto->name, addend, NULL, NULL,
12426 (bfd_vma) 0);
12427 break;
12428 }
12429
12430 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12431 output_section);
12432 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12433 octets, size);
12434 free (buf);
12435 if (! ok)
12436 return false;
12437 }
12438
12439 /* The address of a reloc is relative to the section in a
12440 relocatable file, and is a virtual address in an executable
12441 file. */
12442 offset = link_order->offset;
12443 if (! bfd_link_relocatable (info))
12444 offset += output_section->vma;
12445
12446 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12447 {
12448 irel[i].r_offset = offset;
12449 irel[i].r_info = 0;
12450 irel[i].r_addend = 0;
12451 }
12452 if (bed->s->arch_size == 32)
12453 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12454 else
12455 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12456
12457 rel_hdr = reldata->hdr;
12458 erel = rel_hdr->contents;
12459 if (rel_hdr->sh_type == SHT_REL)
12460 {
12461 erel += reldata->count * bed->s->sizeof_rel;
12462 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12463 }
12464 else
12465 {
12466 irel[0].r_addend = addend;
12467 erel += reldata->count * bed->s->sizeof_rela;
12468 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12469 }
12470
12471 ++reldata->count;
12472
12473 return true;
12474 }
12475
12476 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12477 Returns TRUE upon success, FALSE otherwise. */
12478
12479 static bool
12480 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12481 {
12482 bool ret = false;
12483 bfd *implib_bfd;
12484 const struct elf_backend_data *bed;
12485 flagword flags;
12486 enum bfd_architecture arch;
12487 unsigned int mach;
12488 asymbol **sympp = NULL;
12489 long symsize;
12490 long symcount;
12491 long src_count;
12492 elf_symbol_type *osymbuf;
12493 size_t amt;
12494
12495 implib_bfd = info->out_implib_bfd;
12496 bed = get_elf_backend_data (abfd);
12497
12498 if (!bfd_set_format (implib_bfd, bfd_object))
12499 return false;
12500
12501 /* Use flag from executable but make it a relocatable object. */
12502 flags = bfd_get_file_flags (abfd);
12503 flags &= ~HAS_RELOC;
12504 if (!bfd_set_start_address (implib_bfd, 0)
12505 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12506 return false;
12507
12508 /* Copy architecture of output file to import library file. */
12509 arch = bfd_get_arch (abfd);
12510 mach = bfd_get_mach (abfd);
12511 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12512 && (abfd->target_defaulted
12513 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12514 return false;
12515
12516 /* Get symbol table size. */
12517 symsize = bfd_get_symtab_upper_bound (abfd);
12518 if (symsize < 0)
12519 return false;
12520
12521 /* Read in the symbol table. */
12522 sympp = (asymbol **) bfd_malloc (symsize);
12523 if (sympp == NULL)
12524 return false;
12525
12526 symcount = bfd_canonicalize_symtab (abfd, sympp);
12527 if (symcount < 0)
12528 goto free_sym_buf;
12529
12530 /* Allow the BFD backend to copy any private header data it
12531 understands from the output BFD to the import library BFD. */
12532 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12533 goto free_sym_buf;
12534
12535 /* Filter symbols to appear in the import library. */
12536 if (bed->elf_backend_filter_implib_symbols)
12537 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12538 symcount);
12539 else
12540 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12541 if (symcount == 0)
12542 {
12543 bfd_set_error (bfd_error_no_symbols);
12544 _bfd_error_handler (_("%pB: no symbol found for import library"),
12545 implib_bfd);
12546 goto free_sym_buf;
12547 }
12548
12549
12550 /* Make symbols absolute. */
12551 amt = symcount * sizeof (*osymbuf);
12552 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12553 if (osymbuf == NULL)
12554 goto free_sym_buf;
12555
12556 for (src_count = 0; src_count < symcount; src_count++)
12557 {
12558 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12559 sizeof (*osymbuf));
12560 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12561 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12562 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12563 osymbuf[src_count].internal_elf_sym.st_value =
12564 osymbuf[src_count].symbol.value;
12565 sympp[src_count] = &osymbuf[src_count].symbol;
12566 }
12567
12568 bfd_set_symtab (implib_bfd, sympp, symcount);
12569
12570 /* Allow the BFD backend to copy any private data it understands
12571 from the output BFD to the import library BFD. This is done last
12572 to permit the routine to look at the filtered symbol table. */
12573 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12574 goto free_sym_buf;
12575
12576 if (!bfd_close (implib_bfd))
12577 goto free_sym_buf;
12578
12579 ret = true;
12580
12581 free_sym_buf:
12582 free (sympp);
12583 return ret;
12584 }
12585
12586 static void
12587 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12588 {
12589 asection *o;
12590
12591 if (flinfo->symstrtab != NULL)
12592 _bfd_elf_strtab_free (flinfo->symstrtab);
12593 free (flinfo->contents);
12594 free (flinfo->external_relocs);
12595 free (flinfo->internal_relocs);
12596 free (flinfo->external_syms);
12597 free (flinfo->locsym_shndx);
12598 free (flinfo->internal_syms);
12599 free (flinfo->indices);
12600 free (flinfo->sections);
12601 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12602 free (flinfo->symshndxbuf);
12603 for (o = obfd->sections; o != NULL; o = o->next)
12604 {
12605 struct bfd_elf_section_data *esdo = elf_section_data (o);
12606 free (esdo->rel.hashes);
12607 free (esdo->rela.hashes);
12608 }
12609 }
12610
12611 /* Do the final step of an ELF link. */
12612
12613 bool
12614 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12615 {
12616 bool dynamic;
12617 bool emit_relocs;
12618 bfd *dynobj;
12619 struct elf_final_link_info flinfo;
12620 asection *o;
12621 struct bfd_link_order *p;
12622 bfd *sub;
12623 bfd_size_type max_contents_size;
12624 bfd_size_type max_external_reloc_size;
12625 bfd_size_type max_internal_reloc_count;
12626 bfd_size_type max_sym_count;
12627 bfd_size_type max_sym_shndx_count;
12628 Elf_Internal_Sym elfsym;
12629 unsigned int i;
12630 Elf_Internal_Shdr *symtab_hdr;
12631 Elf_Internal_Shdr *symtab_shndx_hdr;
12632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12633 struct elf_outext_info eoinfo;
12634 bool merged;
12635 size_t relativecount;
12636 size_t relr_entsize;
12637 asection *reldyn = 0;
12638 bfd_size_type amt;
12639 struct elf_link_hash_table *htab = elf_hash_table (info);
12640 bool sections_removed;
12641
12642 if (!is_elf_hash_table (&htab->root))
12643 return false;
12644
12645 if (bfd_link_pic (info))
12646 abfd->flags |= DYNAMIC;
12647
12648 dynamic = htab->dynamic_sections_created;
12649 dynobj = htab->dynobj;
12650
12651 emit_relocs = (bfd_link_relocatable (info)
12652 || info->emitrelocations);
12653
12654 memset (&flinfo, 0, sizeof (flinfo));
12655 flinfo.info = info;
12656 flinfo.output_bfd = abfd;
12657 flinfo.symstrtab = _bfd_elf_strtab_init ();
12658 if (flinfo.symstrtab == NULL)
12659 return false;
12660
12661 if (! dynamic)
12662 {
12663 flinfo.hash_sec = NULL;
12664 flinfo.symver_sec = NULL;
12665 }
12666 else
12667 {
12668 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12669 /* Note that dynsym_sec can be NULL (on VMS). */
12670 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12671 /* Note that it is OK if symver_sec is NULL. */
12672 }
12673
12674 if (info->unique_symbol
12675 && !bfd_hash_table_init (&flinfo.local_hash_table,
12676 local_hash_newfunc,
12677 sizeof (struct local_hash_entry)))
12678 return false;
12679
12680 /* The object attributes have been merged. Remove the input
12681 sections from the link, and set the contents of the output
12682 section. */
12683 sections_removed = false;
12684 const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12685 for (o = abfd->sections; o != NULL; o = o->next)
12686 {
12687 bool remove_section = false;
12688
12689 if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
12690 || strcmp (o->name, ".gnu.attributes") == 0)
12691 {
12692 for (p = o->map_head.link_order; p != NULL; p = p->next)
12693 {
12694 asection *input_section;
12695
12696 if (p->type != bfd_indirect_link_order)
12697 continue;
12698 input_section = p->u.indirect.section;
12699 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12700 elf_link_input_bfd ignores this section. */
12701 input_section->flags &= ~SEC_HAS_CONTENTS;
12702 }
12703
12704 /* Skip this section later on. */
12705 o->map_head.link_order = NULL;
12706
12707 bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
12708 /* Once ELF headers have been written, the size of a section is
12709 frozen. We need to set the size of the attribute section before
12710 _bfd_elf_compute_section_file_positions. */
12711 bfd_set_section_size (o, attr_size);
12712 if (attr_size > 0)
12713 elf_obj_build_attributes (abfd) = o;
12714 else
12715 remove_section = true;
12716 }
12717 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12718 {
12719 /* Remove empty group section from linker output. */
12720 remove_section = true;
12721 }
12722 if (remove_section)
12723 {
12724 o->flags |= SEC_EXCLUDE;
12725 bfd_section_list_remove (abfd, o);
12726 abfd->section_count--;
12727 sections_removed = true;
12728 }
12729 }
12730 if (sections_removed)
12731 _bfd_fix_excluded_sec_syms (abfd, info);
12732
12733 /* Count up the number of relocations we will output for each output
12734 section, so that we know the sizes of the reloc sections. We
12735 also figure out some maximum sizes. */
12736 #ifdef USE_MMAP
12737 if (bed->use_mmap)
12738 {
12739 /* Mmap is used only if section size >= the minimum mmap section
12740 size. The initial max_contents_size value covers all sections
12741 smaller than the minimum mmap section size. It may be increased
12742 for compressed or linker created sections or sections whose
12743 rawsize != size. max_external_reloc_size covers all relocation
12744 sections smaller than the minimum mmap section size. */
12745 max_contents_size = _bfd_minimum_mmap_size;
12746 max_external_reloc_size = _bfd_minimum_mmap_size;
12747 }
12748 else
12749 #endif
12750 {
12751 max_contents_size = 0;
12752 max_external_reloc_size = 0;
12753 }
12754 max_internal_reloc_count = 0;
12755 max_sym_count = 0;
12756 max_sym_shndx_count = 0;
12757 merged = false;
12758 for (o = abfd->sections; o != NULL; o = o->next)
12759 {
12760 struct bfd_elf_section_data *esdo = elf_section_data (o);
12761 o->reloc_count = 0;
12762
12763 for (p = o->map_head.link_order; p != NULL; p = p->next)
12764 {
12765 unsigned int reloc_count = 0;
12766 unsigned int additional_reloc_count = 0;
12767 struct bfd_elf_section_data *esdi = NULL;
12768
12769 if (p->type == bfd_section_reloc_link_order
12770 || p->type == bfd_symbol_reloc_link_order)
12771 reloc_count = 1;
12772 else if (p->type == bfd_indirect_link_order)
12773 {
12774 asection *sec;
12775
12776 sec = p->u.indirect.section;
12777
12778 /* Mark all sections which are to be included in the
12779 link. This will normally be every section. We need
12780 to do this so that we can identify any sections which
12781 the linker has decided to not include. */
12782 sec->linker_mark = true;
12783
12784 if (sec->flags & SEC_MERGE)
12785 merged = true;
12786
12787 #ifdef USE_MMAP
12788 /* Mmap is used only on non-compressed, non-linker created
12789 sections whose rawsize == size. */
12790 if (!bed->use_mmap
12791 || sec->compress_status != COMPRESS_SECTION_NONE
12792 || (sec->flags & SEC_LINKER_CREATED) != 0
12793 || sec->rawsize != sec->size)
12794 #endif
12795 {
12796 if (sec->rawsize > max_contents_size)
12797 max_contents_size = sec->rawsize;
12798 if (sec->size > max_contents_size)
12799 max_contents_size = sec->size;
12800 }
12801
12802 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12803 && (sec->owner->flags & DYNAMIC) == 0)
12804 {
12805 size_t sym_count;
12806
12807 /* We are interested in just local symbols, not all
12808 symbols. */
12809 if (elf_bad_symtab (sec->owner))
12810 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12811 / bed->s->sizeof_sym);
12812 else
12813 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12814
12815 if (sym_count > max_sym_count)
12816 max_sym_count = sym_count;
12817
12818 if (sym_count > max_sym_shndx_count
12819 && elf_symtab_shndx_list (sec->owner) != NULL)
12820 max_sym_shndx_count = sym_count;
12821
12822 esdi = elf_section_data (sec);
12823
12824 if (esdi->this_hdr.sh_type == SHT_REL
12825 || esdi->this_hdr.sh_type == SHT_RELA)
12826 /* Some backends use reloc_count in relocation sections
12827 to count particular types of relocs. Of course,
12828 reloc sections themselves can't have relocations. */
12829 ;
12830 else if (emit_relocs)
12831 {
12832 reloc_count = sec->reloc_count;
12833 if (bed->elf_backend_count_additional_relocs)
12834 {
12835 int c;
12836 c = (*bed->elf_backend_count_additional_relocs) (sec);
12837 additional_reloc_count += c;
12838 }
12839 }
12840 else if (bed->elf_backend_count_relocs)
12841 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12842
12843 if ((sec->flags & SEC_RELOC) != 0)
12844 {
12845 #ifdef USE_MMAP
12846 if (!bed->use_mmap)
12847 #endif
12848 {
12849 size_t ext_size = 0;
12850
12851 if (esdi->rel.hdr != NULL)
12852 ext_size = esdi->rel.hdr->sh_size;
12853 if (esdi->rela.hdr != NULL)
12854 ext_size += esdi->rela.hdr->sh_size;
12855
12856 if (ext_size > max_external_reloc_size)
12857 max_external_reloc_size = ext_size;
12858 }
12859 if (sec->reloc_count > max_internal_reloc_count)
12860 max_internal_reloc_count = sec->reloc_count;
12861 }
12862 }
12863 }
12864
12865 if (reloc_count == 0)
12866 continue;
12867
12868 reloc_count += additional_reloc_count;
12869 o->reloc_count += reloc_count;
12870
12871 if (p->type == bfd_indirect_link_order && emit_relocs)
12872 {
12873 if (esdi->rel.hdr)
12874 {
12875 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12876 esdo->rel.count += additional_reloc_count;
12877 }
12878 if (esdi->rela.hdr)
12879 {
12880 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12881 esdo->rela.count += additional_reloc_count;
12882 }
12883 }
12884 else
12885 {
12886 if (o->use_rela_p)
12887 esdo->rela.count += reloc_count;
12888 else
12889 esdo->rel.count += reloc_count;
12890 }
12891 }
12892
12893 if (o->reloc_count > 0)
12894 o->flags |= SEC_RELOC;
12895 else
12896 {
12897 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12898 set it (this is probably a bug) and if it is set
12899 assign_section_numbers will create a reloc section. */
12900 o->flags &=~ SEC_RELOC;
12901 }
12902
12903 /* If the SEC_ALLOC flag is not set, force the section VMA to
12904 zero. This is done in elf_fake_sections as well, but forcing
12905 the VMA to 0 here will ensure that relocs against these
12906 sections are handled correctly. */
12907 if ((o->flags & SEC_ALLOC) == 0
12908 && ! o->user_set_vma)
12909 o->vma = 0;
12910 }
12911
12912 if (! bfd_link_relocatable (info) && merged)
12913 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12914
12915 /* Figure out the file positions for everything but the symbol table
12916 and the relocs. We set symcount to force assign_section_numbers
12917 to create a symbol table. */
12918 abfd->symcount = info->strip != strip_all || emit_relocs;
12919 BFD_ASSERT (! abfd->output_has_begun);
12920 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12921 goto error_return;
12922
12923 /* Set sizes, and assign file positions for reloc sections. */
12924 for (o = abfd->sections; o != NULL; o = o->next)
12925 {
12926 struct bfd_elf_section_data *esdo = elf_section_data (o);
12927 if ((o->flags & SEC_RELOC) != 0)
12928 {
12929 if (esdo->rel.hdr
12930 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12931 goto error_return;
12932
12933 if (esdo->rela.hdr
12934 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12935 goto error_return;
12936 }
12937
12938 /* _bfd_elf_compute_section_file_positions makes temporary use
12939 of target_index. Reset it. */
12940 o->target_index = 0;
12941
12942 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12943 to count upwards while actually outputting the relocations. */
12944 esdo->rel.count = 0;
12945 esdo->rela.count = 0;
12946
12947 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12948 && !bfd_section_is_ctf (o))
12949 {
12950 /* Cache the section contents so that they can be compressed
12951 later. Use bfd_malloc since it will be freed by
12952 bfd_compress_section_contents. */
12953 unsigned char *contents = esdo->this_hdr.contents;
12954 if (contents != NULL)
12955 abort ();
12956 contents
12957 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12958 if (contents == NULL)
12959 goto error_return;
12960 esdo->this_hdr.contents = contents;
12961 }
12962 }
12963
12964 /* We have now assigned file positions for all the sections except .symtab,
12965 .strtab, and non-loaded reloc and compressed debugging sections. We start
12966 the .symtab section at the current file position, and write directly to it.
12967 We build the .strtab section in memory. */
12968 abfd->symcount = 0;
12969 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12970 /* sh_name is set in prep_headers. */
12971 symtab_hdr->sh_type = SHT_SYMTAB;
12972 /* sh_flags, sh_addr and sh_size all start off zero. */
12973 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12974 /* sh_link is set in assign_section_numbers. */
12975 /* sh_info is set below. */
12976 /* sh_offset is set just below. */
12977 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12978
12979 if (max_sym_count < 20)
12980 max_sym_count = 20;
12981 htab->strtabsize = max_sym_count;
12982 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12983 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12984 if (htab->strtab == NULL)
12985 goto error_return;
12986 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12987 flinfo.symshndxbuf
12988 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12989 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12990
12991 if (info->strip != strip_all || emit_relocs)
12992 {
12993 file_ptr off = elf_next_file_pos (abfd);
12994
12995 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
12996
12997 /* Note that at this point elf_next_file_pos (abfd) is
12998 incorrect. We do not yet know the size of the .symtab section.
12999 We correct next_file_pos below, after we do know the size. */
13000
13001 /* Start writing out the symbol table. The first symbol is always a
13002 dummy symbol. */
13003 elfsym.st_value = 0;
13004 elfsym.st_size = 0;
13005 elfsym.st_info = 0;
13006 elfsym.st_other = 0;
13007 elfsym.st_shndx = SHN_UNDEF;
13008 elfsym.st_target_internal = 0;
13009 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13010 bfd_und_section_ptr, NULL) != 1)
13011 goto error_return;
13012
13013 /* Output a symbol for each section if asked or they are used for
13014 relocs. These symbols usually have no names. We store the
13015 index of each one in the index field of the section, so that
13016 we can find it again when outputting relocs. */
13017
13018 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
13019 {
13020 bool name_local_sections
13021 = (bed->elf_backend_name_local_section_symbols
13022 && bed->elf_backend_name_local_section_symbols (abfd));
13023 const char *name = NULL;
13024
13025 elfsym.st_size = 0;
13026 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13027 elfsym.st_other = 0;
13028 elfsym.st_value = 0;
13029 elfsym.st_target_internal = 0;
13030 for (i = 1; i < elf_numsections (abfd); i++)
13031 {
13032 o = bfd_section_from_elf_index (abfd, i);
13033 if (o != NULL)
13034 {
13035 o->target_index = bfd_get_symcount (abfd);
13036 elfsym.st_shndx = i;
13037 if (!bfd_link_relocatable (info))
13038 elfsym.st_value = o->vma;
13039 if (name_local_sections)
13040 name = o->name;
13041 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13042 NULL) != 1)
13043 goto error_return;
13044 }
13045 }
13046 }
13047 }
13048
13049 /* On some targets like Irix 5 the symbol split between local and global
13050 ones recorded in the sh_info field needs to be done between section
13051 and all other symbols. */
13052 if (bed->elf_backend_elfsym_local_is_section
13053 && bed->elf_backend_elfsym_local_is_section (abfd))
13054 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13055
13056 /* Allocate some memory to hold information read in from the input
13057 files. */
13058 if (max_contents_size != 0)
13059 {
13060 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
13061 if (flinfo.contents == NULL)
13062 goto error_return;
13063 }
13064
13065 if (max_external_reloc_size != 0)
13066 {
13067 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13068 if (flinfo.external_relocs == NULL)
13069 goto error_return;
13070 }
13071
13072 if (max_internal_reloc_count != 0)
13073 {
13074 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
13075 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13076 if (flinfo.internal_relocs == NULL)
13077 goto error_return;
13078 }
13079
13080 if (max_sym_count != 0)
13081 {
13082 amt = max_sym_count * bed->s->sizeof_sym;
13083 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13084 if (flinfo.external_syms == NULL)
13085 goto error_return;
13086
13087 amt = max_sym_count * sizeof (Elf_Internal_Sym);
13088 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13089 if (flinfo.internal_syms == NULL)
13090 goto error_return;
13091
13092 amt = max_sym_count * sizeof (long);
13093 flinfo.indices = (long int *) bfd_malloc (amt);
13094 if (flinfo.indices == NULL)
13095 goto error_return;
13096
13097 amt = max_sym_count * sizeof (asection *);
13098 flinfo.sections = (asection **) bfd_malloc (amt);
13099 if (flinfo.sections == NULL)
13100 goto error_return;
13101 }
13102
13103 if (max_sym_shndx_count != 0)
13104 {
13105 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13106 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13107 if (flinfo.locsym_shndx == NULL)
13108 goto error_return;
13109 }
13110
13111 if (htab->tls_sec)
13112 {
13113 bfd_vma base, end = 0; /* Both bytes. */
13114 asection *sec;
13115
13116 for (sec = htab->tls_sec;
13117 sec && (sec->flags & SEC_THREAD_LOCAL);
13118 sec = sec->next)
13119 {
13120 bfd_size_type size = sec->size;
13121 unsigned int opb = bfd_octets_per_byte (abfd, sec);
13122
13123 if (size == 0
13124 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13125 {
13126 struct bfd_link_order *ord = sec->map_tail.link_order;
13127
13128 if (ord != NULL)
13129 size = ord->offset * opb + ord->size;
13130 }
13131 end = sec->vma + size / opb;
13132 }
13133 base = htab->tls_sec->vma;
13134 /* Only align end of TLS section if static TLS doesn't have special
13135 alignment requirements. */
13136 if (bed->static_tls_alignment == 1)
13137 end = align_power (end, htab->tls_sec->alignment_power);
13138 htab->tls_size = end - base;
13139 }
13140
13141 if (!_bfd_elf_fixup_eh_frame_hdr (info))
13142 return false;
13143
13144 /* Finish relative relocations here after regular symbol processing
13145 is finished if DT_RELR is enabled. */
13146 if (info->enable_dt_relr
13147 && bed->finish_relative_relocs
13148 && !bed->finish_relative_relocs (info))
13149 info->callbacks->fatal
13150 (_("%P: %pB: failed to finish relative relocations\n"), abfd);
13151
13152 /* Since ELF permits relocations to be against local symbols, we
13153 must have the local symbols available when we do the relocations.
13154 Since we would rather only read the local symbols once, and we
13155 would rather not keep them in memory, we handle all the
13156 relocations for a single input file at the same time.
13157
13158 Unfortunately, there is no way to know the total number of local
13159 symbols until we have seen all of them, and the local symbol
13160 indices precede the global symbol indices. This means that when
13161 we are generating relocatable output, and we see a reloc against
13162 a global symbol, we can not know the symbol index until we have
13163 finished examining all the local symbols to see which ones we are
13164 going to output. To deal with this, we keep the relocations in
13165 memory, and don't output them until the end of the link. This is
13166 an unfortunate waste of memory, but I don't see a good way around
13167 it. Fortunately, it only happens when performing a relocatable
13168 link, which is not the common case. FIXME: If keep_memory is set
13169 we could write the relocs out and then read them again; I don't
13170 know how bad the memory loss will be. */
13171
13172 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13173 sub->output_has_begun = false;
13174 for (o = abfd->sections; o != NULL; o = o->next)
13175 {
13176 for (p = o->map_head.link_order; p != NULL; p = p->next)
13177 {
13178 if (p->type == bfd_indirect_link_order
13179 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13180 == bfd_target_elf_flavour)
13181 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13182 {
13183 if (! sub->output_has_begun)
13184 {
13185 if (! elf_link_input_bfd (&flinfo, sub))
13186 goto error_return;
13187 sub->output_has_begun = true;
13188 }
13189 }
13190 else if (p->type == bfd_section_reloc_link_order
13191 || p->type == bfd_symbol_reloc_link_order)
13192 {
13193 if (! elf_reloc_link_order (abfd, info, o, p))
13194 goto error_return;
13195 }
13196 else
13197 {
13198 if (! _bfd_default_link_order (abfd, info, o, p))
13199 {
13200 if (p->type == bfd_indirect_link_order
13201 && (bfd_get_flavour (sub)
13202 == bfd_target_elf_flavour)
13203 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13204 != bed->s->elfclass))
13205 {
13206 const char *iclass, *oclass;
13207
13208 switch (bed->s->elfclass)
13209 {
13210 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13211 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13212 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13213 default: abort ();
13214 }
13215
13216 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13217 {
13218 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13219 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13220 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13221 default: abort ();
13222 }
13223
13224 bfd_set_error (bfd_error_wrong_format);
13225 _bfd_error_handler
13226 /* xgettext:c-format */
13227 (_("%pB: file class %s incompatible with %s"),
13228 sub, iclass, oclass);
13229 }
13230
13231 goto error_return;
13232 }
13233 }
13234 }
13235 }
13236
13237 /* Free symbol buffer if needed. */
13238 if (!info->reduce_memory_overheads)
13239 {
13240 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13241 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13242 {
13243 free (elf_tdata (sub)->symbuf);
13244 elf_tdata (sub)->symbuf = NULL;
13245 }
13246 }
13247
13248 /* Output any global symbols that got converted to local in a
13249 version script or due to symbol visibility. We do this in a
13250 separate step since ELF requires all local symbols to appear
13251 prior to any global symbols. FIXME: We should only do this if
13252 some global symbols were, in fact, converted to become local.
13253 FIXME: Will this work correctly with the Irix 5 linker? */
13254 eoinfo.failed = false;
13255 eoinfo.flinfo = &flinfo;
13256 eoinfo.localsyms = true;
13257 eoinfo.file_sym_done = false;
13258 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13259 if (eoinfo.failed)
13260 goto error_return;
13261
13262 /* If backend needs to output some local symbols not present in the hash
13263 table, do it now. */
13264 if (bed->elf_backend_output_arch_local_syms)
13265 {
13266 if (! ((*bed->elf_backend_output_arch_local_syms)
13267 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13268 goto error_return;
13269 }
13270
13271 /* That wrote out all the local symbols. Finish up the symbol table
13272 with the global symbols. Even if we want to strip everything we
13273 can, we still need to deal with those global symbols that got
13274 converted to local in a version script. */
13275
13276 /* The sh_info field records the index of the first non local symbol. */
13277 if (!symtab_hdr->sh_info)
13278 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13279
13280 if (dynamic
13281 && htab->dynsym != NULL
13282 && htab->dynsym->output_section != bfd_abs_section_ptr)
13283 {
13284 Elf_Internal_Sym sym;
13285 bfd_byte *dynsym = htab->dynsym->contents;
13286
13287 o = htab->dynsym->output_section;
13288 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13289
13290 /* Write out the section symbols for the output sections. */
13291 if (bfd_link_pic (info)
13292 || htab->is_relocatable_executable)
13293 {
13294 asection *s;
13295
13296 sym.st_size = 0;
13297 sym.st_name = 0;
13298 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13299 sym.st_other = 0;
13300 sym.st_target_internal = 0;
13301
13302 for (s = abfd->sections; s != NULL; s = s->next)
13303 {
13304 int indx;
13305 bfd_byte *dest;
13306 long dynindx;
13307
13308 dynindx = elf_section_data (s)->dynindx;
13309 if (dynindx <= 0)
13310 continue;
13311 indx = elf_section_data (s)->this_idx;
13312 BFD_ASSERT (indx > 0);
13313 sym.st_shndx = indx;
13314 if (! check_dynsym (abfd, &sym))
13315 goto error_return;
13316 sym.st_value = s->vma;
13317 dest = dynsym + dynindx * bed->s->sizeof_sym;
13318
13319 /* Inform the linker of the addition of this symbol. */
13320
13321 if (info->callbacks->ctf_new_dynsym)
13322 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13323
13324 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13325 }
13326 }
13327
13328 /* Write out the local dynsyms. */
13329 if (htab->dynlocal)
13330 {
13331 struct elf_link_local_dynamic_entry *e;
13332 for (e = htab->dynlocal; e ; e = e->next)
13333 {
13334 asection *s;
13335 bfd_byte *dest;
13336
13337 /* Copy the internal symbol and turn off visibility.
13338 Note that we saved a word of storage and overwrote
13339 the original st_name with the dynstr_index. */
13340 sym = e->isym;
13341 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13342 sym.st_shndx = SHN_UNDEF;
13343
13344 s = bfd_section_from_elf_index (e->input_bfd,
13345 e->isym.st_shndx);
13346 if (s != NULL
13347 && s->output_section != NULL
13348 && elf_section_data (s->output_section) != NULL)
13349 {
13350 sym.st_shndx =
13351 elf_section_data (s->output_section)->this_idx;
13352 if (! check_dynsym (abfd, &sym))
13353 goto error_return;
13354 sym.st_value = (s->output_section->vma
13355 + s->output_offset
13356 + e->isym.st_value);
13357 }
13358
13359 /* Inform the linker of the addition of this symbol. */
13360
13361 if (info->callbacks->ctf_new_dynsym)
13362 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13363
13364 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13365 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13366 }
13367 }
13368 }
13369
13370 /* We get the global symbols from the hash table. */
13371 eoinfo.failed = false;
13372 eoinfo.localsyms = false;
13373 eoinfo.flinfo = &flinfo;
13374 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13375 if (eoinfo.failed)
13376 goto error_return;
13377
13378 /* If backend needs to output some symbols not present in the hash
13379 table, do it now. */
13380 if (bed->elf_backend_output_arch_syms
13381 && (info->strip != strip_all || emit_relocs))
13382 {
13383 if (! ((*bed->elf_backend_output_arch_syms)
13384 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13385 goto error_return;
13386 }
13387
13388 /* Finalize the .strtab section. */
13389 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13390
13391 /* Swap out the .strtab section. */
13392 if (!elf_link_swap_symbols_out (&flinfo))
13393 goto error_return;
13394 free (htab->strtab);
13395 htab->strtab = NULL;
13396
13397 /* Now we know the size of the symtab section. */
13398 if (bfd_get_symcount (abfd) > 0)
13399 {
13400 /* Finish up and write out the symbol string table (.strtab)
13401 section. */
13402 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13403 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13404
13405 if (elf_symtab_shndx_list (abfd))
13406 {
13407 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13408
13409 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13410 {
13411 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13412 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13413 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13414 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13415 symtab_shndx_hdr->sh_size = amt;
13416
13417 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13418 off, true, 0);
13419
13420 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13421 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13422 goto error_return;
13423 }
13424 }
13425
13426 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13427 /* sh_name was set in prep_headers. */
13428 symstrtab_hdr->sh_type = SHT_STRTAB;
13429 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13430 symstrtab_hdr->sh_addr = 0;
13431 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13432 symstrtab_hdr->sh_entsize = 0;
13433 symstrtab_hdr->sh_link = 0;
13434 symstrtab_hdr->sh_info = 0;
13435 /* sh_offset is set just below. */
13436 symstrtab_hdr->sh_addralign = 1;
13437
13438 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13439 off, true, 0);
13440 elf_next_file_pos (abfd) = off;
13441
13442 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13443 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13444 goto error_return;
13445 }
13446
13447 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13448 {
13449 _bfd_error_handler (_("%pB: failed to generate import library"),
13450 info->out_implib_bfd);
13451 goto error_return;
13452 }
13453
13454 /* Adjust the relocs to have the correct symbol indices. */
13455 for (o = abfd->sections; o != NULL; o = o->next)
13456 {
13457 struct bfd_elf_section_data *esdo = elf_section_data (o);
13458 bool sort;
13459
13460 if ((o->flags & SEC_RELOC) == 0)
13461 continue;
13462
13463 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13464 if (esdo->rel.hdr != NULL
13465 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13466 goto error_return;
13467 if (esdo->rela.hdr != NULL
13468 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13469 goto error_return;
13470
13471 /* Set the reloc_count field to 0 to prevent write_relocs from
13472 trying to swap the relocs out itself. */
13473 o->reloc_count = 0;
13474 }
13475
13476 relativecount = 0;
13477 if (dynamic && info->combreloc && dynobj != NULL)
13478 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13479
13480 relr_entsize = 0;
13481 if (htab->srelrdyn != NULL
13482 && htab->srelrdyn->output_section != NULL
13483 && htab->srelrdyn->size != 0)
13484 {
13485 asection *s = htab->srelrdyn->output_section;
13486 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13487 if (relr_entsize == 0)
13488 {
13489 relr_entsize = bed->s->arch_size / 8;
13490 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13491 }
13492 }
13493
13494 /* If we are linking against a dynamic object, or generating a
13495 shared library, finish up the dynamic linking information. */
13496 if (dynamic)
13497 {
13498 bfd_byte *dyncon, *dynconend;
13499
13500 /* Fix up .dynamic entries. */
13501 o = htab->dynamic;
13502 BFD_ASSERT (o != NULL);
13503
13504 dyncon = o->contents;
13505 dynconend = PTR_ADD (o->contents, o->size);
13506 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13507 {
13508 Elf_Internal_Dyn dyn;
13509 const char *name;
13510 unsigned int type;
13511 bfd_size_type sh_size;
13512 bfd_vma sh_addr;
13513
13514 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13515
13516 switch (dyn.d_tag)
13517 {
13518 default:
13519 continue;
13520 case DT_NULL:
13521 if (relativecount != 0)
13522 {
13523 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13524 {
13525 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13526 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13527 }
13528 if (dyn.d_tag != DT_NULL
13529 && dynconend - dyncon >= bed->s->sizeof_dyn)
13530 {
13531 dyn.d_un.d_val = relativecount;
13532 relativecount = 0;
13533 break;
13534 }
13535 relativecount = 0;
13536 }
13537 if (relr_entsize != 0)
13538 {
13539 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13540 {
13541 asection *s = htab->srelrdyn;
13542 dyn.d_tag = DT_RELR;
13543 dyn.d_un.d_ptr
13544 = s->output_section->vma + s->output_offset;
13545 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13546 dyncon += bed->s->sizeof_dyn;
13547
13548 dyn.d_tag = DT_RELRSZ;
13549 dyn.d_un.d_val = s->size;
13550 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13551 dyncon += bed->s->sizeof_dyn;
13552
13553 dyn.d_tag = DT_RELRENT;
13554 dyn.d_un.d_val = relr_entsize;
13555 relr_entsize = 0;
13556 break;
13557 }
13558 relr_entsize = 0;
13559 }
13560 continue;
13561
13562 case DT_INIT:
13563 name = info->init_function;
13564 goto get_sym;
13565 case DT_FINI:
13566 name = info->fini_function;
13567 get_sym:
13568 {
13569 struct elf_link_hash_entry *h;
13570
13571 h = elf_link_hash_lookup (htab, name, false, false, true);
13572 if (h != NULL
13573 && (h->root.type == bfd_link_hash_defined
13574 || h->root.type == bfd_link_hash_defweak))
13575 {
13576 dyn.d_un.d_ptr = h->root.u.def.value;
13577 o = h->root.u.def.section;
13578 if (o->output_section != NULL)
13579 dyn.d_un.d_ptr += (o->output_section->vma
13580 + o->output_offset);
13581 else
13582 {
13583 /* The symbol is imported from another shared
13584 library and does not apply to this one. */
13585 dyn.d_un.d_ptr = 0;
13586 }
13587 break;
13588 }
13589 }
13590 continue;
13591
13592 case DT_PREINIT_ARRAYSZ:
13593 name = ".preinit_array";
13594 goto get_out_size;
13595 case DT_INIT_ARRAYSZ:
13596 name = ".init_array";
13597 goto get_out_size;
13598 case DT_FINI_ARRAYSZ:
13599 name = ".fini_array";
13600 get_out_size:
13601 o = bfd_get_section_by_name (abfd, name);
13602 if (o == NULL)
13603 {
13604 _bfd_error_handler
13605 (_("could not find section %s"), name);
13606 goto error_return;
13607 }
13608 if (o->size == 0)
13609 _bfd_error_handler
13610 (_("warning: %s section has zero size"), name);
13611 dyn.d_un.d_val = o->size;
13612 break;
13613
13614 case DT_PREINIT_ARRAY:
13615 name = ".preinit_array";
13616 goto get_out_vma;
13617 case DT_INIT_ARRAY:
13618 name = ".init_array";
13619 goto get_out_vma;
13620 case DT_FINI_ARRAY:
13621 name = ".fini_array";
13622 get_out_vma:
13623 o = bfd_get_section_by_name (abfd, name);
13624 goto do_vma;
13625
13626 case DT_HASH:
13627 name = ".hash";
13628 goto get_vma;
13629 case DT_GNU_HASH:
13630 name = ".gnu.hash";
13631 goto get_vma;
13632 case DT_STRTAB:
13633 name = ".dynstr";
13634 goto get_vma;
13635 case DT_SYMTAB:
13636 name = ".dynsym";
13637 goto get_vma;
13638 case DT_VERDEF:
13639 name = ".gnu.version_d";
13640 goto get_vma;
13641 case DT_VERNEED:
13642 name = ".gnu.version_r";
13643 goto get_vma;
13644 case DT_VERSYM:
13645 name = ".gnu.version";
13646 get_vma:
13647 o = bfd_get_linker_section (dynobj, name);
13648 do_vma:
13649 if (o == NULL || bfd_is_abs_section (o->output_section))
13650 {
13651 _bfd_error_handler
13652 (_("could not find section %s"), name);
13653 goto error_return;
13654 }
13655 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13656 {
13657 _bfd_error_handler
13658 (_("warning: section '%s' is being made into a note"), name);
13659 bfd_set_error (bfd_error_nonrepresentable_section);
13660 goto error_return;
13661 }
13662 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13663 break;
13664
13665 case DT_REL:
13666 case DT_RELA:
13667 case DT_RELSZ:
13668 case DT_RELASZ:
13669 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13670 type = SHT_REL;
13671 else
13672 type = SHT_RELA;
13673 sh_size = 0;
13674 sh_addr = 0;
13675 for (i = 1; i < elf_numsections (abfd); i++)
13676 {
13677 Elf_Internal_Shdr *hdr;
13678
13679 hdr = elf_elfsections (abfd)[i];
13680 if (hdr->sh_type == type
13681 && (hdr->sh_flags & SHF_ALLOC) != 0)
13682 {
13683 sh_size += hdr->sh_size;
13684 if (sh_addr == 0
13685 || sh_addr > hdr->sh_addr)
13686 sh_addr = hdr->sh_addr;
13687 }
13688 }
13689
13690 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13691 {
13692 unsigned int opb = bfd_octets_per_byte (abfd, o);
13693
13694 /* Don't count procedure linkage table relocs in the
13695 overall reloc count. */
13696 sh_size -= htab->srelplt->size;
13697 if (sh_size == 0)
13698 /* If the size is zero, make the address zero too.
13699 This is to avoid a glibc bug. If the backend
13700 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13701 zero, then we'll put DT_RELA at the end of
13702 DT_JMPREL. glibc will interpret the end of
13703 DT_RELA matching the end of DT_JMPREL as the
13704 case where DT_RELA includes DT_JMPREL, and for
13705 LD_BIND_NOW will decide that processing DT_RELA
13706 will process the PLT relocs too. Net result:
13707 No PLT relocs applied. */
13708 sh_addr = 0;
13709
13710 /* If .rela.plt is the first .rela section, exclude
13711 it from DT_RELA. */
13712 else if (sh_addr == (htab->srelplt->output_section->vma
13713 + htab->srelplt->output_offset) * opb)
13714 sh_addr += htab->srelplt->size;
13715 }
13716
13717 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13718 dyn.d_un.d_val = sh_size;
13719 else
13720 dyn.d_un.d_ptr = sh_addr;
13721 break;
13722 }
13723 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13724 }
13725 }
13726
13727 /* If we have created any dynamic sections, then output them. */
13728 if (dynobj != NULL)
13729 {
13730 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13731 goto error_return;
13732
13733 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13734 if (bfd_link_textrel_check (info)
13735 && (o = htab->dynamic) != NULL
13736 && o->size != 0)
13737 {
13738 bfd_byte *dyncon, *dynconend;
13739
13740 dyncon = o->contents;
13741 dynconend = o->contents + o->size;
13742 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13743 {
13744 Elf_Internal_Dyn dyn;
13745
13746 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13747
13748 if (dyn.d_tag == DT_TEXTREL)
13749 {
13750 if (info->textrel_check == textrel_check_error)
13751 info->callbacks->einfo
13752 (_("%P%X: read-only segment has dynamic relocations\n"));
13753 else if (bfd_link_dll (info))
13754 info->callbacks->einfo
13755 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13756 else if (bfd_link_pde (info))
13757 info->callbacks->einfo
13758 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13759 else
13760 info->callbacks->einfo
13761 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13762 break;
13763 }
13764 }
13765 }
13766
13767 for (o = dynobj->sections; o != NULL; o = o->next)
13768 {
13769 if ((o->flags & SEC_HAS_CONTENTS) == 0
13770 || o->size == 0
13771 || o->output_section == bfd_abs_section_ptr)
13772 continue;
13773 if ((o->flags & SEC_LINKER_CREATED) == 0)
13774 {
13775 /* At this point, we are only interested in sections
13776 created by _bfd_elf_link_create_dynamic_sections. */
13777 continue;
13778 }
13779 if (htab->stab_info.stabstr == o)
13780 continue;
13781 if (htab->eh_info.hdr_sec == o)
13782 continue;
13783 if (strcmp (o->name, ".dynstr") != 0)
13784 {
13785 bfd_size_type octets = ((file_ptr) o->output_offset
13786 * bfd_octets_per_byte (abfd, o));
13787 if (!bfd_set_section_contents (abfd, o->output_section,
13788 o->contents, octets, o->size))
13789 goto error_return;
13790 }
13791 else
13792 {
13793 /* The contents of the .dynstr section are actually in a
13794 stringtab. */
13795 file_ptr off;
13796
13797 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13798 if (bfd_seek (abfd, off, SEEK_SET) != 0
13799 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13800 goto error_return;
13801 }
13802 }
13803 }
13804
13805 if (!info->resolve_section_groups)
13806 {
13807 bool failed = false;
13808
13809 BFD_ASSERT (bfd_link_relocatable (info));
13810 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13811 if (failed)
13812 goto error_return;
13813 }
13814
13815 /* If we have optimized stabs strings, output them. */
13816 if (htab->stab_info.stabstr != NULL)
13817 {
13818 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13819 goto error_return;
13820 }
13821
13822 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13823 goto error_return;
13824
13825 if (! _bfd_elf_write_section_sframe (abfd, info))
13826 goto error_return;
13827
13828 if (! _bfd_elf_write_section_build_attributes (abfd, info))
13829 goto error_ret2;
13830
13831 if (info->callbacks->emit_ctf)
13832 info->callbacks->emit_ctf ();
13833
13834 elf_final_link_free (abfd, &flinfo);
13835
13836 if (info->unique_symbol)
13837 bfd_hash_table_free (&flinfo.local_hash_table);
13838 return true;
13839
13840 error_return:
13841 free (htab->strtab);
13842 htab->strtab = NULL;
13843 elf_final_link_free (abfd, &flinfo);
13844 error_ret2:
13845 if (info->unique_symbol)
13846 bfd_hash_table_free (&flinfo.local_hash_table);
13847 return false;
13848 }
13849 \f
13850 /* Initialize COOKIE for input bfd ABFD. */
13851
13852 static bool
13853 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13854 struct bfd_link_info *info, bfd *abfd,
13855 bool keep_memory)
13856 {
13857 Elf_Internal_Shdr *symtab_hdr;
13858 const struct elf_backend_data *bed;
13859
13860 bed = get_elf_backend_data (abfd);
13861 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13862
13863 cookie->abfd = abfd;
13864 cookie->sym_hashes = elf_sym_hashes (abfd);
13865 cookie->bad_symtab = elf_bad_symtab (abfd);
13866 if (cookie->bad_symtab)
13867 {
13868 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13869 cookie->extsymoff = 0;
13870 }
13871 else
13872 {
13873 cookie->locsymcount = symtab_hdr->sh_info;
13874 cookie->extsymoff = symtab_hdr->sh_info;
13875 }
13876
13877 if (bed->s->arch_size == 32)
13878 cookie->r_sym_shift = 8;
13879 else
13880 cookie->r_sym_shift = 32;
13881
13882 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13883 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13884 {
13885 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13886 cookie->locsymcount, 0,
13887 NULL, NULL, NULL);
13888 if (cookie->locsyms == NULL)
13889 {
13890 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13891 return false;
13892 }
13893 if (keep_memory || _bfd_elf_link_keep_memory (info))
13894 {
13895 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13896 info->cache_size += (cookie->locsymcount
13897 * sizeof (Elf_Internal_Sym));
13898 }
13899 }
13900 return true;
13901 }
13902
13903 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13904
13905 static void
13906 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13907 {
13908 Elf_Internal_Shdr *symtab_hdr;
13909
13910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13911 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13912 free (cookie->locsyms);
13913 }
13914
13915 /* Initialize the relocation information in COOKIE for input section SEC
13916 of input bfd ABFD. */
13917
13918 static bool
13919 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13920 struct bfd_link_info *info, bfd *abfd,
13921 asection *sec, bool keep_memory)
13922 {
13923 if (sec->reloc_count == 0)
13924 {
13925 cookie->rels = NULL;
13926 cookie->relend = NULL;
13927 }
13928 else
13929 {
13930 cookie->rels = _bfd_elf_link_info_read_relocs
13931 (abfd, info, sec, NULL, NULL,
13932 keep_memory || _bfd_elf_link_keep_memory (info));
13933 if (cookie->rels == NULL)
13934 return false;
13935 cookie->rel = cookie->rels;
13936 cookie->relend = cookie->rels + sec->reloc_count;
13937 }
13938 cookie->rel = cookie->rels;
13939 return true;
13940 }
13941
13942 /* Free the memory allocated by init_reloc_cookie_rels,
13943 if appropriate. */
13944
13945 static void
13946 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13947 asection *sec)
13948 {
13949 if (elf_section_data (sec)->relocs != cookie->rels)
13950 free (cookie->rels);
13951 }
13952
13953 /* Initialize the whole of COOKIE for input section SEC. */
13954
13955 static bool
13956 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13957 struct bfd_link_info *info,
13958 asection *sec, bool keep_memory)
13959 {
13960 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13961 goto error1;
13962 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13963 keep_memory))
13964 goto error2;
13965 return true;
13966
13967 error2:
13968 fini_reloc_cookie (cookie, sec->owner);
13969 error1:
13970 return false;
13971 }
13972
13973 /* Free the memory allocated by init_reloc_cookie_for_section,
13974 if appropriate. */
13975
13976 static void
13977 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13978 asection *sec)
13979 {
13980 fini_reloc_cookie_rels (cookie, sec);
13981 fini_reloc_cookie (cookie, sec->owner);
13982 }
13983 \f
13984 /* Garbage collect unused sections. */
13985
13986 /* Default gc_mark_hook. */
13987
13988 asection *
13989 _bfd_elf_gc_mark_hook (asection *sec,
13990 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13991 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13992 struct elf_link_hash_entry *h,
13993 Elf_Internal_Sym *sym)
13994 {
13995 if (h == NULL)
13996 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13997
13998 switch (h->root.type)
13999 {
14000 case bfd_link_hash_defined:
14001 case bfd_link_hash_defweak:
14002 return h->root.u.def.section;
14003
14004 case bfd_link_hash_common:
14005 return h->root.u.c.p->section;
14006
14007 default:
14008 return NULL;
14009 }
14010 }
14011
14012 /* Return the debug definition section. */
14013
14014 static asection *
14015 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14016 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14017 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
14018 struct elf_link_hash_entry *h,
14019 Elf_Internal_Sym *sym)
14020 {
14021 if (h != NULL)
14022 {
14023 /* Return the global debug definition section. */
14024 if ((h->root.type == bfd_link_hash_defined
14025 || h->root.type == bfd_link_hash_defweak)
14026 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14027 return h->root.u.def.section;
14028 }
14029 else
14030 {
14031 /* Return the local debug definition section. */
14032 asection *isec = bfd_section_from_elf_index (sec->owner,
14033 sym->st_shndx);
14034 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
14035 return isec;
14036 }
14037
14038 return NULL;
14039 }
14040
14041 /* COOKIE->rel describes a relocation against section SEC, which is
14042 a section we've decided to keep. Return the section that contains
14043 the relocation symbol, or NULL if no section contains it. */
14044
14045 asection *
14046 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14047 elf_gc_mark_hook_fn gc_mark_hook,
14048 struct elf_reloc_cookie *cookie,
14049 bool *start_stop)
14050 {
14051 unsigned long r_symndx;
14052 struct elf_link_hash_entry *h, *hw;
14053
14054 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
14055 if (r_symndx == STN_UNDEF)
14056 return NULL;
14057
14058 h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14059 if (h == NULL)
14060 {
14061 /* A corrupt input file can lead to a situation where the index
14062 does not reference either a local or an external symbol. */
14063 if (r_symndx >= cookie->locsymcount)
14064 return NULL;
14065
14066 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14067 &cookie->locsyms[r_symndx]);
14068 }
14069
14070 bool was_marked = h->mark;
14071
14072 h->mark = 1;
14073 /* Keep all aliases of the symbol too. If an object symbol
14074 needs to be copied into .dynbss then all of its aliases
14075 should be present as dynamic symbols, not just the one used
14076 on the copy relocation. */
14077 hw = h;
14078 while (hw->is_weakalias)
14079 {
14080 hw = hw->u.alias;
14081 hw->mark = 1;
14082 }
14083
14084 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14085 {
14086 if (info->start_stop_gc)
14087 return NULL;
14088
14089 /* To work around a glibc bug, mark XXX input sections
14090 when there is a reference to __start_XXX or __stop_XXX
14091 symbols. */
14092 else if (start_stop != NULL)
14093 {
14094 asection *s = h->u2.start_stop_section;
14095 *start_stop = true;
14096 return s;
14097 }
14098 }
14099
14100 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14101 }
14102
14103 /* COOKIE->rel describes a relocation against section SEC, which is
14104 a section we've decided to keep. Mark the section that contains
14105 the relocation symbol. */
14106
14107 bool
14108 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14109 asection *sec,
14110 elf_gc_mark_hook_fn gc_mark_hook,
14111 struct elf_reloc_cookie *cookie)
14112 {
14113 asection *rsec;
14114 bool start_stop = false;
14115
14116 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14117 while (rsec != NULL)
14118 {
14119 if (!rsec->gc_mark)
14120 {
14121 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14122 || (rsec->owner->flags & DYNAMIC) != 0)
14123 rsec->gc_mark = 1;
14124 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14125 return false;
14126 }
14127 if (!start_stop)
14128 break;
14129 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14130 }
14131 return true;
14132 }
14133
14134 /* The mark phase of garbage collection. For a given section, mark
14135 it and any sections in this section's group, and all the sections
14136 which define symbols to which it refers. */
14137
14138 bool
14139 _bfd_elf_gc_mark (struct bfd_link_info *info,
14140 asection *sec,
14141 elf_gc_mark_hook_fn gc_mark_hook)
14142 {
14143 bool ret;
14144 asection *group_sec, *eh_frame;
14145
14146 sec->gc_mark = 1;
14147
14148 /* Mark all the sections in the group. */
14149 group_sec = elf_section_data (sec)->next_in_group;
14150 if (group_sec && !group_sec->gc_mark)
14151 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14152 return false;
14153
14154 /* Look through the section relocs. */
14155 ret = true;
14156 eh_frame = elf_eh_frame_section (sec->owner);
14157 if ((sec->flags & SEC_RELOC) != 0
14158 && sec->reloc_count > 0
14159 && sec != eh_frame)
14160 {
14161 struct elf_reloc_cookie cookie;
14162
14163 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14164 ret = false;
14165 else
14166 {
14167 for (; cookie.rel < cookie.relend; cookie.rel++)
14168 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14169 {
14170 ret = false;
14171 break;
14172 }
14173 fini_reloc_cookie_for_section (&cookie, sec);
14174 }
14175 }
14176
14177 if (ret && eh_frame && elf_fde_list (sec))
14178 {
14179 struct elf_reloc_cookie cookie;
14180
14181 /* NB: When --no-keep-memory is used, the symbol table and
14182 relocation info for eh_frame are freed after they are retrieved
14183 for each text section in the input object. If an input object
14184 has many text sections, the same data is retrieved and freed
14185 many times which can take a very long time. Always keep the
14186 symbol table and relocation info for eh_frame to avoid it. */
14187 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14188 true))
14189 ret = false;
14190 else
14191 {
14192 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14193 gc_mark_hook, &cookie))
14194 ret = false;
14195 fini_reloc_cookie_for_section (&cookie, eh_frame);
14196 }
14197 }
14198
14199 eh_frame = elf_section_eh_frame_entry (sec);
14200 if (ret && eh_frame && !eh_frame->gc_mark)
14201 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14202 ret = false;
14203
14204 return ret;
14205 }
14206
14207 /* Scan and mark sections in a special or debug section group. */
14208
14209 static void
14210 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14211 {
14212 /* Point to first section of section group. */
14213 asection *ssec;
14214 /* Used to iterate the section group. */
14215 asection *msec;
14216
14217 bool is_special_grp = true;
14218 bool is_debug_grp = true;
14219
14220 /* First scan to see if group contains any section other than debug
14221 and special section. */
14222 ssec = msec = elf_next_in_group (grp);
14223 do
14224 {
14225 if ((msec->flags & SEC_DEBUGGING) == 0)
14226 is_debug_grp = false;
14227
14228 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14229 is_special_grp = false;
14230
14231 msec = elf_next_in_group (msec);
14232 }
14233 while (msec != ssec);
14234
14235 /* If this is a pure debug section group or pure special section group,
14236 keep all sections in this group. */
14237 if (is_debug_grp || is_special_grp)
14238 {
14239 do
14240 {
14241 msec->gc_mark = 1;
14242 msec = elf_next_in_group (msec);
14243 }
14244 while (msec != ssec);
14245 }
14246 }
14247
14248 /* Keep debug and special sections. */
14249
14250 bool
14251 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14252 elf_gc_mark_hook_fn mark_hook)
14253 {
14254 bfd *ibfd;
14255
14256 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14257 {
14258 asection *isec;
14259 bool some_kept;
14260 bool debug_frag_seen;
14261 bool has_kept_debug_info;
14262
14263 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14264 continue;
14265 isec = ibfd->sections;
14266 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14267 continue;
14268
14269 /* Ensure all linker created sections are kept,
14270 see if any other section is already marked,
14271 and note if we have any fragmented debug sections. */
14272 debug_frag_seen = some_kept = has_kept_debug_info = false;
14273 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14274 {
14275 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14276 isec->gc_mark = 1;
14277 else if (isec->gc_mark
14278 && (isec->flags & SEC_ALLOC) != 0
14279 && elf_section_type (isec) != SHT_NOTE)
14280 some_kept = true;
14281 else
14282 {
14283 /* Since all sections, except for backend specific ones,
14284 have been garbage collected, call mark_hook on this
14285 section if any of its linked-to sections is marked. */
14286 asection *linked_to_sec;
14287 for (linked_to_sec = elf_linked_to_section (isec);
14288 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14289 linked_to_sec = elf_linked_to_section (linked_to_sec))
14290 {
14291 if (linked_to_sec->gc_mark)
14292 {
14293 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14294 return false;
14295 break;
14296 }
14297 linked_to_sec->linker_mark = 1;
14298 }
14299 for (linked_to_sec = elf_linked_to_section (isec);
14300 linked_to_sec != NULL && linked_to_sec->linker_mark;
14301 linked_to_sec = elf_linked_to_section (linked_to_sec))
14302 linked_to_sec->linker_mark = 0;
14303 }
14304
14305 if (!debug_frag_seen
14306 && (isec->flags & SEC_DEBUGGING)
14307 && startswith (isec->name, ".debug_line."))
14308 debug_frag_seen = true;
14309 else if (strcmp (bfd_section_name (isec),
14310 "__patchable_function_entries") == 0
14311 && elf_linked_to_section (isec) == NULL)
14312 info->callbacks->fatal (_("%P: %pB(%pA): error: "
14313 "need linked-to section "
14314 "for --gc-sections\n"),
14315 isec->owner, isec);
14316 }
14317
14318 /* If no non-note alloc section in this file will be kept, then
14319 we can toss out the debug and special sections. */
14320 if (!some_kept)
14321 continue;
14322
14323 /* Keep debug and special sections like .comment when they are
14324 not part of a group. Also keep section groups that contain
14325 just debug sections or special sections. NB: Sections with
14326 linked-to section has been handled above. */
14327 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14328 {
14329 if ((isec->flags & SEC_GROUP) != 0)
14330 _bfd_elf_gc_mark_debug_special_section_group (isec);
14331 else if (((isec->flags & SEC_DEBUGGING) != 0
14332 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14333 && elf_next_in_group (isec) == NULL
14334 && elf_linked_to_section (isec) == NULL)
14335 isec->gc_mark = 1;
14336 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14337 has_kept_debug_info = true;
14338 }
14339
14340 /* Look for CODE sections which are going to be discarded,
14341 and find and discard any fragmented debug sections which
14342 are associated with that code section. */
14343 if (debug_frag_seen)
14344 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14345 if ((isec->flags & SEC_CODE) != 0
14346 && isec->gc_mark == 0)
14347 {
14348 unsigned int ilen;
14349 asection *dsec;
14350
14351 ilen = strlen (isec->name);
14352
14353 /* Association is determined by the name of the debug
14354 section containing the name of the code section as
14355 a suffix. For example .debug_line.text.foo is a
14356 debug section associated with .text.foo. */
14357 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14358 {
14359 unsigned int dlen;
14360
14361 if (dsec->gc_mark == 0
14362 || (dsec->flags & SEC_DEBUGGING) == 0)
14363 continue;
14364
14365 dlen = strlen (dsec->name);
14366
14367 if (dlen > ilen
14368 && strncmp (dsec->name + (dlen - ilen),
14369 isec->name, ilen) == 0)
14370 dsec->gc_mark = 0;
14371 }
14372 }
14373
14374 /* Mark debug sections referenced by kept debug sections. */
14375 if (has_kept_debug_info)
14376 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14377 if (isec->gc_mark
14378 && (isec->flags & SEC_DEBUGGING) != 0)
14379 if (!_bfd_elf_gc_mark (info, isec,
14380 elf_gc_mark_debug_section))
14381 return false;
14382 }
14383 return true;
14384 }
14385
14386 static bool
14387 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14388 {
14389 bfd *sub;
14390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14391
14392 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14393 {
14394 asection *o;
14395
14396 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14397 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14398 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14399 continue;
14400 o = sub->sections;
14401 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14402 continue;
14403
14404 for (o = sub->sections; o != NULL; o = o->next)
14405 {
14406 /* When any section in a section group is kept, we keep all
14407 sections in the section group. If the first member of
14408 the section group is excluded, we will also exclude the
14409 group section. */
14410 if (o->flags & SEC_GROUP)
14411 {
14412 asection *first = elf_next_in_group (o);
14413 if (first != NULL)
14414 o->gc_mark = first->gc_mark;
14415 }
14416
14417 if (o->gc_mark)
14418 continue;
14419
14420 /* Skip sweeping sections already excluded. */
14421 if (o->flags & SEC_EXCLUDE)
14422 continue;
14423
14424 /* Since this is early in the link process, it is simple
14425 to remove a section from the output. */
14426 o->flags |= SEC_EXCLUDE;
14427
14428 if (info->print_gc_sections && o->size != 0)
14429 /* xgettext:c-format */
14430 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14431 o, sub);
14432 }
14433 }
14434
14435 return true;
14436 }
14437
14438 /* Propagate collected vtable information. This is called through
14439 elf_link_hash_traverse. */
14440
14441 static bool
14442 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14443 {
14444 /* Those that are not vtables. */
14445 if (h->start_stop
14446 || h->u2.vtable == NULL
14447 || h->u2.vtable->parent == NULL)
14448 return true;
14449
14450 /* Those vtables that do not have parents, we cannot merge. */
14451 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14452 return true;
14453
14454 /* If we've already been done, exit. */
14455 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14456 return true;
14457
14458 /* Make sure the parent's table is up to date. */
14459 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14460
14461 if (h->u2.vtable->used == NULL)
14462 {
14463 /* None of this table's entries were referenced. Re-use the
14464 parent's table. */
14465 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14466 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14467 }
14468 else
14469 {
14470 size_t n;
14471 bool *cu, *pu;
14472
14473 /* Or the parent's entries into ours. */
14474 cu = h->u2.vtable->used;
14475 cu[-1] = true;
14476 pu = h->u2.vtable->parent->u2.vtable->used;
14477 if (pu != NULL)
14478 {
14479 const struct elf_backend_data *bed;
14480 unsigned int log_file_align;
14481
14482 bed = get_elf_backend_data (h->root.u.def.section->owner);
14483 log_file_align = bed->s->log_file_align;
14484 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14485 while (n--)
14486 {
14487 if (*pu)
14488 *cu = true;
14489 pu++;
14490 cu++;
14491 }
14492 }
14493 }
14494
14495 return true;
14496 }
14497
14498 struct link_info_ok
14499 {
14500 struct bfd_link_info *info;
14501 bool ok;
14502 };
14503
14504 static bool
14505 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14506 void *ptr)
14507 {
14508 asection *sec;
14509 bfd_vma hstart, hend;
14510 Elf_Internal_Rela *relstart, *relend, *rel;
14511 const struct elf_backend_data *bed;
14512 unsigned int log_file_align;
14513 struct link_info_ok *info = (struct link_info_ok *) ptr;
14514
14515 /* Take care of both those symbols that do not describe vtables as
14516 well as those that are not loaded. */
14517 if (h->start_stop
14518 || h->u2.vtable == NULL
14519 || h->u2.vtable->parent == NULL)
14520 return true;
14521
14522 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14523 || h->root.type == bfd_link_hash_defweak);
14524
14525 sec = h->root.u.def.section;
14526 hstart = h->root.u.def.value;
14527 hend = hstart + h->size;
14528
14529 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14530 sec, NULL, NULL, true);
14531 if (!relstart)
14532 return info->ok = false;
14533 bed = get_elf_backend_data (sec->owner);
14534 log_file_align = bed->s->log_file_align;
14535
14536 relend = relstart + sec->reloc_count;
14537
14538 for (rel = relstart; rel < relend; ++rel)
14539 if (rel->r_offset >= hstart && rel->r_offset < hend)
14540 {
14541 /* If the entry is in use, do nothing. */
14542 if (h->u2.vtable->used
14543 && (rel->r_offset - hstart) < h->u2.vtable->size)
14544 {
14545 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14546 if (h->u2.vtable->used[entry])
14547 continue;
14548 }
14549 /* Otherwise, kill it. */
14550 rel->r_offset = rel->r_info = rel->r_addend = 0;
14551 }
14552
14553 return true;
14554 }
14555
14556 /* Mark sections containing dynamically referenced symbols. When
14557 building shared libraries, we must assume that any visible symbol is
14558 referenced. */
14559
14560 bool
14561 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14562 {
14563 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14564 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14565
14566 if ((h->root.type == bfd_link_hash_defined
14567 || h->root.type == bfd_link_hash_defweak)
14568 && (!h->start_stop
14569 || h->root.ldscript_def
14570 || !info->start_stop_gc)
14571 && ((h->ref_dynamic && !h->forced_local)
14572 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14573 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14574 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14575 && (!bfd_link_executable (info)
14576 || info->gc_keep_exported
14577 || info->export_dynamic
14578 || (h->dynamic
14579 && d != NULL
14580 && (*d->match) (&d->head, NULL, h->root.root.string)))
14581 && (h->versioned >= versioned
14582 || !bfd_hide_sym_by_version (info->version_info,
14583 h->root.root.string)))))
14584 h->root.u.def.section->flags |= SEC_KEEP;
14585
14586 return true;
14587 }
14588
14589 /* Keep all sections containing symbols undefined on the command-line,
14590 and the section containing the entry symbol. */
14591
14592 void
14593 _bfd_elf_gc_keep (struct bfd_link_info *info)
14594 {
14595 struct bfd_sym_chain *sym;
14596
14597 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14598 {
14599 struct elf_link_hash_entry *h;
14600
14601 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14602 false, false, false);
14603
14604 if (h != NULL
14605 && (h->root.type == bfd_link_hash_defined
14606 || h->root.type == bfd_link_hash_defweak)
14607 && !bfd_is_const_section (h->root.u.def.section))
14608 h->root.u.def.section->flags |= SEC_KEEP;
14609 }
14610 }
14611
14612 bool
14613 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14614 struct bfd_link_info *info)
14615 {
14616 bfd *ibfd = info->input_bfds;
14617
14618 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14619 {
14620 asection *sec;
14621 struct elf_reloc_cookie cookie;
14622
14623 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14624 continue;
14625 sec = ibfd->sections;
14626 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14627 continue;
14628
14629 if (!init_reloc_cookie (&cookie, info, ibfd, false))
14630 return false;
14631
14632 for (sec = ibfd->sections; sec; sec = sec->next)
14633 {
14634 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14635 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14636 false))
14637 {
14638 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14639 fini_reloc_cookie_rels (&cookie, sec);
14640 }
14641 }
14642 }
14643 return true;
14644 }
14645
14646 /* Do mark and sweep of unused sections. */
14647
14648 bool
14649 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14650 {
14651 bool ok = true;
14652 bfd *sub;
14653 elf_gc_mark_hook_fn gc_mark_hook;
14654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14655 struct elf_link_hash_table *htab;
14656 struct link_info_ok info_ok;
14657
14658 if (!bed->can_gc_sections
14659 || !is_elf_hash_table (info->hash))
14660 {
14661 _bfd_error_handler(_("warning: gc-sections option ignored"));
14662 return true;
14663 }
14664
14665 bed->gc_keep (info);
14666 htab = elf_hash_table (info);
14667
14668 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14669 at the .eh_frame section if we can mark the FDEs individually. */
14670 for (sub = info->input_bfds;
14671 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14672 sub = sub->link.next)
14673 {
14674 asection *sec;
14675 struct elf_reloc_cookie cookie;
14676
14677 sec = sub->sections;
14678 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14679 continue;
14680 sec = bfd_get_section_by_name (sub, ".eh_frame");
14681 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14682 false))
14683 {
14684 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14685 if (elf_section_data (sec)->sec_info
14686 && (sec->flags & SEC_LINKER_CREATED) == 0)
14687 elf_eh_frame_section (sub) = sec;
14688 fini_reloc_cookie_for_section (&cookie, sec);
14689 sec = bfd_get_next_section_by_name (NULL, sec);
14690 }
14691 }
14692
14693 /* Apply transitive closure to the vtable entry usage info. */
14694 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14695 if (!ok)
14696 return false;
14697
14698 /* Kill the vtable relocations that were not used. */
14699 info_ok.info = info;
14700 info_ok.ok = true;
14701 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14702 if (!info_ok.ok)
14703 return false;
14704
14705 /* Mark dynamically referenced symbols. */
14706 if (htab->dynamic_sections_created || info->gc_keep_exported)
14707 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14708
14709 /* Grovel through relocs to find out who stays ... */
14710 gc_mark_hook = bed->gc_mark_hook;
14711 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14712 {
14713 asection *o;
14714
14715 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14716 || elf_object_id (sub) != elf_hash_table_id (htab)
14717 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14718 continue;
14719
14720 o = sub->sections;
14721 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14722 continue;
14723
14724 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14725 Also treat note sections as a root, if the section is not part
14726 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14727 well as FINI_ARRAY sections for ld -r. */
14728 for (o = sub->sections; o != NULL; o = o->next)
14729 if (!o->gc_mark
14730 && (o->flags & SEC_EXCLUDE) == 0
14731 && ((o->flags & SEC_KEEP) != 0
14732 || (bfd_link_relocatable (info)
14733 && ((elf_section_data (o)->this_hdr.sh_type
14734 == SHT_PREINIT_ARRAY)
14735 || (elf_section_data (o)->this_hdr.sh_type
14736 == SHT_INIT_ARRAY)
14737 || (elf_section_data (o)->this_hdr.sh_type
14738 == SHT_FINI_ARRAY)))
14739 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14740 && elf_next_in_group (o) == NULL
14741 && elf_linked_to_section (o) == NULL)
14742 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14743 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14744 {
14745 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14746 return false;
14747 }
14748 }
14749
14750 /* Allow the backend to mark additional target specific sections. */
14751 bed->gc_mark_extra_sections (info, gc_mark_hook);
14752
14753 /* ... and mark SEC_EXCLUDE for those that go. */
14754 return elf_gc_sweep (abfd, info);
14755 }
14756 \f
14757 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14758
14759 bool
14760 bfd_elf_gc_record_vtinherit (bfd *abfd,
14761 asection *sec,
14762 struct elf_link_hash_entry *h,
14763 bfd_vma offset)
14764 {
14765 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14766 struct elf_link_hash_entry **search, *child;
14767 size_t extsymcount;
14768 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14769
14770 /* The sh_info field of the symtab header tells us where the
14771 external symbols start. We don't care about the local symbols at
14772 this point. */
14773 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14774 if (!elf_bad_symtab (abfd))
14775 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14776
14777 sym_hashes = elf_sym_hashes (abfd);
14778 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14779
14780 /* Hunt down the child symbol, which is in this section at the same
14781 offset as the relocation. */
14782 for (search = sym_hashes; search != sym_hashes_end; ++search)
14783 {
14784 if ((child = *search) != NULL
14785 && (child->root.type == bfd_link_hash_defined
14786 || child->root.type == bfd_link_hash_defweak)
14787 && child->root.u.def.section == sec
14788 && child->root.u.def.value == offset)
14789 goto win;
14790 }
14791
14792 /* xgettext:c-format */
14793 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14794 abfd, sec, (uint64_t) offset);
14795 bfd_set_error (bfd_error_invalid_operation);
14796 return false;
14797
14798 win:
14799 if (!child->u2.vtable)
14800 {
14801 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14802 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14803 if (!child->u2.vtable)
14804 return false;
14805 }
14806 if (!h)
14807 {
14808 /* This *should* only be the absolute section. It could potentially
14809 be that someone has defined a non-global vtable though, which
14810 would be bad. It isn't worth paging in the local symbols to be
14811 sure though; that case should simply be handled by the assembler. */
14812
14813 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14814 }
14815 else
14816 child->u2.vtable->parent = h;
14817
14818 return true;
14819 }
14820
14821 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14822
14823 bool
14824 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14825 struct elf_link_hash_entry *h,
14826 bfd_vma addend)
14827 {
14828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14829 unsigned int log_file_align = bed->s->log_file_align;
14830
14831 if (!h)
14832 {
14833 /* xgettext:c-format */
14834 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14835 abfd, sec);
14836 bfd_set_error (bfd_error_bad_value);
14837 return false;
14838 }
14839
14840 if (!h->u2.vtable)
14841 {
14842 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14843 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14844 if (!h->u2.vtable)
14845 return false;
14846 }
14847
14848 if (addend >= h->u2.vtable->size)
14849 {
14850 size_t size, bytes, file_align;
14851 bool *ptr = h->u2.vtable->used;
14852
14853 /* While the symbol is undefined, we have to be prepared to handle
14854 a zero size. */
14855 file_align = 1 << log_file_align;
14856 if (h->root.type == bfd_link_hash_undefined)
14857 size = addend + file_align;
14858 else
14859 {
14860 size = h->size;
14861 if (addend >= size)
14862 {
14863 /* Oops! We've got a reference past the defined end of
14864 the table. This is probably a bug -- shall we warn? */
14865 size = addend + file_align;
14866 }
14867 }
14868 size = (size + file_align - 1) & -file_align;
14869
14870 /* Allocate one extra entry for use as a "done" flag for the
14871 consolidation pass. */
14872 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14873
14874 if (ptr)
14875 {
14876 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14877
14878 if (ptr != NULL)
14879 {
14880 size_t oldbytes;
14881
14882 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14883 * sizeof (bool));
14884 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14885 }
14886 }
14887 else
14888 ptr = (bool *) bfd_zmalloc (bytes);
14889
14890 if (ptr == NULL)
14891 return false;
14892
14893 /* And arrange for that done flag to be at index -1. */
14894 h->u2.vtable->used = ptr + 1;
14895 h->u2.vtable->size = size;
14896 }
14897
14898 h->u2.vtable->used[addend >> log_file_align] = true;
14899
14900 return true;
14901 }
14902
14903 /* Map an ELF section header flag to its corresponding string. */
14904 typedef struct
14905 {
14906 char *flag_name;
14907 flagword flag_value;
14908 } elf_flags_to_name_table;
14909
14910 static const elf_flags_to_name_table elf_flags_to_names [] =
14911 {
14912 { "SHF_WRITE", SHF_WRITE },
14913 { "SHF_ALLOC", SHF_ALLOC },
14914 { "SHF_EXECINSTR", SHF_EXECINSTR },
14915 { "SHF_MERGE", SHF_MERGE },
14916 { "SHF_STRINGS", SHF_STRINGS },
14917 { "SHF_INFO_LINK", SHF_INFO_LINK},
14918 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14919 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14920 { "SHF_GROUP", SHF_GROUP },
14921 { "SHF_TLS", SHF_TLS },
14922 { "SHF_MASKOS", SHF_MASKOS },
14923 { "SHF_EXCLUDE", SHF_EXCLUDE },
14924 };
14925
14926 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14927 bool
14928 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14929 struct flag_info *flaginfo,
14930 asection *section)
14931 {
14932 const bfd_vma sh_flags = elf_section_flags (section);
14933
14934 if (!flaginfo->flags_initialized)
14935 {
14936 bfd *obfd = info->output_bfd;
14937 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14938 struct flag_info_list *tf = flaginfo->flag_list;
14939 int with_hex = 0;
14940 int without_hex = 0;
14941
14942 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14943 {
14944 unsigned i;
14945 flagword (*lookup) (char *);
14946
14947 lookup = bed->elf_backend_lookup_section_flags_hook;
14948 if (lookup != NULL)
14949 {
14950 flagword hexval = (*lookup) ((char *) tf->name);
14951
14952 if (hexval != 0)
14953 {
14954 if (tf->with == with_flags)
14955 with_hex |= hexval;
14956 else if (tf->with == without_flags)
14957 without_hex |= hexval;
14958 tf->valid = true;
14959 continue;
14960 }
14961 }
14962 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14963 {
14964 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14965 {
14966 if (tf->with == with_flags)
14967 with_hex |= elf_flags_to_names[i].flag_value;
14968 else if (tf->with == without_flags)
14969 without_hex |= elf_flags_to_names[i].flag_value;
14970 tf->valid = true;
14971 break;
14972 }
14973 }
14974 if (!tf->valid)
14975 {
14976 info->callbacks->einfo
14977 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14978 return false;
14979 }
14980 }
14981 flaginfo->flags_initialized = true;
14982 flaginfo->only_with_flags |= with_hex;
14983 flaginfo->not_with_flags |= without_hex;
14984 }
14985
14986 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14987 return false;
14988
14989 if ((flaginfo->not_with_flags & sh_flags) != 0)
14990 return false;
14991
14992 return true;
14993 }
14994
14995 struct alloc_got_off_arg {
14996 bfd_vma gotoff;
14997 struct bfd_link_info *info;
14998 };
14999
15000 /* We need a special top-level link routine to convert got reference counts
15001 to real got offsets. */
15002
15003 static bool
15004 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15005 {
15006 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
15007 bfd *obfd = gofarg->info->output_bfd;
15008 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
15009
15010 if (h->got.refcount > 0)
15011 {
15012 h->got.offset = gofarg->gotoff;
15013 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
15014 }
15015 else
15016 h->got.offset = (bfd_vma) -1;
15017
15018 return true;
15019 }
15020
15021 /* And an accompanying bit to work out final got entry offsets once
15022 we're done. Should be called from final_link. */
15023
15024 bool
15025 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15026 struct bfd_link_info *info)
15027 {
15028 bfd *i;
15029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15030 bfd_vma gotoff;
15031 struct alloc_got_off_arg gofarg;
15032
15033 BFD_ASSERT (abfd == info->output_bfd);
15034
15035 if (! is_elf_hash_table (info->hash))
15036 return false;
15037
15038 /* The GOT offset is relative to the .got section, but the GOT header is
15039 put into the .got.plt section, if the backend uses it. */
15040 if (bed->want_got_plt)
15041 gotoff = 0;
15042 else
15043 gotoff = bed->got_header_size;
15044
15045 /* Do the local .got entries first. */
15046 for (i = info->input_bfds; i; i = i->link.next)
15047 {
15048 bfd_signed_vma *local_got;
15049 size_t j, locsymcount;
15050 Elf_Internal_Shdr *symtab_hdr;
15051
15052 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15053 continue;
15054
15055 local_got = elf_local_got_refcounts (i);
15056 if (!local_got)
15057 continue;
15058
15059 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15060 if (elf_bad_symtab (i))
15061 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15062 else
15063 locsymcount = symtab_hdr->sh_info;
15064
15065 for (j = 0; j < locsymcount; ++j)
15066 {
15067 if (local_got[j] > 0)
15068 {
15069 local_got[j] = gotoff;
15070 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15071 }
15072 else
15073 local_got[j] = (bfd_vma) -1;
15074 }
15075 }
15076
15077 /* Then the global .got entries. .plt refcounts are handled by
15078 adjust_dynamic_symbol */
15079 gofarg.gotoff = gotoff;
15080 gofarg.info = info;
15081 elf_link_hash_traverse (elf_hash_table (info),
15082 elf_gc_allocate_got_offsets,
15083 &gofarg);
15084 return true;
15085 }
15086
15087 /* Many folk need no more in the way of final link than this, once
15088 got entry reference counting is enabled. */
15089
15090 bool
15091 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15092 {
15093 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15094 return false;
15095
15096 /* Invoke the regular ELF backend linker to do all the work. */
15097 return bfd_elf_final_link (abfd, info);
15098 }
15099
15100 bool
15101 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15102 {
15103 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15104
15105 if (rcookie->bad_symtab)
15106 rcookie->rel = rcookie->rels;
15107
15108 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15109 {
15110 unsigned long r_symndx;
15111
15112 if (! rcookie->bad_symtab)
15113 if (rcookie->rel->r_offset > offset)
15114 return false;
15115 if (rcookie->rel->r_offset != offset)
15116 continue;
15117
15118 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15119 if (r_symndx == STN_UNDEF)
15120 return true;
15121
15122 struct elf_link_hash_entry *h;
15123
15124 h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
15125
15126 if (h != NULL)
15127 {
15128 if ((h->root.type == bfd_link_hash_defined
15129 || h->root.type == bfd_link_hash_defweak)
15130 && (h->root.u.def.section->owner != rcookie->abfd
15131 || h->root.u.def.section->kept_section != NULL
15132 || discarded_section (h->root.u.def.section)))
15133 return true;
15134 }
15135 else
15136 {
15137 if (r_symndx >= rcookie->locsymcount)
15138 /* This can happen with corrupt input. */
15139 return false;
15140
15141 /* It's not a relocation against a global symbol,
15142 but it could be a relocation against a local
15143 symbol for a discarded section. */
15144 asection *isec;
15145 Elf_Internal_Sym *isym;
15146
15147 /* Need to: get the symbol; get the section. */
15148 isym = &rcookie->locsyms[r_symndx];
15149 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15150 if (isec != NULL
15151 && (isec->kept_section != NULL
15152 || discarded_section (isec)))
15153 return true;
15154 }
15155
15156 return false;
15157 }
15158 return false;
15159 }
15160
15161 /* Discard unneeded references to discarded sections.
15162 Returns -1 on error, 1 if any section's size was changed, 0 if
15163 nothing changed. This function assumes that the relocations are in
15164 sorted order, which is true for all known assemblers. */
15165
15166 int
15167 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15168 {
15169 struct elf_reloc_cookie cookie;
15170 asection *o;
15171 bfd *abfd;
15172 int changed = 0;
15173
15174 if (info->traditional_format
15175 || !is_elf_hash_table (info->hash))
15176 return 0;
15177
15178 o = bfd_get_section_by_name (output_bfd, ".stab");
15179 if (o != NULL)
15180 {
15181 asection *i;
15182
15183 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15184 {
15185 if (i->size == 0
15186 || i->reloc_count == 0
15187 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15188 continue;
15189
15190 abfd = i->owner;
15191 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15192 continue;
15193
15194 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15195 return -1;
15196
15197 if (_bfd_discard_section_stabs (abfd, i,
15198 elf_section_data (i)->sec_info,
15199 bfd_elf_reloc_symbol_deleted_p,
15200 &cookie))
15201 changed = 1;
15202
15203 fini_reloc_cookie_for_section (&cookie, i);
15204 }
15205 }
15206
15207 o = NULL;
15208 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15209 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15210 if (o != NULL)
15211 {
15212 asection *i;
15213 int eh_changed = 0;
15214 unsigned int eh_alignment; /* Octets. */
15215
15216 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15217 {
15218 if (i->size == 0)
15219 continue;
15220
15221 abfd = i->owner;
15222 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15223 continue;
15224
15225 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15226 return -1;
15227
15228 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15229 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15230 bfd_elf_reloc_symbol_deleted_p,
15231 &cookie))
15232 {
15233 eh_changed = 1;
15234 if (i->size != i->rawsize)
15235 changed = 1;
15236 }
15237
15238 fini_reloc_cookie_for_section (&cookie, i);
15239 }
15240
15241 eh_alignment = ((1 << o->alignment_power)
15242 * bfd_octets_per_byte (output_bfd, o));
15243 /* Skip over zero terminator, and prevent empty sections from
15244 adding alignment padding at the end. */
15245 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15246 if (i->size == 0)
15247 i->flags |= SEC_EXCLUDE;
15248 else if (i->size > 4)
15249 break;
15250 /* The last non-empty eh_frame section doesn't need padding. */
15251 if (i != NULL)
15252 i = i->map_tail.s;
15253 /* Any prior sections must pad the last FDE out to the output
15254 section alignment. Otherwise we might have zero padding
15255 between sections, which would be seen as a terminator. */
15256 for (; i != NULL; i = i->map_tail.s)
15257 if (i->size == 4)
15258 /* All but the last zero terminator should have been removed. */
15259 BFD_FAIL ();
15260 else
15261 {
15262 bfd_size_type size
15263 = (i->size + eh_alignment - 1) & -eh_alignment;
15264 if (i->size != size)
15265 {
15266 i->size = size;
15267 changed = 1;
15268 eh_changed = 1;
15269 }
15270 }
15271 if (eh_changed)
15272 elf_link_hash_traverse (elf_hash_table (info),
15273 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15274 }
15275
15276 o = bfd_get_section_by_name (output_bfd, ".sframe");
15277 if (o != NULL)
15278 {
15279 asection *i;
15280
15281 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15282 {
15283 if (i->size == 0)
15284 continue;
15285
15286 abfd = i->owner;
15287 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15288 continue;
15289
15290 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15291 return -1;
15292
15293 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15294 {
15295 if (_bfd_elf_discard_section_sframe (i,
15296 bfd_elf_reloc_symbol_deleted_p,
15297 &cookie))
15298 {
15299 if (i->size != i->rawsize)
15300 changed = 1;
15301 }
15302 }
15303 fini_reloc_cookie_for_section (&cookie, i);
15304 }
15305 /* Update the reference to the output .sframe section. Used to
15306 determine later if PT_GNU_SFRAME segment is to be generated. */
15307 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15308 return -1;
15309 }
15310
15311 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15312 {
15313 const struct elf_backend_data *bed;
15314 asection *s;
15315
15316 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15317 continue;
15318 s = abfd->sections;
15319 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15320 continue;
15321
15322 bed = get_elf_backend_data (abfd);
15323
15324 if (bed->elf_backend_discard_info != NULL)
15325 {
15326 if (!init_reloc_cookie (&cookie, info, abfd, false))
15327 return -1;
15328
15329 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15330 changed = 1;
15331
15332 fini_reloc_cookie (&cookie, abfd);
15333 }
15334 }
15335
15336 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15337 _bfd_elf_end_eh_frame_parsing (info);
15338
15339 if (_bfd_elf_discard_section_eh_frame_hdr (info))
15340 changed = 1;
15341
15342 return changed;
15343 }
15344
15345 bool
15346 _bfd_elf_section_already_linked (bfd *abfd,
15347 asection *sec,
15348 struct bfd_link_info *info)
15349 {
15350 flagword flags;
15351 const char *name, *key;
15352 struct bfd_section_already_linked *l;
15353 struct bfd_section_already_linked_hash_entry *already_linked_list;
15354
15355 if (sec->output_section == bfd_abs_section_ptr)
15356 return false;
15357
15358 flags = sec->flags;
15359
15360 /* Return if it isn't a linkonce section. A comdat group section
15361 also has SEC_LINK_ONCE set. */
15362 if ((flags & SEC_LINK_ONCE) == 0)
15363 return false;
15364
15365 /* Don't put group member sections on our list of already linked
15366 sections. They are handled as a group via their group section. */
15367 if (elf_sec_group (sec) != NULL)
15368 return false;
15369
15370 /* For a SHT_GROUP section, use the group signature as the key. */
15371 name = sec->name;
15372 if ((flags & SEC_GROUP) != 0
15373 && elf_next_in_group (sec) != NULL
15374 && elf_group_name (elf_next_in_group (sec)) != NULL)
15375 key = elf_group_name (elf_next_in_group (sec));
15376 else
15377 {
15378 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15379 if (startswith (name, ".gnu.linkonce.")
15380 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15381 key++;
15382 else
15383 /* Must be a user linkonce section that doesn't follow gcc's
15384 naming convention. In this case we won't be matching
15385 single member groups. */
15386 key = name;
15387 }
15388
15389 already_linked_list = bfd_section_already_linked_table_lookup (key);
15390
15391 for (l = already_linked_list->entry; l != NULL; l = l->next)
15392 {
15393 /* We may have 2 different types of sections on the list: group
15394 sections with a signature of <key> (<key> is some string),
15395 and linkonce sections named .gnu.linkonce.<type>.<key>.
15396 Match like sections. LTO plugin sections are an exception.
15397 They are always named .gnu.linkonce.t.<key> and match either
15398 type of section. */
15399 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15400 && ((flags & SEC_GROUP) != 0
15401 || strcmp (name, l->sec->name) == 0))
15402 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15403 || (sec->owner->flags & BFD_PLUGIN) != 0)
15404 {
15405 /* The section has already been linked. See if we should
15406 issue a warning. */
15407 if (!_bfd_handle_already_linked (sec, l, info))
15408 return false;
15409
15410 if (flags & SEC_GROUP)
15411 {
15412 asection *first = elf_next_in_group (sec);
15413 asection *s = first;
15414
15415 while (s != NULL)
15416 {
15417 s->output_section = bfd_abs_section_ptr;
15418 /* Record which group discards it. */
15419 s->kept_section = l->sec;
15420 s = elf_next_in_group (s);
15421 /* These lists are circular. */
15422 if (s == first)
15423 break;
15424 }
15425 }
15426
15427 return true;
15428 }
15429 }
15430
15431 /* A single member comdat group section may be discarded by a
15432 linkonce section and vice versa. */
15433 if ((flags & SEC_GROUP) != 0)
15434 {
15435 asection *first = elf_next_in_group (sec);
15436
15437 if (first != NULL && elf_next_in_group (first) == first)
15438 /* Check this single member group against linkonce sections. */
15439 for (l = already_linked_list->entry; l != NULL; l = l->next)
15440 if ((l->sec->flags & SEC_GROUP) == 0
15441 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15442 {
15443 first->output_section = bfd_abs_section_ptr;
15444 first->kept_section = l->sec;
15445 sec->output_section = bfd_abs_section_ptr;
15446 break;
15447 }
15448 }
15449 else
15450 /* Check this linkonce section against single member groups. */
15451 for (l = already_linked_list->entry; l != NULL; l = l->next)
15452 if (l->sec->flags & SEC_GROUP)
15453 {
15454 asection *first = elf_next_in_group (l->sec);
15455
15456 if (first != NULL
15457 && elf_next_in_group (first) == first
15458 && bfd_elf_match_symbols_in_sections (first, sec, info))
15459 {
15460 sec->output_section = bfd_abs_section_ptr;
15461 sec->kept_section = first;
15462 break;
15463 }
15464 }
15465
15466 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15467 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15468 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15469 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15470 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15471 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15472 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15473 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15474 The reverse order cannot happen as there is never a bfd with only the
15475 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15476 matter as here were are looking only for cross-bfd sections. */
15477
15478 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15479 for (l = already_linked_list->entry; l != NULL; l = l->next)
15480 if ((l->sec->flags & SEC_GROUP) == 0
15481 && startswith (l->sec->name, ".gnu.linkonce.t."))
15482 {
15483 if (abfd != l->sec->owner)
15484 sec->output_section = bfd_abs_section_ptr;
15485 break;
15486 }
15487
15488 /* This is the first section with this name. Record it. */
15489 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15490 info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
15491 return sec->output_section == bfd_abs_section_ptr;
15492 }
15493
15494 bool
15495 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15496 {
15497 return sym->st_shndx == SHN_COMMON;
15498 }
15499
15500 unsigned int
15501 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15502 {
15503 return SHN_COMMON;
15504 }
15505
15506 asection *
15507 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15508 {
15509 return bfd_com_section_ptr;
15510 }
15511
15512 bfd_vma
15513 _bfd_elf_default_got_elt_size (bfd *abfd,
15514 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15515 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15516 bfd *ibfd ATTRIBUTE_UNUSED,
15517 unsigned long symndx ATTRIBUTE_UNUSED)
15518 {
15519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15520 return bed->s->arch_size / 8;
15521 }
15522
15523 /* Routines to support the creation of dynamic relocs. */
15524
15525 /* Returns the name of the dynamic reloc section associated with SEC. */
15526
15527 static const char *
15528 get_dynamic_reloc_section_name (bfd * abfd,
15529 asection * sec,
15530 bool is_rela)
15531 {
15532 char *name;
15533 const char *old_name = bfd_section_name (sec);
15534 const char *prefix = is_rela ? ".rela" : ".rel";
15535
15536 if (old_name == NULL)
15537 return NULL;
15538
15539 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15540 sprintf (name, "%s%s", prefix, old_name);
15541
15542 return name;
15543 }
15544
15545 /* Returns the dynamic reloc section associated with SEC.
15546 If necessary compute the name of the dynamic reloc section based
15547 on SEC's name (looked up in ABFD's string table) and the setting
15548 of IS_RELA. */
15549
15550 asection *
15551 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15552 asection *sec,
15553 bool is_rela)
15554 {
15555 asection *reloc_sec = elf_section_data (sec)->sreloc;
15556
15557 if (reloc_sec == NULL)
15558 {
15559 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15560
15561 if (name != NULL)
15562 {
15563 reloc_sec = bfd_get_linker_section (abfd, name);
15564
15565 if (reloc_sec != NULL)
15566 elf_section_data (sec)->sreloc = reloc_sec;
15567 }
15568 }
15569
15570 return reloc_sec;
15571 }
15572
15573 /* Returns the dynamic reloc section associated with SEC. If the
15574 section does not exist it is created and attached to the DYNOBJ
15575 bfd and stored in the SRELOC field of SEC's elf_section_data
15576 structure.
15577
15578 ALIGNMENT is the alignment for the newly created section and
15579 IS_RELA defines whether the name should be .rela.<SEC's name>
15580 or .rel.<SEC's name>. The section name is looked up in the
15581 string table associated with ABFD. */
15582
15583 asection *
15584 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15585 bfd *dynobj,
15586 unsigned int alignment,
15587 bfd *abfd,
15588 bool is_rela)
15589 {
15590 asection * reloc_sec = elf_section_data (sec)->sreloc;
15591
15592 if (reloc_sec == NULL)
15593 {
15594 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15595
15596 if (name == NULL)
15597 return NULL;
15598
15599 reloc_sec = bfd_get_linker_section (dynobj, name);
15600
15601 if (reloc_sec == NULL)
15602 {
15603 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15604 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15605 if ((sec->flags & SEC_ALLOC) != 0)
15606 flags |= SEC_ALLOC | SEC_LOAD;
15607
15608 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15609 if (reloc_sec != NULL)
15610 {
15611 /* _bfd_elf_get_sec_type_attr chooses a section type by
15612 name. Override as it may be wrong, eg. for a user
15613 section named "auto" we'll get ".relauto" which is
15614 seen to be a .rela section. */
15615 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15616 if (!bfd_set_section_alignment (reloc_sec, alignment))
15617 reloc_sec = NULL;
15618 }
15619 }
15620
15621 elf_section_data (sec)->sreloc = reloc_sec;
15622 }
15623
15624 return reloc_sec;
15625 }
15626
15627 /* Copy the ELF symbol type and other attributes for a linker script
15628 assignment from HSRC to HDEST. Generally this should be treated as
15629 if we found a strong non-dynamic definition for HDEST (except that
15630 ld ignores multiple definition errors). */
15631 void
15632 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15633 struct bfd_link_hash_entry *hdest,
15634 struct bfd_link_hash_entry *hsrc)
15635 {
15636 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15637 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15638 Elf_Internal_Sym isym;
15639
15640 ehdest->type = ehsrc->type;
15641 ehdest->target_internal = ehsrc->target_internal;
15642
15643 isym.st_other = ehsrc->other;
15644 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15645 }
15646
15647 /* Append a RELA relocation REL to section S in BFD. */
15648
15649 void
15650 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15651 {
15652 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15653 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15654 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15655 bed->s->swap_reloca_out (abfd, rel, loc);
15656 }
15657
15658 /* Append a REL relocation REL to section S in BFD. */
15659
15660 void
15661 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15662 {
15663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15664 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15665 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15666 bed->s->swap_reloc_out (abfd, rel, loc);
15667 }
15668
15669 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15670
15671 struct bfd_link_hash_entry *
15672 bfd_elf_define_start_stop (struct bfd_link_info *info,
15673 const char *symbol, asection *sec)
15674 {
15675 struct elf_link_hash_entry *h;
15676
15677 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15678 false, false, true);
15679 /* NB: Common symbols will be turned into definition later. */
15680 if (h != NULL
15681 && !h->root.ldscript_def
15682 && (h->root.type == bfd_link_hash_undefined
15683 || h->root.type == bfd_link_hash_undefweak
15684 || ((h->ref_regular || h->def_dynamic)
15685 && !h->def_regular
15686 && h->root.type != bfd_link_hash_common)))
15687 {
15688 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15689 h->verinfo.verdef = NULL;
15690 h->root.type = bfd_link_hash_defined;
15691 h->root.u.def.section = sec;
15692 h->root.u.def.value = 0;
15693 h->def_regular = 1;
15694 h->def_dynamic = 0;
15695 h->start_stop = 1;
15696 h->u2.start_stop_section = sec;
15697 if (symbol[0] == '.')
15698 {
15699 /* .startof. and .sizeof. symbols are local. */
15700 const struct elf_backend_data *bed;
15701 bed = get_elf_backend_data (info->output_bfd);
15702 (*bed->elf_backend_hide_symbol) (info, h, true);
15703 }
15704 else
15705 {
15706 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15707 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15708 | info->start_stop_visibility);
15709 if (was_dynamic)
15710 bfd_elf_link_record_dynamic_symbol (info, h);
15711 }
15712 return &h->root;
15713 }
15714 return NULL;
15715 }
15716
15717 /* Find dynamic relocs for H that apply to read-only sections. */
15718
15719 asection *
15720 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15721 {
15722 struct elf_dyn_relocs *p;
15723
15724 for (p = h->dyn_relocs; p != NULL; p = p->next)
15725 {
15726 asection *s = p->sec->output_section;
15727
15728 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15729 return p->sec;
15730 }
15731 return NULL;
15732 }
15733
15734 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15735 read-only sections. */
15736
15737 bool
15738 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15739 {
15740 asection *sec;
15741
15742 if (h->root.type == bfd_link_hash_indirect)
15743 return true;
15744
15745 sec = _bfd_elf_readonly_dynrelocs (h);
15746 if (sec != NULL)
15747 {
15748 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15749
15750 info->flags |= DF_TEXTREL;
15751 /* xgettext:c-format */
15752 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15753 "in read-only section `%pA'\n"),
15754 sec->owner, h->root.root.string, sec);
15755
15756 if (bfd_link_textrel_check (info))
15757 /* xgettext:c-format */
15758 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15759 "in read-only section `%pA'\n"),
15760 sec->owner, h->root.root.string, sec);
15761
15762 /* Not an error, just cut short the traversal. */
15763 return false;
15764 }
15765 return true;
15766 }
15767
15768 /* Add dynamic tags. */
15769
15770 bool
15771 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15772 bool need_dynamic_reloc)
15773 {
15774 struct elf_link_hash_table *htab = elf_hash_table (info);
15775
15776 if (htab->dynamic_sections_created)
15777 {
15778 /* Add some entries to the .dynamic section. We fill in the
15779 values later, in finish_dynamic_sections, but we must add
15780 the entries now so that we get the correct size for the
15781 .dynamic section. The DT_DEBUG entry is filled in by the
15782 dynamic linker and used by the debugger. */
15783 #define add_dynamic_entry(TAG, VAL) \
15784 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15785
15786 const struct elf_backend_data *bed
15787 = get_elf_backend_data (output_bfd);
15788
15789 if (bfd_link_executable (info))
15790 {
15791 if (!add_dynamic_entry (DT_DEBUG, 0))
15792 return false;
15793 }
15794
15795 if (htab->dt_pltgot_required || htab->splt->size != 0)
15796 {
15797 /* DT_PLTGOT is used by prelink even if there is no PLT
15798 relocation. */
15799 if (!add_dynamic_entry (DT_PLTGOT, 0))
15800 return false;
15801 }
15802
15803 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15804 {
15805 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15806 || !add_dynamic_entry (DT_PLTREL,
15807 (bed->rela_plts_and_copies_p
15808 ? DT_RELA : DT_REL))
15809 || !add_dynamic_entry (DT_JMPREL, 0))
15810 return false;
15811 }
15812
15813 if (htab->tlsdesc_plt
15814 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15815 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15816 return false;
15817
15818 if (need_dynamic_reloc)
15819 {
15820 if (bed->rela_plts_and_copies_p)
15821 {
15822 if (!add_dynamic_entry (DT_RELA, 0)
15823 || !add_dynamic_entry (DT_RELASZ, 0)
15824 || !add_dynamic_entry (DT_RELAENT,
15825 bed->s->sizeof_rela))
15826 return false;
15827 }
15828 else
15829 {
15830 if (!add_dynamic_entry (DT_REL, 0)
15831 || !add_dynamic_entry (DT_RELSZ, 0)
15832 || !add_dynamic_entry (DT_RELENT,
15833 bed->s->sizeof_rel))
15834 return false;
15835 }
15836
15837 /* If any dynamic relocs apply to a read-only section,
15838 then we need a DT_TEXTREL entry. */
15839 if ((info->flags & DF_TEXTREL) == 0)
15840 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15841 info);
15842
15843 if ((info->flags & DF_TEXTREL) != 0)
15844 {
15845 if (htab->ifunc_resolvers)
15846 info->callbacks->einfo
15847 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15848 "may result in a segfault at runtime; recompile with %s\n"),
15849 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15850
15851 if (!add_dynamic_entry (DT_TEXTREL, 0))
15852 return false;
15853 }
15854 }
15855 }
15856 #undef add_dynamic_entry
15857
15858 return true;
15859 }