]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
321e3d5e2ff614daeb20ff41b340b3a93aa0b789
[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 /* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
54
55 static bool
56 _bfd_elf_link_keep_memory (struct bfd_link_info *info)
57 {
58 #ifdef USE_MMAP
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
61
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
63
64 this is opt-in by each backend. */
65 const struct elf_backend_data *bed
66 = get_elf_backend_data (info->output_bfd);
67 if (bed->use_mmap)
68 return false;
69 #endif
70 bfd *abfd;
71 bfd_size_type size;
72
73 if (!info->keep_memory)
74 return false;
75
76 if (info->max_cache_size == (bfd_size_type) -1)
77 return true;
78
79 abfd = info->input_bfds;
80 size = info->cache_size;
81 do
82 {
83 if (size >= info->max_cache_size)
84 {
85 /* Over the limit. Reduce the memory usage. */
86 info->keep_memory = false;
87 return false;
88 }
89 if (!abfd)
90 break;
91 size += abfd->alloc_size;
92 abfd = abfd->link.next;
93 }
94 while (1);
95
96 return true;
97 }
98
99 asection *
100 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
101 unsigned long r_symndx,
102 bool discard)
103 {
104 if (r_symndx >= cookie->locsymcount
105 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
106 {
107 struct elf_link_hash_entry *h;
108
109 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
110
111 while (h->root.type == bfd_link_hash_indirect
112 || h->root.type == bfd_link_hash_warning)
113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
114
115 if ((h->root.type == bfd_link_hash_defined
116 || h->root.type == bfd_link_hash_defweak)
117 && discarded_section (h->root.u.def.section))
118 return h->root.u.def.section;
119 else
120 return NULL;
121 }
122 else
123 {
124 /* It's not a relocation against a global symbol,
125 but it could be a relocation against a local
126 symbol for a discarded section. */
127 asection *isec;
128 Elf_Internal_Sym *isym;
129
130 /* Need to: get the symbol; get the section. */
131 isym = &cookie->locsyms[r_symndx];
132 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
133 if (isec != NULL
134 && discard ? discarded_section (isec) : 1)
135 return isec;
136 }
137 return NULL;
138 }
139
140 /* Define a symbol in a dynamic linkage section. */
141
142 struct elf_link_hash_entry *
143 _bfd_elf_define_linkage_sym (bfd *abfd,
144 struct bfd_link_info *info,
145 asection *sec,
146 const char *name)
147 {
148 struct elf_link_hash_entry *h;
149 struct bfd_link_hash_entry *bh;
150 const struct elf_backend_data *bed;
151
152 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
153 if (h != NULL)
154 {
155 /* Zap symbol defined in an as-needed lib that wasn't linked.
156 This is a symptom of a larger problem: Absolute symbols
157 defined in shared libraries can't be overridden, because we
158 lose the link to the bfd which is via the symbol section. */
159 h->root.type = bfd_link_hash_new;
160 bh = &h->root;
161 }
162 else
163 bh = NULL;
164
165 bed = get_elf_backend_data (abfd);
166 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
167 sec, 0, NULL, false, bed->collect,
168 &bh))
169 return NULL;
170 h = (struct elf_link_hash_entry *) bh;
171 BFD_ASSERT (h != NULL);
172 h->def_regular = 1;
173 h->non_elf = 0;
174 h->root.linker_def = 1;
175 h->type = STT_OBJECT;
176 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
177 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
178
179 (*bed->elf_backend_hide_symbol) (info, h, true);
180 return h;
181 }
182
183 bool
184 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
185 {
186 flagword flags;
187 asection *s;
188 struct elf_link_hash_entry *h;
189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
190 struct elf_link_hash_table *htab = elf_hash_table (info);
191
192 /* This function may be called more than once. */
193 if (htab->sgot != NULL)
194 return true;
195
196 flags = bed->dynamic_sec_flags;
197
198 s = bfd_make_section_anyway_with_flags (abfd,
199 (bed->rela_plts_and_copies_p
200 ? ".rela.got" : ".rel.got"),
201 (bed->dynamic_sec_flags
202 | SEC_READONLY));
203 if (s == NULL
204 || !bfd_set_section_alignment (s, bed->s->log_file_align))
205 return false;
206 htab->srelgot = s;
207
208 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
209 if (s == NULL
210 || !bfd_set_section_alignment (s, bed->s->log_file_align))
211 return false;
212 htab->sgot = s;
213
214 if (bed->want_got_plt)
215 {
216 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
217 if (s == NULL
218 || !bfd_set_section_alignment (s, bed->s->log_file_align))
219 return false;
220 htab->sgotplt = s;
221 }
222
223 /* The first bit of the global offset table is the header. */
224 s->size += bed->got_header_size;
225
226 if (bed->want_got_sym)
227 {
228 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
229 (or .got.plt) section. We don't do this in the linker script
230 because we don't want to define the symbol if we are not creating
231 a global offset table. */
232 h = _bfd_elf_define_linkage_sym (abfd, info, s,
233 "_GLOBAL_OFFSET_TABLE_");
234 elf_hash_table (info)->hgot = h;
235 if (h == NULL)
236 return false;
237 }
238
239 return true;
240 }
241 \f
242 /* Create a strtab to hold the dynamic symbol names. */
243 static bool
244 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
245 {
246 struct elf_link_hash_table *hash_table;
247
248 hash_table = elf_hash_table (info);
249 if (hash_table->dynobj == NULL)
250 {
251 /* We may not set dynobj, an input file holding linker created
252 dynamic sections to abfd, which may be a dynamic object with
253 its own dynamic sections. We need to find a normal input file
254 to hold linker created sections if possible. */
255 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
256 {
257 bfd *ibfd;
258 asection *s;
259 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
260 if ((ibfd->flags
261 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
262 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
263 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
264 && !((s = ibfd->sections) != NULL
265 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
266 {
267 abfd = ibfd;
268 break;
269 }
270 }
271 hash_table->dynobj = abfd;
272 }
273
274 if (hash_table->dynstr == NULL)
275 {
276 hash_table->dynstr = _bfd_elf_strtab_init ();
277 if (hash_table->dynstr == NULL)
278 return false;
279 }
280 return true;
281 }
282
283 /* Create some sections which will be filled in with dynamic linking
284 information. ABFD is an input file which requires dynamic sections
285 to be created. The dynamic sections take up virtual memory space
286 when the final executable is run, so we need to create them before
287 addresses are assigned to the output sections. We work out the
288 actual contents and size of these sections later. */
289
290 bool
291 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
292 {
293 flagword flags;
294 asection *s;
295 const struct elf_backend_data *bed;
296 struct elf_link_hash_entry *h;
297
298 if (! is_elf_hash_table (info->hash))
299 return false;
300
301 if (elf_hash_table (info)->dynamic_sections_created)
302 return true;
303
304 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
305 return false;
306
307 abfd = elf_hash_table (info)->dynobj;
308 bed = get_elf_backend_data (abfd);
309
310 flags = bed->dynamic_sec_flags;
311
312 /* A dynamically linked executable has a .interp section, but a
313 shared library does not. */
314 if (bfd_link_executable (info) && !info->nointerp)
315 {
316 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
317 flags | SEC_READONLY);
318 if (s == NULL)
319 return false;
320 }
321
322 /* Create sections to hold version informations. These are removed
323 if they are not needed. */
324 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
325 flags | SEC_READONLY);
326 if (s == NULL
327 || !bfd_set_section_alignment (s, bed->s->log_file_align))
328 return false;
329
330 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
331 flags | SEC_READONLY);
332 if (s == NULL
333 || !bfd_set_section_alignment (s, 1))
334 return false;
335
336 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
337 flags | SEC_READONLY);
338 if (s == NULL
339 || !bfd_set_section_alignment (s, bed->s->log_file_align))
340 return false;
341
342 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
343 flags | SEC_READONLY);
344 if (s == NULL
345 || !bfd_set_section_alignment (s, bed->s->log_file_align))
346 return false;
347 elf_hash_table (info)->dynsym = s;
348
349 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
350 flags | SEC_READONLY);
351 if (s == NULL)
352 return false;
353
354 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
355 if (s == NULL
356 || !bfd_set_section_alignment (s, bed->s->log_file_align))
357 return false;
358 elf_hash_table (info)->dynamic = s;
359
360 /* The special symbol _DYNAMIC is always set to the start of the
361 .dynamic section. We could set _DYNAMIC in a linker script, but we
362 only want to define it if we are, in fact, creating a .dynamic
363 section. We don't want to define it if there is no .dynamic
364 section, since on some ELF platforms the start up code examines it
365 to decide how to initialize the process. */
366 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
367 elf_hash_table (info)->hdynamic = h;
368 if (h == NULL)
369 return false;
370
371 if (info->emit_hash)
372 {
373 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
374 flags | SEC_READONLY);
375 if (s == NULL
376 || !bfd_set_section_alignment (s, bed->s->log_file_align))
377 return false;
378 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
379 }
380
381 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
382 {
383 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
384 flags | SEC_READONLY);
385 if (s == NULL
386 || !bfd_set_section_alignment (s, bed->s->log_file_align))
387 return false;
388 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
389 4 32-bit words followed by variable count of 64-bit words, then
390 variable count of 32-bit words. */
391 if (bed->s->arch_size == 64)
392 elf_section_data (s)->this_hdr.sh_entsize = 0;
393 else
394 elf_section_data (s)->this_hdr.sh_entsize = 4;
395 }
396
397 if (info->enable_dt_relr)
398 {
399 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
400 (bed->dynamic_sec_flags
401 | SEC_READONLY));
402 if (s == NULL
403 || !bfd_set_section_alignment (s, bed->s->log_file_align))
404 return false;
405 elf_hash_table (info)->srelrdyn = s;
406 }
407
408 /* Let the backend create the rest of the sections. This lets the
409 backend set the right flags. The backend will normally create
410 the .got and .plt sections. */
411 if (bed->elf_backend_create_dynamic_sections == NULL
412 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
413 return false;
414
415 elf_hash_table (info)->dynamic_sections_created = true;
416
417 return true;
418 }
419
420 /* Create dynamic sections when linking against a dynamic object. */
421
422 bool
423 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
424 {
425 flagword flags, pltflags;
426 struct elf_link_hash_entry *h;
427 asection *s;
428 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
429 struct elf_link_hash_table *htab = elf_hash_table (info);
430
431 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
432 .rel[a].bss sections. */
433 flags = bed->dynamic_sec_flags;
434
435 pltflags = flags;
436 if (bed->plt_not_loaded)
437 /* We do not clear SEC_ALLOC here because we still want the OS to
438 allocate space for the section; it's just that there's nothing
439 to read in from the object file. */
440 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
441 else
442 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
443 if (bed->plt_readonly)
444 pltflags |= SEC_READONLY;
445
446 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
447 if (s == NULL
448 || !bfd_set_section_alignment (s, bed->plt_alignment))
449 return false;
450 htab->splt = s;
451
452 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
453 .plt section. */
454 if (bed->want_plt_sym)
455 {
456 h = _bfd_elf_define_linkage_sym (abfd, info, s,
457 "_PROCEDURE_LINKAGE_TABLE_");
458 elf_hash_table (info)->hplt = h;
459 if (h == NULL)
460 return false;
461 }
462
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.plt" : ".rel.plt"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return false;
470 htab->srelplt = s;
471
472 if (! _bfd_elf_create_got_section (abfd, info))
473 return false;
474
475 if (bed->want_dynbss)
476 {
477 /* The .dynbss section is a place to put symbols which are defined
478 by dynamic objects, are referenced by regular objects, and are
479 not functions. We must allocate space for them in the process
480 image and use a R_*_COPY reloc to tell the dynamic linker to
481 initialize them at run time. The linker script puts the .dynbss
482 section into the .bss section of the final image. */
483 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
484 SEC_ALLOC | SEC_LINKER_CREATED);
485 if (s == NULL)
486 return false;
487 htab->sdynbss = s;
488
489 if (bed->want_dynrelro)
490 {
491 /* Similarly, but for symbols that were originally in read-only
492 sections. This section doesn't really need to have contents,
493 but make it like other .data.rel.ro sections. */
494 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
495 flags);
496 if (s == NULL)
497 return false;
498 htab->sdynrelro = s;
499 }
500
501 /* The .rel[a].bss section holds copy relocs. This section is not
502 normally needed. We need to create it here, though, so that the
503 linker will map it to an output section. We can't just create it
504 only if we need it, because we will not know whether we need it
505 until we have seen all the input files, and the first time the
506 main linker code calls BFD after examining all the input files
507 (size_dynamic_sections) the input sections have already been
508 mapped to the output sections. If the section turns out not to
509 be needed, we can discard it later. We will never need this
510 section when generating a shared object, since they do not use
511 copy relocs. */
512 if (bfd_link_executable (info))
513 {
514 s = bfd_make_section_anyway_with_flags (abfd,
515 (bed->rela_plts_and_copies_p
516 ? ".rela.bss" : ".rel.bss"),
517 flags | SEC_READONLY);
518 if (s == NULL
519 || !bfd_set_section_alignment (s, bed->s->log_file_align))
520 return false;
521 htab->srelbss = s;
522
523 if (bed->want_dynrelro)
524 {
525 s = (bfd_make_section_anyway_with_flags
526 (abfd, (bed->rela_plts_and_copies_p
527 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
528 flags | SEC_READONLY));
529 if (s == NULL
530 || !bfd_set_section_alignment (s, bed->s->log_file_align))
531 return false;
532 htab->sreldynrelro = s;
533 }
534 }
535 }
536
537 return true;
538 }
539 \f
540 /* Record a new dynamic symbol. We record the dynamic symbols as we
541 read the input files, since we need to have a list of all of them
542 before we can determine the final sizes of the output sections.
543 Note that we may actually call this function even though we are not
544 going to output any dynamic symbols; in some cases we know that a
545 symbol should be in the dynamic symbol table, but only if there is
546 one. */
547
548 bool
549 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
550 struct elf_link_hash_entry *h)
551 {
552 if (h->dynindx == -1)
553 {
554 struct elf_strtab_hash *dynstr;
555 char *p;
556 const char *name;
557 size_t indx;
558
559 if (h->root.type == bfd_link_hash_defined
560 || h->root.type == bfd_link_hash_defweak)
561 {
562 /* An IR symbol should not be made dynamic. */
563 if (h->root.u.def.section != NULL
564 && h->root.u.def.section->owner != NULL
565 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
566 return true;
567 }
568
569 /* XXX: The ABI draft says the linker must turn hidden and
570 internal symbols into STB_LOCAL symbols when producing the
571 DSO. However, if ld.so honors st_other in the dynamic table,
572 this would not be necessary. */
573 switch (ELF_ST_VISIBILITY (h->other))
574 {
575 case STV_INTERNAL:
576 case STV_HIDDEN:
577 if (h->root.type != bfd_link_hash_undefined
578 && h->root.type != bfd_link_hash_undefweak)
579 {
580 h->forced_local = 1;
581 return true;
582 }
583
584 default:
585 break;
586 }
587
588 h->dynindx = elf_hash_table (info)->dynsymcount;
589 ++elf_hash_table (info)->dynsymcount;
590
591 dynstr = elf_hash_table (info)->dynstr;
592 if (dynstr == NULL)
593 {
594 /* Create a strtab to hold the dynamic symbol names. */
595 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
596 if (dynstr == NULL)
597 return false;
598 }
599
600 char *unversioned_name = NULL;
601
602 /* We don't put any version information in the dynamic string
603 table. */
604 name = h->root.root.string;
605 p = strchr (name, ELF_VER_CHR);
606 if (p != NULL)
607 {
608 unversioned_name = bfd_malloc (p - name + 1);
609 memcpy (unversioned_name, name, p - name);
610 unversioned_name[p - name] = 0;
611 name = unversioned_name;
612 }
613
614 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
615
616 if (p != NULL)
617 free (unversioned_name);
618
619 if (indx == (size_t) -1)
620 return false;
621 h->dynstr_index = indx;
622 }
623
624 return true;
625 }
626 \f
627 /* Mark a symbol dynamic. */
628
629 static void
630 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
631 struct elf_link_hash_entry *h,
632 Elf_Internal_Sym *sym)
633 {
634 struct bfd_elf_dynamic_list *d = info->dynamic_list;
635
636 /* It may be called more than once on the same H. */
637 if(h->dynamic || bfd_link_relocatable (info))
638 return;
639
640 if ((info->dynamic_data
641 && (h->type == STT_OBJECT
642 || h->type == STT_COMMON
643 || (sym != NULL
644 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
645 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
646 || (d != NULL
647 && h->non_elf
648 && (*d->match) (&d->head, NULL, h->root.root.string)))
649 {
650 h->dynamic = 1;
651 /* NB: If a symbol is made dynamic by --dynamic-list, it has
652 non-IR reference. */
653 h->root.non_ir_ref_dynamic = 1;
654 }
655 }
656
657 /* Record an assignment to a symbol made by a linker script. We need
658 this in case some dynamic object refers to this symbol. */
659
660 bool
661 bfd_elf_record_link_assignment (bfd *output_bfd,
662 struct bfd_link_info *info,
663 const char *name,
664 bool provide,
665 bool hidden)
666 {
667 struct elf_link_hash_entry *h, *hv;
668 struct elf_link_hash_table *htab;
669 const struct elf_backend_data *bed;
670
671 if (!is_elf_hash_table (info->hash))
672 return true;
673
674 htab = elf_hash_table (info);
675 h = elf_link_hash_lookup (htab, name, !provide, true, false);
676 if (h == NULL)
677 return provide;
678
679 if (h->root.type == bfd_link_hash_warning)
680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
681
682 if (h->versioned == unknown)
683 {
684 /* Set versioned if symbol version is unknown. */
685 char *version = strrchr (name, ELF_VER_CHR);
686 if (version)
687 {
688 if (version > name && version[-1] != ELF_VER_CHR)
689 h->versioned = versioned_hidden;
690 else
691 h->versioned = versioned;
692 }
693 }
694
695 /* Symbols defined in a linker script but not referenced anywhere
696 else will have non_elf set. */
697 if (h->non_elf)
698 {
699 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
700 h->non_elf = 0;
701 }
702
703 switch (h->root.type)
704 {
705 case bfd_link_hash_defined:
706 case bfd_link_hash_defweak:
707 case bfd_link_hash_common:
708 break;
709 case bfd_link_hash_undefweak:
710 case bfd_link_hash_undefined:
711 /* Since we're defining the symbol, don't let it seem to have not
712 been defined. record_dynamic_symbol and size_dynamic_sections
713 may depend on this. */
714 h->root.type = bfd_link_hash_new;
715 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
716 bfd_link_repair_undef_list (&htab->root);
717 break;
718 case bfd_link_hash_new:
719 break;
720 case bfd_link_hash_indirect:
721 /* We had a versioned symbol in a dynamic library. We make the
722 the versioned symbol point to this one. */
723 bed = get_elf_backend_data (output_bfd);
724 hv = h;
725 while (hv->root.type == bfd_link_hash_indirect
726 || hv->root.type == bfd_link_hash_warning)
727 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
728 /* We don't need to update h->root.u since linker will set them
729 later. */
730 h->root.type = bfd_link_hash_undefined;
731 hv->root.type = bfd_link_hash_indirect;
732 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
733 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
734 break;
735 default:
736 BFD_FAIL ();
737 return false;
738 }
739
740 /* If this symbol is being provided by the linker script, and it is
741 currently defined by a dynamic object, but not by a regular
742 object, then mark it as undefined so that the generic linker will
743 force the correct value. */
744 if (provide
745 && h->def_dynamic
746 && !h->def_regular)
747 h->root.type = bfd_link_hash_undefined;
748
749 /* If this symbol is currently defined by a dynamic object, but not
750 by a regular object, then clear out any version information because
751 the symbol will not be associated with the dynamic object any
752 more. */
753 if (h->def_dynamic && !h->def_regular)
754 h->verinfo.verdef = NULL;
755
756 /* Make sure this symbol is not garbage collected. */
757 h->mark = 1;
758
759 h->def_regular = 1;
760
761 if (hidden)
762 {
763 bed = get_elf_backend_data (output_bfd);
764 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
765 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
766 (*bed->elf_backend_hide_symbol) (info, h, true);
767 }
768
769 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
770 and executables. */
771 if (!bfd_link_relocatable (info)
772 && h->dynindx != -1
773 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
774 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
775 h->forced_local = 1;
776
777 if ((h->def_dynamic
778 || h->ref_dynamic
779 || bfd_link_dll (info))
780 && !h->forced_local
781 && h->dynindx == -1)
782 {
783 if (! bfd_elf_link_record_dynamic_symbol (info, h))
784 return false;
785
786 /* If this is a weak defined symbol, and we know a corresponding
787 real symbol from the same dynamic object, make sure the real
788 symbol is also made into a dynamic symbol. */
789 if (h->is_weakalias)
790 {
791 struct elf_link_hash_entry *def = weakdef (h);
792
793 if (def->dynindx == -1
794 && !bfd_elf_link_record_dynamic_symbol (info, def))
795 return false;
796 }
797 }
798
799 return true;
800 }
801
802 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
803 success, and 2 on a failure caused by attempting to record a symbol
804 in a discarded section, eg. a discarded link-once section symbol. */
805
806 int
807 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
808 bfd *input_bfd,
809 long input_indx)
810 {
811 size_t amt;
812 struct elf_link_local_dynamic_entry *entry;
813 struct elf_link_hash_table *eht;
814 struct elf_strtab_hash *dynstr;
815 size_t dynstr_index;
816 char *name;
817 Elf_External_Sym_Shndx eshndx;
818 char esym[sizeof (Elf64_External_Sym)];
819
820 if (! is_elf_hash_table (info->hash))
821 return 0;
822
823 /* See if the entry exists already. */
824 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
825 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
826 return 1;
827
828 amt = sizeof (*entry);
829 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
830 if (entry == NULL)
831 return 0;
832
833 /* Go find the symbol, so that we can find it's name. */
834 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
835 1, input_indx, &entry->isym, esym, &eshndx))
836 {
837 bfd_release (input_bfd, entry);
838 return 0;
839 }
840
841 if (entry->isym.st_shndx != SHN_UNDEF
842 && entry->isym.st_shndx < SHN_LORESERVE)
843 {
844 asection *s;
845
846 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
847 if (s == NULL || bfd_is_abs_section (s->output_section))
848 {
849 /* We can still bfd_release here as nothing has done another
850 bfd_alloc. We can't do this later in this function. */
851 bfd_release (input_bfd, entry);
852 return 2;
853 }
854 }
855
856 name = (bfd_elf_string_from_elf_section
857 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
858 entry->isym.st_name));
859
860 dynstr = elf_hash_table (info)->dynstr;
861 if (dynstr == NULL)
862 {
863 /* Create a strtab to hold the dynamic symbol names. */
864 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
865 if (dynstr == NULL)
866 return 0;
867 }
868
869 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
870 if (dynstr_index == (size_t) -1)
871 return 0;
872 entry->isym.st_name = dynstr_index;
873
874 eht = elf_hash_table (info);
875
876 entry->next = eht->dynlocal;
877 eht->dynlocal = entry;
878 entry->input_bfd = input_bfd;
879 entry->input_indx = input_indx;
880 eht->dynsymcount++;
881
882 /* Whatever binding the symbol had before, it's now local. */
883 entry->isym.st_info
884 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
885
886 /* The dynindx will be set at the end of size_dynamic_sections. */
887
888 return 1;
889 }
890
891 /* Return the dynindex of a local dynamic symbol. */
892
893 long
894 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
895 bfd *input_bfd,
896 long input_indx)
897 {
898 struct elf_link_local_dynamic_entry *e;
899
900 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
901 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
902 return e->dynindx;
903 return -1;
904 }
905
906 /* This function is used to renumber the dynamic symbols, if some of
907 them are removed because they are marked as local. This is called
908 via elf_link_hash_traverse. */
909
910 static bool
911 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
912 void *data)
913 {
914 size_t *count = (size_t *) data;
915
916 if (h->forced_local)
917 return true;
918
919 if (h->dynindx != -1)
920 h->dynindx = ++(*count);
921
922 return true;
923 }
924
925
926 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
927 STB_LOCAL binding. */
928
929 static bool
930 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
931 void *data)
932 {
933 size_t *count = (size_t *) data;
934
935 if (!h->forced_local)
936 return true;
937
938 if (h->dynindx != -1)
939 h->dynindx = ++(*count);
940
941 return true;
942 }
943
944 /* Return true if the dynamic symbol for a given section should be
945 omitted when creating a shared library. */
946 bool
947 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
948 struct bfd_link_info *info,
949 asection *p)
950 {
951 struct elf_link_hash_table *htab;
952 asection *ip;
953
954 switch (elf_section_data (p)->this_hdr.sh_type)
955 {
956 case SHT_PROGBITS:
957 case SHT_NOBITS:
958 /* If sh_type is yet undecided, assume it could be
959 SHT_PROGBITS/SHT_NOBITS. */
960 case SHT_NULL:
961 htab = elf_hash_table (info);
962 if (htab->text_index_section != NULL)
963 return p != htab->text_index_section && p != htab->data_index_section;
964
965 return (htab->dynobj != NULL
966 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
967 && ip->output_section == p);
968
969 /* There shouldn't be section relative relocations
970 against any other section. */
971 default:
972 return true;
973 }
974 }
975
976 bool
977 _bfd_elf_omit_section_dynsym_all
978 (bfd *output_bfd ATTRIBUTE_UNUSED,
979 struct bfd_link_info *info ATTRIBUTE_UNUSED,
980 asection *p ATTRIBUTE_UNUSED)
981 {
982 return true;
983 }
984
985 /* Assign dynsym indices. In a shared library we generate a section
986 symbol for each output section, which come first. Next come symbols
987 which have been forced to local binding. Then all of the back-end
988 allocated local dynamic syms, followed by the rest of the global
989 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
990 (This prevents the early call before elf_backend_init_index_section
991 and strip_excluded_output_sections setting dynindx for sections
992 that are stripped.) */
993
994 static unsigned long
995 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
996 struct bfd_link_info *info,
997 unsigned long *section_sym_count)
998 {
999 unsigned long dynsymcount = 0;
1000 bool do_sec = section_sym_count != NULL;
1001
1002 if (bfd_link_pic (info)
1003 || elf_hash_table (info)->is_relocatable_executable)
1004 {
1005 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1006 asection *p;
1007 for (p = output_bfd->sections; p ; p = p->next)
1008 if ((p->flags & SEC_EXCLUDE) == 0
1009 && (p->flags & SEC_ALLOC) != 0
1010 && elf_hash_table (info)->dynamic_relocs
1011 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
1012 {
1013 ++dynsymcount;
1014 if (do_sec)
1015 elf_section_data (p)->dynindx = dynsymcount;
1016 }
1017 else if (do_sec)
1018 elf_section_data (p)->dynindx = 0;
1019 }
1020 if (do_sec)
1021 *section_sym_count = dynsymcount;
1022
1023 elf_link_hash_traverse (elf_hash_table (info),
1024 elf_link_renumber_local_hash_table_dynsyms,
1025 &dynsymcount);
1026
1027 if (elf_hash_table (info)->dynlocal)
1028 {
1029 struct elf_link_local_dynamic_entry *p;
1030 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1031 p->dynindx = ++dynsymcount;
1032 }
1033 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1034
1035 elf_link_hash_traverse (elf_hash_table (info),
1036 elf_link_renumber_hash_table_dynsyms,
1037 &dynsymcount);
1038
1039 /* There is an unused NULL entry at the head of the table which we
1040 must account for in our count even if the table is empty since it
1041 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1042 .dynamic section. */
1043 dynsymcount++;
1044
1045 elf_hash_table (info)->dynsymcount = dynsymcount;
1046 return dynsymcount;
1047 }
1048
1049 /* Merge st_other field. */
1050
1051 static void
1052 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1053 unsigned int st_other, asection *sec,
1054 bool definition, bool dynamic)
1055 {
1056 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1057
1058 /* If st_other has a processor-specific meaning, specific
1059 code might be needed here. */
1060 if (bed->elf_backend_merge_symbol_attribute)
1061 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1062 dynamic);
1063
1064 if (!dynamic)
1065 {
1066 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1067 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1068
1069 /* Keep the most constraining visibility. Leave the remainder
1070 of the st_other field to elf_backend_merge_symbol_attribute. */
1071 if (symvis - 1 < hvis - 1)
1072 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1073 }
1074 else if (definition
1075 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1076 && (sec->flags & SEC_READONLY) == 0)
1077 h->protected_def = 1;
1078 }
1079
1080 /* This function is called when we want to merge a new symbol with an
1081 existing symbol. It handles the various cases which arise when we
1082 find a definition in a dynamic object, or when there is already a
1083 definition in a dynamic object. The new symbol is described by
1084 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1085 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1086 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1087 of an old common symbol. We set OVERRIDE if the old symbol is
1088 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1089 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1090 to change. By OK to change, we mean that we shouldn't warn if the
1091 type or size does change. */
1092
1093 static bool
1094 _bfd_elf_merge_symbol (bfd *abfd,
1095 struct bfd_link_info *info,
1096 const char *name,
1097 Elf_Internal_Sym *sym,
1098 asection **psec,
1099 bfd_vma *pvalue,
1100 struct elf_link_hash_entry **sym_hash,
1101 bfd **poldbfd,
1102 bool *pold_weak,
1103 unsigned int *pold_alignment,
1104 bool *skip,
1105 bfd **override,
1106 bool *type_change_ok,
1107 bool *size_change_ok,
1108 bool *matched)
1109 {
1110 asection *sec, *oldsec;
1111 struct elf_link_hash_entry *h;
1112 struct elf_link_hash_entry *hi;
1113 struct elf_link_hash_entry *flip;
1114 int bind;
1115 bfd *oldbfd;
1116 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1117 bool newweak, oldweak, newfunc, oldfunc;
1118 const struct elf_backend_data *bed;
1119 char *new_version;
1120 bool default_sym = *matched;
1121 struct elf_link_hash_table *htab;
1122
1123 *skip = false;
1124 *override = NULL;
1125
1126 sec = *psec;
1127 bind = ELF_ST_BIND (sym->st_info);
1128
1129 if (! bfd_is_und_section (sec))
1130 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1131 else
1132 h = ((struct elf_link_hash_entry *)
1133 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1134 if (h == NULL)
1135 return false;
1136 *sym_hash = h;
1137
1138 bed = get_elf_backend_data (abfd);
1139
1140 /* NEW_VERSION is the symbol version of the new symbol. */
1141 if (h->versioned != unversioned)
1142 {
1143 /* Symbol version is unknown or versioned. */
1144 new_version = strrchr (name, ELF_VER_CHR);
1145 if (new_version)
1146 {
1147 if (h->versioned == unknown)
1148 {
1149 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1150 h->versioned = versioned_hidden;
1151 else
1152 h->versioned = versioned;
1153 }
1154 new_version += 1;
1155 if (new_version[0] == '\0')
1156 new_version = NULL;
1157 }
1158 else
1159 h->versioned = unversioned;
1160 }
1161 else
1162 new_version = NULL;
1163
1164 /* For merging, we only care about real symbols. But we need to make
1165 sure that indirect symbol dynamic flags are updated. */
1166 hi = h;
1167 while (h->root.type == bfd_link_hash_indirect
1168 || h->root.type == bfd_link_hash_warning)
1169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1170
1171 if (!*matched)
1172 {
1173 if (hi == h || h->root.type == bfd_link_hash_new)
1174 *matched = true;
1175 else
1176 {
1177 /* OLD_HIDDEN is true if the existing symbol is only visible
1178 to the symbol with the same symbol version. NEW_HIDDEN is
1179 true if the new symbol is only visible to the symbol with
1180 the same symbol version. */
1181 bool old_hidden = h->versioned == versioned_hidden;
1182 bool new_hidden = hi->versioned == versioned_hidden;
1183 if (!old_hidden && !new_hidden)
1184 /* The new symbol matches the existing symbol if both
1185 aren't hidden. */
1186 *matched = true;
1187 else
1188 {
1189 /* OLD_VERSION is the symbol version of the existing
1190 symbol. */
1191 char *old_version;
1192
1193 if (h->versioned >= versioned)
1194 old_version = strrchr (h->root.root.string,
1195 ELF_VER_CHR) + 1;
1196 else
1197 old_version = NULL;
1198
1199 /* The new symbol matches the existing symbol if they
1200 have the same symbol version. */
1201 *matched = (old_version == new_version
1202 || (old_version != NULL
1203 && new_version != NULL
1204 && strcmp (old_version, new_version) == 0));
1205 }
1206 }
1207 }
1208
1209 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1210 existing symbol. */
1211
1212 oldbfd = NULL;
1213 oldsec = NULL;
1214 switch (h->root.type)
1215 {
1216 default:
1217 break;
1218
1219 case bfd_link_hash_undefined:
1220 case bfd_link_hash_undefweak:
1221 oldbfd = h->root.u.undef.abfd;
1222 break;
1223
1224 case bfd_link_hash_defined:
1225 case bfd_link_hash_defweak:
1226 oldbfd = h->root.u.def.section->owner;
1227 oldsec = h->root.u.def.section;
1228 break;
1229
1230 case bfd_link_hash_common:
1231 oldbfd = h->root.u.c.p->section->owner;
1232 oldsec = h->root.u.c.p->section;
1233 if (pold_alignment)
1234 *pold_alignment = h->root.u.c.p->alignment_power;
1235 break;
1236 }
1237 if (poldbfd && *poldbfd == NULL)
1238 *poldbfd = oldbfd;
1239
1240 /* Differentiate strong and weak symbols. */
1241 newweak = bind == STB_WEAK;
1242 oldweak = (h->root.type == bfd_link_hash_defweak
1243 || h->root.type == bfd_link_hash_undefweak);
1244 if (pold_weak)
1245 *pold_weak = oldweak;
1246
1247 /* We have to check it for every instance since the first few may be
1248 references and not all compilers emit symbol type for undefined
1249 symbols. */
1250 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1251
1252 htab = elf_hash_table (info);
1253
1254 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1255 respectively, is from a dynamic object. */
1256
1257 newdyn = (abfd->flags & DYNAMIC) != 0;
1258
1259 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1260 syms and defined syms in dynamic libraries respectively.
1261 ref_dynamic on the other hand can be set for a symbol defined in
1262 a dynamic library, and def_dynamic may not be set; When the
1263 definition in a dynamic lib is overridden by a definition in the
1264 executable use of the symbol in the dynamic lib becomes a
1265 reference to the executable symbol. */
1266 if (newdyn)
1267 {
1268 if (bfd_is_und_section (sec))
1269 {
1270 if (bind != STB_WEAK)
1271 {
1272 h->ref_dynamic_nonweak = 1;
1273 hi->ref_dynamic_nonweak = 1;
1274 }
1275 }
1276 else
1277 {
1278 /* Update the existing symbol only if they match. */
1279 if (*matched)
1280 h->dynamic_def = 1;
1281 hi->dynamic_def = 1;
1282 }
1283 }
1284
1285 /* If we just created the symbol, mark it as being an ELF symbol.
1286 Other than that, there is nothing to do--there is no merge issue
1287 with a newly defined symbol--so we just return. */
1288
1289 if (h->root.type == bfd_link_hash_new)
1290 {
1291 h->non_elf = 0;
1292 return true;
1293 }
1294
1295 /* In cases involving weak versioned symbols, we may wind up trying
1296 to merge a symbol with itself. Catch that here, to avoid the
1297 confusion that results if we try to override a symbol with
1298 itself. The additional tests catch cases like
1299 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1300 dynamic object, which we do want to handle here. */
1301 if (abfd == oldbfd
1302 && (newweak || oldweak)
1303 && ((abfd->flags & DYNAMIC) == 0
1304 || !h->def_regular))
1305 return true;
1306
1307 olddyn = false;
1308 if (oldbfd != NULL)
1309 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1310 else if (oldsec != NULL)
1311 {
1312 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1313 indices used by MIPS ELF. */
1314 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1315 }
1316
1317 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1318 if (!htab->handling_dt_needed
1319 && oldbfd != NULL
1320 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1321 {
1322 if (newdyn != olddyn)
1323 {
1324 /* Handle a case where plugin_notice won't be called and thus
1325 won't set the non_ir_ref flags on the first pass over
1326 symbols. */
1327 h->root.non_ir_ref_dynamic = true;
1328 hi->root.non_ir_ref_dynamic = true;
1329 }
1330 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1331 && hi->root.type == bfd_link_hash_indirect)
1332 {
1333 /* Change indirect symbol from IR to undefined. */
1334 hi->root.type = bfd_link_hash_undefined;
1335 hi->root.u.undef.abfd = oldbfd;
1336 }
1337 }
1338
1339 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1340 respectively, appear to be a definition rather than reference. */
1341
1342 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1343
1344 olddef = (h->root.type != bfd_link_hash_undefined
1345 && h->root.type != bfd_link_hash_undefweak
1346 && h->root.type != bfd_link_hash_common);
1347
1348 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1349 respectively, appear to be a function. */
1350
1351 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1352 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1353
1354 oldfunc = (h->type != STT_NOTYPE
1355 && bed->is_function_type (h->type));
1356
1357 if (!(newfunc && oldfunc)
1358 && ELF_ST_TYPE (sym->st_info) != h->type
1359 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1360 && h->type != STT_NOTYPE
1361 && (newdef || bfd_is_com_section (sec))
1362 && (olddef || h->root.type == bfd_link_hash_common))
1363 {
1364 /* If creating a default indirect symbol ("foo" or "foo@") from
1365 a dynamic versioned definition ("foo@@") skip doing so if
1366 there is an existing regular definition with a different
1367 type. We don't want, for example, a "time" variable in the
1368 executable overriding a "time" function in a shared library. */
1369 if (newdyn
1370 && !olddyn)
1371 {
1372 *skip = true;
1373 return true;
1374 }
1375
1376 /* When adding a symbol from a regular object file after we have
1377 created indirect symbols, undo the indirection and any
1378 dynamic state. */
1379 if (hi != h
1380 && !newdyn
1381 && olddyn)
1382 {
1383 h = hi;
1384 (*bed->elf_backend_hide_symbol) (info, h, true);
1385 h->forced_local = 0;
1386 h->ref_dynamic = 0;
1387 h->def_dynamic = 0;
1388 h->dynamic_def = 0;
1389 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1390 {
1391 h->root.type = bfd_link_hash_undefined;
1392 h->root.u.undef.abfd = abfd;
1393 }
1394 else
1395 {
1396 h->root.type = bfd_link_hash_new;
1397 h->root.u.undef.abfd = NULL;
1398 }
1399 return true;
1400 }
1401 }
1402
1403 /* Check TLS symbols. We don't check undefined symbols introduced
1404 by "ld -u" which have no type (and oldbfd NULL), and we don't
1405 check symbols from plugins because they also have no type. */
1406 if (oldbfd != NULL
1407 && (oldbfd->flags & BFD_PLUGIN) == 0
1408 && (abfd->flags & BFD_PLUGIN) == 0
1409 && ELF_ST_TYPE (sym->st_info) != h->type
1410 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1411 {
1412 bfd *ntbfd, *tbfd;
1413 bool ntdef, tdef;
1414 asection *ntsec, *tsec;
1415
1416 if (h->type == STT_TLS)
1417 {
1418 ntbfd = abfd;
1419 ntsec = sec;
1420 ntdef = newdef;
1421 tbfd = oldbfd;
1422 tsec = oldsec;
1423 tdef = olddef;
1424 }
1425 else
1426 {
1427 ntbfd = oldbfd;
1428 ntsec = oldsec;
1429 ntdef = olddef;
1430 tbfd = abfd;
1431 tsec = sec;
1432 tdef = newdef;
1433 }
1434
1435 if (tdef && ntdef)
1436 _bfd_error_handler
1437 /* xgettext:c-format */
1438 (_("%s: TLS definition in %pB section %pA "
1439 "mismatches non-TLS definition in %pB section %pA"),
1440 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1441 else if (!tdef && !ntdef)
1442 _bfd_error_handler
1443 /* xgettext:c-format */
1444 (_("%s: TLS reference in %pB "
1445 "mismatches non-TLS reference in %pB"),
1446 h->root.root.string, tbfd, ntbfd);
1447 else if (tdef)
1448 _bfd_error_handler
1449 /* xgettext:c-format */
1450 (_("%s: TLS definition in %pB section %pA "
1451 "mismatches non-TLS reference in %pB"),
1452 h->root.root.string, tbfd, tsec, ntbfd);
1453 else
1454 _bfd_error_handler
1455 /* xgettext:c-format */
1456 (_("%s: TLS reference in %pB "
1457 "mismatches non-TLS definition in %pB section %pA"),
1458 h->root.root.string, tbfd, ntbfd, ntsec);
1459
1460 bfd_set_error (bfd_error_bad_value);
1461 return false;
1462 }
1463
1464 /* If the old symbol has non-default visibility, we ignore the new
1465 definition from a dynamic object. */
1466 if (newdyn
1467 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1468 && !bfd_is_und_section (sec))
1469 {
1470 *skip = true;
1471 /* Make sure this symbol is dynamic. */
1472 h->ref_dynamic = 1;
1473 hi->ref_dynamic = 1;
1474 /* A protected symbol has external availability. Make sure it is
1475 recorded as dynamic.
1476
1477 FIXME: Should we check type and size for protected symbol? */
1478 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1479 return bfd_elf_link_record_dynamic_symbol (info, h);
1480 else
1481 return true;
1482 }
1483 else if (!newdyn
1484 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1485 && h->def_dynamic)
1486 {
1487 /* If the new symbol with non-default visibility comes from a
1488 relocatable file and the old definition comes from a dynamic
1489 object, we remove the old definition. */
1490 if (hi->root.type == bfd_link_hash_indirect)
1491 {
1492 /* Handle the case where the old dynamic definition is
1493 default versioned. We need to copy the symbol info from
1494 the symbol with default version to the normal one if it
1495 was referenced before. */
1496 if (h->ref_regular)
1497 {
1498 hi->root.type = h->root.type;
1499 h->root.type = bfd_link_hash_indirect;
1500 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1501
1502 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1503 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1504 {
1505 /* If the new symbol is hidden or internal, completely undo
1506 any dynamic link state. */
1507 (*bed->elf_backend_hide_symbol) (info, h, true);
1508 h->forced_local = 0;
1509 h->ref_dynamic = 0;
1510 }
1511 else
1512 h->ref_dynamic = 1;
1513
1514 h->def_dynamic = 0;
1515 /* FIXME: Should we check type and size for protected symbol? */
1516 h->size = 0;
1517 h->type = 0;
1518
1519 h = hi;
1520 }
1521 else
1522 h = hi;
1523 }
1524
1525 /* If the old symbol was undefined before, then it will still be
1526 on the undefs list. If the new symbol is undefined or
1527 common, we can't make it bfd_link_hash_new here, because new
1528 undefined or common symbols will be added to the undefs list
1529 by _bfd_generic_link_add_one_symbol. Symbols may not be
1530 added twice to the undefs list. Also, if the new symbol is
1531 undefweak then we don't want to lose the strong undef. */
1532 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1533 {
1534 h->root.type = bfd_link_hash_undefined;
1535 h->root.u.undef.abfd = abfd;
1536 }
1537 else
1538 {
1539 h->root.type = bfd_link_hash_new;
1540 h->root.u.undef.abfd = NULL;
1541 }
1542
1543 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1544 {
1545 /* If the new symbol is hidden or internal, completely undo
1546 any dynamic link state. */
1547 (*bed->elf_backend_hide_symbol) (info, h, true);
1548 h->forced_local = 0;
1549 h->ref_dynamic = 0;
1550 }
1551 else
1552 h->ref_dynamic = 1;
1553 h->def_dynamic = 0;
1554 /* FIXME: Should we check type and size for protected symbol? */
1555 h->size = 0;
1556 h->type = 0;
1557 return true;
1558 }
1559
1560 /* If a new weak symbol definition comes from a regular file and the
1561 old symbol comes from a dynamic library, we treat the new one as
1562 strong. Similarly, an old weak symbol definition from a regular
1563 file is treated as strong when the new symbol comes from a dynamic
1564 library. Further, an old weak symbol from a dynamic library is
1565 treated as strong if the new symbol is from a dynamic library.
1566 This reflects the way glibc's ld.so works.
1567
1568 Also allow a weak symbol to override a linker script symbol
1569 defined by an early pass over the script. This is done so the
1570 linker knows the symbol is defined in an object file, for the
1571 DEFINED script function.
1572
1573 Do this before setting *type_change_ok or *size_change_ok so that
1574 we warn properly when dynamic library symbols are overridden. */
1575
1576 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1577 newweak = false;
1578 if (olddef && newdyn)
1579 oldweak = false;
1580
1581 /* Allow changes between different types of function symbol. */
1582 if (newfunc && oldfunc)
1583 *type_change_ok = true;
1584
1585 /* It's OK to change the type if either the existing symbol or the
1586 new symbol is weak. A type change is also OK if the old symbol
1587 is undefined and the new symbol is defined. */
1588
1589 if (oldweak
1590 || newweak
1591 || (newdef
1592 && h->root.type == bfd_link_hash_undefined))
1593 *type_change_ok = true;
1594
1595 /* It's OK to change the size if either the existing symbol or the
1596 new symbol is weak, or if the old symbol is undefined. */
1597
1598 if (*type_change_ok
1599 || h->root.type == bfd_link_hash_undefined)
1600 *size_change_ok = true;
1601
1602 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1603 symbol, respectively, appears to be a common symbol in a dynamic
1604 object. If a symbol appears in an uninitialized section, and is
1605 not weak, and is not a function, then it may be a common symbol
1606 which was resolved when the dynamic object was created. We want
1607 to treat such symbols specially, because they raise special
1608 considerations when setting the symbol size: if the symbol
1609 appears as a common symbol in a regular object, and the size in
1610 the regular object is larger, we must make sure that we use the
1611 larger size. This problematic case can always be avoided in C,
1612 but it must be handled correctly when using Fortran shared
1613 libraries.
1614
1615 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1616 likewise for OLDDYNCOMMON and OLDDEF.
1617
1618 Note that this test is just a heuristic, and that it is quite
1619 possible to have an uninitialized symbol in a shared object which
1620 is really a definition, rather than a common symbol. This could
1621 lead to some minor confusion when the symbol really is a common
1622 symbol in some regular object. However, I think it will be
1623 harmless. */
1624
1625 if (newdyn
1626 && newdef
1627 && !newweak
1628 && (sec->flags & SEC_ALLOC) != 0
1629 && (sec->flags & SEC_LOAD) == 0
1630 && sym->st_size > 0
1631 && !newfunc)
1632 newdyncommon = true;
1633 else
1634 newdyncommon = false;
1635
1636 if (olddyn
1637 && olddef
1638 && h->root.type == bfd_link_hash_defined
1639 && h->def_dynamic
1640 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1641 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1642 && h->size > 0
1643 && !oldfunc)
1644 olddyncommon = true;
1645 else
1646 olddyncommon = false;
1647
1648 /* We now know everything about the old and new symbols. We ask the
1649 backend to check if we can merge them. */
1650 if (bed->merge_symbol != NULL)
1651 {
1652 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1653 return false;
1654 sec = *psec;
1655 }
1656
1657 /* There are multiple definitions of a normal symbol. Skip the
1658 default symbol as well as definition from an IR object. */
1659 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1660 && !default_sym && h->def_regular
1661 && !(oldbfd != NULL
1662 && (oldbfd->flags & BFD_PLUGIN) != 0
1663 && (abfd->flags & BFD_PLUGIN) == 0))
1664 {
1665 /* Handle a multiple definition. */
1666 (*info->callbacks->multiple_definition) (info, &h->root,
1667 abfd, sec, *pvalue);
1668 *skip = true;
1669 return true;
1670 }
1671
1672 /* If both the old and the new symbols look like common symbols in a
1673 dynamic object, set the size of the symbol to the larger of the
1674 two. */
1675
1676 if (olddyncommon
1677 && newdyncommon
1678 && sym->st_size != h->size)
1679 {
1680 /* Since we think we have two common symbols, issue a multiple
1681 common warning if desired. Note that we only warn if the
1682 size is different. If the size is the same, we simply let
1683 the old symbol override the new one as normally happens with
1684 symbols defined in dynamic objects. */
1685
1686 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1687 bfd_link_hash_common, sym->st_size);
1688 if (sym->st_size > h->size)
1689 h->size = sym->st_size;
1690
1691 *size_change_ok = true;
1692 }
1693
1694 /* If we are looking at a dynamic object, and we have found a
1695 definition, we need to see if the symbol was already defined by
1696 some other object. If so, we want to use the existing
1697 definition, and we do not want to report a multiple symbol
1698 definition error; we do this by clobbering *PSEC to be
1699 bfd_und_section_ptr.
1700
1701 We treat a common symbol as a definition if the symbol in the
1702 shared library is a function, since common symbols always
1703 represent variables; this can cause confusion in principle, but
1704 any such confusion would seem to indicate an erroneous program or
1705 shared library. We also permit a common symbol in a regular
1706 object to override a weak symbol in a shared object. */
1707
1708 if (newdyn
1709 && newdef
1710 && (olddef
1711 || (h->root.type == bfd_link_hash_common
1712 && (newweak || newfunc))))
1713 {
1714 *override = abfd;
1715 newdef = false;
1716 newdyncommon = false;
1717
1718 *psec = sec = bfd_und_section_ptr;
1719 *size_change_ok = true;
1720
1721 /* If we get here when the old symbol is a common symbol, then
1722 we are explicitly letting it override a weak symbol or
1723 function in a dynamic object, and we don't want to warn about
1724 a type change. If the old symbol is a defined symbol, a type
1725 change warning may still be appropriate. */
1726
1727 if (h->root.type == bfd_link_hash_common)
1728 *type_change_ok = true;
1729 }
1730
1731 /* Handle the special case of an old common symbol merging with a
1732 new symbol which looks like a common symbol in a shared object.
1733 We change *PSEC and *PVALUE to make the new symbol look like a
1734 common symbol, and let _bfd_generic_link_add_one_symbol do the
1735 right thing. */
1736
1737 if (newdyncommon
1738 && h->root.type == bfd_link_hash_common)
1739 {
1740 *override = oldbfd;
1741 newdef = false;
1742 newdyncommon = false;
1743 *pvalue = sym->st_size;
1744 *psec = sec = bed->common_section (oldsec);
1745 *size_change_ok = true;
1746 }
1747
1748 /* Skip weak definitions of symbols that are already defined. */
1749 if (newdef && olddef && newweak)
1750 {
1751 /* Don't skip new non-IR weak syms. */
1752 if (!(oldbfd != NULL
1753 && (oldbfd->flags & BFD_PLUGIN) != 0
1754 && (abfd->flags & BFD_PLUGIN) == 0))
1755 {
1756 newdef = false;
1757 *skip = true;
1758 }
1759
1760 /* Merge st_other. If the symbol already has a dynamic index,
1761 but visibility says it should not be visible, turn it into a
1762 local symbol. */
1763 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1764 if (h->dynindx != -1)
1765 switch (ELF_ST_VISIBILITY (h->other))
1766 {
1767 case STV_INTERNAL:
1768 case STV_HIDDEN:
1769 (*bed->elf_backend_hide_symbol) (info, h, true);
1770 break;
1771 }
1772 }
1773
1774 /* If the old symbol is from a dynamic object, and the new symbol is
1775 a definition which is not from a dynamic object, then the new
1776 symbol overrides the old symbol. Symbols from regular files
1777 always take precedence over symbols from dynamic objects, even if
1778 they are defined after the dynamic object in the link.
1779
1780 As above, we again permit a common symbol in a regular object to
1781 override a definition in a shared object if the shared object
1782 symbol is a function or is weak. */
1783
1784 flip = NULL;
1785 if (!newdyn
1786 && (newdef
1787 || (bfd_is_com_section (sec)
1788 && (oldweak || oldfunc)))
1789 && olddyn
1790 && olddef
1791 && h->def_dynamic)
1792 {
1793 /* Change the hash table entry to undefined, and let
1794 _bfd_generic_link_add_one_symbol do the right thing with the
1795 new definition. */
1796
1797 h->root.type = bfd_link_hash_undefined;
1798 h->root.u.undef.abfd = h->root.u.def.section->owner;
1799 *size_change_ok = true;
1800
1801 olddef = false;
1802 olddyncommon = false;
1803
1804 /* We again permit a type change when a common symbol may be
1805 overriding a function. */
1806
1807 if (bfd_is_com_section (sec))
1808 {
1809 if (oldfunc)
1810 {
1811 /* If a common symbol overrides a function, make sure
1812 that it isn't defined dynamically nor has type
1813 function. */
1814 h->def_dynamic = 0;
1815 h->type = STT_NOTYPE;
1816 }
1817 *type_change_ok = true;
1818 }
1819
1820 if (hi->root.type == bfd_link_hash_indirect)
1821 flip = hi;
1822 else
1823 /* This union may have been set to be non-NULL when this symbol
1824 was seen in a dynamic object. We must force the union to be
1825 NULL, so that it is correct for a regular symbol. */
1826 h->verinfo.vertree = NULL;
1827 }
1828
1829 /* Handle the special case of a new common symbol merging with an
1830 old symbol that looks like it might be a common symbol defined in
1831 a shared object. Note that we have already handled the case in
1832 which a new common symbol should simply override the definition
1833 in the shared library. */
1834
1835 if (! newdyn
1836 && bfd_is_com_section (sec)
1837 && olddyncommon)
1838 {
1839 /* It would be best if we could set the hash table entry to a
1840 common symbol, but we don't know what to use for the section
1841 or the alignment. */
1842 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1843 bfd_link_hash_common, sym->st_size);
1844
1845 /* If the presumed common symbol in the dynamic object is
1846 larger, pretend that the new symbol has its size. */
1847
1848 if (h->size > *pvalue)
1849 *pvalue = h->size;
1850
1851 /* We need to remember the alignment required by the symbol
1852 in the dynamic object. */
1853 BFD_ASSERT (pold_alignment);
1854 *pold_alignment = h->root.u.def.section->alignment_power;
1855
1856 olddef = false;
1857 olddyncommon = false;
1858
1859 h->root.type = bfd_link_hash_undefined;
1860 h->root.u.undef.abfd = h->root.u.def.section->owner;
1861
1862 *size_change_ok = true;
1863 *type_change_ok = true;
1864
1865 if (hi->root.type == bfd_link_hash_indirect)
1866 flip = hi;
1867 else
1868 h->verinfo.vertree = NULL;
1869 }
1870
1871 if (flip != NULL)
1872 {
1873 /* Handle the case where we had a versioned symbol in a dynamic
1874 library and now find a definition in a normal object. In this
1875 case, we make the versioned symbol point to the normal one. */
1876 flip->root.type = h->root.type;
1877 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1878 h->root.type = bfd_link_hash_indirect;
1879 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1880 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1881 if (h->def_dynamic)
1882 {
1883 h->def_dynamic = 0;
1884 flip->ref_dynamic = 1;
1885 }
1886 }
1887
1888 return true;
1889 }
1890
1891 /* This function is called to create an indirect symbol from the
1892 default for the symbol with the default version if needed. The
1893 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1894 set DYNSYM if the new indirect symbol is dynamic. */
1895
1896 static bool
1897 _bfd_elf_add_default_symbol (bfd *abfd,
1898 struct bfd_link_info *info,
1899 struct elf_link_hash_entry *h,
1900 const char *name,
1901 Elf_Internal_Sym *sym,
1902 asection *sec,
1903 bfd_vma value,
1904 bfd **poldbfd,
1905 bool *dynsym)
1906 {
1907 bool type_change_ok;
1908 bool size_change_ok;
1909 bool skip;
1910 char *shortname;
1911 struct elf_link_hash_entry *hi;
1912 struct bfd_link_hash_entry *bh;
1913 const struct elf_backend_data *bed;
1914 bool collect;
1915 bool dynamic;
1916 bfd *override;
1917 char *p;
1918 size_t len, shortlen;
1919 asection *tmp_sec;
1920 bool matched;
1921
1922 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1923 return true;
1924
1925 /* If this symbol has a version, and it is the default version, we
1926 create an indirect symbol from the default name to the fully
1927 decorated name. This will cause external references which do not
1928 specify a version to be bound to this version of the symbol. */
1929 p = strchr (name, ELF_VER_CHR);
1930 if (h->versioned == unknown)
1931 {
1932 if (p == NULL)
1933 {
1934 h->versioned = unversioned;
1935 return true;
1936 }
1937 else
1938 {
1939 if (p[1] != ELF_VER_CHR)
1940 {
1941 h->versioned = versioned_hidden;
1942 return true;
1943 }
1944 else
1945 h->versioned = versioned;
1946 }
1947 }
1948 else
1949 {
1950 /* PR ld/19073: We may see an unversioned definition after the
1951 default version. */
1952 if (p == NULL)
1953 return true;
1954 }
1955
1956 bed = get_elf_backend_data (abfd);
1957 collect = bed->collect;
1958 dynamic = (abfd->flags & DYNAMIC) != 0;
1959
1960 shortlen = p - name;
1961 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1962 if (shortname == NULL)
1963 return false;
1964 memcpy (shortname, name, shortlen);
1965 shortname[shortlen] = '\0';
1966
1967 /* We are going to create a new symbol. Merge it with any existing
1968 symbol with this name. For the purposes of the merge, act as
1969 though we were defining the symbol we just defined, although we
1970 actually going to define an indirect symbol. */
1971 type_change_ok = false;
1972 size_change_ok = false;
1973 matched = true;
1974 tmp_sec = sec;
1975 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1976 &hi, poldbfd, NULL, NULL, &skip, &override,
1977 &type_change_ok, &size_change_ok, &matched))
1978 return false;
1979
1980 if (skip)
1981 goto nondefault;
1982
1983 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1984 {
1985 /* If the undecorated symbol will have a version added by a
1986 script different to H, then don't indirect to/from the
1987 undecorated symbol. This isn't ideal because we may not yet
1988 have seen symbol versions, if given by a script on the
1989 command line rather than via --version-script. */
1990 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1991 {
1992 bool hide;
1993
1994 hi->verinfo.vertree
1995 = bfd_find_version_for_sym (info->version_info,
1996 hi->root.root.string, &hide);
1997 if (hi->verinfo.vertree != NULL && hide)
1998 {
1999 (*bed->elf_backend_hide_symbol) (info, hi, true);
2000 goto nondefault;
2001 }
2002 }
2003 if (hi->verinfo.vertree != NULL
2004 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
2005 goto nondefault;
2006 }
2007
2008 if (! override)
2009 {
2010 /* Add the default symbol if not performing a relocatable link. */
2011 if (! bfd_link_relocatable (info))
2012 {
2013 bh = &hi->root;
2014 if (bh->type == bfd_link_hash_defined
2015 && bh->u.def.section->owner != NULL
2016 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2017 {
2018 /* Mark the previous definition from IR object as
2019 undefined so that the generic linker will override
2020 it. */
2021 bh->type = bfd_link_hash_undefined;
2022 bh->u.undef.abfd = bh->u.def.section->owner;
2023 }
2024 if (! (_bfd_generic_link_add_one_symbol
2025 (info, abfd, shortname, BSF_INDIRECT,
2026 bfd_ind_section_ptr,
2027 0, name, false, collect, &bh)))
2028 return false;
2029 hi = (struct elf_link_hash_entry *) bh;
2030 }
2031 }
2032 else
2033 {
2034 /* In this case the symbol named SHORTNAME is overriding the
2035 indirect symbol we want to add. We were planning on making
2036 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2037 is the name without a version. NAME is the fully versioned
2038 name, and it is the default version.
2039
2040 Overriding means that we already saw a definition for the
2041 symbol SHORTNAME in a regular object, and it is overriding
2042 the symbol defined in the dynamic object.
2043
2044 When this happens, we actually want to change NAME, the
2045 symbol we just added, to refer to SHORTNAME. This will cause
2046 references to NAME in the shared object to become references
2047 to SHORTNAME in the regular object. This is what we expect
2048 when we override a function in a shared object: that the
2049 references in the shared object will be mapped to the
2050 definition in the regular object. */
2051
2052 while (hi->root.type == bfd_link_hash_indirect
2053 || hi->root.type == bfd_link_hash_warning)
2054 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2055
2056 h->root.type = bfd_link_hash_indirect;
2057 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2058 if (h->def_dynamic)
2059 {
2060 h->def_dynamic = 0;
2061 hi->ref_dynamic = 1;
2062 if (hi->ref_regular
2063 || hi->def_regular)
2064 {
2065 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2066 return false;
2067 }
2068 }
2069
2070 /* Now set HI to H, so that the following code will set the
2071 other fields correctly. */
2072 hi = h;
2073 }
2074
2075 /* Check if HI is a warning symbol. */
2076 if (hi->root.type == bfd_link_hash_warning)
2077 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2078
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error to
2081 the user in that case. */
2082
2083 if (hi->root.type == bfd_link_hash_indirect)
2084 {
2085 struct elf_link_hash_entry *ht;
2086
2087 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2088 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2089
2090 /* If we first saw a reference to SHORTNAME with non-default
2091 visibility, merge that visibility to the @@VER symbol. */
2092 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2093
2094 /* A reference to the SHORTNAME symbol from a dynamic library
2095 will be satisfied by the versioned symbol at runtime. In
2096 effect, we have a reference to the versioned symbol. */
2097 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2098 hi->dynamic_def |= ht->dynamic_def;
2099
2100 /* See if the new flags lead us to realize that the symbol must
2101 be dynamic. */
2102 if (! *dynsym)
2103 {
2104 if (! dynamic)
2105 {
2106 if (! bfd_link_executable (info)
2107 || hi->def_dynamic
2108 || hi->ref_dynamic)
2109 *dynsym = true;
2110 }
2111 else
2112 {
2113 if (hi->ref_regular)
2114 *dynsym = true;
2115 }
2116 }
2117 }
2118
2119 /* We also need to define an indirection from the nondefault version
2120 of the symbol. */
2121
2122 nondefault:
2123 len = strlen (name);
2124 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2125 if (shortname == NULL)
2126 return false;
2127 memcpy (shortname, name, shortlen);
2128 memcpy (shortname + shortlen, p + 1, len - shortlen);
2129
2130 /* Once again, merge with any existing symbol. */
2131 type_change_ok = false;
2132 size_change_ok = false;
2133 tmp_sec = sec;
2134 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2135 &hi, poldbfd, NULL, NULL, &skip, &override,
2136 &type_change_ok, &size_change_ok, &matched))
2137 return false;
2138
2139 if (skip)
2140 {
2141 if (!dynamic
2142 && h->root.type == bfd_link_hash_defweak
2143 && hi->root.type == bfd_link_hash_defined)
2144 {
2145 /* We are handling a weak sym@@ver and attempting to define
2146 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2147 new weak sym@ver because there is already a strong sym@ver.
2148 However, sym@ver and sym@@ver are really the same symbol.
2149 The existing strong sym@ver ought to override sym@@ver. */
2150 h->root.type = bfd_link_hash_defined;
2151 h->root.u.def.section = hi->root.u.def.section;
2152 h->root.u.def.value = hi->root.u.def.value;
2153 hi->root.type = bfd_link_hash_indirect;
2154 hi->root.u.i.link = &h->root;
2155 }
2156 else
2157 return true;
2158 }
2159 else if (override)
2160 {
2161 /* Here SHORTNAME is a versioned name, so we don't expect to see
2162 the type of override we do in the case above unless it is
2163 overridden by a versioned definition. */
2164 if (hi->root.type != bfd_link_hash_defined
2165 && hi->root.type != bfd_link_hash_defweak)
2166 _bfd_error_handler
2167 /* xgettext:c-format */
2168 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2169 abfd, shortname);
2170 return true;
2171 }
2172 else
2173 {
2174 bh = &hi->root;
2175 if (! (_bfd_generic_link_add_one_symbol
2176 (info, abfd, shortname, BSF_INDIRECT,
2177 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2178 return false;
2179 hi = (struct elf_link_hash_entry *) bh;
2180 }
2181
2182 /* If there is a duplicate definition somewhere, then HI may not
2183 point to an indirect symbol. We will have reported an error
2184 to the user in that case. */
2185 if (hi->root.type == bfd_link_hash_indirect)
2186 {
2187 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2188 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2189 hi->dynamic_def |= h->dynamic_def;
2190
2191 /* If we first saw a reference to @VER symbol with
2192 non-default visibility, merge that visibility to the
2193 @@VER symbol. */
2194 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2195
2196 /* See if the new flags lead us to realize that the symbol
2197 must be dynamic. */
2198 if (! *dynsym)
2199 {
2200 if (! dynamic)
2201 {
2202 if (! bfd_link_executable (info)
2203 || hi->ref_dynamic)
2204 *dynsym = true;
2205 }
2206 else
2207 {
2208 if (hi->ref_regular)
2209 *dynsym = true;
2210 }
2211 }
2212 }
2213
2214 return true;
2215 }
2216 \f
2217 /* This routine is used to export all defined symbols into the dynamic
2218 symbol table. It is called via elf_link_hash_traverse. */
2219
2220 static bool
2221 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2222 {
2223 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2224
2225 /* Ignore indirect symbols. These are added by the versioning code. */
2226 if (h->root.type == bfd_link_hash_indirect)
2227 return true;
2228
2229 /* Ignore this if we won't export it. */
2230 if (!eif->info->export_dynamic && !h->dynamic)
2231 return true;
2232
2233 if (h->dynindx == -1
2234 && (h->def_regular || h->ref_regular)
2235 && ! bfd_hide_sym_by_version (eif->info->version_info,
2236 h->root.root.string))
2237 {
2238 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2239 {
2240 eif->failed = true;
2241 return false;
2242 }
2243 }
2244
2245 return true;
2246 }
2247 \f
2248 /* Return the glibc version reference if VERSION_DEP is added to the
2249 list of glibc version dependencies successfully. VERSION_DEP will
2250 be put into the .gnu.version_r section. */
2251
2252 static Elf_Internal_Verneed *
2253 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2254 Elf_Internal_Verneed *glibc_verref,
2255 const char *version_dep)
2256 {
2257 Elf_Internal_Verneed *t;
2258 Elf_Internal_Vernaux *a;
2259 size_t amt;
2260
2261 if (glibc_verref != NULL)
2262 {
2263 t = glibc_verref;
2264
2265 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2266 {
2267 /* Return if VERSION_DEP dependency has been added. */
2268 if (a->vna_nodename == version_dep
2269 || strcmp (a->vna_nodename, version_dep) == 0)
2270 return t;
2271 }
2272 }
2273 else
2274 {
2275 bool is_glibc;
2276
2277 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2278 t != NULL;
2279 t = t->vn_nextref)
2280 {
2281 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2282 if (soname != NULL && startswith (soname, "libc.so."))
2283 break;
2284 }
2285
2286 /* Skip the shared library if it isn't libc.so. */
2287 if (t == NULL)
2288 return t;
2289
2290 is_glibc = false;
2291 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2292 {
2293 /* Return if VERSION_DEP dependency has been added. */
2294 if (a->vna_nodename == version_dep
2295 || strcmp (a->vna_nodename, version_dep) == 0)
2296 return t;
2297
2298 /* Check if libc.so provides GLIBC_2.XX version. */
2299 if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2300 is_glibc = true;
2301 }
2302
2303 /* Skip if it isn't linked against glibc. */
2304 if (!is_glibc)
2305 return NULL;
2306 }
2307
2308 amt = sizeof *a;
2309 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2310 if (a == NULL)
2311 {
2312 rinfo->failed = true;
2313 return NULL;
2314 }
2315
2316 a->vna_nodename = version_dep;
2317 a->vna_flags = 0;
2318 a->vna_nextptr = t->vn_auxptr;
2319 a->vna_other = rinfo->vers + 1;
2320 ++rinfo->vers;
2321
2322 t->vn_auxptr = a;
2323
2324 return t;
2325 }
2326
2327 /* Add VERSION_DEP to the list of version dependencies when linked
2328 against glibc. */
2329
2330 void
2331 _bfd_elf_link_add_glibc_version_dependency
2332 (struct elf_find_verdep_info *rinfo,
2333 const char *version_dep[])
2334 {
2335 Elf_Internal_Verneed *t = NULL;
2336
2337 do
2338 {
2339 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2340 /* Return if there is no glibc version reference. */
2341 if (t == NULL)
2342 return;
2343 version_dep++;
2344 }
2345 while (*version_dep != NULL);
2346 }
2347
2348 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2349 linked against glibc. */
2350
2351 void
2352 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2353 {
2354 if (rinfo->info->enable_dt_relr)
2355 {
2356 const char *version[] =
2357 {
2358 "GLIBC_ABI_DT_RELR",
2359 NULL
2360 };
2361 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2362 }
2363 }
2364
2365 /* Look through the symbols which are defined in other shared
2366 libraries and referenced here. Update the list of version
2367 dependencies. This will be put into the .gnu.version_r section.
2368 This function is called via elf_link_hash_traverse. */
2369
2370 static bool
2371 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2372 void *data)
2373 {
2374 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2375 Elf_Internal_Verneed *t;
2376 Elf_Internal_Vernaux *a;
2377 size_t amt;
2378
2379 /* We only care about symbols defined in shared objects with version
2380 information. */
2381 if (!h->def_dynamic
2382 || h->def_regular
2383 || h->dynindx == -1
2384 || h->verinfo.verdef == NULL
2385 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2386 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2387 return true;
2388
2389 /* See if we already know about this version. */
2390 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2391 t != NULL;
2392 t = t->vn_nextref)
2393 {
2394 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2395 continue;
2396
2397 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2398 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2399 return true;
2400
2401 break;
2402 }
2403
2404 /* This is a new version. Add it to tree we are building. */
2405
2406 if (t == NULL)
2407 {
2408 amt = sizeof *t;
2409 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2410 if (t == NULL)
2411 {
2412 rinfo->failed = true;
2413 return false;
2414 }
2415
2416 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2417 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2418 elf_tdata (rinfo->info->output_bfd)->verref = t;
2419 }
2420
2421 amt = sizeof *a;
2422 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2423 if (a == NULL)
2424 {
2425 rinfo->failed = true;
2426 return false;
2427 }
2428
2429 /* Note that we are copying a string pointer here, and testing it
2430 above. If bfd_elf_string_from_elf_section is ever changed to
2431 discard the string data when low in memory, this will have to be
2432 fixed. */
2433 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2434
2435 a->vna_flags = h->verinfo.verdef->vd_flags;
2436 a->vna_nextptr = t->vn_auxptr;
2437
2438 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2439 ++rinfo->vers;
2440
2441 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2442
2443 t->vn_auxptr = a;
2444
2445 return true;
2446 }
2447
2448 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2449 hidden. Set *T_P to NULL if there is no match. */
2450
2451 static bool
2452 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2453 struct elf_link_hash_entry *h,
2454 const char *version_p,
2455 struct bfd_elf_version_tree **t_p,
2456 bool *hide)
2457 {
2458 struct bfd_elf_version_tree *t;
2459
2460 /* Look for the version. If we find it, it is no longer weak. */
2461 for (t = info->version_info; t != NULL; t = t->next)
2462 {
2463 if (strcmp (t->name, version_p) == 0)
2464 {
2465 size_t len;
2466 char *alc;
2467 struct bfd_elf_version_expr *d;
2468
2469 len = version_p - h->root.root.string;
2470 alc = (char *) bfd_malloc (len);
2471 if (alc == NULL)
2472 return false;
2473 memcpy (alc, h->root.root.string, len - 1);
2474 alc[len - 1] = '\0';
2475 if (alc[len - 2] == ELF_VER_CHR)
2476 alc[len - 2] = '\0';
2477
2478 h->verinfo.vertree = t;
2479 t->used = true;
2480 d = NULL;
2481
2482 if (t->globals.list != NULL)
2483 d = (*t->match) (&t->globals, NULL, alc);
2484
2485 /* See if there is anything to force this symbol to
2486 local scope. */
2487 if (d == NULL && t->locals.list != NULL)
2488 {
2489 d = (*t->match) (&t->locals, NULL, alc);
2490 if (d != NULL
2491 && h->dynindx != -1
2492 && ! info->export_dynamic)
2493 *hide = true;
2494 }
2495
2496 free (alc);
2497 break;
2498 }
2499 }
2500
2501 *t_p = t;
2502
2503 return true;
2504 }
2505
2506 /* Return TRUE if the symbol H is hidden by version script. */
2507
2508 bool
2509 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2510 struct elf_link_hash_entry *h)
2511 {
2512 const char *p;
2513 bool hide = false;
2514 const struct elf_backend_data *bed
2515 = get_elf_backend_data (info->output_bfd);
2516
2517 /* Version script only hides symbols defined in regular objects. */
2518 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2519 return true;
2520
2521 p = strchr (h->root.root.string, ELF_VER_CHR);
2522 if (p != NULL && h->verinfo.vertree == NULL)
2523 {
2524 struct bfd_elf_version_tree *t;
2525
2526 ++p;
2527 if (*p == ELF_VER_CHR)
2528 ++p;
2529
2530 if (*p != '\0'
2531 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2532 && hide)
2533 {
2534 if (hide)
2535 (*bed->elf_backend_hide_symbol) (info, h, true);
2536 return true;
2537 }
2538 }
2539
2540 /* If we don't have a version for this symbol, see if we can find
2541 something. */
2542 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2543 {
2544 h->verinfo.vertree
2545 = bfd_find_version_for_sym (info->version_info,
2546 h->root.root.string, &hide);
2547 if (h->verinfo.vertree != NULL && hide)
2548 {
2549 (*bed->elf_backend_hide_symbol) (info, h, true);
2550 return true;
2551 }
2552 }
2553
2554 return false;
2555 }
2556
2557 /* Figure out appropriate versions for all the symbols. We may not
2558 have the version number script until we have read all of the input
2559 files, so until that point we don't know which symbols should be
2560 local. This function is called via elf_link_hash_traverse. */
2561
2562 static bool
2563 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2564 {
2565 struct elf_info_failed *sinfo;
2566 struct bfd_link_info *info;
2567 const struct elf_backend_data *bed;
2568 struct elf_info_failed eif;
2569 char *p;
2570 bool hide;
2571
2572 sinfo = (struct elf_info_failed *) data;
2573 info = sinfo->info;
2574
2575 /* Fix the symbol flags. */
2576 eif.failed = false;
2577 eif.info = info;
2578 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2579 {
2580 if (eif.failed)
2581 sinfo->failed = true;
2582 return false;
2583 }
2584
2585 bed = get_elf_backend_data (info->output_bfd);
2586
2587 /* We only need version numbers for symbols defined in regular
2588 objects. */
2589 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2590 {
2591 /* Hide symbols defined in discarded input sections. */
2592 if ((h->root.type == bfd_link_hash_defined
2593 || h->root.type == bfd_link_hash_defweak)
2594 && discarded_section (h->root.u.def.section))
2595 (*bed->elf_backend_hide_symbol) (info, h, true);
2596 return true;
2597 }
2598
2599 hide = false;
2600 p = strchr (h->root.root.string, ELF_VER_CHR);
2601 if (p != NULL && h->verinfo.vertree == NULL)
2602 {
2603 struct bfd_elf_version_tree *t;
2604
2605 ++p;
2606 if (*p == ELF_VER_CHR)
2607 ++p;
2608
2609 /* If there is no version string, we can just return out. */
2610 if (*p == '\0')
2611 return true;
2612
2613 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2614 {
2615 sinfo->failed = true;
2616 return false;
2617 }
2618
2619 if (hide)
2620 (*bed->elf_backend_hide_symbol) (info, h, true);
2621
2622 /* If we are building an application, we need to create a
2623 version node for this version. */
2624 if (t == NULL && bfd_link_executable (info))
2625 {
2626 struct bfd_elf_version_tree **pp;
2627 int version_index;
2628
2629 /* If we aren't going to export this symbol, we don't need
2630 to worry about it. */
2631 if (h->dynindx == -1)
2632 return true;
2633
2634 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2635 sizeof *t);
2636 if (t == NULL)
2637 {
2638 sinfo->failed = true;
2639 return false;
2640 }
2641
2642 t->name = p;
2643 t->name_indx = (unsigned int) -1;
2644 t->used = true;
2645
2646 version_index = 1;
2647 /* Don't count anonymous version tag. */
2648 if (sinfo->info->version_info != NULL
2649 && sinfo->info->version_info->vernum == 0)
2650 version_index = 0;
2651 for (pp = &sinfo->info->version_info;
2652 *pp != NULL;
2653 pp = &(*pp)->next)
2654 ++version_index;
2655 t->vernum = version_index;
2656
2657 *pp = t;
2658
2659 h->verinfo.vertree = t;
2660 }
2661 else if (t == NULL)
2662 {
2663 /* We could not find the version for a symbol when
2664 generating a shared archive. Return an error. */
2665 _bfd_error_handler
2666 /* xgettext:c-format */
2667 (_("%pB: version node not found for symbol %s"),
2668 info->output_bfd, h->root.root.string);
2669 bfd_set_error (bfd_error_bad_value);
2670 sinfo->failed = true;
2671 return false;
2672 }
2673 }
2674
2675 /* If we don't have a version for this symbol, see if we can find
2676 something. */
2677 if (!hide
2678 && h->verinfo.vertree == NULL
2679 && sinfo->info->version_info != NULL)
2680 {
2681 h->verinfo.vertree
2682 = bfd_find_version_for_sym (sinfo->info->version_info,
2683 h->root.root.string, &hide);
2684 if (h->verinfo.vertree != NULL && hide)
2685 (*bed->elf_backend_hide_symbol) (info, h, true);
2686 }
2687
2688 return true;
2689 }
2690 \f
2691 /* Read and swap the relocs from the section indicated by SHDR. This
2692 may be either a REL or a RELA section. The relocations are
2693 translated into RELA relocations and stored in INTERNAL_RELOCS,
2694 which should have already been allocated to contain enough space.
2695 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2696 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2697 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2698 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2699 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2700
2701 Returns FALSE if something goes wrong. */
2702
2703 static bool
2704 elf_link_read_relocs_from_section (bfd *abfd,
2705 const asection *sec,
2706 Elf_Internal_Shdr *shdr,
2707 void **external_relocs_addr,
2708 size_t *external_relocs_size,
2709 Elf_Internal_Rela *internal_relocs)
2710 {
2711 const struct elf_backend_data *bed;
2712 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2713 const bfd_byte *erela;
2714 const bfd_byte *erelaend;
2715 Elf_Internal_Rela *irela;
2716 Elf_Internal_Shdr *symtab_hdr;
2717 size_t nsyms;
2718 void *external_relocs = *external_relocs_addr;
2719
2720 /* Position ourselves at the start of the section. */
2721 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2722 return false;
2723
2724 /* Read the relocations. */
2725 *external_relocs_size = shdr->sh_size;
2726 if (!_bfd_mmap_read_temporary (&external_relocs,
2727 external_relocs_size,
2728 external_relocs_addr, abfd, true))
2729 return false;
2730
2731 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2732 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2733
2734 bed = get_elf_backend_data (abfd);
2735
2736 /* Convert the external relocations to the internal format. */
2737 if (shdr->sh_entsize == bed->s->sizeof_rel)
2738 swap_in = bed->s->swap_reloc_in;
2739 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2740 swap_in = bed->s->swap_reloca_in;
2741 else
2742 {
2743 bfd_set_error (bfd_error_wrong_format);
2744 return false;
2745 }
2746
2747 erela = (const bfd_byte *) external_relocs;
2748 /* Setting erelaend like this and comparing with <= handles case of
2749 a fuzzed object with sh_size not a multiple of sh_entsize. */
2750 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2751 irela = internal_relocs;
2752 while (erela <= erelaend)
2753 {
2754 bfd_vma r_symndx;
2755
2756 (*swap_in) (abfd, erela, irela);
2757 r_symndx = ELF32_R_SYM (irela->r_info);
2758 if (bed->s->arch_size == 64)
2759 r_symndx >>= 24;
2760 if (nsyms > 0)
2761 {
2762 if ((size_t) r_symndx >= nsyms)
2763 {
2764 _bfd_error_handler
2765 /* xgettext:c-format */
2766 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2767 " for offset %#" PRIx64 " in section `%pA'"),
2768 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2769 (uint64_t) irela->r_offset, sec);
2770 bfd_set_error (bfd_error_bad_value);
2771 return false;
2772 }
2773 }
2774 else if (r_symndx != STN_UNDEF)
2775 {
2776 _bfd_error_handler
2777 /* xgettext:c-format */
2778 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2779 " for offset %#" PRIx64 " in section `%pA'"
2780 " when the object file has no symbol table"),
2781 abfd, (uint64_t) r_symndx,
2782 (uint64_t) irela->r_offset, sec);
2783 bfd_set_error (bfd_error_bad_value);
2784 return false;
2785 }
2786 irela += bed->s->int_rels_per_ext_rel;
2787 erela += shdr->sh_entsize;
2788 }
2789
2790 return true;
2791 }
2792
2793 /* Read and swap the relocs for a section O. They may have been
2794 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2795 not NULL, they are used as buffers to read into. They are known to
2796 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2797 the return value is allocated using either malloc or bfd_alloc,
2798 according to the KEEP_MEMORY argument. If O has two relocation
2799 sections (both REL and RELA relocations), then the REL_HDR
2800 relocations will appear first in INTERNAL_RELOCS, followed by the
2801 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2802 update cache_size. */
2803
2804 Elf_Internal_Rela *
2805 _bfd_elf_link_info_read_relocs (bfd *abfd,
2806 struct bfd_link_info *info,
2807 const asection *o,
2808 void *external_relocs,
2809 Elf_Internal_Rela *internal_relocs,
2810 bool keep_memory)
2811 {
2812 void *alloc1 = NULL;
2813 size_t alloc1_size;
2814 Elf_Internal_Rela *alloc2 = NULL;
2815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2816 struct bfd_elf_section_data *esdo = elf_section_data (o);
2817 Elf_Internal_Rela *internal_rela_relocs;
2818
2819 if (esdo->relocs != NULL)
2820 return esdo->relocs;
2821
2822 if (o->reloc_count == 0)
2823 return NULL;
2824
2825 if (internal_relocs == NULL)
2826 {
2827 bfd_size_type size;
2828
2829 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2830 if (keep_memory)
2831 {
2832 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2833 if (info)
2834 info->cache_size += size;
2835 }
2836 else
2837 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2838 if (internal_relocs == NULL)
2839 return NULL;
2840 }
2841
2842 alloc1 = external_relocs;
2843 internal_rela_relocs = internal_relocs;
2844 if (esdo->rel.hdr)
2845 {
2846 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2847 &alloc1, &alloc1_size,
2848 internal_relocs))
2849 goto error_return;
2850 external_relocs = (((bfd_byte *) external_relocs)
2851 + esdo->rel.hdr->sh_size);
2852 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2853 * bed->s->int_rels_per_ext_rel);
2854 }
2855
2856 if (esdo->rela.hdr
2857 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2858 &alloc1, &alloc1_size,
2859 internal_rela_relocs)))
2860 goto error_return;
2861
2862 /* Cache the results for next time, if we can. */
2863 if (keep_memory)
2864 esdo->relocs = internal_relocs;
2865
2866 _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2867
2868 /* Don't free alloc2, since if it was allocated we are passing it
2869 back (under the name of internal_relocs). */
2870
2871 return internal_relocs;
2872
2873 error_return:
2874 _bfd_munmap_readonly_temporary (alloc1, alloc1_size);
2875 if (alloc2 != NULL)
2876 {
2877 if (keep_memory)
2878 bfd_release (abfd, alloc2);
2879 else
2880 free (alloc2);
2881 }
2882 return NULL;
2883 }
2884
2885 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2886 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2887 struct bfd_link_info. */
2888
2889 Elf_Internal_Rela *
2890 _bfd_elf_link_read_relocs (bfd *abfd,
2891 const asection *o,
2892 void *external_relocs,
2893 Elf_Internal_Rela *internal_relocs,
2894 bool keep_memory)
2895 {
2896 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2897 internal_relocs, keep_memory);
2898
2899 }
2900
2901 /* Compute the size of, and allocate space for, REL_HDR which is the
2902 section header for a section containing relocations for O. */
2903
2904 static bool
2905 _bfd_elf_link_size_reloc_section (bfd *abfd,
2906 struct bfd_elf_section_reloc_data *reldata)
2907 {
2908 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2909
2910 /* That allows us to calculate the size of the section. */
2911 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2912
2913 /* The contents field must last into write_object_contents, so we
2914 allocate it with bfd_alloc rather than malloc. Also since we
2915 cannot be sure that the contents will actually be filled in,
2916 we zero the allocated space. */
2917 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2918 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2919 return false;
2920
2921 if (reldata->hashes == NULL && reldata->count)
2922 {
2923 struct elf_link_hash_entry **p;
2924
2925 p = ((struct elf_link_hash_entry **)
2926 bfd_zmalloc (reldata->count * sizeof (*p)));
2927 if (p == NULL)
2928 return false;
2929
2930 reldata->hashes = p;
2931 }
2932
2933 return true;
2934 }
2935
2936 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2937 originated from the section given by INPUT_REL_HDR) to the
2938 OUTPUT_BFD. */
2939
2940 bool
2941 _bfd_elf_link_output_relocs (bfd *output_bfd,
2942 asection *input_section,
2943 Elf_Internal_Shdr *input_rel_hdr,
2944 Elf_Internal_Rela *internal_relocs,
2945 struct elf_link_hash_entry **rel_hash
2946 ATTRIBUTE_UNUSED)
2947 {
2948 Elf_Internal_Rela *irela;
2949 Elf_Internal_Rela *irelaend;
2950 bfd_byte *erel;
2951 struct bfd_elf_section_reloc_data *output_reldata;
2952 asection *output_section;
2953 const struct elf_backend_data *bed;
2954 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2955 struct bfd_elf_section_data *esdo;
2956
2957 output_section = input_section->output_section;
2958
2959 bed = get_elf_backend_data (output_bfd);
2960 esdo = elf_section_data (output_section);
2961 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2962 {
2963 output_reldata = &esdo->rel;
2964 swap_out = bed->s->swap_reloc_out;
2965 }
2966 else if (esdo->rela.hdr
2967 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2968 {
2969 output_reldata = &esdo->rela;
2970 swap_out = bed->s->swap_reloca_out;
2971 }
2972 else
2973 {
2974 _bfd_error_handler
2975 /* xgettext:c-format */
2976 (_("%pB: relocation size mismatch in %pB section %pA"),
2977 output_bfd, input_section->owner, input_section);
2978 bfd_set_error (bfd_error_wrong_format);
2979 return false;
2980 }
2981
2982 erel = output_reldata->hdr->contents;
2983 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2984 irela = internal_relocs;
2985 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2986 * bed->s->int_rels_per_ext_rel);
2987 while (irela < irelaend)
2988 {
2989 (*swap_out) (output_bfd, irela, erel);
2990 irela += bed->s->int_rels_per_ext_rel;
2991 erel += input_rel_hdr->sh_entsize;
2992 }
2993
2994 /* Bump the counter, so that we know where to add the next set of
2995 relocations. */
2996 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2997
2998 return true;
2999 }
3000 \f
3001 /* Make weak undefined symbols in PIE dynamic. */
3002
3003 bool
3004 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3005 struct elf_link_hash_entry *h)
3006 {
3007 if (bfd_link_pie (info)
3008 && h->dynindx == -1
3009 && h->root.type == bfd_link_hash_undefweak)
3010 return bfd_elf_link_record_dynamic_symbol (info, h);
3011
3012 return true;
3013 }
3014
3015 /* Fix up the flags for a symbol. This handles various cases which
3016 can only be fixed after all the input files are seen. This is
3017 currently called by both adjust_dynamic_symbol and
3018 assign_sym_version, which is unnecessary but perhaps more robust in
3019 the face of future changes. */
3020
3021 static bool
3022 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3023 struct elf_info_failed *eif)
3024 {
3025 const struct elf_backend_data *bed;
3026
3027 /* If this symbol was mentioned in a non-ELF file, try to set
3028 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3029 permit a non-ELF file to correctly refer to a symbol defined in
3030 an ELF dynamic object. */
3031 if (h->non_elf)
3032 {
3033 while (h->root.type == bfd_link_hash_indirect)
3034 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3035
3036 if (h->root.type != bfd_link_hash_defined
3037 && h->root.type != bfd_link_hash_defweak)
3038 {
3039 h->ref_regular = 1;
3040 h->ref_regular_nonweak = 1;
3041 }
3042 else
3043 {
3044 if (h->root.u.def.section->owner != NULL
3045 && (bfd_get_flavour (h->root.u.def.section->owner)
3046 == bfd_target_elf_flavour))
3047 {
3048 h->ref_regular = 1;
3049 h->ref_regular_nonweak = 1;
3050 }
3051 else
3052 h->def_regular = 1;
3053 }
3054
3055 if (h->dynindx == -1
3056 && (h->def_dynamic
3057 || h->ref_dynamic))
3058 {
3059 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3060 {
3061 eif->failed = true;
3062 return false;
3063 }
3064 }
3065 }
3066 else
3067 {
3068 /* Unfortunately, NON_ELF is only correct if the symbol
3069 was first seen in a non-ELF file. Fortunately, if the symbol
3070 was first seen in an ELF file, we're probably OK unless the
3071 symbol was defined in a non-ELF file. Catch that case here.
3072 FIXME: We're still in trouble if the symbol was first seen in
3073 a dynamic object, and then later in a non-ELF regular object. */
3074 if ((h->root.type == bfd_link_hash_defined
3075 || h->root.type == bfd_link_hash_defweak)
3076 && !h->def_regular
3077 && (h->root.u.def.section->owner != NULL
3078 ? (bfd_get_flavour (h->root.u.def.section->owner)
3079 != bfd_target_elf_flavour)
3080 : (bfd_is_abs_section (h->root.u.def.section)
3081 && !h->def_dynamic)))
3082 h->def_regular = 1;
3083 }
3084
3085 /* Backend specific symbol fixup. */
3086 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3087 if (bed->elf_backend_fixup_symbol
3088 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3089 return false;
3090
3091 /* If this is a final link, and the symbol was defined as a common
3092 symbol in a regular object file, and there was no definition in
3093 any dynamic object, then the linker will have allocated space for
3094 the symbol in a common section but the DEF_REGULAR
3095 flag will not have been set. */
3096 if (h->root.type == bfd_link_hash_defined
3097 && !h->def_regular
3098 && h->ref_regular
3099 && !h->def_dynamic
3100 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3101 h->def_regular = 1;
3102
3103 /* Symbols defined in discarded sections shouldn't be dynamic. */
3104 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3105 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3106
3107 /* If a weak undefined symbol has non-default visibility, we also
3108 hide it from the dynamic linker. */
3109 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3110 && h->root.type == bfd_link_hash_undefweak)
3111 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3112
3113 /* A hidden versioned symbol in executable should be forced local if
3114 it is is locally defined, not referenced by shared library and not
3115 exported. */
3116 else if (bfd_link_executable (eif->info)
3117 && h->versioned == versioned_hidden
3118 && !eif->info->export_dynamic
3119 && !h->dynamic
3120 && !h->ref_dynamic
3121 && h->def_regular)
3122 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3123
3124 /* If -Bsymbolic was used (which means to bind references to global
3125 symbols to the definition within the shared object), and this
3126 symbol was defined in a regular object, then it actually doesn't
3127 need a PLT entry. Likewise, if the symbol has non-default
3128 visibility. If the symbol has hidden or internal visibility, we
3129 will force it local. */
3130 else if (h->needs_plt
3131 && bfd_link_pic (eif->info)
3132 && is_elf_hash_table (eif->info->hash)
3133 && (SYMBOLIC_BIND (eif->info, h)
3134 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3135 && h->def_regular)
3136 {
3137 bool force_local;
3138
3139 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3140 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3141 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3142 }
3143
3144 /* If this is a weak defined symbol in a dynamic object, and we know
3145 the real definition in the dynamic object, copy interesting flags
3146 over to the real definition. */
3147 if (h->is_weakalias)
3148 {
3149 struct elf_link_hash_entry *def = weakdef (h);
3150
3151 /* If the real definition is defined by a regular object file,
3152 don't do anything special. See the longer description in
3153 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3154 bfd_link_hash_defined as it was when put on the alias list
3155 then it must have originally been a versioned symbol (for
3156 which a non-versioned indirect symbol is created) and later
3157 a definition for the non-versioned symbol is found. In that
3158 case the indirection is flipped with the versioned symbol
3159 becoming an indirect pointing at the non-versioned symbol.
3160 Thus, not an alias any more. */
3161 if (def->def_regular
3162 || def->root.type != bfd_link_hash_defined)
3163 {
3164 h = def;
3165 while ((h = h->u.alias) != def)
3166 h->is_weakalias = 0;
3167 }
3168 else
3169 {
3170 while (h->root.type == bfd_link_hash_indirect)
3171 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3172 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3173 || h->root.type == bfd_link_hash_defweak);
3174 BFD_ASSERT (def->def_dynamic);
3175 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3176 }
3177 }
3178
3179 return true;
3180 }
3181
3182 /* Make the backend pick a good value for a dynamic symbol. This is
3183 called via elf_link_hash_traverse, and also calls itself
3184 recursively. */
3185
3186 static bool
3187 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3188 {
3189 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3190 struct elf_link_hash_table *htab;
3191 const struct elf_backend_data *bed;
3192
3193 if (! is_elf_hash_table (eif->info->hash))
3194 return false;
3195
3196 /* Ignore indirect symbols. These are added by the versioning code. */
3197 if (h->root.type == bfd_link_hash_indirect)
3198 return true;
3199
3200 /* Fix the symbol flags. */
3201 if (! _bfd_elf_fix_symbol_flags (h, eif))
3202 return false;
3203
3204 htab = elf_hash_table (eif->info);
3205 bed = get_elf_backend_data (htab->dynobj);
3206
3207 if (h->root.type == bfd_link_hash_undefweak)
3208 {
3209 if (eif->info->dynamic_undefined_weak == 0)
3210 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3211 else if (eif->info->dynamic_undefined_weak > 0
3212 && h->ref_regular
3213 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3214 && !bfd_hide_sym_by_version (eif->info->version_info,
3215 h->root.root.string))
3216 {
3217 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3218 {
3219 eif->failed = true;
3220 return false;
3221 }
3222 }
3223 }
3224
3225 /* If this symbol does not require a PLT entry, and it is not
3226 defined by a dynamic object, or is not referenced by a regular
3227 object, ignore it. We do have to handle a weak defined symbol,
3228 even if no regular object refers to it, if we decided to add it
3229 to the dynamic symbol table. FIXME: Do we normally need to worry
3230 about symbols which are defined by one dynamic object and
3231 referenced by another one? */
3232 if (!h->needs_plt
3233 && h->type != STT_GNU_IFUNC
3234 && (h->def_regular
3235 || !h->def_dynamic
3236 || (!h->ref_regular
3237 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3238 {
3239 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3240 return true;
3241 }
3242
3243 /* If we've already adjusted this symbol, don't do it again. This
3244 can happen via a recursive call. */
3245 if (h->dynamic_adjusted)
3246 return true;
3247
3248 /* Don't look at this symbol again. Note that we must set this
3249 after checking the above conditions, because we may look at a
3250 symbol once, decide not to do anything, and then get called
3251 recursively later after REF_REGULAR is set below. */
3252 h->dynamic_adjusted = 1;
3253
3254 /* If this is a weak definition, and we know a real definition, and
3255 the real symbol is not itself defined by a regular object file,
3256 then get a good value for the real definition. We handle the
3257 real symbol first, for the convenience of the backend routine.
3258
3259 Note that there is a confusing case here. If the real definition
3260 is defined by a regular object file, we don't get the real symbol
3261 from the dynamic object, but we do get the weak symbol. If the
3262 processor backend uses a COPY reloc, then if some routine in the
3263 dynamic object changes the real symbol, we will not see that
3264 change in the corresponding weak symbol. This is the way other
3265 ELF linkers work as well, and seems to be a result of the shared
3266 library model.
3267
3268 I will clarify this issue. Most SVR4 shared libraries define the
3269 variable _timezone and define timezone as a weak synonym. The
3270 tzset call changes _timezone. If you write
3271 extern int timezone;
3272 int _timezone = 5;
3273 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3274 you might expect that, since timezone is a synonym for _timezone,
3275 the same number will print both times. However, if the processor
3276 backend uses a COPY reloc, then actually timezone will be copied
3277 into your process image, and, since you define _timezone
3278 yourself, _timezone will not. Thus timezone and _timezone will
3279 wind up at different memory locations. The tzset call will set
3280 _timezone, leaving timezone unchanged. */
3281
3282 if (h->is_weakalias)
3283 {
3284 struct elf_link_hash_entry *def = weakdef (h);
3285
3286 /* If we get to this point, there is an implicit reference to
3287 the alias by a regular object file via the weak symbol H. */
3288 def->ref_regular = 1;
3289
3290 /* Ensure that the backend adjust_dynamic_symbol function sees
3291 the strong alias before H by recursively calling ourselves. */
3292 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3293 return false;
3294 }
3295
3296 /* If a symbol has no type and no size and does not require a PLT
3297 entry, then we are probably about to do the wrong thing here: we
3298 are probably going to create a COPY reloc for an empty object.
3299 This case can arise when a shared object is built with assembly
3300 code, and the assembly code fails to set the symbol type. */
3301 if (h->size == 0
3302 && h->type == STT_NOTYPE
3303 && !h->needs_plt)
3304 _bfd_error_handler
3305 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3306 h->root.root.string);
3307
3308 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3309 {
3310 eif->failed = true;
3311 return false;
3312 }
3313
3314 return true;
3315 }
3316
3317 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3318 DYNBSS. */
3319
3320 bool
3321 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3322 struct elf_link_hash_entry *h,
3323 asection *dynbss)
3324 {
3325 unsigned int power_of_two;
3326 bfd_vma mask;
3327 asection *sec = h->root.u.def.section;
3328
3329 /* The section alignment of the definition is the maximum alignment
3330 requirement of symbols defined in the section. Since we don't
3331 know the symbol alignment requirement, we start with the
3332 maximum alignment and check low bits of the symbol address
3333 for the minimum alignment. */
3334 power_of_two = bfd_section_alignment (sec);
3335 mask = ((bfd_vma) 1 << power_of_two) - 1;
3336 while ((h->root.u.def.value & mask) != 0)
3337 {
3338 mask >>= 1;
3339 --power_of_two;
3340 }
3341
3342 if (power_of_two > bfd_section_alignment (dynbss))
3343 {
3344 /* Adjust the section alignment if needed. */
3345 if (!bfd_set_section_alignment (dynbss, power_of_two))
3346 return false;
3347 }
3348
3349 /* We make sure that the symbol will be aligned properly. */
3350 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3351
3352 /* Define the symbol as being at this point in DYNBSS. */
3353 h->root.u.def.section = dynbss;
3354 h->root.u.def.value = dynbss->size;
3355
3356 /* Increment the size of DYNBSS to make room for the symbol. */
3357 dynbss->size += h->size;
3358
3359 /* No error if extern_protected_data is true. */
3360 if (h->protected_def
3361 && (!info->extern_protected_data
3362 || (info->extern_protected_data < 0
3363 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3364 info->callbacks->einfo
3365 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3366 h->root.root.string);
3367
3368 return true;
3369 }
3370
3371 /* Adjust all external symbols pointing into SEC_MERGE sections
3372 to reflect the object merging within the sections. */
3373
3374 static bool
3375 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3376 {
3377 asection *sec;
3378
3379 if ((h->root.type == bfd_link_hash_defined
3380 || h->root.type == bfd_link_hash_defweak)
3381 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3382 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3383 {
3384 bfd *output_bfd = (bfd *) data;
3385
3386 h->root.u.def.value =
3387 _bfd_merged_section_offset (output_bfd,
3388 &h->root.u.def.section,
3389 elf_section_data (sec)->sec_info,
3390 h->root.u.def.value);
3391 }
3392
3393 return true;
3394 }
3395
3396 /* Returns false if the symbol referred to by H should be considered
3397 to resolve local to the current module, and true if it should be
3398 considered to bind dynamically. */
3399
3400 bool
3401 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3402 struct bfd_link_info *info,
3403 bool not_local_protected)
3404 {
3405 bool binding_stays_local_p;
3406 const struct elf_backend_data *bed;
3407 struct elf_link_hash_table *hash_table;
3408
3409 if (h == NULL)
3410 return false;
3411
3412 while (h->root.type == bfd_link_hash_indirect
3413 || h->root.type == bfd_link_hash_warning)
3414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3415
3416 /* If it was forced local, then clearly it's not dynamic. */
3417 if (h->dynindx == -1)
3418 return false;
3419 if (h->forced_local)
3420 return false;
3421
3422 /* Identify the cases where name binding rules say that a
3423 visible symbol resolves locally. */
3424 binding_stays_local_p = (bfd_link_executable (info)
3425 || SYMBOLIC_BIND (info, h));
3426
3427 switch (ELF_ST_VISIBILITY (h->other))
3428 {
3429 case STV_INTERNAL:
3430 case STV_HIDDEN:
3431 return false;
3432
3433 case STV_PROTECTED:
3434 hash_table = elf_hash_table (info);
3435 if (!is_elf_hash_table (&hash_table->root))
3436 return false;
3437
3438 bed = get_elf_backend_data (hash_table->dynobj);
3439
3440 /* Proper resolution for function pointer equality may require
3441 that these symbols perhaps be resolved dynamically, even though
3442 we should be resolving them to the current module. */
3443 if (!not_local_protected || !bed->is_function_type (h->type))
3444 binding_stays_local_p = true;
3445 break;
3446
3447 default:
3448 break;
3449 }
3450
3451 /* If it isn't defined locally, then clearly it's dynamic. */
3452 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3453 return true;
3454
3455 /* Otherwise, the symbol is dynamic if binding rules don't tell
3456 us that it remains local. */
3457 return !binding_stays_local_p;
3458 }
3459
3460 /* Return true if the symbol referred to by H should be considered
3461 to resolve local to the current module, and false otherwise. Differs
3462 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3463 undefined symbols. The two functions are virtually identical except
3464 for the place where dynindx == -1 is tested. If that test is true,
3465 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3466 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3467 defined symbols.
3468 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3469 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3470 treatment of undefined weak symbols. For those that do not make
3471 undefined weak symbols dynamic, both functions may return false. */
3472
3473 bool
3474 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3475 struct bfd_link_info *info,
3476 bool local_protected)
3477 {
3478 const struct elf_backend_data *bed;
3479 struct elf_link_hash_table *hash_table;
3480
3481 /* If it's a local sym, of course we resolve locally. */
3482 if (h == NULL)
3483 return true;
3484
3485 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3486 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3487 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3488 return true;
3489
3490 /* Forced local symbols resolve locally. */
3491 if (h->forced_local)
3492 return true;
3493
3494 /* Common symbols that become definitions don't get the DEF_REGULAR
3495 flag set, so test it first, and don't bail out. */
3496 if (ELF_COMMON_DEF_P (h))
3497 /* Do nothing. */;
3498 /* If we don't have a definition in a regular file, then we can't
3499 resolve locally. The sym is either undefined or dynamic. */
3500 else if (!h->def_regular)
3501 return false;
3502
3503 /* Non-dynamic symbols resolve locally. */
3504 if (h->dynindx == -1)
3505 return true;
3506
3507 /* At this point, we know the symbol is defined and dynamic. In an
3508 executable it must resolve locally, likewise when building symbolic
3509 shared libraries. */
3510 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3511 return true;
3512
3513 /* Now deal with defined dynamic symbols in shared libraries. Ones
3514 with default visibility might not resolve locally. */
3515 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3516 return false;
3517
3518 hash_table = elf_hash_table (info);
3519 if (!is_elf_hash_table (&hash_table->root))
3520 return true;
3521
3522 /* STV_PROTECTED symbols with indirect external access are local. */
3523 if (info->indirect_extern_access > 0)
3524 return true;
3525
3526 bed = get_elf_backend_data (hash_table->dynobj);
3527
3528 /* If extern_protected_data is false, STV_PROTECTED non-function
3529 symbols are local. */
3530 if ((!info->extern_protected_data
3531 || (info->extern_protected_data < 0
3532 && !bed->extern_protected_data))
3533 && !bed->is_function_type (h->type))
3534 return true;
3535
3536 /* Function pointer equality tests may require that STV_PROTECTED
3537 symbols be treated as dynamic symbols. If the address of a
3538 function not defined in an executable is set to that function's
3539 plt entry in the executable, then the address of the function in
3540 a shared library must also be the plt entry in the executable. */
3541 return local_protected;
3542 }
3543
3544 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3545 aligned. Returns the first TLS output section. */
3546
3547 struct bfd_section *
3548 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3549 {
3550 struct bfd_section *sec, *tls;
3551 unsigned int align = 0;
3552
3553 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3554 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3555 break;
3556 tls = sec;
3557
3558 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3559 if (sec->alignment_power > align)
3560 align = sec->alignment_power;
3561
3562 elf_hash_table (info)->tls_sec = tls;
3563
3564 /* Ensure the alignment of the first section (usually .tdata) is the largest
3565 alignment, so that the tls segment starts aligned. */
3566 if (tls != NULL)
3567 tls->alignment_power = align;
3568
3569 return tls;
3570 }
3571
3572 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3573 static bool
3574 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3575 Elf_Internal_Sym *sym)
3576 {
3577 const struct elf_backend_data *bed;
3578
3579 /* Local symbols do not count, but target specific ones might. */
3580 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3581 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3582 return false;
3583
3584 bed = get_elf_backend_data (abfd);
3585 /* Function symbols do not count. */
3586 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3587 return false;
3588
3589 /* If the section is undefined, then so is the symbol. */
3590 if (sym->st_shndx == SHN_UNDEF)
3591 return false;
3592
3593 /* If the symbol is defined in the common section, then
3594 it is a common definition and so does not count. */
3595 if (bed->common_definition (sym))
3596 return false;
3597
3598 /* If the symbol is in a target specific section then we
3599 must rely upon the backend to tell us what it is. */
3600 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3601 /* FIXME - this function is not coded yet:
3602
3603 return _bfd_is_global_symbol_definition (abfd, sym);
3604
3605 Instead for now assume that the definition is not global,
3606 Even if this is wrong, at least the linker will behave
3607 in the same way that it used to do. */
3608 return false;
3609
3610 return true;
3611 }
3612
3613 /* Search the symbol table of the archive element of the archive ABFD
3614 whose archive map contains a mention of SYMDEF, and determine if
3615 the symbol is defined in this element. */
3616 static bool
3617 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3618 {
3619 Elf_Internal_Shdr * hdr;
3620 size_t symcount;
3621 size_t extsymcount;
3622 size_t extsymoff;
3623 Elf_Internal_Sym *isymbuf;
3624 Elf_Internal_Sym *isym;
3625 Elf_Internal_Sym *isymend;
3626 bool result;
3627
3628 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3629 if (abfd == NULL)
3630 return false;
3631
3632 if (! bfd_check_format (abfd, bfd_object))
3633 return false;
3634
3635 /* Select the appropriate symbol table. If we don't know if the
3636 object file is an IR object, give linker LTO plugin a chance to
3637 get the correct symbol table. */
3638 if (abfd->plugin_format == bfd_plugin_yes
3639 #if BFD_SUPPORTS_PLUGINS
3640 || (abfd->plugin_format == bfd_plugin_unknown
3641 && bfd_link_plugin_object_p (abfd))
3642 #endif
3643 )
3644 {
3645 /* Use the IR symbol table if the object has been claimed by
3646 plugin. */
3647 abfd = abfd->plugin_dummy_bfd;
3648 hdr = &elf_tdata (abfd)->symtab_hdr;
3649 }
3650 else
3651 {
3652 if (elf_use_dt_symtab_p (abfd))
3653 {
3654 bfd_set_error (bfd_error_wrong_format);
3655 return false;
3656 }
3657
3658 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3659 hdr = &elf_tdata (abfd)->symtab_hdr;
3660 else
3661 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3662 }
3663
3664 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3665
3666 /* The sh_info field of the symtab header tells us where the
3667 external symbols start. We don't care about the local symbols. */
3668 if (elf_bad_symtab (abfd))
3669 {
3670 extsymcount = symcount;
3671 extsymoff = 0;
3672 }
3673 else
3674 {
3675 extsymcount = symcount - hdr->sh_info;
3676 extsymoff = hdr->sh_info;
3677 }
3678
3679 if (extsymcount == 0)
3680 return false;
3681
3682 /* Read in the symbol table. */
3683 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3684 NULL, NULL, NULL);
3685 if (isymbuf == NULL)
3686 return false;
3687
3688 /* Scan the symbol table looking for SYMDEF. */
3689 result = false;
3690 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3691 {
3692 const char *name;
3693
3694 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3695 isym->st_name);
3696 if (name == NULL)
3697 break;
3698
3699 if (strcmp (name, symdef->name) == 0)
3700 {
3701 result = is_global_data_symbol_definition (abfd, isym);
3702 break;
3703 }
3704 }
3705
3706 free (isymbuf);
3707
3708 return result;
3709 }
3710 \f
3711 /* Add an entry to the .dynamic table. */
3712
3713 bool
3714 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3715 bfd_vma tag,
3716 bfd_vma val)
3717 {
3718 struct elf_link_hash_table *hash_table;
3719 const struct elf_backend_data *bed;
3720 asection *s;
3721 bfd_size_type newsize;
3722 bfd_byte *newcontents;
3723 Elf_Internal_Dyn dyn;
3724
3725 hash_table = elf_hash_table (info);
3726 if (! is_elf_hash_table (&hash_table->root))
3727 return false;
3728
3729 if (tag == DT_RELA || tag == DT_REL)
3730 hash_table->dynamic_relocs = true;
3731
3732 bed = get_elf_backend_data (hash_table->dynobj);
3733 s = hash_table->dynamic;
3734 BFD_ASSERT (s != NULL);
3735
3736 newsize = s->size + bed->s->sizeof_dyn;
3737 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3738 if (newcontents == NULL)
3739 return false;
3740
3741 dyn.d_tag = tag;
3742 dyn.d_un.d_val = val;
3743 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3744
3745 s->size = newsize;
3746 s->contents = newcontents;
3747
3748 return true;
3749 }
3750
3751 /* Strip zero-sized dynamic sections. */
3752
3753 bool
3754 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3755 {
3756 struct elf_link_hash_table *hash_table;
3757 const struct elf_backend_data *bed;
3758 asection *s, *sdynamic, **pp;
3759 asection *rela_dyn, *rel_dyn;
3760 Elf_Internal_Dyn dyn;
3761 bfd_byte *extdyn, *next;
3762 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3763 bool strip_zero_sized;
3764 bool strip_zero_sized_plt;
3765
3766 if (bfd_link_relocatable (info))
3767 return true;
3768
3769 hash_table = elf_hash_table (info);
3770 if (!is_elf_hash_table (&hash_table->root))
3771 return false;
3772
3773 if (!hash_table->dynobj)
3774 return true;
3775
3776 sdynamic= hash_table->dynamic;
3777 if (!sdynamic)
3778 return true;
3779
3780 bed = get_elf_backend_data (hash_table->dynobj);
3781 swap_dyn_in = bed->s->swap_dyn_in;
3782
3783 strip_zero_sized = false;
3784 strip_zero_sized_plt = false;
3785
3786 /* Strip zero-sized dynamic sections. */
3787 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3788 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3789 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3790 if (s->size == 0
3791 && (s == rela_dyn
3792 || s == rel_dyn
3793 || s == hash_table->srelplt->output_section
3794 || s == hash_table->splt->output_section))
3795 {
3796 *pp = s->next;
3797 info->output_bfd->section_count--;
3798 strip_zero_sized = true;
3799 if (s == rela_dyn)
3800 s = rela_dyn;
3801 if (s == rel_dyn)
3802 s = rel_dyn;
3803 else if (s == hash_table->splt->output_section)
3804 {
3805 s = hash_table->splt;
3806 strip_zero_sized_plt = true;
3807 }
3808 else
3809 s = hash_table->srelplt;
3810 s->flags |= SEC_EXCLUDE;
3811 s->output_section = bfd_abs_section_ptr;
3812 }
3813 else
3814 pp = &s->next;
3815
3816 if (strip_zero_sized_plt && sdynamic->size != 0)
3817 for (extdyn = sdynamic->contents;
3818 extdyn < sdynamic->contents + sdynamic->size;
3819 extdyn = next)
3820 {
3821 next = extdyn + bed->s->sizeof_dyn;
3822 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3823 switch (dyn.d_tag)
3824 {
3825 default:
3826 break;
3827 case DT_JMPREL:
3828 case DT_PLTRELSZ:
3829 case DT_PLTREL:
3830 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3831 the procedure linkage table (the .plt section) has been
3832 removed. */
3833 memmove (extdyn, next,
3834 sdynamic->size - (next - sdynamic->contents));
3835 next = extdyn;
3836 }
3837 }
3838
3839 if (strip_zero_sized)
3840 {
3841 /* Regenerate program headers. */
3842 elf_seg_map (info->output_bfd) = NULL;
3843 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3844 NULL);
3845 }
3846
3847 return true;
3848 }
3849
3850 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3851 1 if a DT_NEEDED tag already exists, and 0 on success. */
3852
3853 int
3854 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3855 {
3856 struct elf_link_hash_table *hash_table;
3857 size_t strindex;
3858 const char *soname;
3859
3860 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3861 return -1;
3862
3863 hash_table = elf_hash_table (info);
3864 soname = elf_dt_name (abfd);
3865 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3866 if (strindex == (size_t) -1)
3867 return -1;
3868
3869 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3870 {
3871 asection *sdyn;
3872 const struct elf_backend_data *bed;
3873 bfd_byte *extdyn;
3874
3875 bed = get_elf_backend_data (hash_table->dynobj);
3876 sdyn = hash_table->dynamic;
3877 if (sdyn != NULL && sdyn->size != 0)
3878 for (extdyn = sdyn->contents;
3879 extdyn < sdyn->contents + sdyn->size;
3880 extdyn += bed->s->sizeof_dyn)
3881 {
3882 Elf_Internal_Dyn dyn;
3883
3884 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3885 if (dyn.d_tag == DT_NEEDED
3886 && dyn.d_un.d_val == strindex)
3887 {
3888 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3889 return 1;
3890 }
3891 }
3892 }
3893
3894 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3895 return -1;
3896
3897 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3898 return -1;
3899
3900 return 0;
3901 }
3902
3903 /* Return true if SONAME is on the needed list between NEEDED and STOP
3904 (or the end of list if STOP is NULL), and needed by a library that
3905 will be loaded. */
3906
3907 static bool
3908 on_needed_list (const char *soname,
3909 struct bfd_link_needed_list *needed,
3910 struct bfd_link_needed_list *stop)
3911 {
3912 struct bfd_link_needed_list *look;
3913 for (look = needed; look != stop; look = look->next)
3914 if (strcmp (soname, look->name) == 0
3915 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3916 /* If needed by a library that itself is not directly
3917 needed, recursively check whether that library is
3918 indirectly needed. Since we add DT_NEEDED entries to
3919 the end of the list, library dependencies appear after
3920 the library. Therefore search prior to the current
3921 LOOK, preventing possible infinite recursion. */
3922 || on_needed_list (elf_dt_name (look->by), needed, look)))
3923 return true;
3924
3925 return false;
3926 }
3927
3928 /* Sort symbol by value, section, size, and type. */
3929 static int
3930 elf_sort_symbol (const void *arg1, const void *arg2)
3931 {
3932 const struct elf_link_hash_entry *h1;
3933 const struct elf_link_hash_entry *h2;
3934 bfd_signed_vma vdiff;
3935 int sdiff;
3936 const char *n1;
3937 const char *n2;
3938
3939 h1 = *(const struct elf_link_hash_entry **) arg1;
3940 h2 = *(const struct elf_link_hash_entry **) arg2;
3941 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3942 if (vdiff != 0)
3943 return vdiff > 0 ? 1 : -1;
3944
3945 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3946 if (sdiff != 0)
3947 return sdiff;
3948
3949 /* Sort so that sized symbols are selected over zero size symbols. */
3950 vdiff = h1->size - h2->size;
3951 if (vdiff != 0)
3952 return vdiff > 0 ? 1 : -1;
3953
3954 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3955 if (h1->type != h2->type)
3956 return h1->type - h2->type;
3957
3958 /* If symbols are properly sized and typed, and multiple strong
3959 aliases are not defined in a shared library by the user we
3960 shouldn't get here. Unfortunately linker script symbols like
3961 __bss_start sometimes match a user symbol defined at the start of
3962 .bss without proper size and type. We'd like to preference the
3963 user symbol over reserved system symbols. Sort on leading
3964 underscores. */
3965 n1 = h1->root.root.string;
3966 n2 = h2->root.root.string;
3967 while (*n1 == *n2)
3968 {
3969 if (*n1 == 0)
3970 break;
3971 ++n1;
3972 ++n2;
3973 }
3974 if (*n1 == '_')
3975 return -1;
3976 if (*n2 == '_')
3977 return 1;
3978
3979 /* Final sort on name selects user symbols like '_u' over reserved
3980 system symbols like '_Z' and also will avoid qsort instability. */
3981 return *n1 - *n2;
3982 }
3983
3984 /* This function is used to adjust offsets into .dynstr for
3985 dynamic symbols. This is called via elf_link_hash_traverse. */
3986
3987 static bool
3988 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3989 {
3990 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3991
3992 if (h->dynindx != -1)
3993 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3994 return true;
3995 }
3996
3997 /* Assign string offsets in .dynstr, update all structures referencing
3998 them. */
3999
4000 static bool
4001 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
4002 {
4003 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4004 struct elf_link_local_dynamic_entry *entry;
4005 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4006 bfd *dynobj = hash_table->dynobj;
4007 asection *sdyn;
4008 bfd_size_type size;
4009 const struct elf_backend_data *bed;
4010 bfd_byte *extdyn;
4011
4012 _bfd_elf_strtab_finalize (dynstr);
4013 size = _bfd_elf_strtab_size (dynstr);
4014
4015 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4016
4017 if (info->callbacks->examine_strtab)
4018 info->callbacks->examine_strtab (dynstr);
4019
4020 bed = get_elf_backend_data (dynobj);
4021 sdyn = hash_table->dynamic;
4022 BFD_ASSERT (sdyn != NULL);
4023
4024 /* Update all .dynamic entries referencing .dynstr strings. */
4025 for (extdyn = sdyn->contents;
4026 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
4027 extdyn += bed->s->sizeof_dyn)
4028 {
4029 Elf_Internal_Dyn dyn;
4030
4031 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4032 switch (dyn.d_tag)
4033 {
4034 case DT_STRSZ:
4035 dyn.d_un.d_val = size;
4036 break;
4037 case DT_NEEDED:
4038 case DT_SONAME:
4039 case DT_RPATH:
4040 case DT_RUNPATH:
4041 case DT_FILTER:
4042 case DT_AUXILIARY:
4043 case DT_AUDIT:
4044 case DT_DEPAUDIT:
4045 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4046 break;
4047 default:
4048 continue;
4049 }
4050 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4051 }
4052
4053 /* Now update local dynamic symbols. */
4054 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4055 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4056 entry->isym.st_name);
4057
4058 /* And the rest of dynamic symbols. */
4059 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4060
4061 /* Adjust version definitions. */
4062 if (elf_tdata (output_bfd)->cverdefs)
4063 {
4064 asection *s;
4065 bfd_byte *p;
4066 size_t i;
4067 Elf_Internal_Verdef def;
4068 Elf_Internal_Verdaux defaux;
4069
4070 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4071 p = s->contents;
4072 do
4073 {
4074 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4075 &def);
4076 p += sizeof (Elf_External_Verdef);
4077 if (def.vd_aux != sizeof (Elf_External_Verdef))
4078 continue;
4079 for (i = 0; i < def.vd_cnt; ++i)
4080 {
4081 _bfd_elf_swap_verdaux_in (output_bfd,
4082 (Elf_External_Verdaux *) p, &defaux);
4083 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4084 defaux.vda_name);
4085 _bfd_elf_swap_verdaux_out (output_bfd,
4086 &defaux, (Elf_External_Verdaux *) p);
4087 p += sizeof (Elf_External_Verdaux);
4088 }
4089 }
4090 while (def.vd_next);
4091 }
4092
4093 /* Adjust version references. */
4094 if (elf_tdata (output_bfd)->verref)
4095 {
4096 asection *s;
4097 bfd_byte *p;
4098 size_t i;
4099 Elf_Internal_Verneed need;
4100 Elf_Internal_Vernaux needaux;
4101
4102 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4103 p = s->contents;
4104 do
4105 {
4106 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4107 &need);
4108 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4109 _bfd_elf_swap_verneed_out (output_bfd, &need,
4110 (Elf_External_Verneed *) p);
4111 p += sizeof (Elf_External_Verneed);
4112 for (i = 0; i < need.vn_cnt; ++i)
4113 {
4114 _bfd_elf_swap_vernaux_in (output_bfd,
4115 (Elf_External_Vernaux *) p, &needaux);
4116 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4117 needaux.vna_name);
4118 _bfd_elf_swap_vernaux_out (output_bfd,
4119 &needaux,
4120 (Elf_External_Vernaux *) p);
4121 p += sizeof (Elf_External_Vernaux);
4122 }
4123 }
4124 while (need.vn_next);
4125 }
4126
4127 return true;
4128 }
4129 \f
4130 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4131 The default is to only match when the INPUT and OUTPUT are exactly
4132 the same target. */
4133
4134 bool
4135 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4136 const bfd_target *output)
4137 {
4138 return input == output;
4139 }
4140
4141 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4142 This version is used when different targets for the same architecture
4143 are virtually identical. */
4144
4145 bool
4146 _bfd_elf_relocs_compatible (const bfd_target *input,
4147 const bfd_target *output)
4148 {
4149 const struct elf_backend_data *obed, *ibed;
4150
4151 if (input == output)
4152 return true;
4153
4154 ibed = xvec_get_elf_backend_data (input);
4155 obed = xvec_get_elf_backend_data (output);
4156
4157 if (ibed->arch != obed->arch)
4158 return false;
4159
4160 /* If both backends are using this function, deem them compatible. */
4161 return ibed->relocs_compatible == obed->relocs_compatible;
4162 }
4163
4164 /* Make a special call to the linker "notice" function to tell it that
4165 we are about to handle an as-needed lib, or have finished
4166 processing the lib. */
4167
4168 bool
4169 _bfd_elf_notice_as_needed (bfd *ibfd,
4170 struct bfd_link_info *info,
4171 enum notice_asneeded_action act)
4172 {
4173 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4174 }
4175
4176 /* Call ACTION on each relocation in an ELF object file. */
4177
4178 bool
4179 _bfd_elf_link_iterate_on_relocs
4180 (bfd *abfd, struct bfd_link_info *info,
4181 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4182 const Elf_Internal_Rela *))
4183 {
4184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4185 struct elf_link_hash_table *htab = elf_hash_table (info);
4186
4187 /* If this object is the same format as the output object, and it is
4188 not a shared library, then let the backend look through the
4189 relocs.
4190
4191 This is required to build global offset table entries and to
4192 arrange for dynamic relocs. It is not required for the
4193 particular common case of linking non PIC code, even when linking
4194 against shared libraries, but unfortunately there is no way of
4195 knowing whether an object file has been compiled PIC or not.
4196 Looking through the relocs is not particularly time consuming.
4197 The problem is that we must either (1) keep the relocs in memory,
4198 which causes the linker to require additional runtime memory or
4199 (2) read the relocs twice from the input file, which wastes time.
4200 This would be a good case for using mmap.
4201
4202 I have no idea how to handle linking PIC code into a file of a
4203 different format. It probably can't be done. */
4204 if ((abfd->flags & DYNAMIC) == 0
4205 && is_elf_hash_table (&htab->root)
4206 && elf_object_id (abfd) == elf_hash_table_id (htab)
4207 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4208 {
4209 asection *o;
4210
4211 for (o = abfd->sections; o != NULL; o = o->next)
4212 {
4213 Elf_Internal_Rela *internal_relocs;
4214 bool ok;
4215
4216 /* Don't check relocations in excluded sections. Don't do
4217 anything special with non-loaded, non-alloced sections.
4218 In particular, any relocs in such sections should not
4219 affect GOT and PLT reference counting (ie. we don't
4220 allow them to create GOT or PLT entries), there's no
4221 possibility or desire to optimize TLS relocs, and
4222 there's not much point in propagating relocs to shared
4223 libs that the dynamic linker won't relocate. */
4224 if ((o->flags & SEC_ALLOC) == 0
4225 || (o->flags & SEC_RELOC) == 0
4226 || (o->flags & SEC_EXCLUDE) != 0
4227 || o->reloc_count == 0
4228 || ((info->strip == strip_all || info->strip == strip_debugger)
4229 && (o->flags & SEC_DEBUGGING) != 0)
4230 || bfd_is_abs_section (o->output_section))
4231 continue;
4232
4233 internal_relocs = _bfd_elf_link_info_read_relocs
4234 (abfd, info, o, NULL, NULL,
4235 _bfd_elf_link_keep_memory (info));
4236 if (internal_relocs == NULL)
4237 return false;
4238
4239 ok = action (abfd, info, o, internal_relocs);
4240
4241 if (elf_section_data (o)->relocs != internal_relocs)
4242 free (internal_relocs);
4243
4244 if (! ok)
4245 return false;
4246 }
4247 }
4248
4249 return true;
4250 }
4251
4252 /* Check relocations in an ELF object file. This is called after
4253 all input files have been opened. */
4254
4255 bool
4256 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4257 {
4258 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4259 if (bed->check_relocs != NULL)
4260 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4261 bed->check_relocs);
4262 return true;
4263 }
4264
4265 /* An entry in the first definition hash table. */
4266
4267 struct elf_link_first_hash_entry
4268 {
4269 struct bfd_hash_entry root;
4270 /* The object of the first definition. */
4271 bfd *abfd;
4272 };
4273
4274 /* The function to create a new entry in the first definition hash
4275 table. */
4276
4277 static struct bfd_hash_entry *
4278 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4279 struct bfd_hash_table *table,
4280 const char *string)
4281 {
4282 struct elf_link_first_hash_entry *ret =
4283 (struct elf_link_first_hash_entry *) entry;
4284
4285 /* Allocate the structure if it has not already been allocated by a
4286 subclass. */
4287 if (ret == NULL)
4288 ret = (struct elf_link_first_hash_entry *)
4289 bfd_hash_allocate (table,
4290 sizeof (struct elf_link_first_hash_entry));
4291 if (ret == NULL)
4292 return NULL;
4293
4294 /* Call the allocation method of the superclass. */
4295 ret = ((struct elf_link_first_hash_entry *)
4296 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4297 string));
4298 if (ret != NULL)
4299 ret->abfd = NULL;
4300
4301 return (struct bfd_hash_entry *) ret;
4302 }
4303
4304 /* Add the symbol NAME from ABFD to first hash. */
4305
4306 static void
4307 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
4308 const char *name, bool copy)
4309 {
4310 struct elf_link_hash_table *htab = elf_hash_table (info);
4311 /* Skip if there is no first hash. */
4312 if (htab->first_hash == NULL)
4313 return;
4314
4315 struct elf_link_first_hash_entry *e
4316 = ((struct elf_link_first_hash_entry *)
4317 bfd_hash_lookup (htab->first_hash, name, true, copy));
4318 if (e == NULL)
4319 info->callbacks->einfo
4320 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd, name);
4321
4322 if (e->abfd == NULL)
4323 /* Store ABFD in abfd. */
4324 e->abfd = abfd;
4325 }
4326
4327 /* Add symbols from an ELF object file to the linker hash table. */
4328
4329 static bool
4330 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4331 {
4332 Elf_Internal_Ehdr *ehdr;
4333 Elf_Internal_Shdr *hdr;
4334 size_t symcount;
4335 size_t extsymcount;
4336 size_t extsymoff;
4337 struct elf_link_hash_entry **sym_hash;
4338 bool dynamic;
4339 Elf_External_Versym *extversym = NULL;
4340 Elf_External_Versym *extversym_end = NULL;
4341 Elf_External_Versym *ever;
4342 struct elf_link_hash_entry *weaks;
4343 struct elf_link_hash_entry **nondeflt_vers = NULL;
4344 size_t nondeflt_vers_cnt = 0;
4345 Elf_Internal_Sym *isymbuf = NULL;
4346 Elf_Internal_Sym *isym;
4347 Elf_Internal_Sym *isymend;
4348 const struct elf_backend_data *bed;
4349 bool add_needed;
4350 struct elf_link_hash_table *htab;
4351 void *alloc_mark = NULL;
4352 struct bfd_hash_entry **old_table = NULL;
4353 unsigned int old_size = 0;
4354 unsigned int old_count = 0;
4355 void *old_tab = NULL;
4356 void *old_ent;
4357 struct bfd_link_hash_entry *old_undefs = NULL;
4358 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4359 void *old_strtab = NULL;
4360 size_t tabsize = 0;
4361 asection *s;
4362 bool just_syms;
4363
4364 htab = elf_hash_table (info);
4365 bed = get_elf_backend_data (abfd);
4366
4367 if (elf_use_dt_symtab_p (abfd))
4368 {
4369 bfd_set_error (bfd_error_wrong_format);
4370 return false;
4371 }
4372
4373 if ((abfd->flags & DYNAMIC) == 0)
4374 {
4375 dynamic = false;
4376 if ((abfd->flags & BFD_PLUGIN) != 0
4377 && is_elf_hash_table (&htab->root)
4378 && htab->first_hash == NULL)
4379 {
4380 /* Initialize first_hash for an IR input. */
4381 htab->first_hash = (struct bfd_hash_table *)
4382 bfd_malloc (sizeof (struct bfd_hash_table));
4383 if (htab->first_hash == NULL
4384 || !bfd_hash_table_init
4385 (htab->first_hash, elf_link_first_hash_newfunc,
4386 sizeof (struct elf_link_first_hash_entry)))
4387 info->callbacks->einfo
4388 (_("%F%P: first_hash failed to create: %E\n"));
4389 }
4390 }
4391 else
4392 {
4393 dynamic = true;
4394
4395 /* You can't use -r against a dynamic object. Also, there's no
4396 hope of using a dynamic object which does not exactly match
4397 the format of the output file. */
4398 if (bfd_link_relocatable (info)
4399 || !is_elf_hash_table (&htab->root)
4400 || info->output_bfd->xvec != abfd->xvec)
4401 {
4402 if (bfd_link_relocatable (info))
4403 bfd_set_error (bfd_error_invalid_operation);
4404 else
4405 bfd_set_error (bfd_error_wrong_format);
4406 goto error_return;
4407 }
4408 }
4409
4410 ehdr = elf_elfheader (abfd);
4411 if (info->warn_alternate_em
4412 && bed->elf_machine_code != ehdr->e_machine
4413 && ((bed->elf_machine_alt1 != 0
4414 && ehdr->e_machine == bed->elf_machine_alt1)
4415 || (bed->elf_machine_alt2 != 0
4416 && ehdr->e_machine == bed->elf_machine_alt2)))
4417 _bfd_error_handler
4418 /* xgettext:c-format */
4419 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4420 ehdr->e_machine, abfd, bed->elf_machine_code);
4421
4422 /* As a GNU extension, any input sections which are named
4423 .gnu.warning.SYMBOL are treated as warning symbols for the given
4424 symbol. This differs from .gnu.warning sections, which generate
4425 warnings when they are included in an output file. */
4426 /* PR 12761: Also generate this warning when building shared libraries. */
4427 for (s = abfd->sections; s != NULL; s = s->next)
4428 {
4429 const char *name;
4430
4431 name = bfd_section_name (s);
4432 if (startswith (name, ".gnu.warning."))
4433 {
4434 char *msg;
4435 bfd_size_type sz;
4436
4437 name += sizeof ".gnu.warning." - 1;
4438
4439 /* If this is a shared object, then look up the symbol
4440 in the hash table. If it is there, and it is already
4441 been defined, then we will not be using the entry
4442 from this shared object, so we don't need to warn.
4443 FIXME: If we see the definition in a regular object
4444 later on, we will warn, but we shouldn't. The only
4445 fix is to keep track of what warnings we are supposed
4446 to emit, and then handle them all at the end of the
4447 link. */
4448 if (dynamic)
4449 {
4450 struct elf_link_hash_entry *h;
4451
4452 h = elf_link_hash_lookup (htab, name, false, false, true);
4453
4454 /* FIXME: What about bfd_link_hash_common? */
4455 if (h != NULL
4456 && (h->root.type == bfd_link_hash_defined
4457 || h->root.type == bfd_link_hash_defweak))
4458 continue;
4459 }
4460
4461 sz = s->size;
4462 msg = (char *) bfd_alloc (abfd, sz + 1);
4463 if (msg == NULL)
4464 goto error_return;
4465
4466 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4467 goto error_return;
4468
4469 msg[sz] = '\0';
4470
4471 if (! (_bfd_generic_link_add_one_symbol
4472 (info, abfd, name, BSF_WARNING, s, 0, msg,
4473 false, bed->collect, NULL)))
4474 goto error_return;
4475
4476 if (bfd_link_executable (info))
4477 {
4478 /* Clobber the section size so that the warning does
4479 not get copied into the output file. */
4480 s->size = 0;
4481
4482 /* Also set SEC_EXCLUDE, so that symbols defined in
4483 the warning section don't get copied to the output. */
4484 s->flags |= SEC_EXCLUDE;
4485 }
4486 }
4487 }
4488
4489 just_syms = ((s = abfd->sections) != NULL
4490 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4491
4492 add_needed = true;
4493 if (! dynamic)
4494 {
4495 /* If we are creating a shared library, create all the dynamic
4496 sections immediately. We need to attach them to something,
4497 so we attach them to this BFD, provided it is the right
4498 format and is not from ld --just-symbols. Always create the
4499 dynamic sections for -E/--dynamic-list. FIXME: If there
4500 are no input BFD's of the same format as the output, we can't
4501 make a shared library. */
4502 if (!just_syms
4503 && (bfd_link_pic (info)
4504 || (!bfd_link_relocatable (info)
4505 && info->nointerp
4506 && (info->export_dynamic || info->dynamic)))
4507 && is_elf_hash_table (&htab->root)
4508 && info->output_bfd->xvec == abfd->xvec
4509 && !htab->dynamic_sections_created)
4510 {
4511 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4512 goto error_return;
4513 }
4514 }
4515 else if (!is_elf_hash_table (&htab->root))
4516 goto error_return;
4517 else
4518 {
4519 const char *soname = NULL;
4520 char *audit = NULL;
4521 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4522 const Elf_Internal_Phdr *phdr;
4523 struct elf_link_loaded_list *loaded_lib;
4524
4525 /* ld --just-symbols and dynamic objects don't mix very well.
4526 ld shouldn't allow it. */
4527 if (just_syms)
4528 abort ();
4529
4530 /* If this dynamic lib was specified on the command line with
4531 --as-needed in effect, then we don't want to add a DT_NEEDED
4532 tag unless the lib is actually used. Similary for libs brought
4533 in by another lib's DT_NEEDED. When --no-add-needed is used
4534 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4535 any dynamic library in DT_NEEDED tags in the dynamic lib at
4536 all. */
4537 add_needed = (elf_dyn_lib_class (abfd)
4538 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4539 | DYN_NO_NEEDED)) == 0;
4540
4541 s = bfd_get_section_by_name (abfd, ".dynamic");
4542 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4543 {
4544 bfd_byte *dynbuf;
4545 bfd_byte *extdyn;
4546 unsigned int elfsec;
4547 unsigned long shlink;
4548
4549 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4550 {
4551 error_free_dyn:
4552 _bfd_elf_munmap_section_contents (s, dynbuf);
4553 goto error_return;
4554 }
4555
4556 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4557 if (elfsec == SHN_BAD)
4558 goto error_free_dyn;
4559 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4560
4561 for (extdyn = dynbuf;
4562 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4563 extdyn += bed->s->sizeof_dyn)
4564 {
4565 Elf_Internal_Dyn dyn;
4566
4567 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4568 if (dyn.d_tag == DT_SONAME)
4569 {
4570 unsigned int tagv = dyn.d_un.d_val;
4571 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4572 if (soname == NULL)
4573 goto error_free_dyn;
4574 }
4575 if (dyn.d_tag == DT_NEEDED)
4576 {
4577 struct bfd_link_needed_list *n, **pn;
4578 char *fnm, *anm;
4579 unsigned int tagv = dyn.d_un.d_val;
4580 size_t amt = sizeof (struct bfd_link_needed_list);
4581
4582 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4583 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4584 if (n == NULL || fnm == NULL)
4585 goto error_free_dyn;
4586 amt = strlen (fnm) + 1;
4587 anm = (char *) bfd_alloc (abfd, amt);
4588 if (anm == NULL)
4589 goto error_free_dyn;
4590 memcpy (anm, fnm, amt);
4591 n->name = anm;
4592 n->by = abfd;
4593 n->next = NULL;
4594 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4595 ;
4596 *pn = n;
4597 }
4598 if (dyn.d_tag == DT_RUNPATH)
4599 {
4600 struct bfd_link_needed_list *n, **pn;
4601 char *fnm, *anm;
4602 unsigned int tagv = dyn.d_un.d_val;
4603 size_t amt = sizeof (struct bfd_link_needed_list);
4604
4605 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4606 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4607 if (n == NULL || fnm == NULL)
4608 goto error_free_dyn;
4609 amt = strlen (fnm) + 1;
4610 anm = (char *) bfd_alloc (abfd, amt);
4611 if (anm == NULL)
4612 goto error_free_dyn;
4613 memcpy (anm, fnm, amt);
4614 n->name = anm;
4615 n->by = abfd;
4616 n->next = NULL;
4617 for (pn = & runpath;
4618 *pn != NULL;
4619 pn = &(*pn)->next)
4620 ;
4621 *pn = n;
4622 }
4623 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4624 if (!runpath && dyn.d_tag == DT_RPATH)
4625 {
4626 struct bfd_link_needed_list *n, **pn;
4627 char *fnm, *anm;
4628 unsigned int tagv = dyn.d_un.d_val;
4629 size_t amt = sizeof (struct bfd_link_needed_list);
4630
4631 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4632 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4633 if (n == NULL || fnm == NULL)
4634 goto error_free_dyn;
4635 amt = strlen (fnm) + 1;
4636 anm = (char *) bfd_alloc (abfd, amt);
4637 if (anm == NULL)
4638 goto error_free_dyn;
4639 memcpy (anm, fnm, amt);
4640 n->name = anm;
4641 n->by = abfd;
4642 n->next = NULL;
4643 for (pn = & rpath;
4644 *pn != NULL;
4645 pn = &(*pn)->next)
4646 ;
4647 *pn = n;
4648 }
4649 if (dyn.d_tag == DT_AUDIT)
4650 {
4651 unsigned int tagv = dyn.d_un.d_val;
4652 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4653 }
4654 if (dyn.d_tag == DT_FLAGS_1)
4655 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4656 }
4657
4658 _bfd_elf_munmap_section_contents (s, dynbuf);
4659 }
4660
4661 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4662 frees all more recently bfd_alloc'd blocks as well. */
4663 if (runpath)
4664 rpath = runpath;
4665
4666 if (rpath)
4667 {
4668 struct bfd_link_needed_list **pn;
4669 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4670 ;
4671 *pn = rpath;
4672 }
4673
4674 /* If we have a PT_GNU_RELRO program header, mark as read-only
4675 all sections contained fully therein. This makes relro
4676 shared library sections appear as they will at run-time. */
4677 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4678 while (phdr-- > elf_tdata (abfd)->phdr)
4679 if (phdr->p_type == PT_GNU_RELRO)
4680 {
4681 for (s = abfd->sections; s != NULL; s = s->next)
4682 {
4683 unsigned int opb = bfd_octets_per_byte (abfd, s);
4684
4685 if ((s->flags & SEC_ALLOC) != 0
4686 && s->vma * opb >= phdr->p_vaddr
4687 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4688 s->flags |= SEC_READONLY;
4689 }
4690 break;
4691 }
4692
4693 /* We do not want to include any of the sections in a dynamic
4694 object in the output file. We hack by simply clobbering the
4695 list of sections in the BFD. This could be handled more
4696 cleanly by, say, a new section flag; the existing
4697 SEC_NEVER_LOAD flag is not the one we want, because that one
4698 still implies that the section takes up space in the output
4699 file. */
4700 bfd_section_list_clear (abfd);
4701
4702 /* Find the name to use in a DT_NEEDED entry that refers to this
4703 object. If the object has a DT_SONAME entry, we use it.
4704 Otherwise, if the generic linker stuck something in
4705 elf_dt_name, we use that. Otherwise, we just use the file
4706 name. */
4707 if (soname == NULL || *soname == '\0')
4708 {
4709 soname = elf_dt_name (abfd);
4710 if (soname == NULL || *soname == '\0')
4711 soname = bfd_get_filename (abfd);
4712 }
4713
4714 /* Save the SONAME because sometimes the linker emulation code
4715 will need to know it. */
4716 elf_dt_name (abfd) = soname;
4717
4718 /* If we have already included this dynamic object in the
4719 link, just ignore it. There is no reason to include a
4720 particular dynamic object more than once. */
4721 for (loaded_lib = htab->dyn_loaded;
4722 loaded_lib != NULL;
4723 loaded_lib = loaded_lib->next)
4724 {
4725 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4726 return true;
4727 }
4728
4729 /* Create dynamic sections for backends that require that be done
4730 before setup_gnu_properties. */
4731 if (add_needed
4732 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4733 return false;
4734
4735 /* Save the DT_AUDIT entry for the linker emulation code. */
4736 elf_dt_audit (abfd) = audit;
4737 }
4738
4739 /* If this is a dynamic object, we always link against the .dynsym
4740 symbol table, not the .symtab symbol table. The dynamic linker
4741 will only see the .dynsym symbol table, so there is no reason to
4742 look at .symtab for a dynamic object. */
4743
4744 if (! dynamic || elf_dynsymtab (abfd) == 0)
4745 hdr = &elf_tdata (abfd)->symtab_hdr;
4746 else
4747 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4748
4749 symcount = hdr->sh_size / bed->s->sizeof_sym;
4750
4751 /* The sh_info field of the symtab header tells us where the
4752 external symbols start. We don't care about the local symbols at
4753 this point. */
4754 if (elf_bad_symtab (abfd))
4755 {
4756 extsymcount = symcount;
4757 extsymoff = 0;
4758 }
4759 else
4760 {
4761 extsymcount = symcount - hdr->sh_info;
4762 extsymoff = hdr->sh_info;
4763 }
4764
4765 sym_hash = elf_sym_hashes (abfd);
4766 if (extsymcount != 0)
4767 {
4768 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4769 NULL, NULL, NULL);
4770 if (isymbuf == NULL)
4771 goto error_return;
4772
4773 if (sym_hash == NULL)
4774 {
4775 /* We store a pointer to the hash table entry for each
4776 external symbol. */
4777 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4778 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4779 if (sym_hash == NULL)
4780 goto error_free_sym;
4781 elf_sym_hashes (abfd) = sym_hash;
4782 }
4783 }
4784
4785 if (dynamic)
4786 {
4787 /* Read in any version definitions. */
4788 if (!_bfd_elf_slurp_version_tables (abfd,
4789 info->default_imported_symver))
4790 goto error_free_sym;
4791
4792 /* Read in the symbol versions, but don't bother to convert them
4793 to internal format. */
4794 if (elf_dynversym (abfd) != 0)
4795 {
4796 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4797 bfd_size_type amt = versymhdr->sh_size;
4798
4799 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4800 goto error_free_sym;
4801 extversym = (Elf_External_Versym *)
4802 _bfd_malloc_and_read (abfd, amt, amt);
4803 if (extversym == NULL)
4804 goto error_free_sym;
4805 extversym_end = extversym + amt / sizeof (*extversym);
4806 }
4807 }
4808
4809 /* If we are loading an as-needed shared lib, save the symbol table
4810 state before we start adding symbols. If the lib turns out
4811 to be unneeded, restore the state. */
4812 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4813 {
4814 unsigned int i;
4815 size_t entsize;
4816
4817 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4818 {
4819 struct bfd_hash_entry *p;
4820 struct elf_link_hash_entry *h;
4821
4822 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4823 {
4824 h = (struct elf_link_hash_entry *) p;
4825 entsize += htab->root.table.entsize;
4826 if (h->root.type == bfd_link_hash_warning)
4827 {
4828 entsize += htab->root.table.entsize;
4829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4830 }
4831 if (h->root.type == bfd_link_hash_common)
4832 entsize += sizeof (*h->root.u.c.p);
4833 }
4834 }
4835
4836 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4837 old_tab = bfd_malloc (tabsize + entsize);
4838 if (old_tab == NULL)
4839 goto error_free_vers;
4840
4841 /* Remember the current objalloc pointer, so that all mem for
4842 symbols added can later be reclaimed. */
4843 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4844 if (alloc_mark == NULL)
4845 goto error_free_vers;
4846
4847 /* Make a special call to the linker "notice" function to
4848 tell it that we are about to handle an as-needed lib. */
4849 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4850 goto error_free_vers;
4851
4852 /* Clone the symbol table. Remember some pointers into the
4853 symbol table, and dynamic symbol count. */
4854 old_ent = (char *) old_tab + tabsize;
4855 memcpy (old_tab, htab->root.table.table, tabsize);
4856 old_undefs = htab->root.undefs;
4857 old_undefs_tail = htab->root.undefs_tail;
4858 old_table = htab->root.table.table;
4859 old_size = htab->root.table.size;
4860 old_count = htab->root.table.count;
4861 old_strtab = NULL;
4862 if (htab->dynstr != NULL)
4863 {
4864 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4865 if (old_strtab == NULL)
4866 goto error_free_vers;
4867 }
4868
4869 for (i = 0; i < htab->root.table.size; i++)
4870 {
4871 struct bfd_hash_entry *p;
4872 struct elf_link_hash_entry *h;
4873
4874 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4875 {
4876 h = (struct elf_link_hash_entry *) p;
4877 memcpy (old_ent, h, htab->root.table.entsize);
4878 old_ent = (char *) old_ent + htab->root.table.entsize;
4879 if (h->root.type == bfd_link_hash_warning)
4880 {
4881 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4882 memcpy (old_ent, h, htab->root.table.entsize);
4883 old_ent = (char *) old_ent + htab->root.table.entsize;
4884 }
4885 if (h->root.type == bfd_link_hash_common)
4886 {
4887 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4888 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4889 }
4890 }
4891 }
4892 }
4893
4894 weaks = NULL;
4895 if (extversym == NULL)
4896 ever = NULL;
4897 else if (extversym + extsymoff < extversym_end)
4898 ever = extversym + extsymoff;
4899 else
4900 {
4901 /* xgettext:c-format */
4902 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4903 abfd, (long) extsymoff,
4904 (long) (extversym_end - extversym) / sizeof (* extversym));
4905 bfd_set_error (bfd_error_bad_value);
4906 goto error_free_vers;
4907 }
4908
4909 if (!bfd_link_relocatable (info)
4910 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
4911 {
4912 _bfd_error_handler
4913 (_("%pB: plugin needed to handle lto object"), abfd);
4914 }
4915
4916 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4917 isym < isymend;
4918 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4919 {
4920 int bind;
4921 bfd_vma value;
4922 asection *sec, *new_sec;
4923 flagword flags;
4924 const char *name;
4925 bool must_copy_name = false;
4926 struct elf_link_hash_entry *h;
4927 struct elf_link_hash_entry *hi;
4928 bool definition;
4929 bool size_change_ok;
4930 bool type_change_ok;
4931 bool new_weak;
4932 bool old_weak;
4933 bfd *override;
4934 bool common;
4935 bool discarded;
4936 unsigned int old_alignment;
4937 unsigned int shindex;
4938 bfd *old_bfd;
4939 bool matched;
4940
4941 override = NULL;
4942
4943 flags = BSF_NO_FLAGS;
4944 sec = NULL;
4945 value = isym->st_value;
4946 common = bed->common_definition (isym);
4947 if (common && info->inhibit_common_definition)
4948 {
4949 /* Treat common symbol as undefined for --no-define-common. */
4950 isym->st_shndx = SHN_UNDEF;
4951 common = false;
4952 }
4953 discarded = false;
4954
4955 bind = ELF_ST_BIND (isym->st_info);
4956 switch (bind)
4957 {
4958 case STB_LOCAL:
4959 /* This should be impossible, since ELF requires that all
4960 global symbols follow all local symbols, and that sh_info
4961 point to the first global symbol. Unfortunately, Irix 5
4962 screws this up. */
4963 if (elf_bad_symtab (abfd))
4964 continue;
4965
4966 /* If we aren't prepared to handle locals within the globals
4967 then we'll likely segfault on a NULL symbol hash if the
4968 symbol is ever referenced in relocations. */
4969 shindex = elf_elfheader (abfd)->e_shstrndx;
4970 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4971 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4972 " (>= sh_info of %lu)"),
4973 abfd, name, (long) (isym - isymbuf + extsymoff),
4974 (long) extsymoff);
4975
4976 /* Dynamic object relocations are not processed by ld, so
4977 ld won't run into the problem mentioned above. */
4978 if (dynamic)
4979 continue;
4980 bfd_set_error (bfd_error_bad_value);
4981 goto error_free_vers;
4982
4983 case STB_GLOBAL:
4984 if (isym->st_shndx != SHN_UNDEF && !common)
4985 flags = BSF_GLOBAL;
4986 break;
4987
4988 case STB_WEAK:
4989 flags = BSF_WEAK;
4990 break;
4991
4992 case STB_GNU_UNIQUE:
4993 flags = BSF_GNU_UNIQUE;
4994 break;
4995
4996 default:
4997 /* Leave it up to the processor backend. */
4998 break;
4999 }
5000
5001 if (isym->st_shndx == SHN_UNDEF)
5002 sec = bfd_und_section_ptr;
5003 else if (isym->st_shndx == SHN_ABS)
5004 sec = bfd_abs_section_ptr;
5005 else if (isym->st_shndx == SHN_COMMON)
5006 {
5007 sec = bfd_com_section_ptr;
5008 /* What ELF calls the size we call the value. What ELF
5009 calls the value we call the alignment. */
5010 value = isym->st_size;
5011 }
5012 else
5013 {
5014 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5015 if (sec == NULL)
5016 sec = bfd_abs_section_ptr;
5017 else if (discarded_section (sec))
5018 {
5019 /* Symbols from discarded section are undefined. We keep
5020 its visibility. */
5021 sec = bfd_und_section_ptr;
5022 discarded = true;
5023 isym->st_shndx = SHN_UNDEF;
5024 }
5025 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5026 value -= sec->vma;
5027 }
5028
5029 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5030 isym->st_name);
5031 if (name == NULL)
5032 goto error_free_vers;
5033
5034 if (isym->st_shndx == SHN_COMMON
5035 && (abfd->flags & BFD_PLUGIN) != 0)
5036 {
5037 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5038
5039 if (xc == NULL)
5040 {
5041 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5042 | SEC_EXCLUDE);
5043 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5044 if (xc == NULL)
5045 goto error_free_vers;
5046 }
5047 sec = xc;
5048 }
5049 else if (isym->st_shndx == SHN_COMMON
5050 && ELF_ST_TYPE (isym->st_info) == STT_TLS
5051 && !bfd_link_relocatable (info))
5052 {
5053 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5054
5055 if (tcomm == NULL)
5056 {
5057 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5058 | SEC_LINKER_CREATED);
5059 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
5060 if (tcomm == NULL)
5061 goto error_free_vers;
5062 }
5063 sec = tcomm;
5064 }
5065 else if (bed->elf_add_symbol_hook)
5066 {
5067 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5068 &sec, &value))
5069 goto error_free_vers;
5070
5071 /* The hook function sets the name to NULL if this symbol
5072 should be skipped for some reason. */
5073 if (name == NULL)
5074 continue;
5075 }
5076
5077 /* Sanity check that all possibilities were handled. */
5078 if (sec == NULL)
5079 abort ();
5080
5081 /* Silently discard TLS symbols from --just-syms. There's
5082 no way to combine a static TLS block with a new TLS block
5083 for this executable. */
5084 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5085 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5086 continue;
5087
5088 if (bfd_is_und_section (sec)
5089 || bfd_is_com_section (sec))
5090 definition = false;
5091 else
5092 definition = true;
5093
5094 size_change_ok = false;
5095 type_change_ok = bed->type_change_ok;
5096 old_weak = false;
5097 matched = false;
5098 old_alignment = 0;
5099 old_bfd = NULL;
5100 new_sec = sec;
5101
5102 if (is_elf_hash_table (&htab->root))
5103 {
5104 Elf_Internal_Versym iver;
5105 unsigned int vernum = 0;
5106 bool skip;
5107
5108 if (ever == NULL)
5109 {
5110 if (info->default_imported_symver)
5111 /* Use the default symbol version created earlier. */
5112 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5113 else
5114 iver.vs_vers = 0;
5115 }
5116 else if (ever >= extversym_end)
5117 {
5118 /* xgettext:c-format */
5119 _bfd_error_handler (_("%pB: not enough version information"),
5120 abfd);
5121 bfd_set_error (bfd_error_bad_value);
5122 goto error_free_vers;
5123 }
5124 else
5125 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5126
5127 vernum = iver.vs_vers & VERSYM_VERSION;
5128
5129 /* If this is a hidden symbol, or if it is not version
5130 1, we append the version name to the symbol name.
5131 However, we do not modify a non-hidden absolute symbol
5132 if it is not a function, because it might be the version
5133 symbol itself. FIXME: What if it isn't? */
5134 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5135 || (vernum > 1
5136 && (!bfd_is_abs_section (sec)
5137 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5138 {
5139 const char *verstr;
5140 size_t namelen, verlen, newlen;
5141 char *newname, *p;
5142
5143 if (isym->st_shndx != SHN_UNDEF)
5144 {
5145 if (vernum > elf_tdata (abfd)->cverdefs)
5146 verstr = NULL;
5147 else if (vernum > 1)
5148 verstr =
5149 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5150 else
5151 verstr = "";
5152
5153 if (verstr == NULL)
5154 {
5155 _bfd_error_handler
5156 /* xgettext:c-format */
5157 (_("%pB: %s: invalid version %u (max %d)"),
5158 abfd, name, vernum,
5159 elf_tdata (abfd)->cverdefs);
5160 bfd_set_error (bfd_error_bad_value);
5161 goto error_free_vers;
5162 }
5163 }
5164 else
5165 {
5166 /* We cannot simply test for the number of
5167 entries in the VERNEED section since the
5168 numbers for the needed versions do not start
5169 at 0. */
5170 Elf_Internal_Verneed *t;
5171
5172 verstr = NULL;
5173 for (t = elf_tdata (abfd)->verref;
5174 t != NULL;
5175 t = t->vn_nextref)
5176 {
5177 Elf_Internal_Vernaux *a;
5178
5179 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5180 {
5181 if (a->vna_other == vernum)
5182 {
5183 verstr = a->vna_nodename;
5184 break;
5185 }
5186 }
5187 if (a != NULL)
5188 break;
5189 }
5190 if (verstr == NULL)
5191 {
5192 _bfd_error_handler
5193 /* xgettext:c-format */
5194 (_("%pB: %s: invalid needed version %d"),
5195 abfd, name, vernum);
5196 bfd_set_error (bfd_error_bad_value);
5197 goto error_free_vers;
5198 }
5199 }
5200
5201 namelen = strlen (name);
5202 verlen = strlen (verstr);
5203 newlen = namelen + verlen + 2;
5204 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5205 && isym->st_shndx != SHN_UNDEF)
5206 ++newlen;
5207
5208 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5209 if (newname == NULL)
5210 goto error_free_vers;
5211 memcpy (newname, name, namelen);
5212 p = newname + namelen;
5213 *p++ = ELF_VER_CHR;
5214 /* If this is a defined non-hidden version symbol,
5215 we add another @ to the name. This indicates the
5216 default version of the symbol. */
5217 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5218 && isym->st_shndx != SHN_UNDEF)
5219 *p++ = ELF_VER_CHR;
5220 memcpy (p, verstr, verlen + 1);
5221
5222 name = newname;
5223 /* Since bfd_hash_alloc is used for "name", the string
5224 must be copied if added to first_hash. The string
5225 memory can be freed when an --as-needed library is
5226 not needed. */
5227 must_copy_name = true;
5228 }
5229
5230 /* If this symbol has default visibility and the user has
5231 requested we not re-export it, then mark it as hidden. */
5232 if (!bfd_is_und_section (sec)
5233 && !dynamic
5234 && abfd->no_export
5235 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5236 isym->st_other = (STV_HIDDEN
5237 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5238
5239 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5240 sym_hash, &old_bfd, &old_weak,
5241 &old_alignment, &skip, &override,
5242 &type_change_ok, &size_change_ok,
5243 &matched))
5244 goto error_free_vers;
5245
5246 if (skip)
5247 continue;
5248
5249 h = *sym_hash;
5250 while (h->root.type == bfd_link_hash_indirect
5251 || h->root.type == bfd_link_hash_warning)
5252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5253
5254 /* Override a definition only if the new symbol matches the
5255 existing one. */
5256 if (override && matched)
5257 {
5258 definition = false;
5259 if (htab->first_hash != NULL
5260 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5261 && h->root.non_ir_ref_regular)
5262 {
5263 /* When reloading --as-needed shared objects for new
5264 symbols added from IR inputs, if this shared object
5265 has the first definition, use it. */
5266 struct elf_link_first_hash_entry *e
5267 = ((struct elf_link_first_hash_entry *)
5268 bfd_hash_lookup (htab->first_hash, name, false,
5269 false));
5270 if (e != NULL && e->abfd == abfd)
5271 definition = true;
5272 }
5273 }
5274
5275 if (h->versioned != unversioned
5276 && elf_tdata (abfd)->verdef != NULL
5277 && vernum > 1
5278 && definition)
5279 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5280 }
5281
5282 if (! (_bfd_generic_link_add_one_symbol
5283 (info, override ? override : abfd, name, flags, sec, value,
5284 NULL, false, bed->collect,
5285 (struct bfd_link_hash_entry **) sym_hash)))
5286 goto error_free_vers;
5287
5288 h = *sym_hash;
5289 /* We need to make sure that indirect symbol dynamic flags are
5290 updated. */
5291 hi = h;
5292 while (h->root.type == bfd_link_hash_indirect
5293 || h->root.type == bfd_link_hash_warning)
5294 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5295
5296 *sym_hash = h;
5297
5298 /* Setting the index to -3 tells elf_link_output_extsym that
5299 this symbol is defined in a discarded section. */
5300 if (discarded && is_elf_hash_table (&htab->root))
5301 h->indx = -3;
5302
5303 new_weak = (flags & BSF_WEAK) != 0;
5304 if (dynamic
5305 && definition
5306 && new_weak
5307 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5308 && is_elf_hash_table (&htab->root)
5309 && h->u.alias == NULL)
5310 {
5311 /* Keep a list of all weak defined non function symbols from
5312 a dynamic object, using the alias field. Later in this
5313 function we will set the alias field to the correct
5314 value. We only put non-function symbols from dynamic
5315 objects on this list, because that happens to be the only
5316 time we need to know the normal symbol corresponding to a
5317 weak symbol, and the information is time consuming to
5318 figure out. If the alias field is not already NULL,
5319 then this symbol was already defined by some previous
5320 dynamic object, and we will be using that previous
5321 definition anyhow. */
5322
5323 h->u.alias = weaks;
5324 weaks = h;
5325 }
5326
5327 /* Set the alignment of a common symbol. */
5328 if ((common || bfd_is_com_section (sec))
5329 && h->root.type == bfd_link_hash_common)
5330 {
5331 unsigned int align;
5332
5333 if (common)
5334 align = bfd_log2 (isym->st_value);
5335 else
5336 {
5337 /* The new symbol is a common symbol in a shared object.
5338 We need to get the alignment from the section. */
5339 align = new_sec->alignment_power;
5340 }
5341 if (align > old_alignment)
5342 h->root.u.c.p->alignment_power = align;
5343 else
5344 h->root.u.c.p->alignment_power = old_alignment;
5345 }
5346
5347 if (is_elf_hash_table (&htab->root))
5348 {
5349 /* Set a flag in the hash table entry indicating the type of
5350 reference or definition we just found. A dynamic symbol
5351 is one which is referenced or defined by both a regular
5352 object and a shared object. */
5353 bool dynsym = false;
5354
5355 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5356 if ((abfd->flags & BFD_PLUGIN) != 0)
5357 {
5358 /* Except for this flag to track nonweak references. */
5359 if (!definition
5360 && bind != STB_WEAK)
5361 h->ref_ir_nonweak = 1;
5362 }
5363 else if (!dynamic)
5364 {
5365 if (! definition)
5366 {
5367 h->ref_regular = 1;
5368 if (bind != STB_WEAK)
5369 h->ref_regular_nonweak = 1;
5370 }
5371 else
5372 {
5373 h->def_regular = 1;
5374 if (h->def_dynamic)
5375 {
5376 h->def_dynamic = 0;
5377 h->ref_dynamic = 1;
5378 }
5379 }
5380 }
5381 else
5382 {
5383 if (! definition)
5384 {
5385 h->ref_dynamic = 1;
5386 hi->ref_dynamic = 1;
5387 }
5388 else
5389 {
5390 h->def_dynamic = 1;
5391 hi->def_dynamic = 1;
5392 }
5393 }
5394
5395 /* If an indirect symbol has been forced local, don't
5396 make the real symbol dynamic. */
5397 if (h != hi && hi->forced_local)
5398 ;
5399 else if (!dynamic)
5400 {
5401 if (bfd_link_dll (info)
5402 || h->def_dynamic
5403 || h->ref_dynamic)
5404 dynsym = true;
5405 }
5406 else
5407 {
5408 if (h->def_regular
5409 || h->ref_regular
5410 || (h->is_weakalias
5411 && weakdef (h)->dynindx != -1))
5412 dynsym = true;
5413 }
5414
5415 /* Check to see if we need to add an indirect symbol for
5416 the default name. */
5417 if ((definition
5418 || (!override && h->root.type == bfd_link_hash_common))
5419 && !(hi != h
5420 && hi->versioned == versioned_hidden))
5421 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5422 sec, value, &old_bfd, &dynsym))
5423 goto error_free_vers;
5424
5425 /* Check the alignment when a common symbol is involved. This
5426 can change when a common symbol is overridden by a normal
5427 definition or a common symbol is ignored due to the old
5428 normal definition. We need to make sure the maximum
5429 alignment is maintained. */
5430 if ((old_alignment || common)
5431 && h->root.type != bfd_link_hash_common)
5432 {
5433 unsigned int common_align;
5434 unsigned int normal_align;
5435 unsigned int symbol_align;
5436 bfd *normal_bfd;
5437 bfd *common_bfd;
5438
5439 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5440 || h->root.type == bfd_link_hash_defweak);
5441
5442 symbol_align = ffs (h->root.u.def.value) - 1;
5443 if (h->root.u.def.section->owner != NULL
5444 && (h->root.u.def.section->owner->flags
5445 & (DYNAMIC | BFD_PLUGIN)) == 0)
5446 {
5447 normal_align = h->root.u.def.section->alignment_power;
5448 if (normal_align > symbol_align)
5449 normal_align = symbol_align;
5450 }
5451 else
5452 normal_align = symbol_align;
5453
5454 if (old_alignment)
5455 {
5456 common_align = old_alignment;
5457 common_bfd = old_bfd;
5458 normal_bfd = abfd;
5459 }
5460 else
5461 {
5462 common_align = bfd_log2 (isym->st_value);
5463 common_bfd = abfd;
5464 normal_bfd = old_bfd;
5465 }
5466
5467 if (normal_align < common_align)
5468 {
5469 /* PR binutils/2735 */
5470 if (normal_bfd == NULL)
5471 _bfd_error_handler
5472 /* xgettext:c-format */
5473 (_("warning: alignment %u of common symbol `%s' in %pB is"
5474 " greater than the alignment (%u) of its section %pA"),
5475 1 << common_align, name, common_bfd,
5476 1 << normal_align, h->root.u.def.section);
5477 else
5478 _bfd_error_handler
5479 /* xgettext:c-format */
5480 (_("warning: alignment %u of normal symbol `%s' in %pB"
5481 " is smaller than %u used by the common definition in %pB"),
5482 1 << normal_align, name, normal_bfd,
5483 1 << common_align, common_bfd);
5484
5485 /* PR 30499: make sure that users understand that this warning is serious. */
5486 _bfd_error_handler
5487 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5488 }
5489 }
5490
5491 /* Remember the symbol size if it isn't undefined. */
5492 if (isym->st_size != 0
5493 && isym->st_shndx != SHN_UNDEF
5494 && (definition || h->size == 0))
5495 {
5496 if (h->size != 0
5497 && h->size != isym->st_size
5498 && ! size_change_ok)
5499 {
5500 _bfd_error_handler
5501 /* xgettext:c-format */
5502 (_("warning: size of symbol `%s' changed"
5503 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5504 name, (uint64_t) h->size, old_bfd,
5505 (uint64_t) isym->st_size, abfd);
5506
5507 /* PR 30499: make sure that users understand that this warning is serious. */
5508 _bfd_error_handler
5509 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5510 }
5511
5512 h->size = isym->st_size;
5513 }
5514
5515 /* If this is a common symbol, then we always want H->SIZE
5516 to be the size of the common symbol. The code just above
5517 won't fix the size if a common symbol becomes larger. We
5518 don't warn about a size change here, because that is
5519 covered by --warn-common. Allow changes between different
5520 function types. */
5521 if (h->root.type == bfd_link_hash_common)
5522 h->size = h->root.u.c.size;
5523
5524 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5525 && ((definition && !new_weak)
5526 || (old_weak && h->root.type == bfd_link_hash_common)
5527 || h->type == STT_NOTYPE))
5528 {
5529 unsigned int type = ELF_ST_TYPE (isym->st_info);
5530
5531 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5532 symbol. */
5533 if (type == STT_GNU_IFUNC
5534 && (abfd->flags & DYNAMIC) != 0)
5535 type = STT_FUNC;
5536
5537 if (h->type != type)
5538 {
5539 if (h->type != STT_NOTYPE && ! type_change_ok)
5540 /* xgettext:c-format */
5541 _bfd_error_handler
5542 (_("warning: type of symbol `%s' changed"
5543 " from %d to %d in %pB"),
5544 name, h->type, type, abfd);
5545
5546 h->type = type;
5547 }
5548 }
5549
5550 /* Merge st_other field. */
5551 elf_merge_st_other (abfd, h, isym->st_other, sec,
5552 definition, dynamic);
5553
5554 /* We don't want to make debug symbol dynamic. */
5555 if (definition
5556 && (sec->flags & SEC_DEBUGGING)
5557 && !bfd_link_relocatable (info))
5558 dynsym = false;
5559
5560 /* Nor should we make plugin symbols dynamic. */
5561 if ((abfd->flags & BFD_PLUGIN) != 0)
5562 dynsym = false;
5563
5564 if (definition)
5565 {
5566 h->target_internal = isym->st_target_internal;
5567 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5568 }
5569
5570 /* Don't add indirect symbols for .symver x, x@FOO aliases
5571 in IR. Since all data or text symbols in IR have the
5572 same type, value and section, we can't tell if a symbol
5573 is an alias of another symbol by their types, values and
5574 sections. */
5575 if (definition
5576 && !dynamic
5577 && (abfd->flags & BFD_PLUGIN) == 0)
5578 {
5579 char *p = strchr (name, ELF_VER_CHR);
5580 if (p != NULL && p[1] != ELF_VER_CHR)
5581 {
5582 /* Queue non-default versions so that .symver x, x@FOO
5583 aliases can be checked. */
5584 if (!nondeflt_vers)
5585 {
5586 size_t amt = ((isymend - isym + 1)
5587 * sizeof (struct elf_link_hash_entry *));
5588 nondeflt_vers
5589 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5590 if (!nondeflt_vers)
5591 goto error_free_vers;
5592 }
5593 nondeflt_vers[nondeflt_vers_cnt++] = h;
5594 }
5595 }
5596
5597 if (dynsym && h->dynindx == -1)
5598 {
5599 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5600 goto error_free_vers;
5601 if (h->is_weakalias
5602 && weakdef (h)->dynindx == -1)
5603 {
5604 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5605 goto error_free_vers;
5606 }
5607 }
5608 else if (h->dynindx != -1)
5609 /* If the symbol already has a dynamic index, but
5610 visibility says it should not be visible, turn it into
5611 a local symbol. */
5612 switch (ELF_ST_VISIBILITY (h->other))
5613 {
5614 case STV_INTERNAL:
5615 case STV_HIDDEN:
5616 (*bed->elf_backend_hide_symbol) (info, h, true);
5617 dynsym = false;
5618 break;
5619 }
5620
5621 if (!add_needed
5622 && matched
5623 && definition
5624 && h->root.type != bfd_link_hash_indirect)
5625 {
5626 if ((dynsym
5627 && h->ref_regular_nonweak)
5628 || (old_bfd != NULL
5629 && (old_bfd->flags & BFD_PLUGIN) != 0
5630 && h->ref_ir_nonweak
5631 && !info->lto_all_symbols_read)
5632 || (h->ref_dynamic_nonweak
5633 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5634 && !on_needed_list (elf_dt_name (abfd),
5635 htab->needed, NULL)))
5636 {
5637 const char *soname = elf_dt_name (abfd);
5638
5639 info->callbacks->minfo ("%!", soname, old_bfd,
5640 h->root.root.string);
5641
5642 /* A symbol from a library loaded via DT_NEEDED of some
5643 other library is referenced by a regular object.
5644 Add a DT_NEEDED entry for it. Issue an error if
5645 --no-add-needed is used and the reference was not
5646 a weak one. */
5647 if (old_bfd != NULL
5648 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5649 {
5650 _bfd_error_handler
5651 /* xgettext:c-format */
5652 (_("%pB: undefined reference to symbol '%s'"),
5653 old_bfd, name);
5654 bfd_set_error (bfd_error_missing_dso);
5655 goto error_free_vers;
5656 }
5657
5658 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5659 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5660
5661 /* Create dynamic sections for backends that require
5662 that be done before setup_gnu_properties. */
5663 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5664 return false;
5665 add_needed = true;
5666 }
5667 else if (dynamic
5668 && h->root.u.def.section->owner == abfd)
5669 /* Add this symbol to first hash if this shared
5670 object has the first definition. */
5671 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5672 }
5673 }
5674 }
5675
5676 if (info->lto_plugin_active
5677 && !bfd_link_relocatable (info)
5678 && (abfd->flags & BFD_PLUGIN) == 0
5679 && !just_syms
5680 && extsymcount)
5681 {
5682 int r_sym_shift;
5683
5684 if (bed->s->arch_size == 32)
5685 r_sym_shift = 8;
5686 else
5687 r_sym_shift = 32;
5688
5689 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5690 referenced in regular objects so that linker plugin will get
5691 the correct symbol resolution. */
5692
5693 sym_hash = elf_sym_hashes (abfd);
5694 for (s = abfd->sections; s != NULL; s = s->next)
5695 {
5696 Elf_Internal_Rela *internal_relocs;
5697 Elf_Internal_Rela *rel, *relend;
5698
5699 /* Don't check relocations in excluded sections. */
5700 if ((s->flags & SEC_RELOC) == 0
5701 || s->reloc_count == 0
5702 || (s->flags & SEC_EXCLUDE) != 0
5703 || ((info->strip == strip_all
5704 || info->strip == strip_debugger)
5705 && (s->flags & SEC_DEBUGGING) != 0))
5706 continue;
5707
5708 internal_relocs = _bfd_elf_link_info_read_relocs
5709 (abfd, info, s, NULL, NULL,
5710 _bfd_elf_link_keep_memory (info));
5711 if (internal_relocs == NULL)
5712 goto error_free_vers;
5713
5714 rel = internal_relocs;
5715 relend = rel + s->reloc_count;
5716 for ( ; rel < relend; rel++)
5717 {
5718 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5719 struct elf_link_hash_entry *h;
5720
5721 /* Skip local symbols. */
5722 if (r_symndx < extsymoff)
5723 continue;
5724
5725 h = sym_hash[r_symndx - extsymoff];
5726 if (h != NULL)
5727 h->root.non_ir_ref_regular = 1;
5728 }
5729
5730 if (elf_section_data (s)->relocs != internal_relocs)
5731 free (internal_relocs);
5732 }
5733 }
5734
5735 free (extversym);
5736 extversym = NULL;
5737 free (isymbuf);
5738 isymbuf = NULL;
5739
5740 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5741 {
5742 unsigned int i;
5743
5744 /* Restore the symbol table. */
5745 old_ent = (char *) old_tab + tabsize;
5746 memset (elf_sym_hashes (abfd), 0,
5747 extsymcount * sizeof (struct elf_link_hash_entry *));
5748 htab->root.table.table = old_table;
5749 htab->root.table.size = old_size;
5750 htab->root.table.count = old_count;
5751 memcpy (htab->root.table.table, old_tab, tabsize);
5752 htab->root.undefs = old_undefs;
5753 htab->root.undefs_tail = old_undefs_tail;
5754 if (htab->dynstr != NULL)
5755 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5756 free (old_strtab);
5757 old_strtab = NULL;
5758 for (i = 0; i < htab->root.table.size; i++)
5759 {
5760 struct bfd_hash_entry *p;
5761 struct elf_link_hash_entry *h;
5762 unsigned int non_ir_ref_dynamic;
5763
5764 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5765 {
5766 /* Preserve non_ir_ref_dynamic so that this symbol
5767 will be exported when the dynamic lib becomes needed
5768 in the second pass. */
5769 h = (struct elf_link_hash_entry *) p;
5770 if (h->root.type == bfd_link_hash_warning)
5771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5772 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5773
5774 h = (struct elf_link_hash_entry *) p;
5775 memcpy (h, old_ent, htab->root.table.entsize);
5776 old_ent = (char *) old_ent + htab->root.table.entsize;
5777 if (h->root.type == bfd_link_hash_warning)
5778 {
5779 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5780 memcpy (h, old_ent, htab->root.table.entsize);
5781 old_ent = (char *) old_ent + htab->root.table.entsize;
5782 }
5783 if (h->root.type == bfd_link_hash_common)
5784 {
5785 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5786 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5787 }
5788 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5789 }
5790 }
5791
5792 /* Make a special call to the linker "notice" function to
5793 tell it that symbols added for crefs may need to be removed. */
5794 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5795 goto error_free_vers;
5796
5797 free (old_tab);
5798 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5799 alloc_mark);
5800 free (nondeflt_vers);
5801 return true;
5802 }
5803
5804 if (old_tab != NULL)
5805 {
5806 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5807 goto error_free_vers;
5808 free (old_tab);
5809 old_tab = NULL;
5810 }
5811
5812 /* Now that all the symbols from this input file are created, if
5813 not performing a relocatable link, handle .symver foo, foo@BAR
5814 such that any relocs against foo become foo@BAR. */
5815 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5816 {
5817 size_t cnt, symidx;
5818
5819 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5820 {
5821 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5822 char *shortname, *p;
5823 size_t amt;
5824
5825 p = strchr (h->root.root.string, ELF_VER_CHR);
5826 if (p == NULL
5827 || (h->root.type != bfd_link_hash_defined
5828 && h->root.type != bfd_link_hash_defweak))
5829 continue;
5830
5831 amt = p - h->root.root.string;
5832 shortname = (char *) bfd_malloc (amt + 1);
5833 if (!shortname)
5834 goto error_free_vers;
5835 memcpy (shortname, h->root.root.string, amt);
5836 shortname[amt] = '\0';
5837
5838 hi = (struct elf_link_hash_entry *)
5839 bfd_link_hash_lookup (&htab->root, shortname,
5840 false, false, false);
5841 if (hi != NULL
5842 && hi->root.type == h->root.type
5843 && hi->root.u.def.value == h->root.u.def.value
5844 && hi->root.u.def.section == h->root.u.def.section)
5845 {
5846 (*bed->elf_backend_hide_symbol) (info, hi, true);
5847 hi->root.type = bfd_link_hash_indirect;
5848 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5849 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5850 sym_hash = elf_sym_hashes (abfd);
5851 if (sym_hash)
5852 for (symidx = 0; symidx < extsymcount; ++symidx)
5853 if (sym_hash[symidx] == hi)
5854 {
5855 sym_hash[symidx] = h;
5856 break;
5857 }
5858 }
5859 free (shortname);
5860 }
5861 free (nondeflt_vers);
5862 nondeflt_vers = NULL;
5863 }
5864
5865 /* Now set the alias field correctly for all the weak defined
5866 symbols we found. The only way to do this is to search all the
5867 symbols. Since we only need the information for non functions in
5868 dynamic objects, that's the only time we actually put anything on
5869 the list WEAKS. We need this information so that if a regular
5870 object refers to a symbol defined weakly in a dynamic object, the
5871 real symbol in the dynamic object is also put in the dynamic
5872 symbols; we also must arrange for both symbols to point to the
5873 same memory location. We could handle the general case of symbol
5874 aliasing, but a general symbol alias can only be generated in
5875 assembler code, handling it correctly would be very time
5876 consuming, and other ELF linkers don't handle general aliasing
5877 either. */
5878 if (weaks != NULL)
5879 {
5880 struct elf_link_hash_entry **hpp;
5881 struct elf_link_hash_entry **hppend;
5882 struct elf_link_hash_entry **sorted_sym_hash;
5883 struct elf_link_hash_entry *h;
5884 size_t sym_count, amt;
5885
5886 /* Since we have to search the whole symbol list for each weak
5887 defined symbol, search time for N weak defined symbols will be
5888 O(N^2). Binary search will cut it down to O(NlogN). */
5889 amt = extsymcount * sizeof (*sorted_sym_hash);
5890 sorted_sym_hash = bfd_malloc (amt);
5891 if (sorted_sym_hash == NULL)
5892 goto error_return;
5893 sym_hash = sorted_sym_hash;
5894 hpp = elf_sym_hashes (abfd);
5895 hppend = hpp + extsymcount;
5896 sym_count = 0;
5897 for (; hpp < hppend; hpp++)
5898 {
5899 h = *hpp;
5900 if (h != NULL
5901 && h->root.type == bfd_link_hash_defined
5902 && !bed->is_function_type (h->type))
5903 {
5904 *sym_hash = h;
5905 sym_hash++;
5906 sym_count++;
5907 }
5908 }
5909
5910 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5911 elf_sort_symbol);
5912
5913 while (weaks != NULL)
5914 {
5915 struct elf_link_hash_entry *hlook;
5916 asection *slook;
5917 bfd_vma vlook;
5918 size_t i, j, idx = 0;
5919
5920 hlook = weaks;
5921 weaks = hlook->u.alias;
5922 hlook->u.alias = NULL;
5923
5924 if (hlook->root.type != bfd_link_hash_defined
5925 && hlook->root.type != bfd_link_hash_defweak)
5926 continue;
5927
5928 slook = hlook->root.u.def.section;
5929 vlook = hlook->root.u.def.value;
5930
5931 i = 0;
5932 j = sym_count;
5933 while (i != j)
5934 {
5935 bfd_signed_vma vdiff;
5936 idx = (i + j) / 2;
5937 h = sorted_sym_hash[idx];
5938 vdiff = vlook - h->root.u.def.value;
5939 if (vdiff < 0)
5940 j = idx;
5941 else if (vdiff > 0)
5942 i = idx + 1;
5943 else
5944 {
5945 int sdiff = slook->id - h->root.u.def.section->id;
5946 if (sdiff < 0)
5947 j = idx;
5948 else if (sdiff > 0)
5949 i = idx + 1;
5950 else
5951 break;
5952 }
5953 }
5954
5955 /* We didn't find a value/section match. */
5956 if (i == j)
5957 continue;
5958
5959 /* With multiple aliases, or when the weak symbol is already
5960 strongly defined, we have multiple matching symbols and
5961 the binary search above may land on any of them. Step
5962 one past the matching symbol(s). */
5963 while (++idx != j)
5964 {
5965 h = sorted_sym_hash[idx];
5966 if (h->root.u.def.section != slook
5967 || h->root.u.def.value != vlook)
5968 break;
5969 }
5970
5971 /* Now look back over the aliases. Since we sorted by size
5972 as well as value and section, we'll choose the one with
5973 the largest size. */
5974 while (idx-- != i)
5975 {
5976 h = sorted_sym_hash[idx];
5977
5978 /* Stop if value or section doesn't match. */
5979 if (h->root.u.def.section != slook
5980 || h->root.u.def.value != vlook)
5981 break;
5982 else if (h != hlook)
5983 {
5984 struct elf_link_hash_entry *t;
5985
5986 hlook->u.alias = h;
5987 hlook->is_weakalias = 1;
5988 t = h;
5989 if (t->u.alias != NULL)
5990 while (t->u.alias != h)
5991 t = t->u.alias;
5992 t->u.alias = hlook;
5993
5994 /* If the weak definition is in the list of dynamic
5995 symbols, make sure the real definition is put
5996 there as well. */
5997 if (hlook->dynindx != -1 && h->dynindx == -1)
5998 {
5999 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6000 {
6001 err_free_sym_hash:
6002 free (sorted_sym_hash);
6003 goto error_return;
6004 }
6005 }
6006
6007 /* If the real definition is in the list of dynamic
6008 symbols, make sure the weak definition is put
6009 there as well. If we don't do this, then the
6010 dynamic loader might not merge the entries for the
6011 real definition and the weak definition. */
6012 if (h->dynindx != -1 && hlook->dynindx == -1)
6013 {
6014 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
6015 goto err_free_sym_hash;
6016 }
6017 break;
6018 }
6019 }
6020 }
6021
6022 free (sorted_sym_hash);
6023 }
6024
6025 if (bed->check_directives
6026 && !(*bed->check_directives) (abfd, info))
6027 return false;
6028
6029 /* If this is a non-traditional link, try to optimize the handling
6030 of the .stab/.stabstr sections. */
6031 if (! dynamic
6032 && ! info->traditional_format
6033 && is_elf_hash_table (&htab->root)
6034 && (info->strip != strip_all && info->strip != strip_debugger))
6035 {
6036 asection *stabstr;
6037
6038 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6039 if (stabstr != NULL)
6040 {
6041 bfd_size_type string_offset = 0;
6042 asection *stab;
6043
6044 for (stab = abfd->sections; stab; stab = stab->next)
6045 if (startswith (stab->name, ".stab")
6046 && (!stab->name[5] ||
6047 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6048 && (stab->flags & SEC_MERGE) == 0
6049 && !bfd_is_abs_section (stab->output_section))
6050 {
6051 struct bfd_elf_section_data *secdata;
6052
6053 secdata = elf_section_data (stab);
6054 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6055 stabstr, &secdata->sec_info,
6056 &string_offset))
6057 goto error_return;
6058 if (secdata->sec_info)
6059 stab->sec_info_type = SEC_INFO_TYPE_STABS;
6060 }
6061 }
6062 }
6063
6064 if (dynamic && add_needed)
6065 {
6066 /* Add this bfd to the loaded list. */
6067 struct elf_link_loaded_list *n;
6068
6069 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
6070 if (n == NULL)
6071 goto error_return;
6072 n->abfd = abfd;
6073 n->next = htab->dyn_loaded;
6074 htab->dyn_loaded = n;
6075 }
6076 if (dynamic && !add_needed
6077 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6078 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
6079
6080 return true;
6081
6082 error_free_vers:
6083 free (old_tab);
6084 free (old_strtab);
6085 free (nondeflt_vers);
6086 free (extversym);
6087 error_free_sym:
6088 free (isymbuf);
6089 error_return:
6090 return false;
6091 }
6092
6093 /* Return the linker hash table entry of a symbol that might be
6094 satisfied by an archive symbol. Return -1 on error. */
6095
6096 struct bfd_link_hash_entry *
6097 _bfd_elf_archive_symbol_lookup (bfd *abfd,
6098 struct bfd_link_info *info,
6099 const char *name)
6100 {
6101 struct bfd_link_hash_entry *h;
6102 char *p, *copy;
6103 size_t len, first;
6104
6105 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
6106 if (h != NULL)
6107 return h;
6108
6109 /* If this is a default version (the name contains @@), look up the
6110 symbol again with only one `@' as well as without the version.
6111 The effect is that references to the symbol with and without the
6112 version will be matched by the default symbol in the archive. */
6113
6114 p = strchr (name, ELF_VER_CHR);
6115 if (p == NULL || p[1] != ELF_VER_CHR)
6116 {
6117 /* Add this symbol to first hash if this archive has the first
6118 definition. */
6119 if (is_elf_hash_table (info->hash))
6120 elf_link_add_to_first_hash (abfd, info, name, false);
6121 return h;
6122 }
6123
6124 /* First check with only one `@'. */
6125 len = strlen (name);
6126 copy = (char *) bfd_alloc (abfd, len);
6127 if (copy == NULL)
6128 return (struct bfd_link_hash_entry *) -1;
6129
6130 first = p - name + 1;
6131 memcpy (copy, name, first);
6132 memcpy (copy + first, name + first + 1, len - first);
6133
6134 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6135 if (h == NULL)
6136 {
6137 /* We also need to check references to the symbol without the
6138 version. */
6139 copy[first - 1] = '\0';
6140 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
6141 }
6142
6143 bfd_release (abfd, copy);
6144 return h;
6145 }
6146
6147 /* Add symbols from an ELF archive file to the linker hash table. We
6148 don't use _bfd_generic_link_add_archive_symbols because we need to
6149 handle versioned symbols.
6150
6151 Fortunately, ELF archive handling is simpler than that done by
6152 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6153 oddities. In ELF, if we find a symbol in the archive map, and the
6154 symbol is currently undefined, we know that we must pull in that
6155 object file.
6156
6157 Unfortunately, we do have to make multiple passes over the symbol
6158 table until nothing further is resolved. */
6159
6160 static bool
6161 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6162 {
6163 symindex c;
6164 unsigned char *included = NULL;
6165 carsym *symdefs;
6166 bool loop;
6167 size_t amt;
6168 const struct elf_backend_data *bed;
6169 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6170 (bfd *, struct bfd_link_info *, const char *);
6171
6172 if (! bfd_has_map (abfd))
6173 {
6174 /* An empty archive is a special case. */
6175 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6176 return true;
6177 bfd_set_error (bfd_error_no_armap);
6178 return false;
6179 }
6180
6181 /* Keep track of all symbols we know to be already defined, and all
6182 files we know to be already included. This is to speed up the
6183 second and subsequent passes. */
6184 c = bfd_ardata (abfd)->symdef_count;
6185 if (c == 0)
6186 return true;
6187 amt = c * sizeof (*included);
6188 included = (unsigned char *) bfd_zmalloc (amt);
6189 if (included == NULL)
6190 return false;
6191
6192 symdefs = bfd_ardata (abfd)->symdefs;
6193 bed = get_elf_backend_data (abfd);
6194 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6195
6196 do
6197 {
6198 file_ptr last;
6199 symindex i;
6200 carsym *symdef;
6201 carsym *symdefend;
6202
6203 loop = false;
6204 last = -1;
6205
6206 symdef = symdefs;
6207 symdefend = symdef + c;
6208 for (i = 0; symdef < symdefend; symdef++, i++)
6209 {
6210 struct bfd_link_hash_entry *h;
6211 bfd *element;
6212 struct bfd_link_hash_entry *undefs_tail;
6213 symindex mark;
6214
6215 if (included[i])
6216 continue;
6217 if (symdef->file_offset == last)
6218 {
6219 included[i] = true;
6220 continue;
6221 }
6222
6223 h = archive_symbol_lookup (abfd, info, symdef->name);
6224 if (h == (struct bfd_link_hash_entry *) -1)
6225 goto error_return;
6226
6227 if (h == NULL)
6228 continue;
6229
6230 if (h->type == bfd_link_hash_undefined)
6231 {
6232 /* If the archive element has already been loaded then one
6233 of the symbols defined by that element might have been
6234 made undefined due to being in a discarded section. */
6235 if (is_elf_hash_table (info->hash)
6236 && ((struct elf_link_hash_entry *) h)->indx == -3)
6237 continue;
6238 }
6239 else if (h->type == bfd_link_hash_common)
6240 {
6241 /* We currently have a common symbol. The archive map contains
6242 a reference to this symbol, so we may want to include it. We
6243 only want to include it however, if this archive element
6244 contains a definition of the symbol, not just another common
6245 declaration of it.
6246
6247 Unfortunately some archivers (including GNU ar) will put
6248 declarations of common symbols into their archive maps, as
6249 well as real definitions, so we cannot just go by the archive
6250 map alone. Instead we must read in the element's symbol
6251 table and check that to see what kind of symbol definition
6252 this is. */
6253 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6254 continue;
6255 }
6256 else
6257 {
6258 if (h->type != bfd_link_hash_undefweak)
6259 /* Symbol must be defined. Don't check it again. */
6260 included[i] = true;
6261
6262 if (!is_elf_hash_table (info->hash))
6263 continue;
6264 /* Ignore the archive if the symbol isn't defined in a
6265 shared object. */
6266 if (!((struct elf_link_hash_entry *) h)->def_dynamic)
6267 continue;
6268 /* Ignore the dynamic definition if symbol is first
6269 defined in this archive. */
6270 struct elf_link_hash_table *htab = elf_hash_table (info);
6271 if (htab->first_hash == NULL)
6272 continue;
6273 struct elf_link_first_hash_entry *e
6274 = ((struct elf_link_first_hash_entry *)
6275 bfd_hash_lookup (htab->first_hash, symdef->name,
6276 false, false));
6277 if (e == NULL || e->abfd != abfd)
6278 continue;
6279 }
6280
6281 /* We need to include this archive member. */
6282 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6283 info);
6284 if (element == NULL)
6285 goto error_return;
6286
6287 if (! bfd_check_format (element, bfd_object))
6288 goto error_return;
6289
6290 undefs_tail = info->hash->undefs_tail;
6291
6292 if (!(*info->callbacks
6293 ->add_archive_element) (info, element, symdef->name, &element))
6294 continue;
6295 if (!bfd_link_add_symbols (element, info))
6296 goto error_return;
6297
6298 /* If there are any new undefined symbols, we need to make
6299 another pass through the archive in order to see whether
6300 they can be defined. FIXME: This isn't perfect, because
6301 common symbols wind up on undefs_tail and because an
6302 undefined symbol which is defined later on in this pass
6303 does not require another pass. This isn't a bug, but it
6304 does make the code less efficient than it could be. */
6305 if (undefs_tail != info->hash->undefs_tail)
6306 loop = true;
6307
6308 /* Look backward to mark all symbols from this object file
6309 which we have already seen in this pass. */
6310 mark = i;
6311 do
6312 {
6313 included[mark] = true;
6314 if (mark == 0)
6315 break;
6316 --mark;
6317 }
6318 while (symdefs[mark].file_offset == symdef->file_offset);
6319
6320 /* We mark subsequent symbols from this object file as we go
6321 on through the loop. */
6322 last = symdef->file_offset;
6323 }
6324 }
6325 while (loop);
6326
6327 free (included);
6328 return true;
6329
6330 error_return:
6331 free (included);
6332 return false;
6333 }
6334
6335 /* Given an ELF BFD, add symbols to the global hash table as
6336 appropriate. */
6337
6338 bool
6339 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6340 {
6341 switch (bfd_get_format (abfd))
6342 {
6343 case bfd_object:
6344 return elf_link_add_object_symbols (abfd, info);
6345 case bfd_archive:
6346 return elf_link_add_archive_symbols (abfd, info);
6347 default:
6348 bfd_set_error (bfd_error_wrong_format);
6349 return false;
6350 }
6351 }
6352 \f
6353 struct hash_codes_info
6354 {
6355 unsigned long *hashcodes;
6356 bool error;
6357 };
6358
6359 /* This function will be called though elf_link_hash_traverse to store
6360 all hash value of the exported symbols in an array. */
6361
6362 static bool
6363 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6364 {
6365 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6366 const char *name;
6367 unsigned long ha;
6368 char *alc = NULL;
6369
6370 /* Ignore indirect symbols. These are added by the versioning code. */
6371 if (h->dynindx == -1)
6372 return true;
6373
6374 name = h->root.root.string;
6375 if (h->versioned >= versioned)
6376 {
6377 char *p = strchr (name, ELF_VER_CHR);
6378 if (p != NULL)
6379 {
6380 alc = (char *) bfd_malloc (p - name + 1);
6381 if (alc == NULL)
6382 {
6383 inf->error = true;
6384 return false;
6385 }
6386 memcpy (alc, name, p - name);
6387 alc[p - name] = '\0';
6388 name = alc;
6389 }
6390 }
6391
6392 /* Compute the hash value. */
6393 ha = bfd_elf_hash (name);
6394
6395 /* Store the found hash value in the array given as the argument. */
6396 *(inf->hashcodes)++ = ha;
6397
6398 /* And store it in the struct so that we can put it in the hash table
6399 later. */
6400 h->u.elf_hash_value = ha;
6401
6402 free (alc);
6403 return true;
6404 }
6405
6406 struct collect_gnu_hash_codes
6407 {
6408 bfd *output_bfd;
6409 const struct elf_backend_data *bed;
6410 unsigned long int nsyms;
6411 unsigned long int maskbits;
6412 unsigned long int *hashcodes;
6413 unsigned long int *hashval;
6414 unsigned long int *indx;
6415 unsigned long int *counts;
6416 bfd_vma *bitmask;
6417 bfd_byte *contents;
6418 bfd_size_type xlat;
6419 long int min_dynindx;
6420 unsigned long int bucketcount;
6421 unsigned long int symindx;
6422 long int local_indx;
6423 long int shift1, shift2;
6424 unsigned long int mask;
6425 bool error;
6426 };
6427
6428 /* This function will be called though elf_link_hash_traverse to store
6429 all hash value of the exported symbols in an array. */
6430
6431 static bool
6432 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6433 {
6434 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6435 const char *name;
6436 unsigned long ha;
6437 char *alc = NULL;
6438
6439 /* Ignore indirect symbols. These are added by the versioning code. */
6440 if (h->dynindx == -1)
6441 return true;
6442
6443 /* Ignore also local symbols and undefined symbols. */
6444 if (! (*s->bed->elf_hash_symbol) (h))
6445 return true;
6446
6447 name = h->root.root.string;
6448 if (h->versioned >= versioned)
6449 {
6450 char *p = strchr (name, ELF_VER_CHR);
6451 if (p != NULL)
6452 {
6453 alc = (char *) bfd_malloc (p - name + 1);
6454 if (alc == NULL)
6455 {
6456 s->error = true;
6457 return false;
6458 }
6459 memcpy (alc, name, p - name);
6460 alc[p - name] = '\0';
6461 name = alc;
6462 }
6463 }
6464
6465 /* Compute the hash value. */
6466 ha = bfd_elf_gnu_hash (name);
6467
6468 /* Store the found hash value in the array for compute_bucket_count,
6469 and also for .dynsym reordering purposes. */
6470 s->hashcodes[s->nsyms] = ha;
6471 s->hashval[h->dynindx] = ha;
6472 ++s->nsyms;
6473 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6474 s->min_dynindx = h->dynindx;
6475
6476 free (alc);
6477 return true;
6478 }
6479
6480 /* This function will be called though elf_link_hash_traverse to do
6481 final dynamic symbol renumbering in case of .gnu.hash.
6482 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6483 to the translation table. */
6484
6485 static bool
6486 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6487 {
6488 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6489 unsigned long int bucket;
6490 unsigned long int val;
6491
6492 /* Ignore indirect symbols. */
6493 if (h->dynindx == -1)
6494 return true;
6495
6496 /* Ignore also local symbols and undefined symbols. */
6497 if (! (*s->bed->elf_hash_symbol) (h))
6498 {
6499 if (h->dynindx >= s->min_dynindx)
6500 {
6501 if (s->bed->record_xhash_symbol != NULL)
6502 {
6503 (*s->bed->record_xhash_symbol) (h, 0);
6504 s->local_indx++;
6505 }
6506 else
6507 h->dynindx = s->local_indx++;
6508 }
6509 return true;
6510 }
6511
6512 bucket = s->hashval[h->dynindx] % s->bucketcount;
6513 val = (s->hashval[h->dynindx] >> s->shift1)
6514 & ((s->maskbits >> s->shift1) - 1);
6515 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6516 s->bitmask[val]
6517 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6518 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6519 if (s->counts[bucket] == 1)
6520 /* Last element terminates the chain. */
6521 val |= 1;
6522 bfd_put_32 (s->output_bfd, val,
6523 s->contents + (s->indx[bucket] - s->symindx) * 4);
6524 --s->counts[bucket];
6525 if (s->bed->record_xhash_symbol != NULL)
6526 {
6527 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6528
6529 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6530 }
6531 else
6532 h->dynindx = s->indx[bucket]++;
6533 return true;
6534 }
6535
6536 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6537
6538 bool
6539 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6540 {
6541 return !(h->forced_local
6542 || h->root.type == bfd_link_hash_undefined
6543 || h->root.type == bfd_link_hash_undefweak
6544 || ((h->root.type == bfd_link_hash_defined
6545 || h->root.type == bfd_link_hash_defweak)
6546 && h->root.u.def.section->output_section == NULL));
6547 }
6548
6549 /* Array used to determine the number of hash table buckets to use
6550 based on the number of symbols there are. If there are fewer than
6551 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6552 fewer than 37 we use 17 buckets, and so forth. We never use more
6553 than 32771 buckets. */
6554
6555 static const size_t elf_buckets[] =
6556 {
6557 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6558 16411, 32771, 0
6559 };
6560
6561 /* Compute bucket count for hashing table. We do not use a static set
6562 of possible tables sizes anymore. Instead we determine for all
6563 possible reasonable sizes of the table the outcome (i.e., the
6564 number of collisions etc) and choose the best solution. The
6565 weighting functions are not too simple to allow the table to grow
6566 without bounds. Instead one of the weighting factors is the size.
6567 Therefore the result is always a good payoff between few collisions
6568 (= short chain lengths) and table size. */
6569 static size_t
6570 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6571 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6572 unsigned long int nsyms,
6573 int gnu_hash)
6574 {
6575 size_t best_size = 0;
6576 unsigned long int i;
6577
6578 if (info->optimize)
6579 {
6580 size_t minsize;
6581 size_t maxsize;
6582 uint64_t best_chlen = ~((uint64_t) 0);
6583 bfd *dynobj = elf_hash_table (info)->dynobj;
6584 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6585 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6586 unsigned long int *counts;
6587 bfd_size_type amt;
6588 unsigned int no_improvement_count = 0;
6589
6590 /* Possible optimization parameters: if we have NSYMS symbols we say
6591 that the hashing table must at least have NSYMS/4 and at most
6592 2*NSYMS buckets. */
6593 minsize = nsyms / 4;
6594 if (minsize == 0)
6595 minsize = 1;
6596 best_size = maxsize = nsyms * 2;
6597 if (gnu_hash)
6598 {
6599 if (minsize < 2)
6600 minsize = 2;
6601 if ((best_size & 31) == 0)
6602 ++best_size;
6603 }
6604
6605 /* Create array where we count the collisions in. We must use bfd_malloc
6606 since the size could be large. */
6607 amt = maxsize;
6608 amt *= sizeof (unsigned long int);
6609 counts = (unsigned long int *) bfd_malloc (amt);
6610 if (counts == NULL)
6611 return 0;
6612
6613 /* Compute the "optimal" size for the hash table. The criteria is a
6614 minimal chain length. The minor criteria is (of course) the size
6615 of the table. */
6616 for (i = minsize; i < maxsize; ++i)
6617 {
6618 /* Walk through the array of hashcodes and count the collisions. */
6619 uint64_t max;
6620 unsigned long int j;
6621 unsigned long int fact;
6622
6623 if (gnu_hash && (i & 31) == 0)
6624 continue;
6625
6626 memset (counts, '\0', i * sizeof (unsigned long int));
6627
6628 /* Determine how often each hash bucket is used. */
6629 for (j = 0; j < nsyms; ++j)
6630 ++counts[hashcodes[j] % i];
6631
6632 /* For the weight function we need some information about the
6633 pagesize on the target. This is information need not be 100%
6634 accurate. Since this information is not available (so far) we
6635 define it here to a reasonable default value. If it is crucial
6636 to have a better value some day simply define this value. */
6637 # ifndef BFD_TARGET_PAGESIZE
6638 # define BFD_TARGET_PAGESIZE (4096)
6639 # endif
6640
6641 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6642 and the chains. */
6643 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6644
6645 # if 1
6646 /* Variant 1: optimize for short chains. We add the squares
6647 of all the chain lengths (which favors many small chain
6648 over a few long chains). */
6649 for (j = 0; j < i; ++j)
6650 max += counts[j] * counts[j];
6651
6652 /* This adds penalties for the overall size of the table. */
6653 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6654 max *= fact * fact;
6655 # else
6656 /* Variant 2: Optimize a lot more for small table. Here we
6657 also add squares of the size but we also add penalties for
6658 empty slots (the +1 term). */
6659 for (j = 0; j < i; ++j)
6660 max += (1 + counts[j]) * (1 + counts[j]);
6661
6662 /* The overall size of the table is considered, but not as
6663 strong as in variant 1, where it is squared. */
6664 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6665 max *= fact;
6666 # endif
6667
6668 /* Compare with current best results. */
6669 if (max < best_chlen)
6670 {
6671 best_chlen = max;
6672 best_size = i;
6673 no_improvement_count = 0;
6674 }
6675 /* PR 11843: Avoid futile long searches for the best bucket size
6676 when there are a large number of symbols. */
6677 else if (++no_improvement_count == 100)
6678 break;
6679 }
6680
6681 free (counts);
6682 }
6683 else
6684 {
6685 for (i = 0; elf_buckets[i] != 0; i++)
6686 {
6687 best_size = elf_buckets[i];
6688 if (nsyms < elf_buckets[i + 1])
6689 break;
6690 }
6691 if (gnu_hash && best_size < 2)
6692 best_size = 2;
6693 }
6694
6695 return best_size;
6696 }
6697
6698 /* Size any SHT_GROUP section for ld -r. */
6699
6700 bool
6701 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6702 {
6703 bfd *ibfd;
6704 asection *s;
6705
6706 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6707 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6708 && (s = ibfd->sections) != NULL
6709 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6710 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6711 return false;
6712 return true;
6713 }
6714
6715 /* Set a default stack segment size. The value in INFO wins. If it
6716 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6717 undefined it is initialized. */
6718
6719 bool
6720 bfd_elf_stack_segment_size (bfd *output_bfd,
6721 struct bfd_link_info *info,
6722 const char *legacy_symbol,
6723 bfd_vma default_size)
6724 {
6725 struct elf_link_hash_entry *h = NULL;
6726
6727 /* Look for legacy symbol. */
6728 if (legacy_symbol)
6729 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6730 false, false, false);
6731 if (h && (h->root.type == bfd_link_hash_defined
6732 || h->root.type == bfd_link_hash_defweak)
6733 && h->def_regular
6734 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6735 {
6736 /* The symbol has no type if specified on the command line. */
6737 h->type = STT_OBJECT;
6738 if (info->stacksize)
6739 /* xgettext:c-format */
6740 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6741 output_bfd, legacy_symbol);
6742 else if (h->root.u.def.section != bfd_abs_section_ptr)
6743 /* xgettext:c-format */
6744 _bfd_error_handler (_("%pB: %s not absolute"),
6745 output_bfd, legacy_symbol);
6746 else
6747 info->stacksize = h->root.u.def.value;
6748 }
6749
6750 if (!info->stacksize)
6751 /* If the user didn't set a size, or explicitly inhibit the
6752 size, set it now. */
6753 info->stacksize = default_size;
6754
6755 /* Provide the legacy symbol, if it is referenced. */
6756 if (h && (h->root.type == bfd_link_hash_undefined
6757 || h->root.type == bfd_link_hash_undefweak))
6758 {
6759 struct bfd_link_hash_entry *bh = NULL;
6760
6761 if (!(_bfd_generic_link_add_one_symbol
6762 (info, output_bfd, legacy_symbol,
6763 BSF_GLOBAL, bfd_abs_section_ptr,
6764 info->stacksize >= 0 ? info->stacksize : 0,
6765 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6766 return false;
6767
6768 h = (struct elf_link_hash_entry *) bh;
6769 h->def_regular = 1;
6770 h->type = STT_OBJECT;
6771 }
6772
6773 return true;
6774 }
6775
6776 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6777
6778 struct elf_gc_sweep_symbol_info
6779 {
6780 struct bfd_link_info *info;
6781 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6782 bool);
6783 };
6784
6785 static bool
6786 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6787 {
6788 if (!h->mark
6789 && (((h->root.type == bfd_link_hash_defined
6790 || h->root.type == bfd_link_hash_defweak)
6791 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6792 && h->root.u.def.section->gc_mark))
6793 || h->root.type == bfd_link_hash_undefined
6794 || h->root.type == bfd_link_hash_undefweak))
6795 {
6796 struct elf_gc_sweep_symbol_info *inf;
6797
6798 inf = (struct elf_gc_sweep_symbol_info *) data;
6799 (*inf->hide_symbol) (inf->info, h, true);
6800 h->def_regular = 0;
6801 h->ref_regular = 0;
6802 h->ref_regular_nonweak = 0;
6803 }
6804
6805 return true;
6806 }
6807
6808 /* Set up the sizes and contents of the ELF dynamic sections. This is
6809 called by the ELF linker emulation before_allocation routine. We
6810 must set the sizes of the sections before the linker sets the
6811 addresses of the various sections. */
6812
6813 bool
6814 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6815 const char *soname,
6816 const char *rpath,
6817 const char *filter_shlib,
6818 const char *audit,
6819 const char *depaudit,
6820 const char * const *auxiliary_filters,
6821 struct bfd_link_info *info,
6822 asection **sinterpptr)
6823 {
6824 bfd *dynobj;
6825 const struct elf_backend_data *bed;
6826
6827 *sinterpptr = NULL;
6828
6829 if (!is_elf_hash_table (info->hash))
6830 return true;
6831
6832 /* Any syms created from now on start with -1 in
6833 got.refcount/offset and plt.refcount/offset. */
6834 elf_hash_table (info)->init_got_refcount
6835 = elf_hash_table (info)->init_got_offset;
6836 elf_hash_table (info)->init_plt_refcount
6837 = elf_hash_table (info)->init_plt_offset;
6838
6839 bed = get_elf_backend_data (output_bfd);
6840
6841 /* The backend may have to create some sections regardless of whether
6842 we're dynamic or not. */
6843 if (bed->elf_backend_early_size_sections
6844 && !bed->elf_backend_early_size_sections (output_bfd, info))
6845 return false;
6846
6847 dynobj = elf_hash_table (info)->dynobj;
6848
6849 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6850 {
6851 struct bfd_elf_version_tree *verdefs;
6852 struct elf_info_failed asvinfo;
6853 struct bfd_elf_version_tree *t;
6854 struct bfd_elf_version_expr *d;
6855 asection *s;
6856 size_t soname_indx;
6857
6858 /* If we are supposed to export all symbols into the dynamic symbol
6859 table (this is not the normal case), then do so. */
6860 if (info->export_dynamic
6861 || (bfd_link_executable (info) && info->dynamic))
6862 {
6863 struct elf_info_failed eif;
6864
6865 eif.info = info;
6866 eif.failed = false;
6867 elf_link_hash_traverse (elf_hash_table (info),
6868 _bfd_elf_export_symbol,
6869 &eif);
6870 if (eif.failed)
6871 return false;
6872 }
6873
6874 if (soname != NULL)
6875 {
6876 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6877 soname, true);
6878 if (soname_indx == (size_t) -1
6879 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6880 return false;
6881 }
6882 else
6883 soname_indx = (size_t) -1;
6884
6885 /* Make all global versions with definition. */
6886 for (t = info->version_info; t != NULL; t = t->next)
6887 for (d = t->globals.list; d != NULL; d = d->next)
6888 if (!d->symver && d->literal)
6889 {
6890 const char *verstr, *name;
6891 size_t namelen, verlen, newlen;
6892 char *newname, *p, leading_char;
6893 struct elf_link_hash_entry *newh;
6894
6895 leading_char = bfd_get_symbol_leading_char (output_bfd);
6896 name = d->pattern;
6897 namelen = strlen (name) + (leading_char != '\0');
6898 verstr = t->name;
6899 verlen = strlen (verstr);
6900 newlen = namelen + verlen + 3;
6901
6902 newname = (char *) bfd_malloc (newlen);
6903 if (newname == NULL)
6904 return false;
6905 newname[0] = leading_char;
6906 memcpy (newname + (leading_char != '\0'), name, namelen);
6907
6908 /* Check the hidden versioned definition. */
6909 p = newname + namelen;
6910 *p++ = ELF_VER_CHR;
6911 memcpy (p, verstr, verlen + 1);
6912 newh = elf_link_hash_lookup (elf_hash_table (info),
6913 newname, false, false,
6914 false);
6915 if (newh == NULL
6916 || (newh->root.type != bfd_link_hash_defined
6917 && newh->root.type != bfd_link_hash_defweak))
6918 {
6919 /* Check the default versioned definition. */
6920 *p++ = ELF_VER_CHR;
6921 memcpy (p, verstr, verlen + 1);
6922 newh = elf_link_hash_lookup (elf_hash_table (info),
6923 newname, false, false,
6924 false);
6925 }
6926 free (newname);
6927
6928 /* Mark this version if there is a definition and it is
6929 not defined in a shared object. */
6930 if (newh != NULL
6931 && !newh->def_dynamic
6932 && (newh->root.type == bfd_link_hash_defined
6933 || newh->root.type == bfd_link_hash_defweak))
6934 d->symver = 1;
6935 }
6936
6937 /* Attach all the symbols to their version information. */
6938 asvinfo.info = info;
6939 asvinfo.failed = false;
6940
6941 elf_link_hash_traverse (elf_hash_table (info),
6942 _bfd_elf_link_assign_sym_version,
6943 &asvinfo);
6944 if (asvinfo.failed)
6945 return false;
6946
6947 if (!info->allow_undefined_version)
6948 {
6949 /* Check if all global versions have a definition. */
6950 bool all_defined = true;
6951 for (t = info->version_info; t != NULL; t = t->next)
6952 for (d = t->globals.list; d != NULL; d = d->next)
6953 if (d->literal && !d->symver && !d->script)
6954 {
6955 _bfd_error_handler
6956 (_("%s: undefined version: %s"),
6957 d->pattern, t->name);
6958 all_defined = false;
6959 }
6960
6961 if (!all_defined)
6962 {
6963 bfd_set_error (bfd_error_bad_value);
6964 return false;
6965 }
6966 }
6967
6968 /* Set up the version definition section. */
6969 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6970 BFD_ASSERT (s != NULL);
6971
6972 /* We may have created additional version definitions if we are
6973 just linking a regular application. */
6974 verdefs = info->version_info;
6975
6976 /* Skip anonymous version tag. */
6977 if (verdefs != NULL && verdefs->vernum == 0)
6978 verdefs = verdefs->next;
6979
6980 if (verdefs == NULL && !info->create_default_symver)
6981 s->flags |= SEC_EXCLUDE;
6982 else
6983 {
6984 unsigned int cdefs;
6985 bfd_size_type size;
6986 bfd_byte *p;
6987 Elf_Internal_Verdef def;
6988 Elf_Internal_Verdaux defaux;
6989 struct bfd_link_hash_entry *bh;
6990 struct elf_link_hash_entry *h;
6991 const char *name;
6992
6993 cdefs = 0;
6994 size = 0;
6995
6996 /* Make space for the base version. */
6997 size += sizeof (Elf_External_Verdef);
6998 size += sizeof (Elf_External_Verdaux);
6999 ++cdefs;
7000
7001 /* Make space for the default version. */
7002 if (info->create_default_symver)
7003 {
7004 size += sizeof (Elf_External_Verdef);
7005 ++cdefs;
7006 }
7007
7008 for (t = verdefs; t != NULL; t = t->next)
7009 {
7010 struct bfd_elf_version_deps *n;
7011
7012 /* Don't emit base version twice. */
7013 if (t->vernum == 0)
7014 continue;
7015
7016 size += sizeof (Elf_External_Verdef);
7017 size += sizeof (Elf_External_Verdaux);
7018 ++cdefs;
7019
7020 for (n = t->deps; n != NULL; n = n->next)
7021 size += sizeof (Elf_External_Verdaux);
7022 }
7023
7024 s->size = size;
7025 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7026 if (s->contents == NULL && s->size != 0)
7027 return false;
7028
7029 /* Fill in the version definition section. */
7030
7031 p = s->contents;
7032
7033 def.vd_version = VER_DEF_CURRENT;
7034 def.vd_flags = VER_FLG_BASE;
7035 def.vd_ndx = 1;
7036 def.vd_cnt = 1;
7037 if (info->create_default_symver)
7038 {
7039 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7040 def.vd_next = sizeof (Elf_External_Verdef);
7041 }
7042 else
7043 {
7044 def.vd_aux = sizeof (Elf_External_Verdef);
7045 def.vd_next = (sizeof (Elf_External_Verdef)
7046 + sizeof (Elf_External_Verdaux));
7047 }
7048
7049 if (soname_indx != (size_t) -1)
7050 {
7051 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7052 soname_indx);
7053 def.vd_hash = bfd_elf_hash (soname);
7054 defaux.vda_name = soname_indx;
7055 name = soname;
7056 }
7057 else
7058 {
7059 size_t indx;
7060
7061 name = lbasename (bfd_get_filename (output_bfd));
7062 def.vd_hash = bfd_elf_hash (name);
7063 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7064 name, false);
7065 if (indx == (size_t) -1)
7066 return false;
7067 defaux.vda_name = indx;
7068 }
7069 defaux.vda_next = 0;
7070
7071 _bfd_elf_swap_verdef_out (output_bfd, &def,
7072 (Elf_External_Verdef *) p);
7073 p += sizeof (Elf_External_Verdef);
7074 if (info->create_default_symver)
7075 {
7076 /* Add a symbol representing this version. */
7077 bh = NULL;
7078 if (! (_bfd_generic_link_add_one_symbol
7079 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
7080 0, NULL, false,
7081 get_elf_backend_data (dynobj)->collect, &bh)))
7082 return false;
7083 h = (struct elf_link_hash_entry *) bh;
7084 h->non_elf = 0;
7085 h->def_regular = 1;
7086 h->type = STT_OBJECT;
7087 h->verinfo.vertree = NULL;
7088
7089 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7090 return false;
7091
7092 /* Create a duplicate of the base version with the same
7093 aux block, but different flags. */
7094 def.vd_flags = 0;
7095 def.vd_ndx = 2;
7096 def.vd_aux = sizeof (Elf_External_Verdef);
7097 if (verdefs)
7098 def.vd_next = (sizeof (Elf_External_Verdef)
7099 + sizeof (Elf_External_Verdaux));
7100 else
7101 def.vd_next = 0;
7102 _bfd_elf_swap_verdef_out (output_bfd, &def,
7103 (Elf_External_Verdef *) p);
7104 p += sizeof (Elf_External_Verdef);
7105 }
7106 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7107 (Elf_External_Verdaux *) p);
7108 p += sizeof (Elf_External_Verdaux);
7109
7110 for (t = verdefs; t != NULL; t = t->next)
7111 {
7112 unsigned int cdeps;
7113 struct bfd_elf_version_deps *n;
7114
7115 /* Don't emit the base version twice. */
7116 if (t->vernum == 0)
7117 continue;
7118
7119 cdeps = 0;
7120 for (n = t->deps; n != NULL; n = n->next)
7121 ++cdeps;
7122
7123 /* Add a symbol representing this version. */
7124 bh = NULL;
7125 if (! (_bfd_generic_link_add_one_symbol
7126 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
7127 0, NULL, false,
7128 get_elf_backend_data (dynobj)->collect, &bh)))
7129 return false;
7130 h = (struct elf_link_hash_entry *) bh;
7131 h->non_elf = 0;
7132 h->def_regular = 1;
7133 h->type = STT_OBJECT;
7134 h->verinfo.vertree = t;
7135
7136 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7137 return false;
7138
7139 def.vd_version = VER_DEF_CURRENT;
7140 def.vd_flags = 0;
7141 if (t->globals.list == NULL
7142 && t->locals.list == NULL
7143 && ! t->used)
7144 def.vd_flags |= VER_FLG_WEAK;
7145 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
7146 def.vd_cnt = cdeps + 1;
7147 def.vd_hash = bfd_elf_hash (t->name);
7148 def.vd_aux = sizeof (Elf_External_Verdef);
7149 def.vd_next = 0;
7150
7151 /* If a basever node is next, it *must* be the last node in
7152 the chain, otherwise Verdef construction breaks. */
7153 if (t->next != NULL && t->next->vernum == 0)
7154 BFD_ASSERT (t->next->next == NULL);
7155
7156 if (t->next != NULL && t->next->vernum != 0)
7157 def.vd_next = (sizeof (Elf_External_Verdef)
7158 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7159
7160 _bfd_elf_swap_verdef_out (output_bfd, &def,
7161 (Elf_External_Verdef *) p);
7162 p += sizeof (Elf_External_Verdef);
7163
7164 defaux.vda_name = h->dynstr_index;
7165 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7166 h->dynstr_index);
7167 defaux.vda_next = 0;
7168 if (t->deps != NULL)
7169 defaux.vda_next = sizeof (Elf_External_Verdaux);
7170 t->name_indx = defaux.vda_name;
7171
7172 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7173 (Elf_External_Verdaux *) p);
7174 p += sizeof (Elf_External_Verdaux);
7175
7176 for (n = t->deps; n != NULL; n = n->next)
7177 {
7178 if (n->version_needed == NULL)
7179 {
7180 /* This can happen if there was an error in the
7181 version script. */
7182 defaux.vda_name = 0;
7183 }
7184 else
7185 {
7186 defaux.vda_name = n->version_needed->name_indx;
7187 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7188 defaux.vda_name);
7189 }
7190 if (n->next == NULL)
7191 defaux.vda_next = 0;
7192 else
7193 defaux.vda_next = sizeof (Elf_External_Verdaux);
7194
7195 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7196 (Elf_External_Verdaux *) p);
7197 p += sizeof (Elf_External_Verdaux);
7198 }
7199 }
7200
7201 elf_tdata (output_bfd)->cverdefs = cdefs;
7202 }
7203 }
7204
7205 if (info->gc_sections && bed->can_gc_sections)
7206 {
7207 struct elf_gc_sweep_symbol_info sweep_info;
7208
7209 /* Remove the symbols that were in the swept sections from the
7210 dynamic symbol table. */
7211 sweep_info.info = info;
7212 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7213 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7214 &sweep_info);
7215 }
7216
7217 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7218 {
7219 asection *s;
7220 struct elf_find_verdep_info sinfo;
7221
7222 /* Work out the size of the version reference section. */
7223
7224 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7225 BFD_ASSERT (s != NULL);
7226
7227 sinfo.info = info;
7228 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7229 if (sinfo.vers == 0)
7230 sinfo.vers = 1;
7231 sinfo.failed = false;
7232
7233 elf_link_hash_traverse (elf_hash_table (info),
7234 _bfd_elf_link_find_version_dependencies,
7235 &sinfo);
7236 if (sinfo.failed)
7237 return false;
7238
7239 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7240 if (sinfo.failed)
7241 return false;
7242
7243 if (elf_tdata (output_bfd)->verref == NULL)
7244 s->flags |= SEC_EXCLUDE;
7245 else
7246 {
7247 Elf_Internal_Verneed *vn;
7248 unsigned int size;
7249 unsigned int crefs;
7250 bfd_byte *p;
7251
7252 /* Build the version dependency section. */
7253 size = 0;
7254 crefs = 0;
7255 for (vn = elf_tdata (output_bfd)->verref;
7256 vn != NULL;
7257 vn = vn->vn_nextref)
7258 {
7259 Elf_Internal_Vernaux *a;
7260
7261 size += sizeof (Elf_External_Verneed);
7262 ++crefs;
7263 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7264 size += sizeof (Elf_External_Vernaux);
7265 }
7266
7267 s->size = size;
7268 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7269 if (s->contents == NULL)
7270 return false;
7271
7272 p = s->contents;
7273 for (vn = elf_tdata (output_bfd)->verref;
7274 vn != NULL;
7275 vn = vn->vn_nextref)
7276 {
7277 unsigned int caux;
7278 Elf_Internal_Vernaux *a;
7279 size_t indx;
7280
7281 caux = 0;
7282 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7283 ++caux;
7284
7285 vn->vn_version = VER_NEED_CURRENT;
7286 vn->vn_cnt = caux;
7287 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7288 elf_dt_name (vn->vn_bfd) != NULL
7289 ? elf_dt_name (vn->vn_bfd)
7290 : lbasename (bfd_get_filename
7291 (vn->vn_bfd)),
7292 false);
7293 if (indx == (size_t) -1)
7294 return false;
7295 vn->vn_file = indx;
7296 vn->vn_aux = sizeof (Elf_External_Verneed);
7297 if (vn->vn_nextref == NULL)
7298 vn->vn_next = 0;
7299 else
7300 vn->vn_next = (sizeof (Elf_External_Verneed)
7301 + caux * sizeof (Elf_External_Vernaux));
7302
7303 _bfd_elf_swap_verneed_out (output_bfd, vn,
7304 (Elf_External_Verneed *) p);
7305 p += sizeof (Elf_External_Verneed);
7306
7307 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7308 {
7309 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7310 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7311 a->vna_nodename, false);
7312 if (indx == (size_t) -1)
7313 return false;
7314 a->vna_name = indx;
7315 if (a->vna_nextptr == NULL)
7316 a->vna_next = 0;
7317 else
7318 a->vna_next = sizeof (Elf_External_Vernaux);
7319
7320 _bfd_elf_swap_vernaux_out (output_bfd, a,
7321 (Elf_External_Vernaux *) p);
7322 p += sizeof (Elf_External_Vernaux);
7323 }
7324 }
7325
7326 elf_tdata (output_bfd)->cverrefs = crefs;
7327 }
7328 }
7329
7330 if (bfd_link_relocatable (info)
7331 && !_bfd_elf_size_group_sections (info))
7332 return false;
7333
7334 /* Determine any GNU_STACK segment requirements, after the backend
7335 has had a chance to set a default segment size. */
7336 if (info->execstack)
7337 {
7338 /* If the user has explicitly requested warnings, then generate one even
7339 though the choice is the result of another command line option. */
7340 if (info->warn_execstack == 1)
7341 {
7342 if (info->error_execstack)
7343 {
7344 _bfd_error_handler
7345 (_("\
7346 error: creating an executable stack because of -z execstack command line option"));
7347 return false;
7348 }
7349
7350 _bfd_error_handler
7351 (_("\
7352 warning: enabling an executable stack because of -z execstack command line option"));
7353 }
7354
7355 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7356 }
7357 else if (info->noexecstack)
7358 elf_stack_flags (output_bfd) = PF_R | PF_W;
7359 else
7360 {
7361 bfd *inputobj;
7362 asection *notesec = NULL;
7363 bfd *noteobj = NULL;
7364 bfd *emptyobj = NULL;
7365 int exec = 0;
7366
7367 for (inputobj = info->input_bfds;
7368 inputobj;
7369 inputobj = inputobj->link.next)
7370 {
7371 asection *s;
7372
7373 if (inputobj->flags
7374 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7375 continue;
7376 s = inputobj->sections;
7377 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7378 continue;
7379
7380 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7381 if (s)
7382 {
7383 notesec = s;
7384 if (s->flags & SEC_CODE)
7385 {
7386 noteobj = inputobj;
7387 exec = PF_X;
7388 /* There is no point in scanning the remaining bfds. */
7389 break;
7390 }
7391 }
7392 else if (bed->default_execstack && info->default_execstack)
7393 {
7394 exec = PF_X;
7395 emptyobj = inputobj;
7396 }
7397 }
7398
7399 if (notesec || info->stacksize > 0)
7400 {
7401 if (exec)
7402 {
7403 if (info->warn_execstack != 0)
7404 {
7405 /* PR 29072: Because an executable stack is a serious
7406 security risk, make sure that the user knows that it is
7407 being enabled despite the fact that it was not requested
7408 on the command line. */
7409 if (noteobj)
7410 {
7411 if (info->error_execstack)
7412 {
7413 _bfd_error_handler (_("\
7414 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7415 bfd_get_filename (noteobj));
7416 return false;
7417 }
7418
7419 _bfd_error_handler (_("\
7420 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7421 bfd_get_filename (noteobj));
7422 }
7423 else if (emptyobj)
7424 {
7425 if (info->error_execstack)
7426 {
7427 _bfd_error_handler (_("\
7428 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7429 bfd_get_filename (emptyobj));
7430 return false;
7431 }
7432
7433 _bfd_error_handler (_("\
7434 warning: %s: missing .note.GNU-stack section implies executable stack"),
7435 bfd_get_filename (emptyobj));
7436 _bfd_error_handler (_("\
7437 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7438 }
7439 }
7440 }
7441 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7442 }
7443
7444 if (notesec && exec && bfd_link_relocatable (info)
7445 && notesec->output_section != bfd_abs_section_ptr)
7446 notesec->output_section->flags |= SEC_CODE;
7447 }
7448
7449 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7450 {
7451 struct elf_info_failed eif;
7452 struct elf_link_hash_entry *h;
7453 asection *dynstr;
7454 asection *s;
7455
7456 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7457 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7458
7459 if (info->symbolic)
7460 {
7461 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7462 return false;
7463 info->flags |= DF_SYMBOLIC;
7464 }
7465
7466 if (rpath != NULL)
7467 {
7468 size_t indx;
7469 bfd_vma tag;
7470
7471 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7472 true);
7473 if (indx == (size_t) -1)
7474 return false;
7475
7476 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7477 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7478 return false;
7479 }
7480
7481 if (filter_shlib != NULL)
7482 {
7483 size_t indx;
7484
7485 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7486 filter_shlib, true);
7487 if (indx == (size_t) -1
7488 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7489 return false;
7490 }
7491
7492 if (auxiliary_filters != NULL)
7493 {
7494 const char * const *p;
7495
7496 for (p = auxiliary_filters; *p != NULL; p++)
7497 {
7498 size_t indx;
7499
7500 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7501 *p, true);
7502 if (indx == (size_t) -1
7503 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7504 return false;
7505 }
7506 }
7507
7508 if (audit != NULL)
7509 {
7510 size_t indx;
7511
7512 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7513 true);
7514 if (indx == (size_t) -1
7515 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7516 return false;
7517 }
7518
7519 if (depaudit != NULL)
7520 {
7521 size_t indx;
7522
7523 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7524 true);
7525 if (indx == (size_t) -1
7526 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7527 return false;
7528 }
7529
7530 eif.info = info;
7531 eif.failed = false;
7532
7533 /* Find all symbols which were defined in a dynamic object and make
7534 the backend pick a reasonable value for them. */
7535 elf_link_hash_traverse (elf_hash_table (info),
7536 _bfd_elf_adjust_dynamic_symbol,
7537 &eif);
7538 if (eif.failed)
7539 return false;
7540
7541 /* Add some entries to the .dynamic section. We fill in some of the
7542 values later, in bfd_elf_final_link, but we must add the entries
7543 now so that we know the final size of the .dynamic section. */
7544
7545 /* If there are initialization and/or finalization functions to
7546 call then add the corresponding DT_INIT/DT_FINI entries. */
7547 h = (info->init_function
7548 ? elf_link_hash_lookup (elf_hash_table (info),
7549 info->init_function, false,
7550 false, false)
7551 : NULL);
7552 if (h != NULL
7553 && (h->ref_regular
7554 || h->def_regular))
7555 {
7556 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7557 return false;
7558 }
7559 h = (info->fini_function
7560 ? elf_link_hash_lookup (elf_hash_table (info),
7561 info->fini_function, false,
7562 false, false)
7563 : NULL);
7564 if (h != NULL
7565 && (h->ref_regular
7566 || h->def_regular))
7567 {
7568 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7569 return false;
7570 }
7571
7572 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7573 if (s != NULL && s->linker_has_input)
7574 {
7575 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7576 if (! bfd_link_executable (info))
7577 {
7578 bfd *sub;
7579 asection *o;
7580
7581 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7582 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7583 && (o = sub->sections) != NULL
7584 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7585 for (o = sub->sections; o != NULL; o = o->next)
7586 if (elf_section_data (o)->this_hdr.sh_type
7587 == SHT_PREINIT_ARRAY)
7588 {
7589 _bfd_error_handler
7590 (_("%pB: .preinit_array section is not allowed in DSO"),
7591 sub);
7592 break;
7593 }
7594
7595 bfd_set_error (bfd_error_nonrepresentable_section);
7596 return false;
7597 }
7598
7599 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7600 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7601 return false;
7602 }
7603 s = bfd_get_section_by_name (output_bfd, ".init_array");
7604 if (s != NULL && s->linker_has_input)
7605 {
7606 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7607 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7608 return false;
7609 }
7610 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7611 if (s != NULL && s->linker_has_input)
7612 {
7613 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7614 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7615 return false;
7616 }
7617
7618 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7619 /* If .dynstr is excluded from the link, we don't want any of
7620 these tags. Strictly, we should be checking each section
7621 individually; This quick check covers for the case where
7622 someone does a /DISCARD/ : { *(*) }. */
7623 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7624 {
7625 bfd_size_type strsize;
7626
7627 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7628 if ((info->emit_hash
7629 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7630 || (info->emit_gnu_hash
7631 && (bed->record_xhash_symbol == NULL
7632 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7633 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7634 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7635 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7636 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7637 bed->s->sizeof_sym)
7638 || (info->gnu_flags_1
7639 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7640 info->gnu_flags_1)))
7641 return false;
7642 }
7643 }
7644
7645 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7646 return false;
7647
7648 /* The backend must work out the sizes of all the other dynamic
7649 sections. */
7650 if (bed->elf_backend_late_size_sections != NULL
7651 && !bed->elf_backend_late_size_sections (output_bfd, info))
7652 return false;
7653
7654 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7655 {
7656 if (elf_tdata (output_bfd)->cverdefs)
7657 {
7658 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7659
7660 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7661 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7662 return false;
7663 }
7664
7665 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7666 {
7667 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7668 return false;
7669 }
7670 else if (info->flags & DF_BIND_NOW)
7671 {
7672 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7673 return false;
7674 }
7675
7676 if (info->flags_1)
7677 {
7678 if (bfd_link_executable (info))
7679 info->flags_1 &= ~ (DF_1_INITFIRST
7680 | DF_1_NODELETE
7681 | DF_1_NOOPEN);
7682 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7683 return false;
7684 }
7685
7686 if (elf_tdata (output_bfd)->cverrefs)
7687 {
7688 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7689
7690 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7691 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7692 return false;
7693 }
7694
7695 if ((elf_tdata (output_bfd)->cverrefs == 0
7696 && elf_tdata (output_bfd)->cverdefs == 0)
7697 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7698 {
7699 asection *s;
7700
7701 s = bfd_get_linker_section (dynobj, ".gnu.version");
7702 s->flags |= SEC_EXCLUDE;
7703 }
7704 }
7705 return true;
7706 }
7707
7708 /* Find the first non-excluded output section. We'll use its
7709 section symbol for some emitted relocs. */
7710 void
7711 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7712 {
7713 asection *s;
7714 asection *found = NULL;
7715
7716 for (s = output_bfd->sections; s != NULL; s = s->next)
7717 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7718 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7719 {
7720 found = s;
7721 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7722 break;
7723 }
7724 elf_hash_table (info)->text_index_section = found;
7725 }
7726
7727 /* Find two non-excluded output sections, one for code, one for data.
7728 We'll use their section symbols for some emitted relocs. */
7729 void
7730 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7731 {
7732 asection *s;
7733 asection *found = NULL;
7734
7735 /* Data first, since setting text_index_section changes
7736 _bfd_elf_omit_section_dynsym_default. */
7737 for (s = output_bfd->sections; s != NULL; s = s->next)
7738 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7739 && !(s->flags & SEC_READONLY)
7740 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7741 {
7742 found = s;
7743 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7744 break;
7745 }
7746 elf_hash_table (info)->data_index_section = found;
7747
7748 for (s = output_bfd->sections; s != NULL; s = s->next)
7749 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7750 && (s->flags & SEC_READONLY)
7751 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7752 {
7753 found = s;
7754 break;
7755 }
7756 elf_hash_table (info)->text_index_section = found;
7757 }
7758
7759 #define GNU_HASH_SECTION_NAME(bed) \
7760 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7761
7762 bool
7763 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7764 {
7765 const struct elf_backend_data *bed;
7766 unsigned long section_sym_count;
7767 bfd_size_type dynsymcount = 0;
7768
7769 if (!is_elf_hash_table (info->hash))
7770 return true;
7771
7772 bed = get_elf_backend_data (output_bfd);
7773 (*bed->elf_backend_init_index_section) (output_bfd, info);
7774
7775 /* Assign dynsym indices. In a shared library we generate a section
7776 symbol for each output section, which come first. Next come all
7777 of the back-end allocated local dynamic syms, followed by the rest
7778 of the global symbols.
7779
7780 This is usually not needed for static binaries, however backends
7781 can request to always do it, e.g. the MIPS backend uses dynamic
7782 symbol counts to lay out GOT, which will be produced in the
7783 presence of GOT relocations even in static binaries (holding fixed
7784 data in that case, to satisfy those relocations). */
7785
7786 if (elf_hash_table (info)->dynamic_sections_created
7787 || bed->always_renumber_dynsyms)
7788 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7789 &section_sym_count);
7790
7791 if (elf_hash_table (info)->dynamic_sections_created)
7792 {
7793 bfd *dynobj;
7794 asection *s;
7795 unsigned int dtagcount;
7796
7797 dynobj = elf_hash_table (info)->dynobj;
7798
7799 /* Work out the size of the symbol version section. */
7800 s = bfd_get_linker_section (dynobj, ".gnu.version");
7801 BFD_ASSERT (s != NULL);
7802 if ((s->flags & SEC_EXCLUDE) == 0)
7803 {
7804 s->size = dynsymcount * sizeof (Elf_External_Versym);
7805 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7806 if (s->contents == NULL)
7807 return false;
7808
7809 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7810 return false;
7811 }
7812
7813 /* Set the size of the .dynsym and .hash sections. We counted
7814 the number of dynamic symbols in elf_link_add_object_symbols.
7815 We will build the contents of .dynsym and .hash when we build
7816 the final symbol table, because until then we do not know the
7817 correct value to give the symbols. We built the .dynstr
7818 section as we went along in elf_link_add_object_symbols. */
7819 s = elf_hash_table (info)->dynsym;
7820 BFD_ASSERT (s != NULL);
7821 s->size = dynsymcount * bed->s->sizeof_sym;
7822
7823 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7824 if (s->contents == NULL)
7825 return false;
7826
7827 /* The first entry in .dynsym is a dummy symbol. Clear all the
7828 section syms, in case we don't output them all. */
7829 ++section_sym_count;
7830 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7831
7832 elf_hash_table (info)->bucketcount = 0;
7833
7834 /* Compute the size of the hashing table. As a side effect this
7835 computes the hash values for all the names we export. */
7836 if (info->emit_hash)
7837 {
7838 unsigned long int *hashcodes;
7839 struct hash_codes_info hashinf;
7840 bfd_size_type amt;
7841 unsigned long int nsyms;
7842 size_t bucketcount;
7843 size_t hash_entry_size;
7844
7845 /* Compute the hash values for all exported symbols. At the same
7846 time store the values in an array so that we could use them for
7847 optimizations. */
7848 amt = dynsymcount * sizeof (unsigned long int);
7849 hashcodes = (unsigned long int *) bfd_malloc (amt);
7850 if (hashcodes == NULL)
7851 return false;
7852 hashinf.hashcodes = hashcodes;
7853 hashinf.error = false;
7854
7855 /* Put all hash values in HASHCODES. */
7856 elf_link_hash_traverse (elf_hash_table (info),
7857 elf_collect_hash_codes, &hashinf);
7858 if (hashinf.error)
7859 {
7860 free (hashcodes);
7861 return false;
7862 }
7863
7864 nsyms = hashinf.hashcodes - hashcodes;
7865 bucketcount
7866 = compute_bucket_count (info, hashcodes, nsyms, 0);
7867 free (hashcodes);
7868
7869 if (bucketcount == 0 && nsyms > 0)
7870 return false;
7871
7872 elf_hash_table (info)->bucketcount = bucketcount;
7873
7874 s = bfd_get_linker_section (dynobj, ".hash");
7875 BFD_ASSERT (s != NULL);
7876 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7877 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7878 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7879 if (s->contents == NULL)
7880 return false;
7881
7882 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7883 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7884 s->contents + hash_entry_size);
7885 }
7886
7887 if (info->emit_gnu_hash)
7888 {
7889 size_t i, cnt;
7890 unsigned char *contents;
7891 struct collect_gnu_hash_codes cinfo;
7892 bfd_size_type amt;
7893 size_t bucketcount;
7894
7895 memset (&cinfo, 0, sizeof (cinfo));
7896
7897 /* Compute the hash values for all exported symbols. At the same
7898 time store the values in an array so that we could use them for
7899 optimizations. */
7900 amt = dynsymcount * 2 * sizeof (unsigned long int);
7901 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7902 if (cinfo.hashcodes == NULL)
7903 return false;
7904
7905 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7906 cinfo.min_dynindx = -1;
7907 cinfo.output_bfd = output_bfd;
7908 cinfo.bed = bed;
7909
7910 /* Put all hash values in HASHCODES. */
7911 elf_link_hash_traverse (elf_hash_table (info),
7912 elf_collect_gnu_hash_codes, &cinfo);
7913 if (cinfo.error)
7914 {
7915 free (cinfo.hashcodes);
7916 return false;
7917 }
7918
7919 bucketcount
7920 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7921
7922 if (bucketcount == 0)
7923 {
7924 free (cinfo.hashcodes);
7925 return false;
7926 }
7927
7928 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7929 BFD_ASSERT (s != NULL);
7930
7931 if (cinfo.nsyms == 0)
7932 {
7933 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7934 BFD_ASSERT (cinfo.min_dynindx == -1);
7935 free (cinfo.hashcodes);
7936 s->size = 5 * 4 + bed->s->arch_size / 8;
7937 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7938 if (contents == NULL)
7939 return false;
7940 s->contents = contents;
7941 /* 1 empty bucket. */
7942 bfd_put_32 (output_bfd, 1, contents);
7943 /* SYMIDX above the special symbol 0. */
7944 bfd_put_32 (output_bfd, 1, contents + 4);
7945 /* Just one word for bitmask. */
7946 bfd_put_32 (output_bfd, 1, contents + 8);
7947 /* Only hash fn bloom filter. */
7948 bfd_put_32 (output_bfd, 0, contents + 12);
7949 /* No hashes are valid - empty bitmask. */
7950 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7951 /* No hashes in the only bucket. */
7952 bfd_put_32 (output_bfd, 0,
7953 contents + 16 + bed->s->arch_size / 8);
7954 }
7955 else
7956 {
7957 unsigned long int maskwords, maskbitslog2, x;
7958 BFD_ASSERT (cinfo.min_dynindx != -1);
7959
7960 x = cinfo.nsyms;
7961 maskbitslog2 = 1;
7962 while ((x >>= 1) != 0)
7963 ++maskbitslog2;
7964 if (maskbitslog2 < 3)
7965 maskbitslog2 = 5;
7966 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7967 maskbitslog2 = maskbitslog2 + 3;
7968 else
7969 maskbitslog2 = maskbitslog2 + 2;
7970 if (bed->s->arch_size == 64)
7971 {
7972 if (maskbitslog2 == 5)
7973 maskbitslog2 = 6;
7974 cinfo.shift1 = 6;
7975 }
7976 else
7977 cinfo.shift1 = 5;
7978 cinfo.mask = (1 << cinfo.shift1) - 1;
7979 cinfo.shift2 = maskbitslog2;
7980 cinfo.maskbits = 1 << maskbitslog2;
7981 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7982 amt = bucketcount * sizeof (unsigned long int) * 2;
7983 amt += maskwords * sizeof (bfd_vma);
7984 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7985 if (cinfo.bitmask == NULL)
7986 {
7987 free (cinfo.hashcodes);
7988 return false;
7989 }
7990
7991 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7992 cinfo.indx = cinfo.counts + bucketcount;
7993 cinfo.symindx = dynsymcount - cinfo.nsyms;
7994 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7995
7996 /* Determine how often each hash bucket is used. */
7997 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7998 for (i = 0; i < cinfo.nsyms; ++i)
7999 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8000
8001 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8002 if (cinfo.counts[i] != 0)
8003 {
8004 cinfo.indx[i] = cnt;
8005 cnt += cinfo.counts[i];
8006 }
8007 BFD_ASSERT (cnt == dynsymcount);
8008 cinfo.bucketcount = bucketcount;
8009 cinfo.local_indx = cinfo.min_dynindx;
8010
8011 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8012 s->size += cinfo.maskbits / 8;
8013 if (bed->record_xhash_symbol != NULL)
8014 s->size += cinfo.nsyms * 4;
8015 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
8016 if (contents == NULL)
8017 {
8018 free (cinfo.bitmask);
8019 free (cinfo.hashcodes);
8020 return false;
8021 }
8022
8023 s->contents = contents;
8024 bfd_put_32 (output_bfd, bucketcount, contents);
8025 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8026 bfd_put_32 (output_bfd, maskwords, contents + 8);
8027 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8028 contents += 16 + cinfo.maskbits / 8;
8029
8030 for (i = 0; i < bucketcount; ++i)
8031 {
8032 if (cinfo.counts[i] == 0)
8033 bfd_put_32 (output_bfd, 0, contents);
8034 else
8035 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8036 contents += 4;
8037 }
8038
8039 cinfo.contents = contents;
8040
8041 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8042 /* Renumber dynamic symbols, if populating .gnu.hash section.
8043 If using .MIPS.xhash, populate the translation table. */
8044 elf_link_hash_traverse (elf_hash_table (info),
8045 elf_gnu_hash_process_symidx, &cinfo);
8046
8047 contents = s->contents + 16;
8048 for (i = 0; i < maskwords; ++i)
8049 {
8050 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8051 contents);
8052 contents += bed->s->arch_size / 8;
8053 }
8054
8055 free (cinfo.bitmask);
8056 free (cinfo.hashcodes);
8057 }
8058 }
8059
8060 s = bfd_get_linker_section (dynobj, ".dynstr");
8061 BFD_ASSERT (s != NULL);
8062
8063 elf_finalize_dynstr (output_bfd, info);
8064
8065 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8066
8067 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8068 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
8069 return false;
8070 }
8071
8072 return true;
8073 }
8074 \f
8075 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8076
8077 static void
8078 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8079 asection *sec)
8080 {
8081 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8082 sec->sec_info_type = SEC_INFO_TYPE_NONE;
8083 }
8084
8085 /* Finish SHF_MERGE section merging. */
8086
8087 bool
8088 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
8089 {
8090 bfd *ibfd;
8091 asection *sec;
8092
8093 if (!is_elf_hash_table (info->hash))
8094 return false;
8095
8096 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8097 if ((ibfd->flags & DYNAMIC) == 0
8098 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
8099 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8100 == get_elf_backend_data (obfd)->s->elfclass))
8101 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8102 if ((sec->flags & SEC_MERGE) != 0
8103 && !bfd_is_abs_section (sec->output_section))
8104 {
8105 struct bfd_elf_section_data *secdata;
8106
8107 secdata = elf_section_data (sec);
8108 if (! _bfd_add_merge_section (obfd,
8109 &elf_hash_table (info)->merge_info,
8110 sec, &secdata->sec_info))
8111 return false;
8112 else if (secdata->sec_info)
8113 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
8114 }
8115
8116 if (elf_hash_table (info)->merge_info != NULL)
8117 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8118 merge_sections_remove_hook);
8119 return true;
8120 }
8121
8122 /* Create an entry in an ELF linker hash table. */
8123
8124 struct bfd_hash_entry *
8125 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8126 struct bfd_hash_table *table,
8127 const char *string)
8128 {
8129 /* Allocate the structure if it has not already been allocated by a
8130 subclass. */
8131 if (entry == NULL)
8132 {
8133 entry = (struct bfd_hash_entry *)
8134 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
8135 if (entry == NULL)
8136 return entry;
8137 }
8138
8139 /* Call the allocation method of the superclass. */
8140 entry = _bfd_link_hash_newfunc (entry, table, string);
8141 if (entry != NULL)
8142 {
8143 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8144 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8145
8146 /* Set local fields. */
8147 ret->indx = -1;
8148 ret->dynindx = -1;
8149 ret->got = htab->init_got_refcount;
8150 ret->plt = htab->init_plt_refcount;
8151 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8152 - offsetof (struct elf_link_hash_entry, size)));
8153 /* Assume that we have been called by a non-ELF symbol reader.
8154 This flag is then reset by the code which reads an ELF input
8155 file. This ensures that a symbol created by a non-ELF symbol
8156 reader will have the flag set correctly. */
8157 ret->non_elf = 1;
8158 }
8159
8160 return entry;
8161 }
8162
8163 /* Copy data from an indirect symbol to its direct symbol, hiding the
8164 old indirect symbol. Also used for copying flags to a weakdef. */
8165
8166 void
8167 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8168 struct elf_link_hash_entry *dir,
8169 struct elf_link_hash_entry *ind)
8170 {
8171 struct elf_link_hash_table *htab;
8172
8173 if (ind->dyn_relocs != NULL)
8174 {
8175 if (dir->dyn_relocs != NULL)
8176 {
8177 struct elf_dyn_relocs **pp;
8178 struct elf_dyn_relocs *p;
8179
8180 /* Add reloc counts against the indirect sym to the direct sym
8181 list. Merge any entries against the same section. */
8182 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8183 {
8184 struct elf_dyn_relocs *q;
8185
8186 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8187 if (q->sec == p->sec)
8188 {
8189 q->pc_count += p->pc_count;
8190 q->count += p->count;
8191 *pp = p->next;
8192 break;
8193 }
8194 if (q == NULL)
8195 pp = &p->next;
8196 }
8197 *pp = dir->dyn_relocs;
8198 }
8199
8200 dir->dyn_relocs = ind->dyn_relocs;
8201 ind->dyn_relocs = NULL;
8202 }
8203
8204 /* Copy down any references that we may have already seen to the
8205 symbol which just became indirect. */
8206
8207 if (dir->versioned != versioned_hidden)
8208 dir->ref_dynamic |= ind->ref_dynamic;
8209 dir->ref_regular |= ind->ref_regular;
8210 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8211 dir->non_got_ref |= ind->non_got_ref;
8212 dir->needs_plt |= ind->needs_plt;
8213 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8214
8215 if (ind->root.type != bfd_link_hash_indirect)
8216 return;
8217
8218 /* Copy over the global and procedure linkage table refcount entries.
8219 These may have been already set up by a check_relocs routine. */
8220 htab = elf_hash_table (info);
8221 if (ind->got.refcount > htab->init_got_refcount.refcount)
8222 {
8223 if (dir->got.refcount < 0)
8224 dir->got.refcount = 0;
8225 dir->got.refcount += ind->got.refcount;
8226 ind->got.refcount = htab->init_got_refcount.refcount;
8227 }
8228
8229 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8230 {
8231 if (dir->plt.refcount < 0)
8232 dir->plt.refcount = 0;
8233 dir->plt.refcount += ind->plt.refcount;
8234 ind->plt.refcount = htab->init_plt_refcount.refcount;
8235 }
8236
8237 if (ind->dynindx != -1)
8238 {
8239 if (dir->dynindx != -1)
8240 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8241 dir->dynindx = ind->dynindx;
8242 dir->dynstr_index = ind->dynstr_index;
8243 ind->dynindx = -1;
8244 ind->dynstr_index = 0;
8245 }
8246 }
8247
8248 void
8249 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8250 struct elf_link_hash_entry *h,
8251 bool force_local)
8252 {
8253 /* STT_GNU_IFUNC symbol must go through PLT. */
8254 if (h->type != STT_GNU_IFUNC)
8255 {
8256 h->plt = elf_hash_table (info)->init_plt_offset;
8257 h->needs_plt = 0;
8258 }
8259 if (force_local)
8260 {
8261 h->forced_local = 1;
8262 if (h->dynindx != -1)
8263 {
8264 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8265 h->dynstr_index);
8266 h->dynindx = -1;
8267 h->dynstr_index = 0;
8268 }
8269 }
8270 }
8271
8272 /* Hide a symbol. */
8273
8274 void
8275 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8276 struct bfd_link_info *info,
8277 struct bfd_link_hash_entry *h)
8278 {
8279 if (is_elf_hash_table (info->hash))
8280 {
8281 const struct elf_backend_data *bed
8282 = get_elf_backend_data (output_bfd);
8283 struct elf_link_hash_entry *eh
8284 = (struct elf_link_hash_entry *) h;
8285 bed->elf_backend_hide_symbol (info, eh, true);
8286 eh->def_dynamic = 0;
8287 eh->ref_dynamic = 0;
8288 eh->dynamic_def = 0;
8289 }
8290 }
8291
8292 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8293 caller. */
8294
8295 bool
8296 _bfd_elf_link_hash_table_init
8297 (struct elf_link_hash_table *table,
8298 bfd *abfd,
8299 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8300 struct bfd_hash_table *,
8301 const char *),
8302 unsigned int entsize,
8303 enum elf_target_id target_id)
8304 {
8305 bool ret;
8306 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8307
8308 table->init_got_refcount.refcount = can_refcount - 1;
8309 table->init_plt_refcount.refcount = can_refcount - 1;
8310 table->init_got_offset.offset = -(bfd_vma) 1;
8311 table->init_plt_offset.offset = -(bfd_vma) 1;
8312 /* The first dynamic symbol is a dummy. */
8313 table->dynsymcount = 1;
8314
8315 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8316
8317 table->root.type = bfd_link_elf_hash_table;
8318 table->hash_table_id = target_id;
8319 table->target_os = get_elf_backend_data (abfd)->target_os;
8320
8321 return ret;
8322 }
8323
8324 /* Create an ELF linker hash table. */
8325
8326 struct bfd_link_hash_table *
8327 _bfd_elf_link_hash_table_create (bfd *abfd)
8328 {
8329 struct elf_link_hash_table *ret;
8330 size_t amt = sizeof (struct elf_link_hash_table);
8331
8332 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8333 if (ret == NULL)
8334 return NULL;
8335
8336 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8337 sizeof (struct elf_link_hash_entry),
8338 GENERIC_ELF_DATA))
8339 {
8340 free (ret);
8341 return NULL;
8342 }
8343 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8344
8345 return &ret->root;
8346 }
8347
8348 /* Destroy an ELF linker hash table. */
8349
8350 void
8351 _bfd_elf_link_hash_table_free (bfd *obfd)
8352 {
8353 struct elf_link_hash_table *htab;
8354
8355 htab = (struct elf_link_hash_table *) obfd->link.hash;
8356 if (htab->dynstr != NULL)
8357 _bfd_elf_strtab_free (htab->dynstr);
8358 _bfd_merge_sections_free (htab->merge_info);
8359 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8360 if (htab->dynamic != NULL)
8361 free (htab->dynamic->contents);
8362 if (htab->first_hash != NULL)
8363 {
8364 bfd_hash_table_free (htab->first_hash);
8365 free (htab->first_hash);
8366 }
8367 _bfd_generic_link_hash_table_free (obfd);
8368 }
8369
8370 /* This is a hook for the ELF emulation code in the generic linker to
8371 tell the backend linker what file name to use for the DT_NEEDED
8372 entry for a dynamic object. */
8373
8374 void
8375 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8376 {
8377 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8378 && bfd_get_format (abfd) == bfd_object)
8379 elf_dt_name (abfd) = name;
8380 }
8381
8382 int
8383 bfd_elf_get_dyn_lib_class (bfd *abfd)
8384 {
8385 int lib_class;
8386 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8387 && bfd_get_format (abfd) == bfd_object)
8388 lib_class = elf_dyn_lib_class (abfd);
8389 else
8390 lib_class = 0;
8391 return lib_class;
8392 }
8393
8394 void
8395 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8396 {
8397 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8398 && bfd_get_format (abfd) == bfd_object)
8399 elf_dyn_lib_class (abfd) = lib_class;
8400 }
8401
8402 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8403 the linker ELF emulation code. */
8404
8405 struct bfd_link_needed_list *
8406 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8407 struct bfd_link_info *info)
8408 {
8409 if (! is_elf_hash_table (info->hash))
8410 return NULL;
8411 return elf_hash_table (info)->needed;
8412 }
8413
8414 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8415 hook for the linker ELF emulation code. */
8416
8417 struct bfd_link_needed_list *
8418 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8419 struct bfd_link_info *info)
8420 {
8421 if (! is_elf_hash_table (info->hash))
8422 return NULL;
8423 return elf_hash_table (info)->runpath;
8424 }
8425
8426 /* Get the name actually used for a dynamic object for a link. This
8427 is the SONAME entry if there is one. Otherwise, it is the string
8428 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8429
8430 const char *
8431 bfd_elf_get_dt_soname (bfd *abfd)
8432 {
8433 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8434 && bfd_get_format (abfd) == bfd_object)
8435 return elf_dt_name (abfd);
8436 return NULL;
8437 }
8438
8439 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8440 the ELF linker emulation code. */
8441
8442 bool
8443 bfd_elf_get_bfd_needed_list (bfd *abfd,
8444 struct bfd_link_needed_list **pneeded)
8445 {
8446 asection *s;
8447 bfd_byte *dynbuf = NULL;
8448 unsigned int elfsec;
8449 unsigned long shlink;
8450 bfd_byte *extdyn, *extdynend;
8451 size_t extdynsize;
8452 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8453
8454 *pneeded = NULL;
8455
8456 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8457 || bfd_get_format (abfd) != bfd_object)
8458 return true;
8459
8460 s = bfd_get_section_by_name (abfd, ".dynamic");
8461 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8462 return true;
8463
8464 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
8465 goto error_return;
8466
8467 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8468 if (elfsec == SHN_BAD)
8469 goto error_return;
8470
8471 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8472
8473 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8474 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8475
8476 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8477 (size_t) (extdynend - extdyn) >= extdynsize;
8478 extdyn += extdynsize)
8479 {
8480 Elf_Internal_Dyn dyn;
8481
8482 (*swap_dyn_in) (abfd, extdyn, &dyn);
8483
8484 if (dyn.d_tag == DT_NULL)
8485 break;
8486
8487 if (dyn.d_tag == DT_NEEDED)
8488 {
8489 const char *string;
8490 struct bfd_link_needed_list *l;
8491 unsigned int tagv = dyn.d_un.d_val;
8492 size_t amt;
8493
8494 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8495 if (string == NULL)
8496 goto error_return;
8497
8498 amt = sizeof *l;
8499 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8500 if (l == NULL)
8501 goto error_return;
8502
8503 l->by = abfd;
8504 l->name = string;
8505 l->next = *pneeded;
8506 *pneeded = l;
8507 }
8508 }
8509
8510 _bfd_elf_munmap_section_contents (s, dynbuf);
8511
8512 return true;
8513
8514 error_return:
8515 _bfd_elf_munmap_section_contents (s, dynbuf);
8516 return false;
8517 }
8518
8519 struct elf_symbuf_symbol
8520 {
8521 unsigned long st_name; /* Symbol name, index in string tbl */
8522 unsigned char st_info; /* Type and binding attributes */
8523 unsigned char st_other; /* Visibilty, and target specific */
8524 };
8525
8526 struct elf_symbuf_head
8527 {
8528 struct elf_symbuf_symbol *ssym;
8529 size_t count;
8530 unsigned int st_shndx;
8531 };
8532
8533 struct elf_symbol
8534 {
8535 union
8536 {
8537 Elf_Internal_Sym *isym;
8538 struct elf_symbuf_symbol *ssym;
8539 void *p;
8540 } u;
8541 const char *name;
8542 };
8543
8544 /* Sort references to symbols by ascending section number. */
8545
8546 static int
8547 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8548 {
8549 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8550 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8551
8552 if (s1->st_shndx != s2->st_shndx)
8553 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8554 /* Final sort by the address of the sym in the symbuf ensures
8555 a stable sort. */
8556 if (s1 != s2)
8557 return s1 > s2 ? 1 : -1;
8558 return 0;
8559 }
8560
8561 static int
8562 elf_sym_name_compare (const void *arg1, const void *arg2)
8563 {
8564 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8565 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8566 int ret = strcmp (s1->name, s2->name);
8567 if (ret != 0)
8568 return ret;
8569 if (s1->u.p != s2->u.p)
8570 return s1->u.p > s2->u.p ? 1 : -1;
8571 return 0;
8572 }
8573
8574 static struct elf_symbuf_head *
8575 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8576 {
8577 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8578 struct elf_symbuf_symbol *ssym;
8579 struct elf_symbuf_head *ssymbuf, *ssymhead;
8580 size_t i, shndx_count, total_size, amt;
8581
8582 amt = symcount * sizeof (*indbuf);
8583 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8584 if (indbuf == NULL)
8585 return NULL;
8586
8587 for (ind = indbuf, i = 0; i < symcount; i++)
8588 if (isymbuf[i].st_shndx != SHN_UNDEF)
8589 *ind++ = &isymbuf[i];
8590 indbufend = ind;
8591
8592 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8593 elf_sort_elf_symbol);
8594
8595 shndx_count = 0;
8596 if (indbufend > indbuf)
8597 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8598 if (ind[0]->st_shndx != ind[1]->st_shndx)
8599 shndx_count++;
8600
8601 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8602 + (indbufend - indbuf) * sizeof (*ssym));
8603 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8604 if (ssymbuf == NULL)
8605 {
8606 free (indbuf);
8607 return NULL;
8608 }
8609
8610 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8611 ssymbuf->ssym = NULL;
8612 ssymbuf->count = shndx_count;
8613 ssymbuf->st_shndx = 0;
8614 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8615 {
8616 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8617 {
8618 ssymhead++;
8619 ssymhead->ssym = ssym;
8620 ssymhead->count = 0;
8621 ssymhead->st_shndx = (*ind)->st_shndx;
8622 }
8623 ssym->st_name = (*ind)->st_name;
8624 ssym->st_info = (*ind)->st_info;
8625 ssym->st_other = (*ind)->st_other;
8626 ssymhead->count++;
8627 }
8628 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8629 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8630
8631 free (indbuf);
8632 return ssymbuf;
8633 }
8634
8635 /* Check if 2 sections define the same set of local and global
8636 symbols. */
8637
8638 static bool
8639 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8640 struct bfd_link_info *info)
8641 {
8642 bfd *bfd1, *bfd2;
8643 const struct elf_backend_data *bed1, *bed2;
8644 Elf_Internal_Shdr *hdr1, *hdr2;
8645 size_t symcount1, symcount2;
8646 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8647 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8648 Elf_Internal_Sym *isym, *isymend;
8649 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8650 size_t count1, count2, sec_count1, sec_count2, i;
8651 unsigned int shndx1, shndx2;
8652 bool result;
8653 bool ignore_section_symbol_p;
8654
8655 bfd1 = sec1->owner;
8656 bfd2 = sec2->owner;
8657
8658 /* Both sections have to be in ELF. */
8659 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8660 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8661 return false;
8662
8663 if (elf_section_type (sec1) != elf_section_type (sec2))
8664 return false;
8665
8666 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8667 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8668 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8669 return false;
8670
8671 bed1 = get_elf_backend_data (bfd1);
8672 bed2 = get_elf_backend_data (bfd2);
8673 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8674 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8675 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8676 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8677
8678 if (symcount1 == 0 || symcount2 == 0)
8679 return false;
8680
8681 result = false;
8682 isymbuf1 = NULL;
8683 isymbuf2 = NULL;
8684 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8685 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8686
8687 /* Ignore section symbols only when matching non-debugging sections
8688 or linkonce section with comdat section. */
8689 ignore_section_symbol_p
8690 = ((sec1->flags & SEC_DEBUGGING) == 0
8691 || ((elf_section_flags (sec1) & SHF_GROUP)
8692 != (elf_section_flags (sec2) & SHF_GROUP)));
8693
8694 if (ssymbuf1 == NULL)
8695 {
8696 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8697 NULL, NULL, NULL);
8698 if (isymbuf1 == NULL)
8699 goto done;
8700
8701 if (info != NULL && !info->reduce_memory_overheads)
8702 {
8703 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8704 elf_tdata (bfd1)->symbuf = ssymbuf1;
8705 }
8706 }
8707
8708 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8709 {
8710 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8711 NULL, NULL, NULL);
8712 if (isymbuf2 == NULL)
8713 goto done;
8714
8715 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8716 {
8717 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8718 elf_tdata (bfd2)->symbuf = ssymbuf2;
8719 }
8720 }
8721
8722 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8723 {
8724 /* Optimized faster version. */
8725 size_t lo, hi, mid;
8726 struct elf_symbol *symp;
8727 struct elf_symbuf_symbol *ssym, *ssymend;
8728
8729 lo = 0;
8730 hi = ssymbuf1->count;
8731 ssymbuf1++;
8732 count1 = 0;
8733 sec_count1 = 0;
8734 while (lo < hi)
8735 {
8736 mid = (lo + hi) / 2;
8737 if (shndx1 < ssymbuf1[mid].st_shndx)
8738 hi = mid;
8739 else if (shndx1 > ssymbuf1[mid].st_shndx)
8740 lo = mid + 1;
8741 else
8742 {
8743 count1 = ssymbuf1[mid].count;
8744 ssymbuf1 += mid;
8745 break;
8746 }
8747 }
8748 if (ignore_section_symbol_p)
8749 {
8750 for (i = 0; i < count1; i++)
8751 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8752 sec_count1++;
8753 count1 -= sec_count1;
8754 }
8755
8756 lo = 0;
8757 hi = ssymbuf2->count;
8758 ssymbuf2++;
8759 count2 = 0;
8760 sec_count2 = 0;
8761 while (lo < hi)
8762 {
8763 mid = (lo + hi) / 2;
8764 if (shndx2 < ssymbuf2[mid].st_shndx)
8765 hi = mid;
8766 else if (shndx2 > ssymbuf2[mid].st_shndx)
8767 lo = mid + 1;
8768 else
8769 {
8770 count2 = ssymbuf2[mid].count;
8771 ssymbuf2 += mid;
8772 break;
8773 }
8774 }
8775 if (ignore_section_symbol_p)
8776 {
8777 for (i = 0; i < count2; i++)
8778 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8779 sec_count2++;
8780 count2 -= sec_count2;
8781 }
8782
8783 if (count1 == 0 || count2 == 0 || count1 != count2)
8784 goto done;
8785
8786 symtable1
8787 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8788 symtable2
8789 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8790 if (symtable1 == NULL || symtable2 == NULL)
8791 goto done;
8792
8793 symp = symtable1;
8794 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8795 ssym < ssymend; ssym++)
8796 if (sec_count1 == 0
8797 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8798 {
8799 symp->u.ssym = ssym;
8800 symp->name = bfd_elf_string_from_elf_section (bfd1,
8801 hdr1->sh_link,
8802 ssym->st_name);
8803 symp++;
8804 }
8805
8806 symp = symtable2;
8807 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8808 ssym < ssymend; ssym++)
8809 if (sec_count2 == 0
8810 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8811 {
8812 symp->u.ssym = ssym;
8813 symp->name = bfd_elf_string_from_elf_section (bfd2,
8814 hdr2->sh_link,
8815 ssym->st_name);
8816 symp++;
8817 }
8818
8819 /* Sort symbol by name. */
8820 qsort (symtable1, count1, sizeof (struct elf_symbol),
8821 elf_sym_name_compare);
8822 qsort (symtable2, count1, sizeof (struct elf_symbol),
8823 elf_sym_name_compare);
8824
8825 for (i = 0; i < count1; i++)
8826 /* Two symbols must have the same binding, type and name. */
8827 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8828 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8829 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8830 goto done;
8831
8832 result = true;
8833 goto done;
8834 }
8835
8836 symtable1 = (struct elf_symbol *)
8837 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8838 symtable2 = (struct elf_symbol *)
8839 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8840 if (symtable1 == NULL || symtable2 == NULL)
8841 goto done;
8842
8843 /* Count definitions in the section. */
8844 count1 = 0;
8845 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8846 if (isym->st_shndx == shndx1
8847 && (!ignore_section_symbol_p
8848 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8849 symtable1[count1++].u.isym = isym;
8850
8851 count2 = 0;
8852 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8853 if (isym->st_shndx == shndx2
8854 && (!ignore_section_symbol_p
8855 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8856 symtable2[count2++].u.isym = isym;
8857
8858 if (count1 == 0 || count2 == 0 || count1 != count2)
8859 goto done;
8860
8861 for (i = 0; i < count1; i++)
8862 symtable1[i].name
8863 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8864 symtable1[i].u.isym->st_name);
8865
8866 for (i = 0; i < count2; i++)
8867 symtable2[i].name
8868 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8869 symtable2[i].u.isym->st_name);
8870
8871 /* Sort symbol by name. */
8872 qsort (symtable1, count1, sizeof (struct elf_symbol),
8873 elf_sym_name_compare);
8874 qsort (symtable2, count1, sizeof (struct elf_symbol),
8875 elf_sym_name_compare);
8876
8877 for (i = 0; i < count1; i++)
8878 /* Two symbols must have the same binding, type and name. */
8879 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8880 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8881 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8882 goto done;
8883
8884 result = true;
8885
8886 done:
8887 free (symtable1);
8888 free (symtable2);
8889 free (isymbuf1);
8890 free (isymbuf2);
8891
8892 return result;
8893 }
8894
8895 /* Return TRUE if 2 section types are compatible. */
8896
8897 bool
8898 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8899 bfd *bbfd, const asection *bsec)
8900 {
8901 if (asec == NULL
8902 || bsec == NULL
8903 || abfd->xvec->flavour != bfd_target_elf_flavour
8904 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8905 return true;
8906
8907 return elf_section_type (asec) == elf_section_type (bsec);
8908 }
8909 \f
8910 /* Final phase of ELF linker. */
8911
8912 /* A structure we use to avoid passing large numbers of arguments. */
8913
8914 struct elf_final_link_info
8915 {
8916 /* General link information. */
8917 struct bfd_link_info *info;
8918 /* Output BFD. */
8919 bfd *output_bfd;
8920 /* Symbol string table. */
8921 struct elf_strtab_hash *symstrtab;
8922 /* .hash section. */
8923 asection *hash_sec;
8924 /* symbol version section (.gnu.version). */
8925 asection *symver_sec;
8926 /* Buffer large enough to hold contents of any section. */
8927 bfd_byte *contents;
8928 /* Buffer large enough to hold external relocs of any section. */
8929 void *external_relocs;
8930 /* Buffer large enough to hold internal relocs of any section. */
8931 Elf_Internal_Rela *internal_relocs;
8932 /* Buffer large enough to hold external local symbols of any input
8933 BFD. */
8934 bfd_byte *external_syms;
8935 /* And a buffer for symbol section indices. */
8936 Elf_External_Sym_Shndx *locsym_shndx;
8937 /* Buffer large enough to hold internal local symbols of any input
8938 BFD. */
8939 Elf_Internal_Sym *internal_syms;
8940 /* Array large enough to hold a symbol index for each local symbol
8941 of any input BFD. */
8942 long *indices;
8943 /* Array large enough to hold a section pointer for each local
8944 symbol of any input BFD. */
8945 asection **sections;
8946 /* Buffer for SHT_SYMTAB_SHNDX section. */
8947 Elf_External_Sym_Shndx *symshndxbuf;
8948 /* Number of STT_FILE syms seen. */
8949 size_t filesym_count;
8950 /* Local symbol hash table. */
8951 struct bfd_hash_table local_hash_table;
8952 };
8953
8954 struct local_hash_entry
8955 {
8956 /* Base hash table entry structure. */
8957 struct bfd_hash_entry root;
8958 /* Size of the local symbol name. */
8959 size_t size;
8960 /* Number of the duplicated local symbol names. */
8961 long count;
8962 };
8963
8964 /* Create an entry in the local symbol hash table. */
8965
8966 static struct bfd_hash_entry *
8967 local_hash_newfunc (struct bfd_hash_entry *entry,
8968 struct bfd_hash_table *table,
8969 const char *string)
8970 {
8971
8972 /* Allocate the structure if it has not already been allocated by a
8973 subclass. */
8974 if (entry == NULL)
8975 {
8976 entry = bfd_hash_allocate (table,
8977 sizeof (struct local_hash_entry));
8978 if (entry == NULL)
8979 return entry;
8980 }
8981
8982 /* Call the allocation method of the superclass. */
8983 entry = bfd_hash_newfunc (entry, table, string);
8984 if (entry != NULL)
8985 {
8986 ((struct local_hash_entry *) entry)->count = 0;
8987 ((struct local_hash_entry *) entry)->size = 0;
8988 }
8989
8990 return entry;
8991 }
8992
8993 /* This struct is used to pass information to elf_link_output_extsym. */
8994
8995 struct elf_outext_info
8996 {
8997 bool failed;
8998 bool localsyms;
8999 bool file_sym_done;
9000 struct elf_final_link_info *flinfo;
9001 };
9002
9003
9004 /* Support for evaluating a complex relocation.
9005
9006 Complex relocations are generalized, self-describing relocations. The
9007 implementation of them consists of two parts: complex symbols, and the
9008 relocations themselves.
9009
9010 The relocations use a reserved elf-wide relocation type code (R_RELC
9011 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9012 information (start bit, end bit, word width, etc) into the addend. This
9013 information is extracted from CGEN-generated operand tables within gas.
9014
9015 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9016 internal) representing prefix-notation expressions, including but not
9017 limited to those sorts of expressions normally encoded as addends in the
9018 addend field. The symbol mangling format is:
9019
9020 <node> := <literal>
9021 | <unary-operator> ':' <node>
9022 | <binary-operator> ':' <node> ':' <node>
9023 ;
9024
9025 <literal> := 's' <digits=N> ':' <N character symbol name>
9026 | 'S' <digits=N> ':' <N character section name>
9027 | '#' <hexdigits>
9028 ;
9029
9030 <binary-operator> := as in C
9031 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9032
9033 static void
9034 set_symbol_value (bfd *bfd_with_globals,
9035 Elf_Internal_Sym *isymbuf,
9036 size_t locsymcount,
9037 size_t symidx,
9038 bfd_vma val)
9039 {
9040 struct elf_link_hash_entry **sym_hashes;
9041 struct elf_link_hash_entry *h;
9042 size_t extsymoff = locsymcount;
9043
9044 if (symidx < locsymcount)
9045 {
9046 Elf_Internal_Sym *sym;
9047
9048 sym = isymbuf + symidx;
9049 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9050 {
9051 /* It is a local symbol: move it to the
9052 "absolute" section and give it a value. */
9053 sym->st_shndx = SHN_ABS;
9054 sym->st_value = val;
9055 return;
9056 }
9057 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9058 extsymoff = 0;
9059 }
9060
9061 /* It is a global symbol: set its link type
9062 to "defined" and give it a value. */
9063
9064 sym_hashes = elf_sym_hashes (bfd_with_globals);
9065 h = sym_hashes [symidx - extsymoff];
9066 while (h->root.type == bfd_link_hash_indirect
9067 || h->root.type == bfd_link_hash_warning)
9068 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9069 h->root.type = bfd_link_hash_defined;
9070 h->root.u.def.value = val;
9071 h->root.u.def.section = bfd_abs_section_ptr;
9072 }
9073
9074 static bool
9075 resolve_symbol (const char *name,
9076 bfd *input_bfd,
9077 struct elf_final_link_info *flinfo,
9078 bfd_vma *result,
9079 Elf_Internal_Sym *isymbuf,
9080 size_t locsymcount)
9081 {
9082 Elf_Internal_Sym *sym;
9083 struct bfd_link_hash_entry *global_entry;
9084 const char *candidate = NULL;
9085 Elf_Internal_Shdr *symtab_hdr;
9086 size_t i;
9087
9088 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9089
9090 for (i = 0; i < locsymcount; ++ i)
9091 {
9092 sym = isymbuf + i;
9093
9094 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9095 continue;
9096
9097 candidate = bfd_elf_string_from_elf_section (input_bfd,
9098 symtab_hdr->sh_link,
9099 sym->st_name);
9100 #ifdef DEBUG
9101 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9102 name, candidate, (unsigned long) sym->st_value);
9103 #endif
9104 if (candidate && strcmp (candidate, name) == 0)
9105 {
9106 asection *sec = flinfo->sections [i];
9107
9108 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9109 *result += sec->output_offset + sec->output_section->vma;
9110 #ifdef DEBUG
9111 printf ("Found symbol with value %8.8lx\n",
9112 (unsigned long) *result);
9113 #endif
9114 return true;
9115 }
9116 }
9117
9118 /* Hmm, haven't found it yet. perhaps it is a global. */
9119 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
9120 false, false, true);
9121 if (!global_entry)
9122 return false;
9123
9124 if (global_entry->type == bfd_link_hash_defined
9125 || global_entry->type == bfd_link_hash_defweak)
9126 {
9127 *result = (global_entry->u.def.value
9128 + global_entry->u.def.section->output_section->vma
9129 + global_entry->u.def.section->output_offset);
9130 #ifdef DEBUG
9131 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9132 global_entry->root.string, (unsigned long) *result);
9133 #endif
9134 return true;
9135 }
9136
9137 return false;
9138 }
9139
9140 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9141 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9142 names like "foo.end" which is the end address of section "foo". */
9143
9144 static bool
9145 resolve_section (const char *name,
9146 asection *sections,
9147 bfd_vma *result,
9148 bfd * abfd)
9149 {
9150 asection *curr;
9151 unsigned int len;
9152
9153 for (curr = sections; curr; curr = curr->next)
9154 if (strcmp (curr->name, name) == 0)
9155 {
9156 *result = curr->vma;
9157 return true;
9158 }
9159
9160 /* Hmm. still haven't found it. try pseudo-section names. */
9161 /* FIXME: This could be coded more efficiently... */
9162 for (curr = sections; curr; curr = curr->next)
9163 {
9164 len = strlen (curr->name);
9165 if (len > strlen (name))
9166 continue;
9167
9168 if (strncmp (curr->name, name, len) == 0)
9169 {
9170 if (startswith (name + len, ".end"))
9171 {
9172 *result = (curr->vma
9173 + curr->size / bfd_octets_per_byte (abfd, curr));
9174 return true;
9175 }
9176
9177 /* Insert more pseudo-section names here, if you like. */
9178 }
9179 }
9180
9181 return false;
9182 }
9183
9184 static void
9185 undefined_reference (const char *reftype, const char *name)
9186 {
9187 /* xgettext:c-format */
9188 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9189 reftype, name);
9190 bfd_set_error (bfd_error_bad_value);
9191 }
9192
9193 static bool
9194 eval_symbol (bfd_vma *result,
9195 const char **symp,
9196 bfd *input_bfd,
9197 struct elf_final_link_info *flinfo,
9198 bfd_vma dot,
9199 Elf_Internal_Sym *isymbuf,
9200 size_t locsymcount,
9201 int signed_p)
9202 {
9203 size_t len;
9204 size_t symlen;
9205 bfd_vma a;
9206 bfd_vma b;
9207 char symbuf[4096];
9208 const char *sym = *symp;
9209 const char *symend;
9210 bool symbol_is_section = false;
9211
9212 len = strlen (sym);
9213 symend = sym + len;
9214
9215 if (len < 1 || len > sizeof (symbuf))
9216 {
9217 bfd_set_error (bfd_error_invalid_operation);
9218 return false;
9219 }
9220
9221 switch (* sym)
9222 {
9223 case '.':
9224 *result = dot;
9225 *symp = sym + 1;
9226 return true;
9227
9228 case '#':
9229 ++sym;
9230 *result = strtoul (sym, (char **) symp, 16);
9231 return true;
9232
9233 case 'S':
9234 symbol_is_section = true;
9235 /* Fall through. */
9236 case 's':
9237 ++sym;
9238 symlen = strtol (sym, (char **) symp, 10);
9239 sym = *symp + 1; /* Skip the trailing ':'. */
9240
9241 if (symend < sym || symlen + 1 > sizeof (symbuf))
9242 {
9243 bfd_set_error (bfd_error_invalid_operation);
9244 return false;
9245 }
9246
9247 memcpy (symbuf, sym, symlen);
9248 symbuf[symlen] = '\0';
9249 *symp = sym + symlen;
9250
9251 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9252 the symbol as a section, or vice-versa. so we're pretty liberal in our
9253 interpretation here; section means "try section first", not "must be a
9254 section", and likewise with symbol. */
9255
9256 if (symbol_is_section)
9257 {
9258 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9259 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9260 isymbuf, locsymcount))
9261 {
9262 undefined_reference ("section", symbuf);
9263 return false;
9264 }
9265 }
9266 else
9267 {
9268 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9269 isymbuf, locsymcount)
9270 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9271 result, input_bfd))
9272 {
9273 undefined_reference ("symbol", symbuf);
9274 return false;
9275 }
9276 }
9277
9278 return true;
9279
9280 /* All that remains are operators. */
9281
9282 #define UNARY_OP(op) \
9283 if (startswith (sym, #op)) \
9284 { \
9285 sym += strlen (#op); \
9286 if (*sym == ':') \
9287 ++sym; \
9288 *symp = sym; \
9289 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9290 isymbuf, locsymcount, signed_p)) \
9291 return false; \
9292 if (signed_p) \
9293 *result = op ((bfd_signed_vma) a); \
9294 else \
9295 *result = op a; \
9296 return true; \
9297 }
9298
9299 #define BINARY_OP_HEAD(op) \
9300 if (startswith (sym, #op)) \
9301 { \
9302 sym += strlen (#op); \
9303 if (*sym == ':') \
9304 ++sym; \
9305 *symp = sym; \
9306 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9307 isymbuf, locsymcount, signed_p)) \
9308 return false; \
9309 ++*symp; \
9310 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9311 isymbuf, locsymcount, signed_p)) \
9312 return false;
9313 #define BINARY_OP_TAIL(op) \
9314 if (signed_p) \
9315 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9316 else \
9317 *result = a op b; \
9318 return true; \
9319 }
9320 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9321
9322 default:
9323 UNARY_OP (0-);
9324 BINARY_OP_HEAD (<<);
9325 if (b >= sizeof (a) * CHAR_BIT)
9326 {
9327 *result = 0;
9328 return true;
9329 }
9330 signed_p = 0;
9331 BINARY_OP_TAIL (<<);
9332 BINARY_OP_HEAD (>>);
9333 if (b >= sizeof (a) * CHAR_BIT)
9334 {
9335 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9336 return true;
9337 }
9338 BINARY_OP_TAIL (>>);
9339 BINARY_OP (==);
9340 BINARY_OP (!=);
9341 BINARY_OP (<=);
9342 BINARY_OP (>=);
9343 BINARY_OP (&&);
9344 BINARY_OP (||);
9345 UNARY_OP (~);
9346 UNARY_OP (!);
9347 BINARY_OP (*);
9348 BINARY_OP_HEAD (/);
9349 if (b == 0)
9350 {
9351 _bfd_error_handler (_("division by zero"));
9352 bfd_set_error (bfd_error_bad_value);
9353 return false;
9354 }
9355 BINARY_OP_TAIL (/);
9356 BINARY_OP_HEAD (%);
9357 if (b == 0)
9358 {
9359 _bfd_error_handler (_("division by zero"));
9360 bfd_set_error (bfd_error_bad_value);
9361 return false;
9362 }
9363 BINARY_OP_TAIL (%);
9364 BINARY_OP (^);
9365 BINARY_OP (|);
9366 BINARY_OP (&);
9367 BINARY_OP (+);
9368 BINARY_OP (-);
9369 BINARY_OP (<);
9370 BINARY_OP (>);
9371 #undef UNARY_OP
9372 #undef BINARY_OP
9373 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9374 bfd_set_error (bfd_error_invalid_operation);
9375 return false;
9376 }
9377 }
9378
9379 static void
9380 put_value (bfd_vma size,
9381 unsigned long chunksz,
9382 bfd *input_bfd,
9383 bfd_vma x,
9384 bfd_byte *location)
9385 {
9386 location += (size - chunksz);
9387
9388 for (; size; size -= chunksz, location -= chunksz)
9389 {
9390 switch (chunksz)
9391 {
9392 case 1:
9393 bfd_put_8 (input_bfd, x, location);
9394 x >>= 8;
9395 break;
9396 case 2:
9397 bfd_put_16 (input_bfd, x, location);
9398 x >>= 16;
9399 break;
9400 case 4:
9401 bfd_put_32 (input_bfd, x, location);
9402 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9403 x >>= 16;
9404 x >>= 16;
9405 break;
9406 #ifdef BFD64
9407 case 8:
9408 bfd_put_64 (input_bfd, x, location);
9409 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9410 x >>= 32;
9411 x >>= 32;
9412 break;
9413 #endif
9414 default:
9415 abort ();
9416 break;
9417 }
9418 }
9419 }
9420
9421 static bfd_vma
9422 get_value (bfd_vma size,
9423 unsigned long chunksz,
9424 bfd *input_bfd,
9425 bfd_byte *location)
9426 {
9427 int shift;
9428 bfd_vma x = 0;
9429
9430 /* Sanity checks. */
9431 BFD_ASSERT (chunksz <= sizeof (x)
9432 && size >= chunksz
9433 && chunksz != 0
9434 && (size % chunksz) == 0
9435 && input_bfd != NULL
9436 && location != NULL);
9437
9438 if (chunksz == sizeof (x))
9439 {
9440 BFD_ASSERT (size == chunksz);
9441
9442 /* Make sure that we do not perform an undefined shift operation.
9443 We know that size == chunksz so there will only be one iteration
9444 of the loop below. */
9445 shift = 0;
9446 }
9447 else
9448 shift = 8 * chunksz;
9449
9450 for (; size; size -= chunksz, location += chunksz)
9451 {
9452 switch (chunksz)
9453 {
9454 case 1:
9455 x = (x << shift) | bfd_get_8 (input_bfd, location);
9456 break;
9457 case 2:
9458 x = (x << shift) | bfd_get_16 (input_bfd, location);
9459 break;
9460 case 4:
9461 x = (x << shift) | bfd_get_32 (input_bfd, location);
9462 break;
9463 #ifdef BFD64
9464 case 8:
9465 x = (x << shift) | bfd_get_64 (input_bfd, location);
9466 break;
9467 #endif
9468 default:
9469 abort ();
9470 }
9471 }
9472 return x;
9473 }
9474
9475 static void
9476 decode_complex_addend (unsigned long *start, /* in bits */
9477 unsigned long *oplen, /* in bits */
9478 unsigned long *len, /* in bits */
9479 unsigned long *wordsz, /* in bytes */
9480 unsigned long *chunksz, /* in bytes */
9481 unsigned long *lsb0_p,
9482 unsigned long *signed_p,
9483 unsigned long *trunc_p,
9484 unsigned long encoded)
9485 {
9486 * start = encoded & 0x3F;
9487 * len = (encoded >> 6) & 0x3F;
9488 * oplen = (encoded >> 12) & 0x3F;
9489 * wordsz = (encoded >> 18) & 0xF;
9490 * chunksz = (encoded >> 22) & 0xF;
9491 * lsb0_p = (encoded >> 27) & 1;
9492 * signed_p = (encoded >> 28) & 1;
9493 * trunc_p = (encoded >> 29) & 1;
9494 }
9495
9496 bfd_reloc_status_type
9497 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9498 asection *input_section,
9499 bfd_byte *contents,
9500 Elf_Internal_Rela *rel,
9501 bfd_vma relocation)
9502 {
9503 bfd_vma shift, x, mask;
9504 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9505 bfd_reloc_status_type r;
9506 bfd_size_type octets;
9507
9508 /* Perform this reloc, since it is complex.
9509 (this is not to say that it necessarily refers to a complex
9510 symbol; merely that it is a self-describing CGEN based reloc.
9511 i.e. the addend has the complete reloc information (bit start, end,
9512 word size, etc) encoded within it.). */
9513
9514 decode_complex_addend (&start, &oplen, &len, &wordsz,
9515 &chunksz, &lsb0_p, &signed_p,
9516 &trunc_p, rel->r_addend);
9517
9518 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9519
9520 if (lsb0_p)
9521 shift = (start + 1) - len;
9522 else
9523 shift = (8 * wordsz) - (start + len);
9524
9525 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9526 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9527
9528 #ifdef DEBUG
9529 printf ("Doing complex reloc: "
9530 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9531 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9532 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9533 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9534 oplen, (unsigned long) x, (unsigned long) mask,
9535 (unsigned long) relocation);
9536 #endif
9537
9538 r = bfd_reloc_ok;
9539 if (! trunc_p)
9540 /* Now do an overflow check. */
9541 r = bfd_check_overflow ((signed_p
9542 ? complain_overflow_signed
9543 : complain_overflow_unsigned),
9544 len, 0, (8 * wordsz),
9545 relocation);
9546
9547 /* Do the deed. */
9548 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9549
9550 #ifdef DEBUG
9551 printf (" relocation: %8.8lx\n"
9552 " shifted mask: %8.8lx\n"
9553 " shifted/masked reloc: %8.8lx\n"
9554 " result: %8.8lx\n",
9555 (unsigned long) relocation, (unsigned long) (mask << shift),
9556 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9557 #endif
9558 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9559 return r;
9560 }
9561
9562 /* Functions to read r_offset from external (target order) reloc
9563 entry. Faster than bfd_getl32 et al, because we let the compiler
9564 know the value is aligned. */
9565
9566 static bfd_vma
9567 ext32l_r_offset (const void *p)
9568 {
9569 union aligned32
9570 {
9571 uint32_t v;
9572 unsigned char c[4];
9573 };
9574 const union aligned32 *a
9575 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9576
9577 uint32_t aval = ( (uint32_t) a->c[0]
9578 | (uint32_t) a->c[1] << 8
9579 | (uint32_t) a->c[2] << 16
9580 | (uint32_t) a->c[3] << 24);
9581 return aval;
9582 }
9583
9584 static bfd_vma
9585 ext32b_r_offset (const void *p)
9586 {
9587 union aligned32
9588 {
9589 uint32_t v;
9590 unsigned char c[4];
9591 };
9592 const union aligned32 *a
9593 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9594
9595 uint32_t aval = ( (uint32_t) a->c[0] << 24
9596 | (uint32_t) a->c[1] << 16
9597 | (uint32_t) a->c[2] << 8
9598 | (uint32_t) a->c[3]);
9599 return aval;
9600 }
9601
9602 static bfd_vma
9603 ext64l_r_offset (const void *p)
9604 {
9605 union aligned64
9606 {
9607 uint64_t v;
9608 unsigned char c[8];
9609 };
9610 const union aligned64 *a
9611 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9612
9613 uint64_t aval = ( (uint64_t) a->c[0]
9614 | (uint64_t) a->c[1] << 8
9615 | (uint64_t) a->c[2] << 16
9616 | (uint64_t) a->c[3] << 24
9617 | (uint64_t) a->c[4] << 32
9618 | (uint64_t) a->c[5] << 40
9619 | (uint64_t) a->c[6] << 48
9620 | (uint64_t) a->c[7] << 56);
9621 return aval;
9622 }
9623
9624 static bfd_vma
9625 ext64b_r_offset (const void *p)
9626 {
9627 union aligned64
9628 {
9629 uint64_t v;
9630 unsigned char c[8];
9631 };
9632 const union aligned64 *a
9633 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9634
9635 uint64_t aval = ( (uint64_t) a->c[0] << 56
9636 | (uint64_t) a->c[1] << 48
9637 | (uint64_t) a->c[2] << 40
9638 | (uint64_t) a->c[3] << 32
9639 | (uint64_t) a->c[4] << 24
9640 | (uint64_t) a->c[5] << 16
9641 | (uint64_t) a->c[6] << 8
9642 | (uint64_t) a->c[7]);
9643 return aval;
9644 }
9645
9646 /* When performing a relocatable link, the input relocations are
9647 preserved. But, if they reference global symbols, the indices
9648 referenced must be updated. Update all the relocations found in
9649 RELDATA. */
9650
9651 static bool
9652 elf_link_adjust_relocs (bfd *abfd,
9653 asection *sec,
9654 struct bfd_elf_section_reloc_data *reldata,
9655 bool sort,
9656 struct bfd_link_info *info)
9657 {
9658 unsigned int i;
9659 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9660 bfd_byte *erela;
9661 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9662 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9663 bfd_vma r_type_mask;
9664 int r_sym_shift;
9665 unsigned int count = reldata->count;
9666 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9667
9668 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9669 {
9670 swap_in = bed->s->swap_reloc_in;
9671 swap_out = bed->s->swap_reloc_out;
9672 }
9673 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9674 {
9675 swap_in = bed->s->swap_reloca_in;
9676 swap_out = bed->s->swap_reloca_out;
9677 }
9678 else
9679 abort ();
9680
9681 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9682 abort ();
9683
9684 if (bed->s->arch_size == 32)
9685 {
9686 r_type_mask = 0xff;
9687 r_sym_shift = 8;
9688 }
9689 else
9690 {
9691 r_type_mask = 0xffffffff;
9692 r_sym_shift = 32;
9693 }
9694
9695 erela = reldata->hdr->contents;
9696 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9697 {
9698 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9699 unsigned int j;
9700
9701 if (*rel_hash == NULL)
9702 continue;
9703
9704 if ((*rel_hash)->indx == -2
9705 && info->gc_sections
9706 && ! info->gc_keep_exported)
9707 {
9708 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9709 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9710 abfd, sec,
9711 (*rel_hash)->root.root.string);
9712 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9713 abfd, sec);
9714 bfd_set_error (bfd_error_invalid_operation);
9715 return false;
9716 }
9717 BFD_ASSERT ((*rel_hash)->indx >= 0);
9718
9719 (*swap_in) (abfd, erela, irela);
9720 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9721 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9722 | (irela[j].r_info & r_type_mask));
9723 (*swap_out) (abfd, irela, erela);
9724 }
9725
9726 if (bed->elf_backend_update_relocs)
9727 (*bed->elf_backend_update_relocs) (sec, reldata);
9728
9729 if (sort && count != 0)
9730 {
9731 bfd_vma (*ext_r_off) (const void *);
9732 bfd_vma r_off;
9733 size_t elt_size;
9734 bfd_byte *base, *end, *p, *loc;
9735 bfd_byte *buf = NULL;
9736
9737 if (bed->s->arch_size == 32)
9738 {
9739 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9740 ext_r_off = ext32l_r_offset;
9741 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9742 ext_r_off = ext32b_r_offset;
9743 else
9744 abort ();
9745 }
9746 else
9747 {
9748 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9749 ext_r_off = ext64l_r_offset;
9750 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9751 ext_r_off = ext64b_r_offset;
9752 else
9753 abort ();
9754 }
9755
9756 /* Must use a stable sort here. A modified insertion sort,
9757 since the relocs are mostly sorted already. */
9758 elt_size = reldata->hdr->sh_entsize;
9759 base = reldata->hdr->contents;
9760 end = base + count * elt_size;
9761 if (elt_size > sizeof (Elf64_External_Rela))
9762 abort ();
9763
9764 /* Ensure the first element is lowest. This acts as a sentinel,
9765 speeding the main loop below. */
9766 r_off = (*ext_r_off) (base);
9767 for (p = loc = base; (p += elt_size) < end; )
9768 {
9769 bfd_vma r_off2 = (*ext_r_off) (p);
9770 if (r_off > r_off2)
9771 {
9772 r_off = r_off2;
9773 loc = p;
9774 }
9775 }
9776 if (loc != base)
9777 {
9778 /* Don't just swap *base and *loc as that changes the order
9779 of the original base[0] and base[1] if they happen to
9780 have the same r_offset. */
9781 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9782 memcpy (onebuf, loc, elt_size);
9783 memmove (base + elt_size, base, loc - base);
9784 memcpy (base, onebuf, elt_size);
9785 }
9786
9787 for (p = base + elt_size; (p += elt_size) < end; )
9788 {
9789 /* base to p is sorted, *p is next to insert. */
9790 r_off = (*ext_r_off) (p);
9791 /* Search the sorted region for location to insert. */
9792 loc = p - elt_size;
9793 while (r_off < (*ext_r_off) (loc))
9794 loc -= elt_size;
9795 loc += elt_size;
9796 if (loc != p)
9797 {
9798 /* Chances are there is a run of relocs to insert here,
9799 from one of more input files. Files are not always
9800 linked in order due to the way elf_link_input_bfd is
9801 called. See pr17666. */
9802 size_t sortlen = p - loc;
9803 bfd_vma r_off2 = (*ext_r_off) (loc);
9804 size_t runlen = elt_size;
9805 bfd_vma r_off_runend = r_off;
9806 bfd_vma r_off_runend_next;
9807 size_t buf_size = 96 * 1024;
9808 while (p + runlen < end
9809 && (sortlen <= buf_size
9810 || runlen + elt_size <= buf_size)
9811 /* run must not break the ordering of base..loc+1 */
9812 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9813 /* run must be already sorted */
9814 && r_off_runend_next >= r_off_runend)
9815 {
9816 runlen += elt_size;
9817 r_off_runend = r_off_runend_next;
9818 }
9819 if (buf == NULL)
9820 {
9821 buf = bfd_malloc (buf_size);
9822 if (buf == NULL)
9823 return false;
9824 }
9825 if (runlen < sortlen)
9826 {
9827 memcpy (buf, p, runlen);
9828 memmove (loc + runlen, loc, sortlen);
9829 memcpy (loc, buf, runlen);
9830 }
9831 else
9832 {
9833 memcpy (buf, loc, sortlen);
9834 memmove (loc, p, runlen);
9835 memcpy (loc + runlen, buf, sortlen);
9836 }
9837 p += runlen - elt_size;
9838 }
9839 }
9840 /* Hashes are no longer valid. */
9841 free (reldata->hashes);
9842 reldata->hashes = NULL;
9843 free (buf);
9844 }
9845 return true;
9846 }
9847
9848 struct elf_link_sort_rela
9849 {
9850 union {
9851 bfd_vma offset;
9852 bfd_vma sym_mask;
9853 } u;
9854 enum elf_reloc_type_class type;
9855 /* We use this as an array of size int_rels_per_ext_rel. */
9856 Elf_Internal_Rela rela[1];
9857 };
9858
9859 /* qsort stability here and for cmp2 is only an issue if multiple
9860 dynamic relocations are emitted at the same address. But targets
9861 that apply a series of dynamic relocations each operating on the
9862 result of the prior relocation can't use -z combreloc as
9863 implemented anyway. Such schemes tend to be broken by sorting on
9864 symbol index. That leaves dynamic NONE relocs as the only other
9865 case where ld might emit multiple relocs at the same address, and
9866 those are only emitted due to target bugs. */
9867
9868 static int
9869 elf_link_sort_cmp1 (const void *A, const void *B)
9870 {
9871 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9872 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9873 int relativea, relativeb;
9874
9875 relativea = a->type == reloc_class_relative;
9876 relativeb = b->type == reloc_class_relative;
9877
9878 if (relativea < relativeb)
9879 return 1;
9880 if (relativea > relativeb)
9881 return -1;
9882 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9883 return -1;
9884 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9885 return 1;
9886 if (a->rela->r_offset < b->rela->r_offset)
9887 return -1;
9888 if (a->rela->r_offset > b->rela->r_offset)
9889 return 1;
9890 return 0;
9891 }
9892
9893 static int
9894 elf_link_sort_cmp2 (const void *A, const void *B)
9895 {
9896 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9897 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9898
9899 if (a->type < b->type)
9900 return -1;
9901 if (a->type > b->type)
9902 return 1;
9903 if (a->u.offset < b->u.offset)
9904 return -1;
9905 if (a->u.offset > b->u.offset)
9906 return 1;
9907 if (a->rela->r_offset < b->rela->r_offset)
9908 return -1;
9909 if (a->rela->r_offset > b->rela->r_offset)
9910 return 1;
9911 return 0;
9912 }
9913
9914 static size_t
9915 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9916 {
9917 asection *dynamic_relocs;
9918 asection *rela_dyn;
9919 asection *rel_dyn;
9920 bfd_size_type count, size;
9921 size_t i, ret, sort_elt, ext_size;
9922 bfd_byte *sort, *s_non_relative, *p;
9923 struct elf_link_sort_rela *sq;
9924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9925 int i2e = bed->s->int_rels_per_ext_rel;
9926 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9927 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9928 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9929 struct bfd_link_order *lo;
9930 bfd_vma r_sym_mask;
9931 bool use_rela;
9932
9933 /* Find a dynamic reloc section. */
9934 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9935 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9936 if (rela_dyn != NULL && rela_dyn->size > 0
9937 && rel_dyn != NULL && rel_dyn->size > 0)
9938 {
9939 bool use_rela_initialised = false;
9940
9941 /* This is just here to stop gcc from complaining.
9942 Its initialization checking code is not perfect. */
9943 use_rela = true;
9944
9945 /* Both sections are present. Examine the sizes
9946 of the indirect sections to help us choose. */
9947 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9948 if (lo->type == bfd_indirect_link_order)
9949 {
9950 asection *o = lo->u.indirect.section;
9951
9952 if ((o->size % bed->s->sizeof_rela) == 0)
9953 {
9954 if ((o->size % bed->s->sizeof_rel) == 0)
9955 /* Section size is divisible by both rel and rela sizes.
9956 It is of no help to us. */
9957 ;
9958 else
9959 {
9960 /* Section size is only divisible by rela. */
9961 if (use_rela_initialised && !use_rela)
9962 {
9963 _bfd_error_handler (_("%pB: unable to sort relocs - "
9964 "they are in more than one size"),
9965 abfd);
9966 bfd_set_error (bfd_error_invalid_operation);
9967 return 0;
9968 }
9969 else
9970 {
9971 use_rela = true;
9972 use_rela_initialised = true;
9973 }
9974 }
9975 }
9976 else if ((o->size % bed->s->sizeof_rel) == 0)
9977 {
9978 /* Section size is only divisible by rel. */
9979 if (use_rela_initialised && use_rela)
9980 {
9981 _bfd_error_handler (_("%pB: unable to sort relocs - "
9982 "they are in more than one size"),
9983 abfd);
9984 bfd_set_error (bfd_error_invalid_operation);
9985 return 0;
9986 }
9987 else
9988 {
9989 use_rela = false;
9990 use_rela_initialised = true;
9991 }
9992 }
9993 else
9994 {
9995 /* The section size is not divisible by either -
9996 something is wrong. */
9997 _bfd_error_handler (_("%pB: unable to sort relocs - "
9998 "they are of an unknown size"), abfd);
9999 bfd_set_error (bfd_error_invalid_operation);
10000 return 0;
10001 }
10002 }
10003
10004 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10005 if (lo->type == bfd_indirect_link_order)
10006 {
10007 asection *o = lo->u.indirect.section;
10008
10009 if ((o->size % bed->s->sizeof_rela) == 0)
10010 {
10011 if ((o->size % bed->s->sizeof_rel) == 0)
10012 /* Section size is divisible by both rel and rela sizes.
10013 It is of no help to us. */
10014 ;
10015 else
10016 {
10017 /* Section size is only divisible by rela. */
10018 if (use_rela_initialised && !use_rela)
10019 {
10020 _bfd_error_handler (_("%pB: unable to sort relocs - "
10021 "they are in more than one size"),
10022 abfd);
10023 bfd_set_error (bfd_error_invalid_operation);
10024 return 0;
10025 }
10026 else
10027 {
10028 use_rela = true;
10029 use_rela_initialised = true;
10030 }
10031 }
10032 }
10033 else if ((o->size % bed->s->sizeof_rel) == 0)
10034 {
10035 /* Section size is only divisible by rel. */
10036 if (use_rela_initialised && use_rela)
10037 {
10038 _bfd_error_handler (_("%pB: unable to sort relocs - "
10039 "they are in more than one size"),
10040 abfd);
10041 bfd_set_error (bfd_error_invalid_operation);
10042 return 0;
10043 }
10044 else
10045 {
10046 use_rela = false;
10047 use_rela_initialised = true;
10048 }
10049 }
10050 else
10051 {
10052 /* The section size is not divisible by either -
10053 something is wrong. */
10054 _bfd_error_handler (_("%pB: unable to sort relocs - "
10055 "they are of an unknown size"), abfd);
10056 bfd_set_error (bfd_error_invalid_operation);
10057 return 0;
10058 }
10059 }
10060
10061 if (! use_rela_initialised)
10062 /* Make a guess. */
10063 use_rela = true;
10064 }
10065 else if (rela_dyn != NULL && rela_dyn->size > 0)
10066 use_rela = true;
10067 else if (rel_dyn != NULL && rel_dyn->size > 0)
10068 use_rela = false;
10069 else
10070 return 0;
10071
10072 if (use_rela)
10073 {
10074 dynamic_relocs = rela_dyn;
10075 ext_size = bed->s->sizeof_rela;
10076 swap_in = bed->s->swap_reloca_in;
10077 swap_out = bed->s->swap_reloca_out;
10078 }
10079 else
10080 {
10081 dynamic_relocs = rel_dyn;
10082 ext_size = bed->s->sizeof_rel;
10083 swap_in = bed->s->swap_reloc_in;
10084 swap_out = bed->s->swap_reloc_out;
10085 }
10086
10087 size = 0;
10088 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10089 if (lo->type == bfd_indirect_link_order)
10090 size += lo->u.indirect.section->size;
10091
10092 if (size != dynamic_relocs->size)
10093 return 0;
10094
10095 sort_elt = (sizeof (struct elf_link_sort_rela)
10096 + (i2e - 1) * sizeof (Elf_Internal_Rela));
10097
10098 count = dynamic_relocs->size / ext_size;
10099 if (count == 0)
10100 return 0;
10101 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
10102
10103 if (sort == NULL)
10104 {
10105 (*info->callbacks->warning)
10106 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
10107 return 0;
10108 }
10109
10110 if (bed->s->arch_size == 32)
10111 r_sym_mask = ~(bfd_vma) 0xff;
10112 else
10113 r_sym_mask = ~(bfd_vma) 0xffffffff;
10114
10115 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10116 if (lo->type == bfd_indirect_link_order)
10117 {
10118 bfd_byte *erel, *erelend;
10119 asection *o = lo->u.indirect.section;
10120
10121 if (o->contents == NULL && o->size != 0)
10122 {
10123 /* This is a reloc section that is being handled as a normal
10124 section. See bfd_section_from_shdr. We can't combine
10125 relocs in this case. */
10126 free (sort);
10127 return 0;
10128 }
10129 erel = o->contents;
10130 erelend = o->contents + o->size;
10131 p = sort + o->output_offset * opb / ext_size * sort_elt;
10132
10133 while (erel < erelend)
10134 {
10135 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10136
10137 (*swap_in) (abfd, erel, s->rela);
10138 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
10139 s->u.sym_mask = r_sym_mask;
10140 p += sort_elt;
10141 erel += ext_size;
10142 }
10143 }
10144
10145 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10146
10147 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10148 {
10149 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10150 if (s->type != reloc_class_relative)
10151 break;
10152 }
10153 ret = i;
10154 s_non_relative = p;
10155
10156 sq = (struct elf_link_sort_rela *) s_non_relative;
10157 for (; i < count; i++, p += sort_elt)
10158 {
10159 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10160 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10161 sq = sp;
10162 sp->u.offset = sq->rela->r_offset;
10163 }
10164
10165 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10166
10167 struct elf_link_hash_table *htab = elf_hash_table (info);
10168 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10169 {
10170 /* We have plt relocs in .rela.dyn. */
10171 sq = (struct elf_link_sort_rela *) sort;
10172 for (i = 0; i < count; i++)
10173 if (sq[count - i - 1].type != reloc_class_plt)
10174 break;
10175 if (i != 0 && htab->srelplt->size == i * ext_size)
10176 {
10177 struct bfd_link_order **plo;
10178 /* Put srelplt link_order last. This is so the output_offset
10179 set in the next loop is correct for DT_JMPREL. */
10180 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10181 if ((*plo)->type == bfd_indirect_link_order
10182 && (*plo)->u.indirect.section == htab->srelplt)
10183 {
10184 lo = *plo;
10185 *plo = lo->next;
10186 }
10187 else
10188 plo = &(*plo)->next;
10189 *plo = lo;
10190 lo->next = NULL;
10191 dynamic_relocs->map_tail.link_order = lo;
10192 }
10193 }
10194
10195 p = sort;
10196 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10197 if (lo->type == bfd_indirect_link_order)
10198 {
10199 bfd_byte *erel, *erelend;
10200 asection *o = lo->u.indirect.section;
10201
10202 erel = o->contents;
10203 erelend = o->contents + o->size;
10204 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10205 while (erel < erelend)
10206 {
10207 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10208 (*swap_out) (abfd, s->rela, erel);
10209 p += sort_elt;
10210 erel += ext_size;
10211 }
10212 }
10213
10214 free (sort);
10215 *psec = dynamic_relocs;
10216 return ret;
10217 }
10218
10219 /* Add a symbol to the output symbol string table. */
10220
10221 static int
10222 elf_link_output_symstrtab (void *finf,
10223 const char *name,
10224 Elf_Internal_Sym *elfsym,
10225 asection *input_sec,
10226 struct elf_link_hash_entry *h)
10227 {
10228 struct elf_final_link_info *flinfo = finf;
10229 int (*output_symbol_hook)
10230 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10231 struct elf_link_hash_entry *);
10232 struct elf_link_hash_table *hash_table;
10233 const struct elf_backend_data *bed;
10234 bfd_size_type strtabsize;
10235
10236 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10237
10238 bed = get_elf_backend_data (flinfo->output_bfd);
10239 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10240 if (output_symbol_hook != NULL)
10241 {
10242 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10243 if (ret != 1)
10244 return ret;
10245 }
10246
10247 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10248 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10249 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10250 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10251
10252 if (name == NULL || *name == '\0')
10253 elfsym->st_name = (unsigned long) -1;
10254 else
10255 {
10256 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10257 to get the final offset for st_name. */
10258 char *versioned_name = (char *) name;
10259 if (h != NULL)
10260 {
10261 if (h->versioned == versioned && h->def_dynamic)
10262 {
10263 /* Keep only one '@' for versioned symbols defined in
10264 shared objects. */
10265 char *version = strrchr (name, ELF_VER_CHR);
10266 char *base_end = strchr (name, ELF_VER_CHR);
10267 if (version != base_end)
10268 {
10269 size_t base_len;
10270 size_t len = strlen (name);
10271 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10272 if (versioned_name == NULL)
10273 return 0;
10274 base_len = base_end - name;
10275 memcpy (versioned_name, name, base_len);
10276 memcpy (versioned_name + base_len, version,
10277 len - base_len);
10278 }
10279 }
10280 }
10281 else if (flinfo->info->unique_symbol
10282 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10283 {
10284 struct local_hash_entry *lh;
10285 size_t count_len;
10286 size_t base_len;
10287 char buf[30];
10288 switch (ELF_ST_TYPE (elfsym->st_info))
10289 {
10290 case STT_FILE:
10291 case STT_SECTION:
10292 break;
10293 default:
10294 lh = (struct local_hash_entry *) bfd_hash_lookup
10295 (&flinfo->local_hash_table, name, true, false);
10296 if (lh == NULL)
10297 return 0;
10298 /* Always append ".COUNT" to local symbols to avoid
10299 potential conflicts with local symbol "XXX.COUNT". */
10300 sprintf (buf, "%lx", lh->count);
10301 base_len = lh->size;
10302 if (!base_len)
10303 {
10304 base_len = strlen (name);
10305 lh->size = base_len;
10306 }
10307 count_len = strlen (buf);
10308 versioned_name = bfd_alloc (flinfo->output_bfd,
10309 base_len + count_len + 2);
10310 if (versioned_name == NULL)
10311 return 0;
10312 memcpy (versioned_name, name, base_len);
10313 versioned_name[base_len] = '.';
10314 memcpy (versioned_name + base_len + 1, buf,
10315 count_len + 1);
10316 lh->count++;
10317 break;
10318 }
10319 }
10320 elfsym->st_name
10321 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10322 versioned_name, false);
10323 if (elfsym->st_name == (unsigned long) -1)
10324 return 0;
10325 }
10326
10327 hash_table = elf_hash_table (flinfo->info);
10328 strtabsize = hash_table->strtabsize;
10329 if (strtabsize <= flinfo->output_bfd->symcount)
10330 {
10331 strtabsize += strtabsize;
10332 hash_table->strtabsize = strtabsize;
10333 strtabsize *= sizeof (*hash_table->strtab);
10334 hash_table->strtab
10335 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10336 strtabsize);
10337 if (hash_table->strtab == NULL)
10338 return 0;
10339 }
10340 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10341 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10342 = flinfo->output_bfd->symcount;
10343 flinfo->output_bfd->symcount += 1;
10344
10345 return 1;
10346 }
10347
10348 /* Swap symbols out to the symbol table and flush the output symbols to
10349 the file. */
10350
10351 static bool
10352 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10353 {
10354 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10355 size_t amt;
10356 size_t i;
10357 const struct elf_backend_data *bed;
10358 bfd_byte *symbuf;
10359 Elf_Internal_Shdr *hdr;
10360 file_ptr pos;
10361 bool ret;
10362
10363 if (flinfo->output_bfd->symcount == 0)
10364 return true;
10365
10366 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10367
10368 bed = get_elf_backend_data (flinfo->output_bfd);
10369
10370 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10371 symbuf = (bfd_byte *) bfd_malloc (amt);
10372 if (symbuf == NULL)
10373 return false;
10374
10375 if (flinfo->symshndxbuf)
10376 {
10377 amt = sizeof (Elf_External_Sym_Shndx);
10378 amt *= bfd_get_symcount (flinfo->output_bfd);
10379 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10380 if (flinfo->symshndxbuf == NULL)
10381 {
10382 free (symbuf);
10383 return false;
10384 }
10385 }
10386
10387 /* Now swap out the symbols. */
10388 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10389 {
10390 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10391 if (elfsym->sym.st_name == (unsigned long) -1)
10392 elfsym->sym.st_name = 0;
10393 else
10394 elfsym->sym.st_name
10395 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10396 elfsym->sym.st_name);
10397
10398 /* Inform the linker of the addition of this symbol. */
10399
10400 if (flinfo->info->callbacks->ctf_new_symbol)
10401 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10402 &elfsym->sym);
10403
10404 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10405 ((bfd_byte *) symbuf
10406 + (elfsym->dest_index
10407 * bed->s->sizeof_sym)),
10408 NPTR_ADD (flinfo->symshndxbuf,
10409 elfsym->dest_index));
10410 }
10411
10412 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10413 pos = hdr->sh_offset + hdr->sh_size;
10414 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10415 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10416 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10417 {
10418 hdr->sh_size += amt;
10419 ret = true;
10420 }
10421 else
10422 ret = false;
10423
10424 free (symbuf);
10425
10426 free (hash_table->strtab);
10427 hash_table->strtab = NULL;
10428
10429 return ret;
10430 }
10431
10432 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10433
10434 static bool
10435 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10436 {
10437 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10438 && sym->st_shndx < SHN_LORESERVE)
10439 {
10440 /* The gABI doesn't support dynamic symbols in output sections
10441 beyond 64k. */
10442 _bfd_error_handler
10443 /* xgettext:c-format */
10444 (_("%pB: too many sections: %d (>= %d)"),
10445 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10446 bfd_set_error (bfd_error_nonrepresentable_section);
10447 return false;
10448 }
10449 return true;
10450 }
10451
10452 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10453 allowing an unsatisfied unversioned symbol in the DSO to match a
10454 versioned symbol that would normally require an explicit version.
10455 We also handle the case that a DSO references a hidden symbol
10456 which may be satisfied by a versioned symbol in another DSO. */
10457
10458 static bool
10459 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10460 const struct elf_backend_data *bed,
10461 struct elf_link_hash_entry *h)
10462 {
10463 bfd *abfd;
10464 struct elf_link_loaded_list *loaded;
10465
10466 if (!is_elf_hash_table (info->hash))
10467 return false;
10468
10469 /* Check indirect symbol. */
10470 while (h->root.type == bfd_link_hash_indirect)
10471 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10472
10473 switch (h->root.type)
10474 {
10475 default:
10476 abfd = NULL;
10477 break;
10478
10479 case bfd_link_hash_undefined:
10480 case bfd_link_hash_undefweak:
10481 abfd = h->root.u.undef.abfd;
10482 if (abfd == NULL
10483 || (abfd->flags & DYNAMIC) == 0
10484 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10485 return false;
10486 break;
10487
10488 case bfd_link_hash_defined:
10489 case bfd_link_hash_defweak:
10490 abfd = h->root.u.def.section->owner;
10491 break;
10492
10493 case bfd_link_hash_common:
10494 abfd = h->root.u.c.p->section->owner;
10495 break;
10496 }
10497 BFD_ASSERT (abfd != NULL);
10498
10499 for (loaded = elf_hash_table (info)->dyn_loaded;
10500 loaded != NULL;
10501 loaded = loaded->next)
10502 {
10503 bfd *input;
10504 Elf_Internal_Shdr *hdr;
10505 size_t symcount;
10506 size_t extsymcount;
10507 size_t extsymoff;
10508 Elf_Internal_Shdr *versymhdr;
10509 Elf_Internal_Sym *isym;
10510 Elf_Internal_Sym *isymend;
10511 Elf_Internal_Sym *isymbuf;
10512 Elf_External_Versym *ever;
10513 Elf_External_Versym *extversym;
10514
10515 input = loaded->abfd;
10516
10517 /* We check each DSO for a possible hidden versioned definition. */
10518 if (input == abfd
10519 || elf_dynversym (input) == 0)
10520 continue;
10521
10522 hdr = &elf_tdata (input)->dynsymtab_hdr;
10523
10524 symcount = hdr->sh_size / bed->s->sizeof_sym;
10525 if (elf_bad_symtab (input))
10526 {
10527 extsymcount = symcount;
10528 extsymoff = 0;
10529 }
10530 else
10531 {
10532 extsymcount = symcount - hdr->sh_info;
10533 extsymoff = hdr->sh_info;
10534 }
10535
10536 if (extsymcount == 0)
10537 continue;
10538
10539 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10540 NULL, NULL, NULL);
10541 if (isymbuf == NULL)
10542 return false;
10543
10544 /* Read in any version definitions. */
10545 versymhdr = &elf_tdata (input)->dynversym_hdr;
10546 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10547 || (extversym = (Elf_External_Versym *)
10548 _bfd_malloc_and_read (input, versymhdr->sh_size,
10549 versymhdr->sh_size)) == NULL)
10550 {
10551 free (isymbuf);
10552 return false;
10553 }
10554
10555 ever = extversym + extsymoff;
10556 isymend = isymbuf + extsymcount;
10557 for (isym = isymbuf; isym < isymend; isym++, ever++)
10558 {
10559 const char *name;
10560 Elf_Internal_Versym iver;
10561 unsigned short version_index;
10562
10563 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10564 || isym->st_shndx == SHN_UNDEF)
10565 continue;
10566
10567 name = bfd_elf_string_from_elf_section (input,
10568 hdr->sh_link,
10569 isym->st_name);
10570 if (strcmp (name, h->root.root.string) != 0)
10571 continue;
10572
10573 _bfd_elf_swap_versym_in (input, ever, &iver);
10574
10575 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10576 && !(h->def_regular
10577 && h->forced_local))
10578 {
10579 /* If we have a non-hidden versioned sym, then it should
10580 have provided a definition for the undefined sym unless
10581 it is defined in a non-shared object and forced local.
10582 */
10583 abort ();
10584 }
10585
10586 version_index = iver.vs_vers & VERSYM_VERSION;
10587 if (version_index == 1 || version_index == 2)
10588 {
10589 /* This is the base or first version. We can use it. */
10590 free (extversym);
10591 free (isymbuf);
10592 return true;
10593 }
10594 }
10595
10596 free (extversym);
10597 free (isymbuf);
10598 }
10599
10600 return false;
10601 }
10602
10603 /* Convert ELF common symbol TYPE. */
10604
10605 static int
10606 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10607 {
10608 /* Commom symbol can only appear in relocatable link. */
10609 if (!bfd_link_relocatable (info))
10610 abort ();
10611 switch (info->elf_stt_common)
10612 {
10613 case unchanged:
10614 break;
10615 case elf_stt_common:
10616 type = STT_COMMON;
10617 break;
10618 case no_elf_stt_common:
10619 type = STT_OBJECT;
10620 break;
10621 }
10622 return type;
10623 }
10624
10625 /* Add an external symbol to the symbol table. This is called from
10626 the hash table traversal routine. When generating a shared object,
10627 we go through the symbol table twice. The first time we output
10628 anything that might have been forced to local scope in a version
10629 script. The second time we output the symbols that are still
10630 global symbols. */
10631
10632 static bool
10633 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10634 {
10635 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10636 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10637 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10638 bool strip;
10639 Elf_Internal_Sym sym;
10640 asection *input_sec;
10641 const struct elf_backend_data *bed;
10642 long indx;
10643 int ret;
10644 unsigned int type;
10645
10646 if (h->root.type == bfd_link_hash_warning)
10647 {
10648 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10649 if (h->root.type == bfd_link_hash_new)
10650 return true;
10651 }
10652
10653 /* Decide whether to output this symbol in this pass. */
10654 if (eoinfo->localsyms)
10655 {
10656 if (!h->forced_local)
10657 return true;
10658 }
10659 else
10660 {
10661 if (h->forced_local)
10662 return true;
10663 }
10664
10665 bed = get_elf_backend_data (flinfo->output_bfd);
10666
10667 if (h->root.type == bfd_link_hash_undefined)
10668 {
10669 /* If we have an undefined symbol reference here then it must have
10670 come from a shared library that is being linked in. (Undefined
10671 references in regular files have already been handled unless
10672 they are in unreferenced sections which are removed by garbage
10673 collection). */
10674 bool ignore_undef = false;
10675
10676 /* Some symbols may be special in that the fact that they're
10677 undefined can be safely ignored - let backend determine that. */
10678 if (bed->elf_backend_ignore_undef_symbol)
10679 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10680
10681 /* If we are reporting errors for this situation then do so now. */
10682 if (!ignore_undef
10683 && h->ref_dynamic_nonweak
10684 && (!h->ref_regular || flinfo->info->gc_sections)
10685 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10686 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10687 {
10688 flinfo->info->callbacks->undefined_symbol
10689 (flinfo->info, h->root.root.string,
10690 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10691 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10692 && !flinfo->info->warn_unresolved_syms);
10693 }
10694
10695 /* Strip a global symbol defined in a discarded section. */
10696 if (h->indx == -3)
10697 return true;
10698 }
10699
10700 /* We should also warn if a forced local symbol is referenced from
10701 shared libraries. */
10702 if (bfd_link_executable (flinfo->info)
10703 && h->forced_local
10704 && h->ref_dynamic
10705 && h->def_regular
10706 && !h->dynamic_def
10707 && h->ref_dynamic_nonweak
10708 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10709 {
10710 bfd *def_bfd;
10711 const char *msg;
10712 struct elf_link_hash_entry *hi = h;
10713
10714 /* Check indirect symbol. */
10715 while (hi->root.type == bfd_link_hash_indirect)
10716 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10717
10718 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10719 /* xgettext:c-format */
10720 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10721 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10722 /* xgettext:c-format */
10723 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10724 else
10725 /* xgettext:c-format */
10726 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10727 def_bfd = flinfo->output_bfd;
10728 if (hi->root.u.def.section != bfd_abs_section_ptr)
10729 def_bfd = hi->root.u.def.section->owner;
10730 _bfd_error_handler (msg, flinfo->output_bfd,
10731 h->root.root.string, def_bfd);
10732 bfd_set_error (bfd_error_bad_value);
10733 eoinfo->failed = true;
10734 return false;
10735 }
10736
10737 /* We don't want to output symbols that have never been mentioned by
10738 a regular file, or that we have been told to strip. However, if
10739 h->indx is set to -2, the symbol is used by a reloc and we must
10740 output it. */
10741 strip = false;
10742 if (h->indx == -2)
10743 ;
10744 else if ((h->def_dynamic
10745 || h->ref_dynamic
10746 || h->root.type == bfd_link_hash_new)
10747 && !h->def_regular
10748 && !h->ref_regular)
10749 strip = true;
10750 else if (flinfo->info->strip == strip_all)
10751 strip = true;
10752 else if (flinfo->info->strip == strip_some
10753 && bfd_hash_lookup (flinfo->info->keep_hash,
10754 h->root.root.string, false, false) == NULL)
10755 strip = true;
10756 else if ((h->root.type == bfd_link_hash_defined
10757 || h->root.type == bfd_link_hash_defweak)
10758 && ((flinfo->info->strip_discarded
10759 && discarded_section (h->root.u.def.section))
10760 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10761 && h->root.u.def.section->owner != NULL
10762 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10763 strip = true;
10764 else if ((h->root.type == bfd_link_hash_undefined
10765 || h->root.type == bfd_link_hash_undefweak)
10766 && h->root.u.undef.abfd != NULL
10767 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10768 strip = true;
10769
10770 type = h->type;
10771
10772 /* If we're stripping it, and it's not a dynamic symbol, there's
10773 nothing else to do. However, if it is a forced local symbol or
10774 an ifunc symbol we need to give the backend finish_dynamic_symbol
10775 function a chance to make it dynamic. */
10776 if (strip
10777 && h->dynindx == -1
10778 && type != STT_GNU_IFUNC
10779 && !h->forced_local)
10780 return true;
10781
10782 sym.st_value = 0;
10783 sym.st_size = h->size;
10784 sym.st_other = h->other;
10785 switch (h->root.type)
10786 {
10787 default:
10788 case bfd_link_hash_new:
10789 case bfd_link_hash_warning:
10790 abort ();
10791 return false;
10792
10793 case bfd_link_hash_undefined:
10794 case bfd_link_hash_undefweak:
10795 input_sec = bfd_und_section_ptr;
10796 sym.st_shndx = SHN_UNDEF;
10797 break;
10798
10799 case bfd_link_hash_defined:
10800 case bfd_link_hash_defweak:
10801 {
10802 input_sec = h->root.u.def.section;
10803 if (input_sec->output_section != NULL)
10804 {
10805 sym.st_shndx =
10806 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10807 input_sec->output_section);
10808 if (sym.st_shndx == SHN_BAD)
10809 {
10810 _bfd_error_handler
10811 /* xgettext:c-format */
10812 (_("%pB: could not find output section %pA for input section %pA"),
10813 flinfo->output_bfd, input_sec->output_section, input_sec);
10814 bfd_set_error (bfd_error_nonrepresentable_section);
10815 eoinfo->failed = true;
10816 return false;
10817 }
10818
10819 /* ELF symbols in relocatable files are section relative,
10820 but in nonrelocatable files they are virtual
10821 addresses. */
10822 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10823 if (!bfd_link_relocatable (flinfo->info))
10824 {
10825 sym.st_value += input_sec->output_section->vma;
10826 if (h->type == STT_TLS)
10827 {
10828 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10829 if (tls_sec != NULL)
10830 sym.st_value -= tls_sec->vma;
10831 }
10832 }
10833 }
10834 else
10835 {
10836 BFD_ASSERT (input_sec->owner == NULL
10837 || (input_sec->owner->flags & DYNAMIC) != 0);
10838 sym.st_shndx = SHN_UNDEF;
10839 input_sec = bfd_und_section_ptr;
10840 }
10841 }
10842 break;
10843
10844 case bfd_link_hash_common:
10845 input_sec = h->root.u.c.p->section;
10846 sym.st_shndx = bed->common_section_index (input_sec);
10847 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10848 break;
10849
10850 case bfd_link_hash_indirect:
10851 /* These symbols are created by symbol versioning. They point
10852 to the decorated version of the name. For example, if the
10853 symbol foo@@GNU_1.2 is the default, which should be used when
10854 foo is used with no version, then we add an indirect symbol
10855 foo which points to foo@@GNU_1.2. We ignore these symbols,
10856 since the indirected symbol is already in the hash table. */
10857 return true;
10858 }
10859
10860 if (type == STT_COMMON || type == STT_OBJECT)
10861 switch (h->root.type)
10862 {
10863 case bfd_link_hash_common:
10864 type = elf_link_convert_common_type (flinfo->info, type);
10865 break;
10866 case bfd_link_hash_defined:
10867 case bfd_link_hash_defweak:
10868 if (bed->common_definition (&sym))
10869 type = elf_link_convert_common_type (flinfo->info, type);
10870 else
10871 type = STT_OBJECT;
10872 break;
10873 case bfd_link_hash_undefined:
10874 case bfd_link_hash_undefweak:
10875 break;
10876 default:
10877 abort ();
10878 }
10879
10880 if (h->forced_local)
10881 {
10882 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10883 /* Turn off visibility on local symbol. */
10884 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10885 }
10886 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10887 else if (h->unique_global && h->def_regular)
10888 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10889 else if (h->root.type == bfd_link_hash_undefweak
10890 || h->root.type == bfd_link_hash_defweak)
10891 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10892 else
10893 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10894 sym.st_target_internal = h->target_internal;
10895
10896 /* Give the processor backend a chance to tweak the symbol value,
10897 and also to finish up anything that needs to be done for this
10898 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10899 forced local syms when non-shared is due to a historical quirk.
10900 STT_GNU_IFUNC symbol must go through PLT. */
10901 if ((h->type == STT_GNU_IFUNC
10902 && h->def_regular
10903 && !bfd_link_relocatable (flinfo->info))
10904 || ((h->dynindx != -1
10905 || h->forced_local)
10906 && ((bfd_link_pic (flinfo->info)
10907 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10908 || h->root.type != bfd_link_hash_undefweak))
10909 || !h->forced_local)
10910 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10911 {
10912 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10913 (flinfo->output_bfd, flinfo->info, h, &sym)))
10914 {
10915 eoinfo->failed = true;
10916 return false;
10917 }
10918 }
10919
10920 /* If we are marking the symbol as undefined, and there are no
10921 non-weak references to this symbol from a regular object, then
10922 mark the symbol as weak undefined; if there are non-weak
10923 references, mark the symbol as strong. We can't do this earlier,
10924 because it might not be marked as undefined until the
10925 finish_dynamic_symbol routine gets through with it. */
10926 if (sym.st_shndx == SHN_UNDEF
10927 && h->ref_regular
10928 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10929 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10930 {
10931 int bindtype;
10932 type = ELF_ST_TYPE (sym.st_info);
10933
10934 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10935 if (type == STT_GNU_IFUNC)
10936 type = STT_FUNC;
10937
10938 if (h->ref_regular_nonweak)
10939 bindtype = STB_GLOBAL;
10940 else
10941 bindtype = STB_WEAK;
10942 sym.st_info = ELF_ST_INFO (bindtype, type);
10943 }
10944
10945 /* If this is a symbol defined in a dynamic library, don't use the
10946 symbol size from the dynamic library. Relinking an executable
10947 against a new library may introduce gratuitous changes in the
10948 executable's symbols if we keep the size. */
10949 if (sym.st_shndx == SHN_UNDEF
10950 && !h->def_regular
10951 && h->def_dynamic)
10952 sym.st_size = 0;
10953
10954 /* If a non-weak symbol with non-default visibility is not defined
10955 locally, it is a fatal error. */
10956 if (!bfd_link_relocatable (flinfo->info)
10957 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10958 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10959 && h->root.type == bfd_link_hash_undefined
10960 && !h->def_regular)
10961 {
10962 const char *msg;
10963
10964 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10965 /* xgettext:c-format */
10966 msg = _("%pB: protected symbol `%s' isn't defined");
10967 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10968 /* xgettext:c-format */
10969 msg = _("%pB: internal symbol `%s' isn't defined");
10970 else
10971 /* xgettext:c-format */
10972 msg = _("%pB: hidden symbol `%s' isn't defined");
10973 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10974 bfd_set_error (bfd_error_bad_value);
10975 eoinfo->failed = true;
10976 return false;
10977 }
10978
10979 /* If this symbol should be put in the .dynsym section, then put it
10980 there now. We already know the symbol index. We also fill in
10981 the entry in the .hash section. */
10982 if (h->dynindx != -1
10983 && elf_hash_table (flinfo->info)->dynamic_sections_created
10984 && elf_hash_table (flinfo->info)->dynsym != NULL
10985 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10986 {
10987 bfd_byte *esym;
10988
10989 /* Since there is no version information in the dynamic string,
10990 if there is no version info in symbol version section, we will
10991 have a run-time problem if not linking executable, referenced
10992 by shared library, or not bound locally. */
10993 if (h->verinfo.verdef == NULL
10994 && (!bfd_link_executable (flinfo->info)
10995 || h->ref_dynamic
10996 || !h->def_regular))
10997 {
10998 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10999
11000 if (p && p [1] != '\0')
11001 {
11002 _bfd_error_handler
11003 /* xgettext:c-format */
11004 (_("%pB: no symbol version section for versioned symbol `%s'"),
11005 flinfo->output_bfd, h->root.root.string);
11006 eoinfo->failed = true;
11007 return false;
11008 }
11009 }
11010
11011 sym.st_name = h->dynstr_index;
11012 esym = (elf_hash_table (flinfo->info)->dynsym->contents
11013 + h->dynindx * bed->s->sizeof_sym);
11014 if (!check_dynsym (flinfo->output_bfd, &sym))
11015 {
11016 eoinfo->failed = true;
11017 return false;
11018 }
11019
11020 /* Inform the linker of the addition of this symbol. */
11021
11022 if (flinfo->info->callbacks->ctf_new_dynsym)
11023 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11024
11025 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
11026
11027 if (flinfo->hash_sec != NULL)
11028 {
11029 size_t hash_entry_size;
11030 bfd_byte *bucketpos;
11031 bfd_vma chain;
11032 size_t bucketcount;
11033 size_t bucket;
11034
11035 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
11036 bucket = h->u.elf_hash_value % bucketcount;
11037
11038 hash_entry_size
11039 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11040 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
11041 + (bucket + 2) * hash_entry_size);
11042 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11043 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11044 bucketpos);
11045 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11046 ((bfd_byte *) flinfo->hash_sec->contents
11047 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11048 }
11049
11050 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
11051 {
11052 Elf_Internal_Versym iversym;
11053 Elf_External_Versym *eversym;
11054
11055 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
11056 {
11057 if (h->verinfo.verdef == NULL
11058 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11059 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
11060 iversym.vs_vers = 1;
11061 else
11062 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11063 }
11064 else
11065 {
11066 if (h->verinfo.vertree == NULL)
11067 iversym.vs_vers = 1;
11068 else
11069 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
11070 if (flinfo->info->create_default_symver)
11071 iversym.vs_vers++;
11072 }
11073
11074 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11075 defined locally. */
11076 if (h->versioned == versioned_hidden && h->def_regular)
11077 iversym.vs_vers |= VERSYM_HIDDEN;
11078
11079 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
11080 eversym += h->dynindx;
11081 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
11082 }
11083 }
11084
11085 /* If the symbol is undefined, and we didn't output it to .dynsym,
11086 strip it from .symtab too. Obviously we can't do this for
11087 relocatable output or when needed for --emit-relocs. */
11088 else if (input_sec == bfd_und_section_ptr
11089 && h->indx != -2
11090 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11091 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
11092 && !bfd_link_relocatable (flinfo->info))
11093 return true;
11094
11095 /* Also strip others that we couldn't earlier due to dynamic symbol
11096 processing. */
11097 if (strip)
11098 return true;
11099 if ((input_sec->flags & SEC_EXCLUDE) != 0)
11100 return true;
11101
11102 /* Output a FILE symbol so that following locals are not associated
11103 with the wrong input file. We need one for forced local symbols
11104 if we've seen more than one FILE symbol or when we have exactly
11105 one FILE symbol but global symbols are present in a file other
11106 than the one with the FILE symbol. We also need one if linker
11107 defined symbols are present. In practice these conditions are
11108 always met, so just emit the FILE symbol unconditionally. */
11109 if (eoinfo->localsyms
11110 && !eoinfo->file_sym_done
11111 && eoinfo->flinfo->filesym_count != 0)
11112 {
11113 Elf_Internal_Sym fsym;
11114
11115 memset (&fsym, 0, sizeof (fsym));
11116 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11117 fsym.st_shndx = SHN_ABS;
11118 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11119 bfd_und_section_ptr, NULL))
11120 return false;
11121
11122 eoinfo->file_sym_done = true;
11123 }
11124
11125 indx = bfd_get_symcount (flinfo->output_bfd);
11126 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11127 input_sec, h);
11128 if (ret == 0)
11129 {
11130 eoinfo->failed = true;
11131 return false;
11132 }
11133 else if (ret == 1)
11134 h->indx = indx;
11135 else if (h->indx == -2)
11136 abort();
11137
11138 return true;
11139 }
11140
11141 /* Return TRUE if special handling is done for relocs in SEC against
11142 symbols defined in discarded sections. */
11143
11144 static bool
11145 elf_section_ignore_discarded_relocs (asection *sec)
11146 {
11147 const struct elf_backend_data *bed;
11148
11149 switch (sec->sec_info_type)
11150 {
11151 case SEC_INFO_TYPE_STABS:
11152 case SEC_INFO_TYPE_EH_FRAME:
11153 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11154 case SEC_INFO_TYPE_SFRAME:
11155 return true;
11156 default:
11157 break;
11158 }
11159
11160 bed = get_elf_backend_data (sec->owner);
11161 if (bed->elf_backend_ignore_discarded_relocs != NULL
11162 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
11163 return true;
11164
11165 return false;
11166 }
11167
11168 /* Return a mask saying how ld should treat relocations in SEC against
11169 symbols defined in discarded sections. If this function returns
11170 COMPLAIN set, ld will issue a warning message. If this function
11171 returns PRETEND set, and the discarded section was link-once and the
11172 same size as the kept link-once section, ld will pretend that the
11173 symbol was actually defined in the kept section. Otherwise ld will
11174 zero the reloc (at least that is the intent, but some cooperation by
11175 the target dependent code is needed, particularly for REL targets). */
11176
11177 unsigned int
11178 _bfd_elf_default_action_discarded (asection *sec)
11179 {
11180 const struct elf_backend_data *bed;
11181 bed = get_elf_backend_data (sec->owner);
11182
11183 if (sec->flags & SEC_DEBUGGING)
11184 return PRETEND;
11185
11186 if (strcmp (".eh_frame", sec->name) == 0)
11187 return 0;
11188
11189 if (bed->elf_backend_can_make_multiple_eh_frame
11190 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11191 return 0;
11192
11193 if (strcmp (".sframe", sec->name) == 0)
11194 return 0;
11195
11196 if (strcmp (".gcc_except_table", sec->name) == 0)
11197 return 0;
11198
11199 return COMPLAIN | PRETEND;
11200 }
11201
11202 /* Find a match between a section and a member of a section group. */
11203
11204 static asection *
11205 match_group_member (asection *sec, asection *group,
11206 struct bfd_link_info *info)
11207 {
11208 asection *first = elf_next_in_group (group);
11209 asection *s = first;
11210
11211 while (s != NULL)
11212 {
11213 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11214 return s;
11215
11216 s = elf_next_in_group (s);
11217 if (s == first)
11218 break;
11219 }
11220
11221 return NULL;
11222 }
11223
11224 /* Check if the kept section of a discarded section SEC can be used
11225 to replace it. Return the replacement if it is OK. Otherwise return
11226 NULL. */
11227
11228 asection *
11229 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11230 {
11231 asection *kept;
11232
11233 kept = sec->kept_section;
11234 if (kept != NULL)
11235 {
11236 if ((kept->flags & SEC_GROUP) != 0)
11237 kept = match_group_member (sec, kept, info);
11238 if (kept != NULL)
11239 {
11240 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11241 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11242 kept = NULL;
11243 else
11244 {
11245 /* Get the real kept section. */
11246 asection *next;
11247 for (next = kept->kept_section;
11248 next != NULL;
11249 next = next->kept_section)
11250 kept = next;
11251 }
11252 }
11253 sec->kept_section = kept;
11254 }
11255 return kept;
11256 }
11257
11258 /* Link an input file into the linker output file. This function
11259 handles all the sections and relocations of the input file at once.
11260 This is so that we only have to read the local symbols once, and
11261 don't have to keep them in memory. */
11262
11263 static bool
11264 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11265 {
11266 int (*relocate_section)
11267 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11268 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11269 bfd *output_bfd;
11270 Elf_Internal_Shdr *symtab_hdr;
11271 size_t locsymcount;
11272 size_t extsymoff;
11273 Elf_Internal_Sym *isymbuf;
11274 Elf_Internal_Sym *isym;
11275 Elf_Internal_Sym *isymend;
11276 long *pindex;
11277 asection **ppsection;
11278 asection *o;
11279 const struct elf_backend_data *bed;
11280 struct elf_link_hash_entry **sym_hashes;
11281 bfd_size_type address_size;
11282 bfd_vma r_type_mask;
11283 int r_sym_shift;
11284 bool have_file_sym = false;
11285
11286 output_bfd = flinfo->output_bfd;
11287 bed = get_elf_backend_data (output_bfd);
11288 relocate_section = bed->elf_backend_relocate_section;
11289
11290 /* If this is a dynamic object, we don't want to do anything here:
11291 we don't want the local symbols, and we don't want the section
11292 contents. */
11293 if ((input_bfd->flags & DYNAMIC) != 0)
11294 return true;
11295
11296 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11297 if (elf_bad_symtab (input_bfd))
11298 {
11299 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11300 extsymoff = 0;
11301 }
11302 else
11303 {
11304 locsymcount = symtab_hdr->sh_info;
11305 extsymoff = symtab_hdr->sh_info;
11306 }
11307
11308 /* Enable GNU OSABI features in the output BFD that are used in the input
11309 BFD. */
11310 if (bed->elf_osabi == ELFOSABI_NONE
11311 || bed->elf_osabi == ELFOSABI_GNU
11312 || bed->elf_osabi == ELFOSABI_FREEBSD)
11313 elf_tdata (output_bfd)->has_gnu_osabi
11314 |= (elf_tdata (input_bfd)->has_gnu_osabi
11315 & (bfd_link_relocatable (flinfo->info)
11316 ? -1 : ~elf_gnu_osabi_retain));
11317
11318 /* Read the local symbols. */
11319 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11320 if (isymbuf == NULL && locsymcount != 0)
11321 {
11322 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11323 flinfo->internal_syms,
11324 flinfo->external_syms,
11325 flinfo->locsym_shndx);
11326 if (isymbuf == NULL)
11327 return false;
11328 }
11329
11330 /* Find local symbol sections and adjust values of symbols in
11331 SEC_MERGE sections. Write out those local symbols we know are
11332 going into the output file. */
11333 isymend = PTR_ADD (isymbuf, locsymcount);
11334 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11335 isym < isymend;
11336 isym++, pindex++, ppsection++)
11337 {
11338 asection *isec;
11339 const char *name;
11340 Elf_Internal_Sym osym;
11341 long indx;
11342 int ret;
11343
11344 *pindex = -1;
11345
11346 if (elf_bad_symtab (input_bfd))
11347 {
11348 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11349 {
11350 *ppsection = NULL;
11351 continue;
11352 }
11353 }
11354
11355 if (isym->st_shndx == SHN_UNDEF)
11356 isec = bfd_und_section_ptr;
11357 else if (isym->st_shndx == SHN_ABS)
11358 isec = bfd_abs_section_ptr;
11359 else if (isym->st_shndx == SHN_COMMON)
11360 isec = bfd_com_section_ptr;
11361 else
11362 {
11363 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11364 if (isec == NULL)
11365 {
11366 /* Don't attempt to output symbols with st_shnx in the
11367 reserved range other than SHN_ABS and SHN_COMMON. */
11368 isec = bfd_und_section_ptr;
11369 }
11370 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11371 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11372 isym->st_value =
11373 _bfd_merged_section_offset (output_bfd, &isec,
11374 elf_section_data (isec)->sec_info,
11375 isym->st_value);
11376 }
11377
11378 *ppsection = isec;
11379
11380 /* Don't output the first, undefined, symbol. In fact, don't
11381 output any undefined local symbol. */
11382 if (isec == bfd_und_section_ptr)
11383 continue;
11384
11385 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11386 {
11387 /* We never output section symbols. Instead, we use the
11388 section symbol of the corresponding section in the output
11389 file. */
11390 continue;
11391 }
11392
11393 /* If we are stripping all symbols, we don't want to output this
11394 one. */
11395 if (flinfo->info->strip == strip_all)
11396 continue;
11397
11398 /* If we are discarding all local symbols, we don't want to
11399 output this one. If we are generating a relocatable output
11400 file, then some of the local symbols may be required by
11401 relocs; we output them below as we discover that they are
11402 needed. */
11403 if (flinfo->info->discard == discard_all)
11404 continue;
11405
11406 /* If this symbol is defined in a section which we are
11407 discarding, we don't need to keep it. */
11408 if (isym->st_shndx < SHN_LORESERVE
11409 && (isec->output_section == NULL
11410 || bfd_section_removed_from_list (output_bfd,
11411 isec->output_section)))
11412 continue;
11413
11414 /* Get the name of the symbol. */
11415 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11416 isym->st_name);
11417 if (name == NULL)
11418 return false;
11419
11420 /* See if we are discarding symbols with this name. */
11421 if ((flinfo->info->strip == strip_some
11422 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11423 == NULL))
11424 || (((flinfo->info->discard == discard_sec_merge
11425 && (isec->flags & SEC_MERGE)
11426 && !bfd_link_relocatable (flinfo->info))
11427 || flinfo->info->discard == discard_l)
11428 && bfd_is_local_label_name (input_bfd, name)))
11429 continue;
11430
11431 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11432 {
11433 if (input_bfd->lto_output)
11434 /* -flto puts a temp file name here. This means builds
11435 are not reproducible. Discard the symbol. */
11436 continue;
11437 have_file_sym = true;
11438 flinfo->filesym_count += 1;
11439 }
11440 if (!have_file_sym)
11441 {
11442 /* In the absence of debug info, bfd_find_nearest_line uses
11443 FILE symbols to determine the source file for local
11444 function symbols. Provide a FILE symbol here if input
11445 files lack such, so that their symbols won't be
11446 associated with a previous input file. It's not the
11447 source file, but the best we can do. */
11448 const char *filename;
11449 have_file_sym = true;
11450 flinfo->filesym_count += 1;
11451 memset (&osym, 0, sizeof (osym));
11452 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11453 osym.st_shndx = SHN_ABS;
11454 if (input_bfd->lto_output)
11455 filename = NULL;
11456 else
11457 filename = lbasename (bfd_get_filename (input_bfd));
11458 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11459 bfd_abs_section_ptr, NULL))
11460 return false;
11461 }
11462
11463 osym = *isym;
11464
11465 /* Adjust the section index for the output file. */
11466 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11467 isec->output_section);
11468 if (osym.st_shndx == SHN_BAD)
11469 return false;
11470
11471 /* ELF symbols in relocatable files are section relative, but
11472 in executable files they are virtual addresses. Note that
11473 this code assumes that all ELF sections have an associated
11474 BFD section with a reasonable value for output_offset; below
11475 we assume that they also have a reasonable value for
11476 output_section. Any special sections must be set up to meet
11477 these requirements. */
11478 osym.st_value += isec->output_offset;
11479 if (!bfd_link_relocatable (flinfo->info))
11480 {
11481 osym.st_value += isec->output_section->vma;
11482 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11483 {
11484 /* STT_TLS symbols are relative to PT_TLS segment base. */
11485 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11486 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11487 else
11488 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11489 STT_NOTYPE);
11490 }
11491 }
11492
11493 indx = bfd_get_symcount (output_bfd);
11494 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11495 if (ret == 0)
11496 return false;
11497 else if (ret == 1)
11498 *pindex = indx;
11499 }
11500
11501 if (bed->s->arch_size == 32)
11502 {
11503 r_type_mask = 0xff;
11504 r_sym_shift = 8;
11505 address_size = 4;
11506 }
11507 else
11508 {
11509 r_type_mask = 0xffffffff;
11510 r_sym_shift = 32;
11511 address_size = 8;
11512 }
11513
11514 /* Relocate the contents of each section. */
11515 sym_hashes = elf_sym_hashes (input_bfd);
11516 for (o = input_bfd->sections; o != NULL; o = o->next)
11517 {
11518 bfd_byte *contents;
11519
11520 if (! o->linker_mark)
11521 {
11522 /* This section was omitted from the link. */
11523 continue;
11524 }
11525
11526 if (!flinfo->info->resolve_section_groups
11527 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11528 {
11529 /* Deal with the group signature symbol. */
11530 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11531 unsigned long symndx = sec_data->this_hdr.sh_info;
11532 asection *osec = o->output_section;
11533
11534 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11535 if (symndx >= locsymcount
11536 || (elf_bad_symtab (input_bfd)
11537 && flinfo->sections[symndx] == NULL))
11538 {
11539 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11540 while (h->root.type == bfd_link_hash_indirect
11541 || h->root.type == bfd_link_hash_warning)
11542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11543 /* Arrange for symbol to be output. */
11544 h->indx = -2;
11545 elf_section_data (osec)->this_hdr.sh_info = -2;
11546 }
11547 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11548 {
11549 /* We'll use the output section target_index. */
11550 asection *sec = flinfo->sections[symndx]->output_section;
11551 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11552 }
11553 else
11554 {
11555 if (flinfo->indices[symndx] == -1)
11556 {
11557 /* Otherwise output the local symbol now. */
11558 Elf_Internal_Sym sym = isymbuf[symndx];
11559 asection *sec = flinfo->sections[symndx]->output_section;
11560 const char *name;
11561 long indx;
11562 int ret;
11563
11564 name = bfd_elf_string_from_elf_section (input_bfd,
11565 symtab_hdr->sh_link,
11566 sym.st_name);
11567 if (name == NULL)
11568 return false;
11569
11570 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11571 sec);
11572 if (sym.st_shndx == SHN_BAD)
11573 return false;
11574
11575 sym.st_value += o->output_offset;
11576
11577 indx = bfd_get_symcount (output_bfd);
11578 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11579 NULL);
11580 if (ret == 0)
11581 return false;
11582 else if (ret == 1)
11583 flinfo->indices[symndx] = indx;
11584 else
11585 abort ();
11586 }
11587 elf_section_data (osec)->this_hdr.sh_info
11588 = flinfo->indices[symndx];
11589 }
11590 }
11591
11592 if ((o->flags & SEC_HAS_CONTENTS) == 0
11593 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11594 continue;
11595
11596 if ((o->flags & SEC_LINKER_CREATED) != 0)
11597 {
11598 /* Section was created by _bfd_elf_link_create_dynamic_sections
11599 or somesuch. */
11600 continue;
11601 }
11602
11603 /* Get the contents of the section. They have been cached by a
11604 relaxation routine. Note that o is a section in an input
11605 file, so the contents field will not have been set by any of
11606 the routines which work on output files. */
11607 if (elf_section_data (o)->this_hdr.contents != NULL)
11608 {
11609 contents = elf_section_data (o)->this_hdr.contents;
11610 if (bed->caches_rawsize
11611 && o->rawsize != 0
11612 && o->rawsize < o->size)
11613 {
11614 memcpy (flinfo->contents, contents, o->rawsize);
11615 contents = flinfo->contents;
11616 }
11617 }
11618 else if (!(o->flags & SEC_RELOC)
11619 && !bed->elf_backend_write_section
11620 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11621 /* A MERGE section that has no relocations doesn't need the
11622 contents anymore, they have been recorded earlier. Except
11623 if the backend has special provisions for writing sections. */
11624 contents = NULL;
11625 else
11626 {
11627 contents = flinfo->contents;
11628 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11629 &contents))
11630 return false;
11631 }
11632
11633 if ((o->flags & SEC_RELOC) != 0)
11634 {
11635 Elf_Internal_Rela *internal_relocs;
11636 Elf_Internal_Rela *rel, *relend;
11637 int action_discarded;
11638 int ret;
11639
11640 /* Get the swapped relocs. */
11641 internal_relocs
11642 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11643 flinfo->external_relocs,
11644 flinfo->internal_relocs,
11645 false);
11646 if (internal_relocs == NULL
11647 && o->reloc_count > 0)
11648 return false;
11649
11650 action_discarded = -1;
11651 if (!elf_section_ignore_discarded_relocs (o))
11652 action_discarded = (*bed->action_discarded) (o);
11653
11654 /* Run through the relocs evaluating complex reloc symbols and
11655 looking for relocs against symbols from discarded sections
11656 or section symbols from removed link-once sections.
11657 Complain about relocs against discarded sections. Zero
11658 relocs against removed link-once sections. */
11659
11660 rel = internal_relocs;
11661 relend = rel + o->reloc_count;
11662 for ( ; rel < relend; rel++)
11663 {
11664 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11665 unsigned int s_type;
11666 asection **ps, *sec;
11667 struct elf_link_hash_entry *h = NULL;
11668 const char *sym_name;
11669
11670 if (r_symndx == STN_UNDEF)
11671 continue;
11672
11673 if (r_symndx >= locsymcount
11674 || (elf_bad_symtab (input_bfd)
11675 && flinfo->sections[r_symndx] == NULL))
11676 {
11677 h = sym_hashes[r_symndx - extsymoff];
11678
11679 /* Badly formatted input files can contain relocs that
11680 reference non-existant symbols. Check here so that
11681 we do not seg fault. */
11682 if (h == NULL)
11683 {
11684 _bfd_error_handler
11685 /* xgettext:c-format */
11686 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11687 "that references a non-existent global symbol"),
11688 input_bfd, (uint64_t) rel->r_info, o);
11689 bfd_set_error (bfd_error_bad_value);
11690 return false;
11691 }
11692
11693 while (h->root.type == bfd_link_hash_indirect
11694 || h->root.type == bfd_link_hash_warning)
11695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11696
11697 s_type = h->type;
11698
11699 /* If a plugin symbol is referenced from a non-IR file,
11700 mark the symbol as undefined. Note that the
11701 linker may attach linker created dynamic sections
11702 to the plugin bfd. Symbols defined in linker
11703 created sections are not plugin symbols. */
11704 if ((h->root.non_ir_ref_regular
11705 || h->root.non_ir_ref_dynamic)
11706 && (h->root.type == bfd_link_hash_defined
11707 || h->root.type == bfd_link_hash_defweak)
11708 && (h->root.u.def.section->flags
11709 & SEC_LINKER_CREATED) == 0
11710 && h->root.u.def.section->owner != NULL
11711 && (h->root.u.def.section->owner->flags
11712 & BFD_PLUGIN) != 0)
11713 {
11714 h->root.type = bfd_link_hash_undefined;
11715 h->root.u.undef.abfd = h->root.u.def.section->owner;
11716 }
11717
11718 ps = NULL;
11719 if (h->root.type == bfd_link_hash_defined
11720 || h->root.type == bfd_link_hash_defweak)
11721 ps = &h->root.u.def.section;
11722
11723 sym_name = h->root.root.string;
11724 }
11725 else
11726 {
11727 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11728
11729 s_type = ELF_ST_TYPE (sym->st_info);
11730 ps = &flinfo->sections[r_symndx];
11731 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11732 sym, *ps);
11733 }
11734
11735 if ((s_type == STT_RELC || s_type == STT_SRELC)
11736 && !bfd_link_relocatable (flinfo->info))
11737 {
11738 bfd_vma val;
11739 bfd_vma dot = (rel->r_offset
11740 + o->output_offset + o->output_section->vma);
11741 #ifdef DEBUG
11742 printf ("Encountered a complex symbol!");
11743 printf (" (input_bfd %s, section %s, reloc %ld\n",
11744 bfd_get_filename (input_bfd), o->name,
11745 (long) (rel - internal_relocs));
11746 printf (" symbol: idx %8.8lx, name %s\n",
11747 r_symndx, sym_name);
11748 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11749 (unsigned long) rel->r_info,
11750 (unsigned long) rel->r_offset);
11751 #endif
11752 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11753 isymbuf, locsymcount, s_type == STT_SRELC))
11754 return false;
11755
11756 /* Symbol evaluated OK. Update to absolute value. */
11757 set_symbol_value (input_bfd, isymbuf, locsymcount,
11758 r_symndx, val);
11759 continue;
11760 }
11761
11762 if (action_discarded != -1 && ps != NULL)
11763 {
11764 /* Complain if the definition comes from a
11765 discarded section. */
11766 if ((sec = *ps) != NULL && discarded_section (sec))
11767 {
11768 BFD_ASSERT (r_symndx != STN_UNDEF);
11769 if (action_discarded & COMPLAIN)
11770 (*flinfo->info->callbacks->einfo)
11771 /* xgettext:c-format */
11772 (_("%X`%s' referenced in section `%pA' of %pB: "
11773 "defined in discarded section `%pA' of %pB\n"),
11774 sym_name, o, input_bfd, sec, sec->owner);
11775
11776 /* Try to do the best we can to support buggy old
11777 versions of gcc. Pretend that the symbol is
11778 really defined in the kept linkonce section.
11779 FIXME: This is quite broken. Modifying the
11780 symbol here means we will be changing all later
11781 uses of the symbol, not just in this section. */
11782 if (action_discarded & PRETEND)
11783 {
11784 asection *kept;
11785
11786 kept = _bfd_elf_check_kept_section (sec,
11787 flinfo->info);
11788 if (kept != NULL)
11789 {
11790 *ps = kept;
11791 continue;
11792 }
11793 }
11794 }
11795 }
11796 }
11797
11798 /* Relocate the section by invoking a back end routine.
11799
11800 The back end routine is responsible for adjusting the
11801 section contents as necessary, and (if using Rela relocs
11802 and generating a relocatable output file) adjusting the
11803 reloc addend as necessary.
11804
11805 The back end routine does not have to worry about setting
11806 the reloc address or the reloc symbol index.
11807
11808 The back end routine is given a pointer to the swapped in
11809 internal symbols, and can access the hash table entries
11810 for the external symbols via elf_sym_hashes (input_bfd).
11811
11812 When generating relocatable output, the back end routine
11813 must handle STB_LOCAL/STT_SECTION symbols specially. The
11814 output symbol is going to be a section symbol
11815 corresponding to the output section, which will require
11816 the addend to be adjusted. */
11817
11818 ret = (*relocate_section) (output_bfd, flinfo->info,
11819 input_bfd, o, contents,
11820 internal_relocs,
11821 isymbuf,
11822 flinfo->sections);
11823 if (!ret)
11824 return false;
11825
11826 if (ret == 2
11827 || bfd_link_relocatable (flinfo->info)
11828 || flinfo->info->emitrelocations)
11829 {
11830 Elf_Internal_Rela *irela;
11831 Elf_Internal_Rela *irelaend, *irelamid;
11832 bfd_vma last_offset;
11833 struct elf_link_hash_entry **rel_hash;
11834 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11835 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11836 unsigned int next_erel;
11837 bool rela_normal;
11838 struct bfd_elf_section_data *esdi, *esdo;
11839
11840 esdi = elf_section_data (o);
11841 esdo = elf_section_data (o->output_section);
11842 rela_normal = false;
11843
11844 /* Adjust the reloc addresses and symbol indices. */
11845
11846 irela = internal_relocs;
11847 irelaend = irela + o->reloc_count;
11848 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11849 /* We start processing the REL relocs, if any. When we reach
11850 IRELAMID in the loop, we switch to the RELA relocs. */
11851 irelamid = irela;
11852 if (esdi->rel.hdr != NULL)
11853 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11854 * bed->s->int_rels_per_ext_rel);
11855 rel_hash_list = rel_hash;
11856 rela_hash_list = NULL;
11857 last_offset = o->output_offset;
11858 if (!bfd_link_relocatable (flinfo->info))
11859 last_offset += o->output_section->vma;
11860 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11861 {
11862 unsigned long r_symndx;
11863 asection *sec;
11864 Elf_Internal_Sym sym;
11865
11866 if (next_erel == bed->s->int_rels_per_ext_rel)
11867 {
11868 rel_hash++;
11869 next_erel = 0;
11870 }
11871
11872 if (irela == irelamid)
11873 {
11874 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11875 rela_hash_list = rel_hash;
11876 if (bed->is_rela_normal != NULL)
11877 rela_normal = bed->is_rela_normal (irela);
11878 else
11879 rela_normal = bed->rela_normal;
11880 }
11881
11882 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11883 flinfo->info, o,
11884 irela->r_offset);
11885 if (irela->r_offset >= (bfd_vma) -2)
11886 {
11887 /* This is a reloc for a deleted entry or somesuch.
11888 Turn it into an R_*_NONE reloc, at the same
11889 offset as the last reloc. elf_eh_frame.c and
11890 bfd_elf_discard_info rely on reloc offsets
11891 being ordered. */
11892 irela->r_offset = last_offset;
11893 irela->r_info = 0;
11894 irela->r_addend = 0;
11895 continue;
11896 }
11897
11898 irela->r_offset += o->output_offset;
11899
11900 /* Relocs in an executable have to be virtual addresses. */
11901 if (!bfd_link_relocatable (flinfo->info))
11902 irela->r_offset += o->output_section->vma;
11903
11904 last_offset = irela->r_offset;
11905
11906 r_symndx = irela->r_info >> r_sym_shift;
11907 if (r_symndx == STN_UNDEF)
11908 continue;
11909
11910 if (r_symndx >= locsymcount
11911 || (elf_bad_symtab (input_bfd)
11912 && flinfo->sections[r_symndx] == NULL))
11913 {
11914 struct elf_link_hash_entry *rh;
11915 unsigned long indx;
11916
11917 /* This is a reloc against a global symbol. We
11918 have not yet output all the local symbols, so
11919 we do not know the symbol index of any global
11920 symbol. We set the rel_hash entry for this
11921 reloc to point to the global hash table entry
11922 for this symbol. The symbol index is then
11923 set at the end of bfd_elf_final_link. */
11924 indx = r_symndx - extsymoff;
11925 rh = elf_sym_hashes (input_bfd)[indx];
11926 while (rh->root.type == bfd_link_hash_indirect
11927 || rh->root.type == bfd_link_hash_warning)
11928 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11929
11930 /* Setting the index to -2 tells
11931 elf_link_output_extsym that this symbol is
11932 used by a reloc. */
11933 BFD_ASSERT (rh->indx < 0);
11934 rh->indx = -2;
11935 *rel_hash = rh;
11936
11937 continue;
11938 }
11939
11940 /* This is a reloc against a local symbol. */
11941
11942 *rel_hash = NULL;
11943 sym = isymbuf[r_symndx];
11944 sec = flinfo->sections[r_symndx];
11945 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11946 {
11947 /* I suppose the backend ought to fill in the
11948 section of any STT_SECTION symbol against a
11949 processor specific section. */
11950 r_symndx = STN_UNDEF;
11951 if (bfd_is_abs_section (sec))
11952 ;
11953 else if (sec == NULL || sec->owner == NULL)
11954 {
11955 bfd_set_error (bfd_error_bad_value);
11956 return false;
11957 }
11958 else
11959 {
11960 asection *osec = sec->output_section;
11961
11962 /* If we have discarded a section, the output
11963 section will be the absolute section. In
11964 case of discarded SEC_MERGE sections, use
11965 the kept section. relocate_section should
11966 have already handled discarded linkonce
11967 sections. */
11968 if (bfd_is_abs_section (osec)
11969 && sec->kept_section != NULL
11970 && sec->kept_section->output_section != NULL)
11971 {
11972 osec = sec->kept_section->output_section;
11973 irela->r_addend -= osec->vma;
11974 }
11975
11976 if (!bfd_is_abs_section (osec))
11977 {
11978 r_symndx = osec->target_index;
11979 if (r_symndx == STN_UNDEF)
11980 {
11981 irela->r_addend += osec->vma;
11982 osec = _bfd_nearby_section (output_bfd, osec,
11983 osec->vma);
11984 irela->r_addend -= osec->vma;
11985 r_symndx = osec->target_index;
11986 }
11987 }
11988 }
11989
11990 /* Adjust the addend according to where the
11991 section winds up in the output section. */
11992 if (rela_normal)
11993 irela->r_addend += sec->output_offset;
11994 }
11995 else
11996 {
11997 if (flinfo->indices[r_symndx] == -1)
11998 {
11999 unsigned long shlink;
12000 const char *name;
12001 asection *osec;
12002 long indx;
12003
12004 if (flinfo->info->strip == strip_all)
12005 {
12006 /* You can't do ld -r -s. */
12007 bfd_set_error (bfd_error_invalid_operation);
12008 return false;
12009 }
12010
12011 /* This symbol was skipped earlier, but
12012 since it is needed by a reloc, we
12013 must output it now. */
12014 shlink = symtab_hdr->sh_link;
12015 name = (bfd_elf_string_from_elf_section
12016 (input_bfd, shlink, sym.st_name));
12017 if (name == NULL)
12018 return false;
12019
12020 osec = sec->output_section;
12021 sym.st_shndx =
12022 _bfd_elf_section_from_bfd_section (output_bfd,
12023 osec);
12024 if (sym.st_shndx == SHN_BAD)
12025 return false;
12026
12027 sym.st_value += sec->output_offset;
12028 if (!bfd_link_relocatable (flinfo->info))
12029 {
12030 sym.st_value += osec->vma;
12031 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12032 {
12033 struct elf_link_hash_table *htab
12034 = elf_hash_table (flinfo->info);
12035
12036 /* STT_TLS symbols are relative to PT_TLS
12037 segment base. */
12038 if (htab->tls_sec != NULL)
12039 sym.st_value -= htab->tls_sec->vma;
12040 else
12041 sym.st_info
12042 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12043 STT_NOTYPE);
12044 }
12045 }
12046
12047 indx = bfd_get_symcount (output_bfd);
12048 ret = elf_link_output_symstrtab (flinfo, name,
12049 &sym, sec,
12050 NULL);
12051 if (ret == 0)
12052 return false;
12053 else if (ret == 1)
12054 flinfo->indices[r_symndx] = indx;
12055 else
12056 abort ();
12057 }
12058
12059 r_symndx = flinfo->indices[r_symndx];
12060 }
12061
12062 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12063 | (irela->r_info & r_type_mask));
12064 }
12065
12066 /* Swap out the relocs. */
12067 input_rel_hdr = esdi->rel.hdr;
12068 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
12069 {
12070 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12071 input_rel_hdr,
12072 internal_relocs,
12073 rel_hash_list))
12074 return false;
12075 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12076 * bed->s->int_rels_per_ext_rel);
12077 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
12078 }
12079
12080 input_rela_hdr = esdi->rela.hdr;
12081 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12082 {
12083 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12084 input_rela_hdr,
12085 internal_relocs,
12086 rela_hash_list))
12087 return false;
12088 }
12089 }
12090 }
12091
12092 /* Write out the modified section contents. */
12093 if (bed->elf_backend_write_section
12094 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
12095 contents))
12096 {
12097 /* Section written out. */
12098 }
12099 else switch (o->sec_info_type)
12100 {
12101 case SEC_INFO_TYPE_STABS:
12102 if (! (_bfd_write_section_stabs
12103 (output_bfd,
12104 &elf_hash_table (flinfo->info)->stab_info,
12105 o, &elf_section_data (o)->sec_info, contents)))
12106 return false;
12107 break;
12108 case SEC_INFO_TYPE_MERGE:
12109 if (! _bfd_write_merged_section (output_bfd, o,
12110 elf_section_data (o)->sec_info))
12111 return false;
12112 break;
12113 case SEC_INFO_TYPE_EH_FRAME:
12114 {
12115 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
12116 o, contents))
12117 return false;
12118 }
12119 break;
12120 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12121 {
12122 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12123 flinfo->info,
12124 o, contents))
12125 return false;
12126 }
12127 break;
12128 case SEC_INFO_TYPE_SFRAME:
12129 {
12130 /* Merge .sframe sections into the ctf frame encoder
12131 context of the output_bfd's section. The final .sframe
12132 output section will be written out later. */
12133 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12134 o, contents))
12135 return false;
12136 }
12137 break;
12138 default:
12139 {
12140 if (! (o->flags & SEC_EXCLUDE))
12141 {
12142 file_ptr offset = (file_ptr) o->output_offset;
12143 bfd_size_type todo = o->size;
12144
12145 offset *= bfd_octets_per_byte (output_bfd, o);
12146
12147 if ((o->flags & SEC_ELF_REVERSE_COPY)
12148 && o->size > address_size)
12149 {
12150 /* Reverse-copy input section to output. */
12151
12152 if ((o->size & (address_size - 1)) != 0
12153 || (o->reloc_count != 0
12154 && (o->size * bed->s->int_rels_per_ext_rel
12155 != o->reloc_count * address_size)))
12156 {
12157 _bfd_error_handler
12158 /* xgettext:c-format */
12159 (_("error: %pB: size of section %pA is not "
12160 "multiple of address size"),
12161 input_bfd, o);
12162 bfd_set_error (bfd_error_bad_value);
12163 return false;
12164 }
12165
12166 do
12167 {
12168 todo -= address_size;
12169 if (! bfd_set_section_contents (output_bfd,
12170 o->output_section,
12171 contents + todo,
12172 offset,
12173 address_size))
12174 return false;
12175 if (todo == 0)
12176 break;
12177 offset += address_size;
12178 }
12179 while (1);
12180 }
12181 else if (! bfd_set_section_contents (output_bfd,
12182 o->output_section,
12183 contents,
12184 offset, todo))
12185 return false;
12186 }
12187 }
12188 break;
12189 }
12190
12191 /* Munmap the section contents for each input section. */
12192 _bfd_elf_link_munmap_section_contents (o);
12193 }
12194
12195 return true;
12196 }
12197
12198 /* Generate a reloc when linking an ELF file. This is a reloc
12199 requested by the linker, and does not come from any input file. This
12200 is used to build constructor and destructor tables when linking
12201 with -Ur. */
12202
12203 static bool
12204 elf_reloc_link_order (bfd *output_bfd,
12205 struct bfd_link_info *info,
12206 asection *output_section,
12207 struct bfd_link_order *link_order)
12208 {
12209 reloc_howto_type *howto;
12210 long indx;
12211 bfd_vma offset;
12212 bfd_vma addend;
12213 struct bfd_elf_section_reloc_data *reldata;
12214 struct elf_link_hash_entry **rel_hash_ptr;
12215 Elf_Internal_Shdr *rel_hdr;
12216 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12217 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12218 bfd_byte *erel;
12219 unsigned int i;
12220 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12221
12222 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12223 if (howto == NULL)
12224 {
12225 bfd_set_error (bfd_error_bad_value);
12226 return false;
12227 }
12228
12229 addend = link_order->u.reloc.p->addend;
12230
12231 if (esdo->rel.hdr)
12232 reldata = &esdo->rel;
12233 else if (esdo->rela.hdr)
12234 reldata = &esdo->rela;
12235 else
12236 {
12237 reldata = NULL;
12238 BFD_ASSERT (0);
12239 }
12240
12241 /* Figure out the symbol index. */
12242 rel_hash_ptr = reldata->hashes + reldata->count;
12243 if (link_order->type == bfd_section_reloc_link_order)
12244 {
12245 indx = link_order->u.reloc.p->u.section->target_index;
12246 BFD_ASSERT (indx != 0);
12247 *rel_hash_ptr = NULL;
12248 }
12249 else
12250 {
12251 struct elf_link_hash_entry *h;
12252
12253 /* Treat a reloc against a defined symbol as though it were
12254 actually against the section. */
12255 h = ((struct elf_link_hash_entry *)
12256 bfd_wrapped_link_hash_lookup (output_bfd, info,
12257 link_order->u.reloc.p->u.name,
12258 false, false, true));
12259 if (h != NULL
12260 && (h->root.type == bfd_link_hash_defined
12261 || h->root.type == bfd_link_hash_defweak))
12262 {
12263 asection *section;
12264
12265 section = h->root.u.def.section;
12266 indx = section->output_section->target_index;
12267 *rel_hash_ptr = NULL;
12268 /* It seems that we ought to add the symbol value to the
12269 addend here, but in practice it has already been added
12270 because it was passed to constructor_callback. */
12271 addend += section->output_section->vma + section->output_offset;
12272 }
12273 else if (h != NULL)
12274 {
12275 /* Setting the index to -2 tells elf_link_output_extsym that
12276 this symbol is used by a reloc. */
12277 h->indx = -2;
12278 *rel_hash_ptr = h;
12279 indx = 0;
12280 }
12281 else
12282 {
12283 (*info->callbacks->unattached_reloc)
12284 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12285 indx = 0;
12286 }
12287 }
12288
12289 /* If this is an inplace reloc, we must write the addend into the
12290 object file. */
12291 if (howto->partial_inplace && addend != 0)
12292 {
12293 bfd_size_type size;
12294 bfd_reloc_status_type rstat;
12295 bfd_byte *buf;
12296 bool ok;
12297 const char *sym_name;
12298 bfd_size_type octets;
12299
12300 size = (bfd_size_type) bfd_get_reloc_size (howto);
12301 buf = (bfd_byte *) bfd_zmalloc (size);
12302 if (buf == NULL && size != 0)
12303 return false;
12304 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12305 switch (rstat)
12306 {
12307 case bfd_reloc_ok:
12308 break;
12309
12310 default:
12311 case bfd_reloc_outofrange:
12312 abort ();
12313
12314 case bfd_reloc_overflow:
12315 if (link_order->type == bfd_section_reloc_link_order)
12316 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12317 else
12318 sym_name = link_order->u.reloc.p->u.name;
12319 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12320 howto->name, addend, NULL, NULL,
12321 (bfd_vma) 0);
12322 break;
12323 }
12324
12325 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12326 output_section);
12327 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12328 octets, size);
12329 free (buf);
12330 if (! ok)
12331 return false;
12332 }
12333
12334 /* The address of a reloc is relative to the section in a
12335 relocatable file, and is a virtual address in an executable
12336 file. */
12337 offset = link_order->offset;
12338 if (! bfd_link_relocatable (info))
12339 offset += output_section->vma;
12340
12341 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12342 {
12343 irel[i].r_offset = offset;
12344 irel[i].r_info = 0;
12345 irel[i].r_addend = 0;
12346 }
12347 if (bed->s->arch_size == 32)
12348 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12349 else
12350 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12351
12352 rel_hdr = reldata->hdr;
12353 erel = rel_hdr->contents;
12354 if (rel_hdr->sh_type == SHT_REL)
12355 {
12356 erel += reldata->count * bed->s->sizeof_rel;
12357 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12358 }
12359 else
12360 {
12361 irel[0].r_addend = addend;
12362 erel += reldata->count * bed->s->sizeof_rela;
12363 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12364 }
12365
12366 ++reldata->count;
12367
12368 return true;
12369 }
12370
12371 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12372 Returns TRUE upon success, FALSE otherwise. */
12373
12374 static bool
12375 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12376 {
12377 bool ret = false;
12378 bfd *implib_bfd;
12379 const struct elf_backend_data *bed;
12380 flagword flags;
12381 enum bfd_architecture arch;
12382 unsigned int mach;
12383 asymbol **sympp = NULL;
12384 long symsize;
12385 long symcount;
12386 long src_count;
12387 elf_symbol_type *osymbuf;
12388 size_t amt;
12389
12390 implib_bfd = info->out_implib_bfd;
12391 bed = get_elf_backend_data (abfd);
12392
12393 if (!bfd_set_format (implib_bfd, bfd_object))
12394 return false;
12395
12396 /* Use flag from executable but make it a relocatable object. */
12397 flags = bfd_get_file_flags (abfd);
12398 flags &= ~HAS_RELOC;
12399 if (!bfd_set_start_address (implib_bfd, 0)
12400 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12401 return false;
12402
12403 /* Copy architecture of output file to import library file. */
12404 arch = bfd_get_arch (abfd);
12405 mach = bfd_get_mach (abfd);
12406 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12407 && (abfd->target_defaulted
12408 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12409 return false;
12410
12411 /* Get symbol table size. */
12412 symsize = bfd_get_symtab_upper_bound (abfd);
12413 if (symsize < 0)
12414 return false;
12415
12416 /* Read in the symbol table. */
12417 sympp = (asymbol **) bfd_malloc (symsize);
12418 if (sympp == NULL)
12419 return false;
12420
12421 symcount = bfd_canonicalize_symtab (abfd, sympp);
12422 if (symcount < 0)
12423 goto free_sym_buf;
12424
12425 /* Allow the BFD backend to copy any private header data it
12426 understands from the output BFD to the import library BFD. */
12427 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12428 goto free_sym_buf;
12429
12430 /* Filter symbols to appear in the import library. */
12431 if (bed->elf_backend_filter_implib_symbols)
12432 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12433 symcount);
12434 else
12435 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12436 if (symcount == 0)
12437 {
12438 bfd_set_error (bfd_error_no_symbols);
12439 _bfd_error_handler (_("%pB: no symbol found for import library"),
12440 implib_bfd);
12441 goto free_sym_buf;
12442 }
12443
12444
12445 /* Make symbols absolute. */
12446 amt = symcount * sizeof (*osymbuf);
12447 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12448 if (osymbuf == NULL)
12449 goto free_sym_buf;
12450
12451 for (src_count = 0; src_count < symcount; src_count++)
12452 {
12453 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12454 sizeof (*osymbuf));
12455 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12456 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12457 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12458 osymbuf[src_count].internal_elf_sym.st_value =
12459 osymbuf[src_count].symbol.value;
12460 sympp[src_count] = &osymbuf[src_count].symbol;
12461 }
12462
12463 bfd_set_symtab (implib_bfd, sympp, symcount);
12464
12465 /* Allow the BFD backend to copy any private data it understands
12466 from the output BFD to the import library BFD. This is done last
12467 to permit the routine to look at the filtered symbol table. */
12468 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12469 goto free_sym_buf;
12470
12471 if (!bfd_close (implib_bfd))
12472 goto free_sym_buf;
12473
12474 ret = true;
12475
12476 free_sym_buf:
12477 free (sympp);
12478 return ret;
12479 }
12480
12481 static void
12482 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12483 {
12484 asection *o;
12485
12486 if (flinfo->symstrtab != NULL)
12487 _bfd_elf_strtab_free (flinfo->symstrtab);
12488 free (flinfo->contents);
12489 free (flinfo->external_relocs);
12490 free (flinfo->internal_relocs);
12491 free (flinfo->external_syms);
12492 free (flinfo->locsym_shndx);
12493 free (flinfo->internal_syms);
12494 free (flinfo->indices);
12495 free (flinfo->sections);
12496 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12497 free (flinfo->symshndxbuf);
12498 for (o = obfd->sections; o != NULL; o = o->next)
12499 {
12500 struct bfd_elf_section_data *esdo = elf_section_data (o);
12501 free (esdo->rel.hashes);
12502 free (esdo->rela.hashes);
12503 }
12504 }
12505
12506 /* Do the final step of an ELF link. */
12507
12508 bool
12509 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12510 {
12511 bool dynamic;
12512 bool emit_relocs;
12513 bfd *dynobj;
12514 struct elf_final_link_info flinfo;
12515 asection *o;
12516 struct bfd_link_order *p;
12517 bfd *sub;
12518 bfd_size_type max_contents_size;
12519 bfd_size_type max_external_reloc_size;
12520 bfd_size_type max_internal_reloc_count;
12521 bfd_size_type max_sym_count;
12522 bfd_size_type max_sym_shndx_count;
12523 Elf_Internal_Sym elfsym;
12524 unsigned int i;
12525 Elf_Internal_Shdr *symtab_hdr;
12526 Elf_Internal_Shdr *symtab_shndx_hdr;
12527 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12528 struct elf_outext_info eoinfo;
12529 bool merged;
12530 size_t relativecount;
12531 size_t relr_entsize;
12532 asection *reldyn = 0;
12533 bfd_size_type amt;
12534 asection *attr_section = NULL;
12535 bfd_vma attr_size = 0;
12536 const char *std_attrs_section;
12537 struct elf_link_hash_table *htab = elf_hash_table (info);
12538 bool sections_removed;
12539 bool ret;
12540
12541 if (!is_elf_hash_table (&htab->root))
12542 return false;
12543
12544 if (bfd_link_pic (info))
12545 abfd->flags |= DYNAMIC;
12546
12547 dynamic = htab->dynamic_sections_created;
12548 dynobj = htab->dynobj;
12549
12550 emit_relocs = (bfd_link_relocatable (info)
12551 || info->emitrelocations);
12552
12553 memset (&flinfo, 0, sizeof (flinfo));
12554 flinfo.info = info;
12555 flinfo.output_bfd = abfd;
12556 flinfo.symstrtab = _bfd_elf_strtab_init ();
12557 if (flinfo.symstrtab == NULL)
12558 return false;
12559
12560 if (! dynamic)
12561 {
12562 flinfo.hash_sec = NULL;
12563 flinfo.symver_sec = NULL;
12564 }
12565 else
12566 {
12567 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12568 /* Note that dynsym_sec can be NULL (on VMS). */
12569 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12570 /* Note that it is OK if symver_sec is NULL. */
12571 }
12572
12573 if (info->unique_symbol
12574 && !bfd_hash_table_init (&flinfo.local_hash_table,
12575 local_hash_newfunc,
12576 sizeof (struct local_hash_entry)))
12577 return false;
12578
12579 /* The object attributes have been merged. Remove the input
12580 sections from the link, and set the contents of the output
12581 section. */
12582 sections_removed = false;
12583 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12584 for (o = abfd->sections; o != NULL; o = o->next)
12585 {
12586 bool remove_section = false;
12587
12588 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12589 || strcmp (o->name, ".gnu.attributes") == 0)
12590 {
12591 for (p = o->map_head.link_order; p != NULL; p = p->next)
12592 {
12593 asection *input_section;
12594
12595 if (p->type != bfd_indirect_link_order)
12596 continue;
12597 input_section = p->u.indirect.section;
12598 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12599 elf_link_input_bfd ignores this section. */
12600 input_section->flags &= ~SEC_HAS_CONTENTS;
12601 }
12602
12603 attr_size = bfd_elf_obj_attr_size (abfd);
12604 bfd_set_section_size (o, attr_size);
12605 /* Skip this section later on. */
12606 o->map_head.link_order = NULL;
12607 if (attr_size)
12608 attr_section = o;
12609 else
12610 remove_section = true;
12611 }
12612 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12613 {
12614 /* Remove empty group section from linker output. */
12615 remove_section = true;
12616 }
12617 if (remove_section)
12618 {
12619 o->flags |= SEC_EXCLUDE;
12620 bfd_section_list_remove (abfd, o);
12621 abfd->section_count--;
12622 sections_removed = true;
12623 }
12624 }
12625 if (sections_removed)
12626 _bfd_fix_excluded_sec_syms (abfd, info);
12627
12628 /* Count up the number of relocations we will output for each output
12629 section, so that we know the sizes of the reloc sections. We
12630 also figure out some maximum sizes. */
12631 #ifdef USE_MMAP
12632 if (bed->use_mmap)
12633 {
12634 /* Mmap is used only if section size >= the minimum mmap section
12635 size. The initial max_contents_size value covers all sections
12636 smaller than the minimum mmap section size. It may be increased
12637 for compressed or linker created sections or sections whose
12638 rawsize != size. max_external_reloc_size covers all relocation
12639 sections smaller than the minimum mmap section size. */
12640 max_contents_size = _bfd_minimum_mmap_size;
12641 max_external_reloc_size = _bfd_minimum_mmap_size;
12642 }
12643 else
12644 #endif
12645 {
12646 max_contents_size = 0;
12647 max_external_reloc_size = 0;
12648 }
12649 max_internal_reloc_count = 0;
12650 max_sym_count = 0;
12651 max_sym_shndx_count = 0;
12652 merged = false;
12653 for (o = abfd->sections; o != NULL; o = o->next)
12654 {
12655 struct bfd_elf_section_data *esdo = elf_section_data (o);
12656 o->reloc_count = 0;
12657
12658 for (p = o->map_head.link_order; p != NULL; p = p->next)
12659 {
12660 unsigned int reloc_count = 0;
12661 unsigned int additional_reloc_count = 0;
12662 struct bfd_elf_section_data *esdi = NULL;
12663
12664 if (p->type == bfd_section_reloc_link_order
12665 || p->type == bfd_symbol_reloc_link_order)
12666 reloc_count = 1;
12667 else if (p->type == bfd_indirect_link_order)
12668 {
12669 asection *sec;
12670
12671 sec = p->u.indirect.section;
12672
12673 /* Mark all sections which are to be included in the
12674 link. This will normally be every section. We need
12675 to do this so that we can identify any sections which
12676 the linker has decided to not include. */
12677 sec->linker_mark = true;
12678
12679 if (sec->flags & SEC_MERGE)
12680 merged = true;
12681
12682 #ifdef USE_MMAP
12683 /* Mmap is used only on non-compressed, non-linker created
12684 sections whose rawsize == size. */
12685 if (!bed->use_mmap
12686 || sec->compress_status != COMPRESS_SECTION_NONE
12687 || (sec->flags & SEC_LINKER_CREATED) != 0
12688 || sec->rawsize != sec->size)
12689 #endif
12690 {
12691 if (sec->rawsize > max_contents_size)
12692 max_contents_size = sec->rawsize;
12693 if (sec->size > max_contents_size)
12694 max_contents_size = sec->size;
12695 }
12696
12697 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12698 && (sec->owner->flags & DYNAMIC) == 0)
12699 {
12700 size_t sym_count;
12701
12702 /* We are interested in just local symbols, not all
12703 symbols. */
12704 if (elf_bad_symtab (sec->owner))
12705 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12706 / bed->s->sizeof_sym);
12707 else
12708 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12709
12710 if (sym_count > max_sym_count)
12711 max_sym_count = sym_count;
12712
12713 if (sym_count > max_sym_shndx_count
12714 && elf_symtab_shndx_list (sec->owner) != NULL)
12715 max_sym_shndx_count = sym_count;
12716
12717 esdi = elf_section_data (sec);
12718
12719 if (esdi->this_hdr.sh_type == SHT_REL
12720 || esdi->this_hdr.sh_type == SHT_RELA)
12721 /* Some backends use reloc_count in relocation sections
12722 to count particular types of relocs. Of course,
12723 reloc sections themselves can't have relocations. */
12724 ;
12725 else if (emit_relocs)
12726 {
12727 reloc_count = sec->reloc_count;
12728 if (bed->elf_backend_count_additional_relocs)
12729 {
12730 int c;
12731 c = (*bed->elf_backend_count_additional_relocs) (sec);
12732 additional_reloc_count += c;
12733 }
12734 }
12735 else if (bed->elf_backend_count_relocs)
12736 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12737
12738 if ((sec->flags & SEC_RELOC) != 0)
12739 {
12740 #ifdef USE_MMAP
12741 if (!bed->use_mmap)
12742 #endif
12743 {
12744 size_t ext_size = 0;
12745
12746 if (esdi->rel.hdr != NULL)
12747 ext_size = esdi->rel.hdr->sh_size;
12748 if (esdi->rela.hdr != NULL)
12749 ext_size += esdi->rela.hdr->sh_size;
12750
12751 if (ext_size > max_external_reloc_size)
12752 max_external_reloc_size = ext_size;
12753 }
12754 if (sec->reloc_count > max_internal_reloc_count)
12755 max_internal_reloc_count = sec->reloc_count;
12756 }
12757 }
12758 }
12759
12760 if (reloc_count == 0)
12761 continue;
12762
12763 reloc_count += additional_reloc_count;
12764 o->reloc_count += reloc_count;
12765
12766 if (p->type == bfd_indirect_link_order && emit_relocs)
12767 {
12768 if (esdi->rel.hdr)
12769 {
12770 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12771 esdo->rel.count += additional_reloc_count;
12772 }
12773 if (esdi->rela.hdr)
12774 {
12775 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12776 esdo->rela.count += additional_reloc_count;
12777 }
12778 }
12779 else
12780 {
12781 if (o->use_rela_p)
12782 esdo->rela.count += reloc_count;
12783 else
12784 esdo->rel.count += reloc_count;
12785 }
12786 }
12787
12788 if (o->reloc_count > 0)
12789 o->flags |= SEC_RELOC;
12790 else
12791 {
12792 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12793 set it (this is probably a bug) and if it is set
12794 assign_section_numbers will create a reloc section. */
12795 o->flags &=~ SEC_RELOC;
12796 }
12797
12798 /* If the SEC_ALLOC flag is not set, force the section VMA to
12799 zero. This is done in elf_fake_sections as well, but forcing
12800 the VMA to 0 here will ensure that relocs against these
12801 sections are handled correctly. */
12802 if ((o->flags & SEC_ALLOC) == 0
12803 && ! o->user_set_vma)
12804 o->vma = 0;
12805 }
12806
12807 if (! bfd_link_relocatable (info) && merged)
12808 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12809
12810 /* Figure out the file positions for everything but the symbol table
12811 and the relocs. We set symcount to force assign_section_numbers
12812 to create a symbol table. */
12813 abfd->symcount = info->strip != strip_all || emit_relocs;
12814 BFD_ASSERT (! abfd->output_has_begun);
12815 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12816 goto error_return;
12817
12818 /* Set sizes, and assign file positions for reloc sections. */
12819 for (o = abfd->sections; o != NULL; o = o->next)
12820 {
12821 struct bfd_elf_section_data *esdo = elf_section_data (o);
12822 if ((o->flags & SEC_RELOC) != 0)
12823 {
12824 if (esdo->rel.hdr
12825 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12826 goto error_return;
12827
12828 if (esdo->rela.hdr
12829 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12830 goto error_return;
12831 }
12832
12833 /* _bfd_elf_compute_section_file_positions makes temporary use
12834 of target_index. Reset it. */
12835 o->target_index = 0;
12836
12837 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12838 to count upwards while actually outputting the relocations. */
12839 esdo->rel.count = 0;
12840 esdo->rela.count = 0;
12841
12842 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12843 && !bfd_section_is_ctf (o))
12844 {
12845 /* Cache the section contents so that they can be compressed
12846 later. Use bfd_malloc since it will be freed by
12847 bfd_compress_section_contents. */
12848 unsigned char *contents = esdo->this_hdr.contents;
12849 if (contents != NULL)
12850 abort ();
12851 contents
12852 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12853 if (contents == NULL)
12854 goto error_return;
12855 esdo->this_hdr.contents = contents;
12856 }
12857 }
12858
12859 /* We have now assigned file positions for all the sections except .symtab,
12860 .strtab, and non-loaded reloc and compressed debugging sections. We start
12861 the .symtab section at the current file position, and write directly to it.
12862 We build the .strtab section in memory. */
12863 abfd->symcount = 0;
12864 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12865 /* sh_name is set in prep_headers. */
12866 symtab_hdr->sh_type = SHT_SYMTAB;
12867 /* sh_flags, sh_addr and sh_size all start off zero. */
12868 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12869 /* sh_link is set in assign_section_numbers. */
12870 /* sh_info is set below. */
12871 /* sh_offset is set just below. */
12872 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12873
12874 if (max_sym_count < 20)
12875 max_sym_count = 20;
12876 htab->strtabsize = max_sym_count;
12877 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12878 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12879 if (htab->strtab == NULL)
12880 goto error_return;
12881 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12882 flinfo.symshndxbuf
12883 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12884 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12885
12886 if (info->strip != strip_all || emit_relocs)
12887 {
12888 file_ptr off = elf_next_file_pos (abfd);
12889
12890 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12891
12892 /* Note that at this point elf_next_file_pos (abfd) is
12893 incorrect. We do not yet know the size of the .symtab section.
12894 We correct next_file_pos below, after we do know the size. */
12895
12896 /* Start writing out the symbol table. The first symbol is always a
12897 dummy symbol. */
12898 elfsym.st_value = 0;
12899 elfsym.st_size = 0;
12900 elfsym.st_info = 0;
12901 elfsym.st_other = 0;
12902 elfsym.st_shndx = SHN_UNDEF;
12903 elfsym.st_target_internal = 0;
12904 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12905 bfd_und_section_ptr, NULL) != 1)
12906 goto error_return;
12907
12908 /* Output a symbol for each section if asked or they are used for
12909 relocs. These symbols usually have no names. We store the
12910 index of each one in the index field of the section, so that
12911 we can find it again when outputting relocs. */
12912
12913 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12914 {
12915 bool name_local_sections
12916 = (bed->elf_backend_name_local_section_symbols
12917 && bed->elf_backend_name_local_section_symbols (abfd));
12918 const char *name = NULL;
12919
12920 elfsym.st_size = 0;
12921 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12922 elfsym.st_other = 0;
12923 elfsym.st_value = 0;
12924 elfsym.st_target_internal = 0;
12925 for (i = 1; i < elf_numsections (abfd); i++)
12926 {
12927 o = bfd_section_from_elf_index (abfd, i);
12928 if (o != NULL)
12929 {
12930 o->target_index = bfd_get_symcount (abfd);
12931 elfsym.st_shndx = i;
12932 if (!bfd_link_relocatable (info))
12933 elfsym.st_value = o->vma;
12934 if (name_local_sections)
12935 name = o->name;
12936 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12937 NULL) != 1)
12938 goto error_return;
12939 }
12940 }
12941 }
12942 }
12943
12944 /* On some targets like Irix 5 the symbol split between local and global
12945 ones recorded in the sh_info field needs to be done between section
12946 and all other symbols. */
12947 if (bed->elf_backend_elfsym_local_is_section
12948 && bed->elf_backend_elfsym_local_is_section (abfd))
12949 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12950
12951 /* Allocate some memory to hold information read in from the input
12952 files. */
12953 if (max_contents_size != 0)
12954 {
12955 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12956 if (flinfo.contents == NULL)
12957 goto error_return;
12958 }
12959
12960 if (max_external_reloc_size != 0)
12961 {
12962 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12963 if (flinfo.external_relocs == NULL)
12964 goto error_return;
12965 }
12966
12967 if (max_internal_reloc_count != 0)
12968 {
12969 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12970 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12971 if (flinfo.internal_relocs == NULL)
12972 goto error_return;
12973 }
12974
12975 if (max_sym_count != 0)
12976 {
12977 amt = max_sym_count * bed->s->sizeof_sym;
12978 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12979 if (flinfo.external_syms == NULL)
12980 goto error_return;
12981
12982 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12983 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12984 if (flinfo.internal_syms == NULL)
12985 goto error_return;
12986
12987 amt = max_sym_count * sizeof (long);
12988 flinfo.indices = (long int *) bfd_malloc (amt);
12989 if (flinfo.indices == NULL)
12990 goto error_return;
12991
12992 amt = max_sym_count * sizeof (asection *);
12993 flinfo.sections = (asection **) bfd_malloc (amt);
12994 if (flinfo.sections == NULL)
12995 goto error_return;
12996 }
12997
12998 if (max_sym_shndx_count != 0)
12999 {
13000 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
13001 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13002 if (flinfo.locsym_shndx == NULL)
13003 goto error_return;
13004 }
13005
13006 if (htab->tls_sec)
13007 {
13008 bfd_vma base, end = 0; /* Both bytes. */
13009 asection *sec;
13010
13011 for (sec = htab->tls_sec;
13012 sec && (sec->flags & SEC_THREAD_LOCAL);
13013 sec = sec->next)
13014 {
13015 bfd_size_type size = sec->size;
13016 unsigned int opb = bfd_octets_per_byte (abfd, sec);
13017
13018 if (size == 0
13019 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13020 {
13021 struct bfd_link_order *ord = sec->map_tail.link_order;
13022
13023 if (ord != NULL)
13024 size = ord->offset * opb + ord->size;
13025 }
13026 end = sec->vma + size / opb;
13027 }
13028 base = htab->tls_sec->vma;
13029 /* Only align end of TLS section if static TLS doesn't have special
13030 alignment requirements. */
13031 if (bed->static_tls_alignment == 1)
13032 end = align_power (end, htab->tls_sec->alignment_power);
13033 htab->tls_size = end - base;
13034 }
13035
13036 if (!_bfd_elf_fixup_eh_frame_hdr (info))
13037 return false;
13038
13039 /* Finish relative relocations here after regular symbol processing
13040 is finished if DT_RELR is enabled. */
13041 if (info->enable_dt_relr
13042 && bed->finish_relative_relocs
13043 && !bed->finish_relative_relocs (info))
13044 info->callbacks->einfo
13045 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
13046
13047 /* Since ELF permits relocations to be against local symbols, we
13048 must have the local symbols available when we do the relocations.
13049 Since we would rather only read the local symbols once, and we
13050 would rather not keep them in memory, we handle all the
13051 relocations for a single input file at the same time.
13052
13053 Unfortunately, there is no way to know the total number of local
13054 symbols until we have seen all of them, and the local symbol
13055 indices precede the global symbol indices. This means that when
13056 we are generating relocatable output, and we see a reloc against
13057 a global symbol, we can not know the symbol index until we have
13058 finished examining all the local symbols to see which ones we are
13059 going to output. To deal with this, we keep the relocations in
13060 memory, and don't output them until the end of the link. This is
13061 an unfortunate waste of memory, but I don't see a good way around
13062 it. Fortunately, it only happens when performing a relocatable
13063 link, which is not the common case. FIXME: If keep_memory is set
13064 we could write the relocs out and then read them again; I don't
13065 know how bad the memory loss will be. */
13066
13067 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13068 sub->output_has_begun = false;
13069 for (o = abfd->sections; o != NULL; o = o->next)
13070 {
13071 for (p = o->map_head.link_order; p != NULL; p = p->next)
13072 {
13073 if (p->type == bfd_indirect_link_order
13074 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13075 == bfd_target_elf_flavour)
13076 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13077 {
13078 if (! sub->output_has_begun)
13079 {
13080 if (! elf_link_input_bfd (&flinfo, sub))
13081 goto error_return;
13082 sub->output_has_begun = true;
13083 }
13084 }
13085 else if (p->type == bfd_section_reloc_link_order
13086 || p->type == bfd_symbol_reloc_link_order)
13087 {
13088 if (! elf_reloc_link_order (abfd, info, o, p))
13089 goto error_return;
13090 }
13091 else
13092 {
13093 if (! _bfd_default_link_order (abfd, info, o, p))
13094 {
13095 if (p->type == bfd_indirect_link_order
13096 && (bfd_get_flavour (sub)
13097 == bfd_target_elf_flavour)
13098 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13099 != bed->s->elfclass))
13100 {
13101 const char *iclass, *oclass;
13102
13103 switch (bed->s->elfclass)
13104 {
13105 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13106 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13107 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13108 default: abort ();
13109 }
13110
13111 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
13112 {
13113 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13114 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13115 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13116 default: abort ();
13117 }
13118
13119 bfd_set_error (bfd_error_wrong_format);
13120 _bfd_error_handler
13121 /* xgettext:c-format */
13122 (_("%pB: file class %s incompatible with %s"),
13123 sub, iclass, oclass);
13124 }
13125
13126 goto error_return;
13127 }
13128 }
13129 }
13130 }
13131
13132 /* Free symbol buffer if needed. */
13133 if (!info->reduce_memory_overheads)
13134 {
13135 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13136 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
13137 {
13138 free (elf_tdata (sub)->symbuf);
13139 elf_tdata (sub)->symbuf = NULL;
13140 }
13141 }
13142
13143 ret = true;
13144
13145 /* Output any global symbols that got converted to local in a
13146 version script or due to symbol visibility. We do this in a
13147 separate step since ELF requires all local symbols to appear
13148 prior to any global symbols. FIXME: We should only do this if
13149 some global symbols were, in fact, converted to become local.
13150 FIXME: Will this work correctly with the Irix 5 linker? */
13151 eoinfo.failed = false;
13152 eoinfo.flinfo = &flinfo;
13153 eoinfo.localsyms = true;
13154 eoinfo.file_sym_done = false;
13155 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13156 if (eoinfo.failed)
13157 {
13158 ret = false;
13159 goto return_local_hash_table;
13160 }
13161
13162 /* If backend needs to output some local symbols not present in the hash
13163 table, do it now. */
13164 if (bed->elf_backend_output_arch_local_syms)
13165 {
13166 if (! ((*bed->elf_backend_output_arch_local_syms)
13167 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13168 {
13169 ret = false;
13170 goto return_local_hash_table;
13171 }
13172 }
13173
13174 /* That wrote out all the local symbols. Finish up the symbol table
13175 with the global symbols. Even if we want to strip everything we
13176 can, we still need to deal with those global symbols that got
13177 converted to local in a version script. */
13178
13179 /* The sh_info field records the index of the first non local symbol. */
13180 if (!symtab_hdr->sh_info)
13181 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13182
13183 if (dynamic
13184 && htab->dynsym != NULL
13185 && htab->dynsym->output_section != bfd_abs_section_ptr)
13186 {
13187 Elf_Internal_Sym sym;
13188 bfd_byte *dynsym = htab->dynsym->contents;
13189
13190 o = htab->dynsym->output_section;
13191 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
13192
13193 /* Write out the section symbols for the output sections. */
13194 if (bfd_link_pic (info)
13195 || htab->is_relocatable_executable)
13196 {
13197 asection *s;
13198
13199 sym.st_size = 0;
13200 sym.st_name = 0;
13201 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13202 sym.st_other = 0;
13203 sym.st_target_internal = 0;
13204
13205 for (s = abfd->sections; s != NULL; s = s->next)
13206 {
13207 int indx;
13208 bfd_byte *dest;
13209 long dynindx;
13210
13211 dynindx = elf_section_data (s)->dynindx;
13212 if (dynindx <= 0)
13213 continue;
13214 indx = elf_section_data (s)->this_idx;
13215 BFD_ASSERT (indx > 0);
13216 sym.st_shndx = indx;
13217 if (! check_dynsym (abfd, &sym))
13218 {
13219 ret = false;
13220 goto return_local_hash_table;
13221 }
13222 sym.st_value = s->vma;
13223 dest = dynsym + dynindx * bed->s->sizeof_sym;
13224
13225 /* Inform the linker of the addition of this symbol. */
13226
13227 if (info->callbacks->ctf_new_dynsym)
13228 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13229
13230 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13231 }
13232 }
13233
13234 /* Write out the local dynsyms. */
13235 if (htab->dynlocal)
13236 {
13237 struct elf_link_local_dynamic_entry *e;
13238 for (e = htab->dynlocal; e ; e = e->next)
13239 {
13240 asection *s;
13241 bfd_byte *dest;
13242
13243 /* Copy the internal symbol and turn off visibility.
13244 Note that we saved a word of storage and overwrote
13245 the original st_name with the dynstr_index. */
13246 sym = e->isym;
13247 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13248 sym.st_shndx = SHN_UNDEF;
13249
13250 s = bfd_section_from_elf_index (e->input_bfd,
13251 e->isym.st_shndx);
13252 if (s != NULL
13253 && s->output_section != NULL
13254 && elf_section_data (s->output_section) != NULL)
13255 {
13256 sym.st_shndx =
13257 elf_section_data (s->output_section)->this_idx;
13258 if (! check_dynsym (abfd, &sym))
13259 {
13260 ret = false;
13261 goto return_local_hash_table;
13262 }
13263 sym.st_value = (s->output_section->vma
13264 + s->output_offset
13265 + e->isym.st_value);
13266 }
13267
13268 /* Inform the linker of the addition of this symbol. */
13269
13270 if (info->callbacks->ctf_new_dynsym)
13271 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13272
13273 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13274 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13275 }
13276 }
13277 }
13278
13279 /* We get the global symbols from the hash table. */
13280 eoinfo.failed = false;
13281 eoinfo.localsyms = false;
13282 eoinfo.flinfo = &flinfo;
13283 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13284 if (eoinfo.failed)
13285 {
13286 ret = false;
13287 goto return_local_hash_table;
13288 }
13289
13290 /* If backend needs to output some symbols not present in the hash
13291 table, do it now. */
13292 if (bed->elf_backend_output_arch_syms
13293 && (info->strip != strip_all || emit_relocs))
13294 {
13295 if (! ((*bed->elf_backend_output_arch_syms)
13296 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13297 {
13298 ret = false;
13299 goto return_local_hash_table;
13300 }
13301 }
13302
13303 /* Finalize the .strtab section. */
13304 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13305
13306 /* Swap out the .strtab section. */
13307 if (!elf_link_swap_symbols_out (&flinfo))
13308 {
13309 ret = false;
13310 goto return_local_hash_table;
13311 }
13312
13313 /* Now we know the size of the symtab section. */
13314 if (bfd_get_symcount (abfd) > 0)
13315 {
13316 /* Finish up and write out the symbol string table (.strtab)
13317 section. */
13318 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13319 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13320
13321 if (elf_symtab_shndx_list (abfd))
13322 {
13323 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13324
13325 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13326 {
13327 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13328 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13329 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13330 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13331 symtab_shndx_hdr->sh_size = amt;
13332
13333 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13334 off, true);
13335
13336 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13337 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13338 {
13339 ret = false;
13340 goto return_local_hash_table;
13341 }
13342 }
13343 }
13344
13345 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13346 /* sh_name was set in prep_headers. */
13347 symstrtab_hdr->sh_type = SHT_STRTAB;
13348 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13349 symstrtab_hdr->sh_addr = 0;
13350 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13351 symstrtab_hdr->sh_entsize = 0;
13352 symstrtab_hdr->sh_link = 0;
13353 symstrtab_hdr->sh_info = 0;
13354 /* sh_offset is set just below. */
13355 symstrtab_hdr->sh_addralign = 1;
13356
13357 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13358 off, true);
13359 elf_next_file_pos (abfd) = off;
13360
13361 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13362 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13363 {
13364 ret = false;
13365 goto return_local_hash_table;
13366 }
13367 }
13368
13369 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13370 {
13371 _bfd_error_handler (_("%pB: failed to generate import library"),
13372 info->out_implib_bfd);
13373 ret = false;
13374 goto return_local_hash_table;
13375 }
13376
13377 /* Adjust the relocs to have the correct symbol indices. */
13378 for (o = abfd->sections; o != NULL; o = o->next)
13379 {
13380 struct bfd_elf_section_data *esdo = elf_section_data (o);
13381 bool sort;
13382
13383 if ((o->flags & SEC_RELOC) == 0)
13384 continue;
13385
13386 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13387 if (esdo->rel.hdr != NULL
13388 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13389 {
13390 ret = false;
13391 goto return_local_hash_table;
13392 }
13393 if (esdo->rela.hdr != NULL
13394 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13395 {
13396 ret = false;
13397 goto return_local_hash_table;
13398 }
13399
13400 /* Set the reloc_count field to 0 to prevent write_relocs from
13401 trying to swap the relocs out itself. */
13402 o->reloc_count = 0;
13403 }
13404
13405 relativecount = 0;
13406 if (dynamic && info->combreloc && dynobj != NULL)
13407 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13408
13409 relr_entsize = 0;
13410 if (htab->srelrdyn != NULL
13411 && htab->srelrdyn->output_section != NULL
13412 && htab->srelrdyn->size != 0)
13413 {
13414 asection *s = htab->srelrdyn->output_section;
13415 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13416 if (relr_entsize == 0)
13417 {
13418 relr_entsize = bed->s->arch_size / 8;
13419 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13420 }
13421 }
13422
13423 /* If we are linking against a dynamic object, or generating a
13424 shared library, finish up the dynamic linking information. */
13425 if (dynamic)
13426 {
13427 bfd_byte *dyncon, *dynconend;
13428
13429 /* Fix up .dynamic entries. */
13430 o = htab->dynamic;
13431 BFD_ASSERT (o != NULL);
13432
13433 dyncon = o->contents;
13434 dynconend = PTR_ADD (o->contents, o->size);
13435 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13436 {
13437 Elf_Internal_Dyn dyn;
13438 const char *name;
13439 unsigned int type;
13440 bfd_size_type sh_size;
13441 bfd_vma sh_addr;
13442
13443 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13444
13445 switch (dyn.d_tag)
13446 {
13447 default:
13448 continue;
13449 case DT_NULL:
13450 if (relativecount != 0)
13451 {
13452 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13453 {
13454 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13455 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13456 }
13457 if (dyn.d_tag != DT_NULL
13458 && dynconend - dyncon >= bed->s->sizeof_dyn)
13459 {
13460 dyn.d_un.d_val = relativecount;
13461 relativecount = 0;
13462 break;
13463 }
13464 relativecount = 0;
13465 }
13466 if (relr_entsize != 0)
13467 {
13468 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13469 {
13470 asection *s = htab->srelrdyn;
13471 dyn.d_tag = DT_RELR;
13472 dyn.d_un.d_ptr
13473 = s->output_section->vma + s->output_offset;
13474 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13475 dyncon += bed->s->sizeof_dyn;
13476
13477 dyn.d_tag = DT_RELRSZ;
13478 dyn.d_un.d_val = s->size;
13479 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13480 dyncon += bed->s->sizeof_dyn;
13481
13482 dyn.d_tag = DT_RELRENT;
13483 dyn.d_un.d_val = relr_entsize;
13484 relr_entsize = 0;
13485 break;
13486 }
13487 relr_entsize = 0;
13488 }
13489 continue;
13490
13491 case DT_INIT:
13492 name = info->init_function;
13493 goto get_sym;
13494 case DT_FINI:
13495 name = info->fini_function;
13496 get_sym:
13497 {
13498 struct elf_link_hash_entry *h;
13499
13500 h = elf_link_hash_lookup (htab, name, false, false, true);
13501 if (h != NULL
13502 && (h->root.type == bfd_link_hash_defined
13503 || h->root.type == bfd_link_hash_defweak))
13504 {
13505 dyn.d_un.d_ptr = h->root.u.def.value;
13506 o = h->root.u.def.section;
13507 if (o->output_section != NULL)
13508 dyn.d_un.d_ptr += (o->output_section->vma
13509 + o->output_offset);
13510 else
13511 {
13512 /* The symbol is imported from another shared
13513 library and does not apply to this one. */
13514 dyn.d_un.d_ptr = 0;
13515 }
13516 break;
13517 }
13518 }
13519 continue;
13520
13521 case DT_PREINIT_ARRAYSZ:
13522 name = ".preinit_array";
13523 goto get_out_size;
13524 case DT_INIT_ARRAYSZ:
13525 name = ".init_array";
13526 goto get_out_size;
13527 case DT_FINI_ARRAYSZ:
13528 name = ".fini_array";
13529 get_out_size:
13530 o = bfd_get_section_by_name (abfd, name);
13531 if (o == NULL)
13532 {
13533 _bfd_error_handler
13534 (_("could not find section %s"), name);
13535 goto error_return;
13536 }
13537 if (o->size == 0)
13538 _bfd_error_handler
13539 (_("warning: %s section has zero size"), name);
13540 dyn.d_un.d_val = o->size;
13541 break;
13542
13543 case DT_PREINIT_ARRAY:
13544 name = ".preinit_array";
13545 goto get_out_vma;
13546 case DT_INIT_ARRAY:
13547 name = ".init_array";
13548 goto get_out_vma;
13549 case DT_FINI_ARRAY:
13550 name = ".fini_array";
13551 get_out_vma:
13552 o = bfd_get_section_by_name (abfd, name);
13553 goto do_vma;
13554
13555 case DT_HASH:
13556 name = ".hash";
13557 goto get_vma;
13558 case DT_GNU_HASH:
13559 name = ".gnu.hash";
13560 goto get_vma;
13561 case DT_STRTAB:
13562 name = ".dynstr";
13563 goto get_vma;
13564 case DT_SYMTAB:
13565 name = ".dynsym";
13566 goto get_vma;
13567 case DT_VERDEF:
13568 name = ".gnu.version_d";
13569 goto get_vma;
13570 case DT_VERNEED:
13571 name = ".gnu.version_r";
13572 goto get_vma;
13573 case DT_VERSYM:
13574 name = ".gnu.version";
13575 get_vma:
13576 o = bfd_get_linker_section (dynobj, name);
13577 do_vma:
13578 if (o == NULL || bfd_is_abs_section (o->output_section))
13579 {
13580 _bfd_error_handler
13581 (_("could not find section %s"), name);
13582 goto error_return;
13583 }
13584 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13585 {
13586 _bfd_error_handler
13587 (_("warning: section '%s' is being made into a note"), name);
13588 bfd_set_error (bfd_error_nonrepresentable_section);
13589 goto error_return;
13590 }
13591 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13592 break;
13593
13594 case DT_REL:
13595 case DT_RELA:
13596 case DT_RELSZ:
13597 case DT_RELASZ:
13598 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13599 type = SHT_REL;
13600 else
13601 type = SHT_RELA;
13602 sh_size = 0;
13603 sh_addr = 0;
13604 for (i = 1; i < elf_numsections (abfd); i++)
13605 {
13606 Elf_Internal_Shdr *hdr;
13607
13608 hdr = elf_elfsections (abfd)[i];
13609 if (hdr->sh_type == type
13610 && (hdr->sh_flags & SHF_ALLOC) != 0)
13611 {
13612 sh_size += hdr->sh_size;
13613 if (sh_addr == 0
13614 || sh_addr > hdr->sh_addr)
13615 sh_addr = hdr->sh_addr;
13616 }
13617 }
13618
13619 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13620 {
13621 unsigned int opb = bfd_octets_per_byte (abfd, o);
13622
13623 /* Don't count procedure linkage table relocs in the
13624 overall reloc count. */
13625 sh_size -= htab->srelplt->size;
13626 if (sh_size == 0)
13627 /* If the size is zero, make the address zero too.
13628 This is to avoid a glibc bug. If the backend
13629 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13630 zero, then we'll put DT_RELA at the end of
13631 DT_JMPREL. glibc will interpret the end of
13632 DT_RELA matching the end of DT_JMPREL as the
13633 case where DT_RELA includes DT_JMPREL, and for
13634 LD_BIND_NOW will decide that processing DT_RELA
13635 will process the PLT relocs too. Net result:
13636 No PLT relocs applied. */
13637 sh_addr = 0;
13638
13639 /* If .rela.plt is the first .rela section, exclude
13640 it from DT_RELA. */
13641 else if (sh_addr == (htab->srelplt->output_section->vma
13642 + htab->srelplt->output_offset) * opb)
13643 sh_addr += htab->srelplt->size;
13644 }
13645
13646 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13647 dyn.d_un.d_val = sh_size;
13648 else
13649 dyn.d_un.d_ptr = sh_addr;
13650 break;
13651 }
13652 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13653 }
13654 }
13655
13656 /* If we have created any dynamic sections, then output them. */
13657 if (dynobj != NULL)
13658 {
13659 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13660 goto error_return;
13661
13662 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13663 if (bfd_link_textrel_check (info)
13664 && (o = htab->dynamic) != NULL
13665 && o->size != 0)
13666 {
13667 bfd_byte *dyncon, *dynconend;
13668
13669 dyncon = o->contents;
13670 dynconend = o->contents + o->size;
13671 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13672 {
13673 Elf_Internal_Dyn dyn;
13674
13675 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13676
13677 if (dyn.d_tag == DT_TEXTREL)
13678 {
13679 if (info->textrel_check == textrel_check_error)
13680 info->callbacks->einfo
13681 (_("%P%X: read-only segment has dynamic relocations\n"));
13682 else if (bfd_link_dll (info))
13683 info->callbacks->einfo
13684 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13685 else if (bfd_link_pde (info))
13686 info->callbacks->einfo
13687 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13688 else
13689 info->callbacks->einfo
13690 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13691 break;
13692 }
13693 }
13694 }
13695
13696 for (o = dynobj->sections; o != NULL; o = o->next)
13697 {
13698 if ((o->flags & SEC_HAS_CONTENTS) == 0
13699 || o->size == 0
13700 || o->output_section == bfd_abs_section_ptr)
13701 continue;
13702 if ((o->flags & SEC_LINKER_CREATED) == 0)
13703 {
13704 /* At this point, we are only interested in sections
13705 created by _bfd_elf_link_create_dynamic_sections. */
13706 continue;
13707 }
13708 if (htab->stab_info.stabstr == o)
13709 continue;
13710 if (htab->eh_info.hdr_sec == o)
13711 continue;
13712 if (strcmp (o->name, ".dynstr") != 0)
13713 {
13714 bfd_size_type octets = ((file_ptr) o->output_offset
13715 * bfd_octets_per_byte (abfd, o));
13716 if (!bfd_set_section_contents (abfd, o->output_section,
13717 o->contents, octets, o->size))
13718 goto error_return;
13719 }
13720 else
13721 {
13722 /* The contents of the .dynstr section are actually in a
13723 stringtab. */
13724 file_ptr off;
13725
13726 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13727 if (bfd_seek (abfd, off, SEEK_SET) != 0
13728 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13729 goto error_return;
13730 }
13731 }
13732 }
13733
13734 if (!info->resolve_section_groups)
13735 {
13736 bool failed = false;
13737
13738 BFD_ASSERT (bfd_link_relocatable (info));
13739 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13740 if (failed)
13741 goto error_return;
13742 }
13743
13744 /* If we have optimized stabs strings, output them. */
13745 if (htab->stab_info.stabstr != NULL)
13746 {
13747 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13748 goto error_return;
13749 }
13750
13751 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13752 goto error_return;
13753
13754 if (! _bfd_elf_write_section_sframe (abfd, info))
13755 goto error_return;
13756
13757 if (info->callbacks->emit_ctf)
13758 info->callbacks->emit_ctf ();
13759
13760 elf_final_link_free (abfd, &flinfo);
13761
13762 if (attr_section)
13763 {
13764 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13765 if (contents == NULL)
13766 {
13767 /* Bail out and fail. */
13768 ret = false;
13769 goto return_local_hash_table;
13770 }
13771 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13772 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13773 free (contents);
13774 }
13775
13776 return_local_hash_table:
13777 if (info->unique_symbol)
13778 bfd_hash_table_free (&flinfo.local_hash_table);
13779 return ret;
13780
13781 error_return:
13782 elf_final_link_free (abfd, &flinfo);
13783 ret = false;
13784 goto return_local_hash_table;
13785 }
13786 \f
13787 /* Initialize COOKIE for input bfd ABFD. */
13788
13789 static bool
13790 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13791 struct bfd_link_info *info, bfd *abfd,
13792 bool keep_memory)
13793 {
13794 Elf_Internal_Shdr *symtab_hdr;
13795 const struct elf_backend_data *bed;
13796
13797 bed = get_elf_backend_data (abfd);
13798 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13799
13800 cookie->abfd = abfd;
13801 cookie->sym_hashes = elf_sym_hashes (abfd);
13802 cookie->bad_symtab = elf_bad_symtab (abfd);
13803 if (cookie->bad_symtab)
13804 {
13805 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13806 cookie->extsymoff = 0;
13807 }
13808 else
13809 {
13810 cookie->locsymcount = symtab_hdr->sh_info;
13811 cookie->extsymoff = symtab_hdr->sh_info;
13812 }
13813
13814 if (bed->s->arch_size == 32)
13815 cookie->r_sym_shift = 8;
13816 else
13817 cookie->r_sym_shift = 32;
13818
13819 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13820 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13821 {
13822 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13823 cookie->locsymcount, 0,
13824 NULL, NULL, NULL);
13825 if (cookie->locsyms == NULL)
13826 {
13827 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13828 return false;
13829 }
13830 if (keep_memory || _bfd_elf_link_keep_memory (info))
13831 {
13832 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13833 info->cache_size += (cookie->locsymcount
13834 * sizeof (Elf_External_Sym_Shndx));
13835 }
13836 }
13837 return true;
13838 }
13839
13840 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13841
13842 static void
13843 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13844 {
13845 Elf_Internal_Shdr *symtab_hdr;
13846
13847 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13848 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13849 free (cookie->locsyms);
13850 }
13851
13852 /* Initialize the relocation information in COOKIE for input section SEC
13853 of input bfd ABFD. */
13854
13855 static bool
13856 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13857 struct bfd_link_info *info, bfd *abfd,
13858 asection *sec, bool keep_memory)
13859 {
13860 if (sec->reloc_count == 0)
13861 {
13862 cookie->rels = NULL;
13863 cookie->relend = NULL;
13864 }
13865 else
13866 {
13867 cookie->rels = _bfd_elf_link_info_read_relocs
13868 (abfd, info, sec, NULL, NULL,
13869 keep_memory || _bfd_elf_link_keep_memory (info));
13870 if (cookie->rels == NULL)
13871 return false;
13872 cookie->rel = cookie->rels;
13873 cookie->relend = cookie->rels + sec->reloc_count;
13874 }
13875 cookie->rel = cookie->rels;
13876 return true;
13877 }
13878
13879 /* Free the memory allocated by init_reloc_cookie_rels,
13880 if appropriate. */
13881
13882 static void
13883 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13884 asection *sec)
13885 {
13886 if (elf_section_data (sec)->relocs != cookie->rels)
13887 free (cookie->rels);
13888 }
13889
13890 /* Initialize the whole of COOKIE for input section SEC. */
13891
13892 static bool
13893 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13894 struct bfd_link_info *info,
13895 asection *sec, bool keep_memory)
13896 {
13897 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
13898 goto error1;
13899 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13900 keep_memory))
13901 goto error2;
13902 return true;
13903
13904 error2:
13905 fini_reloc_cookie (cookie, sec->owner);
13906 error1:
13907 return false;
13908 }
13909
13910 /* Free the memory allocated by init_reloc_cookie_for_section,
13911 if appropriate. */
13912
13913 static void
13914 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13915 asection *sec)
13916 {
13917 fini_reloc_cookie_rels (cookie, sec);
13918 fini_reloc_cookie (cookie, sec->owner);
13919 }
13920 \f
13921 /* Garbage collect unused sections. */
13922
13923 /* Default gc_mark_hook. */
13924
13925 asection *
13926 _bfd_elf_gc_mark_hook (asection *sec,
13927 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13928 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13929 struct elf_link_hash_entry *h,
13930 Elf_Internal_Sym *sym)
13931 {
13932 if (h != NULL)
13933 {
13934 switch (h->root.type)
13935 {
13936 case bfd_link_hash_defined:
13937 case bfd_link_hash_defweak:
13938 return h->root.u.def.section;
13939
13940 case bfd_link_hash_common:
13941 return h->root.u.c.p->section;
13942
13943 default:
13944 break;
13945 }
13946 }
13947 else
13948 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13949
13950 return NULL;
13951 }
13952
13953 /* Return the debug definition section. */
13954
13955 static asection *
13956 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13957 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13958 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13959 struct elf_link_hash_entry *h,
13960 Elf_Internal_Sym *sym)
13961 {
13962 if (h != NULL)
13963 {
13964 /* Return the global debug definition section. */
13965 if ((h->root.type == bfd_link_hash_defined
13966 || h->root.type == bfd_link_hash_defweak)
13967 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13968 return h->root.u.def.section;
13969 }
13970 else
13971 {
13972 /* Return the local debug definition section. */
13973 asection *isec = bfd_section_from_elf_index (sec->owner,
13974 sym->st_shndx);
13975 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13976 return isec;
13977 }
13978
13979 return NULL;
13980 }
13981
13982 /* COOKIE->rel describes a relocation against section SEC, which is
13983 a section we've decided to keep. Return the section that contains
13984 the relocation symbol, or NULL if no section contains it. */
13985
13986 asection *
13987 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13988 elf_gc_mark_hook_fn gc_mark_hook,
13989 struct elf_reloc_cookie *cookie,
13990 bool *start_stop)
13991 {
13992 unsigned long r_symndx;
13993 struct elf_link_hash_entry *h, *hw;
13994
13995 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13996 if (r_symndx == STN_UNDEF)
13997 return NULL;
13998
13999 if (r_symndx >= cookie->locsymcount
14000 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14001 {
14002 bool was_marked;
14003
14004 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
14005 if (h == NULL)
14006 {
14007 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
14008 sec->owner);
14009 return NULL;
14010 }
14011 while (h->root.type == bfd_link_hash_indirect
14012 || h->root.type == bfd_link_hash_warning)
14013 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14014
14015 was_marked = h->mark;
14016 h->mark = 1;
14017 /* Keep all aliases of the symbol too. If an object symbol
14018 needs to be copied into .dynbss then all of its aliases
14019 should be present as dynamic symbols, not just the one used
14020 on the copy relocation. */
14021 hw = h;
14022 while (hw->is_weakalias)
14023 {
14024 hw = hw->u.alias;
14025 hw->mark = 1;
14026 }
14027
14028 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14029 {
14030 if (info->start_stop_gc)
14031 return NULL;
14032
14033 /* To work around a glibc bug, mark XXX input sections
14034 when there is a reference to __start_XXX or __stop_XXX
14035 symbols. */
14036 else if (start_stop != NULL)
14037 {
14038 asection *s = h->u2.start_stop_section;
14039 *start_stop = true;
14040 return s;
14041 }
14042 }
14043
14044 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
14045 }
14046
14047 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14048 &cookie->locsyms[r_symndx]);
14049 }
14050
14051 /* COOKIE->rel describes a relocation against section SEC, which is
14052 a section we've decided to keep. Mark the section that contains
14053 the relocation symbol. */
14054
14055 bool
14056 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14057 asection *sec,
14058 elf_gc_mark_hook_fn gc_mark_hook,
14059 struct elf_reloc_cookie *cookie)
14060 {
14061 asection *rsec;
14062 bool start_stop = false;
14063
14064 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14065 while (rsec != NULL)
14066 {
14067 if (!rsec->gc_mark)
14068 {
14069 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14070 || (rsec->owner->flags & DYNAMIC) != 0)
14071 rsec->gc_mark = 1;
14072 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
14073 return false;
14074 }
14075 if (!start_stop)
14076 break;
14077 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
14078 }
14079 return true;
14080 }
14081
14082 /* The mark phase of garbage collection. For a given section, mark
14083 it and any sections in this section's group, and all the sections
14084 which define symbols to which it refers. */
14085
14086 bool
14087 _bfd_elf_gc_mark (struct bfd_link_info *info,
14088 asection *sec,
14089 elf_gc_mark_hook_fn gc_mark_hook)
14090 {
14091 bool ret;
14092 asection *group_sec, *eh_frame;
14093
14094 sec->gc_mark = 1;
14095
14096 /* Mark all the sections in the group. */
14097 group_sec = elf_section_data (sec)->next_in_group;
14098 if (group_sec && !group_sec->gc_mark)
14099 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
14100 return false;
14101
14102 /* Look through the section relocs. */
14103 ret = true;
14104 eh_frame = elf_eh_frame_section (sec->owner);
14105 if ((sec->flags & SEC_RELOC) != 0
14106 && sec->reloc_count > 0
14107 && sec != eh_frame)
14108 {
14109 struct elf_reloc_cookie cookie;
14110
14111 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
14112 ret = false;
14113 else
14114 {
14115 for (; cookie.rel < cookie.relend; cookie.rel++)
14116 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
14117 {
14118 ret = false;
14119 break;
14120 }
14121 fini_reloc_cookie_for_section (&cookie, sec);
14122 }
14123 }
14124
14125 if (ret && eh_frame && elf_fde_list (sec))
14126 {
14127 struct elf_reloc_cookie cookie;
14128
14129 /* NB: When --no-keep-memory is used, the symbol table and
14130 relocation info for eh_frame are freed after they are retrieved
14131 for each text section in the input object. If an input object
14132 has many text sections, the same data is retrieved and freed
14133 many times which can take a very long time. Always keep the
14134 symbol table and relocation info for eh_frame to avoid it. */
14135 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14136 true))
14137 ret = false;
14138 else
14139 {
14140 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14141 gc_mark_hook, &cookie))
14142 ret = false;
14143 fini_reloc_cookie_for_section (&cookie, eh_frame);
14144 }
14145 }
14146
14147 eh_frame = elf_section_eh_frame_entry (sec);
14148 if (ret && eh_frame && !eh_frame->gc_mark)
14149 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
14150 ret = false;
14151
14152 return ret;
14153 }
14154
14155 /* Scan and mark sections in a special or debug section group. */
14156
14157 static void
14158 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14159 {
14160 /* Point to first section of section group. */
14161 asection *ssec;
14162 /* Used to iterate the section group. */
14163 asection *msec;
14164
14165 bool is_special_grp = true;
14166 bool is_debug_grp = true;
14167
14168 /* First scan to see if group contains any section other than debug
14169 and special section. */
14170 ssec = msec = elf_next_in_group (grp);
14171 do
14172 {
14173 if ((msec->flags & SEC_DEBUGGING) == 0)
14174 is_debug_grp = false;
14175
14176 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
14177 is_special_grp = false;
14178
14179 msec = elf_next_in_group (msec);
14180 }
14181 while (msec != ssec);
14182
14183 /* If this is a pure debug section group or pure special section group,
14184 keep all sections in this group. */
14185 if (is_debug_grp || is_special_grp)
14186 {
14187 do
14188 {
14189 msec->gc_mark = 1;
14190 msec = elf_next_in_group (msec);
14191 }
14192 while (msec != ssec);
14193 }
14194 }
14195
14196 /* Keep debug and special sections. */
14197
14198 bool
14199 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
14200 elf_gc_mark_hook_fn mark_hook)
14201 {
14202 bfd *ibfd;
14203
14204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14205 {
14206 asection *isec;
14207 bool some_kept;
14208 bool debug_frag_seen;
14209 bool has_kept_debug_info;
14210
14211 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14212 continue;
14213 isec = ibfd->sections;
14214 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14215 continue;
14216
14217 /* Ensure all linker created sections are kept,
14218 see if any other section is already marked,
14219 and note if we have any fragmented debug sections. */
14220 debug_frag_seen = some_kept = has_kept_debug_info = false;
14221 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14222 {
14223 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14224 isec->gc_mark = 1;
14225 else if (isec->gc_mark
14226 && (isec->flags & SEC_ALLOC) != 0
14227 && elf_section_type (isec) != SHT_NOTE)
14228 some_kept = true;
14229 else
14230 {
14231 /* Since all sections, except for backend specific ones,
14232 have been garbage collected, call mark_hook on this
14233 section if any of its linked-to sections is marked. */
14234 asection *linked_to_sec;
14235 for (linked_to_sec = elf_linked_to_section (isec);
14236 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14237 linked_to_sec = elf_linked_to_section (linked_to_sec))
14238 {
14239 if (linked_to_sec->gc_mark)
14240 {
14241 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14242 return false;
14243 break;
14244 }
14245 linked_to_sec->linker_mark = 1;
14246 }
14247 for (linked_to_sec = elf_linked_to_section (isec);
14248 linked_to_sec != NULL && linked_to_sec->linker_mark;
14249 linked_to_sec = elf_linked_to_section (linked_to_sec))
14250 linked_to_sec->linker_mark = 0;
14251 }
14252
14253 if (!debug_frag_seen
14254 && (isec->flags & SEC_DEBUGGING)
14255 && startswith (isec->name, ".debug_line."))
14256 debug_frag_seen = true;
14257 else if (strcmp (bfd_section_name (isec),
14258 "__patchable_function_entries") == 0
14259 && elf_linked_to_section (isec) == NULL)
14260 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14261 "need linked-to section "
14262 "for --gc-sections\n"),
14263 isec->owner, isec);
14264 }
14265
14266 /* If no non-note alloc section in this file will be kept, then
14267 we can toss out the debug and special sections. */
14268 if (!some_kept)
14269 continue;
14270
14271 /* Keep debug and special sections like .comment when they are
14272 not part of a group. Also keep section groups that contain
14273 just debug sections or special sections. NB: Sections with
14274 linked-to section has been handled above. */
14275 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14276 {
14277 if ((isec->flags & SEC_GROUP) != 0)
14278 _bfd_elf_gc_mark_debug_special_section_group (isec);
14279 else if (((isec->flags & SEC_DEBUGGING) != 0
14280 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14281 && elf_next_in_group (isec) == NULL
14282 && elf_linked_to_section (isec) == NULL)
14283 isec->gc_mark = 1;
14284 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14285 has_kept_debug_info = true;
14286 }
14287
14288 /* Look for CODE sections which are going to be discarded,
14289 and find and discard any fragmented debug sections which
14290 are associated with that code section. */
14291 if (debug_frag_seen)
14292 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14293 if ((isec->flags & SEC_CODE) != 0
14294 && isec->gc_mark == 0)
14295 {
14296 unsigned int ilen;
14297 asection *dsec;
14298
14299 ilen = strlen (isec->name);
14300
14301 /* Association is determined by the name of the debug
14302 section containing the name of the code section as
14303 a suffix. For example .debug_line.text.foo is a
14304 debug section associated with .text.foo. */
14305 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14306 {
14307 unsigned int dlen;
14308
14309 if (dsec->gc_mark == 0
14310 || (dsec->flags & SEC_DEBUGGING) == 0)
14311 continue;
14312
14313 dlen = strlen (dsec->name);
14314
14315 if (dlen > ilen
14316 && strncmp (dsec->name + (dlen - ilen),
14317 isec->name, ilen) == 0)
14318 dsec->gc_mark = 0;
14319 }
14320 }
14321
14322 /* Mark debug sections referenced by kept debug sections. */
14323 if (has_kept_debug_info)
14324 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14325 if (isec->gc_mark
14326 && (isec->flags & SEC_DEBUGGING) != 0)
14327 if (!_bfd_elf_gc_mark (info, isec,
14328 elf_gc_mark_debug_section))
14329 return false;
14330 }
14331 return true;
14332 }
14333
14334 static bool
14335 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14336 {
14337 bfd *sub;
14338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14339
14340 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14341 {
14342 asection *o;
14343
14344 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14345 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14346 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14347 continue;
14348 o = sub->sections;
14349 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14350 continue;
14351
14352 for (o = sub->sections; o != NULL; o = o->next)
14353 {
14354 /* When any section in a section group is kept, we keep all
14355 sections in the section group. If the first member of
14356 the section group is excluded, we will also exclude the
14357 group section. */
14358 if (o->flags & SEC_GROUP)
14359 {
14360 asection *first = elf_next_in_group (o);
14361 o->gc_mark = first->gc_mark;
14362 }
14363
14364 if (o->gc_mark)
14365 continue;
14366
14367 /* Skip sweeping sections already excluded. */
14368 if (o->flags & SEC_EXCLUDE)
14369 continue;
14370
14371 /* Since this is early in the link process, it is simple
14372 to remove a section from the output. */
14373 o->flags |= SEC_EXCLUDE;
14374
14375 if (info->print_gc_sections && o->size != 0)
14376 /* xgettext:c-format */
14377 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14378 o, sub);
14379 }
14380 }
14381
14382 return true;
14383 }
14384
14385 /* Propagate collected vtable information. This is called through
14386 elf_link_hash_traverse. */
14387
14388 static bool
14389 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14390 {
14391 /* Those that are not vtables. */
14392 if (h->start_stop
14393 || h->u2.vtable == NULL
14394 || h->u2.vtable->parent == NULL)
14395 return true;
14396
14397 /* Those vtables that do not have parents, we cannot merge. */
14398 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14399 return true;
14400
14401 /* If we've already been done, exit. */
14402 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14403 return true;
14404
14405 /* Make sure the parent's table is up to date. */
14406 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14407
14408 if (h->u2.vtable->used == NULL)
14409 {
14410 /* None of this table's entries were referenced. Re-use the
14411 parent's table. */
14412 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14413 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14414 }
14415 else
14416 {
14417 size_t n;
14418 bool *cu, *pu;
14419
14420 /* Or the parent's entries into ours. */
14421 cu = h->u2.vtable->used;
14422 cu[-1] = true;
14423 pu = h->u2.vtable->parent->u2.vtable->used;
14424 if (pu != NULL)
14425 {
14426 const struct elf_backend_data *bed;
14427 unsigned int log_file_align;
14428
14429 bed = get_elf_backend_data (h->root.u.def.section->owner);
14430 log_file_align = bed->s->log_file_align;
14431 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14432 while (n--)
14433 {
14434 if (*pu)
14435 *cu = true;
14436 pu++;
14437 cu++;
14438 }
14439 }
14440 }
14441
14442 return true;
14443 }
14444
14445 struct link_info_ok
14446 {
14447 struct bfd_link_info *info;
14448 bool ok;
14449 };
14450
14451 static bool
14452 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14453 void *ptr)
14454 {
14455 asection *sec;
14456 bfd_vma hstart, hend;
14457 Elf_Internal_Rela *relstart, *relend, *rel;
14458 const struct elf_backend_data *bed;
14459 unsigned int log_file_align;
14460 struct link_info_ok *info = (struct link_info_ok *) ptr;
14461
14462 /* Take care of both those symbols that do not describe vtables as
14463 well as those that are not loaded. */
14464 if (h->start_stop
14465 || h->u2.vtable == NULL
14466 || h->u2.vtable->parent == NULL)
14467 return true;
14468
14469 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14470 || h->root.type == bfd_link_hash_defweak);
14471
14472 sec = h->root.u.def.section;
14473 hstart = h->root.u.def.value;
14474 hend = hstart + h->size;
14475
14476 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14477 sec, NULL, NULL, true);
14478 if (!relstart)
14479 return info->ok = false;
14480 bed = get_elf_backend_data (sec->owner);
14481 log_file_align = bed->s->log_file_align;
14482
14483 relend = relstart + sec->reloc_count;
14484
14485 for (rel = relstart; rel < relend; ++rel)
14486 if (rel->r_offset >= hstart && rel->r_offset < hend)
14487 {
14488 /* If the entry is in use, do nothing. */
14489 if (h->u2.vtable->used
14490 && (rel->r_offset - hstart) < h->u2.vtable->size)
14491 {
14492 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14493 if (h->u2.vtable->used[entry])
14494 continue;
14495 }
14496 /* Otherwise, kill it. */
14497 rel->r_offset = rel->r_info = rel->r_addend = 0;
14498 }
14499
14500 return true;
14501 }
14502
14503 /* Mark sections containing dynamically referenced symbols. When
14504 building shared libraries, we must assume that any visible symbol is
14505 referenced. */
14506
14507 bool
14508 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14509 {
14510 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14511 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14512
14513 if ((h->root.type == bfd_link_hash_defined
14514 || h->root.type == bfd_link_hash_defweak)
14515 && (!h->start_stop
14516 || h->root.ldscript_def
14517 || !info->start_stop_gc)
14518 && ((h->ref_dynamic && !h->forced_local)
14519 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14520 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14521 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14522 && (!bfd_link_executable (info)
14523 || info->gc_keep_exported
14524 || info->export_dynamic
14525 || (h->dynamic
14526 && d != NULL
14527 && (*d->match) (&d->head, NULL, h->root.root.string)))
14528 && (h->versioned >= versioned
14529 || !bfd_hide_sym_by_version (info->version_info,
14530 h->root.root.string)))))
14531 h->root.u.def.section->flags |= SEC_KEEP;
14532
14533 return true;
14534 }
14535
14536 /* Keep all sections containing symbols undefined on the command-line,
14537 and the section containing the entry symbol. */
14538
14539 void
14540 _bfd_elf_gc_keep (struct bfd_link_info *info)
14541 {
14542 struct bfd_sym_chain *sym;
14543
14544 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14545 {
14546 struct elf_link_hash_entry *h;
14547
14548 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14549 false, false, false);
14550
14551 if (h != NULL
14552 && (h->root.type == bfd_link_hash_defined
14553 || h->root.type == bfd_link_hash_defweak)
14554 && !bfd_is_const_section (h->root.u.def.section))
14555 h->root.u.def.section->flags |= SEC_KEEP;
14556 }
14557 }
14558
14559 bool
14560 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14561 struct bfd_link_info *info)
14562 {
14563 bfd *ibfd = info->input_bfds;
14564
14565 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14566 {
14567 asection *sec;
14568 struct elf_reloc_cookie cookie;
14569
14570 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14571 continue;
14572 sec = ibfd->sections;
14573 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14574 continue;
14575
14576 if (!init_reloc_cookie (&cookie, info, ibfd, false))
14577 return false;
14578
14579 for (sec = ibfd->sections; sec; sec = sec->next)
14580 {
14581 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14582 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14583 false))
14584 {
14585 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14586 fini_reloc_cookie_rels (&cookie, sec);
14587 }
14588 }
14589 }
14590 return true;
14591 }
14592
14593 /* Do mark and sweep of unused sections. */
14594
14595 bool
14596 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14597 {
14598 bool ok = true;
14599 bfd *sub;
14600 elf_gc_mark_hook_fn gc_mark_hook;
14601 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14602 struct elf_link_hash_table *htab;
14603 struct link_info_ok info_ok;
14604
14605 if (!bed->can_gc_sections
14606 || !is_elf_hash_table (info->hash))
14607 {
14608 _bfd_error_handler(_("warning: gc-sections option ignored"));
14609 return true;
14610 }
14611
14612 bed->gc_keep (info);
14613 htab = elf_hash_table (info);
14614
14615 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14616 at the .eh_frame section if we can mark the FDEs individually. */
14617 for (sub = info->input_bfds;
14618 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14619 sub = sub->link.next)
14620 {
14621 asection *sec;
14622 struct elf_reloc_cookie cookie;
14623
14624 sec = sub->sections;
14625 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14626 continue;
14627 sec = bfd_get_section_by_name (sub, ".eh_frame");
14628 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14629 false))
14630 {
14631 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14632 if (elf_section_data (sec)->sec_info
14633 && (sec->flags & SEC_LINKER_CREATED) == 0)
14634 elf_eh_frame_section (sub) = sec;
14635 fini_reloc_cookie_for_section (&cookie, sec);
14636 sec = bfd_get_next_section_by_name (NULL, sec);
14637 }
14638 }
14639
14640 /* Apply transitive closure to the vtable entry usage info. */
14641 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14642 if (!ok)
14643 return false;
14644
14645 /* Kill the vtable relocations that were not used. */
14646 info_ok.info = info;
14647 info_ok.ok = true;
14648 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14649 if (!info_ok.ok)
14650 return false;
14651
14652 /* Mark dynamically referenced symbols. */
14653 if (htab->dynamic_sections_created || info->gc_keep_exported)
14654 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14655
14656 /* Grovel through relocs to find out who stays ... */
14657 gc_mark_hook = bed->gc_mark_hook;
14658 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14659 {
14660 asection *o;
14661
14662 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14663 || elf_object_id (sub) != elf_hash_table_id (htab)
14664 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14665 continue;
14666
14667 o = sub->sections;
14668 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14669 continue;
14670
14671 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14672 Also treat note sections as a root, if the section is not part
14673 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14674 well as FINI_ARRAY sections for ld -r. */
14675 for (o = sub->sections; o != NULL; o = o->next)
14676 if (!o->gc_mark
14677 && (o->flags & SEC_EXCLUDE) == 0
14678 && ((o->flags & SEC_KEEP) != 0
14679 || (bfd_link_relocatable (info)
14680 && ((elf_section_data (o)->this_hdr.sh_type
14681 == SHT_PREINIT_ARRAY)
14682 || (elf_section_data (o)->this_hdr.sh_type
14683 == SHT_INIT_ARRAY)
14684 || (elf_section_data (o)->this_hdr.sh_type
14685 == SHT_FINI_ARRAY)))
14686 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14687 && elf_next_in_group (o) == NULL
14688 && elf_linked_to_section (o) == NULL)
14689 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14690 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14691 {
14692 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14693 return false;
14694 }
14695 }
14696
14697 /* Allow the backend to mark additional target specific sections. */
14698 bed->gc_mark_extra_sections (info, gc_mark_hook);
14699
14700 /* ... and mark SEC_EXCLUDE for those that go. */
14701 return elf_gc_sweep (abfd, info);
14702 }
14703 \f
14704 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14705
14706 bool
14707 bfd_elf_gc_record_vtinherit (bfd *abfd,
14708 asection *sec,
14709 struct elf_link_hash_entry *h,
14710 bfd_vma offset)
14711 {
14712 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14713 struct elf_link_hash_entry **search, *child;
14714 size_t extsymcount;
14715 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14716
14717 /* The sh_info field of the symtab header tells us where the
14718 external symbols start. We don't care about the local symbols at
14719 this point. */
14720 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14721 if (!elf_bad_symtab (abfd))
14722 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14723
14724 sym_hashes = elf_sym_hashes (abfd);
14725 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14726
14727 /* Hunt down the child symbol, which is in this section at the same
14728 offset as the relocation. */
14729 for (search = sym_hashes; search != sym_hashes_end; ++search)
14730 {
14731 if ((child = *search) != NULL
14732 && (child->root.type == bfd_link_hash_defined
14733 || child->root.type == bfd_link_hash_defweak)
14734 && child->root.u.def.section == sec
14735 && child->root.u.def.value == offset)
14736 goto win;
14737 }
14738
14739 /* xgettext:c-format */
14740 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14741 abfd, sec, (uint64_t) offset);
14742 bfd_set_error (bfd_error_invalid_operation);
14743 return false;
14744
14745 win:
14746 if (!child->u2.vtable)
14747 {
14748 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14749 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14750 if (!child->u2.vtable)
14751 return false;
14752 }
14753 if (!h)
14754 {
14755 /* This *should* only be the absolute section. It could potentially
14756 be that someone has defined a non-global vtable though, which
14757 would be bad. It isn't worth paging in the local symbols to be
14758 sure though; that case should simply be handled by the assembler. */
14759
14760 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14761 }
14762 else
14763 child->u2.vtable->parent = h;
14764
14765 return true;
14766 }
14767
14768 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14769
14770 bool
14771 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14772 struct elf_link_hash_entry *h,
14773 bfd_vma addend)
14774 {
14775 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14776 unsigned int log_file_align = bed->s->log_file_align;
14777
14778 if (!h)
14779 {
14780 /* xgettext:c-format */
14781 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14782 abfd, sec);
14783 bfd_set_error (bfd_error_bad_value);
14784 return false;
14785 }
14786
14787 if (!h->u2.vtable)
14788 {
14789 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14790 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14791 if (!h->u2.vtable)
14792 return false;
14793 }
14794
14795 if (addend >= h->u2.vtable->size)
14796 {
14797 size_t size, bytes, file_align;
14798 bool *ptr = h->u2.vtable->used;
14799
14800 /* While the symbol is undefined, we have to be prepared to handle
14801 a zero size. */
14802 file_align = 1 << log_file_align;
14803 if (h->root.type == bfd_link_hash_undefined)
14804 size = addend + file_align;
14805 else
14806 {
14807 size = h->size;
14808 if (addend >= size)
14809 {
14810 /* Oops! We've got a reference past the defined end of
14811 the table. This is probably a bug -- shall we warn? */
14812 size = addend + file_align;
14813 }
14814 }
14815 size = (size + file_align - 1) & -file_align;
14816
14817 /* Allocate one extra entry for use as a "done" flag for the
14818 consolidation pass. */
14819 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14820
14821 if (ptr)
14822 {
14823 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14824
14825 if (ptr != NULL)
14826 {
14827 size_t oldbytes;
14828
14829 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14830 * sizeof (bool));
14831 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14832 }
14833 }
14834 else
14835 ptr = (bool *) bfd_zmalloc (bytes);
14836
14837 if (ptr == NULL)
14838 return false;
14839
14840 /* And arrange for that done flag to be at index -1. */
14841 h->u2.vtable->used = ptr + 1;
14842 h->u2.vtable->size = size;
14843 }
14844
14845 h->u2.vtable->used[addend >> log_file_align] = true;
14846
14847 return true;
14848 }
14849
14850 /* Map an ELF section header flag to its corresponding string. */
14851 typedef struct
14852 {
14853 char *flag_name;
14854 flagword flag_value;
14855 } elf_flags_to_name_table;
14856
14857 static const elf_flags_to_name_table elf_flags_to_names [] =
14858 {
14859 { "SHF_WRITE", SHF_WRITE },
14860 { "SHF_ALLOC", SHF_ALLOC },
14861 { "SHF_EXECINSTR", SHF_EXECINSTR },
14862 { "SHF_MERGE", SHF_MERGE },
14863 { "SHF_STRINGS", SHF_STRINGS },
14864 { "SHF_INFO_LINK", SHF_INFO_LINK},
14865 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14866 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14867 { "SHF_GROUP", SHF_GROUP },
14868 { "SHF_TLS", SHF_TLS },
14869 { "SHF_MASKOS", SHF_MASKOS },
14870 { "SHF_EXCLUDE", SHF_EXCLUDE },
14871 };
14872
14873 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14874 bool
14875 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14876 struct flag_info *flaginfo,
14877 asection *section)
14878 {
14879 const bfd_vma sh_flags = elf_section_flags (section);
14880
14881 if (!flaginfo->flags_initialized)
14882 {
14883 bfd *obfd = info->output_bfd;
14884 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14885 struct flag_info_list *tf = flaginfo->flag_list;
14886 int with_hex = 0;
14887 int without_hex = 0;
14888
14889 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14890 {
14891 unsigned i;
14892 flagword (*lookup) (char *);
14893
14894 lookup = bed->elf_backend_lookup_section_flags_hook;
14895 if (lookup != NULL)
14896 {
14897 flagword hexval = (*lookup) ((char *) tf->name);
14898
14899 if (hexval != 0)
14900 {
14901 if (tf->with == with_flags)
14902 with_hex |= hexval;
14903 else if (tf->with == without_flags)
14904 without_hex |= hexval;
14905 tf->valid = true;
14906 continue;
14907 }
14908 }
14909 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14910 {
14911 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14912 {
14913 if (tf->with == with_flags)
14914 with_hex |= elf_flags_to_names[i].flag_value;
14915 else if (tf->with == without_flags)
14916 without_hex |= elf_flags_to_names[i].flag_value;
14917 tf->valid = true;
14918 break;
14919 }
14920 }
14921 if (!tf->valid)
14922 {
14923 info->callbacks->einfo
14924 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14925 return false;
14926 }
14927 }
14928 flaginfo->flags_initialized = true;
14929 flaginfo->only_with_flags |= with_hex;
14930 flaginfo->not_with_flags |= without_hex;
14931 }
14932
14933 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14934 return false;
14935
14936 if ((flaginfo->not_with_flags & sh_flags) != 0)
14937 return false;
14938
14939 return true;
14940 }
14941
14942 struct alloc_got_off_arg {
14943 bfd_vma gotoff;
14944 struct bfd_link_info *info;
14945 };
14946
14947 /* We need a special top-level link routine to convert got reference counts
14948 to real got offsets. */
14949
14950 static bool
14951 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14952 {
14953 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14954 bfd *obfd = gofarg->info->output_bfd;
14955 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14956
14957 if (h->got.refcount > 0)
14958 {
14959 h->got.offset = gofarg->gotoff;
14960 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14961 }
14962 else
14963 h->got.offset = (bfd_vma) -1;
14964
14965 return true;
14966 }
14967
14968 /* And an accompanying bit to work out final got entry offsets once
14969 we're done. Should be called from final_link. */
14970
14971 bool
14972 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14973 struct bfd_link_info *info)
14974 {
14975 bfd *i;
14976 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14977 bfd_vma gotoff;
14978 struct alloc_got_off_arg gofarg;
14979
14980 BFD_ASSERT (abfd == info->output_bfd);
14981
14982 if (! is_elf_hash_table (info->hash))
14983 return false;
14984
14985 /* The GOT offset is relative to the .got section, but the GOT header is
14986 put into the .got.plt section, if the backend uses it. */
14987 if (bed->want_got_plt)
14988 gotoff = 0;
14989 else
14990 gotoff = bed->got_header_size;
14991
14992 /* Do the local .got entries first. */
14993 for (i = info->input_bfds; i; i = i->link.next)
14994 {
14995 bfd_signed_vma *local_got;
14996 size_t j, locsymcount;
14997 Elf_Internal_Shdr *symtab_hdr;
14998
14999 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15000 continue;
15001
15002 local_got = elf_local_got_refcounts (i);
15003 if (!local_got)
15004 continue;
15005
15006 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15007 if (elf_bad_symtab (i))
15008 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15009 else
15010 locsymcount = symtab_hdr->sh_info;
15011
15012 for (j = 0; j < locsymcount; ++j)
15013 {
15014 if (local_got[j] > 0)
15015 {
15016 local_got[j] = gotoff;
15017 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
15018 }
15019 else
15020 local_got[j] = (bfd_vma) -1;
15021 }
15022 }
15023
15024 /* Then the global .got entries. .plt refcounts are handled by
15025 adjust_dynamic_symbol */
15026 gofarg.gotoff = gotoff;
15027 gofarg.info = info;
15028 elf_link_hash_traverse (elf_hash_table (info),
15029 elf_gc_allocate_got_offsets,
15030 &gofarg);
15031 return true;
15032 }
15033
15034 /* Many folk need no more in the way of final link than this, once
15035 got entry reference counting is enabled. */
15036
15037 bool
15038 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15039 {
15040 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
15041 return false;
15042
15043 /* Invoke the regular ELF backend linker to do all the work. */
15044 return bfd_elf_final_link (abfd, info);
15045 }
15046
15047 bool
15048 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15049 {
15050 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
15051
15052 if (rcookie->bad_symtab)
15053 rcookie->rel = rcookie->rels;
15054
15055 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15056 {
15057 unsigned long r_symndx;
15058
15059 if (! rcookie->bad_symtab)
15060 if (rcookie->rel->r_offset > offset)
15061 return false;
15062 if (rcookie->rel->r_offset != offset)
15063 continue;
15064
15065 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
15066 if (r_symndx == STN_UNDEF)
15067 return true;
15068
15069 if (r_symndx >= rcookie->locsymcount
15070 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
15071 {
15072 struct elf_link_hash_entry *h;
15073
15074 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
15075
15076 while (h->root.type == bfd_link_hash_indirect
15077 || h->root.type == bfd_link_hash_warning)
15078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
15079
15080 if ((h->root.type == bfd_link_hash_defined
15081 || h->root.type == bfd_link_hash_defweak)
15082 && (h->root.u.def.section->owner != rcookie->abfd
15083 || h->root.u.def.section->kept_section != NULL
15084 || discarded_section (h->root.u.def.section)))
15085 return true;
15086 }
15087 else
15088 {
15089 /* It's not a relocation against a global symbol,
15090 but it could be a relocation against a local
15091 symbol for a discarded section. */
15092 asection *isec;
15093 Elf_Internal_Sym *isym;
15094
15095 /* Need to: get the symbol; get the section. */
15096 isym = &rcookie->locsyms[r_symndx];
15097 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
15098 if (isec != NULL
15099 && (isec->kept_section != NULL
15100 || discarded_section (isec)))
15101 return true;
15102 }
15103 return false;
15104 }
15105 return false;
15106 }
15107
15108 /* Discard unneeded references to discarded sections.
15109 Returns -1 on error, 1 if any section's size was changed, 0 if
15110 nothing changed. This function assumes that the relocations are in
15111 sorted order, which is true for all known assemblers. */
15112
15113 int
15114 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15115 {
15116 struct elf_reloc_cookie cookie;
15117 asection *o;
15118 bfd *abfd;
15119 int changed = 0;
15120
15121 if (info->traditional_format
15122 || !is_elf_hash_table (info->hash))
15123 return 0;
15124
15125 o = bfd_get_section_by_name (output_bfd, ".stab");
15126 if (o != NULL)
15127 {
15128 asection *i;
15129
15130 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15131 {
15132 if (i->size == 0
15133 || i->reloc_count == 0
15134 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15135 continue;
15136
15137 abfd = i->owner;
15138 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15139 continue;
15140
15141 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15142 return -1;
15143
15144 if (_bfd_discard_section_stabs (abfd, i,
15145 elf_section_data (i)->sec_info,
15146 bfd_elf_reloc_symbol_deleted_p,
15147 &cookie))
15148 changed = 1;
15149
15150 fini_reloc_cookie_for_section (&cookie, i);
15151 }
15152 }
15153
15154 o = NULL;
15155 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15156 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
15157 if (o != NULL)
15158 {
15159 asection *i;
15160 int eh_changed = 0;
15161 unsigned int eh_alignment; /* Octets. */
15162
15163 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15164 {
15165 if (i->size == 0)
15166 continue;
15167
15168 abfd = i->owner;
15169 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15170 continue;
15171
15172 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15173 return -1;
15174
15175 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15176 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
15177 bfd_elf_reloc_symbol_deleted_p,
15178 &cookie))
15179 {
15180 eh_changed = 1;
15181 if (i->size != i->rawsize)
15182 changed = 1;
15183 }
15184
15185 fini_reloc_cookie_for_section (&cookie, i);
15186 }
15187
15188 eh_alignment = ((1 << o->alignment_power)
15189 * bfd_octets_per_byte (output_bfd, o));
15190 /* Skip over zero terminator, and prevent empty sections from
15191 adding alignment padding at the end. */
15192 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15193 if (i->size == 0)
15194 i->flags |= SEC_EXCLUDE;
15195 else if (i->size > 4)
15196 break;
15197 /* The last non-empty eh_frame section doesn't need padding. */
15198 if (i != NULL)
15199 i = i->map_tail.s;
15200 /* Any prior sections must pad the last FDE out to the output
15201 section alignment. Otherwise we might have zero padding
15202 between sections, which would be seen as a terminator. */
15203 for (; i != NULL; i = i->map_tail.s)
15204 if (i->size == 4)
15205 /* All but the last zero terminator should have been removed. */
15206 BFD_FAIL ();
15207 else
15208 {
15209 bfd_size_type size
15210 = (i->size + eh_alignment - 1) & -eh_alignment;
15211 if (i->size != size)
15212 {
15213 i->size = size;
15214 changed = 1;
15215 eh_changed = 1;
15216 }
15217 }
15218 if (eh_changed)
15219 elf_link_hash_traverse (elf_hash_table (info),
15220 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15221 }
15222
15223 o = bfd_get_section_by_name (output_bfd, ".sframe");
15224 if (o != NULL)
15225 {
15226 asection *i;
15227
15228 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15229 {
15230 if (i->size == 0)
15231 continue;
15232
15233 abfd = i->owner;
15234 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15235 continue;
15236
15237 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
15238 return -1;
15239
15240 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15241 {
15242 if (_bfd_elf_discard_section_sframe (i,
15243 bfd_elf_reloc_symbol_deleted_p,
15244 &cookie))
15245 {
15246 if (i->size != i->rawsize)
15247 changed = 1;
15248 }
15249 }
15250 fini_reloc_cookie_for_section (&cookie, i);
15251 }
15252 /* Update the reference to the output .sframe section. Used to
15253 determine later if PT_GNU_SFRAME segment is to be generated. */
15254 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15255 return -1;
15256 }
15257
15258 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15259 {
15260 const struct elf_backend_data *bed;
15261 asection *s;
15262
15263 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15264 continue;
15265 s = abfd->sections;
15266 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15267 continue;
15268
15269 bed = get_elf_backend_data (abfd);
15270
15271 if (bed->elf_backend_discard_info != NULL)
15272 {
15273 if (!init_reloc_cookie (&cookie, info, abfd, false))
15274 return -1;
15275
15276 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15277 changed = 1;
15278
15279 fini_reloc_cookie (&cookie, abfd);
15280 }
15281 }
15282
15283 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15284 _bfd_elf_end_eh_frame_parsing (info);
15285
15286 if (info->eh_frame_hdr_type
15287 && !bfd_link_relocatable (info)
15288 && _bfd_elf_discard_section_eh_frame_hdr (info))
15289 changed = 1;
15290
15291 return changed;
15292 }
15293
15294 bool
15295 _bfd_elf_section_already_linked (bfd *abfd,
15296 asection *sec,
15297 struct bfd_link_info *info)
15298 {
15299 flagword flags;
15300 const char *name, *key;
15301 struct bfd_section_already_linked *l;
15302 struct bfd_section_already_linked_hash_entry *already_linked_list;
15303
15304 if (sec->output_section == bfd_abs_section_ptr)
15305 return false;
15306
15307 flags = sec->flags;
15308
15309 /* Return if it isn't a linkonce section. A comdat group section
15310 also has SEC_LINK_ONCE set. */
15311 if ((flags & SEC_LINK_ONCE) == 0)
15312 return false;
15313
15314 /* Don't put group member sections on our list of already linked
15315 sections. They are handled as a group via their group section. */
15316 if (elf_sec_group (sec) != NULL)
15317 return false;
15318
15319 /* For a SHT_GROUP section, use the group signature as the key. */
15320 name = sec->name;
15321 if ((flags & SEC_GROUP) != 0
15322 && elf_next_in_group (sec) != NULL
15323 && elf_group_name (elf_next_in_group (sec)) != NULL)
15324 key = elf_group_name (elf_next_in_group (sec));
15325 else
15326 {
15327 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15328 if (startswith (name, ".gnu.linkonce.")
15329 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15330 key++;
15331 else
15332 /* Must be a user linkonce section that doesn't follow gcc's
15333 naming convention. In this case we won't be matching
15334 single member groups. */
15335 key = name;
15336 }
15337
15338 already_linked_list = bfd_section_already_linked_table_lookup (key);
15339
15340 for (l = already_linked_list->entry; l != NULL; l = l->next)
15341 {
15342 /* We may have 2 different types of sections on the list: group
15343 sections with a signature of <key> (<key> is some string),
15344 and linkonce sections named .gnu.linkonce.<type>.<key>.
15345 Match like sections. LTO plugin sections are an exception.
15346 They are always named .gnu.linkonce.t.<key> and match either
15347 type of section. */
15348 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15349 && ((flags & SEC_GROUP) != 0
15350 || strcmp (name, l->sec->name) == 0))
15351 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15352 || (sec->owner->flags & BFD_PLUGIN) != 0)
15353 {
15354 /* The section has already been linked. See if we should
15355 issue a warning. */
15356 if (!_bfd_handle_already_linked (sec, l, info))
15357 return false;
15358
15359 if (flags & SEC_GROUP)
15360 {
15361 asection *first = elf_next_in_group (sec);
15362 asection *s = first;
15363
15364 while (s != NULL)
15365 {
15366 s->output_section = bfd_abs_section_ptr;
15367 /* Record which group discards it. */
15368 s->kept_section = l->sec;
15369 s = elf_next_in_group (s);
15370 /* These lists are circular. */
15371 if (s == first)
15372 break;
15373 }
15374 }
15375
15376 return true;
15377 }
15378 }
15379
15380 /* A single member comdat group section may be discarded by a
15381 linkonce section and vice versa. */
15382 if ((flags & SEC_GROUP) != 0)
15383 {
15384 asection *first = elf_next_in_group (sec);
15385
15386 if (first != NULL && elf_next_in_group (first) == first)
15387 /* Check this single member group against linkonce sections. */
15388 for (l = already_linked_list->entry; l != NULL; l = l->next)
15389 if ((l->sec->flags & SEC_GROUP) == 0
15390 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15391 {
15392 first->output_section = bfd_abs_section_ptr;
15393 first->kept_section = l->sec;
15394 sec->output_section = bfd_abs_section_ptr;
15395 break;
15396 }
15397 }
15398 else
15399 /* Check this linkonce section against single member groups. */
15400 for (l = already_linked_list->entry; l != NULL; l = l->next)
15401 if (l->sec->flags & SEC_GROUP)
15402 {
15403 asection *first = elf_next_in_group (l->sec);
15404
15405 if (first != NULL
15406 && elf_next_in_group (first) == first
15407 && bfd_elf_match_symbols_in_sections (first, sec, info))
15408 {
15409 sec->output_section = bfd_abs_section_ptr;
15410 sec->kept_section = first;
15411 break;
15412 }
15413 }
15414
15415 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15416 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15417 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15418 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15419 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15420 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15421 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15422 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15423 The reverse order cannot happen as there is never a bfd with only the
15424 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15425 matter as here were are looking only for cross-bfd sections. */
15426
15427 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15428 for (l = already_linked_list->entry; l != NULL; l = l->next)
15429 if ((l->sec->flags & SEC_GROUP) == 0
15430 && startswith (l->sec->name, ".gnu.linkonce.t."))
15431 {
15432 if (abfd != l->sec->owner)
15433 sec->output_section = bfd_abs_section_ptr;
15434 break;
15435 }
15436
15437 /* This is the first section with this name. Record it. */
15438 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15439 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15440 return sec->output_section == bfd_abs_section_ptr;
15441 }
15442
15443 bool
15444 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15445 {
15446 return sym->st_shndx == SHN_COMMON;
15447 }
15448
15449 unsigned int
15450 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15451 {
15452 return SHN_COMMON;
15453 }
15454
15455 asection *
15456 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15457 {
15458 return bfd_com_section_ptr;
15459 }
15460
15461 bfd_vma
15462 _bfd_elf_default_got_elt_size (bfd *abfd,
15463 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15464 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15465 bfd *ibfd ATTRIBUTE_UNUSED,
15466 unsigned long symndx ATTRIBUTE_UNUSED)
15467 {
15468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15469 return bed->s->arch_size / 8;
15470 }
15471
15472 /* Routines to support the creation of dynamic relocs. */
15473
15474 /* Returns the name of the dynamic reloc section associated with SEC. */
15475
15476 static const char *
15477 get_dynamic_reloc_section_name (bfd * abfd,
15478 asection * sec,
15479 bool is_rela)
15480 {
15481 char *name;
15482 const char *old_name = bfd_section_name (sec);
15483 const char *prefix = is_rela ? ".rela" : ".rel";
15484
15485 if (old_name == NULL)
15486 return NULL;
15487
15488 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15489 sprintf (name, "%s%s", prefix, old_name);
15490
15491 return name;
15492 }
15493
15494 /* Returns the dynamic reloc section associated with SEC.
15495 If necessary compute the name of the dynamic reloc section based
15496 on SEC's name (looked up in ABFD's string table) and the setting
15497 of IS_RELA. */
15498
15499 asection *
15500 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15501 asection *sec,
15502 bool is_rela)
15503 {
15504 asection *reloc_sec = elf_section_data (sec)->sreloc;
15505
15506 if (reloc_sec == NULL)
15507 {
15508 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15509
15510 if (name != NULL)
15511 {
15512 reloc_sec = bfd_get_linker_section (abfd, name);
15513
15514 if (reloc_sec != NULL)
15515 elf_section_data (sec)->sreloc = reloc_sec;
15516 }
15517 }
15518
15519 return reloc_sec;
15520 }
15521
15522 /* Returns the dynamic reloc section associated with SEC. If the
15523 section does not exist it is created and attached to the DYNOBJ
15524 bfd and stored in the SRELOC field of SEC's elf_section_data
15525 structure.
15526
15527 ALIGNMENT is the alignment for the newly created section and
15528 IS_RELA defines whether the name should be .rela.<SEC's name>
15529 or .rel.<SEC's name>. The section name is looked up in the
15530 string table associated with ABFD. */
15531
15532 asection *
15533 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15534 bfd *dynobj,
15535 unsigned int alignment,
15536 bfd *abfd,
15537 bool is_rela)
15538 {
15539 asection * reloc_sec = elf_section_data (sec)->sreloc;
15540
15541 if (reloc_sec == NULL)
15542 {
15543 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15544
15545 if (name == NULL)
15546 return NULL;
15547
15548 reloc_sec = bfd_get_linker_section (dynobj, name);
15549
15550 if (reloc_sec == NULL)
15551 {
15552 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15553 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15554 if ((sec->flags & SEC_ALLOC) != 0)
15555 flags |= SEC_ALLOC | SEC_LOAD;
15556
15557 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15558 if (reloc_sec != NULL)
15559 {
15560 /* _bfd_elf_get_sec_type_attr chooses a section type by
15561 name. Override as it may be wrong, eg. for a user
15562 section named "auto" we'll get ".relauto" which is
15563 seen to be a .rela section. */
15564 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15565 if (!bfd_set_section_alignment (reloc_sec, alignment))
15566 reloc_sec = NULL;
15567 }
15568 }
15569
15570 elf_section_data (sec)->sreloc = reloc_sec;
15571 }
15572
15573 return reloc_sec;
15574 }
15575
15576 /* Copy the ELF symbol type and other attributes for a linker script
15577 assignment from HSRC to HDEST. Generally this should be treated as
15578 if we found a strong non-dynamic definition for HDEST (except that
15579 ld ignores multiple definition errors). */
15580 void
15581 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15582 struct bfd_link_hash_entry *hdest,
15583 struct bfd_link_hash_entry *hsrc)
15584 {
15585 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15586 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15587 Elf_Internal_Sym isym;
15588
15589 ehdest->type = ehsrc->type;
15590 ehdest->target_internal = ehsrc->target_internal;
15591
15592 isym.st_other = ehsrc->other;
15593 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15594 }
15595
15596 /* Append a RELA relocation REL to section S in BFD. */
15597
15598 void
15599 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15600 {
15601 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15602 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15603 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15604 bed->s->swap_reloca_out (abfd, rel, loc);
15605 }
15606
15607 /* Append a REL relocation REL to section S in BFD. */
15608
15609 void
15610 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15611 {
15612 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15613 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15614 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15615 bed->s->swap_reloc_out (abfd, rel, loc);
15616 }
15617
15618 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15619
15620 struct bfd_link_hash_entry *
15621 bfd_elf_define_start_stop (struct bfd_link_info *info,
15622 const char *symbol, asection *sec)
15623 {
15624 struct elf_link_hash_entry *h;
15625
15626 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15627 false, false, true);
15628 /* NB: Common symbols will be turned into definition later. */
15629 if (h != NULL
15630 && !h->root.ldscript_def
15631 && (h->root.type == bfd_link_hash_undefined
15632 || h->root.type == bfd_link_hash_undefweak
15633 || ((h->ref_regular || h->def_dynamic)
15634 && !h->def_regular
15635 && h->root.type != bfd_link_hash_common)))
15636 {
15637 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15638 h->verinfo.verdef = NULL;
15639 h->root.type = bfd_link_hash_defined;
15640 h->root.u.def.section = sec;
15641 h->root.u.def.value = 0;
15642 h->def_regular = 1;
15643 h->def_dynamic = 0;
15644 h->start_stop = 1;
15645 h->u2.start_stop_section = sec;
15646 if (symbol[0] == '.')
15647 {
15648 /* .startof. and .sizeof. symbols are local. */
15649 const struct elf_backend_data *bed;
15650 bed = get_elf_backend_data (info->output_bfd);
15651 (*bed->elf_backend_hide_symbol) (info, h, true);
15652 }
15653 else
15654 {
15655 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15656 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15657 | info->start_stop_visibility);
15658 if (was_dynamic)
15659 bfd_elf_link_record_dynamic_symbol (info, h);
15660 }
15661 return &h->root;
15662 }
15663 return NULL;
15664 }
15665
15666 /* Find dynamic relocs for H that apply to read-only sections. */
15667
15668 asection *
15669 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15670 {
15671 struct elf_dyn_relocs *p;
15672
15673 for (p = h->dyn_relocs; p != NULL; p = p->next)
15674 {
15675 asection *s = p->sec->output_section;
15676
15677 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15678 return p->sec;
15679 }
15680 return NULL;
15681 }
15682
15683 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15684 read-only sections. */
15685
15686 bool
15687 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15688 {
15689 asection *sec;
15690
15691 if (h->root.type == bfd_link_hash_indirect)
15692 return true;
15693
15694 sec = _bfd_elf_readonly_dynrelocs (h);
15695 if (sec != NULL)
15696 {
15697 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15698
15699 info->flags |= DF_TEXTREL;
15700 /* xgettext:c-format */
15701 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15702 "in read-only section `%pA'\n"),
15703 sec->owner, h->root.root.string, sec);
15704
15705 if (bfd_link_textrel_check (info))
15706 /* xgettext:c-format */
15707 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15708 "in read-only section `%pA'\n"),
15709 sec->owner, h->root.root.string, sec);
15710
15711 /* Not an error, just cut short the traversal. */
15712 return false;
15713 }
15714 return true;
15715 }
15716
15717 /* Add dynamic tags. */
15718
15719 bool
15720 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15721 bool need_dynamic_reloc)
15722 {
15723 struct elf_link_hash_table *htab = elf_hash_table (info);
15724
15725 if (htab->dynamic_sections_created)
15726 {
15727 /* Add some entries to the .dynamic section. We fill in the
15728 values later, in finish_dynamic_sections, but we must add
15729 the entries now so that we get the correct size for the
15730 .dynamic section. The DT_DEBUG entry is filled in by the
15731 dynamic linker and used by the debugger. */
15732 #define add_dynamic_entry(TAG, VAL) \
15733 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15734
15735 const struct elf_backend_data *bed
15736 = get_elf_backend_data (output_bfd);
15737
15738 if (bfd_link_executable (info))
15739 {
15740 if (!add_dynamic_entry (DT_DEBUG, 0))
15741 return false;
15742 }
15743
15744 if (htab->dt_pltgot_required || htab->splt->size != 0)
15745 {
15746 /* DT_PLTGOT is used by prelink even if there is no PLT
15747 relocation. */
15748 if (!add_dynamic_entry (DT_PLTGOT, 0))
15749 return false;
15750 }
15751
15752 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15753 {
15754 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15755 || !add_dynamic_entry (DT_PLTREL,
15756 (bed->rela_plts_and_copies_p
15757 ? DT_RELA : DT_REL))
15758 || !add_dynamic_entry (DT_JMPREL, 0))
15759 return false;
15760 }
15761
15762 if (htab->tlsdesc_plt
15763 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15764 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15765 return false;
15766
15767 if (need_dynamic_reloc)
15768 {
15769 if (bed->rela_plts_and_copies_p)
15770 {
15771 if (!add_dynamic_entry (DT_RELA, 0)
15772 || !add_dynamic_entry (DT_RELASZ, 0)
15773 || !add_dynamic_entry (DT_RELAENT,
15774 bed->s->sizeof_rela))
15775 return false;
15776 }
15777 else
15778 {
15779 if (!add_dynamic_entry (DT_REL, 0)
15780 || !add_dynamic_entry (DT_RELSZ, 0)
15781 || !add_dynamic_entry (DT_RELENT,
15782 bed->s->sizeof_rel))
15783 return false;
15784 }
15785
15786 /* If any dynamic relocs apply to a read-only section,
15787 then we need a DT_TEXTREL entry. */
15788 if ((info->flags & DF_TEXTREL) == 0)
15789 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15790 info);
15791
15792 if ((info->flags & DF_TEXTREL) != 0)
15793 {
15794 if (htab->ifunc_resolvers)
15795 info->callbacks->einfo
15796 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15797 "may result in a segfault at runtime; recompile with %s\n"),
15798 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15799
15800 if (!add_dynamic_entry (DT_TEXTREL, 0))
15801 return false;
15802 }
15803 }
15804 }
15805 #undef add_dynamic_entry
15806
15807 return true;
15808 }