]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
Fix: A potential bug of null pointer dereference
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207 \f
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 if (info->enable_dt_relr)
363 {
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
371 }
372
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
379
380 elf_hash_table (info)->dynamic_sections_created = true;
381
382 return true;
383 }
384
385 /* Create dynamic sections when linking against a dynamic object. */
386
387 bool
388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389 {
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
395
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
399
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
410
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
416
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
420 {
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
426 }
427
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
436
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
439
440 if (bed->want_dynbss)
441 {
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
453
454 if (bed->want_dynrelro)
455 {
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
464 }
465
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
478 {
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
487
488 if (bed->want_dynrelro)
489 {
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
498 }
499 }
500 }
501
502 return true;
503 }
504 \f
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
512
513 bool
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
516 {
517 if (h->dynindx == -1)
518 {
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
523
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
526 {
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
532 }
533
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
539 {
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
544 {
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
555 }
556
557 default:
558 break;
559 }
560
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
563
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
566 {
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
571 }
572
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
584
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587 if (p != NULL)
588 *p = ELF_VER_CHR;
589
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
593 }
594
595 return true;
596 }
597 \f
598 /* Mark a symbol dynamic. */
599
600 static void
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
604 {
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
610
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
620 {
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
625 }
626 }
627
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
630
631 bool
632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
637 {
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
641
642 if (!is_elf_hash_table (info->hash))
643 return true;
644
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
649
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653 if (h->versioned == unknown)
654 {
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
658 {
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
663 }
664 }
665
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
669 {
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
672 }
673
674 switch (h->root.type)
675 {
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
709 }
710
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
719
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
726
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
729
730 h->def_regular = 1;
731
732 if (hidden)
733 {
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
738 }
739
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
747
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
754 {
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
757
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
762 {
763 struct elf_link_hash_entry *def = weakdef (h);
764
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
768 }
769 }
770
771 return true;
772 }
773
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
777
778 int
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
782 {
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
791
792 if (! is_elf_hash_table (info->hash))
793 return 0;
794
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
799
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
804
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
808 {
809 bfd_release (input_bfd, entry);
810 return 0;
811 }
812
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
815 {
816 asection *s;
817
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
820 {
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
825 }
826 }
827
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
831
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
834 {
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
839 }
840
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
845
846 eht = elf_hash_table (info);
847
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
853
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858 /* The dynindx will be set at the end of size_dynamic_sections. */
859
860 return 1;
861 }
862
863 /* Return the dynindex of a local dynamic symbol. */
864
865 long
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
869 {
870 struct elf_link_local_dynamic_entry *e;
871
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
876 }
877
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
881
882 static bool
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
885 {
886 size_t *count = (size_t *) data;
887
888 if (h->forced_local)
889 return true;
890
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
893
894 return true;
895 }
896
897
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
900
901 static bool
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
904 {
905 size_t *count = (size_t *) data;
906
907 if (!h->forced_local)
908 return true;
909
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
912
913 return true;
914 }
915
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
922 {
923 struct elf_link_hash_table *htab;
924 asection *ip;
925
926 switch (elf_section_data (p)->this_hdr.sh_type)
927 {
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
936
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
940
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
945 }
946 }
947
948 bool
949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
953 {
954 return true;
955 }
956
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
965
966 static unsigned long
967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
970 {
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
973
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984 {
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
988 }
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
991 }
992 if (do_sec)
993 *section_sym_count = dynsymcount;
994
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
998
999 if (elf_hash_table (info)->dynlocal)
1000 {
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1004 }
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1010
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1016
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1019 }
1020
1021 /* Merge st_other field. */
1022
1023 static void
1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1027 {
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1035
1036 if (!dynamic)
1037 {
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045 }
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1050 }
1051
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1064
1065 static bool
1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1081 {
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093 struct elf_link_hash_table *htab;
1094
1095 *skip = false;
1096 *override = NULL;
1097
1098 sec = *psec;
1099 bind = ELF_ST_BIND (sym->st_info);
1100
1101 if (! bfd_is_und_section (sec))
1102 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1103 else
1104 h = ((struct elf_link_hash_entry *)
1105 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1106 if (h == NULL)
1107 return false;
1108 *sym_hash = h;
1109
1110 bed = get_elf_backend_data (abfd);
1111
1112 /* NEW_VERSION is the symbol version of the new symbol. */
1113 if (h->versioned != unversioned)
1114 {
1115 /* Symbol version is unknown or versioned. */
1116 new_version = strrchr (name, ELF_VER_CHR);
1117 if (new_version)
1118 {
1119 if (h->versioned == unknown)
1120 {
1121 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1122 h->versioned = versioned_hidden;
1123 else
1124 h->versioned = versioned;
1125 }
1126 new_version += 1;
1127 if (new_version[0] == '\0')
1128 new_version = NULL;
1129 }
1130 else
1131 h->versioned = unversioned;
1132 }
1133 else
1134 new_version = NULL;
1135
1136 /* For merging, we only care about real symbols. But we need to make
1137 sure that indirect symbol dynamic flags are updated. */
1138 hi = h;
1139 while (h->root.type == bfd_link_hash_indirect
1140 || h->root.type == bfd_link_hash_warning)
1141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1142
1143 if (!*matched)
1144 {
1145 if (hi == h || h->root.type == bfd_link_hash_new)
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_HIDDEN is true if the existing symbol is only visible
1150 to the symbol with the same symbol version. NEW_HIDDEN is
1151 true if the new symbol is only visible to the symbol with
1152 the same symbol version. */
1153 bool old_hidden = h->versioned == versioned_hidden;
1154 bool new_hidden = hi->versioned == versioned_hidden;
1155 if (!old_hidden && !new_hidden)
1156 /* The new symbol matches the existing symbol if both
1157 aren't hidden. */
1158 *matched = true;
1159 else
1160 {
1161 /* OLD_VERSION is the symbol version of the existing
1162 symbol. */
1163 char *old_version;
1164
1165 if (h->versioned >= versioned)
1166 old_version = strrchr (h->root.root.string,
1167 ELF_VER_CHR) + 1;
1168 else
1169 old_version = NULL;
1170
1171 /* The new symbol matches the existing symbol if they
1172 have the same symbol version. */
1173 *matched = (old_version == new_version
1174 || (old_version != NULL
1175 && new_version != NULL
1176 && strcmp (old_version, new_version) == 0));
1177 }
1178 }
1179 }
1180
1181 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1182 existing symbol. */
1183
1184 oldbfd = NULL;
1185 oldsec = NULL;
1186 switch (h->root.type)
1187 {
1188 default:
1189 break;
1190
1191 case bfd_link_hash_undefined:
1192 case bfd_link_hash_undefweak:
1193 oldbfd = h->root.u.undef.abfd;
1194 break;
1195
1196 case bfd_link_hash_defined:
1197 case bfd_link_hash_defweak:
1198 oldbfd = h->root.u.def.section->owner;
1199 oldsec = h->root.u.def.section;
1200 break;
1201
1202 case bfd_link_hash_common:
1203 oldbfd = h->root.u.c.p->section->owner;
1204 oldsec = h->root.u.c.p->section;
1205 if (pold_alignment)
1206 *pold_alignment = h->root.u.c.p->alignment_power;
1207 break;
1208 }
1209 if (poldbfd && *poldbfd == NULL)
1210 *poldbfd = oldbfd;
1211
1212 /* Differentiate strong and weak symbols. */
1213 newweak = bind == STB_WEAK;
1214 oldweak = (h->root.type == bfd_link_hash_defweak
1215 || h->root.type == bfd_link_hash_undefweak);
1216 if (pold_weak)
1217 *pold_weak = oldweak;
1218
1219 /* We have to check it for every instance since the first few may be
1220 references and not all compilers emit symbol type for undefined
1221 symbols. */
1222 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1223
1224 htab = elf_hash_table (info);
1225
1226 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227 respectively, is from a dynamic object. */
1228
1229 newdyn = (abfd->flags & DYNAMIC) != 0;
1230
1231 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232 syms and defined syms in dynamic libraries respectively.
1233 ref_dynamic on the other hand can be set for a symbol defined in
1234 a dynamic library, and def_dynamic may not be set; When the
1235 definition in a dynamic lib is overridden by a definition in the
1236 executable use of the symbol in the dynamic lib becomes a
1237 reference to the executable symbol. */
1238 if (newdyn)
1239 {
1240 if (bfd_is_und_section (sec))
1241 {
1242 if (bind != STB_WEAK)
1243 {
1244 h->ref_dynamic_nonweak = 1;
1245 hi->ref_dynamic_nonweak = 1;
1246 }
1247 }
1248 else
1249 {
1250 /* Update the existing symbol only if they match. */
1251 if (*matched)
1252 h->dynamic_def = 1;
1253 hi->dynamic_def = 1;
1254 }
1255 }
1256
1257 /* If we just created the symbol, mark it as being an ELF symbol.
1258 Other than that, there is nothing to do--there is no merge issue
1259 with a newly defined symbol--so we just return. */
1260
1261 if (h->root.type == bfd_link_hash_new)
1262 {
1263 h->non_elf = 0;
1264 return true;
1265 }
1266
1267 /* In cases involving weak versioned symbols, we may wind up trying
1268 to merge a symbol with itself. Catch that here, to avoid the
1269 confusion that results if we try to override a symbol with
1270 itself. The additional tests catch cases like
1271 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272 dynamic object, which we do want to handle here. */
1273 if (abfd == oldbfd
1274 && (newweak || oldweak)
1275 && ((abfd->flags & DYNAMIC) == 0
1276 || !h->def_regular))
1277 return true;
1278
1279 olddyn = false;
1280 if (oldbfd != NULL)
1281 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1282 else if (oldsec != NULL)
1283 {
1284 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285 indices used by MIPS ELF. */
1286 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1287 }
1288
1289 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1290 if (!htab->handling_dt_needed
1291 && oldbfd != NULL
1292 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1293 {
1294 if (newdyn != olddyn)
1295 {
1296 /* Handle a case where plugin_notice won't be called and thus
1297 won't set the non_ir_ref flags on the first pass over
1298 symbols. */
1299 h->root.non_ir_ref_dynamic = true;
1300 hi->root.non_ir_ref_dynamic = true;
1301 }
1302 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1303 && hi->root.type == bfd_link_hash_indirect)
1304 {
1305 /* Change indirect symbol from IR to undefined. */
1306 hi->root.type = bfd_link_hash_undefined;
1307 hi->root.u.undef.abfd = oldbfd;
1308 }
1309 }
1310
1311 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312 respectively, appear to be a definition rather than reference. */
1313
1314 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1315
1316 olddef = (h->root.type != bfd_link_hash_undefined
1317 && h->root.type != bfd_link_hash_undefweak
1318 && h->root.type != bfd_link_hash_common);
1319
1320 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321 respectively, appear to be a function. */
1322
1323 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1324 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1325
1326 oldfunc = (h->type != STT_NOTYPE
1327 && bed->is_function_type (h->type));
1328
1329 if (!(newfunc && oldfunc)
1330 && ELF_ST_TYPE (sym->st_info) != h->type
1331 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1332 && h->type != STT_NOTYPE
1333 && (newdef || bfd_is_com_section (sec))
1334 && (olddef || h->root.type == bfd_link_hash_common))
1335 {
1336 /* If creating a default indirect symbol ("foo" or "foo@") from
1337 a dynamic versioned definition ("foo@@") skip doing so if
1338 there is an existing regular definition with a different
1339 type. We don't want, for example, a "time" variable in the
1340 executable overriding a "time" function in a shared library. */
1341 if (newdyn
1342 && !olddyn)
1343 {
1344 *skip = true;
1345 return true;
1346 }
1347
1348 /* When adding a symbol from a regular object file after we have
1349 created indirect symbols, undo the indirection and any
1350 dynamic state. */
1351 if (hi != h
1352 && !newdyn
1353 && olddyn)
1354 {
1355 h = hi;
1356 (*bed->elf_backend_hide_symbol) (info, h, true);
1357 h->forced_local = 0;
1358 h->ref_dynamic = 0;
1359 h->def_dynamic = 0;
1360 h->dynamic_def = 0;
1361 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1362 {
1363 h->root.type = bfd_link_hash_undefined;
1364 h->root.u.undef.abfd = abfd;
1365 }
1366 else
1367 {
1368 h->root.type = bfd_link_hash_new;
1369 h->root.u.undef.abfd = NULL;
1370 }
1371 return true;
1372 }
1373 }
1374
1375 /* Check TLS symbols. We don't check undefined symbols introduced
1376 by "ld -u" which have no type (and oldbfd NULL), and we don't
1377 check symbols from plugins because they also have no type. */
1378 if (oldbfd != NULL
1379 && (oldbfd->flags & BFD_PLUGIN) == 0
1380 && (abfd->flags & BFD_PLUGIN) == 0
1381 && ELF_ST_TYPE (sym->st_info) != h->type
1382 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1383 {
1384 bfd *ntbfd, *tbfd;
1385 bool ntdef, tdef;
1386 asection *ntsec, *tsec;
1387
1388 if (h->type == STT_TLS)
1389 {
1390 ntbfd = abfd;
1391 ntsec = sec;
1392 ntdef = newdef;
1393 tbfd = oldbfd;
1394 tsec = oldsec;
1395 tdef = olddef;
1396 }
1397 else
1398 {
1399 ntbfd = oldbfd;
1400 ntsec = oldsec;
1401 ntdef = olddef;
1402 tbfd = abfd;
1403 tsec = sec;
1404 tdef = newdef;
1405 }
1406
1407 if (tdef && ntdef)
1408 _bfd_error_handler
1409 /* xgettext:c-format */
1410 (_("%s: TLS definition in %pB section %pA "
1411 "mismatches non-TLS definition in %pB section %pA"),
1412 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1413 else if (!tdef && !ntdef)
1414 _bfd_error_handler
1415 /* xgettext:c-format */
1416 (_("%s: TLS reference in %pB "
1417 "mismatches non-TLS reference in %pB"),
1418 h->root.root.string, tbfd, ntbfd);
1419 else if (tdef)
1420 _bfd_error_handler
1421 /* xgettext:c-format */
1422 (_("%s: TLS definition in %pB section %pA "
1423 "mismatches non-TLS reference in %pB"),
1424 h->root.root.string, tbfd, tsec, ntbfd);
1425 else
1426 _bfd_error_handler
1427 /* xgettext:c-format */
1428 (_("%s: TLS reference in %pB "
1429 "mismatches non-TLS definition in %pB section %pA"),
1430 h->root.root.string, tbfd, ntbfd, ntsec);
1431
1432 bfd_set_error (bfd_error_bad_value);
1433 return false;
1434 }
1435
1436 /* If the old symbol has non-default visibility, we ignore the new
1437 definition from a dynamic object. */
1438 if (newdyn
1439 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440 && !bfd_is_und_section (sec))
1441 {
1442 *skip = true;
1443 /* Make sure this symbol is dynamic. */
1444 h->ref_dynamic = 1;
1445 hi->ref_dynamic = 1;
1446 /* A protected symbol has external availability. Make sure it is
1447 recorded as dynamic.
1448
1449 FIXME: Should we check type and size for protected symbol? */
1450 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1451 return bfd_elf_link_record_dynamic_symbol (info, h);
1452 else
1453 return true;
1454 }
1455 else if (!newdyn
1456 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1457 && h->def_dynamic)
1458 {
1459 /* If the new symbol with non-default visibility comes from a
1460 relocatable file and the old definition comes from a dynamic
1461 object, we remove the old definition. */
1462 if (hi->root.type == bfd_link_hash_indirect)
1463 {
1464 /* Handle the case where the old dynamic definition is
1465 default versioned. We need to copy the symbol info from
1466 the symbol with default version to the normal one if it
1467 was referenced before. */
1468 if (h->ref_regular)
1469 {
1470 hi->root.type = h->root.type;
1471 h->root.type = bfd_link_hash_indirect;
1472 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1473
1474 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1475 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1476 {
1477 /* If the new symbol is hidden or internal, completely undo
1478 any dynamic link state. */
1479 (*bed->elf_backend_hide_symbol) (info, h, true);
1480 h->forced_local = 0;
1481 h->ref_dynamic = 0;
1482 }
1483 else
1484 h->ref_dynamic = 1;
1485
1486 h->def_dynamic = 0;
1487 /* FIXME: Should we check type and size for protected symbol? */
1488 h->size = 0;
1489 h->type = 0;
1490
1491 h = hi;
1492 }
1493 else
1494 h = hi;
1495 }
1496
1497 /* If the old symbol was undefined before, then it will still be
1498 on the undefs list. If the new symbol is undefined or
1499 common, we can't make it bfd_link_hash_new here, because new
1500 undefined or common symbols will be added to the undefs list
1501 by _bfd_generic_link_add_one_symbol. Symbols may not be
1502 added twice to the undefs list. Also, if the new symbol is
1503 undefweak then we don't want to lose the strong undef. */
1504 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1505 {
1506 h->root.type = bfd_link_hash_undefined;
1507 h->root.u.undef.abfd = abfd;
1508 }
1509 else
1510 {
1511 h->root.type = bfd_link_hash_new;
1512 h->root.u.undef.abfd = NULL;
1513 }
1514
1515 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1516 {
1517 /* If the new symbol is hidden or internal, completely undo
1518 any dynamic link state. */
1519 (*bed->elf_backend_hide_symbol) (info, h, true);
1520 h->forced_local = 0;
1521 h->ref_dynamic = 0;
1522 }
1523 else
1524 h->ref_dynamic = 1;
1525 h->def_dynamic = 0;
1526 /* FIXME: Should we check type and size for protected symbol? */
1527 h->size = 0;
1528 h->type = 0;
1529 return true;
1530 }
1531
1532 /* If a new weak symbol definition comes from a regular file and the
1533 old symbol comes from a dynamic library, we treat the new one as
1534 strong. Similarly, an old weak symbol definition from a regular
1535 file is treated as strong when the new symbol comes from a dynamic
1536 library. Further, an old weak symbol from a dynamic library is
1537 treated as strong if the new symbol is from a dynamic library.
1538 This reflects the way glibc's ld.so works.
1539
1540 Also allow a weak symbol to override a linker script symbol
1541 defined by an early pass over the script. This is done so the
1542 linker knows the symbol is defined in an object file, for the
1543 DEFINED script function.
1544
1545 Do this before setting *type_change_ok or *size_change_ok so that
1546 we warn properly when dynamic library symbols are overridden. */
1547
1548 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1549 newweak = false;
1550 if (olddef && newdyn)
1551 oldweak = false;
1552
1553 /* Allow changes between different types of function symbol. */
1554 if (newfunc && oldfunc)
1555 *type_change_ok = true;
1556
1557 /* It's OK to change the type if either the existing symbol or the
1558 new symbol is weak. A type change is also OK if the old symbol
1559 is undefined and the new symbol is defined. */
1560
1561 if (oldweak
1562 || newweak
1563 || (newdef
1564 && h->root.type == bfd_link_hash_undefined))
1565 *type_change_ok = true;
1566
1567 /* It's OK to change the size if either the existing symbol or the
1568 new symbol is weak, or if the old symbol is undefined. */
1569
1570 if (*type_change_ok
1571 || h->root.type == bfd_link_hash_undefined)
1572 *size_change_ok = true;
1573
1574 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575 symbol, respectively, appears to be a common symbol in a dynamic
1576 object. If a symbol appears in an uninitialized section, and is
1577 not weak, and is not a function, then it may be a common symbol
1578 which was resolved when the dynamic object was created. We want
1579 to treat such symbols specially, because they raise special
1580 considerations when setting the symbol size: if the symbol
1581 appears as a common symbol in a regular object, and the size in
1582 the regular object is larger, we must make sure that we use the
1583 larger size. This problematic case can always be avoided in C,
1584 but it must be handled correctly when using Fortran shared
1585 libraries.
1586
1587 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588 likewise for OLDDYNCOMMON and OLDDEF.
1589
1590 Note that this test is just a heuristic, and that it is quite
1591 possible to have an uninitialized symbol in a shared object which
1592 is really a definition, rather than a common symbol. This could
1593 lead to some minor confusion when the symbol really is a common
1594 symbol in some regular object. However, I think it will be
1595 harmless. */
1596
1597 if (newdyn
1598 && newdef
1599 && !newweak
1600 && (sec->flags & SEC_ALLOC) != 0
1601 && (sec->flags & SEC_LOAD) == 0
1602 && sym->st_size > 0
1603 && !newfunc)
1604 newdyncommon = true;
1605 else
1606 newdyncommon = false;
1607
1608 if (olddyn
1609 && olddef
1610 && h->root.type == bfd_link_hash_defined
1611 && h->def_dynamic
1612 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1613 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1614 && h->size > 0
1615 && !oldfunc)
1616 olddyncommon = true;
1617 else
1618 olddyncommon = false;
1619
1620 /* We now know everything about the old and new symbols. We ask the
1621 backend to check if we can merge them. */
1622 if (bed->merge_symbol != NULL)
1623 {
1624 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1625 return false;
1626 sec = *psec;
1627 }
1628
1629 /* There are multiple definitions of a normal symbol. Skip the
1630 default symbol as well as definition from an IR object. */
1631 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1632 && !default_sym && h->def_regular
1633 && !(oldbfd != NULL
1634 && (oldbfd->flags & BFD_PLUGIN) != 0
1635 && (abfd->flags & BFD_PLUGIN) == 0))
1636 {
1637 /* Handle a multiple definition. */
1638 (*info->callbacks->multiple_definition) (info, &h->root,
1639 abfd, sec, *pvalue);
1640 *skip = true;
1641 return true;
1642 }
1643
1644 /* If both the old and the new symbols look like common symbols in a
1645 dynamic object, set the size of the symbol to the larger of the
1646 two. */
1647
1648 if (olddyncommon
1649 && newdyncommon
1650 && sym->st_size != h->size)
1651 {
1652 /* Since we think we have two common symbols, issue a multiple
1653 common warning if desired. Note that we only warn if the
1654 size is different. If the size is the same, we simply let
1655 the old symbol override the new one as normally happens with
1656 symbols defined in dynamic objects. */
1657
1658 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1659 bfd_link_hash_common, sym->st_size);
1660 if (sym->st_size > h->size)
1661 h->size = sym->st_size;
1662
1663 *size_change_ok = true;
1664 }
1665
1666 /* If we are looking at a dynamic object, and we have found a
1667 definition, we need to see if the symbol was already defined by
1668 some other object. If so, we want to use the existing
1669 definition, and we do not want to report a multiple symbol
1670 definition error; we do this by clobbering *PSEC to be
1671 bfd_und_section_ptr.
1672
1673 We treat a common symbol as a definition if the symbol in the
1674 shared library is a function, since common symbols always
1675 represent variables; this can cause confusion in principle, but
1676 any such confusion would seem to indicate an erroneous program or
1677 shared library. We also permit a common symbol in a regular
1678 object to override a weak symbol in a shared object. */
1679
1680 if (newdyn
1681 && newdef
1682 && (olddef
1683 || (h->root.type == bfd_link_hash_common
1684 && (newweak || newfunc))))
1685 {
1686 *override = abfd;
1687 newdef = false;
1688 newdyncommon = false;
1689
1690 *psec = sec = bfd_und_section_ptr;
1691 *size_change_ok = true;
1692
1693 /* If we get here when the old symbol is a common symbol, then
1694 we are explicitly letting it override a weak symbol or
1695 function in a dynamic object, and we don't want to warn about
1696 a type change. If the old symbol is a defined symbol, a type
1697 change warning may still be appropriate. */
1698
1699 if (h->root.type == bfd_link_hash_common)
1700 *type_change_ok = true;
1701 }
1702
1703 /* Handle the special case of an old common symbol merging with a
1704 new symbol which looks like a common symbol in a shared object.
1705 We change *PSEC and *PVALUE to make the new symbol look like a
1706 common symbol, and let _bfd_generic_link_add_one_symbol do the
1707 right thing. */
1708
1709 if (newdyncommon
1710 && h->root.type == bfd_link_hash_common)
1711 {
1712 *override = oldbfd;
1713 newdef = false;
1714 newdyncommon = false;
1715 *pvalue = sym->st_size;
1716 *psec = sec = bed->common_section (oldsec);
1717 *size_change_ok = true;
1718 }
1719
1720 /* Skip weak definitions of symbols that are already defined. */
1721 if (newdef && olddef && newweak)
1722 {
1723 /* Don't skip new non-IR weak syms. */
1724 if (!(oldbfd != NULL
1725 && (oldbfd->flags & BFD_PLUGIN) != 0
1726 && (abfd->flags & BFD_PLUGIN) == 0))
1727 {
1728 newdef = false;
1729 *skip = true;
1730 }
1731
1732 /* Merge st_other. If the symbol already has a dynamic index,
1733 but visibility says it should not be visible, turn it into a
1734 local symbol. */
1735 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1736 if (h->dynindx != -1)
1737 switch (ELF_ST_VISIBILITY (h->other))
1738 {
1739 case STV_INTERNAL:
1740 case STV_HIDDEN:
1741 (*bed->elf_backend_hide_symbol) (info, h, true);
1742 break;
1743 }
1744 }
1745
1746 /* If the old symbol is from a dynamic object, and the new symbol is
1747 a definition which is not from a dynamic object, then the new
1748 symbol overrides the old symbol. Symbols from regular files
1749 always take precedence over symbols from dynamic objects, even if
1750 they are defined after the dynamic object in the link.
1751
1752 As above, we again permit a common symbol in a regular object to
1753 override a definition in a shared object if the shared object
1754 symbol is a function or is weak. */
1755
1756 flip = NULL;
1757 if (!newdyn
1758 && (newdef
1759 || (bfd_is_com_section (sec)
1760 && (oldweak || oldfunc)))
1761 && olddyn
1762 && olddef
1763 && h->def_dynamic)
1764 {
1765 /* Change the hash table entry to undefined, and let
1766 _bfd_generic_link_add_one_symbol do the right thing with the
1767 new definition. */
1768
1769 h->root.type = bfd_link_hash_undefined;
1770 h->root.u.undef.abfd = h->root.u.def.section->owner;
1771 *size_change_ok = true;
1772
1773 olddef = false;
1774 olddyncommon = false;
1775
1776 /* We again permit a type change when a common symbol may be
1777 overriding a function. */
1778
1779 if (bfd_is_com_section (sec))
1780 {
1781 if (oldfunc)
1782 {
1783 /* If a common symbol overrides a function, make sure
1784 that it isn't defined dynamically nor has type
1785 function. */
1786 h->def_dynamic = 0;
1787 h->type = STT_NOTYPE;
1788 }
1789 *type_change_ok = true;
1790 }
1791
1792 if (hi->root.type == bfd_link_hash_indirect)
1793 flip = hi;
1794 else
1795 /* This union may have been set to be non-NULL when this symbol
1796 was seen in a dynamic object. We must force the union to be
1797 NULL, so that it is correct for a regular symbol. */
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 /* Handle the special case of a new common symbol merging with an
1802 old symbol that looks like it might be a common symbol defined in
1803 a shared object. Note that we have already handled the case in
1804 which a new common symbol should simply override the definition
1805 in the shared library. */
1806
1807 if (! newdyn
1808 && bfd_is_com_section (sec)
1809 && olddyncommon)
1810 {
1811 /* It would be best if we could set the hash table entry to a
1812 common symbol, but we don't know what to use for the section
1813 or the alignment. */
1814 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1815 bfd_link_hash_common, sym->st_size);
1816
1817 /* If the presumed common symbol in the dynamic object is
1818 larger, pretend that the new symbol has its size. */
1819
1820 if (h->size > *pvalue)
1821 *pvalue = h->size;
1822
1823 /* We need to remember the alignment required by the symbol
1824 in the dynamic object. */
1825 BFD_ASSERT (pold_alignment);
1826 *pold_alignment = h->root.u.def.section->alignment_power;
1827
1828 olddef = false;
1829 olddyncommon = false;
1830
1831 h->root.type = bfd_link_hash_undefined;
1832 h->root.u.undef.abfd = h->root.u.def.section->owner;
1833
1834 *size_change_ok = true;
1835 *type_change_ok = true;
1836
1837 if (hi->root.type == bfd_link_hash_indirect)
1838 flip = hi;
1839 else
1840 h->verinfo.vertree = NULL;
1841 }
1842
1843 if (flip != NULL)
1844 {
1845 /* Handle the case where we had a versioned symbol in a dynamic
1846 library and now find a definition in a normal object. In this
1847 case, we make the versioned symbol point to the normal one. */
1848 flip->root.type = h->root.type;
1849 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1850 h->root.type = bfd_link_hash_indirect;
1851 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1852 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1853 if (h->def_dynamic)
1854 {
1855 h->def_dynamic = 0;
1856 flip->ref_dynamic = 1;
1857 }
1858 }
1859
1860 return true;
1861 }
1862
1863 /* This function is called to create an indirect symbol from the
1864 default for the symbol with the default version if needed. The
1865 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1866 set DYNSYM if the new indirect symbol is dynamic. */
1867
1868 static bool
1869 _bfd_elf_add_default_symbol (bfd *abfd,
1870 struct bfd_link_info *info,
1871 struct elf_link_hash_entry *h,
1872 const char *name,
1873 Elf_Internal_Sym *sym,
1874 asection *sec,
1875 bfd_vma value,
1876 bfd **poldbfd,
1877 bool *dynsym)
1878 {
1879 bool type_change_ok;
1880 bool size_change_ok;
1881 bool skip;
1882 char *shortname;
1883 struct elf_link_hash_entry *hi;
1884 struct bfd_link_hash_entry *bh;
1885 const struct elf_backend_data *bed;
1886 bool collect;
1887 bool dynamic;
1888 bfd *override;
1889 char *p;
1890 size_t len, shortlen;
1891 asection *tmp_sec;
1892 bool matched;
1893
1894 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1895 return true;
1896
1897 /* If this symbol has a version, and it is the default version, we
1898 create an indirect symbol from the default name to the fully
1899 decorated name. This will cause external references which do not
1900 specify a version to be bound to this version of the symbol. */
1901 p = strchr (name, ELF_VER_CHR);
1902 if (h->versioned == unknown)
1903 {
1904 if (p == NULL)
1905 {
1906 h->versioned = unversioned;
1907 return true;
1908 }
1909 else
1910 {
1911 if (p[1] != ELF_VER_CHR)
1912 {
1913 h->versioned = versioned_hidden;
1914 return true;
1915 }
1916 else
1917 h->versioned = versioned;
1918 }
1919 }
1920 else
1921 {
1922 /* PR ld/19073: We may see an unversioned definition after the
1923 default version. */
1924 if (p == NULL)
1925 return true;
1926 }
1927
1928 bed = get_elf_backend_data (abfd);
1929 collect = bed->collect;
1930 dynamic = (abfd->flags & DYNAMIC) != 0;
1931
1932 shortlen = p - name;
1933 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1934 if (shortname == NULL)
1935 return false;
1936 memcpy (shortname, name, shortlen);
1937 shortname[shortlen] = '\0';
1938
1939 /* We are going to create a new symbol. Merge it with any existing
1940 symbol with this name. For the purposes of the merge, act as
1941 though we were defining the symbol we just defined, although we
1942 actually going to define an indirect symbol. */
1943 type_change_ok = false;
1944 size_change_ok = false;
1945 matched = true;
1946 tmp_sec = sec;
1947 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1948 &hi, poldbfd, NULL, NULL, &skip, &override,
1949 &type_change_ok, &size_change_ok, &matched))
1950 return false;
1951
1952 if (skip)
1953 goto nondefault;
1954
1955 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1956 {
1957 /* If the undecorated symbol will have a version added by a
1958 script different to H, then don't indirect to/from the
1959 undecorated symbol. This isn't ideal because we may not yet
1960 have seen symbol versions, if given by a script on the
1961 command line rather than via --version-script. */
1962 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1963 {
1964 bool hide;
1965
1966 hi->verinfo.vertree
1967 = bfd_find_version_for_sym (info->version_info,
1968 hi->root.root.string, &hide);
1969 if (hi->verinfo.vertree != NULL && hide)
1970 {
1971 (*bed->elf_backend_hide_symbol) (info, hi, true);
1972 goto nondefault;
1973 }
1974 }
1975 if (hi->verinfo.vertree != NULL
1976 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1977 goto nondefault;
1978 }
1979
1980 if (! override)
1981 {
1982 /* Add the default symbol if not performing a relocatable link. */
1983 if (! bfd_link_relocatable (info))
1984 {
1985 bh = &hi->root;
1986 if (bh->type == bfd_link_hash_defined
1987 && bh->u.def.section->owner != NULL
1988 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1989 {
1990 /* Mark the previous definition from IR object as
1991 undefined so that the generic linker will override
1992 it. */
1993 bh->type = bfd_link_hash_undefined;
1994 bh->u.undef.abfd = bh->u.def.section->owner;
1995 }
1996 if (! (_bfd_generic_link_add_one_symbol
1997 (info, abfd, shortname, BSF_INDIRECT,
1998 bfd_ind_section_ptr,
1999 0, name, false, collect, &bh)))
2000 return false;
2001 hi = (struct elf_link_hash_entry *) bh;
2002 }
2003 }
2004 else
2005 {
2006 /* In this case the symbol named SHORTNAME is overriding the
2007 indirect symbol we want to add. We were planning on making
2008 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2009 is the name without a version. NAME is the fully versioned
2010 name, and it is the default version.
2011
2012 Overriding means that we already saw a definition for the
2013 symbol SHORTNAME in a regular object, and it is overriding
2014 the symbol defined in the dynamic object.
2015
2016 When this happens, we actually want to change NAME, the
2017 symbol we just added, to refer to SHORTNAME. This will cause
2018 references to NAME in the shared object to become references
2019 to SHORTNAME in the regular object. This is what we expect
2020 when we override a function in a shared object: that the
2021 references in the shared object will be mapped to the
2022 definition in the regular object. */
2023
2024 while (hi->root.type == bfd_link_hash_indirect
2025 || hi->root.type == bfd_link_hash_warning)
2026 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2027
2028 h->root.type = bfd_link_hash_indirect;
2029 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2030 if (h->def_dynamic)
2031 {
2032 h->def_dynamic = 0;
2033 hi->ref_dynamic = 1;
2034 if (hi->ref_regular
2035 || hi->def_regular)
2036 {
2037 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2038 return false;
2039 }
2040 }
2041
2042 /* Now set HI to H, so that the following code will set the
2043 other fields correctly. */
2044 hi = h;
2045 }
2046
2047 /* Check if HI is a warning symbol. */
2048 if (hi->root.type == bfd_link_hash_warning)
2049 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2050
2051 /* If there is a duplicate definition somewhere, then HI may not
2052 point to an indirect symbol. We will have reported an error to
2053 the user in that case. */
2054
2055 if (hi->root.type == bfd_link_hash_indirect)
2056 {
2057 struct elf_link_hash_entry *ht;
2058
2059 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2060 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2061
2062 /* If we first saw a reference to SHORTNAME with non-default
2063 visibility, merge that visibility to the @@VER symbol. */
2064 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2065
2066 /* A reference to the SHORTNAME symbol from a dynamic library
2067 will be satisfied by the versioned symbol at runtime. In
2068 effect, we have a reference to the versioned symbol. */
2069 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2070 hi->dynamic_def |= ht->dynamic_def;
2071
2072 /* See if the new flags lead us to realize that the symbol must
2073 be dynamic. */
2074 if (! *dynsym)
2075 {
2076 if (! dynamic)
2077 {
2078 if (! bfd_link_executable (info)
2079 || hi->def_dynamic
2080 || hi->ref_dynamic)
2081 *dynsym = true;
2082 }
2083 else
2084 {
2085 if (hi->ref_regular)
2086 *dynsym = true;
2087 }
2088 }
2089 }
2090
2091 /* We also need to define an indirection from the nondefault version
2092 of the symbol. */
2093
2094 nondefault:
2095 len = strlen (name);
2096 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2097 if (shortname == NULL)
2098 return false;
2099 memcpy (shortname, name, shortlen);
2100 memcpy (shortname + shortlen, p + 1, len - shortlen);
2101
2102 /* Once again, merge with any existing symbol. */
2103 type_change_ok = false;
2104 size_change_ok = false;
2105 tmp_sec = sec;
2106 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2107 &hi, poldbfd, NULL, NULL, &skip, &override,
2108 &type_change_ok, &size_change_ok, &matched))
2109 return false;
2110
2111 if (skip)
2112 {
2113 if (!dynamic
2114 && h->root.type == bfd_link_hash_defweak
2115 && hi->root.type == bfd_link_hash_defined)
2116 {
2117 /* We are handling a weak sym@@ver and attempting to define
2118 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119 new weak sym@ver because there is already a strong sym@ver.
2120 However, sym@ver and sym@@ver are really the same symbol.
2121 The existing strong sym@ver ought to override sym@@ver. */
2122 h->root.type = bfd_link_hash_defined;
2123 h->root.u.def.section = hi->root.u.def.section;
2124 h->root.u.def.value = hi->root.u.def.value;
2125 hi->root.type = bfd_link_hash_indirect;
2126 hi->root.u.i.link = &h->root;
2127 }
2128 else
2129 return true;
2130 }
2131 else if (override)
2132 {
2133 /* Here SHORTNAME is a versioned name, so we don't expect to see
2134 the type of override we do in the case above unless it is
2135 overridden by a versioned definition. */
2136 if (hi->root.type != bfd_link_hash_defined
2137 && hi->root.type != bfd_link_hash_defweak)
2138 _bfd_error_handler
2139 /* xgettext:c-format */
2140 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2141 abfd, shortname);
2142 return true;
2143 }
2144 else
2145 {
2146 bh = &hi->root;
2147 if (! (_bfd_generic_link_add_one_symbol
2148 (info, abfd, shortname, BSF_INDIRECT,
2149 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2150 return false;
2151 hi = (struct elf_link_hash_entry *) bh;
2152 }
2153
2154 /* If there is a duplicate definition somewhere, then HI may not
2155 point to an indirect symbol. We will have reported an error
2156 to the user in that case. */
2157 if (hi->root.type == bfd_link_hash_indirect)
2158 {
2159 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2160 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2161 hi->dynamic_def |= h->dynamic_def;
2162
2163 /* If we first saw a reference to @VER symbol with
2164 non-default visibility, merge that visibility to the
2165 @@VER symbol. */
2166 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2167
2168 /* See if the new flags lead us to realize that the symbol
2169 must be dynamic. */
2170 if (! *dynsym)
2171 {
2172 if (! dynamic)
2173 {
2174 if (! bfd_link_executable (info)
2175 || hi->ref_dynamic)
2176 *dynsym = true;
2177 }
2178 else
2179 {
2180 if (hi->ref_regular)
2181 *dynsym = true;
2182 }
2183 }
2184 }
2185
2186 return true;
2187 }
2188 \f
2189 /* This routine is used to export all defined symbols into the dynamic
2190 symbol table. It is called via elf_link_hash_traverse. */
2191
2192 static bool
2193 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2194 {
2195 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2196
2197 /* Ignore indirect symbols. These are added by the versioning code. */
2198 if (h->root.type == bfd_link_hash_indirect)
2199 return true;
2200
2201 /* Ignore this if we won't export it. */
2202 if (!eif->info->export_dynamic && !h->dynamic)
2203 return true;
2204
2205 if (h->dynindx == -1
2206 && (h->def_regular || h->ref_regular)
2207 && ! bfd_hide_sym_by_version (eif->info->version_info,
2208 h->root.root.string))
2209 {
2210 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2211 {
2212 eif->failed = true;
2213 return false;
2214 }
2215 }
2216
2217 return true;
2218 }
2219 \f
2220 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2222 .gnu.version_r section. */
2223
2224 static bool
2225 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2226 {
2227 bfd *glibc_bfd = NULL;
2228 Elf_Internal_Verneed *t;
2229 Elf_Internal_Vernaux *a;
2230 size_t amt;
2231 const char *relr = "GLIBC_ABI_DT_RELR";
2232
2233 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2234 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2235 t != NULL;
2236 t = t->vn_nextref)
2237 {
2238 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2239 /* Skip the shared library if it isn't libc.so. */
2240 if (!soname || !startswith (soname, "libc.so."))
2241 continue;
2242
2243 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2244 {
2245 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2246 added. */
2247 if (a->vna_nodename == relr
2248 || strcmp (a->vna_nodename, relr) == 0)
2249 return true;
2250
2251 /* Check if libc.so provides GLIBC_2.XX version. */
2252 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2253 glibc_bfd = t->vn_bfd;
2254 }
2255
2256 break;
2257 }
2258
2259 /* Skip if it isn't linked against glibc. */
2260 if (glibc_bfd == NULL)
2261 return true;
2262
2263 /* This is a new version. Add it to tree we are building. */
2264 if (t == NULL)
2265 {
2266 amt = sizeof *t;
2267 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2268 amt);
2269 if (t == NULL)
2270 {
2271 rinfo->failed = true;
2272 return false;
2273 }
2274
2275 t->vn_bfd = glibc_bfd;
2276 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2277 elf_tdata (rinfo->info->output_bfd)->verref = t;
2278 }
2279
2280 amt = sizeof *a;
2281 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2282 if (a == NULL)
2283 {
2284 rinfo->failed = true;
2285 return false;
2286 }
2287
2288 a->vna_nodename = relr;
2289 a->vna_flags = 0;
2290 a->vna_nextptr = t->vn_auxptr;
2291 a->vna_other = rinfo->vers + 1;
2292 ++rinfo->vers;
2293
2294 t->vn_auxptr = a;
2295
2296 return true;
2297 }
2298
2299 /* Look through the symbols which are defined in other shared
2300 libraries and referenced here. Update the list of version
2301 dependencies. This will be put into the .gnu.version_r section.
2302 This function is called via elf_link_hash_traverse. */
2303
2304 static bool
2305 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2306 void *data)
2307 {
2308 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2309 Elf_Internal_Verneed *t;
2310 Elf_Internal_Vernaux *a;
2311 size_t amt;
2312
2313 /* We only care about symbols defined in shared objects with version
2314 information. */
2315 if (!h->def_dynamic
2316 || h->def_regular
2317 || h->dynindx == -1
2318 || h->verinfo.verdef == NULL
2319 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2320 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2321 return true;
2322
2323 /* See if we already know about this version. */
2324 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2325 t != NULL;
2326 t = t->vn_nextref)
2327 {
2328 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2329 continue;
2330
2331 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2332 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2333 return true;
2334
2335 break;
2336 }
2337
2338 /* This is a new version. Add it to tree we are building. */
2339
2340 if (t == NULL)
2341 {
2342 amt = sizeof *t;
2343 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2344 if (t == NULL)
2345 {
2346 rinfo->failed = true;
2347 return false;
2348 }
2349
2350 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2351 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2352 elf_tdata (rinfo->info->output_bfd)->verref = t;
2353 }
2354
2355 amt = sizeof *a;
2356 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2357 if (a == NULL)
2358 {
2359 rinfo->failed = true;
2360 return false;
2361 }
2362
2363 /* Note that we are copying a string pointer here, and testing it
2364 above. If bfd_elf_string_from_elf_section is ever changed to
2365 discard the string data when low in memory, this will have to be
2366 fixed. */
2367 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2368
2369 a->vna_flags = h->verinfo.verdef->vd_flags;
2370 a->vna_nextptr = t->vn_auxptr;
2371
2372 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2373 ++rinfo->vers;
2374
2375 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2376
2377 t->vn_auxptr = a;
2378
2379 return true;
2380 }
2381
2382 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383 hidden. Set *T_P to NULL if there is no match. */
2384
2385 static bool
2386 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2387 struct elf_link_hash_entry *h,
2388 const char *version_p,
2389 struct bfd_elf_version_tree **t_p,
2390 bool *hide)
2391 {
2392 struct bfd_elf_version_tree *t;
2393
2394 /* Look for the version. If we find it, it is no longer weak. */
2395 for (t = info->version_info; t != NULL; t = t->next)
2396 {
2397 if (strcmp (t->name, version_p) == 0)
2398 {
2399 size_t len;
2400 char *alc;
2401 struct bfd_elf_version_expr *d;
2402
2403 len = version_p - h->root.root.string;
2404 alc = (char *) bfd_malloc (len);
2405 if (alc == NULL)
2406 return false;
2407 memcpy (alc, h->root.root.string, len - 1);
2408 alc[len - 1] = '\0';
2409 if (alc[len - 2] == ELF_VER_CHR)
2410 alc[len - 2] = '\0';
2411
2412 h->verinfo.vertree = t;
2413 t->used = true;
2414 d = NULL;
2415
2416 if (t->globals.list != NULL)
2417 d = (*t->match) (&t->globals, NULL, alc);
2418
2419 /* See if there is anything to force this symbol to
2420 local scope. */
2421 if (d == NULL && t->locals.list != NULL)
2422 {
2423 d = (*t->match) (&t->locals, NULL, alc);
2424 if (d != NULL
2425 && h->dynindx != -1
2426 && ! info->export_dynamic)
2427 *hide = true;
2428 }
2429
2430 free (alc);
2431 break;
2432 }
2433 }
2434
2435 *t_p = t;
2436
2437 return true;
2438 }
2439
2440 /* Return TRUE if the symbol H is hidden by version script. */
2441
2442 bool
2443 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2444 struct elf_link_hash_entry *h)
2445 {
2446 const char *p;
2447 bool hide = false;
2448 const struct elf_backend_data *bed
2449 = get_elf_backend_data (info->output_bfd);
2450
2451 /* Version script only hides symbols defined in regular objects. */
2452 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2453 return true;
2454
2455 p = strchr (h->root.root.string, ELF_VER_CHR);
2456 if (p != NULL && h->verinfo.vertree == NULL)
2457 {
2458 struct bfd_elf_version_tree *t;
2459
2460 ++p;
2461 if (*p == ELF_VER_CHR)
2462 ++p;
2463
2464 if (*p != '\0'
2465 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2466 && hide)
2467 {
2468 if (hide)
2469 (*bed->elf_backend_hide_symbol) (info, h, true);
2470 return true;
2471 }
2472 }
2473
2474 /* If we don't have a version for this symbol, see if we can find
2475 something. */
2476 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2477 {
2478 h->verinfo.vertree
2479 = bfd_find_version_for_sym (info->version_info,
2480 h->root.root.string, &hide);
2481 if (h->verinfo.vertree != NULL && hide)
2482 {
2483 (*bed->elf_backend_hide_symbol) (info, h, true);
2484 return true;
2485 }
2486 }
2487
2488 return false;
2489 }
2490
2491 /* Figure out appropriate versions for all the symbols. We may not
2492 have the version number script until we have read all of the input
2493 files, so until that point we don't know which symbols should be
2494 local. This function is called via elf_link_hash_traverse. */
2495
2496 static bool
2497 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2498 {
2499 struct elf_info_failed *sinfo;
2500 struct bfd_link_info *info;
2501 const struct elf_backend_data *bed;
2502 struct elf_info_failed eif;
2503 char *p;
2504 bool hide;
2505
2506 sinfo = (struct elf_info_failed *) data;
2507 info = sinfo->info;
2508
2509 /* Fix the symbol flags. */
2510 eif.failed = false;
2511 eif.info = info;
2512 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2513 {
2514 if (eif.failed)
2515 sinfo->failed = true;
2516 return false;
2517 }
2518
2519 bed = get_elf_backend_data (info->output_bfd);
2520
2521 /* We only need version numbers for symbols defined in regular
2522 objects. */
2523 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2524 {
2525 /* Hide symbols defined in discarded input sections. */
2526 if ((h->root.type == bfd_link_hash_defined
2527 || h->root.type == bfd_link_hash_defweak)
2528 && discarded_section (h->root.u.def.section))
2529 (*bed->elf_backend_hide_symbol) (info, h, true);
2530 return true;
2531 }
2532
2533 hide = false;
2534 p = strchr (h->root.root.string, ELF_VER_CHR);
2535 if (p != NULL && h->verinfo.vertree == NULL)
2536 {
2537 struct bfd_elf_version_tree *t;
2538
2539 ++p;
2540 if (*p == ELF_VER_CHR)
2541 ++p;
2542
2543 /* If there is no version string, we can just return out. */
2544 if (*p == '\0')
2545 return true;
2546
2547 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2548 {
2549 sinfo->failed = true;
2550 return false;
2551 }
2552
2553 if (hide)
2554 (*bed->elf_backend_hide_symbol) (info, h, true);
2555
2556 /* If we are building an application, we need to create a
2557 version node for this version. */
2558 if (t == NULL && bfd_link_executable (info))
2559 {
2560 struct bfd_elf_version_tree **pp;
2561 int version_index;
2562
2563 /* If we aren't going to export this symbol, we don't need
2564 to worry about it. */
2565 if (h->dynindx == -1)
2566 return true;
2567
2568 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2569 sizeof *t);
2570 if (t == NULL)
2571 {
2572 sinfo->failed = true;
2573 return false;
2574 }
2575
2576 t->name = p;
2577 t->name_indx = (unsigned int) -1;
2578 t->used = true;
2579
2580 version_index = 1;
2581 /* Don't count anonymous version tag. */
2582 if (sinfo->info->version_info != NULL
2583 && sinfo->info->version_info->vernum == 0)
2584 version_index = 0;
2585 for (pp = &sinfo->info->version_info;
2586 *pp != NULL;
2587 pp = &(*pp)->next)
2588 ++version_index;
2589 t->vernum = version_index;
2590
2591 *pp = t;
2592
2593 h->verinfo.vertree = t;
2594 }
2595 else if (t == NULL)
2596 {
2597 /* We could not find the version for a symbol when
2598 generating a shared archive. Return an error. */
2599 _bfd_error_handler
2600 /* xgettext:c-format */
2601 (_("%pB: version node not found for symbol %s"),
2602 info->output_bfd, h->root.root.string);
2603 bfd_set_error (bfd_error_bad_value);
2604 sinfo->failed = true;
2605 return false;
2606 }
2607 }
2608
2609 /* If we don't have a version for this symbol, see if we can find
2610 something. */
2611 if (!hide
2612 && h->verinfo.vertree == NULL
2613 && sinfo->info->version_info != NULL)
2614 {
2615 h->verinfo.vertree
2616 = bfd_find_version_for_sym (sinfo->info->version_info,
2617 h->root.root.string, &hide);
2618 if (h->verinfo.vertree != NULL && hide)
2619 (*bed->elf_backend_hide_symbol) (info, h, true);
2620 }
2621
2622 return true;
2623 }
2624 \f
2625 /* Read and swap the relocs from the section indicated by SHDR. This
2626 may be either a REL or a RELA section. The relocations are
2627 translated into RELA relocations and stored in INTERNAL_RELOCS,
2628 which should have already been allocated to contain enough space.
2629 The EXTERNAL_RELOCS are a buffer where the external form of the
2630 relocations should be stored.
2631
2632 Returns FALSE if something goes wrong. */
2633
2634 static bool
2635 elf_link_read_relocs_from_section (bfd *abfd,
2636 asection *sec,
2637 Elf_Internal_Shdr *shdr,
2638 void *external_relocs,
2639 Elf_Internal_Rela *internal_relocs)
2640 {
2641 const struct elf_backend_data *bed;
2642 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2643 const bfd_byte *erela;
2644 const bfd_byte *erelaend;
2645 Elf_Internal_Rela *irela;
2646 Elf_Internal_Shdr *symtab_hdr;
2647 size_t nsyms;
2648
2649 /* Position ourselves at the start of the section. */
2650 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2651 return false;
2652
2653 /* Read the relocations. */
2654 if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2655 return false;
2656
2657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2658 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2659
2660 bed = get_elf_backend_data (abfd);
2661
2662 /* Convert the external relocations to the internal format. */
2663 if (shdr->sh_entsize == bed->s->sizeof_rel)
2664 swap_in = bed->s->swap_reloc_in;
2665 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2666 swap_in = bed->s->swap_reloca_in;
2667 else
2668 {
2669 bfd_set_error (bfd_error_wrong_format);
2670 return false;
2671 }
2672
2673 erela = (const bfd_byte *) external_relocs;
2674 /* Setting erelaend like this and comparing with <= handles case of
2675 a fuzzed object with sh_size not a multiple of sh_entsize. */
2676 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2677 irela = internal_relocs;
2678 while (erela <= erelaend)
2679 {
2680 bfd_vma r_symndx;
2681
2682 (*swap_in) (abfd, erela, irela);
2683 r_symndx = ELF32_R_SYM (irela->r_info);
2684 if (bed->s->arch_size == 64)
2685 r_symndx >>= 24;
2686 if (nsyms > 0)
2687 {
2688 if ((size_t) r_symndx >= nsyms)
2689 {
2690 _bfd_error_handler
2691 /* xgettext:c-format */
2692 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2693 " for offset %#" PRIx64 " in section `%pA'"),
2694 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2695 (uint64_t) irela->r_offset, sec);
2696 bfd_set_error (bfd_error_bad_value);
2697 return false;
2698 }
2699 }
2700 else if (r_symndx != STN_UNDEF)
2701 {
2702 _bfd_error_handler
2703 /* xgettext:c-format */
2704 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2705 " for offset %#" PRIx64 " in section `%pA'"
2706 " when the object file has no symbol table"),
2707 abfd, (uint64_t) r_symndx,
2708 (uint64_t) irela->r_offset, sec);
2709 bfd_set_error (bfd_error_bad_value);
2710 return false;
2711 }
2712 irela += bed->s->int_rels_per_ext_rel;
2713 erela += shdr->sh_entsize;
2714 }
2715
2716 return true;
2717 }
2718
2719 /* Read and swap the relocs for a section O. They may have been
2720 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721 not NULL, they are used as buffers to read into. They are known to
2722 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2723 the return value is allocated using either malloc or bfd_alloc,
2724 according to the KEEP_MEMORY argument. If O has two relocation
2725 sections (both REL and RELA relocations), then the REL_HDR
2726 relocations will appear first in INTERNAL_RELOCS, followed by the
2727 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2728 update cache_size. */
2729
2730 Elf_Internal_Rela *
2731 _bfd_elf_link_info_read_relocs (bfd *abfd,
2732 struct bfd_link_info *info,
2733 asection *o,
2734 void *external_relocs,
2735 Elf_Internal_Rela *internal_relocs,
2736 bool keep_memory)
2737 {
2738 void *alloc1 = NULL;
2739 Elf_Internal_Rela *alloc2 = NULL;
2740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2741 struct bfd_elf_section_data *esdo = elf_section_data (o);
2742 Elf_Internal_Rela *internal_rela_relocs;
2743
2744 if (esdo->relocs != NULL)
2745 return esdo->relocs;
2746
2747 if (o->reloc_count == 0)
2748 return NULL;
2749
2750 if (internal_relocs == NULL)
2751 {
2752 bfd_size_type size;
2753
2754 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2755 if (keep_memory)
2756 {
2757 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2758 if (info)
2759 info->cache_size += size;
2760 }
2761 else
2762 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2763 if (internal_relocs == NULL)
2764 goto error_return;
2765 }
2766
2767 if (external_relocs == NULL)
2768 {
2769 bfd_size_type size = 0;
2770
2771 if (esdo->rel.hdr)
2772 size += esdo->rel.hdr->sh_size;
2773 if (esdo->rela.hdr)
2774 size += esdo->rela.hdr->sh_size;
2775
2776 alloc1 = bfd_malloc (size);
2777 if (alloc1 == NULL)
2778 goto error_return;
2779 external_relocs = alloc1;
2780 }
2781
2782 internal_rela_relocs = internal_relocs;
2783 if (esdo->rel.hdr)
2784 {
2785 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2786 external_relocs,
2787 internal_relocs))
2788 goto error_return;
2789 external_relocs = (((bfd_byte *) external_relocs)
2790 + esdo->rel.hdr->sh_size);
2791 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2792 * bed->s->int_rels_per_ext_rel);
2793 }
2794
2795 if (esdo->rela.hdr
2796 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2797 external_relocs,
2798 internal_rela_relocs)))
2799 goto error_return;
2800
2801 /* Cache the results for next time, if we can. */
2802 if (keep_memory)
2803 esdo->relocs = internal_relocs;
2804
2805 free (alloc1);
2806
2807 /* Don't free alloc2, since if it was allocated we are passing it
2808 back (under the name of internal_relocs). */
2809
2810 return internal_relocs;
2811
2812 error_return:
2813 free (alloc1);
2814 if (alloc2 != NULL)
2815 {
2816 if (keep_memory)
2817 bfd_release (abfd, alloc2);
2818 else
2819 free (alloc2);
2820 }
2821 return NULL;
2822 }
2823
2824 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826 struct bfd_link_info. */
2827
2828 Elf_Internal_Rela *
2829 _bfd_elf_link_read_relocs (bfd *abfd,
2830 asection *o,
2831 void *external_relocs,
2832 Elf_Internal_Rela *internal_relocs,
2833 bool keep_memory)
2834 {
2835 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2836 internal_relocs, keep_memory);
2837
2838 }
2839
2840 /* Compute the size of, and allocate space for, REL_HDR which is the
2841 section header for a section containing relocations for O. */
2842
2843 static bool
2844 _bfd_elf_link_size_reloc_section (bfd *abfd,
2845 struct bfd_elf_section_reloc_data *reldata)
2846 {
2847 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2848
2849 /* That allows us to calculate the size of the section. */
2850 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2851
2852 /* The contents field must last into write_object_contents, so we
2853 allocate it with bfd_alloc rather than malloc. Also since we
2854 cannot be sure that the contents will actually be filled in,
2855 we zero the allocated space. */
2856 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2857 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2858 return false;
2859
2860 if (reldata->hashes == NULL && reldata->count)
2861 {
2862 struct elf_link_hash_entry **p;
2863
2864 p = ((struct elf_link_hash_entry **)
2865 bfd_zmalloc (reldata->count * sizeof (*p)));
2866 if (p == NULL)
2867 return false;
2868
2869 reldata->hashes = p;
2870 }
2871
2872 return true;
2873 }
2874
2875 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876 originated from the section given by INPUT_REL_HDR) to the
2877 OUTPUT_BFD. */
2878
2879 bool
2880 _bfd_elf_link_output_relocs (bfd *output_bfd,
2881 asection *input_section,
2882 Elf_Internal_Shdr *input_rel_hdr,
2883 Elf_Internal_Rela *internal_relocs,
2884 struct elf_link_hash_entry **rel_hash
2885 ATTRIBUTE_UNUSED)
2886 {
2887 Elf_Internal_Rela *irela;
2888 Elf_Internal_Rela *irelaend;
2889 bfd_byte *erel;
2890 struct bfd_elf_section_reloc_data *output_reldata;
2891 asection *output_section;
2892 const struct elf_backend_data *bed;
2893 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2894 struct bfd_elf_section_data *esdo;
2895
2896 output_section = input_section->output_section;
2897
2898 bed = get_elf_backend_data (output_bfd);
2899 esdo = elf_section_data (output_section);
2900 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2901 {
2902 output_reldata = &esdo->rel;
2903 swap_out = bed->s->swap_reloc_out;
2904 }
2905 else if (esdo->rela.hdr
2906 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2907 {
2908 output_reldata = &esdo->rela;
2909 swap_out = bed->s->swap_reloca_out;
2910 }
2911 else
2912 {
2913 _bfd_error_handler
2914 /* xgettext:c-format */
2915 (_("%pB: relocation size mismatch in %pB section %pA"),
2916 output_bfd, input_section->owner, input_section);
2917 bfd_set_error (bfd_error_wrong_format);
2918 return false;
2919 }
2920
2921 erel = output_reldata->hdr->contents;
2922 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2923 irela = internal_relocs;
2924 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2925 * bed->s->int_rels_per_ext_rel);
2926 while (irela < irelaend)
2927 {
2928 (*swap_out) (output_bfd, irela, erel);
2929 irela += bed->s->int_rels_per_ext_rel;
2930 erel += input_rel_hdr->sh_entsize;
2931 }
2932
2933 /* Bump the counter, so that we know where to add the next set of
2934 relocations. */
2935 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2936
2937 return true;
2938 }
2939 \f
2940 /* Make weak undefined symbols in PIE dynamic. */
2941
2942 bool
2943 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2944 struct elf_link_hash_entry *h)
2945 {
2946 if (bfd_link_pie (info)
2947 && h->dynindx == -1
2948 && h->root.type == bfd_link_hash_undefweak)
2949 return bfd_elf_link_record_dynamic_symbol (info, h);
2950
2951 return true;
2952 }
2953
2954 /* Fix up the flags for a symbol. This handles various cases which
2955 can only be fixed after all the input files are seen. This is
2956 currently called by both adjust_dynamic_symbol and
2957 assign_sym_version, which is unnecessary but perhaps more robust in
2958 the face of future changes. */
2959
2960 static bool
2961 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2962 struct elf_info_failed *eif)
2963 {
2964 const struct elf_backend_data *bed;
2965
2966 /* If this symbol was mentioned in a non-ELF file, try to set
2967 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2968 permit a non-ELF file to correctly refer to a symbol defined in
2969 an ELF dynamic object. */
2970 if (h->non_elf)
2971 {
2972 while (h->root.type == bfd_link_hash_indirect)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975 if (h->root.type != bfd_link_hash_defined
2976 && h->root.type != bfd_link_hash_defweak)
2977 {
2978 h->ref_regular = 1;
2979 h->ref_regular_nonweak = 1;
2980 }
2981 else
2982 {
2983 if (h->root.u.def.section->owner != NULL
2984 && (bfd_get_flavour (h->root.u.def.section->owner)
2985 == bfd_target_elf_flavour))
2986 {
2987 h->ref_regular = 1;
2988 h->ref_regular_nonweak = 1;
2989 }
2990 else
2991 h->def_regular = 1;
2992 }
2993
2994 if (h->dynindx == -1
2995 && (h->def_dynamic
2996 || h->ref_dynamic))
2997 {
2998 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999 {
3000 eif->failed = true;
3001 return false;
3002 }
3003 }
3004 }
3005 else
3006 {
3007 /* Unfortunately, NON_ELF is only correct if the symbol
3008 was first seen in a non-ELF file. Fortunately, if the symbol
3009 was first seen in an ELF file, we're probably OK unless the
3010 symbol was defined in a non-ELF file. Catch that case here.
3011 FIXME: We're still in trouble if the symbol was first seen in
3012 a dynamic object, and then later in a non-ELF regular object. */
3013 if ((h->root.type == bfd_link_hash_defined
3014 || h->root.type == bfd_link_hash_defweak)
3015 && !h->def_regular
3016 && (h->root.u.def.section->owner != NULL
3017 ? (bfd_get_flavour (h->root.u.def.section->owner)
3018 != bfd_target_elf_flavour)
3019 : (bfd_is_abs_section (h->root.u.def.section)
3020 && !h->def_dynamic)))
3021 h->def_regular = 1;
3022 }
3023
3024 /* Backend specific symbol fixup. */
3025 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3026 if (bed->elf_backend_fixup_symbol
3027 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3028 return false;
3029
3030 /* If this is a final link, and the symbol was defined as a common
3031 symbol in a regular object file, and there was no definition in
3032 any dynamic object, then the linker will have allocated space for
3033 the symbol in a common section but the DEF_REGULAR
3034 flag will not have been set. */
3035 if (h->root.type == bfd_link_hash_defined
3036 && !h->def_regular
3037 && h->ref_regular
3038 && !h->def_dynamic
3039 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3040 h->def_regular = 1;
3041
3042 /* Symbols defined in discarded sections shouldn't be dynamic. */
3043 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3045
3046 /* If a weak undefined symbol has non-default visibility, we also
3047 hide it from the dynamic linker. */
3048 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3049 && h->root.type == bfd_link_hash_undefweak)
3050 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3051
3052 /* A hidden versioned symbol in executable should be forced local if
3053 it is is locally defined, not referenced by shared library and not
3054 exported. */
3055 else if (bfd_link_executable (eif->info)
3056 && h->versioned == versioned_hidden
3057 && !eif->info->export_dynamic
3058 && !h->dynamic
3059 && !h->ref_dynamic
3060 && h->def_regular)
3061 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3062
3063 /* If -Bsymbolic was used (which means to bind references to global
3064 symbols to the definition within the shared object), and this
3065 symbol was defined in a regular object, then it actually doesn't
3066 need a PLT entry. Likewise, if the symbol has non-default
3067 visibility. If the symbol has hidden or internal visibility, we
3068 will force it local. */
3069 else if (h->needs_plt
3070 && bfd_link_pic (eif->info)
3071 && is_elf_hash_table (eif->info->hash)
3072 && (SYMBOLIC_BIND (eif->info, h)
3073 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3074 && h->def_regular)
3075 {
3076 bool force_local;
3077
3078 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3079 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3080 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3081 }
3082
3083 /* If this is a weak defined symbol in a dynamic object, and we know
3084 the real definition in the dynamic object, copy interesting flags
3085 over to the real definition. */
3086 if (h->is_weakalias)
3087 {
3088 struct elf_link_hash_entry *def = weakdef (h);
3089
3090 /* If the real definition is defined by a regular object file,
3091 don't do anything special. See the longer description in
3092 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3093 bfd_link_hash_defined as it was when put on the alias list
3094 then it must have originally been a versioned symbol (for
3095 which a non-versioned indirect symbol is created) and later
3096 a definition for the non-versioned symbol is found. In that
3097 case the indirection is flipped with the versioned symbol
3098 becoming an indirect pointing at the non-versioned symbol.
3099 Thus, not an alias any more. */
3100 if (def->def_regular
3101 || def->root.type != bfd_link_hash_defined)
3102 {
3103 h = def;
3104 while ((h = h->u.alias) != def)
3105 h->is_weakalias = 0;
3106 }
3107 else
3108 {
3109 while (h->root.type == bfd_link_hash_indirect)
3110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3111 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3112 || h->root.type == bfd_link_hash_defweak);
3113 BFD_ASSERT (def->def_dynamic);
3114 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3115 }
3116 }
3117
3118 return true;
3119 }
3120
3121 /* Make the backend pick a good value for a dynamic symbol. This is
3122 called via elf_link_hash_traverse, and also calls itself
3123 recursively. */
3124
3125 static bool
3126 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3127 {
3128 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3129 struct elf_link_hash_table *htab;
3130 const struct elf_backend_data *bed;
3131
3132 if (! is_elf_hash_table (eif->info->hash))
3133 return false;
3134
3135 /* Ignore indirect symbols. These are added by the versioning code. */
3136 if (h->root.type == bfd_link_hash_indirect)
3137 return true;
3138
3139 /* Fix the symbol flags. */
3140 if (! _bfd_elf_fix_symbol_flags (h, eif))
3141 return false;
3142
3143 htab = elf_hash_table (eif->info);
3144 bed = get_elf_backend_data (htab->dynobj);
3145
3146 if (h->root.type == bfd_link_hash_undefweak)
3147 {
3148 if (eif->info->dynamic_undefined_weak == 0)
3149 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3150 else if (eif->info->dynamic_undefined_weak > 0
3151 && h->ref_regular
3152 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3153 && !bfd_hide_sym_by_version (eif->info->version_info,
3154 h->root.root.string))
3155 {
3156 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3157 {
3158 eif->failed = true;
3159 return false;
3160 }
3161 }
3162 }
3163
3164 /* If this symbol does not require a PLT entry, and it is not
3165 defined by a dynamic object, or is not referenced by a regular
3166 object, ignore it. We do have to handle a weak defined symbol,
3167 even if no regular object refers to it, if we decided to add it
3168 to the dynamic symbol table. FIXME: Do we normally need to worry
3169 about symbols which are defined by one dynamic object and
3170 referenced by another one? */
3171 if (!h->needs_plt
3172 && h->type != STT_GNU_IFUNC
3173 && (h->def_regular
3174 || !h->def_dynamic
3175 || (!h->ref_regular
3176 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3177 {
3178 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3179 return true;
3180 }
3181
3182 /* If we've already adjusted this symbol, don't do it again. This
3183 can happen via a recursive call. */
3184 if (h->dynamic_adjusted)
3185 return true;
3186
3187 /* Don't look at this symbol again. Note that we must set this
3188 after checking the above conditions, because we may look at a
3189 symbol once, decide not to do anything, and then get called
3190 recursively later after REF_REGULAR is set below. */
3191 h->dynamic_adjusted = 1;
3192
3193 /* If this is a weak definition, and we know a real definition, and
3194 the real symbol is not itself defined by a regular object file,
3195 then get a good value for the real definition. We handle the
3196 real symbol first, for the convenience of the backend routine.
3197
3198 Note that there is a confusing case here. If the real definition
3199 is defined by a regular object file, we don't get the real symbol
3200 from the dynamic object, but we do get the weak symbol. If the
3201 processor backend uses a COPY reloc, then if some routine in the
3202 dynamic object changes the real symbol, we will not see that
3203 change in the corresponding weak symbol. This is the way other
3204 ELF linkers work as well, and seems to be a result of the shared
3205 library model.
3206
3207 I will clarify this issue. Most SVR4 shared libraries define the
3208 variable _timezone and define timezone as a weak synonym. The
3209 tzset call changes _timezone. If you write
3210 extern int timezone;
3211 int _timezone = 5;
3212 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3213 you might expect that, since timezone is a synonym for _timezone,
3214 the same number will print both times. However, if the processor
3215 backend uses a COPY reloc, then actually timezone will be copied
3216 into your process image, and, since you define _timezone
3217 yourself, _timezone will not. Thus timezone and _timezone will
3218 wind up at different memory locations. The tzset call will set
3219 _timezone, leaving timezone unchanged. */
3220
3221 if (h->is_weakalias)
3222 {
3223 struct elf_link_hash_entry *def = weakdef (h);
3224
3225 /* If we get to this point, there is an implicit reference to
3226 the alias by a regular object file via the weak symbol H. */
3227 def->ref_regular = 1;
3228
3229 /* Ensure that the backend adjust_dynamic_symbol function sees
3230 the strong alias before H by recursively calling ourselves. */
3231 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3232 return false;
3233 }
3234
3235 /* If a symbol has no type and no size and does not require a PLT
3236 entry, then we are probably about to do the wrong thing here: we
3237 are probably going to create a COPY reloc for an empty object.
3238 This case can arise when a shared object is built with assembly
3239 code, and the assembly code fails to set the symbol type. */
3240 if (h->size == 0
3241 && h->type == STT_NOTYPE
3242 && !h->needs_plt)
3243 _bfd_error_handler
3244 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3245 h->root.root.string);
3246
3247 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3248 {
3249 eif->failed = true;
3250 return false;
3251 }
3252
3253 return true;
3254 }
3255
3256 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3257 DYNBSS. */
3258
3259 bool
3260 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3261 struct elf_link_hash_entry *h,
3262 asection *dynbss)
3263 {
3264 unsigned int power_of_two;
3265 bfd_vma mask;
3266 asection *sec = h->root.u.def.section;
3267
3268 /* The section alignment of the definition is the maximum alignment
3269 requirement of symbols defined in the section. Since we don't
3270 know the symbol alignment requirement, we start with the
3271 maximum alignment and check low bits of the symbol address
3272 for the minimum alignment. */
3273 power_of_two = bfd_section_alignment (sec);
3274 mask = ((bfd_vma) 1 << power_of_two) - 1;
3275 while ((h->root.u.def.value & mask) != 0)
3276 {
3277 mask >>= 1;
3278 --power_of_two;
3279 }
3280
3281 if (power_of_two > bfd_section_alignment (dynbss))
3282 {
3283 /* Adjust the section alignment if needed. */
3284 if (!bfd_set_section_alignment (dynbss, power_of_two))
3285 return false;
3286 }
3287
3288 /* We make sure that the symbol will be aligned properly. */
3289 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3290
3291 /* Define the symbol as being at this point in DYNBSS. */
3292 h->root.u.def.section = dynbss;
3293 h->root.u.def.value = dynbss->size;
3294
3295 /* Increment the size of DYNBSS to make room for the symbol. */
3296 dynbss->size += h->size;
3297
3298 /* No error if extern_protected_data is true. */
3299 if (h->protected_def
3300 && (!info->extern_protected_data
3301 || (info->extern_protected_data < 0
3302 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3303 info->callbacks->einfo
3304 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3305 h->root.root.string);
3306
3307 return true;
3308 }
3309
3310 /* Adjust all external symbols pointing into SEC_MERGE sections
3311 to reflect the object merging within the sections. */
3312
3313 static bool
3314 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3315 {
3316 asection *sec;
3317
3318 if ((h->root.type == bfd_link_hash_defined
3319 || h->root.type == bfd_link_hash_defweak)
3320 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3321 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3322 {
3323 bfd *output_bfd = (bfd *) data;
3324
3325 h->root.u.def.value =
3326 _bfd_merged_section_offset (output_bfd,
3327 &h->root.u.def.section,
3328 elf_section_data (sec)->sec_info,
3329 h->root.u.def.value);
3330 }
3331
3332 return true;
3333 }
3334
3335 /* Returns false if the symbol referred to by H should be considered
3336 to resolve local to the current module, and true if it should be
3337 considered to bind dynamically. */
3338
3339 bool
3340 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3341 struct bfd_link_info *info,
3342 bool not_local_protected)
3343 {
3344 bool binding_stays_local_p;
3345 const struct elf_backend_data *bed;
3346 struct elf_link_hash_table *hash_table;
3347
3348 if (h == NULL)
3349 return false;
3350
3351 while (h->root.type == bfd_link_hash_indirect
3352 || h->root.type == bfd_link_hash_warning)
3353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3354
3355 /* If it was forced local, then clearly it's not dynamic. */
3356 if (h->dynindx == -1)
3357 return false;
3358 if (h->forced_local)
3359 return false;
3360
3361 /* Identify the cases where name binding rules say that a
3362 visible symbol resolves locally. */
3363 binding_stays_local_p = (bfd_link_executable (info)
3364 || SYMBOLIC_BIND (info, h));
3365
3366 switch (ELF_ST_VISIBILITY (h->other))
3367 {
3368 case STV_INTERNAL:
3369 case STV_HIDDEN:
3370 return false;
3371
3372 case STV_PROTECTED:
3373 hash_table = elf_hash_table (info);
3374 if (!is_elf_hash_table (&hash_table->root))
3375 return false;
3376
3377 bed = get_elf_backend_data (hash_table->dynobj);
3378
3379 /* Proper resolution for function pointer equality may require
3380 that these symbols perhaps be resolved dynamically, even though
3381 we should be resolving them to the current module. */
3382 if (!not_local_protected || !bed->is_function_type (h->type))
3383 binding_stays_local_p = true;
3384 break;
3385
3386 default:
3387 break;
3388 }
3389
3390 /* If it isn't defined locally, then clearly it's dynamic. */
3391 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3392 return true;
3393
3394 /* Otherwise, the symbol is dynamic if binding rules don't tell
3395 us that it remains local. */
3396 return !binding_stays_local_p;
3397 }
3398
3399 /* Return true if the symbol referred to by H should be considered
3400 to resolve local to the current module, and false otherwise. Differs
3401 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3402 undefined symbols. The two functions are virtually identical except
3403 for the place where dynindx == -1 is tested. If that test is true,
3404 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3405 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3406 defined symbols.
3407 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3408 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3409 treatment of undefined weak symbols. For those that do not make
3410 undefined weak symbols dynamic, both functions may return false. */
3411
3412 bool
3413 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3414 struct bfd_link_info *info,
3415 bool local_protected)
3416 {
3417 const struct elf_backend_data *bed;
3418 struct elf_link_hash_table *hash_table;
3419
3420 /* If it's a local sym, of course we resolve locally. */
3421 if (h == NULL)
3422 return true;
3423
3424 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3425 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3426 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3427 return true;
3428
3429 /* Forced local symbols resolve locally. */
3430 if (h->forced_local)
3431 return true;
3432
3433 /* Common symbols that become definitions don't get the DEF_REGULAR
3434 flag set, so test it first, and don't bail out. */
3435 if (ELF_COMMON_DEF_P (h))
3436 /* Do nothing. */;
3437 /* If we don't have a definition in a regular file, then we can't
3438 resolve locally. The sym is either undefined or dynamic. */
3439 else if (!h->def_regular)
3440 return false;
3441
3442 /* Non-dynamic symbols resolve locally. */
3443 if (h->dynindx == -1)
3444 return true;
3445
3446 /* At this point, we know the symbol is defined and dynamic. In an
3447 executable it must resolve locally, likewise when building symbolic
3448 shared libraries. */
3449 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3450 return true;
3451
3452 /* Now deal with defined dynamic symbols in shared libraries. Ones
3453 with default visibility might not resolve locally. */
3454 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3455 return false;
3456
3457 hash_table = elf_hash_table (info);
3458 if (!is_elf_hash_table (&hash_table->root))
3459 return true;
3460
3461 /* STV_PROTECTED symbols with indirect external access are local. */
3462 if (info->indirect_extern_access > 0)
3463 return true;
3464
3465 bed = get_elf_backend_data (hash_table->dynobj);
3466
3467 /* If extern_protected_data is false, STV_PROTECTED non-function
3468 symbols are local. */
3469 if ((!info->extern_protected_data
3470 || (info->extern_protected_data < 0
3471 && !bed->extern_protected_data))
3472 && !bed->is_function_type (h->type))
3473 return true;
3474
3475 /* Function pointer equality tests may require that STV_PROTECTED
3476 symbols be treated as dynamic symbols. If the address of a
3477 function not defined in an executable is set to that function's
3478 plt entry in the executable, then the address of the function in
3479 a shared library must also be the plt entry in the executable. */
3480 return local_protected;
3481 }
3482
3483 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3484 aligned. Returns the first TLS output section. */
3485
3486 struct bfd_section *
3487 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3488 {
3489 struct bfd_section *sec, *tls;
3490 unsigned int align = 0;
3491
3492 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3493 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3494 break;
3495 tls = sec;
3496
3497 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3498 if (sec->alignment_power > align)
3499 align = sec->alignment_power;
3500
3501 elf_hash_table (info)->tls_sec = tls;
3502
3503 /* Ensure the alignment of the first section (usually .tdata) is the largest
3504 alignment, so that the tls segment starts aligned. */
3505 if (tls != NULL)
3506 tls->alignment_power = align;
3507
3508 return tls;
3509 }
3510
3511 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3512 static bool
3513 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3514 Elf_Internal_Sym *sym)
3515 {
3516 const struct elf_backend_data *bed;
3517
3518 /* Local symbols do not count, but target specific ones might. */
3519 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3520 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3521 return false;
3522
3523 bed = get_elf_backend_data (abfd);
3524 /* Function symbols do not count. */
3525 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3526 return false;
3527
3528 /* If the section is undefined, then so is the symbol. */
3529 if (sym->st_shndx == SHN_UNDEF)
3530 return false;
3531
3532 /* If the symbol is defined in the common section, then
3533 it is a common definition and so does not count. */
3534 if (bed->common_definition (sym))
3535 return false;
3536
3537 /* If the symbol is in a target specific section then we
3538 must rely upon the backend to tell us what it is. */
3539 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3540 /* FIXME - this function is not coded yet:
3541
3542 return _bfd_is_global_symbol_definition (abfd, sym);
3543
3544 Instead for now assume that the definition is not global,
3545 Even if this is wrong, at least the linker will behave
3546 in the same way that it used to do. */
3547 return false;
3548
3549 return true;
3550 }
3551
3552 /* Search the symbol table of the archive element of the archive ABFD
3553 whose archive map contains a mention of SYMDEF, and determine if
3554 the symbol is defined in this element. */
3555 static bool
3556 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3557 {
3558 Elf_Internal_Shdr * hdr;
3559 size_t symcount;
3560 size_t extsymcount;
3561 size_t extsymoff;
3562 Elf_Internal_Sym *isymbuf;
3563 Elf_Internal_Sym *isym;
3564 Elf_Internal_Sym *isymend;
3565 bool result;
3566
3567 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3568 if (abfd == NULL)
3569 return false;
3570
3571 if (! bfd_check_format (abfd, bfd_object))
3572 return false;
3573
3574 /* Select the appropriate symbol table. If we don't know if the
3575 object file is an IR object, give linker LTO plugin a chance to
3576 get the correct symbol table. */
3577 if (abfd->plugin_format == bfd_plugin_yes
3578 #if BFD_SUPPORTS_PLUGINS
3579 || (abfd->plugin_format == bfd_plugin_unknown
3580 && bfd_link_plugin_object_p (abfd))
3581 #endif
3582 )
3583 {
3584 /* Use the IR symbol table if the object has been claimed by
3585 plugin. */
3586 abfd = abfd->plugin_dummy_bfd;
3587 hdr = &elf_tdata (abfd)->symtab_hdr;
3588 }
3589 else
3590 {
3591 if (elf_use_dt_symtab_p (abfd))
3592 {
3593 bfd_set_error (bfd_error_wrong_format);
3594 return false;
3595 }
3596
3597 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3598 hdr = &elf_tdata (abfd)->symtab_hdr;
3599 else
3600 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3601 }
3602
3603 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3604
3605 /* The sh_info field of the symtab header tells us where the
3606 external symbols start. We don't care about the local symbols. */
3607 if (elf_bad_symtab (abfd))
3608 {
3609 extsymcount = symcount;
3610 extsymoff = 0;
3611 }
3612 else
3613 {
3614 extsymcount = symcount - hdr->sh_info;
3615 extsymoff = hdr->sh_info;
3616 }
3617
3618 if (extsymcount == 0)
3619 return false;
3620
3621 /* Read in the symbol table. */
3622 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3623 NULL, NULL, NULL);
3624 if (isymbuf == NULL)
3625 return false;
3626
3627 /* Scan the symbol table looking for SYMDEF. */
3628 result = false;
3629 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3630 {
3631 const char *name;
3632
3633 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3634 isym->st_name);
3635 if (name == NULL)
3636 break;
3637
3638 if (strcmp (name, symdef->name) == 0)
3639 {
3640 result = is_global_data_symbol_definition (abfd, isym);
3641 break;
3642 }
3643 }
3644
3645 free (isymbuf);
3646
3647 return result;
3648 }
3649 \f
3650 /* Add an entry to the .dynamic table. */
3651
3652 bool
3653 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3654 bfd_vma tag,
3655 bfd_vma val)
3656 {
3657 struct elf_link_hash_table *hash_table;
3658 const struct elf_backend_data *bed;
3659 asection *s;
3660 bfd_size_type newsize;
3661 bfd_byte *newcontents;
3662 Elf_Internal_Dyn dyn;
3663
3664 hash_table = elf_hash_table (info);
3665 if (! is_elf_hash_table (&hash_table->root))
3666 return false;
3667
3668 if (tag == DT_RELA || tag == DT_REL)
3669 hash_table->dynamic_relocs = true;
3670
3671 bed = get_elf_backend_data (hash_table->dynobj);
3672 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3673 BFD_ASSERT (s != NULL);
3674
3675 newsize = s->size + bed->s->sizeof_dyn;
3676 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3677 if (newcontents == NULL)
3678 return false;
3679
3680 dyn.d_tag = tag;
3681 dyn.d_un.d_val = val;
3682 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3683
3684 s->size = newsize;
3685 s->contents = newcontents;
3686
3687 return true;
3688 }
3689
3690 /* Strip zero-sized dynamic sections. */
3691
3692 bool
3693 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3694 {
3695 struct elf_link_hash_table *hash_table;
3696 const struct elf_backend_data *bed;
3697 asection *s, *sdynamic, **pp;
3698 asection *rela_dyn, *rel_dyn;
3699 Elf_Internal_Dyn dyn;
3700 bfd_byte *extdyn, *next;
3701 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3702 bool strip_zero_sized;
3703 bool strip_zero_sized_plt;
3704
3705 if (bfd_link_relocatable (info))
3706 return true;
3707
3708 hash_table = elf_hash_table (info);
3709 if (!is_elf_hash_table (&hash_table->root))
3710 return false;
3711
3712 if (!hash_table->dynobj)
3713 return true;
3714
3715 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3716 if (!sdynamic)
3717 return true;
3718
3719 bed = get_elf_backend_data (hash_table->dynobj);
3720 swap_dyn_in = bed->s->swap_dyn_in;
3721
3722 strip_zero_sized = false;
3723 strip_zero_sized_plt = false;
3724
3725 /* Strip zero-sized dynamic sections. */
3726 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3727 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3728 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3729 if (s->size == 0
3730 && (s == rela_dyn
3731 || s == rel_dyn
3732 || s == hash_table->srelplt->output_section
3733 || s == hash_table->splt->output_section))
3734 {
3735 *pp = s->next;
3736 info->output_bfd->section_count--;
3737 strip_zero_sized = true;
3738 if (s == rela_dyn)
3739 s = rela_dyn;
3740 if (s == rel_dyn)
3741 s = rel_dyn;
3742 else if (s == hash_table->splt->output_section)
3743 {
3744 s = hash_table->splt;
3745 strip_zero_sized_plt = true;
3746 }
3747 else
3748 s = hash_table->srelplt;
3749 s->flags |= SEC_EXCLUDE;
3750 s->output_section = bfd_abs_section_ptr;
3751 }
3752 else
3753 pp = &s->next;
3754
3755 if (strip_zero_sized_plt && sdynamic->size != 0)
3756 for (extdyn = sdynamic->contents;
3757 extdyn < sdynamic->contents + sdynamic->size;
3758 extdyn = next)
3759 {
3760 next = extdyn + bed->s->sizeof_dyn;
3761 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3762 switch (dyn.d_tag)
3763 {
3764 default:
3765 break;
3766 case DT_JMPREL:
3767 case DT_PLTRELSZ:
3768 case DT_PLTREL:
3769 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3770 the procedure linkage table (the .plt section) has been
3771 removed. */
3772 memmove (extdyn, next,
3773 sdynamic->size - (next - sdynamic->contents));
3774 next = extdyn;
3775 }
3776 }
3777
3778 if (strip_zero_sized)
3779 {
3780 /* Regenerate program headers. */
3781 elf_seg_map (info->output_bfd) = NULL;
3782 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3783 NULL);
3784 }
3785
3786 return true;
3787 }
3788
3789 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3790 1 if a DT_NEEDED tag already exists, and 0 on success. */
3791
3792 int
3793 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3794 {
3795 struct elf_link_hash_table *hash_table;
3796 size_t strindex;
3797 const char *soname;
3798
3799 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3800 return -1;
3801
3802 hash_table = elf_hash_table (info);
3803 soname = elf_dt_name (abfd);
3804 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3805 if (strindex == (size_t) -1)
3806 return -1;
3807
3808 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3809 {
3810 asection *sdyn;
3811 const struct elf_backend_data *bed;
3812 bfd_byte *extdyn;
3813
3814 bed = get_elf_backend_data (hash_table->dynobj);
3815 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3816 if (sdyn != NULL && sdyn->size != 0)
3817 for (extdyn = sdyn->contents;
3818 extdyn < sdyn->contents + sdyn->size;
3819 extdyn += bed->s->sizeof_dyn)
3820 {
3821 Elf_Internal_Dyn dyn;
3822
3823 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3824 if (dyn.d_tag == DT_NEEDED
3825 && dyn.d_un.d_val == strindex)
3826 {
3827 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3828 return 1;
3829 }
3830 }
3831 }
3832
3833 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3834 return -1;
3835
3836 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3837 return -1;
3838
3839 return 0;
3840 }
3841
3842 /* Return true if SONAME is on the needed list between NEEDED and STOP
3843 (or the end of list if STOP is NULL), and needed by a library that
3844 will be loaded. */
3845
3846 static bool
3847 on_needed_list (const char *soname,
3848 struct bfd_link_needed_list *needed,
3849 struct bfd_link_needed_list *stop)
3850 {
3851 struct bfd_link_needed_list *look;
3852 for (look = needed; look != stop; look = look->next)
3853 if (strcmp (soname, look->name) == 0
3854 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3855 /* If needed by a library that itself is not directly
3856 needed, recursively check whether that library is
3857 indirectly needed. Since we add DT_NEEDED entries to
3858 the end of the list, library dependencies appear after
3859 the library. Therefore search prior to the current
3860 LOOK, preventing possible infinite recursion. */
3861 || on_needed_list (elf_dt_name (look->by), needed, look)))
3862 return true;
3863
3864 return false;
3865 }
3866
3867 /* Sort symbol by value, section, size, and type. */
3868 static int
3869 elf_sort_symbol (const void *arg1, const void *arg2)
3870 {
3871 const struct elf_link_hash_entry *h1;
3872 const struct elf_link_hash_entry *h2;
3873 bfd_signed_vma vdiff;
3874 int sdiff;
3875 const char *n1;
3876 const char *n2;
3877
3878 h1 = *(const struct elf_link_hash_entry **) arg1;
3879 h2 = *(const struct elf_link_hash_entry **) arg2;
3880 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3881 if (vdiff != 0)
3882 return vdiff > 0 ? 1 : -1;
3883
3884 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3885 if (sdiff != 0)
3886 return sdiff;
3887
3888 /* Sort so that sized symbols are selected over zero size symbols. */
3889 vdiff = h1->size - h2->size;
3890 if (vdiff != 0)
3891 return vdiff > 0 ? 1 : -1;
3892
3893 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3894 if (h1->type != h2->type)
3895 return h1->type - h2->type;
3896
3897 /* If symbols are properly sized and typed, and multiple strong
3898 aliases are not defined in a shared library by the user we
3899 shouldn't get here. Unfortunately linker script symbols like
3900 __bss_start sometimes match a user symbol defined at the start of
3901 .bss without proper size and type. We'd like to preference the
3902 user symbol over reserved system symbols. Sort on leading
3903 underscores. */
3904 n1 = h1->root.root.string;
3905 n2 = h2->root.root.string;
3906 while (*n1 == *n2)
3907 {
3908 if (*n1 == 0)
3909 break;
3910 ++n1;
3911 ++n2;
3912 }
3913 if (*n1 == '_')
3914 return -1;
3915 if (*n2 == '_')
3916 return 1;
3917
3918 /* Final sort on name selects user symbols like '_u' over reserved
3919 system symbols like '_Z' and also will avoid qsort instability. */
3920 return *n1 - *n2;
3921 }
3922
3923 /* This function is used to adjust offsets into .dynstr for
3924 dynamic symbols. This is called via elf_link_hash_traverse. */
3925
3926 static bool
3927 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3928 {
3929 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3930
3931 if (h->dynindx != -1)
3932 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3933 return true;
3934 }
3935
3936 /* Assign string offsets in .dynstr, update all structures referencing
3937 them. */
3938
3939 static bool
3940 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3941 {
3942 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3943 struct elf_link_local_dynamic_entry *entry;
3944 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3945 bfd *dynobj = hash_table->dynobj;
3946 asection *sdyn;
3947 bfd_size_type size;
3948 const struct elf_backend_data *bed;
3949 bfd_byte *extdyn;
3950
3951 _bfd_elf_strtab_finalize (dynstr);
3952 size = _bfd_elf_strtab_size (dynstr);
3953
3954 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3955
3956 if (info->callbacks->examine_strtab)
3957 info->callbacks->examine_strtab (dynstr);
3958
3959 bed = get_elf_backend_data (dynobj);
3960 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3961 BFD_ASSERT (sdyn != NULL);
3962
3963 /* Update all .dynamic entries referencing .dynstr strings. */
3964 for (extdyn = sdyn->contents;
3965 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3966 extdyn += bed->s->sizeof_dyn)
3967 {
3968 Elf_Internal_Dyn dyn;
3969
3970 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3971 switch (dyn.d_tag)
3972 {
3973 case DT_STRSZ:
3974 dyn.d_un.d_val = size;
3975 break;
3976 case DT_NEEDED:
3977 case DT_SONAME:
3978 case DT_RPATH:
3979 case DT_RUNPATH:
3980 case DT_FILTER:
3981 case DT_AUXILIARY:
3982 case DT_AUDIT:
3983 case DT_DEPAUDIT:
3984 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3985 break;
3986 default:
3987 continue;
3988 }
3989 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3990 }
3991
3992 /* Now update local dynamic symbols. */
3993 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3994 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3995 entry->isym.st_name);
3996
3997 /* And the rest of dynamic symbols. */
3998 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3999
4000 /* Adjust version definitions. */
4001 if (elf_tdata (output_bfd)->cverdefs)
4002 {
4003 asection *s;
4004 bfd_byte *p;
4005 size_t i;
4006 Elf_Internal_Verdef def;
4007 Elf_Internal_Verdaux defaux;
4008
4009 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4010 p = s->contents;
4011 do
4012 {
4013 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4014 &def);
4015 p += sizeof (Elf_External_Verdef);
4016 if (def.vd_aux != sizeof (Elf_External_Verdef))
4017 continue;
4018 for (i = 0; i < def.vd_cnt; ++i)
4019 {
4020 _bfd_elf_swap_verdaux_in (output_bfd,
4021 (Elf_External_Verdaux *) p, &defaux);
4022 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4023 defaux.vda_name);
4024 _bfd_elf_swap_verdaux_out (output_bfd,
4025 &defaux, (Elf_External_Verdaux *) p);
4026 p += sizeof (Elf_External_Verdaux);
4027 }
4028 }
4029 while (def.vd_next);
4030 }
4031
4032 /* Adjust version references. */
4033 if (elf_tdata (output_bfd)->verref)
4034 {
4035 asection *s;
4036 bfd_byte *p;
4037 size_t i;
4038 Elf_Internal_Verneed need;
4039 Elf_Internal_Vernaux needaux;
4040
4041 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4042 p = s->contents;
4043 do
4044 {
4045 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4046 &need);
4047 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4048 _bfd_elf_swap_verneed_out (output_bfd, &need,
4049 (Elf_External_Verneed *) p);
4050 p += sizeof (Elf_External_Verneed);
4051 for (i = 0; i < need.vn_cnt; ++i)
4052 {
4053 _bfd_elf_swap_vernaux_in (output_bfd,
4054 (Elf_External_Vernaux *) p, &needaux);
4055 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4056 needaux.vna_name);
4057 _bfd_elf_swap_vernaux_out (output_bfd,
4058 &needaux,
4059 (Elf_External_Vernaux *) p);
4060 p += sizeof (Elf_External_Vernaux);
4061 }
4062 }
4063 while (need.vn_next);
4064 }
4065
4066 return true;
4067 }
4068 \f
4069 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4070 The default is to only match when the INPUT and OUTPUT are exactly
4071 the same target. */
4072
4073 bool
4074 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4075 const bfd_target *output)
4076 {
4077 return input == output;
4078 }
4079
4080 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4081 This version is used when different targets for the same architecture
4082 are virtually identical. */
4083
4084 bool
4085 _bfd_elf_relocs_compatible (const bfd_target *input,
4086 const bfd_target *output)
4087 {
4088 const struct elf_backend_data *obed, *ibed;
4089
4090 if (input == output)
4091 return true;
4092
4093 ibed = xvec_get_elf_backend_data (input);
4094 obed = xvec_get_elf_backend_data (output);
4095
4096 if (ibed->arch != obed->arch)
4097 return false;
4098
4099 /* If both backends are using this function, deem them compatible. */
4100 return ibed->relocs_compatible == obed->relocs_compatible;
4101 }
4102
4103 /* Make a special call to the linker "notice" function to tell it that
4104 we are about to handle an as-needed lib, or have finished
4105 processing the lib. */
4106
4107 bool
4108 _bfd_elf_notice_as_needed (bfd *ibfd,
4109 struct bfd_link_info *info,
4110 enum notice_asneeded_action act)
4111 {
4112 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4113 }
4114
4115 /* Call ACTION on each relocation in an ELF object file. */
4116
4117 bool
4118 _bfd_elf_link_iterate_on_relocs
4119 (bfd *abfd, struct bfd_link_info *info,
4120 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4121 const Elf_Internal_Rela *))
4122 {
4123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4124 struct elf_link_hash_table *htab = elf_hash_table (info);
4125
4126 /* If this object is the same format as the output object, and it is
4127 not a shared library, then let the backend look through the
4128 relocs.
4129
4130 This is required to build global offset table entries and to
4131 arrange for dynamic relocs. It is not required for the
4132 particular common case of linking non PIC code, even when linking
4133 against shared libraries, but unfortunately there is no way of
4134 knowing whether an object file has been compiled PIC or not.
4135 Looking through the relocs is not particularly time consuming.
4136 The problem is that we must either (1) keep the relocs in memory,
4137 which causes the linker to require additional runtime memory or
4138 (2) read the relocs twice from the input file, which wastes time.
4139 This would be a good case for using mmap.
4140
4141 I have no idea how to handle linking PIC code into a file of a
4142 different format. It probably can't be done. */
4143 if ((abfd->flags & DYNAMIC) == 0
4144 && is_elf_hash_table (&htab->root)
4145 && elf_object_id (abfd) == elf_hash_table_id (htab)
4146 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4147 {
4148 asection *o;
4149
4150 for (o = abfd->sections; o != NULL; o = o->next)
4151 {
4152 Elf_Internal_Rela *internal_relocs;
4153 bool ok;
4154
4155 /* Don't check relocations in excluded sections. Don't do
4156 anything special with non-loaded, non-alloced sections.
4157 In particular, any relocs in such sections should not
4158 affect GOT and PLT reference counting (ie. we don't
4159 allow them to create GOT or PLT entries), there's no
4160 possibility or desire to optimize TLS relocs, and
4161 there's not much point in propagating relocs to shared
4162 libs that the dynamic linker won't relocate. */
4163 if ((o->flags & SEC_ALLOC) == 0
4164 || (o->flags & SEC_RELOC) == 0
4165 || (o->flags & SEC_EXCLUDE) != 0
4166 || o->reloc_count == 0
4167 || ((info->strip == strip_all || info->strip == strip_debugger)
4168 && (o->flags & SEC_DEBUGGING) != 0)
4169 || bfd_is_abs_section (o->output_section))
4170 continue;
4171
4172 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4173 o, NULL,
4174 NULL,
4175 _bfd_link_keep_memory (info));
4176 if (internal_relocs == NULL)
4177 return false;
4178
4179 ok = action (abfd, info, o, internal_relocs);
4180
4181 if (elf_section_data (o)->relocs != internal_relocs)
4182 free (internal_relocs);
4183
4184 if (! ok)
4185 return false;
4186 }
4187 }
4188
4189 return true;
4190 }
4191
4192 /* Check relocations in an ELF object file. This is called after
4193 all input files have been opened. */
4194
4195 bool
4196 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4197 {
4198 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4199 if (bed->check_relocs != NULL)
4200 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4201 bed->check_relocs);
4202 return true;
4203 }
4204
4205 /* Add symbols from an ELF object file to the linker hash table. */
4206
4207 static bool
4208 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4209 {
4210 Elf_Internal_Ehdr *ehdr;
4211 Elf_Internal_Shdr *hdr;
4212 size_t symcount;
4213 size_t extsymcount;
4214 size_t extsymoff;
4215 struct elf_link_hash_entry **sym_hash;
4216 bool dynamic;
4217 Elf_External_Versym *extversym = NULL;
4218 Elf_External_Versym *extversym_end = NULL;
4219 Elf_External_Versym *ever;
4220 struct elf_link_hash_entry *weaks;
4221 struct elf_link_hash_entry **nondeflt_vers = NULL;
4222 size_t nondeflt_vers_cnt = 0;
4223 Elf_Internal_Sym *isymbuf = NULL;
4224 Elf_Internal_Sym *isym;
4225 Elf_Internal_Sym *isymend;
4226 const struct elf_backend_data *bed;
4227 bool add_needed;
4228 struct elf_link_hash_table *htab;
4229 void *alloc_mark = NULL;
4230 struct bfd_hash_entry **old_table = NULL;
4231 unsigned int old_size = 0;
4232 unsigned int old_count = 0;
4233 void *old_tab = NULL;
4234 void *old_ent;
4235 struct bfd_link_hash_entry *old_undefs = NULL;
4236 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4237 void *old_strtab = NULL;
4238 size_t tabsize = 0;
4239 asection *s;
4240 bool just_syms;
4241
4242 htab = elf_hash_table (info);
4243 bed = get_elf_backend_data (abfd);
4244
4245 if (elf_use_dt_symtab_p (abfd))
4246 {
4247 bfd_set_error (bfd_error_wrong_format);
4248 return false;
4249 }
4250
4251 if ((abfd->flags & DYNAMIC) == 0)
4252 dynamic = false;
4253 else
4254 {
4255 dynamic = true;
4256
4257 /* You can't use -r against a dynamic object. Also, there's no
4258 hope of using a dynamic object which does not exactly match
4259 the format of the output file. */
4260 if (bfd_link_relocatable (info)
4261 || !is_elf_hash_table (&htab->root)
4262 || info->output_bfd->xvec != abfd->xvec)
4263 {
4264 if (bfd_link_relocatable (info))
4265 bfd_set_error (bfd_error_invalid_operation);
4266 else
4267 bfd_set_error (bfd_error_wrong_format);
4268 goto error_return;
4269 }
4270 }
4271
4272 ehdr = elf_elfheader (abfd);
4273 if (info->warn_alternate_em
4274 && bed->elf_machine_code != ehdr->e_machine
4275 && ((bed->elf_machine_alt1 != 0
4276 && ehdr->e_machine == bed->elf_machine_alt1)
4277 || (bed->elf_machine_alt2 != 0
4278 && ehdr->e_machine == bed->elf_machine_alt2)))
4279 _bfd_error_handler
4280 /* xgettext:c-format */
4281 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4282 ehdr->e_machine, abfd, bed->elf_machine_code);
4283
4284 /* As a GNU extension, any input sections which are named
4285 .gnu.warning.SYMBOL are treated as warning symbols for the given
4286 symbol. This differs from .gnu.warning sections, which generate
4287 warnings when they are included in an output file. */
4288 /* PR 12761: Also generate this warning when building shared libraries. */
4289 for (s = abfd->sections; s != NULL; s = s->next)
4290 {
4291 const char *name;
4292
4293 name = bfd_section_name (s);
4294 if (startswith (name, ".gnu.warning."))
4295 {
4296 char *msg;
4297 bfd_size_type sz;
4298
4299 name += sizeof ".gnu.warning." - 1;
4300
4301 /* If this is a shared object, then look up the symbol
4302 in the hash table. If it is there, and it is already
4303 been defined, then we will not be using the entry
4304 from this shared object, so we don't need to warn.
4305 FIXME: If we see the definition in a regular object
4306 later on, we will warn, but we shouldn't. The only
4307 fix is to keep track of what warnings we are supposed
4308 to emit, and then handle them all at the end of the
4309 link. */
4310 if (dynamic)
4311 {
4312 struct elf_link_hash_entry *h;
4313
4314 h = elf_link_hash_lookup (htab, name, false, false, true);
4315
4316 /* FIXME: What about bfd_link_hash_common? */
4317 if (h != NULL
4318 && (h->root.type == bfd_link_hash_defined
4319 || h->root.type == bfd_link_hash_defweak))
4320 continue;
4321 }
4322
4323 sz = s->size;
4324 msg = (char *) bfd_alloc (abfd, sz + 1);
4325 if (msg == NULL)
4326 goto error_return;
4327
4328 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4329 goto error_return;
4330
4331 msg[sz] = '\0';
4332
4333 if (! (_bfd_generic_link_add_one_symbol
4334 (info, abfd, name, BSF_WARNING, s, 0, msg,
4335 false, bed->collect, NULL)))
4336 goto error_return;
4337
4338 if (bfd_link_executable (info))
4339 {
4340 /* Clobber the section size so that the warning does
4341 not get copied into the output file. */
4342 s->size = 0;
4343
4344 /* Also set SEC_EXCLUDE, so that symbols defined in
4345 the warning section don't get copied to the output. */
4346 s->flags |= SEC_EXCLUDE;
4347 }
4348 }
4349 }
4350
4351 just_syms = ((s = abfd->sections) != NULL
4352 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4353
4354 add_needed = true;
4355 if (! dynamic)
4356 {
4357 /* If we are creating a shared library, create all the dynamic
4358 sections immediately. We need to attach them to something,
4359 so we attach them to this BFD, provided it is the right
4360 format and is not from ld --just-symbols. Always create the
4361 dynamic sections for -E/--dynamic-list. FIXME: If there
4362 are no input BFD's of the same format as the output, we can't
4363 make a shared library. */
4364 if (!just_syms
4365 && (bfd_link_pic (info)
4366 || (!bfd_link_relocatable (info)
4367 && info->nointerp
4368 && (info->export_dynamic || info->dynamic)))
4369 && is_elf_hash_table (&htab->root)
4370 && info->output_bfd->xvec == abfd->xvec
4371 && !htab->dynamic_sections_created)
4372 {
4373 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4374 goto error_return;
4375 }
4376 }
4377 else if (!is_elf_hash_table (&htab->root))
4378 goto error_return;
4379 else
4380 {
4381 const char *soname = NULL;
4382 char *audit = NULL;
4383 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4384 const Elf_Internal_Phdr *phdr;
4385 struct elf_link_loaded_list *loaded_lib;
4386
4387 /* ld --just-symbols and dynamic objects don't mix very well.
4388 ld shouldn't allow it. */
4389 if (just_syms)
4390 abort ();
4391
4392 /* If this dynamic lib was specified on the command line with
4393 --as-needed in effect, then we don't want to add a DT_NEEDED
4394 tag unless the lib is actually used. Similary for libs brought
4395 in by another lib's DT_NEEDED. When --no-add-needed is used
4396 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4397 any dynamic library in DT_NEEDED tags in the dynamic lib at
4398 all. */
4399 add_needed = (elf_dyn_lib_class (abfd)
4400 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4401 | DYN_NO_NEEDED)) == 0;
4402
4403 s = bfd_get_section_by_name (abfd, ".dynamic");
4404 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4405 {
4406 bfd_byte *dynbuf;
4407 bfd_byte *extdyn;
4408 unsigned int elfsec;
4409 unsigned long shlink;
4410
4411 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4412 {
4413 error_free_dyn:
4414 free (dynbuf);
4415 goto error_return;
4416 }
4417
4418 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4419 if (elfsec == SHN_BAD)
4420 goto error_free_dyn;
4421 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4422
4423 for (extdyn = dynbuf;
4424 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4425 extdyn += bed->s->sizeof_dyn)
4426 {
4427 Elf_Internal_Dyn dyn;
4428
4429 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4430 if (dyn.d_tag == DT_SONAME)
4431 {
4432 unsigned int tagv = dyn.d_un.d_val;
4433 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4434 if (soname == NULL)
4435 goto error_free_dyn;
4436 }
4437 if (dyn.d_tag == DT_NEEDED)
4438 {
4439 struct bfd_link_needed_list *n, **pn;
4440 char *fnm, *anm;
4441 unsigned int tagv = dyn.d_un.d_val;
4442 size_t amt = sizeof (struct bfd_link_needed_list);
4443
4444 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4445 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4446 if (n == NULL || fnm == NULL)
4447 goto error_free_dyn;
4448 amt = strlen (fnm) + 1;
4449 anm = (char *) bfd_alloc (abfd, amt);
4450 if (anm == NULL)
4451 goto error_free_dyn;
4452 memcpy (anm, fnm, amt);
4453 n->name = anm;
4454 n->by = abfd;
4455 n->next = NULL;
4456 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4457 ;
4458 *pn = n;
4459 }
4460 if (dyn.d_tag == DT_RUNPATH)
4461 {
4462 struct bfd_link_needed_list *n, **pn;
4463 char *fnm, *anm;
4464 unsigned int tagv = dyn.d_un.d_val;
4465 size_t amt = sizeof (struct bfd_link_needed_list);
4466
4467 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4468 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4469 if (n == NULL || fnm == NULL)
4470 goto error_free_dyn;
4471 amt = strlen (fnm) + 1;
4472 anm = (char *) bfd_alloc (abfd, amt);
4473 if (anm == NULL)
4474 goto error_free_dyn;
4475 memcpy (anm, fnm, amt);
4476 n->name = anm;
4477 n->by = abfd;
4478 n->next = NULL;
4479 for (pn = & runpath;
4480 *pn != NULL;
4481 pn = &(*pn)->next)
4482 ;
4483 *pn = n;
4484 }
4485 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4486 if (!runpath && dyn.d_tag == DT_RPATH)
4487 {
4488 struct bfd_link_needed_list *n, **pn;
4489 char *fnm, *anm;
4490 unsigned int tagv = dyn.d_un.d_val;
4491 size_t amt = sizeof (struct bfd_link_needed_list);
4492
4493 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4494 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4495 if (n == NULL || fnm == NULL)
4496 goto error_free_dyn;
4497 amt = strlen (fnm) + 1;
4498 anm = (char *) bfd_alloc (abfd, amt);
4499 if (anm == NULL)
4500 goto error_free_dyn;
4501 memcpy (anm, fnm, amt);
4502 n->name = anm;
4503 n->by = abfd;
4504 n->next = NULL;
4505 for (pn = & rpath;
4506 *pn != NULL;
4507 pn = &(*pn)->next)
4508 ;
4509 *pn = n;
4510 }
4511 if (dyn.d_tag == DT_AUDIT)
4512 {
4513 unsigned int tagv = dyn.d_un.d_val;
4514 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4515 }
4516 if (dyn.d_tag == DT_FLAGS_1)
4517 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4518 }
4519
4520 free (dynbuf);
4521 }
4522
4523 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4524 frees all more recently bfd_alloc'd blocks as well. */
4525 if (runpath)
4526 rpath = runpath;
4527
4528 if (rpath)
4529 {
4530 struct bfd_link_needed_list **pn;
4531 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4532 ;
4533 *pn = rpath;
4534 }
4535
4536 /* If we have a PT_GNU_RELRO program header, mark as read-only
4537 all sections contained fully therein. This makes relro
4538 shared library sections appear as they will at run-time. */
4539 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4540 while (phdr-- > elf_tdata (abfd)->phdr)
4541 if (phdr->p_type == PT_GNU_RELRO)
4542 {
4543 for (s = abfd->sections; s != NULL; s = s->next)
4544 {
4545 unsigned int opb = bfd_octets_per_byte (abfd, s);
4546
4547 if ((s->flags & SEC_ALLOC) != 0
4548 && s->vma * opb >= phdr->p_vaddr
4549 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4550 s->flags |= SEC_READONLY;
4551 }
4552 break;
4553 }
4554
4555 /* We do not want to include any of the sections in a dynamic
4556 object in the output file. We hack by simply clobbering the
4557 list of sections in the BFD. This could be handled more
4558 cleanly by, say, a new section flag; the existing
4559 SEC_NEVER_LOAD flag is not the one we want, because that one
4560 still implies that the section takes up space in the output
4561 file. */
4562 bfd_section_list_clear (abfd);
4563
4564 /* Find the name to use in a DT_NEEDED entry that refers to this
4565 object. If the object has a DT_SONAME entry, we use it.
4566 Otherwise, if the generic linker stuck something in
4567 elf_dt_name, we use that. Otherwise, we just use the file
4568 name. */
4569 if (soname == NULL || *soname == '\0')
4570 {
4571 soname = elf_dt_name (abfd);
4572 if (soname == NULL || *soname == '\0')
4573 soname = bfd_get_filename (abfd);
4574 }
4575
4576 /* Save the SONAME because sometimes the linker emulation code
4577 will need to know it. */
4578 elf_dt_name (abfd) = soname;
4579
4580 /* If we have already included this dynamic object in the
4581 link, just ignore it. There is no reason to include a
4582 particular dynamic object more than once. */
4583 for (loaded_lib = htab->dyn_loaded;
4584 loaded_lib != NULL;
4585 loaded_lib = loaded_lib->next)
4586 {
4587 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4588 return true;
4589 }
4590
4591 /* Create dynamic sections for backends that require that be done
4592 before setup_gnu_properties. */
4593 if (add_needed
4594 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4595 return false;
4596
4597 /* Save the DT_AUDIT entry for the linker emulation code. */
4598 elf_dt_audit (abfd) = audit;
4599 }
4600
4601 /* If this is a dynamic object, we always link against the .dynsym
4602 symbol table, not the .symtab symbol table. The dynamic linker
4603 will only see the .dynsym symbol table, so there is no reason to
4604 look at .symtab for a dynamic object. */
4605
4606 if (! dynamic || elf_dynsymtab (abfd) == 0)
4607 hdr = &elf_tdata (abfd)->symtab_hdr;
4608 else
4609 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4610
4611 symcount = hdr->sh_size / bed->s->sizeof_sym;
4612
4613 /* The sh_info field of the symtab header tells us where the
4614 external symbols start. We don't care about the local symbols at
4615 this point. */
4616 if (elf_bad_symtab (abfd))
4617 {
4618 extsymcount = symcount;
4619 extsymoff = 0;
4620 }
4621 else
4622 {
4623 extsymcount = symcount - hdr->sh_info;
4624 extsymoff = hdr->sh_info;
4625 }
4626
4627 sym_hash = elf_sym_hashes (abfd);
4628 if (extsymcount != 0)
4629 {
4630 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4631 NULL, NULL, NULL);
4632 if (isymbuf == NULL)
4633 goto error_return;
4634
4635 if (sym_hash == NULL)
4636 {
4637 /* We store a pointer to the hash table entry for each
4638 external symbol. */
4639 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4640 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4641 if (sym_hash == NULL)
4642 goto error_free_sym;
4643 elf_sym_hashes (abfd) = sym_hash;
4644 }
4645 }
4646
4647 if (dynamic)
4648 {
4649 /* Read in any version definitions. */
4650 if (!_bfd_elf_slurp_version_tables (abfd,
4651 info->default_imported_symver))
4652 goto error_free_sym;
4653
4654 /* Read in the symbol versions, but don't bother to convert them
4655 to internal format. */
4656 if (elf_dynversym (abfd) != 0)
4657 {
4658 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4659 bfd_size_type amt = versymhdr->sh_size;
4660
4661 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4662 goto error_free_sym;
4663 extversym = (Elf_External_Versym *)
4664 _bfd_malloc_and_read (abfd, amt, amt);
4665 if (extversym == NULL)
4666 goto error_free_sym;
4667 extversym_end = extversym + amt / sizeof (*extversym);
4668 }
4669 }
4670
4671 /* If we are loading an as-needed shared lib, save the symbol table
4672 state before we start adding symbols. If the lib turns out
4673 to be unneeded, restore the state. */
4674 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4675 {
4676 unsigned int i;
4677 size_t entsize;
4678
4679 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4680 {
4681 struct bfd_hash_entry *p;
4682 struct elf_link_hash_entry *h;
4683
4684 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4685 {
4686 h = (struct elf_link_hash_entry *) p;
4687 entsize += htab->root.table.entsize;
4688 if (h->root.type == bfd_link_hash_warning)
4689 {
4690 entsize += htab->root.table.entsize;
4691 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4692 }
4693 if (h->root.type == bfd_link_hash_common)
4694 entsize += sizeof (*h->root.u.c.p);
4695 }
4696 }
4697
4698 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4699 old_tab = bfd_malloc (tabsize + entsize);
4700 if (old_tab == NULL)
4701 goto error_free_vers;
4702
4703 /* Remember the current objalloc pointer, so that all mem for
4704 symbols added can later be reclaimed. */
4705 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4706 if (alloc_mark == NULL)
4707 goto error_free_vers;
4708
4709 /* Make a special call to the linker "notice" function to
4710 tell it that we are about to handle an as-needed lib. */
4711 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4712 goto error_free_vers;
4713
4714 /* Clone the symbol table. Remember some pointers into the
4715 symbol table, and dynamic symbol count. */
4716 old_ent = (char *) old_tab + tabsize;
4717 memcpy (old_tab, htab->root.table.table, tabsize);
4718 old_undefs = htab->root.undefs;
4719 old_undefs_tail = htab->root.undefs_tail;
4720 old_table = htab->root.table.table;
4721 old_size = htab->root.table.size;
4722 old_count = htab->root.table.count;
4723 old_strtab = NULL;
4724 if (htab->dynstr != NULL)
4725 {
4726 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4727 if (old_strtab == NULL)
4728 goto error_free_vers;
4729 }
4730
4731 for (i = 0; i < htab->root.table.size; i++)
4732 {
4733 struct bfd_hash_entry *p;
4734 struct elf_link_hash_entry *h;
4735
4736 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4737 {
4738 h = (struct elf_link_hash_entry *) p;
4739 memcpy (old_ent, h, htab->root.table.entsize);
4740 old_ent = (char *) old_ent + htab->root.table.entsize;
4741 if (h->root.type == bfd_link_hash_warning)
4742 {
4743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4744 memcpy (old_ent, h, htab->root.table.entsize);
4745 old_ent = (char *) old_ent + htab->root.table.entsize;
4746 }
4747 if (h->root.type == bfd_link_hash_common)
4748 {
4749 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4750 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4751 }
4752 }
4753 }
4754 }
4755
4756 weaks = NULL;
4757 if (extversym == NULL)
4758 ever = NULL;
4759 else if (extversym + extsymoff < extversym_end)
4760 ever = extversym + extsymoff;
4761 else
4762 {
4763 /* xgettext:c-format */
4764 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4765 abfd, (long) extsymoff,
4766 (long) (extversym_end - extversym) / sizeof (* extversym));
4767 bfd_set_error (bfd_error_bad_value);
4768 goto error_free_vers;
4769 }
4770
4771 if (!bfd_link_relocatable (info)
4772 && abfd->lto_slim_object)
4773 {
4774 _bfd_error_handler
4775 (_("%pB: plugin needed to handle lto object"), abfd);
4776 }
4777
4778 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4779 isym < isymend;
4780 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4781 {
4782 int bind;
4783 bfd_vma value;
4784 asection *sec, *new_sec;
4785 flagword flags;
4786 const char *name;
4787 struct elf_link_hash_entry *h;
4788 struct elf_link_hash_entry *hi;
4789 bool definition;
4790 bool size_change_ok;
4791 bool type_change_ok;
4792 bool new_weak;
4793 bool old_weak;
4794 bfd *override;
4795 bool common;
4796 bool discarded;
4797 unsigned int old_alignment;
4798 unsigned int shindex;
4799 bfd *old_bfd;
4800 bool matched;
4801
4802 override = NULL;
4803
4804 flags = BSF_NO_FLAGS;
4805 sec = NULL;
4806 value = isym->st_value;
4807 common = bed->common_definition (isym);
4808 if (common && info->inhibit_common_definition)
4809 {
4810 /* Treat common symbol as undefined for --no-define-common. */
4811 isym->st_shndx = SHN_UNDEF;
4812 common = false;
4813 }
4814 discarded = false;
4815
4816 bind = ELF_ST_BIND (isym->st_info);
4817 switch (bind)
4818 {
4819 case STB_LOCAL:
4820 /* This should be impossible, since ELF requires that all
4821 global symbols follow all local symbols, and that sh_info
4822 point to the first global symbol. Unfortunately, Irix 5
4823 screws this up. */
4824 if (elf_bad_symtab (abfd))
4825 continue;
4826
4827 /* If we aren't prepared to handle locals within the globals
4828 then we'll likely segfault on a NULL symbol hash if the
4829 symbol is ever referenced in relocations. */
4830 shindex = elf_elfheader (abfd)->e_shstrndx;
4831 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4832 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4833 " (>= sh_info of %lu)"),
4834 abfd, name, (long) (isym - isymbuf + extsymoff),
4835 (long) extsymoff);
4836
4837 /* Dynamic object relocations are not processed by ld, so
4838 ld won't run into the problem mentioned above. */
4839 if (dynamic)
4840 continue;
4841 bfd_set_error (bfd_error_bad_value);
4842 goto error_free_vers;
4843
4844 case STB_GLOBAL:
4845 if (isym->st_shndx != SHN_UNDEF && !common)
4846 flags = BSF_GLOBAL;
4847 break;
4848
4849 case STB_WEAK:
4850 flags = BSF_WEAK;
4851 break;
4852
4853 case STB_GNU_UNIQUE:
4854 flags = BSF_GNU_UNIQUE;
4855 break;
4856
4857 default:
4858 /* Leave it up to the processor backend. */
4859 break;
4860 }
4861
4862 if (isym->st_shndx == SHN_UNDEF)
4863 sec = bfd_und_section_ptr;
4864 else if (isym->st_shndx == SHN_ABS)
4865 sec = bfd_abs_section_ptr;
4866 else if (isym->st_shndx == SHN_COMMON)
4867 {
4868 sec = bfd_com_section_ptr;
4869 /* What ELF calls the size we call the value. What ELF
4870 calls the value we call the alignment. */
4871 value = isym->st_size;
4872 }
4873 else
4874 {
4875 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4876 if (sec == NULL)
4877 sec = bfd_abs_section_ptr;
4878 else if (discarded_section (sec))
4879 {
4880 /* Symbols from discarded section are undefined. We keep
4881 its visibility. */
4882 sec = bfd_und_section_ptr;
4883 discarded = true;
4884 isym->st_shndx = SHN_UNDEF;
4885 }
4886 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4887 value -= sec->vma;
4888 }
4889
4890 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4891 isym->st_name);
4892 if (name == NULL)
4893 goto error_free_vers;
4894
4895 if (isym->st_shndx == SHN_COMMON
4896 && (abfd->flags & BFD_PLUGIN) != 0)
4897 {
4898 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4899
4900 if (xc == NULL)
4901 {
4902 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4903 | SEC_EXCLUDE);
4904 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4905 if (xc == NULL)
4906 goto error_free_vers;
4907 }
4908 sec = xc;
4909 }
4910 else if (isym->st_shndx == SHN_COMMON
4911 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4912 && !bfd_link_relocatable (info))
4913 {
4914 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4915
4916 if (tcomm == NULL)
4917 {
4918 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4919 | SEC_LINKER_CREATED);
4920 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4921 if (tcomm == NULL)
4922 goto error_free_vers;
4923 }
4924 sec = tcomm;
4925 }
4926 else if (bed->elf_add_symbol_hook)
4927 {
4928 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4929 &sec, &value))
4930 goto error_free_vers;
4931
4932 /* The hook function sets the name to NULL if this symbol
4933 should be skipped for some reason. */
4934 if (name == NULL)
4935 continue;
4936 }
4937
4938 /* Sanity check that all possibilities were handled. */
4939 if (sec == NULL)
4940 abort ();
4941
4942 /* Silently discard TLS symbols from --just-syms. There's
4943 no way to combine a static TLS block with a new TLS block
4944 for this executable. */
4945 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4946 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4947 continue;
4948
4949 if (bfd_is_und_section (sec)
4950 || bfd_is_com_section (sec))
4951 definition = false;
4952 else
4953 definition = true;
4954
4955 size_change_ok = false;
4956 type_change_ok = bed->type_change_ok;
4957 old_weak = false;
4958 matched = false;
4959 old_alignment = 0;
4960 old_bfd = NULL;
4961 new_sec = sec;
4962
4963 if (is_elf_hash_table (&htab->root))
4964 {
4965 Elf_Internal_Versym iver;
4966 unsigned int vernum = 0;
4967 bool skip;
4968
4969 if (ever == NULL)
4970 {
4971 if (info->default_imported_symver)
4972 /* Use the default symbol version created earlier. */
4973 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4974 else
4975 iver.vs_vers = 0;
4976 }
4977 else if (ever >= extversym_end)
4978 {
4979 /* xgettext:c-format */
4980 _bfd_error_handler (_("%pB: not enough version information"),
4981 abfd);
4982 bfd_set_error (bfd_error_bad_value);
4983 goto error_free_vers;
4984 }
4985 else
4986 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4987
4988 vernum = iver.vs_vers & VERSYM_VERSION;
4989
4990 /* If this is a hidden symbol, or if it is not version
4991 1, we append the version name to the symbol name.
4992 However, we do not modify a non-hidden absolute symbol
4993 if it is not a function, because it might be the version
4994 symbol itself. FIXME: What if it isn't? */
4995 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4996 || (vernum > 1
4997 && (!bfd_is_abs_section (sec)
4998 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4999 {
5000 const char *verstr;
5001 size_t namelen, verlen, newlen;
5002 char *newname, *p;
5003
5004 if (isym->st_shndx != SHN_UNDEF)
5005 {
5006 if (vernum > elf_tdata (abfd)->cverdefs)
5007 verstr = NULL;
5008 else if (vernum > 1)
5009 verstr =
5010 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5011 else
5012 verstr = "";
5013
5014 if (verstr == NULL)
5015 {
5016 _bfd_error_handler
5017 /* xgettext:c-format */
5018 (_("%pB: %s: invalid version %u (max %d)"),
5019 abfd, name, vernum,
5020 elf_tdata (abfd)->cverdefs);
5021 bfd_set_error (bfd_error_bad_value);
5022 goto error_free_vers;
5023 }
5024 }
5025 else
5026 {
5027 /* We cannot simply test for the number of
5028 entries in the VERNEED section since the
5029 numbers for the needed versions do not start
5030 at 0. */
5031 Elf_Internal_Verneed *t;
5032
5033 verstr = NULL;
5034 for (t = elf_tdata (abfd)->verref;
5035 t != NULL;
5036 t = t->vn_nextref)
5037 {
5038 Elf_Internal_Vernaux *a;
5039
5040 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5041 {
5042 if (a->vna_other == vernum)
5043 {
5044 verstr = a->vna_nodename;
5045 break;
5046 }
5047 }
5048 if (a != NULL)
5049 break;
5050 }
5051 if (verstr == NULL)
5052 {
5053 _bfd_error_handler
5054 /* xgettext:c-format */
5055 (_("%pB: %s: invalid needed version %d"),
5056 abfd, name, vernum);
5057 bfd_set_error (bfd_error_bad_value);
5058 goto error_free_vers;
5059 }
5060 }
5061
5062 namelen = strlen (name);
5063 verlen = strlen (verstr);
5064 newlen = namelen + verlen + 2;
5065 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5066 && isym->st_shndx != SHN_UNDEF)
5067 ++newlen;
5068
5069 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5070 if (newname == NULL)
5071 goto error_free_vers;
5072 memcpy (newname, name, namelen);
5073 p = newname + namelen;
5074 *p++ = ELF_VER_CHR;
5075 /* If this is a defined non-hidden version symbol,
5076 we add another @ to the name. This indicates the
5077 default version of the symbol. */
5078 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5079 && isym->st_shndx != SHN_UNDEF)
5080 *p++ = ELF_VER_CHR;
5081 memcpy (p, verstr, verlen + 1);
5082
5083 name = newname;
5084 }
5085
5086 /* If this symbol has default visibility and the user has
5087 requested we not re-export it, then mark it as hidden. */
5088 if (!bfd_is_und_section (sec)
5089 && !dynamic
5090 && abfd->no_export
5091 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5092 isym->st_other = (STV_HIDDEN
5093 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5094
5095 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5096 sym_hash, &old_bfd, &old_weak,
5097 &old_alignment, &skip, &override,
5098 &type_change_ok, &size_change_ok,
5099 &matched))
5100 goto error_free_vers;
5101
5102 if (skip)
5103 continue;
5104
5105 /* Override a definition only if the new symbol matches the
5106 existing one. */
5107 if (override && matched)
5108 definition = false;
5109
5110 h = *sym_hash;
5111 while (h->root.type == bfd_link_hash_indirect
5112 || h->root.type == bfd_link_hash_warning)
5113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5114
5115 if (h->versioned != unversioned
5116 && elf_tdata (abfd)->verdef != NULL
5117 && vernum > 1
5118 && definition)
5119 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5120 }
5121
5122 if (! (_bfd_generic_link_add_one_symbol
5123 (info, override ? override : abfd, name, flags, sec, value,
5124 NULL, false, bed->collect,
5125 (struct bfd_link_hash_entry **) sym_hash)))
5126 goto error_free_vers;
5127
5128 h = *sym_hash;
5129 /* We need to make sure that indirect symbol dynamic flags are
5130 updated. */
5131 hi = h;
5132 while (h->root.type == bfd_link_hash_indirect
5133 || h->root.type == bfd_link_hash_warning)
5134 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5135
5136 *sym_hash = h;
5137
5138 /* Setting the index to -3 tells elf_link_output_extsym that
5139 this symbol is defined in a discarded section. */
5140 if (discarded && is_elf_hash_table (&htab->root))
5141 h->indx = -3;
5142
5143 new_weak = (flags & BSF_WEAK) != 0;
5144 if (dynamic
5145 && definition
5146 && new_weak
5147 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5148 && is_elf_hash_table (&htab->root)
5149 && h->u.alias == NULL)
5150 {
5151 /* Keep a list of all weak defined non function symbols from
5152 a dynamic object, using the alias field. Later in this
5153 function we will set the alias field to the correct
5154 value. We only put non-function symbols from dynamic
5155 objects on this list, because that happens to be the only
5156 time we need to know the normal symbol corresponding to a
5157 weak symbol, and the information is time consuming to
5158 figure out. If the alias field is not already NULL,
5159 then this symbol was already defined by some previous
5160 dynamic object, and we will be using that previous
5161 definition anyhow. */
5162
5163 h->u.alias = weaks;
5164 weaks = h;
5165 }
5166
5167 /* Set the alignment of a common symbol. */
5168 if ((common || bfd_is_com_section (sec))
5169 && h->root.type == bfd_link_hash_common)
5170 {
5171 unsigned int align;
5172
5173 if (common)
5174 align = bfd_log2 (isym->st_value);
5175 else
5176 {
5177 /* The new symbol is a common symbol in a shared object.
5178 We need to get the alignment from the section. */
5179 align = new_sec->alignment_power;
5180 }
5181 if (align > old_alignment)
5182 h->root.u.c.p->alignment_power = align;
5183 else
5184 h->root.u.c.p->alignment_power = old_alignment;
5185 }
5186
5187 if (is_elf_hash_table (&htab->root))
5188 {
5189 /* Set a flag in the hash table entry indicating the type of
5190 reference or definition we just found. A dynamic symbol
5191 is one which is referenced or defined by both a regular
5192 object and a shared object. */
5193 bool dynsym = false;
5194
5195 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5196 if ((abfd->flags & BFD_PLUGIN) != 0)
5197 {
5198 /* Except for this flag to track nonweak references. */
5199 if (!definition
5200 && bind != STB_WEAK)
5201 h->ref_ir_nonweak = 1;
5202 }
5203 else if (!dynamic)
5204 {
5205 if (! definition)
5206 {
5207 h->ref_regular = 1;
5208 if (bind != STB_WEAK)
5209 h->ref_regular_nonweak = 1;
5210 }
5211 else
5212 {
5213 h->def_regular = 1;
5214 if (h->def_dynamic)
5215 {
5216 h->def_dynamic = 0;
5217 h->ref_dynamic = 1;
5218 }
5219 }
5220 }
5221 else
5222 {
5223 if (! definition)
5224 {
5225 h->ref_dynamic = 1;
5226 hi->ref_dynamic = 1;
5227 }
5228 else
5229 {
5230 h->def_dynamic = 1;
5231 hi->def_dynamic = 1;
5232 }
5233 }
5234
5235 /* If an indirect symbol has been forced local, don't
5236 make the real symbol dynamic. */
5237 if (h != hi && hi->forced_local)
5238 ;
5239 else if (!dynamic)
5240 {
5241 if (bfd_link_dll (info)
5242 || h->def_dynamic
5243 || h->ref_dynamic)
5244 dynsym = true;
5245 }
5246 else
5247 {
5248 if (h->def_regular
5249 || h->ref_regular
5250 || (h->is_weakalias
5251 && weakdef (h)->dynindx != -1))
5252 dynsym = true;
5253 }
5254
5255 /* Check to see if we need to add an indirect symbol for
5256 the default name. */
5257 if ((definition
5258 || (!override && h->root.type == bfd_link_hash_common))
5259 && !(hi != h
5260 && hi->versioned == versioned_hidden))
5261 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5262 sec, value, &old_bfd, &dynsym))
5263 goto error_free_vers;
5264
5265 /* Check the alignment when a common symbol is involved. This
5266 can change when a common symbol is overridden by a normal
5267 definition or a common symbol is ignored due to the old
5268 normal definition. We need to make sure the maximum
5269 alignment is maintained. */
5270 if ((old_alignment || common)
5271 && h->root.type != bfd_link_hash_common)
5272 {
5273 unsigned int common_align;
5274 unsigned int normal_align;
5275 unsigned int symbol_align;
5276 bfd *normal_bfd;
5277 bfd *common_bfd;
5278
5279 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5280 || h->root.type == bfd_link_hash_defweak);
5281
5282 symbol_align = ffs (h->root.u.def.value) - 1;
5283 if (h->root.u.def.section->owner != NULL
5284 && (h->root.u.def.section->owner->flags
5285 & (DYNAMIC | BFD_PLUGIN)) == 0)
5286 {
5287 normal_align = h->root.u.def.section->alignment_power;
5288 if (normal_align > symbol_align)
5289 normal_align = symbol_align;
5290 }
5291 else
5292 normal_align = symbol_align;
5293
5294 if (old_alignment)
5295 {
5296 common_align = old_alignment;
5297 common_bfd = old_bfd;
5298 normal_bfd = abfd;
5299 }
5300 else
5301 {
5302 common_align = bfd_log2 (isym->st_value);
5303 common_bfd = abfd;
5304 normal_bfd = old_bfd;
5305 }
5306
5307 if (normal_align < common_align)
5308 {
5309 /* PR binutils/2735 */
5310 if (normal_bfd == NULL)
5311 _bfd_error_handler
5312 /* xgettext:c-format */
5313 (_("warning: alignment %u of common symbol `%s' in %pB is"
5314 " greater than the alignment (%u) of its section %pA"),
5315 1 << common_align, name, common_bfd,
5316 1 << normal_align, h->root.u.def.section);
5317 else
5318 _bfd_error_handler
5319 /* xgettext:c-format */
5320 (_("warning: alignment %u of normal symbol `%s' in %pB"
5321 " is smaller than %u used by the common definition in %pB"),
5322 1 << normal_align, name, normal_bfd,
5323 1 << common_align, common_bfd);
5324
5325 /* PR 30499: make sure that users understand that this warning is serious. */
5326 _bfd_error_handler
5327 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5328 }
5329 }
5330
5331 /* Remember the symbol size if it isn't undefined. */
5332 if (isym->st_size != 0
5333 && isym->st_shndx != SHN_UNDEF
5334 && (definition || h->size == 0))
5335 {
5336 if (h->size != 0
5337 && h->size != isym->st_size
5338 && ! size_change_ok)
5339 {
5340 _bfd_error_handler
5341 /* xgettext:c-format */
5342 (_("warning: size of symbol `%s' changed"
5343 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5344 name, (uint64_t) h->size, old_bfd,
5345 (uint64_t) isym->st_size, abfd);
5346
5347 /* PR 30499: make sure that users understand that this warning is serious. */
5348 _bfd_error_handler
5349 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5350 }
5351
5352 h->size = isym->st_size;
5353 }
5354
5355 /* If this is a common symbol, then we always want H->SIZE
5356 to be the size of the common symbol. The code just above
5357 won't fix the size if a common symbol becomes larger. We
5358 don't warn about a size change here, because that is
5359 covered by --warn-common. Allow changes between different
5360 function types. */
5361 if (h->root.type == bfd_link_hash_common)
5362 h->size = h->root.u.c.size;
5363
5364 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5365 && ((definition && !new_weak)
5366 || (old_weak && h->root.type == bfd_link_hash_common)
5367 || h->type == STT_NOTYPE))
5368 {
5369 unsigned int type = ELF_ST_TYPE (isym->st_info);
5370
5371 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5372 symbol. */
5373 if (type == STT_GNU_IFUNC
5374 && (abfd->flags & DYNAMIC) != 0)
5375 type = STT_FUNC;
5376
5377 if (h->type != type)
5378 {
5379 if (h->type != STT_NOTYPE && ! type_change_ok)
5380 /* xgettext:c-format */
5381 _bfd_error_handler
5382 (_("warning: type of symbol `%s' changed"
5383 " from %d to %d in %pB"),
5384 name, h->type, type, abfd);
5385
5386 h->type = type;
5387 }
5388 }
5389
5390 /* Merge st_other field. */
5391 elf_merge_st_other (abfd, h, isym->st_other, sec,
5392 definition, dynamic);
5393
5394 /* We don't want to make debug symbol dynamic. */
5395 if (definition
5396 && (sec->flags & SEC_DEBUGGING)
5397 && !bfd_link_relocatable (info))
5398 dynsym = false;
5399
5400 /* Nor should we make plugin symbols dynamic. */
5401 if ((abfd->flags & BFD_PLUGIN) != 0)
5402 dynsym = false;
5403
5404 if (definition)
5405 {
5406 h->target_internal = isym->st_target_internal;
5407 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5408 }
5409
5410 /* Don't add indirect symbols for .symver x, x@FOO aliases
5411 in IR. Since all data or text symbols in IR have the
5412 same type, value and section, we can't tell if a symbol
5413 is an alias of another symbol by their types, values and
5414 sections. */
5415 if (definition
5416 && !dynamic
5417 && (abfd->flags & BFD_PLUGIN) == 0)
5418 {
5419 char *p = strchr (name, ELF_VER_CHR);
5420 if (p != NULL && p[1] != ELF_VER_CHR)
5421 {
5422 /* Queue non-default versions so that .symver x, x@FOO
5423 aliases can be checked. */
5424 if (!nondeflt_vers)
5425 {
5426 size_t amt = ((isymend - isym + 1)
5427 * sizeof (struct elf_link_hash_entry *));
5428 nondeflt_vers
5429 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5430 if (!nondeflt_vers)
5431 goto error_free_vers;
5432 }
5433 nondeflt_vers[nondeflt_vers_cnt++] = h;
5434 }
5435 }
5436
5437 if (dynsym && h->dynindx == -1)
5438 {
5439 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5440 goto error_free_vers;
5441 if (h->is_weakalias
5442 && weakdef (h)->dynindx == -1)
5443 {
5444 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5445 goto error_free_vers;
5446 }
5447 }
5448 else if (h->dynindx != -1)
5449 /* If the symbol already has a dynamic index, but
5450 visibility says it should not be visible, turn it into
5451 a local symbol. */
5452 switch (ELF_ST_VISIBILITY (h->other))
5453 {
5454 case STV_INTERNAL:
5455 case STV_HIDDEN:
5456 (*bed->elf_backend_hide_symbol) (info, h, true);
5457 dynsym = false;
5458 break;
5459 }
5460
5461 if (!add_needed
5462 && matched
5463 && definition
5464 && h->root.type != bfd_link_hash_indirect
5465 && ((dynsym
5466 && h->ref_regular_nonweak)
5467 || (old_bfd != NULL
5468 && (old_bfd->flags & BFD_PLUGIN) != 0
5469 && h->ref_ir_nonweak
5470 && !info->lto_all_symbols_read)
5471 || (h->ref_dynamic_nonweak
5472 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5473 && !on_needed_list (elf_dt_name (abfd),
5474 htab->needed, NULL))))
5475 {
5476 const char *soname = elf_dt_name (abfd);
5477
5478 info->callbacks->minfo ("%!", soname, old_bfd,
5479 h->root.root.string);
5480
5481 /* A symbol from a library loaded via DT_NEEDED of some
5482 other library is referenced by a regular object.
5483 Add a DT_NEEDED entry for it. Issue an error if
5484 --no-add-needed is used and the reference was not
5485 a weak one. */
5486 if (old_bfd != NULL
5487 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5488 {
5489 _bfd_error_handler
5490 /* xgettext:c-format */
5491 (_("%pB: undefined reference to symbol '%s'"),
5492 old_bfd, name);
5493 bfd_set_error (bfd_error_missing_dso);
5494 goto error_free_vers;
5495 }
5496
5497 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5498 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5499
5500 /* Create dynamic sections for backends that require
5501 that be done before setup_gnu_properties. */
5502 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5503 return false;
5504 add_needed = true;
5505 }
5506 }
5507 }
5508
5509 if (info->lto_plugin_active
5510 && !bfd_link_relocatable (info)
5511 && (abfd->flags & BFD_PLUGIN) == 0
5512 && !just_syms
5513 && extsymcount)
5514 {
5515 int r_sym_shift;
5516
5517 if (bed->s->arch_size == 32)
5518 r_sym_shift = 8;
5519 else
5520 r_sym_shift = 32;
5521
5522 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5523 referenced in regular objects so that linker plugin will get
5524 the correct symbol resolution. */
5525
5526 sym_hash = elf_sym_hashes (abfd);
5527 for (s = abfd->sections; s != NULL; s = s->next)
5528 {
5529 Elf_Internal_Rela *internal_relocs;
5530 Elf_Internal_Rela *rel, *relend;
5531
5532 /* Don't check relocations in excluded sections. */
5533 if ((s->flags & SEC_RELOC) == 0
5534 || s->reloc_count == 0
5535 || (s->flags & SEC_EXCLUDE) != 0
5536 || ((info->strip == strip_all
5537 || info->strip == strip_debugger)
5538 && (s->flags & SEC_DEBUGGING) != 0))
5539 continue;
5540
5541 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5542 s, NULL,
5543 NULL,
5544 _bfd_link_keep_memory (info));
5545 if (internal_relocs == NULL)
5546 goto error_free_vers;
5547
5548 rel = internal_relocs;
5549 relend = rel + s->reloc_count;
5550 for ( ; rel < relend; rel++)
5551 {
5552 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5553 struct elf_link_hash_entry *h;
5554
5555 /* Skip local symbols. */
5556 if (r_symndx < extsymoff)
5557 continue;
5558
5559 h = sym_hash[r_symndx - extsymoff];
5560 if (h != NULL)
5561 h->root.non_ir_ref_regular = 1;
5562 }
5563
5564 if (elf_section_data (s)->relocs != internal_relocs)
5565 free (internal_relocs);
5566 }
5567 }
5568
5569 free (extversym);
5570 extversym = NULL;
5571 free (isymbuf);
5572 isymbuf = NULL;
5573
5574 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5575 {
5576 unsigned int i;
5577
5578 /* Restore the symbol table. */
5579 old_ent = (char *) old_tab + tabsize;
5580 memset (elf_sym_hashes (abfd), 0,
5581 extsymcount * sizeof (struct elf_link_hash_entry *));
5582 htab->root.table.table = old_table;
5583 htab->root.table.size = old_size;
5584 htab->root.table.count = old_count;
5585 memcpy (htab->root.table.table, old_tab, tabsize);
5586 htab->root.undefs = old_undefs;
5587 htab->root.undefs_tail = old_undefs_tail;
5588 if (htab->dynstr != NULL)
5589 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5590 free (old_strtab);
5591 old_strtab = NULL;
5592 for (i = 0; i < htab->root.table.size; i++)
5593 {
5594 struct bfd_hash_entry *p;
5595 struct elf_link_hash_entry *h;
5596 unsigned int non_ir_ref_dynamic;
5597
5598 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5599 {
5600 /* Preserve non_ir_ref_dynamic so that this symbol
5601 will be exported when the dynamic lib becomes needed
5602 in the second pass. */
5603 h = (struct elf_link_hash_entry *) p;
5604 if (h->root.type == bfd_link_hash_warning)
5605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5606 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5607
5608 h = (struct elf_link_hash_entry *) p;
5609 memcpy (h, old_ent, htab->root.table.entsize);
5610 old_ent = (char *) old_ent + htab->root.table.entsize;
5611 if (h->root.type == bfd_link_hash_warning)
5612 {
5613 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5614 memcpy (h, old_ent, htab->root.table.entsize);
5615 old_ent = (char *) old_ent + htab->root.table.entsize;
5616 }
5617 if (h->root.type == bfd_link_hash_common)
5618 {
5619 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5620 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5621 }
5622 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5623 }
5624 }
5625
5626 /* Make a special call to the linker "notice" function to
5627 tell it that symbols added for crefs may need to be removed. */
5628 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5629 goto error_free_vers;
5630
5631 free (old_tab);
5632 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5633 alloc_mark);
5634 free (nondeflt_vers);
5635 return true;
5636 }
5637
5638 if (old_tab != NULL)
5639 {
5640 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5641 goto error_free_vers;
5642 free (old_tab);
5643 old_tab = NULL;
5644 }
5645
5646 /* Now that all the symbols from this input file are created, if
5647 not performing a relocatable link, handle .symver foo, foo@BAR
5648 such that any relocs against foo become foo@BAR. */
5649 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5650 {
5651 size_t cnt, symidx;
5652
5653 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5654 {
5655 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5656 char *shortname, *p;
5657 size_t amt;
5658
5659 p = strchr (h->root.root.string, ELF_VER_CHR);
5660 if (p == NULL
5661 || (h->root.type != bfd_link_hash_defined
5662 && h->root.type != bfd_link_hash_defweak))
5663 continue;
5664
5665 amt = p - h->root.root.string;
5666 shortname = (char *) bfd_malloc (amt + 1);
5667 if (!shortname)
5668 goto error_free_vers;
5669 memcpy (shortname, h->root.root.string, amt);
5670 shortname[amt] = '\0';
5671
5672 hi = (struct elf_link_hash_entry *)
5673 bfd_link_hash_lookup (&htab->root, shortname,
5674 false, false, false);
5675 if (hi != NULL
5676 && hi->root.type == h->root.type
5677 && hi->root.u.def.value == h->root.u.def.value
5678 && hi->root.u.def.section == h->root.u.def.section)
5679 {
5680 (*bed->elf_backend_hide_symbol) (info, hi, true);
5681 hi->root.type = bfd_link_hash_indirect;
5682 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5683 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5684 sym_hash = elf_sym_hashes (abfd);
5685 if (sym_hash)
5686 for (symidx = 0; symidx < extsymcount; ++symidx)
5687 if (sym_hash[symidx] == hi)
5688 {
5689 sym_hash[symidx] = h;
5690 break;
5691 }
5692 }
5693 free (shortname);
5694 }
5695 free (nondeflt_vers);
5696 nondeflt_vers = NULL;
5697 }
5698
5699 /* Now set the alias field correctly for all the weak defined
5700 symbols we found. The only way to do this is to search all the
5701 symbols. Since we only need the information for non functions in
5702 dynamic objects, that's the only time we actually put anything on
5703 the list WEAKS. We need this information so that if a regular
5704 object refers to a symbol defined weakly in a dynamic object, the
5705 real symbol in the dynamic object is also put in the dynamic
5706 symbols; we also must arrange for both symbols to point to the
5707 same memory location. We could handle the general case of symbol
5708 aliasing, but a general symbol alias can only be generated in
5709 assembler code, handling it correctly would be very time
5710 consuming, and other ELF linkers don't handle general aliasing
5711 either. */
5712 if (weaks != NULL)
5713 {
5714 struct elf_link_hash_entry **hpp;
5715 struct elf_link_hash_entry **hppend;
5716 struct elf_link_hash_entry **sorted_sym_hash;
5717 struct elf_link_hash_entry *h;
5718 size_t sym_count, amt;
5719
5720 /* Since we have to search the whole symbol list for each weak
5721 defined symbol, search time for N weak defined symbols will be
5722 O(N^2). Binary search will cut it down to O(NlogN). */
5723 amt = extsymcount * sizeof (*sorted_sym_hash);
5724 sorted_sym_hash = bfd_malloc (amt);
5725 if (sorted_sym_hash == NULL)
5726 goto error_return;
5727 sym_hash = sorted_sym_hash;
5728 hpp = elf_sym_hashes (abfd);
5729 hppend = hpp + extsymcount;
5730 sym_count = 0;
5731 for (; hpp < hppend; hpp++)
5732 {
5733 h = *hpp;
5734 if (h != NULL
5735 && h->root.type == bfd_link_hash_defined
5736 && !bed->is_function_type (h->type))
5737 {
5738 *sym_hash = h;
5739 sym_hash++;
5740 sym_count++;
5741 }
5742 }
5743
5744 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5745 elf_sort_symbol);
5746
5747 while (weaks != NULL)
5748 {
5749 struct elf_link_hash_entry *hlook;
5750 asection *slook;
5751 bfd_vma vlook;
5752 size_t i, j, idx = 0;
5753
5754 hlook = weaks;
5755 weaks = hlook->u.alias;
5756 hlook->u.alias = NULL;
5757
5758 if (hlook->root.type != bfd_link_hash_defined
5759 && hlook->root.type != bfd_link_hash_defweak)
5760 continue;
5761
5762 slook = hlook->root.u.def.section;
5763 vlook = hlook->root.u.def.value;
5764
5765 i = 0;
5766 j = sym_count;
5767 while (i != j)
5768 {
5769 bfd_signed_vma vdiff;
5770 idx = (i + j) / 2;
5771 h = sorted_sym_hash[idx];
5772 vdiff = vlook - h->root.u.def.value;
5773 if (vdiff < 0)
5774 j = idx;
5775 else if (vdiff > 0)
5776 i = idx + 1;
5777 else
5778 {
5779 int sdiff = slook->id - h->root.u.def.section->id;
5780 if (sdiff < 0)
5781 j = idx;
5782 else if (sdiff > 0)
5783 i = idx + 1;
5784 else
5785 break;
5786 }
5787 }
5788
5789 /* We didn't find a value/section match. */
5790 if (i == j)
5791 continue;
5792
5793 /* With multiple aliases, or when the weak symbol is already
5794 strongly defined, we have multiple matching symbols and
5795 the binary search above may land on any of them. Step
5796 one past the matching symbol(s). */
5797 while (++idx != j)
5798 {
5799 h = sorted_sym_hash[idx];
5800 if (h->root.u.def.section != slook
5801 || h->root.u.def.value != vlook)
5802 break;
5803 }
5804
5805 /* Now look back over the aliases. Since we sorted by size
5806 as well as value and section, we'll choose the one with
5807 the largest size. */
5808 while (idx-- != i)
5809 {
5810 h = sorted_sym_hash[idx];
5811
5812 /* Stop if value or section doesn't match. */
5813 if (h->root.u.def.section != slook
5814 || h->root.u.def.value != vlook)
5815 break;
5816 else if (h != hlook)
5817 {
5818 struct elf_link_hash_entry *t;
5819
5820 hlook->u.alias = h;
5821 hlook->is_weakalias = 1;
5822 t = h;
5823 if (t->u.alias != NULL)
5824 while (t->u.alias != h)
5825 t = t->u.alias;
5826 t->u.alias = hlook;
5827
5828 /* If the weak definition is in the list of dynamic
5829 symbols, make sure the real definition is put
5830 there as well. */
5831 if (hlook->dynindx != -1 && h->dynindx == -1)
5832 {
5833 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5834 {
5835 err_free_sym_hash:
5836 free (sorted_sym_hash);
5837 goto error_return;
5838 }
5839 }
5840
5841 /* If the real definition is in the list of dynamic
5842 symbols, make sure the weak definition is put
5843 there as well. If we don't do this, then the
5844 dynamic loader might not merge the entries for the
5845 real definition and the weak definition. */
5846 if (h->dynindx != -1 && hlook->dynindx == -1)
5847 {
5848 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5849 goto err_free_sym_hash;
5850 }
5851 break;
5852 }
5853 }
5854 }
5855
5856 free (sorted_sym_hash);
5857 }
5858
5859 if (bed->check_directives
5860 && !(*bed->check_directives) (abfd, info))
5861 return false;
5862
5863 /* If this is a non-traditional link, try to optimize the handling
5864 of the .stab/.stabstr sections. */
5865 if (! dynamic
5866 && ! info->traditional_format
5867 && is_elf_hash_table (&htab->root)
5868 && (info->strip != strip_all && info->strip != strip_debugger))
5869 {
5870 asection *stabstr;
5871
5872 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5873 if (stabstr != NULL)
5874 {
5875 bfd_size_type string_offset = 0;
5876 asection *stab;
5877
5878 for (stab = abfd->sections; stab; stab = stab->next)
5879 if (startswith (stab->name, ".stab")
5880 && (!stab->name[5] ||
5881 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5882 && (stab->flags & SEC_MERGE) == 0
5883 && !bfd_is_abs_section (stab->output_section))
5884 {
5885 struct bfd_elf_section_data *secdata;
5886
5887 secdata = elf_section_data (stab);
5888 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5889 stabstr, &secdata->sec_info,
5890 &string_offset))
5891 goto error_return;
5892 if (secdata->sec_info)
5893 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5894 }
5895 }
5896 }
5897
5898 if (dynamic && add_needed)
5899 {
5900 /* Add this bfd to the loaded list. */
5901 struct elf_link_loaded_list *n;
5902
5903 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5904 if (n == NULL)
5905 goto error_return;
5906 n->abfd = abfd;
5907 n->next = htab->dyn_loaded;
5908 htab->dyn_loaded = n;
5909 }
5910 if (dynamic && !add_needed
5911 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5912 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5913
5914 return true;
5915
5916 error_free_vers:
5917 free (old_tab);
5918 free (old_strtab);
5919 free (nondeflt_vers);
5920 free (extversym);
5921 error_free_sym:
5922 free (isymbuf);
5923 error_return:
5924 return false;
5925 }
5926
5927 /* Return the linker hash table entry of a symbol that might be
5928 satisfied by an archive symbol. Return -1 on error. */
5929
5930 struct bfd_link_hash_entry *
5931 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5932 struct bfd_link_info *info,
5933 const char *name)
5934 {
5935 struct bfd_link_hash_entry *h;
5936 char *p, *copy;
5937 size_t len, first;
5938
5939 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5940 if (h != NULL)
5941 return h;
5942
5943 /* If this is a default version (the name contains @@), look up the
5944 symbol again with only one `@' as well as without the version.
5945 The effect is that references to the symbol with and without the
5946 version will be matched by the default symbol in the archive. */
5947
5948 p = strchr (name, ELF_VER_CHR);
5949 if (p == NULL || p[1] != ELF_VER_CHR)
5950 return h;
5951
5952 /* First check with only one `@'. */
5953 len = strlen (name);
5954 copy = (char *) bfd_alloc (abfd, len);
5955 if (copy == NULL)
5956 return (struct bfd_link_hash_entry *) -1;
5957
5958 first = p - name + 1;
5959 memcpy (copy, name, first);
5960 memcpy (copy + first, name + first + 1, len - first);
5961
5962 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5963 if (h == NULL)
5964 {
5965 /* We also need to check references to the symbol without the
5966 version. */
5967 copy[first - 1] = '\0';
5968 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5969 }
5970
5971 bfd_release (abfd, copy);
5972 return h;
5973 }
5974
5975 /* Add symbols from an ELF archive file to the linker hash table. We
5976 don't use _bfd_generic_link_add_archive_symbols because we need to
5977 handle versioned symbols.
5978
5979 Fortunately, ELF archive handling is simpler than that done by
5980 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5981 oddities. In ELF, if we find a symbol in the archive map, and the
5982 symbol is currently undefined, we know that we must pull in that
5983 object file.
5984
5985 Unfortunately, we do have to make multiple passes over the symbol
5986 table until nothing further is resolved. */
5987
5988 static bool
5989 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5990 {
5991 symindex c;
5992 unsigned char *included = NULL;
5993 carsym *symdefs;
5994 bool loop;
5995 size_t amt;
5996 const struct elf_backend_data *bed;
5997 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5998 (bfd *, struct bfd_link_info *, const char *);
5999
6000 if (! bfd_has_map (abfd))
6001 {
6002 /* An empty archive is a special case. */
6003 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6004 return true;
6005 bfd_set_error (bfd_error_no_armap);
6006 return false;
6007 }
6008
6009 /* Keep track of all symbols we know to be already defined, and all
6010 files we know to be already included. This is to speed up the
6011 second and subsequent passes. */
6012 c = bfd_ardata (abfd)->symdef_count;
6013 if (c == 0)
6014 return true;
6015 amt = c * sizeof (*included);
6016 included = (unsigned char *) bfd_zmalloc (amt);
6017 if (included == NULL)
6018 return false;
6019
6020 symdefs = bfd_ardata (abfd)->symdefs;
6021 bed = get_elf_backend_data (abfd);
6022 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6023
6024 do
6025 {
6026 file_ptr last;
6027 symindex i;
6028 carsym *symdef;
6029 carsym *symdefend;
6030
6031 loop = false;
6032 last = -1;
6033
6034 symdef = symdefs;
6035 symdefend = symdef + c;
6036 for (i = 0; symdef < symdefend; symdef++, i++)
6037 {
6038 struct bfd_link_hash_entry *h;
6039 bfd *element;
6040 struct bfd_link_hash_entry *undefs_tail;
6041 symindex mark;
6042
6043 if (included[i])
6044 continue;
6045 if (symdef->file_offset == last)
6046 {
6047 included[i] = true;
6048 continue;
6049 }
6050
6051 h = archive_symbol_lookup (abfd, info, symdef->name);
6052 if (h == (struct bfd_link_hash_entry *) -1)
6053 goto error_return;
6054
6055 if (h == NULL)
6056 continue;
6057
6058 if (h->type == bfd_link_hash_undefined)
6059 {
6060 /* If the archive element has already been loaded then one
6061 of the symbols defined by that element might have been
6062 made undefined due to being in a discarded section. */
6063 if (is_elf_hash_table (info->hash)
6064 && ((struct elf_link_hash_entry *) h)->indx == -3)
6065 continue;
6066 }
6067 else if (h->type == bfd_link_hash_common)
6068 {
6069 /* We currently have a common symbol. The archive map contains
6070 a reference to this symbol, so we may want to include it. We
6071 only want to include it however, if this archive element
6072 contains a definition of the symbol, not just another common
6073 declaration of it.
6074
6075 Unfortunately some archivers (including GNU ar) will put
6076 declarations of common symbols into their archive maps, as
6077 well as real definitions, so we cannot just go by the archive
6078 map alone. Instead we must read in the element's symbol
6079 table and check that to see what kind of symbol definition
6080 this is. */
6081 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6082 continue;
6083 }
6084 else
6085 {
6086 if (h->type != bfd_link_hash_undefweak)
6087 /* Symbol must be defined. Don't check it again. */
6088 included[i] = true;
6089 continue;
6090 }
6091
6092 /* We need to include this archive member. */
6093 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6094 info);
6095 if (element == NULL)
6096 goto error_return;
6097
6098 if (! bfd_check_format (element, bfd_object))
6099 goto error_return;
6100
6101 undefs_tail = info->hash->undefs_tail;
6102
6103 if (!(*info->callbacks
6104 ->add_archive_element) (info, element, symdef->name, &element))
6105 continue;
6106 if (!bfd_link_add_symbols (element, info))
6107 goto error_return;
6108
6109 /* If there are any new undefined symbols, we need to make
6110 another pass through the archive in order to see whether
6111 they can be defined. FIXME: This isn't perfect, because
6112 common symbols wind up on undefs_tail and because an
6113 undefined symbol which is defined later on in this pass
6114 does not require another pass. This isn't a bug, but it
6115 does make the code less efficient than it could be. */
6116 if (undefs_tail != info->hash->undefs_tail)
6117 loop = true;
6118
6119 /* Look backward to mark all symbols from this object file
6120 which we have already seen in this pass. */
6121 mark = i;
6122 do
6123 {
6124 included[mark] = true;
6125 if (mark == 0)
6126 break;
6127 --mark;
6128 }
6129 while (symdefs[mark].file_offset == symdef->file_offset);
6130
6131 /* We mark subsequent symbols from this object file as we go
6132 on through the loop. */
6133 last = symdef->file_offset;
6134 }
6135 }
6136 while (loop);
6137
6138 free (included);
6139 return true;
6140
6141 error_return:
6142 free (included);
6143 return false;
6144 }
6145
6146 /* Given an ELF BFD, add symbols to the global hash table as
6147 appropriate. */
6148
6149 bool
6150 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6151 {
6152 switch (bfd_get_format (abfd))
6153 {
6154 case bfd_object:
6155 return elf_link_add_object_symbols (abfd, info);
6156 case bfd_archive:
6157 return elf_link_add_archive_symbols (abfd, info);
6158 default:
6159 bfd_set_error (bfd_error_wrong_format);
6160 return false;
6161 }
6162 }
6163 \f
6164 struct hash_codes_info
6165 {
6166 unsigned long *hashcodes;
6167 bool error;
6168 };
6169
6170 /* This function will be called though elf_link_hash_traverse to store
6171 all hash value of the exported symbols in an array. */
6172
6173 static bool
6174 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6175 {
6176 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6177 const char *name;
6178 unsigned long ha;
6179 char *alc = NULL;
6180
6181 /* Ignore indirect symbols. These are added by the versioning code. */
6182 if (h->dynindx == -1)
6183 return true;
6184
6185 name = h->root.root.string;
6186 if (h->versioned >= versioned)
6187 {
6188 char *p = strchr (name, ELF_VER_CHR);
6189 if (p != NULL)
6190 {
6191 alc = (char *) bfd_malloc (p - name + 1);
6192 if (alc == NULL)
6193 {
6194 inf->error = true;
6195 return false;
6196 }
6197 memcpy (alc, name, p - name);
6198 alc[p - name] = '\0';
6199 name = alc;
6200 }
6201 }
6202
6203 /* Compute the hash value. */
6204 ha = bfd_elf_hash (name);
6205
6206 /* Store the found hash value in the array given as the argument. */
6207 *(inf->hashcodes)++ = ha;
6208
6209 /* And store it in the struct so that we can put it in the hash table
6210 later. */
6211 h->u.elf_hash_value = ha;
6212
6213 free (alc);
6214 return true;
6215 }
6216
6217 struct collect_gnu_hash_codes
6218 {
6219 bfd *output_bfd;
6220 const struct elf_backend_data *bed;
6221 unsigned long int nsyms;
6222 unsigned long int maskbits;
6223 unsigned long int *hashcodes;
6224 unsigned long int *hashval;
6225 unsigned long int *indx;
6226 unsigned long int *counts;
6227 bfd_vma *bitmask;
6228 bfd_byte *contents;
6229 bfd_size_type xlat;
6230 long int min_dynindx;
6231 unsigned long int bucketcount;
6232 unsigned long int symindx;
6233 long int local_indx;
6234 long int shift1, shift2;
6235 unsigned long int mask;
6236 bool error;
6237 };
6238
6239 /* This function will be called though elf_link_hash_traverse to store
6240 all hash value of the exported symbols in an array. */
6241
6242 static bool
6243 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6244 {
6245 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6246 const char *name;
6247 unsigned long ha;
6248 char *alc = NULL;
6249
6250 /* Ignore indirect symbols. These are added by the versioning code. */
6251 if (h->dynindx == -1)
6252 return true;
6253
6254 /* Ignore also local symbols and undefined symbols. */
6255 if (! (*s->bed->elf_hash_symbol) (h))
6256 return true;
6257
6258 name = h->root.root.string;
6259 if (h->versioned >= versioned)
6260 {
6261 char *p = strchr (name, ELF_VER_CHR);
6262 if (p != NULL)
6263 {
6264 alc = (char *) bfd_malloc (p - name + 1);
6265 if (alc == NULL)
6266 {
6267 s->error = true;
6268 return false;
6269 }
6270 memcpy (alc, name, p - name);
6271 alc[p - name] = '\0';
6272 name = alc;
6273 }
6274 }
6275
6276 /* Compute the hash value. */
6277 ha = bfd_elf_gnu_hash (name);
6278
6279 /* Store the found hash value in the array for compute_bucket_count,
6280 and also for .dynsym reordering purposes. */
6281 s->hashcodes[s->nsyms] = ha;
6282 s->hashval[h->dynindx] = ha;
6283 ++s->nsyms;
6284 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6285 s->min_dynindx = h->dynindx;
6286
6287 free (alc);
6288 return true;
6289 }
6290
6291 /* This function will be called though elf_link_hash_traverse to do
6292 final dynamic symbol renumbering in case of .gnu.hash.
6293 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6294 to the translation table. */
6295
6296 static bool
6297 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6298 {
6299 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6300 unsigned long int bucket;
6301 unsigned long int val;
6302
6303 /* Ignore indirect symbols. */
6304 if (h->dynindx == -1)
6305 return true;
6306
6307 /* Ignore also local symbols and undefined symbols. */
6308 if (! (*s->bed->elf_hash_symbol) (h))
6309 {
6310 if (h->dynindx >= s->min_dynindx)
6311 {
6312 if (s->bed->record_xhash_symbol != NULL)
6313 {
6314 (*s->bed->record_xhash_symbol) (h, 0);
6315 s->local_indx++;
6316 }
6317 else
6318 h->dynindx = s->local_indx++;
6319 }
6320 return true;
6321 }
6322
6323 bucket = s->hashval[h->dynindx] % s->bucketcount;
6324 val = (s->hashval[h->dynindx] >> s->shift1)
6325 & ((s->maskbits >> s->shift1) - 1);
6326 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6327 s->bitmask[val]
6328 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6329 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6330 if (s->counts[bucket] == 1)
6331 /* Last element terminates the chain. */
6332 val |= 1;
6333 bfd_put_32 (s->output_bfd, val,
6334 s->contents + (s->indx[bucket] - s->symindx) * 4);
6335 --s->counts[bucket];
6336 if (s->bed->record_xhash_symbol != NULL)
6337 {
6338 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6339
6340 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6341 }
6342 else
6343 h->dynindx = s->indx[bucket]++;
6344 return true;
6345 }
6346
6347 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6348
6349 bool
6350 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6351 {
6352 return !(h->forced_local
6353 || h->root.type == bfd_link_hash_undefined
6354 || h->root.type == bfd_link_hash_undefweak
6355 || ((h->root.type == bfd_link_hash_defined
6356 || h->root.type == bfd_link_hash_defweak)
6357 && h->root.u.def.section->output_section == NULL));
6358 }
6359
6360 /* Array used to determine the number of hash table buckets to use
6361 based on the number of symbols there are. If there are fewer than
6362 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6363 fewer than 37 we use 17 buckets, and so forth. We never use more
6364 than 32771 buckets. */
6365
6366 static const size_t elf_buckets[] =
6367 {
6368 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6369 16411, 32771, 0
6370 };
6371
6372 /* Compute bucket count for hashing table. We do not use a static set
6373 of possible tables sizes anymore. Instead we determine for all
6374 possible reasonable sizes of the table the outcome (i.e., the
6375 number of collisions etc) and choose the best solution. The
6376 weighting functions are not too simple to allow the table to grow
6377 without bounds. Instead one of the weighting factors is the size.
6378 Therefore the result is always a good payoff between few collisions
6379 (= short chain lengths) and table size. */
6380 static size_t
6381 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6382 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6383 unsigned long int nsyms,
6384 int gnu_hash)
6385 {
6386 size_t best_size = 0;
6387 unsigned long int i;
6388
6389 if (info->optimize)
6390 {
6391 size_t minsize;
6392 size_t maxsize;
6393 uint64_t best_chlen = ~((uint64_t) 0);
6394 bfd *dynobj = elf_hash_table (info)->dynobj;
6395 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6396 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6397 unsigned long int *counts;
6398 bfd_size_type amt;
6399 unsigned int no_improvement_count = 0;
6400
6401 /* Possible optimization parameters: if we have NSYMS symbols we say
6402 that the hashing table must at least have NSYMS/4 and at most
6403 2*NSYMS buckets. */
6404 minsize = nsyms / 4;
6405 if (minsize == 0)
6406 minsize = 1;
6407 best_size = maxsize = nsyms * 2;
6408 if (gnu_hash)
6409 {
6410 if (minsize < 2)
6411 minsize = 2;
6412 if ((best_size & 31) == 0)
6413 ++best_size;
6414 }
6415
6416 /* Create array where we count the collisions in. We must use bfd_malloc
6417 since the size could be large. */
6418 amt = maxsize;
6419 amt *= sizeof (unsigned long int);
6420 counts = (unsigned long int *) bfd_malloc (amt);
6421 if (counts == NULL)
6422 return 0;
6423
6424 /* Compute the "optimal" size for the hash table. The criteria is a
6425 minimal chain length. The minor criteria is (of course) the size
6426 of the table. */
6427 for (i = minsize; i < maxsize; ++i)
6428 {
6429 /* Walk through the array of hashcodes and count the collisions. */
6430 uint64_t max;
6431 unsigned long int j;
6432 unsigned long int fact;
6433
6434 if (gnu_hash && (i & 31) == 0)
6435 continue;
6436
6437 memset (counts, '\0', i * sizeof (unsigned long int));
6438
6439 /* Determine how often each hash bucket is used. */
6440 for (j = 0; j < nsyms; ++j)
6441 ++counts[hashcodes[j] % i];
6442
6443 /* For the weight function we need some information about the
6444 pagesize on the target. This is information need not be 100%
6445 accurate. Since this information is not available (so far) we
6446 define it here to a reasonable default value. If it is crucial
6447 to have a better value some day simply define this value. */
6448 # ifndef BFD_TARGET_PAGESIZE
6449 # define BFD_TARGET_PAGESIZE (4096)
6450 # endif
6451
6452 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6453 and the chains. */
6454 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6455
6456 # if 1
6457 /* Variant 1: optimize for short chains. We add the squares
6458 of all the chain lengths (which favors many small chain
6459 over a few long chains). */
6460 for (j = 0; j < i; ++j)
6461 max += counts[j] * counts[j];
6462
6463 /* This adds penalties for the overall size of the table. */
6464 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6465 max *= fact * fact;
6466 # else
6467 /* Variant 2: Optimize a lot more for small table. Here we
6468 also add squares of the size but we also add penalties for
6469 empty slots (the +1 term). */
6470 for (j = 0; j < i; ++j)
6471 max += (1 + counts[j]) * (1 + counts[j]);
6472
6473 /* The overall size of the table is considered, but not as
6474 strong as in variant 1, where it is squared. */
6475 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6476 max *= fact;
6477 # endif
6478
6479 /* Compare with current best results. */
6480 if (max < best_chlen)
6481 {
6482 best_chlen = max;
6483 best_size = i;
6484 no_improvement_count = 0;
6485 }
6486 /* PR 11843: Avoid futile long searches for the best bucket size
6487 when there are a large number of symbols. */
6488 else if (++no_improvement_count == 100)
6489 break;
6490 }
6491
6492 free (counts);
6493 }
6494 else
6495 {
6496 for (i = 0; elf_buckets[i] != 0; i++)
6497 {
6498 best_size = elf_buckets[i];
6499 if (nsyms < elf_buckets[i + 1])
6500 break;
6501 }
6502 if (gnu_hash && best_size < 2)
6503 best_size = 2;
6504 }
6505
6506 return best_size;
6507 }
6508
6509 /* Size any SHT_GROUP section for ld -r. */
6510
6511 bool
6512 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6513 {
6514 bfd *ibfd;
6515 asection *s;
6516
6517 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6518 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6519 && (s = ibfd->sections) != NULL
6520 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6521 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6522 return false;
6523 return true;
6524 }
6525
6526 /* Set a default stack segment size. The value in INFO wins. If it
6527 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6528 undefined it is initialized. */
6529
6530 bool
6531 bfd_elf_stack_segment_size (bfd *output_bfd,
6532 struct bfd_link_info *info,
6533 const char *legacy_symbol,
6534 bfd_vma default_size)
6535 {
6536 struct elf_link_hash_entry *h = NULL;
6537
6538 /* Look for legacy symbol. */
6539 if (legacy_symbol)
6540 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6541 false, false, false);
6542 if (h && (h->root.type == bfd_link_hash_defined
6543 || h->root.type == bfd_link_hash_defweak)
6544 && h->def_regular
6545 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6546 {
6547 /* The symbol has no type if specified on the command line. */
6548 h->type = STT_OBJECT;
6549 if (info->stacksize)
6550 /* xgettext:c-format */
6551 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6552 output_bfd, legacy_symbol);
6553 else if (h->root.u.def.section != bfd_abs_section_ptr)
6554 /* xgettext:c-format */
6555 _bfd_error_handler (_("%pB: %s not absolute"),
6556 output_bfd, legacy_symbol);
6557 else
6558 info->stacksize = h->root.u.def.value;
6559 }
6560
6561 if (!info->stacksize)
6562 /* If the user didn't set a size, or explicitly inhibit the
6563 size, set it now. */
6564 info->stacksize = default_size;
6565
6566 /* Provide the legacy symbol, if it is referenced. */
6567 if (h && (h->root.type == bfd_link_hash_undefined
6568 || h->root.type == bfd_link_hash_undefweak))
6569 {
6570 struct bfd_link_hash_entry *bh = NULL;
6571
6572 if (!(_bfd_generic_link_add_one_symbol
6573 (info, output_bfd, legacy_symbol,
6574 BSF_GLOBAL, bfd_abs_section_ptr,
6575 info->stacksize >= 0 ? info->stacksize : 0,
6576 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6577 return false;
6578
6579 h = (struct elf_link_hash_entry *) bh;
6580 h->def_regular = 1;
6581 h->type = STT_OBJECT;
6582 }
6583
6584 return true;
6585 }
6586
6587 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6588
6589 struct elf_gc_sweep_symbol_info
6590 {
6591 struct bfd_link_info *info;
6592 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6593 bool);
6594 };
6595
6596 static bool
6597 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6598 {
6599 if (!h->mark
6600 && (((h->root.type == bfd_link_hash_defined
6601 || h->root.type == bfd_link_hash_defweak)
6602 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6603 && h->root.u.def.section->gc_mark))
6604 || h->root.type == bfd_link_hash_undefined
6605 || h->root.type == bfd_link_hash_undefweak))
6606 {
6607 struct elf_gc_sweep_symbol_info *inf;
6608
6609 inf = (struct elf_gc_sweep_symbol_info *) data;
6610 (*inf->hide_symbol) (inf->info, h, true);
6611 h->def_regular = 0;
6612 h->ref_regular = 0;
6613 h->ref_regular_nonweak = 0;
6614 }
6615
6616 return true;
6617 }
6618
6619 /* Set up the sizes and contents of the ELF dynamic sections. This is
6620 called by the ELF linker emulation before_allocation routine. We
6621 must set the sizes of the sections before the linker sets the
6622 addresses of the various sections. */
6623
6624 bool
6625 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6626 const char *soname,
6627 const char *rpath,
6628 const char *filter_shlib,
6629 const char *audit,
6630 const char *depaudit,
6631 const char * const *auxiliary_filters,
6632 struct bfd_link_info *info,
6633 asection **sinterpptr)
6634 {
6635 bfd *dynobj;
6636 const struct elf_backend_data *bed;
6637
6638 *sinterpptr = NULL;
6639
6640 if (!is_elf_hash_table (info->hash))
6641 return true;
6642
6643 /* Any syms created from now on start with -1 in
6644 got.refcount/offset and plt.refcount/offset. */
6645 elf_hash_table (info)->init_got_refcount
6646 = elf_hash_table (info)->init_got_offset;
6647 elf_hash_table (info)->init_plt_refcount
6648 = elf_hash_table (info)->init_plt_offset;
6649
6650 bed = get_elf_backend_data (output_bfd);
6651
6652 /* The backend may have to create some sections regardless of whether
6653 we're dynamic or not. */
6654 if (bed->elf_backend_always_size_sections
6655 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6656 return false;
6657
6658 dynobj = elf_hash_table (info)->dynobj;
6659
6660 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6661 {
6662 struct bfd_elf_version_tree *verdefs;
6663 struct elf_info_failed asvinfo;
6664 struct bfd_elf_version_tree *t;
6665 struct bfd_elf_version_expr *d;
6666 asection *s;
6667 size_t soname_indx;
6668
6669 /* If we are supposed to export all symbols into the dynamic symbol
6670 table (this is not the normal case), then do so. */
6671 if (info->export_dynamic
6672 || (bfd_link_executable (info) && info->dynamic))
6673 {
6674 struct elf_info_failed eif;
6675
6676 eif.info = info;
6677 eif.failed = false;
6678 elf_link_hash_traverse (elf_hash_table (info),
6679 _bfd_elf_export_symbol,
6680 &eif);
6681 if (eif.failed)
6682 return false;
6683 }
6684
6685 if (soname != NULL)
6686 {
6687 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6688 soname, true);
6689 if (soname_indx == (size_t) -1
6690 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6691 return false;
6692 }
6693 else
6694 soname_indx = (size_t) -1;
6695
6696 /* Make all global versions with definition. */
6697 for (t = info->version_info; t != NULL; t = t->next)
6698 for (d = t->globals.list; d != NULL; d = d->next)
6699 if (!d->symver && d->literal)
6700 {
6701 const char *verstr, *name;
6702 size_t namelen, verlen, newlen;
6703 char *newname, *p, leading_char;
6704 struct elf_link_hash_entry *newh;
6705
6706 leading_char = bfd_get_symbol_leading_char (output_bfd);
6707 name = d->pattern;
6708 namelen = strlen (name) + (leading_char != '\0');
6709 verstr = t->name;
6710 verlen = strlen (verstr);
6711 newlen = namelen + verlen + 3;
6712
6713 newname = (char *) bfd_malloc (newlen);
6714 if (newname == NULL)
6715 return false;
6716 newname[0] = leading_char;
6717 memcpy (newname + (leading_char != '\0'), name, namelen);
6718
6719 /* Check the hidden versioned definition. */
6720 p = newname + namelen;
6721 *p++ = ELF_VER_CHR;
6722 memcpy (p, verstr, verlen + 1);
6723 newh = elf_link_hash_lookup (elf_hash_table (info),
6724 newname, false, false,
6725 false);
6726 if (newh == NULL
6727 || (newh->root.type != bfd_link_hash_defined
6728 && newh->root.type != bfd_link_hash_defweak))
6729 {
6730 /* Check the default versioned definition. */
6731 *p++ = ELF_VER_CHR;
6732 memcpy (p, verstr, verlen + 1);
6733 newh = elf_link_hash_lookup (elf_hash_table (info),
6734 newname, false, false,
6735 false);
6736 }
6737 free (newname);
6738
6739 /* Mark this version if there is a definition and it is
6740 not defined in a shared object. */
6741 if (newh != NULL
6742 && !newh->def_dynamic
6743 && (newh->root.type == bfd_link_hash_defined
6744 || newh->root.type == bfd_link_hash_defweak))
6745 d->symver = 1;
6746 }
6747
6748 /* Attach all the symbols to their version information. */
6749 asvinfo.info = info;
6750 asvinfo.failed = false;
6751
6752 elf_link_hash_traverse (elf_hash_table (info),
6753 _bfd_elf_link_assign_sym_version,
6754 &asvinfo);
6755 if (asvinfo.failed)
6756 return false;
6757
6758 if (!info->allow_undefined_version)
6759 {
6760 /* Check if all global versions have a definition. */
6761 bool all_defined = true;
6762 for (t = info->version_info; t != NULL; t = t->next)
6763 for (d = t->globals.list; d != NULL; d = d->next)
6764 if (d->literal && !d->symver && !d->script)
6765 {
6766 _bfd_error_handler
6767 (_("%s: undefined version: %s"),
6768 d->pattern, t->name);
6769 all_defined = false;
6770 }
6771
6772 if (!all_defined)
6773 {
6774 bfd_set_error (bfd_error_bad_value);
6775 return false;
6776 }
6777 }
6778
6779 /* Set up the version definition section. */
6780 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6781 BFD_ASSERT (s != NULL);
6782
6783 /* We may have created additional version definitions if we are
6784 just linking a regular application. */
6785 verdefs = info->version_info;
6786
6787 /* Skip anonymous version tag. */
6788 if (verdefs != NULL && verdefs->vernum == 0)
6789 verdefs = verdefs->next;
6790
6791 if (verdefs == NULL && !info->create_default_symver)
6792 s->flags |= SEC_EXCLUDE;
6793 else
6794 {
6795 unsigned int cdefs;
6796 bfd_size_type size;
6797 bfd_byte *p;
6798 Elf_Internal_Verdef def;
6799 Elf_Internal_Verdaux defaux;
6800 struct bfd_link_hash_entry *bh;
6801 struct elf_link_hash_entry *h;
6802 const char *name;
6803
6804 cdefs = 0;
6805 size = 0;
6806
6807 /* Make space for the base version. */
6808 size += sizeof (Elf_External_Verdef);
6809 size += sizeof (Elf_External_Verdaux);
6810 ++cdefs;
6811
6812 /* Make space for the default version. */
6813 if (info->create_default_symver)
6814 {
6815 size += sizeof (Elf_External_Verdef);
6816 ++cdefs;
6817 }
6818
6819 for (t = verdefs; t != NULL; t = t->next)
6820 {
6821 struct bfd_elf_version_deps *n;
6822
6823 /* Don't emit base version twice. */
6824 if (t->vernum == 0)
6825 continue;
6826
6827 size += sizeof (Elf_External_Verdef);
6828 size += sizeof (Elf_External_Verdaux);
6829 ++cdefs;
6830
6831 for (n = t->deps; n != NULL; n = n->next)
6832 size += sizeof (Elf_External_Verdaux);
6833 }
6834
6835 s->size = size;
6836 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6837 if (s->contents == NULL && s->size != 0)
6838 return false;
6839
6840 /* Fill in the version definition section. */
6841
6842 p = s->contents;
6843
6844 def.vd_version = VER_DEF_CURRENT;
6845 def.vd_flags = VER_FLG_BASE;
6846 def.vd_ndx = 1;
6847 def.vd_cnt = 1;
6848 if (info->create_default_symver)
6849 {
6850 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6851 def.vd_next = sizeof (Elf_External_Verdef);
6852 }
6853 else
6854 {
6855 def.vd_aux = sizeof (Elf_External_Verdef);
6856 def.vd_next = (sizeof (Elf_External_Verdef)
6857 + sizeof (Elf_External_Verdaux));
6858 }
6859
6860 if (soname_indx != (size_t) -1)
6861 {
6862 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6863 soname_indx);
6864 def.vd_hash = bfd_elf_hash (soname);
6865 defaux.vda_name = soname_indx;
6866 name = soname;
6867 }
6868 else
6869 {
6870 size_t indx;
6871
6872 name = lbasename (bfd_get_filename (output_bfd));
6873 def.vd_hash = bfd_elf_hash (name);
6874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6875 name, false);
6876 if (indx == (size_t) -1)
6877 return false;
6878 defaux.vda_name = indx;
6879 }
6880 defaux.vda_next = 0;
6881
6882 _bfd_elf_swap_verdef_out (output_bfd, &def,
6883 (Elf_External_Verdef *) p);
6884 p += sizeof (Elf_External_Verdef);
6885 if (info->create_default_symver)
6886 {
6887 /* Add a symbol representing this version. */
6888 bh = NULL;
6889 if (! (_bfd_generic_link_add_one_symbol
6890 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6891 0, NULL, false,
6892 get_elf_backend_data (dynobj)->collect, &bh)))
6893 return false;
6894 h = (struct elf_link_hash_entry *) bh;
6895 h->non_elf = 0;
6896 h->def_regular = 1;
6897 h->type = STT_OBJECT;
6898 h->verinfo.vertree = NULL;
6899
6900 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6901 return false;
6902
6903 /* Create a duplicate of the base version with the same
6904 aux block, but different flags. */
6905 def.vd_flags = 0;
6906 def.vd_ndx = 2;
6907 def.vd_aux = sizeof (Elf_External_Verdef);
6908 if (verdefs)
6909 def.vd_next = (sizeof (Elf_External_Verdef)
6910 + sizeof (Elf_External_Verdaux));
6911 else
6912 def.vd_next = 0;
6913 _bfd_elf_swap_verdef_out (output_bfd, &def,
6914 (Elf_External_Verdef *) p);
6915 p += sizeof (Elf_External_Verdef);
6916 }
6917 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6918 (Elf_External_Verdaux *) p);
6919 p += sizeof (Elf_External_Verdaux);
6920
6921 for (t = verdefs; t != NULL; t = t->next)
6922 {
6923 unsigned int cdeps;
6924 struct bfd_elf_version_deps *n;
6925
6926 /* Don't emit the base version twice. */
6927 if (t->vernum == 0)
6928 continue;
6929
6930 cdeps = 0;
6931 for (n = t->deps; n != NULL; n = n->next)
6932 ++cdeps;
6933
6934 /* Add a symbol representing this version. */
6935 bh = NULL;
6936 if (! (_bfd_generic_link_add_one_symbol
6937 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6938 0, NULL, false,
6939 get_elf_backend_data (dynobj)->collect, &bh)))
6940 return false;
6941 h = (struct elf_link_hash_entry *) bh;
6942 h->non_elf = 0;
6943 h->def_regular = 1;
6944 h->type = STT_OBJECT;
6945 h->verinfo.vertree = t;
6946
6947 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6948 return false;
6949
6950 def.vd_version = VER_DEF_CURRENT;
6951 def.vd_flags = 0;
6952 if (t->globals.list == NULL
6953 && t->locals.list == NULL
6954 && ! t->used)
6955 def.vd_flags |= VER_FLG_WEAK;
6956 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6957 def.vd_cnt = cdeps + 1;
6958 def.vd_hash = bfd_elf_hash (t->name);
6959 def.vd_aux = sizeof (Elf_External_Verdef);
6960 def.vd_next = 0;
6961
6962 /* If a basever node is next, it *must* be the last node in
6963 the chain, otherwise Verdef construction breaks. */
6964 if (t->next != NULL && t->next->vernum == 0)
6965 BFD_ASSERT (t->next->next == NULL);
6966
6967 if (t->next != NULL && t->next->vernum != 0)
6968 def.vd_next = (sizeof (Elf_External_Verdef)
6969 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6970
6971 _bfd_elf_swap_verdef_out (output_bfd, &def,
6972 (Elf_External_Verdef *) p);
6973 p += sizeof (Elf_External_Verdef);
6974
6975 defaux.vda_name = h->dynstr_index;
6976 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6977 h->dynstr_index);
6978 defaux.vda_next = 0;
6979 if (t->deps != NULL)
6980 defaux.vda_next = sizeof (Elf_External_Verdaux);
6981 t->name_indx = defaux.vda_name;
6982
6983 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6984 (Elf_External_Verdaux *) p);
6985 p += sizeof (Elf_External_Verdaux);
6986
6987 for (n = t->deps; n != NULL; n = n->next)
6988 {
6989 if (n->version_needed == NULL)
6990 {
6991 /* This can happen if there was an error in the
6992 version script. */
6993 defaux.vda_name = 0;
6994 }
6995 else
6996 {
6997 defaux.vda_name = n->version_needed->name_indx;
6998 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6999 defaux.vda_name);
7000 }
7001 if (n->next == NULL)
7002 defaux.vda_next = 0;
7003 else
7004 defaux.vda_next = sizeof (Elf_External_Verdaux);
7005
7006 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7007 (Elf_External_Verdaux *) p);
7008 p += sizeof (Elf_External_Verdaux);
7009 }
7010 }
7011
7012 elf_tdata (output_bfd)->cverdefs = cdefs;
7013 }
7014 }
7015
7016 if (info->gc_sections && bed->can_gc_sections)
7017 {
7018 struct elf_gc_sweep_symbol_info sweep_info;
7019
7020 /* Remove the symbols that were in the swept sections from the
7021 dynamic symbol table. */
7022 sweep_info.info = info;
7023 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7024 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7025 &sweep_info);
7026 }
7027
7028 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7029 {
7030 asection *s;
7031 struct elf_find_verdep_info sinfo;
7032
7033 /* Work out the size of the version reference section. */
7034
7035 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7036 BFD_ASSERT (s != NULL);
7037
7038 sinfo.info = info;
7039 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7040 if (sinfo.vers == 0)
7041 sinfo.vers = 1;
7042 sinfo.failed = false;
7043
7044 elf_link_hash_traverse (elf_hash_table (info),
7045 _bfd_elf_link_find_version_dependencies,
7046 &sinfo);
7047 if (sinfo.failed)
7048 return false;
7049
7050 if (info->enable_dt_relr)
7051 {
7052 elf_link_add_dt_relr_dependency (&sinfo);
7053 if (sinfo.failed)
7054 return false;
7055 }
7056
7057 if (elf_tdata (output_bfd)->verref == NULL)
7058 s->flags |= SEC_EXCLUDE;
7059 else
7060 {
7061 Elf_Internal_Verneed *vn;
7062 unsigned int size;
7063 unsigned int crefs;
7064 bfd_byte *p;
7065
7066 /* Build the version dependency section. */
7067 size = 0;
7068 crefs = 0;
7069 for (vn = elf_tdata (output_bfd)->verref;
7070 vn != NULL;
7071 vn = vn->vn_nextref)
7072 {
7073 Elf_Internal_Vernaux *a;
7074
7075 size += sizeof (Elf_External_Verneed);
7076 ++crefs;
7077 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7078 size += sizeof (Elf_External_Vernaux);
7079 }
7080
7081 s->size = size;
7082 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7083 if (s->contents == NULL)
7084 return false;
7085
7086 p = s->contents;
7087 for (vn = elf_tdata (output_bfd)->verref;
7088 vn != NULL;
7089 vn = vn->vn_nextref)
7090 {
7091 unsigned int caux;
7092 Elf_Internal_Vernaux *a;
7093 size_t indx;
7094
7095 caux = 0;
7096 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7097 ++caux;
7098
7099 vn->vn_version = VER_NEED_CURRENT;
7100 vn->vn_cnt = caux;
7101 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7102 elf_dt_name (vn->vn_bfd) != NULL
7103 ? elf_dt_name (vn->vn_bfd)
7104 : lbasename (bfd_get_filename
7105 (vn->vn_bfd)),
7106 false);
7107 if (indx == (size_t) -1)
7108 return false;
7109 vn->vn_file = indx;
7110 vn->vn_aux = sizeof (Elf_External_Verneed);
7111 if (vn->vn_nextref == NULL)
7112 vn->vn_next = 0;
7113 else
7114 vn->vn_next = (sizeof (Elf_External_Verneed)
7115 + caux * sizeof (Elf_External_Vernaux));
7116
7117 _bfd_elf_swap_verneed_out (output_bfd, vn,
7118 (Elf_External_Verneed *) p);
7119 p += sizeof (Elf_External_Verneed);
7120
7121 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7122 {
7123 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7124 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7125 a->vna_nodename, false);
7126 if (indx == (size_t) -1)
7127 return false;
7128 a->vna_name = indx;
7129 if (a->vna_nextptr == NULL)
7130 a->vna_next = 0;
7131 else
7132 a->vna_next = sizeof (Elf_External_Vernaux);
7133
7134 _bfd_elf_swap_vernaux_out (output_bfd, a,
7135 (Elf_External_Vernaux *) p);
7136 p += sizeof (Elf_External_Vernaux);
7137 }
7138 }
7139
7140 elf_tdata (output_bfd)->cverrefs = crefs;
7141 }
7142 }
7143
7144 if (bfd_link_relocatable (info)
7145 && !_bfd_elf_size_group_sections (info))
7146 return false;
7147
7148 /* Determine any GNU_STACK segment requirements, after the backend
7149 has had a chance to set a default segment size. */
7150 if (info->execstack)
7151 {
7152 /* If the user has explicitly requested warnings, then generate one even
7153 though the choice is the result of another command line option. */
7154 if (info->warn_execstack == 1)
7155 _bfd_error_handler
7156 (_("\
7157 warning: enabling an executable stack because of -z execstack command line option"));
7158 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7159 }
7160 else if (info->noexecstack)
7161 elf_stack_flags (output_bfd) = PF_R | PF_W;
7162 else
7163 {
7164 bfd *inputobj;
7165 asection *notesec = NULL;
7166 bfd *noteobj = NULL;
7167 bfd *emptyobj = NULL;
7168 int exec = 0;
7169
7170 for (inputobj = info->input_bfds;
7171 inputobj;
7172 inputobj = inputobj->link.next)
7173 {
7174 asection *s;
7175
7176 if (inputobj->flags
7177 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7178 continue;
7179 s = inputobj->sections;
7180 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7181 continue;
7182
7183 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7184 if (s)
7185 {
7186 notesec = s;
7187 if (s->flags & SEC_CODE)
7188 {
7189 noteobj = inputobj;
7190 exec = PF_X;
7191 /* There is no point in scanning the remaining bfds. */
7192 break;
7193 }
7194 }
7195 else if (bed->default_execstack && info->default_execstack)
7196 {
7197 exec = PF_X;
7198 emptyobj = inputobj;
7199 }
7200 }
7201
7202 if (notesec || info->stacksize > 0)
7203 {
7204 if (exec)
7205 {
7206 if (info->warn_execstack != 0)
7207 {
7208 /* PR 29072: Because an executable stack is a serious
7209 security risk, make sure that the user knows that it is
7210 being enabled despite the fact that it was not requested
7211 on the command line. */
7212 if (noteobj)
7213 _bfd_error_handler (_("\
7214 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7215 bfd_get_filename (noteobj));
7216 else if (emptyobj)
7217 {
7218 _bfd_error_handler (_("\
7219 warning: %s: missing .note.GNU-stack section implies executable stack"),
7220 bfd_get_filename (emptyobj));
7221 _bfd_error_handler (_("\
7222 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7223 }
7224 }
7225 }
7226 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7227 }
7228
7229 if (notesec && exec && bfd_link_relocatable (info)
7230 && notesec->output_section != bfd_abs_section_ptr)
7231 notesec->output_section->flags |= SEC_CODE;
7232 }
7233
7234 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7235 {
7236 struct elf_info_failed eif;
7237 struct elf_link_hash_entry *h;
7238 asection *dynstr;
7239 asection *s;
7240
7241 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7242 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7243
7244 if (info->symbolic)
7245 {
7246 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7247 return false;
7248 info->flags |= DF_SYMBOLIC;
7249 }
7250
7251 if (rpath != NULL)
7252 {
7253 size_t indx;
7254 bfd_vma tag;
7255
7256 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7257 true);
7258 if (indx == (size_t) -1)
7259 return false;
7260
7261 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7262 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7263 return false;
7264 }
7265
7266 if (filter_shlib != NULL)
7267 {
7268 size_t indx;
7269
7270 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7271 filter_shlib, true);
7272 if (indx == (size_t) -1
7273 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7274 return false;
7275 }
7276
7277 if (auxiliary_filters != NULL)
7278 {
7279 const char * const *p;
7280
7281 for (p = auxiliary_filters; *p != NULL; p++)
7282 {
7283 size_t indx;
7284
7285 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7286 *p, true);
7287 if (indx == (size_t) -1
7288 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7289 return false;
7290 }
7291 }
7292
7293 if (audit != NULL)
7294 {
7295 size_t indx;
7296
7297 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7298 true);
7299 if (indx == (size_t) -1
7300 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7301 return false;
7302 }
7303
7304 if (depaudit != NULL)
7305 {
7306 size_t indx;
7307
7308 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7309 true);
7310 if (indx == (size_t) -1
7311 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7312 return false;
7313 }
7314
7315 eif.info = info;
7316 eif.failed = false;
7317
7318 /* Find all symbols which were defined in a dynamic object and make
7319 the backend pick a reasonable value for them. */
7320 elf_link_hash_traverse (elf_hash_table (info),
7321 _bfd_elf_adjust_dynamic_symbol,
7322 &eif);
7323 if (eif.failed)
7324 return false;
7325
7326 /* Add some entries to the .dynamic section. We fill in some of the
7327 values later, in bfd_elf_final_link, but we must add the entries
7328 now so that we know the final size of the .dynamic section. */
7329
7330 /* If there are initialization and/or finalization functions to
7331 call then add the corresponding DT_INIT/DT_FINI entries. */
7332 h = (info->init_function
7333 ? elf_link_hash_lookup (elf_hash_table (info),
7334 info->init_function, false,
7335 false, false)
7336 : NULL);
7337 if (h != NULL
7338 && (h->ref_regular
7339 || h->def_regular))
7340 {
7341 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7342 return false;
7343 }
7344 h = (info->fini_function
7345 ? elf_link_hash_lookup (elf_hash_table (info),
7346 info->fini_function, false,
7347 false, false)
7348 : NULL);
7349 if (h != NULL
7350 && (h->ref_regular
7351 || h->def_regular))
7352 {
7353 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7354 return false;
7355 }
7356
7357 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7358 if (s != NULL && s->linker_has_input)
7359 {
7360 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7361 if (! bfd_link_executable (info))
7362 {
7363 bfd *sub;
7364 asection *o;
7365
7366 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7367 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7368 && (o = sub->sections) != NULL
7369 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7370 for (o = sub->sections; o != NULL; o = o->next)
7371 if (elf_section_data (o)->this_hdr.sh_type
7372 == SHT_PREINIT_ARRAY)
7373 {
7374 _bfd_error_handler
7375 (_("%pB: .preinit_array section is not allowed in DSO"),
7376 sub);
7377 break;
7378 }
7379
7380 bfd_set_error (bfd_error_nonrepresentable_section);
7381 return false;
7382 }
7383
7384 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7385 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7386 return false;
7387 }
7388 s = bfd_get_section_by_name (output_bfd, ".init_array");
7389 if (s != NULL && s->linker_has_input)
7390 {
7391 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7392 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7393 return false;
7394 }
7395 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7396 if (s != NULL && s->linker_has_input)
7397 {
7398 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7399 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7400 return false;
7401 }
7402
7403 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7404 /* If .dynstr is excluded from the link, we don't want any of
7405 these tags. Strictly, we should be checking each section
7406 individually; This quick check covers for the case where
7407 someone does a /DISCARD/ : { *(*) }. */
7408 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7409 {
7410 bfd_size_type strsize;
7411
7412 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7413 if ((info->emit_hash
7414 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7415 || (info->emit_gnu_hash
7416 && (bed->record_xhash_symbol == NULL
7417 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7418 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7419 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7420 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7421 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7422 bed->s->sizeof_sym)
7423 || (info->gnu_flags_1
7424 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7425 info->gnu_flags_1)))
7426 return false;
7427 }
7428 }
7429
7430 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7431 return false;
7432
7433 /* The backend must work out the sizes of all the other dynamic
7434 sections. */
7435 if (dynobj != NULL
7436 && bed->elf_backend_size_dynamic_sections != NULL
7437 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7438 return false;
7439
7440 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7441 {
7442 if (elf_tdata (output_bfd)->cverdefs)
7443 {
7444 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7445
7446 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7447 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7448 return false;
7449 }
7450
7451 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7452 {
7453 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7454 return false;
7455 }
7456 else if (info->flags & DF_BIND_NOW)
7457 {
7458 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7459 return false;
7460 }
7461
7462 if (info->flags_1)
7463 {
7464 if (bfd_link_executable (info))
7465 info->flags_1 &= ~ (DF_1_INITFIRST
7466 | DF_1_NODELETE
7467 | DF_1_NOOPEN);
7468 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7469 return false;
7470 }
7471
7472 if (elf_tdata (output_bfd)->cverrefs)
7473 {
7474 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7475
7476 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7477 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7478 return false;
7479 }
7480
7481 if ((elf_tdata (output_bfd)->cverrefs == 0
7482 && elf_tdata (output_bfd)->cverdefs == 0)
7483 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7484 {
7485 asection *s;
7486
7487 s = bfd_get_linker_section (dynobj, ".gnu.version");
7488 s->flags |= SEC_EXCLUDE;
7489 }
7490 }
7491 return true;
7492 }
7493
7494 /* Find the first non-excluded output section. We'll use its
7495 section symbol for some emitted relocs. */
7496 void
7497 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7498 {
7499 asection *s;
7500 asection *found = NULL;
7501
7502 for (s = output_bfd->sections; s != NULL; s = s->next)
7503 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7504 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7505 {
7506 found = s;
7507 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7508 break;
7509 }
7510 elf_hash_table (info)->text_index_section = found;
7511 }
7512
7513 /* Find two non-excluded output sections, one for code, one for data.
7514 We'll use their section symbols for some emitted relocs. */
7515 void
7516 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7517 {
7518 asection *s;
7519 asection *found = NULL;
7520
7521 /* Data first, since setting text_index_section changes
7522 _bfd_elf_omit_section_dynsym_default. */
7523 for (s = output_bfd->sections; s != NULL; s = s->next)
7524 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7525 && !(s->flags & SEC_READONLY)
7526 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7527 {
7528 found = s;
7529 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7530 break;
7531 }
7532 elf_hash_table (info)->data_index_section = found;
7533
7534 for (s = output_bfd->sections; s != NULL; s = s->next)
7535 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7536 && (s->flags & SEC_READONLY)
7537 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7538 {
7539 found = s;
7540 break;
7541 }
7542 elf_hash_table (info)->text_index_section = found;
7543 }
7544
7545 #define GNU_HASH_SECTION_NAME(bed) \
7546 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7547
7548 bool
7549 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7550 {
7551 const struct elf_backend_data *bed;
7552 unsigned long section_sym_count;
7553 bfd_size_type dynsymcount = 0;
7554
7555 if (!is_elf_hash_table (info->hash))
7556 return true;
7557
7558 bed = get_elf_backend_data (output_bfd);
7559 (*bed->elf_backend_init_index_section) (output_bfd, info);
7560
7561 /* Assign dynsym indices. In a shared library we generate a section
7562 symbol for each output section, which come first. Next come all
7563 of the back-end allocated local dynamic syms, followed by the rest
7564 of the global symbols.
7565
7566 This is usually not needed for static binaries, however backends
7567 can request to always do it, e.g. the MIPS backend uses dynamic
7568 symbol counts to lay out GOT, which will be produced in the
7569 presence of GOT relocations even in static binaries (holding fixed
7570 data in that case, to satisfy those relocations). */
7571
7572 if (elf_hash_table (info)->dynamic_sections_created
7573 || bed->always_renumber_dynsyms)
7574 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7575 &section_sym_count);
7576
7577 if (elf_hash_table (info)->dynamic_sections_created)
7578 {
7579 bfd *dynobj;
7580 asection *s;
7581 unsigned int dtagcount;
7582
7583 dynobj = elf_hash_table (info)->dynobj;
7584
7585 /* Work out the size of the symbol version section. */
7586 s = bfd_get_linker_section (dynobj, ".gnu.version");
7587 BFD_ASSERT (s != NULL);
7588 if ((s->flags & SEC_EXCLUDE) == 0)
7589 {
7590 s->size = dynsymcount * sizeof (Elf_External_Versym);
7591 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7592 if (s->contents == NULL)
7593 return false;
7594
7595 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7596 return false;
7597 }
7598
7599 /* Set the size of the .dynsym and .hash sections. We counted
7600 the number of dynamic symbols in elf_link_add_object_symbols.
7601 We will build the contents of .dynsym and .hash when we build
7602 the final symbol table, because until then we do not know the
7603 correct value to give the symbols. We built the .dynstr
7604 section as we went along in elf_link_add_object_symbols. */
7605 s = elf_hash_table (info)->dynsym;
7606 BFD_ASSERT (s != NULL);
7607 s->size = dynsymcount * bed->s->sizeof_sym;
7608
7609 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7610 if (s->contents == NULL)
7611 return false;
7612
7613 /* The first entry in .dynsym is a dummy symbol. Clear all the
7614 section syms, in case we don't output them all. */
7615 ++section_sym_count;
7616 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7617
7618 elf_hash_table (info)->bucketcount = 0;
7619
7620 /* Compute the size of the hashing table. As a side effect this
7621 computes the hash values for all the names we export. */
7622 if (info->emit_hash)
7623 {
7624 unsigned long int *hashcodes;
7625 struct hash_codes_info hashinf;
7626 bfd_size_type amt;
7627 unsigned long int nsyms;
7628 size_t bucketcount;
7629 size_t hash_entry_size;
7630
7631 /* Compute the hash values for all exported symbols. At the same
7632 time store the values in an array so that we could use them for
7633 optimizations. */
7634 amt = dynsymcount * sizeof (unsigned long int);
7635 hashcodes = (unsigned long int *) bfd_malloc (amt);
7636 if (hashcodes == NULL)
7637 return false;
7638 hashinf.hashcodes = hashcodes;
7639 hashinf.error = false;
7640
7641 /* Put all hash values in HASHCODES. */
7642 elf_link_hash_traverse (elf_hash_table (info),
7643 elf_collect_hash_codes, &hashinf);
7644 if (hashinf.error)
7645 {
7646 free (hashcodes);
7647 return false;
7648 }
7649
7650 nsyms = hashinf.hashcodes - hashcodes;
7651 bucketcount
7652 = compute_bucket_count (info, hashcodes, nsyms, 0);
7653 free (hashcodes);
7654
7655 if (bucketcount == 0 && nsyms > 0)
7656 return false;
7657
7658 elf_hash_table (info)->bucketcount = bucketcount;
7659
7660 s = bfd_get_linker_section (dynobj, ".hash");
7661 BFD_ASSERT (s != NULL);
7662 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7663 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7664 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7665 if (s->contents == NULL)
7666 return false;
7667
7668 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7669 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7670 s->contents + hash_entry_size);
7671 }
7672
7673 if (info->emit_gnu_hash)
7674 {
7675 size_t i, cnt;
7676 unsigned char *contents;
7677 struct collect_gnu_hash_codes cinfo;
7678 bfd_size_type amt;
7679 size_t bucketcount;
7680
7681 memset (&cinfo, 0, sizeof (cinfo));
7682
7683 /* Compute the hash values for all exported symbols. At the same
7684 time store the values in an array so that we could use them for
7685 optimizations. */
7686 amt = dynsymcount * 2 * sizeof (unsigned long int);
7687 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7688 if (cinfo.hashcodes == NULL)
7689 return false;
7690
7691 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7692 cinfo.min_dynindx = -1;
7693 cinfo.output_bfd = output_bfd;
7694 cinfo.bed = bed;
7695
7696 /* Put all hash values in HASHCODES. */
7697 elf_link_hash_traverse (elf_hash_table (info),
7698 elf_collect_gnu_hash_codes, &cinfo);
7699 if (cinfo.error)
7700 {
7701 free (cinfo.hashcodes);
7702 return false;
7703 }
7704
7705 bucketcount
7706 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7707
7708 if (bucketcount == 0)
7709 {
7710 free (cinfo.hashcodes);
7711 return false;
7712 }
7713
7714 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7715 BFD_ASSERT (s != NULL);
7716
7717 if (cinfo.nsyms == 0)
7718 {
7719 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7720 BFD_ASSERT (cinfo.min_dynindx == -1);
7721 free (cinfo.hashcodes);
7722 s->size = 5 * 4 + bed->s->arch_size / 8;
7723 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7724 if (contents == NULL)
7725 return false;
7726 s->contents = contents;
7727 /* 1 empty bucket. */
7728 bfd_put_32 (output_bfd, 1, contents);
7729 /* SYMIDX above the special symbol 0. */
7730 bfd_put_32 (output_bfd, 1, contents + 4);
7731 /* Just one word for bitmask. */
7732 bfd_put_32 (output_bfd, 1, contents + 8);
7733 /* Only hash fn bloom filter. */
7734 bfd_put_32 (output_bfd, 0, contents + 12);
7735 /* No hashes are valid - empty bitmask. */
7736 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7737 /* No hashes in the only bucket. */
7738 bfd_put_32 (output_bfd, 0,
7739 contents + 16 + bed->s->arch_size / 8);
7740 }
7741 else
7742 {
7743 unsigned long int maskwords, maskbitslog2, x;
7744 BFD_ASSERT (cinfo.min_dynindx != -1);
7745
7746 x = cinfo.nsyms;
7747 maskbitslog2 = 1;
7748 while ((x >>= 1) != 0)
7749 ++maskbitslog2;
7750 if (maskbitslog2 < 3)
7751 maskbitslog2 = 5;
7752 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7753 maskbitslog2 = maskbitslog2 + 3;
7754 else
7755 maskbitslog2 = maskbitslog2 + 2;
7756 if (bed->s->arch_size == 64)
7757 {
7758 if (maskbitslog2 == 5)
7759 maskbitslog2 = 6;
7760 cinfo.shift1 = 6;
7761 }
7762 else
7763 cinfo.shift1 = 5;
7764 cinfo.mask = (1 << cinfo.shift1) - 1;
7765 cinfo.shift2 = maskbitslog2;
7766 cinfo.maskbits = 1 << maskbitslog2;
7767 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7768 amt = bucketcount * sizeof (unsigned long int) * 2;
7769 amt += maskwords * sizeof (bfd_vma);
7770 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7771 if (cinfo.bitmask == NULL)
7772 {
7773 free (cinfo.hashcodes);
7774 return false;
7775 }
7776
7777 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7778 cinfo.indx = cinfo.counts + bucketcount;
7779 cinfo.symindx = dynsymcount - cinfo.nsyms;
7780 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7781
7782 /* Determine how often each hash bucket is used. */
7783 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7784 for (i = 0; i < cinfo.nsyms; ++i)
7785 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7786
7787 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7788 if (cinfo.counts[i] != 0)
7789 {
7790 cinfo.indx[i] = cnt;
7791 cnt += cinfo.counts[i];
7792 }
7793 BFD_ASSERT (cnt == dynsymcount);
7794 cinfo.bucketcount = bucketcount;
7795 cinfo.local_indx = cinfo.min_dynindx;
7796
7797 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7798 s->size += cinfo.maskbits / 8;
7799 if (bed->record_xhash_symbol != NULL)
7800 s->size += cinfo.nsyms * 4;
7801 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7802 if (contents == NULL)
7803 {
7804 free (cinfo.bitmask);
7805 free (cinfo.hashcodes);
7806 return false;
7807 }
7808
7809 s->contents = contents;
7810 bfd_put_32 (output_bfd, bucketcount, contents);
7811 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7812 bfd_put_32 (output_bfd, maskwords, contents + 8);
7813 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7814 contents += 16 + cinfo.maskbits / 8;
7815
7816 for (i = 0; i < bucketcount; ++i)
7817 {
7818 if (cinfo.counts[i] == 0)
7819 bfd_put_32 (output_bfd, 0, contents);
7820 else
7821 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7822 contents += 4;
7823 }
7824
7825 cinfo.contents = contents;
7826
7827 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7828 /* Renumber dynamic symbols, if populating .gnu.hash section.
7829 If using .MIPS.xhash, populate the translation table. */
7830 elf_link_hash_traverse (elf_hash_table (info),
7831 elf_gnu_hash_process_symidx, &cinfo);
7832
7833 contents = s->contents + 16;
7834 for (i = 0; i < maskwords; ++i)
7835 {
7836 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7837 contents);
7838 contents += bed->s->arch_size / 8;
7839 }
7840
7841 free (cinfo.bitmask);
7842 free (cinfo.hashcodes);
7843 }
7844 }
7845
7846 s = bfd_get_linker_section (dynobj, ".dynstr");
7847 BFD_ASSERT (s != NULL);
7848
7849 elf_finalize_dynstr (output_bfd, info);
7850
7851 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7852
7853 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7854 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7855 return false;
7856 }
7857
7858 return true;
7859 }
7860 \f
7861 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7862
7863 static void
7864 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7865 asection *sec)
7866 {
7867 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7868 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7869 }
7870
7871 /* Finish SHF_MERGE section merging. */
7872
7873 bool
7874 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7875 {
7876 bfd *ibfd;
7877 asection *sec;
7878
7879 if (!is_elf_hash_table (info->hash))
7880 return false;
7881
7882 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7883 if ((ibfd->flags & DYNAMIC) == 0
7884 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7885 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7886 == get_elf_backend_data (obfd)->s->elfclass))
7887 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7888 if ((sec->flags & SEC_MERGE) != 0
7889 && !bfd_is_abs_section (sec->output_section))
7890 {
7891 struct bfd_elf_section_data *secdata;
7892
7893 secdata = elf_section_data (sec);
7894 if (! _bfd_add_merge_section (obfd,
7895 &elf_hash_table (info)->merge_info,
7896 sec, &secdata->sec_info))
7897 return false;
7898 else if (secdata->sec_info)
7899 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7900 }
7901
7902 if (elf_hash_table (info)->merge_info != NULL)
7903 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7904 merge_sections_remove_hook);
7905 return true;
7906 }
7907
7908 /* Create an entry in an ELF linker hash table. */
7909
7910 struct bfd_hash_entry *
7911 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7912 struct bfd_hash_table *table,
7913 const char *string)
7914 {
7915 /* Allocate the structure if it has not already been allocated by a
7916 subclass. */
7917 if (entry == NULL)
7918 {
7919 entry = (struct bfd_hash_entry *)
7920 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7921 if (entry == NULL)
7922 return entry;
7923 }
7924
7925 /* Call the allocation method of the superclass. */
7926 entry = _bfd_link_hash_newfunc (entry, table, string);
7927 if (entry != NULL)
7928 {
7929 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7930 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7931
7932 /* Set local fields. */
7933 ret->indx = -1;
7934 ret->dynindx = -1;
7935 ret->got = htab->init_got_refcount;
7936 ret->plt = htab->init_plt_refcount;
7937 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7938 - offsetof (struct elf_link_hash_entry, size)));
7939 /* Assume that we have been called by a non-ELF symbol reader.
7940 This flag is then reset by the code which reads an ELF input
7941 file. This ensures that a symbol created by a non-ELF symbol
7942 reader will have the flag set correctly. */
7943 ret->non_elf = 1;
7944 }
7945
7946 return entry;
7947 }
7948
7949 /* Copy data from an indirect symbol to its direct symbol, hiding the
7950 old indirect symbol. Also used for copying flags to a weakdef. */
7951
7952 void
7953 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7954 struct elf_link_hash_entry *dir,
7955 struct elf_link_hash_entry *ind)
7956 {
7957 struct elf_link_hash_table *htab;
7958
7959 if (ind->dyn_relocs != NULL)
7960 {
7961 if (dir->dyn_relocs != NULL)
7962 {
7963 struct elf_dyn_relocs **pp;
7964 struct elf_dyn_relocs *p;
7965
7966 /* Add reloc counts against the indirect sym to the direct sym
7967 list. Merge any entries against the same section. */
7968 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7969 {
7970 struct elf_dyn_relocs *q;
7971
7972 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7973 if (q->sec == p->sec)
7974 {
7975 q->pc_count += p->pc_count;
7976 q->count += p->count;
7977 *pp = p->next;
7978 break;
7979 }
7980 if (q == NULL)
7981 pp = &p->next;
7982 }
7983 *pp = dir->dyn_relocs;
7984 }
7985
7986 dir->dyn_relocs = ind->dyn_relocs;
7987 ind->dyn_relocs = NULL;
7988 }
7989
7990 /* Copy down any references that we may have already seen to the
7991 symbol which just became indirect. */
7992
7993 if (dir->versioned != versioned_hidden)
7994 dir->ref_dynamic |= ind->ref_dynamic;
7995 dir->ref_regular |= ind->ref_regular;
7996 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7997 dir->non_got_ref |= ind->non_got_ref;
7998 dir->needs_plt |= ind->needs_plt;
7999 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8000
8001 if (ind->root.type != bfd_link_hash_indirect)
8002 return;
8003
8004 /* Copy over the global and procedure linkage table refcount entries.
8005 These may have been already set up by a check_relocs routine. */
8006 htab = elf_hash_table (info);
8007 if (ind->got.refcount > htab->init_got_refcount.refcount)
8008 {
8009 if (dir->got.refcount < 0)
8010 dir->got.refcount = 0;
8011 dir->got.refcount += ind->got.refcount;
8012 ind->got.refcount = htab->init_got_refcount.refcount;
8013 }
8014
8015 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8016 {
8017 if (dir->plt.refcount < 0)
8018 dir->plt.refcount = 0;
8019 dir->plt.refcount += ind->plt.refcount;
8020 ind->plt.refcount = htab->init_plt_refcount.refcount;
8021 }
8022
8023 if (ind->dynindx != -1)
8024 {
8025 if (dir->dynindx != -1)
8026 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8027 dir->dynindx = ind->dynindx;
8028 dir->dynstr_index = ind->dynstr_index;
8029 ind->dynindx = -1;
8030 ind->dynstr_index = 0;
8031 }
8032 }
8033
8034 void
8035 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8036 struct elf_link_hash_entry *h,
8037 bool force_local)
8038 {
8039 /* STT_GNU_IFUNC symbol must go through PLT. */
8040 if (h->type != STT_GNU_IFUNC)
8041 {
8042 h->plt = elf_hash_table (info)->init_plt_offset;
8043 h->needs_plt = 0;
8044 }
8045 if (force_local)
8046 {
8047 h->forced_local = 1;
8048 if (h->dynindx != -1)
8049 {
8050 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8051 h->dynstr_index);
8052 h->dynindx = -1;
8053 h->dynstr_index = 0;
8054 }
8055 }
8056 }
8057
8058 /* Hide a symbol. */
8059
8060 void
8061 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8062 struct bfd_link_info *info,
8063 struct bfd_link_hash_entry *h)
8064 {
8065 if (is_elf_hash_table (info->hash))
8066 {
8067 const struct elf_backend_data *bed
8068 = get_elf_backend_data (output_bfd);
8069 struct elf_link_hash_entry *eh
8070 = (struct elf_link_hash_entry *) h;
8071 bed->elf_backend_hide_symbol (info, eh, true);
8072 eh->def_dynamic = 0;
8073 eh->ref_dynamic = 0;
8074 eh->dynamic_def = 0;
8075 }
8076 }
8077
8078 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8079 caller. */
8080
8081 bool
8082 _bfd_elf_link_hash_table_init
8083 (struct elf_link_hash_table *table,
8084 bfd *abfd,
8085 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8086 struct bfd_hash_table *,
8087 const char *),
8088 unsigned int entsize,
8089 enum elf_target_id target_id)
8090 {
8091 bool ret;
8092 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8093
8094 table->init_got_refcount.refcount = can_refcount - 1;
8095 table->init_plt_refcount.refcount = can_refcount - 1;
8096 table->init_got_offset.offset = -(bfd_vma) 1;
8097 table->init_plt_offset.offset = -(bfd_vma) 1;
8098 /* The first dynamic symbol is a dummy. */
8099 table->dynsymcount = 1;
8100
8101 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8102
8103 table->root.type = bfd_link_elf_hash_table;
8104 table->hash_table_id = target_id;
8105 table->target_os = get_elf_backend_data (abfd)->target_os;
8106
8107 return ret;
8108 }
8109
8110 /* Create an ELF linker hash table. */
8111
8112 struct bfd_link_hash_table *
8113 _bfd_elf_link_hash_table_create (bfd *abfd)
8114 {
8115 struct elf_link_hash_table *ret;
8116 size_t amt = sizeof (struct elf_link_hash_table);
8117
8118 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8119 if (ret == NULL)
8120 return NULL;
8121
8122 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8123 sizeof (struct elf_link_hash_entry),
8124 GENERIC_ELF_DATA))
8125 {
8126 free (ret);
8127 return NULL;
8128 }
8129 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8130
8131 return &ret->root;
8132 }
8133
8134 /* Destroy an ELF linker hash table. */
8135
8136 void
8137 _bfd_elf_link_hash_table_free (bfd *obfd)
8138 {
8139 struct elf_link_hash_table *htab;
8140
8141 htab = (struct elf_link_hash_table *) obfd->link.hash;
8142 if (htab->dynstr != NULL)
8143 _bfd_elf_strtab_free (htab->dynstr);
8144 _bfd_merge_sections_free (htab->merge_info);
8145 _bfd_generic_link_hash_table_free (obfd);
8146 }
8147
8148 /* This is a hook for the ELF emulation code in the generic linker to
8149 tell the backend linker what file name to use for the DT_NEEDED
8150 entry for a dynamic object. */
8151
8152 void
8153 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8154 {
8155 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8156 && bfd_get_format (abfd) == bfd_object)
8157 elf_dt_name (abfd) = name;
8158 }
8159
8160 int
8161 bfd_elf_get_dyn_lib_class (bfd *abfd)
8162 {
8163 int lib_class;
8164 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8165 && bfd_get_format (abfd) == bfd_object)
8166 lib_class = elf_dyn_lib_class (abfd);
8167 else
8168 lib_class = 0;
8169 return lib_class;
8170 }
8171
8172 void
8173 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8174 {
8175 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8176 && bfd_get_format (abfd) == bfd_object)
8177 elf_dyn_lib_class (abfd) = lib_class;
8178 }
8179
8180 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8181 the linker ELF emulation code. */
8182
8183 struct bfd_link_needed_list *
8184 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8185 struct bfd_link_info *info)
8186 {
8187 if (! is_elf_hash_table (info->hash))
8188 return NULL;
8189 return elf_hash_table (info)->needed;
8190 }
8191
8192 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8193 hook for the linker ELF emulation code. */
8194
8195 struct bfd_link_needed_list *
8196 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8197 struct bfd_link_info *info)
8198 {
8199 if (! is_elf_hash_table (info->hash))
8200 return NULL;
8201 return elf_hash_table (info)->runpath;
8202 }
8203
8204 /* Get the name actually used for a dynamic object for a link. This
8205 is the SONAME entry if there is one. Otherwise, it is the string
8206 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8207
8208 const char *
8209 bfd_elf_get_dt_soname (bfd *abfd)
8210 {
8211 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8212 && bfd_get_format (abfd) == bfd_object)
8213 return elf_dt_name (abfd);
8214 return NULL;
8215 }
8216
8217 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8218 the ELF linker emulation code. */
8219
8220 bool
8221 bfd_elf_get_bfd_needed_list (bfd *abfd,
8222 struct bfd_link_needed_list **pneeded)
8223 {
8224 asection *s;
8225 bfd_byte *dynbuf = NULL;
8226 unsigned int elfsec;
8227 unsigned long shlink;
8228 bfd_byte *extdyn, *extdynend;
8229 size_t extdynsize;
8230 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8231
8232 *pneeded = NULL;
8233
8234 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8235 || bfd_get_format (abfd) != bfd_object)
8236 return true;
8237
8238 s = bfd_get_section_by_name (abfd, ".dynamic");
8239 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8240 return true;
8241
8242 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8243 goto error_return;
8244
8245 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8246 if (elfsec == SHN_BAD)
8247 goto error_return;
8248
8249 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8250
8251 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8252 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8253
8254 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8255 (size_t) (extdynend - extdyn) >= extdynsize;
8256 extdyn += extdynsize)
8257 {
8258 Elf_Internal_Dyn dyn;
8259
8260 (*swap_dyn_in) (abfd, extdyn, &dyn);
8261
8262 if (dyn.d_tag == DT_NULL)
8263 break;
8264
8265 if (dyn.d_tag == DT_NEEDED)
8266 {
8267 const char *string;
8268 struct bfd_link_needed_list *l;
8269 unsigned int tagv = dyn.d_un.d_val;
8270 size_t amt;
8271
8272 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8273 if (string == NULL)
8274 goto error_return;
8275
8276 amt = sizeof *l;
8277 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8278 if (l == NULL)
8279 goto error_return;
8280
8281 l->by = abfd;
8282 l->name = string;
8283 l->next = *pneeded;
8284 *pneeded = l;
8285 }
8286 }
8287
8288 free (dynbuf);
8289
8290 return true;
8291
8292 error_return:
8293 free (dynbuf);
8294 return false;
8295 }
8296
8297 struct elf_symbuf_symbol
8298 {
8299 unsigned long st_name; /* Symbol name, index in string tbl */
8300 unsigned char st_info; /* Type and binding attributes */
8301 unsigned char st_other; /* Visibilty, and target specific */
8302 };
8303
8304 struct elf_symbuf_head
8305 {
8306 struct elf_symbuf_symbol *ssym;
8307 size_t count;
8308 unsigned int st_shndx;
8309 };
8310
8311 struct elf_symbol
8312 {
8313 union
8314 {
8315 Elf_Internal_Sym *isym;
8316 struct elf_symbuf_symbol *ssym;
8317 void *p;
8318 } u;
8319 const char *name;
8320 };
8321
8322 /* Sort references to symbols by ascending section number. */
8323
8324 static int
8325 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8326 {
8327 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8328 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8329
8330 if (s1->st_shndx != s2->st_shndx)
8331 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8332 /* Final sort by the address of the sym in the symbuf ensures
8333 a stable sort. */
8334 if (s1 != s2)
8335 return s1 > s2 ? 1 : -1;
8336 return 0;
8337 }
8338
8339 static int
8340 elf_sym_name_compare (const void *arg1, const void *arg2)
8341 {
8342 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8343 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8344 int ret = strcmp (s1->name, s2->name);
8345 if (ret != 0)
8346 return ret;
8347 if (s1->u.p != s2->u.p)
8348 return s1->u.p > s2->u.p ? 1 : -1;
8349 return 0;
8350 }
8351
8352 static struct elf_symbuf_head *
8353 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8354 {
8355 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8356 struct elf_symbuf_symbol *ssym;
8357 struct elf_symbuf_head *ssymbuf, *ssymhead;
8358 size_t i, shndx_count, total_size, amt;
8359
8360 amt = symcount * sizeof (*indbuf);
8361 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8362 if (indbuf == NULL)
8363 return NULL;
8364
8365 for (ind = indbuf, i = 0; i < symcount; i++)
8366 if (isymbuf[i].st_shndx != SHN_UNDEF)
8367 *ind++ = &isymbuf[i];
8368 indbufend = ind;
8369
8370 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8371 elf_sort_elf_symbol);
8372
8373 shndx_count = 0;
8374 if (indbufend > indbuf)
8375 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8376 if (ind[0]->st_shndx != ind[1]->st_shndx)
8377 shndx_count++;
8378
8379 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8380 + (indbufend - indbuf) * sizeof (*ssym));
8381 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8382 if (ssymbuf == NULL)
8383 {
8384 free (indbuf);
8385 return NULL;
8386 }
8387
8388 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8389 ssymbuf->ssym = NULL;
8390 ssymbuf->count = shndx_count;
8391 ssymbuf->st_shndx = 0;
8392 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8393 {
8394 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8395 {
8396 ssymhead++;
8397 ssymhead->ssym = ssym;
8398 ssymhead->count = 0;
8399 ssymhead->st_shndx = (*ind)->st_shndx;
8400 }
8401 ssym->st_name = (*ind)->st_name;
8402 ssym->st_info = (*ind)->st_info;
8403 ssym->st_other = (*ind)->st_other;
8404 ssymhead->count++;
8405 }
8406 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8407 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8408
8409 free (indbuf);
8410 return ssymbuf;
8411 }
8412
8413 /* Check if 2 sections define the same set of local and global
8414 symbols. */
8415
8416 static bool
8417 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8418 struct bfd_link_info *info)
8419 {
8420 bfd *bfd1, *bfd2;
8421 const struct elf_backend_data *bed1, *bed2;
8422 Elf_Internal_Shdr *hdr1, *hdr2;
8423 size_t symcount1, symcount2;
8424 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8425 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8426 Elf_Internal_Sym *isym, *isymend;
8427 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8428 size_t count1, count2, sec_count1, sec_count2, i;
8429 unsigned int shndx1, shndx2;
8430 bool result;
8431 bool ignore_section_symbol_p;
8432
8433 bfd1 = sec1->owner;
8434 bfd2 = sec2->owner;
8435
8436 /* Both sections have to be in ELF. */
8437 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8438 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8439 return false;
8440
8441 if (elf_section_type (sec1) != elf_section_type (sec2))
8442 return false;
8443
8444 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8445 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8446 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8447 return false;
8448
8449 bed1 = get_elf_backend_data (bfd1);
8450 bed2 = get_elf_backend_data (bfd2);
8451 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8452 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8453 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8454 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8455
8456 if (symcount1 == 0 || symcount2 == 0)
8457 return false;
8458
8459 result = false;
8460 isymbuf1 = NULL;
8461 isymbuf2 = NULL;
8462 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8463 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8464
8465 /* Ignore section symbols only when matching non-debugging sections
8466 or linkonce section with comdat section. */
8467 ignore_section_symbol_p
8468 = ((sec1->flags & SEC_DEBUGGING) == 0
8469 || ((elf_section_flags (sec1) & SHF_GROUP)
8470 != (elf_section_flags (sec2) & SHF_GROUP)));
8471
8472 if (ssymbuf1 == NULL)
8473 {
8474 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8475 NULL, NULL, NULL);
8476 if (isymbuf1 == NULL)
8477 goto done;
8478
8479 if (info != NULL && !info->reduce_memory_overheads)
8480 {
8481 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8482 elf_tdata (bfd1)->symbuf = ssymbuf1;
8483 }
8484 }
8485
8486 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8487 {
8488 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8489 NULL, NULL, NULL);
8490 if (isymbuf2 == NULL)
8491 goto done;
8492
8493 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8494 {
8495 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8496 elf_tdata (bfd2)->symbuf = ssymbuf2;
8497 }
8498 }
8499
8500 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8501 {
8502 /* Optimized faster version. */
8503 size_t lo, hi, mid;
8504 struct elf_symbol *symp;
8505 struct elf_symbuf_symbol *ssym, *ssymend;
8506
8507 lo = 0;
8508 hi = ssymbuf1->count;
8509 ssymbuf1++;
8510 count1 = 0;
8511 sec_count1 = 0;
8512 while (lo < hi)
8513 {
8514 mid = (lo + hi) / 2;
8515 if (shndx1 < ssymbuf1[mid].st_shndx)
8516 hi = mid;
8517 else if (shndx1 > ssymbuf1[mid].st_shndx)
8518 lo = mid + 1;
8519 else
8520 {
8521 count1 = ssymbuf1[mid].count;
8522 ssymbuf1 += mid;
8523 break;
8524 }
8525 }
8526 if (ignore_section_symbol_p)
8527 {
8528 for (i = 0; i < count1; i++)
8529 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8530 sec_count1++;
8531 count1 -= sec_count1;
8532 }
8533
8534 lo = 0;
8535 hi = ssymbuf2->count;
8536 ssymbuf2++;
8537 count2 = 0;
8538 sec_count2 = 0;
8539 while (lo < hi)
8540 {
8541 mid = (lo + hi) / 2;
8542 if (shndx2 < ssymbuf2[mid].st_shndx)
8543 hi = mid;
8544 else if (shndx2 > ssymbuf2[mid].st_shndx)
8545 lo = mid + 1;
8546 else
8547 {
8548 count2 = ssymbuf2[mid].count;
8549 ssymbuf2 += mid;
8550 break;
8551 }
8552 }
8553 if (ignore_section_symbol_p)
8554 {
8555 for (i = 0; i < count2; i++)
8556 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8557 sec_count2++;
8558 count2 -= sec_count2;
8559 }
8560
8561 if (count1 == 0 || count2 == 0 || count1 != count2)
8562 goto done;
8563
8564 symtable1
8565 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8566 symtable2
8567 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8568 if (symtable1 == NULL || symtable2 == NULL)
8569 goto done;
8570
8571 symp = symtable1;
8572 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8573 ssym < ssymend; ssym++)
8574 if (sec_count1 == 0
8575 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8576 {
8577 symp->u.ssym = ssym;
8578 symp->name = bfd_elf_string_from_elf_section (bfd1,
8579 hdr1->sh_link,
8580 ssym->st_name);
8581 symp++;
8582 }
8583
8584 symp = symtable2;
8585 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8586 ssym < ssymend; ssym++)
8587 if (sec_count2 == 0
8588 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8589 {
8590 symp->u.ssym = ssym;
8591 symp->name = bfd_elf_string_from_elf_section (bfd2,
8592 hdr2->sh_link,
8593 ssym->st_name);
8594 symp++;
8595 }
8596
8597 /* Sort symbol by name. */
8598 qsort (symtable1, count1, sizeof (struct elf_symbol),
8599 elf_sym_name_compare);
8600 qsort (symtable2, count1, sizeof (struct elf_symbol),
8601 elf_sym_name_compare);
8602
8603 for (i = 0; i < count1; i++)
8604 /* Two symbols must have the same binding, type and name. */
8605 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8606 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8607 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8608 goto done;
8609
8610 result = true;
8611 goto done;
8612 }
8613
8614 symtable1 = (struct elf_symbol *)
8615 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8616 symtable2 = (struct elf_symbol *)
8617 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8618 if (symtable1 == NULL || symtable2 == NULL)
8619 goto done;
8620
8621 /* Count definitions in the section. */
8622 count1 = 0;
8623 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8624 if (isym->st_shndx == shndx1
8625 && (!ignore_section_symbol_p
8626 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8627 symtable1[count1++].u.isym = isym;
8628
8629 count2 = 0;
8630 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8631 if (isym->st_shndx == shndx2
8632 && (!ignore_section_symbol_p
8633 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8634 symtable2[count2++].u.isym = isym;
8635
8636 if (count1 == 0 || count2 == 0 || count1 != count2)
8637 goto done;
8638
8639 for (i = 0; i < count1; i++)
8640 symtable1[i].name
8641 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8642 symtable1[i].u.isym->st_name);
8643
8644 for (i = 0; i < count2; i++)
8645 symtable2[i].name
8646 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8647 symtable2[i].u.isym->st_name);
8648
8649 /* Sort symbol by name. */
8650 qsort (symtable1, count1, sizeof (struct elf_symbol),
8651 elf_sym_name_compare);
8652 qsort (symtable2, count1, sizeof (struct elf_symbol),
8653 elf_sym_name_compare);
8654
8655 for (i = 0; i < count1; i++)
8656 /* Two symbols must have the same binding, type and name. */
8657 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8658 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8659 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8660 goto done;
8661
8662 result = true;
8663
8664 done:
8665 free (symtable1);
8666 free (symtable2);
8667 free (isymbuf1);
8668 free (isymbuf2);
8669
8670 return result;
8671 }
8672
8673 /* Return TRUE if 2 section types are compatible. */
8674
8675 bool
8676 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8677 bfd *bbfd, const asection *bsec)
8678 {
8679 if (asec == NULL
8680 || bsec == NULL
8681 || abfd->xvec->flavour != bfd_target_elf_flavour
8682 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8683 return true;
8684
8685 return elf_section_type (asec) == elf_section_type (bsec);
8686 }
8687 \f
8688 /* Final phase of ELF linker. */
8689
8690 /* A structure we use to avoid passing large numbers of arguments. */
8691
8692 struct elf_final_link_info
8693 {
8694 /* General link information. */
8695 struct bfd_link_info *info;
8696 /* Output BFD. */
8697 bfd *output_bfd;
8698 /* Symbol string table. */
8699 struct elf_strtab_hash *symstrtab;
8700 /* .hash section. */
8701 asection *hash_sec;
8702 /* symbol version section (.gnu.version). */
8703 asection *symver_sec;
8704 /* Buffer large enough to hold contents of any section. */
8705 bfd_byte *contents;
8706 /* Buffer large enough to hold external relocs of any section. */
8707 void *external_relocs;
8708 /* Buffer large enough to hold internal relocs of any section. */
8709 Elf_Internal_Rela *internal_relocs;
8710 /* Buffer large enough to hold external local symbols of any input
8711 BFD. */
8712 bfd_byte *external_syms;
8713 /* And a buffer for symbol section indices. */
8714 Elf_External_Sym_Shndx *locsym_shndx;
8715 /* Buffer large enough to hold internal local symbols of any input
8716 BFD. */
8717 Elf_Internal_Sym *internal_syms;
8718 /* Array large enough to hold a symbol index for each local symbol
8719 of any input BFD. */
8720 long *indices;
8721 /* Array large enough to hold a section pointer for each local
8722 symbol of any input BFD. */
8723 asection **sections;
8724 /* Buffer for SHT_SYMTAB_SHNDX section. */
8725 Elf_External_Sym_Shndx *symshndxbuf;
8726 /* Number of STT_FILE syms seen. */
8727 size_t filesym_count;
8728 /* Local symbol hash table. */
8729 struct bfd_hash_table local_hash_table;
8730 };
8731
8732 struct local_hash_entry
8733 {
8734 /* Base hash table entry structure. */
8735 struct bfd_hash_entry root;
8736 /* Size of the local symbol name. */
8737 size_t size;
8738 /* Number of the duplicated local symbol names. */
8739 long count;
8740 };
8741
8742 /* Create an entry in the local symbol hash table. */
8743
8744 static struct bfd_hash_entry *
8745 local_hash_newfunc (struct bfd_hash_entry *entry,
8746 struct bfd_hash_table *table,
8747 const char *string)
8748 {
8749
8750 /* Allocate the structure if it has not already been allocated by a
8751 subclass. */
8752 if (entry == NULL)
8753 {
8754 entry = bfd_hash_allocate (table,
8755 sizeof (struct local_hash_entry));
8756 if (entry == NULL)
8757 return entry;
8758 }
8759
8760 /* Call the allocation method of the superclass. */
8761 entry = bfd_hash_newfunc (entry, table, string);
8762 if (entry != NULL)
8763 {
8764 ((struct local_hash_entry *) entry)->count = 0;
8765 ((struct local_hash_entry *) entry)->size = 0;
8766 }
8767
8768 return entry;
8769 }
8770
8771 /* This struct is used to pass information to elf_link_output_extsym. */
8772
8773 struct elf_outext_info
8774 {
8775 bool failed;
8776 bool localsyms;
8777 bool file_sym_done;
8778 struct elf_final_link_info *flinfo;
8779 };
8780
8781
8782 /* Support for evaluating a complex relocation.
8783
8784 Complex relocations are generalized, self-describing relocations. The
8785 implementation of them consists of two parts: complex symbols, and the
8786 relocations themselves.
8787
8788 The relocations use a reserved elf-wide relocation type code (R_RELC
8789 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8790 information (start bit, end bit, word width, etc) into the addend. This
8791 information is extracted from CGEN-generated operand tables within gas.
8792
8793 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8794 internal) representing prefix-notation expressions, including but not
8795 limited to those sorts of expressions normally encoded as addends in the
8796 addend field. The symbol mangling format is:
8797
8798 <node> := <literal>
8799 | <unary-operator> ':' <node>
8800 | <binary-operator> ':' <node> ':' <node>
8801 ;
8802
8803 <literal> := 's' <digits=N> ':' <N character symbol name>
8804 | 'S' <digits=N> ':' <N character section name>
8805 | '#' <hexdigits>
8806 ;
8807
8808 <binary-operator> := as in C
8809 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8810
8811 static void
8812 set_symbol_value (bfd *bfd_with_globals,
8813 Elf_Internal_Sym *isymbuf,
8814 size_t locsymcount,
8815 size_t symidx,
8816 bfd_vma val)
8817 {
8818 struct elf_link_hash_entry **sym_hashes;
8819 struct elf_link_hash_entry *h;
8820 size_t extsymoff = locsymcount;
8821
8822 if (symidx < locsymcount)
8823 {
8824 Elf_Internal_Sym *sym;
8825
8826 sym = isymbuf + symidx;
8827 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8828 {
8829 /* It is a local symbol: move it to the
8830 "absolute" section and give it a value. */
8831 sym->st_shndx = SHN_ABS;
8832 sym->st_value = val;
8833 return;
8834 }
8835 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8836 extsymoff = 0;
8837 }
8838
8839 /* It is a global symbol: set its link type
8840 to "defined" and give it a value. */
8841
8842 sym_hashes = elf_sym_hashes (bfd_with_globals);
8843 h = sym_hashes [symidx - extsymoff];
8844 while (h->root.type == bfd_link_hash_indirect
8845 || h->root.type == bfd_link_hash_warning)
8846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8847 h->root.type = bfd_link_hash_defined;
8848 h->root.u.def.value = val;
8849 h->root.u.def.section = bfd_abs_section_ptr;
8850 }
8851
8852 static bool
8853 resolve_symbol (const char *name,
8854 bfd *input_bfd,
8855 struct elf_final_link_info *flinfo,
8856 bfd_vma *result,
8857 Elf_Internal_Sym *isymbuf,
8858 size_t locsymcount)
8859 {
8860 Elf_Internal_Sym *sym;
8861 struct bfd_link_hash_entry *global_entry;
8862 const char *candidate = NULL;
8863 Elf_Internal_Shdr *symtab_hdr;
8864 size_t i;
8865
8866 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8867
8868 for (i = 0; i < locsymcount; ++ i)
8869 {
8870 sym = isymbuf + i;
8871
8872 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8873 continue;
8874
8875 candidate = bfd_elf_string_from_elf_section (input_bfd,
8876 symtab_hdr->sh_link,
8877 sym->st_name);
8878 #ifdef DEBUG
8879 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8880 name, candidate, (unsigned long) sym->st_value);
8881 #endif
8882 if (candidate && strcmp (candidate, name) == 0)
8883 {
8884 asection *sec = flinfo->sections [i];
8885
8886 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8887 *result += sec->output_offset + sec->output_section->vma;
8888 #ifdef DEBUG
8889 printf ("Found symbol with value %8.8lx\n",
8890 (unsigned long) *result);
8891 #endif
8892 return true;
8893 }
8894 }
8895
8896 /* Hmm, haven't found it yet. perhaps it is a global. */
8897 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8898 false, false, true);
8899 if (!global_entry)
8900 return false;
8901
8902 if (global_entry->type == bfd_link_hash_defined
8903 || global_entry->type == bfd_link_hash_defweak)
8904 {
8905 *result = (global_entry->u.def.value
8906 + global_entry->u.def.section->output_section->vma
8907 + global_entry->u.def.section->output_offset);
8908 #ifdef DEBUG
8909 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8910 global_entry->root.string, (unsigned long) *result);
8911 #endif
8912 return true;
8913 }
8914
8915 return false;
8916 }
8917
8918 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8919 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8920 names like "foo.end" which is the end address of section "foo". */
8921
8922 static bool
8923 resolve_section (const char *name,
8924 asection *sections,
8925 bfd_vma *result,
8926 bfd * abfd)
8927 {
8928 asection *curr;
8929 unsigned int len;
8930
8931 for (curr = sections; curr; curr = curr->next)
8932 if (strcmp (curr->name, name) == 0)
8933 {
8934 *result = curr->vma;
8935 return true;
8936 }
8937
8938 /* Hmm. still haven't found it. try pseudo-section names. */
8939 /* FIXME: This could be coded more efficiently... */
8940 for (curr = sections; curr; curr = curr->next)
8941 {
8942 len = strlen (curr->name);
8943 if (len > strlen (name))
8944 continue;
8945
8946 if (strncmp (curr->name, name, len) == 0)
8947 {
8948 if (startswith (name + len, ".end"))
8949 {
8950 *result = (curr->vma
8951 + curr->size / bfd_octets_per_byte (abfd, curr));
8952 return true;
8953 }
8954
8955 /* Insert more pseudo-section names here, if you like. */
8956 }
8957 }
8958
8959 return false;
8960 }
8961
8962 static void
8963 undefined_reference (const char *reftype, const char *name)
8964 {
8965 /* xgettext:c-format */
8966 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8967 reftype, name);
8968 bfd_set_error (bfd_error_bad_value);
8969 }
8970
8971 static bool
8972 eval_symbol (bfd_vma *result,
8973 const char **symp,
8974 bfd *input_bfd,
8975 struct elf_final_link_info *flinfo,
8976 bfd_vma dot,
8977 Elf_Internal_Sym *isymbuf,
8978 size_t locsymcount,
8979 int signed_p)
8980 {
8981 size_t len;
8982 size_t symlen;
8983 bfd_vma a;
8984 bfd_vma b;
8985 char symbuf[4096];
8986 const char *sym = *symp;
8987 const char *symend;
8988 bool symbol_is_section = false;
8989
8990 len = strlen (sym);
8991 symend = sym + len;
8992
8993 if (len < 1 || len > sizeof (symbuf))
8994 {
8995 bfd_set_error (bfd_error_invalid_operation);
8996 return false;
8997 }
8998
8999 switch (* sym)
9000 {
9001 case '.':
9002 *result = dot;
9003 *symp = sym + 1;
9004 return true;
9005
9006 case '#':
9007 ++sym;
9008 *result = strtoul (sym, (char **) symp, 16);
9009 return true;
9010
9011 case 'S':
9012 symbol_is_section = true;
9013 /* Fall through. */
9014 case 's':
9015 ++sym;
9016 symlen = strtol (sym, (char **) symp, 10);
9017 sym = *symp + 1; /* Skip the trailing ':'. */
9018
9019 if (symend < sym || symlen + 1 > sizeof (symbuf))
9020 {
9021 bfd_set_error (bfd_error_invalid_operation);
9022 return false;
9023 }
9024
9025 memcpy (symbuf, sym, symlen);
9026 symbuf[symlen] = '\0';
9027 *symp = sym + symlen;
9028
9029 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9030 the symbol as a section, or vice-versa. so we're pretty liberal in our
9031 interpretation here; section means "try section first", not "must be a
9032 section", and likewise with symbol. */
9033
9034 if (symbol_is_section)
9035 {
9036 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9037 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9038 isymbuf, locsymcount))
9039 {
9040 undefined_reference ("section", symbuf);
9041 return false;
9042 }
9043 }
9044 else
9045 {
9046 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9047 isymbuf, locsymcount)
9048 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9049 result, input_bfd))
9050 {
9051 undefined_reference ("symbol", symbuf);
9052 return false;
9053 }
9054 }
9055
9056 return true;
9057
9058 /* All that remains are operators. */
9059
9060 #define UNARY_OP(op) \
9061 if (startswith (sym, #op)) \
9062 { \
9063 sym += strlen (#op); \
9064 if (*sym == ':') \
9065 ++sym; \
9066 *symp = sym; \
9067 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9068 isymbuf, locsymcount, signed_p)) \
9069 return false; \
9070 if (signed_p) \
9071 *result = op ((bfd_signed_vma) a); \
9072 else \
9073 *result = op a; \
9074 return true; \
9075 }
9076
9077 #define BINARY_OP_HEAD(op) \
9078 if (startswith (sym, #op)) \
9079 { \
9080 sym += strlen (#op); \
9081 if (*sym == ':') \
9082 ++sym; \
9083 *symp = sym; \
9084 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9085 isymbuf, locsymcount, signed_p)) \
9086 return false; \
9087 ++*symp; \
9088 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9089 isymbuf, locsymcount, signed_p)) \
9090 return false;
9091 #define BINARY_OP_TAIL(op) \
9092 if (signed_p) \
9093 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9094 else \
9095 *result = a op b; \
9096 return true; \
9097 }
9098 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9099
9100 default:
9101 UNARY_OP (0-);
9102 BINARY_OP_HEAD (<<);
9103 if (b >= sizeof (a) * CHAR_BIT)
9104 {
9105 *result = 0;
9106 return true;
9107 }
9108 signed_p = 0;
9109 BINARY_OP_TAIL (<<);
9110 BINARY_OP_HEAD (>>);
9111 if (b >= sizeof (a) * CHAR_BIT)
9112 {
9113 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9114 return true;
9115 }
9116 BINARY_OP_TAIL (>>);
9117 BINARY_OP (==);
9118 BINARY_OP (!=);
9119 BINARY_OP (<=);
9120 BINARY_OP (>=);
9121 BINARY_OP (&&);
9122 BINARY_OP (||);
9123 UNARY_OP (~);
9124 UNARY_OP (!);
9125 BINARY_OP (*);
9126 BINARY_OP_HEAD (/);
9127 if (b == 0)
9128 {
9129 _bfd_error_handler (_("division by zero"));
9130 bfd_set_error (bfd_error_bad_value);
9131 return false;
9132 }
9133 BINARY_OP_TAIL (/);
9134 BINARY_OP_HEAD (%);
9135 if (b == 0)
9136 {
9137 _bfd_error_handler (_("division by zero"));
9138 bfd_set_error (bfd_error_bad_value);
9139 return false;
9140 }
9141 BINARY_OP_TAIL (%);
9142 BINARY_OP (^);
9143 BINARY_OP (|);
9144 BINARY_OP (&);
9145 BINARY_OP (+);
9146 BINARY_OP (-);
9147 BINARY_OP (<);
9148 BINARY_OP (>);
9149 #undef UNARY_OP
9150 #undef BINARY_OP
9151 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9152 bfd_set_error (bfd_error_invalid_operation);
9153 return false;
9154 }
9155 }
9156
9157 static void
9158 put_value (bfd_vma size,
9159 unsigned long chunksz,
9160 bfd *input_bfd,
9161 bfd_vma x,
9162 bfd_byte *location)
9163 {
9164 location += (size - chunksz);
9165
9166 for (; size; size -= chunksz, location -= chunksz)
9167 {
9168 switch (chunksz)
9169 {
9170 case 1:
9171 bfd_put_8 (input_bfd, x, location);
9172 x >>= 8;
9173 break;
9174 case 2:
9175 bfd_put_16 (input_bfd, x, location);
9176 x >>= 16;
9177 break;
9178 case 4:
9179 bfd_put_32 (input_bfd, x, location);
9180 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9181 x >>= 16;
9182 x >>= 16;
9183 break;
9184 #ifdef BFD64
9185 case 8:
9186 bfd_put_64 (input_bfd, x, location);
9187 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9188 x >>= 32;
9189 x >>= 32;
9190 break;
9191 #endif
9192 default:
9193 abort ();
9194 break;
9195 }
9196 }
9197 }
9198
9199 static bfd_vma
9200 get_value (bfd_vma size,
9201 unsigned long chunksz,
9202 bfd *input_bfd,
9203 bfd_byte *location)
9204 {
9205 int shift;
9206 bfd_vma x = 0;
9207
9208 /* Sanity checks. */
9209 BFD_ASSERT (chunksz <= sizeof (x)
9210 && size >= chunksz
9211 && chunksz != 0
9212 && (size % chunksz) == 0
9213 && input_bfd != NULL
9214 && location != NULL);
9215
9216 if (chunksz == sizeof (x))
9217 {
9218 BFD_ASSERT (size == chunksz);
9219
9220 /* Make sure that we do not perform an undefined shift operation.
9221 We know that size == chunksz so there will only be one iteration
9222 of the loop below. */
9223 shift = 0;
9224 }
9225 else
9226 shift = 8 * chunksz;
9227
9228 for (; size; size -= chunksz, location += chunksz)
9229 {
9230 switch (chunksz)
9231 {
9232 case 1:
9233 x = (x << shift) | bfd_get_8 (input_bfd, location);
9234 break;
9235 case 2:
9236 x = (x << shift) | bfd_get_16 (input_bfd, location);
9237 break;
9238 case 4:
9239 x = (x << shift) | bfd_get_32 (input_bfd, location);
9240 break;
9241 #ifdef BFD64
9242 case 8:
9243 x = (x << shift) | bfd_get_64 (input_bfd, location);
9244 break;
9245 #endif
9246 default:
9247 abort ();
9248 }
9249 }
9250 return x;
9251 }
9252
9253 static void
9254 decode_complex_addend (unsigned long *start, /* in bits */
9255 unsigned long *oplen, /* in bits */
9256 unsigned long *len, /* in bits */
9257 unsigned long *wordsz, /* in bytes */
9258 unsigned long *chunksz, /* in bytes */
9259 unsigned long *lsb0_p,
9260 unsigned long *signed_p,
9261 unsigned long *trunc_p,
9262 unsigned long encoded)
9263 {
9264 * start = encoded & 0x3F;
9265 * len = (encoded >> 6) & 0x3F;
9266 * oplen = (encoded >> 12) & 0x3F;
9267 * wordsz = (encoded >> 18) & 0xF;
9268 * chunksz = (encoded >> 22) & 0xF;
9269 * lsb0_p = (encoded >> 27) & 1;
9270 * signed_p = (encoded >> 28) & 1;
9271 * trunc_p = (encoded >> 29) & 1;
9272 }
9273
9274 bfd_reloc_status_type
9275 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9276 asection *input_section,
9277 bfd_byte *contents,
9278 Elf_Internal_Rela *rel,
9279 bfd_vma relocation)
9280 {
9281 bfd_vma shift, x, mask;
9282 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9283 bfd_reloc_status_type r;
9284 bfd_size_type octets;
9285
9286 /* Perform this reloc, since it is complex.
9287 (this is not to say that it necessarily refers to a complex
9288 symbol; merely that it is a self-describing CGEN based reloc.
9289 i.e. the addend has the complete reloc information (bit start, end,
9290 word size, etc) encoded within it.). */
9291
9292 decode_complex_addend (&start, &oplen, &len, &wordsz,
9293 &chunksz, &lsb0_p, &signed_p,
9294 &trunc_p, rel->r_addend);
9295
9296 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9297
9298 if (lsb0_p)
9299 shift = (start + 1) - len;
9300 else
9301 shift = (8 * wordsz) - (start + len);
9302
9303 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9304 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9305
9306 #ifdef DEBUG
9307 printf ("Doing complex reloc: "
9308 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9309 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9310 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9311 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9312 oplen, (unsigned long) x, (unsigned long) mask,
9313 (unsigned long) relocation);
9314 #endif
9315
9316 r = bfd_reloc_ok;
9317 if (! trunc_p)
9318 /* Now do an overflow check. */
9319 r = bfd_check_overflow ((signed_p
9320 ? complain_overflow_signed
9321 : complain_overflow_unsigned),
9322 len, 0, (8 * wordsz),
9323 relocation);
9324
9325 /* Do the deed. */
9326 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9327
9328 #ifdef DEBUG
9329 printf (" relocation: %8.8lx\n"
9330 " shifted mask: %8.8lx\n"
9331 " shifted/masked reloc: %8.8lx\n"
9332 " result: %8.8lx\n",
9333 (unsigned long) relocation, (unsigned long) (mask << shift),
9334 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9335 #endif
9336 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9337 return r;
9338 }
9339
9340 /* Functions to read r_offset from external (target order) reloc
9341 entry. Faster than bfd_getl32 et al, because we let the compiler
9342 know the value is aligned. */
9343
9344 static bfd_vma
9345 ext32l_r_offset (const void *p)
9346 {
9347 union aligned32
9348 {
9349 uint32_t v;
9350 unsigned char c[4];
9351 };
9352 const union aligned32 *a
9353 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9354
9355 uint32_t aval = ( (uint32_t) a->c[0]
9356 | (uint32_t) a->c[1] << 8
9357 | (uint32_t) a->c[2] << 16
9358 | (uint32_t) a->c[3] << 24);
9359 return aval;
9360 }
9361
9362 static bfd_vma
9363 ext32b_r_offset (const void *p)
9364 {
9365 union aligned32
9366 {
9367 uint32_t v;
9368 unsigned char c[4];
9369 };
9370 const union aligned32 *a
9371 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9372
9373 uint32_t aval = ( (uint32_t) a->c[0] << 24
9374 | (uint32_t) a->c[1] << 16
9375 | (uint32_t) a->c[2] << 8
9376 | (uint32_t) a->c[3]);
9377 return aval;
9378 }
9379
9380 static bfd_vma
9381 ext64l_r_offset (const void *p)
9382 {
9383 union aligned64
9384 {
9385 uint64_t v;
9386 unsigned char c[8];
9387 };
9388 const union aligned64 *a
9389 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9390
9391 uint64_t aval = ( (uint64_t) a->c[0]
9392 | (uint64_t) a->c[1] << 8
9393 | (uint64_t) a->c[2] << 16
9394 | (uint64_t) a->c[3] << 24
9395 | (uint64_t) a->c[4] << 32
9396 | (uint64_t) a->c[5] << 40
9397 | (uint64_t) a->c[6] << 48
9398 | (uint64_t) a->c[7] << 56);
9399 return aval;
9400 }
9401
9402 static bfd_vma
9403 ext64b_r_offset (const void *p)
9404 {
9405 union aligned64
9406 {
9407 uint64_t v;
9408 unsigned char c[8];
9409 };
9410 const union aligned64 *a
9411 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9412
9413 uint64_t aval = ( (uint64_t) a->c[0] << 56
9414 | (uint64_t) a->c[1] << 48
9415 | (uint64_t) a->c[2] << 40
9416 | (uint64_t) a->c[3] << 32
9417 | (uint64_t) a->c[4] << 24
9418 | (uint64_t) a->c[5] << 16
9419 | (uint64_t) a->c[6] << 8
9420 | (uint64_t) a->c[7]);
9421 return aval;
9422 }
9423
9424 /* When performing a relocatable link, the input relocations are
9425 preserved. But, if they reference global symbols, the indices
9426 referenced must be updated. Update all the relocations found in
9427 RELDATA. */
9428
9429 static bool
9430 elf_link_adjust_relocs (bfd *abfd,
9431 asection *sec,
9432 struct bfd_elf_section_reloc_data *reldata,
9433 bool sort,
9434 struct bfd_link_info *info)
9435 {
9436 unsigned int i;
9437 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9438 bfd_byte *erela;
9439 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9440 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9441 bfd_vma r_type_mask;
9442 int r_sym_shift;
9443 unsigned int count = reldata->count;
9444 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9445
9446 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9447 {
9448 swap_in = bed->s->swap_reloc_in;
9449 swap_out = bed->s->swap_reloc_out;
9450 }
9451 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9452 {
9453 swap_in = bed->s->swap_reloca_in;
9454 swap_out = bed->s->swap_reloca_out;
9455 }
9456 else
9457 abort ();
9458
9459 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9460 abort ();
9461
9462 if (bed->s->arch_size == 32)
9463 {
9464 r_type_mask = 0xff;
9465 r_sym_shift = 8;
9466 }
9467 else
9468 {
9469 r_type_mask = 0xffffffff;
9470 r_sym_shift = 32;
9471 }
9472
9473 erela = reldata->hdr->contents;
9474 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9475 {
9476 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9477 unsigned int j;
9478
9479 if (*rel_hash == NULL)
9480 continue;
9481
9482 if ((*rel_hash)->indx == -2
9483 && info->gc_sections
9484 && ! info->gc_keep_exported)
9485 {
9486 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9487 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9488 abfd, sec,
9489 (*rel_hash)->root.root.string);
9490 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9491 abfd, sec);
9492 bfd_set_error (bfd_error_invalid_operation);
9493 return false;
9494 }
9495 BFD_ASSERT ((*rel_hash)->indx >= 0);
9496
9497 (*swap_in) (abfd, erela, irela);
9498 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9499 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9500 | (irela[j].r_info & r_type_mask));
9501 (*swap_out) (abfd, irela, erela);
9502 }
9503
9504 if (bed->elf_backend_update_relocs)
9505 (*bed->elf_backend_update_relocs) (sec, reldata);
9506
9507 if (sort && count != 0)
9508 {
9509 bfd_vma (*ext_r_off) (const void *);
9510 bfd_vma r_off;
9511 size_t elt_size;
9512 bfd_byte *base, *end, *p, *loc;
9513 bfd_byte *buf = NULL;
9514
9515 if (bed->s->arch_size == 32)
9516 {
9517 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9518 ext_r_off = ext32l_r_offset;
9519 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9520 ext_r_off = ext32b_r_offset;
9521 else
9522 abort ();
9523 }
9524 else
9525 {
9526 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9527 ext_r_off = ext64l_r_offset;
9528 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9529 ext_r_off = ext64b_r_offset;
9530 else
9531 abort ();
9532 }
9533
9534 /* Must use a stable sort here. A modified insertion sort,
9535 since the relocs are mostly sorted already. */
9536 elt_size = reldata->hdr->sh_entsize;
9537 base = reldata->hdr->contents;
9538 end = base + count * elt_size;
9539 if (elt_size > sizeof (Elf64_External_Rela))
9540 abort ();
9541
9542 /* Ensure the first element is lowest. This acts as a sentinel,
9543 speeding the main loop below. */
9544 r_off = (*ext_r_off) (base);
9545 for (p = loc = base; (p += elt_size) < end; )
9546 {
9547 bfd_vma r_off2 = (*ext_r_off) (p);
9548 if (r_off > r_off2)
9549 {
9550 r_off = r_off2;
9551 loc = p;
9552 }
9553 }
9554 if (loc != base)
9555 {
9556 /* Don't just swap *base and *loc as that changes the order
9557 of the original base[0] and base[1] if they happen to
9558 have the same r_offset. */
9559 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9560 memcpy (onebuf, loc, elt_size);
9561 memmove (base + elt_size, base, loc - base);
9562 memcpy (base, onebuf, elt_size);
9563 }
9564
9565 for (p = base + elt_size; (p += elt_size) < end; )
9566 {
9567 /* base to p is sorted, *p is next to insert. */
9568 r_off = (*ext_r_off) (p);
9569 /* Search the sorted region for location to insert. */
9570 loc = p - elt_size;
9571 while (r_off < (*ext_r_off) (loc))
9572 loc -= elt_size;
9573 loc += elt_size;
9574 if (loc != p)
9575 {
9576 /* Chances are there is a run of relocs to insert here,
9577 from one of more input files. Files are not always
9578 linked in order due to the way elf_link_input_bfd is
9579 called. See pr17666. */
9580 size_t sortlen = p - loc;
9581 bfd_vma r_off2 = (*ext_r_off) (loc);
9582 size_t runlen = elt_size;
9583 bfd_vma r_off_runend = r_off;
9584 bfd_vma r_off_runend_next;
9585 size_t buf_size = 96 * 1024;
9586 while (p + runlen < end
9587 && (sortlen <= buf_size
9588 || runlen + elt_size <= buf_size)
9589 /* run must not break the ordering of base..loc+1 */
9590 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9591 /* run must be already sorted */
9592 && r_off_runend_next >= r_off_runend)
9593 {
9594 runlen += elt_size;
9595 r_off_runend = r_off_runend_next;
9596 }
9597 if (buf == NULL)
9598 {
9599 buf = bfd_malloc (buf_size);
9600 if (buf == NULL)
9601 return false;
9602 }
9603 if (runlen < sortlen)
9604 {
9605 memcpy (buf, p, runlen);
9606 memmove (loc + runlen, loc, sortlen);
9607 memcpy (loc, buf, runlen);
9608 }
9609 else
9610 {
9611 memcpy (buf, loc, sortlen);
9612 memmove (loc, p, runlen);
9613 memcpy (loc + runlen, buf, sortlen);
9614 }
9615 p += runlen - elt_size;
9616 }
9617 }
9618 /* Hashes are no longer valid. */
9619 free (reldata->hashes);
9620 reldata->hashes = NULL;
9621 free (buf);
9622 }
9623 return true;
9624 }
9625
9626 struct elf_link_sort_rela
9627 {
9628 union {
9629 bfd_vma offset;
9630 bfd_vma sym_mask;
9631 } u;
9632 enum elf_reloc_type_class type;
9633 /* We use this as an array of size int_rels_per_ext_rel. */
9634 Elf_Internal_Rela rela[1];
9635 };
9636
9637 /* qsort stability here and for cmp2 is only an issue if multiple
9638 dynamic relocations are emitted at the same address. But targets
9639 that apply a series of dynamic relocations each operating on the
9640 result of the prior relocation can't use -z combreloc as
9641 implemented anyway. Such schemes tend to be broken by sorting on
9642 symbol index. That leaves dynamic NONE relocs as the only other
9643 case where ld might emit multiple relocs at the same address, and
9644 those are only emitted due to target bugs. */
9645
9646 static int
9647 elf_link_sort_cmp1 (const void *A, const void *B)
9648 {
9649 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9650 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9651 int relativea, relativeb;
9652
9653 relativea = a->type == reloc_class_relative;
9654 relativeb = b->type == reloc_class_relative;
9655
9656 if (relativea < relativeb)
9657 return 1;
9658 if (relativea > relativeb)
9659 return -1;
9660 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9661 return -1;
9662 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9663 return 1;
9664 if (a->rela->r_offset < b->rela->r_offset)
9665 return -1;
9666 if (a->rela->r_offset > b->rela->r_offset)
9667 return 1;
9668 return 0;
9669 }
9670
9671 static int
9672 elf_link_sort_cmp2 (const void *A, const void *B)
9673 {
9674 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9675 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9676
9677 if (a->type < b->type)
9678 return -1;
9679 if (a->type > b->type)
9680 return 1;
9681 if (a->u.offset < b->u.offset)
9682 return -1;
9683 if (a->u.offset > b->u.offset)
9684 return 1;
9685 if (a->rela->r_offset < b->rela->r_offset)
9686 return -1;
9687 if (a->rela->r_offset > b->rela->r_offset)
9688 return 1;
9689 return 0;
9690 }
9691
9692 static size_t
9693 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9694 {
9695 asection *dynamic_relocs;
9696 asection *rela_dyn;
9697 asection *rel_dyn;
9698 bfd_size_type count, size;
9699 size_t i, ret, sort_elt, ext_size;
9700 bfd_byte *sort, *s_non_relative, *p;
9701 struct elf_link_sort_rela *sq;
9702 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9703 int i2e = bed->s->int_rels_per_ext_rel;
9704 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9705 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9706 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9707 struct bfd_link_order *lo;
9708 bfd_vma r_sym_mask;
9709 bool use_rela;
9710
9711 /* Find a dynamic reloc section. */
9712 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9713 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9714 if (rela_dyn != NULL && rela_dyn->size > 0
9715 && rel_dyn != NULL && rel_dyn->size > 0)
9716 {
9717 bool use_rela_initialised = false;
9718
9719 /* This is just here to stop gcc from complaining.
9720 Its initialization checking code is not perfect. */
9721 use_rela = true;
9722
9723 /* Both sections are present. Examine the sizes
9724 of the indirect sections to help us choose. */
9725 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9726 if (lo->type == bfd_indirect_link_order)
9727 {
9728 asection *o = lo->u.indirect.section;
9729
9730 if ((o->size % bed->s->sizeof_rela) == 0)
9731 {
9732 if ((o->size % bed->s->sizeof_rel) == 0)
9733 /* Section size is divisible by both rel and rela sizes.
9734 It is of no help to us. */
9735 ;
9736 else
9737 {
9738 /* Section size is only divisible by rela. */
9739 if (use_rela_initialised && !use_rela)
9740 {
9741 _bfd_error_handler (_("%pB: unable to sort relocs - "
9742 "they are in more than one size"),
9743 abfd);
9744 bfd_set_error (bfd_error_invalid_operation);
9745 return 0;
9746 }
9747 else
9748 {
9749 use_rela = true;
9750 use_rela_initialised = true;
9751 }
9752 }
9753 }
9754 else if ((o->size % bed->s->sizeof_rel) == 0)
9755 {
9756 /* Section size is only divisible by rel. */
9757 if (use_rela_initialised && use_rela)
9758 {
9759 _bfd_error_handler (_("%pB: unable to sort relocs - "
9760 "they are in more than one size"),
9761 abfd);
9762 bfd_set_error (bfd_error_invalid_operation);
9763 return 0;
9764 }
9765 else
9766 {
9767 use_rela = false;
9768 use_rela_initialised = true;
9769 }
9770 }
9771 else
9772 {
9773 /* The section size is not divisible by either -
9774 something is wrong. */
9775 _bfd_error_handler (_("%pB: unable to sort relocs - "
9776 "they are of an unknown size"), abfd);
9777 bfd_set_error (bfd_error_invalid_operation);
9778 return 0;
9779 }
9780 }
9781
9782 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9783 if (lo->type == bfd_indirect_link_order)
9784 {
9785 asection *o = lo->u.indirect.section;
9786
9787 if ((o->size % bed->s->sizeof_rela) == 0)
9788 {
9789 if ((o->size % bed->s->sizeof_rel) == 0)
9790 /* Section size is divisible by both rel and rela sizes.
9791 It is of no help to us. */
9792 ;
9793 else
9794 {
9795 /* Section size is only divisible by rela. */
9796 if (use_rela_initialised && !use_rela)
9797 {
9798 _bfd_error_handler (_("%pB: unable to sort relocs - "
9799 "they are in more than one size"),
9800 abfd);
9801 bfd_set_error (bfd_error_invalid_operation);
9802 return 0;
9803 }
9804 else
9805 {
9806 use_rela = true;
9807 use_rela_initialised = true;
9808 }
9809 }
9810 }
9811 else if ((o->size % bed->s->sizeof_rel) == 0)
9812 {
9813 /* Section size is only divisible by rel. */
9814 if (use_rela_initialised && use_rela)
9815 {
9816 _bfd_error_handler (_("%pB: unable to sort relocs - "
9817 "they are in more than one size"),
9818 abfd);
9819 bfd_set_error (bfd_error_invalid_operation);
9820 return 0;
9821 }
9822 else
9823 {
9824 use_rela = false;
9825 use_rela_initialised = true;
9826 }
9827 }
9828 else
9829 {
9830 /* The section size is not divisible by either -
9831 something is wrong. */
9832 _bfd_error_handler (_("%pB: unable to sort relocs - "
9833 "they are of an unknown size"), abfd);
9834 bfd_set_error (bfd_error_invalid_operation);
9835 return 0;
9836 }
9837 }
9838
9839 if (! use_rela_initialised)
9840 /* Make a guess. */
9841 use_rela = true;
9842 }
9843 else if (rela_dyn != NULL && rela_dyn->size > 0)
9844 use_rela = true;
9845 else if (rel_dyn != NULL && rel_dyn->size > 0)
9846 use_rela = false;
9847 else
9848 return 0;
9849
9850 if (use_rela)
9851 {
9852 dynamic_relocs = rela_dyn;
9853 ext_size = bed->s->sizeof_rela;
9854 swap_in = bed->s->swap_reloca_in;
9855 swap_out = bed->s->swap_reloca_out;
9856 }
9857 else
9858 {
9859 dynamic_relocs = rel_dyn;
9860 ext_size = bed->s->sizeof_rel;
9861 swap_in = bed->s->swap_reloc_in;
9862 swap_out = bed->s->swap_reloc_out;
9863 }
9864
9865 size = 0;
9866 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9867 if (lo->type == bfd_indirect_link_order)
9868 size += lo->u.indirect.section->size;
9869
9870 if (size != dynamic_relocs->size)
9871 return 0;
9872
9873 sort_elt = (sizeof (struct elf_link_sort_rela)
9874 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9875
9876 count = dynamic_relocs->size / ext_size;
9877 if (count == 0)
9878 return 0;
9879 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9880
9881 if (sort == NULL)
9882 {
9883 (*info->callbacks->warning)
9884 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9885 return 0;
9886 }
9887
9888 if (bed->s->arch_size == 32)
9889 r_sym_mask = ~(bfd_vma) 0xff;
9890 else
9891 r_sym_mask = ~(bfd_vma) 0xffffffff;
9892
9893 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9894 if (lo->type == bfd_indirect_link_order)
9895 {
9896 bfd_byte *erel, *erelend;
9897 asection *o = lo->u.indirect.section;
9898
9899 if (o->contents == NULL && o->size != 0)
9900 {
9901 /* This is a reloc section that is being handled as a normal
9902 section. See bfd_section_from_shdr. We can't combine
9903 relocs in this case. */
9904 free (sort);
9905 return 0;
9906 }
9907 erel = o->contents;
9908 erelend = o->contents + o->size;
9909 p = sort + o->output_offset * opb / ext_size * sort_elt;
9910
9911 while (erel < erelend)
9912 {
9913 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9914
9915 (*swap_in) (abfd, erel, s->rela);
9916 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9917 s->u.sym_mask = r_sym_mask;
9918 p += sort_elt;
9919 erel += ext_size;
9920 }
9921 }
9922
9923 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9924
9925 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9926 {
9927 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9928 if (s->type != reloc_class_relative)
9929 break;
9930 }
9931 ret = i;
9932 s_non_relative = p;
9933
9934 sq = (struct elf_link_sort_rela *) s_non_relative;
9935 for (; i < count; i++, p += sort_elt)
9936 {
9937 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9938 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9939 sq = sp;
9940 sp->u.offset = sq->rela->r_offset;
9941 }
9942
9943 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9944
9945 struct elf_link_hash_table *htab = elf_hash_table (info);
9946 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9947 {
9948 /* We have plt relocs in .rela.dyn. */
9949 sq = (struct elf_link_sort_rela *) sort;
9950 for (i = 0; i < count; i++)
9951 if (sq[count - i - 1].type != reloc_class_plt)
9952 break;
9953 if (i != 0 && htab->srelplt->size == i * ext_size)
9954 {
9955 struct bfd_link_order **plo;
9956 /* Put srelplt link_order last. This is so the output_offset
9957 set in the next loop is correct for DT_JMPREL. */
9958 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9959 if ((*plo)->type == bfd_indirect_link_order
9960 && (*plo)->u.indirect.section == htab->srelplt)
9961 {
9962 lo = *plo;
9963 *plo = lo->next;
9964 }
9965 else
9966 plo = &(*plo)->next;
9967 *plo = lo;
9968 lo->next = NULL;
9969 dynamic_relocs->map_tail.link_order = lo;
9970 }
9971 }
9972
9973 p = sort;
9974 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9975 if (lo->type == bfd_indirect_link_order)
9976 {
9977 bfd_byte *erel, *erelend;
9978 asection *o = lo->u.indirect.section;
9979
9980 erel = o->contents;
9981 erelend = o->contents + o->size;
9982 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9983 while (erel < erelend)
9984 {
9985 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9986 (*swap_out) (abfd, s->rela, erel);
9987 p += sort_elt;
9988 erel += ext_size;
9989 }
9990 }
9991
9992 free (sort);
9993 *psec = dynamic_relocs;
9994 return ret;
9995 }
9996
9997 /* Add a symbol to the output symbol string table. */
9998
9999 static int
10000 elf_link_output_symstrtab (void *finf,
10001 const char *name,
10002 Elf_Internal_Sym *elfsym,
10003 asection *input_sec,
10004 struct elf_link_hash_entry *h)
10005 {
10006 struct elf_final_link_info *flinfo = finf;
10007 int (*output_symbol_hook)
10008 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10009 struct elf_link_hash_entry *);
10010 struct elf_link_hash_table *hash_table;
10011 const struct elf_backend_data *bed;
10012 bfd_size_type strtabsize;
10013
10014 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10015
10016 bed = get_elf_backend_data (flinfo->output_bfd);
10017 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10018 if (output_symbol_hook != NULL)
10019 {
10020 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10021 if (ret != 1)
10022 return ret;
10023 }
10024
10025 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10026 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10027 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10028 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10029
10030 if (name == NULL || *name == '\0')
10031 elfsym->st_name = (unsigned long) -1;
10032 else
10033 {
10034 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10035 to get the final offset for st_name. */
10036 char *versioned_name = (char *) name;
10037 if (h != NULL)
10038 {
10039 if (h->versioned == versioned && h->def_dynamic)
10040 {
10041 /* Keep only one '@' for versioned symbols defined in
10042 shared objects. */
10043 char *version = strrchr (name, ELF_VER_CHR);
10044 char *base_end = strchr (name, ELF_VER_CHR);
10045 if (version != base_end)
10046 {
10047 size_t base_len;
10048 size_t len = strlen (name);
10049 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10050 if (versioned_name == NULL)
10051 return 0;
10052 base_len = base_end - name;
10053 memcpy (versioned_name, name, base_len);
10054 memcpy (versioned_name + base_len, version,
10055 len - base_len);
10056 }
10057 }
10058 }
10059 else if (flinfo->info->unique_symbol
10060 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10061 {
10062 struct local_hash_entry *lh;
10063 size_t count_len;
10064 size_t base_len;
10065 char buf[30];
10066 switch (ELF_ST_TYPE (elfsym->st_info))
10067 {
10068 case STT_FILE:
10069 case STT_SECTION:
10070 break;
10071 default:
10072 lh = (struct local_hash_entry *) bfd_hash_lookup
10073 (&flinfo->local_hash_table, name, true, false);
10074 if (lh == NULL)
10075 return 0;
10076 /* Always append ".COUNT" to local symbols to avoid
10077 potential conflicts with local symbol "XXX.COUNT". */
10078 sprintf (buf, "%lx", lh->count);
10079 base_len = lh->size;
10080 if (!base_len)
10081 {
10082 base_len = strlen (name);
10083 lh->size = base_len;
10084 }
10085 count_len = strlen (buf);
10086 versioned_name = bfd_alloc (flinfo->output_bfd,
10087 base_len + count_len + 2);
10088 if (versioned_name == NULL)
10089 return 0;
10090 memcpy (versioned_name, name, base_len);
10091 versioned_name[base_len] = '.';
10092 memcpy (versioned_name + base_len + 1, buf,
10093 count_len + 1);
10094 lh->count++;
10095 break;
10096 }
10097 }
10098 elfsym->st_name
10099 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10100 versioned_name, false);
10101 if (elfsym->st_name == (unsigned long) -1)
10102 return 0;
10103 }
10104
10105 hash_table = elf_hash_table (flinfo->info);
10106 strtabsize = hash_table->strtabsize;
10107 if (strtabsize <= flinfo->output_bfd->symcount)
10108 {
10109 strtabsize += strtabsize;
10110 hash_table->strtabsize = strtabsize;
10111 strtabsize *= sizeof (*hash_table->strtab);
10112 hash_table->strtab
10113 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10114 strtabsize);
10115 if (hash_table->strtab == NULL)
10116 return 0;
10117 }
10118 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10119 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10120 = flinfo->output_bfd->symcount;
10121 flinfo->output_bfd->symcount += 1;
10122
10123 return 1;
10124 }
10125
10126 /* Swap symbols out to the symbol table and flush the output symbols to
10127 the file. */
10128
10129 static bool
10130 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10131 {
10132 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10133 size_t amt;
10134 size_t i;
10135 const struct elf_backend_data *bed;
10136 bfd_byte *symbuf;
10137 Elf_Internal_Shdr *hdr;
10138 file_ptr pos;
10139 bool ret;
10140
10141 if (flinfo->output_bfd->symcount == 0)
10142 return true;
10143
10144 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10145
10146 bed = get_elf_backend_data (flinfo->output_bfd);
10147
10148 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10149 symbuf = (bfd_byte *) bfd_malloc (amt);
10150 if (symbuf == NULL)
10151 return false;
10152
10153 if (flinfo->symshndxbuf)
10154 {
10155 amt = sizeof (Elf_External_Sym_Shndx);
10156 amt *= bfd_get_symcount (flinfo->output_bfd);
10157 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10158 if (flinfo->symshndxbuf == NULL)
10159 {
10160 free (symbuf);
10161 return false;
10162 }
10163 }
10164
10165 /* Now swap out the symbols. */
10166 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10167 {
10168 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10169 if (elfsym->sym.st_name == (unsigned long) -1)
10170 elfsym->sym.st_name = 0;
10171 else
10172 elfsym->sym.st_name
10173 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10174 elfsym->sym.st_name);
10175
10176 /* Inform the linker of the addition of this symbol. */
10177
10178 if (flinfo->info->callbacks->ctf_new_symbol)
10179 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10180 &elfsym->sym);
10181
10182 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10183 ((bfd_byte *) symbuf
10184 + (elfsym->dest_index
10185 * bed->s->sizeof_sym)),
10186 NPTR_ADD (flinfo->symshndxbuf,
10187 elfsym->dest_index));
10188 }
10189
10190 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10191 pos = hdr->sh_offset + hdr->sh_size;
10192 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10193 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10194 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10195 {
10196 hdr->sh_size += amt;
10197 ret = true;
10198 }
10199 else
10200 ret = false;
10201
10202 free (symbuf);
10203
10204 free (hash_table->strtab);
10205 hash_table->strtab = NULL;
10206
10207 return ret;
10208 }
10209
10210 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10211
10212 static bool
10213 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10214 {
10215 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10216 && sym->st_shndx < SHN_LORESERVE)
10217 {
10218 /* The gABI doesn't support dynamic symbols in output sections
10219 beyond 64k. */
10220 _bfd_error_handler
10221 /* xgettext:c-format */
10222 (_("%pB: too many sections: %d (>= %d)"),
10223 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10224 bfd_set_error (bfd_error_nonrepresentable_section);
10225 return false;
10226 }
10227 return true;
10228 }
10229
10230 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10231 allowing an unsatisfied unversioned symbol in the DSO to match a
10232 versioned symbol that would normally require an explicit version.
10233 We also handle the case that a DSO references a hidden symbol
10234 which may be satisfied by a versioned symbol in another DSO. */
10235
10236 static bool
10237 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10238 const struct elf_backend_data *bed,
10239 struct elf_link_hash_entry *h)
10240 {
10241 bfd *abfd;
10242 struct elf_link_loaded_list *loaded;
10243
10244 if (!is_elf_hash_table (info->hash))
10245 return false;
10246
10247 /* Check indirect symbol. */
10248 while (h->root.type == bfd_link_hash_indirect)
10249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10250
10251 switch (h->root.type)
10252 {
10253 default:
10254 abfd = NULL;
10255 break;
10256
10257 case bfd_link_hash_undefined:
10258 case bfd_link_hash_undefweak:
10259 abfd = h->root.u.undef.abfd;
10260 if (abfd == NULL
10261 || (abfd->flags & DYNAMIC) == 0
10262 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10263 return false;
10264 break;
10265
10266 case bfd_link_hash_defined:
10267 case bfd_link_hash_defweak:
10268 abfd = h->root.u.def.section->owner;
10269 break;
10270
10271 case bfd_link_hash_common:
10272 abfd = h->root.u.c.p->section->owner;
10273 break;
10274 }
10275 BFD_ASSERT (abfd != NULL);
10276
10277 for (loaded = elf_hash_table (info)->dyn_loaded;
10278 loaded != NULL;
10279 loaded = loaded->next)
10280 {
10281 bfd *input;
10282 Elf_Internal_Shdr *hdr;
10283 size_t symcount;
10284 size_t extsymcount;
10285 size_t extsymoff;
10286 Elf_Internal_Shdr *versymhdr;
10287 Elf_Internal_Sym *isym;
10288 Elf_Internal_Sym *isymend;
10289 Elf_Internal_Sym *isymbuf;
10290 Elf_External_Versym *ever;
10291 Elf_External_Versym *extversym;
10292
10293 input = loaded->abfd;
10294
10295 /* We check each DSO for a possible hidden versioned definition. */
10296 if (input == abfd
10297 || elf_dynversym (input) == 0)
10298 continue;
10299
10300 hdr = &elf_tdata (input)->dynsymtab_hdr;
10301
10302 symcount = hdr->sh_size / bed->s->sizeof_sym;
10303 if (elf_bad_symtab (input))
10304 {
10305 extsymcount = symcount;
10306 extsymoff = 0;
10307 }
10308 else
10309 {
10310 extsymcount = symcount - hdr->sh_info;
10311 extsymoff = hdr->sh_info;
10312 }
10313
10314 if (extsymcount == 0)
10315 continue;
10316
10317 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10318 NULL, NULL, NULL);
10319 if (isymbuf == NULL)
10320 return false;
10321
10322 /* Read in any version definitions. */
10323 versymhdr = &elf_tdata (input)->dynversym_hdr;
10324 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10325 || (extversym = (Elf_External_Versym *)
10326 _bfd_malloc_and_read (input, versymhdr->sh_size,
10327 versymhdr->sh_size)) == NULL)
10328 {
10329 free (isymbuf);
10330 return false;
10331 }
10332
10333 ever = extversym + extsymoff;
10334 isymend = isymbuf + extsymcount;
10335 for (isym = isymbuf; isym < isymend; isym++, ever++)
10336 {
10337 const char *name;
10338 Elf_Internal_Versym iver;
10339 unsigned short version_index;
10340
10341 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10342 || isym->st_shndx == SHN_UNDEF)
10343 continue;
10344
10345 name = bfd_elf_string_from_elf_section (input,
10346 hdr->sh_link,
10347 isym->st_name);
10348 if (strcmp (name, h->root.root.string) != 0)
10349 continue;
10350
10351 _bfd_elf_swap_versym_in (input, ever, &iver);
10352
10353 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10354 && !(h->def_regular
10355 && h->forced_local))
10356 {
10357 /* If we have a non-hidden versioned sym, then it should
10358 have provided a definition for the undefined sym unless
10359 it is defined in a non-shared object and forced local.
10360 */
10361 abort ();
10362 }
10363
10364 version_index = iver.vs_vers & VERSYM_VERSION;
10365 if (version_index == 1 || version_index == 2)
10366 {
10367 /* This is the base or first version. We can use it. */
10368 free (extversym);
10369 free (isymbuf);
10370 return true;
10371 }
10372 }
10373
10374 free (extversym);
10375 free (isymbuf);
10376 }
10377
10378 return false;
10379 }
10380
10381 /* Convert ELF common symbol TYPE. */
10382
10383 static int
10384 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10385 {
10386 /* Commom symbol can only appear in relocatable link. */
10387 if (!bfd_link_relocatable (info))
10388 abort ();
10389 switch (info->elf_stt_common)
10390 {
10391 case unchanged:
10392 break;
10393 case elf_stt_common:
10394 type = STT_COMMON;
10395 break;
10396 case no_elf_stt_common:
10397 type = STT_OBJECT;
10398 break;
10399 }
10400 return type;
10401 }
10402
10403 /* Add an external symbol to the symbol table. This is called from
10404 the hash table traversal routine. When generating a shared object,
10405 we go through the symbol table twice. The first time we output
10406 anything that might have been forced to local scope in a version
10407 script. The second time we output the symbols that are still
10408 global symbols. */
10409
10410 static bool
10411 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10412 {
10413 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10414 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10415 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10416 bool strip;
10417 Elf_Internal_Sym sym;
10418 asection *input_sec;
10419 const struct elf_backend_data *bed;
10420 long indx;
10421 int ret;
10422 unsigned int type;
10423
10424 if (h->root.type == bfd_link_hash_warning)
10425 {
10426 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10427 if (h->root.type == bfd_link_hash_new)
10428 return true;
10429 }
10430
10431 /* Decide whether to output this symbol in this pass. */
10432 if (eoinfo->localsyms)
10433 {
10434 if (!h->forced_local)
10435 return true;
10436 }
10437 else
10438 {
10439 if (h->forced_local)
10440 return true;
10441 }
10442
10443 bed = get_elf_backend_data (flinfo->output_bfd);
10444
10445 if (h->root.type == bfd_link_hash_undefined)
10446 {
10447 /* If we have an undefined symbol reference here then it must have
10448 come from a shared library that is being linked in. (Undefined
10449 references in regular files have already been handled unless
10450 they are in unreferenced sections which are removed by garbage
10451 collection). */
10452 bool ignore_undef = false;
10453
10454 /* Some symbols may be special in that the fact that they're
10455 undefined can be safely ignored - let backend determine that. */
10456 if (bed->elf_backend_ignore_undef_symbol)
10457 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10458
10459 /* If we are reporting errors for this situation then do so now. */
10460 if (!ignore_undef
10461 && h->ref_dynamic_nonweak
10462 && (!h->ref_regular || flinfo->info->gc_sections)
10463 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10464 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10465 {
10466 flinfo->info->callbacks->undefined_symbol
10467 (flinfo->info, h->root.root.string,
10468 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10469 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10470 && !flinfo->info->warn_unresolved_syms);
10471 }
10472
10473 /* Strip a global symbol defined in a discarded section. */
10474 if (h->indx == -3)
10475 return true;
10476 }
10477
10478 /* We should also warn if a forced local symbol is referenced from
10479 shared libraries. */
10480 if (bfd_link_executable (flinfo->info)
10481 && h->forced_local
10482 && h->ref_dynamic
10483 && h->def_regular
10484 && !h->dynamic_def
10485 && h->ref_dynamic_nonweak
10486 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10487 {
10488 bfd *def_bfd;
10489 const char *msg;
10490 struct elf_link_hash_entry *hi = h;
10491
10492 /* Check indirect symbol. */
10493 while (hi->root.type == bfd_link_hash_indirect)
10494 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10495
10496 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10497 /* xgettext:c-format */
10498 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10499 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10500 /* xgettext:c-format */
10501 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10502 else
10503 /* xgettext:c-format */
10504 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10505 def_bfd = flinfo->output_bfd;
10506 if (hi->root.u.def.section != bfd_abs_section_ptr)
10507 def_bfd = hi->root.u.def.section->owner;
10508 _bfd_error_handler (msg, flinfo->output_bfd,
10509 h->root.root.string, def_bfd);
10510 bfd_set_error (bfd_error_bad_value);
10511 eoinfo->failed = true;
10512 return false;
10513 }
10514
10515 /* We don't want to output symbols that have never been mentioned by
10516 a regular file, or that we have been told to strip. However, if
10517 h->indx is set to -2, the symbol is used by a reloc and we must
10518 output it. */
10519 strip = false;
10520 if (h->indx == -2)
10521 ;
10522 else if ((h->def_dynamic
10523 || h->ref_dynamic
10524 || h->root.type == bfd_link_hash_new)
10525 && !h->def_regular
10526 && !h->ref_regular)
10527 strip = true;
10528 else if (flinfo->info->strip == strip_all)
10529 strip = true;
10530 else if (flinfo->info->strip == strip_some
10531 && bfd_hash_lookup (flinfo->info->keep_hash,
10532 h->root.root.string, false, false) == NULL)
10533 strip = true;
10534 else if ((h->root.type == bfd_link_hash_defined
10535 || h->root.type == bfd_link_hash_defweak)
10536 && ((flinfo->info->strip_discarded
10537 && discarded_section (h->root.u.def.section))
10538 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10539 && h->root.u.def.section->owner != NULL
10540 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10541 strip = true;
10542 else if ((h->root.type == bfd_link_hash_undefined
10543 || h->root.type == bfd_link_hash_undefweak)
10544 && h->root.u.undef.abfd != NULL
10545 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10546 strip = true;
10547
10548 type = h->type;
10549
10550 /* If we're stripping it, and it's not a dynamic symbol, there's
10551 nothing else to do. However, if it is a forced local symbol or
10552 an ifunc symbol we need to give the backend finish_dynamic_symbol
10553 function a chance to make it dynamic. */
10554 if (strip
10555 && h->dynindx == -1
10556 && type != STT_GNU_IFUNC
10557 && !h->forced_local)
10558 return true;
10559
10560 sym.st_value = 0;
10561 sym.st_size = h->size;
10562 sym.st_other = h->other;
10563 switch (h->root.type)
10564 {
10565 default:
10566 case bfd_link_hash_new:
10567 case bfd_link_hash_warning:
10568 abort ();
10569 return false;
10570
10571 case bfd_link_hash_undefined:
10572 case bfd_link_hash_undefweak:
10573 input_sec = bfd_und_section_ptr;
10574 sym.st_shndx = SHN_UNDEF;
10575 break;
10576
10577 case bfd_link_hash_defined:
10578 case bfd_link_hash_defweak:
10579 {
10580 input_sec = h->root.u.def.section;
10581 if (input_sec->output_section != NULL)
10582 {
10583 sym.st_shndx =
10584 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10585 input_sec->output_section);
10586 if (sym.st_shndx == SHN_BAD)
10587 {
10588 _bfd_error_handler
10589 /* xgettext:c-format */
10590 (_("%pB: could not find output section %pA for input section %pA"),
10591 flinfo->output_bfd, input_sec->output_section, input_sec);
10592 bfd_set_error (bfd_error_nonrepresentable_section);
10593 eoinfo->failed = true;
10594 return false;
10595 }
10596
10597 /* ELF symbols in relocatable files are section relative,
10598 but in nonrelocatable files they are virtual
10599 addresses. */
10600 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10601 if (!bfd_link_relocatable (flinfo->info))
10602 {
10603 sym.st_value += input_sec->output_section->vma;
10604 if (h->type == STT_TLS)
10605 {
10606 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10607 if (tls_sec != NULL)
10608 sym.st_value -= tls_sec->vma;
10609 }
10610 }
10611 }
10612 else
10613 {
10614 BFD_ASSERT (input_sec->owner == NULL
10615 || (input_sec->owner->flags & DYNAMIC) != 0);
10616 sym.st_shndx = SHN_UNDEF;
10617 input_sec = bfd_und_section_ptr;
10618 }
10619 }
10620 break;
10621
10622 case bfd_link_hash_common:
10623 input_sec = h->root.u.c.p->section;
10624 sym.st_shndx = bed->common_section_index (input_sec);
10625 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10626 break;
10627
10628 case bfd_link_hash_indirect:
10629 /* These symbols are created by symbol versioning. They point
10630 to the decorated version of the name. For example, if the
10631 symbol foo@@GNU_1.2 is the default, which should be used when
10632 foo is used with no version, then we add an indirect symbol
10633 foo which points to foo@@GNU_1.2. We ignore these symbols,
10634 since the indirected symbol is already in the hash table. */
10635 return true;
10636 }
10637
10638 if (type == STT_COMMON || type == STT_OBJECT)
10639 switch (h->root.type)
10640 {
10641 case bfd_link_hash_common:
10642 type = elf_link_convert_common_type (flinfo->info, type);
10643 break;
10644 case bfd_link_hash_defined:
10645 case bfd_link_hash_defweak:
10646 if (bed->common_definition (&sym))
10647 type = elf_link_convert_common_type (flinfo->info, type);
10648 else
10649 type = STT_OBJECT;
10650 break;
10651 case bfd_link_hash_undefined:
10652 case bfd_link_hash_undefweak:
10653 break;
10654 default:
10655 abort ();
10656 }
10657
10658 if (h->forced_local)
10659 {
10660 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10661 /* Turn off visibility on local symbol. */
10662 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10663 }
10664 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10665 else if (h->unique_global && h->def_regular)
10666 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10667 else if (h->root.type == bfd_link_hash_undefweak
10668 || h->root.type == bfd_link_hash_defweak)
10669 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10670 else
10671 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10672 sym.st_target_internal = h->target_internal;
10673
10674 /* Give the processor backend a chance to tweak the symbol value,
10675 and also to finish up anything that needs to be done for this
10676 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10677 forced local syms when non-shared is due to a historical quirk.
10678 STT_GNU_IFUNC symbol must go through PLT. */
10679 if ((h->type == STT_GNU_IFUNC
10680 && h->def_regular
10681 && !bfd_link_relocatable (flinfo->info))
10682 || ((h->dynindx != -1
10683 || h->forced_local)
10684 && ((bfd_link_pic (flinfo->info)
10685 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10686 || h->root.type != bfd_link_hash_undefweak))
10687 || !h->forced_local)
10688 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10689 {
10690 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10691 (flinfo->output_bfd, flinfo->info, h, &sym)))
10692 {
10693 eoinfo->failed = true;
10694 return false;
10695 }
10696 }
10697
10698 /* If we are marking the symbol as undefined, and there are no
10699 non-weak references to this symbol from a regular object, then
10700 mark the symbol as weak undefined; if there are non-weak
10701 references, mark the symbol as strong. We can't do this earlier,
10702 because it might not be marked as undefined until the
10703 finish_dynamic_symbol routine gets through with it. */
10704 if (sym.st_shndx == SHN_UNDEF
10705 && h->ref_regular
10706 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10707 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10708 {
10709 int bindtype;
10710 type = ELF_ST_TYPE (sym.st_info);
10711
10712 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10713 if (type == STT_GNU_IFUNC)
10714 type = STT_FUNC;
10715
10716 if (h->ref_regular_nonweak)
10717 bindtype = STB_GLOBAL;
10718 else
10719 bindtype = STB_WEAK;
10720 sym.st_info = ELF_ST_INFO (bindtype, type);
10721 }
10722
10723 /* If this is a symbol defined in a dynamic library, don't use the
10724 symbol size from the dynamic library. Relinking an executable
10725 against a new library may introduce gratuitous changes in the
10726 executable's symbols if we keep the size. */
10727 if (sym.st_shndx == SHN_UNDEF
10728 && !h->def_regular
10729 && h->def_dynamic)
10730 sym.st_size = 0;
10731
10732 /* If a non-weak symbol with non-default visibility is not defined
10733 locally, it is a fatal error. */
10734 if (!bfd_link_relocatable (flinfo->info)
10735 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10736 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10737 && h->root.type == bfd_link_hash_undefined
10738 && !h->def_regular)
10739 {
10740 const char *msg;
10741
10742 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10743 /* xgettext:c-format */
10744 msg = _("%pB: protected symbol `%s' isn't defined");
10745 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10746 /* xgettext:c-format */
10747 msg = _("%pB: internal symbol `%s' isn't defined");
10748 else
10749 /* xgettext:c-format */
10750 msg = _("%pB: hidden symbol `%s' isn't defined");
10751 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10752 bfd_set_error (bfd_error_bad_value);
10753 eoinfo->failed = true;
10754 return false;
10755 }
10756
10757 /* If this symbol should be put in the .dynsym section, then put it
10758 there now. We already know the symbol index. We also fill in
10759 the entry in the .hash section. */
10760 if (h->dynindx != -1
10761 && elf_hash_table (flinfo->info)->dynamic_sections_created
10762 && elf_hash_table (flinfo->info)->dynsym != NULL
10763 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10764 {
10765 bfd_byte *esym;
10766
10767 /* Since there is no version information in the dynamic string,
10768 if there is no version info in symbol version section, we will
10769 have a run-time problem if not linking executable, referenced
10770 by shared library, or not bound locally. */
10771 if (h->verinfo.verdef == NULL
10772 && (!bfd_link_executable (flinfo->info)
10773 || h->ref_dynamic
10774 || !h->def_regular))
10775 {
10776 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10777
10778 if (p && p [1] != '\0')
10779 {
10780 _bfd_error_handler
10781 /* xgettext:c-format */
10782 (_("%pB: no symbol version section for versioned symbol `%s'"),
10783 flinfo->output_bfd, h->root.root.string);
10784 eoinfo->failed = true;
10785 return false;
10786 }
10787 }
10788
10789 sym.st_name = h->dynstr_index;
10790 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10791 + h->dynindx * bed->s->sizeof_sym);
10792 if (!check_dynsym (flinfo->output_bfd, &sym))
10793 {
10794 eoinfo->failed = true;
10795 return false;
10796 }
10797
10798 /* Inform the linker of the addition of this symbol. */
10799
10800 if (flinfo->info->callbacks->ctf_new_dynsym)
10801 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10802
10803 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10804
10805 if (flinfo->hash_sec != NULL)
10806 {
10807 size_t hash_entry_size;
10808 bfd_byte *bucketpos;
10809 bfd_vma chain;
10810 size_t bucketcount;
10811 size_t bucket;
10812
10813 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10814 bucket = h->u.elf_hash_value % bucketcount;
10815
10816 hash_entry_size
10817 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10818 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10819 + (bucket + 2) * hash_entry_size);
10820 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10821 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10822 bucketpos);
10823 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10824 ((bfd_byte *) flinfo->hash_sec->contents
10825 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10826 }
10827
10828 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10829 {
10830 Elf_Internal_Versym iversym;
10831 Elf_External_Versym *eversym;
10832
10833 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10834 {
10835 if (h->verinfo.verdef == NULL
10836 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10837 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10838 iversym.vs_vers = 1;
10839 else
10840 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10841 }
10842 else
10843 {
10844 if (h->verinfo.vertree == NULL)
10845 iversym.vs_vers = 1;
10846 else
10847 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10848 if (flinfo->info->create_default_symver)
10849 iversym.vs_vers++;
10850 }
10851
10852 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10853 defined locally. */
10854 if (h->versioned == versioned_hidden && h->def_regular)
10855 iversym.vs_vers |= VERSYM_HIDDEN;
10856
10857 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10858 eversym += h->dynindx;
10859 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10860 }
10861 }
10862
10863 /* If the symbol is undefined, and we didn't output it to .dynsym,
10864 strip it from .symtab too. Obviously we can't do this for
10865 relocatable output or when needed for --emit-relocs. */
10866 else if (input_sec == bfd_und_section_ptr
10867 && h->indx != -2
10868 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10869 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10870 && !bfd_link_relocatable (flinfo->info))
10871 return true;
10872
10873 /* Also strip others that we couldn't earlier due to dynamic symbol
10874 processing. */
10875 if (strip)
10876 return true;
10877 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10878 return true;
10879
10880 /* Output a FILE symbol so that following locals are not associated
10881 with the wrong input file. We need one for forced local symbols
10882 if we've seen more than one FILE symbol or when we have exactly
10883 one FILE symbol but global symbols are present in a file other
10884 than the one with the FILE symbol. We also need one if linker
10885 defined symbols are present. In practice these conditions are
10886 always met, so just emit the FILE symbol unconditionally. */
10887 if (eoinfo->localsyms
10888 && !eoinfo->file_sym_done
10889 && eoinfo->flinfo->filesym_count != 0)
10890 {
10891 Elf_Internal_Sym fsym;
10892
10893 memset (&fsym, 0, sizeof (fsym));
10894 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10895 fsym.st_shndx = SHN_ABS;
10896 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10897 bfd_und_section_ptr, NULL))
10898 return false;
10899
10900 eoinfo->file_sym_done = true;
10901 }
10902
10903 indx = bfd_get_symcount (flinfo->output_bfd);
10904 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10905 input_sec, h);
10906 if (ret == 0)
10907 {
10908 eoinfo->failed = true;
10909 return false;
10910 }
10911 else if (ret == 1)
10912 h->indx = indx;
10913 else if (h->indx == -2)
10914 abort();
10915
10916 return true;
10917 }
10918
10919 /* Return TRUE if special handling is done for relocs in SEC against
10920 symbols defined in discarded sections. */
10921
10922 static bool
10923 elf_section_ignore_discarded_relocs (asection *sec)
10924 {
10925 const struct elf_backend_data *bed;
10926
10927 switch (sec->sec_info_type)
10928 {
10929 case SEC_INFO_TYPE_STABS:
10930 case SEC_INFO_TYPE_EH_FRAME:
10931 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10932 case SEC_INFO_TYPE_SFRAME:
10933 return true;
10934 default:
10935 break;
10936 }
10937
10938 bed = get_elf_backend_data (sec->owner);
10939 if (bed->elf_backend_ignore_discarded_relocs != NULL
10940 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10941 return true;
10942
10943 return false;
10944 }
10945
10946 /* Return a mask saying how ld should treat relocations in SEC against
10947 symbols defined in discarded sections. If this function returns
10948 COMPLAIN set, ld will issue a warning message. If this function
10949 returns PRETEND set, and the discarded section was link-once and the
10950 same size as the kept link-once section, ld will pretend that the
10951 symbol was actually defined in the kept section. Otherwise ld will
10952 zero the reloc (at least that is the intent, but some cooperation by
10953 the target dependent code is needed, particularly for REL targets). */
10954
10955 unsigned int
10956 _bfd_elf_default_action_discarded (asection *sec)
10957 {
10958 const struct elf_backend_data *bed;
10959 bed = get_elf_backend_data (sec->owner);
10960
10961 if (sec->flags & SEC_DEBUGGING)
10962 return PRETEND;
10963
10964 if (strcmp (".eh_frame", sec->name) == 0)
10965 return 0;
10966
10967 if (bed->elf_backend_can_make_multiple_eh_frame
10968 && strncmp (sec->name, ".eh_frame.", 10) == 0)
10969 return 0;
10970
10971 if (strcmp (".sframe", sec->name) == 0)
10972 return 0;
10973
10974 if (strcmp (".gcc_except_table", sec->name) == 0)
10975 return 0;
10976
10977 return COMPLAIN | PRETEND;
10978 }
10979
10980 /* Find a match between a section and a member of a section group. */
10981
10982 static asection *
10983 match_group_member (asection *sec, asection *group,
10984 struct bfd_link_info *info)
10985 {
10986 asection *first = elf_next_in_group (group);
10987 asection *s = first;
10988
10989 while (s != NULL)
10990 {
10991 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10992 return s;
10993
10994 s = elf_next_in_group (s);
10995 if (s == first)
10996 break;
10997 }
10998
10999 return NULL;
11000 }
11001
11002 /* Check if the kept section of a discarded section SEC can be used
11003 to replace it. Return the replacement if it is OK. Otherwise return
11004 NULL. */
11005
11006 asection *
11007 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11008 {
11009 asection *kept;
11010
11011 kept = sec->kept_section;
11012 if (kept != NULL)
11013 {
11014 if ((kept->flags & SEC_GROUP) != 0)
11015 kept = match_group_member (sec, kept, info);
11016 if (kept != NULL)
11017 {
11018 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11019 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11020 kept = NULL;
11021 else
11022 {
11023 /* Get the real kept section. */
11024 asection *next;
11025 for (next = kept->kept_section;
11026 next != NULL;
11027 next = next->kept_section)
11028 kept = next;
11029 }
11030 }
11031 sec->kept_section = kept;
11032 }
11033 return kept;
11034 }
11035
11036 /* Link an input file into the linker output file. This function
11037 handles all the sections and relocations of the input file at once.
11038 This is so that we only have to read the local symbols once, and
11039 don't have to keep them in memory. */
11040
11041 static bool
11042 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11043 {
11044 int (*relocate_section)
11045 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11046 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11047 bfd *output_bfd;
11048 Elf_Internal_Shdr *symtab_hdr;
11049 size_t locsymcount;
11050 size_t extsymoff;
11051 Elf_Internal_Sym *isymbuf;
11052 Elf_Internal_Sym *isym;
11053 Elf_Internal_Sym *isymend;
11054 long *pindex;
11055 asection **ppsection;
11056 asection *o;
11057 const struct elf_backend_data *bed;
11058 struct elf_link_hash_entry **sym_hashes;
11059 bfd_size_type address_size;
11060 bfd_vma r_type_mask;
11061 int r_sym_shift;
11062 bool have_file_sym = false;
11063
11064 output_bfd = flinfo->output_bfd;
11065 bed = get_elf_backend_data (output_bfd);
11066 relocate_section = bed->elf_backend_relocate_section;
11067
11068 /* If this is a dynamic object, we don't want to do anything here:
11069 we don't want the local symbols, and we don't want the section
11070 contents. */
11071 if ((input_bfd->flags & DYNAMIC) != 0)
11072 return true;
11073
11074 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11075 if (elf_bad_symtab (input_bfd))
11076 {
11077 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11078 extsymoff = 0;
11079 }
11080 else
11081 {
11082 locsymcount = symtab_hdr->sh_info;
11083 extsymoff = symtab_hdr->sh_info;
11084 }
11085
11086 /* Enable GNU OSABI features in the output BFD that are used in the input
11087 BFD. */
11088 if (bed->elf_osabi == ELFOSABI_NONE
11089 || bed->elf_osabi == ELFOSABI_GNU
11090 || bed->elf_osabi == ELFOSABI_FREEBSD)
11091 elf_tdata (output_bfd)->has_gnu_osabi
11092 |= (elf_tdata (input_bfd)->has_gnu_osabi
11093 & (bfd_link_relocatable (flinfo->info)
11094 ? -1 : ~elf_gnu_osabi_retain));
11095
11096 /* Read the local symbols. */
11097 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11098 if (isymbuf == NULL && locsymcount != 0)
11099 {
11100 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11101 flinfo->internal_syms,
11102 flinfo->external_syms,
11103 flinfo->locsym_shndx);
11104 if (isymbuf == NULL)
11105 return false;
11106 }
11107
11108 /* Find local symbol sections and adjust values of symbols in
11109 SEC_MERGE sections. Write out those local symbols we know are
11110 going into the output file. */
11111 isymend = PTR_ADD (isymbuf, locsymcount);
11112 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11113 isym < isymend;
11114 isym++, pindex++, ppsection++)
11115 {
11116 asection *isec;
11117 const char *name;
11118 Elf_Internal_Sym osym;
11119 long indx;
11120 int ret;
11121
11122 *pindex = -1;
11123
11124 if (elf_bad_symtab (input_bfd))
11125 {
11126 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11127 {
11128 *ppsection = NULL;
11129 continue;
11130 }
11131 }
11132
11133 if (isym->st_shndx == SHN_UNDEF)
11134 isec = bfd_und_section_ptr;
11135 else if (isym->st_shndx == SHN_ABS)
11136 isec = bfd_abs_section_ptr;
11137 else if (isym->st_shndx == SHN_COMMON)
11138 isec = bfd_com_section_ptr;
11139 else
11140 {
11141 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11142 if (isec == NULL)
11143 {
11144 /* Don't attempt to output symbols with st_shnx in the
11145 reserved range other than SHN_ABS and SHN_COMMON. */
11146 isec = bfd_und_section_ptr;
11147 }
11148 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11149 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11150 isym->st_value =
11151 _bfd_merged_section_offset (output_bfd, &isec,
11152 elf_section_data (isec)->sec_info,
11153 isym->st_value);
11154 }
11155
11156 *ppsection = isec;
11157
11158 /* Don't output the first, undefined, symbol. In fact, don't
11159 output any undefined local symbol. */
11160 if (isec == bfd_und_section_ptr)
11161 continue;
11162
11163 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11164 {
11165 /* We never output section symbols. Instead, we use the
11166 section symbol of the corresponding section in the output
11167 file. */
11168 continue;
11169 }
11170
11171 /* If we are stripping all symbols, we don't want to output this
11172 one. */
11173 if (flinfo->info->strip == strip_all)
11174 continue;
11175
11176 /* If we are discarding all local symbols, we don't want to
11177 output this one. If we are generating a relocatable output
11178 file, then some of the local symbols may be required by
11179 relocs; we output them below as we discover that they are
11180 needed. */
11181 if (flinfo->info->discard == discard_all)
11182 continue;
11183
11184 /* If this symbol is defined in a section which we are
11185 discarding, we don't need to keep it. */
11186 if (isym->st_shndx < SHN_LORESERVE
11187 && (isec->output_section == NULL
11188 || bfd_section_removed_from_list (output_bfd,
11189 isec->output_section)))
11190 continue;
11191
11192 /* Get the name of the symbol. */
11193 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11194 isym->st_name);
11195 if (name == NULL)
11196 return false;
11197
11198 /* See if we are discarding symbols with this name. */
11199 if ((flinfo->info->strip == strip_some
11200 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11201 == NULL))
11202 || (((flinfo->info->discard == discard_sec_merge
11203 && (isec->flags & SEC_MERGE)
11204 && !bfd_link_relocatable (flinfo->info))
11205 || flinfo->info->discard == discard_l)
11206 && bfd_is_local_label_name (input_bfd, name)))
11207 continue;
11208
11209 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11210 {
11211 if (input_bfd->lto_output)
11212 /* -flto puts a temp file name here. This means builds
11213 are not reproducible. Discard the symbol. */
11214 continue;
11215 have_file_sym = true;
11216 flinfo->filesym_count += 1;
11217 }
11218 if (!have_file_sym)
11219 {
11220 /* In the absence of debug info, bfd_find_nearest_line uses
11221 FILE symbols to determine the source file for local
11222 function symbols. Provide a FILE symbol here if input
11223 files lack such, so that their symbols won't be
11224 associated with a previous input file. It's not the
11225 source file, but the best we can do. */
11226 const char *filename;
11227 have_file_sym = true;
11228 flinfo->filesym_count += 1;
11229 memset (&osym, 0, sizeof (osym));
11230 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11231 osym.st_shndx = SHN_ABS;
11232 if (input_bfd->lto_output)
11233 filename = NULL;
11234 else
11235 filename = lbasename (bfd_get_filename (input_bfd));
11236 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11237 bfd_abs_section_ptr, NULL))
11238 return false;
11239 }
11240
11241 osym = *isym;
11242
11243 /* Adjust the section index for the output file. */
11244 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11245 isec->output_section);
11246 if (osym.st_shndx == SHN_BAD)
11247 return false;
11248
11249 /* ELF symbols in relocatable files are section relative, but
11250 in executable files they are virtual addresses. Note that
11251 this code assumes that all ELF sections have an associated
11252 BFD section with a reasonable value for output_offset; below
11253 we assume that they also have a reasonable value for
11254 output_section. Any special sections must be set up to meet
11255 these requirements. */
11256 osym.st_value += isec->output_offset;
11257 if (!bfd_link_relocatable (flinfo->info))
11258 {
11259 osym.st_value += isec->output_section->vma;
11260 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11261 {
11262 /* STT_TLS symbols are relative to PT_TLS segment base. */
11263 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11264 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11265 else
11266 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11267 STT_NOTYPE);
11268 }
11269 }
11270
11271 indx = bfd_get_symcount (output_bfd);
11272 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11273 if (ret == 0)
11274 return false;
11275 else if (ret == 1)
11276 *pindex = indx;
11277 }
11278
11279 if (bed->s->arch_size == 32)
11280 {
11281 r_type_mask = 0xff;
11282 r_sym_shift = 8;
11283 address_size = 4;
11284 }
11285 else
11286 {
11287 r_type_mask = 0xffffffff;
11288 r_sym_shift = 32;
11289 address_size = 8;
11290 }
11291
11292 /* Relocate the contents of each section. */
11293 sym_hashes = elf_sym_hashes (input_bfd);
11294 for (o = input_bfd->sections; o != NULL; o = o->next)
11295 {
11296 bfd_byte *contents;
11297
11298 if (! o->linker_mark)
11299 {
11300 /* This section was omitted from the link. */
11301 continue;
11302 }
11303
11304 if (!flinfo->info->resolve_section_groups
11305 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11306 {
11307 /* Deal with the group signature symbol. */
11308 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11309 unsigned long symndx = sec_data->this_hdr.sh_info;
11310 asection *osec = o->output_section;
11311
11312 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11313 if (symndx >= locsymcount
11314 || (elf_bad_symtab (input_bfd)
11315 && flinfo->sections[symndx] == NULL))
11316 {
11317 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11318 while (h->root.type == bfd_link_hash_indirect
11319 || h->root.type == bfd_link_hash_warning)
11320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11321 /* Arrange for symbol to be output. */
11322 h->indx = -2;
11323 elf_section_data (osec)->this_hdr.sh_info = -2;
11324 }
11325 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11326 {
11327 /* We'll use the output section target_index. */
11328 asection *sec = flinfo->sections[symndx]->output_section;
11329 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11330 }
11331 else
11332 {
11333 if (flinfo->indices[symndx] == -1)
11334 {
11335 /* Otherwise output the local symbol now. */
11336 Elf_Internal_Sym sym = isymbuf[symndx];
11337 asection *sec = flinfo->sections[symndx]->output_section;
11338 const char *name;
11339 long indx;
11340 int ret;
11341
11342 name = bfd_elf_string_from_elf_section (input_bfd,
11343 symtab_hdr->sh_link,
11344 sym.st_name);
11345 if (name == NULL)
11346 return false;
11347
11348 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11349 sec);
11350 if (sym.st_shndx == SHN_BAD)
11351 return false;
11352
11353 sym.st_value += o->output_offset;
11354
11355 indx = bfd_get_symcount (output_bfd);
11356 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11357 NULL);
11358 if (ret == 0)
11359 return false;
11360 else if (ret == 1)
11361 flinfo->indices[symndx] = indx;
11362 else
11363 abort ();
11364 }
11365 elf_section_data (osec)->this_hdr.sh_info
11366 = flinfo->indices[symndx];
11367 }
11368 }
11369
11370 if ((o->flags & SEC_HAS_CONTENTS) == 0
11371 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11372 continue;
11373
11374 if ((o->flags & SEC_LINKER_CREATED) != 0)
11375 {
11376 /* Section was created by _bfd_elf_link_create_dynamic_sections
11377 or somesuch. */
11378 continue;
11379 }
11380
11381 /* Get the contents of the section. They have been cached by a
11382 relaxation routine. Note that o is a section in an input
11383 file, so the contents field will not have been set by any of
11384 the routines which work on output files. */
11385 if (elf_section_data (o)->this_hdr.contents != NULL)
11386 {
11387 contents = elf_section_data (o)->this_hdr.contents;
11388 if (bed->caches_rawsize
11389 && o->rawsize != 0
11390 && o->rawsize < o->size)
11391 {
11392 memcpy (flinfo->contents, contents, o->rawsize);
11393 contents = flinfo->contents;
11394 }
11395 }
11396 else if (!(o->flags & SEC_RELOC)
11397 && !bed->elf_backend_write_section
11398 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11399 /* A MERGE section that has no relocations doesn't need the
11400 contents anymore, they have been recorded earlier. Except
11401 if the backend has special provisions for writing sections. */
11402 contents = NULL;
11403 else
11404 {
11405 contents = flinfo->contents;
11406 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11407 return false;
11408 }
11409
11410 if ((o->flags & SEC_RELOC) != 0)
11411 {
11412 Elf_Internal_Rela *internal_relocs;
11413 Elf_Internal_Rela *rel, *relend;
11414 int action_discarded;
11415 int ret;
11416
11417 /* Get the swapped relocs. */
11418 internal_relocs
11419 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11420 flinfo->external_relocs,
11421 flinfo->internal_relocs,
11422 false);
11423 if (internal_relocs == NULL
11424 && o->reloc_count > 0)
11425 return false;
11426
11427 action_discarded = -1;
11428 if (!elf_section_ignore_discarded_relocs (o))
11429 action_discarded = (*bed->action_discarded) (o);
11430
11431 /* Run through the relocs evaluating complex reloc symbols and
11432 looking for relocs against symbols from discarded sections
11433 or section symbols from removed link-once sections.
11434 Complain about relocs against discarded sections. Zero
11435 relocs against removed link-once sections. */
11436
11437 rel = internal_relocs;
11438 relend = rel + o->reloc_count;
11439 for ( ; rel < relend; rel++)
11440 {
11441 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11442 unsigned int s_type;
11443 asection **ps, *sec;
11444 struct elf_link_hash_entry *h = NULL;
11445 const char *sym_name;
11446
11447 if (r_symndx == STN_UNDEF)
11448 continue;
11449
11450 if (r_symndx >= locsymcount
11451 || (elf_bad_symtab (input_bfd)
11452 && flinfo->sections[r_symndx] == NULL))
11453 {
11454 h = sym_hashes[r_symndx - extsymoff];
11455
11456 /* Badly formatted input files can contain relocs that
11457 reference non-existant symbols. Check here so that
11458 we do not seg fault. */
11459 if (h == NULL)
11460 {
11461 _bfd_error_handler
11462 /* xgettext:c-format */
11463 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11464 "that references a non-existent global symbol"),
11465 input_bfd, (uint64_t) rel->r_info, o);
11466 bfd_set_error (bfd_error_bad_value);
11467 return false;
11468 }
11469
11470 while (h->root.type == bfd_link_hash_indirect
11471 || h->root.type == bfd_link_hash_warning)
11472 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11473
11474 s_type = h->type;
11475
11476 /* If a plugin symbol is referenced from a non-IR file,
11477 mark the symbol as undefined. Note that the
11478 linker may attach linker created dynamic sections
11479 to the plugin bfd. Symbols defined in linker
11480 created sections are not plugin symbols. */
11481 if ((h->root.non_ir_ref_regular
11482 || h->root.non_ir_ref_dynamic)
11483 && (h->root.type == bfd_link_hash_defined
11484 || h->root.type == bfd_link_hash_defweak)
11485 && (h->root.u.def.section->flags
11486 & SEC_LINKER_CREATED) == 0
11487 && h->root.u.def.section->owner != NULL
11488 && (h->root.u.def.section->owner->flags
11489 & BFD_PLUGIN) != 0)
11490 {
11491 h->root.type = bfd_link_hash_undefined;
11492 h->root.u.undef.abfd = h->root.u.def.section->owner;
11493 }
11494
11495 ps = NULL;
11496 if (h->root.type == bfd_link_hash_defined
11497 || h->root.type == bfd_link_hash_defweak)
11498 ps = &h->root.u.def.section;
11499
11500 sym_name = h->root.root.string;
11501 }
11502 else
11503 {
11504 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11505
11506 s_type = ELF_ST_TYPE (sym->st_info);
11507 ps = &flinfo->sections[r_symndx];
11508 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11509 sym, *ps);
11510 }
11511
11512 if ((s_type == STT_RELC || s_type == STT_SRELC)
11513 && !bfd_link_relocatable (flinfo->info))
11514 {
11515 bfd_vma val;
11516 bfd_vma dot = (rel->r_offset
11517 + o->output_offset + o->output_section->vma);
11518 #ifdef DEBUG
11519 printf ("Encountered a complex symbol!");
11520 printf (" (input_bfd %s, section %s, reloc %ld\n",
11521 bfd_get_filename (input_bfd), o->name,
11522 (long) (rel - internal_relocs));
11523 printf (" symbol: idx %8.8lx, name %s\n",
11524 r_symndx, sym_name);
11525 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11526 (unsigned long) rel->r_info,
11527 (unsigned long) rel->r_offset);
11528 #endif
11529 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11530 isymbuf, locsymcount, s_type == STT_SRELC))
11531 return false;
11532
11533 /* Symbol evaluated OK. Update to absolute value. */
11534 set_symbol_value (input_bfd, isymbuf, locsymcount,
11535 r_symndx, val);
11536 continue;
11537 }
11538
11539 if (action_discarded != -1 && ps != NULL)
11540 {
11541 /* Complain if the definition comes from a
11542 discarded section. */
11543 if ((sec = *ps) != NULL && discarded_section (sec))
11544 {
11545 BFD_ASSERT (r_symndx != STN_UNDEF);
11546 if (action_discarded & COMPLAIN)
11547 (*flinfo->info->callbacks->einfo)
11548 /* xgettext:c-format */
11549 (_("%X`%s' referenced in section `%pA' of %pB: "
11550 "defined in discarded section `%pA' of %pB\n"),
11551 sym_name, o, input_bfd, sec, sec->owner);
11552
11553 /* Try to do the best we can to support buggy old
11554 versions of gcc. Pretend that the symbol is
11555 really defined in the kept linkonce section.
11556 FIXME: This is quite broken. Modifying the
11557 symbol here means we will be changing all later
11558 uses of the symbol, not just in this section. */
11559 if (action_discarded & PRETEND)
11560 {
11561 asection *kept;
11562
11563 kept = _bfd_elf_check_kept_section (sec,
11564 flinfo->info);
11565 if (kept != NULL)
11566 {
11567 *ps = kept;
11568 continue;
11569 }
11570 }
11571 }
11572 }
11573 }
11574
11575 /* Relocate the section by invoking a back end routine.
11576
11577 The back end routine is responsible for adjusting the
11578 section contents as necessary, and (if using Rela relocs
11579 and generating a relocatable output file) adjusting the
11580 reloc addend as necessary.
11581
11582 The back end routine does not have to worry about setting
11583 the reloc address or the reloc symbol index.
11584
11585 The back end routine is given a pointer to the swapped in
11586 internal symbols, and can access the hash table entries
11587 for the external symbols via elf_sym_hashes (input_bfd).
11588
11589 When generating relocatable output, the back end routine
11590 must handle STB_LOCAL/STT_SECTION symbols specially. The
11591 output symbol is going to be a section symbol
11592 corresponding to the output section, which will require
11593 the addend to be adjusted. */
11594
11595 ret = (*relocate_section) (output_bfd, flinfo->info,
11596 input_bfd, o, contents,
11597 internal_relocs,
11598 isymbuf,
11599 flinfo->sections);
11600 if (!ret)
11601 return false;
11602
11603 if (ret == 2
11604 || bfd_link_relocatable (flinfo->info)
11605 || flinfo->info->emitrelocations)
11606 {
11607 Elf_Internal_Rela *irela;
11608 Elf_Internal_Rela *irelaend, *irelamid;
11609 bfd_vma last_offset;
11610 struct elf_link_hash_entry **rel_hash;
11611 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11612 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11613 unsigned int next_erel;
11614 bool rela_normal;
11615 struct bfd_elf_section_data *esdi, *esdo;
11616
11617 esdi = elf_section_data (o);
11618 esdo = elf_section_data (o->output_section);
11619 rela_normal = false;
11620
11621 /* Adjust the reloc addresses and symbol indices. */
11622
11623 irela = internal_relocs;
11624 irelaend = irela + o->reloc_count;
11625 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11626 /* We start processing the REL relocs, if any. When we reach
11627 IRELAMID in the loop, we switch to the RELA relocs. */
11628 irelamid = irela;
11629 if (esdi->rel.hdr != NULL)
11630 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11631 * bed->s->int_rels_per_ext_rel);
11632 rel_hash_list = rel_hash;
11633 rela_hash_list = NULL;
11634 last_offset = o->output_offset;
11635 if (!bfd_link_relocatable (flinfo->info))
11636 last_offset += o->output_section->vma;
11637 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11638 {
11639 unsigned long r_symndx;
11640 asection *sec;
11641 Elf_Internal_Sym sym;
11642
11643 if (next_erel == bed->s->int_rels_per_ext_rel)
11644 {
11645 rel_hash++;
11646 next_erel = 0;
11647 }
11648
11649 if (irela == irelamid)
11650 {
11651 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11652 rela_hash_list = rel_hash;
11653 rela_normal = bed->rela_normal;
11654 }
11655
11656 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11657 flinfo->info, o,
11658 irela->r_offset);
11659 if (irela->r_offset >= (bfd_vma) -2)
11660 {
11661 /* This is a reloc for a deleted entry or somesuch.
11662 Turn it into an R_*_NONE reloc, at the same
11663 offset as the last reloc. elf_eh_frame.c and
11664 bfd_elf_discard_info rely on reloc offsets
11665 being ordered. */
11666 irela->r_offset = last_offset;
11667 irela->r_info = 0;
11668 irela->r_addend = 0;
11669 continue;
11670 }
11671
11672 irela->r_offset += o->output_offset;
11673
11674 /* Relocs in an executable have to be virtual addresses. */
11675 if (!bfd_link_relocatable (flinfo->info))
11676 irela->r_offset += o->output_section->vma;
11677
11678 last_offset = irela->r_offset;
11679
11680 r_symndx = irela->r_info >> r_sym_shift;
11681 if (r_symndx == STN_UNDEF)
11682 continue;
11683
11684 if (r_symndx >= locsymcount
11685 || (elf_bad_symtab (input_bfd)
11686 && flinfo->sections[r_symndx] == NULL))
11687 {
11688 struct elf_link_hash_entry *rh;
11689 unsigned long indx;
11690
11691 /* This is a reloc against a global symbol. We
11692 have not yet output all the local symbols, so
11693 we do not know the symbol index of any global
11694 symbol. We set the rel_hash entry for this
11695 reloc to point to the global hash table entry
11696 for this symbol. The symbol index is then
11697 set at the end of bfd_elf_final_link. */
11698 indx = r_symndx - extsymoff;
11699 rh = elf_sym_hashes (input_bfd)[indx];
11700 while (rh->root.type == bfd_link_hash_indirect
11701 || rh->root.type == bfd_link_hash_warning)
11702 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11703
11704 /* Setting the index to -2 tells
11705 elf_link_output_extsym that this symbol is
11706 used by a reloc. */
11707 BFD_ASSERT (rh->indx < 0);
11708 rh->indx = -2;
11709 *rel_hash = rh;
11710
11711 continue;
11712 }
11713
11714 /* This is a reloc against a local symbol. */
11715
11716 *rel_hash = NULL;
11717 sym = isymbuf[r_symndx];
11718 sec = flinfo->sections[r_symndx];
11719 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11720 {
11721 /* I suppose the backend ought to fill in the
11722 section of any STT_SECTION symbol against a
11723 processor specific section. */
11724 r_symndx = STN_UNDEF;
11725 if (bfd_is_abs_section (sec))
11726 ;
11727 else if (sec == NULL || sec->owner == NULL)
11728 {
11729 bfd_set_error (bfd_error_bad_value);
11730 return false;
11731 }
11732 else
11733 {
11734 asection *osec = sec->output_section;
11735
11736 /* If we have discarded a section, the output
11737 section will be the absolute section. In
11738 case of discarded SEC_MERGE sections, use
11739 the kept section. relocate_section should
11740 have already handled discarded linkonce
11741 sections. */
11742 if (bfd_is_abs_section (osec)
11743 && sec->kept_section != NULL
11744 && sec->kept_section->output_section != NULL)
11745 {
11746 osec = sec->kept_section->output_section;
11747 irela->r_addend -= osec->vma;
11748 }
11749
11750 if (!bfd_is_abs_section (osec))
11751 {
11752 r_symndx = osec->target_index;
11753 if (r_symndx == STN_UNDEF)
11754 {
11755 irela->r_addend += osec->vma;
11756 osec = _bfd_nearby_section (output_bfd, osec,
11757 osec->vma);
11758 irela->r_addend -= osec->vma;
11759 r_symndx = osec->target_index;
11760 }
11761 }
11762 }
11763
11764 /* Adjust the addend according to where the
11765 section winds up in the output section. */
11766 if (rela_normal)
11767 irela->r_addend += sec->output_offset;
11768 }
11769 else
11770 {
11771 if (flinfo->indices[r_symndx] == -1)
11772 {
11773 unsigned long shlink;
11774 const char *name;
11775 asection *osec;
11776 long indx;
11777
11778 if (flinfo->info->strip == strip_all)
11779 {
11780 /* You can't do ld -r -s. */
11781 bfd_set_error (bfd_error_invalid_operation);
11782 return false;
11783 }
11784
11785 /* This symbol was skipped earlier, but
11786 since it is needed by a reloc, we
11787 must output it now. */
11788 shlink = symtab_hdr->sh_link;
11789 name = (bfd_elf_string_from_elf_section
11790 (input_bfd, shlink, sym.st_name));
11791 if (name == NULL)
11792 return false;
11793
11794 osec = sec->output_section;
11795 sym.st_shndx =
11796 _bfd_elf_section_from_bfd_section (output_bfd,
11797 osec);
11798 if (sym.st_shndx == SHN_BAD)
11799 return false;
11800
11801 sym.st_value += sec->output_offset;
11802 if (!bfd_link_relocatable (flinfo->info))
11803 {
11804 sym.st_value += osec->vma;
11805 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11806 {
11807 struct elf_link_hash_table *htab
11808 = elf_hash_table (flinfo->info);
11809
11810 /* STT_TLS symbols are relative to PT_TLS
11811 segment base. */
11812 if (htab->tls_sec != NULL)
11813 sym.st_value -= htab->tls_sec->vma;
11814 else
11815 sym.st_info
11816 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11817 STT_NOTYPE);
11818 }
11819 }
11820
11821 indx = bfd_get_symcount (output_bfd);
11822 ret = elf_link_output_symstrtab (flinfo, name,
11823 &sym, sec,
11824 NULL);
11825 if (ret == 0)
11826 return false;
11827 else if (ret == 1)
11828 flinfo->indices[r_symndx] = indx;
11829 else
11830 abort ();
11831 }
11832
11833 r_symndx = flinfo->indices[r_symndx];
11834 }
11835
11836 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11837 | (irela->r_info & r_type_mask));
11838 }
11839
11840 /* Swap out the relocs. */
11841 input_rel_hdr = esdi->rel.hdr;
11842 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11843 {
11844 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11845 input_rel_hdr,
11846 internal_relocs,
11847 rel_hash_list))
11848 return false;
11849 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11850 * bed->s->int_rels_per_ext_rel);
11851 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11852 }
11853
11854 input_rela_hdr = esdi->rela.hdr;
11855 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11856 {
11857 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11858 input_rela_hdr,
11859 internal_relocs,
11860 rela_hash_list))
11861 return false;
11862 }
11863 }
11864 }
11865
11866 /* Write out the modified section contents. */
11867 if (bed->elf_backend_write_section
11868 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11869 contents))
11870 {
11871 /* Section written out. */
11872 }
11873 else switch (o->sec_info_type)
11874 {
11875 case SEC_INFO_TYPE_STABS:
11876 if (! (_bfd_write_section_stabs
11877 (output_bfd,
11878 &elf_hash_table (flinfo->info)->stab_info,
11879 o, &elf_section_data (o)->sec_info, contents)))
11880 return false;
11881 break;
11882 case SEC_INFO_TYPE_MERGE:
11883 if (! _bfd_write_merged_section (output_bfd, o,
11884 elf_section_data (o)->sec_info))
11885 return false;
11886 break;
11887 case SEC_INFO_TYPE_EH_FRAME:
11888 {
11889 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11890 o, contents))
11891 return false;
11892 }
11893 break;
11894 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11895 {
11896 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11897 flinfo->info,
11898 o, contents))
11899 return false;
11900 }
11901 break;
11902 case SEC_INFO_TYPE_SFRAME:
11903 {
11904 /* Merge .sframe sections into the ctf frame encoder
11905 context of the output_bfd's section. The final .sframe
11906 output section will be written out later. */
11907 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11908 o, contents))
11909 return false;
11910 }
11911 break;
11912 default:
11913 {
11914 if (! (o->flags & SEC_EXCLUDE))
11915 {
11916 file_ptr offset = (file_ptr) o->output_offset;
11917 bfd_size_type todo = o->size;
11918
11919 offset *= bfd_octets_per_byte (output_bfd, o);
11920
11921 if ((o->flags & SEC_ELF_REVERSE_COPY)
11922 && o->size > address_size)
11923 {
11924 /* Reverse-copy input section to output. */
11925
11926 if ((o->size & (address_size - 1)) != 0
11927 || (o->reloc_count != 0
11928 && (o->size * bed->s->int_rels_per_ext_rel
11929 != o->reloc_count * address_size)))
11930 {
11931 _bfd_error_handler
11932 /* xgettext:c-format */
11933 (_("error: %pB: size of section %pA is not "
11934 "multiple of address size"),
11935 input_bfd, o);
11936 bfd_set_error (bfd_error_bad_value);
11937 return false;
11938 }
11939
11940 do
11941 {
11942 todo -= address_size;
11943 if (! bfd_set_section_contents (output_bfd,
11944 o->output_section,
11945 contents + todo,
11946 offset,
11947 address_size))
11948 return false;
11949 if (todo == 0)
11950 break;
11951 offset += address_size;
11952 }
11953 while (1);
11954 }
11955 else if (! bfd_set_section_contents (output_bfd,
11956 o->output_section,
11957 contents,
11958 offset, todo))
11959 return false;
11960 }
11961 }
11962 break;
11963 }
11964 }
11965
11966 return true;
11967 }
11968
11969 /* Generate a reloc when linking an ELF file. This is a reloc
11970 requested by the linker, and does not come from any input file. This
11971 is used to build constructor and destructor tables when linking
11972 with -Ur. */
11973
11974 static bool
11975 elf_reloc_link_order (bfd *output_bfd,
11976 struct bfd_link_info *info,
11977 asection *output_section,
11978 struct bfd_link_order *link_order)
11979 {
11980 reloc_howto_type *howto;
11981 long indx;
11982 bfd_vma offset;
11983 bfd_vma addend;
11984 struct bfd_elf_section_reloc_data *reldata;
11985 struct elf_link_hash_entry **rel_hash_ptr;
11986 Elf_Internal_Shdr *rel_hdr;
11987 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11988 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11989 bfd_byte *erel;
11990 unsigned int i;
11991 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11992
11993 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11994 if (howto == NULL)
11995 {
11996 bfd_set_error (bfd_error_bad_value);
11997 return false;
11998 }
11999
12000 addend = link_order->u.reloc.p->addend;
12001
12002 if (esdo->rel.hdr)
12003 reldata = &esdo->rel;
12004 else if (esdo->rela.hdr)
12005 reldata = &esdo->rela;
12006 else
12007 {
12008 reldata = NULL;
12009 BFD_ASSERT (0);
12010 }
12011
12012 /* Figure out the symbol index. */
12013 rel_hash_ptr = reldata->hashes + reldata->count;
12014 if (link_order->type == bfd_section_reloc_link_order)
12015 {
12016 indx = link_order->u.reloc.p->u.section->target_index;
12017 BFD_ASSERT (indx != 0);
12018 *rel_hash_ptr = NULL;
12019 }
12020 else
12021 {
12022 struct elf_link_hash_entry *h;
12023
12024 /* Treat a reloc against a defined symbol as though it were
12025 actually against the section. */
12026 h = ((struct elf_link_hash_entry *)
12027 bfd_wrapped_link_hash_lookup (output_bfd, info,
12028 link_order->u.reloc.p->u.name,
12029 false, false, true));
12030 if (h != NULL
12031 && (h->root.type == bfd_link_hash_defined
12032 || h->root.type == bfd_link_hash_defweak))
12033 {
12034 asection *section;
12035
12036 section = h->root.u.def.section;
12037 indx = section->output_section->target_index;
12038 *rel_hash_ptr = NULL;
12039 /* It seems that we ought to add the symbol value to the
12040 addend here, but in practice it has already been added
12041 because it was passed to constructor_callback. */
12042 addend += section->output_section->vma + section->output_offset;
12043 }
12044 else if (h != NULL)
12045 {
12046 /* Setting the index to -2 tells elf_link_output_extsym that
12047 this symbol is used by a reloc. */
12048 h->indx = -2;
12049 *rel_hash_ptr = h;
12050 indx = 0;
12051 }
12052 else
12053 {
12054 (*info->callbacks->unattached_reloc)
12055 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12056 indx = 0;
12057 }
12058 }
12059
12060 /* If this is an inplace reloc, we must write the addend into the
12061 object file. */
12062 if (howto->partial_inplace && addend != 0)
12063 {
12064 bfd_size_type size;
12065 bfd_reloc_status_type rstat;
12066 bfd_byte *buf;
12067 bool ok;
12068 const char *sym_name;
12069 bfd_size_type octets;
12070
12071 size = (bfd_size_type) bfd_get_reloc_size (howto);
12072 buf = (bfd_byte *) bfd_zmalloc (size);
12073 if (buf == NULL && size != 0)
12074 return false;
12075 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12076 switch (rstat)
12077 {
12078 case bfd_reloc_ok:
12079 break;
12080
12081 default:
12082 case bfd_reloc_outofrange:
12083 abort ();
12084
12085 case bfd_reloc_overflow:
12086 if (link_order->type == bfd_section_reloc_link_order)
12087 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12088 else
12089 sym_name = link_order->u.reloc.p->u.name;
12090 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12091 howto->name, addend, NULL, NULL,
12092 (bfd_vma) 0);
12093 break;
12094 }
12095
12096 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12097 output_section);
12098 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12099 octets, size);
12100 free (buf);
12101 if (! ok)
12102 return false;
12103 }
12104
12105 /* The address of a reloc is relative to the section in a
12106 relocatable file, and is a virtual address in an executable
12107 file. */
12108 offset = link_order->offset;
12109 if (! bfd_link_relocatable (info))
12110 offset += output_section->vma;
12111
12112 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12113 {
12114 irel[i].r_offset = offset;
12115 irel[i].r_info = 0;
12116 irel[i].r_addend = 0;
12117 }
12118 if (bed->s->arch_size == 32)
12119 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12120 else
12121 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12122
12123 rel_hdr = reldata->hdr;
12124 erel = rel_hdr->contents;
12125 if (rel_hdr->sh_type == SHT_REL)
12126 {
12127 erel += reldata->count * bed->s->sizeof_rel;
12128 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12129 }
12130 else
12131 {
12132 irel[0].r_addend = addend;
12133 erel += reldata->count * bed->s->sizeof_rela;
12134 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12135 }
12136
12137 ++reldata->count;
12138
12139 return true;
12140 }
12141
12142 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12143 Returns TRUE upon success, FALSE otherwise. */
12144
12145 static bool
12146 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12147 {
12148 bool ret = false;
12149 bfd *implib_bfd;
12150 const struct elf_backend_data *bed;
12151 flagword flags;
12152 enum bfd_architecture arch;
12153 unsigned int mach;
12154 asymbol **sympp = NULL;
12155 long symsize;
12156 long symcount;
12157 long src_count;
12158 elf_symbol_type *osymbuf;
12159 size_t amt;
12160
12161 implib_bfd = info->out_implib_bfd;
12162 bed = get_elf_backend_data (abfd);
12163
12164 if (!bfd_set_format (implib_bfd, bfd_object))
12165 return false;
12166
12167 /* Use flag from executable but make it a relocatable object. */
12168 flags = bfd_get_file_flags (abfd);
12169 flags &= ~HAS_RELOC;
12170 if (!bfd_set_start_address (implib_bfd, 0)
12171 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12172 return false;
12173
12174 /* Copy architecture of output file to import library file. */
12175 arch = bfd_get_arch (abfd);
12176 mach = bfd_get_mach (abfd);
12177 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12178 && (abfd->target_defaulted
12179 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12180 return false;
12181
12182 /* Get symbol table size. */
12183 symsize = bfd_get_symtab_upper_bound (abfd);
12184 if (symsize < 0)
12185 return false;
12186
12187 /* Read in the symbol table. */
12188 sympp = (asymbol **) bfd_malloc (symsize);
12189 if (sympp == NULL)
12190 return false;
12191
12192 symcount = bfd_canonicalize_symtab (abfd, sympp);
12193 if (symcount < 0)
12194 goto free_sym_buf;
12195
12196 /* Allow the BFD backend to copy any private header data it
12197 understands from the output BFD to the import library BFD. */
12198 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12199 goto free_sym_buf;
12200
12201 /* Filter symbols to appear in the import library. */
12202 if (bed->elf_backend_filter_implib_symbols)
12203 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12204 symcount);
12205 else
12206 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12207 if (symcount == 0)
12208 {
12209 bfd_set_error (bfd_error_no_symbols);
12210 _bfd_error_handler (_("%pB: no symbol found for import library"),
12211 implib_bfd);
12212 goto free_sym_buf;
12213 }
12214
12215
12216 /* Make symbols absolute. */
12217 amt = symcount * sizeof (*osymbuf);
12218 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12219 if (osymbuf == NULL)
12220 goto free_sym_buf;
12221
12222 for (src_count = 0; src_count < symcount; src_count++)
12223 {
12224 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12225 sizeof (*osymbuf));
12226 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12227 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12228 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12229 osymbuf[src_count].internal_elf_sym.st_value =
12230 osymbuf[src_count].symbol.value;
12231 sympp[src_count] = &osymbuf[src_count].symbol;
12232 }
12233
12234 bfd_set_symtab (implib_bfd, sympp, symcount);
12235
12236 /* Allow the BFD backend to copy any private data it understands
12237 from the output BFD to the import library BFD. This is done last
12238 to permit the routine to look at the filtered symbol table. */
12239 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12240 goto free_sym_buf;
12241
12242 if (!bfd_close (implib_bfd))
12243 goto free_sym_buf;
12244
12245 ret = true;
12246
12247 free_sym_buf:
12248 free (sympp);
12249 return ret;
12250 }
12251
12252 static void
12253 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12254 {
12255 asection *o;
12256
12257 if (flinfo->symstrtab != NULL)
12258 _bfd_elf_strtab_free (flinfo->symstrtab);
12259 free (flinfo->contents);
12260 free (flinfo->external_relocs);
12261 free (flinfo->internal_relocs);
12262 free (flinfo->external_syms);
12263 free (flinfo->locsym_shndx);
12264 free (flinfo->internal_syms);
12265 free (flinfo->indices);
12266 free (flinfo->sections);
12267 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12268 free (flinfo->symshndxbuf);
12269 for (o = obfd->sections; o != NULL; o = o->next)
12270 {
12271 struct bfd_elf_section_data *esdo = elf_section_data (o);
12272 free (esdo->rel.hashes);
12273 free (esdo->rela.hashes);
12274 }
12275 }
12276
12277 /* Do the final step of an ELF link. */
12278
12279 bool
12280 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12281 {
12282 bool dynamic;
12283 bool emit_relocs;
12284 bfd *dynobj;
12285 struct elf_final_link_info flinfo;
12286 asection *o;
12287 struct bfd_link_order *p;
12288 bfd *sub;
12289 bfd_size_type max_contents_size;
12290 bfd_size_type max_external_reloc_size;
12291 bfd_size_type max_internal_reloc_count;
12292 bfd_size_type max_sym_count;
12293 bfd_size_type max_sym_shndx_count;
12294 Elf_Internal_Sym elfsym;
12295 unsigned int i;
12296 Elf_Internal_Shdr *symtab_hdr;
12297 Elf_Internal_Shdr *symtab_shndx_hdr;
12298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12299 struct elf_outext_info eoinfo;
12300 bool merged;
12301 size_t relativecount;
12302 size_t relr_entsize;
12303 asection *reldyn = 0;
12304 bfd_size_type amt;
12305 asection *attr_section = NULL;
12306 bfd_vma attr_size = 0;
12307 const char *std_attrs_section;
12308 struct elf_link_hash_table *htab = elf_hash_table (info);
12309 bool sections_removed;
12310 bool ret;
12311
12312 if (!is_elf_hash_table (&htab->root))
12313 return false;
12314
12315 if (bfd_link_pic (info))
12316 abfd->flags |= DYNAMIC;
12317
12318 dynamic = htab->dynamic_sections_created;
12319 dynobj = htab->dynobj;
12320
12321 emit_relocs = (bfd_link_relocatable (info)
12322 || info->emitrelocations);
12323
12324 memset (&flinfo, 0, sizeof (flinfo));
12325 flinfo.info = info;
12326 flinfo.output_bfd = abfd;
12327 flinfo.symstrtab = _bfd_elf_strtab_init ();
12328 if (flinfo.symstrtab == NULL)
12329 return false;
12330
12331 if (! dynamic)
12332 {
12333 flinfo.hash_sec = NULL;
12334 flinfo.symver_sec = NULL;
12335 }
12336 else
12337 {
12338 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12339 /* Note that dynsym_sec can be NULL (on VMS). */
12340 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12341 /* Note that it is OK if symver_sec is NULL. */
12342 }
12343
12344 if (info->unique_symbol
12345 && !bfd_hash_table_init (&flinfo.local_hash_table,
12346 local_hash_newfunc,
12347 sizeof (struct local_hash_entry)))
12348 return false;
12349
12350 /* The object attributes have been merged. Remove the input
12351 sections from the link, and set the contents of the output
12352 section. */
12353 sections_removed = false;
12354 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12355 for (o = abfd->sections; o != NULL; o = o->next)
12356 {
12357 bool remove_section = false;
12358
12359 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12360 || strcmp (o->name, ".gnu.attributes") == 0)
12361 {
12362 for (p = o->map_head.link_order; p != NULL; p = p->next)
12363 {
12364 asection *input_section;
12365
12366 if (p->type != bfd_indirect_link_order)
12367 continue;
12368 input_section = p->u.indirect.section;
12369 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12370 elf_link_input_bfd ignores this section. */
12371 input_section->flags &= ~SEC_HAS_CONTENTS;
12372 }
12373
12374 attr_size = bfd_elf_obj_attr_size (abfd);
12375 bfd_set_section_size (o, attr_size);
12376 /* Skip this section later on. */
12377 o->map_head.link_order = NULL;
12378 if (attr_size)
12379 attr_section = o;
12380 else
12381 remove_section = true;
12382 }
12383 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12384 {
12385 /* Remove empty group section from linker output. */
12386 remove_section = true;
12387 }
12388 if (remove_section)
12389 {
12390 o->flags |= SEC_EXCLUDE;
12391 bfd_section_list_remove (abfd, o);
12392 abfd->section_count--;
12393 sections_removed = true;
12394 }
12395 }
12396 if (sections_removed)
12397 _bfd_fix_excluded_sec_syms (abfd, info);
12398
12399 /* Count up the number of relocations we will output for each output
12400 section, so that we know the sizes of the reloc sections. We
12401 also figure out some maximum sizes. */
12402 max_contents_size = 0;
12403 max_external_reloc_size = 0;
12404 max_internal_reloc_count = 0;
12405 max_sym_count = 0;
12406 max_sym_shndx_count = 0;
12407 merged = false;
12408 for (o = abfd->sections; o != NULL; o = o->next)
12409 {
12410 struct bfd_elf_section_data *esdo = elf_section_data (o);
12411 o->reloc_count = 0;
12412
12413 for (p = o->map_head.link_order; p != NULL; p = p->next)
12414 {
12415 unsigned int reloc_count = 0;
12416 unsigned int additional_reloc_count = 0;
12417 struct bfd_elf_section_data *esdi = NULL;
12418
12419 if (p->type == bfd_section_reloc_link_order
12420 || p->type == bfd_symbol_reloc_link_order)
12421 reloc_count = 1;
12422 else if (p->type == bfd_indirect_link_order)
12423 {
12424 asection *sec;
12425
12426 sec = p->u.indirect.section;
12427
12428 /* Mark all sections which are to be included in the
12429 link. This will normally be every section. We need
12430 to do this so that we can identify any sections which
12431 the linker has decided to not include. */
12432 sec->linker_mark = true;
12433
12434 if (sec->flags & SEC_MERGE)
12435 merged = true;
12436
12437 if (sec->rawsize > max_contents_size)
12438 max_contents_size = sec->rawsize;
12439 if (sec->size > max_contents_size)
12440 max_contents_size = sec->size;
12441
12442 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12443 && (sec->owner->flags & DYNAMIC) == 0)
12444 {
12445 size_t sym_count;
12446
12447 /* We are interested in just local symbols, not all
12448 symbols. */
12449 if (elf_bad_symtab (sec->owner))
12450 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12451 / bed->s->sizeof_sym);
12452 else
12453 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12454
12455 if (sym_count > max_sym_count)
12456 max_sym_count = sym_count;
12457
12458 if (sym_count > max_sym_shndx_count
12459 && elf_symtab_shndx_list (sec->owner) != NULL)
12460 max_sym_shndx_count = sym_count;
12461
12462 esdi = elf_section_data (sec);
12463
12464 if (esdi->this_hdr.sh_type == SHT_REL
12465 || esdi->this_hdr.sh_type == SHT_RELA)
12466 /* Some backends use reloc_count in relocation sections
12467 to count particular types of relocs. Of course,
12468 reloc sections themselves can't have relocations. */
12469 ;
12470 else if (emit_relocs)
12471 {
12472 reloc_count = sec->reloc_count;
12473 if (bed->elf_backend_count_additional_relocs)
12474 {
12475 int c;
12476 c = (*bed->elf_backend_count_additional_relocs) (sec);
12477 additional_reloc_count += c;
12478 }
12479 }
12480 else if (bed->elf_backend_count_relocs)
12481 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12482
12483 if ((sec->flags & SEC_RELOC) != 0)
12484 {
12485 size_t ext_size = 0;
12486
12487 if (esdi->rel.hdr != NULL)
12488 ext_size = esdi->rel.hdr->sh_size;
12489 if (esdi->rela.hdr != NULL)
12490 ext_size += esdi->rela.hdr->sh_size;
12491
12492 if (ext_size > max_external_reloc_size)
12493 max_external_reloc_size = ext_size;
12494 if (sec->reloc_count > max_internal_reloc_count)
12495 max_internal_reloc_count = sec->reloc_count;
12496 }
12497 }
12498 }
12499
12500 if (reloc_count == 0)
12501 continue;
12502
12503 reloc_count += additional_reloc_count;
12504 o->reloc_count += reloc_count;
12505
12506 if (p->type == bfd_indirect_link_order && emit_relocs)
12507 {
12508 if (esdi->rel.hdr)
12509 {
12510 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12511 esdo->rel.count += additional_reloc_count;
12512 }
12513 if (esdi->rela.hdr)
12514 {
12515 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12516 esdo->rela.count += additional_reloc_count;
12517 }
12518 }
12519 else
12520 {
12521 if (o->use_rela_p)
12522 esdo->rela.count += reloc_count;
12523 else
12524 esdo->rel.count += reloc_count;
12525 }
12526 }
12527
12528 if (o->reloc_count > 0)
12529 o->flags |= SEC_RELOC;
12530 else
12531 {
12532 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12533 set it (this is probably a bug) and if it is set
12534 assign_section_numbers will create a reloc section. */
12535 o->flags &=~ SEC_RELOC;
12536 }
12537
12538 /* If the SEC_ALLOC flag is not set, force the section VMA to
12539 zero. This is done in elf_fake_sections as well, but forcing
12540 the VMA to 0 here will ensure that relocs against these
12541 sections are handled correctly. */
12542 if ((o->flags & SEC_ALLOC) == 0
12543 && ! o->user_set_vma)
12544 o->vma = 0;
12545 }
12546
12547 if (! bfd_link_relocatable (info) && merged)
12548 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12549
12550 /* Figure out the file positions for everything but the symbol table
12551 and the relocs. We set symcount to force assign_section_numbers
12552 to create a symbol table. */
12553 abfd->symcount = info->strip != strip_all || emit_relocs;
12554 BFD_ASSERT (! abfd->output_has_begun);
12555 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12556 goto error_return;
12557
12558 /* Set sizes, and assign file positions for reloc sections. */
12559 for (o = abfd->sections; o != NULL; o = o->next)
12560 {
12561 struct bfd_elf_section_data *esdo = elf_section_data (o);
12562 if ((o->flags & SEC_RELOC) != 0)
12563 {
12564 if (esdo->rel.hdr
12565 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12566 goto error_return;
12567
12568 if (esdo->rela.hdr
12569 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12570 goto error_return;
12571 }
12572
12573 /* _bfd_elf_compute_section_file_positions makes temporary use
12574 of target_index. Reset it. */
12575 o->target_index = 0;
12576
12577 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12578 to count upwards while actually outputting the relocations. */
12579 esdo->rel.count = 0;
12580 esdo->rela.count = 0;
12581
12582 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12583 && !bfd_section_is_ctf (o))
12584 {
12585 /* Cache the section contents so that they can be compressed
12586 later. Use bfd_malloc since it will be freed by
12587 bfd_compress_section_contents. */
12588 unsigned char *contents = esdo->this_hdr.contents;
12589 if (contents != NULL)
12590 abort ();
12591 contents
12592 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12593 if (contents == NULL)
12594 goto error_return;
12595 esdo->this_hdr.contents = contents;
12596 }
12597 }
12598
12599 /* We have now assigned file positions for all the sections except .symtab,
12600 .strtab, and non-loaded reloc and compressed debugging sections. We start
12601 the .symtab section at the current file position, and write directly to it.
12602 We build the .strtab section in memory. */
12603 abfd->symcount = 0;
12604 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12605 /* sh_name is set in prep_headers. */
12606 symtab_hdr->sh_type = SHT_SYMTAB;
12607 /* sh_flags, sh_addr and sh_size all start off zero. */
12608 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12609 /* sh_link is set in assign_section_numbers. */
12610 /* sh_info is set below. */
12611 /* sh_offset is set just below. */
12612 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12613
12614 if (max_sym_count < 20)
12615 max_sym_count = 20;
12616 htab->strtabsize = max_sym_count;
12617 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12618 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12619 if (htab->strtab == NULL)
12620 goto error_return;
12621 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12622 flinfo.symshndxbuf
12623 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12624 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12625
12626 if (info->strip != strip_all || emit_relocs)
12627 {
12628 file_ptr off = elf_next_file_pos (abfd);
12629
12630 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12631
12632 /* Note that at this point elf_next_file_pos (abfd) is
12633 incorrect. We do not yet know the size of the .symtab section.
12634 We correct next_file_pos below, after we do know the size. */
12635
12636 /* Start writing out the symbol table. The first symbol is always a
12637 dummy symbol. */
12638 elfsym.st_value = 0;
12639 elfsym.st_size = 0;
12640 elfsym.st_info = 0;
12641 elfsym.st_other = 0;
12642 elfsym.st_shndx = SHN_UNDEF;
12643 elfsym.st_target_internal = 0;
12644 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12645 bfd_und_section_ptr, NULL) != 1)
12646 goto error_return;
12647
12648 /* Output a symbol for each section if asked or they are used for
12649 relocs. These symbols usually have no names. We store the
12650 index of each one in the index field of the section, so that
12651 we can find it again when outputting relocs. */
12652
12653 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12654 {
12655 bool name_local_sections
12656 = (bed->elf_backend_name_local_section_symbols
12657 && bed->elf_backend_name_local_section_symbols (abfd));
12658 const char *name = NULL;
12659
12660 elfsym.st_size = 0;
12661 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12662 elfsym.st_other = 0;
12663 elfsym.st_value = 0;
12664 elfsym.st_target_internal = 0;
12665 for (i = 1; i < elf_numsections (abfd); i++)
12666 {
12667 o = bfd_section_from_elf_index (abfd, i);
12668 if (o != NULL)
12669 {
12670 o->target_index = bfd_get_symcount (abfd);
12671 elfsym.st_shndx = i;
12672 if (!bfd_link_relocatable (info))
12673 elfsym.st_value = o->vma;
12674 if (name_local_sections)
12675 name = o->name;
12676 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12677 NULL) != 1)
12678 goto error_return;
12679 }
12680 }
12681 }
12682 }
12683
12684 /* On some targets like Irix 5 the symbol split between local and global
12685 ones recorded in the sh_info field needs to be done between section
12686 and all other symbols. */
12687 if (bed->elf_backend_elfsym_local_is_section
12688 && bed->elf_backend_elfsym_local_is_section (abfd))
12689 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12690
12691 /* Allocate some memory to hold information read in from the input
12692 files. */
12693 if (max_contents_size != 0)
12694 {
12695 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12696 if (flinfo.contents == NULL)
12697 goto error_return;
12698 }
12699
12700 if (max_external_reloc_size != 0)
12701 {
12702 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12703 if (flinfo.external_relocs == NULL)
12704 goto error_return;
12705 }
12706
12707 if (max_internal_reloc_count != 0)
12708 {
12709 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12710 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12711 if (flinfo.internal_relocs == NULL)
12712 goto error_return;
12713 }
12714
12715 if (max_sym_count != 0)
12716 {
12717 amt = max_sym_count * bed->s->sizeof_sym;
12718 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12719 if (flinfo.external_syms == NULL)
12720 goto error_return;
12721
12722 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12723 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12724 if (flinfo.internal_syms == NULL)
12725 goto error_return;
12726
12727 amt = max_sym_count * sizeof (long);
12728 flinfo.indices = (long int *) bfd_malloc (amt);
12729 if (flinfo.indices == NULL)
12730 goto error_return;
12731
12732 amt = max_sym_count * sizeof (asection *);
12733 flinfo.sections = (asection **) bfd_malloc (amt);
12734 if (flinfo.sections == NULL)
12735 goto error_return;
12736 }
12737
12738 if (max_sym_shndx_count != 0)
12739 {
12740 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12741 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12742 if (flinfo.locsym_shndx == NULL)
12743 goto error_return;
12744 }
12745
12746 if (htab->tls_sec)
12747 {
12748 bfd_vma base, end = 0; /* Both bytes. */
12749 asection *sec;
12750
12751 for (sec = htab->tls_sec;
12752 sec && (sec->flags & SEC_THREAD_LOCAL);
12753 sec = sec->next)
12754 {
12755 bfd_size_type size = sec->size;
12756 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12757
12758 if (size == 0
12759 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12760 {
12761 struct bfd_link_order *ord = sec->map_tail.link_order;
12762
12763 if (ord != NULL)
12764 size = ord->offset * opb + ord->size;
12765 }
12766 end = sec->vma + size / opb;
12767 }
12768 base = htab->tls_sec->vma;
12769 /* Only align end of TLS section if static TLS doesn't have special
12770 alignment requirements. */
12771 if (bed->static_tls_alignment == 1)
12772 end = align_power (end, htab->tls_sec->alignment_power);
12773 htab->tls_size = end - base;
12774 }
12775
12776 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12777 return false;
12778
12779 /* Finish relative relocations here after regular symbol processing
12780 is finished if DT_RELR is enabled. */
12781 if (info->enable_dt_relr
12782 && bed->finish_relative_relocs
12783 && !bed->finish_relative_relocs (info))
12784 info->callbacks->einfo
12785 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12786
12787 /* Since ELF permits relocations to be against local symbols, we
12788 must have the local symbols available when we do the relocations.
12789 Since we would rather only read the local symbols once, and we
12790 would rather not keep them in memory, we handle all the
12791 relocations for a single input file at the same time.
12792
12793 Unfortunately, there is no way to know the total number of local
12794 symbols until we have seen all of them, and the local symbol
12795 indices precede the global symbol indices. This means that when
12796 we are generating relocatable output, and we see a reloc against
12797 a global symbol, we can not know the symbol index until we have
12798 finished examining all the local symbols to see which ones we are
12799 going to output. To deal with this, we keep the relocations in
12800 memory, and don't output them until the end of the link. This is
12801 an unfortunate waste of memory, but I don't see a good way around
12802 it. Fortunately, it only happens when performing a relocatable
12803 link, which is not the common case. FIXME: If keep_memory is set
12804 we could write the relocs out and then read them again; I don't
12805 know how bad the memory loss will be. */
12806
12807 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12808 sub->output_has_begun = false;
12809 for (o = abfd->sections; o != NULL; o = o->next)
12810 {
12811 for (p = o->map_head.link_order; p != NULL; p = p->next)
12812 {
12813 if (p->type == bfd_indirect_link_order
12814 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12815 == bfd_target_elf_flavour)
12816 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12817 {
12818 if (! sub->output_has_begun)
12819 {
12820 if (! elf_link_input_bfd (&flinfo, sub))
12821 goto error_return;
12822 sub->output_has_begun = true;
12823 }
12824 }
12825 else if (p->type == bfd_section_reloc_link_order
12826 || p->type == bfd_symbol_reloc_link_order)
12827 {
12828 if (! elf_reloc_link_order (abfd, info, o, p))
12829 goto error_return;
12830 }
12831 else
12832 {
12833 if (! _bfd_default_link_order (abfd, info, o, p))
12834 {
12835 if (p->type == bfd_indirect_link_order
12836 && (bfd_get_flavour (sub)
12837 == bfd_target_elf_flavour)
12838 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12839 != bed->s->elfclass))
12840 {
12841 const char *iclass, *oclass;
12842
12843 switch (bed->s->elfclass)
12844 {
12845 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12846 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12847 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12848 default: abort ();
12849 }
12850
12851 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12852 {
12853 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12854 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12855 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12856 default: abort ();
12857 }
12858
12859 bfd_set_error (bfd_error_wrong_format);
12860 _bfd_error_handler
12861 /* xgettext:c-format */
12862 (_("%pB: file class %s incompatible with %s"),
12863 sub, iclass, oclass);
12864 }
12865
12866 goto error_return;
12867 }
12868 }
12869 }
12870 }
12871
12872 /* Free symbol buffer if needed. */
12873 if (!info->reduce_memory_overheads)
12874 {
12875 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12876 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12877 {
12878 free (elf_tdata (sub)->symbuf);
12879 elf_tdata (sub)->symbuf = NULL;
12880 }
12881 }
12882
12883 ret = true;
12884
12885 /* Output any global symbols that got converted to local in a
12886 version script or due to symbol visibility. We do this in a
12887 separate step since ELF requires all local symbols to appear
12888 prior to any global symbols. FIXME: We should only do this if
12889 some global symbols were, in fact, converted to become local.
12890 FIXME: Will this work correctly with the Irix 5 linker? */
12891 eoinfo.failed = false;
12892 eoinfo.flinfo = &flinfo;
12893 eoinfo.localsyms = true;
12894 eoinfo.file_sym_done = false;
12895 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12896 if (eoinfo.failed)
12897 {
12898 ret = false;
12899 goto return_local_hash_table;
12900 }
12901
12902 /* If backend needs to output some local symbols not present in the hash
12903 table, do it now. */
12904 if (bed->elf_backend_output_arch_local_syms)
12905 {
12906 if (! ((*bed->elf_backend_output_arch_local_syms)
12907 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12908 {
12909 ret = false;
12910 goto return_local_hash_table;
12911 }
12912 }
12913
12914 /* That wrote out all the local symbols. Finish up the symbol table
12915 with the global symbols. Even if we want to strip everything we
12916 can, we still need to deal with those global symbols that got
12917 converted to local in a version script. */
12918
12919 /* The sh_info field records the index of the first non local symbol. */
12920 if (!symtab_hdr->sh_info)
12921 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12922
12923 if (dynamic
12924 && htab->dynsym != NULL
12925 && htab->dynsym->output_section != bfd_abs_section_ptr)
12926 {
12927 Elf_Internal_Sym sym;
12928 bfd_byte *dynsym = htab->dynsym->contents;
12929
12930 o = htab->dynsym->output_section;
12931 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12932
12933 /* Write out the section symbols for the output sections. */
12934 if (bfd_link_pic (info)
12935 || htab->is_relocatable_executable)
12936 {
12937 asection *s;
12938
12939 sym.st_size = 0;
12940 sym.st_name = 0;
12941 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12942 sym.st_other = 0;
12943 sym.st_target_internal = 0;
12944
12945 for (s = abfd->sections; s != NULL; s = s->next)
12946 {
12947 int indx;
12948 bfd_byte *dest;
12949 long dynindx;
12950
12951 dynindx = elf_section_data (s)->dynindx;
12952 if (dynindx <= 0)
12953 continue;
12954 indx = elf_section_data (s)->this_idx;
12955 BFD_ASSERT (indx > 0);
12956 sym.st_shndx = indx;
12957 if (! check_dynsym (abfd, &sym))
12958 {
12959 ret = false;
12960 goto return_local_hash_table;
12961 }
12962 sym.st_value = s->vma;
12963 dest = dynsym + dynindx * bed->s->sizeof_sym;
12964
12965 /* Inform the linker of the addition of this symbol. */
12966
12967 if (info->callbacks->ctf_new_dynsym)
12968 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12969
12970 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12971 }
12972 }
12973
12974 /* Write out the local dynsyms. */
12975 if (htab->dynlocal)
12976 {
12977 struct elf_link_local_dynamic_entry *e;
12978 for (e = htab->dynlocal; e ; e = e->next)
12979 {
12980 asection *s;
12981 bfd_byte *dest;
12982
12983 /* Copy the internal symbol and turn off visibility.
12984 Note that we saved a word of storage and overwrote
12985 the original st_name with the dynstr_index. */
12986 sym = e->isym;
12987 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12988 sym.st_shndx = SHN_UNDEF;
12989
12990 s = bfd_section_from_elf_index (e->input_bfd,
12991 e->isym.st_shndx);
12992 if (s != NULL
12993 && s->output_section != NULL
12994 && elf_section_data (s->output_section) != NULL)
12995 {
12996 sym.st_shndx =
12997 elf_section_data (s->output_section)->this_idx;
12998 if (! check_dynsym (abfd, &sym))
12999 {
13000 ret = false;
13001 goto return_local_hash_table;
13002 }
13003 sym.st_value = (s->output_section->vma
13004 + s->output_offset
13005 + e->isym.st_value);
13006 }
13007
13008 /* Inform the linker of the addition of this symbol. */
13009
13010 if (info->callbacks->ctf_new_dynsym)
13011 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13012
13013 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13014 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13015 }
13016 }
13017 }
13018
13019 /* We get the global symbols from the hash table. */
13020 eoinfo.failed = false;
13021 eoinfo.localsyms = false;
13022 eoinfo.flinfo = &flinfo;
13023 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13024 if (eoinfo.failed)
13025 {
13026 ret = false;
13027 goto return_local_hash_table;
13028 }
13029
13030 /* If backend needs to output some symbols not present in the hash
13031 table, do it now. */
13032 if (bed->elf_backend_output_arch_syms
13033 && (info->strip != strip_all || emit_relocs))
13034 {
13035 if (! ((*bed->elf_backend_output_arch_syms)
13036 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13037 {
13038 ret = false;
13039 goto return_local_hash_table;
13040 }
13041 }
13042
13043 /* Finalize the .strtab section. */
13044 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13045
13046 /* Swap out the .strtab section. */
13047 if (!elf_link_swap_symbols_out (&flinfo))
13048 {
13049 ret = false;
13050 goto return_local_hash_table;
13051 }
13052
13053 /* Now we know the size of the symtab section. */
13054 if (bfd_get_symcount (abfd) > 0)
13055 {
13056 /* Finish up and write out the symbol string table (.strtab)
13057 section. */
13058 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13059 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13060
13061 if (elf_symtab_shndx_list (abfd))
13062 {
13063 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13064
13065 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13066 {
13067 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13068 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13069 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13070 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13071 symtab_shndx_hdr->sh_size = amt;
13072
13073 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13074 off, true);
13075
13076 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13077 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13078 {
13079 ret = false;
13080 goto return_local_hash_table;
13081 }
13082 }
13083 }
13084
13085 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13086 /* sh_name was set in prep_headers. */
13087 symstrtab_hdr->sh_type = SHT_STRTAB;
13088 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13089 symstrtab_hdr->sh_addr = 0;
13090 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13091 symstrtab_hdr->sh_entsize = 0;
13092 symstrtab_hdr->sh_link = 0;
13093 symstrtab_hdr->sh_info = 0;
13094 /* sh_offset is set just below. */
13095 symstrtab_hdr->sh_addralign = 1;
13096
13097 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13098 off, true);
13099 elf_next_file_pos (abfd) = off;
13100
13101 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13102 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13103 {
13104 ret = false;
13105 goto return_local_hash_table;
13106 }
13107 }
13108
13109 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13110 {
13111 _bfd_error_handler (_("%pB: failed to generate import library"),
13112 info->out_implib_bfd);
13113 ret = false;
13114 goto return_local_hash_table;
13115 }
13116
13117 /* Adjust the relocs to have the correct symbol indices. */
13118 for (o = abfd->sections; o != NULL; o = o->next)
13119 {
13120 struct bfd_elf_section_data *esdo = elf_section_data (o);
13121 bool sort;
13122
13123 if ((o->flags & SEC_RELOC) == 0)
13124 continue;
13125
13126 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13127 if (esdo->rel.hdr != NULL
13128 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13129 {
13130 ret = false;
13131 goto return_local_hash_table;
13132 }
13133 if (esdo->rela.hdr != NULL
13134 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13135 {
13136 ret = false;
13137 goto return_local_hash_table;
13138 }
13139
13140 /* Set the reloc_count field to 0 to prevent write_relocs from
13141 trying to swap the relocs out itself. */
13142 o->reloc_count = 0;
13143 }
13144
13145 relativecount = 0;
13146 if (dynamic && info->combreloc && dynobj != NULL)
13147 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13148
13149 relr_entsize = 0;
13150 if (htab->srelrdyn != NULL
13151 && htab->srelrdyn->output_section != NULL
13152 && htab->srelrdyn->size != 0)
13153 {
13154 asection *s = htab->srelrdyn->output_section;
13155 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13156 if (relr_entsize == 0)
13157 {
13158 relr_entsize = bed->s->arch_size / 8;
13159 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13160 }
13161 }
13162
13163 /* If we are linking against a dynamic object, or generating a
13164 shared library, finish up the dynamic linking information. */
13165 if (dynamic)
13166 {
13167 bfd_byte *dyncon, *dynconend;
13168
13169 /* Fix up .dynamic entries. */
13170 o = bfd_get_linker_section (dynobj, ".dynamic");
13171 BFD_ASSERT (o != NULL);
13172
13173 dyncon = o->contents;
13174 dynconend = PTR_ADD (o->contents, o->size);
13175 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13176 {
13177 Elf_Internal_Dyn dyn;
13178 const char *name;
13179 unsigned int type;
13180 bfd_size_type sh_size;
13181 bfd_vma sh_addr;
13182
13183 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13184
13185 switch (dyn.d_tag)
13186 {
13187 default:
13188 continue;
13189 case DT_NULL:
13190 if (relativecount != 0)
13191 {
13192 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13193 {
13194 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13195 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13196 }
13197 if (dyn.d_tag != DT_NULL
13198 && dynconend - dyncon >= bed->s->sizeof_dyn)
13199 {
13200 dyn.d_un.d_val = relativecount;
13201 relativecount = 0;
13202 break;
13203 }
13204 relativecount = 0;
13205 }
13206 if (relr_entsize != 0)
13207 {
13208 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13209 {
13210 asection *s = htab->srelrdyn;
13211 dyn.d_tag = DT_RELR;
13212 dyn.d_un.d_ptr
13213 = s->output_section->vma + s->output_offset;
13214 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13215 dyncon += bed->s->sizeof_dyn;
13216
13217 dyn.d_tag = DT_RELRSZ;
13218 dyn.d_un.d_val = s->size;
13219 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13220 dyncon += bed->s->sizeof_dyn;
13221
13222 dyn.d_tag = DT_RELRENT;
13223 dyn.d_un.d_val = relr_entsize;
13224 relr_entsize = 0;
13225 break;
13226 }
13227 relr_entsize = 0;
13228 }
13229 continue;
13230
13231 case DT_INIT:
13232 name = info->init_function;
13233 goto get_sym;
13234 case DT_FINI:
13235 name = info->fini_function;
13236 get_sym:
13237 {
13238 struct elf_link_hash_entry *h;
13239
13240 h = elf_link_hash_lookup (htab, name, false, false, true);
13241 if (h != NULL
13242 && (h->root.type == bfd_link_hash_defined
13243 || h->root.type == bfd_link_hash_defweak))
13244 {
13245 dyn.d_un.d_ptr = h->root.u.def.value;
13246 o = h->root.u.def.section;
13247 if (o->output_section != NULL)
13248 dyn.d_un.d_ptr += (o->output_section->vma
13249 + o->output_offset);
13250 else
13251 {
13252 /* The symbol is imported from another shared
13253 library and does not apply to this one. */
13254 dyn.d_un.d_ptr = 0;
13255 }
13256 break;
13257 }
13258 }
13259 continue;
13260
13261 case DT_PREINIT_ARRAYSZ:
13262 name = ".preinit_array";
13263 goto get_out_size;
13264 case DT_INIT_ARRAYSZ:
13265 name = ".init_array";
13266 goto get_out_size;
13267 case DT_FINI_ARRAYSZ:
13268 name = ".fini_array";
13269 get_out_size:
13270 o = bfd_get_section_by_name (abfd, name);
13271 if (o == NULL)
13272 {
13273 _bfd_error_handler
13274 (_("could not find section %s"), name);
13275 goto error_return;
13276 }
13277 if (o->size == 0)
13278 _bfd_error_handler
13279 (_("warning: %s section has zero size"), name);
13280 dyn.d_un.d_val = o->size;
13281 break;
13282
13283 case DT_PREINIT_ARRAY:
13284 name = ".preinit_array";
13285 goto get_out_vma;
13286 case DT_INIT_ARRAY:
13287 name = ".init_array";
13288 goto get_out_vma;
13289 case DT_FINI_ARRAY:
13290 name = ".fini_array";
13291 get_out_vma:
13292 o = bfd_get_section_by_name (abfd, name);
13293 goto do_vma;
13294
13295 case DT_HASH:
13296 name = ".hash";
13297 goto get_vma;
13298 case DT_GNU_HASH:
13299 name = ".gnu.hash";
13300 goto get_vma;
13301 case DT_STRTAB:
13302 name = ".dynstr";
13303 goto get_vma;
13304 case DT_SYMTAB:
13305 name = ".dynsym";
13306 goto get_vma;
13307 case DT_VERDEF:
13308 name = ".gnu.version_d";
13309 goto get_vma;
13310 case DT_VERNEED:
13311 name = ".gnu.version_r";
13312 goto get_vma;
13313 case DT_VERSYM:
13314 name = ".gnu.version";
13315 get_vma:
13316 o = bfd_get_linker_section (dynobj, name);
13317 do_vma:
13318 if (o == NULL || bfd_is_abs_section (o->output_section))
13319 {
13320 _bfd_error_handler
13321 (_("could not find section %s"), name);
13322 goto error_return;
13323 }
13324 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13325 {
13326 _bfd_error_handler
13327 (_("warning: section '%s' is being made into a note"), name);
13328 bfd_set_error (bfd_error_nonrepresentable_section);
13329 goto error_return;
13330 }
13331 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13332 break;
13333
13334 case DT_REL:
13335 case DT_RELA:
13336 case DT_RELSZ:
13337 case DT_RELASZ:
13338 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13339 type = SHT_REL;
13340 else
13341 type = SHT_RELA;
13342 sh_size = 0;
13343 sh_addr = 0;
13344 for (i = 1; i < elf_numsections (abfd); i++)
13345 {
13346 Elf_Internal_Shdr *hdr;
13347
13348 hdr = elf_elfsections (abfd)[i];
13349 if (hdr->sh_type == type
13350 && (hdr->sh_flags & SHF_ALLOC) != 0)
13351 {
13352 sh_size += hdr->sh_size;
13353 if (sh_addr == 0
13354 || sh_addr > hdr->sh_addr)
13355 sh_addr = hdr->sh_addr;
13356 }
13357 }
13358
13359 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13360 {
13361 unsigned int opb = bfd_octets_per_byte (abfd, o);
13362
13363 /* Don't count procedure linkage table relocs in the
13364 overall reloc count. */
13365 sh_size -= htab->srelplt->size;
13366 if (sh_size == 0)
13367 /* If the size is zero, make the address zero too.
13368 This is to avoid a glibc bug. If the backend
13369 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13370 zero, then we'll put DT_RELA at the end of
13371 DT_JMPREL. glibc will interpret the end of
13372 DT_RELA matching the end of DT_JMPREL as the
13373 case where DT_RELA includes DT_JMPREL, and for
13374 LD_BIND_NOW will decide that processing DT_RELA
13375 will process the PLT relocs too. Net result:
13376 No PLT relocs applied. */
13377 sh_addr = 0;
13378
13379 /* If .rela.plt is the first .rela section, exclude
13380 it from DT_RELA. */
13381 else if (sh_addr == (htab->srelplt->output_section->vma
13382 + htab->srelplt->output_offset) * opb)
13383 sh_addr += htab->srelplt->size;
13384 }
13385
13386 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13387 dyn.d_un.d_val = sh_size;
13388 else
13389 dyn.d_un.d_ptr = sh_addr;
13390 break;
13391 }
13392 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13393 }
13394 }
13395
13396 /* If we have created any dynamic sections, then output them. */
13397 if (dynobj != NULL)
13398 {
13399 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13400 goto error_return;
13401
13402 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13403 if (bfd_link_textrel_check (info)
13404 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13405 && o->size != 0)
13406 {
13407 bfd_byte *dyncon, *dynconend;
13408
13409 dyncon = o->contents;
13410 dynconend = o->contents + o->size;
13411 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13412 {
13413 Elf_Internal_Dyn dyn;
13414
13415 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13416
13417 if (dyn.d_tag == DT_TEXTREL)
13418 {
13419 if (info->textrel_check == textrel_check_error)
13420 info->callbacks->einfo
13421 (_("%P%X: read-only segment has dynamic relocations\n"));
13422 else if (bfd_link_dll (info))
13423 info->callbacks->einfo
13424 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13425 else if (bfd_link_pde (info))
13426 info->callbacks->einfo
13427 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13428 else
13429 info->callbacks->einfo
13430 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13431 break;
13432 }
13433 }
13434 }
13435
13436 for (o = dynobj->sections; o != NULL; o = o->next)
13437 {
13438 if ((o->flags & SEC_HAS_CONTENTS) == 0
13439 || o->size == 0
13440 || o->output_section == bfd_abs_section_ptr)
13441 continue;
13442 if ((o->flags & SEC_LINKER_CREATED) == 0)
13443 {
13444 /* At this point, we are only interested in sections
13445 created by _bfd_elf_link_create_dynamic_sections. */
13446 continue;
13447 }
13448 if (htab->stab_info.stabstr == o)
13449 continue;
13450 if (htab->eh_info.hdr_sec == o)
13451 continue;
13452 if (strcmp (o->name, ".dynstr") != 0)
13453 {
13454 bfd_size_type octets = ((file_ptr) o->output_offset
13455 * bfd_octets_per_byte (abfd, o));
13456 if (!bfd_set_section_contents (abfd, o->output_section,
13457 o->contents, octets, o->size))
13458 goto error_return;
13459 }
13460 else
13461 {
13462 /* The contents of the .dynstr section are actually in a
13463 stringtab. */
13464 file_ptr off;
13465
13466 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13467 if (bfd_seek (abfd, off, SEEK_SET) != 0
13468 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13469 goto error_return;
13470 }
13471 }
13472 }
13473
13474 if (!info->resolve_section_groups)
13475 {
13476 bool failed = false;
13477
13478 BFD_ASSERT (bfd_link_relocatable (info));
13479 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13480 if (failed)
13481 goto error_return;
13482 }
13483
13484 /* If we have optimized stabs strings, output them. */
13485 if (htab->stab_info.stabstr != NULL)
13486 {
13487 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13488 goto error_return;
13489 }
13490
13491 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13492 goto error_return;
13493
13494 if (! _bfd_elf_write_section_sframe (abfd, info))
13495 goto error_return;
13496
13497 if (info->callbacks->emit_ctf)
13498 info->callbacks->emit_ctf ();
13499
13500 elf_final_link_free (abfd, &flinfo);
13501
13502 if (attr_section)
13503 {
13504 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13505 if (contents == NULL)
13506 {
13507 /* Bail out and fail. */
13508 ret = false;
13509 goto return_local_hash_table;
13510 }
13511 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13512 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13513 free (contents);
13514 }
13515
13516 return_local_hash_table:
13517 if (info->unique_symbol)
13518 bfd_hash_table_free (&flinfo.local_hash_table);
13519 return ret;
13520
13521 error_return:
13522 elf_final_link_free (abfd, &flinfo);
13523 ret = false;
13524 goto return_local_hash_table;
13525 }
13526 \f
13527 /* Initialize COOKIE for input bfd ABFD. */
13528
13529 static bool
13530 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13531 struct bfd_link_info *info, bfd *abfd)
13532 {
13533 Elf_Internal_Shdr *symtab_hdr;
13534 const struct elf_backend_data *bed;
13535
13536 bed = get_elf_backend_data (abfd);
13537 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13538
13539 cookie->abfd = abfd;
13540 cookie->sym_hashes = elf_sym_hashes (abfd);
13541 cookie->bad_symtab = elf_bad_symtab (abfd);
13542 if (cookie->bad_symtab)
13543 {
13544 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13545 cookie->extsymoff = 0;
13546 }
13547 else
13548 {
13549 cookie->locsymcount = symtab_hdr->sh_info;
13550 cookie->extsymoff = symtab_hdr->sh_info;
13551 }
13552
13553 if (bed->s->arch_size == 32)
13554 cookie->r_sym_shift = 8;
13555 else
13556 cookie->r_sym_shift = 32;
13557
13558 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13559 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13560 {
13561 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13562 cookie->locsymcount, 0,
13563 NULL, NULL, NULL);
13564 if (cookie->locsyms == NULL)
13565 {
13566 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13567 return false;
13568 }
13569 if (_bfd_link_keep_memory (info) )
13570 {
13571 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13572 info->cache_size += (cookie->locsymcount
13573 * sizeof (Elf_External_Sym_Shndx));
13574 }
13575 }
13576 return true;
13577 }
13578
13579 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13580
13581 static void
13582 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13583 {
13584 Elf_Internal_Shdr *symtab_hdr;
13585
13586 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13587 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13588 free (cookie->locsyms);
13589 }
13590
13591 /* Initialize the relocation information in COOKIE for input section SEC
13592 of input bfd ABFD. */
13593
13594 static bool
13595 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13596 struct bfd_link_info *info, bfd *abfd,
13597 asection *sec)
13598 {
13599 if (sec->reloc_count == 0)
13600 {
13601 cookie->rels = NULL;
13602 cookie->relend = NULL;
13603 }
13604 else
13605 {
13606 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13607 NULL, NULL,
13608 _bfd_link_keep_memory (info));
13609 if (cookie->rels == NULL)
13610 return false;
13611 cookie->rel = cookie->rels;
13612 cookie->relend = cookie->rels + sec->reloc_count;
13613 }
13614 cookie->rel = cookie->rels;
13615 return true;
13616 }
13617
13618 /* Free the memory allocated by init_reloc_cookie_rels,
13619 if appropriate. */
13620
13621 static void
13622 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13623 asection *sec)
13624 {
13625 if (elf_section_data (sec)->relocs != cookie->rels)
13626 free (cookie->rels);
13627 }
13628
13629 /* Initialize the whole of COOKIE for input section SEC. */
13630
13631 static bool
13632 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13633 struct bfd_link_info *info,
13634 asection *sec)
13635 {
13636 if (!init_reloc_cookie (cookie, info, sec->owner))
13637 goto error1;
13638 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13639 goto error2;
13640 return true;
13641
13642 error2:
13643 fini_reloc_cookie (cookie, sec->owner);
13644 error1:
13645 return false;
13646 }
13647
13648 /* Free the memory allocated by init_reloc_cookie_for_section,
13649 if appropriate. */
13650
13651 static void
13652 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13653 asection *sec)
13654 {
13655 fini_reloc_cookie_rels (cookie, sec);
13656 fini_reloc_cookie (cookie, sec->owner);
13657 }
13658 \f
13659 /* Garbage collect unused sections. */
13660
13661 /* Default gc_mark_hook. */
13662
13663 asection *
13664 _bfd_elf_gc_mark_hook (asection *sec,
13665 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13666 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13667 struct elf_link_hash_entry *h,
13668 Elf_Internal_Sym *sym)
13669 {
13670 if (h != NULL)
13671 {
13672 switch (h->root.type)
13673 {
13674 case bfd_link_hash_defined:
13675 case bfd_link_hash_defweak:
13676 return h->root.u.def.section;
13677
13678 case bfd_link_hash_common:
13679 return h->root.u.c.p->section;
13680
13681 default:
13682 break;
13683 }
13684 }
13685 else
13686 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13687
13688 return NULL;
13689 }
13690
13691 /* Return the debug definition section. */
13692
13693 static asection *
13694 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13695 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13696 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13697 struct elf_link_hash_entry *h,
13698 Elf_Internal_Sym *sym)
13699 {
13700 if (h != NULL)
13701 {
13702 /* Return the global debug definition section. */
13703 if ((h->root.type == bfd_link_hash_defined
13704 || h->root.type == bfd_link_hash_defweak)
13705 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13706 return h->root.u.def.section;
13707 }
13708 else
13709 {
13710 /* Return the local debug definition section. */
13711 asection *isec = bfd_section_from_elf_index (sec->owner,
13712 sym->st_shndx);
13713 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13714 return isec;
13715 }
13716
13717 return NULL;
13718 }
13719
13720 /* COOKIE->rel describes a relocation against section SEC, which is
13721 a section we've decided to keep. Return the section that contains
13722 the relocation symbol, or NULL if no section contains it. */
13723
13724 asection *
13725 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13726 elf_gc_mark_hook_fn gc_mark_hook,
13727 struct elf_reloc_cookie *cookie,
13728 bool *start_stop)
13729 {
13730 unsigned long r_symndx;
13731 struct elf_link_hash_entry *h, *hw;
13732
13733 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13734 if (r_symndx == STN_UNDEF)
13735 return NULL;
13736
13737 if (r_symndx >= cookie->locsymcount
13738 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13739 {
13740 bool was_marked;
13741
13742 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13743 if (h == NULL)
13744 {
13745 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13746 sec->owner);
13747 return NULL;
13748 }
13749 while (h->root.type == bfd_link_hash_indirect
13750 || h->root.type == bfd_link_hash_warning)
13751 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13752
13753 was_marked = h->mark;
13754 h->mark = 1;
13755 /* Keep all aliases of the symbol too. If an object symbol
13756 needs to be copied into .dynbss then all of its aliases
13757 should be present as dynamic symbols, not just the one used
13758 on the copy relocation. */
13759 hw = h;
13760 while (hw->is_weakalias)
13761 {
13762 hw = hw->u.alias;
13763 hw->mark = 1;
13764 }
13765
13766 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13767 {
13768 if (info->start_stop_gc)
13769 return NULL;
13770
13771 /* To work around a glibc bug, mark XXX input sections
13772 when there is a reference to __start_XXX or __stop_XXX
13773 symbols. */
13774 else if (start_stop != NULL)
13775 {
13776 asection *s = h->u2.start_stop_section;
13777 *start_stop = true;
13778 return s;
13779 }
13780 }
13781
13782 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13783 }
13784
13785 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13786 &cookie->locsyms[r_symndx]);
13787 }
13788
13789 /* COOKIE->rel describes a relocation against section SEC, which is
13790 a section we've decided to keep. Mark the section that contains
13791 the relocation symbol. */
13792
13793 bool
13794 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13795 asection *sec,
13796 elf_gc_mark_hook_fn gc_mark_hook,
13797 struct elf_reloc_cookie *cookie)
13798 {
13799 asection *rsec;
13800 bool start_stop = false;
13801
13802 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13803 while (rsec != NULL)
13804 {
13805 if (!rsec->gc_mark)
13806 {
13807 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13808 || (rsec->owner->flags & DYNAMIC) != 0)
13809 rsec->gc_mark = 1;
13810 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13811 return false;
13812 }
13813 if (!start_stop)
13814 break;
13815 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13816 }
13817 return true;
13818 }
13819
13820 /* The mark phase of garbage collection. For a given section, mark
13821 it and any sections in this section's group, and all the sections
13822 which define symbols to which it refers. */
13823
13824 bool
13825 _bfd_elf_gc_mark (struct bfd_link_info *info,
13826 asection *sec,
13827 elf_gc_mark_hook_fn gc_mark_hook)
13828 {
13829 bool ret;
13830 asection *group_sec, *eh_frame;
13831
13832 sec->gc_mark = 1;
13833
13834 /* Mark all the sections in the group. */
13835 group_sec = elf_section_data (sec)->next_in_group;
13836 if (group_sec && !group_sec->gc_mark)
13837 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13838 return false;
13839
13840 /* Look through the section relocs. */
13841 ret = true;
13842 eh_frame = elf_eh_frame_section (sec->owner);
13843 if ((sec->flags & SEC_RELOC) != 0
13844 && sec->reloc_count > 0
13845 && sec != eh_frame)
13846 {
13847 struct elf_reloc_cookie cookie;
13848
13849 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13850 ret = false;
13851 else
13852 {
13853 for (; cookie.rel < cookie.relend; cookie.rel++)
13854 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13855 {
13856 ret = false;
13857 break;
13858 }
13859 fini_reloc_cookie_for_section (&cookie, sec);
13860 }
13861 }
13862
13863 if (ret && eh_frame && elf_fde_list (sec))
13864 {
13865 struct elf_reloc_cookie cookie;
13866
13867 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13868 ret = false;
13869 else
13870 {
13871 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13872 gc_mark_hook, &cookie))
13873 ret = false;
13874 fini_reloc_cookie_for_section (&cookie, eh_frame);
13875 }
13876 }
13877
13878 eh_frame = elf_section_eh_frame_entry (sec);
13879 if (ret && eh_frame && !eh_frame->gc_mark)
13880 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13881 ret = false;
13882
13883 return ret;
13884 }
13885
13886 /* Scan and mark sections in a special or debug section group. */
13887
13888 static void
13889 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13890 {
13891 /* Point to first section of section group. */
13892 asection *ssec;
13893 /* Used to iterate the section group. */
13894 asection *msec;
13895
13896 bool is_special_grp = true;
13897 bool is_debug_grp = true;
13898
13899 /* First scan to see if group contains any section other than debug
13900 and special section. */
13901 ssec = msec = elf_next_in_group (grp);
13902 do
13903 {
13904 if ((msec->flags & SEC_DEBUGGING) == 0)
13905 is_debug_grp = false;
13906
13907 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13908 is_special_grp = false;
13909
13910 msec = elf_next_in_group (msec);
13911 }
13912 while (msec != ssec);
13913
13914 /* If this is a pure debug section group or pure special section group,
13915 keep all sections in this group. */
13916 if (is_debug_grp || is_special_grp)
13917 {
13918 do
13919 {
13920 msec->gc_mark = 1;
13921 msec = elf_next_in_group (msec);
13922 }
13923 while (msec != ssec);
13924 }
13925 }
13926
13927 /* Keep debug and special sections. */
13928
13929 bool
13930 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13931 elf_gc_mark_hook_fn mark_hook)
13932 {
13933 bfd *ibfd;
13934
13935 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13936 {
13937 asection *isec;
13938 bool some_kept;
13939 bool debug_frag_seen;
13940 bool has_kept_debug_info;
13941
13942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13943 continue;
13944 isec = ibfd->sections;
13945 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13946 continue;
13947
13948 /* Ensure all linker created sections are kept,
13949 see if any other section is already marked,
13950 and note if we have any fragmented debug sections. */
13951 debug_frag_seen = some_kept = has_kept_debug_info = false;
13952 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13953 {
13954 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13955 isec->gc_mark = 1;
13956 else if (isec->gc_mark
13957 && (isec->flags & SEC_ALLOC) != 0
13958 && elf_section_type (isec) != SHT_NOTE)
13959 some_kept = true;
13960 else
13961 {
13962 /* Since all sections, except for backend specific ones,
13963 have been garbage collected, call mark_hook on this
13964 section if any of its linked-to sections is marked. */
13965 asection *linked_to_sec;
13966 for (linked_to_sec = elf_linked_to_section (isec);
13967 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13968 linked_to_sec = elf_linked_to_section (linked_to_sec))
13969 {
13970 if (linked_to_sec->gc_mark)
13971 {
13972 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13973 return false;
13974 break;
13975 }
13976 linked_to_sec->linker_mark = 1;
13977 }
13978 for (linked_to_sec = elf_linked_to_section (isec);
13979 linked_to_sec != NULL && linked_to_sec->linker_mark;
13980 linked_to_sec = elf_linked_to_section (linked_to_sec))
13981 linked_to_sec->linker_mark = 0;
13982 }
13983
13984 if (!debug_frag_seen
13985 && (isec->flags & SEC_DEBUGGING)
13986 && startswith (isec->name, ".debug_line."))
13987 debug_frag_seen = true;
13988 else if (strcmp (bfd_section_name (isec),
13989 "__patchable_function_entries") == 0
13990 && elf_linked_to_section (isec) == NULL)
13991 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13992 "need linked-to section "
13993 "for --gc-sections\n"),
13994 isec->owner, isec);
13995 }
13996
13997 /* If no non-note alloc section in this file will be kept, then
13998 we can toss out the debug and special sections. */
13999 if (!some_kept)
14000 continue;
14001
14002 /* Keep debug and special sections like .comment when they are
14003 not part of a group. Also keep section groups that contain
14004 just debug sections or special sections. NB: Sections with
14005 linked-to section has been handled above. */
14006 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14007 {
14008 if ((isec->flags & SEC_GROUP) != 0)
14009 _bfd_elf_gc_mark_debug_special_section_group (isec);
14010 else if (((isec->flags & SEC_DEBUGGING) != 0
14011 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14012 && elf_next_in_group (isec) == NULL
14013 && elf_linked_to_section (isec) == NULL)
14014 isec->gc_mark = 1;
14015 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14016 has_kept_debug_info = true;
14017 }
14018
14019 /* Look for CODE sections which are going to be discarded,
14020 and find and discard any fragmented debug sections which
14021 are associated with that code section. */
14022 if (debug_frag_seen)
14023 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14024 if ((isec->flags & SEC_CODE) != 0
14025 && isec->gc_mark == 0)
14026 {
14027 unsigned int ilen;
14028 asection *dsec;
14029
14030 ilen = strlen (isec->name);
14031
14032 /* Association is determined by the name of the debug
14033 section containing the name of the code section as
14034 a suffix. For example .debug_line.text.foo is a
14035 debug section associated with .text.foo. */
14036 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14037 {
14038 unsigned int dlen;
14039
14040 if (dsec->gc_mark == 0
14041 || (dsec->flags & SEC_DEBUGGING) == 0)
14042 continue;
14043
14044 dlen = strlen (dsec->name);
14045
14046 if (dlen > ilen
14047 && strncmp (dsec->name + (dlen - ilen),
14048 isec->name, ilen) == 0)
14049 dsec->gc_mark = 0;
14050 }
14051 }
14052
14053 /* Mark debug sections referenced by kept debug sections. */
14054 if (has_kept_debug_info)
14055 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14056 if (isec->gc_mark
14057 && (isec->flags & SEC_DEBUGGING) != 0)
14058 if (!_bfd_elf_gc_mark (info, isec,
14059 elf_gc_mark_debug_section))
14060 return false;
14061 }
14062 return true;
14063 }
14064
14065 static bool
14066 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14067 {
14068 bfd *sub;
14069 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14070
14071 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14072 {
14073 asection *o;
14074
14075 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14076 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14077 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14078 continue;
14079 o = sub->sections;
14080 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14081 continue;
14082
14083 for (o = sub->sections; o != NULL; o = o->next)
14084 {
14085 /* When any section in a section group is kept, we keep all
14086 sections in the section group. If the first member of
14087 the section group is excluded, we will also exclude the
14088 group section. */
14089 if (o->flags & SEC_GROUP)
14090 {
14091 asection *first = elf_next_in_group (o);
14092 o->gc_mark = first->gc_mark;
14093 }
14094
14095 if (o->gc_mark)
14096 continue;
14097
14098 /* Skip sweeping sections already excluded. */
14099 if (o->flags & SEC_EXCLUDE)
14100 continue;
14101
14102 /* Since this is early in the link process, it is simple
14103 to remove a section from the output. */
14104 o->flags |= SEC_EXCLUDE;
14105
14106 if (info->print_gc_sections && o->size != 0)
14107 /* xgettext:c-format */
14108 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14109 o, sub);
14110 }
14111 }
14112
14113 return true;
14114 }
14115
14116 /* Propagate collected vtable information. This is called through
14117 elf_link_hash_traverse. */
14118
14119 static bool
14120 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14121 {
14122 /* Those that are not vtables. */
14123 if (h->start_stop
14124 || h->u2.vtable == NULL
14125 || h->u2.vtable->parent == NULL)
14126 return true;
14127
14128 /* Those vtables that do not have parents, we cannot merge. */
14129 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14130 return true;
14131
14132 /* If we've already been done, exit. */
14133 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14134 return true;
14135
14136 /* Make sure the parent's table is up to date. */
14137 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14138
14139 if (h->u2.vtable->used == NULL)
14140 {
14141 /* None of this table's entries were referenced. Re-use the
14142 parent's table. */
14143 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14144 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14145 }
14146 else
14147 {
14148 size_t n;
14149 bool *cu, *pu;
14150
14151 /* Or the parent's entries into ours. */
14152 cu = h->u2.vtable->used;
14153 cu[-1] = true;
14154 pu = h->u2.vtable->parent->u2.vtable->used;
14155 if (pu != NULL)
14156 {
14157 const struct elf_backend_data *bed;
14158 unsigned int log_file_align;
14159
14160 bed = get_elf_backend_data (h->root.u.def.section->owner);
14161 log_file_align = bed->s->log_file_align;
14162 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14163 while (n--)
14164 {
14165 if (*pu)
14166 *cu = true;
14167 pu++;
14168 cu++;
14169 }
14170 }
14171 }
14172
14173 return true;
14174 }
14175
14176 struct link_info_ok
14177 {
14178 struct bfd_link_info *info;
14179 bool ok;
14180 };
14181
14182 static bool
14183 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14184 void *ptr)
14185 {
14186 asection *sec;
14187 bfd_vma hstart, hend;
14188 Elf_Internal_Rela *relstart, *relend, *rel;
14189 const struct elf_backend_data *bed;
14190 unsigned int log_file_align;
14191 struct link_info_ok *info = (struct link_info_ok *) ptr;
14192
14193 /* Take care of both those symbols that do not describe vtables as
14194 well as those that are not loaded. */
14195 if (h->start_stop
14196 || h->u2.vtable == NULL
14197 || h->u2.vtable->parent == NULL)
14198 return true;
14199
14200 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14201 || h->root.type == bfd_link_hash_defweak);
14202
14203 sec = h->root.u.def.section;
14204 hstart = h->root.u.def.value;
14205 hend = hstart + h->size;
14206
14207 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14208 sec, NULL, NULL, true);
14209 if (!relstart)
14210 return info->ok = false;
14211 bed = get_elf_backend_data (sec->owner);
14212 log_file_align = bed->s->log_file_align;
14213
14214 relend = relstart + sec->reloc_count;
14215
14216 for (rel = relstart; rel < relend; ++rel)
14217 if (rel->r_offset >= hstart && rel->r_offset < hend)
14218 {
14219 /* If the entry is in use, do nothing. */
14220 if (h->u2.vtable->used
14221 && (rel->r_offset - hstart) < h->u2.vtable->size)
14222 {
14223 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14224 if (h->u2.vtable->used[entry])
14225 continue;
14226 }
14227 /* Otherwise, kill it. */
14228 rel->r_offset = rel->r_info = rel->r_addend = 0;
14229 }
14230
14231 return true;
14232 }
14233
14234 /* Mark sections containing dynamically referenced symbols. When
14235 building shared libraries, we must assume that any visible symbol is
14236 referenced. */
14237
14238 bool
14239 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14240 {
14241 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14242 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14243
14244 if ((h->root.type == bfd_link_hash_defined
14245 || h->root.type == bfd_link_hash_defweak)
14246 && (!h->start_stop
14247 || h->root.ldscript_def
14248 || !info->start_stop_gc)
14249 && ((h->ref_dynamic && !h->forced_local)
14250 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14251 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14252 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14253 && (!bfd_link_executable (info)
14254 || info->gc_keep_exported
14255 || info->export_dynamic
14256 || (h->dynamic
14257 && d != NULL
14258 && (*d->match) (&d->head, NULL, h->root.root.string)))
14259 && (h->versioned >= versioned
14260 || !bfd_hide_sym_by_version (info->version_info,
14261 h->root.root.string)))))
14262 h->root.u.def.section->flags |= SEC_KEEP;
14263
14264 return true;
14265 }
14266
14267 /* Keep all sections containing symbols undefined on the command-line,
14268 and the section containing the entry symbol. */
14269
14270 void
14271 _bfd_elf_gc_keep (struct bfd_link_info *info)
14272 {
14273 struct bfd_sym_chain *sym;
14274
14275 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14276 {
14277 struct elf_link_hash_entry *h;
14278
14279 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14280 false, false, false);
14281
14282 if (h != NULL
14283 && (h->root.type == bfd_link_hash_defined
14284 || h->root.type == bfd_link_hash_defweak)
14285 && !bfd_is_const_section (h->root.u.def.section))
14286 h->root.u.def.section->flags |= SEC_KEEP;
14287 }
14288 }
14289
14290 bool
14291 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14292 struct bfd_link_info *info)
14293 {
14294 bfd *ibfd = info->input_bfds;
14295
14296 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14297 {
14298 asection *sec;
14299 struct elf_reloc_cookie cookie;
14300
14301 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14302 continue;
14303 sec = ibfd->sections;
14304 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14305 continue;
14306
14307 if (!init_reloc_cookie (&cookie, info, ibfd))
14308 return false;
14309
14310 for (sec = ibfd->sections; sec; sec = sec->next)
14311 {
14312 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14313 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14314 {
14315 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14316 fini_reloc_cookie_rels (&cookie, sec);
14317 }
14318 }
14319 }
14320 return true;
14321 }
14322
14323 /* Do mark and sweep of unused sections. */
14324
14325 bool
14326 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14327 {
14328 bool ok = true;
14329 bfd *sub;
14330 elf_gc_mark_hook_fn gc_mark_hook;
14331 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14332 struct elf_link_hash_table *htab;
14333 struct link_info_ok info_ok;
14334
14335 if (!bed->can_gc_sections
14336 || !is_elf_hash_table (info->hash))
14337 {
14338 _bfd_error_handler(_("warning: gc-sections option ignored"));
14339 return true;
14340 }
14341
14342 bed->gc_keep (info);
14343 htab = elf_hash_table (info);
14344
14345 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14346 at the .eh_frame section if we can mark the FDEs individually. */
14347 for (sub = info->input_bfds;
14348 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14349 sub = sub->link.next)
14350 {
14351 asection *sec;
14352 struct elf_reloc_cookie cookie;
14353
14354 sec = sub->sections;
14355 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14356 continue;
14357 sec = bfd_get_section_by_name (sub, ".eh_frame");
14358 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14359 {
14360 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14361 if (elf_section_data (sec)->sec_info
14362 && (sec->flags & SEC_LINKER_CREATED) == 0)
14363 elf_eh_frame_section (sub) = sec;
14364 fini_reloc_cookie_for_section (&cookie, sec);
14365 sec = bfd_get_next_section_by_name (NULL, sec);
14366 }
14367 }
14368
14369 /* Apply transitive closure to the vtable entry usage info. */
14370 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14371 if (!ok)
14372 return false;
14373
14374 /* Kill the vtable relocations that were not used. */
14375 info_ok.info = info;
14376 info_ok.ok = true;
14377 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14378 if (!info_ok.ok)
14379 return false;
14380
14381 /* Mark dynamically referenced symbols. */
14382 if (htab->dynamic_sections_created || info->gc_keep_exported)
14383 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14384
14385 /* Grovel through relocs to find out who stays ... */
14386 gc_mark_hook = bed->gc_mark_hook;
14387 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14388 {
14389 asection *o;
14390
14391 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14392 || elf_object_id (sub) != elf_hash_table_id (htab)
14393 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14394 continue;
14395
14396 o = sub->sections;
14397 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14398 continue;
14399
14400 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14401 Also treat note sections as a root, if the section is not part
14402 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14403 well as FINI_ARRAY sections for ld -r. */
14404 for (o = sub->sections; o != NULL; o = o->next)
14405 if (!o->gc_mark
14406 && (o->flags & SEC_EXCLUDE) == 0
14407 && ((o->flags & SEC_KEEP) != 0
14408 || (bfd_link_relocatable (info)
14409 && ((elf_section_data (o)->this_hdr.sh_type
14410 == SHT_PREINIT_ARRAY)
14411 || (elf_section_data (o)->this_hdr.sh_type
14412 == SHT_INIT_ARRAY)
14413 || (elf_section_data (o)->this_hdr.sh_type
14414 == SHT_FINI_ARRAY)))
14415 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14416 && elf_next_in_group (o) == NULL
14417 && elf_linked_to_section (o) == NULL)
14418 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14419 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14420 {
14421 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14422 return false;
14423 }
14424 }
14425
14426 /* Allow the backend to mark additional target specific sections. */
14427 bed->gc_mark_extra_sections (info, gc_mark_hook);
14428
14429 /* ... and mark SEC_EXCLUDE for those that go. */
14430 return elf_gc_sweep (abfd, info);
14431 }
14432 \f
14433 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14434
14435 bool
14436 bfd_elf_gc_record_vtinherit (bfd *abfd,
14437 asection *sec,
14438 struct elf_link_hash_entry *h,
14439 bfd_vma offset)
14440 {
14441 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14442 struct elf_link_hash_entry **search, *child;
14443 size_t extsymcount;
14444 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14445
14446 /* The sh_info field of the symtab header tells us where the
14447 external symbols start. We don't care about the local symbols at
14448 this point. */
14449 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14450 if (!elf_bad_symtab (abfd))
14451 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14452
14453 sym_hashes = elf_sym_hashes (abfd);
14454 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14455
14456 /* Hunt down the child symbol, which is in this section at the same
14457 offset as the relocation. */
14458 for (search = sym_hashes; search != sym_hashes_end; ++search)
14459 {
14460 if ((child = *search) != NULL
14461 && (child->root.type == bfd_link_hash_defined
14462 || child->root.type == bfd_link_hash_defweak)
14463 && child->root.u.def.section == sec
14464 && child->root.u.def.value == offset)
14465 goto win;
14466 }
14467
14468 /* xgettext:c-format */
14469 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14470 abfd, sec, (uint64_t) offset);
14471 bfd_set_error (bfd_error_invalid_operation);
14472 return false;
14473
14474 win:
14475 if (!child->u2.vtable)
14476 {
14477 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14478 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14479 if (!child->u2.vtable)
14480 return false;
14481 }
14482 if (!h)
14483 {
14484 /* This *should* only be the absolute section. It could potentially
14485 be that someone has defined a non-global vtable though, which
14486 would be bad. It isn't worth paging in the local symbols to be
14487 sure though; that case should simply be handled by the assembler. */
14488
14489 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14490 }
14491 else
14492 child->u2.vtable->parent = h;
14493
14494 return true;
14495 }
14496
14497 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14498
14499 bool
14500 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14501 struct elf_link_hash_entry *h,
14502 bfd_vma addend)
14503 {
14504 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14505 unsigned int log_file_align = bed->s->log_file_align;
14506
14507 if (!h)
14508 {
14509 /* xgettext:c-format */
14510 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14511 abfd, sec);
14512 bfd_set_error (bfd_error_bad_value);
14513 return false;
14514 }
14515
14516 if (!h->u2.vtable)
14517 {
14518 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14519 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14520 if (!h->u2.vtable)
14521 return false;
14522 }
14523
14524 if (addend >= h->u2.vtable->size)
14525 {
14526 size_t size, bytes, file_align;
14527 bool *ptr = h->u2.vtable->used;
14528
14529 /* While the symbol is undefined, we have to be prepared to handle
14530 a zero size. */
14531 file_align = 1 << log_file_align;
14532 if (h->root.type == bfd_link_hash_undefined)
14533 size = addend + file_align;
14534 else
14535 {
14536 size = h->size;
14537 if (addend >= size)
14538 {
14539 /* Oops! We've got a reference past the defined end of
14540 the table. This is probably a bug -- shall we warn? */
14541 size = addend + file_align;
14542 }
14543 }
14544 size = (size + file_align - 1) & -file_align;
14545
14546 /* Allocate one extra entry for use as a "done" flag for the
14547 consolidation pass. */
14548 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14549
14550 if (ptr)
14551 {
14552 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14553
14554 if (ptr != NULL)
14555 {
14556 size_t oldbytes;
14557
14558 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14559 * sizeof (bool));
14560 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14561 }
14562 }
14563 else
14564 ptr = (bool *) bfd_zmalloc (bytes);
14565
14566 if (ptr == NULL)
14567 return false;
14568
14569 /* And arrange for that done flag to be at index -1. */
14570 h->u2.vtable->used = ptr + 1;
14571 h->u2.vtable->size = size;
14572 }
14573
14574 h->u2.vtable->used[addend >> log_file_align] = true;
14575
14576 return true;
14577 }
14578
14579 /* Map an ELF section header flag to its corresponding string. */
14580 typedef struct
14581 {
14582 char *flag_name;
14583 flagword flag_value;
14584 } elf_flags_to_name_table;
14585
14586 static const elf_flags_to_name_table elf_flags_to_names [] =
14587 {
14588 { "SHF_WRITE", SHF_WRITE },
14589 { "SHF_ALLOC", SHF_ALLOC },
14590 { "SHF_EXECINSTR", SHF_EXECINSTR },
14591 { "SHF_MERGE", SHF_MERGE },
14592 { "SHF_STRINGS", SHF_STRINGS },
14593 { "SHF_INFO_LINK", SHF_INFO_LINK},
14594 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14595 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14596 { "SHF_GROUP", SHF_GROUP },
14597 { "SHF_TLS", SHF_TLS },
14598 { "SHF_MASKOS", SHF_MASKOS },
14599 { "SHF_EXCLUDE", SHF_EXCLUDE },
14600 };
14601
14602 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14603 bool
14604 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14605 struct flag_info *flaginfo,
14606 asection *section)
14607 {
14608 const bfd_vma sh_flags = elf_section_flags (section);
14609
14610 if (!flaginfo->flags_initialized)
14611 {
14612 bfd *obfd = info->output_bfd;
14613 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14614 struct flag_info_list *tf = flaginfo->flag_list;
14615 int with_hex = 0;
14616 int without_hex = 0;
14617
14618 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14619 {
14620 unsigned i;
14621 flagword (*lookup) (char *);
14622
14623 lookup = bed->elf_backend_lookup_section_flags_hook;
14624 if (lookup != NULL)
14625 {
14626 flagword hexval = (*lookup) ((char *) tf->name);
14627
14628 if (hexval != 0)
14629 {
14630 if (tf->with == with_flags)
14631 with_hex |= hexval;
14632 else if (tf->with == without_flags)
14633 without_hex |= hexval;
14634 tf->valid = true;
14635 continue;
14636 }
14637 }
14638 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14639 {
14640 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14641 {
14642 if (tf->with == with_flags)
14643 with_hex |= elf_flags_to_names[i].flag_value;
14644 else if (tf->with == without_flags)
14645 without_hex |= elf_flags_to_names[i].flag_value;
14646 tf->valid = true;
14647 break;
14648 }
14649 }
14650 if (!tf->valid)
14651 {
14652 info->callbacks->einfo
14653 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14654 return false;
14655 }
14656 }
14657 flaginfo->flags_initialized = true;
14658 flaginfo->only_with_flags |= with_hex;
14659 flaginfo->not_with_flags |= without_hex;
14660 }
14661
14662 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14663 return false;
14664
14665 if ((flaginfo->not_with_flags & sh_flags) != 0)
14666 return false;
14667
14668 return true;
14669 }
14670
14671 struct alloc_got_off_arg {
14672 bfd_vma gotoff;
14673 struct bfd_link_info *info;
14674 };
14675
14676 /* We need a special top-level link routine to convert got reference counts
14677 to real got offsets. */
14678
14679 static bool
14680 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14681 {
14682 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14683 bfd *obfd = gofarg->info->output_bfd;
14684 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14685
14686 if (h->got.refcount > 0)
14687 {
14688 h->got.offset = gofarg->gotoff;
14689 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14690 }
14691 else
14692 h->got.offset = (bfd_vma) -1;
14693
14694 return true;
14695 }
14696
14697 /* And an accompanying bit to work out final got entry offsets once
14698 we're done. Should be called from final_link. */
14699
14700 bool
14701 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14702 struct bfd_link_info *info)
14703 {
14704 bfd *i;
14705 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14706 bfd_vma gotoff;
14707 struct alloc_got_off_arg gofarg;
14708
14709 BFD_ASSERT (abfd == info->output_bfd);
14710
14711 if (! is_elf_hash_table (info->hash))
14712 return false;
14713
14714 /* The GOT offset is relative to the .got section, but the GOT header is
14715 put into the .got.plt section, if the backend uses it. */
14716 if (bed->want_got_plt)
14717 gotoff = 0;
14718 else
14719 gotoff = bed->got_header_size;
14720
14721 /* Do the local .got entries first. */
14722 for (i = info->input_bfds; i; i = i->link.next)
14723 {
14724 bfd_signed_vma *local_got;
14725 size_t j, locsymcount;
14726 Elf_Internal_Shdr *symtab_hdr;
14727
14728 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14729 continue;
14730
14731 local_got = elf_local_got_refcounts (i);
14732 if (!local_got)
14733 continue;
14734
14735 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14736 if (elf_bad_symtab (i))
14737 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14738 else
14739 locsymcount = symtab_hdr->sh_info;
14740
14741 for (j = 0; j < locsymcount; ++j)
14742 {
14743 if (local_got[j] > 0)
14744 {
14745 local_got[j] = gotoff;
14746 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14747 }
14748 else
14749 local_got[j] = (bfd_vma) -1;
14750 }
14751 }
14752
14753 /* Then the global .got entries. .plt refcounts are handled by
14754 adjust_dynamic_symbol */
14755 gofarg.gotoff = gotoff;
14756 gofarg.info = info;
14757 elf_link_hash_traverse (elf_hash_table (info),
14758 elf_gc_allocate_got_offsets,
14759 &gofarg);
14760 return true;
14761 }
14762
14763 /* Many folk need no more in the way of final link than this, once
14764 got entry reference counting is enabled. */
14765
14766 bool
14767 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14768 {
14769 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14770 return false;
14771
14772 /* Invoke the regular ELF backend linker to do all the work. */
14773 return bfd_elf_final_link (abfd, info);
14774 }
14775
14776 bool
14777 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14778 {
14779 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14780
14781 if (rcookie->bad_symtab)
14782 rcookie->rel = rcookie->rels;
14783
14784 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14785 {
14786 unsigned long r_symndx;
14787
14788 if (! rcookie->bad_symtab)
14789 if (rcookie->rel->r_offset > offset)
14790 return false;
14791 if (rcookie->rel->r_offset != offset)
14792 continue;
14793
14794 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14795 if (r_symndx == STN_UNDEF)
14796 return true;
14797
14798 if (r_symndx >= rcookie->locsymcount
14799 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14800 {
14801 struct elf_link_hash_entry *h;
14802
14803 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14804
14805 while (h->root.type == bfd_link_hash_indirect
14806 || h->root.type == bfd_link_hash_warning)
14807 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14808
14809 if ((h->root.type == bfd_link_hash_defined
14810 || h->root.type == bfd_link_hash_defweak)
14811 && (h->root.u.def.section->owner != rcookie->abfd
14812 || h->root.u.def.section->kept_section != NULL
14813 || discarded_section (h->root.u.def.section)))
14814 return true;
14815 }
14816 else
14817 {
14818 /* It's not a relocation against a global symbol,
14819 but it could be a relocation against a local
14820 symbol for a discarded section. */
14821 asection *isec;
14822 Elf_Internal_Sym *isym;
14823
14824 /* Need to: get the symbol; get the section. */
14825 isym = &rcookie->locsyms[r_symndx];
14826 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14827 if (isec != NULL
14828 && (isec->kept_section != NULL
14829 || discarded_section (isec)))
14830 return true;
14831 }
14832 return false;
14833 }
14834 return false;
14835 }
14836
14837 /* Discard unneeded references to discarded sections.
14838 Returns -1 on error, 1 if any section's size was changed, 0 if
14839 nothing changed. This function assumes that the relocations are in
14840 sorted order, which is true for all known assemblers. */
14841
14842 int
14843 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14844 {
14845 struct elf_reloc_cookie cookie;
14846 asection *o;
14847 bfd *abfd;
14848 int changed = 0;
14849
14850 if (info->traditional_format
14851 || !is_elf_hash_table (info->hash))
14852 return 0;
14853
14854 o = bfd_get_section_by_name (output_bfd, ".stab");
14855 if (o != NULL)
14856 {
14857 asection *i;
14858
14859 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14860 {
14861 if (i->size == 0
14862 || i->reloc_count == 0
14863 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14864 continue;
14865
14866 abfd = i->owner;
14867 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14868 continue;
14869
14870 if (!init_reloc_cookie_for_section (&cookie, info, i))
14871 return -1;
14872
14873 if (_bfd_discard_section_stabs (abfd, i,
14874 elf_section_data (i)->sec_info,
14875 bfd_elf_reloc_symbol_deleted_p,
14876 &cookie))
14877 changed = 1;
14878
14879 fini_reloc_cookie_for_section (&cookie, i);
14880 }
14881 }
14882
14883 o = NULL;
14884 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14885 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14886 if (o != NULL)
14887 {
14888 asection *i;
14889 int eh_changed = 0;
14890 unsigned int eh_alignment; /* Octets. */
14891
14892 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14893 {
14894 if (i->size == 0)
14895 continue;
14896
14897 abfd = i->owner;
14898 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14899 continue;
14900
14901 if (!init_reloc_cookie_for_section (&cookie, info, i))
14902 return -1;
14903
14904 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14905 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14906 bfd_elf_reloc_symbol_deleted_p,
14907 &cookie))
14908 {
14909 eh_changed = 1;
14910 if (i->size != i->rawsize)
14911 changed = 1;
14912 }
14913
14914 fini_reloc_cookie_for_section (&cookie, i);
14915 }
14916
14917 eh_alignment = ((1 << o->alignment_power)
14918 * bfd_octets_per_byte (output_bfd, o));
14919 /* Skip over zero terminator, and prevent empty sections from
14920 adding alignment padding at the end. */
14921 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14922 if (i->size == 0)
14923 i->flags |= SEC_EXCLUDE;
14924 else if (i->size > 4)
14925 break;
14926 /* The last non-empty eh_frame section doesn't need padding. */
14927 if (i != NULL)
14928 i = i->map_tail.s;
14929 /* Any prior sections must pad the last FDE out to the output
14930 section alignment. Otherwise we might have zero padding
14931 between sections, which would be seen as a terminator. */
14932 for (; i != NULL; i = i->map_tail.s)
14933 if (i->size == 4)
14934 /* All but the last zero terminator should have been removed. */
14935 BFD_FAIL ();
14936 else
14937 {
14938 bfd_size_type size
14939 = (i->size + eh_alignment - 1) & -eh_alignment;
14940 if (i->size != size)
14941 {
14942 i->size = size;
14943 changed = 1;
14944 eh_changed = 1;
14945 }
14946 }
14947 if (eh_changed)
14948 elf_link_hash_traverse (elf_hash_table (info),
14949 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14950 }
14951
14952 o = bfd_get_section_by_name (output_bfd, ".sframe");
14953 if (o != NULL)
14954 {
14955 asection *i;
14956
14957 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14958 {
14959 if (i->size == 0)
14960 continue;
14961
14962 abfd = i->owner;
14963 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14964 continue;
14965
14966 if (!init_reloc_cookie_for_section (&cookie, info, i))
14967 return -1;
14968
14969 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
14970 {
14971 if (_bfd_elf_discard_section_sframe (i,
14972 bfd_elf_reloc_symbol_deleted_p,
14973 &cookie))
14974 {
14975 if (i->size != i->rawsize)
14976 changed = 1;
14977 }
14978 }
14979 fini_reloc_cookie_for_section (&cookie, i);
14980 }
14981 /* Update the reference to the output .sframe section. Used to
14982 determine later if PT_GNU_SFRAME segment is to be generated. */
14983 if (!_bfd_elf_set_section_sframe (output_bfd, info))
14984 return -1;
14985 }
14986
14987 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14988 {
14989 const struct elf_backend_data *bed;
14990 asection *s;
14991
14992 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14993 continue;
14994 s = abfd->sections;
14995 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14996 continue;
14997
14998 bed = get_elf_backend_data (abfd);
14999
15000 if (bed->elf_backend_discard_info != NULL)
15001 {
15002 if (!init_reloc_cookie (&cookie, info, abfd))
15003 return -1;
15004
15005 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15006 changed = 1;
15007
15008 fini_reloc_cookie (&cookie, abfd);
15009 }
15010 }
15011
15012 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15013 _bfd_elf_end_eh_frame_parsing (info);
15014
15015 if (info->eh_frame_hdr_type
15016 && !bfd_link_relocatable (info)
15017 && _bfd_elf_discard_section_eh_frame_hdr (info))
15018 changed = 1;
15019
15020 return changed;
15021 }
15022
15023 bool
15024 _bfd_elf_section_already_linked (bfd *abfd,
15025 asection *sec,
15026 struct bfd_link_info *info)
15027 {
15028 flagword flags;
15029 const char *name, *key;
15030 struct bfd_section_already_linked *l;
15031 struct bfd_section_already_linked_hash_entry *already_linked_list;
15032
15033 if (sec->output_section == bfd_abs_section_ptr)
15034 return false;
15035
15036 flags = sec->flags;
15037
15038 /* Return if it isn't a linkonce section. A comdat group section
15039 also has SEC_LINK_ONCE set. */
15040 if ((flags & SEC_LINK_ONCE) == 0)
15041 return false;
15042
15043 /* Don't put group member sections on our list of already linked
15044 sections. They are handled as a group via their group section. */
15045 if (elf_sec_group (sec) != NULL)
15046 return false;
15047
15048 /* For a SHT_GROUP section, use the group signature as the key. */
15049 name = sec->name;
15050 if ((flags & SEC_GROUP) != 0
15051 && elf_next_in_group (sec) != NULL
15052 && elf_group_name (elf_next_in_group (sec)) != NULL)
15053 key = elf_group_name (elf_next_in_group (sec));
15054 else
15055 {
15056 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15057 if (startswith (name, ".gnu.linkonce.")
15058 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15059 key++;
15060 else
15061 /* Must be a user linkonce section that doesn't follow gcc's
15062 naming convention. In this case we won't be matching
15063 single member groups. */
15064 key = name;
15065 }
15066
15067 already_linked_list = bfd_section_already_linked_table_lookup (key);
15068
15069 for (l = already_linked_list->entry; l != NULL; l = l->next)
15070 {
15071 /* We may have 2 different types of sections on the list: group
15072 sections with a signature of <key> (<key> is some string),
15073 and linkonce sections named .gnu.linkonce.<type>.<key>.
15074 Match like sections. LTO plugin sections are an exception.
15075 They are always named .gnu.linkonce.t.<key> and match either
15076 type of section. */
15077 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15078 && ((flags & SEC_GROUP) != 0
15079 || strcmp (name, l->sec->name) == 0))
15080 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15081 || (sec->owner->flags & BFD_PLUGIN) != 0)
15082 {
15083 /* The section has already been linked. See if we should
15084 issue a warning. */
15085 if (!_bfd_handle_already_linked (sec, l, info))
15086 return false;
15087
15088 if (flags & SEC_GROUP)
15089 {
15090 asection *first = elf_next_in_group (sec);
15091 asection *s = first;
15092
15093 while (s != NULL)
15094 {
15095 s->output_section = bfd_abs_section_ptr;
15096 /* Record which group discards it. */
15097 s->kept_section = l->sec;
15098 s = elf_next_in_group (s);
15099 /* These lists are circular. */
15100 if (s == first)
15101 break;
15102 }
15103 }
15104
15105 return true;
15106 }
15107 }
15108
15109 /* A single member comdat group section may be discarded by a
15110 linkonce section and vice versa. */
15111 if ((flags & SEC_GROUP) != 0)
15112 {
15113 asection *first = elf_next_in_group (sec);
15114
15115 if (first != NULL && elf_next_in_group (first) == first)
15116 /* Check this single member group against linkonce sections. */
15117 for (l = already_linked_list->entry; l != NULL; l = l->next)
15118 if ((l->sec->flags & SEC_GROUP) == 0
15119 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15120 {
15121 first->output_section = bfd_abs_section_ptr;
15122 first->kept_section = l->sec;
15123 sec->output_section = bfd_abs_section_ptr;
15124 break;
15125 }
15126 }
15127 else
15128 /* Check this linkonce section against single member groups. */
15129 for (l = already_linked_list->entry; l != NULL; l = l->next)
15130 if (l->sec->flags & SEC_GROUP)
15131 {
15132 asection *first = elf_next_in_group (l->sec);
15133
15134 if (first != NULL
15135 && elf_next_in_group (first) == first
15136 && bfd_elf_match_symbols_in_sections (first, sec, info))
15137 {
15138 sec->output_section = bfd_abs_section_ptr;
15139 sec->kept_section = first;
15140 break;
15141 }
15142 }
15143
15144 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15145 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15146 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15147 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15148 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15149 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15150 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15151 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15152 The reverse order cannot happen as there is never a bfd with only the
15153 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15154 matter as here were are looking only for cross-bfd sections. */
15155
15156 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15157 for (l = already_linked_list->entry; l != NULL; l = l->next)
15158 if ((l->sec->flags & SEC_GROUP) == 0
15159 && startswith (l->sec->name, ".gnu.linkonce.t."))
15160 {
15161 if (abfd != l->sec->owner)
15162 sec->output_section = bfd_abs_section_ptr;
15163 break;
15164 }
15165
15166 /* This is the first section with this name. Record it. */
15167 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15168 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15169 return sec->output_section == bfd_abs_section_ptr;
15170 }
15171
15172 bool
15173 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15174 {
15175 return sym->st_shndx == SHN_COMMON;
15176 }
15177
15178 unsigned int
15179 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15180 {
15181 return SHN_COMMON;
15182 }
15183
15184 asection *
15185 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15186 {
15187 return bfd_com_section_ptr;
15188 }
15189
15190 bfd_vma
15191 _bfd_elf_default_got_elt_size (bfd *abfd,
15192 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15193 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15194 bfd *ibfd ATTRIBUTE_UNUSED,
15195 unsigned long symndx ATTRIBUTE_UNUSED)
15196 {
15197 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15198 return bed->s->arch_size / 8;
15199 }
15200
15201 /* Routines to support the creation of dynamic relocs. */
15202
15203 /* Returns the name of the dynamic reloc section associated with SEC. */
15204
15205 static const char *
15206 get_dynamic_reloc_section_name (bfd * abfd,
15207 asection * sec,
15208 bool is_rela)
15209 {
15210 char *name;
15211 const char *old_name = bfd_section_name (sec);
15212 const char *prefix = is_rela ? ".rela" : ".rel";
15213
15214 if (old_name == NULL)
15215 return NULL;
15216
15217 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15218 sprintf (name, "%s%s", prefix, old_name);
15219
15220 return name;
15221 }
15222
15223 /* Returns the dynamic reloc section associated with SEC.
15224 If necessary compute the name of the dynamic reloc section based
15225 on SEC's name (looked up in ABFD's string table) and the setting
15226 of IS_RELA. */
15227
15228 asection *
15229 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15230 asection *sec,
15231 bool is_rela)
15232 {
15233 asection *reloc_sec = elf_section_data (sec)->sreloc;
15234
15235 if (reloc_sec == NULL)
15236 {
15237 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15238
15239 if (name != NULL)
15240 {
15241 reloc_sec = bfd_get_linker_section (abfd, name);
15242
15243 if (reloc_sec != NULL)
15244 elf_section_data (sec)->sreloc = reloc_sec;
15245 }
15246 }
15247
15248 return reloc_sec;
15249 }
15250
15251 /* Returns the dynamic reloc section associated with SEC. If the
15252 section does not exist it is created and attached to the DYNOBJ
15253 bfd and stored in the SRELOC field of SEC's elf_section_data
15254 structure.
15255
15256 ALIGNMENT is the alignment for the newly created section and
15257 IS_RELA defines whether the name should be .rela.<SEC's name>
15258 or .rel.<SEC's name>. The section name is looked up in the
15259 string table associated with ABFD. */
15260
15261 asection *
15262 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15263 bfd *dynobj,
15264 unsigned int alignment,
15265 bfd *abfd,
15266 bool is_rela)
15267 {
15268 asection * reloc_sec = elf_section_data (sec)->sreloc;
15269
15270 if (reloc_sec == NULL)
15271 {
15272 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15273
15274 if (name == NULL)
15275 return NULL;
15276
15277 reloc_sec = bfd_get_linker_section (dynobj, name);
15278
15279 if (reloc_sec == NULL)
15280 {
15281 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15282 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15283 if ((sec->flags & SEC_ALLOC) != 0)
15284 flags |= SEC_ALLOC | SEC_LOAD;
15285
15286 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15287 if (reloc_sec != NULL)
15288 {
15289 /* _bfd_elf_get_sec_type_attr chooses a section type by
15290 name. Override as it may be wrong, eg. for a user
15291 section named "auto" we'll get ".relauto" which is
15292 seen to be a .rela section. */
15293 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15294 if (!bfd_set_section_alignment (reloc_sec, alignment))
15295 reloc_sec = NULL;
15296 }
15297 }
15298
15299 elf_section_data (sec)->sreloc = reloc_sec;
15300 }
15301
15302 return reloc_sec;
15303 }
15304
15305 /* Copy the ELF symbol type and other attributes for a linker script
15306 assignment from HSRC to HDEST. Generally this should be treated as
15307 if we found a strong non-dynamic definition for HDEST (except that
15308 ld ignores multiple definition errors). */
15309 void
15310 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15311 struct bfd_link_hash_entry *hdest,
15312 struct bfd_link_hash_entry *hsrc)
15313 {
15314 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15315 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15316 Elf_Internal_Sym isym;
15317
15318 ehdest->type = ehsrc->type;
15319 ehdest->target_internal = ehsrc->target_internal;
15320
15321 isym.st_other = ehsrc->other;
15322 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15323 }
15324
15325 /* Append a RELA relocation REL to section S in BFD. */
15326
15327 void
15328 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15329 {
15330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15331 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15332 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15333 bed->s->swap_reloca_out (abfd, rel, loc);
15334 }
15335
15336 /* Append a REL relocation REL to section S in BFD. */
15337
15338 void
15339 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15340 {
15341 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15342 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15343 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15344 bed->s->swap_reloc_out (abfd, rel, loc);
15345 }
15346
15347 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15348
15349 struct bfd_link_hash_entry *
15350 bfd_elf_define_start_stop (struct bfd_link_info *info,
15351 const char *symbol, asection *sec)
15352 {
15353 struct elf_link_hash_entry *h;
15354
15355 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15356 false, false, true);
15357 /* NB: Common symbols will be turned into definition later. */
15358 if (h != NULL
15359 && !h->root.ldscript_def
15360 && (h->root.type == bfd_link_hash_undefined
15361 || h->root.type == bfd_link_hash_undefweak
15362 || ((h->ref_regular || h->def_dynamic)
15363 && !h->def_regular
15364 && h->root.type != bfd_link_hash_common)))
15365 {
15366 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15367 h->verinfo.verdef = NULL;
15368 h->root.type = bfd_link_hash_defined;
15369 h->root.u.def.section = sec;
15370 h->root.u.def.value = 0;
15371 h->def_regular = 1;
15372 h->def_dynamic = 0;
15373 h->start_stop = 1;
15374 h->u2.start_stop_section = sec;
15375 if (symbol[0] == '.')
15376 {
15377 /* .startof. and .sizeof. symbols are local. */
15378 const struct elf_backend_data *bed;
15379 bed = get_elf_backend_data (info->output_bfd);
15380 (*bed->elf_backend_hide_symbol) (info, h, true);
15381 }
15382 else
15383 {
15384 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15385 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15386 | info->start_stop_visibility);
15387 if (was_dynamic)
15388 bfd_elf_link_record_dynamic_symbol (info, h);
15389 }
15390 return &h->root;
15391 }
15392 return NULL;
15393 }
15394
15395 /* Find dynamic relocs for H that apply to read-only sections. */
15396
15397 asection *
15398 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15399 {
15400 struct elf_dyn_relocs *p;
15401
15402 for (p = h->dyn_relocs; p != NULL; p = p->next)
15403 {
15404 asection *s = p->sec->output_section;
15405
15406 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15407 return p->sec;
15408 }
15409 return NULL;
15410 }
15411
15412 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15413 read-only sections. */
15414
15415 bool
15416 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15417 {
15418 asection *sec;
15419
15420 if (h->root.type == bfd_link_hash_indirect)
15421 return true;
15422
15423 sec = _bfd_elf_readonly_dynrelocs (h);
15424 if (sec != NULL)
15425 {
15426 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15427
15428 info->flags |= DF_TEXTREL;
15429 /* xgettext:c-format */
15430 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15431 "in read-only section `%pA'\n"),
15432 sec->owner, h->root.root.string, sec);
15433
15434 if (bfd_link_textrel_check (info))
15435 /* xgettext:c-format */
15436 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15437 "in read-only section `%pA'\n"),
15438 sec->owner, h->root.root.string, sec);
15439
15440 /* Not an error, just cut short the traversal. */
15441 return false;
15442 }
15443 return true;
15444 }
15445
15446 /* Add dynamic tags. */
15447
15448 bool
15449 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15450 bool need_dynamic_reloc)
15451 {
15452 struct elf_link_hash_table *htab = elf_hash_table (info);
15453
15454 if (htab->dynamic_sections_created)
15455 {
15456 /* Add some entries to the .dynamic section. We fill in the
15457 values later, in finish_dynamic_sections, but we must add
15458 the entries now so that we get the correct size for the
15459 .dynamic section. The DT_DEBUG entry is filled in by the
15460 dynamic linker and used by the debugger. */
15461 #define add_dynamic_entry(TAG, VAL) \
15462 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15463
15464 const struct elf_backend_data *bed
15465 = get_elf_backend_data (output_bfd);
15466
15467 if (bfd_link_executable (info))
15468 {
15469 if (!add_dynamic_entry (DT_DEBUG, 0))
15470 return false;
15471 }
15472
15473 if (htab->dt_pltgot_required || htab->splt->size != 0)
15474 {
15475 /* DT_PLTGOT is used by prelink even if there is no PLT
15476 relocation. */
15477 if (!add_dynamic_entry (DT_PLTGOT, 0))
15478 return false;
15479 }
15480
15481 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15482 {
15483 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15484 || !add_dynamic_entry (DT_PLTREL,
15485 (bed->rela_plts_and_copies_p
15486 ? DT_RELA : DT_REL))
15487 || !add_dynamic_entry (DT_JMPREL, 0))
15488 return false;
15489 }
15490
15491 if (htab->tlsdesc_plt
15492 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15493 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15494 return false;
15495
15496 if (need_dynamic_reloc)
15497 {
15498 if (bed->rela_plts_and_copies_p)
15499 {
15500 if (!add_dynamic_entry (DT_RELA, 0)
15501 || !add_dynamic_entry (DT_RELASZ, 0)
15502 || !add_dynamic_entry (DT_RELAENT,
15503 bed->s->sizeof_rela))
15504 return false;
15505 }
15506 else
15507 {
15508 if (!add_dynamic_entry (DT_REL, 0)
15509 || !add_dynamic_entry (DT_RELSZ, 0)
15510 || !add_dynamic_entry (DT_RELENT,
15511 bed->s->sizeof_rel))
15512 return false;
15513 }
15514
15515 /* If any dynamic relocs apply to a read-only section,
15516 then we need a DT_TEXTREL entry. */
15517 if ((info->flags & DF_TEXTREL) == 0)
15518 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15519 info);
15520
15521 if ((info->flags & DF_TEXTREL) != 0)
15522 {
15523 if (htab->ifunc_resolvers)
15524 info->callbacks->einfo
15525 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15526 "may result in a segfault at runtime; recompile with %s\n"),
15527 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15528
15529 if (!add_dynamic_entry (DT_TEXTREL, 0))
15530 return false;
15531 }
15532 }
15533 }
15534 #undef add_dynamic_entry
15535
15536 return true;
15537 }