Re: PR27311, ld.bfd (symbol from plugin): undefined reference
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 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 || ((h->root.type == bfd_link_hash_defined
539 || h->root.type == bfd_link_hash_defweak)
540 && h->root.u.def.section->owner != NULL
541 && h->root.u.def.section->owner->no_export)
542 || (h->root.type == bfd_link_hash_common
543 && h->root.u.c.p->section->owner != NULL
544 && h->root.u.c.p->section->owner->no_export))
545 return TRUE;
546 }
547
548 default:
549 break;
550 }
551
552 h->dynindx = elf_hash_table (info)->dynsymcount;
553 ++elf_hash_table (info)->dynsymcount;
554
555 dynstr = elf_hash_table (info)->dynstr;
556 if (dynstr == NULL)
557 {
558 /* Create a strtab to hold the dynamic symbol names. */
559 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
560 if (dynstr == NULL)
561 return FALSE;
562 }
563
564 /* We don't put any version information in the dynamic string
565 table. */
566 name = h->root.root.string;
567 p = strchr (name, ELF_VER_CHR);
568 if (p != NULL)
569 /* We know that the p points into writable memory. In fact,
570 there are only a few symbols that have read-only names, being
571 those like _GLOBAL_OFFSET_TABLE_ that are created specially
572 by the backends. Most symbols will have names pointing into
573 an ELF string table read from a file, or to objalloc memory. */
574 *p = 0;
575
576 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
577
578 if (p != NULL)
579 *p = ELF_VER_CHR;
580
581 if (indx == (size_t) -1)
582 return FALSE;
583 h->dynstr_index = indx;
584 }
585
586 return TRUE;
587 }
588 \f
589 /* Mark a symbol dynamic. */
590
591 static void
592 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
593 struct elf_link_hash_entry *h,
594 Elf_Internal_Sym *sym)
595 {
596 struct bfd_elf_dynamic_list *d = info->dynamic_list;
597
598 /* It may be called more than once on the same H. */
599 if(h->dynamic || bfd_link_relocatable (info))
600 return;
601
602 if ((info->dynamic_data
603 && (h->type == STT_OBJECT
604 || h->type == STT_COMMON
605 || (sym != NULL
606 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
607 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
608 || (d != NULL
609 && h->non_elf
610 && (*d->match) (&d->head, NULL, h->root.root.string)))
611 {
612 h->dynamic = 1;
613 /* NB: If a symbol is made dynamic by --dynamic-list, it has
614 non-IR reference. */
615 h->root.non_ir_ref_dynamic = 1;
616 }
617 }
618
619 /* Record an assignment to a symbol made by a linker script. We need
620 this in case some dynamic object refers to this symbol. */
621
622 bfd_boolean
623 bfd_elf_record_link_assignment (bfd *output_bfd,
624 struct bfd_link_info *info,
625 const char *name,
626 bfd_boolean provide,
627 bfd_boolean hidden)
628 {
629 struct elf_link_hash_entry *h, *hv;
630 struct elf_link_hash_table *htab;
631 const struct elf_backend_data *bed;
632
633 if (!is_elf_hash_table (info->hash))
634 return TRUE;
635
636 htab = elf_hash_table (info);
637 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
638 if (h == NULL)
639 return provide;
640
641 if (h->root.type == bfd_link_hash_warning)
642 h = (struct elf_link_hash_entry *) h->root.u.i.link;
643
644 if (h->versioned == unknown)
645 {
646 /* Set versioned if symbol version is unknown. */
647 char *version = strrchr (name, ELF_VER_CHR);
648 if (version)
649 {
650 if (version > name && version[-1] != ELF_VER_CHR)
651 h->versioned = versioned_hidden;
652 else
653 h->versioned = versioned;
654 }
655 }
656
657 /* Symbols defined in a linker script but not referenced anywhere
658 else will have non_elf set. */
659 if (h->non_elf)
660 {
661 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
662 h->non_elf = 0;
663 }
664
665 switch (h->root.type)
666 {
667 case bfd_link_hash_defined:
668 case bfd_link_hash_defweak:
669 case bfd_link_hash_common:
670 break;
671 case bfd_link_hash_undefweak:
672 case bfd_link_hash_undefined:
673 /* Since we're defining the symbol, don't let it seem to have not
674 been defined. record_dynamic_symbol and size_dynamic_sections
675 may depend on this. */
676 h->root.type = bfd_link_hash_new;
677 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
678 bfd_link_repair_undef_list (&htab->root);
679 break;
680 case bfd_link_hash_new:
681 break;
682 case bfd_link_hash_indirect:
683 /* We had a versioned symbol in a dynamic library. We make the
684 the versioned symbol point to this one. */
685 bed = get_elf_backend_data (output_bfd);
686 hv = h;
687 while (hv->root.type == bfd_link_hash_indirect
688 || hv->root.type == bfd_link_hash_warning)
689 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
690 /* We don't need to update h->root.u since linker will set them
691 later. */
692 h->root.type = bfd_link_hash_undefined;
693 hv->root.type = bfd_link_hash_indirect;
694 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
695 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
696 break;
697 default:
698 BFD_FAIL ();
699 return FALSE;
700 }
701
702 /* If this symbol is being provided by the linker script, and it is
703 currently defined by a dynamic object, but not by a regular
704 object, then mark it as undefined so that the generic linker will
705 force the correct value. */
706 if (provide
707 && h->def_dynamic
708 && !h->def_regular)
709 h->root.type = bfd_link_hash_undefined;
710
711 /* If this symbol is currently defined by a dynamic object, but not
712 by a regular object, then clear out any version information because
713 the symbol will not be associated with the dynamic object any
714 more. */
715 if (h->def_dynamic && !h->def_regular)
716 h->verinfo.verdef = NULL;
717
718 /* Make sure this symbol is not garbage collected. */
719 h->mark = 1;
720
721 h->def_regular = 1;
722
723 if (hidden)
724 {
725 bed = get_elf_backend_data (output_bfd);
726 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
727 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
728 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
729 }
730
731 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
732 and executables. */
733 if (!bfd_link_relocatable (info)
734 && h->dynindx != -1
735 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
736 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
737 h->forced_local = 1;
738
739 if ((h->def_dynamic
740 || h->ref_dynamic
741 || bfd_link_dll (info)
742 || elf_hash_table (info)->is_relocatable_executable)
743 && !h->forced_local
744 && h->dynindx == -1)
745 {
746 if (! bfd_elf_link_record_dynamic_symbol (info, h))
747 return FALSE;
748
749 /* If this is a weak defined symbol, and we know a corresponding
750 real symbol from the same dynamic object, make sure the real
751 symbol is also made into a dynamic symbol. */
752 if (h->is_weakalias)
753 {
754 struct elf_link_hash_entry *def = weakdef (h);
755
756 if (def->dynindx == -1
757 && !bfd_elf_link_record_dynamic_symbol (info, def))
758 return FALSE;
759 }
760 }
761
762 return TRUE;
763 }
764
765 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
766 success, and 2 on a failure caused by attempting to record a symbol
767 in a discarded section, eg. a discarded link-once section symbol. */
768
769 int
770 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
771 bfd *input_bfd,
772 long input_indx)
773 {
774 size_t amt;
775 struct elf_link_local_dynamic_entry *entry;
776 struct elf_link_hash_table *eht;
777 struct elf_strtab_hash *dynstr;
778 size_t dynstr_index;
779 char *name;
780 Elf_External_Sym_Shndx eshndx;
781 char esym[sizeof (Elf64_External_Sym)];
782
783 if (! is_elf_hash_table (info->hash))
784 return 0;
785
786 /* See if the entry exists already. */
787 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
788 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
789 return 1;
790
791 amt = sizeof (*entry);
792 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
793 if (entry == NULL)
794 return 0;
795
796 /* Go find the symbol, so that we can find it's name. */
797 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
798 1, input_indx, &entry->isym, esym, &eshndx))
799 {
800 bfd_release (input_bfd, entry);
801 return 0;
802 }
803
804 if (entry->isym.st_shndx != SHN_UNDEF
805 && entry->isym.st_shndx < SHN_LORESERVE)
806 {
807 asection *s;
808
809 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
810 if (s == NULL || bfd_is_abs_section (s->output_section))
811 {
812 /* We can still bfd_release here as nothing has done another
813 bfd_alloc. We can't do this later in this function. */
814 bfd_release (input_bfd, entry);
815 return 2;
816 }
817 }
818
819 name = (bfd_elf_string_from_elf_section
820 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
821 entry->isym.st_name));
822
823 dynstr = elf_hash_table (info)->dynstr;
824 if (dynstr == NULL)
825 {
826 /* Create a strtab to hold the dynamic symbol names. */
827 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
828 if (dynstr == NULL)
829 return 0;
830 }
831
832 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
833 if (dynstr_index == (size_t) -1)
834 return 0;
835 entry->isym.st_name = dynstr_index;
836
837 eht = elf_hash_table (info);
838
839 entry->next = eht->dynlocal;
840 eht->dynlocal = entry;
841 entry->input_bfd = input_bfd;
842 entry->input_indx = input_indx;
843 eht->dynsymcount++;
844
845 /* Whatever binding the symbol had before, it's now local. */
846 entry->isym.st_info
847 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
848
849 /* The dynindx will be set at the end of size_dynamic_sections. */
850
851 return 1;
852 }
853
854 /* Return the dynindex of a local dynamic symbol. */
855
856 long
857 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
858 bfd *input_bfd,
859 long input_indx)
860 {
861 struct elf_link_local_dynamic_entry *e;
862
863 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
864 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
865 return e->dynindx;
866 return -1;
867 }
868
869 /* This function is used to renumber the dynamic symbols, if some of
870 them are removed because they are marked as local. This is called
871 via elf_link_hash_traverse. */
872
873 static bfd_boolean
874 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
875 void *data)
876 {
877 size_t *count = (size_t *) data;
878
879 if (h->forced_local)
880 return TRUE;
881
882 if (h->dynindx != -1)
883 h->dynindx = ++(*count);
884
885 return TRUE;
886 }
887
888
889 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
890 STB_LOCAL binding. */
891
892 static bfd_boolean
893 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
894 void *data)
895 {
896 size_t *count = (size_t *) data;
897
898 if (!h->forced_local)
899 return TRUE;
900
901 if (h->dynindx != -1)
902 h->dynindx = ++(*count);
903
904 return TRUE;
905 }
906
907 /* Return true if the dynamic symbol for a given section should be
908 omitted when creating a shared library. */
909 bfd_boolean
910 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
911 struct bfd_link_info *info,
912 asection *p)
913 {
914 struct elf_link_hash_table *htab;
915 asection *ip;
916
917 switch (elf_section_data (p)->this_hdr.sh_type)
918 {
919 case SHT_PROGBITS:
920 case SHT_NOBITS:
921 /* If sh_type is yet undecided, assume it could be
922 SHT_PROGBITS/SHT_NOBITS. */
923 case SHT_NULL:
924 htab = elf_hash_table (info);
925 if (htab->text_index_section != NULL)
926 return p != htab->text_index_section && p != htab->data_index_section;
927
928 return (htab->dynobj != NULL
929 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
930 && ip->output_section == p);
931
932 /* There shouldn't be section relative relocations
933 against any other section. */
934 default:
935 return TRUE;
936 }
937 }
938
939 bfd_boolean
940 _bfd_elf_omit_section_dynsym_all
941 (bfd *output_bfd ATTRIBUTE_UNUSED,
942 struct bfd_link_info *info ATTRIBUTE_UNUSED,
943 asection *p ATTRIBUTE_UNUSED)
944 {
945 return TRUE;
946 }
947
948 /* Assign dynsym indices. In a shared library we generate a section
949 symbol for each output section, which come first. Next come symbols
950 which have been forced to local binding. Then all of the back-end
951 allocated local dynamic syms, followed by the rest of the global
952 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
953 (This prevents the early call before elf_backend_init_index_section
954 and strip_excluded_output_sections setting dynindx for sections
955 that are stripped.) */
956
957 static unsigned long
958 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
959 struct bfd_link_info *info,
960 unsigned long *section_sym_count)
961 {
962 unsigned long dynsymcount = 0;
963 bfd_boolean do_sec = section_sym_count != NULL;
964
965 if (bfd_link_pic (info)
966 || elf_hash_table (info)->is_relocatable_executable)
967 {
968 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
969 asection *p;
970 for (p = output_bfd->sections; p ; p = p->next)
971 if ((p->flags & SEC_EXCLUDE) == 0
972 && (p->flags & SEC_ALLOC) != 0
973 && elf_hash_table (info)->dynamic_relocs
974 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
975 {
976 ++dynsymcount;
977 if (do_sec)
978 elf_section_data (p)->dynindx = dynsymcount;
979 }
980 else if (do_sec)
981 elf_section_data (p)->dynindx = 0;
982 }
983 if (do_sec)
984 *section_sym_count = dynsymcount;
985
986 elf_link_hash_traverse (elf_hash_table (info),
987 elf_link_renumber_local_hash_table_dynsyms,
988 &dynsymcount);
989
990 if (elf_hash_table (info)->dynlocal)
991 {
992 struct elf_link_local_dynamic_entry *p;
993 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
994 p->dynindx = ++dynsymcount;
995 }
996 elf_hash_table (info)->local_dynsymcount = dynsymcount;
997
998 elf_link_hash_traverse (elf_hash_table (info),
999 elf_link_renumber_hash_table_dynsyms,
1000 &dynsymcount);
1001
1002 /* There is an unused NULL entry at the head of the table which we
1003 must account for in our count even if the table is empty since it
1004 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1005 .dynamic section. */
1006 dynsymcount++;
1007
1008 elf_hash_table (info)->dynsymcount = dynsymcount;
1009 return dynsymcount;
1010 }
1011
1012 /* Merge st_other field. */
1013
1014 static void
1015 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1016 unsigned int st_other, asection *sec,
1017 bfd_boolean definition, bfd_boolean dynamic)
1018 {
1019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1020
1021 /* If st_other has a processor-specific meaning, specific
1022 code might be needed here. */
1023 if (bed->elf_backend_merge_symbol_attribute)
1024 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1025 dynamic);
1026
1027 if (!dynamic)
1028 {
1029 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1030 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1031
1032 /* Keep the most constraining visibility. Leave the remainder
1033 of the st_other field to elf_backend_merge_symbol_attribute. */
1034 if (symvis - 1 < hvis - 1)
1035 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1036 }
1037 else if (definition
1038 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1039 && (sec->flags & SEC_READONLY) == 0)
1040 h->protected_def = 1;
1041 }
1042
1043 /* This function is called when we want to merge a new symbol with an
1044 existing symbol. It handles the various cases which arise when we
1045 find a definition in a dynamic object, or when there is already a
1046 definition in a dynamic object. The new symbol is described by
1047 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1048 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1049 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1050 of an old common symbol. We set OVERRIDE if the old symbol is
1051 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1052 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1053 to change. By OK to change, we mean that we shouldn't warn if the
1054 type or size does change. */
1055
1056 static bfd_boolean
1057 _bfd_elf_merge_symbol (bfd *abfd,
1058 struct bfd_link_info *info,
1059 const char *name,
1060 Elf_Internal_Sym *sym,
1061 asection **psec,
1062 bfd_vma *pvalue,
1063 struct elf_link_hash_entry **sym_hash,
1064 bfd **poldbfd,
1065 bfd_boolean *pold_weak,
1066 unsigned int *pold_alignment,
1067 bfd_boolean *skip,
1068 bfd **override,
1069 bfd_boolean *type_change_ok,
1070 bfd_boolean *size_change_ok,
1071 bfd_boolean *matched)
1072 {
1073 asection *sec, *oldsec;
1074 struct elf_link_hash_entry *h;
1075 struct elf_link_hash_entry *hi;
1076 struct elf_link_hash_entry *flip;
1077 int bind;
1078 bfd *oldbfd;
1079 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1080 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1081 const struct elf_backend_data *bed;
1082 char *new_version;
1083 bfd_boolean default_sym = *matched;
1084
1085 *skip = FALSE;
1086 *override = NULL;
1087
1088 sec = *psec;
1089 bind = ELF_ST_BIND (sym->st_info);
1090
1091 if (! bfd_is_und_section (sec))
1092 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1093 else
1094 h = ((struct elf_link_hash_entry *)
1095 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1096 if (h == NULL)
1097 return FALSE;
1098 *sym_hash = h;
1099
1100 bed = get_elf_backend_data (abfd);
1101
1102 /* NEW_VERSION is the symbol version of the new symbol. */
1103 if (h->versioned != unversioned)
1104 {
1105 /* Symbol version is unknown or versioned. */
1106 new_version = strrchr (name, ELF_VER_CHR);
1107 if (new_version)
1108 {
1109 if (h->versioned == unknown)
1110 {
1111 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1112 h->versioned = versioned_hidden;
1113 else
1114 h->versioned = versioned;
1115 }
1116 new_version += 1;
1117 if (new_version[0] == '\0')
1118 new_version = NULL;
1119 }
1120 else
1121 h->versioned = unversioned;
1122 }
1123 else
1124 new_version = NULL;
1125
1126 /* For merging, we only care about real symbols. But we need to make
1127 sure that indirect symbol dynamic flags are updated. */
1128 hi = h;
1129 while (h->root.type == bfd_link_hash_indirect
1130 || h->root.type == bfd_link_hash_warning)
1131 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1132
1133 if (!*matched)
1134 {
1135 if (hi == h || h->root.type == bfd_link_hash_new)
1136 *matched = TRUE;
1137 else
1138 {
1139 /* OLD_HIDDEN is true if the existing symbol is only visible
1140 to the symbol with the same symbol version. NEW_HIDDEN is
1141 true if the new symbol is only visible to the symbol with
1142 the same symbol version. */
1143 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1144 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1145 if (!old_hidden && !new_hidden)
1146 /* The new symbol matches the existing symbol if both
1147 aren't hidden. */
1148 *matched = TRUE;
1149 else
1150 {
1151 /* OLD_VERSION is the symbol version of the existing
1152 symbol. */
1153 char *old_version;
1154
1155 if (h->versioned >= versioned)
1156 old_version = strrchr (h->root.root.string,
1157 ELF_VER_CHR) + 1;
1158 else
1159 old_version = NULL;
1160
1161 /* The new symbol matches the existing symbol if they
1162 have the same symbol version. */
1163 *matched = (old_version == new_version
1164 || (old_version != NULL
1165 && new_version != NULL
1166 && strcmp (old_version, new_version) == 0));
1167 }
1168 }
1169 }
1170
1171 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1172 existing symbol. */
1173
1174 oldbfd = NULL;
1175 oldsec = NULL;
1176 switch (h->root.type)
1177 {
1178 default:
1179 break;
1180
1181 case bfd_link_hash_undefined:
1182 case bfd_link_hash_undefweak:
1183 oldbfd = h->root.u.undef.abfd;
1184 break;
1185
1186 case bfd_link_hash_defined:
1187 case bfd_link_hash_defweak:
1188 oldbfd = h->root.u.def.section->owner;
1189 oldsec = h->root.u.def.section;
1190 break;
1191
1192 case bfd_link_hash_common:
1193 oldbfd = h->root.u.c.p->section->owner;
1194 oldsec = h->root.u.c.p->section;
1195 if (pold_alignment)
1196 *pold_alignment = h->root.u.c.p->alignment_power;
1197 break;
1198 }
1199 if (poldbfd && *poldbfd == NULL)
1200 *poldbfd = oldbfd;
1201
1202 /* Differentiate strong and weak symbols. */
1203 newweak = bind == STB_WEAK;
1204 oldweak = (h->root.type == bfd_link_hash_defweak
1205 || h->root.type == bfd_link_hash_undefweak);
1206 if (pold_weak)
1207 *pold_weak = oldweak;
1208
1209 /* We have to check it for every instance since the first few may be
1210 references and not all compilers emit symbol type for undefined
1211 symbols. */
1212 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1213
1214 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1215 respectively, is from a dynamic object. */
1216
1217 newdyn = (abfd->flags & DYNAMIC) != 0;
1218
1219 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1220 syms and defined syms in dynamic libraries respectively.
1221 ref_dynamic on the other hand can be set for a symbol defined in
1222 a dynamic library, and def_dynamic may not be set; When the
1223 definition in a dynamic lib is overridden by a definition in the
1224 executable use of the symbol in the dynamic lib becomes a
1225 reference to the executable symbol. */
1226 if (newdyn)
1227 {
1228 if (bfd_is_und_section (sec))
1229 {
1230 if (bind != STB_WEAK)
1231 {
1232 h->ref_dynamic_nonweak = 1;
1233 hi->ref_dynamic_nonweak = 1;
1234 }
1235 }
1236 else
1237 {
1238 /* Update the existing symbol only if they match. */
1239 if (*matched)
1240 h->dynamic_def = 1;
1241 hi->dynamic_def = 1;
1242 }
1243 }
1244
1245 /* If we just created the symbol, mark it as being an ELF symbol.
1246 Other than that, there is nothing to do--there is no merge issue
1247 with a newly defined symbol--so we just return. */
1248
1249 if (h->root.type == bfd_link_hash_new)
1250 {
1251 h->non_elf = 0;
1252 return TRUE;
1253 }
1254
1255 /* In cases involving weak versioned symbols, we may wind up trying
1256 to merge a symbol with itself. Catch that here, to avoid the
1257 confusion that results if we try to override a symbol with
1258 itself. The additional tests catch cases like
1259 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1260 dynamic object, which we do want to handle here. */
1261 if (abfd == oldbfd
1262 && (newweak || oldweak)
1263 && ((abfd->flags & DYNAMIC) == 0
1264 || !h->def_regular))
1265 return TRUE;
1266
1267 olddyn = FALSE;
1268 if (oldbfd != NULL)
1269 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1270 else if (oldsec != NULL)
1271 {
1272 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1273 indices used by MIPS ELF. */
1274 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1275 }
1276
1277 /* Handle a case where plugin_notice won't be called and thus won't
1278 set the non_ir_ref flags on the first pass over symbols. */
1279 if (oldbfd != NULL
1280 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1281 && newdyn != olddyn)
1282 {
1283 h->root.non_ir_ref_dynamic = TRUE;
1284 hi->root.non_ir_ref_dynamic = TRUE;
1285 }
1286
1287 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1288 respectively, appear to be a definition rather than reference. */
1289
1290 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1291
1292 olddef = (h->root.type != bfd_link_hash_undefined
1293 && h->root.type != bfd_link_hash_undefweak
1294 && h->root.type != bfd_link_hash_common);
1295
1296 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1297 respectively, appear to be a function. */
1298
1299 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1300 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1301
1302 oldfunc = (h->type != STT_NOTYPE
1303 && bed->is_function_type (h->type));
1304
1305 if (!(newfunc && oldfunc)
1306 && ELF_ST_TYPE (sym->st_info) != h->type
1307 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1308 && h->type != STT_NOTYPE
1309 && (newdef || bfd_is_com_section (sec))
1310 && (olddef || h->root.type == bfd_link_hash_common))
1311 {
1312 /* If creating a default indirect symbol ("foo" or "foo@") from
1313 a dynamic versioned definition ("foo@@") skip doing so if
1314 there is an existing regular definition with a different
1315 type. We don't want, for example, a "time" variable in the
1316 executable overriding a "time" function in a shared library. */
1317 if (newdyn
1318 && !olddyn)
1319 {
1320 *skip = TRUE;
1321 return TRUE;
1322 }
1323
1324 /* When adding a symbol from a regular object file after we have
1325 created indirect symbols, undo the indirection and any
1326 dynamic state. */
1327 if (hi != h
1328 && !newdyn
1329 && olddyn)
1330 {
1331 h = hi;
1332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1333 h->forced_local = 0;
1334 h->ref_dynamic = 0;
1335 h->def_dynamic = 0;
1336 h->dynamic_def = 0;
1337 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1338 {
1339 h->root.type = bfd_link_hash_undefined;
1340 h->root.u.undef.abfd = abfd;
1341 }
1342 else
1343 {
1344 h->root.type = bfd_link_hash_new;
1345 h->root.u.undef.abfd = NULL;
1346 }
1347 return TRUE;
1348 }
1349 }
1350
1351 /* Check TLS symbols. We don't check undefined symbols introduced
1352 by "ld -u" which have no type (and oldbfd NULL), and we don't
1353 check symbols from plugins because they also have no type. */
1354 if (oldbfd != NULL
1355 && (oldbfd->flags & BFD_PLUGIN) == 0
1356 && (abfd->flags & BFD_PLUGIN) == 0
1357 && ELF_ST_TYPE (sym->st_info) != h->type
1358 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1359 {
1360 bfd *ntbfd, *tbfd;
1361 bfd_boolean ntdef, tdef;
1362 asection *ntsec, *tsec;
1363
1364 if (h->type == STT_TLS)
1365 {
1366 ntbfd = abfd;
1367 ntsec = sec;
1368 ntdef = newdef;
1369 tbfd = oldbfd;
1370 tsec = oldsec;
1371 tdef = olddef;
1372 }
1373 else
1374 {
1375 ntbfd = oldbfd;
1376 ntsec = oldsec;
1377 ntdef = olddef;
1378 tbfd = abfd;
1379 tsec = sec;
1380 tdef = newdef;
1381 }
1382
1383 if (tdef && ntdef)
1384 _bfd_error_handler
1385 /* xgettext:c-format */
1386 (_("%s: TLS definition in %pB section %pA "
1387 "mismatches non-TLS definition in %pB section %pA"),
1388 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1389 else if (!tdef && !ntdef)
1390 _bfd_error_handler
1391 /* xgettext:c-format */
1392 (_("%s: TLS reference in %pB "
1393 "mismatches non-TLS reference in %pB"),
1394 h->root.root.string, tbfd, ntbfd);
1395 else if (tdef)
1396 _bfd_error_handler
1397 /* xgettext:c-format */
1398 (_("%s: TLS definition in %pB section %pA "
1399 "mismatches non-TLS reference in %pB"),
1400 h->root.root.string, tbfd, tsec, ntbfd);
1401 else
1402 _bfd_error_handler
1403 /* xgettext:c-format */
1404 (_("%s: TLS reference in %pB "
1405 "mismatches non-TLS definition in %pB section %pA"),
1406 h->root.root.string, tbfd, ntbfd, ntsec);
1407
1408 bfd_set_error (bfd_error_bad_value);
1409 return FALSE;
1410 }
1411
1412 /* If the old symbol has non-default visibility, we ignore the new
1413 definition from a dynamic object. */
1414 if (newdyn
1415 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1416 && !bfd_is_und_section (sec))
1417 {
1418 *skip = TRUE;
1419 /* Make sure this symbol is dynamic. */
1420 h->ref_dynamic = 1;
1421 hi->ref_dynamic = 1;
1422 /* A protected symbol has external availability. Make sure it is
1423 recorded as dynamic.
1424
1425 FIXME: Should we check type and size for protected symbol? */
1426 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1427 return bfd_elf_link_record_dynamic_symbol (info, h);
1428 else
1429 return TRUE;
1430 }
1431 else if (!newdyn
1432 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1433 && h->def_dynamic)
1434 {
1435 /* If the new symbol with non-default visibility comes from a
1436 relocatable file and the old definition comes from a dynamic
1437 object, we remove the old definition. */
1438 if (hi->root.type == bfd_link_hash_indirect)
1439 {
1440 /* Handle the case where the old dynamic definition is
1441 default versioned. We need to copy the symbol info from
1442 the symbol with default version to the normal one if it
1443 was referenced before. */
1444 if (h->ref_regular)
1445 {
1446 hi->root.type = h->root.type;
1447 h->root.type = bfd_link_hash_indirect;
1448 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1449
1450 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1451 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1452 {
1453 /* If the new symbol is hidden or internal, completely undo
1454 any dynamic link state. */
1455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1456 h->forced_local = 0;
1457 h->ref_dynamic = 0;
1458 }
1459 else
1460 h->ref_dynamic = 1;
1461
1462 h->def_dynamic = 0;
1463 /* FIXME: Should we check type and size for protected symbol? */
1464 h->size = 0;
1465 h->type = 0;
1466
1467 h = hi;
1468 }
1469 else
1470 h = hi;
1471 }
1472
1473 /* If the old symbol was undefined before, then it will still be
1474 on the undefs list. If the new symbol is undefined or
1475 common, we can't make it bfd_link_hash_new here, because new
1476 undefined or common symbols will be added to the undefs list
1477 by _bfd_generic_link_add_one_symbol. Symbols may not be
1478 added twice to the undefs list. Also, if the new symbol is
1479 undefweak then we don't want to lose the strong undef. */
1480 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1481 {
1482 h->root.type = bfd_link_hash_undefined;
1483 h->root.u.undef.abfd = abfd;
1484 }
1485 else
1486 {
1487 h->root.type = bfd_link_hash_new;
1488 h->root.u.undef.abfd = NULL;
1489 }
1490
1491 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1492 {
1493 /* If the new symbol is hidden or internal, completely undo
1494 any dynamic link state. */
1495 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1496 h->forced_local = 0;
1497 h->ref_dynamic = 0;
1498 }
1499 else
1500 h->ref_dynamic = 1;
1501 h->def_dynamic = 0;
1502 /* FIXME: Should we check type and size for protected symbol? */
1503 h->size = 0;
1504 h->type = 0;
1505 return TRUE;
1506 }
1507
1508 /* If a new weak symbol definition comes from a regular file and the
1509 old symbol comes from a dynamic library, we treat the new one as
1510 strong. Similarly, an old weak symbol definition from a regular
1511 file is treated as strong when the new symbol comes from a dynamic
1512 library. Further, an old weak symbol from a dynamic library is
1513 treated as strong if the new symbol is from a dynamic library.
1514 This reflects the way glibc's ld.so works.
1515
1516 Also allow a weak symbol to override a linker script symbol
1517 defined by an early pass over the script. This is done so the
1518 linker knows the symbol is defined in an object file, for the
1519 DEFINED script function.
1520
1521 Do this before setting *type_change_ok or *size_change_ok so that
1522 we warn properly when dynamic library symbols are overridden. */
1523
1524 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1525 newweak = FALSE;
1526 if (olddef && newdyn)
1527 oldweak = FALSE;
1528
1529 /* Allow changes between different types of function symbol. */
1530 if (newfunc && oldfunc)
1531 *type_change_ok = TRUE;
1532
1533 /* It's OK to change the type if either the existing symbol or the
1534 new symbol is weak. A type change is also OK if the old symbol
1535 is undefined and the new symbol is defined. */
1536
1537 if (oldweak
1538 || newweak
1539 || (newdef
1540 && h->root.type == bfd_link_hash_undefined))
1541 *type_change_ok = TRUE;
1542
1543 /* It's OK to change the size if either the existing symbol or the
1544 new symbol is weak, or if the old symbol is undefined. */
1545
1546 if (*type_change_ok
1547 || h->root.type == bfd_link_hash_undefined)
1548 *size_change_ok = TRUE;
1549
1550 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1551 symbol, respectively, appears to be a common symbol in a dynamic
1552 object. If a symbol appears in an uninitialized section, and is
1553 not weak, and is not a function, then it may be a common symbol
1554 which was resolved when the dynamic object was created. We want
1555 to treat such symbols specially, because they raise special
1556 considerations when setting the symbol size: if the symbol
1557 appears as a common symbol in a regular object, and the size in
1558 the regular object is larger, we must make sure that we use the
1559 larger size. This problematic case can always be avoided in C,
1560 but it must be handled correctly when using Fortran shared
1561 libraries.
1562
1563 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1564 likewise for OLDDYNCOMMON and OLDDEF.
1565
1566 Note that this test is just a heuristic, and that it is quite
1567 possible to have an uninitialized symbol in a shared object which
1568 is really a definition, rather than a common symbol. This could
1569 lead to some minor confusion when the symbol really is a common
1570 symbol in some regular object. However, I think it will be
1571 harmless. */
1572
1573 if (newdyn
1574 && newdef
1575 && !newweak
1576 && (sec->flags & SEC_ALLOC) != 0
1577 && (sec->flags & SEC_LOAD) == 0
1578 && sym->st_size > 0
1579 && !newfunc)
1580 newdyncommon = TRUE;
1581 else
1582 newdyncommon = FALSE;
1583
1584 if (olddyn
1585 && olddef
1586 && h->root.type == bfd_link_hash_defined
1587 && h->def_dynamic
1588 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1589 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1590 && h->size > 0
1591 && !oldfunc)
1592 olddyncommon = TRUE;
1593 else
1594 olddyncommon = FALSE;
1595
1596 /* We now know everything about the old and new symbols. We ask the
1597 backend to check if we can merge them. */
1598 if (bed->merge_symbol != NULL)
1599 {
1600 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1601 return FALSE;
1602 sec = *psec;
1603 }
1604
1605 /* There are multiple definitions of a normal symbol. Skip the
1606 default symbol as well as definition from an IR object. */
1607 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1608 && !default_sym && h->def_regular
1609 && !(oldbfd != NULL
1610 && (oldbfd->flags & BFD_PLUGIN) != 0
1611 && (abfd->flags & BFD_PLUGIN) == 0))
1612 {
1613 /* Handle a multiple definition. */
1614 (*info->callbacks->multiple_definition) (info, &h->root,
1615 abfd, sec, *pvalue);
1616 *skip = TRUE;
1617 return TRUE;
1618 }
1619
1620 /* If both the old and the new symbols look like common symbols in a
1621 dynamic object, set the size of the symbol to the larger of the
1622 two. */
1623
1624 if (olddyncommon
1625 && newdyncommon
1626 && sym->st_size != h->size)
1627 {
1628 /* Since we think we have two common symbols, issue a multiple
1629 common warning if desired. Note that we only warn if the
1630 size is different. If the size is the same, we simply let
1631 the old symbol override the new one as normally happens with
1632 symbols defined in dynamic objects. */
1633
1634 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1635 bfd_link_hash_common, sym->st_size);
1636 if (sym->st_size > h->size)
1637 h->size = sym->st_size;
1638
1639 *size_change_ok = TRUE;
1640 }
1641
1642 /* If we are looking at a dynamic object, and we have found a
1643 definition, we need to see if the symbol was already defined by
1644 some other object. If so, we want to use the existing
1645 definition, and we do not want to report a multiple symbol
1646 definition error; we do this by clobbering *PSEC to be
1647 bfd_und_section_ptr.
1648
1649 We treat a common symbol as a definition if the symbol in the
1650 shared library is a function, since common symbols always
1651 represent variables; this can cause confusion in principle, but
1652 any such confusion would seem to indicate an erroneous program or
1653 shared library. We also permit a common symbol in a regular
1654 object to override a weak symbol in a shared object. */
1655
1656 if (newdyn
1657 && newdef
1658 && (olddef
1659 || (h->root.type == bfd_link_hash_common
1660 && (newweak || newfunc))))
1661 {
1662 *override = abfd;
1663 newdef = FALSE;
1664 newdyncommon = FALSE;
1665
1666 *psec = sec = bfd_und_section_ptr;
1667 *size_change_ok = TRUE;
1668
1669 /* If we get here when the old symbol is a common symbol, then
1670 we are explicitly letting it override a weak symbol or
1671 function in a dynamic object, and we don't want to warn about
1672 a type change. If the old symbol is a defined symbol, a type
1673 change warning may still be appropriate. */
1674
1675 if (h->root.type == bfd_link_hash_common)
1676 *type_change_ok = TRUE;
1677 }
1678
1679 /* Handle the special case of an old common symbol merging with a
1680 new symbol which looks like a common symbol in a shared object.
1681 We change *PSEC and *PVALUE to make the new symbol look like a
1682 common symbol, and let _bfd_generic_link_add_one_symbol do the
1683 right thing. */
1684
1685 if (newdyncommon
1686 && h->root.type == bfd_link_hash_common)
1687 {
1688 *override = oldbfd;
1689 newdef = FALSE;
1690 newdyncommon = FALSE;
1691 *pvalue = sym->st_size;
1692 *psec = sec = bed->common_section (oldsec);
1693 *size_change_ok = TRUE;
1694 }
1695
1696 /* Skip weak definitions of symbols that are already defined. */
1697 if (newdef && olddef && newweak)
1698 {
1699 /* Don't skip new non-IR weak syms. */
1700 if (!(oldbfd != NULL
1701 && (oldbfd->flags & BFD_PLUGIN) != 0
1702 && (abfd->flags & BFD_PLUGIN) == 0))
1703 {
1704 newdef = FALSE;
1705 *skip = TRUE;
1706 }
1707
1708 /* Merge st_other. If the symbol already has a dynamic index,
1709 but visibility says it should not be visible, turn it into a
1710 local symbol. */
1711 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1712 if (h->dynindx != -1)
1713 switch (ELF_ST_VISIBILITY (h->other))
1714 {
1715 case STV_INTERNAL:
1716 case STV_HIDDEN:
1717 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1718 break;
1719 }
1720 }
1721
1722 /* If the old symbol is from a dynamic object, and the new symbol is
1723 a definition which is not from a dynamic object, then the new
1724 symbol overrides the old symbol. Symbols from regular files
1725 always take precedence over symbols from dynamic objects, even if
1726 they are defined after the dynamic object in the link.
1727
1728 As above, we again permit a common symbol in a regular object to
1729 override a definition in a shared object if the shared object
1730 symbol is a function or is weak. */
1731
1732 flip = NULL;
1733 if (!newdyn
1734 && (newdef
1735 || (bfd_is_com_section (sec)
1736 && (oldweak || oldfunc)))
1737 && olddyn
1738 && olddef
1739 && h->def_dynamic)
1740 {
1741 /* Change the hash table entry to undefined, and let
1742 _bfd_generic_link_add_one_symbol do the right thing with the
1743 new definition. */
1744
1745 h->root.type = bfd_link_hash_undefined;
1746 h->root.u.undef.abfd = h->root.u.def.section->owner;
1747 *size_change_ok = TRUE;
1748
1749 olddef = FALSE;
1750 olddyncommon = FALSE;
1751
1752 /* We again permit a type change when a common symbol may be
1753 overriding a function. */
1754
1755 if (bfd_is_com_section (sec))
1756 {
1757 if (oldfunc)
1758 {
1759 /* If a common symbol overrides a function, make sure
1760 that it isn't defined dynamically nor has type
1761 function. */
1762 h->def_dynamic = 0;
1763 h->type = STT_NOTYPE;
1764 }
1765 *type_change_ok = TRUE;
1766 }
1767
1768 if (hi->root.type == bfd_link_hash_indirect)
1769 flip = hi;
1770 else
1771 /* This union may have been set to be non-NULL when this symbol
1772 was seen in a dynamic object. We must force the union to be
1773 NULL, so that it is correct for a regular symbol. */
1774 h->verinfo.vertree = NULL;
1775 }
1776
1777 /* Handle the special case of a new common symbol merging with an
1778 old symbol that looks like it might be a common symbol defined in
1779 a shared object. Note that we have already handled the case in
1780 which a new common symbol should simply override the definition
1781 in the shared library. */
1782
1783 if (! newdyn
1784 && bfd_is_com_section (sec)
1785 && olddyncommon)
1786 {
1787 /* It would be best if we could set the hash table entry to a
1788 common symbol, but we don't know what to use for the section
1789 or the alignment. */
1790 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1791 bfd_link_hash_common, sym->st_size);
1792
1793 /* If the presumed common symbol in the dynamic object is
1794 larger, pretend that the new symbol has its size. */
1795
1796 if (h->size > *pvalue)
1797 *pvalue = h->size;
1798
1799 /* We need to remember the alignment required by the symbol
1800 in the dynamic object. */
1801 BFD_ASSERT (pold_alignment);
1802 *pold_alignment = h->root.u.def.section->alignment_power;
1803
1804 olddef = FALSE;
1805 olddyncommon = FALSE;
1806
1807 h->root.type = bfd_link_hash_undefined;
1808 h->root.u.undef.abfd = h->root.u.def.section->owner;
1809
1810 *size_change_ok = TRUE;
1811 *type_change_ok = TRUE;
1812
1813 if (hi->root.type == bfd_link_hash_indirect)
1814 flip = hi;
1815 else
1816 h->verinfo.vertree = NULL;
1817 }
1818
1819 if (flip != NULL)
1820 {
1821 /* Handle the case where we had a versioned symbol in a dynamic
1822 library and now find a definition in a normal object. In this
1823 case, we make the versioned symbol point to the normal one. */
1824 flip->root.type = h->root.type;
1825 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1826 h->root.type = bfd_link_hash_indirect;
1827 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1828 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1829 if (h->def_dynamic)
1830 {
1831 h->def_dynamic = 0;
1832 flip->ref_dynamic = 1;
1833 }
1834 }
1835
1836 return TRUE;
1837 }
1838
1839 /* This function is called to create an indirect symbol from the
1840 default for the symbol with the default version if needed. The
1841 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1842 set DYNSYM if the new indirect symbol is dynamic. */
1843
1844 static bfd_boolean
1845 _bfd_elf_add_default_symbol (bfd *abfd,
1846 struct bfd_link_info *info,
1847 struct elf_link_hash_entry *h,
1848 const char *name,
1849 Elf_Internal_Sym *sym,
1850 asection *sec,
1851 bfd_vma value,
1852 bfd **poldbfd,
1853 bfd_boolean *dynsym)
1854 {
1855 bfd_boolean type_change_ok;
1856 bfd_boolean size_change_ok;
1857 bfd_boolean skip;
1858 char *shortname;
1859 struct elf_link_hash_entry *hi;
1860 struct bfd_link_hash_entry *bh;
1861 const struct elf_backend_data *bed;
1862 bfd_boolean collect;
1863 bfd_boolean dynamic;
1864 bfd *override;
1865 char *p;
1866 size_t len, shortlen;
1867 asection *tmp_sec;
1868 bfd_boolean matched;
1869
1870 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1871 return TRUE;
1872
1873 /* If this symbol has a version, and it is the default version, we
1874 create an indirect symbol from the default name to the fully
1875 decorated name. This will cause external references which do not
1876 specify a version to be bound to this version of the symbol. */
1877 p = strchr (name, ELF_VER_CHR);
1878 if (h->versioned == unknown)
1879 {
1880 if (p == NULL)
1881 {
1882 h->versioned = unversioned;
1883 return TRUE;
1884 }
1885 else
1886 {
1887 if (p[1] != ELF_VER_CHR)
1888 {
1889 h->versioned = versioned_hidden;
1890 return TRUE;
1891 }
1892 else
1893 h->versioned = versioned;
1894 }
1895 }
1896 else
1897 {
1898 /* PR ld/19073: We may see an unversioned definition after the
1899 default version. */
1900 if (p == NULL)
1901 return TRUE;
1902 }
1903
1904 bed = get_elf_backend_data (abfd);
1905 collect = bed->collect;
1906 dynamic = (abfd->flags & DYNAMIC) != 0;
1907
1908 shortlen = p - name;
1909 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1910 if (shortname == NULL)
1911 return FALSE;
1912 memcpy (shortname, name, shortlen);
1913 shortname[shortlen] = '\0';
1914
1915 /* We are going to create a new symbol. Merge it with any existing
1916 symbol with this name. For the purposes of the merge, act as
1917 though we were defining the symbol we just defined, although we
1918 actually going to define an indirect symbol. */
1919 type_change_ok = FALSE;
1920 size_change_ok = FALSE;
1921 matched = TRUE;
1922 tmp_sec = sec;
1923 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1924 &hi, poldbfd, NULL, NULL, &skip, &override,
1925 &type_change_ok, &size_change_ok, &matched))
1926 return FALSE;
1927
1928 if (skip)
1929 goto nondefault;
1930
1931 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1932 {
1933 /* If the undecorated symbol will have a version added by a
1934 script different to H, then don't indirect to/from the
1935 undecorated symbol. This isn't ideal because we may not yet
1936 have seen symbol versions, if given by a script on the
1937 command line rather than via --version-script. */
1938 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1939 {
1940 bfd_boolean hide;
1941
1942 hi->verinfo.vertree
1943 = bfd_find_version_for_sym (info->version_info,
1944 hi->root.root.string, &hide);
1945 if (hi->verinfo.vertree != NULL && hide)
1946 {
1947 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1948 goto nondefault;
1949 }
1950 }
1951 if (hi->verinfo.vertree != NULL
1952 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1953 goto nondefault;
1954 }
1955
1956 if (! override)
1957 {
1958 /* Add the default symbol if not performing a relocatable link. */
1959 if (! bfd_link_relocatable (info))
1960 {
1961 bh = &hi->root;
1962 if (bh->type == bfd_link_hash_defined
1963 && bh->u.def.section->owner != NULL
1964 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1965 {
1966 /* Mark the previous definition from IR object as
1967 undefined so that the generic linker will override
1968 it. */
1969 bh->type = bfd_link_hash_undefined;
1970 bh->u.undef.abfd = bh->u.def.section->owner;
1971 }
1972 if (! (_bfd_generic_link_add_one_symbol
1973 (info, abfd, shortname, BSF_INDIRECT,
1974 bfd_ind_section_ptr,
1975 0, name, FALSE, collect, &bh)))
1976 return FALSE;
1977 hi = (struct elf_link_hash_entry *) bh;
1978 }
1979 }
1980 else
1981 {
1982 /* In this case the symbol named SHORTNAME is overriding the
1983 indirect symbol we want to add. We were planning on making
1984 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1985 is the name without a version. NAME is the fully versioned
1986 name, and it is the default version.
1987
1988 Overriding means that we already saw a definition for the
1989 symbol SHORTNAME in a regular object, and it is overriding
1990 the symbol defined in the dynamic object.
1991
1992 When this happens, we actually want to change NAME, the
1993 symbol we just added, to refer to SHORTNAME. This will cause
1994 references to NAME in the shared object to become references
1995 to SHORTNAME in the regular object. This is what we expect
1996 when we override a function in a shared object: that the
1997 references in the shared object will be mapped to the
1998 definition in the regular object. */
1999
2000 while (hi->root.type == bfd_link_hash_indirect
2001 || hi->root.type == bfd_link_hash_warning)
2002 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2003
2004 h->root.type = bfd_link_hash_indirect;
2005 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2006 if (h->def_dynamic)
2007 {
2008 h->def_dynamic = 0;
2009 hi->ref_dynamic = 1;
2010 if (hi->ref_regular
2011 || hi->def_regular)
2012 {
2013 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2014 return FALSE;
2015 }
2016 }
2017
2018 /* Now set HI to H, so that the following code will set the
2019 other fields correctly. */
2020 hi = h;
2021 }
2022
2023 /* Check if HI is a warning symbol. */
2024 if (hi->root.type == bfd_link_hash_warning)
2025 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2026
2027 /* If there is a duplicate definition somewhere, then HI may not
2028 point to an indirect symbol. We will have reported an error to
2029 the user in that case. */
2030
2031 if (hi->root.type == bfd_link_hash_indirect)
2032 {
2033 struct elf_link_hash_entry *ht;
2034
2035 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2036 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2037
2038 /* If we first saw a reference to SHORTNAME with non-default
2039 visibility, merge that visibility to the @@VER symbol. */
2040 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2041
2042 /* A reference to the SHORTNAME symbol from a dynamic library
2043 will be satisfied by the versioned symbol at runtime. In
2044 effect, we have a reference to the versioned symbol. */
2045 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2046 hi->dynamic_def |= ht->dynamic_def;
2047
2048 /* See if the new flags lead us to realize that the symbol must
2049 be dynamic. */
2050 if (! *dynsym)
2051 {
2052 if (! dynamic)
2053 {
2054 if (! bfd_link_executable (info)
2055 || hi->def_dynamic
2056 || hi->ref_dynamic)
2057 *dynsym = TRUE;
2058 }
2059 else
2060 {
2061 if (hi->ref_regular)
2062 *dynsym = TRUE;
2063 }
2064 }
2065 }
2066
2067 /* We also need to define an indirection from the nondefault version
2068 of the symbol. */
2069
2070 nondefault:
2071 len = strlen (name);
2072 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2073 if (shortname == NULL)
2074 return FALSE;
2075 memcpy (shortname, name, shortlen);
2076 memcpy (shortname + shortlen, p + 1, len - shortlen);
2077
2078 /* Once again, merge with any existing symbol. */
2079 type_change_ok = FALSE;
2080 size_change_ok = FALSE;
2081 tmp_sec = sec;
2082 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2083 &hi, poldbfd, NULL, NULL, &skip, &override,
2084 &type_change_ok, &size_change_ok, &matched))
2085 return FALSE;
2086
2087 if (skip)
2088 {
2089 if (!dynamic
2090 && h->root.type == bfd_link_hash_defweak
2091 && hi->root.type == bfd_link_hash_defined)
2092 {
2093 /* We are handling a weak sym@@ver and attempting to define
2094 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2095 new weak sym@ver because there is already a strong sym@ver.
2096 However, sym@ver and sym@@ver are really the same symbol.
2097 The existing strong sym@ver ought to override sym@@ver. */
2098 h->root.type = bfd_link_hash_defined;
2099 h->root.u.def.section = hi->root.u.def.section;
2100 h->root.u.def.value = hi->root.u.def.value;
2101 hi->root.type = bfd_link_hash_indirect;
2102 hi->root.u.i.link = &h->root;
2103 }
2104 else
2105 return TRUE;
2106 }
2107 else if (override)
2108 {
2109 /* Here SHORTNAME is a versioned name, so we don't expect to see
2110 the type of override we do in the case above unless it is
2111 overridden by a versioned definition. */
2112 if (hi->root.type != bfd_link_hash_defined
2113 && hi->root.type != bfd_link_hash_defweak)
2114 _bfd_error_handler
2115 /* xgettext:c-format */
2116 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2117 abfd, shortname);
2118 return TRUE;
2119 }
2120 else
2121 {
2122 bh = &hi->root;
2123 if (! (_bfd_generic_link_add_one_symbol
2124 (info, abfd, shortname, BSF_INDIRECT,
2125 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2126 return FALSE;
2127 hi = (struct elf_link_hash_entry *) bh;
2128 }
2129
2130 /* If there is a duplicate definition somewhere, then HI may not
2131 point to an indirect symbol. We will have reported an error
2132 to the user in that case. */
2133 if (hi->root.type == bfd_link_hash_indirect)
2134 {
2135 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2136 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2137 hi->dynamic_def |= h->dynamic_def;
2138
2139 /* If we first saw a reference to @VER symbol with
2140 non-default visibility, merge that visibility to the
2141 @@VER symbol. */
2142 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2143
2144 /* See if the new flags lead us to realize that the symbol
2145 must be dynamic. */
2146 if (! *dynsym)
2147 {
2148 if (! dynamic)
2149 {
2150 if (! bfd_link_executable (info)
2151 || hi->ref_dynamic)
2152 *dynsym = TRUE;
2153 }
2154 else
2155 {
2156 if (hi->ref_regular)
2157 *dynsym = TRUE;
2158 }
2159 }
2160 }
2161
2162 return TRUE;
2163 }
2164 \f
2165 /* This routine is used to export all defined symbols into the dynamic
2166 symbol table. It is called via elf_link_hash_traverse. */
2167
2168 static bfd_boolean
2169 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2170 {
2171 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2172
2173 /* Ignore indirect symbols. These are added by the versioning code. */
2174 if (h->root.type == bfd_link_hash_indirect)
2175 return TRUE;
2176
2177 /* Ignore this if we won't export it. */
2178 if (!eif->info->export_dynamic && !h->dynamic)
2179 return TRUE;
2180
2181 if (h->dynindx == -1
2182 && (h->def_regular || h->ref_regular)
2183 && ! bfd_hide_sym_by_version (eif->info->version_info,
2184 h->root.root.string))
2185 {
2186 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2187 {
2188 eif->failed = TRUE;
2189 return FALSE;
2190 }
2191 }
2192
2193 return TRUE;
2194 }
2195 \f
2196 /* Look through the symbols which are defined in other shared
2197 libraries and referenced here. Update the list of version
2198 dependencies. This will be put into the .gnu.version_r section.
2199 This function is called via elf_link_hash_traverse. */
2200
2201 static bfd_boolean
2202 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2203 void *data)
2204 {
2205 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2206 Elf_Internal_Verneed *t;
2207 Elf_Internal_Vernaux *a;
2208 size_t amt;
2209
2210 /* We only care about symbols defined in shared objects with version
2211 information. */
2212 if (!h->def_dynamic
2213 || h->def_regular
2214 || h->dynindx == -1
2215 || h->verinfo.verdef == NULL
2216 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2217 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2218 return TRUE;
2219
2220 /* See if we already know about this version. */
2221 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2222 t != NULL;
2223 t = t->vn_nextref)
2224 {
2225 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2226 continue;
2227
2228 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2229 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2230 return TRUE;
2231
2232 break;
2233 }
2234
2235 /* This is a new version. Add it to tree we are building. */
2236
2237 if (t == NULL)
2238 {
2239 amt = sizeof *t;
2240 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2241 if (t == NULL)
2242 {
2243 rinfo->failed = TRUE;
2244 return FALSE;
2245 }
2246
2247 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2248 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2249 elf_tdata (rinfo->info->output_bfd)->verref = t;
2250 }
2251
2252 amt = sizeof *a;
2253 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2254 if (a == NULL)
2255 {
2256 rinfo->failed = TRUE;
2257 return FALSE;
2258 }
2259
2260 /* Note that we are copying a string pointer here, and testing it
2261 above. If bfd_elf_string_from_elf_section is ever changed to
2262 discard the string data when low in memory, this will have to be
2263 fixed. */
2264 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2265
2266 a->vna_flags = h->verinfo.verdef->vd_flags;
2267 a->vna_nextptr = t->vn_auxptr;
2268
2269 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2270 ++rinfo->vers;
2271
2272 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2273
2274 t->vn_auxptr = a;
2275
2276 return TRUE;
2277 }
2278
2279 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2280 hidden. Set *T_P to NULL if there is no match. */
2281
2282 static bfd_boolean
2283 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2284 struct elf_link_hash_entry *h,
2285 const char *version_p,
2286 struct bfd_elf_version_tree **t_p,
2287 bfd_boolean *hide)
2288 {
2289 struct bfd_elf_version_tree *t;
2290
2291 /* Look for the version. If we find it, it is no longer weak. */
2292 for (t = info->version_info; t != NULL; t = t->next)
2293 {
2294 if (strcmp (t->name, version_p) == 0)
2295 {
2296 size_t len;
2297 char *alc;
2298 struct bfd_elf_version_expr *d;
2299
2300 len = version_p - h->root.root.string;
2301 alc = (char *) bfd_malloc (len);
2302 if (alc == NULL)
2303 return FALSE;
2304 memcpy (alc, h->root.root.string, len - 1);
2305 alc[len - 1] = '\0';
2306 if (alc[len - 2] == ELF_VER_CHR)
2307 alc[len - 2] = '\0';
2308
2309 h->verinfo.vertree = t;
2310 t->used = TRUE;
2311 d = NULL;
2312
2313 if (t->globals.list != NULL)
2314 d = (*t->match) (&t->globals, NULL, alc);
2315
2316 /* See if there is anything to force this symbol to
2317 local scope. */
2318 if (d == NULL && t->locals.list != NULL)
2319 {
2320 d = (*t->match) (&t->locals, NULL, alc);
2321 if (d != NULL
2322 && h->dynindx != -1
2323 && ! info->export_dynamic)
2324 *hide = TRUE;
2325 }
2326
2327 free (alc);
2328 break;
2329 }
2330 }
2331
2332 *t_p = t;
2333
2334 return TRUE;
2335 }
2336
2337 /* Return TRUE if the symbol H is hidden by version script. */
2338
2339 bfd_boolean
2340 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2341 struct elf_link_hash_entry *h)
2342 {
2343 const char *p;
2344 bfd_boolean hide = FALSE;
2345 const struct elf_backend_data *bed
2346 = get_elf_backend_data (info->output_bfd);
2347
2348 /* Version script only hides symbols defined in regular objects. */
2349 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2350 return TRUE;
2351
2352 p = strchr (h->root.root.string, ELF_VER_CHR);
2353 if (p != NULL && h->verinfo.vertree == NULL)
2354 {
2355 struct bfd_elf_version_tree *t;
2356
2357 ++p;
2358 if (*p == ELF_VER_CHR)
2359 ++p;
2360
2361 if (*p != '\0'
2362 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2363 && hide)
2364 {
2365 if (hide)
2366 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2367 return TRUE;
2368 }
2369 }
2370
2371 /* If we don't have a version for this symbol, see if we can find
2372 something. */
2373 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2374 {
2375 h->verinfo.vertree
2376 = bfd_find_version_for_sym (info->version_info,
2377 h->root.root.string, &hide);
2378 if (h->verinfo.vertree != NULL && hide)
2379 {
2380 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2381 return TRUE;
2382 }
2383 }
2384
2385 return FALSE;
2386 }
2387
2388 /* Figure out appropriate versions for all the symbols. We may not
2389 have the version number script until we have read all of the input
2390 files, so until that point we don't know which symbols should be
2391 local. This function is called via elf_link_hash_traverse. */
2392
2393 static bfd_boolean
2394 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2395 {
2396 struct elf_info_failed *sinfo;
2397 struct bfd_link_info *info;
2398 const struct elf_backend_data *bed;
2399 struct elf_info_failed eif;
2400 char *p;
2401 bfd_boolean hide;
2402
2403 sinfo = (struct elf_info_failed *) data;
2404 info = sinfo->info;
2405
2406 /* Fix the symbol flags. */
2407 eif.failed = FALSE;
2408 eif.info = info;
2409 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2410 {
2411 if (eif.failed)
2412 sinfo->failed = TRUE;
2413 return FALSE;
2414 }
2415
2416 bed = get_elf_backend_data (info->output_bfd);
2417
2418 /* We only need version numbers for symbols defined in regular
2419 objects. */
2420 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2421 {
2422 /* Hide symbols defined in discarded input sections. */
2423 if ((h->root.type == bfd_link_hash_defined
2424 || h->root.type == bfd_link_hash_defweak)
2425 && discarded_section (h->root.u.def.section))
2426 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2427 return TRUE;
2428 }
2429
2430 hide = FALSE;
2431 p = strchr (h->root.root.string, ELF_VER_CHR);
2432 if (p != NULL && h->verinfo.vertree == NULL)
2433 {
2434 struct bfd_elf_version_tree *t;
2435
2436 ++p;
2437 if (*p == ELF_VER_CHR)
2438 ++p;
2439
2440 /* If there is no version string, we can just return out. */
2441 if (*p == '\0')
2442 return TRUE;
2443
2444 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2445 {
2446 sinfo->failed = TRUE;
2447 return FALSE;
2448 }
2449
2450 if (hide)
2451 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2452
2453 /* If we are building an application, we need to create a
2454 version node for this version. */
2455 if (t == NULL && bfd_link_executable (info))
2456 {
2457 struct bfd_elf_version_tree **pp;
2458 int version_index;
2459
2460 /* If we aren't going to export this symbol, we don't need
2461 to worry about it. */
2462 if (h->dynindx == -1)
2463 return TRUE;
2464
2465 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2466 sizeof *t);
2467 if (t == NULL)
2468 {
2469 sinfo->failed = TRUE;
2470 return FALSE;
2471 }
2472
2473 t->name = p;
2474 t->name_indx = (unsigned int) -1;
2475 t->used = TRUE;
2476
2477 version_index = 1;
2478 /* Don't count anonymous version tag. */
2479 if (sinfo->info->version_info != NULL
2480 && sinfo->info->version_info->vernum == 0)
2481 version_index = 0;
2482 for (pp = &sinfo->info->version_info;
2483 *pp != NULL;
2484 pp = &(*pp)->next)
2485 ++version_index;
2486 t->vernum = version_index;
2487
2488 *pp = t;
2489
2490 h->verinfo.vertree = t;
2491 }
2492 else if (t == NULL)
2493 {
2494 /* We could not find the version for a symbol when
2495 generating a shared archive. Return an error. */
2496 _bfd_error_handler
2497 /* xgettext:c-format */
2498 (_("%pB: version node not found for symbol %s"),
2499 info->output_bfd, h->root.root.string);
2500 bfd_set_error (bfd_error_bad_value);
2501 sinfo->failed = TRUE;
2502 return FALSE;
2503 }
2504 }
2505
2506 /* If we don't have a version for this symbol, see if we can find
2507 something. */
2508 if (!hide
2509 && h->verinfo.vertree == NULL
2510 && sinfo->info->version_info != NULL)
2511 {
2512 h->verinfo.vertree
2513 = bfd_find_version_for_sym (sinfo->info->version_info,
2514 h->root.root.string, &hide);
2515 if (h->verinfo.vertree != NULL && hide)
2516 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2517 }
2518
2519 return TRUE;
2520 }
2521 \f
2522 /* Read and swap the relocs from the section indicated by SHDR. This
2523 may be either a REL or a RELA section. The relocations are
2524 translated into RELA relocations and stored in INTERNAL_RELOCS,
2525 which should have already been allocated to contain enough space.
2526 The EXTERNAL_RELOCS are a buffer where the external form of the
2527 relocations should be stored.
2528
2529 Returns FALSE if something goes wrong. */
2530
2531 static bfd_boolean
2532 elf_link_read_relocs_from_section (bfd *abfd,
2533 asection *sec,
2534 Elf_Internal_Shdr *shdr,
2535 void *external_relocs,
2536 Elf_Internal_Rela *internal_relocs)
2537 {
2538 const struct elf_backend_data *bed;
2539 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2540 const bfd_byte *erela;
2541 const bfd_byte *erelaend;
2542 Elf_Internal_Rela *irela;
2543 Elf_Internal_Shdr *symtab_hdr;
2544 size_t nsyms;
2545
2546 /* Position ourselves at the start of the section. */
2547 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2548 return FALSE;
2549
2550 /* Read the relocations. */
2551 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2552 return FALSE;
2553
2554 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2555 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2556
2557 bed = get_elf_backend_data (abfd);
2558
2559 /* Convert the external relocations to the internal format. */
2560 if (shdr->sh_entsize == bed->s->sizeof_rel)
2561 swap_in = bed->s->swap_reloc_in;
2562 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2563 swap_in = bed->s->swap_reloca_in;
2564 else
2565 {
2566 bfd_set_error (bfd_error_wrong_format);
2567 return FALSE;
2568 }
2569
2570 erela = (const bfd_byte *) external_relocs;
2571 /* Setting erelaend like this and comparing with <= handles case of
2572 a fuzzed object with sh_size not a multiple of sh_entsize. */
2573 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2574 irela = internal_relocs;
2575 while (erela <= erelaend)
2576 {
2577 bfd_vma r_symndx;
2578
2579 (*swap_in) (abfd, erela, irela);
2580 r_symndx = ELF32_R_SYM (irela->r_info);
2581 if (bed->s->arch_size == 64)
2582 r_symndx >>= 24;
2583 if (nsyms > 0)
2584 {
2585 if ((size_t) r_symndx >= nsyms)
2586 {
2587 _bfd_error_handler
2588 /* xgettext:c-format */
2589 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2590 " for offset %#" PRIx64 " in section `%pA'"),
2591 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2592 (uint64_t) irela->r_offset, sec);
2593 bfd_set_error (bfd_error_bad_value);
2594 return FALSE;
2595 }
2596 }
2597 else if (r_symndx != STN_UNDEF)
2598 {
2599 _bfd_error_handler
2600 /* xgettext:c-format */
2601 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2602 " for offset %#" PRIx64 " in section `%pA'"
2603 " when the object file has no symbol table"),
2604 abfd, (uint64_t) r_symndx,
2605 (uint64_t) irela->r_offset, sec);
2606 bfd_set_error (bfd_error_bad_value);
2607 return FALSE;
2608 }
2609 irela += bed->s->int_rels_per_ext_rel;
2610 erela += shdr->sh_entsize;
2611 }
2612
2613 return TRUE;
2614 }
2615
2616 /* Read and swap the relocs for a section O. They may have been
2617 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2618 not NULL, they are used as buffers to read into. They are known to
2619 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2620 the return value is allocated using either malloc or bfd_alloc,
2621 according to the KEEP_MEMORY argument. If O has two relocation
2622 sections (both REL and RELA relocations), then the REL_HDR
2623 relocations will appear first in INTERNAL_RELOCS, followed by the
2624 RELA_HDR relocations. */
2625
2626 Elf_Internal_Rela *
2627 _bfd_elf_link_read_relocs (bfd *abfd,
2628 asection *o,
2629 void *external_relocs,
2630 Elf_Internal_Rela *internal_relocs,
2631 bfd_boolean keep_memory)
2632 {
2633 void *alloc1 = NULL;
2634 Elf_Internal_Rela *alloc2 = NULL;
2635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2636 struct bfd_elf_section_data *esdo = elf_section_data (o);
2637 Elf_Internal_Rela *internal_rela_relocs;
2638
2639 if (esdo->relocs != NULL)
2640 return esdo->relocs;
2641
2642 if (o->reloc_count == 0)
2643 return NULL;
2644
2645 if (internal_relocs == NULL)
2646 {
2647 bfd_size_type size;
2648
2649 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2650 if (keep_memory)
2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2652 else
2653 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2654 if (internal_relocs == NULL)
2655 goto error_return;
2656 }
2657
2658 if (external_relocs == NULL)
2659 {
2660 bfd_size_type size = 0;
2661
2662 if (esdo->rel.hdr)
2663 size += esdo->rel.hdr->sh_size;
2664 if (esdo->rela.hdr)
2665 size += esdo->rela.hdr->sh_size;
2666
2667 alloc1 = bfd_malloc (size);
2668 if (alloc1 == NULL)
2669 goto error_return;
2670 external_relocs = alloc1;
2671 }
2672
2673 internal_rela_relocs = internal_relocs;
2674 if (esdo->rel.hdr)
2675 {
2676 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2677 external_relocs,
2678 internal_relocs))
2679 goto error_return;
2680 external_relocs = (((bfd_byte *) external_relocs)
2681 + esdo->rel.hdr->sh_size);
2682 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2683 * bed->s->int_rels_per_ext_rel);
2684 }
2685
2686 if (esdo->rela.hdr
2687 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2688 external_relocs,
2689 internal_rela_relocs)))
2690 goto error_return;
2691
2692 /* Cache the results for next time, if we can. */
2693 if (keep_memory)
2694 esdo->relocs = internal_relocs;
2695
2696 free (alloc1);
2697
2698 /* Don't free alloc2, since if it was allocated we are passing it
2699 back (under the name of internal_relocs). */
2700
2701 return internal_relocs;
2702
2703 error_return:
2704 free (alloc1);
2705 if (alloc2 != NULL)
2706 {
2707 if (keep_memory)
2708 bfd_release (abfd, alloc2);
2709 else
2710 free (alloc2);
2711 }
2712 return NULL;
2713 }
2714
2715 /* Compute the size of, and allocate space for, REL_HDR which is the
2716 section header for a section containing relocations for O. */
2717
2718 static bfd_boolean
2719 _bfd_elf_link_size_reloc_section (bfd *abfd,
2720 struct bfd_elf_section_reloc_data *reldata)
2721 {
2722 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2723
2724 /* That allows us to calculate the size of the section. */
2725 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2726
2727 /* The contents field must last into write_object_contents, so we
2728 allocate it with bfd_alloc rather than malloc. Also since we
2729 cannot be sure that the contents will actually be filled in,
2730 we zero the allocated space. */
2731 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2732 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2733 return FALSE;
2734
2735 if (reldata->hashes == NULL && reldata->count)
2736 {
2737 struct elf_link_hash_entry **p;
2738
2739 p = ((struct elf_link_hash_entry **)
2740 bfd_zmalloc (reldata->count * sizeof (*p)));
2741 if (p == NULL)
2742 return FALSE;
2743
2744 reldata->hashes = p;
2745 }
2746
2747 return TRUE;
2748 }
2749
2750 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2751 originated from the section given by INPUT_REL_HDR) to the
2752 OUTPUT_BFD. */
2753
2754 bfd_boolean
2755 _bfd_elf_link_output_relocs (bfd *output_bfd,
2756 asection *input_section,
2757 Elf_Internal_Shdr *input_rel_hdr,
2758 Elf_Internal_Rela *internal_relocs,
2759 struct elf_link_hash_entry **rel_hash
2760 ATTRIBUTE_UNUSED)
2761 {
2762 Elf_Internal_Rela *irela;
2763 Elf_Internal_Rela *irelaend;
2764 bfd_byte *erel;
2765 struct bfd_elf_section_reloc_data *output_reldata;
2766 asection *output_section;
2767 const struct elf_backend_data *bed;
2768 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2769 struct bfd_elf_section_data *esdo;
2770
2771 output_section = input_section->output_section;
2772
2773 bed = get_elf_backend_data (output_bfd);
2774 esdo = elf_section_data (output_section);
2775 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2776 {
2777 output_reldata = &esdo->rel;
2778 swap_out = bed->s->swap_reloc_out;
2779 }
2780 else if (esdo->rela.hdr
2781 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2782 {
2783 output_reldata = &esdo->rela;
2784 swap_out = bed->s->swap_reloca_out;
2785 }
2786 else
2787 {
2788 _bfd_error_handler
2789 /* xgettext:c-format */
2790 (_("%pB: relocation size mismatch in %pB section %pA"),
2791 output_bfd, input_section->owner, input_section);
2792 bfd_set_error (bfd_error_wrong_format);
2793 return FALSE;
2794 }
2795
2796 erel = output_reldata->hdr->contents;
2797 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2798 irela = internal_relocs;
2799 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2800 * bed->s->int_rels_per_ext_rel);
2801 while (irela < irelaend)
2802 {
2803 (*swap_out) (output_bfd, irela, erel);
2804 irela += bed->s->int_rels_per_ext_rel;
2805 erel += input_rel_hdr->sh_entsize;
2806 }
2807
2808 /* Bump the counter, so that we know where to add the next set of
2809 relocations. */
2810 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2811
2812 return TRUE;
2813 }
2814 \f
2815 /* Make weak undefined symbols in PIE dynamic. */
2816
2817 bfd_boolean
2818 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2819 struct elf_link_hash_entry *h)
2820 {
2821 if (bfd_link_pie (info)
2822 && h->dynindx == -1
2823 && h->root.type == bfd_link_hash_undefweak)
2824 return bfd_elf_link_record_dynamic_symbol (info, h);
2825
2826 return TRUE;
2827 }
2828
2829 /* Fix up the flags for a symbol. This handles various cases which
2830 can only be fixed after all the input files are seen. This is
2831 currently called by both adjust_dynamic_symbol and
2832 assign_sym_version, which is unnecessary but perhaps more robust in
2833 the face of future changes. */
2834
2835 static bfd_boolean
2836 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2837 struct elf_info_failed *eif)
2838 {
2839 const struct elf_backend_data *bed;
2840
2841 /* If this symbol was mentioned in a non-ELF file, try to set
2842 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2843 permit a non-ELF file to correctly refer to a symbol defined in
2844 an ELF dynamic object. */
2845 if (h->non_elf)
2846 {
2847 while (h->root.type == bfd_link_hash_indirect)
2848 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2849
2850 if (h->root.type != bfd_link_hash_defined
2851 && h->root.type != bfd_link_hash_defweak)
2852 {
2853 h->ref_regular = 1;
2854 h->ref_regular_nonweak = 1;
2855 }
2856 else
2857 {
2858 if (h->root.u.def.section->owner != NULL
2859 && (bfd_get_flavour (h->root.u.def.section->owner)
2860 == bfd_target_elf_flavour))
2861 {
2862 h->ref_regular = 1;
2863 h->ref_regular_nonweak = 1;
2864 }
2865 else
2866 h->def_regular = 1;
2867 }
2868
2869 if (h->dynindx == -1
2870 && (h->def_dynamic
2871 || h->ref_dynamic))
2872 {
2873 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2874 {
2875 eif->failed = TRUE;
2876 return FALSE;
2877 }
2878 }
2879 }
2880 else
2881 {
2882 /* Unfortunately, NON_ELF is only correct if the symbol
2883 was first seen in a non-ELF file. Fortunately, if the symbol
2884 was first seen in an ELF file, we're probably OK unless the
2885 symbol was defined in a non-ELF file. Catch that case here.
2886 FIXME: We're still in trouble if the symbol was first seen in
2887 a dynamic object, and then later in a non-ELF regular object. */
2888 if ((h->root.type == bfd_link_hash_defined
2889 || h->root.type == bfd_link_hash_defweak)
2890 && !h->def_regular
2891 && (h->root.u.def.section->owner != NULL
2892 ? (bfd_get_flavour (h->root.u.def.section->owner)
2893 != bfd_target_elf_flavour)
2894 : (bfd_is_abs_section (h->root.u.def.section)
2895 && !h->def_dynamic)))
2896 h->def_regular = 1;
2897 }
2898
2899 /* Backend specific symbol fixup. */
2900 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2901 if (bed->elf_backend_fixup_symbol
2902 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2903 return FALSE;
2904
2905 /* If this is a final link, and the symbol was defined as a common
2906 symbol in a regular object file, and there was no definition in
2907 any dynamic object, then the linker will have allocated space for
2908 the symbol in a common section but the DEF_REGULAR
2909 flag will not have been set. */
2910 if (h->root.type == bfd_link_hash_defined
2911 && !h->def_regular
2912 && h->ref_regular
2913 && !h->def_dynamic
2914 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2915 h->def_regular = 1;
2916
2917 /* Symbols defined in discarded sections shouldn't be dynamic. */
2918 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2919 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2920
2921 /* If a weak undefined symbol has non-default visibility, we also
2922 hide it from the dynamic linker. */
2923 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2924 && h->root.type == bfd_link_hash_undefweak)
2925 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2926
2927 /* A hidden versioned symbol in executable should be forced local if
2928 it is is locally defined, not referenced by shared library and not
2929 exported. */
2930 else if (bfd_link_executable (eif->info)
2931 && h->versioned == versioned_hidden
2932 && !eif->info->export_dynamic
2933 && !h->dynamic
2934 && !h->ref_dynamic
2935 && h->def_regular)
2936 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2937
2938 /* If -Bsymbolic was used (which means to bind references to global
2939 symbols to the definition within the shared object), and this
2940 symbol was defined in a regular object, then it actually doesn't
2941 need a PLT entry. Likewise, if the symbol has non-default
2942 visibility. If the symbol has hidden or internal visibility, we
2943 will force it local. */
2944 else if (h->needs_plt
2945 && bfd_link_pic (eif->info)
2946 && is_elf_hash_table (eif->info->hash)
2947 && (SYMBOLIC_BIND (eif->info, h)
2948 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2949 && h->def_regular)
2950 {
2951 bfd_boolean force_local;
2952
2953 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2954 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2955 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2956 }
2957
2958 /* If this is a weak defined symbol in a dynamic object, and we know
2959 the real definition in the dynamic object, copy interesting flags
2960 over to the real definition. */
2961 if (h->is_weakalias)
2962 {
2963 struct elf_link_hash_entry *def = weakdef (h);
2964
2965 /* If the real definition is defined by a regular object file,
2966 don't do anything special. See the longer description in
2967 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2968 bfd_link_hash_defined as it was when put on the alias list
2969 then it must have originally been a versioned symbol (for
2970 which a non-versioned indirect symbol is created) and later
2971 a definition for the non-versioned symbol is found. In that
2972 case the indirection is flipped with the versioned symbol
2973 becoming an indirect pointing at the non-versioned symbol.
2974 Thus, not an alias any more. */
2975 if (def->def_regular
2976 || def->root.type != bfd_link_hash_defined)
2977 {
2978 h = def;
2979 while ((h = h->u.alias) != def)
2980 h->is_weakalias = 0;
2981 }
2982 else
2983 {
2984 while (h->root.type == bfd_link_hash_indirect)
2985 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2986 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2987 || h->root.type == bfd_link_hash_defweak);
2988 BFD_ASSERT (def->def_dynamic);
2989 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2990 }
2991 }
2992
2993 return TRUE;
2994 }
2995
2996 /* Make the backend pick a good value for a dynamic symbol. This is
2997 called via elf_link_hash_traverse, and also calls itself
2998 recursively. */
2999
3000 static bfd_boolean
3001 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3002 {
3003 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3004 struct elf_link_hash_table *htab;
3005 const struct elf_backend_data *bed;
3006
3007 if (! is_elf_hash_table (eif->info->hash))
3008 return FALSE;
3009
3010 /* Ignore indirect symbols. These are added by the versioning code. */
3011 if (h->root.type == bfd_link_hash_indirect)
3012 return TRUE;
3013
3014 /* Fix the symbol flags. */
3015 if (! _bfd_elf_fix_symbol_flags (h, eif))
3016 return FALSE;
3017
3018 htab = elf_hash_table (eif->info);
3019 bed = get_elf_backend_data (htab->dynobj);
3020
3021 if (h->root.type == bfd_link_hash_undefweak)
3022 {
3023 if (eif->info->dynamic_undefined_weak == 0)
3024 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
3025 else if (eif->info->dynamic_undefined_weak > 0
3026 && h->ref_regular
3027 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3028 && !bfd_hide_sym_by_version (eif->info->version_info,
3029 h->root.root.string))
3030 {
3031 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3032 {
3033 eif->failed = TRUE;
3034 return FALSE;
3035 }
3036 }
3037 }
3038
3039 /* If this symbol does not require a PLT entry, and it is not
3040 defined by a dynamic object, or is not referenced by a regular
3041 object, ignore it. We do have to handle a weak defined symbol,
3042 even if no regular object refers to it, if we decided to add it
3043 to the dynamic symbol table. FIXME: Do we normally need to worry
3044 about symbols which are defined by one dynamic object and
3045 referenced by another one? */
3046 if (!h->needs_plt
3047 && h->type != STT_GNU_IFUNC
3048 && (h->def_regular
3049 || !h->def_dynamic
3050 || (!h->ref_regular
3051 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3052 {
3053 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3054 return TRUE;
3055 }
3056
3057 /* If we've already adjusted this symbol, don't do it again. This
3058 can happen via a recursive call. */
3059 if (h->dynamic_adjusted)
3060 return TRUE;
3061
3062 /* Don't look at this symbol again. Note that we must set this
3063 after checking the above conditions, because we may look at a
3064 symbol once, decide not to do anything, and then get called
3065 recursively later after REF_REGULAR is set below. */
3066 h->dynamic_adjusted = 1;
3067
3068 /* If this is a weak definition, and we know a real definition, and
3069 the real symbol is not itself defined by a regular object file,
3070 then get a good value for the real definition. We handle the
3071 real symbol first, for the convenience of the backend routine.
3072
3073 Note that there is a confusing case here. If the real definition
3074 is defined by a regular object file, we don't get the real symbol
3075 from the dynamic object, but we do get the weak symbol. If the
3076 processor backend uses a COPY reloc, then if some routine in the
3077 dynamic object changes the real symbol, we will not see that
3078 change in the corresponding weak symbol. This is the way other
3079 ELF linkers work as well, and seems to be a result of the shared
3080 library model.
3081
3082 I will clarify this issue. Most SVR4 shared libraries define the
3083 variable _timezone and define timezone as a weak synonym. The
3084 tzset call changes _timezone. If you write
3085 extern int timezone;
3086 int _timezone = 5;
3087 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3088 you might expect that, since timezone is a synonym for _timezone,
3089 the same number will print both times. However, if the processor
3090 backend uses a COPY reloc, then actually timezone will be copied
3091 into your process image, and, since you define _timezone
3092 yourself, _timezone will not. Thus timezone and _timezone will
3093 wind up at different memory locations. The tzset call will set
3094 _timezone, leaving timezone unchanged. */
3095
3096 if (h->is_weakalias)
3097 {
3098 struct elf_link_hash_entry *def = weakdef (h);
3099
3100 /* If we get to this point, there is an implicit reference to
3101 the alias by a regular object file via the weak symbol H. */
3102 def->ref_regular = 1;
3103
3104 /* Ensure that the backend adjust_dynamic_symbol function sees
3105 the strong alias before H by recursively calling ourselves. */
3106 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3107 return FALSE;
3108 }
3109
3110 /* If a symbol has no type and no size and does not require a PLT
3111 entry, then we are probably about to do the wrong thing here: we
3112 are probably going to create a COPY reloc for an empty object.
3113 This case can arise when a shared object is built with assembly
3114 code, and the assembly code fails to set the symbol type. */
3115 if (h->size == 0
3116 && h->type == STT_NOTYPE
3117 && !h->needs_plt)
3118 _bfd_error_handler
3119 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3120 h->root.root.string);
3121
3122 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3123 {
3124 eif->failed = TRUE;
3125 return FALSE;
3126 }
3127
3128 return TRUE;
3129 }
3130
3131 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3132 DYNBSS. */
3133
3134 bfd_boolean
3135 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3136 struct elf_link_hash_entry *h,
3137 asection *dynbss)
3138 {
3139 unsigned int power_of_two;
3140 bfd_vma mask;
3141 asection *sec = h->root.u.def.section;
3142
3143 /* The section alignment of the definition is the maximum alignment
3144 requirement of symbols defined in the section. Since we don't
3145 know the symbol alignment requirement, we start with the
3146 maximum alignment and check low bits of the symbol address
3147 for the minimum alignment. */
3148 power_of_two = bfd_section_alignment (sec);
3149 mask = ((bfd_vma) 1 << power_of_two) - 1;
3150 while ((h->root.u.def.value & mask) != 0)
3151 {
3152 mask >>= 1;
3153 --power_of_two;
3154 }
3155
3156 if (power_of_two > bfd_section_alignment (dynbss))
3157 {
3158 /* Adjust the section alignment if needed. */
3159 if (!bfd_set_section_alignment (dynbss, power_of_two))
3160 return FALSE;
3161 }
3162
3163 /* We make sure that the symbol will be aligned properly. */
3164 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3165
3166 /* Define the symbol as being at this point in DYNBSS. */
3167 h->root.u.def.section = dynbss;
3168 h->root.u.def.value = dynbss->size;
3169
3170 /* Increment the size of DYNBSS to make room for the symbol. */
3171 dynbss->size += h->size;
3172
3173 /* No error if extern_protected_data is true. */
3174 if (h->protected_def
3175 && (!info->extern_protected_data
3176 || (info->extern_protected_data < 0
3177 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3178 info->callbacks->einfo
3179 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3180 h->root.root.string);
3181
3182 return TRUE;
3183 }
3184
3185 /* Adjust all external symbols pointing into SEC_MERGE sections
3186 to reflect the object merging within the sections. */
3187
3188 static bfd_boolean
3189 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3190 {
3191 asection *sec;
3192
3193 if ((h->root.type == bfd_link_hash_defined
3194 || h->root.type == bfd_link_hash_defweak)
3195 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3196 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3197 {
3198 bfd *output_bfd = (bfd *) data;
3199
3200 h->root.u.def.value =
3201 _bfd_merged_section_offset (output_bfd,
3202 &h->root.u.def.section,
3203 elf_section_data (sec)->sec_info,
3204 h->root.u.def.value);
3205 }
3206
3207 return TRUE;
3208 }
3209
3210 /* Returns false if the symbol referred to by H should be considered
3211 to resolve local to the current module, and true if it should be
3212 considered to bind dynamically. */
3213
3214 bfd_boolean
3215 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3216 struct bfd_link_info *info,
3217 bfd_boolean not_local_protected)
3218 {
3219 bfd_boolean binding_stays_local_p;
3220 const struct elf_backend_data *bed;
3221 struct elf_link_hash_table *hash_table;
3222
3223 if (h == NULL)
3224 return FALSE;
3225
3226 while (h->root.type == bfd_link_hash_indirect
3227 || h->root.type == bfd_link_hash_warning)
3228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3229
3230 /* If it was forced local, then clearly it's not dynamic. */
3231 if (h->dynindx == -1)
3232 return FALSE;
3233 if (h->forced_local)
3234 return FALSE;
3235
3236 /* Identify the cases where name binding rules say that a
3237 visible symbol resolves locally. */
3238 binding_stays_local_p = (bfd_link_executable (info)
3239 || SYMBOLIC_BIND (info, h));
3240
3241 switch (ELF_ST_VISIBILITY (h->other))
3242 {
3243 case STV_INTERNAL:
3244 case STV_HIDDEN:
3245 return FALSE;
3246
3247 case STV_PROTECTED:
3248 hash_table = elf_hash_table (info);
3249 if (!is_elf_hash_table (hash_table))
3250 return FALSE;
3251
3252 bed = get_elf_backend_data (hash_table->dynobj);
3253
3254 /* Proper resolution for function pointer equality may require
3255 that these symbols perhaps be resolved dynamically, even though
3256 we should be resolving them to the current module. */
3257 if (!not_local_protected || !bed->is_function_type (h->type))
3258 binding_stays_local_p = TRUE;
3259 break;
3260
3261 default:
3262 break;
3263 }
3264
3265 /* If it isn't defined locally, then clearly it's dynamic. */
3266 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3267 return TRUE;
3268
3269 /* Otherwise, the symbol is dynamic if binding rules don't tell
3270 us that it remains local. */
3271 return !binding_stays_local_p;
3272 }
3273
3274 /* Return true if the symbol referred to by H should be considered
3275 to resolve local to the current module, and false otherwise. Differs
3276 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3277 undefined symbols. The two functions are virtually identical except
3278 for the place where dynindx == -1 is tested. If that test is true,
3279 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3280 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3281 defined symbols.
3282 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3283 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3284 treatment of undefined weak symbols. For those that do not make
3285 undefined weak symbols dynamic, both functions may return false. */
3286
3287 bfd_boolean
3288 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3289 struct bfd_link_info *info,
3290 bfd_boolean local_protected)
3291 {
3292 const struct elf_backend_data *bed;
3293 struct elf_link_hash_table *hash_table;
3294
3295 /* If it's a local sym, of course we resolve locally. */
3296 if (h == NULL)
3297 return TRUE;
3298
3299 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3300 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3301 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3302 return TRUE;
3303
3304 /* Forced local symbols resolve locally. */
3305 if (h->forced_local)
3306 return TRUE;
3307
3308 /* Common symbols that become definitions don't get the DEF_REGULAR
3309 flag set, so test it first, and don't bail out. */
3310 if (ELF_COMMON_DEF_P (h))
3311 /* Do nothing. */;
3312 /* If we don't have a definition in a regular file, then we can't
3313 resolve locally. The sym is either undefined or dynamic. */
3314 else if (!h->def_regular)
3315 return FALSE;
3316
3317 /* Non-dynamic symbols resolve locally. */
3318 if (h->dynindx == -1)
3319 return TRUE;
3320
3321 /* At this point, we know the symbol is defined and dynamic. In an
3322 executable it must resolve locally, likewise when building symbolic
3323 shared libraries. */
3324 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3325 return TRUE;
3326
3327 /* Now deal with defined dynamic symbols in shared libraries. Ones
3328 with default visibility might not resolve locally. */
3329 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3330 return FALSE;
3331
3332 hash_table = elf_hash_table (info);
3333 if (!is_elf_hash_table (hash_table))
3334 return TRUE;
3335
3336 bed = get_elf_backend_data (hash_table->dynobj);
3337
3338 /* If extern_protected_data is false, STV_PROTECTED non-function
3339 symbols are local. */
3340 if ((!info->extern_protected_data
3341 || (info->extern_protected_data < 0
3342 && !bed->extern_protected_data))
3343 && !bed->is_function_type (h->type))
3344 return TRUE;
3345
3346 /* Function pointer equality tests may require that STV_PROTECTED
3347 symbols be treated as dynamic symbols. If the address of a
3348 function not defined in an executable is set to that function's
3349 plt entry in the executable, then the address of the function in
3350 a shared library must also be the plt entry in the executable. */
3351 return local_protected;
3352 }
3353
3354 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3355 aligned. Returns the first TLS output section. */
3356
3357 struct bfd_section *
3358 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3359 {
3360 struct bfd_section *sec, *tls;
3361 unsigned int align = 0;
3362
3363 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3364 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3365 break;
3366 tls = sec;
3367
3368 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3369 if (sec->alignment_power > align)
3370 align = sec->alignment_power;
3371
3372 elf_hash_table (info)->tls_sec = tls;
3373
3374 /* Ensure the alignment of the first section (usually .tdata) is the largest
3375 alignment, so that the tls segment starts aligned. */
3376 if (tls != NULL)
3377 tls->alignment_power = align;
3378
3379 return tls;
3380 }
3381
3382 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3383 static bfd_boolean
3384 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3385 Elf_Internal_Sym *sym)
3386 {
3387 const struct elf_backend_data *bed;
3388
3389 /* Local symbols do not count, but target specific ones might. */
3390 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3391 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3392 return FALSE;
3393
3394 bed = get_elf_backend_data (abfd);
3395 /* Function symbols do not count. */
3396 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3397 return FALSE;
3398
3399 /* If the section is undefined, then so is the symbol. */
3400 if (sym->st_shndx == SHN_UNDEF)
3401 return FALSE;
3402
3403 /* If the symbol is defined in the common section, then
3404 it is a common definition and so does not count. */
3405 if (bed->common_definition (sym))
3406 return FALSE;
3407
3408 /* If the symbol is in a target specific section then we
3409 must rely upon the backend to tell us what it is. */
3410 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3411 /* FIXME - this function is not coded yet:
3412
3413 return _bfd_is_global_symbol_definition (abfd, sym);
3414
3415 Instead for now assume that the definition is not global,
3416 Even if this is wrong, at least the linker will behave
3417 in the same way that it used to do. */
3418 return FALSE;
3419
3420 return TRUE;
3421 }
3422
3423 /* Search the symbol table of the archive element of the archive ABFD
3424 whose archive map contains a mention of SYMDEF, and determine if
3425 the symbol is defined in this element. */
3426 static bfd_boolean
3427 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3428 {
3429 Elf_Internal_Shdr * hdr;
3430 size_t symcount;
3431 size_t extsymcount;
3432 size_t extsymoff;
3433 Elf_Internal_Sym *isymbuf;
3434 Elf_Internal_Sym *isym;
3435 Elf_Internal_Sym *isymend;
3436 bfd_boolean result;
3437
3438 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3439 if (abfd == NULL)
3440 return FALSE;
3441
3442 if (! bfd_check_format (abfd, bfd_object))
3443 return FALSE;
3444
3445 /* Select the appropriate symbol table. If we don't know if the
3446 object file is an IR object, give linker LTO plugin a chance to
3447 get the correct symbol table. */
3448 if (abfd->plugin_format == bfd_plugin_yes
3449 #if BFD_SUPPORTS_PLUGINS
3450 || (abfd->plugin_format == bfd_plugin_unknown
3451 && bfd_link_plugin_object_p (abfd))
3452 #endif
3453 )
3454 {
3455 /* Use the IR symbol table if the object has been claimed by
3456 plugin. */
3457 abfd = abfd->plugin_dummy_bfd;
3458 hdr = &elf_tdata (abfd)->symtab_hdr;
3459 }
3460 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3461 hdr = &elf_tdata (abfd)->symtab_hdr;
3462 else
3463 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3464
3465 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3466
3467 /* The sh_info field of the symtab header tells us where the
3468 external symbols start. We don't care about the local symbols. */
3469 if (elf_bad_symtab (abfd))
3470 {
3471 extsymcount = symcount;
3472 extsymoff = 0;
3473 }
3474 else
3475 {
3476 extsymcount = symcount - hdr->sh_info;
3477 extsymoff = hdr->sh_info;
3478 }
3479
3480 if (extsymcount == 0)
3481 return FALSE;
3482
3483 /* Read in the symbol table. */
3484 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3485 NULL, NULL, NULL);
3486 if (isymbuf == NULL)
3487 return FALSE;
3488
3489 /* Scan the symbol table looking for SYMDEF. */
3490 result = FALSE;
3491 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3492 {
3493 const char *name;
3494
3495 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3496 isym->st_name);
3497 if (name == NULL)
3498 break;
3499
3500 if (strcmp (name, symdef->name) == 0)
3501 {
3502 result = is_global_data_symbol_definition (abfd, isym);
3503 break;
3504 }
3505 }
3506
3507 free (isymbuf);
3508
3509 return result;
3510 }
3511 \f
3512 /* Add an entry to the .dynamic table. */
3513
3514 bfd_boolean
3515 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3516 bfd_vma tag,
3517 bfd_vma val)
3518 {
3519 struct elf_link_hash_table *hash_table;
3520 const struct elf_backend_data *bed;
3521 asection *s;
3522 bfd_size_type newsize;
3523 bfd_byte *newcontents;
3524 Elf_Internal_Dyn dyn;
3525
3526 hash_table = elf_hash_table (info);
3527 if (! is_elf_hash_table (hash_table))
3528 return FALSE;
3529
3530 if (tag == DT_RELA || tag == DT_REL)
3531 hash_table->dynamic_relocs = TRUE;
3532
3533 bed = get_elf_backend_data (hash_table->dynobj);
3534 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3535 BFD_ASSERT (s != NULL);
3536
3537 newsize = s->size + bed->s->sizeof_dyn;
3538 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3539 if (newcontents == NULL)
3540 return FALSE;
3541
3542 dyn.d_tag = tag;
3543 dyn.d_un.d_val = val;
3544 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3545
3546 s->size = newsize;
3547 s->contents = newcontents;
3548
3549 return TRUE;
3550 }
3551
3552 /* Strip zero-sized dynamic sections. */
3553
3554 bfd_boolean
3555 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3556 {
3557 struct elf_link_hash_table *hash_table;
3558 const struct elf_backend_data *bed;
3559 asection *s, *sdynamic, **pp;
3560 asection *rela_dyn, *rel_dyn;
3561 Elf_Internal_Dyn dyn;
3562 bfd_byte *extdyn, *next;
3563 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3564 bfd_boolean strip_zero_sized;
3565 bfd_boolean strip_zero_sized_plt;
3566
3567 if (bfd_link_relocatable (info))
3568 return TRUE;
3569
3570 hash_table = elf_hash_table (info);
3571 if (!is_elf_hash_table (hash_table))
3572 return FALSE;
3573
3574 if (!hash_table->dynobj)
3575 return TRUE;
3576
3577 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3578 if (!sdynamic)
3579 return TRUE;
3580
3581 bed = get_elf_backend_data (hash_table->dynobj);
3582 swap_dyn_in = bed->s->swap_dyn_in;
3583
3584 strip_zero_sized = FALSE;
3585 strip_zero_sized_plt = FALSE;
3586
3587 /* Strip zero-sized dynamic sections. */
3588 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3589 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3590 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3591 if (s->size == 0
3592 && (s == rela_dyn
3593 || s == rel_dyn
3594 || s == hash_table->srelplt->output_section
3595 || s == hash_table->splt->output_section))
3596 {
3597 *pp = s->next;
3598 info->output_bfd->section_count--;
3599 strip_zero_sized = TRUE;
3600 if (s == rela_dyn)
3601 s = rela_dyn;
3602 if (s == rel_dyn)
3603 s = rel_dyn;
3604 else if (s == hash_table->splt->output_section)
3605 {
3606 s = hash_table->splt;
3607 strip_zero_sized_plt = TRUE;
3608 }
3609 else
3610 s = hash_table->srelplt;
3611 s->flags |= SEC_EXCLUDE;
3612 s->output_section = bfd_abs_section_ptr;
3613 }
3614 else
3615 pp = &s->next;
3616
3617 if (strip_zero_sized_plt)
3618 for (extdyn = sdynamic->contents;
3619 extdyn < sdynamic->contents + sdynamic->size;
3620 extdyn = next)
3621 {
3622 next = extdyn + bed->s->sizeof_dyn;
3623 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3624 switch (dyn.d_tag)
3625 {
3626 default:
3627 break;
3628 case DT_JMPREL:
3629 case DT_PLTRELSZ:
3630 case DT_PLTREL:
3631 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3632 the procedure linkage table (the .plt section) has been
3633 removed. */
3634 memmove (extdyn, next,
3635 sdynamic->size - (next - sdynamic->contents));
3636 next = extdyn;
3637 }
3638 }
3639
3640 if (strip_zero_sized)
3641 {
3642 /* Regenerate program headers. */
3643 elf_seg_map (info->output_bfd) = NULL;
3644 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3645 }
3646
3647 return TRUE;
3648 }
3649
3650 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3651 1 if a DT_NEEDED tag already exists, and 0 on success. */
3652
3653 int
3654 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3655 {
3656 struct elf_link_hash_table *hash_table;
3657 size_t strindex;
3658 const char *soname;
3659
3660 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3661 return -1;
3662
3663 hash_table = elf_hash_table (info);
3664 soname = elf_dt_name (abfd);
3665 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3666 if (strindex == (size_t) -1)
3667 return -1;
3668
3669 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3670 {
3671 asection *sdyn;
3672 const struct elf_backend_data *bed;
3673 bfd_byte *extdyn;
3674
3675 bed = get_elf_backend_data (hash_table->dynobj);
3676 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3677 if (sdyn != NULL)
3678 for (extdyn = sdyn->contents;
3679 extdyn < sdyn->contents + sdyn->size;
3680 extdyn += bed->s->sizeof_dyn)
3681 {
3682 Elf_Internal_Dyn dyn;
3683
3684 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3685 if (dyn.d_tag == DT_NEEDED
3686 && dyn.d_un.d_val == strindex)
3687 {
3688 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3689 return 1;
3690 }
3691 }
3692 }
3693
3694 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3695 return -1;
3696
3697 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3698 return -1;
3699
3700 return 0;
3701 }
3702
3703 /* Return true if SONAME is on the needed list between NEEDED and STOP
3704 (or the end of list if STOP is NULL), and needed by a library that
3705 will be loaded. */
3706
3707 static bfd_boolean
3708 on_needed_list (const char *soname,
3709 struct bfd_link_needed_list *needed,
3710 struct bfd_link_needed_list *stop)
3711 {
3712 struct bfd_link_needed_list *look;
3713 for (look = needed; look != stop; look = look->next)
3714 if (strcmp (soname, look->name) == 0
3715 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3716 /* If needed by a library that itself is not directly
3717 needed, recursively check whether that library is
3718 indirectly needed. Since we add DT_NEEDED entries to
3719 the end of the list, library dependencies appear after
3720 the library. Therefore search prior to the current
3721 LOOK, preventing possible infinite recursion. */
3722 || on_needed_list (elf_dt_name (look->by), needed, look)))
3723 return TRUE;
3724
3725 return FALSE;
3726 }
3727
3728 /* Sort symbol by value, section, size, and type. */
3729 static int
3730 elf_sort_symbol (const void *arg1, const void *arg2)
3731 {
3732 const struct elf_link_hash_entry *h1;
3733 const struct elf_link_hash_entry *h2;
3734 bfd_signed_vma vdiff;
3735 int sdiff;
3736 const char *n1;
3737 const char *n2;
3738
3739 h1 = *(const struct elf_link_hash_entry **) arg1;
3740 h2 = *(const struct elf_link_hash_entry **) arg2;
3741 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3742 if (vdiff != 0)
3743 return vdiff > 0 ? 1 : -1;
3744
3745 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3746 if (sdiff != 0)
3747 return sdiff;
3748
3749 /* Sort so that sized symbols are selected over zero size symbols. */
3750 vdiff = h1->size - h2->size;
3751 if (vdiff != 0)
3752 return vdiff > 0 ? 1 : -1;
3753
3754 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3755 if (h1->type != h2->type)
3756 return h1->type - h2->type;
3757
3758 /* If symbols are properly sized and typed, and multiple strong
3759 aliases are not defined in a shared library by the user we
3760 shouldn't get here. Unfortunately linker script symbols like
3761 __bss_start sometimes match a user symbol defined at the start of
3762 .bss without proper size and type. We'd like to preference the
3763 user symbol over reserved system symbols. Sort on leading
3764 underscores. */
3765 n1 = h1->root.root.string;
3766 n2 = h2->root.root.string;
3767 while (*n1 == *n2)
3768 {
3769 if (*n1 == 0)
3770 break;
3771 ++n1;
3772 ++n2;
3773 }
3774 if (*n1 == '_')
3775 return -1;
3776 if (*n2 == '_')
3777 return 1;
3778
3779 /* Final sort on name selects user symbols like '_u' over reserved
3780 system symbols like '_Z' and also will avoid qsort instability. */
3781 return *n1 - *n2;
3782 }
3783
3784 /* This function is used to adjust offsets into .dynstr for
3785 dynamic symbols. This is called via elf_link_hash_traverse. */
3786
3787 static bfd_boolean
3788 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3789 {
3790 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3791
3792 if (h->dynindx != -1)
3793 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3794 return TRUE;
3795 }
3796
3797 /* Assign string offsets in .dynstr, update all structures referencing
3798 them. */
3799
3800 static bfd_boolean
3801 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3802 {
3803 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3804 struct elf_link_local_dynamic_entry *entry;
3805 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3806 bfd *dynobj = hash_table->dynobj;
3807 asection *sdyn;
3808 bfd_size_type size;
3809 const struct elf_backend_data *bed;
3810 bfd_byte *extdyn;
3811
3812 _bfd_elf_strtab_finalize (dynstr);
3813 size = _bfd_elf_strtab_size (dynstr);
3814
3815 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3816
3817 if (info->callbacks->examine_strtab)
3818 info->callbacks->examine_strtab (dynstr);
3819
3820 bed = get_elf_backend_data (dynobj);
3821 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3822 BFD_ASSERT (sdyn != NULL);
3823
3824 /* Update all .dynamic entries referencing .dynstr strings. */
3825 for (extdyn = sdyn->contents;
3826 extdyn < sdyn->contents + sdyn->size;
3827 extdyn += bed->s->sizeof_dyn)
3828 {
3829 Elf_Internal_Dyn dyn;
3830
3831 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3832 switch (dyn.d_tag)
3833 {
3834 case DT_STRSZ:
3835 dyn.d_un.d_val = size;
3836 break;
3837 case DT_NEEDED:
3838 case DT_SONAME:
3839 case DT_RPATH:
3840 case DT_RUNPATH:
3841 case DT_FILTER:
3842 case DT_AUXILIARY:
3843 case DT_AUDIT:
3844 case DT_DEPAUDIT:
3845 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3846 break;
3847 default:
3848 continue;
3849 }
3850 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3851 }
3852
3853 /* Now update local dynamic symbols. */
3854 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3855 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3856 entry->isym.st_name);
3857
3858 /* And the rest of dynamic symbols. */
3859 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3860
3861 /* Adjust version definitions. */
3862 if (elf_tdata (output_bfd)->cverdefs)
3863 {
3864 asection *s;
3865 bfd_byte *p;
3866 size_t i;
3867 Elf_Internal_Verdef def;
3868 Elf_Internal_Verdaux defaux;
3869
3870 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3871 p = s->contents;
3872 do
3873 {
3874 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3875 &def);
3876 p += sizeof (Elf_External_Verdef);
3877 if (def.vd_aux != sizeof (Elf_External_Verdef))
3878 continue;
3879 for (i = 0; i < def.vd_cnt; ++i)
3880 {
3881 _bfd_elf_swap_verdaux_in (output_bfd,
3882 (Elf_External_Verdaux *) p, &defaux);
3883 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3884 defaux.vda_name);
3885 _bfd_elf_swap_verdaux_out (output_bfd,
3886 &defaux, (Elf_External_Verdaux *) p);
3887 p += sizeof (Elf_External_Verdaux);
3888 }
3889 }
3890 while (def.vd_next);
3891 }
3892
3893 /* Adjust version references. */
3894 if (elf_tdata (output_bfd)->verref)
3895 {
3896 asection *s;
3897 bfd_byte *p;
3898 size_t i;
3899 Elf_Internal_Verneed need;
3900 Elf_Internal_Vernaux needaux;
3901
3902 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3903 p = s->contents;
3904 do
3905 {
3906 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3907 &need);
3908 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3909 _bfd_elf_swap_verneed_out (output_bfd, &need,
3910 (Elf_External_Verneed *) p);
3911 p += sizeof (Elf_External_Verneed);
3912 for (i = 0; i < need.vn_cnt; ++i)
3913 {
3914 _bfd_elf_swap_vernaux_in (output_bfd,
3915 (Elf_External_Vernaux *) p, &needaux);
3916 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3917 needaux.vna_name);
3918 _bfd_elf_swap_vernaux_out (output_bfd,
3919 &needaux,
3920 (Elf_External_Vernaux *) p);
3921 p += sizeof (Elf_External_Vernaux);
3922 }
3923 }
3924 while (need.vn_next);
3925 }
3926
3927 return TRUE;
3928 }
3929 \f
3930 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3931 The default is to only match when the INPUT and OUTPUT are exactly
3932 the same target. */
3933
3934 bfd_boolean
3935 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3936 const bfd_target *output)
3937 {
3938 return input == output;
3939 }
3940
3941 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3942 This version is used when different targets for the same architecture
3943 are virtually identical. */
3944
3945 bfd_boolean
3946 _bfd_elf_relocs_compatible (const bfd_target *input,
3947 const bfd_target *output)
3948 {
3949 const struct elf_backend_data *obed, *ibed;
3950
3951 if (input == output)
3952 return TRUE;
3953
3954 ibed = xvec_get_elf_backend_data (input);
3955 obed = xvec_get_elf_backend_data (output);
3956
3957 if (ibed->arch != obed->arch)
3958 return FALSE;
3959
3960 /* If both backends are using this function, deem them compatible. */
3961 return ibed->relocs_compatible == obed->relocs_compatible;
3962 }
3963
3964 /* Make a special call to the linker "notice" function to tell it that
3965 we are about to handle an as-needed lib, or have finished
3966 processing the lib. */
3967
3968 bfd_boolean
3969 _bfd_elf_notice_as_needed (bfd *ibfd,
3970 struct bfd_link_info *info,
3971 enum notice_asneeded_action act)
3972 {
3973 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3974 }
3975
3976 /* Check relocations an ELF object file. */
3977
3978 bfd_boolean
3979 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3980 {
3981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3982 struct elf_link_hash_table *htab = elf_hash_table (info);
3983
3984 /* If this object is the same format as the output object, and it is
3985 not a shared library, then let the backend look through the
3986 relocs.
3987
3988 This is required to build global offset table entries and to
3989 arrange for dynamic relocs. It is not required for the
3990 particular common case of linking non PIC code, even when linking
3991 against shared libraries, but unfortunately there is no way of
3992 knowing whether an object file has been compiled PIC or not.
3993 Looking through the relocs is not particularly time consuming.
3994 The problem is that we must either (1) keep the relocs in memory,
3995 which causes the linker to require additional runtime memory or
3996 (2) read the relocs twice from the input file, which wastes time.
3997 This would be a good case for using mmap.
3998
3999 I have no idea how to handle linking PIC code into a file of a
4000 different format. It probably can't be done. */
4001 if ((abfd->flags & DYNAMIC) == 0
4002 && is_elf_hash_table (htab)
4003 && bed->check_relocs != NULL
4004 && elf_object_id (abfd) == elf_hash_table_id (htab)
4005 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4006 {
4007 asection *o;
4008
4009 for (o = abfd->sections; o != NULL; o = o->next)
4010 {
4011 Elf_Internal_Rela *internal_relocs;
4012 bfd_boolean ok;
4013
4014 /* Don't check relocations in excluded sections. Don't do
4015 anything special with non-loaded, non-alloced sections.
4016 In particular, any relocs in such sections should not
4017 affect GOT and PLT reference counting (ie. we don't
4018 allow them to create GOT or PLT entries), there's no
4019 possibility or desire to optimize TLS relocs, and
4020 there's not much point in propagating relocs to shared
4021 libs that the dynamic linker won't relocate. */
4022 if ((o->flags & SEC_ALLOC) == 0
4023 || (o->flags & SEC_RELOC) == 0
4024 || (o->flags & SEC_EXCLUDE) != 0
4025 || o->reloc_count == 0
4026 || ((info->strip == strip_all || info->strip == strip_debugger)
4027 && (o->flags & SEC_DEBUGGING) != 0)
4028 || bfd_is_abs_section (o->output_section))
4029 continue;
4030
4031 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4032 info->keep_memory);
4033 if (internal_relocs == NULL)
4034 return FALSE;
4035
4036 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4037
4038 if (elf_section_data (o)->relocs != internal_relocs)
4039 free (internal_relocs);
4040
4041 if (! ok)
4042 return FALSE;
4043 }
4044 }
4045
4046 return TRUE;
4047 }
4048
4049 /* Add symbols from an ELF object file to the linker hash table. */
4050
4051 static bfd_boolean
4052 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4053 {
4054 Elf_Internal_Ehdr *ehdr;
4055 Elf_Internal_Shdr *hdr;
4056 size_t symcount;
4057 size_t extsymcount;
4058 size_t extsymoff;
4059 struct elf_link_hash_entry **sym_hash;
4060 bfd_boolean dynamic;
4061 Elf_External_Versym *extversym = NULL;
4062 Elf_External_Versym *extversym_end = NULL;
4063 Elf_External_Versym *ever;
4064 struct elf_link_hash_entry *weaks;
4065 struct elf_link_hash_entry **nondeflt_vers = NULL;
4066 size_t nondeflt_vers_cnt = 0;
4067 Elf_Internal_Sym *isymbuf = NULL;
4068 Elf_Internal_Sym *isym;
4069 Elf_Internal_Sym *isymend;
4070 const struct elf_backend_data *bed;
4071 bfd_boolean add_needed;
4072 struct elf_link_hash_table *htab;
4073 void *alloc_mark = NULL;
4074 struct bfd_hash_entry **old_table = NULL;
4075 unsigned int old_size = 0;
4076 unsigned int old_count = 0;
4077 void *old_tab = NULL;
4078 void *old_ent;
4079 struct bfd_link_hash_entry *old_undefs = NULL;
4080 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4081 void *old_strtab = NULL;
4082 size_t tabsize = 0;
4083 asection *s;
4084 bfd_boolean just_syms;
4085
4086 htab = elf_hash_table (info);
4087 bed = get_elf_backend_data (abfd);
4088
4089 if ((abfd->flags & DYNAMIC) == 0)
4090 dynamic = FALSE;
4091 else
4092 {
4093 dynamic = TRUE;
4094
4095 /* You can't use -r against a dynamic object. Also, there's no
4096 hope of using a dynamic object which does not exactly match
4097 the format of the output file. */
4098 if (bfd_link_relocatable (info)
4099 || !is_elf_hash_table (htab)
4100 || info->output_bfd->xvec != abfd->xvec)
4101 {
4102 if (bfd_link_relocatable (info))
4103 bfd_set_error (bfd_error_invalid_operation);
4104 else
4105 bfd_set_error (bfd_error_wrong_format);
4106 goto error_return;
4107 }
4108 }
4109
4110 ehdr = elf_elfheader (abfd);
4111 if (info->warn_alternate_em
4112 && bed->elf_machine_code != ehdr->e_machine
4113 && ((bed->elf_machine_alt1 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt1)
4115 || (bed->elf_machine_alt2 != 0
4116 && ehdr->e_machine == bed->elf_machine_alt2)))
4117 _bfd_error_handler
4118 /* xgettext:c-format */
4119 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4120 ehdr->e_machine, abfd, bed->elf_machine_code);
4121
4122 /* As a GNU extension, any input sections which are named
4123 .gnu.warning.SYMBOL are treated as warning symbols for the given
4124 symbol. This differs from .gnu.warning sections, which generate
4125 warnings when they are included in an output file. */
4126 /* PR 12761: Also generate this warning when building shared libraries. */
4127 for (s = abfd->sections; s != NULL; s = s->next)
4128 {
4129 const char *name;
4130
4131 name = bfd_section_name (s);
4132 if (CONST_STRNEQ (name, ".gnu.warning."))
4133 {
4134 char *msg;
4135 bfd_size_type sz;
4136
4137 name += sizeof ".gnu.warning." - 1;
4138
4139 /* If this is a shared object, then look up the symbol
4140 in the hash table. If it is there, and it is already
4141 been defined, then we will not be using the entry
4142 from this shared object, so we don't need to warn.
4143 FIXME: If we see the definition in a regular object
4144 later on, we will warn, but we shouldn't. The only
4145 fix is to keep track of what warnings we are supposed
4146 to emit, and then handle them all at the end of the
4147 link. */
4148 if (dynamic)
4149 {
4150 struct elf_link_hash_entry *h;
4151
4152 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4153
4154 /* FIXME: What about bfd_link_hash_common? */
4155 if (h != NULL
4156 && (h->root.type == bfd_link_hash_defined
4157 || h->root.type == bfd_link_hash_defweak))
4158 continue;
4159 }
4160
4161 sz = s->size;
4162 msg = (char *) bfd_alloc (abfd, sz + 1);
4163 if (msg == NULL)
4164 goto error_return;
4165
4166 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4167 goto error_return;
4168
4169 msg[sz] = '\0';
4170
4171 if (! (_bfd_generic_link_add_one_symbol
4172 (info, abfd, name, BSF_WARNING, s, 0, msg,
4173 FALSE, bed->collect, NULL)))
4174 goto error_return;
4175
4176 if (bfd_link_executable (info))
4177 {
4178 /* Clobber the section size so that the warning does
4179 not get copied into the output file. */
4180 s->size = 0;
4181
4182 /* Also set SEC_EXCLUDE, so that symbols defined in
4183 the warning section don't get copied to the output. */
4184 s->flags |= SEC_EXCLUDE;
4185 }
4186 }
4187 }
4188
4189 just_syms = ((s = abfd->sections) != NULL
4190 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4191
4192 add_needed = TRUE;
4193 if (! dynamic)
4194 {
4195 /* If we are creating a shared library, create all the dynamic
4196 sections immediately. We need to attach them to something,
4197 so we attach them to this BFD, provided it is the right
4198 format and is not from ld --just-symbols. Always create the
4199 dynamic sections for -E/--dynamic-list. FIXME: If there
4200 are no input BFD's of the same format as the output, we can't
4201 make a shared library. */
4202 if (!just_syms
4203 && (bfd_link_pic (info)
4204 || (!bfd_link_relocatable (info)
4205 && info->nointerp
4206 && (info->export_dynamic || info->dynamic)))
4207 && is_elf_hash_table (htab)
4208 && info->output_bfd->xvec == abfd->xvec
4209 && !htab->dynamic_sections_created)
4210 {
4211 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4212 goto error_return;
4213 }
4214 }
4215 else if (!is_elf_hash_table (htab))
4216 goto error_return;
4217 else
4218 {
4219 const char *soname = NULL;
4220 char *audit = NULL;
4221 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4222 const Elf_Internal_Phdr *phdr;
4223 struct elf_link_loaded_list *loaded_lib;
4224
4225 /* ld --just-symbols and dynamic objects don't mix very well.
4226 ld shouldn't allow it. */
4227 if (just_syms)
4228 abort ();
4229
4230 /* If this dynamic lib was specified on the command line with
4231 --as-needed in effect, then we don't want to add a DT_NEEDED
4232 tag unless the lib is actually used. Similary for libs brought
4233 in by another lib's DT_NEEDED. When --no-add-needed is used
4234 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4235 any dynamic library in DT_NEEDED tags in the dynamic lib at
4236 all. */
4237 add_needed = (elf_dyn_lib_class (abfd)
4238 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4239 | DYN_NO_NEEDED)) == 0;
4240
4241 s = bfd_get_section_by_name (abfd, ".dynamic");
4242 if (s != NULL)
4243 {
4244 bfd_byte *dynbuf;
4245 bfd_byte *extdyn;
4246 unsigned int elfsec;
4247 unsigned long shlink;
4248
4249 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4250 {
4251 error_free_dyn:
4252 free (dynbuf);
4253 goto error_return;
4254 }
4255
4256 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4257 if (elfsec == SHN_BAD)
4258 goto error_free_dyn;
4259 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4260
4261 for (extdyn = dynbuf;
4262 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4263 extdyn += bed->s->sizeof_dyn)
4264 {
4265 Elf_Internal_Dyn dyn;
4266
4267 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4268 if (dyn.d_tag == DT_SONAME)
4269 {
4270 unsigned int tagv = dyn.d_un.d_val;
4271 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4272 if (soname == NULL)
4273 goto error_free_dyn;
4274 }
4275 if (dyn.d_tag == DT_NEEDED)
4276 {
4277 struct bfd_link_needed_list *n, **pn;
4278 char *fnm, *anm;
4279 unsigned int tagv = dyn.d_un.d_val;
4280 size_t amt = sizeof (struct bfd_link_needed_list);
4281
4282 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4283 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4284 if (n == NULL || fnm == NULL)
4285 goto error_free_dyn;
4286 amt = strlen (fnm) + 1;
4287 anm = (char *) bfd_alloc (abfd, amt);
4288 if (anm == NULL)
4289 goto error_free_dyn;
4290 memcpy (anm, fnm, amt);
4291 n->name = anm;
4292 n->by = abfd;
4293 n->next = NULL;
4294 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4295 ;
4296 *pn = n;
4297 }
4298 if (dyn.d_tag == DT_RUNPATH)
4299 {
4300 struct bfd_link_needed_list *n, **pn;
4301 char *fnm, *anm;
4302 unsigned int tagv = dyn.d_un.d_val;
4303 size_t amt = sizeof (struct bfd_link_needed_list);
4304
4305 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4306 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4307 if (n == NULL || fnm == NULL)
4308 goto error_free_dyn;
4309 amt = strlen (fnm) + 1;
4310 anm = (char *) bfd_alloc (abfd, amt);
4311 if (anm == NULL)
4312 goto error_free_dyn;
4313 memcpy (anm, fnm, amt);
4314 n->name = anm;
4315 n->by = abfd;
4316 n->next = NULL;
4317 for (pn = & runpath;
4318 *pn != NULL;
4319 pn = &(*pn)->next)
4320 ;
4321 *pn = n;
4322 }
4323 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4324 if (!runpath && dyn.d_tag == DT_RPATH)
4325 {
4326 struct bfd_link_needed_list *n, **pn;
4327 char *fnm, *anm;
4328 unsigned int tagv = dyn.d_un.d_val;
4329 size_t amt = sizeof (struct bfd_link_needed_list);
4330
4331 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4332 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4333 if (n == NULL || fnm == NULL)
4334 goto error_free_dyn;
4335 amt = strlen (fnm) + 1;
4336 anm = (char *) bfd_alloc (abfd, amt);
4337 if (anm == NULL)
4338 goto error_free_dyn;
4339 memcpy (anm, fnm, amt);
4340 n->name = anm;
4341 n->by = abfd;
4342 n->next = NULL;
4343 for (pn = & rpath;
4344 *pn != NULL;
4345 pn = &(*pn)->next)
4346 ;
4347 *pn = n;
4348 }
4349 if (dyn.d_tag == DT_AUDIT)
4350 {
4351 unsigned int tagv = dyn.d_un.d_val;
4352 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4353 }
4354 }
4355
4356 free (dynbuf);
4357 }
4358
4359 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4360 frees all more recently bfd_alloc'd blocks as well. */
4361 if (runpath)
4362 rpath = runpath;
4363
4364 if (rpath)
4365 {
4366 struct bfd_link_needed_list **pn;
4367 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4368 ;
4369 *pn = rpath;
4370 }
4371
4372 /* If we have a PT_GNU_RELRO program header, mark as read-only
4373 all sections contained fully therein. This makes relro
4374 shared library sections appear as they will at run-time. */
4375 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4376 while (phdr-- > elf_tdata (abfd)->phdr)
4377 if (phdr->p_type == PT_GNU_RELRO)
4378 {
4379 for (s = abfd->sections; s != NULL; s = s->next)
4380 {
4381 unsigned int opb = bfd_octets_per_byte (abfd, s);
4382
4383 if ((s->flags & SEC_ALLOC) != 0
4384 && s->vma * opb >= phdr->p_vaddr
4385 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4386 s->flags |= SEC_READONLY;
4387 }
4388 break;
4389 }
4390
4391 /* We do not want to include any of the sections in a dynamic
4392 object in the output file. We hack by simply clobbering the
4393 list of sections in the BFD. This could be handled more
4394 cleanly by, say, a new section flag; the existing
4395 SEC_NEVER_LOAD flag is not the one we want, because that one
4396 still implies that the section takes up space in the output
4397 file. */
4398 bfd_section_list_clear (abfd);
4399
4400 /* Find the name to use in a DT_NEEDED entry that refers to this
4401 object. If the object has a DT_SONAME entry, we use it.
4402 Otherwise, if the generic linker stuck something in
4403 elf_dt_name, we use that. Otherwise, we just use the file
4404 name. */
4405 if (soname == NULL || *soname == '\0')
4406 {
4407 soname = elf_dt_name (abfd);
4408 if (soname == NULL || *soname == '\0')
4409 soname = bfd_get_filename (abfd);
4410 }
4411
4412 /* Save the SONAME because sometimes the linker emulation code
4413 will need to know it. */
4414 elf_dt_name (abfd) = soname;
4415
4416 /* If we have already included this dynamic object in the
4417 link, just ignore it. There is no reason to include a
4418 particular dynamic object more than once. */
4419 for (loaded_lib = htab->dyn_loaded;
4420 loaded_lib != NULL;
4421 loaded_lib = loaded_lib->next)
4422 {
4423 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4424 return TRUE;
4425 }
4426
4427 /* Create dynamic sections for backends that require that be done
4428 before setup_gnu_properties. */
4429 if (add_needed
4430 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4431 return FALSE;
4432
4433 /* Save the DT_AUDIT entry for the linker emulation code. */
4434 elf_dt_audit (abfd) = audit;
4435 }
4436
4437 /* If this is a dynamic object, we always link against the .dynsym
4438 symbol table, not the .symtab symbol table. The dynamic linker
4439 will only see the .dynsym symbol table, so there is no reason to
4440 look at .symtab for a dynamic object. */
4441
4442 if (! dynamic || elf_dynsymtab (abfd) == 0)
4443 hdr = &elf_tdata (abfd)->symtab_hdr;
4444 else
4445 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4446
4447 symcount = hdr->sh_size / bed->s->sizeof_sym;
4448
4449 /* The sh_info field of the symtab header tells us where the
4450 external symbols start. We don't care about the local symbols at
4451 this point. */
4452 if (elf_bad_symtab (abfd))
4453 {
4454 extsymcount = symcount;
4455 extsymoff = 0;
4456 }
4457 else
4458 {
4459 extsymcount = symcount - hdr->sh_info;
4460 extsymoff = hdr->sh_info;
4461 }
4462
4463 sym_hash = elf_sym_hashes (abfd);
4464 if (extsymcount != 0)
4465 {
4466 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4467 NULL, NULL, NULL);
4468 if (isymbuf == NULL)
4469 goto error_return;
4470
4471 if (sym_hash == NULL)
4472 {
4473 /* We store a pointer to the hash table entry for each
4474 external symbol. */
4475 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4476 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4477 if (sym_hash == NULL)
4478 goto error_free_sym;
4479 elf_sym_hashes (abfd) = sym_hash;
4480 }
4481 }
4482
4483 if (dynamic)
4484 {
4485 /* Read in any version definitions. */
4486 if (!_bfd_elf_slurp_version_tables (abfd,
4487 info->default_imported_symver))
4488 goto error_free_sym;
4489
4490 /* Read in the symbol versions, but don't bother to convert them
4491 to internal format. */
4492 if (elf_dynversym (abfd) != 0)
4493 {
4494 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4495 bfd_size_type amt = versymhdr->sh_size;
4496
4497 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4498 goto error_free_sym;
4499 extversym = (Elf_External_Versym *)
4500 _bfd_malloc_and_read (abfd, amt, amt);
4501 if (extversym == NULL)
4502 goto error_free_sym;
4503 extversym_end = extversym + amt / sizeof (*extversym);
4504 }
4505 }
4506
4507 /* If we are loading an as-needed shared lib, save the symbol table
4508 state before we start adding symbols. If the lib turns out
4509 to be unneeded, restore the state. */
4510 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4511 {
4512 unsigned int i;
4513 size_t entsize;
4514
4515 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4516 {
4517 struct bfd_hash_entry *p;
4518 struct elf_link_hash_entry *h;
4519
4520 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4521 {
4522 h = (struct elf_link_hash_entry *) p;
4523 entsize += htab->root.table.entsize;
4524 if (h->root.type == bfd_link_hash_warning)
4525 {
4526 entsize += htab->root.table.entsize;
4527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4528 }
4529 if (h->root.type == bfd_link_hash_common)
4530 entsize += sizeof (*h->root.u.c.p);
4531 }
4532 }
4533
4534 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4535 old_tab = bfd_malloc (tabsize + entsize);
4536 if (old_tab == NULL)
4537 goto error_free_vers;
4538
4539 /* Remember the current objalloc pointer, so that all mem for
4540 symbols added can later be reclaimed. */
4541 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4542 if (alloc_mark == NULL)
4543 goto error_free_vers;
4544
4545 /* Make a special call to the linker "notice" function to
4546 tell it that we are about to handle an as-needed lib. */
4547 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4548 goto error_free_vers;
4549
4550 /* Clone the symbol table. Remember some pointers into the
4551 symbol table, and dynamic symbol count. */
4552 old_ent = (char *) old_tab + tabsize;
4553 memcpy (old_tab, htab->root.table.table, tabsize);
4554 old_undefs = htab->root.undefs;
4555 old_undefs_tail = htab->root.undefs_tail;
4556 old_table = htab->root.table.table;
4557 old_size = htab->root.table.size;
4558 old_count = htab->root.table.count;
4559 old_strtab = NULL;
4560 if (htab->dynstr != NULL)
4561 {
4562 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4563 if (old_strtab == NULL)
4564 goto error_free_vers;
4565 }
4566
4567 for (i = 0; i < htab->root.table.size; i++)
4568 {
4569 struct bfd_hash_entry *p;
4570 struct elf_link_hash_entry *h;
4571
4572 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4573 {
4574 h = (struct elf_link_hash_entry *) p;
4575 memcpy (old_ent, h, htab->root.table.entsize);
4576 old_ent = (char *) old_ent + htab->root.table.entsize;
4577 if (h->root.type == bfd_link_hash_warning)
4578 {
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4580 memcpy (old_ent, h, htab->root.table.entsize);
4581 old_ent = (char *) old_ent + htab->root.table.entsize;
4582 }
4583 if (h->root.type == bfd_link_hash_common)
4584 {
4585 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4586 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4587 }
4588 }
4589 }
4590 }
4591
4592 weaks = NULL;
4593 if (extversym == NULL)
4594 ever = NULL;
4595 else if (extversym + extsymoff < extversym_end)
4596 ever = extversym + extsymoff;
4597 else
4598 {
4599 /* xgettext:c-format */
4600 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4601 abfd, (long) extsymoff,
4602 (long) (extversym_end - extversym) / sizeof (* extversym));
4603 bfd_set_error (bfd_error_bad_value);
4604 goto error_free_vers;
4605 }
4606
4607 if (!bfd_link_relocatable (info)
4608 && abfd->lto_slim_object)
4609 {
4610 _bfd_error_handler
4611 (_("%pB: plugin needed to handle lto object"), abfd);
4612 }
4613
4614 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4615 isym < isymend;
4616 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4617 {
4618 int bind;
4619 bfd_vma value;
4620 asection *sec, *new_sec;
4621 flagword flags;
4622 const char *name;
4623 struct elf_link_hash_entry *h;
4624 struct elf_link_hash_entry *hi;
4625 bfd_boolean definition;
4626 bfd_boolean size_change_ok;
4627 bfd_boolean type_change_ok;
4628 bfd_boolean new_weak;
4629 bfd_boolean old_weak;
4630 bfd *override;
4631 bfd_boolean common;
4632 bfd_boolean discarded;
4633 unsigned int old_alignment;
4634 unsigned int shindex;
4635 bfd *old_bfd;
4636 bfd_boolean matched;
4637
4638 override = NULL;
4639
4640 flags = BSF_NO_FLAGS;
4641 sec = NULL;
4642 value = isym->st_value;
4643 common = bed->common_definition (isym);
4644 if (common && info->inhibit_common_definition)
4645 {
4646 /* Treat common symbol as undefined for --no-define-common. */
4647 isym->st_shndx = SHN_UNDEF;
4648 common = FALSE;
4649 }
4650 discarded = FALSE;
4651
4652 bind = ELF_ST_BIND (isym->st_info);
4653 switch (bind)
4654 {
4655 case STB_LOCAL:
4656 /* This should be impossible, since ELF requires that all
4657 global symbols follow all local symbols, and that sh_info
4658 point to the first global symbol. Unfortunately, Irix 5
4659 screws this up. */
4660 if (elf_bad_symtab (abfd))
4661 continue;
4662
4663 /* If we aren't prepared to handle locals within the globals
4664 then we'll likely segfault on a NULL symbol hash if the
4665 symbol is ever referenced in relocations. */
4666 shindex = elf_elfheader (abfd)->e_shstrndx;
4667 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4668 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4669 " (>= sh_info of %lu)"),
4670 abfd, name, (long) (isym - isymbuf + extsymoff),
4671 (long) extsymoff);
4672
4673 /* Dynamic object relocations are not processed by ld, so
4674 ld won't run into the problem mentioned above. */
4675 if (dynamic)
4676 continue;
4677 bfd_set_error (bfd_error_bad_value);
4678 goto error_free_vers;
4679
4680 case STB_GLOBAL:
4681 if (isym->st_shndx != SHN_UNDEF && !common)
4682 flags = BSF_GLOBAL;
4683 break;
4684
4685 case STB_WEAK:
4686 flags = BSF_WEAK;
4687 break;
4688
4689 case STB_GNU_UNIQUE:
4690 flags = BSF_GNU_UNIQUE;
4691 break;
4692
4693 default:
4694 /* Leave it up to the processor backend. */
4695 break;
4696 }
4697
4698 if (isym->st_shndx == SHN_UNDEF)
4699 sec = bfd_und_section_ptr;
4700 else if (isym->st_shndx == SHN_ABS)
4701 sec = bfd_abs_section_ptr;
4702 else if (isym->st_shndx == SHN_COMMON)
4703 {
4704 sec = bfd_com_section_ptr;
4705 /* What ELF calls the size we call the value. What ELF
4706 calls the value we call the alignment. */
4707 value = isym->st_size;
4708 }
4709 else
4710 {
4711 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4712 if (sec == NULL)
4713 sec = bfd_abs_section_ptr;
4714 else if (discarded_section (sec))
4715 {
4716 /* Symbols from discarded section are undefined. We keep
4717 its visibility. */
4718 sec = bfd_und_section_ptr;
4719 discarded = TRUE;
4720 isym->st_shndx = SHN_UNDEF;
4721 }
4722 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4723 value -= sec->vma;
4724 }
4725
4726 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4727 isym->st_name);
4728 if (name == NULL)
4729 goto error_free_vers;
4730
4731 if (isym->st_shndx == SHN_COMMON
4732 && (abfd->flags & BFD_PLUGIN) != 0)
4733 {
4734 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4735
4736 if (xc == NULL)
4737 {
4738 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4739 | SEC_EXCLUDE);
4740 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4741 if (xc == NULL)
4742 goto error_free_vers;
4743 }
4744 sec = xc;
4745 }
4746 else if (isym->st_shndx == SHN_COMMON
4747 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4748 && !bfd_link_relocatable (info))
4749 {
4750 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4751
4752 if (tcomm == NULL)
4753 {
4754 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4755 | SEC_LINKER_CREATED);
4756 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4757 if (tcomm == NULL)
4758 goto error_free_vers;
4759 }
4760 sec = tcomm;
4761 }
4762 else if (bed->elf_add_symbol_hook)
4763 {
4764 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4765 &sec, &value))
4766 goto error_free_vers;
4767
4768 /* The hook function sets the name to NULL if this symbol
4769 should be skipped for some reason. */
4770 if (name == NULL)
4771 continue;
4772 }
4773
4774 /* Sanity check that all possibilities were handled. */
4775 if (sec == NULL)
4776 abort ();
4777
4778 /* Silently discard TLS symbols from --just-syms. There's
4779 no way to combine a static TLS block with a new TLS block
4780 for this executable. */
4781 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4782 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4783 continue;
4784
4785 if (bfd_is_und_section (sec)
4786 || bfd_is_com_section (sec))
4787 definition = FALSE;
4788 else
4789 definition = TRUE;
4790
4791 size_change_ok = FALSE;
4792 type_change_ok = bed->type_change_ok;
4793 old_weak = FALSE;
4794 matched = FALSE;
4795 old_alignment = 0;
4796 old_bfd = NULL;
4797 new_sec = sec;
4798
4799 if (is_elf_hash_table (htab))
4800 {
4801 Elf_Internal_Versym iver;
4802 unsigned int vernum = 0;
4803 bfd_boolean skip;
4804
4805 if (ever == NULL)
4806 {
4807 if (info->default_imported_symver)
4808 /* Use the default symbol version created earlier. */
4809 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4810 else
4811 iver.vs_vers = 0;
4812 }
4813 else if (ever >= extversym_end)
4814 {
4815 /* xgettext:c-format */
4816 _bfd_error_handler (_("%pB: not enough version information"),
4817 abfd);
4818 bfd_set_error (bfd_error_bad_value);
4819 goto error_free_vers;
4820 }
4821 else
4822 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4823
4824 vernum = iver.vs_vers & VERSYM_VERSION;
4825
4826 /* If this is a hidden symbol, or if it is not version
4827 1, we append the version name to the symbol name.
4828 However, we do not modify a non-hidden absolute symbol
4829 if it is not a function, because it might be the version
4830 symbol itself. FIXME: What if it isn't? */
4831 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4832 || (vernum > 1
4833 && (!bfd_is_abs_section (sec)
4834 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4835 {
4836 const char *verstr;
4837 size_t namelen, verlen, newlen;
4838 char *newname, *p;
4839
4840 if (isym->st_shndx != SHN_UNDEF)
4841 {
4842 if (vernum > elf_tdata (abfd)->cverdefs)
4843 verstr = NULL;
4844 else if (vernum > 1)
4845 verstr =
4846 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4847 else
4848 verstr = "";
4849
4850 if (verstr == NULL)
4851 {
4852 _bfd_error_handler
4853 /* xgettext:c-format */
4854 (_("%pB: %s: invalid version %u (max %d)"),
4855 abfd, name, vernum,
4856 elf_tdata (abfd)->cverdefs);
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4859 }
4860 }
4861 else
4862 {
4863 /* We cannot simply test for the number of
4864 entries in the VERNEED section since the
4865 numbers for the needed versions do not start
4866 at 0. */
4867 Elf_Internal_Verneed *t;
4868
4869 verstr = NULL;
4870 for (t = elf_tdata (abfd)->verref;
4871 t != NULL;
4872 t = t->vn_nextref)
4873 {
4874 Elf_Internal_Vernaux *a;
4875
4876 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4877 {
4878 if (a->vna_other == vernum)
4879 {
4880 verstr = a->vna_nodename;
4881 break;
4882 }
4883 }
4884 if (a != NULL)
4885 break;
4886 }
4887 if (verstr == NULL)
4888 {
4889 _bfd_error_handler
4890 /* xgettext:c-format */
4891 (_("%pB: %s: invalid needed version %d"),
4892 abfd, name, vernum);
4893 bfd_set_error (bfd_error_bad_value);
4894 goto error_free_vers;
4895 }
4896 }
4897
4898 namelen = strlen (name);
4899 verlen = strlen (verstr);
4900 newlen = namelen + verlen + 2;
4901 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4902 && isym->st_shndx != SHN_UNDEF)
4903 ++newlen;
4904
4905 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4906 if (newname == NULL)
4907 goto error_free_vers;
4908 memcpy (newname, name, namelen);
4909 p = newname + namelen;
4910 *p++ = ELF_VER_CHR;
4911 /* If this is a defined non-hidden version symbol,
4912 we add another @ to the name. This indicates the
4913 default version of the symbol. */
4914 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4915 && isym->st_shndx != SHN_UNDEF)
4916 *p++ = ELF_VER_CHR;
4917 memcpy (p, verstr, verlen + 1);
4918
4919 name = newname;
4920 }
4921
4922 /* If this symbol has default visibility and the user has
4923 requested we not re-export it, then mark it as hidden. */
4924 if (!bfd_is_und_section (sec)
4925 && !dynamic
4926 && abfd->no_export
4927 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4928 isym->st_other = (STV_HIDDEN
4929 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4930
4931 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4932 sym_hash, &old_bfd, &old_weak,
4933 &old_alignment, &skip, &override,
4934 &type_change_ok, &size_change_ok,
4935 &matched))
4936 goto error_free_vers;
4937
4938 if (skip)
4939 continue;
4940
4941 /* Override a definition only if the new symbol matches the
4942 existing one. */
4943 if (override && matched)
4944 definition = FALSE;
4945
4946 h = *sym_hash;
4947 while (h->root.type == bfd_link_hash_indirect
4948 || h->root.type == bfd_link_hash_warning)
4949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4950
4951 if (elf_tdata (abfd)->verdef != NULL
4952 && vernum > 1
4953 && definition)
4954 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4955 }
4956
4957 if (! (_bfd_generic_link_add_one_symbol
4958 (info, override ? override : abfd, name, flags, sec, value,
4959 NULL, FALSE, bed->collect,
4960 (struct bfd_link_hash_entry **) sym_hash)))
4961 goto error_free_vers;
4962
4963 h = *sym_hash;
4964 /* We need to make sure that indirect symbol dynamic flags are
4965 updated. */
4966 hi = h;
4967 while (h->root.type == bfd_link_hash_indirect
4968 || h->root.type == bfd_link_hash_warning)
4969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4970
4971 /* Setting the index to -3 tells elf_link_output_extsym that
4972 this symbol is defined in a discarded section. */
4973 if (discarded)
4974 h->indx = -3;
4975
4976 *sym_hash = h;
4977
4978 new_weak = (flags & BSF_WEAK) != 0;
4979 if (dynamic
4980 && definition
4981 && new_weak
4982 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4983 && is_elf_hash_table (htab)
4984 && h->u.alias == NULL)
4985 {
4986 /* Keep a list of all weak defined non function symbols from
4987 a dynamic object, using the alias field. Later in this
4988 function we will set the alias field to the correct
4989 value. We only put non-function symbols from dynamic
4990 objects on this list, because that happens to be the only
4991 time we need to know the normal symbol corresponding to a
4992 weak symbol, and the information is time consuming to
4993 figure out. If the alias field is not already NULL,
4994 then this symbol was already defined by some previous
4995 dynamic object, and we will be using that previous
4996 definition anyhow. */
4997
4998 h->u.alias = weaks;
4999 weaks = h;
5000 }
5001
5002 /* Set the alignment of a common symbol. */
5003 if ((common || bfd_is_com_section (sec))
5004 && h->root.type == bfd_link_hash_common)
5005 {
5006 unsigned int align;
5007
5008 if (common)
5009 align = bfd_log2 (isym->st_value);
5010 else
5011 {
5012 /* The new symbol is a common symbol in a shared object.
5013 We need to get the alignment from the section. */
5014 align = new_sec->alignment_power;
5015 }
5016 if (align > old_alignment)
5017 h->root.u.c.p->alignment_power = align;
5018 else
5019 h->root.u.c.p->alignment_power = old_alignment;
5020 }
5021
5022 if (is_elf_hash_table (htab))
5023 {
5024 /* Set a flag in the hash table entry indicating the type of
5025 reference or definition we just found. A dynamic symbol
5026 is one which is referenced or defined by both a regular
5027 object and a shared object. */
5028 bfd_boolean dynsym = FALSE;
5029
5030 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5031 if ((abfd->flags & BFD_PLUGIN) != 0)
5032 ;
5033 else if (!dynamic)
5034 {
5035 if (! definition)
5036 {
5037 h->ref_regular = 1;
5038 if (bind != STB_WEAK)
5039 h->ref_regular_nonweak = 1;
5040 }
5041 else
5042 {
5043 h->def_regular = 1;
5044 if (h->def_dynamic)
5045 {
5046 h->def_dynamic = 0;
5047 h->ref_dynamic = 1;
5048 }
5049 }
5050 }
5051 else
5052 {
5053 if (! definition)
5054 {
5055 h->ref_dynamic = 1;
5056 hi->ref_dynamic = 1;
5057 }
5058 else
5059 {
5060 h->def_dynamic = 1;
5061 hi->def_dynamic = 1;
5062 }
5063 }
5064
5065 /* If an indirect symbol has been forced local, don't
5066 make the real symbol dynamic. */
5067 if (h != hi && hi->forced_local)
5068 ;
5069 else if (!dynamic)
5070 {
5071 if (bfd_link_dll (info)
5072 || h->def_dynamic
5073 || h->ref_dynamic)
5074 dynsym = TRUE;
5075 }
5076 else
5077 {
5078 if (h->def_regular
5079 || h->ref_regular
5080 || (h->is_weakalias
5081 && weakdef (h)->dynindx != -1))
5082 dynsym = TRUE;
5083 }
5084
5085 /* Check to see if we need to add an indirect symbol for
5086 the default name. */
5087 if ((definition
5088 || (!override && h->root.type == bfd_link_hash_common))
5089 && !(hi != h
5090 && hi->versioned == versioned_hidden))
5091 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5092 sec, value, &old_bfd, &dynsym))
5093 goto error_free_vers;
5094
5095 /* Check the alignment when a common symbol is involved. This
5096 can change when a common symbol is overridden by a normal
5097 definition or a common symbol is ignored due to the old
5098 normal definition. We need to make sure the maximum
5099 alignment is maintained. */
5100 if ((old_alignment || common)
5101 && h->root.type != bfd_link_hash_common)
5102 {
5103 unsigned int common_align;
5104 unsigned int normal_align;
5105 unsigned int symbol_align;
5106 bfd *normal_bfd;
5107 bfd *common_bfd;
5108
5109 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5110 || h->root.type == bfd_link_hash_defweak);
5111
5112 symbol_align = ffs (h->root.u.def.value) - 1;
5113 if (h->root.u.def.section->owner != NULL
5114 && (h->root.u.def.section->owner->flags
5115 & (DYNAMIC | BFD_PLUGIN)) == 0)
5116 {
5117 normal_align = h->root.u.def.section->alignment_power;
5118 if (normal_align > symbol_align)
5119 normal_align = symbol_align;
5120 }
5121 else
5122 normal_align = symbol_align;
5123
5124 if (old_alignment)
5125 {
5126 common_align = old_alignment;
5127 common_bfd = old_bfd;
5128 normal_bfd = abfd;
5129 }
5130 else
5131 {
5132 common_align = bfd_log2 (isym->st_value);
5133 common_bfd = abfd;
5134 normal_bfd = old_bfd;
5135 }
5136
5137 if (normal_align < common_align)
5138 {
5139 /* PR binutils/2735 */
5140 if (normal_bfd == NULL)
5141 _bfd_error_handler
5142 /* xgettext:c-format */
5143 (_("warning: alignment %u of common symbol `%s' in %pB is"
5144 " greater than the alignment (%u) of its section %pA"),
5145 1 << common_align, name, common_bfd,
5146 1 << normal_align, h->root.u.def.section);
5147 else
5148 _bfd_error_handler
5149 /* xgettext:c-format */
5150 (_("warning: alignment %u of symbol `%s' in %pB"
5151 " is smaller than %u in %pB"),
5152 1 << normal_align, name, normal_bfd,
5153 1 << common_align, common_bfd);
5154 }
5155 }
5156
5157 /* Remember the symbol size if it isn't undefined. */
5158 if (isym->st_size != 0
5159 && isym->st_shndx != SHN_UNDEF
5160 && (definition || h->size == 0))
5161 {
5162 if (h->size != 0
5163 && h->size != isym->st_size
5164 && ! size_change_ok)
5165 _bfd_error_handler
5166 /* xgettext:c-format */
5167 (_("warning: size of symbol `%s' changed"
5168 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5169 name, (uint64_t) h->size, old_bfd,
5170 (uint64_t) isym->st_size, abfd);
5171
5172 h->size = isym->st_size;
5173 }
5174
5175 /* If this is a common symbol, then we always want H->SIZE
5176 to be the size of the common symbol. The code just above
5177 won't fix the size if a common symbol becomes larger. We
5178 don't warn about a size change here, because that is
5179 covered by --warn-common. Allow changes between different
5180 function types. */
5181 if (h->root.type == bfd_link_hash_common)
5182 h->size = h->root.u.c.size;
5183
5184 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5185 && ((definition && !new_weak)
5186 || (old_weak && h->root.type == bfd_link_hash_common)
5187 || h->type == STT_NOTYPE))
5188 {
5189 unsigned int type = ELF_ST_TYPE (isym->st_info);
5190
5191 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5192 symbol. */
5193 if (type == STT_GNU_IFUNC
5194 && (abfd->flags & DYNAMIC) != 0)
5195 type = STT_FUNC;
5196
5197 if (h->type != type)
5198 {
5199 if (h->type != STT_NOTYPE && ! type_change_ok)
5200 /* xgettext:c-format */
5201 _bfd_error_handler
5202 (_("warning: type of symbol `%s' changed"
5203 " from %d to %d in %pB"),
5204 name, h->type, type, abfd);
5205
5206 h->type = type;
5207 }
5208 }
5209
5210 /* Merge st_other field. */
5211 elf_merge_st_other (abfd, h, isym->st_other, sec,
5212 definition, dynamic);
5213
5214 /* We don't want to make debug symbol dynamic. */
5215 if (definition
5216 && (sec->flags & SEC_DEBUGGING)
5217 && !bfd_link_relocatable (info))
5218 dynsym = FALSE;
5219
5220 /* Nor should we make plugin symbols dynamic. */
5221 if ((abfd->flags & BFD_PLUGIN) != 0)
5222 dynsym = FALSE;
5223
5224 if (definition)
5225 {
5226 h->target_internal = isym->st_target_internal;
5227 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5228 }
5229
5230 if (definition && !dynamic)
5231 {
5232 char *p = strchr (name, ELF_VER_CHR);
5233 if (p != NULL && p[1] != ELF_VER_CHR)
5234 {
5235 /* Queue non-default versions so that .symver x, x@FOO
5236 aliases can be checked. */
5237 if (!nondeflt_vers)
5238 {
5239 size_t amt = ((isymend - isym + 1)
5240 * sizeof (struct elf_link_hash_entry *));
5241 nondeflt_vers
5242 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5243 if (!nondeflt_vers)
5244 goto error_free_vers;
5245 }
5246 nondeflt_vers[nondeflt_vers_cnt++] = h;
5247 }
5248 }
5249
5250 if (dynsym && h->dynindx == -1)
5251 {
5252 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5253 goto error_free_vers;
5254 if (h->is_weakalias
5255 && weakdef (h)->dynindx == -1)
5256 {
5257 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5258 goto error_free_vers;
5259 }
5260 }
5261 else if (h->dynindx != -1)
5262 /* If the symbol already has a dynamic index, but
5263 visibility says it should not be visible, turn it into
5264 a local symbol. */
5265 switch (ELF_ST_VISIBILITY (h->other))
5266 {
5267 case STV_INTERNAL:
5268 case STV_HIDDEN:
5269 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5270 dynsym = FALSE;
5271 break;
5272 }
5273
5274 if (!add_needed
5275 && matched
5276 && definition
5277 && h->root.type != bfd_link_hash_indirect
5278 && ((dynsym
5279 && h->ref_regular_nonweak)
5280 || (old_bfd != NULL
5281 && (old_bfd->flags & BFD_PLUGIN) != 0
5282 && bind != STB_WEAK)
5283 || (h->ref_dynamic_nonweak
5284 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5285 && !on_needed_list (elf_dt_name (abfd),
5286 htab->needed, NULL))))
5287 {
5288 const char *soname = elf_dt_name (abfd);
5289
5290 info->callbacks->minfo ("%!", soname, old_bfd,
5291 h->root.root.string);
5292
5293 /* A symbol from a library loaded via DT_NEEDED of some
5294 other library is referenced by a regular object.
5295 Add a DT_NEEDED entry for it. Issue an error if
5296 --no-add-needed is used and the reference was not
5297 a weak one. */
5298 if (old_bfd != NULL
5299 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5300 {
5301 _bfd_error_handler
5302 /* xgettext:c-format */
5303 (_("%pB: undefined reference to symbol '%s'"),
5304 old_bfd, name);
5305 bfd_set_error (bfd_error_missing_dso);
5306 goto error_free_vers;
5307 }
5308
5309 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5310 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5311
5312 /* Create dynamic sections for backends that require
5313 that be done before setup_gnu_properties. */
5314 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5315 return FALSE;
5316 add_needed = TRUE;
5317 }
5318 }
5319 }
5320
5321 if (info->lto_plugin_active
5322 && !bfd_link_relocatable (info)
5323 && (abfd->flags & BFD_PLUGIN) == 0
5324 && !just_syms
5325 && extsymcount)
5326 {
5327 int r_sym_shift;
5328
5329 if (bed->s->arch_size == 32)
5330 r_sym_shift = 8;
5331 else
5332 r_sym_shift = 32;
5333
5334 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5335 referenced in regular objects so that linker plugin will get
5336 the correct symbol resolution. */
5337
5338 sym_hash = elf_sym_hashes (abfd);
5339 for (s = abfd->sections; s != NULL; s = s->next)
5340 {
5341 Elf_Internal_Rela *internal_relocs;
5342 Elf_Internal_Rela *rel, *relend;
5343
5344 /* Don't check relocations in excluded sections. */
5345 if ((s->flags & SEC_RELOC) == 0
5346 || s->reloc_count == 0
5347 || (s->flags & SEC_EXCLUDE) != 0
5348 || ((info->strip == strip_all
5349 || info->strip == strip_debugger)
5350 && (s->flags & SEC_DEBUGGING) != 0))
5351 continue;
5352
5353 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5354 NULL,
5355 info->keep_memory);
5356 if (internal_relocs == NULL)
5357 goto error_free_vers;
5358
5359 rel = internal_relocs;
5360 relend = rel + s->reloc_count;
5361 for ( ; rel < relend; rel++)
5362 {
5363 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5364 struct elf_link_hash_entry *h;
5365
5366 /* Skip local symbols. */
5367 if (r_symndx < extsymoff)
5368 continue;
5369
5370 h = sym_hash[r_symndx - extsymoff];
5371 if (h != NULL)
5372 h->root.non_ir_ref_regular = 1;
5373 }
5374
5375 if (elf_section_data (s)->relocs != internal_relocs)
5376 free (internal_relocs);
5377 }
5378 }
5379
5380 free (extversym);
5381 extversym = NULL;
5382 free (isymbuf);
5383 isymbuf = NULL;
5384
5385 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5386 {
5387 unsigned int i;
5388
5389 /* Restore the symbol table. */
5390 old_ent = (char *) old_tab + tabsize;
5391 memset (elf_sym_hashes (abfd), 0,
5392 extsymcount * sizeof (struct elf_link_hash_entry *));
5393 htab->root.table.table = old_table;
5394 htab->root.table.size = old_size;
5395 htab->root.table.count = old_count;
5396 memcpy (htab->root.table.table, old_tab, tabsize);
5397 htab->root.undefs = old_undefs;
5398 htab->root.undefs_tail = old_undefs_tail;
5399 if (htab->dynstr != NULL)
5400 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5401 free (old_strtab);
5402 old_strtab = NULL;
5403 for (i = 0; i < htab->root.table.size; i++)
5404 {
5405 struct bfd_hash_entry *p;
5406 struct elf_link_hash_entry *h;
5407 unsigned int non_ir_ref_dynamic;
5408
5409 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5410 {
5411 /* Preserve non_ir_ref_dynamic so that this symbol
5412 will be exported when the dynamic lib becomes needed
5413 in the second pass. */
5414 h = (struct elf_link_hash_entry *) p;
5415 if (h->root.type == bfd_link_hash_warning)
5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5417 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5418
5419 h = (struct elf_link_hash_entry *) p;
5420 memcpy (h, old_ent, htab->root.table.entsize);
5421 old_ent = (char *) old_ent + htab->root.table.entsize;
5422 if (h->root.type == bfd_link_hash_warning)
5423 {
5424 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5425 memcpy (h, old_ent, htab->root.table.entsize);
5426 old_ent = (char *) old_ent + htab->root.table.entsize;
5427 }
5428 if (h->root.type == bfd_link_hash_common)
5429 {
5430 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5431 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5432 }
5433 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5434 }
5435 }
5436
5437 /* Make a special call to the linker "notice" function to
5438 tell it that symbols added for crefs may need to be removed. */
5439 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5440 goto error_free_vers;
5441
5442 free (old_tab);
5443 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5444 alloc_mark);
5445 free (nondeflt_vers);
5446 return TRUE;
5447 }
5448
5449 if (old_tab != NULL)
5450 {
5451 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5452 goto error_free_vers;
5453 free (old_tab);
5454 old_tab = NULL;
5455 }
5456
5457 /* Now that all the symbols from this input file are created, if
5458 not performing a relocatable link, handle .symver foo, foo@BAR
5459 such that any relocs against foo become foo@BAR. */
5460 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5461 {
5462 size_t cnt, symidx;
5463
5464 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5465 {
5466 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5467 char *shortname, *p;
5468 size_t amt;
5469
5470 p = strchr (h->root.root.string, ELF_VER_CHR);
5471 if (p == NULL
5472 || (h->root.type != bfd_link_hash_defined
5473 && h->root.type != bfd_link_hash_defweak))
5474 continue;
5475
5476 amt = p - h->root.root.string;
5477 shortname = (char *) bfd_malloc (amt + 1);
5478 if (!shortname)
5479 goto error_free_vers;
5480 memcpy (shortname, h->root.root.string, amt);
5481 shortname[amt] = '\0';
5482
5483 hi = (struct elf_link_hash_entry *)
5484 bfd_link_hash_lookup (&htab->root, shortname,
5485 FALSE, FALSE, FALSE);
5486 if (hi != NULL
5487 && hi->root.type == h->root.type
5488 && hi->root.u.def.value == h->root.u.def.value
5489 && hi->root.u.def.section == h->root.u.def.section)
5490 {
5491 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5492 hi->root.type = bfd_link_hash_indirect;
5493 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5494 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5495 sym_hash = elf_sym_hashes (abfd);
5496 if (sym_hash)
5497 for (symidx = 0; symidx < extsymcount; ++symidx)
5498 if (sym_hash[symidx] == hi)
5499 {
5500 sym_hash[symidx] = h;
5501 break;
5502 }
5503 }
5504 free (shortname);
5505 }
5506 free (nondeflt_vers);
5507 nondeflt_vers = NULL;
5508 }
5509
5510 /* Now set the alias field correctly for all the weak defined
5511 symbols we found. The only way to do this is to search all the
5512 symbols. Since we only need the information for non functions in
5513 dynamic objects, that's the only time we actually put anything on
5514 the list WEAKS. We need this information so that if a regular
5515 object refers to a symbol defined weakly in a dynamic object, the
5516 real symbol in the dynamic object is also put in the dynamic
5517 symbols; we also must arrange for both symbols to point to the
5518 same memory location. We could handle the general case of symbol
5519 aliasing, but a general symbol alias can only be generated in
5520 assembler code, handling it correctly would be very time
5521 consuming, and other ELF linkers don't handle general aliasing
5522 either. */
5523 if (weaks != NULL)
5524 {
5525 struct elf_link_hash_entry **hpp;
5526 struct elf_link_hash_entry **hppend;
5527 struct elf_link_hash_entry **sorted_sym_hash;
5528 struct elf_link_hash_entry *h;
5529 size_t sym_count, amt;
5530
5531 /* Since we have to search the whole symbol list for each weak
5532 defined symbol, search time for N weak defined symbols will be
5533 O(N^2). Binary search will cut it down to O(NlogN). */
5534 amt = extsymcount * sizeof (*sorted_sym_hash);
5535 sorted_sym_hash = bfd_malloc (amt);
5536 if (sorted_sym_hash == NULL)
5537 goto error_return;
5538 sym_hash = sorted_sym_hash;
5539 hpp = elf_sym_hashes (abfd);
5540 hppend = hpp + extsymcount;
5541 sym_count = 0;
5542 for (; hpp < hppend; hpp++)
5543 {
5544 h = *hpp;
5545 if (h != NULL
5546 && h->root.type == bfd_link_hash_defined
5547 && !bed->is_function_type (h->type))
5548 {
5549 *sym_hash = h;
5550 sym_hash++;
5551 sym_count++;
5552 }
5553 }
5554
5555 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5556 elf_sort_symbol);
5557
5558 while (weaks != NULL)
5559 {
5560 struct elf_link_hash_entry *hlook;
5561 asection *slook;
5562 bfd_vma vlook;
5563 size_t i, j, idx = 0;
5564
5565 hlook = weaks;
5566 weaks = hlook->u.alias;
5567 hlook->u.alias = NULL;
5568
5569 if (hlook->root.type != bfd_link_hash_defined
5570 && hlook->root.type != bfd_link_hash_defweak)
5571 continue;
5572
5573 slook = hlook->root.u.def.section;
5574 vlook = hlook->root.u.def.value;
5575
5576 i = 0;
5577 j = sym_count;
5578 while (i != j)
5579 {
5580 bfd_signed_vma vdiff;
5581 idx = (i + j) / 2;
5582 h = sorted_sym_hash[idx];
5583 vdiff = vlook - h->root.u.def.value;
5584 if (vdiff < 0)
5585 j = idx;
5586 else if (vdiff > 0)
5587 i = idx + 1;
5588 else
5589 {
5590 int sdiff = slook->id - h->root.u.def.section->id;
5591 if (sdiff < 0)
5592 j = idx;
5593 else if (sdiff > 0)
5594 i = idx + 1;
5595 else
5596 break;
5597 }
5598 }
5599
5600 /* We didn't find a value/section match. */
5601 if (i == j)
5602 continue;
5603
5604 /* With multiple aliases, or when the weak symbol is already
5605 strongly defined, we have multiple matching symbols and
5606 the binary search above may land on any of them. Step
5607 one past the matching symbol(s). */
5608 while (++idx != j)
5609 {
5610 h = sorted_sym_hash[idx];
5611 if (h->root.u.def.section != slook
5612 || h->root.u.def.value != vlook)
5613 break;
5614 }
5615
5616 /* Now look back over the aliases. Since we sorted by size
5617 as well as value and section, we'll choose the one with
5618 the largest size. */
5619 while (idx-- != i)
5620 {
5621 h = sorted_sym_hash[idx];
5622
5623 /* Stop if value or section doesn't match. */
5624 if (h->root.u.def.section != slook
5625 || h->root.u.def.value != vlook)
5626 break;
5627 else if (h != hlook)
5628 {
5629 struct elf_link_hash_entry *t;
5630
5631 hlook->u.alias = h;
5632 hlook->is_weakalias = 1;
5633 t = h;
5634 if (t->u.alias != NULL)
5635 while (t->u.alias != h)
5636 t = t->u.alias;
5637 t->u.alias = hlook;
5638
5639 /* If the weak definition is in the list of dynamic
5640 symbols, make sure the real definition is put
5641 there as well. */
5642 if (hlook->dynindx != -1 && h->dynindx == -1)
5643 {
5644 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5645 {
5646 err_free_sym_hash:
5647 free (sorted_sym_hash);
5648 goto error_return;
5649 }
5650 }
5651
5652 /* If the real definition is in the list of dynamic
5653 symbols, make sure the weak definition is put
5654 there as well. If we don't do this, then the
5655 dynamic loader might not merge the entries for the
5656 real definition and the weak definition. */
5657 if (h->dynindx != -1 && hlook->dynindx == -1)
5658 {
5659 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5660 goto err_free_sym_hash;
5661 }
5662 break;
5663 }
5664 }
5665 }
5666
5667 free (sorted_sym_hash);
5668 }
5669
5670 if (bed->check_directives
5671 && !(*bed->check_directives) (abfd, info))
5672 return FALSE;
5673
5674 /* If this is a non-traditional link, try to optimize the handling
5675 of the .stab/.stabstr sections. */
5676 if (! dynamic
5677 && ! info->traditional_format
5678 && is_elf_hash_table (htab)
5679 && (info->strip != strip_all && info->strip != strip_debugger))
5680 {
5681 asection *stabstr;
5682
5683 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5684 if (stabstr != NULL)
5685 {
5686 bfd_size_type string_offset = 0;
5687 asection *stab;
5688
5689 for (stab = abfd->sections; stab; stab = stab->next)
5690 if (CONST_STRNEQ (stab->name, ".stab")
5691 && (!stab->name[5] ||
5692 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5693 && (stab->flags & SEC_MERGE) == 0
5694 && !bfd_is_abs_section (stab->output_section))
5695 {
5696 struct bfd_elf_section_data *secdata;
5697
5698 secdata = elf_section_data (stab);
5699 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5700 stabstr, &secdata->sec_info,
5701 &string_offset))
5702 goto error_return;
5703 if (secdata->sec_info)
5704 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5705 }
5706 }
5707 }
5708
5709 if (dynamic && add_needed)
5710 {
5711 /* Add this bfd to the loaded list. */
5712 struct elf_link_loaded_list *n;
5713
5714 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5715 if (n == NULL)
5716 goto error_return;
5717 n->abfd = abfd;
5718 n->next = htab->dyn_loaded;
5719 htab->dyn_loaded = n;
5720 }
5721 if (dynamic && !add_needed
5722 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5723 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5724
5725 return TRUE;
5726
5727 error_free_vers:
5728 free (old_tab);
5729 free (old_strtab);
5730 free (nondeflt_vers);
5731 free (extversym);
5732 error_free_sym:
5733 free (isymbuf);
5734 error_return:
5735 return FALSE;
5736 }
5737
5738 /* Return the linker hash table entry of a symbol that might be
5739 satisfied by an archive symbol. Return -1 on error. */
5740
5741 struct elf_link_hash_entry *
5742 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5743 struct bfd_link_info *info,
5744 const char *name)
5745 {
5746 struct elf_link_hash_entry *h;
5747 char *p, *copy;
5748 size_t len, first;
5749
5750 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5751 if (h != NULL)
5752 return h;
5753
5754 /* If this is a default version (the name contains @@), look up the
5755 symbol again with only one `@' as well as without the version.
5756 The effect is that references to the symbol with and without the
5757 version will be matched by the default symbol in the archive. */
5758
5759 p = strchr (name, ELF_VER_CHR);
5760 if (p == NULL || p[1] != ELF_VER_CHR)
5761 return h;
5762
5763 /* First check with only one `@'. */
5764 len = strlen (name);
5765 copy = (char *) bfd_alloc (abfd, len);
5766 if (copy == NULL)
5767 return (struct elf_link_hash_entry *) -1;
5768
5769 first = p - name + 1;
5770 memcpy (copy, name, first);
5771 memcpy (copy + first, name + first + 1, len - first);
5772
5773 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5774 if (h == NULL)
5775 {
5776 /* We also need to check references to the symbol without the
5777 version. */
5778 copy[first - 1] = '\0';
5779 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5780 FALSE, FALSE, TRUE);
5781 }
5782
5783 bfd_release (abfd, copy);
5784 return h;
5785 }
5786
5787 /* Add symbols from an ELF archive file to the linker hash table. We
5788 don't use _bfd_generic_link_add_archive_symbols because we need to
5789 handle versioned symbols.
5790
5791 Fortunately, ELF archive handling is simpler than that done by
5792 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5793 oddities. In ELF, if we find a symbol in the archive map, and the
5794 symbol is currently undefined, we know that we must pull in that
5795 object file.
5796
5797 Unfortunately, we do have to make multiple passes over the symbol
5798 table until nothing further is resolved. */
5799
5800 static bfd_boolean
5801 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5802 {
5803 symindex c;
5804 unsigned char *included = NULL;
5805 carsym *symdefs;
5806 bfd_boolean loop;
5807 size_t amt;
5808 const struct elf_backend_data *bed;
5809 struct elf_link_hash_entry * (*archive_symbol_lookup)
5810 (bfd *, struct bfd_link_info *, const char *);
5811
5812 if (! bfd_has_map (abfd))
5813 {
5814 /* An empty archive is a special case. */
5815 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5816 return TRUE;
5817 bfd_set_error (bfd_error_no_armap);
5818 return FALSE;
5819 }
5820
5821 /* Keep track of all symbols we know to be already defined, and all
5822 files we know to be already included. This is to speed up the
5823 second and subsequent passes. */
5824 c = bfd_ardata (abfd)->symdef_count;
5825 if (c == 0)
5826 return TRUE;
5827 amt = c * sizeof (*included);
5828 included = (unsigned char *) bfd_zmalloc (amt);
5829 if (included == NULL)
5830 return FALSE;
5831
5832 symdefs = bfd_ardata (abfd)->symdefs;
5833 bed = get_elf_backend_data (abfd);
5834 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5835
5836 do
5837 {
5838 file_ptr last;
5839 symindex i;
5840 carsym *symdef;
5841 carsym *symdefend;
5842
5843 loop = FALSE;
5844 last = -1;
5845
5846 symdef = symdefs;
5847 symdefend = symdef + c;
5848 for (i = 0; symdef < symdefend; symdef++, i++)
5849 {
5850 struct elf_link_hash_entry *h;
5851 bfd *element;
5852 struct bfd_link_hash_entry *undefs_tail;
5853 symindex mark;
5854
5855 if (included[i])
5856 continue;
5857 if (symdef->file_offset == last)
5858 {
5859 included[i] = TRUE;
5860 continue;
5861 }
5862
5863 h = archive_symbol_lookup (abfd, info, symdef->name);
5864 if (h == (struct elf_link_hash_entry *) -1)
5865 goto error_return;
5866
5867 if (h == NULL)
5868 continue;
5869
5870 if (h->root.type == bfd_link_hash_undefined)
5871 {
5872 /* If the archive element has already been loaded then one
5873 of the symbols defined by that element might have been
5874 made undefined due to being in a discarded section. */
5875 if (h->indx == -3)
5876 continue;
5877 }
5878 else if (h->root.type == bfd_link_hash_common)
5879 {
5880 /* We currently have a common symbol. The archive map contains
5881 a reference to this symbol, so we may want to include it. We
5882 only want to include it however, if this archive element
5883 contains a definition of the symbol, not just another common
5884 declaration of it.
5885
5886 Unfortunately some archivers (including GNU ar) will put
5887 declarations of common symbols into their archive maps, as
5888 well as real definitions, so we cannot just go by the archive
5889 map alone. Instead we must read in the element's symbol
5890 table and check that to see what kind of symbol definition
5891 this is. */
5892 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5893 continue;
5894 }
5895 else
5896 {
5897 if (h->root.type != bfd_link_hash_undefweak)
5898 /* Symbol must be defined. Don't check it again. */
5899 included[i] = TRUE;
5900 continue;
5901 }
5902
5903 /* We need to include this archive member. */
5904 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5905 if (element == NULL)
5906 goto error_return;
5907
5908 if (! bfd_check_format (element, bfd_object))
5909 goto error_return;
5910
5911 undefs_tail = info->hash->undefs_tail;
5912
5913 if (!(*info->callbacks
5914 ->add_archive_element) (info, element, symdef->name, &element))
5915 continue;
5916 if (!bfd_link_add_symbols (element, info))
5917 goto error_return;
5918
5919 /* If there are any new undefined symbols, we need to make
5920 another pass through the archive in order to see whether
5921 they can be defined. FIXME: This isn't perfect, because
5922 common symbols wind up on undefs_tail and because an
5923 undefined symbol which is defined later on in this pass
5924 does not require another pass. This isn't a bug, but it
5925 does make the code less efficient than it could be. */
5926 if (undefs_tail != info->hash->undefs_tail)
5927 loop = TRUE;
5928
5929 /* Look backward to mark all symbols from this object file
5930 which we have already seen in this pass. */
5931 mark = i;
5932 do
5933 {
5934 included[mark] = TRUE;
5935 if (mark == 0)
5936 break;
5937 --mark;
5938 }
5939 while (symdefs[mark].file_offset == symdef->file_offset);
5940
5941 /* We mark subsequent symbols from this object file as we go
5942 on through the loop. */
5943 last = symdef->file_offset;
5944 }
5945 }
5946 while (loop);
5947
5948 free (included);
5949 return TRUE;
5950
5951 error_return:
5952 free (included);
5953 return FALSE;
5954 }
5955
5956 /* Given an ELF BFD, add symbols to the global hash table as
5957 appropriate. */
5958
5959 bfd_boolean
5960 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5961 {
5962 switch (bfd_get_format (abfd))
5963 {
5964 case bfd_object:
5965 return elf_link_add_object_symbols (abfd, info);
5966 case bfd_archive:
5967 return elf_link_add_archive_symbols (abfd, info);
5968 default:
5969 bfd_set_error (bfd_error_wrong_format);
5970 return FALSE;
5971 }
5972 }
5973 \f
5974 struct hash_codes_info
5975 {
5976 unsigned long *hashcodes;
5977 bfd_boolean error;
5978 };
5979
5980 /* This function will be called though elf_link_hash_traverse to store
5981 all hash value of the exported symbols in an array. */
5982
5983 static bfd_boolean
5984 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5985 {
5986 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5987 const char *name;
5988 unsigned long ha;
5989 char *alc = NULL;
5990
5991 /* Ignore indirect symbols. These are added by the versioning code. */
5992 if (h->dynindx == -1)
5993 return TRUE;
5994
5995 name = h->root.root.string;
5996 if (h->versioned >= versioned)
5997 {
5998 char *p = strchr (name, ELF_VER_CHR);
5999 if (p != NULL)
6000 {
6001 alc = (char *) bfd_malloc (p - name + 1);
6002 if (alc == NULL)
6003 {
6004 inf->error = TRUE;
6005 return FALSE;
6006 }
6007 memcpy (alc, name, p - name);
6008 alc[p - name] = '\0';
6009 name = alc;
6010 }
6011 }
6012
6013 /* Compute the hash value. */
6014 ha = bfd_elf_hash (name);
6015
6016 /* Store the found hash value in the array given as the argument. */
6017 *(inf->hashcodes)++ = ha;
6018
6019 /* And store it in the struct so that we can put it in the hash table
6020 later. */
6021 h->u.elf_hash_value = ha;
6022
6023 free (alc);
6024 return TRUE;
6025 }
6026
6027 struct collect_gnu_hash_codes
6028 {
6029 bfd *output_bfd;
6030 const struct elf_backend_data *bed;
6031 unsigned long int nsyms;
6032 unsigned long int maskbits;
6033 unsigned long int *hashcodes;
6034 unsigned long int *hashval;
6035 unsigned long int *indx;
6036 unsigned long int *counts;
6037 bfd_vma *bitmask;
6038 bfd_byte *contents;
6039 bfd_size_type xlat;
6040 long int min_dynindx;
6041 unsigned long int bucketcount;
6042 unsigned long int symindx;
6043 long int local_indx;
6044 long int shift1, shift2;
6045 unsigned long int mask;
6046 bfd_boolean error;
6047 };
6048
6049 /* This function will be called though elf_link_hash_traverse to store
6050 all hash value of the exported symbols in an array. */
6051
6052 static bfd_boolean
6053 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6054 {
6055 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6056 const char *name;
6057 unsigned long ha;
6058 char *alc = NULL;
6059
6060 /* Ignore indirect symbols. These are added by the versioning code. */
6061 if (h->dynindx == -1)
6062 return TRUE;
6063
6064 /* Ignore also local symbols and undefined symbols. */
6065 if (! (*s->bed->elf_hash_symbol) (h))
6066 return TRUE;
6067
6068 name = h->root.root.string;
6069 if (h->versioned >= versioned)
6070 {
6071 char *p = strchr (name, ELF_VER_CHR);
6072 if (p != NULL)
6073 {
6074 alc = (char *) bfd_malloc (p - name + 1);
6075 if (alc == NULL)
6076 {
6077 s->error = TRUE;
6078 return FALSE;
6079 }
6080 memcpy (alc, name, p - name);
6081 alc[p - name] = '\0';
6082 name = alc;
6083 }
6084 }
6085
6086 /* Compute the hash value. */
6087 ha = bfd_elf_gnu_hash (name);
6088
6089 /* Store the found hash value in the array for compute_bucket_count,
6090 and also for .dynsym reordering purposes. */
6091 s->hashcodes[s->nsyms] = ha;
6092 s->hashval[h->dynindx] = ha;
6093 ++s->nsyms;
6094 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6095 s->min_dynindx = h->dynindx;
6096
6097 free (alc);
6098 return TRUE;
6099 }
6100
6101 /* This function will be called though elf_link_hash_traverse to do
6102 final dynamic symbol renumbering in case of .gnu.hash.
6103 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6104 to the translation table. */
6105
6106 static bfd_boolean
6107 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6108 {
6109 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6110 unsigned long int bucket;
6111 unsigned long int val;
6112
6113 /* Ignore indirect symbols. */
6114 if (h->dynindx == -1)
6115 return TRUE;
6116
6117 /* Ignore also local symbols and undefined symbols. */
6118 if (! (*s->bed->elf_hash_symbol) (h))
6119 {
6120 if (h->dynindx >= s->min_dynindx)
6121 {
6122 if (s->bed->record_xhash_symbol != NULL)
6123 {
6124 (*s->bed->record_xhash_symbol) (h, 0);
6125 s->local_indx++;
6126 }
6127 else
6128 h->dynindx = s->local_indx++;
6129 }
6130 return TRUE;
6131 }
6132
6133 bucket = s->hashval[h->dynindx] % s->bucketcount;
6134 val = (s->hashval[h->dynindx] >> s->shift1)
6135 & ((s->maskbits >> s->shift1) - 1);
6136 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6137 s->bitmask[val]
6138 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6139 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6140 if (s->counts[bucket] == 1)
6141 /* Last element terminates the chain. */
6142 val |= 1;
6143 bfd_put_32 (s->output_bfd, val,
6144 s->contents + (s->indx[bucket] - s->symindx) * 4);
6145 --s->counts[bucket];
6146 if (s->bed->record_xhash_symbol != NULL)
6147 {
6148 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6149
6150 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6151 }
6152 else
6153 h->dynindx = s->indx[bucket]++;
6154 return TRUE;
6155 }
6156
6157 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6158
6159 bfd_boolean
6160 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6161 {
6162 return !(h->forced_local
6163 || h->root.type == bfd_link_hash_undefined
6164 || h->root.type == bfd_link_hash_undefweak
6165 || ((h->root.type == bfd_link_hash_defined
6166 || h->root.type == bfd_link_hash_defweak)
6167 && h->root.u.def.section->output_section == NULL));
6168 }
6169
6170 /* Array used to determine the number of hash table buckets to use
6171 based on the number of symbols there are. If there are fewer than
6172 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6173 fewer than 37 we use 17 buckets, and so forth. We never use more
6174 than 32771 buckets. */
6175
6176 static const size_t elf_buckets[] =
6177 {
6178 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6179 16411, 32771, 0
6180 };
6181
6182 /* Compute bucket count for hashing table. We do not use a static set
6183 of possible tables sizes anymore. Instead we determine for all
6184 possible reasonable sizes of the table the outcome (i.e., the
6185 number of collisions etc) and choose the best solution. The
6186 weighting functions are not too simple to allow the table to grow
6187 without bounds. Instead one of the weighting factors is the size.
6188 Therefore the result is always a good payoff between few collisions
6189 (= short chain lengths) and table size. */
6190 static size_t
6191 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6192 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6193 unsigned long int nsyms,
6194 int gnu_hash)
6195 {
6196 size_t best_size = 0;
6197 unsigned long int i;
6198
6199 /* We have a problem here. The following code to optimize the table
6200 size requires an integer type with more the 32 bits. If
6201 BFD_HOST_U_64_BIT is set we know about such a type. */
6202 #ifdef BFD_HOST_U_64_BIT
6203 if (info->optimize)
6204 {
6205 size_t minsize;
6206 size_t maxsize;
6207 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6208 bfd *dynobj = elf_hash_table (info)->dynobj;
6209 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6210 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6211 unsigned long int *counts;
6212 bfd_size_type amt;
6213 unsigned int no_improvement_count = 0;
6214
6215 /* Possible optimization parameters: if we have NSYMS symbols we say
6216 that the hashing table must at least have NSYMS/4 and at most
6217 2*NSYMS buckets. */
6218 minsize = nsyms / 4;
6219 if (minsize == 0)
6220 minsize = 1;
6221 best_size = maxsize = nsyms * 2;
6222 if (gnu_hash)
6223 {
6224 if (minsize < 2)
6225 minsize = 2;
6226 if ((best_size & 31) == 0)
6227 ++best_size;
6228 }
6229
6230 /* Create array where we count the collisions in. We must use bfd_malloc
6231 since the size could be large. */
6232 amt = maxsize;
6233 amt *= sizeof (unsigned long int);
6234 counts = (unsigned long int *) bfd_malloc (amt);
6235 if (counts == NULL)
6236 return 0;
6237
6238 /* Compute the "optimal" size for the hash table. The criteria is a
6239 minimal chain length. The minor criteria is (of course) the size
6240 of the table. */
6241 for (i = minsize; i < maxsize; ++i)
6242 {
6243 /* Walk through the array of hashcodes and count the collisions. */
6244 BFD_HOST_U_64_BIT max;
6245 unsigned long int j;
6246 unsigned long int fact;
6247
6248 if (gnu_hash && (i & 31) == 0)
6249 continue;
6250
6251 memset (counts, '\0', i * sizeof (unsigned long int));
6252
6253 /* Determine how often each hash bucket is used. */
6254 for (j = 0; j < nsyms; ++j)
6255 ++counts[hashcodes[j] % i];
6256
6257 /* For the weight function we need some information about the
6258 pagesize on the target. This is information need not be 100%
6259 accurate. Since this information is not available (so far) we
6260 define it here to a reasonable default value. If it is crucial
6261 to have a better value some day simply define this value. */
6262 # ifndef BFD_TARGET_PAGESIZE
6263 # define BFD_TARGET_PAGESIZE (4096)
6264 # endif
6265
6266 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6267 and the chains. */
6268 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6269
6270 # if 1
6271 /* Variant 1: optimize for short chains. We add the squares
6272 of all the chain lengths (which favors many small chain
6273 over a few long chains). */
6274 for (j = 0; j < i; ++j)
6275 max += counts[j] * counts[j];
6276
6277 /* This adds penalties for the overall size of the table. */
6278 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6279 max *= fact * fact;
6280 # else
6281 /* Variant 2: Optimize a lot more for small table. Here we
6282 also add squares of the size but we also add penalties for
6283 empty slots (the +1 term). */
6284 for (j = 0; j < i; ++j)
6285 max += (1 + counts[j]) * (1 + counts[j]);
6286
6287 /* The overall size of the table is considered, but not as
6288 strong as in variant 1, where it is squared. */
6289 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6290 max *= fact;
6291 # endif
6292
6293 /* Compare with current best results. */
6294 if (max < best_chlen)
6295 {
6296 best_chlen = max;
6297 best_size = i;
6298 no_improvement_count = 0;
6299 }
6300 /* PR 11843: Avoid futile long searches for the best bucket size
6301 when there are a large number of symbols. */
6302 else if (++no_improvement_count == 100)
6303 break;
6304 }
6305
6306 free (counts);
6307 }
6308 else
6309 #endif /* defined (BFD_HOST_U_64_BIT) */
6310 {
6311 /* This is the fallback solution if no 64bit type is available or if we
6312 are not supposed to spend much time on optimizations. We select the
6313 bucket count using a fixed set of numbers. */
6314 for (i = 0; elf_buckets[i] != 0; i++)
6315 {
6316 best_size = elf_buckets[i];
6317 if (nsyms < elf_buckets[i + 1])
6318 break;
6319 }
6320 if (gnu_hash && best_size < 2)
6321 best_size = 2;
6322 }
6323
6324 return best_size;
6325 }
6326
6327 /* Size any SHT_GROUP section for ld -r. */
6328
6329 bfd_boolean
6330 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6331 {
6332 bfd *ibfd;
6333 asection *s;
6334
6335 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6336 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6337 && (s = ibfd->sections) != NULL
6338 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6339 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6340 return FALSE;
6341 return TRUE;
6342 }
6343
6344 /* Set a default stack segment size. The value in INFO wins. If it
6345 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6346 undefined it is initialized. */
6347
6348 bfd_boolean
6349 bfd_elf_stack_segment_size (bfd *output_bfd,
6350 struct bfd_link_info *info,
6351 const char *legacy_symbol,
6352 bfd_vma default_size)
6353 {
6354 struct elf_link_hash_entry *h = NULL;
6355
6356 /* Look for legacy symbol. */
6357 if (legacy_symbol)
6358 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6359 FALSE, FALSE, FALSE);
6360 if (h && (h->root.type == bfd_link_hash_defined
6361 || h->root.type == bfd_link_hash_defweak)
6362 && h->def_regular
6363 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6364 {
6365 /* The symbol has no type if specified on the command line. */
6366 h->type = STT_OBJECT;
6367 if (info->stacksize)
6368 /* xgettext:c-format */
6369 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6370 output_bfd, legacy_symbol);
6371 else if (h->root.u.def.section != bfd_abs_section_ptr)
6372 /* xgettext:c-format */
6373 _bfd_error_handler (_("%pB: %s not absolute"),
6374 output_bfd, legacy_symbol);
6375 else
6376 info->stacksize = h->root.u.def.value;
6377 }
6378
6379 if (!info->stacksize)
6380 /* If the user didn't set a size, or explicitly inhibit the
6381 size, set it now. */
6382 info->stacksize = default_size;
6383
6384 /* Provide the legacy symbol, if it is referenced. */
6385 if (h && (h->root.type == bfd_link_hash_undefined
6386 || h->root.type == bfd_link_hash_undefweak))
6387 {
6388 struct bfd_link_hash_entry *bh = NULL;
6389
6390 if (!(_bfd_generic_link_add_one_symbol
6391 (info, output_bfd, legacy_symbol,
6392 BSF_GLOBAL, bfd_abs_section_ptr,
6393 info->stacksize >= 0 ? info->stacksize : 0,
6394 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6395 return FALSE;
6396
6397 h = (struct elf_link_hash_entry *) bh;
6398 h->def_regular = 1;
6399 h->type = STT_OBJECT;
6400 }
6401
6402 return TRUE;
6403 }
6404
6405 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6406
6407 struct elf_gc_sweep_symbol_info
6408 {
6409 struct bfd_link_info *info;
6410 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6411 bfd_boolean);
6412 };
6413
6414 static bfd_boolean
6415 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6416 {
6417 if (!h->mark
6418 && (((h->root.type == bfd_link_hash_defined
6419 || h->root.type == bfd_link_hash_defweak)
6420 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6421 && h->root.u.def.section->gc_mark))
6422 || h->root.type == bfd_link_hash_undefined
6423 || h->root.type == bfd_link_hash_undefweak))
6424 {
6425 struct elf_gc_sweep_symbol_info *inf;
6426
6427 inf = (struct elf_gc_sweep_symbol_info *) data;
6428 (*inf->hide_symbol) (inf->info, h, TRUE);
6429 h->def_regular = 0;
6430 h->ref_regular = 0;
6431 h->ref_regular_nonweak = 0;
6432 }
6433
6434 return TRUE;
6435 }
6436
6437 /* Set up the sizes and contents of the ELF dynamic sections. This is
6438 called by the ELF linker emulation before_allocation routine. We
6439 must set the sizes of the sections before the linker sets the
6440 addresses of the various sections. */
6441
6442 bfd_boolean
6443 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6444 const char *soname,
6445 const char *rpath,
6446 const char *filter_shlib,
6447 const char *audit,
6448 const char *depaudit,
6449 const char * const *auxiliary_filters,
6450 struct bfd_link_info *info,
6451 asection **sinterpptr)
6452 {
6453 bfd *dynobj;
6454 const struct elf_backend_data *bed;
6455
6456 *sinterpptr = NULL;
6457
6458 if (!is_elf_hash_table (info->hash))
6459 return TRUE;
6460
6461 dynobj = elf_hash_table (info)->dynobj;
6462
6463 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6464 {
6465 struct bfd_elf_version_tree *verdefs;
6466 struct elf_info_failed asvinfo;
6467 struct bfd_elf_version_tree *t;
6468 struct bfd_elf_version_expr *d;
6469 asection *s;
6470 size_t soname_indx;
6471
6472 /* If we are supposed to export all symbols into the dynamic symbol
6473 table (this is not the normal case), then do so. */
6474 if (info->export_dynamic
6475 || (bfd_link_executable (info) && info->dynamic))
6476 {
6477 struct elf_info_failed eif;
6478
6479 eif.info = info;
6480 eif.failed = FALSE;
6481 elf_link_hash_traverse (elf_hash_table (info),
6482 _bfd_elf_export_symbol,
6483 &eif);
6484 if (eif.failed)
6485 return FALSE;
6486 }
6487
6488 if (soname != NULL)
6489 {
6490 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6491 soname, TRUE);
6492 if (soname_indx == (size_t) -1
6493 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6494 return FALSE;
6495 }
6496 else
6497 soname_indx = (size_t) -1;
6498
6499 /* Make all global versions with definition. */
6500 for (t = info->version_info; t != NULL; t = t->next)
6501 for (d = t->globals.list; d != NULL; d = d->next)
6502 if (!d->symver && d->literal)
6503 {
6504 const char *verstr, *name;
6505 size_t namelen, verlen, newlen;
6506 char *newname, *p, leading_char;
6507 struct elf_link_hash_entry *newh;
6508
6509 leading_char = bfd_get_symbol_leading_char (output_bfd);
6510 name = d->pattern;
6511 namelen = strlen (name) + (leading_char != '\0');
6512 verstr = t->name;
6513 verlen = strlen (verstr);
6514 newlen = namelen + verlen + 3;
6515
6516 newname = (char *) bfd_malloc (newlen);
6517 if (newname == NULL)
6518 return FALSE;
6519 newname[0] = leading_char;
6520 memcpy (newname + (leading_char != '\0'), name, namelen);
6521
6522 /* Check the hidden versioned definition. */
6523 p = newname + namelen;
6524 *p++ = ELF_VER_CHR;
6525 memcpy (p, verstr, verlen + 1);
6526 newh = elf_link_hash_lookup (elf_hash_table (info),
6527 newname, FALSE, FALSE,
6528 FALSE);
6529 if (newh == NULL
6530 || (newh->root.type != bfd_link_hash_defined
6531 && newh->root.type != bfd_link_hash_defweak))
6532 {
6533 /* Check the default versioned definition. */
6534 *p++ = ELF_VER_CHR;
6535 memcpy (p, verstr, verlen + 1);
6536 newh = elf_link_hash_lookup (elf_hash_table (info),
6537 newname, FALSE, FALSE,
6538 FALSE);
6539 }
6540 free (newname);
6541
6542 /* Mark this version if there is a definition and it is
6543 not defined in a shared object. */
6544 if (newh != NULL
6545 && !newh->def_dynamic
6546 && (newh->root.type == bfd_link_hash_defined
6547 || newh->root.type == bfd_link_hash_defweak))
6548 d->symver = 1;
6549 }
6550
6551 /* Attach all the symbols to their version information. */
6552 asvinfo.info = info;
6553 asvinfo.failed = FALSE;
6554
6555 elf_link_hash_traverse (elf_hash_table (info),
6556 _bfd_elf_link_assign_sym_version,
6557 &asvinfo);
6558 if (asvinfo.failed)
6559 return FALSE;
6560
6561 if (!info->allow_undefined_version)
6562 {
6563 /* Check if all global versions have a definition. */
6564 bfd_boolean all_defined = TRUE;
6565 for (t = info->version_info; t != NULL; t = t->next)
6566 for (d = t->globals.list; d != NULL; d = d->next)
6567 if (d->literal && !d->symver && !d->script)
6568 {
6569 _bfd_error_handler
6570 (_("%s: undefined version: %s"),
6571 d->pattern, t->name);
6572 all_defined = FALSE;
6573 }
6574
6575 if (!all_defined)
6576 {
6577 bfd_set_error (bfd_error_bad_value);
6578 return FALSE;
6579 }
6580 }
6581
6582 /* Set up the version definition section. */
6583 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6584 BFD_ASSERT (s != NULL);
6585
6586 /* We may have created additional version definitions if we are
6587 just linking a regular application. */
6588 verdefs = info->version_info;
6589
6590 /* Skip anonymous version tag. */
6591 if (verdefs != NULL && verdefs->vernum == 0)
6592 verdefs = verdefs->next;
6593
6594 if (verdefs == NULL && !info->create_default_symver)
6595 s->flags |= SEC_EXCLUDE;
6596 else
6597 {
6598 unsigned int cdefs;
6599 bfd_size_type size;
6600 bfd_byte *p;
6601 Elf_Internal_Verdef def;
6602 Elf_Internal_Verdaux defaux;
6603 struct bfd_link_hash_entry *bh;
6604 struct elf_link_hash_entry *h;
6605 const char *name;
6606
6607 cdefs = 0;
6608 size = 0;
6609
6610 /* Make space for the base version. */
6611 size += sizeof (Elf_External_Verdef);
6612 size += sizeof (Elf_External_Verdaux);
6613 ++cdefs;
6614
6615 /* Make space for the default version. */
6616 if (info->create_default_symver)
6617 {
6618 size += sizeof (Elf_External_Verdef);
6619 ++cdefs;
6620 }
6621
6622 for (t = verdefs; t != NULL; t = t->next)
6623 {
6624 struct bfd_elf_version_deps *n;
6625
6626 /* Don't emit base version twice. */
6627 if (t->vernum == 0)
6628 continue;
6629
6630 size += sizeof (Elf_External_Verdef);
6631 size += sizeof (Elf_External_Verdaux);
6632 ++cdefs;
6633
6634 for (n = t->deps; n != NULL; n = n->next)
6635 size += sizeof (Elf_External_Verdaux);
6636 }
6637
6638 s->size = size;
6639 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6640 if (s->contents == NULL && s->size != 0)
6641 return FALSE;
6642
6643 /* Fill in the version definition section. */
6644
6645 p = s->contents;
6646
6647 def.vd_version = VER_DEF_CURRENT;
6648 def.vd_flags = VER_FLG_BASE;
6649 def.vd_ndx = 1;
6650 def.vd_cnt = 1;
6651 if (info->create_default_symver)
6652 {
6653 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6654 def.vd_next = sizeof (Elf_External_Verdef);
6655 }
6656 else
6657 {
6658 def.vd_aux = sizeof (Elf_External_Verdef);
6659 def.vd_next = (sizeof (Elf_External_Verdef)
6660 + sizeof (Elf_External_Verdaux));
6661 }
6662
6663 if (soname_indx != (size_t) -1)
6664 {
6665 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6666 soname_indx);
6667 def.vd_hash = bfd_elf_hash (soname);
6668 defaux.vda_name = soname_indx;
6669 name = soname;
6670 }
6671 else
6672 {
6673 size_t indx;
6674
6675 name = lbasename (bfd_get_filename (output_bfd));
6676 def.vd_hash = bfd_elf_hash (name);
6677 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6678 name, FALSE);
6679 if (indx == (size_t) -1)
6680 return FALSE;
6681 defaux.vda_name = indx;
6682 }
6683 defaux.vda_next = 0;
6684
6685 _bfd_elf_swap_verdef_out (output_bfd, &def,
6686 (Elf_External_Verdef *) p);
6687 p += sizeof (Elf_External_Verdef);
6688 if (info->create_default_symver)
6689 {
6690 /* Add a symbol representing this version. */
6691 bh = NULL;
6692 if (! (_bfd_generic_link_add_one_symbol
6693 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6694 0, NULL, FALSE,
6695 get_elf_backend_data (dynobj)->collect, &bh)))
6696 return FALSE;
6697 h = (struct elf_link_hash_entry *) bh;
6698 h->non_elf = 0;
6699 h->def_regular = 1;
6700 h->type = STT_OBJECT;
6701 h->verinfo.vertree = NULL;
6702
6703 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6704 return FALSE;
6705
6706 /* Create a duplicate of the base version with the same
6707 aux block, but different flags. */
6708 def.vd_flags = 0;
6709 def.vd_ndx = 2;
6710 def.vd_aux = sizeof (Elf_External_Verdef);
6711 if (verdefs)
6712 def.vd_next = (sizeof (Elf_External_Verdef)
6713 + sizeof (Elf_External_Verdaux));
6714 else
6715 def.vd_next = 0;
6716 _bfd_elf_swap_verdef_out (output_bfd, &def,
6717 (Elf_External_Verdef *) p);
6718 p += sizeof (Elf_External_Verdef);
6719 }
6720 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6721 (Elf_External_Verdaux *) p);
6722 p += sizeof (Elf_External_Verdaux);
6723
6724 for (t = verdefs; t != NULL; t = t->next)
6725 {
6726 unsigned int cdeps;
6727 struct bfd_elf_version_deps *n;
6728
6729 /* Don't emit the base version twice. */
6730 if (t->vernum == 0)
6731 continue;
6732
6733 cdeps = 0;
6734 for (n = t->deps; n != NULL; n = n->next)
6735 ++cdeps;
6736
6737 /* Add a symbol representing this version. */
6738 bh = NULL;
6739 if (! (_bfd_generic_link_add_one_symbol
6740 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6741 0, NULL, FALSE,
6742 get_elf_backend_data (dynobj)->collect, &bh)))
6743 return FALSE;
6744 h = (struct elf_link_hash_entry *) bh;
6745 h->non_elf = 0;
6746 h->def_regular = 1;
6747 h->type = STT_OBJECT;
6748 h->verinfo.vertree = t;
6749
6750 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6751 return FALSE;
6752
6753 def.vd_version = VER_DEF_CURRENT;
6754 def.vd_flags = 0;
6755 if (t->globals.list == NULL
6756 && t->locals.list == NULL
6757 && ! t->used)
6758 def.vd_flags |= VER_FLG_WEAK;
6759 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6760 def.vd_cnt = cdeps + 1;
6761 def.vd_hash = bfd_elf_hash (t->name);
6762 def.vd_aux = sizeof (Elf_External_Verdef);
6763 def.vd_next = 0;
6764
6765 /* If a basever node is next, it *must* be the last node in
6766 the chain, otherwise Verdef construction breaks. */
6767 if (t->next != NULL && t->next->vernum == 0)
6768 BFD_ASSERT (t->next->next == NULL);
6769
6770 if (t->next != NULL && t->next->vernum != 0)
6771 def.vd_next = (sizeof (Elf_External_Verdef)
6772 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6773
6774 _bfd_elf_swap_verdef_out (output_bfd, &def,
6775 (Elf_External_Verdef *) p);
6776 p += sizeof (Elf_External_Verdef);
6777
6778 defaux.vda_name = h->dynstr_index;
6779 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6780 h->dynstr_index);
6781 defaux.vda_next = 0;
6782 if (t->deps != NULL)
6783 defaux.vda_next = sizeof (Elf_External_Verdaux);
6784 t->name_indx = defaux.vda_name;
6785
6786 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6787 (Elf_External_Verdaux *) p);
6788 p += sizeof (Elf_External_Verdaux);
6789
6790 for (n = t->deps; n != NULL; n = n->next)
6791 {
6792 if (n->version_needed == NULL)
6793 {
6794 /* This can happen if there was an error in the
6795 version script. */
6796 defaux.vda_name = 0;
6797 }
6798 else
6799 {
6800 defaux.vda_name = n->version_needed->name_indx;
6801 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6802 defaux.vda_name);
6803 }
6804 if (n->next == NULL)
6805 defaux.vda_next = 0;
6806 else
6807 defaux.vda_next = sizeof (Elf_External_Verdaux);
6808
6809 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6810 (Elf_External_Verdaux *) p);
6811 p += sizeof (Elf_External_Verdaux);
6812 }
6813 }
6814
6815 elf_tdata (output_bfd)->cverdefs = cdefs;
6816 }
6817 }
6818
6819 bed = get_elf_backend_data (output_bfd);
6820
6821 if (info->gc_sections && bed->can_gc_sections)
6822 {
6823 struct elf_gc_sweep_symbol_info sweep_info;
6824
6825 /* Remove the symbols that were in the swept sections from the
6826 dynamic symbol table. */
6827 sweep_info.info = info;
6828 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6829 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6830 &sweep_info);
6831 }
6832
6833 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6834 {
6835 asection *s;
6836 struct elf_find_verdep_info sinfo;
6837
6838 /* Work out the size of the version reference section. */
6839
6840 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6841 BFD_ASSERT (s != NULL);
6842
6843 sinfo.info = info;
6844 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6845 if (sinfo.vers == 0)
6846 sinfo.vers = 1;
6847 sinfo.failed = FALSE;
6848
6849 elf_link_hash_traverse (elf_hash_table (info),
6850 _bfd_elf_link_find_version_dependencies,
6851 &sinfo);
6852 if (sinfo.failed)
6853 return FALSE;
6854
6855 if (elf_tdata (output_bfd)->verref == NULL)
6856 s->flags |= SEC_EXCLUDE;
6857 else
6858 {
6859 Elf_Internal_Verneed *vn;
6860 unsigned int size;
6861 unsigned int crefs;
6862 bfd_byte *p;
6863
6864 /* Build the version dependency section. */
6865 size = 0;
6866 crefs = 0;
6867 for (vn = elf_tdata (output_bfd)->verref;
6868 vn != NULL;
6869 vn = vn->vn_nextref)
6870 {
6871 Elf_Internal_Vernaux *a;
6872
6873 size += sizeof (Elf_External_Verneed);
6874 ++crefs;
6875 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6876 size += sizeof (Elf_External_Vernaux);
6877 }
6878
6879 s->size = size;
6880 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6881 if (s->contents == NULL)
6882 return FALSE;
6883
6884 p = s->contents;
6885 for (vn = elf_tdata (output_bfd)->verref;
6886 vn != NULL;
6887 vn = vn->vn_nextref)
6888 {
6889 unsigned int caux;
6890 Elf_Internal_Vernaux *a;
6891 size_t indx;
6892
6893 caux = 0;
6894 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6895 ++caux;
6896
6897 vn->vn_version = VER_NEED_CURRENT;
6898 vn->vn_cnt = caux;
6899 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6900 elf_dt_name (vn->vn_bfd) != NULL
6901 ? elf_dt_name (vn->vn_bfd)
6902 : lbasename (bfd_get_filename
6903 (vn->vn_bfd)),
6904 FALSE);
6905 if (indx == (size_t) -1)
6906 return FALSE;
6907 vn->vn_file = indx;
6908 vn->vn_aux = sizeof (Elf_External_Verneed);
6909 if (vn->vn_nextref == NULL)
6910 vn->vn_next = 0;
6911 else
6912 vn->vn_next = (sizeof (Elf_External_Verneed)
6913 + caux * sizeof (Elf_External_Vernaux));
6914
6915 _bfd_elf_swap_verneed_out (output_bfd, vn,
6916 (Elf_External_Verneed *) p);
6917 p += sizeof (Elf_External_Verneed);
6918
6919 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6920 {
6921 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6922 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6923 a->vna_nodename, FALSE);
6924 if (indx == (size_t) -1)
6925 return FALSE;
6926 a->vna_name = indx;
6927 if (a->vna_nextptr == NULL)
6928 a->vna_next = 0;
6929 else
6930 a->vna_next = sizeof (Elf_External_Vernaux);
6931
6932 _bfd_elf_swap_vernaux_out (output_bfd, a,
6933 (Elf_External_Vernaux *) p);
6934 p += sizeof (Elf_External_Vernaux);
6935 }
6936 }
6937
6938 elf_tdata (output_bfd)->cverrefs = crefs;
6939 }
6940 }
6941
6942 /* Any syms created from now on start with -1 in
6943 got.refcount/offset and plt.refcount/offset. */
6944 elf_hash_table (info)->init_got_refcount
6945 = elf_hash_table (info)->init_got_offset;
6946 elf_hash_table (info)->init_plt_refcount
6947 = elf_hash_table (info)->init_plt_offset;
6948
6949 if (bfd_link_relocatable (info)
6950 && !_bfd_elf_size_group_sections (info))
6951 return FALSE;
6952
6953 /* The backend may have to create some sections regardless of whether
6954 we're dynamic or not. */
6955 if (bed->elf_backend_always_size_sections
6956 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6957 return FALSE;
6958
6959 /* Determine any GNU_STACK segment requirements, after the backend
6960 has had a chance to set a default segment size. */
6961 if (info->execstack)
6962 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6963 else if (info->noexecstack)
6964 elf_stack_flags (output_bfd) = PF_R | PF_W;
6965 else
6966 {
6967 bfd *inputobj;
6968 asection *notesec = NULL;
6969 int exec = 0;
6970
6971 for (inputobj = info->input_bfds;
6972 inputobj;
6973 inputobj = inputobj->link.next)
6974 {
6975 asection *s;
6976
6977 if (inputobj->flags
6978 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6979 continue;
6980 s = inputobj->sections;
6981 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6982 continue;
6983
6984 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6985 if (s)
6986 {
6987 if (s->flags & SEC_CODE)
6988 exec = PF_X;
6989 notesec = s;
6990 }
6991 else if (bed->default_execstack)
6992 exec = PF_X;
6993 }
6994 if (notesec || info->stacksize > 0)
6995 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6996 if (notesec && exec && bfd_link_relocatable (info)
6997 && notesec->output_section != bfd_abs_section_ptr)
6998 notesec->output_section->flags |= SEC_CODE;
6999 }
7000
7001 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7002 {
7003 struct elf_info_failed eif;
7004 struct elf_link_hash_entry *h;
7005 asection *dynstr;
7006 asection *s;
7007
7008 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7009 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7010
7011 if (info->symbolic)
7012 {
7013 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7014 return FALSE;
7015 info->flags |= DF_SYMBOLIC;
7016 }
7017
7018 if (rpath != NULL)
7019 {
7020 size_t indx;
7021 bfd_vma tag;
7022
7023 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7024 TRUE);
7025 if (indx == (size_t) -1)
7026 return FALSE;
7027
7028 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7029 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7030 return FALSE;
7031 }
7032
7033 if (filter_shlib != NULL)
7034 {
7035 size_t indx;
7036
7037 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7038 filter_shlib, TRUE);
7039 if (indx == (size_t) -1
7040 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7041 return FALSE;
7042 }
7043
7044 if (auxiliary_filters != NULL)
7045 {
7046 const char * const *p;
7047
7048 for (p = auxiliary_filters; *p != NULL; p++)
7049 {
7050 size_t indx;
7051
7052 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7053 *p, TRUE);
7054 if (indx == (size_t) -1
7055 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7056 return FALSE;
7057 }
7058 }
7059
7060 if (audit != NULL)
7061 {
7062 size_t indx;
7063
7064 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7065 TRUE);
7066 if (indx == (size_t) -1
7067 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7068 return FALSE;
7069 }
7070
7071 if (depaudit != NULL)
7072 {
7073 size_t indx;
7074
7075 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7076 TRUE);
7077 if (indx == (size_t) -1
7078 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7079 return FALSE;
7080 }
7081
7082 eif.info = info;
7083 eif.failed = FALSE;
7084
7085 /* Find all symbols which were defined in a dynamic object and make
7086 the backend pick a reasonable value for them. */
7087 elf_link_hash_traverse (elf_hash_table (info),
7088 _bfd_elf_adjust_dynamic_symbol,
7089 &eif);
7090 if (eif.failed)
7091 return FALSE;
7092
7093 /* Add some entries to the .dynamic section. We fill in some of the
7094 values later, in bfd_elf_final_link, but we must add the entries
7095 now so that we know the final size of the .dynamic section. */
7096
7097 /* If there are initialization and/or finalization functions to
7098 call then add the corresponding DT_INIT/DT_FINI entries. */
7099 h = (info->init_function
7100 ? elf_link_hash_lookup (elf_hash_table (info),
7101 info->init_function, FALSE,
7102 FALSE, FALSE)
7103 : NULL);
7104 if (h != NULL
7105 && (h->ref_regular
7106 || h->def_regular))
7107 {
7108 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7109 return FALSE;
7110 }
7111 h = (info->fini_function
7112 ? elf_link_hash_lookup (elf_hash_table (info),
7113 info->fini_function, FALSE,
7114 FALSE, FALSE)
7115 : NULL);
7116 if (h != NULL
7117 && (h->ref_regular
7118 || h->def_regular))
7119 {
7120 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7121 return FALSE;
7122 }
7123
7124 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7125 if (s != NULL && s->linker_has_input)
7126 {
7127 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7128 if (! bfd_link_executable (info))
7129 {
7130 bfd *sub;
7131 asection *o;
7132
7133 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7134 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7135 && (o = sub->sections) != NULL
7136 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7137 for (o = sub->sections; o != NULL; o = o->next)
7138 if (elf_section_data (o)->this_hdr.sh_type
7139 == SHT_PREINIT_ARRAY)
7140 {
7141 _bfd_error_handler
7142 (_("%pB: .preinit_array section is not allowed in DSO"),
7143 sub);
7144 break;
7145 }
7146
7147 bfd_set_error (bfd_error_nonrepresentable_section);
7148 return FALSE;
7149 }
7150
7151 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7152 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7153 return FALSE;
7154 }
7155 s = bfd_get_section_by_name (output_bfd, ".init_array");
7156 if (s != NULL && s->linker_has_input)
7157 {
7158 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7159 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7160 return FALSE;
7161 }
7162 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7163 if (s != NULL && s->linker_has_input)
7164 {
7165 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7166 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7167 return FALSE;
7168 }
7169
7170 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7171 /* If .dynstr is excluded from the link, we don't want any of
7172 these tags. Strictly, we should be checking each section
7173 individually; This quick check covers for the case where
7174 someone does a /DISCARD/ : { *(*) }. */
7175 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7176 {
7177 bfd_size_type strsize;
7178
7179 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7180 if ((info->emit_hash
7181 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7182 || (info->emit_gnu_hash
7183 && (bed->record_xhash_symbol == NULL
7184 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7185 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7186 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7187 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7188 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7189 bed->s->sizeof_sym)
7190 || (info->gnu_flags_1
7191 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7192 info->gnu_flags_1)))
7193 return FALSE;
7194 }
7195 }
7196
7197 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7198 return FALSE;
7199
7200 /* The backend must work out the sizes of all the other dynamic
7201 sections. */
7202 if (dynobj != NULL
7203 && bed->elf_backend_size_dynamic_sections != NULL
7204 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7205 return FALSE;
7206
7207 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7208 {
7209 if (elf_tdata (output_bfd)->cverdefs)
7210 {
7211 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7212
7213 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7214 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7215 return FALSE;
7216 }
7217
7218 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7219 {
7220 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7221 return FALSE;
7222 }
7223 else if (info->flags & DF_BIND_NOW)
7224 {
7225 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7226 return FALSE;
7227 }
7228
7229 if (info->flags_1)
7230 {
7231 if (bfd_link_executable (info))
7232 info->flags_1 &= ~ (DF_1_INITFIRST
7233 | DF_1_NODELETE
7234 | DF_1_NOOPEN);
7235 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7236 return FALSE;
7237 }
7238
7239 if (elf_tdata (output_bfd)->cverrefs)
7240 {
7241 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7242
7243 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7244 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7245 return FALSE;
7246 }
7247
7248 if ((elf_tdata (output_bfd)->cverrefs == 0
7249 && elf_tdata (output_bfd)->cverdefs == 0)
7250 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7251 {
7252 asection *s;
7253
7254 s = bfd_get_linker_section (dynobj, ".gnu.version");
7255 s->flags |= SEC_EXCLUDE;
7256 }
7257 }
7258 return TRUE;
7259 }
7260
7261 /* Find the first non-excluded output section. We'll use its
7262 section symbol for some emitted relocs. */
7263 void
7264 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7265 {
7266 asection *s;
7267 asection *found = NULL;
7268
7269 for (s = output_bfd->sections; s != NULL; s = s->next)
7270 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7271 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7272 {
7273 found = s;
7274 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7275 break;
7276 }
7277 elf_hash_table (info)->text_index_section = found;
7278 }
7279
7280 /* Find two non-excluded output sections, one for code, one for data.
7281 We'll use their section symbols for some emitted relocs. */
7282 void
7283 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7284 {
7285 asection *s;
7286 asection *found = NULL;
7287
7288 /* Data first, since setting text_index_section changes
7289 _bfd_elf_omit_section_dynsym_default. */
7290 for (s = output_bfd->sections; s != NULL; s = s->next)
7291 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7292 && !(s->flags & SEC_READONLY)
7293 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7294 {
7295 found = s;
7296 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7297 break;
7298 }
7299 elf_hash_table (info)->data_index_section = found;
7300
7301 for (s = output_bfd->sections; s != NULL; s = s->next)
7302 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7303 && (s->flags & SEC_READONLY)
7304 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7305 {
7306 found = s;
7307 break;
7308 }
7309 elf_hash_table (info)->text_index_section = found;
7310 }
7311
7312 #define GNU_HASH_SECTION_NAME(bed) \
7313 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7314
7315 bfd_boolean
7316 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7317 {
7318 const struct elf_backend_data *bed;
7319 unsigned long section_sym_count;
7320 bfd_size_type dynsymcount = 0;
7321
7322 if (!is_elf_hash_table (info->hash))
7323 return TRUE;
7324
7325 bed = get_elf_backend_data (output_bfd);
7326 (*bed->elf_backend_init_index_section) (output_bfd, info);
7327
7328 /* Assign dynsym indices. In a shared library we generate a section
7329 symbol for each output section, which come first. Next come all
7330 of the back-end allocated local dynamic syms, followed by the rest
7331 of the global symbols.
7332
7333 This is usually not needed for static binaries, however backends
7334 can request to always do it, e.g. the MIPS backend uses dynamic
7335 symbol counts to lay out GOT, which will be produced in the
7336 presence of GOT relocations even in static binaries (holding fixed
7337 data in that case, to satisfy those relocations). */
7338
7339 if (elf_hash_table (info)->dynamic_sections_created
7340 || bed->always_renumber_dynsyms)
7341 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7342 &section_sym_count);
7343
7344 if (elf_hash_table (info)->dynamic_sections_created)
7345 {
7346 bfd *dynobj;
7347 asection *s;
7348 unsigned int dtagcount;
7349
7350 dynobj = elf_hash_table (info)->dynobj;
7351
7352 /* Work out the size of the symbol version section. */
7353 s = bfd_get_linker_section (dynobj, ".gnu.version");
7354 BFD_ASSERT (s != NULL);
7355 if ((s->flags & SEC_EXCLUDE) == 0)
7356 {
7357 s->size = dynsymcount * sizeof (Elf_External_Versym);
7358 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7359 if (s->contents == NULL)
7360 return FALSE;
7361
7362 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7363 return FALSE;
7364 }
7365
7366 /* Set the size of the .dynsym and .hash sections. We counted
7367 the number of dynamic symbols in elf_link_add_object_symbols.
7368 We will build the contents of .dynsym and .hash when we build
7369 the final symbol table, because until then we do not know the
7370 correct value to give the symbols. We built the .dynstr
7371 section as we went along in elf_link_add_object_symbols. */
7372 s = elf_hash_table (info)->dynsym;
7373 BFD_ASSERT (s != NULL);
7374 s->size = dynsymcount * bed->s->sizeof_sym;
7375
7376 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7377 if (s->contents == NULL)
7378 return FALSE;
7379
7380 /* The first entry in .dynsym is a dummy symbol. Clear all the
7381 section syms, in case we don't output them all. */
7382 ++section_sym_count;
7383 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7384
7385 elf_hash_table (info)->bucketcount = 0;
7386
7387 /* Compute the size of the hashing table. As a side effect this
7388 computes the hash values for all the names we export. */
7389 if (info->emit_hash)
7390 {
7391 unsigned long int *hashcodes;
7392 struct hash_codes_info hashinf;
7393 bfd_size_type amt;
7394 unsigned long int nsyms;
7395 size_t bucketcount;
7396 size_t hash_entry_size;
7397
7398 /* Compute the hash values for all exported symbols. At the same
7399 time store the values in an array so that we could use them for
7400 optimizations. */
7401 amt = dynsymcount * sizeof (unsigned long int);
7402 hashcodes = (unsigned long int *) bfd_malloc (amt);
7403 if (hashcodes == NULL)
7404 return FALSE;
7405 hashinf.hashcodes = hashcodes;
7406 hashinf.error = FALSE;
7407
7408 /* Put all hash values in HASHCODES. */
7409 elf_link_hash_traverse (elf_hash_table (info),
7410 elf_collect_hash_codes, &hashinf);
7411 if (hashinf.error)
7412 {
7413 free (hashcodes);
7414 return FALSE;
7415 }
7416
7417 nsyms = hashinf.hashcodes - hashcodes;
7418 bucketcount
7419 = compute_bucket_count (info, hashcodes, nsyms, 0);
7420 free (hashcodes);
7421
7422 if (bucketcount == 0 && nsyms > 0)
7423 return FALSE;
7424
7425 elf_hash_table (info)->bucketcount = bucketcount;
7426
7427 s = bfd_get_linker_section (dynobj, ".hash");
7428 BFD_ASSERT (s != NULL);
7429 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7430 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7431 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7432 if (s->contents == NULL)
7433 return FALSE;
7434
7435 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7436 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7437 s->contents + hash_entry_size);
7438 }
7439
7440 if (info->emit_gnu_hash)
7441 {
7442 size_t i, cnt;
7443 unsigned char *contents;
7444 struct collect_gnu_hash_codes cinfo;
7445 bfd_size_type amt;
7446 size_t bucketcount;
7447
7448 memset (&cinfo, 0, sizeof (cinfo));
7449
7450 /* Compute the hash values for all exported symbols. At the same
7451 time store the values in an array so that we could use them for
7452 optimizations. */
7453 amt = dynsymcount * 2 * sizeof (unsigned long int);
7454 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7455 if (cinfo.hashcodes == NULL)
7456 return FALSE;
7457
7458 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7459 cinfo.min_dynindx = -1;
7460 cinfo.output_bfd = output_bfd;
7461 cinfo.bed = bed;
7462
7463 /* Put all hash values in HASHCODES. */
7464 elf_link_hash_traverse (elf_hash_table (info),
7465 elf_collect_gnu_hash_codes, &cinfo);
7466 if (cinfo.error)
7467 {
7468 free (cinfo.hashcodes);
7469 return FALSE;
7470 }
7471
7472 bucketcount
7473 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7474
7475 if (bucketcount == 0)
7476 {
7477 free (cinfo.hashcodes);
7478 return FALSE;
7479 }
7480
7481 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7482 BFD_ASSERT (s != NULL);
7483
7484 if (cinfo.nsyms == 0)
7485 {
7486 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7487 BFD_ASSERT (cinfo.min_dynindx == -1);
7488 free (cinfo.hashcodes);
7489 s->size = 5 * 4 + bed->s->arch_size / 8;
7490 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7491 if (contents == NULL)
7492 return FALSE;
7493 s->contents = contents;
7494 /* 1 empty bucket. */
7495 bfd_put_32 (output_bfd, 1, contents);
7496 /* SYMIDX above the special symbol 0. */
7497 bfd_put_32 (output_bfd, 1, contents + 4);
7498 /* Just one word for bitmask. */
7499 bfd_put_32 (output_bfd, 1, contents + 8);
7500 /* Only hash fn bloom filter. */
7501 bfd_put_32 (output_bfd, 0, contents + 12);
7502 /* No hashes are valid - empty bitmask. */
7503 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7504 /* No hashes in the only bucket. */
7505 bfd_put_32 (output_bfd, 0,
7506 contents + 16 + bed->s->arch_size / 8);
7507 }
7508 else
7509 {
7510 unsigned long int maskwords, maskbitslog2, x;
7511 BFD_ASSERT (cinfo.min_dynindx != -1);
7512
7513 x = cinfo.nsyms;
7514 maskbitslog2 = 1;
7515 while ((x >>= 1) != 0)
7516 ++maskbitslog2;
7517 if (maskbitslog2 < 3)
7518 maskbitslog2 = 5;
7519 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7520 maskbitslog2 = maskbitslog2 + 3;
7521 else
7522 maskbitslog2 = maskbitslog2 + 2;
7523 if (bed->s->arch_size == 64)
7524 {
7525 if (maskbitslog2 == 5)
7526 maskbitslog2 = 6;
7527 cinfo.shift1 = 6;
7528 }
7529 else
7530 cinfo.shift1 = 5;
7531 cinfo.mask = (1 << cinfo.shift1) - 1;
7532 cinfo.shift2 = maskbitslog2;
7533 cinfo.maskbits = 1 << maskbitslog2;
7534 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7535 amt = bucketcount * sizeof (unsigned long int) * 2;
7536 amt += maskwords * sizeof (bfd_vma);
7537 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7538 if (cinfo.bitmask == NULL)
7539 {
7540 free (cinfo.hashcodes);
7541 return FALSE;
7542 }
7543
7544 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7545 cinfo.indx = cinfo.counts + bucketcount;
7546 cinfo.symindx = dynsymcount - cinfo.nsyms;
7547 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7548
7549 /* Determine how often each hash bucket is used. */
7550 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7551 for (i = 0; i < cinfo.nsyms; ++i)
7552 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7553
7554 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7555 if (cinfo.counts[i] != 0)
7556 {
7557 cinfo.indx[i] = cnt;
7558 cnt += cinfo.counts[i];
7559 }
7560 BFD_ASSERT (cnt == dynsymcount);
7561 cinfo.bucketcount = bucketcount;
7562 cinfo.local_indx = cinfo.min_dynindx;
7563
7564 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7565 s->size += cinfo.maskbits / 8;
7566 if (bed->record_xhash_symbol != NULL)
7567 s->size += cinfo.nsyms * 4;
7568 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7569 if (contents == NULL)
7570 {
7571 free (cinfo.bitmask);
7572 free (cinfo.hashcodes);
7573 return FALSE;
7574 }
7575
7576 s->contents = contents;
7577 bfd_put_32 (output_bfd, bucketcount, contents);
7578 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7579 bfd_put_32 (output_bfd, maskwords, contents + 8);
7580 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7581 contents += 16 + cinfo.maskbits / 8;
7582
7583 for (i = 0; i < bucketcount; ++i)
7584 {
7585 if (cinfo.counts[i] == 0)
7586 bfd_put_32 (output_bfd, 0, contents);
7587 else
7588 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7589 contents += 4;
7590 }
7591
7592 cinfo.contents = contents;
7593
7594 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7595 /* Renumber dynamic symbols, if populating .gnu.hash section.
7596 If using .MIPS.xhash, populate the translation table. */
7597 elf_link_hash_traverse (elf_hash_table (info),
7598 elf_gnu_hash_process_symidx, &cinfo);
7599
7600 contents = s->contents + 16;
7601 for (i = 0; i < maskwords; ++i)
7602 {
7603 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7604 contents);
7605 contents += bed->s->arch_size / 8;
7606 }
7607
7608 free (cinfo.bitmask);
7609 free (cinfo.hashcodes);
7610 }
7611 }
7612
7613 s = bfd_get_linker_section (dynobj, ".dynstr");
7614 BFD_ASSERT (s != NULL);
7615
7616 elf_finalize_dynstr (output_bfd, info);
7617
7618 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7619
7620 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7621 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7622 return FALSE;
7623 }
7624
7625 return TRUE;
7626 }
7627 \f
7628 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7629
7630 static void
7631 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7632 asection *sec)
7633 {
7634 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7635 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7636 }
7637
7638 /* Finish SHF_MERGE section merging. */
7639
7640 bfd_boolean
7641 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7642 {
7643 bfd *ibfd;
7644 asection *sec;
7645
7646 if (!is_elf_hash_table (info->hash))
7647 return FALSE;
7648
7649 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7650 if ((ibfd->flags & DYNAMIC) == 0
7651 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7652 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7653 == get_elf_backend_data (obfd)->s->elfclass))
7654 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7655 if ((sec->flags & SEC_MERGE) != 0
7656 && !bfd_is_abs_section (sec->output_section))
7657 {
7658 struct bfd_elf_section_data *secdata;
7659
7660 secdata = elf_section_data (sec);
7661 if (! _bfd_add_merge_section (obfd,
7662 &elf_hash_table (info)->merge_info,
7663 sec, &secdata->sec_info))
7664 return FALSE;
7665 else if (secdata->sec_info)
7666 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7667 }
7668
7669 if (elf_hash_table (info)->merge_info != NULL)
7670 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7671 merge_sections_remove_hook);
7672 return TRUE;
7673 }
7674
7675 /* Create an entry in an ELF linker hash table. */
7676
7677 struct bfd_hash_entry *
7678 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7679 struct bfd_hash_table *table,
7680 const char *string)
7681 {
7682 /* Allocate the structure if it has not already been allocated by a
7683 subclass. */
7684 if (entry == NULL)
7685 {
7686 entry = (struct bfd_hash_entry *)
7687 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7688 if (entry == NULL)
7689 return entry;
7690 }
7691
7692 /* Call the allocation method of the superclass. */
7693 entry = _bfd_link_hash_newfunc (entry, table, string);
7694 if (entry != NULL)
7695 {
7696 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7697 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7698
7699 /* Set local fields. */
7700 ret->indx = -1;
7701 ret->dynindx = -1;
7702 ret->got = htab->init_got_refcount;
7703 ret->plt = htab->init_plt_refcount;
7704 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7705 - offsetof (struct elf_link_hash_entry, size)));
7706 /* Assume that we have been called by a non-ELF symbol reader.
7707 This flag is then reset by the code which reads an ELF input
7708 file. This ensures that a symbol created by a non-ELF symbol
7709 reader will have the flag set correctly. */
7710 ret->non_elf = 1;
7711 }
7712
7713 return entry;
7714 }
7715
7716 /* Copy data from an indirect symbol to its direct symbol, hiding the
7717 old indirect symbol. Also used for copying flags to a weakdef. */
7718
7719 void
7720 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7721 struct elf_link_hash_entry *dir,
7722 struct elf_link_hash_entry *ind)
7723 {
7724 struct elf_link_hash_table *htab;
7725
7726 if (ind->dyn_relocs != NULL)
7727 {
7728 if (dir->dyn_relocs != NULL)
7729 {
7730 struct elf_dyn_relocs **pp;
7731 struct elf_dyn_relocs *p;
7732
7733 /* Add reloc counts against the indirect sym to the direct sym
7734 list. Merge any entries against the same section. */
7735 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7736 {
7737 struct elf_dyn_relocs *q;
7738
7739 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7740 if (q->sec == p->sec)
7741 {
7742 q->pc_count += p->pc_count;
7743 q->count += p->count;
7744 *pp = p->next;
7745 break;
7746 }
7747 if (q == NULL)
7748 pp = &p->next;
7749 }
7750 *pp = dir->dyn_relocs;
7751 }
7752
7753 dir->dyn_relocs = ind->dyn_relocs;
7754 ind->dyn_relocs = NULL;
7755 }
7756
7757 /* Copy down any references that we may have already seen to the
7758 symbol which just became indirect. */
7759
7760 if (dir->versioned != versioned_hidden)
7761 dir->ref_dynamic |= ind->ref_dynamic;
7762 dir->ref_regular |= ind->ref_regular;
7763 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7764 dir->non_got_ref |= ind->non_got_ref;
7765 dir->needs_plt |= ind->needs_plt;
7766 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7767
7768 if (ind->root.type != bfd_link_hash_indirect)
7769 return;
7770
7771 /* Copy over the global and procedure linkage table refcount entries.
7772 These may have been already set up by a check_relocs routine. */
7773 htab = elf_hash_table (info);
7774 if (ind->got.refcount > htab->init_got_refcount.refcount)
7775 {
7776 if (dir->got.refcount < 0)
7777 dir->got.refcount = 0;
7778 dir->got.refcount += ind->got.refcount;
7779 ind->got.refcount = htab->init_got_refcount.refcount;
7780 }
7781
7782 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7783 {
7784 if (dir->plt.refcount < 0)
7785 dir->plt.refcount = 0;
7786 dir->plt.refcount += ind->plt.refcount;
7787 ind->plt.refcount = htab->init_plt_refcount.refcount;
7788 }
7789
7790 if (ind->dynindx != -1)
7791 {
7792 if (dir->dynindx != -1)
7793 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7794 dir->dynindx = ind->dynindx;
7795 dir->dynstr_index = ind->dynstr_index;
7796 ind->dynindx = -1;
7797 ind->dynstr_index = 0;
7798 }
7799 }
7800
7801 void
7802 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7803 struct elf_link_hash_entry *h,
7804 bfd_boolean force_local)
7805 {
7806 /* STT_GNU_IFUNC symbol must go through PLT. */
7807 if (h->type != STT_GNU_IFUNC)
7808 {
7809 h->plt = elf_hash_table (info)->init_plt_offset;
7810 h->needs_plt = 0;
7811 }
7812 if (force_local)
7813 {
7814 h->forced_local = 1;
7815 if (h->dynindx != -1)
7816 {
7817 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7818 h->dynstr_index);
7819 h->dynindx = -1;
7820 h->dynstr_index = 0;
7821 }
7822 }
7823 }
7824
7825 /* Hide a symbol. */
7826
7827 void
7828 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7829 struct bfd_link_info *info,
7830 struct bfd_link_hash_entry *h)
7831 {
7832 if (is_elf_hash_table (info->hash))
7833 {
7834 const struct elf_backend_data *bed
7835 = get_elf_backend_data (output_bfd);
7836 struct elf_link_hash_entry *eh
7837 = (struct elf_link_hash_entry *) h;
7838 bed->elf_backend_hide_symbol (info, eh, TRUE);
7839 eh->def_dynamic = 0;
7840 eh->ref_dynamic = 0;
7841 eh->dynamic_def = 0;
7842 }
7843 }
7844
7845 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7846 caller. */
7847
7848 bfd_boolean
7849 _bfd_elf_link_hash_table_init
7850 (struct elf_link_hash_table *table,
7851 bfd *abfd,
7852 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7853 struct bfd_hash_table *,
7854 const char *),
7855 unsigned int entsize,
7856 enum elf_target_id target_id)
7857 {
7858 bfd_boolean ret;
7859 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7860
7861 table->init_got_refcount.refcount = can_refcount - 1;
7862 table->init_plt_refcount.refcount = can_refcount - 1;
7863 table->init_got_offset.offset = -(bfd_vma) 1;
7864 table->init_plt_offset.offset = -(bfd_vma) 1;
7865 /* The first dynamic symbol is a dummy. */
7866 table->dynsymcount = 1;
7867
7868 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7869
7870 table->root.type = bfd_link_elf_hash_table;
7871 table->hash_table_id = target_id;
7872 table->target_os = get_elf_backend_data (abfd)->target_os;
7873
7874 return ret;
7875 }
7876
7877 /* Create an ELF linker hash table. */
7878
7879 struct bfd_link_hash_table *
7880 _bfd_elf_link_hash_table_create (bfd *abfd)
7881 {
7882 struct elf_link_hash_table *ret;
7883 size_t amt = sizeof (struct elf_link_hash_table);
7884
7885 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7886 if (ret == NULL)
7887 return NULL;
7888
7889 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7890 sizeof (struct elf_link_hash_entry),
7891 GENERIC_ELF_DATA))
7892 {
7893 free (ret);
7894 return NULL;
7895 }
7896 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7897
7898 return &ret->root;
7899 }
7900
7901 /* Destroy an ELF linker hash table. */
7902
7903 void
7904 _bfd_elf_link_hash_table_free (bfd *obfd)
7905 {
7906 struct elf_link_hash_table *htab;
7907
7908 htab = (struct elf_link_hash_table *) obfd->link.hash;
7909 if (htab->dynstr != NULL)
7910 _bfd_elf_strtab_free (htab->dynstr);
7911 _bfd_merge_sections_free (htab->merge_info);
7912 _bfd_generic_link_hash_table_free (obfd);
7913 }
7914
7915 /* This is a hook for the ELF emulation code in the generic linker to
7916 tell the backend linker what file name to use for the DT_NEEDED
7917 entry for a dynamic object. */
7918
7919 void
7920 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7921 {
7922 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7923 && bfd_get_format (abfd) == bfd_object)
7924 elf_dt_name (abfd) = name;
7925 }
7926
7927 int
7928 bfd_elf_get_dyn_lib_class (bfd *abfd)
7929 {
7930 int lib_class;
7931 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7932 && bfd_get_format (abfd) == bfd_object)
7933 lib_class = elf_dyn_lib_class (abfd);
7934 else
7935 lib_class = 0;
7936 return lib_class;
7937 }
7938
7939 void
7940 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7941 {
7942 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7943 && bfd_get_format (abfd) == bfd_object)
7944 elf_dyn_lib_class (abfd) = lib_class;
7945 }
7946
7947 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7948 the linker ELF emulation code. */
7949
7950 struct bfd_link_needed_list *
7951 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7952 struct bfd_link_info *info)
7953 {
7954 if (! is_elf_hash_table (info->hash))
7955 return NULL;
7956 return elf_hash_table (info)->needed;
7957 }
7958
7959 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7960 hook for the linker ELF emulation code. */
7961
7962 struct bfd_link_needed_list *
7963 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7964 struct bfd_link_info *info)
7965 {
7966 if (! is_elf_hash_table (info->hash))
7967 return NULL;
7968 return elf_hash_table (info)->runpath;
7969 }
7970
7971 /* Get the name actually used for a dynamic object for a link. This
7972 is the SONAME entry if there is one. Otherwise, it is the string
7973 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7974
7975 const char *
7976 bfd_elf_get_dt_soname (bfd *abfd)
7977 {
7978 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7979 && bfd_get_format (abfd) == bfd_object)
7980 return elf_dt_name (abfd);
7981 return NULL;
7982 }
7983
7984 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7985 the ELF linker emulation code. */
7986
7987 bfd_boolean
7988 bfd_elf_get_bfd_needed_list (bfd *abfd,
7989 struct bfd_link_needed_list **pneeded)
7990 {
7991 asection *s;
7992 bfd_byte *dynbuf = NULL;
7993 unsigned int elfsec;
7994 unsigned long shlink;
7995 bfd_byte *extdyn, *extdynend;
7996 size_t extdynsize;
7997 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7998
7999 *pneeded = NULL;
8000
8001 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8002 || bfd_get_format (abfd) != bfd_object)
8003 return TRUE;
8004
8005 s = bfd_get_section_by_name (abfd, ".dynamic");
8006 if (s == NULL || s->size == 0)
8007 return TRUE;
8008
8009 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8010 goto error_return;
8011
8012 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8013 if (elfsec == SHN_BAD)
8014 goto error_return;
8015
8016 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8017
8018 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8019 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8020
8021 extdyn = dynbuf;
8022 extdynend = extdyn + s->size;
8023 for (; extdyn < extdynend; extdyn += extdynsize)
8024 {
8025 Elf_Internal_Dyn dyn;
8026
8027 (*swap_dyn_in) (abfd, extdyn, &dyn);
8028
8029 if (dyn.d_tag == DT_NULL)
8030 break;
8031
8032 if (dyn.d_tag == DT_NEEDED)
8033 {
8034 const char *string;
8035 struct bfd_link_needed_list *l;
8036 unsigned int tagv = dyn.d_un.d_val;
8037 size_t amt;
8038
8039 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8040 if (string == NULL)
8041 goto error_return;
8042
8043 amt = sizeof *l;
8044 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8045 if (l == NULL)
8046 goto error_return;
8047
8048 l->by = abfd;
8049 l->name = string;
8050 l->next = *pneeded;
8051 *pneeded = l;
8052 }
8053 }
8054
8055 free (dynbuf);
8056
8057 return TRUE;
8058
8059 error_return:
8060 free (dynbuf);
8061 return FALSE;
8062 }
8063
8064 struct elf_symbuf_symbol
8065 {
8066 unsigned long st_name; /* Symbol name, index in string tbl */
8067 unsigned char st_info; /* Type and binding attributes */
8068 unsigned char st_other; /* Visibilty, and target specific */
8069 };
8070
8071 struct elf_symbuf_head
8072 {
8073 struct elf_symbuf_symbol *ssym;
8074 size_t count;
8075 unsigned int st_shndx;
8076 };
8077
8078 struct elf_symbol
8079 {
8080 union
8081 {
8082 Elf_Internal_Sym *isym;
8083 struct elf_symbuf_symbol *ssym;
8084 void *p;
8085 } u;
8086 const char *name;
8087 };
8088
8089 /* Sort references to symbols by ascending section number. */
8090
8091 static int
8092 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8093 {
8094 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8095 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8096
8097 if (s1->st_shndx != s2->st_shndx)
8098 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8099 /* Final sort by the address of the sym in the symbuf ensures
8100 a stable sort. */
8101 if (s1 != s2)
8102 return s1 > s2 ? 1 : -1;
8103 return 0;
8104 }
8105
8106 static int
8107 elf_sym_name_compare (const void *arg1, const void *arg2)
8108 {
8109 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8110 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8111 int ret = strcmp (s1->name, s2->name);
8112 if (ret != 0)
8113 return ret;
8114 if (s1->u.p != s2->u.p)
8115 return s1->u.p > s2->u.p ? 1 : -1;
8116 return 0;
8117 }
8118
8119 static struct elf_symbuf_head *
8120 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8121 {
8122 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8123 struct elf_symbuf_symbol *ssym;
8124 struct elf_symbuf_head *ssymbuf, *ssymhead;
8125 size_t i, shndx_count, total_size, amt;
8126
8127 amt = symcount * sizeof (*indbuf);
8128 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8129 if (indbuf == NULL)
8130 return NULL;
8131
8132 /* NB: When checking if 2 sections define the same set of local and
8133 global symbols, ignore both undefined and section symbols in the
8134 symbol table. */
8135 for (ind = indbuf, i = 0; i < symcount; i++)
8136 if (isymbuf[i].st_shndx != SHN_UNDEF
8137 && ELF_ST_TYPE (isymbuf[i].st_info) != STT_SECTION)
8138 *ind++ = &isymbuf[i];
8139 indbufend = ind;
8140
8141 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8142 elf_sort_elf_symbol);
8143
8144 shndx_count = 0;
8145 if (indbufend > indbuf)
8146 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8147 if (ind[0]->st_shndx != ind[1]->st_shndx)
8148 shndx_count++;
8149
8150 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8151 + (indbufend - indbuf) * sizeof (*ssym));
8152 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8153 if (ssymbuf == NULL)
8154 {
8155 free (indbuf);
8156 return NULL;
8157 }
8158
8159 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8160 ssymbuf->ssym = NULL;
8161 ssymbuf->count = shndx_count;
8162 ssymbuf->st_shndx = 0;
8163 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8164 {
8165 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8166 {
8167 ssymhead++;
8168 ssymhead->ssym = ssym;
8169 ssymhead->count = 0;
8170 ssymhead->st_shndx = (*ind)->st_shndx;
8171 }
8172 ssym->st_name = (*ind)->st_name;
8173 ssym->st_info = (*ind)->st_info;
8174 ssym->st_other = (*ind)->st_other;
8175 ssymhead->count++;
8176 }
8177 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8178 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8179 == total_size));
8180
8181 free (indbuf);
8182 return ssymbuf;
8183 }
8184
8185 /* Check if 2 sections define the same set of local and global
8186 symbols. */
8187
8188 static bfd_boolean
8189 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8190 struct bfd_link_info *info)
8191 {
8192 bfd *bfd1, *bfd2;
8193 const struct elf_backend_data *bed1, *bed2;
8194 Elf_Internal_Shdr *hdr1, *hdr2;
8195 size_t symcount1, symcount2;
8196 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8197 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8198 Elf_Internal_Sym *isym, *isymend;
8199 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8200 size_t count1, count2, i;
8201 unsigned int shndx1, shndx2;
8202 bfd_boolean result;
8203
8204 bfd1 = sec1->owner;
8205 bfd2 = sec2->owner;
8206
8207 /* Both sections have to be in ELF. */
8208 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8209 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8210 return FALSE;
8211
8212 if (elf_section_type (sec1) != elf_section_type (sec2))
8213 return FALSE;
8214
8215 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8216 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8217 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8218 return FALSE;
8219
8220 bed1 = get_elf_backend_data (bfd1);
8221 bed2 = get_elf_backend_data (bfd2);
8222 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8223 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8224 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8225 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8226
8227 if (symcount1 == 0 || symcount2 == 0)
8228 return FALSE;
8229
8230 result = FALSE;
8231 isymbuf1 = NULL;
8232 isymbuf2 = NULL;
8233 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8234 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8235
8236 if (ssymbuf1 == NULL)
8237 {
8238 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8239 NULL, NULL, NULL);
8240 if (isymbuf1 == NULL)
8241 goto done;
8242
8243 if (info != NULL && !info->reduce_memory_overheads)
8244 {
8245 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8246 elf_tdata (bfd1)->symbuf = ssymbuf1;
8247 }
8248 }
8249
8250 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8251 {
8252 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8253 NULL, NULL, NULL);
8254 if (isymbuf2 == NULL)
8255 goto done;
8256
8257 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8258 {
8259 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8260 elf_tdata (bfd2)->symbuf = ssymbuf2;
8261 }
8262 }
8263
8264 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8265 {
8266 /* Optimized faster version. */
8267 size_t lo, hi, mid;
8268 struct elf_symbol *symp;
8269 struct elf_symbuf_symbol *ssym, *ssymend;
8270
8271 lo = 0;
8272 hi = ssymbuf1->count;
8273 ssymbuf1++;
8274 count1 = 0;
8275 while (lo < hi)
8276 {
8277 mid = (lo + hi) / 2;
8278 if (shndx1 < ssymbuf1[mid].st_shndx)
8279 hi = mid;
8280 else if (shndx1 > ssymbuf1[mid].st_shndx)
8281 lo = mid + 1;
8282 else
8283 {
8284 count1 = ssymbuf1[mid].count;
8285 ssymbuf1 += mid;
8286 break;
8287 }
8288 }
8289
8290 lo = 0;
8291 hi = ssymbuf2->count;
8292 ssymbuf2++;
8293 count2 = 0;
8294 while (lo < hi)
8295 {
8296 mid = (lo + hi) / 2;
8297 if (shndx2 < ssymbuf2[mid].st_shndx)
8298 hi = mid;
8299 else if (shndx2 > ssymbuf2[mid].st_shndx)
8300 lo = mid + 1;
8301 else
8302 {
8303 count2 = ssymbuf2[mid].count;
8304 ssymbuf2 += mid;
8305 break;
8306 }
8307 }
8308
8309 if (count1 == 0 || count2 == 0 || count1 != count2)
8310 goto done;
8311
8312 symtable1
8313 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8314 symtable2
8315 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8316 if (symtable1 == NULL || symtable2 == NULL)
8317 goto done;
8318
8319 symp = symtable1;
8320 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8321 ssym < ssymend; ssym++, symp++)
8322 {
8323 symp->u.ssym = ssym;
8324 symp->name = bfd_elf_string_from_elf_section (bfd1,
8325 hdr1->sh_link,
8326 ssym->st_name);
8327 }
8328
8329 symp = symtable2;
8330 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8331 ssym < ssymend; ssym++, symp++)
8332 {
8333 symp->u.ssym = ssym;
8334 symp->name = bfd_elf_string_from_elf_section (bfd2,
8335 hdr2->sh_link,
8336 ssym->st_name);
8337 }
8338
8339 /* Sort symbol by name. */
8340 qsort (symtable1, count1, sizeof (struct elf_symbol),
8341 elf_sym_name_compare);
8342 qsort (symtable2, count1, sizeof (struct elf_symbol),
8343 elf_sym_name_compare);
8344
8345 for (i = 0; i < count1; i++)
8346 /* Two symbols must have the same binding, type and name. */
8347 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8348 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8349 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8350 goto done;
8351
8352 result = TRUE;
8353 goto done;
8354 }
8355
8356 symtable1 = (struct elf_symbol *)
8357 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8358 symtable2 = (struct elf_symbol *)
8359 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8360 if (symtable1 == NULL || symtable2 == NULL)
8361 goto done;
8362
8363 /* Count definitions in the section. */
8364 count1 = 0;
8365 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8366 if (isym->st_shndx == shndx1)
8367 symtable1[count1++].u.isym = isym;
8368
8369 count2 = 0;
8370 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8371 if (isym->st_shndx == shndx2)
8372 symtable2[count2++].u.isym = isym;
8373
8374 if (count1 == 0 || count2 == 0 || count1 != count2)
8375 goto done;
8376
8377 for (i = 0; i < count1; i++)
8378 symtable1[i].name
8379 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8380 symtable1[i].u.isym->st_name);
8381
8382 for (i = 0; i < count2; i++)
8383 symtable2[i].name
8384 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8385 symtable2[i].u.isym->st_name);
8386
8387 /* Sort symbol by name. */
8388 qsort (symtable1, count1, sizeof (struct elf_symbol),
8389 elf_sym_name_compare);
8390 qsort (symtable2, count1, sizeof (struct elf_symbol),
8391 elf_sym_name_compare);
8392
8393 for (i = 0; i < count1; i++)
8394 /* Two symbols must have the same binding, type and name. */
8395 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8396 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8397 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8398 goto done;
8399
8400 result = TRUE;
8401
8402 done:
8403 free (symtable1);
8404 free (symtable2);
8405 free (isymbuf1);
8406 free (isymbuf2);
8407
8408 return result;
8409 }
8410
8411 /* Return TRUE if 2 section types are compatible. */
8412
8413 bfd_boolean
8414 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8415 bfd *bbfd, const asection *bsec)
8416 {
8417 if (asec == NULL
8418 || bsec == NULL
8419 || abfd->xvec->flavour != bfd_target_elf_flavour
8420 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8421 return TRUE;
8422
8423 return elf_section_type (asec) == elf_section_type (bsec);
8424 }
8425 \f
8426 /* Final phase of ELF linker. */
8427
8428 /* A structure we use to avoid passing large numbers of arguments. */
8429
8430 struct elf_final_link_info
8431 {
8432 /* General link information. */
8433 struct bfd_link_info *info;
8434 /* Output BFD. */
8435 bfd *output_bfd;
8436 /* Symbol string table. */
8437 struct elf_strtab_hash *symstrtab;
8438 /* .hash section. */
8439 asection *hash_sec;
8440 /* symbol version section (.gnu.version). */
8441 asection *symver_sec;
8442 /* Buffer large enough to hold contents of any section. */
8443 bfd_byte *contents;
8444 /* Buffer large enough to hold external relocs of any section. */
8445 void *external_relocs;
8446 /* Buffer large enough to hold internal relocs of any section. */
8447 Elf_Internal_Rela *internal_relocs;
8448 /* Buffer large enough to hold external local symbols of any input
8449 BFD. */
8450 bfd_byte *external_syms;
8451 /* And a buffer for symbol section indices. */
8452 Elf_External_Sym_Shndx *locsym_shndx;
8453 /* Buffer large enough to hold internal local symbols of any input
8454 BFD. */
8455 Elf_Internal_Sym *internal_syms;
8456 /* Array large enough to hold a symbol index for each local symbol
8457 of any input BFD. */
8458 long *indices;
8459 /* Array large enough to hold a section pointer for each local
8460 symbol of any input BFD. */
8461 asection **sections;
8462 /* Buffer for SHT_SYMTAB_SHNDX section. */
8463 Elf_External_Sym_Shndx *symshndxbuf;
8464 /* Number of STT_FILE syms seen. */
8465 size_t filesym_count;
8466 /* Local symbol hash table. */
8467 struct bfd_hash_table local_hash_table;
8468 };
8469
8470 struct local_hash_entry
8471 {
8472 /* Base hash table entry structure. */
8473 struct bfd_hash_entry root;
8474 /* Size of the local symbol name. */
8475 size_t size;
8476 /* Number of the duplicated local symbol names. */
8477 long count;
8478 };
8479
8480 /* Create an entry in the local symbol hash table. */
8481
8482 static struct bfd_hash_entry *
8483 local_hash_newfunc (struct bfd_hash_entry *entry,
8484 struct bfd_hash_table *table,
8485 const char *string)
8486 {
8487
8488 /* Allocate the structure if it has not already been allocated by a
8489 subclass. */
8490 if (entry == NULL)
8491 {
8492 entry = bfd_hash_allocate (table,
8493 sizeof (struct local_hash_entry));
8494 if (entry == NULL)
8495 return entry;
8496 }
8497
8498 /* Call the allocation method of the superclass. */
8499 entry = bfd_hash_newfunc (entry, table, string);
8500 if (entry != NULL)
8501 {
8502 ((struct local_hash_entry *) entry)->count = 0;
8503 ((struct local_hash_entry *) entry)->size = 0;
8504 }
8505
8506 return entry;
8507 }
8508
8509 /* This struct is used to pass information to elf_link_output_extsym. */
8510
8511 struct elf_outext_info
8512 {
8513 bfd_boolean failed;
8514 bfd_boolean localsyms;
8515 bfd_boolean file_sym_done;
8516 struct elf_final_link_info *flinfo;
8517 };
8518
8519
8520 /* Support for evaluating a complex relocation.
8521
8522 Complex relocations are generalized, self-describing relocations. The
8523 implementation of them consists of two parts: complex symbols, and the
8524 relocations themselves.
8525
8526 The relocations use a reserved elf-wide relocation type code (R_RELC
8527 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8528 information (start bit, end bit, word width, etc) into the addend. This
8529 information is extracted from CGEN-generated operand tables within gas.
8530
8531 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8532 internal) representing prefix-notation expressions, including but not
8533 limited to those sorts of expressions normally encoded as addends in the
8534 addend field. The symbol mangling format is:
8535
8536 <node> := <literal>
8537 | <unary-operator> ':' <node>
8538 | <binary-operator> ':' <node> ':' <node>
8539 ;
8540
8541 <literal> := 's' <digits=N> ':' <N character symbol name>
8542 | 'S' <digits=N> ':' <N character section name>
8543 | '#' <hexdigits>
8544 ;
8545
8546 <binary-operator> := as in C
8547 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8548
8549 static void
8550 set_symbol_value (bfd *bfd_with_globals,
8551 Elf_Internal_Sym *isymbuf,
8552 size_t locsymcount,
8553 size_t symidx,
8554 bfd_vma val)
8555 {
8556 struct elf_link_hash_entry **sym_hashes;
8557 struct elf_link_hash_entry *h;
8558 size_t extsymoff = locsymcount;
8559
8560 if (symidx < locsymcount)
8561 {
8562 Elf_Internal_Sym *sym;
8563
8564 sym = isymbuf + symidx;
8565 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8566 {
8567 /* It is a local symbol: move it to the
8568 "absolute" section and give it a value. */
8569 sym->st_shndx = SHN_ABS;
8570 sym->st_value = val;
8571 return;
8572 }
8573 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8574 extsymoff = 0;
8575 }
8576
8577 /* It is a global symbol: set its link type
8578 to "defined" and give it a value. */
8579
8580 sym_hashes = elf_sym_hashes (bfd_with_globals);
8581 h = sym_hashes [symidx - extsymoff];
8582 while (h->root.type == bfd_link_hash_indirect
8583 || h->root.type == bfd_link_hash_warning)
8584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8585 h->root.type = bfd_link_hash_defined;
8586 h->root.u.def.value = val;
8587 h->root.u.def.section = bfd_abs_section_ptr;
8588 }
8589
8590 static bfd_boolean
8591 resolve_symbol (const char *name,
8592 bfd *input_bfd,
8593 struct elf_final_link_info *flinfo,
8594 bfd_vma *result,
8595 Elf_Internal_Sym *isymbuf,
8596 size_t locsymcount)
8597 {
8598 Elf_Internal_Sym *sym;
8599 struct bfd_link_hash_entry *global_entry;
8600 const char *candidate = NULL;
8601 Elf_Internal_Shdr *symtab_hdr;
8602 size_t i;
8603
8604 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8605
8606 for (i = 0; i < locsymcount; ++ i)
8607 {
8608 sym = isymbuf + i;
8609
8610 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8611 continue;
8612
8613 candidate = bfd_elf_string_from_elf_section (input_bfd,
8614 symtab_hdr->sh_link,
8615 sym->st_name);
8616 #ifdef DEBUG
8617 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8618 name, candidate, (unsigned long) sym->st_value);
8619 #endif
8620 if (candidate && strcmp (candidate, name) == 0)
8621 {
8622 asection *sec = flinfo->sections [i];
8623
8624 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8625 *result += sec->output_offset + sec->output_section->vma;
8626 #ifdef DEBUG
8627 printf ("Found symbol with value %8.8lx\n",
8628 (unsigned long) *result);
8629 #endif
8630 return TRUE;
8631 }
8632 }
8633
8634 /* Hmm, haven't found it yet. perhaps it is a global. */
8635 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8636 FALSE, FALSE, TRUE);
8637 if (!global_entry)
8638 return FALSE;
8639
8640 if (global_entry->type == bfd_link_hash_defined
8641 || global_entry->type == bfd_link_hash_defweak)
8642 {
8643 *result = (global_entry->u.def.value
8644 + global_entry->u.def.section->output_section->vma
8645 + global_entry->u.def.section->output_offset);
8646 #ifdef DEBUG
8647 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8648 global_entry->root.string, (unsigned long) *result);
8649 #endif
8650 return TRUE;
8651 }
8652
8653 return FALSE;
8654 }
8655
8656 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8657 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8658 names like "foo.end" which is the end address of section "foo". */
8659
8660 static bfd_boolean
8661 resolve_section (const char *name,
8662 asection *sections,
8663 bfd_vma *result,
8664 bfd * abfd)
8665 {
8666 asection *curr;
8667 unsigned int len;
8668
8669 for (curr = sections; curr; curr = curr->next)
8670 if (strcmp (curr->name, name) == 0)
8671 {
8672 *result = curr->vma;
8673 return TRUE;
8674 }
8675
8676 /* Hmm. still haven't found it. try pseudo-section names. */
8677 /* FIXME: This could be coded more efficiently... */
8678 for (curr = sections; curr; curr = curr->next)
8679 {
8680 len = strlen (curr->name);
8681 if (len > strlen (name))
8682 continue;
8683
8684 if (strncmp (curr->name, name, len) == 0)
8685 {
8686 if (strncmp (".end", name + len, 4) == 0)
8687 {
8688 *result = (curr->vma
8689 + curr->size / bfd_octets_per_byte (abfd, curr));
8690 return TRUE;
8691 }
8692
8693 /* Insert more pseudo-section names here, if you like. */
8694 }
8695 }
8696
8697 return FALSE;
8698 }
8699
8700 static void
8701 undefined_reference (const char *reftype, const char *name)
8702 {
8703 /* xgettext:c-format */
8704 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8705 reftype, name);
8706 bfd_set_error (bfd_error_bad_value);
8707 }
8708
8709 static bfd_boolean
8710 eval_symbol (bfd_vma *result,
8711 const char **symp,
8712 bfd *input_bfd,
8713 struct elf_final_link_info *flinfo,
8714 bfd_vma dot,
8715 Elf_Internal_Sym *isymbuf,
8716 size_t locsymcount,
8717 int signed_p)
8718 {
8719 size_t len;
8720 size_t symlen;
8721 bfd_vma a;
8722 bfd_vma b;
8723 char symbuf[4096];
8724 const char *sym = *symp;
8725 const char *symend;
8726 bfd_boolean symbol_is_section = FALSE;
8727
8728 len = strlen (sym);
8729 symend = sym + len;
8730
8731 if (len < 1 || len > sizeof (symbuf))
8732 {
8733 bfd_set_error (bfd_error_invalid_operation);
8734 return FALSE;
8735 }
8736
8737 switch (* sym)
8738 {
8739 case '.':
8740 *result = dot;
8741 *symp = sym + 1;
8742 return TRUE;
8743
8744 case '#':
8745 ++sym;
8746 *result = strtoul (sym, (char **) symp, 16);
8747 return TRUE;
8748
8749 case 'S':
8750 symbol_is_section = TRUE;
8751 /* Fall through. */
8752 case 's':
8753 ++sym;
8754 symlen = strtol (sym, (char **) symp, 10);
8755 sym = *symp + 1; /* Skip the trailing ':'. */
8756
8757 if (symend < sym || symlen + 1 > sizeof (symbuf))
8758 {
8759 bfd_set_error (bfd_error_invalid_operation);
8760 return FALSE;
8761 }
8762
8763 memcpy (symbuf, sym, symlen);
8764 symbuf[symlen] = '\0';
8765 *symp = sym + symlen;
8766
8767 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8768 the symbol as a section, or vice-versa. so we're pretty liberal in our
8769 interpretation here; section means "try section first", not "must be a
8770 section", and likewise with symbol. */
8771
8772 if (symbol_is_section)
8773 {
8774 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8775 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8776 isymbuf, locsymcount))
8777 {
8778 undefined_reference ("section", symbuf);
8779 return FALSE;
8780 }
8781 }
8782 else
8783 {
8784 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8785 isymbuf, locsymcount)
8786 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8787 result, input_bfd))
8788 {
8789 undefined_reference ("symbol", symbuf);
8790 return FALSE;
8791 }
8792 }
8793
8794 return TRUE;
8795
8796 /* All that remains are operators. */
8797
8798 #define UNARY_OP(op) \
8799 if (strncmp (sym, #op, strlen (#op)) == 0) \
8800 { \
8801 sym += strlen (#op); \
8802 if (*sym == ':') \
8803 ++sym; \
8804 *symp = sym; \
8805 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8806 isymbuf, locsymcount, signed_p)) \
8807 return FALSE; \
8808 if (signed_p) \
8809 *result = op ((bfd_signed_vma) a); \
8810 else \
8811 *result = op a; \
8812 return TRUE; \
8813 }
8814
8815 #define BINARY_OP_HEAD(op) \
8816 if (strncmp (sym, #op, strlen (#op)) == 0) \
8817 { \
8818 sym += strlen (#op); \
8819 if (*sym == ':') \
8820 ++sym; \
8821 *symp = sym; \
8822 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8823 isymbuf, locsymcount, signed_p)) \
8824 return FALSE; \
8825 ++*symp; \
8826 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8827 isymbuf, locsymcount, signed_p)) \
8828 return FALSE;
8829 #define BINARY_OP_TAIL(op) \
8830 if (signed_p) \
8831 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8832 else \
8833 *result = a op b; \
8834 return TRUE; \
8835 }
8836 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8837
8838 default:
8839 UNARY_OP (0-);
8840 BINARY_OP_HEAD (<<);
8841 if (b >= sizeof (a) * CHAR_BIT)
8842 {
8843 *result = 0;
8844 return TRUE;
8845 }
8846 signed_p = 0;
8847 BINARY_OP_TAIL (<<);
8848 BINARY_OP_HEAD (>>);
8849 if (b >= sizeof (a) * CHAR_BIT)
8850 {
8851 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8852 return TRUE;
8853 }
8854 BINARY_OP_TAIL (>>);
8855 BINARY_OP (==);
8856 BINARY_OP (!=);
8857 BINARY_OP (<=);
8858 BINARY_OP (>=);
8859 BINARY_OP (&&);
8860 BINARY_OP (||);
8861 UNARY_OP (~);
8862 UNARY_OP (!);
8863 BINARY_OP (*);
8864 BINARY_OP_HEAD (/);
8865 if (b == 0)
8866 {
8867 _bfd_error_handler (_("division by zero"));
8868 bfd_set_error (bfd_error_bad_value);
8869 return FALSE;
8870 }
8871 BINARY_OP_TAIL (/);
8872 BINARY_OP_HEAD (%);
8873 if (b == 0)
8874 {
8875 _bfd_error_handler (_("division by zero"));
8876 bfd_set_error (bfd_error_bad_value);
8877 return FALSE;
8878 }
8879 BINARY_OP_TAIL (%);
8880 BINARY_OP (^);
8881 BINARY_OP (|);
8882 BINARY_OP (&);
8883 BINARY_OP (+);
8884 BINARY_OP (-);
8885 BINARY_OP (<);
8886 BINARY_OP (>);
8887 #undef UNARY_OP
8888 #undef BINARY_OP
8889 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8890 bfd_set_error (bfd_error_invalid_operation);
8891 return FALSE;
8892 }
8893 }
8894
8895 static void
8896 put_value (bfd_vma size,
8897 unsigned long chunksz,
8898 bfd *input_bfd,
8899 bfd_vma x,
8900 bfd_byte *location)
8901 {
8902 location += (size - chunksz);
8903
8904 for (; size; size -= chunksz, location -= chunksz)
8905 {
8906 switch (chunksz)
8907 {
8908 case 1:
8909 bfd_put_8 (input_bfd, x, location);
8910 x >>= 8;
8911 break;
8912 case 2:
8913 bfd_put_16 (input_bfd, x, location);
8914 x >>= 16;
8915 break;
8916 case 4:
8917 bfd_put_32 (input_bfd, x, location);
8918 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8919 x >>= 16;
8920 x >>= 16;
8921 break;
8922 #ifdef BFD64
8923 case 8:
8924 bfd_put_64 (input_bfd, x, location);
8925 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8926 x >>= 32;
8927 x >>= 32;
8928 break;
8929 #endif
8930 default:
8931 abort ();
8932 break;
8933 }
8934 }
8935 }
8936
8937 static bfd_vma
8938 get_value (bfd_vma size,
8939 unsigned long chunksz,
8940 bfd *input_bfd,
8941 bfd_byte *location)
8942 {
8943 int shift;
8944 bfd_vma x = 0;
8945
8946 /* Sanity checks. */
8947 BFD_ASSERT (chunksz <= sizeof (x)
8948 && size >= chunksz
8949 && chunksz != 0
8950 && (size % chunksz) == 0
8951 && input_bfd != NULL
8952 && location != NULL);
8953
8954 if (chunksz == sizeof (x))
8955 {
8956 BFD_ASSERT (size == chunksz);
8957
8958 /* Make sure that we do not perform an undefined shift operation.
8959 We know that size == chunksz so there will only be one iteration
8960 of the loop below. */
8961 shift = 0;
8962 }
8963 else
8964 shift = 8 * chunksz;
8965
8966 for (; size; size -= chunksz, location += chunksz)
8967 {
8968 switch (chunksz)
8969 {
8970 case 1:
8971 x = (x << shift) | bfd_get_8 (input_bfd, location);
8972 break;
8973 case 2:
8974 x = (x << shift) | bfd_get_16 (input_bfd, location);
8975 break;
8976 case 4:
8977 x = (x << shift) | bfd_get_32 (input_bfd, location);
8978 break;
8979 #ifdef BFD64
8980 case 8:
8981 x = (x << shift) | bfd_get_64 (input_bfd, location);
8982 break;
8983 #endif
8984 default:
8985 abort ();
8986 }
8987 }
8988 return x;
8989 }
8990
8991 static void
8992 decode_complex_addend (unsigned long *start, /* in bits */
8993 unsigned long *oplen, /* in bits */
8994 unsigned long *len, /* in bits */
8995 unsigned long *wordsz, /* in bytes */
8996 unsigned long *chunksz, /* in bytes */
8997 unsigned long *lsb0_p,
8998 unsigned long *signed_p,
8999 unsigned long *trunc_p,
9000 unsigned long encoded)
9001 {
9002 * start = encoded & 0x3F;
9003 * len = (encoded >> 6) & 0x3F;
9004 * oplen = (encoded >> 12) & 0x3F;
9005 * wordsz = (encoded >> 18) & 0xF;
9006 * chunksz = (encoded >> 22) & 0xF;
9007 * lsb0_p = (encoded >> 27) & 1;
9008 * signed_p = (encoded >> 28) & 1;
9009 * trunc_p = (encoded >> 29) & 1;
9010 }
9011
9012 bfd_reloc_status_type
9013 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9014 asection *input_section,
9015 bfd_byte *contents,
9016 Elf_Internal_Rela *rel,
9017 bfd_vma relocation)
9018 {
9019 bfd_vma shift, x, mask;
9020 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9021 bfd_reloc_status_type r;
9022 bfd_size_type octets;
9023
9024 /* Perform this reloc, since it is complex.
9025 (this is not to say that it necessarily refers to a complex
9026 symbol; merely that it is a self-describing CGEN based reloc.
9027 i.e. the addend has the complete reloc information (bit start, end,
9028 word size, etc) encoded within it.). */
9029
9030 decode_complex_addend (&start, &oplen, &len, &wordsz,
9031 &chunksz, &lsb0_p, &signed_p,
9032 &trunc_p, rel->r_addend);
9033
9034 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9035
9036 if (lsb0_p)
9037 shift = (start + 1) - len;
9038 else
9039 shift = (8 * wordsz) - (start + len);
9040
9041 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9042 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9043
9044 #ifdef DEBUG
9045 printf ("Doing complex reloc: "
9046 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9047 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9048 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9049 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9050 oplen, (unsigned long) x, (unsigned long) mask,
9051 (unsigned long) relocation);
9052 #endif
9053
9054 r = bfd_reloc_ok;
9055 if (! trunc_p)
9056 /* Now do an overflow check. */
9057 r = bfd_check_overflow ((signed_p
9058 ? complain_overflow_signed
9059 : complain_overflow_unsigned),
9060 len, 0, (8 * wordsz),
9061 relocation);
9062
9063 /* Do the deed. */
9064 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9065
9066 #ifdef DEBUG
9067 printf (" relocation: %8.8lx\n"
9068 " shifted mask: %8.8lx\n"
9069 " shifted/masked reloc: %8.8lx\n"
9070 " result: %8.8lx\n",
9071 (unsigned long) relocation, (unsigned long) (mask << shift),
9072 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9073 #endif
9074 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9075 return r;
9076 }
9077
9078 /* Functions to read r_offset from external (target order) reloc
9079 entry. Faster than bfd_getl32 et al, because we let the compiler
9080 know the value is aligned. */
9081
9082 static bfd_vma
9083 ext32l_r_offset (const void *p)
9084 {
9085 union aligned32
9086 {
9087 uint32_t v;
9088 unsigned char c[4];
9089 };
9090 const union aligned32 *a
9091 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9092
9093 uint32_t aval = ( (uint32_t) a->c[0]
9094 | (uint32_t) a->c[1] << 8
9095 | (uint32_t) a->c[2] << 16
9096 | (uint32_t) a->c[3] << 24);
9097 return aval;
9098 }
9099
9100 static bfd_vma
9101 ext32b_r_offset (const void *p)
9102 {
9103 union aligned32
9104 {
9105 uint32_t v;
9106 unsigned char c[4];
9107 };
9108 const union aligned32 *a
9109 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9110
9111 uint32_t aval = ( (uint32_t) a->c[0] << 24
9112 | (uint32_t) a->c[1] << 16
9113 | (uint32_t) a->c[2] << 8
9114 | (uint32_t) a->c[3]);
9115 return aval;
9116 }
9117
9118 #ifdef BFD_HOST_64_BIT
9119 static bfd_vma
9120 ext64l_r_offset (const void *p)
9121 {
9122 union aligned64
9123 {
9124 uint64_t v;
9125 unsigned char c[8];
9126 };
9127 const union aligned64 *a
9128 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9129
9130 uint64_t aval = ( (uint64_t) a->c[0]
9131 | (uint64_t) a->c[1] << 8
9132 | (uint64_t) a->c[2] << 16
9133 | (uint64_t) a->c[3] << 24
9134 | (uint64_t) a->c[4] << 32
9135 | (uint64_t) a->c[5] << 40
9136 | (uint64_t) a->c[6] << 48
9137 | (uint64_t) a->c[7] << 56);
9138 return aval;
9139 }
9140
9141 static bfd_vma
9142 ext64b_r_offset (const void *p)
9143 {
9144 union aligned64
9145 {
9146 uint64_t v;
9147 unsigned char c[8];
9148 };
9149 const union aligned64 *a
9150 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9151
9152 uint64_t aval = ( (uint64_t) a->c[0] << 56
9153 | (uint64_t) a->c[1] << 48
9154 | (uint64_t) a->c[2] << 40
9155 | (uint64_t) a->c[3] << 32
9156 | (uint64_t) a->c[4] << 24
9157 | (uint64_t) a->c[5] << 16
9158 | (uint64_t) a->c[6] << 8
9159 | (uint64_t) a->c[7]);
9160 return aval;
9161 }
9162 #endif
9163
9164 /* When performing a relocatable link, the input relocations are
9165 preserved. But, if they reference global symbols, the indices
9166 referenced must be updated. Update all the relocations found in
9167 RELDATA. */
9168
9169 static bfd_boolean
9170 elf_link_adjust_relocs (bfd *abfd,
9171 asection *sec,
9172 struct bfd_elf_section_reloc_data *reldata,
9173 bfd_boolean sort,
9174 struct bfd_link_info *info)
9175 {
9176 unsigned int i;
9177 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9178 bfd_byte *erela;
9179 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9180 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9181 bfd_vma r_type_mask;
9182 int r_sym_shift;
9183 unsigned int count = reldata->count;
9184 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9185
9186 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9187 {
9188 swap_in = bed->s->swap_reloc_in;
9189 swap_out = bed->s->swap_reloc_out;
9190 }
9191 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9192 {
9193 swap_in = bed->s->swap_reloca_in;
9194 swap_out = bed->s->swap_reloca_out;
9195 }
9196 else
9197 abort ();
9198
9199 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9200 abort ();
9201
9202 if (bed->s->arch_size == 32)
9203 {
9204 r_type_mask = 0xff;
9205 r_sym_shift = 8;
9206 }
9207 else
9208 {
9209 r_type_mask = 0xffffffff;
9210 r_sym_shift = 32;
9211 }
9212
9213 erela = reldata->hdr->contents;
9214 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9215 {
9216 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9217 unsigned int j;
9218
9219 if (*rel_hash == NULL)
9220 continue;
9221
9222 if ((*rel_hash)->indx == -2
9223 && info->gc_sections
9224 && ! info->gc_keep_exported)
9225 {
9226 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9227 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9228 abfd, sec,
9229 (*rel_hash)->root.root.string);
9230 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9231 abfd, sec);
9232 bfd_set_error (bfd_error_invalid_operation);
9233 return FALSE;
9234 }
9235 BFD_ASSERT ((*rel_hash)->indx >= 0);
9236
9237 (*swap_in) (abfd, erela, irela);
9238 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9239 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9240 | (irela[j].r_info & r_type_mask));
9241 (*swap_out) (abfd, irela, erela);
9242 }
9243
9244 if (bed->elf_backend_update_relocs)
9245 (*bed->elf_backend_update_relocs) (sec, reldata);
9246
9247 if (sort && count != 0)
9248 {
9249 bfd_vma (*ext_r_off) (const void *);
9250 bfd_vma r_off;
9251 size_t elt_size;
9252 bfd_byte *base, *end, *p, *loc;
9253 bfd_byte *buf = NULL;
9254
9255 if (bed->s->arch_size == 32)
9256 {
9257 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9258 ext_r_off = ext32l_r_offset;
9259 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9260 ext_r_off = ext32b_r_offset;
9261 else
9262 abort ();
9263 }
9264 else
9265 {
9266 #ifdef BFD_HOST_64_BIT
9267 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9268 ext_r_off = ext64l_r_offset;
9269 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9270 ext_r_off = ext64b_r_offset;
9271 else
9272 #endif
9273 abort ();
9274 }
9275
9276 /* Must use a stable sort here. A modified insertion sort,
9277 since the relocs are mostly sorted already. */
9278 elt_size = reldata->hdr->sh_entsize;
9279 base = reldata->hdr->contents;
9280 end = base + count * elt_size;
9281 if (elt_size > sizeof (Elf64_External_Rela))
9282 abort ();
9283
9284 /* Ensure the first element is lowest. This acts as a sentinel,
9285 speeding the main loop below. */
9286 r_off = (*ext_r_off) (base);
9287 for (p = loc = base; (p += elt_size) < end; )
9288 {
9289 bfd_vma r_off2 = (*ext_r_off) (p);
9290 if (r_off > r_off2)
9291 {
9292 r_off = r_off2;
9293 loc = p;
9294 }
9295 }
9296 if (loc != base)
9297 {
9298 /* Don't just swap *base and *loc as that changes the order
9299 of the original base[0] and base[1] if they happen to
9300 have the same r_offset. */
9301 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9302 memcpy (onebuf, loc, elt_size);
9303 memmove (base + elt_size, base, loc - base);
9304 memcpy (base, onebuf, elt_size);
9305 }
9306
9307 for (p = base + elt_size; (p += elt_size) < end; )
9308 {
9309 /* base to p is sorted, *p is next to insert. */
9310 r_off = (*ext_r_off) (p);
9311 /* Search the sorted region for location to insert. */
9312 loc = p - elt_size;
9313 while (r_off < (*ext_r_off) (loc))
9314 loc -= elt_size;
9315 loc += elt_size;
9316 if (loc != p)
9317 {
9318 /* Chances are there is a run of relocs to insert here,
9319 from one of more input files. Files are not always
9320 linked in order due to the way elf_link_input_bfd is
9321 called. See pr17666. */
9322 size_t sortlen = p - loc;
9323 bfd_vma r_off2 = (*ext_r_off) (loc);
9324 size_t runlen = elt_size;
9325 size_t buf_size = 96 * 1024;
9326 while (p + runlen < end
9327 && (sortlen <= buf_size
9328 || runlen + elt_size <= buf_size)
9329 && r_off2 > (*ext_r_off) (p + runlen))
9330 runlen += elt_size;
9331 if (buf == NULL)
9332 {
9333 buf = bfd_malloc (buf_size);
9334 if (buf == NULL)
9335 return FALSE;
9336 }
9337 if (runlen < sortlen)
9338 {
9339 memcpy (buf, p, runlen);
9340 memmove (loc + runlen, loc, sortlen);
9341 memcpy (loc, buf, runlen);
9342 }
9343 else
9344 {
9345 memcpy (buf, loc, sortlen);
9346 memmove (loc, p, runlen);
9347 memcpy (loc + runlen, buf, sortlen);
9348 }
9349 p += runlen - elt_size;
9350 }
9351 }
9352 /* Hashes are no longer valid. */
9353 free (reldata->hashes);
9354 reldata->hashes = NULL;
9355 free (buf);
9356 }
9357 return TRUE;
9358 }
9359
9360 struct elf_link_sort_rela
9361 {
9362 union {
9363 bfd_vma offset;
9364 bfd_vma sym_mask;
9365 } u;
9366 enum elf_reloc_type_class type;
9367 /* We use this as an array of size int_rels_per_ext_rel. */
9368 Elf_Internal_Rela rela[1];
9369 };
9370
9371 /* qsort stability here and for cmp2 is only an issue if multiple
9372 dynamic relocations are emitted at the same address. But targets
9373 that apply a series of dynamic relocations each operating on the
9374 result of the prior relocation can't use -z combreloc as
9375 implemented anyway. Such schemes tend to be broken by sorting on
9376 symbol index. That leaves dynamic NONE relocs as the only other
9377 case where ld might emit multiple relocs at the same address, and
9378 those are only emitted due to target bugs. */
9379
9380 static int
9381 elf_link_sort_cmp1 (const void *A, const void *B)
9382 {
9383 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9384 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9385 int relativea, relativeb;
9386
9387 relativea = a->type == reloc_class_relative;
9388 relativeb = b->type == reloc_class_relative;
9389
9390 if (relativea < relativeb)
9391 return 1;
9392 if (relativea > relativeb)
9393 return -1;
9394 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9395 return -1;
9396 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9397 return 1;
9398 if (a->rela->r_offset < b->rela->r_offset)
9399 return -1;
9400 if (a->rela->r_offset > b->rela->r_offset)
9401 return 1;
9402 return 0;
9403 }
9404
9405 static int
9406 elf_link_sort_cmp2 (const void *A, const void *B)
9407 {
9408 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9409 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9410
9411 if (a->type < b->type)
9412 return -1;
9413 if (a->type > b->type)
9414 return 1;
9415 if (a->u.offset < b->u.offset)
9416 return -1;
9417 if (a->u.offset > b->u.offset)
9418 return 1;
9419 if (a->rela->r_offset < b->rela->r_offset)
9420 return -1;
9421 if (a->rela->r_offset > b->rela->r_offset)
9422 return 1;
9423 return 0;
9424 }
9425
9426 static size_t
9427 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9428 {
9429 asection *dynamic_relocs;
9430 asection *rela_dyn;
9431 asection *rel_dyn;
9432 bfd_size_type count, size;
9433 size_t i, ret, sort_elt, ext_size;
9434 bfd_byte *sort, *s_non_relative, *p;
9435 struct elf_link_sort_rela *sq;
9436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9437 int i2e = bed->s->int_rels_per_ext_rel;
9438 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9439 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9440 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9441 struct bfd_link_order *lo;
9442 bfd_vma r_sym_mask;
9443 bfd_boolean use_rela;
9444
9445 /* Find a dynamic reloc section. */
9446 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9447 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9448 if (rela_dyn != NULL && rela_dyn->size > 0
9449 && rel_dyn != NULL && rel_dyn->size > 0)
9450 {
9451 bfd_boolean use_rela_initialised = FALSE;
9452
9453 /* This is just here to stop gcc from complaining.
9454 Its initialization checking code is not perfect. */
9455 use_rela = TRUE;
9456
9457 /* Both sections are present. Examine the sizes
9458 of the indirect sections to help us choose. */
9459 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9460 if (lo->type == bfd_indirect_link_order)
9461 {
9462 asection *o = lo->u.indirect.section;
9463
9464 if ((o->size % bed->s->sizeof_rela) == 0)
9465 {
9466 if ((o->size % bed->s->sizeof_rel) == 0)
9467 /* Section size is divisible by both rel and rela sizes.
9468 It is of no help to us. */
9469 ;
9470 else
9471 {
9472 /* Section size is only divisible by rela. */
9473 if (use_rela_initialised && !use_rela)
9474 {
9475 _bfd_error_handler (_("%pB: unable to sort relocs - "
9476 "they are in more than one size"),
9477 abfd);
9478 bfd_set_error (bfd_error_invalid_operation);
9479 return 0;
9480 }
9481 else
9482 {
9483 use_rela = TRUE;
9484 use_rela_initialised = TRUE;
9485 }
9486 }
9487 }
9488 else if ((o->size % bed->s->sizeof_rel) == 0)
9489 {
9490 /* Section size is only divisible by rel. */
9491 if (use_rela_initialised && use_rela)
9492 {
9493 _bfd_error_handler (_("%pB: unable to sort relocs - "
9494 "they are in more than one size"),
9495 abfd);
9496 bfd_set_error (bfd_error_invalid_operation);
9497 return 0;
9498 }
9499 else
9500 {
9501 use_rela = FALSE;
9502 use_rela_initialised = TRUE;
9503 }
9504 }
9505 else
9506 {
9507 /* The section size is not divisible by either -
9508 something is wrong. */
9509 _bfd_error_handler (_("%pB: unable to sort relocs - "
9510 "they are of an unknown size"), abfd);
9511 bfd_set_error (bfd_error_invalid_operation);
9512 return 0;
9513 }
9514 }
9515
9516 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9517 if (lo->type == bfd_indirect_link_order)
9518 {
9519 asection *o = lo->u.indirect.section;
9520
9521 if ((o->size % bed->s->sizeof_rela) == 0)
9522 {
9523 if ((o->size % bed->s->sizeof_rel) == 0)
9524 /* Section size is divisible by both rel and rela sizes.
9525 It is of no help to us. */
9526 ;
9527 else
9528 {
9529 /* Section size is only divisible by rela. */
9530 if (use_rela_initialised && !use_rela)
9531 {
9532 _bfd_error_handler (_("%pB: unable to sort relocs - "
9533 "they are in more than one size"),
9534 abfd);
9535 bfd_set_error (bfd_error_invalid_operation);
9536 return 0;
9537 }
9538 else
9539 {
9540 use_rela = TRUE;
9541 use_rela_initialised = TRUE;
9542 }
9543 }
9544 }
9545 else if ((o->size % bed->s->sizeof_rel) == 0)
9546 {
9547 /* Section size is only divisible by rel. */
9548 if (use_rela_initialised && use_rela)
9549 {
9550 _bfd_error_handler (_("%pB: unable to sort relocs - "
9551 "they are in more than one size"),
9552 abfd);
9553 bfd_set_error (bfd_error_invalid_operation);
9554 return 0;
9555 }
9556 else
9557 {
9558 use_rela = FALSE;
9559 use_rela_initialised = TRUE;
9560 }
9561 }
9562 else
9563 {
9564 /* The section size is not divisible by either -
9565 something is wrong. */
9566 _bfd_error_handler (_("%pB: unable to sort relocs - "
9567 "they are of an unknown size"), abfd);
9568 bfd_set_error (bfd_error_invalid_operation);
9569 return 0;
9570 }
9571 }
9572
9573 if (! use_rela_initialised)
9574 /* Make a guess. */
9575 use_rela = TRUE;
9576 }
9577 else if (rela_dyn != NULL && rela_dyn->size > 0)
9578 use_rela = TRUE;
9579 else if (rel_dyn != NULL && rel_dyn->size > 0)
9580 use_rela = FALSE;
9581 else
9582 return 0;
9583
9584 if (use_rela)
9585 {
9586 dynamic_relocs = rela_dyn;
9587 ext_size = bed->s->sizeof_rela;
9588 swap_in = bed->s->swap_reloca_in;
9589 swap_out = bed->s->swap_reloca_out;
9590 }
9591 else
9592 {
9593 dynamic_relocs = rel_dyn;
9594 ext_size = bed->s->sizeof_rel;
9595 swap_in = bed->s->swap_reloc_in;
9596 swap_out = bed->s->swap_reloc_out;
9597 }
9598
9599 size = 0;
9600 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9601 if (lo->type == bfd_indirect_link_order)
9602 size += lo->u.indirect.section->size;
9603
9604 if (size != dynamic_relocs->size)
9605 return 0;
9606
9607 sort_elt = (sizeof (struct elf_link_sort_rela)
9608 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9609
9610 count = dynamic_relocs->size / ext_size;
9611 if (count == 0)
9612 return 0;
9613 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9614
9615 if (sort == NULL)
9616 {
9617 (*info->callbacks->warning)
9618 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9619 return 0;
9620 }
9621
9622 if (bed->s->arch_size == 32)
9623 r_sym_mask = ~(bfd_vma) 0xff;
9624 else
9625 r_sym_mask = ~(bfd_vma) 0xffffffff;
9626
9627 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9628 if (lo->type == bfd_indirect_link_order)
9629 {
9630 bfd_byte *erel, *erelend;
9631 asection *o = lo->u.indirect.section;
9632
9633 if (o->contents == NULL && o->size != 0)
9634 {
9635 /* This is a reloc section that is being handled as a normal
9636 section. See bfd_section_from_shdr. We can't combine
9637 relocs in this case. */
9638 free (sort);
9639 return 0;
9640 }
9641 erel = o->contents;
9642 erelend = o->contents + o->size;
9643 p = sort + o->output_offset * opb / ext_size * sort_elt;
9644
9645 while (erel < erelend)
9646 {
9647 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9648
9649 (*swap_in) (abfd, erel, s->rela);
9650 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9651 s->u.sym_mask = r_sym_mask;
9652 p += sort_elt;
9653 erel += ext_size;
9654 }
9655 }
9656
9657 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9658
9659 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9660 {
9661 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9662 if (s->type != reloc_class_relative)
9663 break;
9664 }
9665 ret = i;
9666 s_non_relative = p;
9667
9668 sq = (struct elf_link_sort_rela *) s_non_relative;
9669 for (; i < count; i++, p += sort_elt)
9670 {
9671 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9672 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9673 sq = sp;
9674 sp->u.offset = sq->rela->r_offset;
9675 }
9676
9677 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9678
9679 struct elf_link_hash_table *htab = elf_hash_table (info);
9680 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9681 {
9682 /* We have plt relocs in .rela.dyn. */
9683 sq = (struct elf_link_sort_rela *) sort;
9684 for (i = 0; i < count; i++)
9685 if (sq[count - i - 1].type != reloc_class_plt)
9686 break;
9687 if (i != 0 && htab->srelplt->size == i * ext_size)
9688 {
9689 struct bfd_link_order **plo;
9690 /* Put srelplt link_order last. This is so the output_offset
9691 set in the next loop is correct for DT_JMPREL. */
9692 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9693 if ((*plo)->type == bfd_indirect_link_order
9694 && (*plo)->u.indirect.section == htab->srelplt)
9695 {
9696 lo = *plo;
9697 *plo = lo->next;
9698 }
9699 else
9700 plo = &(*plo)->next;
9701 *plo = lo;
9702 lo->next = NULL;
9703 dynamic_relocs->map_tail.link_order = lo;
9704 }
9705 }
9706
9707 p = sort;
9708 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9709 if (lo->type == bfd_indirect_link_order)
9710 {
9711 bfd_byte *erel, *erelend;
9712 asection *o = lo->u.indirect.section;
9713
9714 erel = o->contents;
9715 erelend = o->contents + o->size;
9716 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9717 while (erel < erelend)
9718 {
9719 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9720 (*swap_out) (abfd, s->rela, erel);
9721 p += sort_elt;
9722 erel += ext_size;
9723 }
9724 }
9725
9726 free (sort);
9727 *psec = dynamic_relocs;
9728 return ret;
9729 }
9730
9731 /* Add a symbol to the output symbol string table. */
9732
9733 static int
9734 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9735 const char *name,
9736 Elf_Internal_Sym *elfsym,
9737 asection *input_sec,
9738 struct elf_link_hash_entry *h)
9739 {
9740 int (*output_symbol_hook)
9741 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9742 struct elf_link_hash_entry *);
9743 struct elf_link_hash_table *hash_table;
9744 const struct elf_backend_data *bed;
9745 bfd_size_type strtabsize;
9746
9747 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9748
9749 bed = get_elf_backend_data (flinfo->output_bfd);
9750 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9751 if (output_symbol_hook != NULL)
9752 {
9753 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9754 if (ret != 1)
9755 return ret;
9756 }
9757
9758 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9759 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9760 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9761 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9762
9763 if (name == NULL
9764 || *name == '\0'
9765 || (input_sec->flags & SEC_EXCLUDE))
9766 elfsym->st_name = (unsigned long) -1;
9767 else
9768 {
9769 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9770 to get the final offset for st_name. */
9771 char *versioned_name = (char *) name;
9772 if (h != NULL)
9773 {
9774 if (h->versioned == versioned && h->def_dynamic)
9775 {
9776 /* Keep only one '@' for versioned symbols defined in
9777 shared objects. */
9778 char *version = strrchr (name, ELF_VER_CHR);
9779 char *base_end = strchr (name, ELF_VER_CHR);
9780 if (version != base_end)
9781 {
9782 size_t base_len;
9783 size_t len = strlen (name);
9784 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9785 if (versioned_name == NULL)
9786 return 0;
9787 base_len = base_end - name;
9788 memcpy (versioned_name, name, base_len);
9789 memcpy (versioned_name + base_len, version,
9790 len - base_len);
9791 }
9792 }
9793 }
9794 else if (flinfo->info->unique_symbol
9795 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9796 {
9797 struct local_hash_entry *lh;
9798 switch (ELF_ST_TYPE (elfsym->st_info))
9799 {
9800 case STT_FILE:
9801 case STT_SECTION:
9802 break;
9803 default:
9804 lh = (struct local_hash_entry *) bfd_hash_lookup
9805 (&flinfo->local_hash_table, name, TRUE, FALSE);
9806 if (lh == NULL)
9807 return 0;
9808 if (lh->count)
9809 {
9810 /* Append ".COUNT" to duplicated local symbols. */
9811 size_t count_len;
9812 size_t base_len = lh->size;
9813 char buf[30];
9814 sprintf (buf, "%lx", lh->count);
9815 if (!base_len)
9816 {
9817 base_len = strlen (name);
9818 lh->size = base_len;
9819 }
9820 count_len = strlen (buf);
9821 versioned_name = bfd_alloc (flinfo->output_bfd,
9822 base_len + count_len + 2);
9823 if (versioned_name == NULL)
9824 return 0;
9825 memcpy (versioned_name, name, base_len);
9826 versioned_name[base_len] = '.';
9827 memcpy (versioned_name + base_len + 1, buf,
9828 count_len + 1);
9829 }
9830 lh->count++;
9831 break;
9832 }
9833 }
9834 elfsym->st_name
9835 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9836 versioned_name, FALSE);
9837 if (elfsym->st_name == (unsigned long) -1)
9838 return 0;
9839 }
9840
9841 hash_table = elf_hash_table (flinfo->info);
9842 strtabsize = hash_table->strtabsize;
9843 if (strtabsize <= hash_table->strtabcount)
9844 {
9845 strtabsize += strtabsize;
9846 hash_table->strtabsize = strtabsize;
9847 strtabsize *= sizeof (*hash_table->strtab);
9848 hash_table->strtab
9849 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9850 strtabsize);
9851 if (hash_table->strtab == NULL)
9852 return 0;
9853 }
9854 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9855 hash_table->strtab[hash_table->strtabcount].dest_index
9856 = hash_table->strtabcount;
9857 hash_table->strtab[hash_table->strtabcount].destshndx_index
9858 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9859
9860 flinfo->output_bfd->symcount += 1;
9861 hash_table->strtabcount += 1;
9862
9863 return 1;
9864 }
9865
9866 /* Swap symbols out to the symbol table and flush the output symbols to
9867 the file. */
9868
9869 static bfd_boolean
9870 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9871 {
9872 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9873 size_t amt;
9874 size_t i;
9875 const struct elf_backend_data *bed;
9876 bfd_byte *symbuf;
9877 Elf_Internal_Shdr *hdr;
9878 file_ptr pos;
9879 bfd_boolean ret;
9880
9881 if (!hash_table->strtabcount)
9882 return TRUE;
9883
9884 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9885
9886 bed = get_elf_backend_data (flinfo->output_bfd);
9887
9888 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9889 symbuf = (bfd_byte *) bfd_malloc (amt);
9890 if (symbuf == NULL)
9891 return FALSE;
9892
9893 if (flinfo->symshndxbuf)
9894 {
9895 amt = sizeof (Elf_External_Sym_Shndx);
9896 amt *= bfd_get_symcount (flinfo->output_bfd);
9897 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9898 if (flinfo->symshndxbuf == NULL)
9899 {
9900 free (symbuf);
9901 return FALSE;
9902 }
9903 }
9904
9905 /* Now swap out the symbols. */
9906 for (i = 0; i < hash_table->strtabcount; i++)
9907 {
9908 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9909 if (elfsym->sym.st_name == (unsigned long) -1)
9910 elfsym->sym.st_name = 0;
9911 else
9912 elfsym->sym.st_name
9913 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9914 elfsym->sym.st_name);
9915
9916 /* Inform the linker of the addition of this symbol. */
9917
9918 if (flinfo->info->callbacks->ctf_new_symbol)
9919 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9920 &elfsym->sym);
9921
9922 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9923 ((bfd_byte *) symbuf
9924 + (elfsym->dest_index
9925 * bed->s->sizeof_sym)),
9926 (flinfo->symshndxbuf
9927 + elfsym->destshndx_index));
9928 }
9929
9930 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9931 pos = hdr->sh_offset + hdr->sh_size;
9932 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9933 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9934 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9935 {
9936 hdr->sh_size += amt;
9937 ret = TRUE;
9938 }
9939 else
9940 ret = FALSE;
9941
9942 free (symbuf);
9943
9944 free (hash_table->strtab);
9945 hash_table->strtab = NULL;
9946
9947 return ret;
9948 }
9949
9950 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9951
9952 static bfd_boolean
9953 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9954 {
9955 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9956 && sym->st_shndx < SHN_LORESERVE)
9957 {
9958 /* The gABI doesn't support dynamic symbols in output sections
9959 beyond 64k. */
9960 _bfd_error_handler
9961 /* xgettext:c-format */
9962 (_("%pB: too many sections: %d (>= %d)"),
9963 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9964 bfd_set_error (bfd_error_nonrepresentable_section);
9965 return FALSE;
9966 }
9967 return TRUE;
9968 }
9969
9970 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9971 allowing an unsatisfied unversioned symbol in the DSO to match a
9972 versioned symbol that would normally require an explicit version.
9973 We also handle the case that a DSO references a hidden symbol
9974 which may be satisfied by a versioned symbol in another DSO. */
9975
9976 static bfd_boolean
9977 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9978 const struct elf_backend_data *bed,
9979 struct elf_link_hash_entry *h)
9980 {
9981 bfd *abfd;
9982 struct elf_link_loaded_list *loaded;
9983
9984 if (!is_elf_hash_table (info->hash))
9985 return FALSE;
9986
9987 /* Check indirect symbol. */
9988 while (h->root.type == bfd_link_hash_indirect)
9989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9990
9991 switch (h->root.type)
9992 {
9993 default:
9994 abfd = NULL;
9995 break;
9996
9997 case bfd_link_hash_undefined:
9998 case bfd_link_hash_undefweak:
9999 abfd = h->root.u.undef.abfd;
10000 if (abfd == NULL
10001 || (abfd->flags & DYNAMIC) == 0
10002 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10003 return FALSE;
10004 break;
10005
10006 case bfd_link_hash_defined:
10007 case bfd_link_hash_defweak:
10008 abfd = h->root.u.def.section->owner;
10009 break;
10010
10011 case bfd_link_hash_common:
10012 abfd = h->root.u.c.p->section->owner;
10013 break;
10014 }
10015 BFD_ASSERT (abfd != NULL);
10016
10017 for (loaded = elf_hash_table (info)->dyn_loaded;
10018 loaded != NULL;
10019 loaded = loaded->next)
10020 {
10021 bfd *input;
10022 Elf_Internal_Shdr *hdr;
10023 size_t symcount;
10024 size_t extsymcount;
10025 size_t extsymoff;
10026 Elf_Internal_Shdr *versymhdr;
10027 Elf_Internal_Sym *isym;
10028 Elf_Internal_Sym *isymend;
10029 Elf_Internal_Sym *isymbuf;
10030 Elf_External_Versym *ever;
10031 Elf_External_Versym *extversym;
10032
10033 input = loaded->abfd;
10034
10035 /* We check each DSO for a possible hidden versioned definition. */
10036 if (input == abfd
10037 || elf_dynversym (input) == 0)
10038 continue;
10039
10040 hdr = &elf_tdata (input)->dynsymtab_hdr;
10041
10042 symcount = hdr->sh_size / bed->s->sizeof_sym;
10043 if (elf_bad_symtab (input))
10044 {
10045 extsymcount = symcount;
10046 extsymoff = 0;
10047 }
10048 else
10049 {
10050 extsymcount = symcount - hdr->sh_info;
10051 extsymoff = hdr->sh_info;
10052 }
10053
10054 if (extsymcount == 0)
10055 continue;
10056
10057 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10058 NULL, NULL, NULL);
10059 if (isymbuf == NULL)
10060 return FALSE;
10061
10062 /* Read in any version definitions. */
10063 versymhdr = &elf_tdata (input)->dynversym_hdr;
10064 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10065 || (extversym = (Elf_External_Versym *)
10066 _bfd_malloc_and_read (input, versymhdr->sh_size,
10067 versymhdr->sh_size)) == NULL)
10068 {
10069 free (isymbuf);
10070 return FALSE;
10071 }
10072
10073 ever = extversym + extsymoff;
10074 isymend = isymbuf + extsymcount;
10075 for (isym = isymbuf; isym < isymend; isym++, ever++)
10076 {
10077 const char *name;
10078 Elf_Internal_Versym iver;
10079 unsigned short version_index;
10080
10081 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10082 || isym->st_shndx == SHN_UNDEF)
10083 continue;
10084
10085 name = bfd_elf_string_from_elf_section (input,
10086 hdr->sh_link,
10087 isym->st_name);
10088 if (strcmp (name, h->root.root.string) != 0)
10089 continue;
10090
10091 _bfd_elf_swap_versym_in (input, ever, &iver);
10092
10093 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10094 && !(h->def_regular
10095 && h->forced_local))
10096 {
10097 /* If we have a non-hidden versioned sym, then it should
10098 have provided a definition for the undefined sym unless
10099 it is defined in a non-shared object and forced local.
10100 */
10101 abort ();
10102 }
10103
10104 version_index = iver.vs_vers & VERSYM_VERSION;
10105 if (version_index == 1 || version_index == 2)
10106 {
10107 /* This is the base or first version. We can use it. */
10108 free (extversym);
10109 free (isymbuf);
10110 return TRUE;
10111 }
10112 }
10113
10114 free (extversym);
10115 free (isymbuf);
10116 }
10117
10118 return FALSE;
10119 }
10120
10121 /* Convert ELF common symbol TYPE. */
10122
10123 static int
10124 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10125 {
10126 /* Commom symbol can only appear in relocatable link. */
10127 if (!bfd_link_relocatable (info))
10128 abort ();
10129 switch (info->elf_stt_common)
10130 {
10131 case unchanged:
10132 break;
10133 case elf_stt_common:
10134 type = STT_COMMON;
10135 break;
10136 case no_elf_stt_common:
10137 type = STT_OBJECT;
10138 break;
10139 }
10140 return type;
10141 }
10142
10143 /* Add an external symbol to the symbol table. This is called from
10144 the hash table traversal routine. When generating a shared object,
10145 we go through the symbol table twice. The first time we output
10146 anything that might have been forced to local scope in a version
10147 script. The second time we output the symbols that are still
10148 global symbols. */
10149
10150 static bfd_boolean
10151 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10152 {
10153 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10154 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10155 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10156 bfd_boolean strip;
10157 Elf_Internal_Sym sym;
10158 asection *input_sec;
10159 const struct elf_backend_data *bed;
10160 long indx;
10161 int ret;
10162 unsigned int type;
10163
10164 if (h->root.type == bfd_link_hash_warning)
10165 {
10166 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10167 if (h->root.type == bfd_link_hash_new)
10168 return TRUE;
10169 }
10170
10171 /* Decide whether to output this symbol in this pass. */
10172 if (eoinfo->localsyms)
10173 {
10174 if (!h->forced_local)
10175 return TRUE;
10176 }
10177 else
10178 {
10179 if (h->forced_local)
10180 return TRUE;
10181 }
10182
10183 bed = get_elf_backend_data (flinfo->output_bfd);
10184
10185 if (h->root.type == bfd_link_hash_undefined)
10186 {
10187 /* If we have an undefined symbol reference here then it must have
10188 come from a shared library that is being linked in. (Undefined
10189 references in regular files have already been handled unless
10190 they are in unreferenced sections which are removed by garbage
10191 collection). */
10192 bfd_boolean ignore_undef = FALSE;
10193
10194 /* Some symbols may be special in that the fact that they're
10195 undefined can be safely ignored - let backend determine that. */
10196 if (bed->elf_backend_ignore_undef_symbol)
10197 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10198
10199 /* If we are reporting errors for this situation then do so now. */
10200 if (!ignore_undef
10201 && h->ref_dynamic_nonweak
10202 && (!h->ref_regular || flinfo->info->gc_sections)
10203 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10204 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10205 {
10206 flinfo->info->callbacks->undefined_symbol
10207 (flinfo->info, h->root.root.string,
10208 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10209 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10210 && !flinfo->info->warn_unresolved_syms);
10211 }
10212
10213 /* Strip a global symbol defined in a discarded section. */
10214 if (h->indx == -3)
10215 return TRUE;
10216 }
10217
10218 /* We should also warn if a forced local symbol is referenced from
10219 shared libraries. */
10220 if (bfd_link_executable (flinfo->info)
10221 && h->forced_local
10222 && h->ref_dynamic
10223 && h->def_regular
10224 && !h->dynamic_def
10225 && h->ref_dynamic_nonweak
10226 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10227 {
10228 bfd *def_bfd;
10229 const char *msg;
10230 struct elf_link_hash_entry *hi = h;
10231
10232 /* Check indirect symbol. */
10233 while (hi->root.type == bfd_link_hash_indirect)
10234 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10235
10236 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10237 /* xgettext:c-format */
10238 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10239 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10240 /* xgettext:c-format */
10241 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10242 else
10243 /* xgettext:c-format */
10244 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10245 def_bfd = flinfo->output_bfd;
10246 if (hi->root.u.def.section != bfd_abs_section_ptr)
10247 def_bfd = hi->root.u.def.section->owner;
10248 _bfd_error_handler (msg, flinfo->output_bfd,
10249 h->root.root.string, def_bfd);
10250 bfd_set_error (bfd_error_bad_value);
10251 eoinfo->failed = TRUE;
10252 return FALSE;
10253 }
10254
10255 /* We don't want to output symbols that have never been mentioned by
10256 a regular file, or that we have been told to strip. However, if
10257 h->indx is set to -2, the symbol is used by a reloc and we must
10258 output it. */
10259 strip = FALSE;
10260 if (h->indx == -2)
10261 ;
10262 else if ((h->def_dynamic
10263 || h->ref_dynamic
10264 || h->root.type == bfd_link_hash_new)
10265 && !h->def_regular
10266 && !h->ref_regular)
10267 strip = TRUE;
10268 else if (flinfo->info->strip == strip_all)
10269 strip = TRUE;
10270 else if (flinfo->info->strip == strip_some
10271 && bfd_hash_lookup (flinfo->info->keep_hash,
10272 h->root.root.string, FALSE, FALSE) == NULL)
10273 strip = TRUE;
10274 else if ((h->root.type == bfd_link_hash_defined
10275 || h->root.type == bfd_link_hash_defweak)
10276 && ((flinfo->info->strip_discarded
10277 && discarded_section (h->root.u.def.section))
10278 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10279 && h->root.u.def.section->owner != NULL
10280 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10281 strip = TRUE;
10282 else if ((h->root.type == bfd_link_hash_undefined
10283 || h->root.type == bfd_link_hash_undefweak)
10284 && h->root.u.undef.abfd != NULL
10285 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10286 strip = TRUE;
10287
10288 type = h->type;
10289
10290 /* If we're stripping it, and it's not a dynamic symbol, there's
10291 nothing else to do. However, if it is a forced local symbol or
10292 an ifunc symbol we need to give the backend finish_dynamic_symbol
10293 function a chance to make it dynamic. */
10294 if (strip
10295 && h->dynindx == -1
10296 && type != STT_GNU_IFUNC
10297 && !h->forced_local)
10298 return TRUE;
10299
10300 sym.st_value = 0;
10301 sym.st_size = h->size;
10302 sym.st_other = h->other;
10303 switch (h->root.type)
10304 {
10305 default:
10306 case bfd_link_hash_new:
10307 case bfd_link_hash_warning:
10308 abort ();
10309 return FALSE;
10310
10311 case bfd_link_hash_undefined:
10312 case bfd_link_hash_undefweak:
10313 input_sec = bfd_und_section_ptr;
10314 sym.st_shndx = SHN_UNDEF;
10315 break;
10316
10317 case bfd_link_hash_defined:
10318 case bfd_link_hash_defweak:
10319 {
10320 input_sec = h->root.u.def.section;
10321 if (input_sec->output_section != NULL)
10322 {
10323 sym.st_shndx =
10324 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10325 input_sec->output_section);
10326 if (sym.st_shndx == SHN_BAD)
10327 {
10328 _bfd_error_handler
10329 /* xgettext:c-format */
10330 (_("%pB: could not find output section %pA for input section %pA"),
10331 flinfo->output_bfd, input_sec->output_section, input_sec);
10332 bfd_set_error (bfd_error_nonrepresentable_section);
10333 eoinfo->failed = TRUE;
10334 return FALSE;
10335 }
10336
10337 /* ELF symbols in relocatable files are section relative,
10338 but in nonrelocatable files they are virtual
10339 addresses. */
10340 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10341 if (!bfd_link_relocatable (flinfo->info))
10342 {
10343 sym.st_value += input_sec->output_section->vma;
10344 if (h->type == STT_TLS)
10345 {
10346 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10347 if (tls_sec != NULL)
10348 sym.st_value -= tls_sec->vma;
10349 }
10350 }
10351 }
10352 else
10353 {
10354 BFD_ASSERT (input_sec->owner == NULL
10355 || (input_sec->owner->flags & DYNAMIC) != 0);
10356 sym.st_shndx = SHN_UNDEF;
10357 input_sec = bfd_und_section_ptr;
10358 }
10359 }
10360 break;
10361
10362 case bfd_link_hash_common:
10363 input_sec = h->root.u.c.p->section;
10364 sym.st_shndx = bed->common_section_index (input_sec);
10365 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10366 break;
10367
10368 case bfd_link_hash_indirect:
10369 /* These symbols are created by symbol versioning. They point
10370 to the decorated version of the name. For example, if the
10371 symbol foo@@GNU_1.2 is the default, which should be used when
10372 foo is used with no version, then we add an indirect symbol
10373 foo which points to foo@@GNU_1.2. We ignore these symbols,
10374 since the indirected symbol is already in the hash table. */
10375 return TRUE;
10376 }
10377
10378 if (type == STT_COMMON || type == STT_OBJECT)
10379 switch (h->root.type)
10380 {
10381 case bfd_link_hash_common:
10382 type = elf_link_convert_common_type (flinfo->info, type);
10383 break;
10384 case bfd_link_hash_defined:
10385 case bfd_link_hash_defweak:
10386 if (bed->common_definition (&sym))
10387 type = elf_link_convert_common_type (flinfo->info, type);
10388 else
10389 type = STT_OBJECT;
10390 break;
10391 case bfd_link_hash_undefined:
10392 case bfd_link_hash_undefweak:
10393 break;
10394 default:
10395 abort ();
10396 }
10397
10398 if (h->forced_local)
10399 {
10400 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10401 /* Turn off visibility on local symbol. */
10402 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10403 }
10404 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10405 else if (h->unique_global && h->def_regular)
10406 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10407 else if (h->root.type == bfd_link_hash_undefweak
10408 || h->root.type == bfd_link_hash_defweak)
10409 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10410 else
10411 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10412 sym.st_target_internal = h->target_internal;
10413
10414 /* Give the processor backend a chance to tweak the symbol value,
10415 and also to finish up anything that needs to be done for this
10416 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10417 forced local syms when non-shared is due to a historical quirk.
10418 STT_GNU_IFUNC symbol must go through PLT. */
10419 if ((h->type == STT_GNU_IFUNC
10420 && h->def_regular
10421 && !bfd_link_relocatable (flinfo->info))
10422 || ((h->dynindx != -1
10423 || h->forced_local)
10424 && ((bfd_link_pic (flinfo->info)
10425 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10426 || h->root.type != bfd_link_hash_undefweak))
10427 || !h->forced_local)
10428 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10429 {
10430 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10431 (flinfo->output_bfd, flinfo->info, h, &sym)))
10432 {
10433 eoinfo->failed = TRUE;
10434 return FALSE;
10435 }
10436 }
10437
10438 /* If we are marking the symbol as undefined, and there are no
10439 non-weak references to this symbol from a regular object, then
10440 mark the symbol as weak undefined; if there are non-weak
10441 references, mark the symbol as strong. We can't do this earlier,
10442 because it might not be marked as undefined until the
10443 finish_dynamic_symbol routine gets through with it. */
10444 if (sym.st_shndx == SHN_UNDEF
10445 && h->ref_regular
10446 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10447 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10448 {
10449 int bindtype;
10450 type = ELF_ST_TYPE (sym.st_info);
10451
10452 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10453 if (type == STT_GNU_IFUNC)
10454 type = STT_FUNC;
10455
10456 if (h->ref_regular_nonweak)
10457 bindtype = STB_GLOBAL;
10458 else
10459 bindtype = STB_WEAK;
10460 sym.st_info = ELF_ST_INFO (bindtype, type);
10461 }
10462
10463 /* If this is a symbol defined in a dynamic library, don't use the
10464 symbol size from the dynamic library. Relinking an executable
10465 against a new library may introduce gratuitous changes in the
10466 executable's symbols if we keep the size. */
10467 if (sym.st_shndx == SHN_UNDEF
10468 && !h->def_regular
10469 && h->def_dynamic)
10470 sym.st_size = 0;
10471
10472 /* If a non-weak symbol with non-default visibility is not defined
10473 locally, it is a fatal error. */
10474 if (!bfd_link_relocatable (flinfo->info)
10475 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10476 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10477 && h->root.type == bfd_link_hash_undefined
10478 && !h->def_regular)
10479 {
10480 const char *msg;
10481
10482 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10483 /* xgettext:c-format */
10484 msg = _("%pB: protected symbol `%s' isn't defined");
10485 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10486 /* xgettext:c-format */
10487 msg = _("%pB: internal symbol `%s' isn't defined");
10488 else
10489 /* xgettext:c-format */
10490 msg = _("%pB: hidden symbol `%s' isn't defined");
10491 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10492 bfd_set_error (bfd_error_bad_value);
10493 eoinfo->failed = TRUE;
10494 return FALSE;
10495 }
10496
10497 /* If this symbol should be put in the .dynsym section, then put it
10498 there now. We already know the symbol index. We also fill in
10499 the entry in the .hash section. */
10500 if (h->dynindx != -1
10501 && elf_hash_table (flinfo->info)->dynamic_sections_created
10502 && elf_hash_table (flinfo->info)->dynsym != NULL
10503 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10504 {
10505 bfd_byte *esym;
10506
10507 /* Since there is no version information in the dynamic string,
10508 if there is no version info in symbol version section, we will
10509 have a run-time problem if not linking executable, referenced
10510 by shared library, or not bound locally. */
10511 if (h->verinfo.verdef == NULL
10512 && (!bfd_link_executable (flinfo->info)
10513 || h->ref_dynamic
10514 || !h->def_regular))
10515 {
10516 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10517
10518 if (p && p [1] != '\0')
10519 {
10520 _bfd_error_handler
10521 /* xgettext:c-format */
10522 (_("%pB: no symbol version section for versioned symbol `%s'"),
10523 flinfo->output_bfd, h->root.root.string);
10524 eoinfo->failed = TRUE;
10525 return FALSE;
10526 }
10527 }
10528
10529 sym.st_name = h->dynstr_index;
10530 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10531 + h->dynindx * bed->s->sizeof_sym);
10532 if (!check_dynsym (flinfo->output_bfd, &sym))
10533 {
10534 eoinfo->failed = TRUE;
10535 return FALSE;
10536 }
10537
10538 /* Inform the linker of the addition of this symbol. */
10539
10540 if (flinfo->info->callbacks->ctf_new_dynsym)
10541 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10542
10543 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10544
10545 if (flinfo->hash_sec != NULL)
10546 {
10547 size_t hash_entry_size;
10548 bfd_byte *bucketpos;
10549 bfd_vma chain;
10550 size_t bucketcount;
10551 size_t bucket;
10552
10553 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10554 bucket = h->u.elf_hash_value % bucketcount;
10555
10556 hash_entry_size
10557 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10558 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10559 + (bucket + 2) * hash_entry_size);
10560 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10561 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10562 bucketpos);
10563 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10564 ((bfd_byte *) flinfo->hash_sec->contents
10565 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10566 }
10567
10568 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10569 {
10570 Elf_Internal_Versym iversym;
10571 Elf_External_Versym *eversym;
10572
10573 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10574 {
10575 if (h->verinfo.verdef == NULL
10576 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10577 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10578 iversym.vs_vers = 1;
10579 else
10580 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10581 }
10582 else
10583 {
10584 if (h->verinfo.vertree == NULL)
10585 iversym.vs_vers = 1;
10586 else
10587 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10588 if (flinfo->info->create_default_symver)
10589 iversym.vs_vers++;
10590 }
10591
10592 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10593 defined locally. */
10594 if (h->versioned == versioned_hidden && h->def_regular)
10595 iversym.vs_vers |= VERSYM_HIDDEN;
10596
10597 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10598 eversym += h->dynindx;
10599 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10600 }
10601 }
10602
10603 /* If the symbol is undefined, and we didn't output it to .dynsym,
10604 strip it from .symtab too. Obviously we can't do this for
10605 relocatable output or when needed for --emit-relocs. */
10606 else if (input_sec == bfd_und_section_ptr
10607 && h->indx != -2
10608 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10609 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10610 && !bfd_link_relocatable (flinfo->info))
10611 return TRUE;
10612
10613 /* Also strip others that we couldn't earlier due to dynamic symbol
10614 processing. */
10615 if (strip)
10616 return TRUE;
10617 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10618 return TRUE;
10619
10620 /* Output a FILE symbol so that following locals are not associated
10621 with the wrong input file. We need one for forced local symbols
10622 if we've seen more than one FILE symbol or when we have exactly
10623 one FILE symbol but global symbols are present in a file other
10624 than the one with the FILE symbol. We also need one if linker
10625 defined symbols are present. In practice these conditions are
10626 always met, so just emit the FILE symbol unconditionally. */
10627 if (eoinfo->localsyms
10628 && !eoinfo->file_sym_done
10629 && eoinfo->flinfo->filesym_count != 0)
10630 {
10631 Elf_Internal_Sym fsym;
10632
10633 memset (&fsym, 0, sizeof (fsym));
10634 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10635 fsym.st_shndx = SHN_ABS;
10636 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10637 bfd_und_section_ptr, NULL))
10638 return FALSE;
10639
10640 eoinfo->file_sym_done = TRUE;
10641 }
10642
10643 indx = bfd_get_symcount (flinfo->output_bfd);
10644 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10645 input_sec, h);
10646 if (ret == 0)
10647 {
10648 eoinfo->failed = TRUE;
10649 return FALSE;
10650 }
10651 else if (ret == 1)
10652 h->indx = indx;
10653 else if (h->indx == -2)
10654 abort();
10655
10656 return TRUE;
10657 }
10658
10659 /* Return TRUE if special handling is done for relocs in SEC against
10660 symbols defined in discarded sections. */
10661
10662 static bfd_boolean
10663 elf_section_ignore_discarded_relocs (asection *sec)
10664 {
10665 const struct elf_backend_data *bed;
10666
10667 switch (sec->sec_info_type)
10668 {
10669 case SEC_INFO_TYPE_STABS:
10670 case SEC_INFO_TYPE_EH_FRAME:
10671 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10672 return TRUE;
10673 default:
10674 break;
10675 }
10676
10677 bed = get_elf_backend_data (sec->owner);
10678 if (bed->elf_backend_ignore_discarded_relocs != NULL
10679 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10680 return TRUE;
10681
10682 return FALSE;
10683 }
10684
10685 /* Return a mask saying how ld should treat relocations in SEC against
10686 symbols defined in discarded sections. If this function returns
10687 COMPLAIN set, ld will issue a warning message. If this function
10688 returns PRETEND set, and the discarded section was link-once and the
10689 same size as the kept link-once section, ld will pretend that the
10690 symbol was actually defined in the kept section. Otherwise ld will
10691 zero the reloc (at least that is the intent, but some cooperation by
10692 the target dependent code is needed, particularly for REL targets). */
10693
10694 unsigned int
10695 _bfd_elf_default_action_discarded (asection *sec)
10696 {
10697 if (sec->flags & SEC_DEBUGGING)
10698 return PRETEND;
10699
10700 if (strcmp (".eh_frame", sec->name) == 0)
10701 return 0;
10702
10703 if (strcmp (".gcc_except_table", sec->name) == 0)
10704 return 0;
10705
10706 return COMPLAIN | PRETEND;
10707 }
10708
10709 /* Find a match between a section and a member of a section group. */
10710
10711 static asection *
10712 match_group_member (asection *sec, asection *group,
10713 struct bfd_link_info *info)
10714 {
10715 asection *first = elf_next_in_group (group);
10716 asection *s = first;
10717
10718 while (s != NULL)
10719 {
10720 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10721 return s;
10722
10723 s = elf_next_in_group (s);
10724 if (s == first)
10725 break;
10726 }
10727
10728 return NULL;
10729 }
10730
10731 /* Check if the kept section of a discarded section SEC can be used
10732 to replace it. Return the replacement if it is OK. Otherwise return
10733 NULL. */
10734
10735 asection *
10736 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10737 {
10738 asection *kept;
10739
10740 kept = sec->kept_section;
10741 if (kept != NULL)
10742 {
10743 if ((kept->flags & SEC_GROUP) != 0)
10744 kept = match_group_member (sec, kept, info);
10745 if (kept != NULL)
10746 {
10747 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10748 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10749 kept = NULL;
10750 else
10751 {
10752 /* Get the real kept section. */
10753 asection *next;
10754 for (next = kept->kept_section;
10755 next != NULL;
10756 next = next->kept_section)
10757 kept = next;
10758 }
10759 }
10760 sec->kept_section = kept;
10761 }
10762 return kept;
10763 }
10764
10765 /* Link an input file into the linker output file. This function
10766 handles all the sections and relocations of the input file at once.
10767 This is so that we only have to read the local symbols once, and
10768 don't have to keep them in memory. */
10769
10770 static bfd_boolean
10771 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10772 {
10773 int (*relocate_section)
10774 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10775 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10776 bfd *output_bfd;
10777 Elf_Internal_Shdr *symtab_hdr;
10778 size_t locsymcount;
10779 size_t extsymoff;
10780 Elf_Internal_Sym *isymbuf;
10781 Elf_Internal_Sym *isym;
10782 Elf_Internal_Sym *isymend;
10783 long *pindex;
10784 asection **ppsection;
10785 asection *o;
10786 const struct elf_backend_data *bed;
10787 struct elf_link_hash_entry **sym_hashes;
10788 bfd_size_type address_size;
10789 bfd_vma r_type_mask;
10790 int r_sym_shift;
10791 bfd_boolean have_file_sym = FALSE;
10792
10793 output_bfd = flinfo->output_bfd;
10794 bed = get_elf_backend_data (output_bfd);
10795 relocate_section = bed->elf_backend_relocate_section;
10796
10797 /* If this is a dynamic object, we don't want to do anything here:
10798 we don't want the local symbols, and we don't want the section
10799 contents. */
10800 if ((input_bfd->flags & DYNAMIC) != 0)
10801 return TRUE;
10802
10803 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10804 if (elf_bad_symtab (input_bfd))
10805 {
10806 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10807 extsymoff = 0;
10808 }
10809 else
10810 {
10811 locsymcount = symtab_hdr->sh_info;
10812 extsymoff = symtab_hdr->sh_info;
10813 }
10814
10815 /* Enable GNU OSABI features in the output BFD that are used in the input
10816 BFD. */
10817 if (bed->elf_osabi == ELFOSABI_NONE
10818 || bed->elf_osabi == ELFOSABI_GNU
10819 || bed->elf_osabi == ELFOSABI_FREEBSD)
10820 elf_tdata (output_bfd)->has_gnu_osabi
10821 |= (elf_tdata (input_bfd)->has_gnu_osabi
10822 & (bfd_link_relocatable (flinfo->info)
10823 ? -1 : ~elf_gnu_osabi_retain));
10824
10825 /* Read the local symbols. */
10826 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10827 if (isymbuf == NULL && locsymcount != 0)
10828 {
10829 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10830 flinfo->internal_syms,
10831 flinfo->external_syms,
10832 flinfo->locsym_shndx);
10833 if (isymbuf == NULL)
10834 return FALSE;
10835 }
10836
10837 /* Find local symbol sections and adjust values of symbols in
10838 SEC_MERGE sections. Write out those local symbols we know are
10839 going into the output file. */
10840 isymend = isymbuf + locsymcount;
10841 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10842 isym < isymend;
10843 isym++, pindex++, ppsection++)
10844 {
10845 asection *isec;
10846 const char *name;
10847 Elf_Internal_Sym osym;
10848 long indx;
10849 int ret;
10850
10851 *pindex = -1;
10852
10853 if (elf_bad_symtab (input_bfd))
10854 {
10855 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10856 {
10857 *ppsection = NULL;
10858 continue;
10859 }
10860 }
10861
10862 if (isym->st_shndx == SHN_UNDEF)
10863 isec = bfd_und_section_ptr;
10864 else if (isym->st_shndx == SHN_ABS)
10865 isec = bfd_abs_section_ptr;
10866 else if (isym->st_shndx == SHN_COMMON)
10867 isec = bfd_com_section_ptr;
10868 else
10869 {
10870 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10871 if (isec == NULL)
10872 {
10873 /* Don't attempt to output symbols with st_shnx in the
10874 reserved range other than SHN_ABS and SHN_COMMON. */
10875 isec = bfd_und_section_ptr;
10876 }
10877 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10878 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10879 isym->st_value =
10880 _bfd_merged_section_offset (output_bfd, &isec,
10881 elf_section_data (isec)->sec_info,
10882 isym->st_value);
10883 }
10884
10885 *ppsection = isec;
10886
10887 /* Don't output the first, undefined, symbol. In fact, don't
10888 output any undefined local symbol. */
10889 if (isec == bfd_und_section_ptr)
10890 continue;
10891
10892 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10893 {
10894 /* We never output section symbols. Instead, we use the
10895 section symbol of the corresponding section in the output
10896 file. */
10897 continue;
10898 }
10899
10900 /* If we are stripping all symbols, we don't want to output this
10901 one. */
10902 if (flinfo->info->strip == strip_all)
10903 continue;
10904
10905 /* If we are discarding all local symbols, we don't want to
10906 output this one. If we are generating a relocatable output
10907 file, then some of the local symbols may be required by
10908 relocs; we output them below as we discover that they are
10909 needed. */
10910 if (flinfo->info->discard == discard_all)
10911 continue;
10912
10913 /* If this symbol is defined in a section which we are
10914 discarding, we don't need to keep it. */
10915 if (isym->st_shndx != SHN_UNDEF
10916 && isym->st_shndx < SHN_LORESERVE
10917 && isec->output_section == NULL
10918 && flinfo->info->non_contiguous_regions
10919 && flinfo->info->non_contiguous_regions_warnings)
10920 {
10921 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10922 "discards section `%s' from '%s'\n"),
10923 isec->name, bfd_get_filename (isec->owner));
10924 continue;
10925 }
10926
10927 if (isym->st_shndx != SHN_UNDEF
10928 && isym->st_shndx < SHN_LORESERVE
10929 && bfd_section_removed_from_list (output_bfd,
10930 isec->output_section))
10931 continue;
10932
10933 /* Get the name of the symbol. */
10934 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10935 isym->st_name);
10936 if (name == NULL)
10937 return FALSE;
10938
10939 /* See if we are discarding symbols with this name. */
10940 if ((flinfo->info->strip == strip_some
10941 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10942 == NULL))
10943 || (((flinfo->info->discard == discard_sec_merge
10944 && (isec->flags & SEC_MERGE)
10945 && !bfd_link_relocatable (flinfo->info))
10946 || flinfo->info->discard == discard_l)
10947 && bfd_is_local_label_name (input_bfd, name)))
10948 continue;
10949
10950 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10951 {
10952 if (input_bfd->lto_output)
10953 /* -flto puts a temp file name here. This means builds
10954 are not reproducible. Discard the symbol. */
10955 continue;
10956 have_file_sym = TRUE;
10957 flinfo->filesym_count += 1;
10958 }
10959 if (!have_file_sym)
10960 {
10961 /* In the absence of debug info, bfd_find_nearest_line uses
10962 FILE symbols to determine the source file for local
10963 function symbols. Provide a FILE symbol here if input
10964 files lack such, so that their symbols won't be
10965 associated with a previous input file. It's not the
10966 source file, but the best we can do. */
10967 const char *filename;
10968 have_file_sym = TRUE;
10969 flinfo->filesym_count += 1;
10970 memset (&osym, 0, sizeof (osym));
10971 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10972 osym.st_shndx = SHN_ABS;
10973 if (input_bfd->lto_output)
10974 filename = NULL;
10975 else
10976 filename = lbasename (bfd_get_filename (input_bfd));
10977 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
10978 bfd_abs_section_ptr, NULL))
10979 return FALSE;
10980 }
10981
10982 osym = *isym;
10983
10984 /* Adjust the section index for the output file. */
10985 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10986 isec->output_section);
10987 if (osym.st_shndx == SHN_BAD)
10988 return FALSE;
10989
10990 /* ELF symbols in relocatable files are section relative, but
10991 in executable files they are virtual addresses. Note that
10992 this code assumes that all ELF sections have an associated
10993 BFD section with a reasonable value for output_offset; below
10994 we assume that they also have a reasonable value for
10995 output_section. Any special sections must be set up to meet
10996 these requirements. */
10997 osym.st_value += isec->output_offset;
10998 if (!bfd_link_relocatable (flinfo->info))
10999 {
11000 osym.st_value += isec->output_section->vma;
11001 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11002 {
11003 /* STT_TLS symbols are relative to PT_TLS segment base. */
11004 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11005 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11006 else
11007 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11008 STT_NOTYPE);
11009 }
11010 }
11011
11012 indx = bfd_get_symcount (output_bfd);
11013 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11014 if (ret == 0)
11015 return FALSE;
11016 else if (ret == 1)
11017 *pindex = indx;
11018 }
11019
11020 if (bed->s->arch_size == 32)
11021 {
11022 r_type_mask = 0xff;
11023 r_sym_shift = 8;
11024 address_size = 4;
11025 }
11026 else
11027 {
11028 r_type_mask = 0xffffffff;
11029 r_sym_shift = 32;
11030 address_size = 8;
11031 }
11032
11033 /* Relocate the contents of each section. */
11034 sym_hashes = elf_sym_hashes (input_bfd);
11035 for (o = input_bfd->sections; o != NULL; o = o->next)
11036 {
11037 bfd_byte *contents;
11038
11039 if (! o->linker_mark)
11040 {
11041 /* This section was omitted from the link. */
11042 continue;
11043 }
11044
11045 if (!flinfo->info->resolve_section_groups
11046 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11047 {
11048 /* Deal with the group signature symbol. */
11049 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11050 unsigned long symndx = sec_data->this_hdr.sh_info;
11051 asection *osec = o->output_section;
11052
11053 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11054 if (symndx >= locsymcount
11055 || (elf_bad_symtab (input_bfd)
11056 && flinfo->sections[symndx] == NULL))
11057 {
11058 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11059 while (h->root.type == bfd_link_hash_indirect
11060 || h->root.type == bfd_link_hash_warning)
11061 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11062 /* Arrange for symbol to be output. */
11063 h->indx = -2;
11064 elf_section_data (osec)->this_hdr.sh_info = -2;
11065 }
11066 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11067 {
11068 /* We'll use the output section target_index. */
11069 asection *sec = flinfo->sections[symndx]->output_section;
11070 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11071 }
11072 else
11073 {
11074 if (flinfo->indices[symndx] == -1)
11075 {
11076 /* Otherwise output the local symbol now. */
11077 Elf_Internal_Sym sym = isymbuf[symndx];
11078 asection *sec = flinfo->sections[symndx]->output_section;
11079 const char *name;
11080 long indx;
11081 int ret;
11082
11083 name = bfd_elf_string_from_elf_section (input_bfd,
11084 symtab_hdr->sh_link,
11085 sym.st_name);
11086 if (name == NULL)
11087 return FALSE;
11088
11089 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11090 sec);
11091 if (sym.st_shndx == SHN_BAD)
11092 return FALSE;
11093
11094 sym.st_value += o->output_offset;
11095
11096 indx = bfd_get_symcount (output_bfd);
11097 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11098 NULL);
11099 if (ret == 0)
11100 return FALSE;
11101 else if (ret == 1)
11102 flinfo->indices[symndx] = indx;
11103 else
11104 abort ();
11105 }
11106 elf_section_data (osec)->this_hdr.sh_info
11107 = flinfo->indices[symndx];
11108 }
11109 }
11110
11111 if ((o->flags & SEC_HAS_CONTENTS) == 0
11112 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11113 continue;
11114
11115 if ((o->flags & SEC_LINKER_CREATED) != 0)
11116 {
11117 /* Section was created by _bfd_elf_link_create_dynamic_sections
11118 or somesuch. */
11119 continue;
11120 }
11121
11122 /* Get the contents of the section. They have been cached by a
11123 relaxation routine. Note that o is a section in an input
11124 file, so the contents field will not have been set by any of
11125 the routines which work on output files. */
11126 if (elf_section_data (o)->this_hdr.contents != NULL)
11127 {
11128 contents = elf_section_data (o)->this_hdr.contents;
11129 if (bed->caches_rawsize
11130 && o->rawsize != 0
11131 && o->rawsize < o->size)
11132 {
11133 memcpy (flinfo->contents, contents, o->rawsize);
11134 contents = flinfo->contents;
11135 }
11136 }
11137 else
11138 {
11139 contents = flinfo->contents;
11140 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11141 return FALSE;
11142 }
11143
11144 if ((o->flags & SEC_RELOC) != 0)
11145 {
11146 Elf_Internal_Rela *internal_relocs;
11147 Elf_Internal_Rela *rel, *relend;
11148 int action_discarded;
11149 int ret;
11150
11151 /* Get the swapped relocs. */
11152 internal_relocs
11153 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11154 flinfo->internal_relocs, FALSE);
11155 if (internal_relocs == NULL
11156 && o->reloc_count > 0)
11157 return FALSE;
11158
11159 /* We need to reverse-copy input .ctors/.dtors sections if
11160 they are placed in .init_array/.finit_array for output. */
11161 if (o->size > address_size
11162 && ((strncmp (o->name, ".ctors", 6) == 0
11163 && strcmp (o->output_section->name,
11164 ".init_array") == 0)
11165 || (strncmp (o->name, ".dtors", 6) == 0
11166 && strcmp (o->output_section->name,
11167 ".fini_array") == 0))
11168 && (o->name[6] == 0 || o->name[6] == '.'))
11169 {
11170 if (o->size * bed->s->int_rels_per_ext_rel
11171 != o->reloc_count * address_size)
11172 {
11173 _bfd_error_handler
11174 /* xgettext:c-format */
11175 (_("error: %pB: size of section %pA is not "
11176 "multiple of address size"),
11177 input_bfd, o);
11178 bfd_set_error (bfd_error_bad_value);
11179 return FALSE;
11180 }
11181 o->flags |= SEC_ELF_REVERSE_COPY;
11182 }
11183
11184 action_discarded = -1;
11185 if (!elf_section_ignore_discarded_relocs (o))
11186 action_discarded = (*bed->action_discarded) (o);
11187
11188 /* Run through the relocs evaluating complex reloc symbols and
11189 looking for relocs against symbols from discarded sections
11190 or section symbols from removed link-once sections.
11191 Complain about relocs against discarded sections. Zero
11192 relocs against removed link-once sections. */
11193
11194 rel = internal_relocs;
11195 relend = rel + o->reloc_count;
11196 for ( ; rel < relend; rel++)
11197 {
11198 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11199 unsigned int s_type;
11200 asection **ps, *sec;
11201 struct elf_link_hash_entry *h = NULL;
11202 const char *sym_name;
11203
11204 if (r_symndx == STN_UNDEF)
11205 continue;
11206
11207 if (r_symndx >= locsymcount
11208 || (elf_bad_symtab (input_bfd)
11209 && flinfo->sections[r_symndx] == NULL))
11210 {
11211 h = sym_hashes[r_symndx - extsymoff];
11212
11213 /* Badly formatted input files can contain relocs that
11214 reference non-existant symbols. Check here so that
11215 we do not seg fault. */
11216 if (h == NULL)
11217 {
11218 _bfd_error_handler
11219 /* xgettext:c-format */
11220 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11221 "that references a non-existent global symbol"),
11222 input_bfd, (uint64_t) rel->r_info, o);
11223 bfd_set_error (bfd_error_bad_value);
11224 return FALSE;
11225 }
11226
11227 while (h->root.type == bfd_link_hash_indirect
11228 || h->root.type == bfd_link_hash_warning)
11229 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11230
11231 s_type = h->type;
11232
11233 /* If a plugin symbol is referenced from a non-IR file,
11234 mark the symbol as undefined. Note that the
11235 linker may attach linker created dynamic sections
11236 to the plugin bfd. Symbols defined in linker
11237 created sections are not plugin symbols. */
11238 if ((h->root.non_ir_ref_regular
11239 || h->root.non_ir_ref_dynamic)
11240 && (h->root.type == bfd_link_hash_defined
11241 || h->root.type == bfd_link_hash_defweak)
11242 && (h->root.u.def.section->flags
11243 & SEC_LINKER_CREATED) == 0
11244 && h->root.u.def.section->owner != NULL
11245 && (h->root.u.def.section->owner->flags
11246 & BFD_PLUGIN) != 0)
11247 {
11248 h->root.type = bfd_link_hash_undefined;
11249 h->root.u.undef.abfd = h->root.u.def.section->owner;
11250 }
11251
11252 ps = NULL;
11253 if (h->root.type == bfd_link_hash_defined
11254 || h->root.type == bfd_link_hash_defweak)
11255 ps = &h->root.u.def.section;
11256
11257 sym_name = h->root.root.string;
11258 }
11259 else
11260 {
11261 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11262
11263 s_type = ELF_ST_TYPE (sym->st_info);
11264 ps = &flinfo->sections[r_symndx];
11265 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11266 sym, *ps);
11267 }
11268
11269 if ((s_type == STT_RELC || s_type == STT_SRELC)
11270 && !bfd_link_relocatable (flinfo->info))
11271 {
11272 bfd_vma val;
11273 bfd_vma dot = (rel->r_offset
11274 + o->output_offset + o->output_section->vma);
11275 #ifdef DEBUG
11276 printf ("Encountered a complex symbol!");
11277 printf (" (input_bfd %s, section %s, reloc %ld\n",
11278 bfd_get_filename (input_bfd), o->name,
11279 (long) (rel - internal_relocs));
11280 printf (" symbol: idx %8.8lx, name %s\n",
11281 r_symndx, sym_name);
11282 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11283 (unsigned long) rel->r_info,
11284 (unsigned long) rel->r_offset);
11285 #endif
11286 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11287 isymbuf, locsymcount, s_type == STT_SRELC))
11288 return FALSE;
11289
11290 /* Symbol evaluated OK. Update to absolute value. */
11291 set_symbol_value (input_bfd, isymbuf, locsymcount,
11292 r_symndx, val);
11293 continue;
11294 }
11295
11296 if (action_discarded != -1 && ps != NULL)
11297 {
11298 /* Complain if the definition comes from a
11299 discarded section. */
11300 if ((sec = *ps) != NULL && discarded_section (sec))
11301 {
11302 BFD_ASSERT (r_symndx != STN_UNDEF);
11303 if (action_discarded & COMPLAIN)
11304 (*flinfo->info->callbacks->einfo)
11305 /* xgettext:c-format */
11306 (_("%X`%s' referenced in section `%pA' of %pB: "
11307 "defined in discarded section `%pA' of %pB\n"),
11308 sym_name, o, input_bfd, sec, sec->owner);
11309
11310 /* Try to do the best we can to support buggy old
11311 versions of gcc. Pretend that the symbol is
11312 really defined in the kept linkonce section.
11313 FIXME: This is quite broken. Modifying the
11314 symbol here means we will be changing all later
11315 uses of the symbol, not just in this section. */
11316 if (action_discarded & PRETEND)
11317 {
11318 asection *kept;
11319
11320 kept = _bfd_elf_check_kept_section (sec,
11321 flinfo->info);
11322 if (kept != NULL)
11323 {
11324 *ps = kept;
11325 continue;
11326 }
11327 }
11328 }
11329 }
11330 }
11331
11332 /* Relocate the section by invoking a back end routine.
11333
11334 The back end routine is responsible for adjusting the
11335 section contents as necessary, and (if using Rela relocs
11336 and generating a relocatable output file) adjusting the
11337 reloc addend as necessary.
11338
11339 The back end routine does not have to worry about setting
11340 the reloc address or the reloc symbol index.
11341
11342 The back end routine is given a pointer to the swapped in
11343 internal symbols, and can access the hash table entries
11344 for the external symbols via elf_sym_hashes (input_bfd).
11345
11346 When generating relocatable output, the back end routine
11347 must handle STB_LOCAL/STT_SECTION symbols specially. The
11348 output symbol is going to be a section symbol
11349 corresponding to the output section, which will require
11350 the addend to be adjusted. */
11351
11352 ret = (*relocate_section) (output_bfd, flinfo->info,
11353 input_bfd, o, contents,
11354 internal_relocs,
11355 isymbuf,
11356 flinfo->sections);
11357 if (!ret)
11358 return FALSE;
11359
11360 if (ret == 2
11361 || bfd_link_relocatable (flinfo->info)
11362 || flinfo->info->emitrelocations)
11363 {
11364 Elf_Internal_Rela *irela;
11365 Elf_Internal_Rela *irelaend, *irelamid;
11366 bfd_vma last_offset;
11367 struct elf_link_hash_entry **rel_hash;
11368 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11369 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11370 unsigned int next_erel;
11371 bfd_boolean rela_normal;
11372 struct bfd_elf_section_data *esdi, *esdo;
11373
11374 esdi = elf_section_data (o);
11375 esdo = elf_section_data (o->output_section);
11376 rela_normal = FALSE;
11377
11378 /* Adjust the reloc addresses and symbol indices. */
11379
11380 irela = internal_relocs;
11381 irelaend = irela + o->reloc_count;
11382 rel_hash = esdo->rel.hashes + esdo->rel.count;
11383 /* We start processing the REL relocs, if any. When we reach
11384 IRELAMID in the loop, we switch to the RELA relocs. */
11385 irelamid = irela;
11386 if (esdi->rel.hdr != NULL)
11387 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11388 * bed->s->int_rels_per_ext_rel);
11389 rel_hash_list = rel_hash;
11390 rela_hash_list = NULL;
11391 last_offset = o->output_offset;
11392 if (!bfd_link_relocatable (flinfo->info))
11393 last_offset += o->output_section->vma;
11394 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11395 {
11396 unsigned long r_symndx;
11397 asection *sec;
11398 Elf_Internal_Sym sym;
11399
11400 if (next_erel == bed->s->int_rels_per_ext_rel)
11401 {
11402 rel_hash++;
11403 next_erel = 0;
11404 }
11405
11406 if (irela == irelamid)
11407 {
11408 rel_hash = esdo->rela.hashes + esdo->rela.count;
11409 rela_hash_list = rel_hash;
11410 rela_normal = bed->rela_normal;
11411 }
11412
11413 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11414 flinfo->info, o,
11415 irela->r_offset);
11416 if (irela->r_offset >= (bfd_vma) -2)
11417 {
11418 /* This is a reloc for a deleted entry or somesuch.
11419 Turn it into an R_*_NONE reloc, at the same
11420 offset as the last reloc. elf_eh_frame.c and
11421 bfd_elf_discard_info rely on reloc offsets
11422 being ordered. */
11423 irela->r_offset = last_offset;
11424 irela->r_info = 0;
11425 irela->r_addend = 0;
11426 continue;
11427 }
11428
11429 irela->r_offset += o->output_offset;
11430
11431 /* Relocs in an executable have to be virtual addresses. */
11432 if (!bfd_link_relocatable (flinfo->info))
11433 irela->r_offset += o->output_section->vma;
11434
11435 last_offset = irela->r_offset;
11436
11437 r_symndx = irela->r_info >> r_sym_shift;
11438 if (r_symndx == STN_UNDEF)
11439 continue;
11440
11441 if (r_symndx >= locsymcount
11442 || (elf_bad_symtab (input_bfd)
11443 && flinfo->sections[r_symndx] == NULL))
11444 {
11445 struct elf_link_hash_entry *rh;
11446 unsigned long indx;
11447
11448 /* This is a reloc against a global symbol. We
11449 have not yet output all the local symbols, so
11450 we do not know the symbol index of any global
11451 symbol. We set the rel_hash entry for this
11452 reloc to point to the global hash table entry
11453 for this symbol. The symbol index is then
11454 set at the end of bfd_elf_final_link. */
11455 indx = r_symndx - extsymoff;
11456 rh = elf_sym_hashes (input_bfd)[indx];
11457 while (rh->root.type == bfd_link_hash_indirect
11458 || rh->root.type == bfd_link_hash_warning)
11459 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11460
11461 /* Setting the index to -2 tells
11462 elf_link_output_extsym that this symbol is
11463 used by a reloc. */
11464 BFD_ASSERT (rh->indx < 0);
11465 rh->indx = -2;
11466 *rel_hash = rh;
11467
11468 continue;
11469 }
11470
11471 /* This is a reloc against a local symbol. */
11472
11473 *rel_hash = NULL;
11474 sym = isymbuf[r_symndx];
11475 sec = flinfo->sections[r_symndx];
11476 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11477 {
11478 /* I suppose the backend ought to fill in the
11479 section of any STT_SECTION symbol against a
11480 processor specific section. */
11481 r_symndx = STN_UNDEF;
11482 if (bfd_is_abs_section (sec))
11483 ;
11484 else if (sec == NULL || sec->owner == NULL)
11485 {
11486 bfd_set_error (bfd_error_bad_value);
11487 return FALSE;
11488 }
11489 else
11490 {
11491 asection *osec = sec->output_section;
11492
11493 /* If we have discarded a section, the output
11494 section will be the absolute section. In
11495 case of discarded SEC_MERGE sections, use
11496 the kept section. relocate_section should
11497 have already handled discarded linkonce
11498 sections. */
11499 if (bfd_is_abs_section (osec)
11500 && sec->kept_section != NULL
11501 && sec->kept_section->output_section != NULL)
11502 {
11503 osec = sec->kept_section->output_section;
11504 irela->r_addend -= osec->vma;
11505 }
11506
11507 if (!bfd_is_abs_section (osec))
11508 {
11509 r_symndx = osec->target_index;
11510 if (r_symndx == STN_UNDEF)
11511 {
11512 irela->r_addend += osec->vma;
11513 osec = _bfd_nearby_section (output_bfd, osec,
11514 osec->vma);
11515 irela->r_addend -= osec->vma;
11516 r_symndx = osec->target_index;
11517 }
11518 }
11519 }
11520
11521 /* Adjust the addend according to where the
11522 section winds up in the output section. */
11523 if (rela_normal)
11524 irela->r_addend += sec->output_offset;
11525 }
11526 else
11527 {
11528 if (flinfo->indices[r_symndx] == -1)
11529 {
11530 unsigned long shlink;
11531 const char *name;
11532 asection *osec;
11533 long indx;
11534
11535 if (flinfo->info->strip == strip_all)
11536 {
11537 /* You can't do ld -r -s. */
11538 bfd_set_error (bfd_error_invalid_operation);
11539 return FALSE;
11540 }
11541
11542 /* This symbol was skipped earlier, but
11543 since it is needed by a reloc, we
11544 must output it now. */
11545 shlink = symtab_hdr->sh_link;
11546 name = (bfd_elf_string_from_elf_section
11547 (input_bfd, shlink, sym.st_name));
11548 if (name == NULL)
11549 return FALSE;
11550
11551 osec = sec->output_section;
11552 sym.st_shndx =
11553 _bfd_elf_section_from_bfd_section (output_bfd,
11554 osec);
11555 if (sym.st_shndx == SHN_BAD)
11556 return FALSE;
11557
11558 sym.st_value += sec->output_offset;
11559 if (!bfd_link_relocatable (flinfo->info))
11560 {
11561 sym.st_value += osec->vma;
11562 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11563 {
11564 struct elf_link_hash_table *htab
11565 = elf_hash_table (flinfo->info);
11566
11567 /* STT_TLS symbols are relative to PT_TLS
11568 segment base. */
11569 if (htab->tls_sec != NULL)
11570 sym.st_value -= htab->tls_sec->vma;
11571 else
11572 sym.st_info
11573 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11574 STT_NOTYPE);
11575 }
11576 }
11577
11578 indx = bfd_get_symcount (output_bfd);
11579 ret = elf_link_output_symstrtab (flinfo, name,
11580 &sym, sec,
11581 NULL);
11582 if (ret == 0)
11583 return FALSE;
11584 else if (ret == 1)
11585 flinfo->indices[r_symndx] = indx;
11586 else
11587 abort ();
11588 }
11589
11590 r_symndx = flinfo->indices[r_symndx];
11591 }
11592
11593 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11594 | (irela->r_info & r_type_mask));
11595 }
11596
11597 /* Swap out the relocs. */
11598 input_rel_hdr = esdi->rel.hdr;
11599 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11600 {
11601 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11602 input_rel_hdr,
11603 internal_relocs,
11604 rel_hash_list))
11605 return FALSE;
11606 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11607 * bed->s->int_rels_per_ext_rel);
11608 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11609 }
11610
11611 input_rela_hdr = esdi->rela.hdr;
11612 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11613 {
11614 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11615 input_rela_hdr,
11616 internal_relocs,
11617 rela_hash_list))
11618 return FALSE;
11619 }
11620 }
11621 }
11622
11623 /* Write out the modified section contents. */
11624 if (bed->elf_backend_write_section
11625 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11626 contents))
11627 {
11628 /* Section written out. */
11629 }
11630 else switch (o->sec_info_type)
11631 {
11632 case SEC_INFO_TYPE_STABS:
11633 if (! (_bfd_write_section_stabs
11634 (output_bfd,
11635 &elf_hash_table (flinfo->info)->stab_info,
11636 o, &elf_section_data (o)->sec_info, contents)))
11637 return FALSE;
11638 break;
11639 case SEC_INFO_TYPE_MERGE:
11640 if (! _bfd_write_merged_section (output_bfd, o,
11641 elf_section_data (o)->sec_info))
11642 return FALSE;
11643 break;
11644 case SEC_INFO_TYPE_EH_FRAME:
11645 {
11646 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11647 o, contents))
11648 return FALSE;
11649 }
11650 break;
11651 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11652 {
11653 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11654 flinfo->info,
11655 o, contents))
11656 return FALSE;
11657 }
11658 break;
11659 default:
11660 {
11661 if (! (o->flags & SEC_EXCLUDE))
11662 {
11663 file_ptr offset = (file_ptr) o->output_offset;
11664 bfd_size_type todo = o->size;
11665
11666 offset *= bfd_octets_per_byte (output_bfd, o);
11667
11668 if ((o->flags & SEC_ELF_REVERSE_COPY))
11669 {
11670 /* Reverse-copy input section to output. */
11671 do
11672 {
11673 todo -= address_size;
11674 if (! bfd_set_section_contents (output_bfd,
11675 o->output_section,
11676 contents + todo,
11677 offset,
11678 address_size))
11679 return FALSE;
11680 if (todo == 0)
11681 break;
11682 offset += address_size;
11683 }
11684 while (1);
11685 }
11686 else if (! bfd_set_section_contents (output_bfd,
11687 o->output_section,
11688 contents,
11689 offset, todo))
11690 return FALSE;
11691 }
11692 }
11693 break;
11694 }
11695 }
11696
11697 return TRUE;
11698 }
11699
11700 /* Generate a reloc when linking an ELF file. This is a reloc
11701 requested by the linker, and does not come from any input file. This
11702 is used to build constructor and destructor tables when linking
11703 with -Ur. */
11704
11705 static bfd_boolean
11706 elf_reloc_link_order (bfd *output_bfd,
11707 struct bfd_link_info *info,
11708 asection *output_section,
11709 struct bfd_link_order *link_order)
11710 {
11711 reloc_howto_type *howto;
11712 long indx;
11713 bfd_vma offset;
11714 bfd_vma addend;
11715 struct bfd_elf_section_reloc_data *reldata;
11716 struct elf_link_hash_entry **rel_hash_ptr;
11717 Elf_Internal_Shdr *rel_hdr;
11718 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11719 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11720 bfd_byte *erel;
11721 unsigned int i;
11722 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11723
11724 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11725 if (howto == NULL)
11726 {
11727 bfd_set_error (bfd_error_bad_value);
11728 return FALSE;
11729 }
11730
11731 addend = link_order->u.reloc.p->addend;
11732
11733 if (esdo->rel.hdr)
11734 reldata = &esdo->rel;
11735 else if (esdo->rela.hdr)
11736 reldata = &esdo->rela;
11737 else
11738 {
11739 reldata = NULL;
11740 BFD_ASSERT (0);
11741 }
11742
11743 /* Figure out the symbol index. */
11744 rel_hash_ptr = reldata->hashes + reldata->count;
11745 if (link_order->type == bfd_section_reloc_link_order)
11746 {
11747 indx = link_order->u.reloc.p->u.section->target_index;
11748 BFD_ASSERT (indx != 0);
11749 *rel_hash_ptr = NULL;
11750 }
11751 else
11752 {
11753 struct elf_link_hash_entry *h;
11754
11755 /* Treat a reloc against a defined symbol as though it were
11756 actually against the section. */
11757 h = ((struct elf_link_hash_entry *)
11758 bfd_wrapped_link_hash_lookup (output_bfd, info,
11759 link_order->u.reloc.p->u.name,
11760 FALSE, FALSE, TRUE));
11761 if (h != NULL
11762 && (h->root.type == bfd_link_hash_defined
11763 || h->root.type == bfd_link_hash_defweak))
11764 {
11765 asection *section;
11766
11767 section = h->root.u.def.section;
11768 indx = section->output_section->target_index;
11769 *rel_hash_ptr = NULL;
11770 /* It seems that we ought to add the symbol value to the
11771 addend here, but in practice it has already been added
11772 because it was passed to constructor_callback. */
11773 addend += section->output_section->vma + section->output_offset;
11774 }
11775 else if (h != NULL)
11776 {
11777 /* Setting the index to -2 tells elf_link_output_extsym that
11778 this symbol is used by a reloc. */
11779 h->indx = -2;
11780 *rel_hash_ptr = h;
11781 indx = 0;
11782 }
11783 else
11784 {
11785 (*info->callbacks->unattached_reloc)
11786 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11787 indx = 0;
11788 }
11789 }
11790
11791 /* If this is an inplace reloc, we must write the addend into the
11792 object file. */
11793 if (howto->partial_inplace && addend != 0)
11794 {
11795 bfd_size_type size;
11796 bfd_reloc_status_type rstat;
11797 bfd_byte *buf;
11798 bfd_boolean ok;
11799 const char *sym_name;
11800 bfd_size_type octets;
11801
11802 size = (bfd_size_type) bfd_get_reloc_size (howto);
11803 buf = (bfd_byte *) bfd_zmalloc (size);
11804 if (buf == NULL && size != 0)
11805 return FALSE;
11806 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11807 switch (rstat)
11808 {
11809 case bfd_reloc_ok:
11810 break;
11811
11812 default:
11813 case bfd_reloc_outofrange:
11814 abort ();
11815
11816 case bfd_reloc_overflow:
11817 if (link_order->type == bfd_section_reloc_link_order)
11818 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11819 else
11820 sym_name = link_order->u.reloc.p->u.name;
11821 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11822 howto->name, addend, NULL, NULL,
11823 (bfd_vma) 0);
11824 break;
11825 }
11826
11827 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11828 output_section);
11829 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11830 octets, size);
11831 free (buf);
11832 if (! ok)
11833 return FALSE;
11834 }
11835
11836 /* The address of a reloc is relative to the section in a
11837 relocatable file, and is a virtual address in an executable
11838 file. */
11839 offset = link_order->offset;
11840 if (! bfd_link_relocatable (info))
11841 offset += output_section->vma;
11842
11843 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11844 {
11845 irel[i].r_offset = offset;
11846 irel[i].r_info = 0;
11847 irel[i].r_addend = 0;
11848 }
11849 if (bed->s->arch_size == 32)
11850 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11851 else
11852 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11853
11854 rel_hdr = reldata->hdr;
11855 erel = rel_hdr->contents;
11856 if (rel_hdr->sh_type == SHT_REL)
11857 {
11858 erel += reldata->count * bed->s->sizeof_rel;
11859 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11860 }
11861 else
11862 {
11863 irel[0].r_addend = addend;
11864 erel += reldata->count * bed->s->sizeof_rela;
11865 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11866 }
11867
11868 ++reldata->count;
11869
11870 return TRUE;
11871 }
11872
11873 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11874 Returns TRUE upon success, FALSE otherwise. */
11875
11876 static bfd_boolean
11877 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11878 {
11879 bfd_boolean ret = FALSE;
11880 bfd *implib_bfd;
11881 const struct elf_backend_data *bed;
11882 flagword flags;
11883 enum bfd_architecture arch;
11884 unsigned int mach;
11885 asymbol **sympp = NULL;
11886 long symsize;
11887 long symcount;
11888 long src_count;
11889 elf_symbol_type *osymbuf;
11890 size_t amt;
11891
11892 implib_bfd = info->out_implib_bfd;
11893 bed = get_elf_backend_data (abfd);
11894
11895 if (!bfd_set_format (implib_bfd, bfd_object))
11896 return FALSE;
11897
11898 /* Use flag from executable but make it a relocatable object. */
11899 flags = bfd_get_file_flags (abfd);
11900 flags &= ~HAS_RELOC;
11901 if (!bfd_set_start_address (implib_bfd, 0)
11902 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11903 return FALSE;
11904
11905 /* Copy architecture of output file to import library file. */
11906 arch = bfd_get_arch (abfd);
11907 mach = bfd_get_mach (abfd);
11908 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11909 && (abfd->target_defaulted
11910 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11911 return FALSE;
11912
11913 /* Get symbol table size. */
11914 symsize = bfd_get_symtab_upper_bound (abfd);
11915 if (symsize < 0)
11916 return FALSE;
11917
11918 /* Read in the symbol table. */
11919 sympp = (asymbol **) bfd_malloc (symsize);
11920 if (sympp == NULL)
11921 return FALSE;
11922
11923 symcount = bfd_canonicalize_symtab (abfd, sympp);
11924 if (symcount < 0)
11925 goto free_sym_buf;
11926
11927 /* Allow the BFD backend to copy any private header data it
11928 understands from the output BFD to the import library BFD. */
11929 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11930 goto free_sym_buf;
11931
11932 /* Filter symbols to appear in the import library. */
11933 if (bed->elf_backend_filter_implib_symbols)
11934 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11935 symcount);
11936 else
11937 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11938 if (symcount == 0)
11939 {
11940 bfd_set_error (bfd_error_no_symbols);
11941 _bfd_error_handler (_("%pB: no symbol found for import library"),
11942 implib_bfd);
11943 goto free_sym_buf;
11944 }
11945
11946
11947 /* Make symbols absolute. */
11948 amt = symcount * sizeof (*osymbuf);
11949 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11950 if (osymbuf == NULL)
11951 goto free_sym_buf;
11952
11953 for (src_count = 0; src_count < symcount; src_count++)
11954 {
11955 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11956 sizeof (*osymbuf));
11957 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11958 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11959 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11960 osymbuf[src_count].internal_elf_sym.st_value =
11961 osymbuf[src_count].symbol.value;
11962 sympp[src_count] = &osymbuf[src_count].symbol;
11963 }
11964
11965 bfd_set_symtab (implib_bfd, sympp, symcount);
11966
11967 /* Allow the BFD backend to copy any private data it understands
11968 from the output BFD to the import library BFD. This is done last
11969 to permit the routine to look at the filtered symbol table. */
11970 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11971 goto free_sym_buf;
11972
11973 if (!bfd_close (implib_bfd))
11974 goto free_sym_buf;
11975
11976 ret = TRUE;
11977
11978 free_sym_buf:
11979 free (sympp);
11980 return ret;
11981 }
11982
11983 static void
11984 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11985 {
11986 asection *o;
11987
11988 if (flinfo->symstrtab != NULL)
11989 _bfd_elf_strtab_free (flinfo->symstrtab);
11990 free (flinfo->contents);
11991 free (flinfo->external_relocs);
11992 free (flinfo->internal_relocs);
11993 free (flinfo->external_syms);
11994 free (flinfo->locsym_shndx);
11995 free (flinfo->internal_syms);
11996 free (flinfo->indices);
11997 free (flinfo->sections);
11998 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11999 free (flinfo->symshndxbuf);
12000 for (o = obfd->sections; o != NULL; o = o->next)
12001 {
12002 struct bfd_elf_section_data *esdo = elf_section_data (o);
12003 free (esdo->rel.hashes);
12004 free (esdo->rela.hashes);
12005 }
12006 }
12007
12008 /* Do the final step of an ELF link. */
12009
12010 bfd_boolean
12011 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12012 {
12013 bfd_boolean dynamic;
12014 bfd_boolean emit_relocs;
12015 bfd *dynobj;
12016 struct elf_final_link_info flinfo;
12017 asection *o;
12018 struct bfd_link_order *p;
12019 bfd *sub;
12020 bfd_size_type max_contents_size;
12021 bfd_size_type max_external_reloc_size;
12022 bfd_size_type max_internal_reloc_count;
12023 bfd_size_type max_sym_count;
12024 bfd_size_type max_sym_shndx_count;
12025 Elf_Internal_Sym elfsym;
12026 unsigned int i;
12027 Elf_Internal_Shdr *symtab_hdr;
12028 Elf_Internal_Shdr *symtab_shndx_hdr;
12029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12030 struct elf_outext_info eoinfo;
12031 bfd_boolean merged;
12032 size_t relativecount = 0;
12033 asection *reldyn = 0;
12034 bfd_size_type amt;
12035 asection *attr_section = NULL;
12036 bfd_vma attr_size = 0;
12037 const char *std_attrs_section;
12038 struct elf_link_hash_table *htab = elf_hash_table (info);
12039 bfd_boolean sections_removed;
12040 bfd_boolean ret;
12041
12042 if (!is_elf_hash_table (htab))
12043 return FALSE;
12044
12045 if (bfd_link_pic (info))
12046 abfd->flags |= DYNAMIC;
12047
12048 dynamic = htab->dynamic_sections_created;
12049 dynobj = htab->dynobj;
12050
12051 emit_relocs = (bfd_link_relocatable (info)
12052 || info->emitrelocations);
12053
12054 memset (&flinfo, 0, sizeof (flinfo));
12055 flinfo.info = info;
12056 flinfo.output_bfd = abfd;
12057 flinfo.symstrtab = _bfd_elf_strtab_init ();
12058 if (flinfo.symstrtab == NULL)
12059 return FALSE;
12060
12061 if (! dynamic)
12062 {
12063 flinfo.hash_sec = NULL;
12064 flinfo.symver_sec = NULL;
12065 }
12066 else
12067 {
12068 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12069 /* Note that dynsym_sec can be NULL (on VMS). */
12070 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12071 /* Note that it is OK if symver_sec is NULL. */
12072 }
12073
12074 if (info->unique_symbol
12075 && !bfd_hash_table_init (&flinfo.local_hash_table,
12076 local_hash_newfunc,
12077 sizeof (struct local_hash_entry)))
12078 return FALSE;
12079
12080 /* The object attributes have been merged. Remove the input
12081 sections from the link, and set the contents of the output
12082 section. */
12083 sections_removed = FALSE;
12084 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12085 for (o = abfd->sections; o != NULL; o = o->next)
12086 {
12087 bfd_boolean remove_section = FALSE;
12088
12089 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12090 || strcmp (o->name, ".gnu.attributes") == 0)
12091 {
12092 for (p = o->map_head.link_order; p != NULL; p = p->next)
12093 {
12094 asection *input_section;
12095
12096 if (p->type != bfd_indirect_link_order)
12097 continue;
12098 input_section = p->u.indirect.section;
12099 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12100 elf_link_input_bfd ignores this section. */
12101 input_section->flags &= ~SEC_HAS_CONTENTS;
12102 }
12103
12104 attr_size = bfd_elf_obj_attr_size (abfd);
12105 bfd_set_section_size (o, attr_size);
12106 /* Skip this section later on. */
12107 o->map_head.link_order = NULL;
12108 if (attr_size)
12109 attr_section = o;
12110 else
12111 remove_section = TRUE;
12112 }
12113 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12114 {
12115 /* Remove empty group section from linker output. */
12116 remove_section = TRUE;
12117 }
12118 if (remove_section)
12119 {
12120 o->flags |= SEC_EXCLUDE;
12121 bfd_section_list_remove (abfd, o);
12122 abfd->section_count--;
12123 sections_removed = TRUE;
12124 }
12125 }
12126 if (sections_removed)
12127 _bfd_fix_excluded_sec_syms (abfd, info);
12128
12129 /* Count up the number of relocations we will output for each output
12130 section, so that we know the sizes of the reloc sections. We
12131 also figure out some maximum sizes. */
12132 max_contents_size = 0;
12133 max_external_reloc_size = 0;
12134 max_internal_reloc_count = 0;
12135 max_sym_count = 0;
12136 max_sym_shndx_count = 0;
12137 merged = FALSE;
12138 for (o = abfd->sections; o != NULL; o = o->next)
12139 {
12140 struct bfd_elf_section_data *esdo = elf_section_data (o);
12141 o->reloc_count = 0;
12142
12143 for (p = o->map_head.link_order; p != NULL; p = p->next)
12144 {
12145 unsigned int reloc_count = 0;
12146 unsigned int additional_reloc_count = 0;
12147 struct bfd_elf_section_data *esdi = NULL;
12148
12149 if (p->type == bfd_section_reloc_link_order
12150 || p->type == bfd_symbol_reloc_link_order)
12151 reloc_count = 1;
12152 else if (p->type == bfd_indirect_link_order)
12153 {
12154 asection *sec;
12155
12156 sec = p->u.indirect.section;
12157
12158 /* Mark all sections which are to be included in the
12159 link. This will normally be every section. We need
12160 to do this so that we can identify any sections which
12161 the linker has decided to not include. */
12162 sec->linker_mark = TRUE;
12163
12164 if (sec->flags & SEC_MERGE)
12165 merged = TRUE;
12166
12167 if (sec->rawsize > max_contents_size)
12168 max_contents_size = sec->rawsize;
12169 if (sec->size > max_contents_size)
12170 max_contents_size = sec->size;
12171
12172 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12173 && (sec->owner->flags & DYNAMIC) == 0)
12174 {
12175 size_t sym_count;
12176
12177 /* We are interested in just local symbols, not all
12178 symbols. */
12179 if (elf_bad_symtab (sec->owner))
12180 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12181 / bed->s->sizeof_sym);
12182 else
12183 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12184
12185 if (sym_count > max_sym_count)
12186 max_sym_count = sym_count;
12187
12188 if (sym_count > max_sym_shndx_count
12189 && elf_symtab_shndx_list (sec->owner) != NULL)
12190 max_sym_shndx_count = sym_count;
12191
12192 if (esdo->this_hdr.sh_type == SHT_REL
12193 || esdo->this_hdr.sh_type == SHT_RELA)
12194 /* Some backends use reloc_count in relocation sections
12195 to count particular types of relocs. Of course,
12196 reloc sections themselves can't have relocations. */
12197 ;
12198 else if (emit_relocs)
12199 {
12200 reloc_count = sec->reloc_count;
12201 if (bed->elf_backend_count_additional_relocs)
12202 {
12203 int c;
12204 c = (*bed->elf_backend_count_additional_relocs) (sec);
12205 additional_reloc_count += c;
12206 }
12207 }
12208 else if (bed->elf_backend_count_relocs)
12209 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12210
12211 esdi = elf_section_data (sec);
12212
12213 if ((sec->flags & SEC_RELOC) != 0)
12214 {
12215 size_t ext_size = 0;
12216
12217 if (esdi->rel.hdr != NULL)
12218 ext_size = esdi->rel.hdr->sh_size;
12219 if (esdi->rela.hdr != NULL)
12220 ext_size += esdi->rela.hdr->sh_size;
12221
12222 if (ext_size > max_external_reloc_size)
12223 max_external_reloc_size = ext_size;
12224 if (sec->reloc_count > max_internal_reloc_count)
12225 max_internal_reloc_count = sec->reloc_count;
12226 }
12227 }
12228 }
12229
12230 if (reloc_count == 0)
12231 continue;
12232
12233 reloc_count += additional_reloc_count;
12234 o->reloc_count += reloc_count;
12235
12236 if (p->type == bfd_indirect_link_order && emit_relocs)
12237 {
12238 if (esdi->rel.hdr)
12239 {
12240 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12241 esdo->rel.count += additional_reloc_count;
12242 }
12243 if (esdi->rela.hdr)
12244 {
12245 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12246 esdo->rela.count += additional_reloc_count;
12247 }
12248 }
12249 else
12250 {
12251 if (o->use_rela_p)
12252 esdo->rela.count += reloc_count;
12253 else
12254 esdo->rel.count += reloc_count;
12255 }
12256 }
12257
12258 if (o->reloc_count > 0)
12259 o->flags |= SEC_RELOC;
12260 else
12261 {
12262 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12263 set it (this is probably a bug) and if it is set
12264 assign_section_numbers will create a reloc section. */
12265 o->flags &=~ SEC_RELOC;
12266 }
12267
12268 /* If the SEC_ALLOC flag is not set, force the section VMA to
12269 zero. This is done in elf_fake_sections as well, but forcing
12270 the VMA to 0 here will ensure that relocs against these
12271 sections are handled correctly. */
12272 if ((o->flags & SEC_ALLOC) == 0
12273 && ! o->user_set_vma)
12274 o->vma = 0;
12275 }
12276
12277 if (! bfd_link_relocatable (info) && merged)
12278 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12279
12280 /* Figure out the file positions for everything but the symbol table
12281 and the relocs. We set symcount to force assign_section_numbers
12282 to create a symbol table. */
12283 abfd->symcount = info->strip != strip_all || emit_relocs;
12284 BFD_ASSERT (! abfd->output_has_begun);
12285 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12286 goto error_return;
12287
12288 /* Set sizes, and assign file positions for reloc sections. */
12289 for (o = abfd->sections; o != NULL; o = o->next)
12290 {
12291 struct bfd_elf_section_data *esdo = elf_section_data (o);
12292 if ((o->flags & SEC_RELOC) != 0)
12293 {
12294 if (esdo->rel.hdr
12295 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12296 goto error_return;
12297
12298 if (esdo->rela.hdr
12299 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12300 goto error_return;
12301 }
12302
12303 /* _bfd_elf_compute_section_file_positions makes temporary use
12304 of target_index. Reset it. */
12305 o->target_index = 0;
12306
12307 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12308 to count upwards while actually outputting the relocations. */
12309 esdo->rel.count = 0;
12310 esdo->rela.count = 0;
12311
12312 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12313 && !bfd_section_is_ctf (o))
12314 {
12315 /* Cache the section contents so that they can be compressed
12316 later. Use bfd_malloc since it will be freed by
12317 bfd_compress_section_contents. */
12318 unsigned char *contents = esdo->this_hdr.contents;
12319 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12320 abort ();
12321 contents
12322 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12323 if (contents == NULL)
12324 goto error_return;
12325 esdo->this_hdr.contents = contents;
12326 }
12327 }
12328
12329 /* We have now assigned file positions for all the sections except .symtab,
12330 .strtab, and non-loaded reloc and compressed debugging sections. We start
12331 the .symtab section at the current file position, and write directly to it.
12332 We build the .strtab section in memory. */
12333 abfd->symcount = 0;
12334 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12335 /* sh_name is set in prep_headers. */
12336 symtab_hdr->sh_type = SHT_SYMTAB;
12337 /* sh_flags, sh_addr and sh_size all start off zero. */
12338 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12339 /* sh_link is set in assign_section_numbers. */
12340 /* sh_info is set below. */
12341 /* sh_offset is set just below. */
12342 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12343
12344 if (max_sym_count < 20)
12345 max_sym_count = 20;
12346 htab->strtabsize = max_sym_count;
12347 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12348 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12349 if (htab->strtab == NULL)
12350 goto error_return;
12351 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12352 flinfo.symshndxbuf
12353 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12354 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12355
12356 if (info->strip != strip_all || emit_relocs)
12357 {
12358 file_ptr off = elf_next_file_pos (abfd);
12359
12360 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12361
12362 /* Note that at this point elf_next_file_pos (abfd) is
12363 incorrect. We do not yet know the size of the .symtab section.
12364 We correct next_file_pos below, after we do know the size. */
12365
12366 /* Start writing out the symbol table. The first symbol is always a
12367 dummy symbol. */
12368 elfsym.st_value = 0;
12369 elfsym.st_size = 0;
12370 elfsym.st_info = 0;
12371 elfsym.st_other = 0;
12372 elfsym.st_shndx = SHN_UNDEF;
12373 elfsym.st_target_internal = 0;
12374 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12375 bfd_und_section_ptr, NULL) != 1)
12376 goto error_return;
12377
12378 /* Output a symbol for each section if asked or they are used for
12379 relocs. These symbols usually have no names. We store the
12380 index of each one in the index field of the section, so that
12381 we can find it again when outputting relocs. */
12382
12383 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12384 {
12385 bfd_boolean name_local_sections
12386 = (bed->elf_backend_name_local_section_symbols
12387 && bed->elf_backend_name_local_section_symbols (abfd));
12388 const char *name = NULL;
12389
12390 elfsym.st_size = 0;
12391 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12392 elfsym.st_other = 0;
12393 elfsym.st_value = 0;
12394 elfsym.st_target_internal = 0;
12395 for (i = 1; i < elf_numsections (abfd); i++)
12396 {
12397 o = bfd_section_from_elf_index (abfd, i);
12398 if (o != NULL)
12399 {
12400 o->target_index = bfd_get_symcount (abfd);
12401 elfsym.st_shndx = i;
12402 if (!bfd_link_relocatable (info))
12403 elfsym.st_value = o->vma;
12404 if (name_local_sections)
12405 name = o->name;
12406 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12407 NULL) != 1)
12408 goto error_return;
12409 }
12410 }
12411 }
12412 }
12413
12414 /* On some targets like Irix 5 the symbol split between local and global
12415 ones recorded in the sh_info field needs to be done between section
12416 and all other symbols. */
12417 if (bed->elf_backend_elfsym_local_is_section
12418 && bed->elf_backend_elfsym_local_is_section (abfd))
12419 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12420
12421 /* Allocate some memory to hold information read in from the input
12422 files. */
12423 if (max_contents_size != 0)
12424 {
12425 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12426 if (flinfo.contents == NULL)
12427 goto error_return;
12428 }
12429
12430 if (max_external_reloc_size != 0)
12431 {
12432 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12433 if (flinfo.external_relocs == NULL)
12434 goto error_return;
12435 }
12436
12437 if (max_internal_reloc_count != 0)
12438 {
12439 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12440 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12441 if (flinfo.internal_relocs == NULL)
12442 goto error_return;
12443 }
12444
12445 if (max_sym_count != 0)
12446 {
12447 amt = max_sym_count * bed->s->sizeof_sym;
12448 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12449 if (flinfo.external_syms == NULL)
12450 goto error_return;
12451
12452 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12453 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12454 if (flinfo.internal_syms == NULL)
12455 goto error_return;
12456
12457 amt = max_sym_count * sizeof (long);
12458 flinfo.indices = (long int *) bfd_malloc (amt);
12459 if (flinfo.indices == NULL)
12460 goto error_return;
12461
12462 amt = max_sym_count * sizeof (asection *);
12463 flinfo.sections = (asection **) bfd_malloc (amt);
12464 if (flinfo.sections == NULL)
12465 goto error_return;
12466 }
12467
12468 if (max_sym_shndx_count != 0)
12469 {
12470 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12471 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12472 if (flinfo.locsym_shndx == NULL)
12473 goto error_return;
12474 }
12475
12476 if (htab->tls_sec)
12477 {
12478 bfd_vma base, end = 0; /* Both bytes. */
12479 asection *sec;
12480
12481 for (sec = htab->tls_sec;
12482 sec && (sec->flags & SEC_THREAD_LOCAL);
12483 sec = sec->next)
12484 {
12485 bfd_size_type size = sec->size;
12486 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12487
12488 if (size == 0
12489 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12490 {
12491 struct bfd_link_order *ord = sec->map_tail.link_order;
12492
12493 if (ord != NULL)
12494 size = ord->offset * opb + ord->size;
12495 }
12496 end = sec->vma + size / opb;
12497 }
12498 base = htab->tls_sec->vma;
12499 /* Only align end of TLS section if static TLS doesn't have special
12500 alignment requirements. */
12501 if (bed->static_tls_alignment == 1)
12502 end = align_power (end, htab->tls_sec->alignment_power);
12503 htab->tls_size = end - base;
12504 }
12505
12506 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12507 return FALSE;
12508
12509 /* Since ELF permits relocations to be against local symbols, we
12510 must have the local symbols available when we do the relocations.
12511 Since we would rather only read the local symbols once, and we
12512 would rather not keep them in memory, we handle all the
12513 relocations for a single input file at the same time.
12514
12515 Unfortunately, there is no way to know the total number of local
12516 symbols until we have seen all of them, and the local symbol
12517 indices precede the global symbol indices. This means that when
12518 we are generating relocatable output, and we see a reloc against
12519 a global symbol, we can not know the symbol index until we have
12520 finished examining all the local symbols to see which ones we are
12521 going to output. To deal with this, we keep the relocations in
12522 memory, and don't output them until the end of the link. This is
12523 an unfortunate waste of memory, but I don't see a good way around
12524 it. Fortunately, it only happens when performing a relocatable
12525 link, which is not the common case. FIXME: If keep_memory is set
12526 we could write the relocs out and then read them again; I don't
12527 know how bad the memory loss will be. */
12528
12529 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12530 sub->output_has_begun = FALSE;
12531 for (o = abfd->sections; o != NULL; o = o->next)
12532 {
12533 for (p = o->map_head.link_order; p != NULL; p = p->next)
12534 {
12535 if (p->type == bfd_indirect_link_order
12536 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12537 == bfd_target_elf_flavour)
12538 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12539 {
12540 if (! sub->output_has_begun)
12541 {
12542 if (! elf_link_input_bfd (&flinfo, sub))
12543 goto error_return;
12544 sub->output_has_begun = TRUE;
12545 }
12546 }
12547 else if (p->type == bfd_section_reloc_link_order
12548 || p->type == bfd_symbol_reloc_link_order)
12549 {
12550 if (! elf_reloc_link_order (abfd, info, o, p))
12551 goto error_return;
12552 }
12553 else
12554 {
12555 if (! _bfd_default_link_order (abfd, info, o, p))
12556 {
12557 if (p->type == bfd_indirect_link_order
12558 && (bfd_get_flavour (sub)
12559 == bfd_target_elf_flavour)
12560 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12561 != bed->s->elfclass))
12562 {
12563 const char *iclass, *oclass;
12564
12565 switch (bed->s->elfclass)
12566 {
12567 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12568 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12569 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12570 default: abort ();
12571 }
12572
12573 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12574 {
12575 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12576 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12577 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12578 default: abort ();
12579 }
12580
12581 bfd_set_error (bfd_error_wrong_format);
12582 _bfd_error_handler
12583 /* xgettext:c-format */
12584 (_("%pB: file class %s incompatible with %s"),
12585 sub, iclass, oclass);
12586 }
12587
12588 goto error_return;
12589 }
12590 }
12591 }
12592 }
12593
12594 /* Free symbol buffer if needed. */
12595 if (!info->reduce_memory_overheads)
12596 {
12597 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12598 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12599 {
12600 free (elf_tdata (sub)->symbuf);
12601 elf_tdata (sub)->symbuf = NULL;
12602 }
12603 }
12604
12605 ret = TRUE;
12606
12607 /* Output any global symbols that got converted to local in a
12608 version script or due to symbol visibility. We do this in a
12609 separate step since ELF requires all local symbols to appear
12610 prior to any global symbols. FIXME: We should only do this if
12611 some global symbols were, in fact, converted to become local.
12612 FIXME: Will this work correctly with the Irix 5 linker? */
12613 eoinfo.failed = FALSE;
12614 eoinfo.flinfo = &flinfo;
12615 eoinfo.localsyms = TRUE;
12616 eoinfo.file_sym_done = FALSE;
12617 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12618 if (eoinfo.failed)
12619 {
12620 ret = FALSE;
12621 goto return_local_hash_table;
12622 }
12623
12624 /* If backend needs to output some local symbols not present in the hash
12625 table, do it now. */
12626 if (bed->elf_backend_output_arch_local_syms
12627 && (info->strip != strip_all || emit_relocs))
12628 {
12629 typedef int (*out_sym_func)
12630 (void *, const char *, Elf_Internal_Sym *, asection *,
12631 struct elf_link_hash_entry *);
12632
12633 if (! ((*bed->elf_backend_output_arch_local_syms)
12634 (abfd, info, &flinfo,
12635 (out_sym_func) elf_link_output_symstrtab)))
12636 {
12637 ret = FALSE;
12638 goto return_local_hash_table;
12639 }
12640 }
12641
12642 /* That wrote out all the local symbols. Finish up the symbol table
12643 with the global symbols. Even if we want to strip everything we
12644 can, we still need to deal with those global symbols that got
12645 converted to local in a version script. */
12646
12647 /* The sh_info field records the index of the first non local symbol. */
12648 if (!symtab_hdr->sh_info)
12649 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12650
12651 if (dynamic
12652 && htab->dynsym != NULL
12653 && htab->dynsym->output_section != bfd_abs_section_ptr)
12654 {
12655 Elf_Internal_Sym sym;
12656 bfd_byte *dynsym = htab->dynsym->contents;
12657
12658 o = htab->dynsym->output_section;
12659 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12660
12661 /* Write out the section symbols for the output sections. */
12662 if (bfd_link_pic (info)
12663 || htab->is_relocatable_executable)
12664 {
12665 asection *s;
12666
12667 sym.st_size = 0;
12668 sym.st_name = 0;
12669 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12670 sym.st_other = 0;
12671 sym.st_target_internal = 0;
12672
12673 for (s = abfd->sections; s != NULL; s = s->next)
12674 {
12675 int indx;
12676 bfd_byte *dest;
12677 long dynindx;
12678
12679 dynindx = elf_section_data (s)->dynindx;
12680 if (dynindx <= 0)
12681 continue;
12682 indx = elf_section_data (s)->this_idx;
12683 BFD_ASSERT (indx > 0);
12684 sym.st_shndx = indx;
12685 if (! check_dynsym (abfd, &sym))
12686 {
12687 ret = FALSE;
12688 goto return_local_hash_table;
12689 }
12690 sym.st_value = s->vma;
12691 dest = dynsym + dynindx * bed->s->sizeof_sym;
12692
12693 /* Inform the linker of the addition of this symbol. */
12694
12695 if (info->callbacks->ctf_new_dynsym)
12696 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12697
12698 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12699 }
12700 }
12701
12702 /* Write out the local dynsyms. */
12703 if (htab->dynlocal)
12704 {
12705 struct elf_link_local_dynamic_entry *e;
12706 for (e = htab->dynlocal; e ; e = e->next)
12707 {
12708 asection *s;
12709 bfd_byte *dest;
12710
12711 /* Copy the internal symbol and turn off visibility.
12712 Note that we saved a word of storage and overwrote
12713 the original st_name with the dynstr_index. */
12714 sym = e->isym;
12715 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12716 sym.st_shndx = SHN_UNDEF;
12717
12718 s = bfd_section_from_elf_index (e->input_bfd,
12719 e->isym.st_shndx);
12720 if (s != NULL
12721 && s->output_section != NULL
12722 && elf_section_data (s->output_section) != NULL)
12723 {
12724 sym.st_shndx =
12725 elf_section_data (s->output_section)->this_idx;
12726 if (! check_dynsym (abfd, &sym))
12727 {
12728 ret = FALSE;
12729 goto return_local_hash_table;
12730 }
12731 sym.st_value = (s->output_section->vma
12732 + s->output_offset
12733 + e->isym.st_value);
12734 }
12735
12736 /* Inform the linker of the addition of this symbol. */
12737
12738 if (info->callbacks->ctf_new_dynsym)
12739 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12740
12741 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12742 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12743 }
12744 }
12745 }
12746
12747 /* We get the global symbols from the hash table. */
12748 eoinfo.failed = FALSE;
12749 eoinfo.localsyms = FALSE;
12750 eoinfo.flinfo = &flinfo;
12751 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12752 if (eoinfo.failed)
12753 {
12754 ret = FALSE;
12755 goto return_local_hash_table;
12756 }
12757
12758 /* If backend needs to output some symbols not present in the hash
12759 table, do it now. */
12760 if (bed->elf_backend_output_arch_syms
12761 && (info->strip != strip_all || emit_relocs))
12762 {
12763 typedef int (*out_sym_func)
12764 (void *, const char *, Elf_Internal_Sym *, asection *,
12765 struct elf_link_hash_entry *);
12766
12767 if (! ((*bed->elf_backend_output_arch_syms)
12768 (abfd, info, &flinfo,
12769 (out_sym_func) elf_link_output_symstrtab)))
12770 {
12771 ret = FALSE;
12772 goto return_local_hash_table;
12773 }
12774 }
12775
12776 /* Finalize the .strtab section. */
12777 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12778
12779 /* Swap out the .strtab section. */
12780 if (!elf_link_swap_symbols_out (&flinfo))
12781 {
12782 ret = FALSE;
12783 goto return_local_hash_table;
12784 }
12785
12786 /* Now we know the size of the symtab section. */
12787 if (bfd_get_symcount (abfd) > 0)
12788 {
12789 /* Finish up and write out the symbol string table (.strtab)
12790 section. */
12791 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12792 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12793
12794 if (elf_symtab_shndx_list (abfd))
12795 {
12796 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12797
12798 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12799 {
12800 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12801 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12802 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12803 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12804 symtab_shndx_hdr->sh_size = amt;
12805
12806 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12807 off, TRUE);
12808
12809 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12810 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12811 {
12812 ret = FALSE;
12813 goto return_local_hash_table;
12814 }
12815 }
12816 }
12817
12818 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12819 /* sh_name was set in prep_headers. */
12820 symstrtab_hdr->sh_type = SHT_STRTAB;
12821 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12822 symstrtab_hdr->sh_addr = 0;
12823 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12824 symstrtab_hdr->sh_entsize = 0;
12825 symstrtab_hdr->sh_link = 0;
12826 symstrtab_hdr->sh_info = 0;
12827 /* sh_offset is set just below. */
12828 symstrtab_hdr->sh_addralign = 1;
12829
12830 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12831 off, TRUE);
12832 elf_next_file_pos (abfd) = off;
12833
12834 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12835 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12836 {
12837 ret = FALSE;
12838 goto return_local_hash_table;
12839 }
12840 }
12841
12842 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12843 {
12844 _bfd_error_handler (_("%pB: failed to generate import library"),
12845 info->out_implib_bfd);
12846 ret = FALSE;
12847 goto return_local_hash_table;
12848 }
12849
12850 /* Adjust the relocs to have the correct symbol indices. */
12851 for (o = abfd->sections; o != NULL; o = o->next)
12852 {
12853 struct bfd_elf_section_data *esdo = elf_section_data (o);
12854 bfd_boolean sort;
12855
12856 if ((o->flags & SEC_RELOC) == 0)
12857 continue;
12858
12859 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12860 if (esdo->rel.hdr != NULL
12861 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12862 {
12863 ret = FALSE;
12864 goto return_local_hash_table;
12865 }
12866 if (esdo->rela.hdr != NULL
12867 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12868 {
12869 ret = FALSE;
12870 goto return_local_hash_table;
12871 }
12872
12873 /* Set the reloc_count field to 0 to prevent write_relocs from
12874 trying to swap the relocs out itself. */
12875 o->reloc_count = 0;
12876 }
12877
12878 if (dynamic && info->combreloc && dynobj != NULL)
12879 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12880
12881 /* If we are linking against a dynamic object, or generating a
12882 shared library, finish up the dynamic linking information. */
12883 if (dynamic)
12884 {
12885 bfd_byte *dyncon, *dynconend;
12886
12887 /* Fix up .dynamic entries. */
12888 o = bfd_get_linker_section (dynobj, ".dynamic");
12889 BFD_ASSERT (o != NULL);
12890
12891 dyncon = o->contents;
12892 dynconend = o->contents + o->size;
12893 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12894 {
12895 Elf_Internal_Dyn dyn;
12896 const char *name;
12897 unsigned int type;
12898 bfd_size_type sh_size;
12899 bfd_vma sh_addr;
12900
12901 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12902
12903 switch (dyn.d_tag)
12904 {
12905 default:
12906 continue;
12907 case DT_NULL:
12908 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12909 {
12910 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12911 {
12912 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12913 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12914 default: continue;
12915 }
12916 dyn.d_un.d_val = relativecount;
12917 relativecount = 0;
12918 break;
12919 }
12920 continue;
12921
12922 case DT_INIT:
12923 name = info->init_function;
12924 goto get_sym;
12925 case DT_FINI:
12926 name = info->fini_function;
12927 get_sym:
12928 {
12929 struct elf_link_hash_entry *h;
12930
12931 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12932 if (h != NULL
12933 && (h->root.type == bfd_link_hash_defined
12934 || h->root.type == bfd_link_hash_defweak))
12935 {
12936 dyn.d_un.d_ptr = h->root.u.def.value;
12937 o = h->root.u.def.section;
12938 if (o->output_section != NULL)
12939 dyn.d_un.d_ptr += (o->output_section->vma
12940 + o->output_offset);
12941 else
12942 {
12943 /* The symbol is imported from another shared
12944 library and does not apply to this one. */
12945 dyn.d_un.d_ptr = 0;
12946 }
12947 break;
12948 }
12949 }
12950 continue;
12951
12952 case DT_PREINIT_ARRAYSZ:
12953 name = ".preinit_array";
12954 goto get_out_size;
12955 case DT_INIT_ARRAYSZ:
12956 name = ".init_array";
12957 goto get_out_size;
12958 case DT_FINI_ARRAYSZ:
12959 name = ".fini_array";
12960 get_out_size:
12961 o = bfd_get_section_by_name (abfd, name);
12962 if (o == NULL)
12963 {
12964 _bfd_error_handler
12965 (_("could not find section %s"), name);
12966 goto error_return;
12967 }
12968 if (o->size == 0)
12969 _bfd_error_handler
12970 (_("warning: %s section has zero size"), name);
12971 dyn.d_un.d_val = o->size;
12972 break;
12973
12974 case DT_PREINIT_ARRAY:
12975 name = ".preinit_array";
12976 goto get_out_vma;
12977 case DT_INIT_ARRAY:
12978 name = ".init_array";
12979 goto get_out_vma;
12980 case DT_FINI_ARRAY:
12981 name = ".fini_array";
12982 get_out_vma:
12983 o = bfd_get_section_by_name (abfd, name);
12984 goto do_vma;
12985
12986 case DT_HASH:
12987 name = ".hash";
12988 goto get_vma;
12989 case DT_GNU_HASH:
12990 name = ".gnu.hash";
12991 goto get_vma;
12992 case DT_STRTAB:
12993 name = ".dynstr";
12994 goto get_vma;
12995 case DT_SYMTAB:
12996 name = ".dynsym";
12997 goto get_vma;
12998 case DT_VERDEF:
12999 name = ".gnu.version_d";
13000 goto get_vma;
13001 case DT_VERNEED:
13002 name = ".gnu.version_r";
13003 goto get_vma;
13004 case DT_VERSYM:
13005 name = ".gnu.version";
13006 get_vma:
13007 o = bfd_get_linker_section (dynobj, name);
13008 do_vma:
13009 if (o == NULL || bfd_is_abs_section (o->output_section))
13010 {
13011 _bfd_error_handler
13012 (_("could not find section %s"), name);
13013 goto error_return;
13014 }
13015 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13016 {
13017 _bfd_error_handler
13018 (_("warning: section '%s' is being made into a note"), name);
13019 bfd_set_error (bfd_error_nonrepresentable_section);
13020 goto error_return;
13021 }
13022 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13023 break;
13024
13025 case DT_REL:
13026 case DT_RELA:
13027 case DT_RELSZ:
13028 case DT_RELASZ:
13029 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13030 type = SHT_REL;
13031 else
13032 type = SHT_RELA;
13033 sh_size = 0;
13034 sh_addr = 0;
13035 for (i = 1; i < elf_numsections (abfd); i++)
13036 {
13037 Elf_Internal_Shdr *hdr;
13038
13039 hdr = elf_elfsections (abfd)[i];
13040 if (hdr->sh_type == type
13041 && (hdr->sh_flags & SHF_ALLOC) != 0)
13042 {
13043 sh_size += hdr->sh_size;
13044 if (sh_addr == 0
13045 || sh_addr > hdr->sh_addr)
13046 sh_addr = hdr->sh_addr;
13047 }
13048 }
13049
13050 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13051 {
13052 unsigned int opb = bfd_octets_per_byte (abfd, o);
13053
13054 /* Don't count procedure linkage table relocs in the
13055 overall reloc count. */
13056 sh_size -= htab->srelplt->size;
13057 if (sh_size == 0)
13058 /* If the size is zero, make the address zero too.
13059 This is to avoid a glibc bug. If the backend
13060 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13061 zero, then we'll put DT_RELA at the end of
13062 DT_JMPREL. glibc will interpret the end of
13063 DT_RELA matching the end of DT_JMPREL as the
13064 case where DT_RELA includes DT_JMPREL, and for
13065 LD_BIND_NOW will decide that processing DT_RELA
13066 will process the PLT relocs too. Net result:
13067 No PLT relocs applied. */
13068 sh_addr = 0;
13069
13070 /* If .rela.plt is the first .rela section, exclude
13071 it from DT_RELA. */
13072 else if (sh_addr == (htab->srelplt->output_section->vma
13073 + htab->srelplt->output_offset) * opb)
13074 sh_addr += htab->srelplt->size;
13075 }
13076
13077 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13078 dyn.d_un.d_val = sh_size;
13079 else
13080 dyn.d_un.d_ptr = sh_addr;
13081 break;
13082 }
13083 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13084 }
13085 }
13086
13087 /* If we have created any dynamic sections, then output them. */
13088 if (dynobj != NULL)
13089 {
13090 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13091 goto error_return;
13092
13093 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13094 if (bfd_link_textrel_check (info)
13095 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13096 {
13097 bfd_byte *dyncon, *dynconend;
13098
13099 dyncon = o->contents;
13100 dynconend = o->contents + o->size;
13101 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13102 {
13103 Elf_Internal_Dyn dyn;
13104
13105 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13106
13107 if (dyn.d_tag == DT_TEXTREL)
13108 {
13109 if (info->textrel_check == textrel_check_error)
13110 info->callbacks->einfo
13111 (_("%P%X: read-only segment has dynamic relocations\n"));
13112 else if (bfd_link_dll (info))
13113 info->callbacks->einfo
13114 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13115 else
13116 info->callbacks->einfo
13117 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13118 break;
13119 }
13120 }
13121 }
13122
13123 for (o = dynobj->sections; o != NULL; o = o->next)
13124 {
13125 if ((o->flags & SEC_HAS_CONTENTS) == 0
13126 || o->size == 0
13127 || o->output_section == bfd_abs_section_ptr)
13128 continue;
13129 if ((o->flags & SEC_LINKER_CREATED) == 0)
13130 {
13131 /* At this point, we are only interested in sections
13132 created by _bfd_elf_link_create_dynamic_sections. */
13133 continue;
13134 }
13135 if (htab->stab_info.stabstr == o)
13136 continue;
13137 if (htab->eh_info.hdr_sec == o)
13138 continue;
13139 if (strcmp (o->name, ".dynstr") != 0)
13140 {
13141 bfd_size_type octets = ((file_ptr) o->output_offset
13142 * bfd_octets_per_byte (abfd, o));
13143 if (!bfd_set_section_contents (abfd, o->output_section,
13144 o->contents, octets, o->size))
13145 goto error_return;
13146 }
13147 else
13148 {
13149 /* The contents of the .dynstr section are actually in a
13150 stringtab. */
13151 file_ptr off;
13152
13153 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13154 if (bfd_seek (abfd, off, SEEK_SET) != 0
13155 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13156 goto error_return;
13157 }
13158 }
13159 }
13160
13161 if (!info->resolve_section_groups)
13162 {
13163 bfd_boolean failed = FALSE;
13164
13165 BFD_ASSERT (bfd_link_relocatable (info));
13166 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13167 if (failed)
13168 goto error_return;
13169 }
13170
13171 /* If we have optimized stabs strings, output them. */
13172 if (htab->stab_info.stabstr != NULL)
13173 {
13174 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13175 goto error_return;
13176 }
13177
13178 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13179 goto error_return;
13180
13181 if (info->callbacks->emit_ctf)
13182 info->callbacks->emit_ctf ();
13183
13184 elf_final_link_free (abfd, &flinfo);
13185
13186 if (attr_section)
13187 {
13188 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13189 if (contents == NULL)
13190 {
13191 /* Bail out and fail. */
13192 ret = FALSE;
13193 goto return_local_hash_table;
13194 }
13195 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13196 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13197 free (contents);
13198 }
13199
13200 return_local_hash_table:
13201 if (info->unique_symbol)
13202 bfd_hash_table_free (&flinfo.local_hash_table);
13203 return ret;
13204
13205 error_return:
13206 elf_final_link_free (abfd, &flinfo);
13207 ret = FALSE;
13208 goto return_local_hash_table;
13209 }
13210 \f
13211 /* Initialize COOKIE for input bfd ABFD. */
13212
13213 static bfd_boolean
13214 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13215 struct bfd_link_info *info, bfd *abfd)
13216 {
13217 Elf_Internal_Shdr *symtab_hdr;
13218 const struct elf_backend_data *bed;
13219
13220 bed = get_elf_backend_data (abfd);
13221 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13222
13223 cookie->abfd = abfd;
13224 cookie->sym_hashes = elf_sym_hashes (abfd);
13225 cookie->bad_symtab = elf_bad_symtab (abfd);
13226 if (cookie->bad_symtab)
13227 {
13228 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13229 cookie->extsymoff = 0;
13230 }
13231 else
13232 {
13233 cookie->locsymcount = symtab_hdr->sh_info;
13234 cookie->extsymoff = symtab_hdr->sh_info;
13235 }
13236
13237 if (bed->s->arch_size == 32)
13238 cookie->r_sym_shift = 8;
13239 else
13240 cookie->r_sym_shift = 32;
13241
13242 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13243 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13244 {
13245 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13246 cookie->locsymcount, 0,
13247 NULL, NULL, NULL);
13248 if (cookie->locsyms == NULL)
13249 {
13250 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13251 return FALSE;
13252 }
13253 if (info->keep_memory)
13254 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13255 }
13256 return TRUE;
13257 }
13258
13259 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13260
13261 static void
13262 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13263 {
13264 Elf_Internal_Shdr *symtab_hdr;
13265
13266 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13267 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13268 free (cookie->locsyms);
13269 }
13270
13271 /* Initialize the relocation information in COOKIE for input section SEC
13272 of input bfd ABFD. */
13273
13274 static bfd_boolean
13275 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13276 struct bfd_link_info *info, bfd *abfd,
13277 asection *sec)
13278 {
13279 if (sec->reloc_count == 0)
13280 {
13281 cookie->rels = NULL;
13282 cookie->relend = NULL;
13283 }
13284 else
13285 {
13286 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13287 info->keep_memory);
13288 if (cookie->rels == NULL)
13289 return FALSE;
13290 cookie->rel = cookie->rels;
13291 cookie->relend = cookie->rels + sec->reloc_count;
13292 }
13293 cookie->rel = cookie->rels;
13294 return TRUE;
13295 }
13296
13297 /* Free the memory allocated by init_reloc_cookie_rels,
13298 if appropriate. */
13299
13300 static void
13301 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13302 asection *sec)
13303 {
13304 if (elf_section_data (sec)->relocs != cookie->rels)
13305 free (cookie->rels);
13306 }
13307
13308 /* Initialize the whole of COOKIE for input section SEC. */
13309
13310 static bfd_boolean
13311 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13312 struct bfd_link_info *info,
13313 asection *sec)
13314 {
13315 if (!init_reloc_cookie (cookie, info, sec->owner))
13316 goto error1;
13317 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13318 goto error2;
13319 return TRUE;
13320
13321 error2:
13322 fini_reloc_cookie (cookie, sec->owner);
13323 error1:
13324 return FALSE;
13325 }
13326
13327 /* Free the memory allocated by init_reloc_cookie_for_section,
13328 if appropriate. */
13329
13330 static void
13331 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13332 asection *sec)
13333 {
13334 fini_reloc_cookie_rels (cookie, sec);
13335 fini_reloc_cookie (cookie, sec->owner);
13336 }
13337 \f
13338 /* Garbage collect unused sections. */
13339
13340 /* Default gc_mark_hook. */
13341
13342 asection *
13343 _bfd_elf_gc_mark_hook (asection *sec,
13344 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13345 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13346 struct elf_link_hash_entry *h,
13347 Elf_Internal_Sym *sym)
13348 {
13349 if (h != NULL)
13350 {
13351 switch (h->root.type)
13352 {
13353 case bfd_link_hash_defined:
13354 case bfd_link_hash_defweak:
13355 return h->root.u.def.section;
13356
13357 case bfd_link_hash_common:
13358 return h->root.u.c.p->section;
13359
13360 default:
13361 break;
13362 }
13363 }
13364 else
13365 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13366
13367 return NULL;
13368 }
13369
13370 /* Return the debug definition section. */
13371
13372 static asection *
13373 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13374 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13375 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13376 struct elf_link_hash_entry *h,
13377 Elf_Internal_Sym *sym)
13378 {
13379 if (h != NULL)
13380 {
13381 /* Return the global debug definition section. */
13382 if ((h->root.type == bfd_link_hash_defined
13383 || h->root.type == bfd_link_hash_defweak)
13384 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13385 return h->root.u.def.section;
13386 }
13387 else
13388 {
13389 /* Return the local debug definition section. */
13390 asection *isec = bfd_section_from_elf_index (sec->owner,
13391 sym->st_shndx);
13392 if ((isec->flags & SEC_DEBUGGING) != 0)
13393 return isec;
13394 }
13395
13396 return NULL;
13397 }
13398
13399 /* COOKIE->rel describes a relocation against section SEC, which is
13400 a section we've decided to keep. Return the section that contains
13401 the relocation symbol, or NULL if no section contains it. */
13402
13403 asection *
13404 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13405 elf_gc_mark_hook_fn gc_mark_hook,
13406 struct elf_reloc_cookie *cookie,
13407 bfd_boolean *start_stop)
13408 {
13409 unsigned long r_symndx;
13410 struct elf_link_hash_entry *h, *hw;
13411
13412 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13413 if (r_symndx == STN_UNDEF)
13414 return NULL;
13415
13416 if (r_symndx >= cookie->locsymcount
13417 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13418 {
13419 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13420 if (h == NULL)
13421 {
13422 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13423 sec->owner);
13424 return NULL;
13425 }
13426 while (h->root.type == bfd_link_hash_indirect
13427 || h->root.type == bfd_link_hash_warning)
13428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13429 h->mark = 1;
13430 /* Keep all aliases of the symbol too. If an object symbol
13431 needs to be copied into .dynbss then all of its aliases
13432 should be present as dynamic symbols, not just the one used
13433 on the copy relocation. */
13434 hw = h;
13435 while (hw->is_weakalias)
13436 {
13437 hw = hw->u.alias;
13438 hw->mark = 1;
13439 }
13440
13441 if (start_stop != NULL)
13442 {
13443 /* To work around a glibc bug, mark XXX input sections
13444 when there is a reference to __start_XXX or __stop_XXX
13445 symbols. */
13446 if (h->start_stop)
13447 {
13448 asection *s = h->u2.start_stop_section;
13449 *start_stop = !s->gc_mark;
13450 return s;
13451 }
13452 }
13453
13454 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13455 }
13456
13457 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13458 &cookie->locsyms[r_symndx]);
13459 }
13460
13461 /* COOKIE->rel describes a relocation against section SEC, which is
13462 a section we've decided to keep. Mark the section that contains
13463 the relocation symbol. */
13464
13465 bfd_boolean
13466 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13467 asection *sec,
13468 elf_gc_mark_hook_fn gc_mark_hook,
13469 struct elf_reloc_cookie *cookie)
13470 {
13471 asection *rsec;
13472 bfd_boolean start_stop = FALSE;
13473
13474 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13475 while (rsec != NULL)
13476 {
13477 if (!rsec->gc_mark)
13478 {
13479 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13480 || (rsec->owner->flags & DYNAMIC) != 0)
13481 rsec->gc_mark = 1;
13482 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13483 return FALSE;
13484 }
13485 if (!start_stop)
13486 break;
13487 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13488 }
13489 return TRUE;
13490 }
13491
13492 /* The mark phase of garbage collection. For a given section, mark
13493 it and any sections in this section's group, and all the sections
13494 which define symbols to which it refers. */
13495
13496 bfd_boolean
13497 _bfd_elf_gc_mark (struct bfd_link_info *info,
13498 asection *sec,
13499 elf_gc_mark_hook_fn gc_mark_hook)
13500 {
13501 bfd_boolean ret;
13502 asection *group_sec, *eh_frame;
13503
13504 sec->gc_mark = 1;
13505
13506 /* Mark all the sections in the group. */
13507 group_sec = elf_section_data (sec)->next_in_group;
13508 if (group_sec && !group_sec->gc_mark)
13509 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13510 return FALSE;
13511
13512 /* Look through the section relocs. */
13513 ret = TRUE;
13514 eh_frame = elf_eh_frame_section (sec->owner);
13515 if ((sec->flags & SEC_RELOC) != 0
13516 && sec->reloc_count > 0
13517 && sec != eh_frame)
13518 {
13519 struct elf_reloc_cookie cookie;
13520
13521 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13522 ret = FALSE;
13523 else
13524 {
13525 for (; cookie.rel < cookie.relend; cookie.rel++)
13526 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13527 {
13528 ret = FALSE;
13529 break;
13530 }
13531 fini_reloc_cookie_for_section (&cookie, sec);
13532 }
13533 }
13534
13535 if (ret && eh_frame && elf_fde_list (sec))
13536 {
13537 struct elf_reloc_cookie cookie;
13538
13539 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13540 ret = FALSE;
13541 else
13542 {
13543 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13544 gc_mark_hook, &cookie))
13545 ret = FALSE;
13546 fini_reloc_cookie_for_section (&cookie, eh_frame);
13547 }
13548 }
13549
13550 eh_frame = elf_section_eh_frame_entry (sec);
13551 if (ret && eh_frame && !eh_frame->gc_mark)
13552 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13553 ret = FALSE;
13554
13555 return ret;
13556 }
13557
13558 /* Scan and mark sections in a special or debug section group. */
13559
13560 static void
13561 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13562 {
13563 /* Point to first section of section group. */
13564 asection *ssec;
13565 /* Used to iterate the section group. */
13566 asection *msec;
13567
13568 bfd_boolean is_special_grp = TRUE;
13569 bfd_boolean is_debug_grp = TRUE;
13570
13571 /* First scan to see if group contains any section other than debug
13572 and special section. */
13573 ssec = msec = elf_next_in_group (grp);
13574 do
13575 {
13576 if ((msec->flags & SEC_DEBUGGING) == 0)
13577 is_debug_grp = FALSE;
13578
13579 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13580 is_special_grp = FALSE;
13581
13582 msec = elf_next_in_group (msec);
13583 }
13584 while (msec != ssec);
13585
13586 /* If this is a pure debug section group or pure special section group,
13587 keep all sections in this group. */
13588 if (is_debug_grp || is_special_grp)
13589 {
13590 do
13591 {
13592 msec->gc_mark = 1;
13593 msec = elf_next_in_group (msec);
13594 }
13595 while (msec != ssec);
13596 }
13597 }
13598
13599 /* Keep debug and special sections. */
13600
13601 bfd_boolean
13602 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13603 elf_gc_mark_hook_fn mark_hook)
13604 {
13605 bfd *ibfd;
13606
13607 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13608 {
13609 asection *isec;
13610 bfd_boolean some_kept;
13611 bfd_boolean debug_frag_seen;
13612 bfd_boolean has_kept_debug_info;
13613
13614 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13615 continue;
13616 isec = ibfd->sections;
13617 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13618 continue;
13619
13620 /* Ensure all linker created sections are kept,
13621 see if any other section is already marked,
13622 and note if we have any fragmented debug sections. */
13623 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13624 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13625 {
13626 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13627 isec->gc_mark = 1;
13628 else if (isec->gc_mark
13629 && (isec->flags & SEC_ALLOC) != 0
13630 && elf_section_type (isec) != SHT_NOTE)
13631 some_kept = TRUE;
13632 else
13633 {
13634 /* Since all sections, except for backend specific ones,
13635 have been garbage collected, call mark_hook on this
13636 section if any of its linked-to sections is marked. */
13637 asection *linked_to_sec;
13638 for (linked_to_sec = elf_linked_to_section (isec);
13639 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13640 linked_to_sec = elf_linked_to_section (linked_to_sec))
13641 {
13642 if (linked_to_sec->gc_mark)
13643 {
13644 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13645 return FALSE;
13646 break;
13647 }
13648 linked_to_sec->linker_mark = 1;
13649 }
13650 for (linked_to_sec = elf_linked_to_section (isec);
13651 linked_to_sec != NULL && linked_to_sec->linker_mark;
13652 linked_to_sec = elf_linked_to_section (linked_to_sec))
13653 linked_to_sec->linker_mark = 0;
13654 }
13655
13656 if (!debug_frag_seen
13657 && (isec->flags & SEC_DEBUGGING)
13658 && CONST_STRNEQ (isec->name, ".debug_line."))
13659 debug_frag_seen = TRUE;
13660 else if (strcmp (bfd_section_name (isec),
13661 "__patchable_function_entries") == 0
13662 && elf_linked_to_section (isec) == NULL)
13663 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13664 "need linked-to section "
13665 "for --gc-sections\n"),
13666 isec->owner, isec);
13667 }
13668
13669 /* If no non-note alloc section in this file will be kept, then
13670 we can toss out the debug and special sections. */
13671 if (!some_kept)
13672 continue;
13673
13674 /* Keep debug and special sections like .comment when they are
13675 not part of a group. Also keep section groups that contain
13676 just debug sections or special sections. NB: Sections with
13677 linked-to section has been handled above. */
13678 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13679 {
13680 if ((isec->flags & SEC_GROUP) != 0)
13681 _bfd_elf_gc_mark_debug_special_section_group (isec);
13682 else if (((isec->flags & SEC_DEBUGGING) != 0
13683 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13684 && elf_next_in_group (isec) == NULL
13685 && elf_linked_to_section (isec) == NULL)
13686 isec->gc_mark = 1;
13687 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13688 has_kept_debug_info = TRUE;
13689 }
13690
13691 /* Look for CODE sections which are going to be discarded,
13692 and find and discard any fragmented debug sections which
13693 are associated with that code section. */
13694 if (debug_frag_seen)
13695 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13696 if ((isec->flags & SEC_CODE) != 0
13697 && isec->gc_mark == 0)
13698 {
13699 unsigned int ilen;
13700 asection *dsec;
13701
13702 ilen = strlen (isec->name);
13703
13704 /* Association is determined by the name of the debug
13705 section containing the name of the code section as
13706 a suffix. For example .debug_line.text.foo is a
13707 debug section associated with .text.foo. */
13708 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13709 {
13710 unsigned int dlen;
13711
13712 if (dsec->gc_mark == 0
13713 || (dsec->flags & SEC_DEBUGGING) == 0)
13714 continue;
13715
13716 dlen = strlen (dsec->name);
13717
13718 if (dlen > ilen
13719 && strncmp (dsec->name + (dlen - ilen),
13720 isec->name, ilen) == 0)
13721 dsec->gc_mark = 0;
13722 }
13723 }
13724
13725 /* Mark debug sections referenced by kept debug sections. */
13726 if (has_kept_debug_info)
13727 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13728 if (isec->gc_mark
13729 && (isec->flags & SEC_DEBUGGING) != 0)
13730 if (!_bfd_elf_gc_mark (info, isec,
13731 elf_gc_mark_debug_section))
13732 return FALSE;
13733 }
13734 return TRUE;
13735 }
13736
13737 static bfd_boolean
13738 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13739 {
13740 bfd *sub;
13741 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13742
13743 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13744 {
13745 asection *o;
13746
13747 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13748 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13749 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13750 continue;
13751 o = sub->sections;
13752 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13753 continue;
13754
13755 for (o = sub->sections; o != NULL; o = o->next)
13756 {
13757 /* When any section in a section group is kept, we keep all
13758 sections in the section group. If the first member of
13759 the section group is excluded, we will also exclude the
13760 group section. */
13761 if (o->flags & SEC_GROUP)
13762 {
13763 asection *first = elf_next_in_group (o);
13764 o->gc_mark = first->gc_mark;
13765 }
13766
13767 if (o->gc_mark)
13768 continue;
13769
13770 /* Skip sweeping sections already excluded. */
13771 if (o->flags & SEC_EXCLUDE)
13772 continue;
13773
13774 /* Since this is early in the link process, it is simple
13775 to remove a section from the output. */
13776 o->flags |= SEC_EXCLUDE;
13777
13778 if (info->print_gc_sections && o->size != 0)
13779 /* xgettext:c-format */
13780 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13781 o, sub);
13782 }
13783 }
13784
13785 return TRUE;
13786 }
13787
13788 /* Propagate collected vtable information. This is called through
13789 elf_link_hash_traverse. */
13790
13791 static bfd_boolean
13792 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13793 {
13794 /* Those that are not vtables. */
13795 if (h->start_stop
13796 || h->u2.vtable == NULL
13797 || h->u2.vtable->parent == NULL)
13798 return TRUE;
13799
13800 /* Those vtables that do not have parents, we cannot merge. */
13801 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13802 return TRUE;
13803
13804 /* If we've already been done, exit. */
13805 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13806 return TRUE;
13807
13808 /* Make sure the parent's table is up to date. */
13809 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13810
13811 if (h->u2.vtable->used == NULL)
13812 {
13813 /* None of this table's entries were referenced. Re-use the
13814 parent's table. */
13815 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13816 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13817 }
13818 else
13819 {
13820 size_t n;
13821 bfd_boolean *cu, *pu;
13822
13823 /* Or the parent's entries into ours. */
13824 cu = h->u2.vtable->used;
13825 cu[-1] = TRUE;
13826 pu = h->u2.vtable->parent->u2.vtable->used;
13827 if (pu != NULL)
13828 {
13829 const struct elf_backend_data *bed;
13830 unsigned int log_file_align;
13831
13832 bed = get_elf_backend_data (h->root.u.def.section->owner);
13833 log_file_align = bed->s->log_file_align;
13834 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13835 while (n--)
13836 {
13837 if (*pu)
13838 *cu = TRUE;
13839 pu++;
13840 cu++;
13841 }
13842 }
13843 }
13844
13845 return TRUE;
13846 }
13847
13848 static bfd_boolean
13849 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13850 {
13851 asection *sec;
13852 bfd_vma hstart, hend;
13853 Elf_Internal_Rela *relstart, *relend, *rel;
13854 const struct elf_backend_data *bed;
13855 unsigned int log_file_align;
13856
13857 /* Take care of both those symbols that do not describe vtables as
13858 well as those that are not loaded. */
13859 if (h->start_stop
13860 || h->u2.vtable == NULL
13861 || h->u2.vtable->parent == NULL)
13862 return TRUE;
13863
13864 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13865 || h->root.type == bfd_link_hash_defweak);
13866
13867 sec = h->root.u.def.section;
13868 hstart = h->root.u.def.value;
13869 hend = hstart + h->size;
13870
13871 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13872 if (!relstart)
13873 return *(bfd_boolean *) okp = FALSE;
13874 bed = get_elf_backend_data (sec->owner);
13875 log_file_align = bed->s->log_file_align;
13876
13877 relend = relstart + sec->reloc_count;
13878
13879 for (rel = relstart; rel < relend; ++rel)
13880 if (rel->r_offset >= hstart && rel->r_offset < hend)
13881 {
13882 /* If the entry is in use, do nothing. */
13883 if (h->u2.vtable->used
13884 && (rel->r_offset - hstart) < h->u2.vtable->size)
13885 {
13886 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13887 if (h->u2.vtable->used[entry])
13888 continue;
13889 }
13890 /* Otherwise, kill it. */
13891 rel->r_offset = rel->r_info = rel->r_addend = 0;
13892 }
13893
13894 return TRUE;
13895 }
13896
13897 /* Mark sections containing dynamically referenced symbols. When
13898 building shared libraries, we must assume that any visible symbol is
13899 referenced. */
13900
13901 bfd_boolean
13902 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13903 {
13904 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13905 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13906
13907 if ((h->root.type == bfd_link_hash_defined
13908 || h->root.type == bfd_link_hash_defweak)
13909 && ((h->ref_dynamic && !h->forced_local)
13910 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13911 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13912 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13913 && (!bfd_link_executable (info)
13914 || info->gc_keep_exported
13915 || info->export_dynamic
13916 || (h->dynamic
13917 && d != NULL
13918 && (*d->match) (&d->head, NULL, h->root.root.string)))
13919 && (h->versioned >= versioned
13920 || !bfd_hide_sym_by_version (info->version_info,
13921 h->root.root.string)))))
13922 h->root.u.def.section->flags |= SEC_KEEP;
13923
13924 return TRUE;
13925 }
13926
13927 /* Keep all sections containing symbols undefined on the command-line,
13928 and the section containing the entry symbol. */
13929
13930 void
13931 _bfd_elf_gc_keep (struct bfd_link_info *info)
13932 {
13933 struct bfd_sym_chain *sym;
13934
13935 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13936 {
13937 struct elf_link_hash_entry *h;
13938
13939 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13940 FALSE, FALSE, FALSE);
13941
13942 if (h != NULL
13943 && (h->root.type == bfd_link_hash_defined
13944 || h->root.type == bfd_link_hash_defweak)
13945 && !bfd_is_const_section (h->root.u.def.section))
13946 h->root.u.def.section->flags |= SEC_KEEP;
13947 }
13948 }
13949
13950 bfd_boolean
13951 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13952 struct bfd_link_info *info)
13953 {
13954 bfd *ibfd = info->input_bfds;
13955
13956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13957 {
13958 asection *sec;
13959 struct elf_reloc_cookie cookie;
13960
13961 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13962 continue;
13963 sec = ibfd->sections;
13964 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13965 continue;
13966
13967 if (!init_reloc_cookie (&cookie, info, ibfd))
13968 return FALSE;
13969
13970 for (sec = ibfd->sections; sec; sec = sec->next)
13971 {
13972 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13973 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13974 {
13975 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13976 fini_reloc_cookie_rels (&cookie, sec);
13977 }
13978 }
13979 }
13980 return TRUE;
13981 }
13982
13983 /* Do mark and sweep of unused sections. */
13984
13985 bfd_boolean
13986 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13987 {
13988 bfd_boolean ok = TRUE;
13989 bfd *sub;
13990 elf_gc_mark_hook_fn gc_mark_hook;
13991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13992 struct elf_link_hash_table *htab;
13993
13994 if (!bed->can_gc_sections
13995 || !is_elf_hash_table (info->hash))
13996 {
13997 _bfd_error_handler(_("warning: gc-sections option ignored"));
13998 return TRUE;
13999 }
14000
14001 bed->gc_keep (info);
14002 htab = elf_hash_table (info);
14003
14004 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14005 at the .eh_frame section if we can mark the FDEs individually. */
14006 for (sub = info->input_bfds;
14007 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14008 sub = sub->link.next)
14009 {
14010 asection *sec;
14011 struct elf_reloc_cookie cookie;
14012
14013 sec = sub->sections;
14014 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14015 continue;
14016 sec = bfd_get_section_by_name (sub, ".eh_frame");
14017 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14018 {
14019 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14020 if (elf_section_data (sec)->sec_info
14021 && (sec->flags & SEC_LINKER_CREATED) == 0)
14022 elf_eh_frame_section (sub) = sec;
14023 fini_reloc_cookie_for_section (&cookie, sec);
14024 sec = bfd_get_next_section_by_name (NULL, sec);
14025 }
14026 }
14027
14028 /* Apply transitive closure to the vtable entry usage info. */
14029 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14030 if (!ok)
14031 return FALSE;
14032
14033 /* Kill the vtable relocations that were not used. */
14034 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14035 if (!ok)
14036 return FALSE;
14037
14038 /* Mark dynamically referenced symbols. */
14039 if (htab->dynamic_sections_created || info->gc_keep_exported)
14040 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14041
14042 /* Grovel through relocs to find out who stays ... */
14043 gc_mark_hook = bed->gc_mark_hook;
14044 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14045 {
14046 asection *o;
14047
14048 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14049 || elf_object_id (sub) != elf_hash_table_id (htab)
14050 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14051 continue;
14052
14053 o = sub->sections;
14054 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14055 continue;
14056
14057 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14058 Also treat note sections as a root, if the section is not part
14059 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14060 well as FINI_ARRAY sections for ld -r. */
14061 for (o = sub->sections; o != NULL; o = o->next)
14062 if (!o->gc_mark
14063 && (o->flags & SEC_EXCLUDE) == 0
14064 && ((o->flags & SEC_KEEP) != 0
14065 || (bfd_link_relocatable (info)
14066 && ((elf_section_data (o)->this_hdr.sh_type
14067 == SHT_PREINIT_ARRAY)
14068 || (elf_section_data (o)->this_hdr.sh_type
14069 == SHT_INIT_ARRAY)
14070 || (elf_section_data (o)->this_hdr.sh_type
14071 == SHT_FINI_ARRAY)))
14072 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14073 && elf_next_in_group (o) == NULL
14074 && elf_linked_to_section (o) == NULL)
14075 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14076 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14077 {
14078 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14079 return FALSE;
14080 }
14081 }
14082
14083 /* Allow the backend to mark additional target specific sections. */
14084 bed->gc_mark_extra_sections (info, gc_mark_hook);
14085
14086 /* ... and mark SEC_EXCLUDE for those that go. */
14087 return elf_gc_sweep (abfd, info);
14088 }
14089 \f
14090 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14091
14092 bfd_boolean
14093 bfd_elf_gc_record_vtinherit (bfd *abfd,
14094 asection *sec,
14095 struct elf_link_hash_entry *h,
14096 bfd_vma offset)
14097 {
14098 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14099 struct elf_link_hash_entry **search, *child;
14100 size_t extsymcount;
14101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14102
14103 /* The sh_info field of the symtab header tells us where the
14104 external symbols start. We don't care about the local symbols at
14105 this point. */
14106 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14107 if (!elf_bad_symtab (abfd))
14108 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14109
14110 sym_hashes = elf_sym_hashes (abfd);
14111 sym_hashes_end = sym_hashes + extsymcount;
14112
14113 /* Hunt down the child symbol, which is in this section at the same
14114 offset as the relocation. */
14115 for (search = sym_hashes; search != sym_hashes_end; ++search)
14116 {
14117 if ((child = *search) != NULL
14118 && (child->root.type == bfd_link_hash_defined
14119 || child->root.type == bfd_link_hash_defweak)
14120 && child->root.u.def.section == sec
14121 && child->root.u.def.value == offset)
14122 goto win;
14123 }
14124
14125 /* xgettext:c-format */
14126 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14127 abfd, sec, (uint64_t) offset);
14128 bfd_set_error (bfd_error_invalid_operation);
14129 return FALSE;
14130
14131 win:
14132 if (!child->u2.vtable)
14133 {
14134 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14135 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14136 if (!child->u2.vtable)
14137 return FALSE;
14138 }
14139 if (!h)
14140 {
14141 /* This *should* only be the absolute section. It could potentially
14142 be that someone has defined a non-global vtable though, which
14143 would be bad. It isn't worth paging in the local symbols to be
14144 sure though; that case should simply be handled by the assembler. */
14145
14146 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14147 }
14148 else
14149 child->u2.vtable->parent = h;
14150
14151 return TRUE;
14152 }
14153
14154 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14155
14156 bfd_boolean
14157 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14158 struct elf_link_hash_entry *h,
14159 bfd_vma addend)
14160 {
14161 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14162 unsigned int log_file_align = bed->s->log_file_align;
14163
14164 if (!h)
14165 {
14166 /* xgettext:c-format */
14167 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14168 abfd, sec);
14169 bfd_set_error (bfd_error_bad_value);
14170 return FALSE;
14171 }
14172
14173 if (!h->u2.vtable)
14174 {
14175 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14176 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14177 if (!h->u2.vtable)
14178 return FALSE;
14179 }
14180
14181 if (addend >= h->u2.vtable->size)
14182 {
14183 size_t size, bytes, file_align;
14184 bfd_boolean *ptr = h->u2.vtable->used;
14185
14186 /* While the symbol is undefined, we have to be prepared to handle
14187 a zero size. */
14188 file_align = 1 << log_file_align;
14189 if (h->root.type == bfd_link_hash_undefined)
14190 size = addend + file_align;
14191 else
14192 {
14193 size = h->size;
14194 if (addend >= size)
14195 {
14196 /* Oops! We've got a reference past the defined end of
14197 the table. This is probably a bug -- shall we warn? */
14198 size = addend + file_align;
14199 }
14200 }
14201 size = (size + file_align - 1) & -file_align;
14202
14203 /* Allocate one extra entry for use as a "done" flag for the
14204 consolidation pass. */
14205 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14206
14207 if (ptr)
14208 {
14209 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14210
14211 if (ptr != NULL)
14212 {
14213 size_t oldbytes;
14214
14215 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14216 * sizeof (bfd_boolean));
14217 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14218 }
14219 }
14220 else
14221 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14222
14223 if (ptr == NULL)
14224 return FALSE;
14225
14226 /* And arrange for that done flag to be at index -1. */
14227 h->u2.vtable->used = ptr + 1;
14228 h->u2.vtable->size = size;
14229 }
14230
14231 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14232
14233 return TRUE;
14234 }
14235
14236 /* Map an ELF section header flag to its corresponding string. */
14237 typedef struct
14238 {
14239 char *flag_name;
14240 flagword flag_value;
14241 } elf_flags_to_name_table;
14242
14243 static const elf_flags_to_name_table elf_flags_to_names [] =
14244 {
14245 { "SHF_WRITE", SHF_WRITE },
14246 { "SHF_ALLOC", SHF_ALLOC },
14247 { "SHF_EXECINSTR", SHF_EXECINSTR },
14248 { "SHF_MERGE", SHF_MERGE },
14249 { "SHF_STRINGS", SHF_STRINGS },
14250 { "SHF_INFO_LINK", SHF_INFO_LINK},
14251 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14252 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14253 { "SHF_GROUP", SHF_GROUP },
14254 { "SHF_TLS", SHF_TLS },
14255 { "SHF_MASKOS", SHF_MASKOS },
14256 { "SHF_EXCLUDE", SHF_EXCLUDE },
14257 };
14258
14259 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14260 bfd_boolean
14261 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14262 struct flag_info *flaginfo,
14263 asection *section)
14264 {
14265 const bfd_vma sh_flags = elf_section_flags (section);
14266
14267 if (!flaginfo->flags_initialized)
14268 {
14269 bfd *obfd = info->output_bfd;
14270 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14271 struct flag_info_list *tf = flaginfo->flag_list;
14272 int with_hex = 0;
14273 int without_hex = 0;
14274
14275 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14276 {
14277 unsigned i;
14278 flagword (*lookup) (char *);
14279
14280 lookup = bed->elf_backend_lookup_section_flags_hook;
14281 if (lookup != NULL)
14282 {
14283 flagword hexval = (*lookup) ((char *) tf->name);
14284
14285 if (hexval != 0)
14286 {
14287 if (tf->with == with_flags)
14288 with_hex |= hexval;
14289 else if (tf->with == without_flags)
14290 without_hex |= hexval;
14291 tf->valid = TRUE;
14292 continue;
14293 }
14294 }
14295 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14296 {
14297 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14298 {
14299 if (tf->with == with_flags)
14300 with_hex |= elf_flags_to_names[i].flag_value;
14301 else if (tf->with == without_flags)
14302 without_hex |= elf_flags_to_names[i].flag_value;
14303 tf->valid = TRUE;
14304 break;
14305 }
14306 }
14307 if (!tf->valid)
14308 {
14309 info->callbacks->einfo
14310 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14311 return FALSE;
14312 }
14313 }
14314 flaginfo->flags_initialized = TRUE;
14315 flaginfo->only_with_flags |= with_hex;
14316 flaginfo->not_with_flags |= without_hex;
14317 }
14318
14319 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14320 return FALSE;
14321
14322 if ((flaginfo->not_with_flags & sh_flags) != 0)
14323 return FALSE;
14324
14325 return TRUE;
14326 }
14327
14328 struct alloc_got_off_arg {
14329 bfd_vma gotoff;
14330 struct bfd_link_info *info;
14331 };
14332
14333 /* We need a special top-level link routine to convert got reference counts
14334 to real got offsets. */
14335
14336 static bfd_boolean
14337 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14338 {
14339 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14340 bfd *obfd = gofarg->info->output_bfd;
14341 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14342
14343 if (h->got.refcount > 0)
14344 {
14345 h->got.offset = gofarg->gotoff;
14346 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14347 }
14348 else
14349 h->got.offset = (bfd_vma) -1;
14350
14351 return TRUE;
14352 }
14353
14354 /* And an accompanying bit to work out final got entry offsets once
14355 we're done. Should be called from final_link. */
14356
14357 bfd_boolean
14358 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14359 struct bfd_link_info *info)
14360 {
14361 bfd *i;
14362 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14363 bfd_vma gotoff;
14364 struct alloc_got_off_arg gofarg;
14365
14366 BFD_ASSERT (abfd == info->output_bfd);
14367
14368 if (! is_elf_hash_table (info->hash))
14369 return FALSE;
14370
14371 /* The GOT offset is relative to the .got section, but the GOT header is
14372 put into the .got.plt section, if the backend uses it. */
14373 if (bed->want_got_plt)
14374 gotoff = 0;
14375 else
14376 gotoff = bed->got_header_size;
14377
14378 /* Do the local .got entries first. */
14379 for (i = info->input_bfds; i; i = i->link.next)
14380 {
14381 bfd_signed_vma *local_got;
14382 size_t j, locsymcount;
14383 Elf_Internal_Shdr *symtab_hdr;
14384
14385 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14386 continue;
14387
14388 local_got = elf_local_got_refcounts (i);
14389 if (!local_got)
14390 continue;
14391
14392 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14393 if (elf_bad_symtab (i))
14394 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14395 else
14396 locsymcount = symtab_hdr->sh_info;
14397
14398 for (j = 0; j < locsymcount; ++j)
14399 {
14400 if (local_got[j] > 0)
14401 {
14402 local_got[j] = gotoff;
14403 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14404 }
14405 else
14406 local_got[j] = (bfd_vma) -1;
14407 }
14408 }
14409
14410 /* Then the global .got entries. .plt refcounts are handled by
14411 adjust_dynamic_symbol */
14412 gofarg.gotoff = gotoff;
14413 gofarg.info = info;
14414 elf_link_hash_traverse (elf_hash_table (info),
14415 elf_gc_allocate_got_offsets,
14416 &gofarg);
14417 return TRUE;
14418 }
14419
14420 /* Many folk need no more in the way of final link than this, once
14421 got entry reference counting is enabled. */
14422
14423 bfd_boolean
14424 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14425 {
14426 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14427 return FALSE;
14428
14429 /* Invoke the regular ELF backend linker to do all the work. */
14430 return bfd_elf_final_link (abfd, info);
14431 }
14432
14433 bfd_boolean
14434 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14435 {
14436 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14437
14438 if (rcookie->bad_symtab)
14439 rcookie->rel = rcookie->rels;
14440
14441 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14442 {
14443 unsigned long r_symndx;
14444
14445 if (! rcookie->bad_symtab)
14446 if (rcookie->rel->r_offset > offset)
14447 return FALSE;
14448 if (rcookie->rel->r_offset != offset)
14449 continue;
14450
14451 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14452 if (r_symndx == STN_UNDEF)
14453 return TRUE;
14454
14455 if (r_symndx >= rcookie->locsymcount
14456 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14457 {
14458 struct elf_link_hash_entry *h;
14459
14460 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14461
14462 while (h->root.type == bfd_link_hash_indirect
14463 || h->root.type == bfd_link_hash_warning)
14464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14465
14466 if ((h->root.type == bfd_link_hash_defined
14467 || h->root.type == bfd_link_hash_defweak)
14468 && (h->root.u.def.section->owner != rcookie->abfd
14469 || h->root.u.def.section->kept_section != NULL
14470 || discarded_section (h->root.u.def.section)))
14471 return TRUE;
14472 }
14473 else
14474 {
14475 /* It's not a relocation against a global symbol,
14476 but it could be a relocation against a local
14477 symbol for a discarded section. */
14478 asection *isec;
14479 Elf_Internal_Sym *isym;
14480
14481 /* Need to: get the symbol; get the section. */
14482 isym = &rcookie->locsyms[r_symndx];
14483 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14484 if (isec != NULL
14485 && (isec->kept_section != NULL
14486 || discarded_section (isec)))
14487 return TRUE;
14488 }
14489 return FALSE;
14490 }
14491 return FALSE;
14492 }
14493
14494 /* Discard unneeded references to discarded sections.
14495 Returns -1 on error, 1 if any section's size was changed, 0 if
14496 nothing changed. This function assumes that the relocations are in
14497 sorted order, which is true for all known assemblers. */
14498
14499 int
14500 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14501 {
14502 struct elf_reloc_cookie cookie;
14503 asection *o;
14504 bfd *abfd;
14505 int changed = 0;
14506
14507 if (info->traditional_format
14508 || !is_elf_hash_table (info->hash))
14509 return 0;
14510
14511 o = bfd_get_section_by_name (output_bfd, ".stab");
14512 if (o != NULL)
14513 {
14514 asection *i;
14515
14516 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14517 {
14518 if (i->size == 0
14519 || i->reloc_count == 0
14520 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14521 continue;
14522
14523 abfd = i->owner;
14524 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14525 continue;
14526
14527 if (!init_reloc_cookie_for_section (&cookie, info, i))
14528 return -1;
14529
14530 if (_bfd_discard_section_stabs (abfd, i,
14531 elf_section_data (i)->sec_info,
14532 bfd_elf_reloc_symbol_deleted_p,
14533 &cookie))
14534 changed = 1;
14535
14536 fini_reloc_cookie_for_section (&cookie, i);
14537 }
14538 }
14539
14540 o = NULL;
14541 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14542 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14543 if (o != NULL)
14544 {
14545 asection *i;
14546 int eh_changed = 0;
14547 unsigned int eh_alignment; /* Octets. */
14548
14549 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14550 {
14551 if (i->size == 0)
14552 continue;
14553
14554 abfd = i->owner;
14555 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14556 continue;
14557
14558 if (!init_reloc_cookie_for_section (&cookie, info, i))
14559 return -1;
14560
14561 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14562 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14563 bfd_elf_reloc_symbol_deleted_p,
14564 &cookie))
14565 {
14566 eh_changed = 1;
14567 if (i->size != i->rawsize)
14568 changed = 1;
14569 }
14570
14571 fini_reloc_cookie_for_section (&cookie, i);
14572 }
14573
14574 eh_alignment = ((1 << o->alignment_power)
14575 * bfd_octets_per_byte (output_bfd, o));
14576 /* Skip over zero terminator, and prevent empty sections from
14577 adding alignment padding at the end. */
14578 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14579 if (i->size == 0)
14580 i->flags |= SEC_EXCLUDE;
14581 else if (i->size > 4)
14582 break;
14583 /* The last non-empty eh_frame section doesn't need padding. */
14584 if (i != NULL)
14585 i = i->map_tail.s;
14586 /* Any prior sections must pad the last FDE out to the output
14587 section alignment. Otherwise we might have zero padding
14588 between sections, which would be seen as a terminator. */
14589 for (; i != NULL; i = i->map_tail.s)
14590 if (i->size == 4)
14591 /* All but the last zero terminator should have been removed. */
14592 BFD_FAIL ();
14593 else
14594 {
14595 bfd_size_type size
14596 = (i->size + eh_alignment - 1) & -eh_alignment;
14597 if (i->size != size)
14598 {
14599 i->size = size;
14600 changed = 1;
14601 eh_changed = 1;
14602 }
14603 }
14604 if (eh_changed)
14605 elf_link_hash_traverse (elf_hash_table (info),
14606 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14607 }
14608
14609 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14610 {
14611 const struct elf_backend_data *bed;
14612 asection *s;
14613
14614 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14615 continue;
14616 s = abfd->sections;
14617 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14618 continue;
14619
14620 bed = get_elf_backend_data (abfd);
14621
14622 if (bed->elf_backend_discard_info != NULL)
14623 {
14624 if (!init_reloc_cookie (&cookie, info, abfd))
14625 return -1;
14626
14627 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14628 changed = 1;
14629
14630 fini_reloc_cookie (&cookie, abfd);
14631 }
14632 }
14633
14634 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14635 _bfd_elf_end_eh_frame_parsing (info);
14636
14637 if (info->eh_frame_hdr_type
14638 && !bfd_link_relocatable (info)
14639 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14640 changed = 1;
14641
14642 return changed;
14643 }
14644
14645 bfd_boolean
14646 _bfd_elf_section_already_linked (bfd *abfd,
14647 asection *sec,
14648 struct bfd_link_info *info)
14649 {
14650 flagword flags;
14651 const char *name, *key;
14652 struct bfd_section_already_linked *l;
14653 struct bfd_section_already_linked_hash_entry *already_linked_list;
14654
14655 if (sec->output_section == bfd_abs_section_ptr)
14656 return FALSE;
14657
14658 flags = sec->flags;
14659
14660 /* Return if it isn't a linkonce section. A comdat group section
14661 also has SEC_LINK_ONCE set. */
14662 if ((flags & SEC_LINK_ONCE) == 0)
14663 return FALSE;
14664
14665 /* Don't put group member sections on our list of already linked
14666 sections. They are handled as a group via their group section. */
14667 if (elf_sec_group (sec) != NULL)
14668 return FALSE;
14669
14670 /* For a SHT_GROUP section, use the group signature as the key. */
14671 name = sec->name;
14672 if ((flags & SEC_GROUP) != 0
14673 && elf_next_in_group (sec) != NULL
14674 && elf_group_name (elf_next_in_group (sec)) != NULL)
14675 key = elf_group_name (elf_next_in_group (sec));
14676 else
14677 {
14678 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14679 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14680 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14681 key++;
14682 else
14683 /* Must be a user linkonce section that doesn't follow gcc's
14684 naming convention. In this case we won't be matching
14685 single member groups. */
14686 key = name;
14687 }
14688
14689 already_linked_list = bfd_section_already_linked_table_lookup (key);
14690
14691 for (l = already_linked_list->entry; l != NULL; l = l->next)
14692 {
14693 /* We may have 2 different types of sections on the list: group
14694 sections with a signature of <key> (<key> is some string),
14695 and linkonce sections named .gnu.linkonce.<type>.<key>.
14696 Match like sections. LTO plugin sections are an exception.
14697 They are always named .gnu.linkonce.t.<key> and match either
14698 type of section. */
14699 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14700 && ((flags & SEC_GROUP) != 0
14701 || strcmp (name, l->sec->name) == 0))
14702 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14703 || (sec->owner->flags & BFD_PLUGIN) != 0)
14704 {
14705 /* The section has already been linked. See if we should
14706 issue a warning. */
14707 if (!_bfd_handle_already_linked (sec, l, info))
14708 return FALSE;
14709
14710 if (flags & SEC_GROUP)
14711 {
14712 asection *first = elf_next_in_group (sec);
14713 asection *s = first;
14714
14715 while (s != NULL)
14716 {
14717 s->output_section = bfd_abs_section_ptr;
14718 /* Record which group discards it. */
14719 s->kept_section = l->sec;
14720 s = elf_next_in_group (s);
14721 /* These lists are circular. */
14722 if (s == first)
14723 break;
14724 }
14725 }
14726
14727 return TRUE;
14728 }
14729 }
14730
14731 /* A single member comdat group section may be discarded by a
14732 linkonce section and vice versa. */
14733 if ((flags & SEC_GROUP) != 0)
14734 {
14735 asection *first = elf_next_in_group (sec);
14736
14737 if (first != NULL && elf_next_in_group (first) == first)
14738 /* Check this single member group against linkonce sections. */
14739 for (l = already_linked_list->entry; l != NULL; l = l->next)
14740 if ((l->sec->flags & SEC_GROUP) == 0
14741 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14742 {
14743 first->output_section = bfd_abs_section_ptr;
14744 first->kept_section = l->sec;
14745 sec->output_section = bfd_abs_section_ptr;
14746 break;
14747 }
14748 }
14749 else
14750 /* Check this linkonce section against single member groups. */
14751 for (l = already_linked_list->entry; l != NULL; l = l->next)
14752 if (l->sec->flags & SEC_GROUP)
14753 {
14754 asection *first = elf_next_in_group (l->sec);
14755
14756 if (first != NULL
14757 && elf_next_in_group (first) == first
14758 && bfd_elf_match_symbols_in_sections (first, sec, info))
14759 {
14760 sec->output_section = bfd_abs_section_ptr;
14761 sec->kept_section = first;
14762 break;
14763 }
14764 }
14765
14766 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14767 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14768 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14769 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14770 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14771 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14772 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14773 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14774 The reverse order cannot happen as there is never a bfd with only the
14775 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14776 matter as here were are looking only for cross-bfd sections. */
14777
14778 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14779 for (l = already_linked_list->entry; l != NULL; l = l->next)
14780 if ((l->sec->flags & SEC_GROUP) == 0
14781 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14782 {
14783 if (abfd != l->sec->owner)
14784 sec->output_section = bfd_abs_section_ptr;
14785 break;
14786 }
14787
14788 /* This is the first section with this name. Record it. */
14789 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14790 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14791 return sec->output_section == bfd_abs_section_ptr;
14792 }
14793
14794 bfd_boolean
14795 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14796 {
14797 return sym->st_shndx == SHN_COMMON;
14798 }
14799
14800 unsigned int
14801 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14802 {
14803 return SHN_COMMON;
14804 }
14805
14806 asection *
14807 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14808 {
14809 return bfd_com_section_ptr;
14810 }
14811
14812 bfd_vma
14813 _bfd_elf_default_got_elt_size (bfd *abfd,
14814 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14815 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14816 bfd *ibfd ATTRIBUTE_UNUSED,
14817 unsigned long symndx ATTRIBUTE_UNUSED)
14818 {
14819 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14820 return bed->s->arch_size / 8;
14821 }
14822
14823 /* Routines to support the creation of dynamic relocs. */
14824
14825 /* Returns the name of the dynamic reloc section associated with SEC. */
14826
14827 static const char *
14828 get_dynamic_reloc_section_name (bfd * abfd,
14829 asection * sec,
14830 bfd_boolean is_rela)
14831 {
14832 char *name;
14833 const char *old_name = bfd_section_name (sec);
14834 const char *prefix = is_rela ? ".rela" : ".rel";
14835
14836 if (old_name == NULL)
14837 return NULL;
14838
14839 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14840 sprintf (name, "%s%s", prefix, old_name);
14841
14842 return name;
14843 }
14844
14845 /* Returns the dynamic reloc section associated with SEC.
14846 If necessary compute the name of the dynamic reloc section based
14847 on SEC's name (looked up in ABFD's string table) and the setting
14848 of IS_RELA. */
14849
14850 asection *
14851 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14852 asection * sec,
14853 bfd_boolean is_rela)
14854 {
14855 asection * reloc_sec = elf_section_data (sec)->sreloc;
14856
14857 if (reloc_sec == NULL)
14858 {
14859 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14860
14861 if (name != NULL)
14862 {
14863 reloc_sec = bfd_get_linker_section (abfd, name);
14864
14865 if (reloc_sec != NULL)
14866 elf_section_data (sec)->sreloc = reloc_sec;
14867 }
14868 }
14869
14870 return reloc_sec;
14871 }
14872
14873 /* Returns the dynamic reloc section associated with SEC. If the
14874 section does not exist it is created and attached to the DYNOBJ
14875 bfd and stored in the SRELOC field of SEC's elf_section_data
14876 structure.
14877
14878 ALIGNMENT is the alignment for the newly created section and
14879 IS_RELA defines whether the name should be .rela.<SEC's name>
14880 or .rel.<SEC's name>. The section name is looked up in the
14881 string table associated with ABFD. */
14882
14883 asection *
14884 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14885 bfd *dynobj,
14886 unsigned int alignment,
14887 bfd *abfd,
14888 bfd_boolean is_rela)
14889 {
14890 asection * reloc_sec = elf_section_data (sec)->sreloc;
14891
14892 if (reloc_sec == NULL)
14893 {
14894 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14895
14896 if (name == NULL)
14897 return NULL;
14898
14899 reloc_sec = bfd_get_linker_section (dynobj, name);
14900
14901 if (reloc_sec == NULL)
14902 {
14903 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14904 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14905 if ((sec->flags & SEC_ALLOC) != 0)
14906 flags |= SEC_ALLOC | SEC_LOAD;
14907
14908 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14909 if (reloc_sec != NULL)
14910 {
14911 /* _bfd_elf_get_sec_type_attr chooses a section type by
14912 name. Override as it may be wrong, eg. for a user
14913 section named "auto" we'll get ".relauto" which is
14914 seen to be a .rela section. */
14915 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14916 if (!bfd_set_section_alignment (reloc_sec, alignment))
14917 reloc_sec = NULL;
14918 }
14919 }
14920
14921 elf_section_data (sec)->sreloc = reloc_sec;
14922 }
14923
14924 return reloc_sec;
14925 }
14926
14927 /* Copy the ELF symbol type and other attributes for a linker script
14928 assignment from HSRC to HDEST. Generally this should be treated as
14929 if we found a strong non-dynamic definition for HDEST (except that
14930 ld ignores multiple definition errors). */
14931 void
14932 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14933 struct bfd_link_hash_entry *hdest,
14934 struct bfd_link_hash_entry *hsrc)
14935 {
14936 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14937 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14938 Elf_Internal_Sym isym;
14939
14940 ehdest->type = ehsrc->type;
14941 ehdest->target_internal = ehsrc->target_internal;
14942
14943 isym.st_other = ehsrc->other;
14944 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
14945 }
14946
14947 /* Append a RELA relocation REL to section S in BFD. */
14948
14949 void
14950 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14951 {
14952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14953 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14954 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14955 bed->s->swap_reloca_out (abfd, rel, loc);
14956 }
14957
14958 /* Append a REL relocation REL to section S in BFD. */
14959
14960 void
14961 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14962 {
14963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14964 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14965 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14966 bed->s->swap_reloc_out (abfd, rel, loc);
14967 }
14968
14969 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14970
14971 struct bfd_link_hash_entry *
14972 bfd_elf_define_start_stop (struct bfd_link_info *info,
14973 const char *symbol, asection *sec)
14974 {
14975 struct elf_link_hash_entry *h;
14976
14977 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14978 FALSE, FALSE, TRUE);
14979 /* NB: Common symbols will be turned into definition later. */
14980 if (h != NULL
14981 && (h->root.type == bfd_link_hash_undefined
14982 || h->root.type == bfd_link_hash_undefweak
14983 || ((h->ref_regular || h->def_dynamic)
14984 && !h->def_regular
14985 && h->root.type != bfd_link_hash_common)))
14986 {
14987 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14988 h->verinfo.verdef = NULL;
14989 h->root.type = bfd_link_hash_defined;
14990 h->root.u.def.section = sec;
14991 h->root.u.def.value = 0;
14992 h->def_regular = 1;
14993 h->def_dynamic = 0;
14994 h->start_stop = 1;
14995 h->u2.start_stop_section = sec;
14996 if (symbol[0] == '.')
14997 {
14998 /* .startof. and .sizeof. symbols are local. */
14999 const struct elf_backend_data *bed;
15000 bed = get_elf_backend_data (info->output_bfd);
15001 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
15002 }
15003 else
15004 {
15005 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15006 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15007 | info->start_stop_visibility);
15008 if (was_dynamic)
15009 bfd_elf_link_record_dynamic_symbol (info, h);
15010 }
15011 return &h->root;
15012 }
15013 return NULL;
15014 }
15015
15016 /* Find dynamic relocs for H that apply to read-only sections. */
15017
15018 asection *
15019 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15020 {
15021 struct elf_dyn_relocs *p;
15022
15023 for (p = h->dyn_relocs; p != NULL; p = p->next)
15024 {
15025 asection *s = p->sec->output_section;
15026
15027 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15028 return p->sec;
15029 }
15030 return NULL;
15031 }
15032
15033 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15034 read-only sections. */
15035
15036 bfd_boolean
15037 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15038 {
15039 asection *sec;
15040
15041 if (h->root.type == bfd_link_hash_indirect)
15042 return TRUE;
15043
15044 sec = _bfd_elf_readonly_dynrelocs (h);
15045 if (sec != NULL)
15046 {
15047 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15048
15049 info->flags |= DF_TEXTREL;
15050 /* xgettext:c-format */
15051 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15052 "in read-only section `%pA'\n"),
15053 sec->owner, h->root.root.string, sec);
15054
15055 if (bfd_link_textrel_check (info))
15056 /* xgettext:c-format */
15057 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15058 "in read-only section `%pA'\n"),
15059 sec->owner, h->root.root.string, sec);
15060
15061 /* Not an error, just cut short the traversal. */
15062 return FALSE;
15063 }
15064 return TRUE;
15065 }
15066
15067 /* Add dynamic tags. */
15068
15069 bfd_boolean
15070 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15071 bfd_boolean need_dynamic_reloc)
15072 {
15073 struct elf_link_hash_table *htab = elf_hash_table (info);
15074
15075 if (htab->dynamic_sections_created)
15076 {
15077 /* Add some entries to the .dynamic section. We fill in the
15078 values later, in finish_dynamic_sections, but we must add
15079 the entries now so that we get the correct size for the
15080 .dynamic section. The DT_DEBUG entry is filled in by the
15081 dynamic linker and used by the debugger. */
15082 #define add_dynamic_entry(TAG, VAL) \
15083 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15084
15085 const struct elf_backend_data *bed
15086 = get_elf_backend_data (output_bfd);
15087
15088 if (bfd_link_executable (info))
15089 {
15090 if (!add_dynamic_entry (DT_DEBUG, 0))
15091 return FALSE;
15092 }
15093
15094 if (htab->dt_pltgot_required || htab->splt->size != 0)
15095 {
15096 /* DT_PLTGOT is used by prelink even if there is no PLT
15097 relocation. */
15098 if (!add_dynamic_entry (DT_PLTGOT, 0))
15099 return FALSE;
15100 }
15101
15102 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15103 {
15104 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15105 || !add_dynamic_entry (DT_PLTREL,
15106 (bed->rela_plts_and_copies_p
15107 ? DT_RELA : DT_REL))
15108 || !add_dynamic_entry (DT_JMPREL, 0))
15109 return FALSE;
15110 }
15111
15112 if (htab->tlsdesc_plt
15113 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15114 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15115 return FALSE;
15116
15117 if (need_dynamic_reloc)
15118 {
15119 if (bed->rela_plts_and_copies_p)
15120 {
15121 if (!add_dynamic_entry (DT_RELA, 0)
15122 || !add_dynamic_entry (DT_RELASZ, 0)
15123 || !add_dynamic_entry (DT_RELAENT,
15124 bed->s->sizeof_rela))
15125 return FALSE;
15126 }
15127 else
15128 {
15129 if (!add_dynamic_entry (DT_REL, 0)
15130 || !add_dynamic_entry (DT_RELSZ, 0)
15131 || !add_dynamic_entry (DT_RELENT,
15132 bed->s->sizeof_rel))
15133 return FALSE;
15134 }
15135
15136 /* If any dynamic relocs apply to a read-only section,
15137 then we need a DT_TEXTREL entry. */
15138 if ((info->flags & DF_TEXTREL) == 0)
15139 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15140 info);
15141
15142 if ((info->flags & DF_TEXTREL) != 0)
15143 {
15144 if (htab->ifunc_resolvers)
15145 info->callbacks->einfo
15146 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15147 "may result in a segfault at runtime; recompile with %s\n"),
15148 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15149
15150 if (!add_dynamic_entry (DT_TEXTREL, 0))
15151 return FALSE;
15152 }
15153 }
15154 }
15155 #undef add_dynamic_entry
15156
15157 return TRUE;
15158 }