]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
elf: Correct DT_TEXTREL warning in PDE
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 /* Let the backend create the rest of the sections. This lets the
363 backend set the right flags. The backend will normally create
364 the .got and .plt sections. */
365 if (bed->elf_backend_create_dynamic_sections == NULL
366 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
367 return false;
368
369 elf_hash_table (info)->dynamic_sections_created = true;
370
371 return true;
372 }
373
374 /* Create dynamic sections when linking against a dynamic object. */
375
376 bool
377 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
378 {
379 flagword flags, pltflags;
380 struct elf_link_hash_entry *h;
381 asection *s;
382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
383 struct elf_link_hash_table *htab = elf_hash_table (info);
384
385 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
386 .rel[a].bss sections. */
387 flags = bed->dynamic_sec_flags;
388
389 pltflags = flags;
390 if (bed->plt_not_loaded)
391 /* We do not clear SEC_ALLOC here because we still want the OS to
392 allocate space for the section; it's just that there's nothing
393 to read in from the object file. */
394 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
395 else
396 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
397 if (bed->plt_readonly)
398 pltflags |= SEC_READONLY;
399
400 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
401 if (s == NULL
402 || !bfd_set_section_alignment (s, bed->plt_alignment))
403 return false;
404 htab->splt = s;
405
406 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
407 .plt section. */
408 if (bed->want_plt_sym)
409 {
410 h = _bfd_elf_define_linkage_sym (abfd, info, s,
411 "_PROCEDURE_LINKAGE_TABLE_");
412 elf_hash_table (info)->hplt = h;
413 if (h == NULL)
414 return false;
415 }
416
417 s = bfd_make_section_anyway_with_flags (abfd,
418 (bed->rela_plts_and_copies_p
419 ? ".rela.plt" : ".rel.plt"),
420 flags | SEC_READONLY);
421 if (s == NULL
422 || !bfd_set_section_alignment (s, bed->s->log_file_align))
423 return false;
424 htab->srelplt = s;
425
426 if (! _bfd_elf_create_got_section (abfd, info))
427 return false;
428
429 if (bed->want_dynbss)
430 {
431 /* The .dynbss section is a place to put symbols which are defined
432 by dynamic objects, are referenced by regular objects, and are
433 not functions. We must allocate space for them in the process
434 image and use a R_*_COPY reloc to tell the dynamic linker to
435 initialize them at run time. The linker script puts the .dynbss
436 section into the .bss section of the final image. */
437 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
438 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (s == NULL)
440 return false;
441 htab->sdynbss = s;
442
443 if (bed->want_dynrelro)
444 {
445 /* Similarly, but for symbols that were originally in read-only
446 sections. This section doesn't really need to have contents,
447 but make it like other .data.rel.ro sections. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
449 flags);
450 if (s == NULL)
451 return false;
452 htab->sdynrelro = s;
453 }
454
455 /* The .rel[a].bss section holds copy relocs. This section is not
456 normally needed. We need to create it here, though, so that the
457 linker will map it to an output section. We can't just create it
458 only if we need it, because we will not know whether we need it
459 until we have seen all the input files, and the first time the
460 main linker code calls BFD after examining all the input files
461 (size_dynamic_sections) the input sections have already been
462 mapped to the output sections. If the section turns out not to
463 be needed, we can discard it later. We will never need this
464 section when generating a shared object, since they do not use
465 copy relocs. */
466 if (bfd_link_executable (info))
467 {
468 s = bfd_make_section_anyway_with_flags (abfd,
469 (bed->rela_plts_and_copies_p
470 ? ".rela.bss" : ".rel.bss"),
471 flags | SEC_READONLY);
472 if (s == NULL
473 || !bfd_set_section_alignment (s, bed->s->log_file_align))
474 return false;
475 htab->srelbss = s;
476
477 if (bed->want_dynrelro)
478 {
479 s = (bfd_make_section_anyway_with_flags
480 (abfd, (bed->rela_plts_and_copies_p
481 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
482 flags | SEC_READONLY));
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->sreldynrelro = s;
487 }
488 }
489 }
490
491 return true;
492 }
493 \f
494 /* Record a new dynamic symbol. We record the dynamic symbols as we
495 read the input files, since we need to have a list of all of them
496 before we can determine the final sizes of the output sections.
497 Note that we may actually call this function even though we are not
498 going to output any dynamic symbols; in some cases we know that a
499 symbol should be in the dynamic symbol table, but only if there is
500 one. */
501
502 bool
503 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
504 struct elf_link_hash_entry *h)
505 {
506 if (h->dynindx == -1)
507 {
508 struct elf_strtab_hash *dynstr;
509 char *p;
510 const char *name;
511 size_t indx;
512
513 if (h->root.type == bfd_link_hash_defined
514 || h->root.type == bfd_link_hash_defweak)
515 {
516 /* An IR symbol should not be made dynamic. */
517 if (h->root.u.def.section != NULL
518 && h->root.u.def.section->owner != NULL
519 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
520 return true;
521 }
522
523 /* XXX: The ABI draft says the linker must turn hidden and
524 internal symbols into STB_LOCAL symbols when producing the
525 DSO. However, if ld.so honors st_other in the dynamic table,
526 this would not be necessary. */
527 switch (ELF_ST_VISIBILITY (h->other))
528 {
529 case STV_INTERNAL:
530 case STV_HIDDEN:
531 if (h->root.type != bfd_link_hash_undefined
532 && h->root.type != bfd_link_hash_undefweak)
533 {
534 h->forced_local = 1;
535 if (!elf_hash_table (info)->is_relocatable_executable
536 || ((h->root.type == bfd_link_hash_defined
537 || h->root.type == bfd_link_hash_defweak)
538 && h->root.u.def.section->owner != NULL
539 && h->root.u.def.section->owner->no_export)
540 || (h->root.type == bfd_link_hash_common
541 && h->root.u.c.p->section->owner != NULL
542 && h->root.u.c.p->section->owner->no_export))
543 return true;
544 }
545
546 default:
547 break;
548 }
549
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
558 if (dynstr == NULL)
559 return false;
560 }
561
562 /* We don't put any version information in the dynamic string
563 table. */
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
578
579 if (indx == (size_t) -1)
580 return false;
581 h->dynstr_index = indx;
582 }
583
584 return true;
585 }
586 \f
587 /* Mark a symbol dynamic. */
588
589 static void
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
593 {
594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
595
596 /* It may be called more than once on the same H. */
597 if(h->dynamic || bfd_link_relocatable (info))
598 return;
599
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
602 || h->type == STT_COMMON
603 || (sym != NULL
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
606 || (d != NULL
607 && h->non_elf
608 && (*d->match) (&d->head, NULL, h->root.root.string)))
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
615 }
616
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
620 bool
621 bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
623 const char *name,
624 bool provide,
625 bool hidden)
626 {
627 struct elf_link_hash_entry *h, *hv;
628 struct elf_link_hash_table *htab;
629 const struct elf_backend_data *bed;
630
631 if (!is_elf_hash_table (info->hash))
632 return true;
633
634 htab = elf_hash_table (info);
635 h = elf_link_hash_lookup (htab, name, !provide, true, false);
636 if (h == NULL)
637 return provide;
638
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
663 switch (h->root.type)
664 {
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h->root.type = bfd_link_hash_new;
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
677 break;
678 case bfd_link_hash_new:
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
695 default:
696 BFD_FAIL ();
697 return false;
698 }
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
705 && h->def_dynamic
706 && !h->def_regular)
707 h->root.type = bfd_link_hash_undefined;
708
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
718
719 h->def_regular = 1;
720
721 if (hidden)
722 {
723 bed = get_elf_backend_data (output_bfd);
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
726 (*bed->elf_backend_hide_symbol) (info, h, true);
727 }
728
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
731 if (!bfd_link_relocatable (info)
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
737 if ((h->def_dynamic
738 || h->ref_dynamic
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
741 && !h->forced_local
742 && h->dynindx == -1)
743 {
744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
745 return false;
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
750 if (h->is_weakalias)
751 {
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
756 return false;
757 }
758 }
759
760 return true;
761 }
762
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767 int
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
771 {
772 size_t amt;
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
776 size_t dynstr_index;
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
781 if (! is_elf_hash_table (info->hash))
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
796 1, input_indx, &entry->isym, esym, &eshndx))
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
803 && entry->isym.st_shndx < SHN_LORESERVE)
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
831 if (dynstr_index == (size_t) -1)
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850 }
851
852 /* Return the dynindex of a local dynamic symbol. */
853
854 long
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
858 {
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865 }
866
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
871 static bool
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (h->forced_local)
878 return true;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return true;
884 }
885
886
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
890 static bool
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893 {
894 size_t *count = (size_t *) data;
895
896 if (!h->forced_local)
897 return true;
898
899 if (h->dynindx != -1)
900 h->dynindx = ++(*count);
901
902 return true;
903 }
904
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
907 bool
908 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
911 {
912 struct elf_link_hash_table *htab;
913 asection *ip;
914
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
922 htab = elf_hash_table (info);
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
926 return (htab->dynobj != NULL
927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
928 && ip->output_section == p);
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
933 return true;
934 }
935 }
936
937 bool
938 _bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942 {
943 return true;
944 }
945
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
954
955 static unsigned long
956 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
959 {
960 unsigned long dynsymcount = 0;
961 bool do_sec = section_sym_count != NULL;
962
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
965 {
966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
969 if ((p->flags & SEC_EXCLUDE) == 0
970 && (p->flags & SEC_ALLOC) != 0
971 && elf_hash_table (info)->dynamic_relocs
972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
979 elf_section_data (p)->dynindx = 0;
980 }
981 if (do_sec)
982 *section_sym_count = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
1005
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
1008 }
1009
1010 /* Merge st_other field. */
1011
1012 static void
1013 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1014 unsigned int st_other, asection *sec,
1015 bool definition, bool dynamic)
1016 {
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed->elf_backend_merge_symbol_attribute)
1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1023 dynamic);
1024
1025 if (!dynamic)
1026 {
1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1029
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1034 }
1035 else if (definition
1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1037 && (sec->flags & SEC_READONLY) == 0)
1038 h->protected_def = 1;
1039 }
1040
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1053
1054 static bool
1055 _bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
1063 bool *pold_weak,
1064 unsigned int *pold_alignment,
1065 bool *skip,
1066 bfd **override,
1067 bool *type_change_ok,
1068 bool *size_change_ok,
1069 bool *matched)
1070 {
1071 asection *sec, *oldsec;
1072 struct elf_link_hash_entry *h;
1073 struct elf_link_hash_entry *hi;
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
1077 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1078 bool newweak, oldweak, newfunc, oldfunc;
1079 const struct elf_backend_data *bed;
1080 char *new_version;
1081 bool default_sym = *matched;
1082
1083 *skip = false;
1084 *override = NULL;
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
1090 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1091 else
1092 h = ((struct elf_link_hash_entry *)
1093 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1094 if (h == NULL)
1095 return false;
1096 *sym_hash = h;
1097
1098 bed = get_elf_backend_data (abfd);
1099
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h->versioned != unversioned)
1102 {
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
1120 }
1121 else
1122 new_version = NULL;
1123
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
1134 *matched = true;
1135 else
1136 {
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bool old_hidden = h->versioned == versioned_hidden;
1142 bool new_hidden = hi->versioned == versioned_hidden;
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
1248 {
1249 h->non_elf = 0;
1250 return true;
1251 }
1252
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
1260 && (newweak || oldweak)
1261 && ((abfd->flags & DYNAMIC) == 0
1262 || !h->def_regular))
1263 return true;
1264
1265 olddyn = false;
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1268 else if (oldsec != NULL)
1269 {
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1273 }
1274
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1277 if (oldbfd != NULL
1278 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1279 && newdyn != olddyn)
1280 {
1281 h->root.non_ir_ref_dynamic = true;
1282 hi->root.non_ir_ref_dynamic = true;
1283 }
1284
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1287
1288 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1289
1290 olddef = (h->root.type != bfd_link_hash_undefined
1291 && h->root.type != bfd_link_hash_undefweak
1292 && h->root.type != bfd_link_hash_common);
1293
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1296
1297 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1298 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1299
1300 oldfunc = (h->type != STT_NOTYPE
1301 && bed->is_function_type (h->type));
1302
1303 if (!(newfunc && oldfunc)
1304 && ELF_ST_TYPE (sym->st_info) != h->type
1305 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1306 && h->type != STT_NOTYPE
1307 && (newdef || bfd_is_com_section (sec))
1308 && (olddef || h->root.type == bfd_link_hash_common))
1309 {
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1315 if (newdyn
1316 && !olddyn)
1317 {
1318 *skip = true;
1319 return true;
1320 }
1321
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1324 dynamic state. */
1325 if (hi != h
1326 && !newdyn
1327 && olddyn)
1328 {
1329 h = hi;
1330 (*bed->elf_backend_hide_symbol) (info, h, true);
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 h->def_dynamic = 0;
1334 h->dynamic_def = 0;
1335 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1336 {
1337 h->root.type = bfd_link_hash_undefined;
1338 h->root.u.undef.abfd = abfd;
1339 }
1340 else
1341 {
1342 h->root.type = bfd_link_hash_new;
1343 h->root.u.undef.abfd = NULL;
1344 }
1345 return true;
1346 }
1347 }
1348
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1352 if (oldbfd != NULL
1353 && (oldbfd->flags & BFD_PLUGIN) == 0
1354 && (abfd->flags & BFD_PLUGIN) == 0
1355 && ELF_ST_TYPE (sym->st_info) != h->type
1356 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1357 {
1358 bfd *ntbfd, *tbfd;
1359 bool ntdef, tdef;
1360 asection *ntsec, *tsec;
1361
1362 if (h->type == STT_TLS)
1363 {
1364 ntbfd = abfd;
1365 ntsec = sec;
1366 ntdef = newdef;
1367 tbfd = oldbfd;
1368 tsec = oldsec;
1369 tdef = olddef;
1370 }
1371 else
1372 {
1373 ntbfd = oldbfd;
1374 ntsec = oldsec;
1375 ntdef = olddef;
1376 tbfd = abfd;
1377 tsec = sec;
1378 tdef = newdef;
1379 }
1380
1381 if (tdef && ntdef)
1382 _bfd_error_handler
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
1386 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1387 else if (!tdef && !ntdef)
1388 _bfd_error_handler
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
1392 h->root.root.string, tbfd, ntbfd);
1393 else if (tdef)
1394 _bfd_error_handler
1395 /* xgettext:c-format */
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
1398 h->root.root.string, tbfd, tsec, ntbfd);
1399 else
1400 _bfd_error_handler
1401 /* xgettext:c-format */
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
1404 h->root.root.string, tbfd, ntbfd, ntsec);
1405
1406 bfd_set_error (bfd_error_bad_value);
1407 return false;
1408 }
1409
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1412 if (newdyn
1413 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1414 && !bfd_is_und_section (sec))
1415 {
1416 *skip = true;
1417 /* Make sure this symbol is dynamic. */
1418 h->ref_dynamic = 1;
1419 hi->ref_dynamic = 1;
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1422
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1425 return bfd_elf_link_record_dynamic_symbol (info, h);
1426 else
1427 return true;
1428 }
1429 else if (!newdyn
1430 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1431 && h->def_dynamic)
1432 {
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
1436 if (hi->root.type == bfd_link_hash_indirect)
1437 {
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1442 if (h->ref_regular)
1443 {
1444 hi->root.type = h->root.type;
1445 h->root.type = bfd_link_hash_indirect;
1446 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1447
1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1450 {
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, true);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
1456 }
1457 else
1458 h->ref_dynamic = 1;
1459
1460 h->def_dynamic = 0;
1461 /* FIXME: Should we check type and size for protected symbol? */
1462 h->size = 0;
1463 h->type = 0;
1464
1465 h = hi;
1466 }
1467 else
1468 h = hi;
1469 }
1470
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1479 {
1480 h->root.type = bfd_link_hash_undefined;
1481 h->root.u.undef.abfd = abfd;
1482 }
1483 else
1484 {
1485 h->root.type = bfd_link_hash_new;
1486 h->root.u.undef.abfd = NULL;
1487 }
1488
1489 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1490 {
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed->elf_backend_hide_symbol) (info, h, true);
1494 h->forced_local = 0;
1495 h->ref_dynamic = 0;
1496 }
1497 else
1498 h->ref_dynamic = 1;
1499 h->def_dynamic = 0;
1500 /* FIXME: Should we check type and size for protected symbol? */
1501 h->size = 0;
1502 h->type = 0;
1503 return true;
1504 }
1505
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1513
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1518
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1521
1522 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1523 newweak = false;
1524 if (olddef && newdyn)
1525 oldweak = false;
1526
1527 /* Allow changes between different types of function symbol. */
1528 if (newfunc && oldfunc)
1529 *type_change_ok = true;
1530
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
1534
1535 if (oldweak
1536 || newweak
1537 || (newdef
1538 && h->root.type == bfd_link_hash_undefined))
1539 *type_change_ok = true;
1540
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1543
1544 if (*type_change_ok
1545 || h->root.type == bfd_link_hash_undefined)
1546 *size_change_ok = true;
1547
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1559 libraries.
1560
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1563
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1569 harmless. */
1570
1571 if (newdyn
1572 && newdef
1573 && !newweak
1574 && (sec->flags & SEC_ALLOC) != 0
1575 && (sec->flags & SEC_LOAD) == 0
1576 && sym->st_size > 0
1577 && !newfunc)
1578 newdyncommon = true;
1579 else
1580 newdyncommon = false;
1581
1582 if (olddyn
1583 && olddef
1584 && h->root.type == bfd_link_hash_defined
1585 && h->def_dynamic
1586 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1587 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1588 && h->size > 0
1589 && !oldfunc)
1590 olddyncommon = true;
1591 else
1592 olddyncommon = false;
1593
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
1596 if (bed->merge_symbol != NULL)
1597 {
1598 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1599 return false;
1600 sec = *psec;
1601 }
1602
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
1605 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1606 && !default_sym && h->def_regular
1607 && !(oldbfd != NULL
1608 && (oldbfd->flags & BFD_PLUGIN) != 0
1609 && (abfd->flags & BFD_PLUGIN) == 0))
1610 {
1611 /* Handle a multiple definition. */
1612 (*info->callbacks->multiple_definition) (info, &h->root,
1613 abfd, sec, *pvalue);
1614 *skip = true;
1615 return true;
1616 }
1617
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1620 two. */
1621
1622 if (olddyncommon
1623 && newdyncommon
1624 && sym->st_size != h->size)
1625 {
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1631
1632 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1633 bfd_link_hash_common, sym->st_size);
1634 if (sym->st_size > h->size)
1635 h->size = sym->st_size;
1636
1637 *size_change_ok = true;
1638 }
1639
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1646
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
1652 object to override a weak symbol in a shared object. */
1653
1654 if (newdyn
1655 && newdef
1656 && (olddef
1657 || (h->root.type == bfd_link_hash_common
1658 && (newweak || newfunc))))
1659 {
1660 *override = abfd;
1661 newdef = false;
1662 newdyncommon = false;
1663
1664 *psec = sec = bfd_und_section_ptr;
1665 *size_change_ok = true;
1666
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
1672
1673 if (h->root.type == bfd_link_hash_common)
1674 *type_change_ok = true;
1675 }
1676
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1681 right thing. */
1682
1683 if (newdyncommon
1684 && h->root.type == bfd_link_hash_common)
1685 {
1686 *override = oldbfd;
1687 newdef = false;
1688 newdyncommon = false;
1689 *pvalue = sym->st_size;
1690 *psec = sec = bed->common_section (oldsec);
1691 *size_change_ok = true;
1692 }
1693
1694 /* Skip weak definitions of symbols that are already defined. */
1695 if (newdef && olddef && newweak)
1696 {
1697 /* Don't skip new non-IR weak syms. */
1698 if (!(oldbfd != NULL
1699 && (oldbfd->flags & BFD_PLUGIN) != 0
1700 && (abfd->flags & BFD_PLUGIN) == 0))
1701 {
1702 newdef = false;
1703 *skip = true;
1704 }
1705
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1708 local symbol. */
1709 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1710 if (h->dynindx != -1)
1711 switch (ELF_ST_VISIBILITY (h->other))
1712 {
1713 case STV_INTERNAL:
1714 case STV_HIDDEN:
1715 (*bed->elf_backend_hide_symbol) (info, h, true);
1716 break;
1717 }
1718 }
1719
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1725
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
1728 symbol is a function or is weak. */
1729
1730 flip = NULL;
1731 if (!newdyn
1732 && (newdef
1733 || (bfd_is_com_section (sec)
1734 && (oldweak || oldfunc)))
1735 && olddyn
1736 && olddef
1737 && h->def_dynamic)
1738 {
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1741 new definition. */
1742
1743 h->root.type = bfd_link_hash_undefined;
1744 h->root.u.undef.abfd = h->root.u.def.section->owner;
1745 *size_change_ok = true;
1746
1747 olddef = false;
1748 olddyncommon = false;
1749
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1752
1753 if (bfd_is_com_section (sec))
1754 {
1755 if (oldfunc)
1756 {
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1759 function. */
1760 h->def_dynamic = 0;
1761 h->type = STT_NOTYPE;
1762 }
1763 *type_change_ok = true;
1764 }
1765
1766 if (hi->root.type == bfd_link_hash_indirect)
1767 flip = hi;
1768 else
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h->verinfo.vertree = NULL;
1773 }
1774
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1780
1781 if (! newdyn
1782 && bfd_is_com_section (sec)
1783 && olddyncommon)
1784 {
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1788 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1789 bfd_link_hash_common, sym->st_size);
1790
1791 /* If the presumed common symbol in the dynamic object is
1792 larger, pretend that the new symbol has its size. */
1793
1794 if (h->size > *pvalue)
1795 *pvalue = h->size;
1796
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment);
1800 *pold_alignment = h->root.u.def.section->alignment_power;
1801
1802 olddef = false;
1803 olddyncommon = false;
1804
1805 h->root.type = bfd_link_hash_undefined;
1806 h->root.u.undef.abfd = h->root.u.def.section->owner;
1807
1808 *size_change_ok = true;
1809 *type_change_ok = true;
1810
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 flip = hi;
1813 else
1814 h->verinfo.vertree = NULL;
1815 }
1816
1817 if (flip != NULL)
1818 {
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
1822 flip->root.type = h->root.type;
1823 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1826 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1827 if (h->def_dynamic)
1828 {
1829 h->def_dynamic = 0;
1830 flip->ref_dynamic = 1;
1831 }
1832 }
1833
1834 return true;
1835 }
1836
1837 /* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1840 set DYNSYM if the new indirect symbol is dynamic. */
1841
1842 static bool
1843 _bfd_elf_add_default_symbol (bfd *abfd,
1844 struct bfd_link_info *info,
1845 struct elf_link_hash_entry *h,
1846 const char *name,
1847 Elf_Internal_Sym *sym,
1848 asection *sec,
1849 bfd_vma value,
1850 bfd **poldbfd,
1851 bool *dynsym)
1852 {
1853 bool type_change_ok;
1854 bool size_change_ok;
1855 bool skip;
1856 char *shortname;
1857 struct elf_link_hash_entry *hi;
1858 struct bfd_link_hash_entry *bh;
1859 const struct elf_backend_data *bed;
1860 bool collect;
1861 bool dynamic;
1862 bfd *override;
1863 char *p;
1864 size_t len, shortlen;
1865 asection *tmp_sec;
1866 bool matched;
1867
1868 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1869 return true;
1870
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p = strchr (name, ELF_VER_CHR);
1876 if (h->versioned == unknown)
1877 {
1878 if (p == NULL)
1879 {
1880 h->versioned = unversioned;
1881 return true;
1882 }
1883 else
1884 {
1885 if (p[1] != ELF_VER_CHR)
1886 {
1887 h->versioned = versioned_hidden;
1888 return true;
1889 }
1890 else
1891 h->versioned = versioned;
1892 }
1893 }
1894 else
1895 {
1896 /* PR ld/19073: We may see an unversioned definition after the
1897 default version. */
1898 if (p == NULL)
1899 return true;
1900 }
1901
1902 bed = get_elf_backend_data (abfd);
1903 collect = bed->collect;
1904 dynamic = (abfd->flags & DYNAMIC) != 0;
1905
1906 shortlen = p - name;
1907 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1908 if (shortname == NULL)
1909 return false;
1910 memcpy (shortname, name, shortlen);
1911 shortname[shortlen] = '\0';
1912
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok = false;
1918 size_change_ok = false;
1919 matched = true;
1920 tmp_sec = sec;
1921 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1922 &hi, poldbfd, NULL, NULL, &skip, &override,
1923 &type_change_ok, &size_change_ok, &matched))
1924 return false;
1925
1926 if (skip)
1927 goto nondefault;
1928
1929 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1930 {
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1937 {
1938 bool hide;
1939
1940 hi->verinfo.vertree
1941 = bfd_find_version_for_sym (info->version_info,
1942 hi->root.root.string, &hide);
1943 if (hi->verinfo.vertree != NULL && hide)
1944 {
1945 (*bed->elf_backend_hide_symbol) (info, hi, true);
1946 goto nondefault;
1947 }
1948 }
1949 if (hi->verinfo.vertree != NULL
1950 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1951 goto nondefault;
1952 }
1953
1954 if (! override)
1955 {
1956 /* Add the default symbol if not performing a relocatable link. */
1957 if (! bfd_link_relocatable (info))
1958 {
1959 bh = &hi->root;
1960 if (bh->type == bfd_link_hash_defined
1961 && bh->u.def.section->owner != NULL
1962 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1963 {
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1966 it. */
1967 bh->type = bfd_link_hash_undefined;
1968 bh->u.undef.abfd = bh->u.def.section->owner;
1969 }
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info, abfd, shortname, BSF_INDIRECT,
1972 bfd_ind_section_ptr,
1973 0, name, false, collect, &bh)))
1974 return false;
1975 hi = (struct elf_link_hash_entry *) bh;
1976 }
1977 }
1978 else
1979 {
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1985
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1989
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1997
1998 while (hi->root.type == bfd_link_hash_indirect
1999 || hi->root.type == bfd_link_hash_warning)
2000 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2001
2002 h->root.type = bfd_link_hash_indirect;
2003 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2004 if (h->def_dynamic)
2005 {
2006 h->def_dynamic = 0;
2007 hi->ref_dynamic = 1;
2008 if (hi->ref_regular
2009 || hi->def_regular)
2010 {
2011 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2012 return false;
2013 }
2014 }
2015
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2018 hi = h;
2019 }
2020
2021 /* Check if HI is a warning symbol. */
2022 if (hi->root.type == bfd_link_hash_warning)
2023 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2024
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2028
2029 if (hi->root.type == bfd_link_hash_indirect)
2030 {
2031 struct elf_link_hash_entry *ht;
2032
2033 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2034 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2035
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2039
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2044 hi->dynamic_def |= ht->dynamic_def;
2045
2046 /* See if the new flags lead us to realize that the symbol must
2047 be dynamic. */
2048 if (! *dynsym)
2049 {
2050 if (! dynamic)
2051 {
2052 if (! bfd_link_executable (info)
2053 || hi->def_dynamic
2054 || hi->ref_dynamic)
2055 *dynsym = true;
2056 }
2057 else
2058 {
2059 if (hi->ref_regular)
2060 *dynsym = true;
2061 }
2062 }
2063 }
2064
2065 /* We also need to define an indirection from the nondefault version
2066 of the symbol. */
2067
2068 nondefault:
2069 len = strlen (name);
2070 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2071 if (shortname == NULL)
2072 return false;
2073 memcpy (shortname, name, shortlen);
2074 memcpy (shortname + shortlen, p + 1, len - shortlen);
2075
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok = false;
2078 size_change_ok = false;
2079 tmp_sec = sec;
2080 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2081 &hi, poldbfd, NULL, NULL, &skip, &override,
2082 &type_change_ok, &size_change_ok, &matched))
2083 return false;
2084
2085 if (skip)
2086 {
2087 if (!dynamic
2088 && h->root.type == bfd_link_hash_defweak
2089 && hi->root.type == bfd_link_hash_defined)
2090 {
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h->root.type = bfd_link_hash_defined;
2097 h->root.u.def.section = hi->root.u.def.section;
2098 h->root.u.def.value = hi->root.u.def.value;
2099 hi->root.type = bfd_link_hash_indirect;
2100 hi->root.u.i.link = &h->root;
2101 }
2102 else
2103 return true;
2104 }
2105 else if (override)
2106 {
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
2109 overridden by a versioned definition. */
2110 if (hi->root.type != bfd_link_hash_defined
2111 && hi->root.type != bfd_link_hash_defweak)
2112 _bfd_error_handler
2113 /* xgettext:c-format */
2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2115 abfd, shortname);
2116 return true;
2117 }
2118 else
2119 {
2120 bh = &hi->root;
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info, abfd, shortname, BSF_INDIRECT,
2123 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2124 return false;
2125 hi = (struct elf_link_hash_entry *) bh;
2126 }
2127
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi->root.type == bfd_link_hash_indirect)
2132 {
2133 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2134 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2135 hi->dynamic_def |= h->dynamic_def;
2136
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2139 @@VER symbol. */
2140 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2141
2142 /* See if the new flags lead us to realize that the symbol
2143 must be dynamic. */
2144 if (! *dynsym)
2145 {
2146 if (! dynamic)
2147 {
2148 if (! bfd_link_executable (info)
2149 || hi->ref_dynamic)
2150 *dynsym = true;
2151 }
2152 else
2153 {
2154 if (hi->ref_regular)
2155 *dynsym = true;
2156 }
2157 }
2158 }
2159
2160 return true;
2161 }
2162 \f
2163 /* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2165
2166 static bool
2167 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2168 {
2169 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2170
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h->root.type == bfd_link_hash_indirect)
2173 return true;
2174
2175 /* Ignore this if we won't export it. */
2176 if (!eif->info->export_dynamic && !h->dynamic)
2177 return true;
2178
2179 if (h->dynindx == -1
2180 && (h->def_regular || h->ref_regular)
2181 && ! bfd_hide_sym_by_version (eif->info->version_info,
2182 h->root.root.string))
2183 {
2184 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2185 {
2186 eif->failed = true;
2187 return false;
2188 }
2189 }
2190
2191 return true;
2192 }
2193 \f
2194 /* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2198
2199 static bool
2200 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2201 void *data)
2202 {
2203 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2204 Elf_Internal_Verneed *t;
2205 Elf_Internal_Vernaux *a;
2206 size_t amt;
2207
2208 /* We only care about symbols defined in shared objects with version
2209 information. */
2210 if (!h->def_dynamic
2211 || h->def_regular
2212 || h->dynindx == -1
2213 || h->verinfo.verdef == NULL
2214 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2215 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2216 return true;
2217
2218 /* See if we already know about this version. */
2219 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2220 t != NULL;
2221 t = t->vn_nextref)
2222 {
2223 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2224 continue;
2225
2226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2227 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2228 return true;
2229
2230 break;
2231 }
2232
2233 /* This is a new version. Add it to tree we are building. */
2234
2235 if (t == NULL)
2236 {
2237 amt = sizeof *t;
2238 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2239 if (t == NULL)
2240 {
2241 rinfo->failed = true;
2242 return false;
2243 }
2244
2245 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2246 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2247 elf_tdata (rinfo->info->output_bfd)->verref = t;
2248 }
2249
2250 amt = sizeof *a;
2251 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2252 if (a == NULL)
2253 {
2254 rinfo->failed = true;
2255 return false;
2256 }
2257
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2261 fixed. */
2262 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2263
2264 a->vna_flags = h->verinfo.verdef->vd_flags;
2265 a->vna_nextptr = t->vn_auxptr;
2266
2267 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2268 ++rinfo->vers;
2269
2270 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2271
2272 t->vn_auxptr = a;
2273
2274 return true;
2275 }
2276
2277 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2279
2280 static bool
2281 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2282 struct elf_link_hash_entry *h,
2283 const char *version_p,
2284 struct bfd_elf_version_tree **t_p,
2285 bool *hide)
2286 {
2287 struct bfd_elf_version_tree *t;
2288
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t = info->version_info; t != NULL; t = t->next)
2291 {
2292 if (strcmp (t->name, version_p) == 0)
2293 {
2294 size_t len;
2295 char *alc;
2296 struct bfd_elf_version_expr *d;
2297
2298 len = version_p - h->root.root.string;
2299 alc = (char *) bfd_malloc (len);
2300 if (alc == NULL)
2301 return false;
2302 memcpy (alc, h->root.root.string, len - 1);
2303 alc[len - 1] = '\0';
2304 if (alc[len - 2] == ELF_VER_CHR)
2305 alc[len - 2] = '\0';
2306
2307 h->verinfo.vertree = t;
2308 t->used = true;
2309 d = NULL;
2310
2311 if (t->globals.list != NULL)
2312 d = (*t->match) (&t->globals, NULL, alc);
2313
2314 /* See if there is anything to force this symbol to
2315 local scope. */
2316 if (d == NULL && t->locals.list != NULL)
2317 {
2318 d = (*t->match) (&t->locals, NULL, alc);
2319 if (d != NULL
2320 && h->dynindx != -1
2321 && ! info->export_dynamic)
2322 *hide = true;
2323 }
2324
2325 free (alc);
2326 break;
2327 }
2328 }
2329
2330 *t_p = t;
2331
2332 return true;
2333 }
2334
2335 /* Return TRUE if the symbol H is hidden by version script. */
2336
2337 bool
2338 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2339 struct elf_link_hash_entry *h)
2340 {
2341 const char *p;
2342 bool hide = false;
2343 const struct elf_backend_data *bed
2344 = get_elf_backend_data (info->output_bfd);
2345
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2348 return true;
2349
2350 p = strchr (h->root.root.string, ELF_VER_CHR);
2351 if (p != NULL && h->verinfo.vertree == NULL)
2352 {
2353 struct bfd_elf_version_tree *t;
2354
2355 ++p;
2356 if (*p == ELF_VER_CHR)
2357 ++p;
2358
2359 if (*p != '\0'
2360 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2361 && hide)
2362 {
2363 if (hide)
2364 (*bed->elf_backend_hide_symbol) (info, h, true);
2365 return true;
2366 }
2367 }
2368
2369 /* If we don't have a version for this symbol, see if we can find
2370 something. */
2371 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2372 {
2373 h->verinfo.vertree
2374 = bfd_find_version_for_sym (info->version_info,
2375 h->root.root.string, &hide);
2376 if (h->verinfo.vertree != NULL && hide)
2377 {
2378 (*bed->elf_backend_hide_symbol) (info, h, true);
2379 return true;
2380 }
2381 }
2382
2383 return false;
2384 }
2385
2386 /* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2390
2391 static bool
2392 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2393 {
2394 struct elf_info_failed *sinfo;
2395 struct bfd_link_info *info;
2396 const struct elf_backend_data *bed;
2397 struct elf_info_failed eif;
2398 char *p;
2399 bool hide;
2400
2401 sinfo = (struct elf_info_failed *) data;
2402 info = sinfo->info;
2403
2404 /* Fix the symbol flags. */
2405 eif.failed = false;
2406 eif.info = info;
2407 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2408 {
2409 if (eif.failed)
2410 sinfo->failed = true;
2411 return false;
2412 }
2413
2414 bed = get_elf_backend_data (info->output_bfd);
2415
2416 /* We only need version numbers for symbols defined in regular
2417 objects. */
2418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2419 {
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h->root.type == bfd_link_hash_defined
2422 || h->root.type == bfd_link_hash_defweak)
2423 && discarded_section (h->root.u.def.section))
2424 (*bed->elf_backend_hide_symbol) (info, h, true);
2425 return true;
2426 }
2427
2428 hide = false;
2429 p = strchr (h->root.root.string, ELF_VER_CHR);
2430 if (p != NULL && h->verinfo.vertree == NULL)
2431 {
2432 struct bfd_elf_version_tree *t;
2433
2434 ++p;
2435 if (*p == ELF_VER_CHR)
2436 ++p;
2437
2438 /* If there is no version string, we can just return out. */
2439 if (*p == '\0')
2440 return true;
2441
2442 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2443 {
2444 sinfo->failed = true;
2445 return false;
2446 }
2447
2448 if (hide)
2449 (*bed->elf_backend_hide_symbol) (info, h, true);
2450
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
2453 if (t == NULL && bfd_link_executable (info))
2454 {
2455 struct bfd_elf_version_tree **pp;
2456 int version_index;
2457
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h->dynindx == -1)
2461 return true;
2462
2463 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2464 sizeof *t);
2465 if (t == NULL)
2466 {
2467 sinfo->failed = true;
2468 return false;
2469 }
2470
2471 t->name = p;
2472 t->name_indx = (unsigned int) -1;
2473 t->used = true;
2474
2475 version_index = 1;
2476 /* Don't count anonymous version tag. */
2477 if (sinfo->info->version_info != NULL
2478 && sinfo->info->version_info->vernum == 0)
2479 version_index = 0;
2480 for (pp = &sinfo->info->version_info;
2481 *pp != NULL;
2482 pp = &(*pp)->next)
2483 ++version_index;
2484 t->vernum = version_index;
2485
2486 *pp = t;
2487
2488 h->verinfo.vertree = t;
2489 }
2490 else if (t == NULL)
2491 {
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
2494 _bfd_error_handler
2495 /* xgettext:c-format */
2496 (_("%pB: version node not found for symbol %s"),
2497 info->output_bfd, h->root.root.string);
2498 bfd_set_error (bfd_error_bad_value);
2499 sinfo->failed = true;
2500 return false;
2501 }
2502 }
2503
2504 /* If we don't have a version for this symbol, see if we can find
2505 something. */
2506 if (!hide
2507 && h->verinfo.vertree == NULL
2508 && sinfo->info->version_info != NULL)
2509 {
2510 h->verinfo.vertree
2511 = bfd_find_version_for_sym (sinfo->info->version_info,
2512 h->root.root.string, &hide);
2513 if (h->verinfo.vertree != NULL && hide)
2514 (*bed->elf_backend_hide_symbol) (info, h, true);
2515 }
2516
2517 return true;
2518 }
2519 \f
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2526
2527 Returns FALSE if something goes wrong. */
2528
2529 static bool
2530 elf_link_read_relocs_from_section (bfd *abfd,
2531 asection *sec,
2532 Elf_Internal_Shdr *shdr,
2533 void *external_relocs,
2534 Elf_Internal_Rela *internal_relocs)
2535 {
2536 const struct elf_backend_data *bed;
2537 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2538 const bfd_byte *erela;
2539 const bfd_byte *erelaend;
2540 Elf_Internal_Rela *irela;
2541 Elf_Internal_Shdr *symtab_hdr;
2542 size_t nsyms;
2543
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2546 return false;
2547
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2550 return false;
2551
2552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2553 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2554
2555 bed = get_elf_backend_data (abfd);
2556
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr->sh_entsize == bed->s->sizeof_rel)
2559 swap_in = bed->s->swap_reloc_in;
2560 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2561 swap_in = bed->s->swap_reloca_in;
2562 else
2563 {
2564 bfd_set_error (bfd_error_wrong_format);
2565 return false;
2566 }
2567
2568 erela = (const bfd_byte *) external_relocs;
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2572 irela = internal_relocs;
2573 while (erela <= erelaend)
2574 {
2575 bfd_vma r_symndx;
2576
2577 (*swap_in) (abfd, erela, irela);
2578 r_symndx = ELF32_R_SYM (irela->r_info);
2579 if (bed->s->arch_size == 64)
2580 r_symndx >>= 24;
2581 if (nsyms > 0)
2582 {
2583 if ((size_t) r_symndx >= nsyms)
2584 {
2585 _bfd_error_handler
2586 /* xgettext:c-format */
2587 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2588 " for offset %#" PRIx64 " in section `%pA'"),
2589 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2590 (uint64_t) irela->r_offset, sec);
2591 bfd_set_error (bfd_error_bad_value);
2592 return false;
2593 }
2594 }
2595 else if (r_symndx != STN_UNDEF)
2596 {
2597 _bfd_error_handler
2598 /* xgettext:c-format */
2599 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2600 " for offset %#" PRIx64 " in section `%pA'"
2601 " when the object file has no symbol table"),
2602 abfd, (uint64_t) r_symndx,
2603 (uint64_t) irela->r_offset, sec);
2604 bfd_set_error (bfd_error_bad_value);
2605 return false;
2606 }
2607 irela += bed->s->int_rels_per_ext_rel;
2608 erela += shdr->sh_entsize;
2609 }
2610
2611 return true;
2612 }
2613
2614 /* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
2622 RELA_HDR relocations. */
2623
2624 Elf_Internal_Rela *
2625 _bfd_elf_link_read_relocs (bfd *abfd,
2626 asection *o,
2627 void *external_relocs,
2628 Elf_Internal_Rela *internal_relocs,
2629 bool keep_memory)
2630 {
2631 void *alloc1 = NULL;
2632 Elf_Internal_Rela *alloc2 = NULL;
2633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2634 struct bfd_elf_section_data *esdo = elf_section_data (o);
2635 Elf_Internal_Rela *internal_rela_relocs;
2636
2637 if (esdo->relocs != NULL)
2638 return esdo->relocs;
2639
2640 if (o->reloc_count == 0)
2641 return NULL;
2642
2643 if (internal_relocs == NULL)
2644 {
2645 bfd_size_type size;
2646
2647 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2648 if (keep_memory)
2649 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2650 else
2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2652 if (internal_relocs == NULL)
2653 goto error_return;
2654 }
2655
2656 if (external_relocs == NULL)
2657 {
2658 bfd_size_type size = 0;
2659
2660 if (esdo->rel.hdr)
2661 size += esdo->rel.hdr->sh_size;
2662 if (esdo->rela.hdr)
2663 size += esdo->rela.hdr->sh_size;
2664
2665 alloc1 = bfd_malloc (size);
2666 if (alloc1 == NULL)
2667 goto error_return;
2668 external_relocs = alloc1;
2669 }
2670
2671 internal_rela_relocs = internal_relocs;
2672 if (esdo->rel.hdr)
2673 {
2674 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2675 external_relocs,
2676 internal_relocs))
2677 goto error_return;
2678 external_relocs = (((bfd_byte *) external_relocs)
2679 + esdo->rel.hdr->sh_size);
2680 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2681 * bed->s->int_rels_per_ext_rel);
2682 }
2683
2684 if (esdo->rela.hdr
2685 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2686 external_relocs,
2687 internal_rela_relocs)))
2688 goto error_return;
2689
2690 /* Cache the results for next time, if we can. */
2691 if (keep_memory)
2692 esdo->relocs = internal_relocs;
2693
2694 free (alloc1);
2695
2696 /* Don't free alloc2, since if it was allocated we are passing it
2697 back (under the name of internal_relocs). */
2698
2699 return internal_relocs;
2700
2701 error_return:
2702 free (alloc1);
2703 if (alloc2 != NULL)
2704 {
2705 if (keep_memory)
2706 bfd_release (abfd, alloc2);
2707 else
2708 free (alloc2);
2709 }
2710 return NULL;
2711 }
2712
2713 /* Compute the size of, and allocate space for, REL_HDR which is the
2714 section header for a section containing relocations for O. */
2715
2716 static bool
2717 _bfd_elf_link_size_reloc_section (bfd *abfd,
2718 struct bfd_elf_section_reloc_data *reldata)
2719 {
2720 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2721
2722 /* That allows us to calculate the size of the section. */
2723 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2724
2725 /* The contents field must last into write_object_contents, so we
2726 allocate it with bfd_alloc rather than malloc. Also since we
2727 cannot be sure that the contents will actually be filled in,
2728 we zero the allocated space. */
2729 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2730 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2731 return false;
2732
2733 if (reldata->hashes == NULL && reldata->count)
2734 {
2735 struct elf_link_hash_entry **p;
2736
2737 p = ((struct elf_link_hash_entry **)
2738 bfd_zmalloc (reldata->count * sizeof (*p)));
2739 if (p == NULL)
2740 return false;
2741
2742 reldata->hashes = p;
2743 }
2744
2745 return true;
2746 }
2747
2748 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2749 originated from the section given by INPUT_REL_HDR) to the
2750 OUTPUT_BFD. */
2751
2752 bool
2753 _bfd_elf_link_output_relocs (bfd *output_bfd,
2754 asection *input_section,
2755 Elf_Internal_Shdr *input_rel_hdr,
2756 Elf_Internal_Rela *internal_relocs,
2757 struct elf_link_hash_entry **rel_hash
2758 ATTRIBUTE_UNUSED)
2759 {
2760 Elf_Internal_Rela *irela;
2761 Elf_Internal_Rela *irelaend;
2762 bfd_byte *erel;
2763 struct bfd_elf_section_reloc_data *output_reldata;
2764 asection *output_section;
2765 const struct elf_backend_data *bed;
2766 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2767 struct bfd_elf_section_data *esdo;
2768
2769 output_section = input_section->output_section;
2770
2771 bed = get_elf_backend_data (output_bfd);
2772 esdo = elf_section_data (output_section);
2773 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2774 {
2775 output_reldata = &esdo->rel;
2776 swap_out = bed->s->swap_reloc_out;
2777 }
2778 else if (esdo->rela.hdr
2779 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2780 {
2781 output_reldata = &esdo->rela;
2782 swap_out = bed->s->swap_reloca_out;
2783 }
2784 else
2785 {
2786 _bfd_error_handler
2787 /* xgettext:c-format */
2788 (_("%pB: relocation size mismatch in %pB section %pA"),
2789 output_bfd, input_section->owner, input_section);
2790 bfd_set_error (bfd_error_wrong_format);
2791 return false;
2792 }
2793
2794 erel = output_reldata->hdr->contents;
2795 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2796 irela = internal_relocs;
2797 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2798 * bed->s->int_rels_per_ext_rel);
2799 while (irela < irelaend)
2800 {
2801 (*swap_out) (output_bfd, irela, erel);
2802 irela += bed->s->int_rels_per_ext_rel;
2803 erel += input_rel_hdr->sh_entsize;
2804 }
2805
2806 /* Bump the counter, so that we know where to add the next set of
2807 relocations. */
2808 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2809
2810 return true;
2811 }
2812 \f
2813 /* Make weak undefined symbols in PIE dynamic. */
2814
2815 bool
2816 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2817 struct elf_link_hash_entry *h)
2818 {
2819 if (bfd_link_pie (info)
2820 && h->dynindx == -1
2821 && h->root.type == bfd_link_hash_undefweak)
2822 return bfd_elf_link_record_dynamic_symbol (info, h);
2823
2824 return true;
2825 }
2826
2827 /* Fix up the flags for a symbol. This handles various cases which
2828 can only be fixed after all the input files are seen. This is
2829 currently called by both adjust_dynamic_symbol and
2830 assign_sym_version, which is unnecessary but perhaps more robust in
2831 the face of future changes. */
2832
2833 static bool
2834 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2835 struct elf_info_failed *eif)
2836 {
2837 const struct elf_backend_data *bed;
2838
2839 /* If this symbol was mentioned in a non-ELF file, try to set
2840 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2841 permit a non-ELF file to correctly refer to a symbol defined in
2842 an ELF dynamic object. */
2843 if (h->non_elf)
2844 {
2845 while (h->root.type == bfd_link_hash_indirect)
2846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2847
2848 if (h->root.type != bfd_link_hash_defined
2849 && h->root.type != bfd_link_hash_defweak)
2850 {
2851 h->ref_regular = 1;
2852 h->ref_regular_nonweak = 1;
2853 }
2854 else
2855 {
2856 if (h->root.u.def.section->owner != NULL
2857 && (bfd_get_flavour (h->root.u.def.section->owner)
2858 == bfd_target_elf_flavour))
2859 {
2860 h->ref_regular = 1;
2861 h->ref_regular_nonweak = 1;
2862 }
2863 else
2864 h->def_regular = 1;
2865 }
2866
2867 if (h->dynindx == -1
2868 && (h->def_dynamic
2869 || h->ref_dynamic))
2870 {
2871 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2872 {
2873 eif->failed = true;
2874 return false;
2875 }
2876 }
2877 }
2878 else
2879 {
2880 /* Unfortunately, NON_ELF is only correct if the symbol
2881 was first seen in a non-ELF file. Fortunately, if the symbol
2882 was first seen in an ELF file, we're probably OK unless the
2883 symbol was defined in a non-ELF file. Catch that case here.
2884 FIXME: We're still in trouble if the symbol was first seen in
2885 a dynamic object, and then later in a non-ELF regular object. */
2886 if ((h->root.type == bfd_link_hash_defined
2887 || h->root.type == bfd_link_hash_defweak)
2888 && !h->def_regular
2889 && (h->root.u.def.section->owner != NULL
2890 ? (bfd_get_flavour (h->root.u.def.section->owner)
2891 != bfd_target_elf_flavour)
2892 : (bfd_is_abs_section (h->root.u.def.section)
2893 && !h->def_dynamic)))
2894 h->def_regular = 1;
2895 }
2896
2897 /* Backend specific symbol fixup. */
2898 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2899 if (bed->elf_backend_fixup_symbol
2900 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2901 return false;
2902
2903 /* If this is a final link, and the symbol was defined as a common
2904 symbol in a regular object file, and there was no definition in
2905 any dynamic object, then the linker will have allocated space for
2906 the symbol in a common section but the DEF_REGULAR
2907 flag will not have been set. */
2908 if (h->root.type == bfd_link_hash_defined
2909 && !h->def_regular
2910 && h->ref_regular
2911 && !h->def_dynamic
2912 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2913 h->def_regular = 1;
2914
2915 /* Symbols defined in discarded sections shouldn't be dynamic. */
2916 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2917 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2918
2919 /* If a weak undefined symbol has non-default visibility, we also
2920 hide it from the dynamic linker. */
2921 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2922 && h->root.type == bfd_link_hash_undefweak)
2923 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2924
2925 /* A hidden versioned symbol in executable should be forced local if
2926 it is is locally defined, not referenced by shared library and not
2927 exported. */
2928 else if (bfd_link_executable (eif->info)
2929 && h->versioned == versioned_hidden
2930 && !eif->info->export_dynamic
2931 && !h->dynamic
2932 && !h->ref_dynamic
2933 && h->def_regular)
2934 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
2935
2936 /* If -Bsymbolic was used (which means to bind references to global
2937 symbols to the definition within the shared object), and this
2938 symbol was defined in a regular object, then it actually doesn't
2939 need a PLT entry. Likewise, if the symbol has non-default
2940 visibility. If the symbol has hidden or internal visibility, we
2941 will force it local. */
2942 else if (h->needs_plt
2943 && bfd_link_pic (eif->info)
2944 && is_elf_hash_table (eif->info->hash)
2945 && (SYMBOLIC_BIND (eif->info, h)
2946 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2947 && h->def_regular)
2948 {
2949 bool force_local;
2950
2951 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2952 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2953 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2954 }
2955
2956 /* If this is a weak defined symbol in a dynamic object, and we know
2957 the real definition in the dynamic object, copy interesting flags
2958 over to the real definition. */
2959 if (h->is_weakalias)
2960 {
2961 struct elf_link_hash_entry *def = weakdef (h);
2962
2963 /* If the real definition is defined by a regular object file,
2964 don't do anything special. See the longer description in
2965 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2966 bfd_link_hash_defined as it was when put on the alias list
2967 then it must have originally been a versioned symbol (for
2968 which a non-versioned indirect symbol is created) and later
2969 a definition for the non-versioned symbol is found. In that
2970 case the indirection is flipped with the versioned symbol
2971 becoming an indirect pointing at the non-versioned symbol.
2972 Thus, not an alias any more. */
2973 if (def->def_regular
2974 || def->root.type != bfd_link_hash_defined)
2975 {
2976 h = def;
2977 while ((h = h->u.alias) != def)
2978 h->is_weakalias = 0;
2979 }
2980 else
2981 {
2982 while (h->root.type == bfd_link_hash_indirect)
2983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2985 || h->root.type == bfd_link_hash_defweak);
2986 BFD_ASSERT (def->def_dynamic);
2987 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2988 }
2989 }
2990
2991 return true;
2992 }
2993
2994 /* Make the backend pick a good value for a dynamic symbol. This is
2995 called via elf_link_hash_traverse, and also calls itself
2996 recursively. */
2997
2998 static bool
2999 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3000 {
3001 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3002 struct elf_link_hash_table *htab;
3003 const struct elf_backend_data *bed;
3004
3005 if (! is_elf_hash_table (eif->info->hash))
3006 return false;
3007
3008 /* Ignore indirect symbols. These are added by the versioning code. */
3009 if (h->root.type == bfd_link_hash_indirect)
3010 return true;
3011
3012 /* Fix the symbol flags. */
3013 if (! _bfd_elf_fix_symbol_flags (h, eif))
3014 return false;
3015
3016 htab = elf_hash_table (eif->info);
3017 bed = get_elf_backend_data (htab->dynobj);
3018
3019 if (h->root.type == bfd_link_hash_undefweak)
3020 {
3021 if (eif->info->dynamic_undefined_weak == 0)
3022 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3023 else if (eif->info->dynamic_undefined_weak > 0
3024 && h->ref_regular
3025 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3026 && !bfd_hide_sym_by_version (eif->info->version_info,
3027 h->root.root.string))
3028 {
3029 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3030 {
3031 eif->failed = true;
3032 return false;
3033 }
3034 }
3035 }
3036
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
3044 if (!h->needs_plt
3045 && h->type != STT_GNU_IFUNC
3046 && (h->def_regular
3047 || !h->def_dynamic
3048 || (!h->ref_regular
3049 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3050 {
3051 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3052 return true;
3053 }
3054
3055 /* If we've already adjusted this symbol, don't do it again. This
3056 can happen via a recursive call. */
3057 if (h->dynamic_adjusted)
3058 return true;
3059
3060 /* Don't look at this symbol again. Note that we must set this
3061 after checking the above conditions, because we may look at a
3062 symbol once, decide not to do anything, and then get called
3063 recursively later after REF_REGULAR is set below. */
3064 h->dynamic_adjusted = 1;
3065
3066 /* If this is a weak definition, and we know a real definition, and
3067 the real symbol is not itself defined by a regular object file,
3068 then get a good value for the real definition. We handle the
3069 real symbol first, for the convenience of the backend routine.
3070
3071 Note that there is a confusing case here. If the real definition
3072 is defined by a regular object file, we don't get the real symbol
3073 from the dynamic object, but we do get the weak symbol. If the
3074 processor backend uses a COPY reloc, then if some routine in the
3075 dynamic object changes the real symbol, we will not see that
3076 change in the corresponding weak symbol. This is the way other
3077 ELF linkers work as well, and seems to be a result of the shared
3078 library model.
3079
3080 I will clarify this issue. Most SVR4 shared libraries define the
3081 variable _timezone and define timezone as a weak synonym. The
3082 tzset call changes _timezone. If you write
3083 extern int timezone;
3084 int _timezone = 5;
3085 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3086 you might expect that, since timezone is a synonym for _timezone,
3087 the same number will print both times. However, if the processor
3088 backend uses a COPY reloc, then actually timezone will be copied
3089 into your process image, and, since you define _timezone
3090 yourself, _timezone will not. Thus timezone and _timezone will
3091 wind up at different memory locations. The tzset call will set
3092 _timezone, leaving timezone unchanged. */
3093
3094 if (h->is_weakalias)
3095 {
3096 struct elf_link_hash_entry *def = weakdef (h);
3097
3098 /* If we get to this point, there is an implicit reference to
3099 the alias by a regular object file via the weak symbol H. */
3100 def->ref_regular = 1;
3101
3102 /* Ensure that the backend adjust_dynamic_symbol function sees
3103 the strong alias before H by recursively calling ourselves. */
3104 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3105 return false;
3106 }
3107
3108 /* If a symbol has no type and no size and does not require a PLT
3109 entry, then we are probably about to do the wrong thing here: we
3110 are probably going to create a COPY reloc for an empty object.
3111 This case can arise when a shared object is built with assembly
3112 code, and the assembly code fails to set the symbol type. */
3113 if (h->size == 0
3114 && h->type == STT_NOTYPE
3115 && !h->needs_plt)
3116 _bfd_error_handler
3117 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3118 h->root.root.string);
3119
3120 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3121 {
3122 eif->failed = true;
3123 return false;
3124 }
3125
3126 return true;
3127 }
3128
3129 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3130 DYNBSS. */
3131
3132 bool
3133 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3134 struct elf_link_hash_entry *h,
3135 asection *dynbss)
3136 {
3137 unsigned int power_of_two;
3138 bfd_vma mask;
3139 asection *sec = h->root.u.def.section;
3140
3141 /* The section alignment of the definition is the maximum alignment
3142 requirement of symbols defined in the section. Since we don't
3143 know the symbol alignment requirement, we start with the
3144 maximum alignment and check low bits of the symbol address
3145 for the minimum alignment. */
3146 power_of_two = bfd_section_alignment (sec);
3147 mask = ((bfd_vma) 1 << power_of_two) - 1;
3148 while ((h->root.u.def.value & mask) != 0)
3149 {
3150 mask >>= 1;
3151 --power_of_two;
3152 }
3153
3154 if (power_of_two > bfd_section_alignment (dynbss))
3155 {
3156 /* Adjust the section alignment if needed. */
3157 if (!bfd_set_section_alignment (dynbss, power_of_two))
3158 return false;
3159 }
3160
3161 /* We make sure that the symbol will be aligned properly. */
3162 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3163
3164 /* Define the symbol as being at this point in DYNBSS. */
3165 h->root.u.def.section = dynbss;
3166 h->root.u.def.value = dynbss->size;
3167
3168 /* Increment the size of DYNBSS to make room for the symbol. */
3169 dynbss->size += h->size;
3170
3171 /* No error if extern_protected_data is true. */
3172 if (h->protected_def
3173 && (!info->extern_protected_data
3174 || (info->extern_protected_data < 0
3175 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3176 info->callbacks->einfo
3177 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3178 h->root.root.string);
3179
3180 return true;
3181 }
3182
3183 /* Adjust all external symbols pointing into SEC_MERGE sections
3184 to reflect the object merging within the sections. */
3185
3186 static bool
3187 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3188 {
3189 asection *sec;
3190
3191 if ((h->root.type == bfd_link_hash_defined
3192 || h->root.type == bfd_link_hash_defweak)
3193 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3194 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3195 {
3196 bfd *output_bfd = (bfd *) data;
3197
3198 h->root.u.def.value =
3199 _bfd_merged_section_offset (output_bfd,
3200 &h->root.u.def.section,
3201 elf_section_data (sec)->sec_info,
3202 h->root.u.def.value);
3203 }
3204
3205 return true;
3206 }
3207
3208 /* Returns false if the symbol referred to by H should be considered
3209 to resolve local to the current module, and true if it should be
3210 considered to bind dynamically. */
3211
3212 bool
3213 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3214 struct bfd_link_info *info,
3215 bool not_local_protected)
3216 {
3217 bool binding_stays_local_p;
3218 const struct elf_backend_data *bed;
3219 struct elf_link_hash_table *hash_table;
3220
3221 if (h == NULL)
3222 return false;
3223
3224 while (h->root.type == bfd_link_hash_indirect
3225 || h->root.type == bfd_link_hash_warning)
3226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3227
3228 /* If it was forced local, then clearly it's not dynamic. */
3229 if (h->dynindx == -1)
3230 return false;
3231 if (h->forced_local)
3232 return false;
3233
3234 /* Identify the cases where name binding rules say that a
3235 visible symbol resolves locally. */
3236 binding_stays_local_p = (bfd_link_executable (info)
3237 || SYMBOLIC_BIND (info, h));
3238
3239 switch (ELF_ST_VISIBILITY (h->other))
3240 {
3241 case STV_INTERNAL:
3242 case STV_HIDDEN:
3243 return false;
3244
3245 case STV_PROTECTED:
3246 hash_table = elf_hash_table (info);
3247 if (!is_elf_hash_table (&hash_table->root))
3248 return false;
3249
3250 bed = get_elf_backend_data (hash_table->dynobj);
3251
3252 /* Proper resolution for function pointer equality may require
3253 that these symbols perhaps be resolved dynamically, even though
3254 we should be resolving them to the current module. */
3255 if (!not_local_protected || !bed->is_function_type (h->type))
3256 binding_stays_local_p = true;
3257 break;
3258
3259 default:
3260 break;
3261 }
3262
3263 /* If it isn't defined locally, then clearly it's dynamic. */
3264 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3265 return true;
3266
3267 /* Otherwise, the symbol is dynamic if binding rules don't tell
3268 us that it remains local. */
3269 return !binding_stays_local_p;
3270 }
3271
3272 /* Return true if the symbol referred to by H should be considered
3273 to resolve local to the current module, and false otherwise. Differs
3274 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3275 undefined symbols. The two functions are virtually identical except
3276 for the place where dynindx == -1 is tested. If that test is true,
3277 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3278 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3279 defined symbols.
3280 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3281 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3282 treatment of undefined weak symbols. For those that do not make
3283 undefined weak symbols dynamic, both functions may return false. */
3284
3285 bool
3286 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3287 struct bfd_link_info *info,
3288 bool local_protected)
3289 {
3290 const struct elf_backend_data *bed;
3291 struct elf_link_hash_table *hash_table;
3292
3293 /* If it's a local sym, of course we resolve locally. */
3294 if (h == NULL)
3295 return true;
3296
3297 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3298 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3299 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3300 return true;
3301
3302 /* Forced local symbols resolve locally. */
3303 if (h->forced_local)
3304 return true;
3305
3306 /* Common symbols that become definitions don't get the DEF_REGULAR
3307 flag set, so test it first, and don't bail out. */
3308 if (ELF_COMMON_DEF_P (h))
3309 /* Do nothing. */;
3310 /* If we don't have a definition in a regular file, then we can't
3311 resolve locally. The sym is either undefined or dynamic. */
3312 else if (!h->def_regular)
3313 return false;
3314
3315 /* Non-dynamic symbols resolve locally. */
3316 if (h->dynindx == -1)
3317 return true;
3318
3319 /* At this point, we know the symbol is defined and dynamic. In an
3320 executable it must resolve locally, likewise when building symbolic
3321 shared libraries. */
3322 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3323 return true;
3324
3325 /* Now deal with defined dynamic symbols in shared libraries. Ones
3326 with default visibility might not resolve locally. */
3327 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3328 return false;
3329
3330 hash_table = elf_hash_table (info);
3331 if (!is_elf_hash_table (&hash_table->root))
3332 return true;
3333
3334 bed = get_elf_backend_data (hash_table->dynobj);
3335
3336 /* If extern_protected_data is false, STV_PROTECTED non-function
3337 symbols are local. */
3338 if ((!info->extern_protected_data
3339 || (info->extern_protected_data < 0
3340 && !bed->extern_protected_data))
3341 && !bed->is_function_type (h->type))
3342 return true;
3343
3344 /* Function pointer equality tests may require that STV_PROTECTED
3345 symbols be treated as dynamic symbols. If the address of a
3346 function not defined in an executable is set to that function's
3347 plt entry in the executable, then the address of the function in
3348 a shared library must also be the plt entry in the executable. */
3349 return local_protected;
3350 }
3351
3352 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3353 aligned. Returns the first TLS output section. */
3354
3355 struct bfd_section *
3356 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3357 {
3358 struct bfd_section *sec, *tls;
3359 unsigned int align = 0;
3360
3361 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3362 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3363 break;
3364 tls = sec;
3365
3366 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3367 if (sec->alignment_power > align)
3368 align = sec->alignment_power;
3369
3370 elf_hash_table (info)->tls_sec = tls;
3371
3372 /* Ensure the alignment of the first section (usually .tdata) is the largest
3373 alignment, so that the tls segment starts aligned. */
3374 if (tls != NULL)
3375 tls->alignment_power = align;
3376
3377 return tls;
3378 }
3379
3380 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3381 static bool
3382 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3383 Elf_Internal_Sym *sym)
3384 {
3385 const struct elf_backend_data *bed;
3386
3387 /* Local symbols do not count, but target specific ones might. */
3388 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3389 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3390 return false;
3391
3392 bed = get_elf_backend_data (abfd);
3393 /* Function symbols do not count. */
3394 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3395 return false;
3396
3397 /* If the section is undefined, then so is the symbol. */
3398 if (sym->st_shndx == SHN_UNDEF)
3399 return false;
3400
3401 /* If the symbol is defined in the common section, then
3402 it is a common definition and so does not count. */
3403 if (bed->common_definition (sym))
3404 return false;
3405
3406 /* If the symbol is in a target specific section then we
3407 must rely upon the backend to tell us what it is. */
3408 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3409 /* FIXME - this function is not coded yet:
3410
3411 return _bfd_is_global_symbol_definition (abfd, sym);
3412
3413 Instead for now assume that the definition is not global,
3414 Even if this is wrong, at least the linker will behave
3415 in the same way that it used to do. */
3416 return false;
3417
3418 return true;
3419 }
3420
3421 /* Search the symbol table of the archive element of the archive ABFD
3422 whose archive map contains a mention of SYMDEF, and determine if
3423 the symbol is defined in this element. */
3424 static bool
3425 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3426 {
3427 Elf_Internal_Shdr * hdr;
3428 size_t symcount;
3429 size_t extsymcount;
3430 size_t extsymoff;
3431 Elf_Internal_Sym *isymbuf;
3432 Elf_Internal_Sym *isym;
3433 Elf_Internal_Sym *isymend;
3434 bool result;
3435
3436 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3437 if (abfd == NULL)
3438 return false;
3439
3440 if (! bfd_check_format (abfd, bfd_object))
3441 return false;
3442
3443 /* Select the appropriate symbol table. If we don't know if the
3444 object file is an IR object, give linker LTO plugin a chance to
3445 get the correct symbol table. */
3446 if (abfd->plugin_format == bfd_plugin_yes
3447 #if BFD_SUPPORTS_PLUGINS
3448 || (abfd->plugin_format == bfd_plugin_unknown
3449 && bfd_link_plugin_object_p (abfd))
3450 #endif
3451 )
3452 {
3453 /* Use the IR symbol table if the object has been claimed by
3454 plugin. */
3455 abfd = abfd->plugin_dummy_bfd;
3456 hdr = &elf_tdata (abfd)->symtab_hdr;
3457 }
3458 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3459 hdr = &elf_tdata (abfd)->symtab_hdr;
3460 else
3461 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3462
3463 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3464
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols. */
3467 if (elf_bad_symtab (abfd))
3468 {
3469 extsymcount = symcount;
3470 extsymoff = 0;
3471 }
3472 else
3473 {
3474 extsymcount = symcount - hdr->sh_info;
3475 extsymoff = hdr->sh_info;
3476 }
3477
3478 if (extsymcount == 0)
3479 return false;
3480
3481 /* Read in the symbol table. */
3482 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3483 NULL, NULL, NULL);
3484 if (isymbuf == NULL)
3485 return false;
3486
3487 /* Scan the symbol table looking for SYMDEF. */
3488 result = false;
3489 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3490 {
3491 const char *name;
3492
3493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3494 isym->st_name);
3495 if (name == NULL)
3496 break;
3497
3498 if (strcmp (name, symdef->name) == 0)
3499 {
3500 result = is_global_data_symbol_definition (abfd, isym);
3501 break;
3502 }
3503 }
3504
3505 free (isymbuf);
3506
3507 return result;
3508 }
3509 \f
3510 /* Add an entry to the .dynamic table. */
3511
3512 bool
3513 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3514 bfd_vma tag,
3515 bfd_vma val)
3516 {
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s;
3520 bfd_size_type newsize;
3521 bfd_byte *newcontents;
3522 Elf_Internal_Dyn dyn;
3523
3524 hash_table = elf_hash_table (info);
3525 if (! is_elf_hash_table (&hash_table->root))
3526 return false;
3527
3528 if (tag == DT_RELA || tag == DT_REL)
3529 hash_table->dynamic_relocs = true;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3533 BFD_ASSERT (s != NULL);
3534
3535 newsize = s->size + bed->s->sizeof_dyn;
3536 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3537 if (newcontents == NULL)
3538 return false;
3539
3540 dyn.d_tag = tag;
3541 dyn.d_un.d_val = val;
3542 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3543
3544 s->size = newsize;
3545 s->contents = newcontents;
3546
3547 return true;
3548 }
3549
3550 /* Strip zero-sized dynamic sections. */
3551
3552 bool
3553 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3554 {
3555 struct elf_link_hash_table *hash_table;
3556 const struct elf_backend_data *bed;
3557 asection *s, *sdynamic, **pp;
3558 asection *rela_dyn, *rel_dyn;
3559 Elf_Internal_Dyn dyn;
3560 bfd_byte *extdyn, *next;
3561 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3562 bool strip_zero_sized;
3563 bool strip_zero_sized_plt;
3564
3565 if (bfd_link_relocatable (info))
3566 return true;
3567
3568 hash_table = elf_hash_table (info);
3569 if (!is_elf_hash_table (&hash_table->root))
3570 return false;
3571
3572 if (!hash_table->dynobj)
3573 return true;
3574
3575 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3576 if (!sdynamic)
3577 return true;
3578
3579 bed = get_elf_backend_data (hash_table->dynobj);
3580 swap_dyn_in = bed->s->swap_dyn_in;
3581
3582 strip_zero_sized = false;
3583 strip_zero_sized_plt = false;
3584
3585 /* Strip zero-sized dynamic sections. */
3586 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3587 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3588 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3589 if (s->size == 0
3590 && (s == rela_dyn
3591 || s == rel_dyn
3592 || s == hash_table->srelplt->output_section
3593 || s == hash_table->splt->output_section))
3594 {
3595 *pp = s->next;
3596 info->output_bfd->section_count--;
3597 strip_zero_sized = true;
3598 if (s == rela_dyn)
3599 s = rela_dyn;
3600 if (s == rel_dyn)
3601 s = rel_dyn;
3602 else if (s == hash_table->splt->output_section)
3603 {
3604 s = hash_table->splt;
3605 strip_zero_sized_plt = true;
3606 }
3607 else
3608 s = hash_table->srelplt;
3609 s->flags |= SEC_EXCLUDE;
3610 s->output_section = bfd_abs_section_ptr;
3611 }
3612 else
3613 pp = &s->next;
3614
3615 if (strip_zero_sized_plt)
3616 for (extdyn = sdynamic->contents;
3617 extdyn < sdynamic->contents + sdynamic->size;
3618 extdyn = next)
3619 {
3620 next = extdyn + bed->s->sizeof_dyn;
3621 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3622 switch (dyn.d_tag)
3623 {
3624 default:
3625 break;
3626 case DT_JMPREL:
3627 case DT_PLTRELSZ:
3628 case DT_PLTREL:
3629 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3630 the procedure linkage table (the .plt section) has been
3631 removed. */
3632 memmove (extdyn, next,
3633 sdynamic->size - (next - sdynamic->contents));
3634 next = extdyn;
3635 }
3636 }
3637
3638 if (strip_zero_sized)
3639 {
3640 /* Regenerate program headers. */
3641 elf_seg_map (info->output_bfd) = NULL;
3642 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3643 }
3644
3645 return true;
3646 }
3647
3648 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3649 1 if a DT_NEEDED tag already exists, and 0 on success. */
3650
3651 int
3652 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3653 {
3654 struct elf_link_hash_table *hash_table;
3655 size_t strindex;
3656 const char *soname;
3657
3658 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3659 return -1;
3660
3661 hash_table = elf_hash_table (info);
3662 soname = elf_dt_name (abfd);
3663 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3664 if (strindex == (size_t) -1)
3665 return -1;
3666
3667 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3668 {
3669 asection *sdyn;
3670 const struct elf_backend_data *bed;
3671 bfd_byte *extdyn;
3672
3673 bed = get_elf_backend_data (hash_table->dynobj);
3674 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3675 if (sdyn != NULL)
3676 for (extdyn = sdyn->contents;
3677 extdyn < sdyn->contents + sdyn->size;
3678 extdyn += bed->s->sizeof_dyn)
3679 {
3680 Elf_Internal_Dyn dyn;
3681
3682 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3683 if (dyn.d_tag == DT_NEEDED
3684 && dyn.d_un.d_val == strindex)
3685 {
3686 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3687 return 1;
3688 }
3689 }
3690 }
3691
3692 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3693 return -1;
3694
3695 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3696 return -1;
3697
3698 return 0;
3699 }
3700
3701 /* Return true if SONAME is on the needed list between NEEDED and STOP
3702 (or the end of list if STOP is NULL), and needed by a library that
3703 will be loaded. */
3704
3705 static bool
3706 on_needed_list (const char *soname,
3707 struct bfd_link_needed_list *needed,
3708 struct bfd_link_needed_list *stop)
3709 {
3710 struct bfd_link_needed_list *look;
3711 for (look = needed; look != stop; look = look->next)
3712 if (strcmp (soname, look->name) == 0
3713 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3714 /* If needed by a library that itself is not directly
3715 needed, recursively check whether that library is
3716 indirectly needed. Since we add DT_NEEDED entries to
3717 the end of the list, library dependencies appear after
3718 the library. Therefore search prior to the current
3719 LOOK, preventing possible infinite recursion. */
3720 || on_needed_list (elf_dt_name (look->by), needed, look)))
3721 return true;
3722
3723 return false;
3724 }
3725
3726 /* Sort symbol by value, section, size, and type. */
3727 static int
3728 elf_sort_symbol (const void *arg1, const void *arg2)
3729 {
3730 const struct elf_link_hash_entry *h1;
3731 const struct elf_link_hash_entry *h2;
3732 bfd_signed_vma vdiff;
3733 int sdiff;
3734 const char *n1;
3735 const char *n2;
3736
3737 h1 = *(const struct elf_link_hash_entry **) arg1;
3738 h2 = *(const struct elf_link_hash_entry **) arg2;
3739 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3740 if (vdiff != 0)
3741 return vdiff > 0 ? 1 : -1;
3742
3743 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3744 if (sdiff != 0)
3745 return sdiff;
3746
3747 /* Sort so that sized symbols are selected over zero size symbols. */
3748 vdiff = h1->size - h2->size;
3749 if (vdiff != 0)
3750 return vdiff > 0 ? 1 : -1;
3751
3752 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3753 if (h1->type != h2->type)
3754 return h1->type - h2->type;
3755
3756 /* If symbols are properly sized and typed, and multiple strong
3757 aliases are not defined in a shared library by the user we
3758 shouldn't get here. Unfortunately linker script symbols like
3759 __bss_start sometimes match a user symbol defined at the start of
3760 .bss without proper size and type. We'd like to preference the
3761 user symbol over reserved system symbols. Sort on leading
3762 underscores. */
3763 n1 = h1->root.root.string;
3764 n2 = h2->root.root.string;
3765 while (*n1 == *n2)
3766 {
3767 if (*n1 == 0)
3768 break;
3769 ++n1;
3770 ++n2;
3771 }
3772 if (*n1 == '_')
3773 return -1;
3774 if (*n2 == '_')
3775 return 1;
3776
3777 /* Final sort on name selects user symbols like '_u' over reserved
3778 system symbols like '_Z' and also will avoid qsort instability. */
3779 return *n1 - *n2;
3780 }
3781
3782 /* This function is used to adjust offsets into .dynstr for
3783 dynamic symbols. This is called via elf_link_hash_traverse. */
3784
3785 static bool
3786 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3787 {
3788 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3789
3790 if (h->dynindx != -1)
3791 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3792 return true;
3793 }
3794
3795 /* Assign string offsets in .dynstr, update all structures referencing
3796 them. */
3797
3798 static bool
3799 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3800 {
3801 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3802 struct elf_link_local_dynamic_entry *entry;
3803 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3804 bfd *dynobj = hash_table->dynobj;
3805 asection *sdyn;
3806 bfd_size_type size;
3807 const struct elf_backend_data *bed;
3808 bfd_byte *extdyn;
3809
3810 _bfd_elf_strtab_finalize (dynstr);
3811 size = _bfd_elf_strtab_size (dynstr);
3812
3813 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3814
3815 if (info->callbacks->examine_strtab)
3816 info->callbacks->examine_strtab (dynstr);
3817
3818 bed = get_elf_backend_data (dynobj);
3819 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3820 BFD_ASSERT (sdyn != NULL);
3821
3822 /* Update all .dynamic entries referencing .dynstr strings. */
3823 for (extdyn = sdyn->contents;
3824 extdyn < sdyn->contents + sdyn->size;
3825 extdyn += bed->s->sizeof_dyn)
3826 {
3827 Elf_Internal_Dyn dyn;
3828
3829 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3830 switch (dyn.d_tag)
3831 {
3832 case DT_STRSZ:
3833 dyn.d_un.d_val = size;
3834 break;
3835 case DT_NEEDED:
3836 case DT_SONAME:
3837 case DT_RPATH:
3838 case DT_RUNPATH:
3839 case DT_FILTER:
3840 case DT_AUXILIARY:
3841 case DT_AUDIT:
3842 case DT_DEPAUDIT:
3843 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3844 break;
3845 default:
3846 continue;
3847 }
3848 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3849 }
3850
3851 /* Now update local dynamic symbols. */
3852 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3853 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3854 entry->isym.st_name);
3855
3856 /* And the rest of dynamic symbols. */
3857 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3858
3859 /* Adjust version definitions. */
3860 if (elf_tdata (output_bfd)->cverdefs)
3861 {
3862 asection *s;
3863 bfd_byte *p;
3864 size_t i;
3865 Elf_Internal_Verdef def;
3866 Elf_Internal_Verdaux defaux;
3867
3868 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3869 p = s->contents;
3870 do
3871 {
3872 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3873 &def);
3874 p += sizeof (Elf_External_Verdef);
3875 if (def.vd_aux != sizeof (Elf_External_Verdef))
3876 continue;
3877 for (i = 0; i < def.vd_cnt; ++i)
3878 {
3879 _bfd_elf_swap_verdaux_in (output_bfd,
3880 (Elf_External_Verdaux *) p, &defaux);
3881 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3882 defaux.vda_name);
3883 _bfd_elf_swap_verdaux_out (output_bfd,
3884 &defaux, (Elf_External_Verdaux *) p);
3885 p += sizeof (Elf_External_Verdaux);
3886 }
3887 }
3888 while (def.vd_next);
3889 }
3890
3891 /* Adjust version references. */
3892 if (elf_tdata (output_bfd)->verref)
3893 {
3894 asection *s;
3895 bfd_byte *p;
3896 size_t i;
3897 Elf_Internal_Verneed need;
3898 Elf_Internal_Vernaux needaux;
3899
3900 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3901 p = s->contents;
3902 do
3903 {
3904 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3905 &need);
3906 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3907 _bfd_elf_swap_verneed_out (output_bfd, &need,
3908 (Elf_External_Verneed *) p);
3909 p += sizeof (Elf_External_Verneed);
3910 for (i = 0; i < need.vn_cnt; ++i)
3911 {
3912 _bfd_elf_swap_vernaux_in (output_bfd,
3913 (Elf_External_Vernaux *) p, &needaux);
3914 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3915 needaux.vna_name);
3916 _bfd_elf_swap_vernaux_out (output_bfd,
3917 &needaux,
3918 (Elf_External_Vernaux *) p);
3919 p += sizeof (Elf_External_Vernaux);
3920 }
3921 }
3922 while (need.vn_next);
3923 }
3924
3925 return true;
3926 }
3927 \f
3928 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3929 The default is to only match when the INPUT and OUTPUT are exactly
3930 the same target. */
3931
3932 bool
3933 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3934 const bfd_target *output)
3935 {
3936 return input == output;
3937 }
3938
3939 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3940 This version is used when different targets for the same architecture
3941 are virtually identical. */
3942
3943 bool
3944 _bfd_elf_relocs_compatible (const bfd_target *input,
3945 const bfd_target *output)
3946 {
3947 const struct elf_backend_data *obed, *ibed;
3948
3949 if (input == output)
3950 return true;
3951
3952 ibed = xvec_get_elf_backend_data (input);
3953 obed = xvec_get_elf_backend_data (output);
3954
3955 if (ibed->arch != obed->arch)
3956 return false;
3957
3958 /* If both backends are using this function, deem them compatible. */
3959 return ibed->relocs_compatible == obed->relocs_compatible;
3960 }
3961
3962 /* Make a special call to the linker "notice" function to tell it that
3963 we are about to handle an as-needed lib, or have finished
3964 processing the lib. */
3965
3966 bool
3967 _bfd_elf_notice_as_needed (bfd *ibfd,
3968 struct bfd_link_info *info,
3969 enum notice_asneeded_action act)
3970 {
3971 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3972 }
3973
3974 /* Check relocations an ELF object file. */
3975
3976 bool
3977 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3978 {
3979 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3980 struct elf_link_hash_table *htab = elf_hash_table (info);
3981
3982 /* If this object is the same format as the output object, and it is
3983 not a shared library, then let the backend look through the
3984 relocs.
3985
3986 This is required to build global offset table entries and to
3987 arrange for dynamic relocs. It is not required for the
3988 particular common case of linking non PIC code, even when linking
3989 against shared libraries, but unfortunately there is no way of
3990 knowing whether an object file has been compiled PIC or not.
3991 Looking through the relocs is not particularly time consuming.
3992 The problem is that we must either (1) keep the relocs in memory,
3993 which causes the linker to require additional runtime memory or
3994 (2) read the relocs twice from the input file, which wastes time.
3995 This would be a good case for using mmap.
3996
3997 I have no idea how to handle linking PIC code into a file of a
3998 different format. It probably can't be done. */
3999 if ((abfd->flags & DYNAMIC) == 0
4000 && is_elf_hash_table (&htab->root)
4001 && bed->check_relocs != NULL
4002 && elf_object_id (abfd) == elf_hash_table_id (htab)
4003 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4004 {
4005 asection *o;
4006
4007 for (o = abfd->sections; o != NULL; o = o->next)
4008 {
4009 Elf_Internal_Rela *internal_relocs;
4010 bool ok;
4011
4012 /* Don't check relocations in excluded sections. Don't do
4013 anything special with non-loaded, non-alloced sections.
4014 In particular, any relocs in such sections should not
4015 affect GOT and PLT reference counting (ie. we don't
4016 allow them to create GOT or PLT entries), there's no
4017 possibility or desire to optimize TLS relocs, and
4018 there's not much point in propagating relocs to shared
4019 libs that the dynamic linker won't relocate. */
4020 if ((o->flags & SEC_ALLOC) == 0
4021 || (o->flags & SEC_RELOC) == 0
4022 || (o->flags & SEC_EXCLUDE) != 0
4023 || o->reloc_count == 0
4024 || ((info->strip == strip_all || info->strip == strip_debugger)
4025 && (o->flags & SEC_DEBUGGING) != 0)
4026 || bfd_is_abs_section (o->output_section))
4027 continue;
4028
4029 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4030 info->keep_memory);
4031 if (internal_relocs == NULL)
4032 return false;
4033
4034 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4035
4036 if (elf_section_data (o)->relocs != internal_relocs)
4037 free (internal_relocs);
4038
4039 if (! ok)
4040 return false;
4041 }
4042 }
4043
4044 return true;
4045 }
4046
4047 /* Add symbols from an ELF object file to the linker hash table. */
4048
4049 static bool
4050 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4051 {
4052 Elf_Internal_Ehdr *ehdr;
4053 Elf_Internal_Shdr *hdr;
4054 size_t symcount;
4055 size_t extsymcount;
4056 size_t extsymoff;
4057 struct elf_link_hash_entry **sym_hash;
4058 bool dynamic;
4059 Elf_External_Versym *extversym = NULL;
4060 Elf_External_Versym *extversym_end = NULL;
4061 Elf_External_Versym *ever;
4062 struct elf_link_hash_entry *weaks;
4063 struct elf_link_hash_entry **nondeflt_vers = NULL;
4064 size_t nondeflt_vers_cnt = 0;
4065 Elf_Internal_Sym *isymbuf = NULL;
4066 Elf_Internal_Sym *isym;
4067 Elf_Internal_Sym *isymend;
4068 const struct elf_backend_data *bed;
4069 bool add_needed;
4070 struct elf_link_hash_table *htab;
4071 void *alloc_mark = NULL;
4072 struct bfd_hash_entry **old_table = NULL;
4073 unsigned int old_size = 0;
4074 unsigned int old_count = 0;
4075 void *old_tab = NULL;
4076 void *old_ent;
4077 struct bfd_link_hash_entry *old_undefs = NULL;
4078 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4079 void *old_strtab = NULL;
4080 size_t tabsize = 0;
4081 asection *s;
4082 bool just_syms;
4083
4084 htab = elf_hash_table (info);
4085 bed = get_elf_backend_data (abfd);
4086
4087 if ((abfd->flags & DYNAMIC) == 0)
4088 dynamic = false;
4089 else
4090 {
4091 dynamic = true;
4092
4093 /* You can't use -r against a dynamic object. Also, there's no
4094 hope of using a dynamic object which does not exactly match
4095 the format of the output file. */
4096 if (bfd_link_relocatable (info)
4097 || !is_elf_hash_table (&htab->root)
4098 || info->output_bfd->xvec != abfd->xvec)
4099 {
4100 if (bfd_link_relocatable (info))
4101 bfd_set_error (bfd_error_invalid_operation);
4102 else
4103 bfd_set_error (bfd_error_wrong_format);
4104 goto error_return;
4105 }
4106 }
4107
4108 ehdr = elf_elfheader (abfd);
4109 if (info->warn_alternate_em
4110 && bed->elf_machine_code != ehdr->e_machine
4111 && ((bed->elf_machine_alt1 != 0
4112 && ehdr->e_machine == bed->elf_machine_alt1)
4113 || (bed->elf_machine_alt2 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt2)))
4115 _bfd_error_handler
4116 /* xgettext:c-format */
4117 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4118 ehdr->e_machine, abfd, bed->elf_machine_code);
4119
4120 /* As a GNU extension, any input sections which are named
4121 .gnu.warning.SYMBOL are treated as warning symbols for the given
4122 symbol. This differs from .gnu.warning sections, which generate
4123 warnings when they are included in an output file. */
4124 /* PR 12761: Also generate this warning when building shared libraries. */
4125 for (s = abfd->sections; s != NULL; s = s->next)
4126 {
4127 const char *name;
4128
4129 name = bfd_section_name (s);
4130 if (startswith (name, ".gnu.warning."))
4131 {
4132 char *msg;
4133 bfd_size_type sz;
4134
4135 name += sizeof ".gnu.warning." - 1;
4136
4137 /* If this is a shared object, then look up the symbol
4138 in the hash table. If it is there, and it is already
4139 been defined, then we will not be using the entry
4140 from this shared object, so we don't need to warn.
4141 FIXME: If we see the definition in a regular object
4142 later on, we will warn, but we shouldn't. The only
4143 fix is to keep track of what warnings we are supposed
4144 to emit, and then handle them all at the end of the
4145 link. */
4146 if (dynamic)
4147 {
4148 struct elf_link_hash_entry *h;
4149
4150 h = elf_link_hash_lookup (htab, name, false, false, true);
4151
4152 /* FIXME: What about bfd_link_hash_common? */
4153 if (h != NULL
4154 && (h->root.type == bfd_link_hash_defined
4155 || h->root.type == bfd_link_hash_defweak))
4156 continue;
4157 }
4158
4159 sz = s->size;
4160 msg = (char *) bfd_alloc (abfd, sz + 1);
4161 if (msg == NULL)
4162 goto error_return;
4163
4164 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4165 goto error_return;
4166
4167 msg[sz] = '\0';
4168
4169 if (! (_bfd_generic_link_add_one_symbol
4170 (info, abfd, name, BSF_WARNING, s, 0, msg,
4171 false, bed->collect, NULL)))
4172 goto error_return;
4173
4174 if (bfd_link_executable (info))
4175 {
4176 /* Clobber the section size so that the warning does
4177 not get copied into the output file. */
4178 s->size = 0;
4179
4180 /* Also set SEC_EXCLUDE, so that symbols defined in
4181 the warning section don't get copied to the output. */
4182 s->flags |= SEC_EXCLUDE;
4183 }
4184 }
4185 }
4186
4187 just_syms = ((s = abfd->sections) != NULL
4188 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4189
4190 add_needed = true;
4191 if (! dynamic)
4192 {
4193 /* If we are creating a shared library, create all the dynamic
4194 sections immediately. We need to attach them to something,
4195 so we attach them to this BFD, provided it is the right
4196 format and is not from ld --just-symbols. Always create the
4197 dynamic sections for -E/--dynamic-list. FIXME: If there
4198 are no input BFD's of the same format as the output, we can't
4199 make a shared library. */
4200 if (!just_syms
4201 && (bfd_link_pic (info)
4202 || (!bfd_link_relocatable (info)
4203 && info->nointerp
4204 && (info->export_dynamic || info->dynamic)))
4205 && is_elf_hash_table (&htab->root)
4206 && info->output_bfd->xvec == abfd->xvec
4207 && !htab->dynamic_sections_created)
4208 {
4209 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4210 goto error_return;
4211 }
4212 }
4213 else if (!is_elf_hash_table (&htab->root))
4214 goto error_return;
4215 else
4216 {
4217 const char *soname = NULL;
4218 char *audit = NULL;
4219 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4220 const Elf_Internal_Phdr *phdr;
4221 struct elf_link_loaded_list *loaded_lib;
4222
4223 /* ld --just-symbols and dynamic objects don't mix very well.
4224 ld shouldn't allow it. */
4225 if (just_syms)
4226 abort ();
4227
4228 /* If this dynamic lib was specified on the command line with
4229 --as-needed in effect, then we don't want to add a DT_NEEDED
4230 tag unless the lib is actually used. Similary for libs brought
4231 in by another lib's DT_NEEDED. When --no-add-needed is used
4232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4233 any dynamic library in DT_NEEDED tags in the dynamic lib at
4234 all. */
4235 add_needed = (elf_dyn_lib_class (abfd)
4236 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4237 | DYN_NO_NEEDED)) == 0;
4238
4239 s = bfd_get_section_by_name (abfd, ".dynamic");
4240 if (s != NULL)
4241 {
4242 bfd_byte *dynbuf;
4243 bfd_byte *extdyn;
4244 unsigned int elfsec;
4245 unsigned long shlink;
4246
4247 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4248 {
4249 error_free_dyn:
4250 free (dynbuf);
4251 goto error_return;
4252 }
4253
4254 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4255 if (elfsec == SHN_BAD)
4256 goto error_free_dyn;
4257 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4258
4259 for (extdyn = dynbuf;
4260 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4261 extdyn += bed->s->sizeof_dyn)
4262 {
4263 Elf_Internal_Dyn dyn;
4264
4265 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4266 if (dyn.d_tag == DT_SONAME)
4267 {
4268 unsigned int tagv = dyn.d_un.d_val;
4269 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4270 if (soname == NULL)
4271 goto error_free_dyn;
4272 }
4273 if (dyn.d_tag == DT_NEEDED)
4274 {
4275 struct bfd_link_needed_list *n, **pn;
4276 char *fnm, *anm;
4277 unsigned int tagv = dyn.d_un.d_val;
4278 size_t amt = sizeof (struct bfd_link_needed_list);
4279
4280 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4281 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4282 if (n == NULL || fnm == NULL)
4283 goto error_free_dyn;
4284 amt = strlen (fnm) + 1;
4285 anm = (char *) bfd_alloc (abfd, amt);
4286 if (anm == NULL)
4287 goto error_free_dyn;
4288 memcpy (anm, fnm, amt);
4289 n->name = anm;
4290 n->by = abfd;
4291 n->next = NULL;
4292 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4293 ;
4294 *pn = n;
4295 }
4296 if (dyn.d_tag == DT_RUNPATH)
4297 {
4298 struct bfd_link_needed_list *n, **pn;
4299 char *fnm, *anm;
4300 unsigned int tagv = dyn.d_un.d_val;
4301 size_t amt = sizeof (struct bfd_link_needed_list);
4302
4303 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4304 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4305 if (n == NULL || fnm == NULL)
4306 goto error_free_dyn;
4307 amt = strlen (fnm) + 1;
4308 anm = (char *) bfd_alloc (abfd, amt);
4309 if (anm == NULL)
4310 goto error_free_dyn;
4311 memcpy (anm, fnm, amt);
4312 n->name = anm;
4313 n->by = abfd;
4314 n->next = NULL;
4315 for (pn = & runpath;
4316 *pn != NULL;
4317 pn = &(*pn)->next)
4318 ;
4319 *pn = n;
4320 }
4321 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4322 if (!runpath && dyn.d_tag == DT_RPATH)
4323 {
4324 struct bfd_link_needed_list *n, **pn;
4325 char *fnm, *anm;
4326 unsigned int tagv = dyn.d_un.d_val;
4327 size_t amt = sizeof (struct bfd_link_needed_list);
4328
4329 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4330 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4331 if (n == NULL || fnm == NULL)
4332 goto error_free_dyn;
4333 amt = strlen (fnm) + 1;
4334 anm = (char *) bfd_alloc (abfd, amt);
4335 if (anm == NULL)
4336 goto error_free_dyn;
4337 memcpy (anm, fnm, amt);
4338 n->name = anm;
4339 n->by = abfd;
4340 n->next = NULL;
4341 for (pn = & rpath;
4342 *pn != NULL;
4343 pn = &(*pn)->next)
4344 ;
4345 *pn = n;
4346 }
4347 if (dyn.d_tag == DT_AUDIT)
4348 {
4349 unsigned int tagv = dyn.d_un.d_val;
4350 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4351 }
4352 if (dyn.d_tag == DT_FLAGS_1)
4353 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4354 }
4355
4356 free (dynbuf);
4357 }
4358
4359 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4360 frees all more recently bfd_alloc'd blocks as well. */
4361 if (runpath)
4362 rpath = runpath;
4363
4364 if (rpath)
4365 {
4366 struct bfd_link_needed_list **pn;
4367 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4368 ;
4369 *pn = rpath;
4370 }
4371
4372 /* If we have a PT_GNU_RELRO program header, mark as read-only
4373 all sections contained fully therein. This makes relro
4374 shared library sections appear as they will at run-time. */
4375 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4376 while (phdr-- > elf_tdata (abfd)->phdr)
4377 if (phdr->p_type == PT_GNU_RELRO)
4378 {
4379 for (s = abfd->sections; s != NULL; s = s->next)
4380 {
4381 unsigned int opb = bfd_octets_per_byte (abfd, s);
4382
4383 if ((s->flags & SEC_ALLOC) != 0
4384 && s->vma * opb >= phdr->p_vaddr
4385 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4386 s->flags |= SEC_READONLY;
4387 }
4388 break;
4389 }
4390
4391 /* We do not want to include any of the sections in a dynamic
4392 object in the output file. We hack by simply clobbering the
4393 list of sections in the BFD. This could be handled more
4394 cleanly by, say, a new section flag; the existing
4395 SEC_NEVER_LOAD flag is not the one we want, because that one
4396 still implies that the section takes up space in the output
4397 file. */
4398 bfd_section_list_clear (abfd);
4399
4400 /* Find the name to use in a DT_NEEDED entry that refers to this
4401 object. If the object has a DT_SONAME entry, we use it.
4402 Otherwise, if the generic linker stuck something in
4403 elf_dt_name, we use that. Otherwise, we just use the file
4404 name. */
4405 if (soname == NULL || *soname == '\0')
4406 {
4407 soname = elf_dt_name (abfd);
4408 if (soname == NULL || *soname == '\0')
4409 soname = bfd_get_filename (abfd);
4410 }
4411
4412 /* Save the SONAME because sometimes the linker emulation code
4413 will need to know it. */
4414 elf_dt_name (abfd) = soname;
4415
4416 /* If we have already included this dynamic object in the
4417 link, just ignore it. There is no reason to include a
4418 particular dynamic object more than once. */
4419 for (loaded_lib = htab->dyn_loaded;
4420 loaded_lib != NULL;
4421 loaded_lib = loaded_lib->next)
4422 {
4423 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4424 return true;
4425 }
4426
4427 /* Create dynamic sections for backends that require that be done
4428 before setup_gnu_properties. */
4429 if (add_needed
4430 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4431 return false;
4432
4433 /* Save the DT_AUDIT entry for the linker emulation code. */
4434 elf_dt_audit (abfd) = audit;
4435 }
4436
4437 /* If this is a dynamic object, we always link against the .dynsym
4438 symbol table, not the .symtab symbol table. The dynamic linker
4439 will only see the .dynsym symbol table, so there is no reason to
4440 look at .symtab for a dynamic object. */
4441
4442 if (! dynamic || elf_dynsymtab (abfd) == 0)
4443 hdr = &elf_tdata (abfd)->symtab_hdr;
4444 else
4445 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4446
4447 symcount = hdr->sh_size / bed->s->sizeof_sym;
4448
4449 /* The sh_info field of the symtab header tells us where the
4450 external symbols start. We don't care about the local symbols at
4451 this point. */
4452 if (elf_bad_symtab (abfd))
4453 {
4454 extsymcount = symcount;
4455 extsymoff = 0;
4456 }
4457 else
4458 {
4459 extsymcount = symcount - hdr->sh_info;
4460 extsymoff = hdr->sh_info;
4461 }
4462
4463 sym_hash = elf_sym_hashes (abfd);
4464 if (extsymcount != 0)
4465 {
4466 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4467 NULL, NULL, NULL);
4468 if (isymbuf == NULL)
4469 goto error_return;
4470
4471 if (sym_hash == NULL)
4472 {
4473 /* We store a pointer to the hash table entry for each
4474 external symbol. */
4475 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4476 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4477 if (sym_hash == NULL)
4478 goto error_free_sym;
4479 elf_sym_hashes (abfd) = sym_hash;
4480 }
4481 }
4482
4483 if (dynamic)
4484 {
4485 /* Read in any version definitions. */
4486 if (!_bfd_elf_slurp_version_tables (abfd,
4487 info->default_imported_symver))
4488 goto error_free_sym;
4489
4490 /* Read in the symbol versions, but don't bother to convert them
4491 to internal format. */
4492 if (elf_dynversym (abfd) != 0)
4493 {
4494 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4495 bfd_size_type amt = versymhdr->sh_size;
4496
4497 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4498 goto error_free_sym;
4499 extversym = (Elf_External_Versym *)
4500 _bfd_malloc_and_read (abfd, amt, amt);
4501 if (extversym == NULL)
4502 goto error_free_sym;
4503 extversym_end = extversym + amt / sizeof (*extversym);
4504 }
4505 }
4506
4507 /* If we are loading an as-needed shared lib, save the symbol table
4508 state before we start adding symbols. If the lib turns out
4509 to be unneeded, restore the state. */
4510 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4511 {
4512 unsigned int i;
4513 size_t entsize;
4514
4515 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4516 {
4517 struct bfd_hash_entry *p;
4518 struct elf_link_hash_entry *h;
4519
4520 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4521 {
4522 h = (struct elf_link_hash_entry *) p;
4523 entsize += htab->root.table.entsize;
4524 if (h->root.type == bfd_link_hash_warning)
4525 {
4526 entsize += htab->root.table.entsize;
4527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4528 }
4529 if (h->root.type == bfd_link_hash_common)
4530 entsize += sizeof (*h->root.u.c.p);
4531 }
4532 }
4533
4534 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4535 old_tab = bfd_malloc (tabsize + entsize);
4536 if (old_tab == NULL)
4537 goto error_free_vers;
4538
4539 /* Remember the current objalloc pointer, so that all mem for
4540 symbols added can later be reclaimed. */
4541 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4542 if (alloc_mark == NULL)
4543 goto error_free_vers;
4544
4545 /* Make a special call to the linker "notice" function to
4546 tell it that we are about to handle an as-needed lib. */
4547 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4548 goto error_free_vers;
4549
4550 /* Clone the symbol table. Remember some pointers into the
4551 symbol table, and dynamic symbol count. */
4552 old_ent = (char *) old_tab + tabsize;
4553 memcpy (old_tab, htab->root.table.table, tabsize);
4554 old_undefs = htab->root.undefs;
4555 old_undefs_tail = htab->root.undefs_tail;
4556 old_table = htab->root.table.table;
4557 old_size = htab->root.table.size;
4558 old_count = htab->root.table.count;
4559 old_strtab = NULL;
4560 if (htab->dynstr != NULL)
4561 {
4562 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4563 if (old_strtab == NULL)
4564 goto error_free_vers;
4565 }
4566
4567 for (i = 0; i < htab->root.table.size; i++)
4568 {
4569 struct bfd_hash_entry *p;
4570 struct elf_link_hash_entry *h;
4571
4572 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4573 {
4574 h = (struct elf_link_hash_entry *) p;
4575 memcpy (old_ent, h, htab->root.table.entsize);
4576 old_ent = (char *) old_ent + htab->root.table.entsize;
4577 if (h->root.type == bfd_link_hash_warning)
4578 {
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4580 memcpy (old_ent, h, htab->root.table.entsize);
4581 old_ent = (char *) old_ent + htab->root.table.entsize;
4582 }
4583 if (h->root.type == bfd_link_hash_common)
4584 {
4585 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4586 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4587 }
4588 }
4589 }
4590 }
4591
4592 weaks = NULL;
4593 if (extversym == NULL)
4594 ever = NULL;
4595 else if (extversym + extsymoff < extversym_end)
4596 ever = extversym + extsymoff;
4597 else
4598 {
4599 /* xgettext:c-format */
4600 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4601 abfd, (long) extsymoff,
4602 (long) (extversym_end - extversym) / sizeof (* extversym));
4603 bfd_set_error (bfd_error_bad_value);
4604 goto error_free_vers;
4605 }
4606
4607 if (!bfd_link_relocatable (info)
4608 && abfd->lto_slim_object)
4609 {
4610 _bfd_error_handler
4611 (_("%pB: plugin needed to handle lto object"), abfd);
4612 }
4613
4614 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4615 isym < isymend;
4616 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4617 {
4618 int bind;
4619 bfd_vma value;
4620 asection *sec, *new_sec;
4621 flagword flags;
4622 const char *name;
4623 struct elf_link_hash_entry *h;
4624 struct elf_link_hash_entry *hi;
4625 bool definition;
4626 bool size_change_ok;
4627 bool type_change_ok;
4628 bool new_weak;
4629 bool old_weak;
4630 bfd *override;
4631 bool common;
4632 bool discarded;
4633 unsigned int old_alignment;
4634 unsigned int shindex;
4635 bfd *old_bfd;
4636 bool matched;
4637
4638 override = NULL;
4639
4640 flags = BSF_NO_FLAGS;
4641 sec = NULL;
4642 value = isym->st_value;
4643 common = bed->common_definition (isym);
4644 if (common && info->inhibit_common_definition)
4645 {
4646 /* Treat common symbol as undefined for --no-define-common. */
4647 isym->st_shndx = SHN_UNDEF;
4648 common = false;
4649 }
4650 discarded = false;
4651
4652 bind = ELF_ST_BIND (isym->st_info);
4653 switch (bind)
4654 {
4655 case STB_LOCAL:
4656 /* This should be impossible, since ELF requires that all
4657 global symbols follow all local symbols, and that sh_info
4658 point to the first global symbol. Unfortunately, Irix 5
4659 screws this up. */
4660 if (elf_bad_symtab (abfd))
4661 continue;
4662
4663 /* If we aren't prepared to handle locals within the globals
4664 then we'll likely segfault on a NULL symbol hash if the
4665 symbol is ever referenced in relocations. */
4666 shindex = elf_elfheader (abfd)->e_shstrndx;
4667 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4668 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4669 " (>= sh_info of %lu)"),
4670 abfd, name, (long) (isym - isymbuf + extsymoff),
4671 (long) extsymoff);
4672
4673 /* Dynamic object relocations are not processed by ld, so
4674 ld won't run into the problem mentioned above. */
4675 if (dynamic)
4676 continue;
4677 bfd_set_error (bfd_error_bad_value);
4678 goto error_free_vers;
4679
4680 case STB_GLOBAL:
4681 if (isym->st_shndx != SHN_UNDEF && !common)
4682 flags = BSF_GLOBAL;
4683 break;
4684
4685 case STB_WEAK:
4686 flags = BSF_WEAK;
4687 break;
4688
4689 case STB_GNU_UNIQUE:
4690 flags = BSF_GNU_UNIQUE;
4691 break;
4692
4693 default:
4694 /* Leave it up to the processor backend. */
4695 break;
4696 }
4697
4698 if (isym->st_shndx == SHN_UNDEF)
4699 sec = bfd_und_section_ptr;
4700 else if (isym->st_shndx == SHN_ABS)
4701 sec = bfd_abs_section_ptr;
4702 else if (isym->st_shndx == SHN_COMMON)
4703 {
4704 sec = bfd_com_section_ptr;
4705 /* What ELF calls the size we call the value. What ELF
4706 calls the value we call the alignment. */
4707 value = isym->st_size;
4708 }
4709 else
4710 {
4711 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4712 if (sec == NULL)
4713 sec = bfd_abs_section_ptr;
4714 else if (discarded_section (sec))
4715 {
4716 /* Symbols from discarded section are undefined. We keep
4717 its visibility. */
4718 sec = bfd_und_section_ptr;
4719 discarded = true;
4720 isym->st_shndx = SHN_UNDEF;
4721 }
4722 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4723 value -= sec->vma;
4724 }
4725
4726 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4727 isym->st_name);
4728 if (name == NULL)
4729 goto error_free_vers;
4730
4731 if (isym->st_shndx == SHN_COMMON
4732 && (abfd->flags & BFD_PLUGIN) != 0)
4733 {
4734 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4735
4736 if (xc == NULL)
4737 {
4738 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4739 | SEC_EXCLUDE);
4740 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4741 if (xc == NULL)
4742 goto error_free_vers;
4743 }
4744 sec = xc;
4745 }
4746 else if (isym->st_shndx == SHN_COMMON
4747 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4748 && !bfd_link_relocatable (info))
4749 {
4750 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4751
4752 if (tcomm == NULL)
4753 {
4754 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4755 | SEC_LINKER_CREATED);
4756 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4757 if (tcomm == NULL)
4758 goto error_free_vers;
4759 }
4760 sec = tcomm;
4761 }
4762 else if (bed->elf_add_symbol_hook)
4763 {
4764 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4765 &sec, &value))
4766 goto error_free_vers;
4767
4768 /* The hook function sets the name to NULL if this symbol
4769 should be skipped for some reason. */
4770 if (name == NULL)
4771 continue;
4772 }
4773
4774 /* Sanity check that all possibilities were handled. */
4775 if (sec == NULL)
4776 abort ();
4777
4778 /* Silently discard TLS symbols from --just-syms. There's
4779 no way to combine a static TLS block with a new TLS block
4780 for this executable. */
4781 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4782 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4783 continue;
4784
4785 if (bfd_is_und_section (sec)
4786 || bfd_is_com_section (sec))
4787 definition = false;
4788 else
4789 definition = true;
4790
4791 size_change_ok = false;
4792 type_change_ok = bed->type_change_ok;
4793 old_weak = false;
4794 matched = false;
4795 old_alignment = 0;
4796 old_bfd = NULL;
4797 new_sec = sec;
4798
4799 if (is_elf_hash_table (&htab->root))
4800 {
4801 Elf_Internal_Versym iver;
4802 unsigned int vernum = 0;
4803 bool skip;
4804
4805 if (ever == NULL)
4806 {
4807 if (info->default_imported_symver)
4808 /* Use the default symbol version created earlier. */
4809 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4810 else
4811 iver.vs_vers = 0;
4812 }
4813 else if (ever >= extversym_end)
4814 {
4815 /* xgettext:c-format */
4816 _bfd_error_handler (_("%pB: not enough version information"),
4817 abfd);
4818 bfd_set_error (bfd_error_bad_value);
4819 goto error_free_vers;
4820 }
4821 else
4822 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4823
4824 vernum = iver.vs_vers & VERSYM_VERSION;
4825
4826 /* If this is a hidden symbol, or if it is not version
4827 1, we append the version name to the symbol name.
4828 However, we do not modify a non-hidden absolute symbol
4829 if it is not a function, because it might be the version
4830 symbol itself. FIXME: What if it isn't? */
4831 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4832 || (vernum > 1
4833 && (!bfd_is_abs_section (sec)
4834 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4835 {
4836 const char *verstr;
4837 size_t namelen, verlen, newlen;
4838 char *newname, *p;
4839
4840 if (isym->st_shndx != SHN_UNDEF)
4841 {
4842 if (vernum > elf_tdata (abfd)->cverdefs)
4843 verstr = NULL;
4844 else if (vernum > 1)
4845 verstr =
4846 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4847 else
4848 verstr = "";
4849
4850 if (verstr == NULL)
4851 {
4852 _bfd_error_handler
4853 /* xgettext:c-format */
4854 (_("%pB: %s: invalid version %u (max %d)"),
4855 abfd, name, vernum,
4856 elf_tdata (abfd)->cverdefs);
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4859 }
4860 }
4861 else
4862 {
4863 /* We cannot simply test for the number of
4864 entries in the VERNEED section since the
4865 numbers for the needed versions do not start
4866 at 0. */
4867 Elf_Internal_Verneed *t;
4868
4869 verstr = NULL;
4870 for (t = elf_tdata (abfd)->verref;
4871 t != NULL;
4872 t = t->vn_nextref)
4873 {
4874 Elf_Internal_Vernaux *a;
4875
4876 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4877 {
4878 if (a->vna_other == vernum)
4879 {
4880 verstr = a->vna_nodename;
4881 break;
4882 }
4883 }
4884 if (a != NULL)
4885 break;
4886 }
4887 if (verstr == NULL)
4888 {
4889 _bfd_error_handler
4890 /* xgettext:c-format */
4891 (_("%pB: %s: invalid needed version %d"),
4892 abfd, name, vernum);
4893 bfd_set_error (bfd_error_bad_value);
4894 goto error_free_vers;
4895 }
4896 }
4897
4898 namelen = strlen (name);
4899 verlen = strlen (verstr);
4900 newlen = namelen + verlen + 2;
4901 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4902 && isym->st_shndx != SHN_UNDEF)
4903 ++newlen;
4904
4905 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4906 if (newname == NULL)
4907 goto error_free_vers;
4908 memcpy (newname, name, namelen);
4909 p = newname + namelen;
4910 *p++ = ELF_VER_CHR;
4911 /* If this is a defined non-hidden version symbol,
4912 we add another @ to the name. This indicates the
4913 default version of the symbol. */
4914 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4915 && isym->st_shndx != SHN_UNDEF)
4916 *p++ = ELF_VER_CHR;
4917 memcpy (p, verstr, verlen + 1);
4918
4919 name = newname;
4920 }
4921
4922 /* If this symbol has default visibility and the user has
4923 requested we not re-export it, then mark it as hidden. */
4924 if (!bfd_is_und_section (sec)
4925 && !dynamic
4926 && abfd->no_export
4927 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4928 isym->st_other = (STV_HIDDEN
4929 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4930
4931 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4932 sym_hash, &old_bfd, &old_weak,
4933 &old_alignment, &skip, &override,
4934 &type_change_ok, &size_change_ok,
4935 &matched))
4936 goto error_free_vers;
4937
4938 if (skip)
4939 continue;
4940
4941 /* Override a definition only if the new symbol matches the
4942 existing one. */
4943 if (override && matched)
4944 definition = false;
4945
4946 h = *sym_hash;
4947 while (h->root.type == bfd_link_hash_indirect
4948 || h->root.type == bfd_link_hash_warning)
4949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4950
4951 if (elf_tdata (abfd)->verdef != NULL
4952 && vernum > 1
4953 && definition)
4954 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4955 }
4956
4957 if (! (_bfd_generic_link_add_one_symbol
4958 (info, override ? override : abfd, name, flags, sec, value,
4959 NULL, false, bed->collect,
4960 (struct bfd_link_hash_entry **) sym_hash)))
4961 goto error_free_vers;
4962
4963 h = *sym_hash;
4964 /* We need to make sure that indirect symbol dynamic flags are
4965 updated. */
4966 hi = h;
4967 while (h->root.type == bfd_link_hash_indirect
4968 || h->root.type == bfd_link_hash_warning)
4969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4970
4971 *sym_hash = h;
4972
4973 /* Setting the index to -3 tells elf_link_output_extsym that
4974 this symbol is defined in a discarded section. */
4975 if (discarded && is_elf_hash_table (&htab->root))
4976 h->indx = -3;
4977
4978 new_weak = (flags & BSF_WEAK) != 0;
4979 if (dynamic
4980 && definition
4981 && new_weak
4982 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4983 && is_elf_hash_table (&htab->root)
4984 && h->u.alias == NULL)
4985 {
4986 /* Keep a list of all weak defined non function symbols from
4987 a dynamic object, using the alias field. Later in this
4988 function we will set the alias field to the correct
4989 value. We only put non-function symbols from dynamic
4990 objects on this list, because that happens to be the only
4991 time we need to know the normal symbol corresponding to a
4992 weak symbol, and the information is time consuming to
4993 figure out. If the alias field is not already NULL,
4994 then this symbol was already defined by some previous
4995 dynamic object, and we will be using that previous
4996 definition anyhow. */
4997
4998 h->u.alias = weaks;
4999 weaks = h;
5000 }
5001
5002 /* Set the alignment of a common symbol. */
5003 if ((common || bfd_is_com_section (sec))
5004 && h->root.type == bfd_link_hash_common)
5005 {
5006 unsigned int align;
5007
5008 if (common)
5009 align = bfd_log2 (isym->st_value);
5010 else
5011 {
5012 /* The new symbol is a common symbol in a shared object.
5013 We need to get the alignment from the section. */
5014 align = new_sec->alignment_power;
5015 }
5016 if (align > old_alignment)
5017 h->root.u.c.p->alignment_power = align;
5018 else
5019 h->root.u.c.p->alignment_power = old_alignment;
5020 }
5021
5022 if (is_elf_hash_table (&htab->root))
5023 {
5024 /* Set a flag in the hash table entry indicating the type of
5025 reference or definition we just found. A dynamic symbol
5026 is one which is referenced or defined by both a regular
5027 object and a shared object. */
5028 bool dynsym = false;
5029
5030 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5031 if ((abfd->flags & BFD_PLUGIN) != 0)
5032 {
5033 /* Except for this flag to track nonweak references. */
5034 if (!definition
5035 && bind != STB_WEAK)
5036 h->ref_ir_nonweak = 1;
5037 }
5038 else if (!dynamic)
5039 {
5040 if (! definition)
5041 {
5042 h->ref_regular = 1;
5043 if (bind != STB_WEAK)
5044 h->ref_regular_nonweak = 1;
5045 }
5046 else
5047 {
5048 h->def_regular = 1;
5049 if (h->def_dynamic)
5050 {
5051 h->def_dynamic = 0;
5052 h->ref_dynamic = 1;
5053 }
5054 }
5055 }
5056 else
5057 {
5058 if (! definition)
5059 {
5060 h->ref_dynamic = 1;
5061 hi->ref_dynamic = 1;
5062 }
5063 else
5064 {
5065 h->def_dynamic = 1;
5066 hi->def_dynamic = 1;
5067 }
5068 }
5069
5070 /* If an indirect symbol has been forced local, don't
5071 make the real symbol dynamic. */
5072 if (h != hi && hi->forced_local)
5073 ;
5074 else if (!dynamic)
5075 {
5076 if (bfd_link_dll (info)
5077 || h->def_dynamic
5078 || h->ref_dynamic)
5079 dynsym = true;
5080 }
5081 else
5082 {
5083 if (h->def_regular
5084 || h->ref_regular
5085 || (h->is_weakalias
5086 && weakdef (h)->dynindx != -1))
5087 dynsym = true;
5088 }
5089
5090 /* Check to see if we need to add an indirect symbol for
5091 the default name. */
5092 if ((definition
5093 || (!override && h->root.type == bfd_link_hash_common))
5094 && !(hi != h
5095 && hi->versioned == versioned_hidden))
5096 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5097 sec, value, &old_bfd, &dynsym))
5098 goto error_free_vers;
5099
5100 /* Check the alignment when a common symbol is involved. This
5101 can change when a common symbol is overridden by a normal
5102 definition or a common symbol is ignored due to the old
5103 normal definition. We need to make sure the maximum
5104 alignment is maintained. */
5105 if ((old_alignment || common)
5106 && h->root.type != bfd_link_hash_common)
5107 {
5108 unsigned int common_align;
5109 unsigned int normal_align;
5110 unsigned int symbol_align;
5111 bfd *normal_bfd;
5112 bfd *common_bfd;
5113
5114 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5115 || h->root.type == bfd_link_hash_defweak);
5116
5117 symbol_align = ffs (h->root.u.def.value) - 1;
5118 if (h->root.u.def.section->owner != NULL
5119 && (h->root.u.def.section->owner->flags
5120 & (DYNAMIC | BFD_PLUGIN)) == 0)
5121 {
5122 normal_align = h->root.u.def.section->alignment_power;
5123 if (normal_align > symbol_align)
5124 normal_align = symbol_align;
5125 }
5126 else
5127 normal_align = symbol_align;
5128
5129 if (old_alignment)
5130 {
5131 common_align = old_alignment;
5132 common_bfd = old_bfd;
5133 normal_bfd = abfd;
5134 }
5135 else
5136 {
5137 common_align = bfd_log2 (isym->st_value);
5138 common_bfd = abfd;
5139 normal_bfd = old_bfd;
5140 }
5141
5142 if (normal_align < common_align)
5143 {
5144 /* PR binutils/2735 */
5145 if (normal_bfd == NULL)
5146 _bfd_error_handler
5147 /* xgettext:c-format */
5148 (_("warning: alignment %u of common symbol `%s' in %pB is"
5149 " greater than the alignment (%u) of its section %pA"),
5150 1 << common_align, name, common_bfd,
5151 1 << normal_align, h->root.u.def.section);
5152 else
5153 _bfd_error_handler
5154 /* xgettext:c-format */
5155 (_("warning: alignment %u of symbol `%s' in %pB"
5156 " is smaller than %u in %pB"),
5157 1 << normal_align, name, normal_bfd,
5158 1 << common_align, common_bfd);
5159 }
5160 }
5161
5162 /* Remember the symbol size if it isn't undefined. */
5163 if (isym->st_size != 0
5164 && isym->st_shndx != SHN_UNDEF
5165 && (definition || h->size == 0))
5166 {
5167 if (h->size != 0
5168 && h->size != isym->st_size
5169 && ! size_change_ok)
5170 _bfd_error_handler
5171 /* xgettext:c-format */
5172 (_("warning: size of symbol `%s' changed"
5173 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5174 name, (uint64_t) h->size, old_bfd,
5175 (uint64_t) isym->st_size, abfd);
5176
5177 h->size = isym->st_size;
5178 }
5179
5180 /* If this is a common symbol, then we always want H->SIZE
5181 to be the size of the common symbol. The code just above
5182 won't fix the size if a common symbol becomes larger. We
5183 don't warn about a size change here, because that is
5184 covered by --warn-common. Allow changes between different
5185 function types. */
5186 if (h->root.type == bfd_link_hash_common)
5187 h->size = h->root.u.c.size;
5188
5189 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5190 && ((definition && !new_weak)
5191 || (old_weak && h->root.type == bfd_link_hash_common)
5192 || h->type == STT_NOTYPE))
5193 {
5194 unsigned int type = ELF_ST_TYPE (isym->st_info);
5195
5196 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5197 symbol. */
5198 if (type == STT_GNU_IFUNC
5199 && (abfd->flags & DYNAMIC) != 0)
5200 type = STT_FUNC;
5201
5202 if (h->type != type)
5203 {
5204 if (h->type != STT_NOTYPE && ! type_change_ok)
5205 /* xgettext:c-format */
5206 _bfd_error_handler
5207 (_("warning: type of symbol `%s' changed"
5208 " from %d to %d in %pB"),
5209 name, h->type, type, abfd);
5210
5211 h->type = type;
5212 }
5213 }
5214
5215 /* Merge st_other field. */
5216 elf_merge_st_other (abfd, h, isym->st_other, sec,
5217 definition, dynamic);
5218
5219 /* We don't want to make debug symbol dynamic. */
5220 if (definition
5221 && (sec->flags & SEC_DEBUGGING)
5222 && !bfd_link_relocatable (info))
5223 dynsym = false;
5224
5225 /* Nor should we make plugin symbols dynamic. */
5226 if ((abfd->flags & BFD_PLUGIN) != 0)
5227 dynsym = false;
5228
5229 if (definition)
5230 {
5231 h->target_internal = isym->st_target_internal;
5232 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5233 }
5234
5235 if (definition && !dynamic)
5236 {
5237 char *p = strchr (name, ELF_VER_CHR);
5238 if (p != NULL && p[1] != ELF_VER_CHR)
5239 {
5240 /* Queue non-default versions so that .symver x, x@FOO
5241 aliases can be checked. */
5242 if (!nondeflt_vers)
5243 {
5244 size_t amt = ((isymend - isym + 1)
5245 * sizeof (struct elf_link_hash_entry *));
5246 nondeflt_vers
5247 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5248 if (!nondeflt_vers)
5249 goto error_free_vers;
5250 }
5251 nondeflt_vers[nondeflt_vers_cnt++] = h;
5252 }
5253 }
5254
5255 if (dynsym && h->dynindx == -1)
5256 {
5257 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5258 goto error_free_vers;
5259 if (h->is_weakalias
5260 && weakdef (h)->dynindx == -1)
5261 {
5262 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5263 goto error_free_vers;
5264 }
5265 }
5266 else if (h->dynindx != -1)
5267 /* If the symbol already has a dynamic index, but
5268 visibility says it should not be visible, turn it into
5269 a local symbol. */
5270 switch (ELF_ST_VISIBILITY (h->other))
5271 {
5272 case STV_INTERNAL:
5273 case STV_HIDDEN:
5274 (*bed->elf_backend_hide_symbol) (info, h, true);
5275 dynsym = false;
5276 break;
5277 }
5278
5279 if (!add_needed
5280 && matched
5281 && definition
5282 && h->root.type != bfd_link_hash_indirect
5283 && ((dynsym
5284 && h->ref_regular_nonweak)
5285 || (old_bfd != NULL
5286 && (old_bfd->flags & BFD_PLUGIN) != 0
5287 && h->ref_ir_nonweak
5288 && !info->lto_all_symbols_read)
5289 || (h->ref_dynamic_nonweak
5290 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5291 && !on_needed_list (elf_dt_name (abfd),
5292 htab->needed, NULL))))
5293 {
5294 const char *soname = elf_dt_name (abfd);
5295
5296 info->callbacks->minfo ("%!", soname, old_bfd,
5297 h->root.root.string);
5298
5299 /* A symbol from a library loaded via DT_NEEDED of some
5300 other library is referenced by a regular object.
5301 Add a DT_NEEDED entry for it. Issue an error if
5302 --no-add-needed is used and the reference was not
5303 a weak one. */
5304 if (old_bfd != NULL
5305 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5306 {
5307 _bfd_error_handler
5308 /* xgettext:c-format */
5309 (_("%pB: undefined reference to symbol '%s'"),
5310 old_bfd, name);
5311 bfd_set_error (bfd_error_missing_dso);
5312 goto error_free_vers;
5313 }
5314
5315 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5316 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5317
5318 /* Create dynamic sections for backends that require
5319 that be done before setup_gnu_properties. */
5320 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5321 return false;
5322 add_needed = true;
5323 }
5324 }
5325 }
5326
5327 if (info->lto_plugin_active
5328 && !bfd_link_relocatable (info)
5329 && (abfd->flags & BFD_PLUGIN) == 0
5330 && !just_syms
5331 && extsymcount)
5332 {
5333 int r_sym_shift;
5334
5335 if (bed->s->arch_size == 32)
5336 r_sym_shift = 8;
5337 else
5338 r_sym_shift = 32;
5339
5340 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5341 referenced in regular objects so that linker plugin will get
5342 the correct symbol resolution. */
5343
5344 sym_hash = elf_sym_hashes (abfd);
5345 for (s = abfd->sections; s != NULL; s = s->next)
5346 {
5347 Elf_Internal_Rela *internal_relocs;
5348 Elf_Internal_Rela *rel, *relend;
5349
5350 /* Don't check relocations in excluded sections. */
5351 if ((s->flags & SEC_RELOC) == 0
5352 || s->reloc_count == 0
5353 || (s->flags & SEC_EXCLUDE) != 0
5354 || ((info->strip == strip_all
5355 || info->strip == strip_debugger)
5356 && (s->flags & SEC_DEBUGGING) != 0))
5357 continue;
5358
5359 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5360 NULL,
5361 info->keep_memory);
5362 if (internal_relocs == NULL)
5363 goto error_free_vers;
5364
5365 rel = internal_relocs;
5366 relend = rel + s->reloc_count;
5367 for ( ; rel < relend; rel++)
5368 {
5369 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5370 struct elf_link_hash_entry *h;
5371
5372 /* Skip local symbols. */
5373 if (r_symndx < extsymoff)
5374 continue;
5375
5376 h = sym_hash[r_symndx - extsymoff];
5377 if (h != NULL)
5378 h->root.non_ir_ref_regular = 1;
5379 }
5380
5381 if (elf_section_data (s)->relocs != internal_relocs)
5382 free (internal_relocs);
5383 }
5384 }
5385
5386 free (extversym);
5387 extversym = NULL;
5388 free (isymbuf);
5389 isymbuf = NULL;
5390
5391 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5392 {
5393 unsigned int i;
5394
5395 /* Restore the symbol table. */
5396 old_ent = (char *) old_tab + tabsize;
5397 memset (elf_sym_hashes (abfd), 0,
5398 extsymcount * sizeof (struct elf_link_hash_entry *));
5399 htab->root.table.table = old_table;
5400 htab->root.table.size = old_size;
5401 htab->root.table.count = old_count;
5402 memcpy (htab->root.table.table, old_tab, tabsize);
5403 htab->root.undefs = old_undefs;
5404 htab->root.undefs_tail = old_undefs_tail;
5405 if (htab->dynstr != NULL)
5406 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5407 free (old_strtab);
5408 old_strtab = NULL;
5409 for (i = 0; i < htab->root.table.size; i++)
5410 {
5411 struct bfd_hash_entry *p;
5412 struct elf_link_hash_entry *h;
5413 unsigned int non_ir_ref_dynamic;
5414
5415 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5416 {
5417 /* Preserve non_ir_ref_dynamic so that this symbol
5418 will be exported when the dynamic lib becomes needed
5419 in the second pass. */
5420 h = (struct elf_link_hash_entry *) p;
5421 if (h->root.type == bfd_link_hash_warning)
5422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5423 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5424
5425 h = (struct elf_link_hash_entry *) p;
5426 memcpy (h, old_ent, htab->root.table.entsize);
5427 old_ent = (char *) old_ent + htab->root.table.entsize;
5428 if (h->root.type == bfd_link_hash_warning)
5429 {
5430 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5431 memcpy (h, old_ent, htab->root.table.entsize);
5432 old_ent = (char *) old_ent + htab->root.table.entsize;
5433 }
5434 if (h->root.type == bfd_link_hash_common)
5435 {
5436 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5437 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5438 }
5439 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5440 }
5441 }
5442
5443 /* Make a special call to the linker "notice" function to
5444 tell it that symbols added for crefs may need to be removed. */
5445 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5446 goto error_free_vers;
5447
5448 free (old_tab);
5449 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5450 alloc_mark);
5451 free (nondeflt_vers);
5452 return true;
5453 }
5454
5455 if (old_tab != NULL)
5456 {
5457 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5458 goto error_free_vers;
5459 free (old_tab);
5460 old_tab = NULL;
5461 }
5462
5463 /* Now that all the symbols from this input file are created, if
5464 not performing a relocatable link, handle .symver foo, foo@BAR
5465 such that any relocs against foo become foo@BAR. */
5466 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5467 {
5468 size_t cnt, symidx;
5469
5470 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5471 {
5472 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5473 char *shortname, *p;
5474 size_t amt;
5475
5476 p = strchr (h->root.root.string, ELF_VER_CHR);
5477 if (p == NULL
5478 || (h->root.type != bfd_link_hash_defined
5479 && h->root.type != bfd_link_hash_defweak))
5480 continue;
5481
5482 amt = p - h->root.root.string;
5483 shortname = (char *) bfd_malloc (amt + 1);
5484 if (!shortname)
5485 goto error_free_vers;
5486 memcpy (shortname, h->root.root.string, amt);
5487 shortname[amt] = '\0';
5488
5489 hi = (struct elf_link_hash_entry *)
5490 bfd_link_hash_lookup (&htab->root, shortname,
5491 false, false, false);
5492 if (hi != NULL
5493 && hi->root.type == h->root.type
5494 && hi->root.u.def.value == h->root.u.def.value
5495 && hi->root.u.def.section == h->root.u.def.section)
5496 {
5497 (*bed->elf_backend_hide_symbol) (info, hi, true);
5498 hi->root.type = bfd_link_hash_indirect;
5499 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5500 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5501 sym_hash = elf_sym_hashes (abfd);
5502 if (sym_hash)
5503 for (symidx = 0; symidx < extsymcount; ++symidx)
5504 if (sym_hash[symidx] == hi)
5505 {
5506 sym_hash[symidx] = h;
5507 break;
5508 }
5509 }
5510 free (shortname);
5511 }
5512 free (nondeflt_vers);
5513 nondeflt_vers = NULL;
5514 }
5515
5516 /* Now set the alias field correctly for all the weak defined
5517 symbols we found. The only way to do this is to search all the
5518 symbols. Since we only need the information for non functions in
5519 dynamic objects, that's the only time we actually put anything on
5520 the list WEAKS. We need this information so that if a regular
5521 object refers to a symbol defined weakly in a dynamic object, the
5522 real symbol in the dynamic object is also put in the dynamic
5523 symbols; we also must arrange for both symbols to point to the
5524 same memory location. We could handle the general case of symbol
5525 aliasing, but a general symbol alias can only be generated in
5526 assembler code, handling it correctly would be very time
5527 consuming, and other ELF linkers don't handle general aliasing
5528 either. */
5529 if (weaks != NULL)
5530 {
5531 struct elf_link_hash_entry **hpp;
5532 struct elf_link_hash_entry **hppend;
5533 struct elf_link_hash_entry **sorted_sym_hash;
5534 struct elf_link_hash_entry *h;
5535 size_t sym_count, amt;
5536
5537 /* Since we have to search the whole symbol list for each weak
5538 defined symbol, search time for N weak defined symbols will be
5539 O(N^2). Binary search will cut it down to O(NlogN). */
5540 amt = extsymcount * sizeof (*sorted_sym_hash);
5541 sorted_sym_hash = bfd_malloc (amt);
5542 if (sorted_sym_hash == NULL)
5543 goto error_return;
5544 sym_hash = sorted_sym_hash;
5545 hpp = elf_sym_hashes (abfd);
5546 hppend = hpp + extsymcount;
5547 sym_count = 0;
5548 for (; hpp < hppend; hpp++)
5549 {
5550 h = *hpp;
5551 if (h != NULL
5552 && h->root.type == bfd_link_hash_defined
5553 && !bed->is_function_type (h->type))
5554 {
5555 *sym_hash = h;
5556 sym_hash++;
5557 sym_count++;
5558 }
5559 }
5560
5561 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5562 elf_sort_symbol);
5563
5564 while (weaks != NULL)
5565 {
5566 struct elf_link_hash_entry *hlook;
5567 asection *slook;
5568 bfd_vma vlook;
5569 size_t i, j, idx = 0;
5570
5571 hlook = weaks;
5572 weaks = hlook->u.alias;
5573 hlook->u.alias = NULL;
5574
5575 if (hlook->root.type != bfd_link_hash_defined
5576 && hlook->root.type != bfd_link_hash_defweak)
5577 continue;
5578
5579 slook = hlook->root.u.def.section;
5580 vlook = hlook->root.u.def.value;
5581
5582 i = 0;
5583 j = sym_count;
5584 while (i != j)
5585 {
5586 bfd_signed_vma vdiff;
5587 idx = (i + j) / 2;
5588 h = sorted_sym_hash[idx];
5589 vdiff = vlook - h->root.u.def.value;
5590 if (vdiff < 0)
5591 j = idx;
5592 else if (vdiff > 0)
5593 i = idx + 1;
5594 else
5595 {
5596 int sdiff = slook->id - h->root.u.def.section->id;
5597 if (sdiff < 0)
5598 j = idx;
5599 else if (sdiff > 0)
5600 i = idx + 1;
5601 else
5602 break;
5603 }
5604 }
5605
5606 /* We didn't find a value/section match. */
5607 if (i == j)
5608 continue;
5609
5610 /* With multiple aliases, or when the weak symbol is already
5611 strongly defined, we have multiple matching symbols and
5612 the binary search above may land on any of them. Step
5613 one past the matching symbol(s). */
5614 while (++idx != j)
5615 {
5616 h = sorted_sym_hash[idx];
5617 if (h->root.u.def.section != slook
5618 || h->root.u.def.value != vlook)
5619 break;
5620 }
5621
5622 /* Now look back over the aliases. Since we sorted by size
5623 as well as value and section, we'll choose the one with
5624 the largest size. */
5625 while (idx-- != i)
5626 {
5627 h = sorted_sym_hash[idx];
5628
5629 /* Stop if value or section doesn't match. */
5630 if (h->root.u.def.section != slook
5631 || h->root.u.def.value != vlook)
5632 break;
5633 else if (h != hlook)
5634 {
5635 struct elf_link_hash_entry *t;
5636
5637 hlook->u.alias = h;
5638 hlook->is_weakalias = 1;
5639 t = h;
5640 if (t->u.alias != NULL)
5641 while (t->u.alias != h)
5642 t = t->u.alias;
5643 t->u.alias = hlook;
5644
5645 /* If the weak definition is in the list of dynamic
5646 symbols, make sure the real definition is put
5647 there as well. */
5648 if (hlook->dynindx != -1 && h->dynindx == -1)
5649 {
5650 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5651 {
5652 err_free_sym_hash:
5653 free (sorted_sym_hash);
5654 goto error_return;
5655 }
5656 }
5657
5658 /* If the real definition is in the list of dynamic
5659 symbols, make sure the weak definition is put
5660 there as well. If we don't do this, then the
5661 dynamic loader might not merge the entries for the
5662 real definition and the weak definition. */
5663 if (h->dynindx != -1 && hlook->dynindx == -1)
5664 {
5665 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5666 goto err_free_sym_hash;
5667 }
5668 break;
5669 }
5670 }
5671 }
5672
5673 free (sorted_sym_hash);
5674 }
5675
5676 if (bed->check_directives
5677 && !(*bed->check_directives) (abfd, info))
5678 return false;
5679
5680 /* If this is a non-traditional link, try to optimize the handling
5681 of the .stab/.stabstr sections. */
5682 if (! dynamic
5683 && ! info->traditional_format
5684 && is_elf_hash_table (&htab->root)
5685 && (info->strip != strip_all && info->strip != strip_debugger))
5686 {
5687 asection *stabstr;
5688
5689 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5690 if (stabstr != NULL)
5691 {
5692 bfd_size_type string_offset = 0;
5693 asection *stab;
5694
5695 for (stab = abfd->sections; stab; stab = stab->next)
5696 if (startswith (stab->name, ".stab")
5697 && (!stab->name[5] ||
5698 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5699 && (stab->flags & SEC_MERGE) == 0
5700 && !bfd_is_abs_section (stab->output_section))
5701 {
5702 struct bfd_elf_section_data *secdata;
5703
5704 secdata = elf_section_data (stab);
5705 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5706 stabstr, &secdata->sec_info,
5707 &string_offset))
5708 goto error_return;
5709 if (secdata->sec_info)
5710 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5711 }
5712 }
5713 }
5714
5715 if (dynamic && add_needed)
5716 {
5717 /* Add this bfd to the loaded list. */
5718 struct elf_link_loaded_list *n;
5719
5720 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5721 if (n == NULL)
5722 goto error_return;
5723 n->abfd = abfd;
5724 n->next = htab->dyn_loaded;
5725 htab->dyn_loaded = n;
5726 }
5727 if (dynamic && !add_needed
5728 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5729 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5730
5731 return true;
5732
5733 error_free_vers:
5734 free (old_tab);
5735 free (old_strtab);
5736 free (nondeflt_vers);
5737 free (extversym);
5738 error_free_sym:
5739 free (isymbuf);
5740 error_return:
5741 return false;
5742 }
5743
5744 /* Return the linker hash table entry of a symbol that might be
5745 satisfied by an archive symbol. Return -1 on error. */
5746
5747 struct bfd_link_hash_entry *
5748 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5749 struct bfd_link_info *info,
5750 const char *name)
5751 {
5752 struct bfd_link_hash_entry *h;
5753 char *p, *copy;
5754 size_t len, first;
5755
5756 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5757 if (h != NULL)
5758 return h;
5759
5760 /* If this is a default version (the name contains @@), look up the
5761 symbol again with only one `@' as well as without the version.
5762 The effect is that references to the symbol with and without the
5763 version will be matched by the default symbol in the archive. */
5764
5765 p = strchr (name, ELF_VER_CHR);
5766 if (p == NULL || p[1] != ELF_VER_CHR)
5767 return h;
5768
5769 /* First check with only one `@'. */
5770 len = strlen (name);
5771 copy = (char *) bfd_alloc (abfd, len);
5772 if (copy == NULL)
5773 return (struct bfd_link_hash_entry *) -1;
5774
5775 first = p - name + 1;
5776 memcpy (copy, name, first);
5777 memcpy (copy + first, name + first + 1, len - first);
5778
5779 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5780 if (h == NULL)
5781 {
5782 /* We also need to check references to the symbol without the
5783 version. */
5784 copy[first - 1] = '\0';
5785 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5786 }
5787
5788 bfd_release (abfd, copy);
5789 return h;
5790 }
5791
5792 /* Add symbols from an ELF archive file to the linker hash table. We
5793 don't use _bfd_generic_link_add_archive_symbols because we need to
5794 handle versioned symbols.
5795
5796 Fortunately, ELF archive handling is simpler than that done by
5797 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5798 oddities. In ELF, if we find a symbol in the archive map, and the
5799 symbol is currently undefined, we know that we must pull in that
5800 object file.
5801
5802 Unfortunately, we do have to make multiple passes over the symbol
5803 table until nothing further is resolved. */
5804
5805 static bool
5806 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5807 {
5808 symindex c;
5809 unsigned char *included = NULL;
5810 carsym *symdefs;
5811 bool loop;
5812 size_t amt;
5813 const struct elf_backend_data *bed;
5814 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5815 (bfd *, struct bfd_link_info *, const char *);
5816
5817 if (! bfd_has_map (abfd))
5818 {
5819 /* An empty archive is a special case. */
5820 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5821 return true;
5822 bfd_set_error (bfd_error_no_armap);
5823 return false;
5824 }
5825
5826 /* Keep track of all symbols we know to be already defined, and all
5827 files we know to be already included. This is to speed up the
5828 second and subsequent passes. */
5829 c = bfd_ardata (abfd)->symdef_count;
5830 if (c == 0)
5831 return true;
5832 amt = c * sizeof (*included);
5833 included = (unsigned char *) bfd_zmalloc (amt);
5834 if (included == NULL)
5835 return false;
5836
5837 symdefs = bfd_ardata (abfd)->symdefs;
5838 bed = get_elf_backend_data (abfd);
5839 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5840
5841 do
5842 {
5843 file_ptr last;
5844 symindex i;
5845 carsym *symdef;
5846 carsym *symdefend;
5847
5848 loop = false;
5849 last = -1;
5850
5851 symdef = symdefs;
5852 symdefend = symdef + c;
5853 for (i = 0; symdef < symdefend; symdef++, i++)
5854 {
5855 struct bfd_link_hash_entry *h;
5856 bfd *element;
5857 struct bfd_link_hash_entry *undefs_tail;
5858 symindex mark;
5859
5860 if (included[i])
5861 continue;
5862 if (symdef->file_offset == last)
5863 {
5864 included[i] = true;
5865 continue;
5866 }
5867
5868 h = archive_symbol_lookup (abfd, info, symdef->name);
5869 if (h == (struct bfd_link_hash_entry *) -1)
5870 goto error_return;
5871
5872 if (h == NULL)
5873 continue;
5874
5875 if (h->type == bfd_link_hash_undefined)
5876 {
5877 /* If the archive element has already been loaded then one
5878 of the symbols defined by that element might have been
5879 made undefined due to being in a discarded section. */
5880 if (is_elf_hash_table (info->hash)
5881 && ((struct elf_link_hash_entry *) h)->indx == -3)
5882 continue;
5883 }
5884 else if (h->type == bfd_link_hash_common)
5885 {
5886 /* We currently have a common symbol. The archive map contains
5887 a reference to this symbol, so we may want to include it. We
5888 only want to include it however, if this archive element
5889 contains a definition of the symbol, not just another common
5890 declaration of it.
5891
5892 Unfortunately some archivers (including GNU ar) will put
5893 declarations of common symbols into their archive maps, as
5894 well as real definitions, so we cannot just go by the archive
5895 map alone. Instead we must read in the element's symbol
5896 table and check that to see what kind of symbol definition
5897 this is. */
5898 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5899 continue;
5900 }
5901 else
5902 {
5903 if (h->type != bfd_link_hash_undefweak)
5904 /* Symbol must be defined. Don't check it again. */
5905 included[i] = true;
5906 continue;
5907 }
5908
5909 /* We need to include this archive member. */
5910 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5911 if (element == NULL)
5912 goto error_return;
5913
5914 if (! bfd_check_format (element, bfd_object))
5915 goto error_return;
5916
5917 undefs_tail = info->hash->undefs_tail;
5918
5919 if (!(*info->callbacks
5920 ->add_archive_element) (info, element, symdef->name, &element))
5921 continue;
5922 if (!bfd_link_add_symbols (element, info))
5923 goto error_return;
5924
5925 /* If there are any new undefined symbols, we need to make
5926 another pass through the archive in order to see whether
5927 they can be defined. FIXME: This isn't perfect, because
5928 common symbols wind up on undefs_tail and because an
5929 undefined symbol which is defined later on in this pass
5930 does not require another pass. This isn't a bug, but it
5931 does make the code less efficient than it could be. */
5932 if (undefs_tail != info->hash->undefs_tail)
5933 loop = true;
5934
5935 /* Look backward to mark all symbols from this object file
5936 which we have already seen in this pass. */
5937 mark = i;
5938 do
5939 {
5940 included[mark] = true;
5941 if (mark == 0)
5942 break;
5943 --mark;
5944 }
5945 while (symdefs[mark].file_offset == symdef->file_offset);
5946
5947 /* We mark subsequent symbols from this object file as we go
5948 on through the loop. */
5949 last = symdef->file_offset;
5950 }
5951 }
5952 while (loop);
5953
5954 free (included);
5955 return true;
5956
5957 error_return:
5958 free (included);
5959 return false;
5960 }
5961
5962 /* Given an ELF BFD, add symbols to the global hash table as
5963 appropriate. */
5964
5965 bool
5966 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5967 {
5968 switch (bfd_get_format (abfd))
5969 {
5970 case bfd_object:
5971 return elf_link_add_object_symbols (abfd, info);
5972 case bfd_archive:
5973 return elf_link_add_archive_symbols (abfd, info);
5974 default:
5975 bfd_set_error (bfd_error_wrong_format);
5976 return false;
5977 }
5978 }
5979 \f
5980 struct hash_codes_info
5981 {
5982 unsigned long *hashcodes;
5983 bool error;
5984 };
5985
5986 /* This function will be called though elf_link_hash_traverse to store
5987 all hash value of the exported symbols in an array. */
5988
5989 static bool
5990 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5991 {
5992 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5993 const char *name;
5994 unsigned long ha;
5995 char *alc = NULL;
5996
5997 /* Ignore indirect symbols. These are added by the versioning code. */
5998 if (h->dynindx == -1)
5999 return true;
6000
6001 name = h->root.root.string;
6002 if (h->versioned >= versioned)
6003 {
6004 char *p = strchr (name, ELF_VER_CHR);
6005 if (p != NULL)
6006 {
6007 alc = (char *) bfd_malloc (p - name + 1);
6008 if (alc == NULL)
6009 {
6010 inf->error = true;
6011 return false;
6012 }
6013 memcpy (alc, name, p - name);
6014 alc[p - name] = '\0';
6015 name = alc;
6016 }
6017 }
6018
6019 /* Compute the hash value. */
6020 ha = bfd_elf_hash (name);
6021
6022 /* Store the found hash value in the array given as the argument. */
6023 *(inf->hashcodes)++ = ha;
6024
6025 /* And store it in the struct so that we can put it in the hash table
6026 later. */
6027 h->u.elf_hash_value = ha;
6028
6029 free (alc);
6030 return true;
6031 }
6032
6033 struct collect_gnu_hash_codes
6034 {
6035 bfd *output_bfd;
6036 const struct elf_backend_data *bed;
6037 unsigned long int nsyms;
6038 unsigned long int maskbits;
6039 unsigned long int *hashcodes;
6040 unsigned long int *hashval;
6041 unsigned long int *indx;
6042 unsigned long int *counts;
6043 bfd_vma *bitmask;
6044 bfd_byte *contents;
6045 bfd_size_type xlat;
6046 long int min_dynindx;
6047 unsigned long int bucketcount;
6048 unsigned long int symindx;
6049 long int local_indx;
6050 long int shift1, shift2;
6051 unsigned long int mask;
6052 bool error;
6053 };
6054
6055 /* This function will be called though elf_link_hash_traverse to store
6056 all hash value of the exported symbols in an array. */
6057
6058 static bool
6059 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6060 {
6061 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6062 const char *name;
6063 unsigned long ha;
6064 char *alc = NULL;
6065
6066 /* Ignore indirect symbols. These are added by the versioning code. */
6067 if (h->dynindx == -1)
6068 return true;
6069
6070 /* Ignore also local symbols and undefined symbols. */
6071 if (! (*s->bed->elf_hash_symbol) (h))
6072 return true;
6073
6074 name = h->root.root.string;
6075 if (h->versioned >= versioned)
6076 {
6077 char *p = strchr (name, ELF_VER_CHR);
6078 if (p != NULL)
6079 {
6080 alc = (char *) bfd_malloc (p - name + 1);
6081 if (alc == NULL)
6082 {
6083 s->error = true;
6084 return false;
6085 }
6086 memcpy (alc, name, p - name);
6087 alc[p - name] = '\0';
6088 name = alc;
6089 }
6090 }
6091
6092 /* Compute the hash value. */
6093 ha = bfd_elf_gnu_hash (name);
6094
6095 /* Store the found hash value in the array for compute_bucket_count,
6096 and also for .dynsym reordering purposes. */
6097 s->hashcodes[s->nsyms] = ha;
6098 s->hashval[h->dynindx] = ha;
6099 ++s->nsyms;
6100 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6101 s->min_dynindx = h->dynindx;
6102
6103 free (alc);
6104 return true;
6105 }
6106
6107 /* This function will be called though elf_link_hash_traverse to do
6108 final dynamic symbol renumbering in case of .gnu.hash.
6109 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6110 to the translation table. */
6111
6112 static bool
6113 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6114 {
6115 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6116 unsigned long int bucket;
6117 unsigned long int val;
6118
6119 /* Ignore indirect symbols. */
6120 if (h->dynindx == -1)
6121 return true;
6122
6123 /* Ignore also local symbols and undefined symbols. */
6124 if (! (*s->bed->elf_hash_symbol) (h))
6125 {
6126 if (h->dynindx >= s->min_dynindx)
6127 {
6128 if (s->bed->record_xhash_symbol != NULL)
6129 {
6130 (*s->bed->record_xhash_symbol) (h, 0);
6131 s->local_indx++;
6132 }
6133 else
6134 h->dynindx = s->local_indx++;
6135 }
6136 return true;
6137 }
6138
6139 bucket = s->hashval[h->dynindx] % s->bucketcount;
6140 val = (s->hashval[h->dynindx] >> s->shift1)
6141 & ((s->maskbits >> s->shift1) - 1);
6142 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6143 s->bitmask[val]
6144 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6145 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6146 if (s->counts[bucket] == 1)
6147 /* Last element terminates the chain. */
6148 val |= 1;
6149 bfd_put_32 (s->output_bfd, val,
6150 s->contents + (s->indx[bucket] - s->symindx) * 4);
6151 --s->counts[bucket];
6152 if (s->bed->record_xhash_symbol != NULL)
6153 {
6154 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6155
6156 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6157 }
6158 else
6159 h->dynindx = s->indx[bucket]++;
6160 return true;
6161 }
6162
6163 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6164
6165 bool
6166 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6167 {
6168 return !(h->forced_local
6169 || h->root.type == bfd_link_hash_undefined
6170 || h->root.type == bfd_link_hash_undefweak
6171 || ((h->root.type == bfd_link_hash_defined
6172 || h->root.type == bfd_link_hash_defweak)
6173 && h->root.u.def.section->output_section == NULL));
6174 }
6175
6176 /* Array used to determine the number of hash table buckets to use
6177 based on the number of symbols there are. If there are fewer than
6178 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6179 fewer than 37 we use 17 buckets, and so forth. We never use more
6180 than 32771 buckets. */
6181
6182 static const size_t elf_buckets[] =
6183 {
6184 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6185 16411, 32771, 0
6186 };
6187
6188 /* Compute bucket count for hashing table. We do not use a static set
6189 of possible tables sizes anymore. Instead we determine for all
6190 possible reasonable sizes of the table the outcome (i.e., the
6191 number of collisions etc) and choose the best solution. The
6192 weighting functions are not too simple to allow the table to grow
6193 without bounds. Instead one of the weighting factors is the size.
6194 Therefore the result is always a good payoff between few collisions
6195 (= short chain lengths) and table size. */
6196 static size_t
6197 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6198 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6199 unsigned long int nsyms,
6200 int gnu_hash)
6201 {
6202 size_t best_size = 0;
6203 unsigned long int i;
6204
6205 /* We have a problem here. The following code to optimize the table
6206 size requires an integer type with more the 32 bits. If
6207 BFD_HOST_U_64_BIT is set we know about such a type. */
6208 #ifdef BFD_HOST_U_64_BIT
6209 if (info->optimize)
6210 {
6211 size_t minsize;
6212 size_t maxsize;
6213 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6214 bfd *dynobj = elf_hash_table (info)->dynobj;
6215 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6216 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6217 unsigned long int *counts;
6218 bfd_size_type amt;
6219 unsigned int no_improvement_count = 0;
6220
6221 /* Possible optimization parameters: if we have NSYMS symbols we say
6222 that the hashing table must at least have NSYMS/4 and at most
6223 2*NSYMS buckets. */
6224 minsize = nsyms / 4;
6225 if (minsize == 0)
6226 minsize = 1;
6227 best_size = maxsize = nsyms * 2;
6228 if (gnu_hash)
6229 {
6230 if (minsize < 2)
6231 minsize = 2;
6232 if ((best_size & 31) == 0)
6233 ++best_size;
6234 }
6235
6236 /* Create array where we count the collisions in. We must use bfd_malloc
6237 since the size could be large. */
6238 amt = maxsize;
6239 amt *= sizeof (unsigned long int);
6240 counts = (unsigned long int *) bfd_malloc (amt);
6241 if (counts == NULL)
6242 return 0;
6243
6244 /* Compute the "optimal" size for the hash table. The criteria is a
6245 minimal chain length. The minor criteria is (of course) the size
6246 of the table. */
6247 for (i = minsize; i < maxsize; ++i)
6248 {
6249 /* Walk through the array of hashcodes and count the collisions. */
6250 BFD_HOST_U_64_BIT max;
6251 unsigned long int j;
6252 unsigned long int fact;
6253
6254 if (gnu_hash && (i & 31) == 0)
6255 continue;
6256
6257 memset (counts, '\0', i * sizeof (unsigned long int));
6258
6259 /* Determine how often each hash bucket is used. */
6260 for (j = 0; j < nsyms; ++j)
6261 ++counts[hashcodes[j] % i];
6262
6263 /* For the weight function we need some information about the
6264 pagesize on the target. This is information need not be 100%
6265 accurate. Since this information is not available (so far) we
6266 define it here to a reasonable default value. If it is crucial
6267 to have a better value some day simply define this value. */
6268 # ifndef BFD_TARGET_PAGESIZE
6269 # define BFD_TARGET_PAGESIZE (4096)
6270 # endif
6271
6272 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6273 and the chains. */
6274 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6275
6276 # if 1
6277 /* Variant 1: optimize for short chains. We add the squares
6278 of all the chain lengths (which favors many small chain
6279 over a few long chains). */
6280 for (j = 0; j < i; ++j)
6281 max += counts[j] * counts[j];
6282
6283 /* This adds penalties for the overall size of the table. */
6284 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6285 max *= fact * fact;
6286 # else
6287 /* Variant 2: Optimize a lot more for small table. Here we
6288 also add squares of the size but we also add penalties for
6289 empty slots (the +1 term). */
6290 for (j = 0; j < i; ++j)
6291 max += (1 + counts[j]) * (1 + counts[j]);
6292
6293 /* The overall size of the table is considered, but not as
6294 strong as in variant 1, where it is squared. */
6295 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6296 max *= fact;
6297 # endif
6298
6299 /* Compare with current best results. */
6300 if (max < best_chlen)
6301 {
6302 best_chlen = max;
6303 best_size = i;
6304 no_improvement_count = 0;
6305 }
6306 /* PR 11843: Avoid futile long searches for the best bucket size
6307 when there are a large number of symbols. */
6308 else if (++no_improvement_count == 100)
6309 break;
6310 }
6311
6312 free (counts);
6313 }
6314 else
6315 #endif /* defined (BFD_HOST_U_64_BIT) */
6316 {
6317 /* This is the fallback solution if no 64bit type is available or if we
6318 are not supposed to spend much time on optimizations. We select the
6319 bucket count using a fixed set of numbers. */
6320 for (i = 0; elf_buckets[i] != 0; i++)
6321 {
6322 best_size = elf_buckets[i];
6323 if (nsyms < elf_buckets[i + 1])
6324 break;
6325 }
6326 if (gnu_hash && best_size < 2)
6327 best_size = 2;
6328 }
6329
6330 return best_size;
6331 }
6332
6333 /* Size any SHT_GROUP section for ld -r. */
6334
6335 bool
6336 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6337 {
6338 bfd *ibfd;
6339 asection *s;
6340
6341 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6342 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6343 && (s = ibfd->sections) != NULL
6344 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6345 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6346 return false;
6347 return true;
6348 }
6349
6350 /* Set a default stack segment size. The value in INFO wins. If it
6351 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6352 undefined it is initialized. */
6353
6354 bool
6355 bfd_elf_stack_segment_size (bfd *output_bfd,
6356 struct bfd_link_info *info,
6357 const char *legacy_symbol,
6358 bfd_vma default_size)
6359 {
6360 struct elf_link_hash_entry *h = NULL;
6361
6362 /* Look for legacy symbol. */
6363 if (legacy_symbol)
6364 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6365 false, false, false);
6366 if (h && (h->root.type == bfd_link_hash_defined
6367 || h->root.type == bfd_link_hash_defweak)
6368 && h->def_regular
6369 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6370 {
6371 /* The symbol has no type if specified on the command line. */
6372 h->type = STT_OBJECT;
6373 if (info->stacksize)
6374 /* xgettext:c-format */
6375 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6376 output_bfd, legacy_symbol);
6377 else if (h->root.u.def.section != bfd_abs_section_ptr)
6378 /* xgettext:c-format */
6379 _bfd_error_handler (_("%pB: %s not absolute"),
6380 output_bfd, legacy_symbol);
6381 else
6382 info->stacksize = h->root.u.def.value;
6383 }
6384
6385 if (!info->stacksize)
6386 /* If the user didn't set a size, or explicitly inhibit the
6387 size, set it now. */
6388 info->stacksize = default_size;
6389
6390 /* Provide the legacy symbol, if it is referenced. */
6391 if (h && (h->root.type == bfd_link_hash_undefined
6392 || h->root.type == bfd_link_hash_undefweak))
6393 {
6394 struct bfd_link_hash_entry *bh = NULL;
6395
6396 if (!(_bfd_generic_link_add_one_symbol
6397 (info, output_bfd, legacy_symbol,
6398 BSF_GLOBAL, bfd_abs_section_ptr,
6399 info->stacksize >= 0 ? info->stacksize : 0,
6400 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6401 return false;
6402
6403 h = (struct elf_link_hash_entry *) bh;
6404 h->def_regular = 1;
6405 h->type = STT_OBJECT;
6406 }
6407
6408 return true;
6409 }
6410
6411 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6412
6413 struct elf_gc_sweep_symbol_info
6414 {
6415 struct bfd_link_info *info;
6416 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6417 bool);
6418 };
6419
6420 static bool
6421 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6422 {
6423 if (!h->mark
6424 && (((h->root.type == bfd_link_hash_defined
6425 || h->root.type == bfd_link_hash_defweak)
6426 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6427 && h->root.u.def.section->gc_mark))
6428 || h->root.type == bfd_link_hash_undefined
6429 || h->root.type == bfd_link_hash_undefweak))
6430 {
6431 struct elf_gc_sweep_symbol_info *inf;
6432
6433 inf = (struct elf_gc_sweep_symbol_info *) data;
6434 (*inf->hide_symbol) (inf->info, h, true);
6435 h->def_regular = 0;
6436 h->ref_regular = 0;
6437 h->ref_regular_nonweak = 0;
6438 }
6439
6440 return true;
6441 }
6442
6443 /* Set up the sizes and contents of the ELF dynamic sections. This is
6444 called by the ELF linker emulation before_allocation routine. We
6445 must set the sizes of the sections before the linker sets the
6446 addresses of the various sections. */
6447
6448 bool
6449 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6450 const char *soname,
6451 const char *rpath,
6452 const char *filter_shlib,
6453 const char *audit,
6454 const char *depaudit,
6455 const char * const *auxiliary_filters,
6456 struct bfd_link_info *info,
6457 asection **sinterpptr)
6458 {
6459 bfd *dynobj;
6460 const struct elf_backend_data *bed;
6461
6462 *sinterpptr = NULL;
6463
6464 if (!is_elf_hash_table (info->hash))
6465 return true;
6466
6467 dynobj = elf_hash_table (info)->dynobj;
6468
6469 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6470 {
6471 struct bfd_elf_version_tree *verdefs;
6472 struct elf_info_failed asvinfo;
6473 struct bfd_elf_version_tree *t;
6474 struct bfd_elf_version_expr *d;
6475 asection *s;
6476 size_t soname_indx;
6477
6478 /* If we are supposed to export all symbols into the dynamic symbol
6479 table (this is not the normal case), then do so. */
6480 if (info->export_dynamic
6481 || (bfd_link_executable (info) && info->dynamic))
6482 {
6483 struct elf_info_failed eif;
6484
6485 eif.info = info;
6486 eif.failed = false;
6487 elf_link_hash_traverse (elf_hash_table (info),
6488 _bfd_elf_export_symbol,
6489 &eif);
6490 if (eif.failed)
6491 return false;
6492 }
6493
6494 if (soname != NULL)
6495 {
6496 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6497 soname, true);
6498 if (soname_indx == (size_t) -1
6499 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6500 return false;
6501 }
6502 else
6503 soname_indx = (size_t) -1;
6504
6505 /* Make all global versions with definition. */
6506 for (t = info->version_info; t != NULL; t = t->next)
6507 for (d = t->globals.list; d != NULL; d = d->next)
6508 if (!d->symver && d->literal)
6509 {
6510 const char *verstr, *name;
6511 size_t namelen, verlen, newlen;
6512 char *newname, *p, leading_char;
6513 struct elf_link_hash_entry *newh;
6514
6515 leading_char = bfd_get_symbol_leading_char (output_bfd);
6516 name = d->pattern;
6517 namelen = strlen (name) + (leading_char != '\0');
6518 verstr = t->name;
6519 verlen = strlen (verstr);
6520 newlen = namelen + verlen + 3;
6521
6522 newname = (char *) bfd_malloc (newlen);
6523 if (newname == NULL)
6524 return false;
6525 newname[0] = leading_char;
6526 memcpy (newname + (leading_char != '\0'), name, namelen);
6527
6528 /* Check the hidden versioned definition. */
6529 p = newname + namelen;
6530 *p++ = ELF_VER_CHR;
6531 memcpy (p, verstr, verlen + 1);
6532 newh = elf_link_hash_lookup (elf_hash_table (info),
6533 newname, false, false,
6534 false);
6535 if (newh == NULL
6536 || (newh->root.type != bfd_link_hash_defined
6537 && newh->root.type != bfd_link_hash_defweak))
6538 {
6539 /* Check the default versioned definition. */
6540 *p++ = ELF_VER_CHR;
6541 memcpy (p, verstr, verlen + 1);
6542 newh = elf_link_hash_lookup (elf_hash_table (info),
6543 newname, false, false,
6544 false);
6545 }
6546 free (newname);
6547
6548 /* Mark this version if there is a definition and it is
6549 not defined in a shared object. */
6550 if (newh != NULL
6551 && !newh->def_dynamic
6552 && (newh->root.type == bfd_link_hash_defined
6553 || newh->root.type == bfd_link_hash_defweak))
6554 d->symver = 1;
6555 }
6556
6557 /* Attach all the symbols to their version information. */
6558 asvinfo.info = info;
6559 asvinfo.failed = false;
6560
6561 elf_link_hash_traverse (elf_hash_table (info),
6562 _bfd_elf_link_assign_sym_version,
6563 &asvinfo);
6564 if (asvinfo.failed)
6565 return false;
6566
6567 if (!info->allow_undefined_version)
6568 {
6569 /* Check if all global versions have a definition. */
6570 bool all_defined = true;
6571 for (t = info->version_info; t != NULL; t = t->next)
6572 for (d = t->globals.list; d != NULL; d = d->next)
6573 if (d->literal && !d->symver && !d->script)
6574 {
6575 _bfd_error_handler
6576 (_("%s: undefined version: %s"),
6577 d->pattern, t->name);
6578 all_defined = false;
6579 }
6580
6581 if (!all_defined)
6582 {
6583 bfd_set_error (bfd_error_bad_value);
6584 return false;
6585 }
6586 }
6587
6588 /* Set up the version definition section. */
6589 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6590 BFD_ASSERT (s != NULL);
6591
6592 /* We may have created additional version definitions if we are
6593 just linking a regular application. */
6594 verdefs = info->version_info;
6595
6596 /* Skip anonymous version tag. */
6597 if (verdefs != NULL && verdefs->vernum == 0)
6598 verdefs = verdefs->next;
6599
6600 if (verdefs == NULL && !info->create_default_symver)
6601 s->flags |= SEC_EXCLUDE;
6602 else
6603 {
6604 unsigned int cdefs;
6605 bfd_size_type size;
6606 bfd_byte *p;
6607 Elf_Internal_Verdef def;
6608 Elf_Internal_Verdaux defaux;
6609 struct bfd_link_hash_entry *bh;
6610 struct elf_link_hash_entry *h;
6611 const char *name;
6612
6613 cdefs = 0;
6614 size = 0;
6615
6616 /* Make space for the base version. */
6617 size += sizeof (Elf_External_Verdef);
6618 size += sizeof (Elf_External_Verdaux);
6619 ++cdefs;
6620
6621 /* Make space for the default version. */
6622 if (info->create_default_symver)
6623 {
6624 size += sizeof (Elf_External_Verdef);
6625 ++cdefs;
6626 }
6627
6628 for (t = verdefs; t != NULL; t = t->next)
6629 {
6630 struct bfd_elf_version_deps *n;
6631
6632 /* Don't emit base version twice. */
6633 if (t->vernum == 0)
6634 continue;
6635
6636 size += sizeof (Elf_External_Verdef);
6637 size += sizeof (Elf_External_Verdaux);
6638 ++cdefs;
6639
6640 for (n = t->deps; n != NULL; n = n->next)
6641 size += sizeof (Elf_External_Verdaux);
6642 }
6643
6644 s->size = size;
6645 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6646 if (s->contents == NULL && s->size != 0)
6647 return false;
6648
6649 /* Fill in the version definition section. */
6650
6651 p = s->contents;
6652
6653 def.vd_version = VER_DEF_CURRENT;
6654 def.vd_flags = VER_FLG_BASE;
6655 def.vd_ndx = 1;
6656 def.vd_cnt = 1;
6657 if (info->create_default_symver)
6658 {
6659 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6660 def.vd_next = sizeof (Elf_External_Verdef);
6661 }
6662 else
6663 {
6664 def.vd_aux = sizeof (Elf_External_Verdef);
6665 def.vd_next = (sizeof (Elf_External_Verdef)
6666 + sizeof (Elf_External_Verdaux));
6667 }
6668
6669 if (soname_indx != (size_t) -1)
6670 {
6671 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6672 soname_indx);
6673 def.vd_hash = bfd_elf_hash (soname);
6674 defaux.vda_name = soname_indx;
6675 name = soname;
6676 }
6677 else
6678 {
6679 size_t indx;
6680
6681 name = lbasename (bfd_get_filename (output_bfd));
6682 def.vd_hash = bfd_elf_hash (name);
6683 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6684 name, false);
6685 if (indx == (size_t) -1)
6686 return false;
6687 defaux.vda_name = indx;
6688 }
6689 defaux.vda_next = 0;
6690
6691 _bfd_elf_swap_verdef_out (output_bfd, &def,
6692 (Elf_External_Verdef *) p);
6693 p += sizeof (Elf_External_Verdef);
6694 if (info->create_default_symver)
6695 {
6696 /* Add a symbol representing this version. */
6697 bh = NULL;
6698 if (! (_bfd_generic_link_add_one_symbol
6699 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6700 0, NULL, false,
6701 get_elf_backend_data (dynobj)->collect, &bh)))
6702 return false;
6703 h = (struct elf_link_hash_entry *) bh;
6704 h->non_elf = 0;
6705 h->def_regular = 1;
6706 h->type = STT_OBJECT;
6707 h->verinfo.vertree = NULL;
6708
6709 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6710 return false;
6711
6712 /* Create a duplicate of the base version with the same
6713 aux block, but different flags. */
6714 def.vd_flags = 0;
6715 def.vd_ndx = 2;
6716 def.vd_aux = sizeof (Elf_External_Verdef);
6717 if (verdefs)
6718 def.vd_next = (sizeof (Elf_External_Verdef)
6719 + sizeof (Elf_External_Verdaux));
6720 else
6721 def.vd_next = 0;
6722 _bfd_elf_swap_verdef_out (output_bfd, &def,
6723 (Elf_External_Verdef *) p);
6724 p += sizeof (Elf_External_Verdef);
6725 }
6726 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6727 (Elf_External_Verdaux *) p);
6728 p += sizeof (Elf_External_Verdaux);
6729
6730 for (t = verdefs; t != NULL; t = t->next)
6731 {
6732 unsigned int cdeps;
6733 struct bfd_elf_version_deps *n;
6734
6735 /* Don't emit the base version twice. */
6736 if (t->vernum == 0)
6737 continue;
6738
6739 cdeps = 0;
6740 for (n = t->deps; n != NULL; n = n->next)
6741 ++cdeps;
6742
6743 /* Add a symbol representing this version. */
6744 bh = NULL;
6745 if (! (_bfd_generic_link_add_one_symbol
6746 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6747 0, NULL, false,
6748 get_elf_backend_data (dynobj)->collect, &bh)))
6749 return false;
6750 h = (struct elf_link_hash_entry *) bh;
6751 h->non_elf = 0;
6752 h->def_regular = 1;
6753 h->type = STT_OBJECT;
6754 h->verinfo.vertree = t;
6755
6756 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6757 return false;
6758
6759 def.vd_version = VER_DEF_CURRENT;
6760 def.vd_flags = 0;
6761 if (t->globals.list == NULL
6762 && t->locals.list == NULL
6763 && ! t->used)
6764 def.vd_flags |= VER_FLG_WEAK;
6765 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6766 def.vd_cnt = cdeps + 1;
6767 def.vd_hash = bfd_elf_hash (t->name);
6768 def.vd_aux = sizeof (Elf_External_Verdef);
6769 def.vd_next = 0;
6770
6771 /* If a basever node is next, it *must* be the last node in
6772 the chain, otherwise Verdef construction breaks. */
6773 if (t->next != NULL && t->next->vernum == 0)
6774 BFD_ASSERT (t->next->next == NULL);
6775
6776 if (t->next != NULL && t->next->vernum != 0)
6777 def.vd_next = (sizeof (Elf_External_Verdef)
6778 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6779
6780 _bfd_elf_swap_verdef_out (output_bfd, &def,
6781 (Elf_External_Verdef *) p);
6782 p += sizeof (Elf_External_Verdef);
6783
6784 defaux.vda_name = h->dynstr_index;
6785 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6786 h->dynstr_index);
6787 defaux.vda_next = 0;
6788 if (t->deps != NULL)
6789 defaux.vda_next = sizeof (Elf_External_Verdaux);
6790 t->name_indx = defaux.vda_name;
6791
6792 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6793 (Elf_External_Verdaux *) p);
6794 p += sizeof (Elf_External_Verdaux);
6795
6796 for (n = t->deps; n != NULL; n = n->next)
6797 {
6798 if (n->version_needed == NULL)
6799 {
6800 /* This can happen if there was an error in the
6801 version script. */
6802 defaux.vda_name = 0;
6803 }
6804 else
6805 {
6806 defaux.vda_name = n->version_needed->name_indx;
6807 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6808 defaux.vda_name);
6809 }
6810 if (n->next == NULL)
6811 defaux.vda_next = 0;
6812 else
6813 defaux.vda_next = sizeof (Elf_External_Verdaux);
6814
6815 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6816 (Elf_External_Verdaux *) p);
6817 p += sizeof (Elf_External_Verdaux);
6818 }
6819 }
6820
6821 elf_tdata (output_bfd)->cverdefs = cdefs;
6822 }
6823 }
6824
6825 bed = get_elf_backend_data (output_bfd);
6826
6827 if (info->gc_sections && bed->can_gc_sections)
6828 {
6829 struct elf_gc_sweep_symbol_info sweep_info;
6830
6831 /* Remove the symbols that were in the swept sections from the
6832 dynamic symbol table. */
6833 sweep_info.info = info;
6834 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6835 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6836 &sweep_info);
6837 }
6838
6839 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6840 {
6841 asection *s;
6842 struct elf_find_verdep_info sinfo;
6843
6844 /* Work out the size of the version reference section. */
6845
6846 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6847 BFD_ASSERT (s != NULL);
6848
6849 sinfo.info = info;
6850 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6851 if (sinfo.vers == 0)
6852 sinfo.vers = 1;
6853 sinfo.failed = false;
6854
6855 elf_link_hash_traverse (elf_hash_table (info),
6856 _bfd_elf_link_find_version_dependencies,
6857 &sinfo);
6858 if (sinfo.failed)
6859 return false;
6860
6861 if (elf_tdata (output_bfd)->verref == NULL)
6862 s->flags |= SEC_EXCLUDE;
6863 else
6864 {
6865 Elf_Internal_Verneed *vn;
6866 unsigned int size;
6867 unsigned int crefs;
6868 bfd_byte *p;
6869
6870 /* Build the version dependency section. */
6871 size = 0;
6872 crefs = 0;
6873 for (vn = elf_tdata (output_bfd)->verref;
6874 vn != NULL;
6875 vn = vn->vn_nextref)
6876 {
6877 Elf_Internal_Vernaux *a;
6878
6879 size += sizeof (Elf_External_Verneed);
6880 ++crefs;
6881 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6882 size += sizeof (Elf_External_Vernaux);
6883 }
6884
6885 s->size = size;
6886 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6887 if (s->contents == NULL)
6888 return false;
6889
6890 p = s->contents;
6891 for (vn = elf_tdata (output_bfd)->verref;
6892 vn != NULL;
6893 vn = vn->vn_nextref)
6894 {
6895 unsigned int caux;
6896 Elf_Internal_Vernaux *a;
6897 size_t indx;
6898
6899 caux = 0;
6900 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6901 ++caux;
6902
6903 vn->vn_version = VER_NEED_CURRENT;
6904 vn->vn_cnt = caux;
6905 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6906 elf_dt_name (vn->vn_bfd) != NULL
6907 ? elf_dt_name (vn->vn_bfd)
6908 : lbasename (bfd_get_filename
6909 (vn->vn_bfd)),
6910 false);
6911 if (indx == (size_t) -1)
6912 return false;
6913 vn->vn_file = indx;
6914 vn->vn_aux = sizeof (Elf_External_Verneed);
6915 if (vn->vn_nextref == NULL)
6916 vn->vn_next = 0;
6917 else
6918 vn->vn_next = (sizeof (Elf_External_Verneed)
6919 + caux * sizeof (Elf_External_Vernaux));
6920
6921 _bfd_elf_swap_verneed_out (output_bfd, vn,
6922 (Elf_External_Verneed *) p);
6923 p += sizeof (Elf_External_Verneed);
6924
6925 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6926 {
6927 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6928 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6929 a->vna_nodename, false);
6930 if (indx == (size_t) -1)
6931 return false;
6932 a->vna_name = indx;
6933 if (a->vna_nextptr == NULL)
6934 a->vna_next = 0;
6935 else
6936 a->vna_next = sizeof (Elf_External_Vernaux);
6937
6938 _bfd_elf_swap_vernaux_out (output_bfd, a,
6939 (Elf_External_Vernaux *) p);
6940 p += sizeof (Elf_External_Vernaux);
6941 }
6942 }
6943
6944 elf_tdata (output_bfd)->cverrefs = crefs;
6945 }
6946 }
6947
6948 /* Any syms created from now on start with -1 in
6949 got.refcount/offset and plt.refcount/offset. */
6950 elf_hash_table (info)->init_got_refcount
6951 = elf_hash_table (info)->init_got_offset;
6952 elf_hash_table (info)->init_plt_refcount
6953 = elf_hash_table (info)->init_plt_offset;
6954
6955 if (bfd_link_relocatable (info)
6956 && !_bfd_elf_size_group_sections (info))
6957 return false;
6958
6959 /* The backend may have to create some sections regardless of whether
6960 we're dynamic or not. */
6961 if (bed->elf_backend_always_size_sections
6962 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6963 return false;
6964
6965 /* Determine any GNU_STACK segment requirements, after the backend
6966 has had a chance to set a default segment size. */
6967 if (info->execstack)
6968 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6969 else if (info->noexecstack)
6970 elf_stack_flags (output_bfd) = PF_R | PF_W;
6971 else
6972 {
6973 bfd *inputobj;
6974 asection *notesec = NULL;
6975 int exec = 0;
6976
6977 for (inputobj = info->input_bfds;
6978 inputobj;
6979 inputobj = inputobj->link.next)
6980 {
6981 asection *s;
6982
6983 if (inputobj->flags
6984 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6985 continue;
6986 s = inputobj->sections;
6987 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6988 continue;
6989
6990 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6991 if (s)
6992 {
6993 if (s->flags & SEC_CODE)
6994 exec = PF_X;
6995 notesec = s;
6996 }
6997 else if (bed->default_execstack)
6998 exec = PF_X;
6999 }
7000 if (notesec || info->stacksize > 0)
7001 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7002 if (notesec && exec && bfd_link_relocatable (info)
7003 && notesec->output_section != bfd_abs_section_ptr)
7004 notesec->output_section->flags |= SEC_CODE;
7005 }
7006
7007 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7008 {
7009 struct elf_info_failed eif;
7010 struct elf_link_hash_entry *h;
7011 asection *dynstr;
7012 asection *s;
7013
7014 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7015 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7016
7017 if (info->symbolic)
7018 {
7019 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7020 return false;
7021 info->flags |= DF_SYMBOLIC;
7022 }
7023
7024 if (rpath != NULL)
7025 {
7026 size_t indx;
7027 bfd_vma tag;
7028
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7030 true);
7031 if (indx == (size_t) -1)
7032 return false;
7033
7034 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7035 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7036 return false;
7037 }
7038
7039 if (filter_shlib != NULL)
7040 {
7041 size_t indx;
7042
7043 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7044 filter_shlib, true);
7045 if (indx == (size_t) -1
7046 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7047 return false;
7048 }
7049
7050 if (auxiliary_filters != NULL)
7051 {
7052 const char * const *p;
7053
7054 for (p = auxiliary_filters; *p != NULL; p++)
7055 {
7056 size_t indx;
7057
7058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7059 *p, true);
7060 if (indx == (size_t) -1
7061 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7062 return false;
7063 }
7064 }
7065
7066 if (audit != NULL)
7067 {
7068 size_t indx;
7069
7070 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7071 true);
7072 if (indx == (size_t) -1
7073 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7074 return false;
7075 }
7076
7077 if (depaudit != NULL)
7078 {
7079 size_t indx;
7080
7081 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7082 true);
7083 if (indx == (size_t) -1
7084 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7085 return false;
7086 }
7087
7088 eif.info = info;
7089 eif.failed = false;
7090
7091 /* Find all symbols which were defined in a dynamic object and make
7092 the backend pick a reasonable value for them. */
7093 elf_link_hash_traverse (elf_hash_table (info),
7094 _bfd_elf_adjust_dynamic_symbol,
7095 &eif);
7096 if (eif.failed)
7097 return false;
7098
7099 /* Add some entries to the .dynamic section. We fill in some of the
7100 values later, in bfd_elf_final_link, but we must add the entries
7101 now so that we know the final size of the .dynamic section. */
7102
7103 /* If there are initialization and/or finalization functions to
7104 call then add the corresponding DT_INIT/DT_FINI entries. */
7105 h = (info->init_function
7106 ? elf_link_hash_lookup (elf_hash_table (info),
7107 info->init_function, false,
7108 false, false)
7109 : NULL);
7110 if (h != NULL
7111 && (h->ref_regular
7112 || h->def_regular))
7113 {
7114 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7115 return false;
7116 }
7117 h = (info->fini_function
7118 ? elf_link_hash_lookup (elf_hash_table (info),
7119 info->fini_function, false,
7120 false, false)
7121 : NULL);
7122 if (h != NULL
7123 && (h->ref_regular
7124 || h->def_regular))
7125 {
7126 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7127 return false;
7128 }
7129
7130 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7131 if (s != NULL && s->linker_has_input)
7132 {
7133 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7134 if (! bfd_link_executable (info))
7135 {
7136 bfd *sub;
7137 asection *o;
7138
7139 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7140 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7141 && (o = sub->sections) != NULL
7142 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7143 for (o = sub->sections; o != NULL; o = o->next)
7144 if (elf_section_data (o)->this_hdr.sh_type
7145 == SHT_PREINIT_ARRAY)
7146 {
7147 _bfd_error_handler
7148 (_("%pB: .preinit_array section is not allowed in DSO"),
7149 sub);
7150 break;
7151 }
7152
7153 bfd_set_error (bfd_error_nonrepresentable_section);
7154 return false;
7155 }
7156
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7159 return false;
7160 }
7161 s = bfd_get_section_by_name (output_bfd, ".init_array");
7162 if (s != NULL && s->linker_has_input)
7163 {
7164 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7165 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7166 return false;
7167 }
7168 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7169 if (s != NULL && s->linker_has_input)
7170 {
7171 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7172 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7173 return false;
7174 }
7175
7176 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7177 /* If .dynstr is excluded from the link, we don't want any of
7178 these tags. Strictly, we should be checking each section
7179 individually; This quick check covers for the case where
7180 someone does a /DISCARD/ : { *(*) }. */
7181 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7182 {
7183 bfd_size_type strsize;
7184
7185 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7186 if ((info->emit_hash
7187 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7188 || (info->emit_gnu_hash
7189 && (bed->record_xhash_symbol == NULL
7190 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7191 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7192 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7193 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7194 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7195 bed->s->sizeof_sym)
7196 || (info->gnu_flags_1
7197 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7198 info->gnu_flags_1)))
7199 return false;
7200 }
7201 }
7202
7203 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7204 return false;
7205
7206 /* The backend must work out the sizes of all the other dynamic
7207 sections. */
7208 if (dynobj != NULL
7209 && bed->elf_backend_size_dynamic_sections != NULL
7210 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7211 return false;
7212
7213 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7214 {
7215 if (elf_tdata (output_bfd)->cverdefs)
7216 {
7217 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7218
7219 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7220 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7221 return false;
7222 }
7223
7224 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7225 {
7226 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7227 return false;
7228 }
7229 else if (info->flags & DF_BIND_NOW)
7230 {
7231 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7232 return false;
7233 }
7234
7235 if (info->flags_1)
7236 {
7237 if (bfd_link_executable (info))
7238 info->flags_1 &= ~ (DF_1_INITFIRST
7239 | DF_1_NODELETE
7240 | DF_1_NOOPEN);
7241 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7242 return false;
7243 }
7244
7245 if (elf_tdata (output_bfd)->cverrefs)
7246 {
7247 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7248
7249 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7250 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7251 return false;
7252 }
7253
7254 if ((elf_tdata (output_bfd)->cverrefs == 0
7255 && elf_tdata (output_bfd)->cverdefs == 0)
7256 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7257 {
7258 asection *s;
7259
7260 s = bfd_get_linker_section (dynobj, ".gnu.version");
7261 s->flags |= SEC_EXCLUDE;
7262 }
7263 }
7264 return true;
7265 }
7266
7267 /* Find the first non-excluded output section. We'll use its
7268 section symbol for some emitted relocs. */
7269 void
7270 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7271 {
7272 asection *s;
7273 asection *found = NULL;
7274
7275 for (s = output_bfd->sections; s != NULL; s = s->next)
7276 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7277 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7278 {
7279 found = s;
7280 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7281 break;
7282 }
7283 elf_hash_table (info)->text_index_section = found;
7284 }
7285
7286 /* Find two non-excluded output sections, one for code, one for data.
7287 We'll use their section symbols for some emitted relocs. */
7288 void
7289 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7290 {
7291 asection *s;
7292 asection *found = NULL;
7293
7294 /* Data first, since setting text_index_section changes
7295 _bfd_elf_omit_section_dynsym_default. */
7296 for (s = output_bfd->sections; s != NULL; s = s->next)
7297 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7298 && !(s->flags & SEC_READONLY)
7299 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7300 {
7301 found = s;
7302 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7303 break;
7304 }
7305 elf_hash_table (info)->data_index_section = found;
7306
7307 for (s = output_bfd->sections; s != NULL; s = s->next)
7308 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7309 && (s->flags & SEC_READONLY)
7310 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7311 {
7312 found = s;
7313 break;
7314 }
7315 elf_hash_table (info)->text_index_section = found;
7316 }
7317
7318 #define GNU_HASH_SECTION_NAME(bed) \
7319 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7320
7321 bool
7322 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7323 {
7324 const struct elf_backend_data *bed;
7325 unsigned long section_sym_count;
7326 bfd_size_type dynsymcount = 0;
7327
7328 if (!is_elf_hash_table (info->hash))
7329 return true;
7330
7331 bed = get_elf_backend_data (output_bfd);
7332 (*bed->elf_backend_init_index_section) (output_bfd, info);
7333
7334 /* Assign dynsym indices. In a shared library we generate a section
7335 symbol for each output section, which come first. Next come all
7336 of the back-end allocated local dynamic syms, followed by the rest
7337 of the global symbols.
7338
7339 This is usually not needed for static binaries, however backends
7340 can request to always do it, e.g. the MIPS backend uses dynamic
7341 symbol counts to lay out GOT, which will be produced in the
7342 presence of GOT relocations even in static binaries (holding fixed
7343 data in that case, to satisfy those relocations). */
7344
7345 if (elf_hash_table (info)->dynamic_sections_created
7346 || bed->always_renumber_dynsyms)
7347 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7348 &section_sym_count);
7349
7350 if (elf_hash_table (info)->dynamic_sections_created)
7351 {
7352 bfd *dynobj;
7353 asection *s;
7354 unsigned int dtagcount;
7355
7356 dynobj = elf_hash_table (info)->dynobj;
7357
7358 /* Work out the size of the symbol version section. */
7359 s = bfd_get_linker_section (dynobj, ".gnu.version");
7360 BFD_ASSERT (s != NULL);
7361 if ((s->flags & SEC_EXCLUDE) == 0)
7362 {
7363 s->size = dynsymcount * sizeof (Elf_External_Versym);
7364 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7365 if (s->contents == NULL)
7366 return false;
7367
7368 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7369 return false;
7370 }
7371
7372 /* Set the size of the .dynsym and .hash sections. We counted
7373 the number of dynamic symbols in elf_link_add_object_symbols.
7374 We will build the contents of .dynsym and .hash when we build
7375 the final symbol table, because until then we do not know the
7376 correct value to give the symbols. We built the .dynstr
7377 section as we went along in elf_link_add_object_symbols. */
7378 s = elf_hash_table (info)->dynsym;
7379 BFD_ASSERT (s != NULL);
7380 s->size = dynsymcount * bed->s->sizeof_sym;
7381
7382 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7383 if (s->contents == NULL)
7384 return false;
7385
7386 /* The first entry in .dynsym is a dummy symbol. Clear all the
7387 section syms, in case we don't output them all. */
7388 ++section_sym_count;
7389 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7390
7391 elf_hash_table (info)->bucketcount = 0;
7392
7393 /* Compute the size of the hashing table. As a side effect this
7394 computes the hash values for all the names we export. */
7395 if (info->emit_hash)
7396 {
7397 unsigned long int *hashcodes;
7398 struct hash_codes_info hashinf;
7399 bfd_size_type amt;
7400 unsigned long int nsyms;
7401 size_t bucketcount;
7402 size_t hash_entry_size;
7403
7404 /* Compute the hash values for all exported symbols. At the same
7405 time store the values in an array so that we could use them for
7406 optimizations. */
7407 amt = dynsymcount * sizeof (unsigned long int);
7408 hashcodes = (unsigned long int *) bfd_malloc (amt);
7409 if (hashcodes == NULL)
7410 return false;
7411 hashinf.hashcodes = hashcodes;
7412 hashinf.error = false;
7413
7414 /* Put all hash values in HASHCODES. */
7415 elf_link_hash_traverse (elf_hash_table (info),
7416 elf_collect_hash_codes, &hashinf);
7417 if (hashinf.error)
7418 {
7419 free (hashcodes);
7420 return false;
7421 }
7422
7423 nsyms = hashinf.hashcodes - hashcodes;
7424 bucketcount
7425 = compute_bucket_count (info, hashcodes, nsyms, 0);
7426 free (hashcodes);
7427
7428 if (bucketcount == 0 && nsyms > 0)
7429 return false;
7430
7431 elf_hash_table (info)->bucketcount = bucketcount;
7432
7433 s = bfd_get_linker_section (dynobj, ".hash");
7434 BFD_ASSERT (s != NULL);
7435 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7436 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7437 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7438 if (s->contents == NULL)
7439 return false;
7440
7441 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7442 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7443 s->contents + hash_entry_size);
7444 }
7445
7446 if (info->emit_gnu_hash)
7447 {
7448 size_t i, cnt;
7449 unsigned char *contents;
7450 struct collect_gnu_hash_codes cinfo;
7451 bfd_size_type amt;
7452 size_t bucketcount;
7453
7454 memset (&cinfo, 0, sizeof (cinfo));
7455
7456 /* Compute the hash values for all exported symbols. At the same
7457 time store the values in an array so that we could use them for
7458 optimizations. */
7459 amt = dynsymcount * 2 * sizeof (unsigned long int);
7460 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7461 if (cinfo.hashcodes == NULL)
7462 return false;
7463
7464 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7465 cinfo.min_dynindx = -1;
7466 cinfo.output_bfd = output_bfd;
7467 cinfo.bed = bed;
7468
7469 /* Put all hash values in HASHCODES. */
7470 elf_link_hash_traverse (elf_hash_table (info),
7471 elf_collect_gnu_hash_codes, &cinfo);
7472 if (cinfo.error)
7473 {
7474 free (cinfo.hashcodes);
7475 return false;
7476 }
7477
7478 bucketcount
7479 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7480
7481 if (bucketcount == 0)
7482 {
7483 free (cinfo.hashcodes);
7484 return false;
7485 }
7486
7487 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7488 BFD_ASSERT (s != NULL);
7489
7490 if (cinfo.nsyms == 0)
7491 {
7492 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7493 BFD_ASSERT (cinfo.min_dynindx == -1);
7494 free (cinfo.hashcodes);
7495 s->size = 5 * 4 + bed->s->arch_size / 8;
7496 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7497 if (contents == NULL)
7498 return false;
7499 s->contents = contents;
7500 /* 1 empty bucket. */
7501 bfd_put_32 (output_bfd, 1, contents);
7502 /* SYMIDX above the special symbol 0. */
7503 bfd_put_32 (output_bfd, 1, contents + 4);
7504 /* Just one word for bitmask. */
7505 bfd_put_32 (output_bfd, 1, contents + 8);
7506 /* Only hash fn bloom filter. */
7507 bfd_put_32 (output_bfd, 0, contents + 12);
7508 /* No hashes are valid - empty bitmask. */
7509 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7510 /* No hashes in the only bucket. */
7511 bfd_put_32 (output_bfd, 0,
7512 contents + 16 + bed->s->arch_size / 8);
7513 }
7514 else
7515 {
7516 unsigned long int maskwords, maskbitslog2, x;
7517 BFD_ASSERT (cinfo.min_dynindx != -1);
7518
7519 x = cinfo.nsyms;
7520 maskbitslog2 = 1;
7521 while ((x >>= 1) != 0)
7522 ++maskbitslog2;
7523 if (maskbitslog2 < 3)
7524 maskbitslog2 = 5;
7525 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7526 maskbitslog2 = maskbitslog2 + 3;
7527 else
7528 maskbitslog2 = maskbitslog2 + 2;
7529 if (bed->s->arch_size == 64)
7530 {
7531 if (maskbitslog2 == 5)
7532 maskbitslog2 = 6;
7533 cinfo.shift1 = 6;
7534 }
7535 else
7536 cinfo.shift1 = 5;
7537 cinfo.mask = (1 << cinfo.shift1) - 1;
7538 cinfo.shift2 = maskbitslog2;
7539 cinfo.maskbits = 1 << maskbitslog2;
7540 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7541 amt = bucketcount * sizeof (unsigned long int) * 2;
7542 amt += maskwords * sizeof (bfd_vma);
7543 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7544 if (cinfo.bitmask == NULL)
7545 {
7546 free (cinfo.hashcodes);
7547 return false;
7548 }
7549
7550 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7551 cinfo.indx = cinfo.counts + bucketcount;
7552 cinfo.symindx = dynsymcount - cinfo.nsyms;
7553 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7554
7555 /* Determine how often each hash bucket is used. */
7556 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7557 for (i = 0; i < cinfo.nsyms; ++i)
7558 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7559
7560 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7561 if (cinfo.counts[i] != 0)
7562 {
7563 cinfo.indx[i] = cnt;
7564 cnt += cinfo.counts[i];
7565 }
7566 BFD_ASSERT (cnt == dynsymcount);
7567 cinfo.bucketcount = bucketcount;
7568 cinfo.local_indx = cinfo.min_dynindx;
7569
7570 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7571 s->size += cinfo.maskbits / 8;
7572 if (bed->record_xhash_symbol != NULL)
7573 s->size += cinfo.nsyms * 4;
7574 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7575 if (contents == NULL)
7576 {
7577 free (cinfo.bitmask);
7578 free (cinfo.hashcodes);
7579 return false;
7580 }
7581
7582 s->contents = contents;
7583 bfd_put_32 (output_bfd, bucketcount, contents);
7584 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7585 bfd_put_32 (output_bfd, maskwords, contents + 8);
7586 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7587 contents += 16 + cinfo.maskbits / 8;
7588
7589 for (i = 0; i < bucketcount; ++i)
7590 {
7591 if (cinfo.counts[i] == 0)
7592 bfd_put_32 (output_bfd, 0, contents);
7593 else
7594 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7595 contents += 4;
7596 }
7597
7598 cinfo.contents = contents;
7599
7600 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7601 /* Renumber dynamic symbols, if populating .gnu.hash section.
7602 If using .MIPS.xhash, populate the translation table. */
7603 elf_link_hash_traverse (elf_hash_table (info),
7604 elf_gnu_hash_process_symidx, &cinfo);
7605
7606 contents = s->contents + 16;
7607 for (i = 0; i < maskwords; ++i)
7608 {
7609 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7610 contents);
7611 contents += bed->s->arch_size / 8;
7612 }
7613
7614 free (cinfo.bitmask);
7615 free (cinfo.hashcodes);
7616 }
7617 }
7618
7619 s = bfd_get_linker_section (dynobj, ".dynstr");
7620 BFD_ASSERT (s != NULL);
7621
7622 elf_finalize_dynstr (output_bfd, info);
7623
7624 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7625
7626 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7627 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7628 return false;
7629 }
7630
7631 return true;
7632 }
7633 \f
7634 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7635
7636 static void
7637 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7638 asection *sec)
7639 {
7640 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7641 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7642 }
7643
7644 /* Finish SHF_MERGE section merging. */
7645
7646 bool
7647 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7648 {
7649 bfd *ibfd;
7650 asection *sec;
7651
7652 if (!is_elf_hash_table (info->hash))
7653 return false;
7654
7655 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7656 if ((ibfd->flags & DYNAMIC) == 0
7657 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7658 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7659 == get_elf_backend_data (obfd)->s->elfclass))
7660 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7661 if ((sec->flags & SEC_MERGE) != 0
7662 && !bfd_is_abs_section (sec->output_section))
7663 {
7664 struct bfd_elf_section_data *secdata;
7665
7666 secdata = elf_section_data (sec);
7667 if (! _bfd_add_merge_section (obfd,
7668 &elf_hash_table (info)->merge_info,
7669 sec, &secdata->sec_info))
7670 return false;
7671 else if (secdata->sec_info)
7672 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7673 }
7674
7675 if (elf_hash_table (info)->merge_info != NULL)
7676 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7677 merge_sections_remove_hook);
7678 return true;
7679 }
7680
7681 /* Create an entry in an ELF linker hash table. */
7682
7683 struct bfd_hash_entry *
7684 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7685 struct bfd_hash_table *table,
7686 const char *string)
7687 {
7688 /* Allocate the structure if it has not already been allocated by a
7689 subclass. */
7690 if (entry == NULL)
7691 {
7692 entry = (struct bfd_hash_entry *)
7693 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7694 if (entry == NULL)
7695 return entry;
7696 }
7697
7698 /* Call the allocation method of the superclass. */
7699 entry = _bfd_link_hash_newfunc (entry, table, string);
7700 if (entry != NULL)
7701 {
7702 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7703 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7704
7705 /* Set local fields. */
7706 ret->indx = -1;
7707 ret->dynindx = -1;
7708 ret->got = htab->init_got_refcount;
7709 ret->plt = htab->init_plt_refcount;
7710 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7711 - offsetof (struct elf_link_hash_entry, size)));
7712 /* Assume that we have been called by a non-ELF symbol reader.
7713 This flag is then reset by the code which reads an ELF input
7714 file. This ensures that a symbol created by a non-ELF symbol
7715 reader will have the flag set correctly. */
7716 ret->non_elf = 1;
7717 }
7718
7719 return entry;
7720 }
7721
7722 /* Copy data from an indirect symbol to its direct symbol, hiding the
7723 old indirect symbol. Also used for copying flags to a weakdef. */
7724
7725 void
7726 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7727 struct elf_link_hash_entry *dir,
7728 struct elf_link_hash_entry *ind)
7729 {
7730 struct elf_link_hash_table *htab;
7731
7732 if (ind->dyn_relocs != NULL)
7733 {
7734 if (dir->dyn_relocs != NULL)
7735 {
7736 struct elf_dyn_relocs **pp;
7737 struct elf_dyn_relocs *p;
7738
7739 /* Add reloc counts against the indirect sym to the direct sym
7740 list. Merge any entries against the same section. */
7741 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7742 {
7743 struct elf_dyn_relocs *q;
7744
7745 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7746 if (q->sec == p->sec)
7747 {
7748 q->pc_count += p->pc_count;
7749 q->count += p->count;
7750 *pp = p->next;
7751 break;
7752 }
7753 if (q == NULL)
7754 pp = &p->next;
7755 }
7756 *pp = dir->dyn_relocs;
7757 }
7758
7759 dir->dyn_relocs = ind->dyn_relocs;
7760 ind->dyn_relocs = NULL;
7761 }
7762
7763 /* Copy down any references that we may have already seen to the
7764 symbol which just became indirect. */
7765
7766 if (dir->versioned != versioned_hidden)
7767 dir->ref_dynamic |= ind->ref_dynamic;
7768 dir->ref_regular |= ind->ref_regular;
7769 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7770 dir->non_got_ref |= ind->non_got_ref;
7771 dir->needs_plt |= ind->needs_plt;
7772 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7773
7774 if (ind->root.type != bfd_link_hash_indirect)
7775 return;
7776
7777 /* Copy over the global and procedure linkage table refcount entries.
7778 These may have been already set up by a check_relocs routine. */
7779 htab = elf_hash_table (info);
7780 if (ind->got.refcount > htab->init_got_refcount.refcount)
7781 {
7782 if (dir->got.refcount < 0)
7783 dir->got.refcount = 0;
7784 dir->got.refcount += ind->got.refcount;
7785 ind->got.refcount = htab->init_got_refcount.refcount;
7786 }
7787
7788 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7789 {
7790 if (dir->plt.refcount < 0)
7791 dir->plt.refcount = 0;
7792 dir->plt.refcount += ind->plt.refcount;
7793 ind->plt.refcount = htab->init_plt_refcount.refcount;
7794 }
7795
7796 if (ind->dynindx != -1)
7797 {
7798 if (dir->dynindx != -1)
7799 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7800 dir->dynindx = ind->dynindx;
7801 dir->dynstr_index = ind->dynstr_index;
7802 ind->dynindx = -1;
7803 ind->dynstr_index = 0;
7804 }
7805 }
7806
7807 void
7808 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7809 struct elf_link_hash_entry *h,
7810 bool force_local)
7811 {
7812 /* STT_GNU_IFUNC symbol must go through PLT. */
7813 if (h->type != STT_GNU_IFUNC)
7814 {
7815 h->plt = elf_hash_table (info)->init_plt_offset;
7816 h->needs_plt = 0;
7817 }
7818 if (force_local)
7819 {
7820 h->forced_local = 1;
7821 if (h->dynindx != -1)
7822 {
7823 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7824 h->dynstr_index);
7825 h->dynindx = -1;
7826 h->dynstr_index = 0;
7827 }
7828 }
7829 }
7830
7831 /* Hide a symbol. */
7832
7833 void
7834 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7835 struct bfd_link_info *info,
7836 struct bfd_link_hash_entry *h)
7837 {
7838 if (is_elf_hash_table (info->hash))
7839 {
7840 const struct elf_backend_data *bed
7841 = get_elf_backend_data (output_bfd);
7842 struct elf_link_hash_entry *eh
7843 = (struct elf_link_hash_entry *) h;
7844 bed->elf_backend_hide_symbol (info, eh, true);
7845 eh->def_dynamic = 0;
7846 eh->ref_dynamic = 0;
7847 eh->dynamic_def = 0;
7848 }
7849 }
7850
7851 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7852 caller. */
7853
7854 bool
7855 _bfd_elf_link_hash_table_init
7856 (struct elf_link_hash_table *table,
7857 bfd *abfd,
7858 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7859 struct bfd_hash_table *,
7860 const char *),
7861 unsigned int entsize,
7862 enum elf_target_id target_id)
7863 {
7864 bool ret;
7865 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7866
7867 table->init_got_refcount.refcount = can_refcount - 1;
7868 table->init_plt_refcount.refcount = can_refcount - 1;
7869 table->init_got_offset.offset = -(bfd_vma) 1;
7870 table->init_plt_offset.offset = -(bfd_vma) 1;
7871 /* The first dynamic symbol is a dummy. */
7872 table->dynsymcount = 1;
7873
7874 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7875
7876 table->root.type = bfd_link_elf_hash_table;
7877 table->hash_table_id = target_id;
7878 table->target_os = get_elf_backend_data (abfd)->target_os;
7879
7880 return ret;
7881 }
7882
7883 /* Create an ELF linker hash table. */
7884
7885 struct bfd_link_hash_table *
7886 _bfd_elf_link_hash_table_create (bfd *abfd)
7887 {
7888 struct elf_link_hash_table *ret;
7889 size_t amt = sizeof (struct elf_link_hash_table);
7890
7891 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7892 if (ret == NULL)
7893 return NULL;
7894
7895 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7896 sizeof (struct elf_link_hash_entry),
7897 GENERIC_ELF_DATA))
7898 {
7899 free (ret);
7900 return NULL;
7901 }
7902 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7903
7904 return &ret->root;
7905 }
7906
7907 /* Destroy an ELF linker hash table. */
7908
7909 void
7910 _bfd_elf_link_hash_table_free (bfd *obfd)
7911 {
7912 struct elf_link_hash_table *htab;
7913
7914 htab = (struct elf_link_hash_table *) obfd->link.hash;
7915 if (htab->dynstr != NULL)
7916 _bfd_elf_strtab_free (htab->dynstr);
7917 _bfd_merge_sections_free (htab->merge_info);
7918 _bfd_generic_link_hash_table_free (obfd);
7919 }
7920
7921 /* This is a hook for the ELF emulation code in the generic linker to
7922 tell the backend linker what file name to use for the DT_NEEDED
7923 entry for a dynamic object. */
7924
7925 void
7926 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7927 {
7928 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd) == bfd_object)
7930 elf_dt_name (abfd) = name;
7931 }
7932
7933 int
7934 bfd_elf_get_dyn_lib_class (bfd *abfd)
7935 {
7936 int lib_class;
7937 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7938 && bfd_get_format (abfd) == bfd_object)
7939 lib_class = elf_dyn_lib_class (abfd);
7940 else
7941 lib_class = 0;
7942 return lib_class;
7943 }
7944
7945 void
7946 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7947 {
7948 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7949 && bfd_get_format (abfd) == bfd_object)
7950 elf_dyn_lib_class (abfd) = lib_class;
7951 }
7952
7953 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7954 the linker ELF emulation code. */
7955
7956 struct bfd_link_needed_list *
7957 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7958 struct bfd_link_info *info)
7959 {
7960 if (! is_elf_hash_table (info->hash))
7961 return NULL;
7962 return elf_hash_table (info)->needed;
7963 }
7964
7965 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7966 hook for the linker ELF emulation code. */
7967
7968 struct bfd_link_needed_list *
7969 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7970 struct bfd_link_info *info)
7971 {
7972 if (! is_elf_hash_table (info->hash))
7973 return NULL;
7974 return elf_hash_table (info)->runpath;
7975 }
7976
7977 /* Get the name actually used for a dynamic object for a link. This
7978 is the SONAME entry if there is one. Otherwise, it is the string
7979 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7980
7981 const char *
7982 bfd_elf_get_dt_soname (bfd *abfd)
7983 {
7984 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7985 && bfd_get_format (abfd) == bfd_object)
7986 return elf_dt_name (abfd);
7987 return NULL;
7988 }
7989
7990 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7991 the ELF linker emulation code. */
7992
7993 bool
7994 bfd_elf_get_bfd_needed_list (bfd *abfd,
7995 struct bfd_link_needed_list **pneeded)
7996 {
7997 asection *s;
7998 bfd_byte *dynbuf = NULL;
7999 unsigned int elfsec;
8000 unsigned long shlink;
8001 bfd_byte *extdyn, *extdynend;
8002 size_t extdynsize;
8003 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8004
8005 *pneeded = NULL;
8006
8007 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8008 || bfd_get_format (abfd) != bfd_object)
8009 return true;
8010
8011 s = bfd_get_section_by_name (abfd, ".dynamic");
8012 if (s == NULL || s->size == 0)
8013 return true;
8014
8015 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8016 goto error_return;
8017
8018 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8019 if (elfsec == SHN_BAD)
8020 goto error_return;
8021
8022 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8023
8024 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8025 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8026
8027 extdyn = dynbuf;
8028 extdynend = extdyn + s->size;
8029 for (; extdyn < extdynend; extdyn += extdynsize)
8030 {
8031 Elf_Internal_Dyn dyn;
8032
8033 (*swap_dyn_in) (abfd, extdyn, &dyn);
8034
8035 if (dyn.d_tag == DT_NULL)
8036 break;
8037
8038 if (dyn.d_tag == DT_NEEDED)
8039 {
8040 const char *string;
8041 struct bfd_link_needed_list *l;
8042 unsigned int tagv = dyn.d_un.d_val;
8043 size_t amt;
8044
8045 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8046 if (string == NULL)
8047 goto error_return;
8048
8049 amt = sizeof *l;
8050 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8051 if (l == NULL)
8052 goto error_return;
8053
8054 l->by = abfd;
8055 l->name = string;
8056 l->next = *pneeded;
8057 *pneeded = l;
8058 }
8059 }
8060
8061 free (dynbuf);
8062
8063 return true;
8064
8065 error_return:
8066 free (dynbuf);
8067 return false;
8068 }
8069
8070 struct elf_symbuf_symbol
8071 {
8072 unsigned long st_name; /* Symbol name, index in string tbl */
8073 unsigned char st_info; /* Type and binding attributes */
8074 unsigned char st_other; /* Visibilty, and target specific */
8075 };
8076
8077 struct elf_symbuf_head
8078 {
8079 struct elf_symbuf_symbol *ssym;
8080 size_t count;
8081 unsigned int st_shndx;
8082 };
8083
8084 struct elf_symbol
8085 {
8086 union
8087 {
8088 Elf_Internal_Sym *isym;
8089 struct elf_symbuf_symbol *ssym;
8090 void *p;
8091 } u;
8092 const char *name;
8093 };
8094
8095 /* Sort references to symbols by ascending section number. */
8096
8097 static int
8098 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8099 {
8100 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8101 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8102
8103 if (s1->st_shndx != s2->st_shndx)
8104 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8105 /* Final sort by the address of the sym in the symbuf ensures
8106 a stable sort. */
8107 if (s1 != s2)
8108 return s1 > s2 ? 1 : -1;
8109 return 0;
8110 }
8111
8112 static int
8113 elf_sym_name_compare (const void *arg1, const void *arg2)
8114 {
8115 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8116 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8117 int ret = strcmp (s1->name, s2->name);
8118 if (ret != 0)
8119 return ret;
8120 if (s1->u.p != s2->u.p)
8121 return s1->u.p > s2->u.p ? 1 : -1;
8122 return 0;
8123 }
8124
8125 static struct elf_symbuf_head *
8126 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8127 {
8128 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8129 struct elf_symbuf_symbol *ssym;
8130 struct elf_symbuf_head *ssymbuf, *ssymhead;
8131 size_t i, shndx_count, total_size, amt;
8132
8133 amt = symcount * sizeof (*indbuf);
8134 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8135 if (indbuf == NULL)
8136 return NULL;
8137
8138 for (ind = indbuf, i = 0; i < symcount; i++)
8139 if (isymbuf[i].st_shndx != SHN_UNDEF)
8140 *ind++ = &isymbuf[i];
8141 indbufend = ind;
8142
8143 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8144 elf_sort_elf_symbol);
8145
8146 shndx_count = 0;
8147 if (indbufend > indbuf)
8148 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8149 if (ind[0]->st_shndx != ind[1]->st_shndx)
8150 shndx_count++;
8151
8152 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8153 + (indbufend - indbuf) * sizeof (*ssym));
8154 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8155 if (ssymbuf == NULL)
8156 {
8157 free (indbuf);
8158 return NULL;
8159 }
8160
8161 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8162 ssymbuf->ssym = NULL;
8163 ssymbuf->count = shndx_count;
8164 ssymbuf->st_shndx = 0;
8165 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8166 {
8167 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8168 {
8169 ssymhead++;
8170 ssymhead->ssym = ssym;
8171 ssymhead->count = 0;
8172 ssymhead->st_shndx = (*ind)->st_shndx;
8173 }
8174 ssym->st_name = (*ind)->st_name;
8175 ssym->st_info = (*ind)->st_info;
8176 ssym->st_other = (*ind)->st_other;
8177 ssymhead->count++;
8178 }
8179 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8180 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8181 == total_size));
8182
8183 free (indbuf);
8184 return ssymbuf;
8185 }
8186
8187 /* Check if 2 sections define the same set of local and global
8188 symbols. */
8189
8190 static bool
8191 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8192 struct bfd_link_info *info)
8193 {
8194 bfd *bfd1, *bfd2;
8195 const struct elf_backend_data *bed1, *bed2;
8196 Elf_Internal_Shdr *hdr1, *hdr2;
8197 size_t symcount1, symcount2;
8198 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8199 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8200 Elf_Internal_Sym *isym, *isymend;
8201 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8202 size_t count1, count2, sec_count1, sec_count2, i;
8203 unsigned int shndx1, shndx2;
8204 bool result;
8205 bool ignore_section_symbol_p;
8206
8207 bfd1 = sec1->owner;
8208 bfd2 = sec2->owner;
8209
8210 /* Both sections have to be in ELF. */
8211 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8212 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8213 return false;
8214
8215 if (elf_section_type (sec1) != elf_section_type (sec2))
8216 return false;
8217
8218 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8219 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8220 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8221 return false;
8222
8223 bed1 = get_elf_backend_data (bfd1);
8224 bed2 = get_elf_backend_data (bfd2);
8225 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8226 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8227 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8228 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8229
8230 if (symcount1 == 0 || symcount2 == 0)
8231 return false;
8232
8233 result = false;
8234 isymbuf1 = NULL;
8235 isymbuf2 = NULL;
8236 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8237 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8238
8239 /* Ignore section symbols only when matching non-debugging sections
8240 or linkonce section with comdat section. */
8241 ignore_section_symbol_p
8242 = ((sec1->flags & SEC_DEBUGGING) == 0
8243 || ((elf_section_flags (sec1) & SHF_GROUP)
8244 != (elf_section_flags (sec2) & SHF_GROUP)));
8245
8246 if (ssymbuf1 == NULL)
8247 {
8248 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8249 NULL, NULL, NULL);
8250 if (isymbuf1 == NULL)
8251 goto done;
8252
8253 if (info != NULL && !info->reduce_memory_overheads)
8254 {
8255 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8256 elf_tdata (bfd1)->symbuf = ssymbuf1;
8257 }
8258 }
8259
8260 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8261 {
8262 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8263 NULL, NULL, NULL);
8264 if (isymbuf2 == NULL)
8265 goto done;
8266
8267 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8268 {
8269 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8270 elf_tdata (bfd2)->symbuf = ssymbuf2;
8271 }
8272 }
8273
8274 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8275 {
8276 /* Optimized faster version. */
8277 size_t lo, hi, mid;
8278 struct elf_symbol *symp;
8279 struct elf_symbuf_symbol *ssym, *ssymend;
8280
8281 lo = 0;
8282 hi = ssymbuf1->count;
8283 ssymbuf1++;
8284 count1 = 0;
8285 sec_count1 = 0;
8286 while (lo < hi)
8287 {
8288 mid = (lo + hi) / 2;
8289 if (shndx1 < ssymbuf1[mid].st_shndx)
8290 hi = mid;
8291 else if (shndx1 > ssymbuf1[mid].st_shndx)
8292 lo = mid + 1;
8293 else
8294 {
8295 count1 = ssymbuf1[mid].count;
8296 ssymbuf1 += mid;
8297 break;
8298 }
8299 }
8300 if (ignore_section_symbol_p)
8301 {
8302 for (i = 0; i < count1; i++)
8303 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8304 sec_count1++;
8305 count1 -= sec_count1;
8306 }
8307
8308 lo = 0;
8309 hi = ssymbuf2->count;
8310 ssymbuf2++;
8311 count2 = 0;
8312 sec_count2 = 0;
8313 while (lo < hi)
8314 {
8315 mid = (lo + hi) / 2;
8316 if (shndx2 < ssymbuf2[mid].st_shndx)
8317 hi = mid;
8318 else if (shndx2 > ssymbuf2[mid].st_shndx)
8319 lo = mid + 1;
8320 else
8321 {
8322 count2 = ssymbuf2[mid].count;
8323 ssymbuf2 += mid;
8324 break;
8325 }
8326 }
8327 if (ignore_section_symbol_p)
8328 {
8329 for (i = 0; i < count2; i++)
8330 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8331 sec_count2++;
8332 count2 -= sec_count2;
8333 }
8334
8335 if (count1 == 0 || count2 == 0 || count1 != count2)
8336 goto done;
8337
8338 symtable1
8339 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8340 symtable2
8341 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8342 if (symtable1 == NULL || symtable2 == NULL)
8343 goto done;
8344
8345 symp = symtable1;
8346 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8347 ssym < ssymend; ssym++)
8348 if (sec_count1 == 0
8349 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8350 {
8351 symp->u.ssym = ssym;
8352 symp->name = bfd_elf_string_from_elf_section (bfd1,
8353 hdr1->sh_link,
8354 ssym->st_name);
8355 symp++;
8356 }
8357
8358 symp = symtable2;
8359 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8360 ssym < ssymend; ssym++)
8361 if (sec_count2 == 0
8362 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8363 {
8364 symp->u.ssym = ssym;
8365 symp->name = bfd_elf_string_from_elf_section (bfd2,
8366 hdr2->sh_link,
8367 ssym->st_name);
8368 symp++;
8369 }
8370
8371 /* Sort symbol by name. */
8372 qsort (symtable1, count1, sizeof (struct elf_symbol),
8373 elf_sym_name_compare);
8374 qsort (symtable2, count1, sizeof (struct elf_symbol),
8375 elf_sym_name_compare);
8376
8377 for (i = 0; i < count1; i++)
8378 /* Two symbols must have the same binding, type and name. */
8379 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8380 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8381 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8382 goto done;
8383
8384 result = true;
8385 goto done;
8386 }
8387
8388 symtable1 = (struct elf_symbol *)
8389 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8390 symtable2 = (struct elf_symbol *)
8391 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8392 if (symtable1 == NULL || symtable2 == NULL)
8393 goto done;
8394
8395 /* Count definitions in the section. */
8396 count1 = 0;
8397 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8398 if (isym->st_shndx == shndx1
8399 && (!ignore_section_symbol_p
8400 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8401 symtable1[count1++].u.isym = isym;
8402
8403 count2 = 0;
8404 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8405 if (isym->st_shndx == shndx2
8406 && (!ignore_section_symbol_p
8407 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8408 symtable2[count2++].u.isym = isym;
8409
8410 if (count1 == 0 || count2 == 0 || count1 != count2)
8411 goto done;
8412
8413 for (i = 0; i < count1; i++)
8414 symtable1[i].name
8415 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8416 symtable1[i].u.isym->st_name);
8417
8418 for (i = 0; i < count2; i++)
8419 symtable2[i].name
8420 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8421 symtable2[i].u.isym->st_name);
8422
8423 /* Sort symbol by name. */
8424 qsort (symtable1, count1, sizeof (struct elf_symbol),
8425 elf_sym_name_compare);
8426 qsort (symtable2, count1, sizeof (struct elf_symbol),
8427 elf_sym_name_compare);
8428
8429 for (i = 0; i < count1; i++)
8430 /* Two symbols must have the same binding, type and name. */
8431 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8432 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8433 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8434 goto done;
8435
8436 result = true;
8437
8438 done:
8439 free (symtable1);
8440 free (symtable2);
8441 free (isymbuf1);
8442 free (isymbuf2);
8443
8444 return result;
8445 }
8446
8447 /* Return TRUE if 2 section types are compatible. */
8448
8449 bool
8450 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8451 bfd *bbfd, const asection *bsec)
8452 {
8453 if (asec == NULL
8454 || bsec == NULL
8455 || abfd->xvec->flavour != bfd_target_elf_flavour
8456 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8457 return true;
8458
8459 return elf_section_type (asec) == elf_section_type (bsec);
8460 }
8461 \f
8462 /* Final phase of ELF linker. */
8463
8464 /* A structure we use to avoid passing large numbers of arguments. */
8465
8466 struct elf_final_link_info
8467 {
8468 /* General link information. */
8469 struct bfd_link_info *info;
8470 /* Output BFD. */
8471 bfd *output_bfd;
8472 /* Symbol string table. */
8473 struct elf_strtab_hash *symstrtab;
8474 /* .hash section. */
8475 asection *hash_sec;
8476 /* symbol version section (.gnu.version). */
8477 asection *symver_sec;
8478 /* Buffer large enough to hold contents of any section. */
8479 bfd_byte *contents;
8480 /* Buffer large enough to hold external relocs of any section. */
8481 void *external_relocs;
8482 /* Buffer large enough to hold internal relocs of any section. */
8483 Elf_Internal_Rela *internal_relocs;
8484 /* Buffer large enough to hold external local symbols of any input
8485 BFD. */
8486 bfd_byte *external_syms;
8487 /* And a buffer for symbol section indices. */
8488 Elf_External_Sym_Shndx *locsym_shndx;
8489 /* Buffer large enough to hold internal local symbols of any input
8490 BFD. */
8491 Elf_Internal_Sym *internal_syms;
8492 /* Array large enough to hold a symbol index for each local symbol
8493 of any input BFD. */
8494 long *indices;
8495 /* Array large enough to hold a section pointer for each local
8496 symbol of any input BFD. */
8497 asection **sections;
8498 /* Buffer for SHT_SYMTAB_SHNDX section. */
8499 Elf_External_Sym_Shndx *symshndxbuf;
8500 /* Number of STT_FILE syms seen. */
8501 size_t filesym_count;
8502 /* Local symbol hash table. */
8503 struct bfd_hash_table local_hash_table;
8504 };
8505
8506 struct local_hash_entry
8507 {
8508 /* Base hash table entry structure. */
8509 struct bfd_hash_entry root;
8510 /* Size of the local symbol name. */
8511 size_t size;
8512 /* Number of the duplicated local symbol names. */
8513 long count;
8514 };
8515
8516 /* Create an entry in the local symbol hash table. */
8517
8518 static struct bfd_hash_entry *
8519 local_hash_newfunc (struct bfd_hash_entry *entry,
8520 struct bfd_hash_table *table,
8521 const char *string)
8522 {
8523
8524 /* Allocate the structure if it has not already been allocated by a
8525 subclass. */
8526 if (entry == NULL)
8527 {
8528 entry = bfd_hash_allocate (table,
8529 sizeof (struct local_hash_entry));
8530 if (entry == NULL)
8531 return entry;
8532 }
8533
8534 /* Call the allocation method of the superclass. */
8535 entry = bfd_hash_newfunc (entry, table, string);
8536 if (entry != NULL)
8537 {
8538 ((struct local_hash_entry *) entry)->count = 0;
8539 ((struct local_hash_entry *) entry)->size = 0;
8540 }
8541
8542 return entry;
8543 }
8544
8545 /* This struct is used to pass information to elf_link_output_extsym. */
8546
8547 struct elf_outext_info
8548 {
8549 bool failed;
8550 bool localsyms;
8551 bool file_sym_done;
8552 struct elf_final_link_info *flinfo;
8553 };
8554
8555
8556 /* Support for evaluating a complex relocation.
8557
8558 Complex relocations are generalized, self-describing relocations. The
8559 implementation of them consists of two parts: complex symbols, and the
8560 relocations themselves.
8561
8562 The relocations use a reserved elf-wide relocation type code (R_RELC
8563 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8564 information (start bit, end bit, word width, etc) into the addend. This
8565 information is extracted from CGEN-generated operand tables within gas.
8566
8567 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8568 internal) representing prefix-notation expressions, including but not
8569 limited to those sorts of expressions normally encoded as addends in the
8570 addend field. The symbol mangling format is:
8571
8572 <node> := <literal>
8573 | <unary-operator> ':' <node>
8574 | <binary-operator> ':' <node> ':' <node>
8575 ;
8576
8577 <literal> := 's' <digits=N> ':' <N character symbol name>
8578 | 'S' <digits=N> ':' <N character section name>
8579 | '#' <hexdigits>
8580 ;
8581
8582 <binary-operator> := as in C
8583 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8584
8585 static void
8586 set_symbol_value (bfd *bfd_with_globals,
8587 Elf_Internal_Sym *isymbuf,
8588 size_t locsymcount,
8589 size_t symidx,
8590 bfd_vma val)
8591 {
8592 struct elf_link_hash_entry **sym_hashes;
8593 struct elf_link_hash_entry *h;
8594 size_t extsymoff = locsymcount;
8595
8596 if (symidx < locsymcount)
8597 {
8598 Elf_Internal_Sym *sym;
8599
8600 sym = isymbuf + symidx;
8601 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8602 {
8603 /* It is a local symbol: move it to the
8604 "absolute" section and give it a value. */
8605 sym->st_shndx = SHN_ABS;
8606 sym->st_value = val;
8607 return;
8608 }
8609 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8610 extsymoff = 0;
8611 }
8612
8613 /* It is a global symbol: set its link type
8614 to "defined" and give it a value. */
8615
8616 sym_hashes = elf_sym_hashes (bfd_with_globals);
8617 h = sym_hashes [symidx - extsymoff];
8618 while (h->root.type == bfd_link_hash_indirect
8619 || h->root.type == bfd_link_hash_warning)
8620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8621 h->root.type = bfd_link_hash_defined;
8622 h->root.u.def.value = val;
8623 h->root.u.def.section = bfd_abs_section_ptr;
8624 }
8625
8626 static bool
8627 resolve_symbol (const char *name,
8628 bfd *input_bfd,
8629 struct elf_final_link_info *flinfo,
8630 bfd_vma *result,
8631 Elf_Internal_Sym *isymbuf,
8632 size_t locsymcount)
8633 {
8634 Elf_Internal_Sym *sym;
8635 struct bfd_link_hash_entry *global_entry;
8636 const char *candidate = NULL;
8637 Elf_Internal_Shdr *symtab_hdr;
8638 size_t i;
8639
8640 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8641
8642 for (i = 0; i < locsymcount; ++ i)
8643 {
8644 sym = isymbuf + i;
8645
8646 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8647 continue;
8648
8649 candidate = bfd_elf_string_from_elf_section (input_bfd,
8650 symtab_hdr->sh_link,
8651 sym->st_name);
8652 #ifdef DEBUG
8653 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8654 name, candidate, (unsigned long) sym->st_value);
8655 #endif
8656 if (candidate && strcmp (candidate, name) == 0)
8657 {
8658 asection *sec = flinfo->sections [i];
8659
8660 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8661 *result += sec->output_offset + sec->output_section->vma;
8662 #ifdef DEBUG
8663 printf ("Found symbol with value %8.8lx\n",
8664 (unsigned long) *result);
8665 #endif
8666 return true;
8667 }
8668 }
8669
8670 /* Hmm, haven't found it yet. perhaps it is a global. */
8671 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8672 false, false, true);
8673 if (!global_entry)
8674 return false;
8675
8676 if (global_entry->type == bfd_link_hash_defined
8677 || global_entry->type == bfd_link_hash_defweak)
8678 {
8679 *result = (global_entry->u.def.value
8680 + global_entry->u.def.section->output_section->vma
8681 + global_entry->u.def.section->output_offset);
8682 #ifdef DEBUG
8683 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8684 global_entry->root.string, (unsigned long) *result);
8685 #endif
8686 return true;
8687 }
8688
8689 return false;
8690 }
8691
8692 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8693 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8694 names like "foo.end" which is the end address of section "foo". */
8695
8696 static bool
8697 resolve_section (const char *name,
8698 asection *sections,
8699 bfd_vma *result,
8700 bfd * abfd)
8701 {
8702 asection *curr;
8703 unsigned int len;
8704
8705 for (curr = sections; curr; curr = curr->next)
8706 if (strcmp (curr->name, name) == 0)
8707 {
8708 *result = curr->vma;
8709 return true;
8710 }
8711
8712 /* Hmm. still haven't found it. try pseudo-section names. */
8713 /* FIXME: This could be coded more efficiently... */
8714 for (curr = sections; curr; curr = curr->next)
8715 {
8716 len = strlen (curr->name);
8717 if (len > strlen (name))
8718 continue;
8719
8720 if (strncmp (curr->name, name, len) == 0)
8721 {
8722 if (startswith (name + len, ".end"))
8723 {
8724 *result = (curr->vma
8725 + curr->size / bfd_octets_per_byte (abfd, curr));
8726 return true;
8727 }
8728
8729 /* Insert more pseudo-section names here, if you like. */
8730 }
8731 }
8732
8733 return false;
8734 }
8735
8736 static void
8737 undefined_reference (const char *reftype, const char *name)
8738 {
8739 /* xgettext:c-format */
8740 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8741 reftype, name);
8742 bfd_set_error (bfd_error_bad_value);
8743 }
8744
8745 static bool
8746 eval_symbol (bfd_vma *result,
8747 const char **symp,
8748 bfd *input_bfd,
8749 struct elf_final_link_info *flinfo,
8750 bfd_vma dot,
8751 Elf_Internal_Sym *isymbuf,
8752 size_t locsymcount,
8753 int signed_p)
8754 {
8755 size_t len;
8756 size_t symlen;
8757 bfd_vma a;
8758 bfd_vma b;
8759 char symbuf[4096];
8760 const char *sym = *symp;
8761 const char *symend;
8762 bool symbol_is_section = false;
8763
8764 len = strlen (sym);
8765 symend = sym + len;
8766
8767 if (len < 1 || len > sizeof (symbuf))
8768 {
8769 bfd_set_error (bfd_error_invalid_operation);
8770 return false;
8771 }
8772
8773 switch (* sym)
8774 {
8775 case '.':
8776 *result = dot;
8777 *symp = sym + 1;
8778 return true;
8779
8780 case '#':
8781 ++sym;
8782 *result = strtoul (sym, (char **) symp, 16);
8783 return true;
8784
8785 case 'S':
8786 symbol_is_section = true;
8787 /* Fall through. */
8788 case 's':
8789 ++sym;
8790 symlen = strtol (sym, (char **) symp, 10);
8791 sym = *symp + 1; /* Skip the trailing ':'. */
8792
8793 if (symend < sym || symlen + 1 > sizeof (symbuf))
8794 {
8795 bfd_set_error (bfd_error_invalid_operation);
8796 return false;
8797 }
8798
8799 memcpy (symbuf, sym, symlen);
8800 symbuf[symlen] = '\0';
8801 *symp = sym + symlen;
8802
8803 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8804 the symbol as a section, or vice-versa. so we're pretty liberal in our
8805 interpretation here; section means "try section first", not "must be a
8806 section", and likewise with symbol. */
8807
8808 if (symbol_is_section)
8809 {
8810 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8811 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8812 isymbuf, locsymcount))
8813 {
8814 undefined_reference ("section", symbuf);
8815 return false;
8816 }
8817 }
8818 else
8819 {
8820 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8821 isymbuf, locsymcount)
8822 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8823 result, input_bfd))
8824 {
8825 undefined_reference ("symbol", symbuf);
8826 return false;
8827 }
8828 }
8829
8830 return true;
8831
8832 /* All that remains are operators. */
8833
8834 #define UNARY_OP(op) \
8835 if (startswith (sym, #op)) \
8836 { \
8837 sym += strlen (#op); \
8838 if (*sym == ':') \
8839 ++sym; \
8840 *symp = sym; \
8841 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8842 isymbuf, locsymcount, signed_p)) \
8843 return false; \
8844 if (signed_p) \
8845 *result = op ((bfd_signed_vma) a); \
8846 else \
8847 *result = op a; \
8848 return true; \
8849 }
8850
8851 #define BINARY_OP_HEAD(op) \
8852 if (startswith (sym, #op)) \
8853 { \
8854 sym += strlen (#op); \
8855 if (*sym == ':') \
8856 ++sym; \
8857 *symp = sym; \
8858 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8859 isymbuf, locsymcount, signed_p)) \
8860 return false; \
8861 ++*symp; \
8862 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8863 isymbuf, locsymcount, signed_p)) \
8864 return false;
8865 #define BINARY_OP_TAIL(op) \
8866 if (signed_p) \
8867 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8868 else \
8869 *result = a op b; \
8870 return true; \
8871 }
8872 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8873
8874 default:
8875 UNARY_OP (0-);
8876 BINARY_OP_HEAD (<<);
8877 if (b >= sizeof (a) * CHAR_BIT)
8878 {
8879 *result = 0;
8880 return true;
8881 }
8882 signed_p = 0;
8883 BINARY_OP_TAIL (<<);
8884 BINARY_OP_HEAD (>>);
8885 if (b >= sizeof (a) * CHAR_BIT)
8886 {
8887 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8888 return true;
8889 }
8890 BINARY_OP_TAIL (>>);
8891 BINARY_OP (==);
8892 BINARY_OP (!=);
8893 BINARY_OP (<=);
8894 BINARY_OP (>=);
8895 BINARY_OP (&&);
8896 BINARY_OP (||);
8897 UNARY_OP (~);
8898 UNARY_OP (!);
8899 BINARY_OP (*);
8900 BINARY_OP_HEAD (/);
8901 if (b == 0)
8902 {
8903 _bfd_error_handler (_("division by zero"));
8904 bfd_set_error (bfd_error_bad_value);
8905 return false;
8906 }
8907 BINARY_OP_TAIL (/);
8908 BINARY_OP_HEAD (%);
8909 if (b == 0)
8910 {
8911 _bfd_error_handler (_("division by zero"));
8912 bfd_set_error (bfd_error_bad_value);
8913 return false;
8914 }
8915 BINARY_OP_TAIL (%);
8916 BINARY_OP (^);
8917 BINARY_OP (|);
8918 BINARY_OP (&);
8919 BINARY_OP (+);
8920 BINARY_OP (-);
8921 BINARY_OP (<);
8922 BINARY_OP (>);
8923 #undef UNARY_OP
8924 #undef BINARY_OP
8925 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8926 bfd_set_error (bfd_error_invalid_operation);
8927 return false;
8928 }
8929 }
8930
8931 static void
8932 put_value (bfd_vma size,
8933 unsigned long chunksz,
8934 bfd *input_bfd,
8935 bfd_vma x,
8936 bfd_byte *location)
8937 {
8938 location += (size - chunksz);
8939
8940 for (; size; size -= chunksz, location -= chunksz)
8941 {
8942 switch (chunksz)
8943 {
8944 case 1:
8945 bfd_put_8 (input_bfd, x, location);
8946 x >>= 8;
8947 break;
8948 case 2:
8949 bfd_put_16 (input_bfd, x, location);
8950 x >>= 16;
8951 break;
8952 case 4:
8953 bfd_put_32 (input_bfd, x, location);
8954 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8955 x >>= 16;
8956 x >>= 16;
8957 break;
8958 #ifdef BFD64
8959 case 8:
8960 bfd_put_64 (input_bfd, x, location);
8961 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8962 x >>= 32;
8963 x >>= 32;
8964 break;
8965 #endif
8966 default:
8967 abort ();
8968 break;
8969 }
8970 }
8971 }
8972
8973 static bfd_vma
8974 get_value (bfd_vma size,
8975 unsigned long chunksz,
8976 bfd *input_bfd,
8977 bfd_byte *location)
8978 {
8979 int shift;
8980 bfd_vma x = 0;
8981
8982 /* Sanity checks. */
8983 BFD_ASSERT (chunksz <= sizeof (x)
8984 && size >= chunksz
8985 && chunksz != 0
8986 && (size % chunksz) == 0
8987 && input_bfd != NULL
8988 && location != NULL);
8989
8990 if (chunksz == sizeof (x))
8991 {
8992 BFD_ASSERT (size == chunksz);
8993
8994 /* Make sure that we do not perform an undefined shift operation.
8995 We know that size == chunksz so there will only be one iteration
8996 of the loop below. */
8997 shift = 0;
8998 }
8999 else
9000 shift = 8 * chunksz;
9001
9002 for (; size; size -= chunksz, location += chunksz)
9003 {
9004 switch (chunksz)
9005 {
9006 case 1:
9007 x = (x << shift) | bfd_get_8 (input_bfd, location);
9008 break;
9009 case 2:
9010 x = (x << shift) | bfd_get_16 (input_bfd, location);
9011 break;
9012 case 4:
9013 x = (x << shift) | bfd_get_32 (input_bfd, location);
9014 break;
9015 #ifdef BFD64
9016 case 8:
9017 x = (x << shift) | bfd_get_64 (input_bfd, location);
9018 break;
9019 #endif
9020 default:
9021 abort ();
9022 }
9023 }
9024 return x;
9025 }
9026
9027 static void
9028 decode_complex_addend (unsigned long *start, /* in bits */
9029 unsigned long *oplen, /* in bits */
9030 unsigned long *len, /* in bits */
9031 unsigned long *wordsz, /* in bytes */
9032 unsigned long *chunksz, /* in bytes */
9033 unsigned long *lsb0_p,
9034 unsigned long *signed_p,
9035 unsigned long *trunc_p,
9036 unsigned long encoded)
9037 {
9038 * start = encoded & 0x3F;
9039 * len = (encoded >> 6) & 0x3F;
9040 * oplen = (encoded >> 12) & 0x3F;
9041 * wordsz = (encoded >> 18) & 0xF;
9042 * chunksz = (encoded >> 22) & 0xF;
9043 * lsb0_p = (encoded >> 27) & 1;
9044 * signed_p = (encoded >> 28) & 1;
9045 * trunc_p = (encoded >> 29) & 1;
9046 }
9047
9048 bfd_reloc_status_type
9049 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9050 asection *input_section,
9051 bfd_byte *contents,
9052 Elf_Internal_Rela *rel,
9053 bfd_vma relocation)
9054 {
9055 bfd_vma shift, x, mask;
9056 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9057 bfd_reloc_status_type r;
9058 bfd_size_type octets;
9059
9060 /* Perform this reloc, since it is complex.
9061 (this is not to say that it necessarily refers to a complex
9062 symbol; merely that it is a self-describing CGEN based reloc.
9063 i.e. the addend has the complete reloc information (bit start, end,
9064 word size, etc) encoded within it.). */
9065
9066 decode_complex_addend (&start, &oplen, &len, &wordsz,
9067 &chunksz, &lsb0_p, &signed_p,
9068 &trunc_p, rel->r_addend);
9069
9070 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9071
9072 if (lsb0_p)
9073 shift = (start + 1) - len;
9074 else
9075 shift = (8 * wordsz) - (start + len);
9076
9077 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9078 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9079
9080 #ifdef DEBUG
9081 printf ("Doing complex reloc: "
9082 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9083 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9084 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9085 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9086 oplen, (unsigned long) x, (unsigned long) mask,
9087 (unsigned long) relocation);
9088 #endif
9089
9090 r = bfd_reloc_ok;
9091 if (! trunc_p)
9092 /* Now do an overflow check. */
9093 r = bfd_check_overflow ((signed_p
9094 ? complain_overflow_signed
9095 : complain_overflow_unsigned),
9096 len, 0, (8 * wordsz),
9097 relocation);
9098
9099 /* Do the deed. */
9100 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9101
9102 #ifdef DEBUG
9103 printf (" relocation: %8.8lx\n"
9104 " shifted mask: %8.8lx\n"
9105 " shifted/masked reloc: %8.8lx\n"
9106 " result: %8.8lx\n",
9107 (unsigned long) relocation, (unsigned long) (mask << shift),
9108 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9109 #endif
9110 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9111 return r;
9112 }
9113
9114 /* Functions to read r_offset from external (target order) reloc
9115 entry. Faster than bfd_getl32 et al, because we let the compiler
9116 know the value is aligned. */
9117
9118 static bfd_vma
9119 ext32l_r_offset (const void *p)
9120 {
9121 union aligned32
9122 {
9123 uint32_t v;
9124 unsigned char c[4];
9125 };
9126 const union aligned32 *a
9127 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9128
9129 uint32_t aval = ( (uint32_t) a->c[0]
9130 | (uint32_t) a->c[1] << 8
9131 | (uint32_t) a->c[2] << 16
9132 | (uint32_t) a->c[3] << 24);
9133 return aval;
9134 }
9135
9136 static bfd_vma
9137 ext32b_r_offset (const void *p)
9138 {
9139 union aligned32
9140 {
9141 uint32_t v;
9142 unsigned char c[4];
9143 };
9144 const union aligned32 *a
9145 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9146
9147 uint32_t aval = ( (uint32_t) a->c[0] << 24
9148 | (uint32_t) a->c[1] << 16
9149 | (uint32_t) a->c[2] << 8
9150 | (uint32_t) a->c[3]);
9151 return aval;
9152 }
9153
9154 #ifdef BFD_HOST_64_BIT
9155 static bfd_vma
9156 ext64l_r_offset (const void *p)
9157 {
9158 union aligned64
9159 {
9160 uint64_t v;
9161 unsigned char c[8];
9162 };
9163 const union aligned64 *a
9164 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9165
9166 uint64_t aval = ( (uint64_t) a->c[0]
9167 | (uint64_t) a->c[1] << 8
9168 | (uint64_t) a->c[2] << 16
9169 | (uint64_t) a->c[3] << 24
9170 | (uint64_t) a->c[4] << 32
9171 | (uint64_t) a->c[5] << 40
9172 | (uint64_t) a->c[6] << 48
9173 | (uint64_t) a->c[7] << 56);
9174 return aval;
9175 }
9176
9177 static bfd_vma
9178 ext64b_r_offset (const void *p)
9179 {
9180 union aligned64
9181 {
9182 uint64_t v;
9183 unsigned char c[8];
9184 };
9185 const union aligned64 *a
9186 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9187
9188 uint64_t aval = ( (uint64_t) a->c[0] << 56
9189 | (uint64_t) a->c[1] << 48
9190 | (uint64_t) a->c[2] << 40
9191 | (uint64_t) a->c[3] << 32
9192 | (uint64_t) a->c[4] << 24
9193 | (uint64_t) a->c[5] << 16
9194 | (uint64_t) a->c[6] << 8
9195 | (uint64_t) a->c[7]);
9196 return aval;
9197 }
9198 #endif
9199
9200 /* When performing a relocatable link, the input relocations are
9201 preserved. But, if they reference global symbols, the indices
9202 referenced must be updated. Update all the relocations found in
9203 RELDATA. */
9204
9205 static bool
9206 elf_link_adjust_relocs (bfd *abfd,
9207 asection *sec,
9208 struct bfd_elf_section_reloc_data *reldata,
9209 bool sort,
9210 struct bfd_link_info *info)
9211 {
9212 unsigned int i;
9213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9214 bfd_byte *erela;
9215 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9216 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9217 bfd_vma r_type_mask;
9218 int r_sym_shift;
9219 unsigned int count = reldata->count;
9220 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9221
9222 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9223 {
9224 swap_in = bed->s->swap_reloc_in;
9225 swap_out = bed->s->swap_reloc_out;
9226 }
9227 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9228 {
9229 swap_in = bed->s->swap_reloca_in;
9230 swap_out = bed->s->swap_reloca_out;
9231 }
9232 else
9233 abort ();
9234
9235 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9236 abort ();
9237
9238 if (bed->s->arch_size == 32)
9239 {
9240 r_type_mask = 0xff;
9241 r_sym_shift = 8;
9242 }
9243 else
9244 {
9245 r_type_mask = 0xffffffff;
9246 r_sym_shift = 32;
9247 }
9248
9249 erela = reldata->hdr->contents;
9250 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9251 {
9252 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9253 unsigned int j;
9254
9255 if (*rel_hash == NULL)
9256 continue;
9257
9258 if ((*rel_hash)->indx == -2
9259 && info->gc_sections
9260 && ! info->gc_keep_exported)
9261 {
9262 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9263 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9264 abfd, sec,
9265 (*rel_hash)->root.root.string);
9266 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9267 abfd, sec);
9268 bfd_set_error (bfd_error_invalid_operation);
9269 return false;
9270 }
9271 BFD_ASSERT ((*rel_hash)->indx >= 0);
9272
9273 (*swap_in) (abfd, erela, irela);
9274 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9275 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9276 | (irela[j].r_info & r_type_mask));
9277 (*swap_out) (abfd, irela, erela);
9278 }
9279
9280 if (bed->elf_backend_update_relocs)
9281 (*bed->elf_backend_update_relocs) (sec, reldata);
9282
9283 if (sort && count != 0)
9284 {
9285 bfd_vma (*ext_r_off) (const void *);
9286 bfd_vma r_off;
9287 size_t elt_size;
9288 bfd_byte *base, *end, *p, *loc;
9289 bfd_byte *buf = NULL;
9290
9291 if (bed->s->arch_size == 32)
9292 {
9293 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9294 ext_r_off = ext32l_r_offset;
9295 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9296 ext_r_off = ext32b_r_offset;
9297 else
9298 abort ();
9299 }
9300 else
9301 {
9302 #ifdef BFD_HOST_64_BIT
9303 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9304 ext_r_off = ext64l_r_offset;
9305 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9306 ext_r_off = ext64b_r_offset;
9307 else
9308 #endif
9309 abort ();
9310 }
9311
9312 /* Must use a stable sort here. A modified insertion sort,
9313 since the relocs are mostly sorted already. */
9314 elt_size = reldata->hdr->sh_entsize;
9315 base = reldata->hdr->contents;
9316 end = base + count * elt_size;
9317 if (elt_size > sizeof (Elf64_External_Rela))
9318 abort ();
9319
9320 /* Ensure the first element is lowest. This acts as a sentinel,
9321 speeding the main loop below. */
9322 r_off = (*ext_r_off) (base);
9323 for (p = loc = base; (p += elt_size) < end; )
9324 {
9325 bfd_vma r_off2 = (*ext_r_off) (p);
9326 if (r_off > r_off2)
9327 {
9328 r_off = r_off2;
9329 loc = p;
9330 }
9331 }
9332 if (loc != base)
9333 {
9334 /* Don't just swap *base and *loc as that changes the order
9335 of the original base[0] and base[1] if they happen to
9336 have the same r_offset. */
9337 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9338 memcpy (onebuf, loc, elt_size);
9339 memmove (base + elt_size, base, loc - base);
9340 memcpy (base, onebuf, elt_size);
9341 }
9342
9343 for (p = base + elt_size; (p += elt_size) < end; )
9344 {
9345 /* base to p is sorted, *p is next to insert. */
9346 r_off = (*ext_r_off) (p);
9347 /* Search the sorted region for location to insert. */
9348 loc = p - elt_size;
9349 while (r_off < (*ext_r_off) (loc))
9350 loc -= elt_size;
9351 loc += elt_size;
9352 if (loc != p)
9353 {
9354 /* Chances are there is a run of relocs to insert here,
9355 from one of more input files. Files are not always
9356 linked in order due to the way elf_link_input_bfd is
9357 called. See pr17666. */
9358 size_t sortlen = p - loc;
9359 bfd_vma r_off2 = (*ext_r_off) (loc);
9360 size_t runlen = elt_size;
9361 size_t buf_size = 96 * 1024;
9362 while (p + runlen < end
9363 && (sortlen <= buf_size
9364 || runlen + elt_size <= buf_size)
9365 && r_off2 > (*ext_r_off) (p + runlen))
9366 runlen += elt_size;
9367 if (buf == NULL)
9368 {
9369 buf = bfd_malloc (buf_size);
9370 if (buf == NULL)
9371 return false;
9372 }
9373 if (runlen < sortlen)
9374 {
9375 memcpy (buf, p, runlen);
9376 memmove (loc + runlen, loc, sortlen);
9377 memcpy (loc, buf, runlen);
9378 }
9379 else
9380 {
9381 memcpy (buf, loc, sortlen);
9382 memmove (loc, p, runlen);
9383 memcpy (loc + runlen, buf, sortlen);
9384 }
9385 p += runlen - elt_size;
9386 }
9387 }
9388 /* Hashes are no longer valid. */
9389 free (reldata->hashes);
9390 reldata->hashes = NULL;
9391 free (buf);
9392 }
9393 return true;
9394 }
9395
9396 struct elf_link_sort_rela
9397 {
9398 union {
9399 bfd_vma offset;
9400 bfd_vma sym_mask;
9401 } u;
9402 enum elf_reloc_type_class type;
9403 /* We use this as an array of size int_rels_per_ext_rel. */
9404 Elf_Internal_Rela rela[1];
9405 };
9406
9407 /* qsort stability here and for cmp2 is only an issue if multiple
9408 dynamic relocations are emitted at the same address. But targets
9409 that apply a series of dynamic relocations each operating on the
9410 result of the prior relocation can't use -z combreloc as
9411 implemented anyway. Such schemes tend to be broken by sorting on
9412 symbol index. That leaves dynamic NONE relocs as the only other
9413 case where ld might emit multiple relocs at the same address, and
9414 those are only emitted due to target bugs. */
9415
9416 static int
9417 elf_link_sort_cmp1 (const void *A, const void *B)
9418 {
9419 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9420 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9421 int relativea, relativeb;
9422
9423 relativea = a->type == reloc_class_relative;
9424 relativeb = b->type == reloc_class_relative;
9425
9426 if (relativea < relativeb)
9427 return 1;
9428 if (relativea > relativeb)
9429 return -1;
9430 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9431 return -1;
9432 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9433 return 1;
9434 if (a->rela->r_offset < b->rela->r_offset)
9435 return -1;
9436 if (a->rela->r_offset > b->rela->r_offset)
9437 return 1;
9438 return 0;
9439 }
9440
9441 static int
9442 elf_link_sort_cmp2 (const void *A, const void *B)
9443 {
9444 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9445 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9446
9447 if (a->type < b->type)
9448 return -1;
9449 if (a->type > b->type)
9450 return 1;
9451 if (a->u.offset < b->u.offset)
9452 return -1;
9453 if (a->u.offset > b->u.offset)
9454 return 1;
9455 if (a->rela->r_offset < b->rela->r_offset)
9456 return -1;
9457 if (a->rela->r_offset > b->rela->r_offset)
9458 return 1;
9459 return 0;
9460 }
9461
9462 static size_t
9463 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9464 {
9465 asection *dynamic_relocs;
9466 asection *rela_dyn;
9467 asection *rel_dyn;
9468 bfd_size_type count, size;
9469 size_t i, ret, sort_elt, ext_size;
9470 bfd_byte *sort, *s_non_relative, *p;
9471 struct elf_link_sort_rela *sq;
9472 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9473 int i2e = bed->s->int_rels_per_ext_rel;
9474 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9475 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9476 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9477 struct bfd_link_order *lo;
9478 bfd_vma r_sym_mask;
9479 bool use_rela;
9480
9481 /* Find a dynamic reloc section. */
9482 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9483 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9484 if (rela_dyn != NULL && rela_dyn->size > 0
9485 && rel_dyn != NULL && rel_dyn->size > 0)
9486 {
9487 bool use_rela_initialised = false;
9488
9489 /* This is just here to stop gcc from complaining.
9490 Its initialization checking code is not perfect. */
9491 use_rela = true;
9492
9493 /* Both sections are present. Examine the sizes
9494 of the indirect sections to help us choose. */
9495 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9496 if (lo->type == bfd_indirect_link_order)
9497 {
9498 asection *o = lo->u.indirect.section;
9499
9500 if ((o->size % bed->s->sizeof_rela) == 0)
9501 {
9502 if ((o->size % bed->s->sizeof_rel) == 0)
9503 /* Section size is divisible by both rel and rela sizes.
9504 It is of no help to us. */
9505 ;
9506 else
9507 {
9508 /* Section size is only divisible by rela. */
9509 if (use_rela_initialised && !use_rela)
9510 {
9511 _bfd_error_handler (_("%pB: unable to sort relocs - "
9512 "they are in more than one size"),
9513 abfd);
9514 bfd_set_error (bfd_error_invalid_operation);
9515 return 0;
9516 }
9517 else
9518 {
9519 use_rela = true;
9520 use_rela_initialised = true;
9521 }
9522 }
9523 }
9524 else if ((o->size % bed->s->sizeof_rel) == 0)
9525 {
9526 /* Section size is only divisible by rel. */
9527 if (use_rela_initialised && use_rela)
9528 {
9529 _bfd_error_handler (_("%pB: unable to sort relocs - "
9530 "they are in more than one size"),
9531 abfd);
9532 bfd_set_error (bfd_error_invalid_operation);
9533 return 0;
9534 }
9535 else
9536 {
9537 use_rela = false;
9538 use_rela_initialised = true;
9539 }
9540 }
9541 else
9542 {
9543 /* The section size is not divisible by either -
9544 something is wrong. */
9545 _bfd_error_handler (_("%pB: unable to sort relocs - "
9546 "they are of an unknown size"), abfd);
9547 bfd_set_error (bfd_error_invalid_operation);
9548 return 0;
9549 }
9550 }
9551
9552 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9553 if (lo->type == bfd_indirect_link_order)
9554 {
9555 asection *o = lo->u.indirect.section;
9556
9557 if ((o->size % bed->s->sizeof_rela) == 0)
9558 {
9559 if ((o->size % bed->s->sizeof_rel) == 0)
9560 /* Section size is divisible by both rel and rela sizes.
9561 It is of no help to us. */
9562 ;
9563 else
9564 {
9565 /* Section size is only divisible by rela. */
9566 if (use_rela_initialised && !use_rela)
9567 {
9568 _bfd_error_handler (_("%pB: unable to sort relocs - "
9569 "they are in more than one size"),
9570 abfd);
9571 bfd_set_error (bfd_error_invalid_operation);
9572 return 0;
9573 }
9574 else
9575 {
9576 use_rela = true;
9577 use_rela_initialised = true;
9578 }
9579 }
9580 }
9581 else if ((o->size % bed->s->sizeof_rel) == 0)
9582 {
9583 /* Section size is only divisible by rel. */
9584 if (use_rela_initialised && use_rela)
9585 {
9586 _bfd_error_handler (_("%pB: unable to sort relocs - "
9587 "they are in more than one size"),
9588 abfd);
9589 bfd_set_error (bfd_error_invalid_operation);
9590 return 0;
9591 }
9592 else
9593 {
9594 use_rela = false;
9595 use_rela_initialised = true;
9596 }
9597 }
9598 else
9599 {
9600 /* The section size is not divisible by either -
9601 something is wrong. */
9602 _bfd_error_handler (_("%pB: unable to sort relocs - "
9603 "they are of an unknown size"), abfd);
9604 bfd_set_error (bfd_error_invalid_operation);
9605 return 0;
9606 }
9607 }
9608
9609 if (! use_rela_initialised)
9610 /* Make a guess. */
9611 use_rela = true;
9612 }
9613 else if (rela_dyn != NULL && rela_dyn->size > 0)
9614 use_rela = true;
9615 else if (rel_dyn != NULL && rel_dyn->size > 0)
9616 use_rela = false;
9617 else
9618 return 0;
9619
9620 if (use_rela)
9621 {
9622 dynamic_relocs = rela_dyn;
9623 ext_size = bed->s->sizeof_rela;
9624 swap_in = bed->s->swap_reloca_in;
9625 swap_out = bed->s->swap_reloca_out;
9626 }
9627 else
9628 {
9629 dynamic_relocs = rel_dyn;
9630 ext_size = bed->s->sizeof_rel;
9631 swap_in = bed->s->swap_reloc_in;
9632 swap_out = bed->s->swap_reloc_out;
9633 }
9634
9635 size = 0;
9636 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9637 if (lo->type == bfd_indirect_link_order)
9638 size += lo->u.indirect.section->size;
9639
9640 if (size != dynamic_relocs->size)
9641 return 0;
9642
9643 sort_elt = (sizeof (struct elf_link_sort_rela)
9644 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9645
9646 count = dynamic_relocs->size / ext_size;
9647 if (count == 0)
9648 return 0;
9649 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9650
9651 if (sort == NULL)
9652 {
9653 (*info->callbacks->warning)
9654 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9655 return 0;
9656 }
9657
9658 if (bed->s->arch_size == 32)
9659 r_sym_mask = ~(bfd_vma) 0xff;
9660 else
9661 r_sym_mask = ~(bfd_vma) 0xffffffff;
9662
9663 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9664 if (lo->type == bfd_indirect_link_order)
9665 {
9666 bfd_byte *erel, *erelend;
9667 asection *o = lo->u.indirect.section;
9668
9669 if (o->contents == NULL && o->size != 0)
9670 {
9671 /* This is a reloc section that is being handled as a normal
9672 section. See bfd_section_from_shdr. We can't combine
9673 relocs in this case. */
9674 free (sort);
9675 return 0;
9676 }
9677 erel = o->contents;
9678 erelend = o->contents + o->size;
9679 p = sort + o->output_offset * opb / ext_size * sort_elt;
9680
9681 while (erel < erelend)
9682 {
9683 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9684
9685 (*swap_in) (abfd, erel, s->rela);
9686 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9687 s->u.sym_mask = r_sym_mask;
9688 p += sort_elt;
9689 erel += ext_size;
9690 }
9691 }
9692
9693 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9694
9695 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9696 {
9697 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9698 if (s->type != reloc_class_relative)
9699 break;
9700 }
9701 ret = i;
9702 s_non_relative = p;
9703
9704 sq = (struct elf_link_sort_rela *) s_non_relative;
9705 for (; i < count; i++, p += sort_elt)
9706 {
9707 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9708 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9709 sq = sp;
9710 sp->u.offset = sq->rela->r_offset;
9711 }
9712
9713 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9714
9715 struct elf_link_hash_table *htab = elf_hash_table (info);
9716 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9717 {
9718 /* We have plt relocs in .rela.dyn. */
9719 sq = (struct elf_link_sort_rela *) sort;
9720 for (i = 0; i < count; i++)
9721 if (sq[count - i - 1].type != reloc_class_plt)
9722 break;
9723 if (i != 0 && htab->srelplt->size == i * ext_size)
9724 {
9725 struct bfd_link_order **plo;
9726 /* Put srelplt link_order last. This is so the output_offset
9727 set in the next loop is correct for DT_JMPREL. */
9728 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9729 if ((*plo)->type == bfd_indirect_link_order
9730 && (*plo)->u.indirect.section == htab->srelplt)
9731 {
9732 lo = *plo;
9733 *plo = lo->next;
9734 }
9735 else
9736 plo = &(*plo)->next;
9737 *plo = lo;
9738 lo->next = NULL;
9739 dynamic_relocs->map_tail.link_order = lo;
9740 }
9741 }
9742
9743 p = sort;
9744 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9745 if (lo->type == bfd_indirect_link_order)
9746 {
9747 bfd_byte *erel, *erelend;
9748 asection *o = lo->u.indirect.section;
9749
9750 erel = o->contents;
9751 erelend = o->contents + o->size;
9752 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9753 while (erel < erelend)
9754 {
9755 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9756 (*swap_out) (abfd, s->rela, erel);
9757 p += sort_elt;
9758 erel += ext_size;
9759 }
9760 }
9761
9762 free (sort);
9763 *psec = dynamic_relocs;
9764 return ret;
9765 }
9766
9767 /* Add a symbol to the output symbol string table. */
9768
9769 static int
9770 elf_link_output_symstrtab (void *finf,
9771 const char *name,
9772 Elf_Internal_Sym *elfsym,
9773 asection *input_sec,
9774 struct elf_link_hash_entry *h)
9775 {
9776 struct elf_final_link_info *flinfo = finf;
9777 int (*output_symbol_hook)
9778 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9779 struct elf_link_hash_entry *);
9780 struct elf_link_hash_table *hash_table;
9781 const struct elf_backend_data *bed;
9782 bfd_size_type strtabsize;
9783
9784 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9785
9786 bed = get_elf_backend_data (flinfo->output_bfd);
9787 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9788 if (output_symbol_hook != NULL)
9789 {
9790 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9791 if (ret != 1)
9792 return ret;
9793 }
9794
9795 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9796 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9797 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9798 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9799
9800 if (name == NULL
9801 || *name == '\0'
9802 || (input_sec->flags & SEC_EXCLUDE))
9803 elfsym->st_name = (unsigned long) -1;
9804 else
9805 {
9806 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9807 to get the final offset for st_name. */
9808 char *versioned_name = (char *) name;
9809 if (h != NULL)
9810 {
9811 if (h->versioned == versioned && h->def_dynamic)
9812 {
9813 /* Keep only one '@' for versioned symbols defined in
9814 shared objects. */
9815 char *version = strrchr (name, ELF_VER_CHR);
9816 char *base_end = strchr (name, ELF_VER_CHR);
9817 if (version != base_end)
9818 {
9819 size_t base_len;
9820 size_t len = strlen (name);
9821 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9822 if (versioned_name == NULL)
9823 return 0;
9824 base_len = base_end - name;
9825 memcpy (versioned_name, name, base_len);
9826 memcpy (versioned_name + base_len, version,
9827 len - base_len);
9828 }
9829 }
9830 }
9831 else if (flinfo->info->unique_symbol
9832 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9833 {
9834 struct local_hash_entry *lh;
9835 size_t count_len;
9836 size_t base_len;
9837 char buf[30];
9838 switch (ELF_ST_TYPE (elfsym->st_info))
9839 {
9840 case STT_FILE:
9841 case STT_SECTION:
9842 break;
9843 default:
9844 lh = (struct local_hash_entry *) bfd_hash_lookup
9845 (&flinfo->local_hash_table, name, true, false);
9846 if (lh == NULL)
9847 return 0;
9848 /* Always append ".COUNT" to local symbols to avoid
9849 potential conflicts with local symbol "XXX.COUNT". */
9850 sprintf (buf, "%lx", lh->count);
9851 base_len = lh->size;
9852 if (!base_len)
9853 {
9854 base_len = strlen (name);
9855 lh->size = base_len;
9856 }
9857 count_len = strlen (buf);
9858 versioned_name = bfd_alloc (flinfo->output_bfd,
9859 base_len + count_len + 2);
9860 if (versioned_name == NULL)
9861 return 0;
9862 memcpy (versioned_name, name, base_len);
9863 versioned_name[base_len] = '.';
9864 memcpy (versioned_name + base_len + 1, buf,
9865 count_len + 1);
9866 lh->count++;
9867 break;
9868 }
9869 }
9870 elfsym->st_name
9871 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9872 versioned_name, false);
9873 if (elfsym->st_name == (unsigned long) -1)
9874 return 0;
9875 }
9876
9877 hash_table = elf_hash_table (flinfo->info);
9878 strtabsize = hash_table->strtabsize;
9879 if (strtabsize <= hash_table->strtabcount)
9880 {
9881 strtabsize += strtabsize;
9882 hash_table->strtabsize = strtabsize;
9883 strtabsize *= sizeof (*hash_table->strtab);
9884 hash_table->strtab
9885 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9886 strtabsize);
9887 if (hash_table->strtab == NULL)
9888 return 0;
9889 }
9890 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9891 hash_table->strtab[hash_table->strtabcount].dest_index
9892 = hash_table->strtabcount;
9893 hash_table->strtab[hash_table->strtabcount].destshndx_index
9894 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9895
9896 flinfo->output_bfd->symcount += 1;
9897 hash_table->strtabcount += 1;
9898
9899 return 1;
9900 }
9901
9902 /* Swap symbols out to the symbol table and flush the output symbols to
9903 the file. */
9904
9905 static bool
9906 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9907 {
9908 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9909 size_t amt;
9910 size_t i;
9911 const struct elf_backend_data *bed;
9912 bfd_byte *symbuf;
9913 Elf_Internal_Shdr *hdr;
9914 file_ptr pos;
9915 bool ret;
9916
9917 if (!hash_table->strtabcount)
9918 return true;
9919
9920 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9921
9922 bed = get_elf_backend_data (flinfo->output_bfd);
9923
9924 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9925 symbuf = (bfd_byte *) bfd_malloc (amt);
9926 if (symbuf == NULL)
9927 return false;
9928
9929 if (flinfo->symshndxbuf)
9930 {
9931 amt = sizeof (Elf_External_Sym_Shndx);
9932 amt *= bfd_get_symcount (flinfo->output_bfd);
9933 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9934 if (flinfo->symshndxbuf == NULL)
9935 {
9936 free (symbuf);
9937 return false;
9938 }
9939 }
9940
9941 /* Now swap out the symbols. */
9942 for (i = 0; i < hash_table->strtabcount; i++)
9943 {
9944 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9945 if (elfsym->sym.st_name == (unsigned long) -1)
9946 elfsym->sym.st_name = 0;
9947 else
9948 elfsym->sym.st_name
9949 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9950 elfsym->sym.st_name);
9951
9952 /* Inform the linker of the addition of this symbol. */
9953
9954 if (flinfo->info->callbacks->ctf_new_symbol)
9955 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9956 &elfsym->sym);
9957
9958 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9959 ((bfd_byte *) symbuf
9960 + (elfsym->dest_index
9961 * bed->s->sizeof_sym)),
9962 (flinfo->symshndxbuf
9963 + elfsym->destshndx_index));
9964 }
9965
9966 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9967 pos = hdr->sh_offset + hdr->sh_size;
9968 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9969 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9970 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9971 {
9972 hdr->sh_size += amt;
9973 ret = true;
9974 }
9975 else
9976 ret = false;
9977
9978 free (symbuf);
9979
9980 free (hash_table->strtab);
9981 hash_table->strtab = NULL;
9982
9983 return ret;
9984 }
9985
9986 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9987
9988 static bool
9989 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9990 {
9991 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9992 && sym->st_shndx < SHN_LORESERVE)
9993 {
9994 /* The gABI doesn't support dynamic symbols in output sections
9995 beyond 64k. */
9996 _bfd_error_handler
9997 /* xgettext:c-format */
9998 (_("%pB: too many sections: %d (>= %d)"),
9999 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10000 bfd_set_error (bfd_error_nonrepresentable_section);
10001 return false;
10002 }
10003 return true;
10004 }
10005
10006 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10007 allowing an unsatisfied unversioned symbol in the DSO to match a
10008 versioned symbol that would normally require an explicit version.
10009 We also handle the case that a DSO references a hidden symbol
10010 which may be satisfied by a versioned symbol in another DSO. */
10011
10012 static bool
10013 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10014 const struct elf_backend_data *bed,
10015 struct elf_link_hash_entry *h)
10016 {
10017 bfd *abfd;
10018 struct elf_link_loaded_list *loaded;
10019
10020 if (!is_elf_hash_table (info->hash))
10021 return false;
10022
10023 /* Check indirect symbol. */
10024 while (h->root.type == bfd_link_hash_indirect)
10025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10026
10027 switch (h->root.type)
10028 {
10029 default:
10030 abfd = NULL;
10031 break;
10032
10033 case bfd_link_hash_undefined:
10034 case bfd_link_hash_undefweak:
10035 abfd = h->root.u.undef.abfd;
10036 if (abfd == NULL
10037 || (abfd->flags & DYNAMIC) == 0
10038 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10039 return false;
10040 break;
10041
10042 case bfd_link_hash_defined:
10043 case bfd_link_hash_defweak:
10044 abfd = h->root.u.def.section->owner;
10045 break;
10046
10047 case bfd_link_hash_common:
10048 abfd = h->root.u.c.p->section->owner;
10049 break;
10050 }
10051 BFD_ASSERT (abfd != NULL);
10052
10053 for (loaded = elf_hash_table (info)->dyn_loaded;
10054 loaded != NULL;
10055 loaded = loaded->next)
10056 {
10057 bfd *input;
10058 Elf_Internal_Shdr *hdr;
10059 size_t symcount;
10060 size_t extsymcount;
10061 size_t extsymoff;
10062 Elf_Internal_Shdr *versymhdr;
10063 Elf_Internal_Sym *isym;
10064 Elf_Internal_Sym *isymend;
10065 Elf_Internal_Sym *isymbuf;
10066 Elf_External_Versym *ever;
10067 Elf_External_Versym *extversym;
10068
10069 input = loaded->abfd;
10070
10071 /* We check each DSO for a possible hidden versioned definition. */
10072 if (input == abfd
10073 || elf_dynversym (input) == 0)
10074 continue;
10075
10076 hdr = &elf_tdata (input)->dynsymtab_hdr;
10077
10078 symcount = hdr->sh_size / bed->s->sizeof_sym;
10079 if (elf_bad_symtab (input))
10080 {
10081 extsymcount = symcount;
10082 extsymoff = 0;
10083 }
10084 else
10085 {
10086 extsymcount = symcount - hdr->sh_info;
10087 extsymoff = hdr->sh_info;
10088 }
10089
10090 if (extsymcount == 0)
10091 continue;
10092
10093 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10094 NULL, NULL, NULL);
10095 if (isymbuf == NULL)
10096 return false;
10097
10098 /* Read in any version definitions. */
10099 versymhdr = &elf_tdata (input)->dynversym_hdr;
10100 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10101 || (extversym = (Elf_External_Versym *)
10102 _bfd_malloc_and_read (input, versymhdr->sh_size,
10103 versymhdr->sh_size)) == NULL)
10104 {
10105 free (isymbuf);
10106 return false;
10107 }
10108
10109 ever = extversym + extsymoff;
10110 isymend = isymbuf + extsymcount;
10111 for (isym = isymbuf; isym < isymend; isym++, ever++)
10112 {
10113 const char *name;
10114 Elf_Internal_Versym iver;
10115 unsigned short version_index;
10116
10117 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10118 || isym->st_shndx == SHN_UNDEF)
10119 continue;
10120
10121 name = bfd_elf_string_from_elf_section (input,
10122 hdr->sh_link,
10123 isym->st_name);
10124 if (strcmp (name, h->root.root.string) != 0)
10125 continue;
10126
10127 _bfd_elf_swap_versym_in (input, ever, &iver);
10128
10129 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10130 && !(h->def_regular
10131 && h->forced_local))
10132 {
10133 /* If we have a non-hidden versioned sym, then it should
10134 have provided a definition for the undefined sym unless
10135 it is defined in a non-shared object and forced local.
10136 */
10137 abort ();
10138 }
10139
10140 version_index = iver.vs_vers & VERSYM_VERSION;
10141 if (version_index == 1 || version_index == 2)
10142 {
10143 /* This is the base or first version. We can use it. */
10144 free (extversym);
10145 free (isymbuf);
10146 return true;
10147 }
10148 }
10149
10150 free (extversym);
10151 free (isymbuf);
10152 }
10153
10154 return false;
10155 }
10156
10157 /* Convert ELF common symbol TYPE. */
10158
10159 static int
10160 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10161 {
10162 /* Commom symbol can only appear in relocatable link. */
10163 if (!bfd_link_relocatable (info))
10164 abort ();
10165 switch (info->elf_stt_common)
10166 {
10167 case unchanged:
10168 break;
10169 case elf_stt_common:
10170 type = STT_COMMON;
10171 break;
10172 case no_elf_stt_common:
10173 type = STT_OBJECT;
10174 break;
10175 }
10176 return type;
10177 }
10178
10179 /* Add an external symbol to the symbol table. This is called from
10180 the hash table traversal routine. When generating a shared object,
10181 we go through the symbol table twice. The first time we output
10182 anything that might have been forced to local scope in a version
10183 script. The second time we output the symbols that are still
10184 global symbols. */
10185
10186 static bool
10187 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10188 {
10189 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10190 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10191 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10192 bool strip;
10193 Elf_Internal_Sym sym;
10194 asection *input_sec;
10195 const struct elf_backend_data *bed;
10196 long indx;
10197 int ret;
10198 unsigned int type;
10199
10200 if (h->root.type == bfd_link_hash_warning)
10201 {
10202 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10203 if (h->root.type == bfd_link_hash_new)
10204 return true;
10205 }
10206
10207 /* Decide whether to output this symbol in this pass. */
10208 if (eoinfo->localsyms)
10209 {
10210 if (!h->forced_local)
10211 return true;
10212 }
10213 else
10214 {
10215 if (h->forced_local)
10216 return true;
10217 }
10218
10219 bed = get_elf_backend_data (flinfo->output_bfd);
10220
10221 if (h->root.type == bfd_link_hash_undefined)
10222 {
10223 /* If we have an undefined symbol reference here then it must have
10224 come from a shared library that is being linked in. (Undefined
10225 references in regular files have already been handled unless
10226 they are in unreferenced sections which are removed by garbage
10227 collection). */
10228 bool ignore_undef = false;
10229
10230 /* Some symbols may be special in that the fact that they're
10231 undefined can be safely ignored - let backend determine that. */
10232 if (bed->elf_backend_ignore_undef_symbol)
10233 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10234
10235 /* If we are reporting errors for this situation then do so now. */
10236 if (!ignore_undef
10237 && h->ref_dynamic_nonweak
10238 && (!h->ref_regular || flinfo->info->gc_sections)
10239 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10240 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10241 {
10242 flinfo->info->callbacks->undefined_symbol
10243 (flinfo->info, h->root.root.string,
10244 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10245 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10246 && !flinfo->info->warn_unresolved_syms);
10247 }
10248
10249 /* Strip a global symbol defined in a discarded section. */
10250 if (h->indx == -3)
10251 return true;
10252 }
10253
10254 /* We should also warn if a forced local symbol is referenced from
10255 shared libraries. */
10256 if (bfd_link_executable (flinfo->info)
10257 && h->forced_local
10258 && h->ref_dynamic
10259 && h->def_regular
10260 && !h->dynamic_def
10261 && h->ref_dynamic_nonweak
10262 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10263 {
10264 bfd *def_bfd;
10265 const char *msg;
10266 struct elf_link_hash_entry *hi = h;
10267
10268 /* Check indirect symbol. */
10269 while (hi->root.type == bfd_link_hash_indirect)
10270 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10271
10272 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10273 /* xgettext:c-format */
10274 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10275 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10276 /* xgettext:c-format */
10277 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10278 else
10279 /* xgettext:c-format */
10280 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10281 def_bfd = flinfo->output_bfd;
10282 if (hi->root.u.def.section != bfd_abs_section_ptr)
10283 def_bfd = hi->root.u.def.section->owner;
10284 _bfd_error_handler (msg, flinfo->output_bfd,
10285 h->root.root.string, def_bfd);
10286 bfd_set_error (bfd_error_bad_value);
10287 eoinfo->failed = true;
10288 return false;
10289 }
10290
10291 /* We don't want to output symbols that have never been mentioned by
10292 a regular file, or that we have been told to strip. However, if
10293 h->indx is set to -2, the symbol is used by a reloc and we must
10294 output it. */
10295 strip = false;
10296 if (h->indx == -2)
10297 ;
10298 else if ((h->def_dynamic
10299 || h->ref_dynamic
10300 || h->root.type == bfd_link_hash_new)
10301 && !h->def_regular
10302 && !h->ref_regular)
10303 strip = true;
10304 else if (flinfo->info->strip == strip_all)
10305 strip = true;
10306 else if (flinfo->info->strip == strip_some
10307 && bfd_hash_lookup (flinfo->info->keep_hash,
10308 h->root.root.string, false, false) == NULL)
10309 strip = true;
10310 else if ((h->root.type == bfd_link_hash_defined
10311 || h->root.type == bfd_link_hash_defweak)
10312 && ((flinfo->info->strip_discarded
10313 && discarded_section (h->root.u.def.section))
10314 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10315 && h->root.u.def.section->owner != NULL
10316 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10317 strip = true;
10318 else if ((h->root.type == bfd_link_hash_undefined
10319 || h->root.type == bfd_link_hash_undefweak)
10320 && h->root.u.undef.abfd != NULL
10321 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10322 strip = true;
10323
10324 type = h->type;
10325
10326 /* If we're stripping it, and it's not a dynamic symbol, there's
10327 nothing else to do. However, if it is a forced local symbol or
10328 an ifunc symbol we need to give the backend finish_dynamic_symbol
10329 function a chance to make it dynamic. */
10330 if (strip
10331 && h->dynindx == -1
10332 && type != STT_GNU_IFUNC
10333 && !h->forced_local)
10334 return true;
10335
10336 sym.st_value = 0;
10337 sym.st_size = h->size;
10338 sym.st_other = h->other;
10339 switch (h->root.type)
10340 {
10341 default:
10342 case bfd_link_hash_new:
10343 case bfd_link_hash_warning:
10344 abort ();
10345 return false;
10346
10347 case bfd_link_hash_undefined:
10348 case bfd_link_hash_undefweak:
10349 input_sec = bfd_und_section_ptr;
10350 sym.st_shndx = SHN_UNDEF;
10351 break;
10352
10353 case bfd_link_hash_defined:
10354 case bfd_link_hash_defweak:
10355 {
10356 input_sec = h->root.u.def.section;
10357 if (input_sec->output_section != NULL)
10358 {
10359 sym.st_shndx =
10360 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10361 input_sec->output_section);
10362 if (sym.st_shndx == SHN_BAD)
10363 {
10364 _bfd_error_handler
10365 /* xgettext:c-format */
10366 (_("%pB: could not find output section %pA for input section %pA"),
10367 flinfo->output_bfd, input_sec->output_section, input_sec);
10368 bfd_set_error (bfd_error_nonrepresentable_section);
10369 eoinfo->failed = true;
10370 return false;
10371 }
10372
10373 /* ELF symbols in relocatable files are section relative,
10374 but in nonrelocatable files they are virtual
10375 addresses. */
10376 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10377 if (!bfd_link_relocatable (flinfo->info))
10378 {
10379 sym.st_value += input_sec->output_section->vma;
10380 if (h->type == STT_TLS)
10381 {
10382 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10383 if (tls_sec != NULL)
10384 sym.st_value -= tls_sec->vma;
10385 }
10386 }
10387 }
10388 else
10389 {
10390 BFD_ASSERT (input_sec->owner == NULL
10391 || (input_sec->owner->flags & DYNAMIC) != 0);
10392 sym.st_shndx = SHN_UNDEF;
10393 input_sec = bfd_und_section_ptr;
10394 }
10395 }
10396 break;
10397
10398 case bfd_link_hash_common:
10399 input_sec = h->root.u.c.p->section;
10400 sym.st_shndx = bed->common_section_index (input_sec);
10401 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10402 break;
10403
10404 case bfd_link_hash_indirect:
10405 /* These symbols are created by symbol versioning. They point
10406 to the decorated version of the name. For example, if the
10407 symbol foo@@GNU_1.2 is the default, which should be used when
10408 foo is used with no version, then we add an indirect symbol
10409 foo which points to foo@@GNU_1.2. We ignore these symbols,
10410 since the indirected symbol is already in the hash table. */
10411 return true;
10412 }
10413
10414 if (type == STT_COMMON || type == STT_OBJECT)
10415 switch (h->root.type)
10416 {
10417 case bfd_link_hash_common:
10418 type = elf_link_convert_common_type (flinfo->info, type);
10419 break;
10420 case bfd_link_hash_defined:
10421 case bfd_link_hash_defweak:
10422 if (bed->common_definition (&sym))
10423 type = elf_link_convert_common_type (flinfo->info, type);
10424 else
10425 type = STT_OBJECT;
10426 break;
10427 case bfd_link_hash_undefined:
10428 case bfd_link_hash_undefweak:
10429 break;
10430 default:
10431 abort ();
10432 }
10433
10434 if (h->forced_local)
10435 {
10436 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10437 /* Turn off visibility on local symbol. */
10438 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10439 }
10440 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10441 else if (h->unique_global && h->def_regular)
10442 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10443 else if (h->root.type == bfd_link_hash_undefweak
10444 || h->root.type == bfd_link_hash_defweak)
10445 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10446 else
10447 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10448 sym.st_target_internal = h->target_internal;
10449
10450 /* Give the processor backend a chance to tweak the symbol value,
10451 and also to finish up anything that needs to be done for this
10452 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10453 forced local syms when non-shared is due to a historical quirk.
10454 STT_GNU_IFUNC symbol must go through PLT. */
10455 if ((h->type == STT_GNU_IFUNC
10456 && h->def_regular
10457 && !bfd_link_relocatable (flinfo->info))
10458 || ((h->dynindx != -1
10459 || h->forced_local)
10460 && ((bfd_link_pic (flinfo->info)
10461 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10462 || h->root.type != bfd_link_hash_undefweak))
10463 || !h->forced_local)
10464 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10465 {
10466 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10467 (flinfo->output_bfd, flinfo->info, h, &sym)))
10468 {
10469 eoinfo->failed = true;
10470 return false;
10471 }
10472 }
10473
10474 /* If we are marking the symbol as undefined, and there are no
10475 non-weak references to this symbol from a regular object, then
10476 mark the symbol as weak undefined; if there are non-weak
10477 references, mark the symbol as strong. We can't do this earlier,
10478 because it might not be marked as undefined until the
10479 finish_dynamic_symbol routine gets through with it. */
10480 if (sym.st_shndx == SHN_UNDEF
10481 && h->ref_regular
10482 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10483 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10484 {
10485 int bindtype;
10486 type = ELF_ST_TYPE (sym.st_info);
10487
10488 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10489 if (type == STT_GNU_IFUNC)
10490 type = STT_FUNC;
10491
10492 if (h->ref_regular_nonweak)
10493 bindtype = STB_GLOBAL;
10494 else
10495 bindtype = STB_WEAK;
10496 sym.st_info = ELF_ST_INFO (bindtype, type);
10497 }
10498
10499 /* If this is a symbol defined in a dynamic library, don't use the
10500 symbol size from the dynamic library. Relinking an executable
10501 against a new library may introduce gratuitous changes in the
10502 executable's symbols if we keep the size. */
10503 if (sym.st_shndx == SHN_UNDEF
10504 && !h->def_regular
10505 && h->def_dynamic)
10506 sym.st_size = 0;
10507
10508 /* If a non-weak symbol with non-default visibility is not defined
10509 locally, it is a fatal error. */
10510 if (!bfd_link_relocatable (flinfo->info)
10511 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10512 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10513 && h->root.type == bfd_link_hash_undefined
10514 && !h->def_regular)
10515 {
10516 const char *msg;
10517
10518 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10519 /* xgettext:c-format */
10520 msg = _("%pB: protected symbol `%s' isn't defined");
10521 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10522 /* xgettext:c-format */
10523 msg = _("%pB: internal symbol `%s' isn't defined");
10524 else
10525 /* xgettext:c-format */
10526 msg = _("%pB: hidden symbol `%s' isn't defined");
10527 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10528 bfd_set_error (bfd_error_bad_value);
10529 eoinfo->failed = true;
10530 return false;
10531 }
10532
10533 /* If this symbol should be put in the .dynsym section, then put it
10534 there now. We already know the symbol index. We also fill in
10535 the entry in the .hash section. */
10536 if (h->dynindx != -1
10537 && elf_hash_table (flinfo->info)->dynamic_sections_created
10538 && elf_hash_table (flinfo->info)->dynsym != NULL
10539 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10540 {
10541 bfd_byte *esym;
10542
10543 /* Since there is no version information in the dynamic string,
10544 if there is no version info in symbol version section, we will
10545 have a run-time problem if not linking executable, referenced
10546 by shared library, or not bound locally. */
10547 if (h->verinfo.verdef == NULL
10548 && (!bfd_link_executable (flinfo->info)
10549 || h->ref_dynamic
10550 || !h->def_regular))
10551 {
10552 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10553
10554 if (p && p [1] != '\0')
10555 {
10556 _bfd_error_handler
10557 /* xgettext:c-format */
10558 (_("%pB: no symbol version section for versioned symbol `%s'"),
10559 flinfo->output_bfd, h->root.root.string);
10560 eoinfo->failed = true;
10561 return false;
10562 }
10563 }
10564
10565 sym.st_name = h->dynstr_index;
10566 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10567 + h->dynindx * bed->s->sizeof_sym);
10568 if (!check_dynsym (flinfo->output_bfd, &sym))
10569 {
10570 eoinfo->failed = true;
10571 return false;
10572 }
10573
10574 /* Inform the linker of the addition of this symbol. */
10575
10576 if (flinfo->info->callbacks->ctf_new_dynsym)
10577 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10578
10579 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10580
10581 if (flinfo->hash_sec != NULL)
10582 {
10583 size_t hash_entry_size;
10584 bfd_byte *bucketpos;
10585 bfd_vma chain;
10586 size_t bucketcount;
10587 size_t bucket;
10588
10589 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10590 bucket = h->u.elf_hash_value % bucketcount;
10591
10592 hash_entry_size
10593 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10594 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10595 + (bucket + 2) * hash_entry_size);
10596 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10597 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10598 bucketpos);
10599 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10600 ((bfd_byte *) flinfo->hash_sec->contents
10601 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10602 }
10603
10604 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10605 {
10606 Elf_Internal_Versym iversym;
10607 Elf_External_Versym *eversym;
10608
10609 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10610 {
10611 if (h->verinfo.verdef == NULL
10612 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10613 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10614 iversym.vs_vers = 1;
10615 else
10616 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10617 }
10618 else
10619 {
10620 if (h->verinfo.vertree == NULL)
10621 iversym.vs_vers = 1;
10622 else
10623 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10624 if (flinfo->info->create_default_symver)
10625 iversym.vs_vers++;
10626 }
10627
10628 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10629 defined locally. */
10630 if (h->versioned == versioned_hidden && h->def_regular)
10631 iversym.vs_vers |= VERSYM_HIDDEN;
10632
10633 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10634 eversym += h->dynindx;
10635 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10636 }
10637 }
10638
10639 /* If the symbol is undefined, and we didn't output it to .dynsym,
10640 strip it from .symtab too. Obviously we can't do this for
10641 relocatable output or when needed for --emit-relocs. */
10642 else if (input_sec == bfd_und_section_ptr
10643 && h->indx != -2
10644 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10645 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10646 && !bfd_link_relocatable (flinfo->info))
10647 return true;
10648
10649 /* Also strip others that we couldn't earlier due to dynamic symbol
10650 processing. */
10651 if (strip)
10652 return true;
10653 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10654 return true;
10655
10656 /* Output a FILE symbol so that following locals are not associated
10657 with the wrong input file. We need one for forced local symbols
10658 if we've seen more than one FILE symbol or when we have exactly
10659 one FILE symbol but global symbols are present in a file other
10660 than the one with the FILE symbol. We also need one if linker
10661 defined symbols are present. In practice these conditions are
10662 always met, so just emit the FILE symbol unconditionally. */
10663 if (eoinfo->localsyms
10664 && !eoinfo->file_sym_done
10665 && eoinfo->flinfo->filesym_count != 0)
10666 {
10667 Elf_Internal_Sym fsym;
10668
10669 memset (&fsym, 0, sizeof (fsym));
10670 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10671 fsym.st_shndx = SHN_ABS;
10672 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10673 bfd_und_section_ptr, NULL))
10674 return false;
10675
10676 eoinfo->file_sym_done = true;
10677 }
10678
10679 indx = bfd_get_symcount (flinfo->output_bfd);
10680 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10681 input_sec, h);
10682 if (ret == 0)
10683 {
10684 eoinfo->failed = true;
10685 return false;
10686 }
10687 else if (ret == 1)
10688 h->indx = indx;
10689 else if (h->indx == -2)
10690 abort();
10691
10692 return true;
10693 }
10694
10695 /* Return TRUE if special handling is done for relocs in SEC against
10696 symbols defined in discarded sections. */
10697
10698 static bool
10699 elf_section_ignore_discarded_relocs (asection *sec)
10700 {
10701 const struct elf_backend_data *bed;
10702
10703 switch (sec->sec_info_type)
10704 {
10705 case SEC_INFO_TYPE_STABS:
10706 case SEC_INFO_TYPE_EH_FRAME:
10707 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10708 return true;
10709 default:
10710 break;
10711 }
10712
10713 bed = get_elf_backend_data (sec->owner);
10714 if (bed->elf_backend_ignore_discarded_relocs != NULL
10715 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10716 return true;
10717
10718 return false;
10719 }
10720
10721 /* Return a mask saying how ld should treat relocations in SEC against
10722 symbols defined in discarded sections. If this function returns
10723 COMPLAIN set, ld will issue a warning message. If this function
10724 returns PRETEND set, and the discarded section was link-once and the
10725 same size as the kept link-once section, ld will pretend that the
10726 symbol was actually defined in the kept section. Otherwise ld will
10727 zero the reloc (at least that is the intent, but some cooperation by
10728 the target dependent code is needed, particularly for REL targets). */
10729
10730 unsigned int
10731 _bfd_elf_default_action_discarded (asection *sec)
10732 {
10733 if (sec->flags & SEC_DEBUGGING)
10734 return PRETEND;
10735
10736 if (strcmp (".eh_frame", sec->name) == 0)
10737 return 0;
10738
10739 if (strcmp (".gcc_except_table", sec->name) == 0)
10740 return 0;
10741
10742 return COMPLAIN | PRETEND;
10743 }
10744
10745 /* Find a match between a section and a member of a section group. */
10746
10747 static asection *
10748 match_group_member (asection *sec, asection *group,
10749 struct bfd_link_info *info)
10750 {
10751 asection *first = elf_next_in_group (group);
10752 asection *s = first;
10753
10754 while (s != NULL)
10755 {
10756 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10757 return s;
10758
10759 s = elf_next_in_group (s);
10760 if (s == first)
10761 break;
10762 }
10763
10764 return NULL;
10765 }
10766
10767 /* Check if the kept section of a discarded section SEC can be used
10768 to replace it. Return the replacement if it is OK. Otherwise return
10769 NULL. */
10770
10771 asection *
10772 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10773 {
10774 asection *kept;
10775
10776 kept = sec->kept_section;
10777 if (kept != NULL)
10778 {
10779 if ((kept->flags & SEC_GROUP) != 0)
10780 kept = match_group_member (sec, kept, info);
10781 if (kept != NULL)
10782 {
10783 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10784 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10785 kept = NULL;
10786 else
10787 {
10788 /* Get the real kept section. */
10789 asection *next;
10790 for (next = kept->kept_section;
10791 next != NULL;
10792 next = next->kept_section)
10793 kept = next;
10794 }
10795 }
10796 sec->kept_section = kept;
10797 }
10798 return kept;
10799 }
10800
10801 /* Link an input file into the linker output file. This function
10802 handles all the sections and relocations of the input file at once.
10803 This is so that we only have to read the local symbols once, and
10804 don't have to keep them in memory. */
10805
10806 static bool
10807 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10808 {
10809 int (*relocate_section)
10810 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10811 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10812 bfd *output_bfd;
10813 Elf_Internal_Shdr *symtab_hdr;
10814 size_t locsymcount;
10815 size_t extsymoff;
10816 Elf_Internal_Sym *isymbuf;
10817 Elf_Internal_Sym *isym;
10818 Elf_Internal_Sym *isymend;
10819 long *pindex;
10820 asection **ppsection;
10821 asection *o;
10822 const struct elf_backend_data *bed;
10823 struct elf_link_hash_entry **sym_hashes;
10824 bfd_size_type address_size;
10825 bfd_vma r_type_mask;
10826 int r_sym_shift;
10827 bool have_file_sym = false;
10828
10829 output_bfd = flinfo->output_bfd;
10830 bed = get_elf_backend_data (output_bfd);
10831 relocate_section = bed->elf_backend_relocate_section;
10832
10833 /* If this is a dynamic object, we don't want to do anything here:
10834 we don't want the local symbols, and we don't want the section
10835 contents. */
10836 if ((input_bfd->flags & DYNAMIC) != 0)
10837 return true;
10838
10839 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10840 if (elf_bad_symtab (input_bfd))
10841 {
10842 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10843 extsymoff = 0;
10844 }
10845 else
10846 {
10847 locsymcount = symtab_hdr->sh_info;
10848 extsymoff = symtab_hdr->sh_info;
10849 }
10850
10851 /* Enable GNU OSABI features in the output BFD that are used in the input
10852 BFD. */
10853 if (bed->elf_osabi == ELFOSABI_NONE
10854 || bed->elf_osabi == ELFOSABI_GNU
10855 || bed->elf_osabi == ELFOSABI_FREEBSD)
10856 elf_tdata (output_bfd)->has_gnu_osabi
10857 |= (elf_tdata (input_bfd)->has_gnu_osabi
10858 & (bfd_link_relocatable (flinfo->info)
10859 ? -1 : ~elf_gnu_osabi_retain));
10860
10861 /* Read the local symbols. */
10862 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10863 if (isymbuf == NULL && locsymcount != 0)
10864 {
10865 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10866 flinfo->internal_syms,
10867 flinfo->external_syms,
10868 flinfo->locsym_shndx);
10869 if (isymbuf == NULL)
10870 return false;
10871 }
10872
10873 /* Find local symbol sections and adjust values of symbols in
10874 SEC_MERGE sections. Write out those local symbols we know are
10875 going into the output file. */
10876 isymend = isymbuf + locsymcount;
10877 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10878 isym < isymend;
10879 isym++, pindex++, ppsection++)
10880 {
10881 asection *isec;
10882 const char *name;
10883 Elf_Internal_Sym osym;
10884 long indx;
10885 int ret;
10886
10887 *pindex = -1;
10888
10889 if (elf_bad_symtab (input_bfd))
10890 {
10891 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10892 {
10893 *ppsection = NULL;
10894 continue;
10895 }
10896 }
10897
10898 if (isym->st_shndx == SHN_UNDEF)
10899 isec = bfd_und_section_ptr;
10900 else if (isym->st_shndx == SHN_ABS)
10901 isec = bfd_abs_section_ptr;
10902 else if (isym->st_shndx == SHN_COMMON)
10903 isec = bfd_com_section_ptr;
10904 else
10905 {
10906 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10907 if (isec == NULL)
10908 {
10909 /* Don't attempt to output symbols with st_shnx in the
10910 reserved range other than SHN_ABS and SHN_COMMON. */
10911 isec = bfd_und_section_ptr;
10912 }
10913 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10914 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10915 isym->st_value =
10916 _bfd_merged_section_offset (output_bfd, &isec,
10917 elf_section_data (isec)->sec_info,
10918 isym->st_value);
10919 }
10920
10921 *ppsection = isec;
10922
10923 /* Don't output the first, undefined, symbol. In fact, don't
10924 output any undefined local symbol. */
10925 if (isec == bfd_und_section_ptr)
10926 continue;
10927
10928 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10929 {
10930 /* We never output section symbols. Instead, we use the
10931 section symbol of the corresponding section in the output
10932 file. */
10933 continue;
10934 }
10935
10936 /* If we are stripping all symbols, we don't want to output this
10937 one. */
10938 if (flinfo->info->strip == strip_all)
10939 continue;
10940
10941 /* If we are discarding all local symbols, we don't want to
10942 output this one. If we are generating a relocatable output
10943 file, then some of the local symbols may be required by
10944 relocs; we output them below as we discover that they are
10945 needed. */
10946 if (flinfo->info->discard == discard_all)
10947 continue;
10948
10949 /* If this symbol is defined in a section which we are
10950 discarding, we don't need to keep it. */
10951 if (isym->st_shndx != SHN_UNDEF
10952 && isym->st_shndx < SHN_LORESERVE
10953 && isec->output_section == NULL
10954 && flinfo->info->non_contiguous_regions
10955 && flinfo->info->non_contiguous_regions_warnings)
10956 {
10957 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10958 "discards section `%s' from '%s'\n"),
10959 isec->name, bfd_get_filename (isec->owner));
10960 continue;
10961 }
10962
10963 if (isym->st_shndx != SHN_UNDEF
10964 && isym->st_shndx < SHN_LORESERVE
10965 && bfd_section_removed_from_list (output_bfd,
10966 isec->output_section))
10967 continue;
10968
10969 /* Get the name of the symbol. */
10970 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10971 isym->st_name);
10972 if (name == NULL)
10973 return false;
10974
10975 /* See if we are discarding symbols with this name. */
10976 if ((flinfo->info->strip == strip_some
10977 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
10978 == NULL))
10979 || (((flinfo->info->discard == discard_sec_merge
10980 && (isec->flags & SEC_MERGE)
10981 && !bfd_link_relocatable (flinfo->info))
10982 || flinfo->info->discard == discard_l)
10983 && bfd_is_local_label_name (input_bfd, name)))
10984 continue;
10985
10986 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10987 {
10988 if (input_bfd->lto_output)
10989 /* -flto puts a temp file name here. This means builds
10990 are not reproducible. Discard the symbol. */
10991 continue;
10992 have_file_sym = true;
10993 flinfo->filesym_count += 1;
10994 }
10995 if (!have_file_sym)
10996 {
10997 /* In the absence of debug info, bfd_find_nearest_line uses
10998 FILE symbols to determine the source file for local
10999 function symbols. Provide a FILE symbol here if input
11000 files lack such, so that their symbols won't be
11001 associated with a previous input file. It's not the
11002 source file, but the best we can do. */
11003 const char *filename;
11004 have_file_sym = true;
11005 flinfo->filesym_count += 1;
11006 memset (&osym, 0, sizeof (osym));
11007 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11008 osym.st_shndx = SHN_ABS;
11009 if (input_bfd->lto_output)
11010 filename = NULL;
11011 else
11012 filename = lbasename (bfd_get_filename (input_bfd));
11013 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11014 bfd_abs_section_ptr, NULL))
11015 return false;
11016 }
11017
11018 osym = *isym;
11019
11020 /* Adjust the section index for the output file. */
11021 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11022 isec->output_section);
11023 if (osym.st_shndx == SHN_BAD)
11024 return false;
11025
11026 /* ELF symbols in relocatable files are section relative, but
11027 in executable files they are virtual addresses. Note that
11028 this code assumes that all ELF sections have an associated
11029 BFD section with a reasonable value for output_offset; below
11030 we assume that they also have a reasonable value for
11031 output_section. Any special sections must be set up to meet
11032 these requirements. */
11033 osym.st_value += isec->output_offset;
11034 if (!bfd_link_relocatable (flinfo->info))
11035 {
11036 osym.st_value += isec->output_section->vma;
11037 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11038 {
11039 /* STT_TLS symbols are relative to PT_TLS segment base. */
11040 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11041 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11042 else
11043 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11044 STT_NOTYPE);
11045 }
11046 }
11047
11048 indx = bfd_get_symcount (output_bfd);
11049 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11050 if (ret == 0)
11051 return false;
11052 else if (ret == 1)
11053 *pindex = indx;
11054 }
11055
11056 if (bed->s->arch_size == 32)
11057 {
11058 r_type_mask = 0xff;
11059 r_sym_shift = 8;
11060 address_size = 4;
11061 }
11062 else
11063 {
11064 r_type_mask = 0xffffffff;
11065 r_sym_shift = 32;
11066 address_size = 8;
11067 }
11068
11069 /* Relocate the contents of each section. */
11070 sym_hashes = elf_sym_hashes (input_bfd);
11071 for (o = input_bfd->sections; o != NULL; o = o->next)
11072 {
11073 bfd_byte *contents;
11074
11075 if (! o->linker_mark)
11076 {
11077 /* This section was omitted from the link. */
11078 continue;
11079 }
11080
11081 if (!flinfo->info->resolve_section_groups
11082 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11083 {
11084 /* Deal with the group signature symbol. */
11085 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11086 unsigned long symndx = sec_data->this_hdr.sh_info;
11087 asection *osec = o->output_section;
11088
11089 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11090 if (symndx >= locsymcount
11091 || (elf_bad_symtab (input_bfd)
11092 && flinfo->sections[symndx] == NULL))
11093 {
11094 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11095 while (h->root.type == bfd_link_hash_indirect
11096 || h->root.type == bfd_link_hash_warning)
11097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11098 /* Arrange for symbol to be output. */
11099 h->indx = -2;
11100 elf_section_data (osec)->this_hdr.sh_info = -2;
11101 }
11102 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11103 {
11104 /* We'll use the output section target_index. */
11105 asection *sec = flinfo->sections[symndx]->output_section;
11106 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11107 }
11108 else
11109 {
11110 if (flinfo->indices[symndx] == -1)
11111 {
11112 /* Otherwise output the local symbol now. */
11113 Elf_Internal_Sym sym = isymbuf[symndx];
11114 asection *sec = flinfo->sections[symndx]->output_section;
11115 const char *name;
11116 long indx;
11117 int ret;
11118
11119 name = bfd_elf_string_from_elf_section (input_bfd,
11120 symtab_hdr->sh_link,
11121 sym.st_name);
11122 if (name == NULL)
11123 return false;
11124
11125 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11126 sec);
11127 if (sym.st_shndx == SHN_BAD)
11128 return false;
11129
11130 sym.st_value += o->output_offset;
11131
11132 indx = bfd_get_symcount (output_bfd);
11133 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11134 NULL);
11135 if (ret == 0)
11136 return false;
11137 else if (ret == 1)
11138 flinfo->indices[symndx] = indx;
11139 else
11140 abort ();
11141 }
11142 elf_section_data (osec)->this_hdr.sh_info
11143 = flinfo->indices[symndx];
11144 }
11145 }
11146
11147 if ((o->flags & SEC_HAS_CONTENTS) == 0
11148 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11149 continue;
11150
11151 if ((o->flags & SEC_LINKER_CREATED) != 0)
11152 {
11153 /* Section was created by _bfd_elf_link_create_dynamic_sections
11154 or somesuch. */
11155 continue;
11156 }
11157
11158 /* Get the contents of the section. They have been cached by a
11159 relaxation routine. Note that o is a section in an input
11160 file, so the contents field will not have been set by any of
11161 the routines which work on output files. */
11162 if (elf_section_data (o)->this_hdr.contents != NULL)
11163 {
11164 contents = elf_section_data (o)->this_hdr.contents;
11165 if (bed->caches_rawsize
11166 && o->rawsize != 0
11167 && o->rawsize < o->size)
11168 {
11169 memcpy (flinfo->contents, contents, o->rawsize);
11170 contents = flinfo->contents;
11171 }
11172 }
11173 else
11174 {
11175 contents = flinfo->contents;
11176 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11177 return false;
11178 }
11179
11180 if ((o->flags & SEC_RELOC) != 0)
11181 {
11182 Elf_Internal_Rela *internal_relocs;
11183 Elf_Internal_Rela *rel, *relend;
11184 int action_discarded;
11185 int ret;
11186
11187 /* Get the swapped relocs. */
11188 internal_relocs
11189 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11190 flinfo->internal_relocs, false);
11191 if (internal_relocs == NULL
11192 && o->reloc_count > 0)
11193 return false;
11194
11195 /* We need to reverse-copy input .ctors/.dtors sections if
11196 they are placed in .init_array/.finit_array for output. */
11197 if (o->size > address_size
11198 && ((startswith (o->name, ".ctors")
11199 && strcmp (o->output_section->name,
11200 ".init_array") == 0)
11201 || (startswith (o->name, ".dtors")
11202 && strcmp (o->output_section->name,
11203 ".fini_array") == 0))
11204 && (o->name[6] == 0 || o->name[6] == '.'))
11205 {
11206 if (o->size * bed->s->int_rels_per_ext_rel
11207 != o->reloc_count * address_size)
11208 {
11209 _bfd_error_handler
11210 /* xgettext:c-format */
11211 (_("error: %pB: size of section %pA is not "
11212 "multiple of address size"),
11213 input_bfd, o);
11214 bfd_set_error (bfd_error_bad_value);
11215 return false;
11216 }
11217 o->flags |= SEC_ELF_REVERSE_COPY;
11218 }
11219
11220 action_discarded = -1;
11221 if (!elf_section_ignore_discarded_relocs (o))
11222 action_discarded = (*bed->action_discarded) (o);
11223
11224 /* Run through the relocs evaluating complex reloc symbols and
11225 looking for relocs against symbols from discarded sections
11226 or section symbols from removed link-once sections.
11227 Complain about relocs against discarded sections. Zero
11228 relocs against removed link-once sections. */
11229
11230 rel = internal_relocs;
11231 relend = rel + o->reloc_count;
11232 for ( ; rel < relend; rel++)
11233 {
11234 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11235 unsigned int s_type;
11236 asection **ps, *sec;
11237 struct elf_link_hash_entry *h = NULL;
11238 const char *sym_name;
11239
11240 if (r_symndx == STN_UNDEF)
11241 continue;
11242
11243 if (r_symndx >= locsymcount
11244 || (elf_bad_symtab (input_bfd)
11245 && flinfo->sections[r_symndx] == NULL))
11246 {
11247 h = sym_hashes[r_symndx - extsymoff];
11248
11249 /* Badly formatted input files can contain relocs that
11250 reference non-existant symbols. Check here so that
11251 we do not seg fault. */
11252 if (h == NULL)
11253 {
11254 _bfd_error_handler
11255 /* xgettext:c-format */
11256 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11257 "that references a non-existent global symbol"),
11258 input_bfd, (uint64_t) rel->r_info, o);
11259 bfd_set_error (bfd_error_bad_value);
11260 return false;
11261 }
11262
11263 while (h->root.type == bfd_link_hash_indirect
11264 || h->root.type == bfd_link_hash_warning)
11265 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11266
11267 s_type = h->type;
11268
11269 /* If a plugin symbol is referenced from a non-IR file,
11270 mark the symbol as undefined. Note that the
11271 linker may attach linker created dynamic sections
11272 to the plugin bfd. Symbols defined in linker
11273 created sections are not plugin symbols. */
11274 if ((h->root.non_ir_ref_regular
11275 || h->root.non_ir_ref_dynamic)
11276 && (h->root.type == bfd_link_hash_defined
11277 || h->root.type == bfd_link_hash_defweak)
11278 && (h->root.u.def.section->flags
11279 & SEC_LINKER_CREATED) == 0
11280 && h->root.u.def.section->owner != NULL
11281 && (h->root.u.def.section->owner->flags
11282 & BFD_PLUGIN) != 0)
11283 {
11284 h->root.type = bfd_link_hash_undefined;
11285 h->root.u.undef.abfd = h->root.u.def.section->owner;
11286 }
11287
11288 ps = NULL;
11289 if (h->root.type == bfd_link_hash_defined
11290 || h->root.type == bfd_link_hash_defweak)
11291 ps = &h->root.u.def.section;
11292
11293 sym_name = h->root.root.string;
11294 }
11295 else
11296 {
11297 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11298
11299 s_type = ELF_ST_TYPE (sym->st_info);
11300 ps = &flinfo->sections[r_symndx];
11301 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11302 sym, *ps);
11303 }
11304
11305 if ((s_type == STT_RELC || s_type == STT_SRELC)
11306 && !bfd_link_relocatable (flinfo->info))
11307 {
11308 bfd_vma val;
11309 bfd_vma dot = (rel->r_offset
11310 + o->output_offset + o->output_section->vma);
11311 #ifdef DEBUG
11312 printf ("Encountered a complex symbol!");
11313 printf (" (input_bfd %s, section %s, reloc %ld\n",
11314 bfd_get_filename (input_bfd), o->name,
11315 (long) (rel - internal_relocs));
11316 printf (" symbol: idx %8.8lx, name %s\n",
11317 r_symndx, sym_name);
11318 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11319 (unsigned long) rel->r_info,
11320 (unsigned long) rel->r_offset);
11321 #endif
11322 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11323 isymbuf, locsymcount, s_type == STT_SRELC))
11324 return false;
11325
11326 /* Symbol evaluated OK. Update to absolute value. */
11327 set_symbol_value (input_bfd, isymbuf, locsymcount,
11328 r_symndx, val);
11329 continue;
11330 }
11331
11332 if (action_discarded != -1 && ps != NULL)
11333 {
11334 /* Complain if the definition comes from a
11335 discarded section. */
11336 if ((sec = *ps) != NULL && discarded_section (sec))
11337 {
11338 BFD_ASSERT (r_symndx != STN_UNDEF);
11339 if (action_discarded & COMPLAIN)
11340 (*flinfo->info->callbacks->einfo)
11341 /* xgettext:c-format */
11342 (_("%X`%s' referenced in section `%pA' of %pB: "
11343 "defined in discarded section `%pA' of %pB\n"),
11344 sym_name, o, input_bfd, sec, sec->owner);
11345
11346 /* Try to do the best we can to support buggy old
11347 versions of gcc. Pretend that the symbol is
11348 really defined in the kept linkonce section.
11349 FIXME: This is quite broken. Modifying the
11350 symbol here means we will be changing all later
11351 uses of the symbol, not just in this section. */
11352 if (action_discarded & PRETEND)
11353 {
11354 asection *kept;
11355
11356 kept = _bfd_elf_check_kept_section (sec,
11357 flinfo->info);
11358 if (kept != NULL)
11359 {
11360 *ps = kept;
11361 continue;
11362 }
11363 }
11364 }
11365 }
11366 }
11367
11368 /* Relocate the section by invoking a back end routine.
11369
11370 The back end routine is responsible for adjusting the
11371 section contents as necessary, and (if using Rela relocs
11372 and generating a relocatable output file) adjusting the
11373 reloc addend as necessary.
11374
11375 The back end routine does not have to worry about setting
11376 the reloc address or the reloc symbol index.
11377
11378 The back end routine is given a pointer to the swapped in
11379 internal symbols, and can access the hash table entries
11380 for the external symbols via elf_sym_hashes (input_bfd).
11381
11382 When generating relocatable output, the back end routine
11383 must handle STB_LOCAL/STT_SECTION symbols specially. The
11384 output symbol is going to be a section symbol
11385 corresponding to the output section, which will require
11386 the addend to be adjusted. */
11387
11388 ret = (*relocate_section) (output_bfd, flinfo->info,
11389 input_bfd, o, contents,
11390 internal_relocs,
11391 isymbuf,
11392 flinfo->sections);
11393 if (!ret)
11394 return false;
11395
11396 if (ret == 2
11397 || bfd_link_relocatable (flinfo->info)
11398 || flinfo->info->emitrelocations)
11399 {
11400 Elf_Internal_Rela *irela;
11401 Elf_Internal_Rela *irelaend, *irelamid;
11402 bfd_vma last_offset;
11403 struct elf_link_hash_entry **rel_hash;
11404 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11405 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11406 unsigned int next_erel;
11407 bool rela_normal;
11408 struct bfd_elf_section_data *esdi, *esdo;
11409
11410 esdi = elf_section_data (o);
11411 esdo = elf_section_data (o->output_section);
11412 rela_normal = false;
11413
11414 /* Adjust the reloc addresses and symbol indices. */
11415
11416 irela = internal_relocs;
11417 irelaend = irela + o->reloc_count;
11418 rel_hash = esdo->rel.hashes + esdo->rel.count;
11419 /* We start processing the REL relocs, if any. When we reach
11420 IRELAMID in the loop, we switch to the RELA relocs. */
11421 irelamid = irela;
11422 if (esdi->rel.hdr != NULL)
11423 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11424 * bed->s->int_rels_per_ext_rel);
11425 rel_hash_list = rel_hash;
11426 rela_hash_list = NULL;
11427 last_offset = o->output_offset;
11428 if (!bfd_link_relocatable (flinfo->info))
11429 last_offset += o->output_section->vma;
11430 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11431 {
11432 unsigned long r_symndx;
11433 asection *sec;
11434 Elf_Internal_Sym sym;
11435
11436 if (next_erel == bed->s->int_rels_per_ext_rel)
11437 {
11438 rel_hash++;
11439 next_erel = 0;
11440 }
11441
11442 if (irela == irelamid)
11443 {
11444 rel_hash = esdo->rela.hashes + esdo->rela.count;
11445 rela_hash_list = rel_hash;
11446 rela_normal = bed->rela_normal;
11447 }
11448
11449 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11450 flinfo->info, o,
11451 irela->r_offset);
11452 if (irela->r_offset >= (bfd_vma) -2)
11453 {
11454 /* This is a reloc for a deleted entry or somesuch.
11455 Turn it into an R_*_NONE reloc, at the same
11456 offset as the last reloc. elf_eh_frame.c and
11457 bfd_elf_discard_info rely on reloc offsets
11458 being ordered. */
11459 irela->r_offset = last_offset;
11460 irela->r_info = 0;
11461 irela->r_addend = 0;
11462 continue;
11463 }
11464
11465 irela->r_offset += o->output_offset;
11466
11467 /* Relocs in an executable have to be virtual addresses. */
11468 if (!bfd_link_relocatable (flinfo->info))
11469 irela->r_offset += o->output_section->vma;
11470
11471 last_offset = irela->r_offset;
11472
11473 r_symndx = irela->r_info >> r_sym_shift;
11474 if (r_symndx == STN_UNDEF)
11475 continue;
11476
11477 if (r_symndx >= locsymcount
11478 || (elf_bad_symtab (input_bfd)
11479 && flinfo->sections[r_symndx] == NULL))
11480 {
11481 struct elf_link_hash_entry *rh;
11482 unsigned long indx;
11483
11484 /* This is a reloc against a global symbol. We
11485 have not yet output all the local symbols, so
11486 we do not know the symbol index of any global
11487 symbol. We set the rel_hash entry for this
11488 reloc to point to the global hash table entry
11489 for this symbol. The symbol index is then
11490 set at the end of bfd_elf_final_link. */
11491 indx = r_symndx - extsymoff;
11492 rh = elf_sym_hashes (input_bfd)[indx];
11493 while (rh->root.type == bfd_link_hash_indirect
11494 || rh->root.type == bfd_link_hash_warning)
11495 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11496
11497 /* Setting the index to -2 tells
11498 elf_link_output_extsym that this symbol is
11499 used by a reloc. */
11500 BFD_ASSERT (rh->indx < 0);
11501 rh->indx = -2;
11502 *rel_hash = rh;
11503
11504 continue;
11505 }
11506
11507 /* This is a reloc against a local symbol. */
11508
11509 *rel_hash = NULL;
11510 sym = isymbuf[r_symndx];
11511 sec = flinfo->sections[r_symndx];
11512 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11513 {
11514 /* I suppose the backend ought to fill in the
11515 section of any STT_SECTION symbol against a
11516 processor specific section. */
11517 r_symndx = STN_UNDEF;
11518 if (bfd_is_abs_section (sec))
11519 ;
11520 else if (sec == NULL || sec->owner == NULL)
11521 {
11522 bfd_set_error (bfd_error_bad_value);
11523 return false;
11524 }
11525 else
11526 {
11527 asection *osec = sec->output_section;
11528
11529 /* If we have discarded a section, the output
11530 section will be the absolute section. In
11531 case of discarded SEC_MERGE sections, use
11532 the kept section. relocate_section should
11533 have already handled discarded linkonce
11534 sections. */
11535 if (bfd_is_abs_section (osec)
11536 && sec->kept_section != NULL
11537 && sec->kept_section->output_section != NULL)
11538 {
11539 osec = sec->kept_section->output_section;
11540 irela->r_addend -= osec->vma;
11541 }
11542
11543 if (!bfd_is_abs_section (osec))
11544 {
11545 r_symndx = osec->target_index;
11546 if (r_symndx == STN_UNDEF)
11547 {
11548 irela->r_addend += osec->vma;
11549 osec = _bfd_nearby_section (output_bfd, osec,
11550 osec->vma);
11551 irela->r_addend -= osec->vma;
11552 r_symndx = osec->target_index;
11553 }
11554 }
11555 }
11556
11557 /* Adjust the addend according to where the
11558 section winds up in the output section. */
11559 if (rela_normal)
11560 irela->r_addend += sec->output_offset;
11561 }
11562 else
11563 {
11564 if (flinfo->indices[r_symndx] == -1)
11565 {
11566 unsigned long shlink;
11567 const char *name;
11568 asection *osec;
11569 long indx;
11570
11571 if (flinfo->info->strip == strip_all)
11572 {
11573 /* You can't do ld -r -s. */
11574 bfd_set_error (bfd_error_invalid_operation);
11575 return false;
11576 }
11577
11578 /* This symbol was skipped earlier, but
11579 since it is needed by a reloc, we
11580 must output it now. */
11581 shlink = symtab_hdr->sh_link;
11582 name = (bfd_elf_string_from_elf_section
11583 (input_bfd, shlink, sym.st_name));
11584 if (name == NULL)
11585 return false;
11586
11587 osec = sec->output_section;
11588 sym.st_shndx =
11589 _bfd_elf_section_from_bfd_section (output_bfd,
11590 osec);
11591 if (sym.st_shndx == SHN_BAD)
11592 return false;
11593
11594 sym.st_value += sec->output_offset;
11595 if (!bfd_link_relocatable (flinfo->info))
11596 {
11597 sym.st_value += osec->vma;
11598 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11599 {
11600 struct elf_link_hash_table *htab
11601 = elf_hash_table (flinfo->info);
11602
11603 /* STT_TLS symbols are relative to PT_TLS
11604 segment base. */
11605 if (htab->tls_sec != NULL)
11606 sym.st_value -= htab->tls_sec->vma;
11607 else
11608 sym.st_info
11609 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11610 STT_NOTYPE);
11611 }
11612 }
11613
11614 indx = bfd_get_symcount (output_bfd);
11615 ret = elf_link_output_symstrtab (flinfo, name,
11616 &sym, sec,
11617 NULL);
11618 if (ret == 0)
11619 return false;
11620 else if (ret == 1)
11621 flinfo->indices[r_symndx] = indx;
11622 else
11623 abort ();
11624 }
11625
11626 r_symndx = flinfo->indices[r_symndx];
11627 }
11628
11629 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11630 | (irela->r_info & r_type_mask));
11631 }
11632
11633 /* Swap out the relocs. */
11634 input_rel_hdr = esdi->rel.hdr;
11635 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11636 {
11637 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11638 input_rel_hdr,
11639 internal_relocs,
11640 rel_hash_list))
11641 return false;
11642 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11643 * bed->s->int_rels_per_ext_rel);
11644 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11645 }
11646
11647 input_rela_hdr = esdi->rela.hdr;
11648 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11649 {
11650 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11651 input_rela_hdr,
11652 internal_relocs,
11653 rela_hash_list))
11654 return false;
11655 }
11656 }
11657 }
11658
11659 /* Write out the modified section contents. */
11660 if (bed->elf_backend_write_section
11661 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11662 contents))
11663 {
11664 /* Section written out. */
11665 }
11666 else switch (o->sec_info_type)
11667 {
11668 case SEC_INFO_TYPE_STABS:
11669 if (! (_bfd_write_section_stabs
11670 (output_bfd,
11671 &elf_hash_table (flinfo->info)->stab_info,
11672 o, &elf_section_data (o)->sec_info, contents)))
11673 return false;
11674 break;
11675 case SEC_INFO_TYPE_MERGE:
11676 if (! _bfd_write_merged_section (output_bfd, o,
11677 elf_section_data (o)->sec_info))
11678 return false;
11679 break;
11680 case SEC_INFO_TYPE_EH_FRAME:
11681 {
11682 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11683 o, contents))
11684 return false;
11685 }
11686 break;
11687 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11688 {
11689 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11690 flinfo->info,
11691 o, contents))
11692 return false;
11693 }
11694 break;
11695 default:
11696 {
11697 if (! (o->flags & SEC_EXCLUDE))
11698 {
11699 file_ptr offset = (file_ptr) o->output_offset;
11700 bfd_size_type todo = o->size;
11701
11702 offset *= bfd_octets_per_byte (output_bfd, o);
11703
11704 if ((o->flags & SEC_ELF_REVERSE_COPY))
11705 {
11706 /* Reverse-copy input section to output. */
11707 do
11708 {
11709 todo -= address_size;
11710 if (! bfd_set_section_contents (output_bfd,
11711 o->output_section,
11712 contents + todo,
11713 offset,
11714 address_size))
11715 return false;
11716 if (todo == 0)
11717 break;
11718 offset += address_size;
11719 }
11720 while (1);
11721 }
11722 else if (! bfd_set_section_contents (output_bfd,
11723 o->output_section,
11724 contents,
11725 offset, todo))
11726 return false;
11727 }
11728 }
11729 break;
11730 }
11731 }
11732
11733 return true;
11734 }
11735
11736 /* Generate a reloc when linking an ELF file. This is a reloc
11737 requested by the linker, and does not come from any input file. This
11738 is used to build constructor and destructor tables when linking
11739 with -Ur. */
11740
11741 static bool
11742 elf_reloc_link_order (bfd *output_bfd,
11743 struct bfd_link_info *info,
11744 asection *output_section,
11745 struct bfd_link_order *link_order)
11746 {
11747 reloc_howto_type *howto;
11748 long indx;
11749 bfd_vma offset;
11750 bfd_vma addend;
11751 struct bfd_elf_section_reloc_data *reldata;
11752 struct elf_link_hash_entry **rel_hash_ptr;
11753 Elf_Internal_Shdr *rel_hdr;
11754 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11755 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11756 bfd_byte *erel;
11757 unsigned int i;
11758 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11759
11760 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11761 if (howto == NULL)
11762 {
11763 bfd_set_error (bfd_error_bad_value);
11764 return false;
11765 }
11766
11767 addend = link_order->u.reloc.p->addend;
11768
11769 if (esdo->rel.hdr)
11770 reldata = &esdo->rel;
11771 else if (esdo->rela.hdr)
11772 reldata = &esdo->rela;
11773 else
11774 {
11775 reldata = NULL;
11776 BFD_ASSERT (0);
11777 }
11778
11779 /* Figure out the symbol index. */
11780 rel_hash_ptr = reldata->hashes + reldata->count;
11781 if (link_order->type == bfd_section_reloc_link_order)
11782 {
11783 indx = link_order->u.reloc.p->u.section->target_index;
11784 BFD_ASSERT (indx != 0);
11785 *rel_hash_ptr = NULL;
11786 }
11787 else
11788 {
11789 struct elf_link_hash_entry *h;
11790
11791 /* Treat a reloc against a defined symbol as though it were
11792 actually against the section. */
11793 h = ((struct elf_link_hash_entry *)
11794 bfd_wrapped_link_hash_lookup (output_bfd, info,
11795 link_order->u.reloc.p->u.name,
11796 false, false, true));
11797 if (h != NULL
11798 && (h->root.type == bfd_link_hash_defined
11799 || h->root.type == bfd_link_hash_defweak))
11800 {
11801 asection *section;
11802
11803 section = h->root.u.def.section;
11804 indx = section->output_section->target_index;
11805 *rel_hash_ptr = NULL;
11806 /* It seems that we ought to add the symbol value to the
11807 addend here, but in practice it has already been added
11808 because it was passed to constructor_callback. */
11809 addend += section->output_section->vma + section->output_offset;
11810 }
11811 else if (h != NULL)
11812 {
11813 /* Setting the index to -2 tells elf_link_output_extsym that
11814 this symbol is used by a reloc. */
11815 h->indx = -2;
11816 *rel_hash_ptr = h;
11817 indx = 0;
11818 }
11819 else
11820 {
11821 (*info->callbacks->unattached_reloc)
11822 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11823 indx = 0;
11824 }
11825 }
11826
11827 /* If this is an inplace reloc, we must write the addend into the
11828 object file. */
11829 if (howto->partial_inplace && addend != 0)
11830 {
11831 bfd_size_type size;
11832 bfd_reloc_status_type rstat;
11833 bfd_byte *buf;
11834 bool ok;
11835 const char *sym_name;
11836 bfd_size_type octets;
11837
11838 size = (bfd_size_type) bfd_get_reloc_size (howto);
11839 buf = (bfd_byte *) bfd_zmalloc (size);
11840 if (buf == NULL && size != 0)
11841 return false;
11842 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11843 switch (rstat)
11844 {
11845 case bfd_reloc_ok:
11846 break;
11847
11848 default:
11849 case bfd_reloc_outofrange:
11850 abort ();
11851
11852 case bfd_reloc_overflow:
11853 if (link_order->type == bfd_section_reloc_link_order)
11854 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11855 else
11856 sym_name = link_order->u.reloc.p->u.name;
11857 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11858 howto->name, addend, NULL, NULL,
11859 (bfd_vma) 0);
11860 break;
11861 }
11862
11863 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11864 output_section);
11865 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11866 octets, size);
11867 free (buf);
11868 if (! ok)
11869 return false;
11870 }
11871
11872 /* The address of a reloc is relative to the section in a
11873 relocatable file, and is a virtual address in an executable
11874 file. */
11875 offset = link_order->offset;
11876 if (! bfd_link_relocatable (info))
11877 offset += output_section->vma;
11878
11879 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11880 {
11881 irel[i].r_offset = offset;
11882 irel[i].r_info = 0;
11883 irel[i].r_addend = 0;
11884 }
11885 if (bed->s->arch_size == 32)
11886 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11887 else
11888 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11889
11890 rel_hdr = reldata->hdr;
11891 erel = rel_hdr->contents;
11892 if (rel_hdr->sh_type == SHT_REL)
11893 {
11894 erel += reldata->count * bed->s->sizeof_rel;
11895 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11896 }
11897 else
11898 {
11899 irel[0].r_addend = addend;
11900 erel += reldata->count * bed->s->sizeof_rela;
11901 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11902 }
11903
11904 ++reldata->count;
11905
11906 return true;
11907 }
11908
11909 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11910 Returns TRUE upon success, FALSE otherwise. */
11911
11912 static bool
11913 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11914 {
11915 bool ret = false;
11916 bfd *implib_bfd;
11917 const struct elf_backend_data *bed;
11918 flagword flags;
11919 enum bfd_architecture arch;
11920 unsigned int mach;
11921 asymbol **sympp = NULL;
11922 long symsize;
11923 long symcount;
11924 long src_count;
11925 elf_symbol_type *osymbuf;
11926 size_t amt;
11927
11928 implib_bfd = info->out_implib_bfd;
11929 bed = get_elf_backend_data (abfd);
11930
11931 if (!bfd_set_format (implib_bfd, bfd_object))
11932 return false;
11933
11934 /* Use flag from executable but make it a relocatable object. */
11935 flags = bfd_get_file_flags (abfd);
11936 flags &= ~HAS_RELOC;
11937 if (!bfd_set_start_address (implib_bfd, 0)
11938 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11939 return false;
11940
11941 /* Copy architecture of output file to import library file. */
11942 arch = bfd_get_arch (abfd);
11943 mach = bfd_get_mach (abfd);
11944 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11945 && (abfd->target_defaulted
11946 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11947 return false;
11948
11949 /* Get symbol table size. */
11950 symsize = bfd_get_symtab_upper_bound (abfd);
11951 if (symsize < 0)
11952 return false;
11953
11954 /* Read in the symbol table. */
11955 sympp = (asymbol **) bfd_malloc (symsize);
11956 if (sympp == NULL)
11957 return false;
11958
11959 symcount = bfd_canonicalize_symtab (abfd, sympp);
11960 if (symcount < 0)
11961 goto free_sym_buf;
11962
11963 /* Allow the BFD backend to copy any private header data it
11964 understands from the output BFD to the import library BFD. */
11965 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11966 goto free_sym_buf;
11967
11968 /* Filter symbols to appear in the import library. */
11969 if (bed->elf_backend_filter_implib_symbols)
11970 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11971 symcount);
11972 else
11973 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11974 if (symcount == 0)
11975 {
11976 bfd_set_error (bfd_error_no_symbols);
11977 _bfd_error_handler (_("%pB: no symbol found for import library"),
11978 implib_bfd);
11979 goto free_sym_buf;
11980 }
11981
11982
11983 /* Make symbols absolute. */
11984 amt = symcount * sizeof (*osymbuf);
11985 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11986 if (osymbuf == NULL)
11987 goto free_sym_buf;
11988
11989 for (src_count = 0; src_count < symcount; src_count++)
11990 {
11991 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11992 sizeof (*osymbuf));
11993 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11994 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11995 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11996 osymbuf[src_count].internal_elf_sym.st_value =
11997 osymbuf[src_count].symbol.value;
11998 sympp[src_count] = &osymbuf[src_count].symbol;
11999 }
12000
12001 bfd_set_symtab (implib_bfd, sympp, symcount);
12002
12003 /* Allow the BFD backend to copy any private data it understands
12004 from the output BFD to the import library BFD. This is done last
12005 to permit the routine to look at the filtered symbol table. */
12006 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12007 goto free_sym_buf;
12008
12009 if (!bfd_close (implib_bfd))
12010 goto free_sym_buf;
12011
12012 ret = true;
12013
12014 free_sym_buf:
12015 free (sympp);
12016 return ret;
12017 }
12018
12019 static void
12020 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12021 {
12022 asection *o;
12023
12024 if (flinfo->symstrtab != NULL)
12025 _bfd_elf_strtab_free (flinfo->symstrtab);
12026 free (flinfo->contents);
12027 free (flinfo->external_relocs);
12028 free (flinfo->internal_relocs);
12029 free (flinfo->external_syms);
12030 free (flinfo->locsym_shndx);
12031 free (flinfo->internal_syms);
12032 free (flinfo->indices);
12033 free (flinfo->sections);
12034 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12035 free (flinfo->symshndxbuf);
12036 for (o = obfd->sections; o != NULL; o = o->next)
12037 {
12038 struct bfd_elf_section_data *esdo = elf_section_data (o);
12039 free (esdo->rel.hashes);
12040 free (esdo->rela.hashes);
12041 }
12042 }
12043
12044 /* Do the final step of an ELF link. */
12045
12046 bool
12047 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12048 {
12049 bool dynamic;
12050 bool emit_relocs;
12051 bfd *dynobj;
12052 struct elf_final_link_info flinfo;
12053 asection *o;
12054 struct bfd_link_order *p;
12055 bfd *sub;
12056 bfd_size_type max_contents_size;
12057 bfd_size_type max_external_reloc_size;
12058 bfd_size_type max_internal_reloc_count;
12059 bfd_size_type max_sym_count;
12060 bfd_size_type max_sym_shndx_count;
12061 Elf_Internal_Sym elfsym;
12062 unsigned int i;
12063 Elf_Internal_Shdr *symtab_hdr;
12064 Elf_Internal_Shdr *symtab_shndx_hdr;
12065 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12066 struct elf_outext_info eoinfo;
12067 bool merged;
12068 size_t relativecount = 0;
12069 asection *reldyn = 0;
12070 bfd_size_type amt;
12071 asection *attr_section = NULL;
12072 bfd_vma attr_size = 0;
12073 const char *std_attrs_section;
12074 struct elf_link_hash_table *htab = elf_hash_table (info);
12075 bool sections_removed;
12076 bool ret;
12077
12078 if (!is_elf_hash_table (&htab->root))
12079 return false;
12080
12081 if (bfd_link_pic (info))
12082 abfd->flags |= DYNAMIC;
12083
12084 dynamic = htab->dynamic_sections_created;
12085 dynobj = htab->dynobj;
12086
12087 emit_relocs = (bfd_link_relocatable (info)
12088 || info->emitrelocations);
12089
12090 memset (&flinfo, 0, sizeof (flinfo));
12091 flinfo.info = info;
12092 flinfo.output_bfd = abfd;
12093 flinfo.symstrtab = _bfd_elf_strtab_init ();
12094 if (flinfo.symstrtab == NULL)
12095 return false;
12096
12097 if (! dynamic)
12098 {
12099 flinfo.hash_sec = NULL;
12100 flinfo.symver_sec = NULL;
12101 }
12102 else
12103 {
12104 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12105 /* Note that dynsym_sec can be NULL (on VMS). */
12106 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12107 /* Note that it is OK if symver_sec is NULL. */
12108 }
12109
12110 if (info->unique_symbol
12111 && !bfd_hash_table_init (&flinfo.local_hash_table,
12112 local_hash_newfunc,
12113 sizeof (struct local_hash_entry)))
12114 return false;
12115
12116 /* The object attributes have been merged. Remove the input
12117 sections from the link, and set the contents of the output
12118 section. */
12119 sections_removed = false;
12120 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12121 for (o = abfd->sections; o != NULL; o = o->next)
12122 {
12123 bool remove_section = false;
12124
12125 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12126 || strcmp (o->name, ".gnu.attributes") == 0)
12127 {
12128 for (p = o->map_head.link_order; p != NULL; p = p->next)
12129 {
12130 asection *input_section;
12131
12132 if (p->type != bfd_indirect_link_order)
12133 continue;
12134 input_section = p->u.indirect.section;
12135 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12136 elf_link_input_bfd ignores this section. */
12137 input_section->flags &= ~SEC_HAS_CONTENTS;
12138 }
12139
12140 attr_size = bfd_elf_obj_attr_size (abfd);
12141 bfd_set_section_size (o, attr_size);
12142 /* Skip this section later on. */
12143 o->map_head.link_order = NULL;
12144 if (attr_size)
12145 attr_section = o;
12146 else
12147 remove_section = true;
12148 }
12149 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12150 {
12151 /* Remove empty group section from linker output. */
12152 remove_section = true;
12153 }
12154 if (remove_section)
12155 {
12156 o->flags |= SEC_EXCLUDE;
12157 bfd_section_list_remove (abfd, o);
12158 abfd->section_count--;
12159 sections_removed = true;
12160 }
12161 }
12162 if (sections_removed)
12163 _bfd_fix_excluded_sec_syms (abfd, info);
12164
12165 /* Count up the number of relocations we will output for each output
12166 section, so that we know the sizes of the reloc sections. We
12167 also figure out some maximum sizes. */
12168 max_contents_size = 0;
12169 max_external_reloc_size = 0;
12170 max_internal_reloc_count = 0;
12171 max_sym_count = 0;
12172 max_sym_shndx_count = 0;
12173 merged = false;
12174 for (o = abfd->sections; o != NULL; o = o->next)
12175 {
12176 struct bfd_elf_section_data *esdo = elf_section_data (o);
12177 o->reloc_count = 0;
12178
12179 for (p = o->map_head.link_order; p != NULL; p = p->next)
12180 {
12181 unsigned int reloc_count = 0;
12182 unsigned int additional_reloc_count = 0;
12183 struct bfd_elf_section_data *esdi = NULL;
12184
12185 if (p->type == bfd_section_reloc_link_order
12186 || p->type == bfd_symbol_reloc_link_order)
12187 reloc_count = 1;
12188 else if (p->type == bfd_indirect_link_order)
12189 {
12190 asection *sec;
12191
12192 sec = p->u.indirect.section;
12193
12194 /* Mark all sections which are to be included in the
12195 link. This will normally be every section. We need
12196 to do this so that we can identify any sections which
12197 the linker has decided to not include. */
12198 sec->linker_mark = true;
12199
12200 if (sec->flags & SEC_MERGE)
12201 merged = true;
12202
12203 if (sec->rawsize > max_contents_size)
12204 max_contents_size = sec->rawsize;
12205 if (sec->size > max_contents_size)
12206 max_contents_size = sec->size;
12207
12208 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12209 && (sec->owner->flags & DYNAMIC) == 0)
12210 {
12211 size_t sym_count;
12212
12213 /* We are interested in just local symbols, not all
12214 symbols. */
12215 if (elf_bad_symtab (sec->owner))
12216 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12217 / bed->s->sizeof_sym);
12218 else
12219 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12220
12221 if (sym_count > max_sym_count)
12222 max_sym_count = sym_count;
12223
12224 if (sym_count > max_sym_shndx_count
12225 && elf_symtab_shndx_list (sec->owner) != NULL)
12226 max_sym_shndx_count = sym_count;
12227
12228 if (esdo->this_hdr.sh_type == SHT_REL
12229 || esdo->this_hdr.sh_type == SHT_RELA)
12230 /* Some backends use reloc_count in relocation sections
12231 to count particular types of relocs. Of course,
12232 reloc sections themselves can't have relocations. */
12233 ;
12234 else if (emit_relocs)
12235 {
12236 reloc_count = sec->reloc_count;
12237 if (bed->elf_backend_count_additional_relocs)
12238 {
12239 int c;
12240 c = (*bed->elf_backend_count_additional_relocs) (sec);
12241 additional_reloc_count += c;
12242 }
12243 }
12244 else if (bed->elf_backend_count_relocs)
12245 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12246
12247 esdi = elf_section_data (sec);
12248
12249 if ((sec->flags & SEC_RELOC) != 0)
12250 {
12251 size_t ext_size = 0;
12252
12253 if (esdi->rel.hdr != NULL)
12254 ext_size = esdi->rel.hdr->sh_size;
12255 if (esdi->rela.hdr != NULL)
12256 ext_size += esdi->rela.hdr->sh_size;
12257
12258 if (ext_size > max_external_reloc_size)
12259 max_external_reloc_size = ext_size;
12260 if (sec->reloc_count > max_internal_reloc_count)
12261 max_internal_reloc_count = sec->reloc_count;
12262 }
12263 }
12264 }
12265
12266 if (reloc_count == 0)
12267 continue;
12268
12269 reloc_count += additional_reloc_count;
12270 o->reloc_count += reloc_count;
12271
12272 if (p->type == bfd_indirect_link_order && emit_relocs)
12273 {
12274 if (esdi->rel.hdr)
12275 {
12276 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12277 esdo->rel.count += additional_reloc_count;
12278 }
12279 if (esdi->rela.hdr)
12280 {
12281 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12282 esdo->rela.count += additional_reloc_count;
12283 }
12284 }
12285 else
12286 {
12287 if (o->use_rela_p)
12288 esdo->rela.count += reloc_count;
12289 else
12290 esdo->rel.count += reloc_count;
12291 }
12292 }
12293
12294 if (o->reloc_count > 0)
12295 o->flags |= SEC_RELOC;
12296 else
12297 {
12298 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12299 set it (this is probably a bug) and if it is set
12300 assign_section_numbers will create a reloc section. */
12301 o->flags &=~ SEC_RELOC;
12302 }
12303
12304 /* If the SEC_ALLOC flag is not set, force the section VMA to
12305 zero. This is done in elf_fake_sections as well, but forcing
12306 the VMA to 0 here will ensure that relocs against these
12307 sections are handled correctly. */
12308 if ((o->flags & SEC_ALLOC) == 0
12309 && ! o->user_set_vma)
12310 o->vma = 0;
12311 }
12312
12313 if (! bfd_link_relocatable (info) && merged)
12314 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12315
12316 /* Figure out the file positions for everything but the symbol table
12317 and the relocs. We set symcount to force assign_section_numbers
12318 to create a symbol table. */
12319 abfd->symcount = info->strip != strip_all || emit_relocs;
12320 BFD_ASSERT (! abfd->output_has_begun);
12321 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12322 goto error_return;
12323
12324 /* Set sizes, and assign file positions for reloc sections. */
12325 for (o = abfd->sections; o != NULL; o = o->next)
12326 {
12327 struct bfd_elf_section_data *esdo = elf_section_data (o);
12328 if ((o->flags & SEC_RELOC) != 0)
12329 {
12330 if (esdo->rel.hdr
12331 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12332 goto error_return;
12333
12334 if (esdo->rela.hdr
12335 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12336 goto error_return;
12337 }
12338
12339 /* _bfd_elf_compute_section_file_positions makes temporary use
12340 of target_index. Reset it. */
12341 o->target_index = 0;
12342
12343 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12344 to count upwards while actually outputting the relocations. */
12345 esdo->rel.count = 0;
12346 esdo->rela.count = 0;
12347
12348 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12349 && !bfd_section_is_ctf (o))
12350 {
12351 /* Cache the section contents so that they can be compressed
12352 later. Use bfd_malloc since it will be freed by
12353 bfd_compress_section_contents. */
12354 unsigned char *contents = esdo->this_hdr.contents;
12355 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12356 abort ();
12357 contents
12358 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12359 if (contents == NULL)
12360 goto error_return;
12361 esdo->this_hdr.contents = contents;
12362 }
12363 }
12364
12365 /* We have now assigned file positions for all the sections except .symtab,
12366 .strtab, and non-loaded reloc and compressed debugging sections. We start
12367 the .symtab section at the current file position, and write directly to it.
12368 We build the .strtab section in memory. */
12369 abfd->symcount = 0;
12370 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12371 /* sh_name is set in prep_headers. */
12372 symtab_hdr->sh_type = SHT_SYMTAB;
12373 /* sh_flags, sh_addr and sh_size all start off zero. */
12374 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12375 /* sh_link is set in assign_section_numbers. */
12376 /* sh_info is set below. */
12377 /* sh_offset is set just below. */
12378 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12379
12380 if (max_sym_count < 20)
12381 max_sym_count = 20;
12382 htab->strtabsize = max_sym_count;
12383 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12384 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12385 if (htab->strtab == NULL)
12386 goto error_return;
12387 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12388 flinfo.symshndxbuf
12389 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12390 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12391
12392 if (info->strip != strip_all || emit_relocs)
12393 {
12394 file_ptr off = elf_next_file_pos (abfd);
12395
12396 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12397
12398 /* Note that at this point elf_next_file_pos (abfd) is
12399 incorrect. We do not yet know the size of the .symtab section.
12400 We correct next_file_pos below, after we do know the size. */
12401
12402 /* Start writing out the symbol table. The first symbol is always a
12403 dummy symbol. */
12404 elfsym.st_value = 0;
12405 elfsym.st_size = 0;
12406 elfsym.st_info = 0;
12407 elfsym.st_other = 0;
12408 elfsym.st_shndx = SHN_UNDEF;
12409 elfsym.st_target_internal = 0;
12410 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12411 bfd_und_section_ptr, NULL) != 1)
12412 goto error_return;
12413
12414 /* Output a symbol for each section if asked or they are used for
12415 relocs. These symbols usually have no names. We store the
12416 index of each one in the index field of the section, so that
12417 we can find it again when outputting relocs. */
12418
12419 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12420 {
12421 bool name_local_sections
12422 = (bed->elf_backend_name_local_section_symbols
12423 && bed->elf_backend_name_local_section_symbols (abfd));
12424 const char *name = NULL;
12425
12426 elfsym.st_size = 0;
12427 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12428 elfsym.st_other = 0;
12429 elfsym.st_value = 0;
12430 elfsym.st_target_internal = 0;
12431 for (i = 1; i < elf_numsections (abfd); i++)
12432 {
12433 o = bfd_section_from_elf_index (abfd, i);
12434 if (o != NULL)
12435 {
12436 o->target_index = bfd_get_symcount (abfd);
12437 elfsym.st_shndx = i;
12438 if (!bfd_link_relocatable (info))
12439 elfsym.st_value = o->vma;
12440 if (name_local_sections)
12441 name = o->name;
12442 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12443 NULL) != 1)
12444 goto error_return;
12445 }
12446 }
12447 }
12448 }
12449
12450 /* On some targets like Irix 5 the symbol split between local and global
12451 ones recorded in the sh_info field needs to be done between section
12452 and all other symbols. */
12453 if (bed->elf_backend_elfsym_local_is_section
12454 && bed->elf_backend_elfsym_local_is_section (abfd))
12455 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12456
12457 /* Allocate some memory to hold information read in from the input
12458 files. */
12459 if (max_contents_size != 0)
12460 {
12461 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12462 if (flinfo.contents == NULL)
12463 goto error_return;
12464 }
12465
12466 if (max_external_reloc_size != 0)
12467 {
12468 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12469 if (flinfo.external_relocs == NULL)
12470 goto error_return;
12471 }
12472
12473 if (max_internal_reloc_count != 0)
12474 {
12475 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12476 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12477 if (flinfo.internal_relocs == NULL)
12478 goto error_return;
12479 }
12480
12481 if (max_sym_count != 0)
12482 {
12483 amt = max_sym_count * bed->s->sizeof_sym;
12484 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12485 if (flinfo.external_syms == NULL)
12486 goto error_return;
12487
12488 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12489 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12490 if (flinfo.internal_syms == NULL)
12491 goto error_return;
12492
12493 amt = max_sym_count * sizeof (long);
12494 flinfo.indices = (long int *) bfd_malloc (amt);
12495 if (flinfo.indices == NULL)
12496 goto error_return;
12497
12498 amt = max_sym_count * sizeof (asection *);
12499 flinfo.sections = (asection **) bfd_malloc (amt);
12500 if (flinfo.sections == NULL)
12501 goto error_return;
12502 }
12503
12504 if (max_sym_shndx_count != 0)
12505 {
12506 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12507 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12508 if (flinfo.locsym_shndx == NULL)
12509 goto error_return;
12510 }
12511
12512 if (htab->tls_sec)
12513 {
12514 bfd_vma base, end = 0; /* Both bytes. */
12515 asection *sec;
12516
12517 for (sec = htab->tls_sec;
12518 sec && (sec->flags & SEC_THREAD_LOCAL);
12519 sec = sec->next)
12520 {
12521 bfd_size_type size = sec->size;
12522 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12523
12524 if (size == 0
12525 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12526 {
12527 struct bfd_link_order *ord = sec->map_tail.link_order;
12528
12529 if (ord != NULL)
12530 size = ord->offset * opb + ord->size;
12531 }
12532 end = sec->vma + size / opb;
12533 }
12534 base = htab->tls_sec->vma;
12535 /* Only align end of TLS section if static TLS doesn't have special
12536 alignment requirements. */
12537 if (bed->static_tls_alignment == 1)
12538 end = align_power (end, htab->tls_sec->alignment_power);
12539 htab->tls_size = end - base;
12540 }
12541
12542 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12543 return false;
12544
12545 /* Since ELF permits relocations to be against local symbols, we
12546 must have the local symbols available when we do the relocations.
12547 Since we would rather only read the local symbols once, and we
12548 would rather not keep them in memory, we handle all the
12549 relocations for a single input file at the same time.
12550
12551 Unfortunately, there is no way to know the total number of local
12552 symbols until we have seen all of them, and the local symbol
12553 indices precede the global symbol indices. This means that when
12554 we are generating relocatable output, and we see a reloc against
12555 a global symbol, we can not know the symbol index until we have
12556 finished examining all the local symbols to see which ones we are
12557 going to output. To deal with this, we keep the relocations in
12558 memory, and don't output them until the end of the link. This is
12559 an unfortunate waste of memory, but I don't see a good way around
12560 it. Fortunately, it only happens when performing a relocatable
12561 link, which is not the common case. FIXME: If keep_memory is set
12562 we could write the relocs out and then read them again; I don't
12563 know how bad the memory loss will be. */
12564
12565 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12566 sub->output_has_begun = false;
12567 for (o = abfd->sections; o != NULL; o = o->next)
12568 {
12569 for (p = o->map_head.link_order; p != NULL; p = p->next)
12570 {
12571 if (p->type == bfd_indirect_link_order
12572 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12573 == bfd_target_elf_flavour)
12574 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12575 {
12576 if (! sub->output_has_begun)
12577 {
12578 if (! elf_link_input_bfd (&flinfo, sub))
12579 goto error_return;
12580 sub->output_has_begun = true;
12581 }
12582 }
12583 else if (p->type == bfd_section_reloc_link_order
12584 || p->type == bfd_symbol_reloc_link_order)
12585 {
12586 if (! elf_reloc_link_order (abfd, info, o, p))
12587 goto error_return;
12588 }
12589 else
12590 {
12591 if (! _bfd_default_link_order (abfd, info, o, p))
12592 {
12593 if (p->type == bfd_indirect_link_order
12594 && (bfd_get_flavour (sub)
12595 == bfd_target_elf_flavour)
12596 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12597 != bed->s->elfclass))
12598 {
12599 const char *iclass, *oclass;
12600
12601 switch (bed->s->elfclass)
12602 {
12603 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12604 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12605 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12606 default: abort ();
12607 }
12608
12609 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12610 {
12611 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12612 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12613 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12614 default: abort ();
12615 }
12616
12617 bfd_set_error (bfd_error_wrong_format);
12618 _bfd_error_handler
12619 /* xgettext:c-format */
12620 (_("%pB: file class %s incompatible with %s"),
12621 sub, iclass, oclass);
12622 }
12623
12624 goto error_return;
12625 }
12626 }
12627 }
12628 }
12629
12630 /* Free symbol buffer if needed. */
12631 if (!info->reduce_memory_overheads)
12632 {
12633 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12634 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12635 {
12636 free (elf_tdata (sub)->symbuf);
12637 elf_tdata (sub)->symbuf = NULL;
12638 }
12639 }
12640
12641 ret = true;
12642
12643 /* Output any global symbols that got converted to local in a
12644 version script or due to symbol visibility. We do this in a
12645 separate step since ELF requires all local symbols to appear
12646 prior to any global symbols. FIXME: We should only do this if
12647 some global symbols were, in fact, converted to become local.
12648 FIXME: Will this work correctly with the Irix 5 linker? */
12649 eoinfo.failed = false;
12650 eoinfo.flinfo = &flinfo;
12651 eoinfo.localsyms = true;
12652 eoinfo.file_sym_done = false;
12653 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12654 if (eoinfo.failed)
12655 {
12656 ret = false;
12657 goto return_local_hash_table;
12658 }
12659
12660 /* If backend needs to output some local symbols not present in the hash
12661 table, do it now. */
12662 if (bed->elf_backend_output_arch_local_syms
12663 && (info->strip != strip_all || emit_relocs))
12664 {
12665 if (! ((*bed->elf_backend_output_arch_local_syms)
12666 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12667 {
12668 ret = false;
12669 goto return_local_hash_table;
12670 }
12671 }
12672
12673 /* That wrote out all the local symbols. Finish up the symbol table
12674 with the global symbols. Even if we want to strip everything we
12675 can, we still need to deal with those global symbols that got
12676 converted to local in a version script. */
12677
12678 /* The sh_info field records the index of the first non local symbol. */
12679 if (!symtab_hdr->sh_info)
12680 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12681
12682 if (dynamic
12683 && htab->dynsym != NULL
12684 && htab->dynsym->output_section != bfd_abs_section_ptr)
12685 {
12686 Elf_Internal_Sym sym;
12687 bfd_byte *dynsym = htab->dynsym->contents;
12688
12689 o = htab->dynsym->output_section;
12690 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12691
12692 /* Write out the section symbols for the output sections. */
12693 if (bfd_link_pic (info)
12694 || htab->is_relocatable_executable)
12695 {
12696 asection *s;
12697
12698 sym.st_size = 0;
12699 sym.st_name = 0;
12700 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12701 sym.st_other = 0;
12702 sym.st_target_internal = 0;
12703
12704 for (s = abfd->sections; s != NULL; s = s->next)
12705 {
12706 int indx;
12707 bfd_byte *dest;
12708 long dynindx;
12709
12710 dynindx = elf_section_data (s)->dynindx;
12711 if (dynindx <= 0)
12712 continue;
12713 indx = elf_section_data (s)->this_idx;
12714 BFD_ASSERT (indx > 0);
12715 sym.st_shndx = indx;
12716 if (! check_dynsym (abfd, &sym))
12717 {
12718 ret = false;
12719 goto return_local_hash_table;
12720 }
12721 sym.st_value = s->vma;
12722 dest = dynsym + dynindx * bed->s->sizeof_sym;
12723
12724 /* Inform the linker of the addition of this symbol. */
12725
12726 if (info->callbacks->ctf_new_dynsym)
12727 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12728
12729 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12730 }
12731 }
12732
12733 /* Write out the local dynsyms. */
12734 if (htab->dynlocal)
12735 {
12736 struct elf_link_local_dynamic_entry *e;
12737 for (e = htab->dynlocal; e ; e = e->next)
12738 {
12739 asection *s;
12740 bfd_byte *dest;
12741
12742 /* Copy the internal symbol and turn off visibility.
12743 Note that we saved a word of storage and overwrote
12744 the original st_name with the dynstr_index. */
12745 sym = e->isym;
12746 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12747 sym.st_shndx = SHN_UNDEF;
12748
12749 s = bfd_section_from_elf_index (e->input_bfd,
12750 e->isym.st_shndx);
12751 if (s != NULL
12752 && s->output_section != NULL
12753 && elf_section_data (s->output_section) != NULL)
12754 {
12755 sym.st_shndx =
12756 elf_section_data (s->output_section)->this_idx;
12757 if (! check_dynsym (abfd, &sym))
12758 {
12759 ret = false;
12760 goto return_local_hash_table;
12761 }
12762 sym.st_value = (s->output_section->vma
12763 + s->output_offset
12764 + e->isym.st_value);
12765 }
12766
12767 /* Inform the linker of the addition of this symbol. */
12768
12769 if (info->callbacks->ctf_new_dynsym)
12770 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12771
12772 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12773 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12774 }
12775 }
12776 }
12777
12778 /* We get the global symbols from the hash table. */
12779 eoinfo.failed = false;
12780 eoinfo.localsyms = false;
12781 eoinfo.flinfo = &flinfo;
12782 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12783 if (eoinfo.failed)
12784 {
12785 ret = false;
12786 goto return_local_hash_table;
12787 }
12788
12789 /* If backend needs to output some symbols not present in the hash
12790 table, do it now. */
12791 if (bed->elf_backend_output_arch_syms
12792 && (info->strip != strip_all || emit_relocs))
12793 {
12794 if (! ((*bed->elf_backend_output_arch_syms)
12795 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12796 {
12797 ret = false;
12798 goto return_local_hash_table;
12799 }
12800 }
12801
12802 /* Finalize the .strtab section. */
12803 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12804
12805 /* Swap out the .strtab section. */
12806 if (!elf_link_swap_symbols_out (&flinfo))
12807 {
12808 ret = false;
12809 goto return_local_hash_table;
12810 }
12811
12812 /* Now we know the size of the symtab section. */
12813 if (bfd_get_symcount (abfd) > 0)
12814 {
12815 /* Finish up and write out the symbol string table (.strtab)
12816 section. */
12817 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12818 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12819
12820 if (elf_symtab_shndx_list (abfd))
12821 {
12822 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12823
12824 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12825 {
12826 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12827 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12828 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12829 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12830 symtab_shndx_hdr->sh_size = amt;
12831
12832 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12833 off, true);
12834
12835 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12836 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12837 {
12838 ret = false;
12839 goto return_local_hash_table;
12840 }
12841 }
12842 }
12843
12844 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12845 /* sh_name was set in prep_headers. */
12846 symstrtab_hdr->sh_type = SHT_STRTAB;
12847 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12848 symstrtab_hdr->sh_addr = 0;
12849 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12850 symstrtab_hdr->sh_entsize = 0;
12851 symstrtab_hdr->sh_link = 0;
12852 symstrtab_hdr->sh_info = 0;
12853 /* sh_offset is set just below. */
12854 symstrtab_hdr->sh_addralign = 1;
12855
12856 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12857 off, true);
12858 elf_next_file_pos (abfd) = off;
12859
12860 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12861 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12862 {
12863 ret = false;
12864 goto return_local_hash_table;
12865 }
12866 }
12867
12868 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12869 {
12870 _bfd_error_handler (_("%pB: failed to generate import library"),
12871 info->out_implib_bfd);
12872 ret = false;
12873 goto return_local_hash_table;
12874 }
12875
12876 /* Adjust the relocs to have the correct symbol indices. */
12877 for (o = abfd->sections; o != NULL; o = o->next)
12878 {
12879 struct bfd_elf_section_data *esdo = elf_section_data (o);
12880 bool sort;
12881
12882 if ((o->flags & SEC_RELOC) == 0)
12883 continue;
12884
12885 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12886 if (esdo->rel.hdr != NULL
12887 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12888 {
12889 ret = false;
12890 goto return_local_hash_table;
12891 }
12892 if (esdo->rela.hdr != NULL
12893 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12894 {
12895 ret = false;
12896 goto return_local_hash_table;
12897 }
12898
12899 /* Set the reloc_count field to 0 to prevent write_relocs from
12900 trying to swap the relocs out itself. */
12901 o->reloc_count = 0;
12902 }
12903
12904 if (dynamic && info->combreloc && dynobj != NULL)
12905 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12906
12907 /* If we are linking against a dynamic object, or generating a
12908 shared library, finish up the dynamic linking information. */
12909 if (dynamic)
12910 {
12911 bfd_byte *dyncon, *dynconend;
12912
12913 /* Fix up .dynamic entries. */
12914 o = bfd_get_linker_section (dynobj, ".dynamic");
12915 BFD_ASSERT (o != NULL);
12916
12917 dyncon = o->contents;
12918 dynconend = o->contents + o->size;
12919 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12920 {
12921 Elf_Internal_Dyn dyn;
12922 const char *name;
12923 unsigned int type;
12924 bfd_size_type sh_size;
12925 bfd_vma sh_addr;
12926
12927 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12928
12929 switch (dyn.d_tag)
12930 {
12931 default:
12932 continue;
12933 case DT_NULL:
12934 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12935 {
12936 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12937 {
12938 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12939 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12940 default: continue;
12941 }
12942 dyn.d_un.d_val = relativecount;
12943 relativecount = 0;
12944 break;
12945 }
12946 continue;
12947
12948 case DT_INIT:
12949 name = info->init_function;
12950 goto get_sym;
12951 case DT_FINI:
12952 name = info->fini_function;
12953 get_sym:
12954 {
12955 struct elf_link_hash_entry *h;
12956
12957 h = elf_link_hash_lookup (htab, name, false, false, true);
12958 if (h != NULL
12959 && (h->root.type == bfd_link_hash_defined
12960 || h->root.type == bfd_link_hash_defweak))
12961 {
12962 dyn.d_un.d_ptr = h->root.u.def.value;
12963 o = h->root.u.def.section;
12964 if (o->output_section != NULL)
12965 dyn.d_un.d_ptr += (o->output_section->vma
12966 + o->output_offset);
12967 else
12968 {
12969 /* The symbol is imported from another shared
12970 library and does not apply to this one. */
12971 dyn.d_un.d_ptr = 0;
12972 }
12973 break;
12974 }
12975 }
12976 continue;
12977
12978 case DT_PREINIT_ARRAYSZ:
12979 name = ".preinit_array";
12980 goto get_out_size;
12981 case DT_INIT_ARRAYSZ:
12982 name = ".init_array";
12983 goto get_out_size;
12984 case DT_FINI_ARRAYSZ:
12985 name = ".fini_array";
12986 get_out_size:
12987 o = bfd_get_section_by_name (abfd, name);
12988 if (o == NULL)
12989 {
12990 _bfd_error_handler
12991 (_("could not find section %s"), name);
12992 goto error_return;
12993 }
12994 if (o->size == 0)
12995 _bfd_error_handler
12996 (_("warning: %s section has zero size"), name);
12997 dyn.d_un.d_val = o->size;
12998 break;
12999
13000 case DT_PREINIT_ARRAY:
13001 name = ".preinit_array";
13002 goto get_out_vma;
13003 case DT_INIT_ARRAY:
13004 name = ".init_array";
13005 goto get_out_vma;
13006 case DT_FINI_ARRAY:
13007 name = ".fini_array";
13008 get_out_vma:
13009 o = bfd_get_section_by_name (abfd, name);
13010 goto do_vma;
13011
13012 case DT_HASH:
13013 name = ".hash";
13014 goto get_vma;
13015 case DT_GNU_HASH:
13016 name = ".gnu.hash";
13017 goto get_vma;
13018 case DT_STRTAB:
13019 name = ".dynstr";
13020 goto get_vma;
13021 case DT_SYMTAB:
13022 name = ".dynsym";
13023 goto get_vma;
13024 case DT_VERDEF:
13025 name = ".gnu.version_d";
13026 goto get_vma;
13027 case DT_VERNEED:
13028 name = ".gnu.version_r";
13029 goto get_vma;
13030 case DT_VERSYM:
13031 name = ".gnu.version";
13032 get_vma:
13033 o = bfd_get_linker_section (dynobj, name);
13034 do_vma:
13035 if (o == NULL || bfd_is_abs_section (o->output_section))
13036 {
13037 _bfd_error_handler
13038 (_("could not find section %s"), name);
13039 goto error_return;
13040 }
13041 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13042 {
13043 _bfd_error_handler
13044 (_("warning: section '%s' is being made into a note"), name);
13045 bfd_set_error (bfd_error_nonrepresentable_section);
13046 goto error_return;
13047 }
13048 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13049 break;
13050
13051 case DT_REL:
13052 case DT_RELA:
13053 case DT_RELSZ:
13054 case DT_RELASZ:
13055 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13056 type = SHT_REL;
13057 else
13058 type = SHT_RELA;
13059 sh_size = 0;
13060 sh_addr = 0;
13061 for (i = 1; i < elf_numsections (abfd); i++)
13062 {
13063 Elf_Internal_Shdr *hdr;
13064
13065 hdr = elf_elfsections (abfd)[i];
13066 if (hdr->sh_type == type
13067 && (hdr->sh_flags & SHF_ALLOC) != 0)
13068 {
13069 sh_size += hdr->sh_size;
13070 if (sh_addr == 0
13071 || sh_addr > hdr->sh_addr)
13072 sh_addr = hdr->sh_addr;
13073 }
13074 }
13075
13076 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13077 {
13078 unsigned int opb = bfd_octets_per_byte (abfd, o);
13079
13080 /* Don't count procedure linkage table relocs in the
13081 overall reloc count. */
13082 sh_size -= htab->srelplt->size;
13083 if (sh_size == 0)
13084 /* If the size is zero, make the address zero too.
13085 This is to avoid a glibc bug. If the backend
13086 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13087 zero, then we'll put DT_RELA at the end of
13088 DT_JMPREL. glibc will interpret the end of
13089 DT_RELA matching the end of DT_JMPREL as the
13090 case where DT_RELA includes DT_JMPREL, and for
13091 LD_BIND_NOW will decide that processing DT_RELA
13092 will process the PLT relocs too. Net result:
13093 No PLT relocs applied. */
13094 sh_addr = 0;
13095
13096 /* If .rela.plt is the first .rela section, exclude
13097 it from DT_RELA. */
13098 else if (sh_addr == (htab->srelplt->output_section->vma
13099 + htab->srelplt->output_offset) * opb)
13100 sh_addr += htab->srelplt->size;
13101 }
13102
13103 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13104 dyn.d_un.d_val = sh_size;
13105 else
13106 dyn.d_un.d_ptr = sh_addr;
13107 break;
13108 }
13109 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13110 }
13111 }
13112
13113 /* If we have created any dynamic sections, then output them. */
13114 if (dynobj != NULL)
13115 {
13116 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13117 goto error_return;
13118
13119 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13120 if (bfd_link_textrel_check (info)
13121 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13122 {
13123 bfd_byte *dyncon, *dynconend;
13124
13125 dyncon = o->contents;
13126 dynconend = o->contents + o->size;
13127 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13128 {
13129 Elf_Internal_Dyn dyn;
13130
13131 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13132
13133 if (dyn.d_tag == DT_TEXTREL)
13134 {
13135 if (info->textrel_check == textrel_check_error)
13136 info->callbacks->einfo
13137 (_("%P%X: read-only segment has dynamic relocations\n"));
13138 else if (bfd_link_dll (info))
13139 info->callbacks->einfo
13140 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13141 else if (bfd_link_pde (info))
13142 info->callbacks->einfo
13143 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13144 else
13145 info->callbacks->einfo
13146 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13147 break;
13148 }
13149 }
13150 }
13151
13152 for (o = dynobj->sections; o != NULL; o = o->next)
13153 {
13154 if ((o->flags & SEC_HAS_CONTENTS) == 0
13155 || o->size == 0
13156 || o->output_section == bfd_abs_section_ptr)
13157 continue;
13158 if ((o->flags & SEC_LINKER_CREATED) == 0)
13159 {
13160 /* At this point, we are only interested in sections
13161 created by _bfd_elf_link_create_dynamic_sections. */
13162 continue;
13163 }
13164 if (htab->stab_info.stabstr == o)
13165 continue;
13166 if (htab->eh_info.hdr_sec == o)
13167 continue;
13168 if (strcmp (o->name, ".dynstr") != 0)
13169 {
13170 bfd_size_type octets = ((file_ptr) o->output_offset
13171 * bfd_octets_per_byte (abfd, o));
13172 if (!bfd_set_section_contents (abfd, o->output_section,
13173 o->contents, octets, o->size))
13174 goto error_return;
13175 }
13176 else
13177 {
13178 /* The contents of the .dynstr section are actually in a
13179 stringtab. */
13180 file_ptr off;
13181
13182 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13183 if (bfd_seek (abfd, off, SEEK_SET) != 0
13184 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13185 goto error_return;
13186 }
13187 }
13188 }
13189
13190 if (!info->resolve_section_groups)
13191 {
13192 bool failed = false;
13193
13194 BFD_ASSERT (bfd_link_relocatable (info));
13195 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13196 if (failed)
13197 goto error_return;
13198 }
13199
13200 /* If we have optimized stabs strings, output them. */
13201 if (htab->stab_info.stabstr != NULL)
13202 {
13203 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13204 goto error_return;
13205 }
13206
13207 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13208 goto error_return;
13209
13210 if (info->callbacks->emit_ctf)
13211 info->callbacks->emit_ctf ();
13212
13213 elf_final_link_free (abfd, &flinfo);
13214
13215 if (attr_section)
13216 {
13217 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13218 if (contents == NULL)
13219 {
13220 /* Bail out and fail. */
13221 ret = false;
13222 goto return_local_hash_table;
13223 }
13224 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13225 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13226 free (contents);
13227 }
13228
13229 return_local_hash_table:
13230 if (info->unique_symbol)
13231 bfd_hash_table_free (&flinfo.local_hash_table);
13232 return ret;
13233
13234 error_return:
13235 elf_final_link_free (abfd, &flinfo);
13236 ret = false;
13237 goto return_local_hash_table;
13238 }
13239 \f
13240 /* Initialize COOKIE for input bfd ABFD. */
13241
13242 static bool
13243 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13244 struct bfd_link_info *info, bfd *abfd)
13245 {
13246 Elf_Internal_Shdr *symtab_hdr;
13247 const struct elf_backend_data *bed;
13248
13249 bed = get_elf_backend_data (abfd);
13250 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13251
13252 cookie->abfd = abfd;
13253 cookie->sym_hashes = elf_sym_hashes (abfd);
13254 cookie->bad_symtab = elf_bad_symtab (abfd);
13255 if (cookie->bad_symtab)
13256 {
13257 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13258 cookie->extsymoff = 0;
13259 }
13260 else
13261 {
13262 cookie->locsymcount = symtab_hdr->sh_info;
13263 cookie->extsymoff = symtab_hdr->sh_info;
13264 }
13265
13266 if (bed->s->arch_size == 32)
13267 cookie->r_sym_shift = 8;
13268 else
13269 cookie->r_sym_shift = 32;
13270
13271 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13272 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13273 {
13274 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13275 cookie->locsymcount, 0,
13276 NULL, NULL, NULL);
13277 if (cookie->locsyms == NULL)
13278 {
13279 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13280 return false;
13281 }
13282 if (info->keep_memory)
13283 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13284 }
13285 return true;
13286 }
13287
13288 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13289
13290 static void
13291 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13292 {
13293 Elf_Internal_Shdr *symtab_hdr;
13294
13295 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13296 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13297 free (cookie->locsyms);
13298 }
13299
13300 /* Initialize the relocation information in COOKIE for input section SEC
13301 of input bfd ABFD. */
13302
13303 static bool
13304 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13305 struct bfd_link_info *info, bfd *abfd,
13306 asection *sec)
13307 {
13308 if (sec->reloc_count == 0)
13309 {
13310 cookie->rels = NULL;
13311 cookie->relend = NULL;
13312 }
13313 else
13314 {
13315 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13316 info->keep_memory);
13317 if (cookie->rels == NULL)
13318 return false;
13319 cookie->rel = cookie->rels;
13320 cookie->relend = cookie->rels + sec->reloc_count;
13321 }
13322 cookie->rel = cookie->rels;
13323 return true;
13324 }
13325
13326 /* Free the memory allocated by init_reloc_cookie_rels,
13327 if appropriate. */
13328
13329 static void
13330 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13331 asection *sec)
13332 {
13333 if (elf_section_data (sec)->relocs != cookie->rels)
13334 free (cookie->rels);
13335 }
13336
13337 /* Initialize the whole of COOKIE for input section SEC. */
13338
13339 static bool
13340 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13341 struct bfd_link_info *info,
13342 asection *sec)
13343 {
13344 if (!init_reloc_cookie (cookie, info, sec->owner))
13345 goto error1;
13346 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13347 goto error2;
13348 return true;
13349
13350 error2:
13351 fini_reloc_cookie (cookie, sec->owner);
13352 error1:
13353 return false;
13354 }
13355
13356 /* Free the memory allocated by init_reloc_cookie_for_section,
13357 if appropriate. */
13358
13359 static void
13360 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13361 asection *sec)
13362 {
13363 fini_reloc_cookie_rels (cookie, sec);
13364 fini_reloc_cookie (cookie, sec->owner);
13365 }
13366 \f
13367 /* Garbage collect unused sections. */
13368
13369 /* Default gc_mark_hook. */
13370
13371 asection *
13372 _bfd_elf_gc_mark_hook (asection *sec,
13373 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13374 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13375 struct elf_link_hash_entry *h,
13376 Elf_Internal_Sym *sym)
13377 {
13378 if (h != NULL)
13379 {
13380 switch (h->root.type)
13381 {
13382 case bfd_link_hash_defined:
13383 case bfd_link_hash_defweak:
13384 return h->root.u.def.section;
13385
13386 case bfd_link_hash_common:
13387 return h->root.u.c.p->section;
13388
13389 default:
13390 break;
13391 }
13392 }
13393 else
13394 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13395
13396 return NULL;
13397 }
13398
13399 /* Return the debug definition section. */
13400
13401 static asection *
13402 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13403 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13404 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13405 struct elf_link_hash_entry *h,
13406 Elf_Internal_Sym *sym)
13407 {
13408 if (h != NULL)
13409 {
13410 /* Return the global debug definition section. */
13411 if ((h->root.type == bfd_link_hash_defined
13412 || h->root.type == bfd_link_hash_defweak)
13413 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13414 return h->root.u.def.section;
13415 }
13416 else
13417 {
13418 /* Return the local debug definition section. */
13419 asection *isec = bfd_section_from_elf_index (sec->owner,
13420 sym->st_shndx);
13421 if ((isec->flags & SEC_DEBUGGING) != 0)
13422 return isec;
13423 }
13424
13425 return NULL;
13426 }
13427
13428 /* COOKIE->rel describes a relocation against section SEC, which is
13429 a section we've decided to keep. Return the section that contains
13430 the relocation symbol, or NULL if no section contains it. */
13431
13432 asection *
13433 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13434 elf_gc_mark_hook_fn gc_mark_hook,
13435 struct elf_reloc_cookie *cookie,
13436 bool *start_stop)
13437 {
13438 unsigned long r_symndx;
13439 struct elf_link_hash_entry *h, *hw;
13440
13441 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13442 if (r_symndx == STN_UNDEF)
13443 return NULL;
13444
13445 if (r_symndx >= cookie->locsymcount
13446 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13447 {
13448 bool was_marked;
13449
13450 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13451 if (h == NULL)
13452 {
13453 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13454 sec->owner);
13455 return NULL;
13456 }
13457 while (h->root.type == bfd_link_hash_indirect
13458 || h->root.type == bfd_link_hash_warning)
13459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13460
13461 was_marked = h->mark;
13462 h->mark = 1;
13463 /* Keep all aliases of the symbol too. If an object symbol
13464 needs to be copied into .dynbss then all of its aliases
13465 should be present as dynamic symbols, not just the one used
13466 on the copy relocation. */
13467 hw = h;
13468 while (hw->is_weakalias)
13469 {
13470 hw = hw->u.alias;
13471 hw->mark = 1;
13472 }
13473
13474 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13475 {
13476 if (info->start_stop_gc)
13477 return NULL;
13478
13479 /* To work around a glibc bug, mark XXX input sections
13480 when there is a reference to __start_XXX or __stop_XXX
13481 symbols. */
13482 else if (start_stop != NULL)
13483 {
13484 asection *s = h->u2.start_stop_section;
13485 *start_stop = true;
13486 return s;
13487 }
13488 }
13489
13490 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13491 }
13492
13493 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13494 &cookie->locsyms[r_symndx]);
13495 }
13496
13497 /* COOKIE->rel describes a relocation against section SEC, which is
13498 a section we've decided to keep. Mark the section that contains
13499 the relocation symbol. */
13500
13501 bool
13502 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13503 asection *sec,
13504 elf_gc_mark_hook_fn gc_mark_hook,
13505 struct elf_reloc_cookie *cookie)
13506 {
13507 asection *rsec;
13508 bool start_stop = false;
13509
13510 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13511 while (rsec != NULL)
13512 {
13513 if (!rsec->gc_mark)
13514 {
13515 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13516 || (rsec->owner->flags & DYNAMIC) != 0)
13517 rsec->gc_mark = 1;
13518 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13519 return false;
13520 }
13521 if (!start_stop)
13522 break;
13523 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13524 }
13525 return true;
13526 }
13527
13528 /* The mark phase of garbage collection. For a given section, mark
13529 it and any sections in this section's group, and all the sections
13530 which define symbols to which it refers. */
13531
13532 bool
13533 _bfd_elf_gc_mark (struct bfd_link_info *info,
13534 asection *sec,
13535 elf_gc_mark_hook_fn gc_mark_hook)
13536 {
13537 bool ret;
13538 asection *group_sec, *eh_frame;
13539
13540 sec->gc_mark = 1;
13541
13542 /* Mark all the sections in the group. */
13543 group_sec = elf_section_data (sec)->next_in_group;
13544 if (group_sec && !group_sec->gc_mark)
13545 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13546 return false;
13547
13548 /* Look through the section relocs. */
13549 ret = true;
13550 eh_frame = elf_eh_frame_section (sec->owner);
13551 if ((sec->flags & SEC_RELOC) != 0
13552 && sec->reloc_count > 0
13553 && sec != eh_frame)
13554 {
13555 struct elf_reloc_cookie cookie;
13556
13557 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13558 ret = false;
13559 else
13560 {
13561 for (; cookie.rel < cookie.relend; cookie.rel++)
13562 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13563 {
13564 ret = false;
13565 break;
13566 }
13567 fini_reloc_cookie_for_section (&cookie, sec);
13568 }
13569 }
13570
13571 if (ret && eh_frame && elf_fde_list (sec))
13572 {
13573 struct elf_reloc_cookie cookie;
13574
13575 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13576 ret = false;
13577 else
13578 {
13579 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13580 gc_mark_hook, &cookie))
13581 ret = false;
13582 fini_reloc_cookie_for_section (&cookie, eh_frame);
13583 }
13584 }
13585
13586 eh_frame = elf_section_eh_frame_entry (sec);
13587 if (ret && eh_frame && !eh_frame->gc_mark)
13588 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13589 ret = false;
13590
13591 return ret;
13592 }
13593
13594 /* Scan and mark sections in a special or debug section group. */
13595
13596 static void
13597 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13598 {
13599 /* Point to first section of section group. */
13600 asection *ssec;
13601 /* Used to iterate the section group. */
13602 asection *msec;
13603
13604 bool is_special_grp = true;
13605 bool is_debug_grp = true;
13606
13607 /* First scan to see if group contains any section other than debug
13608 and special section. */
13609 ssec = msec = elf_next_in_group (grp);
13610 do
13611 {
13612 if ((msec->flags & SEC_DEBUGGING) == 0)
13613 is_debug_grp = false;
13614
13615 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13616 is_special_grp = false;
13617
13618 msec = elf_next_in_group (msec);
13619 }
13620 while (msec != ssec);
13621
13622 /* If this is a pure debug section group or pure special section group,
13623 keep all sections in this group. */
13624 if (is_debug_grp || is_special_grp)
13625 {
13626 do
13627 {
13628 msec->gc_mark = 1;
13629 msec = elf_next_in_group (msec);
13630 }
13631 while (msec != ssec);
13632 }
13633 }
13634
13635 /* Keep debug and special sections. */
13636
13637 bool
13638 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13639 elf_gc_mark_hook_fn mark_hook)
13640 {
13641 bfd *ibfd;
13642
13643 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13644 {
13645 asection *isec;
13646 bool some_kept;
13647 bool debug_frag_seen;
13648 bool has_kept_debug_info;
13649
13650 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13651 continue;
13652 isec = ibfd->sections;
13653 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13654 continue;
13655
13656 /* Ensure all linker created sections are kept,
13657 see if any other section is already marked,
13658 and note if we have any fragmented debug sections. */
13659 debug_frag_seen = some_kept = has_kept_debug_info = false;
13660 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13661 {
13662 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13663 isec->gc_mark = 1;
13664 else if (isec->gc_mark
13665 && (isec->flags & SEC_ALLOC) != 0
13666 && elf_section_type (isec) != SHT_NOTE)
13667 some_kept = true;
13668 else
13669 {
13670 /* Since all sections, except for backend specific ones,
13671 have been garbage collected, call mark_hook on this
13672 section if any of its linked-to sections is marked. */
13673 asection *linked_to_sec;
13674 for (linked_to_sec = elf_linked_to_section (isec);
13675 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13676 linked_to_sec = elf_linked_to_section (linked_to_sec))
13677 {
13678 if (linked_to_sec->gc_mark)
13679 {
13680 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13681 return false;
13682 break;
13683 }
13684 linked_to_sec->linker_mark = 1;
13685 }
13686 for (linked_to_sec = elf_linked_to_section (isec);
13687 linked_to_sec != NULL && linked_to_sec->linker_mark;
13688 linked_to_sec = elf_linked_to_section (linked_to_sec))
13689 linked_to_sec->linker_mark = 0;
13690 }
13691
13692 if (!debug_frag_seen
13693 && (isec->flags & SEC_DEBUGGING)
13694 && startswith (isec->name, ".debug_line."))
13695 debug_frag_seen = true;
13696 else if (strcmp (bfd_section_name (isec),
13697 "__patchable_function_entries") == 0
13698 && elf_linked_to_section (isec) == NULL)
13699 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13700 "need linked-to section "
13701 "for --gc-sections\n"),
13702 isec->owner, isec);
13703 }
13704
13705 /* If no non-note alloc section in this file will be kept, then
13706 we can toss out the debug and special sections. */
13707 if (!some_kept)
13708 continue;
13709
13710 /* Keep debug and special sections like .comment when they are
13711 not part of a group. Also keep section groups that contain
13712 just debug sections or special sections. NB: Sections with
13713 linked-to section has been handled above. */
13714 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13715 {
13716 if ((isec->flags & SEC_GROUP) != 0)
13717 _bfd_elf_gc_mark_debug_special_section_group (isec);
13718 else if (((isec->flags & SEC_DEBUGGING) != 0
13719 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13720 && elf_next_in_group (isec) == NULL
13721 && elf_linked_to_section (isec) == NULL)
13722 isec->gc_mark = 1;
13723 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13724 has_kept_debug_info = true;
13725 }
13726
13727 /* Look for CODE sections which are going to be discarded,
13728 and find and discard any fragmented debug sections which
13729 are associated with that code section. */
13730 if (debug_frag_seen)
13731 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13732 if ((isec->flags & SEC_CODE) != 0
13733 && isec->gc_mark == 0)
13734 {
13735 unsigned int ilen;
13736 asection *dsec;
13737
13738 ilen = strlen (isec->name);
13739
13740 /* Association is determined by the name of the debug
13741 section containing the name of the code section as
13742 a suffix. For example .debug_line.text.foo is a
13743 debug section associated with .text.foo. */
13744 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13745 {
13746 unsigned int dlen;
13747
13748 if (dsec->gc_mark == 0
13749 || (dsec->flags & SEC_DEBUGGING) == 0)
13750 continue;
13751
13752 dlen = strlen (dsec->name);
13753
13754 if (dlen > ilen
13755 && strncmp (dsec->name + (dlen - ilen),
13756 isec->name, ilen) == 0)
13757 dsec->gc_mark = 0;
13758 }
13759 }
13760
13761 /* Mark debug sections referenced by kept debug sections. */
13762 if (has_kept_debug_info)
13763 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13764 if (isec->gc_mark
13765 && (isec->flags & SEC_DEBUGGING) != 0)
13766 if (!_bfd_elf_gc_mark (info, isec,
13767 elf_gc_mark_debug_section))
13768 return false;
13769 }
13770 return true;
13771 }
13772
13773 static bool
13774 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13775 {
13776 bfd *sub;
13777 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13778
13779 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13780 {
13781 asection *o;
13782
13783 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13784 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13785 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13786 continue;
13787 o = sub->sections;
13788 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13789 continue;
13790
13791 for (o = sub->sections; o != NULL; o = o->next)
13792 {
13793 /* When any section in a section group is kept, we keep all
13794 sections in the section group. If the first member of
13795 the section group is excluded, we will also exclude the
13796 group section. */
13797 if (o->flags & SEC_GROUP)
13798 {
13799 asection *first = elf_next_in_group (o);
13800 o->gc_mark = first->gc_mark;
13801 }
13802
13803 if (o->gc_mark)
13804 continue;
13805
13806 /* Skip sweeping sections already excluded. */
13807 if (o->flags & SEC_EXCLUDE)
13808 continue;
13809
13810 /* Since this is early in the link process, it is simple
13811 to remove a section from the output. */
13812 o->flags |= SEC_EXCLUDE;
13813
13814 if (info->print_gc_sections && o->size != 0)
13815 /* xgettext:c-format */
13816 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13817 o, sub);
13818 }
13819 }
13820
13821 return true;
13822 }
13823
13824 /* Propagate collected vtable information. This is called through
13825 elf_link_hash_traverse. */
13826
13827 static bool
13828 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13829 {
13830 /* Those that are not vtables. */
13831 if (h->start_stop
13832 || h->u2.vtable == NULL
13833 || h->u2.vtable->parent == NULL)
13834 return true;
13835
13836 /* Those vtables that do not have parents, we cannot merge. */
13837 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13838 return true;
13839
13840 /* If we've already been done, exit. */
13841 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13842 return true;
13843
13844 /* Make sure the parent's table is up to date. */
13845 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13846
13847 if (h->u2.vtable->used == NULL)
13848 {
13849 /* None of this table's entries were referenced. Re-use the
13850 parent's table. */
13851 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13852 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13853 }
13854 else
13855 {
13856 size_t n;
13857 bool *cu, *pu;
13858
13859 /* Or the parent's entries into ours. */
13860 cu = h->u2.vtable->used;
13861 cu[-1] = true;
13862 pu = h->u2.vtable->parent->u2.vtable->used;
13863 if (pu != NULL)
13864 {
13865 const struct elf_backend_data *bed;
13866 unsigned int log_file_align;
13867
13868 bed = get_elf_backend_data (h->root.u.def.section->owner);
13869 log_file_align = bed->s->log_file_align;
13870 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13871 while (n--)
13872 {
13873 if (*pu)
13874 *cu = true;
13875 pu++;
13876 cu++;
13877 }
13878 }
13879 }
13880
13881 return true;
13882 }
13883
13884 static bool
13885 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13886 {
13887 asection *sec;
13888 bfd_vma hstart, hend;
13889 Elf_Internal_Rela *relstart, *relend, *rel;
13890 const struct elf_backend_data *bed;
13891 unsigned int log_file_align;
13892
13893 /* Take care of both those symbols that do not describe vtables as
13894 well as those that are not loaded. */
13895 if (h->start_stop
13896 || h->u2.vtable == NULL
13897 || h->u2.vtable->parent == NULL)
13898 return true;
13899
13900 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13901 || h->root.type == bfd_link_hash_defweak);
13902
13903 sec = h->root.u.def.section;
13904 hstart = h->root.u.def.value;
13905 hend = hstart + h->size;
13906
13907 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, true);
13908 if (!relstart)
13909 return *(bool *) okp = false;
13910 bed = get_elf_backend_data (sec->owner);
13911 log_file_align = bed->s->log_file_align;
13912
13913 relend = relstart + sec->reloc_count;
13914
13915 for (rel = relstart; rel < relend; ++rel)
13916 if (rel->r_offset >= hstart && rel->r_offset < hend)
13917 {
13918 /* If the entry is in use, do nothing. */
13919 if (h->u2.vtable->used
13920 && (rel->r_offset - hstart) < h->u2.vtable->size)
13921 {
13922 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13923 if (h->u2.vtable->used[entry])
13924 continue;
13925 }
13926 /* Otherwise, kill it. */
13927 rel->r_offset = rel->r_info = rel->r_addend = 0;
13928 }
13929
13930 return true;
13931 }
13932
13933 /* Mark sections containing dynamically referenced symbols. When
13934 building shared libraries, we must assume that any visible symbol is
13935 referenced. */
13936
13937 bool
13938 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13939 {
13940 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13941 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13942
13943 if ((h->root.type == bfd_link_hash_defined
13944 || h->root.type == bfd_link_hash_defweak)
13945 && (!h->start_stop
13946 || h->root.ldscript_def
13947 || !info->start_stop_gc)
13948 && ((h->ref_dynamic && !h->forced_local)
13949 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13950 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13951 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13952 && (!bfd_link_executable (info)
13953 || info->gc_keep_exported
13954 || info->export_dynamic
13955 || (h->dynamic
13956 && d != NULL
13957 && (*d->match) (&d->head, NULL, h->root.root.string)))
13958 && (h->versioned >= versioned
13959 || !bfd_hide_sym_by_version (info->version_info,
13960 h->root.root.string)))))
13961 h->root.u.def.section->flags |= SEC_KEEP;
13962
13963 return true;
13964 }
13965
13966 /* Keep all sections containing symbols undefined on the command-line,
13967 and the section containing the entry symbol. */
13968
13969 void
13970 _bfd_elf_gc_keep (struct bfd_link_info *info)
13971 {
13972 struct bfd_sym_chain *sym;
13973
13974 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13975 {
13976 struct elf_link_hash_entry *h;
13977
13978 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13979 false, false, false);
13980
13981 if (h != NULL
13982 && (h->root.type == bfd_link_hash_defined
13983 || h->root.type == bfd_link_hash_defweak)
13984 && !bfd_is_const_section (h->root.u.def.section))
13985 h->root.u.def.section->flags |= SEC_KEEP;
13986 }
13987 }
13988
13989 bool
13990 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13991 struct bfd_link_info *info)
13992 {
13993 bfd *ibfd = info->input_bfds;
13994
13995 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13996 {
13997 asection *sec;
13998 struct elf_reloc_cookie cookie;
13999
14000 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14001 continue;
14002 sec = ibfd->sections;
14003 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14004 continue;
14005
14006 if (!init_reloc_cookie (&cookie, info, ibfd))
14007 return false;
14008
14009 for (sec = ibfd->sections; sec; sec = sec->next)
14010 {
14011 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14012 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14013 {
14014 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14015 fini_reloc_cookie_rels (&cookie, sec);
14016 }
14017 }
14018 }
14019 return true;
14020 }
14021
14022 /* Do mark and sweep of unused sections. */
14023
14024 bool
14025 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14026 {
14027 bool ok = true;
14028 bfd *sub;
14029 elf_gc_mark_hook_fn gc_mark_hook;
14030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14031 struct elf_link_hash_table *htab;
14032
14033 if (!bed->can_gc_sections
14034 || !is_elf_hash_table (info->hash))
14035 {
14036 _bfd_error_handler(_("warning: gc-sections option ignored"));
14037 return true;
14038 }
14039
14040 bed->gc_keep (info);
14041 htab = elf_hash_table (info);
14042
14043 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14044 at the .eh_frame section if we can mark the FDEs individually. */
14045 for (sub = info->input_bfds;
14046 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14047 sub = sub->link.next)
14048 {
14049 asection *sec;
14050 struct elf_reloc_cookie cookie;
14051
14052 sec = sub->sections;
14053 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14054 continue;
14055 sec = bfd_get_section_by_name (sub, ".eh_frame");
14056 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14057 {
14058 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14059 if (elf_section_data (sec)->sec_info
14060 && (sec->flags & SEC_LINKER_CREATED) == 0)
14061 elf_eh_frame_section (sub) = sec;
14062 fini_reloc_cookie_for_section (&cookie, sec);
14063 sec = bfd_get_next_section_by_name (NULL, sec);
14064 }
14065 }
14066
14067 /* Apply transitive closure to the vtable entry usage info. */
14068 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14069 if (!ok)
14070 return false;
14071
14072 /* Kill the vtable relocations that were not used. */
14073 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14074 if (!ok)
14075 return false;
14076
14077 /* Mark dynamically referenced symbols. */
14078 if (htab->dynamic_sections_created || info->gc_keep_exported)
14079 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14080
14081 /* Grovel through relocs to find out who stays ... */
14082 gc_mark_hook = bed->gc_mark_hook;
14083 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14084 {
14085 asection *o;
14086
14087 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14088 || elf_object_id (sub) != elf_hash_table_id (htab)
14089 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14090 continue;
14091
14092 o = sub->sections;
14093 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14094 continue;
14095
14096 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14097 Also treat note sections as a root, if the section is not part
14098 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14099 well as FINI_ARRAY sections for ld -r. */
14100 for (o = sub->sections; o != NULL; o = o->next)
14101 if (!o->gc_mark
14102 && (o->flags & SEC_EXCLUDE) == 0
14103 && ((o->flags & SEC_KEEP) != 0
14104 || (bfd_link_relocatable (info)
14105 && ((elf_section_data (o)->this_hdr.sh_type
14106 == SHT_PREINIT_ARRAY)
14107 || (elf_section_data (o)->this_hdr.sh_type
14108 == SHT_INIT_ARRAY)
14109 || (elf_section_data (o)->this_hdr.sh_type
14110 == SHT_FINI_ARRAY)))
14111 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14112 && elf_next_in_group (o) == NULL
14113 && elf_linked_to_section (o) == NULL)
14114 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14115 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14116 {
14117 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14118 return false;
14119 }
14120 }
14121
14122 /* Allow the backend to mark additional target specific sections. */
14123 bed->gc_mark_extra_sections (info, gc_mark_hook);
14124
14125 /* ... and mark SEC_EXCLUDE for those that go. */
14126 return elf_gc_sweep (abfd, info);
14127 }
14128 \f
14129 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14130
14131 bool
14132 bfd_elf_gc_record_vtinherit (bfd *abfd,
14133 asection *sec,
14134 struct elf_link_hash_entry *h,
14135 bfd_vma offset)
14136 {
14137 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14138 struct elf_link_hash_entry **search, *child;
14139 size_t extsymcount;
14140 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14141
14142 /* The sh_info field of the symtab header tells us where the
14143 external symbols start. We don't care about the local symbols at
14144 this point. */
14145 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14146 if (!elf_bad_symtab (abfd))
14147 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14148
14149 sym_hashes = elf_sym_hashes (abfd);
14150 sym_hashes_end = sym_hashes + extsymcount;
14151
14152 /* Hunt down the child symbol, which is in this section at the same
14153 offset as the relocation. */
14154 for (search = sym_hashes; search != sym_hashes_end; ++search)
14155 {
14156 if ((child = *search) != NULL
14157 && (child->root.type == bfd_link_hash_defined
14158 || child->root.type == bfd_link_hash_defweak)
14159 && child->root.u.def.section == sec
14160 && child->root.u.def.value == offset)
14161 goto win;
14162 }
14163
14164 /* xgettext:c-format */
14165 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14166 abfd, sec, (uint64_t) offset);
14167 bfd_set_error (bfd_error_invalid_operation);
14168 return false;
14169
14170 win:
14171 if (!child->u2.vtable)
14172 {
14173 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14174 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14175 if (!child->u2.vtable)
14176 return false;
14177 }
14178 if (!h)
14179 {
14180 /* This *should* only be the absolute section. It could potentially
14181 be that someone has defined a non-global vtable though, which
14182 would be bad. It isn't worth paging in the local symbols to be
14183 sure though; that case should simply be handled by the assembler. */
14184
14185 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14186 }
14187 else
14188 child->u2.vtable->parent = h;
14189
14190 return true;
14191 }
14192
14193 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14194
14195 bool
14196 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14197 struct elf_link_hash_entry *h,
14198 bfd_vma addend)
14199 {
14200 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14201 unsigned int log_file_align = bed->s->log_file_align;
14202
14203 if (!h)
14204 {
14205 /* xgettext:c-format */
14206 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14207 abfd, sec);
14208 bfd_set_error (bfd_error_bad_value);
14209 return false;
14210 }
14211
14212 if (!h->u2.vtable)
14213 {
14214 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14215 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14216 if (!h->u2.vtable)
14217 return false;
14218 }
14219
14220 if (addend >= h->u2.vtable->size)
14221 {
14222 size_t size, bytes, file_align;
14223 bool *ptr = h->u2.vtable->used;
14224
14225 /* While the symbol is undefined, we have to be prepared to handle
14226 a zero size. */
14227 file_align = 1 << log_file_align;
14228 if (h->root.type == bfd_link_hash_undefined)
14229 size = addend + file_align;
14230 else
14231 {
14232 size = h->size;
14233 if (addend >= size)
14234 {
14235 /* Oops! We've got a reference past the defined end of
14236 the table. This is probably a bug -- shall we warn? */
14237 size = addend + file_align;
14238 }
14239 }
14240 size = (size + file_align - 1) & -file_align;
14241
14242 /* Allocate one extra entry for use as a "done" flag for the
14243 consolidation pass. */
14244 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14245
14246 if (ptr)
14247 {
14248 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14249
14250 if (ptr != NULL)
14251 {
14252 size_t oldbytes;
14253
14254 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14255 * sizeof (bool));
14256 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14257 }
14258 }
14259 else
14260 ptr = (bool *) bfd_zmalloc (bytes);
14261
14262 if (ptr == NULL)
14263 return false;
14264
14265 /* And arrange for that done flag to be at index -1. */
14266 h->u2.vtable->used = ptr + 1;
14267 h->u2.vtable->size = size;
14268 }
14269
14270 h->u2.vtable->used[addend >> log_file_align] = true;
14271
14272 return true;
14273 }
14274
14275 /* Map an ELF section header flag to its corresponding string. */
14276 typedef struct
14277 {
14278 char *flag_name;
14279 flagword flag_value;
14280 } elf_flags_to_name_table;
14281
14282 static const elf_flags_to_name_table elf_flags_to_names [] =
14283 {
14284 { "SHF_WRITE", SHF_WRITE },
14285 { "SHF_ALLOC", SHF_ALLOC },
14286 { "SHF_EXECINSTR", SHF_EXECINSTR },
14287 { "SHF_MERGE", SHF_MERGE },
14288 { "SHF_STRINGS", SHF_STRINGS },
14289 { "SHF_INFO_LINK", SHF_INFO_LINK},
14290 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14291 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14292 { "SHF_GROUP", SHF_GROUP },
14293 { "SHF_TLS", SHF_TLS },
14294 { "SHF_MASKOS", SHF_MASKOS },
14295 { "SHF_EXCLUDE", SHF_EXCLUDE },
14296 };
14297
14298 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14299 bool
14300 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14301 struct flag_info *flaginfo,
14302 asection *section)
14303 {
14304 const bfd_vma sh_flags = elf_section_flags (section);
14305
14306 if (!flaginfo->flags_initialized)
14307 {
14308 bfd *obfd = info->output_bfd;
14309 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14310 struct flag_info_list *tf = flaginfo->flag_list;
14311 int with_hex = 0;
14312 int without_hex = 0;
14313
14314 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14315 {
14316 unsigned i;
14317 flagword (*lookup) (char *);
14318
14319 lookup = bed->elf_backend_lookup_section_flags_hook;
14320 if (lookup != NULL)
14321 {
14322 flagword hexval = (*lookup) ((char *) tf->name);
14323
14324 if (hexval != 0)
14325 {
14326 if (tf->with == with_flags)
14327 with_hex |= hexval;
14328 else if (tf->with == without_flags)
14329 without_hex |= hexval;
14330 tf->valid = true;
14331 continue;
14332 }
14333 }
14334 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14335 {
14336 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14337 {
14338 if (tf->with == with_flags)
14339 with_hex |= elf_flags_to_names[i].flag_value;
14340 else if (tf->with == without_flags)
14341 without_hex |= elf_flags_to_names[i].flag_value;
14342 tf->valid = true;
14343 break;
14344 }
14345 }
14346 if (!tf->valid)
14347 {
14348 info->callbacks->einfo
14349 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14350 return false;
14351 }
14352 }
14353 flaginfo->flags_initialized = true;
14354 flaginfo->only_with_flags |= with_hex;
14355 flaginfo->not_with_flags |= without_hex;
14356 }
14357
14358 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14359 return false;
14360
14361 if ((flaginfo->not_with_flags & sh_flags) != 0)
14362 return false;
14363
14364 return true;
14365 }
14366
14367 struct alloc_got_off_arg {
14368 bfd_vma gotoff;
14369 struct bfd_link_info *info;
14370 };
14371
14372 /* We need a special top-level link routine to convert got reference counts
14373 to real got offsets. */
14374
14375 static bool
14376 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14377 {
14378 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14379 bfd *obfd = gofarg->info->output_bfd;
14380 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14381
14382 if (h->got.refcount > 0)
14383 {
14384 h->got.offset = gofarg->gotoff;
14385 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14386 }
14387 else
14388 h->got.offset = (bfd_vma) -1;
14389
14390 return true;
14391 }
14392
14393 /* And an accompanying bit to work out final got entry offsets once
14394 we're done. Should be called from final_link. */
14395
14396 bool
14397 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14398 struct bfd_link_info *info)
14399 {
14400 bfd *i;
14401 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14402 bfd_vma gotoff;
14403 struct alloc_got_off_arg gofarg;
14404
14405 BFD_ASSERT (abfd == info->output_bfd);
14406
14407 if (! is_elf_hash_table (info->hash))
14408 return false;
14409
14410 /* The GOT offset is relative to the .got section, but the GOT header is
14411 put into the .got.plt section, if the backend uses it. */
14412 if (bed->want_got_plt)
14413 gotoff = 0;
14414 else
14415 gotoff = bed->got_header_size;
14416
14417 /* Do the local .got entries first. */
14418 for (i = info->input_bfds; i; i = i->link.next)
14419 {
14420 bfd_signed_vma *local_got;
14421 size_t j, locsymcount;
14422 Elf_Internal_Shdr *symtab_hdr;
14423
14424 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14425 continue;
14426
14427 local_got = elf_local_got_refcounts (i);
14428 if (!local_got)
14429 continue;
14430
14431 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14432 if (elf_bad_symtab (i))
14433 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14434 else
14435 locsymcount = symtab_hdr->sh_info;
14436
14437 for (j = 0; j < locsymcount; ++j)
14438 {
14439 if (local_got[j] > 0)
14440 {
14441 local_got[j] = gotoff;
14442 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14443 }
14444 else
14445 local_got[j] = (bfd_vma) -1;
14446 }
14447 }
14448
14449 /* Then the global .got entries. .plt refcounts are handled by
14450 adjust_dynamic_symbol */
14451 gofarg.gotoff = gotoff;
14452 gofarg.info = info;
14453 elf_link_hash_traverse (elf_hash_table (info),
14454 elf_gc_allocate_got_offsets,
14455 &gofarg);
14456 return true;
14457 }
14458
14459 /* Many folk need no more in the way of final link than this, once
14460 got entry reference counting is enabled. */
14461
14462 bool
14463 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14464 {
14465 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14466 return false;
14467
14468 /* Invoke the regular ELF backend linker to do all the work. */
14469 return bfd_elf_final_link (abfd, info);
14470 }
14471
14472 bool
14473 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14474 {
14475 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14476
14477 if (rcookie->bad_symtab)
14478 rcookie->rel = rcookie->rels;
14479
14480 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14481 {
14482 unsigned long r_symndx;
14483
14484 if (! rcookie->bad_symtab)
14485 if (rcookie->rel->r_offset > offset)
14486 return false;
14487 if (rcookie->rel->r_offset != offset)
14488 continue;
14489
14490 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14491 if (r_symndx == STN_UNDEF)
14492 return true;
14493
14494 if (r_symndx >= rcookie->locsymcount
14495 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14496 {
14497 struct elf_link_hash_entry *h;
14498
14499 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14500
14501 while (h->root.type == bfd_link_hash_indirect
14502 || h->root.type == bfd_link_hash_warning)
14503 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14504
14505 if ((h->root.type == bfd_link_hash_defined
14506 || h->root.type == bfd_link_hash_defweak)
14507 && (h->root.u.def.section->owner != rcookie->abfd
14508 || h->root.u.def.section->kept_section != NULL
14509 || discarded_section (h->root.u.def.section)))
14510 return true;
14511 }
14512 else
14513 {
14514 /* It's not a relocation against a global symbol,
14515 but it could be a relocation against a local
14516 symbol for a discarded section. */
14517 asection *isec;
14518 Elf_Internal_Sym *isym;
14519
14520 /* Need to: get the symbol; get the section. */
14521 isym = &rcookie->locsyms[r_symndx];
14522 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14523 if (isec != NULL
14524 && (isec->kept_section != NULL
14525 || discarded_section (isec)))
14526 return true;
14527 }
14528 return false;
14529 }
14530 return false;
14531 }
14532
14533 /* Discard unneeded references to discarded sections.
14534 Returns -1 on error, 1 if any section's size was changed, 0 if
14535 nothing changed. This function assumes that the relocations are in
14536 sorted order, which is true for all known assemblers. */
14537
14538 int
14539 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14540 {
14541 struct elf_reloc_cookie cookie;
14542 asection *o;
14543 bfd *abfd;
14544 int changed = 0;
14545
14546 if (info->traditional_format
14547 || !is_elf_hash_table (info->hash))
14548 return 0;
14549
14550 o = bfd_get_section_by_name (output_bfd, ".stab");
14551 if (o != NULL)
14552 {
14553 asection *i;
14554
14555 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14556 {
14557 if (i->size == 0
14558 || i->reloc_count == 0
14559 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14560 continue;
14561
14562 abfd = i->owner;
14563 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14564 continue;
14565
14566 if (!init_reloc_cookie_for_section (&cookie, info, i))
14567 return -1;
14568
14569 if (_bfd_discard_section_stabs (abfd, i,
14570 elf_section_data (i)->sec_info,
14571 bfd_elf_reloc_symbol_deleted_p,
14572 &cookie))
14573 changed = 1;
14574
14575 fini_reloc_cookie_for_section (&cookie, i);
14576 }
14577 }
14578
14579 o = NULL;
14580 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14581 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14582 if (o != NULL)
14583 {
14584 asection *i;
14585 int eh_changed = 0;
14586 unsigned int eh_alignment; /* Octets. */
14587
14588 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14589 {
14590 if (i->size == 0)
14591 continue;
14592
14593 abfd = i->owner;
14594 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14595 continue;
14596
14597 if (!init_reloc_cookie_for_section (&cookie, info, i))
14598 return -1;
14599
14600 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14601 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14602 bfd_elf_reloc_symbol_deleted_p,
14603 &cookie))
14604 {
14605 eh_changed = 1;
14606 if (i->size != i->rawsize)
14607 changed = 1;
14608 }
14609
14610 fini_reloc_cookie_for_section (&cookie, i);
14611 }
14612
14613 eh_alignment = ((1 << o->alignment_power)
14614 * bfd_octets_per_byte (output_bfd, o));
14615 /* Skip over zero terminator, and prevent empty sections from
14616 adding alignment padding at the end. */
14617 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14618 if (i->size == 0)
14619 i->flags |= SEC_EXCLUDE;
14620 else if (i->size > 4)
14621 break;
14622 /* The last non-empty eh_frame section doesn't need padding. */
14623 if (i != NULL)
14624 i = i->map_tail.s;
14625 /* Any prior sections must pad the last FDE out to the output
14626 section alignment. Otherwise we might have zero padding
14627 between sections, which would be seen as a terminator. */
14628 for (; i != NULL; i = i->map_tail.s)
14629 if (i->size == 4)
14630 /* All but the last zero terminator should have been removed. */
14631 BFD_FAIL ();
14632 else
14633 {
14634 bfd_size_type size
14635 = (i->size + eh_alignment - 1) & -eh_alignment;
14636 if (i->size != size)
14637 {
14638 i->size = size;
14639 changed = 1;
14640 eh_changed = 1;
14641 }
14642 }
14643 if (eh_changed)
14644 elf_link_hash_traverse (elf_hash_table (info),
14645 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14646 }
14647
14648 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14649 {
14650 const struct elf_backend_data *bed;
14651 asection *s;
14652
14653 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14654 continue;
14655 s = abfd->sections;
14656 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14657 continue;
14658
14659 bed = get_elf_backend_data (abfd);
14660
14661 if (bed->elf_backend_discard_info != NULL)
14662 {
14663 if (!init_reloc_cookie (&cookie, info, abfd))
14664 return -1;
14665
14666 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14667 changed = 1;
14668
14669 fini_reloc_cookie (&cookie, abfd);
14670 }
14671 }
14672
14673 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14674 _bfd_elf_end_eh_frame_parsing (info);
14675
14676 if (info->eh_frame_hdr_type
14677 && !bfd_link_relocatable (info)
14678 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14679 changed = 1;
14680
14681 return changed;
14682 }
14683
14684 bool
14685 _bfd_elf_section_already_linked (bfd *abfd,
14686 asection *sec,
14687 struct bfd_link_info *info)
14688 {
14689 flagword flags;
14690 const char *name, *key;
14691 struct bfd_section_already_linked *l;
14692 struct bfd_section_already_linked_hash_entry *already_linked_list;
14693
14694 if (sec->output_section == bfd_abs_section_ptr)
14695 return false;
14696
14697 flags = sec->flags;
14698
14699 /* Return if it isn't a linkonce section. A comdat group section
14700 also has SEC_LINK_ONCE set. */
14701 if ((flags & SEC_LINK_ONCE) == 0)
14702 return false;
14703
14704 /* Don't put group member sections on our list of already linked
14705 sections. They are handled as a group via their group section. */
14706 if (elf_sec_group (sec) != NULL)
14707 return false;
14708
14709 /* For a SHT_GROUP section, use the group signature as the key. */
14710 name = sec->name;
14711 if ((flags & SEC_GROUP) != 0
14712 && elf_next_in_group (sec) != NULL
14713 && elf_group_name (elf_next_in_group (sec)) != NULL)
14714 key = elf_group_name (elf_next_in_group (sec));
14715 else
14716 {
14717 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14718 if (startswith (name, ".gnu.linkonce.")
14719 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14720 key++;
14721 else
14722 /* Must be a user linkonce section that doesn't follow gcc's
14723 naming convention. In this case we won't be matching
14724 single member groups. */
14725 key = name;
14726 }
14727
14728 already_linked_list = bfd_section_already_linked_table_lookup (key);
14729
14730 for (l = already_linked_list->entry; l != NULL; l = l->next)
14731 {
14732 /* We may have 2 different types of sections on the list: group
14733 sections with a signature of <key> (<key> is some string),
14734 and linkonce sections named .gnu.linkonce.<type>.<key>.
14735 Match like sections. LTO plugin sections are an exception.
14736 They are always named .gnu.linkonce.t.<key> and match either
14737 type of section. */
14738 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14739 && ((flags & SEC_GROUP) != 0
14740 || strcmp (name, l->sec->name) == 0))
14741 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14742 || (sec->owner->flags & BFD_PLUGIN) != 0)
14743 {
14744 /* The section has already been linked. See if we should
14745 issue a warning. */
14746 if (!_bfd_handle_already_linked (sec, l, info))
14747 return false;
14748
14749 if (flags & SEC_GROUP)
14750 {
14751 asection *first = elf_next_in_group (sec);
14752 asection *s = first;
14753
14754 while (s != NULL)
14755 {
14756 s->output_section = bfd_abs_section_ptr;
14757 /* Record which group discards it. */
14758 s->kept_section = l->sec;
14759 s = elf_next_in_group (s);
14760 /* These lists are circular. */
14761 if (s == first)
14762 break;
14763 }
14764 }
14765
14766 return true;
14767 }
14768 }
14769
14770 /* A single member comdat group section may be discarded by a
14771 linkonce section and vice versa. */
14772 if ((flags & SEC_GROUP) != 0)
14773 {
14774 asection *first = elf_next_in_group (sec);
14775
14776 if (first != NULL && elf_next_in_group (first) == first)
14777 /* Check this single member group against linkonce sections. */
14778 for (l = already_linked_list->entry; l != NULL; l = l->next)
14779 if ((l->sec->flags & SEC_GROUP) == 0
14780 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14781 {
14782 first->output_section = bfd_abs_section_ptr;
14783 first->kept_section = l->sec;
14784 sec->output_section = bfd_abs_section_ptr;
14785 break;
14786 }
14787 }
14788 else
14789 /* Check this linkonce section against single member groups. */
14790 for (l = already_linked_list->entry; l != NULL; l = l->next)
14791 if (l->sec->flags & SEC_GROUP)
14792 {
14793 asection *first = elf_next_in_group (l->sec);
14794
14795 if (first != NULL
14796 && elf_next_in_group (first) == first
14797 && bfd_elf_match_symbols_in_sections (first, sec, info))
14798 {
14799 sec->output_section = bfd_abs_section_ptr;
14800 sec->kept_section = first;
14801 break;
14802 }
14803 }
14804
14805 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14806 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14807 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14808 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14809 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14810 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14811 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14812 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14813 The reverse order cannot happen as there is never a bfd with only the
14814 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14815 matter as here were are looking only for cross-bfd sections. */
14816
14817 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
14818 for (l = already_linked_list->entry; l != NULL; l = l->next)
14819 if ((l->sec->flags & SEC_GROUP) == 0
14820 && startswith (l->sec->name, ".gnu.linkonce.t."))
14821 {
14822 if (abfd != l->sec->owner)
14823 sec->output_section = bfd_abs_section_ptr;
14824 break;
14825 }
14826
14827 /* This is the first section with this name. Record it. */
14828 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14829 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14830 return sec->output_section == bfd_abs_section_ptr;
14831 }
14832
14833 bool
14834 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14835 {
14836 return sym->st_shndx == SHN_COMMON;
14837 }
14838
14839 unsigned int
14840 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14841 {
14842 return SHN_COMMON;
14843 }
14844
14845 asection *
14846 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14847 {
14848 return bfd_com_section_ptr;
14849 }
14850
14851 bfd_vma
14852 _bfd_elf_default_got_elt_size (bfd *abfd,
14853 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14854 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14855 bfd *ibfd ATTRIBUTE_UNUSED,
14856 unsigned long symndx ATTRIBUTE_UNUSED)
14857 {
14858 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14859 return bed->s->arch_size / 8;
14860 }
14861
14862 /* Routines to support the creation of dynamic relocs. */
14863
14864 /* Returns the name of the dynamic reloc section associated with SEC. */
14865
14866 static const char *
14867 get_dynamic_reloc_section_name (bfd * abfd,
14868 asection * sec,
14869 bool is_rela)
14870 {
14871 char *name;
14872 const char *old_name = bfd_section_name (sec);
14873 const char *prefix = is_rela ? ".rela" : ".rel";
14874
14875 if (old_name == NULL)
14876 return NULL;
14877
14878 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14879 sprintf (name, "%s%s", prefix, old_name);
14880
14881 return name;
14882 }
14883
14884 /* Returns the dynamic reloc section associated with SEC.
14885 If necessary compute the name of the dynamic reloc section based
14886 on SEC's name (looked up in ABFD's string table) and the setting
14887 of IS_RELA. */
14888
14889 asection *
14890 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14891 asection *sec,
14892 bool is_rela)
14893 {
14894 asection *reloc_sec = elf_section_data (sec)->sreloc;
14895
14896 if (reloc_sec == NULL)
14897 {
14898 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14899
14900 if (name != NULL)
14901 {
14902 reloc_sec = bfd_get_linker_section (abfd, name);
14903
14904 if (reloc_sec != NULL)
14905 elf_section_data (sec)->sreloc = reloc_sec;
14906 }
14907 }
14908
14909 return reloc_sec;
14910 }
14911
14912 /* Returns the dynamic reloc section associated with SEC. If the
14913 section does not exist it is created and attached to the DYNOBJ
14914 bfd and stored in the SRELOC field of SEC's elf_section_data
14915 structure.
14916
14917 ALIGNMENT is the alignment for the newly created section and
14918 IS_RELA defines whether the name should be .rela.<SEC's name>
14919 or .rel.<SEC's name>. The section name is looked up in the
14920 string table associated with ABFD. */
14921
14922 asection *
14923 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14924 bfd *dynobj,
14925 unsigned int alignment,
14926 bfd *abfd,
14927 bool is_rela)
14928 {
14929 asection * reloc_sec = elf_section_data (sec)->sreloc;
14930
14931 if (reloc_sec == NULL)
14932 {
14933 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14934
14935 if (name == NULL)
14936 return NULL;
14937
14938 reloc_sec = bfd_get_linker_section (dynobj, name);
14939
14940 if (reloc_sec == NULL)
14941 {
14942 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14943 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14944 if ((sec->flags & SEC_ALLOC) != 0)
14945 flags |= SEC_ALLOC | SEC_LOAD;
14946
14947 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14948 if (reloc_sec != NULL)
14949 {
14950 /* _bfd_elf_get_sec_type_attr chooses a section type by
14951 name. Override as it may be wrong, eg. for a user
14952 section named "auto" we'll get ".relauto" which is
14953 seen to be a .rela section. */
14954 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14955 if (!bfd_set_section_alignment (reloc_sec, alignment))
14956 reloc_sec = NULL;
14957 }
14958 }
14959
14960 elf_section_data (sec)->sreloc = reloc_sec;
14961 }
14962
14963 return reloc_sec;
14964 }
14965
14966 /* Copy the ELF symbol type and other attributes for a linker script
14967 assignment from HSRC to HDEST. Generally this should be treated as
14968 if we found a strong non-dynamic definition for HDEST (except that
14969 ld ignores multiple definition errors). */
14970 void
14971 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14972 struct bfd_link_hash_entry *hdest,
14973 struct bfd_link_hash_entry *hsrc)
14974 {
14975 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14976 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14977 Elf_Internal_Sym isym;
14978
14979 ehdest->type = ehsrc->type;
14980 ehdest->target_internal = ehsrc->target_internal;
14981
14982 isym.st_other = ehsrc->other;
14983 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
14984 }
14985
14986 /* Append a RELA relocation REL to section S in BFD. */
14987
14988 void
14989 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14990 {
14991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14992 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14993 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14994 bed->s->swap_reloca_out (abfd, rel, loc);
14995 }
14996
14997 /* Append a REL relocation REL to section S in BFD. */
14998
14999 void
15000 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15001 {
15002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15003 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15004 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15005 bed->s->swap_reloc_out (abfd, rel, loc);
15006 }
15007
15008 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15009
15010 struct bfd_link_hash_entry *
15011 bfd_elf_define_start_stop (struct bfd_link_info *info,
15012 const char *symbol, asection *sec)
15013 {
15014 struct elf_link_hash_entry *h;
15015
15016 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15017 false, false, true);
15018 /* NB: Common symbols will be turned into definition later. */
15019 if (h != NULL
15020 && !h->root.ldscript_def
15021 && (h->root.type == bfd_link_hash_undefined
15022 || h->root.type == bfd_link_hash_undefweak
15023 || ((h->ref_regular || h->def_dynamic)
15024 && !h->def_regular
15025 && h->root.type != bfd_link_hash_common)))
15026 {
15027 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15028 h->verinfo.verdef = NULL;
15029 h->root.type = bfd_link_hash_defined;
15030 h->root.u.def.section = sec;
15031 h->root.u.def.value = 0;
15032 h->def_regular = 1;
15033 h->def_dynamic = 0;
15034 h->start_stop = 1;
15035 h->u2.start_stop_section = sec;
15036 if (symbol[0] == '.')
15037 {
15038 /* .startof. and .sizeof. symbols are local. */
15039 const struct elf_backend_data *bed;
15040 bed = get_elf_backend_data (info->output_bfd);
15041 (*bed->elf_backend_hide_symbol) (info, h, true);
15042 }
15043 else
15044 {
15045 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15046 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15047 | info->start_stop_visibility);
15048 if (was_dynamic)
15049 bfd_elf_link_record_dynamic_symbol (info, h);
15050 }
15051 return &h->root;
15052 }
15053 return NULL;
15054 }
15055
15056 /* Find dynamic relocs for H that apply to read-only sections. */
15057
15058 asection *
15059 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15060 {
15061 struct elf_dyn_relocs *p;
15062
15063 for (p = h->dyn_relocs; p != NULL; p = p->next)
15064 {
15065 asection *s = p->sec->output_section;
15066
15067 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15068 return p->sec;
15069 }
15070 return NULL;
15071 }
15072
15073 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15074 read-only sections. */
15075
15076 bool
15077 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15078 {
15079 asection *sec;
15080
15081 if (h->root.type == bfd_link_hash_indirect)
15082 return true;
15083
15084 sec = _bfd_elf_readonly_dynrelocs (h);
15085 if (sec != NULL)
15086 {
15087 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15088
15089 info->flags |= DF_TEXTREL;
15090 /* xgettext:c-format */
15091 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15092 "in read-only section `%pA'\n"),
15093 sec->owner, h->root.root.string, sec);
15094
15095 if (bfd_link_textrel_check (info))
15096 /* xgettext:c-format */
15097 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15098 "in read-only section `%pA'\n"),
15099 sec->owner, h->root.root.string, sec);
15100
15101 /* Not an error, just cut short the traversal. */
15102 return false;
15103 }
15104 return true;
15105 }
15106
15107 /* Add dynamic tags. */
15108
15109 bool
15110 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15111 bool need_dynamic_reloc)
15112 {
15113 struct elf_link_hash_table *htab = elf_hash_table (info);
15114
15115 if (htab->dynamic_sections_created)
15116 {
15117 /* Add some entries to the .dynamic section. We fill in the
15118 values later, in finish_dynamic_sections, but we must add
15119 the entries now so that we get the correct size for the
15120 .dynamic section. The DT_DEBUG entry is filled in by the
15121 dynamic linker and used by the debugger. */
15122 #define add_dynamic_entry(TAG, VAL) \
15123 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15124
15125 const struct elf_backend_data *bed
15126 = get_elf_backend_data (output_bfd);
15127
15128 if (bfd_link_executable (info))
15129 {
15130 if (!add_dynamic_entry (DT_DEBUG, 0))
15131 return false;
15132 }
15133
15134 if (htab->dt_pltgot_required || htab->splt->size != 0)
15135 {
15136 /* DT_PLTGOT is used by prelink even if there is no PLT
15137 relocation. */
15138 if (!add_dynamic_entry (DT_PLTGOT, 0))
15139 return false;
15140 }
15141
15142 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15143 {
15144 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15145 || !add_dynamic_entry (DT_PLTREL,
15146 (bed->rela_plts_and_copies_p
15147 ? DT_RELA : DT_REL))
15148 || !add_dynamic_entry (DT_JMPREL, 0))
15149 return false;
15150 }
15151
15152 if (htab->tlsdesc_plt
15153 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15154 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15155 return false;
15156
15157 if (need_dynamic_reloc)
15158 {
15159 if (bed->rela_plts_and_copies_p)
15160 {
15161 if (!add_dynamic_entry (DT_RELA, 0)
15162 || !add_dynamic_entry (DT_RELASZ, 0)
15163 || !add_dynamic_entry (DT_RELAENT,
15164 bed->s->sizeof_rela))
15165 return false;
15166 }
15167 else
15168 {
15169 if (!add_dynamic_entry (DT_REL, 0)
15170 || !add_dynamic_entry (DT_RELSZ, 0)
15171 || !add_dynamic_entry (DT_RELENT,
15172 bed->s->sizeof_rel))
15173 return false;
15174 }
15175
15176 /* If any dynamic relocs apply to a read-only section,
15177 then we need a DT_TEXTREL entry. */
15178 if ((info->flags & DF_TEXTREL) == 0)
15179 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15180 info);
15181
15182 if ((info->flags & DF_TEXTREL) != 0)
15183 {
15184 if (htab->ifunc_resolvers)
15185 info->callbacks->einfo
15186 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15187 "may result in a segfault at runtime; recompile with %s\n"),
15188 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15189
15190 if (!add_dynamic_entry (DT_TEXTREL, 0))
15191 return false;
15192 }
15193 }
15194 }
15195 #undef add_dynamic_entry
15196
15197 return true;
15198 }