aarch64: Add base support for Armv8-R
[deliverable/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 if (! dynamic)
4993 {
4994 if (! definition)
4995 {
4996 h->ref_regular = 1;
4997 if (bind != STB_WEAK)
4998 h->ref_regular_nonweak = 1;
4999 }
5000 else
5001 {
5002 h->def_regular = 1;
5003 if (h->def_dynamic)
5004 {
5005 h->def_dynamic = 0;
5006 h->ref_dynamic = 1;
5007 }
5008 }
5009
5010 /* If the indirect symbol has been forced local, don't
5011 make the real symbol dynamic. */
5012 if ((h == hi || !hi->forced_local)
5013 && (bfd_link_dll (info)
5014 || h->def_dynamic
5015 || h->ref_dynamic))
5016 dynsym = TRUE;
5017 }
5018 else
5019 {
5020 if (! definition)
5021 {
5022 h->ref_dynamic = 1;
5023 hi->ref_dynamic = 1;
5024 }
5025 else
5026 {
5027 h->def_dynamic = 1;
5028 hi->def_dynamic = 1;
5029 }
5030
5031 /* If the indirect symbol has been forced local, don't
5032 make the real symbol dynamic. */
5033 if ((h == hi || !hi->forced_local)
5034 && (h->def_regular
5035 || h->ref_regular
5036 || (h->is_weakalias
5037 && weakdef (h)->dynindx != -1)))
5038 dynsym = TRUE;
5039 }
5040
5041 /* Check to see if we need to add an indirect symbol for
5042 the default name. */
5043 if (definition
5044 || (!override && h->root.type == bfd_link_hash_common))
5045 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5046 sec, value, &old_bfd, &dynsym))
5047 goto error_free_vers;
5048
5049 /* Check the alignment when a common symbol is involved. This
5050 can change when a common symbol is overridden by a normal
5051 definition or a common symbol is ignored due to the old
5052 normal definition. We need to make sure the maximum
5053 alignment is maintained. */
5054 if ((old_alignment || common)
5055 && h->root.type != bfd_link_hash_common)
5056 {
5057 unsigned int common_align;
5058 unsigned int normal_align;
5059 unsigned int symbol_align;
5060 bfd *normal_bfd;
5061 bfd *common_bfd;
5062
5063 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5064 || h->root.type == bfd_link_hash_defweak);
5065
5066 symbol_align = ffs (h->root.u.def.value) - 1;
5067 if (h->root.u.def.section->owner != NULL
5068 && (h->root.u.def.section->owner->flags
5069 & (DYNAMIC | BFD_PLUGIN)) == 0)
5070 {
5071 normal_align = h->root.u.def.section->alignment_power;
5072 if (normal_align > symbol_align)
5073 normal_align = symbol_align;
5074 }
5075 else
5076 normal_align = symbol_align;
5077
5078 if (old_alignment)
5079 {
5080 common_align = old_alignment;
5081 common_bfd = old_bfd;
5082 normal_bfd = abfd;
5083 }
5084 else
5085 {
5086 common_align = bfd_log2 (isym->st_value);
5087 common_bfd = abfd;
5088 normal_bfd = old_bfd;
5089 }
5090
5091 if (normal_align < common_align)
5092 {
5093 /* PR binutils/2735 */
5094 if (normal_bfd == NULL)
5095 _bfd_error_handler
5096 /* xgettext:c-format */
5097 (_("warning: alignment %u of common symbol `%s' in %pB is"
5098 " greater than the alignment (%u) of its section %pA"),
5099 1 << common_align, name, common_bfd,
5100 1 << normal_align, h->root.u.def.section);
5101 else
5102 _bfd_error_handler
5103 /* xgettext:c-format */
5104 (_("warning: alignment %u of symbol `%s' in %pB"
5105 " is smaller than %u in %pB"),
5106 1 << normal_align, name, normal_bfd,
5107 1 << common_align, common_bfd);
5108 }
5109 }
5110
5111 /* Remember the symbol size if it isn't undefined. */
5112 if (isym->st_size != 0
5113 && isym->st_shndx != SHN_UNDEF
5114 && (definition || h->size == 0))
5115 {
5116 if (h->size != 0
5117 && h->size != isym->st_size
5118 && ! size_change_ok)
5119 _bfd_error_handler
5120 /* xgettext:c-format */
5121 (_("warning: size of symbol `%s' changed"
5122 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5123 name, (uint64_t) h->size, old_bfd,
5124 (uint64_t) isym->st_size, abfd);
5125
5126 h->size = isym->st_size;
5127 }
5128
5129 /* If this is a common symbol, then we always want H->SIZE
5130 to be the size of the common symbol. The code just above
5131 won't fix the size if a common symbol becomes larger. We
5132 don't warn about a size change here, because that is
5133 covered by --warn-common. Allow changes between different
5134 function types. */
5135 if (h->root.type == bfd_link_hash_common)
5136 h->size = h->root.u.c.size;
5137
5138 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5139 && ((definition && !new_weak)
5140 || (old_weak && h->root.type == bfd_link_hash_common)
5141 || h->type == STT_NOTYPE))
5142 {
5143 unsigned int type = ELF_ST_TYPE (isym->st_info);
5144
5145 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5146 symbol. */
5147 if (type == STT_GNU_IFUNC
5148 && (abfd->flags & DYNAMIC) != 0)
5149 type = STT_FUNC;
5150
5151 if (h->type != type)
5152 {
5153 if (h->type != STT_NOTYPE && ! type_change_ok)
5154 /* xgettext:c-format */
5155 _bfd_error_handler
5156 (_("warning: type of symbol `%s' changed"
5157 " from %d to %d in %pB"),
5158 name, h->type, type, abfd);
5159
5160 h->type = type;
5161 }
5162 }
5163
5164 /* Merge st_other field. */
5165 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5166
5167 /* We don't want to make debug symbol dynamic. */
5168 if (definition
5169 && (sec->flags & SEC_DEBUGGING)
5170 && !bfd_link_relocatable (info))
5171 dynsym = FALSE;
5172
5173 if (definition)
5174 {
5175 h->target_internal = isym->st_target_internal;
5176 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5177 }
5178
5179 if (definition && !dynamic)
5180 {
5181 char *p = strchr (name, ELF_VER_CHR);
5182 if (p != NULL && p[1] != ELF_VER_CHR)
5183 {
5184 /* Queue non-default versions so that .symver x, x@FOO
5185 aliases can be checked. */
5186 if (!nondeflt_vers)
5187 {
5188 size_t amt = ((isymend - isym + 1)
5189 * sizeof (struct elf_link_hash_entry *));
5190 nondeflt_vers
5191 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5192 if (!nondeflt_vers)
5193 goto error_free_vers;
5194 }
5195 nondeflt_vers[nondeflt_vers_cnt++] = h;
5196 }
5197 }
5198
5199 if (dynsym && (abfd->flags & BFD_PLUGIN) == 0 && h->dynindx == -1)
5200 {
5201 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5202 goto error_free_vers;
5203 if (h->is_weakalias
5204 && weakdef (h)->dynindx == -1)
5205 {
5206 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5207 goto error_free_vers;
5208 }
5209 }
5210 else if (h->dynindx != -1)
5211 /* If the symbol already has a dynamic index, but
5212 visibility says it should not be visible, turn it into
5213 a local symbol. */
5214 switch (ELF_ST_VISIBILITY (h->other))
5215 {
5216 case STV_INTERNAL:
5217 case STV_HIDDEN:
5218 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5219 dynsym = FALSE;
5220 break;
5221 }
5222
5223 if (!add_needed
5224 && matched
5225 && definition
5226 && ((dynsym
5227 && h->ref_regular_nonweak)
5228 || (h->ref_dynamic_nonweak
5229 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5230 && !on_needed_list (elf_dt_name (abfd),
5231 htab->needed, NULL))))
5232 {
5233 const char *soname = elf_dt_name (abfd);
5234
5235 info->callbacks->minfo ("%!", soname, old_bfd,
5236 h->root.root.string);
5237
5238 /* A symbol from a library loaded via DT_NEEDED of some
5239 other library is referenced by a regular object.
5240 Add a DT_NEEDED entry for it. Issue an error if
5241 --no-add-needed is used and the reference was not
5242 a weak one. */
5243 if (old_bfd != NULL
5244 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5245 {
5246 _bfd_error_handler
5247 /* xgettext:c-format */
5248 (_("%pB: undefined reference to symbol '%s'"),
5249 old_bfd, name);
5250 bfd_set_error (bfd_error_missing_dso);
5251 goto error_free_vers;
5252 }
5253
5254 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5255 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5256
5257 /* Create dynamic sections for backends that require
5258 that be done before setup_gnu_properties. */
5259 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5260 return FALSE;
5261 add_needed = TRUE;
5262 }
5263 }
5264 }
5265
5266 if (info->lto_plugin_active
5267 && !bfd_link_relocatable (info)
5268 && (abfd->flags & BFD_PLUGIN) == 0
5269 && !just_syms
5270 && extsymcount)
5271 {
5272 int r_sym_shift;
5273
5274 if (bed->s->arch_size == 32)
5275 r_sym_shift = 8;
5276 else
5277 r_sym_shift = 32;
5278
5279 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5280 referenced in regular objects so that linker plugin will get
5281 the correct symbol resolution. */
5282
5283 sym_hash = elf_sym_hashes (abfd);
5284 for (s = abfd->sections; s != NULL; s = s->next)
5285 {
5286 Elf_Internal_Rela *internal_relocs;
5287 Elf_Internal_Rela *rel, *relend;
5288
5289 /* Don't check relocations in excluded sections. */
5290 if ((s->flags & SEC_RELOC) == 0
5291 || s->reloc_count == 0
5292 || (s->flags & SEC_EXCLUDE) != 0
5293 || ((info->strip == strip_all
5294 || info->strip == strip_debugger)
5295 && (s->flags & SEC_DEBUGGING) != 0))
5296 continue;
5297
5298 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5299 NULL,
5300 info->keep_memory);
5301 if (internal_relocs == NULL)
5302 goto error_free_vers;
5303
5304 rel = internal_relocs;
5305 relend = rel + s->reloc_count;
5306 for ( ; rel < relend; rel++)
5307 {
5308 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5309 struct elf_link_hash_entry *h;
5310
5311 /* Skip local symbols. */
5312 if (r_symndx < extsymoff)
5313 continue;
5314
5315 h = sym_hash[r_symndx - extsymoff];
5316 if (h != NULL)
5317 h->root.non_ir_ref_regular = 1;
5318 }
5319
5320 if (elf_section_data (s)->relocs != internal_relocs)
5321 free (internal_relocs);
5322 }
5323 }
5324
5325 free (extversym);
5326 extversym = NULL;
5327 free (isymbuf);
5328 isymbuf = NULL;
5329
5330 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5331 {
5332 unsigned int i;
5333
5334 /* Restore the symbol table. */
5335 old_ent = (char *) old_tab + tabsize;
5336 memset (elf_sym_hashes (abfd), 0,
5337 extsymcount * sizeof (struct elf_link_hash_entry *));
5338 htab->root.table.table = old_table;
5339 htab->root.table.size = old_size;
5340 htab->root.table.count = old_count;
5341 memcpy (htab->root.table.table, old_tab, tabsize);
5342 htab->root.undefs = old_undefs;
5343 htab->root.undefs_tail = old_undefs_tail;
5344 if (htab->dynstr != NULL)
5345 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5346 free (old_strtab);
5347 old_strtab = NULL;
5348 for (i = 0; i < htab->root.table.size; i++)
5349 {
5350 struct bfd_hash_entry *p;
5351 struct elf_link_hash_entry *h;
5352 unsigned int non_ir_ref_dynamic;
5353
5354 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5355 {
5356 /* Preserve non_ir_ref_dynamic so that this symbol
5357 will be exported when the dynamic lib becomes needed
5358 in the second pass. */
5359 h = (struct elf_link_hash_entry *) p;
5360 if (h->root.type == bfd_link_hash_warning)
5361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5362 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5363
5364 h = (struct elf_link_hash_entry *) p;
5365 memcpy (h, old_ent, htab->root.table.entsize);
5366 old_ent = (char *) old_ent + htab->root.table.entsize;
5367 if (h->root.type == bfd_link_hash_warning)
5368 {
5369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5370 memcpy (h, old_ent, htab->root.table.entsize);
5371 old_ent = (char *) old_ent + htab->root.table.entsize;
5372 }
5373 if (h->root.type == bfd_link_hash_common)
5374 {
5375 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5376 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5377 }
5378 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5379 }
5380 }
5381
5382 /* Make a special call to the linker "notice" function to
5383 tell it that symbols added for crefs may need to be removed. */
5384 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5385 goto error_free_vers;
5386
5387 free (old_tab);
5388 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5389 alloc_mark);
5390 free (nondeflt_vers);
5391 return TRUE;
5392 }
5393
5394 if (old_tab != NULL)
5395 {
5396 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5397 goto error_free_vers;
5398 free (old_tab);
5399 old_tab = NULL;
5400 }
5401
5402 /* Now that all the symbols from this input file are created, if
5403 not performing a relocatable link, handle .symver foo, foo@BAR
5404 such that any relocs against foo become foo@BAR. */
5405 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5406 {
5407 size_t cnt, symidx;
5408
5409 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5410 {
5411 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5412 char *shortname, *p;
5413 size_t amt;
5414
5415 p = strchr (h->root.root.string, ELF_VER_CHR);
5416 if (p == NULL
5417 || (h->root.type != bfd_link_hash_defined
5418 && h->root.type != bfd_link_hash_defweak))
5419 continue;
5420
5421 amt = p - h->root.root.string;
5422 shortname = (char *) bfd_malloc (amt + 1);
5423 if (!shortname)
5424 goto error_free_vers;
5425 memcpy (shortname, h->root.root.string, amt);
5426 shortname[amt] = '\0';
5427
5428 hi = (struct elf_link_hash_entry *)
5429 bfd_link_hash_lookup (&htab->root, shortname,
5430 FALSE, FALSE, FALSE);
5431 if (hi != NULL
5432 && hi->root.type == h->root.type
5433 && hi->root.u.def.value == h->root.u.def.value
5434 && hi->root.u.def.section == h->root.u.def.section)
5435 {
5436 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5437 hi->root.type = bfd_link_hash_indirect;
5438 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5439 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5440 sym_hash = elf_sym_hashes (abfd);
5441 if (sym_hash)
5442 for (symidx = 0; symidx < extsymcount; ++symidx)
5443 if (sym_hash[symidx] == hi)
5444 {
5445 sym_hash[symidx] = h;
5446 break;
5447 }
5448 }
5449 free (shortname);
5450 }
5451 free (nondeflt_vers);
5452 nondeflt_vers = NULL;
5453 }
5454
5455 /* Now set the alias field correctly for all the weak defined
5456 symbols we found. The only way to do this is to search all the
5457 symbols. Since we only need the information for non functions in
5458 dynamic objects, that's the only time we actually put anything on
5459 the list WEAKS. We need this information so that if a regular
5460 object refers to a symbol defined weakly in a dynamic object, the
5461 real symbol in the dynamic object is also put in the dynamic
5462 symbols; we also must arrange for both symbols to point to the
5463 same memory location. We could handle the general case of symbol
5464 aliasing, but a general symbol alias can only be generated in
5465 assembler code, handling it correctly would be very time
5466 consuming, and other ELF linkers don't handle general aliasing
5467 either. */
5468 if (weaks != NULL)
5469 {
5470 struct elf_link_hash_entry **hpp;
5471 struct elf_link_hash_entry **hppend;
5472 struct elf_link_hash_entry **sorted_sym_hash;
5473 struct elf_link_hash_entry *h;
5474 size_t sym_count, amt;
5475
5476 /* Since we have to search the whole symbol list for each weak
5477 defined symbol, search time for N weak defined symbols will be
5478 O(N^2). Binary search will cut it down to O(NlogN). */
5479 amt = extsymcount * sizeof (*sorted_sym_hash);
5480 sorted_sym_hash = bfd_malloc (amt);
5481 if (sorted_sym_hash == NULL)
5482 goto error_return;
5483 sym_hash = sorted_sym_hash;
5484 hpp = elf_sym_hashes (abfd);
5485 hppend = hpp + extsymcount;
5486 sym_count = 0;
5487 for (; hpp < hppend; hpp++)
5488 {
5489 h = *hpp;
5490 if (h != NULL
5491 && h->root.type == bfd_link_hash_defined
5492 && !bed->is_function_type (h->type))
5493 {
5494 *sym_hash = h;
5495 sym_hash++;
5496 sym_count++;
5497 }
5498 }
5499
5500 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5501 elf_sort_symbol);
5502
5503 while (weaks != NULL)
5504 {
5505 struct elf_link_hash_entry *hlook;
5506 asection *slook;
5507 bfd_vma vlook;
5508 size_t i, j, idx = 0;
5509
5510 hlook = weaks;
5511 weaks = hlook->u.alias;
5512 hlook->u.alias = NULL;
5513
5514 if (hlook->root.type != bfd_link_hash_defined
5515 && hlook->root.type != bfd_link_hash_defweak)
5516 continue;
5517
5518 slook = hlook->root.u.def.section;
5519 vlook = hlook->root.u.def.value;
5520
5521 i = 0;
5522 j = sym_count;
5523 while (i != j)
5524 {
5525 bfd_signed_vma vdiff;
5526 idx = (i + j) / 2;
5527 h = sorted_sym_hash[idx];
5528 vdiff = vlook - h->root.u.def.value;
5529 if (vdiff < 0)
5530 j = idx;
5531 else if (vdiff > 0)
5532 i = idx + 1;
5533 else
5534 {
5535 int sdiff = slook->id - h->root.u.def.section->id;
5536 if (sdiff < 0)
5537 j = idx;
5538 else if (sdiff > 0)
5539 i = idx + 1;
5540 else
5541 break;
5542 }
5543 }
5544
5545 /* We didn't find a value/section match. */
5546 if (i == j)
5547 continue;
5548
5549 /* With multiple aliases, or when the weak symbol is already
5550 strongly defined, we have multiple matching symbols and
5551 the binary search above may land on any of them. Step
5552 one past the matching symbol(s). */
5553 while (++idx != j)
5554 {
5555 h = sorted_sym_hash[idx];
5556 if (h->root.u.def.section != slook
5557 || h->root.u.def.value != vlook)
5558 break;
5559 }
5560
5561 /* Now look back over the aliases. Since we sorted by size
5562 as well as value and section, we'll choose the one with
5563 the largest size. */
5564 while (idx-- != i)
5565 {
5566 h = sorted_sym_hash[idx];
5567
5568 /* Stop if value or section doesn't match. */
5569 if (h->root.u.def.section != slook
5570 || h->root.u.def.value != vlook)
5571 break;
5572 else if (h != hlook)
5573 {
5574 struct elf_link_hash_entry *t;
5575
5576 hlook->u.alias = h;
5577 hlook->is_weakalias = 1;
5578 t = h;
5579 if (t->u.alias != NULL)
5580 while (t->u.alias != h)
5581 t = t->u.alias;
5582 t->u.alias = hlook;
5583
5584 /* If the weak definition is in the list of dynamic
5585 symbols, make sure the real definition is put
5586 there as well. */
5587 if (hlook->dynindx != -1 && h->dynindx == -1)
5588 {
5589 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5590 {
5591 err_free_sym_hash:
5592 free (sorted_sym_hash);
5593 goto error_return;
5594 }
5595 }
5596
5597 /* If the real definition is in the list of dynamic
5598 symbols, make sure the weak definition is put
5599 there as well. If we don't do this, then the
5600 dynamic loader might not merge the entries for the
5601 real definition and the weak definition. */
5602 if (h->dynindx != -1 && hlook->dynindx == -1)
5603 {
5604 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5605 goto err_free_sym_hash;
5606 }
5607 break;
5608 }
5609 }
5610 }
5611
5612 free (sorted_sym_hash);
5613 }
5614
5615 if (bed->check_directives
5616 && !(*bed->check_directives) (abfd, info))
5617 return FALSE;
5618
5619 /* If this is a non-traditional link, try to optimize the handling
5620 of the .stab/.stabstr sections. */
5621 if (! dynamic
5622 && ! info->traditional_format
5623 && is_elf_hash_table (htab)
5624 && (info->strip != strip_all && info->strip != strip_debugger))
5625 {
5626 asection *stabstr;
5627
5628 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5629 if (stabstr != NULL)
5630 {
5631 bfd_size_type string_offset = 0;
5632 asection *stab;
5633
5634 for (stab = abfd->sections; stab; stab = stab->next)
5635 if (CONST_STRNEQ (stab->name, ".stab")
5636 && (!stab->name[5] ||
5637 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5638 && (stab->flags & SEC_MERGE) == 0
5639 && !bfd_is_abs_section (stab->output_section))
5640 {
5641 struct bfd_elf_section_data *secdata;
5642
5643 secdata = elf_section_data (stab);
5644 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5645 stabstr, &secdata->sec_info,
5646 &string_offset))
5647 goto error_return;
5648 if (secdata->sec_info)
5649 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5650 }
5651 }
5652 }
5653
5654 if (dynamic && add_needed)
5655 {
5656 /* Add this bfd to the loaded list. */
5657 struct elf_link_loaded_list *n;
5658
5659 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5660 if (n == NULL)
5661 goto error_return;
5662 n->abfd = abfd;
5663 n->next = htab->dyn_loaded;
5664 htab->dyn_loaded = n;
5665 }
5666 if (dynamic && !add_needed
5667 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5668 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5669
5670 return TRUE;
5671
5672 error_free_vers:
5673 free (old_tab);
5674 free (old_strtab);
5675 free (nondeflt_vers);
5676 free (extversym);
5677 error_free_sym:
5678 free (isymbuf);
5679 error_return:
5680 return FALSE;
5681 }
5682
5683 /* Return the linker hash table entry of a symbol that might be
5684 satisfied by an archive symbol. Return -1 on error. */
5685
5686 struct elf_link_hash_entry *
5687 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5688 struct bfd_link_info *info,
5689 const char *name)
5690 {
5691 struct elf_link_hash_entry *h;
5692 char *p, *copy;
5693 size_t len, first;
5694
5695 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5696 if (h != NULL)
5697 return h;
5698
5699 /* If this is a default version (the name contains @@), look up the
5700 symbol again with only one `@' as well as without the version.
5701 The effect is that references to the symbol with and without the
5702 version will be matched by the default symbol in the archive. */
5703
5704 p = strchr (name, ELF_VER_CHR);
5705 if (p == NULL || p[1] != ELF_VER_CHR)
5706 return h;
5707
5708 /* First check with only one `@'. */
5709 len = strlen (name);
5710 copy = (char *) bfd_alloc (abfd, len);
5711 if (copy == NULL)
5712 return (struct elf_link_hash_entry *) -1;
5713
5714 first = p - name + 1;
5715 memcpy (copy, name, first);
5716 memcpy (copy + first, name + first + 1, len - first);
5717
5718 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5719 if (h == NULL)
5720 {
5721 /* We also need to check references to the symbol without the
5722 version. */
5723 copy[first - 1] = '\0';
5724 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5725 FALSE, FALSE, TRUE);
5726 }
5727
5728 bfd_release (abfd, copy);
5729 return h;
5730 }
5731
5732 /* Add symbols from an ELF archive file to the linker hash table. We
5733 don't use _bfd_generic_link_add_archive_symbols because we need to
5734 handle versioned symbols.
5735
5736 Fortunately, ELF archive handling is simpler than that done by
5737 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5738 oddities. In ELF, if we find a symbol in the archive map, and the
5739 symbol is currently undefined, we know that we must pull in that
5740 object file.
5741
5742 Unfortunately, we do have to make multiple passes over the symbol
5743 table until nothing further is resolved. */
5744
5745 static bfd_boolean
5746 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5747 {
5748 symindex c;
5749 unsigned char *included = NULL;
5750 carsym *symdefs;
5751 bfd_boolean loop;
5752 size_t amt;
5753 const struct elf_backend_data *bed;
5754 struct elf_link_hash_entry * (*archive_symbol_lookup)
5755 (bfd *, struct bfd_link_info *, const char *);
5756
5757 if (! bfd_has_map (abfd))
5758 {
5759 /* An empty archive is a special case. */
5760 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5761 return TRUE;
5762 bfd_set_error (bfd_error_no_armap);
5763 return FALSE;
5764 }
5765
5766 /* Keep track of all symbols we know to be already defined, and all
5767 files we know to be already included. This is to speed up the
5768 second and subsequent passes. */
5769 c = bfd_ardata (abfd)->symdef_count;
5770 if (c == 0)
5771 return TRUE;
5772 amt = c * sizeof (*included);
5773 included = (unsigned char *) bfd_zmalloc (amt);
5774 if (included == NULL)
5775 return FALSE;
5776
5777 symdefs = bfd_ardata (abfd)->symdefs;
5778 bed = get_elf_backend_data (abfd);
5779 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5780
5781 do
5782 {
5783 file_ptr last;
5784 symindex i;
5785 carsym *symdef;
5786 carsym *symdefend;
5787
5788 loop = FALSE;
5789 last = -1;
5790
5791 symdef = symdefs;
5792 symdefend = symdef + c;
5793 for (i = 0; symdef < symdefend; symdef++, i++)
5794 {
5795 struct elf_link_hash_entry *h;
5796 bfd *element;
5797 struct bfd_link_hash_entry *undefs_tail;
5798 symindex mark;
5799
5800 if (included[i])
5801 continue;
5802 if (symdef->file_offset == last)
5803 {
5804 included[i] = TRUE;
5805 continue;
5806 }
5807
5808 h = archive_symbol_lookup (abfd, info, symdef->name);
5809 if (h == (struct elf_link_hash_entry *) -1)
5810 goto error_return;
5811
5812 if (h == NULL)
5813 continue;
5814
5815 if (h->root.type == bfd_link_hash_undefined)
5816 {
5817 /* If the archive element has already been loaded then one
5818 of the symbols defined by that element might have been
5819 made undefined due to being in a discarded section. */
5820 if (h->indx == -3)
5821 continue;
5822 }
5823 else if (h->root.type == bfd_link_hash_common)
5824 {
5825 /* We currently have a common symbol. The archive map contains
5826 a reference to this symbol, so we may want to include it. We
5827 only want to include it however, if this archive element
5828 contains a definition of the symbol, not just another common
5829 declaration of it.
5830
5831 Unfortunately some archivers (including GNU ar) will put
5832 declarations of common symbols into their archive maps, as
5833 well as real definitions, so we cannot just go by the archive
5834 map alone. Instead we must read in the element's symbol
5835 table and check that to see what kind of symbol definition
5836 this is. */
5837 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5838 continue;
5839 }
5840 else
5841 {
5842 if (h->root.type != bfd_link_hash_undefweak)
5843 /* Symbol must be defined. Don't check it again. */
5844 included[i] = TRUE;
5845 continue;
5846 }
5847
5848 /* We need to include this archive member. */
5849 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5850 if (element == NULL)
5851 goto error_return;
5852
5853 if (! bfd_check_format (element, bfd_object))
5854 goto error_return;
5855
5856 undefs_tail = info->hash->undefs_tail;
5857
5858 if (!(*info->callbacks
5859 ->add_archive_element) (info, element, symdef->name, &element))
5860 continue;
5861 if (!bfd_link_add_symbols (element, info))
5862 goto error_return;
5863
5864 /* If there are any new undefined symbols, we need to make
5865 another pass through the archive in order to see whether
5866 they can be defined. FIXME: This isn't perfect, because
5867 common symbols wind up on undefs_tail and because an
5868 undefined symbol which is defined later on in this pass
5869 does not require another pass. This isn't a bug, but it
5870 does make the code less efficient than it could be. */
5871 if (undefs_tail != info->hash->undefs_tail)
5872 loop = TRUE;
5873
5874 /* Look backward to mark all symbols from this object file
5875 which we have already seen in this pass. */
5876 mark = i;
5877 do
5878 {
5879 included[mark] = TRUE;
5880 if (mark == 0)
5881 break;
5882 --mark;
5883 }
5884 while (symdefs[mark].file_offset == symdef->file_offset);
5885
5886 /* We mark subsequent symbols from this object file as we go
5887 on through the loop. */
5888 last = symdef->file_offset;
5889 }
5890 }
5891 while (loop);
5892
5893 free (included);
5894 return TRUE;
5895
5896 error_return:
5897 free (included);
5898 return FALSE;
5899 }
5900
5901 /* Given an ELF BFD, add symbols to the global hash table as
5902 appropriate. */
5903
5904 bfd_boolean
5905 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5906 {
5907 switch (bfd_get_format (abfd))
5908 {
5909 case bfd_object:
5910 return elf_link_add_object_symbols (abfd, info);
5911 case bfd_archive:
5912 return elf_link_add_archive_symbols (abfd, info);
5913 default:
5914 bfd_set_error (bfd_error_wrong_format);
5915 return FALSE;
5916 }
5917 }
5918 \f
5919 struct hash_codes_info
5920 {
5921 unsigned long *hashcodes;
5922 bfd_boolean error;
5923 };
5924
5925 /* This function will be called though elf_link_hash_traverse to store
5926 all hash value of the exported symbols in an array. */
5927
5928 static bfd_boolean
5929 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5930 {
5931 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5932 const char *name;
5933 unsigned long ha;
5934 char *alc = NULL;
5935
5936 /* Ignore indirect symbols. These are added by the versioning code. */
5937 if (h->dynindx == -1)
5938 return TRUE;
5939
5940 name = h->root.root.string;
5941 if (h->versioned >= versioned)
5942 {
5943 char *p = strchr (name, ELF_VER_CHR);
5944 if (p != NULL)
5945 {
5946 alc = (char *) bfd_malloc (p - name + 1);
5947 if (alc == NULL)
5948 {
5949 inf->error = TRUE;
5950 return FALSE;
5951 }
5952 memcpy (alc, name, p - name);
5953 alc[p - name] = '\0';
5954 name = alc;
5955 }
5956 }
5957
5958 /* Compute the hash value. */
5959 ha = bfd_elf_hash (name);
5960
5961 /* Store the found hash value in the array given as the argument. */
5962 *(inf->hashcodes)++ = ha;
5963
5964 /* And store it in the struct so that we can put it in the hash table
5965 later. */
5966 h->u.elf_hash_value = ha;
5967
5968 free (alc);
5969 return TRUE;
5970 }
5971
5972 struct collect_gnu_hash_codes
5973 {
5974 bfd *output_bfd;
5975 const struct elf_backend_data *bed;
5976 unsigned long int nsyms;
5977 unsigned long int maskbits;
5978 unsigned long int *hashcodes;
5979 unsigned long int *hashval;
5980 unsigned long int *indx;
5981 unsigned long int *counts;
5982 bfd_vma *bitmask;
5983 bfd_byte *contents;
5984 bfd_size_type xlat;
5985 long int min_dynindx;
5986 unsigned long int bucketcount;
5987 unsigned long int symindx;
5988 long int local_indx;
5989 long int shift1, shift2;
5990 unsigned long int mask;
5991 bfd_boolean error;
5992 };
5993
5994 /* This function will be called though elf_link_hash_traverse to store
5995 all hash value of the exported symbols in an array. */
5996
5997 static bfd_boolean
5998 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5999 {
6000 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6001 const char *name;
6002 unsigned long ha;
6003 char *alc = NULL;
6004
6005 /* Ignore indirect symbols. These are added by the versioning code. */
6006 if (h->dynindx == -1)
6007 return TRUE;
6008
6009 /* Ignore also local symbols and undefined symbols. */
6010 if (! (*s->bed->elf_hash_symbol) (h))
6011 return TRUE;
6012
6013 name = h->root.root.string;
6014 if (h->versioned >= versioned)
6015 {
6016 char *p = strchr (name, ELF_VER_CHR);
6017 if (p != NULL)
6018 {
6019 alc = (char *) bfd_malloc (p - name + 1);
6020 if (alc == NULL)
6021 {
6022 s->error = TRUE;
6023 return FALSE;
6024 }
6025 memcpy (alc, name, p - name);
6026 alc[p - name] = '\0';
6027 name = alc;
6028 }
6029 }
6030
6031 /* Compute the hash value. */
6032 ha = bfd_elf_gnu_hash (name);
6033
6034 /* Store the found hash value in the array for compute_bucket_count,
6035 and also for .dynsym reordering purposes. */
6036 s->hashcodes[s->nsyms] = ha;
6037 s->hashval[h->dynindx] = ha;
6038 ++s->nsyms;
6039 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6040 s->min_dynindx = h->dynindx;
6041
6042 free (alc);
6043 return TRUE;
6044 }
6045
6046 /* This function will be called though elf_link_hash_traverse to do
6047 final dynamic symbol renumbering in case of .gnu.hash.
6048 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6049 to the translation table. */
6050
6051 static bfd_boolean
6052 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6053 {
6054 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6055 unsigned long int bucket;
6056 unsigned long int val;
6057
6058 /* Ignore indirect symbols. */
6059 if (h->dynindx == -1)
6060 return TRUE;
6061
6062 /* Ignore also local symbols and undefined symbols. */
6063 if (! (*s->bed->elf_hash_symbol) (h))
6064 {
6065 if (h->dynindx >= s->min_dynindx)
6066 {
6067 if (s->bed->record_xhash_symbol != NULL)
6068 {
6069 (*s->bed->record_xhash_symbol) (h, 0);
6070 s->local_indx++;
6071 }
6072 else
6073 h->dynindx = s->local_indx++;
6074 }
6075 return TRUE;
6076 }
6077
6078 bucket = s->hashval[h->dynindx] % s->bucketcount;
6079 val = (s->hashval[h->dynindx] >> s->shift1)
6080 & ((s->maskbits >> s->shift1) - 1);
6081 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6082 s->bitmask[val]
6083 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6084 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6085 if (s->counts[bucket] == 1)
6086 /* Last element terminates the chain. */
6087 val |= 1;
6088 bfd_put_32 (s->output_bfd, val,
6089 s->contents + (s->indx[bucket] - s->symindx) * 4);
6090 --s->counts[bucket];
6091 if (s->bed->record_xhash_symbol != NULL)
6092 {
6093 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6094
6095 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6096 }
6097 else
6098 h->dynindx = s->indx[bucket]++;
6099 return TRUE;
6100 }
6101
6102 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6103
6104 bfd_boolean
6105 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6106 {
6107 return !(h->forced_local
6108 || h->root.type == bfd_link_hash_undefined
6109 || h->root.type == bfd_link_hash_undefweak
6110 || ((h->root.type == bfd_link_hash_defined
6111 || h->root.type == bfd_link_hash_defweak)
6112 && h->root.u.def.section->output_section == NULL));
6113 }
6114
6115 /* Array used to determine the number of hash table buckets to use
6116 based on the number of symbols there are. If there are fewer than
6117 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6118 fewer than 37 we use 17 buckets, and so forth. We never use more
6119 than 32771 buckets. */
6120
6121 static const size_t elf_buckets[] =
6122 {
6123 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6124 16411, 32771, 0
6125 };
6126
6127 /* Compute bucket count for hashing table. We do not use a static set
6128 of possible tables sizes anymore. Instead we determine for all
6129 possible reasonable sizes of the table the outcome (i.e., the
6130 number of collisions etc) and choose the best solution. The
6131 weighting functions are not too simple to allow the table to grow
6132 without bounds. Instead one of the weighting factors is the size.
6133 Therefore the result is always a good payoff between few collisions
6134 (= short chain lengths) and table size. */
6135 static size_t
6136 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6137 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6138 unsigned long int nsyms,
6139 int gnu_hash)
6140 {
6141 size_t best_size = 0;
6142 unsigned long int i;
6143
6144 /* We have a problem here. The following code to optimize the table
6145 size requires an integer type with more the 32 bits. If
6146 BFD_HOST_U_64_BIT is set we know about such a type. */
6147 #ifdef BFD_HOST_U_64_BIT
6148 if (info->optimize)
6149 {
6150 size_t minsize;
6151 size_t maxsize;
6152 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6153 bfd *dynobj = elf_hash_table (info)->dynobj;
6154 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6155 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6156 unsigned long int *counts;
6157 bfd_size_type amt;
6158 unsigned int no_improvement_count = 0;
6159
6160 /* Possible optimization parameters: if we have NSYMS symbols we say
6161 that the hashing table must at least have NSYMS/4 and at most
6162 2*NSYMS buckets. */
6163 minsize = nsyms / 4;
6164 if (minsize == 0)
6165 minsize = 1;
6166 best_size = maxsize = nsyms * 2;
6167 if (gnu_hash)
6168 {
6169 if (minsize < 2)
6170 minsize = 2;
6171 if ((best_size & 31) == 0)
6172 ++best_size;
6173 }
6174
6175 /* Create array where we count the collisions in. We must use bfd_malloc
6176 since the size could be large. */
6177 amt = maxsize;
6178 amt *= sizeof (unsigned long int);
6179 counts = (unsigned long int *) bfd_malloc (amt);
6180 if (counts == NULL)
6181 return 0;
6182
6183 /* Compute the "optimal" size for the hash table. The criteria is a
6184 minimal chain length. The minor criteria is (of course) the size
6185 of the table. */
6186 for (i = minsize; i < maxsize; ++i)
6187 {
6188 /* Walk through the array of hashcodes and count the collisions. */
6189 BFD_HOST_U_64_BIT max;
6190 unsigned long int j;
6191 unsigned long int fact;
6192
6193 if (gnu_hash && (i & 31) == 0)
6194 continue;
6195
6196 memset (counts, '\0', i * sizeof (unsigned long int));
6197
6198 /* Determine how often each hash bucket is used. */
6199 for (j = 0; j < nsyms; ++j)
6200 ++counts[hashcodes[j] % i];
6201
6202 /* For the weight function we need some information about the
6203 pagesize on the target. This is information need not be 100%
6204 accurate. Since this information is not available (so far) we
6205 define it here to a reasonable default value. If it is crucial
6206 to have a better value some day simply define this value. */
6207 # ifndef BFD_TARGET_PAGESIZE
6208 # define BFD_TARGET_PAGESIZE (4096)
6209 # endif
6210
6211 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6212 and the chains. */
6213 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6214
6215 # if 1
6216 /* Variant 1: optimize for short chains. We add the squares
6217 of all the chain lengths (which favors many small chain
6218 over a few long chains). */
6219 for (j = 0; j < i; ++j)
6220 max += counts[j] * counts[j];
6221
6222 /* This adds penalties for the overall size of the table. */
6223 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6224 max *= fact * fact;
6225 # else
6226 /* Variant 2: Optimize a lot more for small table. Here we
6227 also add squares of the size but we also add penalties for
6228 empty slots (the +1 term). */
6229 for (j = 0; j < i; ++j)
6230 max += (1 + counts[j]) * (1 + counts[j]);
6231
6232 /* The overall size of the table is considered, but not as
6233 strong as in variant 1, where it is squared. */
6234 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6235 max *= fact;
6236 # endif
6237
6238 /* Compare with current best results. */
6239 if (max < best_chlen)
6240 {
6241 best_chlen = max;
6242 best_size = i;
6243 no_improvement_count = 0;
6244 }
6245 /* PR 11843: Avoid futile long searches for the best bucket size
6246 when there are a large number of symbols. */
6247 else if (++no_improvement_count == 100)
6248 break;
6249 }
6250
6251 free (counts);
6252 }
6253 else
6254 #endif /* defined (BFD_HOST_U_64_BIT) */
6255 {
6256 /* This is the fallback solution if no 64bit type is available or if we
6257 are not supposed to spend much time on optimizations. We select the
6258 bucket count using a fixed set of numbers. */
6259 for (i = 0; elf_buckets[i] != 0; i++)
6260 {
6261 best_size = elf_buckets[i];
6262 if (nsyms < elf_buckets[i + 1])
6263 break;
6264 }
6265 if (gnu_hash && best_size < 2)
6266 best_size = 2;
6267 }
6268
6269 return best_size;
6270 }
6271
6272 /* Size any SHT_GROUP section for ld -r. */
6273
6274 bfd_boolean
6275 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6276 {
6277 bfd *ibfd;
6278 asection *s;
6279
6280 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6281 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6282 && (s = ibfd->sections) != NULL
6283 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6284 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6285 return FALSE;
6286 return TRUE;
6287 }
6288
6289 /* Set a default stack segment size. The value in INFO wins. If it
6290 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6291 undefined it is initialized. */
6292
6293 bfd_boolean
6294 bfd_elf_stack_segment_size (bfd *output_bfd,
6295 struct bfd_link_info *info,
6296 const char *legacy_symbol,
6297 bfd_vma default_size)
6298 {
6299 struct elf_link_hash_entry *h = NULL;
6300
6301 /* Look for legacy symbol. */
6302 if (legacy_symbol)
6303 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6304 FALSE, FALSE, FALSE);
6305 if (h && (h->root.type == bfd_link_hash_defined
6306 || h->root.type == bfd_link_hash_defweak)
6307 && h->def_regular
6308 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6309 {
6310 /* The symbol has no type if specified on the command line. */
6311 h->type = STT_OBJECT;
6312 if (info->stacksize)
6313 /* xgettext:c-format */
6314 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6315 output_bfd, legacy_symbol);
6316 else if (h->root.u.def.section != bfd_abs_section_ptr)
6317 /* xgettext:c-format */
6318 _bfd_error_handler (_("%pB: %s not absolute"),
6319 output_bfd, legacy_symbol);
6320 else
6321 info->stacksize = h->root.u.def.value;
6322 }
6323
6324 if (!info->stacksize)
6325 /* If the user didn't set a size, or explicitly inhibit the
6326 size, set it now. */
6327 info->stacksize = default_size;
6328
6329 /* Provide the legacy symbol, if it is referenced. */
6330 if (h && (h->root.type == bfd_link_hash_undefined
6331 || h->root.type == bfd_link_hash_undefweak))
6332 {
6333 struct bfd_link_hash_entry *bh = NULL;
6334
6335 if (!(_bfd_generic_link_add_one_symbol
6336 (info, output_bfd, legacy_symbol,
6337 BSF_GLOBAL, bfd_abs_section_ptr,
6338 info->stacksize >= 0 ? info->stacksize : 0,
6339 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6340 return FALSE;
6341
6342 h = (struct elf_link_hash_entry *) bh;
6343 h->def_regular = 1;
6344 h->type = STT_OBJECT;
6345 }
6346
6347 return TRUE;
6348 }
6349
6350 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6351
6352 struct elf_gc_sweep_symbol_info
6353 {
6354 struct bfd_link_info *info;
6355 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6356 bfd_boolean);
6357 };
6358
6359 static bfd_boolean
6360 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6361 {
6362 if (!h->mark
6363 && (((h->root.type == bfd_link_hash_defined
6364 || h->root.type == bfd_link_hash_defweak)
6365 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6366 && h->root.u.def.section->gc_mark))
6367 || h->root.type == bfd_link_hash_undefined
6368 || h->root.type == bfd_link_hash_undefweak))
6369 {
6370 struct elf_gc_sweep_symbol_info *inf;
6371
6372 inf = (struct elf_gc_sweep_symbol_info *) data;
6373 (*inf->hide_symbol) (inf->info, h, TRUE);
6374 h->def_regular = 0;
6375 h->ref_regular = 0;
6376 h->ref_regular_nonweak = 0;
6377 }
6378
6379 return TRUE;
6380 }
6381
6382 /* Set up the sizes and contents of the ELF dynamic sections. This is
6383 called by the ELF linker emulation before_allocation routine. We
6384 must set the sizes of the sections before the linker sets the
6385 addresses of the various sections. */
6386
6387 bfd_boolean
6388 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6389 const char *soname,
6390 const char *rpath,
6391 const char *filter_shlib,
6392 const char *audit,
6393 const char *depaudit,
6394 const char * const *auxiliary_filters,
6395 struct bfd_link_info *info,
6396 asection **sinterpptr)
6397 {
6398 bfd *dynobj;
6399 const struct elf_backend_data *bed;
6400
6401 *sinterpptr = NULL;
6402
6403 if (!is_elf_hash_table (info->hash))
6404 return TRUE;
6405
6406 dynobj = elf_hash_table (info)->dynobj;
6407
6408 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6409 {
6410 struct bfd_elf_version_tree *verdefs;
6411 struct elf_info_failed asvinfo;
6412 struct bfd_elf_version_tree *t;
6413 struct bfd_elf_version_expr *d;
6414 asection *s;
6415 size_t soname_indx;
6416
6417 /* If we are supposed to export all symbols into the dynamic symbol
6418 table (this is not the normal case), then do so. */
6419 if (info->export_dynamic
6420 || (bfd_link_executable (info) && info->dynamic))
6421 {
6422 struct elf_info_failed eif;
6423
6424 eif.info = info;
6425 eif.failed = FALSE;
6426 elf_link_hash_traverse (elf_hash_table (info),
6427 _bfd_elf_export_symbol,
6428 &eif);
6429 if (eif.failed)
6430 return FALSE;
6431 }
6432
6433 if (soname != NULL)
6434 {
6435 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6436 soname, TRUE);
6437 if (soname_indx == (size_t) -1
6438 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6439 return FALSE;
6440 }
6441 else
6442 soname_indx = (size_t) -1;
6443
6444 /* Make all global versions with definition. */
6445 for (t = info->version_info; t != NULL; t = t->next)
6446 for (d = t->globals.list; d != NULL; d = d->next)
6447 if (!d->symver && d->literal)
6448 {
6449 const char *verstr, *name;
6450 size_t namelen, verlen, newlen;
6451 char *newname, *p, leading_char;
6452 struct elf_link_hash_entry *newh;
6453
6454 leading_char = bfd_get_symbol_leading_char (output_bfd);
6455 name = d->pattern;
6456 namelen = strlen (name) + (leading_char != '\0');
6457 verstr = t->name;
6458 verlen = strlen (verstr);
6459 newlen = namelen + verlen + 3;
6460
6461 newname = (char *) bfd_malloc (newlen);
6462 if (newname == NULL)
6463 return FALSE;
6464 newname[0] = leading_char;
6465 memcpy (newname + (leading_char != '\0'), name, namelen);
6466
6467 /* Check the hidden versioned definition. */
6468 p = newname + namelen;
6469 *p++ = ELF_VER_CHR;
6470 memcpy (p, verstr, verlen + 1);
6471 newh = elf_link_hash_lookup (elf_hash_table (info),
6472 newname, FALSE, FALSE,
6473 FALSE);
6474 if (newh == NULL
6475 || (newh->root.type != bfd_link_hash_defined
6476 && newh->root.type != bfd_link_hash_defweak))
6477 {
6478 /* Check the default versioned definition. */
6479 *p++ = ELF_VER_CHR;
6480 memcpy (p, verstr, verlen + 1);
6481 newh = elf_link_hash_lookup (elf_hash_table (info),
6482 newname, FALSE, FALSE,
6483 FALSE);
6484 }
6485 free (newname);
6486
6487 /* Mark this version if there is a definition and it is
6488 not defined in a shared object. */
6489 if (newh != NULL
6490 && !newh->def_dynamic
6491 && (newh->root.type == bfd_link_hash_defined
6492 || newh->root.type == bfd_link_hash_defweak))
6493 d->symver = 1;
6494 }
6495
6496 /* Attach all the symbols to their version information. */
6497 asvinfo.info = info;
6498 asvinfo.failed = FALSE;
6499
6500 elf_link_hash_traverse (elf_hash_table (info),
6501 _bfd_elf_link_assign_sym_version,
6502 &asvinfo);
6503 if (asvinfo.failed)
6504 return FALSE;
6505
6506 if (!info->allow_undefined_version)
6507 {
6508 /* Check if all global versions have a definition. */
6509 bfd_boolean all_defined = TRUE;
6510 for (t = info->version_info; t != NULL; t = t->next)
6511 for (d = t->globals.list; d != NULL; d = d->next)
6512 if (d->literal && !d->symver && !d->script)
6513 {
6514 _bfd_error_handler
6515 (_("%s: undefined version: %s"),
6516 d->pattern, t->name);
6517 all_defined = FALSE;
6518 }
6519
6520 if (!all_defined)
6521 {
6522 bfd_set_error (bfd_error_bad_value);
6523 return FALSE;
6524 }
6525 }
6526
6527 /* Set up the version definition section. */
6528 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6529 BFD_ASSERT (s != NULL);
6530
6531 /* We may have created additional version definitions if we are
6532 just linking a regular application. */
6533 verdefs = info->version_info;
6534
6535 /* Skip anonymous version tag. */
6536 if (verdefs != NULL && verdefs->vernum == 0)
6537 verdefs = verdefs->next;
6538
6539 if (verdefs == NULL && !info->create_default_symver)
6540 s->flags |= SEC_EXCLUDE;
6541 else
6542 {
6543 unsigned int cdefs;
6544 bfd_size_type size;
6545 bfd_byte *p;
6546 Elf_Internal_Verdef def;
6547 Elf_Internal_Verdaux defaux;
6548 struct bfd_link_hash_entry *bh;
6549 struct elf_link_hash_entry *h;
6550 const char *name;
6551
6552 cdefs = 0;
6553 size = 0;
6554
6555 /* Make space for the base version. */
6556 size += sizeof (Elf_External_Verdef);
6557 size += sizeof (Elf_External_Verdaux);
6558 ++cdefs;
6559
6560 /* Make space for the default version. */
6561 if (info->create_default_symver)
6562 {
6563 size += sizeof (Elf_External_Verdef);
6564 ++cdefs;
6565 }
6566
6567 for (t = verdefs; t != NULL; t = t->next)
6568 {
6569 struct bfd_elf_version_deps *n;
6570
6571 /* Don't emit base version twice. */
6572 if (t->vernum == 0)
6573 continue;
6574
6575 size += sizeof (Elf_External_Verdef);
6576 size += sizeof (Elf_External_Verdaux);
6577 ++cdefs;
6578
6579 for (n = t->deps; n != NULL; n = n->next)
6580 size += sizeof (Elf_External_Verdaux);
6581 }
6582
6583 s->size = size;
6584 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6585 if (s->contents == NULL && s->size != 0)
6586 return FALSE;
6587
6588 /* Fill in the version definition section. */
6589
6590 p = s->contents;
6591
6592 def.vd_version = VER_DEF_CURRENT;
6593 def.vd_flags = VER_FLG_BASE;
6594 def.vd_ndx = 1;
6595 def.vd_cnt = 1;
6596 if (info->create_default_symver)
6597 {
6598 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6599 def.vd_next = sizeof (Elf_External_Verdef);
6600 }
6601 else
6602 {
6603 def.vd_aux = sizeof (Elf_External_Verdef);
6604 def.vd_next = (sizeof (Elf_External_Verdef)
6605 + sizeof (Elf_External_Verdaux));
6606 }
6607
6608 if (soname_indx != (size_t) -1)
6609 {
6610 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6611 soname_indx);
6612 def.vd_hash = bfd_elf_hash (soname);
6613 defaux.vda_name = soname_indx;
6614 name = soname;
6615 }
6616 else
6617 {
6618 size_t indx;
6619
6620 name = lbasename (bfd_get_filename (output_bfd));
6621 def.vd_hash = bfd_elf_hash (name);
6622 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6623 name, FALSE);
6624 if (indx == (size_t) -1)
6625 return FALSE;
6626 defaux.vda_name = indx;
6627 }
6628 defaux.vda_next = 0;
6629
6630 _bfd_elf_swap_verdef_out (output_bfd, &def,
6631 (Elf_External_Verdef *) p);
6632 p += sizeof (Elf_External_Verdef);
6633 if (info->create_default_symver)
6634 {
6635 /* Add a symbol representing this version. */
6636 bh = NULL;
6637 if (! (_bfd_generic_link_add_one_symbol
6638 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6639 0, NULL, FALSE,
6640 get_elf_backend_data (dynobj)->collect, &bh)))
6641 return FALSE;
6642 h = (struct elf_link_hash_entry *) bh;
6643 h->non_elf = 0;
6644 h->def_regular = 1;
6645 h->type = STT_OBJECT;
6646 h->verinfo.vertree = NULL;
6647
6648 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6649 return FALSE;
6650
6651 /* Create a duplicate of the base version with the same
6652 aux block, but different flags. */
6653 def.vd_flags = 0;
6654 def.vd_ndx = 2;
6655 def.vd_aux = sizeof (Elf_External_Verdef);
6656 if (verdefs)
6657 def.vd_next = (sizeof (Elf_External_Verdef)
6658 + sizeof (Elf_External_Verdaux));
6659 else
6660 def.vd_next = 0;
6661 _bfd_elf_swap_verdef_out (output_bfd, &def,
6662 (Elf_External_Verdef *) p);
6663 p += sizeof (Elf_External_Verdef);
6664 }
6665 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6666 (Elf_External_Verdaux *) p);
6667 p += sizeof (Elf_External_Verdaux);
6668
6669 for (t = verdefs; t != NULL; t = t->next)
6670 {
6671 unsigned int cdeps;
6672 struct bfd_elf_version_deps *n;
6673
6674 /* Don't emit the base version twice. */
6675 if (t->vernum == 0)
6676 continue;
6677
6678 cdeps = 0;
6679 for (n = t->deps; n != NULL; n = n->next)
6680 ++cdeps;
6681
6682 /* Add a symbol representing this version. */
6683 bh = NULL;
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6686 0, NULL, FALSE,
6687 get_elf_backend_data (dynobj)->collect, &bh)))
6688 return FALSE;
6689 h = (struct elf_link_hash_entry *) bh;
6690 h->non_elf = 0;
6691 h->def_regular = 1;
6692 h->type = STT_OBJECT;
6693 h->verinfo.vertree = t;
6694
6695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6696 return FALSE;
6697
6698 def.vd_version = VER_DEF_CURRENT;
6699 def.vd_flags = 0;
6700 if (t->globals.list == NULL
6701 && t->locals.list == NULL
6702 && ! t->used)
6703 def.vd_flags |= VER_FLG_WEAK;
6704 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6705 def.vd_cnt = cdeps + 1;
6706 def.vd_hash = bfd_elf_hash (t->name);
6707 def.vd_aux = sizeof (Elf_External_Verdef);
6708 def.vd_next = 0;
6709
6710 /* If a basever node is next, it *must* be the last node in
6711 the chain, otherwise Verdef construction breaks. */
6712 if (t->next != NULL && t->next->vernum == 0)
6713 BFD_ASSERT (t->next->next == NULL);
6714
6715 if (t->next != NULL && t->next->vernum != 0)
6716 def.vd_next = (sizeof (Elf_External_Verdef)
6717 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6718
6719 _bfd_elf_swap_verdef_out (output_bfd, &def,
6720 (Elf_External_Verdef *) p);
6721 p += sizeof (Elf_External_Verdef);
6722
6723 defaux.vda_name = h->dynstr_index;
6724 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6725 h->dynstr_index);
6726 defaux.vda_next = 0;
6727 if (t->deps != NULL)
6728 defaux.vda_next = sizeof (Elf_External_Verdaux);
6729 t->name_indx = defaux.vda_name;
6730
6731 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6732 (Elf_External_Verdaux *) p);
6733 p += sizeof (Elf_External_Verdaux);
6734
6735 for (n = t->deps; n != NULL; n = n->next)
6736 {
6737 if (n->version_needed == NULL)
6738 {
6739 /* This can happen if there was an error in the
6740 version script. */
6741 defaux.vda_name = 0;
6742 }
6743 else
6744 {
6745 defaux.vda_name = n->version_needed->name_indx;
6746 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6747 defaux.vda_name);
6748 }
6749 if (n->next == NULL)
6750 defaux.vda_next = 0;
6751 else
6752 defaux.vda_next = sizeof (Elf_External_Verdaux);
6753
6754 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6755 (Elf_External_Verdaux *) p);
6756 p += sizeof (Elf_External_Verdaux);
6757 }
6758 }
6759
6760 elf_tdata (output_bfd)->cverdefs = cdefs;
6761 }
6762 }
6763
6764 bed = get_elf_backend_data (output_bfd);
6765
6766 if (info->gc_sections && bed->can_gc_sections)
6767 {
6768 struct elf_gc_sweep_symbol_info sweep_info;
6769
6770 /* Remove the symbols that were in the swept sections from the
6771 dynamic symbol table. */
6772 sweep_info.info = info;
6773 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6774 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6775 &sweep_info);
6776 }
6777
6778 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6779 {
6780 asection *s;
6781 struct elf_find_verdep_info sinfo;
6782
6783 /* Work out the size of the version reference section. */
6784
6785 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6786 BFD_ASSERT (s != NULL);
6787
6788 sinfo.info = info;
6789 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6790 if (sinfo.vers == 0)
6791 sinfo.vers = 1;
6792 sinfo.failed = FALSE;
6793
6794 elf_link_hash_traverse (elf_hash_table (info),
6795 _bfd_elf_link_find_version_dependencies,
6796 &sinfo);
6797 if (sinfo.failed)
6798 return FALSE;
6799
6800 if (elf_tdata (output_bfd)->verref == NULL)
6801 s->flags |= SEC_EXCLUDE;
6802 else
6803 {
6804 Elf_Internal_Verneed *vn;
6805 unsigned int size;
6806 unsigned int crefs;
6807 bfd_byte *p;
6808
6809 /* Build the version dependency section. */
6810 size = 0;
6811 crefs = 0;
6812 for (vn = elf_tdata (output_bfd)->verref;
6813 vn != NULL;
6814 vn = vn->vn_nextref)
6815 {
6816 Elf_Internal_Vernaux *a;
6817
6818 size += sizeof (Elf_External_Verneed);
6819 ++crefs;
6820 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6821 size += sizeof (Elf_External_Vernaux);
6822 }
6823
6824 s->size = size;
6825 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6826 if (s->contents == NULL)
6827 return FALSE;
6828
6829 p = s->contents;
6830 for (vn = elf_tdata (output_bfd)->verref;
6831 vn != NULL;
6832 vn = vn->vn_nextref)
6833 {
6834 unsigned int caux;
6835 Elf_Internal_Vernaux *a;
6836 size_t indx;
6837
6838 caux = 0;
6839 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6840 ++caux;
6841
6842 vn->vn_version = VER_NEED_CURRENT;
6843 vn->vn_cnt = caux;
6844 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6845 elf_dt_name (vn->vn_bfd) != NULL
6846 ? elf_dt_name (vn->vn_bfd)
6847 : lbasename (bfd_get_filename
6848 (vn->vn_bfd)),
6849 FALSE);
6850 if (indx == (size_t) -1)
6851 return FALSE;
6852 vn->vn_file = indx;
6853 vn->vn_aux = sizeof (Elf_External_Verneed);
6854 if (vn->vn_nextref == NULL)
6855 vn->vn_next = 0;
6856 else
6857 vn->vn_next = (sizeof (Elf_External_Verneed)
6858 + caux * sizeof (Elf_External_Vernaux));
6859
6860 _bfd_elf_swap_verneed_out (output_bfd, vn,
6861 (Elf_External_Verneed *) p);
6862 p += sizeof (Elf_External_Verneed);
6863
6864 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6865 {
6866 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6868 a->vna_nodename, FALSE);
6869 if (indx == (size_t) -1)
6870 return FALSE;
6871 a->vna_name = indx;
6872 if (a->vna_nextptr == NULL)
6873 a->vna_next = 0;
6874 else
6875 a->vna_next = sizeof (Elf_External_Vernaux);
6876
6877 _bfd_elf_swap_vernaux_out (output_bfd, a,
6878 (Elf_External_Vernaux *) p);
6879 p += sizeof (Elf_External_Vernaux);
6880 }
6881 }
6882
6883 elf_tdata (output_bfd)->cverrefs = crefs;
6884 }
6885 }
6886
6887 /* Any syms created from now on start with -1 in
6888 got.refcount/offset and plt.refcount/offset. */
6889 elf_hash_table (info)->init_got_refcount
6890 = elf_hash_table (info)->init_got_offset;
6891 elf_hash_table (info)->init_plt_refcount
6892 = elf_hash_table (info)->init_plt_offset;
6893
6894 if (bfd_link_relocatable (info)
6895 && !_bfd_elf_size_group_sections (info))
6896 return FALSE;
6897
6898 /* The backend may have to create some sections regardless of whether
6899 we're dynamic or not. */
6900 if (bed->elf_backend_always_size_sections
6901 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6902 return FALSE;
6903
6904 /* Determine any GNU_STACK segment requirements, after the backend
6905 has had a chance to set a default segment size. */
6906 if (info->execstack)
6907 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6908 else if (info->noexecstack)
6909 elf_stack_flags (output_bfd) = PF_R | PF_W;
6910 else
6911 {
6912 bfd *inputobj;
6913 asection *notesec = NULL;
6914 int exec = 0;
6915
6916 for (inputobj = info->input_bfds;
6917 inputobj;
6918 inputobj = inputobj->link.next)
6919 {
6920 asection *s;
6921
6922 if (inputobj->flags
6923 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6924 continue;
6925 s = inputobj->sections;
6926 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6927 continue;
6928
6929 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6930 if (s)
6931 {
6932 if (s->flags & SEC_CODE)
6933 exec = PF_X;
6934 notesec = s;
6935 }
6936 else if (bed->default_execstack)
6937 exec = PF_X;
6938 }
6939 if (notesec || info->stacksize > 0)
6940 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6941 if (notesec && exec && bfd_link_relocatable (info)
6942 && notesec->output_section != bfd_abs_section_ptr)
6943 notesec->output_section->flags |= SEC_CODE;
6944 }
6945
6946 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6947 {
6948 struct elf_info_failed eif;
6949 struct elf_link_hash_entry *h;
6950 asection *dynstr;
6951 asection *s;
6952
6953 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6954 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6955
6956 if (info->symbolic)
6957 {
6958 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6959 return FALSE;
6960 info->flags |= DF_SYMBOLIC;
6961 }
6962
6963 if (rpath != NULL)
6964 {
6965 size_t indx;
6966 bfd_vma tag;
6967
6968 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6969 TRUE);
6970 if (indx == (size_t) -1)
6971 return FALSE;
6972
6973 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6974 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6975 return FALSE;
6976 }
6977
6978 if (filter_shlib != NULL)
6979 {
6980 size_t indx;
6981
6982 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6983 filter_shlib, TRUE);
6984 if (indx == (size_t) -1
6985 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6986 return FALSE;
6987 }
6988
6989 if (auxiliary_filters != NULL)
6990 {
6991 const char * const *p;
6992
6993 for (p = auxiliary_filters; *p != NULL; p++)
6994 {
6995 size_t indx;
6996
6997 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6998 *p, TRUE);
6999 if (indx == (size_t) -1
7000 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7001 return FALSE;
7002 }
7003 }
7004
7005 if (audit != NULL)
7006 {
7007 size_t indx;
7008
7009 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7010 TRUE);
7011 if (indx == (size_t) -1
7012 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7013 return FALSE;
7014 }
7015
7016 if (depaudit != NULL)
7017 {
7018 size_t indx;
7019
7020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7021 TRUE);
7022 if (indx == (size_t) -1
7023 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7024 return FALSE;
7025 }
7026
7027 eif.info = info;
7028 eif.failed = FALSE;
7029
7030 /* Find all symbols which were defined in a dynamic object and make
7031 the backend pick a reasonable value for them. */
7032 elf_link_hash_traverse (elf_hash_table (info),
7033 _bfd_elf_adjust_dynamic_symbol,
7034 &eif);
7035 if (eif.failed)
7036 return FALSE;
7037
7038 /* Add some entries to the .dynamic section. We fill in some of the
7039 values later, in bfd_elf_final_link, but we must add the entries
7040 now so that we know the final size of the .dynamic section. */
7041
7042 /* If there are initialization and/or finalization functions to
7043 call then add the corresponding DT_INIT/DT_FINI entries. */
7044 h = (info->init_function
7045 ? elf_link_hash_lookup (elf_hash_table (info),
7046 info->init_function, FALSE,
7047 FALSE, FALSE)
7048 : NULL);
7049 if (h != NULL
7050 && (h->ref_regular
7051 || h->def_regular))
7052 {
7053 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7054 return FALSE;
7055 }
7056 h = (info->fini_function
7057 ? elf_link_hash_lookup (elf_hash_table (info),
7058 info->fini_function, FALSE,
7059 FALSE, FALSE)
7060 : NULL);
7061 if (h != NULL
7062 && (h->ref_regular
7063 || h->def_regular))
7064 {
7065 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7066 return FALSE;
7067 }
7068
7069 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7070 if (s != NULL && s->linker_has_input)
7071 {
7072 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7073 if (! bfd_link_executable (info))
7074 {
7075 bfd *sub;
7076 asection *o;
7077
7078 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7079 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7080 && (o = sub->sections) != NULL
7081 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7082 for (o = sub->sections; o != NULL; o = o->next)
7083 if (elf_section_data (o)->this_hdr.sh_type
7084 == SHT_PREINIT_ARRAY)
7085 {
7086 _bfd_error_handler
7087 (_("%pB: .preinit_array section is not allowed in DSO"),
7088 sub);
7089 break;
7090 }
7091
7092 bfd_set_error (bfd_error_nonrepresentable_section);
7093 return FALSE;
7094 }
7095
7096 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7097 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7098 return FALSE;
7099 }
7100 s = bfd_get_section_by_name (output_bfd, ".init_array");
7101 if (s != NULL && s->linker_has_input)
7102 {
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7104 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7105 return FALSE;
7106 }
7107 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7108 if (s != NULL && s->linker_has_input)
7109 {
7110 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7111 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7112 return FALSE;
7113 }
7114
7115 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7116 /* If .dynstr is excluded from the link, we don't want any of
7117 these tags. Strictly, we should be checking each section
7118 individually; This quick check covers for the case where
7119 someone does a /DISCARD/ : { *(*) }. */
7120 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7121 {
7122 bfd_size_type strsize;
7123
7124 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7125 if ((info->emit_hash
7126 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7127 || (info->emit_gnu_hash
7128 && (bed->record_xhash_symbol == NULL
7129 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7130 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7131 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7132 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7133 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7134 bed->s->sizeof_sym))
7135 return FALSE;
7136 }
7137 }
7138
7139 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7140 return FALSE;
7141
7142 /* The backend must work out the sizes of all the other dynamic
7143 sections. */
7144 if (dynobj != NULL
7145 && bed->elf_backend_size_dynamic_sections != NULL
7146 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7147 return FALSE;
7148
7149 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7150 {
7151 if (elf_tdata (output_bfd)->cverdefs)
7152 {
7153 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7154
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7156 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7157 return FALSE;
7158 }
7159
7160 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7161 {
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7163 return FALSE;
7164 }
7165 else if (info->flags & DF_BIND_NOW)
7166 {
7167 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7168 return FALSE;
7169 }
7170
7171 if (info->flags_1)
7172 {
7173 if (bfd_link_executable (info))
7174 info->flags_1 &= ~ (DF_1_INITFIRST
7175 | DF_1_NODELETE
7176 | DF_1_NOOPEN);
7177 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7178 return FALSE;
7179 }
7180
7181 if (elf_tdata (output_bfd)->cverrefs)
7182 {
7183 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7184
7185 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7186 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7187 return FALSE;
7188 }
7189
7190 if ((elf_tdata (output_bfd)->cverrefs == 0
7191 && elf_tdata (output_bfd)->cverdefs == 0)
7192 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7193 {
7194 asection *s;
7195
7196 s = bfd_get_linker_section (dynobj, ".gnu.version");
7197 s->flags |= SEC_EXCLUDE;
7198 }
7199 }
7200 return TRUE;
7201 }
7202
7203 /* Find the first non-excluded output section. We'll use its
7204 section symbol for some emitted relocs. */
7205 void
7206 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7207 {
7208 asection *s;
7209 asection *found = NULL;
7210
7211 for (s = output_bfd->sections; s != NULL; s = s->next)
7212 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7213 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7214 {
7215 found = s;
7216 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7217 break;
7218 }
7219 elf_hash_table (info)->text_index_section = found;
7220 }
7221
7222 /* Find two non-excluded output sections, one for code, one for data.
7223 We'll use their section symbols for some emitted relocs. */
7224 void
7225 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7226 {
7227 asection *s;
7228 asection *found = NULL;
7229
7230 /* Data first, since setting text_index_section changes
7231 _bfd_elf_omit_section_dynsym_default. */
7232 for (s = output_bfd->sections; s != NULL; s = s->next)
7233 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7234 && !(s->flags & SEC_READONLY)
7235 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7236 {
7237 found = s;
7238 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7239 break;
7240 }
7241 elf_hash_table (info)->data_index_section = found;
7242
7243 for (s = output_bfd->sections; s != NULL; s = s->next)
7244 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7245 && (s->flags & SEC_READONLY)
7246 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7247 {
7248 found = s;
7249 break;
7250 }
7251 elf_hash_table (info)->text_index_section = found;
7252 }
7253
7254 #define GNU_HASH_SECTION_NAME(bed) \
7255 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7256
7257 bfd_boolean
7258 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7259 {
7260 const struct elf_backend_data *bed;
7261 unsigned long section_sym_count;
7262 bfd_size_type dynsymcount = 0;
7263
7264 if (!is_elf_hash_table (info->hash))
7265 return TRUE;
7266
7267 bed = get_elf_backend_data (output_bfd);
7268 (*bed->elf_backend_init_index_section) (output_bfd, info);
7269
7270 /* Assign dynsym indices. In a shared library we generate a section
7271 symbol for each output section, which come first. Next come all
7272 of the back-end allocated local dynamic syms, followed by the rest
7273 of the global symbols.
7274
7275 This is usually not needed for static binaries, however backends
7276 can request to always do it, e.g. the MIPS backend uses dynamic
7277 symbol counts to lay out GOT, which will be produced in the
7278 presence of GOT relocations even in static binaries (holding fixed
7279 data in that case, to satisfy those relocations). */
7280
7281 if (elf_hash_table (info)->dynamic_sections_created
7282 || bed->always_renumber_dynsyms)
7283 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7284 &section_sym_count);
7285
7286 if (elf_hash_table (info)->dynamic_sections_created)
7287 {
7288 bfd *dynobj;
7289 asection *s;
7290 unsigned int dtagcount;
7291
7292 dynobj = elf_hash_table (info)->dynobj;
7293
7294 /* Work out the size of the symbol version section. */
7295 s = bfd_get_linker_section (dynobj, ".gnu.version");
7296 BFD_ASSERT (s != NULL);
7297 if ((s->flags & SEC_EXCLUDE) == 0)
7298 {
7299 s->size = dynsymcount * sizeof (Elf_External_Versym);
7300 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7301 if (s->contents == NULL)
7302 return FALSE;
7303
7304 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7305 return FALSE;
7306 }
7307
7308 /* Set the size of the .dynsym and .hash sections. We counted
7309 the number of dynamic symbols in elf_link_add_object_symbols.
7310 We will build the contents of .dynsym and .hash when we build
7311 the final symbol table, because until then we do not know the
7312 correct value to give the symbols. We built the .dynstr
7313 section as we went along in elf_link_add_object_symbols. */
7314 s = elf_hash_table (info)->dynsym;
7315 BFD_ASSERT (s != NULL);
7316 s->size = dynsymcount * bed->s->sizeof_sym;
7317
7318 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7319 if (s->contents == NULL)
7320 return FALSE;
7321
7322 /* The first entry in .dynsym is a dummy symbol. Clear all the
7323 section syms, in case we don't output them all. */
7324 ++section_sym_count;
7325 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7326
7327 elf_hash_table (info)->bucketcount = 0;
7328
7329 /* Compute the size of the hashing table. As a side effect this
7330 computes the hash values for all the names we export. */
7331 if (info->emit_hash)
7332 {
7333 unsigned long int *hashcodes;
7334 struct hash_codes_info hashinf;
7335 bfd_size_type amt;
7336 unsigned long int nsyms;
7337 size_t bucketcount;
7338 size_t hash_entry_size;
7339
7340 /* Compute the hash values for all exported symbols. At the same
7341 time store the values in an array so that we could use them for
7342 optimizations. */
7343 amt = dynsymcount * sizeof (unsigned long int);
7344 hashcodes = (unsigned long int *) bfd_malloc (amt);
7345 if (hashcodes == NULL)
7346 return FALSE;
7347 hashinf.hashcodes = hashcodes;
7348 hashinf.error = FALSE;
7349
7350 /* Put all hash values in HASHCODES. */
7351 elf_link_hash_traverse (elf_hash_table (info),
7352 elf_collect_hash_codes, &hashinf);
7353 if (hashinf.error)
7354 {
7355 free (hashcodes);
7356 return FALSE;
7357 }
7358
7359 nsyms = hashinf.hashcodes - hashcodes;
7360 bucketcount
7361 = compute_bucket_count (info, hashcodes, nsyms, 0);
7362 free (hashcodes);
7363
7364 if (bucketcount == 0 && nsyms > 0)
7365 return FALSE;
7366
7367 elf_hash_table (info)->bucketcount = bucketcount;
7368
7369 s = bfd_get_linker_section (dynobj, ".hash");
7370 BFD_ASSERT (s != NULL);
7371 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7372 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7373 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7374 if (s->contents == NULL)
7375 return FALSE;
7376
7377 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7378 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7379 s->contents + hash_entry_size);
7380 }
7381
7382 if (info->emit_gnu_hash)
7383 {
7384 size_t i, cnt;
7385 unsigned char *contents;
7386 struct collect_gnu_hash_codes cinfo;
7387 bfd_size_type amt;
7388 size_t bucketcount;
7389
7390 memset (&cinfo, 0, sizeof (cinfo));
7391
7392 /* Compute the hash values for all exported symbols. At the same
7393 time store the values in an array so that we could use them for
7394 optimizations. */
7395 amt = dynsymcount * 2 * sizeof (unsigned long int);
7396 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7397 if (cinfo.hashcodes == NULL)
7398 return FALSE;
7399
7400 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7401 cinfo.min_dynindx = -1;
7402 cinfo.output_bfd = output_bfd;
7403 cinfo.bed = bed;
7404
7405 /* Put all hash values in HASHCODES. */
7406 elf_link_hash_traverse (elf_hash_table (info),
7407 elf_collect_gnu_hash_codes, &cinfo);
7408 if (cinfo.error)
7409 {
7410 free (cinfo.hashcodes);
7411 return FALSE;
7412 }
7413
7414 bucketcount
7415 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7416
7417 if (bucketcount == 0)
7418 {
7419 free (cinfo.hashcodes);
7420 return FALSE;
7421 }
7422
7423 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7424 BFD_ASSERT (s != NULL);
7425
7426 if (cinfo.nsyms == 0)
7427 {
7428 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7429 BFD_ASSERT (cinfo.min_dynindx == -1);
7430 free (cinfo.hashcodes);
7431 s->size = 5 * 4 + bed->s->arch_size / 8;
7432 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7433 if (contents == NULL)
7434 return FALSE;
7435 s->contents = contents;
7436 /* 1 empty bucket. */
7437 bfd_put_32 (output_bfd, 1, contents);
7438 /* SYMIDX above the special symbol 0. */
7439 bfd_put_32 (output_bfd, 1, contents + 4);
7440 /* Just one word for bitmask. */
7441 bfd_put_32 (output_bfd, 1, contents + 8);
7442 /* Only hash fn bloom filter. */
7443 bfd_put_32 (output_bfd, 0, contents + 12);
7444 /* No hashes are valid - empty bitmask. */
7445 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7446 /* No hashes in the only bucket. */
7447 bfd_put_32 (output_bfd, 0,
7448 contents + 16 + bed->s->arch_size / 8);
7449 }
7450 else
7451 {
7452 unsigned long int maskwords, maskbitslog2, x;
7453 BFD_ASSERT (cinfo.min_dynindx != -1);
7454
7455 x = cinfo.nsyms;
7456 maskbitslog2 = 1;
7457 while ((x >>= 1) != 0)
7458 ++maskbitslog2;
7459 if (maskbitslog2 < 3)
7460 maskbitslog2 = 5;
7461 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7462 maskbitslog2 = maskbitslog2 + 3;
7463 else
7464 maskbitslog2 = maskbitslog2 + 2;
7465 if (bed->s->arch_size == 64)
7466 {
7467 if (maskbitslog2 == 5)
7468 maskbitslog2 = 6;
7469 cinfo.shift1 = 6;
7470 }
7471 else
7472 cinfo.shift1 = 5;
7473 cinfo.mask = (1 << cinfo.shift1) - 1;
7474 cinfo.shift2 = maskbitslog2;
7475 cinfo.maskbits = 1 << maskbitslog2;
7476 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7477 amt = bucketcount * sizeof (unsigned long int) * 2;
7478 amt += maskwords * sizeof (bfd_vma);
7479 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7480 if (cinfo.bitmask == NULL)
7481 {
7482 free (cinfo.hashcodes);
7483 return FALSE;
7484 }
7485
7486 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7487 cinfo.indx = cinfo.counts + bucketcount;
7488 cinfo.symindx = dynsymcount - cinfo.nsyms;
7489 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7490
7491 /* Determine how often each hash bucket is used. */
7492 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7493 for (i = 0; i < cinfo.nsyms; ++i)
7494 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7495
7496 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7497 if (cinfo.counts[i] != 0)
7498 {
7499 cinfo.indx[i] = cnt;
7500 cnt += cinfo.counts[i];
7501 }
7502 BFD_ASSERT (cnt == dynsymcount);
7503 cinfo.bucketcount = bucketcount;
7504 cinfo.local_indx = cinfo.min_dynindx;
7505
7506 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7507 s->size += cinfo.maskbits / 8;
7508 if (bed->record_xhash_symbol != NULL)
7509 s->size += cinfo.nsyms * 4;
7510 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7511 if (contents == NULL)
7512 {
7513 free (cinfo.bitmask);
7514 free (cinfo.hashcodes);
7515 return FALSE;
7516 }
7517
7518 s->contents = contents;
7519 bfd_put_32 (output_bfd, bucketcount, contents);
7520 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7521 bfd_put_32 (output_bfd, maskwords, contents + 8);
7522 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7523 contents += 16 + cinfo.maskbits / 8;
7524
7525 for (i = 0; i < bucketcount; ++i)
7526 {
7527 if (cinfo.counts[i] == 0)
7528 bfd_put_32 (output_bfd, 0, contents);
7529 else
7530 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7531 contents += 4;
7532 }
7533
7534 cinfo.contents = contents;
7535
7536 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7537 /* Renumber dynamic symbols, if populating .gnu.hash section.
7538 If using .MIPS.xhash, populate the translation table. */
7539 elf_link_hash_traverse (elf_hash_table (info),
7540 elf_gnu_hash_process_symidx, &cinfo);
7541
7542 contents = s->contents + 16;
7543 for (i = 0; i < maskwords; ++i)
7544 {
7545 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7546 contents);
7547 contents += bed->s->arch_size / 8;
7548 }
7549
7550 free (cinfo.bitmask);
7551 free (cinfo.hashcodes);
7552 }
7553 }
7554
7555 s = bfd_get_linker_section (dynobj, ".dynstr");
7556 BFD_ASSERT (s != NULL);
7557
7558 elf_finalize_dynstr (output_bfd, info);
7559
7560 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7561
7562 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7563 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7564 return FALSE;
7565 }
7566
7567 return TRUE;
7568 }
7569 \f
7570 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7571
7572 static void
7573 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7574 asection *sec)
7575 {
7576 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7577 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7578 }
7579
7580 /* Finish SHF_MERGE section merging. */
7581
7582 bfd_boolean
7583 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7584 {
7585 bfd *ibfd;
7586 asection *sec;
7587
7588 if (!is_elf_hash_table (info->hash))
7589 return FALSE;
7590
7591 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7592 if ((ibfd->flags & DYNAMIC) == 0
7593 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7594 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7595 == get_elf_backend_data (obfd)->s->elfclass))
7596 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7597 if ((sec->flags & SEC_MERGE) != 0
7598 && !bfd_is_abs_section (sec->output_section))
7599 {
7600 struct bfd_elf_section_data *secdata;
7601
7602 secdata = elf_section_data (sec);
7603 if (! _bfd_add_merge_section (obfd,
7604 &elf_hash_table (info)->merge_info,
7605 sec, &secdata->sec_info))
7606 return FALSE;
7607 else if (secdata->sec_info)
7608 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7609 }
7610
7611 if (elf_hash_table (info)->merge_info != NULL)
7612 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7613 merge_sections_remove_hook);
7614 return TRUE;
7615 }
7616
7617 /* Create an entry in an ELF linker hash table. */
7618
7619 struct bfd_hash_entry *
7620 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7621 struct bfd_hash_table *table,
7622 const char *string)
7623 {
7624 /* Allocate the structure if it has not already been allocated by a
7625 subclass. */
7626 if (entry == NULL)
7627 {
7628 entry = (struct bfd_hash_entry *)
7629 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7630 if (entry == NULL)
7631 return entry;
7632 }
7633
7634 /* Call the allocation method of the superclass. */
7635 entry = _bfd_link_hash_newfunc (entry, table, string);
7636 if (entry != NULL)
7637 {
7638 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7639 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7640
7641 /* Set local fields. */
7642 ret->indx = -1;
7643 ret->dynindx = -1;
7644 ret->got = htab->init_got_refcount;
7645 ret->plt = htab->init_plt_refcount;
7646 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7647 - offsetof (struct elf_link_hash_entry, size)));
7648 /* Assume that we have been called by a non-ELF symbol reader.
7649 This flag is then reset by the code which reads an ELF input
7650 file. This ensures that a symbol created by a non-ELF symbol
7651 reader will have the flag set correctly. */
7652 ret->non_elf = 1;
7653 }
7654
7655 return entry;
7656 }
7657
7658 /* Copy data from an indirect symbol to its direct symbol, hiding the
7659 old indirect symbol. Also used for copying flags to a weakdef. */
7660
7661 void
7662 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7663 struct elf_link_hash_entry *dir,
7664 struct elf_link_hash_entry *ind)
7665 {
7666 struct elf_link_hash_table *htab;
7667
7668 if (ind->dyn_relocs != NULL)
7669 {
7670 if (dir->dyn_relocs != NULL)
7671 {
7672 struct elf_dyn_relocs **pp;
7673 struct elf_dyn_relocs *p;
7674
7675 /* Add reloc counts against the indirect sym to the direct sym
7676 list. Merge any entries against the same section. */
7677 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7678 {
7679 struct elf_dyn_relocs *q;
7680
7681 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7682 if (q->sec == p->sec)
7683 {
7684 q->pc_count += p->pc_count;
7685 q->count += p->count;
7686 *pp = p->next;
7687 break;
7688 }
7689 if (q == NULL)
7690 pp = &p->next;
7691 }
7692 *pp = dir->dyn_relocs;
7693 }
7694
7695 dir->dyn_relocs = ind->dyn_relocs;
7696 ind->dyn_relocs = NULL;
7697 }
7698
7699 /* Copy down any references that we may have already seen to the
7700 symbol which just became indirect. */
7701
7702 if (dir->versioned != versioned_hidden)
7703 dir->ref_dynamic |= ind->ref_dynamic;
7704 dir->ref_regular |= ind->ref_regular;
7705 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7706 dir->non_got_ref |= ind->non_got_ref;
7707 dir->needs_plt |= ind->needs_plt;
7708 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7709
7710 if (ind->root.type != bfd_link_hash_indirect)
7711 return;
7712
7713 /* Copy over the global and procedure linkage table refcount entries.
7714 These may have been already set up by a check_relocs routine. */
7715 htab = elf_hash_table (info);
7716 if (ind->got.refcount > htab->init_got_refcount.refcount)
7717 {
7718 if (dir->got.refcount < 0)
7719 dir->got.refcount = 0;
7720 dir->got.refcount += ind->got.refcount;
7721 ind->got.refcount = htab->init_got_refcount.refcount;
7722 }
7723
7724 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7725 {
7726 if (dir->plt.refcount < 0)
7727 dir->plt.refcount = 0;
7728 dir->plt.refcount += ind->plt.refcount;
7729 ind->plt.refcount = htab->init_plt_refcount.refcount;
7730 }
7731
7732 if (ind->dynindx != -1)
7733 {
7734 if (dir->dynindx != -1)
7735 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7736 dir->dynindx = ind->dynindx;
7737 dir->dynstr_index = ind->dynstr_index;
7738 ind->dynindx = -1;
7739 ind->dynstr_index = 0;
7740 }
7741 }
7742
7743 void
7744 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7745 struct elf_link_hash_entry *h,
7746 bfd_boolean force_local)
7747 {
7748 /* STT_GNU_IFUNC symbol must go through PLT. */
7749 if (h->type != STT_GNU_IFUNC)
7750 {
7751 h->plt = elf_hash_table (info)->init_plt_offset;
7752 h->needs_plt = 0;
7753 }
7754 if (force_local)
7755 {
7756 h->forced_local = 1;
7757 if (h->dynindx != -1)
7758 {
7759 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7760 h->dynstr_index);
7761 h->dynindx = -1;
7762 h->dynstr_index = 0;
7763 }
7764 }
7765 }
7766
7767 /* Hide a symbol. */
7768
7769 void
7770 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7771 struct bfd_link_info *info,
7772 struct bfd_link_hash_entry *h)
7773 {
7774 if (is_elf_hash_table (info->hash))
7775 {
7776 const struct elf_backend_data *bed
7777 = get_elf_backend_data (output_bfd);
7778 struct elf_link_hash_entry *eh
7779 = (struct elf_link_hash_entry *) h;
7780 bed->elf_backend_hide_symbol (info, eh, TRUE);
7781 eh->def_dynamic = 0;
7782 eh->ref_dynamic = 0;
7783 eh->dynamic_def = 0;
7784 }
7785 }
7786
7787 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7788 caller. */
7789
7790 bfd_boolean
7791 _bfd_elf_link_hash_table_init
7792 (struct elf_link_hash_table *table,
7793 bfd *abfd,
7794 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7795 struct bfd_hash_table *,
7796 const char *),
7797 unsigned int entsize,
7798 enum elf_target_id target_id)
7799 {
7800 bfd_boolean ret;
7801 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7802
7803 table->init_got_refcount.refcount = can_refcount - 1;
7804 table->init_plt_refcount.refcount = can_refcount - 1;
7805 table->init_got_offset.offset = -(bfd_vma) 1;
7806 table->init_plt_offset.offset = -(bfd_vma) 1;
7807 /* The first dynamic symbol is a dummy. */
7808 table->dynsymcount = 1;
7809
7810 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7811
7812 table->root.type = bfd_link_elf_hash_table;
7813 table->hash_table_id = target_id;
7814 table->target_os = get_elf_backend_data (abfd)->target_os;
7815
7816 return ret;
7817 }
7818
7819 /* Create an ELF linker hash table. */
7820
7821 struct bfd_link_hash_table *
7822 _bfd_elf_link_hash_table_create (bfd *abfd)
7823 {
7824 struct elf_link_hash_table *ret;
7825 size_t amt = sizeof (struct elf_link_hash_table);
7826
7827 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7828 if (ret == NULL)
7829 return NULL;
7830
7831 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7832 sizeof (struct elf_link_hash_entry),
7833 GENERIC_ELF_DATA))
7834 {
7835 free (ret);
7836 return NULL;
7837 }
7838 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7839
7840 return &ret->root;
7841 }
7842
7843 /* Destroy an ELF linker hash table. */
7844
7845 void
7846 _bfd_elf_link_hash_table_free (bfd *obfd)
7847 {
7848 struct elf_link_hash_table *htab;
7849
7850 htab = (struct elf_link_hash_table *) obfd->link.hash;
7851 if (htab->dynstr != NULL)
7852 _bfd_elf_strtab_free (htab->dynstr);
7853 _bfd_merge_sections_free (htab->merge_info);
7854 _bfd_generic_link_hash_table_free (obfd);
7855 }
7856
7857 /* This is a hook for the ELF emulation code in the generic linker to
7858 tell the backend linker what file name to use for the DT_NEEDED
7859 entry for a dynamic object. */
7860
7861 void
7862 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7863 {
7864 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7865 && bfd_get_format (abfd) == bfd_object)
7866 elf_dt_name (abfd) = name;
7867 }
7868
7869 int
7870 bfd_elf_get_dyn_lib_class (bfd *abfd)
7871 {
7872 int lib_class;
7873 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7874 && bfd_get_format (abfd) == bfd_object)
7875 lib_class = elf_dyn_lib_class (abfd);
7876 else
7877 lib_class = 0;
7878 return lib_class;
7879 }
7880
7881 void
7882 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7883 {
7884 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7885 && bfd_get_format (abfd) == bfd_object)
7886 elf_dyn_lib_class (abfd) = lib_class;
7887 }
7888
7889 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7890 the linker ELF emulation code. */
7891
7892 struct bfd_link_needed_list *
7893 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7894 struct bfd_link_info *info)
7895 {
7896 if (! is_elf_hash_table (info->hash))
7897 return NULL;
7898 return elf_hash_table (info)->needed;
7899 }
7900
7901 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7902 hook for the linker ELF emulation code. */
7903
7904 struct bfd_link_needed_list *
7905 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7906 struct bfd_link_info *info)
7907 {
7908 if (! is_elf_hash_table (info->hash))
7909 return NULL;
7910 return elf_hash_table (info)->runpath;
7911 }
7912
7913 /* Get the name actually used for a dynamic object for a link. This
7914 is the SONAME entry if there is one. Otherwise, it is the string
7915 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7916
7917 const char *
7918 bfd_elf_get_dt_soname (bfd *abfd)
7919 {
7920 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7921 && bfd_get_format (abfd) == bfd_object)
7922 return elf_dt_name (abfd);
7923 return NULL;
7924 }
7925
7926 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7927 the ELF linker emulation code. */
7928
7929 bfd_boolean
7930 bfd_elf_get_bfd_needed_list (bfd *abfd,
7931 struct bfd_link_needed_list **pneeded)
7932 {
7933 asection *s;
7934 bfd_byte *dynbuf = NULL;
7935 unsigned int elfsec;
7936 unsigned long shlink;
7937 bfd_byte *extdyn, *extdynend;
7938 size_t extdynsize;
7939 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7940
7941 *pneeded = NULL;
7942
7943 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7944 || bfd_get_format (abfd) != bfd_object)
7945 return TRUE;
7946
7947 s = bfd_get_section_by_name (abfd, ".dynamic");
7948 if (s == NULL || s->size == 0)
7949 return TRUE;
7950
7951 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7952 goto error_return;
7953
7954 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7955 if (elfsec == SHN_BAD)
7956 goto error_return;
7957
7958 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7959
7960 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7961 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7962
7963 extdyn = dynbuf;
7964 extdynend = extdyn + s->size;
7965 for (; extdyn < extdynend; extdyn += extdynsize)
7966 {
7967 Elf_Internal_Dyn dyn;
7968
7969 (*swap_dyn_in) (abfd, extdyn, &dyn);
7970
7971 if (dyn.d_tag == DT_NULL)
7972 break;
7973
7974 if (dyn.d_tag == DT_NEEDED)
7975 {
7976 const char *string;
7977 struct bfd_link_needed_list *l;
7978 unsigned int tagv = dyn.d_un.d_val;
7979 size_t amt;
7980
7981 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7982 if (string == NULL)
7983 goto error_return;
7984
7985 amt = sizeof *l;
7986 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7987 if (l == NULL)
7988 goto error_return;
7989
7990 l->by = abfd;
7991 l->name = string;
7992 l->next = *pneeded;
7993 *pneeded = l;
7994 }
7995 }
7996
7997 free (dynbuf);
7998
7999 return TRUE;
8000
8001 error_return:
8002 free (dynbuf);
8003 return FALSE;
8004 }
8005
8006 struct elf_symbuf_symbol
8007 {
8008 unsigned long st_name; /* Symbol name, index in string tbl */
8009 unsigned char st_info; /* Type and binding attributes */
8010 unsigned char st_other; /* Visibilty, and target specific */
8011 };
8012
8013 struct elf_symbuf_head
8014 {
8015 struct elf_symbuf_symbol *ssym;
8016 size_t count;
8017 unsigned int st_shndx;
8018 };
8019
8020 struct elf_symbol
8021 {
8022 union
8023 {
8024 Elf_Internal_Sym *isym;
8025 struct elf_symbuf_symbol *ssym;
8026 void *p;
8027 } u;
8028 const char *name;
8029 };
8030
8031 /* Sort references to symbols by ascending section number. */
8032
8033 static int
8034 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8035 {
8036 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8037 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8038
8039 if (s1->st_shndx != s2->st_shndx)
8040 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8041 /* Final sort by the address of the sym in the symbuf ensures
8042 a stable sort. */
8043 if (s1 != s2)
8044 return s1 > s2 ? 1 : -1;
8045 return 0;
8046 }
8047
8048 static int
8049 elf_sym_name_compare (const void *arg1, const void *arg2)
8050 {
8051 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8052 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8053 int ret = strcmp (s1->name, s2->name);
8054 if (ret != 0)
8055 return ret;
8056 if (s1->u.p != s2->u.p)
8057 return s1->u.p > s2->u.p ? 1 : -1;
8058 return 0;
8059 }
8060
8061 static struct elf_symbuf_head *
8062 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8063 {
8064 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8065 struct elf_symbuf_symbol *ssym;
8066 struct elf_symbuf_head *ssymbuf, *ssymhead;
8067 size_t i, shndx_count, total_size, amt;
8068
8069 amt = symcount * sizeof (*indbuf);
8070 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8071 if (indbuf == NULL)
8072 return NULL;
8073
8074 for (ind = indbuf, i = 0; i < symcount; i++)
8075 if (isymbuf[i].st_shndx != SHN_UNDEF)
8076 *ind++ = &isymbuf[i];
8077 indbufend = ind;
8078
8079 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8080 elf_sort_elf_symbol);
8081
8082 shndx_count = 0;
8083 if (indbufend > indbuf)
8084 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8085 if (ind[0]->st_shndx != ind[1]->st_shndx)
8086 shndx_count++;
8087
8088 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8089 + (indbufend - indbuf) * sizeof (*ssym));
8090 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8091 if (ssymbuf == NULL)
8092 {
8093 free (indbuf);
8094 return NULL;
8095 }
8096
8097 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8098 ssymbuf->ssym = NULL;
8099 ssymbuf->count = shndx_count;
8100 ssymbuf->st_shndx = 0;
8101 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8102 {
8103 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8104 {
8105 ssymhead++;
8106 ssymhead->ssym = ssym;
8107 ssymhead->count = 0;
8108 ssymhead->st_shndx = (*ind)->st_shndx;
8109 }
8110 ssym->st_name = (*ind)->st_name;
8111 ssym->st_info = (*ind)->st_info;
8112 ssym->st_other = (*ind)->st_other;
8113 ssymhead->count++;
8114 }
8115 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8116 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8117 == total_size));
8118
8119 free (indbuf);
8120 return ssymbuf;
8121 }
8122
8123 /* Check if 2 sections define the same set of local and global
8124 symbols. */
8125
8126 static bfd_boolean
8127 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8128 struct bfd_link_info *info)
8129 {
8130 bfd *bfd1, *bfd2;
8131 const struct elf_backend_data *bed1, *bed2;
8132 Elf_Internal_Shdr *hdr1, *hdr2;
8133 size_t symcount1, symcount2;
8134 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8135 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8136 Elf_Internal_Sym *isym, *isymend;
8137 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8138 size_t count1, count2, i;
8139 unsigned int shndx1, shndx2;
8140 bfd_boolean result;
8141
8142 bfd1 = sec1->owner;
8143 bfd2 = sec2->owner;
8144
8145 /* Both sections have to be in ELF. */
8146 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8147 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8148 return FALSE;
8149
8150 if (elf_section_type (sec1) != elf_section_type (sec2))
8151 return FALSE;
8152
8153 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8154 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8155 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8156 return FALSE;
8157
8158 bed1 = get_elf_backend_data (bfd1);
8159 bed2 = get_elf_backend_data (bfd2);
8160 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8161 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8162 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8163 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8164
8165 if (symcount1 == 0 || symcount2 == 0)
8166 return FALSE;
8167
8168 result = FALSE;
8169 isymbuf1 = NULL;
8170 isymbuf2 = NULL;
8171 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8172 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8173
8174 if (ssymbuf1 == NULL)
8175 {
8176 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8177 NULL, NULL, NULL);
8178 if (isymbuf1 == NULL)
8179 goto done;
8180
8181 if (info != NULL && !info->reduce_memory_overheads)
8182 {
8183 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8184 elf_tdata (bfd1)->symbuf = ssymbuf1;
8185 }
8186 }
8187
8188 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8189 {
8190 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8191 NULL, NULL, NULL);
8192 if (isymbuf2 == NULL)
8193 goto done;
8194
8195 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8196 {
8197 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8198 elf_tdata (bfd2)->symbuf = ssymbuf2;
8199 }
8200 }
8201
8202 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8203 {
8204 /* Optimized faster version. */
8205 size_t lo, hi, mid;
8206 struct elf_symbol *symp;
8207 struct elf_symbuf_symbol *ssym, *ssymend;
8208
8209 lo = 0;
8210 hi = ssymbuf1->count;
8211 ssymbuf1++;
8212 count1 = 0;
8213 while (lo < hi)
8214 {
8215 mid = (lo + hi) / 2;
8216 if (shndx1 < ssymbuf1[mid].st_shndx)
8217 hi = mid;
8218 else if (shndx1 > ssymbuf1[mid].st_shndx)
8219 lo = mid + 1;
8220 else
8221 {
8222 count1 = ssymbuf1[mid].count;
8223 ssymbuf1 += mid;
8224 break;
8225 }
8226 }
8227
8228 lo = 0;
8229 hi = ssymbuf2->count;
8230 ssymbuf2++;
8231 count2 = 0;
8232 while (lo < hi)
8233 {
8234 mid = (lo + hi) / 2;
8235 if (shndx2 < ssymbuf2[mid].st_shndx)
8236 hi = mid;
8237 else if (shndx2 > ssymbuf2[mid].st_shndx)
8238 lo = mid + 1;
8239 else
8240 {
8241 count2 = ssymbuf2[mid].count;
8242 ssymbuf2 += mid;
8243 break;
8244 }
8245 }
8246
8247 if (count1 == 0 || count2 == 0 || count1 != count2)
8248 goto done;
8249
8250 symtable1
8251 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8252 symtable2
8253 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8254 if (symtable1 == NULL || symtable2 == NULL)
8255 goto done;
8256
8257 symp = symtable1;
8258 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8259 ssym < ssymend; ssym++, symp++)
8260 {
8261 symp->u.ssym = ssym;
8262 symp->name = bfd_elf_string_from_elf_section (bfd1,
8263 hdr1->sh_link,
8264 ssym->st_name);
8265 }
8266
8267 symp = symtable2;
8268 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8269 ssym < ssymend; ssym++, symp++)
8270 {
8271 symp->u.ssym = ssym;
8272 symp->name = bfd_elf_string_from_elf_section (bfd2,
8273 hdr2->sh_link,
8274 ssym->st_name);
8275 }
8276
8277 /* Sort symbol by name. */
8278 qsort (symtable1, count1, sizeof (struct elf_symbol),
8279 elf_sym_name_compare);
8280 qsort (symtable2, count1, sizeof (struct elf_symbol),
8281 elf_sym_name_compare);
8282
8283 for (i = 0; i < count1; i++)
8284 /* Two symbols must have the same binding, type and name. */
8285 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8286 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8287 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8288 goto done;
8289
8290 result = TRUE;
8291 goto done;
8292 }
8293
8294 symtable1 = (struct elf_symbol *)
8295 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8296 symtable2 = (struct elf_symbol *)
8297 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8298 if (symtable1 == NULL || symtable2 == NULL)
8299 goto done;
8300
8301 /* Count definitions in the section. */
8302 count1 = 0;
8303 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8304 if (isym->st_shndx == shndx1)
8305 symtable1[count1++].u.isym = isym;
8306
8307 count2 = 0;
8308 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8309 if (isym->st_shndx == shndx2)
8310 symtable2[count2++].u.isym = isym;
8311
8312 if (count1 == 0 || count2 == 0 || count1 != count2)
8313 goto done;
8314
8315 for (i = 0; i < count1; i++)
8316 symtable1[i].name
8317 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8318 symtable1[i].u.isym->st_name);
8319
8320 for (i = 0; i < count2; i++)
8321 symtable2[i].name
8322 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8323 symtable2[i].u.isym->st_name);
8324
8325 /* Sort symbol by name. */
8326 qsort (symtable1, count1, sizeof (struct elf_symbol),
8327 elf_sym_name_compare);
8328 qsort (symtable2, count1, sizeof (struct elf_symbol),
8329 elf_sym_name_compare);
8330
8331 for (i = 0; i < count1; i++)
8332 /* Two symbols must have the same binding, type and name. */
8333 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8334 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8335 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8336 goto done;
8337
8338 result = TRUE;
8339
8340 done:
8341 free (symtable1);
8342 free (symtable2);
8343 free (isymbuf1);
8344 free (isymbuf2);
8345
8346 return result;
8347 }
8348
8349 /* Return TRUE if 2 section types are compatible. */
8350
8351 bfd_boolean
8352 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8353 bfd *bbfd, const asection *bsec)
8354 {
8355 if (asec == NULL
8356 || bsec == NULL
8357 || abfd->xvec->flavour != bfd_target_elf_flavour
8358 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8359 return TRUE;
8360
8361 return elf_section_type (asec) == elf_section_type (bsec);
8362 }
8363 \f
8364 /* Final phase of ELF linker. */
8365
8366 /* A structure we use to avoid passing large numbers of arguments. */
8367
8368 struct elf_final_link_info
8369 {
8370 /* General link information. */
8371 struct bfd_link_info *info;
8372 /* Output BFD. */
8373 bfd *output_bfd;
8374 /* Symbol string table. */
8375 struct elf_strtab_hash *symstrtab;
8376 /* .hash section. */
8377 asection *hash_sec;
8378 /* symbol version section (.gnu.version). */
8379 asection *symver_sec;
8380 /* Buffer large enough to hold contents of any section. */
8381 bfd_byte *contents;
8382 /* Buffer large enough to hold external relocs of any section. */
8383 void *external_relocs;
8384 /* Buffer large enough to hold internal relocs of any section. */
8385 Elf_Internal_Rela *internal_relocs;
8386 /* Buffer large enough to hold external local symbols of any input
8387 BFD. */
8388 bfd_byte *external_syms;
8389 /* And a buffer for symbol section indices. */
8390 Elf_External_Sym_Shndx *locsym_shndx;
8391 /* Buffer large enough to hold internal local symbols of any input
8392 BFD. */
8393 Elf_Internal_Sym *internal_syms;
8394 /* Array large enough to hold a symbol index for each local symbol
8395 of any input BFD. */
8396 long *indices;
8397 /* Array large enough to hold a section pointer for each local
8398 symbol of any input BFD. */
8399 asection **sections;
8400 /* Buffer for SHT_SYMTAB_SHNDX section. */
8401 Elf_External_Sym_Shndx *symshndxbuf;
8402 /* Number of STT_FILE syms seen. */
8403 size_t filesym_count;
8404 };
8405
8406 /* This struct is used to pass information to elf_link_output_extsym. */
8407
8408 struct elf_outext_info
8409 {
8410 bfd_boolean failed;
8411 bfd_boolean localsyms;
8412 bfd_boolean file_sym_done;
8413 struct elf_final_link_info *flinfo;
8414 };
8415
8416
8417 /* Support for evaluating a complex relocation.
8418
8419 Complex relocations are generalized, self-describing relocations. The
8420 implementation of them consists of two parts: complex symbols, and the
8421 relocations themselves.
8422
8423 The relocations use a reserved elf-wide relocation type code (R_RELC
8424 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8425 information (start bit, end bit, word width, etc) into the addend. This
8426 information is extracted from CGEN-generated operand tables within gas.
8427
8428 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8429 internal) representing prefix-notation expressions, including but not
8430 limited to those sorts of expressions normally encoded as addends in the
8431 addend field. The symbol mangling format is:
8432
8433 <node> := <literal>
8434 | <unary-operator> ':' <node>
8435 | <binary-operator> ':' <node> ':' <node>
8436 ;
8437
8438 <literal> := 's' <digits=N> ':' <N character symbol name>
8439 | 'S' <digits=N> ':' <N character section name>
8440 | '#' <hexdigits>
8441 ;
8442
8443 <binary-operator> := as in C
8444 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8445
8446 static void
8447 set_symbol_value (bfd *bfd_with_globals,
8448 Elf_Internal_Sym *isymbuf,
8449 size_t locsymcount,
8450 size_t symidx,
8451 bfd_vma val)
8452 {
8453 struct elf_link_hash_entry **sym_hashes;
8454 struct elf_link_hash_entry *h;
8455 size_t extsymoff = locsymcount;
8456
8457 if (symidx < locsymcount)
8458 {
8459 Elf_Internal_Sym *sym;
8460
8461 sym = isymbuf + symidx;
8462 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8463 {
8464 /* It is a local symbol: move it to the
8465 "absolute" section and give it a value. */
8466 sym->st_shndx = SHN_ABS;
8467 sym->st_value = val;
8468 return;
8469 }
8470 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8471 extsymoff = 0;
8472 }
8473
8474 /* It is a global symbol: set its link type
8475 to "defined" and give it a value. */
8476
8477 sym_hashes = elf_sym_hashes (bfd_with_globals);
8478 h = sym_hashes [symidx - extsymoff];
8479 while (h->root.type == bfd_link_hash_indirect
8480 || h->root.type == bfd_link_hash_warning)
8481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8482 h->root.type = bfd_link_hash_defined;
8483 h->root.u.def.value = val;
8484 h->root.u.def.section = bfd_abs_section_ptr;
8485 }
8486
8487 static bfd_boolean
8488 resolve_symbol (const char *name,
8489 bfd *input_bfd,
8490 struct elf_final_link_info *flinfo,
8491 bfd_vma *result,
8492 Elf_Internal_Sym *isymbuf,
8493 size_t locsymcount)
8494 {
8495 Elf_Internal_Sym *sym;
8496 struct bfd_link_hash_entry *global_entry;
8497 const char *candidate = NULL;
8498 Elf_Internal_Shdr *symtab_hdr;
8499 size_t i;
8500
8501 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8502
8503 for (i = 0; i < locsymcount; ++ i)
8504 {
8505 sym = isymbuf + i;
8506
8507 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8508 continue;
8509
8510 candidate = bfd_elf_string_from_elf_section (input_bfd,
8511 symtab_hdr->sh_link,
8512 sym->st_name);
8513 #ifdef DEBUG
8514 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8515 name, candidate, (unsigned long) sym->st_value);
8516 #endif
8517 if (candidate && strcmp (candidate, name) == 0)
8518 {
8519 asection *sec = flinfo->sections [i];
8520
8521 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8522 *result += sec->output_offset + sec->output_section->vma;
8523 #ifdef DEBUG
8524 printf ("Found symbol with value %8.8lx\n",
8525 (unsigned long) *result);
8526 #endif
8527 return TRUE;
8528 }
8529 }
8530
8531 /* Hmm, haven't found it yet. perhaps it is a global. */
8532 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8533 FALSE, FALSE, TRUE);
8534 if (!global_entry)
8535 return FALSE;
8536
8537 if (global_entry->type == bfd_link_hash_defined
8538 || global_entry->type == bfd_link_hash_defweak)
8539 {
8540 *result = (global_entry->u.def.value
8541 + global_entry->u.def.section->output_section->vma
8542 + global_entry->u.def.section->output_offset);
8543 #ifdef DEBUG
8544 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8545 global_entry->root.string, (unsigned long) *result);
8546 #endif
8547 return TRUE;
8548 }
8549
8550 return FALSE;
8551 }
8552
8553 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8554 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8555 names like "foo.end" which is the end address of section "foo". */
8556
8557 static bfd_boolean
8558 resolve_section (const char *name,
8559 asection *sections,
8560 bfd_vma *result,
8561 bfd * abfd)
8562 {
8563 asection *curr;
8564 unsigned int len;
8565
8566 for (curr = sections; curr; curr = curr->next)
8567 if (strcmp (curr->name, name) == 0)
8568 {
8569 *result = curr->vma;
8570 return TRUE;
8571 }
8572
8573 /* Hmm. still haven't found it. try pseudo-section names. */
8574 /* FIXME: This could be coded more efficiently... */
8575 for (curr = sections; curr; curr = curr->next)
8576 {
8577 len = strlen (curr->name);
8578 if (len > strlen (name))
8579 continue;
8580
8581 if (strncmp (curr->name, name, len) == 0)
8582 {
8583 if (strncmp (".end", name + len, 4) == 0)
8584 {
8585 *result = (curr->vma
8586 + curr->size / bfd_octets_per_byte (abfd, curr));
8587 return TRUE;
8588 }
8589
8590 /* Insert more pseudo-section names here, if you like. */
8591 }
8592 }
8593
8594 return FALSE;
8595 }
8596
8597 static void
8598 undefined_reference (const char *reftype, const char *name)
8599 {
8600 /* xgettext:c-format */
8601 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8602 reftype, name);
8603 bfd_set_error (bfd_error_bad_value);
8604 }
8605
8606 static bfd_boolean
8607 eval_symbol (bfd_vma *result,
8608 const char **symp,
8609 bfd *input_bfd,
8610 struct elf_final_link_info *flinfo,
8611 bfd_vma dot,
8612 Elf_Internal_Sym *isymbuf,
8613 size_t locsymcount,
8614 int signed_p)
8615 {
8616 size_t len;
8617 size_t symlen;
8618 bfd_vma a;
8619 bfd_vma b;
8620 char symbuf[4096];
8621 const char *sym = *symp;
8622 const char *symend;
8623 bfd_boolean symbol_is_section = FALSE;
8624
8625 len = strlen (sym);
8626 symend = sym + len;
8627
8628 if (len < 1 || len > sizeof (symbuf))
8629 {
8630 bfd_set_error (bfd_error_invalid_operation);
8631 return FALSE;
8632 }
8633
8634 switch (* sym)
8635 {
8636 case '.':
8637 *result = dot;
8638 *symp = sym + 1;
8639 return TRUE;
8640
8641 case '#':
8642 ++sym;
8643 *result = strtoul (sym, (char **) symp, 16);
8644 return TRUE;
8645
8646 case 'S':
8647 symbol_is_section = TRUE;
8648 /* Fall through. */
8649 case 's':
8650 ++sym;
8651 symlen = strtol (sym, (char **) symp, 10);
8652 sym = *symp + 1; /* Skip the trailing ':'. */
8653
8654 if (symend < sym || symlen + 1 > sizeof (symbuf))
8655 {
8656 bfd_set_error (bfd_error_invalid_operation);
8657 return FALSE;
8658 }
8659
8660 memcpy (symbuf, sym, symlen);
8661 symbuf[symlen] = '\0';
8662 *symp = sym + symlen;
8663
8664 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8665 the symbol as a section, or vice-versa. so we're pretty liberal in our
8666 interpretation here; section means "try section first", not "must be a
8667 section", and likewise with symbol. */
8668
8669 if (symbol_is_section)
8670 {
8671 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8672 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8673 isymbuf, locsymcount))
8674 {
8675 undefined_reference ("section", symbuf);
8676 return FALSE;
8677 }
8678 }
8679 else
8680 {
8681 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8682 isymbuf, locsymcount)
8683 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8684 result, input_bfd))
8685 {
8686 undefined_reference ("symbol", symbuf);
8687 return FALSE;
8688 }
8689 }
8690
8691 return TRUE;
8692
8693 /* All that remains are operators. */
8694
8695 #define UNARY_OP(op) \
8696 if (strncmp (sym, #op, strlen (#op)) == 0) \
8697 { \
8698 sym += strlen (#op); \
8699 if (*sym == ':') \
8700 ++sym; \
8701 *symp = sym; \
8702 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8703 isymbuf, locsymcount, signed_p)) \
8704 return FALSE; \
8705 if (signed_p) \
8706 *result = op ((bfd_signed_vma) a); \
8707 else \
8708 *result = op a; \
8709 return TRUE; \
8710 }
8711
8712 #define BINARY_OP_HEAD(op) \
8713 if (strncmp (sym, #op, strlen (#op)) == 0) \
8714 { \
8715 sym += strlen (#op); \
8716 if (*sym == ':') \
8717 ++sym; \
8718 *symp = sym; \
8719 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8720 isymbuf, locsymcount, signed_p)) \
8721 return FALSE; \
8722 ++*symp; \
8723 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8724 isymbuf, locsymcount, signed_p)) \
8725 return FALSE;
8726 #define BINARY_OP_TAIL(op) \
8727 if (signed_p) \
8728 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8729 else \
8730 *result = a op b; \
8731 return TRUE; \
8732 }
8733 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8734
8735 default:
8736 UNARY_OP (0-);
8737 BINARY_OP_HEAD (<<);
8738 if (b >= sizeof (a) * CHAR_BIT)
8739 {
8740 *result = 0;
8741 return TRUE;
8742 }
8743 signed_p = 0;
8744 BINARY_OP_TAIL (<<);
8745 BINARY_OP_HEAD (>>);
8746 if (b >= sizeof (a) * CHAR_BIT)
8747 {
8748 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8749 return TRUE;
8750 }
8751 BINARY_OP_TAIL (>>);
8752 BINARY_OP (==);
8753 BINARY_OP (!=);
8754 BINARY_OP (<=);
8755 BINARY_OP (>=);
8756 BINARY_OP (&&);
8757 BINARY_OP (||);
8758 UNARY_OP (~);
8759 UNARY_OP (!);
8760 BINARY_OP (*);
8761 BINARY_OP_HEAD (/);
8762 if (b == 0)
8763 {
8764 _bfd_error_handler (_("division by zero"));
8765 bfd_set_error (bfd_error_bad_value);
8766 return FALSE;
8767 }
8768 BINARY_OP_TAIL (/);
8769 BINARY_OP_HEAD (%);
8770 if (b == 0)
8771 {
8772 _bfd_error_handler (_("division by zero"));
8773 bfd_set_error (bfd_error_bad_value);
8774 return FALSE;
8775 }
8776 BINARY_OP_TAIL (%);
8777 BINARY_OP (^);
8778 BINARY_OP (|);
8779 BINARY_OP (&);
8780 BINARY_OP (+);
8781 BINARY_OP (-);
8782 BINARY_OP (<);
8783 BINARY_OP (>);
8784 #undef UNARY_OP
8785 #undef BINARY_OP
8786 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8787 bfd_set_error (bfd_error_invalid_operation);
8788 return FALSE;
8789 }
8790 }
8791
8792 static void
8793 put_value (bfd_vma size,
8794 unsigned long chunksz,
8795 bfd *input_bfd,
8796 bfd_vma x,
8797 bfd_byte *location)
8798 {
8799 location += (size - chunksz);
8800
8801 for (; size; size -= chunksz, location -= chunksz)
8802 {
8803 switch (chunksz)
8804 {
8805 case 1:
8806 bfd_put_8 (input_bfd, x, location);
8807 x >>= 8;
8808 break;
8809 case 2:
8810 bfd_put_16 (input_bfd, x, location);
8811 x >>= 16;
8812 break;
8813 case 4:
8814 bfd_put_32 (input_bfd, x, location);
8815 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8816 x >>= 16;
8817 x >>= 16;
8818 break;
8819 #ifdef BFD64
8820 case 8:
8821 bfd_put_64 (input_bfd, x, location);
8822 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8823 x >>= 32;
8824 x >>= 32;
8825 break;
8826 #endif
8827 default:
8828 abort ();
8829 break;
8830 }
8831 }
8832 }
8833
8834 static bfd_vma
8835 get_value (bfd_vma size,
8836 unsigned long chunksz,
8837 bfd *input_bfd,
8838 bfd_byte *location)
8839 {
8840 int shift;
8841 bfd_vma x = 0;
8842
8843 /* Sanity checks. */
8844 BFD_ASSERT (chunksz <= sizeof (x)
8845 && size >= chunksz
8846 && chunksz != 0
8847 && (size % chunksz) == 0
8848 && input_bfd != NULL
8849 && location != NULL);
8850
8851 if (chunksz == sizeof (x))
8852 {
8853 BFD_ASSERT (size == chunksz);
8854
8855 /* Make sure that we do not perform an undefined shift operation.
8856 We know that size == chunksz so there will only be one iteration
8857 of the loop below. */
8858 shift = 0;
8859 }
8860 else
8861 shift = 8 * chunksz;
8862
8863 for (; size; size -= chunksz, location += chunksz)
8864 {
8865 switch (chunksz)
8866 {
8867 case 1:
8868 x = (x << shift) | bfd_get_8 (input_bfd, location);
8869 break;
8870 case 2:
8871 x = (x << shift) | bfd_get_16 (input_bfd, location);
8872 break;
8873 case 4:
8874 x = (x << shift) | bfd_get_32 (input_bfd, location);
8875 break;
8876 #ifdef BFD64
8877 case 8:
8878 x = (x << shift) | bfd_get_64 (input_bfd, location);
8879 break;
8880 #endif
8881 default:
8882 abort ();
8883 }
8884 }
8885 return x;
8886 }
8887
8888 static void
8889 decode_complex_addend (unsigned long *start, /* in bits */
8890 unsigned long *oplen, /* in bits */
8891 unsigned long *len, /* in bits */
8892 unsigned long *wordsz, /* in bytes */
8893 unsigned long *chunksz, /* in bytes */
8894 unsigned long *lsb0_p,
8895 unsigned long *signed_p,
8896 unsigned long *trunc_p,
8897 unsigned long encoded)
8898 {
8899 * start = encoded & 0x3F;
8900 * len = (encoded >> 6) & 0x3F;
8901 * oplen = (encoded >> 12) & 0x3F;
8902 * wordsz = (encoded >> 18) & 0xF;
8903 * chunksz = (encoded >> 22) & 0xF;
8904 * lsb0_p = (encoded >> 27) & 1;
8905 * signed_p = (encoded >> 28) & 1;
8906 * trunc_p = (encoded >> 29) & 1;
8907 }
8908
8909 bfd_reloc_status_type
8910 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8911 asection *input_section,
8912 bfd_byte *contents,
8913 Elf_Internal_Rela *rel,
8914 bfd_vma relocation)
8915 {
8916 bfd_vma shift, x, mask;
8917 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8918 bfd_reloc_status_type r;
8919 bfd_size_type octets;
8920
8921 /* Perform this reloc, since it is complex.
8922 (this is not to say that it necessarily refers to a complex
8923 symbol; merely that it is a self-describing CGEN based reloc.
8924 i.e. the addend has the complete reloc information (bit start, end,
8925 word size, etc) encoded within it.). */
8926
8927 decode_complex_addend (&start, &oplen, &len, &wordsz,
8928 &chunksz, &lsb0_p, &signed_p,
8929 &trunc_p, rel->r_addend);
8930
8931 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8932
8933 if (lsb0_p)
8934 shift = (start + 1) - len;
8935 else
8936 shift = (8 * wordsz) - (start + len);
8937
8938 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8939 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8940
8941 #ifdef DEBUG
8942 printf ("Doing complex reloc: "
8943 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8944 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8945 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8946 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8947 oplen, (unsigned long) x, (unsigned long) mask,
8948 (unsigned long) relocation);
8949 #endif
8950
8951 r = bfd_reloc_ok;
8952 if (! trunc_p)
8953 /* Now do an overflow check. */
8954 r = bfd_check_overflow ((signed_p
8955 ? complain_overflow_signed
8956 : complain_overflow_unsigned),
8957 len, 0, (8 * wordsz),
8958 relocation);
8959
8960 /* Do the deed. */
8961 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8962
8963 #ifdef DEBUG
8964 printf (" relocation: %8.8lx\n"
8965 " shifted mask: %8.8lx\n"
8966 " shifted/masked reloc: %8.8lx\n"
8967 " result: %8.8lx\n",
8968 (unsigned long) relocation, (unsigned long) (mask << shift),
8969 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8970 #endif
8971 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8972 return r;
8973 }
8974
8975 /* Functions to read r_offset from external (target order) reloc
8976 entry. Faster than bfd_getl32 et al, because we let the compiler
8977 know the value is aligned. */
8978
8979 static bfd_vma
8980 ext32l_r_offset (const void *p)
8981 {
8982 union aligned32
8983 {
8984 uint32_t v;
8985 unsigned char c[4];
8986 };
8987 const union aligned32 *a
8988 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8989
8990 uint32_t aval = ( (uint32_t) a->c[0]
8991 | (uint32_t) a->c[1] << 8
8992 | (uint32_t) a->c[2] << 16
8993 | (uint32_t) a->c[3] << 24);
8994 return aval;
8995 }
8996
8997 static bfd_vma
8998 ext32b_r_offset (const void *p)
8999 {
9000 union aligned32
9001 {
9002 uint32_t v;
9003 unsigned char c[4];
9004 };
9005 const union aligned32 *a
9006 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9007
9008 uint32_t aval = ( (uint32_t) a->c[0] << 24
9009 | (uint32_t) a->c[1] << 16
9010 | (uint32_t) a->c[2] << 8
9011 | (uint32_t) a->c[3]);
9012 return aval;
9013 }
9014
9015 #ifdef BFD_HOST_64_BIT
9016 static bfd_vma
9017 ext64l_r_offset (const void *p)
9018 {
9019 union aligned64
9020 {
9021 uint64_t v;
9022 unsigned char c[8];
9023 };
9024 const union aligned64 *a
9025 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9026
9027 uint64_t aval = ( (uint64_t) a->c[0]
9028 | (uint64_t) a->c[1] << 8
9029 | (uint64_t) a->c[2] << 16
9030 | (uint64_t) a->c[3] << 24
9031 | (uint64_t) a->c[4] << 32
9032 | (uint64_t) a->c[5] << 40
9033 | (uint64_t) a->c[6] << 48
9034 | (uint64_t) a->c[7] << 56);
9035 return aval;
9036 }
9037
9038 static bfd_vma
9039 ext64b_r_offset (const void *p)
9040 {
9041 union aligned64
9042 {
9043 uint64_t v;
9044 unsigned char c[8];
9045 };
9046 const union aligned64 *a
9047 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9048
9049 uint64_t aval = ( (uint64_t) a->c[0] << 56
9050 | (uint64_t) a->c[1] << 48
9051 | (uint64_t) a->c[2] << 40
9052 | (uint64_t) a->c[3] << 32
9053 | (uint64_t) a->c[4] << 24
9054 | (uint64_t) a->c[5] << 16
9055 | (uint64_t) a->c[6] << 8
9056 | (uint64_t) a->c[7]);
9057 return aval;
9058 }
9059 #endif
9060
9061 /* When performing a relocatable link, the input relocations are
9062 preserved. But, if they reference global symbols, the indices
9063 referenced must be updated. Update all the relocations found in
9064 RELDATA. */
9065
9066 static bfd_boolean
9067 elf_link_adjust_relocs (bfd *abfd,
9068 asection *sec,
9069 struct bfd_elf_section_reloc_data *reldata,
9070 bfd_boolean sort,
9071 struct bfd_link_info *info)
9072 {
9073 unsigned int i;
9074 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9075 bfd_byte *erela;
9076 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9077 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9078 bfd_vma r_type_mask;
9079 int r_sym_shift;
9080 unsigned int count = reldata->count;
9081 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9082
9083 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9084 {
9085 swap_in = bed->s->swap_reloc_in;
9086 swap_out = bed->s->swap_reloc_out;
9087 }
9088 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9089 {
9090 swap_in = bed->s->swap_reloca_in;
9091 swap_out = bed->s->swap_reloca_out;
9092 }
9093 else
9094 abort ();
9095
9096 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9097 abort ();
9098
9099 if (bed->s->arch_size == 32)
9100 {
9101 r_type_mask = 0xff;
9102 r_sym_shift = 8;
9103 }
9104 else
9105 {
9106 r_type_mask = 0xffffffff;
9107 r_sym_shift = 32;
9108 }
9109
9110 erela = reldata->hdr->contents;
9111 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9112 {
9113 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9114 unsigned int j;
9115
9116 if (*rel_hash == NULL)
9117 continue;
9118
9119 if ((*rel_hash)->indx == -2
9120 && info->gc_sections
9121 && ! info->gc_keep_exported)
9122 {
9123 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9124 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9125 abfd, sec,
9126 (*rel_hash)->root.root.string);
9127 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9128 abfd, sec);
9129 bfd_set_error (bfd_error_invalid_operation);
9130 return FALSE;
9131 }
9132 BFD_ASSERT ((*rel_hash)->indx >= 0);
9133
9134 (*swap_in) (abfd, erela, irela);
9135 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9136 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9137 | (irela[j].r_info & r_type_mask));
9138 (*swap_out) (abfd, irela, erela);
9139 }
9140
9141 if (bed->elf_backend_update_relocs)
9142 (*bed->elf_backend_update_relocs) (sec, reldata);
9143
9144 if (sort && count != 0)
9145 {
9146 bfd_vma (*ext_r_off) (const void *);
9147 bfd_vma r_off;
9148 size_t elt_size;
9149 bfd_byte *base, *end, *p, *loc;
9150 bfd_byte *buf = NULL;
9151
9152 if (bed->s->arch_size == 32)
9153 {
9154 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9155 ext_r_off = ext32l_r_offset;
9156 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9157 ext_r_off = ext32b_r_offset;
9158 else
9159 abort ();
9160 }
9161 else
9162 {
9163 #ifdef BFD_HOST_64_BIT
9164 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9165 ext_r_off = ext64l_r_offset;
9166 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9167 ext_r_off = ext64b_r_offset;
9168 else
9169 #endif
9170 abort ();
9171 }
9172
9173 /* Must use a stable sort here. A modified insertion sort,
9174 since the relocs are mostly sorted already. */
9175 elt_size = reldata->hdr->sh_entsize;
9176 base = reldata->hdr->contents;
9177 end = base + count * elt_size;
9178 if (elt_size > sizeof (Elf64_External_Rela))
9179 abort ();
9180
9181 /* Ensure the first element is lowest. This acts as a sentinel,
9182 speeding the main loop below. */
9183 r_off = (*ext_r_off) (base);
9184 for (p = loc = base; (p += elt_size) < end; )
9185 {
9186 bfd_vma r_off2 = (*ext_r_off) (p);
9187 if (r_off > r_off2)
9188 {
9189 r_off = r_off2;
9190 loc = p;
9191 }
9192 }
9193 if (loc != base)
9194 {
9195 /* Don't just swap *base and *loc as that changes the order
9196 of the original base[0] and base[1] if they happen to
9197 have the same r_offset. */
9198 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9199 memcpy (onebuf, loc, elt_size);
9200 memmove (base + elt_size, base, loc - base);
9201 memcpy (base, onebuf, elt_size);
9202 }
9203
9204 for (p = base + elt_size; (p += elt_size) < end; )
9205 {
9206 /* base to p is sorted, *p is next to insert. */
9207 r_off = (*ext_r_off) (p);
9208 /* Search the sorted region for location to insert. */
9209 loc = p - elt_size;
9210 while (r_off < (*ext_r_off) (loc))
9211 loc -= elt_size;
9212 loc += elt_size;
9213 if (loc != p)
9214 {
9215 /* Chances are there is a run of relocs to insert here,
9216 from one of more input files. Files are not always
9217 linked in order due to the way elf_link_input_bfd is
9218 called. See pr17666. */
9219 size_t sortlen = p - loc;
9220 bfd_vma r_off2 = (*ext_r_off) (loc);
9221 size_t runlen = elt_size;
9222 size_t buf_size = 96 * 1024;
9223 while (p + runlen < end
9224 && (sortlen <= buf_size
9225 || runlen + elt_size <= buf_size)
9226 && r_off2 > (*ext_r_off) (p + runlen))
9227 runlen += elt_size;
9228 if (buf == NULL)
9229 {
9230 buf = bfd_malloc (buf_size);
9231 if (buf == NULL)
9232 return FALSE;
9233 }
9234 if (runlen < sortlen)
9235 {
9236 memcpy (buf, p, runlen);
9237 memmove (loc + runlen, loc, sortlen);
9238 memcpy (loc, buf, runlen);
9239 }
9240 else
9241 {
9242 memcpy (buf, loc, sortlen);
9243 memmove (loc, p, runlen);
9244 memcpy (loc + runlen, buf, sortlen);
9245 }
9246 p += runlen - elt_size;
9247 }
9248 }
9249 /* Hashes are no longer valid. */
9250 free (reldata->hashes);
9251 reldata->hashes = NULL;
9252 free (buf);
9253 }
9254 return TRUE;
9255 }
9256
9257 struct elf_link_sort_rela
9258 {
9259 union {
9260 bfd_vma offset;
9261 bfd_vma sym_mask;
9262 } u;
9263 enum elf_reloc_type_class type;
9264 /* We use this as an array of size int_rels_per_ext_rel. */
9265 Elf_Internal_Rela rela[1];
9266 };
9267
9268 /* qsort stability here and for cmp2 is only an issue if multiple
9269 dynamic relocations are emitted at the same address. But targets
9270 that apply a series of dynamic relocations each operating on the
9271 result of the prior relocation can't use -z combreloc as
9272 implemented anyway. Such schemes tend to be broken by sorting on
9273 symbol index. That leaves dynamic NONE relocs as the only other
9274 case where ld might emit multiple relocs at the same address, and
9275 those are only emitted due to target bugs. */
9276
9277 static int
9278 elf_link_sort_cmp1 (const void *A, const void *B)
9279 {
9280 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9281 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9282 int relativea, relativeb;
9283
9284 relativea = a->type == reloc_class_relative;
9285 relativeb = b->type == reloc_class_relative;
9286
9287 if (relativea < relativeb)
9288 return 1;
9289 if (relativea > relativeb)
9290 return -1;
9291 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9292 return -1;
9293 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9294 return 1;
9295 if (a->rela->r_offset < b->rela->r_offset)
9296 return -1;
9297 if (a->rela->r_offset > b->rela->r_offset)
9298 return 1;
9299 return 0;
9300 }
9301
9302 static int
9303 elf_link_sort_cmp2 (const void *A, const void *B)
9304 {
9305 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9306 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9307
9308 if (a->type < b->type)
9309 return -1;
9310 if (a->type > b->type)
9311 return 1;
9312 if (a->u.offset < b->u.offset)
9313 return -1;
9314 if (a->u.offset > b->u.offset)
9315 return 1;
9316 if (a->rela->r_offset < b->rela->r_offset)
9317 return -1;
9318 if (a->rela->r_offset > b->rela->r_offset)
9319 return 1;
9320 return 0;
9321 }
9322
9323 static size_t
9324 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9325 {
9326 asection *dynamic_relocs;
9327 asection *rela_dyn;
9328 asection *rel_dyn;
9329 bfd_size_type count, size;
9330 size_t i, ret, sort_elt, ext_size;
9331 bfd_byte *sort, *s_non_relative, *p;
9332 struct elf_link_sort_rela *sq;
9333 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9334 int i2e = bed->s->int_rels_per_ext_rel;
9335 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9336 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9337 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9338 struct bfd_link_order *lo;
9339 bfd_vma r_sym_mask;
9340 bfd_boolean use_rela;
9341
9342 /* Find a dynamic reloc section. */
9343 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9344 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9345 if (rela_dyn != NULL && rela_dyn->size > 0
9346 && rel_dyn != NULL && rel_dyn->size > 0)
9347 {
9348 bfd_boolean use_rela_initialised = FALSE;
9349
9350 /* This is just here to stop gcc from complaining.
9351 Its initialization checking code is not perfect. */
9352 use_rela = TRUE;
9353
9354 /* Both sections are present. Examine the sizes
9355 of the indirect sections to help us choose. */
9356 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9357 if (lo->type == bfd_indirect_link_order)
9358 {
9359 asection *o = lo->u.indirect.section;
9360
9361 if ((o->size % bed->s->sizeof_rela) == 0)
9362 {
9363 if ((o->size % bed->s->sizeof_rel) == 0)
9364 /* Section size is divisible by both rel and rela sizes.
9365 It is of no help to us. */
9366 ;
9367 else
9368 {
9369 /* Section size is only divisible by rela. */
9370 if (use_rela_initialised && !use_rela)
9371 {
9372 _bfd_error_handler (_("%pB: unable to sort relocs - "
9373 "they are in more than one size"),
9374 abfd);
9375 bfd_set_error (bfd_error_invalid_operation);
9376 return 0;
9377 }
9378 else
9379 {
9380 use_rela = TRUE;
9381 use_rela_initialised = TRUE;
9382 }
9383 }
9384 }
9385 else if ((o->size % bed->s->sizeof_rel) == 0)
9386 {
9387 /* Section size is only divisible by rel. */
9388 if (use_rela_initialised && use_rela)
9389 {
9390 _bfd_error_handler (_("%pB: unable to sort relocs - "
9391 "they are in more than one size"),
9392 abfd);
9393 bfd_set_error (bfd_error_invalid_operation);
9394 return 0;
9395 }
9396 else
9397 {
9398 use_rela = FALSE;
9399 use_rela_initialised = TRUE;
9400 }
9401 }
9402 else
9403 {
9404 /* The section size is not divisible by either -
9405 something is wrong. */
9406 _bfd_error_handler (_("%pB: unable to sort relocs - "
9407 "they are of an unknown size"), abfd);
9408 bfd_set_error (bfd_error_invalid_operation);
9409 return 0;
9410 }
9411 }
9412
9413 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9414 if (lo->type == bfd_indirect_link_order)
9415 {
9416 asection *o = lo->u.indirect.section;
9417
9418 if ((o->size % bed->s->sizeof_rela) == 0)
9419 {
9420 if ((o->size % bed->s->sizeof_rel) == 0)
9421 /* Section size is divisible by both rel and rela sizes.
9422 It is of no help to us. */
9423 ;
9424 else
9425 {
9426 /* Section size is only divisible by rela. */
9427 if (use_rela_initialised && !use_rela)
9428 {
9429 _bfd_error_handler (_("%pB: unable to sort relocs - "
9430 "they are in more than one size"),
9431 abfd);
9432 bfd_set_error (bfd_error_invalid_operation);
9433 return 0;
9434 }
9435 else
9436 {
9437 use_rela = TRUE;
9438 use_rela_initialised = TRUE;
9439 }
9440 }
9441 }
9442 else if ((o->size % bed->s->sizeof_rel) == 0)
9443 {
9444 /* Section size is only divisible by rel. */
9445 if (use_rela_initialised && use_rela)
9446 {
9447 _bfd_error_handler (_("%pB: unable to sort relocs - "
9448 "they are in more than one size"),
9449 abfd);
9450 bfd_set_error (bfd_error_invalid_operation);
9451 return 0;
9452 }
9453 else
9454 {
9455 use_rela = FALSE;
9456 use_rela_initialised = TRUE;
9457 }
9458 }
9459 else
9460 {
9461 /* The section size is not divisible by either -
9462 something is wrong. */
9463 _bfd_error_handler (_("%pB: unable to sort relocs - "
9464 "they are of an unknown size"), abfd);
9465 bfd_set_error (bfd_error_invalid_operation);
9466 return 0;
9467 }
9468 }
9469
9470 if (! use_rela_initialised)
9471 /* Make a guess. */
9472 use_rela = TRUE;
9473 }
9474 else if (rela_dyn != NULL && rela_dyn->size > 0)
9475 use_rela = TRUE;
9476 else if (rel_dyn != NULL && rel_dyn->size > 0)
9477 use_rela = FALSE;
9478 else
9479 return 0;
9480
9481 if (use_rela)
9482 {
9483 dynamic_relocs = rela_dyn;
9484 ext_size = bed->s->sizeof_rela;
9485 swap_in = bed->s->swap_reloca_in;
9486 swap_out = bed->s->swap_reloca_out;
9487 }
9488 else
9489 {
9490 dynamic_relocs = rel_dyn;
9491 ext_size = bed->s->sizeof_rel;
9492 swap_in = bed->s->swap_reloc_in;
9493 swap_out = bed->s->swap_reloc_out;
9494 }
9495
9496 size = 0;
9497 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9498 if (lo->type == bfd_indirect_link_order)
9499 size += lo->u.indirect.section->size;
9500
9501 if (size != dynamic_relocs->size)
9502 return 0;
9503
9504 sort_elt = (sizeof (struct elf_link_sort_rela)
9505 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9506
9507 count = dynamic_relocs->size / ext_size;
9508 if (count == 0)
9509 return 0;
9510 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9511
9512 if (sort == NULL)
9513 {
9514 (*info->callbacks->warning)
9515 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9516 return 0;
9517 }
9518
9519 if (bed->s->arch_size == 32)
9520 r_sym_mask = ~(bfd_vma) 0xff;
9521 else
9522 r_sym_mask = ~(bfd_vma) 0xffffffff;
9523
9524 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9525 if (lo->type == bfd_indirect_link_order)
9526 {
9527 bfd_byte *erel, *erelend;
9528 asection *o = lo->u.indirect.section;
9529
9530 if (o->contents == NULL && o->size != 0)
9531 {
9532 /* This is a reloc section that is being handled as a normal
9533 section. See bfd_section_from_shdr. We can't combine
9534 relocs in this case. */
9535 free (sort);
9536 return 0;
9537 }
9538 erel = o->contents;
9539 erelend = o->contents + o->size;
9540 p = sort + o->output_offset * opb / ext_size * sort_elt;
9541
9542 while (erel < erelend)
9543 {
9544 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9545
9546 (*swap_in) (abfd, erel, s->rela);
9547 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9548 s->u.sym_mask = r_sym_mask;
9549 p += sort_elt;
9550 erel += ext_size;
9551 }
9552 }
9553
9554 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9555
9556 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9557 {
9558 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9559 if (s->type != reloc_class_relative)
9560 break;
9561 }
9562 ret = i;
9563 s_non_relative = p;
9564
9565 sq = (struct elf_link_sort_rela *) s_non_relative;
9566 for (; i < count; i++, p += sort_elt)
9567 {
9568 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9569 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9570 sq = sp;
9571 sp->u.offset = sq->rela->r_offset;
9572 }
9573
9574 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9575
9576 struct elf_link_hash_table *htab = elf_hash_table (info);
9577 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9578 {
9579 /* We have plt relocs in .rela.dyn. */
9580 sq = (struct elf_link_sort_rela *) sort;
9581 for (i = 0; i < count; i++)
9582 if (sq[count - i - 1].type != reloc_class_plt)
9583 break;
9584 if (i != 0 && htab->srelplt->size == i * ext_size)
9585 {
9586 struct bfd_link_order **plo;
9587 /* Put srelplt link_order last. This is so the output_offset
9588 set in the next loop is correct for DT_JMPREL. */
9589 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9590 if ((*plo)->type == bfd_indirect_link_order
9591 && (*plo)->u.indirect.section == htab->srelplt)
9592 {
9593 lo = *plo;
9594 *plo = lo->next;
9595 }
9596 else
9597 plo = &(*plo)->next;
9598 *plo = lo;
9599 lo->next = NULL;
9600 dynamic_relocs->map_tail.link_order = lo;
9601 }
9602 }
9603
9604 p = sort;
9605 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9606 if (lo->type == bfd_indirect_link_order)
9607 {
9608 bfd_byte *erel, *erelend;
9609 asection *o = lo->u.indirect.section;
9610
9611 erel = o->contents;
9612 erelend = o->contents + o->size;
9613 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9614 while (erel < erelend)
9615 {
9616 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9617 (*swap_out) (abfd, s->rela, erel);
9618 p += sort_elt;
9619 erel += ext_size;
9620 }
9621 }
9622
9623 free (sort);
9624 *psec = dynamic_relocs;
9625 return ret;
9626 }
9627
9628 /* Add a symbol to the output symbol string table. */
9629
9630 static int
9631 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9632 const char *name,
9633 Elf_Internal_Sym *elfsym,
9634 asection *input_sec,
9635 struct elf_link_hash_entry *h)
9636 {
9637 int (*output_symbol_hook)
9638 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9639 struct elf_link_hash_entry *);
9640 struct elf_link_hash_table *hash_table;
9641 const struct elf_backend_data *bed;
9642 bfd_size_type strtabsize;
9643
9644 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9645
9646 bed = get_elf_backend_data (flinfo->output_bfd);
9647 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9648 if (output_symbol_hook != NULL)
9649 {
9650 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9651 if (ret != 1)
9652 return ret;
9653 }
9654
9655 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9656 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9657 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9658 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9659
9660 if (name == NULL
9661 || *name == '\0'
9662 || (input_sec->flags & SEC_EXCLUDE))
9663 elfsym->st_name = (unsigned long) -1;
9664 else
9665 {
9666 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9667 to get the final offset for st_name. */
9668 char *versioned_name = (char *) name;
9669 if (h != NULL && h->versioned == versioned && h->def_dynamic)
9670 {
9671 /* Keep only one '@' for versioned symbols defined in shared
9672 objects. */
9673 char *version = strrchr (name, ELF_VER_CHR);
9674 char *base_end = strchr (name, ELF_VER_CHR);
9675 if (version != base_end)
9676 {
9677 size_t base_len;
9678 size_t len = strlen (name);
9679 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9680 if (versioned_name == NULL)
9681 return 0;
9682 base_len = base_end - name;
9683 memcpy (versioned_name, name, base_len);
9684 memcpy (versioned_name + base_len, version,
9685 len - base_len);
9686 }
9687 }
9688 elfsym->st_name
9689 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9690 versioned_name, FALSE);
9691 if (elfsym->st_name == (unsigned long) -1)
9692 return 0;
9693 }
9694
9695 hash_table = elf_hash_table (flinfo->info);
9696 strtabsize = hash_table->strtabsize;
9697 if (strtabsize <= hash_table->strtabcount)
9698 {
9699 strtabsize += strtabsize;
9700 hash_table->strtabsize = strtabsize;
9701 strtabsize *= sizeof (*hash_table->strtab);
9702 hash_table->strtab
9703 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9704 strtabsize);
9705 if (hash_table->strtab == NULL)
9706 return 0;
9707 }
9708 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9709 hash_table->strtab[hash_table->strtabcount].dest_index
9710 = hash_table->strtabcount;
9711 hash_table->strtab[hash_table->strtabcount].destshndx_index
9712 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9713
9714 flinfo->output_bfd->symcount += 1;
9715 hash_table->strtabcount += 1;
9716
9717 return 1;
9718 }
9719
9720 /* Swap symbols out to the symbol table and flush the output symbols to
9721 the file. */
9722
9723 static bfd_boolean
9724 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9725 {
9726 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9727 size_t amt;
9728 size_t i;
9729 const struct elf_backend_data *bed;
9730 bfd_byte *symbuf;
9731 Elf_Internal_Shdr *hdr;
9732 file_ptr pos;
9733 bfd_boolean ret;
9734
9735 if (!hash_table->strtabcount)
9736 return TRUE;
9737
9738 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9739
9740 bed = get_elf_backend_data (flinfo->output_bfd);
9741
9742 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9743 symbuf = (bfd_byte *) bfd_malloc (amt);
9744 if (symbuf == NULL)
9745 return FALSE;
9746
9747 if (flinfo->symshndxbuf)
9748 {
9749 amt = sizeof (Elf_External_Sym_Shndx);
9750 amt *= bfd_get_symcount (flinfo->output_bfd);
9751 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9752 if (flinfo->symshndxbuf == NULL)
9753 {
9754 free (symbuf);
9755 return FALSE;
9756 }
9757 }
9758
9759 for (i = 0; i < hash_table->strtabcount; i++)
9760 {
9761 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9762 if (elfsym->sym.st_name == (unsigned long) -1)
9763 elfsym->sym.st_name = 0;
9764 else
9765 elfsym->sym.st_name
9766 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9767 elfsym->sym.st_name);
9768 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9769 ((bfd_byte *) symbuf
9770 + (elfsym->dest_index
9771 * bed->s->sizeof_sym)),
9772 (flinfo->symshndxbuf
9773 + elfsym->destshndx_index));
9774 }
9775
9776 /* Allow the linker to examine the strtab and symtab now they are
9777 populated. */
9778
9779 if (flinfo->info->callbacks->examine_strtab)
9780 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9781 hash_table->strtabcount,
9782 flinfo->symstrtab);
9783
9784 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9785 pos = hdr->sh_offset + hdr->sh_size;
9786 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9787 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9788 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9789 {
9790 hdr->sh_size += amt;
9791 ret = TRUE;
9792 }
9793 else
9794 ret = FALSE;
9795
9796 free (symbuf);
9797
9798 free (hash_table->strtab);
9799 hash_table->strtab = NULL;
9800
9801 return ret;
9802 }
9803
9804 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9805
9806 static bfd_boolean
9807 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9808 {
9809 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9810 && sym->st_shndx < SHN_LORESERVE)
9811 {
9812 /* The gABI doesn't support dynamic symbols in output sections
9813 beyond 64k. */
9814 _bfd_error_handler
9815 /* xgettext:c-format */
9816 (_("%pB: too many sections: %d (>= %d)"),
9817 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9818 bfd_set_error (bfd_error_nonrepresentable_section);
9819 return FALSE;
9820 }
9821 return TRUE;
9822 }
9823
9824 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9825 allowing an unsatisfied unversioned symbol in the DSO to match a
9826 versioned symbol that would normally require an explicit version.
9827 We also handle the case that a DSO references a hidden symbol
9828 which may be satisfied by a versioned symbol in another DSO. */
9829
9830 static bfd_boolean
9831 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9832 const struct elf_backend_data *bed,
9833 struct elf_link_hash_entry *h)
9834 {
9835 bfd *abfd;
9836 struct elf_link_loaded_list *loaded;
9837
9838 if (!is_elf_hash_table (info->hash))
9839 return FALSE;
9840
9841 /* Check indirect symbol. */
9842 while (h->root.type == bfd_link_hash_indirect)
9843 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9844
9845 switch (h->root.type)
9846 {
9847 default:
9848 abfd = NULL;
9849 break;
9850
9851 case bfd_link_hash_undefined:
9852 case bfd_link_hash_undefweak:
9853 abfd = h->root.u.undef.abfd;
9854 if (abfd == NULL
9855 || (abfd->flags & DYNAMIC) == 0
9856 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9857 return FALSE;
9858 break;
9859
9860 case bfd_link_hash_defined:
9861 case bfd_link_hash_defweak:
9862 abfd = h->root.u.def.section->owner;
9863 break;
9864
9865 case bfd_link_hash_common:
9866 abfd = h->root.u.c.p->section->owner;
9867 break;
9868 }
9869 BFD_ASSERT (abfd != NULL);
9870
9871 for (loaded = elf_hash_table (info)->dyn_loaded;
9872 loaded != NULL;
9873 loaded = loaded->next)
9874 {
9875 bfd *input;
9876 Elf_Internal_Shdr *hdr;
9877 size_t symcount;
9878 size_t extsymcount;
9879 size_t extsymoff;
9880 Elf_Internal_Shdr *versymhdr;
9881 Elf_Internal_Sym *isym;
9882 Elf_Internal_Sym *isymend;
9883 Elf_Internal_Sym *isymbuf;
9884 Elf_External_Versym *ever;
9885 Elf_External_Versym *extversym;
9886
9887 input = loaded->abfd;
9888
9889 /* We check each DSO for a possible hidden versioned definition. */
9890 if (input == abfd
9891 || elf_dynversym (input) == 0)
9892 continue;
9893
9894 hdr = &elf_tdata (input)->dynsymtab_hdr;
9895
9896 symcount = hdr->sh_size / bed->s->sizeof_sym;
9897 if (elf_bad_symtab (input))
9898 {
9899 extsymcount = symcount;
9900 extsymoff = 0;
9901 }
9902 else
9903 {
9904 extsymcount = symcount - hdr->sh_info;
9905 extsymoff = hdr->sh_info;
9906 }
9907
9908 if (extsymcount == 0)
9909 continue;
9910
9911 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9912 NULL, NULL, NULL);
9913 if (isymbuf == NULL)
9914 return FALSE;
9915
9916 /* Read in any version definitions. */
9917 versymhdr = &elf_tdata (input)->dynversym_hdr;
9918 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9919 || (extversym = (Elf_External_Versym *)
9920 _bfd_malloc_and_read (input, versymhdr->sh_size,
9921 versymhdr->sh_size)) == NULL)
9922 {
9923 free (isymbuf);
9924 return FALSE;
9925 }
9926
9927 ever = extversym + extsymoff;
9928 isymend = isymbuf + extsymcount;
9929 for (isym = isymbuf; isym < isymend; isym++, ever++)
9930 {
9931 const char *name;
9932 Elf_Internal_Versym iver;
9933 unsigned short version_index;
9934
9935 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9936 || isym->st_shndx == SHN_UNDEF)
9937 continue;
9938
9939 name = bfd_elf_string_from_elf_section (input,
9940 hdr->sh_link,
9941 isym->st_name);
9942 if (strcmp (name, h->root.root.string) != 0)
9943 continue;
9944
9945 _bfd_elf_swap_versym_in (input, ever, &iver);
9946
9947 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9948 && !(h->def_regular
9949 && h->forced_local))
9950 {
9951 /* If we have a non-hidden versioned sym, then it should
9952 have provided a definition for the undefined sym unless
9953 it is defined in a non-shared object and forced local.
9954 */
9955 abort ();
9956 }
9957
9958 version_index = iver.vs_vers & VERSYM_VERSION;
9959 if (version_index == 1 || version_index == 2)
9960 {
9961 /* This is the base or first version. We can use it. */
9962 free (extversym);
9963 free (isymbuf);
9964 return TRUE;
9965 }
9966 }
9967
9968 free (extversym);
9969 free (isymbuf);
9970 }
9971
9972 return FALSE;
9973 }
9974
9975 /* Convert ELF common symbol TYPE. */
9976
9977 static int
9978 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9979 {
9980 /* Commom symbol can only appear in relocatable link. */
9981 if (!bfd_link_relocatable (info))
9982 abort ();
9983 switch (info->elf_stt_common)
9984 {
9985 case unchanged:
9986 break;
9987 case elf_stt_common:
9988 type = STT_COMMON;
9989 break;
9990 case no_elf_stt_common:
9991 type = STT_OBJECT;
9992 break;
9993 }
9994 return type;
9995 }
9996
9997 /* Add an external symbol to the symbol table. This is called from
9998 the hash table traversal routine. When generating a shared object,
9999 we go through the symbol table twice. The first time we output
10000 anything that might have been forced to local scope in a version
10001 script. The second time we output the symbols that are still
10002 global symbols. */
10003
10004 static bfd_boolean
10005 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10006 {
10007 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10008 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10009 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10010 bfd_boolean strip;
10011 Elf_Internal_Sym sym;
10012 asection *input_sec;
10013 const struct elf_backend_data *bed;
10014 long indx;
10015 int ret;
10016 unsigned int type;
10017
10018 if (h->root.type == bfd_link_hash_warning)
10019 {
10020 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10021 if (h->root.type == bfd_link_hash_new)
10022 return TRUE;
10023 }
10024
10025 /* Decide whether to output this symbol in this pass. */
10026 if (eoinfo->localsyms)
10027 {
10028 if (!h->forced_local)
10029 return TRUE;
10030 }
10031 else
10032 {
10033 if (h->forced_local)
10034 return TRUE;
10035 }
10036
10037 bed = get_elf_backend_data (flinfo->output_bfd);
10038
10039 if (h->root.type == bfd_link_hash_undefined)
10040 {
10041 /* If we have an undefined symbol reference here then it must have
10042 come from a shared library that is being linked in. (Undefined
10043 references in regular files have already been handled unless
10044 they are in unreferenced sections which are removed by garbage
10045 collection). */
10046 bfd_boolean ignore_undef = FALSE;
10047
10048 /* Some symbols may be special in that the fact that they're
10049 undefined can be safely ignored - let backend determine that. */
10050 if (bed->elf_backend_ignore_undef_symbol)
10051 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10052
10053 /* If we are reporting errors for this situation then do so now. */
10054 if (!ignore_undef
10055 && h->ref_dynamic_nonweak
10056 && (!h->ref_regular || flinfo->info->gc_sections)
10057 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10058 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10059 {
10060 flinfo->info->callbacks->undefined_symbol
10061 (flinfo->info, h->root.root.string,
10062 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10063 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10064 && !flinfo->info->warn_unresolved_syms);
10065 }
10066
10067 /* Strip a global symbol defined in a discarded section. */
10068 if (h->indx == -3)
10069 return TRUE;
10070 }
10071
10072 /* We should also warn if a forced local symbol is referenced from
10073 shared libraries. */
10074 if (bfd_link_executable (flinfo->info)
10075 && h->forced_local
10076 && h->ref_dynamic
10077 && h->def_regular
10078 && !h->dynamic_def
10079 && h->ref_dynamic_nonweak
10080 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10081 {
10082 bfd *def_bfd;
10083 const char *msg;
10084 struct elf_link_hash_entry *hi = h;
10085
10086 /* Check indirect symbol. */
10087 while (hi->root.type == bfd_link_hash_indirect)
10088 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10089
10090 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10091 /* xgettext:c-format */
10092 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10093 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10094 /* xgettext:c-format */
10095 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10096 else
10097 /* xgettext:c-format */
10098 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10099 def_bfd = flinfo->output_bfd;
10100 if (hi->root.u.def.section != bfd_abs_section_ptr)
10101 def_bfd = hi->root.u.def.section->owner;
10102 _bfd_error_handler (msg, flinfo->output_bfd,
10103 h->root.root.string, def_bfd);
10104 bfd_set_error (bfd_error_bad_value);
10105 eoinfo->failed = TRUE;
10106 return FALSE;
10107 }
10108
10109 /* We don't want to output symbols that have never been mentioned by
10110 a regular file, or that we have been told to strip. However, if
10111 h->indx is set to -2, the symbol is used by a reloc and we must
10112 output it. */
10113 strip = FALSE;
10114 if (h->indx == -2)
10115 ;
10116 else if ((h->def_dynamic
10117 || h->ref_dynamic
10118 || h->root.type == bfd_link_hash_new)
10119 && !h->def_regular
10120 && !h->ref_regular)
10121 strip = TRUE;
10122 else if (flinfo->info->strip == strip_all)
10123 strip = TRUE;
10124 else if (flinfo->info->strip == strip_some
10125 && bfd_hash_lookup (flinfo->info->keep_hash,
10126 h->root.root.string, FALSE, FALSE) == NULL)
10127 strip = TRUE;
10128 else if ((h->root.type == bfd_link_hash_defined
10129 || h->root.type == bfd_link_hash_defweak)
10130 && ((flinfo->info->strip_discarded
10131 && discarded_section (h->root.u.def.section))
10132 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10133 && h->root.u.def.section->owner != NULL
10134 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10135 strip = TRUE;
10136 else if ((h->root.type == bfd_link_hash_undefined
10137 || h->root.type == bfd_link_hash_undefweak)
10138 && h->root.u.undef.abfd != NULL
10139 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10140 strip = TRUE;
10141
10142 type = h->type;
10143
10144 /* If we're stripping it, and it's not a dynamic symbol, there's
10145 nothing else to do. However, if it is a forced local symbol or
10146 an ifunc symbol we need to give the backend finish_dynamic_symbol
10147 function a chance to make it dynamic. */
10148 if (strip
10149 && h->dynindx == -1
10150 && type != STT_GNU_IFUNC
10151 && !h->forced_local)
10152 return TRUE;
10153
10154 sym.st_value = 0;
10155 sym.st_size = h->size;
10156 sym.st_other = h->other;
10157 switch (h->root.type)
10158 {
10159 default:
10160 case bfd_link_hash_new:
10161 case bfd_link_hash_warning:
10162 abort ();
10163 return FALSE;
10164
10165 case bfd_link_hash_undefined:
10166 case bfd_link_hash_undefweak:
10167 input_sec = bfd_und_section_ptr;
10168 sym.st_shndx = SHN_UNDEF;
10169 break;
10170
10171 case bfd_link_hash_defined:
10172 case bfd_link_hash_defweak:
10173 {
10174 input_sec = h->root.u.def.section;
10175 if (input_sec->output_section != NULL)
10176 {
10177 sym.st_shndx =
10178 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10179 input_sec->output_section);
10180 if (sym.st_shndx == SHN_BAD)
10181 {
10182 _bfd_error_handler
10183 /* xgettext:c-format */
10184 (_("%pB: could not find output section %pA for input section %pA"),
10185 flinfo->output_bfd, input_sec->output_section, input_sec);
10186 bfd_set_error (bfd_error_nonrepresentable_section);
10187 eoinfo->failed = TRUE;
10188 return FALSE;
10189 }
10190
10191 /* ELF symbols in relocatable files are section relative,
10192 but in nonrelocatable files they are virtual
10193 addresses. */
10194 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10195 if (!bfd_link_relocatable (flinfo->info))
10196 {
10197 sym.st_value += input_sec->output_section->vma;
10198 if (h->type == STT_TLS)
10199 {
10200 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10201 if (tls_sec != NULL)
10202 sym.st_value -= tls_sec->vma;
10203 }
10204 }
10205 }
10206 else
10207 {
10208 BFD_ASSERT (input_sec->owner == NULL
10209 || (input_sec->owner->flags & DYNAMIC) != 0);
10210 sym.st_shndx = SHN_UNDEF;
10211 input_sec = bfd_und_section_ptr;
10212 }
10213 }
10214 break;
10215
10216 case bfd_link_hash_common:
10217 input_sec = h->root.u.c.p->section;
10218 sym.st_shndx = bed->common_section_index (input_sec);
10219 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10220 break;
10221
10222 case bfd_link_hash_indirect:
10223 /* These symbols are created by symbol versioning. They point
10224 to the decorated version of the name. For example, if the
10225 symbol foo@@GNU_1.2 is the default, which should be used when
10226 foo is used with no version, then we add an indirect symbol
10227 foo which points to foo@@GNU_1.2. We ignore these symbols,
10228 since the indirected symbol is already in the hash table. */
10229 return TRUE;
10230 }
10231
10232 if (type == STT_COMMON || type == STT_OBJECT)
10233 switch (h->root.type)
10234 {
10235 case bfd_link_hash_common:
10236 type = elf_link_convert_common_type (flinfo->info, type);
10237 break;
10238 case bfd_link_hash_defined:
10239 case bfd_link_hash_defweak:
10240 if (bed->common_definition (&sym))
10241 type = elf_link_convert_common_type (flinfo->info, type);
10242 else
10243 type = STT_OBJECT;
10244 break;
10245 case bfd_link_hash_undefined:
10246 case bfd_link_hash_undefweak:
10247 break;
10248 default:
10249 abort ();
10250 }
10251
10252 if (h->forced_local)
10253 {
10254 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10255 /* Turn off visibility on local symbol. */
10256 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10257 }
10258 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10259 else if (h->unique_global && h->def_regular)
10260 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10261 else if (h->root.type == bfd_link_hash_undefweak
10262 || h->root.type == bfd_link_hash_defweak)
10263 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10264 else
10265 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10266 sym.st_target_internal = h->target_internal;
10267
10268 /* Give the processor backend a chance to tweak the symbol value,
10269 and also to finish up anything that needs to be done for this
10270 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10271 forced local syms when non-shared is due to a historical quirk.
10272 STT_GNU_IFUNC symbol must go through PLT. */
10273 if ((h->type == STT_GNU_IFUNC
10274 && h->def_regular
10275 && !bfd_link_relocatable (flinfo->info))
10276 || ((h->dynindx != -1
10277 || h->forced_local)
10278 && ((bfd_link_pic (flinfo->info)
10279 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10280 || h->root.type != bfd_link_hash_undefweak))
10281 || !h->forced_local)
10282 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10283 {
10284 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10285 (flinfo->output_bfd, flinfo->info, h, &sym)))
10286 {
10287 eoinfo->failed = TRUE;
10288 return FALSE;
10289 }
10290 }
10291
10292 /* If we are marking the symbol as undefined, and there are no
10293 non-weak references to this symbol from a regular object, then
10294 mark the symbol as weak undefined; if there are non-weak
10295 references, mark the symbol as strong. We can't do this earlier,
10296 because it might not be marked as undefined until the
10297 finish_dynamic_symbol routine gets through with it. */
10298 if (sym.st_shndx == SHN_UNDEF
10299 && h->ref_regular
10300 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10301 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10302 {
10303 int bindtype;
10304 type = ELF_ST_TYPE (sym.st_info);
10305
10306 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10307 if (type == STT_GNU_IFUNC)
10308 type = STT_FUNC;
10309
10310 if (h->ref_regular_nonweak)
10311 bindtype = STB_GLOBAL;
10312 else
10313 bindtype = STB_WEAK;
10314 sym.st_info = ELF_ST_INFO (bindtype, type);
10315 }
10316
10317 /* If this is a symbol defined in a dynamic library, don't use the
10318 symbol size from the dynamic library. Relinking an executable
10319 against a new library may introduce gratuitous changes in the
10320 executable's symbols if we keep the size. */
10321 if (sym.st_shndx == SHN_UNDEF
10322 && !h->def_regular
10323 && h->def_dynamic)
10324 sym.st_size = 0;
10325
10326 /* If a non-weak symbol with non-default visibility is not defined
10327 locally, it is a fatal error. */
10328 if (!bfd_link_relocatable (flinfo->info)
10329 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10330 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10331 && h->root.type == bfd_link_hash_undefined
10332 && !h->def_regular)
10333 {
10334 const char *msg;
10335
10336 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10337 /* xgettext:c-format */
10338 msg = _("%pB: protected symbol `%s' isn't defined");
10339 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10340 /* xgettext:c-format */
10341 msg = _("%pB: internal symbol `%s' isn't defined");
10342 else
10343 /* xgettext:c-format */
10344 msg = _("%pB: hidden symbol `%s' isn't defined");
10345 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10346 bfd_set_error (bfd_error_bad_value);
10347 eoinfo->failed = TRUE;
10348 return FALSE;
10349 }
10350
10351 /* If this symbol should be put in the .dynsym section, then put it
10352 there now. We already know the symbol index. We also fill in
10353 the entry in the .hash section. */
10354 if (h->dynindx != -1
10355 && elf_hash_table (flinfo->info)->dynamic_sections_created
10356 && elf_hash_table (flinfo->info)->dynsym != NULL
10357 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10358 {
10359 bfd_byte *esym;
10360
10361 /* Since there is no version information in the dynamic string,
10362 if there is no version info in symbol version section, we will
10363 have a run-time problem if not linking executable, referenced
10364 by shared library, or not bound locally. */
10365 if (h->verinfo.verdef == NULL
10366 && (!bfd_link_executable (flinfo->info)
10367 || h->ref_dynamic
10368 || !h->def_regular))
10369 {
10370 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10371
10372 if (p && p [1] != '\0')
10373 {
10374 _bfd_error_handler
10375 /* xgettext:c-format */
10376 (_("%pB: no symbol version section for versioned symbol `%s'"),
10377 flinfo->output_bfd, h->root.root.string);
10378 eoinfo->failed = TRUE;
10379 return FALSE;
10380 }
10381 }
10382
10383 sym.st_name = h->dynstr_index;
10384 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10385 + h->dynindx * bed->s->sizeof_sym);
10386 if (!check_dynsym (flinfo->output_bfd, &sym))
10387 {
10388 eoinfo->failed = TRUE;
10389 return FALSE;
10390 }
10391 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10392
10393 if (flinfo->hash_sec != NULL)
10394 {
10395 size_t hash_entry_size;
10396 bfd_byte *bucketpos;
10397 bfd_vma chain;
10398 size_t bucketcount;
10399 size_t bucket;
10400
10401 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10402 bucket = h->u.elf_hash_value % bucketcount;
10403
10404 hash_entry_size
10405 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10406 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10407 + (bucket + 2) * hash_entry_size);
10408 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10409 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10410 bucketpos);
10411 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10412 ((bfd_byte *) flinfo->hash_sec->contents
10413 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10414 }
10415
10416 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10417 {
10418 Elf_Internal_Versym iversym;
10419 Elf_External_Versym *eversym;
10420
10421 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10422 {
10423 if (h->verinfo.verdef == NULL
10424 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10425 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10426 iversym.vs_vers = 0;
10427 else
10428 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10429 }
10430 else
10431 {
10432 if (h->verinfo.vertree == NULL)
10433 iversym.vs_vers = 1;
10434 else
10435 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10436 if (flinfo->info->create_default_symver)
10437 iversym.vs_vers++;
10438 }
10439
10440 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10441 defined locally. */
10442 if (h->versioned == versioned_hidden && h->def_regular)
10443 iversym.vs_vers |= VERSYM_HIDDEN;
10444
10445 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10446 eversym += h->dynindx;
10447 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10448 }
10449 }
10450
10451 /* If the symbol is undefined, and we didn't output it to .dynsym,
10452 strip it from .symtab too. Obviously we can't do this for
10453 relocatable output or when needed for --emit-relocs. */
10454 else if (input_sec == bfd_und_section_ptr
10455 && h->indx != -2
10456 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10457 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10458 && !bfd_link_relocatable (flinfo->info))
10459 return TRUE;
10460
10461 /* Also strip others that we couldn't earlier due to dynamic symbol
10462 processing. */
10463 if (strip)
10464 return TRUE;
10465 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10466 return TRUE;
10467
10468 /* Output a FILE symbol so that following locals are not associated
10469 with the wrong input file. We need one for forced local symbols
10470 if we've seen more than one FILE symbol or when we have exactly
10471 one FILE symbol but global symbols are present in a file other
10472 than the one with the FILE symbol. We also need one if linker
10473 defined symbols are present. In practice these conditions are
10474 always met, so just emit the FILE symbol unconditionally. */
10475 if (eoinfo->localsyms
10476 && !eoinfo->file_sym_done
10477 && eoinfo->flinfo->filesym_count != 0)
10478 {
10479 Elf_Internal_Sym fsym;
10480
10481 memset (&fsym, 0, sizeof (fsym));
10482 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10483 fsym.st_shndx = SHN_ABS;
10484 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10485 bfd_und_section_ptr, NULL))
10486 return FALSE;
10487
10488 eoinfo->file_sym_done = TRUE;
10489 }
10490
10491 indx = bfd_get_symcount (flinfo->output_bfd);
10492 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10493 input_sec, h);
10494 if (ret == 0)
10495 {
10496 eoinfo->failed = TRUE;
10497 return FALSE;
10498 }
10499 else if (ret == 1)
10500 h->indx = indx;
10501 else if (h->indx == -2)
10502 abort();
10503
10504 return TRUE;
10505 }
10506
10507 /* Return TRUE if special handling is done for relocs in SEC against
10508 symbols defined in discarded sections. */
10509
10510 static bfd_boolean
10511 elf_section_ignore_discarded_relocs (asection *sec)
10512 {
10513 const struct elf_backend_data *bed;
10514
10515 switch (sec->sec_info_type)
10516 {
10517 case SEC_INFO_TYPE_STABS:
10518 case SEC_INFO_TYPE_EH_FRAME:
10519 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10520 return TRUE;
10521 default:
10522 break;
10523 }
10524
10525 bed = get_elf_backend_data (sec->owner);
10526 if (bed->elf_backend_ignore_discarded_relocs != NULL
10527 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10528 return TRUE;
10529
10530 return FALSE;
10531 }
10532
10533 /* Return a mask saying how ld should treat relocations in SEC against
10534 symbols defined in discarded sections. If this function returns
10535 COMPLAIN set, ld will issue a warning message. If this function
10536 returns PRETEND set, and the discarded section was link-once and the
10537 same size as the kept link-once section, ld will pretend that the
10538 symbol was actually defined in the kept section. Otherwise ld will
10539 zero the reloc (at least that is the intent, but some cooperation by
10540 the target dependent code is needed, particularly for REL targets). */
10541
10542 unsigned int
10543 _bfd_elf_default_action_discarded (asection *sec)
10544 {
10545 if (sec->flags & SEC_DEBUGGING)
10546 return PRETEND;
10547
10548 if (strcmp (".eh_frame", sec->name) == 0)
10549 return 0;
10550
10551 if (strcmp (".gcc_except_table", sec->name) == 0)
10552 return 0;
10553
10554 return COMPLAIN | PRETEND;
10555 }
10556
10557 /* Find a match between a section and a member of a section group. */
10558
10559 static asection *
10560 match_group_member (asection *sec, asection *group,
10561 struct bfd_link_info *info)
10562 {
10563 asection *first = elf_next_in_group (group);
10564 asection *s = first;
10565
10566 while (s != NULL)
10567 {
10568 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10569 return s;
10570
10571 s = elf_next_in_group (s);
10572 if (s == first)
10573 break;
10574 }
10575
10576 return NULL;
10577 }
10578
10579 /* Check if the kept section of a discarded section SEC can be used
10580 to replace it. Return the replacement if it is OK. Otherwise return
10581 NULL. */
10582
10583 asection *
10584 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10585 {
10586 asection *kept;
10587
10588 kept = sec->kept_section;
10589 if (kept != NULL)
10590 {
10591 if ((kept->flags & SEC_GROUP) != 0)
10592 kept = match_group_member (sec, kept, info);
10593 if (kept != NULL
10594 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10595 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10596 kept = NULL;
10597 sec->kept_section = kept;
10598 }
10599 return kept;
10600 }
10601
10602 /* Link an input file into the linker output file. This function
10603 handles all the sections and relocations of the input file at once.
10604 This is so that we only have to read the local symbols once, and
10605 don't have to keep them in memory. */
10606
10607 static bfd_boolean
10608 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10609 {
10610 int (*relocate_section)
10611 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10612 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10613 bfd *output_bfd;
10614 Elf_Internal_Shdr *symtab_hdr;
10615 size_t locsymcount;
10616 size_t extsymoff;
10617 Elf_Internal_Sym *isymbuf;
10618 Elf_Internal_Sym *isym;
10619 Elf_Internal_Sym *isymend;
10620 long *pindex;
10621 asection **ppsection;
10622 asection *o;
10623 const struct elf_backend_data *bed;
10624 struct elf_link_hash_entry **sym_hashes;
10625 bfd_size_type address_size;
10626 bfd_vma r_type_mask;
10627 int r_sym_shift;
10628 bfd_boolean have_file_sym = FALSE;
10629
10630 output_bfd = flinfo->output_bfd;
10631 bed = get_elf_backend_data (output_bfd);
10632 relocate_section = bed->elf_backend_relocate_section;
10633
10634 /* If this is a dynamic object, we don't want to do anything here:
10635 we don't want the local symbols, and we don't want the section
10636 contents. */
10637 if ((input_bfd->flags & DYNAMIC) != 0)
10638 return TRUE;
10639
10640 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10641 if (elf_bad_symtab (input_bfd))
10642 {
10643 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10644 extsymoff = 0;
10645 }
10646 else
10647 {
10648 locsymcount = symtab_hdr->sh_info;
10649 extsymoff = symtab_hdr->sh_info;
10650 }
10651
10652 /* Read the local symbols. */
10653 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10654 if (isymbuf == NULL && locsymcount != 0)
10655 {
10656 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10657 flinfo->internal_syms,
10658 flinfo->external_syms,
10659 flinfo->locsym_shndx);
10660 if (isymbuf == NULL)
10661 return FALSE;
10662 }
10663
10664 /* Find local symbol sections and adjust values of symbols in
10665 SEC_MERGE sections. Write out those local symbols we know are
10666 going into the output file. */
10667 isymend = isymbuf + locsymcount;
10668 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10669 isym < isymend;
10670 isym++, pindex++, ppsection++)
10671 {
10672 asection *isec;
10673 const char *name;
10674 Elf_Internal_Sym osym;
10675 long indx;
10676 int ret;
10677
10678 *pindex = -1;
10679
10680 if (elf_bad_symtab (input_bfd))
10681 {
10682 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10683 {
10684 *ppsection = NULL;
10685 continue;
10686 }
10687 }
10688
10689 if (isym->st_shndx == SHN_UNDEF)
10690 isec = bfd_und_section_ptr;
10691 else if (isym->st_shndx == SHN_ABS)
10692 isec = bfd_abs_section_ptr;
10693 else if (isym->st_shndx == SHN_COMMON)
10694 isec = bfd_com_section_ptr;
10695 else
10696 {
10697 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10698 if (isec == NULL)
10699 {
10700 /* Don't attempt to output symbols with st_shnx in the
10701 reserved range other than SHN_ABS and SHN_COMMON. */
10702 isec = bfd_und_section_ptr;
10703 }
10704 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10705 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10706 isym->st_value =
10707 _bfd_merged_section_offset (output_bfd, &isec,
10708 elf_section_data (isec)->sec_info,
10709 isym->st_value);
10710 }
10711
10712 *ppsection = isec;
10713
10714 /* Don't output the first, undefined, symbol. In fact, don't
10715 output any undefined local symbol. */
10716 if (isec == bfd_und_section_ptr)
10717 continue;
10718
10719 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10720 {
10721 /* We never output section symbols. Instead, we use the
10722 section symbol of the corresponding section in the output
10723 file. */
10724 continue;
10725 }
10726
10727 /* If we are stripping all symbols, we don't want to output this
10728 one. */
10729 if (flinfo->info->strip == strip_all)
10730 continue;
10731
10732 /* If we are discarding all local symbols, we don't want to
10733 output this one. If we are generating a relocatable output
10734 file, then some of the local symbols may be required by
10735 relocs; we output them below as we discover that they are
10736 needed. */
10737 if (flinfo->info->discard == discard_all)
10738 continue;
10739
10740 /* If this symbol is defined in a section which we are
10741 discarding, we don't need to keep it. */
10742 if (isym->st_shndx != SHN_UNDEF
10743 && isym->st_shndx < SHN_LORESERVE
10744 && isec->output_section == NULL
10745 && flinfo->info->non_contiguous_regions
10746 && flinfo->info->non_contiguous_regions_warnings)
10747 {
10748 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10749 "discards section `%s' from '%s'\n"),
10750 isec->name, bfd_get_filename (isec->owner));
10751 continue;
10752 }
10753
10754 if (isym->st_shndx != SHN_UNDEF
10755 && isym->st_shndx < SHN_LORESERVE
10756 && bfd_section_removed_from_list (output_bfd,
10757 isec->output_section))
10758 continue;
10759
10760 /* Get the name of the symbol. */
10761 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10762 isym->st_name);
10763 if (name == NULL)
10764 return FALSE;
10765
10766 /* See if we are discarding symbols with this name. */
10767 if ((flinfo->info->strip == strip_some
10768 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10769 == NULL))
10770 || (((flinfo->info->discard == discard_sec_merge
10771 && (isec->flags & SEC_MERGE)
10772 && !bfd_link_relocatable (flinfo->info))
10773 || flinfo->info->discard == discard_l)
10774 && bfd_is_local_label_name (input_bfd, name)))
10775 continue;
10776
10777 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10778 {
10779 if (input_bfd->lto_output)
10780 /* -flto puts a temp file name here. This means builds
10781 are not reproducible. Discard the symbol. */
10782 continue;
10783 have_file_sym = TRUE;
10784 flinfo->filesym_count += 1;
10785 }
10786 if (!have_file_sym)
10787 {
10788 /* In the absence of debug info, bfd_find_nearest_line uses
10789 FILE symbols to determine the source file for local
10790 function symbols. Provide a FILE symbol here if input
10791 files lack such, so that their symbols won't be
10792 associated with a previous input file. It's not the
10793 source file, but the best we can do. */
10794 have_file_sym = TRUE;
10795 flinfo->filesym_count += 1;
10796 memset (&osym, 0, sizeof (osym));
10797 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10798 osym.st_shndx = SHN_ABS;
10799 if (!elf_link_output_symstrtab (flinfo,
10800 (input_bfd->lto_output ? NULL
10801 : bfd_get_filename (input_bfd)),
10802 &osym, bfd_abs_section_ptr,
10803 NULL))
10804 return FALSE;
10805 }
10806
10807 osym = *isym;
10808
10809 /* Adjust the section index for the output file. */
10810 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10811 isec->output_section);
10812 if (osym.st_shndx == SHN_BAD)
10813 return FALSE;
10814
10815 /* ELF symbols in relocatable files are section relative, but
10816 in executable files they are virtual addresses. Note that
10817 this code assumes that all ELF sections have an associated
10818 BFD section with a reasonable value for output_offset; below
10819 we assume that they also have a reasonable value for
10820 output_section. Any special sections must be set up to meet
10821 these requirements. */
10822 osym.st_value += isec->output_offset;
10823 if (!bfd_link_relocatable (flinfo->info))
10824 {
10825 osym.st_value += isec->output_section->vma;
10826 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10827 {
10828 /* STT_TLS symbols are relative to PT_TLS segment base. */
10829 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10830 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10831 else
10832 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10833 STT_NOTYPE);
10834 }
10835 }
10836
10837 indx = bfd_get_symcount (output_bfd);
10838 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10839 if (ret == 0)
10840 return FALSE;
10841 else if (ret == 1)
10842 *pindex = indx;
10843 }
10844
10845 if (bed->s->arch_size == 32)
10846 {
10847 r_type_mask = 0xff;
10848 r_sym_shift = 8;
10849 address_size = 4;
10850 }
10851 else
10852 {
10853 r_type_mask = 0xffffffff;
10854 r_sym_shift = 32;
10855 address_size = 8;
10856 }
10857
10858 /* Relocate the contents of each section. */
10859 sym_hashes = elf_sym_hashes (input_bfd);
10860 for (o = input_bfd->sections; o != NULL; o = o->next)
10861 {
10862 bfd_byte *contents;
10863
10864 if (! o->linker_mark)
10865 {
10866 /* This section was omitted from the link. */
10867 continue;
10868 }
10869
10870 if (!flinfo->info->resolve_section_groups
10871 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10872 {
10873 /* Deal with the group signature symbol. */
10874 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10875 unsigned long symndx = sec_data->this_hdr.sh_info;
10876 asection *osec = o->output_section;
10877
10878 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10879 if (symndx >= locsymcount
10880 || (elf_bad_symtab (input_bfd)
10881 && flinfo->sections[symndx] == NULL))
10882 {
10883 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10884 while (h->root.type == bfd_link_hash_indirect
10885 || h->root.type == bfd_link_hash_warning)
10886 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10887 /* Arrange for symbol to be output. */
10888 h->indx = -2;
10889 elf_section_data (osec)->this_hdr.sh_info = -2;
10890 }
10891 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10892 {
10893 /* We'll use the output section target_index. */
10894 asection *sec = flinfo->sections[symndx]->output_section;
10895 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10896 }
10897 else
10898 {
10899 if (flinfo->indices[symndx] == -1)
10900 {
10901 /* Otherwise output the local symbol now. */
10902 Elf_Internal_Sym sym = isymbuf[symndx];
10903 asection *sec = flinfo->sections[symndx]->output_section;
10904 const char *name;
10905 long indx;
10906 int ret;
10907
10908 name = bfd_elf_string_from_elf_section (input_bfd,
10909 symtab_hdr->sh_link,
10910 sym.st_name);
10911 if (name == NULL)
10912 return FALSE;
10913
10914 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10915 sec);
10916 if (sym.st_shndx == SHN_BAD)
10917 return FALSE;
10918
10919 sym.st_value += o->output_offset;
10920
10921 indx = bfd_get_symcount (output_bfd);
10922 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10923 NULL);
10924 if (ret == 0)
10925 return FALSE;
10926 else if (ret == 1)
10927 flinfo->indices[symndx] = indx;
10928 else
10929 abort ();
10930 }
10931 elf_section_data (osec)->this_hdr.sh_info
10932 = flinfo->indices[symndx];
10933 }
10934 }
10935
10936 if ((o->flags & SEC_HAS_CONTENTS) == 0
10937 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10938 continue;
10939
10940 if ((o->flags & SEC_LINKER_CREATED) != 0)
10941 {
10942 /* Section was created by _bfd_elf_link_create_dynamic_sections
10943 or somesuch. */
10944 continue;
10945 }
10946
10947 /* Get the contents of the section. They have been cached by a
10948 relaxation routine. Note that o is a section in an input
10949 file, so the contents field will not have been set by any of
10950 the routines which work on output files. */
10951 if (elf_section_data (o)->this_hdr.contents != NULL)
10952 {
10953 contents = elf_section_data (o)->this_hdr.contents;
10954 if (bed->caches_rawsize
10955 && o->rawsize != 0
10956 && o->rawsize < o->size)
10957 {
10958 memcpy (flinfo->contents, contents, o->rawsize);
10959 contents = flinfo->contents;
10960 }
10961 }
10962 else
10963 {
10964 contents = flinfo->contents;
10965 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10966 return FALSE;
10967 }
10968
10969 if ((o->flags & SEC_RELOC) != 0)
10970 {
10971 Elf_Internal_Rela *internal_relocs;
10972 Elf_Internal_Rela *rel, *relend;
10973 int action_discarded;
10974 int ret;
10975
10976 /* Get the swapped relocs. */
10977 internal_relocs
10978 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10979 flinfo->internal_relocs, FALSE);
10980 if (internal_relocs == NULL
10981 && o->reloc_count > 0)
10982 return FALSE;
10983
10984 /* We need to reverse-copy input .ctors/.dtors sections if
10985 they are placed in .init_array/.finit_array for output. */
10986 if (o->size > address_size
10987 && ((strncmp (o->name, ".ctors", 6) == 0
10988 && strcmp (o->output_section->name,
10989 ".init_array") == 0)
10990 || (strncmp (o->name, ".dtors", 6) == 0
10991 && strcmp (o->output_section->name,
10992 ".fini_array") == 0))
10993 && (o->name[6] == 0 || o->name[6] == '.'))
10994 {
10995 if (o->size * bed->s->int_rels_per_ext_rel
10996 != o->reloc_count * address_size)
10997 {
10998 _bfd_error_handler
10999 /* xgettext:c-format */
11000 (_("error: %pB: size of section %pA is not "
11001 "multiple of address size"),
11002 input_bfd, o);
11003 bfd_set_error (bfd_error_bad_value);
11004 return FALSE;
11005 }
11006 o->flags |= SEC_ELF_REVERSE_COPY;
11007 }
11008
11009 action_discarded = -1;
11010 if (!elf_section_ignore_discarded_relocs (o))
11011 action_discarded = (*bed->action_discarded) (o);
11012
11013 /* Run through the relocs evaluating complex reloc symbols and
11014 looking for relocs against symbols from discarded sections
11015 or section symbols from removed link-once sections.
11016 Complain about relocs against discarded sections. Zero
11017 relocs against removed link-once sections. */
11018
11019 rel = internal_relocs;
11020 relend = rel + o->reloc_count;
11021 for ( ; rel < relend; rel++)
11022 {
11023 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11024 unsigned int s_type;
11025 asection **ps, *sec;
11026 struct elf_link_hash_entry *h = NULL;
11027 const char *sym_name;
11028
11029 if (r_symndx == STN_UNDEF)
11030 continue;
11031
11032 if (r_symndx >= locsymcount
11033 || (elf_bad_symtab (input_bfd)
11034 && flinfo->sections[r_symndx] == NULL))
11035 {
11036 h = sym_hashes[r_symndx - extsymoff];
11037
11038 /* Badly formatted input files can contain relocs that
11039 reference non-existant symbols. Check here so that
11040 we do not seg fault. */
11041 if (h == NULL)
11042 {
11043 _bfd_error_handler
11044 /* xgettext:c-format */
11045 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11046 "that references a non-existent global symbol"),
11047 input_bfd, (uint64_t) rel->r_info, o);
11048 bfd_set_error (bfd_error_bad_value);
11049 return FALSE;
11050 }
11051
11052 while (h->root.type == bfd_link_hash_indirect
11053 || h->root.type == bfd_link_hash_warning)
11054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11055
11056 s_type = h->type;
11057
11058 /* If a plugin symbol is referenced from a non-IR file,
11059 mark the symbol as undefined. Note that the
11060 linker may attach linker created dynamic sections
11061 to the plugin bfd. Symbols defined in linker
11062 created sections are not plugin symbols. */
11063 if ((h->root.non_ir_ref_regular
11064 || h->root.non_ir_ref_dynamic)
11065 && (h->root.type == bfd_link_hash_defined
11066 || h->root.type == bfd_link_hash_defweak)
11067 && (h->root.u.def.section->flags
11068 & SEC_LINKER_CREATED) == 0
11069 && h->root.u.def.section->owner != NULL
11070 && (h->root.u.def.section->owner->flags
11071 & BFD_PLUGIN) != 0)
11072 {
11073 h->root.type = bfd_link_hash_undefined;
11074 h->root.u.undef.abfd = h->root.u.def.section->owner;
11075 }
11076
11077 ps = NULL;
11078 if (h->root.type == bfd_link_hash_defined
11079 || h->root.type == bfd_link_hash_defweak)
11080 ps = &h->root.u.def.section;
11081
11082 sym_name = h->root.root.string;
11083 }
11084 else
11085 {
11086 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11087
11088 s_type = ELF_ST_TYPE (sym->st_info);
11089 ps = &flinfo->sections[r_symndx];
11090 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11091 sym, *ps);
11092 }
11093
11094 if ((s_type == STT_RELC || s_type == STT_SRELC)
11095 && !bfd_link_relocatable (flinfo->info))
11096 {
11097 bfd_vma val;
11098 bfd_vma dot = (rel->r_offset
11099 + o->output_offset + o->output_section->vma);
11100 #ifdef DEBUG
11101 printf ("Encountered a complex symbol!");
11102 printf (" (input_bfd %s, section %s, reloc %ld\n",
11103 bfd_get_filename (input_bfd), o->name,
11104 (long) (rel - internal_relocs));
11105 printf (" symbol: idx %8.8lx, name %s\n",
11106 r_symndx, sym_name);
11107 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11108 (unsigned long) rel->r_info,
11109 (unsigned long) rel->r_offset);
11110 #endif
11111 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11112 isymbuf, locsymcount, s_type == STT_SRELC))
11113 return FALSE;
11114
11115 /* Symbol evaluated OK. Update to absolute value. */
11116 set_symbol_value (input_bfd, isymbuf, locsymcount,
11117 r_symndx, val);
11118 continue;
11119 }
11120
11121 if (action_discarded != -1 && ps != NULL)
11122 {
11123 /* Complain if the definition comes from a
11124 discarded section. */
11125 if ((sec = *ps) != NULL && discarded_section (sec))
11126 {
11127 BFD_ASSERT (r_symndx != STN_UNDEF);
11128 if (action_discarded & COMPLAIN)
11129 (*flinfo->info->callbacks->einfo)
11130 /* xgettext:c-format */
11131 (_("%X`%s' referenced in section `%pA' of %pB: "
11132 "defined in discarded section `%pA' of %pB\n"),
11133 sym_name, o, input_bfd, sec, sec->owner);
11134
11135 /* Try to do the best we can to support buggy old
11136 versions of gcc. Pretend that the symbol is
11137 really defined in the kept linkonce section.
11138 FIXME: This is quite broken. Modifying the
11139 symbol here means we will be changing all later
11140 uses of the symbol, not just in this section. */
11141 if (action_discarded & PRETEND)
11142 {
11143 asection *kept;
11144
11145 kept = _bfd_elf_check_kept_section (sec,
11146 flinfo->info);
11147 if (kept != NULL)
11148 {
11149 *ps = kept;
11150 continue;
11151 }
11152 }
11153 }
11154 }
11155 }
11156
11157 /* Relocate the section by invoking a back end routine.
11158
11159 The back end routine is responsible for adjusting the
11160 section contents as necessary, and (if using Rela relocs
11161 and generating a relocatable output file) adjusting the
11162 reloc addend as necessary.
11163
11164 The back end routine does not have to worry about setting
11165 the reloc address or the reloc symbol index.
11166
11167 The back end routine is given a pointer to the swapped in
11168 internal symbols, and can access the hash table entries
11169 for the external symbols via elf_sym_hashes (input_bfd).
11170
11171 When generating relocatable output, the back end routine
11172 must handle STB_LOCAL/STT_SECTION symbols specially. The
11173 output symbol is going to be a section symbol
11174 corresponding to the output section, which will require
11175 the addend to be adjusted. */
11176
11177 ret = (*relocate_section) (output_bfd, flinfo->info,
11178 input_bfd, o, contents,
11179 internal_relocs,
11180 isymbuf,
11181 flinfo->sections);
11182 if (!ret)
11183 return FALSE;
11184
11185 if (ret == 2
11186 || bfd_link_relocatable (flinfo->info)
11187 || flinfo->info->emitrelocations)
11188 {
11189 Elf_Internal_Rela *irela;
11190 Elf_Internal_Rela *irelaend, *irelamid;
11191 bfd_vma last_offset;
11192 struct elf_link_hash_entry **rel_hash;
11193 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11194 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11195 unsigned int next_erel;
11196 bfd_boolean rela_normal;
11197 struct bfd_elf_section_data *esdi, *esdo;
11198
11199 esdi = elf_section_data (o);
11200 esdo = elf_section_data (o->output_section);
11201 rela_normal = FALSE;
11202
11203 /* Adjust the reloc addresses and symbol indices. */
11204
11205 irela = internal_relocs;
11206 irelaend = irela + o->reloc_count;
11207 rel_hash = esdo->rel.hashes + esdo->rel.count;
11208 /* We start processing the REL relocs, if any. When we reach
11209 IRELAMID in the loop, we switch to the RELA relocs. */
11210 irelamid = irela;
11211 if (esdi->rel.hdr != NULL)
11212 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11213 * bed->s->int_rels_per_ext_rel);
11214 rel_hash_list = rel_hash;
11215 rela_hash_list = NULL;
11216 last_offset = o->output_offset;
11217 if (!bfd_link_relocatable (flinfo->info))
11218 last_offset += o->output_section->vma;
11219 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11220 {
11221 unsigned long r_symndx;
11222 asection *sec;
11223 Elf_Internal_Sym sym;
11224
11225 if (next_erel == bed->s->int_rels_per_ext_rel)
11226 {
11227 rel_hash++;
11228 next_erel = 0;
11229 }
11230
11231 if (irela == irelamid)
11232 {
11233 rel_hash = esdo->rela.hashes + esdo->rela.count;
11234 rela_hash_list = rel_hash;
11235 rela_normal = bed->rela_normal;
11236 }
11237
11238 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11239 flinfo->info, o,
11240 irela->r_offset);
11241 if (irela->r_offset >= (bfd_vma) -2)
11242 {
11243 /* This is a reloc for a deleted entry or somesuch.
11244 Turn it into an R_*_NONE reloc, at the same
11245 offset as the last reloc. elf_eh_frame.c and
11246 bfd_elf_discard_info rely on reloc offsets
11247 being ordered. */
11248 irela->r_offset = last_offset;
11249 irela->r_info = 0;
11250 irela->r_addend = 0;
11251 continue;
11252 }
11253
11254 irela->r_offset += o->output_offset;
11255
11256 /* Relocs in an executable have to be virtual addresses. */
11257 if (!bfd_link_relocatable (flinfo->info))
11258 irela->r_offset += o->output_section->vma;
11259
11260 last_offset = irela->r_offset;
11261
11262 r_symndx = irela->r_info >> r_sym_shift;
11263 if (r_symndx == STN_UNDEF)
11264 continue;
11265
11266 if (r_symndx >= locsymcount
11267 || (elf_bad_symtab (input_bfd)
11268 && flinfo->sections[r_symndx] == NULL))
11269 {
11270 struct elf_link_hash_entry *rh;
11271 unsigned long indx;
11272
11273 /* This is a reloc against a global symbol. We
11274 have not yet output all the local symbols, so
11275 we do not know the symbol index of any global
11276 symbol. We set the rel_hash entry for this
11277 reloc to point to the global hash table entry
11278 for this symbol. The symbol index is then
11279 set at the end of bfd_elf_final_link. */
11280 indx = r_symndx - extsymoff;
11281 rh = elf_sym_hashes (input_bfd)[indx];
11282 while (rh->root.type == bfd_link_hash_indirect
11283 || rh->root.type == bfd_link_hash_warning)
11284 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11285
11286 /* Setting the index to -2 tells
11287 elf_link_output_extsym that this symbol is
11288 used by a reloc. */
11289 BFD_ASSERT (rh->indx < 0);
11290 rh->indx = -2;
11291 *rel_hash = rh;
11292
11293 continue;
11294 }
11295
11296 /* This is a reloc against a local symbol. */
11297
11298 *rel_hash = NULL;
11299 sym = isymbuf[r_symndx];
11300 sec = flinfo->sections[r_symndx];
11301 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11302 {
11303 /* I suppose the backend ought to fill in the
11304 section of any STT_SECTION symbol against a
11305 processor specific section. */
11306 r_symndx = STN_UNDEF;
11307 if (bfd_is_abs_section (sec))
11308 ;
11309 else if (sec == NULL || sec->owner == NULL)
11310 {
11311 bfd_set_error (bfd_error_bad_value);
11312 return FALSE;
11313 }
11314 else
11315 {
11316 asection *osec = sec->output_section;
11317
11318 /* If we have discarded a section, the output
11319 section will be the absolute section. In
11320 case of discarded SEC_MERGE sections, use
11321 the kept section. relocate_section should
11322 have already handled discarded linkonce
11323 sections. */
11324 if (bfd_is_abs_section (osec)
11325 && sec->kept_section != NULL
11326 && sec->kept_section->output_section != NULL)
11327 {
11328 osec = sec->kept_section->output_section;
11329 irela->r_addend -= osec->vma;
11330 }
11331
11332 if (!bfd_is_abs_section (osec))
11333 {
11334 r_symndx = osec->target_index;
11335 if (r_symndx == STN_UNDEF)
11336 {
11337 irela->r_addend += osec->vma;
11338 osec = _bfd_nearby_section (output_bfd, osec,
11339 osec->vma);
11340 irela->r_addend -= osec->vma;
11341 r_symndx = osec->target_index;
11342 }
11343 }
11344 }
11345
11346 /* Adjust the addend according to where the
11347 section winds up in the output section. */
11348 if (rela_normal)
11349 irela->r_addend += sec->output_offset;
11350 }
11351 else
11352 {
11353 if (flinfo->indices[r_symndx] == -1)
11354 {
11355 unsigned long shlink;
11356 const char *name;
11357 asection *osec;
11358 long indx;
11359
11360 if (flinfo->info->strip == strip_all)
11361 {
11362 /* You can't do ld -r -s. */
11363 bfd_set_error (bfd_error_invalid_operation);
11364 return FALSE;
11365 }
11366
11367 /* This symbol was skipped earlier, but
11368 since it is needed by a reloc, we
11369 must output it now. */
11370 shlink = symtab_hdr->sh_link;
11371 name = (bfd_elf_string_from_elf_section
11372 (input_bfd, shlink, sym.st_name));
11373 if (name == NULL)
11374 return FALSE;
11375
11376 osec = sec->output_section;
11377 sym.st_shndx =
11378 _bfd_elf_section_from_bfd_section (output_bfd,
11379 osec);
11380 if (sym.st_shndx == SHN_BAD)
11381 return FALSE;
11382
11383 sym.st_value += sec->output_offset;
11384 if (!bfd_link_relocatable (flinfo->info))
11385 {
11386 sym.st_value += osec->vma;
11387 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11388 {
11389 struct elf_link_hash_table *htab
11390 = elf_hash_table (flinfo->info);
11391
11392 /* STT_TLS symbols are relative to PT_TLS
11393 segment base. */
11394 if (htab->tls_sec != NULL)
11395 sym.st_value -= htab->tls_sec->vma;
11396 else
11397 sym.st_info
11398 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11399 STT_NOTYPE);
11400 }
11401 }
11402
11403 indx = bfd_get_symcount (output_bfd);
11404 ret = elf_link_output_symstrtab (flinfo, name,
11405 &sym, sec,
11406 NULL);
11407 if (ret == 0)
11408 return FALSE;
11409 else if (ret == 1)
11410 flinfo->indices[r_symndx] = indx;
11411 else
11412 abort ();
11413 }
11414
11415 r_symndx = flinfo->indices[r_symndx];
11416 }
11417
11418 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11419 | (irela->r_info & r_type_mask));
11420 }
11421
11422 /* Swap out the relocs. */
11423 input_rel_hdr = esdi->rel.hdr;
11424 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11425 {
11426 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11427 input_rel_hdr,
11428 internal_relocs,
11429 rel_hash_list))
11430 return FALSE;
11431 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11432 * bed->s->int_rels_per_ext_rel);
11433 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11434 }
11435
11436 input_rela_hdr = esdi->rela.hdr;
11437 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11438 {
11439 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11440 input_rela_hdr,
11441 internal_relocs,
11442 rela_hash_list))
11443 return FALSE;
11444 }
11445 }
11446 }
11447
11448 /* Write out the modified section contents. */
11449 if (bed->elf_backend_write_section
11450 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11451 contents))
11452 {
11453 /* Section written out. */
11454 }
11455 else switch (o->sec_info_type)
11456 {
11457 case SEC_INFO_TYPE_STABS:
11458 if (! (_bfd_write_section_stabs
11459 (output_bfd,
11460 &elf_hash_table (flinfo->info)->stab_info,
11461 o, &elf_section_data (o)->sec_info, contents)))
11462 return FALSE;
11463 break;
11464 case SEC_INFO_TYPE_MERGE:
11465 if (! _bfd_write_merged_section (output_bfd, o,
11466 elf_section_data (o)->sec_info))
11467 return FALSE;
11468 break;
11469 case SEC_INFO_TYPE_EH_FRAME:
11470 {
11471 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11472 o, contents))
11473 return FALSE;
11474 }
11475 break;
11476 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11477 {
11478 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11479 flinfo->info,
11480 o, contents))
11481 return FALSE;
11482 }
11483 break;
11484 default:
11485 {
11486 if (! (o->flags & SEC_EXCLUDE))
11487 {
11488 file_ptr offset = (file_ptr) o->output_offset;
11489 bfd_size_type todo = o->size;
11490
11491 offset *= bfd_octets_per_byte (output_bfd, o);
11492
11493 if ((o->flags & SEC_ELF_REVERSE_COPY))
11494 {
11495 /* Reverse-copy input section to output. */
11496 do
11497 {
11498 todo -= address_size;
11499 if (! bfd_set_section_contents (output_bfd,
11500 o->output_section,
11501 contents + todo,
11502 offset,
11503 address_size))
11504 return FALSE;
11505 if (todo == 0)
11506 break;
11507 offset += address_size;
11508 }
11509 while (1);
11510 }
11511 else if (! bfd_set_section_contents (output_bfd,
11512 o->output_section,
11513 contents,
11514 offset, todo))
11515 return FALSE;
11516 }
11517 }
11518 break;
11519 }
11520 }
11521
11522 return TRUE;
11523 }
11524
11525 /* Generate a reloc when linking an ELF file. This is a reloc
11526 requested by the linker, and does not come from any input file. This
11527 is used to build constructor and destructor tables when linking
11528 with -Ur. */
11529
11530 static bfd_boolean
11531 elf_reloc_link_order (bfd *output_bfd,
11532 struct bfd_link_info *info,
11533 asection *output_section,
11534 struct bfd_link_order *link_order)
11535 {
11536 reloc_howto_type *howto;
11537 long indx;
11538 bfd_vma offset;
11539 bfd_vma addend;
11540 struct bfd_elf_section_reloc_data *reldata;
11541 struct elf_link_hash_entry **rel_hash_ptr;
11542 Elf_Internal_Shdr *rel_hdr;
11543 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11544 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11545 bfd_byte *erel;
11546 unsigned int i;
11547 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11548
11549 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11550 if (howto == NULL)
11551 {
11552 bfd_set_error (bfd_error_bad_value);
11553 return FALSE;
11554 }
11555
11556 addend = link_order->u.reloc.p->addend;
11557
11558 if (esdo->rel.hdr)
11559 reldata = &esdo->rel;
11560 else if (esdo->rela.hdr)
11561 reldata = &esdo->rela;
11562 else
11563 {
11564 reldata = NULL;
11565 BFD_ASSERT (0);
11566 }
11567
11568 /* Figure out the symbol index. */
11569 rel_hash_ptr = reldata->hashes + reldata->count;
11570 if (link_order->type == bfd_section_reloc_link_order)
11571 {
11572 indx = link_order->u.reloc.p->u.section->target_index;
11573 BFD_ASSERT (indx != 0);
11574 *rel_hash_ptr = NULL;
11575 }
11576 else
11577 {
11578 struct elf_link_hash_entry *h;
11579
11580 /* Treat a reloc against a defined symbol as though it were
11581 actually against the section. */
11582 h = ((struct elf_link_hash_entry *)
11583 bfd_wrapped_link_hash_lookup (output_bfd, info,
11584 link_order->u.reloc.p->u.name,
11585 FALSE, FALSE, TRUE));
11586 if (h != NULL
11587 && (h->root.type == bfd_link_hash_defined
11588 || h->root.type == bfd_link_hash_defweak))
11589 {
11590 asection *section;
11591
11592 section = h->root.u.def.section;
11593 indx = section->output_section->target_index;
11594 *rel_hash_ptr = NULL;
11595 /* It seems that we ought to add the symbol value to the
11596 addend here, but in practice it has already been added
11597 because it was passed to constructor_callback. */
11598 addend += section->output_section->vma + section->output_offset;
11599 }
11600 else if (h != NULL)
11601 {
11602 /* Setting the index to -2 tells elf_link_output_extsym that
11603 this symbol is used by a reloc. */
11604 h->indx = -2;
11605 *rel_hash_ptr = h;
11606 indx = 0;
11607 }
11608 else
11609 {
11610 (*info->callbacks->unattached_reloc)
11611 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11612 indx = 0;
11613 }
11614 }
11615
11616 /* If this is an inplace reloc, we must write the addend into the
11617 object file. */
11618 if (howto->partial_inplace && addend != 0)
11619 {
11620 bfd_size_type size;
11621 bfd_reloc_status_type rstat;
11622 bfd_byte *buf;
11623 bfd_boolean ok;
11624 const char *sym_name;
11625 bfd_size_type octets;
11626
11627 size = (bfd_size_type) bfd_get_reloc_size (howto);
11628 buf = (bfd_byte *) bfd_zmalloc (size);
11629 if (buf == NULL && size != 0)
11630 return FALSE;
11631 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11632 switch (rstat)
11633 {
11634 case bfd_reloc_ok:
11635 break;
11636
11637 default:
11638 case bfd_reloc_outofrange:
11639 abort ();
11640
11641 case bfd_reloc_overflow:
11642 if (link_order->type == bfd_section_reloc_link_order)
11643 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11644 else
11645 sym_name = link_order->u.reloc.p->u.name;
11646 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11647 howto->name, addend, NULL, NULL,
11648 (bfd_vma) 0);
11649 break;
11650 }
11651
11652 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11653 output_section);
11654 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11655 octets, size);
11656 free (buf);
11657 if (! ok)
11658 return FALSE;
11659 }
11660
11661 /* The address of a reloc is relative to the section in a
11662 relocatable file, and is a virtual address in an executable
11663 file. */
11664 offset = link_order->offset;
11665 if (! bfd_link_relocatable (info))
11666 offset += output_section->vma;
11667
11668 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11669 {
11670 irel[i].r_offset = offset;
11671 irel[i].r_info = 0;
11672 irel[i].r_addend = 0;
11673 }
11674 if (bed->s->arch_size == 32)
11675 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11676 else
11677 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11678
11679 rel_hdr = reldata->hdr;
11680 erel = rel_hdr->contents;
11681 if (rel_hdr->sh_type == SHT_REL)
11682 {
11683 erel += reldata->count * bed->s->sizeof_rel;
11684 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11685 }
11686 else
11687 {
11688 irel[0].r_addend = addend;
11689 erel += reldata->count * bed->s->sizeof_rela;
11690 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11691 }
11692
11693 ++reldata->count;
11694
11695 return TRUE;
11696 }
11697
11698
11699 /* Compare two sections based on the locations of the sections they are
11700 linked to. Used by elf_fixup_link_order. */
11701
11702 static int
11703 compare_link_order (const void *a, const void *b)
11704 {
11705 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11706 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11707 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11708 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11709 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11710 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11711
11712 if (apos < bpos)
11713 return -1;
11714 if (apos > bpos)
11715 return 1;
11716
11717 /* The only way we should get matching LMAs is when the first of two
11718 sections has zero size. */
11719 if (asec->size < bsec->size)
11720 return -1;
11721 if (asec->size > bsec->size)
11722 return 1;
11723
11724 /* If they are both zero size then they almost certainly have the same
11725 VMA and thus are not ordered with respect to each other. Test VMA
11726 anyway, and fall back to id to make the result reproducible across
11727 qsort implementations. */
11728 apos = asec->output_section->vma + asec->output_offset;
11729 bpos = bsec->output_section->vma + bsec->output_offset;
11730 if (apos < bpos)
11731 return -1;
11732 if (apos > bpos)
11733 return 1;
11734
11735 return asec->id - bsec->id;
11736 }
11737
11738
11739 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11740 order as their linked sections. Returns false if this could not be done
11741 because an output section includes both ordered and unordered
11742 sections. Ideally we'd do this in the linker proper. */
11743
11744 static bfd_boolean
11745 elf_fixup_link_order (bfd *abfd, asection *o)
11746 {
11747 size_t seen_linkorder;
11748 size_t seen_other;
11749 size_t n;
11750 struct bfd_link_order *p;
11751 bfd *sub;
11752 struct bfd_link_order **sections;
11753 asection *other_sec, *linkorder_sec;
11754 bfd_vma offset; /* Octets. */
11755
11756 other_sec = NULL;
11757 linkorder_sec = NULL;
11758 seen_other = 0;
11759 seen_linkorder = 0;
11760 for (p = o->map_head.link_order; p != NULL; p = p->next)
11761 {
11762 if (p->type == bfd_indirect_link_order)
11763 {
11764 asection *s = p->u.indirect.section;
11765 sub = s->owner;
11766 if ((s->flags & SEC_LINKER_CREATED) == 0
11767 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11768 && elf_section_data (s) != NULL
11769 && elf_linked_to_section (s) != NULL)
11770 {
11771 seen_linkorder++;
11772 linkorder_sec = s;
11773 }
11774 else
11775 {
11776 seen_other++;
11777 other_sec = s;
11778 }
11779 }
11780 else
11781 seen_other++;
11782
11783 if (seen_other && seen_linkorder)
11784 {
11785 if (other_sec && linkorder_sec)
11786 _bfd_error_handler
11787 /* xgettext:c-format */
11788 (_("%pA has both ordered [`%pA' in %pB] "
11789 "and unordered [`%pA' in %pB] sections"),
11790 o, linkorder_sec, linkorder_sec->owner,
11791 other_sec, other_sec->owner);
11792 else
11793 _bfd_error_handler
11794 (_("%pA has both ordered and unordered sections"), o);
11795 bfd_set_error (bfd_error_bad_value);
11796 return FALSE;
11797 }
11798 }
11799
11800 if (!seen_linkorder)
11801 return TRUE;
11802
11803 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11804 if (sections == NULL)
11805 return FALSE;
11806
11807 seen_linkorder = 0;
11808 for (p = o->map_head.link_order; p != NULL; p = p->next)
11809 sections[seen_linkorder++] = p;
11810
11811 /* Sort the input sections in the order of their linked section. */
11812 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11813
11814 /* Change the offsets of the sections. */
11815 offset = 0;
11816 for (n = 0; n < seen_linkorder; n++)
11817 {
11818 bfd_vma mask;
11819 asection *s = sections[n]->u.indirect.section;
11820 unsigned int opb = bfd_octets_per_byte (abfd, s);
11821
11822 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11823 offset = (offset + ~mask) & mask;
11824 sections[n]->offset = s->output_offset = offset / opb;
11825 offset += sections[n]->size;
11826 }
11827
11828 free (sections);
11829 return TRUE;
11830 }
11831
11832 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11833 Returns TRUE upon success, FALSE otherwise. */
11834
11835 static bfd_boolean
11836 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11837 {
11838 bfd_boolean ret = FALSE;
11839 bfd *implib_bfd;
11840 const struct elf_backend_data *bed;
11841 flagword flags;
11842 enum bfd_architecture arch;
11843 unsigned int mach;
11844 asymbol **sympp = NULL;
11845 long symsize;
11846 long symcount;
11847 long src_count;
11848 elf_symbol_type *osymbuf;
11849 size_t amt;
11850
11851 implib_bfd = info->out_implib_bfd;
11852 bed = get_elf_backend_data (abfd);
11853
11854 if (!bfd_set_format (implib_bfd, bfd_object))
11855 return FALSE;
11856
11857 /* Use flag from executable but make it a relocatable object. */
11858 flags = bfd_get_file_flags (abfd);
11859 flags &= ~HAS_RELOC;
11860 if (!bfd_set_start_address (implib_bfd, 0)
11861 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11862 return FALSE;
11863
11864 /* Copy architecture of output file to import library file. */
11865 arch = bfd_get_arch (abfd);
11866 mach = bfd_get_mach (abfd);
11867 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11868 && (abfd->target_defaulted
11869 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11870 return FALSE;
11871
11872 /* Get symbol table size. */
11873 symsize = bfd_get_symtab_upper_bound (abfd);
11874 if (symsize < 0)
11875 return FALSE;
11876
11877 /* Read in the symbol table. */
11878 sympp = (asymbol **) bfd_malloc (symsize);
11879 if (sympp == NULL)
11880 return FALSE;
11881
11882 symcount = bfd_canonicalize_symtab (abfd, sympp);
11883 if (symcount < 0)
11884 goto free_sym_buf;
11885
11886 /* Allow the BFD backend to copy any private header data it
11887 understands from the output BFD to the import library BFD. */
11888 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11889 goto free_sym_buf;
11890
11891 /* Filter symbols to appear in the import library. */
11892 if (bed->elf_backend_filter_implib_symbols)
11893 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11894 symcount);
11895 else
11896 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11897 if (symcount == 0)
11898 {
11899 bfd_set_error (bfd_error_no_symbols);
11900 _bfd_error_handler (_("%pB: no symbol found for import library"),
11901 implib_bfd);
11902 goto free_sym_buf;
11903 }
11904
11905
11906 /* Make symbols absolute. */
11907 amt = symcount * sizeof (*osymbuf);
11908 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11909 if (osymbuf == NULL)
11910 goto free_sym_buf;
11911
11912 for (src_count = 0; src_count < symcount; src_count++)
11913 {
11914 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11915 sizeof (*osymbuf));
11916 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11917 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11918 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11919 osymbuf[src_count].internal_elf_sym.st_value =
11920 osymbuf[src_count].symbol.value;
11921 sympp[src_count] = &osymbuf[src_count].symbol;
11922 }
11923
11924 bfd_set_symtab (implib_bfd, sympp, symcount);
11925
11926 /* Allow the BFD backend to copy any private data it understands
11927 from the output BFD to the import library BFD. This is done last
11928 to permit the routine to look at the filtered symbol table. */
11929 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11930 goto free_sym_buf;
11931
11932 if (!bfd_close (implib_bfd))
11933 goto free_sym_buf;
11934
11935 ret = TRUE;
11936
11937 free_sym_buf:
11938 free (sympp);
11939 return ret;
11940 }
11941
11942 static void
11943 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11944 {
11945 asection *o;
11946
11947 if (flinfo->symstrtab != NULL)
11948 _bfd_elf_strtab_free (flinfo->symstrtab);
11949 free (flinfo->contents);
11950 free (flinfo->external_relocs);
11951 free (flinfo->internal_relocs);
11952 free (flinfo->external_syms);
11953 free (flinfo->locsym_shndx);
11954 free (flinfo->internal_syms);
11955 free (flinfo->indices);
11956 free (flinfo->sections);
11957 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11958 free (flinfo->symshndxbuf);
11959 for (o = obfd->sections; o != NULL; o = o->next)
11960 {
11961 struct bfd_elf_section_data *esdo = elf_section_data (o);
11962 free (esdo->rel.hashes);
11963 free (esdo->rela.hashes);
11964 }
11965 }
11966
11967 /* Do the final step of an ELF link. */
11968
11969 bfd_boolean
11970 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11971 {
11972 bfd_boolean dynamic;
11973 bfd_boolean emit_relocs;
11974 bfd *dynobj;
11975 struct elf_final_link_info flinfo;
11976 asection *o;
11977 struct bfd_link_order *p;
11978 bfd *sub;
11979 bfd_size_type max_contents_size;
11980 bfd_size_type max_external_reloc_size;
11981 bfd_size_type max_internal_reloc_count;
11982 bfd_size_type max_sym_count;
11983 bfd_size_type max_sym_shndx_count;
11984 Elf_Internal_Sym elfsym;
11985 unsigned int i;
11986 Elf_Internal_Shdr *symtab_hdr;
11987 Elf_Internal_Shdr *symtab_shndx_hdr;
11988 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11989 struct elf_outext_info eoinfo;
11990 bfd_boolean merged;
11991 size_t relativecount = 0;
11992 asection *reldyn = 0;
11993 bfd_size_type amt;
11994 asection *attr_section = NULL;
11995 bfd_vma attr_size = 0;
11996 const char *std_attrs_section;
11997 struct elf_link_hash_table *htab = elf_hash_table (info);
11998 bfd_boolean sections_removed;
11999
12000 if (!is_elf_hash_table (htab))
12001 return FALSE;
12002
12003 if (bfd_link_pic (info))
12004 abfd->flags |= DYNAMIC;
12005
12006 dynamic = htab->dynamic_sections_created;
12007 dynobj = htab->dynobj;
12008
12009 emit_relocs = (bfd_link_relocatable (info)
12010 || info->emitrelocations);
12011
12012 flinfo.info = info;
12013 flinfo.output_bfd = abfd;
12014 flinfo.symstrtab = _bfd_elf_strtab_init ();
12015 if (flinfo.symstrtab == NULL)
12016 return FALSE;
12017
12018 if (! dynamic)
12019 {
12020 flinfo.hash_sec = NULL;
12021 flinfo.symver_sec = NULL;
12022 }
12023 else
12024 {
12025 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12026 /* Note that dynsym_sec can be NULL (on VMS). */
12027 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12028 /* Note that it is OK if symver_sec is NULL. */
12029 }
12030
12031 flinfo.contents = NULL;
12032 flinfo.external_relocs = NULL;
12033 flinfo.internal_relocs = NULL;
12034 flinfo.external_syms = NULL;
12035 flinfo.locsym_shndx = NULL;
12036 flinfo.internal_syms = NULL;
12037 flinfo.indices = NULL;
12038 flinfo.sections = NULL;
12039 flinfo.symshndxbuf = NULL;
12040 flinfo.filesym_count = 0;
12041
12042 /* The object attributes have been merged. Remove the input
12043 sections from the link, and set the contents of the output
12044 section. */
12045 sections_removed = FALSE;
12046 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12047 for (o = abfd->sections; o != NULL; o = o->next)
12048 {
12049 bfd_boolean remove_section = FALSE;
12050
12051 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12052 || strcmp (o->name, ".gnu.attributes") == 0)
12053 {
12054 for (p = o->map_head.link_order; p != NULL; p = p->next)
12055 {
12056 asection *input_section;
12057
12058 if (p->type != bfd_indirect_link_order)
12059 continue;
12060 input_section = p->u.indirect.section;
12061 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12062 elf_link_input_bfd ignores this section. */
12063 input_section->flags &= ~SEC_HAS_CONTENTS;
12064 }
12065
12066 attr_size = bfd_elf_obj_attr_size (abfd);
12067 bfd_set_section_size (o, attr_size);
12068 /* Skip this section later on. */
12069 o->map_head.link_order = NULL;
12070 if (attr_size)
12071 attr_section = o;
12072 else
12073 remove_section = TRUE;
12074 }
12075 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12076 {
12077 /* Remove empty group section from linker output. */
12078 remove_section = TRUE;
12079 }
12080 if (remove_section)
12081 {
12082 o->flags |= SEC_EXCLUDE;
12083 bfd_section_list_remove (abfd, o);
12084 abfd->section_count--;
12085 sections_removed = TRUE;
12086 }
12087 }
12088 if (sections_removed)
12089 _bfd_fix_excluded_sec_syms (abfd, info);
12090
12091 /* Count up the number of relocations we will output for each output
12092 section, so that we know the sizes of the reloc sections. We
12093 also figure out some maximum sizes. */
12094 max_contents_size = 0;
12095 max_external_reloc_size = 0;
12096 max_internal_reloc_count = 0;
12097 max_sym_count = 0;
12098 max_sym_shndx_count = 0;
12099 merged = FALSE;
12100 for (o = abfd->sections; o != NULL; o = o->next)
12101 {
12102 struct bfd_elf_section_data *esdo = elf_section_data (o);
12103 o->reloc_count = 0;
12104
12105 for (p = o->map_head.link_order; p != NULL; p = p->next)
12106 {
12107 unsigned int reloc_count = 0;
12108 unsigned int additional_reloc_count = 0;
12109 struct bfd_elf_section_data *esdi = NULL;
12110
12111 if (p->type == bfd_section_reloc_link_order
12112 || p->type == bfd_symbol_reloc_link_order)
12113 reloc_count = 1;
12114 else if (p->type == bfd_indirect_link_order)
12115 {
12116 asection *sec;
12117
12118 sec = p->u.indirect.section;
12119
12120 /* Mark all sections which are to be included in the
12121 link. This will normally be every section. We need
12122 to do this so that we can identify any sections which
12123 the linker has decided to not include. */
12124 sec->linker_mark = TRUE;
12125
12126 if (sec->flags & SEC_MERGE)
12127 merged = TRUE;
12128
12129 if (sec->rawsize > max_contents_size)
12130 max_contents_size = sec->rawsize;
12131 if (sec->size > max_contents_size)
12132 max_contents_size = sec->size;
12133
12134 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12135 && (sec->owner->flags & DYNAMIC) == 0)
12136 {
12137 size_t sym_count;
12138
12139 /* We are interested in just local symbols, not all
12140 symbols. */
12141 if (elf_bad_symtab (sec->owner))
12142 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12143 / bed->s->sizeof_sym);
12144 else
12145 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12146
12147 if (sym_count > max_sym_count)
12148 max_sym_count = sym_count;
12149
12150 if (sym_count > max_sym_shndx_count
12151 && elf_symtab_shndx_list (sec->owner) != NULL)
12152 max_sym_shndx_count = sym_count;
12153
12154 if (esdo->this_hdr.sh_type == SHT_REL
12155 || esdo->this_hdr.sh_type == SHT_RELA)
12156 /* Some backends use reloc_count in relocation sections
12157 to count particular types of relocs. Of course,
12158 reloc sections themselves can't have relocations. */
12159 ;
12160 else if (emit_relocs)
12161 {
12162 reloc_count = sec->reloc_count;
12163 if (bed->elf_backend_count_additional_relocs)
12164 {
12165 int c;
12166 c = (*bed->elf_backend_count_additional_relocs) (sec);
12167 additional_reloc_count += c;
12168 }
12169 }
12170 else if (bed->elf_backend_count_relocs)
12171 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12172
12173 esdi = elf_section_data (sec);
12174
12175 if ((sec->flags & SEC_RELOC) != 0)
12176 {
12177 size_t ext_size = 0;
12178
12179 if (esdi->rel.hdr != NULL)
12180 ext_size = esdi->rel.hdr->sh_size;
12181 if (esdi->rela.hdr != NULL)
12182 ext_size += esdi->rela.hdr->sh_size;
12183
12184 if (ext_size > max_external_reloc_size)
12185 max_external_reloc_size = ext_size;
12186 if (sec->reloc_count > max_internal_reloc_count)
12187 max_internal_reloc_count = sec->reloc_count;
12188 }
12189 }
12190 }
12191
12192 if (reloc_count == 0)
12193 continue;
12194
12195 reloc_count += additional_reloc_count;
12196 o->reloc_count += reloc_count;
12197
12198 if (p->type == bfd_indirect_link_order && emit_relocs)
12199 {
12200 if (esdi->rel.hdr)
12201 {
12202 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12203 esdo->rel.count += additional_reloc_count;
12204 }
12205 if (esdi->rela.hdr)
12206 {
12207 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12208 esdo->rela.count += additional_reloc_count;
12209 }
12210 }
12211 else
12212 {
12213 if (o->use_rela_p)
12214 esdo->rela.count += reloc_count;
12215 else
12216 esdo->rel.count += reloc_count;
12217 }
12218 }
12219
12220 if (o->reloc_count > 0)
12221 o->flags |= SEC_RELOC;
12222 else
12223 {
12224 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12225 set it (this is probably a bug) and if it is set
12226 assign_section_numbers will create a reloc section. */
12227 o->flags &=~ SEC_RELOC;
12228 }
12229
12230 /* If the SEC_ALLOC flag is not set, force the section VMA to
12231 zero. This is done in elf_fake_sections as well, but forcing
12232 the VMA to 0 here will ensure that relocs against these
12233 sections are handled correctly. */
12234 if ((o->flags & SEC_ALLOC) == 0
12235 && ! o->user_set_vma)
12236 o->vma = 0;
12237 }
12238
12239 if (! bfd_link_relocatable (info) && merged)
12240 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12241
12242 /* Figure out the file positions for everything but the symbol table
12243 and the relocs. We set symcount to force assign_section_numbers
12244 to create a symbol table. */
12245 abfd->symcount = info->strip != strip_all || emit_relocs;
12246 BFD_ASSERT (! abfd->output_has_begun);
12247 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12248 goto error_return;
12249
12250 /* Set sizes, and assign file positions for reloc sections. */
12251 for (o = abfd->sections; o != NULL; o = o->next)
12252 {
12253 struct bfd_elf_section_data *esdo = elf_section_data (o);
12254 if ((o->flags & SEC_RELOC) != 0)
12255 {
12256 if (esdo->rel.hdr
12257 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12258 goto error_return;
12259
12260 if (esdo->rela.hdr
12261 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12262 goto error_return;
12263 }
12264
12265 /* _bfd_elf_compute_section_file_positions makes temporary use
12266 of target_index. Reset it. */
12267 o->target_index = 0;
12268
12269 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12270 to count upwards while actually outputting the relocations. */
12271 esdo->rel.count = 0;
12272 esdo->rela.count = 0;
12273
12274 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12275 && !bfd_section_is_ctf (o))
12276 {
12277 /* Cache the section contents so that they can be compressed
12278 later. Use bfd_malloc since it will be freed by
12279 bfd_compress_section_contents. */
12280 unsigned char *contents = esdo->this_hdr.contents;
12281 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12282 abort ();
12283 contents
12284 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12285 if (contents == NULL)
12286 goto error_return;
12287 esdo->this_hdr.contents = contents;
12288 }
12289 }
12290
12291 /* We have now assigned file positions for all the sections except .symtab,
12292 .strtab, and non-loaded reloc and compressed debugging sections. We start
12293 the .symtab section at the current file position, and write directly to it.
12294 We build the .strtab section in memory. */
12295 abfd->symcount = 0;
12296 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12297 /* sh_name is set in prep_headers. */
12298 symtab_hdr->sh_type = SHT_SYMTAB;
12299 /* sh_flags, sh_addr and sh_size all start off zero. */
12300 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12301 /* sh_link is set in assign_section_numbers. */
12302 /* sh_info is set below. */
12303 /* sh_offset is set just below. */
12304 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12305
12306 if (max_sym_count < 20)
12307 max_sym_count = 20;
12308 htab->strtabsize = max_sym_count;
12309 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12310 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12311 if (htab->strtab == NULL)
12312 goto error_return;
12313 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12314 flinfo.symshndxbuf
12315 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12316 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12317
12318 if (info->strip != strip_all || emit_relocs)
12319 {
12320 bfd_boolean name_local_sections;
12321 const char *name;
12322
12323 file_ptr off = elf_next_file_pos (abfd);
12324
12325 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12326
12327 /* Note that at this point elf_next_file_pos (abfd) is
12328 incorrect. We do not yet know the size of the .symtab section.
12329 We correct next_file_pos below, after we do know the size. */
12330
12331 /* Start writing out the symbol table. The first symbol is always a
12332 dummy symbol. */
12333 elfsym.st_value = 0;
12334 elfsym.st_size = 0;
12335 elfsym.st_info = 0;
12336 elfsym.st_other = 0;
12337 elfsym.st_shndx = SHN_UNDEF;
12338 elfsym.st_target_internal = 0;
12339 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12340 bfd_und_section_ptr, NULL) != 1)
12341 goto error_return;
12342
12343 /* Output a symbol for each section. We output these even if we are
12344 discarding local symbols, since they are used for relocs. These
12345 symbols usually have no names. We store the index of each one in
12346 the index field of the section, so that we can find it again when
12347 outputting relocs. */
12348
12349 name_local_sections
12350 = (bed->elf_backend_name_local_section_symbols
12351 && bed->elf_backend_name_local_section_symbols (abfd));
12352
12353 name = NULL;
12354 elfsym.st_size = 0;
12355 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12356 elfsym.st_other = 0;
12357 elfsym.st_value = 0;
12358 elfsym.st_target_internal = 0;
12359 for (i = 1; i < elf_numsections (abfd); i++)
12360 {
12361 o = bfd_section_from_elf_index (abfd, i);
12362 if (o != NULL)
12363 {
12364 o->target_index = bfd_get_symcount (abfd);
12365 elfsym.st_shndx = i;
12366 if (!bfd_link_relocatable (info))
12367 elfsym.st_value = o->vma;
12368 if (name_local_sections)
12369 name = o->name;
12370 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12371 NULL) != 1)
12372 goto error_return;
12373 }
12374 }
12375 }
12376
12377 /* On some targets like Irix 5 the symbol split between local and global
12378 ones recorded in the sh_info field needs to be done between section
12379 and all other symbols. */
12380 if (bed->elf_backend_elfsym_local_is_section
12381 && bed->elf_backend_elfsym_local_is_section (abfd))
12382 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12383
12384 /* Allocate some memory to hold information read in from the input
12385 files. */
12386 if (max_contents_size != 0)
12387 {
12388 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12389 if (flinfo.contents == NULL)
12390 goto error_return;
12391 }
12392
12393 if (max_external_reloc_size != 0)
12394 {
12395 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12396 if (flinfo.external_relocs == NULL)
12397 goto error_return;
12398 }
12399
12400 if (max_internal_reloc_count != 0)
12401 {
12402 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12403 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12404 if (flinfo.internal_relocs == NULL)
12405 goto error_return;
12406 }
12407
12408 if (max_sym_count != 0)
12409 {
12410 amt = max_sym_count * bed->s->sizeof_sym;
12411 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12412 if (flinfo.external_syms == NULL)
12413 goto error_return;
12414
12415 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12416 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12417 if (flinfo.internal_syms == NULL)
12418 goto error_return;
12419
12420 amt = max_sym_count * sizeof (long);
12421 flinfo.indices = (long int *) bfd_malloc (amt);
12422 if (flinfo.indices == NULL)
12423 goto error_return;
12424
12425 amt = max_sym_count * sizeof (asection *);
12426 flinfo.sections = (asection **) bfd_malloc (amt);
12427 if (flinfo.sections == NULL)
12428 goto error_return;
12429 }
12430
12431 if (max_sym_shndx_count != 0)
12432 {
12433 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12434 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12435 if (flinfo.locsym_shndx == NULL)
12436 goto error_return;
12437 }
12438
12439 if (htab->tls_sec)
12440 {
12441 bfd_vma base, end = 0; /* Both bytes. */
12442 asection *sec;
12443
12444 for (sec = htab->tls_sec;
12445 sec && (sec->flags & SEC_THREAD_LOCAL);
12446 sec = sec->next)
12447 {
12448 bfd_size_type size = sec->size;
12449 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12450
12451 if (size == 0
12452 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12453 {
12454 struct bfd_link_order *ord = sec->map_tail.link_order;
12455
12456 if (ord != NULL)
12457 size = ord->offset * opb + ord->size;
12458 }
12459 end = sec->vma + size / opb;
12460 }
12461 base = htab->tls_sec->vma;
12462 /* Only align end of TLS section if static TLS doesn't have special
12463 alignment requirements. */
12464 if (bed->static_tls_alignment == 1)
12465 end = align_power (end, htab->tls_sec->alignment_power);
12466 htab->tls_size = end - base;
12467 }
12468
12469 /* Reorder SHF_LINK_ORDER sections. */
12470 for (o = abfd->sections; o != NULL; o = o->next)
12471 {
12472 if (!elf_fixup_link_order (abfd, o))
12473 return FALSE;
12474 }
12475
12476 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12477 return FALSE;
12478
12479 /* Since ELF permits relocations to be against local symbols, we
12480 must have the local symbols available when we do the relocations.
12481 Since we would rather only read the local symbols once, and we
12482 would rather not keep them in memory, we handle all the
12483 relocations for a single input file at the same time.
12484
12485 Unfortunately, there is no way to know the total number of local
12486 symbols until we have seen all of them, and the local symbol
12487 indices precede the global symbol indices. This means that when
12488 we are generating relocatable output, and we see a reloc against
12489 a global symbol, we can not know the symbol index until we have
12490 finished examining all the local symbols to see which ones we are
12491 going to output. To deal with this, we keep the relocations in
12492 memory, and don't output them until the end of the link. This is
12493 an unfortunate waste of memory, but I don't see a good way around
12494 it. Fortunately, it only happens when performing a relocatable
12495 link, which is not the common case. FIXME: If keep_memory is set
12496 we could write the relocs out and then read them again; I don't
12497 know how bad the memory loss will be. */
12498
12499 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12500 sub->output_has_begun = FALSE;
12501 for (o = abfd->sections; o != NULL; o = o->next)
12502 {
12503 for (p = o->map_head.link_order; p != NULL; p = p->next)
12504 {
12505 if (p->type == bfd_indirect_link_order
12506 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12507 == bfd_target_elf_flavour)
12508 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12509 {
12510 if (! sub->output_has_begun)
12511 {
12512 if (! elf_link_input_bfd (&flinfo, sub))
12513 goto error_return;
12514 sub->output_has_begun = TRUE;
12515 }
12516 }
12517 else if (p->type == bfd_section_reloc_link_order
12518 || p->type == bfd_symbol_reloc_link_order)
12519 {
12520 if (! elf_reloc_link_order (abfd, info, o, p))
12521 goto error_return;
12522 }
12523 else
12524 {
12525 if (! _bfd_default_link_order (abfd, info, o, p))
12526 {
12527 if (p->type == bfd_indirect_link_order
12528 && (bfd_get_flavour (sub)
12529 == bfd_target_elf_flavour)
12530 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12531 != bed->s->elfclass))
12532 {
12533 const char *iclass, *oclass;
12534
12535 switch (bed->s->elfclass)
12536 {
12537 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12538 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12539 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12540 default: abort ();
12541 }
12542
12543 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12544 {
12545 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12546 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12547 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12548 default: abort ();
12549 }
12550
12551 bfd_set_error (bfd_error_wrong_format);
12552 _bfd_error_handler
12553 /* xgettext:c-format */
12554 (_("%pB: file class %s incompatible with %s"),
12555 sub, iclass, oclass);
12556 }
12557
12558 goto error_return;
12559 }
12560 }
12561 }
12562 }
12563
12564 /* Free symbol buffer if needed. */
12565 if (!info->reduce_memory_overheads)
12566 {
12567 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12568 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12569 {
12570 free (elf_tdata (sub)->symbuf);
12571 elf_tdata (sub)->symbuf = NULL;
12572 }
12573 }
12574
12575 /* Output any global symbols that got converted to local in a
12576 version script or due to symbol visibility. We do this in a
12577 separate step since ELF requires all local symbols to appear
12578 prior to any global symbols. FIXME: We should only do this if
12579 some global symbols were, in fact, converted to become local.
12580 FIXME: Will this work correctly with the Irix 5 linker? */
12581 eoinfo.failed = FALSE;
12582 eoinfo.flinfo = &flinfo;
12583 eoinfo.localsyms = TRUE;
12584 eoinfo.file_sym_done = FALSE;
12585 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12586 if (eoinfo.failed)
12587 return FALSE;
12588
12589 /* If backend needs to output some local symbols not present in the hash
12590 table, do it now. */
12591 if (bed->elf_backend_output_arch_local_syms
12592 && (info->strip != strip_all || emit_relocs))
12593 {
12594 typedef int (*out_sym_func)
12595 (void *, const char *, Elf_Internal_Sym *, asection *,
12596 struct elf_link_hash_entry *);
12597
12598 if (! ((*bed->elf_backend_output_arch_local_syms)
12599 (abfd, info, &flinfo,
12600 (out_sym_func) elf_link_output_symstrtab)))
12601 return FALSE;
12602 }
12603
12604 /* That wrote out all the local symbols. Finish up the symbol table
12605 with the global symbols. Even if we want to strip everything we
12606 can, we still need to deal with those global symbols that got
12607 converted to local in a version script. */
12608
12609 /* The sh_info field records the index of the first non local symbol. */
12610 if (!symtab_hdr->sh_info)
12611 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12612
12613 if (dynamic
12614 && htab->dynsym != NULL
12615 && htab->dynsym->output_section != bfd_abs_section_ptr)
12616 {
12617 Elf_Internal_Sym sym;
12618 bfd_byte *dynsym = htab->dynsym->contents;
12619
12620 o = htab->dynsym->output_section;
12621 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12622
12623 /* Write out the section symbols for the output sections. */
12624 if (bfd_link_pic (info)
12625 || htab->is_relocatable_executable)
12626 {
12627 asection *s;
12628
12629 sym.st_size = 0;
12630 sym.st_name = 0;
12631 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12632 sym.st_other = 0;
12633 sym.st_target_internal = 0;
12634
12635 for (s = abfd->sections; s != NULL; s = s->next)
12636 {
12637 int indx;
12638 bfd_byte *dest;
12639 long dynindx;
12640
12641 dynindx = elf_section_data (s)->dynindx;
12642 if (dynindx <= 0)
12643 continue;
12644 indx = elf_section_data (s)->this_idx;
12645 BFD_ASSERT (indx > 0);
12646 sym.st_shndx = indx;
12647 if (! check_dynsym (abfd, &sym))
12648 return FALSE;
12649 sym.st_value = s->vma;
12650 dest = dynsym + dynindx * bed->s->sizeof_sym;
12651 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12652 }
12653 }
12654
12655 /* Write out the local dynsyms. */
12656 if (htab->dynlocal)
12657 {
12658 struct elf_link_local_dynamic_entry *e;
12659 for (e = htab->dynlocal; e ; e = e->next)
12660 {
12661 asection *s;
12662 bfd_byte *dest;
12663
12664 /* Copy the internal symbol and turn off visibility.
12665 Note that we saved a word of storage and overwrote
12666 the original st_name with the dynstr_index. */
12667 sym = e->isym;
12668 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12669 sym.st_shndx = SHN_UNDEF;
12670
12671 s = bfd_section_from_elf_index (e->input_bfd,
12672 e->isym.st_shndx);
12673 if (s != NULL
12674 && s->output_section != NULL
12675 && elf_section_data (s->output_section) != NULL)
12676 {
12677 sym.st_shndx =
12678 elf_section_data (s->output_section)->this_idx;
12679 if (! check_dynsym (abfd, &sym))
12680 return FALSE;
12681 sym.st_value = (s->output_section->vma
12682 + s->output_offset
12683 + e->isym.st_value);
12684 }
12685
12686 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12687 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12688 }
12689 }
12690 }
12691
12692 /* We get the global symbols from the hash table. */
12693 eoinfo.failed = FALSE;
12694 eoinfo.localsyms = FALSE;
12695 eoinfo.flinfo = &flinfo;
12696 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12697 if (eoinfo.failed)
12698 return FALSE;
12699
12700 /* If backend needs to output some symbols not present in the hash
12701 table, do it now. */
12702 if (bed->elf_backend_output_arch_syms
12703 && (info->strip != strip_all || emit_relocs))
12704 {
12705 typedef int (*out_sym_func)
12706 (void *, const char *, Elf_Internal_Sym *, asection *,
12707 struct elf_link_hash_entry *);
12708
12709 if (! ((*bed->elf_backend_output_arch_syms)
12710 (abfd, info, &flinfo,
12711 (out_sym_func) elf_link_output_symstrtab)))
12712 return FALSE;
12713 }
12714
12715 /* Finalize the .strtab section. */
12716 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12717
12718 /* Swap out the .strtab section. */
12719 if (!elf_link_swap_symbols_out (&flinfo))
12720 return FALSE;
12721
12722 /* Now we know the size of the symtab section. */
12723 if (bfd_get_symcount (abfd) > 0)
12724 {
12725 /* Finish up and write out the symbol string table (.strtab)
12726 section. */
12727 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12728 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12729
12730 if (elf_symtab_shndx_list (abfd))
12731 {
12732 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12733
12734 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12735 {
12736 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12737 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12738 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12739 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12740 symtab_shndx_hdr->sh_size = amt;
12741
12742 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12743 off, TRUE);
12744
12745 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12746 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12747 return FALSE;
12748 }
12749 }
12750
12751 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12752 /* sh_name was set in prep_headers. */
12753 symstrtab_hdr->sh_type = SHT_STRTAB;
12754 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12755 symstrtab_hdr->sh_addr = 0;
12756 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12757 symstrtab_hdr->sh_entsize = 0;
12758 symstrtab_hdr->sh_link = 0;
12759 symstrtab_hdr->sh_info = 0;
12760 /* sh_offset is set just below. */
12761 symstrtab_hdr->sh_addralign = 1;
12762
12763 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12764 off, TRUE);
12765 elf_next_file_pos (abfd) = off;
12766
12767 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12768 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12769 return FALSE;
12770 }
12771
12772 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12773 {
12774 _bfd_error_handler (_("%pB: failed to generate import library"),
12775 info->out_implib_bfd);
12776 return FALSE;
12777 }
12778
12779 /* Adjust the relocs to have the correct symbol indices. */
12780 for (o = abfd->sections; o != NULL; o = o->next)
12781 {
12782 struct bfd_elf_section_data *esdo = elf_section_data (o);
12783 bfd_boolean sort;
12784
12785 if ((o->flags & SEC_RELOC) == 0)
12786 continue;
12787
12788 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12789 if (esdo->rel.hdr != NULL
12790 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12791 return FALSE;
12792 if (esdo->rela.hdr != NULL
12793 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12794 return FALSE;
12795
12796 /* Set the reloc_count field to 0 to prevent write_relocs from
12797 trying to swap the relocs out itself. */
12798 o->reloc_count = 0;
12799 }
12800
12801 if (dynamic && info->combreloc && dynobj != NULL)
12802 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12803
12804 /* If we are linking against a dynamic object, or generating a
12805 shared library, finish up the dynamic linking information. */
12806 if (dynamic)
12807 {
12808 bfd_byte *dyncon, *dynconend;
12809
12810 /* Fix up .dynamic entries. */
12811 o = bfd_get_linker_section (dynobj, ".dynamic");
12812 BFD_ASSERT (o != NULL);
12813
12814 dyncon = o->contents;
12815 dynconend = o->contents + o->size;
12816 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12817 {
12818 Elf_Internal_Dyn dyn;
12819 const char *name;
12820 unsigned int type;
12821 bfd_size_type sh_size;
12822 bfd_vma sh_addr;
12823
12824 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12825
12826 switch (dyn.d_tag)
12827 {
12828 default:
12829 continue;
12830 case DT_NULL:
12831 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12832 {
12833 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12834 {
12835 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12836 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12837 default: continue;
12838 }
12839 dyn.d_un.d_val = relativecount;
12840 relativecount = 0;
12841 break;
12842 }
12843 continue;
12844
12845 case DT_INIT:
12846 name = info->init_function;
12847 goto get_sym;
12848 case DT_FINI:
12849 name = info->fini_function;
12850 get_sym:
12851 {
12852 struct elf_link_hash_entry *h;
12853
12854 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12855 if (h != NULL
12856 && (h->root.type == bfd_link_hash_defined
12857 || h->root.type == bfd_link_hash_defweak))
12858 {
12859 dyn.d_un.d_ptr = h->root.u.def.value;
12860 o = h->root.u.def.section;
12861 if (o->output_section != NULL)
12862 dyn.d_un.d_ptr += (o->output_section->vma
12863 + o->output_offset);
12864 else
12865 {
12866 /* The symbol is imported from another shared
12867 library and does not apply to this one. */
12868 dyn.d_un.d_ptr = 0;
12869 }
12870 break;
12871 }
12872 }
12873 continue;
12874
12875 case DT_PREINIT_ARRAYSZ:
12876 name = ".preinit_array";
12877 goto get_out_size;
12878 case DT_INIT_ARRAYSZ:
12879 name = ".init_array";
12880 goto get_out_size;
12881 case DT_FINI_ARRAYSZ:
12882 name = ".fini_array";
12883 get_out_size:
12884 o = bfd_get_section_by_name (abfd, name);
12885 if (o == NULL)
12886 {
12887 _bfd_error_handler
12888 (_("could not find section %s"), name);
12889 goto error_return;
12890 }
12891 if (o->size == 0)
12892 _bfd_error_handler
12893 (_("warning: %s section has zero size"), name);
12894 dyn.d_un.d_val = o->size;
12895 break;
12896
12897 case DT_PREINIT_ARRAY:
12898 name = ".preinit_array";
12899 goto get_out_vma;
12900 case DT_INIT_ARRAY:
12901 name = ".init_array";
12902 goto get_out_vma;
12903 case DT_FINI_ARRAY:
12904 name = ".fini_array";
12905 get_out_vma:
12906 o = bfd_get_section_by_name (abfd, name);
12907 goto do_vma;
12908
12909 case DT_HASH:
12910 name = ".hash";
12911 goto get_vma;
12912 case DT_GNU_HASH:
12913 name = ".gnu.hash";
12914 goto get_vma;
12915 case DT_STRTAB:
12916 name = ".dynstr";
12917 goto get_vma;
12918 case DT_SYMTAB:
12919 name = ".dynsym";
12920 goto get_vma;
12921 case DT_VERDEF:
12922 name = ".gnu.version_d";
12923 goto get_vma;
12924 case DT_VERNEED:
12925 name = ".gnu.version_r";
12926 goto get_vma;
12927 case DT_VERSYM:
12928 name = ".gnu.version";
12929 get_vma:
12930 o = bfd_get_linker_section (dynobj, name);
12931 do_vma:
12932 if (o == NULL || bfd_is_abs_section (o->output_section))
12933 {
12934 _bfd_error_handler
12935 (_("could not find section %s"), name);
12936 goto error_return;
12937 }
12938 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12939 {
12940 _bfd_error_handler
12941 (_("warning: section '%s' is being made into a note"), name);
12942 bfd_set_error (bfd_error_nonrepresentable_section);
12943 goto error_return;
12944 }
12945 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12946 break;
12947
12948 case DT_REL:
12949 case DT_RELA:
12950 case DT_RELSZ:
12951 case DT_RELASZ:
12952 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12953 type = SHT_REL;
12954 else
12955 type = SHT_RELA;
12956 sh_size = 0;
12957 sh_addr = 0;
12958 for (i = 1; i < elf_numsections (abfd); i++)
12959 {
12960 Elf_Internal_Shdr *hdr;
12961
12962 hdr = elf_elfsections (abfd)[i];
12963 if (hdr->sh_type == type
12964 && (hdr->sh_flags & SHF_ALLOC) != 0)
12965 {
12966 sh_size += hdr->sh_size;
12967 if (sh_addr == 0
12968 || sh_addr > hdr->sh_addr)
12969 sh_addr = hdr->sh_addr;
12970 }
12971 }
12972
12973 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12974 {
12975 unsigned int opb = bfd_octets_per_byte (abfd, o);
12976
12977 /* Don't count procedure linkage table relocs in the
12978 overall reloc count. */
12979 sh_size -= htab->srelplt->size;
12980 if (sh_size == 0)
12981 /* If the size is zero, make the address zero too.
12982 This is to avoid a glibc bug. If the backend
12983 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12984 zero, then we'll put DT_RELA at the end of
12985 DT_JMPREL. glibc will interpret the end of
12986 DT_RELA matching the end of DT_JMPREL as the
12987 case where DT_RELA includes DT_JMPREL, and for
12988 LD_BIND_NOW will decide that processing DT_RELA
12989 will process the PLT relocs too. Net result:
12990 No PLT relocs applied. */
12991 sh_addr = 0;
12992
12993 /* If .rela.plt is the first .rela section, exclude
12994 it from DT_RELA. */
12995 else if (sh_addr == (htab->srelplt->output_section->vma
12996 + htab->srelplt->output_offset) * opb)
12997 sh_addr += htab->srelplt->size;
12998 }
12999
13000 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13001 dyn.d_un.d_val = sh_size;
13002 else
13003 dyn.d_un.d_ptr = sh_addr;
13004 break;
13005 }
13006 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13007 }
13008 }
13009
13010 /* If we have created any dynamic sections, then output them. */
13011 if (dynobj != NULL)
13012 {
13013 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13014 goto error_return;
13015
13016 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13017 if (bfd_link_textrel_check (info)
13018 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13019 {
13020 bfd_byte *dyncon, *dynconend;
13021
13022 dyncon = o->contents;
13023 dynconend = o->contents + o->size;
13024 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13025 {
13026 Elf_Internal_Dyn dyn;
13027
13028 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13029
13030 if (dyn.d_tag == DT_TEXTREL)
13031 {
13032 if (info->textrel_check == textrel_check_error)
13033 info->callbacks->einfo
13034 (_("%P%X: read-only segment has dynamic relocations\n"));
13035 else if (bfd_link_dll (info))
13036 info->callbacks->einfo
13037 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13038 else
13039 info->callbacks->einfo
13040 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13041 break;
13042 }
13043 }
13044 }
13045
13046 for (o = dynobj->sections; o != NULL; o = o->next)
13047 {
13048 if ((o->flags & SEC_HAS_CONTENTS) == 0
13049 || o->size == 0
13050 || o->output_section == bfd_abs_section_ptr)
13051 continue;
13052 if ((o->flags & SEC_LINKER_CREATED) == 0)
13053 {
13054 /* At this point, we are only interested in sections
13055 created by _bfd_elf_link_create_dynamic_sections. */
13056 continue;
13057 }
13058 if (htab->stab_info.stabstr == o)
13059 continue;
13060 if (htab->eh_info.hdr_sec == o)
13061 continue;
13062 if (strcmp (o->name, ".dynstr") != 0)
13063 {
13064 bfd_size_type octets = ((file_ptr) o->output_offset
13065 * bfd_octets_per_byte (abfd, o));
13066 if (!bfd_set_section_contents (abfd, o->output_section,
13067 o->contents, octets, o->size))
13068 goto error_return;
13069 }
13070 else
13071 {
13072 /* The contents of the .dynstr section are actually in a
13073 stringtab. */
13074 file_ptr off;
13075
13076 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13077 if (bfd_seek (abfd, off, SEEK_SET) != 0
13078 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13079 goto error_return;
13080 }
13081 }
13082 }
13083
13084 if (!info->resolve_section_groups)
13085 {
13086 bfd_boolean failed = FALSE;
13087
13088 BFD_ASSERT (bfd_link_relocatable (info));
13089 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13090 if (failed)
13091 goto error_return;
13092 }
13093
13094 /* If we have optimized stabs strings, output them. */
13095 if (htab->stab_info.stabstr != NULL)
13096 {
13097 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13098 goto error_return;
13099 }
13100
13101 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13102 goto error_return;
13103
13104 if (info->callbacks->emit_ctf)
13105 info->callbacks->emit_ctf ();
13106
13107 elf_final_link_free (abfd, &flinfo);
13108
13109 if (attr_section)
13110 {
13111 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13112 if (contents == NULL)
13113 return FALSE; /* Bail out and fail. */
13114 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13115 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13116 free (contents);
13117 }
13118
13119 return TRUE;
13120
13121 error_return:
13122 elf_final_link_free (abfd, &flinfo);
13123 return FALSE;
13124 }
13125 \f
13126 /* Initialize COOKIE for input bfd ABFD. */
13127
13128 static bfd_boolean
13129 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13130 struct bfd_link_info *info, bfd *abfd)
13131 {
13132 Elf_Internal_Shdr *symtab_hdr;
13133 const struct elf_backend_data *bed;
13134
13135 bed = get_elf_backend_data (abfd);
13136 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13137
13138 cookie->abfd = abfd;
13139 cookie->sym_hashes = elf_sym_hashes (abfd);
13140 cookie->bad_symtab = elf_bad_symtab (abfd);
13141 if (cookie->bad_symtab)
13142 {
13143 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13144 cookie->extsymoff = 0;
13145 }
13146 else
13147 {
13148 cookie->locsymcount = symtab_hdr->sh_info;
13149 cookie->extsymoff = symtab_hdr->sh_info;
13150 }
13151
13152 if (bed->s->arch_size == 32)
13153 cookie->r_sym_shift = 8;
13154 else
13155 cookie->r_sym_shift = 32;
13156
13157 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13158 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13159 {
13160 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13161 cookie->locsymcount, 0,
13162 NULL, NULL, NULL);
13163 if (cookie->locsyms == NULL)
13164 {
13165 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13166 return FALSE;
13167 }
13168 if (info->keep_memory)
13169 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13170 }
13171 return TRUE;
13172 }
13173
13174 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13175
13176 static void
13177 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13178 {
13179 Elf_Internal_Shdr *symtab_hdr;
13180
13181 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13182 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13183 free (cookie->locsyms);
13184 }
13185
13186 /* Initialize the relocation information in COOKIE for input section SEC
13187 of input bfd ABFD. */
13188
13189 static bfd_boolean
13190 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13191 struct bfd_link_info *info, bfd *abfd,
13192 asection *sec)
13193 {
13194 if (sec->reloc_count == 0)
13195 {
13196 cookie->rels = NULL;
13197 cookie->relend = NULL;
13198 }
13199 else
13200 {
13201 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13202 info->keep_memory);
13203 if (cookie->rels == NULL)
13204 return FALSE;
13205 cookie->rel = cookie->rels;
13206 cookie->relend = cookie->rels + sec->reloc_count;
13207 }
13208 cookie->rel = cookie->rels;
13209 return TRUE;
13210 }
13211
13212 /* Free the memory allocated by init_reloc_cookie_rels,
13213 if appropriate. */
13214
13215 static void
13216 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13217 asection *sec)
13218 {
13219 if (elf_section_data (sec)->relocs != cookie->rels)
13220 free (cookie->rels);
13221 }
13222
13223 /* Initialize the whole of COOKIE for input section SEC. */
13224
13225 static bfd_boolean
13226 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13227 struct bfd_link_info *info,
13228 asection *sec)
13229 {
13230 if (!init_reloc_cookie (cookie, info, sec->owner))
13231 goto error1;
13232 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13233 goto error2;
13234 return TRUE;
13235
13236 error2:
13237 fini_reloc_cookie (cookie, sec->owner);
13238 error1:
13239 return FALSE;
13240 }
13241
13242 /* Free the memory allocated by init_reloc_cookie_for_section,
13243 if appropriate. */
13244
13245 static void
13246 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13247 asection *sec)
13248 {
13249 fini_reloc_cookie_rels (cookie, sec);
13250 fini_reloc_cookie (cookie, sec->owner);
13251 }
13252 \f
13253 /* Garbage collect unused sections. */
13254
13255 /* Default gc_mark_hook. */
13256
13257 asection *
13258 _bfd_elf_gc_mark_hook (asection *sec,
13259 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13260 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13261 struct elf_link_hash_entry *h,
13262 Elf_Internal_Sym *sym)
13263 {
13264 if (h != NULL)
13265 {
13266 switch (h->root.type)
13267 {
13268 case bfd_link_hash_defined:
13269 case bfd_link_hash_defweak:
13270 return h->root.u.def.section;
13271
13272 case bfd_link_hash_common:
13273 return h->root.u.c.p->section;
13274
13275 default:
13276 break;
13277 }
13278 }
13279 else
13280 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13281
13282 return NULL;
13283 }
13284
13285 /* Return the debug definition section. */
13286
13287 static asection *
13288 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13289 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13290 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13291 struct elf_link_hash_entry *h,
13292 Elf_Internal_Sym *sym)
13293 {
13294 if (h != NULL)
13295 {
13296 /* Return the global debug definition section. */
13297 if ((h->root.type == bfd_link_hash_defined
13298 || h->root.type == bfd_link_hash_defweak)
13299 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13300 return h->root.u.def.section;
13301 }
13302 else
13303 {
13304 /* Return the local debug definition section. */
13305 asection *isec = bfd_section_from_elf_index (sec->owner,
13306 sym->st_shndx);
13307 if ((isec->flags & SEC_DEBUGGING) != 0)
13308 return isec;
13309 }
13310
13311 return NULL;
13312 }
13313
13314 /* COOKIE->rel describes a relocation against section SEC, which is
13315 a section we've decided to keep. Return the section that contains
13316 the relocation symbol, or NULL if no section contains it. */
13317
13318 asection *
13319 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13320 elf_gc_mark_hook_fn gc_mark_hook,
13321 struct elf_reloc_cookie *cookie,
13322 bfd_boolean *start_stop)
13323 {
13324 unsigned long r_symndx;
13325 struct elf_link_hash_entry *h, *hw;
13326
13327 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13328 if (r_symndx == STN_UNDEF)
13329 return NULL;
13330
13331 if (r_symndx >= cookie->locsymcount
13332 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13333 {
13334 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13335 if (h == NULL)
13336 {
13337 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13338 sec->owner);
13339 return NULL;
13340 }
13341 while (h->root.type == bfd_link_hash_indirect
13342 || h->root.type == bfd_link_hash_warning)
13343 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13344 h->mark = 1;
13345 /* Keep all aliases of the symbol too. If an object symbol
13346 needs to be copied into .dynbss then all of its aliases
13347 should be present as dynamic symbols, not just the one used
13348 on the copy relocation. */
13349 hw = h;
13350 while (hw->is_weakalias)
13351 {
13352 hw = hw->u.alias;
13353 hw->mark = 1;
13354 }
13355
13356 if (start_stop != NULL)
13357 {
13358 /* To work around a glibc bug, mark XXX input sections
13359 when there is a reference to __start_XXX or __stop_XXX
13360 symbols. */
13361 if (h->start_stop)
13362 {
13363 asection *s = h->u2.start_stop_section;
13364 *start_stop = !s->gc_mark;
13365 return s;
13366 }
13367 }
13368
13369 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13370 }
13371
13372 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13373 &cookie->locsyms[r_symndx]);
13374 }
13375
13376 /* COOKIE->rel describes a relocation against section SEC, which is
13377 a section we've decided to keep. Mark the section that contains
13378 the relocation symbol. */
13379
13380 bfd_boolean
13381 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13382 asection *sec,
13383 elf_gc_mark_hook_fn gc_mark_hook,
13384 struct elf_reloc_cookie *cookie)
13385 {
13386 asection *rsec;
13387 bfd_boolean start_stop = FALSE;
13388
13389 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13390 while (rsec != NULL)
13391 {
13392 if (!rsec->gc_mark)
13393 {
13394 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13395 || (rsec->owner->flags & DYNAMIC) != 0)
13396 rsec->gc_mark = 1;
13397 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13398 return FALSE;
13399 }
13400 if (!start_stop)
13401 break;
13402 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13403 }
13404 return TRUE;
13405 }
13406
13407 /* The mark phase of garbage collection. For a given section, mark
13408 it and any sections in this section's group, and all the sections
13409 which define symbols to which it refers. */
13410
13411 bfd_boolean
13412 _bfd_elf_gc_mark (struct bfd_link_info *info,
13413 asection *sec,
13414 elf_gc_mark_hook_fn gc_mark_hook)
13415 {
13416 bfd_boolean ret;
13417 asection *group_sec, *eh_frame;
13418
13419 sec->gc_mark = 1;
13420
13421 /* Mark all the sections in the group. */
13422 group_sec = elf_section_data (sec)->next_in_group;
13423 if (group_sec && !group_sec->gc_mark)
13424 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13425 return FALSE;
13426
13427 /* Look through the section relocs. */
13428 ret = TRUE;
13429 eh_frame = elf_eh_frame_section (sec->owner);
13430 if ((sec->flags & SEC_RELOC) != 0
13431 && sec->reloc_count > 0
13432 && sec != eh_frame)
13433 {
13434 struct elf_reloc_cookie cookie;
13435
13436 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13437 ret = FALSE;
13438 else
13439 {
13440 for (; cookie.rel < cookie.relend; cookie.rel++)
13441 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13442 {
13443 ret = FALSE;
13444 break;
13445 }
13446 fini_reloc_cookie_for_section (&cookie, sec);
13447 }
13448 }
13449
13450 if (ret && eh_frame && elf_fde_list (sec))
13451 {
13452 struct elf_reloc_cookie cookie;
13453
13454 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13455 ret = FALSE;
13456 else
13457 {
13458 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13459 gc_mark_hook, &cookie))
13460 ret = FALSE;
13461 fini_reloc_cookie_for_section (&cookie, eh_frame);
13462 }
13463 }
13464
13465 eh_frame = elf_section_eh_frame_entry (sec);
13466 if (ret && eh_frame && !eh_frame->gc_mark)
13467 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13468 ret = FALSE;
13469
13470 return ret;
13471 }
13472
13473 /* Scan and mark sections in a special or debug section group. */
13474
13475 static void
13476 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13477 {
13478 /* Point to first section of section group. */
13479 asection *ssec;
13480 /* Used to iterate the section group. */
13481 asection *msec;
13482
13483 bfd_boolean is_special_grp = TRUE;
13484 bfd_boolean is_debug_grp = TRUE;
13485
13486 /* First scan to see if group contains any section other than debug
13487 and special section. */
13488 ssec = msec = elf_next_in_group (grp);
13489 do
13490 {
13491 if ((msec->flags & SEC_DEBUGGING) == 0)
13492 is_debug_grp = FALSE;
13493
13494 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13495 is_special_grp = FALSE;
13496
13497 msec = elf_next_in_group (msec);
13498 }
13499 while (msec != ssec);
13500
13501 /* If this is a pure debug section group or pure special section group,
13502 keep all sections in this group. */
13503 if (is_debug_grp || is_special_grp)
13504 {
13505 do
13506 {
13507 msec->gc_mark = 1;
13508 msec = elf_next_in_group (msec);
13509 }
13510 while (msec != ssec);
13511 }
13512 }
13513
13514 /* Keep debug and special sections. */
13515
13516 bfd_boolean
13517 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13518 elf_gc_mark_hook_fn mark_hook)
13519 {
13520 bfd *ibfd;
13521
13522 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13523 {
13524 asection *isec;
13525 bfd_boolean some_kept;
13526 bfd_boolean debug_frag_seen;
13527 bfd_boolean has_kept_debug_info;
13528
13529 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13530 continue;
13531 isec = ibfd->sections;
13532 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13533 continue;
13534
13535 /* Ensure all linker created sections are kept,
13536 see if any other section is already marked,
13537 and note if we have any fragmented debug sections. */
13538 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13539 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13540 {
13541 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13542 isec->gc_mark = 1;
13543 else if (isec->gc_mark
13544 && (isec->flags & SEC_ALLOC) != 0
13545 && elf_section_type (isec) != SHT_NOTE)
13546 some_kept = TRUE;
13547 else
13548 {
13549 /* Since all sections, except for backend specific ones,
13550 have been garbage collected, call mark_hook on this
13551 section if any of its linked-to sections is marked. */
13552 asection *linked_to_sec = elf_linked_to_section (isec);
13553 for (; linked_to_sec != NULL;
13554 linked_to_sec = elf_linked_to_section (linked_to_sec))
13555 if (linked_to_sec->gc_mark)
13556 {
13557 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13558 return FALSE;
13559 break;
13560 }
13561 }
13562
13563 if (!debug_frag_seen
13564 && (isec->flags & SEC_DEBUGGING)
13565 && CONST_STRNEQ (isec->name, ".debug_line."))
13566 debug_frag_seen = TRUE;
13567 else if (strcmp (bfd_section_name (isec),
13568 "__patchable_function_entries") == 0
13569 && elf_linked_to_section (isec) == NULL)
13570 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13571 "need linked-to section "
13572 "for --gc-sections\n"),
13573 isec->owner, isec);
13574 }
13575
13576 /* If no non-note alloc section in this file will be kept, then
13577 we can toss out the debug and special sections. */
13578 if (!some_kept)
13579 continue;
13580
13581 /* Keep debug and special sections like .comment when they are
13582 not part of a group. Also keep section groups that contain
13583 just debug sections or special sections. NB: Sections with
13584 linked-to section has been handled above. */
13585 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13586 {
13587 if ((isec->flags & SEC_GROUP) != 0)
13588 _bfd_elf_gc_mark_debug_special_section_group (isec);
13589 else if (((isec->flags & SEC_DEBUGGING) != 0
13590 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13591 && elf_next_in_group (isec) == NULL
13592 && elf_linked_to_section (isec) == NULL)
13593 isec->gc_mark = 1;
13594 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13595 has_kept_debug_info = TRUE;
13596 }
13597
13598 /* Look for CODE sections which are going to be discarded,
13599 and find and discard any fragmented debug sections which
13600 are associated with that code section. */
13601 if (debug_frag_seen)
13602 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13603 if ((isec->flags & SEC_CODE) != 0
13604 && isec->gc_mark == 0)
13605 {
13606 unsigned int ilen;
13607 asection *dsec;
13608
13609 ilen = strlen (isec->name);
13610
13611 /* Association is determined by the name of the debug
13612 section containing the name of the code section as
13613 a suffix. For example .debug_line.text.foo is a
13614 debug section associated with .text.foo. */
13615 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13616 {
13617 unsigned int dlen;
13618
13619 if (dsec->gc_mark == 0
13620 || (dsec->flags & SEC_DEBUGGING) == 0)
13621 continue;
13622
13623 dlen = strlen (dsec->name);
13624
13625 if (dlen > ilen
13626 && strncmp (dsec->name + (dlen - ilen),
13627 isec->name, ilen) == 0)
13628 dsec->gc_mark = 0;
13629 }
13630 }
13631
13632 /* Mark debug sections referenced by kept debug sections. */
13633 if (has_kept_debug_info)
13634 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13635 if (isec->gc_mark
13636 && (isec->flags & SEC_DEBUGGING) != 0)
13637 if (!_bfd_elf_gc_mark (info, isec,
13638 elf_gc_mark_debug_section))
13639 return FALSE;
13640 }
13641 return TRUE;
13642 }
13643
13644 static bfd_boolean
13645 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13646 {
13647 bfd *sub;
13648 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13649
13650 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13651 {
13652 asection *o;
13653
13654 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13655 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13656 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13657 continue;
13658 o = sub->sections;
13659 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13660 continue;
13661
13662 for (o = sub->sections; o != NULL; o = o->next)
13663 {
13664 /* When any section in a section group is kept, we keep all
13665 sections in the section group. If the first member of
13666 the section group is excluded, we will also exclude the
13667 group section. */
13668 if (o->flags & SEC_GROUP)
13669 {
13670 asection *first = elf_next_in_group (o);
13671 o->gc_mark = first->gc_mark;
13672 }
13673
13674 if (o->gc_mark)
13675 continue;
13676
13677 /* Skip sweeping sections already excluded. */
13678 if (o->flags & SEC_EXCLUDE)
13679 continue;
13680
13681 /* Since this is early in the link process, it is simple
13682 to remove a section from the output. */
13683 o->flags |= SEC_EXCLUDE;
13684
13685 if (info->print_gc_sections && o->size != 0)
13686 /* xgettext:c-format */
13687 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13688 o, sub);
13689 }
13690 }
13691
13692 return TRUE;
13693 }
13694
13695 /* Propagate collected vtable information. This is called through
13696 elf_link_hash_traverse. */
13697
13698 static bfd_boolean
13699 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13700 {
13701 /* Those that are not vtables. */
13702 if (h->start_stop
13703 || h->u2.vtable == NULL
13704 || h->u2.vtable->parent == NULL)
13705 return TRUE;
13706
13707 /* Those vtables that do not have parents, we cannot merge. */
13708 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13709 return TRUE;
13710
13711 /* If we've already been done, exit. */
13712 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13713 return TRUE;
13714
13715 /* Make sure the parent's table is up to date. */
13716 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13717
13718 if (h->u2.vtable->used == NULL)
13719 {
13720 /* None of this table's entries were referenced. Re-use the
13721 parent's table. */
13722 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13723 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13724 }
13725 else
13726 {
13727 size_t n;
13728 bfd_boolean *cu, *pu;
13729
13730 /* Or the parent's entries into ours. */
13731 cu = h->u2.vtable->used;
13732 cu[-1] = TRUE;
13733 pu = h->u2.vtable->parent->u2.vtable->used;
13734 if (pu != NULL)
13735 {
13736 const struct elf_backend_data *bed;
13737 unsigned int log_file_align;
13738
13739 bed = get_elf_backend_data (h->root.u.def.section->owner);
13740 log_file_align = bed->s->log_file_align;
13741 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13742 while (n--)
13743 {
13744 if (*pu)
13745 *cu = TRUE;
13746 pu++;
13747 cu++;
13748 }
13749 }
13750 }
13751
13752 return TRUE;
13753 }
13754
13755 static bfd_boolean
13756 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13757 {
13758 asection *sec;
13759 bfd_vma hstart, hend;
13760 Elf_Internal_Rela *relstart, *relend, *rel;
13761 const struct elf_backend_data *bed;
13762 unsigned int log_file_align;
13763
13764 /* Take care of both those symbols that do not describe vtables as
13765 well as those that are not loaded. */
13766 if (h->start_stop
13767 || h->u2.vtable == NULL
13768 || h->u2.vtable->parent == NULL)
13769 return TRUE;
13770
13771 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13772 || h->root.type == bfd_link_hash_defweak);
13773
13774 sec = h->root.u.def.section;
13775 hstart = h->root.u.def.value;
13776 hend = hstart + h->size;
13777
13778 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13779 if (!relstart)
13780 return *(bfd_boolean *) okp = FALSE;
13781 bed = get_elf_backend_data (sec->owner);
13782 log_file_align = bed->s->log_file_align;
13783
13784 relend = relstart + sec->reloc_count;
13785
13786 for (rel = relstart; rel < relend; ++rel)
13787 if (rel->r_offset >= hstart && rel->r_offset < hend)
13788 {
13789 /* If the entry is in use, do nothing. */
13790 if (h->u2.vtable->used
13791 && (rel->r_offset - hstart) < h->u2.vtable->size)
13792 {
13793 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13794 if (h->u2.vtable->used[entry])
13795 continue;
13796 }
13797 /* Otherwise, kill it. */
13798 rel->r_offset = rel->r_info = rel->r_addend = 0;
13799 }
13800
13801 return TRUE;
13802 }
13803
13804 /* Mark sections containing dynamically referenced symbols. When
13805 building shared libraries, we must assume that any visible symbol is
13806 referenced. */
13807
13808 bfd_boolean
13809 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13810 {
13811 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13812 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13813
13814 if ((h->root.type == bfd_link_hash_defined
13815 || h->root.type == bfd_link_hash_defweak)
13816 && ((h->ref_dynamic && !h->forced_local)
13817 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13818 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13819 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13820 && (!bfd_link_executable (info)
13821 || info->gc_keep_exported
13822 || info->export_dynamic
13823 || (h->dynamic
13824 && d != NULL
13825 && (*d->match) (&d->head, NULL, h->root.root.string)))
13826 && (h->versioned >= versioned
13827 || !bfd_hide_sym_by_version (info->version_info,
13828 h->root.root.string)))))
13829 h->root.u.def.section->flags |= SEC_KEEP;
13830
13831 return TRUE;
13832 }
13833
13834 /* Keep all sections containing symbols undefined on the command-line,
13835 and the section containing the entry symbol. */
13836
13837 void
13838 _bfd_elf_gc_keep (struct bfd_link_info *info)
13839 {
13840 struct bfd_sym_chain *sym;
13841
13842 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13843 {
13844 struct elf_link_hash_entry *h;
13845
13846 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13847 FALSE, FALSE, FALSE);
13848
13849 if (h != NULL
13850 && (h->root.type == bfd_link_hash_defined
13851 || h->root.type == bfd_link_hash_defweak)
13852 && !bfd_is_const_section (h->root.u.def.section))
13853 h->root.u.def.section->flags |= SEC_KEEP;
13854 }
13855 }
13856
13857 bfd_boolean
13858 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13859 struct bfd_link_info *info)
13860 {
13861 bfd *ibfd = info->input_bfds;
13862
13863 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13864 {
13865 asection *sec;
13866 struct elf_reloc_cookie cookie;
13867
13868 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13869 continue;
13870 sec = ibfd->sections;
13871 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13872 continue;
13873
13874 if (!init_reloc_cookie (&cookie, info, ibfd))
13875 return FALSE;
13876
13877 for (sec = ibfd->sections; sec; sec = sec->next)
13878 {
13879 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13880 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13881 {
13882 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13883 fini_reloc_cookie_rels (&cookie, sec);
13884 }
13885 }
13886 }
13887 return TRUE;
13888 }
13889
13890 /* Do mark and sweep of unused sections. */
13891
13892 bfd_boolean
13893 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13894 {
13895 bfd_boolean ok = TRUE;
13896 bfd *sub;
13897 elf_gc_mark_hook_fn gc_mark_hook;
13898 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13899 struct elf_link_hash_table *htab;
13900
13901 if (!bed->can_gc_sections
13902 || !is_elf_hash_table (info->hash))
13903 {
13904 _bfd_error_handler(_("warning: gc-sections option ignored"));
13905 return TRUE;
13906 }
13907
13908 bed->gc_keep (info);
13909 htab = elf_hash_table (info);
13910
13911 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13912 at the .eh_frame section if we can mark the FDEs individually. */
13913 for (sub = info->input_bfds;
13914 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13915 sub = sub->link.next)
13916 {
13917 asection *sec;
13918 struct elf_reloc_cookie cookie;
13919
13920 sec = sub->sections;
13921 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13922 continue;
13923 sec = bfd_get_section_by_name (sub, ".eh_frame");
13924 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13925 {
13926 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13927 if (elf_section_data (sec)->sec_info
13928 && (sec->flags & SEC_LINKER_CREATED) == 0)
13929 elf_eh_frame_section (sub) = sec;
13930 fini_reloc_cookie_for_section (&cookie, sec);
13931 sec = bfd_get_next_section_by_name (NULL, sec);
13932 }
13933 }
13934
13935 /* Apply transitive closure to the vtable entry usage info. */
13936 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13937 if (!ok)
13938 return FALSE;
13939
13940 /* Kill the vtable relocations that were not used. */
13941 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13942 if (!ok)
13943 return FALSE;
13944
13945 /* Mark dynamically referenced symbols. */
13946 if (htab->dynamic_sections_created || info->gc_keep_exported)
13947 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13948
13949 /* Grovel through relocs to find out who stays ... */
13950 gc_mark_hook = bed->gc_mark_hook;
13951 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13952 {
13953 asection *o;
13954
13955 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13956 || elf_object_id (sub) != elf_hash_table_id (htab)
13957 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13958 continue;
13959
13960 o = sub->sections;
13961 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13962 continue;
13963
13964 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13965 Also treat note sections as a root, if the section is not part
13966 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13967 well as FINI_ARRAY sections for ld -r. */
13968 for (o = sub->sections; o != NULL; o = o->next)
13969 if (!o->gc_mark
13970 && (o->flags & SEC_EXCLUDE) == 0
13971 && ((o->flags & SEC_KEEP) != 0
13972 || (bfd_link_relocatable (info)
13973 && ((elf_section_data (o)->this_hdr.sh_type
13974 == SHT_PREINIT_ARRAY)
13975 || (elf_section_data (o)->this_hdr.sh_type
13976 == SHT_INIT_ARRAY)
13977 || (elf_section_data (o)->this_hdr.sh_type
13978 == SHT_FINI_ARRAY)))
13979 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13980 && elf_next_in_group (o) == NULL )))
13981 {
13982 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13983 return FALSE;
13984 }
13985 }
13986
13987 /* Allow the backend to mark additional target specific sections. */
13988 bed->gc_mark_extra_sections (info, gc_mark_hook);
13989
13990 /* ... and mark SEC_EXCLUDE for those that go. */
13991 return elf_gc_sweep (abfd, info);
13992 }
13993 \f
13994 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13995
13996 bfd_boolean
13997 bfd_elf_gc_record_vtinherit (bfd *abfd,
13998 asection *sec,
13999 struct elf_link_hash_entry *h,
14000 bfd_vma offset)
14001 {
14002 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14003 struct elf_link_hash_entry **search, *child;
14004 size_t extsymcount;
14005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14006
14007 /* The sh_info field of the symtab header tells us where the
14008 external symbols start. We don't care about the local symbols at
14009 this point. */
14010 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14011 if (!elf_bad_symtab (abfd))
14012 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14013
14014 sym_hashes = elf_sym_hashes (abfd);
14015 sym_hashes_end = sym_hashes + extsymcount;
14016
14017 /* Hunt down the child symbol, which is in this section at the same
14018 offset as the relocation. */
14019 for (search = sym_hashes; search != sym_hashes_end; ++search)
14020 {
14021 if ((child = *search) != NULL
14022 && (child->root.type == bfd_link_hash_defined
14023 || child->root.type == bfd_link_hash_defweak)
14024 && child->root.u.def.section == sec
14025 && child->root.u.def.value == offset)
14026 goto win;
14027 }
14028
14029 /* xgettext:c-format */
14030 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14031 abfd, sec, (uint64_t) offset);
14032 bfd_set_error (bfd_error_invalid_operation);
14033 return FALSE;
14034
14035 win:
14036 if (!child->u2.vtable)
14037 {
14038 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14039 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14040 if (!child->u2.vtable)
14041 return FALSE;
14042 }
14043 if (!h)
14044 {
14045 /* This *should* only be the absolute section. It could potentially
14046 be that someone has defined a non-global vtable though, which
14047 would be bad. It isn't worth paging in the local symbols to be
14048 sure though; that case should simply be handled by the assembler. */
14049
14050 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14051 }
14052 else
14053 child->u2.vtable->parent = h;
14054
14055 return TRUE;
14056 }
14057
14058 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14059
14060 bfd_boolean
14061 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14062 struct elf_link_hash_entry *h,
14063 bfd_vma addend)
14064 {
14065 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14066 unsigned int log_file_align = bed->s->log_file_align;
14067
14068 if (!h)
14069 {
14070 /* xgettext:c-format */
14071 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14072 abfd, sec);
14073 bfd_set_error (bfd_error_bad_value);
14074 return FALSE;
14075 }
14076
14077 if (!h->u2.vtable)
14078 {
14079 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14080 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14081 if (!h->u2.vtable)
14082 return FALSE;
14083 }
14084
14085 if (addend >= h->u2.vtable->size)
14086 {
14087 size_t size, bytes, file_align;
14088 bfd_boolean *ptr = h->u2.vtable->used;
14089
14090 /* While the symbol is undefined, we have to be prepared to handle
14091 a zero size. */
14092 file_align = 1 << log_file_align;
14093 if (h->root.type == bfd_link_hash_undefined)
14094 size = addend + file_align;
14095 else
14096 {
14097 size = h->size;
14098 if (addend >= size)
14099 {
14100 /* Oops! We've got a reference past the defined end of
14101 the table. This is probably a bug -- shall we warn? */
14102 size = addend + file_align;
14103 }
14104 }
14105 size = (size + file_align - 1) & -file_align;
14106
14107 /* Allocate one extra entry for use as a "done" flag for the
14108 consolidation pass. */
14109 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14110
14111 if (ptr)
14112 {
14113 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14114
14115 if (ptr != NULL)
14116 {
14117 size_t oldbytes;
14118
14119 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14120 * sizeof (bfd_boolean));
14121 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14122 }
14123 }
14124 else
14125 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14126
14127 if (ptr == NULL)
14128 return FALSE;
14129
14130 /* And arrange for that done flag to be at index -1. */
14131 h->u2.vtable->used = ptr + 1;
14132 h->u2.vtable->size = size;
14133 }
14134
14135 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14136
14137 return TRUE;
14138 }
14139
14140 /* Map an ELF section header flag to its corresponding string. */
14141 typedef struct
14142 {
14143 char *flag_name;
14144 flagword flag_value;
14145 } elf_flags_to_name_table;
14146
14147 static elf_flags_to_name_table elf_flags_to_names [] =
14148 {
14149 { "SHF_WRITE", SHF_WRITE },
14150 { "SHF_ALLOC", SHF_ALLOC },
14151 { "SHF_EXECINSTR", SHF_EXECINSTR },
14152 { "SHF_MERGE", SHF_MERGE },
14153 { "SHF_STRINGS", SHF_STRINGS },
14154 { "SHF_INFO_LINK", SHF_INFO_LINK},
14155 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14156 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14157 { "SHF_GROUP", SHF_GROUP },
14158 { "SHF_TLS", SHF_TLS },
14159 { "SHF_MASKOS", SHF_MASKOS },
14160 { "SHF_EXCLUDE", SHF_EXCLUDE },
14161 };
14162
14163 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14164 bfd_boolean
14165 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14166 struct flag_info *flaginfo,
14167 asection *section)
14168 {
14169 const bfd_vma sh_flags = elf_section_flags (section);
14170
14171 if (!flaginfo->flags_initialized)
14172 {
14173 bfd *obfd = info->output_bfd;
14174 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14175 struct flag_info_list *tf = flaginfo->flag_list;
14176 int with_hex = 0;
14177 int without_hex = 0;
14178
14179 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14180 {
14181 unsigned i;
14182 flagword (*lookup) (char *);
14183
14184 lookup = bed->elf_backend_lookup_section_flags_hook;
14185 if (lookup != NULL)
14186 {
14187 flagword hexval = (*lookup) ((char *) tf->name);
14188
14189 if (hexval != 0)
14190 {
14191 if (tf->with == with_flags)
14192 with_hex |= hexval;
14193 else if (tf->with == without_flags)
14194 without_hex |= hexval;
14195 tf->valid = TRUE;
14196 continue;
14197 }
14198 }
14199 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14200 {
14201 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14202 {
14203 if (tf->with == with_flags)
14204 with_hex |= elf_flags_to_names[i].flag_value;
14205 else if (tf->with == without_flags)
14206 without_hex |= elf_flags_to_names[i].flag_value;
14207 tf->valid = TRUE;
14208 break;
14209 }
14210 }
14211 if (!tf->valid)
14212 {
14213 info->callbacks->einfo
14214 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14215 return FALSE;
14216 }
14217 }
14218 flaginfo->flags_initialized = TRUE;
14219 flaginfo->only_with_flags |= with_hex;
14220 flaginfo->not_with_flags |= without_hex;
14221 }
14222
14223 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14224 return FALSE;
14225
14226 if ((flaginfo->not_with_flags & sh_flags) != 0)
14227 return FALSE;
14228
14229 return TRUE;
14230 }
14231
14232 struct alloc_got_off_arg {
14233 bfd_vma gotoff;
14234 struct bfd_link_info *info;
14235 };
14236
14237 /* We need a special top-level link routine to convert got reference counts
14238 to real got offsets. */
14239
14240 static bfd_boolean
14241 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14242 {
14243 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14244 bfd *obfd = gofarg->info->output_bfd;
14245 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14246
14247 if (h->got.refcount > 0)
14248 {
14249 h->got.offset = gofarg->gotoff;
14250 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14251 }
14252 else
14253 h->got.offset = (bfd_vma) -1;
14254
14255 return TRUE;
14256 }
14257
14258 /* And an accompanying bit to work out final got entry offsets once
14259 we're done. Should be called from final_link. */
14260
14261 bfd_boolean
14262 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14263 struct bfd_link_info *info)
14264 {
14265 bfd *i;
14266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14267 bfd_vma gotoff;
14268 struct alloc_got_off_arg gofarg;
14269
14270 BFD_ASSERT (abfd == info->output_bfd);
14271
14272 if (! is_elf_hash_table (info->hash))
14273 return FALSE;
14274
14275 /* The GOT offset is relative to the .got section, but the GOT header is
14276 put into the .got.plt section, if the backend uses it. */
14277 if (bed->want_got_plt)
14278 gotoff = 0;
14279 else
14280 gotoff = bed->got_header_size;
14281
14282 /* Do the local .got entries first. */
14283 for (i = info->input_bfds; i; i = i->link.next)
14284 {
14285 bfd_signed_vma *local_got;
14286 size_t j, locsymcount;
14287 Elf_Internal_Shdr *symtab_hdr;
14288
14289 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14290 continue;
14291
14292 local_got = elf_local_got_refcounts (i);
14293 if (!local_got)
14294 continue;
14295
14296 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14297 if (elf_bad_symtab (i))
14298 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14299 else
14300 locsymcount = symtab_hdr->sh_info;
14301
14302 for (j = 0; j < locsymcount; ++j)
14303 {
14304 if (local_got[j] > 0)
14305 {
14306 local_got[j] = gotoff;
14307 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14308 }
14309 else
14310 local_got[j] = (bfd_vma) -1;
14311 }
14312 }
14313
14314 /* Then the global .got entries. .plt refcounts are handled by
14315 adjust_dynamic_symbol */
14316 gofarg.gotoff = gotoff;
14317 gofarg.info = info;
14318 elf_link_hash_traverse (elf_hash_table (info),
14319 elf_gc_allocate_got_offsets,
14320 &gofarg);
14321 return TRUE;
14322 }
14323
14324 /* Many folk need no more in the way of final link than this, once
14325 got entry reference counting is enabled. */
14326
14327 bfd_boolean
14328 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14329 {
14330 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14331 return FALSE;
14332
14333 /* Invoke the regular ELF backend linker to do all the work. */
14334 return bfd_elf_final_link (abfd, info);
14335 }
14336
14337 bfd_boolean
14338 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14339 {
14340 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14341
14342 if (rcookie->bad_symtab)
14343 rcookie->rel = rcookie->rels;
14344
14345 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14346 {
14347 unsigned long r_symndx;
14348
14349 if (! rcookie->bad_symtab)
14350 if (rcookie->rel->r_offset > offset)
14351 return FALSE;
14352 if (rcookie->rel->r_offset != offset)
14353 continue;
14354
14355 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14356 if (r_symndx == STN_UNDEF)
14357 return TRUE;
14358
14359 if (r_symndx >= rcookie->locsymcount
14360 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14361 {
14362 struct elf_link_hash_entry *h;
14363
14364 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14365
14366 while (h->root.type == bfd_link_hash_indirect
14367 || h->root.type == bfd_link_hash_warning)
14368 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14369
14370 if ((h->root.type == bfd_link_hash_defined
14371 || h->root.type == bfd_link_hash_defweak)
14372 && (h->root.u.def.section->owner != rcookie->abfd
14373 || h->root.u.def.section->kept_section != NULL
14374 || discarded_section (h->root.u.def.section)))
14375 return TRUE;
14376 }
14377 else
14378 {
14379 /* It's not a relocation against a global symbol,
14380 but it could be a relocation against a local
14381 symbol for a discarded section. */
14382 asection *isec;
14383 Elf_Internal_Sym *isym;
14384
14385 /* Need to: get the symbol; get the section. */
14386 isym = &rcookie->locsyms[r_symndx];
14387 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14388 if (isec != NULL
14389 && (isec->kept_section != NULL
14390 || discarded_section (isec)))
14391 return TRUE;
14392 }
14393 return FALSE;
14394 }
14395 return FALSE;
14396 }
14397
14398 /* Discard unneeded references to discarded sections.
14399 Returns -1 on error, 1 if any section's size was changed, 0 if
14400 nothing changed. This function assumes that the relocations are in
14401 sorted order, which is true for all known assemblers. */
14402
14403 int
14404 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14405 {
14406 struct elf_reloc_cookie cookie;
14407 asection *o;
14408 bfd *abfd;
14409 int changed = 0;
14410
14411 if (info->traditional_format
14412 || !is_elf_hash_table (info->hash))
14413 return 0;
14414
14415 o = bfd_get_section_by_name (output_bfd, ".stab");
14416 if (o != NULL)
14417 {
14418 asection *i;
14419
14420 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14421 {
14422 if (i->size == 0
14423 || i->reloc_count == 0
14424 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14425 continue;
14426
14427 abfd = i->owner;
14428 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14429 continue;
14430
14431 if (!init_reloc_cookie_for_section (&cookie, info, i))
14432 return -1;
14433
14434 if (_bfd_discard_section_stabs (abfd, i,
14435 elf_section_data (i)->sec_info,
14436 bfd_elf_reloc_symbol_deleted_p,
14437 &cookie))
14438 changed = 1;
14439
14440 fini_reloc_cookie_for_section (&cookie, i);
14441 }
14442 }
14443
14444 o = NULL;
14445 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14446 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14447 if (o != NULL)
14448 {
14449 asection *i;
14450 int eh_changed = 0;
14451 unsigned int eh_alignment; /* Octets. */
14452
14453 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14454 {
14455 if (i->size == 0)
14456 continue;
14457
14458 abfd = i->owner;
14459 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14460 continue;
14461
14462 if (!init_reloc_cookie_for_section (&cookie, info, i))
14463 return -1;
14464
14465 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14466 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14467 bfd_elf_reloc_symbol_deleted_p,
14468 &cookie))
14469 {
14470 eh_changed = 1;
14471 if (i->size != i->rawsize)
14472 changed = 1;
14473 }
14474
14475 fini_reloc_cookie_for_section (&cookie, i);
14476 }
14477
14478 eh_alignment = ((1 << o->alignment_power)
14479 * bfd_octets_per_byte (output_bfd, o));
14480 /* Skip over zero terminator, and prevent empty sections from
14481 adding alignment padding at the end. */
14482 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14483 if (i->size == 0)
14484 i->flags |= SEC_EXCLUDE;
14485 else if (i->size > 4)
14486 break;
14487 /* The last non-empty eh_frame section doesn't need padding. */
14488 if (i != NULL)
14489 i = i->map_tail.s;
14490 /* Any prior sections must pad the last FDE out to the output
14491 section alignment. Otherwise we might have zero padding
14492 between sections, which would be seen as a terminator. */
14493 for (; i != NULL; i = i->map_tail.s)
14494 if (i->size == 4)
14495 /* All but the last zero terminator should have been removed. */
14496 BFD_FAIL ();
14497 else
14498 {
14499 bfd_size_type size
14500 = (i->size + eh_alignment - 1) & -eh_alignment;
14501 if (i->size != size)
14502 {
14503 i->size = size;
14504 changed = 1;
14505 eh_changed = 1;
14506 }
14507 }
14508 if (eh_changed)
14509 elf_link_hash_traverse (elf_hash_table (info),
14510 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14511 }
14512
14513 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14514 {
14515 const struct elf_backend_data *bed;
14516 asection *s;
14517
14518 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14519 continue;
14520 s = abfd->sections;
14521 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14522 continue;
14523
14524 bed = get_elf_backend_data (abfd);
14525
14526 if (bed->elf_backend_discard_info != NULL)
14527 {
14528 if (!init_reloc_cookie (&cookie, info, abfd))
14529 return -1;
14530
14531 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14532 changed = 1;
14533
14534 fini_reloc_cookie (&cookie, abfd);
14535 }
14536 }
14537
14538 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14539 _bfd_elf_end_eh_frame_parsing (info);
14540
14541 if (info->eh_frame_hdr_type
14542 && !bfd_link_relocatable (info)
14543 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14544 changed = 1;
14545
14546 return changed;
14547 }
14548
14549 bfd_boolean
14550 _bfd_elf_section_already_linked (bfd *abfd,
14551 asection *sec,
14552 struct bfd_link_info *info)
14553 {
14554 flagword flags;
14555 const char *name, *key;
14556 struct bfd_section_already_linked *l;
14557 struct bfd_section_already_linked_hash_entry *already_linked_list;
14558
14559 if (sec->output_section == bfd_abs_section_ptr)
14560 return FALSE;
14561
14562 flags = sec->flags;
14563
14564 /* Return if it isn't a linkonce section. A comdat group section
14565 also has SEC_LINK_ONCE set. */
14566 if ((flags & SEC_LINK_ONCE) == 0)
14567 return FALSE;
14568
14569 /* Don't put group member sections on our list of already linked
14570 sections. They are handled as a group via their group section. */
14571 if (elf_sec_group (sec) != NULL)
14572 return FALSE;
14573
14574 /* For a SHT_GROUP section, use the group signature as the key. */
14575 name = sec->name;
14576 if ((flags & SEC_GROUP) != 0
14577 && elf_next_in_group (sec) != NULL
14578 && elf_group_name (elf_next_in_group (sec)) != NULL)
14579 key = elf_group_name (elf_next_in_group (sec));
14580 else
14581 {
14582 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14583 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14584 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14585 key++;
14586 else
14587 /* Must be a user linkonce section that doesn't follow gcc's
14588 naming convention. In this case we won't be matching
14589 single member groups. */
14590 key = name;
14591 }
14592
14593 already_linked_list = bfd_section_already_linked_table_lookup (key);
14594
14595 for (l = already_linked_list->entry; l != NULL; l = l->next)
14596 {
14597 /* We may have 2 different types of sections on the list: group
14598 sections with a signature of <key> (<key> is some string),
14599 and linkonce sections named .gnu.linkonce.<type>.<key>.
14600 Match like sections. LTO plugin sections are an exception.
14601 They are always named .gnu.linkonce.t.<key> and match either
14602 type of section. */
14603 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14604 && ((flags & SEC_GROUP) != 0
14605 || strcmp (name, l->sec->name) == 0))
14606 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14607 || (sec->owner->flags & BFD_PLUGIN) != 0)
14608 {
14609 /* The section has already been linked. See if we should
14610 issue a warning. */
14611 if (!_bfd_handle_already_linked (sec, l, info))
14612 return FALSE;
14613
14614 if (flags & SEC_GROUP)
14615 {
14616 asection *first = elf_next_in_group (sec);
14617 asection *s = first;
14618
14619 while (s != NULL)
14620 {
14621 s->output_section = bfd_abs_section_ptr;
14622 /* Record which group discards it. */
14623 s->kept_section = l->sec;
14624 s = elf_next_in_group (s);
14625 /* These lists are circular. */
14626 if (s == first)
14627 break;
14628 }
14629 }
14630
14631 return TRUE;
14632 }
14633 }
14634
14635 /* A single member comdat group section may be discarded by a
14636 linkonce section and vice versa. */
14637 if ((flags & SEC_GROUP) != 0)
14638 {
14639 asection *first = elf_next_in_group (sec);
14640
14641 if (first != NULL && elf_next_in_group (first) == first)
14642 /* Check this single member group against linkonce sections. */
14643 for (l = already_linked_list->entry; l != NULL; l = l->next)
14644 if ((l->sec->flags & SEC_GROUP) == 0
14645 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14646 {
14647 first->output_section = bfd_abs_section_ptr;
14648 first->kept_section = l->sec;
14649 sec->output_section = bfd_abs_section_ptr;
14650 break;
14651 }
14652 }
14653 else
14654 /* Check this linkonce section against single member groups. */
14655 for (l = already_linked_list->entry; l != NULL; l = l->next)
14656 if (l->sec->flags & SEC_GROUP)
14657 {
14658 asection *first = elf_next_in_group (l->sec);
14659
14660 if (first != NULL
14661 && elf_next_in_group (first) == first
14662 && bfd_elf_match_symbols_in_sections (first, sec, info))
14663 {
14664 sec->output_section = bfd_abs_section_ptr;
14665 sec->kept_section = first;
14666 break;
14667 }
14668 }
14669
14670 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14671 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14672 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14673 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14674 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14675 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14676 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14677 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14678 The reverse order cannot happen as there is never a bfd with only the
14679 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14680 matter as here were are looking only for cross-bfd sections. */
14681
14682 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14683 for (l = already_linked_list->entry; l != NULL; l = l->next)
14684 if ((l->sec->flags & SEC_GROUP) == 0
14685 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14686 {
14687 if (abfd != l->sec->owner)
14688 sec->output_section = bfd_abs_section_ptr;
14689 break;
14690 }
14691
14692 /* This is the first section with this name. Record it. */
14693 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14694 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14695 return sec->output_section == bfd_abs_section_ptr;
14696 }
14697
14698 bfd_boolean
14699 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14700 {
14701 return sym->st_shndx == SHN_COMMON;
14702 }
14703
14704 unsigned int
14705 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14706 {
14707 return SHN_COMMON;
14708 }
14709
14710 asection *
14711 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14712 {
14713 return bfd_com_section_ptr;
14714 }
14715
14716 bfd_vma
14717 _bfd_elf_default_got_elt_size (bfd *abfd,
14718 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14719 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14720 bfd *ibfd ATTRIBUTE_UNUSED,
14721 unsigned long symndx ATTRIBUTE_UNUSED)
14722 {
14723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14724 return bed->s->arch_size / 8;
14725 }
14726
14727 /* Routines to support the creation of dynamic relocs. */
14728
14729 /* Returns the name of the dynamic reloc section associated with SEC. */
14730
14731 static const char *
14732 get_dynamic_reloc_section_name (bfd * abfd,
14733 asection * sec,
14734 bfd_boolean is_rela)
14735 {
14736 char *name;
14737 const char *old_name = bfd_section_name (sec);
14738 const char *prefix = is_rela ? ".rela" : ".rel";
14739
14740 if (old_name == NULL)
14741 return NULL;
14742
14743 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14744 sprintf (name, "%s%s", prefix, old_name);
14745
14746 return name;
14747 }
14748
14749 /* Returns the dynamic reloc section associated with SEC.
14750 If necessary compute the name of the dynamic reloc section based
14751 on SEC's name (looked up in ABFD's string table) and the setting
14752 of IS_RELA. */
14753
14754 asection *
14755 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14756 asection * sec,
14757 bfd_boolean is_rela)
14758 {
14759 asection * reloc_sec = elf_section_data (sec)->sreloc;
14760
14761 if (reloc_sec == NULL)
14762 {
14763 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14764
14765 if (name != NULL)
14766 {
14767 reloc_sec = bfd_get_linker_section (abfd, name);
14768
14769 if (reloc_sec != NULL)
14770 elf_section_data (sec)->sreloc = reloc_sec;
14771 }
14772 }
14773
14774 return reloc_sec;
14775 }
14776
14777 /* Returns the dynamic reloc section associated with SEC. If the
14778 section does not exist it is created and attached to the DYNOBJ
14779 bfd and stored in the SRELOC field of SEC's elf_section_data
14780 structure.
14781
14782 ALIGNMENT is the alignment for the newly created section and
14783 IS_RELA defines whether the name should be .rela.<SEC's name>
14784 or .rel.<SEC's name>. The section name is looked up in the
14785 string table associated with ABFD. */
14786
14787 asection *
14788 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14789 bfd *dynobj,
14790 unsigned int alignment,
14791 bfd *abfd,
14792 bfd_boolean is_rela)
14793 {
14794 asection * reloc_sec = elf_section_data (sec)->sreloc;
14795
14796 if (reloc_sec == NULL)
14797 {
14798 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14799
14800 if (name == NULL)
14801 return NULL;
14802
14803 reloc_sec = bfd_get_linker_section (dynobj, name);
14804
14805 if (reloc_sec == NULL)
14806 {
14807 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14808 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14809 if ((sec->flags & SEC_ALLOC) != 0)
14810 flags |= SEC_ALLOC | SEC_LOAD;
14811
14812 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14813 if (reloc_sec != NULL)
14814 {
14815 /* _bfd_elf_get_sec_type_attr chooses a section type by
14816 name. Override as it may be wrong, eg. for a user
14817 section named "auto" we'll get ".relauto" which is
14818 seen to be a .rela section. */
14819 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14820 if (!bfd_set_section_alignment (reloc_sec, alignment))
14821 reloc_sec = NULL;
14822 }
14823 }
14824
14825 elf_section_data (sec)->sreloc = reloc_sec;
14826 }
14827
14828 return reloc_sec;
14829 }
14830
14831 /* Copy the ELF symbol type and other attributes for a linker script
14832 assignment from HSRC to HDEST. Generally this should be treated as
14833 if we found a strong non-dynamic definition for HDEST (except that
14834 ld ignores multiple definition errors). */
14835 void
14836 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14837 struct bfd_link_hash_entry *hdest,
14838 struct bfd_link_hash_entry *hsrc)
14839 {
14840 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14841 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14842 Elf_Internal_Sym isym;
14843
14844 ehdest->type = ehsrc->type;
14845 ehdest->target_internal = ehsrc->target_internal;
14846
14847 isym.st_other = ehsrc->other;
14848 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14849 }
14850
14851 /* Append a RELA relocation REL to section S in BFD. */
14852
14853 void
14854 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14855 {
14856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14857 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14858 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14859 bed->s->swap_reloca_out (abfd, rel, loc);
14860 }
14861
14862 /* Append a REL relocation REL to section S in BFD. */
14863
14864 void
14865 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14866 {
14867 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14868 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14869 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14870 bed->s->swap_reloc_out (abfd, rel, loc);
14871 }
14872
14873 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14874
14875 struct bfd_link_hash_entry *
14876 bfd_elf_define_start_stop (struct bfd_link_info *info,
14877 const char *symbol, asection *sec)
14878 {
14879 struct elf_link_hash_entry *h;
14880
14881 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14882 FALSE, FALSE, TRUE);
14883 /* NB: Common symbols will be turned into definition later. */
14884 if (h != NULL
14885 && (h->root.type == bfd_link_hash_undefined
14886 || h->root.type == bfd_link_hash_undefweak
14887 || ((h->ref_regular || h->def_dynamic)
14888 && !h->def_regular
14889 && h->root.type != bfd_link_hash_common)))
14890 {
14891 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14892 h->verinfo.verdef = NULL;
14893 h->root.type = bfd_link_hash_defined;
14894 h->root.u.def.section = sec;
14895 h->root.u.def.value = 0;
14896 h->def_regular = 1;
14897 h->def_dynamic = 0;
14898 h->start_stop = 1;
14899 h->u2.start_stop_section = sec;
14900 if (symbol[0] == '.')
14901 {
14902 /* .startof. and .sizeof. symbols are local. */
14903 const struct elf_backend_data *bed;
14904 bed = get_elf_backend_data (info->output_bfd);
14905 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14906 }
14907 else
14908 {
14909 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14910 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
14911 | info->start_stop_visibility);
14912 if (was_dynamic)
14913 bfd_elf_link_record_dynamic_symbol (info, h);
14914 }
14915 return &h->root;
14916 }
14917 return NULL;
14918 }
14919
14920 /* Find dynamic relocs for H that apply to read-only sections. */
14921
14922 asection *
14923 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
14924 {
14925 struct elf_dyn_relocs *p;
14926
14927 for (p = h->dyn_relocs; p != NULL; p = p->next)
14928 {
14929 asection *s = p->sec->output_section;
14930
14931 if (s != NULL && (s->flags & SEC_READONLY) != 0)
14932 return p->sec;
14933 }
14934 return NULL;
14935 }
14936
14937 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
14938 read-only sections. */
14939
14940 bfd_boolean
14941 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
14942 {
14943 asection *sec;
14944
14945 if (h->root.type == bfd_link_hash_indirect)
14946 return TRUE;
14947
14948 sec = _bfd_elf_readonly_dynrelocs (h);
14949 if (sec != NULL)
14950 {
14951 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14952
14953 info->flags |= DF_TEXTREL;
14954 /* xgettext:c-format */
14955 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
14956 "in read-only section `%pA'\n"),
14957 sec->owner, h->root.root.string, sec);
14958
14959 if (bfd_link_textrel_check (info))
14960 /* xgettext:c-format */
14961 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
14962 "in read-only section `%pA'\n"),
14963 sec->owner, h->root.root.string, sec);
14964
14965 /* Not an error, just cut short the traversal. */
14966 return FALSE;
14967 }
14968 return TRUE;
14969 }
14970
14971 /* Add dynamic tags. */
14972
14973 bfd_boolean
14974 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
14975 bfd_boolean need_dynamic_reloc)
14976 {
14977 struct elf_link_hash_table *htab = elf_hash_table (info);
14978
14979 if (htab->dynamic_sections_created)
14980 {
14981 /* Add some entries to the .dynamic section. We fill in the
14982 values later, in finish_dynamic_sections, but we must add
14983 the entries now so that we get the correct size for the
14984 .dynamic section. The DT_DEBUG entry is filled in by the
14985 dynamic linker and used by the debugger. */
14986 #define add_dynamic_entry(TAG, VAL) \
14987 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14988
14989 const struct elf_backend_data *bed
14990 = get_elf_backend_data (output_bfd);
14991
14992 if (bfd_link_executable (info))
14993 {
14994 if (!add_dynamic_entry (DT_DEBUG, 0))
14995 return FALSE;
14996 }
14997
14998 if (htab->dt_pltgot_required || htab->splt->size != 0)
14999 {
15000 /* DT_PLTGOT is used by prelink even if there is no PLT
15001 relocation. */
15002 if (!add_dynamic_entry (DT_PLTGOT, 0))
15003 return FALSE;
15004 }
15005
15006 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15007 {
15008 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15009 || !add_dynamic_entry (DT_PLTREL,
15010 (bed->rela_plts_and_copies_p
15011 ? DT_RELA : DT_REL))
15012 || !add_dynamic_entry (DT_JMPREL, 0))
15013 return FALSE;
15014 }
15015
15016 if (htab->tlsdesc_plt
15017 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15018 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15019 return FALSE;
15020
15021 if (need_dynamic_reloc)
15022 {
15023 if (bed->rela_plts_and_copies_p)
15024 {
15025 if (!add_dynamic_entry (DT_RELA, 0)
15026 || !add_dynamic_entry (DT_RELASZ, 0)
15027 || !add_dynamic_entry (DT_RELAENT,
15028 bed->s->sizeof_rela))
15029 return FALSE;
15030 }
15031 else
15032 {
15033 if (!add_dynamic_entry (DT_REL, 0)
15034 || !add_dynamic_entry (DT_RELSZ, 0)
15035 || !add_dynamic_entry (DT_RELENT,
15036 bed->s->sizeof_rel))
15037 return FALSE;
15038 }
15039
15040 /* If any dynamic relocs apply to a read-only section,
15041 then we need a DT_TEXTREL entry. */
15042 if ((info->flags & DF_TEXTREL) == 0)
15043 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15044 info);
15045
15046 if ((info->flags & DF_TEXTREL) != 0)
15047 {
15048 if (htab->ifunc_resolvers)
15049 info->callbacks->einfo
15050 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15051 "may result in a segfault at runtime; recompile with %s\n"),
15052 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15053
15054 if (!add_dynamic_entry (DT_TEXTREL, 0))
15055 return FALSE;
15056 }
15057 }
15058 }
15059 #undef add_dynamic_entry
15060
15061 return TRUE;
15062 }
This page took 0.36795 seconds and 4 git commands to generate.