]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
ld/elf: Ignore section symbols when matching linkonce with comdat
[thirdparty/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->no_export)
541 || (h->root.type == bfd_link_hash_common
542 && h->root.u.c.p->section->owner->no_export))
543 return TRUE;
544 }
545
546 default:
547 break;
548 }
549
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
558 if (dynstr == NULL)
559 return FALSE;
560 }
561
562 /* We don't put any version information in the dynamic string
563 table. */
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
578
579 if (indx == (size_t) -1)
580 return FALSE;
581 h->dynstr_index = indx;
582 }
583
584 return TRUE;
585 }
586 \f
587 /* Mark a symbol dynamic. */
588
589 static void
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
593 {
594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
595
596 /* It may be called more than once on the same H. */
597 if(h->dynamic || bfd_link_relocatable (info))
598 return;
599
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
602 || h->type == STT_COMMON
603 || (sym != NULL
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
606 || (d != NULL
607 && h->non_elf
608 && (*d->match) (&d->head, NULL, h->root.root.string)))
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
615 }
616
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
620 bfd_boolean
621 bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
623 const char *name,
624 bfd_boolean provide,
625 bfd_boolean hidden)
626 {
627 struct elf_link_hash_entry *h, *hv;
628 struct elf_link_hash_table *htab;
629 const struct elf_backend_data *bed;
630
631 if (!is_elf_hash_table (info->hash))
632 return TRUE;
633
634 htab = elf_hash_table (info);
635 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
636 if (h == NULL)
637 return provide;
638
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
663 switch (h->root.type)
664 {
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h->root.type = bfd_link_hash_new;
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
677 break;
678 case bfd_link_hash_new:
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
695 default:
696 BFD_FAIL ();
697 return FALSE;
698 }
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
705 && h->def_dynamic
706 && !h->def_regular)
707 h->root.type = bfd_link_hash_undefined;
708
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
718
719 h->def_regular = 1;
720
721 if (hidden)
722 {
723 bed = get_elf_backend_data (output_bfd);
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
726 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
727 }
728
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
731 if (!bfd_link_relocatable (info)
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
737 if ((h->def_dynamic
738 || h->ref_dynamic
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
741 && !h->forced_local
742 && h->dynindx == -1)
743 {
744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
745 return FALSE;
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
750 if (h->is_weakalias)
751 {
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
756 return FALSE;
757 }
758 }
759
760 return TRUE;
761 }
762
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767 int
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
771 {
772 size_t amt;
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
776 size_t dynstr_index;
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
781 if (! is_elf_hash_table (info->hash))
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
796 1, input_indx, &entry->isym, esym, &eshndx))
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
803 && entry->isym.st_shndx < SHN_LORESERVE)
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
831 if (dynstr_index == (size_t) -1)
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850 }
851
852 /* Return the dynindex of a local dynamic symbol. */
853
854 long
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
858 {
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865 }
866
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
871 static bfd_boolean
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (h->forced_local)
878 return TRUE;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return TRUE;
884 }
885
886
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
890 static bfd_boolean
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893 {
894 size_t *count = (size_t *) data;
895
896 if (!h->forced_local)
897 return TRUE;
898
899 if (h->dynindx != -1)
900 h->dynindx = ++(*count);
901
902 return TRUE;
903 }
904
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
907 bfd_boolean
908 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
911 {
912 struct elf_link_hash_table *htab;
913 asection *ip;
914
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
922 htab = elf_hash_table (info);
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
926 return (htab->dynobj != NULL
927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
928 && ip->output_section == p);
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
933 return TRUE;
934 }
935 }
936
937 bfd_boolean
938 _bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942 {
943 return TRUE;
944 }
945
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
954
955 static unsigned long
956 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
959 {
960 unsigned long dynsymcount = 0;
961 bfd_boolean do_sec = section_sym_count != NULL;
962
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
965 {
966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
969 if ((p->flags & SEC_EXCLUDE) == 0
970 && (p->flags & SEC_ALLOC) != 0
971 && elf_hash_table (info)->dynamic_relocs
972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
979 elf_section_data (p)->dynindx = 0;
980 }
981 if (do_sec)
982 *section_sym_count = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
1005
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
1008 }
1009
1010 /* Merge st_other field. */
1011
1012 static void
1013 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1014 unsigned int st_other, asection *sec,
1015 bfd_boolean definition, bfd_boolean dynamic)
1016 {
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed->elf_backend_merge_symbol_attribute)
1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1023 dynamic);
1024
1025 if (!dynamic)
1026 {
1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1029
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1034 }
1035 else if (definition
1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1037 && (sec->flags & SEC_READONLY) == 0)
1038 h->protected_def = 1;
1039 }
1040
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1053
1054 static bfd_boolean
1055 _bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
1063 bfd_boolean *pold_weak,
1064 unsigned int *pold_alignment,
1065 bfd_boolean *skip,
1066 bfd **override,
1067 bfd_boolean *type_change_ok,
1068 bfd_boolean *size_change_ok,
1069 bfd_boolean *matched)
1070 {
1071 asection *sec, *oldsec;
1072 struct elf_link_hash_entry *h;
1073 struct elf_link_hash_entry *hi;
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
1077 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1078 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1079 const struct elf_backend_data *bed;
1080 char *new_version;
1081 bfd_boolean default_sym = *matched;
1082
1083 *skip = FALSE;
1084 *override = NULL;
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
1090 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1091 else
1092 h = ((struct elf_link_hash_entry *)
1093 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1094 if (h == NULL)
1095 return FALSE;
1096 *sym_hash = h;
1097
1098 bed = get_elf_backend_data (abfd);
1099
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h->versioned != unversioned)
1102 {
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
1120 }
1121 else
1122 new_version = NULL;
1123
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
1134 *matched = TRUE;
1135 else
1136 {
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1142 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
1146 *matched = TRUE;
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
1248 {
1249 h->non_elf = 0;
1250 return TRUE;
1251 }
1252
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
1260 && (newweak || oldweak)
1261 && ((abfd->flags & DYNAMIC) == 0
1262 || !h->def_regular))
1263 return TRUE;
1264
1265 olddyn = FALSE;
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1268 else if (oldsec != NULL)
1269 {
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1273 }
1274
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1277 if (oldbfd != NULL
1278 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1279 && newdyn != olddyn)
1280 {
1281 h->root.non_ir_ref_dynamic = TRUE;
1282 hi->root.non_ir_ref_dynamic = TRUE;
1283 }
1284
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1287
1288 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1289
1290 olddef = (h->root.type != bfd_link_hash_undefined
1291 && h->root.type != bfd_link_hash_undefweak
1292 && h->root.type != bfd_link_hash_common);
1293
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1296
1297 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1298 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1299
1300 oldfunc = (h->type != STT_NOTYPE
1301 && bed->is_function_type (h->type));
1302
1303 if (!(newfunc && oldfunc)
1304 && ELF_ST_TYPE (sym->st_info) != h->type
1305 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1306 && h->type != STT_NOTYPE
1307 && (newdef || bfd_is_com_section (sec))
1308 && (olddef || h->root.type == bfd_link_hash_common))
1309 {
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1315 if (newdyn
1316 && !olddyn)
1317 {
1318 *skip = TRUE;
1319 return TRUE;
1320 }
1321
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1324 dynamic state. */
1325 if (hi != h
1326 && !newdyn
1327 && olddyn)
1328 {
1329 h = hi;
1330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 h->def_dynamic = 0;
1334 h->dynamic_def = 0;
1335 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1336 {
1337 h->root.type = bfd_link_hash_undefined;
1338 h->root.u.undef.abfd = abfd;
1339 }
1340 else
1341 {
1342 h->root.type = bfd_link_hash_new;
1343 h->root.u.undef.abfd = NULL;
1344 }
1345 return TRUE;
1346 }
1347 }
1348
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1352 if (oldbfd != NULL
1353 && (oldbfd->flags & BFD_PLUGIN) == 0
1354 && (abfd->flags & BFD_PLUGIN) == 0
1355 && ELF_ST_TYPE (sym->st_info) != h->type
1356 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1357 {
1358 bfd *ntbfd, *tbfd;
1359 bfd_boolean ntdef, tdef;
1360 asection *ntsec, *tsec;
1361
1362 if (h->type == STT_TLS)
1363 {
1364 ntbfd = abfd;
1365 ntsec = sec;
1366 ntdef = newdef;
1367 tbfd = oldbfd;
1368 tsec = oldsec;
1369 tdef = olddef;
1370 }
1371 else
1372 {
1373 ntbfd = oldbfd;
1374 ntsec = oldsec;
1375 ntdef = olddef;
1376 tbfd = abfd;
1377 tsec = sec;
1378 tdef = newdef;
1379 }
1380
1381 if (tdef && ntdef)
1382 _bfd_error_handler
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
1386 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1387 else if (!tdef && !ntdef)
1388 _bfd_error_handler
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
1392 h->root.root.string, tbfd, ntbfd);
1393 else if (tdef)
1394 _bfd_error_handler
1395 /* xgettext:c-format */
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
1398 h->root.root.string, tbfd, tsec, ntbfd);
1399 else
1400 _bfd_error_handler
1401 /* xgettext:c-format */
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
1404 h->root.root.string, tbfd, ntbfd, ntsec);
1405
1406 bfd_set_error (bfd_error_bad_value);
1407 return FALSE;
1408 }
1409
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1412 if (newdyn
1413 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1414 && !bfd_is_und_section (sec))
1415 {
1416 *skip = TRUE;
1417 /* Make sure this symbol is dynamic. */
1418 h->ref_dynamic = 1;
1419 hi->ref_dynamic = 1;
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1422
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1425 return bfd_elf_link_record_dynamic_symbol (info, h);
1426 else
1427 return TRUE;
1428 }
1429 else if (!newdyn
1430 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1431 && h->def_dynamic)
1432 {
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
1436 if (hi->root.type == bfd_link_hash_indirect)
1437 {
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1442 if (h->ref_regular)
1443 {
1444 hi->root.type = h->root.type;
1445 h->root.type = bfd_link_hash_indirect;
1446 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1447
1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1450 {
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
1456 }
1457 else
1458 h->ref_dynamic = 1;
1459
1460 h->def_dynamic = 0;
1461 /* FIXME: Should we check type and size for protected symbol? */
1462 h->size = 0;
1463 h->type = 0;
1464
1465 h = hi;
1466 }
1467 else
1468 h = hi;
1469 }
1470
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1479 {
1480 h->root.type = bfd_link_hash_undefined;
1481 h->root.u.undef.abfd = abfd;
1482 }
1483 else
1484 {
1485 h->root.type = bfd_link_hash_new;
1486 h->root.u.undef.abfd = NULL;
1487 }
1488
1489 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1490 {
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1494 h->forced_local = 0;
1495 h->ref_dynamic = 0;
1496 }
1497 else
1498 h->ref_dynamic = 1;
1499 h->def_dynamic = 0;
1500 /* FIXME: Should we check type and size for protected symbol? */
1501 h->size = 0;
1502 h->type = 0;
1503 return TRUE;
1504 }
1505
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1513
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1518
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1521
1522 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1523 newweak = FALSE;
1524 if (olddef && newdyn)
1525 oldweak = FALSE;
1526
1527 /* Allow changes between different types of function symbol. */
1528 if (newfunc && oldfunc)
1529 *type_change_ok = TRUE;
1530
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
1534
1535 if (oldweak
1536 || newweak
1537 || (newdef
1538 && h->root.type == bfd_link_hash_undefined))
1539 *type_change_ok = TRUE;
1540
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1543
1544 if (*type_change_ok
1545 || h->root.type == bfd_link_hash_undefined)
1546 *size_change_ok = TRUE;
1547
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1559 libraries.
1560
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1563
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1569 harmless. */
1570
1571 if (newdyn
1572 && newdef
1573 && !newweak
1574 && (sec->flags & SEC_ALLOC) != 0
1575 && (sec->flags & SEC_LOAD) == 0
1576 && sym->st_size > 0
1577 && !newfunc)
1578 newdyncommon = TRUE;
1579 else
1580 newdyncommon = FALSE;
1581
1582 if (olddyn
1583 && olddef
1584 && h->root.type == bfd_link_hash_defined
1585 && h->def_dynamic
1586 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1587 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1588 && h->size > 0
1589 && !oldfunc)
1590 olddyncommon = TRUE;
1591 else
1592 olddyncommon = FALSE;
1593
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
1596 if (bed->merge_symbol != NULL)
1597 {
1598 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1599 return FALSE;
1600 sec = *psec;
1601 }
1602
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
1605 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1606 && !default_sym && h->def_regular
1607 && !(oldbfd != NULL
1608 && (oldbfd->flags & BFD_PLUGIN) != 0
1609 && (abfd->flags & BFD_PLUGIN) == 0))
1610 {
1611 /* Handle a multiple definition. */
1612 (*info->callbacks->multiple_definition) (info, &h->root,
1613 abfd, sec, *pvalue);
1614 *skip = TRUE;
1615 return TRUE;
1616 }
1617
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1620 two. */
1621
1622 if (olddyncommon
1623 && newdyncommon
1624 && sym->st_size != h->size)
1625 {
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1631
1632 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1633 bfd_link_hash_common, sym->st_size);
1634 if (sym->st_size > h->size)
1635 h->size = sym->st_size;
1636
1637 *size_change_ok = TRUE;
1638 }
1639
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1646
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
1652 object to override a weak symbol in a shared object. */
1653
1654 if (newdyn
1655 && newdef
1656 && (olddef
1657 || (h->root.type == bfd_link_hash_common
1658 && (newweak || newfunc))))
1659 {
1660 *override = abfd;
1661 newdef = FALSE;
1662 newdyncommon = FALSE;
1663
1664 *psec = sec = bfd_und_section_ptr;
1665 *size_change_ok = TRUE;
1666
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
1672
1673 if (h->root.type == bfd_link_hash_common)
1674 *type_change_ok = TRUE;
1675 }
1676
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1681 right thing. */
1682
1683 if (newdyncommon
1684 && h->root.type == bfd_link_hash_common)
1685 {
1686 *override = oldbfd;
1687 newdef = FALSE;
1688 newdyncommon = FALSE;
1689 *pvalue = sym->st_size;
1690 *psec = sec = bed->common_section (oldsec);
1691 *size_change_ok = TRUE;
1692 }
1693
1694 /* Skip weak definitions of symbols that are already defined. */
1695 if (newdef && olddef && newweak)
1696 {
1697 /* Don't skip new non-IR weak syms. */
1698 if (!(oldbfd != NULL
1699 && (oldbfd->flags & BFD_PLUGIN) != 0
1700 && (abfd->flags & BFD_PLUGIN) == 0))
1701 {
1702 newdef = FALSE;
1703 *skip = TRUE;
1704 }
1705
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1708 local symbol. */
1709 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1710 if (h->dynindx != -1)
1711 switch (ELF_ST_VISIBILITY (h->other))
1712 {
1713 case STV_INTERNAL:
1714 case STV_HIDDEN:
1715 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1716 break;
1717 }
1718 }
1719
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1725
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
1728 symbol is a function or is weak. */
1729
1730 flip = NULL;
1731 if (!newdyn
1732 && (newdef
1733 || (bfd_is_com_section (sec)
1734 && (oldweak || oldfunc)))
1735 && olddyn
1736 && olddef
1737 && h->def_dynamic)
1738 {
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1741 new definition. */
1742
1743 h->root.type = bfd_link_hash_undefined;
1744 h->root.u.undef.abfd = h->root.u.def.section->owner;
1745 *size_change_ok = TRUE;
1746
1747 olddef = FALSE;
1748 olddyncommon = FALSE;
1749
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1752
1753 if (bfd_is_com_section (sec))
1754 {
1755 if (oldfunc)
1756 {
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1759 function. */
1760 h->def_dynamic = 0;
1761 h->type = STT_NOTYPE;
1762 }
1763 *type_change_ok = TRUE;
1764 }
1765
1766 if (hi->root.type == bfd_link_hash_indirect)
1767 flip = hi;
1768 else
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h->verinfo.vertree = NULL;
1773 }
1774
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1780
1781 if (! newdyn
1782 && bfd_is_com_section (sec)
1783 && olddyncommon)
1784 {
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1788 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1789 bfd_link_hash_common, sym->st_size);
1790
1791 /* If the presumed common symbol in the dynamic object is
1792 larger, pretend that the new symbol has its size. */
1793
1794 if (h->size > *pvalue)
1795 *pvalue = h->size;
1796
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment);
1800 *pold_alignment = h->root.u.def.section->alignment_power;
1801
1802 olddef = FALSE;
1803 olddyncommon = FALSE;
1804
1805 h->root.type = bfd_link_hash_undefined;
1806 h->root.u.undef.abfd = h->root.u.def.section->owner;
1807
1808 *size_change_ok = TRUE;
1809 *type_change_ok = TRUE;
1810
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 flip = hi;
1813 else
1814 h->verinfo.vertree = NULL;
1815 }
1816
1817 if (flip != NULL)
1818 {
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
1822 flip->root.type = h->root.type;
1823 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1826 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1827 if (h->def_dynamic)
1828 {
1829 h->def_dynamic = 0;
1830 flip->ref_dynamic = 1;
1831 }
1832 }
1833
1834 return TRUE;
1835 }
1836
1837 /* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1840 set DYNSYM if the new indirect symbol is dynamic. */
1841
1842 static bfd_boolean
1843 _bfd_elf_add_default_symbol (bfd *abfd,
1844 struct bfd_link_info *info,
1845 struct elf_link_hash_entry *h,
1846 const char *name,
1847 Elf_Internal_Sym *sym,
1848 asection *sec,
1849 bfd_vma value,
1850 bfd **poldbfd,
1851 bfd_boolean *dynsym)
1852 {
1853 bfd_boolean type_change_ok;
1854 bfd_boolean size_change_ok;
1855 bfd_boolean skip;
1856 char *shortname;
1857 struct elf_link_hash_entry *hi;
1858 struct bfd_link_hash_entry *bh;
1859 const struct elf_backend_data *bed;
1860 bfd_boolean collect;
1861 bfd_boolean dynamic;
1862 bfd *override;
1863 char *p;
1864 size_t len, shortlen;
1865 asection *tmp_sec;
1866 bfd_boolean matched;
1867
1868 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1869 return TRUE;
1870
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p = strchr (name, ELF_VER_CHR);
1876 if (h->versioned == unknown)
1877 {
1878 if (p == NULL)
1879 {
1880 h->versioned = unversioned;
1881 return TRUE;
1882 }
1883 else
1884 {
1885 if (p[1] != ELF_VER_CHR)
1886 {
1887 h->versioned = versioned_hidden;
1888 return TRUE;
1889 }
1890 else
1891 h->versioned = versioned;
1892 }
1893 }
1894 else
1895 {
1896 /* PR ld/19073: We may see an unversioned definition after the
1897 default version. */
1898 if (p == NULL)
1899 return TRUE;
1900 }
1901
1902 bed = get_elf_backend_data (abfd);
1903 collect = bed->collect;
1904 dynamic = (abfd->flags & DYNAMIC) != 0;
1905
1906 shortlen = p - name;
1907 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1908 if (shortname == NULL)
1909 return FALSE;
1910 memcpy (shortname, name, shortlen);
1911 shortname[shortlen] = '\0';
1912
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok = FALSE;
1918 size_change_ok = FALSE;
1919 matched = TRUE;
1920 tmp_sec = sec;
1921 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1922 &hi, poldbfd, NULL, NULL, &skip, &override,
1923 &type_change_ok, &size_change_ok, &matched))
1924 return FALSE;
1925
1926 if (skip)
1927 goto nondefault;
1928
1929 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1930 {
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1937 {
1938 bfd_boolean hide;
1939
1940 hi->verinfo.vertree
1941 = bfd_find_version_for_sym (info->version_info,
1942 hi->root.root.string, &hide);
1943 if (hi->verinfo.vertree != NULL && hide)
1944 {
1945 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1946 goto nondefault;
1947 }
1948 }
1949 if (hi->verinfo.vertree != NULL
1950 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1951 goto nondefault;
1952 }
1953
1954 if (! override)
1955 {
1956 /* Add the default symbol if not performing a relocatable link. */
1957 if (! bfd_link_relocatable (info))
1958 {
1959 bh = &hi->root;
1960 if (bh->type == bfd_link_hash_defined
1961 && bh->u.def.section->owner != NULL
1962 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1963 {
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1966 it. */
1967 bh->type = bfd_link_hash_undefined;
1968 bh->u.undef.abfd = bh->u.def.section->owner;
1969 }
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info, abfd, shortname, BSF_INDIRECT,
1972 bfd_ind_section_ptr,
1973 0, name, FALSE, collect, &bh)))
1974 return FALSE;
1975 hi = (struct elf_link_hash_entry *) bh;
1976 }
1977 }
1978 else
1979 {
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1985
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1989
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1997
1998 while (hi->root.type == bfd_link_hash_indirect
1999 || hi->root.type == bfd_link_hash_warning)
2000 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2001
2002 h->root.type = bfd_link_hash_indirect;
2003 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2004 if (h->def_dynamic)
2005 {
2006 h->def_dynamic = 0;
2007 hi->ref_dynamic = 1;
2008 if (hi->ref_regular
2009 || hi->def_regular)
2010 {
2011 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2012 return FALSE;
2013 }
2014 }
2015
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2018 hi = h;
2019 }
2020
2021 /* Check if HI is a warning symbol. */
2022 if (hi->root.type == bfd_link_hash_warning)
2023 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2024
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2028
2029 if (hi->root.type == bfd_link_hash_indirect)
2030 {
2031 struct elf_link_hash_entry *ht;
2032
2033 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2034 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2035
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2039
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2044 hi->dynamic_def |= ht->dynamic_def;
2045
2046 /* See if the new flags lead us to realize that the symbol must
2047 be dynamic. */
2048 if (! *dynsym)
2049 {
2050 if (! dynamic)
2051 {
2052 if (! bfd_link_executable (info)
2053 || hi->def_dynamic
2054 || hi->ref_dynamic)
2055 *dynsym = TRUE;
2056 }
2057 else
2058 {
2059 if (hi->ref_regular)
2060 *dynsym = TRUE;
2061 }
2062 }
2063 }
2064
2065 /* We also need to define an indirection from the nondefault version
2066 of the symbol. */
2067
2068 nondefault:
2069 len = strlen (name);
2070 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2071 if (shortname == NULL)
2072 return FALSE;
2073 memcpy (shortname, name, shortlen);
2074 memcpy (shortname + shortlen, p + 1, len - shortlen);
2075
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok = FALSE;
2078 size_change_ok = FALSE;
2079 tmp_sec = sec;
2080 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2081 &hi, poldbfd, NULL, NULL, &skip, &override,
2082 &type_change_ok, &size_change_ok, &matched))
2083 return FALSE;
2084
2085 if (skip)
2086 {
2087 if (!dynamic
2088 && h->root.type == bfd_link_hash_defweak
2089 && hi->root.type == bfd_link_hash_defined)
2090 {
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h->root.type = bfd_link_hash_defined;
2097 h->root.u.def.section = hi->root.u.def.section;
2098 h->root.u.def.value = hi->root.u.def.value;
2099 hi->root.type = bfd_link_hash_indirect;
2100 hi->root.u.i.link = &h->root;
2101 }
2102 else
2103 return TRUE;
2104 }
2105 else if (override)
2106 {
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
2109 overridden by a versioned definition. */
2110 if (hi->root.type != bfd_link_hash_defined
2111 && hi->root.type != bfd_link_hash_defweak)
2112 _bfd_error_handler
2113 /* xgettext:c-format */
2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2115 abfd, shortname);
2116 return TRUE;
2117 }
2118 else
2119 {
2120 bh = &hi->root;
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info, abfd, shortname, BSF_INDIRECT,
2123 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2124 return FALSE;
2125 hi = (struct elf_link_hash_entry *) bh;
2126 }
2127
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi->root.type == bfd_link_hash_indirect)
2132 {
2133 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2134 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2135 hi->dynamic_def |= h->dynamic_def;
2136
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2139 @@VER symbol. */
2140 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2141
2142 /* See if the new flags lead us to realize that the symbol
2143 must be dynamic. */
2144 if (! *dynsym)
2145 {
2146 if (! dynamic)
2147 {
2148 if (! bfd_link_executable (info)
2149 || hi->ref_dynamic)
2150 *dynsym = TRUE;
2151 }
2152 else
2153 {
2154 if (hi->ref_regular)
2155 *dynsym = TRUE;
2156 }
2157 }
2158 }
2159
2160 return TRUE;
2161 }
2162 \f
2163 /* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2165
2166 static bfd_boolean
2167 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2168 {
2169 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2170
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h->root.type == bfd_link_hash_indirect)
2173 return TRUE;
2174
2175 /* Ignore this if we won't export it. */
2176 if (!eif->info->export_dynamic && !h->dynamic)
2177 return TRUE;
2178
2179 if (h->dynindx == -1
2180 && (h->def_regular || h->ref_regular)
2181 && ! bfd_hide_sym_by_version (eif->info->version_info,
2182 h->root.root.string))
2183 {
2184 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2185 {
2186 eif->failed = TRUE;
2187 return FALSE;
2188 }
2189 }
2190
2191 return TRUE;
2192 }
2193 \f
2194 /* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2198
2199 static bfd_boolean
2200 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2201 void *data)
2202 {
2203 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2204 Elf_Internal_Verneed *t;
2205 Elf_Internal_Vernaux *a;
2206 size_t amt;
2207
2208 /* We only care about symbols defined in shared objects with version
2209 information. */
2210 if (!h->def_dynamic
2211 || h->def_regular
2212 || h->dynindx == -1
2213 || h->verinfo.verdef == NULL
2214 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2215 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2216 return TRUE;
2217
2218 /* See if we already know about this version. */
2219 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2220 t != NULL;
2221 t = t->vn_nextref)
2222 {
2223 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2224 continue;
2225
2226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2227 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2228 return TRUE;
2229
2230 break;
2231 }
2232
2233 /* This is a new version. Add it to tree we are building. */
2234
2235 if (t == NULL)
2236 {
2237 amt = sizeof *t;
2238 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2239 if (t == NULL)
2240 {
2241 rinfo->failed = TRUE;
2242 return FALSE;
2243 }
2244
2245 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2246 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2247 elf_tdata (rinfo->info->output_bfd)->verref = t;
2248 }
2249
2250 amt = sizeof *a;
2251 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2252 if (a == NULL)
2253 {
2254 rinfo->failed = TRUE;
2255 return FALSE;
2256 }
2257
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2261 fixed. */
2262 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2263
2264 a->vna_flags = h->verinfo.verdef->vd_flags;
2265 a->vna_nextptr = t->vn_auxptr;
2266
2267 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2268 ++rinfo->vers;
2269
2270 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2271
2272 t->vn_auxptr = a;
2273
2274 return TRUE;
2275 }
2276
2277 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2279
2280 static bfd_boolean
2281 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2282 struct elf_link_hash_entry *h,
2283 const char *version_p,
2284 struct bfd_elf_version_tree **t_p,
2285 bfd_boolean *hide)
2286 {
2287 struct bfd_elf_version_tree *t;
2288
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t = info->version_info; t != NULL; t = t->next)
2291 {
2292 if (strcmp (t->name, version_p) == 0)
2293 {
2294 size_t len;
2295 char *alc;
2296 struct bfd_elf_version_expr *d;
2297
2298 len = version_p - h->root.root.string;
2299 alc = (char *) bfd_malloc (len);
2300 if (alc == NULL)
2301 return FALSE;
2302 memcpy (alc, h->root.root.string, len - 1);
2303 alc[len - 1] = '\0';
2304 if (alc[len - 2] == ELF_VER_CHR)
2305 alc[len - 2] = '\0';
2306
2307 h->verinfo.vertree = t;
2308 t->used = TRUE;
2309 d = NULL;
2310
2311 if (t->globals.list != NULL)
2312 d = (*t->match) (&t->globals, NULL, alc);
2313
2314 /* See if there is anything to force this symbol to
2315 local scope. */
2316 if (d == NULL && t->locals.list != NULL)
2317 {
2318 d = (*t->match) (&t->locals, NULL, alc);
2319 if (d != NULL
2320 && h->dynindx != -1
2321 && ! info->export_dynamic)
2322 *hide = TRUE;
2323 }
2324
2325 free (alc);
2326 break;
2327 }
2328 }
2329
2330 *t_p = t;
2331
2332 return TRUE;
2333 }
2334
2335 /* Return TRUE if the symbol H is hidden by version script. */
2336
2337 bfd_boolean
2338 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2339 struct elf_link_hash_entry *h)
2340 {
2341 const char *p;
2342 bfd_boolean hide = FALSE;
2343 const struct elf_backend_data *bed
2344 = get_elf_backend_data (info->output_bfd);
2345
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2348 return TRUE;
2349
2350 p = strchr (h->root.root.string, ELF_VER_CHR);
2351 if (p != NULL && h->verinfo.vertree == NULL)
2352 {
2353 struct bfd_elf_version_tree *t;
2354
2355 ++p;
2356 if (*p == ELF_VER_CHR)
2357 ++p;
2358
2359 if (*p != '\0'
2360 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2361 && hide)
2362 {
2363 if (hide)
2364 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2365 return TRUE;
2366 }
2367 }
2368
2369 /* If we don't have a version for this symbol, see if we can find
2370 something. */
2371 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2372 {
2373 h->verinfo.vertree
2374 = bfd_find_version_for_sym (info->version_info,
2375 h->root.root.string, &hide);
2376 if (h->verinfo.vertree != NULL && hide)
2377 {
2378 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2379 return TRUE;
2380 }
2381 }
2382
2383 return FALSE;
2384 }
2385
2386 /* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2390
2391 static bfd_boolean
2392 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2393 {
2394 struct elf_info_failed *sinfo;
2395 struct bfd_link_info *info;
2396 const struct elf_backend_data *bed;
2397 struct elf_info_failed eif;
2398 char *p;
2399 bfd_boolean hide;
2400
2401 sinfo = (struct elf_info_failed *) data;
2402 info = sinfo->info;
2403
2404 /* Fix the symbol flags. */
2405 eif.failed = FALSE;
2406 eif.info = info;
2407 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2408 {
2409 if (eif.failed)
2410 sinfo->failed = TRUE;
2411 return FALSE;
2412 }
2413
2414 bed = get_elf_backend_data (info->output_bfd);
2415
2416 /* We only need version numbers for symbols defined in regular
2417 objects. */
2418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2419 {
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h->root.type == bfd_link_hash_defined
2422 || h->root.type == bfd_link_hash_defweak)
2423 && discarded_section (h->root.u.def.section))
2424 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2425 return TRUE;
2426 }
2427
2428 hide = FALSE;
2429 p = strchr (h->root.root.string, ELF_VER_CHR);
2430 if (p != NULL && h->verinfo.vertree == NULL)
2431 {
2432 struct bfd_elf_version_tree *t;
2433
2434 ++p;
2435 if (*p == ELF_VER_CHR)
2436 ++p;
2437
2438 /* If there is no version string, we can just return out. */
2439 if (*p == '\0')
2440 return TRUE;
2441
2442 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2443 {
2444 sinfo->failed = TRUE;
2445 return FALSE;
2446 }
2447
2448 if (hide)
2449 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2450
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
2453 if (t == NULL && bfd_link_executable (info))
2454 {
2455 struct bfd_elf_version_tree **pp;
2456 int version_index;
2457
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h->dynindx == -1)
2461 return TRUE;
2462
2463 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2464 sizeof *t);
2465 if (t == NULL)
2466 {
2467 sinfo->failed = TRUE;
2468 return FALSE;
2469 }
2470
2471 t->name = p;
2472 t->name_indx = (unsigned int) -1;
2473 t->used = TRUE;
2474
2475 version_index = 1;
2476 /* Don't count anonymous version tag. */
2477 if (sinfo->info->version_info != NULL
2478 && sinfo->info->version_info->vernum == 0)
2479 version_index = 0;
2480 for (pp = &sinfo->info->version_info;
2481 *pp != NULL;
2482 pp = &(*pp)->next)
2483 ++version_index;
2484 t->vernum = version_index;
2485
2486 *pp = t;
2487
2488 h->verinfo.vertree = t;
2489 }
2490 else if (t == NULL)
2491 {
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
2494 _bfd_error_handler
2495 /* xgettext:c-format */
2496 (_("%pB: version node not found for symbol %s"),
2497 info->output_bfd, h->root.root.string);
2498 bfd_set_error (bfd_error_bad_value);
2499 sinfo->failed = TRUE;
2500 return FALSE;
2501 }
2502 }
2503
2504 /* If we don't have a version for this symbol, see if we can find
2505 something. */
2506 if (!hide
2507 && h->verinfo.vertree == NULL
2508 && sinfo->info->version_info != NULL)
2509 {
2510 h->verinfo.vertree
2511 = bfd_find_version_for_sym (sinfo->info->version_info,
2512 h->root.root.string, &hide);
2513 if (h->verinfo.vertree != NULL && hide)
2514 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2515 }
2516
2517 return TRUE;
2518 }
2519 \f
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2526
2527 Returns FALSE if something goes wrong. */
2528
2529 static bfd_boolean
2530 elf_link_read_relocs_from_section (bfd *abfd,
2531 asection *sec,
2532 Elf_Internal_Shdr *shdr,
2533 void *external_relocs,
2534 Elf_Internal_Rela *internal_relocs)
2535 {
2536 const struct elf_backend_data *bed;
2537 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2538 const bfd_byte *erela;
2539 const bfd_byte *erelaend;
2540 Elf_Internal_Rela *irela;
2541 Elf_Internal_Shdr *symtab_hdr;
2542 size_t nsyms;
2543
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2546 return FALSE;
2547
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2550 return FALSE;
2551
2552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2553 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2554
2555 bed = get_elf_backend_data (abfd);
2556
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr->sh_entsize == bed->s->sizeof_rel)
2559 swap_in = bed->s->swap_reloc_in;
2560 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2561 swap_in = bed->s->swap_reloca_in;
2562 else
2563 {
2564 bfd_set_error (bfd_error_wrong_format);
2565 return FALSE;
2566 }
2567
2568 erela = (const bfd_byte *) external_relocs;
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2572 irela = internal_relocs;
2573 while (erela <= erelaend)
2574 {
2575 bfd_vma r_symndx;
2576
2577 (*swap_in) (abfd, erela, irela);
2578 r_symndx = ELF32_R_SYM (irela->r_info);
2579 if (bed->s->arch_size == 64)
2580 r_symndx >>= 24;
2581 if (nsyms > 0)
2582 {
2583 if ((size_t) r_symndx >= nsyms)
2584 {
2585 _bfd_error_handler
2586 /* xgettext:c-format */
2587 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2588 " for offset %#" PRIx64 " in section `%pA'"),
2589 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2590 (uint64_t) irela->r_offset, sec);
2591 bfd_set_error (bfd_error_bad_value);
2592 return FALSE;
2593 }
2594 }
2595 else if (r_symndx != STN_UNDEF)
2596 {
2597 _bfd_error_handler
2598 /* xgettext:c-format */
2599 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2600 " for offset %#" PRIx64 " in section `%pA'"
2601 " when the object file has no symbol table"),
2602 abfd, (uint64_t) r_symndx,
2603 (uint64_t) irela->r_offset, sec);
2604 bfd_set_error (bfd_error_bad_value);
2605 return FALSE;
2606 }
2607 irela += bed->s->int_rels_per_ext_rel;
2608 erela += shdr->sh_entsize;
2609 }
2610
2611 return TRUE;
2612 }
2613
2614 /* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
2622 RELA_HDR relocations. */
2623
2624 Elf_Internal_Rela *
2625 _bfd_elf_link_read_relocs (bfd *abfd,
2626 asection *o,
2627 void *external_relocs,
2628 Elf_Internal_Rela *internal_relocs,
2629 bfd_boolean keep_memory)
2630 {
2631 void *alloc1 = NULL;
2632 Elf_Internal_Rela *alloc2 = NULL;
2633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2634 struct bfd_elf_section_data *esdo = elf_section_data (o);
2635 Elf_Internal_Rela *internal_rela_relocs;
2636
2637 if (esdo->relocs != NULL)
2638 return esdo->relocs;
2639
2640 if (o->reloc_count == 0)
2641 return NULL;
2642
2643 if (internal_relocs == NULL)
2644 {
2645 bfd_size_type size;
2646
2647 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2648 if (keep_memory)
2649 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2650 else
2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2652 if (internal_relocs == NULL)
2653 goto error_return;
2654 }
2655
2656 if (external_relocs == NULL)
2657 {
2658 bfd_size_type size = 0;
2659
2660 if (esdo->rel.hdr)
2661 size += esdo->rel.hdr->sh_size;
2662 if (esdo->rela.hdr)
2663 size += esdo->rela.hdr->sh_size;
2664
2665 alloc1 = bfd_malloc (size);
2666 if (alloc1 == NULL)
2667 goto error_return;
2668 external_relocs = alloc1;
2669 }
2670
2671 internal_rela_relocs = internal_relocs;
2672 if (esdo->rel.hdr)
2673 {
2674 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2675 external_relocs,
2676 internal_relocs))
2677 goto error_return;
2678 external_relocs = (((bfd_byte *) external_relocs)
2679 + esdo->rel.hdr->sh_size);
2680 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2681 * bed->s->int_rels_per_ext_rel);
2682 }
2683
2684 if (esdo->rela.hdr
2685 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2686 external_relocs,
2687 internal_rela_relocs)))
2688 goto error_return;
2689
2690 /* Cache the results for next time, if we can. */
2691 if (keep_memory)
2692 esdo->relocs = internal_relocs;
2693
2694 free (alloc1);
2695
2696 /* Don't free alloc2, since if it was allocated we are passing it
2697 back (under the name of internal_relocs). */
2698
2699 return internal_relocs;
2700
2701 error_return:
2702 free (alloc1);
2703 if (alloc2 != NULL)
2704 {
2705 if (keep_memory)
2706 bfd_release (abfd, alloc2);
2707 else
2708 free (alloc2);
2709 }
2710 return NULL;
2711 }
2712
2713 /* Compute the size of, and allocate space for, REL_HDR which is the
2714 section header for a section containing relocations for O. */
2715
2716 static bfd_boolean
2717 _bfd_elf_link_size_reloc_section (bfd *abfd,
2718 struct bfd_elf_section_reloc_data *reldata)
2719 {
2720 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2721
2722 /* That allows us to calculate the size of the section. */
2723 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2724
2725 /* The contents field must last into write_object_contents, so we
2726 allocate it with bfd_alloc rather than malloc. Also since we
2727 cannot be sure that the contents will actually be filled in,
2728 we zero the allocated space. */
2729 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2730 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2731 return FALSE;
2732
2733 if (reldata->hashes == NULL && reldata->count)
2734 {
2735 struct elf_link_hash_entry **p;
2736
2737 p = ((struct elf_link_hash_entry **)
2738 bfd_zmalloc (reldata->count * sizeof (*p)));
2739 if (p == NULL)
2740 return FALSE;
2741
2742 reldata->hashes = p;
2743 }
2744
2745 return TRUE;
2746 }
2747
2748 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2749 originated from the section given by INPUT_REL_HDR) to the
2750 OUTPUT_BFD. */
2751
2752 bfd_boolean
2753 _bfd_elf_link_output_relocs (bfd *output_bfd,
2754 asection *input_section,
2755 Elf_Internal_Shdr *input_rel_hdr,
2756 Elf_Internal_Rela *internal_relocs,
2757 struct elf_link_hash_entry **rel_hash
2758 ATTRIBUTE_UNUSED)
2759 {
2760 Elf_Internal_Rela *irela;
2761 Elf_Internal_Rela *irelaend;
2762 bfd_byte *erel;
2763 struct bfd_elf_section_reloc_data *output_reldata;
2764 asection *output_section;
2765 const struct elf_backend_data *bed;
2766 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2767 struct bfd_elf_section_data *esdo;
2768
2769 output_section = input_section->output_section;
2770
2771 bed = get_elf_backend_data (output_bfd);
2772 esdo = elf_section_data (output_section);
2773 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2774 {
2775 output_reldata = &esdo->rel;
2776 swap_out = bed->s->swap_reloc_out;
2777 }
2778 else if (esdo->rela.hdr
2779 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2780 {
2781 output_reldata = &esdo->rela;
2782 swap_out = bed->s->swap_reloca_out;
2783 }
2784 else
2785 {
2786 _bfd_error_handler
2787 /* xgettext:c-format */
2788 (_("%pB: relocation size mismatch in %pB section %pA"),
2789 output_bfd, input_section->owner, input_section);
2790 bfd_set_error (bfd_error_wrong_format);
2791 return FALSE;
2792 }
2793
2794 erel = output_reldata->hdr->contents;
2795 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2796 irela = internal_relocs;
2797 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2798 * bed->s->int_rels_per_ext_rel);
2799 while (irela < irelaend)
2800 {
2801 (*swap_out) (output_bfd, irela, erel);
2802 irela += bed->s->int_rels_per_ext_rel;
2803 erel += input_rel_hdr->sh_entsize;
2804 }
2805
2806 /* Bump the counter, so that we know where to add the next set of
2807 relocations. */
2808 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2809
2810 return TRUE;
2811 }
2812 \f
2813 /* Make weak undefined symbols in PIE dynamic. */
2814
2815 bfd_boolean
2816 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2817 struct elf_link_hash_entry *h)
2818 {
2819 if (bfd_link_pie (info)
2820 && h->dynindx == -1
2821 && h->root.type == bfd_link_hash_undefweak)
2822 return bfd_elf_link_record_dynamic_symbol (info, h);
2823
2824 return TRUE;
2825 }
2826
2827 /* Fix up the flags for a symbol. This handles various cases which
2828 can only be fixed after all the input files are seen. This is
2829 currently called by both adjust_dynamic_symbol and
2830 assign_sym_version, which is unnecessary but perhaps more robust in
2831 the face of future changes. */
2832
2833 static bfd_boolean
2834 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2835 struct elf_info_failed *eif)
2836 {
2837 const struct elf_backend_data *bed;
2838
2839 /* If this symbol was mentioned in a non-ELF file, try to set
2840 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2841 permit a non-ELF file to correctly refer to a symbol defined in
2842 an ELF dynamic object. */
2843 if (h->non_elf)
2844 {
2845 while (h->root.type == bfd_link_hash_indirect)
2846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2847
2848 if (h->root.type != bfd_link_hash_defined
2849 && h->root.type != bfd_link_hash_defweak)
2850 {
2851 h->ref_regular = 1;
2852 h->ref_regular_nonweak = 1;
2853 }
2854 else
2855 {
2856 if (h->root.u.def.section->owner != NULL
2857 && (bfd_get_flavour (h->root.u.def.section->owner)
2858 == bfd_target_elf_flavour))
2859 {
2860 h->ref_regular = 1;
2861 h->ref_regular_nonweak = 1;
2862 }
2863 else
2864 h->def_regular = 1;
2865 }
2866
2867 if (h->dynindx == -1
2868 && (h->def_dynamic
2869 || h->ref_dynamic))
2870 {
2871 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2872 {
2873 eif->failed = TRUE;
2874 return FALSE;
2875 }
2876 }
2877 }
2878 else
2879 {
2880 /* Unfortunately, NON_ELF is only correct if the symbol
2881 was first seen in a non-ELF file. Fortunately, if the symbol
2882 was first seen in an ELF file, we're probably OK unless the
2883 symbol was defined in a non-ELF file. Catch that case here.
2884 FIXME: We're still in trouble if the symbol was first seen in
2885 a dynamic object, and then later in a non-ELF regular object. */
2886 if ((h->root.type == bfd_link_hash_defined
2887 || h->root.type == bfd_link_hash_defweak)
2888 && !h->def_regular
2889 && (h->root.u.def.section->owner != NULL
2890 ? (bfd_get_flavour (h->root.u.def.section->owner)
2891 != bfd_target_elf_flavour)
2892 : (bfd_is_abs_section (h->root.u.def.section)
2893 && !h->def_dynamic)))
2894 h->def_regular = 1;
2895 }
2896
2897 /* Backend specific symbol fixup. */
2898 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2899 if (bed->elf_backend_fixup_symbol
2900 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2901 return FALSE;
2902
2903 /* If this is a final link, and the symbol was defined as a common
2904 symbol in a regular object file, and there was no definition in
2905 any dynamic object, then the linker will have allocated space for
2906 the symbol in a common section but the DEF_REGULAR
2907 flag will not have been set. */
2908 if (h->root.type == bfd_link_hash_defined
2909 && !h->def_regular
2910 && h->ref_regular
2911 && !h->def_dynamic
2912 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2913 h->def_regular = 1;
2914
2915 /* Symbols defined in discarded sections shouldn't be dynamic. */
2916 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2917 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2918
2919 /* If a weak undefined symbol has non-default visibility, we also
2920 hide it from the dynamic linker. */
2921 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2922 && h->root.type == bfd_link_hash_undefweak)
2923 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2924
2925 /* A hidden versioned symbol in executable should be forced local if
2926 it is is locally defined, not referenced by shared library and not
2927 exported. */
2928 else if (bfd_link_executable (eif->info)
2929 && h->versioned == versioned_hidden
2930 && !eif->info->export_dynamic
2931 && !h->dynamic
2932 && !h->ref_dynamic
2933 && h->def_regular)
2934 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2935
2936 /* If -Bsymbolic was used (which means to bind references to global
2937 symbols to the definition within the shared object), and this
2938 symbol was defined in a regular object, then it actually doesn't
2939 need a PLT entry. Likewise, if the symbol has non-default
2940 visibility. If the symbol has hidden or internal visibility, we
2941 will force it local. */
2942 else if (h->needs_plt
2943 && bfd_link_pic (eif->info)
2944 && is_elf_hash_table (eif->info->hash)
2945 && (SYMBOLIC_BIND (eif->info, h)
2946 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2947 && h->def_regular)
2948 {
2949 bfd_boolean force_local;
2950
2951 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2952 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2953 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2954 }
2955
2956 /* If this is a weak defined symbol in a dynamic object, and we know
2957 the real definition in the dynamic object, copy interesting flags
2958 over to the real definition. */
2959 if (h->is_weakalias)
2960 {
2961 struct elf_link_hash_entry *def = weakdef (h);
2962
2963 /* If the real definition is defined by a regular object file,
2964 don't do anything special. See the longer description in
2965 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2966 bfd_link_hash_defined as it was when put on the alias list
2967 then it must have originally been a versioned symbol (for
2968 which a non-versioned indirect symbol is created) and later
2969 a definition for the non-versioned symbol is found. In that
2970 case the indirection is flipped with the versioned symbol
2971 becoming an indirect pointing at the non-versioned symbol.
2972 Thus, not an alias any more. */
2973 if (def->def_regular
2974 || def->root.type != bfd_link_hash_defined)
2975 {
2976 h = def;
2977 while ((h = h->u.alias) != def)
2978 h->is_weakalias = 0;
2979 }
2980 else
2981 {
2982 while (h->root.type == bfd_link_hash_indirect)
2983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2985 || h->root.type == bfd_link_hash_defweak);
2986 BFD_ASSERT (def->def_dynamic);
2987 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2988 }
2989 }
2990
2991 return TRUE;
2992 }
2993
2994 /* Make the backend pick a good value for a dynamic symbol. This is
2995 called via elf_link_hash_traverse, and also calls itself
2996 recursively. */
2997
2998 static bfd_boolean
2999 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3000 {
3001 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3002 struct elf_link_hash_table *htab;
3003 const struct elf_backend_data *bed;
3004
3005 if (! is_elf_hash_table (eif->info->hash))
3006 return FALSE;
3007
3008 /* Ignore indirect symbols. These are added by the versioning code. */
3009 if (h->root.type == bfd_link_hash_indirect)
3010 return TRUE;
3011
3012 /* Fix the symbol flags. */
3013 if (! _bfd_elf_fix_symbol_flags (h, eif))
3014 return FALSE;
3015
3016 htab = elf_hash_table (eif->info);
3017 bed = get_elf_backend_data (htab->dynobj);
3018
3019 if (h->root.type == bfd_link_hash_undefweak)
3020 {
3021 if (eif->info->dynamic_undefined_weak == 0)
3022 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
3023 else if (eif->info->dynamic_undefined_weak > 0
3024 && h->ref_regular
3025 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3026 && !bfd_hide_sym_by_version (eif->info->version_info,
3027 h->root.root.string))
3028 {
3029 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3030 {
3031 eif->failed = TRUE;
3032 return FALSE;
3033 }
3034 }
3035 }
3036
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
3044 if (!h->needs_plt
3045 && h->type != STT_GNU_IFUNC
3046 && (h->def_regular
3047 || !h->def_dynamic
3048 || (!h->ref_regular
3049 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3050 {
3051 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3052 return TRUE;
3053 }
3054
3055 /* If we've already adjusted this symbol, don't do it again. This
3056 can happen via a recursive call. */
3057 if (h->dynamic_adjusted)
3058 return TRUE;
3059
3060 /* Don't look at this symbol again. Note that we must set this
3061 after checking the above conditions, because we may look at a
3062 symbol once, decide not to do anything, and then get called
3063 recursively later after REF_REGULAR is set below. */
3064 h->dynamic_adjusted = 1;
3065
3066 /* If this is a weak definition, and we know a real definition, and
3067 the real symbol is not itself defined by a regular object file,
3068 then get a good value for the real definition. We handle the
3069 real symbol first, for the convenience of the backend routine.
3070
3071 Note that there is a confusing case here. If the real definition
3072 is defined by a regular object file, we don't get the real symbol
3073 from the dynamic object, but we do get the weak symbol. If the
3074 processor backend uses a COPY reloc, then if some routine in the
3075 dynamic object changes the real symbol, we will not see that
3076 change in the corresponding weak symbol. This is the way other
3077 ELF linkers work as well, and seems to be a result of the shared
3078 library model.
3079
3080 I will clarify this issue. Most SVR4 shared libraries define the
3081 variable _timezone and define timezone as a weak synonym. The
3082 tzset call changes _timezone. If you write
3083 extern int timezone;
3084 int _timezone = 5;
3085 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3086 you might expect that, since timezone is a synonym for _timezone,
3087 the same number will print both times. However, if the processor
3088 backend uses a COPY reloc, then actually timezone will be copied
3089 into your process image, and, since you define _timezone
3090 yourself, _timezone will not. Thus timezone and _timezone will
3091 wind up at different memory locations. The tzset call will set
3092 _timezone, leaving timezone unchanged. */
3093
3094 if (h->is_weakalias)
3095 {
3096 struct elf_link_hash_entry *def = weakdef (h);
3097
3098 /* If we get to this point, there is an implicit reference to
3099 the alias by a regular object file via the weak symbol H. */
3100 def->ref_regular = 1;
3101
3102 /* Ensure that the backend adjust_dynamic_symbol function sees
3103 the strong alias before H by recursively calling ourselves. */
3104 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3105 return FALSE;
3106 }
3107
3108 /* If a symbol has no type and no size and does not require a PLT
3109 entry, then we are probably about to do the wrong thing here: we
3110 are probably going to create a COPY reloc for an empty object.
3111 This case can arise when a shared object is built with assembly
3112 code, and the assembly code fails to set the symbol type. */
3113 if (h->size == 0
3114 && h->type == STT_NOTYPE
3115 && !h->needs_plt)
3116 _bfd_error_handler
3117 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3118 h->root.root.string);
3119
3120 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3121 {
3122 eif->failed = TRUE;
3123 return FALSE;
3124 }
3125
3126 return TRUE;
3127 }
3128
3129 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3130 DYNBSS. */
3131
3132 bfd_boolean
3133 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3134 struct elf_link_hash_entry *h,
3135 asection *dynbss)
3136 {
3137 unsigned int power_of_two;
3138 bfd_vma mask;
3139 asection *sec = h->root.u.def.section;
3140
3141 /* The section alignment of the definition is the maximum alignment
3142 requirement of symbols defined in the section. Since we don't
3143 know the symbol alignment requirement, we start with the
3144 maximum alignment and check low bits of the symbol address
3145 for the minimum alignment. */
3146 power_of_two = bfd_section_alignment (sec);
3147 mask = ((bfd_vma) 1 << power_of_two) - 1;
3148 while ((h->root.u.def.value & mask) != 0)
3149 {
3150 mask >>= 1;
3151 --power_of_two;
3152 }
3153
3154 if (power_of_two > bfd_section_alignment (dynbss))
3155 {
3156 /* Adjust the section alignment if needed. */
3157 if (!bfd_set_section_alignment (dynbss, power_of_two))
3158 return FALSE;
3159 }
3160
3161 /* We make sure that the symbol will be aligned properly. */
3162 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3163
3164 /* Define the symbol as being at this point in DYNBSS. */
3165 h->root.u.def.section = dynbss;
3166 h->root.u.def.value = dynbss->size;
3167
3168 /* Increment the size of DYNBSS to make room for the symbol. */
3169 dynbss->size += h->size;
3170
3171 /* No error if extern_protected_data is true. */
3172 if (h->protected_def
3173 && (!info->extern_protected_data
3174 || (info->extern_protected_data < 0
3175 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3176 info->callbacks->einfo
3177 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3178 h->root.root.string);
3179
3180 return TRUE;
3181 }
3182
3183 /* Adjust all external symbols pointing into SEC_MERGE sections
3184 to reflect the object merging within the sections. */
3185
3186 static bfd_boolean
3187 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3188 {
3189 asection *sec;
3190
3191 if ((h->root.type == bfd_link_hash_defined
3192 || h->root.type == bfd_link_hash_defweak)
3193 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3194 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3195 {
3196 bfd *output_bfd = (bfd *) data;
3197
3198 h->root.u.def.value =
3199 _bfd_merged_section_offset (output_bfd,
3200 &h->root.u.def.section,
3201 elf_section_data (sec)->sec_info,
3202 h->root.u.def.value);
3203 }
3204
3205 return TRUE;
3206 }
3207
3208 /* Returns false if the symbol referred to by H should be considered
3209 to resolve local to the current module, and true if it should be
3210 considered to bind dynamically. */
3211
3212 bfd_boolean
3213 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3214 struct bfd_link_info *info,
3215 bfd_boolean not_local_protected)
3216 {
3217 bfd_boolean binding_stays_local_p;
3218 const struct elf_backend_data *bed;
3219 struct elf_link_hash_table *hash_table;
3220
3221 if (h == NULL)
3222 return FALSE;
3223
3224 while (h->root.type == bfd_link_hash_indirect
3225 || h->root.type == bfd_link_hash_warning)
3226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3227
3228 /* If it was forced local, then clearly it's not dynamic. */
3229 if (h->dynindx == -1)
3230 return FALSE;
3231 if (h->forced_local)
3232 return FALSE;
3233
3234 /* Identify the cases where name binding rules say that a
3235 visible symbol resolves locally. */
3236 binding_stays_local_p = (bfd_link_executable (info)
3237 || SYMBOLIC_BIND (info, h));
3238
3239 switch (ELF_ST_VISIBILITY (h->other))
3240 {
3241 case STV_INTERNAL:
3242 case STV_HIDDEN:
3243 return FALSE;
3244
3245 case STV_PROTECTED:
3246 hash_table = elf_hash_table (info);
3247 if (!is_elf_hash_table (hash_table))
3248 return FALSE;
3249
3250 bed = get_elf_backend_data (hash_table->dynobj);
3251
3252 /* Proper resolution for function pointer equality may require
3253 that these symbols perhaps be resolved dynamically, even though
3254 we should be resolving them to the current module. */
3255 if (!not_local_protected || !bed->is_function_type (h->type))
3256 binding_stays_local_p = TRUE;
3257 break;
3258
3259 default:
3260 break;
3261 }
3262
3263 /* If it isn't defined locally, then clearly it's dynamic. */
3264 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3265 return TRUE;
3266
3267 /* Otherwise, the symbol is dynamic if binding rules don't tell
3268 us that it remains local. */
3269 return !binding_stays_local_p;
3270 }
3271
3272 /* Return true if the symbol referred to by H should be considered
3273 to resolve local to the current module, and false otherwise. Differs
3274 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3275 undefined symbols. The two functions are virtually identical except
3276 for the place where dynindx == -1 is tested. If that test is true,
3277 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3278 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3279 defined symbols.
3280 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3281 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3282 treatment of undefined weak symbols. For those that do not make
3283 undefined weak symbols dynamic, both functions may return false. */
3284
3285 bfd_boolean
3286 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3287 struct bfd_link_info *info,
3288 bfd_boolean local_protected)
3289 {
3290 const struct elf_backend_data *bed;
3291 struct elf_link_hash_table *hash_table;
3292
3293 /* If it's a local sym, of course we resolve locally. */
3294 if (h == NULL)
3295 return TRUE;
3296
3297 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3298 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3299 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3300 return TRUE;
3301
3302 /* Forced local symbols resolve locally. */
3303 if (h->forced_local)
3304 return TRUE;
3305
3306 /* Common symbols that become definitions don't get the DEF_REGULAR
3307 flag set, so test it first, and don't bail out. */
3308 if (ELF_COMMON_DEF_P (h))
3309 /* Do nothing. */;
3310 /* If we don't have a definition in a regular file, then we can't
3311 resolve locally. The sym is either undefined or dynamic. */
3312 else if (!h->def_regular)
3313 return FALSE;
3314
3315 /* Non-dynamic symbols resolve locally. */
3316 if (h->dynindx == -1)
3317 return TRUE;
3318
3319 /* At this point, we know the symbol is defined and dynamic. In an
3320 executable it must resolve locally, likewise when building symbolic
3321 shared libraries. */
3322 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3323 return TRUE;
3324
3325 /* Now deal with defined dynamic symbols in shared libraries. Ones
3326 with default visibility might not resolve locally. */
3327 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3328 return FALSE;
3329
3330 hash_table = elf_hash_table (info);
3331 if (!is_elf_hash_table (hash_table))
3332 return TRUE;
3333
3334 bed = get_elf_backend_data (hash_table->dynobj);
3335
3336 /* If extern_protected_data is false, STV_PROTECTED non-function
3337 symbols are local. */
3338 if ((!info->extern_protected_data
3339 || (info->extern_protected_data < 0
3340 && !bed->extern_protected_data))
3341 && !bed->is_function_type (h->type))
3342 return TRUE;
3343
3344 /* Function pointer equality tests may require that STV_PROTECTED
3345 symbols be treated as dynamic symbols. If the address of a
3346 function not defined in an executable is set to that function's
3347 plt entry in the executable, then the address of the function in
3348 a shared library must also be the plt entry in the executable. */
3349 return local_protected;
3350 }
3351
3352 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3353 aligned. Returns the first TLS output section. */
3354
3355 struct bfd_section *
3356 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3357 {
3358 struct bfd_section *sec, *tls;
3359 unsigned int align = 0;
3360
3361 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3362 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3363 break;
3364 tls = sec;
3365
3366 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3367 if (sec->alignment_power > align)
3368 align = sec->alignment_power;
3369
3370 elf_hash_table (info)->tls_sec = tls;
3371
3372 /* Ensure the alignment of the first section (usually .tdata) is the largest
3373 alignment, so that the tls segment starts aligned. */
3374 if (tls != NULL)
3375 tls->alignment_power = align;
3376
3377 return tls;
3378 }
3379
3380 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3381 static bfd_boolean
3382 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3383 Elf_Internal_Sym *sym)
3384 {
3385 const struct elf_backend_data *bed;
3386
3387 /* Local symbols do not count, but target specific ones might. */
3388 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3389 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3390 return FALSE;
3391
3392 bed = get_elf_backend_data (abfd);
3393 /* Function symbols do not count. */
3394 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3395 return FALSE;
3396
3397 /* If the section is undefined, then so is the symbol. */
3398 if (sym->st_shndx == SHN_UNDEF)
3399 return FALSE;
3400
3401 /* If the symbol is defined in the common section, then
3402 it is a common definition and so does not count. */
3403 if (bed->common_definition (sym))
3404 return FALSE;
3405
3406 /* If the symbol is in a target specific section then we
3407 must rely upon the backend to tell us what it is. */
3408 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3409 /* FIXME - this function is not coded yet:
3410
3411 return _bfd_is_global_symbol_definition (abfd, sym);
3412
3413 Instead for now assume that the definition is not global,
3414 Even if this is wrong, at least the linker will behave
3415 in the same way that it used to do. */
3416 return FALSE;
3417
3418 return TRUE;
3419 }
3420
3421 /* Search the symbol table of the archive element of the archive ABFD
3422 whose archive map contains a mention of SYMDEF, and determine if
3423 the symbol is defined in this element. */
3424 static bfd_boolean
3425 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3426 {
3427 Elf_Internal_Shdr * hdr;
3428 size_t symcount;
3429 size_t extsymcount;
3430 size_t extsymoff;
3431 Elf_Internal_Sym *isymbuf;
3432 Elf_Internal_Sym *isym;
3433 Elf_Internal_Sym *isymend;
3434 bfd_boolean result;
3435
3436 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3437 if (abfd == NULL)
3438 return FALSE;
3439
3440 if (! bfd_check_format (abfd, bfd_object))
3441 return FALSE;
3442
3443 /* Select the appropriate symbol table. If we don't know if the
3444 object file is an IR object, give linker LTO plugin a chance to
3445 get the correct symbol table. */
3446 if (abfd->plugin_format == bfd_plugin_yes
3447 #if BFD_SUPPORTS_PLUGINS
3448 || (abfd->plugin_format == bfd_plugin_unknown
3449 && bfd_link_plugin_object_p (abfd))
3450 #endif
3451 )
3452 {
3453 /* Use the IR symbol table if the object has been claimed by
3454 plugin. */
3455 abfd = abfd->plugin_dummy_bfd;
3456 hdr = &elf_tdata (abfd)->symtab_hdr;
3457 }
3458 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3459 hdr = &elf_tdata (abfd)->symtab_hdr;
3460 else
3461 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3462
3463 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3464
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols. */
3467 if (elf_bad_symtab (abfd))
3468 {
3469 extsymcount = symcount;
3470 extsymoff = 0;
3471 }
3472 else
3473 {
3474 extsymcount = symcount - hdr->sh_info;
3475 extsymoff = hdr->sh_info;
3476 }
3477
3478 if (extsymcount == 0)
3479 return FALSE;
3480
3481 /* Read in the symbol table. */
3482 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3483 NULL, NULL, NULL);
3484 if (isymbuf == NULL)
3485 return FALSE;
3486
3487 /* Scan the symbol table looking for SYMDEF. */
3488 result = FALSE;
3489 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3490 {
3491 const char *name;
3492
3493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3494 isym->st_name);
3495 if (name == NULL)
3496 break;
3497
3498 if (strcmp (name, symdef->name) == 0)
3499 {
3500 result = is_global_data_symbol_definition (abfd, isym);
3501 break;
3502 }
3503 }
3504
3505 free (isymbuf);
3506
3507 return result;
3508 }
3509 \f
3510 /* Add an entry to the .dynamic table. */
3511
3512 bfd_boolean
3513 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3514 bfd_vma tag,
3515 bfd_vma val)
3516 {
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s;
3520 bfd_size_type newsize;
3521 bfd_byte *newcontents;
3522 Elf_Internal_Dyn dyn;
3523
3524 hash_table = elf_hash_table (info);
3525 if (! is_elf_hash_table (hash_table))
3526 return FALSE;
3527
3528 if (tag == DT_RELA || tag == DT_REL)
3529 hash_table->dynamic_relocs = TRUE;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3533 BFD_ASSERT (s != NULL);
3534
3535 newsize = s->size + bed->s->sizeof_dyn;
3536 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3537 if (newcontents == NULL)
3538 return FALSE;
3539
3540 dyn.d_tag = tag;
3541 dyn.d_un.d_val = val;
3542 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3543
3544 s->size = newsize;
3545 s->contents = newcontents;
3546
3547 return TRUE;
3548 }
3549
3550 /* Strip zero-sized dynamic sections. */
3551
3552 bfd_boolean
3553 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3554 {
3555 struct elf_link_hash_table *hash_table;
3556 const struct elf_backend_data *bed;
3557 asection *s, *sdynamic, **pp;
3558 asection *rela_dyn, *rel_dyn;
3559 Elf_Internal_Dyn dyn;
3560 bfd_byte *extdyn, *next;
3561 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3562 bfd_boolean strip_zero_sized;
3563 bfd_boolean strip_zero_sized_plt;
3564
3565 if (bfd_link_relocatable (info))
3566 return TRUE;
3567
3568 hash_table = elf_hash_table (info);
3569 if (!is_elf_hash_table (hash_table))
3570 return FALSE;
3571
3572 if (!hash_table->dynobj)
3573 return TRUE;
3574
3575 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3576 if (!sdynamic)
3577 return TRUE;
3578
3579 bed = get_elf_backend_data (hash_table->dynobj);
3580 swap_dyn_in = bed->s->swap_dyn_in;
3581
3582 strip_zero_sized = FALSE;
3583 strip_zero_sized_plt = FALSE;
3584
3585 /* Strip zero-sized dynamic sections. */
3586 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3587 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3588 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3589 if (s->size == 0
3590 && (s == rela_dyn
3591 || s == rel_dyn
3592 || s == hash_table->srelplt->output_section
3593 || s == hash_table->splt->output_section))
3594 {
3595 *pp = s->next;
3596 info->output_bfd->section_count--;
3597 strip_zero_sized = TRUE;
3598 if (s == rela_dyn)
3599 s = rela_dyn;
3600 if (s == rel_dyn)
3601 s = rel_dyn;
3602 else if (s == hash_table->splt->output_section)
3603 {
3604 s = hash_table->splt;
3605 strip_zero_sized_plt = TRUE;
3606 }
3607 else
3608 s = hash_table->srelplt;
3609 s->flags |= SEC_EXCLUDE;
3610 s->output_section = bfd_abs_section_ptr;
3611 }
3612 else
3613 pp = &s->next;
3614
3615 if (strip_zero_sized_plt)
3616 for (extdyn = sdynamic->contents;
3617 extdyn < sdynamic->contents + sdynamic->size;
3618 extdyn = next)
3619 {
3620 next = extdyn + bed->s->sizeof_dyn;
3621 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3622 switch (dyn.d_tag)
3623 {
3624 default:
3625 break;
3626 case DT_JMPREL:
3627 case DT_PLTRELSZ:
3628 case DT_PLTREL:
3629 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3630 the procedure linkage table (the .plt section) has been
3631 removed. */
3632 memmove (extdyn, next,
3633 sdynamic->size - (next - sdynamic->contents));
3634 next = extdyn;
3635 }
3636 }
3637
3638 if (strip_zero_sized)
3639 {
3640 /* Regenerate program headers. */
3641 elf_seg_map (info->output_bfd) = NULL;
3642 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3643 }
3644
3645 return TRUE;
3646 }
3647
3648 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3649 1 if a DT_NEEDED tag already exists, and 0 on success. */
3650
3651 int
3652 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3653 {
3654 struct elf_link_hash_table *hash_table;
3655 size_t strindex;
3656 const char *soname;
3657
3658 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3659 return -1;
3660
3661 hash_table = elf_hash_table (info);
3662 soname = elf_dt_name (abfd);
3663 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3664 if (strindex == (size_t) -1)
3665 return -1;
3666
3667 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3668 {
3669 asection *sdyn;
3670 const struct elf_backend_data *bed;
3671 bfd_byte *extdyn;
3672
3673 bed = get_elf_backend_data (hash_table->dynobj);
3674 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3675 if (sdyn != NULL)
3676 for (extdyn = sdyn->contents;
3677 extdyn < sdyn->contents + sdyn->size;
3678 extdyn += bed->s->sizeof_dyn)
3679 {
3680 Elf_Internal_Dyn dyn;
3681
3682 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3683 if (dyn.d_tag == DT_NEEDED
3684 && dyn.d_un.d_val == strindex)
3685 {
3686 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3687 return 1;
3688 }
3689 }
3690 }
3691
3692 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3693 return -1;
3694
3695 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3696 return -1;
3697
3698 return 0;
3699 }
3700
3701 /* Return true if SONAME is on the needed list between NEEDED and STOP
3702 (or the end of list if STOP is NULL), and needed by a library that
3703 will be loaded. */
3704
3705 static bfd_boolean
3706 on_needed_list (const char *soname,
3707 struct bfd_link_needed_list *needed,
3708 struct bfd_link_needed_list *stop)
3709 {
3710 struct bfd_link_needed_list *look;
3711 for (look = needed; look != stop; look = look->next)
3712 if (strcmp (soname, look->name) == 0
3713 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3714 /* If needed by a library that itself is not directly
3715 needed, recursively check whether that library is
3716 indirectly needed. Since we add DT_NEEDED entries to
3717 the end of the list, library dependencies appear after
3718 the library. Therefore search prior to the current
3719 LOOK, preventing possible infinite recursion. */
3720 || on_needed_list (elf_dt_name (look->by), needed, look)))
3721 return TRUE;
3722
3723 return FALSE;
3724 }
3725
3726 /* Sort symbol by value, section, size, and type. */
3727 static int
3728 elf_sort_symbol (const void *arg1, const void *arg2)
3729 {
3730 const struct elf_link_hash_entry *h1;
3731 const struct elf_link_hash_entry *h2;
3732 bfd_signed_vma vdiff;
3733 int sdiff;
3734 const char *n1;
3735 const char *n2;
3736
3737 h1 = *(const struct elf_link_hash_entry **) arg1;
3738 h2 = *(const struct elf_link_hash_entry **) arg2;
3739 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3740 if (vdiff != 0)
3741 return vdiff > 0 ? 1 : -1;
3742
3743 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3744 if (sdiff != 0)
3745 return sdiff;
3746
3747 /* Sort so that sized symbols are selected over zero size symbols. */
3748 vdiff = h1->size - h2->size;
3749 if (vdiff != 0)
3750 return vdiff > 0 ? 1 : -1;
3751
3752 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3753 if (h1->type != h2->type)
3754 return h1->type - h2->type;
3755
3756 /* If symbols are properly sized and typed, and multiple strong
3757 aliases are not defined in a shared library by the user we
3758 shouldn't get here. Unfortunately linker script symbols like
3759 __bss_start sometimes match a user symbol defined at the start of
3760 .bss without proper size and type. We'd like to preference the
3761 user symbol over reserved system symbols. Sort on leading
3762 underscores. */
3763 n1 = h1->root.root.string;
3764 n2 = h2->root.root.string;
3765 while (*n1 == *n2)
3766 {
3767 if (*n1 == 0)
3768 break;
3769 ++n1;
3770 ++n2;
3771 }
3772 if (*n1 == '_')
3773 return -1;
3774 if (*n2 == '_')
3775 return 1;
3776
3777 /* Final sort on name selects user symbols like '_u' over reserved
3778 system symbols like '_Z' and also will avoid qsort instability. */
3779 return *n1 - *n2;
3780 }
3781
3782 /* This function is used to adjust offsets into .dynstr for
3783 dynamic symbols. This is called via elf_link_hash_traverse. */
3784
3785 static bfd_boolean
3786 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3787 {
3788 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3789
3790 if (h->dynindx != -1)
3791 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3792 return TRUE;
3793 }
3794
3795 /* Assign string offsets in .dynstr, update all structures referencing
3796 them. */
3797
3798 static bfd_boolean
3799 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3800 {
3801 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3802 struct elf_link_local_dynamic_entry *entry;
3803 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3804 bfd *dynobj = hash_table->dynobj;
3805 asection *sdyn;
3806 bfd_size_type size;
3807 const struct elf_backend_data *bed;
3808 bfd_byte *extdyn;
3809
3810 _bfd_elf_strtab_finalize (dynstr);
3811 size = _bfd_elf_strtab_size (dynstr);
3812
3813 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3814
3815 if (info->callbacks->examine_strtab)
3816 info->callbacks->examine_strtab (dynstr);
3817
3818 bed = get_elf_backend_data (dynobj);
3819 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3820 BFD_ASSERT (sdyn != NULL);
3821
3822 /* Update all .dynamic entries referencing .dynstr strings. */
3823 for (extdyn = sdyn->contents;
3824 extdyn < sdyn->contents + sdyn->size;
3825 extdyn += bed->s->sizeof_dyn)
3826 {
3827 Elf_Internal_Dyn dyn;
3828
3829 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3830 switch (dyn.d_tag)
3831 {
3832 case DT_STRSZ:
3833 dyn.d_un.d_val = size;
3834 break;
3835 case DT_NEEDED:
3836 case DT_SONAME:
3837 case DT_RPATH:
3838 case DT_RUNPATH:
3839 case DT_FILTER:
3840 case DT_AUXILIARY:
3841 case DT_AUDIT:
3842 case DT_DEPAUDIT:
3843 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3844 break;
3845 default:
3846 continue;
3847 }
3848 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3849 }
3850
3851 /* Now update local dynamic symbols. */
3852 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3853 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3854 entry->isym.st_name);
3855
3856 /* And the rest of dynamic symbols. */
3857 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3858
3859 /* Adjust version definitions. */
3860 if (elf_tdata (output_bfd)->cverdefs)
3861 {
3862 asection *s;
3863 bfd_byte *p;
3864 size_t i;
3865 Elf_Internal_Verdef def;
3866 Elf_Internal_Verdaux defaux;
3867
3868 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3869 p = s->contents;
3870 do
3871 {
3872 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3873 &def);
3874 p += sizeof (Elf_External_Verdef);
3875 if (def.vd_aux != sizeof (Elf_External_Verdef))
3876 continue;
3877 for (i = 0; i < def.vd_cnt; ++i)
3878 {
3879 _bfd_elf_swap_verdaux_in (output_bfd,
3880 (Elf_External_Verdaux *) p, &defaux);
3881 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3882 defaux.vda_name);
3883 _bfd_elf_swap_verdaux_out (output_bfd,
3884 &defaux, (Elf_External_Verdaux *) p);
3885 p += sizeof (Elf_External_Verdaux);
3886 }
3887 }
3888 while (def.vd_next);
3889 }
3890
3891 /* Adjust version references. */
3892 if (elf_tdata (output_bfd)->verref)
3893 {
3894 asection *s;
3895 bfd_byte *p;
3896 size_t i;
3897 Elf_Internal_Verneed need;
3898 Elf_Internal_Vernaux needaux;
3899
3900 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3901 p = s->contents;
3902 do
3903 {
3904 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3905 &need);
3906 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3907 _bfd_elf_swap_verneed_out (output_bfd, &need,
3908 (Elf_External_Verneed *) p);
3909 p += sizeof (Elf_External_Verneed);
3910 for (i = 0; i < need.vn_cnt; ++i)
3911 {
3912 _bfd_elf_swap_vernaux_in (output_bfd,
3913 (Elf_External_Vernaux *) p, &needaux);
3914 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3915 needaux.vna_name);
3916 _bfd_elf_swap_vernaux_out (output_bfd,
3917 &needaux,
3918 (Elf_External_Vernaux *) p);
3919 p += sizeof (Elf_External_Vernaux);
3920 }
3921 }
3922 while (need.vn_next);
3923 }
3924
3925 return TRUE;
3926 }
3927 \f
3928 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3929 The default is to only match when the INPUT and OUTPUT are exactly
3930 the same target. */
3931
3932 bfd_boolean
3933 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3934 const bfd_target *output)
3935 {
3936 return input == output;
3937 }
3938
3939 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3940 This version is used when different targets for the same architecture
3941 are virtually identical. */
3942
3943 bfd_boolean
3944 _bfd_elf_relocs_compatible (const bfd_target *input,
3945 const bfd_target *output)
3946 {
3947 const struct elf_backend_data *obed, *ibed;
3948
3949 if (input == output)
3950 return TRUE;
3951
3952 ibed = xvec_get_elf_backend_data (input);
3953 obed = xvec_get_elf_backend_data (output);
3954
3955 if (ibed->arch != obed->arch)
3956 return FALSE;
3957
3958 /* If both backends are using this function, deem them compatible. */
3959 return ibed->relocs_compatible == obed->relocs_compatible;
3960 }
3961
3962 /* Make a special call to the linker "notice" function to tell it that
3963 we are about to handle an as-needed lib, or have finished
3964 processing the lib. */
3965
3966 bfd_boolean
3967 _bfd_elf_notice_as_needed (bfd *ibfd,
3968 struct bfd_link_info *info,
3969 enum notice_asneeded_action act)
3970 {
3971 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3972 }
3973
3974 /* Check relocations an ELF object file. */
3975
3976 bfd_boolean
3977 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3978 {
3979 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3980 struct elf_link_hash_table *htab = elf_hash_table (info);
3981
3982 /* If this object is the same format as the output object, and it is
3983 not a shared library, then let the backend look through the
3984 relocs.
3985
3986 This is required to build global offset table entries and to
3987 arrange for dynamic relocs. It is not required for the
3988 particular common case of linking non PIC code, even when linking
3989 against shared libraries, but unfortunately there is no way of
3990 knowing whether an object file has been compiled PIC or not.
3991 Looking through the relocs is not particularly time consuming.
3992 The problem is that we must either (1) keep the relocs in memory,
3993 which causes the linker to require additional runtime memory or
3994 (2) read the relocs twice from the input file, which wastes time.
3995 This would be a good case for using mmap.
3996
3997 I have no idea how to handle linking PIC code into a file of a
3998 different format. It probably can't be done. */
3999 if ((abfd->flags & DYNAMIC) == 0
4000 && is_elf_hash_table (htab)
4001 && bed->check_relocs != NULL
4002 && elf_object_id (abfd) == elf_hash_table_id (htab)
4003 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4004 {
4005 asection *o;
4006
4007 for (o = abfd->sections; o != NULL; o = o->next)
4008 {
4009 Elf_Internal_Rela *internal_relocs;
4010 bfd_boolean ok;
4011
4012 /* Don't check relocations in excluded sections. Don't do
4013 anything special with non-loaded, non-alloced sections.
4014 In particular, any relocs in such sections should not
4015 affect GOT and PLT reference counting (ie. we don't
4016 allow them to create GOT or PLT entries), there's no
4017 possibility or desire to optimize TLS relocs, and
4018 there's not much point in propagating relocs to shared
4019 libs that the dynamic linker won't relocate. */
4020 if ((o->flags & SEC_ALLOC) == 0
4021 || (o->flags & SEC_RELOC) == 0
4022 || (o->flags & SEC_EXCLUDE) != 0
4023 || o->reloc_count == 0
4024 || ((info->strip == strip_all || info->strip == strip_debugger)
4025 && (o->flags & SEC_DEBUGGING) != 0)
4026 || bfd_is_abs_section (o->output_section))
4027 continue;
4028
4029 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4030 info->keep_memory);
4031 if (internal_relocs == NULL)
4032 return FALSE;
4033
4034 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4035
4036 if (elf_section_data (o)->relocs != internal_relocs)
4037 free (internal_relocs);
4038
4039 if (! ok)
4040 return FALSE;
4041 }
4042 }
4043
4044 return TRUE;
4045 }
4046
4047 /* Add symbols from an ELF object file to the linker hash table. */
4048
4049 static bfd_boolean
4050 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4051 {
4052 Elf_Internal_Ehdr *ehdr;
4053 Elf_Internal_Shdr *hdr;
4054 size_t symcount;
4055 size_t extsymcount;
4056 size_t extsymoff;
4057 struct elf_link_hash_entry **sym_hash;
4058 bfd_boolean dynamic;
4059 Elf_External_Versym *extversym = NULL;
4060 Elf_External_Versym *extversym_end = NULL;
4061 Elf_External_Versym *ever;
4062 struct elf_link_hash_entry *weaks;
4063 struct elf_link_hash_entry **nondeflt_vers = NULL;
4064 size_t nondeflt_vers_cnt = 0;
4065 Elf_Internal_Sym *isymbuf = NULL;
4066 Elf_Internal_Sym *isym;
4067 Elf_Internal_Sym *isymend;
4068 const struct elf_backend_data *bed;
4069 bfd_boolean add_needed;
4070 struct elf_link_hash_table *htab;
4071 void *alloc_mark = NULL;
4072 struct bfd_hash_entry **old_table = NULL;
4073 unsigned int old_size = 0;
4074 unsigned int old_count = 0;
4075 void *old_tab = NULL;
4076 void *old_ent;
4077 struct bfd_link_hash_entry *old_undefs = NULL;
4078 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4079 void *old_strtab = NULL;
4080 size_t tabsize = 0;
4081 asection *s;
4082 bfd_boolean just_syms;
4083
4084 htab = elf_hash_table (info);
4085 bed = get_elf_backend_data (abfd);
4086
4087 if ((abfd->flags & DYNAMIC) == 0)
4088 dynamic = FALSE;
4089 else
4090 {
4091 dynamic = TRUE;
4092
4093 /* You can't use -r against a dynamic object. Also, there's no
4094 hope of using a dynamic object which does not exactly match
4095 the format of the output file. */
4096 if (bfd_link_relocatable (info)
4097 || !is_elf_hash_table (htab)
4098 || info->output_bfd->xvec != abfd->xvec)
4099 {
4100 if (bfd_link_relocatable (info))
4101 bfd_set_error (bfd_error_invalid_operation);
4102 else
4103 bfd_set_error (bfd_error_wrong_format);
4104 goto error_return;
4105 }
4106 }
4107
4108 ehdr = elf_elfheader (abfd);
4109 if (info->warn_alternate_em
4110 && bed->elf_machine_code != ehdr->e_machine
4111 && ((bed->elf_machine_alt1 != 0
4112 && ehdr->e_machine == bed->elf_machine_alt1)
4113 || (bed->elf_machine_alt2 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt2)))
4115 _bfd_error_handler
4116 /* xgettext:c-format */
4117 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4118 ehdr->e_machine, abfd, bed->elf_machine_code);
4119
4120 /* As a GNU extension, any input sections which are named
4121 .gnu.warning.SYMBOL are treated as warning symbols for the given
4122 symbol. This differs from .gnu.warning sections, which generate
4123 warnings when they are included in an output file. */
4124 /* PR 12761: Also generate this warning when building shared libraries. */
4125 for (s = abfd->sections; s != NULL; s = s->next)
4126 {
4127 const char *name;
4128
4129 name = bfd_section_name (s);
4130 if (CONST_STRNEQ (name, ".gnu.warning."))
4131 {
4132 char *msg;
4133 bfd_size_type sz;
4134
4135 name += sizeof ".gnu.warning." - 1;
4136
4137 /* If this is a shared object, then look up the symbol
4138 in the hash table. If it is there, and it is already
4139 been defined, then we will not be using the entry
4140 from this shared object, so we don't need to warn.
4141 FIXME: If we see the definition in a regular object
4142 later on, we will warn, but we shouldn't. The only
4143 fix is to keep track of what warnings we are supposed
4144 to emit, and then handle them all at the end of the
4145 link. */
4146 if (dynamic)
4147 {
4148 struct elf_link_hash_entry *h;
4149
4150 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4151
4152 /* FIXME: What about bfd_link_hash_common? */
4153 if (h != NULL
4154 && (h->root.type == bfd_link_hash_defined
4155 || h->root.type == bfd_link_hash_defweak))
4156 continue;
4157 }
4158
4159 sz = s->size;
4160 msg = (char *) bfd_alloc (abfd, sz + 1);
4161 if (msg == NULL)
4162 goto error_return;
4163
4164 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4165 goto error_return;
4166
4167 msg[sz] = '\0';
4168
4169 if (! (_bfd_generic_link_add_one_symbol
4170 (info, abfd, name, BSF_WARNING, s, 0, msg,
4171 FALSE, bed->collect, NULL)))
4172 goto error_return;
4173
4174 if (bfd_link_executable (info))
4175 {
4176 /* Clobber the section size so that the warning does
4177 not get copied into the output file. */
4178 s->size = 0;
4179
4180 /* Also set SEC_EXCLUDE, so that symbols defined in
4181 the warning section don't get copied to the output. */
4182 s->flags |= SEC_EXCLUDE;
4183 }
4184 }
4185 }
4186
4187 just_syms = ((s = abfd->sections) != NULL
4188 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4189
4190 add_needed = TRUE;
4191 if (! dynamic)
4192 {
4193 /* If we are creating a shared library, create all the dynamic
4194 sections immediately. We need to attach them to something,
4195 so we attach them to this BFD, provided it is the right
4196 format and is not from ld --just-symbols. Always create the
4197 dynamic sections for -E/--dynamic-list. FIXME: If there
4198 are no input BFD's of the same format as the output, we can't
4199 make a shared library. */
4200 if (!just_syms
4201 && (bfd_link_pic (info)
4202 || (!bfd_link_relocatable (info)
4203 && info->nointerp
4204 && (info->export_dynamic || info->dynamic)))
4205 && is_elf_hash_table (htab)
4206 && info->output_bfd->xvec == abfd->xvec
4207 && !htab->dynamic_sections_created)
4208 {
4209 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4210 goto error_return;
4211 }
4212 }
4213 else if (!is_elf_hash_table (htab))
4214 goto error_return;
4215 else
4216 {
4217 const char *soname = NULL;
4218 char *audit = NULL;
4219 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4220 const Elf_Internal_Phdr *phdr;
4221 struct elf_link_loaded_list *loaded_lib;
4222
4223 /* ld --just-symbols and dynamic objects don't mix very well.
4224 ld shouldn't allow it. */
4225 if (just_syms)
4226 abort ();
4227
4228 /* If this dynamic lib was specified on the command line with
4229 --as-needed in effect, then we don't want to add a DT_NEEDED
4230 tag unless the lib is actually used. Similary for libs brought
4231 in by another lib's DT_NEEDED. When --no-add-needed is used
4232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4233 any dynamic library in DT_NEEDED tags in the dynamic lib at
4234 all. */
4235 add_needed = (elf_dyn_lib_class (abfd)
4236 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4237 | DYN_NO_NEEDED)) == 0;
4238
4239 s = bfd_get_section_by_name (abfd, ".dynamic");
4240 if (s != NULL)
4241 {
4242 bfd_byte *dynbuf;
4243 bfd_byte *extdyn;
4244 unsigned int elfsec;
4245 unsigned long shlink;
4246
4247 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4248 {
4249 error_free_dyn:
4250 free (dynbuf);
4251 goto error_return;
4252 }
4253
4254 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4255 if (elfsec == SHN_BAD)
4256 goto error_free_dyn;
4257 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4258
4259 for (extdyn = dynbuf;
4260 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4261 extdyn += bed->s->sizeof_dyn)
4262 {
4263 Elf_Internal_Dyn dyn;
4264
4265 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4266 if (dyn.d_tag == DT_SONAME)
4267 {
4268 unsigned int tagv = dyn.d_un.d_val;
4269 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4270 if (soname == NULL)
4271 goto error_free_dyn;
4272 }
4273 if (dyn.d_tag == DT_NEEDED)
4274 {
4275 struct bfd_link_needed_list *n, **pn;
4276 char *fnm, *anm;
4277 unsigned int tagv = dyn.d_un.d_val;
4278 size_t amt = sizeof (struct bfd_link_needed_list);
4279
4280 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4281 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4282 if (n == NULL || fnm == NULL)
4283 goto error_free_dyn;
4284 amt = strlen (fnm) + 1;
4285 anm = (char *) bfd_alloc (abfd, amt);
4286 if (anm == NULL)
4287 goto error_free_dyn;
4288 memcpy (anm, fnm, amt);
4289 n->name = anm;
4290 n->by = abfd;
4291 n->next = NULL;
4292 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4293 ;
4294 *pn = n;
4295 }
4296 if (dyn.d_tag == DT_RUNPATH)
4297 {
4298 struct bfd_link_needed_list *n, **pn;
4299 char *fnm, *anm;
4300 unsigned int tagv = dyn.d_un.d_val;
4301 size_t amt = sizeof (struct bfd_link_needed_list);
4302
4303 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4304 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4305 if (n == NULL || fnm == NULL)
4306 goto error_free_dyn;
4307 amt = strlen (fnm) + 1;
4308 anm = (char *) bfd_alloc (abfd, amt);
4309 if (anm == NULL)
4310 goto error_free_dyn;
4311 memcpy (anm, fnm, amt);
4312 n->name = anm;
4313 n->by = abfd;
4314 n->next = NULL;
4315 for (pn = & runpath;
4316 *pn != NULL;
4317 pn = &(*pn)->next)
4318 ;
4319 *pn = n;
4320 }
4321 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4322 if (!runpath && dyn.d_tag == DT_RPATH)
4323 {
4324 struct bfd_link_needed_list *n, **pn;
4325 char *fnm, *anm;
4326 unsigned int tagv = dyn.d_un.d_val;
4327 size_t amt = sizeof (struct bfd_link_needed_list);
4328
4329 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4330 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4331 if (n == NULL || fnm == NULL)
4332 goto error_free_dyn;
4333 amt = strlen (fnm) + 1;
4334 anm = (char *) bfd_alloc (abfd, amt);
4335 if (anm == NULL)
4336 goto error_free_dyn;
4337 memcpy (anm, fnm, amt);
4338 n->name = anm;
4339 n->by = abfd;
4340 n->next = NULL;
4341 for (pn = & rpath;
4342 *pn != NULL;
4343 pn = &(*pn)->next)
4344 ;
4345 *pn = n;
4346 }
4347 if (dyn.d_tag == DT_AUDIT)
4348 {
4349 unsigned int tagv = dyn.d_un.d_val;
4350 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4351 }
4352 }
4353
4354 free (dynbuf);
4355 }
4356
4357 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4358 frees all more recently bfd_alloc'd blocks as well. */
4359 if (runpath)
4360 rpath = runpath;
4361
4362 if (rpath)
4363 {
4364 struct bfd_link_needed_list **pn;
4365 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4366 ;
4367 *pn = rpath;
4368 }
4369
4370 /* If we have a PT_GNU_RELRO program header, mark as read-only
4371 all sections contained fully therein. This makes relro
4372 shared library sections appear as they will at run-time. */
4373 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4374 while (phdr-- > elf_tdata (abfd)->phdr)
4375 if (phdr->p_type == PT_GNU_RELRO)
4376 {
4377 for (s = abfd->sections; s != NULL; s = s->next)
4378 {
4379 unsigned int opb = bfd_octets_per_byte (abfd, s);
4380
4381 if ((s->flags & SEC_ALLOC) != 0
4382 && s->vma * opb >= phdr->p_vaddr
4383 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4384 s->flags |= SEC_READONLY;
4385 }
4386 break;
4387 }
4388
4389 /* We do not want to include any of the sections in a dynamic
4390 object in the output file. We hack by simply clobbering the
4391 list of sections in the BFD. This could be handled more
4392 cleanly by, say, a new section flag; the existing
4393 SEC_NEVER_LOAD flag is not the one we want, because that one
4394 still implies that the section takes up space in the output
4395 file. */
4396 bfd_section_list_clear (abfd);
4397
4398 /* Find the name to use in a DT_NEEDED entry that refers to this
4399 object. If the object has a DT_SONAME entry, we use it.
4400 Otherwise, if the generic linker stuck something in
4401 elf_dt_name, we use that. Otherwise, we just use the file
4402 name. */
4403 if (soname == NULL || *soname == '\0')
4404 {
4405 soname = elf_dt_name (abfd);
4406 if (soname == NULL || *soname == '\0')
4407 soname = bfd_get_filename (abfd);
4408 }
4409
4410 /* Save the SONAME because sometimes the linker emulation code
4411 will need to know it. */
4412 elf_dt_name (abfd) = soname;
4413
4414 /* If we have already included this dynamic object in the
4415 link, just ignore it. There is no reason to include a
4416 particular dynamic object more than once. */
4417 for (loaded_lib = htab->dyn_loaded;
4418 loaded_lib != NULL;
4419 loaded_lib = loaded_lib->next)
4420 {
4421 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4422 return TRUE;
4423 }
4424
4425 /* Create dynamic sections for backends that require that be done
4426 before setup_gnu_properties. */
4427 if (add_needed
4428 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4429 return FALSE;
4430
4431 /* Save the DT_AUDIT entry for the linker emulation code. */
4432 elf_dt_audit (abfd) = audit;
4433 }
4434
4435 /* If this is a dynamic object, we always link against the .dynsym
4436 symbol table, not the .symtab symbol table. The dynamic linker
4437 will only see the .dynsym symbol table, so there is no reason to
4438 look at .symtab for a dynamic object. */
4439
4440 if (! dynamic || elf_dynsymtab (abfd) == 0)
4441 hdr = &elf_tdata (abfd)->symtab_hdr;
4442 else
4443 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4444
4445 symcount = hdr->sh_size / bed->s->sizeof_sym;
4446
4447 /* The sh_info field of the symtab header tells us where the
4448 external symbols start. We don't care about the local symbols at
4449 this point. */
4450 if (elf_bad_symtab (abfd))
4451 {
4452 extsymcount = symcount;
4453 extsymoff = 0;
4454 }
4455 else
4456 {
4457 extsymcount = symcount - hdr->sh_info;
4458 extsymoff = hdr->sh_info;
4459 }
4460
4461 sym_hash = elf_sym_hashes (abfd);
4462 if (extsymcount != 0)
4463 {
4464 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4465 NULL, NULL, NULL);
4466 if (isymbuf == NULL)
4467 goto error_return;
4468
4469 if (sym_hash == NULL)
4470 {
4471 /* We store a pointer to the hash table entry for each
4472 external symbol. */
4473 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4474 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4475 if (sym_hash == NULL)
4476 goto error_free_sym;
4477 elf_sym_hashes (abfd) = sym_hash;
4478 }
4479 }
4480
4481 if (dynamic)
4482 {
4483 /* Read in any version definitions. */
4484 if (!_bfd_elf_slurp_version_tables (abfd,
4485 info->default_imported_symver))
4486 goto error_free_sym;
4487
4488 /* Read in the symbol versions, but don't bother to convert them
4489 to internal format. */
4490 if (elf_dynversym (abfd) != 0)
4491 {
4492 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4493 bfd_size_type amt = versymhdr->sh_size;
4494
4495 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4496 goto error_free_sym;
4497 extversym = (Elf_External_Versym *)
4498 _bfd_malloc_and_read (abfd, amt, amt);
4499 if (extversym == NULL)
4500 goto error_free_sym;
4501 extversym_end = extversym + amt / sizeof (*extversym);
4502 }
4503 }
4504
4505 /* If we are loading an as-needed shared lib, save the symbol table
4506 state before we start adding symbols. If the lib turns out
4507 to be unneeded, restore the state. */
4508 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4509 {
4510 unsigned int i;
4511 size_t entsize;
4512
4513 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4514 {
4515 struct bfd_hash_entry *p;
4516 struct elf_link_hash_entry *h;
4517
4518 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4519 {
4520 h = (struct elf_link_hash_entry *) p;
4521 entsize += htab->root.table.entsize;
4522 if (h->root.type == bfd_link_hash_warning)
4523 {
4524 entsize += htab->root.table.entsize;
4525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4526 }
4527 if (h->root.type == bfd_link_hash_common)
4528 entsize += sizeof (*h->root.u.c.p);
4529 }
4530 }
4531
4532 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4533 old_tab = bfd_malloc (tabsize + entsize);
4534 if (old_tab == NULL)
4535 goto error_free_vers;
4536
4537 /* Remember the current objalloc pointer, so that all mem for
4538 symbols added can later be reclaimed. */
4539 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4540 if (alloc_mark == NULL)
4541 goto error_free_vers;
4542
4543 /* Make a special call to the linker "notice" function to
4544 tell it that we are about to handle an as-needed lib. */
4545 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4546 goto error_free_vers;
4547
4548 /* Clone the symbol table. Remember some pointers into the
4549 symbol table, and dynamic symbol count. */
4550 old_ent = (char *) old_tab + tabsize;
4551 memcpy (old_tab, htab->root.table.table, tabsize);
4552 old_undefs = htab->root.undefs;
4553 old_undefs_tail = htab->root.undefs_tail;
4554 old_table = htab->root.table.table;
4555 old_size = htab->root.table.size;
4556 old_count = htab->root.table.count;
4557 old_strtab = NULL;
4558 if (htab->dynstr != NULL)
4559 {
4560 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4561 if (old_strtab == NULL)
4562 goto error_free_vers;
4563 }
4564
4565 for (i = 0; i < htab->root.table.size; i++)
4566 {
4567 struct bfd_hash_entry *p;
4568 struct elf_link_hash_entry *h;
4569
4570 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4571 {
4572 h = (struct elf_link_hash_entry *) p;
4573 memcpy (old_ent, h, htab->root.table.entsize);
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
4575 if (h->root.type == bfd_link_hash_warning)
4576 {
4577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4578 memcpy (old_ent, h, htab->root.table.entsize);
4579 old_ent = (char *) old_ent + htab->root.table.entsize;
4580 }
4581 if (h->root.type == bfd_link_hash_common)
4582 {
4583 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4584 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4585 }
4586 }
4587 }
4588 }
4589
4590 weaks = NULL;
4591 if (extversym == NULL)
4592 ever = NULL;
4593 else if (extversym + extsymoff < extversym_end)
4594 ever = extversym + extsymoff;
4595 else
4596 {
4597 /* xgettext:c-format */
4598 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4599 abfd, (long) extsymoff,
4600 (long) (extversym_end - extversym) / sizeof (* extversym));
4601 bfd_set_error (bfd_error_bad_value);
4602 goto error_free_vers;
4603 }
4604
4605 if (!bfd_link_relocatable (info)
4606 && abfd->lto_slim_object)
4607 {
4608 _bfd_error_handler
4609 (_("%pB: plugin needed to handle lto object"), abfd);
4610 }
4611
4612 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4613 isym < isymend;
4614 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4615 {
4616 int bind;
4617 bfd_vma value;
4618 asection *sec, *new_sec;
4619 flagword flags;
4620 const char *name;
4621 struct elf_link_hash_entry *h;
4622 struct elf_link_hash_entry *hi;
4623 bfd_boolean definition;
4624 bfd_boolean size_change_ok;
4625 bfd_boolean type_change_ok;
4626 bfd_boolean new_weak;
4627 bfd_boolean old_weak;
4628 bfd *override;
4629 bfd_boolean common;
4630 bfd_boolean discarded;
4631 unsigned int old_alignment;
4632 unsigned int shindex;
4633 bfd *old_bfd;
4634 bfd_boolean matched;
4635
4636 override = NULL;
4637
4638 flags = BSF_NO_FLAGS;
4639 sec = NULL;
4640 value = isym->st_value;
4641 common = bed->common_definition (isym);
4642 if (common && info->inhibit_common_definition)
4643 {
4644 /* Treat common symbol as undefined for --no-define-common. */
4645 isym->st_shndx = SHN_UNDEF;
4646 common = FALSE;
4647 }
4648 discarded = FALSE;
4649
4650 bind = ELF_ST_BIND (isym->st_info);
4651 switch (bind)
4652 {
4653 case STB_LOCAL:
4654 /* This should be impossible, since ELF requires that all
4655 global symbols follow all local symbols, and that sh_info
4656 point to the first global symbol. Unfortunately, Irix 5
4657 screws this up. */
4658 if (elf_bad_symtab (abfd))
4659 continue;
4660
4661 /* If we aren't prepared to handle locals within the globals
4662 then we'll likely segfault on a NULL symbol hash if the
4663 symbol is ever referenced in relocations. */
4664 shindex = elf_elfheader (abfd)->e_shstrndx;
4665 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4666 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4667 " (>= sh_info of %lu)"),
4668 abfd, name, (long) (isym - isymbuf + extsymoff),
4669 (long) extsymoff);
4670
4671 /* Dynamic object relocations are not processed by ld, so
4672 ld won't run into the problem mentioned above. */
4673 if (dynamic)
4674 continue;
4675 bfd_set_error (bfd_error_bad_value);
4676 goto error_free_vers;
4677
4678 case STB_GLOBAL:
4679 if (isym->st_shndx != SHN_UNDEF && !common)
4680 flags = BSF_GLOBAL;
4681 break;
4682
4683 case STB_WEAK:
4684 flags = BSF_WEAK;
4685 break;
4686
4687 case STB_GNU_UNIQUE:
4688 flags = BSF_GNU_UNIQUE;
4689 break;
4690
4691 default:
4692 /* Leave it up to the processor backend. */
4693 break;
4694 }
4695
4696 if (isym->st_shndx == SHN_UNDEF)
4697 sec = bfd_und_section_ptr;
4698 else if (isym->st_shndx == SHN_ABS)
4699 sec = bfd_abs_section_ptr;
4700 else if (isym->st_shndx == SHN_COMMON)
4701 {
4702 sec = bfd_com_section_ptr;
4703 /* What ELF calls the size we call the value. What ELF
4704 calls the value we call the alignment. */
4705 value = isym->st_size;
4706 }
4707 else
4708 {
4709 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4710 if (sec == NULL)
4711 sec = bfd_abs_section_ptr;
4712 else if (discarded_section (sec))
4713 {
4714 /* Symbols from discarded section are undefined. We keep
4715 its visibility. */
4716 sec = bfd_und_section_ptr;
4717 discarded = TRUE;
4718 isym->st_shndx = SHN_UNDEF;
4719 }
4720 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4721 value -= sec->vma;
4722 }
4723
4724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4725 isym->st_name);
4726 if (name == NULL)
4727 goto error_free_vers;
4728
4729 if (isym->st_shndx == SHN_COMMON
4730 && (abfd->flags & BFD_PLUGIN) != 0)
4731 {
4732 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4733
4734 if (xc == NULL)
4735 {
4736 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4737 | SEC_EXCLUDE);
4738 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4739 if (xc == NULL)
4740 goto error_free_vers;
4741 }
4742 sec = xc;
4743 }
4744 else if (isym->st_shndx == SHN_COMMON
4745 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4746 && !bfd_link_relocatable (info))
4747 {
4748 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4749
4750 if (tcomm == NULL)
4751 {
4752 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4753 | SEC_LINKER_CREATED);
4754 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4755 if (tcomm == NULL)
4756 goto error_free_vers;
4757 }
4758 sec = tcomm;
4759 }
4760 else if (bed->elf_add_symbol_hook)
4761 {
4762 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4763 &sec, &value))
4764 goto error_free_vers;
4765
4766 /* The hook function sets the name to NULL if this symbol
4767 should be skipped for some reason. */
4768 if (name == NULL)
4769 continue;
4770 }
4771
4772 /* Sanity check that all possibilities were handled. */
4773 if (sec == NULL)
4774 abort ();
4775
4776 /* Silently discard TLS symbols from --just-syms. There's
4777 no way to combine a static TLS block with a new TLS block
4778 for this executable. */
4779 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4780 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4781 continue;
4782
4783 if (bfd_is_und_section (sec)
4784 || bfd_is_com_section (sec))
4785 definition = FALSE;
4786 else
4787 definition = TRUE;
4788
4789 size_change_ok = FALSE;
4790 type_change_ok = bed->type_change_ok;
4791 old_weak = FALSE;
4792 matched = FALSE;
4793 old_alignment = 0;
4794 old_bfd = NULL;
4795 new_sec = sec;
4796
4797 if (is_elf_hash_table (htab))
4798 {
4799 Elf_Internal_Versym iver;
4800 unsigned int vernum = 0;
4801 bfd_boolean skip;
4802
4803 if (ever == NULL)
4804 {
4805 if (info->default_imported_symver)
4806 /* Use the default symbol version created earlier. */
4807 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4808 else
4809 iver.vs_vers = 0;
4810 }
4811 else if (ever >= extversym_end)
4812 {
4813 /* xgettext:c-format */
4814 _bfd_error_handler (_("%pB: not enough version information"),
4815 abfd);
4816 bfd_set_error (bfd_error_bad_value);
4817 goto error_free_vers;
4818 }
4819 else
4820 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4821
4822 vernum = iver.vs_vers & VERSYM_VERSION;
4823
4824 /* If this is a hidden symbol, or if it is not version
4825 1, we append the version name to the symbol name.
4826 However, we do not modify a non-hidden absolute symbol
4827 if it is not a function, because it might be the version
4828 symbol itself. FIXME: What if it isn't? */
4829 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4830 || (vernum > 1
4831 && (!bfd_is_abs_section (sec)
4832 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4833 {
4834 const char *verstr;
4835 size_t namelen, verlen, newlen;
4836 char *newname, *p;
4837
4838 if (isym->st_shndx != SHN_UNDEF)
4839 {
4840 if (vernum > elf_tdata (abfd)->cverdefs)
4841 verstr = NULL;
4842 else if (vernum > 1)
4843 verstr =
4844 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4845 else
4846 verstr = "";
4847
4848 if (verstr == NULL)
4849 {
4850 _bfd_error_handler
4851 /* xgettext:c-format */
4852 (_("%pB: %s: invalid version %u (max %d)"),
4853 abfd, name, vernum,
4854 elf_tdata (abfd)->cverdefs);
4855 bfd_set_error (bfd_error_bad_value);
4856 goto error_free_vers;
4857 }
4858 }
4859 else
4860 {
4861 /* We cannot simply test for the number of
4862 entries in the VERNEED section since the
4863 numbers for the needed versions do not start
4864 at 0. */
4865 Elf_Internal_Verneed *t;
4866
4867 verstr = NULL;
4868 for (t = elf_tdata (abfd)->verref;
4869 t != NULL;
4870 t = t->vn_nextref)
4871 {
4872 Elf_Internal_Vernaux *a;
4873
4874 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4875 {
4876 if (a->vna_other == vernum)
4877 {
4878 verstr = a->vna_nodename;
4879 break;
4880 }
4881 }
4882 if (a != NULL)
4883 break;
4884 }
4885 if (verstr == NULL)
4886 {
4887 _bfd_error_handler
4888 /* xgettext:c-format */
4889 (_("%pB: %s: invalid needed version %d"),
4890 abfd, name, vernum);
4891 bfd_set_error (bfd_error_bad_value);
4892 goto error_free_vers;
4893 }
4894 }
4895
4896 namelen = strlen (name);
4897 verlen = strlen (verstr);
4898 newlen = namelen + verlen + 2;
4899 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4900 && isym->st_shndx != SHN_UNDEF)
4901 ++newlen;
4902
4903 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4904 if (newname == NULL)
4905 goto error_free_vers;
4906 memcpy (newname, name, namelen);
4907 p = newname + namelen;
4908 *p++ = ELF_VER_CHR;
4909 /* If this is a defined non-hidden version symbol,
4910 we add another @ to the name. This indicates the
4911 default version of the symbol. */
4912 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4913 && isym->st_shndx != SHN_UNDEF)
4914 *p++ = ELF_VER_CHR;
4915 memcpy (p, verstr, verlen + 1);
4916
4917 name = newname;
4918 }
4919
4920 /* If this symbol has default visibility and the user has
4921 requested we not re-export it, then mark it as hidden. */
4922 if (!bfd_is_und_section (sec)
4923 && !dynamic
4924 && abfd->no_export
4925 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4926 isym->st_other = (STV_HIDDEN
4927 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4928
4929 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4930 sym_hash, &old_bfd, &old_weak,
4931 &old_alignment, &skip, &override,
4932 &type_change_ok, &size_change_ok,
4933 &matched))
4934 goto error_free_vers;
4935
4936 if (skip)
4937 continue;
4938
4939 /* Override a definition only if the new symbol matches the
4940 existing one. */
4941 if (override && matched)
4942 definition = FALSE;
4943
4944 h = *sym_hash;
4945 while (h->root.type == bfd_link_hash_indirect
4946 || h->root.type == bfd_link_hash_warning)
4947 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4948
4949 if (elf_tdata (abfd)->verdef != NULL
4950 && vernum > 1
4951 && definition)
4952 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4953 }
4954
4955 if (! (_bfd_generic_link_add_one_symbol
4956 (info, override ? override : abfd, name, flags, sec, value,
4957 NULL, FALSE, bed->collect,
4958 (struct bfd_link_hash_entry **) sym_hash)))
4959 goto error_free_vers;
4960
4961 h = *sym_hash;
4962 /* We need to make sure that indirect symbol dynamic flags are
4963 updated. */
4964 hi = h;
4965 while (h->root.type == bfd_link_hash_indirect
4966 || h->root.type == bfd_link_hash_warning)
4967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4968
4969 /* Setting the index to -3 tells elf_link_output_extsym that
4970 this symbol is defined in a discarded section. */
4971 if (discarded)
4972 h->indx = -3;
4973
4974 *sym_hash = h;
4975
4976 new_weak = (flags & BSF_WEAK) != 0;
4977 if (dynamic
4978 && definition
4979 && new_weak
4980 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4981 && is_elf_hash_table (htab)
4982 && h->u.alias == NULL)
4983 {
4984 /* Keep a list of all weak defined non function symbols from
4985 a dynamic object, using the alias field. Later in this
4986 function we will set the alias field to the correct
4987 value. We only put non-function symbols from dynamic
4988 objects on this list, because that happens to be the only
4989 time we need to know the normal symbol corresponding to a
4990 weak symbol, and the information is time consuming to
4991 figure out. If the alias field is not already NULL,
4992 then this symbol was already defined by some previous
4993 dynamic object, and we will be using that previous
4994 definition anyhow. */
4995
4996 h->u.alias = weaks;
4997 weaks = h;
4998 }
4999
5000 /* Set the alignment of a common symbol. */
5001 if ((common || bfd_is_com_section (sec))
5002 && h->root.type == bfd_link_hash_common)
5003 {
5004 unsigned int align;
5005
5006 if (common)
5007 align = bfd_log2 (isym->st_value);
5008 else
5009 {
5010 /* The new symbol is a common symbol in a shared object.
5011 We need to get the alignment from the section. */
5012 align = new_sec->alignment_power;
5013 }
5014 if (align > old_alignment)
5015 h->root.u.c.p->alignment_power = align;
5016 else
5017 h->root.u.c.p->alignment_power = old_alignment;
5018 }
5019
5020 if (is_elf_hash_table (htab))
5021 {
5022 /* Set a flag in the hash table entry indicating the type of
5023 reference or definition we just found. A dynamic symbol
5024 is one which is referenced or defined by both a regular
5025 object and a shared object. */
5026 bfd_boolean dynsym = FALSE;
5027
5028 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5029 if ((abfd->flags & BFD_PLUGIN) != 0)
5030 ;
5031 else if (!dynamic)
5032 {
5033 if (! definition)
5034 {
5035 h->ref_regular = 1;
5036 if (bind != STB_WEAK)
5037 h->ref_regular_nonweak = 1;
5038 }
5039 else
5040 {
5041 h->def_regular = 1;
5042 if (h->def_dynamic)
5043 {
5044 h->def_dynamic = 0;
5045 h->ref_dynamic = 1;
5046 }
5047 }
5048 }
5049 else
5050 {
5051 if (! definition)
5052 {
5053 h->ref_dynamic = 1;
5054 hi->ref_dynamic = 1;
5055 }
5056 else
5057 {
5058 h->def_dynamic = 1;
5059 hi->def_dynamic = 1;
5060 }
5061 }
5062
5063 /* If an indirect symbol has been forced local, don't
5064 make the real symbol dynamic. */
5065 if (h != hi && hi->forced_local)
5066 ;
5067 else if (!dynamic)
5068 {
5069 if (bfd_link_dll (info)
5070 || h->def_dynamic
5071 || h->ref_dynamic)
5072 dynsym = TRUE;
5073 }
5074 else
5075 {
5076 if (h->def_regular
5077 || h->ref_regular
5078 || (h->is_weakalias
5079 && weakdef (h)->dynindx != -1))
5080 dynsym = TRUE;
5081 }
5082
5083 /* Check to see if we need to add an indirect symbol for
5084 the default name. */
5085 if ((definition
5086 || (!override && h->root.type == bfd_link_hash_common))
5087 && !(hi != h
5088 && hi->versioned == versioned_hidden))
5089 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5090 sec, value, &old_bfd, &dynsym))
5091 goto error_free_vers;
5092
5093 /* Check the alignment when a common symbol is involved. This
5094 can change when a common symbol is overridden by a normal
5095 definition or a common symbol is ignored due to the old
5096 normal definition. We need to make sure the maximum
5097 alignment is maintained. */
5098 if ((old_alignment || common)
5099 && h->root.type != bfd_link_hash_common)
5100 {
5101 unsigned int common_align;
5102 unsigned int normal_align;
5103 unsigned int symbol_align;
5104 bfd *normal_bfd;
5105 bfd *common_bfd;
5106
5107 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5108 || h->root.type == bfd_link_hash_defweak);
5109
5110 symbol_align = ffs (h->root.u.def.value) - 1;
5111 if (h->root.u.def.section->owner != NULL
5112 && (h->root.u.def.section->owner->flags
5113 & (DYNAMIC | BFD_PLUGIN)) == 0)
5114 {
5115 normal_align = h->root.u.def.section->alignment_power;
5116 if (normal_align > symbol_align)
5117 normal_align = symbol_align;
5118 }
5119 else
5120 normal_align = symbol_align;
5121
5122 if (old_alignment)
5123 {
5124 common_align = old_alignment;
5125 common_bfd = old_bfd;
5126 normal_bfd = abfd;
5127 }
5128 else
5129 {
5130 common_align = bfd_log2 (isym->st_value);
5131 common_bfd = abfd;
5132 normal_bfd = old_bfd;
5133 }
5134
5135 if (normal_align < common_align)
5136 {
5137 /* PR binutils/2735 */
5138 if (normal_bfd == NULL)
5139 _bfd_error_handler
5140 /* xgettext:c-format */
5141 (_("warning: alignment %u of common symbol `%s' in %pB is"
5142 " greater than the alignment (%u) of its section %pA"),
5143 1 << common_align, name, common_bfd,
5144 1 << normal_align, h->root.u.def.section);
5145 else
5146 _bfd_error_handler
5147 /* xgettext:c-format */
5148 (_("warning: alignment %u of symbol `%s' in %pB"
5149 " is smaller than %u in %pB"),
5150 1 << normal_align, name, normal_bfd,
5151 1 << common_align, common_bfd);
5152 }
5153 }
5154
5155 /* Remember the symbol size if it isn't undefined. */
5156 if (isym->st_size != 0
5157 && isym->st_shndx != SHN_UNDEF
5158 && (definition || h->size == 0))
5159 {
5160 if (h->size != 0
5161 && h->size != isym->st_size
5162 && ! size_change_ok)
5163 _bfd_error_handler
5164 /* xgettext:c-format */
5165 (_("warning: size of symbol `%s' changed"
5166 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5167 name, (uint64_t) h->size, old_bfd,
5168 (uint64_t) isym->st_size, abfd);
5169
5170 h->size = isym->st_size;
5171 }
5172
5173 /* If this is a common symbol, then we always want H->SIZE
5174 to be the size of the common symbol. The code just above
5175 won't fix the size if a common symbol becomes larger. We
5176 don't warn about a size change here, because that is
5177 covered by --warn-common. Allow changes between different
5178 function types. */
5179 if (h->root.type == bfd_link_hash_common)
5180 h->size = h->root.u.c.size;
5181
5182 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5183 && ((definition && !new_weak)
5184 || (old_weak && h->root.type == bfd_link_hash_common)
5185 || h->type == STT_NOTYPE))
5186 {
5187 unsigned int type = ELF_ST_TYPE (isym->st_info);
5188
5189 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5190 symbol. */
5191 if (type == STT_GNU_IFUNC
5192 && (abfd->flags & DYNAMIC) != 0)
5193 type = STT_FUNC;
5194
5195 if (h->type != type)
5196 {
5197 if (h->type != STT_NOTYPE && ! type_change_ok)
5198 /* xgettext:c-format */
5199 _bfd_error_handler
5200 (_("warning: type of symbol `%s' changed"
5201 " from %d to %d in %pB"),
5202 name, h->type, type, abfd);
5203
5204 h->type = type;
5205 }
5206 }
5207
5208 /* Merge st_other field. */
5209 elf_merge_st_other (abfd, h, isym->st_other, sec,
5210 definition, dynamic);
5211
5212 /* We don't want to make debug symbol dynamic. */
5213 if (definition
5214 && (sec->flags & SEC_DEBUGGING)
5215 && !bfd_link_relocatable (info))
5216 dynsym = FALSE;
5217
5218 /* Nor should we make plugin symbols dynamic. */
5219 if ((abfd->flags & BFD_PLUGIN) != 0)
5220 dynsym = FALSE;
5221
5222 if (definition)
5223 {
5224 h->target_internal = isym->st_target_internal;
5225 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5226 }
5227
5228 if (definition && !dynamic)
5229 {
5230 char *p = strchr (name, ELF_VER_CHR);
5231 if (p != NULL && p[1] != ELF_VER_CHR)
5232 {
5233 /* Queue non-default versions so that .symver x, x@FOO
5234 aliases can be checked. */
5235 if (!nondeflt_vers)
5236 {
5237 size_t amt = ((isymend - isym + 1)
5238 * sizeof (struct elf_link_hash_entry *));
5239 nondeflt_vers
5240 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5241 if (!nondeflt_vers)
5242 goto error_free_vers;
5243 }
5244 nondeflt_vers[nondeflt_vers_cnt++] = h;
5245 }
5246 }
5247
5248 if (dynsym && h->dynindx == -1)
5249 {
5250 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5251 goto error_free_vers;
5252 if (h->is_weakalias
5253 && weakdef (h)->dynindx == -1)
5254 {
5255 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5256 goto error_free_vers;
5257 }
5258 }
5259 else if (h->dynindx != -1)
5260 /* If the symbol already has a dynamic index, but
5261 visibility says it should not be visible, turn it into
5262 a local symbol. */
5263 switch (ELF_ST_VISIBILITY (h->other))
5264 {
5265 case STV_INTERNAL:
5266 case STV_HIDDEN:
5267 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5268 dynsym = FALSE;
5269 break;
5270 }
5271
5272 if (!add_needed
5273 && matched
5274 && definition
5275 && ((dynsym
5276 && h->ref_regular_nonweak)
5277 || (old_bfd != NULL
5278 && (old_bfd->flags & BFD_PLUGIN) != 0
5279 && bind != STB_WEAK)
5280 || (h->ref_dynamic_nonweak
5281 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5282 && !on_needed_list (elf_dt_name (abfd),
5283 htab->needed, NULL))))
5284 {
5285 const char *soname = elf_dt_name (abfd);
5286
5287 info->callbacks->minfo ("%!", soname, old_bfd,
5288 h->root.root.string);
5289
5290 /* A symbol from a library loaded via DT_NEEDED of some
5291 other library is referenced by a regular object.
5292 Add a DT_NEEDED entry for it. Issue an error if
5293 --no-add-needed is used and the reference was not
5294 a weak one. */
5295 if (old_bfd != NULL
5296 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5297 {
5298 _bfd_error_handler
5299 /* xgettext:c-format */
5300 (_("%pB: undefined reference to symbol '%s'"),
5301 old_bfd, name);
5302 bfd_set_error (bfd_error_missing_dso);
5303 goto error_free_vers;
5304 }
5305
5306 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5307 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5308
5309 /* Create dynamic sections for backends that require
5310 that be done before setup_gnu_properties. */
5311 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5312 return FALSE;
5313 add_needed = TRUE;
5314 }
5315 }
5316 }
5317
5318 if (info->lto_plugin_active
5319 && !bfd_link_relocatable (info)
5320 && (abfd->flags & BFD_PLUGIN) == 0
5321 && !just_syms
5322 && extsymcount)
5323 {
5324 int r_sym_shift;
5325
5326 if (bed->s->arch_size == 32)
5327 r_sym_shift = 8;
5328 else
5329 r_sym_shift = 32;
5330
5331 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5332 referenced in regular objects so that linker plugin will get
5333 the correct symbol resolution. */
5334
5335 sym_hash = elf_sym_hashes (abfd);
5336 for (s = abfd->sections; s != NULL; s = s->next)
5337 {
5338 Elf_Internal_Rela *internal_relocs;
5339 Elf_Internal_Rela *rel, *relend;
5340
5341 /* Don't check relocations in excluded sections. */
5342 if ((s->flags & SEC_RELOC) == 0
5343 || s->reloc_count == 0
5344 || (s->flags & SEC_EXCLUDE) != 0
5345 || ((info->strip == strip_all
5346 || info->strip == strip_debugger)
5347 && (s->flags & SEC_DEBUGGING) != 0))
5348 continue;
5349
5350 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5351 NULL,
5352 info->keep_memory);
5353 if (internal_relocs == NULL)
5354 goto error_free_vers;
5355
5356 rel = internal_relocs;
5357 relend = rel + s->reloc_count;
5358 for ( ; rel < relend; rel++)
5359 {
5360 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5361 struct elf_link_hash_entry *h;
5362
5363 /* Skip local symbols. */
5364 if (r_symndx < extsymoff)
5365 continue;
5366
5367 h = sym_hash[r_symndx - extsymoff];
5368 if (h != NULL)
5369 h->root.non_ir_ref_regular = 1;
5370 }
5371
5372 if (elf_section_data (s)->relocs != internal_relocs)
5373 free (internal_relocs);
5374 }
5375 }
5376
5377 free (extversym);
5378 extversym = NULL;
5379 free (isymbuf);
5380 isymbuf = NULL;
5381
5382 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5383 {
5384 unsigned int i;
5385
5386 /* Restore the symbol table. */
5387 old_ent = (char *) old_tab + tabsize;
5388 memset (elf_sym_hashes (abfd), 0,
5389 extsymcount * sizeof (struct elf_link_hash_entry *));
5390 htab->root.table.table = old_table;
5391 htab->root.table.size = old_size;
5392 htab->root.table.count = old_count;
5393 memcpy (htab->root.table.table, old_tab, tabsize);
5394 htab->root.undefs = old_undefs;
5395 htab->root.undefs_tail = old_undefs_tail;
5396 if (htab->dynstr != NULL)
5397 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5398 free (old_strtab);
5399 old_strtab = NULL;
5400 for (i = 0; i < htab->root.table.size; i++)
5401 {
5402 struct bfd_hash_entry *p;
5403 struct elf_link_hash_entry *h;
5404 unsigned int non_ir_ref_dynamic;
5405
5406 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5407 {
5408 /* Preserve non_ir_ref_dynamic so that this symbol
5409 will be exported when the dynamic lib becomes needed
5410 in the second pass. */
5411 h = (struct elf_link_hash_entry *) p;
5412 if (h->root.type == bfd_link_hash_warning)
5413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5414 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5415
5416 h = (struct elf_link_hash_entry *) p;
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
5419 if (h->root.type == bfd_link_hash_warning)
5420 {
5421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5422 memcpy (h, old_ent, htab->root.table.entsize);
5423 old_ent = (char *) old_ent + htab->root.table.entsize;
5424 }
5425 if (h->root.type == bfd_link_hash_common)
5426 {
5427 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5428 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5429 }
5430 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5431 }
5432 }
5433
5434 /* Make a special call to the linker "notice" function to
5435 tell it that symbols added for crefs may need to be removed. */
5436 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5437 goto error_free_vers;
5438
5439 free (old_tab);
5440 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5441 alloc_mark);
5442 free (nondeflt_vers);
5443 return TRUE;
5444 }
5445
5446 if (old_tab != NULL)
5447 {
5448 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5449 goto error_free_vers;
5450 free (old_tab);
5451 old_tab = NULL;
5452 }
5453
5454 /* Now that all the symbols from this input file are created, if
5455 not performing a relocatable link, handle .symver foo, foo@BAR
5456 such that any relocs against foo become foo@BAR. */
5457 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5458 {
5459 size_t cnt, symidx;
5460
5461 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5462 {
5463 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5464 char *shortname, *p;
5465 size_t amt;
5466
5467 p = strchr (h->root.root.string, ELF_VER_CHR);
5468 if (p == NULL
5469 || (h->root.type != bfd_link_hash_defined
5470 && h->root.type != bfd_link_hash_defweak))
5471 continue;
5472
5473 amt = p - h->root.root.string;
5474 shortname = (char *) bfd_malloc (amt + 1);
5475 if (!shortname)
5476 goto error_free_vers;
5477 memcpy (shortname, h->root.root.string, amt);
5478 shortname[amt] = '\0';
5479
5480 hi = (struct elf_link_hash_entry *)
5481 bfd_link_hash_lookup (&htab->root, shortname,
5482 FALSE, FALSE, FALSE);
5483 if (hi != NULL
5484 && hi->root.type == h->root.type
5485 && hi->root.u.def.value == h->root.u.def.value
5486 && hi->root.u.def.section == h->root.u.def.section)
5487 {
5488 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5489 hi->root.type = bfd_link_hash_indirect;
5490 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5491 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5492 sym_hash = elf_sym_hashes (abfd);
5493 if (sym_hash)
5494 for (symidx = 0; symidx < extsymcount; ++symidx)
5495 if (sym_hash[symidx] == hi)
5496 {
5497 sym_hash[symidx] = h;
5498 break;
5499 }
5500 }
5501 free (shortname);
5502 }
5503 free (nondeflt_vers);
5504 nondeflt_vers = NULL;
5505 }
5506
5507 /* Now set the alias field correctly for all the weak defined
5508 symbols we found. The only way to do this is to search all the
5509 symbols. Since we only need the information for non functions in
5510 dynamic objects, that's the only time we actually put anything on
5511 the list WEAKS. We need this information so that if a regular
5512 object refers to a symbol defined weakly in a dynamic object, the
5513 real symbol in the dynamic object is also put in the dynamic
5514 symbols; we also must arrange for both symbols to point to the
5515 same memory location. We could handle the general case of symbol
5516 aliasing, but a general symbol alias can only be generated in
5517 assembler code, handling it correctly would be very time
5518 consuming, and other ELF linkers don't handle general aliasing
5519 either. */
5520 if (weaks != NULL)
5521 {
5522 struct elf_link_hash_entry **hpp;
5523 struct elf_link_hash_entry **hppend;
5524 struct elf_link_hash_entry **sorted_sym_hash;
5525 struct elf_link_hash_entry *h;
5526 size_t sym_count, amt;
5527
5528 /* Since we have to search the whole symbol list for each weak
5529 defined symbol, search time for N weak defined symbols will be
5530 O(N^2). Binary search will cut it down to O(NlogN). */
5531 amt = extsymcount * sizeof (*sorted_sym_hash);
5532 sorted_sym_hash = bfd_malloc (amt);
5533 if (sorted_sym_hash == NULL)
5534 goto error_return;
5535 sym_hash = sorted_sym_hash;
5536 hpp = elf_sym_hashes (abfd);
5537 hppend = hpp + extsymcount;
5538 sym_count = 0;
5539 for (; hpp < hppend; hpp++)
5540 {
5541 h = *hpp;
5542 if (h != NULL
5543 && h->root.type == bfd_link_hash_defined
5544 && !bed->is_function_type (h->type))
5545 {
5546 *sym_hash = h;
5547 sym_hash++;
5548 sym_count++;
5549 }
5550 }
5551
5552 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5553 elf_sort_symbol);
5554
5555 while (weaks != NULL)
5556 {
5557 struct elf_link_hash_entry *hlook;
5558 asection *slook;
5559 bfd_vma vlook;
5560 size_t i, j, idx = 0;
5561
5562 hlook = weaks;
5563 weaks = hlook->u.alias;
5564 hlook->u.alias = NULL;
5565
5566 if (hlook->root.type != bfd_link_hash_defined
5567 && hlook->root.type != bfd_link_hash_defweak)
5568 continue;
5569
5570 slook = hlook->root.u.def.section;
5571 vlook = hlook->root.u.def.value;
5572
5573 i = 0;
5574 j = sym_count;
5575 while (i != j)
5576 {
5577 bfd_signed_vma vdiff;
5578 idx = (i + j) / 2;
5579 h = sorted_sym_hash[idx];
5580 vdiff = vlook - h->root.u.def.value;
5581 if (vdiff < 0)
5582 j = idx;
5583 else if (vdiff > 0)
5584 i = idx + 1;
5585 else
5586 {
5587 int sdiff = slook->id - h->root.u.def.section->id;
5588 if (sdiff < 0)
5589 j = idx;
5590 else if (sdiff > 0)
5591 i = idx + 1;
5592 else
5593 break;
5594 }
5595 }
5596
5597 /* We didn't find a value/section match. */
5598 if (i == j)
5599 continue;
5600
5601 /* With multiple aliases, or when the weak symbol is already
5602 strongly defined, we have multiple matching symbols and
5603 the binary search above may land on any of them. Step
5604 one past the matching symbol(s). */
5605 while (++idx != j)
5606 {
5607 h = sorted_sym_hash[idx];
5608 if (h->root.u.def.section != slook
5609 || h->root.u.def.value != vlook)
5610 break;
5611 }
5612
5613 /* Now look back over the aliases. Since we sorted by size
5614 as well as value and section, we'll choose the one with
5615 the largest size. */
5616 while (idx-- != i)
5617 {
5618 h = sorted_sym_hash[idx];
5619
5620 /* Stop if value or section doesn't match. */
5621 if (h->root.u.def.section != slook
5622 || h->root.u.def.value != vlook)
5623 break;
5624 else if (h != hlook)
5625 {
5626 struct elf_link_hash_entry *t;
5627
5628 hlook->u.alias = h;
5629 hlook->is_weakalias = 1;
5630 t = h;
5631 if (t->u.alias != NULL)
5632 while (t->u.alias != h)
5633 t = t->u.alias;
5634 t->u.alias = hlook;
5635
5636 /* If the weak definition is in the list of dynamic
5637 symbols, make sure the real definition is put
5638 there as well. */
5639 if (hlook->dynindx != -1 && h->dynindx == -1)
5640 {
5641 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5642 {
5643 err_free_sym_hash:
5644 free (sorted_sym_hash);
5645 goto error_return;
5646 }
5647 }
5648
5649 /* If the real definition is in the list of dynamic
5650 symbols, make sure the weak definition is put
5651 there as well. If we don't do this, then the
5652 dynamic loader might not merge the entries for the
5653 real definition and the weak definition. */
5654 if (h->dynindx != -1 && hlook->dynindx == -1)
5655 {
5656 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5657 goto err_free_sym_hash;
5658 }
5659 break;
5660 }
5661 }
5662 }
5663
5664 free (sorted_sym_hash);
5665 }
5666
5667 if (bed->check_directives
5668 && !(*bed->check_directives) (abfd, info))
5669 return FALSE;
5670
5671 /* If this is a non-traditional link, try to optimize the handling
5672 of the .stab/.stabstr sections. */
5673 if (! dynamic
5674 && ! info->traditional_format
5675 && is_elf_hash_table (htab)
5676 && (info->strip != strip_all && info->strip != strip_debugger))
5677 {
5678 asection *stabstr;
5679
5680 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5681 if (stabstr != NULL)
5682 {
5683 bfd_size_type string_offset = 0;
5684 asection *stab;
5685
5686 for (stab = abfd->sections; stab; stab = stab->next)
5687 if (CONST_STRNEQ (stab->name, ".stab")
5688 && (!stab->name[5] ||
5689 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5690 && (stab->flags & SEC_MERGE) == 0
5691 && !bfd_is_abs_section (stab->output_section))
5692 {
5693 struct bfd_elf_section_data *secdata;
5694
5695 secdata = elf_section_data (stab);
5696 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5697 stabstr, &secdata->sec_info,
5698 &string_offset))
5699 goto error_return;
5700 if (secdata->sec_info)
5701 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5702 }
5703 }
5704 }
5705
5706 if (dynamic && add_needed)
5707 {
5708 /* Add this bfd to the loaded list. */
5709 struct elf_link_loaded_list *n;
5710
5711 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5712 if (n == NULL)
5713 goto error_return;
5714 n->abfd = abfd;
5715 n->next = htab->dyn_loaded;
5716 htab->dyn_loaded = n;
5717 }
5718 if (dynamic && !add_needed
5719 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5720 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5721
5722 return TRUE;
5723
5724 error_free_vers:
5725 free (old_tab);
5726 free (old_strtab);
5727 free (nondeflt_vers);
5728 free (extversym);
5729 error_free_sym:
5730 free (isymbuf);
5731 error_return:
5732 return FALSE;
5733 }
5734
5735 /* Return the linker hash table entry of a symbol that might be
5736 satisfied by an archive symbol. Return -1 on error. */
5737
5738 struct elf_link_hash_entry *
5739 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5740 struct bfd_link_info *info,
5741 const char *name)
5742 {
5743 struct elf_link_hash_entry *h;
5744 char *p, *copy;
5745 size_t len, first;
5746
5747 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5748 if (h != NULL)
5749 return h;
5750
5751 /* If this is a default version (the name contains @@), look up the
5752 symbol again with only one `@' as well as without the version.
5753 The effect is that references to the symbol with and without the
5754 version will be matched by the default symbol in the archive. */
5755
5756 p = strchr (name, ELF_VER_CHR);
5757 if (p == NULL || p[1] != ELF_VER_CHR)
5758 return h;
5759
5760 /* First check with only one `@'. */
5761 len = strlen (name);
5762 copy = (char *) bfd_alloc (abfd, len);
5763 if (copy == NULL)
5764 return (struct elf_link_hash_entry *) -1;
5765
5766 first = p - name + 1;
5767 memcpy (copy, name, first);
5768 memcpy (copy + first, name + first + 1, len - first);
5769
5770 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5771 if (h == NULL)
5772 {
5773 /* We also need to check references to the symbol without the
5774 version. */
5775 copy[first - 1] = '\0';
5776 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5777 FALSE, FALSE, TRUE);
5778 }
5779
5780 bfd_release (abfd, copy);
5781 return h;
5782 }
5783
5784 /* Add symbols from an ELF archive file to the linker hash table. We
5785 don't use _bfd_generic_link_add_archive_symbols because we need to
5786 handle versioned symbols.
5787
5788 Fortunately, ELF archive handling is simpler than that done by
5789 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5790 oddities. In ELF, if we find a symbol in the archive map, and the
5791 symbol is currently undefined, we know that we must pull in that
5792 object file.
5793
5794 Unfortunately, we do have to make multiple passes over the symbol
5795 table until nothing further is resolved. */
5796
5797 static bfd_boolean
5798 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5799 {
5800 symindex c;
5801 unsigned char *included = NULL;
5802 carsym *symdefs;
5803 bfd_boolean loop;
5804 size_t amt;
5805 const struct elf_backend_data *bed;
5806 struct elf_link_hash_entry * (*archive_symbol_lookup)
5807 (bfd *, struct bfd_link_info *, const char *);
5808
5809 if (! bfd_has_map (abfd))
5810 {
5811 /* An empty archive is a special case. */
5812 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5813 return TRUE;
5814 bfd_set_error (bfd_error_no_armap);
5815 return FALSE;
5816 }
5817
5818 /* Keep track of all symbols we know to be already defined, and all
5819 files we know to be already included. This is to speed up the
5820 second and subsequent passes. */
5821 c = bfd_ardata (abfd)->symdef_count;
5822 if (c == 0)
5823 return TRUE;
5824 amt = c * sizeof (*included);
5825 included = (unsigned char *) bfd_zmalloc (amt);
5826 if (included == NULL)
5827 return FALSE;
5828
5829 symdefs = bfd_ardata (abfd)->symdefs;
5830 bed = get_elf_backend_data (abfd);
5831 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5832
5833 do
5834 {
5835 file_ptr last;
5836 symindex i;
5837 carsym *symdef;
5838 carsym *symdefend;
5839
5840 loop = FALSE;
5841 last = -1;
5842
5843 symdef = symdefs;
5844 symdefend = symdef + c;
5845 for (i = 0; symdef < symdefend; symdef++, i++)
5846 {
5847 struct elf_link_hash_entry *h;
5848 bfd *element;
5849 struct bfd_link_hash_entry *undefs_tail;
5850 symindex mark;
5851
5852 if (included[i])
5853 continue;
5854 if (symdef->file_offset == last)
5855 {
5856 included[i] = TRUE;
5857 continue;
5858 }
5859
5860 h = archive_symbol_lookup (abfd, info, symdef->name);
5861 if (h == (struct elf_link_hash_entry *) -1)
5862 goto error_return;
5863
5864 if (h == NULL)
5865 continue;
5866
5867 if (h->root.type == bfd_link_hash_undefined)
5868 {
5869 /* If the archive element has already been loaded then one
5870 of the symbols defined by that element might have been
5871 made undefined due to being in a discarded section. */
5872 if (h->indx == -3)
5873 continue;
5874 }
5875 else if (h->root.type == bfd_link_hash_common)
5876 {
5877 /* We currently have a common symbol. The archive map contains
5878 a reference to this symbol, so we may want to include it. We
5879 only want to include it however, if this archive element
5880 contains a definition of the symbol, not just another common
5881 declaration of it.
5882
5883 Unfortunately some archivers (including GNU ar) will put
5884 declarations of common symbols into their archive maps, as
5885 well as real definitions, so we cannot just go by the archive
5886 map alone. Instead we must read in the element's symbol
5887 table and check that to see what kind of symbol definition
5888 this is. */
5889 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5890 continue;
5891 }
5892 else
5893 {
5894 if (h->root.type != bfd_link_hash_undefweak)
5895 /* Symbol must be defined. Don't check it again. */
5896 included[i] = TRUE;
5897 continue;
5898 }
5899
5900 /* We need to include this archive member. */
5901 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5902 if (element == NULL)
5903 goto error_return;
5904
5905 if (! bfd_check_format (element, bfd_object))
5906 goto error_return;
5907
5908 undefs_tail = info->hash->undefs_tail;
5909
5910 if (!(*info->callbacks
5911 ->add_archive_element) (info, element, symdef->name, &element))
5912 continue;
5913 if (!bfd_link_add_symbols (element, info))
5914 goto error_return;
5915
5916 /* If there are any new undefined symbols, we need to make
5917 another pass through the archive in order to see whether
5918 they can be defined. FIXME: This isn't perfect, because
5919 common symbols wind up on undefs_tail and because an
5920 undefined symbol which is defined later on in this pass
5921 does not require another pass. This isn't a bug, but it
5922 does make the code less efficient than it could be. */
5923 if (undefs_tail != info->hash->undefs_tail)
5924 loop = TRUE;
5925
5926 /* Look backward to mark all symbols from this object file
5927 which we have already seen in this pass. */
5928 mark = i;
5929 do
5930 {
5931 included[mark] = TRUE;
5932 if (mark == 0)
5933 break;
5934 --mark;
5935 }
5936 while (symdefs[mark].file_offset == symdef->file_offset);
5937
5938 /* We mark subsequent symbols from this object file as we go
5939 on through the loop. */
5940 last = symdef->file_offset;
5941 }
5942 }
5943 while (loop);
5944
5945 free (included);
5946 return TRUE;
5947
5948 error_return:
5949 free (included);
5950 return FALSE;
5951 }
5952
5953 /* Given an ELF BFD, add symbols to the global hash table as
5954 appropriate. */
5955
5956 bfd_boolean
5957 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5958 {
5959 switch (bfd_get_format (abfd))
5960 {
5961 case bfd_object:
5962 return elf_link_add_object_symbols (abfd, info);
5963 case bfd_archive:
5964 return elf_link_add_archive_symbols (abfd, info);
5965 default:
5966 bfd_set_error (bfd_error_wrong_format);
5967 return FALSE;
5968 }
5969 }
5970 \f
5971 struct hash_codes_info
5972 {
5973 unsigned long *hashcodes;
5974 bfd_boolean error;
5975 };
5976
5977 /* This function will be called though elf_link_hash_traverse to store
5978 all hash value of the exported symbols in an array. */
5979
5980 static bfd_boolean
5981 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5982 {
5983 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5984 const char *name;
5985 unsigned long ha;
5986 char *alc = NULL;
5987
5988 /* Ignore indirect symbols. These are added by the versioning code. */
5989 if (h->dynindx == -1)
5990 return TRUE;
5991
5992 name = h->root.root.string;
5993 if (h->versioned >= versioned)
5994 {
5995 char *p = strchr (name, ELF_VER_CHR);
5996 if (p != NULL)
5997 {
5998 alc = (char *) bfd_malloc (p - name + 1);
5999 if (alc == NULL)
6000 {
6001 inf->error = TRUE;
6002 return FALSE;
6003 }
6004 memcpy (alc, name, p - name);
6005 alc[p - name] = '\0';
6006 name = alc;
6007 }
6008 }
6009
6010 /* Compute the hash value. */
6011 ha = bfd_elf_hash (name);
6012
6013 /* Store the found hash value in the array given as the argument. */
6014 *(inf->hashcodes)++ = ha;
6015
6016 /* And store it in the struct so that we can put it in the hash table
6017 later. */
6018 h->u.elf_hash_value = ha;
6019
6020 free (alc);
6021 return TRUE;
6022 }
6023
6024 struct collect_gnu_hash_codes
6025 {
6026 bfd *output_bfd;
6027 const struct elf_backend_data *bed;
6028 unsigned long int nsyms;
6029 unsigned long int maskbits;
6030 unsigned long int *hashcodes;
6031 unsigned long int *hashval;
6032 unsigned long int *indx;
6033 unsigned long int *counts;
6034 bfd_vma *bitmask;
6035 bfd_byte *contents;
6036 bfd_size_type xlat;
6037 long int min_dynindx;
6038 unsigned long int bucketcount;
6039 unsigned long int symindx;
6040 long int local_indx;
6041 long int shift1, shift2;
6042 unsigned long int mask;
6043 bfd_boolean error;
6044 };
6045
6046 /* This function will be called though elf_link_hash_traverse to store
6047 all hash value of the exported symbols in an array. */
6048
6049 static bfd_boolean
6050 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6051 {
6052 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6053 const char *name;
6054 unsigned long ha;
6055 char *alc = NULL;
6056
6057 /* Ignore indirect symbols. These are added by the versioning code. */
6058 if (h->dynindx == -1)
6059 return TRUE;
6060
6061 /* Ignore also local symbols and undefined symbols. */
6062 if (! (*s->bed->elf_hash_symbol) (h))
6063 return TRUE;
6064
6065 name = h->root.root.string;
6066 if (h->versioned >= versioned)
6067 {
6068 char *p = strchr (name, ELF_VER_CHR);
6069 if (p != NULL)
6070 {
6071 alc = (char *) bfd_malloc (p - name + 1);
6072 if (alc == NULL)
6073 {
6074 s->error = TRUE;
6075 return FALSE;
6076 }
6077 memcpy (alc, name, p - name);
6078 alc[p - name] = '\0';
6079 name = alc;
6080 }
6081 }
6082
6083 /* Compute the hash value. */
6084 ha = bfd_elf_gnu_hash (name);
6085
6086 /* Store the found hash value in the array for compute_bucket_count,
6087 and also for .dynsym reordering purposes. */
6088 s->hashcodes[s->nsyms] = ha;
6089 s->hashval[h->dynindx] = ha;
6090 ++s->nsyms;
6091 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6092 s->min_dynindx = h->dynindx;
6093
6094 free (alc);
6095 return TRUE;
6096 }
6097
6098 /* This function will be called though elf_link_hash_traverse to do
6099 final dynamic symbol renumbering in case of .gnu.hash.
6100 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6101 to the translation table. */
6102
6103 static bfd_boolean
6104 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6105 {
6106 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6107 unsigned long int bucket;
6108 unsigned long int val;
6109
6110 /* Ignore indirect symbols. */
6111 if (h->dynindx == -1)
6112 return TRUE;
6113
6114 /* Ignore also local symbols and undefined symbols. */
6115 if (! (*s->bed->elf_hash_symbol) (h))
6116 {
6117 if (h->dynindx >= s->min_dynindx)
6118 {
6119 if (s->bed->record_xhash_symbol != NULL)
6120 {
6121 (*s->bed->record_xhash_symbol) (h, 0);
6122 s->local_indx++;
6123 }
6124 else
6125 h->dynindx = s->local_indx++;
6126 }
6127 return TRUE;
6128 }
6129
6130 bucket = s->hashval[h->dynindx] % s->bucketcount;
6131 val = (s->hashval[h->dynindx] >> s->shift1)
6132 & ((s->maskbits >> s->shift1) - 1);
6133 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6134 s->bitmask[val]
6135 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6136 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6137 if (s->counts[bucket] == 1)
6138 /* Last element terminates the chain. */
6139 val |= 1;
6140 bfd_put_32 (s->output_bfd, val,
6141 s->contents + (s->indx[bucket] - s->symindx) * 4);
6142 --s->counts[bucket];
6143 if (s->bed->record_xhash_symbol != NULL)
6144 {
6145 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6146
6147 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6148 }
6149 else
6150 h->dynindx = s->indx[bucket]++;
6151 return TRUE;
6152 }
6153
6154 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6155
6156 bfd_boolean
6157 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6158 {
6159 return !(h->forced_local
6160 || h->root.type == bfd_link_hash_undefined
6161 || h->root.type == bfd_link_hash_undefweak
6162 || ((h->root.type == bfd_link_hash_defined
6163 || h->root.type == bfd_link_hash_defweak)
6164 && h->root.u.def.section->output_section == NULL));
6165 }
6166
6167 /* Array used to determine the number of hash table buckets to use
6168 based on the number of symbols there are. If there are fewer than
6169 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6170 fewer than 37 we use 17 buckets, and so forth. We never use more
6171 than 32771 buckets. */
6172
6173 static const size_t elf_buckets[] =
6174 {
6175 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6176 16411, 32771, 0
6177 };
6178
6179 /* Compute bucket count for hashing table. We do not use a static set
6180 of possible tables sizes anymore. Instead we determine for all
6181 possible reasonable sizes of the table the outcome (i.e., the
6182 number of collisions etc) and choose the best solution. The
6183 weighting functions are not too simple to allow the table to grow
6184 without bounds. Instead one of the weighting factors is the size.
6185 Therefore the result is always a good payoff between few collisions
6186 (= short chain lengths) and table size. */
6187 static size_t
6188 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6189 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6190 unsigned long int nsyms,
6191 int gnu_hash)
6192 {
6193 size_t best_size = 0;
6194 unsigned long int i;
6195
6196 /* We have a problem here. The following code to optimize the table
6197 size requires an integer type with more the 32 bits. If
6198 BFD_HOST_U_64_BIT is set we know about such a type. */
6199 #ifdef BFD_HOST_U_64_BIT
6200 if (info->optimize)
6201 {
6202 size_t minsize;
6203 size_t maxsize;
6204 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6205 bfd *dynobj = elf_hash_table (info)->dynobj;
6206 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6207 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6208 unsigned long int *counts;
6209 bfd_size_type amt;
6210 unsigned int no_improvement_count = 0;
6211
6212 /* Possible optimization parameters: if we have NSYMS symbols we say
6213 that the hashing table must at least have NSYMS/4 and at most
6214 2*NSYMS buckets. */
6215 minsize = nsyms / 4;
6216 if (minsize == 0)
6217 minsize = 1;
6218 best_size = maxsize = nsyms * 2;
6219 if (gnu_hash)
6220 {
6221 if (minsize < 2)
6222 minsize = 2;
6223 if ((best_size & 31) == 0)
6224 ++best_size;
6225 }
6226
6227 /* Create array where we count the collisions in. We must use bfd_malloc
6228 since the size could be large. */
6229 amt = maxsize;
6230 amt *= sizeof (unsigned long int);
6231 counts = (unsigned long int *) bfd_malloc (amt);
6232 if (counts == NULL)
6233 return 0;
6234
6235 /* Compute the "optimal" size for the hash table. The criteria is a
6236 minimal chain length. The minor criteria is (of course) the size
6237 of the table. */
6238 for (i = minsize; i < maxsize; ++i)
6239 {
6240 /* Walk through the array of hashcodes and count the collisions. */
6241 BFD_HOST_U_64_BIT max;
6242 unsigned long int j;
6243 unsigned long int fact;
6244
6245 if (gnu_hash && (i & 31) == 0)
6246 continue;
6247
6248 memset (counts, '\0', i * sizeof (unsigned long int));
6249
6250 /* Determine how often each hash bucket is used. */
6251 for (j = 0; j < nsyms; ++j)
6252 ++counts[hashcodes[j] % i];
6253
6254 /* For the weight function we need some information about the
6255 pagesize on the target. This is information need not be 100%
6256 accurate. Since this information is not available (so far) we
6257 define it here to a reasonable default value. If it is crucial
6258 to have a better value some day simply define this value. */
6259 # ifndef BFD_TARGET_PAGESIZE
6260 # define BFD_TARGET_PAGESIZE (4096)
6261 # endif
6262
6263 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6264 and the chains. */
6265 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6266
6267 # if 1
6268 /* Variant 1: optimize for short chains. We add the squares
6269 of all the chain lengths (which favors many small chain
6270 over a few long chains). */
6271 for (j = 0; j < i; ++j)
6272 max += counts[j] * counts[j];
6273
6274 /* This adds penalties for the overall size of the table. */
6275 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6276 max *= fact * fact;
6277 # else
6278 /* Variant 2: Optimize a lot more for small table. Here we
6279 also add squares of the size but we also add penalties for
6280 empty slots (the +1 term). */
6281 for (j = 0; j < i; ++j)
6282 max += (1 + counts[j]) * (1 + counts[j]);
6283
6284 /* The overall size of the table is considered, but not as
6285 strong as in variant 1, where it is squared. */
6286 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6287 max *= fact;
6288 # endif
6289
6290 /* Compare with current best results. */
6291 if (max < best_chlen)
6292 {
6293 best_chlen = max;
6294 best_size = i;
6295 no_improvement_count = 0;
6296 }
6297 /* PR 11843: Avoid futile long searches for the best bucket size
6298 when there are a large number of symbols. */
6299 else if (++no_improvement_count == 100)
6300 break;
6301 }
6302
6303 free (counts);
6304 }
6305 else
6306 #endif /* defined (BFD_HOST_U_64_BIT) */
6307 {
6308 /* This is the fallback solution if no 64bit type is available or if we
6309 are not supposed to spend much time on optimizations. We select the
6310 bucket count using a fixed set of numbers. */
6311 for (i = 0; elf_buckets[i] != 0; i++)
6312 {
6313 best_size = elf_buckets[i];
6314 if (nsyms < elf_buckets[i + 1])
6315 break;
6316 }
6317 if (gnu_hash && best_size < 2)
6318 best_size = 2;
6319 }
6320
6321 return best_size;
6322 }
6323
6324 /* Size any SHT_GROUP section for ld -r. */
6325
6326 bfd_boolean
6327 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6328 {
6329 bfd *ibfd;
6330 asection *s;
6331
6332 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6333 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6334 && (s = ibfd->sections) != NULL
6335 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6336 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6337 return FALSE;
6338 return TRUE;
6339 }
6340
6341 /* Set a default stack segment size. The value in INFO wins. If it
6342 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6343 undefined it is initialized. */
6344
6345 bfd_boolean
6346 bfd_elf_stack_segment_size (bfd *output_bfd,
6347 struct bfd_link_info *info,
6348 const char *legacy_symbol,
6349 bfd_vma default_size)
6350 {
6351 struct elf_link_hash_entry *h = NULL;
6352
6353 /* Look for legacy symbol. */
6354 if (legacy_symbol)
6355 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6356 FALSE, FALSE, FALSE);
6357 if (h && (h->root.type == bfd_link_hash_defined
6358 || h->root.type == bfd_link_hash_defweak)
6359 && h->def_regular
6360 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6361 {
6362 /* The symbol has no type if specified on the command line. */
6363 h->type = STT_OBJECT;
6364 if (info->stacksize)
6365 /* xgettext:c-format */
6366 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6367 output_bfd, legacy_symbol);
6368 else if (h->root.u.def.section != bfd_abs_section_ptr)
6369 /* xgettext:c-format */
6370 _bfd_error_handler (_("%pB: %s not absolute"),
6371 output_bfd, legacy_symbol);
6372 else
6373 info->stacksize = h->root.u.def.value;
6374 }
6375
6376 if (!info->stacksize)
6377 /* If the user didn't set a size, or explicitly inhibit the
6378 size, set it now. */
6379 info->stacksize = default_size;
6380
6381 /* Provide the legacy symbol, if it is referenced. */
6382 if (h && (h->root.type == bfd_link_hash_undefined
6383 || h->root.type == bfd_link_hash_undefweak))
6384 {
6385 struct bfd_link_hash_entry *bh = NULL;
6386
6387 if (!(_bfd_generic_link_add_one_symbol
6388 (info, output_bfd, legacy_symbol,
6389 BSF_GLOBAL, bfd_abs_section_ptr,
6390 info->stacksize >= 0 ? info->stacksize : 0,
6391 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6392 return FALSE;
6393
6394 h = (struct elf_link_hash_entry *) bh;
6395 h->def_regular = 1;
6396 h->type = STT_OBJECT;
6397 }
6398
6399 return TRUE;
6400 }
6401
6402 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6403
6404 struct elf_gc_sweep_symbol_info
6405 {
6406 struct bfd_link_info *info;
6407 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6408 bfd_boolean);
6409 };
6410
6411 static bfd_boolean
6412 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6413 {
6414 if (!h->mark
6415 && (((h->root.type == bfd_link_hash_defined
6416 || h->root.type == bfd_link_hash_defweak)
6417 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6418 && h->root.u.def.section->gc_mark))
6419 || h->root.type == bfd_link_hash_undefined
6420 || h->root.type == bfd_link_hash_undefweak))
6421 {
6422 struct elf_gc_sweep_symbol_info *inf;
6423
6424 inf = (struct elf_gc_sweep_symbol_info *) data;
6425 (*inf->hide_symbol) (inf->info, h, TRUE);
6426 h->def_regular = 0;
6427 h->ref_regular = 0;
6428 h->ref_regular_nonweak = 0;
6429 }
6430
6431 return TRUE;
6432 }
6433
6434 /* Set up the sizes and contents of the ELF dynamic sections. This is
6435 called by the ELF linker emulation before_allocation routine. We
6436 must set the sizes of the sections before the linker sets the
6437 addresses of the various sections. */
6438
6439 bfd_boolean
6440 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6441 const char *soname,
6442 const char *rpath,
6443 const char *filter_shlib,
6444 const char *audit,
6445 const char *depaudit,
6446 const char * const *auxiliary_filters,
6447 struct bfd_link_info *info,
6448 asection **sinterpptr)
6449 {
6450 bfd *dynobj;
6451 const struct elf_backend_data *bed;
6452
6453 *sinterpptr = NULL;
6454
6455 if (!is_elf_hash_table (info->hash))
6456 return TRUE;
6457
6458 dynobj = elf_hash_table (info)->dynobj;
6459
6460 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6461 {
6462 struct bfd_elf_version_tree *verdefs;
6463 struct elf_info_failed asvinfo;
6464 struct bfd_elf_version_tree *t;
6465 struct bfd_elf_version_expr *d;
6466 asection *s;
6467 size_t soname_indx;
6468
6469 /* If we are supposed to export all symbols into the dynamic symbol
6470 table (this is not the normal case), then do so. */
6471 if (info->export_dynamic
6472 || (bfd_link_executable (info) && info->dynamic))
6473 {
6474 struct elf_info_failed eif;
6475
6476 eif.info = info;
6477 eif.failed = FALSE;
6478 elf_link_hash_traverse (elf_hash_table (info),
6479 _bfd_elf_export_symbol,
6480 &eif);
6481 if (eif.failed)
6482 return FALSE;
6483 }
6484
6485 if (soname != NULL)
6486 {
6487 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6488 soname, TRUE);
6489 if (soname_indx == (size_t) -1
6490 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6491 return FALSE;
6492 }
6493 else
6494 soname_indx = (size_t) -1;
6495
6496 /* Make all global versions with definition. */
6497 for (t = info->version_info; t != NULL; t = t->next)
6498 for (d = t->globals.list; d != NULL; d = d->next)
6499 if (!d->symver && d->literal)
6500 {
6501 const char *verstr, *name;
6502 size_t namelen, verlen, newlen;
6503 char *newname, *p, leading_char;
6504 struct elf_link_hash_entry *newh;
6505
6506 leading_char = bfd_get_symbol_leading_char (output_bfd);
6507 name = d->pattern;
6508 namelen = strlen (name) + (leading_char != '\0');
6509 verstr = t->name;
6510 verlen = strlen (verstr);
6511 newlen = namelen + verlen + 3;
6512
6513 newname = (char *) bfd_malloc (newlen);
6514 if (newname == NULL)
6515 return FALSE;
6516 newname[0] = leading_char;
6517 memcpy (newname + (leading_char != '\0'), name, namelen);
6518
6519 /* Check the hidden versioned definition. */
6520 p = newname + namelen;
6521 *p++ = ELF_VER_CHR;
6522 memcpy (p, verstr, verlen + 1);
6523 newh = elf_link_hash_lookup (elf_hash_table (info),
6524 newname, FALSE, FALSE,
6525 FALSE);
6526 if (newh == NULL
6527 || (newh->root.type != bfd_link_hash_defined
6528 && newh->root.type != bfd_link_hash_defweak))
6529 {
6530 /* Check the default versioned definition. */
6531 *p++ = ELF_VER_CHR;
6532 memcpy (p, verstr, verlen + 1);
6533 newh = elf_link_hash_lookup (elf_hash_table (info),
6534 newname, FALSE, FALSE,
6535 FALSE);
6536 }
6537 free (newname);
6538
6539 /* Mark this version if there is a definition and it is
6540 not defined in a shared object. */
6541 if (newh != NULL
6542 && !newh->def_dynamic
6543 && (newh->root.type == bfd_link_hash_defined
6544 || newh->root.type == bfd_link_hash_defweak))
6545 d->symver = 1;
6546 }
6547
6548 /* Attach all the symbols to their version information. */
6549 asvinfo.info = info;
6550 asvinfo.failed = FALSE;
6551
6552 elf_link_hash_traverse (elf_hash_table (info),
6553 _bfd_elf_link_assign_sym_version,
6554 &asvinfo);
6555 if (asvinfo.failed)
6556 return FALSE;
6557
6558 if (!info->allow_undefined_version)
6559 {
6560 /* Check if all global versions have a definition. */
6561 bfd_boolean all_defined = TRUE;
6562 for (t = info->version_info; t != NULL; t = t->next)
6563 for (d = t->globals.list; d != NULL; d = d->next)
6564 if (d->literal && !d->symver && !d->script)
6565 {
6566 _bfd_error_handler
6567 (_("%s: undefined version: %s"),
6568 d->pattern, t->name);
6569 all_defined = FALSE;
6570 }
6571
6572 if (!all_defined)
6573 {
6574 bfd_set_error (bfd_error_bad_value);
6575 return FALSE;
6576 }
6577 }
6578
6579 /* Set up the version definition section. */
6580 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6581 BFD_ASSERT (s != NULL);
6582
6583 /* We may have created additional version definitions if we are
6584 just linking a regular application. */
6585 verdefs = info->version_info;
6586
6587 /* Skip anonymous version tag. */
6588 if (verdefs != NULL && verdefs->vernum == 0)
6589 verdefs = verdefs->next;
6590
6591 if (verdefs == NULL && !info->create_default_symver)
6592 s->flags |= SEC_EXCLUDE;
6593 else
6594 {
6595 unsigned int cdefs;
6596 bfd_size_type size;
6597 bfd_byte *p;
6598 Elf_Internal_Verdef def;
6599 Elf_Internal_Verdaux defaux;
6600 struct bfd_link_hash_entry *bh;
6601 struct elf_link_hash_entry *h;
6602 const char *name;
6603
6604 cdefs = 0;
6605 size = 0;
6606
6607 /* Make space for the base version. */
6608 size += sizeof (Elf_External_Verdef);
6609 size += sizeof (Elf_External_Verdaux);
6610 ++cdefs;
6611
6612 /* Make space for the default version. */
6613 if (info->create_default_symver)
6614 {
6615 size += sizeof (Elf_External_Verdef);
6616 ++cdefs;
6617 }
6618
6619 for (t = verdefs; t != NULL; t = t->next)
6620 {
6621 struct bfd_elf_version_deps *n;
6622
6623 /* Don't emit base version twice. */
6624 if (t->vernum == 0)
6625 continue;
6626
6627 size += sizeof (Elf_External_Verdef);
6628 size += sizeof (Elf_External_Verdaux);
6629 ++cdefs;
6630
6631 for (n = t->deps; n != NULL; n = n->next)
6632 size += sizeof (Elf_External_Verdaux);
6633 }
6634
6635 s->size = size;
6636 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6637 if (s->contents == NULL && s->size != 0)
6638 return FALSE;
6639
6640 /* Fill in the version definition section. */
6641
6642 p = s->contents;
6643
6644 def.vd_version = VER_DEF_CURRENT;
6645 def.vd_flags = VER_FLG_BASE;
6646 def.vd_ndx = 1;
6647 def.vd_cnt = 1;
6648 if (info->create_default_symver)
6649 {
6650 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6651 def.vd_next = sizeof (Elf_External_Verdef);
6652 }
6653 else
6654 {
6655 def.vd_aux = sizeof (Elf_External_Verdef);
6656 def.vd_next = (sizeof (Elf_External_Verdef)
6657 + sizeof (Elf_External_Verdaux));
6658 }
6659
6660 if (soname_indx != (size_t) -1)
6661 {
6662 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6663 soname_indx);
6664 def.vd_hash = bfd_elf_hash (soname);
6665 defaux.vda_name = soname_indx;
6666 name = soname;
6667 }
6668 else
6669 {
6670 size_t indx;
6671
6672 name = lbasename (bfd_get_filename (output_bfd));
6673 def.vd_hash = bfd_elf_hash (name);
6674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6675 name, FALSE);
6676 if (indx == (size_t) -1)
6677 return FALSE;
6678 defaux.vda_name = indx;
6679 }
6680 defaux.vda_next = 0;
6681
6682 _bfd_elf_swap_verdef_out (output_bfd, &def,
6683 (Elf_External_Verdef *) p);
6684 p += sizeof (Elf_External_Verdef);
6685 if (info->create_default_symver)
6686 {
6687 /* Add a symbol representing this version. */
6688 bh = NULL;
6689 if (! (_bfd_generic_link_add_one_symbol
6690 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6691 0, NULL, FALSE,
6692 get_elf_backend_data (dynobj)->collect, &bh)))
6693 return FALSE;
6694 h = (struct elf_link_hash_entry *) bh;
6695 h->non_elf = 0;
6696 h->def_regular = 1;
6697 h->type = STT_OBJECT;
6698 h->verinfo.vertree = NULL;
6699
6700 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6701 return FALSE;
6702
6703 /* Create a duplicate of the base version with the same
6704 aux block, but different flags. */
6705 def.vd_flags = 0;
6706 def.vd_ndx = 2;
6707 def.vd_aux = sizeof (Elf_External_Verdef);
6708 if (verdefs)
6709 def.vd_next = (sizeof (Elf_External_Verdef)
6710 + sizeof (Elf_External_Verdaux));
6711 else
6712 def.vd_next = 0;
6713 _bfd_elf_swap_verdef_out (output_bfd, &def,
6714 (Elf_External_Verdef *) p);
6715 p += sizeof (Elf_External_Verdef);
6716 }
6717 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6718 (Elf_External_Verdaux *) p);
6719 p += sizeof (Elf_External_Verdaux);
6720
6721 for (t = verdefs; t != NULL; t = t->next)
6722 {
6723 unsigned int cdeps;
6724 struct bfd_elf_version_deps *n;
6725
6726 /* Don't emit the base version twice. */
6727 if (t->vernum == 0)
6728 continue;
6729
6730 cdeps = 0;
6731 for (n = t->deps; n != NULL; n = n->next)
6732 ++cdeps;
6733
6734 /* Add a symbol representing this version. */
6735 bh = NULL;
6736 if (! (_bfd_generic_link_add_one_symbol
6737 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6738 0, NULL, FALSE,
6739 get_elf_backend_data (dynobj)->collect, &bh)))
6740 return FALSE;
6741 h = (struct elf_link_hash_entry *) bh;
6742 h->non_elf = 0;
6743 h->def_regular = 1;
6744 h->type = STT_OBJECT;
6745 h->verinfo.vertree = t;
6746
6747 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6748 return FALSE;
6749
6750 def.vd_version = VER_DEF_CURRENT;
6751 def.vd_flags = 0;
6752 if (t->globals.list == NULL
6753 && t->locals.list == NULL
6754 && ! t->used)
6755 def.vd_flags |= VER_FLG_WEAK;
6756 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6757 def.vd_cnt = cdeps + 1;
6758 def.vd_hash = bfd_elf_hash (t->name);
6759 def.vd_aux = sizeof (Elf_External_Verdef);
6760 def.vd_next = 0;
6761
6762 /* If a basever node is next, it *must* be the last node in
6763 the chain, otherwise Verdef construction breaks. */
6764 if (t->next != NULL && t->next->vernum == 0)
6765 BFD_ASSERT (t->next->next == NULL);
6766
6767 if (t->next != NULL && t->next->vernum != 0)
6768 def.vd_next = (sizeof (Elf_External_Verdef)
6769 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6770
6771 _bfd_elf_swap_verdef_out (output_bfd, &def,
6772 (Elf_External_Verdef *) p);
6773 p += sizeof (Elf_External_Verdef);
6774
6775 defaux.vda_name = h->dynstr_index;
6776 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6777 h->dynstr_index);
6778 defaux.vda_next = 0;
6779 if (t->deps != NULL)
6780 defaux.vda_next = sizeof (Elf_External_Verdaux);
6781 t->name_indx = defaux.vda_name;
6782
6783 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6784 (Elf_External_Verdaux *) p);
6785 p += sizeof (Elf_External_Verdaux);
6786
6787 for (n = t->deps; n != NULL; n = n->next)
6788 {
6789 if (n->version_needed == NULL)
6790 {
6791 /* This can happen if there was an error in the
6792 version script. */
6793 defaux.vda_name = 0;
6794 }
6795 else
6796 {
6797 defaux.vda_name = n->version_needed->name_indx;
6798 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6799 defaux.vda_name);
6800 }
6801 if (n->next == NULL)
6802 defaux.vda_next = 0;
6803 else
6804 defaux.vda_next = sizeof (Elf_External_Verdaux);
6805
6806 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6807 (Elf_External_Verdaux *) p);
6808 p += sizeof (Elf_External_Verdaux);
6809 }
6810 }
6811
6812 elf_tdata (output_bfd)->cverdefs = cdefs;
6813 }
6814 }
6815
6816 bed = get_elf_backend_data (output_bfd);
6817
6818 if (info->gc_sections && bed->can_gc_sections)
6819 {
6820 struct elf_gc_sweep_symbol_info sweep_info;
6821
6822 /* Remove the symbols that were in the swept sections from the
6823 dynamic symbol table. */
6824 sweep_info.info = info;
6825 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6826 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6827 &sweep_info);
6828 }
6829
6830 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6831 {
6832 asection *s;
6833 struct elf_find_verdep_info sinfo;
6834
6835 /* Work out the size of the version reference section. */
6836
6837 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6838 BFD_ASSERT (s != NULL);
6839
6840 sinfo.info = info;
6841 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6842 if (sinfo.vers == 0)
6843 sinfo.vers = 1;
6844 sinfo.failed = FALSE;
6845
6846 elf_link_hash_traverse (elf_hash_table (info),
6847 _bfd_elf_link_find_version_dependencies,
6848 &sinfo);
6849 if (sinfo.failed)
6850 return FALSE;
6851
6852 if (elf_tdata (output_bfd)->verref == NULL)
6853 s->flags |= SEC_EXCLUDE;
6854 else
6855 {
6856 Elf_Internal_Verneed *vn;
6857 unsigned int size;
6858 unsigned int crefs;
6859 bfd_byte *p;
6860
6861 /* Build the version dependency section. */
6862 size = 0;
6863 crefs = 0;
6864 for (vn = elf_tdata (output_bfd)->verref;
6865 vn != NULL;
6866 vn = vn->vn_nextref)
6867 {
6868 Elf_Internal_Vernaux *a;
6869
6870 size += sizeof (Elf_External_Verneed);
6871 ++crefs;
6872 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6873 size += sizeof (Elf_External_Vernaux);
6874 }
6875
6876 s->size = size;
6877 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6878 if (s->contents == NULL)
6879 return FALSE;
6880
6881 p = s->contents;
6882 for (vn = elf_tdata (output_bfd)->verref;
6883 vn != NULL;
6884 vn = vn->vn_nextref)
6885 {
6886 unsigned int caux;
6887 Elf_Internal_Vernaux *a;
6888 size_t indx;
6889
6890 caux = 0;
6891 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6892 ++caux;
6893
6894 vn->vn_version = VER_NEED_CURRENT;
6895 vn->vn_cnt = caux;
6896 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6897 elf_dt_name (vn->vn_bfd) != NULL
6898 ? elf_dt_name (vn->vn_bfd)
6899 : lbasename (bfd_get_filename
6900 (vn->vn_bfd)),
6901 FALSE);
6902 if (indx == (size_t) -1)
6903 return FALSE;
6904 vn->vn_file = indx;
6905 vn->vn_aux = sizeof (Elf_External_Verneed);
6906 if (vn->vn_nextref == NULL)
6907 vn->vn_next = 0;
6908 else
6909 vn->vn_next = (sizeof (Elf_External_Verneed)
6910 + caux * sizeof (Elf_External_Vernaux));
6911
6912 _bfd_elf_swap_verneed_out (output_bfd, vn,
6913 (Elf_External_Verneed *) p);
6914 p += sizeof (Elf_External_Verneed);
6915
6916 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6917 {
6918 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6919 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6920 a->vna_nodename, FALSE);
6921 if (indx == (size_t) -1)
6922 return FALSE;
6923 a->vna_name = indx;
6924 if (a->vna_nextptr == NULL)
6925 a->vna_next = 0;
6926 else
6927 a->vna_next = sizeof (Elf_External_Vernaux);
6928
6929 _bfd_elf_swap_vernaux_out (output_bfd, a,
6930 (Elf_External_Vernaux *) p);
6931 p += sizeof (Elf_External_Vernaux);
6932 }
6933 }
6934
6935 elf_tdata (output_bfd)->cverrefs = crefs;
6936 }
6937 }
6938
6939 /* Any syms created from now on start with -1 in
6940 got.refcount/offset and plt.refcount/offset. */
6941 elf_hash_table (info)->init_got_refcount
6942 = elf_hash_table (info)->init_got_offset;
6943 elf_hash_table (info)->init_plt_refcount
6944 = elf_hash_table (info)->init_plt_offset;
6945
6946 if (bfd_link_relocatable (info)
6947 && !_bfd_elf_size_group_sections (info))
6948 return FALSE;
6949
6950 /* The backend may have to create some sections regardless of whether
6951 we're dynamic or not. */
6952 if (bed->elf_backend_always_size_sections
6953 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6954 return FALSE;
6955
6956 /* Determine any GNU_STACK segment requirements, after the backend
6957 has had a chance to set a default segment size. */
6958 if (info->execstack)
6959 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6960 else if (info->noexecstack)
6961 elf_stack_flags (output_bfd) = PF_R | PF_W;
6962 else
6963 {
6964 bfd *inputobj;
6965 asection *notesec = NULL;
6966 int exec = 0;
6967
6968 for (inputobj = info->input_bfds;
6969 inputobj;
6970 inputobj = inputobj->link.next)
6971 {
6972 asection *s;
6973
6974 if (inputobj->flags
6975 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6976 continue;
6977 s = inputobj->sections;
6978 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6979 continue;
6980
6981 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6982 if (s)
6983 {
6984 if (s->flags & SEC_CODE)
6985 exec = PF_X;
6986 notesec = s;
6987 }
6988 else if (bed->default_execstack)
6989 exec = PF_X;
6990 }
6991 if (notesec || info->stacksize > 0)
6992 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6993 if (notesec && exec && bfd_link_relocatable (info)
6994 && notesec->output_section != bfd_abs_section_ptr)
6995 notesec->output_section->flags |= SEC_CODE;
6996 }
6997
6998 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6999 {
7000 struct elf_info_failed eif;
7001 struct elf_link_hash_entry *h;
7002 asection *dynstr;
7003 asection *s;
7004
7005 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7006 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7007
7008 if (info->symbolic)
7009 {
7010 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7011 return FALSE;
7012 info->flags |= DF_SYMBOLIC;
7013 }
7014
7015 if (rpath != NULL)
7016 {
7017 size_t indx;
7018 bfd_vma tag;
7019
7020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7021 TRUE);
7022 if (indx == (size_t) -1)
7023 return FALSE;
7024
7025 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7026 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7027 return FALSE;
7028 }
7029
7030 if (filter_shlib != NULL)
7031 {
7032 size_t indx;
7033
7034 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7035 filter_shlib, TRUE);
7036 if (indx == (size_t) -1
7037 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7038 return FALSE;
7039 }
7040
7041 if (auxiliary_filters != NULL)
7042 {
7043 const char * const *p;
7044
7045 for (p = auxiliary_filters; *p != NULL; p++)
7046 {
7047 size_t indx;
7048
7049 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7050 *p, TRUE);
7051 if (indx == (size_t) -1
7052 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7053 return FALSE;
7054 }
7055 }
7056
7057 if (audit != NULL)
7058 {
7059 size_t indx;
7060
7061 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7062 TRUE);
7063 if (indx == (size_t) -1
7064 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7065 return FALSE;
7066 }
7067
7068 if (depaudit != NULL)
7069 {
7070 size_t indx;
7071
7072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7073 TRUE);
7074 if (indx == (size_t) -1
7075 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7076 return FALSE;
7077 }
7078
7079 eif.info = info;
7080 eif.failed = FALSE;
7081
7082 /* Find all symbols which were defined in a dynamic object and make
7083 the backend pick a reasonable value for them. */
7084 elf_link_hash_traverse (elf_hash_table (info),
7085 _bfd_elf_adjust_dynamic_symbol,
7086 &eif);
7087 if (eif.failed)
7088 return FALSE;
7089
7090 /* Add some entries to the .dynamic section. We fill in some of the
7091 values later, in bfd_elf_final_link, but we must add the entries
7092 now so that we know the final size of the .dynamic section. */
7093
7094 /* If there are initialization and/or finalization functions to
7095 call then add the corresponding DT_INIT/DT_FINI entries. */
7096 h = (info->init_function
7097 ? elf_link_hash_lookup (elf_hash_table (info),
7098 info->init_function, FALSE,
7099 FALSE, FALSE)
7100 : NULL);
7101 if (h != NULL
7102 && (h->ref_regular
7103 || h->def_regular))
7104 {
7105 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7106 return FALSE;
7107 }
7108 h = (info->fini_function
7109 ? elf_link_hash_lookup (elf_hash_table (info),
7110 info->fini_function, FALSE,
7111 FALSE, FALSE)
7112 : NULL);
7113 if (h != NULL
7114 && (h->ref_regular
7115 || h->def_regular))
7116 {
7117 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7118 return FALSE;
7119 }
7120
7121 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7122 if (s != NULL && s->linker_has_input)
7123 {
7124 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7125 if (! bfd_link_executable (info))
7126 {
7127 bfd *sub;
7128 asection *o;
7129
7130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7131 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7132 && (o = sub->sections) != NULL
7133 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7134 for (o = sub->sections; o != NULL; o = o->next)
7135 if (elf_section_data (o)->this_hdr.sh_type
7136 == SHT_PREINIT_ARRAY)
7137 {
7138 _bfd_error_handler
7139 (_("%pB: .preinit_array section is not allowed in DSO"),
7140 sub);
7141 break;
7142 }
7143
7144 bfd_set_error (bfd_error_nonrepresentable_section);
7145 return FALSE;
7146 }
7147
7148 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7149 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7150 return FALSE;
7151 }
7152 s = bfd_get_section_by_name (output_bfd, ".init_array");
7153 if (s != NULL && s->linker_has_input)
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7156 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7157 return FALSE;
7158 }
7159 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7160 if (s != NULL && s->linker_has_input)
7161 {
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7163 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7164 return FALSE;
7165 }
7166
7167 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7168 /* If .dynstr is excluded from the link, we don't want any of
7169 these tags. Strictly, we should be checking each section
7170 individually; This quick check covers for the case where
7171 someone does a /DISCARD/ : { *(*) }. */
7172 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7173 {
7174 bfd_size_type strsize;
7175
7176 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7177 if ((info->emit_hash
7178 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7179 || (info->emit_gnu_hash
7180 && (bed->record_xhash_symbol == NULL
7181 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7182 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7183 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7184 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7185 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7186 bed->s->sizeof_sym)
7187 || (info->gnu_flags_1
7188 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7189 info->gnu_flags_1)))
7190 return FALSE;
7191 }
7192 }
7193
7194 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7195 return FALSE;
7196
7197 /* The backend must work out the sizes of all the other dynamic
7198 sections. */
7199 if (dynobj != NULL
7200 && bed->elf_backend_size_dynamic_sections != NULL
7201 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7202 return FALSE;
7203
7204 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7205 {
7206 if (elf_tdata (output_bfd)->cverdefs)
7207 {
7208 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7209
7210 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7211 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7212 return FALSE;
7213 }
7214
7215 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7216 {
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7218 return FALSE;
7219 }
7220 else if (info->flags & DF_BIND_NOW)
7221 {
7222 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7223 return FALSE;
7224 }
7225
7226 if (info->flags_1)
7227 {
7228 if (bfd_link_executable (info))
7229 info->flags_1 &= ~ (DF_1_INITFIRST
7230 | DF_1_NODELETE
7231 | DF_1_NOOPEN);
7232 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7233 return FALSE;
7234 }
7235
7236 if (elf_tdata (output_bfd)->cverrefs)
7237 {
7238 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7239
7240 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7241 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7242 return FALSE;
7243 }
7244
7245 if ((elf_tdata (output_bfd)->cverrefs == 0
7246 && elf_tdata (output_bfd)->cverdefs == 0)
7247 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7248 {
7249 asection *s;
7250
7251 s = bfd_get_linker_section (dynobj, ".gnu.version");
7252 s->flags |= SEC_EXCLUDE;
7253 }
7254 }
7255 return TRUE;
7256 }
7257
7258 /* Find the first non-excluded output section. We'll use its
7259 section symbol for some emitted relocs. */
7260 void
7261 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7262 {
7263 asection *s;
7264 asection *found = NULL;
7265
7266 for (s = output_bfd->sections; s != NULL; s = s->next)
7267 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7268 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7269 {
7270 found = s;
7271 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7272 break;
7273 }
7274 elf_hash_table (info)->text_index_section = found;
7275 }
7276
7277 /* Find two non-excluded output sections, one for code, one for data.
7278 We'll use their section symbols for some emitted relocs. */
7279 void
7280 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7281 {
7282 asection *s;
7283 asection *found = NULL;
7284
7285 /* Data first, since setting text_index_section changes
7286 _bfd_elf_omit_section_dynsym_default. */
7287 for (s = output_bfd->sections; s != NULL; s = s->next)
7288 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7289 && !(s->flags & SEC_READONLY)
7290 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7291 {
7292 found = s;
7293 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7294 break;
7295 }
7296 elf_hash_table (info)->data_index_section = found;
7297
7298 for (s = output_bfd->sections; s != NULL; s = s->next)
7299 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7300 && (s->flags & SEC_READONLY)
7301 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7302 {
7303 found = s;
7304 break;
7305 }
7306 elf_hash_table (info)->text_index_section = found;
7307 }
7308
7309 #define GNU_HASH_SECTION_NAME(bed) \
7310 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7311
7312 bfd_boolean
7313 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7314 {
7315 const struct elf_backend_data *bed;
7316 unsigned long section_sym_count;
7317 bfd_size_type dynsymcount = 0;
7318
7319 if (!is_elf_hash_table (info->hash))
7320 return TRUE;
7321
7322 bed = get_elf_backend_data (output_bfd);
7323 (*bed->elf_backend_init_index_section) (output_bfd, info);
7324
7325 /* Assign dynsym indices. In a shared library we generate a section
7326 symbol for each output section, which come first. Next come all
7327 of the back-end allocated local dynamic syms, followed by the rest
7328 of the global symbols.
7329
7330 This is usually not needed for static binaries, however backends
7331 can request to always do it, e.g. the MIPS backend uses dynamic
7332 symbol counts to lay out GOT, which will be produced in the
7333 presence of GOT relocations even in static binaries (holding fixed
7334 data in that case, to satisfy those relocations). */
7335
7336 if (elf_hash_table (info)->dynamic_sections_created
7337 || bed->always_renumber_dynsyms)
7338 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7339 &section_sym_count);
7340
7341 if (elf_hash_table (info)->dynamic_sections_created)
7342 {
7343 bfd *dynobj;
7344 asection *s;
7345 unsigned int dtagcount;
7346
7347 dynobj = elf_hash_table (info)->dynobj;
7348
7349 /* Work out the size of the symbol version section. */
7350 s = bfd_get_linker_section (dynobj, ".gnu.version");
7351 BFD_ASSERT (s != NULL);
7352 if ((s->flags & SEC_EXCLUDE) == 0)
7353 {
7354 s->size = dynsymcount * sizeof (Elf_External_Versym);
7355 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7356 if (s->contents == NULL)
7357 return FALSE;
7358
7359 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7360 return FALSE;
7361 }
7362
7363 /* Set the size of the .dynsym and .hash sections. We counted
7364 the number of dynamic symbols in elf_link_add_object_symbols.
7365 We will build the contents of .dynsym and .hash when we build
7366 the final symbol table, because until then we do not know the
7367 correct value to give the symbols. We built the .dynstr
7368 section as we went along in elf_link_add_object_symbols. */
7369 s = elf_hash_table (info)->dynsym;
7370 BFD_ASSERT (s != NULL);
7371 s->size = dynsymcount * bed->s->sizeof_sym;
7372
7373 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7374 if (s->contents == NULL)
7375 return FALSE;
7376
7377 /* The first entry in .dynsym is a dummy symbol. Clear all the
7378 section syms, in case we don't output them all. */
7379 ++section_sym_count;
7380 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7381
7382 elf_hash_table (info)->bucketcount = 0;
7383
7384 /* Compute the size of the hashing table. As a side effect this
7385 computes the hash values for all the names we export. */
7386 if (info->emit_hash)
7387 {
7388 unsigned long int *hashcodes;
7389 struct hash_codes_info hashinf;
7390 bfd_size_type amt;
7391 unsigned long int nsyms;
7392 size_t bucketcount;
7393 size_t hash_entry_size;
7394
7395 /* Compute the hash values for all exported symbols. At the same
7396 time store the values in an array so that we could use them for
7397 optimizations. */
7398 amt = dynsymcount * sizeof (unsigned long int);
7399 hashcodes = (unsigned long int *) bfd_malloc (amt);
7400 if (hashcodes == NULL)
7401 return FALSE;
7402 hashinf.hashcodes = hashcodes;
7403 hashinf.error = FALSE;
7404
7405 /* Put all hash values in HASHCODES. */
7406 elf_link_hash_traverse (elf_hash_table (info),
7407 elf_collect_hash_codes, &hashinf);
7408 if (hashinf.error)
7409 {
7410 free (hashcodes);
7411 return FALSE;
7412 }
7413
7414 nsyms = hashinf.hashcodes - hashcodes;
7415 bucketcount
7416 = compute_bucket_count (info, hashcodes, nsyms, 0);
7417 free (hashcodes);
7418
7419 if (bucketcount == 0 && nsyms > 0)
7420 return FALSE;
7421
7422 elf_hash_table (info)->bucketcount = bucketcount;
7423
7424 s = bfd_get_linker_section (dynobj, ".hash");
7425 BFD_ASSERT (s != NULL);
7426 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7427 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7428 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7429 if (s->contents == NULL)
7430 return FALSE;
7431
7432 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7433 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7434 s->contents + hash_entry_size);
7435 }
7436
7437 if (info->emit_gnu_hash)
7438 {
7439 size_t i, cnt;
7440 unsigned char *contents;
7441 struct collect_gnu_hash_codes cinfo;
7442 bfd_size_type amt;
7443 size_t bucketcount;
7444
7445 memset (&cinfo, 0, sizeof (cinfo));
7446
7447 /* Compute the hash values for all exported symbols. At the same
7448 time store the values in an array so that we could use them for
7449 optimizations. */
7450 amt = dynsymcount * 2 * sizeof (unsigned long int);
7451 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7452 if (cinfo.hashcodes == NULL)
7453 return FALSE;
7454
7455 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7456 cinfo.min_dynindx = -1;
7457 cinfo.output_bfd = output_bfd;
7458 cinfo.bed = bed;
7459
7460 /* Put all hash values in HASHCODES. */
7461 elf_link_hash_traverse (elf_hash_table (info),
7462 elf_collect_gnu_hash_codes, &cinfo);
7463 if (cinfo.error)
7464 {
7465 free (cinfo.hashcodes);
7466 return FALSE;
7467 }
7468
7469 bucketcount
7470 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7471
7472 if (bucketcount == 0)
7473 {
7474 free (cinfo.hashcodes);
7475 return FALSE;
7476 }
7477
7478 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7479 BFD_ASSERT (s != NULL);
7480
7481 if (cinfo.nsyms == 0)
7482 {
7483 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7484 BFD_ASSERT (cinfo.min_dynindx == -1);
7485 free (cinfo.hashcodes);
7486 s->size = 5 * 4 + bed->s->arch_size / 8;
7487 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7488 if (contents == NULL)
7489 return FALSE;
7490 s->contents = contents;
7491 /* 1 empty bucket. */
7492 bfd_put_32 (output_bfd, 1, contents);
7493 /* SYMIDX above the special symbol 0. */
7494 bfd_put_32 (output_bfd, 1, contents + 4);
7495 /* Just one word for bitmask. */
7496 bfd_put_32 (output_bfd, 1, contents + 8);
7497 /* Only hash fn bloom filter. */
7498 bfd_put_32 (output_bfd, 0, contents + 12);
7499 /* No hashes are valid - empty bitmask. */
7500 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7501 /* No hashes in the only bucket. */
7502 bfd_put_32 (output_bfd, 0,
7503 contents + 16 + bed->s->arch_size / 8);
7504 }
7505 else
7506 {
7507 unsigned long int maskwords, maskbitslog2, x;
7508 BFD_ASSERT (cinfo.min_dynindx != -1);
7509
7510 x = cinfo.nsyms;
7511 maskbitslog2 = 1;
7512 while ((x >>= 1) != 0)
7513 ++maskbitslog2;
7514 if (maskbitslog2 < 3)
7515 maskbitslog2 = 5;
7516 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7517 maskbitslog2 = maskbitslog2 + 3;
7518 else
7519 maskbitslog2 = maskbitslog2 + 2;
7520 if (bed->s->arch_size == 64)
7521 {
7522 if (maskbitslog2 == 5)
7523 maskbitslog2 = 6;
7524 cinfo.shift1 = 6;
7525 }
7526 else
7527 cinfo.shift1 = 5;
7528 cinfo.mask = (1 << cinfo.shift1) - 1;
7529 cinfo.shift2 = maskbitslog2;
7530 cinfo.maskbits = 1 << maskbitslog2;
7531 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7532 amt = bucketcount * sizeof (unsigned long int) * 2;
7533 amt += maskwords * sizeof (bfd_vma);
7534 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7535 if (cinfo.bitmask == NULL)
7536 {
7537 free (cinfo.hashcodes);
7538 return FALSE;
7539 }
7540
7541 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7542 cinfo.indx = cinfo.counts + bucketcount;
7543 cinfo.symindx = dynsymcount - cinfo.nsyms;
7544 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7545
7546 /* Determine how often each hash bucket is used. */
7547 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7548 for (i = 0; i < cinfo.nsyms; ++i)
7549 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7550
7551 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7552 if (cinfo.counts[i] != 0)
7553 {
7554 cinfo.indx[i] = cnt;
7555 cnt += cinfo.counts[i];
7556 }
7557 BFD_ASSERT (cnt == dynsymcount);
7558 cinfo.bucketcount = bucketcount;
7559 cinfo.local_indx = cinfo.min_dynindx;
7560
7561 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7562 s->size += cinfo.maskbits / 8;
7563 if (bed->record_xhash_symbol != NULL)
7564 s->size += cinfo.nsyms * 4;
7565 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7566 if (contents == NULL)
7567 {
7568 free (cinfo.bitmask);
7569 free (cinfo.hashcodes);
7570 return FALSE;
7571 }
7572
7573 s->contents = contents;
7574 bfd_put_32 (output_bfd, bucketcount, contents);
7575 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7576 bfd_put_32 (output_bfd, maskwords, contents + 8);
7577 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7578 contents += 16 + cinfo.maskbits / 8;
7579
7580 for (i = 0; i < bucketcount; ++i)
7581 {
7582 if (cinfo.counts[i] == 0)
7583 bfd_put_32 (output_bfd, 0, contents);
7584 else
7585 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7586 contents += 4;
7587 }
7588
7589 cinfo.contents = contents;
7590
7591 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7592 /* Renumber dynamic symbols, if populating .gnu.hash section.
7593 If using .MIPS.xhash, populate the translation table. */
7594 elf_link_hash_traverse (elf_hash_table (info),
7595 elf_gnu_hash_process_symidx, &cinfo);
7596
7597 contents = s->contents + 16;
7598 for (i = 0; i < maskwords; ++i)
7599 {
7600 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7601 contents);
7602 contents += bed->s->arch_size / 8;
7603 }
7604
7605 free (cinfo.bitmask);
7606 free (cinfo.hashcodes);
7607 }
7608 }
7609
7610 s = bfd_get_linker_section (dynobj, ".dynstr");
7611 BFD_ASSERT (s != NULL);
7612
7613 elf_finalize_dynstr (output_bfd, info);
7614
7615 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7616
7617 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7618 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7619 return FALSE;
7620 }
7621
7622 return TRUE;
7623 }
7624 \f
7625 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7626
7627 static void
7628 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7629 asection *sec)
7630 {
7631 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7632 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7633 }
7634
7635 /* Finish SHF_MERGE section merging. */
7636
7637 bfd_boolean
7638 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7639 {
7640 bfd *ibfd;
7641 asection *sec;
7642
7643 if (!is_elf_hash_table (info->hash))
7644 return FALSE;
7645
7646 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7647 if ((ibfd->flags & DYNAMIC) == 0
7648 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7649 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7650 == get_elf_backend_data (obfd)->s->elfclass))
7651 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7652 if ((sec->flags & SEC_MERGE) != 0
7653 && !bfd_is_abs_section (sec->output_section))
7654 {
7655 struct bfd_elf_section_data *secdata;
7656
7657 secdata = elf_section_data (sec);
7658 if (! _bfd_add_merge_section (obfd,
7659 &elf_hash_table (info)->merge_info,
7660 sec, &secdata->sec_info))
7661 return FALSE;
7662 else if (secdata->sec_info)
7663 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7664 }
7665
7666 if (elf_hash_table (info)->merge_info != NULL)
7667 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7668 merge_sections_remove_hook);
7669 return TRUE;
7670 }
7671
7672 /* Create an entry in an ELF linker hash table. */
7673
7674 struct bfd_hash_entry *
7675 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7676 struct bfd_hash_table *table,
7677 const char *string)
7678 {
7679 /* Allocate the structure if it has not already been allocated by a
7680 subclass. */
7681 if (entry == NULL)
7682 {
7683 entry = (struct bfd_hash_entry *)
7684 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7685 if (entry == NULL)
7686 return entry;
7687 }
7688
7689 /* Call the allocation method of the superclass. */
7690 entry = _bfd_link_hash_newfunc (entry, table, string);
7691 if (entry != NULL)
7692 {
7693 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7694 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7695
7696 /* Set local fields. */
7697 ret->indx = -1;
7698 ret->dynindx = -1;
7699 ret->got = htab->init_got_refcount;
7700 ret->plt = htab->init_plt_refcount;
7701 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7702 - offsetof (struct elf_link_hash_entry, size)));
7703 /* Assume that we have been called by a non-ELF symbol reader.
7704 This flag is then reset by the code which reads an ELF input
7705 file. This ensures that a symbol created by a non-ELF symbol
7706 reader will have the flag set correctly. */
7707 ret->non_elf = 1;
7708 }
7709
7710 return entry;
7711 }
7712
7713 /* Copy data from an indirect symbol to its direct symbol, hiding the
7714 old indirect symbol. Also used for copying flags to a weakdef. */
7715
7716 void
7717 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7718 struct elf_link_hash_entry *dir,
7719 struct elf_link_hash_entry *ind)
7720 {
7721 struct elf_link_hash_table *htab;
7722
7723 if (ind->dyn_relocs != NULL)
7724 {
7725 if (dir->dyn_relocs != NULL)
7726 {
7727 struct elf_dyn_relocs **pp;
7728 struct elf_dyn_relocs *p;
7729
7730 /* Add reloc counts against the indirect sym to the direct sym
7731 list. Merge any entries against the same section. */
7732 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7733 {
7734 struct elf_dyn_relocs *q;
7735
7736 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7737 if (q->sec == p->sec)
7738 {
7739 q->pc_count += p->pc_count;
7740 q->count += p->count;
7741 *pp = p->next;
7742 break;
7743 }
7744 if (q == NULL)
7745 pp = &p->next;
7746 }
7747 *pp = dir->dyn_relocs;
7748 }
7749
7750 dir->dyn_relocs = ind->dyn_relocs;
7751 ind->dyn_relocs = NULL;
7752 }
7753
7754 /* Copy down any references that we may have already seen to the
7755 symbol which just became indirect. */
7756
7757 if (dir->versioned != versioned_hidden)
7758 dir->ref_dynamic |= ind->ref_dynamic;
7759 dir->ref_regular |= ind->ref_regular;
7760 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7761 dir->non_got_ref |= ind->non_got_ref;
7762 dir->needs_plt |= ind->needs_plt;
7763 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7764
7765 if (ind->root.type != bfd_link_hash_indirect)
7766 return;
7767
7768 /* Copy over the global and procedure linkage table refcount entries.
7769 These may have been already set up by a check_relocs routine. */
7770 htab = elf_hash_table (info);
7771 if (ind->got.refcount > htab->init_got_refcount.refcount)
7772 {
7773 if (dir->got.refcount < 0)
7774 dir->got.refcount = 0;
7775 dir->got.refcount += ind->got.refcount;
7776 ind->got.refcount = htab->init_got_refcount.refcount;
7777 }
7778
7779 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7780 {
7781 if (dir->plt.refcount < 0)
7782 dir->plt.refcount = 0;
7783 dir->plt.refcount += ind->plt.refcount;
7784 ind->plt.refcount = htab->init_plt_refcount.refcount;
7785 }
7786
7787 if (ind->dynindx != -1)
7788 {
7789 if (dir->dynindx != -1)
7790 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7791 dir->dynindx = ind->dynindx;
7792 dir->dynstr_index = ind->dynstr_index;
7793 ind->dynindx = -1;
7794 ind->dynstr_index = 0;
7795 }
7796 }
7797
7798 void
7799 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7800 struct elf_link_hash_entry *h,
7801 bfd_boolean force_local)
7802 {
7803 /* STT_GNU_IFUNC symbol must go through PLT. */
7804 if (h->type != STT_GNU_IFUNC)
7805 {
7806 h->plt = elf_hash_table (info)->init_plt_offset;
7807 h->needs_plt = 0;
7808 }
7809 if (force_local)
7810 {
7811 h->forced_local = 1;
7812 if (h->dynindx != -1)
7813 {
7814 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7815 h->dynstr_index);
7816 h->dynindx = -1;
7817 h->dynstr_index = 0;
7818 }
7819 }
7820 }
7821
7822 /* Hide a symbol. */
7823
7824 void
7825 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7826 struct bfd_link_info *info,
7827 struct bfd_link_hash_entry *h)
7828 {
7829 if (is_elf_hash_table (info->hash))
7830 {
7831 const struct elf_backend_data *bed
7832 = get_elf_backend_data (output_bfd);
7833 struct elf_link_hash_entry *eh
7834 = (struct elf_link_hash_entry *) h;
7835 bed->elf_backend_hide_symbol (info, eh, TRUE);
7836 eh->def_dynamic = 0;
7837 eh->ref_dynamic = 0;
7838 eh->dynamic_def = 0;
7839 }
7840 }
7841
7842 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7843 caller. */
7844
7845 bfd_boolean
7846 _bfd_elf_link_hash_table_init
7847 (struct elf_link_hash_table *table,
7848 bfd *abfd,
7849 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7850 struct bfd_hash_table *,
7851 const char *),
7852 unsigned int entsize,
7853 enum elf_target_id target_id)
7854 {
7855 bfd_boolean ret;
7856 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7857
7858 table->init_got_refcount.refcount = can_refcount - 1;
7859 table->init_plt_refcount.refcount = can_refcount - 1;
7860 table->init_got_offset.offset = -(bfd_vma) 1;
7861 table->init_plt_offset.offset = -(bfd_vma) 1;
7862 /* The first dynamic symbol is a dummy. */
7863 table->dynsymcount = 1;
7864
7865 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7866
7867 table->root.type = bfd_link_elf_hash_table;
7868 table->hash_table_id = target_id;
7869 table->target_os = get_elf_backend_data (abfd)->target_os;
7870
7871 return ret;
7872 }
7873
7874 /* Create an ELF linker hash table. */
7875
7876 struct bfd_link_hash_table *
7877 _bfd_elf_link_hash_table_create (bfd *abfd)
7878 {
7879 struct elf_link_hash_table *ret;
7880 size_t amt = sizeof (struct elf_link_hash_table);
7881
7882 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7883 if (ret == NULL)
7884 return NULL;
7885
7886 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7887 sizeof (struct elf_link_hash_entry),
7888 GENERIC_ELF_DATA))
7889 {
7890 free (ret);
7891 return NULL;
7892 }
7893 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7894
7895 return &ret->root;
7896 }
7897
7898 /* Destroy an ELF linker hash table. */
7899
7900 void
7901 _bfd_elf_link_hash_table_free (bfd *obfd)
7902 {
7903 struct elf_link_hash_table *htab;
7904
7905 htab = (struct elf_link_hash_table *) obfd->link.hash;
7906 if (htab->dynstr != NULL)
7907 _bfd_elf_strtab_free (htab->dynstr);
7908 _bfd_merge_sections_free (htab->merge_info);
7909 _bfd_generic_link_hash_table_free (obfd);
7910 }
7911
7912 /* This is a hook for the ELF emulation code in the generic linker to
7913 tell the backend linker what file name to use for the DT_NEEDED
7914 entry for a dynamic object. */
7915
7916 void
7917 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7918 {
7919 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7920 && bfd_get_format (abfd) == bfd_object)
7921 elf_dt_name (abfd) = name;
7922 }
7923
7924 int
7925 bfd_elf_get_dyn_lib_class (bfd *abfd)
7926 {
7927 int lib_class;
7928 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd) == bfd_object)
7930 lib_class = elf_dyn_lib_class (abfd);
7931 else
7932 lib_class = 0;
7933 return lib_class;
7934 }
7935
7936 void
7937 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7938 {
7939 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7940 && bfd_get_format (abfd) == bfd_object)
7941 elf_dyn_lib_class (abfd) = lib_class;
7942 }
7943
7944 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7945 the linker ELF emulation code. */
7946
7947 struct bfd_link_needed_list *
7948 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7949 struct bfd_link_info *info)
7950 {
7951 if (! is_elf_hash_table (info->hash))
7952 return NULL;
7953 return elf_hash_table (info)->needed;
7954 }
7955
7956 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7957 hook for the linker ELF emulation code. */
7958
7959 struct bfd_link_needed_list *
7960 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7961 struct bfd_link_info *info)
7962 {
7963 if (! is_elf_hash_table (info->hash))
7964 return NULL;
7965 return elf_hash_table (info)->runpath;
7966 }
7967
7968 /* Get the name actually used for a dynamic object for a link. This
7969 is the SONAME entry if there is one. Otherwise, it is the string
7970 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7971
7972 const char *
7973 bfd_elf_get_dt_soname (bfd *abfd)
7974 {
7975 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7976 && bfd_get_format (abfd) == bfd_object)
7977 return elf_dt_name (abfd);
7978 return NULL;
7979 }
7980
7981 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7982 the ELF linker emulation code. */
7983
7984 bfd_boolean
7985 bfd_elf_get_bfd_needed_list (bfd *abfd,
7986 struct bfd_link_needed_list **pneeded)
7987 {
7988 asection *s;
7989 bfd_byte *dynbuf = NULL;
7990 unsigned int elfsec;
7991 unsigned long shlink;
7992 bfd_byte *extdyn, *extdynend;
7993 size_t extdynsize;
7994 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7995
7996 *pneeded = NULL;
7997
7998 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7999 || bfd_get_format (abfd) != bfd_object)
8000 return TRUE;
8001
8002 s = bfd_get_section_by_name (abfd, ".dynamic");
8003 if (s == NULL || s->size == 0)
8004 return TRUE;
8005
8006 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8007 goto error_return;
8008
8009 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8010 if (elfsec == SHN_BAD)
8011 goto error_return;
8012
8013 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8014
8015 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8016 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8017
8018 extdyn = dynbuf;
8019 extdynend = extdyn + s->size;
8020 for (; extdyn < extdynend; extdyn += extdynsize)
8021 {
8022 Elf_Internal_Dyn dyn;
8023
8024 (*swap_dyn_in) (abfd, extdyn, &dyn);
8025
8026 if (dyn.d_tag == DT_NULL)
8027 break;
8028
8029 if (dyn.d_tag == DT_NEEDED)
8030 {
8031 const char *string;
8032 struct bfd_link_needed_list *l;
8033 unsigned int tagv = dyn.d_un.d_val;
8034 size_t amt;
8035
8036 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8037 if (string == NULL)
8038 goto error_return;
8039
8040 amt = sizeof *l;
8041 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8042 if (l == NULL)
8043 goto error_return;
8044
8045 l->by = abfd;
8046 l->name = string;
8047 l->next = *pneeded;
8048 *pneeded = l;
8049 }
8050 }
8051
8052 free (dynbuf);
8053
8054 return TRUE;
8055
8056 error_return:
8057 free (dynbuf);
8058 return FALSE;
8059 }
8060
8061 struct elf_symbuf_symbol
8062 {
8063 unsigned long st_name; /* Symbol name, index in string tbl */
8064 unsigned char st_info; /* Type and binding attributes */
8065 unsigned char st_other; /* Visibilty, and target specific */
8066 };
8067
8068 struct elf_symbuf_head
8069 {
8070 struct elf_symbuf_symbol *ssym;
8071 size_t count;
8072 unsigned int st_shndx;
8073 };
8074
8075 struct elf_symbol
8076 {
8077 union
8078 {
8079 Elf_Internal_Sym *isym;
8080 struct elf_symbuf_symbol *ssym;
8081 void *p;
8082 } u;
8083 const char *name;
8084 };
8085
8086 /* Sort references to symbols by ascending section number. */
8087
8088 static int
8089 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8090 {
8091 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8092 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8093
8094 if (s1->st_shndx != s2->st_shndx)
8095 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8096 /* Final sort by the address of the sym in the symbuf ensures
8097 a stable sort. */
8098 if (s1 != s2)
8099 return s1 > s2 ? 1 : -1;
8100 return 0;
8101 }
8102
8103 static int
8104 elf_sym_name_compare (const void *arg1, const void *arg2)
8105 {
8106 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8107 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8108 int ret = strcmp (s1->name, s2->name);
8109 if (ret != 0)
8110 return ret;
8111 if (s1->u.p != s2->u.p)
8112 return s1->u.p > s2->u.p ? 1 : -1;
8113 return 0;
8114 }
8115
8116 static struct elf_symbuf_head *
8117 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8118 {
8119 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8120 struct elf_symbuf_symbol *ssym;
8121 struct elf_symbuf_head *ssymbuf, *ssymhead;
8122 size_t i, shndx_count, total_size, amt;
8123
8124 amt = symcount * sizeof (*indbuf);
8125 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8126 if (indbuf == NULL)
8127 return NULL;
8128
8129 /* NB: When checking if 2 sections define the same set of local and
8130 global symbols, ignore both undefined and section symbols in the
8131 symbol table. */
8132 for (ind = indbuf, i = 0; i < symcount; i++)
8133 if (isymbuf[i].st_shndx != SHN_UNDEF
8134 && ELF_ST_TYPE (isymbuf[i].st_info) != STT_SECTION)
8135 *ind++ = &isymbuf[i];
8136 indbufend = ind;
8137
8138 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8139 elf_sort_elf_symbol);
8140
8141 shndx_count = 0;
8142 if (indbufend > indbuf)
8143 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8144 if (ind[0]->st_shndx != ind[1]->st_shndx)
8145 shndx_count++;
8146
8147 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8148 + (indbufend - indbuf) * sizeof (*ssym));
8149 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8150 if (ssymbuf == NULL)
8151 {
8152 free (indbuf);
8153 return NULL;
8154 }
8155
8156 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8157 ssymbuf->ssym = NULL;
8158 ssymbuf->count = shndx_count;
8159 ssymbuf->st_shndx = 0;
8160 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8161 {
8162 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8163 {
8164 ssymhead++;
8165 ssymhead->ssym = ssym;
8166 ssymhead->count = 0;
8167 ssymhead->st_shndx = (*ind)->st_shndx;
8168 }
8169 ssym->st_name = (*ind)->st_name;
8170 ssym->st_info = (*ind)->st_info;
8171 ssym->st_other = (*ind)->st_other;
8172 ssymhead->count++;
8173 }
8174 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8175 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8176 == total_size));
8177
8178 free (indbuf);
8179 return ssymbuf;
8180 }
8181
8182 /* Check if 2 sections define the same set of local and global
8183 symbols. */
8184
8185 static bfd_boolean
8186 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8187 struct bfd_link_info *info)
8188 {
8189 bfd *bfd1, *bfd2;
8190 const struct elf_backend_data *bed1, *bed2;
8191 Elf_Internal_Shdr *hdr1, *hdr2;
8192 size_t symcount1, symcount2;
8193 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8194 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8195 Elf_Internal_Sym *isym, *isymend;
8196 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8197 size_t count1, count2, i;
8198 unsigned int shndx1, shndx2;
8199 bfd_boolean result;
8200
8201 bfd1 = sec1->owner;
8202 bfd2 = sec2->owner;
8203
8204 /* Both sections have to be in ELF. */
8205 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8206 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8207 return FALSE;
8208
8209 if (elf_section_type (sec1) != elf_section_type (sec2))
8210 return FALSE;
8211
8212 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8213 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8214 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8215 return FALSE;
8216
8217 bed1 = get_elf_backend_data (bfd1);
8218 bed2 = get_elf_backend_data (bfd2);
8219 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8220 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8221 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8222 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8223
8224 if (symcount1 == 0 || symcount2 == 0)
8225 return FALSE;
8226
8227 result = FALSE;
8228 isymbuf1 = NULL;
8229 isymbuf2 = NULL;
8230 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8231 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8232
8233 if (ssymbuf1 == NULL)
8234 {
8235 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8236 NULL, NULL, NULL);
8237 if (isymbuf1 == NULL)
8238 goto done;
8239
8240 if (info != NULL && !info->reduce_memory_overheads)
8241 {
8242 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8243 elf_tdata (bfd1)->symbuf = ssymbuf1;
8244 }
8245 }
8246
8247 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8248 {
8249 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8250 NULL, NULL, NULL);
8251 if (isymbuf2 == NULL)
8252 goto done;
8253
8254 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8255 {
8256 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8257 elf_tdata (bfd2)->symbuf = ssymbuf2;
8258 }
8259 }
8260
8261 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8262 {
8263 /* Optimized faster version. */
8264 size_t lo, hi, mid;
8265 struct elf_symbol *symp;
8266 struct elf_symbuf_symbol *ssym, *ssymend;
8267
8268 lo = 0;
8269 hi = ssymbuf1->count;
8270 ssymbuf1++;
8271 count1 = 0;
8272 while (lo < hi)
8273 {
8274 mid = (lo + hi) / 2;
8275 if (shndx1 < ssymbuf1[mid].st_shndx)
8276 hi = mid;
8277 else if (shndx1 > ssymbuf1[mid].st_shndx)
8278 lo = mid + 1;
8279 else
8280 {
8281 count1 = ssymbuf1[mid].count;
8282 ssymbuf1 += mid;
8283 break;
8284 }
8285 }
8286
8287 lo = 0;
8288 hi = ssymbuf2->count;
8289 ssymbuf2++;
8290 count2 = 0;
8291 while (lo < hi)
8292 {
8293 mid = (lo + hi) / 2;
8294 if (shndx2 < ssymbuf2[mid].st_shndx)
8295 hi = mid;
8296 else if (shndx2 > ssymbuf2[mid].st_shndx)
8297 lo = mid + 1;
8298 else
8299 {
8300 count2 = ssymbuf2[mid].count;
8301 ssymbuf2 += mid;
8302 break;
8303 }
8304 }
8305
8306 if (count1 == 0 || count2 == 0 || count1 != count2)
8307 goto done;
8308
8309 symtable1
8310 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8311 symtable2
8312 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8313 if (symtable1 == NULL || symtable2 == NULL)
8314 goto done;
8315
8316 symp = symtable1;
8317 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8318 ssym < ssymend; ssym++, symp++)
8319 {
8320 symp->u.ssym = ssym;
8321 symp->name = bfd_elf_string_from_elf_section (bfd1,
8322 hdr1->sh_link,
8323 ssym->st_name);
8324 }
8325
8326 symp = symtable2;
8327 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8328 ssym < ssymend; ssym++, symp++)
8329 {
8330 symp->u.ssym = ssym;
8331 symp->name = bfd_elf_string_from_elf_section (bfd2,
8332 hdr2->sh_link,
8333 ssym->st_name);
8334 }
8335
8336 /* Sort symbol by name. */
8337 qsort (symtable1, count1, sizeof (struct elf_symbol),
8338 elf_sym_name_compare);
8339 qsort (symtable2, count1, sizeof (struct elf_symbol),
8340 elf_sym_name_compare);
8341
8342 for (i = 0; i < count1; i++)
8343 /* Two symbols must have the same binding, type and name. */
8344 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8345 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8346 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8347 goto done;
8348
8349 result = TRUE;
8350 goto done;
8351 }
8352
8353 symtable1 = (struct elf_symbol *)
8354 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8355 symtable2 = (struct elf_symbol *)
8356 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8357 if (symtable1 == NULL || symtable2 == NULL)
8358 goto done;
8359
8360 /* Count definitions in the section. */
8361 count1 = 0;
8362 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8363 if (isym->st_shndx == shndx1)
8364 symtable1[count1++].u.isym = isym;
8365
8366 count2 = 0;
8367 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8368 if (isym->st_shndx == shndx2)
8369 symtable2[count2++].u.isym = isym;
8370
8371 if (count1 == 0 || count2 == 0 || count1 != count2)
8372 goto done;
8373
8374 for (i = 0; i < count1; i++)
8375 symtable1[i].name
8376 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8377 symtable1[i].u.isym->st_name);
8378
8379 for (i = 0; i < count2; i++)
8380 symtable2[i].name
8381 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8382 symtable2[i].u.isym->st_name);
8383
8384 /* Sort symbol by name. */
8385 qsort (symtable1, count1, sizeof (struct elf_symbol),
8386 elf_sym_name_compare);
8387 qsort (symtable2, count1, sizeof (struct elf_symbol),
8388 elf_sym_name_compare);
8389
8390 for (i = 0; i < count1; i++)
8391 /* Two symbols must have the same binding, type and name. */
8392 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8393 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8394 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8395 goto done;
8396
8397 result = TRUE;
8398
8399 done:
8400 free (symtable1);
8401 free (symtable2);
8402 free (isymbuf1);
8403 free (isymbuf2);
8404
8405 return result;
8406 }
8407
8408 /* Return TRUE if 2 section types are compatible. */
8409
8410 bfd_boolean
8411 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8412 bfd *bbfd, const asection *bsec)
8413 {
8414 if (asec == NULL
8415 || bsec == NULL
8416 || abfd->xvec->flavour != bfd_target_elf_flavour
8417 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8418 return TRUE;
8419
8420 return elf_section_type (asec) == elf_section_type (bsec);
8421 }
8422 \f
8423 /* Final phase of ELF linker. */
8424
8425 /* A structure we use to avoid passing large numbers of arguments. */
8426
8427 struct elf_final_link_info
8428 {
8429 /* General link information. */
8430 struct bfd_link_info *info;
8431 /* Output BFD. */
8432 bfd *output_bfd;
8433 /* Symbol string table. */
8434 struct elf_strtab_hash *symstrtab;
8435 /* .hash section. */
8436 asection *hash_sec;
8437 /* symbol version section (.gnu.version). */
8438 asection *symver_sec;
8439 /* Buffer large enough to hold contents of any section. */
8440 bfd_byte *contents;
8441 /* Buffer large enough to hold external relocs of any section. */
8442 void *external_relocs;
8443 /* Buffer large enough to hold internal relocs of any section. */
8444 Elf_Internal_Rela *internal_relocs;
8445 /* Buffer large enough to hold external local symbols of any input
8446 BFD. */
8447 bfd_byte *external_syms;
8448 /* And a buffer for symbol section indices. */
8449 Elf_External_Sym_Shndx *locsym_shndx;
8450 /* Buffer large enough to hold internal local symbols of any input
8451 BFD. */
8452 Elf_Internal_Sym *internal_syms;
8453 /* Array large enough to hold a symbol index for each local symbol
8454 of any input BFD. */
8455 long *indices;
8456 /* Array large enough to hold a section pointer for each local
8457 symbol of any input BFD. */
8458 asection **sections;
8459 /* Buffer for SHT_SYMTAB_SHNDX section. */
8460 Elf_External_Sym_Shndx *symshndxbuf;
8461 /* Number of STT_FILE syms seen. */
8462 size_t filesym_count;
8463 /* Local symbol hash table. */
8464 struct bfd_hash_table local_hash_table;
8465 };
8466
8467 struct local_hash_entry
8468 {
8469 /* Base hash table entry structure. */
8470 struct bfd_hash_entry root;
8471 /* Size of the local symbol name. */
8472 size_t size;
8473 /* Number of the duplicated local symbol names. */
8474 long count;
8475 };
8476
8477 /* Create an entry in the local symbol hash table. */
8478
8479 static struct bfd_hash_entry *
8480 local_hash_newfunc (struct bfd_hash_entry *entry,
8481 struct bfd_hash_table *table,
8482 const char *string)
8483 {
8484
8485 /* Allocate the structure if it has not already been allocated by a
8486 subclass. */
8487 if (entry == NULL)
8488 {
8489 entry = bfd_hash_allocate (table,
8490 sizeof (struct local_hash_entry));
8491 if (entry == NULL)
8492 return entry;
8493 }
8494
8495 /* Call the allocation method of the superclass. */
8496 entry = bfd_hash_newfunc (entry, table, string);
8497 if (entry != NULL)
8498 {
8499 ((struct local_hash_entry *) entry)->count = 0;
8500 ((struct local_hash_entry *) entry)->size = 0;
8501 }
8502
8503 return entry;
8504 }
8505
8506 /* This struct is used to pass information to elf_link_output_extsym. */
8507
8508 struct elf_outext_info
8509 {
8510 bfd_boolean failed;
8511 bfd_boolean localsyms;
8512 bfd_boolean file_sym_done;
8513 struct elf_final_link_info *flinfo;
8514 };
8515
8516
8517 /* Support for evaluating a complex relocation.
8518
8519 Complex relocations are generalized, self-describing relocations. The
8520 implementation of them consists of two parts: complex symbols, and the
8521 relocations themselves.
8522
8523 The relocations use a reserved elf-wide relocation type code (R_RELC
8524 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8525 information (start bit, end bit, word width, etc) into the addend. This
8526 information is extracted from CGEN-generated operand tables within gas.
8527
8528 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8529 internal) representing prefix-notation expressions, including but not
8530 limited to those sorts of expressions normally encoded as addends in the
8531 addend field. The symbol mangling format is:
8532
8533 <node> := <literal>
8534 | <unary-operator> ':' <node>
8535 | <binary-operator> ':' <node> ':' <node>
8536 ;
8537
8538 <literal> := 's' <digits=N> ':' <N character symbol name>
8539 | 'S' <digits=N> ':' <N character section name>
8540 | '#' <hexdigits>
8541 ;
8542
8543 <binary-operator> := as in C
8544 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8545
8546 static void
8547 set_symbol_value (bfd *bfd_with_globals,
8548 Elf_Internal_Sym *isymbuf,
8549 size_t locsymcount,
8550 size_t symidx,
8551 bfd_vma val)
8552 {
8553 struct elf_link_hash_entry **sym_hashes;
8554 struct elf_link_hash_entry *h;
8555 size_t extsymoff = locsymcount;
8556
8557 if (symidx < locsymcount)
8558 {
8559 Elf_Internal_Sym *sym;
8560
8561 sym = isymbuf + symidx;
8562 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8563 {
8564 /* It is a local symbol: move it to the
8565 "absolute" section and give it a value. */
8566 sym->st_shndx = SHN_ABS;
8567 sym->st_value = val;
8568 return;
8569 }
8570 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8571 extsymoff = 0;
8572 }
8573
8574 /* It is a global symbol: set its link type
8575 to "defined" and give it a value. */
8576
8577 sym_hashes = elf_sym_hashes (bfd_with_globals);
8578 h = sym_hashes [symidx - extsymoff];
8579 while (h->root.type == bfd_link_hash_indirect
8580 || h->root.type == bfd_link_hash_warning)
8581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8582 h->root.type = bfd_link_hash_defined;
8583 h->root.u.def.value = val;
8584 h->root.u.def.section = bfd_abs_section_ptr;
8585 }
8586
8587 static bfd_boolean
8588 resolve_symbol (const char *name,
8589 bfd *input_bfd,
8590 struct elf_final_link_info *flinfo,
8591 bfd_vma *result,
8592 Elf_Internal_Sym *isymbuf,
8593 size_t locsymcount)
8594 {
8595 Elf_Internal_Sym *sym;
8596 struct bfd_link_hash_entry *global_entry;
8597 const char *candidate = NULL;
8598 Elf_Internal_Shdr *symtab_hdr;
8599 size_t i;
8600
8601 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8602
8603 for (i = 0; i < locsymcount; ++ i)
8604 {
8605 sym = isymbuf + i;
8606
8607 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8608 continue;
8609
8610 candidate = bfd_elf_string_from_elf_section (input_bfd,
8611 symtab_hdr->sh_link,
8612 sym->st_name);
8613 #ifdef DEBUG
8614 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8615 name, candidate, (unsigned long) sym->st_value);
8616 #endif
8617 if (candidate && strcmp (candidate, name) == 0)
8618 {
8619 asection *sec = flinfo->sections [i];
8620
8621 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8622 *result += sec->output_offset + sec->output_section->vma;
8623 #ifdef DEBUG
8624 printf ("Found symbol with value %8.8lx\n",
8625 (unsigned long) *result);
8626 #endif
8627 return TRUE;
8628 }
8629 }
8630
8631 /* Hmm, haven't found it yet. perhaps it is a global. */
8632 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8633 FALSE, FALSE, TRUE);
8634 if (!global_entry)
8635 return FALSE;
8636
8637 if (global_entry->type == bfd_link_hash_defined
8638 || global_entry->type == bfd_link_hash_defweak)
8639 {
8640 *result = (global_entry->u.def.value
8641 + global_entry->u.def.section->output_section->vma
8642 + global_entry->u.def.section->output_offset);
8643 #ifdef DEBUG
8644 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8645 global_entry->root.string, (unsigned long) *result);
8646 #endif
8647 return TRUE;
8648 }
8649
8650 return FALSE;
8651 }
8652
8653 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8654 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8655 names like "foo.end" which is the end address of section "foo". */
8656
8657 static bfd_boolean
8658 resolve_section (const char *name,
8659 asection *sections,
8660 bfd_vma *result,
8661 bfd * abfd)
8662 {
8663 asection *curr;
8664 unsigned int len;
8665
8666 for (curr = sections; curr; curr = curr->next)
8667 if (strcmp (curr->name, name) == 0)
8668 {
8669 *result = curr->vma;
8670 return TRUE;
8671 }
8672
8673 /* Hmm. still haven't found it. try pseudo-section names. */
8674 /* FIXME: This could be coded more efficiently... */
8675 for (curr = sections; curr; curr = curr->next)
8676 {
8677 len = strlen (curr->name);
8678 if (len > strlen (name))
8679 continue;
8680
8681 if (strncmp (curr->name, name, len) == 0)
8682 {
8683 if (strncmp (".end", name + len, 4) == 0)
8684 {
8685 *result = (curr->vma
8686 + curr->size / bfd_octets_per_byte (abfd, curr));
8687 return TRUE;
8688 }
8689
8690 /* Insert more pseudo-section names here, if you like. */
8691 }
8692 }
8693
8694 return FALSE;
8695 }
8696
8697 static void
8698 undefined_reference (const char *reftype, const char *name)
8699 {
8700 /* xgettext:c-format */
8701 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8702 reftype, name);
8703 bfd_set_error (bfd_error_bad_value);
8704 }
8705
8706 static bfd_boolean
8707 eval_symbol (bfd_vma *result,
8708 const char **symp,
8709 bfd *input_bfd,
8710 struct elf_final_link_info *flinfo,
8711 bfd_vma dot,
8712 Elf_Internal_Sym *isymbuf,
8713 size_t locsymcount,
8714 int signed_p)
8715 {
8716 size_t len;
8717 size_t symlen;
8718 bfd_vma a;
8719 bfd_vma b;
8720 char symbuf[4096];
8721 const char *sym = *symp;
8722 const char *symend;
8723 bfd_boolean symbol_is_section = FALSE;
8724
8725 len = strlen (sym);
8726 symend = sym + len;
8727
8728 if (len < 1 || len > sizeof (symbuf))
8729 {
8730 bfd_set_error (bfd_error_invalid_operation);
8731 return FALSE;
8732 }
8733
8734 switch (* sym)
8735 {
8736 case '.':
8737 *result = dot;
8738 *symp = sym + 1;
8739 return TRUE;
8740
8741 case '#':
8742 ++sym;
8743 *result = strtoul (sym, (char **) symp, 16);
8744 return TRUE;
8745
8746 case 'S':
8747 symbol_is_section = TRUE;
8748 /* Fall through. */
8749 case 's':
8750 ++sym;
8751 symlen = strtol (sym, (char **) symp, 10);
8752 sym = *symp + 1; /* Skip the trailing ':'. */
8753
8754 if (symend < sym || symlen + 1 > sizeof (symbuf))
8755 {
8756 bfd_set_error (bfd_error_invalid_operation);
8757 return FALSE;
8758 }
8759
8760 memcpy (symbuf, sym, symlen);
8761 symbuf[symlen] = '\0';
8762 *symp = sym + symlen;
8763
8764 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8765 the symbol as a section, or vice-versa. so we're pretty liberal in our
8766 interpretation here; section means "try section first", not "must be a
8767 section", and likewise with symbol. */
8768
8769 if (symbol_is_section)
8770 {
8771 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8772 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8773 isymbuf, locsymcount))
8774 {
8775 undefined_reference ("section", symbuf);
8776 return FALSE;
8777 }
8778 }
8779 else
8780 {
8781 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8782 isymbuf, locsymcount)
8783 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8784 result, input_bfd))
8785 {
8786 undefined_reference ("symbol", symbuf);
8787 return FALSE;
8788 }
8789 }
8790
8791 return TRUE;
8792
8793 /* All that remains are operators. */
8794
8795 #define UNARY_OP(op) \
8796 if (strncmp (sym, #op, strlen (#op)) == 0) \
8797 { \
8798 sym += strlen (#op); \
8799 if (*sym == ':') \
8800 ++sym; \
8801 *symp = sym; \
8802 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8803 isymbuf, locsymcount, signed_p)) \
8804 return FALSE; \
8805 if (signed_p) \
8806 *result = op ((bfd_signed_vma) a); \
8807 else \
8808 *result = op a; \
8809 return TRUE; \
8810 }
8811
8812 #define BINARY_OP_HEAD(op) \
8813 if (strncmp (sym, #op, strlen (#op)) == 0) \
8814 { \
8815 sym += strlen (#op); \
8816 if (*sym == ':') \
8817 ++sym; \
8818 *symp = sym; \
8819 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8820 isymbuf, locsymcount, signed_p)) \
8821 return FALSE; \
8822 ++*symp; \
8823 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8824 isymbuf, locsymcount, signed_p)) \
8825 return FALSE;
8826 #define BINARY_OP_TAIL(op) \
8827 if (signed_p) \
8828 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8829 else \
8830 *result = a op b; \
8831 return TRUE; \
8832 }
8833 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8834
8835 default:
8836 UNARY_OP (0-);
8837 BINARY_OP_HEAD (<<);
8838 if (b >= sizeof (a) * CHAR_BIT)
8839 {
8840 *result = 0;
8841 return TRUE;
8842 }
8843 signed_p = 0;
8844 BINARY_OP_TAIL (<<);
8845 BINARY_OP_HEAD (>>);
8846 if (b >= sizeof (a) * CHAR_BIT)
8847 {
8848 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8849 return TRUE;
8850 }
8851 BINARY_OP_TAIL (>>);
8852 BINARY_OP (==);
8853 BINARY_OP (!=);
8854 BINARY_OP (<=);
8855 BINARY_OP (>=);
8856 BINARY_OP (&&);
8857 BINARY_OP (||);
8858 UNARY_OP (~);
8859 UNARY_OP (!);
8860 BINARY_OP (*);
8861 BINARY_OP_HEAD (/);
8862 if (b == 0)
8863 {
8864 _bfd_error_handler (_("division by zero"));
8865 bfd_set_error (bfd_error_bad_value);
8866 return FALSE;
8867 }
8868 BINARY_OP_TAIL (/);
8869 BINARY_OP_HEAD (%);
8870 if (b == 0)
8871 {
8872 _bfd_error_handler (_("division by zero"));
8873 bfd_set_error (bfd_error_bad_value);
8874 return FALSE;
8875 }
8876 BINARY_OP_TAIL (%);
8877 BINARY_OP (^);
8878 BINARY_OP (|);
8879 BINARY_OP (&);
8880 BINARY_OP (+);
8881 BINARY_OP (-);
8882 BINARY_OP (<);
8883 BINARY_OP (>);
8884 #undef UNARY_OP
8885 #undef BINARY_OP
8886 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8887 bfd_set_error (bfd_error_invalid_operation);
8888 return FALSE;
8889 }
8890 }
8891
8892 static void
8893 put_value (bfd_vma size,
8894 unsigned long chunksz,
8895 bfd *input_bfd,
8896 bfd_vma x,
8897 bfd_byte *location)
8898 {
8899 location += (size - chunksz);
8900
8901 for (; size; size -= chunksz, location -= chunksz)
8902 {
8903 switch (chunksz)
8904 {
8905 case 1:
8906 bfd_put_8 (input_bfd, x, location);
8907 x >>= 8;
8908 break;
8909 case 2:
8910 bfd_put_16 (input_bfd, x, location);
8911 x >>= 16;
8912 break;
8913 case 4:
8914 bfd_put_32 (input_bfd, x, location);
8915 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8916 x >>= 16;
8917 x >>= 16;
8918 break;
8919 #ifdef BFD64
8920 case 8:
8921 bfd_put_64 (input_bfd, x, location);
8922 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8923 x >>= 32;
8924 x >>= 32;
8925 break;
8926 #endif
8927 default:
8928 abort ();
8929 break;
8930 }
8931 }
8932 }
8933
8934 static bfd_vma
8935 get_value (bfd_vma size,
8936 unsigned long chunksz,
8937 bfd *input_bfd,
8938 bfd_byte *location)
8939 {
8940 int shift;
8941 bfd_vma x = 0;
8942
8943 /* Sanity checks. */
8944 BFD_ASSERT (chunksz <= sizeof (x)
8945 && size >= chunksz
8946 && chunksz != 0
8947 && (size % chunksz) == 0
8948 && input_bfd != NULL
8949 && location != NULL);
8950
8951 if (chunksz == sizeof (x))
8952 {
8953 BFD_ASSERT (size == chunksz);
8954
8955 /* Make sure that we do not perform an undefined shift operation.
8956 We know that size == chunksz so there will only be one iteration
8957 of the loop below. */
8958 shift = 0;
8959 }
8960 else
8961 shift = 8 * chunksz;
8962
8963 for (; size; size -= chunksz, location += chunksz)
8964 {
8965 switch (chunksz)
8966 {
8967 case 1:
8968 x = (x << shift) | bfd_get_8 (input_bfd, location);
8969 break;
8970 case 2:
8971 x = (x << shift) | bfd_get_16 (input_bfd, location);
8972 break;
8973 case 4:
8974 x = (x << shift) | bfd_get_32 (input_bfd, location);
8975 break;
8976 #ifdef BFD64
8977 case 8:
8978 x = (x << shift) | bfd_get_64 (input_bfd, location);
8979 break;
8980 #endif
8981 default:
8982 abort ();
8983 }
8984 }
8985 return x;
8986 }
8987
8988 static void
8989 decode_complex_addend (unsigned long *start, /* in bits */
8990 unsigned long *oplen, /* in bits */
8991 unsigned long *len, /* in bits */
8992 unsigned long *wordsz, /* in bytes */
8993 unsigned long *chunksz, /* in bytes */
8994 unsigned long *lsb0_p,
8995 unsigned long *signed_p,
8996 unsigned long *trunc_p,
8997 unsigned long encoded)
8998 {
8999 * start = encoded & 0x3F;
9000 * len = (encoded >> 6) & 0x3F;
9001 * oplen = (encoded >> 12) & 0x3F;
9002 * wordsz = (encoded >> 18) & 0xF;
9003 * chunksz = (encoded >> 22) & 0xF;
9004 * lsb0_p = (encoded >> 27) & 1;
9005 * signed_p = (encoded >> 28) & 1;
9006 * trunc_p = (encoded >> 29) & 1;
9007 }
9008
9009 bfd_reloc_status_type
9010 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9011 asection *input_section,
9012 bfd_byte *contents,
9013 Elf_Internal_Rela *rel,
9014 bfd_vma relocation)
9015 {
9016 bfd_vma shift, x, mask;
9017 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9018 bfd_reloc_status_type r;
9019 bfd_size_type octets;
9020
9021 /* Perform this reloc, since it is complex.
9022 (this is not to say that it necessarily refers to a complex
9023 symbol; merely that it is a self-describing CGEN based reloc.
9024 i.e. the addend has the complete reloc information (bit start, end,
9025 word size, etc) encoded within it.). */
9026
9027 decode_complex_addend (&start, &oplen, &len, &wordsz,
9028 &chunksz, &lsb0_p, &signed_p,
9029 &trunc_p, rel->r_addend);
9030
9031 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9032
9033 if (lsb0_p)
9034 shift = (start + 1) - len;
9035 else
9036 shift = (8 * wordsz) - (start + len);
9037
9038 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9039 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9040
9041 #ifdef DEBUG
9042 printf ("Doing complex reloc: "
9043 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9044 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9045 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9046 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9047 oplen, (unsigned long) x, (unsigned long) mask,
9048 (unsigned long) relocation);
9049 #endif
9050
9051 r = bfd_reloc_ok;
9052 if (! trunc_p)
9053 /* Now do an overflow check. */
9054 r = bfd_check_overflow ((signed_p
9055 ? complain_overflow_signed
9056 : complain_overflow_unsigned),
9057 len, 0, (8 * wordsz),
9058 relocation);
9059
9060 /* Do the deed. */
9061 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9062
9063 #ifdef DEBUG
9064 printf (" relocation: %8.8lx\n"
9065 " shifted mask: %8.8lx\n"
9066 " shifted/masked reloc: %8.8lx\n"
9067 " result: %8.8lx\n",
9068 (unsigned long) relocation, (unsigned long) (mask << shift),
9069 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9070 #endif
9071 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9072 return r;
9073 }
9074
9075 /* Functions to read r_offset from external (target order) reloc
9076 entry. Faster than bfd_getl32 et al, because we let the compiler
9077 know the value is aligned. */
9078
9079 static bfd_vma
9080 ext32l_r_offset (const void *p)
9081 {
9082 union aligned32
9083 {
9084 uint32_t v;
9085 unsigned char c[4];
9086 };
9087 const union aligned32 *a
9088 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9089
9090 uint32_t aval = ( (uint32_t) a->c[0]
9091 | (uint32_t) a->c[1] << 8
9092 | (uint32_t) a->c[2] << 16
9093 | (uint32_t) a->c[3] << 24);
9094 return aval;
9095 }
9096
9097 static bfd_vma
9098 ext32b_r_offset (const void *p)
9099 {
9100 union aligned32
9101 {
9102 uint32_t v;
9103 unsigned char c[4];
9104 };
9105 const union aligned32 *a
9106 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9107
9108 uint32_t aval = ( (uint32_t) a->c[0] << 24
9109 | (uint32_t) a->c[1] << 16
9110 | (uint32_t) a->c[2] << 8
9111 | (uint32_t) a->c[3]);
9112 return aval;
9113 }
9114
9115 #ifdef BFD_HOST_64_BIT
9116 static bfd_vma
9117 ext64l_r_offset (const void *p)
9118 {
9119 union aligned64
9120 {
9121 uint64_t v;
9122 unsigned char c[8];
9123 };
9124 const union aligned64 *a
9125 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9126
9127 uint64_t aval = ( (uint64_t) a->c[0]
9128 | (uint64_t) a->c[1] << 8
9129 | (uint64_t) a->c[2] << 16
9130 | (uint64_t) a->c[3] << 24
9131 | (uint64_t) a->c[4] << 32
9132 | (uint64_t) a->c[5] << 40
9133 | (uint64_t) a->c[6] << 48
9134 | (uint64_t) a->c[7] << 56);
9135 return aval;
9136 }
9137
9138 static bfd_vma
9139 ext64b_r_offset (const void *p)
9140 {
9141 union aligned64
9142 {
9143 uint64_t v;
9144 unsigned char c[8];
9145 };
9146 const union aligned64 *a
9147 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9148
9149 uint64_t aval = ( (uint64_t) a->c[0] << 56
9150 | (uint64_t) a->c[1] << 48
9151 | (uint64_t) a->c[2] << 40
9152 | (uint64_t) a->c[3] << 32
9153 | (uint64_t) a->c[4] << 24
9154 | (uint64_t) a->c[5] << 16
9155 | (uint64_t) a->c[6] << 8
9156 | (uint64_t) a->c[7]);
9157 return aval;
9158 }
9159 #endif
9160
9161 /* When performing a relocatable link, the input relocations are
9162 preserved. But, if they reference global symbols, the indices
9163 referenced must be updated. Update all the relocations found in
9164 RELDATA. */
9165
9166 static bfd_boolean
9167 elf_link_adjust_relocs (bfd *abfd,
9168 asection *sec,
9169 struct bfd_elf_section_reloc_data *reldata,
9170 bfd_boolean sort,
9171 struct bfd_link_info *info)
9172 {
9173 unsigned int i;
9174 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9175 bfd_byte *erela;
9176 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9177 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9178 bfd_vma r_type_mask;
9179 int r_sym_shift;
9180 unsigned int count = reldata->count;
9181 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9182
9183 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9184 {
9185 swap_in = bed->s->swap_reloc_in;
9186 swap_out = bed->s->swap_reloc_out;
9187 }
9188 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9189 {
9190 swap_in = bed->s->swap_reloca_in;
9191 swap_out = bed->s->swap_reloca_out;
9192 }
9193 else
9194 abort ();
9195
9196 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9197 abort ();
9198
9199 if (bed->s->arch_size == 32)
9200 {
9201 r_type_mask = 0xff;
9202 r_sym_shift = 8;
9203 }
9204 else
9205 {
9206 r_type_mask = 0xffffffff;
9207 r_sym_shift = 32;
9208 }
9209
9210 erela = reldata->hdr->contents;
9211 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9212 {
9213 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9214 unsigned int j;
9215
9216 if (*rel_hash == NULL)
9217 continue;
9218
9219 if ((*rel_hash)->indx == -2
9220 && info->gc_sections
9221 && ! info->gc_keep_exported)
9222 {
9223 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9224 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9225 abfd, sec,
9226 (*rel_hash)->root.root.string);
9227 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9228 abfd, sec);
9229 bfd_set_error (bfd_error_invalid_operation);
9230 return FALSE;
9231 }
9232 BFD_ASSERT ((*rel_hash)->indx >= 0);
9233
9234 (*swap_in) (abfd, erela, irela);
9235 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9236 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9237 | (irela[j].r_info & r_type_mask));
9238 (*swap_out) (abfd, irela, erela);
9239 }
9240
9241 if (bed->elf_backend_update_relocs)
9242 (*bed->elf_backend_update_relocs) (sec, reldata);
9243
9244 if (sort && count != 0)
9245 {
9246 bfd_vma (*ext_r_off) (const void *);
9247 bfd_vma r_off;
9248 size_t elt_size;
9249 bfd_byte *base, *end, *p, *loc;
9250 bfd_byte *buf = NULL;
9251
9252 if (bed->s->arch_size == 32)
9253 {
9254 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9255 ext_r_off = ext32l_r_offset;
9256 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9257 ext_r_off = ext32b_r_offset;
9258 else
9259 abort ();
9260 }
9261 else
9262 {
9263 #ifdef BFD_HOST_64_BIT
9264 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9265 ext_r_off = ext64l_r_offset;
9266 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9267 ext_r_off = ext64b_r_offset;
9268 else
9269 #endif
9270 abort ();
9271 }
9272
9273 /* Must use a stable sort here. A modified insertion sort,
9274 since the relocs are mostly sorted already. */
9275 elt_size = reldata->hdr->sh_entsize;
9276 base = reldata->hdr->contents;
9277 end = base + count * elt_size;
9278 if (elt_size > sizeof (Elf64_External_Rela))
9279 abort ();
9280
9281 /* Ensure the first element is lowest. This acts as a sentinel,
9282 speeding the main loop below. */
9283 r_off = (*ext_r_off) (base);
9284 for (p = loc = base; (p += elt_size) < end; )
9285 {
9286 bfd_vma r_off2 = (*ext_r_off) (p);
9287 if (r_off > r_off2)
9288 {
9289 r_off = r_off2;
9290 loc = p;
9291 }
9292 }
9293 if (loc != base)
9294 {
9295 /* Don't just swap *base and *loc as that changes the order
9296 of the original base[0] and base[1] if they happen to
9297 have the same r_offset. */
9298 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9299 memcpy (onebuf, loc, elt_size);
9300 memmove (base + elt_size, base, loc - base);
9301 memcpy (base, onebuf, elt_size);
9302 }
9303
9304 for (p = base + elt_size; (p += elt_size) < end; )
9305 {
9306 /* base to p is sorted, *p is next to insert. */
9307 r_off = (*ext_r_off) (p);
9308 /* Search the sorted region for location to insert. */
9309 loc = p - elt_size;
9310 while (r_off < (*ext_r_off) (loc))
9311 loc -= elt_size;
9312 loc += elt_size;
9313 if (loc != p)
9314 {
9315 /* Chances are there is a run of relocs to insert here,
9316 from one of more input files. Files are not always
9317 linked in order due to the way elf_link_input_bfd is
9318 called. See pr17666. */
9319 size_t sortlen = p - loc;
9320 bfd_vma r_off2 = (*ext_r_off) (loc);
9321 size_t runlen = elt_size;
9322 size_t buf_size = 96 * 1024;
9323 while (p + runlen < end
9324 && (sortlen <= buf_size
9325 || runlen + elt_size <= buf_size)
9326 && r_off2 > (*ext_r_off) (p + runlen))
9327 runlen += elt_size;
9328 if (buf == NULL)
9329 {
9330 buf = bfd_malloc (buf_size);
9331 if (buf == NULL)
9332 return FALSE;
9333 }
9334 if (runlen < sortlen)
9335 {
9336 memcpy (buf, p, runlen);
9337 memmove (loc + runlen, loc, sortlen);
9338 memcpy (loc, buf, runlen);
9339 }
9340 else
9341 {
9342 memcpy (buf, loc, sortlen);
9343 memmove (loc, p, runlen);
9344 memcpy (loc + runlen, buf, sortlen);
9345 }
9346 p += runlen - elt_size;
9347 }
9348 }
9349 /* Hashes are no longer valid. */
9350 free (reldata->hashes);
9351 reldata->hashes = NULL;
9352 free (buf);
9353 }
9354 return TRUE;
9355 }
9356
9357 struct elf_link_sort_rela
9358 {
9359 union {
9360 bfd_vma offset;
9361 bfd_vma sym_mask;
9362 } u;
9363 enum elf_reloc_type_class type;
9364 /* We use this as an array of size int_rels_per_ext_rel. */
9365 Elf_Internal_Rela rela[1];
9366 };
9367
9368 /* qsort stability here and for cmp2 is only an issue if multiple
9369 dynamic relocations are emitted at the same address. But targets
9370 that apply a series of dynamic relocations each operating on the
9371 result of the prior relocation can't use -z combreloc as
9372 implemented anyway. Such schemes tend to be broken by sorting on
9373 symbol index. That leaves dynamic NONE relocs as the only other
9374 case where ld might emit multiple relocs at the same address, and
9375 those are only emitted due to target bugs. */
9376
9377 static int
9378 elf_link_sort_cmp1 (const void *A, const void *B)
9379 {
9380 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9381 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9382 int relativea, relativeb;
9383
9384 relativea = a->type == reloc_class_relative;
9385 relativeb = b->type == reloc_class_relative;
9386
9387 if (relativea < relativeb)
9388 return 1;
9389 if (relativea > relativeb)
9390 return -1;
9391 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9392 return -1;
9393 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9394 return 1;
9395 if (a->rela->r_offset < b->rela->r_offset)
9396 return -1;
9397 if (a->rela->r_offset > b->rela->r_offset)
9398 return 1;
9399 return 0;
9400 }
9401
9402 static int
9403 elf_link_sort_cmp2 (const void *A, const void *B)
9404 {
9405 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9406 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9407
9408 if (a->type < b->type)
9409 return -1;
9410 if (a->type > b->type)
9411 return 1;
9412 if (a->u.offset < b->u.offset)
9413 return -1;
9414 if (a->u.offset > b->u.offset)
9415 return 1;
9416 if (a->rela->r_offset < b->rela->r_offset)
9417 return -1;
9418 if (a->rela->r_offset > b->rela->r_offset)
9419 return 1;
9420 return 0;
9421 }
9422
9423 static size_t
9424 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9425 {
9426 asection *dynamic_relocs;
9427 asection *rela_dyn;
9428 asection *rel_dyn;
9429 bfd_size_type count, size;
9430 size_t i, ret, sort_elt, ext_size;
9431 bfd_byte *sort, *s_non_relative, *p;
9432 struct elf_link_sort_rela *sq;
9433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9434 int i2e = bed->s->int_rels_per_ext_rel;
9435 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9436 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9437 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9438 struct bfd_link_order *lo;
9439 bfd_vma r_sym_mask;
9440 bfd_boolean use_rela;
9441
9442 /* Find a dynamic reloc section. */
9443 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9444 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9445 if (rela_dyn != NULL && rela_dyn->size > 0
9446 && rel_dyn != NULL && rel_dyn->size > 0)
9447 {
9448 bfd_boolean use_rela_initialised = FALSE;
9449
9450 /* This is just here to stop gcc from complaining.
9451 Its initialization checking code is not perfect. */
9452 use_rela = TRUE;
9453
9454 /* Both sections are present. Examine the sizes
9455 of the indirect sections to help us choose. */
9456 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9457 if (lo->type == bfd_indirect_link_order)
9458 {
9459 asection *o = lo->u.indirect.section;
9460
9461 if ((o->size % bed->s->sizeof_rela) == 0)
9462 {
9463 if ((o->size % bed->s->sizeof_rel) == 0)
9464 /* Section size is divisible by both rel and rela sizes.
9465 It is of no help to us. */
9466 ;
9467 else
9468 {
9469 /* Section size is only divisible by rela. */
9470 if (use_rela_initialised && !use_rela)
9471 {
9472 _bfd_error_handler (_("%pB: unable to sort relocs - "
9473 "they are in more than one size"),
9474 abfd);
9475 bfd_set_error (bfd_error_invalid_operation);
9476 return 0;
9477 }
9478 else
9479 {
9480 use_rela = TRUE;
9481 use_rela_initialised = TRUE;
9482 }
9483 }
9484 }
9485 else if ((o->size % bed->s->sizeof_rel) == 0)
9486 {
9487 /* Section size is only divisible by rel. */
9488 if (use_rela_initialised && use_rela)
9489 {
9490 _bfd_error_handler (_("%pB: unable to sort relocs - "
9491 "they are in more than one size"),
9492 abfd);
9493 bfd_set_error (bfd_error_invalid_operation);
9494 return 0;
9495 }
9496 else
9497 {
9498 use_rela = FALSE;
9499 use_rela_initialised = TRUE;
9500 }
9501 }
9502 else
9503 {
9504 /* The section size is not divisible by either -
9505 something is wrong. */
9506 _bfd_error_handler (_("%pB: unable to sort relocs - "
9507 "they are of an unknown size"), abfd);
9508 bfd_set_error (bfd_error_invalid_operation);
9509 return 0;
9510 }
9511 }
9512
9513 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9514 if (lo->type == bfd_indirect_link_order)
9515 {
9516 asection *o = lo->u.indirect.section;
9517
9518 if ((o->size % bed->s->sizeof_rela) == 0)
9519 {
9520 if ((o->size % bed->s->sizeof_rel) == 0)
9521 /* Section size is divisible by both rel and rela sizes.
9522 It is of no help to us. */
9523 ;
9524 else
9525 {
9526 /* Section size is only divisible by rela. */
9527 if (use_rela_initialised && !use_rela)
9528 {
9529 _bfd_error_handler (_("%pB: unable to sort relocs - "
9530 "they are in more than one size"),
9531 abfd);
9532 bfd_set_error (bfd_error_invalid_operation);
9533 return 0;
9534 }
9535 else
9536 {
9537 use_rela = TRUE;
9538 use_rela_initialised = TRUE;
9539 }
9540 }
9541 }
9542 else if ((o->size % bed->s->sizeof_rel) == 0)
9543 {
9544 /* Section size is only divisible by rel. */
9545 if (use_rela_initialised && use_rela)
9546 {
9547 _bfd_error_handler (_("%pB: unable to sort relocs - "
9548 "they are in more than one size"),
9549 abfd);
9550 bfd_set_error (bfd_error_invalid_operation);
9551 return 0;
9552 }
9553 else
9554 {
9555 use_rela = FALSE;
9556 use_rela_initialised = TRUE;
9557 }
9558 }
9559 else
9560 {
9561 /* The section size is not divisible by either -
9562 something is wrong. */
9563 _bfd_error_handler (_("%pB: unable to sort relocs - "
9564 "they are of an unknown size"), abfd);
9565 bfd_set_error (bfd_error_invalid_operation);
9566 return 0;
9567 }
9568 }
9569
9570 if (! use_rela_initialised)
9571 /* Make a guess. */
9572 use_rela = TRUE;
9573 }
9574 else if (rela_dyn != NULL && rela_dyn->size > 0)
9575 use_rela = TRUE;
9576 else if (rel_dyn != NULL && rel_dyn->size > 0)
9577 use_rela = FALSE;
9578 else
9579 return 0;
9580
9581 if (use_rela)
9582 {
9583 dynamic_relocs = rela_dyn;
9584 ext_size = bed->s->sizeof_rela;
9585 swap_in = bed->s->swap_reloca_in;
9586 swap_out = bed->s->swap_reloca_out;
9587 }
9588 else
9589 {
9590 dynamic_relocs = rel_dyn;
9591 ext_size = bed->s->sizeof_rel;
9592 swap_in = bed->s->swap_reloc_in;
9593 swap_out = bed->s->swap_reloc_out;
9594 }
9595
9596 size = 0;
9597 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9598 if (lo->type == bfd_indirect_link_order)
9599 size += lo->u.indirect.section->size;
9600
9601 if (size != dynamic_relocs->size)
9602 return 0;
9603
9604 sort_elt = (sizeof (struct elf_link_sort_rela)
9605 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9606
9607 count = dynamic_relocs->size / ext_size;
9608 if (count == 0)
9609 return 0;
9610 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9611
9612 if (sort == NULL)
9613 {
9614 (*info->callbacks->warning)
9615 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9616 return 0;
9617 }
9618
9619 if (bed->s->arch_size == 32)
9620 r_sym_mask = ~(bfd_vma) 0xff;
9621 else
9622 r_sym_mask = ~(bfd_vma) 0xffffffff;
9623
9624 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9625 if (lo->type == bfd_indirect_link_order)
9626 {
9627 bfd_byte *erel, *erelend;
9628 asection *o = lo->u.indirect.section;
9629
9630 if (o->contents == NULL && o->size != 0)
9631 {
9632 /* This is a reloc section that is being handled as a normal
9633 section. See bfd_section_from_shdr. We can't combine
9634 relocs in this case. */
9635 free (sort);
9636 return 0;
9637 }
9638 erel = o->contents;
9639 erelend = o->contents + o->size;
9640 p = sort + o->output_offset * opb / ext_size * sort_elt;
9641
9642 while (erel < erelend)
9643 {
9644 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9645
9646 (*swap_in) (abfd, erel, s->rela);
9647 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9648 s->u.sym_mask = r_sym_mask;
9649 p += sort_elt;
9650 erel += ext_size;
9651 }
9652 }
9653
9654 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9655
9656 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9657 {
9658 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9659 if (s->type != reloc_class_relative)
9660 break;
9661 }
9662 ret = i;
9663 s_non_relative = p;
9664
9665 sq = (struct elf_link_sort_rela *) s_non_relative;
9666 for (; i < count; i++, p += sort_elt)
9667 {
9668 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9669 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9670 sq = sp;
9671 sp->u.offset = sq->rela->r_offset;
9672 }
9673
9674 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9675
9676 struct elf_link_hash_table *htab = elf_hash_table (info);
9677 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9678 {
9679 /* We have plt relocs in .rela.dyn. */
9680 sq = (struct elf_link_sort_rela *) sort;
9681 for (i = 0; i < count; i++)
9682 if (sq[count - i - 1].type != reloc_class_plt)
9683 break;
9684 if (i != 0 && htab->srelplt->size == i * ext_size)
9685 {
9686 struct bfd_link_order **plo;
9687 /* Put srelplt link_order last. This is so the output_offset
9688 set in the next loop is correct for DT_JMPREL. */
9689 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9690 if ((*plo)->type == bfd_indirect_link_order
9691 && (*plo)->u.indirect.section == htab->srelplt)
9692 {
9693 lo = *plo;
9694 *plo = lo->next;
9695 }
9696 else
9697 plo = &(*plo)->next;
9698 *plo = lo;
9699 lo->next = NULL;
9700 dynamic_relocs->map_tail.link_order = lo;
9701 }
9702 }
9703
9704 p = sort;
9705 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9706 if (lo->type == bfd_indirect_link_order)
9707 {
9708 bfd_byte *erel, *erelend;
9709 asection *o = lo->u.indirect.section;
9710
9711 erel = o->contents;
9712 erelend = o->contents + o->size;
9713 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9714 while (erel < erelend)
9715 {
9716 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9717 (*swap_out) (abfd, s->rela, erel);
9718 p += sort_elt;
9719 erel += ext_size;
9720 }
9721 }
9722
9723 free (sort);
9724 *psec = dynamic_relocs;
9725 return ret;
9726 }
9727
9728 /* Add a symbol to the output symbol string table. */
9729
9730 static int
9731 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9732 const char *name,
9733 Elf_Internal_Sym *elfsym,
9734 asection *input_sec,
9735 struct elf_link_hash_entry *h)
9736 {
9737 int (*output_symbol_hook)
9738 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9739 struct elf_link_hash_entry *);
9740 struct elf_link_hash_table *hash_table;
9741 const struct elf_backend_data *bed;
9742 bfd_size_type strtabsize;
9743
9744 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9745
9746 bed = get_elf_backend_data (flinfo->output_bfd);
9747 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9748 if (output_symbol_hook != NULL)
9749 {
9750 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9751 if (ret != 1)
9752 return ret;
9753 }
9754
9755 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9756 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9757 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9758 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9759
9760 if (name == NULL
9761 || *name == '\0'
9762 || (input_sec->flags & SEC_EXCLUDE))
9763 elfsym->st_name = (unsigned long) -1;
9764 else
9765 {
9766 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9767 to get the final offset for st_name. */
9768 char *versioned_name = (char *) name;
9769 if (h != NULL)
9770 {
9771 if (h->versioned == versioned && h->def_dynamic)
9772 {
9773 /* Keep only one '@' for versioned symbols defined in
9774 shared objects. */
9775 char *version = strrchr (name, ELF_VER_CHR);
9776 char *base_end = strchr (name, ELF_VER_CHR);
9777 if (version != base_end)
9778 {
9779 size_t base_len;
9780 size_t len = strlen (name);
9781 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9782 if (versioned_name == NULL)
9783 return 0;
9784 base_len = base_end - name;
9785 memcpy (versioned_name, name, base_len);
9786 memcpy (versioned_name + base_len, version,
9787 len - base_len);
9788 }
9789 }
9790 }
9791 else if (flinfo->info->unique_symbol
9792 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9793 {
9794 struct local_hash_entry *lh;
9795 switch (ELF_ST_TYPE (elfsym->st_info))
9796 {
9797 case STT_FILE:
9798 case STT_SECTION:
9799 break;
9800 default:
9801 lh = (struct local_hash_entry *) bfd_hash_lookup
9802 (&flinfo->local_hash_table, name, TRUE, FALSE);
9803 if (lh == NULL)
9804 return 0;
9805 if (lh->count)
9806 {
9807 /* Append ".COUNT" to duplicated local symbols. */
9808 size_t count_len;
9809 size_t base_len = lh->size;
9810 char buf[30];
9811 sprintf (buf, "%lx", lh->count);
9812 if (!base_len)
9813 {
9814 base_len = strlen (name);
9815 lh->size = base_len;
9816 }
9817 count_len = strlen (buf);
9818 versioned_name = bfd_alloc (flinfo->output_bfd,
9819 base_len + count_len + 2);
9820 if (versioned_name == NULL)
9821 return 0;
9822 memcpy (versioned_name, name, base_len);
9823 versioned_name[base_len] = '.';
9824 memcpy (versioned_name + base_len + 1, buf,
9825 count_len + 1);
9826 }
9827 lh->count++;
9828 break;
9829 }
9830 }
9831 elfsym->st_name
9832 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9833 versioned_name, FALSE);
9834 if (elfsym->st_name == (unsigned long) -1)
9835 return 0;
9836 }
9837
9838 hash_table = elf_hash_table (flinfo->info);
9839 strtabsize = hash_table->strtabsize;
9840 if (strtabsize <= hash_table->strtabcount)
9841 {
9842 strtabsize += strtabsize;
9843 hash_table->strtabsize = strtabsize;
9844 strtabsize *= sizeof (*hash_table->strtab);
9845 hash_table->strtab
9846 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9847 strtabsize);
9848 if (hash_table->strtab == NULL)
9849 return 0;
9850 }
9851 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9852 hash_table->strtab[hash_table->strtabcount].dest_index
9853 = hash_table->strtabcount;
9854 hash_table->strtab[hash_table->strtabcount].destshndx_index
9855 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9856
9857 flinfo->output_bfd->symcount += 1;
9858 hash_table->strtabcount += 1;
9859
9860 return 1;
9861 }
9862
9863 /* Swap symbols out to the symbol table and flush the output symbols to
9864 the file. */
9865
9866 static bfd_boolean
9867 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9868 {
9869 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9870 size_t amt;
9871 size_t i;
9872 const struct elf_backend_data *bed;
9873 bfd_byte *symbuf;
9874 Elf_Internal_Shdr *hdr;
9875 file_ptr pos;
9876 bfd_boolean ret;
9877
9878 if (!hash_table->strtabcount)
9879 return TRUE;
9880
9881 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9882
9883 bed = get_elf_backend_data (flinfo->output_bfd);
9884
9885 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9886 symbuf = (bfd_byte *) bfd_malloc (amt);
9887 if (symbuf == NULL)
9888 return FALSE;
9889
9890 if (flinfo->symshndxbuf)
9891 {
9892 amt = sizeof (Elf_External_Sym_Shndx);
9893 amt *= bfd_get_symcount (flinfo->output_bfd);
9894 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9895 if (flinfo->symshndxbuf == NULL)
9896 {
9897 free (symbuf);
9898 return FALSE;
9899 }
9900 }
9901
9902 /* Now swap out the symbols. */
9903 for (i = 0; i < hash_table->strtabcount; i++)
9904 {
9905 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9906 if (elfsym->sym.st_name == (unsigned long) -1)
9907 elfsym->sym.st_name = 0;
9908 else
9909 elfsym->sym.st_name
9910 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9911 elfsym->sym.st_name);
9912
9913 /* Inform the linker of the addition of this symbol. */
9914
9915 if (flinfo->info->callbacks->ctf_new_symbol)
9916 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9917 &elfsym->sym);
9918
9919 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9920 ((bfd_byte *) symbuf
9921 + (elfsym->dest_index
9922 * bed->s->sizeof_sym)),
9923 (flinfo->symshndxbuf
9924 + elfsym->destshndx_index));
9925 }
9926
9927 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9928 pos = hdr->sh_offset + hdr->sh_size;
9929 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9930 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9931 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9932 {
9933 hdr->sh_size += amt;
9934 ret = TRUE;
9935 }
9936 else
9937 ret = FALSE;
9938
9939 free (symbuf);
9940
9941 free (hash_table->strtab);
9942 hash_table->strtab = NULL;
9943
9944 return ret;
9945 }
9946
9947 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9948
9949 static bfd_boolean
9950 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9951 {
9952 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9953 && sym->st_shndx < SHN_LORESERVE)
9954 {
9955 /* The gABI doesn't support dynamic symbols in output sections
9956 beyond 64k. */
9957 _bfd_error_handler
9958 /* xgettext:c-format */
9959 (_("%pB: too many sections: %d (>= %d)"),
9960 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9961 bfd_set_error (bfd_error_nonrepresentable_section);
9962 return FALSE;
9963 }
9964 return TRUE;
9965 }
9966
9967 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9968 allowing an unsatisfied unversioned symbol in the DSO to match a
9969 versioned symbol that would normally require an explicit version.
9970 We also handle the case that a DSO references a hidden symbol
9971 which may be satisfied by a versioned symbol in another DSO. */
9972
9973 static bfd_boolean
9974 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9975 const struct elf_backend_data *bed,
9976 struct elf_link_hash_entry *h)
9977 {
9978 bfd *abfd;
9979 struct elf_link_loaded_list *loaded;
9980
9981 if (!is_elf_hash_table (info->hash))
9982 return FALSE;
9983
9984 /* Check indirect symbol. */
9985 while (h->root.type == bfd_link_hash_indirect)
9986 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9987
9988 switch (h->root.type)
9989 {
9990 default:
9991 abfd = NULL;
9992 break;
9993
9994 case bfd_link_hash_undefined:
9995 case bfd_link_hash_undefweak:
9996 abfd = h->root.u.undef.abfd;
9997 if (abfd == NULL
9998 || (abfd->flags & DYNAMIC) == 0
9999 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10000 return FALSE;
10001 break;
10002
10003 case bfd_link_hash_defined:
10004 case bfd_link_hash_defweak:
10005 abfd = h->root.u.def.section->owner;
10006 break;
10007
10008 case bfd_link_hash_common:
10009 abfd = h->root.u.c.p->section->owner;
10010 break;
10011 }
10012 BFD_ASSERT (abfd != NULL);
10013
10014 for (loaded = elf_hash_table (info)->dyn_loaded;
10015 loaded != NULL;
10016 loaded = loaded->next)
10017 {
10018 bfd *input;
10019 Elf_Internal_Shdr *hdr;
10020 size_t symcount;
10021 size_t extsymcount;
10022 size_t extsymoff;
10023 Elf_Internal_Shdr *versymhdr;
10024 Elf_Internal_Sym *isym;
10025 Elf_Internal_Sym *isymend;
10026 Elf_Internal_Sym *isymbuf;
10027 Elf_External_Versym *ever;
10028 Elf_External_Versym *extversym;
10029
10030 input = loaded->abfd;
10031
10032 /* We check each DSO for a possible hidden versioned definition. */
10033 if (input == abfd
10034 || elf_dynversym (input) == 0)
10035 continue;
10036
10037 hdr = &elf_tdata (input)->dynsymtab_hdr;
10038
10039 symcount = hdr->sh_size / bed->s->sizeof_sym;
10040 if (elf_bad_symtab (input))
10041 {
10042 extsymcount = symcount;
10043 extsymoff = 0;
10044 }
10045 else
10046 {
10047 extsymcount = symcount - hdr->sh_info;
10048 extsymoff = hdr->sh_info;
10049 }
10050
10051 if (extsymcount == 0)
10052 continue;
10053
10054 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10055 NULL, NULL, NULL);
10056 if (isymbuf == NULL)
10057 return FALSE;
10058
10059 /* Read in any version definitions. */
10060 versymhdr = &elf_tdata (input)->dynversym_hdr;
10061 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10062 || (extversym = (Elf_External_Versym *)
10063 _bfd_malloc_and_read (input, versymhdr->sh_size,
10064 versymhdr->sh_size)) == NULL)
10065 {
10066 free (isymbuf);
10067 return FALSE;
10068 }
10069
10070 ever = extversym + extsymoff;
10071 isymend = isymbuf + extsymcount;
10072 for (isym = isymbuf; isym < isymend; isym++, ever++)
10073 {
10074 const char *name;
10075 Elf_Internal_Versym iver;
10076 unsigned short version_index;
10077
10078 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10079 || isym->st_shndx == SHN_UNDEF)
10080 continue;
10081
10082 name = bfd_elf_string_from_elf_section (input,
10083 hdr->sh_link,
10084 isym->st_name);
10085 if (strcmp (name, h->root.root.string) != 0)
10086 continue;
10087
10088 _bfd_elf_swap_versym_in (input, ever, &iver);
10089
10090 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10091 && !(h->def_regular
10092 && h->forced_local))
10093 {
10094 /* If we have a non-hidden versioned sym, then it should
10095 have provided a definition for the undefined sym unless
10096 it is defined in a non-shared object and forced local.
10097 */
10098 abort ();
10099 }
10100
10101 version_index = iver.vs_vers & VERSYM_VERSION;
10102 if (version_index == 1 || version_index == 2)
10103 {
10104 /* This is the base or first version. We can use it. */
10105 free (extversym);
10106 free (isymbuf);
10107 return TRUE;
10108 }
10109 }
10110
10111 free (extversym);
10112 free (isymbuf);
10113 }
10114
10115 return FALSE;
10116 }
10117
10118 /* Convert ELF common symbol TYPE. */
10119
10120 static int
10121 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10122 {
10123 /* Commom symbol can only appear in relocatable link. */
10124 if (!bfd_link_relocatable (info))
10125 abort ();
10126 switch (info->elf_stt_common)
10127 {
10128 case unchanged:
10129 break;
10130 case elf_stt_common:
10131 type = STT_COMMON;
10132 break;
10133 case no_elf_stt_common:
10134 type = STT_OBJECT;
10135 break;
10136 }
10137 return type;
10138 }
10139
10140 /* Add an external symbol to the symbol table. This is called from
10141 the hash table traversal routine. When generating a shared object,
10142 we go through the symbol table twice. The first time we output
10143 anything that might have been forced to local scope in a version
10144 script. The second time we output the symbols that are still
10145 global symbols. */
10146
10147 static bfd_boolean
10148 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10149 {
10150 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10151 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10152 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10153 bfd_boolean strip;
10154 Elf_Internal_Sym sym;
10155 asection *input_sec;
10156 const struct elf_backend_data *bed;
10157 long indx;
10158 int ret;
10159 unsigned int type;
10160
10161 if (h->root.type == bfd_link_hash_warning)
10162 {
10163 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10164 if (h->root.type == bfd_link_hash_new)
10165 return TRUE;
10166 }
10167
10168 /* Decide whether to output this symbol in this pass. */
10169 if (eoinfo->localsyms)
10170 {
10171 if (!h->forced_local)
10172 return TRUE;
10173 }
10174 else
10175 {
10176 if (h->forced_local)
10177 return TRUE;
10178 }
10179
10180 bed = get_elf_backend_data (flinfo->output_bfd);
10181
10182 if (h->root.type == bfd_link_hash_undefined)
10183 {
10184 /* If we have an undefined symbol reference here then it must have
10185 come from a shared library that is being linked in. (Undefined
10186 references in regular files have already been handled unless
10187 they are in unreferenced sections which are removed by garbage
10188 collection). */
10189 bfd_boolean ignore_undef = FALSE;
10190
10191 /* Some symbols may be special in that the fact that they're
10192 undefined can be safely ignored - let backend determine that. */
10193 if (bed->elf_backend_ignore_undef_symbol)
10194 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10195
10196 /* If we are reporting errors for this situation then do so now. */
10197 if (!ignore_undef
10198 && h->ref_dynamic_nonweak
10199 && (!h->ref_regular || flinfo->info->gc_sections)
10200 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10201 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10202 {
10203 flinfo->info->callbacks->undefined_symbol
10204 (flinfo->info, h->root.root.string,
10205 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10206 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10207 && !flinfo->info->warn_unresolved_syms);
10208 }
10209
10210 /* Strip a global symbol defined in a discarded section. */
10211 if (h->indx == -3)
10212 return TRUE;
10213 }
10214
10215 /* We should also warn if a forced local symbol is referenced from
10216 shared libraries. */
10217 if (bfd_link_executable (flinfo->info)
10218 && h->forced_local
10219 && h->ref_dynamic
10220 && h->def_regular
10221 && !h->dynamic_def
10222 && h->ref_dynamic_nonweak
10223 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10224 {
10225 bfd *def_bfd;
10226 const char *msg;
10227 struct elf_link_hash_entry *hi = h;
10228
10229 /* Check indirect symbol. */
10230 while (hi->root.type == bfd_link_hash_indirect)
10231 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10232
10233 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10234 /* xgettext:c-format */
10235 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10236 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10237 /* xgettext:c-format */
10238 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10239 else
10240 /* xgettext:c-format */
10241 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10242 def_bfd = flinfo->output_bfd;
10243 if (hi->root.u.def.section != bfd_abs_section_ptr)
10244 def_bfd = hi->root.u.def.section->owner;
10245 _bfd_error_handler (msg, flinfo->output_bfd,
10246 h->root.root.string, def_bfd);
10247 bfd_set_error (bfd_error_bad_value);
10248 eoinfo->failed = TRUE;
10249 return FALSE;
10250 }
10251
10252 /* We don't want to output symbols that have never been mentioned by
10253 a regular file, or that we have been told to strip. However, if
10254 h->indx is set to -2, the symbol is used by a reloc and we must
10255 output it. */
10256 strip = FALSE;
10257 if (h->indx == -2)
10258 ;
10259 else if ((h->def_dynamic
10260 || h->ref_dynamic
10261 || h->root.type == bfd_link_hash_new)
10262 && !h->def_regular
10263 && !h->ref_regular)
10264 strip = TRUE;
10265 else if (flinfo->info->strip == strip_all)
10266 strip = TRUE;
10267 else if (flinfo->info->strip == strip_some
10268 && bfd_hash_lookup (flinfo->info->keep_hash,
10269 h->root.root.string, FALSE, FALSE) == NULL)
10270 strip = TRUE;
10271 else if ((h->root.type == bfd_link_hash_defined
10272 || h->root.type == bfd_link_hash_defweak)
10273 && ((flinfo->info->strip_discarded
10274 && discarded_section (h->root.u.def.section))
10275 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10276 && h->root.u.def.section->owner != NULL
10277 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10278 strip = TRUE;
10279 else if ((h->root.type == bfd_link_hash_undefined
10280 || h->root.type == bfd_link_hash_undefweak)
10281 && h->root.u.undef.abfd != NULL
10282 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10283 strip = TRUE;
10284
10285 type = h->type;
10286
10287 /* If we're stripping it, and it's not a dynamic symbol, there's
10288 nothing else to do. However, if it is a forced local symbol or
10289 an ifunc symbol we need to give the backend finish_dynamic_symbol
10290 function a chance to make it dynamic. */
10291 if (strip
10292 && h->dynindx == -1
10293 && type != STT_GNU_IFUNC
10294 && !h->forced_local)
10295 return TRUE;
10296
10297 sym.st_value = 0;
10298 sym.st_size = h->size;
10299 sym.st_other = h->other;
10300 switch (h->root.type)
10301 {
10302 default:
10303 case bfd_link_hash_new:
10304 case bfd_link_hash_warning:
10305 abort ();
10306 return FALSE;
10307
10308 case bfd_link_hash_undefined:
10309 case bfd_link_hash_undefweak:
10310 input_sec = bfd_und_section_ptr;
10311 sym.st_shndx = SHN_UNDEF;
10312 break;
10313
10314 case bfd_link_hash_defined:
10315 case bfd_link_hash_defweak:
10316 {
10317 input_sec = h->root.u.def.section;
10318 if (input_sec->output_section != NULL)
10319 {
10320 sym.st_shndx =
10321 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10322 input_sec->output_section);
10323 if (sym.st_shndx == SHN_BAD)
10324 {
10325 _bfd_error_handler
10326 /* xgettext:c-format */
10327 (_("%pB: could not find output section %pA for input section %pA"),
10328 flinfo->output_bfd, input_sec->output_section, input_sec);
10329 bfd_set_error (bfd_error_nonrepresentable_section);
10330 eoinfo->failed = TRUE;
10331 return FALSE;
10332 }
10333
10334 /* ELF symbols in relocatable files are section relative,
10335 but in nonrelocatable files they are virtual
10336 addresses. */
10337 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10338 if (!bfd_link_relocatable (flinfo->info))
10339 {
10340 sym.st_value += input_sec->output_section->vma;
10341 if (h->type == STT_TLS)
10342 {
10343 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10344 if (tls_sec != NULL)
10345 sym.st_value -= tls_sec->vma;
10346 }
10347 }
10348 }
10349 else
10350 {
10351 BFD_ASSERT (input_sec->owner == NULL
10352 || (input_sec->owner->flags & DYNAMIC) != 0);
10353 sym.st_shndx = SHN_UNDEF;
10354 input_sec = bfd_und_section_ptr;
10355 }
10356 }
10357 break;
10358
10359 case bfd_link_hash_common:
10360 input_sec = h->root.u.c.p->section;
10361 sym.st_shndx = bed->common_section_index (input_sec);
10362 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10363 break;
10364
10365 case bfd_link_hash_indirect:
10366 /* These symbols are created by symbol versioning. They point
10367 to the decorated version of the name. For example, if the
10368 symbol foo@@GNU_1.2 is the default, which should be used when
10369 foo is used with no version, then we add an indirect symbol
10370 foo which points to foo@@GNU_1.2. We ignore these symbols,
10371 since the indirected symbol is already in the hash table. */
10372 return TRUE;
10373 }
10374
10375 if (type == STT_COMMON || type == STT_OBJECT)
10376 switch (h->root.type)
10377 {
10378 case bfd_link_hash_common:
10379 type = elf_link_convert_common_type (flinfo->info, type);
10380 break;
10381 case bfd_link_hash_defined:
10382 case bfd_link_hash_defweak:
10383 if (bed->common_definition (&sym))
10384 type = elf_link_convert_common_type (flinfo->info, type);
10385 else
10386 type = STT_OBJECT;
10387 break;
10388 case bfd_link_hash_undefined:
10389 case bfd_link_hash_undefweak:
10390 break;
10391 default:
10392 abort ();
10393 }
10394
10395 if (h->forced_local)
10396 {
10397 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10398 /* Turn off visibility on local symbol. */
10399 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10400 }
10401 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10402 else if (h->unique_global && h->def_regular)
10403 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10404 else if (h->root.type == bfd_link_hash_undefweak
10405 || h->root.type == bfd_link_hash_defweak)
10406 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10407 else
10408 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10409 sym.st_target_internal = h->target_internal;
10410
10411 /* Give the processor backend a chance to tweak the symbol value,
10412 and also to finish up anything that needs to be done for this
10413 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10414 forced local syms when non-shared is due to a historical quirk.
10415 STT_GNU_IFUNC symbol must go through PLT. */
10416 if ((h->type == STT_GNU_IFUNC
10417 && h->def_regular
10418 && !bfd_link_relocatable (flinfo->info))
10419 || ((h->dynindx != -1
10420 || h->forced_local)
10421 && ((bfd_link_pic (flinfo->info)
10422 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10423 || h->root.type != bfd_link_hash_undefweak))
10424 || !h->forced_local)
10425 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10426 {
10427 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10428 (flinfo->output_bfd, flinfo->info, h, &sym)))
10429 {
10430 eoinfo->failed = TRUE;
10431 return FALSE;
10432 }
10433 }
10434
10435 /* If we are marking the symbol as undefined, and there are no
10436 non-weak references to this symbol from a regular object, then
10437 mark the symbol as weak undefined; if there are non-weak
10438 references, mark the symbol as strong. We can't do this earlier,
10439 because it might not be marked as undefined until the
10440 finish_dynamic_symbol routine gets through with it. */
10441 if (sym.st_shndx == SHN_UNDEF
10442 && h->ref_regular
10443 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10444 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10445 {
10446 int bindtype;
10447 type = ELF_ST_TYPE (sym.st_info);
10448
10449 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10450 if (type == STT_GNU_IFUNC)
10451 type = STT_FUNC;
10452
10453 if (h->ref_regular_nonweak)
10454 bindtype = STB_GLOBAL;
10455 else
10456 bindtype = STB_WEAK;
10457 sym.st_info = ELF_ST_INFO (bindtype, type);
10458 }
10459
10460 /* If this is a symbol defined in a dynamic library, don't use the
10461 symbol size from the dynamic library. Relinking an executable
10462 against a new library may introduce gratuitous changes in the
10463 executable's symbols if we keep the size. */
10464 if (sym.st_shndx == SHN_UNDEF
10465 && !h->def_regular
10466 && h->def_dynamic)
10467 sym.st_size = 0;
10468
10469 /* If a non-weak symbol with non-default visibility is not defined
10470 locally, it is a fatal error. */
10471 if (!bfd_link_relocatable (flinfo->info)
10472 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10473 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10474 && h->root.type == bfd_link_hash_undefined
10475 && !h->def_regular)
10476 {
10477 const char *msg;
10478
10479 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10480 /* xgettext:c-format */
10481 msg = _("%pB: protected symbol `%s' isn't defined");
10482 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10483 /* xgettext:c-format */
10484 msg = _("%pB: internal symbol `%s' isn't defined");
10485 else
10486 /* xgettext:c-format */
10487 msg = _("%pB: hidden symbol `%s' isn't defined");
10488 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10489 bfd_set_error (bfd_error_bad_value);
10490 eoinfo->failed = TRUE;
10491 return FALSE;
10492 }
10493
10494 /* If this symbol should be put in the .dynsym section, then put it
10495 there now. We already know the symbol index. We also fill in
10496 the entry in the .hash section. */
10497 if (h->dynindx != -1
10498 && elf_hash_table (flinfo->info)->dynamic_sections_created
10499 && elf_hash_table (flinfo->info)->dynsym != NULL
10500 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10501 {
10502 bfd_byte *esym;
10503
10504 /* Since there is no version information in the dynamic string,
10505 if there is no version info in symbol version section, we will
10506 have a run-time problem if not linking executable, referenced
10507 by shared library, or not bound locally. */
10508 if (h->verinfo.verdef == NULL
10509 && (!bfd_link_executable (flinfo->info)
10510 || h->ref_dynamic
10511 || !h->def_regular))
10512 {
10513 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10514
10515 if (p && p [1] != '\0')
10516 {
10517 _bfd_error_handler
10518 /* xgettext:c-format */
10519 (_("%pB: no symbol version section for versioned symbol `%s'"),
10520 flinfo->output_bfd, h->root.root.string);
10521 eoinfo->failed = TRUE;
10522 return FALSE;
10523 }
10524 }
10525
10526 sym.st_name = h->dynstr_index;
10527 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10528 + h->dynindx * bed->s->sizeof_sym);
10529 if (!check_dynsym (flinfo->output_bfd, &sym))
10530 {
10531 eoinfo->failed = TRUE;
10532 return FALSE;
10533 }
10534
10535 /* Inform the linker of the addition of this symbol. */
10536
10537 if (flinfo->info->callbacks->ctf_new_dynsym)
10538 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10539
10540 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10541
10542 if (flinfo->hash_sec != NULL)
10543 {
10544 size_t hash_entry_size;
10545 bfd_byte *bucketpos;
10546 bfd_vma chain;
10547 size_t bucketcount;
10548 size_t bucket;
10549
10550 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10551 bucket = h->u.elf_hash_value % bucketcount;
10552
10553 hash_entry_size
10554 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10555 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10556 + (bucket + 2) * hash_entry_size);
10557 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10558 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10559 bucketpos);
10560 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10561 ((bfd_byte *) flinfo->hash_sec->contents
10562 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10563 }
10564
10565 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10566 {
10567 Elf_Internal_Versym iversym;
10568 Elf_External_Versym *eversym;
10569
10570 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10571 {
10572 if (h->verinfo.verdef == NULL
10573 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10574 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10575 iversym.vs_vers = 1;
10576 else
10577 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10578 }
10579 else
10580 {
10581 if (h->verinfo.vertree == NULL)
10582 iversym.vs_vers = 1;
10583 else
10584 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10585 if (flinfo->info->create_default_symver)
10586 iversym.vs_vers++;
10587 }
10588
10589 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10590 defined locally. */
10591 if (h->versioned == versioned_hidden && h->def_regular)
10592 iversym.vs_vers |= VERSYM_HIDDEN;
10593
10594 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10595 eversym += h->dynindx;
10596 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10597 }
10598 }
10599
10600 /* If the symbol is undefined, and we didn't output it to .dynsym,
10601 strip it from .symtab too. Obviously we can't do this for
10602 relocatable output or when needed for --emit-relocs. */
10603 else if (input_sec == bfd_und_section_ptr
10604 && h->indx != -2
10605 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10606 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10607 && !bfd_link_relocatable (flinfo->info))
10608 return TRUE;
10609
10610 /* Also strip others that we couldn't earlier due to dynamic symbol
10611 processing. */
10612 if (strip)
10613 return TRUE;
10614 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10615 return TRUE;
10616
10617 /* Output a FILE symbol so that following locals are not associated
10618 with the wrong input file. We need one for forced local symbols
10619 if we've seen more than one FILE symbol or when we have exactly
10620 one FILE symbol but global symbols are present in a file other
10621 than the one with the FILE symbol. We also need one if linker
10622 defined symbols are present. In practice these conditions are
10623 always met, so just emit the FILE symbol unconditionally. */
10624 if (eoinfo->localsyms
10625 && !eoinfo->file_sym_done
10626 && eoinfo->flinfo->filesym_count != 0)
10627 {
10628 Elf_Internal_Sym fsym;
10629
10630 memset (&fsym, 0, sizeof (fsym));
10631 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10632 fsym.st_shndx = SHN_ABS;
10633 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10634 bfd_und_section_ptr, NULL))
10635 return FALSE;
10636
10637 eoinfo->file_sym_done = TRUE;
10638 }
10639
10640 indx = bfd_get_symcount (flinfo->output_bfd);
10641 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10642 input_sec, h);
10643 if (ret == 0)
10644 {
10645 eoinfo->failed = TRUE;
10646 return FALSE;
10647 }
10648 else if (ret == 1)
10649 h->indx = indx;
10650 else if (h->indx == -2)
10651 abort();
10652
10653 return TRUE;
10654 }
10655
10656 /* Return TRUE if special handling is done for relocs in SEC against
10657 symbols defined in discarded sections. */
10658
10659 static bfd_boolean
10660 elf_section_ignore_discarded_relocs (asection *sec)
10661 {
10662 const struct elf_backend_data *bed;
10663
10664 switch (sec->sec_info_type)
10665 {
10666 case SEC_INFO_TYPE_STABS:
10667 case SEC_INFO_TYPE_EH_FRAME:
10668 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10669 return TRUE;
10670 default:
10671 break;
10672 }
10673
10674 bed = get_elf_backend_data (sec->owner);
10675 if (bed->elf_backend_ignore_discarded_relocs != NULL
10676 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10677 return TRUE;
10678
10679 return FALSE;
10680 }
10681
10682 /* Return a mask saying how ld should treat relocations in SEC against
10683 symbols defined in discarded sections. If this function returns
10684 COMPLAIN set, ld will issue a warning message. If this function
10685 returns PRETEND set, and the discarded section was link-once and the
10686 same size as the kept link-once section, ld will pretend that the
10687 symbol was actually defined in the kept section. Otherwise ld will
10688 zero the reloc (at least that is the intent, but some cooperation by
10689 the target dependent code is needed, particularly for REL targets). */
10690
10691 unsigned int
10692 _bfd_elf_default_action_discarded (asection *sec)
10693 {
10694 if (sec->flags & SEC_DEBUGGING)
10695 return PRETEND;
10696
10697 if (strcmp (".eh_frame", sec->name) == 0)
10698 return 0;
10699
10700 if (strcmp (".gcc_except_table", sec->name) == 0)
10701 return 0;
10702
10703 return COMPLAIN | PRETEND;
10704 }
10705
10706 /* Find a match between a section and a member of a section group. */
10707
10708 static asection *
10709 match_group_member (asection *sec, asection *group,
10710 struct bfd_link_info *info)
10711 {
10712 asection *first = elf_next_in_group (group);
10713 asection *s = first;
10714
10715 while (s != NULL)
10716 {
10717 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10718 return s;
10719
10720 s = elf_next_in_group (s);
10721 if (s == first)
10722 break;
10723 }
10724
10725 return NULL;
10726 }
10727
10728 /* Check if the kept section of a discarded section SEC can be used
10729 to replace it. Return the replacement if it is OK. Otherwise return
10730 NULL. */
10731
10732 asection *
10733 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10734 {
10735 asection *kept;
10736
10737 kept = sec->kept_section;
10738 if (kept != NULL)
10739 {
10740 if ((kept->flags & SEC_GROUP) != 0)
10741 kept = match_group_member (sec, kept, info);
10742 if (kept != NULL)
10743 {
10744 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10745 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10746 kept = NULL;
10747 else
10748 {
10749 /* Get the real kept section. */
10750 asection *next;
10751 for (next = kept->kept_section;
10752 next != NULL;
10753 next = next->kept_section)
10754 kept = next;
10755 }
10756 }
10757 sec->kept_section = kept;
10758 }
10759 return kept;
10760 }
10761
10762 /* Link an input file into the linker output file. This function
10763 handles all the sections and relocations of the input file at once.
10764 This is so that we only have to read the local symbols once, and
10765 don't have to keep them in memory. */
10766
10767 static bfd_boolean
10768 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10769 {
10770 int (*relocate_section)
10771 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10772 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10773 bfd *output_bfd;
10774 Elf_Internal_Shdr *symtab_hdr;
10775 size_t locsymcount;
10776 size_t extsymoff;
10777 Elf_Internal_Sym *isymbuf;
10778 Elf_Internal_Sym *isym;
10779 Elf_Internal_Sym *isymend;
10780 long *pindex;
10781 asection **ppsection;
10782 asection *o;
10783 const struct elf_backend_data *bed;
10784 struct elf_link_hash_entry **sym_hashes;
10785 bfd_size_type address_size;
10786 bfd_vma r_type_mask;
10787 int r_sym_shift;
10788 bfd_boolean have_file_sym = FALSE;
10789
10790 output_bfd = flinfo->output_bfd;
10791 bed = get_elf_backend_data (output_bfd);
10792 relocate_section = bed->elf_backend_relocate_section;
10793
10794 /* If this is a dynamic object, we don't want to do anything here:
10795 we don't want the local symbols, and we don't want the section
10796 contents. */
10797 if ((input_bfd->flags & DYNAMIC) != 0)
10798 return TRUE;
10799
10800 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10801 if (elf_bad_symtab (input_bfd))
10802 {
10803 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10804 extsymoff = 0;
10805 }
10806 else
10807 {
10808 locsymcount = symtab_hdr->sh_info;
10809 extsymoff = symtab_hdr->sh_info;
10810 }
10811
10812 /* Enable GNU OSABI features in the output BFD that are used in the input
10813 BFD. */
10814 if (bed->elf_osabi == ELFOSABI_NONE
10815 || bed->elf_osabi == ELFOSABI_GNU
10816 || bed->elf_osabi == ELFOSABI_FREEBSD)
10817 elf_tdata (output_bfd)->has_gnu_osabi
10818 |= (elf_tdata (input_bfd)->has_gnu_osabi
10819 & (bfd_link_relocatable (flinfo->info)
10820 ? -1 : ~elf_gnu_osabi_retain));
10821
10822 /* Read the local symbols. */
10823 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10824 if (isymbuf == NULL && locsymcount != 0)
10825 {
10826 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10827 flinfo->internal_syms,
10828 flinfo->external_syms,
10829 flinfo->locsym_shndx);
10830 if (isymbuf == NULL)
10831 return FALSE;
10832 }
10833
10834 /* Find local symbol sections and adjust values of symbols in
10835 SEC_MERGE sections. Write out those local symbols we know are
10836 going into the output file. */
10837 isymend = isymbuf + locsymcount;
10838 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10839 isym < isymend;
10840 isym++, pindex++, ppsection++)
10841 {
10842 asection *isec;
10843 const char *name;
10844 Elf_Internal_Sym osym;
10845 long indx;
10846 int ret;
10847
10848 *pindex = -1;
10849
10850 if (elf_bad_symtab (input_bfd))
10851 {
10852 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10853 {
10854 *ppsection = NULL;
10855 continue;
10856 }
10857 }
10858
10859 if (isym->st_shndx == SHN_UNDEF)
10860 isec = bfd_und_section_ptr;
10861 else if (isym->st_shndx == SHN_ABS)
10862 isec = bfd_abs_section_ptr;
10863 else if (isym->st_shndx == SHN_COMMON)
10864 isec = bfd_com_section_ptr;
10865 else
10866 {
10867 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10868 if (isec == NULL)
10869 {
10870 /* Don't attempt to output symbols with st_shnx in the
10871 reserved range other than SHN_ABS and SHN_COMMON. */
10872 isec = bfd_und_section_ptr;
10873 }
10874 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10875 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10876 isym->st_value =
10877 _bfd_merged_section_offset (output_bfd, &isec,
10878 elf_section_data (isec)->sec_info,
10879 isym->st_value);
10880 }
10881
10882 *ppsection = isec;
10883
10884 /* Don't output the first, undefined, symbol. In fact, don't
10885 output any undefined local symbol. */
10886 if (isec == bfd_und_section_ptr)
10887 continue;
10888
10889 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10890 {
10891 /* We never output section symbols. Instead, we use the
10892 section symbol of the corresponding section in the output
10893 file. */
10894 continue;
10895 }
10896
10897 /* If we are stripping all symbols, we don't want to output this
10898 one. */
10899 if (flinfo->info->strip == strip_all)
10900 continue;
10901
10902 /* If we are discarding all local symbols, we don't want to
10903 output this one. If we are generating a relocatable output
10904 file, then some of the local symbols may be required by
10905 relocs; we output them below as we discover that they are
10906 needed. */
10907 if (flinfo->info->discard == discard_all)
10908 continue;
10909
10910 /* If this symbol is defined in a section which we are
10911 discarding, we don't need to keep it. */
10912 if (isym->st_shndx != SHN_UNDEF
10913 && isym->st_shndx < SHN_LORESERVE
10914 && isec->output_section == NULL
10915 && flinfo->info->non_contiguous_regions
10916 && flinfo->info->non_contiguous_regions_warnings)
10917 {
10918 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10919 "discards section `%s' from '%s'\n"),
10920 isec->name, bfd_get_filename (isec->owner));
10921 continue;
10922 }
10923
10924 if (isym->st_shndx != SHN_UNDEF
10925 && isym->st_shndx < SHN_LORESERVE
10926 && bfd_section_removed_from_list (output_bfd,
10927 isec->output_section))
10928 continue;
10929
10930 /* Get the name of the symbol. */
10931 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10932 isym->st_name);
10933 if (name == NULL)
10934 return FALSE;
10935
10936 /* See if we are discarding symbols with this name. */
10937 if ((flinfo->info->strip == strip_some
10938 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10939 == NULL))
10940 || (((flinfo->info->discard == discard_sec_merge
10941 && (isec->flags & SEC_MERGE)
10942 && !bfd_link_relocatable (flinfo->info))
10943 || flinfo->info->discard == discard_l)
10944 && bfd_is_local_label_name (input_bfd, name)))
10945 continue;
10946
10947 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10948 {
10949 if (input_bfd->lto_output)
10950 /* -flto puts a temp file name here. This means builds
10951 are not reproducible. Discard the symbol. */
10952 continue;
10953 have_file_sym = TRUE;
10954 flinfo->filesym_count += 1;
10955 }
10956 if (!have_file_sym)
10957 {
10958 /* In the absence of debug info, bfd_find_nearest_line uses
10959 FILE symbols to determine the source file for local
10960 function symbols. Provide a FILE symbol here if input
10961 files lack such, so that their symbols won't be
10962 associated with a previous input file. It's not the
10963 source file, but the best we can do. */
10964 const char *filename;
10965 have_file_sym = TRUE;
10966 flinfo->filesym_count += 1;
10967 memset (&osym, 0, sizeof (osym));
10968 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10969 osym.st_shndx = SHN_ABS;
10970 if (input_bfd->lto_output)
10971 filename = NULL;
10972 else
10973 filename = lbasename (bfd_get_filename (input_bfd));
10974 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
10975 bfd_abs_section_ptr, NULL))
10976 return FALSE;
10977 }
10978
10979 osym = *isym;
10980
10981 /* Adjust the section index for the output file. */
10982 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10983 isec->output_section);
10984 if (osym.st_shndx == SHN_BAD)
10985 return FALSE;
10986
10987 /* ELF symbols in relocatable files are section relative, but
10988 in executable files they are virtual addresses. Note that
10989 this code assumes that all ELF sections have an associated
10990 BFD section with a reasonable value for output_offset; below
10991 we assume that they also have a reasonable value for
10992 output_section. Any special sections must be set up to meet
10993 these requirements. */
10994 osym.st_value += isec->output_offset;
10995 if (!bfd_link_relocatable (flinfo->info))
10996 {
10997 osym.st_value += isec->output_section->vma;
10998 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10999 {
11000 /* STT_TLS symbols are relative to PT_TLS segment base. */
11001 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11002 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11003 else
11004 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11005 STT_NOTYPE);
11006 }
11007 }
11008
11009 indx = bfd_get_symcount (output_bfd);
11010 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11011 if (ret == 0)
11012 return FALSE;
11013 else if (ret == 1)
11014 *pindex = indx;
11015 }
11016
11017 if (bed->s->arch_size == 32)
11018 {
11019 r_type_mask = 0xff;
11020 r_sym_shift = 8;
11021 address_size = 4;
11022 }
11023 else
11024 {
11025 r_type_mask = 0xffffffff;
11026 r_sym_shift = 32;
11027 address_size = 8;
11028 }
11029
11030 /* Relocate the contents of each section. */
11031 sym_hashes = elf_sym_hashes (input_bfd);
11032 for (o = input_bfd->sections; o != NULL; o = o->next)
11033 {
11034 bfd_byte *contents;
11035
11036 if (! o->linker_mark)
11037 {
11038 /* This section was omitted from the link. */
11039 continue;
11040 }
11041
11042 if (!flinfo->info->resolve_section_groups
11043 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11044 {
11045 /* Deal with the group signature symbol. */
11046 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11047 unsigned long symndx = sec_data->this_hdr.sh_info;
11048 asection *osec = o->output_section;
11049
11050 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11051 if (symndx >= locsymcount
11052 || (elf_bad_symtab (input_bfd)
11053 && flinfo->sections[symndx] == NULL))
11054 {
11055 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11056 while (h->root.type == bfd_link_hash_indirect
11057 || h->root.type == bfd_link_hash_warning)
11058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11059 /* Arrange for symbol to be output. */
11060 h->indx = -2;
11061 elf_section_data (osec)->this_hdr.sh_info = -2;
11062 }
11063 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11064 {
11065 /* We'll use the output section target_index. */
11066 asection *sec = flinfo->sections[symndx]->output_section;
11067 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11068 }
11069 else
11070 {
11071 if (flinfo->indices[symndx] == -1)
11072 {
11073 /* Otherwise output the local symbol now. */
11074 Elf_Internal_Sym sym = isymbuf[symndx];
11075 asection *sec = flinfo->sections[symndx]->output_section;
11076 const char *name;
11077 long indx;
11078 int ret;
11079
11080 name = bfd_elf_string_from_elf_section (input_bfd,
11081 symtab_hdr->sh_link,
11082 sym.st_name);
11083 if (name == NULL)
11084 return FALSE;
11085
11086 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11087 sec);
11088 if (sym.st_shndx == SHN_BAD)
11089 return FALSE;
11090
11091 sym.st_value += o->output_offset;
11092
11093 indx = bfd_get_symcount (output_bfd);
11094 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11095 NULL);
11096 if (ret == 0)
11097 return FALSE;
11098 else if (ret == 1)
11099 flinfo->indices[symndx] = indx;
11100 else
11101 abort ();
11102 }
11103 elf_section_data (osec)->this_hdr.sh_info
11104 = flinfo->indices[symndx];
11105 }
11106 }
11107
11108 if ((o->flags & SEC_HAS_CONTENTS) == 0
11109 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11110 continue;
11111
11112 if ((o->flags & SEC_LINKER_CREATED) != 0)
11113 {
11114 /* Section was created by _bfd_elf_link_create_dynamic_sections
11115 or somesuch. */
11116 continue;
11117 }
11118
11119 /* Get the contents of the section. They have been cached by a
11120 relaxation routine. Note that o is a section in an input
11121 file, so the contents field will not have been set by any of
11122 the routines which work on output files. */
11123 if (elf_section_data (o)->this_hdr.contents != NULL)
11124 {
11125 contents = elf_section_data (o)->this_hdr.contents;
11126 if (bed->caches_rawsize
11127 && o->rawsize != 0
11128 && o->rawsize < o->size)
11129 {
11130 memcpy (flinfo->contents, contents, o->rawsize);
11131 contents = flinfo->contents;
11132 }
11133 }
11134 else
11135 {
11136 contents = flinfo->contents;
11137 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11138 return FALSE;
11139 }
11140
11141 if ((o->flags & SEC_RELOC) != 0)
11142 {
11143 Elf_Internal_Rela *internal_relocs;
11144 Elf_Internal_Rela *rel, *relend;
11145 int action_discarded;
11146 int ret;
11147
11148 /* Get the swapped relocs. */
11149 internal_relocs
11150 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11151 flinfo->internal_relocs, FALSE);
11152 if (internal_relocs == NULL
11153 && o->reloc_count > 0)
11154 return FALSE;
11155
11156 /* We need to reverse-copy input .ctors/.dtors sections if
11157 they are placed in .init_array/.finit_array for output. */
11158 if (o->size > address_size
11159 && ((strncmp (o->name, ".ctors", 6) == 0
11160 && strcmp (o->output_section->name,
11161 ".init_array") == 0)
11162 || (strncmp (o->name, ".dtors", 6) == 0
11163 && strcmp (o->output_section->name,
11164 ".fini_array") == 0))
11165 && (o->name[6] == 0 || o->name[6] == '.'))
11166 {
11167 if (o->size * bed->s->int_rels_per_ext_rel
11168 != o->reloc_count * address_size)
11169 {
11170 _bfd_error_handler
11171 /* xgettext:c-format */
11172 (_("error: %pB: size of section %pA is not "
11173 "multiple of address size"),
11174 input_bfd, o);
11175 bfd_set_error (bfd_error_bad_value);
11176 return FALSE;
11177 }
11178 o->flags |= SEC_ELF_REVERSE_COPY;
11179 }
11180
11181 action_discarded = -1;
11182 if (!elf_section_ignore_discarded_relocs (o))
11183 action_discarded = (*bed->action_discarded) (o);
11184
11185 /* Run through the relocs evaluating complex reloc symbols and
11186 looking for relocs against symbols from discarded sections
11187 or section symbols from removed link-once sections.
11188 Complain about relocs against discarded sections. Zero
11189 relocs against removed link-once sections. */
11190
11191 rel = internal_relocs;
11192 relend = rel + o->reloc_count;
11193 for ( ; rel < relend; rel++)
11194 {
11195 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11196 unsigned int s_type;
11197 asection **ps, *sec;
11198 struct elf_link_hash_entry *h = NULL;
11199 const char *sym_name;
11200
11201 if (r_symndx == STN_UNDEF)
11202 continue;
11203
11204 if (r_symndx >= locsymcount
11205 || (elf_bad_symtab (input_bfd)
11206 && flinfo->sections[r_symndx] == NULL))
11207 {
11208 h = sym_hashes[r_symndx - extsymoff];
11209
11210 /* Badly formatted input files can contain relocs that
11211 reference non-existant symbols. Check here so that
11212 we do not seg fault. */
11213 if (h == NULL)
11214 {
11215 _bfd_error_handler
11216 /* xgettext:c-format */
11217 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11218 "that references a non-existent global symbol"),
11219 input_bfd, (uint64_t) rel->r_info, o);
11220 bfd_set_error (bfd_error_bad_value);
11221 return FALSE;
11222 }
11223
11224 while (h->root.type == bfd_link_hash_indirect
11225 || h->root.type == bfd_link_hash_warning)
11226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11227
11228 s_type = h->type;
11229
11230 /* If a plugin symbol is referenced from a non-IR file,
11231 mark the symbol as undefined. Note that the
11232 linker may attach linker created dynamic sections
11233 to the plugin bfd. Symbols defined in linker
11234 created sections are not plugin symbols. */
11235 if ((h->root.non_ir_ref_regular
11236 || h->root.non_ir_ref_dynamic)
11237 && (h->root.type == bfd_link_hash_defined
11238 || h->root.type == bfd_link_hash_defweak)
11239 && (h->root.u.def.section->flags
11240 & SEC_LINKER_CREATED) == 0
11241 && h->root.u.def.section->owner != NULL
11242 && (h->root.u.def.section->owner->flags
11243 & BFD_PLUGIN) != 0)
11244 {
11245 h->root.type = bfd_link_hash_undefined;
11246 h->root.u.undef.abfd = h->root.u.def.section->owner;
11247 }
11248
11249 ps = NULL;
11250 if (h->root.type == bfd_link_hash_defined
11251 || h->root.type == bfd_link_hash_defweak)
11252 ps = &h->root.u.def.section;
11253
11254 sym_name = h->root.root.string;
11255 }
11256 else
11257 {
11258 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11259
11260 s_type = ELF_ST_TYPE (sym->st_info);
11261 ps = &flinfo->sections[r_symndx];
11262 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11263 sym, *ps);
11264 }
11265
11266 if ((s_type == STT_RELC || s_type == STT_SRELC)
11267 && !bfd_link_relocatable (flinfo->info))
11268 {
11269 bfd_vma val;
11270 bfd_vma dot = (rel->r_offset
11271 + o->output_offset + o->output_section->vma);
11272 #ifdef DEBUG
11273 printf ("Encountered a complex symbol!");
11274 printf (" (input_bfd %s, section %s, reloc %ld\n",
11275 bfd_get_filename (input_bfd), o->name,
11276 (long) (rel - internal_relocs));
11277 printf (" symbol: idx %8.8lx, name %s\n",
11278 r_symndx, sym_name);
11279 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11280 (unsigned long) rel->r_info,
11281 (unsigned long) rel->r_offset);
11282 #endif
11283 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11284 isymbuf, locsymcount, s_type == STT_SRELC))
11285 return FALSE;
11286
11287 /* Symbol evaluated OK. Update to absolute value. */
11288 set_symbol_value (input_bfd, isymbuf, locsymcount,
11289 r_symndx, val);
11290 continue;
11291 }
11292
11293 if (action_discarded != -1 && ps != NULL)
11294 {
11295 /* Complain if the definition comes from a
11296 discarded section. */
11297 if ((sec = *ps) != NULL && discarded_section (sec))
11298 {
11299 BFD_ASSERT (r_symndx != STN_UNDEF);
11300 if (action_discarded & COMPLAIN)
11301 (*flinfo->info->callbacks->einfo)
11302 /* xgettext:c-format */
11303 (_("%X`%s' referenced in section `%pA' of %pB: "
11304 "defined in discarded section `%pA' of %pB\n"),
11305 sym_name, o, input_bfd, sec, sec->owner);
11306
11307 /* Try to do the best we can to support buggy old
11308 versions of gcc. Pretend that the symbol is
11309 really defined in the kept linkonce section.
11310 FIXME: This is quite broken. Modifying the
11311 symbol here means we will be changing all later
11312 uses of the symbol, not just in this section. */
11313 if (action_discarded & PRETEND)
11314 {
11315 asection *kept;
11316
11317 kept = _bfd_elf_check_kept_section (sec,
11318 flinfo->info);
11319 if (kept != NULL)
11320 {
11321 *ps = kept;
11322 continue;
11323 }
11324 }
11325 }
11326 }
11327 }
11328
11329 /* Relocate the section by invoking a back end routine.
11330
11331 The back end routine is responsible for adjusting the
11332 section contents as necessary, and (if using Rela relocs
11333 and generating a relocatable output file) adjusting the
11334 reloc addend as necessary.
11335
11336 The back end routine does not have to worry about setting
11337 the reloc address or the reloc symbol index.
11338
11339 The back end routine is given a pointer to the swapped in
11340 internal symbols, and can access the hash table entries
11341 for the external symbols via elf_sym_hashes (input_bfd).
11342
11343 When generating relocatable output, the back end routine
11344 must handle STB_LOCAL/STT_SECTION symbols specially. The
11345 output symbol is going to be a section symbol
11346 corresponding to the output section, which will require
11347 the addend to be adjusted. */
11348
11349 ret = (*relocate_section) (output_bfd, flinfo->info,
11350 input_bfd, o, contents,
11351 internal_relocs,
11352 isymbuf,
11353 flinfo->sections);
11354 if (!ret)
11355 return FALSE;
11356
11357 if (ret == 2
11358 || bfd_link_relocatable (flinfo->info)
11359 || flinfo->info->emitrelocations)
11360 {
11361 Elf_Internal_Rela *irela;
11362 Elf_Internal_Rela *irelaend, *irelamid;
11363 bfd_vma last_offset;
11364 struct elf_link_hash_entry **rel_hash;
11365 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11366 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11367 unsigned int next_erel;
11368 bfd_boolean rela_normal;
11369 struct bfd_elf_section_data *esdi, *esdo;
11370
11371 esdi = elf_section_data (o);
11372 esdo = elf_section_data (o->output_section);
11373 rela_normal = FALSE;
11374
11375 /* Adjust the reloc addresses and symbol indices. */
11376
11377 irela = internal_relocs;
11378 irelaend = irela + o->reloc_count;
11379 rel_hash = esdo->rel.hashes + esdo->rel.count;
11380 /* We start processing the REL relocs, if any. When we reach
11381 IRELAMID in the loop, we switch to the RELA relocs. */
11382 irelamid = irela;
11383 if (esdi->rel.hdr != NULL)
11384 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11385 * bed->s->int_rels_per_ext_rel);
11386 rel_hash_list = rel_hash;
11387 rela_hash_list = NULL;
11388 last_offset = o->output_offset;
11389 if (!bfd_link_relocatable (flinfo->info))
11390 last_offset += o->output_section->vma;
11391 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11392 {
11393 unsigned long r_symndx;
11394 asection *sec;
11395 Elf_Internal_Sym sym;
11396
11397 if (next_erel == bed->s->int_rels_per_ext_rel)
11398 {
11399 rel_hash++;
11400 next_erel = 0;
11401 }
11402
11403 if (irela == irelamid)
11404 {
11405 rel_hash = esdo->rela.hashes + esdo->rela.count;
11406 rela_hash_list = rel_hash;
11407 rela_normal = bed->rela_normal;
11408 }
11409
11410 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11411 flinfo->info, o,
11412 irela->r_offset);
11413 if (irela->r_offset >= (bfd_vma) -2)
11414 {
11415 /* This is a reloc for a deleted entry or somesuch.
11416 Turn it into an R_*_NONE reloc, at the same
11417 offset as the last reloc. elf_eh_frame.c and
11418 bfd_elf_discard_info rely on reloc offsets
11419 being ordered. */
11420 irela->r_offset = last_offset;
11421 irela->r_info = 0;
11422 irela->r_addend = 0;
11423 continue;
11424 }
11425
11426 irela->r_offset += o->output_offset;
11427
11428 /* Relocs in an executable have to be virtual addresses. */
11429 if (!bfd_link_relocatable (flinfo->info))
11430 irela->r_offset += o->output_section->vma;
11431
11432 last_offset = irela->r_offset;
11433
11434 r_symndx = irela->r_info >> r_sym_shift;
11435 if (r_symndx == STN_UNDEF)
11436 continue;
11437
11438 if (r_symndx >= locsymcount
11439 || (elf_bad_symtab (input_bfd)
11440 && flinfo->sections[r_symndx] == NULL))
11441 {
11442 struct elf_link_hash_entry *rh;
11443 unsigned long indx;
11444
11445 /* This is a reloc against a global symbol. We
11446 have not yet output all the local symbols, so
11447 we do not know the symbol index of any global
11448 symbol. We set the rel_hash entry for this
11449 reloc to point to the global hash table entry
11450 for this symbol. The symbol index is then
11451 set at the end of bfd_elf_final_link. */
11452 indx = r_symndx - extsymoff;
11453 rh = elf_sym_hashes (input_bfd)[indx];
11454 while (rh->root.type == bfd_link_hash_indirect
11455 || rh->root.type == bfd_link_hash_warning)
11456 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11457
11458 /* Setting the index to -2 tells
11459 elf_link_output_extsym that this symbol is
11460 used by a reloc. */
11461 BFD_ASSERT (rh->indx < 0);
11462 rh->indx = -2;
11463 *rel_hash = rh;
11464
11465 continue;
11466 }
11467
11468 /* This is a reloc against a local symbol. */
11469
11470 *rel_hash = NULL;
11471 sym = isymbuf[r_symndx];
11472 sec = flinfo->sections[r_symndx];
11473 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11474 {
11475 /* I suppose the backend ought to fill in the
11476 section of any STT_SECTION symbol against a
11477 processor specific section. */
11478 r_symndx = STN_UNDEF;
11479 if (bfd_is_abs_section (sec))
11480 ;
11481 else if (sec == NULL || sec->owner == NULL)
11482 {
11483 bfd_set_error (bfd_error_bad_value);
11484 return FALSE;
11485 }
11486 else
11487 {
11488 asection *osec = sec->output_section;
11489
11490 /* If we have discarded a section, the output
11491 section will be the absolute section. In
11492 case of discarded SEC_MERGE sections, use
11493 the kept section. relocate_section should
11494 have already handled discarded linkonce
11495 sections. */
11496 if (bfd_is_abs_section (osec)
11497 && sec->kept_section != NULL
11498 && sec->kept_section->output_section != NULL)
11499 {
11500 osec = sec->kept_section->output_section;
11501 irela->r_addend -= osec->vma;
11502 }
11503
11504 if (!bfd_is_abs_section (osec))
11505 {
11506 r_symndx = osec->target_index;
11507 if (r_symndx == STN_UNDEF)
11508 {
11509 irela->r_addend += osec->vma;
11510 osec = _bfd_nearby_section (output_bfd, osec,
11511 osec->vma);
11512 irela->r_addend -= osec->vma;
11513 r_symndx = osec->target_index;
11514 }
11515 }
11516 }
11517
11518 /* Adjust the addend according to where the
11519 section winds up in the output section. */
11520 if (rela_normal)
11521 irela->r_addend += sec->output_offset;
11522 }
11523 else
11524 {
11525 if (flinfo->indices[r_symndx] == -1)
11526 {
11527 unsigned long shlink;
11528 const char *name;
11529 asection *osec;
11530 long indx;
11531
11532 if (flinfo->info->strip == strip_all)
11533 {
11534 /* You can't do ld -r -s. */
11535 bfd_set_error (bfd_error_invalid_operation);
11536 return FALSE;
11537 }
11538
11539 /* This symbol was skipped earlier, but
11540 since it is needed by a reloc, we
11541 must output it now. */
11542 shlink = symtab_hdr->sh_link;
11543 name = (bfd_elf_string_from_elf_section
11544 (input_bfd, shlink, sym.st_name));
11545 if (name == NULL)
11546 return FALSE;
11547
11548 osec = sec->output_section;
11549 sym.st_shndx =
11550 _bfd_elf_section_from_bfd_section (output_bfd,
11551 osec);
11552 if (sym.st_shndx == SHN_BAD)
11553 return FALSE;
11554
11555 sym.st_value += sec->output_offset;
11556 if (!bfd_link_relocatable (flinfo->info))
11557 {
11558 sym.st_value += osec->vma;
11559 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11560 {
11561 struct elf_link_hash_table *htab
11562 = elf_hash_table (flinfo->info);
11563
11564 /* STT_TLS symbols are relative to PT_TLS
11565 segment base. */
11566 if (htab->tls_sec != NULL)
11567 sym.st_value -= htab->tls_sec->vma;
11568 else
11569 sym.st_info
11570 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11571 STT_NOTYPE);
11572 }
11573 }
11574
11575 indx = bfd_get_symcount (output_bfd);
11576 ret = elf_link_output_symstrtab (flinfo, name,
11577 &sym, sec,
11578 NULL);
11579 if (ret == 0)
11580 return FALSE;
11581 else if (ret == 1)
11582 flinfo->indices[r_symndx] = indx;
11583 else
11584 abort ();
11585 }
11586
11587 r_symndx = flinfo->indices[r_symndx];
11588 }
11589
11590 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11591 | (irela->r_info & r_type_mask));
11592 }
11593
11594 /* Swap out the relocs. */
11595 input_rel_hdr = esdi->rel.hdr;
11596 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11597 {
11598 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11599 input_rel_hdr,
11600 internal_relocs,
11601 rel_hash_list))
11602 return FALSE;
11603 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11604 * bed->s->int_rels_per_ext_rel);
11605 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11606 }
11607
11608 input_rela_hdr = esdi->rela.hdr;
11609 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11610 {
11611 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11612 input_rela_hdr,
11613 internal_relocs,
11614 rela_hash_list))
11615 return FALSE;
11616 }
11617 }
11618 }
11619
11620 /* Write out the modified section contents. */
11621 if (bed->elf_backend_write_section
11622 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11623 contents))
11624 {
11625 /* Section written out. */
11626 }
11627 else switch (o->sec_info_type)
11628 {
11629 case SEC_INFO_TYPE_STABS:
11630 if (! (_bfd_write_section_stabs
11631 (output_bfd,
11632 &elf_hash_table (flinfo->info)->stab_info,
11633 o, &elf_section_data (o)->sec_info, contents)))
11634 return FALSE;
11635 break;
11636 case SEC_INFO_TYPE_MERGE:
11637 if (! _bfd_write_merged_section (output_bfd, o,
11638 elf_section_data (o)->sec_info))
11639 return FALSE;
11640 break;
11641 case SEC_INFO_TYPE_EH_FRAME:
11642 {
11643 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11644 o, contents))
11645 return FALSE;
11646 }
11647 break;
11648 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11649 {
11650 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11651 flinfo->info,
11652 o, contents))
11653 return FALSE;
11654 }
11655 break;
11656 default:
11657 {
11658 if (! (o->flags & SEC_EXCLUDE))
11659 {
11660 file_ptr offset = (file_ptr) o->output_offset;
11661 bfd_size_type todo = o->size;
11662
11663 offset *= bfd_octets_per_byte (output_bfd, o);
11664
11665 if ((o->flags & SEC_ELF_REVERSE_COPY))
11666 {
11667 /* Reverse-copy input section to output. */
11668 do
11669 {
11670 todo -= address_size;
11671 if (! bfd_set_section_contents (output_bfd,
11672 o->output_section,
11673 contents + todo,
11674 offset,
11675 address_size))
11676 return FALSE;
11677 if (todo == 0)
11678 break;
11679 offset += address_size;
11680 }
11681 while (1);
11682 }
11683 else if (! bfd_set_section_contents (output_bfd,
11684 o->output_section,
11685 contents,
11686 offset, todo))
11687 return FALSE;
11688 }
11689 }
11690 break;
11691 }
11692 }
11693
11694 return TRUE;
11695 }
11696
11697 /* Generate a reloc when linking an ELF file. This is a reloc
11698 requested by the linker, and does not come from any input file. This
11699 is used to build constructor and destructor tables when linking
11700 with -Ur. */
11701
11702 static bfd_boolean
11703 elf_reloc_link_order (bfd *output_bfd,
11704 struct bfd_link_info *info,
11705 asection *output_section,
11706 struct bfd_link_order *link_order)
11707 {
11708 reloc_howto_type *howto;
11709 long indx;
11710 bfd_vma offset;
11711 bfd_vma addend;
11712 struct bfd_elf_section_reloc_data *reldata;
11713 struct elf_link_hash_entry **rel_hash_ptr;
11714 Elf_Internal_Shdr *rel_hdr;
11715 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11716 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11717 bfd_byte *erel;
11718 unsigned int i;
11719 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11720
11721 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11722 if (howto == NULL)
11723 {
11724 bfd_set_error (bfd_error_bad_value);
11725 return FALSE;
11726 }
11727
11728 addend = link_order->u.reloc.p->addend;
11729
11730 if (esdo->rel.hdr)
11731 reldata = &esdo->rel;
11732 else if (esdo->rela.hdr)
11733 reldata = &esdo->rela;
11734 else
11735 {
11736 reldata = NULL;
11737 BFD_ASSERT (0);
11738 }
11739
11740 /* Figure out the symbol index. */
11741 rel_hash_ptr = reldata->hashes + reldata->count;
11742 if (link_order->type == bfd_section_reloc_link_order)
11743 {
11744 indx = link_order->u.reloc.p->u.section->target_index;
11745 BFD_ASSERT (indx != 0);
11746 *rel_hash_ptr = NULL;
11747 }
11748 else
11749 {
11750 struct elf_link_hash_entry *h;
11751
11752 /* Treat a reloc against a defined symbol as though it were
11753 actually against the section. */
11754 h = ((struct elf_link_hash_entry *)
11755 bfd_wrapped_link_hash_lookup (output_bfd, info,
11756 link_order->u.reloc.p->u.name,
11757 FALSE, FALSE, TRUE));
11758 if (h != NULL
11759 && (h->root.type == bfd_link_hash_defined
11760 || h->root.type == bfd_link_hash_defweak))
11761 {
11762 asection *section;
11763
11764 section = h->root.u.def.section;
11765 indx = section->output_section->target_index;
11766 *rel_hash_ptr = NULL;
11767 /* It seems that we ought to add the symbol value to the
11768 addend here, but in practice it has already been added
11769 because it was passed to constructor_callback. */
11770 addend += section->output_section->vma + section->output_offset;
11771 }
11772 else if (h != NULL)
11773 {
11774 /* Setting the index to -2 tells elf_link_output_extsym that
11775 this symbol is used by a reloc. */
11776 h->indx = -2;
11777 *rel_hash_ptr = h;
11778 indx = 0;
11779 }
11780 else
11781 {
11782 (*info->callbacks->unattached_reloc)
11783 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11784 indx = 0;
11785 }
11786 }
11787
11788 /* If this is an inplace reloc, we must write the addend into the
11789 object file. */
11790 if (howto->partial_inplace && addend != 0)
11791 {
11792 bfd_size_type size;
11793 bfd_reloc_status_type rstat;
11794 bfd_byte *buf;
11795 bfd_boolean ok;
11796 const char *sym_name;
11797 bfd_size_type octets;
11798
11799 size = (bfd_size_type) bfd_get_reloc_size (howto);
11800 buf = (bfd_byte *) bfd_zmalloc (size);
11801 if (buf == NULL && size != 0)
11802 return FALSE;
11803 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11804 switch (rstat)
11805 {
11806 case bfd_reloc_ok:
11807 break;
11808
11809 default:
11810 case bfd_reloc_outofrange:
11811 abort ();
11812
11813 case bfd_reloc_overflow:
11814 if (link_order->type == bfd_section_reloc_link_order)
11815 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11816 else
11817 sym_name = link_order->u.reloc.p->u.name;
11818 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11819 howto->name, addend, NULL, NULL,
11820 (bfd_vma) 0);
11821 break;
11822 }
11823
11824 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11825 output_section);
11826 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11827 octets, size);
11828 free (buf);
11829 if (! ok)
11830 return FALSE;
11831 }
11832
11833 /* The address of a reloc is relative to the section in a
11834 relocatable file, and is a virtual address in an executable
11835 file. */
11836 offset = link_order->offset;
11837 if (! bfd_link_relocatable (info))
11838 offset += output_section->vma;
11839
11840 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11841 {
11842 irel[i].r_offset = offset;
11843 irel[i].r_info = 0;
11844 irel[i].r_addend = 0;
11845 }
11846 if (bed->s->arch_size == 32)
11847 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11848 else
11849 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11850
11851 rel_hdr = reldata->hdr;
11852 erel = rel_hdr->contents;
11853 if (rel_hdr->sh_type == SHT_REL)
11854 {
11855 erel += reldata->count * bed->s->sizeof_rel;
11856 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11857 }
11858 else
11859 {
11860 irel[0].r_addend = addend;
11861 erel += reldata->count * bed->s->sizeof_rela;
11862 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11863 }
11864
11865 ++reldata->count;
11866
11867 return TRUE;
11868 }
11869
11870 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11871 Returns TRUE upon success, FALSE otherwise. */
11872
11873 static bfd_boolean
11874 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11875 {
11876 bfd_boolean ret = FALSE;
11877 bfd *implib_bfd;
11878 const struct elf_backend_data *bed;
11879 flagword flags;
11880 enum bfd_architecture arch;
11881 unsigned int mach;
11882 asymbol **sympp = NULL;
11883 long symsize;
11884 long symcount;
11885 long src_count;
11886 elf_symbol_type *osymbuf;
11887 size_t amt;
11888
11889 implib_bfd = info->out_implib_bfd;
11890 bed = get_elf_backend_data (abfd);
11891
11892 if (!bfd_set_format (implib_bfd, bfd_object))
11893 return FALSE;
11894
11895 /* Use flag from executable but make it a relocatable object. */
11896 flags = bfd_get_file_flags (abfd);
11897 flags &= ~HAS_RELOC;
11898 if (!bfd_set_start_address (implib_bfd, 0)
11899 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11900 return FALSE;
11901
11902 /* Copy architecture of output file to import library file. */
11903 arch = bfd_get_arch (abfd);
11904 mach = bfd_get_mach (abfd);
11905 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11906 && (abfd->target_defaulted
11907 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11908 return FALSE;
11909
11910 /* Get symbol table size. */
11911 symsize = bfd_get_symtab_upper_bound (abfd);
11912 if (symsize < 0)
11913 return FALSE;
11914
11915 /* Read in the symbol table. */
11916 sympp = (asymbol **) bfd_malloc (symsize);
11917 if (sympp == NULL)
11918 return FALSE;
11919
11920 symcount = bfd_canonicalize_symtab (abfd, sympp);
11921 if (symcount < 0)
11922 goto free_sym_buf;
11923
11924 /* Allow the BFD backend to copy any private header data it
11925 understands from the output BFD to the import library BFD. */
11926 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11927 goto free_sym_buf;
11928
11929 /* Filter symbols to appear in the import library. */
11930 if (bed->elf_backend_filter_implib_symbols)
11931 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11932 symcount);
11933 else
11934 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11935 if (symcount == 0)
11936 {
11937 bfd_set_error (bfd_error_no_symbols);
11938 _bfd_error_handler (_("%pB: no symbol found for import library"),
11939 implib_bfd);
11940 goto free_sym_buf;
11941 }
11942
11943
11944 /* Make symbols absolute. */
11945 amt = symcount * sizeof (*osymbuf);
11946 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11947 if (osymbuf == NULL)
11948 goto free_sym_buf;
11949
11950 for (src_count = 0; src_count < symcount; src_count++)
11951 {
11952 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11953 sizeof (*osymbuf));
11954 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11955 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11956 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11957 osymbuf[src_count].internal_elf_sym.st_value =
11958 osymbuf[src_count].symbol.value;
11959 sympp[src_count] = &osymbuf[src_count].symbol;
11960 }
11961
11962 bfd_set_symtab (implib_bfd, sympp, symcount);
11963
11964 /* Allow the BFD backend to copy any private data it understands
11965 from the output BFD to the import library BFD. This is done last
11966 to permit the routine to look at the filtered symbol table. */
11967 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11968 goto free_sym_buf;
11969
11970 if (!bfd_close (implib_bfd))
11971 goto free_sym_buf;
11972
11973 ret = TRUE;
11974
11975 free_sym_buf:
11976 free (sympp);
11977 return ret;
11978 }
11979
11980 static void
11981 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11982 {
11983 asection *o;
11984
11985 if (flinfo->symstrtab != NULL)
11986 _bfd_elf_strtab_free (flinfo->symstrtab);
11987 free (flinfo->contents);
11988 free (flinfo->external_relocs);
11989 free (flinfo->internal_relocs);
11990 free (flinfo->external_syms);
11991 free (flinfo->locsym_shndx);
11992 free (flinfo->internal_syms);
11993 free (flinfo->indices);
11994 free (flinfo->sections);
11995 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11996 free (flinfo->symshndxbuf);
11997 for (o = obfd->sections; o != NULL; o = o->next)
11998 {
11999 struct bfd_elf_section_data *esdo = elf_section_data (o);
12000 free (esdo->rel.hashes);
12001 free (esdo->rela.hashes);
12002 }
12003 }
12004
12005 /* Do the final step of an ELF link. */
12006
12007 bfd_boolean
12008 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12009 {
12010 bfd_boolean dynamic;
12011 bfd_boolean emit_relocs;
12012 bfd *dynobj;
12013 struct elf_final_link_info flinfo;
12014 asection *o;
12015 struct bfd_link_order *p;
12016 bfd *sub;
12017 bfd_size_type max_contents_size;
12018 bfd_size_type max_external_reloc_size;
12019 bfd_size_type max_internal_reloc_count;
12020 bfd_size_type max_sym_count;
12021 bfd_size_type max_sym_shndx_count;
12022 Elf_Internal_Sym elfsym;
12023 unsigned int i;
12024 Elf_Internal_Shdr *symtab_hdr;
12025 Elf_Internal_Shdr *symtab_shndx_hdr;
12026 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12027 struct elf_outext_info eoinfo;
12028 bfd_boolean merged;
12029 size_t relativecount = 0;
12030 asection *reldyn = 0;
12031 bfd_size_type amt;
12032 asection *attr_section = NULL;
12033 bfd_vma attr_size = 0;
12034 const char *std_attrs_section;
12035 struct elf_link_hash_table *htab = elf_hash_table (info);
12036 bfd_boolean sections_removed;
12037 bfd_boolean ret;
12038
12039 if (!is_elf_hash_table (htab))
12040 return FALSE;
12041
12042 if (bfd_link_pic (info))
12043 abfd->flags |= DYNAMIC;
12044
12045 dynamic = htab->dynamic_sections_created;
12046 dynobj = htab->dynobj;
12047
12048 emit_relocs = (bfd_link_relocatable (info)
12049 || info->emitrelocations);
12050
12051 memset (&flinfo, 0, sizeof (flinfo));
12052 flinfo.info = info;
12053 flinfo.output_bfd = abfd;
12054 flinfo.symstrtab = _bfd_elf_strtab_init ();
12055 if (flinfo.symstrtab == NULL)
12056 return FALSE;
12057
12058 if (! dynamic)
12059 {
12060 flinfo.hash_sec = NULL;
12061 flinfo.symver_sec = NULL;
12062 }
12063 else
12064 {
12065 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12066 /* Note that dynsym_sec can be NULL (on VMS). */
12067 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12068 /* Note that it is OK if symver_sec is NULL. */
12069 }
12070
12071 if (info->unique_symbol
12072 && !bfd_hash_table_init (&flinfo.local_hash_table,
12073 local_hash_newfunc,
12074 sizeof (struct local_hash_entry)))
12075 return FALSE;
12076
12077 /* The object attributes have been merged. Remove the input
12078 sections from the link, and set the contents of the output
12079 section. */
12080 sections_removed = FALSE;
12081 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12082 for (o = abfd->sections; o != NULL; o = o->next)
12083 {
12084 bfd_boolean remove_section = FALSE;
12085
12086 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12087 || strcmp (o->name, ".gnu.attributes") == 0)
12088 {
12089 for (p = o->map_head.link_order; p != NULL; p = p->next)
12090 {
12091 asection *input_section;
12092
12093 if (p->type != bfd_indirect_link_order)
12094 continue;
12095 input_section = p->u.indirect.section;
12096 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12097 elf_link_input_bfd ignores this section. */
12098 input_section->flags &= ~SEC_HAS_CONTENTS;
12099 }
12100
12101 attr_size = bfd_elf_obj_attr_size (abfd);
12102 bfd_set_section_size (o, attr_size);
12103 /* Skip this section later on. */
12104 o->map_head.link_order = NULL;
12105 if (attr_size)
12106 attr_section = o;
12107 else
12108 remove_section = TRUE;
12109 }
12110 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12111 {
12112 /* Remove empty group section from linker output. */
12113 remove_section = TRUE;
12114 }
12115 if (remove_section)
12116 {
12117 o->flags |= SEC_EXCLUDE;
12118 bfd_section_list_remove (abfd, o);
12119 abfd->section_count--;
12120 sections_removed = TRUE;
12121 }
12122 }
12123 if (sections_removed)
12124 _bfd_fix_excluded_sec_syms (abfd, info);
12125
12126 /* Count up the number of relocations we will output for each output
12127 section, so that we know the sizes of the reloc sections. We
12128 also figure out some maximum sizes. */
12129 max_contents_size = 0;
12130 max_external_reloc_size = 0;
12131 max_internal_reloc_count = 0;
12132 max_sym_count = 0;
12133 max_sym_shndx_count = 0;
12134 merged = FALSE;
12135 for (o = abfd->sections; o != NULL; o = o->next)
12136 {
12137 struct bfd_elf_section_data *esdo = elf_section_data (o);
12138 o->reloc_count = 0;
12139
12140 for (p = o->map_head.link_order; p != NULL; p = p->next)
12141 {
12142 unsigned int reloc_count = 0;
12143 unsigned int additional_reloc_count = 0;
12144 struct bfd_elf_section_data *esdi = NULL;
12145
12146 if (p->type == bfd_section_reloc_link_order
12147 || p->type == bfd_symbol_reloc_link_order)
12148 reloc_count = 1;
12149 else if (p->type == bfd_indirect_link_order)
12150 {
12151 asection *sec;
12152
12153 sec = p->u.indirect.section;
12154
12155 /* Mark all sections which are to be included in the
12156 link. This will normally be every section. We need
12157 to do this so that we can identify any sections which
12158 the linker has decided to not include. */
12159 sec->linker_mark = TRUE;
12160
12161 if (sec->flags & SEC_MERGE)
12162 merged = TRUE;
12163
12164 if (sec->rawsize > max_contents_size)
12165 max_contents_size = sec->rawsize;
12166 if (sec->size > max_contents_size)
12167 max_contents_size = sec->size;
12168
12169 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12170 && (sec->owner->flags & DYNAMIC) == 0)
12171 {
12172 size_t sym_count;
12173
12174 /* We are interested in just local symbols, not all
12175 symbols. */
12176 if (elf_bad_symtab (sec->owner))
12177 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12178 / bed->s->sizeof_sym);
12179 else
12180 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12181
12182 if (sym_count > max_sym_count)
12183 max_sym_count = sym_count;
12184
12185 if (sym_count > max_sym_shndx_count
12186 && elf_symtab_shndx_list (sec->owner) != NULL)
12187 max_sym_shndx_count = sym_count;
12188
12189 if (esdo->this_hdr.sh_type == SHT_REL
12190 || esdo->this_hdr.sh_type == SHT_RELA)
12191 /* Some backends use reloc_count in relocation sections
12192 to count particular types of relocs. Of course,
12193 reloc sections themselves can't have relocations. */
12194 ;
12195 else if (emit_relocs)
12196 {
12197 reloc_count = sec->reloc_count;
12198 if (bed->elf_backend_count_additional_relocs)
12199 {
12200 int c;
12201 c = (*bed->elf_backend_count_additional_relocs) (sec);
12202 additional_reloc_count += c;
12203 }
12204 }
12205 else if (bed->elf_backend_count_relocs)
12206 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12207
12208 esdi = elf_section_data (sec);
12209
12210 if ((sec->flags & SEC_RELOC) != 0)
12211 {
12212 size_t ext_size = 0;
12213
12214 if (esdi->rel.hdr != NULL)
12215 ext_size = esdi->rel.hdr->sh_size;
12216 if (esdi->rela.hdr != NULL)
12217 ext_size += esdi->rela.hdr->sh_size;
12218
12219 if (ext_size > max_external_reloc_size)
12220 max_external_reloc_size = ext_size;
12221 if (sec->reloc_count > max_internal_reloc_count)
12222 max_internal_reloc_count = sec->reloc_count;
12223 }
12224 }
12225 }
12226
12227 if (reloc_count == 0)
12228 continue;
12229
12230 reloc_count += additional_reloc_count;
12231 o->reloc_count += reloc_count;
12232
12233 if (p->type == bfd_indirect_link_order && emit_relocs)
12234 {
12235 if (esdi->rel.hdr)
12236 {
12237 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12238 esdo->rel.count += additional_reloc_count;
12239 }
12240 if (esdi->rela.hdr)
12241 {
12242 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12243 esdo->rela.count += additional_reloc_count;
12244 }
12245 }
12246 else
12247 {
12248 if (o->use_rela_p)
12249 esdo->rela.count += reloc_count;
12250 else
12251 esdo->rel.count += reloc_count;
12252 }
12253 }
12254
12255 if (o->reloc_count > 0)
12256 o->flags |= SEC_RELOC;
12257 else
12258 {
12259 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12260 set it (this is probably a bug) and if it is set
12261 assign_section_numbers will create a reloc section. */
12262 o->flags &=~ SEC_RELOC;
12263 }
12264
12265 /* If the SEC_ALLOC flag is not set, force the section VMA to
12266 zero. This is done in elf_fake_sections as well, but forcing
12267 the VMA to 0 here will ensure that relocs against these
12268 sections are handled correctly. */
12269 if ((o->flags & SEC_ALLOC) == 0
12270 && ! o->user_set_vma)
12271 o->vma = 0;
12272 }
12273
12274 if (! bfd_link_relocatable (info) && merged)
12275 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12276
12277 /* Figure out the file positions for everything but the symbol table
12278 and the relocs. We set symcount to force assign_section_numbers
12279 to create a symbol table. */
12280 abfd->symcount = info->strip != strip_all || emit_relocs;
12281 BFD_ASSERT (! abfd->output_has_begun);
12282 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12283 goto error_return;
12284
12285 /* Set sizes, and assign file positions for reloc sections. */
12286 for (o = abfd->sections; o != NULL; o = o->next)
12287 {
12288 struct bfd_elf_section_data *esdo = elf_section_data (o);
12289 if ((o->flags & SEC_RELOC) != 0)
12290 {
12291 if (esdo->rel.hdr
12292 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12293 goto error_return;
12294
12295 if (esdo->rela.hdr
12296 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12297 goto error_return;
12298 }
12299
12300 /* _bfd_elf_compute_section_file_positions makes temporary use
12301 of target_index. Reset it. */
12302 o->target_index = 0;
12303
12304 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12305 to count upwards while actually outputting the relocations. */
12306 esdo->rel.count = 0;
12307 esdo->rela.count = 0;
12308
12309 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12310 && !bfd_section_is_ctf (o))
12311 {
12312 /* Cache the section contents so that they can be compressed
12313 later. Use bfd_malloc since it will be freed by
12314 bfd_compress_section_contents. */
12315 unsigned char *contents = esdo->this_hdr.contents;
12316 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12317 abort ();
12318 contents
12319 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12320 if (contents == NULL)
12321 goto error_return;
12322 esdo->this_hdr.contents = contents;
12323 }
12324 }
12325
12326 /* We have now assigned file positions for all the sections except .symtab,
12327 .strtab, and non-loaded reloc and compressed debugging sections. We start
12328 the .symtab section at the current file position, and write directly to it.
12329 We build the .strtab section in memory. */
12330 abfd->symcount = 0;
12331 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12332 /* sh_name is set in prep_headers. */
12333 symtab_hdr->sh_type = SHT_SYMTAB;
12334 /* sh_flags, sh_addr and sh_size all start off zero. */
12335 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12336 /* sh_link is set in assign_section_numbers. */
12337 /* sh_info is set below. */
12338 /* sh_offset is set just below. */
12339 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12340
12341 if (max_sym_count < 20)
12342 max_sym_count = 20;
12343 htab->strtabsize = max_sym_count;
12344 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12345 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12346 if (htab->strtab == NULL)
12347 goto error_return;
12348 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12349 flinfo.symshndxbuf
12350 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12351 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12352
12353 if (info->strip != strip_all || emit_relocs)
12354 {
12355 file_ptr off = elf_next_file_pos (abfd);
12356
12357 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12358
12359 /* Note that at this point elf_next_file_pos (abfd) is
12360 incorrect. We do not yet know the size of the .symtab section.
12361 We correct next_file_pos below, after we do know the size. */
12362
12363 /* Start writing out the symbol table. The first symbol is always a
12364 dummy symbol. */
12365 elfsym.st_value = 0;
12366 elfsym.st_size = 0;
12367 elfsym.st_info = 0;
12368 elfsym.st_other = 0;
12369 elfsym.st_shndx = SHN_UNDEF;
12370 elfsym.st_target_internal = 0;
12371 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12372 bfd_und_section_ptr, NULL) != 1)
12373 goto error_return;
12374
12375 /* Output a symbol for each section if asked or they are used for
12376 relocs. These symbols usually have no names. We store the
12377 index of each one in the index field of the section, so that
12378 we can find it again when outputting relocs. */
12379
12380 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12381 {
12382 bfd_boolean name_local_sections
12383 = (bed->elf_backend_name_local_section_symbols
12384 && bed->elf_backend_name_local_section_symbols (abfd));
12385 const char *name = NULL;
12386
12387 elfsym.st_size = 0;
12388 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12389 elfsym.st_other = 0;
12390 elfsym.st_value = 0;
12391 elfsym.st_target_internal = 0;
12392 for (i = 1; i < elf_numsections (abfd); i++)
12393 {
12394 o = bfd_section_from_elf_index (abfd, i);
12395 if (o != NULL)
12396 {
12397 o->target_index = bfd_get_symcount (abfd);
12398 elfsym.st_shndx = i;
12399 if (!bfd_link_relocatable (info))
12400 elfsym.st_value = o->vma;
12401 if (name_local_sections)
12402 name = o->name;
12403 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12404 NULL) != 1)
12405 goto error_return;
12406 }
12407 }
12408 }
12409 }
12410
12411 /* On some targets like Irix 5 the symbol split between local and global
12412 ones recorded in the sh_info field needs to be done between section
12413 and all other symbols. */
12414 if (bed->elf_backend_elfsym_local_is_section
12415 && bed->elf_backend_elfsym_local_is_section (abfd))
12416 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12417
12418 /* Allocate some memory to hold information read in from the input
12419 files. */
12420 if (max_contents_size != 0)
12421 {
12422 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12423 if (flinfo.contents == NULL)
12424 goto error_return;
12425 }
12426
12427 if (max_external_reloc_size != 0)
12428 {
12429 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12430 if (flinfo.external_relocs == NULL)
12431 goto error_return;
12432 }
12433
12434 if (max_internal_reloc_count != 0)
12435 {
12436 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12437 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12438 if (flinfo.internal_relocs == NULL)
12439 goto error_return;
12440 }
12441
12442 if (max_sym_count != 0)
12443 {
12444 amt = max_sym_count * bed->s->sizeof_sym;
12445 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12446 if (flinfo.external_syms == NULL)
12447 goto error_return;
12448
12449 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12450 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12451 if (flinfo.internal_syms == NULL)
12452 goto error_return;
12453
12454 amt = max_sym_count * sizeof (long);
12455 flinfo.indices = (long int *) bfd_malloc (amt);
12456 if (flinfo.indices == NULL)
12457 goto error_return;
12458
12459 amt = max_sym_count * sizeof (asection *);
12460 flinfo.sections = (asection **) bfd_malloc (amt);
12461 if (flinfo.sections == NULL)
12462 goto error_return;
12463 }
12464
12465 if (max_sym_shndx_count != 0)
12466 {
12467 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12468 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12469 if (flinfo.locsym_shndx == NULL)
12470 goto error_return;
12471 }
12472
12473 if (htab->tls_sec)
12474 {
12475 bfd_vma base, end = 0; /* Both bytes. */
12476 asection *sec;
12477
12478 for (sec = htab->tls_sec;
12479 sec && (sec->flags & SEC_THREAD_LOCAL);
12480 sec = sec->next)
12481 {
12482 bfd_size_type size = sec->size;
12483 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12484
12485 if (size == 0
12486 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12487 {
12488 struct bfd_link_order *ord = sec->map_tail.link_order;
12489
12490 if (ord != NULL)
12491 size = ord->offset * opb + ord->size;
12492 }
12493 end = sec->vma + size / opb;
12494 }
12495 base = htab->tls_sec->vma;
12496 /* Only align end of TLS section if static TLS doesn't have special
12497 alignment requirements. */
12498 if (bed->static_tls_alignment == 1)
12499 end = align_power (end, htab->tls_sec->alignment_power);
12500 htab->tls_size = end - base;
12501 }
12502
12503 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12504 return FALSE;
12505
12506 /* Since ELF permits relocations to be against local symbols, we
12507 must have the local symbols available when we do the relocations.
12508 Since we would rather only read the local symbols once, and we
12509 would rather not keep them in memory, we handle all the
12510 relocations for a single input file at the same time.
12511
12512 Unfortunately, there is no way to know the total number of local
12513 symbols until we have seen all of them, and the local symbol
12514 indices precede the global symbol indices. This means that when
12515 we are generating relocatable output, and we see a reloc against
12516 a global symbol, we can not know the symbol index until we have
12517 finished examining all the local symbols to see which ones we are
12518 going to output. To deal with this, we keep the relocations in
12519 memory, and don't output them until the end of the link. This is
12520 an unfortunate waste of memory, but I don't see a good way around
12521 it. Fortunately, it only happens when performing a relocatable
12522 link, which is not the common case. FIXME: If keep_memory is set
12523 we could write the relocs out and then read them again; I don't
12524 know how bad the memory loss will be. */
12525
12526 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12527 sub->output_has_begun = FALSE;
12528 for (o = abfd->sections; o != NULL; o = o->next)
12529 {
12530 for (p = o->map_head.link_order; p != NULL; p = p->next)
12531 {
12532 if (p->type == bfd_indirect_link_order
12533 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12534 == bfd_target_elf_flavour)
12535 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12536 {
12537 if (! sub->output_has_begun)
12538 {
12539 if (! elf_link_input_bfd (&flinfo, sub))
12540 goto error_return;
12541 sub->output_has_begun = TRUE;
12542 }
12543 }
12544 else if (p->type == bfd_section_reloc_link_order
12545 || p->type == bfd_symbol_reloc_link_order)
12546 {
12547 if (! elf_reloc_link_order (abfd, info, o, p))
12548 goto error_return;
12549 }
12550 else
12551 {
12552 if (! _bfd_default_link_order (abfd, info, o, p))
12553 {
12554 if (p->type == bfd_indirect_link_order
12555 && (bfd_get_flavour (sub)
12556 == bfd_target_elf_flavour)
12557 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12558 != bed->s->elfclass))
12559 {
12560 const char *iclass, *oclass;
12561
12562 switch (bed->s->elfclass)
12563 {
12564 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12565 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12566 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12567 default: abort ();
12568 }
12569
12570 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12571 {
12572 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12573 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12574 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12575 default: abort ();
12576 }
12577
12578 bfd_set_error (bfd_error_wrong_format);
12579 _bfd_error_handler
12580 /* xgettext:c-format */
12581 (_("%pB: file class %s incompatible with %s"),
12582 sub, iclass, oclass);
12583 }
12584
12585 goto error_return;
12586 }
12587 }
12588 }
12589 }
12590
12591 /* Free symbol buffer if needed. */
12592 if (!info->reduce_memory_overheads)
12593 {
12594 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12595 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12596 {
12597 free (elf_tdata (sub)->symbuf);
12598 elf_tdata (sub)->symbuf = NULL;
12599 }
12600 }
12601
12602 ret = TRUE;
12603
12604 /* Output any global symbols that got converted to local in a
12605 version script or due to symbol visibility. We do this in a
12606 separate step since ELF requires all local symbols to appear
12607 prior to any global symbols. FIXME: We should only do this if
12608 some global symbols were, in fact, converted to become local.
12609 FIXME: Will this work correctly with the Irix 5 linker? */
12610 eoinfo.failed = FALSE;
12611 eoinfo.flinfo = &flinfo;
12612 eoinfo.localsyms = TRUE;
12613 eoinfo.file_sym_done = FALSE;
12614 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12615 if (eoinfo.failed)
12616 {
12617 ret = FALSE;
12618 goto return_local_hash_table;
12619 }
12620
12621 /* If backend needs to output some local symbols not present in the hash
12622 table, do it now. */
12623 if (bed->elf_backend_output_arch_local_syms
12624 && (info->strip != strip_all || emit_relocs))
12625 {
12626 typedef int (*out_sym_func)
12627 (void *, const char *, Elf_Internal_Sym *, asection *,
12628 struct elf_link_hash_entry *);
12629
12630 if (! ((*bed->elf_backend_output_arch_local_syms)
12631 (abfd, info, &flinfo,
12632 (out_sym_func) elf_link_output_symstrtab)))
12633 {
12634 ret = FALSE;
12635 goto return_local_hash_table;
12636 }
12637 }
12638
12639 /* That wrote out all the local symbols. Finish up the symbol table
12640 with the global symbols. Even if we want to strip everything we
12641 can, we still need to deal with those global symbols that got
12642 converted to local in a version script. */
12643
12644 /* The sh_info field records the index of the first non local symbol. */
12645 if (!symtab_hdr->sh_info)
12646 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12647
12648 if (dynamic
12649 && htab->dynsym != NULL
12650 && htab->dynsym->output_section != bfd_abs_section_ptr)
12651 {
12652 Elf_Internal_Sym sym;
12653 bfd_byte *dynsym = htab->dynsym->contents;
12654
12655 o = htab->dynsym->output_section;
12656 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12657
12658 /* Write out the section symbols for the output sections. */
12659 if (bfd_link_pic (info)
12660 || htab->is_relocatable_executable)
12661 {
12662 asection *s;
12663
12664 sym.st_size = 0;
12665 sym.st_name = 0;
12666 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12667 sym.st_other = 0;
12668 sym.st_target_internal = 0;
12669
12670 for (s = abfd->sections; s != NULL; s = s->next)
12671 {
12672 int indx;
12673 bfd_byte *dest;
12674 long dynindx;
12675
12676 dynindx = elf_section_data (s)->dynindx;
12677 if (dynindx <= 0)
12678 continue;
12679 indx = elf_section_data (s)->this_idx;
12680 BFD_ASSERT (indx > 0);
12681 sym.st_shndx = indx;
12682 if (! check_dynsym (abfd, &sym))
12683 {
12684 ret = FALSE;
12685 goto return_local_hash_table;
12686 }
12687 sym.st_value = s->vma;
12688 dest = dynsym + dynindx * bed->s->sizeof_sym;
12689
12690 /* Inform the linker of the addition of this symbol. */
12691
12692 if (info->callbacks->ctf_new_dynsym)
12693 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12694
12695 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12696 }
12697 }
12698
12699 /* Write out the local dynsyms. */
12700 if (htab->dynlocal)
12701 {
12702 struct elf_link_local_dynamic_entry *e;
12703 for (e = htab->dynlocal; e ; e = e->next)
12704 {
12705 asection *s;
12706 bfd_byte *dest;
12707
12708 /* Copy the internal symbol and turn off visibility.
12709 Note that we saved a word of storage and overwrote
12710 the original st_name with the dynstr_index. */
12711 sym = e->isym;
12712 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12713 sym.st_shndx = SHN_UNDEF;
12714
12715 s = bfd_section_from_elf_index (e->input_bfd,
12716 e->isym.st_shndx);
12717 if (s != NULL
12718 && s->output_section != NULL
12719 && elf_section_data (s->output_section) != NULL)
12720 {
12721 sym.st_shndx =
12722 elf_section_data (s->output_section)->this_idx;
12723 if (! check_dynsym (abfd, &sym))
12724 {
12725 ret = FALSE;
12726 goto return_local_hash_table;
12727 }
12728 sym.st_value = (s->output_section->vma
12729 + s->output_offset
12730 + e->isym.st_value);
12731 }
12732
12733 /* Inform the linker of the addition of this symbol. */
12734
12735 if (info->callbacks->ctf_new_dynsym)
12736 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12737
12738 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12739 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12740 }
12741 }
12742 }
12743
12744 /* We get the global symbols from the hash table. */
12745 eoinfo.failed = FALSE;
12746 eoinfo.localsyms = FALSE;
12747 eoinfo.flinfo = &flinfo;
12748 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12749 if (eoinfo.failed)
12750 {
12751 ret = FALSE;
12752 goto return_local_hash_table;
12753 }
12754
12755 /* If backend needs to output some symbols not present in the hash
12756 table, do it now. */
12757 if (bed->elf_backend_output_arch_syms
12758 && (info->strip != strip_all || emit_relocs))
12759 {
12760 typedef int (*out_sym_func)
12761 (void *, const char *, Elf_Internal_Sym *, asection *,
12762 struct elf_link_hash_entry *);
12763
12764 if (! ((*bed->elf_backend_output_arch_syms)
12765 (abfd, info, &flinfo,
12766 (out_sym_func) elf_link_output_symstrtab)))
12767 {
12768 ret = FALSE;
12769 goto return_local_hash_table;
12770 }
12771 }
12772
12773 /* Finalize the .strtab section. */
12774 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12775
12776 /* Swap out the .strtab section. */
12777 if (!elf_link_swap_symbols_out (&flinfo))
12778 {
12779 ret = FALSE;
12780 goto return_local_hash_table;
12781 }
12782
12783 /* Now we know the size of the symtab section. */
12784 if (bfd_get_symcount (abfd) > 0)
12785 {
12786 /* Finish up and write out the symbol string table (.strtab)
12787 section. */
12788 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12789 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12790
12791 if (elf_symtab_shndx_list (abfd))
12792 {
12793 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12794
12795 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12796 {
12797 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12798 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12799 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12800 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12801 symtab_shndx_hdr->sh_size = amt;
12802
12803 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12804 off, TRUE);
12805
12806 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12807 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12808 {
12809 ret = FALSE;
12810 goto return_local_hash_table;
12811 }
12812 }
12813 }
12814
12815 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12816 /* sh_name was set in prep_headers. */
12817 symstrtab_hdr->sh_type = SHT_STRTAB;
12818 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12819 symstrtab_hdr->sh_addr = 0;
12820 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12821 symstrtab_hdr->sh_entsize = 0;
12822 symstrtab_hdr->sh_link = 0;
12823 symstrtab_hdr->sh_info = 0;
12824 /* sh_offset is set just below. */
12825 symstrtab_hdr->sh_addralign = 1;
12826
12827 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12828 off, TRUE);
12829 elf_next_file_pos (abfd) = off;
12830
12831 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12832 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12833 {
12834 ret = FALSE;
12835 goto return_local_hash_table;
12836 }
12837 }
12838
12839 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12840 {
12841 _bfd_error_handler (_("%pB: failed to generate import library"),
12842 info->out_implib_bfd);
12843 ret = FALSE;
12844 goto return_local_hash_table;
12845 }
12846
12847 /* Adjust the relocs to have the correct symbol indices. */
12848 for (o = abfd->sections; o != NULL; o = o->next)
12849 {
12850 struct bfd_elf_section_data *esdo = elf_section_data (o);
12851 bfd_boolean sort;
12852
12853 if ((o->flags & SEC_RELOC) == 0)
12854 continue;
12855
12856 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12857 if (esdo->rel.hdr != NULL
12858 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12859 {
12860 ret = FALSE;
12861 goto return_local_hash_table;
12862 }
12863 if (esdo->rela.hdr != NULL
12864 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12865 {
12866 ret = FALSE;
12867 goto return_local_hash_table;
12868 }
12869
12870 /* Set the reloc_count field to 0 to prevent write_relocs from
12871 trying to swap the relocs out itself. */
12872 o->reloc_count = 0;
12873 }
12874
12875 if (dynamic && info->combreloc && dynobj != NULL)
12876 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12877
12878 /* If we are linking against a dynamic object, or generating a
12879 shared library, finish up the dynamic linking information. */
12880 if (dynamic)
12881 {
12882 bfd_byte *dyncon, *dynconend;
12883
12884 /* Fix up .dynamic entries. */
12885 o = bfd_get_linker_section (dynobj, ".dynamic");
12886 BFD_ASSERT (o != NULL);
12887
12888 dyncon = o->contents;
12889 dynconend = o->contents + o->size;
12890 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12891 {
12892 Elf_Internal_Dyn dyn;
12893 const char *name;
12894 unsigned int type;
12895 bfd_size_type sh_size;
12896 bfd_vma sh_addr;
12897
12898 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12899
12900 switch (dyn.d_tag)
12901 {
12902 default:
12903 continue;
12904 case DT_NULL:
12905 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12906 {
12907 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12908 {
12909 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12910 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12911 default: continue;
12912 }
12913 dyn.d_un.d_val = relativecount;
12914 relativecount = 0;
12915 break;
12916 }
12917 continue;
12918
12919 case DT_INIT:
12920 name = info->init_function;
12921 goto get_sym;
12922 case DT_FINI:
12923 name = info->fini_function;
12924 get_sym:
12925 {
12926 struct elf_link_hash_entry *h;
12927
12928 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12929 if (h != NULL
12930 && (h->root.type == bfd_link_hash_defined
12931 || h->root.type == bfd_link_hash_defweak))
12932 {
12933 dyn.d_un.d_ptr = h->root.u.def.value;
12934 o = h->root.u.def.section;
12935 if (o->output_section != NULL)
12936 dyn.d_un.d_ptr += (o->output_section->vma
12937 + o->output_offset);
12938 else
12939 {
12940 /* The symbol is imported from another shared
12941 library and does not apply to this one. */
12942 dyn.d_un.d_ptr = 0;
12943 }
12944 break;
12945 }
12946 }
12947 continue;
12948
12949 case DT_PREINIT_ARRAYSZ:
12950 name = ".preinit_array";
12951 goto get_out_size;
12952 case DT_INIT_ARRAYSZ:
12953 name = ".init_array";
12954 goto get_out_size;
12955 case DT_FINI_ARRAYSZ:
12956 name = ".fini_array";
12957 get_out_size:
12958 o = bfd_get_section_by_name (abfd, name);
12959 if (o == NULL)
12960 {
12961 _bfd_error_handler
12962 (_("could not find section %s"), name);
12963 goto error_return;
12964 }
12965 if (o->size == 0)
12966 _bfd_error_handler
12967 (_("warning: %s section has zero size"), name);
12968 dyn.d_un.d_val = o->size;
12969 break;
12970
12971 case DT_PREINIT_ARRAY:
12972 name = ".preinit_array";
12973 goto get_out_vma;
12974 case DT_INIT_ARRAY:
12975 name = ".init_array";
12976 goto get_out_vma;
12977 case DT_FINI_ARRAY:
12978 name = ".fini_array";
12979 get_out_vma:
12980 o = bfd_get_section_by_name (abfd, name);
12981 goto do_vma;
12982
12983 case DT_HASH:
12984 name = ".hash";
12985 goto get_vma;
12986 case DT_GNU_HASH:
12987 name = ".gnu.hash";
12988 goto get_vma;
12989 case DT_STRTAB:
12990 name = ".dynstr";
12991 goto get_vma;
12992 case DT_SYMTAB:
12993 name = ".dynsym";
12994 goto get_vma;
12995 case DT_VERDEF:
12996 name = ".gnu.version_d";
12997 goto get_vma;
12998 case DT_VERNEED:
12999 name = ".gnu.version_r";
13000 goto get_vma;
13001 case DT_VERSYM:
13002 name = ".gnu.version";
13003 get_vma:
13004 o = bfd_get_linker_section (dynobj, name);
13005 do_vma:
13006 if (o == NULL || bfd_is_abs_section (o->output_section))
13007 {
13008 _bfd_error_handler
13009 (_("could not find section %s"), name);
13010 goto error_return;
13011 }
13012 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13013 {
13014 _bfd_error_handler
13015 (_("warning: section '%s' is being made into a note"), name);
13016 bfd_set_error (bfd_error_nonrepresentable_section);
13017 goto error_return;
13018 }
13019 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13020 break;
13021
13022 case DT_REL:
13023 case DT_RELA:
13024 case DT_RELSZ:
13025 case DT_RELASZ:
13026 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13027 type = SHT_REL;
13028 else
13029 type = SHT_RELA;
13030 sh_size = 0;
13031 sh_addr = 0;
13032 for (i = 1; i < elf_numsections (abfd); i++)
13033 {
13034 Elf_Internal_Shdr *hdr;
13035
13036 hdr = elf_elfsections (abfd)[i];
13037 if (hdr->sh_type == type
13038 && (hdr->sh_flags & SHF_ALLOC) != 0)
13039 {
13040 sh_size += hdr->sh_size;
13041 if (sh_addr == 0
13042 || sh_addr > hdr->sh_addr)
13043 sh_addr = hdr->sh_addr;
13044 }
13045 }
13046
13047 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13048 {
13049 unsigned int opb = bfd_octets_per_byte (abfd, o);
13050
13051 /* Don't count procedure linkage table relocs in the
13052 overall reloc count. */
13053 sh_size -= htab->srelplt->size;
13054 if (sh_size == 0)
13055 /* If the size is zero, make the address zero too.
13056 This is to avoid a glibc bug. If the backend
13057 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13058 zero, then we'll put DT_RELA at the end of
13059 DT_JMPREL. glibc will interpret the end of
13060 DT_RELA matching the end of DT_JMPREL as the
13061 case where DT_RELA includes DT_JMPREL, and for
13062 LD_BIND_NOW will decide that processing DT_RELA
13063 will process the PLT relocs too. Net result:
13064 No PLT relocs applied. */
13065 sh_addr = 0;
13066
13067 /* If .rela.plt is the first .rela section, exclude
13068 it from DT_RELA. */
13069 else if (sh_addr == (htab->srelplt->output_section->vma
13070 + htab->srelplt->output_offset) * opb)
13071 sh_addr += htab->srelplt->size;
13072 }
13073
13074 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13075 dyn.d_un.d_val = sh_size;
13076 else
13077 dyn.d_un.d_ptr = sh_addr;
13078 break;
13079 }
13080 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13081 }
13082 }
13083
13084 /* If we have created any dynamic sections, then output them. */
13085 if (dynobj != NULL)
13086 {
13087 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13088 goto error_return;
13089
13090 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13091 if (bfd_link_textrel_check (info)
13092 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13093 {
13094 bfd_byte *dyncon, *dynconend;
13095
13096 dyncon = o->contents;
13097 dynconend = o->contents + o->size;
13098 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13099 {
13100 Elf_Internal_Dyn dyn;
13101
13102 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13103
13104 if (dyn.d_tag == DT_TEXTREL)
13105 {
13106 if (info->textrel_check == textrel_check_error)
13107 info->callbacks->einfo
13108 (_("%P%X: read-only segment has dynamic relocations\n"));
13109 else if (bfd_link_dll (info))
13110 info->callbacks->einfo
13111 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13112 else
13113 info->callbacks->einfo
13114 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13115 break;
13116 }
13117 }
13118 }
13119
13120 for (o = dynobj->sections; o != NULL; o = o->next)
13121 {
13122 if ((o->flags & SEC_HAS_CONTENTS) == 0
13123 || o->size == 0
13124 || o->output_section == bfd_abs_section_ptr)
13125 continue;
13126 if ((o->flags & SEC_LINKER_CREATED) == 0)
13127 {
13128 /* At this point, we are only interested in sections
13129 created by _bfd_elf_link_create_dynamic_sections. */
13130 continue;
13131 }
13132 if (htab->stab_info.stabstr == o)
13133 continue;
13134 if (htab->eh_info.hdr_sec == o)
13135 continue;
13136 if (strcmp (o->name, ".dynstr") != 0)
13137 {
13138 bfd_size_type octets = ((file_ptr) o->output_offset
13139 * bfd_octets_per_byte (abfd, o));
13140 if (!bfd_set_section_contents (abfd, o->output_section,
13141 o->contents, octets, o->size))
13142 goto error_return;
13143 }
13144 else
13145 {
13146 /* The contents of the .dynstr section are actually in a
13147 stringtab. */
13148 file_ptr off;
13149
13150 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13151 if (bfd_seek (abfd, off, SEEK_SET) != 0
13152 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13153 goto error_return;
13154 }
13155 }
13156 }
13157
13158 if (!info->resolve_section_groups)
13159 {
13160 bfd_boolean failed = FALSE;
13161
13162 BFD_ASSERT (bfd_link_relocatable (info));
13163 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13164 if (failed)
13165 goto error_return;
13166 }
13167
13168 /* If we have optimized stabs strings, output them. */
13169 if (htab->stab_info.stabstr != NULL)
13170 {
13171 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13172 goto error_return;
13173 }
13174
13175 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13176 goto error_return;
13177
13178 if (info->callbacks->emit_ctf)
13179 info->callbacks->emit_ctf ();
13180
13181 elf_final_link_free (abfd, &flinfo);
13182
13183 if (attr_section)
13184 {
13185 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13186 if (contents == NULL)
13187 {
13188 /* Bail out and fail. */
13189 ret = FALSE;
13190 goto return_local_hash_table;
13191 }
13192 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13193 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13194 free (contents);
13195 }
13196
13197 return_local_hash_table:
13198 if (info->unique_symbol)
13199 bfd_hash_table_free (&flinfo.local_hash_table);
13200 return ret;
13201
13202 error_return:
13203 elf_final_link_free (abfd, &flinfo);
13204 ret = FALSE;
13205 goto return_local_hash_table;
13206 }
13207 \f
13208 /* Initialize COOKIE for input bfd ABFD. */
13209
13210 static bfd_boolean
13211 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13212 struct bfd_link_info *info, bfd *abfd)
13213 {
13214 Elf_Internal_Shdr *symtab_hdr;
13215 const struct elf_backend_data *bed;
13216
13217 bed = get_elf_backend_data (abfd);
13218 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13219
13220 cookie->abfd = abfd;
13221 cookie->sym_hashes = elf_sym_hashes (abfd);
13222 cookie->bad_symtab = elf_bad_symtab (abfd);
13223 if (cookie->bad_symtab)
13224 {
13225 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13226 cookie->extsymoff = 0;
13227 }
13228 else
13229 {
13230 cookie->locsymcount = symtab_hdr->sh_info;
13231 cookie->extsymoff = symtab_hdr->sh_info;
13232 }
13233
13234 if (bed->s->arch_size == 32)
13235 cookie->r_sym_shift = 8;
13236 else
13237 cookie->r_sym_shift = 32;
13238
13239 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13240 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13241 {
13242 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13243 cookie->locsymcount, 0,
13244 NULL, NULL, NULL);
13245 if (cookie->locsyms == NULL)
13246 {
13247 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13248 return FALSE;
13249 }
13250 if (info->keep_memory)
13251 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13252 }
13253 return TRUE;
13254 }
13255
13256 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13257
13258 static void
13259 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13260 {
13261 Elf_Internal_Shdr *symtab_hdr;
13262
13263 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13264 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13265 free (cookie->locsyms);
13266 }
13267
13268 /* Initialize the relocation information in COOKIE for input section SEC
13269 of input bfd ABFD. */
13270
13271 static bfd_boolean
13272 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13273 struct bfd_link_info *info, bfd *abfd,
13274 asection *sec)
13275 {
13276 if (sec->reloc_count == 0)
13277 {
13278 cookie->rels = NULL;
13279 cookie->relend = NULL;
13280 }
13281 else
13282 {
13283 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13284 info->keep_memory);
13285 if (cookie->rels == NULL)
13286 return FALSE;
13287 cookie->rel = cookie->rels;
13288 cookie->relend = cookie->rels + sec->reloc_count;
13289 }
13290 cookie->rel = cookie->rels;
13291 return TRUE;
13292 }
13293
13294 /* Free the memory allocated by init_reloc_cookie_rels,
13295 if appropriate. */
13296
13297 static void
13298 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13299 asection *sec)
13300 {
13301 if (elf_section_data (sec)->relocs != cookie->rels)
13302 free (cookie->rels);
13303 }
13304
13305 /* Initialize the whole of COOKIE for input section SEC. */
13306
13307 static bfd_boolean
13308 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13309 struct bfd_link_info *info,
13310 asection *sec)
13311 {
13312 if (!init_reloc_cookie (cookie, info, sec->owner))
13313 goto error1;
13314 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13315 goto error2;
13316 return TRUE;
13317
13318 error2:
13319 fini_reloc_cookie (cookie, sec->owner);
13320 error1:
13321 return FALSE;
13322 }
13323
13324 /* Free the memory allocated by init_reloc_cookie_for_section,
13325 if appropriate. */
13326
13327 static void
13328 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13329 asection *sec)
13330 {
13331 fini_reloc_cookie_rels (cookie, sec);
13332 fini_reloc_cookie (cookie, sec->owner);
13333 }
13334 \f
13335 /* Garbage collect unused sections. */
13336
13337 /* Default gc_mark_hook. */
13338
13339 asection *
13340 _bfd_elf_gc_mark_hook (asection *sec,
13341 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13342 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13343 struct elf_link_hash_entry *h,
13344 Elf_Internal_Sym *sym)
13345 {
13346 if (h != NULL)
13347 {
13348 switch (h->root.type)
13349 {
13350 case bfd_link_hash_defined:
13351 case bfd_link_hash_defweak:
13352 return h->root.u.def.section;
13353
13354 case bfd_link_hash_common:
13355 return h->root.u.c.p->section;
13356
13357 default:
13358 break;
13359 }
13360 }
13361 else
13362 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13363
13364 return NULL;
13365 }
13366
13367 /* Return the debug definition section. */
13368
13369 static asection *
13370 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13371 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13372 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13373 struct elf_link_hash_entry *h,
13374 Elf_Internal_Sym *sym)
13375 {
13376 if (h != NULL)
13377 {
13378 /* Return the global debug definition section. */
13379 if ((h->root.type == bfd_link_hash_defined
13380 || h->root.type == bfd_link_hash_defweak)
13381 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13382 return h->root.u.def.section;
13383 }
13384 else
13385 {
13386 /* Return the local debug definition section. */
13387 asection *isec = bfd_section_from_elf_index (sec->owner,
13388 sym->st_shndx);
13389 if ((isec->flags & SEC_DEBUGGING) != 0)
13390 return isec;
13391 }
13392
13393 return NULL;
13394 }
13395
13396 /* COOKIE->rel describes a relocation against section SEC, which is
13397 a section we've decided to keep. Return the section that contains
13398 the relocation symbol, or NULL if no section contains it. */
13399
13400 asection *
13401 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13402 elf_gc_mark_hook_fn gc_mark_hook,
13403 struct elf_reloc_cookie *cookie,
13404 bfd_boolean *start_stop)
13405 {
13406 unsigned long r_symndx;
13407 struct elf_link_hash_entry *h, *hw;
13408
13409 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13410 if (r_symndx == STN_UNDEF)
13411 return NULL;
13412
13413 if (r_symndx >= cookie->locsymcount
13414 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13415 {
13416 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13417 if (h == NULL)
13418 {
13419 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13420 sec->owner);
13421 return NULL;
13422 }
13423 while (h->root.type == bfd_link_hash_indirect
13424 || h->root.type == bfd_link_hash_warning)
13425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13426 h->mark = 1;
13427 /* Keep all aliases of the symbol too. If an object symbol
13428 needs to be copied into .dynbss then all of its aliases
13429 should be present as dynamic symbols, not just the one used
13430 on the copy relocation. */
13431 hw = h;
13432 while (hw->is_weakalias)
13433 {
13434 hw = hw->u.alias;
13435 hw->mark = 1;
13436 }
13437
13438 if (start_stop != NULL)
13439 {
13440 /* To work around a glibc bug, mark XXX input sections
13441 when there is a reference to __start_XXX or __stop_XXX
13442 symbols. */
13443 if (h->start_stop)
13444 {
13445 asection *s = h->u2.start_stop_section;
13446 *start_stop = !s->gc_mark;
13447 return s;
13448 }
13449 }
13450
13451 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13452 }
13453
13454 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13455 &cookie->locsyms[r_symndx]);
13456 }
13457
13458 /* COOKIE->rel describes a relocation against section SEC, which is
13459 a section we've decided to keep. Mark the section that contains
13460 the relocation symbol. */
13461
13462 bfd_boolean
13463 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13464 asection *sec,
13465 elf_gc_mark_hook_fn gc_mark_hook,
13466 struct elf_reloc_cookie *cookie)
13467 {
13468 asection *rsec;
13469 bfd_boolean start_stop = FALSE;
13470
13471 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13472 while (rsec != NULL)
13473 {
13474 if (!rsec->gc_mark)
13475 {
13476 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13477 || (rsec->owner->flags & DYNAMIC) != 0)
13478 rsec->gc_mark = 1;
13479 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13480 return FALSE;
13481 }
13482 if (!start_stop)
13483 break;
13484 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13485 }
13486 return TRUE;
13487 }
13488
13489 /* The mark phase of garbage collection. For a given section, mark
13490 it and any sections in this section's group, and all the sections
13491 which define symbols to which it refers. */
13492
13493 bfd_boolean
13494 _bfd_elf_gc_mark (struct bfd_link_info *info,
13495 asection *sec,
13496 elf_gc_mark_hook_fn gc_mark_hook)
13497 {
13498 bfd_boolean ret;
13499 asection *group_sec, *eh_frame;
13500
13501 sec->gc_mark = 1;
13502
13503 /* Mark all the sections in the group. */
13504 group_sec = elf_section_data (sec)->next_in_group;
13505 if (group_sec && !group_sec->gc_mark)
13506 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13507 return FALSE;
13508
13509 /* Look through the section relocs. */
13510 ret = TRUE;
13511 eh_frame = elf_eh_frame_section (sec->owner);
13512 if ((sec->flags & SEC_RELOC) != 0
13513 && sec->reloc_count > 0
13514 && sec != eh_frame)
13515 {
13516 struct elf_reloc_cookie cookie;
13517
13518 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13519 ret = FALSE;
13520 else
13521 {
13522 for (; cookie.rel < cookie.relend; cookie.rel++)
13523 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13524 {
13525 ret = FALSE;
13526 break;
13527 }
13528 fini_reloc_cookie_for_section (&cookie, sec);
13529 }
13530 }
13531
13532 if (ret && eh_frame && elf_fde_list (sec))
13533 {
13534 struct elf_reloc_cookie cookie;
13535
13536 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13537 ret = FALSE;
13538 else
13539 {
13540 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13541 gc_mark_hook, &cookie))
13542 ret = FALSE;
13543 fini_reloc_cookie_for_section (&cookie, eh_frame);
13544 }
13545 }
13546
13547 eh_frame = elf_section_eh_frame_entry (sec);
13548 if (ret && eh_frame && !eh_frame->gc_mark)
13549 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13550 ret = FALSE;
13551
13552 return ret;
13553 }
13554
13555 /* Scan and mark sections in a special or debug section group. */
13556
13557 static void
13558 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13559 {
13560 /* Point to first section of section group. */
13561 asection *ssec;
13562 /* Used to iterate the section group. */
13563 asection *msec;
13564
13565 bfd_boolean is_special_grp = TRUE;
13566 bfd_boolean is_debug_grp = TRUE;
13567
13568 /* First scan to see if group contains any section other than debug
13569 and special section. */
13570 ssec = msec = elf_next_in_group (grp);
13571 do
13572 {
13573 if ((msec->flags & SEC_DEBUGGING) == 0)
13574 is_debug_grp = FALSE;
13575
13576 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13577 is_special_grp = FALSE;
13578
13579 msec = elf_next_in_group (msec);
13580 }
13581 while (msec != ssec);
13582
13583 /* If this is a pure debug section group or pure special section group,
13584 keep all sections in this group. */
13585 if (is_debug_grp || is_special_grp)
13586 {
13587 do
13588 {
13589 msec->gc_mark = 1;
13590 msec = elf_next_in_group (msec);
13591 }
13592 while (msec != ssec);
13593 }
13594 }
13595
13596 /* Keep debug and special sections. */
13597
13598 bfd_boolean
13599 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13600 elf_gc_mark_hook_fn mark_hook)
13601 {
13602 bfd *ibfd;
13603
13604 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13605 {
13606 asection *isec;
13607 bfd_boolean some_kept;
13608 bfd_boolean debug_frag_seen;
13609 bfd_boolean has_kept_debug_info;
13610
13611 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13612 continue;
13613 isec = ibfd->sections;
13614 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13615 continue;
13616
13617 /* Ensure all linker created sections are kept,
13618 see if any other section is already marked,
13619 and note if we have any fragmented debug sections. */
13620 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13621 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13622 {
13623 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13624 isec->gc_mark = 1;
13625 else if (isec->gc_mark
13626 && (isec->flags & SEC_ALLOC) != 0
13627 && elf_section_type (isec) != SHT_NOTE)
13628 some_kept = TRUE;
13629 else
13630 {
13631 /* Since all sections, except for backend specific ones,
13632 have been garbage collected, call mark_hook on this
13633 section if any of its linked-to sections is marked. */
13634 asection *linked_to_sec = elf_linked_to_section (isec);
13635 for (; linked_to_sec != NULL;
13636 linked_to_sec = elf_linked_to_section (linked_to_sec))
13637 if (linked_to_sec->gc_mark)
13638 {
13639 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13640 return FALSE;
13641 break;
13642 }
13643 }
13644
13645 if (!debug_frag_seen
13646 && (isec->flags & SEC_DEBUGGING)
13647 && CONST_STRNEQ (isec->name, ".debug_line."))
13648 debug_frag_seen = TRUE;
13649 else if (strcmp (bfd_section_name (isec),
13650 "__patchable_function_entries") == 0
13651 && elf_linked_to_section (isec) == NULL)
13652 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13653 "need linked-to section "
13654 "for --gc-sections\n"),
13655 isec->owner, isec);
13656 }
13657
13658 /* If no non-note alloc section in this file will be kept, then
13659 we can toss out the debug and special sections. */
13660 if (!some_kept)
13661 continue;
13662
13663 /* Keep debug and special sections like .comment when they are
13664 not part of a group. Also keep section groups that contain
13665 just debug sections or special sections. NB: Sections with
13666 linked-to section has been handled above. */
13667 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13668 {
13669 if ((isec->flags & SEC_GROUP) != 0)
13670 _bfd_elf_gc_mark_debug_special_section_group (isec);
13671 else if (((isec->flags & SEC_DEBUGGING) != 0
13672 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13673 && elf_next_in_group (isec) == NULL
13674 && elf_linked_to_section (isec) == NULL)
13675 isec->gc_mark = 1;
13676 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13677 has_kept_debug_info = TRUE;
13678 }
13679
13680 /* Look for CODE sections which are going to be discarded,
13681 and find and discard any fragmented debug sections which
13682 are associated with that code section. */
13683 if (debug_frag_seen)
13684 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13685 if ((isec->flags & SEC_CODE) != 0
13686 && isec->gc_mark == 0)
13687 {
13688 unsigned int ilen;
13689 asection *dsec;
13690
13691 ilen = strlen (isec->name);
13692
13693 /* Association is determined by the name of the debug
13694 section containing the name of the code section as
13695 a suffix. For example .debug_line.text.foo is a
13696 debug section associated with .text.foo. */
13697 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13698 {
13699 unsigned int dlen;
13700
13701 if (dsec->gc_mark == 0
13702 || (dsec->flags & SEC_DEBUGGING) == 0)
13703 continue;
13704
13705 dlen = strlen (dsec->name);
13706
13707 if (dlen > ilen
13708 && strncmp (dsec->name + (dlen - ilen),
13709 isec->name, ilen) == 0)
13710 dsec->gc_mark = 0;
13711 }
13712 }
13713
13714 /* Mark debug sections referenced by kept debug sections. */
13715 if (has_kept_debug_info)
13716 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13717 if (isec->gc_mark
13718 && (isec->flags & SEC_DEBUGGING) != 0)
13719 if (!_bfd_elf_gc_mark (info, isec,
13720 elf_gc_mark_debug_section))
13721 return FALSE;
13722 }
13723 return TRUE;
13724 }
13725
13726 static bfd_boolean
13727 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13728 {
13729 bfd *sub;
13730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13731
13732 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13733 {
13734 asection *o;
13735
13736 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13737 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13738 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13739 continue;
13740 o = sub->sections;
13741 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13742 continue;
13743
13744 for (o = sub->sections; o != NULL; o = o->next)
13745 {
13746 /* When any section in a section group is kept, we keep all
13747 sections in the section group. If the first member of
13748 the section group is excluded, we will also exclude the
13749 group section. */
13750 if (o->flags & SEC_GROUP)
13751 {
13752 asection *first = elf_next_in_group (o);
13753 o->gc_mark = first->gc_mark;
13754 }
13755
13756 if (o->gc_mark)
13757 continue;
13758
13759 /* Skip sweeping sections already excluded. */
13760 if (o->flags & SEC_EXCLUDE)
13761 continue;
13762
13763 /* Since this is early in the link process, it is simple
13764 to remove a section from the output. */
13765 o->flags |= SEC_EXCLUDE;
13766
13767 if (info->print_gc_sections && o->size != 0)
13768 /* xgettext:c-format */
13769 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13770 o, sub);
13771 }
13772 }
13773
13774 return TRUE;
13775 }
13776
13777 /* Propagate collected vtable information. This is called through
13778 elf_link_hash_traverse. */
13779
13780 static bfd_boolean
13781 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13782 {
13783 /* Those that are not vtables. */
13784 if (h->start_stop
13785 || h->u2.vtable == NULL
13786 || h->u2.vtable->parent == NULL)
13787 return TRUE;
13788
13789 /* Those vtables that do not have parents, we cannot merge. */
13790 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13791 return TRUE;
13792
13793 /* If we've already been done, exit. */
13794 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13795 return TRUE;
13796
13797 /* Make sure the parent's table is up to date. */
13798 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13799
13800 if (h->u2.vtable->used == NULL)
13801 {
13802 /* None of this table's entries were referenced. Re-use the
13803 parent's table. */
13804 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13805 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13806 }
13807 else
13808 {
13809 size_t n;
13810 bfd_boolean *cu, *pu;
13811
13812 /* Or the parent's entries into ours. */
13813 cu = h->u2.vtable->used;
13814 cu[-1] = TRUE;
13815 pu = h->u2.vtable->parent->u2.vtable->used;
13816 if (pu != NULL)
13817 {
13818 const struct elf_backend_data *bed;
13819 unsigned int log_file_align;
13820
13821 bed = get_elf_backend_data (h->root.u.def.section->owner);
13822 log_file_align = bed->s->log_file_align;
13823 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13824 while (n--)
13825 {
13826 if (*pu)
13827 *cu = TRUE;
13828 pu++;
13829 cu++;
13830 }
13831 }
13832 }
13833
13834 return TRUE;
13835 }
13836
13837 static bfd_boolean
13838 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13839 {
13840 asection *sec;
13841 bfd_vma hstart, hend;
13842 Elf_Internal_Rela *relstart, *relend, *rel;
13843 const struct elf_backend_data *bed;
13844 unsigned int log_file_align;
13845
13846 /* Take care of both those symbols that do not describe vtables as
13847 well as those that are not loaded. */
13848 if (h->start_stop
13849 || h->u2.vtable == NULL
13850 || h->u2.vtable->parent == NULL)
13851 return TRUE;
13852
13853 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13854 || h->root.type == bfd_link_hash_defweak);
13855
13856 sec = h->root.u.def.section;
13857 hstart = h->root.u.def.value;
13858 hend = hstart + h->size;
13859
13860 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13861 if (!relstart)
13862 return *(bfd_boolean *) okp = FALSE;
13863 bed = get_elf_backend_data (sec->owner);
13864 log_file_align = bed->s->log_file_align;
13865
13866 relend = relstart + sec->reloc_count;
13867
13868 for (rel = relstart; rel < relend; ++rel)
13869 if (rel->r_offset >= hstart && rel->r_offset < hend)
13870 {
13871 /* If the entry is in use, do nothing. */
13872 if (h->u2.vtable->used
13873 && (rel->r_offset - hstart) < h->u2.vtable->size)
13874 {
13875 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13876 if (h->u2.vtable->used[entry])
13877 continue;
13878 }
13879 /* Otherwise, kill it. */
13880 rel->r_offset = rel->r_info = rel->r_addend = 0;
13881 }
13882
13883 return TRUE;
13884 }
13885
13886 /* Mark sections containing dynamically referenced symbols. When
13887 building shared libraries, we must assume that any visible symbol is
13888 referenced. */
13889
13890 bfd_boolean
13891 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13892 {
13893 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13894 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13895
13896 if ((h->root.type == bfd_link_hash_defined
13897 || h->root.type == bfd_link_hash_defweak)
13898 && ((h->ref_dynamic && !h->forced_local)
13899 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13900 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13901 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13902 && (!bfd_link_executable (info)
13903 || info->gc_keep_exported
13904 || info->export_dynamic
13905 || (h->dynamic
13906 && d != NULL
13907 && (*d->match) (&d->head, NULL, h->root.root.string)))
13908 && (h->versioned >= versioned
13909 || !bfd_hide_sym_by_version (info->version_info,
13910 h->root.root.string)))))
13911 h->root.u.def.section->flags |= SEC_KEEP;
13912
13913 return TRUE;
13914 }
13915
13916 /* Keep all sections containing symbols undefined on the command-line,
13917 and the section containing the entry symbol. */
13918
13919 void
13920 _bfd_elf_gc_keep (struct bfd_link_info *info)
13921 {
13922 struct bfd_sym_chain *sym;
13923
13924 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13925 {
13926 struct elf_link_hash_entry *h;
13927
13928 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13929 FALSE, FALSE, FALSE);
13930
13931 if (h != NULL
13932 && (h->root.type == bfd_link_hash_defined
13933 || h->root.type == bfd_link_hash_defweak)
13934 && !bfd_is_const_section (h->root.u.def.section))
13935 h->root.u.def.section->flags |= SEC_KEEP;
13936 }
13937 }
13938
13939 bfd_boolean
13940 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13941 struct bfd_link_info *info)
13942 {
13943 bfd *ibfd = info->input_bfds;
13944
13945 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13946 {
13947 asection *sec;
13948 struct elf_reloc_cookie cookie;
13949
13950 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13951 continue;
13952 sec = ibfd->sections;
13953 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13954 continue;
13955
13956 if (!init_reloc_cookie (&cookie, info, ibfd))
13957 return FALSE;
13958
13959 for (sec = ibfd->sections; sec; sec = sec->next)
13960 {
13961 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13962 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13963 {
13964 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13965 fini_reloc_cookie_rels (&cookie, sec);
13966 }
13967 }
13968 }
13969 return TRUE;
13970 }
13971
13972 /* Do mark and sweep of unused sections. */
13973
13974 bfd_boolean
13975 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13976 {
13977 bfd_boolean ok = TRUE;
13978 bfd *sub;
13979 elf_gc_mark_hook_fn gc_mark_hook;
13980 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13981 struct elf_link_hash_table *htab;
13982
13983 if (!bed->can_gc_sections
13984 || !is_elf_hash_table (info->hash))
13985 {
13986 _bfd_error_handler(_("warning: gc-sections option ignored"));
13987 return TRUE;
13988 }
13989
13990 bed->gc_keep (info);
13991 htab = elf_hash_table (info);
13992
13993 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13994 at the .eh_frame section if we can mark the FDEs individually. */
13995 for (sub = info->input_bfds;
13996 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13997 sub = sub->link.next)
13998 {
13999 asection *sec;
14000 struct elf_reloc_cookie cookie;
14001
14002 sec = sub->sections;
14003 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14004 continue;
14005 sec = bfd_get_section_by_name (sub, ".eh_frame");
14006 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14007 {
14008 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14009 if (elf_section_data (sec)->sec_info
14010 && (sec->flags & SEC_LINKER_CREATED) == 0)
14011 elf_eh_frame_section (sub) = sec;
14012 fini_reloc_cookie_for_section (&cookie, sec);
14013 sec = bfd_get_next_section_by_name (NULL, sec);
14014 }
14015 }
14016
14017 /* Apply transitive closure to the vtable entry usage info. */
14018 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14019 if (!ok)
14020 return FALSE;
14021
14022 /* Kill the vtable relocations that were not used. */
14023 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14024 if (!ok)
14025 return FALSE;
14026
14027 /* Mark dynamically referenced symbols. */
14028 if (htab->dynamic_sections_created || info->gc_keep_exported)
14029 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14030
14031 /* Grovel through relocs to find out who stays ... */
14032 gc_mark_hook = bed->gc_mark_hook;
14033 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14034 {
14035 asection *o;
14036
14037 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14038 || elf_object_id (sub) != elf_hash_table_id (htab)
14039 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14040 continue;
14041
14042 o = sub->sections;
14043 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14044 continue;
14045
14046 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14047 Also treat note sections as a root, if the section is not part
14048 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14049 well as FINI_ARRAY sections for ld -r. */
14050 for (o = sub->sections; o != NULL; o = o->next)
14051 if (!o->gc_mark
14052 && (o->flags & SEC_EXCLUDE) == 0
14053 && ((o->flags & SEC_KEEP) != 0
14054 || (bfd_link_relocatable (info)
14055 && ((elf_section_data (o)->this_hdr.sh_type
14056 == SHT_PREINIT_ARRAY)
14057 || (elf_section_data (o)->this_hdr.sh_type
14058 == SHT_INIT_ARRAY)
14059 || (elf_section_data (o)->this_hdr.sh_type
14060 == SHT_FINI_ARRAY)))
14061 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14062 && elf_next_in_group (o) == NULL
14063 && elf_linked_to_section (o) == NULL)
14064 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14065 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14066 {
14067 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14068 return FALSE;
14069 }
14070 }
14071
14072 /* Allow the backend to mark additional target specific sections. */
14073 bed->gc_mark_extra_sections (info, gc_mark_hook);
14074
14075 /* ... and mark SEC_EXCLUDE for those that go. */
14076 return elf_gc_sweep (abfd, info);
14077 }
14078 \f
14079 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14080
14081 bfd_boolean
14082 bfd_elf_gc_record_vtinherit (bfd *abfd,
14083 asection *sec,
14084 struct elf_link_hash_entry *h,
14085 bfd_vma offset)
14086 {
14087 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14088 struct elf_link_hash_entry **search, *child;
14089 size_t extsymcount;
14090 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14091
14092 /* The sh_info field of the symtab header tells us where the
14093 external symbols start. We don't care about the local symbols at
14094 this point. */
14095 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14096 if (!elf_bad_symtab (abfd))
14097 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14098
14099 sym_hashes = elf_sym_hashes (abfd);
14100 sym_hashes_end = sym_hashes + extsymcount;
14101
14102 /* Hunt down the child symbol, which is in this section at the same
14103 offset as the relocation. */
14104 for (search = sym_hashes; search != sym_hashes_end; ++search)
14105 {
14106 if ((child = *search) != NULL
14107 && (child->root.type == bfd_link_hash_defined
14108 || child->root.type == bfd_link_hash_defweak)
14109 && child->root.u.def.section == sec
14110 && child->root.u.def.value == offset)
14111 goto win;
14112 }
14113
14114 /* xgettext:c-format */
14115 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14116 abfd, sec, (uint64_t) offset);
14117 bfd_set_error (bfd_error_invalid_operation);
14118 return FALSE;
14119
14120 win:
14121 if (!child->u2.vtable)
14122 {
14123 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14124 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14125 if (!child->u2.vtable)
14126 return FALSE;
14127 }
14128 if (!h)
14129 {
14130 /* This *should* only be the absolute section. It could potentially
14131 be that someone has defined a non-global vtable though, which
14132 would be bad. It isn't worth paging in the local symbols to be
14133 sure though; that case should simply be handled by the assembler. */
14134
14135 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14136 }
14137 else
14138 child->u2.vtable->parent = h;
14139
14140 return TRUE;
14141 }
14142
14143 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14144
14145 bfd_boolean
14146 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14147 struct elf_link_hash_entry *h,
14148 bfd_vma addend)
14149 {
14150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14151 unsigned int log_file_align = bed->s->log_file_align;
14152
14153 if (!h)
14154 {
14155 /* xgettext:c-format */
14156 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14157 abfd, sec);
14158 bfd_set_error (bfd_error_bad_value);
14159 return FALSE;
14160 }
14161
14162 if (!h->u2.vtable)
14163 {
14164 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14165 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14166 if (!h->u2.vtable)
14167 return FALSE;
14168 }
14169
14170 if (addend >= h->u2.vtable->size)
14171 {
14172 size_t size, bytes, file_align;
14173 bfd_boolean *ptr = h->u2.vtable->used;
14174
14175 /* While the symbol is undefined, we have to be prepared to handle
14176 a zero size. */
14177 file_align = 1 << log_file_align;
14178 if (h->root.type == bfd_link_hash_undefined)
14179 size = addend + file_align;
14180 else
14181 {
14182 size = h->size;
14183 if (addend >= size)
14184 {
14185 /* Oops! We've got a reference past the defined end of
14186 the table. This is probably a bug -- shall we warn? */
14187 size = addend + file_align;
14188 }
14189 }
14190 size = (size + file_align - 1) & -file_align;
14191
14192 /* Allocate one extra entry for use as a "done" flag for the
14193 consolidation pass. */
14194 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14195
14196 if (ptr)
14197 {
14198 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14199
14200 if (ptr != NULL)
14201 {
14202 size_t oldbytes;
14203
14204 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14205 * sizeof (bfd_boolean));
14206 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14207 }
14208 }
14209 else
14210 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14211
14212 if (ptr == NULL)
14213 return FALSE;
14214
14215 /* And arrange for that done flag to be at index -1. */
14216 h->u2.vtable->used = ptr + 1;
14217 h->u2.vtable->size = size;
14218 }
14219
14220 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14221
14222 return TRUE;
14223 }
14224
14225 /* Map an ELF section header flag to its corresponding string. */
14226 typedef struct
14227 {
14228 char *flag_name;
14229 flagword flag_value;
14230 } elf_flags_to_name_table;
14231
14232 static const elf_flags_to_name_table elf_flags_to_names [] =
14233 {
14234 { "SHF_WRITE", SHF_WRITE },
14235 { "SHF_ALLOC", SHF_ALLOC },
14236 { "SHF_EXECINSTR", SHF_EXECINSTR },
14237 { "SHF_MERGE", SHF_MERGE },
14238 { "SHF_STRINGS", SHF_STRINGS },
14239 { "SHF_INFO_LINK", SHF_INFO_LINK},
14240 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14241 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14242 { "SHF_GROUP", SHF_GROUP },
14243 { "SHF_TLS", SHF_TLS },
14244 { "SHF_MASKOS", SHF_MASKOS },
14245 { "SHF_EXCLUDE", SHF_EXCLUDE },
14246 };
14247
14248 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14249 bfd_boolean
14250 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14251 struct flag_info *flaginfo,
14252 asection *section)
14253 {
14254 const bfd_vma sh_flags = elf_section_flags (section);
14255
14256 if (!flaginfo->flags_initialized)
14257 {
14258 bfd *obfd = info->output_bfd;
14259 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14260 struct flag_info_list *tf = flaginfo->flag_list;
14261 int with_hex = 0;
14262 int without_hex = 0;
14263
14264 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14265 {
14266 unsigned i;
14267 flagword (*lookup) (char *);
14268
14269 lookup = bed->elf_backend_lookup_section_flags_hook;
14270 if (lookup != NULL)
14271 {
14272 flagword hexval = (*lookup) ((char *) tf->name);
14273
14274 if (hexval != 0)
14275 {
14276 if (tf->with == with_flags)
14277 with_hex |= hexval;
14278 else if (tf->with == without_flags)
14279 without_hex |= hexval;
14280 tf->valid = TRUE;
14281 continue;
14282 }
14283 }
14284 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14285 {
14286 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14287 {
14288 if (tf->with == with_flags)
14289 with_hex |= elf_flags_to_names[i].flag_value;
14290 else if (tf->with == without_flags)
14291 without_hex |= elf_flags_to_names[i].flag_value;
14292 tf->valid = TRUE;
14293 break;
14294 }
14295 }
14296 if (!tf->valid)
14297 {
14298 info->callbacks->einfo
14299 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14300 return FALSE;
14301 }
14302 }
14303 flaginfo->flags_initialized = TRUE;
14304 flaginfo->only_with_flags |= with_hex;
14305 flaginfo->not_with_flags |= without_hex;
14306 }
14307
14308 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14309 return FALSE;
14310
14311 if ((flaginfo->not_with_flags & sh_flags) != 0)
14312 return FALSE;
14313
14314 return TRUE;
14315 }
14316
14317 struct alloc_got_off_arg {
14318 bfd_vma gotoff;
14319 struct bfd_link_info *info;
14320 };
14321
14322 /* We need a special top-level link routine to convert got reference counts
14323 to real got offsets. */
14324
14325 static bfd_boolean
14326 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14327 {
14328 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14329 bfd *obfd = gofarg->info->output_bfd;
14330 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14331
14332 if (h->got.refcount > 0)
14333 {
14334 h->got.offset = gofarg->gotoff;
14335 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14336 }
14337 else
14338 h->got.offset = (bfd_vma) -1;
14339
14340 return TRUE;
14341 }
14342
14343 /* And an accompanying bit to work out final got entry offsets once
14344 we're done. Should be called from final_link. */
14345
14346 bfd_boolean
14347 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14348 struct bfd_link_info *info)
14349 {
14350 bfd *i;
14351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14352 bfd_vma gotoff;
14353 struct alloc_got_off_arg gofarg;
14354
14355 BFD_ASSERT (abfd == info->output_bfd);
14356
14357 if (! is_elf_hash_table (info->hash))
14358 return FALSE;
14359
14360 /* The GOT offset is relative to the .got section, but the GOT header is
14361 put into the .got.plt section, if the backend uses it. */
14362 if (bed->want_got_plt)
14363 gotoff = 0;
14364 else
14365 gotoff = bed->got_header_size;
14366
14367 /* Do the local .got entries first. */
14368 for (i = info->input_bfds; i; i = i->link.next)
14369 {
14370 bfd_signed_vma *local_got;
14371 size_t j, locsymcount;
14372 Elf_Internal_Shdr *symtab_hdr;
14373
14374 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14375 continue;
14376
14377 local_got = elf_local_got_refcounts (i);
14378 if (!local_got)
14379 continue;
14380
14381 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14382 if (elf_bad_symtab (i))
14383 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14384 else
14385 locsymcount = symtab_hdr->sh_info;
14386
14387 for (j = 0; j < locsymcount; ++j)
14388 {
14389 if (local_got[j] > 0)
14390 {
14391 local_got[j] = gotoff;
14392 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14393 }
14394 else
14395 local_got[j] = (bfd_vma) -1;
14396 }
14397 }
14398
14399 /* Then the global .got entries. .plt refcounts are handled by
14400 adjust_dynamic_symbol */
14401 gofarg.gotoff = gotoff;
14402 gofarg.info = info;
14403 elf_link_hash_traverse (elf_hash_table (info),
14404 elf_gc_allocate_got_offsets,
14405 &gofarg);
14406 return TRUE;
14407 }
14408
14409 /* Many folk need no more in the way of final link than this, once
14410 got entry reference counting is enabled. */
14411
14412 bfd_boolean
14413 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14414 {
14415 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14416 return FALSE;
14417
14418 /* Invoke the regular ELF backend linker to do all the work. */
14419 return bfd_elf_final_link (abfd, info);
14420 }
14421
14422 bfd_boolean
14423 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14424 {
14425 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14426
14427 if (rcookie->bad_symtab)
14428 rcookie->rel = rcookie->rels;
14429
14430 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14431 {
14432 unsigned long r_symndx;
14433
14434 if (! rcookie->bad_symtab)
14435 if (rcookie->rel->r_offset > offset)
14436 return FALSE;
14437 if (rcookie->rel->r_offset != offset)
14438 continue;
14439
14440 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14441 if (r_symndx == STN_UNDEF)
14442 return TRUE;
14443
14444 if (r_symndx >= rcookie->locsymcount
14445 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14446 {
14447 struct elf_link_hash_entry *h;
14448
14449 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14450
14451 while (h->root.type == bfd_link_hash_indirect
14452 || h->root.type == bfd_link_hash_warning)
14453 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14454
14455 if ((h->root.type == bfd_link_hash_defined
14456 || h->root.type == bfd_link_hash_defweak)
14457 && (h->root.u.def.section->owner != rcookie->abfd
14458 || h->root.u.def.section->kept_section != NULL
14459 || discarded_section (h->root.u.def.section)))
14460 return TRUE;
14461 }
14462 else
14463 {
14464 /* It's not a relocation against a global symbol,
14465 but it could be a relocation against a local
14466 symbol for a discarded section. */
14467 asection *isec;
14468 Elf_Internal_Sym *isym;
14469
14470 /* Need to: get the symbol; get the section. */
14471 isym = &rcookie->locsyms[r_symndx];
14472 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14473 if (isec != NULL
14474 && (isec->kept_section != NULL
14475 || discarded_section (isec)))
14476 return TRUE;
14477 }
14478 return FALSE;
14479 }
14480 return FALSE;
14481 }
14482
14483 /* Discard unneeded references to discarded sections.
14484 Returns -1 on error, 1 if any section's size was changed, 0 if
14485 nothing changed. This function assumes that the relocations are in
14486 sorted order, which is true for all known assemblers. */
14487
14488 int
14489 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14490 {
14491 struct elf_reloc_cookie cookie;
14492 asection *o;
14493 bfd *abfd;
14494 int changed = 0;
14495
14496 if (info->traditional_format
14497 || !is_elf_hash_table (info->hash))
14498 return 0;
14499
14500 o = bfd_get_section_by_name (output_bfd, ".stab");
14501 if (o != NULL)
14502 {
14503 asection *i;
14504
14505 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14506 {
14507 if (i->size == 0
14508 || i->reloc_count == 0
14509 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14510 continue;
14511
14512 abfd = i->owner;
14513 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14514 continue;
14515
14516 if (!init_reloc_cookie_for_section (&cookie, info, i))
14517 return -1;
14518
14519 if (_bfd_discard_section_stabs (abfd, i,
14520 elf_section_data (i)->sec_info,
14521 bfd_elf_reloc_symbol_deleted_p,
14522 &cookie))
14523 changed = 1;
14524
14525 fini_reloc_cookie_for_section (&cookie, i);
14526 }
14527 }
14528
14529 o = NULL;
14530 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14531 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14532 if (o != NULL)
14533 {
14534 asection *i;
14535 int eh_changed = 0;
14536 unsigned int eh_alignment; /* Octets. */
14537
14538 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14539 {
14540 if (i->size == 0)
14541 continue;
14542
14543 abfd = i->owner;
14544 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14545 continue;
14546
14547 if (!init_reloc_cookie_for_section (&cookie, info, i))
14548 return -1;
14549
14550 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14551 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14552 bfd_elf_reloc_symbol_deleted_p,
14553 &cookie))
14554 {
14555 eh_changed = 1;
14556 if (i->size != i->rawsize)
14557 changed = 1;
14558 }
14559
14560 fini_reloc_cookie_for_section (&cookie, i);
14561 }
14562
14563 eh_alignment = ((1 << o->alignment_power)
14564 * bfd_octets_per_byte (output_bfd, o));
14565 /* Skip over zero terminator, and prevent empty sections from
14566 adding alignment padding at the end. */
14567 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14568 if (i->size == 0)
14569 i->flags |= SEC_EXCLUDE;
14570 else if (i->size > 4)
14571 break;
14572 /* The last non-empty eh_frame section doesn't need padding. */
14573 if (i != NULL)
14574 i = i->map_tail.s;
14575 /* Any prior sections must pad the last FDE out to the output
14576 section alignment. Otherwise we might have zero padding
14577 between sections, which would be seen as a terminator. */
14578 for (; i != NULL; i = i->map_tail.s)
14579 if (i->size == 4)
14580 /* All but the last zero terminator should have been removed. */
14581 BFD_FAIL ();
14582 else
14583 {
14584 bfd_size_type size
14585 = (i->size + eh_alignment - 1) & -eh_alignment;
14586 if (i->size != size)
14587 {
14588 i->size = size;
14589 changed = 1;
14590 eh_changed = 1;
14591 }
14592 }
14593 if (eh_changed)
14594 elf_link_hash_traverse (elf_hash_table (info),
14595 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14596 }
14597
14598 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14599 {
14600 const struct elf_backend_data *bed;
14601 asection *s;
14602
14603 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14604 continue;
14605 s = abfd->sections;
14606 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14607 continue;
14608
14609 bed = get_elf_backend_data (abfd);
14610
14611 if (bed->elf_backend_discard_info != NULL)
14612 {
14613 if (!init_reloc_cookie (&cookie, info, abfd))
14614 return -1;
14615
14616 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14617 changed = 1;
14618
14619 fini_reloc_cookie (&cookie, abfd);
14620 }
14621 }
14622
14623 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14624 _bfd_elf_end_eh_frame_parsing (info);
14625
14626 if (info->eh_frame_hdr_type
14627 && !bfd_link_relocatable (info)
14628 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14629 changed = 1;
14630
14631 return changed;
14632 }
14633
14634 bfd_boolean
14635 _bfd_elf_section_already_linked (bfd *abfd,
14636 asection *sec,
14637 struct bfd_link_info *info)
14638 {
14639 flagword flags;
14640 const char *name, *key;
14641 struct bfd_section_already_linked *l;
14642 struct bfd_section_already_linked_hash_entry *already_linked_list;
14643
14644 if (sec->output_section == bfd_abs_section_ptr)
14645 return FALSE;
14646
14647 flags = sec->flags;
14648
14649 /* Return if it isn't a linkonce section. A comdat group section
14650 also has SEC_LINK_ONCE set. */
14651 if ((flags & SEC_LINK_ONCE) == 0)
14652 return FALSE;
14653
14654 /* Don't put group member sections on our list of already linked
14655 sections. They are handled as a group via their group section. */
14656 if (elf_sec_group (sec) != NULL)
14657 return FALSE;
14658
14659 /* For a SHT_GROUP section, use the group signature as the key. */
14660 name = sec->name;
14661 if ((flags & SEC_GROUP) != 0
14662 && elf_next_in_group (sec) != NULL
14663 && elf_group_name (elf_next_in_group (sec)) != NULL)
14664 key = elf_group_name (elf_next_in_group (sec));
14665 else
14666 {
14667 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14668 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14669 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14670 key++;
14671 else
14672 /* Must be a user linkonce section that doesn't follow gcc's
14673 naming convention. In this case we won't be matching
14674 single member groups. */
14675 key = name;
14676 }
14677
14678 already_linked_list = bfd_section_already_linked_table_lookup (key);
14679
14680 for (l = already_linked_list->entry; l != NULL; l = l->next)
14681 {
14682 /* We may have 2 different types of sections on the list: group
14683 sections with a signature of <key> (<key> is some string),
14684 and linkonce sections named .gnu.linkonce.<type>.<key>.
14685 Match like sections. LTO plugin sections are an exception.
14686 They are always named .gnu.linkonce.t.<key> and match either
14687 type of section. */
14688 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14689 && ((flags & SEC_GROUP) != 0
14690 || strcmp (name, l->sec->name) == 0))
14691 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14692 || (sec->owner->flags & BFD_PLUGIN) != 0)
14693 {
14694 /* The section has already been linked. See if we should
14695 issue a warning. */
14696 if (!_bfd_handle_already_linked (sec, l, info))
14697 return FALSE;
14698
14699 if (flags & SEC_GROUP)
14700 {
14701 asection *first = elf_next_in_group (sec);
14702 asection *s = first;
14703
14704 while (s != NULL)
14705 {
14706 s->output_section = bfd_abs_section_ptr;
14707 /* Record which group discards it. */
14708 s->kept_section = l->sec;
14709 s = elf_next_in_group (s);
14710 /* These lists are circular. */
14711 if (s == first)
14712 break;
14713 }
14714 }
14715
14716 return TRUE;
14717 }
14718 }
14719
14720 /* A single member comdat group section may be discarded by a
14721 linkonce section and vice versa. */
14722 if ((flags & SEC_GROUP) != 0)
14723 {
14724 asection *first = elf_next_in_group (sec);
14725
14726 if (first != NULL && elf_next_in_group (first) == first)
14727 /* Check this single member group against linkonce sections. */
14728 for (l = already_linked_list->entry; l != NULL; l = l->next)
14729 if ((l->sec->flags & SEC_GROUP) == 0
14730 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14731 {
14732 first->output_section = bfd_abs_section_ptr;
14733 first->kept_section = l->sec;
14734 sec->output_section = bfd_abs_section_ptr;
14735 break;
14736 }
14737 }
14738 else
14739 /* Check this linkonce section against single member groups. */
14740 for (l = already_linked_list->entry; l != NULL; l = l->next)
14741 if (l->sec->flags & SEC_GROUP)
14742 {
14743 asection *first = elf_next_in_group (l->sec);
14744
14745 if (first != NULL
14746 && elf_next_in_group (first) == first
14747 && bfd_elf_match_symbols_in_sections (first, sec, info))
14748 {
14749 sec->output_section = bfd_abs_section_ptr;
14750 sec->kept_section = first;
14751 break;
14752 }
14753 }
14754
14755 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14756 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14757 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14758 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14759 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14760 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14761 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14762 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14763 The reverse order cannot happen as there is never a bfd with only the
14764 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14765 matter as here were are looking only for cross-bfd sections. */
14766
14767 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14768 for (l = already_linked_list->entry; l != NULL; l = l->next)
14769 if ((l->sec->flags & SEC_GROUP) == 0
14770 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14771 {
14772 if (abfd != l->sec->owner)
14773 sec->output_section = bfd_abs_section_ptr;
14774 break;
14775 }
14776
14777 /* This is the first section with this name. Record it. */
14778 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14779 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14780 return sec->output_section == bfd_abs_section_ptr;
14781 }
14782
14783 bfd_boolean
14784 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14785 {
14786 return sym->st_shndx == SHN_COMMON;
14787 }
14788
14789 unsigned int
14790 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14791 {
14792 return SHN_COMMON;
14793 }
14794
14795 asection *
14796 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14797 {
14798 return bfd_com_section_ptr;
14799 }
14800
14801 bfd_vma
14802 _bfd_elf_default_got_elt_size (bfd *abfd,
14803 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14804 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14805 bfd *ibfd ATTRIBUTE_UNUSED,
14806 unsigned long symndx ATTRIBUTE_UNUSED)
14807 {
14808 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14809 return bed->s->arch_size / 8;
14810 }
14811
14812 /* Routines to support the creation of dynamic relocs. */
14813
14814 /* Returns the name of the dynamic reloc section associated with SEC. */
14815
14816 static const char *
14817 get_dynamic_reloc_section_name (bfd * abfd,
14818 asection * sec,
14819 bfd_boolean is_rela)
14820 {
14821 char *name;
14822 const char *old_name = bfd_section_name (sec);
14823 const char *prefix = is_rela ? ".rela" : ".rel";
14824
14825 if (old_name == NULL)
14826 return NULL;
14827
14828 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14829 sprintf (name, "%s%s", prefix, old_name);
14830
14831 return name;
14832 }
14833
14834 /* Returns the dynamic reloc section associated with SEC.
14835 If necessary compute the name of the dynamic reloc section based
14836 on SEC's name (looked up in ABFD's string table) and the setting
14837 of IS_RELA. */
14838
14839 asection *
14840 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14841 asection * sec,
14842 bfd_boolean is_rela)
14843 {
14844 asection * reloc_sec = elf_section_data (sec)->sreloc;
14845
14846 if (reloc_sec == NULL)
14847 {
14848 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14849
14850 if (name != NULL)
14851 {
14852 reloc_sec = bfd_get_linker_section (abfd, name);
14853
14854 if (reloc_sec != NULL)
14855 elf_section_data (sec)->sreloc = reloc_sec;
14856 }
14857 }
14858
14859 return reloc_sec;
14860 }
14861
14862 /* Returns the dynamic reloc section associated with SEC. If the
14863 section does not exist it is created and attached to the DYNOBJ
14864 bfd and stored in the SRELOC field of SEC's elf_section_data
14865 structure.
14866
14867 ALIGNMENT is the alignment for the newly created section and
14868 IS_RELA defines whether the name should be .rela.<SEC's name>
14869 or .rel.<SEC's name>. The section name is looked up in the
14870 string table associated with ABFD. */
14871
14872 asection *
14873 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14874 bfd *dynobj,
14875 unsigned int alignment,
14876 bfd *abfd,
14877 bfd_boolean is_rela)
14878 {
14879 asection * reloc_sec = elf_section_data (sec)->sreloc;
14880
14881 if (reloc_sec == NULL)
14882 {
14883 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14884
14885 if (name == NULL)
14886 return NULL;
14887
14888 reloc_sec = bfd_get_linker_section (dynobj, name);
14889
14890 if (reloc_sec == NULL)
14891 {
14892 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14893 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14894 if ((sec->flags & SEC_ALLOC) != 0)
14895 flags |= SEC_ALLOC | SEC_LOAD;
14896
14897 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14898 if (reloc_sec != NULL)
14899 {
14900 /* _bfd_elf_get_sec_type_attr chooses a section type by
14901 name. Override as it may be wrong, eg. for a user
14902 section named "auto" we'll get ".relauto" which is
14903 seen to be a .rela section. */
14904 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14905 if (!bfd_set_section_alignment (reloc_sec, alignment))
14906 reloc_sec = NULL;
14907 }
14908 }
14909
14910 elf_section_data (sec)->sreloc = reloc_sec;
14911 }
14912
14913 return reloc_sec;
14914 }
14915
14916 /* Copy the ELF symbol type and other attributes for a linker script
14917 assignment from HSRC to HDEST. Generally this should be treated as
14918 if we found a strong non-dynamic definition for HDEST (except that
14919 ld ignores multiple definition errors). */
14920 void
14921 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14922 struct bfd_link_hash_entry *hdest,
14923 struct bfd_link_hash_entry *hsrc)
14924 {
14925 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14926 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14927 Elf_Internal_Sym isym;
14928
14929 ehdest->type = ehsrc->type;
14930 ehdest->target_internal = ehsrc->target_internal;
14931
14932 isym.st_other = ehsrc->other;
14933 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
14934 }
14935
14936 /* Append a RELA relocation REL to section S in BFD. */
14937
14938 void
14939 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14940 {
14941 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14942 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14943 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14944 bed->s->swap_reloca_out (abfd, rel, loc);
14945 }
14946
14947 /* Append a REL relocation REL to section S in BFD. */
14948
14949 void
14950 elf_append_rel (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_rel);
14954 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14955 bed->s->swap_reloc_out (abfd, rel, loc);
14956 }
14957
14958 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14959
14960 struct bfd_link_hash_entry *
14961 bfd_elf_define_start_stop (struct bfd_link_info *info,
14962 const char *symbol, asection *sec)
14963 {
14964 struct elf_link_hash_entry *h;
14965
14966 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14967 FALSE, FALSE, TRUE);
14968 /* NB: Common symbols will be turned into definition later. */
14969 if (h != NULL
14970 && (h->root.type == bfd_link_hash_undefined
14971 || h->root.type == bfd_link_hash_undefweak
14972 || ((h->ref_regular || h->def_dynamic)
14973 && !h->def_regular
14974 && h->root.type != bfd_link_hash_common)))
14975 {
14976 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14977 h->verinfo.verdef = NULL;
14978 h->root.type = bfd_link_hash_defined;
14979 h->root.u.def.section = sec;
14980 h->root.u.def.value = 0;
14981 h->def_regular = 1;
14982 h->def_dynamic = 0;
14983 h->start_stop = 1;
14984 h->u2.start_stop_section = sec;
14985 if (symbol[0] == '.')
14986 {
14987 /* .startof. and .sizeof. symbols are local. */
14988 const struct elf_backend_data *bed;
14989 bed = get_elf_backend_data (info->output_bfd);
14990 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14991 }
14992 else
14993 {
14994 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14995 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
14996 | info->start_stop_visibility);
14997 if (was_dynamic)
14998 bfd_elf_link_record_dynamic_symbol (info, h);
14999 }
15000 return &h->root;
15001 }
15002 return NULL;
15003 }
15004
15005 /* Find dynamic relocs for H that apply to read-only sections. */
15006
15007 asection *
15008 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15009 {
15010 struct elf_dyn_relocs *p;
15011
15012 for (p = h->dyn_relocs; p != NULL; p = p->next)
15013 {
15014 asection *s = p->sec->output_section;
15015
15016 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15017 return p->sec;
15018 }
15019 return NULL;
15020 }
15021
15022 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15023 read-only sections. */
15024
15025 bfd_boolean
15026 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15027 {
15028 asection *sec;
15029
15030 if (h->root.type == bfd_link_hash_indirect)
15031 return TRUE;
15032
15033 sec = _bfd_elf_readonly_dynrelocs (h);
15034 if (sec != NULL)
15035 {
15036 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15037
15038 info->flags |= DF_TEXTREL;
15039 /* xgettext:c-format */
15040 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15041 "in read-only section `%pA'\n"),
15042 sec->owner, h->root.root.string, sec);
15043
15044 if (bfd_link_textrel_check (info))
15045 /* xgettext:c-format */
15046 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15047 "in read-only section `%pA'\n"),
15048 sec->owner, h->root.root.string, sec);
15049
15050 /* Not an error, just cut short the traversal. */
15051 return FALSE;
15052 }
15053 return TRUE;
15054 }
15055
15056 /* Add dynamic tags. */
15057
15058 bfd_boolean
15059 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15060 bfd_boolean need_dynamic_reloc)
15061 {
15062 struct elf_link_hash_table *htab = elf_hash_table (info);
15063
15064 if (htab->dynamic_sections_created)
15065 {
15066 /* Add some entries to the .dynamic section. We fill in the
15067 values later, in finish_dynamic_sections, but we must add
15068 the entries now so that we get the correct size for the
15069 .dynamic section. The DT_DEBUG entry is filled in by the
15070 dynamic linker and used by the debugger. */
15071 #define add_dynamic_entry(TAG, VAL) \
15072 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15073
15074 const struct elf_backend_data *bed
15075 = get_elf_backend_data (output_bfd);
15076
15077 if (bfd_link_executable (info))
15078 {
15079 if (!add_dynamic_entry (DT_DEBUG, 0))
15080 return FALSE;
15081 }
15082
15083 if (htab->dt_pltgot_required || htab->splt->size != 0)
15084 {
15085 /* DT_PLTGOT is used by prelink even if there is no PLT
15086 relocation. */
15087 if (!add_dynamic_entry (DT_PLTGOT, 0))
15088 return FALSE;
15089 }
15090
15091 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15092 {
15093 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15094 || !add_dynamic_entry (DT_PLTREL,
15095 (bed->rela_plts_and_copies_p
15096 ? DT_RELA : DT_REL))
15097 || !add_dynamic_entry (DT_JMPREL, 0))
15098 return FALSE;
15099 }
15100
15101 if (htab->tlsdesc_plt
15102 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15103 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15104 return FALSE;
15105
15106 if (need_dynamic_reloc)
15107 {
15108 if (bed->rela_plts_and_copies_p)
15109 {
15110 if (!add_dynamic_entry (DT_RELA, 0)
15111 || !add_dynamic_entry (DT_RELASZ, 0)
15112 || !add_dynamic_entry (DT_RELAENT,
15113 bed->s->sizeof_rela))
15114 return FALSE;
15115 }
15116 else
15117 {
15118 if (!add_dynamic_entry (DT_REL, 0)
15119 || !add_dynamic_entry (DT_RELSZ, 0)
15120 || !add_dynamic_entry (DT_RELENT,
15121 bed->s->sizeof_rel))
15122 return FALSE;
15123 }
15124
15125 /* If any dynamic relocs apply to a read-only section,
15126 then we need a DT_TEXTREL entry. */
15127 if ((info->flags & DF_TEXTREL) == 0)
15128 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15129 info);
15130
15131 if ((info->flags & DF_TEXTREL) != 0)
15132 {
15133 if (htab->ifunc_resolvers)
15134 info->callbacks->einfo
15135 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15136 "may result in a segfault at runtime; recompile with %s\n"),
15137 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15138
15139 if (!add_dynamic_entry (DT_TEXTREL, 0))
15140 return FALSE;
15141 }
15142 }
15143 }
15144 #undef add_dynamic_entry
15145
15146 return TRUE;
15147 }