arm: Add support for Cortex-A78C
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 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 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #ifndef CHAR_BIT
39 #define CHAR_BIT 8
40 #endif
41
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45 struct elf_info_failed
46 {
47 struct bfd_link_info *info;
48 bfd_boolean failed;
49 };
50
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54 struct elf_find_verdep_info
55 {
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62 };
63
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
67 asection *
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71 {
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
86 return h->root.u.def.section;
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106 }
107
108 /* Define a symbol in a dynamic linkage section. */
109
110 struct elf_link_hash_entry *
111 _bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115 {
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
128 bh = &h->root;
129 }
130 else
131 bh = NULL;
132
133 bed = get_elf_backend_data (abfd);
134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
135 sec, 0, NULL, FALSE, bed->collect,
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
139 BFD_ASSERT (h != NULL);
140 h->def_regular = 1;
141 h->non_elf = 0;
142 h->root.linker_def = 1;
143 h->type = STT_OBJECT;
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
146
147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
148 return h;
149 }
150
151 bfd_boolean
152 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
153 {
154 flagword flags;
155 asection *s;
156 struct elf_link_hash_entry *h;
157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
158 struct elf_link_hash_table *htab = elf_hash_table (info);
159
160 /* This function may be called more than once. */
161 if (htab->sgot != NULL)
162 return TRUE;
163
164 flags = bed->dynamic_sec_flags;
165
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
171 if (s == NULL
172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
173 return FALSE;
174 htab->srelgot = s;
175
176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
177 if (s == NULL
178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
179 return FALSE;
180 htab->sgot = s;
181
182 if (bed->want_got_plt)
183 {
184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
185 if (s == NULL
186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
187 return FALSE;
188 htab->sgotplt = s;
189 }
190
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info)->hgot = h;
203 if (h == NULL)
204 return FALSE;
205 }
206
207 return TRUE;
208 }
209 \f
210 /* Create a strtab to hold the dynamic symbol names. */
211 static bfd_boolean
212 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213 {
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
226 asection *s;
227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
228 if ((ibfd->flags
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249 }
250
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
257
258 bfd_boolean
259 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
260 {
261 flagword flags;
262 asection *s;
263 const struct elf_backend_data *bed;
264 struct elf_link_hash_entry *h;
265
266 if (! is_elf_hash_table (info->hash))
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
274
275 abfd = elf_hash_table (info)->dynobj;
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info) && !info->nointerp)
283 {
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
288 }
289
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || !bfd_set_section_alignment (s, 1))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 return FALSE;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
312 if (s == NULL
313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
314 return FALSE;
315 elf_hash_table (info)->dynsym = s;
316
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
319 if (s == NULL)
320 return FALSE;
321
322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
323 if (s == NULL
324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
336 return FALSE;
337
338 if (info->emit_hash)
339 {
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
342 if (s == NULL
343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
349 {
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
352 if (s == NULL
353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374 }
375
376 /* Create dynamic sections when linking against a dynamic object. */
377
378 bfd_boolean
379 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
380 {
381 flagword flags, pltflags;
382 struct elf_link_hash_entry *h;
383 asection *s;
384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
385 struct elf_link_hash_table *htab = elf_hash_table (info);
386
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags = bed->dynamic_sec_flags;
390
391 pltflags = flags;
392 if (bed->plt_not_loaded)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
403 if (s == NULL
404 || !bfd_set_section_alignment (s, bed->plt_alignment))
405 return FALSE;
406 htab->splt = s;
407
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
418
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
423 if (s == NULL
424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
425 return FALSE;
426 htab->srelplt = s;
427
428 if (! _bfd_elf_create_got_section (abfd, info))
429 return FALSE;
430
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
440 SEC_ALLOC | SEC_LINKER_CREATED);
441 if (s == NULL)
442 return FALSE;
443 htab->sdynbss = s;
444
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 flags);
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
468 if (bfd_link_executable (info))
469 {
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
474 if (s == NULL
475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
476 return FALSE;
477 htab->srelbss = s;
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
490 }
491 }
492
493 return TRUE;
494 }
495 \f
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
504 bfd_boolean
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
507 {
508 if (h->dynindx == -1)
509 {
510 struct elf_strtab_hash *dynstr;
511 char *p;
512 const char *name;
513 size_t indx;
514
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
535 {
536 h->forced_local = 1;
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
539 }
540
541 default:
542 break;
543 }
544
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
547
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
550 {
551 /* Create a strtab to hold the dynamic symbol names. */
552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
553 if (dynstr == NULL)
554 return FALSE;
555 }
556
557 /* We don't put any version information in the dynamic string
558 table. */
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
568
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
570
571 if (p != NULL)
572 *p = ELF_VER_CHR;
573
574 if (indx == (size_t) -1)
575 return FALSE;
576 h->dynstr_index = indx;
577 }
578
579 return TRUE;
580 }
581 \f
582 /* Mark a symbol dynamic. */
583
584 static void
585 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
588 {
589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
590
591 /* It may be called more than once on the same H. */
592 if(h->dynamic || bfd_link_relocatable (info))
593 return;
594
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
597 || h->type == STT_COMMON
598 || (sym != NULL
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
601 || (d != NULL
602 && h->non_elf
603 && (*d->match) (&d->head, NULL, h->root.root.string)))
604 {
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
609 }
610 }
611
612 /* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
614
615 bfd_boolean
616 bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
618 const char *name,
619 bfd_boolean provide,
620 bfd_boolean hidden)
621 {
622 struct elf_link_hash_entry *h, *hv;
623 struct elf_link_hash_table *htab;
624 const struct elf_backend_data *bed;
625
626 if (!is_elf_hash_table (info->hash))
627 return TRUE;
628
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
631 if (h == NULL)
632 return provide;
633
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
636
637 if (h->versioned == unknown)
638 {
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
642 {
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
647 }
648 }
649
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
653 {
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
656 }
657
658 switch (h->root.type)
659 {
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
669 h->root.type = bfd_link_hash_new;
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
672 break;
673 case bfd_link_hash_new:
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
677 the versioned symbol point to this one. */
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
690 default:
691 BFD_FAIL ();
692 return FALSE;
693 }
694
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
700 && h->def_dynamic
701 && !h->def_regular)
702 h->root.type = bfd_link_hash_undefined;
703
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
709 h->verinfo.verdef = NULL;
710
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
713
714 h->def_regular = 1;
715
716 if (hidden)
717 {
718 bed = get_elf_backend_data (output_bfd);
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
722 }
723
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
726 if (!bfd_link_relocatable (info)
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
731
732 if ((h->def_dynamic
733 || h->ref_dynamic
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
736 && !h->forced_local
737 && h->dynindx == -1)
738 {
739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
740 return FALSE;
741
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
745 if (h->is_weakalias)
746 {
747 struct elf_link_hash_entry *def = weakdef (h);
748
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
751 return FALSE;
752 }
753 }
754
755 return TRUE;
756 }
757
758 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
761
762 int
763 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
766 {
767 size_t amt;
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
771 size_t dynstr_index;
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
775
776 if (! is_elf_hash_table (info->hash))
777 return 0;
778
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
783
784 amt = sizeof (*entry);
785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
786 if (entry == NULL)
787 return 0;
788
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
791 1, input_indx, &entry->isym, esym, &eshndx))
792 {
793 bfd_release (input_bfd, entry);
794 return 0;
795 }
796
797 if (entry->isym.st_shndx != SHN_UNDEF
798 && entry->isym.st_shndx < SHN_LORESERVE)
799 {
800 asection *s;
801
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
804 {
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
809 }
810 }
811
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
815
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
818 {
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
823 }
824
825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
826 if (dynstr_index == (size_t) -1)
827 return 0;
828 entry->isym.st_name = dynstr_index;
829
830 eht = elf_hash_table (info);
831
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
837
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
841
842 /* The dynindx will be set at the end of size_dynamic_sections. */
843
844 return 1;
845 }
846
847 /* Return the dynindex of a local dynamic symbol. */
848
849 long
850 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
853 {
854 struct elf_link_local_dynamic_entry *e;
855
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
860 }
861
862 /* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
865
866 static bfd_boolean
867 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869 {
870 size_t *count = (size_t *) data;
871
872 if (h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879 }
880
881
882 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
884
885 static bfd_boolean
886 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888 {
889 size_t *count = (size_t *) data;
890
891 if (!h->forced_local)
892 return TRUE;
893
894 if (h->dynindx != -1)
895 h->dynindx = ++(*count);
896
897 return TRUE;
898 }
899
900 /* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902 bfd_boolean
903 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
906 {
907 struct elf_link_hash_table *htab;
908 asection *ip;
909
910 switch (elf_section_data (p)->this_hdr.sh_type)
911 {
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
917 htab = elf_hash_table (info);
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
920
921 return (htab->dynobj != NULL
922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
923 && ip->output_section == p);
924
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
929 }
930 }
931
932 bfd_boolean
933 _bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
937 {
938 return TRUE;
939 }
940
941 /* Assign dynsym indices. In a shared library we generate a section
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
949
950 static unsigned long
951 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
954 {
955 unsigned long dynsymcount = 0;
956 bfd_boolean do_sec = section_sym_count != NULL;
957
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
960 {
961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
964 if ((p->flags & SEC_EXCLUDE) == 0
965 && (p->flags & SEC_ALLOC) != 0
966 && elf_hash_table (info)->dynamic_relocs
967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
968 {
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
972 }
973 else if (do_sec)
974 elf_section_data (p)->dynindx = 0;
975 }
976 if (do_sec)
977 *section_sym_count = dynsymcount;
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
982
983 if (elf_hash_table (info)->dynlocal)
984 {
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
988 }
989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
990
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
994
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
1000
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
1003 }
1004
1005 /* Merge st_other field. */
1006
1007 static void
1008 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1009 const Elf_Internal_Sym *isym, asection *sec,
1010 bfd_boolean definition, bfd_boolean dynamic)
1011 {
1012 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1013
1014 /* If st_other has a processor-specific meaning, specific
1015 code might be needed here. */
1016 if (bed->elf_backend_merge_symbol_attribute)
1017 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1018 dynamic);
1019
1020 if (!dynamic)
1021 {
1022 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1023 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1024
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis - 1 < hvis - 1)
1028 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1029 }
1030 else if (definition
1031 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1032 && (sec->flags & SEC_READONLY) == 0)
1033 h->protected_def = 1;
1034 }
1035
1036 /* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
1048
1049 static bfd_boolean
1050 _bfd_elf_merge_symbol (bfd *abfd,
1051 struct bfd_link_info *info,
1052 const char *name,
1053 Elf_Internal_Sym *sym,
1054 asection **psec,
1055 bfd_vma *pvalue,
1056 struct elf_link_hash_entry **sym_hash,
1057 bfd **poldbfd,
1058 bfd_boolean *pold_weak,
1059 unsigned int *pold_alignment,
1060 bfd_boolean *skip,
1061 bfd **override,
1062 bfd_boolean *type_change_ok,
1063 bfd_boolean *size_change_ok,
1064 bfd_boolean *matched)
1065 {
1066 asection *sec, *oldsec;
1067 struct elf_link_hash_entry *h;
1068 struct elf_link_hash_entry *hi;
1069 struct elf_link_hash_entry *flip;
1070 int bind;
1071 bfd *oldbfd;
1072 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1073 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1074 const struct elf_backend_data *bed;
1075 char *new_version;
1076 bfd_boolean default_sym = *matched;
1077
1078 *skip = FALSE;
1079 *override = NULL;
1080
1081 sec = *psec;
1082 bind = ELF_ST_BIND (sym->st_info);
1083
1084 if (! bfd_is_und_section (sec))
1085 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1086 else
1087 h = ((struct elf_link_hash_entry *)
1088 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1089 if (h == NULL)
1090 return FALSE;
1091 *sym_hash = h;
1092
1093 bed = get_elf_backend_data (abfd);
1094
1095 /* NEW_VERSION is the symbol version of the new symbol. */
1096 if (h->versioned != unversioned)
1097 {
1098 /* Symbol version is unknown or versioned. */
1099 new_version = strrchr (name, ELF_VER_CHR);
1100 if (new_version)
1101 {
1102 if (h->versioned == unknown)
1103 {
1104 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1105 h->versioned = versioned_hidden;
1106 else
1107 h->versioned = versioned;
1108 }
1109 new_version += 1;
1110 if (new_version[0] == '\0')
1111 new_version = NULL;
1112 }
1113 else
1114 h->versioned = unversioned;
1115 }
1116 else
1117 new_version = NULL;
1118
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1121 hi = h;
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1125
1126 if (!*matched)
1127 {
1128 if (hi == h || h->root.type == bfd_link_hash_new)
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_HIDDEN is true if the existing symbol is only visible
1133 to the symbol with the same symbol version. NEW_HIDDEN is
1134 true if the new symbol is only visible to the symbol with
1135 the same symbol version. */
1136 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1137 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1138 if (!old_hidden && !new_hidden)
1139 /* The new symbol matches the existing symbol if both
1140 aren't hidden. */
1141 *matched = TRUE;
1142 else
1143 {
1144 /* OLD_VERSION is the symbol version of the existing
1145 symbol. */
1146 char *old_version;
1147
1148 if (h->versioned >= versioned)
1149 old_version = strrchr (h->root.root.string,
1150 ELF_VER_CHR) + 1;
1151 else
1152 old_version = NULL;
1153
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched = (old_version == new_version
1157 || (old_version != NULL
1158 && new_version != NULL
1159 && strcmp (old_version, new_version) == 0));
1160 }
1161 }
1162 }
1163
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1165 existing symbol. */
1166
1167 oldbfd = NULL;
1168 oldsec = NULL;
1169 switch (h->root.type)
1170 {
1171 default:
1172 break;
1173
1174 case bfd_link_hash_undefined:
1175 case bfd_link_hash_undefweak:
1176 oldbfd = h->root.u.undef.abfd;
1177 break;
1178
1179 case bfd_link_hash_defined:
1180 case bfd_link_hash_defweak:
1181 oldbfd = h->root.u.def.section->owner;
1182 oldsec = h->root.u.def.section;
1183 break;
1184
1185 case bfd_link_hash_common:
1186 oldbfd = h->root.u.c.p->section->owner;
1187 oldsec = h->root.u.c.p->section;
1188 if (pold_alignment)
1189 *pold_alignment = h->root.u.c.p->alignment_power;
1190 break;
1191 }
1192 if (poldbfd && *poldbfd == NULL)
1193 *poldbfd = oldbfd;
1194
1195 /* Differentiate strong and weak symbols. */
1196 newweak = bind == STB_WEAK;
1197 oldweak = (h->root.type == bfd_link_hash_defweak
1198 || h->root.type == bfd_link_hash_undefweak);
1199 if (pold_weak)
1200 *pold_weak = oldweak;
1201
1202 /* We have to check it for every instance since the first few may be
1203 references and not all compilers emit symbol type for undefined
1204 symbols. */
1205 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1206
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1209
1210 newdyn = (abfd->flags & DYNAMIC) != 0;
1211
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1219 if (newdyn)
1220 {
1221 if (bfd_is_und_section (sec))
1222 {
1223 if (bind != STB_WEAK)
1224 {
1225 h->ref_dynamic_nonweak = 1;
1226 hi->ref_dynamic_nonweak = 1;
1227 }
1228 }
1229 else
1230 {
1231 /* Update the existing symbol only if they match. */
1232 if (*matched)
1233 h->dynamic_def = 1;
1234 hi->dynamic_def = 1;
1235 }
1236 }
1237
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1241
1242 if (h->root.type == bfd_link_hash_new)
1243 {
1244 h->non_elf = 0;
1245 return TRUE;
1246 }
1247
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1254 if (abfd == oldbfd
1255 && (newweak || oldweak)
1256 && ((abfd->flags & DYNAMIC) == 0
1257 || !h->def_regular))
1258 return TRUE;
1259
1260 olddyn = FALSE;
1261 if (oldbfd != NULL)
1262 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1263 else if (oldsec != NULL)
1264 {
1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1266 indices used by MIPS ELF. */
1267 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1268 }
1269
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1272 if (oldbfd != NULL
1273 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1274 && newdyn != olddyn)
1275 {
1276 h->root.non_ir_ref_dynamic = TRUE;
1277 hi->root.non_ir_ref_dynamic = TRUE;
1278 }
1279
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1282
1283 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1284
1285 olddef = (h->root.type != bfd_link_hash_undefined
1286 && h->root.type != bfd_link_hash_undefweak
1287 && h->root.type != bfd_link_hash_common);
1288
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1291
1292 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1293 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1294
1295 oldfunc = (h->type != STT_NOTYPE
1296 && bed->is_function_type (h->type));
1297
1298 if (!(newfunc && oldfunc)
1299 && ELF_ST_TYPE (sym->st_info) != h->type
1300 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1301 && h->type != STT_NOTYPE
1302 && (newdef || bfd_is_com_section (sec))
1303 && (olddef || h->root.type == bfd_link_hash_common))
1304 {
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1310 if (newdyn
1311 && !olddyn)
1312 {
1313 *skip = TRUE;
1314 return TRUE;
1315 }
1316
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1319 dynamic state. */
1320 if (hi != h
1321 && !newdyn
1322 && olddyn)
1323 {
1324 h = hi;
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 h->def_dynamic = 0;
1329 h->dynamic_def = 0;
1330 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1331 {
1332 h->root.type = bfd_link_hash_undefined;
1333 h->root.u.undef.abfd = abfd;
1334 }
1335 else
1336 {
1337 h->root.type = bfd_link_hash_new;
1338 h->root.u.undef.abfd = NULL;
1339 }
1340 return TRUE;
1341 }
1342 }
1343
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1347 if (oldbfd != NULL
1348 && (oldbfd->flags & BFD_PLUGIN) == 0
1349 && (abfd->flags & BFD_PLUGIN) == 0
1350 && ELF_ST_TYPE (sym->st_info) != h->type
1351 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1352 {
1353 bfd *ntbfd, *tbfd;
1354 bfd_boolean ntdef, tdef;
1355 asection *ntsec, *tsec;
1356
1357 if (h->type == STT_TLS)
1358 {
1359 ntbfd = abfd;
1360 ntsec = sec;
1361 ntdef = newdef;
1362 tbfd = oldbfd;
1363 tsec = oldsec;
1364 tdef = olddef;
1365 }
1366 else
1367 {
1368 ntbfd = oldbfd;
1369 ntsec = oldsec;
1370 ntdef = olddef;
1371 tbfd = abfd;
1372 tsec = sec;
1373 tdef = newdef;
1374 }
1375
1376 if (tdef && ntdef)
1377 _bfd_error_handler
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
1381 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1382 else if (!tdef && !ntdef)
1383 _bfd_error_handler
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
1387 h->root.root.string, tbfd, ntbfd);
1388 else if (tdef)
1389 _bfd_error_handler
1390 /* xgettext:c-format */
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
1393 h->root.root.string, tbfd, tsec, ntbfd);
1394 else
1395 _bfd_error_handler
1396 /* xgettext:c-format */
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h->root.root.string, tbfd, ntbfd, ntsec);
1400
1401 bfd_set_error (bfd_error_bad_value);
1402 return FALSE;
1403 }
1404
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1407 if (newdyn
1408 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1409 && !bfd_is_und_section (sec))
1410 {
1411 *skip = TRUE;
1412 /* Make sure this symbol is dynamic. */
1413 h->ref_dynamic = 1;
1414 hi->ref_dynamic = 1;
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1417
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1420 return bfd_elf_link_record_dynamic_symbol (info, h);
1421 else
1422 return TRUE;
1423 }
1424 else if (!newdyn
1425 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1426 && h->def_dynamic)
1427 {
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
1431 if (hi->root.type == bfd_link_hash_indirect)
1432 {
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1437 if (h->ref_regular)
1438 {
1439 hi->root.type = h->root.type;
1440 h->root.type = bfd_link_hash_indirect;
1441 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1442
1443 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1444 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1445 {
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 h->forced_local = 0;
1450 h->ref_dynamic = 0;
1451 }
1452 else
1453 h->ref_dynamic = 1;
1454
1455 h->def_dynamic = 0;
1456 /* FIXME: Should we check type and size for protected symbol? */
1457 h->size = 0;
1458 h->type = 0;
1459
1460 h = hi;
1461 }
1462 else
1463 h = hi;
1464 }
1465
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1474 {
1475 h->root.type = bfd_link_hash_undefined;
1476 h->root.u.undef.abfd = abfd;
1477 }
1478 else
1479 {
1480 h->root.type = bfd_link_hash_new;
1481 h->root.u.undef.abfd = NULL;
1482 }
1483
1484 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1485 {
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1489 h->forced_local = 0;
1490 h->ref_dynamic = 0;
1491 }
1492 else
1493 h->ref_dynamic = 1;
1494 h->def_dynamic = 0;
1495 /* FIXME: Should we check type and size for protected symbol? */
1496 h->size = 0;
1497 h->type = 0;
1498 return TRUE;
1499 }
1500
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1508
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1513
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1516
1517 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1518 newweak = FALSE;
1519 if (olddef && newdyn)
1520 oldweak = FALSE;
1521
1522 /* Allow changes between different types of function symbol. */
1523 if (newfunc && oldfunc)
1524 *type_change_ok = TRUE;
1525
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
1529
1530 if (oldweak
1531 || newweak
1532 || (newdef
1533 && h->root.type == bfd_link_hash_undefined))
1534 *type_change_ok = TRUE;
1535
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1538
1539 if (*type_change_ok
1540 || h->root.type == bfd_link_hash_undefined)
1541 *size_change_ok = TRUE;
1542
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1554 libraries.
1555
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1558
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1564 harmless. */
1565
1566 if (newdyn
1567 && newdef
1568 && !newweak
1569 && (sec->flags & SEC_ALLOC) != 0
1570 && (sec->flags & SEC_LOAD) == 0
1571 && sym->st_size > 0
1572 && !newfunc)
1573 newdyncommon = TRUE;
1574 else
1575 newdyncommon = FALSE;
1576
1577 if (olddyn
1578 && olddef
1579 && h->root.type == bfd_link_hash_defined
1580 && h->def_dynamic
1581 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1582 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1583 && h->size > 0
1584 && !oldfunc)
1585 olddyncommon = TRUE;
1586 else
1587 olddyncommon = FALSE;
1588
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
1591 if (bed->merge_symbol != NULL)
1592 {
1593 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1594 return FALSE;
1595 sec = *psec;
1596 }
1597
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
1600 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1601 && !default_sym && h->def_regular
1602 && !(oldbfd != NULL
1603 && (oldbfd->flags & BFD_PLUGIN) != 0
1604 && (abfd->flags & BFD_PLUGIN) == 0))
1605 {
1606 /* Handle a multiple definition. */
1607 (*info->callbacks->multiple_definition) (info, &h->root,
1608 abfd, sec, *pvalue);
1609 *skip = TRUE;
1610 return TRUE;
1611 }
1612
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1615 two. */
1616
1617 if (olddyncommon
1618 && newdyncommon
1619 && sym->st_size != h->size)
1620 {
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1626
1627 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1628 bfd_link_hash_common, sym->st_size);
1629 if (sym->st_size > h->size)
1630 h->size = sym->st_size;
1631
1632 *size_change_ok = TRUE;
1633 }
1634
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1641
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
1647 object to override a weak symbol in a shared object. */
1648
1649 if (newdyn
1650 && newdef
1651 && (olddef
1652 || (h->root.type == bfd_link_hash_common
1653 && (newweak || newfunc))))
1654 {
1655 *override = abfd;
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
1658
1659 *psec = sec = bfd_und_section_ptr;
1660 *size_change_ok = TRUE;
1661
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
1667
1668 if (h->root.type == bfd_link_hash_common)
1669 *type_change_ok = TRUE;
1670 }
1671
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1676 right thing. */
1677
1678 if (newdyncommon
1679 && h->root.type == bfd_link_hash_common)
1680 {
1681 *override = oldbfd;
1682 newdef = FALSE;
1683 newdyncommon = FALSE;
1684 *pvalue = sym->st_size;
1685 *psec = sec = bed->common_section (oldsec);
1686 *size_change_ok = TRUE;
1687 }
1688
1689 /* Skip weak definitions of symbols that are already defined. */
1690 if (newdef && olddef && newweak)
1691 {
1692 /* Don't skip new non-IR weak syms. */
1693 if (!(oldbfd != NULL
1694 && (oldbfd->flags & BFD_PLUGIN) != 0
1695 && (abfd->flags & BFD_PLUGIN) == 0))
1696 {
1697 newdef = FALSE;
1698 *skip = TRUE;
1699 }
1700
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1703 local symbol. */
1704 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1705 if (h->dynindx != -1)
1706 switch (ELF_ST_VISIBILITY (h->other))
1707 {
1708 case STV_INTERNAL:
1709 case STV_HIDDEN:
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1711 break;
1712 }
1713 }
1714
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1720
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
1723 symbol is a function or is weak. */
1724
1725 flip = NULL;
1726 if (!newdyn
1727 && (newdef
1728 || (bfd_is_com_section (sec)
1729 && (oldweak || oldfunc)))
1730 && olddyn
1731 && olddef
1732 && h->def_dynamic)
1733 {
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1736 new definition. */
1737
1738 h->root.type = bfd_link_hash_undefined;
1739 h->root.u.undef.abfd = h->root.u.def.section->owner;
1740 *size_change_ok = TRUE;
1741
1742 olddef = FALSE;
1743 olddyncommon = FALSE;
1744
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1747
1748 if (bfd_is_com_section (sec))
1749 {
1750 if (oldfunc)
1751 {
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1754 function. */
1755 h->def_dynamic = 0;
1756 h->type = STT_NOTYPE;
1757 }
1758 *type_change_ok = TRUE;
1759 }
1760
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
1763 else
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h->verinfo.vertree = NULL;
1768 }
1769
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1775
1776 if (! newdyn
1777 && bfd_is_com_section (sec)
1778 && olddyncommon)
1779 {
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1783 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1784 bfd_link_hash_common, sym->st_size);
1785
1786 /* If the presumed common symbol in the dynamic object is
1787 larger, pretend that the new symbol has its size. */
1788
1789 if (h->size > *pvalue)
1790 *pvalue = h->size;
1791
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment);
1795 *pold_alignment = h->root.u.def.section->alignment_power;
1796
1797 olddef = FALSE;
1798 olddyncommon = FALSE;
1799
1800 h->root.type = bfd_link_hash_undefined;
1801 h->root.u.undef.abfd = h->root.u.def.section->owner;
1802
1803 *size_change_ok = TRUE;
1804 *type_change_ok = TRUE;
1805
1806 if (hi->root.type == bfd_link_hash_indirect)
1807 flip = hi;
1808 else
1809 h->verinfo.vertree = NULL;
1810 }
1811
1812 if (flip != NULL)
1813 {
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
1817 flip->root.type = h->root.type;
1818 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1819 h->root.type = bfd_link_hash_indirect;
1820 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1821 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1822 if (h->def_dynamic)
1823 {
1824 h->def_dynamic = 0;
1825 flip->ref_dynamic = 1;
1826 }
1827 }
1828
1829 return TRUE;
1830 }
1831
1832 /* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1835 set DYNSYM if the new indirect symbol is dynamic. */
1836
1837 static bfd_boolean
1838 _bfd_elf_add_default_symbol (bfd *abfd,
1839 struct bfd_link_info *info,
1840 struct elf_link_hash_entry *h,
1841 const char *name,
1842 Elf_Internal_Sym *sym,
1843 asection *sec,
1844 bfd_vma value,
1845 bfd **poldbfd,
1846 bfd_boolean *dynsym)
1847 {
1848 bfd_boolean type_change_ok;
1849 bfd_boolean size_change_ok;
1850 bfd_boolean skip;
1851 char *shortname;
1852 struct elf_link_hash_entry *hi;
1853 struct bfd_link_hash_entry *bh;
1854 const struct elf_backend_data *bed;
1855 bfd_boolean collect;
1856 bfd_boolean dynamic;
1857 bfd *override;
1858 char *p;
1859 size_t len, shortlen;
1860 asection *tmp_sec;
1861 bfd_boolean matched;
1862
1863 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1864 return TRUE;
1865
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p = strchr (name, ELF_VER_CHR);
1871 if (h->versioned == unknown)
1872 {
1873 if (p == NULL)
1874 {
1875 h->versioned = unversioned;
1876 return TRUE;
1877 }
1878 else
1879 {
1880 if (p[1] != ELF_VER_CHR)
1881 {
1882 h->versioned = versioned_hidden;
1883 return TRUE;
1884 }
1885 else
1886 h->versioned = versioned;
1887 }
1888 }
1889 else
1890 {
1891 /* PR ld/19073: We may see an unversioned definition after the
1892 default version. */
1893 if (p == NULL)
1894 return TRUE;
1895 }
1896
1897 bed = get_elf_backend_data (abfd);
1898 collect = bed->collect;
1899 dynamic = (abfd->flags & DYNAMIC) != 0;
1900
1901 shortlen = p - name;
1902 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1903 if (shortname == NULL)
1904 return FALSE;
1905 memcpy (shortname, name, shortlen);
1906 shortname[shortlen] = '\0';
1907
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok = FALSE;
1913 size_change_ok = FALSE;
1914 matched = TRUE;
1915 tmp_sec = sec;
1916 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1917 &hi, poldbfd, NULL, NULL, &skip, &override,
1918 &type_change_ok, &size_change_ok, &matched))
1919 return FALSE;
1920
1921 if (skip)
1922 goto nondefault;
1923
1924 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1925 {
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1932 {
1933 bfd_boolean hide;
1934
1935 hi->verinfo.vertree
1936 = bfd_find_version_for_sym (info->version_info,
1937 hi->root.root.string, &hide);
1938 if (hi->verinfo.vertree != NULL && hide)
1939 {
1940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1941 goto nondefault;
1942 }
1943 }
1944 if (hi->verinfo.vertree != NULL
1945 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1946 goto nondefault;
1947 }
1948
1949 if (! override)
1950 {
1951 /* Add the default symbol if not performing a relocatable link. */
1952 if (! bfd_link_relocatable (info))
1953 {
1954 bh = &hi->root;
1955 if (bh->type == bfd_link_hash_defined
1956 && bh->u.def.section->owner != NULL
1957 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1958 {
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1961 it. */
1962 bh->type = bfd_link_hash_undefined;
1963 bh->u.undef.abfd = bh->u.def.section->owner;
1964 }
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info, abfd, shortname, BSF_INDIRECT,
1967 bfd_ind_section_ptr,
1968 0, name, FALSE, collect, &bh)))
1969 return FALSE;
1970 hi = (struct elf_link_hash_entry *) bh;
1971 }
1972 }
1973 else
1974 {
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1980
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1984
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1992
1993 while (hi->root.type == bfd_link_hash_indirect
1994 || hi->root.type == bfd_link_hash_warning)
1995 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996
1997 h->root.type = bfd_link_hash_indirect;
1998 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1999 if (h->def_dynamic)
2000 {
2001 h->def_dynamic = 0;
2002 hi->ref_dynamic = 1;
2003 if (hi->ref_regular
2004 || hi->def_regular)
2005 {
2006 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2007 return FALSE;
2008 }
2009 }
2010
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2013 hi = h;
2014 }
2015
2016 /* Check if HI is a warning symbol. */
2017 if (hi->root.type == bfd_link_hash_warning)
2018 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2019
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2023
2024 if (hi->root.type == bfd_link_hash_indirect)
2025 {
2026 struct elf_link_hash_entry *ht;
2027
2028 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2029 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2030
2031 /* A reference to the SHORTNAME symbol from a dynamic library
2032 will be satisfied by the versioned symbol at runtime. In
2033 effect, we have a reference to the versioned symbol. */
2034 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2035 hi->dynamic_def |= ht->dynamic_def;
2036
2037 /* See if the new flags lead us to realize that the symbol must
2038 be dynamic. */
2039 if (! *dynsym)
2040 {
2041 if (! dynamic)
2042 {
2043 if (! bfd_link_executable (info)
2044 || hi->def_dynamic
2045 || hi->ref_dynamic)
2046 *dynsym = TRUE;
2047 }
2048 else
2049 {
2050 if (hi->ref_regular)
2051 *dynsym = TRUE;
2052 }
2053 }
2054 }
2055
2056 /* We also need to define an indirection from the nondefault version
2057 of the symbol. */
2058
2059 nondefault:
2060 len = strlen (name);
2061 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2062 if (shortname == NULL)
2063 return FALSE;
2064 memcpy (shortname, name, shortlen);
2065 memcpy (shortname + shortlen, p + 1, len - shortlen);
2066
2067 /* Once again, merge with any existing symbol. */
2068 type_change_ok = FALSE;
2069 size_change_ok = FALSE;
2070 tmp_sec = sec;
2071 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2072 &hi, poldbfd, NULL, NULL, &skip, &override,
2073 &type_change_ok, &size_change_ok, &matched))
2074 return FALSE;
2075
2076 if (skip)
2077 return TRUE;
2078
2079 if (override)
2080 {
2081 /* Here SHORTNAME is a versioned name, so we don't expect to see
2082 the type of override we do in the case above unless it is
2083 overridden by a versioned definition. */
2084 if (hi->root.type != bfd_link_hash_defined
2085 && hi->root.type != bfd_link_hash_defweak)
2086 _bfd_error_handler
2087 /* xgettext:c-format */
2088 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2089 abfd, shortname);
2090 }
2091 else
2092 {
2093 bh = &hi->root;
2094 if (! (_bfd_generic_link_add_one_symbol
2095 (info, abfd, shortname, BSF_INDIRECT,
2096 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2097 return FALSE;
2098 hi = (struct elf_link_hash_entry *) bh;
2099
2100 /* If there is a duplicate definition somewhere, then HI may not
2101 point to an indirect symbol. We will have reported an error
2102 to the user in that case. */
2103
2104 if (hi->root.type == bfd_link_hash_indirect)
2105 {
2106 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2107 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2108 hi->dynamic_def |= h->dynamic_def;
2109
2110 /* See if the new flags lead us to realize that the symbol
2111 must be dynamic. */
2112 if (! *dynsym)
2113 {
2114 if (! dynamic)
2115 {
2116 if (! bfd_link_executable (info)
2117 || hi->ref_dynamic)
2118 *dynsym = TRUE;
2119 }
2120 else
2121 {
2122 if (hi->ref_regular)
2123 *dynsym = TRUE;
2124 }
2125 }
2126 }
2127 }
2128
2129 return TRUE;
2130 }
2131 \f
2132 /* This routine is used to export all defined symbols into the dynamic
2133 symbol table. It is called via elf_link_hash_traverse. */
2134
2135 static bfd_boolean
2136 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2137 {
2138 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2139
2140 /* Ignore indirect symbols. These are added by the versioning code. */
2141 if (h->root.type == bfd_link_hash_indirect)
2142 return TRUE;
2143
2144 /* Ignore this if we won't export it. */
2145 if (!eif->info->export_dynamic && !h->dynamic)
2146 return TRUE;
2147
2148 if (h->dynindx == -1
2149 && (h->def_regular || h->ref_regular)
2150 && ! bfd_hide_sym_by_version (eif->info->version_info,
2151 h->root.root.string))
2152 {
2153 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2154 {
2155 eif->failed = TRUE;
2156 return FALSE;
2157 }
2158 }
2159
2160 return TRUE;
2161 }
2162 \f
2163 /* Look through the symbols which are defined in other shared
2164 libraries and referenced here. Update the list of version
2165 dependencies. This will be put into the .gnu.version_r section.
2166 This function is called via elf_link_hash_traverse. */
2167
2168 static bfd_boolean
2169 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2170 void *data)
2171 {
2172 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2173 Elf_Internal_Verneed *t;
2174 Elf_Internal_Vernaux *a;
2175 size_t amt;
2176
2177 /* We only care about symbols defined in shared objects with version
2178 information. */
2179 if (!h->def_dynamic
2180 || h->def_regular
2181 || h->dynindx == -1
2182 || h->verinfo.verdef == NULL
2183 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2184 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2185 return TRUE;
2186
2187 /* See if we already know about this version. */
2188 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2189 t != NULL;
2190 t = t->vn_nextref)
2191 {
2192 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2193 continue;
2194
2195 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2196 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2197 return TRUE;
2198
2199 break;
2200 }
2201
2202 /* This is a new version. Add it to tree we are building. */
2203
2204 if (t == NULL)
2205 {
2206 amt = sizeof *t;
2207 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2208 if (t == NULL)
2209 {
2210 rinfo->failed = TRUE;
2211 return FALSE;
2212 }
2213
2214 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2215 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2216 elf_tdata (rinfo->info->output_bfd)->verref = t;
2217 }
2218
2219 amt = sizeof *a;
2220 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2221 if (a == NULL)
2222 {
2223 rinfo->failed = TRUE;
2224 return FALSE;
2225 }
2226
2227 /* Note that we are copying a string pointer here, and testing it
2228 above. If bfd_elf_string_from_elf_section is ever changed to
2229 discard the string data when low in memory, this will have to be
2230 fixed. */
2231 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2232
2233 a->vna_flags = h->verinfo.verdef->vd_flags;
2234 a->vna_nextptr = t->vn_auxptr;
2235
2236 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2237 ++rinfo->vers;
2238
2239 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2240
2241 t->vn_auxptr = a;
2242
2243 return TRUE;
2244 }
2245
2246 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2247 hidden. Set *T_P to NULL if there is no match. */
2248
2249 static bfd_boolean
2250 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2251 struct elf_link_hash_entry *h,
2252 const char *version_p,
2253 struct bfd_elf_version_tree **t_p,
2254 bfd_boolean *hide)
2255 {
2256 struct bfd_elf_version_tree *t;
2257
2258 /* Look for the version. If we find it, it is no longer weak. */
2259 for (t = info->version_info; t != NULL; t = t->next)
2260 {
2261 if (strcmp (t->name, version_p) == 0)
2262 {
2263 size_t len;
2264 char *alc;
2265 struct bfd_elf_version_expr *d;
2266
2267 len = version_p - h->root.root.string;
2268 alc = (char *) bfd_malloc (len);
2269 if (alc == NULL)
2270 return FALSE;
2271 memcpy (alc, h->root.root.string, len - 1);
2272 alc[len - 1] = '\0';
2273 if (alc[len - 2] == ELF_VER_CHR)
2274 alc[len - 2] = '\0';
2275
2276 h->verinfo.vertree = t;
2277 t->used = TRUE;
2278 d = NULL;
2279
2280 if (t->globals.list != NULL)
2281 d = (*t->match) (&t->globals, NULL, alc);
2282
2283 /* See if there is anything to force this symbol to
2284 local scope. */
2285 if (d == NULL && t->locals.list != NULL)
2286 {
2287 d = (*t->match) (&t->locals, NULL, alc);
2288 if (d != NULL
2289 && h->dynindx != -1
2290 && ! info->export_dynamic)
2291 *hide = TRUE;
2292 }
2293
2294 free (alc);
2295 break;
2296 }
2297 }
2298
2299 *t_p = t;
2300
2301 return TRUE;
2302 }
2303
2304 /* Return TRUE if the symbol H is hidden by version script. */
2305
2306 bfd_boolean
2307 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2308 struct elf_link_hash_entry *h)
2309 {
2310 const char *p;
2311 bfd_boolean hide = FALSE;
2312 const struct elf_backend_data *bed
2313 = get_elf_backend_data (info->output_bfd);
2314
2315 /* Version script only hides symbols defined in regular objects. */
2316 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2317 return TRUE;
2318
2319 p = strchr (h->root.root.string, ELF_VER_CHR);
2320 if (p != NULL && h->verinfo.vertree == NULL)
2321 {
2322 struct bfd_elf_version_tree *t;
2323
2324 ++p;
2325 if (*p == ELF_VER_CHR)
2326 ++p;
2327
2328 if (*p != '\0'
2329 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2330 && hide)
2331 {
2332 if (hide)
2333 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2334 return TRUE;
2335 }
2336 }
2337
2338 /* If we don't have a version for this symbol, see if we can find
2339 something. */
2340 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2341 {
2342 h->verinfo.vertree
2343 = bfd_find_version_for_sym (info->version_info,
2344 h->root.root.string, &hide);
2345 if (h->verinfo.vertree != NULL && hide)
2346 {
2347 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2348 return TRUE;
2349 }
2350 }
2351
2352 return FALSE;
2353 }
2354
2355 /* Figure out appropriate versions for all the symbols. We may not
2356 have the version number script until we have read all of the input
2357 files, so until that point we don't know which symbols should be
2358 local. This function is called via elf_link_hash_traverse. */
2359
2360 static bfd_boolean
2361 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2362 {
2363 struct elf_info_failed *sinfo;
2364 struct bfd_link_info *info;
2365 const struct elf_backend_data *bed;
2366 struct elf_info_failed eif;
2367 char *p;
2368 bfd_boolean hide;
2369
2370 sinfo = (struct elf_info_failed *) data;
2371 info = sinfo->info;
2372
2373 /* Fix the symbol flags. */
2374 eif.failed = FALSE;
2375 eif.info = info;
2376 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2377 {
2378 if (eif.failed)
2379 sinfo->failed = TRUE;
2380 return FALSE;
2381 }
2382
2383 bed = get_elf_backend_data (info->output_bfd);
2384
2385 /* We only need version numbers for symbols defined in regular
2386 objects. */
2387 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2388 {
2389 /* Hide symbols defined in discarded input sections. */
2390 if ((h->root.type == bfd_link_hash_defined
2391 || h->root.type == bfd_link_hash_defweak)
2392 && discarded_section (h->root.u.def.section))
2393 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2394 return TRUE;
2395 }
2396
2397 hide = FALSE;
2398 p = strchr (h->root.root.string, ELF_VER_CHR);
2399 if (p != NULL && h->verinfo.vertree == NULL)
2400 {
2401 struct bfd_elf_version_tree *t;
2402
2403 ++p;
2404 if (*p == ELF_VER_CHR)
2405 ++p;
2406
2407 /* If there is no version string, we can just return out. */
2408 if (*p == '\0')
2409 return TRUE;
2410
2411 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2412 {
2413 sinfo->failed = TRUE;
2414 return FALSE;
2415 }
2416
2417 if (hide)
2418 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2419
2420 /* If we are building an application, we need to create a
2421 version node for this version. */
2422 if (t == NULL && bfd_link_executable (info))
2423 {
2424 struct bfd_elf_version_tree **pp;
2425 int version_index;
2426
2427 /* If we aren't going to export this symbol, we don't need
2428 to worry about it. */
2429 if (h->dynindx == -1)
2430 return TRUE;
2431
2432 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2433 sizeof *t);
2434 if (t == NULL)
2435 {
2436 sinfo->failed = TRUE;
2437 return FALSE;
2438 }
2439
2440 t->name = p;
2441 t->name_indx = (unsigned int) -1;
2442 t->used = TRUE;
2443
2444 version_index = 1;
2445 /* Don't count anonymous version tag. */
2446 if (sinfo->info->version_info != NULL
2447 && sinfo->info->version_info->vernum == 0)
2448 version_index = 0;
2449 for (pp = &sinfo->info->version_info;
2450 *pp != NULL;
2451 pp = &(*pp)->next)
2452 ++version_index;
2453 t->vernum = version_index;
2454
2455 *pp = t;
2456
2457 h->verinfo.vertree = t;
2458 }
2459 else if (t == NULL)
2460 {
2461 /* We could not find the version for a symbol when
2462 generating a shared archive. Return an error. */
2463 _bfd_error_handler
2464 /* xgettext:c-format */
2465 (_("%pB: version node not found for symbol %s"),
2466 info->output_bfd, h->root.root.string);
2467 bfd_set_error (bfd_error_bad_value);
2468 sinfo->failed = TRUE;
2469 return FALSE;
2470 }
2471 }
2472
2473 /* If we don't have a version for this symbol, see if we can find
2474 something. */
2475 if (!hide
2476 && h->verinfo.vertree == NULL
2477 && sinfo->info->version_info != NULL)
2478 {
2479 h->verinfo.vertree
2480 = bfd_find_version_for_sym (sinfo->info->version_info,
2481 h->root.root.string, &hide);
2482 if (h->verinfo.vertree != NULL && hide)
2483 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2484 }
2485
2486 return TRUE;
2487 }
2488 \f
2489 /* Read and swap the relocs from the section indicated by SHDR. This
2490 may be either a REL or a RELA section. The relocations are
2491 translated into RELA relocations and stored in INTERNAL_RELOCS,
2492 which should have already been allocated to contain enough space.
2493 The EXTERNAL_RELOCS are a buffer where the external form of the
2494 relocations should be stored.
2495
2496 Returns FALSE if something goes wrong. */
2497
2498 static bfd_boolean
2499 elf_link_read_relocs_from_section (bfd *abfd,
2500 asection *sec,
2501 Elf_Internal_Shdr *shdr,
2502 void *external_relocs,
2503 Elf_Internal_Rela *internal_relocs)
2504 {
2505 const struct elf_backend_data *bed;
2506 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2507 const bfd_byte *erela;
2508 const bfd_byte *erelaend;
2509 Elf_Internal_Rela *irela;
2510 Elf_Internal_Shdr *symtab_hdr;
2511 size_t nsyms;
2512
2513 /* Position ourselves at the start of the section. */
2514 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2515 return FALSE;
2516
2517 /* Read the relocations. */
2518 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2519 return FALSE;
2520
2521 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2522 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2523
2524 bed = get_elf_backend_data (abfd);
2525
2526 /* Convert the external relocations to the internal format. */
2527 if (shdr->sh_entsize == bed->s->sizeof_rel)
2528 swap_in = bed->s->swap_reloc_in;
2529 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2530 swap_in = bed->s->swap_reloca_in;
2531 else
2532 {
2533 bfd_set_error (bfd_error_wrong_format);
2534 return FALSE;
2535 }
2536
2537 erela = (const bfd_byte *) external_relocs;
2538 /* Setting erelaend like this and comparing with <= handles case of
2539 a fuzzed object with sh_size not a multiple of sh_entsize. */
2540 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2541 irela = internal_relocs;
2542 while (erela <= erelaend)
2543 {
2544 bfd_vma r_symndx;
2545
2546 (*swap_in) (abfd, erela, irela);
2547 r_symndx = ELF32_R_SYM (irela->r_info);
2548 if (bed->s->arch_size == 64)
2549 r_symndx >>= 24;
2550 if (nsyms > 0)
2551 {
2552 if ((size_t) r_symndx >= nsyms)
2553 {
2554 _bfd_error_handler
2555 /* xgettext:c-format */
2556 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2557 " for offset %#" PRIx64 " in section `%pA'"),
2558 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2559 (uint64_t) irela->r_offset, sec);
2560 bfd_set_error (bfd_error_bad_value);
2561 return FALSE;
2562 }
2563 }
2564 else if (r_symndx != STN_UNDEF)
2565 {
2566 _bfd_error_handler
2567 /* xgettext:c-format */
2568 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2569 " for offset %#" PRIx64 " in section `%pA'"
2570 " when the object file has no symbol table"),
2571 abfd, (uint64_t) r_symndx,
2572 (uint64_t) irela->r_offset, sec);
2573 bfd_set_error (bfd_error_bad_value);
2574 return FALSE;
2575 }
2576 irela += bed->s->int_rels_per_ext_rel;
2577 erela += shdr->sh_entsize;
2578 }
2579
2580 return TRUE;
2581 }
2582
2583 /* Read and swap the relocs for a section O. They may have been
2584 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2585 not NULL, they are used as buffers to read into. They are known to
2586 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2587 the return value is allocated using either malloc or bfd_alloc,
2588 according to the KEEP_MEMORY argument. If O has two relocation
2589 sections (both REL and RELA relocations), then the REL_HDR
2590 relocations will appear first in INTERNAL_RELOCS, followed by the
2591 RELA_HDR relocations. */
2592
2593 Elf_Internal_Rela *
2594 _bfd_elf_link_read_relocs (bfd *abfd,
2595 asection *o,
2596 void *external_relocs,
2597 Elf_Internal_Rela *internal_relocs,
2598 bfd_boolean keep_memory)
2599 {
2600 void *alloc1 = NULL;
2601 Elf_Internal_Rela *alloc2 = NULL;
2602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2603 struct bfd_elf_section_data *esdo = elf_section_data (o);
2604 Elf_Internal_Rela *internal_rela_relocs;
2605
2606 if (esdo->relocs != NULL)
2607 return esdo->relocs;
2608
2609 if (o->reloc_count == 0)
2610 return NULL;
2611
2612 if (internal_relocs == NULL)
2613 {
2614 bfd_size_type size;
2615
2616 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2617 if (keep_memory)
2618 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2619 else
2620 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2621 if (internal_relocs == NULL)
2622 goto error_return;
2623 }
2624
2625 if (external_relocs == NULL)
2626 {
2627 bfd_size_type size = 0;
2628
2629 if (esdo->rel.hdr)
2630 size += esdo->rel.hdr->sh_size;
2631 if (esdo->rela.hdr)
2632 size += esdo->rela.hdr->sh_size;
2633
2634 alloc1 = bfd_malloc (size);
2635 if (alloc1 == NULL)
2636 goto error_return;
2637 external_relocs = alloc1;
2638 }
2639
2640 internal_rela_relocs = internal_relocs;
2641 if (esdo->rel.hdr)
2642 {
2643 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2644 external_relocs,
2645 internal_relocs))
2646 goto error_return;
2647 external_relocs = (((bfd_byte *) external_relocs)
2648 + esdo->rel.hdr->sh_size);
2649 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2650 * bed->s->int_rels_per_ext_rel);
2651 }
2652
2653 if (esdo->rela.hdr
2654 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2655 external_relocs,
2656 internal_rela_relocs)))
2657 goto error_return;
2658
2659 /* Cache the results for next time, if we can. */
2660 if (keep_memory)
2661 esdo->relocs = internal_relocs;
2662
2663 free (alloc1);
2664
2665 /* Don't free alloc2, since if it was allocated we are passing it
2666 back (under the name of internal_relocs). */
2667
2668 return internal_relocs;
2669
2670 error_return:
2671 free (alloc1);
2672 if (alloc2 != NULL)
2673 {
2674 if (keep_memory)
2675 bfd_release (abfd, alloc2);
2676 else
2677 free (alloc2);
2678 }
2679 return NULL;
2680 }
2681
2682 /* Compute the size of, and allocate space for, REL_HDR which is the
2683 section header for a section containing relocations for O. */
2684
2685 static bfd_boolean
2686 _bfd_elf_link_size_reloc_section (bfd *abfd,
2687 struct bfd_elf_section_reloc_data *reldata)
2688 {
2689 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2690
2691 /* That allows us to calculate the size of the section. */
2692 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2693
2694 /* The contents field must last into write_object_contents, so we
2695 allocate it with bfd_alloc rather than malloc. Also since we
2696 cannot be sure that the contents will actually be filled in,
2697 we zero the allocated space. */
2698 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2699 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2700 return FALSE;
2701
2702 if (reldata->hashes == NULL && reldata->count)
2703 {
2704 struct elf_link_hash_entry **p;
2705
2706 p = ((struct elf_link_hash_entry **)
2707 bfd_zmalloc (reldata->count * sizeof (*p)));
2708 if (p == NULL)
2709 return FALSE;
2710
2711 reldata->hashes = p;
2712 }
2713
2714 return TRUE;
2715 }
2716
2717 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2718 originated from the section given by INPUT_REL_HDR) to the
2719 OUTPUT_BFD. */
2720
2721 bfd_boolean
2722 _bfd_elf_link_output_relocs (bfd *output_bfd,
2723 asection *input_section,
2724 Elf_Internal_Shdr *input_rel_hdr,
2725 Elf_Internal_Rela *internal_relocs,
2726 struct elf_link_hash_entry **rel_hash
2727 ATTRIBUTE_UNUSED)
2728 {
2729 Elf_Internal_Rela *irela;
2730 Elf_Internal_Rela *irelaend;
2731 bfd_byte *erel;
2732 struct bfd_elf_section_reloc_data *output_reldata;
2733 asection *output_section;
2734 const struct elf_backend_data *bed;
2735 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2736 struct bfd_elf_section_data *esdo;
2737
2738 output_section = input_section->output_section;
2739
2740 bed = get_elf_backend_data (output_bfd);
2741 esdo = elf_section_data (output_section);
2742 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2743 {
2744 output_reldata = &esdo->rel;
2745 swap_out = bed->s->swap_reloc_out;
2746 }
2747 else if (esdo->rela.hdr
2748 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2749 {
2750 output_reldata = &esdo->rela;
2751 swap_out = bed->s->swap_reloca_out;
2752 }
2753 else
2754 {
2755 _bfd_error_handler
2756 /* xgettext:c-format */
2757 (_("%pB: relocation size mismatch in %pB section %pA"),
2758 output_bfd, input_section->owner, input_section);
2759 bfd_set_error (bfd_error_wrong_format);
2760 return FALSE;
2761 }
2762
2763 erel = output_reldata->hdr->contents;
2764 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2765 irela = internal_relocs;
2766 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2767 * bed->s->int_rels_per_ext_rel);
2768 while (irela < irelaend)
2769 {
2770 (*swap_out) (output_bfd, irela, erel);
2771 irela += bed->s->int_rels_per_ext_rel;
2772 erel += input_rel_hdr->sh_entsize;
2773 }
2774
2775 /* Bump the counter, so that we know where to add the next set of
2776 relocations. */
2777 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2778
2779 return TRUE;
2780 }
2781 \f
2782 /* Make weak undefined symbols in PIE dynamic. */
2783
2784 bfd_boolean
2785 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2786 struct elf_link_hash_entry *h)
2787 {
2788 if (bfd_link_pie (info)
2789 && h->dynindx == -1
2790 && h->root.type == bfd_link_hash_undefweak)
2791 return bfd_elf_link_record_dynamic_symbol (info, h);
2792
2793 return TRUE;
2794 }
2795
2796 /* Fix up the flags for a symbol. This handles various cases which
2797 can only be fixed after all the input files are seen. This is
2798 currently called by both adjust_dynamic_symbol and
2799 assign_sym_version, which is unnecessary but perhaps more robust in
2800 the face of future changes. */
2801
2802 static bfd_boolean
2803 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2804 struct elf_info_failed *eif)
2805 {
2806 const struct elf_backend_data *bed;
2807
2808 /* If this symbol was mentioned in a non-ELF file, try to set
2809 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2810 permit a non-ELF file to correctly refer to a symbol defined in
2811 an ELF dynamic object. */
2812 if (h->non_elf)
2813 {
2814 while (h->root.type == bfd_link_hash_indirect)
2815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2816
2817 if (h->root.type != bfd_link_hash_defined
2818 && h->root.type != bfd_link_hash_defweak)
2819 {
2820 h->ref_regular = 1;
2821 h->ref_regular_nonweak = 1;
2822 }
2823 else
2824 {
2825 if (h->root.u.def.section->owner != NULL
2826 && (bfd_get_flavour (h->root.u.def.section->owner)
2827 == bfd_target_elf_flavour))
2828 {
2829 h->ref_regular = 1;
2830 h->ref_regular_nonweak = 1;
2831 }
2832 else
2833 h->def_regular = 1;
2834 }
2835
2836 if (h->dynindx == -1
2837 && (h->def_dynamic
2838 || h->ref_dynamic))
2839 {
2840 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2841 {
2842 eif->failed = TRUE;
2843 return FALSE;
2844 }
2845 }
2846 }
2847 else
2848 {
2849 /* Unfortunately, NON_ELF is only correct if the symbol
2850 was first seen in a non-ELF file. Fortunately, if the symbol
2851 was first seen in an ELF file, we're probably OK unless the
2852 symbol was defined in a non-ELF file. Catch that case here.
2853 FIXME: We're still in trouble if the symbol was first seen in
2854 a dynamic object, and then later in a non-ELF regular object. */
2855 if ((h->root.type == bfd_link_hash_defined
2856 || h->root.type == bfd_link_hash_defweak)
2857 && !h->def_regular
2858 && (h->root.u.def.section->owner != NULL
2859 ? (bfd_get_flavour (h->root.u.def.section->owner)
2860 != bfd_target_elf_flavour)
2861 : (bfd_is_abs_section (h->root.u.def.section)
2862 && !h->def_dynamic)))
2863 h->def_regular = 1;
2864 }
2865
2866 /* Backend specific symbol fixup. */
2867 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2868 if (bed->elf_backend_fixup_symbol
2869 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2870 return FALSE;
2871
2872 /* If this is a final link, and the symbol was defined as a common
2873 symbol in a regular object file, and there was no definition in
2874 any dynamic object, then the linker will have allocated space for
2875 the symbol in a common section but the DEF_REGULAR
2876 flag will not have been set. */
2877 if (h->root.type == bfd_link_hash_defined
2878 && !h->def_regular
2879 && h->ref_regular
2880 && !h->def_dynamic
2881 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2882 h->def_regular = 1;
2883
2884 /* Symbols defined in discarded sections shouldn't be dynamic. */
2885 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2886 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2887
2888 /* If a weak undefined symbol has non-default visibility, we also
2889 hide it from the dynamic linker. */
2890 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2891 && h->root.type == bfd_link_hash_undefweak)
2892 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2893
2894 /* A hidden versioned symbol in executable should be forced local if
2895 it is is locally defined, not referenced by shared library and not
2896 exported. */
2897 else if (bfd_link_executable (eif->info)
2898 && h->versioned == versioned_hidden
2899 && !eif->info->export_dynamic
2900 && !h->dynamic
2901 && !h->ref_dynamic
2902 && h->def_regular)
2903 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2904
2905 /* If -Bsymbolic was used (which means to bind references to global
2906 symbols to the definition within the shared object), and this
2907 symbol was defined in a regular object, then it actually doesn't
2908 need a PLT entry. Likewise, if the symbol has non-default
2909 visibility. If the symbol has hidden or internal visibility, we
2910 will force it local. */
2911 else if (h->needs_plt
2912 && bfd_link_pic (eif->info)
2913 && is_elf_hash_table (eif->info->hash)
2914 && (SYMBOLIC_BIND (eif->info, h)
2915 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2916 && h->def_regular)
2917 {
2918 bfd_boolean force_local;
2919
2920 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2921 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2922 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2923 }
2924
2925 /* If this is a weak defined symbol in a dynamic object, and we know
2926 the real definition in the dynamic object, copy interesting flags
2927 over to the real definition. */
2928 if (h->is_weakalias)
2929 {
2930 struct elf_link_hash_entry *def = weakdef (h);
2931
2932 /* If the real definition is defined by a regular object file,
2933 don't do anything special. See the longer description in
2934 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2935 bfd_link_hash_defined as it was when put on the alias list
2936 then it must have originally been a versioned symbol (for
2937 which a non-versioned indirect symbol is created) and later
2938 a definition for the non-versioned symbol is found. In that
2939 case the indirection is flipped with the versioned symbol
2940 becoming an indirect pointing at the non-versioned symbol.
2941 Thus, not an alias any more. */
2942 if (def->def_regular
2943 || def->root.type != bfd_link_hash_defined)
2944 {
2945 h = def;
2946 while ((h = h->u.alias) != def)
2947 h->is_weakalias = 0;
2948 }
2949 else
2950 {
2951 while (h->root.type == bfd_link_hash_indirect)
2952 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2953 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2954 || h->root.type == bfd_link_hash_defweak);
2955 BFD_ASSERT (def->def_dynamic);
2956 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2957 }
2958 }
2959
2960 return TRUE;
2961 }
2962
2963 /* Make the backend pick a good value for a dynamic symbol. This is
2964 called via elf_link_hash_traverse, and also calls itself
2965 recursively. */
2966
2967 static bfd_boolean
2968 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2969 {
2970 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2971 struct elf_link_hash_table *htab;
2972 const struct elf_backend_data *bed;
2973
2974 if (! is_elf_hash_table (eif->info->hash))
2975 return FALSE;
2976
2977 /* Ignore indirect symbols. These are added by the versioning code. */
2978 if (h->root.type == bfd_link_hash_indirect)
2979 return TRUE;
2980
2981 /* Fix the symbol flags. */
2982 if (! _bfd_elf_fix_symbol_flags (h, eif))
2983 return FALSE;
2984
2985 htab = elf_hash_table (eif->info);
2986 bed = get_elf_backend_data (htab->dynobj);
2987
2988 if (h->root.type == bfd_link_hash_undefweak)
2989 {
2990 if (eif->info->dynamic_undefined_weak == 0)
2991 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2992 else if (eif->info->dynamic_undefined_weak > 0
2993 && h->ref_regular
2994 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2995 && !bfd_hide_sym_by_version (eif->info->version_info,
2996 h->root.root.string))
2997 {
2998 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999 {
3000 eif->failed = TRUE;
3001 return FALSE;
3002 }
3003 }
3004 }
3005
3006 /* If this symbol does not require a PLT entry, and it is not
3007 defined by a dynamic object, or is not referenced by a regular
3008 object, ignore it. We do have to handle a weak defined symbol,
3009 even if no regular object refers to it, if we decided to add it
3010 to the dynamic symbol table. FIXME: Do we normally need to worry
3011 about symbols which are defined by one dynamic object and
3012 referenced by another one? */
3013 if (!h->needs_plt
3014 && h->type != STT_GNU_IFUNC
3015 && (h->def_regular
3016 || !h->def_dynamic
3017 || (!h->ref_regular
3018 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3019 {
3020 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3021 return TRUE;
3022 }
3023
3024 /* If we've already adjusted this symbol, don't do it again. This
3025 can happen via a recursive call. */
3026 if (h->dynamic_adjusted)
3027 return TRUE;
3028
3029 /* Don't look at this symbol again. Note that we must set this
3030 after checking the above conditions, because we may look at a
3031 symbol once, decide not to do anything, and then get called
3032 recursively later after REF_REGULAR is set below. */
3033 h->dynamic_adjusted = 1;
3034
3035 /* If this is a weak definition, and we know a real definition, and
3036 the real symbol is not itself defined by a regular object file,
3037 then get a good value for the real definition. We handle the
3038 real symbol first, for the convenience of the backend routine.
3039
3040 Note that there is a confusing case here. If the real definition
3041 is defined by a regular object file, we don't get the real symbol
3042 from the dynamic object, but we do get the weak symbol. If the
3043 processor backend uses a COPY reloc, then if some routine in the
3044 dynamic object changes the real symbol, we will not see that
3045 change in the corresponding weak symbol. This is the way other
3046 ELF linkers work as well, and seems to be a result of the shared
3047 library model.
3048
3049 I will clarify this issue. Most SVR4 shared libraries define the
3050 variable _timezone and define timezone as a weak synonym. The
3051 tzset call changes _timezone. If you write
3052 extern int timezone;
3053 int _timezone = 5;
3054 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3055 you might expect that, since timezone is a synonym for _timezone,
3056 the same number will print both times. However, if the processor
3057 backend uses a COPY reloc, then actually timezone will be copied
3058 into your process image, and, since you define _timezone
3059 yourself, _timezone will not. Thus timezone and _timezone will
3060 wind up at different memory locations. The tzset call will set
3061 _timezone, leaving timezone unchanged. */
3062
3063 if (h->is_weakalias)
3064 {
3065 struct elf_link_hash_entry *def = weakdef (h);
3066
3067 /* If we get to this point, there is an implicit reference to
3068 the alias by a regular object file via the weak symbol H. */
3069 def->ref_regular = 1;
3070
3071 /* Ensure that the backend adjust_dynamic_symbol function sees
3072 the strong alias before H by recursively calling ourselves. */
3073 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3074 return FALSE;
3075 }
3076
3077 /* If a symbol has no type and no size and does not require a PLT
3078 entry, then we are probably about to do the wrong thing here: we
3079 are probably going to create a COPY reloc for an empty object.
3080 This case can arise when a shared object is built with assembly
3081 code, and the assembly code fails to set the symbol type. */
3082 if (h->size == 0
3083 && h->type == STT_NOTYPE
3084 && !h->needs_plt)
3085 _bfd_error_handler
3086 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3087 h->root.root.string);
3088
3089 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3090 {
3091 eif->failed = TRUE;
3092 return FALSE;
3093 }
3094
3095 return TRUE;
3096 }
3097
3098 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3099 DYNBSS. */
3100
3101 bfd_boolean
3102 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3103 struct elf_link_hash_entry *h,
3104 asection *dynbss)
3105 {
3106 unsigned int power_of_two;
3107 bfd_vma mask;
3108 asection *sec = h->root.u.def.section;
3109
3110 /* The section alignment of the definition is the maximum alignment
3111 requirement of symbols defined in the section. Since we don't
3112 know the symbol alignment requirement, we start with the
3113 maximum alignment and check low bits of the symbol address
3114 for the minimum alignment. */
3115 power_of_two = bfd_section_alignment (sec);
3116 mask = ((bfd_vma) 1 << power_of_two) - 1;
3117 while ((h->root.u.def.value & mask) != 0)
3118 {
3119 mask >>= 1;
3120 --power_of_two;
3121 }
3122
3123 if (power_of_two > bfd_section_alignment (dynbss))
3124 {
3125 /* Adjust the section alignment if needed. */
3126 if (!bfd_set_section_alignment (dynbss, power_of_two))
3127 return FALSE;
3128 }
3129
3130 /* We make sure that the symbol will be aligned properly. */
3131 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3132
3133 /* Define the symbol as being at this point in DYNBSS. */
3134 h->root.u.def.section = dynbss;
3135 h->root.u.def.value = dynbss->size;
3136
3137 /* Increment the size of DYNBSS to make room for the symbol. */
3138 dynbss->size += h->size;
3139
3140 /* No error if extern_protected_data is true. */
3141 if (h->protected_def
3142 && (!info->extern_protected_data
3143 || (info->extern_protected_data < 0
3144 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3145 info->callbacks->einfo
3146 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3147 h->root.root.string);
3148
3149 return TRUE;
3150 }
3151
3152 /* Adjust all external symbols pointing into SEC_MERGE sections
3153 to reflect the object merging within the sections. */
3154
3155 static bfd_boolean
3156 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3157 {
3158 asection *sec;
3159
3160 if ((h->root.type == bfd_link_hash_defined
3161 || h->root.type == bfd_link_hash_defweak)
3162 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3163 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3164 {
3165 bfd *output_bfd = (bfd *) data;
3166
3167 h->root.u.def.value =
3168 _bfd_merged_section_offset (output_bfd,
3169 &h->root.u.def.section,
3170 elf_section_data (sec)->sec_info,
3171 h->root.u.def.value);
3172 }
3173
3174 return TRUE;
3175 }
3176
3177 /* Returns false if the symbol referred to by H should be considered
3178 to resolve local to the current module, and true if it should be
3179 considered to bind dynamically. */
3180
3181 bfd_boolean
3182 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3183 struct bfd_link_info *info,
3184 bfd_boolean not_local_protected)
3185 {
3186 bfd_boolean binding_stays_local_p;
3187 const struct elf_backend_data *bed;
3188 struct elf_link_hash_table *hash_table;
3189
3190 if (h == NULL)
3191 return FALSE;
3192
3193 while (h->root.type == bfd_link_hash_indirect
3194 || h->root.type == bfd_link_hash_warning)
3195 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3196
3197 /* If it was forced local, then clearly it's not dynamic. */
3198 if (h->dynindx == -1)
3199 return FALSE;
3200 if (h->forced_local)
3201 return FALSE;
3202
3203 /* Identify the cases where name binding rules say that a
3204 visible symbol resolves locally. */
3205 binding_stays_local_p = (bfd_link_executable (info)
3206 || SYMBOLIC_BIND (info, h));
3207
3208 switch (ELF_ST_VISIBILITY (h->other))
3209 {
3210 case STV_INTERNAL:
3211 case STV_HIDDEN:
3212 return FALSE;
3213
3214 case STV_PROTECTED:
3215 hash_table = elf_hash_table (info);
3216 if (!is_elf_hash_table (hash_table))
3217 return FALSE;
3218
3219 bed = get_elf_backend_data (hash_table->dynobj);
3220
3221 /* Proper resolution for function pointer equality may require
3222 that these symbols perhaps be resolved dynamically, even though
3223 we should be resolving them to the current module. */
3224 if (!not_local_protected || !bed->is_function_type (h->type))
3225 binding_stays_local_p = TRUE;
3226 break;
3227
3228 default:
3229 break;
3230 }
3231
3232 /* If it isn't defined locally, then clearly it's dynamic. */
3233 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3234 return TRUE;
3235
3236 /* Otherwise, the symbol is dynamic if binding rules don't tell
3237 us that it remains local. */
3238 return !binding_stays_local_p;
3239 }
3240
3241 /* Return true if the symbol referred to by H should be considered
3242 to resolve local to the current module, and false otherwise. Differs
3243 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3244 undefined symbols. The two functions are virtually identical except
3245 for the place where dynindx == -1 is tested. If that test is true,
3246 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3247 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3248 defined symbols.
3249 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3250 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3251 treatment of undefined weak symbols. For those that do not make
3252 undefined weak symbols dynamic, both functions may return false. */
3253
3254 bfd_boolean
3255 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3256 struct bfd_link_info *info,
3257 bfd_boolean local_protected)
3258 {
3259 const struct elf_backend_data *bed;
3260 struct elf_link_hash_table *hash_table;
3261
3262 /* If it's a local sym, of course we resolve locally. */
3263 if (h == NULL)
3264 return TRUE;
3265
3266 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3267 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3268 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3269 return TRUE;
3270
3271 /* Forced local symbols resolve locally. */
3272 if (h->forced_local)
3273 return TRUE;
3274
3275 /* Common symbols that become definitions don't get the DEF_REGULAR
3276 flag set, so test it first, and don't bail out. */
3277 if (ELF_COMMON_DEF_P (h))
3278 /* Do nothing. */;
3279 /* If we don't have a definition in a regular file, then we can't
3280 resolve locally. The sym is either undefined or dynamic. */
3281 else if (!h->def_regular)
3282 return FALSE;
3283
3284 /* Non-dynamic symbols resolve locally. */
3285 if (h->dynindx == -1)
3286 return TRUE;
3287
3288 /* At this point, we know the symbol is defined and dynamic. In an
3289 executable it must resolve locally, likewise when building symbolic
3290 shared libraries. */
3291 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3292 return TRUE;
3293
3294 /* Now deal with defined dynamic symbols in shared libraries. Ones
3295 with default visibility might not resolve locally. */
3296 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3297 return FALSE;
3298
3299 hash_table = elf_hash_table (info);
3300 if (!is_elf_hash_table (hash_table))
3301 return TRUE;
3302
3303 bed = get_elf_backend_data (hash_table->dynobj);
3304
3305 /* If extern_protected_data is false, STV_PROTECTED non-function
3306 symbols are local. */
3307 if ((!info->extern_protected_data
3308 || (info->extern_protected_data < 0
3309 && !bed->extern_protected_data))
3310 && !bed->is_function_type (h->type))
3311 return TRUE;
3312
3313 /* Function pointer equality tests may require that STV_PROTECTED
3314 symbols be treated as dynamic symbols. If the address of a
3315 function not defined in an executable is set to that function's
3316 plt entry in the executable, then the address of the function in
3317 a shared library must also be the plt entry in the executable. */
3318 return local_protected;
3319 }
3320
3321 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3322 aligned. Returns the first TLS output section. */
3323
3324 struct bfd_section *
3325 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3326 {
3327 struct bfd_section *sec, *tls;
3328 unsigned int align = 0;
3329
3330 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3331 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3332 break;
3333 tls = sec;
3334
3335 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3336 if (sec->alignment_power > align)
3337 align = sec->alignment_power;
3338
3339 elf_hash_table (info)->tls_sec = tls;
3340
3341 /* Ensure the alignment of the first section (usually .tdata) is the largest
3342 alignment, so that the tls segment starts aligned. */
3343 if (tls != NULL)
3344 tls->alignment_power = align;
3345
3346 return tls;
3347 }
3348
3349 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3350 static bfd_boolean
3351 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3352 Elf_Internal_Sym *sym)
3353 {
3354 const struct elf_backend_data *bed;
3355
3356 /* Local symbols do not count, but target specific ones might. */
3357 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3358 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3359 return FALSE;
3360
3361 bed = get_elf_backend_data (abfd);
3362 /* Function symbols do not count. */
3363 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3364 return FALSE;
3365
3366 /* If the section is undefined, then so is the symbol. */
3367 if (sym->st_shndx == SHN_UNDEF)
3368 return FALSE;
3369
3370 /* If the symbol is defined in the common section, then
3371 it is a common definition and so does not count. */
3372 if (bed->common_definition (sym))
3373 return FALSE;
3374
3375 /* If the symbol is in a target specific section then we
3376 must rely upon the backend to tell us what it is. */
3377 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3378 /* FIXME - this function is not coded yet:
3379
3380 return _bfd_is_global_symbol_definition (abfd, sym);
3381
3382 Instead for now assume that the definition is not global,
3383 Even if this is wrong, at least the linker will behave
3384 in the same way that it used to do. */
3385 return FALSE;
3386
3387 return TRUE;
3388 }
3389
3390 /* Search the symbol table of the archive element of the archive ABFD
3391 whose archive map contains a mention of SYMDEF, and determine if
3392 the symbol is defined in this element. */
3393 static bfd_boolean
3394 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3395 {
3396 Elf_Internal_Shdr * hdr;
3397 size_t symcount;
3398 size_t extsymcount;
3399 size_t extsymoff;
3400 Elf_Internal_Sym *isymbuf;
3401 Elf_Internal_Sym *isym;
3402 Elf_Internal_Sym *isymend;
3403 bfd_boolean result;
3404
3405 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3406 if (abfd == NULL)
3407 return FALSE;
3408
3409 if (! bfd_check_format (abfd, bfd_object))
3410 return FALSE;
3411
3412 /* Select the appropriate symbol table. If we don't know if the
3413 object file is an IR object, give linker LTO plugin a chance to
3414 get the correct symbol table. */
3415 if (abfd->plugin_format == bfd_plugin_yes
3416 #if BFD_SUPPORTS_PLUGINS
3417 || (abfd->plugin_format == bfd_plugin_unknown
3418 && bfd_link_plugin_object_p (abfd))
3419 #endif
3420 )
3421 {
3422 /* Use the IR symbol table if the object has been claimed by
3423 plugin. */
3424 abfd = abfd->plugin_dummy_bfd;
3425 hdr = &elf_tdata (abfd)->symtab_hdr;
3426 }
3427 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3428 hdr = &elf_tdata (abfd)->symtab_hdr;
3429 else
3430 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3431
3432 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3433
3434 /* The sh_info field of the symtab header tells us where the
3435 external symbols start. We don't care about the local symbols. */
3436 if (elf_bad_symtab (abfd))
3437 {
3438 extsymcount = symcount;
3439 extsymoff = 0;
3440 }
3441 else
3442 {
3443 extsymcount = symcount - hdr->sh_info;
3444 extsymoff = hdr->sh_info;
3445 }
3446
3447 if (extsymcount == 0)
3448 return FALSE;
3449
3450 /* Read in the symbol table. */
3451 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3452 NULL, NULL, NULL);
3453 if (isymbuf == NULL)
3454 return FALSE;
3455
3456 /* Scan the symbol table looking for SYMDEF. */
3457 result = FALSE;
3458 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3459 {
3460 const char *name;
3461
3462 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3463 isym->st_name);
3464 if (name == NULL)
3465 break;
3466
3467 if (strcmp (name, symdef->name) == 0)
3468 {
3469 result = is_global_data_symbol_definition (abfd, isym);
3470 break;
3471 }
3472 }
3473
3474 free (isymbuf);
3475
3476 return result;
3477 }
3478 \f
3479 /* Add an entry to the .dynamic table. */
3480
3481 bfd_boolean
3482 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3483 bfd_vma tag,
3484 bfd_vma val)
3485 {
3486 struct elf_link_hash_table *hash_table;
3487 const struct elf_backend_data *bed;
3488 asection *s;
3489 bfd_size_type newsize;
3490 bfd_byte *newcontents;
3491 Elf_Internal_Dyn dyn;
3492
3493 hash_table = elf_hash_table (info);
3494 if (! is_elf_hash_table (hash_table))
3495 return FALSE;
3496
3497 if (tag == DT_RELA || tag == DT_REL)
3498 hash_table->dynamic_relocs = TRUE;
3499
3500 bed = get_elf_backend_data (hash_table->dynobj);
3501 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3502 BFD_ASSERT (s != NULL);
3503
3504 newsize = s->size + bed->s->sizeof_dyn;
3505 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3506 if (newcontents == NULL)
3507 return FALSE;
3508
3509 dyn.d_tag = tag;
3510 dyn.d_un.d_val = val;
3511 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3512
3513 s->size = newsize;
3514 s->contents = newcontents;
3515
3516 return TRUE;
3517 }
3518
3519 /* Strip zero-sized dynamic sections. */
3520
3521 bfd_boolean
3522 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3523 {
3524 struct elf_link_hash_table *hash_table;
3525 const struct elf_backend_data *bed;
3526 asection *s, *sdynamic, **pp;
3527 asection *rela_dyn, *rel_dyn;
3528 Elf_Internal_Dyn dyn;
3529 bfd_byte *extdyn, *next;
3530 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3531 bfd_boolean strip_zero_sized;
3532 bfd_boolean strip_zero_sized_plt;
3533
3534 if (bfd_link_relocatable (info))
3535 return TRUE;
3536
3537 hash_table = elf_hash_table (info);
3538 if (!is_elf_hash_table (hash_table))
3539 return FALSE;
3540
3541 if (!hash_table->dynobj)
3542 return TRUE;
3543
3544 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3545 if (!sdynamic)
3546 return TRUE;
3547
3548 bed = get_elf_backend_data (hash_table->dynobj);
3549 swap_dyn_in = bed->s->swap_dyn_in;
3550
3551 strip_zero_sized = FALSE;
3552 strip_zero_sized_plt = FALSE;
3553
3554 /* Strip zero-sized dynamic sections. */
3555 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3556 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3557 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3558 if (s->size == 0
3559 && (s == rela_dyn
3560 || s == rel_dyn
3561 || s == hash_table->srelplt->output_section
3562 || s == hash_table->splt->output_section))
3563 {
3564 *pp = s->next;
3565 info->output_bfd->section_count--;
3566 strip_zero_sized = TRUE;
3567 if (s == rela_dyn)
3568 s = rela_dyn;
3569 if (s == rel_dyn)
3570 s = rel_dyn;
3571 else if (s == hash_table->splt->output_section)
3572 {
3573 s = hash_table->splt;
3574 strip_zero_sized_plt = TRUE;
3575 }
3576 else
3577 s = hash_table->srelplt;
3578 s->flags |= SEC_EXCLUDE;
3579 s->output_section = bfd_abs_section_ptr;
3580 }
3581 else
3582 pp = &s->next;
3583
3584 if (strip_zero_sized_plt)
3585 for (extdyn = sdynamic->contents;
3586 extdyn < sdynamic->contents + sdynamic->size;
3587 extdyn = next)
3588 {
3589 next = extdyn + bed->s->sizeof_dyn;
3590 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3591 switch (dyn.d_tag)
3592 {
3593 default:
3594 break;
3595 case DT_JMPREL:
3596 case DT_PLTRELSZ:
3597 case DT_PLTREL:
3598 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3599 the procedure linkage table (the .plt section) has been
3600 removed. */
3601 memmove (extdyn, next,
3602 sdynamic->size - (next - sdynamic->contents));
3603 next = extdyn;
3604 }
3605 }
3606
3607 if (strip_zero_sized)
3608 {
3609 /* Regenerate program headers. */
3610 elf_seg_map (info->output_bfd) = NULL;
3611 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3612 }
3613
3614 return TRUE;
3615 }
3616
3617 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3618 1 if a DT_NEEDED tag already exists, and 0 on success. */
3619
3620 int
3621 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3622 {
3623 struct elf_link_hash_table *hash_table;
3624 size_t strindex;
3625 const char *soname;
3626
3627 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3628 return -1;
3629
3630 hash_table = elf_hash_table (info);
3631 soname = elf_dt_name (abfd);
3632 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3633 if (strindex == (size_t) -1)
3634 return -1;
3635
3636 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3637 {
3638 asection *sdyn;
3639 const struct elf_backend_data *bed;
3640 bfd_byte *extdyn;
3641
3642 bed = get_elf_backend_data (hash_table->dynobj);
3643 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3644 if (sdyn != NULL)
3645 for (extdyn = sdyn->contents;
3646 extdyn < sdyn->contents + sdyn->size;
3647 extdyn += bed->s->sizeof_dyn)
3648 {
3649 Elf_Internal_Dyn dyn;
3650
3651 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3652 if (dyn.d_tag == DT_NEEDED
3653 && dyn.d_un.d_val == strindex)
3654 {
3655 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3656 return 1;
3657 }
3658 }
3659 }
3660
3661 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3662 return -1;
3663
3664 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3665 return -1;
3666
3667 return 0;
3668 }
3669
3670 /* Return true if SONAME is on the needed list between NEEDED and STOP
3671 (or the end of list if STOP is NULL), and needed by a library that
3672 will be loaded. */
3673
3674 static bfd_boolean
3675 on_needed_list (const char *soname,
3676 struct bfd_link_needed_list *needed,
3677 struct bfd_link_needed_list *stop)
3678 {
3679 struct bfd_link_needed_list *look;
3680 for (look = needed; look != stop; look = look->next)
3681 if (strcmp (soname, look->name) == 0
3682 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3683 /* If needed by a library that itself is not directly
3684 needed, recursively check whether that library is
3685 indirectly needed. Since we add DT_NEEDED entries to
3686 the end of the list, library dependencies appear after
3687 the library. Therefore search prior to the current
3688 LOOK, preventing possible infinite recursion. */
3689 || on_needed_list (elf_dt_name (look->by), needed, look)))
3690 return TRUE;
3691
3692 return FALSE;
3693 }
3694
3695 /* Sort symbol by value, section, size, and type. */
3696 static int
3697 elf_sort_symbol (const void *arg1, const void *arg2)
3698 {
3699 const struct elf_link_hash_entry *h1;
3700 const struct elf_link_hash_entry *h2;
3701 bfd_signed_vma vdiff;
3702 int sdiff;
3703 const char *n1;
3704 const char *n2;
3705
3706 h1 = *(const struct elf_link_hash_entry **) arg1;
3707 h2 = *(const struct elf_link_hash_entry **) arg2;
3708 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3709 if (vdiff != 0)
3710 return vdiff > 0 ? 1 : -1;
3711
3712 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3713 if (sdiff != 0)
3714 return sdiff;
3715
3716 /* Sort so that sized symbols are selected over zero size symbols. */
3717 vdiff = h1->size - h2->size;
3718 if (vdiff != 0)
3719 return vdiff > 0 ? 1 : -1;
3720
3721 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3722 if (h1->type != h2->type)
3723 return h1->type - h2->type;
3724
3725 /* If symbols are properly sized and typed, and multiple strong
3726 aliases are not defined in a shared library by the user we
3727 shouldn't get here. Unfortunately linker script symbols like
3728 __bss_start sometimes match a user symbol defined at the start of
3729 .bss without proper size and type. We'd like to preference the
3730 user symbol over reserved system symbols. Sort on leading
3731 underscores. */
3732 n1 = h1->root.root.string;
3733 n2 = h2->root.root.string;
3734 while (*n1 == *n2)
3735 {
3736 if (*n1 == 0)
3737 break;
3738 ++n1;
3739 ++n2;
3740 }
3741 if (*n1 == '_')
3742 return -1;
3743 if (*n2 == '_')
3744 return 1;
3745
3746 /* Final sort on name selects user symbols like '_u' over reserved
3747 system symbols like '_Z' and also will avoid qsort instability. */
3748 return *n1 - *n2;
3749 }
3750
3751 /* This function is used to adjust offsets into .dynstr for
3752 dynamic symbols. This is called via elf_link_hash_traverse. */
3753
3754 static bfd_boolean
3755 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3756 {
3757 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3758
3759 if (h->dynindx != -1)
3760 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3761 return TRUE;
3762 }
3763
3764 /* Assign string offsets in .dynstr, update all structures referencing
3765 them. */
3766
3767 static bfd_boolean
3768 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3769 {
3770 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3771 struct elf_link_local_dynamic_entry *entry;
3772 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3773 bfd *dynobj = hash_table->dynobj;
3774 asection *sdyn;
3775 bfd_size_type size;
3776 const struct elf_backend_data *bed;
3777 bfd_byte *extdyn;
3778
3779 _bfd_elf_strtab_finalize (dynstr);
3780 size = _bfd_elf_strtab_size (dynstr);
3781
3782 bed = get_elf_backend_data (dynobj);
3783 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3784 BFD_ASSERT (sdyn != NULL);
3785
3786 /* Update all .dynamic entries referencing .dynstr strings. */
3787 for (extdyn = sdyn->contents;
3788 extdyn < sdyn->contents + sdyn->size;
3789 extdyn += bed->s->sizeof_dyn)
3790 {
3791 Elf_Internal_Dyn dyn;
3792
3793 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3794 switch (dyn.d_tag)
3795 {
3796 case DT_STRSZ:
3797 dyn.d_un.d_val = size;
3798 break;
3799 case DT_NEEDED:
3800 case DT_SONAME:
3801 case DT_RPATH:
3802 case DT_RUNPATH:
3803 case DT_FILTER:
3804 case DT_AUXILIARY:
3805 case DT_AUDIT:
3806 case DT_DEPAUDIT:
3807 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3808 break;
3809 default:
3810 continue;
3811 }
3812 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3813 }
3814
3815 /* Now update local dynamic symbols. */
3816 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3817 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3818 entry->isym.st_name);
3819
3820 /* And the rest of dynamic symbols. */
3821 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3822
3823 /* Adjust version definitions. */
3824 if (elf_tdata (output_bfd)->cverdefs)
3825 {
3826 asection *s;
3827 bfd_byte *p;
3828 size_t i;
3829 Elf_Internal_Verdef def;
3830 Elf_Internal_Verdaux defaux;
3831
3832 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3833 p = s->contents;
3834 do
3835 {
3836 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3837 &def);
3838 p += sizeof (Elf_External_Verdef);
3839 if (def.vd_aux != sizeof (Elf_External_Verdef))
3840 continue;
3841 for (i = 0; i < def.vd_cnt; ++i)
3842 {
3843 _bfd_elf_swap_verdaux_in (output_bfd,
3844 (Elf_External_Verdaux *) p, &defaux);
3845 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3846 defaux.vda_name);
3847 _bfd_elf_swap_verdaux_out (output_bfd,
3848 &defaux, (Elf_External_Verdaux *) p);
3849 p += sizeof (Elf_External_Verdaux);
3850 }
3851 }
3852 while (def.vd_next);
3853 }
3854
3855 /* Adjust version references. */
3856 if (elf_tdata (output_bfd)->verref)
3857 {
3858 asection *s;
3859 bfd_byte *p;
3860 size_t i;
3861 Elf_Internal_Verneed need;
3862 Elf_Internal_Vernaux needaux;
3863
3864 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3865 p = s->contents;
3866 do
3867 {
3868 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3869 &need);
3870 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3871 _bfd_elf_swap_verneed_out (output_bfd, &need,
3872 (Elf_External_Verneed *) p);
3873 p += sizeof (Elf_External_Verneed);
3874 for (i = 0; i < need.vn_cnt; ++i)
3875 {
3876 _bfd_elf_swap_vernaux_in (output_bfd,
3877 (Elf_External_Vernaux *) p, &needaux);
3878 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3879 needaux.vna_name);
3880 _bfd_elf_swap_vernaux_out (output_bfd,
3881 &needaux,
3882 (Elf_External_Vernaux *) p);
3883 p += sizeof (Elf_External_Vernaux);
3884 }
3885 }
3886 while (need.vn_next);
3887 }
3888
3889 return TRUE;
3890 }
3891 \f
3892 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3893 The default is to only match when the INPUT and OUTPUT are exactly
3894 the same target. */
3895
3896 bfd_boolean
3897 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3898 const bfd_target *output)
3899 {
3900 return input == output;
3901 }
3902
3903 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3904 This version is used when different targets for the same architecture
3905 are virtually identical. */
3906
3907 bfd_boolean
3908 _bfd_elf_relocs_compatible (const bfd_target *input,
3909 const bfd_target *output)
3910 {
3911 const struct elf_backend_data *obed, *ibed;
3912
3913 if (input == output)
3914 return TRUE;
3915
3916 ibed = xvec_get_elf_backend_data (input);
3917 obed = xvec_get_elf_backend_data (output);
3918
3919 if (ibed->arch != obed->arch)
3920 return FALSE;
3921
3922 /* If both backends are using this function, deem them compatible. */
3923 return ibed->relocs_compatible == obed->relocs_compatible;
3924 }
3925
3926 /* Make a special call to the linker "notice" function to tell it that
3927 we are about to handle an as-needed lib, or have finished
3928 processing the lib. */
3929
3930 bfd_boolean
3931 _bfd_elf_notice_as_needed (bfd *ibfd,
3932 struct bfd_link_info *info,
3933 enum notice_asneeded_action act)
3934 {
3935 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3936 }
3937
3938 /* Check relocations an ELF object file. */
3939
3940 bfd_boolean
3941 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3942 {
3943 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3944 struct elf_link_hash_table *htab = elf_hash_table (info);
3945
3946 /* If this object is the same format as the output object, and it is
3947 not a shared library, then let the backend look through the
3948 relocs.
3949
3950 This is required to build global offset table entries and to
3951 arrange for dynamic relocs. It is not required for the
3952 particular common case of linking non PIC code, even when linking
3953 against shared libraries, but unfortunately there is no way of
3954 knowing whether an object file has been compiled PIC or not.
3955 Looking through the relocs is not particularly time consuming.
3956 The problem is that we must either (1) keep the relocs in memory,
3957 which causes the linker to require additional runtime memory or
3958 (2) read the relocs twice from the input file, which wastes time.
3959 This would be a good case for using mmap.
3960
3961 I have no idea how to handle linking PIC code into a file of a
3962 different format. It probably can't be done. */
3963 if ((abfd->flags & DYNAMIC) == 0
3964 && is_elf_hash_table (htab)
3965 && bed->check_relocs != NULL
3966 && elf_object_id (abfd) == elf_hash_table_id (htab)
3967 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3968 {
3969 asection *o;
3970
3971 for (o = abfd->sections; o != NULL; o = o->next)
3972 {
3973 Elf_Internal_Rela *internal_relocs;
3974 bfd_boolean ok;
3975
3976 /* Don't check relocations in excluded sections. Don't do
3977 anything special with non-loaded, non-alloced sections.
3978 In particular, any relocs in such sections should not
3979 affect GOT and PLT reference counting (ie. we don't
3980 allow them to create GOT or PLT entries), there's no
3981 possibility or desire to optimize TLS relocs, and
3982 there's not much point in propagating relocs to shared
3983 libs that the dynamic linker won't relocate. */
3984 if ((o->flags & SEC_ALLOC) == 0
3985 || (o->flags & SEC_RELOC) == 0
3986 || (o->flags & SEC_EXCLUDE) != 0
3987 || o->reloc_count == 0
3988 || ((info->strip == strip_all || info->strip == strip_debugger)
3989 && (o->flags & SEC_DEBUGGING) != 0)
3990 || bfd_is_abs_section (o->output_section))
3991 continue;
3992
3993 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3994 info->keep_memory);
3995 if (internal_relocs == NULL)
3996 return FALSE;
3997
3998 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3999
4000 if (elf_section_data (o)->relocs != internal_relocs)
4001 free (internal_relocs);
4002
4003 if (! ok)
4004 return FALSE;
4005 }
4006 }
4007
4008 return TRUE;
4009 }
4010
4011 /* Add symbols from an ELF object file to the linker hash table. */
4012
4013 static bfd_boolean
4014 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4015 {
4016 Elf_Internal_Ehdr *ehdr;
4017 Elf_Internal_Shdr *hdr;
4018 size_t symcount;
4019 size_t extsymcount;
4020 size_t extsymoff;
4021 struct elf_link_hash_entry **sym_hash;
4022 bfd_boolean dynamic;
4023 Elf_External_Versym *extversym = NULL;
4024 Elf_External_Versym *extversym_end = NULL;
4025 Elf_External_Versym *ever;
4026 struct elf_link_hash_entry *weaks;
4027 struct elf_link_hash_entry **nondeflt_vers = NULL;
4028 size_t nondeflt_vers_cnt = 0;
4029 Elf_Internal_Sym *isymbuf = NULL;
4030 Elf_Internal_Sym *isym;
4031 Elf_Internal_Sym *isymend;
4032 const struct elf_backend_data *bed;
4033 bfd_boolean add_needed;
4034 struct elf_link_hash_table *htab;
4035 void *alloc_mark = NULL;
4036 struct bfd_hash_entry **old_table = NULL;
4037 unsigned int old_size = 0;
4038 unsigned int old_count = 0;
4039 void *old_tab = NULL;
4040 void *old_ent;
4041 struct bfd_link_hash_entry *old_undefs = NULL;
4042 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4043 void *old_strtab = NULL;
4044 size_t tabsize = 0;
4045 asection *s;
4046 bfd_boolean just_syms;
4047
4048 htab = elf_hash_table (info);
4049 bed = get_elf_backend_data (abfd);
4050
4051 if ((abfd->flags & DYNAMIC) == 0)
4052 dynamic = FALSE;
4053 else
4054 {
4055 dynamic = TRUE;
4056
4057 /* You can't use -r against a dynamic object. Also, there's no
4058 hope of using a dynamic object which does not exactly match
4059 the format of the output file. */
4060 if (bfd_link_relocatable (info)
4061 || !is_elf_hash_table (htab)
4062 || info->output_bfd->xvec != abfd->xvec)
4063 {
4064 if (bfd_link_relocatable (info))
4065 bfd_set_error (bfd_error_invalid_operation);
4066 else
4067 bfd_set_error (bfd_error_wrong_format);
4068 goto error_return;
4069 }
4070 }
4071
4072 ehdr = elf_elfheader (abfd);
4073 if (info->warn_alternate_em
4074 && bed->elf_machine_code != ehdr->e_machine
4075 && ((bed->elf_machine_alt1 != 0
4076 && ehdr->e_machine == bed->elf_machine_alt1)
4077 || (bed->elf_machine_alt2 != 0
4078 && ehdr->e_machine == bed->elf_machine_alt2)))
4079 _bfd_error_handler
4080 /* xgettext:c-format */
4081 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4082 ehdr->e_machine, abfd, bed->elf_machine_code);
4083
4084 /* As a GNU extension, any input sections which are named
4085 .gnu.warning.SYMBOL are treated as warning symbols for the given
4086 symbol. This differs from .gnu.warning sections, which generate
4087 warnings when they are included in an output file. */
4088 /* PR 12761: Also generate this warning when building shared libraries. */
4089 for (s = abfd->sections; s != NULL; s = s->next)
4090 {
4091 const char *name;
4092
4093 name = bfd_section_name (s);
4094 if (CONST_STRNEQ (name, ".gnu.warning."))
4095 {
4096 char *msg;
4097 bfd_size_type sz;
4098
4099 name += sizeof ".gnu.warning." - 1;
4100
4101 /* If this is a shared object, then look up the symbol
4102 in the hash table. If it is there, and it is already
4103 been defined, then we will not be using the entry
4104 from this shared object, so we don't need to warn.
4105 FIXME: If we see the definition in a regular object
4106 later on, we will warn, but we shouldn't. The only
4107 fix is to keep track of what warnings we are supposed
4108 to emit, and then handle them all at the end of the
4109 link. */
4110 if (dynamic)
4111 {
4112 struct elf_link_hash_entry *h;
4113
4114 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4115
4116 /* FIXME: What about bfd_link_hash_common? */
4117 if (h != NULL
4118 && (h->root.type == bfd_link_hash_defined
4119 || h->root.type == bfd_link_hash_defweak))
4120 continue;
4121 }
4122
4123 sz = s->size;
4124 msg = (char *) bfd_alloc (abfd, sz + 1);
4125 if (msg == NULL)
4126 goto error_return;
4127
4128 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4129 goto error_return;
4130
4131 msg[sz] = '\0';
4132
4133 if (! (_bfd_generic_link_add_one_symbol
4134 (info, abfd, name, BSF_WARNING, s, 0, msg,
4135 FALSE, bed->collect, NULL)))
4136 goto error_return;
4137
4138 if (bfd_link_executable (info))
4139 {
4140 /* Clobber the section size so that the warning does
4141 not get copied into the output file. */
4142 s->size = 0;
4143
4144 /* Also set SEC_EXCLUDE, so that symbols defined in
4145 the warning section don't get copied to the output. */
4146 s->flags |= SEC_EXCLUDE;
4147 }
4148 }
4149 }
4150
4151 just_syms = ((s = abfd->sections) != NULL
4152 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4153
4154 add_needed = TRUE;
4155 if (! dynamic)
4156 {
4157 /* If we are creating a shared library, create all the dynamic
4158 sections immediately. We need to attach them to something,
4159 so we attach them to this BFD, provided it is the right
4160 format and is not from ld --just-symbols. Always create the
4161 dynamic sections for -E/--dynamic-list. FIXME: If there
4162 are no input BFD's of the same format as the output, we can't
4163 make a shared library. */
4164 if (!just_syms
4165 && (bfd_link_pic (info)
4166 || (!bfd_link_relocatable (info)
4167 && info->nointerp
4168 && (info->export_dynamic || info->dynamic)))
4169 && is_elf_hash_table (htab)
4170 && info->output_bfd->xvec == abfd->xvec
4171 && !htab->dynamic_sections_created)
4172 {
4173 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4174 goto error_return;
4175 }
4176 }
4177 else if (!is_elf_hash_table (htab))
4178 goto error_return;
4179 else
4180 {
4181 const char *soname = NULL;
4182 char *audit = NULL;
4183 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4184 const Elf_Internal_Phdr *phdr;
4185 struct elf_link_loaded_list *loaded_lib;
4186
4187 /* ld --just-symbols and dynamic objects don't mix very well.
4188 ld shouldn't allow it. */
4189 if (just_syms)
4190 abort ();
4191
4192 /* If this dynamic lib was specified on the command line with
4193 --as-needed in effect, then we don't want to add a DT_NEEDED
4194 tag unless the lib is actually used. Similary for libs brought
4195 in by another lib's DT_NEEDED. When --no-add-needed is used
4196 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4197 any dynamic library in DT_NEEDED tags in the dynamic lib at
4198 all. */
4199 add_needed = (elf_dyn_lib_class (abfd)
4200 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4201 | DYN_NO_NEEDED)) == 0;
4202
4203 s = bfd_get_section_by_name (abfd, ".dynamic");
4204 if (s != NULL)
4205 {
4206 bfd_byte *dynbuf;
4207 bfd_byte *extdyn;
4208 unsigned int elfsec;
4209 unsigned long shlink;
4210
4211 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4212 {
4213 error_free_dyn:
4214 free (dynbuf);
4215 goto error_return;
4216 }
4217
4218 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4219 if (elfsec == SHN_BAD)
4220 goto error_free_dyn;
4221 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4222
4223 for (extdyn = dynbuf;
4224 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4225 extdyn += bed->s->sizeof_dyn)
4226 {
4227 Elf_Internal_Dyn dyn;
4228
4229 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4230 if (dyn.d_tag == DT_SONAME)
4231 {
4232 unsigned int tagv = dyn.d_un.d_val;
4233 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4234 if (soname == NULL)
4235 goto error_free_dyn;
4236 }
4237 if (dyn.d_tag == DT_NEEDED)
4238 {
4239 struct bfd_link_needed_list *n, **pn;
4240 char *fnm, *anm;
4241 unsigned int tagv = dyn.d_un.d_val;
4242 size_t amt = sizeof (struct bfd_link_needed_list);
4243
4244 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4245 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4246 if (n == NULL || fnm == NULL)
4247 goto error_free_dyn;
4248 amt = strlen (fnm) + 1;
4249 anm = (char *) bfd_alloc (abfd, amt);
4250 if (anm == NULL)
4251 goto error_free_dyn;
4252 memcpy (anm, fnm, amt);
4253 n->name = anm;
4254 n->by = abfd;
4255 n->next = NULL;
4256 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4257 ;
4258 *pn = n;
4259 }
4260 if (dyn.d_tag == DT_RUNPATH)
4261 {
4262 struct bfd_link_needed_list *n, **pn;
4263 char *fnm, *anm;
4264 unsigned int tagv = dyn.d_un.d_val;
4265 size_t amt = sizeof (struct bfd_link_needed_list);
4266
4267 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4268 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4269 if (n == NULL || fnm == NULL)
4270 goto error_free_dyn;
4271 amt = strlen (fnm) + 1;
4272 anm = (char *) bfd_alloc (abfd, amt);
4273 if (anm == NULL)
4274 goto error_free_dyn;
4275 memcpy (anm, fnm, amt);
4276 n->name = anm;
4277 n->by = abfd;
4278 n->next = NULL;
4279 for (pn = & runpath;
4280 *pn != NULL;
4281 pn = &(*pn)->next)
4282 ;
4283 *pn = n;
4284 }
4285 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4286 if (!runpath && dyn.d_tag == DT_RPATH)
4287 {
4288 struct bfd_link_needed_list *n, **pn;
4289 char *fnm, *anm;
4290 unsigned int tagv = dyn.d_un.d_val;
4291 size_t amt = sizeof (struct bfd_link_needed_list);
4292
4293 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4294 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4295 if (n == NULL || fnm == NULL)
4296 goto error_free_dyn;
4297 amt = strlen (fnm) + 1;
4298 anm = (char *) bfd_alloc (abfd, amt);
4299 if (anm == NULL)
4300 goto error_free_dyn;
4301 memcpy (anm, fnm, amt);
4302 n->name = anm;
4303 n->by = abfd;
4304 n->next = NULL;
4305 for (pn = & rpath;
4306 *pn != NULL;
4307 pn = &(*pn)->next)
4308 ;
4309 *pn = n;
4310 }
4311 if (dyn.d_tag == DT_AUDIT)
4312 {
4313 unsigned int tagv = dyn.d_un.d_val;
4314 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4315 }
4316 }
4317
4318 free (dynbuf);
4319 }
4320
4321 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4322 frees all more recently bfd_alloc'd blocks as well. */
4323 if (runpath)
4324 rpath = runpath;
4325
4326 if (rpath)
4327 {
4328 struct bfd_link_needed_list **pn;
4329 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4330 ;
4331 *pn = rpath;
4332 }
4333
4334 /* If we have a PT_GNU_RELRO program header, mark as read-only
4335 all sections contained fully therein. This makes relro
4336 shared library sections appear as they will at run-time. */
4337 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4338 while (phdr-- > elf_tdata (abfd)->phdr)
4339 if (phdr->p_type == PT_GNU_RELRO)
4340 {
4341 for (s = abfd->sections; s != NULL; s = s->next)
4342 {
4343 unsigned int opb = bfd_octets_per_byte (abfd, s);
4344
4345 if ((s->flags & SEC_ALLOC) != 0
4346 && s->vma * opb >= phdr->p_vaddr
4347 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4348 s->flags |= SEC_READONLY;
4349 }
4350 break;
4351 }
4352
4353 /* We do not want to include any of the sections in a dynamic
4354 object in the output file. We hack by simply clobbering the
4355 list of sections in the BFD. This could be handled more
4356 cleanly by, say, a new section flag; the existing
4357 SEC_NEVER_LOAD flag is not the one we want, because that one
4358 still implies that the section takes up space in the output
4359 file. */
4360 bfd_section_list_clear (abfd);
4361
4362 /* Find the name to use in a DT_NEEDED entry that refers to this
4363 object. If the object has a DT_SONAME entry, we use it.
4364 Otherwise, if the generic linker stuck something in
4365 elf_dt_name, we use that. Otherwise, we just use the file
4366 name. */
4367 if (soname == NULL || *soname == '\0')
4368 {
4369 soname = elf_dt_name (abfd);
4370 if (soname == NULL || *soname == '\0')
4371 soname = bfd_get_filename (abfd);
4372 }
4373
4374 /* Save the SONAME because sometimes the linker emulation code
4375 will need to know it. */
4376 elf_dt_name (abfd) = soname;
4377
4378 /* If we have already included this dynamic object in the
4379 link, just ignore it. There is no reason to include a
4380 particular dynamic object more than once. */
4381 for (loaded_lib = htab->dyn_loaded;
4382 loaded_lib != NULL;
4383 loaded_lib = loaded_lib->next)
4384 {
4385 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4386 return TRUE;
4387 }
4388
4389 /* Create dynamic sections for backends that require that be done
4390 before setup_gnu_properties. */
4391 if (add_needed
4392 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4393 return FALSE;
4394
4395 /* Save the DT_AUDIT entry for the linker emulation code. */
4396 elf_dt_audit (abfd) = audit;
4397 }
4398
4399 /* If this is a dynamic object, we always link against the .dynsym
4400 symbol table, not the .symtab symbol table. The dynamic linker
4401 will only see the .dynsym symbol table, so there is no reason to
4402 look at .symtab for a dynamic object. */
4403
4404 if (! dynamic || elf_dynsymtab (abfd) == 0)
4405 hdr = &elf_tdata (abfd)->symtab_hdr;
4406 else
4407 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4408
4409 symcount = hdr->sh_size / bed->s->sizeof_sym;
4410
4411 /* The sh_info field of the symtab header tells us where the
4412 external symbols start. We don't care about the local symbols at
4413 this point. */
4414 if (elf_bad_symtab (abfd))
4415 {
4416 extsymcount = symcount;
4417 extsymoff = 0;
4418 }
4419 else
4420 {
4421 extsymcount = symcount - hdr->sh_info;
4422 extsymoff = hdr->sh_info;
4423 }
4424
4425 sym_hash = elf_sym_hashes (abfd);
4426 if (extsymcount != 0)
4427 {
4428 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4429 NULL, NULL, NULL);
4430 if (isymbuf == NULL)
4431 goto error_return;
4432
4433 if (sym_hash == NULL)
4434 {
4435 /* We store a pointer to the hash table entry for each
4436 external symbol. */
4437 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4438 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4439 if (sym_hash == NULL)
4440 goto error_free_sym;
4441 elf_sym_hashes (abfd) = sym_hash;
4442 }
4443 }
4444
4445 if (dynamic)
4446 {
4447 /* Read in any version definitions. */
4448 if (!_bfd_elf_slurp_version_tables (abfd,
4449 info->default_imported_symver))
4450 goto error_free_sym;
4451
4452 /* Read in the symbol versions, but don't bother to convert them
4453 to internal format. */
4454 if (elf_dynversym (abfd) != 0)
4455 {
4456 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4457 bfd_size_type amt = versymhdr->sh_size;
4458
4459 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4460 goto error_free_sym;
4461 extversym = (Elf_External_Versym *)
4462 _bfd_malloc_and_read (abfd, amt, amt);
4463 if (extversym == NULL)
4464 goto error_free_sym;
4465 extversym_end = extversym + amt / sizeof (*extversym);
4466 }
4467 }
4468
4469 /* If we are loading an as-needed shared lib, save the symbol table
4470 state before we start adding symbols. If the lib turns out
4471 to be unneeded, restore the state. */
4472 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4473 {
4474 unsigned int i;
4475 size_t entsize;
4476
4477 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4478 {
4479 struct bfd_hash_entry *p;
4480 struct elf_link_hash_entry *h;
4481
4482 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4483 {
4484 h = (struct elf_link_hash_entry *) p;
4485 entsize += htab->root.table.entsize;
4486 if (h->root.type == bfd_link_hash_warning)
4487 {
4488 entsize += htab->root.table.entsize;
4489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4490 }
4491 if (h->root.type == bfd_link_hash_common)
4492 entsize += sizeof (*h->root.u.c.p);
4493 }
4494 }
4495
4496 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4497 old_tab = bfd_malloc (tabsize + entsize);
4498 if (old_tab == NULL)
4499 goto error_free_vers;
4500
4501 /* Remember the current objalloc pointer, so that all mem for
4502 symbols added can later be reclaimed. */
4503 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4504 if (alloc_mark == NULL)
4505 goto error_free_vers;
4506
4507 /* Make a special call to the linker "notice" function to
4508 tell it that we are about to handle an as-needed lib. */
4509 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4510 goto error_free_vers;
4511
4512 /* Clone the symbol table. Remember some pointers into the
4513 symbol table, and dynamic symbol count. */
4514 old_ent = (char *) old_tab + tabsize;
4515 memcpy (old_tab, htab->root.table.table, tabsize);
4516 old_undefs = htab->root.undefs;
4517 old_undefs_tail = htab->root.undefs_tail;
4518 old_table = htab->root.table.table;
4519 old_size = htab->root.table.size;
4520 old_count = htab->root.table.count;
4521 old_strtab = NULL;
4522 if (htab->dynstr != NULL)
4523 {
4524 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4525 if (old_strtab == NULL)
4526 goto error_free_vers;
4527 }
4528
4529 for (i = 0; i < htab->root.table.size; i++)
4530 {
4531 struct bfd_hash_entry *p;
4532 struct elf_link_hash_entry *h;
4533
4534 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4535 {
4536 h = (struct elf_link_hash_entry *) p;
4537 memcpy (old_ent, h, htab->root.table.entsize);
4538 old_ent = (char *) old_ent + htab->root.table.entsize;
4539 if (h->root.type == bfd_link_hash_warning)
4540 {
4541 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4542 memcpy (old_ent, h, htab->root.table.entsize);
4543 old_ent = (char *) old_ent + htab->root.table.entsize;
4544 }
4545 if (h->root.type == bfd_link_hash_common)
4546 {
4547 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4548 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4549 }
4550 }
4551 }
4552 }
4553
4554 weaks = NULL;
4555 if (extversym == NULL)
4556 ever = NULL;
4557 else if (extversym + extsymoff < extversym_end)
4558 ever = extversym + extsymoff;
4559 else
4560 {
4561 /* xgettext:c-format */
4562 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4563 abfd, (long) extsymoff,
4564 (long) (extversym_end - extversym) / sizeof (* extversym));
4565 bfd_set_error (bfd_error_bad_value);
4566 goto error_free_vers;
4567 }
4568
4569 if (!bfd_link_relocatable (info)
4570 && abfd->lto_slim_object)
4571 {
4572 _bfd_error_handler
4573 (_("%pB: plugin needed to handle lto object"), abfd);
4574 }
4575
4576 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4577 isym < isymend;
4578 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4579 {
4580 int bind;
4581 bfd_vma value;
4582 asection *sec, *new_sec;
4583 flagword flags;
4584 const char *name;
4585 struct elf_link_hash_entry *h;
4586 struct elf_link_hash_entry *hi;
4587 bfd_boolean definition;
4588 bfd_boolean size_change_ok;
4589 bfd_boolean type_change_ok;
4590 bfd_boolean new_weak;
4591 bfd_boolean old_weak;
4592 bfd *override;
4593 bfd_boolean common;
4594 bfd_boolean discarded;
4595 unsigned int old_alignment;
4596 unsigned int shindex;
4597 bfd *old_bfd;
4598 bfd_boolean matched;
4599
4600 override = NULL;
4601
4602 flags = BSF_NO_FLAGS;
4603 sec = NULL;
4604 value = isym->st_value;
4605 common = bed->common_definition (isym);
4606 if (common && info->inhibit_common_definition)
4607 {
4608 /* Treat common symbol as undefined for --no-define-common. */
4609 isym->st_shndx = SHN_UNDEF;
4610 common = FALSE;
4611 }
4612 discarded = FALSE;
4613
4614 bind = ELF_ST_BIND (isym->st_info);
4615 switch (bind)
4616 {
4617 case STB_LOCAL:
4618 /* This should be impossible, since ELF requires that all
4619 global symbols follow all local symbols, and that sh_info
4620 point to the first global symbol. Unfortunately, Irix 5
4621 screws this up. */
4622 if (elf_bad_symtab (abfd))
4623 continue;
4624
4625 /* If we aren't prepared to handle locals within the globals
4626 then we'll likely segfault on a NULL symbol hash if the
4627 symbol is ever referenced in relocations. */
4628 shindex = elf_elfheader (abfd)->e_shstrndx;
4629 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4630 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4631 " (>= sh_info of %lu)"),
4632 abfd, name, (long) (isym - isymbuf + extsymoff),
4633 (long) extsymoff);
4634
4635 /* Dynamic object relocations are not processed by ld, so
4636 ld won't run into the problem mentioned above. */
4637 if (dynamic)
4638 continue;
4639 bfd_set_error (bfd_error_bad_value);
4640 goto error_free_vers;
4641
4642 case STB_GLOBAL:
4643 if (isym->st_shndx != SHN_UNDEF && !common)
4644 flags = BSF_GLOBAL;
4645 break;
4646
4647 case STB_WEAK:
4648 flags = BSF_WEAK;
4649 break;
4650
4651 case STB_GNU_UNIQUE:
4652 flags = BSF_GNU_UNIQUE;
4653 break;
4654
4655 default:
4656 /* Leave it up to the processor backend. */
4657 break;
4658 }
4659
4660 if (isym->st_shndx == SHN_UNDEF)
4661 sec = bfd_und_section_ptr;
4662 else if (isym->st_shndx == SHN_ABS)
4663 sec = bfd_abs_section_ptr;
4664 else if (isym->st_shndx == SHN_COMMON)
4665 {
4666 sec = bfd_com_section_ptr;
4667 /* What ELF calls the size we call the value. What ELF
4668 calls the value we call the alignment. */
4669 value = isym->st_size;
4670 }
4671 else
4672 {
4673 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4674 if (sec == NULL)
4675 sec = bfd_abs_section_ptr;
4676 else if (discarded_section (sec))
4677 {
4678 /* Symbols from discarded section are undefined. We keep
4679 its visibility. */
4680 sec = bfd_und_section_ptr;
4681 discarded = TRUE;
4682 isym->st_shndx = SHN_UNDEF;
4683 }
4684 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4685 value -= sec->vma;
4686 }
4687
4688 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4689 isym->st_name);
4690 if (name == NULL)
4691 goto error_free_vers;
4692
4693 if (isym->st_shndx == SHN_COMMON
4694 && (abfd->flags & BFD_PLUGIN) != 0)
4695 {
4696 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4697
4698 if (xc == NULL)
4699 {
4700 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4701 | SEC_EXCLUDE);
4702 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4703 if (xc == NULL)
4704 goto error_free_vers;
4705 }
4706 sec = xc;
4707 }
4708 else if (isym->st_shndx == SHN_COMMON
4709 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4710 && !bfd_link_relocatable (info))
4711 {
4712 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4713
4714 if (tcomm == NULL)
4715 {
4716 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4717 | SEC_LINKER_CREATED);
4718 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4719 if (tcomm == NULL)
4720 goto error_free_vers;
4721 }
4722 sec = tcomm;
4723 }
4724 else if (bed->elf_add_symbol_hook)
4725 {
4726 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4727 &sec, &value))
4728 goto error_free_vers;
4729
4730 /* The hook function sets the name to NULL if this symbol
4731 should be skipped for some reason. */
4732 if (name == NULL)
4733 continue;
4734 }
4735
4736 /* Sanity check that all possibilities were handled. */
4737 if (sec == NULL)
4738 abort ();
4739
4740 /* Silently discard TLS symbols from --just-syms. There's
4741 no way to combine a static TLS block with a new TLS block
4742 for this executable. */
4743 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4744 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4745 continue;
4746
4747 if (bfd_is_und_section (sec)
4748 || bfd_is_com_section (sec))
4749 definition = FALSE;
4750 else
4751 definition = TRUE;
4752
4753 size_change_ok = FALSE;
4754 type_change_ok = bed->type_change_ok;
4755 old_weak = FALSE;
4756 matched = FALSE;
4757 old_alignment = 0;
4758 old_bfd = NULL;
4759 new_sec = sec;
4760
4761 if (is_elf_hash_table (htab))
4762 {
4763 Elf_Internal_Versym iver;
4764 unsigned int vernum = 0;
4765 bfd_boolean skip;
4766
4767 if (ever == NULL)
4768 {
4769 if (info->default_imported_symver)
4770 /* Use the default symbol version created earlier. */
4771 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4772 else
4773 iver.vs_vers = 0;
4774 }
4775 else if (ever >= extversym_end)
4776 {
4777 /* xgettext:c-format */
4778 _bfd_error_handler (_("%pB: not enough version information"),
4779 abfd);
4780 bfd_set_error (bfd_error_bad_value);
4781 goto error_free_vers;
4782 }
4783 else
4784 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4785
4786 vernum = iver.vs_vers & VERSYM_VERSION;
4787
4788 /* If this is a hidden symbol, or if it is not version
4789 1, we append the version name to the symbol name.
4790 However, we do not modify a non-hidden absolute symbol
4791 if it is not a function, because it might be the version
4792 symbol itself. FIXME: What if it isn't? */
4793 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4794 || (vernum > 1
4795 && (!bfd_is_abs_section (sec)
4796 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4797 {
4798 const char *verstr;
4799 size_t namelen, verlen, newlen;
4800 char *newname, *p;
4801
4802 if (isym->st_shndx != SHN_UNDEF)
4803 {
4804 if (vernum > elf_tdata (abfd)->cverdefs)
4805 verstr = NULL;
4806 else if (vernum > 1)
4807 verstr =
4808 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4809 else
4810 verstr = "";
4811
4812 if (verstr == NULL)
4813 {
4814 _bfd_error_handler
4815 /* xgettext:c-format */
4816 (_("%pB: %s: invalid version %u (max %d)"),
4817 abfd, name, vernum,
4818 elf_tdata (abfd)->cverdefs);
4819 bfd_set_error (bfd_error_bad_value);
4820 goto error_free_vers;
4821 }
4822 }
4823 else
4824 {
4825 /* We cannot simply test for the number of
4826 entries in the VERNEED section since the
4827 numbers for the needed versions do not start
4828 at 0. */
4829 Elf_Internal_Verneed *t;
4830
4831 verstr = NULL;
4832 for (t = elf_tdata (abfd)->verref;
4833 t != NULL;
4834 t = t->vn_nextref)
4835 {
4836 Elf_Internal_Vernaux *a;
4837
4838 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4839 {
4840 if (a->vna_other == vernum)
4841 {
4842 verstr = a->vna_nodename;
4843 break;
4844 }
4845 }
4846 if (a != NULL)
4847 break;
4848 }
4849 if (verstr == NULL)
4850 {
4851 _bfd_error_handler
4852 /* xgettext:c-format */
4853 (_("%pB: %s: invalid needed version %d"),
4854 abfd, name, vernum);
4855 bfd_set_error (bfd_error_bad_value);
4856 goto error_free_vers;
4857 }
4858 }
4859
4860 namelen = strlen (name);
4861 verlen = strlen (verstr);
4862 newlen = namelen + verlen + 2;
4863 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4864 && isym->st_shndx != SHN_UNDEF)
4865 ++newlen;
4866
4867 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4868 if (newname == NULL)
4869 goto error_free_vers;
4870 memcpy (newname, name, namelen);
4871 p = newname + namelen;
4872 *p++ = ELF_VER_CHR;
4873 /* If this is a defined non-hidden version symbol,
4874 we add another @ to the name. This indicates the
4875 default version of the symbol. */
4876 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4877 && isym->st_shndx != SHN_UNDEF)
4878 *p++ = ELF_VER_CHR;
4879 memcpy (p, verstr, verlen + 1);
4880
4881 name = newname;
4882 }
4883
4884 /* If this symbol has default visibility and the user has
4885 requested we not re-export it, then mark it as hidden. */
4886 if (!bfd_is_und_section (sec)
4887 && !dynamic
4888 && abfd->no_export
4889 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4890 isym->st_other = (STV_HIDDEN
4891 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4892
4893 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4894 sym_hash, &old_bfd, &old_weak,
4895 &old_alignment, &skip, &override,
4896 &type_change_ok, &size_change_ok,
4897 &matched))
4898 goto error_free_vers;
4899
4900 if (skip)
4901 continue;
4902
4903 /* Override a definition only if the new symbol matches the
4904 existing one. */
4905 if (override && matched)
4906 definition = FALSE;
4907
4908 h = *sym_hash;
4909 while (h->root.type == bfd_link_hash_indirect
4910 || h->root.type == bfd_link_hash_warning)
4911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4912
4913 if (elf_tdata (abfd)->verdef != NULL
4914 && vernum > 1
4915 && definition)
4916 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4917 }
4918
4919 if (! (_bfd_generic_link_add_one_symbol
4920 (info, override ? override : abfd, name, flags, sec, value,
4921 NULL, FALSE, bed->collect,
4922 (struct bfd_link_hash_entry **) sym_hash)))
4923 goto error_free_vers;
4924
4925 h = *sym_hash;
4926 /* We need to make sure that indirect symbol dynamic flags are
4927 updated. */
4928 hi = h;
4929 while (h->root.type == bfd_link_hash_indirect
4930 || h->root.type == bfd_link_hash_warning)
4931 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4932
4933 /* Setting the index to -3 tells elf_link_output_extsym that
4934 this symbol is defined in a discarded section. */
4935 if (discarded)
4936 h->indx = -3;
4937
4938 *sym_hash = h;
4939
4940 new_weak = (flags & BSF_WEAK) != 0;
4941 if (dynamic
4942 && definition
4943 && new_weak
4944 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4945 && is_elf_hash_table (htab)
4946 && h->u.alias == NULL)
4947 {
4948 /* Keep a list of all weak defined non function symbols from
4949 a dynamic object, using the alias field. Later in this
4950 function we will set the alias field to the correct
4951 value. We only put non-function symbols from dynamic
4952 objects on this list, because that happens to be the only
4953 time we need to know the normal symbol corresponding to a
4954 weak symbol, and the information is time consuming to
4955 figure out. If the alias field is not already NULL,
4956 then this symbol was already defined by some previous
4957 dynamic object, and we will be using that previous
4958 definition anyhow. */
4959
4960 h->u.alias = weaks;
4961 weaks = h;
4962 }
4963
4964 /* Set the alignment of a common symbol. */
4965 if ((common || bfd_is_com_section (sec))
4966 && h->root.type == bfd_link_hash_common)
4967 {
4968 unsigned int align;
4969
4970 if (common)
4971 align = bfd_log2 (isym->st_value);
4972 else
4973 {
4974 /* The new symbol is a common symbol in a shared object.
4975 We need to get the alignment from the section. */
4976 align = new_sec->alignment_power;
4977 }
4978 if (align > old_alignment)
4979 h->root.u.c.p->alignment_power = align;
4980 else
4981 h->root.u.c.p->alignment_power = old_alignment;
4982 }
4983
4984 if (is_elf_hash_table (htab))
4985 {
4986 /* Set a flag in the hash table entry indicating the type of
4987 reference or definition we just found. A dynamic symbol
4988 is one which is referenced or defined by both a regular
4989 object and a shared object. */
4990 bfd_boolean dynsym = FALSE;
4991
4992 /* Plugin symbols aren't normal. Don't set def/ref flags. */
4993 if ((abfd->flags & BFD_PLUGIN) != 0)
4994 ;
4995 else if (!dynamic)
4996 {
4997 if (! definition)
4998 {
4999 h->ref_regular = 1;
5000 if (bind != STB_WEAK)
5001 h->ref_regular_nonweak = 1;
5002 }
5003 else
5004 {
5005 h->def_regular = 1;
5006 if (h->def_dynamic)
5007 {
5008 h->def_dynamic = 0;
5009 h->ref_dynamic = 1;
5010 }
5011 }
5012 }
5013 else
5014 {
5015 if (! definition)
5016 {
5017 h->ref_dynamic = 1;
5018 hi->ref_dynamic = 1;
5019 }
5020 else
5021 {
5022 h->def_dynamic = 1;
5023 hi->def_dynamic = 1;
5024 }
5025 }
5026
5027 /* If an indirect symbol has been forced local, don't
5028 make the real symbol dynamic. */
5029 if (h != hi && hi->forced_local)
5030 ;
5031 else if (!dynamic)
5032 {
5033 if (bfd_link_dll (info)
5034 || h->def_dynamic
5035 || h->ref_dynamic)
5036 dynsym = TRUE;
5037 }
5038 else
5039 {
5040 if (h->def_regular
5041 || h->ref_regular
5042 || (h->is_weakalias
5043 && weakdef (h)->dynindx != -1))
5044 dynsym = TRUE;
5045 }
5046
5047 /* Check to see if we need to add an indirect symbol for
5048 the default name. */
5049 if (definition
5050 || (!override && h->root.type == bfd_link_hash_common))
5051 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5052 sec, value, &old_bfd, &dynsym))
5053 goto error_free_vers;
5054
5055 /* Check the alignment when a common symbol is involved. This
5056 can change when a common symbol is overridden by a normal
5057 definition or a common symbol is ignored due to the old
5058 normal definition. We need to make sure the maximum
5059 alignment is maintained. */
5060 if ((old_alignment || common)
5061 && h->root.type != bfd_link_hash_common)
5062 {
5063 unsigned int common_align;
5064 unsigned int normal_align;
5065 unsigned int symbol_align;
5066 bfd *normal_bfd;
5067 bfd *common_bfd;
5068
5069 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5070 || h->root.type == bfd_link_hash_defweak);
5071
5072 symbol_align = ffs (h->root.u.def.value) - 1;
5073 if (h->root.u.def.section->owner != NULL
5074 && (h->root.u.def.section->owner->flags
5075 & (DYNAMIC | BFD_PLUGIN)) == 0)
5076 {
5077 normal_align = h->root.u.def.section->alignment_power;
5078 if (normal_align > symbol_align)
5079 normal_align = symbol_align;
5080 }
5081 else
5082 normal_align = symbol_align;
5083
5084 if (old_alignment)
5085 {
5086 common_align = old_alignment;
5087 common_bfd = old_bfd;
5088 normal_bfd = abfd;
5089 }
5090 else
5091 {
5092 common_align = bfd_log2 (isym->st_value);
5093 common_bfd = abfd;
5094 normal_bfd = old_bfd;
5095 }
5096
5097 if (normal_align < common_align)
5098 {
5099 /* PR binutils/2735 */
5100 if (normal_bfd == NULL)
5101 _bfd_error_handler
5102 /* xgettext:c-format */
5103 (_("warning: alignment %u of common symbol `%s' in %pB is"
5104 " greater than the alignment (%u) of its section %pA"),
5105 1 << common_align, name, common_bfd,
5106 1 << normal_align, h->root.u.def.section);
5107 else
5108 _bfd_error_handler
5109 /* xgettext:c-format */
5110 (_("warning: alignment %u of symbol `%s' in %pB"
5111 " is smaller than %u in %pB"),
5112 1 << normal_align, name, normal_bfd,
5113 1 << common_align, common_bfd);
5114 }
5115 }
5116
5117 /* Remember the symbol size if it isn't undefined. */
5118 if (isym->st_size != 0
5119 && isym->st_shndx != SHN_UNDEF
5120 && (definition || h->size == 0))
5121 {
5122 if (h->size != 0
5123 && h->size != isym->st_size
5124 && ! size_change_ok)
5125 _bfd_error_handler
5126 /* xgettext:c-format */
5127 (_("warning: size of symbol `%s' changed"
5128 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5129 name, (uint64_t) h->size, old_bfd,
5130 (uint64_t) isym->st_size, abfd);
5131
5132 h->size = isym->st_size;
5133 }
5134
5135 /* If this is a common symbol, then we always want H->SIZE
5136 to be the size of the common symbol. The code just above
5137 won't fix the size if a common symbol becomes larger. We
5138 don't warn about a size change here, because that is
5139 covered by --warn-common. Allow changes between different
5140 function types. */
5141 if (h->root.type == bfd_link_hash_common)
5142 h->size = h->root.u.c.size;
5143
5144 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5145 && ((definition && !new_weak)
5146 || (old_weak && h->root.type == bfd_link_hash_common)
5147 || h->type == STT_NOTYPE))
5148 {
5149 unsigned int type = ELF_ST_TYPE (isym->st_info);
5150
5151 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5152 symbol. */
5153 if (type == STT_GNU_IFUNC
5154 && (abfd->flags & DYNAMIC) != 0)
5155 type = STT_FUNC;
5156
5157 if (h->type != type)
5158 {
5159 if (h->type != STT_NOTYPE && ! type_change_ok)
5160 /* xgettext:c-format */
5161 _bfd_error_handler
5162 (_("warning: type of symbol `%s' changed"
5163 " from %d to %d in %pB"),
5164 name, h->type, type, abfd);
5165
5166 h->type = type;
5167 }
5168 }
5169
5170 /* Merge st_other field. */
5171 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5172
5173 /* We don't want to make debug symbol dynamic. */
5174 if (definition
5175 && (sec->flags & SEC_DEBUGGING)
5176 && !bfd_link_relocatable (info))
5177 dynsym = FALSE;
5178
5179 /* Nor should we make plugin symbols dynamic. */
5180 if ((abfd->flags & BFD_PLUGIN) != 0)
5181 dynsym = FALSE;
5182
5183 if (definition)
5184 {
5185 h->target_internal = isym->st_target_internal;
5186 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5187 }
5188
5189 if (definition && !dynamic)
5190 {
5191 char *p = strchr (name, ELF_VER_CHR);
5192 if (p != NULL && p[1] != ELF_VER_CHR)
5193 {
5194 /* Queue non-default versions so that .symver x, x@FOO
5195 aliases can be checked. */
5196 if (!nondeflt_vers)
5197 {
5198 size_t amt = ((isymend - isym + 1)
5199 * sizeof (struct elf_link_hash_entry *));
5200 nondeflt_vers
5201 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5202 if (!nondeflt_vers)
5203 goto error_free_vers;
5204 }
5205 nondeflt_vers[nondeflt_vers_cnt++] = h;
5206 }
5207 }
5208
5209 if (dynsym && h->dynindx == -1)
5210 {
5211 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5212 goto error_free_vers;
5213 if (h->is_weakalias
5214 && weakdef (h)->dynindx == -1)
5215 {
5216 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5217 goto error_free_vers;
5218 }
5219 }
5220 else if (h->dynindx != -1)
5221 /* If the symbol already has a dynamic index, but
5222 visibility says it should not be visible, turn it into
5223 a local symbol. */
5224 switch (ELF_ST_VISIBILITY (h->other))
5225 {
5226 case STV_INTERNAL:
5227 case STV_HIDDEN:
5228 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5229 dynsym = FALSE;
5230 break;
5231 }
5232
5233 if (!add_needed
5234 && matched
5235 && definition
5236 && ((dynsym
5237 && h->ref_regular_nonweak)
5238 || (old_bfd != NULL
5239 && (old_bfd->flags & BFD_PLUGIN) != 0
5240 && bind != STB_WEAK)
5241 || (h->ref_dynamic_nonweak
5242 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5243 && !on_needed_list (elf_dt_name (abfd),
5244 htab->needed, NULL))))
5245 {
5246 const char *soname = elf_dt_name (abfd);
5247
5248 info->callbacks->minfo ("%!", soname, old_bfd,
5249 h->root.root.string);
5250
5251 /* A symbol from a library loaded via DT_NEEDED of some
5252 other library is referenced by a regular object.
5253 Add a DT_NEEDED entry for it. Issue an error if
5254 --no-add-needed is used and the reference was not
5255 a weak one. */
5256 if (old_bfd != NULL
5257 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5258 {
5259 _bfd_error_handler
5260 /* xgettext:c-format */
5261 (_("%pB: undefined reference to symbol '%s'"),
5262 old_bfd, name);
5263 bfd_set_error (bfd_error_missing_dso);
5264 goto error_free_vers;
5265 }
5266
5267 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5268 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5269
5270 /* Create dynamic sections for backends that require
5271 that be done before setup_gnu_properties. */
5272 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5273 return FALSE;
5274 add_needed = TRUE;
5275 }
5276 }
5277 }
5278
5279 if (info->lto_plugin_active
5280 && !bfd_link_relocatable (info)
5281 && (abfd->flags & BFD_PLUGIN) == 0
5282 && !just_syms
5283 && extsymcount)
5284 {
5285 int r_sym_shift;
5286
5287 if (bed->s->arch_size == 32)
5288 r_sym_shift = 8;
5289 else
5290 r_sym_shift = 32;
5291
5292 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5293 referenced in regular objects so that linker plugin will get
5294 the correct symbol resolution. */
5295
5296 sym_hash = elf_sym_hashes (abfd);
5297 for (s = abfd->sections; s != NULL; s = s->next)
5298 {
5299 Elf_Internal_Rela *internal_relocs;
5300 Elf_Internal_Rela *rel, *relend;
5301
5302 /* Don't check relocations in excluded sections. */
5303 if ((s->flags & SEC_RELOC) == 0
5304 || s->reloc_count == 0
5305 || (s->flags & SEC_EXCLUDE) != 0
5306 || ((info->strip == strip_all
5307 || info->strip == strip_debugger)
5308 && (s->flags & SEC_DEBUGGING) != 0))
5309 continue;
5310
5311 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5312 NULL,
5313 info->keep_memory);
5314 if (internal_relocs == NULL)
5315 goto error_free_vers;
5316
5317 rel = internal_relocs;
5318 relend = rel + s->reloc_count;
5319 for ( ; rel < relend; rel++)
5320 {
5321 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5322 struct elf_link_hash_entry *h;
5323
5324 /* Skip local symbols. */
5325 if (r_symndx < extsymoff)
5326 continue;
5327
5328 h = sym_hash[r_symndx - extsymoff];
5329 if (h != NULL)
5330 h->root.non_ir_ref_regular = 1;
5331 }
5332
5333 if (elf_section_data (s)->relocs != internal_relocs)
5334 free (internal_relocs);
5335 }
5336 }
5337
5338 free (extversym);
5339 extversym = NULL;
5340 free (isymbuf);
5341 isymbuf = NULL;
5342
5343 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5344 {
5345 unsigned int i;
5346
5347 /* Restore the symbol table. */
5348 old_ent = (char *) old_tab + tabsize;
5349 memset (elf_sym_hashes (abfd), 0,
5350 extsymcount * sizeof (struct elf_link_hash_entry *));
5351 htab->root.table.table = old_table;
5352 htab->root.table.size = old_size;
5353 htab->root.table.count = old_count;
5354 memcpy (htab->root.table.table, old_tab, tabsize);
5355 htab->root.undefs = old_undefs;
5356 htab->root.undefs_tail = old_undefs_tail;
5357 if (htab->dynstr != NULL)
5358 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5359 free (old_strtab);
5360 old_strtab = NULL;
5361 for (i = 0; i < htab->root.table.size; i++)
5362 {
5363 struct bfd_hash_entry *p;
5364 struct elf_link_hash_entry *h;
5365 unsigned int non_ir_ref_dynamic;
5366
5367 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5368 {
5369 /* Preserve non_ir_ref_dynamic so that this symbol
5370 will be exported when the dynamic lib becomes needed
5371 in the second pass. */
5372 h = (struct elf_link_hash_entry *) p;
5373 if (h->root.type == bfd_link_hash_warning)
5374 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5375 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5376
5377 h = (struct elf_link_hash_entry *) p;
5378 memcpy (h, old_ent, htab->root.table.entsize);
5379 old_ent = (char *) old_ent + htab->root.table.entsize;
5380 if (h->root.type == bfd_link_hash_warning)
5381 {
5382 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5383 memcpy (h, old_ent, htab->root.table.entsize);
5384 old_ent = (char *) old_ent + htab->root.table.entsize;
5385 }
5386 if (h->root.type == bfd_link_hash_common)
5387 {
5388 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5389 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5390 }
5391 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5392 }
5393 }
5394
5395 /* Make a special call to the linker "notice" function to
5396 tell it that symbols added for crefs may need to be removed. */
5397 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5398 goto error_free_vers;
5399
5400 free (old_tab);
5401 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5402 alloc_mark);
5403 free (nondeflt_vers);
5404 return TRUE;
5405 }
5406
5407 if (old_tab != NULL)
5408 {
5409 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5410 goto error_free_vers;
5411 free (old_tab);
5412 old_tab = NULL;
5413 }
5414
5415 /* Now that all the symbols from this input file are created, if
5416 not performing a relocatable link, handle .symver foo, foo@BAR
5417 such that any relocs against foo become foo@BAR. */
5418 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5419 {
5420 size_t cnt, symidx;
5421
5422 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5423 {
5424 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5425 char *shortname, *p;
5426 size_t amt;
5427
5428 p = strchr (h->root.root.string, ELF_VER_CHR);
5429 if (p == NULL
5430 || (h->root.type != bfd_link_hash_defined
5431 && h->root.type != bfd_link_hash_defweak))
5432 continue;
5433
5434 amt = p - h->root.root.string;
5435 shortname = (char *) bfd_malloc (amt + 1);
5436 if (!shortname)
5437 goto error_free_vers;
5438 memcpy (shortname, h->root.root.string, amt);
5439 shortname[amt] = '\0';
5440
5441 hi = (struct elf_link_hash_entry *)
5442 bfd_link_hash_lookup (&htab->root, shortname,
5443 FALSE, FALSE, FALSE);
5444 if (hi != NULL
5445 && hi->root.type == h->root.type
5446 && hi->root.u.def.value == h->root.u.def.value
5447 && hi->root.u.def.section == h->root.u.def.section)
5448 {
5449 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5450 hi->root.type = bfd_link_hash_indirect;
5451 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5452 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5453 sym_hash = elf_sym_hashes (abfd);
5454 if (sym_hash)
5455 for (symidx = 0; symidx < extsymcount; ++symidx)
5456 if (sym_hash[symidx] == hi)
5457 {
5458 sym_hash[symidx] = h;
5459 break;
5460 }
5461 }
5462 free (shortname);
5463 }
5464 free (nondeflt_vers);
5465 nondeflt_vers = NULL;
5466 }
5467
5468 /* Now set the alias field correctly for all the weak defined
5469 symbols we found. The only way to do this is to search all the
5470 symbols. Since we only need the information for non functions in
5471 dynamic objects, that's the only time we actually put anything on
5472 the list WEAKS. We need this information so that if a regular
5473 object refers to a symbol defined weakly in a dynamic object, the
5474 real symbol in the dynamic object is also put in the dynamic
5475 symbols; we also must arrange for both symbols to point to the
5476 same memory location. We could handle the general case of symbol
5477 aliasing, but a general symbol alias can only be generated in
5478 assembler code, handling it correctly would be very time
5479 consuming, and other ELF linkers don't handle general aliasing
5480 either. */
5481 if (weaks != NULL)
5482 {
5483 struct elf_link_hash_entry **hpp;
5484 struct elf_link_hash_entry **hppend;
5485 struct elf_link_hash_entry **sorted_sym_hash;
5486 struct elf_link_hash_entry *h;
5487 size_t sym_count, amt;
5488
5489 /* Since we have to search the whole symbol list for each weak
5490 defined symbol, search time for N weak defined symbols will be
5491 O(N^2). Binary search will cut it down to O(NlogN). */
5492 amt = extsymcount * sizeof (*sorted_sym_hash);
5493 sorted_sym_hash = bfd_malloc (amt);
5494 if (sorted_sym_hash == NULL)
5495 goto error_return;
5496 sym_hash = sorted_sym_hash;
5497 hpp = elf_sym_hashes (abfd);
5498 hppend = hpp + extsymcount;
5499 sym_count = 0;
5500 for (; hpp < hppend; hpp++)
5501 {
5502 h = *hpp;
5503 if (h != NULL
5504 && h->root.type == bfd_link_hash_defined
5505 && !bed->is_function_type (h->type))
5506 {
5507 *sym_hash = h;
5508 sym_hash++;
5509 sym_count++;
5510 }
5511 }
5512
5513 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5514 elf_sort_symbol);
5515
5516 while (weaks != NULL)
5517 {
5518 struct elf_link_hash_entry *hlook;
5519 asection *slook;
5520 bfd_vma vlook;
5521 size_t i, j, idx = 0;
5522
5523 hlook = weaks;
5524 weaks = hlook->u.alias;
5525 hlook->u.alias = NULL;
5526
5527 if (hlook->root.type != bfd_link_hash_defined
5528 && hlook->root.type != bfd_link_hash_defweak)
5529 continue;
5530
5531 slook = hlook->root.u.def.section;
5532 vlook = hlook->root.u.def.value;
5533
5534 i = 0;
5535 j = sym_count;
5536 while (i != j)
5537 {
5538 bfd_signed_vma vdiff;
5539 idx = (i + j) / 2;
5540 h = sorted_sym_hash[idx];
5541 vdiff = vlook - h->root.u.def.value;
5542 if (vdiff < 0)
5543 j = idx;
5544 else if (vdiff > 0)
5545 i = idx + 1;
5546 else
5547 {
5548 int sdiff = slook->id - h->root.u.def.section->id;
5549 if (sdiff < 0)
5550 j = idx;
5551 else if (sdiff > 0)
5552 i = idx + 1;
5553 else
5554 break;
5555 }
5556 }
5557
5558 /* We didn't find a value/section match. */
5559 if (i == j)
5560 continue;
5561
5562 /* With multiple aliases, or when the weak symbol is already
5563 strongly defined, we have multiple matching symbols and
5564 the binary search above may land on any of them. Step
5565 one past the matching symbol(s). */
5566 while (++idx != j)
5567 {
5568 h = sorted_sym_hash[idx];
5569 if (h->root.u.def.section != slook
5570 || h->root.u.def.value != vlook)
5571 break;
5572 }
5573
5574 /* Now look back over the aliases. Since we sorted by size
5575 as well as value and section, we'll choose the one with
5576 the largest size. */
5577 while (idx-- != i)
5578 {
5579 h = sorted_sym_hash[idx];
5580
5581 /* Stop if value or section doesn't match. */
5582 if (h->root.u.def.section != slook
5583 || h->root.u.def.value != vlook)
5584 break;
5585 else if (h != hlook)
5586 {
5587 struct elf_link_hash_entry *t;
5588
5589 hlook->u.alias = h;
5590 hlook->is_weakalias = 1;
5591 t = h;
5592 if (t->u.alias != NULL)
5593 while (t->u.alias != h)
5594 t = t->u.alias;
5595 t->u.alias = hlook;
5596
5597 /* If the weak definition is in the list of dynamic
5598 symbols, make sure the real definition is put
5599 there as well. */
5600 if (hlook->dynindx != -1 && h->dynindx == -1)
5601 {
5602 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5603 {
5604 err_free_sym_hash:
5605 free (sorted_sym_hash);
5606 goto error_return;
5607 }
5608 }
5609
5610 /* If the real definition is in the list of dynamic
5611 symbols, make sure the weak definition is put
5612 there as well. If we don't do this, then the
5613 dynamic loader might not merge the entries for the
5614 real definition and the weak definition. */
5615 if (h->dynindx != -1 && hlook->dynindx == -1)
5616 {
5617 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5618 goto err_free_sym_hash;
5619 }
5620 break;
5621 }
5622 }
5623 }
5624
5625 free (sorted_sym_hash);
5626 }
5627
5628 if (bed->check_directives
5629 && !(*bed->check_directives) (abfd, info))
5630 return FALSE;
5631
5632 /* If this is a non-traditional link, try to optimize the handling
5633 of the .stab/.stabstr sections. */
5634 if (! dynamic
5635 && ! info->traditional_format
5636 && is_elf_hash_table (htab)
5637 && (info->strip != strip_all && info->strip != strip_debugger))
5638 {
5639 asection *stabstr;
5640
5641 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5642 if (stabstr != NULL)
5643 {
5644 bfd_size_type string_offset = 0;
5645 asection *stab;
5646
5647 for (stab = abfd->sections; stab; stab = stab->next)
5648 if (CONST_STRNEQ (stab->name, ".stab")
5649 && (!stab->name[5] ||
5650 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5651 && (stab->flags & SEC_MERGE) == 0
5652 && !bfd_is_abs_section (stab->output_section))
5653 {
5654 struct bfd_elf_section_data *secdata;
5655
5656 secdata = elf_section_data (stab);
5657 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5658 stabstr, &secdata->sec_info,
5659 &string_offset))
5660 goto error_return;
5661 if (secdata->sec_info)
5662 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5663 }
5664 }
5665 }
5666
5667 if (dynamic && add_needed)
5668 {
5669 /* Add this bfd to the loaded list. */
5670 struct elf_link_loaded_list *n;
5671
5672 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5673 if (n == NULL)
5674 goto error_return;
5675 n->abfd = abfd;
5676 n->next = htab->dyn_loaded;
5677 htab->dyn_loaded = n;
5678 }
5679 if (dynamic && !add_needed
5680 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5681 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5682
5683 return TRUE;
5684
5685 error_free_vers:
5686 free (old_tab);
5687 free (old_strtab);
5688 free (nondeflt_vers);
5689 free (extversym);
5690 error_free_sym:
5691 free (isymbuf);
5692 error_return:
5693 return FALSE;
5694 }
5695
5696 /* Return the linker hash table entry of a symbol that might be
5697 satisfied by an archive symbol. Return -1 on error. */
5698
5699 struct elf_link_hash_entry *
5700 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5701 struct bfd_link_info *info,
5702 const char *name)
5703 {
5704 struct elf_link_hash_entry *h;
5705 char *p, *copy;
5706 size_t len, first;
5707
5708 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5709 if (h != NULL)
5710 return h;
5711
5712 /* If this is a default version (the name contains @@), look up the
5713 symbol again with only one `@' as well as without the version.
5714 The effect is that references to the symbol with and without the
5715 version will be matched by the default symbol in the archive. */
5716
5717 p = strchr (name, ELF_VER_CHR);
5718 if (p == NULL || p[1] != ELF_VER_CHR)
5719 return h;
5720
5721 /* First check with only one `@'. */
5722 len = strlen (name);
5723 copy = (char *) bfd_alloc (abfd, len);
5724 if (copy == NULL)
5725 return (struct elf_link_hash_entry *) -1;
5726
5727 first = p - name + 1;
5728 memcpy (copy, name, first);
5729 memcpy (copy + first, name + first + 1, len - first);
5730
5731 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5732 if (h == NULL)
5733 {
5734 /* We also need to check references to the symbol without the
5735 version. */
5736 copy[first - 1] = '\0';
5737 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5738 FALSE, FALSE, TRUE);
5739 }
5740
5741 bfd_release (abfd, copy);
5742 return h;
5743 }
5744
5745 /* Add symbols from an ELF archive file to the linker hash table. We
5746 don't use _bfd_generic_link_add_archive_symbols because we need to
5747 handle versioned symbols.
5748
5749 Fortunately, ELF archive handling is simpler than that done by
5750 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5751 oddities. In ELF, if we find a symbol in the archive map, and the
5752 symbol is currently undefined, we know that we must pull in that
5753 object file.
5754
5755 Unfortunately, we do have to make multiple passes over the symbol
5756 table until nothing further is resolved. */
5757
5758 static bfd_boolean
5759 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5760 {
5761 symindex c;
5762 unsigned char *included = NULL;
5763 carsym *symdefs;
5764 bfd_boolean loop;
5765 size_t amt;
5766 const struct elf_backend_data *bed;
5767 struct elf_link_hash_entry * (*archive_symbol_lookup)
5768 (bfd *, struct bfd_link_info *, const char *);
5769
5770 if (! bfd_has_map (abfd))
5771 {
5772 /* An empty archive is a special case. */
5773 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5774 return TRUE;
5775 bfd_set_error (bfd_error_no_armap);
5776 return FALSE;
5777 }
5778
5779 /* Keep track of all symbols we know to be already defined, and all
5780 files we know to be already included. This is to speed up the
5781 second and subsequent passes. */
5782 c = bfd_ardata (abfd)->symdef_count;
5783 if (c == 0)
5784 return TRUE;
5785 amt = c * sizeof (*included);
5786 included = (unsigned char *) bfd_zmalloc (amt);
5787 if (included == NULL)
5788 return FALSE;
5789
5790 symdefs = bfd_ardata (abfd)->symdefs;
5791 bed = get_elf_backend_data (abfd);
5792 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5793
5794 do
5795 {
5796 file_ptr last;
5797 symindex i;
5798 carsym *symdef;
5799 carsym *symdefend;
5800
5801 loop = FALSE;
5802 last = -1;
5803
5804 symdef = symdefs;
5805 symdefend = symdef + c;
5806 for (i = 0; symdef < symdefend; symdef++, i++)
5807 {
5808 struct elf_link_hash_entry *h;
5809 bfd *element;
5810 struct bfd_link_hash_entry *undefs_tail;
5811 symindex mark;
5812
5813 if (included[i])
5814 continue;
5815 if (symdef->file_offset == last)
5816 {
5817 included[i] = TRUE;
5818 continue;
5819 }
5820
5821 h = archive_symbol_lookup (abfd, info, symdef->name);
5822 if (h == (struct elf_link_hash_entry *) -1)
5823 goto error_return;
5824
5825 if (h == NULL)
5826 continue;
5827
5828 if (h->root.type == bfd_link_hash_undefined)
5829 {
5830 /* If the archive element has already been loaded then one
5831 of the symbols defined by that element might have been
5832 made undefined due to being in a discarded section. */
5833 if (h->indx == -3)
5834 continue;
5835 }
5836 else if (h->root.type == bfd_link_hash_common)
5837 {
5838 /* We currently have a common symbol. The archive map contains
5839 a reference to this symbol, so we may want to include it. We
5840 only want to include it however, if this archive element
5841 contains a definition of the symbol, not just another common
5842 declaration of it.
5843
5844 Unfortunately some archivers (including GNU ar) will put
5845 declarations of common symbols into their archive maps, as
5846 well as real definitions, so we cannot just go by the archive
5847 map alone. Instead we must read in the element's symbol
5848 table and check that to see what kind of symbol definition
5849 this is. */
5850 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5851 continue;
5852 }
5853 else
5854 {
5855 if (h->root.type != bfd_link_hash_undefweak)
5856 /* Symbol must be defined. Don't check it again. */
5857 included[i] = TRUE;
5858 continue;
5859 }
5860
5861 /* We need to include this archive member. */
5862 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5863 if (element == NULL)
5864 goto error_return;
5865
5866 if (! bfd_check_format (element, bfd_object))
5867 goto error_return;
5868
5869 undefs_tail = info->hash->undefs_tail;
5870
5871 if (!(*info->callbacks
5872 ->add_archive_element) (info, element, symdef->name, &element))
5873 continue;
5874 if (!bfd_link_add_symbols (element, info))
5875 goto error_return;
5876
5877 /* If there are any new undefined symbols, we need to make
5878 another pass through the archive in order to see whether
5879 they can be defined. FIXME: This isn't perfect, because
5880 common symbols wind up on undefs_tail and because an
5881 undefined symbol which is defined later on in this pass
5882 does not require another pass. This isn't a bug, but it
5883 does make the code less efficient than it could be. */
5884 if (undefs_tail != info->hash->undefs_tail)
5885 loop = TRUE;
5886
5887 /* Look backward to mark all symbols from this object file
5888 which we have already seen in this pass. */
5889 mark = i;
5890 do
5891 {
5892 included[mark] = TRUE;
5893 if (mark == 0)
5894 break;
5895 --mark;
5896 }
5897 while (symdefs[mark].file_offset == symdef->file_offset);
5898
5899 /* We mark subsequent symbols from this object file as we go
5900 on through the loop. */
5901 last = symdef->file_offset;
5902 }
5903 }
5904 while (loop);
5905
5906 free (included);
5907 return TRUE;
5908
5909 error_return:
5910 free (included);
5911 return FALSE;
5912 }
5913
5914 /* Given an ELF BFD, add symbols to the global hash table as
5915 appropriate. */
5916
5917 bfd_boolean
5918 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5919 {
5920 switch (bfd_get_format (abfd))
5921 {
5922 case bfd_object:
5923 return elf_link_add_object_symbols (abfd, info);
5924 case bfd_archive:
5925 return elf_link_add_archive_symbols (abfd, info);
5926 default:
5927 bfd_set_error (bfd_error_wrong_format);
5928 return FALSE;
5929 }
5930 }
5931 \f
5932 struct hash_codes_info
5933 {
5934 unsigned long *hashcodes;
5935 bfd_boolean error;
5936 };
5937
5938 /* This function will be called though elf_link_hash_traverse to store
5939 all hash value of the exported symbols in an array. */
5940
5941 static bfd_boolean
5942 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5943 {
5944 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5945 const char *name;
5946 unsigned long ha;
5947 char *alc = NULL;
5948
5949 /* Ignore indirect symbols. These are added by the versioning code. */
5950 if (h->dynindx == -1)
5951 return TRUE;
5952
5953 name = h->root.root.string;
5954 if (h->versioned >= versioned)
5955 {
5956 char *p = strchr (name, ELF_VER_CHR);
5957 if (p != NULL)
5958 {
5959 alc = (char *) bfd_malloc (p - name + 1);
5960 if (alc == NULL)
5961 {
5962 inf->error = TRUE;
5963 return FALSE;
5964 }
5965 memcpy (alc, name, p - name);
5966 alc[p - name] = '\0';
5967 name = alc;
5968 }
5969 }
5970
5971 /* Compute the hash value. */
5972 ha = bfd_elf_hash (name);
5973
5974 /* Store the found hash value in the array given as the argument. */
5975 *(inf->hashcodes)++ = ha;
5976
5977 /* And store it in the struct so that we can put it in the hash table
5978 later. */
5979 h->u.elf_hash_value = ha;
5980
5981 free (alc);
5982 return TRUE;
5983 }
5984
5985 struct collect_gnu_hash_codes
5986 {
5987 bfd *output_bfd;
5988 const struct elf_backend_data *bed;
5989 unsigned long int nsyms;
5990 unsigned long int maskbits;
5991 unsigned long int *hashcodes;
5992 unsigned long int *hashval;
5993 unsigned long int *indx;
5994 unsigned long int *counts;
5995 bfd_vma *bitmask;
5996 bfd_byte *contents;
5997 bfd_size_type xlat;
5998 long int min_dynindx;
5999 unsigned long int bucketcount;
6000 unsigned long int symindx;
6001 long int local_indx;
6002 long int shift1, shift2;
6003 unsigned long int mask;
6004 bfd_boolean error;
6005 };
6006
6007 /* This function will be called though elf_link_hash_traverse to store
6008 all hash value of the exported symbols in an array. */
6009
6010 static bfd_boolean
6011 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6012 {
6013 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6014 const char *name;
6015 unsigned long ha;
6016 char *alc = NULL;
6017
6018 /* Ignore indirect symbols. These are added by the versioning code. */
6019 if (h->dynindx == -1)
6020 return TRUE;
6021
6022 /* Ignore also local symbols and undefined symbols. */
6023 if (! (*s->bed->elf_hash_symbol) (h))
6024 return TRUE;
6025
6026 name = h->root.root.string;
6027 if (h->versioned >= versioned)
6028 {
6029 char *p = strchr (name, ELF_VER_CHR);
6030 if (p != NULL)
6031 {
6032 alc = (char *) bfd_malloc (p - name + 1);
6033 if (alc == NULL)
6034 {
6035 s->error = TRUE;
6036 return FALSE;
6037 }
6038 memcpy (alc, name, p - name);
6039 alc[p - name] = '\0';
6040 name = alc;
6041 }
6042 }
6043
6044 /* Compute the hash value. */
6045 ha = bfd_elf_gnu_hash (name);
6046
6047 /* Store the found hash value in the array for compute_bucket_count,
6048 and also for .dynsym reordering purposes. */
6049 s->hashcodes[s->nsyms] = ha;
6050 s->hashval[h->dynindx] = ha;
6051 ++s->nsyms;
6052 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6053 s->min_dynindx = h->dynindx;
6054
6055 free (alc);
6056 return TRUE;
6057 }
6058
6059 /* This function will be called though elf_link_hash_traverse to do
6060 final dynamic symbol renumbering in case of .gnu.hash.
6061 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6062 to the translation table. */
6063
6064 static bfd_boolean
6065 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6066 {
6067 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6068 unsigned long int bucket;
6069 unsigned long int val;
6070
6071 /* Ignore indirect symbols. */
6072 if (h->dynindx == -1)
6073 return TRUE;
6074
6075 /* Ignore also local symbols and undefined symbols. */
6076 if (! (*s->bed->elf_hash_symbol) (h))
6077 {
6078 if (h->dynindx >= s->min_dynindx)
6079 {
6080 if (s->bed->record_xhash_symbol != NULL)
6081 {
6082 (*s->bed->record_xhash_symbol) (h, 0);
6083 s->local_indx++;
6084 }
6085 else
6086 h->dynindx = s->local_indx++;
6087 }
6088 return TRUE;
6089 }
6090
6091 bucket = s->hashval[h->dynindx] % s->bucketcount;
6092 val = (s->hashval[h->dynindx] >> s->shift1)
6093 & ((s->maskbits >> s->shift1) - 1);
6094 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6095 s->bitmask[val]
6096 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6097 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6098 if (s->counts[bucket] == 1)
6099 /* Last element terminates the chain. */
6100 val |= 1;
6101 bfd_put_32 (s->output_bfd, val,
6102 s->contents + (s->indx[bucket] - s->symindx) * 4);
6103 --s->counts[bucket];
6104 if (s->bed->record_xhash_symbol != NULL)
6105 {
6106 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6107
6108 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6109 }
6110 else
6111 h->dynindx = s->indx[bucket]++;
6112 return TRUE;
6113 }
6114
6115 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6116
6117 bfd_boolean
6118 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6119 {
6120 return !(h->forced_local
6121 || h->root.type == bfd_link_hash_undefined
6122 || h->root.type == bfd_link_hash_undefweak
6123 || ((h->root.type == bfd_link_hash_defined
6124 || h->root.type == bfd_link_hash_defweak)
6125 && h->root.u.def.section->output_section == NULL));
6126 }
6127
6128 /* Array used to determine the number of hash table buckets to use
6129 based on the number of symbols there are. If there are fewer than
6130 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6131 fewer than 37 we use 17 buckets, and so forth. We never use more
6132 than 32771 buckets. */
6133
6134 static const size_t elf_buckets[] =
6135 {
6136 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6137 16411, 32771, 0
6138 };
6139
6140 /* Compute bucket count for hashing table. We do not use a static set
6141 of possible tables sizes anymore. Instead we determine for all
6142 possible reasonable sizes of the table the outcome (i.e., the
6143 number of collisions etc) and choose the best solution. The
6144 weighting functions are not too simple to allow the table to grow
6145 without bounds. Instead one of the weighting factors is the size.
6146 Therefore the result is always a good payoff between few collisions
6147 (= short chain lengths) and table size. */
6148 static size_t
6149 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6150 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6151 unsigned long int nsyms,
6152 int gnu_hash)
6153 {
6154 size_t best_size = 0;
6155 unsigned long int i;
6156
6157 /* We have a problem here. The following code to optimize the table
6158 size requires an integer type with more the 32 bits. If
6159 BFD_HOST_U_64_BIT is set we know about such a type. */
6160 #ifdef BFD_HOST_U_64_BIT
6161 if (info->optimize)
6162 {
6163 size_t minsize;
6164 size_t maxsize;
6165 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6166 bfd *dynobj = elf_hash_table (info)->dynobj;
6167 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6168 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6169 unsigned long int *counts;
6170 bfd_size_type amt;
6171 unsigned int no_improvement_count = 0;
6172
6173 /* Possible optimization parameters: if we have NSYMS symbols we say
6174 that the hashing table must at least have NSYMS/4 and at most
6175 2*NSYMS buckets. */
6176 minsize = nsyms / 4;
6177 if (minsize == 0)
6178 minsize = 1;
6179 best_size = maxsize = nsyms * 2;
6180 if (gnu_hash)
6181 {
6182 if (minsize < 2)
6183 minsize = 2;
6184 if ((best_size & 31) == 0)
6185 ++best_size;
6186 }
6187
6188 /* Create array where we count the collisions in. We must use bfd_malloc
6189 since the size could be large. */
6190 amt = maxsize;
6191 amt *= sizeof (unsigned long int);
6192 counts = (unsigned long int *) bfd_malloc (amt);
6193 if (counts == NULL)
6194 return 0;
6195
6196 /* Compute the "optimal" size for the hash table. The criteria is a
6197 minimal chain length. The minor criteria is (of course) the size
6198 of the table. */
6199 for (i = minsize; i < maxsize; ++i)
6200 {
6201 /* Walk through the array of hashcodes and count the collisions. */
6202 BFD_HOST_U_64_BIT max;
6203 unsigned long int j;
6204 unsigned long int fact;
6205
6206 if (gnu_hash && (i & 31) == 0)
6207 continue;
6208
6209 memset (counts, '\0', i * sizeof (unsigned long int));
6210
6211 /* Determine how often each hash bucket is used. */
6212 for (j = 0; j < nsyms; ++j)
6213 ++counts[hashcodes[j] % i];
6214
6215 /* For the weight function we need some information about the
6216 pagesize on the target. This is information need not be 100%
6217 accurate. Since this information is not available (so far) we
6218 define it here to a reasonable default value. If it is crucial
6219 to have a better value some day simply define this value. */
6220 # ifndef BFD_TARGET_PAGESIZE
6221 # define BFD_TARGET_PAGESIZE (4096)
6222 # endif
6223
6224 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6225 and the chains. */
6226 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6227
6228 # if 1
6229 /* Variant 1: optimize for short chains. We add the squares
6230 of all the chain lengths (which favors many small chain
6231 over a few long chains). */
6232 for (j = 0; j < i; ++j)
6233 max += counts[j] * counts[j];
6234
6235 /* This adds penalties for the overall size of the table. */
6236 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6237 max *= fact * fact;
6238 # else
6239 /* Variant 2: Optimize a lot more for small table. Here we
6240 also add squares of the size but we also add penalties for
6241 empty slots (the +1 term). */
6242 for (j = 0; j < i; ++j)
6243 max += (1 + counts[j]) * (1 + counts[j]);
6244
6245 /* The overall size of the table is considered, but not as
6246 strong as in variant 1, where it is squared. */
6247 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6248 max *= fact;
6249 # endif
6250
6251 /* Compare with current best results. */
6252 if (max < best_chlen)
6253 {
6254 best_chlen = max;
6255 best_size = i;
6256 no_improvement_count = 0;
6257 }
6258 /* PR 11843: Avoid futile long searches for the best bucket size
6259 when there are a large number of symbols. */
6260 else if (++no_improvement_count == 100)
6261 break;
6262 }
6263
6264 free (counts);
6265 }
6266 else
6267 #endif /* defined (BFD_HOST_U_64_BIT) */
6268 {
6269 /* This is the fallback solution if no 64bit type is available or if we
6270 are not supposed to spend much time on optimizations. We select the
6271 bucket count using a fixed set of numbers. */
6272 for (i = 0; elf_buckets[i] != 0; i++)
6273 {
6274 best_size = elf_buckets[i];
6275 if (nsyms < elf_buckets[i + 1])
6276 break;
6277 }
6278 if (gnu_hash && best_size < 2)
6279 best_size = 2;
6280 }
6281
6282 return best_size;
6283 }
6284
6285 /* Size any SHT_GROUP section for ld -r. */
6286
6287 bfd_boolean
6288 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6289 {
6290 bfd *ibfd;
6291 asection *s;
6292
6293 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6294 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6295 && (s = ibfd->sections) != NULL
6296 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6297 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6298 return FALSE;
6299 return TRUE;
6300 }
6301
6302 /* Set a default stack segment size. The value in INFO wins. If it
6303 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6304 undefined it is initialized. */
6305
6306 bfd_boolean
6307 bfd_elf_stack_segment_size (bfd *output_bfd,
6308 struct bfd_link_info *info,
6309 const char *legacy_symbol,
6310 bfd_vma default_size)
6311 {
6312 struct elf_link_hash_entry *h = NULL;
6313
6314 /* Look for legacy symbol. */
6315 if (legacy_symbol)
6316 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6317 FALSE, FALSE, FALSE);
6318 if (h && (h->root.type == bfd_link_hash_defined
6319 || h->root.type == bfd_link_hash_defweak)
6320 && h->def_regular
6321 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6322 {
6323 /* The symbol has no type if specified on the command line. */
6324 h->type = STT_OBJECT;
6325 if (info->stacksize)
6326 /* xgettext:c-format */
6327 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6328 output_bfd, legacy_symbol);
6329 else if (h->root.u.def.section != bfd_abs_section_ptr)
6330 /* xgettext:c-format */
6331 _bfd_error_handler (_("%pB: %s not absolute"),
6332 output_bfd, legacy_symbol);
6333 else
6334 info->stacksize = h->root.u.def.value;
6335 }
6336
6337 if (!info->stacksize)
6338 /* If the user didn't set a size, or explicitly inhibit the
6339 size, set it now. */
6340 info->stacksize = default_size;
6341
6342 /* Provide the legacy symbol, if it is referenced. */
6343 if (h && (h->root.type == bfd_link_hash_undefined
6344 || h->root.type == bfd_link_hash_undefweak))
6345 {
6346 struct bfd_link_hash_entry *bh = NULL;
6347
6348 if (!(_bfd_generic_link_add_one_symbol
6349 (info, output_bfd, legacy_symbol,
6350 BSF_GLOBAL, bfd_abs_section_ptr,
6351 info->stacksize >= 0 ? info->stacksize : 0,
6352 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6353 return FALSE;
6354
6355 h = (struct elf_link_hash_entry *) bh;
6356 h->def_regular = 1;
6357 h->type = STT_OBJECT;
6358 }
6359
6360 return TRUE;
6361 }
6362
6363 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6364
6365 struct elf_gc_sweep_symbol_info
6366 {
6367 struct bfd_link_info *info;
6368 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6369 bfd_boolean);
6370 };
6371
6372 static bfd_boolean
6373 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6374 {
6375 if (!h->mark
6376 && (((h->root.type == bfd_link_hash_defined
6377 || h->root.type == bfd_link_hash_defweak)
6378 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6379 && h->root.u.def.section->gc_mark))
6380 || h->root.type == bfd_link_hash_undefined
6381 || h->root.type == bfd_link_hash_undefweak))
6382 {
6383 struct elf_gc_sweep_symbol_info *inf;
6384
6385 inf = (struct elf_gc_sweep_symbol_info *) data;
6386 (*inf->hide_symbol) (inf->info, h, TRUE);
6387 h->def_regular = 0;
6388 h->ref_regular = 0;
6389 h->ref_regular_nonweak = 0;
6390 }
6391
6392 return TRUE;
6393 }
6394
6395 /* Set up the sizes and contents of the ELF dynamic sections. This is
6396 called by the ELF linker emulation before_allocation routine. We
6397 must set the sizes of the sections before the linker sets the
6398 addresses of the various sections. */
6399
6400 bfd_boolean
6401 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6402 const char *soname,
6403 const char *rpath,
6404 const char *filter_shlib,
6405 const char *audit,
6406 const char *depaudit,
6407 const char * const *auxiliary_filters,
6408 struct bfd_link_info *info,
6409 asection **sinterpptr)
6410 {
6411 bfd *dynobj;
6412 const struct elf_backend_data *bed;
6413
6414 *sinterpptr = NULL;
6415
6416 if (!is_elf_hash_table (info->hash))
6417 return TRUE;
6418
6419 dynobj = elf_hash_table (info)->dynobj;
6420
6421 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6422 {
6423 struct bfd_elf_version_tree *verdefs;
6424 struct elf_info_failed asvinfo;
6425 struct bfd_elf_version_tree *t;
6426 struct bfd_elf_version_expr *d;
6427 asection *s;
6428 size_t soname_indx;
6429
6430 /* If we are supposed to export all symbols into the dynamic symbol
6431 table (this is not the normal case), then do so. */
6432 if (info->export_dynamic
6433 || (bfd_link_executable (info) && info->dynamic))
6434 {
6435 struct elf_info_failed eif;
6436
6437 eif.info = info;
6438 eif.failed = FALSE;
6439 elf_link_hash_traverse (elf_hash_table (info),
6440 _bfd_elf_export_symbol,
6441 &eif);
6442 if (eif.failed)
6443 return FALSE;
6444 }
6445
6446 if (soname != NULL)
6447 {
6448 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6449 soname, TRUE);
6450 if (soname_indx == (size_t) -1
6451 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6452 return FALSE;
6453 }
6454 else
6455 soname_indx = (size_t) -1;
6456
6457 /* Make all global versions with definition. */
6458 for (t = info->version_info; t != NULL; t = t->next)
6459 for (d = t->globals.list; d != NULL; d = d->next)
6460 if (!d->symver && d->literal)
6461 {
6462 const char *verstr, *name;
6463 size_t namelen, verlen, newlen;
6464 char *newname, *p, leading_char;
6465 struct elf_link_hash_entry *newh;
6466
6467 leading_char = bfd_get_symbol_leading_char (output_bfd);
6468 name = d->pattern;
6469 namelen = strlen (name) + (leading_char != '\0');
6470 verstr = t->name;
6471 verlen = strlen (verstr);
6472 newlen = namelen + verlen + 3;
6473
6474 newname = (char *) bfd_malloc (newlen);
6475 if (newname == NULL)
6476 return FALSE;
6477 newname[0] = leading_char;
6478 memcpy (newname + (leading_char != '\0'), name, namelen);
6479
6480 /* Check the hidden versioned definition. */
6481 p = newname + namelen;
6482 *p++ = ELF_VER_CHR;
6483 memcpy (p, verstr, verlen + 1);
6484 newh = elf_link_hash_lookup (elf_hash_table (info),
6485 newname, FALSE, FALSE,
6486 FALSE);
6487 if (newh == NULL
6488 || (newh->root.type != bfd_link_hash_defined
6489 && newh->root.type != bfd_link_hash_defweak))
6490 {
6491 /* Check the default versioned definition. */
6492 *p++ = ELF_VER_CHR;
6493 memcpy (p, verstr, verlen + 1);
6494 newh = elf_link_hash_lookup (elf_hash_table (info),
6495 newname, FALSE, FALSE,
6496 FALSE);
6497 }
6498 free (newname);
6499
6500 /* Mark this version if there is a definition and it is
6501 not defined in a shared object. */
6502 if (newh != NULL
6503 && !newh->def_dynamic
6504 && (newh->root.type == bfd_link_hash_defined
6505 || newh->root.type == bfd_link_hash_defweak))
6506 d->symver = 1;
6507 }
6508
6509 /* Attach all the symbols to their version information. */
6510 asvinfo.info = info;
6511 asvinfo.failed = FALSE;
6512
6513 elf_link_hash_traverse (elf_hash_table (info),
6514 _bfd_elf_link_assign_sym_version,
6515 &asvinfo);
6516 if (asvinfo.failed)
6517 return FALSE;
6518
6519 if (!info->allow_undefined_version)
6520 {
6521 /* Check if all global versions have a definition. */
6522 bfd_boolean all_defined = TRUE;
6523 for (t = info->version_info; t != NULL; t = t->next)
6524 for (d = t->globals.list; d != NULL; d = d->next)
6525 if (d->literal && !d->symver && !d->script)
6526 {
6527 _bfd_error_handler
6528 (_("%s: undefined version: %s"),
6529 d->pattern, t->name);
6530 all_defined = FALSE;
6531 }
6532
6533 if (!all_defined)
6534 {
6535 bfd_set_error (bfd_error_bad_value);
6536 return FALSE;
6537 }
6538 }
6539
6540 /* Set up the version definition section. */
6541 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6542 BFD_ASSERT (s != NULL);
6543
6544 /* We may have created additional version definitions if we are
6545 just linking a regular application. */
6546 verdefs = info->version_info;
6547
6548 /* Skip anonymous version tag. */
6549 if (verdefs != NULL && verdefs->vernum == 0)
6550 verdefs = verdefs->next;
6551
6552 if (verdefs == NULL && !info->create_default_symver)
6553 s->flags |= SEC_EXCLUDE;
6554 else
6555 {
6556 unsigned int cdefs;
6557 bfd_size_type size;
6558 bfd_byte *p;
6559 Elf_Internal_Verdef def;
6560 Elf_Internal_Verdaux defaux;
6561 struct bfd_link_hash_entry *bh;
6562 struct elf_link_hash_entry *h;
6563 const char *name;
6564
6565 cdefs = 0;
6566 size = 0;
6567
6568 /* Make space for the base version. */
6569 size += sizeof (Elf_External_Verdef);
6570 size += sizeof (Elf_External_Verdaux);
6571 ++cdefs;
6572
6573 /* Make space for the default version. */
6574 if (info->create_default_symver)
6575 {
6576 size += sizeof (Elf_External_Verdef);
6577 ++cdefs;
6578 }
6579
6580 for (t = verdefs; t != NULL; t = t->next)
6581 {
6582 struct bfd_elf_version_deps *n;
6583
6584 /* Don't emit base version twice. */
6585 if (t->vernum == 0)
6586 continue;
6587
6588 size += sizeof (Elf_External_Verdef);
6589 size += sizeof (Elf_External_Verdaux);
6590 ++cdefs;
6591
6592 for (n = t->deps; n != NULL; n = n->next)
6593 size += sizeof (Elf_External_Verdaux);
6594 }
6595
6596 s->size = size;
6597 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6598 if (s->contents == NULL && s->size != 0)
6599 return FALSE;
6600
6601 /* Fill in the version definition section. */
6602
6603 p = s->contents;
6604
6605 def.vd_version = VER_DEF_CURRENT;
6606 def.vd_flags = VER_FLG_BASE;
6607 def.vd_ndx = 1;
6608 def.vd_cnt = 1;
6609 if (info->create_default_symver)
6610 {
6611 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6612 def.vd_next = sizeof (Elf_External_Verdef);
6613 }
6614 else
6615 {
6616 def.vd_aux = sizeof (Elf_External_Verdef);
6617 def.vd_next = (sizeof (Elf_External_Verdef)
6618 + sizeof (Elf_External_Verdaux));
6619 }
6620
6621 if (soname_indx != (size_t) -1)
6622 {
6623 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6624 soname_indx);
6625 def.vd_hash = bfd_elf_hash (soname);
6626 defaux.vda_name = soname_indx;
6627 name = soname;
6628 }
6629 else
6630 {
6631 size_t indx;
6632
6633 name = lbasename (bfd_get_filename (output_bfd));
6634 def.vd_hash = bfd_elf_hash (name);
6635 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6636 name, FALSE);
6637 if (indx == (size_t) -1)
6638 return FALSE;
6639 defaux.vda_name = indx;
6640 }
6641 defaux.vda_next = 0;
6642
6643 _bfd_elf_swap_verdef_out (output_bfd, &def,
6644 (Elf_External_Verdef *) p);
6645 p += sizeof (Elf_External_Verdef);
6646 if (info->create_default_symver)
6647 {
6648 /* Add a symbol representing this version. */
6649 bh = NULL;
6650 if (! (_bfd_generic_link_add_one_symbol
6651 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6652 0, NULL, FALSE,
6653 get_elf_backend_data (dynobj)->collect, &bh)))
6654 return FALSE;
6655 h = (struct elf_link_hash_entry *) bh;
6656 h->non_elf = 0;
6657 h->def_regular = 1;
6658 h->type = STT_OBJECT;
6659 h->verinfo.vertree = NULL;
6660
6661 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6662 return FALSE;
6663
6664 /* Create a duplicate of the base version with the same
6665 aux block, but different flags. */
6666 def.vd_flags = 0;
6667 def.vd_ndx = 2;
6668 def.vd_aux = sizeof (Elf_External_Verdef);
6669 if (verdefs)
6670 def.vd_next = (sizeof (Elf_External_Verdef)
6671 + sizeof (Elf_External_Verdaux));
6672 else
6673 def.vd_next = 0;
6674 _bfd_elf_swap_verdef_out (output_bfd, &def,
6675 (Elf_External_Verdef *) p);
6676 p += sizeof (Elf_External_Verdef);
6677 }
6678 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6679 (Elf_External_Verdaux *) p);
6680 p += sizeof (Elf_External_Verdaux);
6681
6682 for (t = verdefs; t != NULL; t = t->next)
6683 {
6684 unsigned int cdeps;
6685 struct bfd_elf_version_deps *n;
6686
6687 /* Don't emit the base version twice. */
6688 if (t->vernum == 0)
6689 continue;
6690
6691 cdeps = 0;
6692 for (n = t->deps; n != NULL; n = n->next)
6693 ++cdeps;
6694
6695 /* Add a symbol representing this version. */
6696 bh = NULL;
6697 if (! (_bfd_generic_link_add_one_symbol
6698 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6699 0, NULL, FALSE,
6700 get_elf_backend_data (dynobj)->collect, &bh)))
6701 return FALSE;
6702 h = (struct elf_link_hash_entry *) bh;
6703 h->non_elf = 0;
6704 h->def_regular = 1;
6705 h->type = STT_OBJECT;
6706 h->verinfo.vertree = t;
6707
6708 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6709 return FALSE;
6710
6711 def.vd_version = VER_DEF_CURRENT;
6712 def.vd_flags = 0;
6713 if (t->globals.list == NULL
6714 && t->locals.list == NULL
6715 && ! t->used)
6716 def.vd_flags |= VER_FLG_WEAK;
6717 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6718 def.vd_cnt = cdeps + 1;
6719 def.vd_hash = bfd_elf_hash (t->name);
6720 def.vd_aux = sizeof (Elf_External_Verdef);
6721 def.vd_next = 0;
6722
6723 /* If a basever node is next, it *must* be the last node in
6724 the chain, otherwise Verdef construction breaks. */
6725 if (t->next != NULL && t->next->vernum == 0)
6726 BFD_ASSERT (t->next->next == NULL);
6727
6728 if (t->next != NULL && t->next->vernum != 0)
6729 def.vd_next = (sizeof (Elf_External_Verdef)
6730 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6731
6732 _bfd_elf_swap_verdef_out (output_bfd, &def,
6733 (Elf_External_Verdef *) p);
6734 p += sizeof (Elf_External_Verdef);
6735
6736 defaux.vda_name = h->dynstr_index;
6737 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6738 h->dynstr_index);
6739 defaux.vda_next = 0;
6740 if (t->deps != NULL)
6741 defaux.vda_next = sizeof (Elf_External_Verdaux);
6742 t->name_indx = defaux.vda_name;
6743
6744 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6745 (Elf_External_Verdaux *) p);
6746 p += sizeof (Elf_External_Verdaux);
6747
6748 for (n = t->deps; n != NULL; n = n->next)
6749 {
6750 if (n->version_needed == NULL)
6751 {
6752 /* This can happen if there was an error in the
6753 version script. */
6754 defaux.vda_name = 0;
6755 }
6756 else
6757 {
6758 defaux.vda_name = n->version_needed->name_indx;
6759 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6760 defaux.vda_name);
6761 }
6762 if (n->next == NULL)
6763 defaux.vda_next = 0;
6764 else
6765 defaux.vda_next = sizeof (Elf_External_Verdaux);
6766
6767 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6768 (Elf_External_Verdaux *) p);
6769 p += sizeof (Elf_External_Verdaux);
6770 }
6771 }
6772
6773 elf_tdata (output_bfd)->cverdefs = cdefs;
6774 }
6775 }
6776
6777 bed = get_elf_backend_data (output_bfd);
6778
6779 if (info->gc_sections && bed->can_gc_sections)
6780 {
6781 struct elf_gc_sweep_symbol_info sweep_info;
6782
6783 /* Remove the symbols that were in the swept sections from the
6784 dynamic symbol table. */
6785 sweep_info.info = info;
6786 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6787 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6788 &sweep_info);
6789 }
6790
6791 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6792 {
6793 asection *s;
6794 struct elf_find_verdep_info sinfo;
6795
6796 /* Work out the size of the version reference section. */
6797
6798 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6799 BFD_ASSERT (s != NULL);
6800
6801 sinfo.info = info;
6802 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6803 if (sinfo.vers == 0)
6804 sinfo.vers = 1;
6805 sinfo.failed = FALSE;
6806
6807 elf_link_hash_traverse (elf_hash_table (info),
6808 _bfd_elf_link_find_version_dependencies,
6809 &sinfo);
6810 if (sinfo.failed)
6811 return FALSE;
6812
6813 if (elf_tdata (output_bfd)->verref == NULL)
6814 s->flags |= SEC_EXCLUDE;
6815 else
6816 {
6817 Elf_Internal_Verneed *vn;
6818 unsigned int size;
6819 unsigned int crefs;
6820 bfd_byte *p;
6821
6822 /* Build the version dependency section. */
6823 size = 0;
6824 crefs = 0;
6825 for (vn = elf_tdata (output_bfd)->verref;
6826 vn != NULL;
6827 vn = vn->vn_nextref)
6828 {
6829 Elf_Internal_Vernaux *a;
6830
6831 size += sizeof (Elf_External_Verneed);
6832 ++crefs;
6833 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6834 size += sizeof (Elf_External_Vernaux);
6835 }
6836
6837 s->size = size;
6838 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6839 if (s->contents == NULL)
6840 return FALSE;
6841
6842 p = s->contents;
6843 for (vn = elf_tdata (output_bfd)->verref;
6844 vn != NULL;
6845 vn = vn->vn_nextref)
6846 {
6847 unsigned int caux;
6848 Elf_Internal_Vernaux *a;
6849 size_t indx;
6850
6851 caux = 0;
6852 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6853 ++caux;
6854
6855 vn->vn_version = VER_NEED_CURRENT;
6856 vn->vn_cnt = caux;
6857 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6858 elf_dt_name (vn->vn_bfd) != NULL
6859 ? elf_dt_name (vn->vn_bfd)
6860 : lbasename (bfd_get_filename
6861 (vn->vn_bfd)),
6862 FALSE);
6863 if (indx == (size_t) -1)
6864 return FALSE;
6865 vn->vn_file = indx;
6866 vn->vn_aux = sizeof (Elf_External_Verneed);
6867 if (vn->vn_nextref == NULL)
6868 vn->vn_next = 0;
6869 else
6870 vn->vn_next = (sizeof (Elf_External_Verneed)
6871 + caux * sizeof (Elf_External_Vernaux));
6872
6873 _bfd_elf_swap_verneed_out (output_bfd, vn,
6874 (Elf_External_Verneed *) p);
6875 p += sizeof (Elf_External_Verneed);
6876
6877 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6878 {
6879 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6881 a->vna_nodename, FALSE);
6882 if (indx == (size_t) -1)
6883 return FALSE;
6884 a->vna_name = indx;
6885 if (a->vna_nextptr == NULL)
6886 a->vna_next = 0;
6887 else
6888 a->vna_next = sizeof (Elf_External_Vernaux);
6889
6890 _bfd_elf_swap_vernaux_out (output_bfd, a,
6891 (Elf_External_Vernaux *) p);
6892 p += sizeof (Elf_External_Vernaux);
6893 }
6894 }
6895
6896 elf_tdata (output_bfd)->cverrefs = crefs;
6897 }
6898 }
6899
6900 /* Any syms created from now on start with -1 in
6901 got.refcount/offset and plt.refcount/offset. */
6902 elf_hash_table (info)->init_got_refcount
6903 = elf_hash_table (info)->init_got_offset;
6904 elf_hash_table (info)->init_plt_refcount
6905 = elf_hash_table (info)->init_plt_offset;
6906
6907 if (bfd_link_relocatable (info)
6908 && !_bfd_elf_size_group_sections (info))
6909 return FALSE;
6910
6911 /* The backend may have to create some sections regardless of whether
6912 we're dynamic or not. */
6913 if (bed->elf_backend_always_size_sections
6914 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6915 return FALSE;
6916
6917 /* Determine any GNU_STACK segment requirements, after the backend
6918 has had a chance to set a default segment size. */
6919 if (info->execstack)
6920 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6921 else if (info->noexecstack)
6922 elf_stack_flags (output_bfd) = PF_R | PF_W;
6923 else
6924 {
6925 bfd *inputobj;
6926 asection *notesec = NULL;
6927 int exec = 0;
6928
6929 for (inputobj = info->input_bfds;
6930 inputobj;
6931 inputobj = inputobj->link.next)
6932 {
6933 asection *s;
6934
6935 if (inputobj->flags
6936 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6937 continue;
6938 s = inputobj->sections;
6939 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6940 continue;
6941
6942 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6943 if (s)
6944 {
6945 if (s->flags & SEC_CODE)
6946 exec = PF_X;
6947 notesec = s;
6948 }
6949 else if (bed->default_execstack)
6950 exec = PF_X;
6951 }
6952 if (notesec || info->stacksize > 0)
6953 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6954 if (notesec && exec && bfd_link_relocatable (info)
6955 && notesec->output_section != bfd_abs_section_ptr)
6956 notesec->output_section->flags |= SEC_CODE;
6957 }
6958
6959 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6960 {
6961 struct elf_info_failed eif;
6962 struct elf_link_hash_entry *h;
6963 asection *dynstr;
6964 asection *s;
6965
6966 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6967 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6968
6969 if (info->symbolic)
6970 {
6971 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6972 return FALSE;
6973 info->flags |= DF_SYMBOLIC;
6974 }
6975
6976 if (rpath != NULL)
6977 {
6978 size_t indx;
6979 bfd_vma tag;
6980
6981 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6982 TRUE);
6983 if (indx == (size_t) -1)
6984 return FALSE;
6985
6986 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6987 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6988 return FALSE;
6989 }
6990
6991 if (filter_shlib != NULL)
6992 {
6993 size_t indx;
6994
6995 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6996 filter_shlib, TRUE);
6997 if (indx == (size_t) -1
6998 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6999 return FALSE;
7000 }
7001
7002 if (auxiliary_filters != NULL)
7003 {
7004 const char * const *p;
7005
7006 for (p = auxiliary_filters; *p != NULL; p++)
7007 {
7008 size_t indx;
7009
7010 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7011 *p, TRUE);
7012 if (indx == (size_t) -1
7013 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7014 return FALSE;
7015 }
7016 }
7017
7018 if (audit != NULL)
7019 {
7020 size_t indx;
7021
7022 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7023 TRUE);
7024 if (indx == (size_t) -1
7025 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7026 return FALSE;
7027 }
7028
7029 if (depaudit != NULL)
7030 {
7031 size_t indx;
7032
7033 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7034 TRUE);
7035 if (indx == (size_t) -1
7036 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7037 return FALSE;
7038 }
7039
7040 eif.info = info;
7041 eif.failed = FALSE;
7042
7043 /* Find all symbols which were defined in a dynamic object and make
7044 the backend pick a reasonable value for them. */
7045 elf_link_hash_traverse (elf_hash_table (info),
7046 _bfd_elf_adjust_dynamic_symbol,
7047 &eif);
7048 if (eif.failed)
7049 return FALSE;
7050
7051 /* Add some entries to the .dynamic section. We fill in some of the
7052 values later, in bfd_elf_final_link, but we must add the entries
7053 now so that we know the final size of the .dynamic section. */
7054
7055 /* If there are initialization and/or finalization functions to
7056 call then add the corresponding DT_INIT/DT_FINI entries. */
7057 h = (info->init_function
7058 ? elf_link_hash_lookup (elf_hash_table (info),
7059 info->init_function, FALSE,
7060 FALSE, FALSE)
7061 : NULL);
7062 if (h != NULL
7063 && (h->ref_regular
7064 || h->def_regular))
7065 {
7066 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7067 return FALSE;
7068 }
7069 h = (info->fini_function
7070 ? elf_link_hash_lookup (elf_hash_table (info),
7071 info->fini_function, FALSE,
7072 FALSE, FALSE)
7073 : NULL);
7074 if (h != NULL
7075 && (h->ref_regular
7076 || h->def_regular))
7077 {
7078 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7079 return FALSE;
7080 }
7081
7082 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7083 if (s != NULL && s->linker_has_input)
7084 {
7085 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7086 if (! bfd_link_executable (info))
7087 {
7088 bfd *sub;
7089 asection *o;
7090
7091 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7092 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7093 && (o = sub->sections) != NULL
7094 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7095 for (o = sub->sections; o != NULL; o = o->next)
7096 if (elf_section_data (o)->this_hdr.sh_type
7097 == SHT_PREINIT_ARRAY)
7098 {
7099 _bfd_error_handler
7100 (_("%pB: .preinit_array section is not allowed in DSO"),
7101 sub);
7102 break;
7103 }
7104
7105 bfd_set_error (bfd_error_nonrepresentable_section);
7106 return FALSE;
7107 }
7108
7109 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7110 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7111 return FALSE;
7112 }
7113 s = bfd_get_section_by_name (output_bfd, ".init_array");
7114 if (s != NULL && s->linker_has_input)
7115 {
7116 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7117 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7118 return FALSE;
7119 }
7120 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7121 if (s != NULL && s->linker_has_input)
7122 {
7123 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7124 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7125 return FALSE;
7126 }
7127
7128 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7129 /* If .dynstr is excluded from the link, we don't want any of
7130 these tags. Strictly, we should be checking each section
7131 individually; This quick check covers for the case where
7132 someone does a /DISCARD/ : { *(*) }. */
7133 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7134 {
7135 bfd_size_type strsize;
7136
7137 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7138 if ((info->emit_hash
7139 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7140 || (info->emit_gnu_hash
7141 && (bed->record_xhash_symbol == NULL
7142 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7143 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7144 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7145 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7146 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7147 bed->s->sizeof_sym))
7148 return FALSE;
7149 }
7150 }
7151
7152 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7153 return FALSE;
7154
7155 /* The backend must work out the sizes of all the other dynamic
7156 sections. */
7157 if (dynobj != NULL
7158 && bed->elf_backend_size_dynamic_sections != NULL
7159 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7160 return FALSE;
7161
7162 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7163 {
7164 if (elf_tdata (output_bfd)->cverdefs)
7165 {
7166 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7167
7168 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7169 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7170 return FALSE;
7171 }
7172
7173 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7174 {
7175 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7176 return FALSE;
7177 }
7178 else if (info->flags & DF_BIND_NOW)
7179 {
7180 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7181 return FALSE;
7182 }
7183
7184 if (info->flags_1)
7185 {
7186 if (bfd_link_executable (info))
7187 info->flags_1 &= ~ (DF_1_INITFIRST
7188 | DF_1_NODELETE
7189 | DF_1_NOOPEN);
7190 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7191 return FALSE;
7192 }
7193
7194 if (elf_tdata (output_bfd)->cverrefs)
7195 {
7196 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7197
7198 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7199 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7200 return FALSE;
7201 }
7202
7203 if ((elf_tdata (output_bfd)->cverrefs == 0
7204 && elf_tdata (output_bfd)->cverdefs == 0)
7205 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7206 {
7207 asection *s;
7208
7209 s = bfd_get_linker_section (dynobj, ".gnu.version");
7210 s->flags |= SEC_EXCLUDE;
7211 }
7212 }
7213 return TRUE;
7214 }
7215
7216 /* Find the first non-excluded output section. We'll use its
7217 section symbol for some emitted relocs. */
7218 void
7219 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7220 {
7221 asection *s;
7222 asection *found = NULL;
7223
7224 for (s = output_bfd->sections; s != NULL; s = s->next)
7225 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7226 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7227 {
7228 found = s;
7229 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7230 break;
7231 }
7232 elf_hash_table (info)->text_index_section = found;
7233 }
7234
7235 /* Find two non-excluded output sections, one for code, one for data.
7236 We'll use their section symbols for some emitted relocs. */
7237 void
7238 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7239 {
7240 asection *s;
7241 asection *found = NULL;
7242
7243 /* Data first, since setting text_index_section changes
7244 _bfd_elf_omit_section_dynsym_default. */
7245 for (s = output_bfd->sections; s != NULL; s = s->next)
7246 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7247 && !(s->flags & SEC_READONLY)
7248 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7249 {
7250 found = s;
7251 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7252 break;
7253 }
7254 elf_hash_table (info)->data_index_section = found;
7255
7256 for (s = output_bfd->sections; s != NULL; s = s->next)
7257 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7258 && (s->flags & SEC_READONLY)
7259 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7260 {
7261 found = s;
7262 break;
7263 }
7264 elf_hash_table (info)->text_index_section = found;
7265 }
7266
7267 #define GNU_HASH_SECTION_NAME(bed) \
7268 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7269
7270 bfd_boolean
7271 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7272 {
7273 const struct elf_backend_data *bed;
7274 unsigned long section_sym_count;
7275 bfd_size_type dynsymcount = 0;
7276
7277 if (!is_elf_hash_table (info->hash))
7278 return TRUE;
7279
7280 bed = get_elf_backend_data (output_bfd);
7281 (*bed->elf_backend_init_index_section) (output_bfd, info);
7282
7283 /* Assign dynsym indices. In a shared library we generate a section
7284 symbol for each output section, which come first. Next come all
7285 of the back-end allocated local dynamic syms, followed by the rest
7286 of the global symbols.
7287
7288 This is usually not needed for static binaries, however backends
7289 can request to always do it, e.g. the MIPS backend uses dynamic
7290 symbol counts to lay out GOT, which will be produced in the
7291 presence of GOT relocations even in static binaries (holding fixed
7292 data in that case, to satisfy those relocations). */
7293
7294 if (elf_hash_table (info)->dynamic_sections_created
7295 || bed->always_renumber_dynsyms)
7296 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7297 &section_sym_count);
7298
7299 if (elf_hash_table (info)->dynamic_sections_created)
7300 {
7301 bfd *dynobj;
7302 asection *s;
7303 unsigned int dtagcount;
7304
7305 dynobj = elf_hash_table (info)->dynobj;
7306
7307 /* Work out the size of the symbol version section. */
7308 s = bfd_get_linker_section (dynobj, ".gnu.version");
7309 BFD_ASSERT (s != NULL);
7310 if ((s->flags & SEC_EXCLUDE) == 0)
7311 {
7312 s->size = dynsymcount * sizeof (Elf_External_Versym);
7313 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7314 if (s->contents == NULL)
7315 return FALSE;
7316
7317 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7318 return FALSE;
7319 }
7320
7321 /* Set the size of the .dynsym and .hash sections. We counted
7322 the number of dynamic symbols in elf_link_add_object_symbols.
7323 We will build the contents of .dynsym and .hash when we build
7324 the final symbol table, because until then we do not know the
7325 correct value to give the symbols. We built the .dynstr
7326 section as we went along in elf_link_add_object_symbols. */
7327 s = elf_hash_table (info)->dynsym;
7328 BFD_ASSERT (s != NULL);
7329 s->size = dynsymcount * bed->s->sizeof_sym;
7330
7331 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7332 if (s->contents == NULL)
7333 return FALSE;
7334
7335 /* The first entry in .dynsym is a dummy symbol. Clear all the
7336 section syms, in case we don't output them all. */
7337 ++section_sym_count;
7338 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7339
7340 elf_hash_table (info)->bucketcount = 0;
7341
7342 /* Compute the size of the hashing table. As a side effect this
7343 computes the hash values for all the names we export. */
7344 if (info->emit_hash)
7345 {
7346 unsigned long int *hashcodes;
7347 struct hash_codes_info hashinf;
7348 bfd_size_type amt;
7349 unsigned long int nsyms;
7350 size_t bucketcount;
7351 size_t hash_entry_size;
7352
7353 /* Compute the hash values for all exported symbols. At the same
7354 time store the values in an array so that we could use them for
7355 optimizations. */
7356 amt = dynsymcount * sizeof (unsigned long int);
7357 hashcodes = (unsigned long int *) bfd_malloc (amt);
7358 if (hashcodes == NULL)
7359 return FALSE;
7360 hashinf.hashcodes = hashcodes;
7361 hashinf.error = FALSE;
7362
7363 /* Put all hash values in HASHCODES. */
7364 elf_link_hash_traverse (elf_hash_table (info),
7365 elf_collect_hash_codes, &hashinf);
7366 if (hashinf.error)
7367 {
7368 free (hashcodes);
7369 return FALSE;
7370 }
7371
7372 nsyms = hashinf.hashcodes - hashcodes;
7373 bucketcount
7374 = compute_bucket_count (info, hashcodes, nsyms, 0);
7375 free (hashcodes);
7376
7377 if (bucketcount == 0 && nsyms > 0)
7378 return FALSE;
7379
7380 elf_hash_table (info)->bucketcount = bucketcount;
7381
7382 s = bfd_get_linker_section (dynobj, ".hash");
7383 BFD_ASSERT (s != NULL);
7384 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7385 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7386 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7387 if (s->contents == NULL)
7388 return FALSE;
7389
7390 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7391 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7392 s->contents + hash_entry_size);
7393 }
7394
7395 if (info->emit_gnu_hash)
7396 {
7397 size_t i, cnt;
7398 unsigned char *contents;
7399 struct collect_gnu_hash_codes cinfo;
7400 bfd_size_type amt;
7401 size_t bucketcount;
7402
7403 memset (&cinfo, 0, sizeof (cinfo));
7404
7405 /* Compute the hash values for all exported symbols. At the same
7406 time store the values in an array so that we could use them for
7407 optimizations. */
7408 amt = dynsymcount * 2 * sizeof (unsigned long int);
7409 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7410 if (cinfo.hashcodes == NULL)
7411 return FALSE;
7412
7413 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7414 cinfo.min_dynindx = -1;
7415 cinfo.output_bfd = output_bfd;
7416 cinfo.bed = bed;
7417
7418 /* Put all hash values in HASHCODES. */
7419 elf_link_hash_traverse (elf_hash_table (info),
7420 elf_collect_gnu_hash_codes, &cinfo);
7421 if (cinfo.error)
7422 {
7423 free (cinfo.hashcodes);
7424 return FALSE;
7425 }
7426
7427 bucketcount
7428 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7429
7430 if (bucketcount == 0)
7431 {
7432 free (cinfo.hashcodes);
7433 return FALSE;
7434 }
7435
7436 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7437 BFD_ASSERT (s != NULL);
7438
7439 if (cinfo.nsyms == 0)
7440 {
7441 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7442 BFD_ASSERT (cinfo.min_dynindx == -1);
7443 free (cinfo.hashcodes);
7444 s->size = 5 * 4 + bed->s->arch_size / 8;
7445 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7446 if (contents == NULL)
7447 return FALSE;
7448 s->contents = contents;
7449 /* 1 empty bucket. */
7450 bfd_put_32 (output_bfd, 1, contents);
7451 /* SYMIDX above the special symbol 0. */
7452 bfd_put_32 (output_bfd, 1, contents + 4);
7453 /* Just one word for bitmask. */
7454 bfd_put_32 (output_bfd, 1, contents + 8);
7455 /* Only hash fn bloom filter. */
7456 bfd_put_32 (output_bfd, 0, contents + 12);
7457 /* No hashes are valid - empty bitmask. */
7458 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7459 /* No hashes in the only bucket. */
7460 bfd_put_32 (output_bfd, 0,
7461 contents + 16 + bed->s->arch_size / 8);
7462 }
7463 else
7464 {
7465 unsigned long int maskwords, maskbitslog2, x;
7466 BFD_ASSERT (cinfo.min_dynindx != -1);
7467
7468 x = cinfo.nsyms;
7469 maskbitslog2 = 1;
7470 while ((x >>= 1) != 0)
7471 ++maskbitslog2;
7472 if (maskbitslog2 < 3)
7473 maskbitslog2 = 5;
7474 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7475 maskbitslog2 = maskbitslog2 + 3;
7476 else
7477 maskbitslog2 = maskbitslog2 + 2;
7478 if (bed->s->arch_size == 64)
7479 {
7480 if (maskbitslog2 == 5)
7481 maskbitslog2 = 6;
7482 cinfo.shift1 = 6;
7483 }
7484 else
7485 cinfo.shift1 = 5;
7486 cinfo.mask = (1 << cinfo.shift1) - 1;
7487 cinfo.shift2 = maskbitslog2;
7488 cinfo.maskbits = 1 << maskbitslog2;
7489 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7490 amt = bucketcount * sizeof (unsigned long int) * 2;
7491 amt += maskwords * sizeof (bfd_vma);
7492 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7493 if (cinfo.bitmask == NULL)
7494 {
7495 free (cinfo.hashcodes);
7496 return FALSE;
7497 }
7498
7499 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7500 cinfo.indx = cinfo.counts + bucketcount;
7501 cinfo.symindx = dynsymcount - cinfo.nsyms;
7502 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7503
7504 /* Determine how often each hash bucket is used. */
7505 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7506 for (i = 0; i < cinfo.nsyms; ++i)
7507 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7508
7509 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7510 if (cinfo.counts[i] != 0)
7511 {
7512 cinfo.indx[i] = cnt;
7513 cnt += cinfo.counts[i];
7514 }
7515 BFD_ASSERT (cnt == dynsymcount);
7516 cinfo.bucketcount = bucketcount;
7517 cinfo.local_indx = cinfo.min_dynindx;
7518
7519 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7520 s->size += cinfo.maskbits / 8;
7521 if (bed->record_xhash_symbol != NULL)
7522 s->size += cinfo.nsyms * 4;
7523 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7524 if (contents == NULL)
7525 {
7526 free (cinfo.bitmask);
7527 free (cinfo.hashcodes);
7528 return FALSE;
7529 }
7530
7531 s->contents = contents;
7532 bfd_put_32 (output_bfd, bucketcount, contents);
7533 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7534 bfd_put_32 (output_bfd, maskwords, contents + 8);
7535 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7536 contents += 16 + cinfo.maskbits / 8;
7537
7538 for (i = 0; i < bucketcount; ++i)
7539 {
7540 if (cinfo.counts[i] == 0)
7541 bfd_put_32 (output_bfd, 0, contents);
7542 else
7543 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7544 contents += 4;
7545 }
7546
7547 cinfo.contents = contents;
7548
7549 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7550 /* Renumber dynamic symbols, if populating .gnu.hash section.
7551 If using .MIPS.xhash, populate the translation table. */
7552 elf_link_hash_traverse (elf_hash_table (info),
7553 elf_gnu_hash_process_symidx, &cinfo);
7554
7555 contents = s->contents + 16;
7556 for (i = 0; i < maskwords; ++i)
7557 {
7558 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7559 contents);
7560 contents += bed->s->arch_size / 8;
7561 }
7562
7563 free (cinfo.bitmask);
7564 free (cinfo.hashcodes);
7565 }
7566 }
7567
7568 s = bfd_get_linker_section (dynobj, ".dynstr");
7569 BFD_ASSERT (s != NULL);
7570
7571 elf_finalize_dynstr (output_bfd, info);
7572
7573 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7574
7575 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7576 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7577 return FALSE;
7578 }
7579
7580 return TRUE;
7581 }
7582 \f
7583 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7584
7585 static void
7586 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7587 asection *sec)
7588 {
7589 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7590 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7591 }
7592
7593 /* Finish SHF_MERGE section merging. */
7594
7595 bfd_boolean
7596 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7597 {
7598 bfd *ibfd;
7599 asection *sec;
7600
7601 if (!is_elf_hash_table (info->hash))
7602 return FALSE;
7603
7604 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7605 if ((ibfd->flags & DYNAMIC) == 0
7606 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7607 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7608 == get_elf_backend_data (obfd)->s->elfclass))
7609 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7610 if ((sec->flags & SEC_MERGE) != 0
7611 && !bfd_is_abs_section (sec->output_section))
7612 {
7613 struct bfd_elf_section_data *secdata;
7614
7615 secdata = elf_section_data (sec);
7616 if (! _bfd_add_merge_section (obfd,
7617 &elf_hash_table (info)->merge_info,
7618 sec, &secdata->sec_info))
7619 return FALSE;
7620 else if (secdata->sec_info)
7621 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7622 }
7623
7624 if (elf_hash_table (info)->merge_info != NULL)
7625 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7626 merge_sections_remove_hook);
7627 return TRUE;
7628 }
7629
7630 /* Create an entry in an ELF linker hash table. */
7631
7632 struct bfd_hash_entry *
7633 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7634 struct bfd_hash_table *table,
7635 const char *string)
7636 {
7637 /* Allocate the structure if it has not already been allocated by a
7638 subclass. */
7639 if (entry == NULL)
7640 {
7641 entry = (struct bfd_hash_entry *)
7642 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7643 if (entry == NULL)
7644 return entry;
7645 }
7646
7647 /* Call the allocation method of the superclass. */
7648 entry = _bfd_link_hash_newfunc (entry, table, string);
7649 if (entry != NULL)
7650 {
7651 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7652 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7653
7654 /* Set local fields. */
7655 ret->indx = -1;
7656 ret->dynindx = -1;
7657 ret->got = htab->init_got_refcount;
7658 ret->plt = htab->init_plt_refcount;
7659 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7660 - offsetof (struct elf_link_hash_entry, size)));
7661 /* Assume that we have been called by a non-ELF symbol reader.
7662 This flag is then reset by the code which reads an ELF input
7663 file. This ensures that a symbol created by a non-ELF symbol
7664 reader will have the flag set correctly. */
7665 ret->non_elf = 1;
7666 }
7667
7668 return entry;
7669 }
7670
7671 /* Copy data from an indirect symbol to its direct symbol, hiding the
7672 old indirect symbol. Also used for copying flags to a weakdef. */
7673
7674 void
7675 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7676 struct elf_link_hash_entry *dir,
7677 struct elf_link_hash_entry *ind)
7678 {
7679 struct elf_link_hash_table *htab;
7680
7681 if (ind->dyn_relocs != NULL)
7682 {
7683 if (dir->dyn_relocs != NULL)
7684 {
7685 struct elf_dyn_relocs **pp;
7686 struct elf_dyn_relocs *p;
7687
7688 /* Add reloc counts against the indirect sym to the direct sym
7689 list. Merge any entries against the same section. */
7690 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7691 {
7692 struct elf_dyn_relocs *q;
7693
7694 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7695 if (q->sec == p->sec)
7696 {
7697 q->pc_count += p->pc_count;
7698 q->count += p->count;
7699 *pp = p->next;
7700 break;
7701 }
7702 if (q == NULL)
7703 pp = &p->next;
7704 }
7705 *pp = dir->dyn_relocs;
7706 }
7707
7708 dir->dyn_relocs = ind->dyn_relocs;
7709 ind->dyn_relocs = NULL;
7710 }
7711
7712 /* Copy down any references that we may have already seen to the
7713 symbol which just became indirect. */
7714
7715 if (dir->versioned != versioned_hidden)
7716 dir->ref_dynamic |= ind->ref_dynamic;
7717 dir->ref_regular |= ind->ref_regular;
7718 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7719 dir->non_got_ref |= ind->non_got_ref;
7720 dir->needs_plt |= ind->needs_plt;
7721 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7722
7723 if (ind->root.type != bfd_link_hash_indirect)
7724 return;
7725
7726 /* Copy over the global and procedure linkage table refcount entries.
7727 These may have been already set up by a check_relocs routine. */
7728 htab = elf_hash_table (info);
7729 if (ind->got.refcount > htab->init_got_refcount.refcount)
7730 {
7731 if (dir->got.refcount < 0)
7732 dir->got.refcount = 0;
7733 dir->got.refcount += ind->got.refcount;
7734 ind->got.refcount = htab->init_got_refcount.refcount;
7735 }
7736
7737 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7738 {
7739 if (dir->plt.refcount < 0)
7740 dir->plt.refcount = 0;
7741 dir->plt.refcount += ind->plt.refcount;
7742 ind->plt.refcount = htab->init_plt_refcount.refcount;
7743 }
7744
7745 if (ind->dynindx != -1)
7746 {
7747 if (dir->dynindx != -1)
7748 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7749 dir->dynindx = ind->dynindx;
7750 dir->dynstr_index = ind->dynstr_index;
7751 ind->dynindx = -1;
7752 ind->dynstr_index = 0;
7753 }
7754 }
7755
7756 void
7757 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7758 struct elf_link_hash_entry *h,
7759 bfd_boolean force_local)
7760 {
7761 /* STT_GNU_IFUNC symbol must go through PLT. */
7762 if (h->type != STT_GNU_IFUNC)
7763 {
7764 h->plt = elf_hash_table (info)->init_plt_offset;
7765 h->needs_plt = 0;
7766 }
7767 if (force_local)
7768 {
7769 h->forced_local = 1;
7770 if (h->dynindx != -1)
7771 {
7772 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7773 h->dynstr_index);
7774 h->dynindx = -1;
7775 h->dynstr_index = 0;
7776 }
7777 }
7778 }
7779
7780 /* Hide a symbol. */
7781
7782 void
7783 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7784 struct bfd_link_info *info,
7785 struct bfd_link_hash_entry *h)
7786 {
7787 if (is_elf_hash_table (info->hash))
7788 {
7789 const struct elf_backend_data *bed
7790 = get_elf_backend_data (output_bfd);
7791 struct elf_link_hash_entry *eh
7792 = (struct elf_link_hash_entry *) h;
7793 bed->elf_backend_hide_symbol (info, eh, TRUE);
7794 eh->def_dynamic = 0;
7795 eh->ref_dynamic = 0;
7796 eh->dynamic_def = 0;
7797 }
7798 }
7799
7800 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7801 caller. */
7802
7803 bfd_boolean
7804 _bfd_elf_link_hash_table_init
7805 (struct elf_link_hash_table *table,
7806 bfd *abfd,
7807 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7808 struct bfd_hash_table *,
7809 const char *),
7810 unsigned int entsize,
7811 enum elf_target_id target_id)
7812 {
7813 bfd_boolean ret;
7814 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7815
7816 table->init_got_refcount.refcount = can_refcount - 1;
7817 table->init_plt_refcount.refcount = can_refcount - 1;
7818 table->init_got_offset.offset = -(bfd_vma) 1;
7819 table->init_plt_offset.offset = -(bfd_vma) 1;
7820 /* The first dynamic symbol is a dummy. */
7821 table->dynsymcount = 1;
7822
7823 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7824
7825 table->root.type = bfd_link_elf_hash_table;
7826 table->hash_table_id = target_id;
7827 table->target_os = get_elf_backend_data (abfd)->target_os;
7828
7829 return ret;
7830 }
7831
7832 /* Create an ELF linker hash table. */
7833
7834 struct bfd_link_hash_table *
7835 _bfd_elf_link_hash_table_create (bfd *abfd)
7836 {
7837 struct elf_link_hash_table *ret;
7838 size_t amt = sizeof (struct elf_link_hash_table);
7839
7840 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7841 if (ret == NULL)
7842 return NULL;
7843
7844 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7845 sizeof (struct elf_link_hash_entry),
7846 GENERIC_ELF_DATA))
7847 {
7848 free (ret);
7849 return NULL;
7850 }
7851 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7852
7853 return &ret->root;
7854 }
7855
7856 /* Destroy an ELF linker hash table. */
7857
7858 void
7859 _bfd_elf_link_hash_table_free (bfd *obfd)
7860 {
7861 struct elf_link_hash_table *htab;
7862
7863 htab = (struct elf_link_hash_table *) obfd->link.hash;
7864 if (htab->dynstr != NULL)
7865 _bfd_elf_strtab_free (htab->dynstr);
7866 _bfd_merge_sections_free (htab->merge_info);
7867 _bfd_generic_link_hash_table_free (obfd);
7868 }
7869
7870 /* This is a hook for the ELF emulation code in the generic linker to
7871 tell the backend linker what file name to use for the DT_NEEDED
7872 entry for a dynamic object. */
7873
7874 void
7875 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7876 {
7877 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7878 && bfd_get_format (abfd) == bfd_object)
7879 elf_dt_name (abfd) = name;
7880 }
7881
7882 int
7883 bfd_elf_get_dyn_lib_class (bfd *abfd)
7884 {
7885 int lib_class;
7886 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7887 && bfd_get_format (abfd) == bfd_object)
7888 lib_class = elf_dyn_lib_class (abfd);
7889 else
7890 lib_class = 0;
7891 return lib_class;
7892 }
7893
7894 void
7895 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7896 {
7897 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7898 && bfd_get_format (abfd) == bfd_object)
7899 elf_dyn_lib_class (abfd) = lib_class;
7900 }
7901
7902 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7903 the linker ELF emulation code. */
7904
7905 struct bfd_link_needed_list *
7906 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7907 struct bfd_link_info *info)
7908 {
7909 if (! is_elf_hash_table (info->hash))
7910 return NULL;
7911 return elf_hash_table (info)->needed;
7912 }
7913
7914 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7915 hook for the linker ELF emulation code. */
7916
7917 struct bfd_link_needed_list *
7918 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7919 struct bfd_link_info *info)
7920 {
7921 if (! is_elf_hash_table (info->hash))
7922 return NULL;
7923 return elf_hash_table (info)->runpath;
7924 }
7925
7926 /* Get the name actually used for a dynamic object for a link. This
7927 is the SONAME entry if there is one. Otherwise, it is the string
7928 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7929
7930 const char *
7931 bfd_elf_get_dt_soname (bfd *abfd)
7932 {
7933 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7934 && bfd_get_format (abfd) == bfd_object)
7935 return elf_dt_name (abfd);
7936 return NULL;
7937 }
7938
7939 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7940 the ELF linker emulation code. */
7941
7942 bfd_boolean
7943 bfd_elf_get_bfd_needed_list (bfd *abfd,
7944 struct bfd_link_needed_list **pneeded)
7945 {
7946 asection *s;
7947 bfd_byte *dynbuf = NULL;
7948 unsigned int elfsec;
7949 unsigned long shlink;
7950 bfd_byte *extdyn, *extdynend;
7951 size_t extdynsize;
7952 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7953
7954 *pneeded = NULL;
7955
7956 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7957 || bfd_get_format (abfd) != bfd_object)
7958 return TRUE;
7959
7960 s = bfd_get_section_by_name (abfd, ".dynamic");
7961 if (s == NULL || s->size == 0)
7962 return TRUE;
7963
7964 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7965 goto error_return;
7966
7967 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7968 if (elfsec == SHN_BAD)
7969 goto error_return;
7970
7971 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7972
7973 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7974 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7975
7976 extdyn = dynbuf;
7977 extdynend = extdyn + s->size;
7978 for (; extdyn < extdynend; extdyn += extdynsize)
7979 {
7980 Elf_Internal_Dyn dyn;
7981
7982 (*swap_dyn_in) (abfd, extdyn, &dyn);
7983
7984 if (dyn.d_tag == DT_NULL)
7985 break;
7986
7987 if (dyn.d_tag == DT_NEEDED)
7988 {
7989 const char *string;
7990 struct bfd_link_needed_list *l;
7991 unsigned int tagv = dyn.d_un.d_val;
7992 size_t amt;
7993
7994 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7995 if (string == NULL)
7996 goto error_return;
7997
7998 amt = sizeof *l;
7999 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8000 if (l == NULL)
8001 goto error_return;
8002
8003 l->by = abfd;
8004 l->name = string;
8005 l->next = *pneeded;
8006 *pneeded = l;
8007 }
8008 }
8009
8010 free (dynbuf);
8011
8012 return TRUE;
8013
8014 error_return:
8015 free (dynbuf);
8016 return FALSE;
8017 }
8018
8019 struct elf_symbuf_symbol
8020 {
8021 unsigned long st_name; /* Symbol name, index in string tbl */
8022 unsigned char st_info; /* Type and binding attributes */
8023 unsigned char st_other; /* Visibilty, and target specific */
8024 };
8025
8026 struct elf_symbuf_head
8027 {
8028 struct elf_symbuf_symbol *ssym;
8029 size_t count;
8030 unsigned int st_shndx;
8031 };
8032
8033 struct elf_symbol
8034 {
8035 union
8036 {
8037 Elf_Internal_Sym *isym;
8038 struct elf_symbuf_symbol *ssym;
8039 void *p;
8040 } u;
8041 const char *name;
8042 };
8043
8044 /* Sort references to symbols by ascending section number. */
8045
8046 static int
8047 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8048 {
8049 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8050 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8051
8052 if (s1->st_shndx != s2->st_shndx)
8053 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8054 /* Final sort by the address of the sym in the symbuf ensures
8055 a stable sort. */
8056 if (s1 != s2)
8057 return s1 > s2 ? 1 : -1;
8058 return 0;
8059 }
8060
8061 static int
8062 elf_sym_name_compare (const void *arg1, const void *arg2)
8063 {
8064 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8065 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8066 int ret = strcmp (s1->name, s2->name);
8067 if (ret != 0)
8068 return ret;
8069 if (s1->u.p != s2->u.p)
8070 return s1->u.p > s2->u.p ? 1 : -1;
8071 return 0;
8072 }
8073
8074 static struct elf_symbuf_head *
8075 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8076 {
8077 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8078 struct elf_symbuf_symbol *ssym;
8079 struct elf_symbuf_head *ssymbuf, *ssymhead;
8080 size_t i, shndx_count, total_size, amt;
8081
8082 amt = symcount * sizeof (*indbuf);
8083 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8084 if (indbuf == NULL)
8085 return NULL;
8086
8087 for (ind = indbuf, i = 0; i < symcount; i++)
8088 if (isymbuf[i].st_shndx != SHN_UNDEF)
8089 *ind++ = &isymbuf[i];
8090 indbufend = ind;
8091
8092 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8093 elf_sort_elf_symbol);
8094
8095 shndx_count = 0;
8096 if (indbufend > indbuf)
8097 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8098 if (ind[0]->st_shndx != ind[1]->st_shndx)
8099 shndx_count++;
8100
8101 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8102 + (indbufend - indbuf) * sizeof (*ssym));
8103 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8104 if (ssymbuf == NULL)
8105 {
8106 free (indbuf);
8107 return NULL;
8108 }
8109
8110 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8111 ssymbuf->ssym = NULL;
8112 ssymbuf->count = shndx_count;
8113 ssymbuf->st_shndx = 0;
8114 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8115 {
8116 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8117 {
8118 ssymhead++;
8119 ssymhead->ssym = ssym;
8120 ssymhead->count = 0;
8121 ssymhead->st_shndx = (*ind)->st_shndx;
8122 }
8123 ssym->st_name = (*ind)->st_name;
8124 ssym->st_info = (*ind)->st_info;
8125 ssym->st_other = (*ind)->st_other;
8126 ssymhead->count++;
8127 }
8128 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8129 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8130 == total_size));
8131
8132 free (indbuf);
8133 return ssymbuf;
8134 }
8135
8136 /* Check if 2 sections define the same set of local and global
8137 symbols. */
8138
8139 static bfd_boolean
8140 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8141 struct bfd_link_info *info)
8142 {
8143 bfd *bfd1, *bfd2;
8144 const struct elf_backend_data *bed1, *bed2;
8145 Elf_Internal_Shdr *hdr1, *hdr2;
8146 size_t symcount1, symcount2;
8147 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8148 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8149 Elf_Internal_Sym *isym, *isymend;
8150 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8151 size_t count1, count2, i;
8152 unsigned int shndx1, shndx2;
8153 bfd_boolean result;
8154
8155 bfd1 = sec1->owner;
8156 bfd2 = sec2->owner;
8157
8158 /* Both sections have to be in ELF. */
8159 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8160 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8161 return FALSE;
8162
8163 if (elf_section_type (sec1) != elf_section_type (sec2))
8164 return FALSE;
8165
8166 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8167 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8168 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8169 return FALSE;
8170
8171 bed1 = get_elf_backend_data (bfd1);
8172 bed2 = get_elf_backend_data (bfd2);
8173 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8174 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8175 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8176 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8177
8178 if (symcount1 == 0 || symcount2 == 0)
8179 return FALSE;
8180
8181 result = FALSE;
8182 isymbuf1 = NULL;
8183 isymbuf2 = NULL;
8184 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8185 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8186
8187 if (ssymbuf1 == NULL)
8188 {
8189 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8190 NULL, NULL, NULL);
8191 if (isymbuf1 == NULL)
8192 goto done;
8193
8194 if (info != NULL && !info->reduce_memory_overheads)
8195 {
8196 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8197 elf_tdata (bfd1)->symbuf = ssymbuf1;
8198 }
8199 }
8200
8201 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8202 {
8203 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8204 NULL, NULL, NULL);
8205 if (isymbuf2 == NULL)
8206 goto done;
8207
8208 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8209 {
8210 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8211 elf_tdata (bfd2)->symbuf = ssymbuf2;
8212 }
8213 }
8214
8215 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8216 {
8217 /* Optimized faster version. */
8218 size_t lo, hi, mid;
8219 struct elf_symbol *symp;
8220 struct elf_symbuf_symbol *ssym, *ssymend;
8221
8222 lo = 0;
8223 hi = ssymbuf1->count;
8224 ssymbuf1++;
8225 count1 = 0;
8226 while (lo < hi)
8227 {
8228 mid = (lo + hi) / 2;
8229 if (shndx1 < ssymbuf1[mid].st_shndx)
8230 hi = mid;
8231 else if (shndx1 > ssymbuf1[mid].st_shndx)
8232 lo = mid + 1;
8233 else
8234 {
8235 count1 = ssymbuf1[mid].count;
8236 ssymbuf1 += mid;
8237 break;
8238 }
8239 }
8240
8241 lo = 0;
8242 hi = ssymbuf2->count;
8243 ssymbuf2++;
8244 count2 = 0;
8245 while (lo < hi)
8246 {
8247 mid = (lo + hi) / 2;
8248 if (shndx2 < ssymbuf2[mid].st_shndx)
8249 hi = mid;
8250 else if (shndx2 > ssymbuf2[mid].st_shndx)
8251 lo = mid + 1;
8252 else
8253 {
8254 count2 = ssymbuf2[mid].count;
8255 ssymbuf2 += mid;
8256 break;
8257 }
8258 }
8259
8260 if (count1 == 0 || count2 == 0 || count1 != count2)
8261 goto done;
8262
8263 symtable1
8264 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8265 symtable2
8266 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8267 if (symtable1 == NULL || symtable2 == NULL)
8268 goto done;
8269
8270 symp = symtable1;
8271 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8272 ssym < ssymend; ssym++, symp++)
8273 {
8274 symp->u.ssym = ssym;
8275 symp->name = bfd_elf_string_from_elf_section (bfd1,
8276 hdr1->sh_link,
8277 ssym->st_name);
8278 }
8279
8280 symp = symtable2;
8281 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8282 ssym < ssymend; ssym++, symp++)
8283 {
8284 symp->u.ssym = ssym;
8285 symp->name = bfd_elf_string_from_elf_section (bfd2,
8286 hdr2->sh_link,
8287 ssym->st_name);
8288 }
8289
8290 /* Sort symbol by name. */
8291 qsort (symtable1, count1, sizeof (struct elf_symbol),
8292 elf_sym_name_compare);
8293 qsort (symtable2, count1, sizeof (struct elf_symbol),
8294 elf_sym_name_compare);
8295
8296 for (i = 0; i < count1; i++)
8297 /* Two symbols must have the same binding, type and name. */
8298 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8299 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8300 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8301 goto done;
8302
8303 result = TRUE;
8304 goto done;
8305 }
8306
8307 symtable1 = (struct elf_symbol *)
8308 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8309 symtable2 = (struct elf_symbol *)
8310 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8311 if (symtable1 == NULL || symtable2 == NULL)
8312 goto done;
8313
8314 /* Count definitions in the section. */
8315 count1 = 0;
8316 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8317 if (isym->st_shndx == shndx1)
8318 symtable1[count1++].u.isym = isym;
8319
8320 count2 = 0;
8321 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8322 if (isym->st_shndx == shndx2)
8323 symtable2[count2++].u.isym = isym;
8324
8325 if (count1 == 0 || count2 == 0 || count1 != count2)
8326 goto done;
8327
8328 for (i = 0; i < count1; i++)
8329 symtable1[i].name
8330 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8331 symtable1[i].u.isym->st_name);
8332
8333 for (i = 0; i < count2; i++)
8334 symtable2[i].name
8335 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8336 symtable2[i].u.isym->st_name);
8337
8338 /* Sort symbol by name. */
8339 qsort (symtable1, count1, sizeof (struct elf_symbol),
8340 elf_sym_name_compare);
8341 qsort (symtable2, count1, sizeof (struct elf_symbol),
8342 elf_sym_name_compare);
8343
8344 for (i = 0; i < count1; i++)
8345 /* Two symbols must have the same binding, type and name. */
8346 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8347 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8348 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8349 goto done;
8350
8351 result = TRUE;
8352
8353 done:
8354 free (symtable1);
8355 free (symtable2);
8356 free (isymbuf1);
8357 free (isymbuf2);
8358
8359 return result;
8360 }
8361
8362 /* Return TRUE if 2 section types are compatible. */
8363
8364 bfd_boolean
8365 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8366 bfd *bbfd, const asection *bsec)
8367 {
8368 if (asec == NULL
8369 || bsec == NULL
8370 || abfd->xvec->flavour != bfd_target_elf_flavour
8371 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8372 return TRUE;
8373
8374 return elf_section_type (asec) == elf_section_type (bsec);
8375 }
8376 \f
8377 /* Final phase of ELF linker. */
8378
8379 /* A structure we use to avoid passing large numbers of arguments. */
8380
8381 struct elf_final_link_info
8382 {
8383 /* General link information. */
8384 struct bfd_link_info *info;
8385 /* Output BFD. */
8386 bfd *output_bfd;
8387 /* Symbol string table. */
8388 struct elf_strtab_hash *symstrtab;
8389 /* .hash section. */
8390 asection *hash_sec;
8391 /* symbol version section (.gnu.version). */
8392 asection *symver_sec;
8393 /* Buffer large enough to hold contents of any section. */
8394 bfd_byte *contents;
8395 /* Buffer large enough to hold external relocs of any section. */
8396 void *external_relocs;
8397 /* Buffer large enough to hold internal relocs of any section. */
8398 Elf_Internal_Rela *internal_relocs;
8399 /* Buffer large enough to hold external local symbols of any input
8400 BFD. */
8401 bfd_byte *external_syms;
8402 /* And a buffer for symbol section indices. */
8403 Elf_External_Sym_Shndx *locsym_shndx;
8404 /* Buffer large enough to hold internal local symbols of any input
8405 BFD. */
8406 Elf_Internal_Sym *internal_syms;
8407 /* Array large enough to hold a symbol index for each local symbol
8408 of any input BFD. */
8409 long *indices;
8410 /* Array large enough to hold a section pointer for each local
8411 symbol of any input BFD. */
8412 asection **sections;
8413 /* Buffer for SHT_SYMTAB_SHNDX section. */
8414 Elf_External_Sym_Shndx *symshndxbuf;
8415 /* Number of STT_FILE syms seen. */
8416 size_t filesym_count;
8417 /* Local symbol hash table. */
8418 struct bfd_hash_table local_hash_table;
8419 };
8420
8421 struct local_hash_entry
8422 {
8423 /* Base hash table entry structure. */
8424 struct bfd_hash_entry root;
8425 /* Size of the local symbol name. */
8426 size_t size;
8427 /* Number of the duplicated local symbol names. */
8428 long count;
8429 };
8430
8431 /* Create an entry in the local symbol hash table. */
8432
8433 static struct bfd_hash_entry *
8434 local_hash_newfunc (struct bfd_hash_entry *entry,
8435 struct bfd_hash_table *table,
8436 const char *string)
8437 {
8438
8439 /* Allocate the structure if it has not already been allocated by a
8440 subclass. */
8441 if (entry == NULL)
8442 {
8443 entry = bfd_hash_allocate (table,
8444 sizeof (struct local_hash_entry));
8445 if (entry == NULL)
8446 return entry;
8447 }
8448
8449 /* Call the allocation method of the superclass. */
8450 entry = bfd_hash_newfunc (entry, table, string);
8451 if (entry != NULL)
8452 {
8453 ((struct local_hash_entry *) entry)->count = 0;
8454 ((struct local_hash_entry *) entry)->size = 0;
8455 }
8456
8457 return entry;
8458 }
8459
8460 /* This struct is used to pass information to elf_link_output_extsym. */
8461
8462 struct elf_outext_info
8463 {
8464 bfd_boolean failed;
8465 bfd_boolean localsyms;
8466 bfd_boolean file_sym_done;
8467 struct elf_final_link_info *flinfo;
8468 };
8469
8470
8471 /* Support for evaluating a complex relocation.
8472
8473 Complex relocations are generalized, self-describing relocations. The
8474 implementation of them consists of two parts: complex symbols, and the
8475 relocations themselves.
8476
8477 The relocations use a reserved elf-wide relocation type code (R_RELC
8478 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8479 information (start bit, end bit, word width, etc) into the addend. This
8480 information is extracted from CGEN-generated operand tables within gas.
8481
8482 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8483 internal) representing prefix-notation expressions, including but not
8484 limited to those sorts of expressions normally encoded as addends in the
8485 addend field. The symbol mangling format is:
8486
8487 <node> := <literal>
8488 | <unary-operator> ':' <node>
8489 | <binary-operator> ':' <node> ':' <node>
8490 ;
8491
8492 <literal> := 's' <digits=N> ':' <N character symbol name>
8493 | 'S' <digits=N> ':' <N character section name>
8494 | '#' <hexdigits>
8495 ;
8496
8497 <binary-operator> := as in C
8498 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8499
8500 static void
8501 set_symbol_value (bfd *bfd_with_globals,
8502 Elf_Internal_Sym *isymbuf,
8503 size_t locsymcount,
8504 size_t symidx,
8505 bfd_vma val)
8506 {
8507 struct elf_link_hash_entry **sym_hashes;
8508 struct elf_link_hash_entry *h;
8509 size_t extsymoff = locsymcount;
8510
8511 if (symidx < locsymcount)
8512 {
8513 Elf_Internal_Sym *sym;
8514
8515 sym = isymbuf + symidx;
8516 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8517 {
8518 /* It is a local symbol: move it to the
8519 "absolute" section and give it a value. */
8520 sym->st_shndx = SHN_ABS;
8521 sym->st_value = val;
8522 return;
8523 }
8524 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8525 extsymoff = 0;
8526 }
8527
8528 /* It is a global symbol: set its link type
8529 to "defined" and give it a value. */
8530
8531 sym_hashes = elf_sym_hashes (bfd_with_globals);
8532 h = sym_hashes [symidx - extsymoff];
8533 while (h->root.type == bfd_link_hash_indirect
8534 || h->root.type == bfd_link_hash_warning)
8535 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8536 h->root.type = bfd_link_hash_defined;
8537 h->root.u.def.value = val;
8538 h->root.u.def.section = bfd_abs_section_ptr;
8539 }
8540
8541 static bfd_boolean
8542 resolve_symbol (const char *name,
8543 bfd *input_bfd,
8544 struct elf_final_link_info *flinfo,
8545 bfd_vma *result,
8546 Elf_Internal_Sym *isymbuf,
8547 size_t locsymcount)
8548 {
8549 Elf_Internal_Sym *sym;
8550 struct bfd_link_hash_entry *global_entry;
8551 const char *candidate = NULL;
8552 Elf_Internal_Shdr *symtab_hdr;
8553 size_t i;
8554
8555 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8556
8557 for (i = 0; i < locsymcount; ++ i)
8558 {
8559 sym = isymbuf + i;
8560
8561 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8562 continue;
8563
8564 candidate = bfd_elf_string_from_elf_section (input_bfd,
8565 symtab_hdr->sh_link,
8566 sym->st_name);
8567 #ifdef DEBUG
8568 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8569 name, candidate, (unsigned long) sym->st_value);
8570 #endif
8571 if (candidate && strcmp (candidate, name) == 0)
8572 {
8573 asection *sec = flinfo->sections [i];
8574
8575 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8576 *result += sec->output_offset + sec->output_section->vma;
8577 #ifdef DEBUG
8578 printf ("Found symbol with value %8.8lx\n",
8579 (unsigned long) *result);
8580 #endif
8581 return TRUE;
8582 }
8583 }
8584
8585 /* Hmm, haven't found it yet. perhaps it is a global. */
8586 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8587 FALSE, FALSE, TRUE);
8588 if (!global_entry)
8589 return FALSE;
8590
8591 if (global_entry->type == bfd_link_hash_defined
8592 || global_entry->type == bfd_link_hash_defweak)
8593 {
8594 *result = (global_entry->u.def.value
8595 + global_entry->u.def.section->output_section->vma
8596 + global_entry->u.def.section->output_offset);
8597 #ifdef DEBUG
8598 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8599 global_entry->root.string, (unsigned long) *result);
8600 #endif
8601 return TRUE;
8602 }
8603
8604 return FALSE;
8605 }
8606
8607 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8608 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8609 names like "foo.end" which is the end address of section "foo". */
8610
8611 static bfd_boolean
8612 resolve_section (const char *name,
8613 asection *sections,
8614 bfd_vma *result,
8615 bfd * abfd)
8616 {
8617 asection *curr;
8618 unsigned int len;
8619
8620 for (curr = sections; curr; curr = curr->next)
8621 if (strcmp (curr->name, name) == 0)
8622 {
8623 *result = curr->vma;
8624 return TRUE;
8625 }
8626
8627 /* Hmm. still haven't found it. try pseudo-section names. */
8628 /* FIXME: This could be coded more efficiently... */
8629 for (curr = sections; curr; curr = curr->next)
8630 {
8631 len = strlen (curr->name);
8632 if (len > strlen (name))
8633 continue;
8634
8635 if (strncmp (curr->name, name, len) == 0)
8636 {
8637 if (strncmp (".end", name + len, 4) == 0)
8638 {
8639 *result = (curr->vma
8640 + curr->size / bfd_octets_per_byte (abfd, curr));
8641 return TRUE;
8642 }
8643
8644 /* Insert more pseudo-section names here, if you like. */
8645 }
8646 }
8647
8648 return FALSE;
8649 }
8650
8651 static void
8652 undefined_reference (const char *reftype, const char *name)
8653 {
8654 /* xgettext:c-format */
8655 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8656 reftype, name);
8657 bfd_set_error (bfd_error_bad_value);
8658 }
8659
8660 static bfd_boolean
8661 eval_symbol (bfd_vma *result,
8662 const char **symp,
8663 bfd *input_bfd,
8664 struct elf_final_link_info *flinfo,
8665 bfd_vma dot,
8666 Elf_Internal_Sym *isymbuf,
8667 size_t locsymcount,
8668 int signed_p)
8669 {
8670 size_t len;
8671 size_t symlen;
8672 bfd_vma a;
8673 bfd_vma b;
8674 char symbuf[4096];
8675 const char *sym = *symp;
8676 const char *symend;
8677 bfd_boolean symbol_is_section = FALSE;
8678
8679 len = strlen (sym);
8680 symend = sym + len;
8681
8682 if (len < 1 || len > sizeof (symbuf))
8683 {
8684 bfd_set_error (bfd_error_invalid_operation);
8685 return FALSE;
8686 }
8687
8688 switch (* sym)
8689 {
8690 case '.':
8691 *result = dot;
8692 *symp = sym + 1;
8693 return TRUE;
8694
8695 case '#':
8696 ++sym;
8697 *result = strtoul (sym, (char **) symp, 16);
8698 return TRUE;
8699
8700 case 'S':
8701 symbol_is_section = TRUE;
8702 /* Fall through. */
8703 case 's':
8704 ++sym;
8705 symlen = strtol (sym, (char **) symp, 10);
8706 sym = *symp + 1; /* Skip the trailing ':'. */
8707
8708 if (symend < sym || symlen + 1 > sizeof (symbuf))
8709 {
8710 bfd_set_error (bfd_error_invalid_operation);
8711 return FALSE;
8712 }
8713
8714 memcpy (symbuf, sym, symlen);
8715 symbuf[symlen] = '\0';
8716 *symp = sym + symlen;
8717
8718 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8719 the symbol as a section, or vice-versa. so we're pretty liberal in our
8720 interpretation here; section means "try section first", not "must be a
8721 section", and likewise with symbol. */
8722
8723 if (symbol_is_section)
8724 {
8725 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8726 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8727 isymbuf, locsymcount))
8728 {
8729 undefined_reference ("section", symbuf);
8730 return FALSE;
8731 }
8732 }
8733 else
8734 {
8735 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8736 isymbuf, locsymcount)
8737 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8738 result, input_bfd))
8739 {
8740 undefined_reference ("symbol", symbuf);
8741 return FALSE;
8742 }
8743 }
8744
8745 return TRUE;
8746
8747 /* All that remains are operators. */
8748
8749 #define UNARY_OP(op) \
8750 if (strncmp (sym, #op, strlen (#op)) == 0) \
8751 { \
8752 sym += strlen (#op); \
8753 if (*sym == ':') \
8754 ++sym; \
8755 *symp = sym; \
8756 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8757 isymbuf, locsymcount, signed_p)) \
8758 return FALSE; \
8759 if (signed_p) \
8760 *result = op ((bfd_signed_vma) a); \
8761 else \
8762 *result = op a; \
8763 return TRUE; \
8764 }
8765
8766 #define BINARY_OP_HEAD(op) \
8767 if (strncmp (sym, #op, strlen (#op)) == 0) \
8768 { \
8769 sym += strlen (#op); \
8770 if (*sym == ':') \
8771 ++sym; \
8772 *symp = sym; \
8773 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8774 isymbuf, locsymcount, signed_p)) \
8775 return FALSE; \
8776 ++*symp; \
8777 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8778 isymbuf, locsymcount, signed_p)) \
8779 return FALSE;
8780 #define BINARY_OP_TAIL(op) \
8781 if (signed_p) \
8782 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8783 else \
8784 *result = a op b; \
8785 return TRUE; \
8786 }
8787 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8788
8789 default:
8790 UNARY_OP (0-);
8791 BINARY_OP_HEAD (<<);
8792 if (b >= sizeof (a) * CHAR_BIT)
8793 {
8794 *result = 0;
8795 return TRUE;
8796 }
8797 signed_p = 0;
8798 BINARY_OP_TAIL (<<);
8799 BINARY_OP_HEAD (>>);
8800 if (b >= sizeof (a) * CHAR_BIT)
8801 {
8802 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8803 return TRUE;
8804 }
8805 BINARY_OP_TAIL (>>);
8806 BINARY_OP (==);
8807 BINARY_OP (!=);
8808 BINARY_OP (<=);
8809 BINARY_OP (>=);
8810 BINARY_OP (&&);
8811 BINARY_OP (||);
8812 UNARY_OP (~);
8813 UNARY_OP (!);
8814 BINARY_OP (*);
8815 BINARY_OP_HEAD (/);
8816 if (b == 0)
8817 {
8818 _bfd_error_handler (_("division by zero"));
8819 bfd_set_error (bfd_error_bad_value);
8820 return FALSE;
8821 }
8822 BINARY_OP_TAIL (/);
8823 BINARY_OP_HEAD (%);
8824 if (b == 0)
8825 {
8826 _bfd_error_handler (_("division by zero"));
8827 bfd_set_error (bfd_error_bad_value);
8828 return FALSE;
8829 }
8830 BINARY_OP_TAIL (%);
8831 BINARY_OP (^);
8832 BINARY_OP (|);
8833 BINARY_OP (&);
8834 BINARY_OP (+);
8835 BINARY_OP (-);
8836 BINARY_OP (<);
8837 BINARY_OP (>);
8838 #undef UNARY_OP
8839 #undef BINARY_OP
8840 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8841 bfd_set_error (bfd_error_invalid_operation);
8842 return FALSE;
8843 }
8844 }
8845
8846 static void
8847 put_value (bfd_vma size,
8848 unsigned long chunksz,
8849 bfd *input_bfd,
8850 bfd_vma x,
8851 bfd_byte *location)
8852 {
8853 location += (size - chunksz);
8854
8855 for (; size; size -= chunksz, location -= chunksz)
8856 {
8857 switch (chunksz)
8858 {
8859 case 1:
8860 bfd_put_8 (input_bfd, x, location);
8861 x >>= 8;
8862 break;
8863 case 2:
8864 bfd_put_16 (input_bfd, x, location);
8865 x >>= 16;
8866 break;
8867 case 4:
8868 bfd_put_32 (input_bfd, x, location);
8869 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8870 x >>= 16;
8871 x >>= 16;
8872 break;
8873 #ifdef BFD64
8874 case 8:
8875 bfd_put_64 (input_bfd, x, location);
8876 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8877 x >>= 32;
8878 x >>= 32;
8879 break;
8880 #endif
8881 default:
8882 abort ();
8883 break;
8884 }
8885 }
8886 }
8887
8888 static bfd_vma
8889 get_value (bfd_vma size,
8890 unsigned long chunksz,
8891 bfd *input_bfd,
8892 bfd_byte *location)
8893 {
8894 int shift;
8895 bfd_vma x = 0;
8896
8897 /* Sanity checks. */
8898 BFD_ASSERT (chunksz <= sizeof (x)
8899 && size >= chunksz
8900 && chunksz != 0
8901 && (size % chunksz) == 0
8902 && input_bfd != NULL
8903 && location != NULL);
8904
8905 if (chunksz == sizeof (x))
8906 {
8907 BFD_ASSERT (size == chunksz);
8908
8909 /* Make sure that we do not perform an undefined shift operation.
8910 We know that size == chunksz so there will only be one iteration
8911 of the loop below. */
8912 shift = 0;
8913 }
8914 else
8915 shift = 8 * chunksz;
8916
8917 for (; size; size -= chunksz, location += chunksz)
8918 {
8919 switch (chunksz)
8920 {
8921 case 1:
8922 x = (x << shift) | bfd_get_8 (input_bfd, location);
8923 break;
8924 case 2:
8925 x = (x << shift) | bfd_get_16 (input_bfd, location);
8926 break;
8927 case 4:
8928 x = (x << shift) | bfd_get_32 (input_bfd, location);
8929 break;
8930 #ifdef BFD64
8931 case 8:
8932 x = (x << shift) | bfd_get_64 (input_bfd, location);
8933 break;
8934 #endif
8935 default:
8936 abort ();
8937 }
8938 }
8939 return x;
8940 }
8941
8942 static void
8943 decode_complex_addend (unsigned long *start, /* in bits */
8944 unsigned long *oplen, /* in bits */
8945 unsigned long *len, /* in bits */
8946 unsigned long *wordsz, /* in bytes */
8947 unsigned long *chunksz, /* in bytes */
8948 unsigned long *lsb0_p,
8949 unsigned long *signed_p,
8950 unsigned long *trunc_p,
8951 unsigned long encoded)
8952 {
8953 * start = encoded & 0x3F;
8954 * len = (encoded >> 6) & 0x3F;
8955 * oplen = (encoded >> 12) & 0x3F;
8956 * wordsz = (encoded >> 18) & 0xF;
8957 * chunksz = (encoded >> 22) & 0xF;
8958 * lsb0_p = (encoded >> 27) & 1;
8959 * signed_p = (encoded >> 28) & 1;
8960 * trunc_p = (encoded >> 29) & 1;
8961 }
8962
8963 bfd_reloc_status_type
8964 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8965 asection *input_section,
8966 bfd_byte *contents,
8967 Elf_Internal_Rela *rel,
8968 bfd_vma relocation)
8969 {
8970 bfd_vma shift, x, mask;
8971 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8972 bfd_reloc_status_type r;
8973 bfd_size_type octets;
8974
8975 /* Perform this reloc, since it is complex.
8976 (this is not to say that it necessarily refers to a complex
8977 symbol; merely that it is a self-describing CGEN based reloc.
8978 i.e. the addend has the complete reloc information (bit start, end,
8979 word size, etc) encoded within it.). */
8980
8981 decode_complex_addend (&start, &oplen, &len, &wordsz,
8982 &chunksz, &lsb0_p, &signed_p,
8983 &trunc_p, rel->r_addend);
8984
8985 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8986
8987 if (lsb0_p)
8988 shift = (start + 1) - len;
8989 else
8990 shift = (8 * wordsz) - (start + len);
8991
8992 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8993 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8994
8995 #ifdef DEBUG
8996 printf ("Doing complex reloc: "
8997 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8998 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8999 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9000 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9001 oplen, (unsigned long) x, (unsigned long) mask,
9002 (unsigned long) relocation);
9003 #endif
9004
9005 r = bfd_reloc_ok;
9006 if (! trunc_p)
9007 /* Now do an overflow check. */
9008 r = bfd_check_overflow ((signed_p
9009 ? complain_overflow_signed
9010 : complain_overflow_unsigned),
9011 len, 0, (8 * wordsz),
9012 relocation);
9013
9014 /* Do the deed. */
9015 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9016
9017 #ifdef DEBUG
9018 printf (" relocation: %8.8lx\n"
9019 " shifted mask: %8.8lx\n"
9020 " shifted/masked reloc: %8.8lx\n"
9021 " result: %8.8lx\n",
9022 (unsigned long) relocation, (unsigned long) (mask << shift),
9023 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9024 #endif
9025 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9026 return r;
9027 }
9028
9029 /* Functions to read r_offset from external (target order) reloc
9030 entry. Faster than bfd_getl32 et al, because we let the compiler
9031 know the value is aligned. */
9032
9033 static bfd_vma
9034 ext32l_r_offset (const void *p)
9035 {
9036 union aligned32
9037 {
9038 uint32_t v;
9039 unsigned char c[4];
9040 };
9041 const union aligned32 *a
9042 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9043
9044 uint32_t aval = ( (uint32_t) a->c[0]
9045 | (uint32_t) a->c[1] << 8
9046 | (uint32_t) a->c[2] << 16
9047 | (uint32_t) a->c[3] << 24);
9048 return aval;
9049 }
9050
9051 static bfd_vma
9052 ext32b_r_offset (const void *p)
9053 {
9054 union aligned32
9055 {
9056 uint32_t v;
9057 unsigned char c[4];
9058 };
9059 const union aligned32 *a
9060 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9061
9062 uint32_t aval = ( (uint32_t) a->c[0] << 24
9063 | (uint32_t) a->c[1] << 16
9064 | (uint32_t) a->c[2] << 8
9065 | (uint32_t) a->c[3]);
9066 return aval;
9067 }
9068
9069 #ifdef BFD_HOST_64_BIT
9070 static bfd_vma
9071 ext64l_r_offset (const void *p)
9072 {
9073 union aligned64
9074 {
9075 uint64_t v;
9076 unsigned char c[8];
9077 };
9078 const union aligned64 *a
9079 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9080
9081 uint64_t aval = ( (uint64_t) a->c[0]
9082 | (uint64_t) a->c[1] << 8
9083 | (uint64_t) a->c[2] << 16
9084 | (uint64_t) a->c[3] << 24
9085 | (uint64_t) a->c[4] << 32
9086 | (uint64_t) a->c[5] << 40
9087 | (uint64_t) a->c[6] << 48
9088 | (uint64_t) a->c[7] << 56);
9089 return aval;
9090 }
9091
9092 static bfd_vma
9093 ext64b_r_offset (const void *p)
9094 {
9095 union aligned64
9096 {
9097 uint64_t v;
9098 unsigned char c[8];
9099 };
9100 const union aligned64 *a
9101 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9102
9103 uint64_t aval = ( (uint64_t) a->c[0] << 56
9104 | (uint64_t) a->c[1] << 48
9105 | (uint64_t) a->c[2] << 40
9106 | (uint64_t) a->c[3] << 32
9107 | (uint64_t) a->c[4] << 24
9108 | (uint64_t) a->c[5] << 16
9109 | (uint64_t) a->c[6] << 8
9110 | (uint64_t) a->c[7]);
9111 return aval;
9112 }
9113 #endif
9114
9115 /* When performing a relocatable link, the input relocations are
9116 preserved. But, if they reference global symbols, the indices
9117 referenced must be updated. Update all the relocations found in
9118 RELDATA. */
9119
9120 static bfd_boolean
9121 elf_link_adjust_relocs (bfd *abfd,
9122 asection *sec,
9123 struct bfd_elf_section_reloc_data *reldata,
9124 bfd_boolean sort,
9125 struct bfd_link_info *info)
9126 {
9127 unsigned int i;
9128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9129 bfd_byte *erela;
9130 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9131 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9132 bfd_vma r_type_mask;
9133 int r_sym_shift;
9134 unsigned int count = reldata->count;
9135 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9136
9137 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9138 {
9139 swap_in = bed->s->swap_reloc_in;
9140 swap_out = bed->s->swap_reloc_out;
9141 }
9142 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9143 {
9144 swap_in = bed->s->swap_reloca_in;
9145 swap_out = bed->s->swap_reloca_out;
9146 }
9147 else
9148 abort ();
9149
9150 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9151 abort ();
9152
9153 if (bed->s->arch_size == 32)
9154 {
9155 r_type_mask = 0xff;
9156 r_sym_shift = 8;
9157 }
9158 else
9159 {
9160 r_type_mask = 0xffffffff;
9161 r_sym_shift = 32;
9162 }
9163
9164 erela = reldata->hdr->contents;
9165 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9166 {
9167 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9168 unsigned int j;
9169
9170 if (*rel_hash == NULL)
9171 continue;
9172
9173 if ((*rel_hash)->indx == -2
9174 && info->gc_sections
9175 && ! info->gc_keep_exported)
9176 {
9177 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9178 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9179 abfd, sec,
9180 (*rel_hash)->root.root.string);
9181 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9182 abfd, sec);
9183 bfd_set_error (bfd_error_invalid_operation);
9184 return FALSE;
9185 }
9186 BFD_ASSERT ((*rel_hash)->indx >= 0);
9187
9188 (*swap_in) (abfd, erela, irela);
9189 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9190 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9191 | (irela[j].r_info & r_type_mask));
9192 (*swap_out) (abfd, irela, erela);
9193 }
9194
9195 if (bed->elf_backend_update_relocs)
9196 (*bed->elf_backend_update_relocs) (sec, reldata);
9197
9198 if (sort && count != 0)
9199 {
9200 bfd_vma (*ext_r_off) (const void *);
9201 bfd_vma r_off;
9202 size_t elt_size;
9203 bfd_byte *base, *end, *p, *loc;
9204 bfd_byte *buf = NULL;
9205
9206 if (bed->s->arch_size == 32)
9207 {
9208 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9209 ext_r_off = ext32l_r_offset;
9210 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9211 ext_r_off = ext32b_r_offset;
9212 else
9213 abort ();
9214 }
9215 else
9216 {
9217 #ifdef BFD_HOST_64_BIT
9218 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9219 ext_r_off = ext64l_r_offset;
9220 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9221 ext_r_off = ext64b_r_offset;
9222 else
9223 #endif
9224 abort ();
9225 }
9226
9227 /* Must use a stable sort here. A modified insertion sort,
9228 since the relocs are mostly sorted already. */
9229 elt_size = reldata->hdr->sh_entsize;
9230 base = reldata->hdr->contents;
9231 end = base + count * elt_size;
9232 if (elt_size > sizeof (Elf64_External_Rela))
9233 abort ();
9234
9235 /* Ensure the first element is lowest. This acts as a sentinel,
9236 speeding the main loop below. */
9237 r_off = (*ext_r_off) (base);
9238 for (p = loc = base; (p += elt_size) < end; )
9239 {
9240 bfd_vma r_off2 = (*ext_r_off) (p);
9241 if (r_off > r_off2)
9242 {
9243 r_off = r_off2;
9244 loc = p;
9245 }
9246 }
9247 if (loc != base)
9248 {
9249 /* Don't just swap *base and *loc as that changes the order
9250 of the original base[0] and base[1] if they happen to
9251 have the same r_offset. */
9252 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9253 memcpy (onebuf, loc, elt_size);
9254 memmove (base + elt_size, base, loc - base);
9255 memcpy (base, onebuf, elt_size);
9256 }
9257
9258 for (p = base + elt_size; (p += elt_size) < end; )
9259 {
9260 /* base to p is sorted, *p is next to insert. */
9261 r_off = (*ext_r_off) (p);
9262 /* Search the sorted region for location to insert. */
9263 loc = p - elt_size;
9264 while (r_off < (*ext_r_off) (loc))
9265 loc -= elt_size;
9266 loc += elt_size;
9267 if (loc != p)
9268 {
9269 /* Chances are there is a run of relocs to insert here,
9270 from one of more input files. Files are not always
9271 linked in order due to the way elf_link_input_bfd is
9272 called. See pr17666. */
9273 size_t sortlen = p - loc;
9274 bfd_vma r_off2 = (*ext_r_off) (loc);
9275 size_t runlen = elt_size;
9276 size_t buf_size = 96 * 1024;
9277 while (p + runlen < end
9278 && (sortlen <= buf_size
9279 || runlen + elt_size <= buf_size)
9280 && r_off2 > (*ext_r_off) (p + runlen))
9281 runlen += elt_size;
9282 if (buf == NULL)
9283 {
9284 buf = bfd_malloc (buf_size);
9285 if (buf == NULL)
9286 return FALSE;
9287 }
9288 if (runlen < sortlen)
9289 {
9290 memcpy (buf, p, runlen);
9291 memmove (loc + runlen, loc, sortlen);
9292 memcpy (loc, buf, runlen);
9293 }
9294 else
9295 {
9296 memcpy (buf, loc, sortlen);
9297 memmove (loc, p, runlen);
9298 memcpy (loc + runlen, buf, sortlen);
9299 }
9300 p += runlen - elt_size;
9301 }
9302 }
9303 /* Hashes are no longer valid. */
9304 free (reldata->hashes);
9305 reldata->hashes = NULL;
9306 free (buf);
9307 }
9308 return TRUE;
9309 }
9310
9311 struct elf_link_sort_rela
9312 {
9313 union {
9314 bfd_vma offset;
9315 bfd_vma sym_mask;
9316 } u;
9317 enum elf_reloc_type_class type;
9318 /* We use this as an array of size int_rels_per_ext_rel. */
9319 Elf_Internal_Rela rela[1];
9320 };
9321
9322 /* qsort stability here and for cmp2 is only an issue if multiple
9323 dynamic relocations are emitted at the same address. But targets
9324 that apply a series of dynamic relocations each operating on the
9325 result of the prior relocation can't use -z combreloc as
9326 implemented anyway. Such schemes tend to be broken by sorting on
9327 symbol index. That leaves dynamic NONE relocs as the only other
9328 case where ld might emit multiple relocs at the same address, and
9329 those are only emitted due to target bugs. */
9330
9331 static int
9332 elf_link_sort_cmp1 (const void *A, const void *B)
9333 {
9334 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9335 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9336 int relativea, relativeb;
9337
9338 relativea = a->type == reloc_class_relative;
9339 relativeb = b->type == reloc_class_relative;
9340
9341 if (relativea < relativeb)
9342 return 1;
9343 if (relativea > relativeb)
9344 return -1;
9345 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9346 return -1;
9347 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9348 return 1;
9349 if (a->rela->r_offset < b->rela->r_offset)
9350 return -1;
9351 if (a->rela->r_offset > b->rela->r_offset)
9352 return 1;
9353 return 0;
9354 }
9355
9356 static int
9357 elf_link_sort_cmp2 (const void *A, const void *B)
9358 {
9359 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9360 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9361
9362 if (a->type < b->type)
9363 return -1;
9364 if (a->type > b->type)
9365 return 1;
9366 if (a->u.offset < b->u.offset)
9367 return -1;
9368 if (a->u.offset > b->u.offset)
9369 return 1;
9370 if (a->rela->r_offset < b->rela->r_offset)
9371 return -1;
9372 if (a->rela->r_offset > b->rela->r_offset)
9373 return 1;
9374 return 0;
9375 }
9376
9377 static size_t
9378 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9379 {
9380 asection *dynamic_relocs;
9381 asection *rela_dyn;
9382 asection *rel_dyn;
9383 bfd_size_type count, size;
9384 size_t i, ret, sort_elt, ext_size;
9385 bfd_byte *sort, *s_non_relative, *p;
9386 struct elf_link_sort_rela *sq;
9387 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9388 int i2e = bed->s->int_rels_per_ext_rel;
9389 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9390 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9391 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9392 struct bfd_link_order *lo;
9393 bfd_vma r_sym_mask;
9394 bfd_boolean use_rela;
9395
9396 /* Find a dynamic reloc section. */
9397 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9398 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9399 if (rela_dyn != NULL && rela_dyn->size > 0
9400 && rel_dyn != NULL && rel_dyn->size > 0)
9401 {
9402 bfd_boolean use_rela_initialised = FALSE;
9403
9404 /* This is just here to stop gcc from complaining.
9405 Its initialization checking code is not perfect. */
9406 use_rela = TRUE;
9407
9408 /* Both sections are present. Examine the sizes
9409 of the indirect sections to help us choose. */
9410 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9411 if (lo->type == bfd_indirect_link_order)
9412 {
9413 asection *o = lo->u.indirect.section;
9414
9415 if ((o->size % bed->s->sizeof_rela) == 0)
9416 {
9417 if ((o->size % bed->s->sizeof_rel) == 0)
9418 /* Section size is divisible by both rel and rela sizes.
9419 It is of no help to us. */
9420 ;
9421 else
9422 {
9423 /* Section size is only divisible by rela. */
9424 if (use_rela_initialised && !use_rela)
9425 {
9426 _bfd_error_handler (_("%pB: unable to sort relocs - "
9427 "they are in more than one size"),
9428 abfd);
9429 bfd_set_error (bfd_error_invalid_operation);
9430 return 0;
9431 }
9432 else
9433 {
9434 use_rela = TRUE;
9435 use_rela_initialised = TRUE;
9436 }
9437 }
9438 }
9439 else if ((o->size % bed->s->sizeof_rel) == 0)
9440 {
9441 /* Section size is only divisible by rel. */
9442 if (use_rela_initialised && use_rela)
9443 {
9444 _bfd_error_handler (_("%pB: unable to sort relocs - "
9445 "they are in more than one size"),
9446 abfd);
9447 bfd_set_error (bfd_error_invalid_operation);
9448 return 0;
9449 }
9450 else
9451 {
9452 use_rela = FALSE;
9453 use_rela_initialised = TRUE;
9454 }
9455 }
9456 else
9457 {
9458 /* The section size is not divisible by either -
9459 something is wrong. */
9460 _bfd_error_handler (_("%pB: unable to sort relocs - "
9461 "they are of an unknown size"), abfd);
9462 bfd_set_error (bfd_error_invalid_operation);
9463 return 0;
9464 }
9465 }
9466
9467 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9468 if (lo->type == bfd_indirect_link_order)
9469 {
9470 asection *o = lo->u.indirect.section;
9471
9472 if ((o->size % bed->s->sizeof_rela) == 0)
9473 {
9474 if ((o->size % bed->s->sizeof_rel) == 0)
9475 /* Section size is divisible by both rel and rela sizes.
9476 It is of no help to us. */
9477 ;
9478 else
9479 {
9480 /* Section size is only divisible by rela. */
9481 if (use_rela_initialised && !use_rela)
9482 {
9483 _bfd_error_handler (_("%pB: unable to sort relocs - "
9484 "they are in more than one size"),
9485 abfd);
9486 bfd_set_error (bfd_error_invalid_operation);
9487 return 0;
9488 }
9489 else
9490 {
9491 use_rela = TRUE;
9492 use_rela_initialised = TRUE;
9493 }
9494 }
9495 }
9496 else if ((o->size % bed->s->sizeof_rel) == 0)
9497 {
9498 /* Section size is only divisible by rel. */
9499 if (use_rela_initialised && use_rela)
9500 {
9501 _bfd_error_handler (_("%pB: unable to sort relocs - "
9502 "they are in more than one size"),
9503 abfd);
9504 bfd_set_error (bfd_error_invalid_operation);
9505 return 0;
9506 }
9507 else
9508 {
9509 use_rela = FALSE;
9510 use_rela_initialised = TRUE;
9511 }
9512 }
9513 else
9514 {
9515 /* The section size is not divisible by either -
9516 something is wrong. */
9517 _bfd_error_handler (_("%pB: unable to sort relocs - "
9518 "they are of an unknown size"), abfd);
9519 bfd_set_error (bfd_error_invalid_operation);
9520 return 0;
9521 }
9522 }
9523
9524 if (! use_rela_initialised)
9525 /* Make a guess. */
9526 use_rela = TRUE;
9527 }
9528 else if (rela_dyn != NULL && rela_dyn->size > 0)
9529 use_rela = TRUE;
9530 else if (rel_dyn != NULL && rel_dyn->size > 0)
9531 use_rela = FALSE;
9532 else
9533 return 0;
9534
9535 if (use_rela)
9536 {
9537 dynamic_relocs = rela_dyn;
9538 ext_size = bed->s->sizeof_rela;
9539 swap_in = bed->s->swap_reloca_in;
9540 swap_out = bed->s->swap_reloca_out;
9541 }
9542 else
9543 {
9544 dynamic_relocs = rel_dyn;
9545 ext_size = bed->s->sizeof_rel;
9546 swap_in = bed->s->swap_reloc_in;
9547 swap_out = bed->s->swap_reloc_out;
9548 }
9549
9550 size = 0;
9551 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9552 if (lo->type == bfd_indirect_link_order)
9553 size += lo->u.indirect.section->size;
9554
9555 if (size != dynamic_relocs->size)
9556 return 0;
9557
9558 sort_elt = (sizeof (struct elf_link_sort_rela)
9559 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9560
9561 count = dynamic_relocs->size / ext_size;
9562 if (count == 0)
9563 return 0;
9564 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9565
9566 if (sort == NULL)
9567 {
9568 (*info->callbacks->warning)
9569 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9570 return 0;
9571 }
9572
9573 if (bed->s->arch_size == 32)
9574 r_sym_mask = ~(bfd_vma) 0xff;
9575 else
9576 r_sym_mask = ~(bfd_vma) 0xffffffff;
9577
9578 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9579 if (lo->type == bfd_indirect_link_order)
9580 {
9581 bfd_byte *erel, *erelend;
9582 asection *o = lo->u.indirect.section;
9583
9584 if (o->contents == NULL && o->size != 0)
9585 {
9586 /* This is a reloc section that is being handled as a normal
9587 section. See bfd_section_from_shdr. We can't combine
9588 relocs in this case. */
9589 free (sort);
9590 return 0;
9591 }
9592 erel = o->contents;
9593 erelend = o->contents + o->size;
9594 p = sort + o->output_offset * opb / ext_size * sort_elt;
9595
9596 while (erel < erelend)
9597 {
9598 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9599
9600 (*swap_in) (abfd, erel, s->rela);
9601 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9602 s->u.sym_mask = r_sym_mask;
9603 p += sort_elt;
9604 erel += ext_size;
9605 }
9606 }
9607
9608 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9609
9610 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9611 {
9612 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9613 if (s->type != reloc_class_relative)
9614 break;
9615 }
9616 ret = i;
9617 s_non_relative = p;
9618
9619 sq = (struct elf_link_sort_rela *) s_non_relative;
9620 for (; i < count; i++, p += sort_elt)
9621 {
9622 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9623 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9624 sq = sp;
9625 sp->u.offset = sq->rela->r_offset;
9626 }
9627
9628 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9629
9630 struct elf_link_hash_table *htab = elf_hash_table (info);
9631 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9632 {
9633 /* We have plt relocs in .rela.dyn. */
9634 sq = (struct elf_link_sort_rela *) sort;
9635 for (i = 0; i < count; i++)
9636 if (sq[count - i - 1].type != reloc_class_plt)
9637 break;
9638 if (i != 0 && htab->srelplt->size == i * ext_size)
9639 {
9640 struct bfd_link_order **plo;
9641 /* Put srelplt link_order last. This is so the output_offset
9642 set in the next loop is correct for DT_JMPREL. */
9643 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9644 if ((*plo)->type == bfd_indirect_link_order
9645 && (*plo)->u.indirect.section == htab->srelplt)
9646 {
9647 lo = *plo;
9648 *plo = lo->next;
9649 }
9650 else
9651 plo = &(*plo)->next;
9652 *plo = lo;
9653 lo->next = NULL;
9654 dynamic_relocs->map_tail.link_order = lo;
9655 }
9656 }
9657
9658 p = sort;
9659 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9660 if (lo->type == bfd_indirect_link_order)
9661 {
9662 bfd_byte *erel, *erelend;
9663 asection *o = lo->u.indirect.section;
9664
9665 erel = o->contents;
9666 erelend = o->contents + o->size;
9667 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9668 while (erel < erelend)
9669 {
9670 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9671 (*swap_out) (abfd, s->rela, erel);
9672 p += sort_elt;
9673 erel += ext_size;
9674 }
9675 }
9676
9677 free (sort);
9678 *psec = dynamic_relocs;
9679 return ret;
9680 }
9681
9682 /* Add a symbol to the output symbol string table. */
9683
9684 static int
9685 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9686 const char *name,
9687 Elf_Internal_Sym *elfsym,
9688 asection *input_sec,
9689 struct elf_link_hash_entry *h)
9690 {
9691 int (*output_symbol_hook)
9692 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9693 struct elf_link_hash_entry *);
9694 struct elf_link_hash_table *hash_table;
9695 const struct elf_backend_data *bed;
9696 bfd_size_type strtabsize;
9697
9698 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9699
9700 bed = get_elf_backend_data (flinfo->output_bfd);
9701 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9702 if (output_symbol_hook != NULL)
9703 {
9704 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9705 if (ret != 1)
9706 return ret;
9707 }
9708
9709 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9710 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9711 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9712 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9713
9714 if (name == NULL
9715 || *name == '\0'
9716 || (input_sec->flags & SEC_EXCLUDE))
9717 elfsym->st_name = (unsigned long) -1;
9718 else
9719 {
9720 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9721 to get the final offset for st_name. */
9722 char *versioned_name = (char *) name;
9723 if (h != NULL)
9724 {
9725 if (h->versioned == versioned && h->def_dynamic)
9726 {
9727 /* Keep only one '@' for versioned symbols defined in
9728 shared objects. */
9729 char *version = strrchr (name, ELF_VER_CHR);
9730 char *base_end = strchr (name, ELF_VER_CHR);
9731 if (version != base_end)
9732 {
9733 size_t base_len;
9734 size_t len = strlen (name);
9735 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9736 if (versioned_name == NULL)
9737 return 0;
9738 base_len = base_end - name;
9739 memcpy (versioned_name, name, base_len);
9740 memcpy (versioned_name + base_len, version,
9741 len - base_len);
9742 }
9743 }
9744 }
9745 else if (flinfo->info->unique_symbol
9746 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9747 {
9748 struct local_hash_entry *lh;
9749 switch (ELF_ST_TYPE (elfsym->st_info))
9750 {
9751 case STT_FILE:
9752 case STT_SECTION:
9753 break;
9754 default:
9755 lh = (struct local_hash_entry *) bfd_hash_lookup
9756 (&flinfo->local_hash_table, name, TRUE, FALSE);
9757 if (lh == NULL)
9758 return 0;
9759 if (lh->count)
9760 {
9761 /* Append ".COUNT" to duplicated local symbols. */
9762 size_t count_len;
9763 size_t base_len = lh->size;
9764 char buf[30];
9765 sprintf (buf, "%lx", lh->count);
9766 if (!base_len)
9767 {
9768 base_len = strlen (name);
9769 lh->size = base_len;
9770 }
9771 count_len = strlen (buf);
9772 versioned_name = bfd_alloc (flinfo->output_bfd,
9773 base_len + count_len + 2);
9774 if (versioned_name == NULL)
9775 return 0;
9776 memcpy (versioned_name, name, base_len);
9777 versioned_name[base_len] = '.';
9778 memcpy (versioned_name + base_len + 1, buf,
9779 count_len + 1);
9780 }
9781 lh->count++;
9782 break;
9783 }
9784 }
9785 elfsym->st_name
9786 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9787 versioned_name, FALSE);
9788 if (elfsym->st_name == (unsigned long) -1)
9789 return 0;
9790 }
9791
9792 hash_table = elf_hash_table (flinfo->info);
9793 strtabsize = hash_table->strtabsize;
9794 if (strtabsize <= hash_table->strtabcount)
9795 {
9796 strtabsize += strtabsize;
9797 hash_table->strtabsize = strtabsize;
9798 strtabsize *= sizeof (*hash_table->strtab);
9799 hash_table->strtab
9800 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9801 strtabsize);
9802 if (hash_table->strtab == NULL)
9803 return 0;
9804 }
9805 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9806 hash_table->strtab[hash_table->strtabcount].dest_index
9807 = hash_table->strtabcount;
9808 hash_table->strtab[hash_table->strtabcount].destshndx_index
9809 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9810
9811 flinfo->output_bfd->symcount += 1;
9812 hash_table->strtabcount += 1;
9813
9814 return 1;
9815 }
9816
9817 /* Swap symbols out to the symbol table and flush the output symbols to
9818 the file. */
9819
9820 static bfd_boolean
9821 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9822 {
9823 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9824 size_t amt;
9825 size_t i;
9826 const struct elf_backend_data *bed;
9827 bfd_byte *symbuf;
9828 Elf_Internal_Shdr *hdr;
9829 file_ptr pos;
9830 bfd_boolean ret;
9831
9832 if (!hash_table->strtabcount)
9833 return TRUE;
9834
9835 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9836
9837 bed = get_elf_backend_data (flinfo->output_bfd);
9838
9839 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9840 symbuf = (bfd_byte *) bfd_malloc (amt);
9841 if (symbuf == NULL)
9842 return FALSE;
9843
9844 if (flinfo->symshndxbuf)
9845 {
9846 amt = sizeof (Elf_External_Sym_Shndx);
9847 amt *= bfd_get_symcount (flinfo->output_bfd);
9848 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9849 if (flinfo->symshndxbuf == NULL)
9850 {
9851 free (symbuf);
9852 return FALSE;
9853 }
9854 }
9855
9856 for (i = 0; i < hash_table->strtabcount; i++)
9857 {
9858 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9859 if (elfsym->sym.st_name == (unsigned long) -1)
9860 elfsym->sym.st_name = 0;
9861 else
9862 elfsym->sym.st_name
9863 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9864 elfsym->sym.st_name);
9865 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9866 ((bfd_byte *) symbuf
9867 + (elfsym->dest_index
9868 * bed->s->sizeof_sym)),
9869 (flinfo->symshndxbuf
9870 + elfsym->destshndx_index));
9871 }
9872
9873 /* Allow the linker to examine the strtab and symtab now they are
9874 populated. */
9875
9876 if (flinfo->info->callbacks->examine_strtab)
9877 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9878 hash_table->strtabcount,
9879 flinfo->symstrtab);
9880
9881 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9882 pos = hdr->sh_offset + hdr->sh_size;
9883 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9884 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9885 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9886 {
9887 hdr->sh_size += amt;
9888 ret = TRUE;
9889 }
9890 else
9891 ret = FALSE;
9892
9893 free (symbuf);
9894
9895 free (hash_table->strtab);
9896 hash_table->strtab = NULL;
9897
9898 return ret;
9899 }
9900
9901 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9902
9903 static bfd_boolean
9904 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9905 {
9906 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9907 && sym->st_shndx < SHN_LORESERVE)
9908 {
9909 /* The gABI doesn't support dynamic symbols in output sections
9910 beyond 64k. */
9911 _bfd_error_handler
9912 /* xgettext:c-format */
9913 (_("%pB: too many sections: %d (>= %d)"),
9914 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9915 bfd_set_error (bfd_error_nonrepresentable_section);
9916 return FALSE;
9917 }
9918 return TRUE;
9919 }
9920
9921 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9922 allowing an unsatisfied unversioned symbol in the DSO to match a
9923 versioned symbol that would normally require an explicit version.
9924 We also handle the case that a DSO references a hidden symbol
9925 which may be satisfied by a versioned symbol in another DSO. */
9926
9927 static bfd_boolean
9928 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9929 const struct elf_backend_data *bed,
9930 struct elf_link_hash_entry *h)
9931 {
9932 bfd *abfd;
9933 struct elf_link_loaded_list *loaded;
9934
9935 if (!is_elf_hash_table (info->hash))
9936 return FALSE;
9937
9938 /* Check indirect symbol. */
9939 while (h->root.type == bfd_link_hash_indirect)
9940 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9941
9942 switch (h->root.type)
9943 {
9944 default:
9945 abfd = NULL;
9946 break;
9947
9948 case bfd_link_hash_undefined:
9949 case bfd_link_hash_undefweak:
9950 abfd = h->root.u.undef.abfd;
9951 if (abfd == NULL
9952 || (abfd->flags & DYNAMIC) == 0
9953 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9954 return FALSE;
9955 break;
9956
9957 case bfd_link_hash_defined:
9958 case bfd_link_hash_defweak:
9959 abfd = h->root.u.def.section->owner;
9960 break;
9961
9962 case bfd_link_hash_common:
9963 abfd = h->root.u.c.p->section->owner;
9964 break;
9965 }
9966 BFD_ASSERT (abfd != NULL);
9967
9968 for (loaded = elf_hash_table (info)->dyn_loaded;
9969 loaded != NULL;
9970 loaded = loaded->next)
9971 {
9972 bfd *input;
9973 Elf_Internal_Shdr *hdr;
9974 size_t symcount;
9975 size_t extsymcount;
9976 size_t extsymoff;
9977 Elf_Internal_Shdr *versymhdr;
9978 Elf_Internal_Sym *isym;
9979 Elf_Internal_Sym *isymend;
9980 Elf_Internal_Sym *isymbuf;
9981 Elf_External_Versym *ever;
9982 Elf_External_Versym *extversym;
9983
9984 input = loaded->abfd;
9985
9986 /* We check each DSO for a possible hidden versioned definition. */
9987 if (input == abfd
9988 || elf_dynversym (input) == 0)
9989 continue;
9990
9991 hdr = &elf_tdata (input)->dynsymtab_hdr;
9992
9993 symcount = hdr->sh_size / bed->s->sizeof_sym;
9994 if (elf_bad_symtab (input))
9995 {
9996 extsymcount = symcount;
9997 extsymoff = 0;
9998 }
9999 else
10000 {
10001 extsymcount = symcount - hdr->sh_info;
10002 extsymoff = hdr->sh_info;
10003 }
10004
10005 if (extsymcount == 0)
10006 continue;
10007
10008 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10009 NULL, NULL, NULL);
10010 if (isymbuf == NULL)
10011 return FALSE;
10012
10013 /* Read in any version definitions. */
10014 versymhdr = &elf_tdata (input)->dynversym_hdr;
10015 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10016 || (extversym = (Elf_External_Versym *)
10017 _bfd_malloc_and_read (input, versymhdr->sh_size,
10018 versymhdr->sh_size)) == NULL)
10019 {
10020 free (isymbuf);
10021 return FALSE;
10022 }
10023
10024 ever = extversym + extsymoff;
10025 isymend = isymbuf + extsymcount;
10026 for (isym = isymbuf; isym < isymend; isym++, ever++)
10027 {
10028 const char *name;
10029 Elf_Internal_Versym iver;
10030 unsigned short version_index;
10031
10032 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10033 || isym->st_shndx == SHN_UNDEF)
10034 continue;
10035
10036 name = bfd_elf_string_from_elf_section (input,
10037 hdr->sh_link,
10038 isym->st_name);
10039 if (strcmp (name, h->root.root.string) != 0)
10040 continue;
10041
10042 _bfd_elf_swap_versym_in (input, ever, &iver);
10043
10044 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10045 && !(h->def_regular
10046 && h->forced_local))
10047 {
10048 /* If we have a non-hidden versioned sym, then it should
10049 have provided a definition for the undefined sym unless
10050 it is defined in a non-shared object and forced local.
10051 */
10052 abort ();
10053 }
10054
10055 version_index = iver.vs_vers & VERSYM_VERSION;
10056 if (version_index == 1 || version_index == 2)
10057 {
10058 /* This is the base or first version. We can use it. */
10059 free (extversym);
10060 free (isymbuf);
10061 return TRUE;
10062 }
10063 }
10064
10065 free (extversym);
10066 free (isymbuf);
10067 }
10068
10069 return FALSE;
10070 }
10071
10072 /* Convert ELF common symbol TYPE. */
10073
10074 static int
10075 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10076 {
10077 /* Commom symbol can only appear in relocatable link. */
10078 if (!bfd_link_relocatable (info))
10079 abort ();
10080 switch (info->elf_stt_common)
10081 {
10082 case unchanged:
10083 break;
10084 case elf_stt_common:
10085 type = STT_COMMON;
10086 break;
10087 case no_elf_stt_common:
10088 type = STT_OBJECT;
10089 break;
10090 }
10091 return type;
10092 }
10093
10094 /* Add an external symbol to the symbol table. This is called from
10095 the hash table traversal routine. When generating a shared object,
10096 we go through the symbol table twice. The first time we output
10097 anything that might have been forced to local scope in a version
10098 script. The second time we output the symbols that are still
10099 global symbols. */
10100
10101 static bfd_boolean
10102 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10103 {
10104 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10105 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10106 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10107 bfd_boolean strip;
10108 Elf_Internal_Sym sym;
10109 asection *input_sec;
10110 const struct elf_backend_data *bed;
10111 long indx;
10112 int ret;
10113 unsigned int type;
10114
10115 if (h->root.type == bfd_link_hash_warning)
10116 {
10117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10118 if (h->root.type == bfd_link_hash_new)
10119 return TRUE;
10120 }
10121
10122 /* Decide whether to output this symbol in this pass. */
10123 if (eoinfo->localsyms)
10124 {
10125 if (!h->forced_local)
10126 return TRUE;
10127 }
10128 else
10129 {
10130 if (h->forced_local)
10131 return TRUE;
10132 }
10133
10134 bed = get_elf_backend_data (flinfo->output_bfd);
10135
10136 if (h->root.type == bfd_link_hash_undefined)
10137 {
10138 /* If we have an undefined symbol reference here then it must have
10139 come from a shared library that is being linked in. (Undefined
10140 references in regular files have already been handled unless
10141 they are in unreferenced sections which are removed by garbage
10142 collection). */
10143 bfd_boolean ignore_undef = FALSE;
10144
10145 /* Some symbols may be special in that the fact that they're
10146 undefined can be safely ignored - let backend determine that. */
10147 if (bed->elf_backend_ignore_undef_symbol)
10148 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10149
10150 /* If we are reporting errors for this situation then do so now. */
10151 if (!ignore_undef
10152 && h->ref_dynamic_nonweak
10153 && (!h->ref_regular || flinfo->info->gc_sections)
10154 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10155 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10156 {
10157 flinfo->info->callbacks->undefined_symbol
10158 (flinfo->info, h->root.root.string,
10159 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10160 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10161 && !flinfo->info->warn_unresolved_syms);
10162 }
10163
10164 /* Strip a global symbol defined in a discarded section. */
10165 if (h->indx == -3)
10166 return TRUE;
10167 }
10168
10169 /* We should also warn if a forced local symbol is referenced from
10170 shared libraries. */
10171 if (bfd_link_executable (flinfo->info)
10172 && h->forced_local
10173 && h->ref_dynamic
10174 && h->def_regular
10175 && !h->dynamic_def
10176 && h->ref_dynamic_nonweak
10177 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10178 {
10179 bfd *def_bfd;
10180 const char *msg;
10181 struct elf_link_hash_entry *hi = h;
10182
10183 /* Check indirect symbol. */
10184 while (hi->root.type == bfd_link_hash_indirect)
10185 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10186
10187 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10188 /* xgettext:c-format */
10189 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10190 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10191 /* xgettext:c-format */
10192 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10193 else
10194 /* xgettext:c-format */
10195 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10196 def_bfd = flinfo->output_bfd;
10197 if (hi->root.u.def.section != bfd_abs_section_ptr)
10198 def_bfd = hi->root.u.def.section->owner;
10199 _bfd_error_handler (msg, flinfo->output_bfd,
10200 h->root.root.string, def_bfd);
10201 bfd_set_error (bfd_error_bad_value);
10202 eoinfo->failed = TRUE;
10203 return FALSE;
10204 }
10205
10206 /* We don't want to output symbols that have never been mentioned by
10207 a regular file, or that we have been told to strip. However, if
10208 h->indx is set to -2, the symbol is used by a reloc and we must
10209 output it. */
10210 strip = FALSE;
10211 if (h->indx == -2)
10212 ;
10213 else if ((h->def_dynamic
10214 || h->ref_dynamic
10215 || h->root.type == bfd_link_hash_new)
10216 && !h->def_regular
10217 && !h->ref_regular)
10218 strip = TRUE;
10219 else if (flinfo->info->strip == strip_all)
10220 strip = TRUE;
10221 else if (flinfo->info->strip == strip_some
10222 && bfd_hash_lookup (flinfo->info->keep_hash,
10223 h->root.root.string, FALSE, FALSE) == NULL)
10224 strip = TRUE;
10225 else if ((h->root.type == bfd_link_hash_defined
10226 || h->root.type == bfd_link_hash_defweak)
10227 && ((flinfo->info->strip_discarded
10228 && discarded_section (h->root.u.def.section))
10229 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10230 && h->root.u.def.section->owner != NULL
10231 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10232 strip = TRUE;
10233 else if ((h->root.type == bfd_link_hash_undefined
10234 || h->root.type == bfd_link_hash_undefweak)
10235 && h->root.u.undef.abfd != NULL
10236 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10237 strip = TRUE;
10238
10239 type = h->type;
10240
10241 /* If we're stripping it, and it's not a dynamic symbol, there's
10242 nothing else to do. However, if it is a forced local symbol or
10243 an ifunc symbol we need to give the backend finish_dynamic_symbol
10244 function a chance to make it dynamic. */
10245 if (strip
10246 && h->dynindx == -1
10247 && type != STT_GNU_IFUNC
10248 && !h->forced_local)
10249 return TRUE;
10250
10251 sym.st_value = 0;
10252 sym.st_size = h->size;
10253 sym.st_other = h->other;
10254 switch (h->root.type)
10255 {
10256 default:
10257 case bfd_link_hash_new:
10258 case bfd_link_hash_warning:
10259 abort ();
10260 return FALSE;
10261
10262 case bfd_link_hash_undefined:
10263 case bfd_link_hash_undefweak:
10264 input_sec = bfd_und_section_ptr;
10265 sym.st_shndx = SHN_UNDEF;
10266 break;
10267
10268 case bfd_link_hash_defined:
10269 case bfd_link_hash_defweak:
10270 {
10271 input_sec = h->root.u.def.section;
10272 if (input_sec->output_section != NULL)
10273 {
10274 sym.st_shndx =
10275 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10276 input_sec->output_section);
10277 if (sym.st_shndx == SHN_BAD)
10278 {
10279 _bfd_error_handler
10280 /* xgettext:c-format */
10281 (_("%pB: could not find output section %pA for input section %pA"),
10282 flinfo->output_bfd, input_sec->output_section, input_sec);
10283 bfd_set_error (bfd_error_nonrepresentable_section);
10284 eoinfo->failed = TRUE;
10285 return FALSE;
10286 }
10287
10288 /* ELF symbols in relocatable files are section relative,
10289 but in nonrelocatable files they are virtual
10290 addresses. */
10291 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10292 if (!bfd_link_relocatable (flinfo->info))
10293 {
10294 sym.st_value += input_sec->output_section->vma;
10295 if (h->type == STT_TLS)
10296 {
10297 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10298 if (tls_sec != NULL)
10299 sym.st_value -= tls_sec->vma;
10300 }
10301 }
10302 }
10303 else
10304 {
10305 BFD_ASSERT (input_sec->owner == NULL
10306 || (input_sec->owner->flags & DYNAMIC) != 0);
10307 sym.st_shndx = SHN_UNDEF;
10308 input_sec = bfd_und_section_ptr;
10309 }
10310 }
10311 break;
10312
10313 case bfd_link_hash_common:
10314 input_sec = h->root.u.c.p->section;
10315 sym.st_shndx = bed->common_section_index (input_sec);
10316 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10317 break;
10318
10319 case bfd_link_hash_indirect:
10320 /* These symbols are created by symbol versioning. They point
10321 to the decorated version of the name. For example, if the
10322 symbol foo@@GNU_1.2 is the default, which should be used when
10323 foo is used with no version, then we add an indirect symbol
10324 foo which points to foo@@GNU_1.2. We ignore these symbols,
10325 since the indirected symbol is already in the hash table. */
10326 return TRUE;
10327 }
10328
10329 if (type == STT_COMMON || type == STT_OBJECT)
10330 switch (h->root.type)
10331 {
10332 case bfd_link_hash_common:
10333 type = elf_link_convert_common_type (flinfo->info, type);
10334 break;
10335 case bfd_link_hash_defined:
10336 case bfd_link_hash_defweak:
10337 if (bed->common_definition (&sym))
10338 type = elf_link_convert_common_type (flinfo->info, type);
10339 else
10340 type = STT_OBJECT;
10341 break;
10342 case bfd_link_hash_undefined:
10343 case bfd_link_hash_undefweak:
10344 break;
10345 default:
10346 abort ();
10347 }
10348
10349 if (h->forced_local)
10350 {
10351 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10352 /* Turn off visibility on local symbol. */
10353 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10354 }
10355 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10356 else if (h->unique_global && h->def_regular)
10357 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10358 else if (h->root.type == bfd_link_hash_undefweak
10359 || h->root.type == bfd_link_hash_defweak)
10360 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10361 else
10362 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10363 sym.st_target_internal = h->target_internal;
10364
10365 /* Give the processor backend a chance to tweak the symbol value,
10366 and also to finish up anything that needs to be done for this
10367 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10368 forced local syms when non-shared is due to a historical quirk.
10369 STT_GNU_IFUNC symbol must go through PLT. */
10370 if ((h->type == STT_GNU_IFUNC
10371 && h->def_regular
10372 && !bfd_link_relocatable (flinfo->info))
10373 || ((h->dynindx != -1
10374 || h->forced_local)
10375 && ((bfd_link_pic (flinfo->info)
10376 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10377 || h->root.type != bfd_link_hash_undefweak))
10378 || !h->forced_local)
10379 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10380 {
10381 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10382 (flinfo->output_bfd, flinfo->info, h, &sym)))
10383 {
10384 eoinfo->failed = TRUE;
10385 return FALSE;
10386 }
10387 }
10388
10389 /* If we are marking the symbol as undefined, and there are no
10390 non-weak references to this symbol from a regular object, then
10391 mark the symbol as weak undefined; if there are non-weak
10392 references, mark the symbol as strong. We can't do this earlier,
10393 because it might not be marked as undefined until the
10394 finish_dynamic_symbol routine gets through with it. */
10395 if (sym.st_shndx == SHN_UNDEF
10396 && h->ref_regular
10397 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10398 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10399 {
10400 int bindtype;
10401 type = ELF_ST_TYPE (sym.st_info);
10402
10403 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10404 if (type == STT_GNU_IFUNC)
10405 type = STT_FUNC;
10406
10407 if (h->ref_regular_nonweak)
10408 bindtype = STB_GLOBAL;
10409 else
10410 bindtype = STB_WEAK;
10411 sym.st_info = ELF_ST_INFO (bindtype, type);
10412 }
10413
10414 /* If this is a symbol defined in a dynamic library, don't use the
10415 symbol size from the dynamic library. Relinking an executable
10416 against a new library may introduce gratuitous changes in the
10417 executable's symbols if we keep the size. */
10418 if (sym.st_shndx == SHN_UNDEF
10419 && !h->def_regular
10420 && h->def_dynamic)
10421 sym.st_size = 0;
10422
10423 /* If a non-weak symbol with non-default visibility is not defined
10424 locally, it is a fatal error. */
10425 if (!bfd_link_relocatable (flinfo->info)
10426 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10427 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10428 && h->root.type == bfd_link_hash_undefined
10429 && !h->def_regular)
10430 {
10431 const char *msg;
10432
10433 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10434 /* xgettext:c-format */
10435 msg = _("%pB: protected symbol `%s' isn't defined");
10436 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10437 /* xgettext:c-format */
10438 msg = _("%pB: internal symbol `%s' isn't defined");
10439 else
10440 /* xgettext:c-format */
10441 msg = _("%pB: hidden symbol `%s' isn't defined");
10442 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10443 bfd_set_error (bfd_error_bad_value);
10444 eoinfo->failed = TRUE;
10445 return FALSE;
10446 }
10447
10448 /* If this symbol should be put in the .dynsym section, then put it
10449 there now. We already know the symbol index. We also fill in
10450 the entry in the .hash section. */
10451 if (h->dynindx != -1
10452 && elf_hash_table (flinfo->info)->dynamic_sections_created
10453 && elf_hash_table (flinfo->info)->dynsym != NULL
10454 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10455 {
10456 bfd_byte *esym;
10457
10458 /* Since there is no version information in the dynamic string,
10459 if there is no version info in symbol version section, we will
10460 have a run-time problem if not linking executable, referenced
10461 by shared library, or not bound locally. */
10462 if (h->verinfo.verdef == NULL
10463 && (!bfd_link_executable (flinfo->info)
10464 || h->ref_dynamic
10465 || !h->def_regular))
10466 {
10467 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10468
10469 if (p && p [1] != '\0')
10470 {
10471 _bfd_error_handler
10472 /* xgettext:c-format */
10473 (_("%pB: no symbol version section for versioned symbol `%s'"),
10474 flinfo->output_bfd, h->root.root.string);
10475 eoinfo->failed = TRUE;
10476 return FALSE;
10477 }
10478 }
10479
10480 sym.st_name = h->dynstr_index;
10481 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10482 + h->dynindx * bed->s->sizeof_sym);
10483 if (!check_dynsym (flinfo->output_bfd, &sym))
10484 {
10485 eoinfo->failed = TRUE;
10486 return FALSE;
10487 }
10488 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10489
10490 if (flinfo->hash_sec != NULL)
10491 {
10492 size_t hash_entry_size;
10493 bfd_byte *bucketpos;
10494 bfd_vma chain;
10495 size_t bucketcount;
10496 size_t bucket;
10497
10498 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10499 bucket = h->u.elf_hash_value % bucketcount;
10500
10501 hash_entry_size
10502 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10503 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10504 + (bucket + 2) * hash_entry_size);
10505 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10506 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10507 bucketpos);
10508 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10509 ((bfd_byte *) flinfo->hash_sec->contents
10510 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10511 }
10512
10513 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10514 {
10515 Elf_Internal_Versym iversym;
10516 Elf_External_Versym *eversym;
10517
10518 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10519 {
10520 if (h->verinfo.verdef == NULL
10521 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10522 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10523 iversym.vs_vers = 0;
10524 else
10525 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10526 }
10527 else
10528 {
10529 if (h->verinfo.vertree == NULL)
10530 iversym.vs_vers = 1;
10531 else
10532 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10533 if (flinfo->info->create_default_symver)
10534 iversym.vs_vers++;
10535 }
10536
10537 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10538 defined locally. */
10539 if (h->versioned == versioned_hidden && h->def_regular)
10540 iversym.vs_vers |= VERSYM_HIDDEN;
10541
10542 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10543 eversym += h->dynindx;
10544 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10545 }
10546 }
10547
10548 /* If the symbol is undefined, and we didn't output it to .dynsym,
10549 strip it from .symtab too. Obviously we can't do this for
10550 relocatable output or when needed for --emit-relocs. */
10551 else if (input_sec == bfd_und_section_ptr
10552 && h->indx != -2
10553 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10554 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10555 && !bfd_link_relocatable (flinfo->info))
10556 return TRUE;
10557
10558 /* Also strip others that we couldn't earlier due to dynamic symbol
10559 processing. */
10560 if (strip)
10561 return TRUE;
10562 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10563 return TRUE;
10564
10565 /* Output a FILE symbol so that following locals are not associated
10566 with the wrong input file. We need one for forced local symbols
10567 if we've seen more than one FILE symbol or when we have exactly
10568 one FILE symbol but global symbols are present in a file other
10569 than the one with the FILE symbol. We also need one if linker
10570 defined symbols are present. In practice these conditions are
10571 always met, so just emit the FILE symbol unconditionally. */
10572 if (eoinfo->localsyms
10573 && !eoinfo->file_sym_done
10574 && eoinfo->flinfo->filesym_count != 0)
10575 {
10576 Elf_Internal_Sym fsym;
10577
10578 memset (&fsym, 0, sizeof (fsym));
10579 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10580 fsym.st_shndx = SHN_ABS;
10581 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10582 bfd_und_section_ptr, NULL))
10583 return FALSE;
10584
10585 eoinfo->file_sym_done = TRUE;
10586 }
10587
10588 indx = bfd_get_symcount (flinfo->output_bfd);
10589 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10590 input_sec, h);
10591 if (ret == 0)
10592 {
10593 eoinfo->failed = TRUE;
10594 return FALSE;
10595 }
10596 else if (ret == 1)
10597 h->indx = indx;
10598 else if (h->indx == -2)
10599 abort();
10600
10601 return TRUE;
10602 }
10603
10604 /* Return TRUE if special handling is done for relocs in SEC against
10605 symbols defined in discarded sections. */
10606
10607 static bfd_boolean
10608 elf_section_ignore_discarded_relocs (asection *sec)
10609 {
10610 const struct elf_backend_data *bed;
10611
10612 switch (sec->sec_info_type)
10613 {
10614 case SEC_INFO_TYPE_STABS:
10615 case SEC_INFO_TYPE_EH_FRAME:
10616 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10617 return TRUE;
10618 default:
10619 break;
10620 }
10621
10622 bed = get_elf_backend_data (sec->owner);
10623 if (bed->elf_backend_ignore_discarded_relocs != NULL
10624 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10625 return TRUE;
10626
10627 return FALSE;
10628 }
10629
10630 /* Return a mask saying how ld should treat relocations in SEC against
10631 symbols defined in discarded sections. If this function returns
10632 COMPLAIN set, ld will issue a warning message. If this function
10633 returns PRETEND set, and the discarded section was link-once and the
10634 same size as the kept link-once section, ld will pretend that the
10635 symbol was actually defined in the kept section. Otherwise ld will
10636 zero the reloc (at least that is the intent, but some cooperation by
10637 the target dependent code is needed, particularly for REL targets). */
10638
10639 unsigned int
10640 _bfd_elf_default_action_discarded (asection *sec)
10641 {
10642 if (sec->flags & SEC_DEBUGGING)
10643 return PRETEND;
10644
10645 if (strcmp (".eh_frame", sec->name) == 0)
10646 return 0;
10647
10648 if (strcmp (".gcc_except_table", sec->name) == 0)
10649 return 0;
10650
10651 return COMPLAIN | PRETEND;
10652 }
10653
10654 /* Find a match between a section and a member of a section group. */
10655
10656 static asection *
10657 match_group_member (asection *sec, asection *group,
10658 struct bfd_link_info *info)
10659 {
10660 asection *first = elf_next_in_group (group);
10661 asection *s = first;
10662
10663 while (s != NULL)
10664 {
10665 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10666 return s;
10667
10668 s = elf_next_in_group (s);
10669 if (s == first)
10670 break;
10671 }
10672
10673 return NULL;
10674 }
10675
10676 /* Check if the kept section of a discarded section SEC can be used
10677 to replace it. Return the replacement if it is OK. Otherwise return
10678 NULL. */
10679
10680 asection *
10681 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10682 {
10683 asection *kept;
10684
10685 kept = sec->kept_section;
10686 if (kept != NULL)
10687 {
10688 if ((kept->flags & SEC_GROUP) != 0)
10689 kept = match_group_member (sec, kept, info);
10690 if (kept != NULL
10691 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10692 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10693 kept = NULL;
10694 sec->kept_section = kept;
10695 }
10696 return kept;
10697 }
10698
10699 /* Link an input file into the linker output file. This function
10700 handles all the sections and relocations of the input file at once.
10701 This is so that we only have to read the local symbols once, and
10702 don't have to keep them in memory. */
10703
10704 static bfd_boolean
10705 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10706 {
10707 int (*relocate_section)
10708 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10709 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10710 bfd *output_bfd;
10711 Elf_Internal_Shdr *symtab_hdr;
10712 size_t locsymcount;
10713 size_t extsymoff;
10714 Elf_Internal_Sym *isymbuf;
10715 Elf_Internal_Sym *isym;
10716 Elf_Internal_Sym *isymend;
10717 long *pindex;
10718 asection **ppsection;
10719 asection *o;
10720 const struct elf_backend_data *bed;
10721 struct elf_link_hash_entry **sym_hashes;
10722 bfd_size_type address_size;
10723 bfd_vma r_type_mask;
10724 int r_sym_shift;
10725 bfd_boolean have_file_sym = FALSE;
10726
10727 output_bfd = flinfo->output_bfd;
10728 bed = get_elf_backend_data (output_bfd);
10729 relocate_section = bed->elf_backend_relocate_section;
10730
10731 /* If this is a dynamic object, we don't want to do anything here:
10732 we don't want the local symbols, and we don't want the section
10733 contents. */
10734 if ((input_bfd->flags & DYNAMIC) != 0)
10735 return TRUE;
10736
10737 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10738 if (elf_bad_symtab (input_bfd))
10739 {
10740 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10741 extsymoff = 0;
10742 }
10743 else
10744 {
10745 locsymcount = symtab_hdr->sh_info;
10746 extsymoff = symtab_hdr->sh_info;
10747 }
10748
10749 /* Read the local symbols. */
10750 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10751 if (isymbuf == NULL && locsymcount != 0)
10752 {
10753 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10754 flinfo->internal_syms,
10755 flinfo->external_syms,
10756 flinfo->locsym_shndx);
10757 if (isymbuf == NULL)
10758 return FALSE;
10759 }
10760
10761 /* Find local symbol sections and adjust values of symbols in
10762 SEC_MERGE sections. Write out those local symbols we know are
10763 going into the output file. */
10764 isymend = isymbuf + locsymcount;
10765 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10766 isym < isymend;
10767 isym++, pindex++, ppsection++)
10768 {
10769 asection *isec;
10770 const char *name;
10771 Elf_Internal_Sym osym;
10772 long indx;
10773 int ret;
10774
10775 *pindex = -1;
10776
10777 if (elf_bad_symtab (input_bfd))
10778 {
10779 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10780 {
10781 *ppsection = NULL;
10782 continue;
10783 }
10784 }
10785
10786 if (isym->st_shndx == SHN_UNDEF)
10787 isec = bfd_und_section_ptr;
10788 else if (isym->st_shndx == SHN_ABS)
10789 isec = bfd_abs_section_ptr;
10790 else if (isym->st_shndx == SHN_COMMON)
10791 isec = bfd_com_section_ptr;
10792 else
10793 {
10794 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10795 if (isec == NULL)
10796 {
10797 /* Don't attempt to output symbols with st_shnx in the
10798 reserved range other than SHN_ABS and SHN_COMMON. */
10799 isec = bfd_und_section_ptr;
10800 }
10801 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10802 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10803 isym->st_value =
10804 _bfd_merged_section_offset (output_bfd, &isec,
10805 elf_section_data (isec)->sec_info,
10806 isym->st_value);
10807 }
10808
10809 *ppsection = isec;
10810
10811 /* Don't output the first, undefined, symbol. In fact, don't
10812 output any undefined local symbol. */
10813 if (isec == bfd_und_section_ptr)
10814 continue;
10815
10816 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10817 {
10818 /* We never output section symbols. Instead, we use the
10819 section symbol of the corresponding section in the output
10820 file. */
10821 continue;
10822 }
10823
10824 /* If we are stripping all symbols, we don't want to output this
10825 one. */
10826 if (flinfo->info->strip == strip_all)
10827 continue;
10828
10829 /* If we are discarding all local symbols, we don't want to
10830 output this one. If we are generating a relocatable output
10831 file, then some of the local symbols may be required by
10832 relocs; we output them below as we discover that they are
10833 needed. */
10834 if (flinfo->info->discard == discard_all)
10835 continue;
10836
10837 /* If this symbol is defined in a section which we are
10838 discarding, we don't need to keep it. */
10839 if (isym->st_shndx != SHN_UNDEF
10840 && isym->st_shndx < SHN_LORESERVE
10841 && isec->output_section == NULL
10842 && flinfo->info->non_contiguous_regions
10843 && flinfo->info->non_contiguous_regions_warnings)
10844 {
10845 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10846 "discards section `%s' from '%s'\n"),
10847 isec->name, bfd_get_filename (isec->owner));
10848 continue;
10849 }
10850
10851 if (isym->st_shndx != SHN_UNDEF
10852 && isym->st_shndx < SHN_LORESERVE
10853 && bfd_section_removed_from_list (output_bfd,
10854 isec->output_section))
10855 continue;
10856
10857 /* Get the name of the symbol. */
10858 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10859 isym->st_name);
10860 if (name == NULL)
10861 return FALSE;
10862
10863 /* See if we are discarding symbols with this name. */
10864 if ((flinfo->info->strip == strip_some
10865 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10866 == NULL))
10867 || (((flinfo->info->discard == discard_sec_merge
10868 && (isec->flags & SEC_MERGE)
10869 && !bfd_link_relocatable (flinfo->info))
10870 || flinfo->info->discard == discard_l)
10871 && bfd_is_local_label_name (input_bfd, name)))
10872 continue;
10873
10874 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10875 {
10876 if (input_bfd->lto_output)
10877 /* -flto puts a temp file name here. This means builds
10878 are not reproducible. Discard the symbol. */
10879 continue;
10880 have_file_sym = TRUE;
10881 flinfo->filesym_count += 1;
10882 }
10883 if (!have_file_sym)
10884 {
10885 /* In the absence of debug info, bfd_find_nearest_line uses
10886 FILE symbols to determine the source file for local
10887 function symbols. Provide a FILE symbol here if input
10888 files lack such, so that their symbols won't be
10889 associated with a previous input file. It's not the
10890 source file, but the best we can do. */
10891 have_file_sym = TRUE;
10892 flinfo->filesym_count += 1;
10893 memset (&osym, 0, sizeof (osym));
10894 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10895 osym.st_shndx = SHN_ABS;
10896 if (!elf_link_output_symstrtab (flinfo,
10897 (input_bfd->lto_output ? NULL
10898 : bfd_get_filename (input_bfd)),
10899 &osym, bfd_abs_section_ptr,
10900 NULL))
10901 return FALSE;
10902 }
10903
10904 osym = *isym;
10905
10906 /* Adjust the section index for the output file. */
10907 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10908 isec->output_section);
10909 if (osym.st_shndx == SHN_BAD)
10910 return FALSE;
10911
10912 /* ELF symbols in relocatable files are section relative, but
10913 in executable files they are virtual addresses. Note that
10914 this code assumes that all ELF sections have an associated
10915 BFD section with a reasonable value for output_offset; below
10916 we assume that they also have a reasonable value for
10917 output_section. Any special sections must be set up to meet
10918 these requirements. */
10919 osym.st_value += isec->output_offset;
10920 if (!bfd_link_relocatable (flinfo->info))
10921 {
10922 osym.st_value += isec->output_section->vma;
10923 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10924 {
10925 /* STT_TLS symbols are relative to PT_TLS segment base. */
10926 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10927 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10928 else
10929 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10930 STT_NOTYPE);
10931 }
10932 }
10933
10934 indx = bfd_get_symcount (output_bfd);
10935 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10936 if (ret == 0)
10937 return FALSE;
10938 else if (ret == 1)
10939 *pindex = indx;
10940 }
10941
10942 if (bed->s->arch_size == 32)
10943 {
10944 r_type_mask = 0xff;
10945 r_sym_shift = 8;
10946 address_size = 4;
10947 }
10948 else
10949 {
10950 r_type_mask = 0xffffffff;
10951 r_sym_shift = 32;
10952 address_size = 8;
10953 }
10954
10955 /* Relocate the contents of each section. */
10956 sym_hashes = elf_sym_hashes (input_bfd);
10957 for (o = input_bfd->sections; o != NULL; o = o->next)
10958 {
10959 bfd_byte *contents;
10960
10961 if (! o->linker_mark)
10962 {
10963 /* This section was omitted from the link. */
10964 continue;
10965 }
10966
10967 if (!flinfo->info->resolve_section_groups
10968 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10969 {
10970 /* Deal with the group signature symbol. */
10971 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10972 unsigned long symndx = sec_data->this_hdr.sh_info;
10973 asection *osec = o->output_section;
10974
10975 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10976 if (symndx >= locsymcount
10977 || (elf_bad_symtab (input_bfd)
10978 && flinfo->sections[symndx] == NULL))
10979 {
10980 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10981 while (h->root.type == bfd_link_hash_indirect
10982 || h->root.type == bfd_link_hash_warning)
10983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10984 /* Arrange for symbol to be output. */
10985 h->indx = -2;
10986 elf_section_data (osec)->this_hdr.sh_info = -2;
10987 }
10988 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10989 {
10990 /* We'll use the output section target_index. */
10991 asection *sec = flinfo->sections[symndx]->output_section;
10992 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10993 }
10994 else
10995 {
10996 if (flinfo->indices[symndx] == -1)
10997 {
10998 /* Otherwise output the local symbol now. */
10999 Elf_Internal_Sym sym = isymbuf[symndx];
11000 asection *sec = flinfo->sections[symndx]->output_section;
11001 const char *name;
11002 long indx;
11003 int ret;
11004
11005 name = bfd_elf_string_from_elf_section (input_bfd,
11006 symtab_hdr->sh_link,
11007 sym.st_name);
11008 if (name == NULL)
11009 return FALSE;
11010
11011 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11012 sec);
11013 if (sym.st_shndx == SHN_BAD)
11014 return FALSE;
11015
11016 sym.st_value += o->output_offset;
11017
11018 indx = bfd_get_symcount (output_bfd);
11019 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11020 NULL);
11021 if (ret == 0)
11022 return FALSE;
11023 else if (ret == 1)
11024 flinfo->indices[symndx] = indx;
11025 else
11026 abort ();
11027 }
11028 elf_section_data (osec)->this_hdr.sh_info
11029 = flinfo->indices[symndx];
11030 }
11031 }
11032
11033 if ((o->flags & SEC_HAS_CONTENTS) == 0
11034 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11035 continue;
11036
11037 if ((o->flags & SEC_LINKER_CREATED) != 0)
11038 {
11039 /* Section was created by _bfd_elf_link_create_dynamic_sections
11040 or somesuch. */
11041 continue;
11042 }
11043
11044 /* Get the contents of the section. They have been cached by a
11045 relaxation routine. Note that o is a section in an input
11046 file, so the contents field will not have been set by any of
11047 the routines which work on output files. */
11048 if (elf_section_data (o)->this_hdr.contents != NULL)
11049 {
11050 contents = elf_section_data (o)->this_hdr.contents;
11051 if (bed->caches_rawsize
11052 && o->rawsize != 0
11053 && o->rawsize < o->size)
11054 {
11055 memcpy (flinfo->contents, contents, o->rawsize);
11056 contents = flinfo->contents;
11057 }
11058 }
11059 else
11060 {
11061 contents = flinfo->contents;
11062 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11063 return FALSE;
11064 }
11065
11066 if ((o->flags & SEC_RELOC) != 0)
11067 {
11068 Elf_Internal_Rela *internal_relocs;
11069 Elf_Internal_Rela *rel, *relend;
11070 int action_discarded;
11071 int ret;
11072
11073 /* Get the swapped relocs. */
11074 internal_relocs
11075 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11076 flinfo->internal_relocs, FALSE);
11077 if (internal_relocs == NULL
11078 && o->reloc_count > 0)
11079 return FALSE;
11080
11081 /* We need to reverse-copy input .ctors/.dtors sections if
11082 they are placed in .init_array/.finit_array for output. */
11083 if (o->size > address_size
11084 && ((strncmp (o->name, ".ctors", 6) == 0
11085 && strcmp (o->output_section->name,
11086 ".init_array") == 0)
11087 || (strncmp (o->name, ".dtors", 6) == 0
11088 && strcmp (o->output_section->name,
11089 ".fini_array") == 0))
11090 && (o->name[6] == 0 || o->name[6] == '.'))
11091 {
11092 if (o->size * bed->s->int_rels_per_ext_rel
11093 != o->reloc_count * address_size)
11094 {
11095 _bfd_error_handler
11096 /* xgettext:c-format */
11097 (_("error: %pB: size of section %pA is not "
11098 "multiple of address size"),
11099 input_bfd, o);
11100 bfd_set_error (bfd_error_bad_value);
11101 return FALSE;
11102 }
11103 o->flags |= SEC_ELF_REVERSE_COPY;
11104 }
11105
11106 action_discarded = -1;
11107 if (!elf_section_ignore_discarded_relocs (o))
11108 action_discarded = (*bed->action_discarded) (o);
11109
11110 /* Run through the relocs evaluating complex reloc symbols and
11111 looking for relocs against symbols from discarded sections
11112 or section symbols from removed link-once sections.
11113 Complain about relocs against discarded sections. Zero
11114 relocs against removed link-once sections. */
11115
11116 rel = internal_relocs;
11117 relend = rel + o->reloc_count;
11118 for ( ; rel < relend; rel++)
11119 {
11120 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11121 unsigned int s_type;
11122 asection **ps, *sec;
11123 struct elf_link_hash_entry *h = NULL;
11124 const char *sym_name;
11125
11126 if (r_symndx == STN_UNDEF)
11127 continue;
11128
11129 if (r_symndx >= locsymcount
11130 || (elf_bad_symtab (input_bfd)
11131 && flinfo->sections[r_symndx] == NULL))
11132 {
11133 h = sym_hashes[r_symndx - extsymoff];
11134
11135 /* Badly formatted input files can contain relocs that
11136 reference non-existant symbols. Check here so that
11137 we do not seg fault. */
11138 if (h == NULL)
11139 {
11140 _bfd_error_handler
11141 /* xgettext:c-format */
11142 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11143 "that references a non-existent global symbol"),
11144 input_bfd, (uint64_t) rel->r_info, o);
11145 bfd_set_error (bfd_error_bad_value);
11146 return FALSE;
11147 }
11148
11149 while (h->root.type == bfd_link_hash_indirect
11150 || h->root.type == bfd_link_hash_warning)
11151 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11152
11153 s_type = h->type;
11154
11155 /* If a plugin symbol is referenced from a non-IR file,
11156 mark the symbol as undefined. Note that the
11157 linker may attach linker created dynamic sections
11158 to the plugin bfd. Symbols defined in linker
11159 created sections are not plugin symbols. */
11160 if ((h->root.non_ir_ref_regular
11161 || h->root.non_ir_ref_dynamic)
11162 && (h->root.type == bfd_link_hash_defined
11163 || h->root.type == bfd_link_hash_defweak)
11164 && (h->root.u.def.section->flags
11165 & SEC_LINKER_CREATED) == 0
11166 && h->root.u.def.section->owner != NULL
11167 && (h->root.u.def.section->owner->flags
11168 & BFD_PLUGIN) != 0)
11169 {
11170 h->root.type = bfd_link_hash_undefined;
11171 h->root.u.undef.abfd = h->root.u.def.section->owner;
11172 }
11173
11174 ps = NULL;
11175 if (h->root.type == bfd_link_hash_defined
11176 || h->root.type == bfd_link_hash_defweak)
11177 ps = &h->root.u.def.section;
11178
11179 sym_name = h->root.root.string;
11180 }
11181 else
11182 {
11183 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11184
11185 s_type = ELF_ST_TYPE (sym->st_info);
11186 ps = &flinfo->sections[r_symndx];
11187 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11188 sym, *ps);
11189 }
11190
11191 if ((s_type == STT_RELC || s_type == STT_SRELC)
11192 && !bfd_link_relocatable (flinfo->info))
11193 {
11194 bfd_vma val;
11195 bfd_vma dot = (rel->r_offset
11196 + o->output_offset + o->output_section->vma);
11197 #ifdef DEBUG
11198 printf ("Encountered a complex symbol!");
11199 printf (" (input_bfd %s, section %s, reloc %ld\n",
11200 bfd_get_filename (input_bfd), o->name,
11201 (long) (rel - internal_relocs));
11202 printf (" symbol: idx %8.8lx, name %s\n",
11203 r_symndx, sym_name);
11204 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11205 (unsigned long) rel->r_info,
11206 (unsigned long) rel->r_offset);
11207 #endif
11208 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11209 isymbuf, locsymcount, s_type == STT_SRELC))
11210 return FALSE;
11211
11212 /* Symbol evaluated OK. Update to absolute value. */
11213 set_symbol_value (input_bfd, isymbuf, locsymcount,
11214 r_symndx, val);
11215 continue;
11216 }
11217
11218 if (action_discarded != -1 && ps != NULL)
11219 {
11220 /* Complain if the definition comes from a
11221 discarded section. */
11222 if ((sec = *ps) != NULL && discarded_section (sec))
11223 {
11224 BFD_ASSERT (r_symndx != STN_UNDEF);
11225 if (action_discarded & COMPLAIN)
11226 (*flinfo->info->callbacks->einfo)
11227 /* xgettext:c-format */
11228 (_("%X`%s' referenced in section `%pA' of %pB: "
11229 "defined in discarded section `%pA' of %pB\n"),
11230 sym_name, o, input_bfd, sec, sec->owner);
11231
11232 /* Try to do the best we can to support buggy old
11233 versions of gcc. Pretend that the symbol is
11234 really defined in the kept linkonce section.
11235 FIXME: This is quite broken. Modifying the
11236 symbol here means we will be changing all later
11237 uses of the symbol, not just in this section. */
11238 if (action_discarded & PRETEND)
11239 {
11240 asection *kept;
11241
11242 kept = _bfd_elf_check_kept_section (sec,
11243 flinfo->info);
11244 if (kept != NULL)
11245 {
11246 *ps = kept;
11247 continue;
11248 }
11249 }
11250 }
11251 }
11252 }
11253
11254 /* Relocate the section by invoking a back end routine.
11255
11256 The back end routine is responsible for adjusting the
11257 section contents as necessary, and (if using Rela relocs
11258 and generating a relocatable output file) adjusting the
11259 reloc addend as necessary.
11260
11261 The back end routine does not have to worry about setting
11262 the reloc address or the reloc symbol index.
11263
11264 The back end routine is given a pointer to the swapped in
11265 internal symbols, and can access the hash table entries
11266 for the external symbols via elf_sym_hashes (input_bfd).
11267
11268 When generating relocatable output, the back end routine
11269 must handle STB_LOCAL/STT_SECTION symbols specially. The
11270 output symbol is going to be a section symbol
11271 corresponding to the output section, which will require
11272 the addend to be adjusted. */
11273
11274 ret = (*relocate_section) (output_bfd, flinfo->info,
11275 input_bfd, o, contents,
11276 internal_relocs,
11277 isymbuf,
11278 flinfo->sections);
11279 if (!ret)
11280 return FALSE;
11281
11282 if (ret == 2
11283 || bfd_link_relocatable (flinfo->info)
11284 || flinfo->info->emitrelocations)
11285 {
11286 Elf_Internal_Rela *irela;
11287 Elf_Internal_Rela *irelaend, *irelamid;
11288 bfd_vma last_offset;
11289 struct elf_link_hash_entry **rel_hash;
11290 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11291 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11292 unsigned int next_erel;
11293 bfd_boolean rela_normal;
11294 struct bfd_elf_section_data *esdi, *esdo;
11295
11296 esdi = elf_section_data (o);
11297 esdo = elf_section_data (o->output_section);
11298 rela_normal = FALSE;
11299
11300 /* Adjust the reloc addresses and symbol indices. */
11301
11302 irela = internal_relocs;
11303 irelaend = irela + o->reloc_count;
11304 rel_hash = esdo->rel.hashes + esdo->rel.count;
11305 /* We start processing the REL relocs, if any. When we reach
11306 IRELAMID in the loop, we switch to the RELA relocs. */
11307 irelamid = irela;
11308 if (esdi->rel.hdr != NULL)
11309 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11310 * bed->s->int_rels_per_ext_rel);
11311 rel_hash_list = rel_hash;
11312 rela_hash_list = NULL;
11313 last_offset = o->output_offset;
11314 if (!bfd_link_relocatable (flinfo->info))
11315 last_offset += o->output_section->vma;
11316 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11317 {
11318 unsigned long r_symndx;
11319 asection *sec;
11320 Elf_Internal_Sym sym;
11321
11322 if (next_erel == bed->s->int_rels_per_ext_rel)
11323 {
11324 rel_hash++;
11325 next_erel = 0;
11326 }
11327
11328 if (irela == irelamid)
11329 {
11330 rel_hash = esdo->rela.hashes + esdo->rela.count;
11331 rela_hash_list = rel_hash;
11332 rela_normal = bed->rela_normal;
11333 }
11334
11335 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11336 flinfo->info, o,
11337 irela->r_offset);
11338 if (irela->r_offset >= (bfd_vma) -2)
11339 {
11340 /* This is a reloc for a deleted entry or somesuch.
11341 Turn it into an R_*_NONE reloc, at the same
11342 offset as the last reloc. elf_eh_frame.c and
11343 bfd_elf_discard_info rely on reloc offsets
11344 being ordered. */
11345 irela->r_offset = last_offset;
11346 irela->r_info = 0;
11347 irela->r_addend = 0;
11348 continue;
11349 }
11350
11351 irela->r_offset += o->output_offset;
11352
11353 /* Relocs in an executable have to be virtual addresses. */
11354 if (!bfd_link_relocatable (flinfo->info))
11355 irela->r_offset += o->output_section->vma;
11356
11357 last_offset = irela->r_offset;
11358
11359 r_symndx = irela->r_info >> r_sym_shift;
11360 if (r_symndx == STN_UNDEF)
11361 continue;
11362
11363 if (r_symndx >= locsymcount
11364 || (elf_bad_symtab (input_bfd)
11365 && flinfo->sections[r_symndx] == NULL))
11366 {
11367 struct elf_link_hash_entry *rh;
11368 unsigned long indx;
11369
11370 /* This is a reloc against a global symbol. We
11371 have not yet output all the local symbols, so
11372 we do not know the symbol index of any global
11373 symbol. We set the rel_hash entry for this
11374 reloc to point to the global hash table entry
11375 for this symbol. The symbol index is then
11376 set at the end of bfd_elf_final_link. */
11377 indx = r_symndx - extsymoff;
11378 rh = elf_sym_hashes (input_bfd)[indx];
11379 while (rh->root.type == bfd_link_hash_indirect
11380 || rh->root.type == bfd_link_hash_warning)
11381 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11382
11383 /* Setting the index to -2 tells
11384 elf_link_output_extsym that this symbol is
11385 used by a reloc. */
11386 BFD_ASSERT (rh->indx < 0);
11387 rh->indx = -2;
11388 *rel_hash = rh;
11389
11390 continue;
11391 }
11392
11393 /* This is a reloc against a local symbol. */
11394
11395 *rel_hash = NULL;
11396 sym = isymbuf[r_symndx];
11397 sec = flinfo->sections[r_symndx];
11398 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11399 {
11400 /* I suppose the backend ought to fill in the
11401 section of any STT_SECTION symbol against a
11402 processor specific section. */
11403 r_symndx = STN_UNDEF;
11404 if (bfd_is_abs_section (sec))
11405 ;
11406 else if (sec == NULL || sec->owner == NULL)
11407 {
11408 bfd_set_error (bfd_error_bad_value);
11409 return FALSE;
11410 }
11411 else
11412 {
11413 asection *osec = sec->output_section;
11414
11415 /* If we have discarded a section, the output
11416 section will be the absolute section. In
11417 case of discarded SEC_MERGE sections, use
11418 the kept section. relocate_section should
11419 have already handled discarded linkonce
11420 sections. */
11421 if (bfd_is_abs_section (osec)
11422 && sec->kept_section != NULL
11423 && sec->kept_section->output_section != NULL)
11424 {
11425 osec = sec->kept_section->output_section;
11426 irela->r_addend -= osec->vma;
11427 }
11428
11429 if (!bfd_is_abs_section (osec))
11430 {
11431 r_symndx = osec->target_index;
11432 if (r_symndx == STN_UNDEF)
11433 {
11434 irela->r_addend += osec->vma;
11435 osec = _bfd_nearby_section (output_bfd, osec,
11436 osec->vma);
11437 irela->r_addend -= osec->vma;
11438 r_symndx = osec->target_index;
11439 }
11440 }
11441 }
11442
11443 /* Adjust the addend according to where the
11444 section winds up in the output section. */
11445 if (rela_normal)
11446 irela->r_addend += sec->output_offset;
11447 }
11448 else
11449 {
11450 if (flinfo->indices[r_symndx] == -1)
11451 {
11452 unsigned long shlink;
11453 const char *name;
11454 asection *osec;
11455 long indx;
11456
11457 if (flinfo->info->strip == strip_all)
11458 {
11459 /* You can't do ld -r -s. */
11460 bfd_set_error (bfd_error_invalid_operation);
11461 return FALSE;
11462 }
11463
11464 /* This symbol was skipped earlier, but
11465 since it is needed by a reloc, we
11466 must output it now. */
11467 shlink = symtab_hdr->sh_link;
11468 name = (bfd_elf_string_from_elf_section
11469 (input_bfd, shlink, sym.st_name));
11470 if (name == NULL)
11471 return FALSE;
11472
11473 osec = sec->output_section;
11474 sym.st_shndx =
11475 _bfd_elf_section_from_bfd_section (output_bfd,
11476 osec);
11477 if (sym.st_shndx == SHN_BAD)
11478 return FALSE;
11479
11480 sym.st_value += sec->output_offset;
11481 if (!bfd_link_relocatable (flinfo->info))
11482 {
11483 sym.st_value += osec->vma;
11484 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11485 {
11486 struct elf_link_hash_table *htab
11487 = elf_hash_table (flinfo->info);
11488
11489 /* STT_TLS symbols are relative to PT_TLS
11490 segment base. */
11491 if (htab->tls_sec != NULL)
11492 sym.st_value -= htab->tls_sec->vma;
11493 else
11494 sym.st_info
11495 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11496 STT_NOTYPE);
11497 }
11498 }
11499
11500 indx = bfd_get_symcount (output_bfd);
11501 ret = elf_link_output_symstrtab (flinfo, name,
11502 &sym, sec,
11503 NULL);
11504 if (ret == 0)
11505 return FALSE;
11506 else if (ret == 1)
11507 flinfo->indices[r_symndx] = indx;
11508 else
11509 abort ();
11510 }
11511
11512 r_symndx = flinfo->indices[r_symndx];
11513 }
11514
11515 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11516 | (irela->r_info & r_type_mask));
11517 }
11518
11519 /* Swap out the relocs. */
11520 input_rel_hdr = esdi->rel.hdr;
11521 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11522 {
11523 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11524 input_rel_hdr,
11525 internal_relocs,
11526 rel_hash_list))
11527 return FALSE;
11528 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11529 * bed->s->int_rels_per_ext_rel);
11530 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11531 }
11532
11533 input_rela_hdr = esdi->rela.hdr;
11534 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11535 {
11536 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11537 input_rela_hdr,
11538 internal_relocs,
11539 rela_hash_list))
11540 return FALSE;
11541 }
11542 }
11543 }
11544
11545 /* Write out the modified section contents. */
11546 if (bed->elf_backend_write_section
11547 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11548 contents))
11549 {
11550 /* Section written out. */
11551 }
11552 else switch (o->sec_info_type)
11553 {
11554 case SEC_INFO_TYPE_STABS:
11555 if (! (_bfd_write_section_stabs
11556 (output_bfd,
11557 &elf_hash_table (flinfo->info)->stab_info,
11558 o, &elf_section_data (o)->sec_info, contents)))
11559 return FALSE;
11560 break;
11561 case SEC_INFO_TYPE_MERGE:
11562 if (! _bfd_write_merged_section (output_bfd, o,
11563 elf_section_data (o)->sec_info))
11564 return FALSE;
11565 break;
11566 case SEC_INFO_TYPE_EH_FRAME:
11567 {
11568 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11569 o, contents))
11570 return FALSE;
11571 }
11572 break;
11573 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11574 {
11575 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11576 flinfo->info,
11577 o, contents))
11578 return FALSE;
11579 }
11580 break;
11581 default:
11582 {
11583 if (! (o->flags & SEC_EXCLUDE))
11584 {
11585 file_ptr offset = (file_ptr) o->output_offset;
11586 bfd_size_type todo = o->size;
11587
11588 offset *= bfd_octets_per_byte (output_bfd, o);
11589
11590 if ((o->flags & SEC_ELF_REVERSE_COPY))
11591 {
11592 /* Reverse-copy input section to output. */
11593 do
11594 {
11595 todo -= address_size;
11596 if (! bfd_set_section_contents (output_bfd,
11597 o->output_section,
11598 contents + todo,
11599 offset,
11600 address_size))
11601 return FALSE;
11602 if (todo == 0)
11603 break;
11604 offset += address_size;
11605 }
11606 while (1);
11607 }
11608 else if (! bfd_set_section_contents (output_bfd,
11609 o->output_section,
11610 contents,
11611 offset, todo))
11612 return FALSE;
11613 }
11614 }
11615 break;
11616 }
11617 }
11618
11619 return TRUE;
11620 }
11621
11622 /* Generate a reloc when linking an ELF file. This is a reloc
11623 requested by the linker, and does not come from any input file. This
11624 is used to build constructor and destructor tables when linking
11625 with -Ur. */
11626
11627 static bfd_boolean
11628 elf_reloc_link_order (bfd *output_bfd,
11629 struct bfd_link_info *info,
11630 asection *output_section,
11631 struct bfd_link_order *link_order)
11632 {
11633 reloc_howto_type *howto;
11634 long indx;
11635 bfd_vma offset;
11636 bfd_vma addend;
11637 struct bfd_elf_section_reloc_data *reldata;
11638 struct elf_link_hash_entry **rel_hash_ptr;
11639 Elf_Internal_Shdr *rel_hdr;
11640 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11641 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11642 bfd_byte *erel;
11643 unsigned int i;
11644 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11645
11646 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11647 if (howto == NULL)
11648 {
11649 bfd_set_error (bfd_error_bad_value);
11650 return FALSE;
11651 }
11652
11653 addend = link_order->u.reloc.p->addend;
11654
11655 if (esdo->rel.hdr)
11656 reldata = &esdo->rel;
11657 else if (esdo->rela.hdr)
11658 reldata = &esdo->rela;
11659 else
11660 {
11661 reldata = NULL;
11662 BFD_ASSERT (0);
11663 }
11664
11665 /* Figure out the symbol index. */
11666 rel_hash_ptr = reldata->hashes + reldata->count;
11667 if (link_order->type == bfd_section_reloc_link_order)
11668 {
11669 indx = link_order->u.reloc.p->u.section->target_index;
11670 BFD_ASSERT (indx != 0);
11671 *rel_hash_ptr = NULL;
11672 }
11673 else
11674 {
11675 struct elf_link_hash_entry *h;
11676
11677 /* Treat a reloc against a defined symbol as though it were
11678 actually against the section. */
11679 h = ((struct elf_link_hash_entry *)
11680 bfd_wrapped_link_hash_lookup (output_bfd, info,
11681 link_order->u.reloc.p->u.name,
11682 FALSE, FALSE, TRUE));
11683 if (h != NULL
11684 && (h->root.type == bfd_link_hash_defined
11685 || h->root.type == bfd_link_hash_defweak))
11686 {
11687 asection *section;
11688
11689 section = h->root.u.def.section;
11690 indx = section->output_section->target_index;
11691 *rel_hash_ptr = NULL;
11692 /* It seems that we ought to add the symbol value to the
11693 addend here, but in practice it has already been added
11694 because it was passed to constructor_callback. */
11695 addend += section->output_section->vma + section->output_offset;
11696 }
11697 else if (h != NULL)
11698 {
11699 /* Setting the index to -2 tells elf_link_output_extsym that
11700 this symbol is used by a reloc. */
11701 h->indx = -2;
11702 *rel_hash_ptr = h;
11703 indx = 0;
11704 }
11705 else
11706 {
11707 (*info->callbacks->unattached_reloc)
11708 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11709 indx = 0;
11710 }
11711 }
11712
11713 /* If this is an inplace reloc, we must write the addend into the
11714 object file. */
11715 if (howto->partial_inplace && addend != 0)
11716 {
11717 bfd_size_type size;
11718 bfd_reloc_status_type rstat;
11719 bfd_byte *buf;
11720 bfd_boolean ok;
11721 const char *sym_name;
11722 bfd_size_type octets;
11723
11724 size = (bfd_size_type) bfd_get_reloc_size (howto);
11725 buf = (bfd_byte *) bfd_zmalloc (size);
11726 if (buf == NULL && size != 0)
11727 return FALSE;
11728 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11729 switch (rstat)
11730 {
11731 case bfd_reloc_ok:
11732 break;
11733
11734 default:
11735 case bfd_reloc_outofrange:
11736 abort ();
11737
11738 case bfd_reloc_overflow:
11739 if (link_order->type == bfd_section_reloc_link_order)
11740 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11741 else
11742 sym_name = link_order->u.reloc.p->u.name;
11743 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11744 howto->name, addend, NULL, NULL,
11745 (bfd_vma) 0);
11746 break;
11747 }
11748
11749 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11750 output_section);
11751 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11752 octets, size);
11753 free (buf);
11754 if (! ok)
11755 return FALSE;
11756 }
11757
11758 /* The address of a reloc is relative to the section in a
11759 relocatable file, and is a virtual address in an executable
11760 file. */
11761 offset = link_order->offset;
11762 if (! bfd_link_relocatable (info))
11763 offset += output_section->vma;
11764
11765 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11766 {
11767 irel[i].r_offset = offset;
11768 irel[i].r_info = 0;
11769 irel[i].r_addend = 0;
11770 }
11771 if (bed->s->arch_size == 32)
11772 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11773 else
11774 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11775
11776 rel_hdr = reldata->hdr;
11777 erel = rel_hdr->contents;
11778 if (rel_hdr->sh_type == SHT_REL)
11779 {
11780 erel += reldata->count * bed->s->sizeof_rel;
11781 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11782 }
11783 else
11784 {
11785 irel[0].r_addend = addend;
11786 erel += reldata->count * bed->s->sizeof_rela;
11787 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11788 }
11789
11790 ++reldata->count;
11791
11792 return TRUE;
11793 }
11794
11795
11796 /* Compare two sections based on the locations of the sections they are
11797 linked to. Used by elf_fixup_link_order. */
11798
11799 static int
11800 compare_link_order (const void *a, const void *b)
11801 {
11802 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11803 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11804 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11805 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11806 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11807 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11808
11809 if (apos < bpos)
11810 return -1;
11811 if (apos > bpos)
11812 return 1;
11813
11814 /* The only way we should get matching LMAs is when the first of two
11815 sections has zero size. */
11816 if (asec->size < bsec->size)
11817 return -1;
11818 if (asec->size > bsec->size)
11819 return 1;
11820
11821 /* If they are both zero size then they almost certainly have the same
11822 VMA and thus are not ordered with respect to each other. Test VMA
11823 anyway, and fall back to id to make the result reproducible across
11824 qsort implementations. */
11825 apos = asec->output_section->vma + asec->output_offset;
11826 bpos = bsec->output_section->vma + bsec->output_offset;
11827 if (apos < bpos)
11828 return -1;
11829 if (apos > bpos)
11830 return 1;
11831
11832 return asec->id - bsec->id;
11833 }
11834
11835
11836 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11837 order as their linked sections. Returns false if this could not be done
11838 because an output section includes both ordered and unordered
11839 sections. Ideally we'd do this in the linker proper. */
11840
11841 static bfd_boolean
11842 elf_fixup_link_order (bfd *abfd, asection *o)
11843 {
11844 size_t seen_linkorder;
11845 size_t seen_other;
11846 size_t n;
11847 struct bfd_link_order *p;
11848 bfd *sub;
11849 struct bfd_link_order **sections;
11850 asection *other_sec, *linkorder_sec;
11851 bfd_vma offset; /* Octets. */
11852
11853 other_sec = NULL;
11854 linkorder_sec = NULL;
11855 seen_other = 0;
11856 seen_linkorder = 0;
11857 for (p = o->map_head.link_order; p != NULL; p = p->next)
11858 {
11859 if (p->type == bfd_indirect_link_order)
11860 {
11861 asection *s = p->u.indirect.section;
11862 sub = s->owner;
11863 if ((s->flags & SEC_LINKER_CREATED) == 0
11864 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11865 && elf_section_data (s) != NULL
11866 && elf_linked_to_section (s) != NULL)
11867 {
11868 seen_linkorder++;
11869 linkorder_sec = s;
11870 }
11871 else
11872 {
11873 seen_other++;
11874 other_sec = s;
11875 }
11876 }
11877 else
11878 seen_other++;
11879
11880 if (seen_other && seen_linkorder)
11881 {
11882 if (other_sec && linkorder_sec)
11883 _bfd_error_handler
11884 /* xgettext:c-format */
11885 (_("%pA has both ordered [`%pA' in %pB] "
11886 "and unordered [`%pA' in %pB] sections"),
11887 o, linkorder_sec, linkorder_sec->owner,
11888 other_sec, other_sec->owner);
11889 else
11890 _bfd_error_handler
11891 (_("%pA has both ordered and unordered sections"), o);
11892 bfd_set_error (bfd_error_bad_value);
11893 return FALSE;
11894 }
11895 }
11896
11897 if (!seen_linkorder)
11898 return TRUE;
11899
11900 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11901 if (sections == NULL)
11902 return FALSE;
11903
11904 seen_linkorder = 0;
11905 for (p = o->map_head.link_order; p != NULL; p = p->next)
11906 sections[seen_linkorder++] = p;
11907
11908 /* Sort the input sections in the order of their linked section. */
11909 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11910
11911 /* Change the offsets of the sections. */
11912 offset = 0;
11913 for (n = 0; n < seen_linkorder; n++)
11914 {
11915 bfd_vma mask;
11916 asection *s = sections[n]->u.indirect.section;
11917 unsigned int opb = bfd_octets_per_byte (abfd, s);
11918
11919 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11920 offset = (offset + ~mask) & mask;
11921 sections[n]->offset = s->output_offset = offset / opb;
11922 offset += sections[n]->size;
11923 }
11924
11925 free (sections);
11926 return TRUE;
11927 }
11928
11929 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11930 Returns TRUE upon success, FALSE otherwise. */
11931
11932 static bfd_boolean
11933 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11934 {
11935 bfd_boolean ret = FALSE;
11936 bfd *implib_bfd;
11937 const struct elf_backend_data *bed;
11938 flagword flags;
11939 enum bfd_architecture arch;
11940 unsigned int mach;
11941 asymbol **sympp = NULL;
11942 long symsize;
11943 long symcount;
11944 long src_count;
11945 elf_symbol_type *osymbuf;
11946 size_t amt;
11947
11948 implib_bfd = info->out_implib_bfd;
11949 bed = get_elf_backend_data (abfd);
11950
11951 if (!bfd_set_format (implib_bfd, bfd_object))
11952 return FALSE;
11953
11954 /* Use flag from executable but make it a relocatable object. */
11955 flags = bfd_get_file_flags (abfd);
11956 flags &= ~HAS_RELOC;
11957 if (!bfd_set_start_address (implib_bfd, 0)
11958 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11959 return FALSE;
11960
11961 /* Copy architecture of output file to import library file. */
11962 arch = bfd_get_arch (abfd);
11963 mach = bfd_get_mach (abfd);
11964 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11965 && (abfd->target_defaulted
11966 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11967 return FALSE;
11968
11969 /* Get symbol table size. */
11970 symsize = bfd_get_symtab_upper_bound (abfd);
11971 if (symsize < 0)
11972 return FALSE;
11973
11974 /* Read in the symbol table. */
11975 sympp = (asymbol **) bfd_malloc (symsize);
11976 if (sympp == NULL)
11977 return FALSE;
11978
11979 symcount = bfd_canonicalize_symtab (abfd, sympp);
11980 if (symcount < 0)
11981 goto free_sym_buf;
11982
11983 /* Allow the BFD backend to copy any private header data it
11984 understands from the output BFD to the import library BFD. */
11985 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11986 goto free_sym_buf;
11987
11988 /* Filter symbols to appear in the import library. */
11989 if (bed->elf_backend_filter_implib_symbols)
11990 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11991 symcount);
11992 else
11993 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11994 if (symcount == 0)
11995 {
11996 bfd_set_error (bfd_error_no_symbols);
11997 _bfd_error_handler (_("%pB: no symbol found for import library"),
11998 implib_bfd);
11999 goto free_sym_buf;
12000 }
12001
12002
12003 /* Make symbols absolute. */
12004 amt = symcount * sizeof (*osymbuf);
12005 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12006 if (osymbuf == NULL)
12007 goto free_sym_buf;
12008
12009 for (src_count = 0; src_count < symcount; src_count++)
12010 {
12011 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12012 sizeof (*osymbuf));
12013 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12014 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12015 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12016 osymbuf[src_count].internal_elf_sym.st_value =
12017 osymbuf[src_count].symbol.value;
12018 sympp[src_count] = &osymbuf[src_count].symbol;
12019 }
12020
12021 bfd_set_symtab (implib_bfd, sympp, symcount);
12022
12023 /* Allow the BFD backend to copy any private data it understands
12024 from the output BFD to the import library BFD. This is done last
12025 to permit the routine to look at the filtered symbol table. */
12026 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12027 goto free_sym_buf;
12028
12029 if (!bfd_close (implib_bfd))
12030 goto free_sym_buf;
12031
12032 ret = TRUE;
12033
12034 free_sym_buf:
12035 free (sympp);
12036 return ret;
12037 }
12038
12039 static void
12040 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12041 {
12042 asection *o;
12043
12044 if (flinfo->symstrtab != NULL)
12045 _bfd_elf_strtab_free (flinfo->symstrtab);
12046 free (flinfo->contents);
12047 free (flinfo->external_relocs);
12048 free (flinfo->internal_relocs);
12049 free (flinfo->external_syms);
12050 free (flinfo->locsym_shndx);
12051 free (flinfo->internal_syms);
12052 free (flinfo->indices);
12053 free (flinfo->sections);
12054 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12055 free (flinfo->symshndxbuf);
12056 for (o = obfd->sections; o != NULL; o = o->next)
12057 {
12058 struct bfd_elf_section_data *esdo = elf_section_data (o);
12059 free (esdo->rel.hashes);
12060 free (esdo->rela.hashes);
12061 }
12062 }
12063
12064 /* Do the final step of an ELF link. */
12065
12066 bfd_boolean
12067 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12068 {
12069 bfd_boolean dynamic;
12070 bfd_boolean emit_relocs;
12071 bfd *dynobj;
12072 struct elf_final_link_info flinfo;
12073 asection *o;
12074 struct bfd_link_order *p;
12075 bfd *sub;
12076 bfd_size_type max_contents_size;
12077 bfd_size_type max_external_reloc_size;
12078 bfd_size_type max_internal_reloc_count;
12079 bfd_size_type max_sym_count;
12080 bfd_size_type max_sym_shndx_count;
12081 Elf_Internal_Sym elfsym;
12082 unsigned int i;
12083 Elf_Internal_Shdr *symtab_hdr;
12084 Elf_Internal_Shdr *symtab_shndx_hdr;
12085 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12086 struct elf_outext_info eoinfo;
12087 bfd_boolean merged;
12088 size_t relativecount = 0;
12089 asection *reldyn = 0;
12090 bfd_size_type amt;
12091 asection *attr_section = NULL;
12092 bfd_vma attr_size = 0;
12093 const char *std_attrs_section;
12094 struct elf_link_hash_table *htab = elf_hash_table (info);
12095 bfd_boolean sections_removed;
12096 bfd_boolean ret;
12097
12098 if (!is_elf_hash_table (htab))
12099 return FALSE;
12100
12101 if (bfd_link_pic (info))
12102 abfd->flags |= DYNAMIC;
12103
12104 dynamic = htab->dynamic_sections_created;
12105 dynobj = htab->dynobj;
12106
12107 emit_relocs = (bfd_link_relocatable (info)
12108 || info->emitrelocations);
12109
12110 memset (&flinfo, 0, sizeof (flinfo));
12111 flinfo.info = info;
12112 flinfo.output_bfd = abfd;
12113 flinfo.symstrtab = _bfd_elf_strtab_init ();
12114 if (flinfo.symstrtab == NULL)
12115 return FALSE;
12116
12117 if (! dynamic)
12118 {
12119 flinfo.hash_sec = NULL;
12120 flinfo.symver_sec = NULL;
12121 }
12122 else
12123 {
12124 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12125 /* Note that dynsym_sec can be NULL (on VMS). */
12126 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12127 /* Note that it is OK if symver_sec is NULL. */
12128 }
12129
12130 if (info->unique_symbol
12131 && !bfd_hash_table_init (&flinfo.local_hash_table,
12132 local_hash_newfunc,
12133 sizeof (struct local_hash_entry)))
12134 return FALSE;
12135
12136 /* The object attributes have been merged. Remove the input
12137 sections from the link, and set the contents of the output
12138 section. */
12139 sections_removed = FALSE;
12140 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12141 for (o = abfd->sections; o != NULL; o = o->next)
12142 {
12143 bfd_boolean remove_section = FALSE;
12144
12145 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12146 || strcmp (o->name, ".gnu.attributes") == 0)
12147 {
12148 for (p = o->map_head.link_order; p != NULL; p = p->next)
12149 {
12150 asection *input_section;
12151
12152 if (p->type != bfd_indirect_link_order)
12153 continue;
12154 input_section = p->u.indirect.section;
12155 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12156 elf_link_input_bfd ignores this section. */
12157 input_section->flags &= ~SEC_HAS_CONTENTS;
12158 }
12159
12160 attr_size = bfd_elf_obj_attr_size (abfd);
12161 bfd_set_section_size (o, attr_size);
12162 /* Skip this section later on. */
12163 o->map_head.link_order = NULL;
12164 if (attr_size)
12165 attr_section = o;
12166 else
12167 remove_section = TRUE;
12168 }
12169 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12170 {
12171 /* Remove empty group section from linker output. */
12172 remove_section = TRUE;
12173 }
12174 if (remove_section)
12175 {
12176 o->flags |= SEC_EXCLUDE;
12177 bfd_section_list_remove (abfd, o);
12178 abfd->section_count--;
12179 sections_removed = TRUE;
12180 }
12181 }
12182 if (sections_removed)
12183 _bfd_fix_excluded_sec_syms (abfd, info);
12184
12185 /* Count up the number of relocations we will output for each output
12186 section, so that we know the sizes of the reloc sections. We
12187 also figure out some maximum sizes. */
12188 max_contents_size = 0;
12189 max_external_reloc_size = 0;
12190 max_internal_reloc_count = 0;
12191 max_sym_count = 0;
12192 max_sym_shndx_count = 0;
12193 merged = FALSE;
12194 for (o = abfd->sections; o != NULL; o = o->next)
12195 {
12196 struct bfd_elf_section_data *esdo = elf_section_data (o);
12197 o->reloc_count = 0;
12198
12199 for (p = o->map_head.link_order; p != NULL; p = p->next)
12200 {
12201 unsigned int reloc_count = 0;
12202 unsigned int additional_reloc_count = 0;
12203 struct bfd_elf_section_data *esdi = NULL;
12204
12205 if (p->type == bfd_section_reloc_link_order
12206 || p->type == bfd_symbol_reloc_link_order)
12207 reloc_count = 1;
12208 else if (p->type == bfd_indirect_link_order)
12209 {
12210 asection *sec;
12211
12212 sec = p->u.indirect.section;
12213
12214 /* Mark all sections which are to be included in the
12215 link. This will normally be every section. We need
12216 to do this so that we can identify any sections which
12217 the linker has decided to not include. */
12218 sec->linker_mark = TRUE;
12219
12220 if (sec->flags & SEC_MERGE)
12221 merged = TRUE;
12222
12223 if (sec->rawsize > max_contents_size)
12224 max_contents_size = sec->rawsize;
12225 if (sec->size > max_contents_size)
12226 max_contents_size = sec->size;
12227
12228 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12229 && (sec->owner->flags & DYNAMIC) == 0)
12230 {
12231 size_t sym_count;
12232
12233 /* We are interested in just local symbols, not all
12234 symbols. */
12235 if (elf_bad_symtab (sec->owner))
12236 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12237 / bed->s->sizeof_sym);
12238 else
12239 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12240
12241 if (sym_count > max_sym_count)
12242 max_sym_count = sym_count;
12243
12244 if (sym_count > max_sym_shndx_count
12245 && elf_symtab_shndx_list (sec->owner) != NULL)
12246 max_sym_shndx_count = sym_count;
12247
12248 if (esdo->this_hdr.sh_type == SHT_REL
12249 || esdo->this_hdr.sh_type == SHT_RELA)
12250 /* Some backends use reloc_count in relocation sections
12251 to count particular types of relocs. Of course,
12252 reloc sections themselves can't have relocations. */
12253 ;
12254 else if (emit_relocs)
12255 {
12256 reloc_count = sec->reloc_count;
12257 if (bed->elf_backend_count_additional_relocs)
12258 {
12259 int c;
12260 c = (*bed->elf_backend_count_additional_relocs) (sec);
12261 additional_reloc_count += c;
12262 }
12263 }
12264 else if (bed->elf_backend_count_relocs)
12265 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12266
12267 esdi = elf_section_data (sec);
12268
12269 if ((sec->flags & SEC_RELOC) != 0)
12270 {
12271 size_t ext_size = 0;
12272
12273 if (esdi->rel.hdr != NULL)
12274 ext_size = esdi->rel.hdr->sh_size;
12275 if (esdi->rela.hdr != NULL)
12276 ext_size += esdi->rela.hdr->sh_size;
12277
12278 if (ext_size > max_external_reloc_size)
12279 max_external_reloc_size = ext_size;
12280 if (sec->reloc_count > max_internal_reloc_count)
12281 max_internal_reloc_count = sec->reloc_count;
12282 }
12283 }
12284 }
12285
12286 if (reloc_count == 0)
12287 continue;
12288
12289 reloc_count += additional_reloc_count;
12290 o->reloc_count += reloc_count;
12291
12292 if (p->type == bfd_indirect_link_order && emit_relocs)
12293 {
12294 if (esdi->rel.hdr)
12295 {
12296 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12297 esdo->rel.count += additional_reloc_count;
12298 }
12299 if (esdi->rela.hdr)
12300 {
12301 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12302 esdo->rela.count += additional_reloc_count;
12303 }
12304 }
12305 else
12306 {
12307 if (o->use_rela_p)
12308 esdo->rela.count += reloc_count;
12309 else
12310 esdo->rel.count += reloc_count;
12311 }
12312 }
12313
12314 if (o->reloc_count > 0)
12315 o->flags |= SEC_RELOC;
12316 else
12317 {
12318 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12319 set it (this is probably a bug) and if it is set
12320 assign_section_numbers will create a reloc section. */
12321 o->flags &=~ SEC_RELOC;
12322 }
12323
12324 /* If the SEC_ALLOC flag is not set, force the section VMA to
12325 zero. This is done in elf_fake_sections as well, but forcing
12326 the VMA to 0 here will ensure that relocs against these
12327 sections are handled correctly. */
12328 if ((o->flags & SEC_ALLOC) == 0
12329 && ! o->user_set_vma)
12330 o->vma = 0;
12331 }
12332
12333 if (! bfd_link_relocatable (info) && merged)
12334 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12335
12336 /* Figure out the file positions for everything but the symbol table
12337 and the relocs. We set symcount to force assign_section_numbers
12338 to create a symbol table. */
12339 abfd->symcount = info->strip != strip_all || emit_relocs;
12340 BFD_ASSERT (! abfd->output_has_begun);
12341 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12342 goto error_return;
12343
12344 /* Set sizes, and assign file positions for reloc sections. */
12345 for (o = abfd->sections; o != NULL; o = o->next)
12346 {
12347 struct bfd_elf_section_data *esdo = elf_section_data (o);
12348 if ((o->flags & SEC_RELOC) != 0)
12349 {
12350 if (esdo->rel.hdr
12351 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12352 goto error_return;
12353
12354 if (esdo->rela.hdr
12355 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12356 goto error_return;
12357 }
12358
12359 /* _bfd_elf_compute_section_file_positions makes temporary use
12360 of target_index. Reset it. */
12361 o->target_index = 0;
12362
12363 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12364 to count upwards while actually outputting the relocations. */
12365 esdo->rel.count = 0;
12366 esdo->rela.count = 0;
12367
12368 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12369 && !bfd_section_is_ctf (o))
12370 {
12371 /* Cache the section contents so that they can be compressed
12372 later. Use bfd_malloc since it will be freed by
12373 bfd_compress_section_contents. */
12374 unsigned char *contents = esdo->this_hdr.contents;
12375 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12376 abort ();
12377 contents
12378 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12379 if (contents == NULL)
12380 goto error_return;
12381 esdo->this_hdr.contents = contents;
12382 }
12383 }
12384
12385 /* We have now assigned file positions for all the sections except .symtab,
12386 .strtab, and non-loaded reloc and compressed debugging sections. We start
12387 the .symtab section at the current file position, and write directly to it.
12388 We build the .strtab section in memory. */
12389 abfd->symcount = 0;
12390 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12391 /* sh_name is set in prep_headers. */
12392 symtab_hdr->sh_type = SHT_SYMTAB;
12393 /* sh_flags, sh_addr and sh_size all start off zero. */
12394 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12395 /* sh_link is set in assign_section_numbers. */
12396 /* sh_info is set below. */
12397 /* sh_offset is set just below. */
12398 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12399
12400 if (max_sym_count < 20)
12401 max_sym_count = 20;
12402 htab->strtabsize = max_sym_count;
12403 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12404 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12405 if (htab->strtab == NULL)
12406 goto error_return;
12407 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12408 flinfo.symshndxbuf
12409 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12410 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12411
12412 if (info->strip != strip_all || emit_relocs)
12413 {
12414 bfd_boolean name_local_sections;
12415 const char *name;
12416
12417 file_ptr off = elf_next_file_pos (abfd);
12418
12419 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12420
12421 /* Note that at this point elf_next_file_pos (abfd) is
12422 incorrect. We do not yet know the size of the .symtab section.
12423 We correct next_file_pos below, after we do know the size. */
12424
12425 /* Start writing out the symbol table. The first symbol is always a
12426 dummy symbol. */
12427 elfsym.st_value = 0;
12428 elfsym.st_size = 0;
12429 elfsym.st_info = 0;
12430 elfsym.st_other = 0;
12431 elfsym.st_shndx = SHN_UNDEF;
12432 elfsym.st_target_internal = 0;
12433 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12434 bfd_und_section_ptr, NULL) != 1)
12435 goto error_return;
12436
12437 /* Output a symbol for each section. We output these even if we are
12438 discarding local symbols, since they are used for relocs. These
12439 symbols usually have no names. We store the index of each one in
12440 the index field of the section, so that we can find it again when
12441 outputting relocs. */
12442
12443 name_local_sections
12444 = (bed->elf_backend_name_local_section_symbols
12445 && bed->elf_backend_name_local_section_symbols (abfd));
12446
12447 name = NULL;
12448 elfsym.st_size = 0;
12449 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12450 elfsym.st_other = 0;
12451 elfsym.st_value = 0;
12452 elfsym.st_target_internal = 0;
12453 for (i = 1; i < elf_numsections (abfd); i++)
12454 {
12455 o = bfd_section_from_elf_index (abfd, i);
12456 if (o != NULL)
12457 {
12458 o->target_index = bfd_get_symcount (abfd);
12459 elfsym.st_shndx = i;
12460 if (!bfd_link_relocatable (info))
12461 elfsym.st_value = o->vma;
12462 if (name_local_sections)
12463 name = o->name;
12464 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12465 NULL) != 1)
12466 goto error_return;
12467 }
12468 }
12469 }
12470
12471 /* On some targets like Irix 5 the symbol split between local and global
12472 ones recorded in the sh_info field needs to be done between section
12473 and all other symbols. */
12474 if (bed->elf_backend_elfsym_local_is_section
12475 && bed->elf_backend_elfsym_local_is_section (abfd))
12476 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12477
12478 /* Allocate some memory to hold information read in from the input
12479 files. */
12480 if (max_contents_size != 0)
12481 {
12482 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12483 if (flinfo.contents == NULL)
12484 goto error_return;
12485 }
12486
12487 if (max_external_reloc_size != 0)
12488 {
12489 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12490 if (flinfo.external_relocs == NULL)
12491 goto error_return;
12492 }
12493
12494 if (max_internal_reloc_count != 0)
12495 {
12496 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12497 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12498 if (flinfo.internal_relocs == NULL)
12499 goto error_return;
12500 }
12501
12502 if (max_sym_count != 0)
12503 {
12504 amt = max_sym_count * bed->s->sizeof_sym;
12505 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12506 if (flinfo.external_syms == NULL)
12507 goto error_return;
12508
12509 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12510 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12511 if (flinfo.internal_syms == NULL)
12512 goto error_return;
12513
12514 amt = max_sym_count * sizeof (long);
12515 flinfo.indices = (long int *) bfd_malloc (amt);
12516 if (flinfo.indices == NULL)
12517 goto error_return;
12518
12519 amt = max_sym_count * sizeof (asection *);
12520 flinfo.sections = (asection **) bfd_malloc (amt);
12521 if (flinfo.sections == NULL)
12522 goto error_return;
12523 }
12524
12525 if (max_sym_shndx_count != 0)
12526 {
12527 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12528 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12529 if (flinfo.locsym_shndx == NULL)
12530 goto error_return;
12531 }
12532
12533 if (htab->tls_sec)
12534 {
12535 bfd_vma base, end = 0; /* Both bytes. */
12536 asection *sec;
12537
12538 for (sec = htab->tls_sec;
12539 sec && (sec->flags & SEC_THREAD_LOCAL);
12540 sec = sec->next)
12541 {
12542 bfd_size_type size = sec->size;
12543 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12544
12545 if (size == 0
12546 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12547 {
12548 struct bfd_link_order *ord = sec->map_tail.link_order;
12549
12550 if (ord != NULL)
12551 size = ord->offset * opb + ord->size;
12552 }
12553 end = sec->vma + size / opb;
12554 }
12555 base = htab->tls_sec->vma;
12556 /* Only align end of TLS section if static TLS doesn't have special
12557 alignment requirements. */
12558 if (bed->static_tls_alignment == 1)
12559 end = align_power (end, htab->tls_sec->alignment_power);
12560 htab->tls_size = end - base;
12561 }
12562
12563 /* Reorder SHF_LINK_ORDER sections. */
12564 for (o = abfd->sections; o != NULL; o = o->next)
12565 {
12566 if (!elf_fixup_link_order (abfd, o))
12567 return FALSE;
12568 }
12569
12570 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12571 return FALSE;
12572
12573 /* Since ELF permits relocations to be against local symbols, we
12574 must have the local symbols available when we do the relocations.
12575 Since we would rather only read the local symbols once, and we
12576 would rather not keep them in memory, we handle all the
12577 relocations for a single input file at the same time.
12578
12579 Unfortunately, there is no way to know the total number of local
12580 symbols until we have seen all of them, and the local symbol
12581 indices precede the global symbol indices. This means that when
12582 we are generating relocatable output, and we see a reloc against
12583 a global symbol, we can not know the symbol index until we have
12584 finished examining all the local symbols to see which ones we are
12585 going to output. To deal with this, we keep the relocations in
12586 memory, and don't output them until the end of the link. This is
12587 an unfortunate waste of memory, but I don't see a good way around
12588 it. Fortunately, it only happens when performing a relocatable
12589 link, which is not the common case. FIXME: If keep_memory is set
12590 we could write the relocs out and then read them again; I don't
12591 know how bad the memory loss will be. */
12592
12593 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12594 sub->output_has_begun = FALSE;
12595 for (o = abfd->sections; o != NULL; o = o->next)
12596 {
12597 for (p = o->map_head.link_order; p != NULL; p = p->next)
12598 {
12599 if (p->type == bfd_indirect_link_order
12600 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12601 == bfd_target_elf_flavour)
12602 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12603 {
12604 if (! sub->output_has_begun)
12605 {
12606 if (! elf_link_input_bfd (&flinfo, sub))
12607 goto error_return;
12608 sub->output_has_begun = TRUE;
12609 }
12610 }
12611 else if (p->type == bfd_section_reloc_link_order
12612 || p->type == bfd_symbol_reloc_link_order)
12613 {
12614 if (! elf_reloc_link_order (abfd, info, o, p))
12615 goto error_return;
12616 }
12617 else
12618 {
12619 if (! _bfd_default_link_order (abfd, info, o, p))
12620 {
12621 if (p->type == bfd_indirect_link_order
12622 && (bfd_get_flavour (sub)
12623 == bfd_target_elf_flavour)
12624 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12625 != bed->s->elfclass))
12626 {
12627 const char *iclass, *oclass;
12628
12629 switch (bed->s->elfclass)
12630 {
12631 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12632 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12633 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12634 default: abort ();
12635 }
12636
12637 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12638 {
12639 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12640 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12641 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12642 default: abort ();
12643 }
12644
12645 bfd_set_error (bfd_error_wrong_format);
12646 _bfd_error_handler
12647 /* xgettext:c-format */
12648 (_("%pB: file class %s incompatible with %s"),
12649 sub, iclass, oclass);
12650 }
12651
12652 goto error_return;
12653 }
12654 }
12655 }
12656 }
12657
12658 /* Free symbol buffer if needed. */
12659 if (!info->reduce_memory_overheads)
12660 {
12661 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12662 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12663 {
12664 free (elf_tdata (sub)->symbuf);
12665 elf_tdata (sub)->symbuf = NULL;
12666 }
12667 }
12668
12669 ret = TRUE;
12670
12671 /* Output any global symbols that got converted to local in a
12672 version script or due to symbol visibility. We do this in a
12673 separate step since ELF requires all local symbols to appear
12674 prior to any global symbols. FIXME: We should only do this if
12675 some global symbols were, in fact, converted to become local.
12676 FIXME: Will this work correctly with the Irix 5 linker? */
12677 eoinfo.failed = FALSE;
12678 eoinfo.flinfo = &flinfo;
12679 eoinfo.localsyms = TRUE;
12680 eoinfo.file_sym_done = FALSE;
12681 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12682 if (eoinfo.failed)
12683 {
12684 ret = FALSE;
12685 goto return_local_hash_table;
12686 }
12687
12688 /* If backend needs to output some local symbols not present in the hash
12689 table, do it now. */
12690 if (bed->elf_backend_output_arch_local_syms
12691 && (info->strip != strip_all || emit_relocs))
12692 {
12693 typedef int (*out_sym_func)
12694 (void *, const char *, Elf_Internal_Sym *, asection *,
12695 struct elf_link_hash_entry *);
12696
12697 if (! ((*bed->elf_backend_output_arch_local_syms)
12698 (abfd, info, &flinfo,
12699 (out_sym_func) elf_link_output_symstrtab)))
12700 {
12701 ret = FALSE;
12702 goto return_local_hash_table;
12703 }
12704 }
12705
12706 /* That wrote out all the local symbols. Finish up the symbol table
12707 with the global symbols. Even if we want to strip everything we
12708 can, we still need to deal with those global symbols that got
12709 converted to local in a version script. */
12710
12711 /* The sh_info field records the index of the first non local symbol. */
12712 if (!symtab_hdr->sh_info)
12713 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12714
12715 if (dynamic
12716 && htab->dynsym != NULL
12717 && htab->dynsym->output_section != bfd_abs_section_ptr)
12718 {
12719 Elf_Internal_Sym sym;
12720 bfd_byte *dynsym = htab->dynsym->contents;
12721
12722 o = htab->dynsym->output_section;
12723 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12724
12725 /* Write out the section symbols for the output sections. */
12726 if (bfd_link_pic (info)
12727 || htab->is_relocatable_executable)
12728 {
12729 asection *s;
12730
12731 sym.st_size = 0;
12732 sym.st_name = 0;
12733 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12734 sym.st_other = 0;
12735 sym.st_target_internal = 0;
12736
12737 for (s = abfd->sections; s != NULL; s = s->next)
12738 {
12739 int indx;
12740 bfd_byte *dest;
12741 long dynindx;
12742
12743 dynindx = elf_section_data (s)->dynindx;
12744 if (dynindx <= 0)
12745 continue;
12746 indx = elf_section_data (s)->this_idx;
12747 BFD_ASSERT (indx > 0);
12748 sym.st_shndx = indx;
12749 if (! check_dynsym (abfd, &sym))
12750 {
12751 ret = FALSE;
12752 goto return_local_hash_table;
12753 }
12754 sym.st_value = s->vma;
12755 dest = dynsym + dynindx * bed->s->sizeof_sym;
12756 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12757 }
12758 }
12759
12760 /* Write out the local dynsyms. */
12761 if (htab->dynlocal)
12762 {
12763 struct elf_link_local_dynamic_entry *e;
12764 for (e = htab->dynlocal; e ; e = e->next)
12765 {
12766 asection *s;
12767 bfd_byte *dest;
12768
12769 /* Copy the internal symbol and turn off visibility.
12770 Note that we saved a word of storage and overwrote
12771 the original st_name with the dynstr_index. */
12772 sym = e->isym;
12773 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12774 sym.st_shndx = SHN_UNDEF;
12775
12776 s = bfd_section_from_elf_index (e->input_bfd,
12777 e->isym.st_shndx);
12778 if (s != NULL
12779 && s->output_section != NULL
12780 && elf_section_data (s->output_section) != NULL)
12781 {
12782 sym.st_shndx =
12783 elf_section_data (s->output_section)->this_idx;
12784 if (! check_dynsym (abfd, &sym))
12785 {
12786 ret = FALSE;
12787 goto return_local_hash_table;
12788 }
12789 sym.st_value = (s->output_section->vma
12790 + s->output_offset
12791 + e->isym.st_value);
12792 }
12793
12794 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12795 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12796 }
12797 }
12798 }
12799
12800 /* We get the global symbols from the hash table. */
12801 eoinfo.failed = FALSE;
12802 eoinfo.localsyms = FALSE;
12803 eoinfo.flinfo = &flinfo;
12804 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12805 if (eoinfo.failed)
12806 {
12807 ret = FALSE;
12808 goto return_local_hash_table;
12809 }
12810
12811 /* If backend needs to output some symbols not present in the hash
12812 table, do it now. */
12813 if (bed->elf_backend_output_arch_syms
12814 && (info->strip != strip_all || emit_relocs))
12815 {
12816 typedef int (*out_sym_func)
12817 (void *, const char *, Elf_Internal_Sym *, asection *,
12818 struct elf_link_hash_entry *);
12819
12820 if (! ((*bed->elf_backend_output_arch_syms)
12821 (abfd, info, &flinfo,
12822 (out_sym_func) elf_link_output_symstrtab)))
12823 {
12824 ret = FALSE;
12825 goto return_local_hash_table;
12826 }
12827 }
12828
12829 /* Finalize the .strtab section. */
12830 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12831
12832 /* Swap out the .strtab section. */
12833 if (!elf_link_swap_symbols_out (&flinfo))
12834 {
12835 ret = FALSE;
12836 goto return_local_hash_table;
12837 }
12838
12839 /* Now we know the size of the symtab section. */
12840 if (bfd_get_symcount (abfd) > 0)
12841 {
12842 /* Finish up and write out the symbol string table (.strtab)
12843 section. */
12844 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12845 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12846
12847 if (elf_symtab_shndx_list (abfd))
12848 {
12849 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12850
12851 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12852 {
12853 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12854 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12855 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12856 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12857 symtab_shndx_hdr->sh_size = amt;
12858
12859 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12860 off, TRUE);
12861
12862 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12863 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12864 {
12865 ret = FALSE;
12866 goto return_local_hash_table;
12867 }
12868 }
12869 }
12870
12871 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12872 /* sh_name was set in prep_headers. */
12873 symstrtab_hdr->sh_type = SHT_STRTAB;
12874 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12875 symstrtab_hdr->sh_addr = 0;
12876 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12877 symstrtab_hdr->sh_entsize = 0;
12878 symstrtab_hdr->sh_link = 0;
12879 symstrtab_hdr->sh_info = 0;
12880 /* sh_offset is set just below. */
12881 symstrtab_hdr->sh_addralign = 1;
12882
12883 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12884 off, TRUE);
12885 elf_next_file_pos (abfd) = off;
12886
12887 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12888 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12889 {
12890 ret = FALSE;
12891 goto return_local_hash_table;
12892 }
12893 }
12894
12895 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12896 {
12897 _bfd_error_handler (_("%pB: failed to generate import library"),
12898 info->out_implib_bfd);
12899 ret = FALSE;
12900 goto return_local_hash_table;
12901 }
12902
12903 /* Adjust the relocs to have the correct symbol indices. */
12904 for (o = abfd->sections; o != NULL; o = o->next)
12905 {
12906 struct bfd_elf_section_data *esdo = elf_section_data (o);
12907 bfd_boolean sort;
12908
12909 if ((o->flags & SEC_RELOC) == 0)
12910 continue;
12911
12912 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12913 if (esdo->rel.hdr != NULL
12914 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12915 {
12916 ret = FALSE;
12917 goto return_local_hash_table;
12918 }
12919 if (esdo->rela.hdr != NULL
12920 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12921 {
12922 ret = FALSE;
12923 goto return_local_hash_table;
12924 }
12925
12926 /* Set the reloc_count field to 0 to prevent write_relocs from
12927 trying to swap the relocs out itself. */
12928 o->reloc_count = 0;
12929 }
12930
12931 if (dynamic && info->combreloc && dynobj != NULL)
12932 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12933
12934 /* If we are linking against a dynamic object, or generating a
12935 shared library, finish up the dynamic linking information. */
12936 if (dynamic)
12937 {
12938 bfd_byte *dyncon, *dynconend;
12939
12940 /* Fix up .dynamic entries. */
12941 o = bfd_get_linker_section (dynobj, ".dynamic");
12942 BFD_ASSERT (o != NULL);
12943
12944 dyncon = o->contents;
12945 dynconend = o->contents + o->size;
12946 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12947 {
12948 Elf_Internal_Dyn dyn;
12949 const char *name;
12950 unsigned int type;
12951 bfd_size_type sh_size;
12952 bfd_vma sh_addr;
12953
12954 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12955
12956 switch (dyn.d_tag)
12957 {
12958 default:
12959 continue;
12960 case DT_NULL:
12961 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12962 {
12963 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12964 {
12965 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12966 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12967 default: continue;
12968 }
12969 dyn.d_un.d_val = relativecount;
12970 relativecount = 0;
12971 break;
12972 }
12973 continue;
12974
12975 case DT_INIT:
12976 name = info->init_function;
12977 goto get_sym;
12978 case DT_FINI:
12979 name = info->fini_function;
12980 get_sym:
12981 {
12982 struct elf_link_hash_entry *h;
12983
12984 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12985 if (h != NULL
12986 && (h->root.type == bfd_link_hash_defined
12987 || h->root.type == bfd_link_hash_defweak))
12988 {
12989 dyn.d_un.d_ptr = h->root.u.def.value;
12990 o = h->root.u.def.section;
12991 if (o->output_section != NULL)
12992 dyn.d_un.d_ptr += (o->output_section->vma
12993 + o->output_offset);
12994 else
12995 {
12996 /* The symbol is imported from another shared
12997 library and does not apply to this one. */
12998 dyn.d_un.d_ptr = 0;
12999 }
13000 break;
13001 }
13002 }
13003 continue;
13004
13005 case DT_PREINIT_ARRAYSZ:
13006 name = ".preinit_array";
13007 goto get_out_size;
13008 case DT_INIT_ARRAYSZ:
13009 name = ".init_array";
13010 goto get_out_size;
13011 case DT_FINI_ARRAYSZ:
13012 name = ".fini_array";
13013 get_out_size:
13014 o = bfd_get_section_by_name (abfd, name);
13015 if (o == NULL)
13016 {
13017 _bfd_error_handler
13018 (_("could not find section %s"), name);
13019 goto error_return;
13020 }
13021 if (o->size == 0)
13022 _bfd_error_handler
13023 (_("warning: %s section has zero size"), name);
13024 dyn.d_un.d_val = o->size;
13025 break;
13026
13027 case DT_PREINIT_ARRAY:
13028 name = ".preinit_array";
13029 goto get_out_vma;
13030 case DT_INIT_ARRAY:
13031 name = ".init_array";
13032 goto get_out_vma;
13033 case DT_FINI_ARRAY:
13034 name = ".fini_array";
13035 get_out_vma:
13036 o = bfd_get_section_by_name (abfd, name);
13037 goto do_vma;
13038
13039 case DT_HASH:
13040 name = ".hash";
13041 goto get_vma;
13042 case DT_GNU_HASH:
13043 name = ".gnu.hash";
13044 goto get_vma;
13045 case DT_STRTAB:
13046 name = ".dynstr";
13047 goto get_vma;
13048 case DT_SYMTAB:
13049 name = ".dynsym";
13050 goto get_vma;
13051 case DT_VERDEF:
13052 name = ".gnu.version_d";
13053 goto get_vma;
13054 case DT_VERNEED:
13055 name = ".gnu.version_r";
13056 goto get_vma;
13057 case DT_VERSYM:
13058 name = ".gnu.version";
13059 get_vma:
13060 o = bfd_get_linker_section (dynobj, name);
13061 do_vma:
13062 if (o == NULL || bfd_is_abs_section (o->output_section))
13063 {
13064 _bfd_error_handler
13065 (_("could not find section %s"), name);
13066 goto error_return;
13067 }
13068 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13069 {
13070 _bfd_error_handler
13071 (_("warning: section '%s' is being made into a note"), name);
13072 bfd_set_error (bfd_error_nonrepresentable_section);
13073 goto error_return;
13074 }
13075 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13076 break;
13077
13078 case DT_REL:
13079 case DT_RELA:
13080 case DT_RELSZ:
13081 case DT_RELASZ:
13082 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13083 type = SHT_REL;
13084 else
13085 type = SHT_RELA;
13086 sh_size = 0;
13087 sh_addr = 0;
13088 for (i = 1; i < elf_numsections (abfd); i++)
13089 {
13090 Elf_Internal_Shdr *hdr;
13091
13092 hdr = elf_elfsections (abfd)[i];
13093 if (hdr->sh_type == type
13094 && (hdr->sh_flags & SHF_ALLOC) != 0)
13095 {
13096 sh_size += hdr->sh_size;
13097 if (sh_addr == 0
13098 || sh_addr > hdr->sh_addr)
13099 sh_addr = hdr->sh_addr;
13100 }
13101 }
13102
13103 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13104 {
13105 unsigned int opb = bfd_octets_per_byte (abfd, o);
13106
13107 /* Don't count procedure linkage table relocs in the
13108 overall reloc count. */
13109 sh_size -= htab->srelplt->size;
13110 if (sh_size == 0)
13111 /* If the size is zero, make the address zero too.
13112 This is to avoid a glibc bug. If the backend
13113 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13114 zero, then we'll put DT_RELA at the end of
13115 DT_JMPREL. glibc will interpret the end of
13116 DT_RELA matching the end of DT_JMPREL as the
13117 case where DT_RELA includes DT_JMPREL, and for
13118 LD_BIND_NOW will decide that processing DT_RELA
13119 will process the PLT relocs too. Net result:
13120 No PLT relocs applied. */
13121 sh_addr = 0;
13122
13123 /* If .rela.plt is the first .rela section, exclude
13124 it from DT_RELA. */
13125 else if (sh_addr == (htab->srelplt->output_section->vma
13126 + htab->srelplt->output_offset) * opb)
13127 sh_addr += htab->srelplt->size;
13128 }
13129
13130 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13131 dyn.d_un.d_val = sh_size;
13132 else
13133 dyn.d_un.d_ptr = sh_addr;
13134 break;
13135 }
13136 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13137 }
13138 }
13139
13140 /* If we have created any dynamic sections, then output them. */
13141 if (dynobj != NULL)
13142 {
13143 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13144 goto error_return;
13145
13146 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13147 if (bfd_link_textrel_check (info)
13148 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13149 {
13150 bfd_byte *dyncon, *dynconend;
13151
13152 dyncon = o->contents;
13153 dynconend = o->contents + o->size;
13154 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13155 {
13156 Elf_Internal_Dyn dyn;
13157
13158 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13159
13160 if (dyn.d_tag == DT_TEXTREL)
13161 {
13162 if (info->textrel_check == textrel_check_error)
13163 info->callbacks->einfo
13164 (_("%P%X: read-only segment has dynamic relocations\n"));
13165 else if (bfd_link_dll (info))
13166 info->callbacks->einfo
13167 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13168 else
13169 info->callbacks->einfo
13170 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13171 break;
13172 }
13173 }
13174 }
13175
13176 for (o = dynobj->sections; o != NULL; o = o->next)
13177 {
13178 if ((o->flags & SEC_HAS_CONTENTS) == 0
13179 || o->size == 0
13180 || o->output_section == bfd_abs_section_ptr)
13181 continue;
13182 if ((o->flags & SEC_LINKER_CREATED) == 0)
13183 {
13184 /* At this point, we are only interested in sections
13185 created by _bfd_elf_link_create_dynamic_sections. */
13186 continue;
13187 }
13188 if (htab->stab_info.stabstr == o)
13189 continue;
13190 if (htab->eh_info.hdr_sec == o)
13191 continue;
13192 if (strcmp (o->name, ".dynstr") != 0)
13193 {
13194 bfd_size_type octets = ((file_ptr) o->output_offset
13195 * bfd_octets_per_byte (abfd, o));
13196 if (!bfd_set_section_contents (abfd, o->output_section,
13197 o->contents, octets, o->size))
13198 goto error_return;
13199 }
13200 else
13201 {
13202 /* The contents of the .dynstr section are actually in a
13203 stringtab. */
13204 file_ptr off;
13205
13206 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13207 if (bfd_seek (abfd, off, SEEK_SET) != 0
13208 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13209 goto error_return;
13210 }
13211 }
13212 }
13213
13214 if (!info->resolve_section_groups)
13215 {
13216 bfd_boolean failed = FALSE;
13217
13218 BFD_ASSERT (bfd_link_relocatable (info));
13219 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13220 if (failed)
13221 goto error_return;
13222 }
13223
13224 /* If we have optimized stabs strings, output them. */
13225 if (htab->stab_info.stabstr != NULL)
13226 {
13227 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13228 goto error_return;
13229 }
13230
13231 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13232 goto error_return;
13233
13234 if (info->callbacks->emit_ctf)
13235 info->callbacks->emit_ctf ();
13236
13237 elf_final_link_free (abfd, &flinfo);
13238
13239 if (attr_section)
13240 {
13241 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13242 if (contents == NULL)
13243 {
13244 /* Bail out and fail. */
13245 ret = FALSE;
13246 goto return_local_hash_table;
13247 }
13248 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13249 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13250 free (contents);
13251 }
13252
13253 return_local_hash_table:
13254 if (info->unique_symbol)
13255 bfd_hash_table_free (&flinfo.local_hash_table);
13256 return ret;
13257
13258 error_return:
13259 elf_final_link_free (abfd, &flinfo);
13260 ret = FALSE;
13261 goto return_local_hash_table;
13262 }
13263 \f
13264 /* Initialize COOKIE for input bfd ABFD. */
13265
13266 static bfd_boolean
13267 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13268 struct bfd_link_info *info, bfd *abfd)
13269 {
13270 Elf_Internal_Shdr *symtab_hdr;
13271 const struct elf_backend_data *bed;
13272
13273 bed = get_elf_backend_data (abfd);
13274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13275
13276 cookie->abfd = abfd;
13277 cookie->sym_hashes = elf_sym_hashes (abfd);
13278 cookie->bad_symtab = elf_bad_symtab (abfd);
13279 if (cookie->bad_symtab)
13280 {
13281 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13282 cookie->extsymoff = 0;
13283 }
13284 else
13285 {
13286 cookie->locsymcount = symtab_hdr->sh_info;
13287 cookie->extsymoff = symtab_hdr->sh_info;
13288 }
13289
13290 if (bed->s->arch_size == 32)
13291 cookie->r_sym_shift = 8;
13292 else
13293 cookie->r_sym_shift = 32;
13294
13295 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13296 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13297 {
13298 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13299 cookie->locsymcount, 0,
13300 NULL, NULL, NULL);
13301 if (cookie->locsyms == NULL)
13302 {
13303 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13304 return FALSE;
13305 }
13306 if (info->keep_memory)
13307 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13308 }
13309 return TRUE;
13310 }
13311
13312 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13313
13314 static void
13315 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13316 {
13317 Elf_Internal_Shdr *symtab_hdr;
13318
13319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13320 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13321 free (cookie->locsyms);
13322 }
13323
13324 /* Initialize the relocation information in COOKIE for input section SEC
13325 of input bfd ABFD. */
13326
13327 static bfd_boolean
13328 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13329 struct bfd_link_info *info, bfd *abfd,
13330 asection *sec)
13331 {
13332 if (sec->reloc_count == 0)
13333 {
13334 cookie->rels = NULL;
13335 cookie->relend = NULL;
13336 }
13337 else
13338 {
13339 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13340 info->keep_memory);
13341 if (cookie->rels == NULL)
13342 return FALSE;
13343 cookie->rel = cookie->rels;
13344 cookie->relend = cookie->rels + sec->reloc_count;
13345 }
13346 cookie->rel = cookie->rels;
13347 return TRUE;
13348 }
13349
13350 /* Free the memory allocated by init_reloc_cookie_rels,
13351 if appropriate. */
13352
13353 static void
13354 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13355 asection *sec)
13356 {
13357 if (elf_section_data (sec)->relocs != cookie->rels)
13358 free (cookie->rels);
13359 }
13360
13361 /* Initialize the whole of COOKIE for input section SEC. */
13362
13363 static bfd_boolean
13364 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13365 struct bfd_link_info *info,
13366 asection *sec)
13367 {
13368 if (!init_reloc_cookie (cookie, info, sec->owner))
13369 goto error1;
13370 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13371 goto error2;
13372 return TRUE;
13373
13374 error2:
13375 fini_reloc_cookie (cookie, sec->owner);
13376 error1:
13377 return FALSE;
13378 }
13379
13380 /* Free the memory allocated by init_reloc_cookie_for_section,
13381 if appropriate. */
13382
13383 static void
13384 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13385 asection *sec)
13386 {
13387 fini_reloc_cookie_rels (cookie, sec);
13388 fini_reloc_cookie (cookie, sec->owner);
13389 }
13390 \f
13391 /* Garbage collect unused sections. */
13392
13393 /* Default gc_mark_hook. */
13394
13395 asection *
13396 _bfd_elf_gc_mark_hook (asection *sec,
13397 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13398 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13399 struct elf_link_hash_entry *h,
13400 Elf_Internal_Sym *sym)
13401 {
13402 if (h != NULL)
13403 {
13404 switch (h->root.type)
13405 {
13406 case bfd_link_hash_defined:
13407 case bfd_link_hash_defweak:
13408 return h->root.u.def.section;
13409
13410 case bfd_link_hash_common:
13411 return h->root.u.c.p->section;
13412
13413 default:
13414 break;
13415 }
13416 }
13417 else
13418 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13419
13420 return NULL;
13421 }
13422
13423 /* Return the debug definition section. */
13424
13425 static asection *
13426 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13427 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13428 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13429 struct elf_link_hash_entry *h,
13430 Elf_Internal_Sym *sym)
13431 {
13432 if (h != NULL)
13433 {
13434 /* Return the global debug definition section. */
13435 if ((h->root.type == bfd_link_hash_defined
13436 || h->root.type == bfd_link_hash_defweak)
13437 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13438 return h->root.u.def.section;
13439 }
13440 else
13441 {
13442 /* Return the local debug definition section. */
13443 asection *isec = bfd_section_from_elf_index (sec->owner,
13444 sym->st_shndx);
13445 if ((isec->flags & SEC_DEBUGGING) != 0)
13446 return isec;
13447 }
13448
13449 return NULL;
13450 }
13451
13452 /* COOKIE->rel describes a relocation against section SEC, which is
13453 a section we've decided to keep. Return the section that contains
13454 the relocation symbol, or NULL if no section contains it. */
13455
13456 asection *
13457 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13458 elf_gc_mark_hook_fn gc_mark_hook,
13459 struct elf_reloc_cookie *cookie,
13460 bfd_boolean *start_stop)
13461 {
13462 unsigned long r_symndx;
13463 struct elf_link_hash_entry *h, *hw;
13464
13465 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13466 if (r_symndx == STN_UNDEF)
13467 return NULL;
13468
13469 if (r_symndx >= cookie->locsymcount
13470 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13471 {
13472 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13473 if (h == NULL)
13474 {
13475 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13476 sec->owner);
13477 return NULL;
13478 }
13479 while (h->root.type == bfd_link_hash_indirect
13480 || h->root.type == bfd_link_hash_warning)
13481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13482 h->mark = 1;
13483 /* Keep all aliases of the symbol too. If an object symbol
13484 needs to be copied into .dynbss then all of its aliases
13485 should be present as dynamic symbols, not just the one used
13486 on the copy relocation. */
13487 hw = h;
13488 while (hw->is_weakalias)
13489 {
13490 hw = hw->u.alias;
13491 hw->mark = 1;
13492 }
13493
13494 if (start_stop != NULL)
13495 {
13496 /* To work around a glibc bug, mark XXX input sections
13497 when there is a reference to __start_XXX or __stop_XXX
13498 symbols. */
13499 if (h->start_stop)
13500 {
13501 asection *s = h->u2.start_stop_section;
13502 *start_stop = !s->gc_mark;
13503 return s;
13504 }
13505 }
13506
13507 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13508 }
13509
13510 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13511 &cookie->locsyms[r_symndx]);
13512 }
13513
13514 /* COOKIE->rel describes a relocation against section SEC, which is
13515 a section we've decided to keep. Mark the section that contains
13516 the relocation symbol. */
13517
13518 bfd_boolean
13519 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13520 asection *sec,
13521 elf_gc_mark_hook_fn gc_mark_hook,
13522 struct elf_reloc_cookie *cookie)
13523 {
13524 asection *rsec;
13525 bfd_boolean start_stop = FALSE;
13526
13527 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13528 while (rsec != NULL)
13529 {
13530 if (!rsec->gc_mark)
13531 {
13532 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13533 || (rsec->owner->flags & DYNAMIC) != 0)
13534 rsec->gc_mark = 1;
13535 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13536 return FALSE;
13537 }
13538 if (!start_stop)
13539 break;
13540 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13541 }
13542 return TRUE;
13543 }
13544
13545 /* The mark phase of garbage collection. For a given section, mark
13546 it and any sections in this section's group, and all the sections
13547 which define symbols to which it refers. */
13548
13549 bfd_boolean
13550 _bfd_elf_gc_mark (struct bfd_link_info *info,
13551 asection *sec,
13552 elf_gc_mark_hook_fn gc_mark_hook)
13553 {
13554 bfd_boolean ret;
13555 asection *group_sec, *eh_frame;
13556
13557 sec->gc_mark = 1;
13558
13559 /* Mark all the sections in the group. */
13560 group_sec = elf_section_data (sec)->next_in_group;
13561 if (group_sec && !group_sec->gc_mark)
13562 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13563 return FALSE;
13564
13565 /* Look through the section relocs. */
13566 ret = TRUE;
13567 eh_frame = elf_eh_frame_section (sec->owner);
13568 if ((sec->flags & SEC_RELOC) != 0
13569 && sec->reloc_count > 0
13570 && sec != eh_frame)
13571 {
13572 struct elf_reloc_cookie cookie;
13573
13574 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13575 ret = FALSE;
13576 else
13577 {
13578 for (; cookie.rel < cookie.relend; cookie.rel++)
13579 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13580 {
13581 ret = FALSE;
13582 break;
13583 }
13584 fini_reloc_cookie_for_section (&cookie, sec);
13585 }
13586 }
13587
13588 if (ret && eh_frame && elf_fde_list (sec))
13589 {
13590 struct elf_reloc_cookie cookie;
13591
13592 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13593 ret = FALSE;
13594 else
13595 {
13596 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13597 gc_mark_hook, &cookie))
13598 ret = FALSE;
13599 fini_reloc_cookie_for_section (&cookie, eh_frame);
13600 }
13601 }
13602
13603 eh_frame = elf_section_eh_frame_entry (sec);
13604 if (ret && eh_frame && !eh_frame->gc_mark)
13605 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13606 ret = FALSE;
13607
13608 return ret;
13609 }
13610
13611 /* Scan and mark sections in a special or debug section group. */
13612
13613 static void
13614 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13615 {
13616 /* Point to first section of section group. */
13617 asection *ssec;
13618 /* Used to iterate the section group. */
13619 asection *msec;
13620
13621 bfd_boolean is_special_grp = TRUE;
13622 bfd_boolean is_debug_grp = TRUE;
13623
13624 /* First scan to see if group contains any section other than debug
13625 and special section. */
13626 ssec = msec = elf_next_in_group (grp);
13627 do
13628 {
13629 if ((msec->flags & SEC_DEBUGGING) == 0)
13630 is_debug_grp = FALSE;
13631
13632 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13633 is_special_grp = FALSE;
13634
13635 msec = elf_next_in_group (msec);
13636 }
13637 while (msec != ssec);
13638
13639 /* If this is a pure debug section group or pure special section group,
13640 keep all sections in this group. */
13641 if (is_debug_grp || is_special_grp)
13642 {
13643 do
13644 {
13645 msec->gc_mark = 1;
13646 msec = elf_next_in_group (msec);
13647 }
13648 while (msec != ssec);
13649 }
13650 }
13651
13652 /* Keep debug and special sections. */
13653
13654 bfd_boolean
13655 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13656 elf_gc_mark_hook_fn mark_hook)
13657 {
13658 bfd *ibfd;
13659
13660 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13661 {
13662 asection *isec;
13663 bfd_boolean some_kept;
13664 bfd_boolean debug_frag_seen;
13665 bfd_boolean has_kept_debug_info;
13666
13667 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13668 continue;
13669 isec = ibfd->sections;
13670 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13671 continue;
13672
13673 /* Ensure all linker created sections are kept,
13674 see if any other section is already marked,
13675 and note if we have any fragmented debug sections. */
13676 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13677 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13678 {
13679 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13680 isec->gc_mark = 1;
13681 else if (isec->gc_mark
13682 && (isec->flags & SEC_ALLOC) != 0
13683 && elf_section_type (isec) != SHT_NOTE)
13684 some_kept = TRUE;
13685 else
13686 {
13687 /* Since all sections, except for backend specific ones,
13688 have been garbage collected, call mark_hook on this
13689 section if any of its linked-to sections is marked. */
13690 asection *linked_to_sec = elf_linked_to_section (isec);
13691 for (; linked_to_sec != NULL;
13692 linked_to_sec = elf_linked_to_section (linked_to_sec))
13693 if (linked_to_sec->gc_mark)
13694 {
13695 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13696 return FALSE;
13697 break;
13698 }
13699 }
13700
13701 if (!debug_frag_seen
13702 && (isec->flags & SEC_DEBUGGING)
13703 && CONST_STRNEQ (isec->name, ".debug_line."))
13704 debug_frag_seen = TRUE;
13705 else if (strcmp (bfd_section_name (isec),
13706 "__patchable_function_entries") == 0
13707 && elf_linked_to_section (isec) == NULL)
13708 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13709 "need linked-to section "
13710 "for --gc-sections\n"),
13711 isec->owner, isec);
13712 }
13713
13714 /* If no non-note alloc section in this file will be kept, then
13715 we can toss out the debug and special sections. */
13716 if (!some_kept)
13717 continue;
13718
13719 /* Keep debug and special sections like .comment when they are
13720 not part of a group. Also keep section groups that contain
13721 just debug sections or special sections. NB: Sections with
13722 linked-to section has been handled above. */
13723 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13724 {
13725 if ((isec->flags & SEC_GROUP) != 0)
13726 _bfd_elf_gc_mark_debug_special_section_group (isec);
13727 else if (((isec->flags & SEC_DEBUGGING) != 0
13728 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13729 && elf_next_in_group (isec) == NULL
13730 && elf_linked_to_section (isec) == NULL)
13731 isec->gc_mark = 1;
13732 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13733 has_kept_debug_info = TRUE;
13734 }
13735
13736 /* Look for CODE sections which are going to be discarded,
13737 and find and discard any fragmented debug sections which
13738 are associated with that code section. */
13739 if (debug_frag_seen)
13740 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13741 if ((isec->flags & SEC_CODE) != 0
13742 && isec->gc_mark == 0)
13743 {
13744 unsigned int ilen;
13745 asection *dsec;
13746
13747 ilen = strlen (isec->name);
13748
13749 /* Association is determined by the name of the debug
13750 section containing the name of the code section as
13751 a suffix. For example .debug_line.text.foo is a
13752 debug section associated with .text.foo. */
13753 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13754 {
13755 unsigned int dlen;
13756
13757 if (dsec->gc_mark == 0
13758 || (dsec->flags & SEC_DEBUGGING) == 0)
13759 continue;
13760
13761 dlen = strlen (dsec->name);
13762
13763 if (dlen > ilen
13764 && strncmp (dsec->name + (dlen - ilen),
13765 isec->name, ilen) == 0)
13766 dsec->gc_mark = 0;
13767 }
13768 }
13769
13770 /* Mark debug sections referenced by kept debug sections. */
13771 if (has_kept_debug_info)
13772 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13773 if (isec->gc_mark
13774 && (isec->flags & SEC_DEBUGGING) != 0)
13775 if (!_bfd_elf_gc_mark (info, isec,
13776 elf_gc_mark_debug_section))
13777 return FALSE;
13778 }
13779 return TRUE;
13780 }
13781
13782 static bfd_boolean
13783 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13784 {
13785 bfd *sub;
13786 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13787
13788 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13789 {
13790 asection *o;
13791
13792 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13793 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13794 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13795 continue;
13796 o = sub->sections;
13797 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13798 continue;
13799
13800 for (o = sub->sections; o != NULL; o = o->next)
13801 {
13802 /* When any section in a section group is kept, we keep all
13803 sections in the section group. If the first member of
13804 the section group is excluded, we will also exclude the
13805 group section. */
13806 if (o->flags & SEC_GROUP)
13807 {
13808 asection *first = elf_next_in_group (o);
13809 o->gc_mark = first->gc_mark;
13810 }
13811
13812 if (o->gc_mark)
13813 continue;
13814
13815 /* Skip sweeping sections already excluded. */
13816 if (o->flags & SEC_EXCLUDE)
13817 continue;
13818
13819 /* Since this is early in the link process, it is simple
13820 to remove a section from the output. */
13821 o->flags |= SEC_EXCLUDE;
13822
13823 if (info->print_gc_sections && o->size != 0)
13824 /* xgettext:c-format */
13825 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13826 o, sub);
13827 }
13828 }
13829
13830 return TRUE;
13831 }
13832
13833 /* Propagate collected vtable information. This is called through
13834 elf_link_hash_traverse. */
13835
13836 static bfd_boolean
13837 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13838 {
13839 /* Those that are not vtables. */
13840 if (h->start_stop
13841 || h->u2.vtable == NULL
13842 || h->u2.vtable->parent == NULL)
13843 return TRUE;
13844
13845 /* Those vtables that do not have parents, we cannot merge. */
13846 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13847 return TRUE;
13848
13849 /* If we've already been done, exit. */
13850 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13851 return TRUE;
13852
13853 /* Make sure the parent's table is up to date. */
13854 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13855
13856 if (h->u2.vtable->used == NULL)
13857 {
13858 /* None of this table's entries were referenced. Re-use the
13859 parent's table. */
13860 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13861 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13862 }
13863 else
13864 {
13865 size_t n;
13866 bfd_boolean *cu, *pu;
13867
13868 /* Or the parent's entries into ours. */
13869 cu = h->u2.vtable->used;
13870 cu[-1] = TRUE;
13871 pu = h->u2.vtable->parent->u2.vtable->used;
13872 if (pu != NULL)
13873 {
13874 const struct elf_backend_data *bed;
13875 unsigned int log_file_align;
13876
13877 bed = get_elf_backend_data (h->root.u.def.section->owner);
13878 log_file_align = bed->s->log_file_align;
13879 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13880 while (n--)
13881 {
13882 if (*pu)
13883 *cu = TRUE;
13884 pu++;
13885 cu++;
13886 }
13887 }
13888 }
13889
13890 return TRUE;
13891 }
13892
13893 static bfd_boolean
13894 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13895 {
13896 asection *sec;
13897 bfd_vma hstart, hend;
13898 Elf_Internal_Rela *relstart, *relend, *rel;
13899 const struct elf_backend_data *bed;
13900 unsigned int log_file_align;
13901
13902 /* Take care of both those symbols that do not describe vtables as
13903 well as those that are not loaded. */
13904 if (h->start_stop
13905 || h->u2.vtable == NULL
13906 || h->u2.vtable->parent == NULL)
13907 return TRUE;
13908
13909 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13910 || h->root.type == bfd_link_hash_defweak);
13911
13912 sec = h->root.u.def.section;
13913 hstart = h->root.u.def.value;
13914 hend = hstart + h->size;
13915
13916 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13917 if (!relstart)
13918 return *(bfd_boolean *) okp = FALSE;
13919 bed = get_elf_backend_data (sec->owner);
13920 log_file_align = bed->s->log_file_align;
13921
13922 relend = relstart + sec->reloc_count;
13923
13924 for (rel = relstart; rel < relend; ++rel)
13925 if (rel->r_offset >= hstart && rel->r_offset < hend)
13926 {
13927 /* If the entry is in use, do nothing. */
13928 if (h->u2.vtable->used
13929 && (rel->r_offset - hstart) < h->u2.vtable->size)
13930 {
13931 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13932 if (h->u2.vtable->used[entry])
13933 continue;
13934 }
13935 /* Otherwise, kill it. */
13936 rel->r_offset = rel->r_info = rel->r_addend = 0;
13937 }
13938
13939 return TRUE;
13940 }
13941
13942 /* Mark sections containing dynamically referenced symbols. When
13943 building shared libraries, we must assume that any visible symbol is
13944 referenced. */
13945
13946 bfd_boolean
13947 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13948 {
13949 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13950 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13951
13952 if ((h->root.type == bfd_link_hash_defined
13953 || h->root.type == bfd_link_hash_defweak)
13954 && ((h->ref_dynamic && !h->forced_local)
13955 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13956 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13957 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13958 && (!bfd_link_executable (info)
13959 || info->gc_keep_exported
13960 || info->export_dynamic
13961 || (h->dynamic
13962 && d != NULL
13963 && (*d->match) (&d->head, NULL, h->root.root.string)))
13964 && (h->versioned >= versioned
13965 || !bfd_hide_sym_by_version (info->version_info,
13966 h->root.root.string)))))
13967 h->root.u.def.section->flags |= SEC_KEEP;
13968
13969 return TRUE;
13970 }
13971
13972 /* Keep all sections containing symbols undefined on the command-line,
13973 and the section containing the entry symbol. */
13974
13975 void
13976 _bfd_elf_gc_keep (struct bfd_link_info *info)
13977 {
13978 struct bfd_sym_chain *sym;
13979
13980 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13981 {
13982 struct elf_link_hash_entry *h;
13983
13984 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13985 FALSE, FALSE, FALSE);
13986
13987 if (h != NULL
13988 && (h->root.type == bfd_link_hash_defined
13989 || h->root.type == bfd_link_hash_defweak)
13990 && !bfd_is_const_section (h->root.u.def.section))
13991 h->root.u.def.section->flags |= SEC_KEEP;
13992 }
13993 }
13994
13995 bfd_boolean
13996 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13997 struct bfd_link_info *info)
13998 {
13999 bfd *ibfd = info->input_bfds;
14000
14001 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14002 {
14003 asection *sec;
14004 struct elf_reloc_cookie cookie;
14005
14006 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14007 continue;
14008 sec = ibfd->sections;
14009 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14010 continue;
14011
14012 if (!init_reloc_cookie (&cookie, info, ibfd))
14013 return FALSE;
14014
14015 for (sec = ibfd->sections; sec; sec = sec->next)
14016 {
14017 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
14018 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14019 {
14020 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14021 fini_reloc_cookie_rels (&cookie, sec);
14022 }
14023 }
14024 }
14025 return TRUE;
14026 }
14027
14028 /* Do mark and sweep of unused sections. */
14029
14030 bfd_boolean
14031 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14032 {
14033 bfd_boolean ok = TRUE;
14034 bfd *sub;
14035 elf_gc_mark_hook_fn gc_mark_hook;
14036 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14037 struct elf_link_hash_table *htab;
14038
14039 if (!bed->can_gc_sections
14040 || !is_elf_hash_table (info->hash))
14041 {
14042 _bfd_error_handler(_("warning: gc-sections option ignored"));
14043 return TRUE;
14044 }
14045
14046 bed->gc_keep (info);
14047 htab = elf_hash_table (info);
14048
14049 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14050 at the .eh_frame section if we can mark the FDEs individually. */
14051 for (sub = info->input_bfds;
14052 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14053 sub = sub->link.next)
14054 {
14055 asection *sec;
14056 struct elf_reloc_cookie cookie;
14057
14058 sec = sub->sections;
14059 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14060 continue;
14061 sec = bfd_get_section_by_name (sub, ".eh_frame");
14062 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14063 {
14064 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14065 if (elf_section_data (sec)->sec_info
14066 && (sec->flags & SEC_LINKER_CREATED) == 0)
14067 elf_eh_frame_section (sub) = sec;
14068 fini_reloc_cookie_for_section (&cookie, sec);
14069 sec = bfd_get_next_section_by_name (NULL, sec);
14070 }
14071 }
14072
14073 /* Apply transitive closure to the vtable entry usage info. */
14074 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14075 if (!ok)
14076 return FALSE;
14077
14078 /* Kill the vtable relocations that were not used. */
14079 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14080 if (!ok)
14081 return FALSE;
14082
14083 /* Mark dynamically referenced symbols. */
14084 if (htab->dynamic_sections_created || info->gc_keep_exported)
14085 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14086
14087 /* Grovel through relocs to find out who stays ... */
14088 gc_mark_hook = bed->gc_mark_hook;
14089 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14090 {
14091 asection *o;
14092
14093 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14094 || elf_object_id (sub) != elf_hash_table_id (htab)
14095 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14096 continue;
14097
14098 o = sub->sections;
14099 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14100 continue;
14101
14102 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14103 Also treat note sections as a root, if the section is not part
14104 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14105 well as FINI_ARRAY sections for ld -r. */
14106 for (o = sub->sections; o != NULL; o = o->next)
14107 if (!o->gc_mark
14108 && (o->flags & SEC_EXCLUDE) == 0
14109 && ((o->flags & SEC_KEEP) != 0
14110 || (bfd_link_relocatable (info)
14111 && ((elf_section_data (o)->this_hdr.sh_type
14112 == SHT_PREINIT_ARRAY)
14113 || (elf_section_data (o)->this_hdr.sh_type
14114 == SHT_INIT_ARRAY)
14115 || (elf_section_data (o)->this_hdr.sh_type
14116 == SHT_FINI_ARRAY)))
14117 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14118 && elf_next_in_group (o) == NULL
14119 && elf_linked_to_section (o) == NULL)))
14120 {
14121 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14122 return FALSE;
14123 }
14124 }
14125
14126 /* Allow the backend to mark additional target specific sections. */
14127 bed->gc_mark_extra_sections (info, gc_mark_hook);
14128
14129 /* ... and mark SEC_EXCLUDE for those that go. */
14130 return elf_gc_sweep (abfd, info);
14131 }
14132 \f
14133 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14134
14135 bfd_boolean
14136 bfd_elf_gc_record_vtinherit (bfd *abfd,
14137 asection *sec,
14138 struct elf_link_hash_entry *h,
14139 bfd_vma offset)
14140 {
14141 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14142 struct elf_link_hash_entry **search, *child;
14143 size_t extsymcount;
14144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14145
14146 /* The sh_info field of the symtab header tells us where the
14147 external symbols start. We don't care about the local symbols at
14148 this point. */
14149 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14150 if (!elf_bad_symtab (abfd))
14151 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14152
14153 sym_hashes = elf_sym_hashes (abfd);
14154 sym_hashes_end = sym_hashes + extsymcount;
14155
14156 /* Hunt down the child symbol, which is in this section at the same
14157 offset as the relocation. */
14158 for (search = sym_hashes; search != sym_hashes_end; ++search)
14159 {
14160 if ((child = *search) != NULL
14161 && (child->root.type == bfd_link_hash_defined
14162 || child->root.type == bfd_link_hash_defweak)
14163 && child->root.u.def.section == sec
14164 && child->root.u.def.value == offset)
14165 goto win;
14166 }
14167
14168 /* xgettext:c-format */
14169 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14170 abfd, sec, (uint64_t) offset);
14171 bfd_set_error (bfd_error_invalid_operation);
14172 return FALSE;
14173
14174 win:
14175 if (!child->u2.vtable)
14176 {
14177 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14178 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14179 if (!child->u2.vtable)
14180 return FALSE;
14181 }
14182 if (!h)
14183 {
14184 /* This *should* only be the absolute section. It could potentially
14185 be that someone has defined a non-global vtable though, which
14186 would be bad. It isn't worth paging in the local symbols to be
14187 sure though; that case should simply be handled by the assembler. */
14188
14189 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14190 }
14191 else
14192 child->u2.vtable->parent = h;
14193
14194 return TRUE;
14195 }
14196
14197 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14198
14199 bfd_boolean
14200 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14201 struct elf_link_hash_entry *h,
14202 bfd_vma addend)
14203 {
14204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14205 unsigned int log_file_align = bed->s->log_file_align;
14206
14207 if (!h)
14208 {
14209 /* xgettext:c-format */
14210 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14211 abfd, sec);
14212 bfd_set_error (bfd_error_bad_value);
14213 return FALSE;
14214 }
14215
14216 if (!h->u2.vtable)
14217 {
14218 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14219 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14220 if (!h->u2.vtable)
14221 return FALSE;
14222 }
14223
14224 if (addend >= h->u2.vtable->size)
14225 {
14226 size_t size, bytes, file_align;
14227 bfd_boolean *ptr = h->u2.vtable->used;
14228
14229 /* While the symbol is undefined, we have to be prepared to handle
14230 a zero size. */
14231 file_align = 1 << log_file_align;
14232 if (h->root.type == bfd_link_hash_undefined)
14233 size = addend + file_align;
14234 else
14235 {
14236 size = h->size;
14237 if (addend >= size)
14238 {
14239 /* Oops! We've got a reference past the defined end of
14240 the table. This is probably a bug -- shall we warn? */
14241 size = addend + file_align;
14242 }
14243 }
14244 size = (size + file_align - 1) & -file_align;
14245
14246 /* Allocate one extra entry for use as a "done" flag for the
14247 consolidation pass. */
14248 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14249
14250 if (ptr)
14251 {
14252 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14253
14254 if (ptr != NULL)
14255 {
14256 size_t oldbytes;
14257
14258 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14259 * sizeof (bfd_boolean));
14260 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14261 }
14262 }
14263 else
14264 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14265
14266 if (ptr == NULL)
14267 return FALSE;
14268
14269 /* And arrange for that done flag to be at index -1. */
14270 h->u2.vtable->used = ptr + 1;
14271 h->u2.vtable->size = size;
14272 }
14273
14274 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14275
14276 return TRUE;
14277 }
14278
14279 /* Map an ELF section header flag to its corresponding string. */
14280 typedef struct
14281 {
14282 char *flag_name;
14283 flagword flag_value;
14284 } elf_flags_to_name_table;
14285
14286 static elf_flags_to_name_table elf_flags_to_names [] =
14287 {
14288 { "SHF_WRITE", SHF_WRITE },
14289 { "SHF_ALLOC", SHF_ALLOC },
14290 { "SHF_EXECINSTR", SHF_EXECINSTR },
14291 { "SHF_MERGE", SHF_MERGE },
14292 { "SHF_STRINGS", SHF_STRINGS },
14293 { "SHF_INFO_LINK", SHF_INFO_LINK},
14294 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14295 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14296 { "SHF_GROUP", SHF_GROUP },
14297 { "SHF_TLS", SHF_TLS },
14298 { "SHF_MASKOS", SHF_MASKOS },
14299 { "SHF_EXCLUDE", SHF_EXCLUDE },
14300 };
14301
14302 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14303 bfd_boolean
14304 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14305 struct flag_info *flaginfo,
14306 asection *section)
14307 {
14308 const bfd_vma sh_flags = elf_section_flags (section);
14309
14310 if (!flaginfo->flags_initialized)
14311 {
14312 bfd *obfd = info->output_bfd;
14313 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14314 struct flag_info_list *tf = flaginfo->flag_list;
14315 int with_hex = 0;
14316 int without_hex = 0;
14317
14318 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14319 {
14320 unsigned i;
14321 flagword (*lookup) (char *);
14322
14323 lookup = bed->elf_backend_lookup_section_flags_hook;
14324 if (lookup != NULL)
14325 {
14326 flagword hexval = (*lookup) ((char *) tf->name);
14327
14328 if (hexval != 0)
14329 {
14330 if (tf->with == with_flags)
14331 with_hex |= hexval;
14332 else if (tf->with == without_flags)
14333 without_hex |= hexval;
14334 tf->valid = TRUE;
14335 continue;
14336 }
14337 }
14338 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14339 {
14340 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14341 {
14342 if (tf->with == with_flags)
14343 with_hex |= elf_flags_to_names[i].flag_value;
14344 else if (tf->with == without_flags)
14345 without_hex |= elf_flags_to_names[i].flag_value;
14346 tf->valid = TRUE;
14347 break;
14348 }
14349 }
14350 if (!tf->valid)
14351 {
14352 info->callbacks->einfo
14353 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14354 return FALSE;
14355 }
14356 }
14357 flaginfo->flags_initialized = TRUE;
14358 flaginfo->only_with_flags |= with_hex;
14359 flaginfo->not_with_flags |= without_hex;
14360 }
14361
14362 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14363 return FALSE;
14364
14365 if ((flaginfo->not_with_flags & sh_flags) != 0)
14366 return FALSE;
14367
14368 return TRUE;
14369 }
14370
14371 struct alloc_got_off_arg {
14372 bfd_vma gotoff;
14373 struct bfd_link_info *info;
14374 };
14375
14376 /* We need a special top-level link routine to convert got reference counts
14377 to real got offsets. */
14378
14379 static bfd_boolean
14380 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14381 {
14382 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14383 bfd *obfd = gofarg->info->output_bfd;
14384 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14385
14386 if (h->got.refcount > 0)
14387 {
14388 h->got.offset = gofarg->gotoff;
14389 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14390 }
14391 else
14392 h->got.offset = (bfd_vma) -1;
14393
14394 return TRUE;
14395 }
14396
14397 /* And an accompanying bit to work out final got entry offsets once
14398 we're done. Should be called from final_link. */
14399
14400 bfd_boolean
14401 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14402 struct bfd_link_info *info)
14403 {
14404 bfd *i;
14405 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14406 bfd_vma gotoff;
14407 struct alloc_got_off_arg gofarg;
14408
14409 BFD_ASSERT (abfd == info->output_bfd);
14410
14411 if (! is_elf_hash_table (info->hash))
14412 return FALSE;
14413
14414 /* The GOT offset is relative to the .got section, but the GOT header is
14415 put into the .got.plt section, if the backend uses it. */
14416 if (bed->want_got_plt)
14417 gotoff = 0;
14418 else
14419 gotoff = bed->got_header_size;
14420
14421 /* Do the local .got entries first. */
14422 for (i = info->input_bfds; i; i = i->link.next)
14423 {
14424 bfd_signed_vma *local_got;
14425 size_t j, locsymcount;
14426 Elf_Internal_Shdr *symtab_hdr;
14427
14428 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14429 continue;
14430
14431 local_got = elf_local_got_refcounts (i);
14432 if (!local_got)
14433 continue;
14434
14435 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14436 if (elf_bad_symtab (i))
14437 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14438 else
14439 locsymcount = symtab_hdr->sh_info;
14440
14441 for (j = 0; j < locsymcount; ++j)
14442 {
14443 if (local_got[j] > 0)
14444 {
14445 local_got[j] = gotoff;
14446 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14447 }
14448 else
14449 local_got[j] = (bfd_vma) -1;
14450 }
14451 }
14452
14453 /* Then the global .got entries. .plt refcounts are handled by
14454 adjust_dynamic_symbol */
14455 gofarg.gotoff = gotoff;
14456 gofarg.info = info;
14457 elf_link_hash_traverse (elf_hash_table (info),
14458 elf_gc_allocate_got_offsets,
14459 &gofarg);
14460 return TRUE;
14461 }
14462
14463 /* Many folk need no more in the way of final link than this, once
14464 got entry reference counting is enabled. */
14465
14466 bfd_boolean
14467 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14468 {
14469 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14470 return FALSE;
14471
14472 /* Invoke the regular ELF backend linker to do all the work. */
14473 return bfd_elf_final_link (abfd, info);
14474 }
14475
14476 bfd_boolean
14477 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14478 {
14479 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14480
14481 if (rcookie->bad_symtab)
14482 rcookie->rel = rcookie->rels;
14483
14484 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14485 {
14486 unsigned long r_symndx;
14487
14488 if (! rcookie->bad_symtab)
14489 if (rcookie->rel->r_offset > offset)
14490 return FALSE;
14491 if (rcookie->rel->r_offset != offset)
14492 continue;
14493
14494 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14495 if (r_symndx == STN_UNDEF)
14496 return TRUE;
14497
14498 if (r_symndx >= rcookie->locsymcount
14499 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14500 {
14501 struct elf_link_hash_entry *h;
14502
14503 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14504
14505 while (h->root.type == bfd_link_hash_indirect
14506 || h->root.type == bfd_link_hash_warning)
14507 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14508
14509 if ((h->root.type == bfd_link_hash_defined
14510 || h->root.type == bfd_link_hash_defweak)
14511 && (h->root.u.def.section->owner != rcookie->abfd
14512 || h->root.u.def.section->kept_section != NULL
14513 || discarded_section (h->root.u.def.section)))
14514 return TRUE;
14515 }
14516 else
14517 {
14518 /* It's not a relocation against a global symbol,
14519 but it could be a relocation against a local
14520 symbol for a discarded section. */
14521 asection *isec;
14522 Elf_Internal_Sym *isym;
14523
14524 /* Need to: get the symbol; get the section. */
14525 isym = &rcookie->locsyms[r_symndx];
14526 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14527 if (isec != NULL
14528 && (isec->kept_section != NULL
14529 || discarded_section (isec)))
14530 return TRUE;
14531 }
14532 return FALSE;
14533 }
14534 return FALSE;
14535 }
14536
14537 /* Discard unneeded references to discarded sections.
14538 Returns -1 on error, 1 if any section's size was changed, 0 if
14539 nothing changed. This function assumes that the relocations are in
14540 sorted order, which is true for all known assemblers. */
14541
14542 int
14543 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14544 {
14545 struct elf_reloc_cookie cookie;
14546 asection *o;
14547 bfd *abfd;
14548 int changed = 0;
14549
14550 if (info->traditional_format
14551 || !is_elf_hash_table (info->hash))
14552 return 0;
14553
14554 o = bfd_get_section_by_name (output_bfd, ".stab");
14555 if (o != NULL)
14556 {
14557 asection *i;
14558
14559 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14560 {
14561 if (i->size == 0
14562 || i->reloc_count == 0
14563 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14564 continue;
14565
14566 abfd = i->owner;
14567 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14568 continue;
14569
14570 if (!init_reloc_cookie_for_section (&cookie, info, i))
14571 return -1;
14572
14573 if (_bfd_discard_section_stabs (abfd, i,
14574 elf_section_data (i)->sec_info,
14575 bfd_elf_reloc_symbol_deleted_p,
14576 &cookie))
14577 changed = 1;
14578
14579 fini_reloc_cookie_for_section (&cookie, i);
14580 }
14581 }
14582
14583 o = NULL;
14584 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14585 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14586 if (o != NULL)
14587 {
14588 asection *i;
14589 int eh_changed = 0;
14590 unsigned int eh_alignment; /* Octets. */
14591
14592 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14593 {
14594 if (i->size == 0)
14595 continue;
14596
14597 abfd = i->owner;
14598 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14599 continue;
14600
14601 if (!init_reloc_cookie_for_section (&cookie, info, i))
14602 return -1;
14603
14604 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14605 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14606 bfd_elf_reloc_symbol_deleted_p,
14607 &cookie))
14608 {
14609 eh_changed = 1;
14610 if (i->size != i->rawsize)
14611 changed = 1;
14612 }
14613
14614 fini_reloc_cookie_for_section (&cookie, i);
14615 }
14616
14617 eh_alignment = ((1 << o->alignment_power)
14618 * bfd_octets_per_byte (output_bfd, o));
14619 /* Skip over zero terminator, and prevent empty sections from
14620 adding alignment padding at the end. */
14621 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14622 if (i->size == 0)
14623 i->flags |= SEC_EXCLUDE;
14624 else if (i->size > 4)
14625 break;
14626 /* The last non-empty eh_frame section doesn't need padding. */
14627 if (i != NULL)
14628 i = i->map_tail.s;
14629 /* Any prior sections must pad the last FDE out to the output
14630 section alignment. Otherwise we might have zero padding
14631 between sections, which would be seen as a terminator. */
14632 for (; i != NULL; i = i->map_tail.s)
14633 if (i->size == 4)
14634 /* All but the last zero terminator should have been removed. */
14635 BFD_FAIL ();
14636 else
14637 {
14638 bfd_size_type size
14639 = (i->size + eh_alignment - 1) & -eh_alignment;
14640 if (i->size != size)
14641 {
14642 i->size = size;
14643 changed = 1;
14644 eh_changed = 1;
14645 }
14646 }
14647 if (eh_changed)
14648 elf_link_hash_traverse (elf_hash_table (info),
14649 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14650 }
14651
14652 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14653 {
14654 const struct elf_backend_data *bed;
14655 asection *s;
14656
14657 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14658 continue;
14659 s = abfd->sections;
14660 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14661 continue;
14662
14663 bed = get_elf_backend_data (abfd);
14664
14665 if (bed->elf_backend_discard_info != NULL)
14666 {
14667 if (!init_reloc_cookie (&cookie, info, abfd))
14668 return -1;
14669
14670 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14671 changed = 1;
14672
14673 fini_reloc_cookie (&cookie, abfd);
14674 }
14675 }
14676
14677 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14678 _bfd_elf_end_eh_frame_parsing (info);
14679
14680 if (info->eh_frame_hdr_type
14681 && !bfd_link_relocatable (info)
14682 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14683 changed = 1;
14684
14685 return changed;
14686 }
14687
14688 bfd_boolean
14689 _bfd_elf_section_already_linked (bfd *abfd,
14690 asection *sec,
14691 struct bfd_link_info *info)
14692 {
14693 flagword flags;
14694 const char *name, *key;
14695 struct bfd_section_already_linked *l;
14696 struct bfd_section_already_linked_hash_entry *already_linked_list;
14697
14698 if (sec->output_section == bfd_abs_section_ptr)
14699 return FALSE;
14700
14701 flags = sec->flags;
14702
14703 /* Return if it isn't a linkonce section. A comdat group section
14704 also has SEC_LINK_ONCE set. */
14705 if ((flags & SEC_LINK_ONCE) == 0)
14706 return FALSE;
14707
14708 /* Don't put group member sections on our list of already linked
14709 sections. They are handled as a group via their group section. */
14710 if (elf_sec_group (sec) != NULL)
14711 return FALSE;
14712
14713 /* For a SHT_GROUP section, use the group signature as the key. */
14714 name = sec->name;
14715 if ((flags & SEC_GROUP) != 0
14716 && elf_next_in_group (sec) != NULL
14717 && elf_group_name (elf_next_in_group (sec)) != NULL)
14718 key = elf_group_name (elf_next_in_group (sec));
14719 else
14720 {
14721 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14722 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14723 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14724 key++;
14725 else
14726 /* Must be a user linkonce section that doesn't follow gcc's
14727 naming convention. In this case we won't be matching
14728 single member groups. */
14729 key = name;
14730 }
14731
14732 already_linked_list = bfd_section_already_linked_table_lookup (key);
14733
14734 for (l = already_linked_list->entry; l != NULL; l = l->next)
14735 {
14736 /* We may have 2 different types of sections on the list: group
14737 sections with a signature of <key> (<key> is some string),
14738 and linkonce sections named .gnu.linkonce.<type>.<key>.
14739 Match like sections. LTO plugin sections are an exception.
14740 They are always named .gnu.linkonce.t.<key> and match either
14741 type of section. */
14742 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14743 && ((flags & SEC_GROUP) != 0
14744 || strcmp (name, l->sec->name) == 0))
14745 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14746 || (sec->owner->flags & BFD_PLUGIN) != 0)
14747 {
14748 /* The section has already been linked. See if we should
14749 issue a warning. */
14750 if (!_bfd_handle_already_linked (sec, l, info))
14751 return FALSE;
14752
14753 if (flags & SEC_GROUP)
14754 {
14755 asection *first = elf_next_in_group (sec);
14756 asection *s = first;
14757
14758 while (s != NULL)
14759 {
14760 s->output_section = bfd_abs_section_ptr;
14761 /* Record which group discards it. */
14762 s->kept_section = l->sec;
14763 s = elf_next_in_group (s);
14764 /* These lists are circular. */
14765 if (s == first)
14766 break;
14767 }
14768 }
14769
14770 return TRUE;
14771 }
14772 }
14773
14774 /* A single member comdat group section may be discarded by a
14775 linkonce section and vice versa. */
14776 if ((flags & SEC_GROUP) != 0)
14777 {
14778 asection *first = elf_next_in_group (sec);
14779
14780 if (first != NULL && elf_next_in_group (first) == first)
14781 /* Check this single member group against linkonce sections. */
14782 for (l = already_linked_list->entry; l != NULL; l = l->next)
14783 if ((l->sec->flags & SEC_GROUP) == 0
14784 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14785 {
14786 first->output_section = bfd_abs_section_ptr;
14787 first->kept_section = l->sec;
14788 sec->output_section = bfd_abs_section_ptr;
14789 break;
14790 }
14791 }
14792 else
14793 /* Check this linkonce section against single member groups. */
14794 for (l = already_linked_list->entry; l != NULL; l = l->next)
14795 if (l->sec->flags & SEC_GROUP)
14796 {
14797 asection *first = elf_next_in_group (l->sec);
14798
14799 if (first != NULL
14800 && elf_next_in_group (first) == first
14801 && bfd_elf_match_symbols_in_sections (first, sec, info))
14802 {
14803 sec->output_section = bfd_abs_section_ptr;
14804 sec->kept_section = first;
14805 break;
14806 }
14807 }
14808
14809 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14810 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14811 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14812 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14813 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14814 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14815 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14816 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14817 The reverse order cannot happen as there is never a bfd with only the
14818 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14819 matter as here were are looking only for cross-bfd sections. */
14820
14821 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14822 for (l = already_linked_list->entry; l != NULL; l = l->next)
14823 if ((l->sec->flags & SEC_GROUP) == 0
14824 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14825 {
14826 if (abfd != l->sec->owner)
14827 sec->output_section = bfd_abs_section_ptr;
14828 break;
14829 }
14830
14831 /* This is the first section with this name. Record it. */
14832 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14833 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14834 return sec->output_section == bfd_abs_section_ptr;
14835 }
14836
14837 bfd_boolean
14838 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14839 {
14840 return sym->st_shndx == SHN_COMMON;
14841 }
14842
14843 unsigned int
14844 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14845 {
14846 return SHN_COMMON;
14847 }
14848
14849 asection *
14850 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14851 {
14852 return bfd_com_section_ptr;
14853 }
14854
14855 bfd_vma
14856 _bfd_elf_default_got_elt_size (bfd *abfd,
14857 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14858 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14859 bfd *ibfd ATTRIBUTE_UNUSED,
14860 unsigned long symndx ATTRIBUTE_UNUSED)
14861 {
14862 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14863 return bed->s->arch_size / 8;
14864 }
14865
14866 /* Routines to support the creation of dynamic relocs. */
14867
14868 /* Returns the name of the dynamic reloc section associated with SEC. */
14869
14870 static const char *
14871 get_dynamic_reloc_section_name (bfd * abfd,
14872 asection * sec,
14873 bfd_boolean is_rela)
14874 {
14875 char *name;
14876 const char *old_name = bfd_section_name (sec);
14877 const char *prefix = is_rela ? ".rela" : ".rel";
14878
14879 if (old_name == NULL)
14880 return NULL;
14881
14882 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14883 sprintf (name, "%s%s", prefix, old_name);
14884
14885 return name;
14886 }
14887
14888 /* Returns the dynamic reloc section associated with SEC.
14889 If necessary compute the name of the dynamic reloc section based
14890 on SEC's name (looked up in ABFD's string table) and the setting
14891 of IS_RELA. */
14892
14893 asection *
14894 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14895 asection * sec,
14896 bfd_boolean is_rela)
14897 {
14898 asection * reloc_sec = elf_section_data (sec)->sreloc;
14899
14900 if (reloc_sec == NULL)
14901 {
14902 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14903
14904 if (name != NULL)
14905 {
14906 reloc_sec = bfd_get_linker_section (abfd, name);
14907
14908 if (reloc_sec != NULL)
14909 elf_section_data (sec)->sreloc = reloc_sec;
14910 }
14911 }
14912
14913 return reloc_sec;
14914 }
14915
14916 /* Returns the dynamic reloc section associated with SEC. If the
14917 section does not exist it is created and attached to the DYNOBJ
14918 bfd and stored in the SRELOC field of SEC's elf_section_data
14919 structure.
14920
14921 ALIGNMENT is the alignment for the newly created section and
14922 IS_RELA defines whether the name should be .rela.<SEC's name>
14923 or .rel.<SEC's name>. The section name is looked up in the
14924 string table associated with ABFD. */
14925
14926 asection *
14927 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14928 bfd *dynobj,
14929 unsigned int alignment,
14930 bfd *abfd,
14931 bfd_boolean is_rela)
14932 {
14933 asection * reloc_sec = elf_section_data (sec)->sreloc;
14934
14935 if (reloc_sec == NULL)
14936 {
14937 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14938
14939 if (name == NULL)
14940 return NULL;
14941
14942 reloc_sec = bfd_get_linker_section (dynobj, name);
14943
14944 if (reloc_sec == NULL)
14945 {
14946 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14947 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14948 if ((sec->flags & SEC_ALLOC) != 0)
14949 flags |= SEC_ALLOC | SEC_LOAD;
14950
14951 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14952 if (reloc_sec != NULL)
14953 {
14954 /* _bfd_elf_get_sec_type_attr chooses a section type by
14955 name. Override as it may be wrong, eg. for a user
14956 section named "auto" we'll get ".relauto" which is
14957 seen to be a .rela section. */
14958 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14959 if (!bfd_set_section_alignment (reloc_sec, alignment))
14960 reloc_sec = NULL;
14961 }
14962 }
14963
14964 elf_section_data (sec)->sreloc = reloc_sec;
14965 }
14966
14967 return reloc_sec;
14968 }
14969
14970 /* Copy the ELF symbol type and other attributes for a linker script
14971 assignment from HSRC to HDEST. Generally this should be treated as
14972 if we found a strong non-dynamic definition for HDEST (except that
14973 ld ignores multiple definition errors). */
14974 void
14975 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14976 struct bfd_link_hash_entry *hdest,
14977 struct bfd_link_hash_entry *hsrc)
14978 {
14979 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14980 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14981 Elf_Internal_Sym isym;
14982
14983 ehdest->type = ehsrc->type;
14984 ehdest->target_internal = ehsrc->target_internal;
14985
14986 isym.st_other = ehsrc->other;
14987 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14988 }
14989
14990 /* Append a RELA relocation REL to section S in BFD. */
14991
14992 void
14993 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14994 {
14995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14996 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14997 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14998 bed->s->swap_reloca_out (abfd, rel, loc);
14999 }
15000
15001 /* Append a REL relocation REL to section S in BFD. */
15002
15003 void
15004 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15005 {
15006 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15007 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15008 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15009 bed->s->swap_reloc_out (abfd, rel, loc);
15010 }
15011
15012 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15013
15014 struct bfd_link_hash_entry *
15015 bfd_elf_define_start_stop (struct bfd_link_info *info,
15016 const char *symbol, asection *sec)
15017 {
15018 struct elf_link_hash_entry *h;
15019
15020 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15021 FALSE, FALSE, TRUE);
15022 /* NB: Common symbols will be turned into definition later. */
15023 if (h != NULL
15024 && (h->root.type == bfd_link_hash_undefined
15025 || h->root.type == bfd_link_hash_undefweak
15026 || ((h->ref_regular || h->def_dynamic)
15027 && !h->def_regular
15028 && h->root.type != bfd_link_hash_common)))
15029 {
15030 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
15031 h->verinfo.verdef = NULL;
15032 h->root.type = bfd_link_hash_defined;
15033 h->root.u.def.section = sec;
15034 h->root.u.def.value = 0;
15035 h->def_regular = 1;
15036 h->def_dynamic = 0;
15037 h->start_stop = 1;
15038 h->u2.start_stop_section = sec;
15039 if (symbol[0] == '.')
15040 {
15041 /* .startof. and .sizeof. symbols are local. */
15042 const struct elf_backend_data *bed;
15043 bed = get_elf_backend_data (info->output_bfd);
15044 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
15045 }
15046 else
15047 {
15048 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15049 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15050 | info->start_stop_visibility);
15051 if (was_dynamic)
15052 bfd_elf_link_record_dynamic_symbol (info, h);
15053 }
15054 return &h->root;
15055 }
15056 return NULL;
15057 }
15058
15059 /* Find dynamic relocs for H that apply to read-only sections. */
15060
15061 asection *
15062 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15063 {
15064 struct elf_dyn_relocs *p;
15065
15066 for (p = h->dyn_relocs; p != NULL; p = p->next)
15067 {
15068 asection *s = p->sec->output_section;
15069
15070 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15071 return p->sec;
15072 }
15073 return NULL;
15074 }
15075
15076 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15077 read-only sections. */
15078
15079 bfd_boolean
15080 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15081 {
15082 asection *sec;
15083
15084 if (h->root.type == bfd_link_hash_indirect)
15085 return TRUE;
15086
15087 sec = _bfd_elf_readonly_dynrelocs (h);
15088 if (sec != NULL)
15089 {
15090 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15091
15092 info->flags |= DF_TEXTREL;
15093 /* xgettext:c-format */
15094 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15095 "in read-only section `%pA'\n"),
15096 sec->owner, h->root.root.string, sec);
15097
15098 if (bfd_link_textrel_check (info))
15099 /* xgettext:c-format */
15100 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15101 "in read-only section `%pA'\n"),
15102 sec->owner, h->root.root.string, sec);
15103
15104 /* Not an error, just cut short the traversal. */
15105 return FALSE;
15106 }
15107 return TRUE;
15108 }
15109
15110 /* Add dynamic tags. */
15111
15112 bfd_boolean
15113 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15114 bfd_boolean need_dynamic_reloc)
15115 {
15116 struct elf_link_hash_table *htab = elf_hash_table (info);
15117
15118 if (htab->dynamic_sections_created)
15119 {
15120 /* Add some entries to the .dynamic section. We fill in the
15121 values later, in finish_dynamic_sections, but we must add
15122 the entries now so that we get the correct size for the
15123 .dynamic section. The DT_DEBUG entry is filled in by the
15124 dynamic linker and used by the debugger. */
15125 #define add_dynamic_entry(TAG, VAL) \
15126 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15127
15128 const struct elf_backend_data *bed
15129 = get_elf_backend_data (output_bfd);
15130
15131 if (bfd_link_executable (info))
15132 {
15133 if (!add_dynamic_entry (DT_DEBUG, 0))
15134 return FALSE;
15135 }
15136
15137 if (htab->dt_pltgot_required || htab->splt->size != 0)
15138 {
15139 /* DT_PLTGOT is used by prelink even if there is no PLT
15140 relocation. */
15141 if (!add_dynamic_entry (DT_PLTGOT, 0))
15142 return FALSE;
15143 }
15144
15145 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15146 {
15147 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15148 || !add_dynamic_entry (DT_PLTREL,
15149 (bed->rela_plts_and_copies_p
15150 ? DT_RELA : DT_REL))
15151 || !add_dynamic_entry (DT_JMPREL, 0))
15152 return FALSE;
15153 }
15154
15155 if (htab->tlsdesc_plt
15156 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15157 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15158 return FALSE;
15159
15160 if (need_dynamic_reloc)
15161 {
15162 if (bed->rela_plts_and_copies_p)
15163 {
15164 if (!add_dynamic_entry (DT_RELA, 0)
15165 || !add_dynamic_entry (DT_RELASZ, 0)
15166 || !add_dynamic_entry (DT_RELAENT,
15167 bed->s->sizeof_rela))
15168 return FALSE;
15169 }
15170 else
15171 {
15172 if (!add_dynamic_entry (DT_REL, 0)
15173 || !add_dynamic_entry (DT_RELSZ, 0)
15174 || !add_dynamic_entry (DT_RELENT,
15175 bed->s->sizeof_rel))
15176 return FALSE;
15177 }
15178
15179 /* If any dynamic relocs apply to a read-only section,
15180 then we need a DT_TEXTREL entry. */
15181 if ((info->flags & DF_TEXTREL) == 0)
15182 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15183 info);
15184
15185 if ((info->flags & DF_TEXTREL) != 0)
15186 {
15187 if (htab->ifunc_resolvers)
15188 info->callbacks->einfo
15189 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15190 "may result in a segfault at runtime; recompile with %s\n"),
15191 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15192
15193 if (!add_dynamic_entry (DT_TEXTREL, 0))
15194 return FALSE;
15195 }
15196 }
15197 }
15198 #undef add_dynamic_entry
15199
15200 return TRUE;
15201 }