]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
PR 30569, always call elf_backend_size_dynamic_sections
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry *, struct elf_info_failed *);
51
52 asection *
53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
54 unsigned long r_symndx,
55 bool discard)
56 {
57 if (r_symndx >= cookie->locsymcount
58 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
59 {
60 struct elf_link_hash_entry *h;
61
62 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
63
64 while (h->root.type == bfd_link_hash_indirect
65 || h->root.type == bfd_link_hash_warning)
66 h = (struct elf_link_hash_entry *) h->root.u.i.link;
67
68 if ((h->root.type == bfd_link_hash_defined
69 || h->root.type == bfd_link_hash_defweak)
70 && discarded_section (h->root.u.def.section))
71 return h->root.u.def.section;
72 else
73 return NULL;
74 }
75 else
76 {
77 /* It's not a relocation against a global symbol,
78 but it could be a relocation against a local
79 symbol for a discarded section. */
80 asection *isec;
81 Elf_Internal_Sym *isym;
82
83 /* Need to: get the symbol; get the section. */
84 isym = &cookie->locsyms[r_symndx];
85 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
86 if (isec != NULL
87 && discard ? discarded_section (isec) : 1)
88 return isec;
89 }
90 return NULL;
91 }
92
93 /* Define a symbol in a dynamic linkage section. */
94
95 struct elf_link_hash_entry *
96 _bfd_elf_define_linkage_sym (bfd *abfd,
97 struct bfd_link_info *info,
98 asection *sec,
99 const char *name)
100 {
101 struct elf_link_hash_entry *h;
102 struct bfd_link_hash_entry *bh;
103 const struct elf_backend_data *bed;
104
105 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
106 if (h != NULL)
107 {
108 /* Zap symbol defined in an as-needed lib that wasn't linked.
109 This is a symptom of a larger problem: Absolute symbols
110 defined in shared libraries can't be overridden, because we
111 lose the link to the bfd which is via the symbol section. */
112 h->root.type = bfd_link_hash_new;
113 bh = &h->root;
114 }
115 else
116 bh = NULL;
117
118 bed = get_elf_backend_data (abfd);
119 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
120 sec, 0, NULL, false, bed->collect,
121 &bh))
122 return NULL;
123 h = (struct elf_link_hash_entry *) bh;
124 BFD_ASSERT (h != NULL);
125 h->def_regular = 1;
126 h->non_elf = 0;
127 h->root.linker_def = 1;
128 h->type = STT_OBJECT;
129 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
130 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
131
132 (*bed->elf_backend_hide_symbol) (info, h, true);
133 return h;
134 }
135
136 bool
137 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
138 {
139 flagword flags;
140 asection *s;
141 struct elf_link_hash_entry *h;
142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
143 struct elf_link_hash_table *htab = elf_hash_table (info);
144
145 /* This function may be called more than once. */
146 if (htab->sgot != NULL)
147 return true;
148
149 flags = bed->dynamic_sec_flags;
150
151 s = bfd_make_section_anyway_with_flags (abfd,
152 (bed->rela_plts_and_copies_p
153 ? ".rela.got" : ".rel.got"),
154 (bed->dynamic_sec_flags
155 | SEC_READONLY));
156 if (s == NULL
157 || !bfd_set_section_alignment (s, bed->s->log_file_align))
158 return false;
159 htab->srelgot = s;
160
161 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
162 if (s == NULL
163 || !bfd_set_section_alignment (s, bed->s->log_file_align))
164 return false;
165 htab->sgot = s;
166
167 if (bed->want_got_plt)
168 {
169 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return false;
173 htab->sgotplt = s;
174 }
175
176 /* The first bit of the global offset table is the header. */
177 s->size += bed->got_header_size;
178
179 if (bed->want_got_sym)
180 {
181 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
182 (or .got.plt) section. We don't do this in the linker script
183 because we don't want to define the symbol if we are not creating
184 a global offset table. */
185 h = _bfd_elf_define_linkage_sym (abfd, info, s,
186 "_GLOBAL_OFFSET_TABLE_");
187 elf_hash_table (info)->hgot = h;
188 if (h == NULL)
189 return false;
190 }
191
192 return true;
193 }
194 \f
195 /* Create a strtab to hold the dynamic symbol names. */
196 static bool
197 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
198 {
199 struct elf_link_hash_table *hash_table;
200
201 hash_table = elf_hash_table (info);
202 if (hash_table->dynobj == NULL)
203 {
204 /* We may not set dynobj, an input file holding linker created
205 dynamic sections to abfd, which may be a dynamic object with
206 its own dynamic sections. We need to find a normal input file
207 to hold linker created sections if possible. */
208 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
209 {
210 bfd *ibfd;
211 asection *s;
212 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
213 if ((ibfd->flags
214 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
215 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
216 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
217 && !((s = ibfd->sections) != NULL
218 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
219 {
220 abfd = ibfd;
221 break;
222 }
223 }
224 hash_table->dynobj = abfd;
225 }
226
227 if (hash_table->dynstr == NULL)
228 {
229 hash_table->dynstr = _bfd_elf_strtab_init ();
230 if (hash_table->dynstr == NULL)
231 return false;
232 }
233 return true;
234 }
235
236 /* Create some sections which will be filled in with dynamic linking
237 information. ABFD is an input file which requires dynamic sections
238 to be created. The dynamic sections take up virtual memory space
239 when the final executable is run, so we need to create them before
240 addresses are assigned to the output sections. We work out the
241 actual contents and size of these sections later. */
242
243 bool
244 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
245 {
246 flagword flags;
247 asection *s;
248 const struct elf_backend_data *bed;
249 struct elf_link_hash_entry *h;
250
251 if (! is_elf_hash_table (info->hash))
252 return false;
253
254 if (elf_hash_table (info)->dynamic_sections_created)
255 return true;
256
257 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
258 return false;
259
260 abfd = elf_hash_table (info)->dynobj;
261 bed = get_elf_backend_data (abfd);
262
263 flags = bed->dynamic_sec_flags;
264
265 /* A dynamically linked executable has a .interp section, but a
266 shared library does not. */
267 if (bfd_link_executable (info) && !info->nointerp)
268 {
269 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
270 flags | SEC_READONLY);
271 if (s == NULL)
272 return false;
273 }
274
275 /* Create sections to hold version informations. These are removed
276 if they are not needed. */
277 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
278 flags | SEC_READONLY);
279 if (s == NULL
280 || !bfd_set_section_alignment (s, bed->s->log_file_align))
281 return false;
282
283 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
284 flags | SEC_READONLY);
285 if (s == NULL
286 || !bfd_set_section_alignment (s, 1))
287 return false;
288
289 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
290 flags | SEC_READONLY);
291 if (s == NULL
292 || !bfd_set_section_alignment (s, bed->s->log_file_align))
293 return false;
294
295 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
296 flags | SEC_READONLY);
297 if (s == NULL
298 || !bfd_set_section_alignment (s, bed->s->log_file_align))
299 return false;
300 elf_hash_table (info)->dynsym = s;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
303 flags | SEC_READONLY);
304 if (s == NULL)
305 return false;
306
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
308 if (s == NULL
309 || !bfd_set_section_alignment (s, bed->s->log_file_align))
310 return false;
311
312 /* The special symbol _DYNAMIC is always set to the start of the
313 .dynamic section. We could set _DYNAMIC in a linker script, but we
314 only want to define it if we are, in fact, creating a .dynamic
315 section. We don't want to define it if there is no .dynamic
316 section, since on some ELF platforms the start up code examines it
317 to decide how to initialize the process. */
318 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
319 elf_hash_table (info)->hdynamic = h;
320 if (h == NULL)
321 return false;
322
323 if (info->emit_hash)
324 {
325 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
326 flags | SEC_READONLY);
327 if (s == NULL
328 || !bfd_set_section_alignment (s, bed->s->log_file_align))
329 return false;
330 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
331 }
332
333 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
334 {
335 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
336 flags | SEC_READONLY);
337 if (s == NULL
338 || !bfd_set_section_alignment (s, bed->s->log_file_align))
339 return false;
340 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
341 4 32-bit words followed by variable count of 64-bit words, then
342 variable count of 32-bit words. */
343 if (bed->s->arch_size == 64)
344 elf_section_data (s)->this_hdr.sh_entsize = 0;
345 else
346 elf_section_data (s)->this_hdr.sh_entsize = 4;
347 }
348
349 if (info->enable_dt_relr)
350 {
351 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
352 (bed->dynamic_sec_flags
353 | SEC_READONLY));
354 if (s == NULL
355 || !bfd_set_section_alignment (s, bed->s->log_file_align))
356 return false;
357 elf_hash_table (info)->srelrdyn = s;
358 }
359
360 /* Let the backend create the rest of the sections. This lets the
361 backend set the right flags. The backend will normally create
362 the .got and .plt sections. */
363 if (bed->elf_backend_create_dynamic_sections == NULL
364 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 return false;
366
367 elf_hash_table (info)->dynamic_sections_created = true;
368
369 return true;
370 }
371
372 /* Create dynamic sections when linking against a dynamic object. */
373
374 bool
375 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
376 {
377 flagword flags, pltflags;
378 struct elf_link_hash_entry *h;
379 asection *s;
380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
381 struct elf_link_hash_table *htab = elf_hash_table (info);
382
383 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
384 .rel[a].bss sections. */
385 flags = bed->dynamic_sec_flags;
386
387 pltflags = flags;
388 if (bed->plt_not_loaded)
389 /* We do not clear SEC_ALLOC here because we still want the OS to
390 allocate space for the section; it's just that there's nothing
391 to read in from the object file. */
392 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
393 else
394 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
395 if (bed->plt_readonly)
396 pltflags |= SEC_READONLY;
397
398 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
399 if (s == NULL
400 || !bfd_set_section_alignment (s, bed->plt_alignment))
401 return false;
402 htab->splt = s;
403
404 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
405 .plt section. */
406 if (bed->want_plt_sym)
407 {
408 h = _bfd_elf_define_linkage_sym (abfd, info, s,
409 "_PROCEDURE_LINKAGE_TABLE_");
410 elf_hash_table (info)->hplt = h;
411 if (h == NULL)
412 return false;
413 }
414
415 s = bfd_make_section_anyway_with_flags (abfd,
416 (bed->rela_plts_and_copies_p
417 ? ".rela.plt" : ".rel.plt"),
418 flags | SEC_READONLY);
419 if (s == NULL
420 || !bfd_set_section_alignment (s, bed->s->log_file_align))
421 return false;
422 htab->srelplt = s;
423
424 if (! _bfd_elf_create_got_section (abfd, info))
425 return false;
426
427 if (bed->want_dynbss)
428 {
429 /* The .dynbss section is a place to put symbols which are defined
430 by dynamic objects, are referenced by regular objects, and are
431 not functions. We must allocate space for them in the process
432 image and use a R_*_COPY reloc to tell the dynamic linker to
433 initialize them at run time. The linker script puts the .dynbss
434 section into the .bss section of the final image. */
435 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
436 SEC_ALLOC | SEC_LINKER_CREATED);
437 if (s == NULL)
438 return false;
439 htab->sdynbss = s;
440
441 if (bed->want_dynrelro)
442 {
443 /* Similarly, but for symbols that were originally in read-only
444 sections. This section doesn't really need to have contents,
445 but make it like other .data.rel.ro sections. */
446 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
447 flags);
448 if (s == NULL)
449 return false;
450 htab->sdynrelro = s;
451 }
452
453 /* The .rel[a].bss section holds copy relocs. This section is not
454 normally needed. We need to create it here, though, so that the
455 linker will map it to an output section. We can't just create it
456 only if we need it, because we will not know whether we need it
457 until we have seen all the input files, and the first time the
458 main linker code calls BFD after examining all the input files
459 (size_dynamic_sections) the input sections have already been
460 mapped to the output sections. If the section turns out not to
461 be needed, we can discard it later. We will never need this
462 section when generating a shared object, since they do not use
463 copy relocs. */
464 if (bfd_link_executable (info))
465 {
466 s = bfd_make_section_anyway_with_flags (abfd,
467 (bed->rela_plts_and_copies_p
468 ? ".rela.bss" : ".rel.bss"),
469 flags | SEC_READONLY);
470 if (s == NULL
471 || !bfd_set_section_alignment (s, bed->s->log_file_align))
472 return false;
473 htab->srelbss = s;
474
475 if (bed->want_dynrelro)
476 {
477 s = (bfd_make_section_anyway_with_flags
478 (abfd, (bed->rela_plts_and_copies_p
479 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
480 flags | SEC_READONLY));
481 if (s == NULL
482 || !bfd_set_section_alignment (s, bed->s->log_file_align))
483 return false;
484 htab->sreldynrelro = s;
485 }
486 }
487 }
488
489 return true;
490 }
491 \f
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
498 one. */
499
500 bool
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
503 {
504 if (h->dynindx == -1)
505 {
506 struct elf_strtab_hash *dynstr;
507 char *p;
508 const char *name;
509 size_t indx;
510
511 if (h->root.type == bfd_link_hash_defined
512 || h->root.type == bfd_link_hash_defweak)
513 {
514 /* An IR symbol should not be made dynamic. */
515 if (h->root.u.def.section != NULL
516 && h->root.u.def.section->owner != NULL
517 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
518 return true;
519 }
520
521 /* XXX: The ABI draft says the linker must turn hidden and
522 internal symbols into STB_LOCAL symbols when producing the
523 DSO. However, if ld.so honors st_other in the dynamic table,
524 this would not be necessary. */
525 switch (ELF_ST_VISIBILITY (h->other))
526 {
527 case STV_INTERNAL:
528 case STV_HIDDEN:
529 if (h->root.type != bfd_link_hash_undefined
530 && h->root.type != bfd_link_hash_undefweak)
531 {
532 h->forced_local = 1;
533 return true;
534 }
535
536 default:
537 break;
538 }
539
540 h->dynindx = elf_hash_table (info)->dynsymcount;
541 ++elf_hash_table (info)->dynsymcount;
542
543 dynstr = elf_hash_table (info)->dynstr;
544 if (dynstr == NULL)
545 {
546 /* Create a strtab to hold the dynamic symbol names. */
547 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
548 if (dynstr == NULL)
549 return false;
550 }
551
552 /* We don't put any version information in the dynamic string
553 table. */
554 name = h->root.root.string;
555 p = strchr (name, ELF_VER_CHR);
556 if (p != NULL)
557 /* We know that the p points into writable memory. In fact,
558 there are only a few symbols that have read-only names, being
559 those like _GLOBAL_OFFSET_TABLE_ that are created specially
560 by the backends. Most symbols will have names pointing into
561 an ELF string table read from a file, or to objalloc memory. */
562 *p = 0;
563
564 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
565
566 if (p != NULL)
567 *p = ELF_VER_CHR;
568
569 if (indx == (size_t) -1)
570 return false;
571 h->dynstr_index = indx;
572 }
573
574 return true;
575 }
576 \f
577 /* Mark a symbol dynamic. */
578
579 static void
580 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
581 struct elf_link_hash_entry *h,
582 Elf_Internal_Sym *sym)
583 {
584 struct bfd_elf_dynamic_list *d = info->dynamic_list;
585
586 /* It may be called more than once on the same H. */
587 if(h->dynamic || bfd_link_relocatable (info))
588 return;
589
590 if ((info->dynamic_data
591 && (h->type == STT_OBJECT
592 || h->type == STT_COMMON
593 || (sym != NULL
594 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
595 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
596 || (d != NULL
597 && h->non_elf
598 && (*d->match) (&d->head, NULL, h->root.root.string)))
599 {
600 h->dynamic = 1;
601 /* NB: If a symbol is made dynamic by --dynamic-list, it has
602 non-IR reference. */
603 h->root.non_ir_ref_dynamic = 1;
604 }
605 }
606
607 /* Record an assignment to a symbol made by a linker script. We need
608 this in case some dynamic object refers to this symbol. */
609
610 bool
611 bfd_elf_record_link_assignment (bfd *output_bfd,
612 struct bfd_link_info *info,
613 const char *name,
614 bool provide,
615 bool hidden)
616 {
617 struct elf_link_hash_entry *h, *hv;
618 struct elf_link_hash_table *htab;
619 const struct elf_backend_data *bed;
620
621 if (!is_elf_hash_table (info->hash))
622 return true;
623
624 htab = elf_hash_table (info);
625 h = elf_link_hash_lookup (htab, name, !provide, true, false);
626 if (h == NULL)
627 return provide;
628
629 if (h->root.type == bfd_link_hash_warning)
630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
631
632 if (h->versioned == unknown)
633 {
634 /* Set versioned if symbol version is unknown. */
635 char *version = strrchr (name, ELF_VER_CHR);
636 if (version)
637 {
638 if (version > name && version[-1] != ELF_VER_CHR)
639 h->versioned = versioned_hidden;
640 else
641 h->versioned = versioned;
642 }
643 }
644
645 /* Symbols defined in a linker script but not referenced anywhere
646 else will have non_elf set. */
647 if (h->non_elf)
648 {
649 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
650 h->non_elf = 0;
651 }
652
653 switch (h->root.type)
654 {
655 case bfd_link_hash_defined:
656 case bfd_link_hash_defweak:
657 case bfd_link_hash_common:
658 break;
659 case bfd_link_hash_undefweak:
660 case bfd_link_hash_undefined:
661 /* Since we're defining the symbol, don't let it seem to have not
662 been defined. record_dynamic_symbol and size_dynamic_sections
663 may depend on this. */
664 h->root.type = bfd_link_hash_new;
665 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
666 bfd_link_repair_undef_list (&htab->root);
667 break;
668 case bfd_link_hash_new:
669 break;
670 case bfd_link_hash_indirect:
671 /* We had a versioned symbol in a dynamic library. We make the
672 the versioned symbol point to this one. */
673 bed = get_elf_backend_data (output_bfd);
674 hv = h;
675 while (hv->root.type == bfd_link_hash_indirect
676 || hv->root.type == bfd_link_hash_warning)
677 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
678 /* We don't need to update h->root.u since linker will set them
679 later. */
680 h->root.type = bfd_link_hash_undefined;
681 hv->root.type = bfd_link_hash_indirect;
682 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
683 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
684 break;
685 default:
686 BFD_FAIL ();
687 return false;
688 }
689
690 /* If this symbol is being provided by the linker script, and it is
691 currently defined by a dynamic object, but not by a regular
692 object, then mark it as undefined so that the generic linker will
693 force the correct value. */
694 if (provide
695 && h->def_dynamic
696 && !h->def_regular)
697 h->root.type = bfd_link_hash_undefined;
698
699 /* If this symbol is currently defined by a dynamic object, but not
700 by a regular object, then clear out any version information because
701 the symbol will not be associated with the dynamic object any
702 more. */
703 if (h->def_dynamic && !h->def_regular)
704 h->verinfo.verdef = NULL;
705
706 /* Make sure this symbol is not garbage collected. */
707 h->mark = 1;
708
709 h->def_regular = 1;
710
711 if (hidden)
712 {
713 bed = get_elf_backend_data (output_bfd);
714 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
715 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
716 (*bed->elf_backend_hide_symbol) (info, h, true);
717 }
718
719 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
720 and executables. */
721 if (!bfd_link_relocatable (info)
722 && h->dynindx != -1
723 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
724 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
725 h->forced_local = 1;
726
727 if ((h->def_dynamic
728 || h->ref_dynamic
729 || bfd_link_dll (info))
730 && !h->forced_local
731 && h->dynindx == -1)
732 {
733 if (! bfd_elf_link_record_dynamic_symbol (info, h))
734 return false;
735
736 /* If this is a weak defined symbol, and we know a corresponding
737 real symbol from the same dynamic object, make sure the real
738 symbol is also made into a dynamic symbol. */
739 if (h->is_weakalias)
740 {
741 struct elf_link_hash_entry *def = weakdef (h);
742
743 if (def->dynindx == -1
744 && !bfd_elf_link_record_dynamic_symbol (info, def))
745 return false;
746 }
747 }
748
749 return true;
750 }
751
752 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
753 success, and 2 on a failure caused by attempting to record a symbol
754 in a discarded section, eg. a discarded link-once section symbol. */
755
756 int
757 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
758 bfd *input_bfd,
759 long input_indx)
760 {
761 size_t amt;
762 struct elf_link_local_dynamic_entry *entry;
763 struct elf_link_hash_table *eht;
764 struct elf_strtab_hash *dynstr;
765 size_t dynstr_index;
766 char *name;
767 Elf_External_Sym_Shndx eshndx;
768 char esym[sizeof (Elf64_External_Sym)];
769
770 if (! is_elf_hash_table (info->hash))
771 return 0;
772
773 /* See if the entry exists already. */
774 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
775 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
776 return 1;
777
778 amt = sizeof (*entry);
779 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
780 if (entry == NULL)
781 return 0;
782
783 /* Go find the symbol, so that we can find it's name. */
784 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
785 1, input_indx, &entry->isym, esym, &eshndx))
786 {
787 bfd_release (input_bfd, entry);
788 return 0;
789 }
790
791 if (entry->isym.st_shndx != SHN_UNDEF
792 && entry->isym.st_shndx < SHN_LORESERVE)
793 {
794 asection *s;
795
796 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
797 if (s == NULL || bfd_is_abs_section (s->output_section))
798 {
799 /* We can still bfd_release here as nothing has done another
800 bfd_alloc. We can't do this later in this function. */
801 bfd_release (input_bfd, entry);
802 return 2;
803 }
804 }
805
806 name = (bfd_elf_string_from_elf_section
807 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
808 entry->isym.st_name));
809
810 dynstr = elf_hash_table (info)->dynstr;
811 if (dynstr == NULL)
812 {
813 /* Create a strtab to hold the dynamic symbol names. */
814 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
815 if (dynstr == NULL)
816 return 0;
817 }
818
819 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
820 if (dynstr_index == (size_t) -1)
821 return 0;
822 entry->isym.st_name = dynstr_index;
823
824 eht = elf_hash_table (info);
825
826 entry->next = eht->dynlocal;
827 eht->dynlocal = entry;
828 entry->input_bfd = input_bfd;
829 entry->input_indx = input_indx;
830 eht->dynsymcount++;
831
832 /* Whatever binding the symbol had before, it's now local. */
833 entry->isym.st_info
834 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
835
836 /* The dynindx will be set at the end of size_dynamic_sections. */
837
838 return 1;
839 }
840
841 /* Return the dynindex of a local dynamic symbol. */
842
843 long
844 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
845 bfd *input_bfd,
846 long input_indx)
847 {
848 struct elf_link_local_dynamic_entry *e;
849
850 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
851 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
852 return e->dynindx;
853 return -1;
854 }
855
856 /* This function is used to renumber the dynamic symbols, if some of
857 them are removed because they are marked as local. This is called
858 via elf_link_hash_traverse. */
859
860 static bool
861 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
862 void *data)
863 {
864 size_t *count = (size_t *) data;
865
866 if (h->forced_local)
867 return true;
868
869 if (h->dynindx != -1)
870 h->dynindx = ++(*count);
871
872 return true;
873 }
874
875
876 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
877 STB_LOCAL binding. */
878
879 static bool
880 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
881 void *data)
882 {
883 size_t *count = (size_t *) data;
884
885 if (!h->forced_local)
886 return true;
887
888 if (h->dynindx != -1)
889 h->dynindx = ++(*count);
890
891 return true;
892 }
893
894 /* Return true if the dynamic symbol for a given section should be
895 omitted when creating a shared library. */
896 bool
897 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
898 struct bfd_link_info *info,
899 asection *p)
900 {
901 struct elf_link_hash_table *htab;
902 asection *ip;
903
904 switch (elf_section_data (p)->this_hdr.sh_type)
905 {
906 case SHT_PROGBITS:
907 case SHT_NOBITS:
908 /* If sh_type is yet undecided, assume it could be
909 SHT_PROGBITS/SHT_NOBITS. */
910 case SHT_NULL:
911 htab = elf_hash_table (info);
912 if (htab->text_index_section != NULL)
913 return p != htab->text_index_section && p != htab->data_index_section;
914
915 return (htab->dynobj != NULL
916 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
917 && ip->output_section == p);
918
919 /* There shouldn't be section relative relocations
920 against any other section. */
921 default:
922 return true;
923 }
924 }
925
926 bool
927 _bfd_elf_omit_section_dynsym_all
928 (bfd *output_bfd ATTRIBUTE_UNUSED,
929 struct bfd_link_info *info ATTRIBUTE_UNUSED,
930 asection *p ATTRIBUTE_UNUSED)
931 {
932 return true;
933 }
934
935 /* Assign dynsym indices. In a shared library we generate a section
936 symbol for each output section, which come first. Next come symbols
937 which have been forced to local binding. Then all of the back-end
938 allocated local dynamic syms, followed by the rest of the global
939 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
940 (This prevents the early call before elf_backend_init_index_section
941 and strip_excluded_output_sections setting dynindx for sections
942 that are stripped.) */
943
944 static unsigned long
945 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
946 struct bfd_link_info *info,
947 unsigned long *section_sym_count)
948 {
949 unsigned long dynsymcount = 0;
950 bool do_sec = section_sym_count != NULL;
951
952 if (bfd_link_pic (info)
953 || elf_hash_table (info)->is_relocatable_executable)
954 {
955 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
956 asection *p;
957 for (p = output_bfd->sections; p ; p = p->next)
958 if ((p->flags & SEC_EXCLUDE) == 0
959 && (p->flags & SEC_ALLOC) != 0
960 && elf_hash_table (info)->dynamic_relocs
961 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
962 {
963 ++dynsymcount;
964 if (do_sec)
965 elf_section_data (p)->dynindx = dynsymcount;
966 }
967 else if (do_sec)
968 elf_section_data (p)->dynindx = 0;
969 }
970 if (do_sec)
971 *section_sym_count = dynsymcount;
972
973 elf_link_hash_traverse (elf_hash_table (info),
974 elf_link_renumber_local_hash_table_dynsyms,
975 &dynsymcount);
976
977 if (elf_hash_table (info)->dynlocal)
978 {
979 struct elf_link_local_dynamic_entry *p;
980 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
981 p->dynindx = ++dynsymcount;
982 }
983 elf_hash_table (info)->local_dynsymcount = dynsymcount;
984
985 elf_link_hash_traverse (elf_hash_table (info),
986 elf_link_renumber_hash_table_dynsyms,
987 &dynsymcount);
988
989 /* There is an unused NULL entry at the head of the table which we
990 must account for in our count even if the table is empty since it
991 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
992 .dynamic section. */
993 dynsymcount++;
994
995 elf_hash_table (info)->dynsymcount = dynsymcount;
996 return dynsymcount;
997 }
998
999 /* Merge st_other field. */
1000
1001 static void
1002 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1003 unsigned int st_other, asection *sec,
1004 bool definition, bool dynamic)
1005 {
1006 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1007
1008 /* If st_other has a processor-specific meaning, specific
1009 code might be needed here. */
1010 if (bed->elf_backend_merge_symbol_attribute)
1011 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1012 dynamic);
1013
1014 if (!dynamic)
1015 {
1016 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1017 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1018
1019 /* Keep the most constraining visibility. Leave the remainder
1020 of the st_other field to elf_backend_merge_symbol_attribute. */
1021 if (symvis - 1 < hvis - 1)
1022 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1023 }
1024 else if (definition
1025 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1026 && (sec->flags & SEC_READONLY) == 0)
1027 h->protected_def = 1;
1028 }
1029
1030 /* This function is called when we want to merge a new symbol with an
1031 existing symbol. It handles the various cases which arise when we
1032 find a definition in a dynamic object, or when there is already a
1033 definition in a dynamic object. The new symbol is described by
1034 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1035 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1036 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1037 of an old common symbol. We set OVERRIDE if the old symbol is
1038 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1039 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1040 to change. By OK to change, we mean that we shouldn't warn if the
1041 type or size does change. */
1042
1043 static bool
1044 _bfd_elf_merge_symbol (bfd *abfd,
1045 struct bfd_link_info *info,
1046 const char *name,
1047 Elf_Internal_Sym *sym,
1048 asection **psec,
1049 bfd_vma *pvalue,
1050 struct elf_link_hash_entry **sym_hash,
1051 bfd **poldbfd,
1052 bool *pold_weak,
1053 unsigned int *pold_alignment,
1054 bool *skip,
1055 bfd **override,
1056 bool *type_change_ok,
1057 bool *size_change_ok,
1058 bool *matched)
1059 {
1060 asection *sec, *oldsec;
1061 struct elf_link_hash_entry *h;
1062 struct elf_link_hash_entry *hi;
1063 struct elf_link_hash_entry *flip;
1064 int bind;
1065 bfd *oldbfd;
1066 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1067 bool newweak, oldweak, newfunc, oldfunc;
1068 const struct elf_backend_data *bed;
1069 char *new_version;
1070 bool default_sym = *matched;
1071 struct elf_link_hash_table *htab;
1072
1073 *skip = false;
1074 *override = NULL;
1075
1076 sec = *psec;
1077 bind = ELF_ST_BIND (sym->st_info);
1078
1079 if (! bfd_is_und_section (sec))
1080 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1081 else
1082 h = ((struct elf_link_hash_entry *)
1083 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1084 if (h == NULL)
1085 return false;
1086 *sym_hash = h;
1087
1088 bed = get_elf_backend_data (abfd);
1089
1090 /* NEW_VERSION is the symbol version of the new symbol. */
1091 if (h->versioned != unversioned)
1092 {
1093 /* Symbol version is unknown or versioned. */
1094 new_version = strrchr (name, ELF_VER_CHR);
1095 if (new_version)
1096 {
1097 if (h->versioned == unknown)
1098 {
1099 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1100 h->versioned = versioned_hidden;
1101 else
1102 h->versioned = versioned;
1103 }
1104 new_version += 1;
1105 if (new_version[0] == '\0')
1106 new_version = NULL;
1107 }
1108 else
1109 h->versioned = unversioned;
1110 }
1111 else
1112 new_version = NULL;
1113
1114 /* For merging, we only care about real symbols. But we need to make
1115 sure that indirect symbol dynamic flags are updated. */
1116 hi = h;
1117 while (h->root.type == bfd_link_hash_indirect
1118 || h->root.type == bfd_link_hash_warning)
1119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1120
1121 if (!*matched)
1122 {
1123 if (hi == h || h->root.type == bfd_link_hash_new)
1124 *matched = true;
1125 else
1126 {
1127 /* OLD_HIDDEN is true if the existing symbol is only visible
1128 to the symbol with the same symbol version. NEW_HIDDEN is
1129 true if the new symbol is only visible to the symbol with
1130 the same symbol version. */
1131 bool old_hidden = h->versioned == versioned_hidden;
1132 bool new_hidden = hi->versioned == versioned_hidden;
1133 if (!old_hidden && !new_hidden)
1134 /* The new symbol matches the existing symbol if both
1135 aren't hidden. */
1136 *matched = true;
1137 else
1138 {
1139 /* OLD_VERSION is the symbol version of the existing
1140 symbol. */
1141 char *old_version;
1142
1143 if (h->versioned >= versioned)
1144 old_version = strrchr (h->root.root.string,
1145 ELF_VER_CHR) + 1;
1146 else
1147 old_version = NULL;
1148
1149 /* The new symbol matches the existing symbol if they
1150 have the same symbol version. */
1151 *matched = (old_version == new_version
1152 || (old_version != NULL
1153 && new_version != NULL
1154 && strcmp (old_version, new_version) == 0));
1155 }
1156 }
1157 }
1158
1159 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1160 existing symbol. */
1161
1162 oldbfd = NULL;
1163 oldsec = NULL;
1164 switch (h->root.type)
1165 {
1166 default:
1167 break;
1168
1169 case bfd_link_hash_undefined:
1170 case bfd_link_hash_undefweak:
1171 oldbfd = h->root.u.undef.abfd;
1172 break;
1173
1174 case bfd_link_hash_defined:
1175 case bfd_link_hash_defweak:
1176 oldbfd = h->root.u.def.section->owner;
1177 oldsec = h->root.u.def.section;
1178 break;
1179
1180 case bfd_link_hash_common:
1181 oldbfd = h->root.u.c.p->section->owner;
1182 oldsec = h->root.u.c.p->section;
1183 if (pold_alignment)
1184 *pold_alignment = h->root.u.c.p->alignment_power;
1185 break;
1186 }
1187 if (poldbfd && *poldbfd == NULL)
1188 *poldbfd = oldbfd;
1189
1190 /* Differentiate strong and weak symbols. */
1191 newweak = bind == STB_WEAK;
1192 oldweak = (h->root.type == bfd_link_hash_defweak
1193 || h->root.type == bfd_link_hash_undefweak);
1194 if (pold_weak)
1195 *pold_weak = oldweak;
1196
1197 /* We have to check it for every instance since the first few may be
1198 references and not all compilers emit symbol type for undefined
1199 symbols. */
1200 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1201
1202 htab = elf_hash_table (info);
1203
1204 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1205 respectively, is from a dynamic object. */
1206
1207 newdyn = (abfd->flags & DYNAMIC) != 0;
1208
1209 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1210 syms and defined syms in dynamic libraries respectively.
1211 ref_dynamic on the other hand can be set for a symbol defined in
1212 a dynamic library, and def_dynamic may not be set; When the
1213 definition in a dynamic lib is overridden by a definition in the
1214 executable use of the symbol in the dynamic lib becomes a
1215 reference to the executable symbol. */
1216 if (newdyn)
1217 {
1218 if (bfd_is_und_section (sec))
1219 {
1220 if (bind != STB_WEAK)
1221 {
1222 h->ref_dynamic_nonweak = 1;
1223 hi->ref_dynamic_nonweak = 1;
1224 }
1225 }
1226 else
1227 {
1228 /* Update the existing symbol only if they match. */
1229 if (*matched)
1230 h->dynamic_def = 1;
1231 hi->dynamic_def = 1;
1232 }
1233 }
1234
1235 /* If we just created the symbol, mark it as being an ELF symbol.
1236 Other than that, there is nothing to do--there is no merge issue
1237 with a newly defined symbol--so we just return. */
1238
1239 if (h->root.type == bfd_link_hash_new)
1240 {
1241 h->non_elf = 0;
1242 return true;
1243 }
1244
1245 /* In cases involving weak versioned symbols, we may wind up trying
1246 to merge a symbol with itself. Catch that here, to avoid the
1247 confusion that results if we try to override a symbol with
1248 itself. The additional tests catch cases like
1249 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1250 dynamic object, which we do want to handle here. */
1251 if (abfd == oldbfd
1252 && (newweak || oldweak)
1253 && ((abfd->flags & DYNAMIC) == 0
1254 || !h->def_regular))
1255 return true;
1256
1257 olddyn = false;
1258 if (oldbfd != NULL)
1259 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1260 else if (oldsec != NULL)
1261 {
1262 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1263 indices used by MIPS ELF. */
1264 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1265 }
1266
1267 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1268 if (!htab->handling_dt_needed
1269 && oldbfd != NULL
1270 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1271 {
1272 if (newdyn != olddyn)
1273 {
1274 /* Handle a case where plugin_notice won't be called and thus
1275 won't set the non_ir_ref flags on the first pass over
1276 symbols. */
1277 h->root.non_ir_ref_dynamic = true;
1278 hi->root.non_ir_ref_dynamic = true;
1279 }
1280 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1281 && hi->root.type == bfd_link_hash_indirect)
1282 {
1283 /* Change indirect symbol from IR to undefined. */
1284 hi->root.type = bfd_link_hash_undefined;
1285 hi->root.u.undef.abfd = oldbfd;
1286 }
1287 }
1288
1289 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1290 respectively, appear to be a definition rather than reference. */
1291
1292 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1293
1294 olddef = (h->root.type != bfd_link_hash_undefined
1295 && h->root.type != bfd_link_hash_undefweak
1296 && h->root.type != bfd_link_hash_common);
1297
1298 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1299 respectively, appear to be a function. */
1300
1301 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1302 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1303
1304 oldfunc = (h->type != STT_NOTYPE
1305 && bed->is_function_type (h->type));
1306
1307 if (!(newfunc && oldfunc)
1308 && ELF_ST_TYPE (sym->st_info) != h->type
1309 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1310 && h->type != STT_NOTYPE
1311 && (newdef || bfd_is_com_section (sec))
1312 && (olddef || h->root.type == bfd_link_hash_common))
1313 {
1314 /* If creating a default indirect symbol ("foo" or "foo@") from
1315 a dynamic versioned definition ("foo@@") skip doing so if
1316 there is an existing regular definition with a different
1317 type. We don't want, for example, a "time" variable in the
1318 executable overriding a "time" function in a shared library. */
1319 if (newdyn
1320 && !olddyn)
1321 {
1322 *skip = true;
1323 return true;
1324 }
1325
1326 /* When adding a symbol from a regular object file after we have
1327 created indirect symbols, undo the indirection and any
1328 dynamic state. */
1329 if (hi != h
1330 && !newdyn
1331 && olddyn)
1332 {
1333 h = hi;
1334 (*bed->elf_backend_hide_symbol) (info, h, true);
1335 h->forced_local = 0;
1336 h->ref_dynamic = 0;
1337 h->def_dynamic = 0;
1338 h->dynamic_def = 0;
1339 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1340 {
1341 h->root.type = bfd_link_hash_undefined;
1342 h->root.u.undef.abfd = abfd;
1343 }
1344 else
1345 {
1346 h->root.type = bfd_link_hash_new;
1347 h->root.u.undef.abfd = NULL;
1348 }
1349 return true;
1350 }
1351 }
1352
1353 /* Check TLS symbols. We don't check undefined symbols introduced
1354 by "ld -u" which have no type (and oldbfd NULL), and we don't
1355 check symbols from plugins because they also have no type. */
1356 if (oldbfd != NULL
1357 && (oldbfd->flags & BFD_PLUGIN) == 0
1358 && (abfd->flags & BFD_PLUGIN) == 0
1359 && ELF_ST_TYPE (sym->st_info) != h->type
1360 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1361 {
1362 bfd *ntbfd, *tbfd;
1363 bool ntdef, tdef;
1364 asection *ntsec, *tsec;
1365
1366 if (h->type == STT_TLS)
1367 {
1368 ntbfd = abfd;
1369 ntsec = sec;
1370 ntdef = newdef;
1371 tbfd = oldbfd;
1372 tsec = oldsec;
1373 tdef = olddef;
1374 }
1375 else
1376 {
1377 ntbfd = oldbfd;
1378 ntsec = oldsec;
1379 ntdef = olddef;
1380 tbfd = abfd;
1381 tsec = sec;
1382 tdef = newdef;
1383 }
1384
1385 if (tdef && ntdef)
1386 _bfd_error_handler
1387 /* xgettext:c-format */
1388 (_("%s: TLS definition in %pB section %pA "
1389 "mismatches non-TLS definition in %pB section %pA"),
1390 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1391 else if (!tdef && !ntdef)
1392 _bfd_error_handler
1393 /* xgettext:c-format */
1394 (_("%s: TLS reference in %pB "
1395 "mismatches non-TLS reference in %pB"),
1396 h->root.root.string, tbfd, ntbfd);
1397 else if (tdef)
1398 _bfd_error_handler
1399 /* xgettext:c-format */
1400 (_("%s: TLS definition in %pB section %pA "
1401 "mismatches non-TLS reference in %pB"),
1402 h->root.root.string, tbfd, tsec, ntbfd);
1403 else
1404 _bfd_error_handler
1405 /* xgettext:c-format */
1406 (_("%s: TLS reference in %pB "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h->root.root.string, tbfd, ntbfd, ntsec);
1409
1410 bfd_set_error (bfd_error_bad_value);
1411 return false;
1412 }
1413
1414 /* If the old symbol has non-default visibility, we ignore the new
1415 definition from a dynamic object. */
1416 if (newdyn
1417 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1418 && !bfd_is_und_section (sec))
1419 {
1420 *skip = true;
1421 /* Make sure this symbol is dynamic. */
1422 h->ref_dynamic = 1;
1423 hi->ref_dynamic = 1;
1424 /* A protected symbol has external availability. Make sure it is
1425 recorded as dynamic.
1426
1427 FIXME: Should we check type and size for protected symbol? */
1428 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1429 return bfd_elf_link_record_dynamic_symbol (info, h);
1430 else
1431 return true;
1432 }
1433 else if (!newdyn
1434 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1435 && h->def_dynamic)
1436 {
1437 /* If the new symbol with non-default visibility comes from a
1438 relocatable file and the old definition comes from a dynamic
1439 object, we remove the old definition. */
1440 if (hi->root.type == bfd_link_hash_indirect)
1441 {
1442 /* Handle the case where the old dynamic definition is
1443 default versioned. We need to copy the symbol info from
1444 the symbol with default version to the normal one if it
1445 was referenced before. */
1446 if (h->ref_regular)
1447 {
1448 hi->root.type = h->root.type;
1449 h->root.type = bfd_link_hash_indirect;
1450 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1451
1452 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1453 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1454 {
1455 /* If the new symbol is hidden or internal, completely undo
1456 any dynamic link state. */
1457 (*bed->elf_backend_hide_symbol) (info, h, true);
1458 h->forced_local = 0;
1459 h->ref_dynamic = 0;
1460 }
1461 else
1462 h->ref_dynamic = 1;
1463
1464 h->def_dynamic = 0;
1465 /* FIXME: Should we check type and size for protected symbol? */
1466 h->size = 0;
1467 h->type = 0;
1468
1469 h = hi;
1470 }
1471 else
1472 h = hi;
1473 }
1474
1475 /* If the old symbol was undefined before, then it will still be
1476 on the undefs list. If the new symbol is undefined or
1477 common, we can't make it bfd_link_hash_new here, because new
1478 undefined or common symbols will be added to the undefs list
1479 by _bfd_generic_link_add_one_symbol. Symbols may not be
1480 added twice to the undefs list. Also, if the new symbol is
1481 undefweak then we don't want to lose the strong undef. */
1482 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1483 {
1484 h->root.type = bfd_link_hash_undefined;
1485 h->root.u.undef.abfd = abfd;
1486 }
1487 else
1488 {
1489 h->root.type = bfd_link_hash_new;
1490 h->root.u.undef.abfd = NULL;
1491 }
1492
1493 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1494 {
1495 /* If the new symbol is hidden or internal, completely undo
1496 any dynamic link state. */
1497 (*bed->elf_backend_hide_symbol) (info, h, true);
1498 h->forced_local = 0;
1499 h->ref_dynamic = 0;
1500 }
1501 else
1502 h->ref_dynamic = 1;
1503 h->def_dynamic = 0;
1504 /* FIXME: Should we check type and size for protected symbol? */
1505 h->size = 0;
1506 h->type = 0;
1507 return true;
1508 }
1509
1510 /* If a new weak symbol definition comes from a regular file and the
1511 old symbol comes from a dynamic library, we treat the new one as
1512 strong. Similarly, an old weak symbol definition from a regular
1513 file is treated as strong when the new symbol comes from a dynamic
1514 library. Further, an old weak symbol from a dynamic library is
1515 treated as strong if the new symbol is from a dynamic library.
1516 This reflects the way glibc's ld.so works.
1517
1518 Also allow a weak symbol to override a linker script symbol
1519 defined by an early pass over the script. This is done so the
1520 linker knows the symbol is defined in an object file, for the
1521 DEFINED script function.
1522
1523 Do this before setting *type_change_ok or *size_change_ok so that
1524 we warn properly when dynamic library symbols are overridden. */
1525
1526 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1527 newweak = false;
1528 if (olddef && newdyn)
1529 oldweak = false;
1530
1531 /* Allow changes between different types of function symbol. */
1532 if (newfunc && oldfunc)
1533 *type_change_ok = true;
1534
1535 /* It's OK to change the type if either the existing symbol or the
1536 new symbol is weak. A type change is also OK if the old symbol
1537 is undefined and the new symbol is defined. */
1538
1539 if (oldweak
1540 || newweak
1541 || (newdef
1542 && h->root.type == bfd_link_hash_undefined))
1543 *type_change_ok = true;
1544
1545 /* It's OK to change the size if either the existing symbol or the
1546 new symbol is weak, or if the old symbol is undefined. */
1547
1548 if (*type_change_ok
1549 || h->root.type == bfd_link_hash_undefined)
1550 *size_change_ok = true;
1551
1552 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1553 symbol, respectively, appears to be a common symbol in a dynamic
1554 object. If a symbol appears in an uninitialized section, and is
1555 not weak, and is not a function, then it may be a common symbol
1556 which was resolved when the dynamic object was created. We want
1557 to treat such symbols specially, because they raise special
1558 considerations when setting the symbol size: if the symbol
1559 appears as a common symbol in a regular object, and the size in
1560 the regular object is larger, we must make sure that we use the
1561 larger size. This problematic case can always be avoided in C,
1562 but it must be handled correctly when using Fortran shared
1563 libraries.
1564
1565 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1566 likewise for OLDDYNCOMMON and OLDDEF.
1567
1568 Note that this test is just a heuristic, and that it is quite
1569 possible to have an uninitialized symbol in a shared object which
1570 is really a definition, rather than a common symbol. This could
1571 lead to some minor confusion when the symbol really is a common
1572 symbol in some regular object. However, I think it will be
1573 harmless. */
1574
1575 if (newdyn
1576 && newdef
1577 && !newweak
1578 && (sec->flags & SEC_ALLOC) != 0
1579 && (sec->flags & SEC_LOAD) == 0
1580 && sym->st_size > 0
1581 && !newfunc)
1582 newdyncommon = true;
1583 else
1584 newdyncommon = false;
1585
1586 if (olddyn
1587 && olddef
1588 && h->root.type == bfd_link_hash_defined
1589 && h->def_dynamic
1590 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1591 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1592 && h->size > 0
1593 && !oldfunc)
1594 olddyncommon = true;
1595 else
1596 olddyncommon = false;
1597
1598 /* We now know everything about the old and new symbols. We ask the
1599 backend to check if we can merge them. */
1600 if (bed->merge_symbol != NULL)
1601 {
1602 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1603 return false;
1604 sec = *psec;
1605 }
1606
1607 /* There are multiple definitions of a normal symbol. Skip the
1608 default symbol as well as definition from an IR object. */
1609 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1610 && !default_sym && h->def_regular
1611 && !(oldbfd != NULL
1612 && (oldbfd->flags & BFD_PLUGIN) != 0
1613 && (abfd->flags & BFD_PLUGIN) == 0))
1614 {
1615 /* Handle a multiple definition. */
1616 (*info->callbacks->multiple_definition) (info, &h->root,
1617 abfd, sec, *pvalue);
1618 *skip = true;
1619 return true;
1620 }
1621
1622 /* If both the old and the new symbols look like common symbols in a
1623 dynamic object, set the size of the symbol to the larger of the
1624 two. */
1625
1626 if (olddyncommon
1627 && newdyncommon
1628 && sym->st_size != h->size)
1629 {
1630 /* Since we think we have two common symbols, issue a multiple
1631 common warning if desired. Note that we only warn if the
1632 size is different. If the size is the same, we simply let
1633 the old symbol override the new one as normally happens with
1634 symbols defined in dynamic objects. */
1635
1636 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1637 bfd_link_hash_common, sym->st_size);
1638 if (sym->st_size > h->size)
1639 h->size = sym->st_size;
1640
1641 *size_change_ok = true;
1642 }
1643
1644 /* If we are looking at a dynamic object, and we have found a
1645 definition, we need to see if the symbol was already defined by
1646 some other object. If so, we want to use the existing
1647 definition, and we do not want to report a multiple symbol
1648 definition error; we do this by clobbering *PSEC to be
1649 bfd_und_section_ptr.
1650
1651 We treat a common symbol as a definition if the symbol in the
1652 shared library is a function, since common symbols always
1653 represent variables; this can cause confusion in principle, but
1654 any such confusion would seem to indicate an erroneous program or
1655 shared library. We also permit a common symbol in a regular
1656 object to override a weak symbol in a shared object. */
1657
1658 if (newdyn
1659 && newdef
1660 && (olddef
1661 || (h->root.type == bfd_link_hash_common
1662 && (newweak || newfunc))))
1663 {
1664 *override = abfd;
1665 newdef = false;
1666 newdyncommon = false;
1667
1668 *psec = sec = bfd_und_section_ptr;
1669 *size_change_ok = true;
1670
1671 /* If we get here when the old symbol is a common symbol, then
1672 we are explicitly letting it override a weak symbol or
1673 function in a dynamic object, and we don't want to warn about
1674 a type change. If the old symbol is a defined symbol, a type
1675 change warning may still be appropriate. */
1676
1677 if (h->root.type == bfd_link_hash_common)
1678 *type_change_ok = true;
1679 }
1680
1681 /* Handle the special case of an old common symbol merging with a
1682 new symbol which looks like a common symbol in a shared object.
1683 We change *PSEC and *PVALUE to make the new symbol look like a
1684 common symbol, and let _bfd_generic_link_add_one_symbol do the
1685 right thing. */
1686
1687 if (newdyncommon
1688 && h->root.type == bfd_link_hash_common)
1689 {
1690 *override = oldbfd;
1691 newdef = false;
1692 newdyncommon = false;
1693 *pvalue = sym->st_size;
1694 *psec = sec = bed->common_section (oldsec);
1695 *size_change_ok = true;
1696 }
1697
1698 /* Skip weak definitions of symbols that are already defined. */
1699 if (newdef && olddef && newweak)
1700 {
1701 /* Don't skip new non-IR weak syms. */
1702 if (!(oldbfd != NULL
1703 && (oldbfd->flags & BFD_PLUGIN) != 0
1704 && (abfd->flags & BFD_PLUGIN) == 0))
1705 {
1706 newdef = false;
1707 *skip = true;
1708 }
1709
1710 /* Merge st_other. If the symbol already has a dynamic index,
1711 but visibility says it should not be visible, turn it into a
1712 local symbol. */
1713 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1714 if (h->dynindx != -1)
1715 switch (ELF_ST_VISIBILITY (h->other))
1716 {
1717 case STV_INTERNAL:
1718 case STV_HIDDEN:
1719 (*bed->elf_backend_hide_symbol) (info, h, true);
1720 break;
1721 }
1722 }
1723
1724 /* If the old symbol is from a dynamic object, and the new symbol is
1725 a definition which is not from a dynamic object, then the new
1726 symbol overrides the old symbol. Symbols from regular files
1727 always take precedence over symbols from dynamic objects, even if
1728 they are defined after the dynamic object in the link.
1729
1730 As above, we again permit a common symbol in a regular object to
1731 override a definition in a shared object if the shared object
1732 symbol is a function or is weak. */
1733
1734 flip = NULL;
1735 if (!newdyn
1736 && (newdef
1737 || (bfd_is_com_section (sec)
1738 && (oldweak || oldfunc)))
1739 && olddyn
1740 && olddef
1741 && h->def_dynamic)
1742 {
1743 /* Change the hash table entry to undefined, and let
1744 _bfd_generic_link_add_one_symbol do the right thing with the
1745 new definition. */
1746
1747 h->root.type = bfd_link_hash_undefined;
1748 h->root.u.undef.abfd = h->root.u.def.section->owner;
1749 *size_change_ok = true;
1750
1751 olddef = false;
1752 olddyncommon = false;
1753
1754 /* We again permit a type change when a common symbol may be
1755 overriding a function. */
1756
1757 if (bfd_is_com_section (sec))
1758 {
1759 if (oldfunc)
1760 {
1761 /* If a common symbol overrides a function, make sure
1762 that it isn't defined dynamically nor has type
1763 function. */
1764 h->def_dynamic = 0;
1765 h->type = STT_NOTYPE;
1766 }
1767 *type_change_ok = true;
1768 }
1769
1770 if (hi->root.type == bfd_link_hash_indirect)
1771 flip = hi;
1772 else
1773 /* This union may have been set to be non-NULL when this symbol
1774 was seen in a dynamic object. We must force the union to be
1775 NULL, so that it is correct for a regular symbol. */
1776 h->verinfo.vertree = NULL;
1777 }
1778
1779 /* Handle the special case of a new common symbol merging with an
1780 old symbol that looks like it might be a common symbol defined in
1781 a shared object. Note that we have already handled the case in
1782 which a new common symbol should simply override the definition
1783 in the shared library. */
1784
1785 if (! newdyn
1786 && bfd_is_com_section (sec)
1787 && olddyncommon)
1788 {
1789 /* It would be best if we could set the hash table entry to a
1790 common symbol, but we don't know what to use for the section
1791 or the alignment. */
1792 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1793 bfd_link_hash_common, sym->st_size);
1794
1795 /* If the presumed common symbol in the dynamic object is
1796 larger, pretend that the new symbol has its size. */
1797
1798 if (h->size > *pvalue)
1799 *pvalue = h->size;
1800
1801 /* We need to remember the alignment required by the symbol
1802 in the dynamic object. */
1803 BFD_ASSERT (pold_alignment);
1804 *pold_alignment = h->root.u.def.section->alignment_power;
1805
1806 olddef = false;
1807 olddyncommon = false;
1808
1809 h->root.type = bfd_link_hash_undefined;
1810 h->root.u.undef.abfd = h->root.u.def.section->owner;
1811
1812 *size_change_ok = true;
1813 *type_change_ok = true;
1814
1815 if (hi->root.type == bfd_link_hash_indirect)
1816 flip = hi;
1817 else
1818 h->verinfo.vertree = NULL;
1819 }
1820
1821 if (flip != NULL)
1822 {
1823 /* Handle the case where we had a versioned symbol in a dynamic
1824 library and now find a definition in a normal object. In this
1825 case, we make the versioned symbol point to the normal one. */
1826 flip->root.type = h->root.type;
1827 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1828 h->root.type = bfd_link_hash_indirect;
1829 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1830 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1831 if (h->def_dynamic)
1832 {
1833 h->def_dynamic = 0;
1834 flip->ref_dynamic = 1;
1835 }
1836 }
1837
1838 return true;
1839 }
1840
1841 /* This function is called to create an indirect symbol from the
1842 default for the symbol with the default version if needed. The
1843 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1844 set DYNSYM if the new indirect symbol is dynamic. */
1845
1846 static bool
1847 _bfd_elf_add_default_symbol (bfd *abfd,
1848 struct bfd_link_info *info,
1849 struct elf_link_hash_entry *h,
1850 const char *name,
1851 Elf_Internal_Sym *sym,
1852 asection *sec,
1853 bfd_vma value,
1854 bfd **poldbfd,
1855 bool *dynsym)
1856 {
1857 bool type_change_ok;
1858 bool size_change_ok;
1859 bool skip;
1860 char *shortname;
1861 struct elf_link_hash_entry *hi;
1862 struct bfd_link_hash_entry *bh;
1863 const struct elf_backend_data *bed;
1864 bool collect;
1865 bool dynamic;
1866 bfd *override;
1867 char *p;
1868 size_t len, shortlen;
1869 asection *tmp_sec;
1870 bool matched;
1871
1872 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1873 return true;
1874
1875 /* If this symbol has a version, and it is the default version, we
1876 create an indirect symbol from the default name to the fully
1877 decorated name. This will cause external references which do not
1878 specify a version to be bound to this version of the symbol. */
1879 p = strchr (name, ELF_VER_CHR);
1880 if (h->versioned == unknown)
1881 {
1882 if (p == NULL)
1883 {
1884 h->versioned = unversioned;
1885 return true;
1886 }
1887 else
1888 {
1889 if (p[1] != ELF_VER_CHR)
1890 {
1891 h->versioned = versioned_hidden;
1892 return true;
1893 }
1894 else
1895 h->versioned = versioned;
1896 }
1897 }
1898 else
1899 {
1900 /* PR ld/19073: We may see an unversioned definition after the
1901 default version. */
1902 if (p == NULL)
1903 return true;
1904 }
1905
1906 bed = get_elf_backend_data (abfd);
1907 collect = bed->collect;
1908 dynamic = (abfd->flags & DYNAMIC) != 0;
1909
1910 shortlen = p - name;
1911 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1912 if (shortname == NULL)
1913 return false;
1914 memcpy (shortname, name, shortlen);
1915 shortname[shortlen] = '\0';
1916
1917 /* We are going to create a new symbol. Merge it with any existing
1918 symbol with this name. For the purposes of the merge, act as
1919 though we were defining the symbol we just defined, although we
1920 actually going to define an indirect symbol. */
1921 type_change_ok = false;
1922 size_change_ok = false;
1923 matched = true;
1924 tmp_sec = sec;
1925 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1926 &hi, poldbfd, NULL, NULL, &skip, &override,
1927 &type_change_ok, &size_change_ok, &matched))
1928 return false;
1929
1930 if (skip)
1931 goto nondefault;
1932
1933 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1934 {
1935 /* If the undecorated symbol will have a version added by a
1936 script different to H, then don't indirect to/from the
1937 undecorated symbol. This isn't ideal because we may not yet
1938 have seen symbol versions, if given by a script on the
1939 command line rather than via --version-script. */
1940 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1941 {
1942 bool hide;
1943
1944 hi->verinfo.vertree
1945 = bfd_find_version_for_sym (info->version_info,
1946 hi->root.root.string, &hide);
1947 if (hi->verinfo.vertree != NULL && hide)
1948 {
1949 (*bed->elf_backend_hide_symbol) (info, hi, true);
1950 goto nondefault;
1951 }
1952 }
1953 if (hi->verinfo.vertree != NULL
1954 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1955 goto nondefault;
1956 }
1957
1958 if (! override)
1959 {
1960 /* Add the default symbol if not performing a relocatable link. */
1961 if (! bfd_link_relocatable (info))
1962 {
1963 bh = &hi->root;
1964 if (bh->type == bfd_link_hash_defined
1965 && bh->u.def.section->owner != NULL
1966 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1967 {
1968 /* Mark the previous definition from IR object as
1969 undefined so that the generic linker will override
1970 it. */
1971 bh->type = bfd_link_hash_undefined;
1972 bh->u.undef.abfd = bh->u.def.section->owner;
1973 }
1974 if (! (_bfd_generic_link_add_one_symbol
1975 (info, abfd, shortname, BSF_INDIRECT,
1976 bfd_ind_section_ptr,
1977 0, name, false, collect, &bh)))
1978 return false;
1979 hi = (struct elf_link_hash_entry *) bh;
1980 }
1981 }
1982 else
1983 {
1984 /* In this case the symbol named SHORTNAME is overriding the
1985 indirect symbol we want to add. We were planning on making
1986 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1987 is the name without a version. NAME is the fully versioned
1988 name, and it is the default version.
1989
1990 Overriding means that we already saw a definition for the
1991 symbol SHORTNAME in a regular object, and it is overriding
1992 the symbol defined in the dynamic object.
1993
1994 When this happens, we actually want to change NAME, the
1995 symbol we just added, to refer to SHORTNAME. This will cause
1996 references to NAME in the shared object to become references
1997 to SHORTNAME in the regular object. This is what we expect
1998 when we override a function in a shared object: that the
1999 references in the shared object will be mapped to the
2000 definition in the regular object. */
2001
2002 while (hi->root.type == bfd_link_hash_indirect
2003 || hi->root.type == bfd_link_hash_warning)
2004 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2005
2006 h->root.type = bfd_link_hash_indirect;
2007 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2008 if (h->def_dynamic)
2009 {
2010 h->def_dynamic = 0;
2011 hi->ref_dynamic = 1;
2012 if (hi->ref_regular
2013 || hi->def_regular)
2014 {
2015 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2016 return false;
2017 }
2018 }
2019
2020 /* Now set HI to H, so that the following code will set the
2021 other fields correctly. */
2022 hi = h;
2023 }
2024
2025 /* Check if HI is a warning symbol. */
2026 if (hi->root.type == bfd_link_hash_warning)
2027 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2028
2029 /* If there is a duplicate definition somewhere, then HI may not
2030 point to an indirect symbol. We will have reported an error to
2031 the user in that case. */
2032
2033 if (hi->root.type == bfd_link_hash_indirect)
2034 {
2035 struct elf_link_hash_entry *ht;
2036
2037 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2038 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2039
2040 /* If we first saw a reference to SHORTNAME with non-default
2041 visibility, merge that visibility to the @@VER symbol. */
2042 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2043
2044 /* A reference to the SHORTNAME symbol from a dynamic library
2045 will be satisfied by the versioned symbol at runtime. In
2046 effect, we have a reference to the versioned symbol. */
2047 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2048 hi->dynamic_def |= ht->dynamic_def;
2049
2050 /* See if the new flags lead us to realize that the symbol must
2051 be dynamic. */
2052 if (! *dynsym)
2053 {
2054 if (! dynamic)
2055 {
2056 if (! bfd_link_executable (info)
2057 || hi->def_dynamic
2058 || hi->ref_dynamic)
2059 *dynsym = true;
2060 }
2061 else
2062 {
2063 if (hi->ref_regular)
2064 *dynsym = true;
2065 }
2066 }
2067 }
2068
2069 /* We also need to define an indirection from the nondefault version
2070 of the symbol. */
2071
2072 nondefault:
2073 len = strlen (name);
2074 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2075 if (shortname == NULL)
2076 return false;
2077 memcpy (shortname, name, shortlen);
2078 memcpy (shortname + shortlen, p + 1, len - shortlen);
2079
2080 /* Once again, merge with any existing symbol. */
2081 type_change_ok = false;
2082 size_change_ok = false;
2083 tmp_sec = sec;
2084 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2085 &hi, poldbfd, NULL, NULL, &skip, &override,
2086 &type_change_ok, &size_change_ok, &matched))
2087 return false;
2088
2089 if (skip)
2090 {
2091 if (!dynamic
2092 && h->root.type == bfd_link_hash_defweak
2093 && hi->root.type == bfd_link_hash_defined)
2094 {
2095 /* We are handling a weak sym@@ver and attempting to define
2096 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2097 new weak sym@ver because there is already a strong sym@ver.
2098 However, sym@ver and sym@@ver are really the same symbol.
2099 The existing strong sym@ver ought to override sym@@ver. */
2100 h->root.type = bfd_link_hash_defined;
2101 h->root.u.def.section = hi->root.u.def.section;
2102 h->root.u.def.value = hi->root.u.def.value;
2103 hi->root.type = bfd_link_hash_indirect;
2104 hi->root.u.i.link = &h->root;
2105 }
2106 else
2107 return true;
2108 }
2109 else if (override)
2110 {
2111 /* Here SHORTNAME is a versioned name, so we don't expect to see
2112 the type of override we do in the case above unless it is
2113 overridden by a versioned definition. */
2114 if (hi->root.type != bfd_link_hash_defined
2115 && hi->root.type != bfd_link_hash_defweak)
2116 _bfd_error_handler
2117 /* xgettext:c-format */
2118 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2119 abfd, shortname);
2120 return true;
2121 }
2122 else
2123 {
2124 bh = &hi->root;
2125 if (! (_bfd_generic_link_add_one_symbol
2126 (info, abfd, shortname, BSF_INDIRECT,
2127 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2128 return false;
2129 hi = (struct elf_link_hash_entry *) bh;
2130 }
2131
2132 /* If there is a duplicate definition somewhere, then HI may not
2133 point to an indirect symbol. We will have reported an error
2134 to the user in that case. */
2135 if (hi->root.type == bfd_link_hash_indirect)
2136 {
2137 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2138 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2139 hi->dynamic_def |= h->dynamic_def;
2140
2141 /* If we first saw a reference to @VER symbol with
2142 non-default visibility, merge that visibility to the
2143 @@VER symbol. */
2144 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2145
2146 /* See if the new flags lead us to realize that the symbol
2147 must be dynamic. */
2148 if (! *dynsym)
2149 {
2150 if (! dynamic)
2151 {
2152 if (! bfd_link_executable (info)
2153 || hi->ref_dynamic)
2154 *dynsym = true;
2155 }
2156 else
2157 {
2158 if (hi->ref_regular)
2159 *dynsym = true;
2160 }
2161 }
2162 }
2163
2164 return true;
2165 }
2166 \f
2167 /* This routine is used to export all defined symbols into the dynamic
2168 symbol table. It is called via elf_link_hash_traverse. */
2169
2170 static bool
2171 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2172 {
2173 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2174
2175 /* Ignore indirect symbols. These are added by the versioning code. */
2176 if (h->root.type == bfd_link_hash_indirect)
2177 return true;
2178
2179 /* Ignore this if we won't export it. */
2180 if (!eif->info->export_dynamic && !h->dynamic)
2181 return true;
2182
2183 if (h->dynindx == -1
2184 && (h->def_regular || h->ref_regular)
2185 && ! bfd_hide_sym_by_version (eif->info->version_info,
2186 h->root.root.string))
2187 {
2188 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2189 {
2190 eif->failed = true;
2191 return false;
2192 }
2193 }
2194
2195 return true;
2196 }
2197 \f
2198 /* Return the glibc version reference if VERSION_DEP is added to the
2199 list of glibc version dependencies successfully. VERSION_DEP will
2200 be put into the .gnu.version_r section. */
2201
2202 static Elf_Internal_Verneed *
2203 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2204 Elf_Internal_Verneed *glibc_verref,
2205 const char *version_dep)
2206 {
2207 Elf_Internal_Verneed *t;
2208 Elf_Internal_Vernaux *a;
2209 size_t amt;
2210
2211 if (glibc_verref != NULL)
2212 {
2213 t = glibc_verref;
2214
2215 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2216 {
2217 /* Return if VERSION_DEP dependency has been added. */
2218 if (a->vna_nodename == version_dep
2219 || strcmp (a->vna_nodename, version_dep) == 0)
2220 return t;
2221 }
2222 }
2223 else
2224 {
2225 bool is_glibc;
2226
2227 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2228 t != NULL;
2229 t = t->vn_nextref)
2230 {
2231 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2232 if (soname != NULL && startswith (soname, "libc.so."))
2233 break;
2234 }
2235
2236 /* Skip the shared library if it isn't libc.so. */
2237 if (t == NULL)
2238 return t;
2239
2240 is_glibc = false;
2241 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2242 {
2243 /* Return if VERSION_DEP dependency has been added. */
2244 if (a->vna_nodename == version_dep
2245 || strcmp (a->vna_nodename, version_dep) == 0)
2246 return t;
2247
2248 /* Check if libc.so provides GLIBC_2.XX version. */
2249 if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2250 is_glibc = true;
2251 }
2252
2253 /* Skip if it isn't linked against glibc. */
2254 if (!is_glibc)
2255 return NULL;
2256 }
2257
2258 amt = sizeof *a;
2259 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2260 if (a == NULL)
2261 {
2262 rinfo->failed = true;
2263 return NULL;
2264 }
2265
2266 a->vna_nodename = version_dep;
2267 a->vna_flags = 0;
2268 a->vna_nextptr = t->vn_auxptr;
2269 a->vna_other = rinfo->vers + 1;
2270 ++rinfo->vers;
2271
2272 t->vn_auxptr = a;
2273
2274 return t;
2275 }
2276
2277 /* Add VERSION_DEP to the list of version dependencies when linked
2278 against glibc. */
2279
2280 void
2281 _bfd_elf_link_add_glibc_version_dependency
2282 (struct elf_find_verdep_info *rinfo,
2283 const char *version_dep[])
2284 {
2285 Elf_Internal_Verneed *t = NULL;
2286
2287 do
2288 {
2289 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2290 /* Return if there is no glibc version reference. */
2291 if (t == NULL)
2292 return;
2293 version_dep++;
2294 }
2295 while (*version_dep != NULL);
2296 }
2297
2298 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2299 linked against glibc. */
2300
2301 void
2302 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2303 {
2304 if (rinfo->info->enable_dt_relr)
2305 {
2306 const char *version[] =
2307 {
2308 "GLIBC_ABI_DT_RELR",
2309 NULL
2310 };
2311 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2312 }
2313 }
2314
2315 /* Look through the symbols which are defined in other shared
2316 libraries and referenced here. Update the list of version
2317 dependencies. This will be put into the .gnu.version_r section.
2318 This function is called via elf_link_hash_traverse. */
2319
2320 static bool
2321 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2322 void *data)
2323 {
2324 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2325 Elf_Internal_Verneed *t;
2326 Elf_Internal_Vernaux *a;
2327 size_t amt;
2328
2329 /* We only care about symbols defined in shared objects with version
2330 information. */
2331 if (!h->def_dynamic
2332 || h->def_regular
2333 || h->dynindx == -1
2334 || h->verinfo.verdef == NULL
2335 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2336 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2337 return true;
2338
2339 /* See if we already know about this version. */
2340 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2341 t != NULL;
2342 t = t->vn_nextref)
2343 {
2344 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2345 continue;
2346
2347 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2348 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2349 return true;
2350
2351 break;
2352 }
2353
2354 /* This is a new version. Add it to tree we are building. */
2355
2356 if (t == NULL)
2357 {
2358 amt = sizeof *t;
2359 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2360 if (t == NULL)
2361 {
2362 rinfo->failed = true;
2363 return false;
2364 }
2365
2366 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2367 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2368 elf_tdata (rinfo->info->output_bfd)->verref = t;
2369 }
2370
2371 amt = sizeof *a;
2372 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2373 if (a == NULL)
2374 {
2375 rinfo->failed = true;
2376 return false;
2377 }
2378
2379 /* Note that we are copying a string pointer here, and testing it
2380 above. If bfd_elf_string_from_elf_section is ever changed to
2381 discard the string data when low in memory, this will have to be
2382 fixed. */
2383 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2384
2385 a->vna_flags = h->verinfo.verdef->vd_flags;
2386 a->vna_nextptr = t->vn_auxptr;
2387
2388 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2389 ++rinfo->vers;
2390
2391 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2392
2393 t->vn_auxptr = a;
2394
2395 return true;
2396 }
2397
2398 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2399 hidden. Set *T_P to NULL if there is no match. */
2400
2401 static bool
2402 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2403 struct elf_link_hash_entry *h,
2404 const char *version_p,
2405 struct bfd_elf_version_tree **t_p,
2406 bool *hide)
2407 {
2408 struct bfd_elf_version_tree *t;
2409
2410 /* Look for the version. If we find it, it is no longer weak. */
2411 for (t = info->version_info; t != NULL; t = t->next)
2412 {
2413 if (strcmp (t->name, version_p) == 0)
2414 {
2415 size_t len;
2416 char *alc;
2417 struct bfd_elf_version_expr *d;
2418
2419 len = version_p - h->root.root.string;
2420 alc = (char *) bfd_malloc (len);
2421 if (alc == NULL)
2422 return false;
2423 memcpy (alc, h->root.root.string, len - 1);
2424 alc[len - 1] = '\0';
2425 if (alc[len - 2] == ELF_VER_CHR)
2426 alc[len - 2] = '\0';
2427
2428 h->verinfo.vertree = t;
2429 t->used = true;
2430 d = NULL;
2431
2432 if (t->globals.list != NULL)
2433 d = (*t->match) (&t->globals, NULL, alc);
2434
2435 /* See if there is anything to force this symbol to
2436 local scope. */
2437 if (d == NULL && t->locals.list != NULL)
2438 {
2439 d = (*t->match) (&t->locals, NULL, alc);
2440 if (d != NULL
2441 && h->dynindx != -1
2442 && ! info->export_dynamic)
2443 *hide = true;
2444 }
2445
2446 free (alc);
2447 break;
2448 }
2449 }
2450
2451 *t_p = t;
2452
2453 return true;
2454 }
2455
2456 /* Return TRUE if the symbol H is hidden by version script. */
2457
2458 bool
2459 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2460 struct elf_link_hash_entry *h)
2461 {
2462 const char *p;
2463 bool hide = false;
2464 const struct elf_backend_data *bed
2465 = get_elf_backend_data (info->output_bfd);
2466
2467 /* Version script only hides symbols defined in regular objects. */
2468 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2469 return true;
2470
2471 p = strchr (h->root.root.string, ELF_VER_CHR);
2472 if (p != NULL && h->verinfo.vertree == NULL)
2473 {
2474 struct bfd_elf_version_tree *t;
2475
2476 ++p;
2477 if (*p == ELF_VER_CHR)
2478 ++p;
2479
2480 if (*p != '\0'
2481 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2482 && hide)
2483 {
2484 if (hide)
2485 (*bed->elf_backend_hide_symbol) (info, h, true);
2486 return true;
2487 }
2488 }
2489
2490 /* If we don't have a version for this symbol, see if we can find
2491 something. */
2492 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2493 {
2494 h->verinfo.vertree
2495 = bfd_find_version_for_sym (info->version_info,
2496 h->root.root.string, &hide);
2497 if (h->verinfo.vertree != NULL && hide)
2498 {
2499 (*bed->elf_backend_hide_symbol) (info, h, true);
2500 return true;
2501 }
2502 }
2503
2504 return false;
2505 }
2506
2507 /* Figure out appropriate versions for all the symbols. We may not
2508 have the version number script until we have read all of the input
2509 files, so until that point we don't know which symbols should be
2510 local. This function is called via elf_link_hash_traverse. */
2511
2512 static bool
2513 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2514 {
2515 struct elf_info_failed *sinfo;
2516 struct bfd_link_info *info;
2517 const struct elf_backend_data *bed;
2518 struct elf_info_failed eif;
2519 char *p;
2520 bool hide;
2521
2522 sinfo = (struct elf_info_failed *) data;
2523 info = sinfo->info;
2524
2525 /* Fix the symbol flags. */
2526 eif.failed = false;
2527 eif.info = info;
2528 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2529 {
2530 if (eif.failed)
2531 sinfo->failed = true;
2532 return false;
2533 }
2534
2535 bed = get_elf_backend_data (info->output_bfd);
2536
2537 /* We only need version numbers for symbols defined in regular
2538 objects. */
2539 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2540 {
2541 /* Hide symbols defined in discarded input sections. */
2542 if ((h->root.type == bfd_link_hash_defined
2543 || h->root.type == bfd_link_hash_defweak)
2544 && discarded_section (h->root.u.def.section))
2545 (*bed->elf_backend_hide_symbol) (info, h, true);
2546 return true;
2547 }
2548
2549 hide = false;
2550 p = strchr (h->root.root.string, ELF_VER_CHR);
2551 if (p != NULL && h->verinfo.vertree == NULL)
2552 {
2553 struct bfd_elf_version_tree *t;
2554
2555 ++p;
2556 if (*p == ELF_VER_CHR)
2557 ++p;
2558
2559 /* If there is no version string, we can just return out. */
2560 if (*p == '\0')
2561 return true;
2562
2563 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2564 {
2565 sinfo->failed = true;
2566 return false;
2567 }
2568
2569 if (hide)
2570 (*bed->elf_backend_hide_symbol) (info, h, true);
2571
2572 /* If we are building an application, we need to create a
2573 version node for this version. */
2574 if (t == NULL && bfd_link_executable (info))
2575 {
2576 struct bfd_elf_version_tree **pp;
2577 int version_index;
2578
2579 /* If we aren't going to export this symbol, we don't need
2580 to worry about it. */
2581 if (h->dynindx == -1)
2582 return true;
2583
2584 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2585 sizeof *t);
2586 if (t == NULL)
2587 {
2588 sinfo->failed = true;
2589 return false;
2590 }
2591
2592 t->name = p;
2593 t->name_indx = (unsigned int) -1;
2594 t->used = true;
2595
2596 version_index = 1;
2597 /* Don't count anonymous version tag. */
2598 if (sinfo->info->version_info != NULL
2599 && sinfo->info->version_info->vernum == 0)
2600 version_index = 0;
2601 for (pp = &sinfo->info->version_info;
2602 *pp != NULL;
2603 pp = &(*pp)->next)
2604 ++version_index;
2605 t->vernum = version_index;
2606
2607 *pp = t;
2608
2609 h->verinfo.vertree = t;
2610 }
2611 else if (t == NULL)
2612 {
2613 /* We could not find the version for a symbol when
2614 generating a shared archive. Return an error. */
2615 _bfd_error_handler
2616 /* xgettext:c-format */
2617 (_("%pB: version node not found for symbol %s"),
2618 info->output_bfd, h->root.root.string);
2619 bfd_set_error (bfd_error_bad_value);
2620 sinfo->failed = true;
2621 return false;
2622 }
2623 }
2624
2625 /* If we don't have a version for this symbol, see if we can find
2626 something. */
2627 if (!hide
2628 && h->verinfo.vertree == NULL
2629 && sinfo->info->version_info != NULL)
2630 {
2631 h->verinfo.vertree
2632 = bfd_find_version_for_sym (sinfo->info->version_info,
2633 h->root.root.string, &hide);
2634 if (h->verinfo.vertree != NULL && hide)
2635 (*bed->elf_backend_hide_symbol) (info, h, true);
2636 }
2637
2638 return true;
2639 }
2640 \f
2641 /* Read and swap the relocs from the section indicated by SHDR. This
2642 may be either a REL or a RELA section. The relocations are
2643 translated into RELA relocations and stored in INTERNAL_RELOCS,
2644 which should have already been allocated to contain enough space.
2645 The EXTERNAL_RELOCS are a buffer where the external form of the
2646 relocations should be stored.
2647
2648 Returns FALSE if something goes wrong. */
2649
2650 static bool
2651 elf_link_read_relocs_from_section (bfd *abfd,
2652 asection *sec,
2653 Elf_Internal_Shdr *shdr,
2654 void *external_relocs,
2655 Elf_Internal_Rela *internal_relocs)
2656 {
2657 const struct elf_backend_data *bed;
2658 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2659 const bfd_byte *erela;
2660 const bfd_byte *erelaend;
2661 Elf_Internal_Rela *irela;
2662 Elf_Internal_Shdr *symtab_hdr;
2663 size_t nsyms;
2664
2665 /* Position ourselves at the start of the section. */
2666 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2667 return false;
2668
2669 /* Read the relocations. */
2670 if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2671 return false;
2672
2673 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2674 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2675
2676 bed = get_elf_backend_data (abfd);
2677
2678 /* Convert the external relocations to the internal format. */
2679 if (shdr->sh_entsize == bed->s->sizeof_rel)
2680 swap_in = bed->s->swap_reloc_in;
2681 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2682 swap_in = bed->s->swap_reloca_in;
2683 else
2684 {
2685 bfd_set_error (bfd_error_wrong_format);
2686 return false;
2687 }
2688
2689 erela = (const bfd_byte *) external_relocs;
2690 /* Setting erelaend like this and comparing with <= handles case of
2691 a fuzzed object with sh_size not a multiple of sh_entsize. */
2692 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2693 irela = internal_relocs;
2694 while (erela <= erelaend)
2695 {
2696 bfd_vma r_symndx;
2697
2698 (*swap_in) (abfd, erela, irela);
2699 r_symndx = ELF32_R_SYM (irela->r_info);
2700 if (bed->s->arch_size == 64)
2701 r_symndx >>= 24;
2702 if (nsyms > 0)
2703 {
2704 if ((size_t) r_symndx >= nsyms)
2705 {
2706 _bfd_error_handler
2707 /* xgettext:c-format */
2708 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2709 " for offset %#" PRIx64 " in section `%pA'"),
2710 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2711 (uint64_t) irela->r_offset, sec);
2712 bfd_set_error (bfd_error_bad_value);
2713 return false;
2714 }
2715 }
2716 else if (r_symndx != STN_UNDEF)
2717 {
2718 _bfd_error_handler
2719 /* xgettext:c-format */
2720 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2721 " for offset %#" PRIx64 " in section `%pA'"
2722 " when the object file has no symbol table"),
2723 abfd, (uint64_t) r_symndx,
2724 (uint64_t) irela->r_offset, sec);
2725 bfd_set_error (bfd_error_bad_value);
2726 return false;
2727 }
2728 irela += bed->s->int_rels_per_ext_rel;
2729 erela += shdr->sh_entsize;
2730 }
2731
2732 return true;
2733 }
2734
2735 /* Read and swap the relocs for a section O. They may have been
2736 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2737 not NULL, they are used as buffers to read into. They are known to
2738 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2739 the return value is allocated using either malloc or bfd_alloc,
2740 according to the KEEP_MEMORY argument. If O has two relocation
2741 sections (both REL and RELA relocations), then the REL_HDR
2742 relocations will appear first in INTERNAL_RELOCS, followed by the
2743 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2744 update cache_size. */
2745
2746 Elf_Internal_Rela *
2747 _bfd_elf_link_info_read_relocs (bfd *abfd,
2748 struct bfd_link_info *info,
2749 asection *o,
2750 void *external_relocs,
2751 Elf_Internal_Rela *internal_relocs,
2752 bool keep_memory)
2753 {
2754 void *alloc1 = NULL;
2755 Elf_Internal_Rela *alloc2 = NULL;
2756 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2757 struct bfd_elf_section_data *esdo = elf_section_data (o);
2758 Elf_Internal_Rela *internal_rela_relocs;
2759
2760 if (esdo->relocs != NULL)
2761 return esdo->relocs;
2762
2763 if (o->reloc_count == 0)
2764 return NULL;
2765
2766 if (internal_relocs == NULL)
2767 {
2768 bfd_size_type size;
2769
2770 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2771 if (keep_memory)
2772 {
2773 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2774 if (info)
2775 info->cache_size += size;
2776 }
2777 else
2778 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2779 if (internal_relocs == NULL)
2780 goto error_return;
2781 }
2782
2783 if (external_relocs == NULL)
2784 {
2785 bfd_size_type size = 0;
2786
2787 if (esdo->rel.hdr)
2788 size += esdo->rel.hdr->sh_size;
2789 if (esdo->rela.hdr)
2790 size += esdo->rela.hdr->sh_size;
2791
2792 alloc1 = bfd_malloc (size);
2793 if (alloc1 == NULL)
2794 goto error_return;
2795 external_relocs = alloc1;
2796 }
2797
2798 internal_rela_relocs = internal_relocs;
2799 if (esdo->rel.hdr)
2800 {
2801 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2802 external_relocs,
2803 internal_relocs))
2804 goto error_return;
2805 external_relocs = (((bfd_byte *) external_relocs)
2806 + esdo->rel.hdr->sh_size);
2807 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2808 * bed->s->int_rels_per_ext_rel);
2809 }
2810
2811 if (esdo->rela.hdr
2812 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2813 external_relocs,
2814 internal_rela_relocs)))
2815 goto error_return;
2816
2817 /* Cache the results for next time, if we can. */
2818 if (keep_memory)
2819 esdo->relocs = internal_relocs;
2820
2821 free (alloc1);
2822
2823 /* Don't free alloc2, since if it was allocated we are passing it
2824 back (under the name of internal_relocs). */
2825
2826 return internal_relocs;
2827
2828 error_return:
2829 free (alloc1);
2830 if (alloc2 != NULL)
2831 {
2832 if (keep_memory)
2833 bfd_release (abfd, alloc2);
2834 else
2835 free (alloc2);
2836 }
2837 return NULL;
2838 }
2839
2840 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2841 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2842 struct bfd_link_info. */
2843
2844 Elf_Internal_Rela *
2845 _bfd_elf_link_read_relocs (bfd *abfd,
2846 asection *o,
2847 void *external_relocs,
2848 Elf_Internal_Rela *internal_relocs,
2849 bool keep_memory)
2850 {
2851 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2852 internal_relocs, keep_memory);
2853
2854 }
2855
2856 /* Compute the size of, and allocate space for, REL_HDR which is the
2857 section header for a section containing relocations for O. */
2858
2859 static bool
2860 _bfd_elf_link_size_reloc_section (bfd *abfd,
2861 struct bfd_elf_section_reloc_data *reldata)
2862 {
2863 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2864
2865 /* That allows us to calculate the size of the section. */
2866 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2867
2868 /* The contents field must last into write_object_contents, so we
2869 allocate it with bfd_alloc rather than malloc. Also since we
2870 cannot be sure that the contents will actually be filled in,
2871 we zero the allocated space. */
2872 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2873 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2874 return false;
2875
2876 if (reldata->hashes == NULL && reldata->count)
2877 {
2878 struct elf_link_hash_entry **p;
2879
2880 p = ((struct elf_link_hash_entry **)
2881 bfd_zmalloc (reldata->count * sizeof (*p)));
2882 if (p == NULL)
2883 return false;
2884
2885 reldata->hashes = p;
2886 }
2887
2888 return true;
2889 }
2890
2891 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2892 originated from the section given by INPUT_REL_HDR) to the
2893 OUTPUT_BFD. */
2894
2895 bool
2896 _bfd_elf_link_output_relocs (bfd *output_bfd,
2897 asection *input_section,
2898 Elf_Internal_Shdr *input_rel_hdr,
2899 Elf_Internal_Rela *internal_relocs,
2900 struct elf_link_hash_entry **rel_hash
2901 ATTRIBUTE_UNUSED)
2902 {
2903 Elf_Internal_Rela *irela;
2904 Elf_Internal_Rela *irelaend;
2905 bfd_byte *erel;
2906 struct bfd_elf_section_reloc_data *output_reldata;
2907 asection *output_section;
2908 const struct elf_backend_data *bed;
2909 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2910 struct bfd_elf_section_data *esdo;
2911
2912 output_section = input_section->output_section;
2913
2914 bed = get_elf_backend_data (output_bfd);
2915 esdo = elf_section_data (output_section);
2916 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2917 {
2918 output_reldata = &esdo->rel;
2919 swap_out = bed->s->swap_reloc_out;
2920 }
2921 else if (esdo->rela.hdr
2922 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2923 {
2924 output_reldata = &esdo->rela;
2925 swap_out = bed->s->swap_reloca_out;
2926 }
2927 else
2928 {
2929 _bfd_error_handler
2930 /* xgettext:c-format */
2931 (_("%pB: relocation size mismatch in %pB section %pA"),
2932 output_bfd, input_section->owner, input_section);
2933 bfd_set_error (bfd_error_wrong_format);
2934 return false;
2935 }
2936
2937 erel = output_reldata->hdr->contents;
2938 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2939 irela = internal_relocs;
2940 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2941 * bed->s->int_rels_per_ext_rel);
2942 while (irela < irelaend)
2943 {
2944 (*swap_out) (output_bfd, irela, erel);
2945 irela += bed->s->int_rels_per_ext_rel;
2946 erel += input_rel_hdr->sh_entsize;
2947 }
2948
2949 /* Bump the counter, so that we know where to add the next set of
2950 relocations. */
2951 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2952
2953 return true;
2954 }
2955 \f
2956 /* Make weak undefined symbols in PIE dynamic. */
2957
2958 bool
2959 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2960 struct elf_link_hash_entry *h)
2961 {
2962 if (bfd_link_pie (info)
2963 && h->dynindx == -1
2964 && h->root.type == bfd_link_hash_undefweak)
2965 return bfd_elf_link_record_dynamic_symbol (info, h);
2966
2967 return true;
2968 }
2969
2970 /* Fix up the flags for a symbol. This handles various cases which
2971 can only be fixed after all the input files are seen. This is
2972 currently called by both adjust_dynamic_symbol and
2973 assign_sym_version, which is unnecessary but perhaps more robust in
2974 the face of future changes. */
2975
2976 static bool
2977 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2978 struct elf_info_failed *eif)
2979 {
2980 const struct elf_backend_data *bed;
2981
2982 /* If this symbol was mentioned in a non-ELF file, try to set
2983 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2984 permit a non-ELF file to correctly refer to a symbol defined in
2985 an ELF dynamic object. */
2986 if (h->non_elf)
2987 {
2988 while (h->root.type == bfd_link_hash_indirect)
2989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2990
2991 if (h->root.type != bfd_link_hash_defined
2992 && h->root.type != bfd_link_hash_defweak)
2993 {
2994 h->ref_regular = 1;
2995 h->ref_regular_nonweak = 1;
2996 }
2997 else
2998 {
2999 if (h->root.u.def.section->owner != NULL
3000 && (bfd_get_flavour (h->root.u.def.section->owner)
3001 == bfd_target_elf_flavour))
3002 {
3003 h->ref_regular = 1;
3004 h->ref_regular_nonweak = 1;
3005 }
3006 else
3007 h->def_regular = 1;
3008 }
3009
3010 if (h->dynindx == -1
3011 && (h->def_dynamic
3012 || h->ref_dynamic))
3013 {
3014 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3015 {
3016 eif->failed = true;
3017 return false;
3018 }
3019 }
3020 }
3021 else
3022 {
3023 /* Unfortunately, NON_ELF is only correct if the symbol
3024 was first seen in a non-ELF file. Fortunately, if the symbol
3025 was first seen in an ELF file, we're probably OK unless the
3026 symbol was defined in a non-ELF file. Catch that case here.
3027 FIXME: We're still in trouble if the symbol was first seen in
3028 a dynamic object, and then later in a non-ELF regular object. */
3029 if ((h->root.type == bfd_link_hash_defined
3030 || h->root.type == bfd_link_hash_defweak)
3031 && !h->def_regular
3032 && (h->root.u.def.section->owner != NULL
3033 ? (bfd_get_flavour (h->root.u.def.section->owner)
3034 != bfd_target_elf_flavour)
3035 : (bfd_is_abs_section (h->root.u.def.section)
3036 && !h->def_dynamic)))
3037 h->def_regular = 1;
3038 }
3039
3040 /* Backend specific symbol fixup. */
3041 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3042 if (bed->elf_backend_fixup_symbol
3043 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3044 return false;
3045
3046 /* If this is a final link, and the symbol was defined as a common
3047 symbol in a regular object file, and there was no definition in
3048 any dynamic object, then the linker will have allocated space for
3049 the symbol in a common section but the DEF_REGULAR
3050 flag will not have been set. */
3051 if (h->root.type == bfd_link_hash_defined
3052 && !h->def_regular
3053 && h->ref_regular
3054 && !h->def_dynamic
3055 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3056 h->def_regular = 1;
3057
3058 /* Symbols defined in discarded sections shouldn't be dynamic. */
3059 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3060 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3061
3062 /* If a weak undefined symbol has non-default visibility, we also
3063 hide it from the dynamic linker. */
3064 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3065 && h->root.type == bfd_link_hash_undefweak)
3066 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3067
3068 /* A hidden versioned symbol in executable should be forced local if
3069 it is is locally defined, not referenced by shared library and not
3070 exported. */
3071 else if (bfd_link_executable (eif->info)
3072 && h->versioned == versioned_hidden
3073 && !eif->info->export_dynamic
3074 && !h->dynamic
3075 && !h->ref_dynamic
3076 && h->def_regular)
3077 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3078
3079 /* If -Bsymbolic was used (which means to bind references to global
3080 symbols to the definition within the shared object), and this
3081 symbol was defined in a regular object, then it actually doesn't
3082 need a PLT entry. Likewise, if the symbol has non-default
3083 visibility. If the symbol has hidden or internal visibility, we
3084 will force it local. */
3085 else if (h->needs_plt
3086 && bfd_link_pic (eif->info)
3087 && is_elf_hash_table (eif->info->hash)
3088 && (SYMBOLIC_BIND (eif->info, h)
3089 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3090 && h->def_regular)
3091 {
3092 bool force_local;
3093
3094 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3095 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3096 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3097 }
3098
3099 /* If this is a weak defined symbol in a dynamic object, and we know
3100 the real definition in the dynamic object, copy interesting flags
3101 over to the real definition. */
3102 if (h->is_weakalias)
3103 {
3104 struct elf_link_hash_entry *def = weakdef (h);
3105
3106 /* If the real definition is defined by a regular object file,
3107 don't do anything special. See the longer description in
3108 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3109 bfd_link_hash_defined as it was when put on the alias list
3110 then it must have originally been a versioned symbol (for
3111 which a non-versioned indirect symbol is created) and later
3112 a definition for the non-versioned symbol is found. In that
3113 case the indirection is flipped with the versioned symbol
3114 becoming an indirect pointing at the non-versioned symbol.
3115 Thus, not an alias any more. */
3116 if (def->def_regular
3117 || def->root.type != bfd_link_hash_defined)
3118 {
3119 h = def;
3120 while ((h = h->u.alias) != def)
3121 h->is_weakalias = 0;
3122 }
3123 else
3124 {
3125 while (h->root.type == bfd_link_hash_indirect)
3126 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3127 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak);
3129 BFD_ASSERT (def->def_dynamic);
3130 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3131 }
3132 }
3133
3134 return true;
3135 }
3136
3137 /* Make the backend pick a good value for a dynamic symbol. This is
3138 called via elf_link_hash_traverse, and also calls itself
3139 recursively. */
3140
3141 static bool
3142 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3143 {
3144 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3145 struct elf_link_hash_table *htab;
3146 const struct elf_backend_data *bed;
3147
3148 if (! is_elf_hash_table (eif->info->hash))
3149 return false;
3150
3151 /* Ignore indirect symbols. These are added by the versioning code. */
3152 if (h->root.type == bfd_link_hash_indirect)
3153 return true;
3154
3155 /* Fix the symbol flags. */
3156 if (! _bfd_elf_fix_symbol_flags (h, eif))
3157 return false;
3158
3159 htab = elf_hash_table (eif->info);
3160 bed = get_elf_backend_data (htab->dynobj);
3161
3162 if (h->root.type == bfd_link_hash_undefweak)
3163 {
3164 if (eif->info->dynamic_undefined_weak == 0)
3165 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3166 else if (eif->info->dynamic_undefined_weak > 0
3167 && h->ref_regular
3168 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3169 && !bfd_hide_sym_by_version (eif->info->version_info,
3170 h->root.root.string))
3171 {
3172 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3173 {
3174 eif->failed = true;
3175 return false;
3176 }
3177 }
3178 }
3179
3180 /* If this symbol does not require a PLT entry, and it is not
3181 defined by a dynamic object, or is not referenced by a regular
3182 object, ignore it. We do have to handle a weak defined symbol,
3183 even if no regular object refers to it, if we decided to add it
3184 to the dynamic symbol table. FIXME: Do we normally need to worry
3185 about symbols which are defined by one dynamic object and
3186 referenced by another one? */
3187 if (!h->needs_plt
3188 && h->type != STT_GNU_IFUNC
3189 && (h->def_regular
3190 || !h->def_dynamic
3191 || (!h->ref_regular
3192 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3193 {
3194 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3195 return true;
3196 }
3197
3198 /* If we've already adjusted this symbol, don't do it again. This
3199 can happen via a recursive call. */
3200 if (h->dynamic_adjusted)
3201 return true;
3202
3203 /* Don't look at this symbol again. Note that we must set this
3204 after checking the above conditions, because we may look at a
3205 symbol once, decide not to do anything, and then get called
3206 recursively later after REF_REGULAR is set below. */
3207 h->dynamic_adjusted = 1;
3208
3209 /* If this is a weak definition, and we know a real definition, and
3210 the real symbol is not itself defined by a regular object file,
3211 then get a good value for the real definition. We handle the
3212 real symbol first, for the convenience of the backend routine.
3213
3214 Note that there is a confusing case here. If the real definition
3215 is defined by a regular object file, we don't get the real symbol
3216 from the dynamic object, but we do get the weak symbol. If the
3217 processor backend uses a COPY reloc, then if some routine in the
3218 dynamic object changes the real symbol, we will not see that
3219 change in the corresponding weak symbol. This is the way other
3220 ELF linkers work as well, and seems to be a result of the shared
3221 library model.
3222
3223 I will clarify this issue. Most SVR4 shared libraries define the
3224 variable _timezone and define timezone as a weak synonym. The
3225 tzset call changes _timezone. If you write
3226 extern int timezone;
3227 int _timezone = 5;
3228 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3229 you might expect that, since timezone is a synonym for _timezone,
3230 the same number will print both times. However, if the processor
3231 backend uses a COPY reloc, then actually timezone will be copied
3232 into your process image, and, since you define _timezone
3233 yourself, _timezone will not. Thus timezone and _timezone will
3234 wind up at different memory locations. The tzset call will set
3235 _timezone, leaving timezone unchanged. */
3236
3237 if (h->is_weakalias)
3238 {
3239 struct elf_link_hash_entry *def = weakdef (h);
3240
3241 /* If we get to this point, there is an implicit reference to
3242 the alias by a regular object file via the weak symbol H. */
3243 def->ref_regular = 1;
3244
3245 /* Ensure that the backend adjust_dynamic_symbol function sees
3246 the strong alias before H by recursively calling ourselves. */
3247 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3248 return false;
3249 }
3250
3251 /* If a symbol has no type and no size and does not require a PLT
3252 entry, then we are probably about to do the wrong thing here: we
3253 are probably going to create a COPY reloc for an empty object.
3254 This case can arise when a shared object is built with assembly
3255 code, and the assembly code fails to set the symbol type. */
3256 if (h->size == 0
3257 && h->type == STT_NOTYPE
3258 && !h->needs_plt)
3259 _bfd_error_handler
3260 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3261 h->root.root.string);
3262
3263 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3264 {
3265 eif->failed = true;
3266 return false;
3267 }
3268
3269 return true;
3270 }
3271
3272 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3273 DYNBSS. */
3274
3275 bool
3276 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3277 struct elf_link_hash_entry *h,
3278 asection *dynbss)
3279 {
3280 unsigned int power_of_two;
3281 bfd_vma mask;
3282 asection *sec = h->root.u.def.section;
3283
3284 /* The section alignment of the definition is the maximum alignment
3285 requirement of symbols defined in the section. Since we don't
3286 know the symbol alignment requirement, we start with the
3287 maximum alignment and check low bits of the symbol address
3288 for the minimum alignment. */
3289 power_of_two = bfd_section_alignment (sec);
3290 mask = ((bfd_vma) 1 << power_of_two) - 1;
3291 while ((h->root.u.def.value & mask) != 0)
3292 {
3293 mask >>= 1;
3294 --power_of_two;
3295 }
3296
3297 if (power_of_two > bfd_section_alignment (dynbss))
3298 {
3299 /* Adjust the section alignment if needed. */
3300 if (!bfd_set_section_alignment (dynbss, power_of_two))
3301 return false;
3302 }
3303
3304 /* We make sure that the symbol will be aligned properly. */
3305 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3306
3307 /* Define the symbol as being at this point in DYNBSS. */
3308 h->root.u.def.section = dynbss;
3309 h->root.u.def.value = dynbss->size;
3310
3311 /* Increment the size of DYNBSS to make room for the symbol. */
3312 dynbss->size += h->size;
3313
3314 /* No error if extern_protected_data is true. */
3315 if (h->protected_def
3316 && (!info->extern_protected_data
3317 || (info->extern_protected_data < 0
3318 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3319 info->callbacks->einfo
3320 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3321 h->root.root.string);
3322
3323 return true;
3324 }
3325
3326 /* Adjust all external symbols pointing into SEC_MERGE sections
3327 to reflect the object merging within the sections. */
3328
3329 static bool
3330 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3331 {
3332 asection *sec;
3333
3334 if ((h->root.type == bfd_link_hash_defined
3335 || h->root.type == bfd_link_hash_defweak)
3336 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3337 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3338 {
3339 bfd *output_bfd = (bfd *) data;
3340
3341 h->root.u.def.value =
3342 _bfd_merged_section_offset (output_bfd,
3343 &h->root.u.def.section,
3344 elf_section_data (sec)->sec_info,
3345 h->root.u.def.value);
3346 }
3347
3348 return true;
3349 }
3350
3351 /* Returns false if the symbol referred to by H should be considered
3352 to resolve local to the current module, and true if it should be
3353 considered to bind dynamically. */
3354
3355 bool
3356 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3357 struct bfd_link_info *info,
3358 bool not_local_protected)
3359 {
3360 bool binding_stays_local_p;
3361 const struct elf_backend_data *bed;
3362 struct elf_link_hash_table *hash_table;
3363
3364 if (h == NULL)
3365 return false;
3366
3367 while (h->root.type == bfd_link_hash_indirect
3368 || h->root.type == bfd_link_hash_warning)
3369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3370
3371 /* If it was forced local, then clearly it's not dynamic. */
3372 if (h->dynindx == -1)
3373 return false;
3374 if (h->forced_local)
3375 return false;
3376
3377 /* Identify the cases where name binding rules say that a
3378 visible symbol resolves locally. */
3379 binding_stays_local_p = (bfd_link_executable (info)
3380 || SYMBOLIC_BIND (info, h));
3381
3382 switch (ELF_ST_VISIBILITY (h->other))
3383 {
3384 case STV_INTERNAL:
3385 case STV_HIDDEN:
3386 return false;
3387
3388 case STV_PROTECTED:
3389 hash_table = elf_hash_table (info);
3390 if (!is_elf_hash_table (&hash_table->root))
3391 return false;
3392
3393 bed = get_elf_backend_data (hash_table->dynobj);
3394
3395 /* Proper resolution for function pointer equality may require
3396 that these symbols perhaps be resolved dynamically, even though
3397 we should be resolving them to the current module. */
3398 if (!not_local_protected || !bed->is_function_type (h->type))
3399 binding_stays_local_p = true;
3400 break;
3401
3402 default:
3403 break;
3404 }
3405
3406 /* If it isn't defined locally, then clearly it's dynamic. */
3407 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3408 return true;
3409
3410 /* Otherwise, the symbol is dynamic if binding rules don't tell
3411 us that it remains local. */
3412 return !binding_stays_local_p;
3413 }
3414
3415 /* Return true if the symbol referred to by H should be considered
3416 to resolve local to the current module, and false otherwise. Differs
3417 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3418 undefined symbols. The two functions are virtually identical except
3419 for the place where dynindx == -1 is tested. If that test is true,
3420 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3421 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3422 defined symbols.
3423 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3424 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3425 treatment of undefined weak symbols. For those that do not make
3426 undefined weak symbols dynamic, both functions may return false. */
3427
3428 bool
3429 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3430 struct bfd_link_info *info,
3431 bool local_protected)
3432 {
3433 const struct elf_backend_data *bed;
3434 struct elf_link_hash_table *hash_table;
3435
3436 /* If it's a local sym, of course we resolve locally. */
3437 if (h == NULL)
3438 return true;
3439
3440 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3441 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3442 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3443 return true;
3444
3445 /* Forced local symbols resolve locally. */
3446 if (h->forced_local)
3447 return true;
3448
3449 /* Common symbols that become definitions don't get the DEF_REGULAR
3450 flag set, so test it first, and don't bail out. */
3451 if (ELF_COMMON_DEF_P (h))
3452 /* Do nothing. */;
3453 /* If we don't have a definition in a regular file, then we can't
3454 resolve locally. The sym is either undefined or dynamic. */
3455 else if (!h->def_regular)
3456 return false;
3457
3458 /* Non-dynamic symbols resolve locally. */
3459 if (h->dynindx == -1)
3460 return true;
3461
3462 /* At this point, we know the symbol is defined and dynamic. In an
3463 executable it must resolve locally, likewise when building symbolic
3464 shared libraries. */
3465 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3466 return true;
3467
3468 /* Now deal with defined dynamic symbols in shared libraries. Ones
3469 with default visibility might not resolve locally. */
3470 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3471 return false;
3472
3473 hash_table = elf_hash_table (info);
3474 if (!is_elf_hash_table (&hash_table->root))
3475 return true;
3476
3477 /* STV_PROTECTED symbols with indirect external access are local. */
3478 if (info->indirect_extern_access > 0)
3479 return true;
3480
3481 bed = get_elf_backend_data (hash_table->dynobj);
3482
3483 /* If extern_protected_data is false, STV_PROTECTED non-function
3484 symbols are local. */
3485 if ((!info->extern_protected_data
3486 || (info->extern_protected_data < 0
3487 && !bed->extern_protected_data))
3488 && !bed->is_function_type (h->type))
3489 return true;
3490
3491 /* Function pointer equality tests may require that STV_PROTECTED
3492 symbols be treated as dynamic symbols. If the address of a
3493 function not defined in an executable is set to that function's
3494 plt entry in the executable, then the address of the function in
3495 a shared library must also be the plt entry in the executable. */
3496 return local_protected;
3497 }
3498
3499 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3500 aligned. Returns the first TLS output section. */
3501
3502 struct bfd_section *
3503 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3504 {
3505 struct bfd_section *sec, *tls;
3506 unsigned int align = 0;
3507
3508 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3509 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3510 break;
3511 tls = sec;
3512
3513 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3514 if (sec->alignment_power > align)
3515 align = sec->alignment_power;
3516
3517 elf_hash_table (info)->tls_sec = tls;
3518
3519 /* Ensure the alignment of the first section (usually .tdata) is the largest
3520 alignment, so that the tls segment starts aligned. */
3521 if (tls != NULL)
3522 tls->alignment_power = align;
3523
3524 return tls;
3525 }
3526
3527 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3528 static bool
3529 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3530 Elf_Internal_Sym *sym)
3531 {
3532 const struct elf_backend_data *bed;
3533
3534 /* Local symbols do not count, but target specific ones might. */
3535 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3536 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3537 return false;
3538
3539 bed = get_elf_backend_data (abfd);
3540 /* Function symbols do not count. */
3541 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3542 return false;
3543
3544 /* If the section is undefined, then so is the symbol. */
3545 if (sym->st_shndx == SHN_UNDEF)
3546 return false;
3547
3548 /* If the symbol is defined in the common section, then
3549 it is a common definition and so does not count. */
3550 if (bed->common_definition (sym))
3551 return false;
3552
3553 /* If the symbol is in a target specific section then we
3554 must rely upon the backend to tell us what it is. */
3555 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3556 /* FIXME - this function is not coded yet:
3557
3558 return _bfd_is_global_symbol_definition (abfd, sym);
3559
3560 Instead for now assume that the definition is not global,
3561 Even if this is wrong, at least the linker will behave
3562 in the same way that it used to do. */
3563 return false;
3564
3565 return true;
3566 }
3567
3568 /* Search the symbol table of the archive element of the archive ABFD
3569 whose archive map contains a mention of SYMDEF, and determine if
3570 the symbol is defined in this element. */
3571 static bool
3572 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3573 {
3574 Elf_Internal_Shdr * hdr;
3575 size_t symcount;
3576 size_t extsymcount;
3577 size_t extsymoff;
3578 Elf_Internal_Sym *isymbuf;
3579 Elf_Internal_Sym *isym;
3580 Elf_Internal_Sym *isymend;
3581 bool result;
3582
3583 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3584 if (abfd == NULL)
3585 return false;
3586
3587 if (! bfd_check_format (abfd, bfd_object))
3588 return false;
3589
3590 /* Select the appropriate symbol table. If we don't know if the
3591 object file is an IR object, give linker LTO plugin a chance to
3592 get the correct symbol table. */
3593 if (abfd->plugin_format == bfd_plugin_yes
3594 #if BFD_SUPPORTS_PLUGINS
3595 || (abfd->plugin_format == bfd_plugin_unknown
3596 && bfd_link_plugin_object_p (abfd))
3597 #endif
3598 )
3599 {
3600 /* Use the IR symbol table if the object has been claimed by
3601 plugin. */
3602 abfd = abfd->plugin_dummy_bfd;
3603 hdr = &elf_tdata (abfd)->symtab_hdr;
3604 }
3605 else
3606 {
3607 if (elf_use_dt_symtab_p (abfd))
3608 {
3609 bfd_set_error (bfd_error_wrong_format);
3610 return false;
3611 }
3612
3613 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3614 hdr = &elf_tdata (abfd)->symtab_hdr;
3615 else
3616 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3617 }
3618
3619 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3620
3621 /* The sh_info field of the symtab header tells us where the
3622 external symbols start. We don't care about the local symbols. */
3623 if (elf_bad_symtab (abfd))
3624 {
3625 extsymcount = symcount;
3626 extsymoff = 0;
3627 }
3628 else
3629 {
3630 extsymcount = symcount - hdr->sh_info;
3631 extsymoff = hdr->sh_info;
3632 }
3633
3634 if (extsymcount == 0)
3635 return false;
3636
3637 /* Read in the symbol table. */
3638 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3639 NULL, NULL, NULL);
3640 if (isymbuf == NULL)
3641 return false;
3642
3643 /* Scan the symbol table looking for SYMDEF. */
3644 result = false;
3645 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3646 {
3647 const char *name;
3648
3649 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3650 isym->st_name);
3651 if (name == NULL)
3652 break;
3653
3654 if (strcmp (name, symdef->name) == 0)
3655 {
3656 result = is_global_data_symbol_definition (abfd, isym);
3657 break;
3658 }
3659 }
3660
3661 free (isymbuf);
3662
3663 return result;
3664 }
3665 \f
3666 /* Add an entry to the .dynamic table. */
3667
3668 bool
3669 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3670 bfd_vma tag,
3671 bfd_vma val)
3672 {
3673 struct elf_link_hash_table *hash_table;
3674 const struct elf_backend_data *bed;
3675 asection *s;
3676 bfd_size_type newsize;
3677 bfd_byte *newcontents;
3678 Elf_Internal_Dyn dyn;
3679
3680 hash_table = elf_hash_table (info);
3681 if (! is_elf_hash_table (&hash_table->root))
3682 return false;
3683
3684 if (tag == DT_RELA || tag == DT_REL)
3685 hash_table->dynamic_relocs = true;
3686
3687 bed = get_elf_backend_data (hash_table->dynobj);
3688 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3689 BFD_ASSERT (s != NULL);
3690
3691 newsize = s->size + bed->s->sizeof_dyn;
3692 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3693 if (newcontents == NULL)
3694 return false;
3695
3696 dyn.d_tag = tag;
3697 dyn.d_un.d_val = val;
3698 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3699
3700 s->size = newsize;
3701 s->contents = newcontents;
3702
3703 return true;
3704 }
3705
3706 /* Strip zero-sized dynamic sections. */
3707
3708 bool
3709 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3710 {
3711 struct elf_link_hash_table *hash_table;
3712 const struct elf_backend_data *bed;
3713 asection *s, *sdynamic, **pp;
3714 asection *rela_dyn, *rel_dyn;
3715 Elf_Internal_Dyn dyn;
3716 bfd_byte *extdyn, *next;
3717 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3718 bool strip_zero_sized;
3719 bool strip_zero_sized_plt;
3720
3721 if (bfd_link_relocatable (info))
3722 return true;
3723
3724 hash_table = elf_hash_table (info);
3725 if (!is_elf_hash_table (&hash_table->root))
3726 return false;
3727
3728 if (!hash_table->dynobj)
3729 return true;
3730
3731 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3732 if (!sdynamic)
3733 return true;
3734
3735 bed = get_elf_backend_data (hash_table->dynobj);
3736 swap_dyn_in = bed->s->swap_dyn_in;
3737
3738 strip_zero_sized = false;
3739 strip_zero_sized_plt = false;
3740
3741 /* Strip zero-sized dynamic sections. */
3742 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3743 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3744 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3745 if (s->size == 0
3746 && (s == rela_dyn
3747 || s == rel_dyn
3748 || s == hash_table->srelplt->output_section
3749 || s == hash_table->splt->output_section))
3750 {
3751 *pp = s->next;
3752 info->output_bfd->section_count--;
3753 strip_zero_sized = true;
3754 if (s == rela_dyn)
3755 s = rela_dyn;
3756 if (s == rel_dyn)
3757 s = rel_dyn;
3758 else if (s == hash_table->splt->output_section)
3759 {
3760 s = hash_table->splt;
3761 strip_zero_sized_plt = true;
3762 }
3763 else
3764 s = hash_table->srelplt;
3765 s->flags |= SEC_EXCLUDE;
3766 s->output_section = bfd_abs_section_ptr;
3767 }
3768 else
3769 pp = &s->next;
3770
3771 if (strip_zero_sized_plt && sdynamic->size != 0)
3772 for (extdyn = sdynamic->contents;
3773 extdyn < sdynamic->contents + sdynamic->size;
3774 extdyn = next)
3775 {
3776 next = extdyn + bed->s->sizeof_dyn;
3777 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3778 switch (dyn.d_tag)
3779 {
3780 default:
3781 break;
3782 case DT_JMPREL:
3783 case DT_PLTRELSZ:
3784 case DT_PLTREL:
3785 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3786 the procedure linkage table (the .plt section) has been
3787 removed. */
3788 memmove (extdyn, next,
3789 sdynamic->size - (next - sdynamic->contents));
3790 next = extdyn;
3791 }
3792 }
3793
3794 if (strip_zero_sized)
3795 {
3796 /* Regenerate program headers. */
3797 elf_seg_map (info->output_bfd) = NULL;
3798 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3799 NULL);
3800 }
3801
3802 return true;
3803 }
3804
3805 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3806 1 if a DT_NEEDED tag already exists, and 0 on success. */
3807
3808 int
3809 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3810 {
3811 struct elf_link_hash_table *hash_table;
3812 size_t strindex;
3813 const char *soname;
3814
3815 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3816 return -1;
3817
3818 hash_table = elf_hash_table (info);
3819 soname = elf_dt_name (abfd);
3820 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3821 if (strindex == (size_t) -1)
3822 return -1;
3823
3824 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3825 {
3826 asection *sdyn;
3827 const struct elf_backend_data *bed;
3828 bfd_byte *extdyn;
3829
3830 bed = get_elf_backend_data (hash_table->dynobj);
3831 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3832 if (sdyn != NULL && sdyn->size != 0)
3833 for (extdyn = sdyn->contents;
3834 extdyn < sdyn->contents + sdyn->size;
3835 extdyn += bed->s->sizeof_dyn)
3836 {
3837 Elf_Internal_Dyn dyn;
3838
3839 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3840 if (dyn.d_tag == DT_NEEDED
3841 && dyn.d_un.d_val == strindex)
3842 {
3843 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3844 return 1;
3845 }
3846 }
3847 }
3848
3849 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3850 return -1;
3851
3852 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3853 return -1;
3854
3855 return 0;
3856 }
3857
3858 /* Return true if SONAME is on the needed list between NEEDED and STOP
3859 (or the end of list if STOP is NULL), and needed by a library that
3860 will be loaded. */
3861
3862 static bool
3863 on_needed_list (const char *soname,
3864 struct bfd_link_needed_list *needed,
3865 struct bfd_link_needed_list *stop)
3866 {
3867 struct bfd_link_needed_list *look;
3868 for (look = needed; look != stop; look = look->next)
3869 if (strcmp (soname, look->name) == 0
3870 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3871 /* If needed by a library that itself is not directly
3872 needed, recursively check whether that library is
3873 indirectly needed. Since we add DT_NEEDED entries to
3874 the end of the list, library dependencies appear after
3875 the library. Therefore search prior to the current
3876 LOOK, preventing possible infinite recursion. */
3877 || on_needed_list (elf_dt_name (look->by), needed, look)))
3878 return true;
3879
3880 return false;
3881 }
3882
3883 /* Sort symbol by value, section, size, and type. */
3884 static int
3885 elf_sort_symbol (const void *arg1, const void *arg2)
3886 {
3887 const struct elf_link_hash_entry *h1;
3888 const struct elf_link_hash_entry *h2;
3889 bfd_signed_vma vdiff;
3890 int sdiff;
3891 const char *n1;
3892 const char *n2;
3893
3894 h1 = *(const struct elf_link_hash_entry **) arg1;
3895 h2 = *(const struct elf_link_hash_entry **) arg2;
3896 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3897 if (vdiff != 0)
3898 return vdiff > 0 ? 1 : -1;
3899
3900 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3901 if (sdiff != 0)
3902 return sdiff;
3903
3904 /* Sort so that sized symbols are selected over zero size symbols. */
3905 vdiff = h1->size - h2->size;
3906 if (vdiff != 0)
3907 return vdiff > 0 ? 1 : -1;
3908
3909 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3910 if (h1->type != h2->type)
3911 return h1->type - h2->type;
3912
3913 /* If symbols are properly sized and typed, and multiple strong
3914 aliases are not defined in a shared library by the user we
3915 shouldn't get here. Unfortunately linker script symbols like
3916 __bss_start sometimes match a user symbol defined at the start of
3917 .bss without proper size and type. We'd like to preference the
3918 user symbol over reserved system symbols. Sort on leading
3919 underscores. */
3920 n1 = h1->root.root.string;
3921 n2 = h2->root.root.string;
3922 while (*n1 == *n2)
3923 {
3924 if (*n1 == 0)
3925 break;
3926 ++n1;
3927 ++n2;
3928 }
3929 if (*n1 == '_')
3930 return -1;
3931 if (*n2 == '_')
3932 return 1;
3933
3934 /* Final sort on name selects user symbols like '_u' over reserved
3935 system symbols like '_Z' and also will avoid qsort instability. */
3936 return *n1 - *n2;
3937 }
3938
3939 /* This function is used to adjust offsets into .dynstr for
3940 dynamic symbols. This is called via elf_link_hash_traverse. */
3941
3942 static bool
3943 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3944 {
3945 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3946
3947 if (h->dynindx != -1)
3948 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3949 return true;
3950 }
3951
3952 /* Assign string offsets in .dynstr, update all structures referencing
3953 them. */
3954
3955 static bool
3956 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3957 {
3958 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3959 struct elf_link_local_dynamic_entry *entry;
3960 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3961 bfd *dynobj = hash_table->dynobj;
3962 asection *sdyn;
3963 bfd_size_type size;
3964 const struct elf_backend_data *bed;
3965 bfd_byte *extdyn;
3966
3967 _bfd_elf_strtab_finalize (dynstr);
3968 size = _bfd_elf_strtab_size (dynstr);
3969
3970 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3971
3972 if (info->callbacks->examine_strtab)
3973 info->callbacks->examine_strtab (dynstr);
3974
3975 bed = get_elf_backend_data (dynobj);
3976 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3977 BFD_ASSERT (sdyn != NULL);
3978
3979 /* Update all .dynamic entries referencing .dynstr strings. */
3980 for (extdyn = sdyn->contents;
3981 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3982 extdyn += bed->s->sizeof_dyn)
3983 {
3984 Elf_Internal_Dyn dyn;
3985
3986 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3987 switch (dyn.d_tag)
3988 {
3989 case DT_STRSZ:
3990 dyn.d_un.d_val = size;
3991 break;
3992 case DT_NEEDED:
3993 case DT_SONAME:
3994 case DT_RPATH:
3995 case DT_RUNPATH:
3996 case DT_FILTER:
3997 case DT_AUXILIARY:
3998 case DT_AUDIT:
3999 case DT_DEPAUDIT:
4000 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4001 break;
4002 default:
4003 continue;
4004 }
4005 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4006 }
4007
4008 /* Now update local dynamic symbols. */
4009 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4010 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4011 entry->isym.st_name);
4012
4013 /* And the rest of dynamic symbols. */
4014 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4015
4016 /* Adjust version definitions. */
4017 if (elf_tdata (output_bfd)->cverdefs)
4018 {
4019 asection *s;
4020 bfd_byte *p;
4021 size_t i;
4022 Elf_Internal_Verdef def;
4023 Elf_Internal_Verdaux defaux;
4024
4025 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4026 p = s->contents;
4027 do
4028 {
4029 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4030 &def);
4031 p += sizeof (Elf_External_Verdef);
4032 if (def.vd_aux != sizeof (Elf_External_Verdef))
4033 continue;
4034 for (i = 0; i < def.vd_cnt; ++i)
4035 {
4036 _bfd_elf_swap_verdaux_in (output_bfd,
4037 (Elf_External_Verdaux *) p, &defaux);
4038 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4039 defaux.vda_name);
4040 _bfd_elf_swap_verdaux_out (output_bfd,
4041 &defaux, (Elf_External_Verdaux *) p);
4042 p += sizeof (Elf_External_Verdaux);
4043 }
4044 }
4045 while (def.vd_next);
4046 }
4047
4048 /* Adjust version references. */
4049 if (elf_tdata (output_bfd)->verref)
4050 {
4051 asection *s;
4052 bfd_byte *p;
4053 size_t i;
4054 Elf_Internal_Verneed need;
4055 Elf_Internal_Vernaux needaux;
4056
4057 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4058 p = s->contents;
4059 do
4060 {
4061 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4062 &need);
4063 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4064 _bfd_elf_swap_verneed_out (output_bfd, &need,
4065 (Elf_External_Verneed *) p);
4066 p += sizeof (Elf_External_Verneed);
4067 for (i = 0; i < need.vn_cnt; ++i)
4068 {
4069 _bfd_elf_swap_vernaux_in (output_bfd,
4070 (Elf_External_Vernaux *) p, &needaux);
4071 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4072 needaux.vna_name);
4073 _bfd_elf_swap_vernaux_out (output_bfd,
4074 &needaux,
4075 (Elf_External_Vernaux *) p);
4076 p += sizeof (Elf_External_Vernaux);
4077 }
4078 }
4079 while (need.vn_next);
4080 }
4081
4082 return true;
4083 }
4084 \f
4085 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4086 The default is to only match when the INPUT and OUTPUT are exactly
4087 the same target. */
4088
4089 bool
4090 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4091 const bfd_target *output)
4092 {
4093 return input == output;
4094 }
4095
4096 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4097 This version is used when different targets for the same architecture
4098 are virtually identical. */
4099
4100 bool
4101 _bfd_elf_relocs_compatible (const bfd_target *input,
4102 const bfd_target *output)
4103 {
4104 const struct elf_backend_data *obed, *ibed;
4105
4106 if (input == output)
4107 return true;
4108
4109 ibed = xvec_get_elf_backend_data (input);
4110 obed = xvec_get_elf_backend_data (output);
4111
4112 if (ibed->arch != obed->arch)
4113 return false;
4114
4115 /* If both backends are using this function, deem them compatible. */
4116 return ibed->relocs_compatible == obed->relocs_compatible;
4117 }
4118
4119 /* Make a special call to the linker "notice" function to tell it that
4120 we are about to handle an as-needed lib, or have finished
4121 processing the lib. */
4122
4123 bool
4124 _bfd_elf_notice_as_needed (bfd *ibfd,
4125 struct bfd_link_info *info,
4126 enum notice_asneeded_action act)
4127 {
4128 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4129 }
4130
4131 /* Call ACTION on each relocation in an ELF object file. */
4132
4133 bool
4134 _bfd_elf_link_iterate_on_relocs
4135 (bfd *abfd, struct bfd_link_info *info,
4136 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4137 const Elf_Internal_Rela *))
4138 {
4139 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4140 struct elf_link_hash_table *htab = elf_hash_table (info);
4141
4142 /* If this object is the same format as the output object, and it is
4143 not a shared library, then let the backend look through the
4144 relocs.
4145
4146 This is required to build global offset table entries and to
4147 arrange for dynamic relocs. It is not required for the
4148 particular common case of linking non PIC code, even when linking
4149 against shared libraries, but unfortunately there is no way of
4150 knowing whether an object file has been compiled PIC or not.
4151 Looking through the relocs is not particularly time consuming.
4152 The problem is that we must either (1) keep the relocs in memory,
4153 which causes the linker to require additional runtime memory or
4154 (2) read the relocs twice from the input file, which wastes time.
4155 This would be a good case for using mmap.
4156
4157 I have no idea how to handle linking PIC code into a file of a
4158 different format. It probably can't be done. */
4159 if ((abfd->flags & DYNAMIC) == 0
4160 && is_elf_hash_table (&htab->root)
4161 && elf_object_id (abfd) == elf_hash_table_id (htab)
4162 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4163 {
4164 asection *o;
4165
4166 for (o = abfd->sections; o != NULL; o = o->next)
4167 {
4168 Elf_Internal_Rela *internal_relocs;
4169 bool ok;
4170
4171 /* Don't check relocations in excluded sections. Don't do
4172 anything special with non-loaded, non-alloced sections.
4173 In particular, any relocs in such sections should not
4174 affect GOT and PLT reference counting (ie. we don't
4175 allow them to create GOT or PLT entries), there's no
4176 possibility or desire to optimize TLS relocs, and
4177 there's not much point in propagating relocs to shared
4178 libs that the dynamic linker won't relocate. */
4179 if ((o->flags & SEC_ALLOC) == 0
4180 || (o->flags & SEC_RELOC) == 0
4181 || (o->flags & SEC_EXCLUDE) != 0
4182 || o->reloc_count == 0
4183 || ((info->strip == strip_all || info->strip == strip_debugger)
4184 && (o->flags & SEC_DEBUGGING) != 0)
4185 || bfd_is_abs_section (o->output_section))
4186 continue;
4187
4188 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4189 o, NULL,
4190 NULL,
4191 _bfd_link_keep_memory (info));
4192 if (internal_relocs == NULL)
4193 return false;
4194
4195 ok = action (abfd, info, o, internal_relocs);
4196
4197 if (elf_section_data (o)->relocs != internal_relocs)
4198 free (internal_relocs);
4199
4200 if (! ok)
4201 return false;
4202 }
4203 }
4204
4205 return true;
4206 }
4207
4208 /* Check relocations in an ELF object file. This is called after
4209 all input files have been opened. */
4210
4211 bool
4212 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4213 {
4214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4215 if (bed->check_relocs != NULL)
4216 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4217 bed->check_relocs);
4218 return true;
4219 }
4220
4221 /* Add symbols from an ELF object file to the linker hash table. */
4222
4223 static bool
4224 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4225 {
4226 Elf_Internal_Ehdr *ehdr;
4227 Elf_Internal_Shdr *hdr;
4228 size_t symcount;
4229 size_t extsymcount;
4230 size_t extsymoff;
4231 struct elf_link_hash_entry **sym_hash;
4232 bool dynamic;
4233 Elf_External_Versym *extversym = NULL;
4234 Elf_External_Versym *extversym_end = NULL;
4235 Elf_External_Versym *ever;
4236 struct elf_link_hash_entry *weaks;
4237 struct elf_link_hash_entry **nondeflt_vers = NULL;
4238 size_t nondeflt_vers_cnt = 0;
4239 Elf_Internal_Sym *isymbuf = NULL;
4240 Elf_Internal_Sym *isym;
4241 Elf_Internal_Sym *isymend;
4242 const struct elf_backend_data *bed;
4243 bool add_needed;
4244 struct elf_link_hash_table *htab;
4245 void *alloc_mark = NULL;
4246 struct bfd_hash_entry **old_table = NULL;
4247 unsigned int old_size = 0;
4248 unsigned int old_count = 0;
4249 void *old_tab = NULL;
4250 void *old_ent;
4251 struct bfd_link_hash_entry *old_undefs = NULL;
4252 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4253 void *old_strtab = NULL;
4254 size_t tabsize = 0;
4255 asection *s;
4256 bool just_syms;
4257
4258 htab = elf_hash_table (info);
4259 bed = get_elf_backend_data (abfd);
4260
4261 if (elf_use_dt_symtab_p (abfd))
4262 {
4263 bfd_set_error (bfd_error_wrong_format);
4264 return false;
4265 }
4266
4267 if ((abfd->flags & DYNAMIC) == 0)
4268 dynamic = false;
4269 else
4270 {
4271 dynamic = true;
4272
4273 /* You can't use -r against a dynamic object. Also, there's no
4274 hope of using a dynamic object which does not exactly match
4275 the format of the output file. */
4276 if (bfd_link_relocatable (info)
4277 || !is_elf_hash_table (&htab->root)
4278 || info->output_bfd->xvec != abfd->xvec)
4279 {
4280 if (bfd_link_relocatable (info))
4281 bfd_set_error (bfd_error_invalid_operation);
4282 else
4283 bfd_set_error (bfd_error_wrong_format);
4284 goto error_return;
4285 }
4286 }
4287
4288 ehdr = elf_elfheader (abfd);
4289 if (info->warn_alternate_em
4290 && bed->elf_machine_code != ehdr->e_machine
4291 && ((bed->elf_machine_alt1 != 0
4292 && ehdr->e_machine == bed->elf_machine_alt1)
4293 || (bed->elf_machine_alt2 != 0
4294 && ehdr->e_machine == bed->elf_machine_alt2)))
4295 _bfd_error_handler
4296 /* xgettext:c-format */
4297 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4298 ehdr->e_machine, abfd, bed->elf_machine_code);
4299
4300 /* As a GNU extension, any input sections which are named
4301 .gnu.warning.SYMBOL are treated as warning symbols for the given
4302 symbol. This differs from .gnu.warning sections, which generate
4303 warnings when they are included in an output file. */
4304 /* PR 12761: Also generate this warning when building shared libraries. */
4305 for (s = abfd->sections; s != NULL; s = s->next)
4306 {
4307 const char *name;
4308
4309 name = bfd_section_name (s);
4310 if (startswith (name, ".gnu.warning."))
4311 {
4312 char *msg;
4313 bfd_size_type sz;
4314
4315 name += sizeof ".gnu.warning." - 1;
4316
4317 /* If this is a shared object, then look up the symbol
4318 in the hash table. If it is there, and it is already
4319 been defined, then we will not be using the entry
4320 from this shared object, so we don't need to warn.
4321 FIXME: If we see the definition in a regular object
4322 later on, we will warn, but we shouldn't. The only
4323 fix is to keep track of what warnings we are supposed
4324 to emit, and then handle them all at the end of the
4325 link. */
4326 if (dynamic)
4327 {
4328 struct elf_link_hash_entry *h;
4329
4330 h = elf_link_hash_lookup (htab, name, false, false, true);
4331
4332 /* FIXME: What about bfd_link_hash_common? */
4333 if (h != NULL
4334 && (h->root.type == bfd_link_hash_defined
4335 || h->root.type == bfd_link_hash_defweak))
4336 continue;
4337 }
4338
4339 sz = s->size;
4340 msg = (char *) bfd_alloc (abfd, sz + 1);
4341 if (msg == NULL)
4342 goto error_return;
4343
4344 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4345 goto error_return;
4346
4347 msg[sz] = '\0';
4348
4349 if (! (_bfd_generic_link_add_one_symbol
4350 (info, abfd, name, BSF_WARNING, s, 0, msg,
4351 false, bed->collect, NULL)))
4352 goto error_return;
4353
4354 if (bfd_link_executable (info))
4355 {
4356 /* Clobber the section size so that the warning does
4357 not get copied into the output file. */
4358 s->size = 0;
4359
4360 /* Also set SEC_EXCLUDE, so that symbols defined in
4361 the warning section don't get copied to the output. */
4362 s->flags |= SEC_EXCLUDE;
4363 }
4364 }
4365 }
4366
4367 just_syms = ((s = abfd->sections) != NULL
4368 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4369
4370 add_needed = true;
4371 if (! dynamic)
4372 {
4373 /* If we are creating a shared library, create all the dynamic
4374 sections immediately. We need to attach them to something,
4375 so we attach them to this BFD, provided it is the right
4376 format and is not from ld --just-symbols. Always create the
4377 dynamic sections for -E/--dynamic-list. FIXME: If there
4378 are no input BFD's of the same format as the output, we can't
4379 make a shared library. */
4380 if (!just_syms
4381 && (bfd_link_pic (info)
4382 || (!bfd_link_relocatable (info)
4383 && info->nointerp
4384 && (info->export_dynamic || info->dynamic)))
4385 && is_elf_hash_table (&htab->root)
4386 && info->output_bfd->xvec == abfd->xvec
4387 && !htab->dynamic_sections_created)
4388 {
4389 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4390 goto error_return;
4391 }
4392 }
4393 else if (!is_elf_hash_table (&htab->root))
4394 goto error_return;
4395 else
4396 {
4397 const char *soname = NULL;
4398 char *audit = NULL;
4399 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4400 const Elf_Internal_Phdr *phdr;
4401 struct elf_link_loaded_list *loaded_lib;
4402
4403 /* ld --just-symbols and dynamic objects don't mix very well.
4404 ld shouldn't allow it. */
4405 if (just_syms)
4406 abort ();
4407
4408 /* If this dynamic lib was specified on the command line with
4409 --as-needed in effect, then we don't want to add a DT_NEEDED
4410 tag unless the lib is actually used. Similary for libs brought
4411 in by another lib's DT_NEEDED. When --no-add-needed is used
4412 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4413 any dynamic library in DT_NEEDED tags in the dynamic lib at
4414 all. */
4415 add_needed = (elf_dyn_lib_class (abfd)
4416 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4417 | DYN_NO_NEEDED)) == 0;
4418
4419 s = bfd_get_section_by_name (abfd, ".dynamic");
4420 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4421 {
4422 bfd_byte *dynbuf;
4423 bfd_byte *extdyn;
4424 unsigned int elfsec;
4425 unsigned long shlink;
4426
4427 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4428 {
4429 error_free_dyn:
4430 free (dynbuf);
4431 goto error_return;
4432 }
4433
4434 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4435 if (elfsec == SHN_BAD)
4436 goto error_free_dyn;
4437 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4438
4439 for (extdyn = dynbuf;
4440 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4441 extdyn += bed->s->sizeof_dyn)
4442 {
4443 Elf_Internal_Dyn dyn;
4444
4445 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4446 if (dyn.d_tag == DT_SONAME)
4447 {
4448 unsigned int tagv = dyn.d_un.d_val;
4449 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4450 if (soname == NULL)
4451 goto error_free_dyn;
4452 }
4453 if (dyn.d_tag == DT_NEEDED)
4454 {
4455 struct bfd_link_needed_list *n, **pn;
4456 char *fnm, *anm;
4457 unsigned int tagv = dyn.d_un.d_val;
4458 size_t amt = sizeof (struct bfd_link_needed_list);
4459
4460 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4461 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4462 if (n == NULL || fnm == NULL)
4463 goto error_free_dyn;
4464 amt = strlen (fnm) + 1;
4465 anm = (char *) bfd_alloc (abfd, amt);
4466 if (anm == NULL)
4467 goto error_free_dyn;
4468 memcpy (anm, fnm, amt);
4469 n->name = anm;
4470 n->by = abfd;
4471 n->next = NULL;
4472 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4473 ;
4474 *pn = n;
4475 }
4476 if (dyn.d_tag == DT_RUNPATH)
4477 {
4478 struct bfd_link_needed_list *n, **pn;
4479 char *fnm, *anm;
4480 unsigned int tagv = dyn.d_un.d_val;
4481 size_t amt = sizeof (struct bfd_link_needed_list);
4482
4483 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4484 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4485 if (n == NULL || fnm == NULL)
4486 goto error_free_dyn;
4487 amt = strlen (fnm) + 1;
4488 anm = (char *) bfd_alloc (abfd, amt);
4489 if (anm == NULL)
4490 goto error_free_dyn;
4491 memcpy (anm, fnm, amt);
4492 n->name = anm;
4493 n->by = abfd;
4494 n->next = NULL;
4495 for (pn = & runpath;
4496 *pn != NULL;
4497 pn = &(*pn)->next)
4498 ;
4499 *pn = n;
4500 }
4501 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4502 if (!runpath && dyn.d_tag == DT_RPATH)
4503 {
4504 struct bfd_link_needed_list *n, **pn;
4505 char *fnm, *anm;
4506 unsigned int tagv = dyn.d_un.d_val;
4507 size_t amt = sizeof (struct bfd_link_needed_list);
4508
4509 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4510 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4511 if (n == NULL || fnm == NULL)
4512 goto error_free_dyn;
4513 amt = strlen (fnm) + 1;
4514 anm = (char *) bfd_alloc (abfd, amt);
4515 if (anm == NULL)
4516 goto error_free_dyn;
4517 memcpy (anm, fnm, amt);
4518 n->name = anm;
4519 n->by = abfd;
4520 n->next = NULL;
4521 for (pn = & rpath;
4522 *pn != NULL;
4523 pn = &(*pn)->next)
4524 ;
4525 *pn = n;
4526 }
4527 if (dyn.d_tag == DT_AUDIT)
4528 {
4529 unsigned int tagv = dyn.d_un.d_val;
4530 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4531 }
4532 if (dyn.d_tag == DT_FLAGS_1)
4533 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4534 }
4535
4536 free (dynbuf);
4537 }
4538
4539 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4540 frees all more recently bfd_alloc'd blocks as well. */
4541 if (runpath)
4542 rpath = runpath;
4543
4544 if (rpath)
4545 {
4546 struct bfd_link_needed_list **pn;
4547 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4548 ;
4549 *pn = rpath;
4550 }
4551
4552 /* If we have a PT_GNU_RELRO program header, mark as read-only
4553 all sections contained fully therein. This makes relro
4554 shared library sections appear as they will at run-time. */
4555 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4556 while (phdr-- > elf_tdata (abfd)->phdr)
4557 if (phdr->p_type == PT_GNU_RELRO)
4558 {
4559 for (s = abfd->sections; s != NULL; s = s->next)
4560 {
4561 unsigned int opb = bfd_octets_per_byte (abfd, s);
4562
4563 if ((s->flags & SEC_ALLOC) != 0
4564 && s->vma * opb >= phdr->p_vaddr
4565 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4566 s->flags |= SEC_READONLY;
4567 }
4568 break;
4569 }
4570
4571 /* We do not want to include any of the sections in a dynamic
4572 object in the output file. We hack by simply clobbering the
4573 list of sections in the BFD. This could be handled more
4574 cleanly by, say, a new section flag; the existing
4575 SEC_NEVER_LOAD flag is not the one we want, because that one
4576 still implies that the section takes up space in the output
4577 file. */
4578 bfd_section_list_clear (abfd);
4579
4580 /* Find the name to use in a DT_NEEDED entry that refers to this
4581 object. If the object has a DT_SONAME entry, we use it.
4582 Otherwise, if the generic linker stuck something in
4583 elf_dt_name, we use that. Otherwise, we just use the file
4584 name. */
4585 if (soname == NULL || *soname == '\0')
4586 {
4587 soname = elf_dt_name (abfd);
4588 if (soname == NULL || *soname == '\0')
4589 soname = bfd_get_filename (abfd);
4590 }
4591
4592 /* Save the SONAME because sometimes the linker emulation code
4593 will need to know it. */
4594 elf_dt_name (abfd) = soname;
4595
4596 /* If we have already included this dynamic object in the
4597 link, just ignore it. There is no reason to include a
4598 particular dynamic object more than once. */
4599 for (loaded_lib = htab->dyn_loaded;
4600 loaded_lib != NULL;
4601 loaded_lib = loaded_lib->next)
4602 {
4603 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4604 return true;
4605 }
4606
4607 /* Create dynamic sections for backends that require that be done
4608 before setup_gnu_properties. */
4609 if (add_needed
4610 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4611 return false;
4612
4613 /* Save the DT_AUDIT entry for the linker emulation code. */
4614 elf_dt_audit (abfd) = audit;
4615 }
4616
4617 /* If this is a dynamic object, we always link against the .dynsym
4618 symbol table, not the .symtab symbol table. The dynamic linker
4619 will only see the .dynsym symbol table, so there is no reason to
4620 look at .symtab for a dynamic object. */
4621
4622 if (! dynamic || elf_dynsymtab (abfd) == 0)
4623 hdr = &elf_tdata (abfd)->symtab_hdr;
4624 else
4625 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4626
4627 symcount = hdr->sh_size / bed->s->sizeof_sym;
4628
4629 /* The sh_info field of the symtab header tells us where the
4630 external symbols start. We don't care about the local symbols at
4631 this point. */
4632 if (elf_bad_symtab (abfd))
4633 {
4634 extsymcount = symcount;
4635 extsymoff = 0;
4636 }
4637 else
4638 {
4639 extsymcount = symcount - hdr->sh_info;
4640 extsymoff = hdr->sh_info;
4641 }
4642
4643 sym_hash = elf_sym_hashes (abfd);
4644 if (extsymcount != 0)
4645 {
4646 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4647 NULL, NULL, NULL);
4648 if (isymbuf == NULL)
4649 goto error_return;
4650
4651 if (sym_hash == NULL)
4652 {
4653 /* We store a pointer to the hash table entry for each
4654 external symbol. */
4655 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4656 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4657 if (sym_hash == NULL)
4658 goto error_free_sym;
4659 elf_sym_hashes (abfd) = sym_hash;
4660 }
4661 }
4662
4663 if (dynamic)
4664 {
4665 /* Read in any version definitions. */
4666 if (!_bfd_elf_slurp_version_tables (abfd,
4667 info->default_imported_symver))
4668 goto error_free_sym;
4669
4670 /* Read in the symbol versions, but don't bother to convert them
4671 to internal format. */
4672 if (elf_dynversym (abfd) != 0)
4673 {
4674 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4675 bfd_size_type amt = versymhdr->sh_size;
4676
4677 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4678 goto error_free_sym;
4679 extversym = (Elf_External_Versym *)
4680 _bfd_malloc_and_read (abfd, amt, amt);
4681 if (extversym == NULL)
4682 goto error_free_sym;
4683 extversym_end = extversym + amt / sizeof (*extversym);
4684 }
4685 }
4686
4687 /* If we are loading an as-needed shared lib, save the symbol table
4688 state before we start adding symbols. If the lib turns out
4689 to be unneeded, restore the state. */
4690 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4691 {
4692 unsigned int i;
4693 size_t entsize;
4694
4695 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4696 {
4697 struct bfd_hash_entry *p;
4698 struct elf_link_hash_entry *h;
4699
4700 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4701 {
4702 h = (struct elf_link_hash_entry *) p;
4703 entsize += htab->root.table.entsize;
4704 if (h->root.type == bfd_link_hash_warning)
4705 {
4706 entsize += htab->root.table.entsize;
4707 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4708 }
4709 if (h->root.type == bfd_link_hash_common)
4710 entsize += sizeof (*h->root.u.c.p);
4711 }
4712 }
4713
4714 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4715 old_tab = bfd_malloc (tabsize + entsize);
4716 if (old_tab == NULL)
4717 goto error_free_vers;
4718
4719 /* Remember the current objalloc pointer, so that all mem for
4720 symbols added can later be reclaimed. */
4721 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4722 if (alloc_mark == NULL)
4723 goto error_free_vers;
4724
4725 /* Make a special call to the linker "notice" function to
4726 tell it that we are about to handle an as-needed lib. */
4727 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4728 goto error_free_vers;
4729
4730 /* Clone the symbol table. Remember some pointers into the
4731 symbol table, and dynamic symbol count. */
4732 old_ent = (char *) old_tab + tabsize;
4733 memcpy (old_tab, htab->root.table.table, tabsize);
4734 old_undefs = htab->root.undefs;
4735 old_undefs_tail = htab->root.undefs_tail;
4736 old_table = htab->root.table.table;
4737 old_size = htab->root.table.size;
4738 old_count = htab->root.table.count;
4739 old_strtab = NULL;
4740 if (htab->dynstr != NULL)
4741 {
4742 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4743 if (old_strtab == NULL)
4744 goto error_free_vers;
4745 }
4746
4747 for (i = 0; i < htab->root.table.size; i++)
4748 {
4749 struct bfd_hash_entry *p;
4750 struct elf_link_hash_entry *h;
4751
4752 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4753 {
4754 h = (struct elf_link_hash_entry *) p;
4755 memcpy (old_ent, h, htab->root.table.entsize);
4756 old_ent = (char *) old_ent + htab->root.table.entsize;
4757 if (h->root.type == bfd_link_hash_warning)
4758 {
4759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4760 memcpy (old_ent, h, htab->root.table.entsize);
4761 old_ent = (char *) old_ent + htab->root.table.entsize;
4762 }
4763 if (h->root.type == bfd_link_hash_common)
4764 {
4765 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4766 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4767 }
4768 }
4769 }
4770 }
4771
4772 weaks = NULL;
4773 if (extversym == NULL)
4774 ever = NULL;
4775 else if (extversym + extsymoff < extversym_end)
4776 ever = extversym + extsymoff;
4777 else
4778 {
4779 /* xgettext:c-format */
4780 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4781 abfd, (long) extsymoff,
4782 (long) (extversym_end - extversym) / sizeof (* extversym));
4783 bfd_set_error (bfd_error_bad_value);
4784 goto error_free_vers;
4785 }
4786
4787 if (!bfd_link_relocatable (info)
4788 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4789 {
4790 _bfd_error_handler
4791 (_("%pB: plugin needed to handle lto object"), abfd);
4792 }
4793
4794 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4795 isym < isymend;
4796 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4797 {
4798 int bind;
4799 bfd_vma value;
4800 asection *sec, *new_sec;
4801 flagword flags;
4802 const char *name;
4803 struct elf_link_hash_entry *h;
4804 struct elf_link_hash_entry *hi;
4805 bool definition;
4806 bool size_change_ok;
4807 bool type_change_ok;
4808 bool new_weak;
4809 bool old_weak;
4810 bfd *override;
4811 bool common;
4812 bool discarded;
4813 unsigned int old_alignment;
4814 unsigned int shindex;
4815 bfd *old_bfd;
4816 bool matched;
4817
4818 override = NULL;
4819
4820 flags = BSF_NO_FLAGS;
4821 sec = NULL;
4822 value = isym->st_value;
4823 common = bed->common_definition (isym);
4824 if (common && info->inhibit_common_definition)
4825 {
4826 /* Treat common symbol as undefined for --no-define-common. */
4827 isym->st_shndx = SHN_UNDEF;
4828 common = false;
4829 }
4830 discarded = false;
4831
4832 bind = ELF_ST_BIND (isym->st_info);
4833 switch (bind)
4834 {
4835 case STB_LOCAL:
4836 /* This should be impossible, since ELF requires that all
4837 global symbols follow all local symbols, and that sh_info
4838 point to the first global symbol. Unfortunately, Irix 5
4839 screws this up. */
4840 if (elf_bad_symtab (abfd))
4841 continue;
4842
4843 /* If we aren't prepared to handle locals within the globals
4844 then we'll likely segfault on a NULL symbol hash if the
4845 symbol is ever referenced in relocations. */
4846 shindex = elf_elfheader (abfd)->e_shstrndx;
4847 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4848 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4849 " (>= sh_info of %lu)"),
4850 abfd, name, (long) (isym - isymbuf + extsymoff),
4851 (long) extsymoff);
4852
4853 /* Dynamic object relocations are not processed by ld, so
4854 ld won't run into the problem mentioned above. */
4855 if (dynamic)
4856 continue;
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4859
4860 case STB_GLOBAL:
4861 if (isym->st_shndx != SHN_UNDEF && !common)
4862 flags = BSF_GLOBAL;
4863 break;
4864
4865 case STB_WEAK:
4866 flags = BSF_WEAK;
4867 break;
4868
4869 case STB_GNU_UNIQUE:
4870 flags = BSF_GNU_UNIQUE;
4871 break;
4872
4873 default:
4874 /* Leave it up to the processor backend. */
4875 break;
4876 }
4877
4878 if (isym->st_shndx == SHN_UNDEF)
4879 sec = bfd_und_section_ptr;
4880 else if (isym->st_shndx == SHN_ABS)
4881 sec = bfd_abs_section_ptr;
4882 else if (isym->st_shndx == SHN_COMMON)
4883 {
4884 sec = bfd_com_section_ptr;
4885 /* What ELF calls the size we call the value. What ELF
4886 calls the value we call the alignment. */
4887 value = isym->st_size;
4888 }
4889 else
4890 {
4891 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4892 if (sec == NULL)
4893 sec = bfd_abs_section_ptr;
4894 else if (discarded_section (sec))
4895 {
4896 /* Symbols from discarded section are undefined. We keep
4897 its visibility. */
4898 sec = bfd_und_section_ptr;
4899 discarded = true;
4900 isym->st_shndx = SHN_UNDEF;
4901 }
4902 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4903 value -= sec->vma;
4904 }
4905
4906 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4907 isym->st_name);
4908 if (name == NULL)
4909 goto error_free_vers;
4910
4911 if (isym->st_shndx == SHN_COMMON
4912 && (abfd->flags & BFD_PLUGIN) != 0)
4913 {
4914 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4915
4916 if (xc == NULL)
4917 {
4918 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4919 | SEC_EXCLUDE);
4920 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4921 if (xc == NULL)
4922 goto error_free_vers;
4923 }
4924 sec = xc;
4925 }
4926 else if (isym->st_shndx == SHN_COMMON
4927 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4928 && !bfd_link_relocatable (info))
4929 {
4930 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4931
4932 if (tcomm == NULL)
4933 {
4934 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4935 | SEC_LINKER_CREATED);
4936 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4937 if (tcomm == NULL)
4938 goto error_free_vers;
4939 }
4940 sec = tcomm;
4941 }
4942 else if (bed->elf_add_symbol_hook)
4943 {
4944 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4945 &sec, &value))
4946 goto error_free_vers;
4947
4948 /* The hook function sets the name to NULL if this symbol
4949 should be skipped for some reason. */
4950 if (name == NULL)
4951 continue;
4952 }
4953
4954 /* Sanity check that all possibilities were handled. */
4955 if (sec == NULL)
4956 abort ();
4957
4958 /* Silently discard TLS symbols from --just-syms. There's
4959 no way to combine a static TLS block with a new TLS block
4960 for this executable. */
4961 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4962 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4963 continue;
4964
4965 if (bfd_is_und_section (sec)
4966 || bfd_is_com_section (sec))
4967 definition = false;
4968 else
4969 definition = true;
4970
4971 size_change_ok = false;
4972 type_change_ok = bed->type_change_ok;
4973 old_weak = false;
4974 matched = false;
4975 old_alignment = 0;
4976 old_bfd = NULL;
4977 new_sec = sec;
4978
4979 if (is_elf_hash_table (&htab->root))
4980 {
4981 Elf_Internal_Versym iver;
4982 unsigned int vernum = 0;
4983 bool skip;
4984
4985 if (ever == NULL)
4986 {
4987 if (info->default_imported_symver)
4988 /* Use the default symbol version created earlier. */
4989 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4990 else
4991 iver.vs_vers = 0;
4992 }
4993 else if (ever >= extversym_end)
4994 {
4995 /* xgettext:c-format */
4996 _bfd_error_handler (_("%pB: not enough version information"),
4997 abfd);
4998 bfd_set_error (bfd_error_bad_value);
4999 goto error_free_vers;
5000 }
5001 else
5002 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5003
5004 vernum = iver.vs_vers & VERSYM_VERSION;
5005
5006 /* If this is a hidden symbol, or if it is not version
5007 1, we append the version name to the symbol name.
5008 However, we do not modify a non-hidden absolute symbol
5009 if it is not a function, because it might be the version
5010 symbol itself. FIXME: What if it isn't? */
5011 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5012 || (vernum > 1
5013 && (!bfd_is_abs_section (sec)
5014 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5015 {
5016 const char *verstr;
5017 size_t namelen, verlen, newlen;
5018 char *newname, *p;
5019
5020 if (isym->st_shndx != SHN_UNDEF)
5021 {
5022 if (vernum > elf_tdata (abfd)->cverdefs)
5023 verstr = NULL;
5024 else if (vernum > 1)
5025 verstr =
5026 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5027 else
5028 verstr = "";
5029
5030 if (verstr == NULL)
5031 {
5032 _bfd_error_handler
5033 /* xgettext:c-format */
5034 (_("%pB: %s: invalid version %u (max %d)"),
5035 abfd, name, vernum,
5036 elf_tdata (abfd)->cverdefs);
5037 bfd_set_error (bfd_error_bad_value);
5038 goto error_free_vers;
5039 }
5040 }
5041 else
5042 {
5043 /* We cannot simply test for the number of
5044 entries in the VERNEED section since the
5045 numbers for the needed versions do not start
5046 at 0. */
5047 Elf_Internal_Verneed *t;
5048
5049 verstr = NULL;
5050 for (t = elf_tdata (abfd)->verref;
5051 t != NULL;
5052 t = t->vn_nextref)
5053 {
5054 Elf_Internal_Vernaux *a;
5055
5056 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5057 {
5058 if (a->vna_other == vernum)
5059 {
5060 verstr = a->vna_nodename;
5061 break;
5062 }
5063 }
5064 if (a != NULL)
5065 break;
5066 }
5067 if (verstr == NULL)
5068 {
5069 _bfd_error_handler
5070 /* xgettext:c-format */
5071 (_("%pB: %s: invalid needed version %d"),
5072 abfd, name, vernum);
5073 bfd_set_error (bfd_error_bad_value);
5074 goto error_free_vers;
5075 }
5076 }
5077
5078 namelen = strlen (name);
5079 verlen = strlen (verstr);
5080 newlen = namelen + verlen + 2;
5081 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5082 && isym->st_shndx != SHN_UNDEF)
5083 ++newlen;
5084
5085 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5086 if (newname == NULL)
5087 goto error_free_vers;
5088 memcpy (newname, name, namelen);
5089 p = newname + namelen;
5090 *p++ = ELF_VER_CHR;
5091 /* If this is a defined non-hidden version symbol,
5092 we add another @ to the name. This indicates the
5093 default version of the symbol. */
5094 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5095 && isym->st_shndx != SHN_UNDEF)
5096 *p++ = ELF_VER_CHR;
5097 memcpy (p, verstr, verlen + 1);
5098
5099 name = newname;
5100 }
5101
5102 /* If this symbol has default visibility and the user has
5103 requested we not re-export it, then mark it as hidden. */
5104 if (!bfd_is_und_section (sec)
5105 && !dynamic
5106 && abfd->no_export
5107 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5108 isym->st_other = (STV_HIDDEN
5109 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5110
5111 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5112 sym_hash, &old_bfd, &old_weak,
5113 &old_alignment, &skip, &override,
5114 &type_change_ok, &size_change_ok,
5115 &matched))
5116 goto error_free_vers;
5117
5118 if (skip)
5119 continue;
5120
5121 /* Override a definition only if the new symbol matches the
5122 existing one. */
5123 if (override && matched)
5124 definition = false;
5125
5126 h = *sym_hash;
5127 while (h->root.type == bfd_link_hash_indirect
5128 || h->root.type == bfd_link_hash_warning)
5129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5130
5131 if (h->versioned != unversioned
5132 && elf_tdata (abfd)->verdef != NULL
5133 && vernum > 1
5134 && definition)
5135 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5136 }
5137
5138 if (! (_bfd_generic_link_add_one_symbol
5139 (info, override ? override : abfd, name, flags, sec, value,
5140 NULL, false, bed->collect,
5141 (struct bfd_link_hash_entry **) sym_hash)))
5142 goto error_free_vers;
5143
5144 h = *sym_hash;
5145 /* We need to make sure that indirect symbol dynamic flags are
5146 updated. */
5147 hi = h;
5148 while (h->root.type == bfd_link_hash_indirect
5149 || h->root.type == bfd_link_hash_warning)
5150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5151
5152 *sym_hash = h;
5153
5154 /* Setting the index to -3 tells elf_link_output_extsym that
5155 this symbol is defined in a discarded section. */
5156 if (discarded && is_elf_hash_table (&htab->root))
5157 h->indx = -3;
5158
5159 new_weak = (flags & BSF_WEAK) != 0;
5160 if (dynamic
5161 && definition
5162 && new_weak
5163 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5164 && is_elf_hash_table (&htab->root)
5165 && h->u.alias == NULL)
5166 {
5167 /* Keep a list of all weak defined non function symbols from
5168 a dynamic object, using the alias field. Later in this
5169 function we will set the alias field to the correct
5170 value. We only put non-function symbols from dynamic
5171 objects on this list, because that happens to be the only
5172 time we need to know the normal symbol corresponding to a
5173 weak symbol, and the information is time consuming to
5174 figure out. If the alias field is not already NULL,
5175 then this symbol was already defined by some previous
5176 dynamic object, and we will be using that previous
5177 definition anyhow. */
5178
5179 h->u.alias = weaks;
5180 weaks = h;
5181 }
5182
5183 /* Set the alignment of a common symbol. */
5184 if ((common || bfd_is_com_section (sec))
5185 && h->root.type == bfd_link_hash_common)
5186 {
5187 unsigned int align;
5188
5189 if (common)
5190 align = bfd_log2 (isym->st_value);
5191 else
5192 {
5193 /* The new symbol is a common symbol in a shared object.
5194 We need to get the alignment from the section. */
5195 align = new_sec->alignment_power;
5196 }
5197 if (align > old_alignment)
5198 h->root.u.c.p->alignment_power = align;
5199 else
5200 h->root.u.c.p->alignment_power = old_alignment;
5201 }
5202
5203 if (is_elf_hash_table (&htab->root))
5204 {
5205 /* Set a flag in the hash table entry indicating the type of
5206 reference or definition we just found. A dynamic symbol
5207 is one which is referenced or defined by both a regular
5208 object and a shared object. */
5209 bool dynsym = false;
5210
5211 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5212 if ((abfd->flags & BFD_PLUGIN) != 0)
5213 {
5214 /* Except for this flag to track nonweak references. */
5215 if (!definition
5216 && bind != STB_WEAK)
5217 h->ref_ir_nonweak = 1;
5218 }
5219 else if (!dynamic)
5220 {
5221 if (! definition)
5222 {
5223 h->ref_regular = 1;
5224 if (bind != STB_WEAK)
5225 h->ref_regular_nonweak = 1;
5226 }
5227 else
5228 {
5229 h->def_regular = 1;
5230 if (h->def_dynamic)
5231 {
5232 h->def_dynamic = 0;
5233 h->ref_dynamic = 1;
5234 }
5235 }
5236 }
5237 else
5238 {
5239 if (! definition)
5240 {
5241 h->ref_dynamic = 1;
5242 hi->ref_dynamic = 1;
5243 }
5244 else
5245 {
5246 h->def_dynamic = 1;
5247 hi->def_dynamic = 1;
5248 }
5249 }
5250
5251 /* If an indirect symbol has been forced local, don't
5252 make the real symbol dynamic. */
5253 if (h != hi && hi->forced_local)
5254 ;
5255 else if (!dynamic)
5256 {
5257 if (bfd_link_dll (info)
5258 || h->def_dynamic
5259 || h->ref_dynamic)
5260 dynsym = true;
5261 }
5262 else
5263 {
5264 if (h->def_regular
5265 || h->ref_regular
5266 || (h->is_weakalias
5267 && weakdef (h)->dynindx != -1))
5268 dynsym = true;
5269 }
5270
5271 /* Check to see if we need to add an indirect symbol for
5272 the default name. */
5273 if ((definition
5274 || (!override && h->root.type == bfd_link_hash_common))
5275 && !(hi != h
5276 && hi->versioned == versioned_hidden))
5277 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5278 sec, value, &old_bfd, &dynsym))
5279 goto error_free_vers;
5280
5281 /* Check the alignment when a common symbol is involved. This
5282 can change when a common symbol is overridden by a normal
5283 definition or a common symbol is ignored due to the old
5284 normal definition. We need to make sure the maximum
5285 alignment is maintained. */
5286 if ((old_alignment || common)
5287 && h->root.type != bfd_link_hash_common)
5288 {
5289 unsigned int common_align;
5290 unsigned int normal_align;
5291 unsigned int symbol_align;
5292 bfd *normal_bfd;
5293 bfd *common_bfd;
5294
5295 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5296 || h->root.type == bfd_link_hash_defweak);
5297
5298 symbol_align = ffs (h->root.u.def.value) - 1;
5299 if (h->root.u.def.section->owner != NULL
5300 && (h->root.u.def.section->owner->flags
5301 & (DYNAMIC | BFD_PLUGIN)) == 0)
5302 {
5303 normal_align = h->root.u.def.section->alignment_power;
5304 if (normal_align > symbol_align)
5305 normal_align = symbol_align;
5306 }
5307 else
5308 normal_align = symbol_align;
5309
5310 if (old_alignment)
5311 {
5312 common_align = old_alignment;
5313 common_bfd = old_bfd;
5314 normal_bfd = abfd;
5315 }
5316 else
5317 {
5318 common_align = bfd_log2 (isym->st_value);
5319 common_bfd = abfd;
5320 normal_bfd = old_bfd;
5321 }
5322
5323 if (normal_align < common_align)
5324 {
5325 /* PR binutils/2735 */
5326 if (normal_bfd == NULL)
5327 _bfd_error_handler
5328 /* xgettext:c-format */
5329 (_("warning: alignment %u of common symbol `%s' in %pB is"
5330 " greater than the alignment (%u) of its section %pA"),
5331 1 << common_align, name, common_bfd,
5332 1 << normal_align, h->root.u.def.section);
5333 else
5334 _bfd_error_handler
5335 /* xgettext:c-format */
5336 (_("warning: alignment %u of normal symbol `%s' in %pB"
5337 " is smaller than %u used by the common definition in %pB"),
5338 1 << normal_align, name, normal_bfd,
5339 1 << common_align, common_bfd);
5340
5341 /* PR 30499: make sure that users understand that this warning is serious. */
5342 _bfd_error_handler
5343 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5344 }
5345 }
5346
5347 /* Remember the symbol size if it isn't undefined. */
5348 if (isym->st_size != 0
5349 && isym->st_shndx != SHN_UNDEF
5350 && (definition || h->size == 0))
5351 {
5352 if (h->size != 0
5353 && h->size != isym->st_size
5354 && ! size_change_ok)
5355 {
5356 _bfd_error_handler
5357 /* xgettext:c-format */
5358 (_("warning: size of symbol `%s' changed"
5359 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5360 name, (uint64_t) h->size, old_bfd,
5361 (uint64_t) isym->st_size, abfd);
5362
5363 /* PR 30499: make sure that users understand that this warning is serious. */
5364 _bfd_error_handler
5365 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5366 }
5367
5368 h->size = isym->st_size;
5369 }
5370
5371 /* If this is a common symbol, then we always want H->SIZE
5372 to be the size of the common symbol. The code just above
5373 won't fix the size if a common symbol becomes larger. We
5374 don't warn about a size change here, because that is
5375 covered by --warn-common. Allow changes between different
5376 function types. */
5377 if (h->root.type == bfd_link_hash_common)
5378 h->size = h->root.u.c.size;
5379
5380 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5381 && ((definition && !new_weak)
5382 || (old_weak && h->root.type == bfd_link_hash_common)
5383 || h->type == STT_NOTYPE))
5384 {
5385 unsigned int type = ELF_ST_TYPE (isym->st_info);
5386
5387 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5388 symbol. */
5389 if (type == STT_GNU_IFUNC
5390 && (abfd->flags & DYNAMIC) != 0)
5391 type = STT_FUNC;
5392
5393 if (h->type != type)
5394 {
5395 if (h->type != STT_NOTYPE && ! type_change_ok)
5396 /* xgettext:c-format */
5397 _bfd_error_handler
5398 (_("warning: type of symbol `%s' changed"
5399 " from %d to %d in %pB"),
5400 name, h->type, type, abfd);
5401
5402 h->type = type;
5403 }
5404 }
5405
5406 /* Merge st_other field. */
5407 elf_merge_st_other (abfd, h, isym->st_other, sec,
5408 definition, dynamic);
5409
5410 /* We don't want to make debug symbol dynamic. */
5411 if (definition
5412 && (sec->flags & SEC_DEBUGGING)
5413 && !bfd_link_relocatable (info))
5414 dynsym = false;
5415
5416 /* Nor should we make plugin symbols dynamic. */
5417 if ((abfd->flags & BFD_PLUGIN) != 0)
5418 dynsym = false;
5419
5420 if (definition)
5421 {
5422 h->target_internal = isym->st_target_internal;
5423 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5424 }
5425
5426 /* Don't add indirect symbols for .symver x, x@FOO aliases
5427 in IR. Since all data or text symbols in IR have the
5428 same type, value and section, we can't tell if a symbol
5429 is an alias of another symbol by their types, values and
5430 sections. */
5431 if (definition
5432 && !dynamic
5433 && (abfd->flags & BFD_PLUGIN) == 0)
5434 {
5435 char *p = strchr (name, ELF_VER_CHR);
5436 if (p != NULL && p[1] != ELF_VER_CHR)
5437 {
5438 /* Queue non-default versions so that .symver x, x@FOO
5439 aliases can be checked. */
5440 if (!nondeflt_vers)
5441 {
5442 size_t amt = ((isymend - isym + 1)
5443 * sizeof (struct elf_link_hash_entry *));
5444 nondeflt_vers
5445 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5446 if (!nondeflt_vers)
5447 goto error_free_vers;
5448 }
5449 nondeflt_vers[nondeflt_vers_cnt++] = h;
5450 }
5451 }
5452
5453 if (dynsym && h->dynindx == -1)
5454 {
5455 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5456 goto error_free_vers;
5457 if (h->is_weakalias
5458 && weakdef (h)->dynindx == -1)
5459 {
5460 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5461 goto error_free_vers;
5462 }
5463 }
5464 else if (h->dynindx != -1)
5465 /* If the symbol already has a dynamic index, but
5466 visibility says it should not be visible, turn it into
5467 a local symbol. */
5468 switch (ELF_ST_VISIBILITY (h->other))
5469 {
5470 case STV_INTERNAL:
5471 case STV_HIDDEN:
5472 (*bed->elf_backend_hide_symbol) (info, h, true);
5473 dynsym = false;
5474 break;
5475 }
5476
5477 if (!add_needed
5478 && matched
5479 && definition
5480 && h->root.type != bfd_link_hash_indirect
5481 && ((dynsym
5482 && h->ref_regular_nonweak)
5483 || (old_bfd != NULL
5484 && (old_bfd->flags & BFD_PLUGIN) != 0
5485 && h->ref_ir_nonweak
5486 && !info->lto_all_symbols_read)
5487 || (h->ref_dynamic_nonweak
5488 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5489 && !on_needed_list (elf_dt_name (abfd),
5490 htab->needed, NULL))))
5491 {
5492 const char *soname = elf_dt_name (abfd);
5493
5494 info->callbacks->minfo ("%!", soname, old_bfd,
5495 h->root.root.string);
5496
5497 /* A symbol from a library loaded via DT_NEEDED of some
5498 other library is referenced by a regular object.
5499 Add a DT_NEEDED entry for it. Issue an error if
5500 --no-add-needed is used and the reference was not
5501 a weak one. */
5502 if (old_bfd != NULL
5503 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5504 {
5505 _bfd_error_handler
5506 /* xgettext:c-format */
5507 (_("%pB: undefined reference to symbol '%s'"),
5508 old_bfd, name);
5509 bfd_set_error (bfd_error_missing_dso);
5510 goto error_free_vers;
5511 }
5512
5513 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5514 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5515
5516 /* Create dynamic sections for backends that require
5517 that be done before setup_gnu_properties. */
5518 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5519 return false;
5520 add_needed = true;
5521 }
5522 }
5523 }
5524
5525 if (info->lto_plugin_active
5526 && !bfd_link_relocatable (info)
5527 && (abfd->flags & BFD_PLUGIN) == 0
5528 && !just_syms
5529 && extsymcount)
5530 {
5531 int r_sym_shift;
5532
5533 if (bed->s->arch_size == 32)
5534 r_sym_shift = 8;
5535 else
5536 r_sym_shift = 32;
5537
5538 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5539 referenced in regular objects so that linker plugin will get
5540 the correct symbol resolution. */
5541
5542 sym_hash = elf_sym_hashes (abfd);
5543 for (s = abfd->sections; s != NULL; s = s->next)
5544 {
5545 Elf_Internal_Rela *internal_relocs;
5546 Elf_Internal_Rela *rel, *relend;
5547
5548 /* Don't check relocations in excluded sections. */
5549 if ((s->flags & SEC_RELOC) == 0
5550 || s->reloc_count == 0
5551 || (s->flags & SEC_EXCLUDE) != 0
5552 || ((info->strip == strip_all
5553 || info->strip == strip_debugger)
5554 && (s->flags & SEC_DEBUGGING) != 0))
5555 continue;
5556
5557 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5558 s, NULL,
5559 NULL,
5560 _bfd_link_keep_memory (info));
5561 if (internal_relocs == NULL)
5562 goto error_free_vers;
5563
5564 rel = internal_relocs;
5565 relend = rel + s->reloc_count;
5566 for ( ; rel < relend; rel++)
5567 {
5568 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5569 struct elf_link_hash_entry *h;
5570
5571 /* Skip local symbols. */
5572 if (r_symndx < extsymoff)
5573 continue;
5574
5575 h = sym_hash[r_symndx - extsymoff];
5576 if (h != NULL)
5577 h->root.non_ir_ref_regular = 1;
5578 }
5579
5580 if (elf_section_data (s)->relocs != internal_relocs)
5581 free (internal_relocs);
5582 }
5583 }
5584
5585 free (extversym);
5586 extversym = NULL;
5587 free (isymbuf);
5588 isymbuf = NULL;
5589
5590 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5591 {
5592 unsigned int i;
5593
5594 /* Restore the symbol table. */
5595 old_ent = (char *) old_tab + tabsize;
5596 memset (elf_sym_hashes (abfd), 0,
5597 extsymcount * sizeof (struct elf_link_hash_entry *));
5598 htab->root.table.table = old_table;
5599 htab->root.table.size = old_size;
5600 htab->root.table.count = old_count;
5601 memcpy (htab->root.table.table, old_tab, tabsize);
5602 htab->root.undefs = old_undefs;
5603 htab->root.undefs_tail = old_undefs_tail;
5604 if (htab->dynstr != NULL)
5605 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5606 free (old_strtab);
5607 old_strtab = NULL;
5608 for (i = 0; i < htab->root.table.size; i++)
5609 {
5610 struct bfd_hash_entry *p;
5611 struct elf_link_hash_entry *h;
5612 unsigned int non_ir_ref_dynamic;
5613
5614 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5615 {
5616 /* Preserve non_ir_ref_dynamic so that this symbol
5617 will be exported when the dynamic lib becomes needed
5618 in the second pass. */
5619 h = (struct elf_link_hash_entry *) p;
5620 if (h->root.type == bfd_link_hash_warning)
5621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5622 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5623
5624 h = (struct elf_link_hash_entry *) p;
5625 memcpy (h, old_ent, htab->root.table.entsize);
5626 old_ent = (char *) old_ent + htab->root.table.entsize;
5627 if (h->root.type == bfd_link_hash_warning)
5628 {
5629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5630 memcpy (h, old_ent, htab->root.table.entsize);
5631 old_ent = (char *) old_ent + htab->root.table.entsize;
5632 }
5633 if (h->root.type == bfd_link_hash_common)
5634 {
5635 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5636 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5637 }
5638 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5639 }
5640 }
5641
5642 /* Make a special call to the linker "notice" function to
5643 tell it that symbols added for crefs may need to be removed. */
5644 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5645 goto error_free_vers;
5646
5647 free (old_tab);
5648 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5649 alloc_mark);
5650 free (nondeflt_vers);
5651 return true;
5652 }
5653
5654 if (old_tab != NULL)
5655 {
5656 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5657 goto error_free_vers;
5658 free (old_tab);
5659 old_tab = NULL;
5660 }
5661
5662 /* Now that all the symbols from this input file are created, if
5663 not performing a relocatable link, handle .symver foo, foo@BAR
5664 such that any relocs against foo become foo@BAR. */
5665 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5666 {
5667 size_t cnt, symidx;
5668
5669 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5670 {
5671 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5672 char *shortname, *p;
5673 size_t amt;
5674
5675 p = strchr (h->root.root.string, ELF_VER_CHR);
5676 if (p == NULL
5677 || (h->root.type != bfd_link_hash_defined
5678 && h->root.type != bfd_link_hash_defweak))
5679 continue;
5680
5681 amt = p - h->root.root.string;
5682 shortname = (char *) bfd_malloc (amt + 1);
5683 if (!shortname)
5684 goto error_free_vers;
5685 memcpy (shortname, h->root.root.string, amt);
5686 shortname[amt] = '\0';
5687
5688 hi = (struct elf_link_hash_entry *)
5689 bfd_link_hash_lookup (&htab->root, shortname,
5690 false, false, false);
5691 if (hi != NULL
5692 && hi->root.type == h->root.type
5693 && hi->root.u.def.value == h->root.u.def.value
5694 && hi->root.u.def.section == h->root.u.def.section)
5695 {
5696 (*bed->elf_backend_hide_symbol) (info, hi, true);
5697 hi->root.type = bfd_link_hash_indirect;
5698 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5699 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5700 sym_hash = elf_sym_hashes (abfd);
5701 if (sym_hash)
5702 for (symidx = 0; symidx < extsymcount; ++symidx)
5703 if (sym_hash[symidx] == hi)
5704 {
5705 sym_hash[symidx] = h;
5706 break;
5707 }
5708 }
5709 free (shortname);
5710 }
5711 free (nondeflt_vers);
5712 nondeflt_vers = NULL;
5713 }
5714
5715 /* Now set the alias field correctly for all the weak defined
5716 symbols we found. The only way to do this is to search all the
5717 symbols. Since we only need the information for non functions in
5718 dynamic objects, that's the only time we actually put anything on
5719 the list WEAKS. We need this information so that if a regular
5720 object refers to a symbol defined weakly in a dynamic object, the
5721 real symbol in the dynamic object is also put in the dynamic
5722 symbols; we also must arrange for both symbols to point to the
5723 same memory location. We could handle the general case of symbol
5724 aliasing, but a general symbol alias can only be generated in
5725 assembler code, handling it correctly would be very time
5726 consuming, and other ELF linkers don't handle general aliasing
5727 either. */
5728 if (weaks != NULL)
5729 {
5730 struct elf_link_hash_entry **hpp;
5731 struct elf_link_hash_entry **hppend;
5732 struct elf_link_hash_entry **sorted_sym_hash;
5733 struct elf_link_hash_entry *h;
5734 size_t sym_count, amt;
5735
5736 /* Since we have to search the whole symbol list for each weak
5737 defined symbol, search time for N weak defined symbols will be
5738 O(N^2). Binary search will cut it down to O(NlogN). */
5739 amt = extsymcount * sizeof (*sorted_sym_hash);
5740 sorted_sym_hash = bfd_malloc (amt);
5741 if (sorted_sym_hash == NULL)
5742 goto error_return;
5743 sym_hash = sorted_sym_hash;
5744 hpp = elf_sym_hashes (abfd);
5745 hppend = hpp + extsymcount;
5746 sym_count = 0;
5747 for (; hpp < hppend; hpp++)
5748 {
5749 h = *hpp;
5750 if (h != NULL
5751 && h->root.type == bfd_link_hash_defined
5752 && !bed->is_function_type (h->type))
5753 {
5754 *sym_hash = h;
5755 sym_hash++;
5756 sym_count++;
5757 }
5758 }
5759
5760 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5761 elf_sort_symbol);
5762
5763 while (weaks != NULL)
5764 {
5765 struct elf_link_hash_entry *hlook;
5766 asection *slook;
5767 bfd_vma vlook;
5768 size_t i, j, idx = 0;
5769
5770 hlook = weaks;
5771 weaks = hlook->u.alias;
5772 hlook->u.alias = NULL;
5773
5774 if (hlook->root.type != bfd_link_hash_defined
5775 && hlook->root.type != bfd_link_hash_defweak)
5776 continue;
5777
5778 slook = hlook->root.u.def.section;
5779 vlook = hlook->root.u.def.value;
5780
5781 i = 0;
5782 j = sym_count;
5783 while (i != j)
5784 {
5785 bfd_signed_vma vdiff;
5786 idx = (i + j) / 2;
5787 h = sorted_sym_hash[idx];
5788 vdiff = vlook - h->root.u.def.value;
5789 if (vdiff < 0)
5790 j = idx;
5791 else if (vdiff > 0)
5792 i = idx + 1;
5793 else
5794 {
5795 int sdiff = slook->id - h->root.u.def.section->id;
5796 if (sdiff < 0)
5797 j = idx;
5798 else if (sdiff > 0)
5799 i = idx + 1;
5800 else
5801 break;
5802 }
5803 }
5804
5805 /* We didn't find a value/section match. */
5806 if (i == j)
5807 continue;
5808
5809 /* With multiple aliases, or when the weak symbol is already
5810 strongly defined, we have multiple matching symbols and
5811 the binary search above may land on any of them. Step
5812 one past the matching symbol(s). */
5813 while (++idx != j)
5814 {
5815 h = sorted_sym_hash[idx];
5816 if (h->root.u.def.section != slook
5817 || h->root.u.def.value != vlook)
5818 break;
5819 }
5820
5821 /* Now look back over the aliases. Since we sorted by size
5822 as well as value and section, we'll choose the one with
5823 the largest size. */
5824 while (idx-- != i)
5825 {
5826 h = sorted_sym_hash[idx];
5827
5828 /* Stop if value or section doesn't match. */
5829 if (h->root.u.def.section != slook
5830 || h->root.u.def.value != vlook)
5831 break;
5832 else if (h != hlook)
5833 {
5834 struct elf_link_hash_entry *t;
5835
5836 hlook->u.alias = h;
5837 hlook->is_weakalias = 1;
5838 t = h;
5839 if (t->u.alias != NULL)
5840 while (t->u.alias != h)
5841 t = t->u.alias;
5842 t->u.alias = hlook;
5843
5844 /* If the weak definition is in the list of dynamic
5845 symbols, make sure the real definition is put
5846 there as well. */
5847 if (hlook->dynindx != -1 && h->dynindx == -1)
5848 {
5849 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5850 {
5851 err_free_sym_hash:
5852 free (sorted_sym_hash);
5853 goto error_return;
5854 }
5855 }
5856
5857 /* If the real definition is in the list of dynamic
5858 symbols, make sure the weak definition is put
5859 there as well. If we don't do this, then the
5860 dynamic loader might not merge the entries for the
5861 real definition and the weak definition. */
5862 if (h->dynindx != -1 && hlook->dynindx == -1)
5863 {
5864 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5865 goto err_free_sym_hash;
5866 }
5867 break;
5868 }
5869 }
5870 }
5871
5872 free (sorted_sym_hash);
5873 }
5874
5875 if (bed->check_directives
5876 && !(*bed->check_directives) (abfd, info))
5877 return false;
5878
5879 /* If this is a non-traditional link, try to optimize the handling
5880 of the .stab/.stabstr sections. */
5881 if (! dynamic
5882 && ! info->traditional_format
5883 && is_elf_hash_table (&htab->root)
5884 && (info->strip != strip_all && info->strip != strip_debugger))
5885 {
5886 asection *stabstr;
5887
5888 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5889 if (stabstr != NULL)
5890 {
5891 bfd_size_type string_offset = 0;
5892 asection *stab;
5893
5894 for (stab = abfd->sections; stab; stab = stab->next)
5895 if (startswith (stab->name, ".stab")
5896 && (!stab->name[5] ||
5897 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5898 && (stab->flags & SEC_MERGE) == 0
5899 && !bfd_is_abs_section (stab->output_section))
5900 {
5901 struct bfd_elf_section_data *secdata;
5902
5903 secdata = elf_section_data (stab);
5904 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5905 stabstr, &secdata->sec_info,
5906 &string_offset))
5907 goto error_return;
5908 if (secdata->sec_info)
5909 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5910 }
5911 }
5912 }
5913
5914 if (dynamic && add_needed)
5915 {
5916 /* Add this bfd to the loaded list. */
5917 struct elf_link_loaded_list *n;
5918
5919 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5920 if (n == NULL)
5921 goto error_return;
5922 n->abfd = abfd;
5923 n->next = htab->dyn_loaded;
5924 htab->dyn_loaded = n;
5925 }
5926 if (dynamic && !add_needed
5927 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5928 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5929
5930 return true;
5931
5932 error_free_vers:
5933 free (old_tab);
5934 free (old_strtab);
5935 free (nondeflt_vers);
5936 free (extversym);
5937 error_free_sym:
5938 free (isymbuf);
5939 error_return:
5940 return false;
5941 }
5942
5943 /* Return the linker hash table entry of a symbol that might be
5944 satisfied by an archive symbol. Return -1 on error. */
5945
5946 struct bfd_link_hash_entry *
5947 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5948 struct bfd_link_info *info,
5949 const char *name)
5950 {
5951 struct bfd_link_hash_entry *h;
5952 char *p, *copy;
5953 size_t len, first;
5954
5955 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5956 if (h != NULL)
5957 return h;
5958
5959 /* If this is a default version (the name contains @@), look up the
5960 symbol again with only one `@' as well as without the version.
5961 The effect is that references to the symbol with and without the
5962 version will be matched by the default symbol in the archive. */
5963
5964 p = strchr (name, ELF_VER_CHR);
5965 if (p == NULL || p[1] != ELF_VER_CHR)
5966 return h;
5967
5968 /* First check with only one `@'. */
5969 len = strlen (name);
5970 copy = (char *) bfd_alloc (abfd, len);
5971 if (copy == NULL)
5972 return (struct bfd_link_hash_entry *) -1;
5973
5974 first = p - name + 1;
5975 memcpy (copy, name, first);
5976 memcpy (copy + first, name + first + 1, len - first);
5977
5978 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5979 if (h == NULL)
5980 {
5981 /* We also need to check references to the symbol without the
5982 version. */
5983 copy[first - 1] = '\0';
5984 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5985 }
5986
5987 bfd_release (abfd, copy);
5988 return h;
5989 }
5990
5991 /* Add symbols from an ELF archive file to the linker hash table. We
5992 don't use _bfd_generic_link_add_archive_symbols because we need to
5993 handle versioned symbols.
5994
5995 Fortunately, ELF archive handling is simpler than that done by
5996 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5997 oddities. In ELF, if we find a symbol in the archive map, and the
5998 symbol is currently undefined, we know that we must pull in that
5999 object file.
6000
6001 Unfortunately, we do have to make multiple passes over the symbol
6002 table until nothing further is resolved. */
6003
6004 static bool
6005 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6006 {
6007 symindex c;
6008 unsigned char *included = NULL;
6009 carsym *symdefs;
6010 bool loop;
6011 size_t amt;
6012 const struct elf_backend_data *bed;
6013 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6014 (bfd *, struct bfd_link_info *, const char *);
6015
6016 if (! bfd_has_map (abfd))
6017 {
6018 /* An empty archive is a special case. */
6019 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6020 return true;
6021 bfd_set_error (bfd_error_no_armap);
6022 return false;
6023 }
6024
6025 /* Keep track of all symbols we know to be already defined, and all
6026 files we know to be already included. This is to speed up the
6027 second and subsequent passes. */
6028 c = bfd_ardata (abfd)->symdef_count;
6029 if (c == 0)
6030 return true;
6031 amt = c * sizeof (*included);
6032 included = (unsigned char *) bfd_zmalloc (amt);
6033 if (included == NULL)
6034 return false;
6035
6036 symdefs = bfd_ardata (abfd)->symdefs;
6037 bed = get_elf_backend_data (abfd);
6038 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6039
6040 do
6041 {
6042 file_ptr last;
6043 symindex i;
6044 carsym *symdef;
6045 carsym *symdefend;
6046
6047 loop = false;
6048 last = -1;
6049
6050 symdef = symdefs;
6051 symdefend = symdef + c;
6052 for (i = 0; symdef < symdefend; symdef++, i++)
6053 {
6054 struct bfd_link_hash_entry *h;
6055 bfd *element;
6056 struct bfd_link_hash_entry *undefs_tail;
6057 symindex mark;
6058
6059 if (included[i])
6060 continue;
6061 if (symdef->file_offset == last)
6062 {
6063 included[i] = true;
6064 continue;
6065 }
6066
6067 h = archive_symbol_lookup (abfd, info, symdef->name);
6068 if (h == (struct bfd_link_hash_entry *) -1)
6069 goto error_return;
6070
6071 if (h == NULL)
6072 continue;
6073
6074 if (h->type == bfd_link_hash_undefined)
6075 {
6076 /* If the archive element has already been loaded then one
6077 of the symbols defined by that element might have been
6078 made undefined due to being in a discarded section. */
6079 if (is_elf_hash_table (info->hash)
6080 && ((struct elf_link_hash_entry *) h)->indx == -3)
6081 continue;
6082 }
6083 else if (h->type == bfd_link_hash_common)
6084 {
6085 /* We currently have a common symbol. The archive map contains
6086 a reference to this symbol, so we may want to include it. We
6087 only want to include it however, if this archive element
6088 contains a definition of the symbol, not just another common
6089 declaration of it.
6090
6091 Unfortunately some archivers (including GNU ar) will put
6092 declarations of common symbols into their archive maps, as
6093 well as real definitions, so we cannot just go by the archive
6094 map alone. Instead we must read in the element's symbol
6095 table and check that to see what kind of symbol definition
6096 this is. */
6097 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6098 continue;
6099 }
6100 else
6101 {
6102 if (h->type != bfd_link_hash_undefweak)
6103 /* Symbol must be defined. Don't check it again. */
6104 included[i] = true;
6105 continue;
6106 }
6107
6108 /* We need to include this archive member. */
6109 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6110 info);
6111 if (element == NULL)
6112 goto error_return;
6113
6114 if (! bfd_check_format (element, bfd_object))
6115 goto error_return;
6116
6117 undefs_tail = info->hash->undefs_tail;
6118
6119 if (!(*info->callbacks
6120 ->add_archive_element) (info, element, symdef->name, &element))
6121 continue;
6122 if (!bfd_link_add_symbols (element, info))
6123 goto error_return;
6124
6125 /* If there are any new undefined symbols, we need to make
6126 another pass through the archive in order to see whether
6127 they can be defined. FIXME: This isn't perfect, because
6128 common symbols wind up on undefs_tail and because an
6129 undefined symbol which is defined later on in this pass
6130 does not require another pass. This isn't a bug, but it
6131 does make the code less efficient than it could be. */
6132 if (undefs_tail != info->hash->undefs_tail)
6133 loop = true;
6134
6135 /* Look backward to mark all symbols from this object file
6136 which we have already seen in this pass. */
6137 mark = i;
6138 do
6139 {
6140 included[mark] = true;
6141 if (mark == 0)
6142 break;
6143 --mark;
6144 }
6145 while (symdefs[mark].file_offset == symdef->file_offset);
6146
6147 /* We mark subsequent symbols from this object file as we go
6148 on through the loop. */
6149 last = symdef->file_offset;
6150 }
6151 }
6152 while (loop);
6153
6154 free (included);
6155 return true;
6156
6157 error_return:
6158 free (included);
6159 return false;
6160 }
6161
6162 /* Given an ELF BFD, add symbols to the global hash table as
6163 appropriate. */
6164
6165 bool
6166 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6167 {
6168 switch (bfd_get_format (abfd))
6169 {
6170 case bfd_object:
6171 return elf_link_add_object_symbols (abfd, info);
6172 case bfd_archive:
6173 return elf_link_add_archive_symbols (abfd, info);
6174 default:
6175 bfd_set_error (bfd_error_wrong_format);
6176 return false;
6177 }
6178 }
6179 \f
6180 struct hash_codes_info
6181 {
6182 unsigned long *hashcodes;
6183 bool error;
6184 };
6185
6186 /* This function will be called though elf_link_hash_traverse to store
6187 all hash value of the exported symbols in an array. */
6188
6189 static bool
6190 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6191 {
6192 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6193 const char *name;
6194 unsigned long ha;
6195 char *alc = NULL;
6196
6197 /* Ignore indirect symbols. These are added by the versioning code. */
6198 if (h->dynindx == -1)
6199 return true;
6200
6201 name = h->root.root.string;
6202 if (h->versioned >= versioned)
6203 {
6204 char *p = strchr (name, ELF_VER_CHR);
6205 if (p != NULL)
6206 {
6207 alc = (char *) bfd_malloc (p - name + 1);
6208 if (alc == NULL)
6209 {
6210 inf->error = true;
6211 return false;
6212 }
6213 memcpy (alc, name, p - name);
6214 alc[p - name] = '\0';
6215 name = alc;
6216 }
6217 }
6218
6219 /* Compute the hash value. */
6220 ha = bfd_elf_hash (name);
6221
6222 /* Store the found hash value in the array given as the argument. */
6223 *(inf->hashcodes)++ = ha;
6224
6225 /* And store it in the struct so that we can put it in the hash table
6226 later. */
6227 h->u.elf_hash_value = ha;
6228
6229 free (alc);
6230 return true;
6231 }
6232
6233 struct collect_gnu_hash_codes
6234 {
6235 bfd *output_bfd;
6236 const struct elf_backend_data *bed;
6237 unsigned long int nsyms;
6238 unsigned long int maskbits;
6239 unsigned long int *hashcodes;
6240 unsigned long int *hashval;
6241 unsigned long int *indx;
6242 unsigned long int *counts;
6243 bfd_vma *bitmask;
6244 bfd_byte *contents;
6245 bfd_size_type xlat;
6246 long int min_dynindx;
6247 unsigned long int bucketcount;
6248 unsigned long int symindx;
6249 long int local_indx;
6250 long int shift1, shift2;
6251 unsigned long int mask;
6252 bool error;
6253 };
6254
6255 /* This function will be called though elf_link_hash_traverse to store
6256 all hash value of the exported symbols in an array. */
6257
6258 static bool
6259 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6260 {
6261 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6262 const char *name;
6263 unsigned long ha;
6264 char *alc = NULL;
6265
6266 /* Ignore indirect symbols. These are added by the versioning code. */
6267 if (h->dynindx == -1)
6268 return true;
6269
6270 /* Ignore also local symbols and undefined symbols. */
6271 if (! (*s->bed->elf_hash_symbol) (h))
6272 return true;
6273
6274 name = h->root.root.string;
6275 if (h->versioned >= versioned)
6276 {
6277 char *p = strchr (name, ELF_VER_CHR);
6278 if (p != NULL)
6279 {
6280 alc = (char *) bfd_malloc (p - name + 1);
6281 if (alc == NULL)
6282 {
6283 s->error = true;
6284 return false;
6285 }
6286 memcpy (alc, name, p - name);
6287 alc[p - name] = '\0';
6288 name = alc;
6289 }
6290 }
6291
6292 /* Compute the hash value. */
6293 ha = bfd_elf_gnu_hash (name);
6294
6295 /* Store the found hash value in the array for compute_bucket_count,
6296 and also for .dynsym reordering purposes. */
6297 s->hashcodes[s->nsyms] = ha;
6298 s->hashval[h->dynindx] = ha;
6299 ++s->nsyms;
6300 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6301 s->min_dynindx = h->dynindx;
6302
6303 free (alc);
6304 return true;
6305 }
6306
6307 /* This function will be called though elf_link_hash_traverse to do
6308 final dynamic symbol renumbering in case of .gnu.hash.
6309 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6310 to the translation table. */
6311
6312 static bool
6313 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6314 {
6315 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6316 unsigned long int bucket;
6317 unsigned long int val;
6318
6319 /* Ignore indirect symbols. */
6320 if (h->dynindx == -1)
6321 return true;
6322
6323 /* Ignore also local symbols and undefined symbols. */
6324 if (! (*s->bed->elf_hash_symbol) (h))
6325 {
6326 if (h->dynindx >= s->min_dynindx)
6327 {
6328 if (s->bed->record_xhash_symbol != NULL)
6329 {
6330 (*s->bed->record_xhash_symbol) (h, 0);
6331 s->local_indx++;
6332 }
6333 else
6334 h->dynindx = s->local_indx++;
6335 }
6336 return true;
6337 }
6338
6339 bucket = s->hashval[h->dynindx] % s->bucketcount;
6340 val = (s->hashval[h->dynindx] >> s->shift1)
6341 & ((s->maskbits >> s->shift1) - 1);
6342 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6343 s->bitmask[val]
6344 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6345 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6346 if (s->counts[bucket] == 1)
6347 /* Last element terminates the chain. */
6348 val |= 1;
6349 bfd_put_32 (s->output_bfd, val,
6350 s->contents + (s->indx[bucket] - s->symindx) * 4);
6351 --s->counts[bucket];
6352 if (s->bed->record_xhash_symbol != NULL)
6353 {
6354 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6355
6356 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6357 }
6358 else
6359 h->dynindx = s->indx[bucket]++;
6360 return true;
6361 }
6362
6363 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6364
6365 bool
6366 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6367 {
6368 return !(h->forced_local
6369 || h->root.type == bfd_link_hash_undefined
6370 || h->root.type == bfd_link_hash_undefweak
6371 || ((h->root.type == bfd_link_hash_defined
6372 || h->root.type == bfd_link_hash_defweak)
6373 && h->root.u.def.section->output_section == NULL));
6374 }
6375
6376 /* Array used to determine the number of hash table buckets to use
6377 based on the number of symbols there are. If there are fewer than
6378 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6379 fewer than 37 we use 17 buckets, and so forth. We never use more
6380 than 32771 buckets. */
6381
6382 static const size_t elf_buckets[] =
6383 {
6384 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6385 16411, 32771, 0
6386 };
6387
6388 /* Compute bucket count for hashing table. We do not use a static set
6389 of possible tables sizes anymore. Instead we determine for all
6390 possible reasonable sizes of the table the outcome (i.e., the
6391 number of collisions etc) and choose the best solution. The
6392 weighting functions are not too simple to allow the table to grow
6393 without bounds. Instead one of the weighting factors is the size.
6394 Therefore the result is always a good payoff between few collisions
6395 (= short chain lengths) and table size. */
6396 static size_t
6397 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6398 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6399 unsigned long int nsyms,
6400 int gnu_hash)
6401 {
6402 size_t best_size = 0;
6403 unsigned long int i;
6404
6405 if (info->optimize)
6406 {
6407 size_t minsize;
6408 size_t maxsize;
6409 uint64_t best_chlen = ~((uint64_t) 0);
6410 bfd *dynobj = elf_hash_table (info)->dynobj;
6411 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6412 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6413 unsigned long int *counts;
6414 bfd_size_type amt;
6415 unsigned int no_improvement_count = 0;
6416
6417 /* Possible optimization parameters: if we have NSYMS symbols we say
6418 that the hashing table must at least have NSYMS/4 and at most
6419 2*NSYMS buckets. */
6420 minsize = nsyms / 4;
6421 if (minsize == 0)
6422 minsize = 1;
6423 best_size = maxsize = nsyms * 2;
6424 if (gnu_hash)
6425 {
6426 if (minsize < 2)
6427 minsize = 2;
6428 if ((best_size & 31) == 0)
6429 ++best_size;
6430 }
6431
6432 /* Create array where we count the collisions in. We must use bfd_malloc
6433 since the size could be large. */
6434 amt = maxsize;
6435 amt *= sizeof (unsigned long int);
6436 counts = (unsigned long int *) bfd_malloc (amt);
6437 if (counts == NULL)
6438 return 0;
6439
6440 /* Compute the "optimal" size for the hash table. The criteria is a
6441 minimal chain length. The minor criteria is (of course) the size
6442 of the table. */
6443 for (i = minsize; i < maxsize; ++i)
6444 {
6445 /* Walk through the array of hashcodes and count the collisions. */
6446 uint64_t max;
6447 unsigned long int j;
6448 unsigned long int fact;
6449
6450 if (gnu_hash && (i & 31) == 0)
6451 continue;
6452
6453 memset (counts, '\0', i * sizeof (unsigned long int));
6454
6455 /* Determine how often each hash bucket is used. */
6456 for (j = 0; j < nsyms; ++j)
6457 ++counts[hashcodes[j] % i];
6458
6459 /* For the weight function we need some information about the
6460 pagesize on the target. This is information need not be 100%
6461 accurate. Since this information is not available (so far) we
6462 define it here to a reasonable default value. If it is crucial
6463 to have a better value some day simply define this value. */
6464 # ifndef BFD_TARGET_PAGESIZE
6465 # define BFD_TARGET_PAGESIZE (4096)
6466 # endif
6467
6468 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6469 and the chains. */
6470 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6471
6472 # if 1
6473 /* Variant 1: optimize for short chains. We add the squares
6474 of all the chain lengths (which favors many small chain
6475 over a few long chains). */
6476 for (j = 0; j < i; ++j)
6477 max += counts[j] * counts[j];
6478
6479 /* This adds penalties for the overall size of the table. */
6480 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6481 max *= fact * fact;
6482 # else
6483 /* Variant 2: Optimize a lot more for small table. Here we
6484 also add squares of the size but we also add penalties for
6485 empty slots (the +1 term). */
6486 for (j = 0; j < i; ++j)
6487 max += (1 + counts[j]) * (1 + counts[j]);
6488
6489 /* The overall size of the table is considered, but not as
6490 strong as in variant 1, where it is squared. */
6491 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6492 max *= fact;
6493 # endif
6494
6495 /* Compare with current best results. */
6496 if (max < best_chlen)
6497 {
6498 best_chlen = max;
6499 best_size = i;
6500 no_improvement_count = 0;
6501 }
6502 /* PR 11843: Avoid futile long searches for the best bucket size
6503 when there are a large number of symbols. */
6504 else if (++no_improvement_count == 100)
6505 break;
6506 }
6507
6508 free (counts);
6509 }
6510 else
6511 {
6512 for (i = 0; elf_buckets[i] != 0; i++)
6513 {
6514 best_size = elf_buckets[i];
6515 if (nsyms < elf_buckets[i + 1])
6516 break;
6517 }
6518 if (gnu_hash && best_size < 2)
6519 best_size = 2;
6520 }
6521
6522 return best_size;
6523 }
6524
6525 /* Size any SHT_GROUP section for ld -r. */
6526
6527 bool
6528 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6529 {
6530 bfd *ibfd;
6531 asection *s;
6532
6533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6534 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6535 && (s = ibfd->sections) != NULL
6536 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6537 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6538 return false;
6539 return true;
6540 }
6541
6542 /* Set a default stack segment size. The value in INFO wins. If it
6543 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6544 undefined it is initialized. */
6545
6546 bool
6547 bfd_elf_stack_segment_size (bfd *output_bfd,
6548 struct bfd_link_info *info,
6549 const char *legacy_symbol,
6550 bfd_vma default_size)
6551 {
6552 struct elf_link_hash_entry *h = NULL;
6553
6554 /* Look for legacy symbol. */
6555 if (legacy_symbol)
6556 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6557 false, false, false);
6558 if (h && (h->root.type == bfd_link_hash_defined
6559 || h->root.type == bfd_link_hash_defweak)
6560 && h->def_regular
6561 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6562 {
6563 /* The symbol has no type if specified on the command line. */
6564 h->type = STT_OBJECT;
6565 if (info->stacksize)
6566 /* xgettext:c-format */
6567 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6568 output_bfd, legacy_symbol);
6569 else if (h->root.u.def.section != bfd_abs_section_ptr)
6570 /* xgettext:c-format */
6571 _bfd_error_handler (_("%pB: %s not absolute"),
6572 output_bfd, legacy_symbol);
6573 else
6574 info->stacksize = h->root.u.def.value;
6575 }
6576
6577 if (!info->stacksize)
6578 /* If the user didn't set a size, or explicitly inhibit the
6579 size, set it now. */
6580 info->stacksize = default_size;
6581
6582 /* Provide the legacy symbol, if it is referenced. */
6583 if (h && (h->root.type == bfd_link_hash_undefined
6584 || h->root.type == bfd_link_hash_undefweak))
6585 {
6586 struct bfd_link_hash_entry *bh = NULL;
6587
6588 if (!(_bfd_generic_link_add_one_symbol
6589 (info, output_bfd, legacy_symbol,
6590 BSF_GLOBAL, bfd_abs_section_ptr,
6591 info->stacksize >= 0 ? info->stacksize : 0,
6592 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6593 return false;
6594
6595 h = (struct elf_link_hash_entry *) bh;
6596 h->def_regular = 1;
6597 h->type = STT_OBJECT;
6598 }
6599
6600 return true;
6601 }
6602
6603 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6604
6605 struct elf_gc_sweep_symbol_info
6606 {
6607 struct bfd_link_info *info;
6608 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6609 bool);
6610 };
6611
6612 static bool
6613 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6614 {
6615 if (!h->mark
6616 && (((h->root.type == bfd_link_hash_defined
6617 || h->root.type == bfd_link_hash_defweak)
6618 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6619 && h->root.u.def.section->gc_mark))
6620 || h->root.type == bfd_link_hash_undefined
6621 || h->root.type == bfd_link_hash_undefweak))
6622 {
6623 struct elf_gc_sweep_symbol_info *inf;
6624
6625 inf = (struct elf_gc_sweep_symbol_info *) data;
6626 (*inf->hide_symbol) (inf->info, h, true);
6627 h->def_regular = 0;
6628 h->ref_regular = 0;
6629 h->ref_regular_nonweak = 0;
6630 }
6631
6632 return true;
6633 }
6634
6635 /* Set up the sizes and contents of the ELF dynamic sections. This is
6636 called by the ELF linker emulation before_allocation routine. We
6637 must set the sizes of the sections before the linker sets the
6638 addresses of the various sections. */
6639
6640 bool
6641 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6642 const char *soname,
6643 const char *rpath,
6644 const char *filter_shlib,
6645 const char *audit,
6646 const char *depaudit,
6647 const char * const *auxiliary_filters,
6648 struct bfd_link_info *info,
6649 asection **sinterpptr)
6650 {
6651 bfd *dynobj;
6652 const struct elf_backend_data *bed;
6653
6654 *sinterpptr = NULL;
6655
6656 if (!is_elf_hash_table (info->hash))
6657 return true;
6658
6659 /* Any syms created from now on start with -1 in
6660 got.refcount/offset and plt.refcount/offset. */
6661 elf_hash_table (info)->init_got_refcount
6662 = elf_hash_table (info)->init_got_offset;
6663 elf_hash_table (info)->init_plt_refcount
6664 = elf_hash_table (info)->init_plt_offset;
6665
6666 bed = get_elf_backend_data (output_bfd);
6667
6668 /* The backend may have to create some sections regardless of whether
6669 we're dynamic or not. */
6670 if (bed->elf_backend_early_size_sections
6671 && !bed->elf_backend_early_size_sections (output_bfd, info))
6672 return false;
6673
6674 dynobj = elf_hash_table (info)->dynobj;
6675
6676 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6677 {
6678 struct bfd_elf_version_tree *verdefs;
6679 struct elf_info_failed asvinfo;
6680 struct bfd_elf_version_tree *t;
6681 struct bfd_elf_version_expr *d;
6682 asection *s;
6683 size_t soname_indx;
6684
6685 /* If we are supposed to export all symbols into the dynamic symbol
6686 table (this is not the normal case), then do so. */
6687 if (info->export_dynamic
6688 || (bfd_link_executable (info) && info->dynamic))
6689 {
6690 struct elf_info_failed eif;
6691
6692 eif.info = info;
6693 eif.failed = false;
6694 elf_link_hash_traverse (elf_hash_table (info),
6695 _bfd_elf_export_symbol,
6696 &eif);
6697 if (eif.failed)
6698 return false;
6699 }
6700
6701 if (soname != NULL)
6702 {
6703 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6704 soname, true);
6705 if (soname_indx == (size_t) -1
6706 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6707 return false;
6708 }
6709 else
6710 soname_indx = (size_t) -1;
6711
6712 /* Make all global versions with definition. */
6713 for (t = info->version_info; t != NULL; t = t->next)
6714 for (d = t->globals.list; d != NULL; d = d->next)
6715 if (!d->symver && d->literal)
6716 {
6717 const char *verstr, *name;
6718 size_t namelen, verlen, newlen;
6719 char *newname, *p, leading_char;
6720 struct elf_link_hash_entry *newh;
6721
6722 leading_char = bfd_get_symbol_leading_char (output_bfd);
6723 name = d->pattern;
6724 namelen = strlen (name) + (leading_char != '\0');
6725 verstr = t->name;
6726 verlen = strlen (verstr);
6727 newlen = namelen + verlen + 3;
6728
6729 newname = (char *) bfd_malloc (newlen);
6730 if (newname == NULL)
6731 return false;
6732 newname[0] = leading_char;
6733 memcpy (newname + (leading_char != '\0'), name, namelen);
6734
6735 /* Check the hidden versioned definition. */
6736 p = newname + namelen;
6737 *p++ = ELF_VER_CHR;
6738 memcpy (p, verstr, verlen + 1);
6739 newh = elf_link_hash_lookup (elf_hash_table (info),
6740 newname, false, false,
6741 false);
6742 if (newh == NULL
6743 || (newh->root.type != bfd_link_hash_defined
6744 && newh->root.type != bfd_link_hash_defweak))
6745 {
6746 /* Check the default versioned definition. */
6747 *p++ = ELF_VER_CHR;
6748 memcpy (p, verstr, verlen + 1);
6749 newh = elf_link_hash_lookup (elf_hash_table (info),
6750 newname, false, false,
6751 false);
6752 }
6753 free (newname);
6754
6755 /* Mark this version if there is a definition and it is
6756 not defined in a shared object. */
6757 if (newh != NULL
6758 && !newh->def_dynamic
6759 && (newh->root.type == bfd_link_hash_defined
6760 || newh->root.type == bfd_link_hash_defweak))
6761 d->symver = 1;
6762 }
6763
6764 /* Attach all the symbols to their version information. */
6765 asvinfo.info = info;
6766 asvinfo.failed = false;
6767
6768 elf_link_hash_traverse (elf_hash_table (info),
6769 _bfd_elf_link_assign_sym_version,
6770 &asvinfo);
6771 if (asvinfo.failed)
6772 return false;
6773
6774 if (!info->allow_undefined_version)
6775 {
6776 /* Check if all global versions have a definition. */
6777 bool all_defined = true;
6778 for (t = info->version_info; t != NULL; t = t->next)
6779 for (d = t->globals.list; d != NULL; d = d->next)
6780 if (d->literal && !d->symver && !d->script)
6781 {
6782 _bfd_error_handler
6783 (_("%s: undefined version: %s"),
6784 d->pattern, t->name);
6785 all_defined = false;
6786 }
6787
6788 if (!all_defined)
6789 {
6790 bfd_set_error (bfd_error_bad_value);
6791 return false;
6792 }
6793 }
6794
6795 /* Set up the version definition section. */
6796 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6797 BFD_ASSERT (s != NULL);
6798
6799 /* We may have created additional version definitions if we are
6800 just linking a regular application. */
6801 verdefs = info->version_info;
6802
6803 /* Skip anonymous version tag. */
6804 if (verdefs != NULL && verdefs->vernum == 0)
6805 verdefs = verdefs->next;
6806
6807 if (verdefs == NULL && !info->create_default_symver)
6808 s->flags |= SEC_EXCLUDE;
6809 else
6810 {
6811 unsigned int cdefs;
6812 bfd_size_type size;
6813 bfd_byte *p;
6814 Elf_Internal_Verdef def;
6815 Elf_Internal_Verdaux defaux;
6816 struct bfd_link_hash_entry *bh;
6817 struct elf_link_hash_entry *h;
6818 const char *name;
6819
6820 cdefs = 0;
6821 size = 0;
6822
6823 /* Make space for the base version. */
6824 size += sizeof (Elf_External_Verdef);
6825 size += sizeof (Elf_External_Verdaux);
6826 ++cdefs;
6827
6828 /* Make space for the default version. */
6829 if (info->create_default_symver)
6830 {
6831 size += sizeof (Elf_External_Verdef);
6832 ++cdefs;
6833 }
6834
6835 for (t = verdefs; t != NULL; t = t->next)
6836 {
6837 struct bfd_elf_version_deps *n;
6838
6839 /* Don't emit base version twice. */
6840 if (t->vernum == 0)
6841 continue;
6842
6843 size += sizeof (Elf_External_Verdef);
6844 size += sizeof (Elf_External_Verdaux);
6845 ++cdefs;
6846
6847 for (n = t->deps; n != NULL; n = n->next)
6848 size += sizeof (Elf_External_Verdaux);
6849 }
6850
6851 s->size = size;
6852 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6853 if (s->contents == NULL && s->size != 0)
6854 return false;
6855
6856 /* Fill in the version definition section. */
6857
6858 p = s->contents;
6859
6860 def.vd_version = VER_DEF_CURRENT;
6861 def.vd_flags = VER_FLG_BASE;
6862 def.vd_ndx = 1;
6863 def.vd_cnt = 1;
6864 if (info->create_default_symver)
6865 {
6866 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6867 def.vd_next = sizeof (Elf_External_Verdef);
6868 }
6869 else
6870 {
6871 def.vd_aux = sizeof (Elf_External_Verdef);
6872 def.vd_next = (sizeof (Elf_External_Verdef)
6873 + sizeof (Elf_External_Verdaux));
6874 }
6875
6876 if (soname_indx != (size_t) -1)
6877 {
6878 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6879 soname_indx);
6880 def.vd_hash = bfd_elf_hash (soname);
6881 defaux.vda_name = soname_indx;
6882 name = soname;
6883 }
6884 else
6885 {
6886 size_t indx;
6887
6888 name = lbasename (bfd_get_filename (output_bfd));
6889 def.vd_hash = bfd_elf_hash (name);
6890 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6891 name, false);
6892 if (indx == (size_t) -1)
6893 return false;
6894 defaux.vda_name = indx;
6895 }
6896 defaux.vda_next = 0;
6897
6898 _bfd_elf_swap_verdef_out (output_bfd, &def,
6899 (Elf_External_Verdef *) p);
6900 p += sizeof (Elf_External_Verdef);
6901 if (info->create_default_symver)
6902 {
6903 /* Add a symbol representing this version. */
6904 bh = NULL;
6905 if (! (_bfd_generic_link_add_one_symbol
6906 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6907 0, NULL, false,
6908 get_elf_backend_data (dynobj)->collect, &bh)))
6909 return false;
6910 h = (struct elf_link_hash_entry *) bh;
6911 h->non_elf = 0;
6912 h->def_regular = 1;
6913 h->type = STT_OBJECT;
6914 h->verinfo.vertree = NULL;
6915
6916 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6917 return false;
6918
6919 /* Create a duplicate of the base version with the same
6920 aux block, but different flags. */
6921 def.vd_flags = 0;
6922 def.vd_ndx = 2;
6923 def.vd_aux = sizeof (Elf_External_Verdef);
6924 if (verdefs)
6925 def.vd_next = (sizeof (Elf_External_Verdef)
6926 + sizeof (Elf_External_Verdaux));
6927 else
6928 def.vd_next = 0;
6929 _bfd_elf_swap_verdef_out (output_bfd, &def,
6930 (Elf_External_Verdef *) p);
6931 p += sizeof (Elf_External_Verdef);
6932 }
6933 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6934 (Elf_External_Verdaux *) p);
6935 p += sizeof (Elf_External_Verdaux);
6936
6937 for (t = verdefs; t != NULL; t = t->next)
6938 {
6939 unsigned int cdeps;
6940 struct bfd_elf_version_deps *n;
6941
6942 /* Don't emit the base version twice. */
6943 if (t->vernum == 0)
6944 continue;
6945
6946 cdeps = 0;
6947 for (n = t->deps; n != NULL; n = n->next)
6948 ++cdeps;
6949
6950 /* Add a symbol representing this version. */
6951 bh = NULL;
6952 if (! (_bfd_generic_link_add_one_symbol
6953 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6954 0, NULL, false,
6955 get_elf_backend_data (dynobj)->collect, &bh)))
6956 return false;
6957 h = (struct elf_link_hash_entry *) bh;
6958 h->non_elf = 0;
6959 h->def_regular = 1;
6960 h->type = STT_OBJECT;
6961 h->verinfo.vertree = t;
6962
6963 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6964 return false;
6965
6966 def.vd_version = VER_DEF_CURRENT;
6967 def.vd_flags = 0;
6968 if (t->globals.list == NULL
6969 && t->locals.list == NULL
6970 && ! t->used)
6971 def.vd_flags |= VER_FLG_WEAK;
6972 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6973 def.vd_cnt = cdeps + 1;
6974 def.vd_hash = bfd_elf_hash (t->name);
6975 def.vd_aux = sizeof (Elf_External_Verdef);
6976 def.vd_next = 0;
6977
6978 /* If a basever node is next, it *must* be the last node in
6979 the chain, otherwise Verdef construction breaks. */
6980 if (t->next != NULL && t->next->vernum == 0)
6981 BFD_ASSERT (t->next->next == NULL);
6982
6983 if (t->next != NULL && t->next->vernum != 0)
6984 def.vd_next = (sizeof (Elf_External_Verdef)
6985 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6986
6987 _bfd_elf_swap_verdef_out (output_bfd, &def,
6988 (Elf_External_Verdef *) p);
6989 p += sizeof (Elf_External_Verdef);
6990
6991 defaux.vda_name = h->dynstr_index;
6992 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6993 h->dynstr_index);
6994 defaux.vda_next = 0;
6995 if (t->deps != NULL)
6996 defaux.vda_next = sizeof (Elf_External_Verdaux);
6997 t->name_indx = defaux.vda_name;
6998
6999 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7000 (Elf_External_Verdaux *) p);
7001 p += sizeof (Elf_External_Verdaux);
7002
7003 for (n = t->deps; n != NULL; n = n->next)
7004 {
7005 if (n->version_needed == NULL)
7006 {
7007 /* This can happen if there was an error in the
7008 version script. */
7009 defaux.vda_name = 0;
7010 }
7011 else
7012 {
7013 defaux.vda_name = n->version_needed->name_indx;
7014 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7015 defaux.vda_name);
7016 }
7017 if (n->next == NULL)
7018 defaux.vda_next = 0;
7019 else
7020 defaux.vda_next = sizeof (Elf_External_Verdaux);
7021
7022 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7023 (Elf_External_Verdaux *) p);
7024 p += sizeof (Elf_External_Verdaux);
7025 }
7026 }
7027
7028 elf_tdata (output_bfd)->cverdefs = cdefs;
7029 }
7030 }
7031
7032 if (info->gc_sections && bed->can_gc_sections)
7033 {
7034 struct elf_gc_sweep_symbol_info sweep_info;
7035
7036 /* Remove the symbols that were in the swept sections from the
7037 dynamic symbol table. */
7038 sweep_info.info = info;
7039 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7040 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7041 &sweep_info);
7042 }
7043
7044 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7045 {
7046 asection *s;
7047 struct elf_find_verdep_info sinfo;
7048
7049 /* Work out the size of the version reference section. */
7050
7051 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7052 BFD_ASSERT (s != NULL);
7053
7054 sinfo.info = info;
7055 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7056 if (sinfo.vers == 0)
7057 sinfo.vers = 1;
7058 sinfo.failed = false;
7059
7060 elf_link_hash_traverse (elf_hash_table (info),
7061 _bfd_elf_link_find_version_dependencies,
7062 &sinfo);
7063 if (sinfo.failed)
7064 return false;
7065
7066 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7067 if (sinfo.failed)
7068 return false;
7069
7070 if (elf_tdata (output_bfd)->verref == NULL)
7071 s->flags |= SEC_EXCLUDE;
7072 else
7073 {
7074 Elf_Internal_Verneed *vn;
7075 unsigned int size;
7076 unsigned int crefs;
7077 bfd_byte *p;
7078
7079 /* Build the version dependency section. */
7080 size = 0;
7081 crefs = 0;
7082 for (vn = elf_tdata (output_bfd)->verref;
7083 vn != NULL;
7084 vn = vn->vn_nextref)
7085 {
7086 Elf_Internal_Vernaux *a;
7087
7088 size += sizeof (Elf_External_Verneed);
7089 ++crefs;
7090 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7091 size += sizeof (Elf_External_Vernaux);
7092 }
7093
7094 s->size = size;
7095 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7096 if (s->contents == NULL)
7097 return false;
7098
7099 p = s->contents;
7100 for (vn = elf_tdata (output_bfd)->verref;
7101 vn != NULL;
7102 vn = vn->vn_nextref)
7103 {
7104 unsigned int caux;
7105 Elf_Internal_Vernaux *a;
7106 size_t indx;
7107
7108 caux = 0;
7109 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7110 ++caux;
7111
7112 vn->vn_version = VER_NEED_CURRENT;
7113 vn->vn_cnt = caux;
7114 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7115 elf_dt_name (vn->vn_bfd) != NULL
7116 ? elf_dt_name (vn->vn_bfd)
7117 : lbasename (bfd_get_filename
7118 (vn->vn_bfd)),
7119 false);
7120 if (indx == (size_t) -1)
7121 return false;
7122 vn->vn_file = indx;
7123 vn->vn_aux = sizeof (Elf_External_Verneed);
7124 if (vn->vn_nextref == NULL)
7125 vn->vn_next = 0;
7126 else
7127 vn->vn_next = (sizeof (Elf_External_Verneed)
7128 + caux * sizeof (Elf_External_Vernaux));
7129
7130 _bfd_elf_swap_verneed_out (output_bfd, vn,
7131 (Elf_External_Verneed *) p);
7132 p += sizeof (Elf_External_Verneed);
7133
7134 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7135 {
7136 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7137 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7138 a->vna_nodename, false);
7139 if (indx == (size_t) -1)
7140 return false;
7141 a->vna_name = indx;
7142 if (a->vna_nextptr == NULL)
7143 a->vna_next = 0;
7144 else
7145 a->vna_next = sizeof (Elf_External_Vernaux);
7146
7147 _bfd_elf_swap_vernaux_out (output_bfd, a,
7148 (Elf_External_Vernaux *) p);
7149 p += sizeof (Elf_External_Vernaux);
7150 }
7151 }
7152
7153 elf_tdata (output_bfd)->cverrefs = crefs;
7154 }
7155 }
7156
7157 if (bfd_link_relocatable (info)
7158 && !_bfd_elf_size_group_sections (info))
7159 return false;
7160
7161 /* Determine any GNU_STACK segment requirements, after the backend
7162 has had a chance to set a default segment size. */
7163 if (info->execstack)
7164 {
7165 /* If the user has explicitly requested warnings, then generate one even
7166 though the choice is the result of another command line option. */
7167 if (info->warn_execstack == 1)
7168 {
7169 if (info->error_execstack)
7170 {
7171 _bfd_error_handler
7172 (_("\
7173 error: creating an executable stack because of -z execstack command line option"));
7174 return false;
7175 }
7176
7177 _bfd_error_handler
7178 (_("\
7179 warning: enabling an executable stack because of -z execstack command line option"));
7180 }
7181
7182 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7183 }
7184 else if (info->noexecstack)
7185 elf_stack_flags (output_bfd) = PF_R | PF_W;
7186 else
7187 {
7188 bfd *inputobj;
7189 asection *notesec = NULL;
7190 bfd *noteobj = NULL;
7191 bfd *emptyobj = NULL;
7192 int exec = 0;
7193
7194 for (inputobj = info->input_bfds;
7195 inputobj;
7196 inputobj = inputobj->link.next)
7197 {
7198 asection *s;
7199
7200 if (inputobj->flags
7201 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7202 continue;
7203 s = inputobj->sections;
7204 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7205 continue;
7206
7207 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7208 if (s)
7209 {
7210 notesec = s;
7211 if (s->flags & SEC_CODE)
7212 {
7213 noteobj = inputobj;
7214 exec = PF_X;
7215 /* There is no point in scanning the remaining bfds. */
7216 break;
7217 }
7218 }
7219 else if (bed->default_execstack && info->default_execstack)
7220 {
7221 exec = PF_X;
7222 emptyobj = inputobj;
7223 }
7224 }
7225
7226 if (notesec || info->stacksize > 0)
7227 {
7228 if (exec)
7229 {
7230 if (info->warn_execstack != 0)
7231 {
7232 /* PR 29072: Because an executable stack is a serious
7233 security risk, make sure that the user knows that it is
7234 being enabled despite the fact that it was not requested
7235 on the command line. */
7236 if (noteobj)
7237 {
7238 if (info->error_execstack)
7239 {
7240 _bfd_error_handler (_("\
7241 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7242 bfd_get_filename (noteobj));
7243 return false;
7244 }
7245
7246 _bfd_error_handler (_("\
7247 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7248 bfd_get_filename (noteobj));
7249 }
7250 else if (emptyobj)
7251 {
7252 if (info->error_execstack)
7253 {
7254 _bfd_error_handler (_("\
7255 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7256 bfd_get_filename (emptyobj));
7257 return false;
7258 }
7259
7260 _bfd_error_handler (_("\
7261 warning: %s: missing .note.GNU-stack section implies executable stack"),
7262 bfd_get_filename (emptyobj));
7263 _bfd_error_handler (_("\
7264 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7265 }
7266 }
7267 }
7268 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7269 }
7270
7271 if (notesec && exec && bfd_link_relocatable (info)
7272 && notesec->output_section != bfd_abs_section_ptr)
7273 notesec->output_section->flags |= SEC_CODE;
7274 }
7275
7276 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7277 {
7278 struct elf_info_failed eif;
7279 struct elf_link_hash_entry *h;
7280 asection *dynstr;
7281 asection *s;
7282
7283 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7284 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7285
7286 if (info->symbolic)
7287 {
7288 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7289 return false;
7290 info->flags |= DF_SYMBOLIC;
7291 }
7292
7293 if (rpath != NULL)
7294 {
7295 size_t indx;
7296 bfd_vma tag;
7297
7298 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7299 true);
7300 if (indx == (size_t) -1)
7301 return false;
7302
7303 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7304 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7305 return false;
7306 }
7307
7308 if (filter_shlib != NULL)
7309 {
7310 size_t indx;
7311
7312 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7313 filter_shlib, true);
7314 if (indx == (size_t) -1
7315 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7316 return false;
7317 }
7318
7319 if (auxiliary_filters != NULL)
7320 {
7321 const char * const *p;
7322
7323 for (p = auxiliary_filters; *p != NULL; p++)
7324 {
7325 size_t indx;
7326
7327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7328 *p, true);
7329 if (indx == (size_t) -1
7330 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7331 return false;
7332 }
7333 }
7334
7335 if (audit != NULL)
7336 {
7337 size_t indx;
7338
7339 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7340 true);
7341 if (indx == (size_t) -1
7342 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7343 return false;
7344 }
7345
7346 if (depaudit != NULL)
7347 {
7348 size_t indx;
7349
7350 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7351 true);
7352 if (indx == (size_t) -1
7353 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7354 return false;
7355 }
7356
7357 eif.info = info;
7358 eif.failed = false;
7359
7360 /* Find all symbols which were defined in a dynamic object and make
7361 the backend pick a reasonable value for them. */
7362 elf_link_hash_traverse (elf_hash_table (info),
7363 _bfd_elf_adjust_dynamic_symbol,
7364 &eif);
7365 if (eif.failed)
7366 return false;
7367
7368 /* Add some entries to the .dynamic section. We fill in some of the
7369 values later, in bfd_elf_final_link, but we must add the entries
7370 now so that we know the final size of the .dynamic section. */
7371
7372 /* If there are initialization and/or finalization functions to
7373 call then add the corresponding DT_INIT/DT_FINI entries. */
7374 h = (info->init_function
7375 ? elf_link_hash_lookup (elf_hash_table (info),
7376 info->init_function, false,
7377 false, false)
7378 : NULL);
7379 if (h != NULL
7380 && (h->ref_regular
7381 || h->def_regular))
7382 {
7383 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7384 return false;
7385 }
7386 h = (info->fini_function
7387 ? elf_link_hash_lookup (elf_hash_table (info),
7388 info->fini_function, false,
7389 false, false)
7390 : NULL);
7391 if (h != NULL
7392 && (h->ref_regular
7393 || h->def_regular))
7394 {
7395 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7396 return false;
7397 }
7398
7399 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7400 if (s != NULL && s->linker_has_input)
7401 {
7402 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7403 if (! bfd_link_executable (info))
7404 {
7405 bfd *sub;
7406 asection *o;
7407
7408 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7409 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7410 && (o = sub->sections) != NULL
7411 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7412 for (o = sub->sections; o != NULL; o = o->next)
7413 if (elf_section_data (o)->this_hdr.sh_type
7414 == SHT_PREINIT_ARRAY)
7415 {
7416 _bfd_error_handler
7417 (_("%pB: .preinit_array section is not allowed in DSO"),
7418 sub);
7419 break;
7420 }
7421
7422 bfd_set_error (bfd_error_nonrepresentable_section);
7423 return false;
7424 }
7425
7426 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7427 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7428 return false;
7429 }
7430 s = bfd_get_section_by_name (output_bfd, ".init_array");
7431 if (s != NULL && s->linker_has_input)
7432 {
7433 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7434 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7435 return false;
7436 }
7437 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7438 if (s != NULL && s->linker_has_input)
7439 {
7440 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7441 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7442 return false;
7443 }
7444
7445 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7446 /* If .dynstr is excluded from the link, we don't want any of
7447 these tags. Strictly, we should be checking each section
7448 individually; This quick check covers for the case where
7449 someone does a /DISCARD/ : { *(*) }. */
7450 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7451 {
7452 bfd_size_type strsize;
7453
7454 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7455 if ((info->emit_hash
7456 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7457 || (info->emit_gnu_hash
7458 && (bed->record_xhash_symbol == NULL
7459 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7460 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7461 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7462 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7463 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7464 bed->s->sizeof_sym)
7465 || (info->gnu_flags_1
7466 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7467 info->gnu_flags_1)))
7468 return false;
7469 }
7470 }
7471
7472 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7473 return false;
7474
7475 /* The backend must work out the sizes of all the other dynamic
7476 sections. */
7477 if (bed->elf_backend_late_size_sections != NULL
7478 && !bed->elf_backend_late_size_sections (output_bfd, info))
7479 return false;
7480
7481 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7482 {
7483 if (elf_tdata (output_bfd)->cverdefs)
7484 {
7485 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7486
7487 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7488 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7489 return false;
7490 }
7491
7492 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7493 {
7494 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7495 return false;
7496 }
7497 else if (info->flags & DF_BIND_NOW)
7498 {
7499 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7500 return false;
7501 }
7502
7503 if (info->flags_1)
7504 {
7505 if (bfd_link_executable (info))
7506 info->flags_1 &= ~ (DF_1_INITFIRST
7507 | DF_1_NODELETE
7508 | DF_1_NOOPEN);
7509 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7510 return false;
7511 }
7512
7513 if (elf_tdata (output_bfd)->cverrefs)
7514 {
7515 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7516
7517 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7518 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7519 return false;
7520 }
7521
7522 if ((elf_tdata (output_bfd)->cverrefs == 0
7523 && elf_tdata (output_bfd)->cverdefs == 0)
7524 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7525 {
7526 asection *s;
7527
7528 s = bfd_get_linker_section (dynobj, ".gnu.version");
7529 s->flags |= SEC_EXCLUDE;
7530 }
7531 }
7532 return true;
7533 }
7534
7535 /* Find the first non-excluded output section. We'll use its
7536 section symbol for some emitted relocs. */
7537 void
7538 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7539 {
7540 asection *s;
7541 asection *found = NULL;
7542
7543 for (s = output_bfd->sections; s != NULL; s = s->next)
7544 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7545 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7546 {
7547 found = s;
7548 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7549 break;
7550 }
7551 elf_hash_table (info)->text_index_section = found;
7552 }
7553
7554 /* Find two non-excluded output sections, one for code, one for data.
7555 We'll use their section symbols for some emitted relocs. */
7556 void
7557 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7558 {
7559 asection *s;
7560 asection *found = NULL;
7561
7562 /* Data first, since setting text_index_section changes
7563 _bfd_elf_omit_section_dynsym_default. */
7564 for (s = output_bfd->sections; s != NULL; s = s->next)
7565 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7566 && !(s->flags & SEC_READONLY)
7567 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7568 {
7569 found = s;
7570 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7571 break;
7572 }
7573 elf_hash_table (info)->data_index_section = found;
7574
7575 for (s = output_bfd->sections; s != NULL; s = s->next)
7576 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7577 && (s->flags & SEC_READONLY)
7578 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7579 {
7580 found = s;
7581 break;
7582 }
7583 elf_hash_table (info)->text_index_section = found;
7584 }
7585
7586 #define GNU_HASH_SECTION_NAME(bed) \
7587 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7588
7589 bool
7590 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7591 {
7592 const struct elf_backend_data *bed;
7593 unsigned long section_sym_count;
7594 bfd_size_type dynsymcount = 0;
7595
7596 if (!is_elf_hash_table (info->hash))
7597 return true;
7598
7599 bed = get_elf_backend_data (output_bfd);
7600 (*bed->elf_backend_init_index_section) (output_bfd, info);
7601
7602 /* Assign dynsym indices. In a shared library we generate a section
7603 symbol for each output section, which come first. Next come all
7604 of the back-end allocated local dynamic syms, followed by the rest
7605 of the global symbols.
7606
7607 This is usually not needed for static binaries, however backends
7608 can request to always do it, e.g. the MIPS backend uses dynamic
7609 symbol counts to lay out GOT, which will be produced in the
7610 presence of GOT relocations even in static binaries (holding fixed
7611 data in that case, to satisfy those relocations). */
7612
7613 if (elf_hash_table (info)->dynamic_sections_created
7614 || bed->always_renumber_dynsyms)
7615 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7616 &section_sym_count);
7617
7618 if (elf_hash_table (info)->dynamic_sections_created)
7619 {
7620 bfd *dynobj;
7621 asection *s;
7622 unsigned int dtagcount;
7623
7624 dynobj = elf_hash_table (info)->dynobj;
7625
7626 /* Work out the size of the symbol version section. */
7627 s = bfd_get_linker_section (dynobj, ".gnu.version");
7628 BFD_ASSERT (s != NULL);
7629 if ((s->flags & SEC_EXCLUDE) == 0)
7630 {
7631 s->size = dynsymcount * sizeof (Elf_External_Versym);
7632 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7633 if (s->contents == NULL)
7634 return false;
7635
7636 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7637 return false;
7638 }
7639
7640 /* Set the size of the .dynsym and .hash sections. We counted
7641 the number of dynamic symbols in elf_link_add_object_symbols.
7642 We will build the contents of .dynsym and .hash when we build
7643 the final symbol table, because until then we do not know the
7644 correct value to give the symbols. We built the .dynstr
7645 section as we went along in elf_link_add_object_symbols. */
7646 s = elf_hash_table (info)->dynsym;
7647 BFD_ASSERT (s != NULL);
7648 s->size = dynsymcount * bed->s->sizeof_sym;
7649
7650 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7651 if (s->contents == NULL)
7652 return false;
7653
7654 /* The first entry in .dynsym is a dummy symbol. Clear all the
7655 section syms, in case we don't output them all. */
7656 ++section_sym_count;
7657 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7658
7659 elf_hash_table (info)->bucketcount = 0;
7660
7661 /* Compute the size of the hashing table. As a side effect this
7662 computes the hash values for all the names we export. */
7663 if (info->emit_hash)
7664 {
7665 unsigned long int *hashcodes;
7666 struct hash_codes_info hashinf;
7667 bfd_size_type amt;
7668 unsigned long int nsyms;
7669 size_t bucketcount;
7670 size_t hash_entry_size;
7671
7672 /* Compute the hash values for all exported symbols. At the same
7673 time store the values in an array so that we could use them for
7674 optimizations. */
7675 amt = dynsymcount * sizeof (unsigned long int);
7676 hashcodes = (unsigned long int *) bfd_malloc (amt);
7677 if (hashcodes == NULL)
7678 return false;
7679 hashinf.hashcodes = hashcodes;
7680 hashinf.error = false;
7681
7682 /* Put all hash values in HASHCODES. */
7683 elf_link_hash_traverse (elf_hash_table (info),
7684 elf_collect_hash_codes, &hashinf);
7685 if (hashinf.error)
7686 {
7687 free (hashcodes);
7688 return false;
7689 }
7690
7691 nsyms = hashinf.hashcodes - hashcodes;
7692 bucketcount
7693 = compute_bucket_count (info, hashcodes, nsyms, 0);
7694 free (hashcodes);
7695
7696 if (bucketcount == 0 && nsyms > 0)
7697 return false;
7698
7699 elf_hash_table (info)->bucketcount = bucketcount;
7700
7701 s = bfd_get_linker_section (dynobj, ".hash");
7702 BFD_ASSERT (s != NULL);
7703 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7704 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7705 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7706 if (s->contents == NULL)
7707 return false;
7708
7709 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7710 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7711 s->contents + hash_entry_size);
7712 }
7713
7714 if (info->emit_gnu_hash)
7715 {
7716 size_t i, cnt;
7717 unsigned char *contents;
7718 struct collect_gnu_hash_codes cinfo;
7719 bfd_size_type amt;
7720 size_t bucketcount;
7721
7722 memset (&cinfo, 0, sizeof (cinfo));
7723
7724 /* Compute the hash values for all exported symbols. At the same
7725 time store the values in an array so that we could use them for
7726 optimizations. */
7727 amt = dynsymcount * 2 * sizeof (unsigned long int);
7728 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7729 if (cinfo.hashcodes == NULL)
7730 return false;
7731
7732 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7733 cinfo.min_dynindx = -1;
7734 cinfo.output_bfd = output_bfd;
7735 cinfo.bed = bed;
7736
7737 /* Put all hash values in HASHCODES. */
7738 elf_link_hash_traverse (elf_hash_table (info),
7739 elf_collect_gnu_hash_codes, &cinfo);
7740 if (cinfo.error)
7741 {
7742 free (cinfo.hashcodes);
7743 return false;
7744 }
7745
7746 bucketcount
7747 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7748
7749 if (bucketcount == 0)
7750 {
7751 free (cinfo.hashcodes);
7752 return false;
7753 }
7754
7755 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7756 BFD_ASSERT (s != NULL);
7757
7758 if (cinfo.nsyms == 0)
7759 {
7760 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7761 BFD_ASSERT (cinfo.min_dynindx == -1);
7762 free (cinfo.hashcodes);
7763 s->size = 5 * 4 + bed->s->arch_size / 8;
7764 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7765 if (contents == NULL)
7766 return false;
7767 s->contents = contents;
7768 /* 1 empty bucket. */
7769 bfd_put_32 (output_bfd, 1, contents);
7770 /* SYMIDX above the special symbol 0. */
7771 bfd_put_32 (output_bfd, 1, contents + 4);
7772 /* Just one word for bitmask. */
7773 bfd_put_32 (output_bfd, 1, contents + 8);
7774 /* Only hash fn bloom filter. */
7775 bfd_put_32 (output_bfd, 0, contents + 12);
7776 /* No hashes are valid - empty bitmask. */
7777 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7778 /* No hashes in the only bucket. */
7779 bfd_put_32 (output_bfd, 0,
7780 contents + 16 + bed->s->arch_size / 8);
7781 }
7782 else
7783 {
7784 unsigned long int maskwords, maskbitslog2, x;
7785 BFD_ASSERT (cinfo.min_dynindx != -1);
7786
7787 x = cinfo.nsyms;
7788 maskbitslog2 = 1;
7789 while ((x >>= 1) != 0)
7790 ++maskbitslog2;
7791 if (maskbitslog2 < 3)
7792 maskbitslog2 = 5;
7793 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7794 maskbitslog2 = maskbitslog2 + 3;
7795 else
7796 maskbitslog2 = maskbitslog2 + 2;
7797 if (bed->s->arch_size == 64)
7798 {
7799 if (maskbitslog2 == 5)
7800 maskbitslog2 = 6;
7801 cinfo.shift1 = 6;
7802 }
7803 else
7804 cinfo.shift1 = 5;
7805 cinfo.mask = (1 << cinfo.shift1) - 1;
7806 cinfo.shift2 = maskbitslog2;
7807 cinfo.maskbits = 1 << maskbitslog2;
7808 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7809 amt = bucketcount * sizeof (unsigned long int) * 2;
7810 amt += maskwords * sizeof (bfd_vma);
7811 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7812 if (cinfo.bitmask == NULL)
7813 {
7814 free (cinfo.hashcodes);
7815 return false;
7816 }
7817
7818 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7819 cinfo.indx = cinfo.counts + bucketcount;
7820 cinfo.symindx = dynsymcount - cinfo.nsyms;
7821 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7822
7823 /* Determine how often each hash bucket is used. */
7824 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7825 for (i = 0; i < cinfo.nsyms; ++i)
7826 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7827
7828 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7829 if (cinfo.counts[i] != 0)
7830 {
7831 cinfo.indx[i] = cnt;
7832 cnt += cinfo.counts[i];
7833 }
7834 BFD_ASSERT (cnt == dynsymcount);
7835 cinfo.bucketcount = bucketcount;
7836 cinfo.local_indx = cinfo.min_dynindx;
7837
7838 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7839 s->size += cinfo.maskbits / 8;
7840 if (bed->record_xhash_symbol != NULL)
7841 s->size += cinfo.nsyms * 4;
7842 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7843 if (contents == NULL)
7844 {
7845 free (cinfo.bitmask);
7846 free (cinfo.hashcodes);
7847 return false;
7848 }
7849
7850 s->contents = contents;
7851 bfd_put_32 (output_bfd, bucketcount, contents);
7852 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7853 bfd_put_32 (output_bfd, maskwords, contents + 8);
7854 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7855 contents += 16 + cinfo.maskbits / 8;
7856
7857 for (i = 0; i < bucketcount; ++i)
7858 {
7859 if (cinfo.counts[i] == 0)
7860 bfd_put_32 (output_bfd, 0, contents);
7861 else
7862 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7863 contents += 4;
7864 }
7865
7866 cinfo.contents = contents;
7867
7868 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7869 /* Renumber dynamic symbols, if populating .gnu.hash section.
7870 If using .MIPS.xhash, populate the translation table. */
7871 elf_link_hash_traverse (elf_hash_table (info),
7872 elf_gnu_hash_process_symidx, &cinfo);
7873
7874 contents = s->contents + 16;
7875 for (i = 0; i < maskwords; ++i)
7876 {
7877 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7878 contents);
7879 contents += bed->s->arch_size / 8;
7880 }
7881
7882 free (cinfo.bitmask);
7883 free (cinfo.hashcodes);
7884 }
7885 }
7886
7887 s = bfd_get_linker_section (dynobj, ".dynstr");
7888 BFD_ASSERT (s != NULL);
7889
7890 elf_finalize_dynstr (output_bfd, info);
7891
7892 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7893
7894 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7895 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7896 return false;
7897 }
7898
7899 return true;
7900 }
7901 \f
7902 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7903
7904 static void
7905 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7906 asection *sec)
7907 {
7908 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7909 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7910 }
7911
7912 /* Finish SHF_MERGE section merging. */
7913
7914 bool
7915 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7916 {
7917 bfd *ibfd;
7918 asection *sec;
7919
7920 if (!is_elf_hash_table (info->hash))
7921 return false;
7922
7923 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7924 if ((ibfd->flags & DYNAMIC) == 0
7925 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7926 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7927 == get_elf_backend_data (obfd)->s->elfclass))
7928 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7929 if ((sec->flags & SEC_MERGE) != 0
7930 && !bfd_is_abs_section (sec->output_section))
7931 {
7932 struct bfd_elf_section_data *secdata;
7933
7934 secdata = elf_section_data (sec);
7935 if (! _bfd_add_merge_section (obfd,
7936 &elf_hash_table (info)->merge_info,
7937 sec, &secdata->sec_info))
7938 return false;
7939 else if (secdata->sec_info)
7940 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7941 }
7942
7943 if (elf_hash_table (info)->merge_info != NULL)
7944 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7945 merge_sections_remove_hook);
7946 return true;
7947 }
7948
7949 /* Create an entry in an ELF linker hash table. */
7950
7951 struct bfd_hash_entry *
7952 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7953 struct bfd_hash_table *table,
7954 const char *string)
7955 {
7956 /* Allocate the structure if it has not already been allocated by a
7957 subclass. */
7958 if (entry == NULL)
7959 {
7960 entry = (struct bfd_hash_entry *)
7961 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7962 if (entry == NULL)
7963 return entry;
7964 }
7965
7966 /* Call the allocation method of the superclass. */
7967 entry = _bfd_link_hash_newfunc (entry, table, string);
7968 if (entry != NULL)
7969 {
7970 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7971 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7972
7973 /* Set local fields. */
7974 ret->indx = -1;
7975 ret->dynindx = -1;
7976 ret->got = htab->init_got_refcount;
7977 ret->plt = htab->init_plt_refcount;
7978 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7979 - offsetof (struct elf_link_hash_entry, size)));
7980 /* Assume that we have been called by a non-ELF symbol reader.
7981 This flag is then reset by the code which reads an ELF input
7982 file. This ensures that a symbol created by a non-ELF symbol
7983 reader will have the flag set correctly. */
7984 ret->non_elf = 1;
7985 }
7986
7987 return entry;
7988 }
7989
7990 /* Copy data from an indirect symbol to its direct symbol, hiding the
7991 old indirect symbol. Also used for copying flags to a weakdef. */
7992
7993 void
7994 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7995 struct elf_link_hash_entry *dir,
7996 struct elf_link_hash_entry *ind)
7997 {
7998 struct elf_link_hash_table *htab;
7999
8000 if (ind->dyn_relocs != NULL)
8001 {
8002 if (dir->dyn_relocs != NULL)
8003 {
8004 struct elf_dyn_relocs **pp;
8005 struct elf_dyn_relocs *p;
8006
8007 /* Add reloc counts against the indirect sym to the direct sym
8008 list. Merge any entries against the same section. */
8009 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8010 {
8011 struct elf_dyn_relocs *q;
8012
8013 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8014 if (q->sec == p->sec)
8015 {
8016 q->pc_count += p->pc_count;
8017 q->count += p->count;
8018 *pp = p->next;
8019 break;
8020 }
8021 if (q == NULL)
8022 pp = &p->next;
8023 }
8024 *pp = dir->dyn_relocs;
8025 }
8026
8027 dir->dyn_relocs = ind->dyn_relocs;
8028 ind->dyn_relocs = NULL;
8029 }
8030
8031 /* Copy down any references that we may have already seen to the
8032 symbol which just became indirect. */
8033
8034 if (dir->versioned != versioned_hidden)
8035 dir->ref_dynamic |= ind->ref_dynamic;
8036 dir->ref_regular |= ind->ref_regular;
8037 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8038 dir->non_got_ref |= ind->non_got_ref;
8039 dir->needs_plt |= ind->needs_plt;
8040 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8041
8042 if (ind->root.type != bfd_link_hash_indirect)
8043 return;
8044
8045 /* Copy over the global and procedure linkage table refcount entries.
8046 These may have been already set up by a check_relocs routine. */
8047 htab = elf_hash_table (info);
8048 if (ind->got.refcount > htab->init_got_refcount.refcount)
8049 {
8050 if (dir->got.refcount < 0)
8051 dir->got.refcount = 0;
8052 dir->got.refcount += ind->got.refcount;
8053 ind->got.refcount = htab->init_got_refcount.refcount;
8054 }
8055
8056 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8057 {
8058 if (dir->plt.refcount < 0)
8059 dir->plt.refcount = 0;
8060 dir->plt.refcount += ind->plt.refcount;
8061 ind->plt.refcount = htab->init_plt_refcount.refcount;
8062 }
8063
8064 if (ind->dynindx != -1)
8065 {
8066 if (dir->dynindx != -1)
8067 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8068 dir->dynindx = ind->dynindx;
8069 dir->dynstr_index = ind->dynstr_index;
8070 ind->dynindx = -1;
8071 ind->dynstr_index = 0;
8072 }
8073 }
8074
8075 void
8076 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8077 struct elf_link_hash_entry *h,
8078 bool force_local)
8079 {
8080 /* STT_GNU_IFUNC symbol must go through PLT. */
8081 if (h->type != STT_GNU_IFUNC)
8082 {
8083 h->plt = elf_hash_table (info)->init_plt_offset;
8084 h->needs_plt = 0;
8085 }
8086 if (force_local)
8087 {
8088 h->forced_local = 1;
8089 if (h->dynindx != -1)
8090 {
8091 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8092 h->dynstr_index);
8093 h->dynindx = -1;
8094 h->dynstr_index = 0;
8095 }
8096 }
8097 }
8098
8099 /* Hide a symbol. */
8100
8101 void
8102 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8103 struct bfd_link_info *info,
8104 struct bfd_link_hash_entry *h)
8105 {
8106 if (is_elf_hash_table (info->hash))
8107 {
8108 const struct elf_backend_data *bed
8109 = get_elf_backend_data (output_bfd);
8110 struct elf_link_hash_entry *eh
8111 = (struct elf_link_hash_entry *) h;
8112 bed->elf_backend_hide_symbol (info, eh, true);
8113 eh->def_dynamic = 0;
8114 eh->ref_dynamic = 0;
8115 eh->dynamic_def = 0;
8116 }
8117 }
8118
8119 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8120 caller. */
8121
8122 bool
8123 _bfd_elf_link_hash_table_init
8124 (struct elf_link_hash_table *table,
8125 bfd *abfd,
8126 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8127 struct bfd_hash_table *,
8128 const char *),
8129 unsigned int entsize,
8130 enum elf_target_id target_id)
8131 {
8132 bool ret;
8133 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8134
8135 table->init_got_refcount.refcount = can_refcount - 1;
8136 table->init_plt_refcount.refcount = can_refcount - 1;
8137 table->init_got_offset.offset = -(bfd_vma) 1;
8138 table->init_plt_offset.offset = -(bfd_vma) 1;
8139 /* The first dynamic symbol is a dummy. */
8140 table->dynsymcount = 1;
8141
8142 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8143
8144 table->root.type = bfd_link_elf_hash_table;
8145 table->hash_table_id = target_id;
8146 table->target_os = get_elf_backend_data (abfd)->target_os;
8147
8148 return ret;
8149 }
8150
8151 /* Create an ELF linker hash table. */
8152
8153 struct bfd_link_hash_table *
8154 _bfd_elf_link_hash_table_create (bfd *abfd)
8155 {
8156 struct elf_link_hash_table *ret;
8157 size_t amt = sizeof (struct elf_link_hash_table);
8158
8159 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8160 if (ret == NULL)
8161 return NULL;
8162
8163 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8164 sizeof (struct elf_link_hash_entry),
8165 GENERIC_ELF_DATA))
8166 {
8167 free (ret);
8168 return NULL;
8169 }
8170 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8171
8172 return &ret->root;
8173 }
8174
8175 /* Destroy an ELF linker hash table. */
8176
8177 void
8178 _bfd_elf_link_hash_table_free (bfd *obfd)
8179 {
8180 struct elf_link_hash_table *htab;
8181
8182 htab = (struct elf_link_hash_table *) obfd->link.hash;
8183 if (htab->dynstr != NULL)
8184 _bfd_elf_strtab_free (htab->dynstr);
8185 _bfd_merge_sections_free (htab->merge_info);
8186 _bfd_generic_link_hash_table_free (obfd);
8187 }
8188
8189 /* This is a hook for the ELF emulation code in the generic linker to
8190 tell the backend linker what file name to use for the DT_NEEDED
8191 entry for a dynamic object. */
8192
8193 void
8194 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8195 {
8196 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8197 && bfd_get_format (abfd) == bfd_object)
8198 elf_dt_name (abfd) = name;
8199 }
8200
8201 int
8202 bfd_elf_get_dyn_lib_class (bfd *abfd)
8203 {
8204 int lib_class;
8205 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8206 && bfd_get_format (abfd) == bfd_object)
8207 lib_class = elf_dyn_lib_class (abfd);
8208 else
8209 lib_class = 0;
8210 return lib_class;
8211 }
8212
8213 void
8214 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8215 {
8216 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8217 && bfd_get_format (abfd) == bfd_object)
8218 elf_dyn_lib_class (abfd) = lib_class;
8219 }
8220
8221 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8222 the linker ELF emulation code. */
8223
8224 struct bfd_link_needed_list *
8225 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8226 struct bfd_link_info *info)
8227 {
8228 if (! is_elf_hash_table (info->hash))
8229 return NULL;
8230 return elf_hash_table (info)->needed;
8231 }
8232
8233 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8234 hook for the linker ELF emulation code. */
8235
8236 struct bfd_link_needed_list *
8237 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8238 struct bfd_link_info *info)
8239 {
8240 if (! is_elf_hash_table (info->hash))
8241 return NULL;
8242 return elf_hash_table (info)->runpath;
8243 }
8244
8245 /* Get the name actually used for a dynamic object for a link. This
8246 is the SONAME entry if there is one. Otherwise, it is the string
8247 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8248
8249 const char *
8250 bfd_elf_get_dt_soname (bfd *abfd)
8251 {
8252 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8253 && bfd_get_format (abfd) == bfd_object)
8254 return elf_dt_name (abfd);
8255 return NULL;
8256 }
8257
8258 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8259 the ELF linker emulation code. */
8260
8261 bool
8262 bfd_elf_get_bfd_needed_list (bfd *abfd,
8263 struct bfd_link_needed_list **pneeded)
8264 {
8265 asection *s;
8266 bfd_byte *dynbuf = NULL;
8267 unsigned int elfsec;
8268 unsigned long shlink;
8269 bfd_byte *extdyn, *extdynend;
8270 size_t extdynsize;
8271 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8272
8273 *pneeded = NULL;
8274
8275 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8276 || bfd_get_format (abfd) != bfd_object)
8277 return true;
8278
8279 s = bfd_get_section_by_name (abfd, ".dynamic");
8280 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8281 return true;
8282
8283 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8284 goto error_return;
8285
8286 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8287 if (elfsec == SHN_BAD)
8288 goto error_return;
8289
8290 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8291
8292 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8293 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8294
8295 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8296 (size_t) (extdynend - extdyn) >= extdynsize;
8297 extdyn += extdynsize)
8298 {
8299 Elf_Internal_Dyn dyn;
8300
8301 (*swap_dyn_in) (abfd, extdyn, &dyn);
8302
8303 if (dyn.d_tag == DT_NULL)
8304 break;
8305
8306 if (dyn.d_tag == DT_NEEDED)
8307 {
8308 const char *string;
8309 struct bfd_link_needed_list *l;
8310 unsigned int tagv = dyn.d_un.d_val;
8311 size_t amt;
8312
8313 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8314 if (string == NULL)
8315 goto error_return;
8316
8317 amt = sizeof *l;
8318 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8319 if (l == NULL)
8320 goto error_return;
8321
8322 l->by = abfd;
8323 l->name = string;
8324 l->next = *pneeded;
8325 *pneeded = l;
8326 }
8327 }
8328
8329 free (dynbuf);
8330
8331 return true;
8332
8333 error_return:
8334 free (dynbuf);
8335 return false;
8336 }
8337
8338 struct elf_symbuf_symbol
8339 {
8340 unsigned long st_name; /* Symbol name, index in string tbl */
8341 unsigned char st_info; /* Type and binding attributes */
8342 unsigned char st_other; /* Visibilty, and target specific */
8343 };
8344
8345 struct elf_symbuf_head
8346 {
8347 struct elf_symbuf_symbol *ssym;
8348 size_t count;
8349 unsigned int st_shndx;
8350 };
8351
8352 struct elf_symbol
8353 {
8354 union
8355 {
8356 Elf_Internal_Sym *isym;
8357 struct elf_symbuf_symbol *ssym;
8358 void *p;
8359 } u;
8360 const char *name;
8361 };
8362
8363 /* Sort references to symbols by ascending section number. */
8364
8365 static int
8366 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8367 {
8368 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8369 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8370
8371 if (s1->st_shndx != s2->st_shndx)
8372 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8373 /* Final sort by the address of the sym in the symbuf ensures
8374 a stable sort. */
8375 if (s1 != s2)
8376 return s1 > s2 ? 1 : -1;
8377 return 0;
8378 }
8379
8380 static int
8381 elf_sym_name_compare (const void *arg1, const void *arg2)
8382 {
8383 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8384 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8385 int ret = strcmp (s1->name, s2->name);
8386 if (ret != 0)
8387 return ret;
8388 if (s1->u.p != s2->u.p)
8389 return s1->u.p > s2->u.p ? 1 : -1;
8390 return 0;
8391 }
8392
8393 static struct elf_symbuf_head *
8394 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8395 {
8396 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8397 struct elf_symbuf_symbol *ssym;
8398 struct elf_symbuf_head *ssymbuf, *ssymhead;
8399 size_t i, shndx_count, total_size, amt;
8400
8401 amt = symcount * sizeof (*indbuf);
8402 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8403 if (indbuf == NULL)
8404 return NULL;
8405
8406 for (ind = indbuf, i = 0; i < symcount; i++)
8407 if (isymbuf[i].st_shndx != SHN_UNDEF)
8408 *ind++ = &isymbuf[i];
8409 indbufend = ind;
8410
8411 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8412 elf_sort_elf_symbol);
8413
8414 shndx_count = 0;
8415 if (indbufend > indbuf)
8416 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8417 if (ind[0]->st_shndx != ind[1]->st_shndx)
8418 shndx_count++;
8419
8420 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8421 + (indbufend - indbuf) * sizeof (*ssym));
8422 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8423 if (ssymbuf == NULL)
8424 {
8425 free (indbuf);
8426 return NULL;
8427 }
8428
8429 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8430 ssymbuf->ssym = NULL;
8431 ssymbuf->count = shndx_count;
8432 ssymbuf->st_shndx = 0;
8433 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8434 {
8435 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8436 {
8437 ssymhead++;
8438 ssymhead->ssym = ssym;
8439 ssymhead->count = 0;
8440 ssymhead->st_shndx = (*ind)->st_shndx;
8441 }
8442 ssym->st_name = (*ind)->st_name;
8443 ssym->st_info = (*ind)->st_info;
8444 ssym->st_other = (*ind)->st_other;
8445 ssymhead->count++;
8446 }
8447 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8448 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8449
8450 free (indbuf);
8451 return ssymbuf;
8452 }
8453
8454 /* Check if 2 sections define the same set of local and global
8455 symbols. */
8456
8457 static bool
8458 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8459 struct bfd_link_info *info)
8460 {
8461 bfd *bfd1, *bfd2;
8462 const struct elf_backend_data *bed1, *bed2;
8463 Elf_Internal_Shdr *hdr1, *hdr2;
8464 size_t symcount1, symcount2;
8465 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8466 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8467 Elf_Internal_Sym *isym, *isymend;
8468 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8469 size_t count1, count2, sec_count1, sec_count2, i;
8470 unsigned int shndx1, shndx2;
8471 bool result;
8472 bool ignore_section_symbol_p;
8473
8474 bfd1 = sec1->owner;
8475 bfd2 = sec2->owner;
8476
8477 /* Both sections have to be in ELF. */
8478 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8479 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8480 return false;
8481
8482 if (elf_section_type (sec1) != elf_section_type (sec2))
8483 return false;
8484
8485 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8486 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8487 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8488 return false;
8489
8490 bed1 = get_elf_backend_data (bfd1);
8491 bed2 = get_elf_backend_data (bfd2);
8492 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8493 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8494 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8495 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8496
8497 if (symcount1 == 0 || symcount2 == 0)
8498 return false;
8499
8500 result = false;
8501 isymbuf1 = NULL;
8502 isymbuf2 = NULL;
8503 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8504 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8505
8506 /* Ignore section symbols only when matching non-debugging sections
8507 or linkonce section with comdat section. */
8508 ignore_section_symbol_p
8509 = ((sec1->flags & SEC_DEBUGGING) == 0
8510 || ((elf_section_flags (sec1) & SHF_GROUP)
8511 != (elf_section_flags (sec2) & SHF_GROUP)));
8512
8513 if (ssymbuf1 == NULL)
8514 {
8515 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8516 NULL, NULL, NULL);
8517 if (isymbuf1 == NULL)
8518 goto done;
8519
8520 if (info != NULL && !info->reduce_memory_overheads)
8521 {
8522 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8523 elf_tdata (bfd1)->symbuf = ssymbuf1;
8524 }
8525 }
8526
8527 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8528 {
8529 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8530 NULL, NULL, NULL);
8531 if (isymbuf2 == NULL)
8532 goto done;
8533
8534 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8535 {
8536 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8537 elf_tdata (bfd2)->symbuf = ssymbuf2;
8538 }
8539 }
8540
8541 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8542 {
8543 /* Optimized faster version. */
8544 size_t lo, hi, mid;
8545 struct elf_symbol *symp;
8546 struct elf_symbuf_symbol *ssym, *ssymend;
8547
8548 lo = 0;
8549 hi = ssymbuf1->count;
8550 ssymbuf1++;
8551 count1 = 0;
8552 sec_count1 = 0;
8553 while (lo < hi)
8554 {
8555 mid = (lo + hi) / 2;
8556 if (shndx1 < ssymbuf1[mid].st_shndx)
8557 hi = mid;
8558 else if (shndx1 > ssymbuf1[mid].st_shndx)
8559 lo = mid + 1;
8560 else
8561 {
8562 count1 = ssymbuf1[mid].count;
8563 ssymbuf1 += mid;
8564 break;
8565 }
8566 }
8567 if (ignore_section_symbol_p)
8568 {
8569 for (i = 0; i < count1; i++)
8570 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8571 sec_count1++;
8572 count1 -= sec_count1;
8573 }
8574
8575 lo = 0;
8576 hi = ssymbuf2->count;
8577 ssymbuf2++;
8578 count2 = 0;
8579 sec_count2 = 0;
8580 while (lo < hi)
8581 {
8582 mid = (lo + hi) / 2;
8583 if (shndx2 < ssymbuf2[mid].st_shndx)
8584 hi = mid;
8585 else if (shndx2 > ssymbuf2[mid].st_shndx)
8586 lo = mid + 1;
8587 else
8588 {
8589 count2 = ssymbuf2[mid].count;
8590 ssymbuf2 += mid;
8591 break;
8592 }
8593 }
8594 if (ignore_section_symbol_p)
8595 {
8596 for (i = 0; i < count2; i++)
8597 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8598 sec_count2++;
8599 count2 -= sec_count2;
8600 }
8601
8602 if (count1 == 0 || count2 == 0 || count1 != count2)
8603 goto done;
8604
8605 symtable1
8606 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8607 symtable2
8608 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8609 if (symtable1 == NULL || symtable2 == NULL)
8610 goto done;
8611
8612 symp = symtable1;
8613 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8614 ssym < ssymend; ssym++)
8615 if (sec_count1 == 0
8616 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8617 {
8618 symp->u.ssym = ssym;
8619 symp->name = bfd_elf_string_from_elf_section (bfd1,
8620 hdr1->sh_link,
8621 ssym->st_name);
8622 symp++;
8623 }
8624
8625 symp = symtable2;
8626 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8627 ssym < ssymend; ssym++)
8628 if (sec_count2 == 0
8629 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8630 {
8631 symp->u.ssym = ssym;
8632 symp->name = bfd_elf_string_from_elf_section (bfd2,
8633 hdr2->sh_link,
8634 ssym->st_name);
8635 symp++;
8636 }
8637
8638 /* Sort symbol by name. */
8639 qsort (symtable1, count1, sizeof (struct elf_symbol),
8640 elf_sym_name_compare);
8641 qsort (symtable2, count1, sizeof (struct elf_symbol),
8642 elf_sym_name_compare);
8643
8644 for (i = 0; i < count1; i++)
8645 /* Two symbols must have the same binding, type and name. */
8646 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8647 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8648 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8649 goto done;
8650
8651 result = true;
8652 goto done;
8653 }
8654
8655 symtable1 = (struct elf_symbol *)
8656 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8657 symtable2 = (struct elf_symbol *)
8658 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8659 if (symtable1 == NULL || symtable2 == NULL)
8660 goto done;
8661
8662 /* Count definitions in the section. */
8663 count1 = 0;
8664 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8665 if (isym->st_shndx == shndx1
8666 && (!ignore_section_symbol_p
8667 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8668 symtable1[count1++].u.isym = isym;
8669
8670 count2 = 0;
8671 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8672 if (isym->st_shndx == shndx2
8673 && (!ignore_section_symbol_p
8674 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8675 symtable2[count2++].u.isym = isym;
8676
8677 if (count1 == 0 || count2 == 0 || count1 != count2)
8678 goto done;
8679
8680 for (i = 0; i < count1; i++)
8681 symtable1[i].name
8682 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8683 symtable1[i].u.isym->st_name);
8684
8685 for (i = 0; i < count2; i++)
8686 symtable2[i].name
8687 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8688 symtable2[i].u.isym->st_name);
8689
8690 /* Sort symbol by name. */
8691 qsort (symtable1, count1, sizeof (struct elf_symbol),
8692 elf_sym_name_compare);
8693 qsort (symtable2, count1, sizeof (struct elf_symbol),
8694 elf_sym_name_compare);
8695
8696 for (i = 0; i < count1; i++)
8697 /* Two symbols must have the same binding, type and name. */
8698 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8699 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8700 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8701 goto done;
8702
8703 result = true;
8704
8705 done:
8706 free (symtable1);
8707 free (symtable2);
8708 free (isymbuf1);
8709 free (isymbuf2);
8710
8711 return result;
8712 }
8713
8714 /* Return TRUE if 2 section types are compatible. */
8715
8716 bool
8717 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8718 bfd *bbfd, const asection *bsec)
8719 {
8720 if (asec == NULL
8721 || bsec == NULL
8722 || abfd->xvec->flavour != bfd_target_elf_flavour
8723 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8724 return true;
8725
8726 return elf_section_type (asec) == elf_section_type (bsec);
8727 }
8728 \f
8729 /* Final phase of ELF linker. */
8730
8731 /* A structure we use to avoid passing large numbers of arguments. */
8732
8733 struct elf_final_link_info
8734 {
8735 /* General link information. */
8736 struct bfd_link_info *info;
8737 /* Output BFD. */
8738 bfd *output_bfd;
8739 /* Symbol string table. */
8740 struct elf_strtab_hash *symstrtab;
8741 /* .hash section. */
8742 asection *hash_sec;
8743 /* symbol version section (.gnu.version). */
8744 asection *symver_sec;
8745 /* Buffer large enough to hold contents of any section. */
8746 bfd_byte *contents;
8747 /* Buffer large enough to hold external relocs of any section. */
8748 void *external_relocs;
8749 /* Buffer large enough to hold internal relocs of any section. */
8750 Elf_Internal_Rela *internal_relocs;
8751 /* Buffer large enough to hold external local symbols of any input
8752 BFD. */
8753 bfd_byte *external_syms;
8754 /* And a buffer for symbol section indices. */
8755 Elf_External_Sym_Shndx *locsym_shndx;
8756 /* Buffer large enough to hold internal local symbols of any input
8757 BFD. */
8758 Elf_Internal_Sym *internal_syms;
8759 /* Array large enough to hold a symbol index for each local symbol
8760 of any input BFD. */
8761 long *indices;
8762 /* Array large enough to hold a section pointer for each local
8763 symbol of any input BFD. */
8764 asection **sections;
8765 /* Buffer for SHT_SYMTAB_SHNDX section. */
8766 Elf_External_Sym_Shndx *symshndxbuf;
8767 /* Number of STT_FILE syms seen. */
8768 size_t filesym_count;
8769 /* Local symbol hash table. */
8770 struct bfd_hash_table local_hash_table;
8771 };
8772
8773 struct local_hash_entry
8774 {
8775 /* Base hash table entry structure. */
8776 struct bfd_hash_entry root;
8777 /* Size of the local symbol name. */
8778 size_t size;
8779 /* Number of the duplicated local symbol names. */
8780 long count;
8781 };
8782
8783 /* Create an entry in the local symbol hash table. */
8784
8785 static struct bfd_hash_entry *
8786 local_hash_newfunc (struct bfd_hash_entry *entry,
8787 struct bfd_hash_table *table,
8788 const char *string)
8789 {
8790
8791 /* Allocate the structure if it has not already been allocated by a
8792 subclass. */
8793 if (entry == NULL)
8794 {
8795 entry = bfd_hash_allocate (table,
8796 sizeof (struct local_hash_entry));
8797 if (entry == NULL)
8798 return entry;
8799 }
8800
8801 /* Call the allocation method of the superclass. */
8802 entry = bfd_hash_newfunc (entry, table, string);
8803 if (entry != NULL)
8804 {
8805 ((struct local_hash_entry *) entry)->count = 0;
8806 ((struct local_hash_entry *) entry)->size = 0;
8807 }
8808
8809 return entry;
8810 }
8811
8812 /* This struct is used to pass information to elf_link_output_extsym. */
8813
8814 struct elf_outext_info
8815 {
8816 bool failed;
8817 bool localsyms;
8818 bool file_sym_done;
8819 struct elf_final_link_info *flinfo;
8820 };
8821
8822
8823 /* Support for evaluating a complex relocation.
8824
8825 Complex relocations are generalized, self-describing relocations. The
8826 implementation of them consists of two parts: complex symbols, and the
8827 relocations themselves.
8828
8829 The relocations use a reserved elf-wide relocation type code (R_RELC
8830 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8831 information (start bit, end bit, word width, etc) into the addend. This
8832 information is extracted from CGEN-generated operand tables within gas.
8833
8834 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8835 internal) representing prefix-notation expressions, including but not
8836 limited to those sorts of expressions normally encoded as addends in the
8837 addend field. The symbol mangling format is:
8838
8839 <node> := <literal>
8840 | <unary-operator> ':' <node>
8841 | <binary-operator> ':' <node> ':' <node>
8842 ;
8843
8844 <literal> := 's' <digits=N> ':' <N character symbol name>
8845 | 'S' <digits=N> ':' <N character section name>
8846 | '#' <hexdigits>
8847 ;
8848
8849 <binary-operator> := as in C
8850 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8851
8852 static void
8853 set_symbol_value (bfd *bfd_with_globals,
8854 Elf_Internal_Sym *isymbuf,
8855 size_t locsymcount,
8856 size_t symidx,
8857 bfd_vma val)
8858 {
8859 struct elf_link_hash_entry **sym_hashes;
8860 struct elf_link_hash_entry *h;
8861 size_t extsymoff = locsymcount;
8862
8863 if (symidx < locsymcount)
8864 {
8865 Elf_Internal_Sym *sym;
8866
8867 sym = isymbuf + symidx;
8868 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8869 {
8870 /* It is a local symbol: move it to the
8871 "absolute" section and give it a value. */
8872 sym->st_shndx = SHN_ABS;
8873 sym->st_value = val;
8874 return;
8875 }
8876 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8877 extsymoff = 0;
8878 }
8879
8880 /* It is a global symbol: set its link type
8881 to "defined" and give it a value. */
8882
8883 sym_hashes = elf_sym_hashes (bfd_with_globals);
8884 h = sym_hashes [symidx - extsymoff];
8885 while (h->root.type == bfd_link_hash_indirect
8886 || h->root.type == bfd_link_hash_warning)
8887 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8888 h->root.type = bfd_link_hash_defined;
8889 h->root.u.def.value = val;
8890 h->root.u.def.section = bfd_abs_section_ptr;
8891 }
8892
8893 static bool
8894 resolve_symbol (const char *name,
8895 bfd *input_bfd,
8896 struct elf_final_link_info *flinfo,
8897 bfd_vma *result,
8898 Elf_Internal_Sym *isymbuf,
8899 size_t locsymcount)
8900 {
8901 Elf_Internal_Sym *sym;
8902 struct bfd_link_hash_entry *global_entry;
8903 const char *candidate = NULL;
8904 Elf_Internal_Shdr *symtab_hdr;
8905 size_t i;
8906
8907 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8908
8909 for (i = 0; i < locsymcount; ++ i)
8910 {
8911 sym = isymbuf + i;
8912
8913 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8914 continue;
8915
8916 candidate = bfd_elf_string_from_elf_section (input_bfd,
8917 symtab_hdr->sh_link,
8918 sym->st_name);
8919 #ifdef DEBUG
8920 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8921 name, candidate, (unsigned long) sym->st_value);
8922 #endif
8923 if (candidate && strcmp (candidate, name) == 0)
8924 {
8925 asection *sec = flinfo->sections [i];
8926
8927 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8928 *result += sec->output_offset + sec->output_section->vma;
8929 #ifdef DEBUG
8930 printf ("Found symbol with value %8.8lx\n",
8931 (unsigned long) *result);
8932 #endif
8933 return true;
8934 }
8935 }
8936
8937 /* Hmm, haven't found it yet. perhaps it is a global. */
8938 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8939 false, false, true);
8940 if (!global_entry)
8941 return false;
8942
8943 if (global_entry->type == bfd_link_hash_defined
8944 || global_entry->type == bfd_link_hash_defweak)
8945 {
8946 *result = (global_entry->u.def.value
8947 + global_entry->u.def.section->output_section->vma
8948 + global_entry->u.def.section->output_offset);
8949 #ifdef DEBUG
8950 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8951 global_entry->root.string, (unsigned long) *result);
8952 #endif
8953 return true;
8954 }
8955
8956 return false;
8957 }
8958
8959 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8960 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8961 names like "foo.end" which is the end address of section "foo". */
8962
8963 static bool
8964 resolve_section (const char *name,
8965 asection *sections,
8966 bfd_vma *result,
8967 bfd * abfd)
8968 {
8969 asection *curr;
8970 unsigned int len;
8971
8972 for (curr = sections; curr; curr = curr->next)
8973 if (strcmp (curr->name, name) == 0)
8974 {
8975 *result = curr->vma;
8976 return true;
8977 }
8978
8979 /* Hmm. still haven't found it. try pseudo-section names. */
8980 /* FIXME: This could be coded more efficiently... */
8981 for (curr = sections; curr; curr = curr->next)
8982 {
8983 len = strlen (curr->name);
8984 if (len > strlen (name))
8985 continue;
8986
8987 if (strncmp (curr->name, name, len) == 0)
8988 {
8989 if (startswith (name + len, ".end"))
8990 {
8991 *result = (curr->vma
8992 + curr->size / bfd_octets_per_byte (abfd, curr));
8993 return true;
8994 }
8995
8996 /* Insert more pseudo-section names here, if you like. */
8997 }
8998 }
8999
9000 return false;
9001 }
9002
9003 static void
9004 undefined_reference (const char *reftype, const char *name)
9005 {
9006 /* xgettext:c-format */
9007 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9008 reftype, name);
9009 bfd_set_error (bfd_error_bad_value);
9010 }
9011
9012 static bool
9013 eval_symbol (bfd_vma *result,
9014 const char **symp,
9015 bfd *input_bfd,
9016 struct elf_final_link_info *flinfo,
9017 bfd_vma dot,
9018 Elf_Internal_Sym *isymbuf,
9019 size_t locsymcount,
9020 int signed_p)
9021 {
9022 size_t len;
9023 size_t symlen;
9024 bfd_vma a;
9025 bfd_vma b;
9026 char symbuf[4096];
9027 const char *sym = *symp;
9028 const char *symend;
9029 bool symbol_is_section = false;
9030
9031 len = strlen (sym);
9032 symend = sym + len;
9033
9034 if (len < 1 || len > sizeof (symbuf))
9035 {
9036 bfd_set_error (bfd_error_invalid_operation);
9037 return false;
9038 }
9039
9040 switch (* sym)
9041 {
9042 case '.':
9043 *result = dot;
9044 *symp = sym + 1;
9045 return true;
9046
9047 case '#':
9048 ++sym;
9049 *result = strtoul (sym, (char **) symp, 16);
9050 return true;
9051
9052 case 'S':
9053 symbol_is_section = true;
9054 /* Fall through. */
9055 case 's':
9056 ++sym;
9057 symlen = strtol (sym, (char **) symp, 10);
9058 sym = *symp + 1; /* Skip the trailing ':'. */
9059
9060 if (symend < sym || symlen + 1 > sizeof (symbuf))
9061 {
9062 bfd_set_error (bfd_error_invalid_operation);
9063 return false;
9064 }
9065
9066 memcpy (symbuf, sym, symlen);
9067 symbuf[symlen] = '\0';
9068 *symp = sym + symlen;
9069
9070 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9071 the symbol as a section, or vice-versa. so we're pretty liberal in our
9072 interpretation here; section means "try section first", not "must be a
9073 section", and likewise with symbol. */
9074
9075 if (symbol_is_section)
9076 {
9077 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9078 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9079 isymbuf, locsymcount))
9080 {
9081 undefined_reference ("section", symbuf);
9082 return false;
9083 }
9084 }
9085 else
9086 {
9087 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9088 isymbuf, locsymcount)
9089 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9090 result, input_bfd))
9091 {
9092 undefined_reference ("symbol", symbuf);
9093 return false;
9094 }
9095 }
9096
9097 return true;
9098
9099 /* All that remains are operators. */
9100
9101 #define UNARY_OP(op) \
9102 if (startswith (sym, #op)) \
9103 { \
9104 sym += strlen (#op); \
9105 if (*sym == ':') \
9106 ++sym; \
9107 *symp = sym; \
9108 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9109 isymbuf, locsymcount, signed_p)) \
9110 return false; \
9111 if (signed_p) \
9112 *result = op ((bfd_signed_vma) a); \
9113 else \
9114 *result = op a; \
9115 return true; \
9116 }
9117
9118 #define BINARY_OP_HEAD(op) \
9119 if (startswith (sym, #op)) \
9120 { \
9121 sym += strlen (#op); \
9122 if (*sym == ':') \
9123 ++sym; \
9124 *symp = sym; \
9125 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9126 isymbuf, locsymcount, signed_p)) \
9127 return false; \
9128 ++*symp; \
9129 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9130 isymbuf, locsymcount, signed_p)) \
9131 return false;
9132 #define BINARY_OP_TAIL(op) \
9133 if (signed_p) \
9134 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9135 else \
9136 *result = a op b; \
9137 return true; \
9138 }
9139 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9140
9141 default:
9142 UNARY_OP (0-);
9143 BINARY_OP_HEAD (<<);
9144 if (b >= sizeof (a) * CHAR_BIT)
9145 {
9146 *result = 0;
9147 return true;
9148 }
9149 signed_p = 0;
9150 BINARY_OP_TAIL (<<);
9151 BINARY_OP_HEAD (>>);
9152 if (b >= sizeof (a) * CHAR_BIT)
9153 {
9154 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9155 return true;
9156 }
9157 BINARY_OP_TAIL (>>);
9158 BINARY_OP (==);
9159 BINARY_OP (!=);
9160 BINARY_OP (<=);
9161 BINARY_OP (>=);
9162 BINARY_OP (&&);
9163 BINARY_OP (||);
9164 UNARY_OP (~);
9165 UNARY_OP (!);
9166 BINARY_OP (*);
9167 BINARY_OP_HEAD (/);
9168 if (b == 0)
9169 {
9170 _bfd_error_handler (_("division by zero"));
9171 bfd_set_error (bfd_error_bad_value);
9172 return false;
9173 }
9174 BINARY_OP_TAIL (/);
9175 BINARY_OP_HEAD (%);
9176 if (b == 0)
9177 {
9178 _bfd_error_handler (_("division by zero"));
9179 bfd_set_error (bfd_error_bad_value);
9180 return false;
9181 }
9182 BINARY_OP_TAIL (%);
9183 BINARY_OP (^);
9184 BINARY_OP (|);
9185 BINARY_OP (&);
9186 BINARY_OP (+);
9187 BINARY_OP (-);
9188 BINARY_OP (<);
9189 BINARY_OP (>);
9190 #undef UNARY_OP
9191 #undef BINARY_OP
9192 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9193 bfd_set_error (bfd_error_invalid_operation);
9194 return false;
9195 }
9196 }
9197
9198 static void
9199 put_value (bfd_vma size,
9200 unsigned long chunksz,
9201 bfd *input_bfd,
9202 bfd_vma x,
9203 bfd_byte *location)
9204 {
9205 location += (size - chunksz);
9206
9207 for (; size; size -= chunksz, location -= chunksz)
9208 {
9209 switch (chunksz)
9210 {
9211 case 1:
9212 bfd_put_8 (input_bfd, x, location);
9213 x >>= 8;
9214 break;
9215 case 2:
9216 bfd_put_16 (input_bfd, x, location);
9217 x >>= 16;
9218 break;
9219 case 4:
9220 bfd_put_32 (input_bfd, x, location);
9221 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9222 x >>= 16;
9223 x >>= 16;
9224 break;
9225 #ifdef BFD64
9226 case 8:
9227 bfd_put_64 (input_bfd, x, location);
9228 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9229 x >>= 32;
9230 x >>= 32;
9231 break;
9232 #endif
9233 default:
9234 abort ();
9235 break;
9236 }
9237 }
9238 }
9239
9240 static bfd_vma
9241 get_value (bfd_vma size,
9242 unsigned long chunksz,
9243 bfd *input_bfd,
9244 bfd_byte *location)
9245 {
9246 int shift;
9247 bfd_vma x = 0;
9248
9249 /* Sanity checks. */
9250 BFD_ASSERT (chunksz <= sizeof (x)
9251 && size >= chunksz
9252 && chunksz != 0
9253 && (size % chunksz) == 0
9254 && input_bfd != NULL
9255 && location != NULL);
9256
9257 if (chunksz == sizeof (x))
9258 {
9259 BFD_ASSERT (size == chunksz);
9260
9261 /* Make sure that we do not perform an undefined shift operation.
9262 We know that size == chunksz so there will only be one iteration
9263 of the loop below. */
9264 shift = 0;
9265 }
9266 else
9267 shift = 8 * chunksz;
9268
9269 for (; size; size -= chunksz, location += chunksz)
9270 {
9271 switch (chunksz)
9272 {
9273 case 1:
9274 x = (x << shift) | bfd_get_8 (input_bfd, location);
9275 break;
9276 case 2:
9277 x = (x << shift) | bfd_get_16 (input_bfd, location);
9278 break;
9279 case 4:
9280 x = (x << shift) | bfd_get_32 (input_bfd, location);
9281 break;
9282 #ifdef BFD64
9283 case 8:
9284 x = (x << shift) | bfd_get_64 (input_bfd, location);
9285 break;
9286 #endif
9287 default:
9288 abort ();
9289 }
9290 }
9291 return x;
9292 }
9293
9294 static void
9295 decode_complex_addend (unsigned long *start, /* in bits */
9296 unsigned long *oplen, /* in bits */
9297 unsigned long *len, /* in bits */
9298 unsigned long *wordsz, /* in bytes */
9299 unsigned long *chunksz, /* in bytes */
9300 unsigned long *lsb0_p,
9301 unsigned long *signed_p,
9302 unsigned long *trunc_p,
9303 unsigned long encoded)
9304 {
9305 * start = encoded & 0x3F;
9306 * len = (encoded >> 6) & 0x3F;
9307 * oplen = (encoded >> 12) & 0x3F;
9308 * wordsz = (encoded >> 18) & 0xF;
9309 * chunksz = (encoded >> 22) & 0xF;
9310 * lsb0_p = (encoded >> 27) & 1;
9311 * signed_p = (encoded >> 28) & 1;
9312 * trunc_p = (encoded >> 29) & 1;
9313 }
9314
9315 bfd_reloc_status_type
9316 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9317 asection *input_section,
9318 bfd_byte *contents,
9319 Elf_Internal_Rela *rel,
9320 bfd_vma relocation)
9321 {
9322 bfd_vma shift, x, mask;
9323 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9324 bfd_reloc_status_type r;
9325 bfd_size_type octets;
9326
9327 /* Perform this reloc, since it is complex.
9328 (this is not to say that it necessarily refers to a complex
9329 symbol; merely that it is a self-describing CGEN based reloc.
9330 i.e. the addend has the complete reloc information (bit start, end,
9331 word size, etc) encoded within it.). */
9332
9333 decode_complex_addend (&start, &oplen, &len, &wordsz,
9334 &chunksz, &lsb0_p, &signed_p,
9335 &trunc_p, rel->r_addend);
9336
9337 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9338
9339 if (lsb0_p)
9340 shift = (start + 1) - len;
9341 else
9342 shift = (8 * wordsz) - (start + len);
9343
9344 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9345 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9346
9347 #ifdef DEBUG
9348 printf ("Doing complex reloc: "
9349 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9350 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9351 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9352 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9353 oplen, (unsigned long) x, (unsigned long) mask,
9354 (unsigned long) relocation);
9355 #endif
9356
9357 r = bfd_reloc_ok;
9358 if (! trunc_p)
9359 /* Now do an overflow check. */
9360 r = bfd_check_overflow ((signed_p
9361 ? complain_overflow_signed
9362 : complain_overflow_unsigned),
9363 len, 0, (8 * wordsz),
9364 relocation);
9365
9366 /* Do the deed. */
9367 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9368
9369 #ifdef DEBUG
9370 printf (" relocation: %8.8lx\n"
9371 " shifted mask: %8.8lx\n"
9372 " shifted/masked reloc: %8.8lx\n"
9373 " result: %8.8lx\n",
9374 (unsigned long) relocation, (unsigned long) (mask << shift),
9375 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9376 #endif
9377 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9378 return r;
9379 }
9380
9381 /* Functions to read r_offset from external (target order) reloc
9382 entry. Faster than bfd_getl32 et al, because we let the compiler
9383 know the value is aligned. */
9384
9385 static bfd_vma
9386 ext32l_r_offset (const void *p)
9387 {
9388 union aligned32
9389 {
9390 uint32_t v;
9391 unsigned char c[4];
9392 };
9393 const union aligned32 *a
9394 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9395
9396 uint32_t aval = ( (uint32_t) a->c[0]
9397 | (uint32_t) a->c[1] << 8
9398 | (uint32_t) a->c[2] << 16
9399 | (uint32_t) a->c[3] << 24);
9400 return aval;
9401 }
9402
9403 static bfd_vma
9404 ext32b_r_offset (const void *p)
9405 {
9406 union aligned32
9407 {
9408 uint32_t v;
9409 unsigned char c[4];
9410 };
9411 const union aligned32 *a
9412 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9413
9414 uint32_t aval = ( (uint32_t) a->c[0] << 24
9415 | (uint32_t) a->c[1] << 16
9416 | (uint32_t) a->c[2] << 8
9417 | (uint32_t) a->c[3]);
9418 return aval;
9419 }
9420
9421 static bfd_vma
9422 ext64l_r_offset (const void *p)
9423 {
9424 union aligned64
9425 {
9426 uint64_t v;
9427 unsigned char c[8];
9428 };
9429 const union aligned64 *a
9430 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9431
9432 uint64_t aval = ( (uint64_t) a->c[0]
9433 | (uint64_t) a->c[1] << 8
9434 | (uint64_t) a->c[2] << 16
9435 | (uint64_t) a->c[3] << 24
9436 | (uint64_t) a->c[4] << 32
9437 | (uint64_t) a->c[5] << 40
9438 | (uint64_t) a->c[6] << 48
9439 | (uint64_t) a->c[7] << 56);
9440 return aval;
9441 }
9442
9443 static bfd_vma
9444 ext64b_r_offset (const void *p)
9445 {
9446 union aligned64
9447 {
9448 uint64_t v;
9449 unsigned char c[8];
9450 };
9451 const union aligned64 *a
9452 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9453
9454 uint64_t aval = ( (uint64_t) a->c[0] << 56
9455 | (uint64_t) a->c[1] << 48
9456 | (uint64_t) a->c[2] << 40
9457 | (uint64_t) a->c[3] << 32
9458 | (uint64_t) a->c[4] << 24
9459 | (uint64_t) a->c[5] << 16
9460 | (uint64_t) a->c[6] << 8
9461 | (uint64_t) a->c[7]);
9462 return aval;
9463 }
9464
9465 /* When performing a relocatable link, the input relocations are
9466 preserved. But, if they reference global symbols, the indices
9467 referenced must be updated. Update all the relocations found in
9468 RELDATA. */
9469
9470 static bool
9471 elf_link_adjust_relocs (bfd *abfd,
9472 asection *sec,
9473 struct bfd_elf_section_reloc_data *reldata,
9474 bool sort,
9475 struct bfd_link_info *info)
9476 {
9477 unsigned int i;
9478 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9479 bfd_byte *erela;
9480 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9481 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9482 bfd_vma r_type_mask;
9483 int r_sym_shift;
9484 unsigned int count = reldata->count;
9485 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9486
9487 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9488 {
9489 swap_in = bed->s->swap_reloc_in;
9490 swap_out = bed->s->swap_reloc_out;
9491 }
9492 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9493 {
9494 swap_in = bed->s->swap_reloca_in;
9495 swap_out = bed->s->swap_reloca_out;
9496 }
9497 else
9498 abort ();
9499
9500 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9501 abort ();
9502
9503 if (bed->s->arch_size == 32)
9504 {
9505 r_type_mask = 0xff;
9506 r_sym_shift = 8;
9507 }
9508 else
9509 {
9510 r_type_mask = 0xffffffff;
9511 r_sym_shift = 32;
9512 }
9513
9514 erela = reldata->hdr->contents;
9515 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9516 {
9517 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9518 unsigned int j;
9519
9520 if (*rel_hash == NULL)
9521 continue;
9522
9523 if ((*rel_hash)->indx == -2
9524 && info->gc_sections
9525 && ! info->gc_keep_exported)
9526 {
9527 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9528 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9529 abfd, sec,
9530 (*rel_hash)->root.root.string);
9531 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9532 abfd, sec);
9533 bfd_set_error (bfd_error_invalid_operation);
9534 return false;
9535 }
9536 BFD_ASSERT ((*rel_hash)->indx >= 0);
9537
9538 (*swap_in) (abfd, erela, irela);
9539 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9540 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9541 | (irela[j].r_info & r_type_mask));
9542 (*swap_out) (abfd, irela, erela);
9543 }
9544
9545 if (bed->elf_backend_update_relocs)
9546 (*bed->elf_backend_update_relocs) (sec, reldata);
9547
9548 if (sort && count != 0)
9549 {
9550 bfd_vma (*ext_r_off) (const void *);
9551 bfd_vma r_off;
9552 size_t elt_size;
9553 bfd_byte *base, *end, *p, *loc;
9554 bfd_byte *buf = NULL;
9555
9556 if (bed->s->arch_size == 32)
9557 {
9558 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9559 ext_r_off = ext32l_r_offset;
9560 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9561 ext_r_off = ext32b_r_offset;
9562 else
9563 abort ();
9564 }
9565 else
9566 {
9567 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9568 ext_r_off = ext64l_r_offset;
9569 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9570 ext_r_off = ext64b_r_offset;
9571 else
9572 abort ();
9573 }
9574
9575 /* Must use a stable sort here. A modified insertion sort,
9576 since the relocs are mostly sorted already. */
9577 elt_size = reldata->hdr->sh_entsize;
9578 base = reldata->hdr->contents;
9579 end = base + count * elt_size;
9580 if (elt_size > sizeof (Elf64_External_Rela))
9581 abort ();
9582
9583 /* Ensure the first element is lowest. This acts as a sentinel,
9584 speeding the main loop below. */
9585 r_off = (*ext_r_off) (base);
9586 for (p = loc = base; (p += elt_size) < end; )
9587 {
9588 bfd_vma r_off2 = (*ext_r_off) (p);
9589 if (r_off > r_off2)
9590 {
9591 r_off = r_off2;
9592 loc = p;
9593 }
9594 }
9595 if (loc != base)
9596 {
9597 /* Don't just swap *base and *loc as that changes the order
9598 of the original base[0] and base[1] if they happen to
9599 have the same r_offset. */
9600 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9601 memcpy (onebuf, loc, elt_size);
9602 memmove (base + elt_size, base, loc - base);
9603 memcpy (base, onebuf, elt_size);
9604 }
9605
9606 for (p = base + elt_size; (p += elt_size) < end; )
9607 {
9608 /* base to p is sorted, *p is next to insert. */
9609 r_off = (*ext_r_off) (p);
9610 /* Search the sorted region for location to insert. */
9611 loc = p - elt_size;
9612 while (r_off < (*ext_r_off) (loc))
9613 loc -= elt_size;
9614 loc += elt_size;
9615 if (loc != p)
9616 {
9617 /* Chances are there is a run of relocs to insert here,
9618 from one of more input files. Files are not always
9619 linked in order due to the way elf_link_input_bfd is
9620 called. See pr17666. */
9621 size_t sortlen = p - loc;
9622 bfd_vma r_off2 = (*ext_r_off) (loc);
9623 size_t runlen = elt_size;
9624 bfd_vma r_off_runend = r_off;
9625 bfd_vma r_off_runend_next;
9626 size_t buf_size = 96 * 1024;
9627 while (p + runlen < end
9628 && (sortlen <= buf_size
9629 || runlen + elt_size <= buf_size)
9630 /* run must not break the ordering of base..loc+1 */
9631 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9632 /* run must be already sorted */
9633 && r_off_runend_next >= r_off_runend)
9634 {
9635 runlen += elt_size;
9636 r_off_runend = r_off_runend_next;
9637 }
9638 if (buf == NULL)
9639 {
9640 buf = bfd_malloc (buf_size);
9641 if (buf == NULL)
9642 return false;
9643 }
9644 if (runlen < sortlen)
9645 {
9646 memcpy (buf, p, runlen);
9647 memmove (loc + runlen, loc, sortlen);
9648 memcpy (loc, buf, runlen);
9649 }
9650 else
9651 {
9652 memcpy (buf, loc, sortlen);
9653 memmove (loc, p, runlen);
9654 memcpy (loc + runlen, buf, sortlen);
9655 }
9656 p += runlen - elt_size;
9657 }
9658 }
9659 /* Hashes are no longer valid. */
9660 free (reldata->hashes);
9661 reldata->hashes = NULL;
9662 free (buf);
9663 }
9664 return true;
9665 }
9666
9667 struct elf_link_sort_rela
9668 {
9669 union {
9670 bfd_vma offset;
9671 bfd_vma sym_mask;
9672 } u;
9673 enum elf_reloc_type_class type;
9674 /* We use this as an array of size int_rels_per_ext_rel. */
9675 Elf_Internal_Rela rela[1];
9676 };
9677
9678 /* qsort stability here and for cmp2 is only an issue if multiple
9679 dynamic relocations are emitted at the same address. But targets
9680 that apply a series of dynamic relocations each operating on the
9681 result of the prior relocation can't use -z combreloc as
9682 implemented anyway. Such schemes tend to be broken by sorting on
9683 symbol index. That leaves dynamic NONE relocs as the only other
9684 case where ld might emit multiple relocs at the same address, and
9685 those are only emitted due to target bugs. */
9686
9687 static int
9688 elf_link_sort_cmp1 (const void *A, const void *B)
9689 {
9690 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9691 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9692 int relativea, relativeb;
9693
9694 relativea = a->type == reloc_class_relative;
9695 relativeb = b->type == reloc_class_relative;
9696
9697 if (relativea < relativeb)
9698 return 1;
9699 if (relativea > relativeb)
9700 return -1;
9701 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9702 return -1;
9703 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9704 return 1;
9705 if (a->rela->r_offset < b->rela->r_offset)
9706 return -1;
9707 if (a->rela->r_offset > b->rela->r_offset)
9708 return 1;
9709 return 0;
9710 }
9711
9712 static int
9713 elf_link_sort_cmp2 (const void *A, const void *B)
9714 {
9715 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9716 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9717
9718 if (a->type < b->type)
9719 return -1;
9720 if (a->type > b->type)
9721 return 1;
9722 if (a->u.offset < b->u.offset)
9723 return -1;
9724 if (a->u.offset > b->u.offset)
9725 return 1;
9726 if (a->rela->r_offset < b->rela->r_offset)
9727 return -1;
9728 if (a->rela->r_offset > b->rela->r_offset)
9729 return 1;
9730 return 0;
9731 }
9732
9733 static size_t
9734 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9735 {
9736 asection *dynamic_relocs;
9737 asection *rela_dyn;
9738 asection *rel_dyn;
9739 bfd_size_type count, size;
9740 size_t i, ret, sort_elt, ext_size;
9741 bfd_byte *sort, *s_non_relative, *p;
9742 struct elf_link_sort_rela *sq;
9743 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9744 int i2e = bed->s->int_rels_per_ext_rel;
9745 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9746 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9747 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9748 struct bfd_link_order *lo;
9749 bfd_vma r_sym_mask;
9750 bool use_rela;
9751
9752 /* Find a dynamic reloc section. */
9753 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9754 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9755 if (rela_dyn != NULL && rela_dyn->size > 0
9756 && rel_dyn != NULL && rel_dyn->size > 0)
9757 {
9758 bool use_rela_initialised = false;
9759
9760 /* This is just here to stop gcc from complaining.
9761 Its initialization checking code is not perfect. */
9762 use_rela = true;
9763
9764 /* Both sections are present. Examine the sizes
9765 of the indirect sections to help us choose. */
9766 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9767 if (lo->type == bfd_indirect_link_order)
9768 {
9769 asection *o = lo->u.indirect.section;
9770
9771 if ((o->size % bed->s->sizeof_rela) == 0)
9772 {
9773 if ((o->size % bed->s->sizeof_rel) == 0)
9774 /* Section size is divisible by both rel and rela sizes.
9775 It is of no help to us. */
9776 ;
9777 else
9778 {
9779 /* Section size is only divisible by rela. */
9780 if (use_rela_initialised && !use_rela)
9781 {
9782 _bfd_error_handler (_("%pB: unable to sort relocs - "
9783 "they are in more than one size"),
9784 abfd);
9785 bfd_set_error (bfd_error_invalid_operation);
9786 return 0;
9787 }
9788 else
9789 {
9790 use_rela = true;
9791 use_rela_initialised = true;
9792 }
9793 }
9794 }
9795 else if ((o->size % bed->s->sizeof_rel) == 0)
9796 {
9797 /* Section size is only divisible by rel. */
9798 if (use_rela_initialised && use_rela)
9799 {
9800 _bfd_error_handler (_("%pB: unable to sort relocs - "
9801 "they are in more than one size"),
9802 abfd);
9803 bfd_set_error (bfd_error_invalid_operation);
9804 return 0;
9805 }
9806 else
9807 {
9808 use_rela = false;
9809 use_rela_initialised = true;
9810 }
9811 }
9812 else
9813 {
9814 /* The section size is not divisible by either -
9815 something is wrong. */
9816 _bfd_error_handler (_("%pB: unable to sort relocs - "
9817 "they are of an unknown size"), abfd);
9818 bfd_set_error (bfd_error_invalid_operation);
9819 return 0;
9820 }
9821 }
9822
9823 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9824 if (lo->type == bfd_indirect_link_order)
9825 {
9826 asection *o = lo->u.indirect.section;
9827
9828 if ((o->size % bed->s->sizeof_rela) == 0)
9829 {
9830 if ((o->size % bed->s->sizeof_rel) == 0)
9831 /* Section size is divisible by both rel and rela sizes.
9832 It is of no help to us. */
9833 ;
9834 else
9835 {
9836 /* Section size is only divisible by rela. */
9837 if (use_rela_initialised && !use_rela)
9838 {
9839 _bfd_error_handler (_("%pB: unable to sort relocs - "
9840 "they are in more than one size"),
9841 abfd);
9842 bfd_set_error (bfd_error_invalid_operation);
9843 return 0;
9844 }
9845 else
9846 {
9847 use_rela = true;
9848 use_rela_initialised = true;
9849 }
9850 }
9851 }
9852 else if ((o->size % bed->s->sizeof_rel) == 0)
9853 {
9854 /* Section size is only divisible by rel. */
9855 if (use_rela_initialised && use_rela)
9856 {
9857 _bfd_error_handler (_("%pB: unable to sort relocs - "
9858 "they are in more than one size"),
9859 abfd);
9860 bfd_set_error (bfd_error_invalid_operation);
9861 return 0;
9862 }
9863 else
9864 {
9865 use_rela = false;
9866 use_rela_initialised = true;
9867 }
9868 }
9869 else
9870 {
9871 /* The section size is not divisible by either -
9872 something is wrong. */
9873 _bfd_error_handler (_("%pB: unable to sort relocs - "
9874 "they are of an unknown size"), abfd);
9875 bfd_set_error (bfd_error_invalid_operation);
9876 return 0;
9877 }
9878 }
9879
9880 if (! use_rela_initialised)
9881 /* Make a guess. */
9882 use_rela = true;
9883 }
9884 else if (rela_dyn != NULL && rela_dyn->size > 0)
9885 use_rela = true;
9886 else if (rel_dyn != NULL && rel_dyn->size > 0)
9887 use_rela = false;
9888 else
9889 return 0;
9890
9891 if (use_rela)
9892 {
9893 dynamic_relocs = rela_dyn;
9894 ext_size = bed->s->sizeof_rela;
9895 swap_in = bed->s->swap_reloca_in;
9896 swap_out = bed->s->swap_reloca_out;
9897 }
9898 else
9899 {
9900 dynamic_relocs = rel_dyn;
9901 ext_size = bed->s->sizeof_rel;
9902 swap_in = bed->s->swap_reloc_in;
9903 swap_out = bed->s->swap_reloc_out;
9904 }
9905
9906 size = 0;
9907 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9908 if (lo->type == bfd_indirect_link_order)
9909 size += lo->u.indirect.section->size;
9910
9911 if (size != dynamic_relocs->size)
9912 return 0;
9913
9914 sort_elt = (sizeof (struct elf_link_sort_rela)
9915 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9916
9917 count = dynamic_relocs->size / ext_size;
9918 if (count == 0)
9919 return 0;
9920 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9921
9922 if (sort == NULL)
9923 {
9924 (*info->callbacks->warning)
9925 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9926 return 0;
9927 }
9928
9929 if (bed->s->arch_size == 32)
9930 r_sym_mask = ~(bfd_vma) 0xff;
9931 else
9932 r_sym_mask = ~(bfd_vma) 0xffffffff;
9933
9934 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9935 if (lo->type == bfd_indirect_link_order)
9936 {
9937 bfd_byte *erel, *erelend;
9938 asection *o = lo->u.indirect.section;
9939
9940 if (o->contents == NULL && o->size != 0)
9941 {
9942 /* This is a reloc section that is being handled as a normal
9943 section. See bfd_section_from_shdr. We can't combine
9944 relocs in this case. */
9945 free (sort);
9946 return 0;
9947 }
9948 erel = o->contents;
9949 erelend = o->contents + o->size;
9950 p = sort + o->output_offset * opb / ext_size * sort_elt;
9951
9952 while (erel < erelend)
9953 {
9954 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9955
9956 (*swap_in) (abfd, erel, s->rela);
9957 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9958 s->u.sym_mask = r_sym_mask;
9959 p += sort_elt;
9960 erel += ext_size;
9961 }
9962 }
9963
9964 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9965
9966 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9967 {
9968 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9969 if (s->type != reloc_class_relative)
9970 break;
9971 }
9972 ret = i;
9973 s_non_relative = p;
9974
9975 sq = (struct elf_link_sort_rela *) s_non_relative;
9976 for (; i < count; i++, p += sort_elt)
9977 {
9978 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9979 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9980 sq = sp;
9981 sp->u.offset = sq->rela->r_offset;
9982 }
9983
9984 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9985
9986 struct elf_link_hash_table *htab = elf_hash_table (info);
9987 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9988 {
9989 /* We have plt relocs in .rela.dyn. */
9990 sq = (struct elf_link_sort_rela *) sort;
9991 for (i = 0; i < count; i++)
9992 if (sq[count - i - 1].type != reloc_class_plt)
9993 break;
9994 if (i != 0 && htab->srelplt->size == i * ext_size)
9995 {
9996 struct bfd_link_order **plo;
9997 /* Put srelplt link_order last. This is so the output_offset
9998 set in the next loop is correct for DT_JMPREL. */
9999 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10000 if ((*plo)->type == bfd_indirect_link_order
10001 && (*plo)->u.indirect.section == htab->srelplt)
10002 {
10003 lo = *plo;
10004 *plo = lo->next;
10005 }
10006 else
10007 plo = &(*plo)->next;
10008 *plo = lo;
10009 lo->next = NULL;
10010 dynamic_relocs->map_tail.link_order = lo;
10011 }
10012 }
10013
10014 p = sort;
10015 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10016 if (lo->type == bfd_indirect_link_order)
10017 {
10018 bfd_byte *erel, *erelend;
10019 asection *o = lo->u.indirect.section;
10020
10021 erel = o->contents;
10022 erelend = o->contents + o->size;
10023 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10024 while (erel < erelend)
10025 {
10026 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10027 (*swap_out) (abfd, s->rela, erel);
10028 p += sort_elt;
10029 erel += ext_size;
10030 }
10031 }
10032
10033 free (sort);
10034 *psec = dynamic_relocs;
10035 return ret;
10036 }
10037
10038 /* Add a symbol to the output symbol string table. */
10039
10040 static int
10041 elf_link_output_symstrtab (void *finf,
10042 const char *name,
10043 Elf_Internal_Sym *elfsym,
10044 asection *input_sec,
10045 struct elf_link_hash_entry *h)
10046 {
10047 struct elf_final_link_info *flinfo = finf;
10048 int (*output_symbol_hook)
10049 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10050 struct elf_link_hash_entry *);
10051 struct elf_link_hash_table *hash_table;
10052 const struct elf_backend_data *bed;
10053 bfd_size_type strtabsize;
10054
10055 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10056
10057 bed = get_elf_backend_data (flinfo->output_bfd);
10058 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10059 if (output_symbol_hook != NULL)
10060 {
10061 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10062 if (ret != 1)
10063 return ret;
10064 }
10065
10066 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10067 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10068 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10069 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10070
10071 if (name == NULL || *name == '\0')
10072 elfsym->st_name = (unsigned long) -1;
10073 else
10074 {
10075 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10076 to get the final offset for st_name. */
10077 char *versioned_name = (char *) name;
10078 if (h != NULL)
10079 {
10080 if (h->versioned == versioned && h->def_dynamic)
10081 {
10082 /* Keep only one '@' for versioned symbols defined in
10083 shared objects. */
10084 char *version = strrchr (name, ELF_VER_CHR);
10085 char *base_end = strchr (name, ELF_VER_CHR);
10086 if (version != base_end)
10087 {
10088 size_t base_len;
10089 size_t len = strlen (name);
10090 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10091 if (versioned_name == NULL)
10092 return 0;
10093 base_len = base_end - name;
10094 memcpy (versioned_name, name, base_len);
10095 memcpy (versioned_name + base_len, version,
10096 len - base_len);
10097 }
10098 }
10099 }
10100 else if (flinfo->info->unique_symbol
10101 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10102 {
10103 struct local_hash_entry *lh;
10104 size_t count_len;
10105 size_t base_len;
10106 char buf[30];
10107 switch (ELF_ST_TYPE (elfsym->st_info))
10108 {
10109 case STT_FILE:
10110 case STT_SECTION:
10111 break;
10112 default:
10113 lh = (struct local_hash_entry *) bfd_hash_lookup
10114 (&flinfo->local_hash_table, name, true, false);
10115 if (lh == NULL)
10116 return 0;
10117 /* Always append ".COUNT" to local symbols to avoid
10118 potential conflicts with local symbol "XXX.COUNT". */
10119 sprintf (buf, "%lx", lh->count);
10120 base_len = lh->size;
10121 if (!base_len)
10122 {
10123 base_len = strlen (name);
10124 lh->size = base_len;
10125 }
10126 count_len = strlen (buf);
10127 versioned_name = bfd_alloc (flinfo->output_bfd,
10128 base_len + count_len + 2);
10129 if (versioned_name == NULL)
10130 return 0;
10131 memcpy (versioned_name, name, base_len);
10132 versioned_name[base_len] = '.';
10133 memcpy (versioned_name + base_len + 1, buf,
10134 count_len + 1);
10135 lh->count++;
10136 break;
10137 }
10138 }
10139 elfsym->st_name
10140 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10141 versioned_name, false);
10142 if (elfsym->st_name == (unsigned long) -1)
10143 return 0;
10144 }
10145
10146 hash_table = elf_hash_table (flinfo->info);
10147 strtabsize = hash_table->strtabsize;
10148 if (strtabsize <= flinfo->output_bfd->symcount)
10149 {
10150 strtabsize += strtabsize;
10151 hash_table->strtabsize = strtabsize;
10152 strtabsize *= sizeof (*hash_table->strtab);
10153 hash_table->strtab
10154 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10155 strtabsize);
10156 if (hash_table->strtab == NULL)
10157 return 0;
10158 }
10159 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10160 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10161 = flinfo->output_bfd->symcount;
10162 flinfo->output_bfd->symcount += 1;
10163
10164 return 1;
10165 }
10166
10167 /* Swap symbols out to the symbol table and flush the output symbols to
10168 the file. */
10169
10170 static bool
10171 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10172 {
10173 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10174 size_t amt;
10175 size_t i;
10176 const struct elf_backend_data *bed;
10177 bfd_byte *symbuf;
10178 Elf_Internal_Shdr *hdr;
10179 file_ptr pos;
10180 bool ret;
10181
10182 if (flinfo->output_bfd->symcount == 0)
10183 return true;
10184
10185 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10186
10187 bed = get_elf_backend_data (flinfo->output_bfd);
10188
10189 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10190 symbuf = (bfd_byte *) bfd_malloc (amt);
10191 if (symbuf == NULL)
10192 return false;
10193
10194 if (flinfo->symshndxbuf)
10195 {
10196 amt = sizeof (Elf_External_Sym_Shndx);
10197 amt *= bfd_get_symcount (flinfo->output_bfd);
10198 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10199 if (flinfo->symshndxbuf == NULL)
10200 {
10201 free (symbuf);
10202 return false;
10203 }
10204 }
10205
10206 /* Now swap out the symbols. */
10207 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10208 {
10209 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10210 if (elfsym->sym.st_name == (unsigned long) -1)
10211 elfsym->sym.st_name = 0;
10212 else
10213 elfsym->sym.st_name
10214 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10215 elfsym->sym.st_name);
10216
10217 /* Inform the linker of the addition of this symbol. */
10218
10219 if (flinfo->info->callbacks->ctf_new_symbol)
10220 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10221 &elfsym->sym);
10222
10223 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10224 ((bfd_byte *) symbuf
10225 + (elfsym->dest_index
10226 * bed->s->sizeof_sym)),
10227 NPTR_ADD (flinfo->symshndxbuf,
10228 elfsym->dest_index));
10229 }
10230
10231 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10232 pos = hdr->sh_offset + hdr->sh_size;
10233 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10234 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10235 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10236 {
10237 hdr->sh_size += amt;
10238 ret = true;
10239 }
10240 else
10241 ret = false;
10242
10243 free (symbuf);
10244
10245 free (hash_table->strtab);
10246 hash_table->strtab = NULL;
10247
10248 return ret;
10249 }
10250
10251 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10252
10253 static bool
10254 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10255 {
10256 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10257 && sym->st_shndx < SHN_LORESERVE)
10258 {
10259 /* The gABI doesn't support dynamic symbols in output sections
10260 beyond 64k. */
10261 _bfd_error_handler
10262 /* xgettext:c-format */
10263 (_("%pB: too many sections: %d (>= %d)"),
10264 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10265 bfd_set_error (bfd_error_nonrepresentable_section);
10266 return false;
10267 }
10268 return true;
10269 }
10270
10271 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10272 allowing an unsatisfied unversioned symbol in the DSO to match a
10273 versioned symbol that would normally require an explicit version.
10274 We also handle the case that a DSO references a hidden symbol
10275 which may be satisfied by a versioned symbol in another DSO. */
10276
10277 static bool
10278 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10279 const struct elf_backend_data *bed,
10280 struct elf_link_hash_entry *h)
10281 {
10282 bfd *abfd;
10283 struct elf_link_loaded_list *loaded;
10284
10285 if (!is_elf_hash_table (info->hash))
10286 return false;
10287
10288 /* Check indirect symbol. */
10289 while (h->root.type == bfd_link_hash_indirect)
10290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10291
10292 switch (h->root.type)
10293 {
10294 default:
10295 abfd = NULL;
10296 break;
10297
10298 case bfd_link_hash_undefined:
10299 case bfd_link_hash_undefweak:
10300 abfd = h->root.u.undef.abfd;
10301 if (abfd == NULL
10302 || (abfd->flags & DYNAMIC) == 0
10303 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10304 return false;
10305 break;
10306
10307 case bfd_link_hash_defined:
10308 case bfd_link_hash_defweak:
10309 abfd = h->root.u.def.section->owner;
10310 break;
10311
10312 case bfd_link_hash_common:
10313 abfd = h->root.u.c.p->section->owner;
10314 break;
10315 }
10316 BFD_ASSERT (abfd != NULL);
10317
10318 for (loaded = elf_hash_table (info)->dyn_loaded;
10319 loaded != NULL;
10320 loaded = loaded->next)
10321 {
10322 bfd *input;
10323 Elf_Internal_Shdr *hdr;
10324 size_t symcount;
10325 size_t extsymcount;
10326 size_t extsymoff;
10327 Elf_Internal_Shdr *versymhdr;
10328 Elf_Internal_Sym *isym;
10329 Elf_Internal_Sym *isymend;
10330 Elf_Internal_Sym *isymbuf;
10331 Elf_External_Versym *ever;
10332 Elf_External_Versym *extversym;
10333
10334 input = loaded->abfd;
10335
10336 /* We check each DSO for a possible hidden versioned definition. */
10337 if (input == abfd
10338 || elf_dynversym (input) == 0)
10339 continue;
10340
10341 hdr = &elf_tdata (input)->dynsymtab_hdr;
10342
10343 symcount = hdr->sh_size / bed->s->sizeof_sym;
10344 if (elf_bad_symtab (input))
10345 {
10346 extsymcount = symcount;
10347 extsymoff = 0;
10348 }
10349 else
10350 {
10351 extsymcount = symcount - hdr->sh_info;
10352 extsymoff = hdr->sh_info;
10353 }
10354
10355 if (extsymcount == 0)
10356 continue;
10357
10358 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10359 NULL, NULL, NULL);
10360 if (isymbuf == NULL)
10361 return false;
10362
10363 /* Read in any version definitions. */
10364 versymhdr = &elf_tdata (input)->dynversym_hdr;
10365 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10366 || (extversym = (Elf_External_Versym *)
10367 _bfd_malloc_and_read (input, versymhdr->sh_size,
10368 versymhdr->sh_size)) == NULL)
10369 {
10370 free (isymbuf);
10371 return false;
10372 }
10373
10374 ever = extversym + extsymoff;
10375 isymend = isymbuf + extsymcount;
10376 for (isym = isymbuf; isym < isymend; isym++, ever++)
10377 {
10378 const char *name;
10379 Elf_Internal_Versym iver;
10380 unsigned short version_index;
10381
10382 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10383 || isym->st_shndx == SHN_UNDEF)
10384 continue;
10385
10386 name = bfd_elf_string_from_elf_section (input,
10387 hdr->sh_link,
10388 isym->st_name);
10389 if (strcmp (name, h->root.root.string) != 0)
10390 continue;
10391
10392 _bfd_elf_swap_versym_in (input, ever, &iver);
10393
10394 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10395 && !(h->def_regular
10396 && h->forced_local))
10397 {
10398 /* If we have a non-hidden versioned sym, then it should
10399 have provided a definition for the undefined sym unless
10400 it is defined in a non-shared object and forced local.
10401 */
10402 abort ();
10403 }
10404
10405 version_index = iver.vs_vers & VERSYM_VERSION;
10406 if (version_index == 1 || version_index == 2)
10407 {
10408 /* This is the base or first version. We can use it. */
10409 free (extversym);
10410 free (isymbuf);
10411 return true;
10412 }
10413 }
10414
10415 free (extversym);
10416 free (isymbuf);
10417 }
10418
10419 return false;
10420 }
10421
10422 /* Convert ELF common symbol TYPE. */
10423
10424 static int
10425 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10426 {
10427 /* Commom symbol can only appear in relocatable link. */
10428 if (!bfd_link_relocatable (info))
10429 abort ();
10430 switch (info->elf_stt_common)
10431 {
10432 case unchanged:
10433 break;
10434 case elf_stt_common:
10435 type = STT_COMMON;
10436 break;
10437 case no_elf_stt_common:
10438 type = STT_OBJECT;
10439 break;
10440 }
10441 return type;
10442 }
10443
10444 /* Add an external symbol to the symbol table. This is called from
10445 the hash table traversal routine. When generating a shared object,
10446 we go through the symbol table twice. The first time we output
10447 anything that might have been forced to local scope in a version
10448 script. The second time we output the symbols that are still
10449 global symbols. */
10450
10451 static bool
10452 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10453 {
10454 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10455 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10456 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10457 bool strip;
10458 Elf_Internal_Sym sym;
10459 asection *input_sec;
10460 const struct elf_backend_data *bed;
10461 long indx;
10462 int ret;
10463 unsigned int type;
10464
10465 if (h->root.type == bfd_link_hash_warning)
10466 {
10467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10468 if (h->root.type == bfd_link_hash_new)
10469 return true;
10470 }
10471
10472 /* Decide whether to output this symbol in this pass. */
10473 if (eoinfo->localsyms)
10474 {
10475 if (!h->forced_local)
10476 return true;
10477 }
10478 else
10479 {
10480 if (h->forced_local)
10481 return true;
10482 }
10483
10484 bed = get_elf_backend_data (flinfo->output_bfd);
10485
10486 if (h->root.type == bfd_link_hash_undefined)
10487 {
10488 /* If we have an undefined symbol reference here then it must have
10489 come from a shared library that is being linked in. (Undefined
10490 references in regular files have already been handled unless
10491 they are in unreferenced sections which are removed by garbage
10492 collection). */
10493 bool ignore_undef = false;
10494
10495 /* Some symbols may be special in that the fact that they're
10496 undefined can be safely ignored - let backend determine that. */
10497 if (bed->elf_backend_ignore_undef_symbol)
10498 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10499
10500 /* If we are reporting errors for this situation then do so now. */
10501 if (!ignore_undef
10502 && h->ref_dynamic_nonweak
10503 && (!h->ref_regular || flinfo->info->gc_sections)
10504 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10505 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10506 {
10507 flinfo->info->callbacks->undefined_symbol
10508 (flinfo->info, h->root.root.string,
10509 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10510 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10511 && !flinfo->info->warn_unresolved_syms);
10512 }
10513
10514 /* Strip a global symbol defined in a discarded section. */
10515 if (h->indx == -3)
10516 return true;
10517 }
10518
10519 /* We should also warn if a forced local symbol is referenced from
10520 shared libraries. */
10521 if (bfd_link_executable (flinfo->info)
10522 && h->forced_local
10523 && h->ref_dynamic
10524 && h->def_regular
10525 && !h->dynamic_def
10526 && h->ref_dynamic_nonweak
10527 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10528 {
10529 bfd *def_bfd;
10530 const char *msg;
10531 struct elf_link_hash_entry *hi = h;
10532
10533 /* Check indirect symbol. */
10534 while (hi->root.type == bfd_link_hash_indirect)
10535 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10536
10537 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10538 /* xgettext:c-format */
10539 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10540 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10541 /* xgettext:c-format */
10542 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10543 else
10544 /* xgettext:c-format */
10545 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10546 def_bfd = flinfo->output_bfd;
10547 if (hi->root.u.def.section != bfd_abs_section_ptr)
10548 def_bfd = hi->root.u.def.section->owner;
10549 _bfd_error_handler (msg, flinfo->output_bfd,
10550 h->root.root.string, def_bfd);
10551 bfd_set_error (bfd_error_bad_value);
10552 eoinfo->failed = true;
10553 return false;
10554 }
10555
10556 /* We don't want to output symbols that have never been mentioned by
10557 a regular file, or that we have been told to strip. However, if
10558 h->indx is set to -2, the symbol is used by a reloc and we must
10559 output it. */
10560 strip = false;
10561 if (h->indx == -2)
10562 ;
10563 else if ((h->def_dynamic
10564 || h->ref_dynamic
10565 || h->root.type == bfd_link_hash_new)
10566 && !h->def_regular
10567 && !h->ref_regular)
10568 strip = true;
10569 else if (flinfo->info->strip == strip_all)
10570 strip = true;
10571 else if (flinfo->info->strip == strip_some
10572 && bfd_hash_lookup (flinfo->info->keep_hash,
10573 h->root.root.string, false, false) == NULL)
10574 strip = true;
10575 else if ((h->root.type == bfd_link_hash_defined
10576 || h->root.type == bfd_link_hash_defweak)
10577 && ((flinfo->info->strip_discarded
10578 && discarded_section (h->root.u.def.section))
10579 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10580 && h->root.u.def.section->owner != NULL
10581 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10582 strip = true;
10583 else if ((h->root.type == bfd_link_hash_undefined
10584 || h->root.type == bfd_link_hash_undefweak)
10585 && h->root.u.undef.abfd != NULL
10586 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10587 strip = true;
10588
10589 type = h->type;
10590
10591 /* If we're stripping it, and it's not a dynamic symbol, there's
10592 nothing else to do. However, if it is a forced local symbol or
10593 an ifunc symbol we need to give the backend finish_dynamic_symbol
10594 function a chance to make it dynamic. */
10595 if (strip
10596 && h->dynindx == -1
10597 && type != STT_GNU_IFUNC
10598 && !h->forced_local)
10599 return true;
10600
10601 sym.st_value = 0;
10602 sym.st_size = h->size;
10603 sym.st_other = h->other;
10604 switch (h->root.type)
10605 {
10606 default:
10607 case bfd_link_hash_new:
10608 case bfd_link_hash_warning:
10609 abort ();
10610 return false;
10611
10612 case bfd_link_hash_undefined:
10613 case bfd_link_hash_undefweak:
10614 input_sec = bfd_und_section_ptr;
10615 sym.st_shndx = SHN_UNDEF;
10616 break;
10617
10618 case bfd_link_hash_defined:
10619 case bfd_link_hash_defweak:
10620 {
10621 input_sec = h->root.u.def.section;
10622 if (input_sec->output_section != NULL)
10623 {
10624 sym.st_shndx =
10625 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10626 input_sec->output_section);
10627 if (sym.st_shndx == SHN_BAD)
10628 {
10629 _bfd_error_handler
10630 /* xgettext:c-format */
10631 (_("%pB: could not find output section %pA for input section %pA"),
10632 flinfo->output_bfd, input_sec->output_section, input_sec);
10633 bfd_set_error (bfd_error_nonrepresentable_section);
10634 eoinfo->failed = true;
10635 return false;
10636 }
10637
10638 /* ELF symbols in relocatable files are section relative,
10639 but in nonrelocatable files they are virtual
10640 addresses. */
10641 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10642 if (!bfd_link_relocatable (flinfo->info))
10643 {
10644 sym.st_value += input_sec->output_section->vma;
10645 if (h->type == STT_TLS)
10646 {
10647 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10648 if (tls_sec != NULL)
10649 sym.st_value -= tls_sec->vma;
10650 }
10651 }
10652 }
10653 else
10654 {
10655 BFD_ASSERT (input_sec->owner == NULL
10656 || (input_sec->owner->flags & DYNAMIC) != 0);
10657 sym.st_shndx = SHN_UNDEF;
10658 input_sec = bfd_und_section_ptr;
10659 }
10660 }
10661 break;
10662
10663 case bfd_link_hash_common:
10664 input_sec = h->root.u.c.p->section;
10665 sym.st_shndx = bed->common_section_index (input_sec);
10666 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10667 break;
10668
10669 case bfd_link_hash_indirect:
10670 /* These symbols are created by symbol versioning. They point
10671 to the decorated version of the name. For example, if the
10672 symbol foo@@GNU_1.2 is the default, which should be used when
10673 foo is used with no version, then we add an indirect symbol
10674 foo which points to foo@@GNU_1.2. We ignore these symbols,
10675 since the indirected symbol is already in the hash table. */
10676 return true;
10677 }
10678
10679 if (type == STT_COMMON || type == STT_OBJECT)
10680 switch (h->root.type)
10681 {
10682 case bfd_link_hash_common:
10683 type = elf_link_convert_common_type (flinfo->info, type);
10684 break;
10685 case bfd_link_hash_defined:
10686 case bfd_link_hash_defweak:
10687 if (bed->common_definition (&sym))
10688 type = elf_link_convert_common_type (flinfo->info, type);
10689 else
10690 type = STT_OBJECT;
10691 break;
10692 case bfd_link_hash_undefined:
10693 case bfd_link_hash_undefweak:
10694 break;
10695 default:
10696 abort ();
10697 }
10698
10699 if (h->forced_local)
10700 {
10701 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10702 /* Turn off visibility on local symbol. */
10703 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10704 }
10705 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10706 else if (h->unique_global && h->def_regular)
10707 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10708 else if (h->root.type == bfd_link_hash_undefweak
10709 || h->root.type == bfd_link_hash_defweak)
10710 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10711 else
10712 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10713 sym.st_target_internal = h->target_internal;
10714
10715 /* Give the processor backend a chance to tweak the symbol value,
10716 and also to finish up anything that needs to be done for this
10717 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10718 forced local syms when non-shared is due to a historical quirk.
10719 STT_GNU_IFUNC symbol must go through PLT. */
10720 if ((h->type == STT_GNU_IFUNC
10721 && h->def_regular
10722 && !bfd_link_relocatable (flinfo->info))
10723 || ((h->dynindx != -1
10724 || h->forced_local)
10725 && ((bfd_link_pic (flinfo->info)
10726 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10727 || h->root.type != bfd_link_hash_undefweak))
10728 || !h->forced_local)
10729 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10730 {
10731 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10732 (flinfo->output_bfd, flinfo->info, h, &sym)))
10733 {
10734 eoinfo->failed = true;
10735 return false;
10736 }
10737 }
10738
10739 /* If we are marking the symbol as undefined, and there are no
10740 non-weak references to this symbol from a regular object, then
10741 mark the symbol as weak undefined; if there are non-weak
10742 references, mark the symbol as strong. We can't do this earlier,
10743 because it might not be marked as undefined until the
10744 finish_dynamic_symbol routine gets through with it. */
10745 if (sym.st_shndx == SHN_UNDEF
10746 && h->ref_regular
10747 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10748 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10749 {
10750 int bindtype;
10751 type = ELF_ST_TYPE (sym.st_info);
10752
10753 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10754 if (type == STT_GNU_IFUNC)
10755 type = STT_FUNC;
10756
10757 if (h->ref_regular_nonweak)
10758 bindtype = STB_GLOBAL;
10759 else
10760 bindtype = STB_WEAK;
10761 sym.st_info = ELF_ST_INFO (bindtype, type);
10762 }
10763
10764 /* If this is a symbol defined in a dynamic library, don't use the
10765 symbol size from the dynamic library. Relinking an executable
10766 against a new library may introduce gratuitous changes in the
10767 executable's symbols if we keep the size. */
10768 if (sym.st_shndx == SHN_UNDEF
10769 && !h->def_regular
10770 && h->def_dynamic)
10771 sym.st_size = 0;
10772
10773 /* If a non-weak symbol with non-default visibility is not defined
10774 locally, it is a fatal error. */
10775 if (!bfd_link_relocatable (flinfo->info)
10776 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10777 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10778 && h->root.type == bfd_link_hash_undefined
10779 && !h->def_regular)
10780 {
10781 const char *msg;
10782
10783 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10784 /* xgettext:c-format */
10785 msg = _("%pB: protected symbol `%s' isn't defined");
10786 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10787 /* xgettext:c-format */
10788 msg = _("%pB: internal symbol `%s' isn't defined");
10789 else
10790 /* xgettext:c-format */
10791 msg = _("%pB: hidden symbol `%s' isn't defined");
10792 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10793 bfd_set_error (bfd_error_bad_value);
10794 eoinfo->failed = true;
10795 return false;
10796 }
10797
10798 /* If this symbol should be put in the .dynsym section, then put it
10799 there now. We already know the symbol index. We also fill in
10800 the entry in the .hash section. */
10801 if (h->dynindx != -1
10802 && elf_hash_table (flinfo->info)->dynamic_sections_created
10803 && elf_hash_table (flinfo->info)->dynsym != NULL
10804 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10805 {
10806 bfd_byte *esym;
10807
10808 /* Since there is no version information in the dynamic string,
10809 if there is no version info in symbol version section, we will
10810 have a run-time problem if not linking executable, referenced
10811 by shared library, or not bound locally. */
10812 if (h->verinfo.verdef == NULL
10813 && (!bfd_link_executable (flinfo->info)
10814 || h->ref_dynamic
10815 || !h->def_regular))
10816 {
10817 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10818
10819 if (p && p [1] != '\0')
10820 {
10821 _bfd_error_handler
10822 /* xgettext:c-format */
10823 (_("%pB: no symbol version section for versioned symbol `%s'"),
10824 flinfo->output_bfd, h->root.root.string);
10825 eoinfo->failed = true;
10826 return false;
10827 }
10828 }
10829
10830 sym.st_name = h->dynstr_index;
10831 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10832 + h->dynindx * bed->s->sizeof_sym);
10833 if (!check_dynsym (flinfo->output_bfd, &sym))
10834 {
10835 eoinfo->failed = true;
10836 return false;
10837 }
10838
10839 /* Inform the linker of the addition of this symbol. */
10840
10841 if (flinfo->info->callbacks->ctf_new_dynsym)
10842 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10843
10844 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10845
10846 if (flinfo->hash_sec != NULL)
10847 {
10848 size_t hash_entry_size;
10849 bfd_byte *bucketpos;
10850 bfd_vma chain;
10851 size_t bucketcount;
10852 size_t bucket;
10853
10854 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10855 bucket = h->u.elf_hash_value % bucketcount;
10856
10857 hash_entry_size
10858 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10859 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10860 + (bucket + 2) * hash_entry_size);
10861 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10862 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10863 bucketpos);
10864 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10865 ((bfd_byte *) flinfo->hash_sec->contents
10866 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10867 }
10868
10869 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10870 {
10871 Elf_Internal_Versym iversym;
10872 Elf_External_Versym *eversym;
10873
10874 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10875 {
10876 if (h->verinfo.verdef == NULL
10877 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10878 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10879 iversym.vs_vers = 1;
10880 else
10881 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10882 }
10883 else
10884 {
10885 if (h->verinfo.vertree == NULL)
10886 iversym.vs_vers = 1;
10887 else
10888 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10889 if (flinfo->info->create_default_symver)
10890 iversym.vs_vers++;
10891 }
10892
10893 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10894 defined locally. */
10895 if (h->versioned == versioned_hidden && h->def_regular)
10896 iversym.vs_vers |= VERSYM_HIDDEN;
10897
10898 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10899 eversym += h->dynindx;
10900 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10901 }
10902 }
10903
10904 /* If the symbol is undefined, and we didn't output it to .dynsym,
10905 strip it from .symtab too. Obviously we can't do this for
10906 relocatable output or when needed for --emit-relocs. */
10907 else if (input_sec == bfd_und_section_ptr
10908 && h->indx != -2
10909 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10910 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10911 && !bfd_link_relocatable (flinfo->info))
10912 return true;
10913
10914 /* Also strip others that we couldn't earlier due to dynamic symbol
10915 processing. */
10916 if (strip)
10917 return true;
10918 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10919 return true;
10920
10921 /* Output a FILE symbol so that following locals are not associated
10922 with the wrong input file. We need one for forced local symbols
10923 if we've seen more than one FILE symbol or when we have exactly
10924 one FILE symbol but global symbols are present in a file other
10925 than the one with the FILE symbol. We also need one if linker
10926 defined symbols are present. In practice these conditions are
10927 always met, so just emit the FILE symbol unconditionally. */
10928 if (eoinfo->localsyms
10929 && !eoinfo->file_sym_done
10930 && eoinfo->flinfo->filesym_count != 0)
10931 {
10932 Elf_Internal_Sym fsym;
10933
10934 memset (&fsym, 0, sizeof (fsym));
10935 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10936 fsym.st_shndx = SHN_ABS;
10937 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10938 bfd_und_section_ptr, NULL))
10939 return false;
10940
10941 eoinfo->file_sym_done = true;
10942 }
10943
10944 indx = bfd_get_symcount (flinfo->output_bfd);
10945 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10946 input_sec, h);
10947 if (ret == 0)
10948 {
10949 eoinfo->failed = true;
10950 return false;
10951 }
10952 else if (ret == 1)
10953 h->indx = indx;
10954 else if (h->indx == -2)
10955 abort();
10956
10957 return true;
10958 }
10959
10960 /* Return TRUE if special handling is done for relocs in SEC against
10961 symbols defined in discarded sections. */
10962
10963 static bool
10964 elf_section_ignore_discarded_relocs (asection *sec)
10965 {
10966 const struct elf_backend_data *bed;
10967
10968 switch (sec->sec_info_type)
10969 {
10970 case SEC_INFO_TYPE_STABS:
10971 case SEC_INFO_TYPE_EH_FRAME:
10972 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10973 case SEC_INFO_TYPE_SFRAME:
10974 return true;
10975 default:
10976 break;
10977 }
10978
10979 bed = get_elf_backend_data (sec->owner);
10980 if (bed->elf_backend_ignore_discarded_relocs != NULL
10981 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10982 return true;
10983
10984 return false;
10985 }
10986
10987 /* Return a mask saying how ld should treat relocations in SEC against
10988 symbols defined in discarded sections. If this function returns
10989 COMPLAIN set, ld will issue a warning message. If this function
10990 returns PRETEND set, and the discarded section was link-once and the
10991 same size as the kept link-once section, ld will pretend that the
10992 symbol was actually defined in the kept section. Otherwise ld will
10993 zero the reloc (at least that is the intent, but some cooperation by
10994 the target dependent code is needed, particularly for REL targets). */
10995
10996 unsigned int
10997 _bfd_elf_default_action_discarded (asection *sec)
10998 {
10999 const struct elf_backend_data *bed;
11000 bed = get_elf_backend_data (sec->owner);
11001
11002 if (sec->flags & SEC_DEBUGGING)
11003 return PRETEND;
11004
11005 if (strcmp (".eh_frame", sec->name) == 0)
11006 return 0;
11007
11008 if (bed->elf_backend_can_make_multiple_eh_frame
11009 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11010 return 0;
11011
11012 if (strcmp (".sframe", sec->name) == 0)
11013 return 0;
11014
11015 if (strcmp (".gcc_except_table", sec->name) == 0)
11016 return 0;
11017
11018 return COMPLAIN | PRETEND;
11019 }
11020
11021 /* Find a match between a section and a member of a section group. */
11022
11023 static asection *
11024 match_group_member (asection *sec, asection *group,
11025 struct bfd_link_info *info)
11026 {
11027 asection *first = elf_next_in_group (group);
11028 asection *s = first;
11029
11030 while (s != NULL)
11031 {
11032 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11033 return s;
11034
11035 s = elf_next_in_group (s);
11036 if (s == first)
11037 break;
11038 }
11039
11040 return NULL;
11041 }
11042
11043 /* Check if the kept section of a discarded section SEC can be used
11044 to replace it. Return the replacement if it is OK. Otherwise return
11045 NULL. */
11046
11047 asection *
11048 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11049 {
11050 asection *kept;
11051
11052 kept = sec->kept_section;
11053 if (kept != NULL)
11054 {
11055 if ((kept->flags & SEC_GROUP) != 0)
11056 kept = match_group_member (sec, kept, info);
11057 if (kept != NULL)
11058 {
11059 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11060 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11061 kept = NULL;
11062 else
11063 {
11064 /* Get the real kept section. */
11065 asection *next;
11066 for (next = kept->kept_section;
11067 next != NULL;
11068 next = next->kept_section)
11069 kept = next;
11070 }
11071 }
11072 sec->kept_section = kept;
11073 }
11074 return kept;
11075 }
11076
11077 /* Link an input file into the linker output file. This function
11078 handles all the sections and relocations of the input file at once.
11079 This is so that we only have to read the local symbols once, and
11080 don't have to keep them in memory. */
11081
11082 static bool
11083 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11084 {
11085 int (*relocate_section)
11086 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11087 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11088 bfd *output_bfd;
11089 Elf_Internal_Shdr *symtab_hdr;
11090 size_t locsymcount;
11091 size_t extsymoff;
11092 Elf_Internal_Sym *isymbuf;
11093 Elf_Internal_Sym *isym;
11094 Elf_Internal_Sym *isymend;
11095 long *pindex;
11096 asection **ppsection;
11097 asection *o;
11098 const struct elf_backend_data *bed;
11099 struct elf_link_hash_entry **sym_hashes;
11100 bfd_size_type address_size;
11101 bfd_vma r_type_mask;
11102 int r_sym_shift;
11103 bool have_file_sym = false;
11104
11105 output_bfd = flinfo->output_bfd;
11106 bed = get_elf_backend_data (output_bfd);
11107 relocate_section = bed->elf_backend_relocate_section;
11108
11109 /* If this is a dynamic object, we don't want to do anything here:
11110 we don't want the local symbols, and we don't want the section
11111 contents. */
11112 if ((input_bfd->flags & DYNAMIC) != 0)
11113 return true;
11114
11115 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11116 if (elf_bad_symtab (input_bfd))
11117 {
11118 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11119 extsymoff = 0;
11120 }
11121 else
11122 {
11123 locsymcount = symtab_hdr->sh_info;
11124 extsymoff = symtab_hdr->sh_info;
11125 }
11126
11127 /* Enable GNU OSABI features in the output BFD that are used in the input
11128 BFD. */
11129 if (bed->elf_osabi == ELFOSABI_NONE
11130 || bed->elf_osabi == ELFOSABI_GNU
11131 || bed->elf_osabi == ELFOSABI_FREEBSD)
11132 elf_tdata (output_bfd)->has_gnu_osabi
11133 |= (elf_tdata (input_bfd)->has_gnu_osabi
11134 & (bfd_link_relocatable (flinfo->info)
11135 ? -1 : ~elf_gnu_osabi_retain));
11136
11137 /* Read the local symbols. */
11138 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11139 if (isymbuf == NULL && locsymcount != 0)
11140 {
11141 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11142 flinfo->internal_syms,
11143 flinfo->external_syms,
11144 flinfo->locsym_shndx);
11145 if (isymbuf == NULL)
11146 return false;
11147 }
11148
11149 /* Find local symbol sections and adjust values of symbols in
11150 SEC_MERGE sections. Write out those local symbols we know are
11151 going into the output file. */
11152 isymend = PTR_ADD (isymbuf, locsymcount);
11153 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11154 isym < isymend;
11155 isym++, pindex++, ppsection++)
11156 {
11157 asection *isec;
11158 const char *name;
11159 Elf_Internal_Sym osym;
11160 long indx;
11161 int ret;
11162
11163 *pindex = -1;
11164
11165 if (elf_bad_symtab (input_bfd))
11166 {
11167 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11168 {
11169 *ppsection = NULL;
11170 continue;
11171 }
11172 }
11173
11174 if (isym->st_shndx == SHN_UNDEF)
11175 isec = bfd_und_section_ptr;
11176 else if (isym->st_shndx == SHN_ABS)
11177 isec = bfd_abs_section_ptr;
11178 else if (isym->st_shndx == SHN_COMMON)
11179 isec = bfd_com_section_ptr;
11180 else
11181 {
11182 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11183 if (isec == NULL)
11184 {
11185 /* Don't attempt to output symbols with st_shnx in the
11186 reserved range other than SHN_ABS and SHN_COMMON. */
11187 isec = bfd_und_section_ptr;
11188 }
11189 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11190 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11191 isym->st_value =
11192 _bfd_merged_section_offset (output_bfd, &isec,
11193 elf_section_data (isec)->sec_info,
11194 isym->st_value);
11195 }
11196
11197 *ppsection = isec;
11198
11199 /* Don't output the first, undefined, symbol. In fact, don't
11200 output any undefined local symbol. */
11201 if (isec == bfd_und_section_ptr)
11202 continue;
11203
11204 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11205 {
11206 /* We never output section symbols. Instead, we use the
11207 section symbol of the corresponding section in the output
11208 file. */
11209 continue;
11210 }
11211
11212 /* If we are stripping all symbols, we don't want to output this
11213 one. */
11214 if (flinfo->info->strip == strip_all)
11215 continue;
11216
11217 /* If we are discarding all local symbols, we don't want to
11218 output this one. If we are generating a relocatable output
11219 file, then some of the local symbols may be required by
11220 relocs; we output them below as we discover that they are
11221 needed. */
11222 if (flinfo->info->discard == discard_all)
11223 continue;
11224
11225 /* If this symbol is defined in a section which we are
11226 discarding, we don't need to keep it. */
11227 if (isym->st_shndx < SHN_LORESERVE
11228 && (isec->output_section == NULL
11229 || bfd_section_removed_from_list (output_bfd,
11230 isec->output_section)))
11231 continue;
11232
11233 /* Get the name of the symbol. */
11234 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11235 isym->st_name);
11236 if (name == NULL)
11237 return false;
11238
11239 /* See if we are discarding symbols with this name. */
11240 if ((flinfo->info->strip == strip_some
11241 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11242 == NULL))
11243 || (((flinfo->info->discard == discard_sec_merge
11244 && (isec->flags & SEC_MERGE)
11245 && !bfd_link_relocatable (flinfo->info))
11246 || flinfo->info->discard == discard_l)
11247 && bfd_is_local_label_name (input_bfd, name)))
11248 continue;
11249
11250 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11251 {
11252 if (input_bfd->lto_output)
11253 /* -flto puts a temp file name here. This means builds
11254 are not reproducible. Discard the symbol. */
11255 continue;
11256 have_file_sym = true;
11257 flinfo->filesym_count += 1;
11258 }
11259 if (!have_file_sym)
11260 {
11261 /* In the absence of debug info, bfd_find_nearest_line uses
11262 FILE symbols to determine the source file for local
11263 function symbols. Provide a FILE symbol here if input
11264 files lack such, so that their symbols won't be
11265 associated with a previous input file. It's not the
11266 source file, but the best we can do. */
11267 const char *filename;
11268 have_file_sym = true;
11269 flinfo->filesym_count += 1;
11270 memset (&osym, 0, sizeof (osym));
11271 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11272 osym.st_shndx = SHN_ABS;
11273 if (input_bfd->lto_output)
11274 filename = NULL;
11275 else
11276 filename = lbasename (bfd_get_filename (input_bfd));
11277 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11278 bfd_abs_section_ptr, NULL))
11279 return false;
11280 }
11281
11282 osym = *isym;
11283
11284 /* Adjust the section index for the output file. */
11285 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11286 isec->output_section);
11287 if (osym.st_shndx == SHN_BAD)
11288 return false;
11289
11290 /* ELF symbols in relocatable files are section relative, but
11291 in executable files they are virtual addresses. Note that
11292 this code assumes that all ELF sections have an associated
11293 BFD section with a reasonable value for output_offset; below
11294 we assume that they also have a reasonable value for
11295 output_section. Any special sections must be set up to meet
11296 these requirements. */
11297 osym.st_value += isec->output_offset;
11298 if (!bfd_link_relocatable (flinfo->info))
11299 {
11300 osym.st_value += isec->output_section->vma;
11301 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11302 {
11303 /* STT_TLS symbols are relative to PT_TLS segment base. */
11304 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11305 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11306 else
11307 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11308 STT_NOTYPE);
11309 }
11310 }
11311
11312 indx = bfd_get_symcount (output_bfd);
11313 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11314 if (ret == 0)
11315 return false;
11316 else if (ret == 1)
11317 *pindex = indx;
11318 }
11319
11320 if (bed->s->arch_size == 32)
11321 {
11322 r_type_mask = 0xff;
11323 r_sym_shift = 8;
11324 address_size = 4;
11325 }
11326 else
11327 {
11328 r_type_mask = 0xffffffff;
11329 r_sym_shift = 32;
11330 address_size = 8;
11331 }
11332
11333 /* Relocate the contents of each section. */
11334 sym_hashes = elf_sym_hashes (input_bfd);
11335 for (o = input_bfd->sections; o != NULL; o = o->next)
11336 {
11337 bfd_byte *contents;
11338
11339 if (! o->linker_mark)
11340 {
11341 /* This section was omitted from the link. */
11342 continue;
11343 }
11344
11345 if (!flinfo->info->resolve_section_groups
11346 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11347 {
11348 /* Deal with the group signature symbol. */
11349 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11350 unsigned long symndx = sec_data->this_hdr.sh_info;
11351 asection *osec = o->output_section;
11352
11353 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11354 if (symndx >= locsymcount
11355 || (elf_bad_symtab (input_bfd)
11356 && flinfo->sections[symndx] == NULL))
11357 {
11358 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11359 while (h->root.type == bfd_link_hash_indirect
11360 || h->root.type == bfd_link_hash_warning)
11361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11362 /* Arrange for symbol to be output. */
11363 h->indx = -2;
11364 elf_section_data (osec)->this_hdr.sh_info = -2;
11365 }
11366 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11367 {
11368 /* We'll use the output section target_index. */
11369 asection *sec = flinfo->sections[symndx]->output_section;
11370 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11371 }
11372 else
11373 {
11374 if (flinfo->indices[symndx] == -1)
11375 {
11376 /* Otherwise output the local symbol now. */
11377 Elf_Internal_Sym sym = isymbuf[symndx];
11378 asection *sec = flinfo->sections[symndx]->output_section;
11379 const char *name;
11380 long indx;
11381 int ret;
11382
11383 name = bfd_elf_string_from_elf_section (input_bfd,
11384 symtab_hdr->sh_link,
11385 sym.st_name);
11386 if (name == NULL)
11387 return false;
11388
11389 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11390 sec);
11391 if (sym.st_shndx == SHN_BAD)
11392 return false;
11393
11394 sym.st_value += o->output_offset;
11395
11396 indx = bfd_get_symcount (output_bfd);
11397 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11398 NULL);
11399 if (ret == 0)
11400 return false;
11401 else if (ret == 1)
11402 flinfo->indices[symndx] = indx;
11403 else
11404 abort ();
11405 }
11406 elf_section_data (osec)->this_hdr.sh_info
11407 = flinfo->indices[symndx];
11408 }
11409 }
11410
11411 if ((o->flags & SEC_HAS_CONTENTS) == 0
11412 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11413 continue;
11414
11415 if ((o->flags & SEC_LINKER_CREATED) != 0)
11416 {
11417 /* Section was created by _bfd_elf_link_create_dynamic_sections
11418 or somesuch. */
11419 continue;
11420 }
11421
11422 /* Get the contents of the section. They have been cached by a
11423 relaxation routine. Note that o is a section in an input
11424 file, so the contents field will not have been set by any of
11425 the routines which work on output files. */
11426 if (elf_section_data (o)->this_hdr.contents != NULL)
11427 {
11428 contents = elf_section_data (o)->this_hdr.contents;
11429 if (bed->caches_rawsize
11430 && o->rawsize != 0
11431 && o->rawsize < o->size)
11432 {
11433 memcpy (flinfo->contents, contents, o->rawsize);
11434 contents = flinfo->contents;
11435 }
11436 }
11437 else if (!(o->flags & SEC_RELOC)
11438 && !bed->elf_backend_write_section
11439 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11440 /* A MERGE section that has no relocations doesn't need the
11441 contents anymore, they have been recorded earlier. Except
11442 if the backend has special provisions for writing sections. */
11443 contents = NULL;
11444 else
11445 {
11446 contents = flinfo->contents;
11447 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11448 return false;
11449 }
11450
11451 if ((o->flags & SEC_RELOC) != 0)
11452 {
11453 Elf_Internal_Rela *internal_relocs;
11454 Elf_Internal_Rela *rel, *relend;
11455 int action_discarded;
11456 int ret;
11457
11458 /* Get the swapped relocs. */
11459 internal_relocs
11460 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11461 flinfo->external_relocs,
11462 flinfo->internal_relocs,
11463 false);
11464 if (internal_relocs == NULL
11465 && o->reloc_count > 0)
11466 return false;
11467
11468 action_discarded = -1;
11469 if (!elf_section_ignore_discarded_relocs (o))
11470 action_discarded = (*bed->action_discarded) (o);
11471
11472 /* Run through the relocs evaluating complex reloc symbols and
11473 looking for relocs against symbols from discarded sections
11474 or section symbols from removed link-once sections.
11475 Complain about relocs against discarded sections. Zero
11476 relocs against removed link-once sections. */
11477
11478 rel = internal_relocs;
11479 relend = rel + o->reloc_count;
11480 for ( ; rel < relend; rel++)
11481 {
11482 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11483 unsigned int s_type;
11484 asection **ps, *sec;
11485 struct elf_link_hash_entry *h = NULL;
11486 const char *sym_name;
11487
11488 if (r_symndx == STN_UNDEF)
11489 continue;
11490
11491 if (r_symndx >= locsymcount
11492 || (elf_bad_symtab (input_bfd)
11493 && flinfo->sections[r_symndx] == NULL))
11494 {
11495 h = sym_hashes[r_symndx - extsymoff];
11496
11497 /* Badly formatted input files can contain relocs that
11498 reference non-existant symbols. Check here so that
11499 we do not seg fault. */
11500 if (h == NULL)
11501 {
11502 _bfd_error_handler
11503 /* xgettext:c-format */
11504 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11505 "that references a non-existent global symbol"),
11506 input_bfd, (uint64_t) rel->r_info, o);
11507 bfd_set_error (bfd_error_bad_value);
11508 return false;
11509 }
11510
11511 while (h->root.type == bfd_link_hash_indirect
11512 || h->root.type == bfd_link_hash_warning)
11513 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11514
11515 s_type = h->type;
11516
11517 /* If a plugin symbol is referenced from a non-IR file,
11518 mark the symbol as undefined. Note that the
11519 linker may attach linker created dynamic sections
11520 to the plugin bfd. Symbols defined in linker
11521 created sections are not plugin symbols. */
11522 if ((h->root.non_ir_ref_regular
11523 || h->root.non_ir_ref_dynamic)
11524 && (h->root.type == bfd_link_hash_defined
11525 || h->root.type == bfd_link_hash_defweak)
11526 && (h->root.u.def.section->flags
11527 & SEC_LINKER_CREATED) == 0
11528 && h->root.u.def.section->owner != NULL
11529 && (h->root.u.def.section->owner->flags
11530 & BFD_PLUGIN) != 0)
11531 {
11532 h->root.type = bfd_link_hash_undefined;
11533 h->root.u.undef.abfd = h->root.u.def.section->owner;
11534 }
11535
11536 ps = NULL;
11537 if (h->root.type == bfd_link_hash_defined
11538 || h->root.type == bfd_link_hash_defweak)
11539 ps = &h->root.u.def.section;
11540
11541 sym_name = h->root.root.string;
11542 }
11543 else
11544 {
11545 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11546
11547 s_type = ELF_ST_TYPE (sym->st_info);
11548 ps = &flinfo->sections[r_symndx];
11549 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11550 sym, *ps);
11551 }
11552
11553 if ((s_type == STT_RELC || s_type == STT_SRELC)
11554 && !bfd_link_relocatable (flinfo->info))
11555 {
11556 bfd_vma val;
11557 bfd_vma dot = (rel->r_offset
11558 + o->output_offset + o->output_section->vma);
11559 #ifdef DEBUG
11560 printf ("Encountered a complex symbol!");
11561 printf (" (input_bfd %s, section %s, reloc %ld\n",
11562 bfd_get_filename (input_bfd), o->name,
11563 (long) (rel - internal_relocs));
11564 printf (" symbol: idx %8.8lx, name %s\n",
11565 r_symndx, sym_name);
11566 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11567 (unsigned long) rel->r_info,
11568 (unsigned long) rel->r_offset);
11569 #endif
11570 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11571 isymbuf, locsymcount, s_type == STT_SRELC))
11572 return false;
11573
11574 /* Symbol evaluated OK. Update to absolute value. */
11575 set_symbol_value (input_bfd, isymbuf, locsymcount,
11576 r_symndx, val);
11577 continue;
11578 }
11579
11580 if (action_discarded != -1 && ps != NULL)
11581 {
11582 /* Complain if the definition comes from a
11583 discarded section. */
11584 if ((sec = *ps) != NULL && discarded_section (sec))
11585 {
11586 BFD_ASSERT (r_symndx != STN_UNDEF);
11587 if (action_discarded & COMPLAIN)
11588 (*flinfo->info->callbacks->einfo)
11589 /* xgettext:c-format */
11590 (_("%X`%s' referenced in section `%pA' of %pB: "
11591 "defined in discarded section `%pA' of %pB\n"),
11592 sym_name, o, input_bfd, sec, sec->owner);
11593
11594 /* Try to do the best we can to support buggy old
11595 versions of gcc. Pretend that the symbol is
11596 really defined in the kept linkonce section.
11597 FIXME: This is quite broken. Modifying the
11598 symbol here means we will be changing all later
11599 uses of the symbol, not just in this section. */
11600 if (action_discarded & PRETEND)
11601 {
11602 asection *kept;
11603
11604 kept = _bfd_elf_check_kept_section (sec,
11605 flinfo->info);
11606 if (kept != NULL)
11607 {
11608 *ps = kept;
11609 continue;
11610 }
11611 }
11612 }
11613 }
11614 }
11615
11616 /* Relocate the section by invoking a back end routine.
11617
11618 The back end routine is responsible for adjusting the
11619 section contents as necessary, and (if using Rela relocs
11620 and generating a relocatable output file) adjusting the
11621 reloc addend as necessary.
11622
11623 The back end routine does not have to worry about setting
11624 the reloc address or the reloc symbol index.
11625
11626 The back end routine is given a pointer to the swapped in
11627 internal symbols, and can access the hash table entries
11628 for the external symbols via elf_sym_hashes (input_bfd).
11629
11630 When generating relocatable output, the back end routine
11631 must handle STB_LOCAL/STT_SECTION symbols specially. The
11632 output symbol is going to be a section symbol
11633 corresponding to the output section, which will require
11634 the addend to be adjusted. */
11635
11636 ret = (*relocate_section) (output_bfd, flinfo->info,
11637 input_bfd, o, contents,
11638 internal_relocs,
11639 isymbuf,
11640 flinfo->sections);
11641 if (!ret)
11642 return false;
11643
11644 if (ret == 2
11645 || bfd_link_relocatable (flinfo->info)
11646 || flinfo->info->emitrelocations)
11647 {
11648 Elf_Internal_Rela *irela;
11649 Elf_Internal_Rela *irelaend, *irelamid;
11650 bfd_vma last_offset;
11651 struct elf_link_hash_entry **rel_hash;
11652 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11653 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11654 unsigned int next_erel;
11655 bool rela_normal;
11656 struct bfd_elf_section_data *esdi, *esdo;
11657
11658 esdi = elf_section_data (o);
11659 esdo = elf_section_data (o->output_section);
11660 rela_normal = false;
11661
11662 /* Adjust the reloc addresses and symbol indices. */
11663
11664 irela = internal_relocs;
11665 irelaend = irela + o->reloc_count;
11666 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11667 /* We start processing the REL relocs, if any. When we reach
11668 IRELAMID in the loop, we switch to the RELA relocs. */
11669 irelamid = irela;
11670 if (esdi->rel.hdr != NULL)
11671 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11672 * bed->s->int_rels_per_ext_rel);
11673 rel_hash_list = rel_hash;
11674 rela_hash_list = NULL;
11675 last_offset = o->output_offset;
11676 if (!bfd_link_relocatable (flinfo->info))
11677 last_offset += o->output_section->vma;
11678 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11679 {
11680 unsigned long r_symndx;
11681 asection *sec;
11682 Elf_Internal_Sym sym;
11683
11684 if (next_erel == bed->s->int_rels_per_ext_rel)
11685 {
11686 rel_hash++;
11687 next_erel = 0;
11688 }
11689
11690 if (irela == irelamid)
11691 {
11692 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11693 rela_hash_list = rel_hash;
11694 if (bed->is_rela_normal != NULL)
11695 rela_normal = bed->is_rela_normal (irela);
11696 else
11697 rela_normal = bed->rela_normal;
11698 }
11699
11700 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11701 flinfo->info, o,
11702 irela->r_offset);
11703 if (irela->r_offset >= (bfd_vma) -2)
11704 {
11705 /* This is a reloc for a deleted entry or somesuch.
11706 Turn it into an R_*_NONE reloc, at the same
11707 offset as the last reloc. elf_eh_frame.c and
11708 bfd_elf_discard_info rely on reloc offsets
11709 being ordered. */
11710 irela->r_offset = last_offset;
11711 irela->r_info = 0;
11712 irela->r_addend = 0;
11713 continue;
11714 }
11715
11716 irela->r_offset += o->output_offset;
11717
11718 /* Relocs in an executable have to be virtual addresses. */
11719 if (!bfd_link_relocatable (flinfo->info))
11720 irela->r_offset += o->output_section->vma;
11721
11722 last_offset = irela->r_offset;
11723
11724 r_symndx = irela->r_info >> r_sym_shift;
11725 if (r_symndx == STN_UNDEF)
11726 continue;
11727
11728 if (r_symndx >= locsymcount
11729 || (elf_bad_symtab (input_bfd)
11730 && flinfo->sections[r_symndx] == NULL))
11731 {
11732 struct elf_link_hash_entry *rh;
11733 unsigned long indx;
11734
11735 /* This is a reloc against a global symbol. We
11736 have not yet output all the local symbols, so
11737 we do not know the symbol index of any global
11738 symbol. We set the rel_hash entry for this
11739 reloc to point to the global hash table entry
11740 for this symbol. The symbol index is then
11741 set at the end of bfd_elf_final_link. */
11742 indx = r_symndx - extsymoff;
11743 rh = elf_sym_hashes (input_bfd)[indx];
11744 while (rh->root.type == bfd_link_hash_indirect
11745 || rh->root.type == bfd_link_hash_warning)
11746 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11747
11748 /* Setting the index to -2 tells
11749 elf_link_output_extsym that this symbol is
11750 used by a reloc. */
11751 BFD_ASSERT (rh->indx < 0);
11752 rh->indx = -2;
11753 *rel_hash = rh;
11754
11755 continue;
11756 }
11757
11758 /* This is a reloc against a local symbol. */
11759
11760 *rel_hash = NULL;
11761 sym = isymbuf[r_symndx];
11762 sec = flinfo->sections[r_symndx];
11763 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11764 {
11765 /* I suppose the backend ought to fill in the
11766 section of any STT_SECTION symbol against a
11767 processor specific section. */
11768 r_symndx = STN_UNDEF;
11769 if (bfd_is_abs_section (sec))
11770 ;
11771 else if (sec == NULL || sec->owner == NULL)
11772 {
11773 bfd_set_error (bfd_error_bad_value);
11774 return false;
11775 }
11776 else
11777 {
11778 asection *osec = sec->output_section;
11779
11780 /* If we have discarded a section, the output
11781 section will be the absolute section. In
11782 case of discarded SEC_MERGE sections, use
11783 the kept section. relocate_section should
11784 have already handled discarded linkonce
11785 sections. */
11786 if (bfd_is_abs_section (osec)
11787 && sec->kept_section != NULL
11788 && sec->kept_section->output_section != NULL)
11789 {
11790 osec = sec->kept_section->output_section;
11791 irela->r_addend -= osec->vma;
11792 }
11793
11794 if (!bfd_is_abs_section (osec))
11795 {
11796 r_symndx = osec->target_index;
11797 if (r_symndx == STN_UNDEF)
11798 {
11799 irela->r_addend += osec->vma;
11800 osec = _bfd_nearby_section (output_bfd, osec,
11801 osec->vma);
11802 irela->r_addend -= osec->vma;
11803 r_symndx = osec->target_index;
11804 }
11805 }
11806 }
11807
11808 /* Adjust the addend according to where the
11809 section winds up in the output section. */
11810 if (rela_normal)
11811 irela->r_addend += sec->output_offset;
11812 }
11813 else
11814 {
11815 if (flinfo->indices[r_symndx] == -1)
11816 {
11817 unsigned long shlink;
11818 const char *name;
11819 asection *osec;
11820 long indx;
11821
11822 if (flinfo->info->strip == strip_all)
11823 {
11824 /* You can't do ld -r -s. */
11825 bfd_set_error (bfd_error_invalid_operation);
11826 return false;
11827 }
11828
11829 /* This symbol was skipped earlier, but
11830 since it is needed by a reloc, we
11831 must output it now. */
11832 shlink = symtab_hdr->sh_link;
11833 name = (bfd_elf_string_from_elf_section
11834 (input_bfd, shlink, sym.st_name));
11835 if (name == NULL)
11836 return false;
11837
11838 osec = sec->output_section;
11839 sym.st_shndx =
11840 _bfd_elf_section_from_bfd_section (output_bfd,
11841 osec);
11842 if (sym.st_shndx == SHN_BAD)
11843 return false;
11844
11845 sym.st_value += sec->output_offset;
11846 if (!bfd_link_relocatable (flinfo->info))
11847 {
11848 sym.st_value += osec->vma;
11849 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11850 {
11851 struct elf_link_hash_table *htab
11852 = elf_hash_table (flinfo->info);
11853
11854 /* STT_TLS symbols are relative to PT_TLS
11855 segment base. */
11856 if (htab->tls_sec != NULL)
11857 sym.st_value -= htab->tls_sec->vma;
11858 else
11859 sym.st_info
11860 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11861 STT_NOTYPE);
11862 }
11863 }
11864
11865 indx = bfd_get_symcount (output_bfd);
11866 ret = elf_link_output_symstrtab (flinfo, name,
11867 &sym, sec,
11868 NULL);
11869 if (ret == 0)
11870 return false;
11871 else if (ret == 1)
11872 flinfo->indices[r_symndx] = indx;
11873 else
11874 abort ();
11875 }
11876
11877 r_symndx = flinfo->indices[r_symndx];
11878 }
11879
11880 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11881 | (irela->r_info & r_type_mask));
11882 }
11883
11884 /* Swap out the relocs. */
11885 input_rel_hdr = esdi->rel.hdr;
11886 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11887 {
11888 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11889 input_rel_hdr,
11890 internal_relocs,
11891 rel_hash_list))
11892 return false;
11893 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11894 * bed->s->int_rels_per_ext_rel);
11895 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11896 }
11897
11898 input_rela_hdr = esdi->rela.hdr;
11899 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11900 {
11901 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11902 input_rela_hdr,
11903 internal_relocs,
11904 rela_hash_list))
11905 return false;
11906 }
11907 }
11908 }
11909
11910 /* Write out the modified section contents. */
11911 if (bed->elf_backend_write_section
11912 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11913 contents))
11914 {
11915 /* Section written out. */
11916 }
11917 else switch (o->sec_info_type)
11918 {
11919 case SEC_INFO_TYPE_STABS:
11920 if (! (_bfd_write_section_stabs
11921 (output_bfd,
11922 &elf_hash_table (flinfo->info)->stab_info,
11923 o, &elf_section_data (o)->sec_info, contents)))
11924 return false;
11925 break;
11926 case SEC_INFO_TYPE_MERGE:
11927 if (! _bfd_write_merged_section (output_bfd, o,
11928 elf_section_data (o)->sec_info))
11929 return false;
11930 break;
11931 case SEC_INFO_TYPE_EH_FRAME:
11932 {
11933 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11934 o, contents))
11935 return false;
11936 }
11937 break;
11938 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11939 {
11940 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11941 flinfo->info,
11942 o, contents))
11943 return false;
11944 }
11945 break;
11946 case SEC_INFO_TYPE_SFRAME:
11947 {
11948 /* Merge .sframe sections into the ctf frame encoder
11949 context of the output_bfd's section. The final .sframe
11950 output section will be written out later. */
11951 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11952 o, contents))
11953 return false;
11954 }
11955 break;
11956 default:
11957 {
11958 if (! (o->flags & SEC_EXCLUDE))
11959 {
11960 file_ptr offset = (file_ptr) o->output_offset;
11961 bfd_size_type todo = o->size;
11962
11963 offset *= bfd_octets_per_byte (output_bfd, o);
11964
11965 if ((o->flags & SEC_ELF_REVERSE_COPY)
11966 && o->size > address_size)
11967 {
11968 /* Reverse-copy input section to output. */
11969
11970 if ((o->size & (address_size - 1)) != 0
11971 || (o->reloc_count != 0
11972 && (o->size * bed->s->int_rels_per_ext_rel
11973 != o->reloc_count * address_size)))
11974 {
11975 _bfd_error_handler
11976 /* xgettext:c-format */
11977 (_("error: %pB: size of section %pA is not "
11978 "multiple of address size"),
11979 input_bfd, o);
11980 bfd_set_error (bfd_error_bad_value);
11981 return false;
11982 }
11983
11984 do
11985 {
11986 todo -= address_size;
11987 if (! bfd_set_section_contents (output_bfd,
11988 o->output_section,
11989 contents + todo,
11990 offset,
11991 address_size))
11992 return false;
11993 if (todo == 0)
11994 break;
11995 offset += address_size;
11996 }
11997 while (1);
11998 }
11999 else if (! bfd_set_section_contents (output_bfd,
12000 o->output_section,
12001 contents,
12002 offset, todo))
12003 return false;
12004 }
12005 }
12006 break;
12007 }
12008 }
12009
12010 return true;
12011 }
12012
12013 /* Generate a reloc when linking an ELF file. This is a reloc
12014 requested by the linker, and does not come from any input file. This
12015 is used to build constructor and destructor tables when linking
12016 with -Ur. */
12017
12018 static bool
12019 elf_reloc_link_order (bfd *output_bfd,
12020 struct bfd_link_info *info,
12021 asection *output_section,
12022 struct bfd_link_order *link_order)
12023 {
12024 reloc_howto_type *howto;
12025 long indx;
12026 bfd_vma offset;
12027 bfd_vma addend;
12028 struct bfd_elf_section_reloc_data *reldata;
12029 struct elf_link_hash_entry **rel_hash_ptr;
12030 Elf_Internal_Shdr *rel_hdr;
12031 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12032 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12033 bfd_byte *erel;
12034 unsigned int i;
12035 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12036
12037 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12038 if (howto == NULL)
12039 {
12040 bfd_set_error (bfd_error_bad_value);
12041 return false;
12042 }
12043
12044 addend = link_order->u.reloc.p->addend;
12045
12046 if (esdo->rel.hdr)
12047 reldata = &esdo->rel;
12048 else if (esdo->rela.hdr)
12049 reldata = &esdo->rela;
12050 else
12051 {
12052 reldata = NULL;
12053 BFD_ASSERT (0);
12054 }
12055
12056 /* Figure out the symbol index. */
12057 rel_hash_ptr = reldata->hashes + reldata->count;
12058 if (link_order->type == bfd_section_reloc_link_order)
12059 {
12060 indx = link_order->u.reloc.p->u.section->target_index;
12061 BFD_ASSERT (indx != 0);
12062 *rel_hash_ptr = NULL;
12063 }
12064 else
12065 {
12066 struct elf_link_hash_entry *h;
12067
12068 /* Treat a reloc against a defined symbol as though it were
12069 actually against the section. */
12070 h = ((struct elf_link_hash_entry *)
12071 bfd_wrapped_link_hash_lookup (output_bfd, info,
12072 link_order->u.reloc.p->u.name,
12073 false, false, true));
12074 if (h != NULL
12075 && (h->root.type == bfd_link_hash_defined
12076 || h->root.type == bfd_link_hash_defweak))
12077 {
12078 asection *section;
12079
12080 section = h->root.u.def.section;
12081 indx = section->output_section->target_index;
12082 *rel_hash_ptr = NULL;
12083 /* It seems that we ought to add the symbol value to the
12084 addend here, but in practice it has already been added
12085 because it was passed to constructor_callback. */
12086 addend += section->output_section->vma + section->output_offset;
12087 }
12088 else if (h != NULL)
12089 {
12090 /* Setting the index to -2 tells elf_link_output_extsym that
12091 this symbol is used by a reloc. */
12092 h->indx = -2;
12093 *rel_hash_ptr = h;
12094 indx = 0;
12095 }
12096 else
12097 {
12098 (*info->callbacks->unattached_reloc)
12099 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12100 indx = 0;
12101 }
12102 }
12103
12104 /* If this is an inplace reloc, we must write the addend into the
12105 object file. */
12106 if (howto->partial_inplace && addend != 0)
12107 {
12108 bfd_size_type size;
12109 bfd_reloc_status_type rstat;
12110 bfd_byte *buf;
12111 bool ok;
12112 const char *sym_name;
12113 bfd_size_type octets;
12114
12115 size = (bfd_size_type) bfd_get_reloc_size (howto);
12116 buf = (bfd_byte *) bfd_zmalloc (size);
12117 if (buf == NULL && size != 0)
12118 return false;
12119 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12120 switch (rstat)
12121 {
12122 case bfd_reloc_ok:
12123 break;
12124
12125 default:
12126 case bfd_reloc_outofrange:
12127 abort ();
12128
12129 case bfd_reloc_overflow:
12130 if (link_order->type == bfd_section_reloc_link_order)
12131 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12132 else
12133 sym_name = link_order->u.reloc.p->u.name;
12134 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12135 howto->name, addend, NULL, NULL,
12136 (bfd_vma) 0);
12137 break;
12138 }
12139
12140 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12141 output_section);
12142 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12143 octets, size);
12144 free (buf);
12145 if (! ok)
12146 return false;
12147 }
12148
12149 /* The address of a reloc is relative to the section in a
12150 relocatable file, and is a virtual address in an executable
12151 file. */
12152 offset = link_order->offset;
12153 if (! bfd_link_relocatable (info))
12154 offset += output_section->vma;
12155
12156 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12157 {
12158 irel[i].r_offset = offset;
12159 irel[i].r_info = 0;
12160 irel[i].r_addend = 0;
12161 }
12162 if (bed->s->arch_size == 32)
12163 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12164 else
12165 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12166
12167 rel_hdr = reldata->hdr;
12168 erel = rel_hdr->contents;
12169 if (rel_hdr->sh_type == SHT_REL)
12170 {
12171 erel += reldata->count * bed->s->sizeof_rel;
12172 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12173 }
12174 else
12175 {
12176 irel[0].r_addend = addend;
12177 erel += reldata->count * bed->s->sizeof_rela;
12178 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12179 }
12180
12181 ++reldata->count;
12182
12183 return true;
12184 }
12185
12186 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12187 Returns TRUE upon success, FALSE otherwise. */
12188
12189 static bool
12190 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12191 {
12192 bool ret = false;
12193 bfd *implib_bfd;
12194 const struct elf_backend_data *bed;
12195 flagword flags;
12196 enum bfd_architecture arch;
12197 unsigned int mach;
12198 asymbol **sympp = NULL;
12199 long symsize;
12200 long symcount;
12201 long src_count;
12202 elf_symbol_type *osymbuf;
12203 size_t amt;
12204
12205 implib_bfd = info->out_implib_bfd;
12206 bed = get_elf_backend_data (abfd);
12207
12208 if (!bfd_set_format (implib_bfd, bfd_object))
12209 return false;
12210
12211 /* Use flag from executable but make it a relocatable object. */
12212 flags = bfd_get_file_flags (abfd);
12213 flags &= ~HAS_RELOC;
12214 if (!bfd_set_start_address (implib_bfd, 0)
12215 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12216 return false;
12217
12218 /* Copy architecture of output file to import library file. */
12219 arch = bfd_get_arch (abfd);
12220 mach = bfd_get_mach (abfd);
12221 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12222 && (abfd->target_defaulted
12223 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12224 return false;
12225
12226 /* Get symbol table size. */
12227 symsize = bfd_get_symtab_upper_bound (abfd);
12228 if (symsize < 0)
12229 return false;
12230
12231 /* Read in the symbol table. */
12232 sympp = (asymbol **) bfd_malloc (symsize);
12233 if (sympp == NULL)
12234 return false;
12235
12236 symcount = bfd_canonicalize_symtab (abfd, sympp);
12237 if (symcount < 0)
12238 goto free_sym_buf;
12239
12240 /* Allow the BFD backend to copy any private header data it
12241 understands from the output BFD to the import library BFD. */
12242 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12243 goto free_sym_buf;
12244
12245 /* Filter symbols to appear in the import library. */
12246 if (bed->elf_backend_filter_implib_symbols)
12247 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12248 symcount);
12249 else
12250 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12251 if (symcount == 0)
12252 {
12253 bfd_set_error (bfd_error_no_symbols);
12254 _bfd_error_handler (_("%pB: no symbol found for import library"),
12255 implib_bfd);
12256 goto free_sym_buf;
12257 }
12258
12259
12260 /* Make symbols absolute. */
12261 amt = symcount * sizeof (*osymbuf);
12262 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12263 if (osymbuf == NULL)
12264 goto free_sym_buf;
12265
12266 for (src_count = 0; src_count < symcount; src_count++)
12267 {
12268 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12269 sizeof (*osymbuf));
12270 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12271 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12272 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12273 osymbuf[src_count].internal_elf_sym.st_value =
12274 osymbuf[src_count].symbol.value;
12275 sympp[src_count] = &osymbuf[src_count].symbol;
12276 }
12277
12278 bfd_set_symtab (implib_bfd, sympp, symcount);
12279
12280 /* Allow the BFD backend to copy any private data it understands
12281 from the output BFD to the import library BFD. This is done last
12282 to permit the routine to look at the filtered symbol table. */
12283 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12284 goto free_sym_buf;
12285
12286 if (!bfd_close (implib_bfd))
12287 goto free_sym_buf;
12288
12289 ret = true;
12290
12291 free_sym_buf:
12292 free (sympp);
12293 return ret;
12294 }
12295
12296 static void
12297 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12298 {
12299 asection *o;
12300
12301 if (flinfo->symstrtab != NULL)
12302 _bfd_elf_strtab_free (flinfo->symstrtab);
12303 free (flinfo->contents);
12304 free (flinfo->external_relocs);
12305 free (flinfo->internal_relocs);
12306 free (flinfo->external_syms);
12307 free (flinfo->locsym_shndx);
12308 free (flinfo->internal_syms);
12309 free (flinfo->indices);
12310 free (flinfo->sections);
12311 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12312 free (flinfo->symshndxbuf);
12313 for (o = obfd->sections; o != NULL; o = o->next)
12314 {
12315 struct bfd_elf_section_data *esdo = elf_section_data (o);
12316 free (esdo->rel.hashes);
12317 free (esdo->rela.hashes);
12318 }
12319 }
12320
12321 /* Do the final step of an ELF link. */
12322
12323 bool
12324 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12325 {
12326 bool dynamic;
12327 bool emit_relocs;
12328 bfd *dynobj;
12329 struct elf_final_link_info flinfo;
12330 asection *o;
12331 struct bfd_link_order *p;
12332 bfd *sub;
12333 bfd_size_type max_contents_size;
12334 bfd_size_type max_external_reloc_size;
12335 bfd_size_type max_internal_reloc_count;
12336 bfd_size_type max_sym_count;
12337 bfd_size_type max_sym_shndx_count;
12338 Elf_Internal_Sym elfsym;
12339 unsigned int i;
12340 Elf_Internal_Shdr *symtab_hdr;
12341 Elf_Internal_Shdr *symtab_shndx_hdr;
12342 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12343 struct elf_outext_info eoinfo;
12344 bool merged;
12345 size_t relativecount;
12346 size_t relr_entsize;
12347 asection *reldyn = 0;
12348 bfd_size_type amt;
12349 asection *attr_section = NULL;
12350 bfd_vma attr_size = 0;
12351 const char *std_attrs_section;
12352 struct elf_link_hash_table *htab = elf_hash_table (info);
12353 bool sections_removed;
12354 bool ret;
12355
12356 if (!is_elf_hash_table (&htab->root))
12357 return false;
12358
12359 if (bfd_link_pic (info))
12360 abfd->flags |= DYNAMIC;
12361
12362 dynamic = htab->dynamic_sections_created;
12363 dynobj = htab->dynobj;
12364
12365 emit_relocs = (bfd_link_relocatable (info)
12366 || info->emitrelocations);
12367
12368 memset (&flinfo, 0, sizeof (flinfo));
12369 flinfo.info = info;
12370 flinfo.output_bfd = abfd;
12371 flinfo.symstrtab = _bfd_elf_strtab_init ();
12372 if (flinfo.symstrtab == NULL)
12373 return false;
12374
12375 if (! dynamic)
12376 {
12377 flinfo.hash_sec = NULL;
12378 flinfo.symver_sec = NULL;
12379 }
12380 else
12381 {
12382 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12383 /* Note that dynsym_sec can be NULL (on VMS). */
12384 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12385 /* Note that it is OK if symver_sec is NULL. */
12386 }
12387
12388 if (info->unique_symbol
12389 && !bfd_hash_table_init (&flinfo.local_hash_table,
12390 local_hash_newfunc,
12391 sizeof (struct local_hash_entry)))
12392 return false;
12393
12394 /* The object attributes have been merged. Remove the input
12395 sections from the link, and set the contents of the output
12396 section. */
12397 sections_removed = false;
12398 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12399 for (o = abfd->sections; o != NULL; o = o->next)
12400 {
12401 bool remove_section = false;
12402
12403 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12404 || strcmp (o->name, ".gnu.attributes") == 0)
12405 {
12406 for (p = o->map_head.link_order; p != NULL; p = p->next)
12407 {
12408 asection *input_section;
12409
12410 if (p->type != bfd_indirect_link_order)
12411 continue;
12412 input_section = p->u.indirect.section;
12413 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12414 elf_link_input_bfd ignores this section. */
12415 input_section->flags &= ~SEC_HAS_CONTENTS;
12416 }
12417
12418 attr_size = bfd_elf_obj_attr_size (abfd);
12419 bfd_set_section_size (o, attr_size);
12420 /* Skip this section later on. */
12421 o->map_head.link_order = NULL;
12422 if (attr_size)
12423 attr_section = o;
12424 else
12425 remove_section = true;
12426 }
12427 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12428 {
12429 /* Remove empty group section from linker output. */
12430 remove_section = true;
12431 }
12432 if (remove_section)
12433 {
12434 o->flags |= SEC_EXCLUDE;
12435 bfd_section_list_remove (abfd, o);
12436 abfd->section_count--;
12437 sections_removed = true;
12438 }
12439 }
12440 if (sections_removed)
12441 _bfd_fix_excluded_sec_syms (abfd, info);
12442
12443 /* Count up the number of relocations we will output for each output
12444 section, so that we know the sizes of the reloc sections. We
12445 also figure out some maximum sizes. */
12446 max_contents_size = 0;
12447 max_external_reloc_size = 0;
12448 max_internal_reloc_count = 0;
12449 max_sym_count = 0;
12450 max_sym_shndx_count = 0;
12451 merged = false;
12452 for (o = abfd->sections; o != NULL; o = o->next)
12453 {
12454 struct bfd_elf_section_data *esdo = elf_section_data (o);
12455 o->reloc_count = 0;
12456
12457 for (p = o->map_head.link_order; p != NULL; p = p->next)
12458 {
12459 unsigned int reloc_count = 0;
12460 unsigned int additional_reloc_count = 0;
12461 struct bfd_elf_section_data *esdi = NULL;
12462
12463 if (p->type == bfd_section_reloc_link_order
12464 || p->type == bfd_symbol_reloc_link_order)
12465 reloc_count = 1;
12466 else if (p->type == bfd_indirect_link_order)
12467 {
12468 asection *sec;
12469
12470 sec = p->u.indirect.section;
12471
12472 /* Mark all sections which are to be included in the
12473 link. This will normally be every section. We need
12474 to do this so that we can identify any sections which
12475 the linker has decided to not include. */
12476 sec->linker_mark = true;
12477
12478 if (sec->flags & SEC_MERGE)
12479 merged = true;
12480
12481 if (sec->rawsize > max_contents_size)
12482 max_contents_size = sec->rawsize;
12483 if (sec->size > max_contents_size)
12484 max_contents_size = sec->size;
12485
12486 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12487 && (sec->owner->flags & DYNAMIC) == 0)
12488 {
12489 size_t sym_count;
12490
12491 /* We are interested in just local symbols, not all
12492 symbols. */
12493 if (elf_bad_symtab (sec->owner))
12494 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12495 / bed->s->sizeof_sym);
12496 else
12497 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12498
12499 if (sym_count > max_sym_count)
12500 max_sym_count = sym_count;
12501
12502 if (sym_count > max_sym_shndx_count
12503 && elf_symtab_shndx_list (sec->owner) != NULL)
12504 max_sym_shndx_count = sym_count;
12505
12506 esdi = elf_section_data (sec);
12507
12508 if (esdi->this_hdr.sh_type == SHT_REL
12509 || esdi->this_hdr.sh_type == SHT_RELA)
12510 /* Some backends use reloc_count in relocation sections
12511 to count particular types of relocs. Of course,
12512 reloc sections themselves can't have relocations. */
12513 ;
12514 else if (emit_relocs)
12515 {
12516 reloc_count = sec->reloc_count;
12517 if (bed->elf_backend_count_additional_relocs)
12518 {
12519 int c;
12520 c = (*bed->elf_backend_count_additional_relocs) (sec);
12521 additional_reloc_count += c;
12522 }
12523 }
12524 else if (bed->elf_backend_count_relocs)
12525 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12526
12527 if ((sec->flags & SEC_RELOC) != 0)
12528 {
12529 size_t ext_size = 0;
12530
12531 if (esdi->rel.hdr != NULL)
12532 ext_size = esdi->rel.hdr->sh_size;
12533 if (esdi->rela.hdr != NULL)
12534 ext_size += esdi->rela.hdr->sh_size;
12535
12536 if (ext_size > max_external_reloc_size)
12537 max_external_reloc_size = ext_size;
12538 if (sec->reloc_count > max_internal_reloc_count)
12539 max_internal_reloc_count = sec->reloc_count;
12540 }
12541 }
12542 }
12543
12544 if (reloc_count == 0)
12545 continue;
12546
12547 reloc_count += additional_reloc_count;
12548 o->reloc_count += reloc_count;
12549
12550 if (p->type == bfd_indirect_link_order && emit_relocs)
12551 {
12552 if (esdi->rel.hdr)
12553 {
12554 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12555 esdo->rel.count += additional_reloc_count;
12556 }
12557 if (esdi->rela.hdr)
12558 {
12559 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12560 esdo->rela.count += additional_reloc_count;
12561 }
12562 }
12563 else
12564 {
12565 if (o->use_rela_p)
12566 esdo->rela.count += reloc_count;
12567 else
12568 esdo->rel.count += reloc_count;
12569 }
12570 }
12571
12572 if (o->reloc_count > 0)
12573 o->flags |= SEC_RELOC;
12574 else
12575 {
12576 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12577 set it (this is probably a bug) and if it is set
12578 assign_section_numbers will create a reloc section. */
12579 o->flags &=~ SEC_RELOC;
12580 }
12581
12582 /* If the SEC_ALLOC flag is not set, force the section VMA to
12583 zero. This is done in elf_fake_sections as well, but forcing
12584 the VMA to 0 here will ensure that relocs against these
12585 sections are handled correctly. */
12586 if ((o->flags & SEC_ALLOC) == 0
12587 && ! o->user_set_vma)
12588 o->vma = 0;
12589 }
12590
12591 if (! bfd_link_relocatable (info) && merged)
12592 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12593
12594 /* Figure out the file positions for everything but the symbol table
12595 and the relocs. We set symcount to force assign_section_numbers
12596 to create a symbol table. */
12597 abfd->symcount = info->strip != strip_all || emit_relocs;
12598 BFD_ASSERT (! abfd->output_has_begun);
12599 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12600 goto error_return;
12601
12602 /* Set sizes, and assign file positions for reloc sections. */
12603 for (o = abfd->sections; o != NULL; o = o->next)
12604 {
12605 struct bfd_elf_section_data *esdo = elf_section_data (o);
12606 if ((o->flags & SEC_RELOC) != 0)
12607 {
12608 if (esdo->rel.hdr
12609 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12610 goto error_return;
12611
12612 if (esdo->rela.hdr
12613 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12614 goto error_return;
12615 }
12616
12617 /* _bfd_elf_compute_section_file_positions makes temporary use
12618 of target_index. Reset it. */
12619 o->target_index = 0;
12620
12621 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12622 to count upwards while actually outputting the relocations. */
12623 esdo->rel.count = 0;
12624 esdo->rela.count = 0;
12625
12626 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12627 && !bfd_section_is_ctf (o))
12628 {
12629 /* Cache the section contents so that they can be compressed
12630 later. Use bfd_malloc since it will be freed by
12631 bfd_compress_section_contents. */
12632 unsigned char *contents = esdo->this_hdr.contents;
12633 if (contents != NULL)
12634 abort ();
12635 contents
12636 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12637 if (contents == NULL)
12638 goto error_return;
12639 esdo->this_hdr.contents = contents;
12640 }
12641 }
12642
12643 /* We have now assigned file positions for all the sections except .symtab,
12644 .strtab, and non-loaded reloc and compressed debugging sections. We start
12645 the .symtab section at the current file position, and write directly to it.
12646 We build the .strtab section in memory. */
12647 abfd->symcount = 0;
12648 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12649 /* sh_name is set in prep_headers. */
12650 symtab_hdr->sh_type = SHT_SYMTAB;
12651 /* sh_flags, sh_addr and sh_size all start off zero. */
12652 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12653 /* sh_link is set in assign_section_numbers. */
12654 /* sh_info is set below. */
12655 /* sh_offset is set just below. */
12656 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12657
12658 if (max_sym_count < 20)
12659 max_sym_count = 20;
12660 htab->strtabsize = max_sym_count;
12661 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12662 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12663 if (htab->strtab == NULL)
12664 goto error_return;
12665 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12666 flinfo.symshndxbuf
12667 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12668 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12669
12670 if (info->strip != strip_all || emit_relocs)
12671 {
12672 file_ptr off = elf_next_file_pos (abfd);
12673
12674 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12675
12676 /* Note that at this point elf_next_file_pos (abfd) is
12677 incorrect. We do not yet know the size of the .symtab section.
12678 We correct next_file_pos below, after we do know the size. */
12679
12680 /* Start writing out the symbol table. The first symbol is always a
12681 dummy symbol. */
12682 elfsym.st_value = 0;
12683 elfsym.st_size = 0;
12684 elfsym.st_info = 0;
12685 elfsym.st_other = 0;
12686 elfsym.st_shndx = SHN_UNDEF;
12687 elfsym.st_target_internal = 0;
12688 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12689 bfd_und_section_ptr, NULL) != 1)
12690 goto error_return;
12691
12692 /* Output a symbol for each section if asked or they are used for
12693 relocs. These symbols usually have no names. We store the
12694 index of each one in the index field of the section, so that
12695 we can find it again when outputting relocs. */
12696
12697 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12698 {
12699 bool name_local_sections
12700 = (bed->elf_backend_name_local_section_symbols
12701 && bed->elf_backend_name_local_section_symbols (abfd));
12702 const char *name = NULL;
12703
12704 elfsym.st_size = 0;
12705 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12706 elfsym.st_other = 0;
12707 elfsym.st_value = 0;
12708 elfsym.st_target_internal = 0;
12709 for (i = 1; i < elf_numsections (abfd); i++)
12710 {
12711 o = bfd_section_from_elf_index (abfd, i);
12712 if (o != NULL)
12713 {
12714 o->target_index = bfd_get_symcount (abfd);
12715 elfsym.st_shndx = i;
12716 if (!bfd_link_relocatable (info))
12717 elfsym.st_value = o->vma;
12718 if (name_local_sections)
12719 name = o->name;
12720 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12721 NULL) != 1)
12722 goto error_return;
12723 }
12724 }
12725 }
12726 }
12727
12728 /* On some targets like Irix 5 the symbol split between local and global
12729 ones recorded in the sh_info field needs to be done between section
12730 and all other symbols. */
12731 if (bed->elf_backend_elfsym_local_is_section
12732 && bed->elf_backend_elfsym_local_is_section (abfd))
12733 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12734
12735 /* Allocate some memory to hold information read in from the input
12736 files. */
12737 if (max_contents_size != 0)
12738 {
12739 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12740 if (flinfo.contents == NULL)
12741 goto error_return;
12742 }
12743
12744 if (max_external_reloc_size != 0)
12745 {
12746 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12747 if (flinfo.external_relocs == NULL)
12748 goto error_return;
12749 }
12750
12751 if (max_internal_reloc_count != 0)
12752 {
12753 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12754 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12755 if (flinfo.internal_relocs == NULL)
12756 goto error_return;
12757 }
12758
12759 if (max_sym_count != 0)
12760 {
12761 amt = max_sym_count * bed->s->sizeof_sym;
12762 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12763 if (flinfo.external_syms == NULL)
12764 goto error_return;
12765
12766 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12767 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12768 if (flinfo.internal_syms == NULL)
12769 goto error_return;
12770
12771 amt = max_sym_count * sizeof (long);
12772 flinfo.indices = (long int *) bfd_malloc (amt);
12773 if (flinfo.indices == NULL)
12774 goto error_return;
12775
12776 amt = max_sym_count * sizeof (asection *);
12777 flinfo.sections = (asection **) bfd_malloc (amt);
12778 if (flinfo.sections == NULL)
12779 goto error_return;
12780 }
12781
12782 if (max_sym_shndx_count != 0)
12783 {
12784 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12785 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12786 if (flinfo.locsym_shndx == NULL)
12787 goto error_return;
12788 }
12789
12790 if (htab->tls_sec)
12791 {
12792 bfd_vma base, end = 0; /* Both bytes. */
12793 asection *sec;
12794
12795 for (sec = htab->tls_sec;
12796 sec && (sec->flags & SEC_THREAD_LOCAL);
12797 sec = sec->next)
12798 {
12799 bfd_size_type size = sec->size;
12800 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12801
12802 if (size == 0
12803 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12804 {
12805 struct bfd_link_order *ord = sec->map_tail.link_order;
12806
12807 if (ord != NULL)
12808 size = ord->offset * opb + ord->size;
12809 }
12810 end = sec->vma + size / opb;
12811 }
12812 base = htab->tls_sec->vma;
12813 /* Only align end of TLS section if static TLS doesn't have special
12814 alignment requirements. */
12815 if (bed->static_tls_alignment == 1)
12816 end = align_power (end, htab->tls_sec->alignment_power);
12817 htab->tls_size = end - base;
12818 }
12819
12820 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12821 return false;
12822
12823 /* Finish relative relocations here after regular symbol processing
12824 is finished if DT_RELR is enabled. */
12825 if (info->enable_dt_relr
12826 && bed->finish_relative_relocs
12827 && !bed->finish_relative_relocs (info))
12828 info->callbacks->einfo
12829 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12830
12831 /* Since ELF permits relocations to be against local symbols, we
12832 must have the local symbols available when we do the relocations.
12833 Since we would rather only read the local symbols once, and we
12834 would rather not keep them in memory, we handle all the
12835 relocations for a single input file at the same time.
12836
12837 Unfortunately, there is no way to know the total number of local
12838 symbols until we have seen all of them, and the local symbol
12839 indices precede the global symbol indices. This means that when
12840 we are generating relocatable output, and we see a reloc against
12841 a global symbol, we can not know the symbol index until we have
12842 finished examining all the local symbols to see which ones we are
12843 going to output. To deal with this, we keep the relocations in
12844 memory, and don't output them until the end of the link. This is
12845 an unfortunate waste of memory, but I don't see a good way around
12846 it. Fortunately, it only happens when performing a relocatable
12847 link, which is not the common case. FIXME: If keep_memory is set
12848 we could write the relocs out and then read them again; I don't
12849 know how bad the memory loss will be. */
12850
12851 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12852 sub->output_has_begun = false;
12853 for (o = abfd->sections; o != NULL; o = o->next)
12854 {
12855 for (p = o->map_head.link_order; p != NULL; p = p->next)
12856 {
12857 if (p->type == bfd_indirect_link_order
12858 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12859 == bfd_target_elf_flavour)
12860 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12861 {
12862 if (! sub->output_has_begun)
12863 {
12864 if (! elf_link_input_bfd (&flinfo, sub))
12865 goto error_return;
12866 sub->output_has_begun = true;
12867 }
12868 }
12869 else if (p->type == bfd_section_reloc_link_order
12870 || p->type == bfd_symbol_reloc_link_order)
12871 {
12872 if (! elf_reloc_link_order (abfd, info, o, p))
12873 goto error_return;
12874 }
12875 else
12876 {
12877 if (! _bfd_default_link_order (abfd, info, o, p))
12878 {
12879 if (p->type == bfd_indirect_link_order
12880 && (bfd_get_flavour (sub)
12881 == bfd_target_elf_flavour)
12882 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12883 != bed->s->elfclass))
12884 {
12885 const char *iclass, *oclass;
12886
12887 switch (bed->s->elfclass)
12888 {
12889 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12890 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12891 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12892 default: abort ();
12893 }
12894
12895 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12896 {
12897 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12898 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12899 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12900 default: abort ();
12901 }
12902
12903 bfd_set_error (bfd_error_wrong_format);
12904 _bfd_error_handler
12905 /* xgettext:c-format */
12906 (_("%pB: file class %s incompatible with %s"),
12907 sub, iclass, oclass);
12908 }
12909
12910 goto error_return;
12911 }
12912 }
12913 }
12914 }
12915
12916 /* Free symbol buffer if needed. */
12917 if (!info->reduce_memory_overheads)
12918 {
12919 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12920 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12921 {
12922 free (elf_tdata (sub)->symbuf);
12923 elf_tdata (sub)->symbuf = NULL;
12924 }
12925 }
12926
12927 ret = true;
12928
12929 /* Output any global symbols that got converted to local in a
12930 version script or due to symbol visibility. We do this in a
12931 separate step since ELF requires all local symbols to appear
12932 prior to any global symbols. FIXME: We should only do this if
12933 some global symbols were, in fact, converted to become local.
12934 FIXME: Will this work correctly with the Irix 5 linker? */
12935 eoinfo.failed = false;
12936 eoinfo.flinfo = &flinfo;
12937 eoinfo.localsyms = true;
12938 eoinfo.file_sym_done = false;
12939 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12940 if (eoinfo.failed)
12941 {
12942 ret = false;
12943 goto return_local_hash_table;
12944 }
12945
12946 /* If backend needs to output some local symbols not present in the hash
12947 table, do it now. */
12948 if (bed->elf_backend_output_arch_local_syms)
12949 {
12950 if (! ((*bed->elf_backend_output_arch_local_syms)
12951 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12952 {
12953 ret = false;
12954 goto return_local_hash_table;
12955 }
12956 }
12957
12958 /* That wrote out all the local symbols. Finish up the symbol table
12959 with the global symbols. Even if we want to strip everything we
12960 can, we still need to deal with those global symbols that got
12961 converted to local in a version script. */
12962
12963 /* The sh_info field records the index of the first non local symbol. */
12964 if (!symtab_hdr->sh_info)
12965 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12966
12967 if (dynamic
12968 && htab->dynsym != NULL
12969 && htab->dynsym->output_section != bfd_abs_section_ptr)
12970 {
12971 Elf_Internal_Sym sym;
12972 bfd_byte *dynsym = htab->dynsym->contents;
12973
12974 o = htab->dynsym->output_section;
12975 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12976
12977 /* Write out the section symbols for the output sections. */
12978 if (bfd_link_pic (info)
12979 || htab->is_relocatable_executable)
12980 {
12981 asection *s;
12982
12983 sym.st_size = 0;
12984 sym.st_name = 0;
12985 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12986 sym.st_other = 0;
12987 sym.st_target_internal = 0;
12988
12989 for (s = abfd->sections; s != NULL; s = s->next)
12990 {
12991 int indx;
12992 bfd_byte *dest;
12993 long dynindx;
12994
12995 dynindx = elf_section_data (s)->dynindx;
12996 if (dynindx <= 0)
12997 continue;
12998 indx = elf_section_data (s)->this_idx;
12999 BFD_ASSERT (indx > 0);
13000 sym.st_shndx = indx;
13001 if (! check_dynsym (abfd, &sym))
13002 {
13003 ret = false;
13004 goto return_local_hash_table;
13005 }
13006 sym.st_value = s->vma;
13007 dest = dynsym + dynindx * bed->s->sizeof_sym;
13008
13009 /* Inform the linker of the addition of this symbol. */
13010
13011 if (info->callbacks->ctf_new_dynsym)
13012 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13013
13014 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13015 }
13016 }
13017
13018 /* Write out the local dynsyms. */
13019 if (htab->dynlocal)
13020 {
13021 struct elf_link_local_dynamic_entry *e;
13022 for (e = htab->dynlocal; e ; e = e->next)
13023 {
13024 asection *s;
13025 bfd_byte *dest;
13026
13027 /* Copy the internal symbol and turn off visibility.
13028 Note that we saved a word of storage and overwrote
13029 the original st_name with the dynstr_index. */
13030 sym = e->isym;
13031 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13032 sym.st_shndx = SHN_UNDEF;
13033
13034 s = bfd_section_from_elf_index (e->input_bfd,
13035 e->isym.st_shndx);
13036 if (s != NULL
13037 && s->output_section != NULL
13038 && elf_section_data (s->output_section) != NULL)
13039 {
13040 sym.st_shndx =
13041 elf_section_data (s->output_section)->this_idx;
13042 if (! check_dynsym (abfd, &sym))
13043 {
13044 ret = false;
13045 goto return_local_hash_table;
13046 }
13047 sym.st_value = (s->output_section->vma
13048 + s->output_offset
13049 + e->isym.st_value);
13050 }
13051
13052 /* Inform the linker of the addition of this symbol. */
13053
13054 if (info->callbacks->ctf_new_dynsym)
13055 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13056
13057 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13058 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13059 }
13060 }
13061 }
13062
13063 /* We get the global symbols from the hash table. */
13064 eoinfo.failed = false;
13065 eoinfo.localsyms = false;
13066 eoinfo.flinfo = &flinfo;
13067 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13068 if (eoinfo.failed)
13069 {
13070 ret = false;
13071 goto return_local_hash_table;
13072 }
13073
13074 /* If backend needs to output some symbols not present in the hash
13075 table, do it now. */
13076 if (bed->elf_backend_output_arch_syms
13077 && (info->strip != strip_all || emit_relocs))
13078 {
13079 if (! ((*bed->elf_backend_output_arch_syms)
13080 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13081 {
13082 ret = false;
13083 goto return_local_hash_table;
13084 }
13085 }
13086
13087 /* Finalize the .strtab section. */
13088 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13089
13090 /* Swap out the .strtab section. */
13091 if (!elf_link_swap_symbols_out (&flinfo))
13092 {
13093 ret = false;
13094 goto return_local_hash_table;
13095 }
13096
13097 /* Now we know the size of the symtab section. */
13098 if (bfd_get_symcount (abfd) > 0)
13099 {
13100 /* Finish up and write out the symbol string table (.strtab)
13101 section. */
13102 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13103 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13104
13105 if (elf_symtab_shndx_list (abfd))
13106 {
13107 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13108
13109 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13110 {
13111 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13112 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13113 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13114 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13115 symtab_shndx_hdr->sh_size = amt;
13116
13117 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13118 off, true);
13119
13120 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13121 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13122 {
13123 ret = false;
13124 goto return_local_hash_table;
13125 }
13126 }
13127 }
13128
13129 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13130 /* sh_name was set in prep_headers. */
13131 symstrtab_hdr->sh_type = SHT_STRTAB;
13132 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13133 symstrtab_hdr->sh_addr = 0;
13134 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13135 symstrtab_hdr->sh_entsize = 0;
13136 symstrtab_hdr->sh_link = 0;
13137 symstrtab_hdr->sh_info = 0;
13138 /* sh_offset is set just below. */
13139 symstrtab_hdr->sh_addralign = 1;
13140
13141 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13142 off, true);
13143 elf_next_file_pos (abfd) = off;
13144
13145 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13146 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13147 {
13148 ret = false;
13149 goto return_local_hash_table;
13150 }
13151 }
13152
13153 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13154 {
13155 _bfd_error_handler (_("%pB: failed to generate import library"),
13156 info->out_implib_bfd);
13157 ret = false;
13158 goto return_local_hash_table;
13159 }
13160
13161 /* Adjust the relocs to have the correct symbol indices. */
13162 for (o = abfd->sections; o != NULL; o = o->next)
13163 {
13164 struct bfd_elf_section_data *esdo = elf_section_data (o);
13165 bool sort;
13166
13167 if ((o->flags & SEC_RELOC) == 0)
13168 continue;
13169
13170 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13171 if (esdo->rel.hdr != NULL
13172 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13173 {
13174 ret = false;
13175 goto return_local_hash_table;
13176 }
13177 if (esdo->rela.hdr != NULL
13178 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13179 {
13180 ret = false;
13181 goto return_local_hash_table;
13182 }
13183
13184 /* Set the reloc_count field to 0 to prevent write_relocs from
13185 trying to swap the relocs out itself. */
13186 o->reloc_count = 0;
13187 }
13188
13189 relativecount = 0;
13190 if (dynamic && info->combreloc && dynobj != NULL)
13191 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13192
13193 relr_entsize = 0;
13194 if (htab->srelrdyn != NULL
13195 && htab->srelrdyn->output_section != NULL
13196 && htab->srelrdyn->size != 0)
13197 {
13198 asection *s = htab->srelrdyn->output_section;
13199 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13200 if (relr_entsize == 0)
13201 {
13202 relr_entsize = bed->s->arch_size / 8;
13203 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13204 }
13205 }
13206
13207 /* If we are linking against a dynamic object, or generating a
13208 shared library, finish up the dynamic linking information. */
13209 if (dynamic)
13210 {
13211 bfd_byte *dyncon, *dynconend;
13212
13213 /* Fix up .dynamic entries. */
13214 o = bfd_get_linker_section (dynobj, ".dynamic");
13215 BFD_ASSERT (o != NULL);
13216
13217 dyncon = o->contents;
13218 dynconend = PTR_ADD (o->contents, o->size);
13219 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13220 {
13221 Elf_Internal_Dyn dyn;
13222 const char *name;
13223 unsigned int type;
13224 bfd_size_type sh_size;
13225 bfd_vma sh_addr;
13226
13227 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13228
13229 switch (dyn.d_tag)
13230 {
13231 default:
13232 continue;
13233 case DT_NULL:
13234 if (relativecount != 0)
13235 {
13236 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13237 {
13238 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13239 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13240 }
13241 if (dyn.d_tag != DT_NULL
13242 && dynconend - dyncon >= bed->s->sizeof_dyn)
13243 {
13244 dyn.d_un.d_val = relativecount;
13245 relativecount = 0;
13246 break;
13247 }
13248 relativecount = 0;
13249 }
13250 if (relr_entsize != 0)
13251 {
13252 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13253 {
13254 asection *s = htab->srelrdyn;
13255 dyn.d_tag = DT_RELR;
13256 dyn.d_un.d_ptr
13257 = s->output_section->vma + s->output_offset;
13258 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13259 dyncon += bed->s->sizeof_dyn;
13260
13261 dyn.d_tag = DT_RELRSZ;
13262 dyn.d_un.d_val = s->size;
13263 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13264 dyncon += bed->s->sizeof_dyn;
13265
13266 dyn.d_tag = DT_RELRENT;
13267 dyn.d_un.d_val = relr_entsize;
13268 relr_entsize = 0;
13269 break;
13270 }
13271 relr_entsize = 0;
13272 }
13273 continue;
13274
13275 case DT_INIT:
13276 name = info->init_function;
13277 goto get_sym;
13278 case DT_FINI:
13279 name = info->fini_function;
13280 get_sym:
13281 {
13282 struct elf_link_hash_entry *h;
13283
13284 h = elf_link_hash_lookup (htab, name, false, false, true);
13285 if (h != NULL
13286 && (h->root.type == bfd_link_hash_defined
13287 || h->root.type == bfd_link_hash_defweak))
13288 {
13289 dyn.d_un.d_ptr = h->root.u.def.value;
13290 o = h->root.u.def.section;
13291 if (o->output_section != NULL)
13292 dyn.d_un.d_ptr += (o->output_section->vma
13293 + o->output_offset);
13294 else
13295 {
13296 /* The symbol is imported from another shared
13297 library and does not apply to this one. */
13298 dyn.d_un.d_ptr = 0;
13299 }
13300 break;
13301 }
13302 }
13303 continue;
13304
13305 case DT_PREINIT_ARRAYSZ:
13306 name = ".preinit_array";
13307 goto get_out_size;
13308 case DT_INIT_ARRAYSZ:
13309 name = ".init_array";
13310 goto get_out_size;
13311 case DT_FINI_ARRAYSZ:
13312 name = ".fini_array";
13313 get_out_size:
13314 o = bfd_get_section_by_name (abfd, name);
13315 if (o == NULL)
13316 {
13317 _bfd_error_handler
13318 (_("could not find section %s"), name);
13319 goto error_return;
13320 }
13321 if (o->size == 0)
13322 _bfd_error_handler
13323 (_("warning: %s section has zero size"), name);
13324 dyn.d_un.d_val = o->size;
13325 break;
13326
13327 case DT_PREINIT_ARRAY:
13328 name = ".preinit_array";
13329 goto get_out_vma;
13330 case DT_INIT_ARRAY:
13331 name = ".init_array";
13332 goto get_out_vma;
13333 case DT_FINI_ARRAY:
13334 name = ".fini_array";
13335 get_out_vma:
13336 o = bfd_get_section_by_name (abfd, name);
13337 goto do_vma;
13338
13339 case DT_HASH:
13340 name = ".hash";
13341 goto get_vma;
13342 case DT_GNU_HASH:
13343 name = ".gnu.hash";
13344 goto get_vma;
13345 case DT_STRTAB:
13346 name = ".dynstr";
13347 goto get_vma;
13348 case DT_SYMTAB:
13349 name = ".dynsym";
13350 goto get_vma;
13351 case DT_VERDEF:
13352 name = ".gnu.version_d";
13353 goto get_vma;
13354 case DT_VERNEED:
13355 name = ".gnu.version_r";
13356 goto get_vma;
13357 case DT_VERSYM:
13358 name = ".gnu.version";
13359 get_vma:
13360 o = bfd_get_linker_section (dynobj, name);
13361 do_vma:
13362 if (o == NULL || bfd_is_abs_section (o->output_section))
13363 {
13364 _bfd_error_handler
13365 (_("could not find section %s"), name);
13366 goto error_return;
13367 }
13368 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13369 {
13370 _bfd_error_handler
13371 (_("warning: section '%s' is being made into a note"), name);
13372 bfd_set_error (bfd_error_nonrepresentable_section);
13373 goto error_return;
13374 }
13375 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13376 break;
13377
13378 case DT_REL:
13379 case DT_RELA:
13380 case DT_RELSZ:
13381 case DT_RELASZ:
13382 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13383 type = SHT_REL;
13384 else
13385 type = SHT_RELA;
13386 sh_size = 0;
13387 sh_addr = 0;
13388 for (i = 1; i < elf_numsections (abfd); i++)
13389 {
13390 Elf_Internal_Shdr *hdr;
13391
13392 hdr = elf_elfsections (abfd)[i];
13393 if (hdr->sh_type == type
13394 && (hdr->sh_flags & SHF_ALLOC) != 0)
13395 {
13396 sh_size += hdr->sh_size;
13397 if (sh_addr == 0
13398 || sh_addr > hdr->sh_addr)
13399 sh_addr = hdr->sh_addr;
13400 }
13401 }
13402
13403 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13404 {
13405 unsigned int opb = bfd_octets_per_byte (abfd, o);
13406
13407 /* Don't count procedure linkage table relocs in the
13408 overall reloc count. */
13409 sh_size -= htab->srelplt->size;
13410 if (sh_size == 0)
13411 /* If the size is zero, make the address zero too.
13412 This is to avoid a glibc bug. If the backend
13413 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13414 zero, then we'll put DT_RELA at the end of
13415 DT_JMPREL. glibc will interpret the end of
13416 DT_RELA matching the end of DT_JMPREL as the
13417 case where DT_RELA includes DT_JMPREL, and for
13418 LD_BIND_NOW will decide that processing DT_RELA
13419 will process the PLT relocs too. Net result:
13420 No PLT relocs applied. */
13421 sh_addr = 0;
13422
13423 /* If .rela.plt is the first .rela section, exclude
13424 it from DT_RELA. */
13425 else if (sh_addr == (htab->srelplt->output_section->vma
13426 + htab->srelplt->output_offset) * opb)
13427 sh_addr += htab->srelplt->size;
13428 }
13429
13430 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13431 dyn.d_un.d_val = sh_size;
13432 else
13433 dyn.d_un.d_ptr = sh_addr;
13434 break;
13435 }
13436 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13437 }
13438 }
13439
13440 /* If we have created any dynamic sections, then output them. */
13441 if (dynobj != NULL)
13442 {
13443 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13444 goto error_return;
13445
13446 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13447 if (bfd_link_textrel_check (info)
13448 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13449 && o->size != 0)
13450 {
13451 bfd_byte *dyncon, *dynconend;
13452
13453 dyncon = o->contents;
13454 dynconend = o->contents + o->size;
13455 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13456 {
13457 Elf_Internal_Dyn dyn;
13458
13459 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13460
13461 if (dyn.d_tag == DT_TEXTREL)
13462 {
13463 if (info->textrel_check == textrel_check_error)
13464 info->callbacks->einfo
13465 (_("%P%X: read-only segment has dynamic relocations\n"));
13466 else if (bfd_link_dll (info))
13467 info->callbacks->einfo
13468 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13469 else if (bfd_link_pde (info))
13470 info->callbacks->einfo
13471 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13472 else
13473 info->callbacks->einfo
13474 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13475 break;
13476 }
13477 }
13478 }
13479
13480 for (o = dynobj->sections; o != NULL; o = o->next)
13481 {
13482 if ((o->flags & SEC_HAS_CONTENTS) == 0
13483 || o->size == 0
13484 || o->output_section == bfd_abs_section_ptr)
13485 continue;
13486 if ((o->flags & SEC_LINKER_CREATED) == 0)
13487 {
13488 /* At this point, we are only interested in sections
13489 created by _bfd_elf_link_create_dynamic_sections. */
13490 continue;
13491 }
13492 if (htab->stab_info.stabstr == o)
13493 continue;
13494 if (htab->eh_info.hdr_sec == o)
13495 continue;
13496 if (strcmp (o->name, ".dynstr") != 0)
13497 {
13498 bfd_size_type octets = ((file_ptr) o->output_offset
13499 * bfd_octets_per_byte (abfd, o));
13500 if (!bfd_set_section_contents (abfd, o->output_section,
13501 o->contents, octets, o->size))
13502 goto error_return;
13503 }
13504 else
13505 {
13506 /* The contents of the .dynstr section are actually in a
13507 stringtab. */
13508 file_ptr off;
13509
13510 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13511 if (bfd_seek (abfd, off, SEEK_SET) != 0
13512 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13513 goto error_return;
13514 }
13515 }
13516 }
13517
13518 if (!info->resolve_section_groups)
13519 {
13520 bool failed = false;
13521
13522 BFD_ASSERT (bfd_link_relocatable (info));
13523 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13524 if (failed)
13525 goto error_return;
13526 }
13527
13528 /* If we have optimized stabs strings, output them. */
13529 if (htab->stab_info.stabstr != NULL)
13530 {
13531 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13532 goto error_return;
13533 }
13534
13535 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13536 goto error_return;
13537
13538 if (! _bfd_elf_write_section_sframe (abfd, info))
13539 goto error_return;
13540
13541 if (info->callbacks->emit_ctf)
13542 info->callbacks->emit_ctf ();
13543
13544 elf_final_link_free (abfd, &flinfo);
13545
13546 if (attr_section)
13547 {
13548 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13549 if (contents == NULL)
13550 {
13551 /* Bail out and fail. */
13552 ret = false;
13553 goto return_local_hash_table;
13554 }
13555 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13556 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13557 free (contents);
13558 }
13559
13560 return_local_hash_table:
13561 if (info->unique_symbol)
13562 bfd_hash_table_free (&flinfo.local_hash_table);
13563 return ret;
13564
13565 error_return:
13566 elf_final_link_free (abfd, &flinfo);
13567 ret = false;
13568 goto return_local_hash_table;
13569 }
13570 \f
13571 /* Initialize COOKIE for input bfd ABFD. */
13572
13573 static bool
13574 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13575 struct bfd_link_info *info, bfd *abfd)
13576 {
13577 Elf_Internal_Shdr *symtab_hdr;
13578 const struct elf_backend_data *bed;
13579
13580 bed = get_elf_backend_data (abfd);
13581 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13582
13583 cookie->abfd = abfd;
13584 cookie->sym_hashes = elf_sym_hashes (abfd);
13585 cookie->bad_symtab = elf_bad_symtab (abfd);
13586 if (cookie->bad_symtab)
13587 {
13588 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13589 cookie->extsymoff = 0;
13590 }
13591 else
13592 {
13593 cookie->locsymcount = symtab_hdr->sh_info;
13594 cookie->extsymoff = symtab_hdr->sh_info;
13595 }
13596
13597 if (bed->s->arch_size == 32)
13598 cookie->r_sym_shift = 8;
13599 else
13600 cookie->r_sym_shift = 32;
13601
13602 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13603 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13604 {
13605 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13606 cookie->locsymcount, 0,
13607 NULL, NULL, NULL);
13608 if (cookie->locsyms == NULL)
13609 {
13610 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13611 return false;
13612 }
13613 if (_bfd_link_keep_memory (info) )
13614 {
13615 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13616 info->cache_size += (cookie->locsymcount
13617 * sizeof (Elf_External_Sym_Shndx));
13618 }
13619 }
13620 return true;
13621 }
13622
13623 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13624
13625 static void
13626 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13627 {
13628 Elf_Internal_Shdr *symtab_hdr;
13629
13630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13631 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13632 free (cookie->locsyms);
13633 }
13634
13635 /* Initialize the relocation information in COOKIE for input section SEC
13636 of input bfd ABFD. */
13637
13638 static bool
13639 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13640 struct bfd_link_info *info, bfd *abfd,
13641 asection *sec)
13642 {
13643 if (sec->reloc_count == 0)
13644 {
13645 cookie->rels = NULL;
13646 cookie->relend = NULL;
13647 }
13648 else
13649 {
13650 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13651 NULL, NULL,
13652 _bfd_link_keep_memory (info));
13653 if (cookie->rels == NULL)
13654 return false;
13655 cookie->rel = cookie->rels;
13656 cookie->relend = cookie->rels + sec->reloc_count;
13657 }
13658 cookie->rel = cookie->rels;
13659 return true;
13660 }
13661
13662 /* Free the memory allocated by init_reloc_cookie_rels,
13663 if appropriate. */
13664
13665 static void
13666 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13667 asection *sec)
13668 {
13669 if (elf_section_data (sec)->relocs != cookie->rels)
13670 free (cookie->rels);
13671 }
13672
13673 /* Initialize the whole of COOKIE for input section SEC. */
13674
13675 static bool
13676 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13677 struct bfd_link_info *info,
13678 asection *sec)
13679 {
13680 if (!init_reloc_cookie (cookie, info, sec->owner))
13681 goto error1;
13682 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13683 goto error2;
13684 return true;
13685
13686 error2:
13687 fini_reloc_cookie (cookie, sec->owner);
13688 error1:
13689 return false;
13690 }
13691
13692 /* Free the memory allocated by init_reloc_cookie_for_section,
13693 if appropriate. */
13694
13695 static void
13696 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13697 asection *sec)
13698 {
13699 fini_reloc_cookie_rels (cookie, sec);
13700 fini_reloc_cookie (cookie, sec->owner);
13701 }
13702 \f
13703 /* Garbage collect unused sections. */
13704
13705 /* Default gc_mark_hook. */
13706
13707 asection *
13708 _bfd_elf_gc_mark_hook (asection *sec,
13709 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13710 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13711 struct elf_link_hash_entry *h,
13712 Elf_Internal_Sym *sym)
13713 {
13714 if (h != NULL)
13715 {
13716 switch (h->root.type)
13717 {
13718 case bfd_link_hash_defined:
13719 case bfd_link_hash_defweak:
13720 return h->root.u.def.section;
13721
13722 case bfd_link_hash_common:
13723 return h->root.u.c.p->section;
13724
13725 default:
13726 break;
13727 }
13728 }
13729 else
13730 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13731
13732 return NULL;
13733 }
13734
13735 /* Return the debug definition section. */
13736
13737 static asection *
13738 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13739 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13740 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13741 struct elf_link_hash_entry *h,
13742 Elf_Internal_Sym *sym)
13743 {
13744 if (h != NULL)
13745 {
13746 /* Return the global debug definition section. */
13747 if ((h->root.type == bfd_link_hash_defined
13748 || h->root.type == bfd_link_hash_defweak)
13749 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13750 return h->root.u.def.section;
13751 }
13752 else
13753 {
13754 /* Return the local debug definition section. */
13755 asection *isec = bfd_section_from_elf_index (sec->owner,
13756 sym->st_shndx);
13757 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13758 return isec;
13759 }
13760
13761 return NULL;
13762 }
13763
13764 /* COOKIE->rel describes a relocation against section SEC, which is
13765 a section we've decided to keep. Return the section that contains
13766 the relocation symbol, or NULL if no section contains it. */
13767
13768 asection *
13769 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13770 elf_gc_mark_hook_fn gc_mark_hook,
13771 struct elf_reloc_cookie *cookie,
13772 bool *start_stop)
13773 {
13774 unsigned long r_symndx;
13775 struct elf_link_hash_entry *h, *hw;
13776
13777 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13778 if (r_symndx == STN_UNDEF)
13779 return NULL;
13780
13781 if (r_symndx >= cookie->locsymcount
13782 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13783 {
13784 bool was_marked;
13785
13786 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13787 if (h == NULL)
13788 {
13789 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13790 sec->owner);
13791 return NULL;
13792 }
13793 while (h->root.type == bfd_link_hash_indirect
13794 || h->root.type == bfd_link_hash_warning)
13795 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13796
13797 was_marked = h->mark;
13798 h->mark = 1;
13799 /* Keep all aliases of the symbol too. If an object symbol
13800 needs to be copied into .dynbss then all of its aliases
13801 should be present as dynamic symbols, not just the one used
13802 on the copy relocation. */
13803 hw = h;
13804 while (hw->is_weakalias)
13805 {
13806 hw = hw->u.alias;
13807 hw->mark = 1;
13808 }
13809
13810 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13811 {
13812 if (info->start_stop_gc)
13813 return NULL;
13814
13815 /* To work around a glibc bug, mark XXX input sections
13816 when there is a reference to __start_XXX or __stop_XXX
13817 symbols. */
13818 else if (start_stop != NULL)
13819 {
13820 asection *s = h->u2.start_stop_section;
13821 *start_stop = true;
13822 return s;
13823 }
13824 }
13825
13826 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13827 }
13828
13829 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13830 &cookie->locsyms[r_symndx]);
13831 }
13832
13833 /* COOKIE->rel describes a relocation against section SEC, which is
13834 a section we've decided to keep. Mark the section that contains
13835 the relocation symbol. */
13836
13837 bool
13838 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13839 asection *sec,
13840 elf_gc_mark_hook_fn gc_mark_hook,
13841 struct elf_reloc_cookie *cookie)
13842 {
13843 asection *rsec;
13844 bool start_stop = false;
13845
13846 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13847 while (rsec != NULL)
13848 {
13849 if (!rsec->gc_mark)
13850 {
13851 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13852 || (rsec->owner->flags & DYNAMIC) != 0)
13853 rsec->gc_mark = 1;
13854 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13855 return false;
13856 }
13857 if (!start_stop)
13858 break;
13859 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13860 }
13861 return true;
13862 }
13863
13864 /* The mark phase of garbage collection. For a given section, mark
13865 it and any sections in this section's group, and all the sections
13866 which define symbols to which it refers. */
13867
13868 bool
13869 _bfd_elf_gc_mark (struct bfd_link_info *info,
13870 asection *sec,
13871 elf_gc_mark_hook_fn gc_mark_hook)
13872 {
13873 bool ret;
13874 asection *group_sec, *eh_frame;
13875
13876 sec->gc_mark = 1;
13877
13878 /* Mark all the sections in the group. */
13879 group_sec = elf_section_data (sec)->next_in_group;
13880 if (group_sec && !group_sec->gc_mark)
13881 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13882 return false;
13883
13884 /* Look through the section relocs. */
13885 ret = true;
13886 eh_frame = elf_eh_frame_section (sec->owner);
13887 if ((sec->flags & SEC_RELOC) != 0
13888 && sec->reloc_count > 0
13889 && sec != eh_frame)
13890 {
13891 struct elf_reloc_cookie cookie;
13892
13893 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13894 ret = false;
13895 else
13896 {
13897 for (; cookie.rel < cookie.relend; cookie.rel++)
13898 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13899 {
13900 ret = false;
13901 break;
13902 }
13903 fini_reloc_cookie_for_section (&cookie, sec);
13904 }
13905 }
13906
13907 if (ret && eh_frame && elf_fde_list (sec))
13908 {
13909 struct elf_reloc_cookie cookie;
13910
13911 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13912 ret = false;
13913 else
13914 {
13915 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13916 gc_mark_hook, &cookie))
13917 ret = false;
13918 fini_reloc_cookie_for_section (&cookie, eh_frame);
13919 }
13920 }
13921
13922 eh_frame = elf_section_eh_frame_entry (sec);
13923 if (ret && eh_frame && !eh_frame->gc_mark)
13924 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13925 ret = false;
13926
13927 return ret;
13928 }
13929
13930 /* Scan and mark sections in a special or debug section group. */
13931
13932 static void
13933 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13934 {
13935 /* Point to first section of section group. */
13936 asection *ssec;
13937 /* Used to iterate the section group. */
13938 asection *msec;
13939
13940 bool is_special_grp = true;
13941 bool is_debug_grp = true;
13942
13943 /* First scan to see if group contains any section other than debug
13944 and special section. */
13945 ssec = msec = elf_next_in_group (grp);
13946 do
13947 {
13948 if ((msec->flags & SEC_DEBUGGING) == 0)
13949 is_debug_grp = false;
13950
13951 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13952 is_special_grp = false;
13953
13954 msec = elf_next_in_group (msec);
13955 }
13956 while (msec != ssec);
13957
13958 /* If this is a pure debug section group or pure special section group,
13959 keep all sections in this group. */
13960 if (is_debug_grp || is_special_grp)
13961 {
13962 do
13963 {
13964 msec->gc_mark = 1;
13965 msec = elf_next_in_group (msec);
13966 }
13967 while (msec != ssec);
13968 }
13969 }
13970
13971 /* Keep debug and special sections. */
13972
13973 bool
13974 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13975 elf_gc_mark_hook_fn mark_hook)
13976 {
13977 bfd *ibfd;
13978
13979 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13980 {
13981 asection *isec;
13982 bool some_kept;
13983 bool debug_frag_seen;
13984 bool has_kept_debug_info;
13985
13986 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13987 continue;
13988 isec = ibfd->sections;
13989 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13990 continue;
13991
13992 /* Ensure all linker created sections are kept,
13993 see if any other section is already marked,
13994 and note if we have any fragmented debug sections. */
13995 debug_frag_seen = some_kept = has_kept_debug_info = false;
13996 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13997 {
13998 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13999 isec->gc_mark = 1;
14000 else if (isec->gc_mark
14001 && (isec->flags & SEC_ALLOC) != 0
14002 && elf_section_type (isec) != SHT_NOTE)
14003 some_kept = true;
14004 else
14005 {
14006 /* Since all sections, except for backend specific ones,
14007 have been garbage collected, call mark_hook on this
14008 section if any of its linked-to sections is marked. */
14009 asection *linked_to_sec;
14010 for (linked_to_sec = elf_linked_to_section (isec);
14011 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14012 linked_to_sec = elf_linked_to_section (linked_to_sec))
14013 {
14014 if (linked_to_sec->gc_mark)
14015 {
14016 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14017 return false;
14018 break;
14019 }
14020 linked_to_sec->linker_mark = 1;
14021 }
14022 for (linked_to_sec = elf_linked_to_section (isec);
14023 linked_to_sec != NULL && linked_to_sec->linker_mark;
14024 linked_to_sec = elf_linked_to_section (linked_to_sec))
14025 linked_to_sec->linker_mark = 0;
14026 }
14027
14028 if (!debug_frag_seen
14029 && (isec->flags & SEC_DEBUGGING)
14030 && startswith (isec->name, ".debug_line."))
14031 debug_frag_seen = true;
14032 else if (strcmp (bfd_section_name (isec),
14033 "__patchable_function_entries") == 0
14034 && elf_linked_to_section (isec) == NULL)
14035 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14036 "need linked-to section "
14037 "for --gc-sections\n"),
14038 isec->owner, isec);
14039 }
14040
14041 /* If no non-note alloc section in this file will be kept, then
14042 we can toss out the debug and special sections. */
14043 if (!some_kept)
14044 continue;
14045
14046 /* Keep debug and special sections like .comment when they are
14047 not part of a group. Also keep section groups that contain
14048 just debug sections or special sections. NB: Sections with
14049 linked-to section has been handled above. */
14050 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14051 {
14052 if ((isec->flags & SEC_GROUP) != 0)
14053 _bfd_elf_gc_mark_debug_special_section_group (isec);
14054 else if (((isec->flags & SEC_DEBUGGING) != 0
14055 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14056 && elf_next_in_group (isec) == NULL
14057 && elf_linked_to_section (isec) == NULL)
14058 isec->gc_mark = 1;
14059 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14060 has_kept_debug_info = true;
14061 }
14062
14063 /* Look for CODE sections which are going to be discarded,
14064 and find and discard any fragmented debug sections which
14065 are associated with that code section. */
14066 if (debug_frag_seen)
14067 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14068 if ((isec->flags & SEC_CODE) != 0
14069 && isec->gc_mark == 0)
14070 {
14071 unsigned int ilen;
14072 asection *dsec;
14073
14074 ilen = strlen (isec->name);
14075
14076 /* Association is determined by the name of the debug
14077 section containing the name of the code section as
14078 a suffix. For example .debug_line.text.foo is a
14079 debug section associated with .text.foo. */
14080 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14081 {
14082 unsigned int dlen;
14083
14084 if (dsec->gc_mark == 0
14085 || (dsec->flags & SEC_DEBUGGING) == 0)
14086 continue;
14087
14088 dlen = strlen (dsec->name);
14089
14090 if (dlen > ilen
14091 && strncmp (dsec->name + (dlen - ilen),
14092 isec->name, ilen) == 0)
14093 dsec->gc_mark = 0;
14094 }
14095 }
14096
14097 /* Mark debug sections referenced by kept debug sections. */
14098 if (has_kept_debug_info)
14099 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14100 if (isec->gc_mark
14101 && (isec->flags & SEC_DEBUGGING) != 0)
14102 if (!_bfd_elf_gc_mark (info, isec,
14103 elf_gc_mark_debug_section))
14104 return false;
14105 }
14106 return true;
14107 }
14108
14109 static bool
14110 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14111 {
14112 bfd *sub;
14113 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14114
14115 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14116 {
14117 asection *o;
14118
14119 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14120 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14121 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14122 continue;
14123 o = sub->sections;
14124 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14125 continue;
14126
14127 for (o = sub->sections; o != NULL; o = o->next)
14128 {
14129 /* When any section in a section group is kept, we keep all
14130 sections in the section group. If the first member of
14131 the section group is excluded, we will also exclude the
14132 group section. */
14133 if (o->flags & SEC_GROUP)
14134 {
14135 asection *first = elf_next_in_group (o);
14136 o->gc_mark = first->gc_mark;
14137 }
14138
14139 if (o->gc_mark)
14140 continue;
14141
14142 /* Skip sweeping sections already excluded. */
14143 if (o->flags & SEC_EXCLUDE)
14144 continue;
14145
14146 /* Since this is early in the link process, it is simple
14147 to remove a section from the output. */
14148 o->flags |= SEC_EXCLUDE;
14149
14150 if (info->print_gc_sections && o->size != 0)
14151 /* xgettext:c-format */
14152 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14153 o, sub);
14154 }
14155 }
14156
14157 return true;
14158 }
14159
14160 /* Propagate collected vtable information. This is called through
14161 elf_link_hash_traverse. */
14162
14163 static bool
14164 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14165 {
14166 /* Those that are not vtables. */
14167 if (h->start_stop
14168 || h->u2.vtable == NULL
14169 || h->u2.vtable->parent == NULL)
14170 return true;
14171
14172 /* Those vtables that do not have parents, we cannot merge. */
14173 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14174 return true;
14175
14176 /* If we've already been done, exit. */
14177 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14178 return true;
14179
14180 /* Make sure the parent's table is up to date. */
14181 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14182
14183 if (h->u2.vtable->used == NULL)
14184 {
14185 /* None of this table's entries were referenced. Re-use the
14186 parent's table. */
14187 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14188 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14189 }
14190 else
14191 {
14192 size_t n;
14193 bool *cu, *pu;
14194
14195 /* Or the parent's entries into ours. */
14196 cu = h->u2.vtable->used;
14197 cu[-1] = true;
14198 pu = h->u2.vtable->parent->u2.vtable->used;
14199 if (pu != NULL)
14200 {
14201 const struct elf_backend_data *bed;
14202 unsigned int log_file_align;
14203
14204 bed = get_elf_backend_data (h->root.u.def.section->owner);
14205 log_file_align = bed->s->log_file_align;
14206 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14207 while (n--)
14208 {
14209 if (*pu)
14210 *cu = true;
14211 pu++;
14212 cu++;
14213 }
14214 }
14215 }
14216
14217 return true;
14218 }
14219
14220 struct link_info_ok
14221 {
14222 struct bfd_link_info *info;
14223 bool ok;
14224 };
14225
14226 static bool
14227 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14228 void *ptr)
14229 {
14230 asection *sec;
14231 bfd_vma hstart, hend;
14232 Elf_Internal_Rela *relstart, *relend, *rel;
14233 const struct elf_backend_data *bed;
14234 unsigned int log_file_align;
14235 struct link_info_ok *info = (struct link_info_ok *) ptr;
14236
14237 /* Take care of both those symbols that do not describe vtables as
14238 well as those that are not loaded. */
14239 if (h->start_stop
14240 || h->u2.vtable == NULL
14241 || h->u2.vtable->parent == NULL)
14242 return true;
14243
14244 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14245 || h->root.type == bfd_link_hash_defweak);
14246
14247 sec = h->root.u.def.section;
14248 hstart = h->root.u.def.value;
14249 hend = hstart + h->size;
14250
14251 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14252 sec, NULL, NULL, true);
14253 if (!relstart)
14254 return info->ok = false;
14255 bed = get_elf_backend_data (sec->owner);
14256 log_file_align = bed->s->log_file_align;
14257
14258 relend = relstart + sec->reloc_count;
14259
14260 for (rel = relstart; rel < relend; ++rel)
14261 if (rel->r_offset >= hstart && rel->r_offset < hend)
14262 {
14263 /* If the entry is in use, do nothing. */
14264 if (h->u2.vtable->used
14265 && (rel->r_offset - hstart) < h->u2.vtable->size)
14266 {
14267 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14268 if (h->u2.vtable->used[entry])
14269 continue;
14270 }
14271 /* Otherwise, kill it. */
14272 rel->r_offset = rel->r_info = rel->r_addend = 0;
14273 }
14274
14275 return true;
14276 }
14277
14278 /* Mark sections containing dynamically referenced symbols. When
14279 building shared libraries, we must assume that any visible symbol is
14280 referenced. */
14281
14282 bool
14283 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14284 {
14285 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14286 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14287
14288 if ((h->root.type == bfd_link_hash_defined
14289 || h->root.type == bfd_link_hash_defweak)
14290 && (!h->start_stop
14291 || h->root.ldscript_def
14292 || !info->start_stop_gc)
14293 && ((h->ref_dynamic && !h->forced_local)
14294 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14295 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14296 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14297 && (!bfd_link_executable (info)
14298 || info->gc_keep_exported
14299 || info->export_dynamic
14300 || (h->dynamic
14301 && d != NULL
14302 && (*d->match) (&d->head, NULL, h->root.root.string)))
14303 && (h->versioned >= versioned
14304 || !bfd_hide_sym_by_version (info->version_info,
14305 h->root.root.string)))))
14306 h->root.u.def.section->flags |= SEC_KEEP;
14307
14308 return true;
14309 }
14310
14311 /* Keep all sections containing symbols undefined on the command-line,
14312 and the section containing the entry symbol. */
14313
14314 void
14315 _bfd_elf_gc_keep (struct bfd_link_info *info)
14316 {
14317 struct bfd_sym_chain *sym;
14318
14319 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14320 {
14321 struct elf_link_hash_entry *h;
14322
14323 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14324 false, false, false);
14325
14326 if (h != NULL
14327 && (h->root.type == bfd_link_hash_defined
14328 || h->root.type == bfd_link_hash_defweak)
14329 && !bfd_is_const_section (h->root.u.def.section))
14330 h->root.u.def.section->flags |= SEC_KEEP;
14331 }
14332 }
14333
14334 bool
14335 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14336 struct bfd_link_info *info)
14337 {
14338 bfd *ibfd = info->input_bfds;
14339
14340 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14341 {
14342 asection *sec;
14343 struct elf_reloc_cookie cookie;
14344
14345 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14346 continue;
14347 sec = ibfd->sections;
14348 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14349 continue;
14350
14351 if (!init_reloc_cookie (&cookie, info, ibfd))
14352 return false;
14353
14354 for (sec = ibfd->sections; sec; sec = sec->next)
14355 {
14356 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14357 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14358 {
14359 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14360 fini_reloc_cookie_rels (&cookie, sec);
14361 }
14362 }
14363 }
14364 return true;
14365 }
14366
14367 /* Do mark and sweep of unused sections. */
14368
14369 bool
14370 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14371 {
14372 bool ok = true;
14373 bfd *sub;
14374 elf_gc_mark_hook_fn gc_mark_hook;
14375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14376 struct elf_link_hash_table *htab;
14377 struct link_info_ok info_ok;
14378
14379 if (!bed->can_gc_sections
14380 || !is_elf_hash_table (info->hash))
14381 {
14382 _bfd_error_handler(_("warning: gc-sections option ignored"));
14383 return true;
14384 }
14385
14386 bed->gc_keep (info);
14387 htab = elf_hash_table (info);
14388
14389 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14390 at the .eh_frame section if we can mark the FDEs individually. */
14391 for (sub = info->input_bfds;
14392 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14393 sub = sub->link.next)
14394 {
14395 asection *sec;
14396 struct elf_reloc_cookie cookie;
14397
14398 sec = sub->sections;
14399 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14400 continue;
14401 sec = bfd_get_section_by_name (sub, ".eh_frame");
14402 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14403 {
14404 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14405 if (elf_section_data (sec)->sec_info
14406 && (sec->flags & SEC_LINKER_CREATED) == 0)
14407 elf_eh_frame_section (sub) = sec;
14408 fini_reloc_cookie_for_section (&cookie, sec);
14409 sec = bfd_get_next_section_by_name (NULL, sec);
14410 }
14411 }
14412
14413 /* Apply transitive closure to the vtable entry usage info. */
14414 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14415 if (!ok)
14416 return false;
14417
14418 /* Kill the vtable relocations that were not used. */
14419 info_ok.info = info;
14420 info_ok.ok = true;
14421 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14422 if (!info_ok.ok)
14423 return false;
14424
14425 /* Mark dynamically referenced symbols. */
14426 if (htab->dynamic_sections_created || info->gc_keep_exported)
14427 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14428
14429 /* Grovel through relocs to find out who stays ... */
14430 gc_mark_hook = bed->gc_mark_hook;
14431 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14432 {
14433 asection *o;
14434
14435 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14436 || elf_object_id (sub) != elf_hash_table_id (htab)
14437 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14438 continue;
14439
14440 o = sub->sections;
14441 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14442 continue;
14443
14444 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14445 Also treat note sections as a root, if the section is not part
14446 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14447 well as FINI_ARRAY sections for ld -r. */
14448 for (o = sub->sections; o != NULL; o = o->next)
14449 if (!o->gc_mark
14450 && (o->flags & SEC_EXCLUDE) == 0
14451 && ((o->flags & SEC_KEEP) != 0
14452 || (bfd_link_relocatable (info)
14453 && ((elf_section_data (o)->this_hdr.sh_type
14454 == SHT_PREINIT_ARRAY)
14455 || (elf_section_data (o)->this_hdr.sh_type
14456 == SHT_INIT_ARRAY)
14457 || (elf_section_data (o)->this_hdr.sh_type
14458 == SHT_FINI_ARRAY)))
14459 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14460 && elf_next_in_group (o) == NULL
14461 && elf_linked_to_section (o) == NULL)
14462 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14463 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14464 {
14465 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14466 return false;
14467 }
14468 }
14469
14470 /* Allow the backend to mark additional target specific sections. */
14471 bed->gc_mark_extra_sections (info, gc_mark_hook);
14472
14473 /* ... and mark SEC_EXCLUDE for those that go. */
14474 return elf_gc_sweep (abfd, info);
14475 }
14476 \f
14477 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14478
14479 bool
14480 bfd_elf_gc_record_vtinherit (bfd *abfd,
14481 asection *sec,
14482 struct elf_link_hash_entry *h,
14483 bfd_vma offset)
14484 {
14485 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14486 struct elf_link_hash_entry **search, *child;
14487 size_t extsymcount;
14488 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14489
14490 /* The sh_info field of the symtab header tells us where the
14491 external symbols start. We don't care about the local symbols at
14492 this point. */
14493 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14494 if (!elf_bad_symtab (abfd))
14495 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14496
14497 sym_hashes = elf_sym_hashes (abfd);
14498 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14499
14500 /* Hunt down the child symbol, which is in this section at the same
14501 offset as the relocation. */
14502 for (search = sym_hashes; search != sym_hashes_end; ++search)
14503 {
14504 if ((child = *search) != NULL
14505 && (child->root.type == bfd_link_hash_defined
14506 || child->root.type == bfd_link_hash_defweak)
14507 && child->root.u.def.section == sec
14508 && child->root.u.def.value == offset)
14509 goto win;
14510 }
14511
14512 /* xgettext:c-format */
14513 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14514 abfd, sec, (uint64_t) offset);
14515 bfd_set_error (bfd_error_invalid_operation);
14516 return false;
14517
14518 win:
14519 if (!child->u2.vtable)
14520 {
14521 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14522 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14523 if (!child->u2.vtable)
14524 return false;
14525 }
14526 if (!h)
14527 {
14528 /* This *should* only be the absolute section. It could potentially
14529 be that someone has defined a non-global vtable though, which
14530 would be bad. It isn't worth paging in the local symbols to be
14531 sure though; that case should simply be handled by the assembler. */
14532
14533 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14534 }
14535 else
14536 child->u2.vtable->parent = h;
14537
14538 return true;
14539 }
14540
14541 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14542
14543 bool
14544 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14545 struct elf_link_hash_entry *h,
14546 bfd_vma addend)
14547 {
14548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14549 unsigned int log_file_align = bed->s->log_file_align;
14550
14551 if (!h)
14552 {
14553 /* xgettext:c-format */
14554 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14555 abfd, sec);
14556 bfd_set_error (bfd_error_bad_value);
14557 return false;
14558 }
14559
14560 if (!h->u2.vtable)
14561 {
14562 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14563 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14564 if (!h->u2.vtable)
14565 return false;
14566 }
14567
14568 if (addend >= h->u2.vtable->size)
14569 {
14570 size_t size, bytes, file_align;
14571 bool *ptr = h->u2.vtable->used;
14572
14573 /* While the symbol is undefined, we have to be prepared to handle
14574 a zero size. */
14575 file_align = 1 << log_file_align;
14576 if (h->root.type == bfd_link_hash_undefined)
14577 size = addend + file_align;
14578 else
14579 {
14580 size = h->size;
14581 if (addend >= size)
14582 {
14583 /* Oops! We've got a reference past the defined end of
14584 the table. This is probably a bug -- shall we warn? */
14585 size = addend + file_align;
14586 }
14587 }
14588 size = (size + file_align - 1) & -file_align;
14589
14590 /* Allocate one extra entry for use as a "done" flag for the
14591 consolidation pass. */
14592 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14593
14594 if (ptr)
14595 {
14596 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14597
14598 if (ptr != NULL)
14599 {
14600 size_t oldbytes;
14601
14602 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14603 * sizeof (bool));
14604 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14605 }
14606 }
14607 else
14608 ptr = (bool *) bfd_zmalloc (bytes);
14609
14610 if (ptr == NULL)
14611 return false;
14612
14613 /* And arrange for that done flag to be at index -1. */
14614 h->u2.vtable->used = ptr + 1;
14615 h->u2.vtable->size = size;
14616 }
14617
14618 h->u2.vtable->used[addend >> log_file_align] = true;
14619
14620 return true;
14621 }
14622
14623 /* Map an ELF section header flag to its corresponding string. */
14624 typedef struct
14625 {
14626 char *flag_name;
14627 flagword flag_value;
14628 } elf_flags_to_name_table;
14629
14630 static const elf_flags_to_name_table elf_flags_to_names [] =
14631 {
14632 { "SHF_WRITE", SHF_WRITE },
14633 { "SHF_ALLOC", SHF_ALLOC },
14634 { "SHF_EXECINSTR", SHF_EXECINSTR },
14635 { "SHF_MERGE", SHF_MERGE },
14636 { "SHF_STRINGS", SHF_STRINGS },
14637 { "SHF_INFO_LINK", SHF_INFO_LINK},
14638 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14639 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14640 { "SHF_GROUP", SHF_GROUP },
14641 { "SHF_TLS", SHF_TLS },
14642 { "SHF_MASKOS", SHF_MASKOS },
14643 { "SHF_EXCLUDE", SHF_EXCLUDE },
14644 };
14645
14646 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14647 bool
14648 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14649 struct flag_info *flaginfo,
14650 asection *section)
14651 {
14652 const bfd_vma sh_flags = elf_section_flags (section);
14653
14654 if (!flaginfo->flags_initialized)
14655 {
14656 bfd *obfd = info->output_bfd;
14657 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14658 struct flag_info_list *tf = flaginfo->flag_list;
14659 int with_hex = 0;
14660 int without_hex = 0;
14661
14662 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14663 {
14664 unsigned i;
14665 flagword (*lookup) (char *);
14666
14667 lookup = bed->elf_backend_lookup_section_flags_hook;
14668 if (lookup != NULL)
14669 {
14670 flagword hexval = (*lookup) ((char *) tf->name);
14671
14672 if (hexval != 0)
14673 {
14674 if (tf->with == with_flags)
14675 with_hex |= hexval;
14676 else if (tf->with == without_flags)
14677 without_hex |= hexval;
14678 tf->valid = true;
14679 continue;
14680 }
14681 }
14682 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14683 {
14684 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14685 {
14686 if (tf->with == with_flags)
14687 with_hex |= elf_flags_to_names[i].flag_value;
14688 else if (tf->with == without_flags)
14689 without_hex |= elf_flags_to_names[i].flag_value;
14690 tf->valid = true;
14691 break;
14692 }
14693 }
14694 if (!tf->valid)
14695 {
14696 info->callbacks->einfo
14697 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14698 return false;
14699 }
14700 }
14701 flaginfo->flags_initialized = true;
14702 flaginfo->only_with_flags |= with_hex;
14703 flaginfo->not_with_flags |= without_hex;
14704 }
14705
14706 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14707 return false;
14708
14709 if ((flaginfo->not_with_flags & sh_flags) != 0)
14710 return false;
14711
14712 return true;
14713 }
14714
14715 struct alloc_got_off_arg {
14716 bfd_vma gotoff;
14717 struct bfd_link_info *info;
14718 };
14719
14720 /* We need a special top-level link routine to convert got reference counts
14721 to real got offsets. */
14722
14723 static bool
14724 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14725 {
14726 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14727 bfd *obfd = gofarg->info->output_bfd;
14728 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14729
14730 if (h->got.refcount > 0)
14731 {
14732 h->got.offset = gofarg->gotoff;
14733 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14734 }
14735 else
14736 h->got.offset = (bfd_vma) -1;
14737
14738 return true;
14739 }
14740
14741 /* And an accompanying bit to work out final got entry offsets once
14742 we're done. Should be called from final_link. */
14743
14744 bool
14745 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14746 struct bfd_link_info *info)
14747 {
14748 bfd *i;
14749 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14750 bfd_vma gotoff;
14751 struct alloc_got_off_arg gofarg;
14752
14753 BFD_ASSERT (abfd == info->output_bfd);
14754
14755 if (! is_elf_hash_table (info->hash))
14756 return false;
14757
14758 /* The GOT offset is relative to the .got section, but the GOT header is
14759 put into the .got.plt section, if the backend uses it. */
14760 if (bed->want_got_plt)
14761 gotoff = 0;
14762 else
14763 gotoff = bed->got_header_size;
14764
14765 /* Do the local .got entries first. */
14766 for (i = info->input_bfds; i; i = i->link.next)
14767 {
14768 bfd_signed_vma *local_got;
14769 size_t j, locsymcount;
14770 Elf_Internal_Shdr *symtab_hdr;
14771
14772 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14773 continue;
14774
14775 local_got = elf_local_got_refcounts (i);
14776 if (!local_got)
14777 continue;
14778
14779 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14780 if (elf_bad_symtab (i))
14781 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14782 else
14783 locsymcount = symtab_hdr->sh_info;
14784
14785 for (j = 0; j < locsymcount; ++j)
14786 {
14787 if (local_got[j] > 0)
14788 {
14789 local_got[j] = gotoff;
14790 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14791 }
14792 else
14793 local_got[j] = (bfd_vma) -1;
14794 }
14795 }
14796
14797 /* Then the global .got entries. .plt refcounts are handled by
14798 adjust_dynamic_symbol */
14799 gofarg.gotoff = gotoff;
14800 gofarg.info = info;
14801 elf_link_hash_traverse (elf_hash_table (info),
14802 elf_gc_allocate_got_offsets,
14803 &gofarg);
14804 return true;
14805 }
14806
14807 /* Many folk need no more in the way of final link than this, once
14808 got entry reference counting is enabled. */
14809
14810 bool
14811 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14812 {
14813 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14814 return false;
14815
14816 /* Invoke the regular ELF backend linker to do all the work. */
14817 return bfd_elf_final_link (abfd, info);
14818 }
14819
14820 bool
14821 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14822 {
14823 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14824
14825 if (rcookie->bad_symtab)
14826 rcookie->rel = rcookie->rels;
14827
14828 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14829 {
14830 unsigned long r_symndx;
14831
14832 if (! rcookie->bad_symtab)
14833 if (rcookie->rel->r_offset > offset)
14834 return false;
14835 if (rcookie->rel->r_offset != offset)
14836 continue;
14837
14838 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14839 if (r_symndx == STN_UNDEF)
14840 return true;
14841
14842 if (r_symndx >= rcookie->locsymcount
14843 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14844 {
14845 struct elf_link_hash_entry *h;
14846
14847 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14848
14849 while (h->root.type == bfd_link_hash_indirect
14850 || h->root.type == bfd_link_hash_warning)
14851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14852
14853 if ((h->root.type == bfd_link_hash_defined
14854 || h->root.type == bfd_link_hash_defweak)
14855 && (h->root.u.def.section->owner != rcookie->abfd
14856 || h->root.u.def.section->kept_section != NULL
14857 || discarded_section (h->root.u.def.section)))
14858 return true;
14859 }
14860 else
14861 {
14862 /* It's not a relocation against a global symbol,
14863 but it could be a relocation against a local
14864 symbol for a discarded section. */
14865 asection *isec;
14866 Elf_Internal_Sym *isym;
14867
14868 /* Need to: get the symbol; get the section. */
14869 isym = &rcookie->locsyms[r_symndx];
14870 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14871 if (isec != NULL
14872 && (isec->kept_section != NULL
14873 || discarded_section (isec)))
14874 return true;
14875 }
14876 return false;
14877 }
14878 return false;
14879 }
14880
14881 /* Discard unneeded references to discarded sections.
14882 Returns -1 on error, 1 if any section's size was changed, 0 if
14883 nothing changed. This function assumes that the relocations are in
14884 sorted order, which is true for all known assemblers. */
14885
14886 int
14887 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14888 {
14889 struct elf_reloc_cookie cookie;
14890 asection *o;
14891 bfd *abfd;
14892 int changed = 0;
14893
14894 if (info->traditional_format
14895 || !is_elf_hash_table (info->hash))
14896 return 0;
14897
14898 o = bfd_get_section_by_name (output_bfd, ".stab");
14899 if (o != NULL)
14900 {
14901 asection *i;
14902
14903 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14904 {
14905 if (i->size == 0
14906 || i->reloc_count == 0
14907 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14908 continue;
14909
14910 abfd = i->owner;
14911 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14912 continue;
14913
14914 if (!init_reloc_cookie_for_section (&cookie, info, i))
14915 return -1;
14916
14917 if (_bfd_discard_section_stabs (abfd, i,
14918 elf_section_data (i)->sec_info,
14919 bfd_elf_reloc_symbol_deleted_p,
14920 &cookie))
14921 changed = 1;
14922
14923 fini_reloc_cookie_for_section (&cookie, i);
14924 }
14925 }
14926
14927 o = NULL;
14928 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14929 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14930 if (o != NULL)
14931 {
14932 asection *i;
14933 int eh_changed = 0;
14934 unsigned int eh_alignment; /* Octets. */
14935
14936 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14937 {
14938 if (i->size == 0)
14939 continue;
14940
14941 abfd = i->owner;
14942 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14943 continue;
14944
14945 if (!init_reloc_cookie_for_section (&cookie, info, i))
14946 return -1;
14947
14948 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14949 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14950 bfd_elf_reloc_symbol_deleted_p,
14951 &cookie))
14952 {
14953 eh_changed = 1;
14954 if (i->size != i->rawsize)
14955 changed = 1;
14956 }
14957
14958 fini_reloc_cookie_for_section (&cookie, i);
14959 }
14960
14961 eh_alignment = ((1 << o->alignment_power)
14962 * bfd_octets_per_byte (output_bfd, o));
14963 /* Skip over zero terminator, and prevent empty sections from
14964 adding alignment padding at the end. */
14965 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14966 if (i->size == 0)
14967 i->flags |= SEC_EXCLUDE;
14968 else if (i->size > 4)
14969 break;
14970 /* The last non-empty eh_frame section doesn't need padding. */
14971 if (i != NULL)
14972 i = i->map_tail.s;
14973 /* Any prior sections must pad the last FDE out to the output
14974 section alignment. Otherwise we might have zero padding
14975 between sections, which would be seen as a terminator. */
14976 for (; i != NULL; i = i->map_tail.s)
14977 if (i->size == 4)
14978 /* All but the last zero terminator should have been removed. */
14979 BFD_FAIL ();
14980 else
14981 {
14982 bfd_size_type size
14983 = (i->size + eh_alignment - 1) & -eh_alignment;
14984 if (i->size != size)
14985 {
14986 i->size = size;
14987 changed = 1;
14988 eh_changed = 1;
14989 }
14990 }
14991 if (eh_changed)
14992 elf_link_hash_traverse (elf_hash_table (info),
14993 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14994 }
14995
14996 o = bfd_get_section_by_name (output_bfd, ".sframe");
14997 if (o != NULL)
14998 {
14999 asection *i;
15000
15001 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15002 {
15003 if (i->size == 0)
15004 continue;
15005
15006 abfd = i->owner;
15007 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15008 continue;
15009
15010 if (!init_reloc_cookie_for_section (&cookie, info, i))
15011 return -1;
15012
15013 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15014 {
15015 if (_bfd_elf_discard_section_sframe (i,
15016 bfd_elf_reloc_symbol_deleted_p,
15017 &cookie))
15018 {
15019 if (i->size != i->rawsize)
15020 changed = 1;
15021 }
15022 }
15023 fini_reloc_cookie_for_section (&cookie, i);
15024 }
15025 /* Update the reference to the output .sframe section. Used to
15026 determine later if PT_GNU_SFRAME segment is to be generated. */
15027 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15028 return -1;
15029 }
15030
15031 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15032 {
15033 const struct elf_backend_data *bed;
15034 asection *s;
15035
15036 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15037 continue;
15038 s = abfd->sections;
15039 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15040 continue;
15041
15042 bed = get_elf_backend_data (abfd);
15043
15044 if (bed->elf_backend_discard_info != NULL)
15045 {
15046 if (!init_reloc_cookie (&cookie, info, abfd))
15047 return -1;
15048
15049 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15050 changed = 1;
15051
15052 fini_reloc_cookie (&cookie, abfd);
15053 }
15054 }
15055
15056 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15057 _bfd_elf_end_eh_frame_parsing (info);
15058
15059 if (info->eh_frame_hdr_type
15060 && !bfd_link_relocatable (info)
15061 && _bfd_elf_discard_section_eh_frame_hdr (info))
15062 changed = 1;
15063
15064 return changed;
15065 }
15066
15067 bool
15068 _bfd_elf_section_already_linked (bfd *abfd,
15069 asection *sec,
15070 struct bfd_link_info *info)
15071 {
15072 flagword flags;
15073 const char *name, *key;
15074 struct bfd_section_already_linked *l;
15075 struct bfd_section_already_linked_hash_entry *already_linked_list;
15076
15077 if (sec->output_section == bfd_abs_section_ptr)
15078 return false;
15079
15080 flags = sec->flags;
15081
15082 /* Return if it isn't a linkonce section. A comdat group section
15083 also has SEC_LINK_ONCE set. */
15084 if ((flags & SEC_LINK_ONCE) == 0)
15085 return false;
15086
15087 /* Don't put group member sections on our list of already linked
15088 sections. They are handled as a group via their group section. */
15089 if (elf_sec_group (sec) != NULL)
15090 return false;
15091
15092 /* For a SHT_GROUP section, use the group signature as the key. */
15093 name = sec->name;
15094 if ((flags & SEC_GROUP) != 0
15095 && elf_next_in_group (sec) != NULL
15096 && elf_group_name (elf_next_in_group (sec)) != NULL)
15097 key = elf_group_name (elf_next_in_group (sec));
15098 else
15099 {
15100 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15101 if (startswith (name, ".gnu.linkonce.")
15102 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15103 key++;
15104 else
15105 /* Must be a user linkonce section that doesn't follow gcc's
15106 naming convention. In this case we won't be matching
15107 single member groups. */
15108 key = name;
15109 }
15110
15111 already_linked_list = bfd_section_already_linked_table_lookup (key);
15112
15113 for (l = already_linked_list->entry; l != NULL; l = l->next)
15114 {
15115 /* We may have 2 different types of sections on the list: group
15116 sections with a signature of <key> (<key> is some string),
15117 and linkonce sections named .gnu.linkonce.<type>.<key>.
15118 Match like sections. LTO plugin sections are an exception.
15119 They are always named .gnu.linkonce.t.<key> and match either
15120 type of section. */
15121 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15122 && ((flags & SEC_GROUP) != 0
15123 || strcmp (name, l->sec->name) == 0))
15124 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15125 || (sec->owner->flags & BFD_PLUGIN) != 0)
15126 {
15127 /* The section has already been linked. See if we should
15128 issue a warning. */
15129 if (!_bfd_handle_already_linked (sec, l, info))
15130 return false;
15131
15132 if (flags & SEC_GROUP)
15133 {
15134 asection *first = elf_next_in_group (sec);
15135 asection *s = first;
15136
15137 while (s != NULL)
15138 {
15139 s->output_section = bfd_abs_section_ptr;
15140 /* Record which group discards it. */
15141 s->kept_section = l->sec;
15142 s = elf_next_in_group (s);
15143 /* These lists are circular. */
15144 if (s == first)
15145 break;
15146 }
15147 }
15148
15149 return true;
15150 }
15151 }
15152
15153 /* A single member comdat group section may be discarded by a
15154 linkonce section and vice versa. */
15155 if ((flags & SEC_GROUP) != 0)
15156 {
15157 asection *first = elf_next_in_group (sec);
15158
15159 if (first != NULL && elf_next_in_group (first) == first)
15160 /* Check this single member group against linkonce sections. */
15161 for (l = already_linked_list->entry; l != NULL; l = l->next)
15162 if ((l->sec->flags & SEC_GROUP) == 0
15163 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15164 {
15165 first->output_section = bfd_abs_section_ptr;
15166 first->kept_section = l->sec;
15167 sec->output_section = bfd_abs_section_ptr;
15168 break;
15169 }
15170 }
15171 else
15172 /* Check this linkonce section against single member groups. */
15173 for (l = already_linked_list->entry; l != NULL; l = l->next)
15174 if (l->sec->flags & SEC_GROUP)
15175 {
15176 asection *first = elf_next_in_group (l->sec);
15177
15178 if (first != NULL
15179 && elf_next_in_group (first) == first
15180 && bfd_elf_match_symbols_in_sections (first, sec, info))
15181 {
15182 sec->output_section = bfd_abs_section_ptr;
15183 sec->kept_section = first;
15184 break;
15185 }
15186 }
15187
15188 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15189 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15190 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15191 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15192 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15193 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15194 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15195 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15196 The reverse order cannot happen as there is never a bfd with only the
15197 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15198 matter as here were are looking only for cross-bfd sections. */
15199
15200 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15201 for (l = already_linked_list->entry; l != NULL; l = l->next)
15202 if ((l->sec->flags & SEC_GROUP) == 0
15203 && startswith (l->sec->name, ".gnu.linkonce.t."))
15204 {
15205 if (abfd != l->sec->owner)
15206 sec->output_section = bfd_abs_section_ptr;
15207 break;
15208 }
15209
15210 /* This is the first section with this name. Record it. */
15211 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15212 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15213 return sec->output_section == bfd_abs_section_ptr;
15214 }
15215
15216 bool
15217 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15218 {
15219 return sym->st_shndx == SHN_COMMON;
15220 }
15221
15222 unsigned int
15223 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15224 {
15225 return SHN_COMMON;
15226 }
15227
15228 asection *
15229 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15230 {
15231 return bfd_com_section_ptr;
15232 }
15233
15234 bfd_vma
15235 _bfd_elf_default_got_elt_size (bfd *abfd,
15236 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15237 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15238 bfd *ibfd ATTRIBUTE_UNUSED,
15239 unsigned long symndx ATTRIBUTE_UNUSED)
15240 {
15241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15242 return bed->s->arch_size / 8;
15243 }
15244
15245 /* Routines to support the creation of dynamic relocs. */
15246
15247 /* Returns the name of the dynamic reloc section associated with SEC. */
15248
15249 static const char *
15250 get_dynamic_reloc_section_name (bfd * abfd,
15251 asection * sec,
15252 bool is_rela)
15253 {
15254 char *name;
15255 const char *old_name = bfd_section_name (sec);
15256 const char *prefix = is_rela ? ".rela" : ".rel";
15257
15258 if (old_name == NULL)
15259 return NULL;
15260
15261 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15262 sprintf (name, "%s%s", prefix, old_name);
15263
15264 return name;
15265 }
15266
15267 /* Returns the dynamic reloc section associated with SEC.
15268 If necessary compute the name of the dynamic reloc section based
15269 on SEC's name (looked up in ABFD's string table) and the setting
15270 of IS_RELA. */
15271
15272 asection *
15273 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15274 asection *sec,
15275 bool is_rela)
15276 {
15277 asection *reloc_sec = elf_section_data (sec)->sreloc;
15278
15279 if (reloc_sec == NULL)
15280 {
15281 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15282
15283 if (name != NULL)
15284 {
15285 reloc_sec = bfd_get_linker_section (abfd, name);
15286
15287 if (reloc_sec != NULL)
15288 elf_section_data (sec)->sreloc = reloc_sec;
15289 }
15290 }
15291
15292 return reloc_sec;
15293 }
15294
15295 /* Returns the dynamic reloc section associated with SEC. If the
15296 section does not exist it is created and attached to the DYNOBJ
15297 bfd and stored in the SRELOC field of SEC's elf_section_data
15298 structure.
15299
15300 ALIGNMENT is the alignment for the newly created section and
15301 IS_RELA defines whether the name should be .rela.<SEC's name>
15302 or .rel.<SEC's name>. The section name is looked up in the
15303 string table associated with ABFD. */
15304
15305 asection *
15306 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15307 bfd *dynobj,
15308 unsigned int alignment,
15309 bfd *abfd,
15310 bool is_rela)
15311 {
15312 asection * reloc_sec = elf_section_data (sec)->sreloc;
15313
15314 if (reloc_sec == NULL)
15315 {
15316 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15317
15318 if (name == NULL)
15319 return NULL;
15320
15321 reloc_sec = bfd_get_linker_section (dynobj, name);
15322
15323 if (reloc_sec == NULL)
15324 {
15325 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15326 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15327 if ((sec->flags & SEC_ALLOC) != 0)
15328 flags |= SEC_ALLOC | SEC_LOAD;
15329
15330 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15331 if (reloc_sec != NULL)
15332 {
15333 /* _bfd_elf_get_sec_type_attr chooses a section type by
15334 name. Override as it may be wrong, eg. for a user
15335 section named "auto" we'll get ".relauto" which is
15336 seen to be a .rela section. */
15337 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15338 if (!bfd_set_section_alignment (reloc_sec, alignment))
15339 reloc_sec = NULL;
15340 }
15341 }
15342
15343 elf_section_data (sec)->sreloc = reloc_sec;
15344 }
15345
15346 return reloc_sec;
15347 }
15348
15349 /* Copy the ELF symbol type and other attributes for a linker script
15350 assignment from HSRC to HDEST. Generally this should be treated as
15351 if we found a strong non-dynamic definition for HDEST (except that
15352 ld ignores multiple definition errors). */
15353 void
15354 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15355 struct bfd_link_hash_entry *hdest,
15356 struct bfd_link_hash_entry *hsrc)
15357 {
15358 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15359 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15360 Elf_Internal_Sym isym;
15361
15362 ehdest->type = ehsrc->type;
15363 ehdest->target_internal = ehsrc->target_internal;
15364
15365 isym.st_other = ehsrc->other;
15366 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15367 }
15368
15369 /* Append a RELA relocation REL to section S in BFD. */
15370
15371 void
15372 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15373 {
15374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15375 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15376 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15377 bed->s->swap_reloca_out (abfd, rel, loc);
15378 }
15379
15380 /* Append a REL relocation REL to section S in BFD. */
15381
15382 void
15383 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15384 {
15385 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15386 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15387 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15388 bed->s->swap_reloc_out (abfd, rel, loc);
15389 }
15390
15391 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15392
15393 struct bfd_link_hash_entry *
15394 bfd_elf_define_start_stop (struct bfd_link_info *info,
15395 const char *symbol, asection *sec)
15396 {
15397 struct elf_link_hash_entry *h;
15398
15399 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15400 false, false, true);
15401 /* NB: Common symbols will be turned into definition later. */
15402 if (h != NULL
15403 && !h->root.ldscript_def
15404 && (h->root.type == bfd_link_hash_undefined
15405 || h->root.type == bfd_link_hash_undefweak
15406 || ((h->ref_regular || h->def_dynamic)
15407 && !h->def_regular
15408 && h->root.type != bfd_link_hash_common)))
15409 {
15410 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15411 h->verinfo.verdef = NULL;
15412 h->root.type = bfd_link_hash_defined;
15413 h->root.u.def.section = sec;
15414 h->root.u.def.value = 0;
15415 h->def_regular = 1;
15416 h->def_dynamic = 0;
15417 h->start_stop = 1;
15418 h->u2.start_stop_section = sec;
15419 if (symbol[0] == '.')
15420 {
15421 /* .startof. and .sizeof. symbols are local. */
15422 const struct elf_backend_data *bed;
15423 bed = get_elf_backend_data (info->output_bfd);
15424 (*bed->elf_backend_hide_symbol) (info, h, true);
15425 }
15426 else
15427 {
15428 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15429 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15430 | info->start_stop_visibility);
15431 if (was_dynamic)
15432 bfd_elf_link_record_dynamic_symbol (info, h);
15433 }
15434 return &h->root;
15435 }
15436 return NULL;
15437 }
15438
15439 /* Find dynamic relocs for H that apply to read-only sections. */
15440
15441 asection *
15442 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15443 {
15444 struct elf_dyn_relocs *p;
15445
15446 for (p = h->dyn_relocs; p != NULL; p = p->next)
15447 {
15448 asection *s = p->sec->output_section;
15449
15450 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15451 return p->sec;
15452 }
15453 return NULL;
15454 }
15455
15456 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15457 read-only sections. */
15458
15459 bool
15460 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15461 {
15462 asection *sec;
15463
15464 if (h->root.type == bfd_link_hash_indirect)
15465 return true;
15466
15467 sec = _bfd_elf_readonly_dynrelocs (h);
15468 if (sec != NULL)
15469 {
15470 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15471
15472 info->flags |= DF_TEXTREL;
15473 /* xgettext:c-format */
15474 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15475 "in read-only section `%pA'\n"),
15476 sec->owner, h->root.root.string, sec);
15477
15478 if (bfd_link_textrel_check (info))
15479 /* xgettext:c-format */
15480 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15481 "in read-only section `%pA'\n"),
15482 sec->owner, h->root.root.string, sec);
15483
15484 /* Not an error, just cut short the traversal. */
15485 return false;
15486 }
15487 return true;
15488 }
15489
15490 /* Add dynamic tags. */
15491
15492 bool
15493 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15494 bool need_dynamic_reloc)
15495 {
15496 struct elf_link_hash_table *htab = elf_hash_table (info);
15497
15498 if (htab->dynamic_sections_created)
15499 {
15500 /* Add some entries to the .dynamic section. We fill in the
15501 values later, in finish_dynamic_sections, but we must add
15502 the entries now so that we get the correct size for the
15503 .dynamic section. The DT_DEBUG entry is filled in by the
15504 dynamic linker and used by the debugger. */
15505 #define add_dynamic_entry(TAG, VAL) \
15506 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15507
15508 const struct elf_backend_data *bed
15509 = get_elf_backend_data (output_bfd);
15510
15511 if (bfd_link_executable (info))
15512 {
15513 if (!add_dynamic_entry (DT_DEBUG, 0))
15514 return false;
15515 }
15516
15517 if (htab->dt_pltgot_required || htab->splt->size != 0)
15518 {
15519 /* DT_PLTGOT is used by prelink even if there is no PLT
15520 relocation. */
15521 if (!add_dynamic_entry (DT_PLTGOT, 0))
15522 return false;
15523 }
15524
15525 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15526 {
15527 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15528 || !add_dynamic_entry (DT_PLTREL,
15529 (bed->rela_plts_and_copies_p
15530 ? DT_RELA : DT_REL))
15531 || !add_dynamic_entry (DT_JMPREL, 0))
15532 return false;
15533 }
15534
15535 if (htab->tlsdesc_plt
15536 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15537 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15538 return false;
15539
15540 if (need_dynamic_reloc)
15541 {
15542 if (bed->rela_plts_and_copies_p)
15543 {
15544 if (!add_dynamic_entry (DT_RELA, 0)
15545 || !add_dynamic_entry (DT_RELASZ, 0)
15546 || !add_dynamic_entry (DT_RELAENT,
15547 bed->s->sizeof_rela))
15548 return false;
15549 }
15550 else
15551 {
15552 if (!add_dynamic_entry (DT_REL, 0)
15553 || !add_dynamic_entry (DT_RELSZ, 0)
15554 || !add_dynamic_entry (DT_RELENT,
15555 bed->s->sizeof_rel))
15556 return false;
15557 }
15558
15559 /* If any dynamic relocs apply to a read-only section,
15560 then we need a DT_TEXTREL entry. */
15561 if ((info->flags & DF_TEXTREL) == 0)
15562 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15563 info);
15564
15565 if ((info->flags & DF_TEXTREL) != 0)
15566 {
15567 if (htab->ifunc_resolvers)
15568 info->callbacks->einfo
15569 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15570 "may result in a segfault at runtime; recompile with %s\n"),
15571 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15572
15573 if (!add_dynamic_entry (DT_TEXTREL, 0))
15574 return false;
15575 }
15576 }
15577 }
15578 #undef add_dynamic_entry
15579
15580 return true;
15581 }