]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
PR26979, Visibility of undefined foo@v1 should constrain foo@@v1
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #ifndef CHAR_BIT
39 #define CHAR_BIT 8
40 #endif
41
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45 struct elf_info_failed
46 {
47 struct bfd_link_info *info;
48 bfd_boolean failed;
49 };
50
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54 struct elf_find_verdep_info
55 {
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62 };
63
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
67 asection *
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71 {
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
86 return h->root.u.def.section;
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106 }
107
108 /* Define a symbol in a dynamic linkage section. */
109
110 struct elf_link_hash_entry *
111 _bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115 {
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
128 bh = &h->root;
129 }
130 else
131 bh = NULL;
132
133 bed = get_elf_backend_data (abfd);
134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
135 sec, 0, NULL, FALSE, bed->collect,
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
139 BFD_ASSERT (h != NULL);
140 h->def_regular = 1;
141 h->non_elf = 0;
142 h->root.linker_def = 1;
143 h->type = STT_OBJECT;
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
146
147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
148 return h;
149 }
150
151 bfd_boolean
152 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
153 {
154 flagword flags;
155 asection *s;
156 struct elf_link_hash_entry *h;
157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
158 struct elf_link_hash_table *htab = elf_hash_table (info);
159
160 /* This function may be called more than once. */
161 if (htab->sgot != NULL)
162 return TRUE;
163
164 flags = bed->dynamic_sec_flags;
165
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
171 if (s == NULL
172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
173 return FALSE;
174 htab->srelgot = s;
175
176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
177 if (s == NULL
178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
179 return FALSE;
180 htab->sgot = s;
181
182 if (bed->want_got_plt)
183 {
184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
185 if (s == NULL
186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
187 return FALSE;
188 htab->sgotplt = s;
189 }
190
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info)->hgot = h;
203 if (h == NULL)
204 return FALSE;
205 }
206
207 return TRUE;
208 }
209 \f
210 /* Create a strtab to hold the dynamic symbol names. */
211 static bfd_boolean
212 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213 {
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
226 asection *s;
227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
228 if ((ibfd->flags
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249 }
250
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
257
258 bfd_boolean
259 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
260 {
261 flagword flags;
262 asection *s;
263 const struct elf_backend_data *bed;
264 struct elf_link_hash_entry *h;
265
266 if (! is_elf_hash_table (info->hash))
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
274
275 abfd = elf_hash_table (info)->dynobj;
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info) && !info->nointerp)
283 {
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
288 }
289
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || !bfd_set_section_alignment (s, 1))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 return FALSE;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
312 if (s == NULL
313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
314 return FALSE;
315 elf_hash_table (info)->dynsym = s;
316
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
319 if (s == NULL)
320 return FALSE;
321
322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
323 if (s == NULL
324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
336 return FALSE;
337
338 if (info->emit_hash)
339 {
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
342 if (s == NULL
343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
349 {
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
352 if (s == NULL
353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374 }
375
376 /* Create dynamic sections when linking against a dynamic object. */
377
378 bfd_boolean
379 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
380 {
381 flagword flags, pltflags;
382 struct elf_link_hash_entry *h;
383 asection *s;
384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
385 struct elf_link_hash_table *htab = elf_hash_table (info);
386
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags = bed->dynamic_sec_flags;
390
391 pltflags = flags;
392 if (bed->plt_not_loaded)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
403 if (s == NULL
404 || !bfd_set_section_alignment (s, bed->plt_alignment))
405 return FALSE;
406 htab->splt = s;
407
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
418
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
423 if (s == NULL
424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
425 return FALSE;
426 htab->srelplt = s;
427
428 if (! _bfd_elf_create_got_section (abfd, info))
429 return FALSE;
430
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
440 SEC_ALLOC | SEC_LINKER_CREATED);
441 if (s == NULL)
442 return FALSE;
443 htab->sdynbss = s;
444
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 flags);
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
468 if (bfd_link_executable (info))
469 {
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
474 if (s == NULL
475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
476 return FALSE;
477 htab->srelbss = s;
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
490 }
491 }
492
493 return TRUE;
494 }
495 \f
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
504 bfd_boolean
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
507 {
508 if (h->dynindx == -1)
509 {
510 struct elf_strtab_hash *dynstr;
511 char *p;
512 const char *name;
513 size_t indx;
514
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
535 {
536 h->forced_local = 1;
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
539 }
540
541 default:
542 break;
543 }
544
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
547
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
550 {
551 /* Create a strtab to hold the dynamic symbol names. */
552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
553 if (dynstr == NULL)
554 return FALSE;
555 }
556
557 /* We don't put any version information in the dynamic string
558 table. */
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
568
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
570
571 if (p != NULL)
572 *p = ELF_VER_CHR;
573
574 if (indx == (size_t) -1)
575 return FALSE;
576 h->dynstr_index = indx;
577 }
578
579 return TRUE;
580 }
581 \f
582 /* Mark a symbol dynamic. */
583
584 static void
585 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
588 {
589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
590
591 /* It may be called more than once on the same H. */
592 if(h->dynamic || bfd_link_relocatable (info))
593 return;
594
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
597 || h->type == STT_COMMON
598 || (sym != NULL
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
601 || (d != NULL
602 && h->non_elf
603 && (*d->match) (&d->head, NULL, h->root.root.string)))
604 {
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
609 }
610 }
611
612 /* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
614
615 bfd_boolean
616 bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
618 const char *name,
619 bfd_boolean provide,
620 bfd_boolean hidden)
621 {
622 struct elf_link_hash_entry *h, *hv;
623 struct elf_link_hash_table *htab;
624 const struct elf_backend_data *bed;
625
626 if (!is_elf_hash_table (info->hash))
627 return TRUE;
628
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
631 if (h == NULL)
632 return provide;
633
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
636
637 if (h->versioned == unknown)
638 {
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
642 {
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
647 }
648 }
649
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
653 {
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
656 }
657
658 switch (h->root.type)
659 {
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
669 h->root.type = bfd_link_hash_new;
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
672 break;
673 case bfd_link_hash_new:
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
677 the versioned symbol point to this one. */
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
690 default:
691 BFD_FAIL ();
692 return FALSE;
693 }
694
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
700 && h->def_dynamic
701 && !h->def_regular)
702 h->root.type = bfd_link_hash_undefined;
703
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
709 h->verinfo.verdef = NULL;
710
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
713
714 h->def_regular = 1;
715
716 if (hidden)
717 {
718 bed = get_elf_backend_data (output_bfd);
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
722 }
723
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
726 if (!bfd_link_relocatable (info)
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
731
732 if ((h->def_dynamic
733 || h->ref_dynamic
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
736 && !h->forced_local
737 && h->dynindx == -1)
738 {
739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
740 return FALSE;
741
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
745 if (h->is_weakalias)
746 {
747 struct elf_link_hash_entry *def = weakdef (h);
748
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
751 return FALSE;
752 }
753 }
754
755 return TRUE;
756 }
757
758 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
761
762 int
763 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
766 {
767 size_t amt;
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
771 size_t dynstr_index;
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
775
776 if (! is_elf_hash_table (info->hash))
777 return 0;
778
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
783
784 amt = sizeof (*entry);
785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
786 if (entry == NULL)
787 return 0;
788
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
791 1, input_indx, &entry->isym, esym, &eshndx))
792 {
793 bfd_release (input_bfd, entry);
794 return 0;
795 }
796
797 if (entry->isym.st_shndx != SHN_UNDEF
798 && entry->isym.st_shndx < SHN_LORESERVE)
799 {
800 asection *s;
801
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
804 {
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
809 }
810 }
811
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
815
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
818 {
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
823 }
824
825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
826 if (dynstr_index == (size_t) -1)
827 return 0;
828 entry->isym.st_name = dynstr_index;
829
830 eht = elf_hash_table (info);
831
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
837
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
841
842 /* The dynindx will be set at the end of size_dynamic_sections. */
843
844 return 1;
845 }
846
847 /* Return the dynindex of a local dynamic symbol. */
848
849 long
850 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
853 {
854 struct elf_link_local_dynamic_entry *e;
855
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
860 }
861
862 /* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
865
866 static bfd_boolean
867 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869 {
870 size_t *count = (size_t *) data;
871
872 if (h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879 }
880
881
882 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
884
885 static bfd_boolean
886 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888 {
889 size_t *count = (size_t *) data;
890
891 if (!h->forced_local)
892 return TRUE;
893
894 if (h->dynindx != -1)
895 h->dynindx = ++(*count);
896
897 return TRUE;
898 }
899
900 /* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902 bfd_boolean
903 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
906 {
907 struct elf_link_hash_table *htab;
908 asection *ip;
909
910 switch (elf_section_data (p)->this_hdr.sh_type)
911 {
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
917 htab = elf_hash_table (info);
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
920
921 return (htab->dynobj != NULL
922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
923 && ip->output_section == p);
924
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
929 }
930 }
931
932 bfd_boolean
933 _bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
937 {
938 return TRUE;
939 }
940
941 /* Assign dynsym indices. In a shared library we generate a section
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
949
950 static unsigned long
951 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
954 {
955 unsigned long dynsymcount = 0;
956 bfd_boolean do_sec = section_sym_count != NULL;
957
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
960 {
961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
964 if ((p->flags & SEC_EXCLUDE) == 0
965 && (p->flags & SEC_ALLOC) != 0
966 && elf_hash_table (info)->dynamic_relocs
967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
968 {
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
972 }
973 else if (do_sec)
974 elf_section_data (p)->dynindx = 0;
975 }
976 if (do_sec)
977 *section_sym_count = dynsymcount;
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
982
983 if (elf_hash_table (info)->dynlocal)
984 {
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
988 }
989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
990
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
994
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
1000
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
1003 }
1004
1005 /* Merge st_other field. */
1006
1007 static void
1008 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1009 unsigned int st_other, asection *sec,
1010 bfd_boolean definition, bfd_boolean dynamic)
1011 {
1012 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1013
1014 /* If st_other has a processor-specific meaning, specific
1015 code might be needed here. */
1016 if (bed->elf_backend_merge_symbol_attribute)
1017 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1018 dynamic);
1019
1020 if (!dynamic)
1021 {
1022 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1023 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1024
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis - 1 < hvis - 1)
1028 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1029 }
1030 else if (definition
1031 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1032 && (sec->flags & SEC_READONLY) == 0)
1033 h->protected_def = 1;
1034 }
1035
1036 /* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
1048
1049 static bfd_boolean
1050 _bfd_elf_merge_symbol (bfd *abfd,
1051 struct bfd_link_info *info,
1052 const char *name,
1053 Elf_Internal_Sym *sym,
1054 asection **psec,
1055 bfd_vma *pvalue,
1056 struct elf_link_hash_entry **sym_hash,
1057 bfd **poldbfd,
1058 bfd_boolean *pold_weak,
1059 unsigned int *pold_alignment,
1060 bfd_boolean *skip,
1061 bfd **override,
1062 bfd_boolean *type_change_ok,
1063 bfd_boolean *size_change_ok,
1064 bfd_boolean *matched)
1065 {
1066 asection *sec, *oldsec;
1067 struct elf_link_hash_entry *h;
1068 struct elf_link_hash_entry *hi;
1069 struct elf_link_hash_entry *flip;
1070 int bind;
1071 bfd *oldbfd;
1072 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1073 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1074 const struct elf_backend_data *bed;
1075 char *new_version;
1076 bfd_boolean default_sym = *matched;
1077
1078 *skip = FALSE;
1079 *override = NULL;
1080
1081 sec = *psec;
1082 bind = ELF_ST_BIND (sym->st_info);
1083
1084 if (! bfd_is_und_section (sec))
1085 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1086 else
1087 h = ((struct elf_link_hash_entry *)
1088 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1089 if (h == NULL)
1090 return FALSE;
1091 *sym_hash = h;
1092
1093 bed = get_elf_backend_data (abfd);
1094
1095 /* NEW_VERSION is the symbol version of the new symbol. */
1096 if (h->versioned != unversioned)
1097 {
1098 /* Symbol version is unknown or versioned. */
1099 new_version = strrchr (name, ELF_VER_CHR);
1100 if (new_version)
1101 {
1102 if (h->versioned == unknown)
1103 {
1104 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1105 h->versioned = versioned_hidden;
1106 else
1107 h->versioned = versioned;
1108 }
1109 new_version += 1;
1110 if (new_version[0] == '\0')
1111 new_version = NULL;
1112 }
1113 else
1114 h->versioned = unversioned;
1115 }
1116 else
1117 new_version = NULL;
1118
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1121 hi = h;
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1125
1126 if (!*matched)
1127 {
1128 if (hi == h || h->root.type == bfd_link_hash_new)
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_HIDDEN is true if the existing symbol is only visible
1133 to the symbol with the same symbol version. NEW_HIDDEN is
1134 true if the new symbol is only visible to the symbol with
1135 the same symbol version. */
1136 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1137 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1138 if (!old_hidden && !new_hidden)
1139 /* The new symbol matches the existing symbol if both
1140 aren't hidden. */
1141 *matched = TRUE;
1142 else
1143 {
1144 /* OLD_VERSION is the symbol version of the existing
1145 symbol. */
1146 char *old_version;
1147
1148 if (h->versioned >= versioned)
1149 old_version = strrchr (h->root.root.string,
1150 ELF_VER_CHR) + 1;
1151 else
1152 old_version = NULL;
1153
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched = (old_version == new_version
1157 || (old_version != NULL
1158 && new_version != NULL
1159 && strcmp (old_version, new_version) == 0));
1160 }
1161 }
1162 }
1163
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1165 existing symbol. */
1166
1167 oldbfd = NULL;
1168 oldsec = NULL;
1169 switch (h->root.type)
1170 {
1171 default:
1172 break;
1173
1174 case bfd_link_hash_undefined:
1175 case bfd_link_hash_undefweak:
1176 oldbfd = h->root.u.undef.abfd;
1177 break;
1178
1179 case bfd_link_hash_defined:
1180 case bfd_link_hash_defweak:
1181 oldbfd = h->root.u.def.section->owner;
1182 oldsec = h->root.u.def.section;
1183 break;
1184
1185 case bfd_link_hash_common:
1186 oldbfd = h->root.u.c.p->section->owner;
1187 oldsec = h->root.u.c.p->section;
1188 if (pold_alignment)
1189 *pold_alignment = h->root.u.c.p->alignment_power;
1190 break;
1191 }
1192 if (poldbfd && *poldbfd == NULL)
1193 *poldbfd = oldbfd;
1194
1195 /* Differentiate strong and weak symbols. */
1196 newweak = bind == STB_WEAK;
1197 oldweak = (h->root.type == bfd_link_hash_defweak
1198 || h->root.type == bfd_link_hash_undefweak);
1199 if (pold_weak)
1200 *pold_weak = oldweak;
1201
1202 /* We have to check it for every instance since the first few may be
1203 references and not all compilers emit symbol type for undefined
1204 symbols. */
1205 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1206
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1209
1210 newdyn = (abfd->flags & DYNAMIC) != 0;
1211
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1219 if (newdyn)
1220 {
1221 if (bfd_is_und_section (sec))
1222 {
1223 if (bind != STB_WEAK)
1224 {
1225 h->ref_dynamic_nonweak = 1;
1226 hi->ref_dynamic_nonweak = 1;
1227 }
1228 }
1229 else
1230 {
1231 /* Update the existing symbol only if they match. */
1232 if (*matched)
1233 h->dynamic_def = 1;
1234 hi->dynamic_def = 1;
1235 }
1236 }
1237
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1241
1242 if (h->root.type == bfd_link_hash_new)
1243 {
1244 h->non_elf = 0;
1245 return TRUE;
1246 }
1247
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1254 if (abfd == oldbfd
1255 && (newweak || oldweak)
1256 && ((abfd->flags & DYNAMIC) == 0
1257 || !h->def_regular))
1258 return TRUE;
1259
1260 olddyn = FALSE;
1261 if (oldbfd != NULL)
1262 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1263 else if (oldsec != NULL)
1264 {
1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1266 indices used by MIPS ELF. */
1267 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1268 }
1269
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1272 if (oldbfd != NULL
1273 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1274 && newdyn != olddyn)
1275 {
1276 h->root.non_ir_ref_dynamic = TRUE;
1277 hi->root.non_ir_ref_dynamic = TRUE;
1278 }
1279
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1282
1283 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1284
1285 olddef = (h->root.type != bfd_link_hash_undefined
1286 && h->root.type != bfd_link_hash_undefweak
1287 && h->root.type != bfd_link_hash_common);
1288
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1291
1292 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1293 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1294
1295 oldfunc = (h->type != STT_NOTYPE
1296 && bed->is_function_type (h->type));
1297
1298 if (!(newfunc && oldfunc)
1299 && ELF_ST_TYPE (sym->st_info) != h->type
1300 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1301 && h->type != STT_NOTYPE
1302 && (newdef || bfd_is_com_section (sec))
1303 && (olddef || h->root.type == bfd_link_hash_common))
1304 {
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1310 if (newdyn
1311 && !olddyn)
1312 {
1313 *skip = TRUE;
1314 return TRUE;
1315 }
1316
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1319 dynamic state. */
1320 if (hi != h
1321 && !newdyn
1322 && olddyn)
1323 {
1324 h = hi;
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 h->def_dynamic = 0;
1329 h->dynamic_def = 0;
1330 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1331 {
1332 h->root.type = bfd_link_hash_undefined;
1333 h->root.u.undef.abfd = abfd;
1334 }
1335 else
1336 {
1337 h->root.type = bfd_link_hash_new;
1338 h->root.u.undef.abfd = NULL;
1339 }
1340 return TRUE;
1341 }
1342 }
1343
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1347 if (oldbfd != NULL
1348 && (oldbfd->flags & BFD_PLUGIN) == 0
1349 && (abfd->flags & BFD_PLUGIN) == 0
1350 && ELF_ST_TYPE (sym->st_info) != h->type
1351 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1352 {
1353 bfd *ntbfd, *tbfd;
1354 bfd_boolean ntdef, tdef;
1355 asection *ntsec, *tsec;
1356
1357 if (h->type == STT_TLS)
1358 {
1359 ntbfd = abfd;
1360 ntsec = sec;
1361 ntdef = newdef;
1362 tbfd = oldbfd;
1363 tsec = oldsec;
1364 tdef = olddef;
1365 }
1366 else
1367 {
1368 ntbfd = oldbfd;
1369 ntsec = oldsec;
1370 ntdef = olddef;
1371 tbfd = abfd;
1372 tsec = sec;
1373 tdef = newdef;
1374 }
1375
1376 if (tdef && ntdef)
1377 _bfd_error_handler
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
1381 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1382 else if (!tdef && !ntdef)
1383 _bfd_error_handler
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
1387 h->root.root.string, tbfd, ntbfd);
1388 else if (tdef)
1389 _bfd_error_handler
1390 /* xgettext:c-format */
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
1393 h->root.root.string, tbfd, tsec, ntbfd);
1394 else
1395 _bfd_error_handler
1396 /* xgettext:c-format */
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h->root.root.string, tbfd, ntbfd, ntsec);
1400
1401 bfd_set_error (bfd_error_bad_value);
1402 return FALSE;
1403 }
1404
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1407 if (newdyn
1408 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1409 && !bfd_is_und_section (sec))
1410 {
1411 *skip = TRUE;
1412 /* Make sure this symbol is dynamic. */
1413 h->ref_dynamic = 1;
1414 hi->ref_dynamic = 1;
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1417
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1420 return bfd_elf_link_record_dynamic_symbol (info, h);
1421 else
1422 return TRUE;
1423 }
1424 else if (!newdyn
1425 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1426 && h->def_dynamic)
1427 {
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
1431 if (hi->root.type == bfd_link_hash_indirect)
1432 {
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1437 if (h->ref_regular)
1438 {
1439 hi->root.type = h->root.type;
1440 h->root.type = bfd_link_hash_indirect;
1441 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1442
1443 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1444 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1445 {
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 h->forced_local = 0;
1450 h->ref_dynamic = 0;
1451 }
1452 else
1453 h->ref_dynamic = 1;
1454
1455 h->def_dynamic = 0;
1456 /* FIXME: Should we check type and size for protected symbol? */
1457 h->size = 0;
1458 h->type = 0;
1459
1460 h = hi;
1461 }
1462 else
1463 h = hi;
1464 }
1465
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1474 {
1475 h->root.type = bfd_link_hash_undefined;
1476 h->root.u.undef.abfd = abfd;
1477 }
1478 else
1479 {
1480 h->root.type = bfd_link_hash_new;
1481 h->root.u.undef.abfd = NULL;
1482 }
1483
1484 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1485 {
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1489 h->forced_local = 0;
1490 h->ref_dynamic = 0;
1491 }
1492 else
1493 h->ref_dynamic = 1;
1494 h->def_dynamic = 0;
1495 /* FIXME: Should we check type and size for protected symbol? */
1496 h->size = 0;
1497 h->type = 0;
1498 return TRUE;
1499 }
1500
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1508
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1513
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1516
1517 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1518 newweak = FALSE;
1519 if (olddef && newdyn)
1520 oldweak = FALSE;
1521
1522 /* Allow changes between different types of function symbol. */
1523 if (newfunc && oldfunc)
1524 *type_change_ok = TRUE;
1525
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
1529
1530 if (oldweak
1531 || newweak
1532 || (newdef
1533 && h->root.type == bfd_link_hash_undefined))
1534 *type_change_ok = TRUE;
1535
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1538
1539 if (*type_change_ok
1540 || h->root.type == bfd_link_hash_undefined)
1541 *size_change_ok = TRUE;
1542
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1554 libraries.
1555
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1558
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1564 harmless. */
1565
1566 if (newdyn
1567 && newdef
1568 && !newweak
1569 && (sec->flags & SEC_ALLOC) != 0
1570 && (sec->flags & SEC_LOAD) == 0
1571 && sym->st_size > 0
1572 && !newfunc)
1573 newdyncommon = TRUE;
1574 else
1575 newdyncommon = FALSE;
1576
1577 if (olddyn
1578 && olddef
1579 && h->root.type == bfd_link_hash_defined
1580 && h->def_dynamic
1581 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1582 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1583 && h->size > 0
1584 && !oldfunc)
1585 olddyncommon = TRUE;
1586 else
1587 olddyncommon = FALSE;
1588
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
1591 if (bed->merge_symbol != NULL)
1592 {
1593 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1594 return FALSE;
1595 sec = *psec;
1596 }
1597
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
1600 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1601 && !default_sym && h->def_regular
1602 && !(oldbfd != NULL
1603 && (oldbfd->flags & BFD_PLUGIN) != 0
1604 && (abfd->flags & BFD_PLUGIN) == 0))
1605 {
1606 /* Handle a multiple definition. */
1607 (*info->callbacks->multiple_definition) (info, &h->root,
1608 abfd, sec, *pvalue);
1609 *skip = TRUE;
1610 return TRUE;
1611 }
1612
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1615 two. */
1616
1617 if (olddyncommon
1618 && newdyncommon
1619 && sym->st_size != h->size)
1620 {
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1626
1627 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1628 bfd_link_hash_common, sym->st_size);
1629 if (sym->st_size > h->size)
1630 h->size = sym->st_size;
1631
1632 *size_change_ok = TRUE;
1633 }
1634
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1641
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
1647 object to override a weak symbol in a shared object. */
1648
1649 if (newdyn
1650 && newdef
1651 && (olddef
1652 || (h->root.type == bfd_link_hash_common
1653 && (newweak || newfunc))))
1654 {
1655 *override = abfd;
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
1658
1659 *psec = sec = bfd_und_section_ptr;
1660 *size_change_ok = TRUE;
1661
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
1667
1668 if (h->root.type == bfd_link_hash_common)
1669 *type_change_ok = TRUE;
1670 }
1671
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1676 right thing. */
1677
1678 if (newdyncommon
1679 && h->root.type == bfd_link_hash_common)
1680 {
1681 *override = oldbfd;
1682 newdef = FALSE;
1683 newdyncommon = FALSE;
1684 *pvalue = sym->st_size;
1685 *psec = sec = bed->common_section (oldsec);
1686 *size_change_ok = TRUE;
1687 }
1688
1689 /* Skip weak definitions of symbols that are already defined. */
1690 if (newdef && olddef && newweak)
1691 {
1692 /* Don't skip new non-IR weak syms. */
1693 if (!(oldbfd != NULL
1694 && (oldbfd->flags & BFD_PLUGIN) != 0
1695 && (abfd->flags & BFD_PLUGIN) == 0))
1696 {
1697 newdef = FALSE;
1698 *skip = TRUE;
1699 }
1700
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1703 local symbol. */
1704 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1705 if (h->dynindx != -1)
1706 switch (ELF_ST_VISIBILITY (h->other))
1707 {
1708 case STV_INTERNAL:
1709 case STV_HIDDEN:
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1711 break;
1712 }
1713 }
1714
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1720
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
1723 symbol is a function or is weak. */
1724
1725 flip = NULL;
1726 if (!newdyn
1727 && (newdef
1728 || (bfd_is_com_section (sec)
1729 && (oldweak || oldfunc)))
1730 && olddyn
1731 && olddef
1732 && h->def_dynamic)
1733 {
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1736 new definition. */
1737
1738 h->root.type = bfd_link_hash_undefined;
1739 h->root.u.undef.abfd = h->root.u.def.section->owner;
1740 *size_change_ok = TRUE;
1741
1742 olddef = FALSE;
1743 olddyncommon = FALSE;
1744
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1747
1748 if (bfd_is_com_section (sec))
1749 {
1750 if (oldfunc)
1751 {
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1754 function. */
1755 h->def_dynamic = 0;
1756 h->type = STT_NOTYPE;
1757 }
1758 *type_change_ok = TRUE;
1759 }
1760
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
1763 else
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h->verinfo.vertree = NULL;
1768 }
1769
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1775
1776 if (! newdyn
1777 && bfd_is_com_section (sec)
1778 && olddyncommon)
1779 {
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1783 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1784 bfd_link_hash_common, sym->st_size);
1785
1786 /* If the presumed common symbol in the dynamic object is
1787 larger, pretend that the new symbol has its size. */
1788
1789 if (h->size > *pvalue)
1790 *pvalue = h->size;
1791
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment);
1795 *pold_alignment = h->root.u.def.section->alignment_power;
1796
1797 olddef = FALSE;
1798 olddyncommon = FALSE;
1799
1800 h->root.type = bfd_link_hash_undefined;
1801 h->root.u.undef.abfd = h->root.u.def.section->owner;
1802
1803 *size_change_ok = TRUE;
1804 *type_change_ok = TRUE;
1805
1806 if (hi->root.type == bfd_link_hash_indirect)
1807 flip = hi;
1808 else
1809 h->verinfo.vertree = NULL;
1810 }
1811
1812 if (flip != NULL)
1813 {
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
1817 flip->root.type = h->root.type;
1818 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1819 h->root.type = bfd_link_hash_indirect;
1820 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1821 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1822 if (h->def_dynamic)
1823 {
1824 h->def_dynamic = 0;
1825 flip->ref_dynamic = 1;
1826 }
1827 }
1828
1829 return TRUE;
1830 }
1831
1832 /* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1835 set DYNSYM if the new indirect symbol is dynamic. */
1836
1837 static bfd_boolean
1838 _bfd_elf_add_default_symbol (bfd *abfd,
1839 struct bfd_link_info *info,
1840 struct elf_link_hash_entry *h,
1841 const char *name,
1842 Elf_Internal_Sym *sym,
1843 asection *sec,
1844 bfd_vma value,
1845 bfd **poldbfd,
1846 bfd_boolean *dynsym)
1847 {
1848 bfd_boolean type_change_ok;
1849 bfd_boolean size_change_ok;
1850 bfd_boolean skip;
1851 char *shortname;
1852 struct elf_link_hash_entry *hi;
1853 struct bfd_link_hash_entry *bh;
1854 const struct elf_backend_data *bed;
1855 bfd_boolean collect;
1856 bfd_boolean dynamic;
1857 bfd *override;
1858 char *p;
1859 size_t len, shortlen;
1860 asection *tmp_sec;
1861 bfd_boolean matched;
1862
1863 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1864 return TRUE;
1865
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p = strchr (name, ELF_VER_CHR);
1871 if (h->versioned == unknown)
1872 {
1873 if (p == NULL)
1874 {
1875 h->versioned = unversioned;
1876 return TRUE;
1877 }
1878 else
1879 {
1880 if (p[1] != ELF_VER_CHR)
1881 {
1882 h->versioned = versioned_hidden;
1883 return TRUE;
1884 }
1885 else
1886 h->versioned = versioned;
1887 }
1888 }
1889 else
1890 {
1891 /* PR ld/19073: We may see an unversioned definition after the
1892 default version. */
1893 if (p == NULL)
1894 return TRUE;
1895 }
1896
1897 bed = get_elf_backend_data (abfd);
1898 collect = bed->collect;
1899 dynamic = (abfd->flags & DYNAMIC) != 0;
1900
1901 shortlen = p - name;
1902 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1903 if (shortname == NULL)
1904 return FALSE;
1905 memcpy (shortname, name, shortlen);
1906 shortname[shortlen] = '\0';
1907
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok = FALSE;
1913 size_change_ok = FALSE;
1914 matched = TRUE;
1915 tmp_sec = sec;
1916 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1917 &hi, poldbfd, NULL, NULL, &skip, &override,
1918 &type_change_ok, &size_change_ok, &matched))
1919 return FALSE;
1920
1921 if (skip)
1922 goto nondefault;
1923
1924 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1925 {
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1932 {
1933 bfd_boolean hide;
1934
1935 hi->verinfo.vertree
1936 = bfd_find_version_for_sym (info->version_info,
1937 hi->root.root.string, &hide);
1938 if (hi->verinfo.vertree != NULL && hide)
1939 {
1940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1941 goto nondefault;
1942 }
1943 }
1944 if (hi->verinfo.vertree != NULL
1945 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1946 goto nondefault;
1947 }
1948
1949 if (! override)
1950 {
1951 /* Add the default symbol if not performing a relocatable link. */
1952 if (! bfd_link_relocatable (info))
1953 {
1954 bh = &hi->root;
1955 if (bh->type == bfd_link_hash_defined
1956 && bh->u.def.section->owner != NULL
1957 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1958 {
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1961 it. */
1962 bh->type = bfd_link_hash_undefined;
1963 bh->u.undef.abfd = bh->u.def.section->owner;
1964 }
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info, abfd, shortname, BSF_INDIRECT,
1967 bfd_ind_section_ptr,
1968 0, name, FALSE, collect, &bh)))
1969 return FALSE;
1970 hi = (struct elf_link_hash_entry *) bh;
1971 }
1972 }
1973 else
1974 {
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1980
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1984
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1992
1993 while (hi->root.type == bfd_link_hash_indirect
1994 || hi->root.type == bfd_link_hash_warning)
1995 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996
1997 h->root.type = bfd_link_hash_indirect;
1998 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1999 if (h->def_dynamic)
2000 {
2001 h->def_dynamic = 0;
2002 hi->ref_dynamic = 1;
2003 if (hi->ref_regular
2004 || hi->def_regular)
2005 {
2006 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2007 return FALSE;
2008 }
2009 }
2010
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2013 hi = h;
2014 }
2015
2016 /* Check if HI is a warning symbol. */
2017 if (hi->root.type == bfd_link_hash_warning)
2018 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2019
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2023
2024 if (hi->root.type == bfd_link_hash_indirect)
2025 {
2026 struct elf_link_hash_entry *ht;
2027
2028 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2029 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2030
2031 /* If we first saw a reference to SHORTNAME with non-default
2032 visibility, merge that visibility to the @@VER symbol. */
2033 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2034
2035 /* A reference to the SHORTNAME symbol from a dynamic library
2036 will be satisfied by the versioned symbol at runtime. In
2037 effect, we have a reference to the versioned symbol. */
2038 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2039 hi->dynamic_def |= ht->dynamic_def;
2040
2041 /* See if the new flags lead us to realize that the symbol must
2042 be dynamic. */
2043 if (! *dynsym)
2044 {
2045 if (! dynamic)
2046 {
2047 if (! bfd_link_executable (info)
2048 || hi->def_dynamic
2049 || hi->ref_dynamic)
2050 *dynsym = TRUE;
2051 }
2052 else
2053 {
2054 if (hi->ref_regular)
2055 *dynsym = TRUE;
2056 }
2057 }
2058 }
2059
2060 /* We also need to define an indirection from the nondefault version
2061 of the symbol. */
2062
2063 nondefault:
2064 len = strlen (name);
2065 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2066 if (shortname == NULL)
2067 return FALSE;
2068 memcpy (shortname, name, shortlen);
2069 memcpy (shortname + shortlen, p + 1, len - shortlen);
2070
2071 /* Once again, merge with any existing symbol. */
2072 type_change_ok = FALSE;
2073 size_change_ok = FALSE;
2074 tmp_sec = sec;
2075 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2076 &hi, poldbfd, NULL, NULL, &skip, &override,
2077 &type_change_ok, &size_change_ok, &matched))
2078 return FALSE;
2079
2080 if (skip)
2081 return TRUE;
2082
2083 if (override)
2084 {
2085 /* Here SHORTNAME is a versioned name, so we don't expect to see
2086 the type of override we do in the case above unless it is
2087 overridden by a versioned definition. */
2088 if (hi->root.type != bfd_link_hash_defined
2089 && hi->root.type != bfd_link_hash_defweak)
2090 _bfd_error_handler
2091 /* xgettext:c-format */
2092 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2093 abfd, shortname);
2094 }
2095 else
2096 {
2097 bh = &hi->root;
2098 if (! (_bfd_generic_link_add_one_symbol
2099 (info, abfd, shortname, BSF_INDIRECT,
2100 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2101 return FALSE;
2102 hi = (struct elf_link_hash_entry *) bh;
2103
2104 /* If there is a duplicate definition somewhere, then HI may not
2105 point to an indirect symbol. We will have reported an error
2106 to the user in that case. */
2107
2108 if (hi->root.type == bfd_link_hash_indirect)
2109 {
2110 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2111 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2112 hi->dynamic_def |= h->dynamic_def;
2113
2114 /* If we first saw a reference to @VER symbol with
2115 non-default visibility, merge that visibility to the
2116 @@VER symbol. */
2117 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2118
2119 /* See if the new flags lead us to realize that the symbol
2120 must be dynamic. */
2121 if (! *dynsym)
2122 {
2123 if (! dynamic)
2124 {
2125 if (! bfd_link_executable (info)
2126 || hi->ref_dynamic)
2127 *dynsym = TRUE;
2128 }
2129 else
2130 {
2131 if (hi->ref_regular)
2132 *dynsym = TRUE;
2133 }
2134 }
2135 }
2136 }
2137
2138 return TRUE;
2139 }
2140 \f
2141 /* This routine is used to export all defined symbols into the dynamic
2142 symbol table. It is called via elf_link_hash_traverse. */
2143
2144 static bfd_boolean
2145 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2146 {
2147 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2148
2149 /* Ignore indirect symbols. These are added by the versioning code. */
2150 if (h->root.type == bfd_link_hash_indirect)
2151 return TRUE;
2152
2153 /* Ignore this if we won't export it. */
2154 if (!eif->info->export_dynamic && !h->dynamic)
2155 return TRUE;
2156
2157 if (h->dynindx == -1
2158 && (h->def_regular || h->ref_regular)
2159 && ! bfd_hide_sym_by_version (eif->info->version_info,
2160 h->root.root.string))
2161 {
2162 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2163 {
2164 eif->failed = TRUE;
2165 return FALSE;
2166 }
2167 }
2168
2169 return TRUE;
2170 }
2171 \f
2172 /* Look through the symbols which are defined in other shared
2173 libraries and referenced here. Update the list of version
2174 dependencies. This will be put into the .gnu.version_r section.
2175 This function is called via elf_link_hash_traverse. */
2176
2177 static bfd_boolean
2178 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2179 void *data)
2180 {
2181 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2182 Elf_Internal_Verneed *t;
2183 Elf_Internal_Vernaux *a;
2184 size_t amt;
2185
2186 /* We only care about symbols defined in shared objects with version
2187 information. */
2188 if (!h->def_dynamic
2189 || h->def_regular
2190 || h->dynindx == -1
2191 || h->verinfo.verdef == NULL
2192 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2193 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2194 return TRUE;
2195
2196 /* See if we already know about this version. */
2197 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2198 t != NULL;
2199 t = t->vn_nextref)
2200 {
2201 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2202 continue;
2203
2204 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2205 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2206 return TRUE;
2207
2208 break;
2209 }
2210
2211 /* This is a new version. Add it to tree we are building. */
2212
2213 if (t == NULL)
2214 {
2215 amt = sizeof *t;
2216 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2217 if (t == NULL)
2218 {
2219 rinfo->failed = TRUE;
2220 return FALSE;
2221 }
2222
2223 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2224 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2225 elf_tdata (rinfo->info->output_bfd)->verref = t;
2226 }
2227
2228 amt = sizeof *a;
2229 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2230 if (a == NULL)
2231 {
2232 rinfo->failed = TRUE;
2233 return FALSE;
2234 }
2235
2236 /* Note that we are copying a string pointer here, and testing it
2237 above. If bfd_elf_string_from_elf_section is ever changed to
2238 discard the string data when low in memory, this will have to be
2239 fixed. */
2240 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2241
2242 a->vna_flags = h->verinfo.verdef->vd_flags;
2243 a->vna_nextptr = t->vn_auxptr;
2244
2245 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2246 ++rinfo->vers;
2247
2248 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2249
2250 t->vn_auxptr = a;
2251
2252 return TRUE;
2253 }
2254
2255 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2256 hidden. Set *T_P to NULL if there is no match. */
2257
2258 static bfd_boolean
2259 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2260 struct elf_link_hash_entry *h,
2261 const char *version_p,
2262 struct bfd_elf_version_tree **t_p,
2263 bfd_boolean *hide)
2264 {
2265 struct bfd_elf_version_tree *t;
2266
2267 /* Look for the version. If we find it, it is no longer weak. */
2268 for (t = info->version_info; t != NULL; t = t->next)
2269 {
2270 if (strcmp (t->name, version_p) == 0)
2271 {
2272 size_t len;
2273 char *alc;
2274 struct bfd_elf_version_expr *d;
2275
2276 len = version_p - h->root.root.string;
2277 alc = (char *) bfd_malloc (len);
2278 if (alc == NULL)
2279 return FALSE;
2280 memcpy (alc, h->root.root.string, len - 1);
2281 alc[len - 1] = '\0';
2282 if (alc[len - 2] == ELF_VER_CHR)
2283 alc[len - 2] = '\0';
2284
2285 h->verinfo.vertree = t;
2286 t->used = TRUE;
2287 d = NULL;
2288
2289 if (t->globals.list != NULL)
2290 d = (*t->match) (&t->globals, NULL, alc);
2291
2292 /* See if there is anything to force this symbol to
2293 local scope. */
2294 if (d == NULL && t->locals.list != NULL)
2295 {
2296 d = (*t->match) (&t->locals, NULL, alc);
2297 if (d != NULL
2298 && h->dynindx != -1
2299 && ! info->export_dynamic)
2300 *hide = TRUE;
2301 }
2302
2303 free (alc);
2304 break;
2305 }
2306 }
2307
2308 *t_p = t;
2309
2310 return TRUE;
2311 }
2312
2313 /* Return TRUE if the symbol H is hidden by version script. */
2314
2315 bfd_boolean
2316 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2317 struct elf_link_hash_entry *h)
2318 {
2319 const char *p;
2320 bfd_boolean hide = FALSE;
2321 const struct elf_backend_data *bed
2322 = get_elf_backend_data (info->output_bfd);
2323
2324 /* Version script only hides symbols defined in regular objects. */
2325 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2326 return TRUE;
2327
2328 p = strchr (h->root.root.string, ELF_VER_CHR);
2329 if (p != NULL && h->verinfo.vertree == NULL)
2330 {
2331 struct bfd_elf_version_tree *t;
2332
2333 ++p;
2334 if (*p == ELF_VER_CHR)
2335 ++p;
2336
2337 if (*p != '\0'
2338 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2339 && hide)
2340 {
2341 if (hide)
2342 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2343 return TRUE;
2344 }
2345 }
2346
2347 /* If we don't have a version for this symbol, see if we can find
2348 something. */
2349 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2350 {
2351 h->verinfo.vertree
2352 = bfd_find_version_for_sym (info->version_info,
2353 h->root.root.string, &hide);
2354 if (h->verinfo.vertree != NULL && hide)
2355 {
2356 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2357 return TRUE;
2358 }
2359 }
2360
2361 return FALSE;
2362 }
2363
2364 /* Figure out appropriate versions for all the symbols. We may not
2365 have the version number script until we have read all of the input
2366 files, so until that point we don't know which symbols should be
2367 local. This function is called via elf_link_hash_traverse. */
2368
2369 static bfd_boolean
2370 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2371 {
2372 struct elf_info_failed *sinfo;
2373 struct bfd_link_info *info;
2374 const struct elf_backend_data *bed;
2375 struct elf_info_failed eif;
2376 char *p;
2377 bfd_boolean hide;
2378
2379 sinfo = (struct elf_info_failed *) data;
2380 info = sinfo->info;
2381
2382 /* Fix the symbol flags. */
2383 eif.failed = FALSE;
2384 eif.info = info;
2385 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2386 {
2387 if (eif.failed)
2388 sinfo->failed = TRUE;
2389 return FALSE;
2390 }
2391
2392 bed = get_elf_backend_data (info->output_bfd);
2393
2394 /* We only need version numbers for symbols defined in regular
2395 objects. */
2396 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2397 {
2398 /* Hide symbols defined in discarded input sections. */
2399 if ((h->root.type == bfd_link_hash_defined
2400 || h->root.type == bfd_link_hash_defweak)
2401 && discarded_section (h->root.u.def.section))
2402 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2403 return TRUE;
2404 }
2405
2406 hide = FALSE;
2407 p = strchr (h->root.root.string, ELF_VER_CHR);
2408 if (p != NULL && h->verinfo.vertree == NULL)
2409 {
2410 struct bfd_elf_version_tree *t;
2411
2412 ++p;
2413 if (*p == ELF_VER_CHR)
2414 ++p;
2415
2416 /* If there is no version string, we can just return out. */
2417 if (*p == '\0')
2418 return TRUE;
2419
2420 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2421 {
2422 sinfo->failed = TRUE;
2423 return FALSE;
2424 }
2425
2426 if (hide)
2427 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2428
2429 /* If we are building an application, we need to create a
2430 version node for this version. */
2431 if (t == NULL && bfd_link_executable (info))
2432 {
2433 struct bfd_elf_version_tree **pp;
2434 int version_index;
2435
2436 /* If we aren't going to export this symbol, we don't need
2437 to worry about it. */
2438 if (h->dynindx == -1)
2439 return TRUE;
2440
2441 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2442 sizeof *t);
2443 if (t == NULL)
2444 {
2445 sinfo->failed = TRUE;
2446 return FALSE;
2447 }
2448
2449 t->name = p;
2450 t->name_indx = (unsigned int) -1;
2451 t->used = TRUE;
2452
2453 version_index = 1;
2454 /* Don't count anonymous version tag. */
2455 if (sinfo->info->version_info != NULL
2456 && sinfo->info->version_info->vernum == 0)
2457 version_index = 0;
2458 for (pp = &sinfo->info->version_info;
2459 *pp != NULL;
2460 pp = &(*pp)->next)
2461 ++version_index;
2462 t->vernum = version_index;
2463
2464 *pp = t;
2465
2466 h->verinfo.vertree = t;
2467 }
2468 else if (t == NULL)
2469 {
2470 /* We could not find the version for a symbol when
2471 generating a shared archive. Return an error. */
2472 _bfd_error_handler
2473 /* xgettext:c-format */
2474 (_("%pB: version node not found for symbol %s"),
2475 info->output_bfd, h->root.root.string);
2476 bfd_set_error (bfd_error_bad_value);
2477 sinfo->failed = TRUE;
2478 return FALSE;
2479 }
2480 }
2481
2482 /* If we don't have a version for this symbol, see if we can find
2483 something. */
2484 if (!hide
2485 && h->verinfo.vertree == NULL
2486 && sinfo->info->version_info != NULL)
2487 {
2488 h->verinfo.vertree
2489 = bfd_find_version_for_sym (sinfo->info->version_info,
2490 h->root.root.string, &hide);
2491 if (h->verinfo.vertree != NULL && hide)
2492 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2493 }
2494
2495 return TRUE;
2496 }
2497 \f
2498 /* Read and swap the relocs from the section indicated by SHDR. This
2499 may be either a REL or a RELA section. The relocations are
2500 translated into RELA relocations and stored in INTERNAL_RELOCS,
2501 which should have already been allocated to contain enough space.
2502 The EXTERNAL_RELOCS are a buffer where the external form of the
2503 relocations should be stored.
2504
2505 Returns FALSE if something goes wrong. */
2506
2507 static bfd_boolean
2508 elf_link_read_relocs_from_section (bfd *abfd,
2509 asection *sec,
2510 Elf_Internal_Shdr *shdr,
2511 void *external_relocs,
2512 Elf_Internal_Rela *internal_relocs)
2513 {
2514 const struct elf_backend_data *bed;
2515 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2516 const bfd_byte *erela;
2517 const bfd_byte *erelaend;
2518 Elf_Internal_Rela *irela;
2519 Elf_Internal_Shdr *symtab_hdr;
2520 size_t nsyms;
2521
2522 /* Position ourselves at the start of the section. */
2523 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2524 return FALSE;
2525
2526 /* Read the relocations. */
2527 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2528 return FALSE;
2529
2530 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2531 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2532
2533 bed = get_elf_backend_data (abfd);
2534
2535 /* Convert the external relocations to the internal format. */
2536 if (shdr->sh_entsize == bed->s->sizeof_rel)
2537 swap_in = bed->s->swap_reloc_in;
2538 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2539 swap_in = bed->s->swap_reloca_in;
2540 else
2541 {
2542 bfd_set_error (bfd_error_wrong_format);
2543 return FALSE;
2544 }
2545
2546 erela = (const bfd_byte *) external_relocs;
2547 /* Setting erelaend like this and comparing with <= handles case of
2548 a fuzzed object with sh_size not a multiple of sh_entsize. */
2549 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2550 irela = internal_relocs;
2551 while (erela <= erelaend)
2552 {
2553 bfd_vma r_symndx;
2554
2555 (*swap_in) (abfd, erela, irela);
2556 r_symndx = ELF32_R_SYM (irela->r_info);
2557 if (bed->s->arch_size == 64)
2558 r_symndx >>= 24;
2559 if (nsyms > 0)
2560 {
2561 if ((size_t) r_symndx >= nsyms)
2562 {
2563 _bfd_error_handler
2564 /* xgettext:c-format */
2565 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2566 " for offset %#" PRIx64 " in section `%pA'"),
2567 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2568 (uint64_t) irela->r_offset, sec);
2569 bfd_set_error (bfd_error_bad_value);
2570 return FALSE;
2571 }
2572 }
2573 else if (r_symndx != STN_UNDEF)
2574 {
2575 _bfd_error_handler
2576 /* xgettext:c-format */
2577 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2578 " for offset %#" PRIx64 " in section `%pA'"
2579 " when the object file has no symbol table"),
2580 abfd, (uint64_t) r_symndx,
2581 (uint64_t) irela->r_offset, sec);
2582 bfd_set_error (bfd_error_bad_value);
2583 return FALSE;
2584 }
2585 irela += bed->s->int_rels_per_ext_rel;
2586 erela += shdr->sh_entsize;
2587 }
2588
2589 return TRUE;
2590 }
2591
2592 /* Read and swap the relocs for a section O. They may have been
2593 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2594 not NULL, they are used as buffers to read into. They are known to
2595 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2596 the return value is allocated using either malloc or bfd_alloc,
2597 according to the KEEP_MEMORY argument. If O has two relocation
2598 sections (both REL and RELA relocations), then the REL_HDR
2599 relocations will appear first in INTERNAL_RELOCS, followed by the
2600 RELA_HDR relocations. */
2601
2602 Elf_Internal_Rela *
2603 _bfd_elf_link_read_relocs (bfd *abfd,
2604 asection *o,
2605 void *external_relocs,
2606 Elf_Internal_Rela *internal_relocs,
2607 bfd_boolean keep_memory)
2608 {
2609 void *alloc1 = NULL;
2610 Elf_Internal_Rela *alloc2 = NULL;
2611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2612 struct bfd_elf_section_data *esdo = elf_section_data (o);
2613 Elf_Internal_Rela *internal_rela_relocs;
2614
2615 if (esdo->relocs != NULL)
2616 return esdo->relocs;
2617
2618 if (o->reloc_count == 0)
2619 return NULL;
2620
2621 if (internal_relocs == NULL)
2622 {
2623 bfd_size_type size;
2624
2625 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2626 if (keep_memory)
2627 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2628 else
2629 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2630 if (internal_relocs == NULL)
2631 goto error_return;
2632 }
2633
2634 if (external_relocs == NULL)
2635 {
2636 bfd_size_type size = 0;
2637
2638 if (esdo->rel.hdr)
2639 size += esdo->rel.hdr->sh_size;
2640 if (esdo->rela.hdr)
2641 size += esdo->rela.hdr->sh_size;
2642
2643 alloc1 = bfd_malloc (size);
2644 if (alloc1 == NULL)
2645 goto error_return;
2646 external_relocs = alloc1;
2647 }
2648
2649 internal_rela_relocs = internal_relocs;
2650 if (esdo->rel.hdr)
2651 {
2652 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2653 external_relocs,
2654 internal_relocs))
2655 goto error_return;
2656 external_relocs = (((bfd_byte *) external_relocs)
2657 + esdo->rel.hdr->sh_size);
2658 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2659 * bed->s->int_rels_per_ext_rel);
2660 }
2661
2662 if (esdo->rela.hdr
2663 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2664 external_relocs,
2665 internal_rela_relocs)))
2666 goto error_return;
2667
2668 /* Cache the results for next time, if we can. */
2669 if (keep_memory)
2670 esdo->relocs = internal_relocs;
2671
2672 free (alloc1);
2673
2674 /* Don't free alloc2, since if it was allocated we are passing it
2675 back (under the name of internal_relocs). */
2676
2677 return internal_relocs;
2678
2679 error_return:
2680 free (alloc1);
2681 if (alloc2 != NULL)
2682 {
2683 if (keep_memory)
2684 bfd_release (abfd, alloc2);
2685 else
2686 free (alloc2);
2687 }
2688 return NULL;
2689 }
2690
2691 /* Compute the size of, and allocate space for, REL_HDR which is the
2692 section header for a section containing relocations for O. */
2693
2694 static bfd_boolean
2695 _bfd_elf_link_size_reloc_section (bfd *abfd,
2696 struct bfd_elf_section_reloc_data *reldata)
2697 {
2698 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2699
2700 /* That allows us to calculate the size of the section. */
2701 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2702
2703 /* The contents field must last into write_object_contents, so we
2704 allocate it with bfd_alloc rather than malloc. Also since we
2705 cannot be sure that the contents will actually be filled in,
2706 we zero the allocated space. */
2707 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2708 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2709 return FALSE;
2710
2711 if (reldata->hashes == NULL && reldata->count)
2712 {
2713 struct elf_link_hash_entry **p;
2714
2715 p = ((struct elf_link_hash_entry **)
2716 bfd_zmalloc (reldata->count * sizeof (*p)));
2717 if (p == NULL)
2718 return FALSE;
2719
2720 reldata->hashes = p;
2721 }
2722
2723 return TRUE;
2724 }
2725
2726 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2727 originated from the section given by INPUT_REL_HDR) to the
2728 OUTPUT_BFD. */
2729
2730 bfd_boolean
2731 _bfd_elf_link_output_relocs (bfd *output_bfd,
2732 asection *input_section,
2733 Elf_Internal_Shdr *input_rel_hdr,
2734 Elf_Internal_Rela *internal_relocs,
2735 struct elf_link_hash_entry **rel_hash
2736 ATTRIBUTE_UNUSED)
2737 {
2738 Elf_Internal_Rela *irela;
2739 Elf_Internal_Rela *irelaend;
2740 bfd_byte *erel;
2741 struct bfd_elf_section_reloc_data *output_reldata;
2742 asection *output_section;
2743 const struct elf_backend_data *bed;
2744 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2745 struct bfd_elf_section_data *esdo;
2746
2747 output_section = input_section->output_section;
2748
2749 bed = get_elf_backend_data (output_bfd);
2750 esdo = elf_section_data (output_section);
2751 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2752 {
2753 output_reldata = &esdo->rel;
2754 swap_out = bed->s->swap_reloc_out;
2755 }
2756 else if (esdo->rela.hdr
2757 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2758 {
2759 output_reldata = &esdo->rela;
2760 swap_out = bed->s->swap_reloca_out;
2761 }
2762 else
2763 {
2764 _bfd_error_handler
2765 /* xgettext:c-format */
2766 (_("%pB: relocation size mismatch in %pB section %pA"),
2767 output_bfd, input_section->owner, input_section);
2768 bfd_set_error (bfd_error_wrong_format);
2769 return FALSE;
2770 }
2771
2772 erel = output_reldata->hdr->contents;
2773 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2774 irela = internal_relocs;
2775 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2776 * bed->s->int_rels_per_ext_rel);
2777 while (irela < irelaend)
2778 {
2779 (*swap_out) (output_bfd, irela, erel);
2780 irela += bed->s->int_rels_per_ext_rel;
2781 erel += input_rel_hdr->sh_entsize;
2782 }
2783
2784 /* Bump the counter, so that we know where to add the next set of
2785 relocations. */
2786 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2787
2788 return TRUE;
2789 }
2790 \f
2791 /* Make weak undefined symbols in PIE dynamic. */
2792
2793 bfd_boolean
2794 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2795 struct elf_link_hash_entry *h)
2796 {
2797 if (bfd_link_pie (info)
2798 && h->dynindx == -1
2799 && h->root.type == bfd_link_hash_undefweak)
2800 return bfd_elf_link_record_dynamic_symbol (info, h);
2801
2802 return TRUE;
2803 }
2804
2805 /* Fix up the flags for a symbol. This handles various cases which
2806 can only be fixed after all the input files are seen. This is
2807 currently called by both adjust_dynamic_symbol and
2808 assign_sym_version, which is unnecessary but perhaps more robust in
2809 the face of future changes. */
2810
2811 static bfd_boolean
2812 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2813 struct elf_info_failed *eif)
2814 {
2815 const struct elf_backend_data *bed;
2816
2817 /* If this symbol was mentioned in a non-ELF file, try to set
2818 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2819 permit a non-ELF file to correctly refer to a symbol defined in
2820 an ELF dynamic object. */
2821 if (h->non_elf)
2822 {
2823 while (h->root.type == bfd_link_hash_indirect)
2824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2825
2826 if (h->root.type != bfd_link_hash_defined
2827 && h->root.type != bfd_link_hash_defweak)
2828 {
2829 h->ref_regular = 1;
2830 h->ref_regular_nonweak = 1;
2831 }
2832 else
2833 {
2834 if (h->root.u.def.section->owner != NULL
2835 && (bfd_get_flavour (h->root.u.def.section->owner)
2836 == bfd_target_elf_flavour))
2837 {
2838 h->ref_regular = 1;
2839 h->ref_regular_nonweak = 1;
2840 }
2841 else
2842 h->def_regular = 1;
2843 }
2844
2845 if (h->dynindx == -1
2846 && (h->def_dynamic
2847 || h->ref_dynamic))
2848 {
2849 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2850 {
2851 eif->failed = TRUE;
2852 return FALSE;
2853 }
2854 }
2855 }
2856 else
2857 {
2858 /* Unfortunately, NON_ELF is only correct if the symbol
2859 was first seen in a non-ELF file. Fortunately, if the symbol
2860 was first seen in an ELF file, we're probably OK unless the
2861 symbol was defined in a non-ELF file. Catch that case here.
2862 FIXME: We're still in trouble if the symbol was first seen in
2863 a dynamic object, and then later in a non-ELF regular object. */
2864 if ((h->root.type == bfd_link_hash_defined
2865 || h->root.type == bfd_link_hash_defweak)
2866 && !h->def_regular
2867 && (h->root.u.def.section->owner != NULL
2868 ? (bfd_get_flavour (h->root.u.def.section->owner)
2869 != bfd_target_elf_flavour)
2870 : (bfd_is_abs_section (h->root.u.def.section)
2871 && !h->def_dynamic)))
2872 h->def_regular = 1;
2873 }
2874
2875 /* Backend specific symbol fixup. */
2876 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2877 if (bed->elf_backend_fixup_symbol
2878 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2879 return FALSE;
2880
2881 /* If this is a final link, and the symbol was defined as a common
2882 symbol in a regular object file, and there was no definition in
2883 any dynamic object, then the linker will have allocated space for
2884 the symbol in a common section but the DEF_REGULAR
2885 flag will not have been set. */
2886 if (h->root.type == bfd_link_hash_defined
2887 && !h->def_regular
2888 && h->ref_regular
2889 && !h->def_dynamic
2890 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2891 h->def_regular = 1;
2892
2893 /* Symbols defined in discarded sections shouldn't be dynamic. */
2894 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2895 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2896
2897 /* If a weak undefined symbol has non-default visibility, we also
2898 hide it from the dynamic linker. */
2899 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2900 && h->root.type == bfd_link_hash_undefweak)
2901 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2902
2903 /* A hidden versioned symbol in executable should be forced local if
2904 it is is locally defined, not referenced by shared library and not
2905 exported. */
2906 else if (bfd_link_executable (eif->info)
2907 && h->versioned == versioned_hidden
2908 && !eif->info->export_dynamic
2909 && !h->dynamic
2910 && !h->ref_dynamic
2911 && h->def_regular)
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2913
2914 /* If -Bsymbolic was used (which means to bind references to global
2915 symbols to the definition within the shared object), and this
2916 symbol was defined in a regular object, then it actually doesn't
2917 need a PLT entry. Likewise, if the symbol has non-default
2918 visibility. If the symbol has hidden or internal visibility, we
2919 will force it local. */
2920 else if (h->needs_plt
2921 && bfd_link_pic (eif->info)
2922 && is_elf_hash_table (eif->info->hash)
2923 && (SYMBOLIC_BIND (eif->info, h)
2924 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2925 && h->def_regular)
2926 {
2927 bfd_boolean force_local;
2928
2929 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2930 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2931 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2932 }
2933
2934 /* If this is a weak defined symbol in a dynamic object, and we know
2935 the real definition in the dynamic object, copy interesting flags
2936 over to the real definition. */
2937 if (h->is_weakalias)
2938 {
2939 struct elf_link_hash_entry *def = weakdef (h);
2940
2941 /* If the real definition is defined by a regular object file,
2942 don't do anything special. See the longer description in
2943 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2944 bfd_link_hash_defined as it was when put on the alias list
2945 then it must have originally been a versioned symbol (for
2946 which a non-versioned indirect symbol is created) and later
2947 a definition for the non-versioned symbol is found. In that
2948 case the indirection is flipped with the versioned symbol
2949 becoming an indirect pointing at the non-versioned symbol.
2950 Thus, not an alias any more. */
2951 if (def->def_regular
2952 || def->root.type != bfd_link_hash_defined)
2953 {
2954 h = def;
2955 while ((h = h->u.alias) != def)
2956 h->is_weakalias = 0;
2957 }
2958 else
2959 {
2960 while (h->root.type == bfd_link_hash_indirect)
2961 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2962 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2963 || h->root.type == bfd_link_hash_defweak);
2964 BFD_ASSERT (def->def_dynamic);
2965 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2966 }
2967 }
2968
2969 return TRUE;
2970 }
2971
2972 /* Make the backend pick a good value for a dynamic symbol. This is
2973 called via elf_link_hash_traverse, and also calls itself
2974 recursively. */
2975
2976 static bfd_boolean
2977 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2978 {
2979 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2980 struct elf_link_hash_table *htab;
2981 const struct elf_backend_data *bed;
2982
2983 if (! is_elf_hash_table (eif->info->hash))
2984 return FALSE;
2985
2986 /* Ignore indirect symbols. These are added by the versioning code. */
2987 if (h->root.type == bfd_link_hash_indirect)
2988 return TRUE;
2989
2990 /* Fix the symbol flags. */
2991 if (! _bfd_elf_fix_symbol_flags (h, eif))
2992 return FALSE;
2993
2994 htab = elf_hash_table (eif->info);
2995 bed = get_elf_backend_data (htab->dynobj);
2996
2997 if (h->root.type == bfd_link_hash_undefweak)
2998 {
2999 if (eif->info->dynamic_undefined_weak == 0)
3000 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
3001 else if (eif->info->dynamic_undefined_weak > 0
3002 && h->ref_regular
3003 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3004 && !bfd_hide_sym_by_version (eif->info->version_info,
3005 h->root.root.string))
3006 {
3007 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3008 {
3009 eif->failed = TRUE;
3010 return FALSE;
3011 }
3012 }
3013 }
3014
3015 /* If this symbol does not require a PLT entry, and it is not
3016 defined by a dynamic object, or is not referenced by a regular
3017 object, ignore it. We do have to handle a weak defined symbol,
3018 even if no regular object refers to it, if we decided to add it
3019 to the dynamic symbol table. FIXME: Do we normally need to worry
3020 about symbols which are defined by one dynamic object and
3021 referenced by another one? */
3022 if (!h->needs_plt
3023 && h->type != STT_GNU_IFUNC
3024 && (h->def_regular
3025 || !h->def_dynamic
3026 || (!h->ref_regular
3027 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3028 {
3029 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3030 return TRUE;
3031 }
3032
3033 /* If we've already adjusted this symbol, don't do it again. This
3034 can happen via a recursive call. */
3035 if (h->dynamic_adjusted)
3036 return TRUE;
3037
3038 /* Don't look at this symbol again. Note that we must set this
3039 after checking the above conditions, because we may look at a
3040 symbol once, decide not to do anything, and then get called
3041 recursively later after REF_REGULAR is set below. */
3042 h->dynamic_adjusted = 1;
3043
3044 /* If this is a weak definition, and we know a real definition, and
3045 the real symbol is not itself defined by a regular object file,
3046 then get a good value for the real definition. We handle the
3047 real symbol first, for the convenience of the backend routine.
3048
3049 Note that there is a confusing case here. If the real definition
3050 is defined by a regular object file, we don't get the real symbol
3051 from the dynamic object, but we do get the weak symbol. If the
3052 processor backend uses a COPY reloc, then if some routine in the
3053 dynamic object changes the real symbol, we will not see that
3054 change in the corresponding weak symbol. This is the way other
3055 ELF linkers work as well, and seems to be a result of the shared
3056 library model.
3057
3058 I will clarify this issue. Most SVR4 shared libraries define the
3059 variable _timezone and define timezone as a weak synonym. The
3060 tzset call changes _timezone. If you write
3061 extern int timezone;
3062 int _timezone = 5;
3063 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3064 you might expect that, since timezone is a synonym for _timezone,
3065 the same number will print both times. However, if the processor
3066 backend uses a COPY reloc, then actually timezone will be copied
3067 into your process image, and, since you define _timezone
3068 yourself, _timezone will not. Thus timezone and _timezone will
3069 wind up at different memory locations. The tzset call will set
3070 _timezone, leaving timezone unchanged. */
3071
3072 if (h->is_weakalias)
3073 {
3074 struct elf_link_hash_entry *def = weakdef (h);
3075
3076 /* If we get to this point, there is an implicit reference to
3077 the alias by a regular object file via the weak symbol H. */
3078 def->ref_regular = 1;
3079
3080 /* Ensure that the backend adjust_dynamic_symbol function sees
3081 the strong alias before H by recursively calling ourselves. */
3082 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3083 return FALSE;
3084 }
3085
3086 /* If a symbol has no type and no size and does not require a PLT
3087 entry, then we are probably about to do the wrong thing here: we
3088 are probably going to create a COPY reloc for an empty object.
3089 This case can arise when a shared object is built with assembly
3090 code, and the assembly code fails to set the symbol type. */
3091 if (h->size == 0
3092 && h->type == STT_NOTYPE
3093 && !h->needs_plt)
3094 _bfd_error_handler
3095 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3096 h->root.root.string);
3097
3098 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3099 {
3100 eif->failed = TRUE;
3101 return FALSE;
3102 }
3103
3104 return TRUE;
3105 }
3106
3107 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3108 DYNBSS. */
3109
3110 bfd_boolean
3111 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3112 struct elf_link_hash_entry *h,
3113 asection *dynbss)
3114 {
3115 unsigned int power_of_two;
3116 bfd_vma mask;
3117 asection *sec = h->root.u.def.section;
3118
3119 /* The section alignment of the definition is the maximum alignment
3120 requirement of symbols defined in the section. Since we don't
3121 know the symbol alignment requirement, we start with the
3122 maximum alignment and check low bits of the symbol address
3123 for the minimum alignment. */
3124 power_of_two = bfd_section_alignment (sec);
3125 mask = ((bfd_vma) 1 << power_of_two) - 1;
3126 while ((h->root.u.def.value & mask) != 0)
3127 {
3128 mask >>= 1;
3129 --power_of_two;
3130 }
3131
3132 if (power_of_two > bfd_section_alignment (dynbss))
3133 {
3134 /* Adjust the section alignment if needed. */
3135 if (!bfd_set_section_alignment (dynbss, power_of_two))
3136 return FALSE;
3137 }
3138
3139 /* We make sure that the symbol will be aligned properly. */
3140 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3141
3142 /* Define the symbol as being at this point in DYNBSS. */
3143 h->root.u.def.section = dynbss;
3144 h->root.u.def.value = dynbss->size;
3145
3146 /* Increment the size of DYNBSS to make room for the symbol. */
3147 dynbss->size += h->size;
3148
3149 /* No error if extern_protected_data is true. */
3150 if (h->protected_def
3151 && (!info->extern_protected_data
3152 || (info->extern_protected_data < 0
3153 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3154 info->callbacks->einfo
3155 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3156 h->root.root.string);
3157
3158 return TRUE;
3159 }
3160
3161 /* Adjust all external symbols pointing into SEC_MERGE sections
3162 to reflect the object merging within the sections. */
3163
3164 static bfd_boolean
3165 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3166 {
3167 asection *sec;
3168
3169 if ((h->root.type == bfd_link_hash_defined
3170 || h->root.type == bfd_link_hash_defweak)
3171 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3172 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3173 {
3174 bfd *output_bfd = (bfd *) data;
3175
3176 h->root.u.def.value =
3177 _bfd_merged_section_offset (output_bfd,
3178 &h->root.u.def.section,
3179 elf_section_data (sec)->sec_info,
3180 h->root.u.def.value);
3181 }
3182
3183 return TRUE;
3184 }
3185
3186 /* Returns false if the symbol referred to by H should be considered
3187 to resolve local to the current module, and true if it should be
3188 considered to bind dynamically. */
3189
3190 bfd_boolean
3191 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3192 struct bfd_link_info *info,
3193 bfd_boolean not_local_protected)
3194 {
3195 bfd_boolean binding_stays_local_p;
3196 const struct elf_backend_data *bed;
3197 struct elf_link_hash_table *hash_table;
3198
3199 if (h == NULL)
3200 return FALSE;
3201
3202 while (h->root.type == bfd_link_hash_indirect
3203 || h->root.type == bfd_link_hash_warning)
3204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3205
3206 /* If it was forced local, then clearly it's not dynamic. */
3207 if (h->dynindx == -1)
3208 return FALSE;
3209 if (h->forced_local)
3210 return FALSE;
3211
3212 /* Identify the cases where name binding rules say that a
3213 visible symbol resolves locally. */
3214 binding_stays_local_p = (bfd_link_executable (info)
3215 || SYMBOLIC_BIND (info, h));
3216
3217 switch (ELF_ST_VISIBILITY (h->other))
3218 {
3219 case STV_INTERNAL:
3220 case STV_HIDDEN:
3221 return FALSE;
3222
3223 case STV_PROTECTED:
3224 hash_table = elf_hash_table (info);
3225 if (!is_elf_hash_table (hash_table))
3226 return FALSE;
3227
3228 bed = get_elf_backend_data (hash_table->dynobj);
3229
3230 /* Proper resolution for function pointer equality may require
3231 that these symbols perhaps be resolved dynamically, even though
3232 we should be resolving them to the current module. */
3233 if (!not_local_protected || !bed->is_function_type (h->type))
3234 binding_stays_local_p = TRUE;
3235 break;
3236
3237 default:
3238 break;
3239 }
3240
3241 /* If it isn't defined locally, then clearly it's dynamic. */
3242 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3243 return TRUE;
3244
3245 /* Otherwise, the symbol is dynamic if binding rules don't tell
3246 us that it remains local. */
3247 return !binding_stays_local_p;
3248 }
3249
3250 /* Return true if the symbol referred to by H should be considered
3251 to resolve local to the current module, and false otherwise. Differs
3252 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3253 undefined symbols. The two functions are virtually identical except
3254 for the place where dynindx == -1 is tested. If that test is true,
3255 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3256 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3257 defined symbols.
3258 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3259 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3260 treatment of undefined weak symbols. For those that do not make
3261 undefined weak symbols dynamic, both functions may return false. */
3262
3263 bfd_boolean
3264 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3265 struct bfd_link_info *info,
3266 bfd_boolean local_protected)
3267 {
3268 const struct elf_backend_data *bed;
3269 struct elf_link_hash_table *hash_table;
3270
3271 /* If it's a local sym, of course we resolve locally. */
3272 if (h == NULL)
3273 return TRUE;
3274
3275 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3276 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3277 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3278 return TRUE;
3279
3280 /* Forced local symbols resolve locally. */
3281 if (h->forced_local)
3282 return TRUE;
3283
3284 /* Common symbols that become definitions don't get the DEF_REGULAR
3285 flag set, so test it first, and don't bail out. */
3286 if (ELF_COMMON_DEF_P (h))
3287 /* Do nothing. */;
3288 /* If we don't have a definition in a regular file, then we can't
3289 resolve locally. The sym is either undefined or dynamic. */
3290 else if (!h->def_regular)
3291 return FALSE;
3292
3293 /* Non-dynamic symbols resolve locally. */
3294 if (h->dynindx == -1)
3295 return TRUE;
3296
3297 /* At this point, we know the symbol is defined and dynamic. In an
3298 executable it must resolve locally, likewise when building symbolic
3299 shared libraries. */
3300 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3301 return TRUE;
3302
3303 /* Now deal with defined dynamic symbols in shared libraries. Ones
3304 with default visibility might not resolve locally. */
3305 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3306 return FALSE;
3307
3308 hash_table = elf_hash_table (info);
3309 if (!is_elf_hash_table (hash_table))
3310 return TRUE;
3311
3312 bed = get_elf_backend_data (hash_table->dynobj);
3313
3314 /* If extern_protected_data is false, STV_PROTECTED non-function
3315 symbols are local. */
3316 if ((!info->extern_protected_data
3317 || (info->extern_protected_data < 0
3318 && !bed->extern_protected_data))
3319 && !bed->is_function_type (h->type))
3320 return TRUE;
3321
3322 /* Function pointer equality tests may require that STV_PROTECTED
3323 symbols be treated as dynamic symbols. If the address of a
3324 function not defined in an executable is set to that function's
3325 plt entry in the executable, then the address of the function in
3326 a shared library must also be the plt entry in the executable. */
3327 return local_protected;
3328 }
3329
3330 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3331 aligned. Returns the first TLS output section. */
3332
3333 struct bfd_section *
3334 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3335 {
3336 struct bfd_section *sec, *tls;
3337 unsigned int align = 0;
3338
3339 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3340 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3341 break;
3342 tls = sec;
3343
3344 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3345 if (sec->alignment_power > align)
3346 align = sec->alignment_power;
3347
3348 elf_hash_table (info)->tls_sec = tls;
3349
3350 /* Ensure the alignment of the first section (usually .tdata) is the largest
3351 alignment, so that the tls segment starts aligned. */
3352 if (tls != NULL)
3353 tls->alignment_power = align;
3354
3355 return tls;
3356 }
3357
3358 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3359 static bfd_boolean
3360 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3361 Elf_Internal_Sym *sym)
3362 {
3363 const struct elf_backend_data *bed;
3364
3365 /* Local symbols do not count, but target specific ones might. */
3366 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3367 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3368 return FALSE;
3369
3370 bed = get_elf_backend_data (abfd);
3371 /* Function symbols do not count. */
3372 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3373 return FALSE;
3374
3375 /* If the section is undefined, then so is the symbol. */
3376 if (sym->st_shndx == SHN_UNDEF)
3377 return FALSE;
3378
3379 /* If the symbol is defined in the common section, then
3380 it is a common definition and so does not count. */
3381 if (bed->common_definition (sym))
3382 return FALSE;
3383
3384 /* If the symbol is in a target specific section then we
3385 must rely upon the backend to tell us what it is. */
3386 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3387 /* FIXME - this function is not coded yet:
3388
3389 return _bfd_is_global_symbol_definition (abfd, sym);
3390
3391 Instead for now assume that the definition is not global,
3392 Even if this is wrong, at least the linker will behave
3393 in the same way that it used to do. */
3394 return FALSE;
3395
3396 return TRUE;
3397 }
3398
3399 /* Search the symbol table of the archive element of the archive ABFD
3400 whose archive map contains a mention of SYMDEF, and determine if
3401 the symbol is defined in this element. */
3402 static bfd_boolean
3403 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3404 {
3405 Elf_Internal_Shdr * hdr;
3406 size_t symcount;
3407 size_t extsymcount;
3408 size_t extsymoff;
3409 Elf_Internal_Sym *isymbuf;
3410 Elf_Internal_Sym *isym;
3411 Elf_Internal_Sym *isymend;
3412 bfd_boolean result;
3413
3414 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3415 if (abfd == NULL)
3416 return FALSE;
3417
3418 if (! bfd_check_format (abfd, bfd_object))
3419 return FALSE;
3420
3421 /* Select the appropriate symbol table. If we don't know if the
3422 object file is an IR object, give linker LTO plugin a chance to
3423 get the correct symbol table. */
3424 if (abfd->plugin_format == bfd_plugin_yes
3425 #if BFD_SUPPORTS_PLUGINS
3426 || (abfd->plugin_format == bfd_plugin_unknown
3427 && bfd_link_plugin_object_p (abfd))
3428 #endif
3429 )
3430 {
3431 /* Use the IR symbol table if the object has been claimed by
3432 plugin. */
3433 abfd = abfd->plugin_dummy_bfd;
3434 hdr = &elf_tdata (abfd)->symtab_hdr;
3435 }
3436 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3437 hdr = &elf_tdata (abfd)->symtab_hdr;
3438 else
3439 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3440
3441 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3442
3443 /* The sh_info field of the symtab header tells us where the
3444 external symbols start. We don't care about the local symbols. */
3445 if (elf_bad_symtab (abfd))
3446 {
3447 extsymcount = symcount;
3448 extsymoff = 0;
3449 }
3450 else
3451 {
3452 extsymcount = symcount - hdr->sh_info;
3453 extsymoff = hdr->sh_info;
3454 }
3455
3456 if (extsymcount == 0)
3457 return FALSE;
3458
3459 /* Read in the symbol table. */
3460 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3461 NULL, NULL, NULL);
3462 if (isymbuf == NULL)
3463 return FALSE;
3464
3465 /* Scan the symbol table looking for SYMDEF. */
3466 result = FALSE;
3467 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3468 {
3469 const char *name;
3470
3471 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3472 isym->st_name);
3473 if (name == NULL)
3474 break;
3475
3476 if (strcmp (name, symdef->name) == 0)
3477 {
3478 result = is_global_data_symbol_definition (abfd, isym);
3479 break;
3480 }
3481 }
3482
3483 free (isymbuf);
3484
3485 return result;
3486 }
3487 \f
3488 /* Add an entry to the .dynamic table. */
3489
3490 bfd_boolean
3491 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3492 bfd_vma tag,
3493 bfd_vma val)
3494 {
3495 struct elf_link_hash_table *hash_table;
3496 const struct elf_backend_data *bed;
3497 asection *s;
3498 bfd_size_type newsize;
3499 bfd_byte *newcontents;
3500 Elf_Internal_Dyn dyn;
3501
3502 hash_table = elf_hash_table (info);
3503 if (! is_elf_hash_table (hash_table))
3504 return FALSE;
3505
3506 if (tag == DT_RELA || tag == DT_REL)
3507 hash_table->dynamic_relocs = TRUE;
3508
3509 bed = get_elf_backend_data (hash_table->dynobj);
3510 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3511 BFD_ASSERT (s != NULL);
3512
3513 newsize = s->size + bed->s->sizeof_dyn;
3514 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3515 if (newcontents == NULL)
3516 return FALSE;
3517
3518 dyn.d_tag = tag;
3519 dyn.d_un.d_val = val;
3520 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3521
3522 s->size = newsize;
3523 s->contents = newcontents;
3524
3525 return TRUE;
3526 }
3527
3528 /* Strip zero-sized dynamic sections. */
3529
3530 bfd_boolean
3531 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3532 {
3533 struct elf_link_hash_table *hash_table;
3534 const struct elf_backend_data *bed;
3535 asection *s, *sdynamic, **pp;
3536 asection *rela_dyn, *rel_dyn;
3537 Elf_Internal_Dyn dyn;
3538 bfd_byte *extdyn, *next;
3539 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3540 bfd_boolean strip_zero_sized;
3541 bfd_boolean strip_zero_sized_plt;
3542
3543 if (bfd_link_relocatable (info))
3544 return TRUE;
3545
3546 hash_table = elf_hash_table (info);
3547 if (!is_elf_hash_table (hash_table))
3548 return FALSE;
3549
3550 if (!hash_table->dynobj)
3551 return TRUE;
3552
3553 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3554 if (!sdynamic)
3555 return TRUE;
3556
3557 bed = get_elf_backend_data (hash_table->dynobj);
3558 swap_dyn_in = bed->s->swap_dyn_in;
3559
3560 strip_zero_sized = FALSE;
3561 strip_zero_sized_plt = FALSE;
3562
3563 /* Strip zero-sized dynamic sections. */
3564 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3565 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3566 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3567 if (s->size == 0
3568 && (s == rela_dyn
3569 || s == rel_dyn
3570 || s == hash_table->srelplt->output_section
3571 || s == hash_table->splt->output_section))
3572 {
3573 *pp = s->next;
3574 info->output_bfd->section_count--;
3575 strip_zero_sized = TRUE;
3576 if (s == rela_dyn)
3577 s = rela_dyn;
3578 if (s == rel_dyn)
3579 s = rel_dyn;
3580 else if (s == hash_table->splt->output_section)
3581 {
3582 s = hash_table->splt;
3583 strip_zero_sized_plt = TRUE;
3584 }
3585 else
3586 s = hash_table->srelplt;
3587 s->flags |= SEC_EXCLUDE;
3588 s->output_section = bfd_abs_section_ptr;
3589 }
3590 else
3591 pp = &s->next;
3592
3593 if (strip_zero_sized_plt)
3594 for (extdyn = sdynamic->contents;
3595 extdyn < sdynamic->contents + sdynamic->size;
3596 extdyn = next)
3597 {
3598 next = extdyn + bed->s->sizeof_dyn;
3599 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3600 switch (dyn.d_tag)
3601 {
3602 default:
3603 break;
3604 case DT_JMPREL:
3605 case DT_PLTRELSZ:
3606 case DT_PLTREL:
3607 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3608 the procedure linkage table (the .plt section) has been
3609 removed. */
3610 memmove (extdyn, next,
3611 sdynamic->size - (next - sdynamic->contents));
3612 next = extdyn;
3613 }
3614 }
3615
3616 if (strip_zero_sized)
3617 {
3618 /* Regenerate program headers. */
3619 elf_seg_map (info->output_bfd) = NULL;
3620 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3621 }
3622
3623 return TRUE;
3624 }
3625
3626 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3627 1 if a DT_NEEDED tag already exists, and 0 on success. */
3628
3629 int
3630 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3631 {
3632 struct elf_link_hash_table *hash_table;
3633 size_t strindex;
3634 const char *soname;
3635
3636 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3637 return -1;
3638
3639 hash_table = elf_hash_table (info);
3640 soname = elf_dt_name (abfd);
3641 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3642 if (strindex == (size_t) -1)
3643 return -1;
3644
3645 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3646 {
3647 asection *sdyn;
3648 const struct elf_backend_data *bed;
3649 bfd_byte *extdyn;
3650
3651 bed = get_elf_backend_data (hash_table->dynobj);
3652 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3653 if (sdyn != NULL)
3654 for (extdyn = sdyn->contents;
3655 extdyn < sdyn->contents + sdyn->size;
3656 extdyn += bed->s->sizeof_dyn)
3657 {
3658 Elf_Internal_Dyn dyn;
3659
3660 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3661 if (dyn.d_tag == DT_NEEDED
3662 && dyn.d_un.d_val == strindex)
3663 {
3664 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3665 return 1;
3666 }
3667 }
3668 }
3669
3670 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3671 return -1;
3672
3673 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3674 return -1;
3675
3676 return 0;
3677 }
3678
3679 /* Return true if SONAME is on the needed list between NEEDED and STOP
3680 (or the end of list if STOP is NULL), and needed by a library that
3681 will be loaded. */
3682
3683 static bfd_boolean
3684 on_needed_list (const char *soname,
3685 struct bfd_link_needed_list *needed,
3686 struct bfd_link_needed_list *stop)
3687 {
3688 struct bfd_link_needed_list *look;
3689 for (look = needed; look != stop; look = look->next)
3690 if (strcmp (soname, look->name) == 0
3691 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3692 /* If needed by a library that itself is not directly
3693 needed, recursively check whether that library is
3694 indirectly needed. Since we add DT_NEEDED entries to
3695 the end of the list, library dependencies appear after
3696 the library. Therefore search prior to the current
3697 LOOK, preventing possible infinite recursion. */
3698 || on_needed_list (elf_dt_name (look->by), needed, look)))
3699 return TRUE;
3700
3701 return FALSE;
3702 }
3703
3704 /* Sort symbol by value, section, size, and type. */
3705 static int
3706 elf_sort_symbol (const void *arg1, const void *arg2)
3707 {
3708 const struct elf_link_hash_entry *h1;
3709 const struct elf_link_hash_entry *h2;
3710 bfd_signed_vma vdiff;
3711 int sdiff;
3712 const char *n1;
3713 const char *n2;
3714
3715 h1 = *(const struct elf_link_hash_entry **) arg1;
3716 h2 = *(const struct elf_link_hash_entry **) arg2;
3717 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3718 if (vdiff != 0)
3719 return vdiff > 0 ? 1 : -1;
3720
3721 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3722 if (sdiff != 0)
3723 return sdiff;
3724
3725 /* Sort so that sized symbols are selected over zero size symbols. */
3726 vdiff = h1->size - h2->size;
3727 if (vdiff != 0)
3728 return vdiff > 0 ? 1 : -1;
3729
3730 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3731 if (h1->type != h2->type)
3732 return h1->type - h2->type;
3733
3734 /* If symbols are properly sized and typed, and multiple strong
3735 aliases are not defined in a shared library by the user we
3736 shouldn't get here. Unfortunately linker script symbols like
3737 __bss_start sometimes match a user symbol defined at the start of
3738 .bss without proper size and type. We'd like to preference the
3739 user symbol over reserved system symbols. Sort on leading
3740 underscores. */
3741 n1 = h1->root.root.string;
3742 n2 = h2->root.root.string;
3743 while (*n1 == *n2)
3744 {
3745 if (*n1 == 0)
3746 break;
3747 ++n1;
3748 ++n2;
3749 }
3750 if (*n1 == '_')
3751 return -1;
3752 if (*n2 == '_')
3753 return 1;
3754
3755 /* Final sort on name selects user symbols like '_u' over reserved
3756 system symbols like '_Z' and also will avoid qsort instability. */
3757 return *n1 - *n2;
3758 }
3759
3760 /* This function is used to adjust offsets into .dynstr for
3761 dynamic symbols. This is called via elf_link_hash_traverse. */
3762
3763 static bfd_boolean
3764 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3765 {
3766 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3767
3768 if (h->dynindx != -1)
3769 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3770 return TRUE;
3771 }
3772
3773 /* Assign string offsets in .dynstr, update all structures referencing
3774 them. */
3775
3776 static bfd_boolean
3777 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3778 {
3779 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3780 struct elf_link_local_dynamic_entry *entry;
3781 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3782 bfd *dynobj = hash_table->dynobj;
3783 asection *sdyn;
3784 bfd_size_type size;
3785 const struct elf_backend_data *bed;
3786 bfd_byte *extdyn;
3787
3788 _bfd_elf_strtab_finalize (dynstr);
3789 size = _bfd_elf_strtab_size (dynstr);
3790
3791 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3792
3793 if (info->callbacks->examine_strtab)
3794 info->callbacks->examine_strtab (dynstr);
3795
3796 bed = get_elf_backend_data (dynobj);
3797 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3798 BFD_ASSERT (sdyn != NULL);
3799
3800 /* Update all .dynamic entries referencing .dynstr strings. */
3801 for (extdyn = sdyn->contents;
3802 extdyn < sdyn->contents + sdyn->size;
3803 extdyn += bed->s->sizeof_dyn)
3804 {
3805 Elf_Internal_Dyn dyn;
3806
3807 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3808 switch (dyn.d_tag)
3809 {
3810 case DT_STRSZ:
3811 dyn.d_un.d_val = size;
3812 break;
3813 case DT_NEEDED:
3814 case DT_SONAME:
3815 case DT_RPATH:
3816 case DT_RUNPATH:
3817 case DT_FILTER:
3818 case DT_AUXILIARY:
3819 case DT_AUDIT:
3820 case DT_DEPAUDIT:
3821 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3822 break;
3823 default:
3824 continue;
3825 }
3826 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3827 }
3828
3829 /* Now update local dynamic symbols. */
3830 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3831 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3832 entry->isym.st_name);
3833
3834 /* And the rest of dynamic symbols. */
3835 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3836
3837 /* Adjust version definitions. */
3838 if (elf_tdata (output_bfd)->cverdefs)
3839 {
3840 asection *s;
3841 bfd_byte *p;
3842 size_t i;
3843 Elf_Internal_Verdef def;
3844 Elf_Internal_Verdaux defaux;
3845
3846 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3847 p = s->contents;
3848 do
3849 {
3850 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3851 &def);
3852 p += sizeof (Elf_External_Verdef);
3853 if (def.vd_aux != sizeof (Elf_External_Verdef))
3854 continue;
3855 for (i = 0; i < def.vd_cnt; ++i)
3856 {
3857 _bfd_elf_swap_verdaux_in (output_bfd,
3858 (Elf_External_Verdaux *) p, &defaux);
3859 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3860 defaux.vda_name);
3861 _bfd_elf_swap_verdaux_out (output_bfd,
3862 &defaux, (Elf_External_Verdaux *) p);
3863 p += sizeof (Elf_External_Verdaux);
3864 }
3865 }
3866 while (def.vd_next);
3867 }
3868
3869 /* Adjust version references. */
3870 if (elf_tdata (output_bfd)->verref)
3871 {
3872 asection *s;
3873 bfd_byte *p;
3874 size_t i;
3875 Elf_Internal_Verneed need;
3876 Elf_Internal_Vernaux needaux;
3877
3878 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3879 p = s->contents;
3880 do
3881 {
3882 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3883 &need);
3884 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3885 _bfd_elf_swap_verneed_out (output_bfd, &need,
3886 (Elf_External_Verneed *) p);
3887 p += sizeof (Elf_External_Verneed);
3888 for (i = 0; i < need.vn_cnt; ++i)
3889 {
3890 _bfd_elf_swap_vernaux_in (output_bfd,
3891 (Elf_External_Vernaux *) p, &needaux);
3892 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3893 needaux.vna_name);
3894 _bfd_elf_swap_vernaux_out (output_bfd,
3895 &needaux,
3896 (Elf_External_Vernaux *) p);
3897 p += sizeof (Elf_External_Vernaux);
3898 }
3899 }
3900 while (need.vn_next);
3901 }
3902
3903 return TRUE;
3904 }
3905 \f
3906 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3907 The default is to only match when the INPUT and OUTPUT are exactly
3908 the same target. */
3909
3910 bfd_boolean
3911 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3912 const bfd_target *output)
3913 {
3914 return input == output;
3915 }
3916
3917 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3918 This version is used when different targets for the same architecture
3919 are virtually identical. */
3920
3921 bfd_boolean
3922 _bfd_elf_relocs_compatible (const bfd_target *input,
3923 const bfd_target *output)
3924 {
3925 const struct elf_backend_data *obed, *ibed;
3926
3927 if (input == output)
3928 return TRUE;
3929
3930 ibed = xvec_get_elf_backend_data (input);
3931 obed = xvec_get_elf_backend_data (output);
3932
3933 if (ibed->arch != obed->arch)
3934 return FALSE;
3935
3936 /* If both backends are using this function, deem them compatible. */
3937 return ibed->relocs_compatible == obed->relocs_compatible;
3938 }
3939
3940 /* Make a special call to the linker "notice" function to tell it that
3941 we are about to handle an as-needed lib, or have finished
3942 processing the lib. */
3943
3944 bfd_boolean
3945 _bfd_elf_notice_as_needed (bfd *ibfd,
3946 struct bfd_link_info *info,
3947 enum notice_asneeded_action act)
3948 {
3949 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3950 }
3951
3952 /* Check relocations an ELF object file. */
3953
3954 bfd_boolean
3955 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3956 {
3957 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3958 struct elf_link_hash_table *htab = elf_hash_table (info);
3959
3960 /* If this object is the same format as the output object, and it is
3961 not a shared library, then let the backend look through the
3962 relocs.
3963
3964 This is required to build global offset table entries and to
3965 arrange for dynamic relocs. It is not required for the
3966 particular common case of linking non PIC code, even when linking
3967 against shared libraries, but unfortunately there is no way of
3968 knowing whether an object file has been compiled PIC or not.
3969 Looking through the relocs is not particularly time consuming.
3970 The problem is that we must either (1) keep the relocs in memory,
3971 which causes the linker to require additional runtime memory or
3972 (2) read the relocs twice from the input file, which wastes time.
3973 This would be a good case for using mmap.
3974
3975 I have no idea how to handle linking PIC code into a file of a
3976 different format. It probably can't be done. */
3977 if ((abfd->flags & DYNAMIC) == 0
3978 && is_elf_hash_table (htab)
3979 && bed->check_relocs != NULL
3980 && elf_object_id (abfd) == elf_hash_table_id (htab)
3981 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3982 {
3983 asection *o;
3984
3985 for (o = abfd->sections; o != NULL; o = o->next)
3986 {
3987 Elf_Internal_Rela *internal_relocs;
3988 bfd_boolean ok;
3989
3990 /* Don't check relocations in excluded sections. Don't do
3991 anything special with non-loaded, non-alloced sections.
3992 In particular, any relocs in such sections should not
3993 affect GOT and PLT reference counting (ie. we don't
3994 allow them to create GOT or PLT entries), there's no
3995 possibility or desire to optimize TLS relocs, and
3996 there's not much point in propagating relocs to shared
3997 libs that the dynamic linker won't relocate. */
3998 if ((o->flags & SEC_ALLOC) == 0
3999 || (o->flags & SEC_RELOC) == 0
4000 || (o->flags & SEC_EXCLUDE) != 0
4001 || o->reloc_count == 0
4002 || ((info->strip == strip_all || info->strip == strip_debugger)
4003 && (o->flags & SEC_DEBUGGING) != 0)
4004 || bfd_is_abs_section (o->output_section))
4005 continue;
4006
4007 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4008 info->keep_memory);
4009 if (internal_relocs == NULL)
4010 return FALSE;
4011
4012 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4013
4014 if (elf_section_data (o)->relocs != internal_relocs)
4015 free (internal_relocs);
4016
4017 if (! ok)
4018 return FALSE;
4019 }
4020 }
4021
4022 return TRUE;
4023 }
4024
4025 /* Add symbols from an ELF object file to the linker hash table. */
4026
4027 static bfd_boolean
4028 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4029 {
4030 Elf_Internal_Ehdr *ehdr;
4031 Elf_Internal_Shdr *hdr;
4032 size_t symcount;
4033 size_t extsymcount;
4034 size_t extsymoff;
4035 struct elf_link_hash_entry **sym_hash;
4036 bfd_boolean dynamic;
4037 Elf_External_Versym *extversym = NULL;
4038 Elf_External_Versym *extversym_end = NULL;
4039 Elf_External_Versym *ever;
4040 struct elf_link_hash_entry *weaks;
4041 struct elf_link_hash_entry **nondeflt_vers = NULL;
4042 size_t nondeflt_vers_cnt = 0;
4043 Elf_Internal_Sym *isymbuf = NULL;
4044 Elf_Internal_Sym *isym;
4045 Elf_Internal_Sym *isymend;
4046 const struct elf_backend_data *bed;
4047 bfd_boolean add_needed;
4048 struct elf_link_hash_table *htab;
4049 void *alloc_mark = NULL;
4050 struct bfd_hash_entry **old_table = NULL;
4051 unsigned int old_size = 0;
4052 unsigned int old_count = 0;
4053 void *old_tab = NULL;
4054 void *old_ent;
4055 struct bfd_link_hash_entry *old_undefs = NULL;
4056 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4057 void *old_strtab = NULL;
4058 size_t tabsize = 0;
4059 asection *s;
4060 bfd_boolean just_syms;
4061
4062 htab = elf_hash_table (info);
4063 bed = get_elf_backend_data (abfd);
4064
4065 if ((abfd->flags & DYNAMIC) == 0)
4066 dynamic = FALSE;
4067 else
4068 {
4069 dynamic = TRUE;
4070
4071 /* You can't use -r against a dynamic object. Also, there's no
4072 hope of using a dynamic object which does not exactly match
4073 the format of the output file. */
4074 if (bfd_link_relocatable (info)
4075 || !is_elf_hash_table (htab)
4076 || info->output_bfd->xvec != abfd->xvec)
4077 {
4078 if (bfd_link_relocatable (info))
4079 bfd_set_error (bfd_error_invalid_operation);
4080 else
4081 bfd_set_error (bfd_error_wrong_format);
4082 goto error_return;
4083 }
4084 }
4085
4086 ehdr = elf_elfheader (abfd);
4087 if (info->warn_alternate_em
4088 && bed->elf_machine_code != ehdr->e_machine
4089 && ((bed->elf_machine_alt1 != 0
4090 && ehdr->e_machine == bed->elf_machine_alt1)
4091 || (bed->elf_machine_alt2 != 0
4092 && ehdr->e_machine == bed->elf_machine_alt2)))
4093 _bfd_error_handler
4094 /* xgettext:c-format */
4095 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4096 ehdr->e_machine, abfd, bed->elf_machine_code);
4097
4098 /* As a GNU extension, any input sections which are named
4099 .gnu.warning.SYMBOL are treated as warning symbols for the given
4100 symbol. This differs from .gnu.warning sections, which generate
4101 warnings when they are included in an output file. */
4102 /* PR 12761: Also generate this warning when building shared libraries. */
4103 for (s = abfd->sections; s != NULL; s = s->next)
4104 {
4105 const char *name;
4106
4107 name = bfd_section_name (s);
4108 if (CONST_STRNEQ (name, ".gnu.warning."))
4109 {
4110 char *msg;
4111 bfd_size_type sz;
4112
4113 name += sizeof ".gnu.warning." - 1;
4114
4115 /* If this is a shared object, then look up the symbol
4116 in the hash table. If it is there, and it is already
4117 been defined, then we will not be using the entry
4118 from this shared object, so we don't need to warn.
4119 FIXME: If we see the definition in a regular object
4120 later on, we will warn, but we shouldn't. The only
4121 fix is to keep track of what warnings we are supposed
4122 to emit, and then handle them all at the end of the
4123 link. */
4124 if (dynamic)
4125 {
4126 struct elf_link_hash_entry *h;
4127
4128 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4129
4130 /* FIXME: What about bfd_link_hash_common? */
4131 if (h != NULL
4132 && (h->root.type == bfd_link_hash_defined
4133 || h->root.type == bfd_link_hash_defweak))
4134 continue;
4135 }
4136
4137 sz = s->size;
4138 msg = (char *) bfd_alloc (abfd, sz + 1);
4139 if (msg == NULL)
4140 goto error_return;
4141
4142 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4143 goto error_return;
4144
4145 msg[sz] = '\0';
4146
4147 if (! (_bfd_generic_link_add_one_symbol
4148 (info, abfd, name, BSF_WARNING, s, 0, msg,
4149 FALSE, bed->collect, NULL)))
4150 goto error_return;
4151
4152 if (bfd_link_executable (info))
4153 {
4154 /* Clobber the section size so that the warning does
4155 not get copied into the output file. */
4156 s->size = 0;
4157
4158 /* Also set SEC_EXCLUDE, so that symbols defined in
4159 the warning section don't get copied to the output. */
4160 s->flags |= SEC_EXCLUDE;
4161 }
4162 }
4163 }
4164
4165 just_syms = ((s = abfd->sections) != NULL
4166 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4167
4168 add_needed = TRUE;
4169 if (! dynamic)
4170 {
4171 /* If we are creating a shared library, create all the dynamic
4172 sections immediately. We need to attach them to something,
4173 so we attach them to this BFD, provided it is the right
4174 format and is not from ld --just-symbols. Always create the
4175 dynamic sections for -E/--dynamic-list. FIXME: If there
4176 are no input BFD's of the same format as the output, we can't
4177 make a shared library. */
4178 if (!just_syms
4179 && (bfd_link_pic (info)
4180 || (!bfd_link_relocatable (info)
4181 && info->nointerp
4182 && (info->export_dynamic || info->dynamic)))
4183 && is_elf_hash_table (htab)
4184 && info->output_bfd->xvec == abfd->xvec
4185 && !htab->dynamic_sections_created)
4186 {
4187 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4188 goto error_return;
4189 }
4190 }
4191 else if (!is_elf_hash_table (htab))
4192 goto error_return;
4193 else
4194 {
4195 const char *soname = NULL;
4196 char *audit = NULL;
4197 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4198 const Elf_Internal_Phdr *phdr;
4199 struct elf_link_loaded_list *loaded_lib;
4200
4201 /* ld --just-symbols and dynamic objects don't mix very well.
4202 ld shouldn't allow it. */
4203 if (just_syms)
4204 abort ();
4205
4206 /* If this dynamic lib was specified on the command line with
4207 --as-needed in effect, then we don't want to add a DT_NEEDED
4208 tag unless the lib is actually used. Similary for libs brought
4209 in by another lib's DT_NEEDED. When --no-add-needed is used
4210 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4211 any dynamic library in DT_NEEDED tags in the dynamic lib at
4212 all. */
4213 add_needed = (elf_dyn_lib_class (abfd)
4214 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4215 | DYN_NO_NEEDED)) == 0;
4216
4217 s = bfd_get_section_by_name (abfd, ".dynamic");
4218 if (s != NULL)
4219 {
4220 bfd_byte *dynbuf;
4221 bfd_byte *extdyn;
4222 unsigned int elfsec;
4223 unsigned long shlink;
4224
4225 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4226 {
4227 error_free_dyn:
4228 free (dynbuf);
4229 goto error_return;
4230 }
4231
4232 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4233 if (elfsec == SHN_BAD)
4234 goto error_free_dyn;
4235 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4236
4237 for (extdyn = dynbuf;
4238 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4239 extdyn += bed->s->sizeof_dyn)
4240 {
4241 Elf_Internal_Dyn dyn;
4242
4243 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4244 if (dyn.d_tag == DT_SONAME)
4245 {
4246 unsigned int tagv = dyn.d_un.d_val;
4247 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4248 if (soname == NULL)
4249 goto error_free_dyn;
4250 }
4251 if (dyn.d_tag == DT_NEEDED)
4252 {
4253 struct bfd_link_needed_list *n, **pn;
4254 char *fnm, *anm;
4255 unsigned int tagv = dyn.d_un.d_val;
4256 size_t amt = sizeof (struct bfd_link_needed_list);
4257
4258 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4259 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4260 if (n == NULL || fnm == NULL)
4261 goto error_free_dyn;
4262 amt = strlen (fnm) + 1;
4263 anm = (char *) bfd_alloc (abfd, amt);
4264 if (anm == NULL)
4265 goto error_free_dyn;
4266 memcpy (anm, fnm, amt);
4267 n->name = anm;
4268 n->by = abfd;
4269 n->next = NULL;
4270 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4271 ;
4272 *pn = n;
4273 }
4274 if (dyn.d_tag == DT_RUNPATH)
4275 {
4276 struct bfd_link_needed_list *n, **pn;
4277 char *fnm, *anm;
4278 unsigned int tagv = dyn.d_un.d_val;
4279 size_t amt = sizeof (struct bfd_link_needed_list);
4280
4281 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4282 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4283 if (n == NULL || fnm == NULL)
4284 goto error_free_dyn;
4285 amt = strlen (fnm) + 1;
4286 anm = (char *) bfd_alloc (abfd, amt);
4287 if (anm == NULL)
4288 goto error_free_dyn;
4289 memcpy (anm, fnm, amt);
4290 n->name = anm;
4291 n->by = abfd;
4292 n->next = NULL;
4293 for (pn = & runpath;
4294 *pn != NULL;
4295 pn = &(*pn)->next)
4296 ;
4297 *pn = n;
4298 }
4299 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4300 if (!runpath && dyn.d_tag == DT_RPATH)
4301 {
4302 struct bfd_link_needed_list *n, **pn;
4303 char *fnm, *anm;
4304 unsigned int tagv = dyn.d_un.d_val;
4305 size_t amt = sizeof (struct bfd_link_needed_list);
4306
4307 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4308 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4309 if (n == NULL || fnm == NULL)
4310 goto error_free_dyn;
4311 amt = strlen (fnm) + 1;
4312 anm = (char *) bfd_alloc (abfd, amt);
4313 if (anm == NULL)
4314 goto error_free_dyn;
4315 memcpy (anm, fnm, amt);
4316 n->name = anm;
4317 n->by = abfd;
4318 n->next = NULL;
4319 for (pn = & rpath;
4320 *pn != NULL;
4321 pn = &(*pn)->next)
4322 ;
4323 *pn = n;
4324 }
4325 if (dyn.d_tag == DT_AUDIT)
4326 {
4327 unsigned int tagv = dyn.d_un.d_val;
4328 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4329 }
4330 }
4331
4332 free (dynbuf);
4333 }
4334
4335 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4336 frees all more recently bfd_alloc'd blocks as well. */
4337 if (runpath)
4338 rpath = runpath;
4339
4340 if (rpath)
4341 {
4342 struct bfd_link_needed_list **pn;
4343 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4344 ;
4345 *pn = rpath;
4346 }
4347
4348 /* If we have a PT_GNU_RELRO program header, mark as read-only
4349 all sections contained fully therein. This makes relro
4350 shared library sections appear as they will at run-time. */
4351 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4352 while (phdr-- > elf_tdata (abfd)->phdr)
4353 if (phdr->p_type == PT_GNU_RELRO)
4354 {
4355 for (s = abfd->sections; s != NULL; s = s->next)
4356 {
4357 unsigned int opb = bfd_octets_per_byte (abfd, s);
4358
4359 if ((s->flags & SEC_ALLOC) != 0
4360 && s->vma * opb >= phdr->p_vaddr
4361 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4362 s->flags |= SEC_READONLY;
4363 }
4364 break;
4365 }
4366
4367 /* We do not want to include any of the sections in a dynamic
4368 object in the output file. We hack by simply clobbering the
4369 list of sections in the BFD. This could be handled more
4370 cleanly by, say, a new section flag; the existing
4371 SEC_NEVER_LOAD flag is not the one we want, because that one
4372 still implies that the section takes up space in the output
4373 file. */
4374 bfd_section_list_clear (abfd);
4375
4376 /* Find the name to use in a DT_NEEDED entry that refers to this
4377 object. If the object has a DT_SONAME entry, we use it.
4378 Otherwise, if the generic linker stuck something in
4379 elf_dt_name, we use that. Otherwise, we just use the file
4380 name. */
4381 if (soname == NULL || *soname == '\0')
4382 {
4383 soname = elf_dt_name (abfd);
4384 if (soname == NULL || *soname == '\0')
4385 soname = bfd_get_filename (abfd);
4386 }
4387
4388 /* Save the SONAME because sometimes the linker emulation code
4389 will need to know it. */
4390 elf_dt_name (abfd) = soname;
4391
4392 /* If we have already included this dynamic object in the
4393 link, just ignore it. There is no reason to include a
4394 particular dynamic object more than once. */
4395 for (loaded_lib = htab->dyn_loaded;
4396 loaded_lib != NULL;
4397 loaded_lib = loaded_lib->next)
4398 {
4399 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4400 return TRUE;
4401 }
4402
4403 /* Create dynamic sections for backends that require that be done
4404 before setup_gnu_properties. */
4405 if (add_needed
4406 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4407 return FALSE;
4408
4409 /* Save the DT_AUDIT entry for the linker emulation code. */
4410 elf_dt_audit (abfd) = audit;
4411 }
4412
4413 /* If this is a dynamic object, we always link against the .dynsym
4414 symbol table, not the .symtab symbol table. The dynamic linker
4415 will only see the .dynsym symbol table, so there is no reason to
4416 look at .symtab for a dynamic object. */
4417
4418 if (! dynamic || elf_dynsymtab (abfd) == 0)
4419 hdr = &elf_tdata (abfd)->symtab_hdr;
4420 else
4421 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4422
4423 symcount = hdr->sh_size / bed->s->sizeof_sym;
4424
4425 /* The sh_info field of the symtab header tells us where the
4426 external symbols start. We don't care about the local symbols at
4427 this point. */
4428 if (elf_bad_symtab (abfd))
4429 {
4430 extsymcount = symcount;
4431 extsymoff = 0;
4432 }
4433 else
4434 {
4435 extsymcount = symcount - hdr->sh_info;
4436 extsymoff = hdr->sh_info;
4437 }
4438
4439 sym_hash = elf_sym_hashes (abfd);
4440 if (extsymcount != 0)
4441 {
4442 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4443 NULL, NULL, NULL);
4444 if (isymbuf == NULL)
4445 goto error_return;
4446
4447 if (sym_hash == NULL)
4448 {
4449 /* We store a pointer to the hash table entry for each
4450 external symbol. */
4451 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4452 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4453 if (sym_hash == NULL)
4454 goto error_free_sym;
4455 elf_sym_hashes (abfd) = sym_hash;
4456 }
4457 }
4458
4459 if (dynamic)
4460 {
4461 /* Read in any version definitions. */
4462 if (!_bfd_elf_slurp_version_tables (abfd,
4463 info->default_imported_symver))
4464 goto error_free_sym;
4465
4466 /* Read in the symbol versions, but don't bother to convert them
4467 to internal format. */
4468 if (elf_dynversym (abfd) != 0)
4469 {
4470 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4471 bfd_size_type amt = versymhdr->sh_size;
4472
4473 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4474 goto error_free_sym;
4475 extversym = (Elf_External_Versym *)
4476 _bfd_malloc_and_read (abfd, amt, amt);
4477 if (extversym == NULL)
4478 goto error_free_sym;
4479 extversym_end = extversym + amt / sizeof (*extversym);
4480 }
4481 }
4482
4483 /* If we are loading an as-needed shared lib, save the symbol table
4484 state before we start adding symbols. If the lib turns out
4485 to be unneeded, restore the state. */
4486 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4487 {
4488 unsigned int i;
4489 size_t entsize;
4490
4491 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4492 {
4493 struct bfd_hash_entry *p;
4494 struct elf_link_hash_entry *h;
4495
4496 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4497 {
4498 h = (struct elf_link_hash_entry *) p;
4499 entsize += htab->root.table.entsize;
4500 if (h->root.type == bfd_link_hash_warning)
4501 {
4502 entsize += htab->root.table.entsize;
4503 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4504 }
4505 if (h->root.type == bfd_link_hash_common)
4506 entsize += sizeof (*h->root.u.c.p);
4507 }
4508 }
4509
4510 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4511 old_tab = bfd_malloc (tabsize + entsize);
4512 if (old_tab == NULL)
4513 goto error_free_vers;
4514
4515 /* Remember the current objalloc pointer, so that all mem for
4516 symbols added can later be reclaimed. */
4517 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4518 if (alloc_mark == NULL)
4519 goto error_free_vers;
4520
4521 /* Make a special call to the linker "notice" function to
4522 tell it that we are about to handle an as-needed lib. */
4523 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4524 goto error_free_vers;
4525
4526 /* Clone the symbol table. Remember some pointers into the
4527 symbol table, and dynamic symbol count. */
4528 old_ent = (char *) old_tab + tabsize;
4529 memcpy (old_tab, htab->root.table.table, tabsize);
4530 old_undefs = htab->root.undefs;
4531 old_undefs_tail = htab->root.undefs_tail;
4532 old_table = htab->root.table.table;
4533 old_size = htab->root.table.size;
4534 old_count = htab->root.table.count;
4535 old_strtab = NULL;
4536 if (htab->dynstr != NULL)
4537 {
4538 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4539 if (old_strtab == NULL)
4540 goto error_free_vers;
4541 }
4542
4543 for (i = 0; i < htab->root.table.size; i++)
4544 {
4545 struct bfd_hash_entry *p;
4546 struct elf_link_hash_entry *h;
4547
4548 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4549 {
4550 h = (struct elf_link_hash_entry *) p;
4551 memcpy (old_ent, h, htab->root.table.entsize);
4552 old_ent = (char *) old_ent + htab->root.table.entsize;
4553 if (h->root.type == bfd_link_hash_warning)
4554 {
4555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4556 memcpy (old_ent, h, htab->root.table.entsize);
4557 old_ent = (char *) old_ent + htab->root.table.entsize;
4558 }
4559 if (h->root.type == bfd_link_hash_common)
4560 {
4561 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4562 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4563 }
4564 }
4565 }
4566 }
4567
4568 weaks = NULL;
4569 if (extversym == NULL)
4570 ever = NULL;
4571 else if (extversym + extsymoff < extversym_end)
4572 ever = extversym + extsymoff;
4573 else
4574 {
4575 /* xgettext:c-format */
4576 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4577 abfd, (long) extsymoff,
4578 (long) (extversym_end - extversym) / sizeof (* extversym));
4579 bfd_set_error (bfd_error_bad_value);
4580 goto error_free_vers;
4581 }
4582
4583 if (!bfd_link_relocatable (info)
4584 && abfd->lto_slim_object)
4585 {
4586 _bfd_error_handler
4587 (_("%pB: plugin needed to handle lto object"), abfd);
4588 }
4589
4590 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4591 isym < isymend;
4592 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4593 {
4594 int bind;
4595 bfd_vma value;
4596 asection *sec, *new_sec;
4597 flagword flags;
4598 const char *name;
4599 struct elf_link_hash_entry *h;
4600 struct elf_link_hash_entry *hi;
4601 bfd_boolean definition;
4602 bfd_boolean size_change_ok;
4603 bfd_boolean type_change_ok;
4604 bfd_boolean new_weak;
4605 bfd_boolean old_weak;
4606 bfd *override;
4607 bfd_boolean common;
4608 bfd_boolean discarded;
4609 unsigned int old_alignment;
4610 unsigned int shindex;
4611 bfd *old_bfd;
4612 bfd_boolean matched;
4613
4614 override = NULL;
4615
4616 flags = BSF_NO_FLAGS;
4617 sec = NULL;
4618 value = isym->st_value;
4619 common = bed->common_definition (isym);
4620 if (common && info->inhibit_common_definition)
4621 {
4622 /* Treat common symbol as undefined for --no-define-common. */
4623 isym->st_shndx = SHN_UNDEF;
4624 common = FALSE;
4625 }
4626 discarded = FALSE;
4627
4628 bind = ELF_ST_BIND (isym->st_info);
4629 switch (bind)
4630 {
4631 case STB_LOCAL:
4632 /* This should be impossible, since ELF requires that all
4633 global symbols follow all local symbols, and that sh_info
4634 point to the first global symbol. Unfortunately, Irix 5
4635 screws this up. */
4636 if (elf_bad_symtab (abfd))
4637 continue;
4638
4639 /* If we aren't prepared to handle locals within the globals
4640 then we'll likely segfault on a NULL symbol hash if the
4641 symbol is ever referenced in relocations. */
4642 shindex = elf_elfheader (abfd)->e_shstrndx;
4643 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4644 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4645 " (>= sh_info of %lu)"),
4646 abfd, name, (long) (isym - isymbuf + extsymoff),
4647 (long) extsymoff);
4648
4649 /* Dynamic object relocations are not processed by ld, so
4650 ld won't run into the problem mentioned above. */
4651 if (dynamic)
4652 continue;
4653 bfd_set_error (bfd_error_bad_value);
4654 goto error_free_vers;
4655
4656 case STB_GLOBAL:
4657 if (isym->st_shndx != SHN_UNDEF && !common)
4658 flags = BSF_GLOBAL;
4659 break;
4660
4661 case STB_WEAK:
4662 flags = BSF_WEAK;
4663 break;
4664
4665 case STB_GNU_UNIQUE:
4666 flags = BSF_GNU_UNIQUE;
4667 break;
4668
4669 default:
4670 /* Leave it up to the processor backend. */
4671 break;
4672 }
4673
4674 if (isym->st_shndx == SHN_UNDEF)
4675 sec = bfd_und_section_ptr;
4676 else if (isym->st_shndx == SHN_ABS)
4677 sec = bfd_abs_section_ptr;
4678 else if (isym->st_shndx == SHN_COMMON)
4679 {
4680 sec = bfd_com_section_ptr;
4681 /* What ELF calls the size we call the value. What ELF
4682 calls the value we call the alignment. */
4683 value = isym->st_size;
4684 }
4685 else
4686 {
4687 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4688 if (sec == NULL)
4689 sec = bfd_abs_section_ptr;
4690 else if (discarded_section (sec))
4691 {
4692 /* Symbols from discarded section are undefined. We keep
4693 its visibility. */
4694 sec = bfd_und_section_ptr;
4695 discarded = TRUE;
4696 isym->st_shndx = SHN_UNDEF;
4697 }
4698 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4699 value -= sec->vma;
4700 }
4701
4702 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4703 isym->st_name);
4704 if (name == NULL)
4705 goto error_free_vers;
4706
4707 if (isym->st_shndx == SHN_COMMON
4708 && (abfd->flags & BFD_PLUGIN) != 0)
4709 {
4710 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4711
4712 if (xc == NULL)
4713 {
4714 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4715 | SEC_EXCLUDE);
4716 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4717 if (xc == NULL)
4718 goto error_free_vers;
4719 }
4720 sec = xc;
4721 }
4722 else if (isym->st_shndx == SHN_COMMON
4723 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4724 && !bfd_link_relocatable (info))
4725 {
4726 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4727
4728 if (tcomm == NULL)
4729 {
4730 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4731 | SEC_LINKER_CREATED);
4732 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4733 if (tcomm == NULL)
4734 goto error_free_vers;
4735 }
4736 sec = tcomm;
4737 }
4738 else if (bed->elf_add_symbol_hook)
4739 {
4740 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4741 &sec, &value))
4742 goto error_free_vers;
4743
4744 /* The hook function sets the name to NULL if this symbol
4745 should be skipped for some reason. */
4746 if (name == NULL)
4747 continue;
4748 }
4749
4750 /* Sanity check that all possibilities were handled. */
4751 if (sec == NULL)
4752 abort ();
4753
4754 /* Silently discard TLS symbols from --just-syms. There's
4755 no way to combine a static TLS block with a new TLS block
4756 for this executable. */
4757 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4758 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4759 continue;
4760
4761 if (bfd_is_und_section (sec)
4762 || bfd_is_com_section (sec))
4763 definition = FALSE;
4764 else
4765 definition = TRUE;
4766
4767 size_change_ok = FALSE;
4768 type_change_ok = bed->type_change_ok;
4769 old_weak = FALSE;
4770 matched = FALSE;
4771 old_alignment = 0;
4772 old_bfd = NULL;
4773 new_sec = sec;
4774
4775 if (is_elf_hash_table (htab))
4776 {
4777 Elf_Internal_Versym iver;
4778 unsigned int vernum = 0;
4779 bfd_boolean skip;
4780
4781 if (ever == NULL)
4782 {
4783 if (info->default_imported_symver)
4784 /* Use the default symbol version created earlier. */
4785 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4786 else
4787 iver.vs_vers = 0;
4788 }
4789 else if (ever >= extversym_end)
4790 {
4791 /* xgettext:c-format */
4792 _bfd_error_handler (_("%pB: not enough version information"),
4793 abfd);
4794 bfd_set_error (bfd_error_bad_value);
4795 goto error_free_vers;
4796 }
4797 else
4798 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4799
4800 vernum = iver.vs_vers & VERSYM_VERSION;
4801
4802 /* If this is a hidden symbol, or if it is not version
4803 1, we append the version name to the symbol name.
4804 However, we do not modify a non-hidden absolute symbol
4805 if it is not a function, because it might be the version
4806 symbol itself. FIXME: What if it isn't? */
4807 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4808 || (vernum > 1
4809 && (!bfd_is_abs_section (sec)
4810 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4811 {
4812 const char *verstr;
4813 size_t namelen, verlen, newlen;
4814 char *newname, *p;
4815
4816 if (isym->st_shndx != SHN_UNDEF)
4817 {
4818 if (vernum > elf_tdata (abfd)->cverdefs)
4819 verstr = NULL;
4820 else if (vernum > 1)
4821 verstr =
4822 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4823 else
4824 verstr = "";
4825
4826 if (verstr == NULL)
4827 {
4828 _bfd_error_handler
4829 /* xgettext:c-format */
4830 (_("%pB: %s: invalid version %u (max %d)"),
4831 abfd, name, vernum,
4832 elf_tdata (abfd)->cverdefs);
4833 bfd_set_error (bfd_error_bad_value);
4834 goto error_free_vers;
4835 }
4836 }
4837 else
4838 {
4839 /* We cannot simply test for the number of
4840 entries in the VERNEED section since the
4841 numbers for the needed versions do not start
4842 at 0. */
4843 Elf_Internal_Verneed *t;
4844
4845 verstr = NULL;
4846 for (t = elf_tdata (abfd)->verref;
4847 t != NULL;
4848 t = t->vn_nextref)
4849 {
4850 Elf_Internal_Vernaux *a;
4851
4852 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4853 {
4854 if (a->vna_other == vernum)
4855 {
4856 verstr = a->vna_nodename;
4857 break;
4858 }
4859 }
4860 if (a != NULL)
4861 break;
4862 }
4863 if (verstr == NULL)
4864 {
4865 _bfd_error_handler
4866 /* xgettext:c-format */
4867 (_("%pB: %s: invalid needed version %d"),
4868 abfd, name, vernum);
4869 bfd_set_error (bfd_error_bad_value);
4870 goto error_free_vers;
4871 }
4872 }
4873
4874 namelen = strlen (name);
4875 verlen = strlen (verstr);
4876 newlen = namelen + verlen + 2;
4877 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4878 && isym->st_shndx != SHN_UNDEF)
4879 ++newlen;
4880
4881 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4882 if (newname == NULL)
4883 goto error_free_vers;
4884 memcpy (newname, name, namelen);
4885 p = newname + namelen;
4886 *p++ = ELF_VER_CHR;
4887 /* If this is a defined non-hidden version symbol,
4888 we add another @ to the name. This indicates the
4889 default version of the symbol. */
4890 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4891 && isym->st_shndx != SHN_UNDEF)
4892 *p++ = ELF_VER_CHR;
4893 memcpy (p, verstr, verlen + 1);
4894
4895 name = newname;
4896 }
4897
4898 /* If this symbol has default visibility and the user has
4899 requested we not re-export it, then mark it as hidden. */
4900 if (!bfd_is_und_section (sec)
4901 && !dynamic
4902 && abfd->no_export
4903 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4904 isym->st_other = (STV_HIDDEN
4905 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4906
4907 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4908 sym_hash, &old_bfd, &old_weak,
4909 &old_alignment, &skip, &override,
4910 &type_change_ok, &size_change_ok,
4911 &matched))
4912 goto error_free_vers;
4913
4914 if (skip)
4915 continue;
4916
4917 /* Override a definition only if the new symbol matches the
4918 existing one. */
4919 if (override && matched)
4920 definition = FALSE;
4921
4922 h = *sym_hash;
4923 while (h->root.type == bfd_link_hash_indirect
4924 || h->root.type == bfd_link_hash_warning)
4925 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4926
4927 if (elf_tdata (abfd)->verdef != NULL
4928 && vernum > 1
4929 && definition)
4930 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4931 }
4932
4933 if (! (_bfd_generic_link_add_one_symbol
4934 (info, override ? override : abfd, name, flags, sec, value,
4935 NULL, FALSE, bed->collect,
4936 (struct bfd_link_hash_entry **) sym_hash)))
4937 goto error_free_vers;
4938
4939 h = *sym_hash;
4940 /* We need to make sure that indirect symbol dynamic flags are
4941 updated. */
4942 hi = h;
4943 while (h->root.type == bfd_link_hash_indirect
4944 || h->root.type == bfd_link_hash_warning)
4945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4946
4947 /* Setting the index to -3 tells elf_link_output_extsym that
4948 this symbol is defined in a discarded section. */
4949 if (discarded)
4950 h->indx = -3;
4951
4952 *sym_hash = h;
4953
4954 new_weak = (flags & BSF_WEAK) != 0;
4955 if (dynamic
4956 && definition
4957 && new_weak
4958 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4959 && is_elf_hash_table (htab)
4960 && h->u.alias == NULL)
4961 {
4962 /* Keep a list of all weak defined non function symbols from
4963 a dynamic object, using the alias field. Later in this
4964 function we will set the alias field to the correct
4965 value. We only put non-function symbols from dynamic
4966 objects on this list, because that happens to be the only
4967 time we need to know the normal symbol corresponding to a
4968 weak symbol, and the information is time consuming to
4969 figure out. If the alias field is not already NULL,
4970 then this symbol was already defined by some previous
4971 dynamic object, and we will be using that previous
4972 definition anyhow. */
4973
4974 h->u.alias = weaks;
4975 weaks = h;
4976 }
4977
4978 /* Set the alignment of a common symbol. */
4979 if ((common || bfd_is_com_section (sec))
4980 && h->root.type == bfd_link_hash_common)
4981 {
4982 unsigned int align;
4983
4984 if (common)
4985 align = bfd_log2 (isym->st_value);
4986 else
4987 {
4988 /* The new symbol is a common symbol in a shared object.
4989 We need to get the alignment from the section. */
4990 align = new_sec->alignment_power;
4991 }
4992 if (align > old_alignment)
4993 h->root.u.c.p->alignment_power = align;
4994 else
4995 h->root.u.c.p->alignment_power = old_alignment;
4996 }
4997
4998 if (is_elf_hash_table (htab))
4999 {
5000 /* Set a flag in the hash table entry indicating the type of
5001 reference or definition we just found. A dynamic symbol
5002 is one which is referenced or defined by both a regular
5003 object and a shared object. */
5004 bfd_boolean dynsym = FALSE;
5005
5006 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5007 if ((abfd->flags & BFD_PLUGIN) != 0)
5008 ;
5009 else if (!dynamic)
5010 {
5011 if (! definition)
5012 {
5013 h->ref_regular = 1;
5014 if (bind != STB_WEAK)
5015 h->ref_regular_nonweak = 1;
5016 }
5017 else
5018 {
5019 h->def_regular = 1;
5020 if (h->def_dynamic)
5021 {
5022 h->def_dynamic = 0;
5023 h->ref_dynamic = 1;
5024 }
5025 }
5026 }
5027 else
5028 {
5029 if (! definition)
5030 {
5031 h->ref_dynamic = 1;
5032 hi->ref_dynamic = 1;
5033 }
5034 else
5035 {
5036 h->def_dynamic = 1;
5037 hi->def_dynamic = 1;
5038 }
5039 }
5040
5041 /* If an indirect symbol has been forced local, don't
5042 make the real symbol dynamic. */
5043 if (h != hi && hi->forced_local)
5044 ;
5045 else if (!dynamic)
5046 {
5047 if (bfd_link_dll (info)
5048 || h->def_dynamic
5049 || h->ref_dynamic)
5050 dynsym = TRUE;
5051 }
5052 else
5053 {
5054 if (h->def_regular
5055 || h->ref_regular
5056 || (h->is_weakalias
5057 && weakdef (h)->dynindx != -1))
5058 dynsym = TRUE;
5059 }
5060
5061 /* Check to see if we need to add an indirect symbol for
5062 the default name. */
5063 if (definition
5064 || (!override && h->root.type == bfd_link_hash_common))
5065 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5066 sec, value, &old_bfd, &dynsym))
5067 goto error_free_vers;
5068
5069 /* Check the alignment when a common symbol is involved. This
5070 can change when a common symbol is overridden by a normal
5071 definition or a common symbol is ignored due to the old
5072 normal definition. We need to make sure the maximum
5073 alignment is maintained. */
5074 if ((old_alignment || common)
5075 && h->root.type != bfd_link_hash_common)
5076 {
5077 unsigned int common_align;
5078 unsigned int normal_align;
5079 unsigned int symbol_align;
5080 bfd *normal_bfd;
5081 bfd *common_bfd;
5082
5083 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5084 || h->root.type == bfd_link_hash_defweak);
5085
5086 symbol_align = ffs (h->root.u.def.value) - 1;
5087 if (h->root.u.def.section->owner != NULL
5088 && (h->root.u.def.section->owner->flags
5089 & (DYNAMIC | BFD_PLUGIN)) == 0)
5090 {
5091 normal_align = h->root.u.def.section->alignment_power;
5092 if (normal_align > symbol_align)
5093 normal_align = symbol_align;
5094 }
5095 else
5096 normal_align = symbol_align;
5097
5098 if (old_alignment)
5099 {
5100 common_align = old_alignment;
5101 common_bfd = old_bfd;
5102 normal_bfd = abfd;
5103 }
5104 else
5105 {
5106 common_align = bfd_log2 (isym->st_value);
5107 common_bfd = abfd;
5108 normal_bfd = old_bfd;
5109 }
5110
5111 if (normal_align < common_align)
5112 {
5113 /* PR binutils/2735 */
5114 if (normal_bfd == NULL)
5115 _bfd_error_handler
5116 /* xgettext:c-format */
5117 (_("warning: alignment %u of common symbol `%s' in %pB is"
5118 " greater than the alignment (%u) of its section %pA"),
5119 1 << common_align, name, common_bfd,
5120 1 << normal_align, h->root.u.def.section);
5121 else
5122 _bfd_error_handler
5123 /* xgettext:c-format */
5124 (_("warning: alignment %u of symbol `%s' in %pB"
5125 " is smaller than %u in %pB"),
5126 1 << normal_align, name, normal_bfd,
5127 1 << common_align, common_bfd);
5128 }
5129 }
5130
5131 /* Remember the symbol size if it isn't undefined. */
5132 if (isym->st_size != 0
5133 && isym->st_shndx != SHN_UNDEF
5134 && (definition || h->size == 0))
5135 {
5136 if (h->size != 0
5137 && h->size != isym->st_size
5138 && ! size_change_ok)
5139 _bfd_error_handler
5140 /* xgettext:c-format */
5141 (_("warning: size of symbol `%s' changed"
5142 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5143 name, (uint64_t) h->size, old_bfd,
5144 (uint64_t) isym->st_size, abfd);
5145
5146 h->size = isym->st_size;
5147 }
5148
5149 /* If this is a common symbol, then we always want H->SIZE
5150 to be the size of the common symbol. The code just above
5151 won't fix the size if a common symbol becomes larger. We
5152 don't warn about a size change here, because that is
5153 covered by --warn-common. Allow changes between different
5154 function types. */
5155 if (h->root.type == bfd_link_hash_common)
5156 h->size = h->root.u.c.size;
5157
5158 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5159 && ((definition && !new_weak)
5160 || (old_weak && h->root.type == bfd_link_hash_common)
5161 || h->type == STT_NOTYPE))
5162 {
5163 unsigned int type = ELF_ST_TYPE (isym->st_info);
5164
5165 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5166 symbol. */
5167 if (type == STT_GNU_IFUNC
5168 && (abfd->flags & DYNAMIC) != 0)
5169 type = STT_FUNC;
5170
5171 if (h->type != type)
5172 {
5173 if (h->type != STT_NOTYPE && ! type_change_ok)
5174 /* xgettext:c-format */
5175 _bfd_error_handler
5176 (_("warning: type of symbol `%s' changed"
5177 " from %d to %d in %pB"),
5178 name, h->type, type, abfd);
5179
5180 h->type = type;
5181 }
5182 }
5183
5184 /* Merge st_other field. */
5185 elf_merge_st_other (abfd, h, isym->st_other, sec,
5186 definition, dynamic);
5187
5188 /* We don't want to make debug symbol dynamic. */
5189 if (definition
5190 && (sec->flags & SEC_DEBUGGING)
5191 && !bfd_link_relocatable (info))
5192 dynsym = FALSE;
5193
5194 /* Nor should we make plugin symbols dynamic. */
5195 if ((abfd->flags & BFD_PLUGIN) != 0)
5196 dynsym = FALSE;
5197
5198 if (definition)
5199 {
5200 h->target_internal = isym->st_target_internal;
5201 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5202 }
5203
5204 if (definition && !dynamic)
5205 {
5206 char *p = strchr (name, ELF_VER_CHR);
5207 if (p != NULL && p[1] != ELF_VER_CHR)
5208 {
5209 /* Queue non-default versions so that .symver x, x@FOO
5210 aliases can be checked. */
5211 if (!nondeflt_vers)
5212 {
5213 size_t amt = ((isymend - isym + 1)
5214 * sizeof (struct elf_link_hash_entry *));
5215 nondeflt_vers
5216 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5217 if (!nondeflt_vers)
5218 goto error_free_vers;
5219 }
5220 nondeflt_vers[nondeflt_vers_cnt++] = h;
5221 }
5222 }
5223
5224 if (dynsym && h->dynindx == -1)
5225 {
5226 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5227 goto error_free_vers;
5228 if (h->is_weakalias
5229 && weakdef (h)->dynindx == -1)
5230 {
5231 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5232 goto error_free_vers;
5233 }
5234 }
5235 else if (h->dynindx != -1)
5236 /* If the symbol already has a dynamic index, but
5237 visibility says it should not be visible, turn it into
5238 a local symbol. */
5239 switch (ELF_ST_VISIBILITY (h->other))
5240 {
5241 case STV_INTERNAL:
5242 case STV_HIDDEN:
5243 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5244 dynsym = FALSE;
5245 break;
5246 }
5247
5248 if (!add_needed
5249 && matched
5250 && definition
5251 && ((dynsym
5252 && h->ref_regular_nonweak)
5253 || (old_bfd != NULL
5254 && (old_bfd->flags & BFD_PLUGIN) != 0
5255 && bind != STB_WEAK)
5256 || (h->ref_dynamic_nonweak
5257 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5258 && !on_needed_list (elf_dt_name (abfd),
5259 htab->needed, NULL))))
5260 {
5261 const char *soname = elf_dt_name (abfd);
5262
5263 info->callbacks->minfo ("%!", soname, old_bfd,
5264 h->root.root.string);
5265
5266 /* A symbol from a library loaded via DT_NEEDED of some
5267 other library is referenced by a regular object.
5268 Add a DT_NEEDED entry for it. Issue an error if
5269 --no-add-needed is used and the reference was not
5270 a weak one. */
5271 if (old_bfd != NULL
5272 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5273 {
5274 _bfd_error_handler
5275 /* xgettext:c-format */
5276 (_("%pB: undefined reference to symbol '%s'"),
5277 old_bfd, name);
5278 bfd_set_error (bfd_error_missing_dso);
5279 goto error_free_vers;
5280 }
5281
5282 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5283 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5284
5285 /* Create dynamic sections for backends that require
5286 that be done before setup_gnu_properties. */
5287 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5288 return FALSE;
5289 add_needed = TRUE;
5290 }
5291 }
5292 }
5293
5294 if (info->lto_plugin_active
5295 && !bfd_link_relocatable (info)
5296 && (abfd->flags & BFD_PLUGIN) == 0
5297 && !just_syms
5298 && extsymcount)
5299 {
5300 int r_sym_shift;
5301
5302 if (bed->s->arch_size == 32)
5303 r_sym_shift = 8;
5304 else
5305 r_sym_shift = 32;
5306
5307 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5308 referenced in regular objects so that linker plugin will get
5309 the correct symbol resolution. */
5310
5311 sym_hash = elf_sym_hashes (abfd);
5312 for (s = abfd->sections; s != NULL; s = s->next)
5313 {
5314 Elf_Internal_Rela *internal_relocs;
5315 Elf_Internal_Rela *rel, *relend;
5316
5317 /* Don't check relocations in excluded sections. */
5318 if ((s->flags & SEC_RELOC) == 0
5319 || s->reloc_count == 0
5320 || (s->flags & SEC_EXCLUDE) != 0
5321 || ((info->strip == strip_all
5322 || info->strip == strip_debugger)
5323 && (s->flags & SEC_DEBUGGING) != 0))
5324 continue;
5325
5326 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5327 NULL,
5328 info->keep_memory);
5329 if (internal_relocs == NULL)
5330 goto error_free_vers;
5331
5332 rel = internal_relocs;
5333 relend = rel + s->reloc_count;
5334 for ( ; rel < relend; rel++)
5335 {
5336 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5337 struct elf_link_hash_entry *h;
5338
5339 /* Skip local symbols. */
5340 if (r_symndx < extsymoff)
5341 continue;
5342
5343 h = sym_hash[r_symndx - extsymoff];
5344 if (h != NULL)
5345 h->root.non_ir_ref_regular = 1;
5346 }
5347
5348 if (elf_section_data (s)->relocs != internal_relocs)
5349 free (internal_relocs);
5350 }
5351 }
5352
5353 free (extversym);
5354 extversym = NULL;
5355 free (isymbuf);
5356 isymbuf = NULL;
5357
5358 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5359 {
5360 unsigned int i;
5361
5362 /* Restore the symbol table. */
5363 old_ent = (char *) old_tab + tabsize;
5364 memset (elf_sym_hashes (abfd), 0,
5365 extsymcount * sizeof (struct elf_link_hash_entry *));
5366 htab->root.table.table = old_table;
5367 htab->root.table.size = old_size;
5368 htab->root.table.count = old_count;
5369 memcpy (htab->root.table.table, old_tab, tabsize);
5370 htab->root.undefs = old_undefs;
5371 htab->root.undefs_tail = old_undefs_tail;
5372 if (htab->dynstr != NULL)
5373 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5374 free (old_strtab);
5375 old_strtab = NULL;
5376 for (i = 0; i < htab->root.table.size; i++)
5377 {
5378 struct bfd_hash_entry *p;
5379 struct elf_link_hash_entry *h;
5380 unsigned int non_ir_ref_dynamic;
5381
5382 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5383 {
5384 /* Preserve non_ir_ref_dynamic so that this symbol
5385 will be exported when the dynamic lib becomes needed
5386 in the second pass. */
5387 h = (struct elf_link_hash_entry *) p;
5388 if (h->root.type == bfd_link_hash_warning)
5389 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5390 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5391
5392 h = (struct elf_link_hash_entry *) p;
5393 memcpy (h, old_ent, htab->root.table.entsize);
5394 old_ent = (char *) old_ent + htab->root.table.entsize;
5395 if (h->root.type == bfd_link_hash_warning)
5396 {
5397 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5398 memcpy (h, old_ent, htab->root.table.entsize);
5399 old_ent = (char *) old_ent + htab->root.table.entsize;
5400 }
5401 if (h->root.type == bfd_link_hash_common)
5402 {
5403 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5404 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5405 }
5406 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5407 }
5408 }
5409
5410 /* Make a special call to the linker "notice" function to
5411 tell it that symbols added for crefs may need to be removed. */
5412 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5413 goto error_free_vers;
5414
5415 free (old_tab);
5416 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5417 alloc_mark);
5418 free (nondeflt_vers);
5419 return TRUE;
5420 }
5421
5422 if (old_tab != NULL)
5423 {
5424 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5425 goto error_free_vers;
5426 free (old_tab);
5427 old_tab = NULL;
5428 }
5429
5430 /* Now that all the symbols from this input file are created, if
5431 not performing a relocatable link, handle .symver foo, foo@BAR
5432 such that any relocs against foo become foo@BAR. */
5433 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5434 {
5435 size_t cnt, symidx;
5436
5437 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5438 {
5439 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5440 char *shortname, *p;
5441 size_t amt;
5442
5443 p = strchr (h->root.root.string, ELF_VER_CHR);
5444 if (p == NULL
5445 || (h->root.type != bfd_link_hash_defined
5446 && h->root.type != bfd_link_hash_defweak))
5447 continue;
5448
5449 amt = p - h->root.root.string;
5450 shortname = (char *) bfd_malloc (amt + 1);
5451 if (!shortname)
5452 goto error_free_vers;
5453 memcpy (shortname, h->root.root.string, amt);
5454 shortname[amt] = '\0';
5455
5456 hi = (struct elf_link_hash_entry *)
5457 bfd_link_hash_lookup (&htab->root, shortname,
5458 FALSE, FALSE, FALSE);
5459 if (hi != NULL
5460 && hi->root.type == h->root.type
5461 && hi->root.u.def.value == h->root.u.def.value
5462 && hi->root.u.def.section == h->root.u.def.section)
5463 {
5464 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5465 hi->root.type = bfd_link_hash_indirect;
5466 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5467 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5468 sym_hash = elf_sym_hashes (abfd);
5469 if (sym_hash)
5470 for (symidx = 0; symidx < extsymcount; ++symidx)
5471 if (sym_hash[symidx] == hi)
5472 {
5473 sym_hash[symidx] = h;
5474 break;
5475 }
5476 }
5477 free (shortname);
5478 }
5479 free (nondeflt_vers);
5480 nondeflt_vers = NULL;
5481 }
5482
5483 /* Now set the alias field correctly for all the weak defined
5484 symbols we found. The only way to do this is to search all the
5485 symbols. Since we only need the information for non functions in
5486 dynamic objects, that's the only time we actually put anything on
5487 the list WEAKS. We need this information so that if a regular
5488 object refers to a symbol defined weakly in a dynamic object, the
5489 real symbol in the dynamic object is also put in the dynamic
5490 symbols; we also must arrange for both symbols to point to the
5491 same memory location. We could handle the general case of symbol
5492 aliasing, but a general symbol alias can only be generated in
5493 assembler code, handling it correctly would be very time
5494 consuming, and other ELF linkers don't handle general aliasing
5495 either. */
5496 if (weaks != NULL)
5497 {
5498 struct elf_link_hash_entry **hpp;
5499 struct elf_link_hash_entry **hppend;
5500 struct elf_link_hash_entry **sorted_sym_hash;
5501 struct elf_link_hash_entry *h;
5502 size_t sym_count, amt;
5503
5504 /* Since we have to search the whole symbol list for each weak
5505 defined symbol, search time for N weak defined symbols will be
5506 O(N^2). Binary search will cut it down to O(NlogN). */
5507 amt = extsymcount * sizeof (*sorted_sym_hash);
5508 sorted_sym_hash = bfd_malloc (amt);
5509 if (sorted_sym_hash == NULL)
5510 goto error_return;
5511 sym_hash = sorted_sym_hash;
5512 hpp = elf_sym_hashes (abfd);
5513 hppend = hpp + extsymcount;
5514 sym_count = 0;
5515 for (; hpp < hppend; hpp++)
5516 {
5517 h = *hpp;
5518 if (h != NULL
5519 && h->root.type == bfd_link_hash_defined
5520 && !bed->is_function_type (h->type))
5521 {
5522 *sym_hash = h;
5523 sym_hash++;
5524 sym_count++;
5525 }
5526 }
5527
5528 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5529 elf_sort_symbol);
5530
5531 while (weaks != NULL)
5532 {
5533 struct elf_link_hash_entry *hlook;
5534 asection *slook;
5535 bfd_vma vlook;
5536 size_t i, j, idx = 0;
5537
5538 hlook = weaks;
5539 weaks = hlook->u.alias;
5540 hlook->u.alias = NULL;
5541
5542 if (hlook->root.type != bfd_link_hash_defined
5543 && hlook->root.type != bfd_link_hash_defweak)
5544 continue;
5545
5546 slook = hlook->root.u.def.section;
5547 vlook = hlook->root.u.def.value;
5548
5549 i = 0;
5550 j = sym_count;
5551 while (i != j)
5552 {
5553 bfd_signed_vma vdiff;
5554 idx = (i + j) / 2;
5555 h = sorted_sym_hash[idx];
5556 vdiff = vlook - h->root.u.def.value;
5557 if (vdiff < 0)
5558 j = idx;
5559 else if (vdiff > 0)
5560 i = idx + 1;
5561 else
5562 {
5563 int sdiff = slook->id - h->root.u.def.section->id;
5564 if (sdiff < 0)
5565 j = idx;
5566 else if (sdiff > 0)
5567 i = idx + 1;
5568 else
5569 break;
5570 }
5571 }
5572
5573 /* We didn't find a value/section match. */
5574 if (i == j)
5575 continue;
5576
5577 /* With multiple aliases, or when the weak symbol is already
5578 strongly defined, we have multiple matching symbols and
5579 the binary search above may land on any of them. Step
5580 one past the matching symbol(s). */
5581 while (++idx != j)
5582 {
5583 h = sorted_sym_hash[idx];
5584 if (h->root.u.def.section != slook
5585 || h->root.u.def.value != vlook)
5586 break;
5587 }
5588
5589 /* Now look back over the aliases. Since we sorted by size
5590 as well as value and section, we'll choose the one with
5591 the largest size. */
5592 while (idx-- != i)
5593 {
5594 h = sorted_sym_hash[idx];
5595
5596 /* Stop if value or section doesn't match. */
5597 if (h->root.u.def.section != slook
5598 || h->root.u.def.value != vlook)
5599 break;
5600 else if (h != hlook)
5601 {
5602 struct elf_link_hash_entry *t;
5603
5604 hlook->u.alias = h;
5605 hlook->is_weakalias = 1;
5606 t = h;
5607 if (t->u.alias != NULL)
5608 while (t->u.alias != h)
5609 t = t->u.alias;
5610 t->u.alias = hlook;
5611
5612 /* If the weak definition is in the list of dynamic
5613 symbols, make sure the real definition is put
5614 there as well. */
5615 if (hlook->dynindx != -1 && h->dynindx == -1)
5616 {
5617 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5618 {
5619 err_free_sym_hash:
5620 free (sorted_sym_hash);
5621 goto error_return;
5622 }
5623 }
5624
5625 /* If the real definition is in the list of dynamic
5626 symbols, make sure the weak definition is put
5627 there as well. If we don't do this, then the
5628 dynamic loader might not merge the entries for the
5629 real definition and the weak definition. */
5630 if (h->dynindx != -1 && hlook->dynindx == -1)
5631 {
5632 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5633 goto err_free_sym_hash;
5634 }
5635 break;
5636 }
5637 }
5638 }
5639
5640 free (sorted_sym_hash);
5641 }
5642
5643 if (bed->check_directives
5644 && !(*bed->check_directives) (abfd, info))
5645 return FALSE;
5646
5647 /* If this is a non-traditional link, try to optimize the handling
5648 of the .stab/.stabstr sections. */
5649 if (! dynamic
5650 && ! info->traditional_format
5651 && is_elf_hash_table (htab)
5652 && (info->strip != strip_all && info->strip != strip_debugger))
5653 {
5654 asection *stabstr;
5655
5656 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5657 if (stabstr != NULL)
5658 {
5659 bfd_size_type string_offset = 0;
5660 asection *stab;
5661
5662 for (stab = abfd->sections; stab; stab = stab->next)
5663 if (CONST_STRNEQ (stab->name, ".stab")
5664 && (!stab->name[5] ||
5665 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5666 && (stab->flags & SEC_MERGE) == 0
5667 && !bfd_is_abs_section (stab->output_section))
5668 {
5669 struct bfd_elf_section_data *secdata;
5670
5671 secdata = elf_section_data (stab);
5672 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5673 stabstr, &secdata->sec_info,
5674 &string_offset))
5675 goto error_return;
5676 if (secdata->sec_info)
5677 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5678 }
5679 }
5680 }
5681
5682 if (dynamic && add_needed)
5683 {
5684 /* Add this bfd to the loaded list. */
5685 struct elf_link_loaded_list *n;
5686
5687 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5688 if (n == NULL)
5689 goto error_return;
5690 n->abfd = abfd;
5691 n->next = htab->dyn_loaded;
5692 htab->dyn_loaded = n;
5693 }
5694 if (dynamic && !add_needed
5695 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5696 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5697
5698 return TRUE;
5699
5700 error_free_vers:
5701 free (old_tab);
5702 free (old_strtab);
5703 free (nondeflt_vers);
5704 free (extversym);
5705 error_free_sym:
5706 free (isymbuf);
5707 error_return:
5708 return FALSE;
5709 }
5710
5711 /* Return the linker hash table entry of a symbol that might be
5712 satisfied by an archive symbol. Return -1 on error. */
5713
5714 struct elf_link_hash_entry *
5715 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5716 struct bfd_link_info *info,
5717 const char *name)
5718 {
5719 struct elf_link_hash_entry *h;
5720 char *p, *copy;
5721 size_t len, first;
5722
5723 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5724 if (h != NULL)
5725 return h;
5726
5727 /* If this is a default version (the name contains @@), look up the
5728 symbol again with only one `@' as well as without the version.
5729 The effect is that references to the symbol with and without the
5730 version will be matched by the default symbol in the archive. */
5731
5732 p = strchr (name, ELF_VER_CHR);
5733 if (p == NULL || p[1] != ELF_VER_CHR)
5734 return h;
5735
5736 /* First check with only one `@'. */
5737 len = strlen (name);
5738 copy = (char *) bfd_alloc (abfd, len);
5739 if (copy == NULL)
5740 return (struct elf_link_hash_entry *) -1;
5741
5742 first = p - name + 1;
5743 memcpy (copy, name, first);
5744 memcpy (copy + first, name + first + 1, len - first);
5745
5746 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5747 if (h == NULL)
5748 {
5749 /* We also need to check references to the symbol without the
5750 version. */
5751 copy[first - 1] = '\0';
5752 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5753 FALSE, FALSE, TRUE);
5754 }
5755
5756 bfd_release (abfd, copy);
5757 return h;
5758 }
5759
5760 /* Add symbols from an ELF archive file to the linker hash table. We
5761 don't use _bfd_generic_link_add_archive_symbols because we need to
5762 handle versioned symbols.
5763
5764 Fortunately, ELF archive handling is simpler than that done by
5765 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5766 oddities. In ELF, if we find a symbol in the archive map, and the
5767 symbol is currently undefined, we know that we must pull in that
5768 object file.
5769
5770 Unfortunately, we do have to make multiple passes over the symbol
5771 table until nothing further is resolved. */
5772
5773 static bfd_boolean
5774 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5775 {
5776 symindex c;
5777 unsigned char *included = NULL;
5778 carsym *symdefs;
5779 bfd_boolean loop;
5780 size_t amt;
5781 const struct elf_backend_data *bed;
5782 struct elf_link_hash_entry * (*archive_symbol_lookup)
5783 (bfd *, struct bfd_link_info *, const char *);
5784
5785 if (! bfd_has_map (abfd))
5786 {
5787 /* An empty archive is a special case. */
5788 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5789 return TRUE;
5790 bfd_set_error (bfd_error_no_armap);
5791 return FALSE;
5792 }
5793
5794 /* Keep track of all symbols we know to be already defined, and all
5795 files we know to be already included. This is to speed up the
5796 second and subsequent passes. */
5797 c = bfd_ardata (abfd)->symdef_count;
5798 if (c == 0)
5799 return TRUE;
5800 amt = c * sizeof (*included);
5801 included = (unsigned char *) bfd_zmalloc (amt);
5802 if (included == NULL)
5803 return FALSE;
5804
5805 symdefs = bfd_ardata (abfd)->symdefs;
5806 bed = get_elf_backend_data (abfd);
5807 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5808
5809 do
5810 {
5811 file_ptr last;
5812 symindex i;
5813 carsym *symdef;
5814 carsym *symdefend;
5815
5816 loop = FALSE;
5817 last = -1;
5818
5819 symdef = symdefs;
5820 symdefend = symdef + c;
5821 for (i = 0; symdef < symdefend; symdef++, i++)
5822 {
5823 struct elf_link_hash_entry *h;
5824 bfd *element;
5825 struct bfd_link_hash_entry *undefs_tail;
5826 symindex mark;
5827
5828 if (included[i])
5829 continue;
5830 if (symdef->file_offset == last)
5831 {
5832 included[i] = TRUE;
5833 continue;
5834 }
5835
5836 h = archive_symbol_lookup (abfd, info, symdef->name);
5837 if (h == (struct elf_link_hash_entry *) -1)
5838 goto error_return;
5839
5840 if (h == NULL)
5841 continue;
5842
5843 if (h->root.type == bfd_link_hash_undefined)
5844 {
5845 /* If the archive element has already been loaded then one
5846 of the symbols defined by that element might have been
5847 made undefined due to being in a discarded section. */
5848 if (h->indx == -3)
5849 continue;
5850 }
5851 else if (h->root.type == bfd_link_hash_common)
5852 {
5853 /* We currently have a common symbol. The archive map contains
5854 a reference to this symbol, so we may want to include it. We
5855 only want to include it however, if this archive element
5856 contains a definition of the symbol, not just another common
5857 declaration of it.
5858
5859 Unfortunately some archivers (including GNU ar) will put
5860 declarations of common symbols into their archive maps, as
5861 well as real definitions, so we cannot just go by the archive
5862 map alone. Instead we must read in the element's symbol
5863 table and check that to see what kind of symbol definition
5864 this is. */
5865 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5866 continue;
5867 }
5868 else
5869 {
5870 if (h->root.type != bfd_link_hash_undefweak)
5871 /* Symbol must be defined. Don't check it again. */
5872 included[i] = TRUE;
5873 continue;
5874 }
5875
5876 /* We need to include this archive member. */
5877 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5878 if (element == NULL)
5879 goto error_return;
5880
5881 if (! bfd_check_format (element, bfd_object))
5882 goto error_return;
5883
5884 undefs_tail = info->hash->undefs_tail;
5885
5886 if (!(*info->callbacks
5887 ->add_archive_element) (info, element, symdef->name, &element))
5888 continue;
5889 if (!bfd_link_add_symbols (element, info))
5890 goto error_return;
5891
5892 /* If there are any new undefined symbols, we need to make
5893 another pass through the archive in order to see whether
5894 they can be defined. FIXME: This isn't perfect, because
5895 common symbols wind up on undefs_tail and because an
5896 undefined symbol which is defined later on in this pass
5897 does not require another pass. This isn't a bug, but it
5898 does make the code less efficient than it could be. */
5899 if (undefs_tail != info->hash->undefs_tail)
5900 loop = TRUE;
5901
5902 /* Look backward to mark all symbols from this object file
5903 which we have already seen in this pass. */
5904 mark = i;
5905 do
5906 {
5907 included[mark] = TRUE;
5908 if (mark == 0)
5909 break;
5910 --mark;
5911 }
5912 while (symdefs[mark].file_offset == symdef->file_offset);
5913
5914 /* We mark subsequent symbols from this object file as we go
5915 on through the loop. */
5916 last = symdef->file_offset;
5917 }
5918 }
5919 while (loop);
5920
5921 free (included);
5922 return TRUE;
5923
5924 error_return:
5925 free (included);
5926 return FALSE;
5927 }
5928
5929 /* Given an ELF BFD, add symbols to the global hash table as
5930 appropriate. */
5931
5932 bfd_boolean
5933 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5934 {
5935 switch (bfd_get_format (abfd))
5936 {
5937 case bfd_object:
5938 return elf_link_add_object_symbols (abfd, info);
5939 case bfd_archive:
5940 return elf_link_add_archive_symbols (abfd, info);
5941 default:
5942 bfd_set_error (bfd_error_wrong_format);
5943 return FALSE;
5944 }
5945 }
5946 \f
5947 struct hash_codes_info
5948 {
5949 unsigned long *hashcodes;
5950 bfd_boolean error;
5951 };
5952
5953 /* This function will be called though elf_link_hash_traverse to store
5954 all hash value of the exported symbols in an array. */
5955
5956 static bfd_boolean
5957 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5958 {
5959 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5960 const char *name;
5961 unsigned long ha;
5962 char *alc = NULL;
5963
5964 /* Ignore indirect symbols. These are added by the versioning code. */
5965 if (h->dynindx == -1)
5966 return TRUE;
5967
5968 name = h->root.root.string;
5969 if (h->versioned >= versioned)
5970 {
5971 char *p = strchr (name, ELF_VER_CHR);
5972 if (p != NULL)
5973 {
5974 alc = (char *) bfd_malloc (p - name + 1);
5975 if (alc == NULL)
5976 {
5977 inf->error = TRUE;
5978 return FALSE;
5979 }
5980 memcpy (alc, name, p - name);
5981 alc[p - name] = '\0';
5982 name = alc;
5983 }
5984 }
5985
5986 /* Compute the hash value. */
5987 ha = bfd_elf_hash (name);
5988
5989 /* Store the found hash value in the array given as the argument. */
5990 *(inf->hashcodes)++ = ha;
5991
5992 /* And store it in the struct so that we can put it in the hash table
5993 later. */
5994 h->u.elf_hash_value = ha;
5995
5996 free (alc);
5997 return TRUE;
5998 }
5999
6000 struct collect_gnu_hash_codes
6001 {
6002 bfd *output_bfd;
6003 const struct elf_backend_data *bed;
6004 unsigned long int nsyms;
6005 unsigned long int maskbits;
6006 unsigned long int *hashcodes;
6007 unsigned long int *hashval;
6008 unsigned long int *indx;
6009 unsigned long int *counts;
6010 bfd_vma *bitmask;
6011 bfd_byte *contents;
6012 bfd_size_type xlat;
6013 long int min_dynindx;
6014 unsigned long int bucketcount;
6015 unsigned long int symindx;
6016 long int local_indx;
6017 long int shift1, shift2;
6018 unsigned long int mask;
6019 bfd_boolean error;
6020 };
6021
6022 /* This function will be called though elf_link_hash_traverse to store
6023 all hash value of the exported symbols in an array. */
6024
6025 static bfd_boolean
6026 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6027 {
6028 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6029 const char *name;
6030 unsigned long ha;
6031 char *alc = NULL;
6032
6033 /* Ignore indirect symbols. These are added by the versioning code. */
6034 if (h->dynindx == -1)
6035 return TRUE;
6036
6037 /* Ignore also local symbols and undefined symbols. */
6038 if (! (*s->bed->elf_hash_symbol) (h))
6039 return TRUE;
6040
6041 name = h->root.root.string;
6042 if (h->versioned >= versioned)
6043 {
6044 char *p = strchr (name, ELF_VER_CHR);
6045 if (p != NULL)
6046 {
6047 alc = (char *) bfd_malloc (p - name + 1);
6048 if (alc == NULL)
6049 {
6050 s->error = TRUE;
6051 return FALSE;
6052 }
6053 memcpy (alc, name, p - name);
6054 alc[p - name] = '\0';
6055 name = alc;
6056 }
6057 }
6058
6059 /* Compute the hash value. */
6060 ha = bfd_elf_gnu_hash (name);
6061
6062 /* Store the found hash value in the array for compute_bucket_count,
6063 and also for .dynsym reordering purposes. */
6064 s->hashcodes[s->nsyms] = ha;
6065 s->hashval[h->dynindx] = ha;
6066 ++s->nsyms;
6067 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6068 s->min_dynindx = h->dynindx;
6069
6070 free (alc);
6071 return TRUE;
6072 }
6073
6074 /* This function will be called though elf_link_hash_traverse to do
6075 final dynamic symbol renumbering in case of .gnu.hash.
6076 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6077 to the translation table. */
6078
6079 static bfd_boolean
6080 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6081 {
6082 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6083 unsigned long int bucket;
6084 unsigned long int val;
6085
6086 /* Ignore indirect symbols. */
6087 if (h->dynindx == -1)
6088 return TRUE;
6089
6090 /* Ignore also local symbols and undefined symbols. */
6091 if (! (*s->bed->elf_hash_symbol) (h))
6092 {
6093 if (h->dynindx >= s->min_dynindx)
6094 {
6095 if (s->bed->record_xhash_symbol != NULL)
6096 {
6097 (*s->bed->record_xhash_symbol) (h, 0);
6098 s->local_indx++;
6099 }
6100 else
6101 h->dynindx = s->local_indx++;
6102 }
6103 return TRUE;
6104 }
6105
6106 bucket = s->hashval[h->dynindx] % s->bucketcount;
6107 val = (s->hashval[h->dynindx] >> s->shift1)
6108 & ((s->maskbits >> s->shift1) - 1);
6109 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6110 s->bitmask[val]
6111 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6112 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6113 if (s->counts[bucket] == 1)
6114 /* Last element terminates the chain. */
6115 val |= 1;
6116 bfd_put_32 (s->output_bfd, val,
6117 s->contents + (s->indx[bucket] - s->symindx) * 4);
6118 --s->counts[bucket];
6119 if (s->bed->record_xhash_symbol != NULL)
6120 {
6121 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6122
6123 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6124 }
6125 else
6126 h->dynindx = s->indx[bucket]++;
6127 return TRUE;
6128 }
6129
6130 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6131
6132 bfd_boolean
6133 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6134 {
6135 return !(h->forced_local
6136 || h->root.type == bfd_link_hash_undefined
6137 || h->root.type == bfd_link_hash_undefweak
6138 || ((h->root.type == bfd_link_hash_defined
6139 || h->root.type == bfd_link_hash_defweak)
6140 && h->root.u.def.section->output_section == NULL));
6141 }
6142
6143 /* Array used to determine the number of hash table buckets to use
6144 based on the number of symbols there are. If there are fewer than
6145 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6146 fewer than 37 we use 17 buckets, and so forth. We never use more
6147 than 32771 buckets. */
6148
6149 static const size_t elf_buckets[] =
6150 {
6151 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6152 16411, 32771, 0
6153 };
6154
6155 /* Compute bucket count for hashing table. We do not use a static set
6156 of possible tables sizes anymore. Instead we determine for all
6157 possible reasonable sizes of the table the outcome (i.e., the
6158 number of collisions etc) and choose the best solution. The
6159 weighting functions are not too simple to allow the table to grow
6160 without bounds. Instead one of the weighting factors is the size.
6161 Therefore the result is always a good payoff between few collisions
6162 (= short chain lengths) and table size. */
6163 static size_t
6164 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6165 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6166 unsigned long int nsyms,
6167 int gnu_hash)
6168 {
6169 size_t best_size = 0;
6170 unsigned long int i;
6171
6172 /* We have a problem here. The following code to optimize the table
6173 size requires an integer type with more the 32 bits. If
6174 BFD_HOST_U_64_BIT is set we know about such a type. */
6175 #ifdef BFD_HOST_U_64_BIT
6176 if (info->optimize)
6177 {
6178 size_t minsize;
6179 size_t maxsize;
6180 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6181 bfd *dynobj = elf_hash_table (info)->dynobj;
6182 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6183 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6184 unsigned long int *counts;
6185 bfd_size_type amt;
6186 unsigned int no_improvement_count = 0;
6187
6188 /* Possible optimization parameters: if we have NSYMS symbols we say
6189 that the hashing table must at least have NSYMS/4 and at most
6190 2*NSYMS buckets. */
6191 minsize = nsyms / 4;
6192 if (minsize == 0)
6193 minsize = 1;
6194 best_size = maxsize = nsyms * 2;
6195 if (gnu_hash)
6196 {
6197 if (minsize < 2)
6198 minsize = 2;
6199 if ((best_size & 31) == 0)
6200 ++best_size;
6201 }
6202
6203 /* Create array where we count the collisions in. We must use bfd_malloc
6204 since the size could be large. */
6205 amt = maxsize;
6206 amt *= sizeof (unsigned long int);
6207 counts = (unsigned long int *) bfd_malloc (amt);
6208 if (counts == NULL)
6209 return 0;
6210
6211 /* Compute the "optimal" size for the hash table. The criteria is a
6212 minimal chain length. The minor criteria is (of course) the size
6213 of the table. */
6214 for (i = minsize; i < maxsize; ++i)
6215 {
6216 /* Walk through the array of hashcodes and count the collisions. */
6217 BFD_HOST_U_64_BIT max;
6218 unsigned long int j;
6219 unsigned long int fact;
6220
6221 if (gnu_hash && (i & 31) == 0)
6222 continue;
6223
6224 memset (counts, '\0', i * sizeof (unsigned long int));
6225
6226 /* Determine how often each hash bucket is used. */
6227 for (j = 0; j < nsyms; ++j)
6228 ++counts[hashcodes[j] % i];
6229
6230 /* For the weight function we need some information about the
6231 pagesize on the target. This is information need not be 100%
6232 accurate. Since this information is not available (so far) we
6233 define it here to a reasonable default value. If it is crucial
6234 to have a better value some day simply define this value. */
6235 # ifndef BFD_TARGET_PAGESIZE
6236 # define BFD_TARGET_PAGESIZE (4096)
6237 # endif
6238
6239 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6240 and the chains. */
6241 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6242
6243 # if 1
6244 /* Variant 1: optimize for short chains. We add the squares
6245 of all the chain lengths (which favors many small chain
6246 over a few long chains). */
6247 for (j = 0; j < i; ++j)
6248 max += counts[j] * counts[j];
6249
6250 /* This adds penalties for the overall size of the table. */
6251 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6252 max *= fact * fact;
6253 # else
6254 /* Variant 2: Optimize a lot more for small table. Here we
6255 also add squares of the size but we also add penalties for
6256 empty slots (the +1 term). */
6257 for (j = 0; j < i; ++j)
6258 max += (1 + counts[j]) * (1 + counts[j]);
6259
6260 /* The overall size of the table is considered, but not as
6261 strong as in variant 1, where it is squared. */
6262 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6263 max *= fact;
6264 # endif
6265
6266 /* Compare with current best results. */
6267 if (max < best_chlen)
6268 {
6269 best_chlen = max;
6270 best_size = i;
6271 no_improvement_count = 0;
6272 }
6273 /* PR 11843: Avoid futile long searches for the best bucket size
6274 when there are a large number of symbols. */
6275 else if (++no_improvement_count == 100)
6276 break;
6277 }
6278
6279 free (counts);
6280 }
6281 else
6282 #endif /* defined (BFD_HOST_U_64_BIT) */
6283 {
6284 /* This is the fallback solution if no 64bit type is available or if we
6285 are not supposed to spend much time on optimizations. We select the
6286 bucket count using a fixed set of numbers. */
6287 for (i = 0; elf_buckets[i] != 0; i++)
6288 {
6289 best_size = elf_buckets[i];
6290 if (nsyms < elf_buckets[i + 1])
6291 break;
6292 }
6293 if (gnu_hash && best_size < 2)
6294 best_size = 2;
6295 }
6296
6297 return best_size;
6298 }
6299
6300 /* Size any SHT_GROUP section for ld -r. */
6301
6302 bfd_boolean
6303 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6304 {
6305 bfd *ibfd;
6306 asection *s;
6307
6308 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6309 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6310 && (s = ibfd->sections) != NULL
6311 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6312 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6313 return FALSE;
6314 return TRUE;
6315 }
6316
6317 /* Set a default stack segment size. The value in INFO wins. If it
6318 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6319 undefined it is initialized. */
6320
6321 bfd_boolean
6322 bfd_elf_stack_segment_size (bfd *output_bfd,
6323 struct bfd_link_info *info,
6324 const char *legacy_symbol,
6325 bfd_vma default_size)
6326 {
6327 struct elf_link_hash_entry *h = NULL;
6328
6329 /* Look for legacy symbol. */
6330 if (legacy_symbol)
6331 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6332 FALSE, FALSE, FALSE);
6333 if (h && (h->root.type == bfd_link_hash_defined
6334 || h->root.type == bfd_link_hash_defweak)
6335 && h->def_regular
6336 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6337 {
6338 /* The symbol has no type if specified on the command line. */
6339 h->type = STT_OBJECT;
6340 if (info->stacksize)
6341 /* xgettext:c-format */
6342 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6343 output_bfd, legacy_symbol);
6344 else if (h->root.u.def.section != bfd_abs_section_ptr)
6345 /* xgettext:c-format */
6346 _bfd_error_handler (_("%pB: %s not absolute"),
6347 output_bfd, legacy_symbol);
6348 else
6349 info->stacksize = h->root.u.def.value;
6350 }
6351
6352 if (!info->stacksize)
6353 /* If the user didn't set a size, or explicitly inhibit the
6354 size, set it now. */
6355 info->stacksize = default_size;
6356
6357 /* Provide the legacy symbol, if it is referenced. */
6358 if (h && (h->root.type == bfd_link_hash_undefined
6359 || h->root.type == bfd_link_hash_undefweak))
6360 {
6361 struct bfd_link_hash_entry *bh = NULL;
6362
6363 if (!(_bfd_generic_link_add_one_symbol
6364 (info, output_bfd, legacy_symbol,
6365 BSF_GLOBAL, bfd_abs_section_ptr,
6366 info->stacksize >= 0 ? info->stacksize : 0,
6367 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6368 return FALSE;
6369
6370 h = (struct elf_link_hash_entry *) bh;
6371 h->def_regular = 1;
6372 h->type = STT_OBJECT;
6373 }
6374
6375 return TRUE;
6376 }
6377
6378 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6379
6380 struct elf_gc_sweep_symbol_info
6381 {
6382 struct bfd_link_info *info;
6383 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6384 bfd_boolean);
6385 };
6386
6387 static bfd_boolean
6388 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6389 {
6390 if (!h->mark
6391 && (((h->root.type == bfd_link_hash_defined
6392 || h->root.type == bfd_link_hash_defweak)
6393 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6394 && h->root.u.def.section->gc_mark))
6395 || h->root.type == bfd_link_hash_undefined
6396 || h->root.type == bfd_link_hash_undefweak))
6397 {
6398 struct elf_gc_sweep_symbol_info *inf;
6399
6400 inf = (struct elf_gc_sweep_symbol_info *) data;
6401 (*inf->hide_symbol) (inf->info, h, TRUE);
6402 h->def_regular = 0;
6403 h->ref_regular = 0;
6404 h->ref_regular_nonweak = 0;
6405 }
6406
6407 return TRUE;
6408 }
6409
6410 /* Set up the sizes and contents of the ELF dynamic sections. This is
6411 called by the ELF linker emulation before_allocation routine. We
6412 must set the sizes of the sections before the linker sets the
6413 addresses of the various sections. */
6414
6415 bfd_boolean
6416 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6417 const char *soname,
6418 const char *rpath,
6419 const char *filter_shlib,
6420 const char *audit,
6421 const char *depaudit,
6422 const char * const *auxiliary_filters,
6423 struct bfd_link_info *info,
6424 asection **sinterpptr)
6425 {
6426 bfd *dynobj;
6427 const struct elf_backend_data *bed;
6428
6429 *sinterpptr = NULL;
6430
6431 if (!is_elf_hash_table (info->hash))
6432 return TRUE;
6433
6434 dynobj = elf_hash_table (info)->dynobj;
6435
6436 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6437 {
6438 struct bfd_elf_version_tree *verdefs;
6439 struct elf_info_failed asvinfo;
6440 struct bfd_elf_version_tree *t;
6441 struct bfd_elf_version_expr *d;
6442 asection *s;
6443 size_t soname_indx;
6444
6445 /* If we are supposed to export all symbols into the dynamic symbol
6446 table (this is not the normal case), then do so. */
6447 if (info->export_dynamic
6448 || (bfd_link_executable (info) && info->dynamic))
6449 {
6450 struct elf_info_failed eif;
6451
6452 eif.info = info;
6453 eif.failed = FALSE;
6454 elf_link_hash_traverse (elf_hash_table (info),
6455 _bfd_elf_export_symbol,
6456 &eif);
6457 if (eif.failed)
6458 return FALSE;
6459 }
6460
6461 if (soname != NULL)
6462 {
6463 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6464 soname, TRUE);
6465 if (soname_indx == (size_t) -1
6466 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6467 return FALSE;
6468 }
6469 else
6470 soname_indx = (size_t) -1;
6471
6472 /* Make all global versions with definition. */
6473 for (t = info->version_info; t != NULL; t = t->next)
6474 for (d = t->globals.list; d != NULL; d = d->next)
6475 if (!d->symver && d->literal)
6476 {
6477 const char *verstr, *name;
6478 size_t namelen, verlen, newlen;
6479 char *newname, *p, leading_char;
6480 struct elf_link_hash_entry *newh;
6481
6482 leading_char = bfd_get_symbol_leading_char (output_bfd);
6483 name = d->pattern;
6484 namelen = strlen (name) + (leading_char != '\0');
6485 verstr = t->name;
6486 verlen = strlen (verstr);
6487 newlen = namelen + verlen + 3;
6488
6489 newname = (char *) bfd_malloc (newlen);
6490 if (newname == NULL)
6491 return FALSE;
6492 newname[0] = leading_char;
6493 memcpy (newname + (leading_char != '\0'), name, namelen);
6494
6495 /* Check the hidden versioned definition. */
6496 p = newname + namelen;
6497 *p++ = ELF_VER_CHR;
6498 memcpy (p, verstr, verlen + 1);
6499 newh = elf_link_hash_lookup (elf_hash_table (info),
6500 newname, FALSE, FALSE,
6501 FALSE);
6502 if (newh == NULL
6503 || (newh->root.type != bfd_link_hash_defined
6504 && newh->root.type != bfd_link_hash_defweak))
6505 {
6506 /* Check the default versioned definition. */
6507 *p++ = ELF_VER_CHR;
6508 memcpy (p, verstr, verlen + 1);
6509 newh = elf_link_hash_lookup (elf_hash_table (info),
6510 newname, FALSE, FALSE,
6511 FALSE);
6512 }
6513 free (newname);
6514
6515 /* Mark this version if there is a definition and it is
6516 not defined in a shared object. */
6517 if (newh != NULL
6518 && !newh->def_dynamic
6519 && (newh->root.type == bfd_link_hash_defined
6520 || newh->root.type == bfd_link_hash_defweak))
6521 d->symver = 1;
6522 }
6523
6524 /* Attach all the symbols to their version information. */
6525 asvinfo.info = info;
6526 asvinfo.failed = FALSE;
6527
6528 elf_link_hash_traverse (elf_hash_table (info),
6529 _bfd_elf_link_assign_sym_version,
6530 &asvinfo);
6531 if (asvinfo.failed)
6532 return FALSE;
6533
6534 if (!info->allow_undefined_version)
6535 {
6536 /* Check if all global versions have a definition. */
6537 bfd_boolean all_defined = TRUE;
6538 for (t = info->version_info; t != NULL; t = t->next)
6539 for (d = t->globals.list; d != NULL; d = d->next)
6540 if (d->literal && !d->symver && !d->script)
6541 {
6542 _bfd_error_handler
6543 (_("%s: undefined version: %s"),
6544 d->pattern, t->name);
6545 all_defined = FALSE;
6546 }
6547
6548 if (!all_defined)
6549 {
6550 bfd_set_error (bfd_error_bad_value);
6551 return FALSE;
6552 }
6553 }
6554
6555 /* Set up the version definition section. */
6556 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6557 BFD_ASSERT (s != NULL);
6558
6559 /* We may have created additional version definitions if we are
6560 just linking a regular application. */
6561 verdefs = info->version_info;
6562
6563 /* Skip anonymous version tag. */
6564 if (verdefs != NULL && verdefs->vernum == 0)
6565 verdefs = verdefs->next;
6566
6567 if (verdefs == NULL && !info->create_default_symver)
6568 s->flags |= SEC_EXCLUDE;
6569 else
6570 {
6571 unsigned int cdefs;
6572 bfd_size_type size;
6573 bfd_byte *p;
6574 Elf_Internal_Verdef def;
6575 Elf_Internal_Verdaux defaux;
6576 struct bfd_link_hash_entry *bh;
6577 struct elf_link_hash_entry *h;
6578 const char *name;
6579
6580 cdefs = 0;
6581 size = 0;
6582
6583 /* Make space for the base version. */
6584 size += sizeof (Elf_External_Verdef);
6585 size += sizeof (Elf_External_Verdaux);
6586 ++cdefs;
6587
6588 /* Make space for the default version. */
6589 if (info->create_default_symver)
6590 {
6591 size += sizeof (Elf_External_Verdef);
6592 ++cdefs;
6593 }
6594
6595 for (t = verdefs; t != NULL; t = t->next)
6596 {
6597 struct bfd_elf_version_deps *n;
6598
6599 /* Don't emit base version twice. */
6600 if (t->vernum == 0)
6601 continue;
6602
6603 size += sizeof (Elf_External_Verdef);
6604 size += sizeof (Elf_External_Verdaux);
6605 ++cdefs;
6606
6607 for (n = t->deps; n != NULL; n = n->next)
6608 size += sizeof (Elf_External_Verdaux);
6609 }
6610
6611 s->size = size;
6612 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6613 if (s->contents == NULL && s->size != 0)
6614 return FALSE;
6615
6616 /* Fill in the version definition section. */
6617
6618 p = s->contents;
6619
6620 def.vd_version = VER_DEF_CURRENT;
6621 def.vd_flags = VER_FLG_BASE;
6622 def.vd_ndx = 1;
6623 def.vd_cnt = 1;
6624 if (info->create_default_symver)
6625 {
6626 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6627 def.vd_next = sizeof (Elf_External_Verdef);
6628 }
6629 else
6630 {
6631 def.vd_aux = sizeof (Elf_External_Verdef);
6632 def.vd_next = (sizeof (Elf_External_Verdef)
6633 + sizeof (Elf_External_Verdaux));
6634 }
6635
6636 if (soname_indx != (size_t) -1)
6637 {
6638 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6639 soname_indx);
6640 def.vd_hash = bfd_elf_hash (soname);
6641 defaux.vda_name = soname_indx;
6642 name = soname;
6643 }
6644 else
6645 {
6646 size_t indx;
6647
6648 name = lbasename (bfd_get_filename (output_bfd));
6649 def.vd_hash = bfd_elf_hash (name);
6650 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6651 name, FALSE);
6652 if (indx == (size_t) -1)
6653 return FALSE;
6654 defaux.vda_name = indx;
6655 }
6656 defaux.vda_next = 0;
6657
6658 _bfd_elf_swap_verdef_out (output_bfd, &def,
6659 (Elf_External_Verdef *) p);
6660 p += sizeof (Elf_External_Verdef);
6661 if (info->create_default_symver)
6662 {
6663 /* Add a symbol representing this version. */
6664 bh = NULL;
6665 if (! (_bfd_generic_link_add_one_symbol
6666 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6667 0, NULL, FALSE,
6668 get_elf_backend_data (dynobj)->collect, &bh)))
6669 return FALSE;
6670 h = (struct elf_link_hash_entry *) bh;
6671 h->non_elf = 0;
6672 h->def_regular = 1;
6673 h->type = STT_OBJECT;
6674 h->verinfo.vertree = NULL;
6675
6676 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6677 return FALSE;
6678
6679 /* Create a duplicate of the base version with the same
6680 aux block, but different flags. */
6681 def.vd_flags = 0;
6682 def.vd_ndx = 2;
6683 def.vd_aux = sizeof (Elf_External_Verdef);
6684 if (verdefs)
6685 def.vd_next = (sizeof (Elf_External_Verdef)
6686 + sizeof (Elf_External_Verdaux));
6687 else
6688 def.vd_next = 0;
6689 _bfd_elf_swap_verdef_out (output_bfd, &def,
6690 (Elf_External_Verdef *) p);
6691 p += sizeof (Elf_External_Verdef);
6692 }
6693 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6694 (Elf_External_Verdaux *) p);
6695 p += sizeof (Elf_External_Verdaux);
6696
6697 for (t = verdefs; t != NULL; t = t->next)
6698 {
6699 unsigned int cdeps;
6700 struct bfd_elf_version_deps *n;
6701
6702 /* Don't emit the base version twice. */
6703 if (t->vernum == 0)
6704 continue;
6705
6706 cdeps = 0;
6707 for (n = t->deps; n != NULL; n = n->next)
6708 ++cdeps;
6709
6710 /* Add a symbol representing this version. */
6711 bh = NULL;
6712 if (! (_bfd_generic_link_add_one_symbol
6713 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6714 0, NULL, FALSE,
6715 get_elf_backend_data (dynobj)->collect, &bh)))
6716 return FALSE;
6717 h = (struct elf_link_hash_entry *) bh;
6718 h->non_elf = 0;
6719 h->def_regular = 1;
6720 h->type = STT_OBJECT;
6721 h->verinfo.vertree = t;
6722
6723 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6724 return FALSE;
6725
6726 def.vd_version = VER_DEF_CURRENT;
6727 def.vd_flags = 0;
6728 if (t->globals.list == NULL
6729 && t->locals.list == NULL
6730 && ! t->used)
6731 def.vd_flags |= VER_FLG_WEAK;
6732 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6733 def.vd_cnt = cdeps + 1;
6734 def.vd_hash = bfd_elf_hash (t->name);
6735 def.vd_aux = sizeof (Elf_External_Verdef);
6736 def.vd_next = 0;
6737
6738 /* If a basever node is next, it *must* be the last node in
6739 the chain, otherwise Verdef construction breaks. */
6740 if (t->next != NULL && t->next->vernum == 0)
6741 BFD_ASSERT (t->next->next == NULL);
6742
6743 if (t->next != NULL && t->next->vernum != 0)
6744 def.vd_next = (sizeof (Elf_External_Verdef)
6745 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6746
6747 _bfd_elf_swap_verdef_out (output_bfd, &def,
6748 (Elf_External_Verdef *) p);
6749 p += sizeof (Elf_External_Verdef);
6750
6751 defaux.vda_name = h->dynstr_index;
6752 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6753 h->dynstr_index);
6754 defaux.vda_next = 0;
6755 if (t->deps != NULL)
6756 defaux.vda_next = sizeof (Elf_External_Verdaux);
6757 t->name_indx = defaux.vda_name;
6758
6759 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6760 (Elf_External_Verdaux *) p);
6761 p += sizeof (Elf_External_Verdaux);
6762
6763 for (n = t->deps; n != NULL; n = n->next)
6764 {
6765 if (n->version_needed == NULL)
6766 {
6767 /* This can happen if there was an error in the
6768 version script. */
6769 defaux.vda_name = 0;
6770 }
6771 else
6772 {
6773 defaux.vda_name = n->version_needed->name_indx;
6774 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6775 defaux.vda_name);
6776 }
6777 if (n->next == NULL)
6778 defaux.vda_next = 0;
6779 else
6780 defaux.vda_next = sizeof (Elf_External_Verdaux);
6781
6782 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6783 (Elf_External_Verdaux *) p);
6784 p += sizeof (Elf_External_Verdaux);
6785 }
6786 }
6787
6788 elf_tdata (output_bfd)->cverdefs = cdefs;
6789 }
6790 }
6791
6792 bed = get_elf_backend_data (output_bfd);
6793
6794 if (info->gc_sections && bed->can_gc_sections)
6795 {
6796 struct elf_gc_sweep_symbol_info sweep_info;
6797
6798 /* Remove the symbols that were in the swept sections from the
6799 dynamic symbol table. */
6800 sweep_info.info = info;
6801 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6802 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6803 &sweep_info);
6804 }
6805
6806 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6807 {
6808 asection *s;
6809 struct elf_find_verdep_info sinfo;
6810
6811 /* Work out the size of the version reference section. */
6812
6813 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6814 BFD_ASSERT (s != NULL);
6815
6816 sinfo.info = info;
6817 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6818 if (sinfo.vers == 0)
6819 sinfo.vers = 1;
6820 sinfo.failed = FALSE;
6821
6822 elf_link_hash_traverse (elf_hash_table (info),
6823 _bfd_elf_link_find_version_dependencies,
6824 &sinfo);
6825 if (sinfo.failed)
6826 return FALSE;
6827
6828 if (elf_tdata (output_bfd)->verref == NULL)
6829 s->flags |= SEC_EXCLUDE;
6830 else
6831 {
6832 Elf_Internal_Verneed *vn;
6833 unsigned int size;
6834 unsigned int crefs;
6835 bfd_byte *p;
6836
6837 /* Build the version dependency section. */
6838 size = 0;
6839 crefs = 0;
6840 for (vn = elf_tdata (output_bfd)->verref;
6841 vn != NULL;
6842 vn = vn->vn_nextref)
6843 {
6844 Elf_Internal_Vernaux *a;
6845
6846 size += sizeof (Elf_External_Verneed);
6847 ++crefs;
6848 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6849 size += sizeof (Elf_External_Vernaux);
6850 }
6851
6852 s->size = size;
6853 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6854 if (s->contents == NULL)
6855 return FALSE;
6856
6857 p = s->contents;
6858 for (vn = elf_tdata (output_bfd)->verref;
6859 vn != NULL;
6860 vn = vn->vn_nextref)
6861 {
6862 unsigned int caux;
6863 Elf_Internal_Vernaux *a;
6864 size_t indx;
6865
6866 caux = 0;
6867 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6868 ++caux;
6869
6870 vn->vn_version = VER_NEED_CURRENT;
6871 vn->vn_cnt = caux;
6872 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6873 elf_dt_name (vn->vn_bfd) != NULL
6874 ? elf_dt_name (vn->vn_bfd)
6875 : lbasename (bfd_get_filename
6876 (vn->vn_bfd)),
6877 FALSE);
6878 if (indx == (size_t) -1)
6879 return FALSE;
6880 vn->vn_file = indx;
6881 vn->vn_aux = sizeof (Elf_External_Verneed);
6882 if (vn->vn_nextref == NULL)
6883 vn->vn_next = 0;
6884 else
6885 vn->vn_next = (sizeof (Elf_External_Verneed)
6886 + caux * sizeof (Elf_External_Vernaux));
6887
6888 _bfd_elf_swap_verneed_out (output_bfd, vn,
6889 (Elf_External_Verneed *) p);
6890 p += sizeof (Elf_External_Verneed);
6891
6892 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6893 {
6894 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6895 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6896 a->vna_nodename, FALSE);
6897 if (indx == (size_t) -1)
6898 return FALSE;
6899 a->vna_name = indx;
6900 if (a->vna_nextptr == NULL)
6901 a->vna_next = 0;
6902 else
6903 a->vna_next = sizeof (Elf_External_Vernaux);
6904
6905 _bfd_elf_swap_vernaux_out (output_bfd, a,
6906 (Elf_External_Vernaux *) p);
6907 p += sizeof (Elf_External_Vernaux);
6908 }
6909 }
6910
6911 elf_tdata (output_bfd)->cverrefs = crefs;
6912 }
6913 }
6914
6915 /* Any syms created from now on start with -1 in
6916 got.refcount/offset and plt.refcount/offset. */
6917 elf_hash_table (info)->init_got_refcount
6918 = elf_hash_table (info)->init_got_offset;
6919 elf_hash_table (info)->init_plt_refcount
6920 = elf_hash_table (info)->init_plt_offset;
6921
6922 if (bfd_link_relocatable (info)
6923 && !_bfd_elf_size_group_sections (info))
6924 return FALSE;
6925
6926 /* The backend may have to create some sections regardless of whether
6927 we're dynamic or not. */
6928 if (bed->elf_backend_always_size_sections
6929 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6930 return FALSE;
6931
6932 /* Determine any GNU_STACK segment requirements, after the backend
6933 has had a chance to set a default segment size. */
6934 if (info->execstack)
6935 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6936 else if (info->noexecstack)
6937 elf_stack_flags (output_bfd) = PF_R | PF_W;
6938 else
6939 {
6940 bfd *inputobj;
6941 asection *notesec = NULL;
6942 int exec = 0;
6943
6944 for (inputobj = info->input_bfds;
6945 inputobj;
6946 inputobj = inputobj->link.next)
6947 {
6948 asection *s;
6949
6950 if (inputobj->flags
6951 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6952 continue;
6953 s = inputobj->sections;
6954 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6955 continue;
6956
6957 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6958 if (s)
6959 {
6960 if (s->flags & SEC_CODE)
6961 exec = PF_X;
6962 notesec = s;
6963 }
6964 else if (bed->default_execstack)
6965 exec = PF_X;
6966 }
6967 if (notesec || info->stacksize > 0)
6968 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6969 if (notesec && exec && bfd_link_relocatable (info)
6970 && notesec->output_section != bfd_abs_section_ptr)
6971 notesec->output_section->flags |= SEC_CODE;
6972 }
6973
6974 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6975 {
6976 struct elf_info_failed eif;
6977 struct elf_link_hash_entry *h;
6978 asection *dynstr;
6979 asection *s;
6980
6981 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6982 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6983
6984 if (info->symbolic)
6985 {
6986 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6987 return FALSE;
6988 info->flags |= DF_SYMBOLIC;
6989 }
6990
6991 if (rpath != NULL)
6992 {
6993 size_t indx;
6994 bfd_vma tag;
6995
6996 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6997 TRUE);
6998 if (indx == (size_t) -1)
6999 return FALSE;
7000
7001 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7002 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7003 return FALSE;
7004 }
7005
7006 if (filter_shlib != NULL)
7007 {
7008 size_t indx;
7009
7010 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7011 filter_shlib, TRUE);
7012 if (indx == (size_t) -1
7013 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7014 return FALSE;
7015 }
7016
7017 if (auxiliary_filters != NULL)
7018 {
7019 const char * const *p;
7020
7021 for (p = auxiliary_filters; *p != NULL; p++)
7022 {
7023 size_t indx;
7024
7025 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7026 *p, TRUE);
7027 if (indx == (size_t) -1
7028 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7029 return FALSE;
7030 }
7031 }
7032
7033 if (audit != NULL)
7034 {
7035 size_t indx;
7036
7037 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7038 TRUE);
7039 if (indx == (size_t) -1
7040 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7041 return FALSE;
7042 }
7043
7044 if (depaudit != NULL)
7045 {
7046 size_t indx;
7047
7048 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7049 TRUE);
7050 if (indx == (size_t) -1
7051 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7052 return FALSE;
7053 }
7054
7055 eif.info = info;
7056 eif.failed = FALSE;
7057
7058 /* Find all symbols which were defined in a dynamic object and make
7059 the backend pick a reasonable value for them. */
7060 elf_link_hash_traverse (elf_hash_table (info),
7061 _bfd_elf_adjust_dynamic_symbol,
7062 &eif);
7063 if (eif.failed)
7064 return FALSE;
7065
7066 /* Add some entries to the .dynamic section. We fill in some of the
7067 values later, in bfd_elf_final_link, but we must add the entries
7068 now so that we know the final size of the .dynamic section. */
7069
7070 /* If there are initialization and/or finalization functions to
7071 call then add the corresponding DT_INIT/DT_FINI entries. */
7072 h = (info->init_function
7073 ? elf_link_hash_lookup (elf_hash_table (info),
7074 info->init_function, FALSE,
7075 FALSE, FALSE)
7076 : NULL);
7077 if (h != NULL
7078 && (h->ref_regular
7079 || h->def_regular))
7080 {
7081 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7082 return FALSE;
7083 }
7084 h = (info->fini_function
7085 ? elf_link_hash_lookup (elf_hash_table (info),
7086 info->fini_function, FALSE,
7087 FALSE, FALSE)
7088 : NULL);
7089 if (h != NULL
7090 && (h->ref_regular
7091 || h->def_regular))
7092 {
7093 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7094 return FALSE;
7095 }
7096
7097 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7098 if (s != NULL && s->linker_has_input)
7099 {
7100 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7101 if (! bfd_link_executable (info))
7102 {
7103 bfd *sub;
7104 asection *o;
7105
7106 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7107 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7108 && (o = sub->sections) != NULL
7109 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7110 for (o = sub->sections; o != NULL; o = o->next)
7111 if (elf_section_data (o)->this_hdr.sh_type
7112 == SHT_PREINIT_ARRAY)
7113 {
7114 _bfd_error_handler
7115 (_("%pB: .preinit_array section is not allowed in DSO"),
7116 sub);
7117 break;
7118 }
7119
7120 bfd_set_error (bfd_error_nonrepresentable_section);
7121 return FALSE;
7122 }
7123
7124 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7125 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7126 return FALSE;
7127 }
7128 s = bfd_get_section_by_name (output_bfd, ".init_array");
7129 if (s != NULL && s->linker_has_input)
7130 {
7131 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7132 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7133 return FALSE;
7134 }
7135 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7136 if (s != NULL && s->linker_has_input)
7137 {
7138 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7139 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7140 return FALSE;
7141 }
7142
7143 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7144 /* If .dynstr is excluded from the link, we don't want any of
7145 these tags. Strictly, we should be checking each section
7146 individually; This quick check covers for the case where
7147 someone does a /DISCARD/ : { *(*) }. */
7148 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7149 {
7150 bfd_size_type strsize;
7151
7152 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7153 if ((info->emit_hash
7154 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7155 || (info->emit_gnu_hash
7156 && (bed->record_xhash_symbol == NULL
7157 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7158 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7159 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7160 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7161 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7162 bed->s->sizeof_sym))
7163 return FALSE;
7164 }
7165 }
7166
7167 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7168 return FALSE;
7169
7170 /* The backend must work out the sizes of all the other dynamic
7171 sections. */
7172 if (dynobj != NULL
7173 && bed->elf_backend_size_dynamic_sections != NULL
7174 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7175 return FALSE;
7176
7177 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7178 {
7179 if (elf_tdata (output_bfd)->cverdefs)
7180 {
7181 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7182
7183 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7184 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7185 return FALSE;
7186 }
7187
7188 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7189 {
7190 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7191 return FALSE;
7192 }
7193 else if (info->flags & DF_BIND_NOW)
7194 {
7195 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7196 return FALSE;
7197 }
7198
7199 if (info->flags_1)
7200 {
7201 if (bfd_link_executable (info))
7202 info->flags_1 &= ~ (DF_1_INITFIRST
7203 | DF_1_NODELETE
7204 | DF_1_NOOPEN);
7205 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7206 return FALSE;
7207 }
7208
7209 if (elf_tdata (output_bfd)->cverrefs)
7210 {
7211 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7212
7213 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7214 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7215 return FALSE;
7216 }
7217
7218 if ((elf_tdata (output_bfd)->cverrefs == 0
7219 && elf_tdata (output_bfd)->cverdefs == 0)
7220 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7221 {
7222 asection *s;
7223
7224 s = bfd_get_linker_section (dynobj, ".gnu.version");
7225 s->flags |= SEC_EXCLUDE;
7226 }
7227 }
7228 return TRUE;
7229 }
7230
7231 /* Find the first non-excluded output section. We'll use its
7232 section symbol for some emitted relocs. */
7233 void
7234 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7235 {
7236 asection *s;
7237 asection *found = NULL;
7238
7239 for (s = output_bfd->sections; s != NULL; s = s->next)
7240 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7241 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7242 {
7243 found = s;
7244 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7245 break;
7246 }
7247 elf_hash_table (info)->text_index_section = found;
7248 }
7249
7250 /* Find two non-excluded output sections, one for code, one for data.
7251 We'll use their section symbols for some emitted relocs. */
7252 void
7253 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7254 {
7255 asection *s;
7256 asection *found = NULL;
7257
7258 /* Data first, since setting text_index_section changes
7259 _bfd_elf_omit_section_dynsym_default. */
7260 for (s = output_bfd->sections; s != NULL; s = s->next)
7261 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7262 && !(s->flags & SEC_READONLY)
7263 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7264 {
7265 found = s;
7266 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7267 break;
7268 }
7269 elf_hash_table (info)->data_index_section = found;
7270
7271 for (s = output_bfd->sections; s != NULL; s = s->next)
7272 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7273 && (s->flags & SEC_READONLY)
7274 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7275 {
7276 found = s;
7277 break;
7278 }
7279 elf_hash_table (info)->text_index_section = found;
7280 }
7281
7282 #define GNU_HASH_SECTION_NAME(bed) \
7283 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7284
7285 bfd_boolean
7286 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7287 {
7288 const struct elf_backend_data *bed;
7289 unsigned long section_sym_count;
7290 bfd_size_type dynsymcount = 0;
7291
7292 if (!is_elf_hash_table (info->hash))
7293 return TRUE;
7294
7295 bed = get_elf_backend_data (output_bfd);
7296 (*bed->elf_backend_init_index_section) (output_bfd, info);
7297
7298 /* Assign dynsym indices. In a shared library we generate a section
7299 symbol for each output section, which come first. Next come all
7300 of the back-end allocated local dynamic syms, followed by the rest
7301 of the global symbols.
7302
7303 This is usually not needed for static binaries, however backends
7304 can request to always do it, e.g. the MIPS backend uses dynamic
7305 symbol counts to lay out GOT, which will be produced in the
7306 presence of GOT relocations even in static binaries (holding fixed
7307 data in that case, to satisfy those relocations). */
7308
7309 if (elf_hash_table (info)->dynamic_sections_created
7310 || bed->always_renumber_dynsyms)
7311 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7312 &section_sym_count);
7313
7314 if (elf_hash_table (info)->dynamic_sections_created)
7315 {
7316 bfd *dynobj;
7317 asection *s;
7318 unsigned int dtagcount;
7319
7320 dynobj = elf_hash_table (info)->dynobj;
7321
7322 /* Work out the size of the symbol version section. */
7323 s = bfd_get_linker_section (dynobj, ".gnu.version");
7324 BFD_ASSERT (s != NULL);
7325 if ((s->flags & SEC_EXCLUDE) == 0)
7326 {
7327 s->size = dynsymcount * sizeof (Elf_External_Versym);
7328 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7329 if (s->contents == NULL)
7330 return FALSE;
7331
7332 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7333 return FALSE;
7334 }
7335
7336 /* Set the size of the .dynsym and .hash sections. We counted
7337 the number of dynamic symbols in elf_link_add_object_symbols.
7338 We will build the contents of .dynsym and .hash when we build
7339 the final symbol table, because until then we do not know the
7340 correct value to give the symbols. We built the .dynstr
7341 section as we went along in elf_link_add_object_symbols. */
7342 s = elf_hash_table (info)->dynsym;
7343 BFD_ASSERT (s != NULL);
7344 s->size = dynsymcount * bed->s->sizeof_sym;
7345
7346 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7347 if (s->contents == NULL)
7348 return FALSE;
7349
7350 /* The first entry in .dynsym is a dummy symbol. Clear all the
7351 section syms, in case we don't output them all. */
7352 ++section_sym_count;
7353 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7354
7355 elf_hash_table (info)->bucketcount = 0;
7356
7357 /* Compute the size of the hashing table. As a side effect this
7358 computes the hash values for all the names we export. */
7359 if (info->emit_hash)
7360 {
7361 unsigned long int *hashcodes;
7362 struct hash_codes_info hashinf;
7363 bfd_size_type amt;
7364 unsigned long int nsyms;
7365 size_t bucketcount;
7366 size_t hash_entry_size;
7367
7368 /* Compute the hash values for all exported symbols. At the same
7369 time store the values in an array so that we could use them for
7370 optimizations. */
7371 amt = dynsymcount * sizeof (unsigned long int);
7372 hashcodes = (unsigned long int *) bfd_malloc (amt);
7373 if (hashcodes == NULL)
7374 return FALSE;
7375 hashinf.hashcodes = hashcodes;
7376 hashinf.error = FALSE;
7377
7378 /* Put all hash values in HASHCODES. */
7379 elf_link_hash_traverse (elf_hash_table (info),
7380 elf_collect_hash_codes, &hashinf);
7381 if (hashinf.error)
7382 {
7383 free (hashcodes);
7384 return FALSE;
7385 }
7386
7387 nsyms = hashinf.hashcodes - hashcodes;
7388 bucketcount
7389 = compute_bucket_count (info, hashcodes, nsyms, 0);
7390 free (hashcodes);
7391
7392 if (bucketcount == 0 && nsyms > 0)
7393 return FALSE;
7394
7395 elf_hash_table (info)->bucketcount = bucketcount;
7396
7397 s = bfd_get_linker_section (dynobj, ".hash");
7398 BFD_ASSERT (s != NULL);
7399 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7400 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7401 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7402 if (s->contents == NULL)
7403 return FALSE;
7404
7405 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7406 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7407 s->contents + hash_entry_size);
7408 }
7409
7410 if (info->emit_gnu_hash)
7411 {
7412 size_t i, cnt;
7413 unsigned char *contents;
7414 struct collect_gnu_hash_codes cinfo;
7415 bfd_size_type amt;
7416 size_t bucketcount;
7417
7418 memset (&cinfo, 0, sizeof (cinfo));
7419
7420 /* Compute the hash values for all exported symbols. At the same
7421 time store the values in an array so that we could use them for
7422 optimizations. */
7423 amt = dynsymcount * 2 * sizeof (unsigned long int);
7424 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7425 if (cinfo.hashcodes == NULL)
7426 return FALSE;
7427
7428 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7429 cinfo.min_dynindx = -1;
7430 cinfo.output_bfd = output_bfd;
7431 cinfo.bed = bed;
7432
7433 /* Put all hash values in HASHCODES. */
7434 elf_link_hash_traverse (elf_hash_table (info),
7435 elf_collect_gnu_hash_codes, &cinfo);
7436 if (cinfo.error)
7437 {
7438 free (cinfo.hashcodes);
7439 return FALSE;
7440 }
7441
7442 bucketcount
7443 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7444
7445 if (bucketcount == 0)
7446 {
7447 free (cinfo.hashcodes);
7448 return FALSE;
7449 }
7450
7451 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7452 BFD_ASSERT (s != NULL);
7453
7454 if (cinfo.nsyms == 0)
7455 {
7456 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7457 BFD_ASSERT (cinfo.min_dynindx == -1);
7458 free (cinfo.hashcodes);
7459 s->size = 5 * 4 + bed->s->arch_size / 8;
7460 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7461 if (contents == NULL)
7462 return FALSE;
7463 s->contents = contents;
7464 /* 1 empty bucket. */
7465 bfd_put_32 (output_bfd, 1, contents);
7466 /* SYMIDX above the special symbol 0. */
7467 bfd_put_32 (output_bfd, 1, contents + 4);
7468 /* Just one word for bitmask. */
7469 bfd_put_32 (output_bfd, 1, contents + 8);
7470 /* Only hash fn bloom filter. */
7471 bfd_put_32 (output_bfd, 0, contents + 12);
7472 /* No hashes are valid - empty bitmask. */
7473 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7474 /* No hashes in the only bucket. */
7475 bfd_put_32 (output_bfd, 0,
7476 contents + 16 + bed->s->arch_size / 8);
7477 }
7478 else
7479 {
7480 unsigned long int maskwords, maskbitslog2, x;
7481 BFD_ASSERT (cinfo.min_dynindx != -1);
7482
7483 x = cinfo.nsyms;
7484 maskbitslog2 = 1;
7485 while ((x >>= 1) != 0)
7486 ++maskbitslog2;
7487 if (maskbitslog2 < 3)
7488 maskbitslog2 = 5;
7489 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7490 maskbitslog2 = maskbitslog2 + 3;
7491 else
7492 maskbitslog2 = maskbitslog2 + 2;
7493 if (bed->s->arch_size == 64)
7494 {
7495 if (maskbitslog2 == 5)
7496 maskbitslog2 = 6;
7497 cinfo.shift1 = 6;
7498 }
7499 else
7500 cinfo.shift1 = 5;
7501 cinfo.mask = (1 << cinfo.shift1) - 1;
7502 cinfo.shift2 = maskbitslog2;
7503 cinfo.maskbits = 1 << maskbitslog2;
7504 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7505 amt = bucketcount * sizeof (unsigned long int) * 2;
7506 amt += maskwords * sizeof (bfd_vma);
7507 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7508 if (cinfo.bitmask == NULL)
7509 {
7510 free (cinfo.hashcodes);
7511 return FALSE;
7512 }
7513
7514 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7515 cinfo.indx = cinfo.counts + bucketcount;
7516 cinfo.symindx = dynsymcount - cinfo.nsyms;
7517 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7518
7519 /* Determine how often each hash bucket is used. */
7520 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7521 for (i = 0; i < cinfo.nsyms; ++i)
7522 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7523
7524 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7525 if (cinfo.counts[i] != 0)
7526 {
7527 cinfo.indx[i] = cnt;
7528 cnt += cinfo.counts[i];
7529 }
7530 BFD_ASSERT (cnt == dynsymcount);
7531 cinfo.bucketcount = bucketcount;
7532 cinfo.local_indx = cinfo.min_dynindx;
7533
7534 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7535 s->size += cinfo.maskbits / 8;
7536 if (bed->record_xhash_symbol != NULL)
7537 s->size += cinfo.nsyms * 4;
7538 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7539 if (contents == NULL)
7540 {
7541 free (cinfo.bitmask);
7542 free (cinfo.hashcodes);
7543 return FALSE;
7544 }
7545
7546 s->contents = contents;
7547 bfd_put_32 (output_bfd, bucketcount, contents);
7548 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7549 bfd_put_32 (output_bfd, maskwords, contents + 8);
7550 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7551 contents += 16 + cinfo.maskbits / 8;
7552
7553 for (i = 0; i < bucketcount; ++i)
7554 {
7555 if (cinfo.counts[i] == 0)
7556 bfd_put_32 (output_bfd, 0, contents);
7557 else
7558 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7559 contents += 4;
7560 }
7561
7562 cinfo.contents = contents;
7563
7564 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7565 /* Renumber dynamic symbols, if populating .gnu.hash section.
7566 If using .MIPS.xhash, populate the translation table. */
7567 elf_link_hash_traverse (elf_hash_table (info),
7568 elf_gnu_hash_process_symidx, &cinfo);
7569
7570 contents = s->contents + 16;
7571 for (i = 0; i < maskwords; ++i)
7572 {
7573 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7574 contents);
7575 contents += bed->s->arch_size / 8;
7576 }
7577
7578 free (cinfo.bitmask);
7579 free (cinfo.hashcodes);
7580 }
7581 }
7582
7583 s = bfd_get_linker_section (dynobj, ".dynstr");
7584 BFD_ASSERT (s != NULL);
7585
7586 elf_finalize_dynstr (output_bfd, info);
7587
7588 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7589
7590 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7591 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7592 return FALSE;
7593 }
7594
7595 return TRUE;
7596 }
7597 \f
7598 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7599
7600 static void
7601 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7602 asection *sec)
7603 {
7604 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7605 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7606 }
7607
7608 /* Finish SHF_MERGE section merging. */
7609
7610 bfd_boolean
7611 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7612 {
7613 bfd *ibfd;
7614 asection *sec;
7615
7616 if (!is_elf_hash_table (info->hash))
7617 return FALSE;
7618
7619 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7620 if ((ibfd->flags & DYNAMIC) == 0
7621 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7622 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7623 == get_elf_backend_data (obfd)->s->elfclass))
7624 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7625 if ((sec->flags & SEC_MERGE) != 0
7626 && !bfd_is_abs_section (sec->output_section))
7627 {
7628 struct bfd_elf_section_data *secdata;
7629
7630 secdata = elf_section_data (sec);
7631 if (! _bfd_add_merge_section (obfd,
7632 &elf_hash_table (info)->merge_info,
7633 sec, &secdata->sec_info))
7634 return FALSE;
7635 else if (secdata->sec_info)
7636 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7637 }
7638
7639 if (elf_hash_table (info)->merge_info != NULL)
7640 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7641 merge_sections_remove_hook);
7642 return TRUE;
7643 }
7644
7645 /* Create an entry in an ELF linker hash table. */
7646
7647 struct bfd_hash_entry *
7648 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7649 struct bfd_hash_table *table,
7650 const char *string)
7651 {
7652 /* Allocate the structure if it has not already been allocated by a
7653 subclass. */
7654 if (entry == NULL)
7655 {
7656 entry = (struct bfd_hash_entry *)
7657 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7658 if (entry == NULL)
7659 return entry;
7660 }
7661
7662 /* Call the allocation method of the superclass. */
7663 entry = _bfd_link_hash_newfunc (entry, table, string);
7664 if (entry != NULL)
7665 {
7666 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7667 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7668
7669 /* Set local fields. */
7670 ret->indx = -1;
7671 ret->dynindx = -1;
7672 ret->got = htab->init_got_refcount;
7673 ret->plt = htab->init_plt_refcount;
7674 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7675 - offsetof (struct elf_link_hash_entry, size)));
7676 /* Assume that we have been called by a non-ELF symbol reader.
7677 This flag is then reset by the code which reads an ELF input
7678 file. This ensures that a symbol created by a non-ELF symbol
7679 reader will have the flag set correctly. */
7680 ret->non_elf = 1;
7681 }
7682
7683 return entry;
7684 }
7685
7686 /* Copy data from an indirect symbol to its direct symbol, hiding the
7687 old indirect symbol. Also used for copying flags to a weakdef. */
7688
7689 void
7690 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7691 struct elf_link_hash_entry *dir,
7692 struct elf_link_hash_entry *ind)
7693 {
7694 struct elf_link_hash_table *htab;
7695
7696 if (ind->dyn_relocs != NULL)
7697 {
7698 if (dir->dyn_relocs != NULL)
7699 {
7700 struct elf_dyn_relocs **pp;
7701 struct elf_dyn_relocs *p;
7702
7703 /* Add reloc counts against the indirect sym to the direct sym
7704 list. Merge any entries against the same section. */
7705 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7706 {
7707 struct elf_dyn_relocs *q;
7708
7709 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7710 if (q->sec == p->sec)
7711 {
7712 q->pc_count += p->pc_count;
7713 q->count += p->count;
7714 *pp = p->next;
7715 break;
7716 }
7717 if (q == NULL)
7718 pp = &p->next;
7719 }
7720 *pp = dir->dyn_relocs;
7721 }
7722
7723 dir->dyn_relocs = ind->dyn_relocs;
7724 ind->dyn_relocs = NULL;
7725 }
7726
7727 /* Copy down any references that we may have already seen to the
7728 symbol which just became indirect. */
7729
7730 if (dir->versioned != versioned_hidden)
7731 dir->ref_dynamic |= ind->ref_dynamic;
7732 dir->ref_regular |= ind->ref_regular;
7733 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7734 dir->non_got_ref |= ind->non_got_ref;
7735 dir->needs_plt |= ind->needs_plt;
7736 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7737
7738 if (ind->root.type != bfd_link_hash_indirect)
7739 return;
7740
7741 /* Copy over the global and procedure linkage table refcount entries.
7742 These may have been already set up by a check_relocs routine. */
7743 htab = elf_hash_table (info);
7744 if (ind->got.refcount > htab->init_got_refcount.refcount)
7745 {
7746 if (dir->got.refcount < 0)
7747 dir->got.refcount = 0;
7748 dir->got.refcount += ind->got.refcount;
7749 ind->got.refcount = htab->init_got_refcount.refcount;
7750 }
7751
7752 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7753 {
7754 if (dir->plt.refcount < 0)
7755 dir->plt.refcount = 0;
7756 dir->plt.refcount += ind->plt.refcount;
7757 ind->plt.refcount = htab->init_plt_refcount.refcount;
7758 }
7759
7760 if (ind->dynindx != -1)
7761 {
7762 if (dir->dynindx != -1)
7763 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7764 dir->dynindx = ind->dynindx;
7765 dir->dynstr_index = ind->dynstr_index;
7766 ind->dynindx = -1;
7767 ind->dynstr_index = 0;
7768 }
7769 }
7770
7771 void
7772 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7773 struct elf_link_hash_entry *h,
7774 bfd_boolean force_local)
7775 {
7776 /* STT_GNU_IFUNC symbol must go through PLT. */
7777 if (h->type != STT_GNU_IFUNC)
7778 {
7779 h->plt = elf_hash_table (info)->init_plt_offset;
7780 h->needs_plt = 0;
7781 }
7782 if (force_local)
7783 {
7784 h->forced_local = 1;
7785 if (h->dynindx != -1)
7786 {
7787 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7788 h->dynstr_index);
7789 h->dynindx = -1;
7790 h->dynstr_index = 0;
7791 }
7792 }
7793 }
7794
7795 /* Hide a symbol. */
7796
7797 void
7798 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7799 struct bfd_link_info *info,
7800 struct bfd_link_hash_entry *h)
7801 {
7802 if (is_elf_hash_table (info->hash))
7803 {
7804 const struct elf_backend_data *bed
7805 = get_elf_backend_data (output_bfd);
7806 struct elf_link_hash_entry *eh
7807 = (struct elf_link_hash_entry *) h;
7808 bed->elf_backend_hide_symbol (info, eh, TRUE);
7809 eh->def_dynamic = 0;
7810 eh->ref_dynamic = 0;
7811 eh->dynamic_def = 0;
7812 }
7813 }
7814
7815 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7816 caller. */
7817
7818 bfd_boolean
7819 _bfd_elf_link_hash_table_init
7820 (struct elf_link_hash_table *table,
7821 bfd *abfd,
7822 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7823 struct bfd_hash_table *,
7824 const char *),
7825 unsigned int entsize,
7826 enum elf_target_id target_id)
7827 {
7828 bfd_boolean ret;
7829 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7830
7831 table->init_got_refcount.refcount = can_refcount - 1;
7832 table->init_plt_refcount.refcount = can_refcount - 1;
7833 table->init_got_offset.offset = -(bfd_vma) 1;
7834 table->init_plt_offset.offset = -(bfd_vma) 1;
7835 /* The first dynamic symbol is a dummy. */
7836 table->dynsymcount = 1;
7837
7838 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7839
7840 table->root.type = bfd_link_elf_hash_table;
7841 table->hash_table_id = target_id;
7842 table->target_os = get_elf_backend_data (abfd)->target_os;
7843
7844 return ret;
7845 }
7846
7847 /* Create an ELF linker hash table. */
7848
7849 struct bfd_link_hash_table *
7850 _bfd_elf_link_hash_table_create (bfd *abfd)
7851 {
7852 struct elf_link_hash_table *ret;
7853 size_t amt = sizeof (struct elf_link_hash_table);
7854
7855 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7856 if (ret == NULL)
7857 return NULL;
7858
7859 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7860 sizeof (struct elf_link_hash_entry),
7861 GENERIC_ELF_DATA))
7862 {
7863 free (ret);
7864 return NULL;
7865 }
7866 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7867
7868 return &ret->root;
7869 }
7870
7871 /* Destroy an ELF linker hash table. */
7872
7873 void
7874 _bfd_elf_link_hash_table_free (bfd *obfd)
7875 {
7876 struct elf_link_hash_table *htab;
7877
7878 htab = (struct elf_link_hash_table *) obfd->link.hash;
7879 if (htab->dynstr != NULL)
7880 _bfd_elf_strtab_free (htab->dynstr);
7881 _bfd_merge_sections_free (htab->merge_info);
7882 _bfd_generic_link_hash_table_free (obfd);
7883 }
7884
7885 /* This is a hook for the ELF emulation code in the generic linker to
7886 tell the backend linker what file name to use for the DT_NEEDED
7887 entry for a dynamic object. */
7888
7889 void
7890 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7891 {
7892 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7893 && bfd_get_format (abfd) == bfd_object)
7894 elf_dt_name (abfd) = name;
7895 }
7896
7897 int
7898 bfd_elf_get_dyn_lib_class (bfd *abfd)
7899 {
7900 int lib_class;
7901 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7902 && bfd_get_format (abfd) == bfd_object)
7903 lib_class = elf_dyn_lib_class (abfd);
7904 else
7905 lib_class = 0;
7906 return lib_class;
7907 }
7908
7909 void
7910 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7911 {
7912 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7913 && bfd_get_format (abfd) == bfd_object)
7914 elf_dyn_lib_class (abfd) = lib_class;
7915 }
7916
7917 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7918 the linker ELF emulation code. */
7919
7920 struct bfd_link_needed_list *
7921 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7922 struct bfd_link_info *info)
7923 {
7924 if (! is_elf_hash_table (info->hash))
7925 return NULL;
7926 return elf_hash_table (info)->needed;
7927 }
7928
7929 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7930 hook for the linker ELF emulation code. */
7931
7932 struct bfd_link_needed_list *
7933 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7934 struct bfd_link_info *info)
7935 {
7936 if (! is_elf_hash_table (info->hash))
7937 return NULL;
7938 return elf_hash_table (info)->runpath;
7939 }
7940
7941 /* Get the name actually used for a dynamic object for a link. This
7942 is the SONAME entry if there is one. Otherwise, it is the string
7943 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7944
7945 const char *
7946 bfd_elf_get_dt_soname (bfd *abfd)
7947 {
7948 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7949 && bfd_get_format (abfd) == bfd_object)
7950 return elf_dt_name (abfd);
7951 return NULL;
7952 }
7953
7954 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7955 the ELF linker emulation code. */
7956
7957 bfd_boolean
7958 bfd_elf_get_bfd_needed_list (bfd *abfd,
7959 struct bfd_link_needed_list **pneeded)
7960 {
7961 asection *s;
7962 bfd_byte *dynbuf = NULL;
7963 unsigned int elfsec;
7964 unsigned long shlink;
7965 bfd_byte *extdyn, *extdynend;
7966 size_t extdynsize;
7967 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7968
7969 *pneeded = NULL;
7970
7971 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7972 || bfd_get_format (abfd) != bfd_object)
7973 return TRUE;
7974
7975 s = bfd_get_section_by_name (abfd, ".dynamic");
7976 if (s == NULL || s->size == 0)
7977 return TRUE;
7978
7979 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7980 goto error_return;
7981
7982 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7983 if (elfsec == SHN_BAD)
7984 goto error_return;
7985
7986 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7987
7988 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7989 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7990
7991 extdyn = dynbuf;
7992 extdynend = extdyn + s->size;
7993 for (; extdyn < extdynend; extdyn += extdynsize)
7994 {
7995 Elf_Internal_Dyn dyn;
7996
7997 (*swap_dyn_in) (abfd, extdyn, &dyn);
7998
7999 if (dyn.d_tag == DT_NULL)
8000 break;
8001
8002 if (dyn.d_tag == DT_NEEDED)
8003 {
8004 const char *string;
8005 struct bfd_link_needed_list *l;
8006 unsigned int tagv = dyn.d_un.d_val;
8007 size_t amt;
8008
8009 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8010 if (string == NULL)
8011 goto error_return;
8012
8013 amt = sizeof *l;
8014 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8015 if (l == NULL)
8016 goto error_return;
8017
8018 l->by = abfd;
8019 l->name = string;
8020 l->next = *pneeded;
8021 *pneeded = l;
8022 }
8023 }
8024
8025 free (dynbuf);
8026
8027 return TRUE;
8028
8029 error_return:
8030 free (dynbuf);
8031 return FALSE;
8032 }
8033
8034 struct elf_symbuf_symbol
8035 {
8036 unsigned long st_name; /* Symbol name, index in string tbl */
8037 unsigned char st_info; /* Type and binding attributes */
8038 unsigned char st_other; /* Visibilty, and target specific */
8039 };
8040
8041 struct elf_symbuf_head
8042 {
8043 struct elf_symbuf_symbol *ssym;
8044 size_t count;
8045 unsigned int st_shndx;
8046 };
8047
8048 struct elf_symbol
8049 {
8050 union
8051 {
8052 Elf_Internal_Sym *isym;
8053 struct elf_symbuf_symbol *ssym;
8054 void *p;
8055 } u;
8056 const char *name;
8057 };
8058
8059 /* Sort references to symbols by ascending section number. */
8060
8061 static int
8062 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8063 {
8064 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8065 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8066
8067 if (s1->st_shndx != s2->st_shndx)
8068 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8069 /* Final sort by the address of the sym in the symbuf ensures
8070 a stable sort. */
8071 if (s1 != s2)
8072 return s1 > s2 ? 1 : -1;
8073 return 0;
8074 }
8075
8076 static int
8077 elf_sym_name_compare (const void *arg1, const void *arg2)
8078 {
8079 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8080 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8081 int ret = strcmp (s1->name, s2->name);
8082 if (ret != 0)
8083 return ret;
8084 if (s1->u.p != s2->u.p)
8085 return s1->u.p > s2->u.p ? 1 : -1;
8086 return 0;
8087 }
8088
8089 static struct elf_symbuf_head *
8090 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8091 {
8092 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8093 struct elf_symbuf_symbol *ssym;
8094 struct elf_symbuf_head *ssymbuf, *ssymhead;
8095 size_t i, shndx_count, total_size, amt;
8096
8097 amt = symcount * sizeof (*indbuf);
8098 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8099 if (indbuf == NULL)
8100 return NULL;
8101
8102 for (ind = indbuf, i = 0; i < symcount; i++)
8103 if (isymbuf[i].st_shndx != SHN_UNDEF)
8104 *ind++ = &isymbuf[i];
8105 indbufend = ind;
8106
8107 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8108 elf_sort_elf_symbol);
8109
8110 shndx_count = 0;
8111 if (indbufend > indbuf)
8112 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8113 if (ind[0]->st_shndx != ind[1]->st_shndx)
8114 shndx_count++;
8115
8116 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8117 + (indbufend - indbuf) * sizeof (*ssym));
8118 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8119 if (ssymbuf == NULL)
8120 {
8121 free (indbuf);
8122 return NULL;
8123 }
8124
8125 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8126 ssymbuf->ssym = NULL;
8127 ssymbuf->count = shndx_count;
8128 ssymbuf->st_shndx = 0;
8129 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8130 {
8131 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8132 {
8133 ssymhead++;
8134 ssymhead->ssym = ssym;
8135 ssymhead->count = 0;
8136 ssymhead->st_shndx = (*ind)->st_shndx;
8137 }
8138 ssym->st_name = (*ind)->st_name;
8139 ssym->st_info = (*ind)->st_info;
8140 ssym->st_other = (*ind)->st_other;
8141 ssymhead->count++;
8142 }
8143 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8144 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8145 == total_size));
8146
8147 free (indbuf);
8148 return ssymbuf;
8149 }
8150
8151 /* Check if 2 sections define the same set of local and global
8152 symbols. */
8153
8154 static bfd_boolean
8155 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8156 struct bfd_link_info *info)
8157 {
8158 bfd *bfd1, *bfd2;
8159 const struct elf_backend_data *bed1, *bed2;
8160 Elf_Internal_Shdr *hdr1, *hdr2;
8161 size_t symcount1, symcount2;
8162 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8163 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8164 Elf_Internal_Sym *isym, *isymend;
8165 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8166 size_t count1, count2, i;
8167 unsigned int shndx1, shndx2;
8168 bfd_boolean result;
8169
8170 bfd1 = sec1->owner;
8171 bfd2 = sec2->owner;
8172
8173 /* Both sections have to be in ELF. */
8174 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8175 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8176 return FALSE;
8177
8178 if (elf_section_type (sec1) != elf_section_type (sec2))
8179 return FALSE;
8180
8181 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8182 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8183 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8184 return FALSE;
8185
8186 bed1 = get_elf_backend_data (bfd1);
8187 bed2 = get_elf_backend_data (bfd2);
8188 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8189 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8190 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8191 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8192
8193 if (symcount1 == 0 || symcount2 == 0)
8194 return FALSE;
8195
8196 result = FALSE;
8197 isymbuf1 = NULL;
8198 isymbuf2 = NULL;
8199 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8200 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8201
8202 if (ssymbuf1 == NULL)
8203 {
8204 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8205 NULL, NULL, NULL);
8206 if (isymbuf1 == NULL)
8207 goto done;
8208
8209 if (info != NULL && !info->reduce_memory_overheads)
8210 {
8211 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8212 elf_tdata (bfd1)->symbuf = ssymbuf1;
8213 }
8214 }
8215
8216 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8217 {
8218 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8219 NULL, NULL, NULL);
8220 if (isymbuf2 == NULL)
8221 goto done;
8222
8223 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8224 {
8225 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8226 elf_tdata (bfd2)->symbuf = ssymbuf2;
8227 }
8228 }
8229
8230 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8231 {
8232 /* Optimized faster version. */
8233 size_t lo, hi, mid;
8234 struct elf_symbol *symp;
8235 struct elf_symbuf_symbol *ssym, *ssymend;
8236
8237 lo = 0;
8238 hi = ssymbuf1->count;
8239 ssymbuf1++;
8240 count1 = 0;
8241 while (lo < hi)
8242 {
8243 mid = (lo + hi) / 2;
8244 if (shndx1 < ssymbuf1[mid].st_shndx)
8245 hi = mid;
8246 else if (shndx1 > ssymbuf1[mid].st_shndx)
8247 lo = mid + 1;
8248 else
8249 {
8250 count1 = ssymbuf1[mid].count;
8251 ssymbuf1 += mid;
8252 break;
8253 }
8254 }
8255
8256 lo = 0;
8257 hi = ssymbuf2->count;
8258 ssymbuf2++;
8259 count2 = 0;
8260 while (lo < hi)
8261 {
8262 mid = (lo + hi) / 2;
8263 if (shndx2 < ssymbuf2[mid].st_shndx)
8264 hi = mid;
8265 else if (shndx2 > ssymbuf2[mid].st_shndx)
8266 lo = mid + 1;
8267 else
8268 {
8269 count2 = ssymbuf2[mid].count;
8270 ssymbuf2 += mid;
8271 break;
8272 }
8273 }
8274
8275 if (count1 == 0 || count2 == 0 || count1 != count2)
8276 goto done;
8277
8278 symtable1
8279 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8280 symtable2
8281 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8282 if (symtable1 == NULL || symtable2 == NULL)
8283 goto done;
8284
8285 symp = symtable1;
8286 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8287 ssym < ssymend; ssym++, symp++)
8288 {
8289 symp->u.ssym = ssym;
8290 symp->name = bfd_elf_string_from_elf_section (bfd1,
8291 hdr1->sh_link,
8292 ssym->st_name);
8293 }
8294
8295 symp = symtable2;
8296 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8297 ssym < ssymend; ssym++, symp++)
8298 {
8299 symp->u.ssym = ssym;
8300 symp->name = bfd_elf_string_from_elf_section (bfd2,
8301 hdr2->sh_link,
8302 ssym->st_name);
8303 }
8304
8305 /* Sort symbol by name. */
8306 qsort (symtable1, count1, sizeof (struct elf_symbol),
8307 elf_sym_name_compare);
8308 qsort (symtable2, count1, sizeof (struct elf_symbol),
8309 elf_sym_name_compare);
8310
8311 for (i = 0; i < count1; i++)
8312 /* Two symbols must have the same binding, type and name. */
8313 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8314 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8315 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8316 goto done;
8317
8318 result = TRUE;
8319 goto done;
8320 }
8321
8322 symtable1 = (struct elf_symbol *)
8323 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8324 symtable2 = (struct elf_symbol *)
8325 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8326 if (symtable1 == NULL || symtable2 == NULL)
8327 goto done;
8328
8329 /* Count definitions in the section. */
8330 count1 = 0;
8331 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8332 if (isym->st_shndx == shndx1)
8333 symtable1[count1++].u.isym = isym;
8334
8335 count2 = 0;
8336 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8337 if (isym->st_shndx == shndx2)
8338 symtable2[count2++].u.isym = isym;
8339
8340 if (count1 == 0 || count2 == 0 || count1 != count2)
8341 goto done;
8342
8343 for (i = 0; i < count1; i++)
8344 symtable1[i].name
8345 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8346 symtable1[i].u.isym->st_name);
8347
8348 for (i = 0; i < count2; i++)
8349 symtable2[i].name
8350 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8351 symtable2[i].u.isym->st_name);
8352
8353 /* Sort symbol by name. */
8354 qsort (symtable1, count1, sizeof (struct elf_symbol),
8355 elf_sym_name_compare);
8356 qsort (symtable2, count1, sizeof (struct elf_symbol),
8357 elf_sym_name_compare);
8358
8359 for (i = 0; i < count1; i++)
8360 /* Two symbols must have the same binding, type and name. */
8361 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8362 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8363 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8364 goto done;
8365
8366 result = TRUE;
8367
8368 done:
8369 free (symtable1);
8370 free (symtable2);
8371 free (isymbuf1);
8372 free (isymbuf2);
8373
8374 return result;
8375 }
8376
8377 /* Return TRUE if 2 section types are compatible. */
8378
8379 bfd_boolean
8380 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8381 bfd *bbfd, const asection *bsec)
8382 {
8383 if (asec == NULL
8384 || bsec == NULL
8385 || abfd->xvec->flavour != bfd_target_elf_flavour
8386 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8387 return TRUE;
8388
8389 return elf_section_type (asec) == elf_section_type (bsec);
8390 }
8391 \f
8392 /* Final phase of ELF linker. */
8393
8394 /* A structure we use to avoid passing large numbers of arguments. */
8395
8396 struct elf_final_link_info
8397 {
8398 /* General link information. */
8399 struct bfd_link_info *info;
8400 /* Output BFD. */
8401 bfd *output_bfd;
8402 /* Symbol string table. */
8403 struct elf_strtab_hash *symstrtab;
8404 /* .hash section. */
8405 asection *hash_sec;
8406 /* symbol version section (.gnu.version). */
8407 asection *symver_sec;
8408 /* Buffer large enough to hold contents of any section. */
8409 bfd_byte *contents;
8410 /* Buffer large enough to hold external relocs of any section. */
8411 void *external_relocs;
8412 /* Buffer large enough to hold internal relocs of any section. */
8413 Elf_Internal_Rela *internal_relocs;
8414 /* Buffer large enough to hold external local symbols of any input
8415 BFD. */
8416 bfd_byte *external_syms;
8417 /* And a buffer for symbol section indices. */
8418 Elf_External_Sym_Shndx *locsym_shndx;
8419 /* Buffer large enough to hold internal local symbols of any input
8420 BFD. */
8421 Elf_Internal_Sym *internal_syms;
8422 /* Array large enough to hold a symbol index for each local symbol
8423 of any input BFD. */
8424 long *indices;
8425 /* Array large enough to hold a section pointer for each local
8426 symbol of any input BFD. */
8427 asection **sections;
8428 /* Buffer for SHT_SYMTAB_SHNDX section. */
8429 Elf_External_Sym_Shndx *symshndxbuf;
8430 /* Number of STT_FILE syms seen. */
8431 size_t filesym_count;
8432 /* Local symbol hash table. */
8433 struct bfd_hash_table local_hash_table;
8434 };
8435
8436 struct local_hash_entry
8437 {
8438 /* Base hash table entry structure. */
8439 struct bfd_hash_entry root;
8440 /* Size of the local symbol name. */
8441 size_t size;
8442 /* Number of the duplicated local symbol names. */
8443 long count;
8444 };
8445
8446 /* Create an entry in the local symbol hash table. */
8447
8448 static struct bfd_hash_entry *
8449 local_hash_newfunc (struct bfd_hash_entry *entry,
8450 struct bfd_hash_table *table,
8451 const char *string)
8452 {
8453
8454 /* Allocate the structure if it has not already been allocated by a
8455 subclass. */
8456 if (entry == NULL)
8457 {
8458 entry = bfd_hash_allocate (table,
8459 sizeof (struct local_hash_entry));
8460 if (entry == NULL)
8461 return entry;
8462 }
8463
8464 /* Call the allocation method of the superclass. */
8465 entry = bfd_hash_newfunc (entry, table, string);
8466 if (entry != NULL)
8467 {
8468 ((struct local_hash_entry *) entry)->count = 0;
8469 ((struct local_hash_entry *) entry)->size = 0;
8470 }
8471
8472 return entry;
8473 }
8474
8475 /* This struct is used to pass information to elf_link_output_extsym. */
8476
8477 struct elf_outext_info
8478 {
8479 bfd_boolean failed;
8480 bfd_boolean localsyms;
8481 bfd_boolean file_sym_done;
8482 struct elf_final_link_info *flinfo;
8483 };
8484
8485
8486 /* Support for evaluating a complex relocation.
8487
8488 Complex relocations are generalized, self-describing relocations. The
8489 implementation of them consists of two parts: complex symbols, and the
8490 relocations themselves.
8491
8492 The relocations use a reserved elf-wide relocation type code (R_RELC
8493 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8494 information (start bit, end bit, word width, etc) into the addend. This
8495 information is extracted from CGEN-generated operand tables within gas.
8496
8497 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8498 internal) representing prefix-notation expressions, including but not
8499 limited to those sorts of expressions normally encoded as addends in the
8500 addend field. The symbol mangling format is:
8501
8502 <node> := <literal>
8503 | <unary-operator> ':' <node>
8504 | <binary-operator> ':' <node> ':' <node>
8505 ;
8506
8507 <literal> := 's' <digits=N> ':' <N character symbol name>
8508 | 'S' <digits=N> ':' <N character section name>
8509 | '#' <hexdigits>
8510 ;
8511
8512 <binary-operator> := as in C
8513 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8514
8515 static void
8516 set_symbol_value (bfd *bfd_with_globals,
8517 Elf_Internal_Sym *isymbuf,
8518 size_t locsymcount,
8519 size_t symidx,
8520 bfd_vma val)
8521 {
8522 struct elf_link_hash_entry **sym_hashes;
8523 struct elf_link_hash_entry *h;
8524 size_t extsymoff = locsymcount;
8525
8526 if (symidx < locsymcount)
8527 {
8528 Elf_Internal_Sym *sym;
8529
8530 sym = isymbuf + symidx;
8531 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8532 {
8533 /* It is a local symbol: move it to the
8534 "absolute" section and give it a value. */
8535 sym->st_shndx = SHN_ABS;
8536 sym->st_value = val;
8537 return;
8538 }
8539 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8540 extsymoff = 0;
8541 }
8542
8543 /* It is a global symbol: set its link type
8544 to "defined" and give it a value. */
8545
8546 sym_hashes = elf_sym_hashes (bfd_with_globals);
8547 h = sym_hashes [symidx - extsymoff];
8548 while (h->root.type == bfd_link_hash_indirect
8549 || h->root.type == bfd_link_hash_warning)
8550 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8551 h->root.type = bfd_link_hash_defined;
8552 h->root.u.def.value = val;
8553 h->root.u.def.section = bfd_abs_section_ptr;
8554 }
8555
8556 static bfd_boolean
8557 resolve_symbol (const char *name,
8558 bfd *input_bfd,
8559 struct elf_final_link_info *flinfo,
8560 bfd_vma *result,
8561 Elf_Internal_Sym *isymbuf,
8562 size_t locsymcount)
8563 {
8564 Elf_Internal_Sym *sym;
8565 struct bfd_link_hash_entry *global_entry;
8566 const char *candidate = NULL;
8567 Elf_Internal_Shdr *symtab_hdr;
8568 size_t i;
8569
8570 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8571
8572 for (i = 0; i < locsymcount; ++ i)
8573 {
8574 sym = isymbuf + i;
8575
8576 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8577 continue;
8578
8579 candidate = bfd_elf_string_from_elf_section (input_bfd,
8580 symtab_hdr->sh_link,
8581 sym->st_name);
8582 #ifdef DEBUG
8583 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8584 name, candidate, (unsigned long) sym->st_value);
8585 #endif
8586 if (candidate && strcmp (candidate, name) == 0)
8587 {
8588 asection *sec = flinfo->sections [i];
8589
8590 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8591 *result += sec->output_offset + sec->output_section->vma;
8592 #ifdef DEBUG
8593 printf ("Found symbol with value %8.8lx\n",
8594 (unsigned long) *result);
8595 #endif
8596 return TRUE;
8597 }
8598 }
8599
8600 /* Hmm, haven't found it yet. perhaps it is a global. */
8601 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8602 FALSE, FALSE, TRUE);
8603 if (!global_entry)
8604 return FALSE;
8605
8606 if (global_entry->type == bfd_link_hash_defined
8607 || global_entry->type == bfd_link_hash_defweak)
8608 {
8609 *result = (global_entry->u.def.value
8610 + global_entry->u.def.section->output_section->vma
8611 + global_entry->u.def.section->output_offset);
8612 #ifdef DEBUG
8613 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8614 global_entry->root.string, (unsigned long) *result);
8615 #endif
8616 return TRUE;
8617 }
8618
8619 return FALSE;
8620 }
8621
8622 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8623 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8624 names like "foo.end" which is the end address of section "foo". */
8625
8626 static bfd_boolean
8627 resolve_section (const char *name,
8628 asection *sections,
8629 bfd_vma *result,
8630 bfd * abfd)
8631 {
8632 asection *curr;
8633 unsigned int len;
8634
8635 for (curr = sections; curr; curr = curr->next)
8636 if (strcmp (curr->name, name) == 0)
8637 {
8638 *result = curr->vma;
8639 return TRUE;
8640 }
8641
8642 /* Hmm. still haven't found it. try pseudo-section names. */
8643 /* FIXME: This could be coded more efficiently... */
8644 for (curr = sections; curr; curr = curr->next)
8645 {
8646 len = strlen (curr->name);
8647 if (len > strlen (name))
8648 continue;
8649
8650 if (strncmp (curr->name, name, len) == 0)
8651 {
8652 if (strncmp (".end", name + len, 4) == 0)
8653 {
8654 *result = (curr->vma
8655 + curr->size / bfd_octets_per_byte (abfd, curr));
8656 return TRUE;
8657 }
8658
8659 /* Insert more pseudo-section names here, if you like. */
8660 }
8661 }
8662
8663 return FALSE;
8664 }
8665
8666 static void
8667 undefined_reference (const char *reftype, const char *name)
8668 {
8669 /* xgettext:c-format */
8670 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8671 reftype, name);
8672 bfd_set_error (bfd_error_bad_value);
8673 }
8674
8675 static bfd_boolean
8676 eval_symbol (bfd_vma *result,
8677 const char **symp,
8678 bfd *input_bfd,
8679 struct elf_final_link_info *flinfo,
8680 bfd_vma dot,
8681 Elf_Internal_Sym *isymbuf,
8682 size_t locsymcount,
8683 int signed_p)
8684 {
8685 size_t len;
8686 size_t symlen;
8687 bfd_vma a;
8688 bfd_vma b;
8689 char symbuf[4096];
8690 const char *sym = *symp;
8691 const char *symend;
8692 bfd_boolean symbol_is_section = FALSE;
8693
8694 len = strlen (sym);
8695 symend = sym + len;
8696
8697 if (len < 1 || len > sizeof (symbuf))
8698 {
8699 bfd_set_error (bfd_error_invalid_operation);
8700 return FALSE;
8701 }
8702
8703 switch (* sym)
8704 {
8705 case '.':
8706 *result = dot;
8707 *symp = sym + 1;
8708 return TRUE;
8709
8710 case '#':
8711 ++sym;
8712 *result = strtoul (sym, (char **) symp, 16);
8713 return TRUE;
8714
8715 case 'S':
8716 symbol_is_section = TRUE;
8717 /* Fall through. */
8718 case 's':
8719 ++sym;
8720 symlen = strtol (sym, (char **) symp, 10);
8721 sym = *symp + 1; /* Skip the trailing ':'. */
8722
8723 if (symend < sym || symlen + 1 > sizeof (symbuf))
8724 {
8725 bfd_set_error (bfd_error_invalid_operation);
8726 return FALSE;
8727 }
8728
8729 memcpy (symbuf, sym, symlen);
8730 symbuf[symlen] = '\0';
8731 *symp = sym + symlen;
8732
8733 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8734 the symbol as a section, or vice-versa. so we're pretty liberal in our
8735 interpretation here; section means "try section first", not "must be a
8736 section", and likewise with symbol. */
8737
8738 if (symbol_is_section)
8739 {
8740 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8741 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8742 isymbuf, locsymcount))
8743 {
8744 undefined_reference ("section", symbuf);
8745 return FALSE;
8746 }
8747 }
8748 else
8749 {
8750 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8751 isymbuf, locsymcount)
8752 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8753 result, input_bfd))
8754 {
8755 undefined_reference ("symbol", symbuf);
8756 return FALSE;
8757 }
8758 }
8759
8760 return TRUE;
8761
8762 /* All that remains are operators. */
8763
8764 #define UNARY_OP(op) \
8765 if (strncmp (sym, #op, strlen (#op)) == 0) \
8766 { \
8767 sym += strlen (#op); \
8768 if (*sym == ':') \
8769 ++sym; \
8770 *symp = sym; \
8771 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8772 isymbuf, locsymcount, signed_p)) \
8773 return FALSE; \
8774 if (signed_p) \
8775 *result = op ((bfd_signed_vma) a); \
8776 else \
8777 *result = op a; \
8778 return TRUE; \
8779 }
8780
8781 #define BINARY_OP_HEAD(op) \
8782 if (strncmp (sym, #op, strlen (#op)) == 0) \
8783 { \
8784 sym += strlen (#op); \
8785 if (*sym == ':') \
8786 ++sym; \
8787 *symp = sym; \
8788 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8789 isymbuf, locsymcount, signed_p)) \
8790 return FALSE; \
8791 ++*symp; \
8792 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8793 isymbuf, locsymcount, signed_p)) \
8794 return FALSE;
8795 #define BINARY_OP_TAIL(op) \
8796 if (signed_p) \
8797 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8798 else \
8799 *result = a op b; \
8800 return TRUE; \
8801 }
8802 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8803
8804 default:
8805 UNARY_OP (0-);
8806 BINARY_OP_HEAD (<<);
8807 if (b >= sizeof (a) * CHAR_BIT)
8808 {
8809 *result = 0;
8810 return TRUE;
8811 }
8812 signed_p = 0;
8813 BINARY_OP_TAIL (<<);
8814 BINARY_OP_HEAD (>>);
8815 if (b >= sizeof (a) * CHAR_BIT)
8816 {
8817 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8818 return TRUE;
8819 }
8820 BINARY_OP_TAIL (>>);
8821 BINARY_OP (==);
8822 BINARY_OP (!=);
8823 BINARY_OP (<=);
8824 BINARY_OP (>=);
8825 BINARY_OP (&&);
8826 BINARY_OP (||);
8827 UNARY_OP (~);
8828 UNARY_OP (!);
8829 BINARY_OP (*);
8830 BINARY_OP_HEAD (/);
8831 if (b == 0)
8832 {
8833 _bfd_error_handler (_("division by zero"));
8834 bfd_set_error (bfd_error_bad_value);
8835 return FALSE;
8836 }
8837 BINARY_OP_TAIL (/);
8838 BINARY_OP_HEAD (%);
8839 if (b == 0)
8840 {
8841 _bfd_error_handler (_("division by zero"));
8842 bfd_set_error (bfd_error_bad_value);
8843 return FALSE;
8844 }
8845 BINARY_OP_TAIL (%);
8846 BINARY_OP (^);
8847 BINARY_OP (|);
8848 BINARY_OP (&);
8849 BINARY_OP (+);
8850 BINARY_OP (-);
8851 BINARY_OP (<);
8852 BINARY_OP (>);
8853 #undef UNARY_OP
8854 #undef BINARY_OP
8855 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8856 bfd_set_error (bfd_error_invalid_operation);
8857 return FALSE;
8858 }
8859 }
8860
8861 static void
8862 put_value (bfd_vma size,
8863 unsigned long chunksz,
8864 bfd *input_bfd,
8865 bfd_vma x,
8866 bfd_byte *location)
8867 {
8868 location += (size - chunksz);
8869
8870 for (; size; size -= chunksz, location -= chunksz)
8871 {
8872 switch (chunksz)
8873 {
8874 case 1:
8875 bfd_put_8 (input_bfd, x, location);
8876 x >>= 8;
8877 break;
8878 case 2:
8879 bfd_put_16 (input_bfd, x, location);
8880 x >>= 16;
8881 break;
8882 case 4:
8883 bfd_put_32 (input_bfd, x, location);
8884 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8885 x >>= 16;
8886 x >>= 16;
8887 break;
8888 #ifdef BFD64
8889 case 8:
8890 bfd_put_64 (input_bfd, x, location);
8891 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8892 x >>= 32;
8893 x >>= 32;
8894 break;
8895 #endif
8896 default:
8897 abort ();
8898 break;
8899 }
8900 }
8901 }
8902
8903 static bfd_vma
8904 get_value (bfd_vma size,
8905 unsigned long chunksz,
8906 bfd *input_bfd,
8907 bfd_byte *location)
8908 {
8909 int shift;
8910 bfd_vma x = 0;
8911
8912 /* Sanity checks. */
8913 BFD_ASSERT (chunksz <= sizeof (x)
8914 && size >= chunksz
8915 && chunksz != 0
8916 && (size % chunksz) == 0
8917 && input_bfd != NULL
8918 && location != NULL);
8919
8920 if (chunksz == sizeof (x))
8921 {
8922 BFD_ASSERT (size == chunksz);
8923
8924 /* Make sure that we do not perform an undefined shift operation.
8925 We know that size == chunksz so there will only be one iteration
8926 of the loop below. */
8927 shift = 0;
8928 }
8929 else
8930 shift = 8 * chunksz;
8931
8932 for (; size; size -= chunksz, location += chunksz)
8933 {
8934 switch (chunksz)
8935 {
8936 case 1:
8937 x = (x << shift) | bfd_get_8 (input_bfd, location);
8938 break;
8939 case 2:
8940 x = (x << shift) | bfd_get_16 (input_bfd, location);
8941 break;
8942 case 4:
8943 x = (x << shift) | bfd_get_32 (input_bfd, location);
8944 break;
8945 #ifdef BFD64
8946 case 8:
8947 x = (x << shift) | bfd_get_64 (input_bfd, location);
8948 break;
8949 #endif
8950 default:
8951 abort ();
8952 }
8953 }
8954 return x;
8955 }
8956
8957 static void
8958 decode_complex_addend (unsigned long *start, /* in bits */
8959 unsigned long *oplen, /* in bits */
8960 unsigned long *len, /* in bits */
8961 unsigned long *wordsz, /* in bytes */
8962 unsigned long *chunksz, /* in bytes */
8963 unsigned long *lsb0_p,
8964 unsigned long *signed_p,
8965 unsigned long *trunc_p,
8966 unsigned long encoded)
8967 {
8968 * start = encoded & 0x3F;
8969 * len = (encoded >> 6) & 0x3F;
8970 * oplen = (encoded >> 12) & 0x3F;
8971 * wordsz = (encoded >> 18) & 0xF;
8972 * chunksz = (encoded >> 22) & 0xF;
8973 * lsb0_p = (encoded >> 27) & 1;
8974 * signed_p = (encoded >> 28) & 1;
8975 * trunc_p = (encoded >> 29) & 1;
8976 }
8977
8978 bfd_reloc_status_type
8979 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8980 asection *input_section,
8981 bfd_byte *contents,
8982 Elf_Internal_Rela *rel,
8983 bfd_vma relocation)
8984 {
8985 bfd_vma shift, x, mask;
8986 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8987 bfd_reloc_status_type r;
8988 bfd_size_type octets;
8989
8990 /* Perform this reloc, since it is complex.
8991 (this is not to say that it necessarily refers to a complex
8992 symbol; merely that it is a self-describing CGEN based reloc.
8993 i.e. the addend has the complete reloc information (bit start, end,
8994 word size, etc) encoded within it.). */
8995
8996 decode_complex_addend (&start, &oplen, &len, &wordsz,
8997 &chunksz, &lsb0_p, &signed_p,
8998 &trunc_p, rel->r_addend);
8999
9000 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9001
9002 if (lsb0_p)
9003 shift = (start + 1) - len;
9004 else
9005 shift = (8 * wordsz) - (start + len);
9006
9007 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9008 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9009
9010 #ifdef DEBUG
9011 printf ("Doing complex reloc: "
9012 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9013 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9014 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9015 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9016 oplen, (unsigned long) x, (unsigned long) mask,
9017 (unsigned long) relocation);
9018 #endif
9019
9020 r = bfd_reloc_ok;
9021 if (! trunc_p)
9022 /* Now do an overflow check. */
9023 r = bfd_check_overflow ((signed_p
9024 ? complain_overflow_signed
9025 : complain_overflow_unsigned),
9026 len, 0, (8 * wordsz),
9027 relocation);
9028
9029 /* Do the deed. */
9030 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9031
9032 #ifdef DEBUG
9033 printf (" relocation: %8.8lx\n"
9034 " shifted mask: %8.8lx\n"
9035 " shifted/masked reloc: %8.8lx\n"
9036 " result: %8.8lx\n",
9037 (unsigned long) relocation, (unsigned long) (mask << shift),
9038 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9039 #endif
9040 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9041 return r;
9042 }
9043
9044 /* Functions to read r_offset from external (target order) reloc
9045 entry. Faster than bfd_getl32 et al, because we let the compiler
9046 know the value is aligned. */
9047
9048 static bfd_vma
9049 ext32l_r_offset (const void *p)
9050 {
9051 union aligned32
9052 {
9053 uint32_t v;
9054 unsigned char c[4];
9055 };
9056 const union aligned32 *a
9057 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9058
9059 uint32_t aval = ( (uint32_t) a->c[0]
9060 | (uint32_t) a->c[1] << 8
9061 | (uint32_t) a->c[2] << 16
9062 | (uint32_t) a->c[3] << 24);
9063 return aval;
9064 }
9065
9066 static bfd_vma
9067 ext32b_r_offset (const void *p)
9068 {
9069 union aligned32
9070 {
9071 uint32_t v;
9072 unsigned char c[4];
9073 };
9074 const union aligned32 *a
9075 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9076
9077 uint32_t aval = ( (uint32_t) a->c[0] << 24
9078 | (uint32_t) a->c[1] << 16
9079 | (uint32_t) a->c[2] << 8
9080 | (uint32_t) a->c[3]);
9081 return aval;
9082 }
9083
9084 #ifdef BFD_HOST_64_BIT
9085 static bfd_vma
9086 ext64l_r_offset (const void *p)
9087 {
9088 union aligned64
9089 {
9090 uint64_t v;
9091 unsigned char c[8];
9092 };
9093 const union aligned64 *a
9094 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9095
9096 uint64_t aval = ( (uint64_t) a->c[0]
9097 | (uint64_t) a->c[1] << 8
9098 | (uint64_t) a->c[2] << 16
9099 | (uint64_t) a->c[3] << 24
9100 | (uint64_t) a->c[4] << 32
9101 | (uint64_t) a->c[5] << 40
9102 | (uint64_t) a->c[6] << 48
9103 | (uint64_t) a->c[7] << 56);
9104 return aval;
9105 }
9106
9107 static bfd_vma
9108 ext64b_r_offset (const void *p)
9109 {
9110 union aligned64
9111 {
9112 uint64_t v;
9113 unsigned char c[8];
9114 };
9115 const union aligned64 *a
9116 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9117
9118 uint64_t aval = ( (uint64_t) a->c[0] << 56
9119 | (uint64_t) a->c[1] << 48
9120 | (uint64_t) a->c[2] << 40
9121 | (uint64_t) a->c[3] << 32
9122 | (uint64_t) a->c[4] << 24
9123 | (uint64_t) a->c[5] << 16
9124 | (uint64_t) a->c[6] << 8
9125 | (uint64_t) a->c[7]);
9126 return aval;
9127 }
9128 #endif
9129
9130 /* When performing a relocatable link, the input relocations are
9131 preserved. But, if they reference global symbols, the indices
9132 referenced must be updated. Update all the relocations found in
9133 RELDATA. */
9134
9135 static bfd_boolean
9136 elf_link_adjust_relocs (bfd *abfd,
9137 asection *sec,
9138 struct bfd_elf_section_reloc_data *reldata,
9139 bfd_boolean sort,
9140 struct bfd_link_info *info)
9141 {
9142 unsigned int i;
9143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9144 bfd_byte *erela;
9145 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9146 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9147 bfd_vma r_type_mask;
9148 int r_sym_shift;
9149 unsigned int count = reldata->count;
9150 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9151
9152 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9153 {
9154 swap_in = bed->s->swap_reloc_in;
9155 swap_out = bed->s->swap_reloc_out;
9156 }
9157 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9158 {
9159 swap_in = bed->s->swap_reloca_in;
9160 swap_out = bed->s->swap_reloca_out;
9161 }
9162 else
9163 abort ();
9164
9165 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9166 abort ();
9167
9168 if (bed->s->arch_size == 32)
9169 {
9170 r_type_mask = 0xff;
9171 r_sym_shift = 8;
9172 }
9173 else
9174 {
9175 r_type_mask = 0xffffffff;
9176 r_sym_shift = 32;
9177 }
9178
9179 erela = reldata->hdr->contents;
9180 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9181 {
9182 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9183 unsigned int j;
9184
9185 if (*rel_hash == NULL)
9186 continue;
9187
9188 if ((*rel_hash)->indx == -2
9189 && info->gc_sections
9190 && ! info->gc_keep_exported)
9191 {
9192 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9193 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9194 abfd, sec,
9195 (*rel_hash)->root.root.string);
9196 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9197 abfd, sec);
9198 bfd_set_error (bfd_error_invalid_operation);
9199 return FALSE;
9200 }
9201 BFD_ASSERT ((*rel_hash)->indx >= 0);
9202
9203 (*swap_in) (abfd, erela, irela);
9204 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9205 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9206 | (irela[j].r_info & r_type_mask));
9207 (*swap_out) (abfd, irela, erela);
9208 }
9209
9210 if (bed->elf_backend_update_relocs)
9211 (*bed->elf_backend_update_relocs) (sec, reldata);
9212
9213 if (sort && count != 0)
9214 {
9215 bfd_vma (*ext_r_off) (const void *);
9216 bfd_vma r_off;
9217 size_t elt_size;
9218 bfd_byte *base, *end, *p, *loc;
9219 bfd_byte *buf = NULL;
9220
9221 if (bed->s->arch_size == 32)
9222 {
9223 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9224 ext_r_off = ext32l_r_offset;
9225 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9226 ext_r_off = ext32b_r_offset;
9227 else
9228 abort ();
9229 }
9230 else
9231 {
9232 #ifdef BFD_HOST_64_BIT
9233 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9234 ext_r_off = ext64l_r_offset;
9235 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9236 ext_r_off = ext64b_r_offset;
9237 else
9238 #endif
9239 abort ();
9240 }
9241
9242 /* Must use a stable sort here. A modified insertion sort,
9243 since the relocs are mostly sorted already. */
9244 elt_size = reldata->hdr->sh_entsize;
9245 base = reldata->hdr->contents;
9246 end = base + count * elt_size;
9247 if (elt_size > sizeof (Elf64_External_Rela))
9248 abort ();
9249
9250 /* Ensure the first element is lowest. This acts as a sentinel,
9251 speeding the main loop below. */
9252 r_off = (*ext_r_off) (base);
9253 for (p = loc = base; (p += elt_size) < end; )
9254 {
9255 bfd_vma r_off2 = (*ext_r_off) (p);
9256 if (r_off > r_off2)
9257 {
9258 r_off = r_off2;
9259 loc = p;
9260 }
9261 }
9262 if (loc != base)
9263 {
9264 /* Don't just swap *base and *loc as that changes the order
9265 of the original base[0] and base[1] if they happen to
9266 have the same r_offset. */
9267 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9268 memcpy (onebuf, loc, elt_size);
9269 memmove (base + elt_size, base, loc - base);
9270 memcpy (base, onebuf, elt_size);
9271 }
9272
9273 for (p = base + elt_size; (p += elt_size) < end; )
9274 {
9275 /* base to p is sorted, *p is next to insert. */
9276 r_off = (*ext_r_off) (p);
9277 /* Search the sorted region for location to insert. */
9278 loc = p - elt_size;
9279 while (r_off < (*ext_r_off) (loc))
9280 loc -= elt_size;
9281 loc += elt_size;
9282 if (loc != p)
9283 {
9284 /* Chances are there is a run of relocs to insert here,
9285 from one of more input files. Files are not always
9286 linked in order due to the way elf_link_input_bfd is
9287 called. See pr17666. */
9288 size_t sortlen = p - loc;
9289 bfd_vma r_off2 = (*ext_r_off) (loc);
9290 size_t runlen = elt_size;
9291 size_t buf_size = 96 * 1024;
9292 while (p + runlen < end
9293 && (sortlen <= buf_size
9294 || runlen + elt_size <= buf_size)
9295 && r_off2 > (*ext_r_off) (p + runlen))
9296 runlen += elt_size;
9297 if (buf == NULL)
9298 {
9299 buf = bfd_malloc (buf_size);
9300 if (buf == NULL)
9301 return FALSE;
9302 }
9303 if (runlen < sortlen)
9304 {
9305 memcpy (buf, p, runlen);
9306 memmove (loc + runlen, loc, sortlen);
9307 memcpy (loc, buf, runlen);
9308 }
9309 else
9310 {
9311 memcpy (buf, loc, sortlen);
9312 memmove (loc, p, runlen);
9313 memcpy (loc + runlen, buf, sortlen);
9314 }
9315 p += runlen - elt_size;
9316 }
9317 }
9318 /* Hashes are no longer valid. */
9319 free (reldata->hashes);
9320 reldata->hashes = NULL;
9321 free (buf);
9322 }
9323 return TRUE;
9324 }
9325
9326 struct elf_link_sort_rela
9327 {
9328 union {
9329 bfd_vma offset;
9330 bfd_vma sym_mask;
9331 } u;
9332 enum elf_reloc_type_class type;
9333 /* We use this as an array of size int_rels_per_ext_rel. */
9334 Elf_Internal_Rela rela[1];
9335 };
9336
9337 /* qsort stability here and for cmp2 is only an issue if multiple
9338 dynamic relocations are emitted at the same address. But targets
9339 that apply a series of dynamic relocations each operating on the
9340 result of the prior relocation can't use -z combreloc as
9341 implemented anyway. Such schemes tend to be broken by sorting on
9342 symbol index. That leaves dynamic NONE relocs as the only other
9343 case where ld might emit multiple relocs at the same address, and
9344 those are only emitted due to target bugs. */
9345
9346 static int
9347 elf_link_sort_cmp1 (const void *A, const void *B)
9348 {
9349 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9350 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9351 int relativea, relativeb;
9352
9353 relativea = a->type == reloc_class_relative;
9354 relativeb = b->type == reloc_class_relative;
9355
9356 if (relativea < relativeb)
9357 return 1;
9358 if (relativea > relativeb)
9359 return -1;
9360 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9361 return -1;
9362 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9363 return 1;
9364 if (a->rela->r_offset < b->rela->r_offset)
9365 return -1;
9366 if (a->rela->r_offset > b->rela->r_offset)
9367 return 1;
9368 return 0;
9369 }
9370
9371 static int
9372 elf_link_sort_cmp2 (const void *A, const void *B)
9373 {
9374 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9375 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9376
9377 if (a->type < b->type)
9378 return -1;
9379 if (a->type > b->type)
9380 return 1;
9381 if (a->u.offset < b->u.offset)
9382 return -1;
9383 if (a->u.offset > b->u.offset)
9384 return 1;
9385 if (a->rela->r_offset < b->rela->r_offset)
9386 return -1;
9387 if (a->rela->r_offset > b->rela->r_offset)
9388 return 1;
9389 return 0;
9390 }
9391
9392 static size_t
9393 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9394 {
9395 asection *dynamic_relocs;
9396 asection *rela_dyn;
9397 asection *rel_dyn;
9398 bfd_size_type count, size;
9399 size_t i, ret, sort_elt, ext_size;
9400 bfd_byte *sort, *s_non_relative, *p;
9401 struct elf_link_sort_rela *sq;
9402 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9403 int i2e = bed->s->int_rels_per_ext_rel;
9404 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9405 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9406 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9407 struct bfd_link_order *lo;
9408 bfd_vma r_sym_mask;
9409 bfd_boolean use_rela;
9410
9411 /* Find a dynamic reloc section. */
9412 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9413 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9414 if (rela_dyn != NULL && rela_dyn->size > 0
9415 && rel_dyn != NULL && rel_dyn->size > 0)
9416 {
9417 bfd_boolean use_rela_initialised = FALSE;
9418
9419 /* This is just here to stop gcc from complaining.
9420 Its initialization checking code is not perfect. */
9421 use_rela = TRUE;
9422
9423 /* Both sections are present. Examine the sizes
9424 of the indirect sections to help us choose. */
9425 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9426 if (lo->type == bfd_indirect_link_order)
9427 {
9428 asection *o = lo->u.indirect.section;
9429
9430 if ((o->size % bed->s->sizeof_rela) == 0)
9431 {
9432 if ((o->size % bed->s->sizeof_rel) == 0)
9433 /* Section size is divisible by both rel and rela sizes.
9434 It is of no help to us. */
9435 ;
9436 else
9437 {
9438 /* Section size is only divisible by rela. */
9439 if (use_rela_initialised && !use_rela)
9440 {
9441 _bfd_error_handler (_("%pB: unable to sort relocs - "
9442 "they are in more than one size"),
9443 abfd);
9444 bfd_set_error (bfd_error_invalid_operation);
9445 return 0;
9446 }
9447 else
9448 {
9449 use_rela = TRUE;
9450 use_rela_initialised = TRUE;
9451 }
9452 }
9453 }
9454 else if ((o->size % bed->s->sizeof_rel) == 0)
9455 {
9456 /* Section size is only divisible by rel. */
9457 if (use_rela_initialised && use_rela)
9458 {
9459 _bfd_error_handler (_("%pB: unable to sort relocs - "
9460 "they are in more than one size"),
9461 abfd);
9462 bfd_set_error (bfd_error_invalid_operation);
9463 return 0;
9464 }
9465 else
9466 {
9467 use_rela = FALSE;
9468 use_rela_initialised = TRUE;
9469 }
9470 }
9471 else
9472 {
9473 /* The section size is not divisible by either -
9474 something is wrong. */
9475 _bfd_error_handler (_("%pB: unable to sort relocs - "
9476 "they are of an unknown size"), abfd);
9477 bfd_set_error (bfd_error_invalid_operation);
9478 return 0;
9479 }
9480 }
9481
9482 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9483 if (lo->type == bfd_indirect_link_order)
9484 {
9485 asection *o = lo->u.indirect.section;
9486
9487 if ((o->size % bed->s->sizeof_rela) == 0)
9488 {
9489 if ((o->size % bed->s->sizeof_rel) == 0)
9490 /* Section size is divisible by both rel and rela sizes.
9491 It is of no help to us. */
9492 ;
9493 else
9494 {
9495 /* Section size is only divisible by rela. */
9496 if (use_rela_initialised && !use_rela)
9497 {
9498 _bfd_error_handler (_("%pB: unable to sort relocs - "
9499 "they are in more than one size"),
9500 abfd);
9501 bfd_set_error (bfd_error_invalid_operation);
9502 return 0;
9503 }
9504 else
9505 {
9506 use_rela = TRUE;
9507 use_rela_initialised = TRUE;
9508 }
9509 }
9510 }
9511 else if ((o->size % bed->s->sizeof_rel) == 0)
9512 {
9513 /* Section size is only divisible by rel. */
9514 if (use_rela_initialised && use_rela)
9515 {
9516 _bfd_error_handler (_("%pB: unable to sort relocs - "
9517 "they are in more than one size"),
9518 abfd);
9519 bfd_set_error (bfd_error_invalid_operation);
9520 return 0;
9521 }
9522 else
9523 {
9524 use_rela = FALSE;
9525 use_rela_initialised = TRUE;
9526 }
9527 }
9528 else
9529 {
9530 /* The section size is not divisible by either -
9531 something is wrong. */
9532 _bfd_error_handler (_("%pB: unable to sort relocs - "
9533 "they are of an unknown size"), abfd);
9534 bfd_set_error (bfd_error_invalid_operation);
9535 return 0;
9536 }
9537 }
9538
9539 if (! use_rela_initialised)
9540 /* Make a guess. */
9541 use_rela = TRUE;
9542 }
9543 else if (rela_dyn != NULL && rela_dyn->size > 0)
9544 use_rela = TRUE;
9545 else if (rel_dyn != NULL && rel_dyn->size > 0)
9546 use_rela = FALSE;
9547 else
9548 return 0;
9549
9550 if (use_rela)
9551 {
9552 dynamic_relocs = rela_dyn;
9553 ext_size = bed->s->sizeof_rela;
9554 swap_in = bed->s->swap_reloca_in;
9555 swap_out = bed->s->swap_reloca_out;
9556 }
9557 else
9558 {
9559 dynamic_relocs = rel_dyn;
9560 ext_size = bed->s->sizeof_rel;
9561 swap_in = bed->s->swap_reloc_in;
9562 swap_out = bed->s->swap_reloc_out;
9563 }
9564
9565 size = 0;
9566 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9567 if (lo->type == bfd_indirect_link_order)
9568 size += lo->u.indirect.section->size;
9569
9570 if (size != dynamic_relocs->size)
9571 return 0;
9572
9573 sort_elt = (sizeof (struct elf_link_sort_rela)
9574 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9575
9576 count = dynamic_relocs->size / ext_size;
9577 if (count == 0)
9578 return 0;
9579 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9580
9581 if (sort == NULL)
9582 {
9583 (*info->callbacks->warning)
9584 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9585 return 0;
9586 }
9587
9588 if (bed->s->arch_size == 32)
9589 r_sym_mask = ~(bfd_vma) 0xff;
9590 else
9591 r_sym_mask = ~(bfd_vma) 0xffffffff;
9592
9593 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9594 if (lo->type == bfd_indirect_link_order)
9595 {
9596 bfd_byte *erel, *erelend;
9597 asection *o = lo->u.indirect.section;
9598
9599 if (o->contents == NULL && o->size != 0)
9600 {
9601 /* This is a reloc section that is being handled as a normal
9602 section. See bfd_section_from_shdr. We can't combine
9603 relocs in this case. */
9604 free (sort);
9605 return 0;
9606 }
9607 erel = o->contents;
9608 erelend = o->contents + o->size;
9609 p = sort + o->output_offset * opb / ext_size * sort_elt;
9610
9611 while (erel < erelend)
9612 {
9613 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9614
9615 (*swap_in) (abfd, erel, s->rela);
9616 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9617 s->u.sym_mask = r_sym_mask;
9618 p += sort_elt;
9619 erel += ext_size;
9620 }
9621 }
9622
9623 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9624
9625 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9626 {
9627 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9628 if (s->type != reloc_class_relative)
9629 break;
9630 }
9631 ret = i;
9632 s_non_relative = p;
9633
9634 sq = (struct elf_link_sort_rela *) s_non_relative;
9635 for (; i < count; i++, p += sort_elt)
9636 {
9637 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9638 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9639 sq = sp;
9640 sp->u.offset = sq->rela->r_offset;
9641 }
9642
9643 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9644
9645 struct elf_link_hash_table *htab = elf_hash_table (info);
9646 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9647 {
9648 /* We have plt relocs in .rela.dyn. */
9649 sq = (struct elf_link_sort_rela *) sort;
9650 for (i = 0; i < count; i++)
9651 if (sq[count - i - 1].type != reloc_class_plt)
9652 break;
9653 if (i != 0 && htab->srelplt->size == i * ext_size)
9654 {
9655 struct bfd_link_order **plo;
9656 /* Put srelplt link_order last. This is so the output_offset
9657 set in the next loop is correct for DT_JMPREL. */
9658 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9659 if ((*plo)->type == bfd_indirect_link_order
9660 && (*plo)->u.indirect.section == htab->srelplt)
9661 {
9662 lo = *plo;
9663 *plo = lo->next;
9664 }
9665 else
9666 plo = &(*plo)->next;
9667 *plo = lo;
9668 lo->next = NULL;
9669 dynamic_relocs->map_tail.link_order = lo;
9670 }
9671 }
9672
9673 p = sort;
9674 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9675 if (lo->type == bfd_indirect_link_order)
9676 {
9677 bfd_byte *erel, *erelend;
9678 asection *o = lo->u.indirect.section;
9679
9680 erel = o->contents;
9681 erelend = o->contents + o->size;
9682 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9683 while (erel < erelend)
9684 {
9685 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9686 (*swap_out) (abfd, s->rela, erel);
9687 p += sort_elt;
9688 erel += ext_size;
9689 }
9690 }
9691
9692 free (sort);
9693 *psec = dynamic_relocs;
9694 return ret;
9695 }
9696
9697 /* Add a symbol to the output symbol string table. */
9698
9699 static int
9700 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9701 const char *name,
9702 Elf_Internal_Sym *elfsym,
9703 asection *input_sec,
9704 struct elf_link_hash_entry *h)
9705 {
9706 int (*output_symbol_hook)
9707 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9708 struct elf_link_hash_entry *);
9709 struct elf_link_hash_table *hash_table;
9710 const struct elf_backend_data *bed;
9711 bfd_size_type strtabsize;
9712
9713 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9714
9715 bed = get_elf_backend_data (flinfo->output_bfd);
9716 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9717 if (output_symbol_hook != NULL)
9718 {
9719 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9720 if (ret != 1)
9721 return ret;
9722 }
9723
9724 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9725 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9726 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9727 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9728
9729 if (name == NULL
9730 || *name == '\0'
9731 || (input_sec->flags & SEC_EXCLUDE))
9732 elfsym->st_name = (unsigned long) -1;
9733 else
9734 {
9735 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9736 to get the final offset for st_name. */
9737 char *versioned_name = (char *) name;
9738 if (h != NULL)
9739 {
9740 if (h->versioned == versioned && h->def_dynamic)
9741 {
9742 /* Keep only one '@' for versioned symbols defined in
9743 shared objects. */
9744 char *version = strrchr (name, ELF_VER_CHR);
9745 char *base_end = strchr (name, ELF_VER_CHR);
9746 if (version != base_end)
9747 {
9748 size_t base_len;
9749 size_t len = strlen (name);
9750 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9751 if (versioned_name == NULL)
9752 return 0;
9753 base_len = base_end - name;
9754 memcpy (versioned_name, name, base_len);
9755 memcpy (versioned_name + base_len, version,
9756 len - base_len);
9757 }
9758 }
9759 }
9760 else if (flinfo->info->unique_symbol
9761 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9762 {
9763 struct local_hash_entry *lh;
9764 switch (ELF_ST_TYPE (elfsym->st_info))
9765 {
9766 case STT_FILE:
9767 case STT_SECTION:
9768 break;
9769 default:
9770 lh = (struct local_hash_entry *) bfd_hash_lookup
9771 (&flinfo->local_hash_table, name, TRUE, FALSE);
9772 if (lh == NULL)
9773 return 0;
9774 if (lh->count)
9775 {
9776 /* Append ".COUNT" to duplicated local symbols. */
9777 size_t count_len;
9778 size_t base_len = lh->size;
9779 char buf[30];
9780 sprintf (buf, "%lx", lh->count);
9781 if (!base_len)
9782 {
9783 base_len = strlen (name);
9784 lh->size = base_len;
9785 }
9786 count_len = strlen (buf);
9787 versioned_name = bfd_alloc (flinfo->output_bfd,
9788 base_len + count_len + 2);
9789 if (versioned_name == NULL)
9790 return 0;
9791 memcpy (versioned_name, name, base_len);
9792 versioned_name[base_len] = '.';
9793 memcpy (versioned_name + base_len + 1, buf,
9794 count_len + 1);
9795 }
9796 lh->count++;
9797 break;
9798 }
9799 }
9800 elfsym->st_name
9801 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9802 versioned_name, FALSE);
9803 if (elfsym->st_name == (unsigned long) -1)
9804 return 0;
9805 }
9806
9807 hash_table = elf_hash_table (flinfo->info);
9808 strtabsize = hash_table->strtabsize;
9809 if (strtabsize <= hash_table->strtabcount)
9810 {
9811 strtabsize += strtabsize;
9812 hash_table->strtabsize = strtabsize;
9813 strtabsize *= sizeof (*hash_table->strtab);
9814 hash_table->strtab
9815 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9816 strtabsize);
9817 if (hash_table->strtab == NULL)
9818 return 0;
9819 }
9820 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9821 hash_table->strtab[hash_table->strtabcount].dest_index
9822 = hash_table->strtabcount;
9823 hash_table->strtab[hash_table->strtabcount].destshndx_index
9824 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9825
9826 flinfo->output_bfd->symcount += 1;
9827 hash_table->strtabcount += 1;
9828
9829 return 1;
9830 }
9831
9832 /* Swap symbols out to the symbol table and flush the output symbols to
9833 the file. */
9834
9835 static bfd_boolean
9836 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9837 {
9838 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9839 size_t amt;
9840 size_t i;
9841 const struct elf_backend_data *bed;
9842 bfd_byte *symbuf;
9843 Elf_Internal_Shdr *hdr;
9844 file_ptr pos;
9845 bfd_boolean ret;
9846
9847 if (!hash_table->strtabcount)
9848 return TRUE;
9849
9850 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9851
9852 bed = get_elf_backend_data (flinfo->output_bfd);
9853
9854 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9855 symbuf = (bfd_byte *) bfd_malloc (amt);
9856 if (symbuf == NULL)
9857 return FALSE;
9858
9859 if (flinfo->symshndxbuf)
9860 {
9861 amt = sizeof (Elf_External_Sym_Shndx);
9862 amt *= bfd_get_symcount (flinfo->output_bfd);
9863 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9864 if (flinfo->symshndxbuf == NULL)
9865 {
9866 free (symbuf);
9867 return FALSE;
9868 }
9869 }
9870
9871 /* Now swap out the symbols. */
9872 for (i = 0; i < hash_table->strtabcount; i++)
9873 {
9874 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9875 if (elfsym->sym.st_name == (unsigned long) -1)
9876 elfsym->sym.st_name = 0;
9877 else
9878 elfsym->sym.st_name
9879 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9880 elfsym->sym.st_name);
9881
9882 /* Inform the linker of the addition of this symbol. */
9883
9884 if (flinfo->info->callbacks->ctf_new_symbol)
9885 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9886 &elfsym->sym);
9887
9888 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9889 ((bfd_byte *) symbuf
9890 + (elfsym->dest_index
9891 * bed->s->sizeof_sym)),
9892 (flinfo->symshndxbuf
9893 + elfsym->destshndx_index));
9894 }
9895
9896 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9897 pos = hdr->sh_offset + hdr->sh_size;
9898 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9899 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9900 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9901 {
9902 hdr->sh_size += amt;
9903 ret = TRUE;
9904 }
9905 else
9906 ret = FALSE;
9907
9908 free (symbuf);
9909
9910 free (hash_table->strtab);
9911 hash_table->strtab = NULL;
9912
9913 return ret;
9914 }
9915
9916 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9917
9918 static bfd_boolean
9919 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9920 {
9921 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9922 && sym->st_shndx < SHN_LORESERVE)
9923 {
9924 /* The gABI doesn't support dynamic symbols in output sections
9925 beyond 64k. */
9926 _bfd_error_handler
9927 /* xgettext:c-format */
9928 (_("%pB: too many sections: %d (>= %d)"),
9929 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9930 bfd_set_error (bfd_error_nonrepresentable_section);
9931 return FALSE;
9932 }
9933 return TRUE;
9934 }
9935
9936 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9937 allowing an unsatisfied unversioned symbol in the DSO to match a
9938 versioned symbol that would normally require an explicit version.
9939 We also handle the case that a DSO references a hidden symbol
9940 which may be satisfied by a versioned symbol in another DSO. */
9941
9942 static bfd_boolean
9943 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9944 const struct elf_backend_data *bed,
9945 struct elf_link_hash_entry *h)
9946 {
9947 bfd *abfd;
9948 struct elf_link_loaded_list *loaded;
9949
9950 if (!is_elf_hash_table (info->hash))
9951 return FALSE;
9952
9953 /* Check indirect symbol. */
9954 while (h->root.type == bfd_link_hash_indirect)
9955 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9956
9957 switch (h->root.type)
9958 {
9959 default:
9960 abfd = NULL;
9961 break;
9962
9963 case bfd_link_hash_undefined:
9964 case bfd_link_hash_undefweak:
9965 abfd = h->root.u.undef.abfd;
9966 if (abfd == NULL
9967 || (abfd->flags & DYNAMIC) == 0
9968 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9969 return FALSE;
9970 break;
9971
9972 case bfd_link_hash_defined:
9973 case bfd_link_hash_defweak:
9974 abfd = h->root.u.def.section->owner;
9975 break;
9976
9977 case bfd_link_hash_common:
9978 abfd = h->root.u.c.p->section->owner;
9979 break;
9980 }
9981 BFD_ASSERT (abfd != NULL);
9982
9983 for (loaded = elf_hash_table (info)->dyn_loaded;
9984 loaded != NULL;
9985 loaded = loaded->next)
9986 {
9987 bfd *input;
9988 Elf_Internal_Shdr *hdr;
9989 size_t symcount;
9990 size_t extsymcount;
9991 size_t extsymoff;
9992 Elf_Internal_Shdr *versymhdr;
9993 Elf_Internal_Sym *isym;
9994 Elf_Internal_Sym *isymend;
9995 Elf_Internal_Sym *isymbuf;
9996 Elf_External_Versym *ever;
9997 Elf_External_Versym *extversym;
9998
9999 input = loaded->abfd;
10000
10001 /* We check each DSO for a possible hidden versioned definition. */
10002 if (input == abfd
10003 || elf_dynversym (input) == 0)
10004 continue;
10005
10006 hdr = &elf_tdata (input)->dynsymtab_hdr;
10007
10008 symcount = hdr->sh_size / bed->s->sizeof_sym;
10009 if (elf_bad_symtab (input))
10010 {
10011 extsymcount = symcount;
10012 extsymoff = 0;
10013 }
10014 else
10015 {
10016 extsymcount = symcount - hdr->sh_info;
10017 extsymoff = hdr->sh_info;
10018 }
10019
10020 if (extsymcount == 0)
10021 continue;
10022
10023 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10024 NULL, NULL, NULL);
10025 if (isymbuf == NULL)
10026 return FALSE;
10027
10028 /* Read in any version definitions. */
10029 versymhdr = &elf_tdata (input)->dynversym_hdr;
10030 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10031 || (extversym = (Elf_External_Versym *)
10032 _bfd_malloc_and_read (input, versymhdr->sh_size,
10033 versymhdr->sh_size)) == NULL)
10034 {
10035 free (isymbuf);
10036 return FALSE;
10037 }
10038
10039 ever = extversym + extsymoff;
10040 isymend = isymbuf + extsymcount;
10041 for (isym = isymbuf; isym < isymend; isym++, ever++)
10042 {
10043 const char *name;
10044 Elf_Internal_Versym iver;
10045 unsigned short version_index;
10046
10047 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10048 || isym->st_shndx == SHN_UNDEF)
10049 continue;
10050
10051 name = bfd_elf_string_from_elf_section (input,
10052 hdr->sh_link,
10053 isym->st_name);
10054 if (strcmp (name, h->root.root.string) != 0)
10055 continue;
10056
10057 _bfd_elf_swap_versym_in (input, ever, &iver);
10058
10059 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10060 && !(h->def_regular
10061 && h->forced_local))
10062 {
10063 /* If we have a non-hidden versioned sym, then it should
10064 have provided a definition for the undefined sym unless
10065 it is defined in a non-shared object and forced local.
10066 */
10067 abort ();
10068 }
10069
10070 version_index = iver.vs_vers & VERSYM_VERSION;
10071 if (version_index == 1 || version_index == 2)
10072 {
10073 /* This is the base or first version. We can use it. */
10074 free (extversym);
10075 free (isymbuf);
10076 return TRUE;
10077 }
10078 }
10079
10080 free (extversym);
10081 free (isymbuf);
10082 }
10083
10084 return FALSE;
10085 }
10086
10087 /* Convert ELF common symbol TYPE. */
10088
10089 static int
10090 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10091 {
10092 /* Commom symbol can only appear in relocatable link. */
10093 if (!bfd_link_relocatable (info))
10094 abort ();
10095 switch (info->elf_stt_common)
10096 {
10097 case unchanged:
10098 break;
10099 case elf_stt_common:
10100 type = STT_COMMON;
10101 break;
10102 case no_elf_stt_common:
10103 type = STT_OBJECT;
10104 break;
10105 }
10106 return type;
10107 }
10108
10109 /* Add an external symbol to the symbol table. This is called from
10110 the hash table traversal routine. When generating a shared object,
10111 we go through the symbol table twice. The first time we output
10112 anything that might have been forced to local scope in a version
10113 script. The second time we output the symbols that are still
10114 global symbols. */
10115
10116 static bfd_boolean
10117 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10118 {
10119 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10120 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10121 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10122 bfd_boolean strip;
10123 Elf_Internal_Sym sym;
10124 asection *input_sec;
10125 const struct elf_backend_data *bed;
10126 long indx;
10127 int ret;
10128 unsigned int type;
10129
10130 if (h->root.type == bfd_link_hash_warning)
10131 {
10132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10133 if (h->root.type == bfd_link_hash_new)
10134 return TRUE;
10135 }
10136
10137 /* Decide whether to output this symbol in this pass. */
10138 if (eoinfo->localsyms)
10139 {
10140 if (!h->forced_local)
10141 return TRUE;
10142 }
10143 else
10144 {
10145 if (h->forced_local)
10146 return TRUE;
10147 }
10148
10149 bed = get_elf_backend_data (flinfo->output_bfd);
10150
10151 if (h->root.type == bfd_link_hash_undefined)
10152 {
10153 /* If we have an undefined symbol reference here then it must have
10154 come from a shared library that is being linked in. (Undefined
10155 references in regular files have already been handled unless
10156 they are in unreferenced sections which are removed by garbage
10157 collection). */
10158 bfd_boolean ignore_undef = FALSE;
10159
10160 /* Some symbols may be special in that the fact that they're
10161 undefined can be safely ignored - let backend determine that. */
10162 if (bed->elf_backend_ignore_undef_symbol)
10163 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10164
10165 /* If we are reporting errors for this situation then do so now. */
10166 if (!ignore_undef
10167 && h->ref_dynamic_nonweak
10168 && (!h->ref_regular || flinfo->info->gc_sections)
10169 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10170 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10171 {
10172 flinfo->info->callbacks->undefined_symbol
10173 (flinfo->info, h->root.root.string,
10174 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10175 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10176 && !flinfo->info->warn_unresolved_syms);
10177 }
10178
10179 /* Strip a global symbol defined in a discarded section. */
10180 if (h->indx == -3)
10181 return TRUE;
10182 }
10183
10184 /* We should also warn if a forced local symbol is referenced from
10185 shared libraries. */
10186 if (bfd_link_executable (flinfo->info)
10187 && h->forced_local
10188 && h->ref_dynamic
10189 && h->def_regular
10190 && !h->dynamic_def
10191 && h->ref_dynamic_nonweak
10192 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10193 {
10194 bfd *def_bfd;
10195 const char *msg;
10196 struct elf_link_hash_entry *hi = h;
10197
10198 /* Check indirect symbol. */
10199 while (hi->root.type == bfd_link_hash_indirect)
10200 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10201
10202 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10203 /* xgettext:c-format */
10204 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10205 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10206 /* xgettext:c-format */
10207 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10208 else
10209 /* xgettext:c-format */
10210 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10211 def_bfd = flinfo->output_bfd;
10212 if (hi->root.u.def.section != bfd_abs_section_ptr)
10213 def_bfd = hi->root.u.def.section->owner;
10214 _bfd_error_handler (msg, flinfo->output_bfd,
10215 h->root.root.string, def_bfd);
10216 bfd_set_error (bfd_error_bad_value);
10217 eoinfo->failed = TRUE;
10218 return FALSE;
10219 }
10220
10221 /* We don't want to output symbols that have never been mentioned by
10222 a regular file, or that we have been told to strip. However, if
10223 h->indx is set to -2, the symbol is used by a reloc and we must
10224 output it. */
10225 strip = FALSE;
10226 if (h->indx == -2)
10227 ;
10228 else if ((h->def_dynamic
10229 || h->ref_dynamic
10230 || h->root.type == bfd_link_hash_new)
10231 && !h->def_regular
10232 && !h->ref_regular)
10233 strip = TRUE;
10234 else if (flinfo->info->strip == strip_all)
10235 strip = TRUE;
10236 else if (flinfo->info->strip == strip_some
10237 && bfd_hash_lookup (flinfo->info->keep_hash,
10238 h->root.root.string, FALSE, FALSE) == NULL)
10239 strip = TRUE;
10240 else if ((h->root.type == bfd_link_hash_defined
10241 || h->root.type == bfd_link_hash_defweak)
10242 && ((flinfo->info->strip_discarded
10243 && discarded_section (h->root.u.def.section))
10244 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10245 && h->root.u.def.section->owner != NULL
10246 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10247 strip = TRUE;
10248 else if ((h->root.type == bfd_link_hash_undefined
10249 || h->root.type == bfd_link_hash_undefweak)
10250 && h->root.u.undef.abfd != NULL
10251 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10252 strip = TRUE;
10253
10254 type = h->type;
10255
10256 /* If we're stripping it, and it's not a dynamic symbol, there's
10257 nothing else to do. However, if it is a forced local symbol or
10258 an ifunc symbol we need to give the backend finish_dynamic_symbol
10259 function a chance to make it dynamic. */
10260 if (strip
10261 && h->dynindx == -1
10262 && type != STT_GNU_IFUNC
10263 && !h->forced_local)
10264 return TRUE;
10265
10266 sym.st_value = 0;
10267 sym.st_size = h->size;
10268 sym.st_other = h->other;
10269 switch (h->root.type)
10270 {
10271 default:
10272 case bfd_link_hash_new:
10273 case bfd_link_hash_warning:
10274 abort ();
10275 return FALSE;
10276
10277 case bfd_link_hash_undefined:
10278 case bfd_link_hash_undefweak:
10279 input_sec = bfd_und_section_ptr;
10280 sym.st_shndx = SHN_UNDEF;
10281 break;
10282
10283 case bfd_link_hash_defined:
10284 case bfd_link_hash_defweak:
10285 {
10286 input_sec = h->root.u.def.section;
10287 if (input_sec->output_section != NULL)
10288 {
10289 sym.st_shndx =
10290 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10291 input_sec->output_section);
10292 if (sym.st_shndx == SHN_BAD)
10293 {
10294 _bfd_error_handler
10295 /* xgettext:c-format */
10296 (_("%pB: could not find output section %pA for input section %pA"),
10297 flinfo->output_bfd, input_sec->output_section, input_sec);
10298 bfd_set_error (bfd_error_nonrepresentable_section);
10299 eoinfo->failed = TRUE;
10300 return FALSE;
10301 }
10302
10303 /* ELF symbols in relocatable files are section relative,
10304 but in nonrelocatable files they are virtual
10305 addresses. */
10306 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10307 if (!bfd_link_relocatable (flinfo->info))
10308 {
10309 sym.st_value += input_sec->output_section->vma;
10310 if (h->type == STT_TLS)
10311 {
10312 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10313 if (tls_sec != NULL)
10314 sym.st_value -= tls_sec->vma;
10315 }
10316 }
10317 }
10318 else
10319 {
10320 BFD_ASSERT (input_sec->owner == NULL
10321 || (input_sec->owner->flags & DYNAMIC) != 0);
10322 sym.st_shndx = SHN_UNDEF;
10323 input_sec = bfd_und_section_ptr;
10324 }
10325 }
10326 break;
10327
10328 case bfd_link_hash_common:
10329 input_sec = h->root.u.c.p->section;
10330 sym.st_shndx = bed->common_section_index (input_sec);
10331 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10332 break;
10333
10334 case bfd_link_hash_indirect:
10335 /* These symbols are created by symbol versioning. They point
10336 to the decorated version of the name. For example, if the
10337 symbol foo@@GNU_1.2 is the default, which should be used when
10338 foo is used with no version, then we add an indirect symbol
10339 foo which points to foo@@GNU_1.2. We ignore these symbols,
10340 since the indirected symbol is already in the hash table. */
10341 return TRUE;
10342 }
10343
10344 if (type == STT_COMMON || type == STT_OBJECT)
10345 switch (h->root.type)
10346 {
10347 case bfd_link_hash_common:
10348 type = elf_link_convert_common_type (flinfo->info, type);
10349 break;
10350 case bfd_link_hash_defined:
10351 case bfd_link_hash_defweak:
10352 if (bed->common_definition (&sym))
10353 type = elf_link_convert_common_type (flinfo->info, type);
10354 else
10355 type = STT_OBJECT;
10356 break;
10357 case bfd_link_hash_undefined:
10358 case bfd_link_hash_undefweak:
10359 break;
10360 default:
10361 abort ();
10362 }
10363
10364 if (h->forced_local)
10365 {
10366 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10367 /* Turn off visibility on local symbol. */
10368 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10369 }
10370 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10371 else if (h->unique_global && h->def_regular)
10372 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10373 else if (h->root.type == bfd_link_hash_undefweak
10374 || h->root.type == bfd_link_hash_defweak)
10375 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10376 else
10377 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10378 sym.st_target_internal = h->target_internal;
10379
10380 /* Give the processor backend a chance to tweak the symbol value,
10381 and also to finish up anything that needs to be done for this
10382 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10383 forced local syms when non-shared is due to a historical quirk.
10384 STT_GNU_IFUNC symbol must go through PLT. */
10385 if ((h->type == STT_GNU_IFUNC
10386 && h->def_regular
10387 && !bfd_link_relocatable (flinfo->info))
10388 || ((h->dynindx != -1
10389 || h->forced_local)
10390 && ((bfd_link_pic (flinfo->info)
10391 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10392 || h->root.type != bfd_link_hash_undefweak))
10393 || !h->forced_local)
10394 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10395 {
10396 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10397 (flinfo->output_bfd, flinfo->info, h, &sym)))
10398 {
10399 eoinfo->failed = TRUE;
10400 return FALSE;
10401 }
10402 }
10403
10404 /* If we are marking the symbol as undefined, and there are no
10405 non-weak references to this symbol from a regular object, then
10406 mark the symbol as weak undefined; if there are non-weak
10407 references, mark the symbol as strong. We can't do this earlier,
10408 because it might not be marked as undefined until the
10409 finish_dynamic_symbol routine gets through with it. */
10410 if (sym.st_shndx == SHN_UNDEF
10411 && h->ref_regular
10412 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10413 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10414 {
10415 int bindtype;
10416 type = ELF_ST_TYPE (sym.st_info);
10417
10418 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10419 if (type == STT_GNU_IFUNC)
10420 type = STT_FUNC;
10421
10422 if (h->ref_regular_nonweak)
10423 bindtype = STB_GLOBAL;
10424 else
10425 bindtype = STB_WEAK;
10426 sym.st_info = ELF_ST_INFO (bindtype, type);
10427 }
10428
10429 /* If this is a symbol defined in a dynamic library, don't use the
10430 symbol size from the dynamic library. Relinking an executable
10431 against a new library may introduce gratuitous changes in the
10432 executable's symbols if we keep the size. */
10433 if (sym.st_shndx == SHN_UNDEF
10434 && !h->def_regular
10435 && h->def_dynamic)
10436 sym.st_size = 0;
10437
10438 /* If a non-weak symbol with non-default visibility is not defined
10439 locally, it is a fatal error. */
10440 if (!bfd_link_relocatable (flinfo->info)
10441 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10442 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10443 && h->root.type == bfd_link_hash_undefined
10444 && !h->def_regular)
10445 {
10446 const char *msg;
10447
10448 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10449 /* xgettext:c-format */
10450 msg = _("%pB: protected symbol `%s' isn't defined");
10451 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10452 /* xgettext:c-format */
10453 msg = _("%pB: internal symbol `%s' isn't defined");
10454 else
10455 /* xgettext:c-format */
10456 msg = _("%pB: hidden symbol `%s' isn't defined");
10457 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10458 bfd_set_error (bfd_error_bad_value);
10459 eoinfo->failed = TRUE;
10460 return FALSE;
10461 }
10462
10463 /* If this symbol should be put in the .dynsym section, then put it
10464 there now. We already know the symbol index. We also fill in
10465 the entry in the .hash section. */
10466 if (h->dynindx != -1
10467 && elf_hash_table (flinfo->info)->dynamic_sections_created
10468 && elf_hash_table (flinfo->info)->dynsym != NULL
10469 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10470 {
10471 bfd_byte *esym;
10472
10473 /* Since there is no version information in the dynamic string,
10474 if there is no version info in symbol version section, we will
10475 have a run-time problem if not linking executable, referenced
10476 by shared library, or not bound locally. */
10477 if (h->verinfo.verdef == NULL
10478 && (!bfd_link_executable (flinfo->info)
10479 || h->ref_dynamic
10480 || !h->def_regular))
10481 {
10482 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10483
10484 if (p && p [1] != '\0')
10485 {
10486 _bfd_error_handler
10487 /* xgettext:c-format */
10488 (_("%pB: no symbol version section for versioned symbol `%s'"),
10489 flinfo->output_bfd, h->root.root.string);
10490 eoinfo->failed = TRUE;
10491 return FALSE;
10492 }
10493 }
10494
10495 sym.st_name = h->dynstr_index;
10496 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10497 + h->dynindx * bed->s->sizeof_sym);
10498 if (!check_dynsym (flinfo->output_bfd, &sym))
10499 {
10500 eoinfo->failed = TRUE;
10501 return FALSE;
10502 }
10503
10504 /* Inform the linker of the addition of this symbol. */
10505
10506 if (flinfo->info->callbacks->ctf_new_dynsym)
10507 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10508
10509 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10510
10511 if (flinfo->hash_sec != NULL)
10512 {
10513 size_t hash_entry_size;
10514 bfd_byte *bucketpos;
10515 bfd_vma chain;
10516 size_t bucketcount;
10517 size_t bucket;
10518
10519 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10520 bucket = h->u.elf_hash_value % bucketcount;
10521
10522 hash_entry_size
10523 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10524 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10525 + (bucket + 2) * hash_entry_size);
10526 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10527 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10528 bucketpos);
10529 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10530 ((bfd_byte *) flinfo->hash_sec->contents
10531 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10532 }
10533
10534 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10535 {
10536 Elf_Internal_Versym iversym;
10537 Elf_External_Versym *eversym;
10538
10539 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10540 {
10541 if (h->verinfo.verdef == NULL
10542 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10543 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10544 iversym.vs_vers = 0;
10545 else
10546 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10547 }
10548 else
10549 {
10550 if (h->verinfo.vertree == NULL)
10551 iversym.vs_vers = 1;
10552 else
10553 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10554 if (flinfo->info->create_default_symver)
10555 iversym.vs_vers++;
10556 }
10557
10558 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10559 defined locally. */
10560 if (h->versioned == versioned_hidden && h->def_regular)
10561 iversym.vs_vers |= VERSYM_HIDDEN;
10562
10563 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10564 eversym += h->dynindx;
10565 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10566 }
10567 }
10568
10569 /* If the symbol is undefined, and we didn't output it to .dynsym,
10570 strip it from .symtab too. Obviously we can't do this for
10571 relocatable output or when needed for --emit-relocs. */
10572 else if (input_sec == bfd_und_section_ptr
10573 && h->indx != -2
10574 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10575 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10576 && !bfd_link_relocatable (flinfo->info))
10577 return TRUE;
10578
10579 /* Also strip others that we couldn't earlier due to dynamic symbol
10580 processing. */
10581 if (strip)
10582 return TRUE;
10583 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10584 return TRUE;
10585
10586 /* Output a FILE symbol so that following locals are not associated
10587 with the wrong input file. We need one for forced local symbols
10588 if we've seen more than one FILE symbol or when we have exactly
10589 one FILE symbol but global symbols are present in a file other
10590 than the one with the FILE symbol. We also need one if linker
10591 defined symbols are present. In practice these conditions are
10592 always met, so just emit the FILE symbol unconditionally. */
10593 if (eoinfo->localsyms
10594 && !eoinfo->file_sym_done
10595 && eoinfo->flinfo->filesym_count != 0)
10596 {
10597 Elf_Internal_Sym fsym;
10598
10599 memset (&fsym, 0, sizeof (fsym));
10600 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10601 fsym.st_shndx = SHN_ABS;
10602 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10603 bfd_und_section_ptr, NULL))
10604 return FALSE;
10605
10606 eoinfo->file_sym_done = TRUE;
10607 }
10608
10609 indx = bfd_get_symcount (flinfo->output_bfd);
10610 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10611 input_sec, h);
10612 if (ret == 0)
10613 {
10614 eoinfo->failed = TRUE;
10615 return FALSE;
10616 }
10617 else if (ret == 1)
10618 h->indx = indx;
10619 else if (h->indx == -2)
10620 abort();
10621
10622 return TRUE;
10623 }
10624
10625 /* Return TRUE if special handling is done for relocs in SEC against
10626 symbols defined in discarded sections. */
10627
10628 static bfd_boolean
10629 elf_section_ignore_discarded_relocs (asection *sec)
10630 {
10631 const struct elf_backend_data *bed;
10632
10633 switch (sec->sec_info_type)
10634 {
10635 case SEC_INFO_TYPE_STABS:
10636 case SEC_INFO_TYPE_EH_FRAME:
10637 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10638 return TRUE;
10639 default:
10640 break;
10641 }
10642
10643 bed = get_elf_backend_data (sec->owner);
10644 if (bed->elf_backend_ignore_discarded_relocs != NULL
10645 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10646 return TRUE;
10647
10648 return FALSE;
10649 }
10650
10651 /* Return a mask saying how ld should treat relocations in SEC against
10652 symbols defined in discarded sections. If this function returns
10653 COMPLAIN set, ld will issue a warning message. If this function
10654 returns PRETEND set, and the discarded section was link-once and the
10655 same size as the kept link-once section, ld will pretend that the
10656 symbol was actually defined in the kept section. Otherwise ld will
10657 zero the reloc (at least that is the intent, but some cooperation by
10658 the target dependent code is needed, particularly for REL targets). */
10659
10660 unsigned int
10661 _bfd_elf_default_action_discarded (asection *sec)
10662 {
10663 if (sec->flags & SEC_DEBUGGING)
10664 return PRETEND;
10665
10666 if (strcmp (".eh_frame", sec->name) == 0)
10667 return 0;
10668
10669 if (strcmp (".gcc_except_table", sec->name) == 0)
10670 return 0;
10671
10672 return COMPLAIN | PRETEND;
10673 }
10674
10675 /* Find a match between a section and a member of a section group. */
10676
10677 static asection *
10678 match_group_member (asection *sec, asection *group,
10679 struct bfd_link_info *info)
10680 {
10681 asection *first = elf_next_in_group (group);
10682 asection *s = first;
10683
10684 while (s != NULL)
10685 {
10686 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10687 return s;
10688
10689 s = elf_next_in_group (s);
10690 if (s == first)
10691 break;
10692 }
10693
10694 return NULL;
10695 }
10696
10697 /* Check if the kept section of a discarded section SEC can be used
10698 to replace it. Return the replacement if it is OK. Otherwise return
10699 NULL. */
10700
10701 asection *
10702 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10703 {
10704 asection *kept;
10705
10706 kept = sec->kept_section;
10707 if (kept != NULL)
10708 {
10709 if ((kept->flags & SEC_GROUP) != 0)
10710 kept = match_group_member (sec, kept, info);
10711 if (kept != NULL)
10712 {
10713 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10714 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10715 kept = NULL;
10716 else
10717 {
10718 /* Get the real kept section. */
10719 asection *next;
10720 for (next = kept->kept_section;
10721 next != NULL;
10722 next = next->kept_section)
10723 kept = next;
10724 }
10725 }
10726 sec->kept_section = kept;
10727 }
10728 return kept;
10729 }
10730
10731 /* Link an input file into the linker output file. This function
10732 handles all the sections and relocations of the input file at once.
10733 This is so that we only have to read the local symbols once, and
10734 don't have to keep them in memory. */
10735
10736 static bfd_boolean
10737 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10738 {
10739 int (*relocate_section)
10740 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10741 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10742 bfd *output_bfd;
10743 Elf_Internal_Shdr *symtab_hdr;
10744 size_t locsymcount;
10745 size_t extsymoff;
10746 Elf_Internal_Sym *isymbuf;
10747 Elf_Internal_Sym *isym;
10748 Elf_Internal_Sym *isymend;
10749 long *pindex;
10750 asection **ppsection;
10751 asection *o;
10752 const struct elf_backend_data *bed;
10753 struct elf_link_hash_entry **sym_hashes;
10754 bfd_size_type address_size;
10755 bfd_vma r_type_mask;
10756 int r_sym_shift;
10757 bfd_boolean have_file_sym = FALSE;
10758
10759 output_bfd = flinfo->output_bfd;
10760 bed = get_elf_backend_data (output_bfd);
10761 relocate_section = bed->elf_backend_relocate_section;
10762
10763 /* If this is a dynamic object, we don't want to do anything here:
10764 we don't want the local symbols, and we don't want the section
10765 contents. */
10766 if ((input_bfd->flags & DYNAMIC) != 0)
10767 return TRUE;
10768
10769 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10770 if (elf_bad_symtab (input_bfd))
10771 {
10772 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10773 extsymoff = 0;
10774 }
10775 else
10776 {
10777 locsymcount = symtab_hdr->sh_info;
10778 extsymoff = symtab_hdr->sh_info;
10779 }
10780
10781 /* Enable GNU OSABI features in the output BFD that are used in the input
10782 BFD. */
10783 if (bed->elf_osabi == ELFOSABI_NONE
10784 || bed->elf_osabi == ELFOSABI_GNU
10785 || bed->elf_osabi == ELFOSABI_FREEBSD)
10786 elf_tdata (output_bfd)->has_gnu_osabi
10787 |= elf_tdata (input_bfd)->has_gnu_osabi;
10788
10789 /* Read the local symbols. */
10790 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10791 if (isymbuf == NULL && locsymcount != 0)
10792 {
10793 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10794 flinfo->internal_syms,
10795 flinfo->external_syms,
10796 flinfo->locsym_shndx);
10797 if (isymbuf == NULL)
10798 return FALSE;
10799 }
10800
10801 /* Find local symbol sections and adjust values of symbols in
10802 SEC_MERGE sections. Write out those local symbols we know are
10803 going into the output file. */
10804 isymend = isymbuf + locsymcount;
10805 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10806 isym < isymend;
10807 isym++, pindex++, ppsection++)
10808 {
10809 asection *isec;
10810 const char *name;
10811 Elf_Internal_Sym osym;
10812 long indx;
10813 int ret;
10814
10815 *pindex = -1;
10816
10817 if (elf_bad_symtab (input_bfd))
10818 {
10819 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10820 {
10821 *ppsection = NULL;
10822 continue;
10823 }
10824 }
10825
10826 if (isym->st_shndx == SHN_UNDEF)
10827 isec = bfd_und_section_ptr;
10828 else if (isym->st_shndx == SHN_ABS)
10829 isec = bfd_abs_section_ptr;
10830 else if (isym->st_shndx == SHN_COMMON)
10831 isec = bfd_com_section_ptr;
10832 else
10833 {
10834 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10835 if (isec == NULL)
10836 {
10837 /* Don't attempt to output symbols with st_shnx in the
10838 reserved range other than SHN_ABS and SHN_COMMON. */
10839 isec = bfd_und_section_ptr;
10840 }
10841 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10842 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10843 isym->st_value =
10844 _bfd_merged_section_offset (output_bfd, &isec,
10845 elf_section_data (isec)->sec_info,
10846 isym->st_value);
10847 }
10848
10849 *ppsection = isec;
10850
10851 /* Don't output the first, undefined, symbol. In fact, don't
10852 output any undefined local symbol. */
10853 if (isec == bfd_und_section_ptr)
10854 continue;
10855
10856 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10857 {
10858 /* We never output section symbols. Instead, we use the
10859 section symbol of the corresponding section in the output
10860 file. */
10861 continue;
10862 }
10863
10864 /* If we are stripping all symbols, we don't want to output this
10865 one. */
10866 if (flinfo->info->strip == strip_all)
10867 continue;
10868
10869 /* If we are discarding all local symbols, we don't want to
10870 output this one. If we are generating a relocatable output
10871 file, then some of the local symbols may be required by
10872 relocs; we output them below as we discover that they are
10873 needed. */
10874 if (flinfo->info->discard == discard_all)
10875 continue;
10876
10877 /* If this symbol is defined in a section which we are
10878 discarding, we don't need to keep it. */
10879 if (isym->st_shndx != SHN_UNDEF
10880 && isym->st_shndx < SHN_LORESERVE
10881 && isec->output_section == NULL
10882 && flinfo->info->non_contiguous_regions
10883 && flinfo->info->non_contiguous_regions_warnings)
10884 {
10885 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10886 "discards section `%s' from '%s'\n"),
10887 isec->name, bfd_get_filename (isec->owner));
10888 continue;
10889 }
10890
10891 if (isym->st_shndx != SHN_UNDEF
10892 && isym->st_shndx < SHN_LORESERVE
10893 && bfd_section_removed_from_list (output_bfd,
10894 isec->output_section))
10895 continue;
10896
10897 /* Get the name of the symbol. */
10898 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10899 isym->st_name);
10900 if (name == NULL)
10901 return FALSE;
10902
10903 /* See if we are discarding symbols with this name. */
10904 if ((flinfo->info->strip == strip_some
10905 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10906 == NULL))
10907 || (((flinfo->info->discard == discard_sec_merge
10908 && (isec->flags & SEC_MERGE)
10909 && !bfd_link_relocatable (flinfo->info))
10910 || flinfo->info->discard == discard_l)
10911 && bfd_is_local_label_name (input_bfd, name)))
10912 continue;
10913
10914 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10915 {
10916 if (input_bfd->lto_output)
10917 /* -flto puts a temp file name here. This means builds
10918 are not reproducible. Discard the symbol. */
10919 continue;
10920 have_file_sym = TRUE;
10921 flinfo->filesym_count += 1;
10922 }
10923 if (!have_file_sym)
10924 {
10925 /* In the absence of debug info, bfd_find_nearest_line uses
10926 FILE symbols to determine the source file for local
10927 function symbols. Provide a FILE symbol here if input
10928 files lack such, so that their symbols won't be
10929 associated with a previous input file. It's not the
10930 source file, but the best we can do. */
10931 have_file_sym = TRUE;
10932 flinfo->filesym_count += 1;
10933 memset (&osym, 0, sizeof (osym));
10934 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10935 osym.st_shndx = SHN_ABS;
10936 if (!elf_link_output_symstrtab (flinfo,
10937 (input_bfd->lto_output ? NULL
10938 : bfd_get_filename (input_bfd)),
10939 &osym, bfd_abs_section_ptr,
10940 NULL))
10941 return FALSE;
10942 }
10943
10944 osym = *isym;
10945
10946 /* Adjust the section index for the output file. */
10947 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10948 isec->output_section);
10949 if (osym.st_shndx == SHN_BAD)
10950 return FALSE;
10951
10952 /* ELF symbols in relocatable files are section relative, but
10953 in executable files they are virtual addresses. Note that
10954 this code assumes that all ELF sections have an associated
10955 BFD section with a reasonable value for output_offset; below
10956 we assume that they also have a reasonable value for
10957 output_section. Any special sections must be set up to meet
10958 these requirements. */
10959 osym.st_value += isec->output_offset;
10960 if (!bfd_link_relocatable (flinfo->info))
10961 {
10962 osym.st_value += isec->output_section->vma;
10963 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10964 {
10965 /* STT_TLS symbols are relative to PT_TLS segment base. */
10966 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10967 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10968 else
10969 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10970 STT_NOTYPE);
10971 }
10972 }
10973
10974 indx = bfd_get_symcount (output_bfd);
10975 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10976 if (ret == 0)
10977 return FALSE;
10978 else if (ret == 1)
10979 *pindex = indx;
10980 }
10981
10982 if (bed->s->arch_size == 32)
10983 {
10984 r_type_mask = 0xff;
10985 r_sym_shift = 8;
10986 address_size = 4;
10987 }
10988 else
10989 {
10990 r_type_mask = 0xffffffff;
10991 r_sym_shift = 32;
10992 address_size = 8;
10993 }
10994
10995 /* Relocate the contents of each section. */
10996 sym_hashes = elf_sym_hashes (input_bfd);
10997 for (o = input_bfd->sections; o != NULL; o = o->next)
10998 {
10999 bfd_byte *contents;
11000
11001 if (! o->linker_mark)
11002 {
11003 /* This section was omitted from the link. */
11004 continue;
11005 }
11006
11007 if (!flinfo->info->resolve_section_groups
11008 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11009 {
11010 /* Deal with the group signature symbol. */
11011 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11012 unsigned long symndx = sec_data->this_hdr.sh_info;
11013 asection *osec = o->output_section;
11014
11015 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11016 if (symndx >= locsymcount
11017 || (elf_bad_symtab (input_bfd)
11018 && flinfo->sections[symndx] == NULL))
11019 {
11020 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11021 while (h->root.type == bfd_link_hash_indirect
11022 || h->root.type == bfd_link_hash_warning)
11023 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11024 /* Arrange for symbol to be output. */
11025 h->indx = -2;
11026 elf_section_data (osec)->this_hdr.sh_info = -2;
11027 }
11028 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11029 {
11030 /* We'll use the output section target_index. */
11031 asection *sec = flinfo->sections[symndx]->output_section;
11032 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11033 }
11034 else
11035 {
11036 if (flinfo->indices[symndx] == -1)
11037 {
11038 /* Otherwise output the local symbol now. */
11039 Elf_Internal_Sym sym = isymbuf[symndx];
11040 asection *sec = flinfo->sections[symndx]->output_section;
11041 const char *name;
11042 long indx;
11043 int ret;
11044
11045 name = bfd_elf_string_from_elf_section (input_bfd,
11046 symtab_hdr->sh_link,
11047 sym.st_name);
11048 if (name == NULL)
11049 return FALSE;
11050
11051 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11052 sec);
11053 if (sym.st_shndx == SHN_BAD)
11054 return FALSE;
11055
11056 sym.st_value += o->output_offset;
11057
11058 indx = bfd_get_symcount (output_bfd);
11059 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11060 NULL);
11061 if (ret == 0)
11062 return FALSE;
11063 else if (ret == 1)
11064 flinfo->indices[symndx] = indx;
11065 else
11066 abort ();
11067 }
11068 elf_section_data (osec)->this_hdr.sh_info
11069 = flinfo->indices[symndx];
11070 }
11071 }
11072
11073 if ((o->flags & SEC_HAS_CONTENTS) == 0
11074 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11075 continue;
11076
11077 if ((o->flags & SEC_LINKER_CREATED) != 0)
11078 {
11079 /* Section was created by _bfd_elf_link_create_dynamic_sections
11080 or somesuch. */
11081 continue;
11082 }
11083
11084 /* Get the contents of the section. They have been cached by a
11085 relaxation routine. Note that o is a section in an input
11086 file, so the contents field will not have been set by any of
11087 the routines which work on output files. */
11088 if (elf_section_data (o)->this_hdr.contents != NULL)
11089 {
11090 contents = elf_section_data (o)->this_hdr.contents;
11091 if (bed->caches_rawsize
11092 && o->rawsize != 0
11093 && o->rawsize < o->size)
11094 {
11095 memcpy (flinfo->contents, contents, o->rawsize);
11096 contents = flinfo->contents;
11097 }
11098 }
11099 else
11100 {
11101 contents = flinfo->contents;
11102 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11103 return FALSE;
11104 }
11105
11106 if ((o->flags & SEC_RELOC) != 0)
11107 {
11108 Elf_Internal_Rela *internal_relocs;
11109 Elf_Internal_Rela *rel, *relend;
11110 int action_discarded;
11111 int ret;
11112
11113 /* Get the swapped relocs. */
11114 internal_relocs
11115 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11116 flinfo->internal_relocs, FALSE);
11117 if (internal_relocs == NULL
11118 && o->reloc_count > 0)
11119 return FALSE;
11120
11121 /* We need to reverse-copy input .ctors/.dtors sections if
11122 they are placed in .init_array/.finit_array for output. */
11123 if (o->size > address_size
11124 && ((strncmp (o->name, ".ctors", 6) == 0
11125 && strcmp (o->output_section->name,
11126 ".init_array") == 0)
11127 || (strncmp (o->name, ".dtors", 6) == 0
11128 && strcmp (o->output_section->name,
11129 ".fini_array") == 0))
11130 && (o->name[6] == 0 || o->name[6] == '.'))
11131 {
11132 if (o->size * bed->s->int_rels_per_ext_rel
11133 != o->reloc_count * address_size)
11134 {
11135 _bfd_error_handler
11136 /* xgettext:c-format */
11137 (_("error: %pB: size of section %pA is not "
11138 "multiple of address size"),
11139 input_bfd, o);
11140 bfd_set_error (bfd_error_bad_value);
11141 return FALSE;
11142 }
11143 o->flags |= SEC_ELF_REVERSE_COPY;
11144 }
11145
11146 action_discarded = -1;
11147 if (!elf_section_ignore_discarded_relocs (o))
11148 action_discarded = (*bed->action_discarded) (o);
11149
11150 /* Run through the relocs evaluating complex reloc symbols and
11151 looking for relocs against symbols from discarded sections
11152 or section symbols from removed link-once sections.
11153 Complain about relocs against discarded sections. Zero
11154 relocs against removed link-once sections. */
11155
11156 rel = internal_relocs;
11157 relend = rel + o->reloc_count;
11158 for ( ; rel < relend; rel++)
11159 {
11160 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11161 unsigned int s_type;
11162 asection **ps, *sec;
11163 struct elf_link_hash_entry *h = NULL;
11164 const char *sym_name;
11165
11166 if (r_symndx == STN_UNDEF)
11167 continue;
11168
11169 if (r_symndx >= locsymcount
11170 || (elf_bad_symtab (input_bfd)
11171 && flinfo->sections[r_symndx] == NULL))
11172 {
11173 h = sym_hashes[r_symndx - extsymoff];
11174
11175 /* Badly formatted input files can contain relocs that
11176 reference non-existant symbols. Check here so that
11177 we do not seg fault. */
11178 if (h == NULL)
11179 {
11180 _bfd_error_handler
11181 /* xgettext:c-format */
11182 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11183 "that references a non-existent global symbol"),
11184 input_bfd, (uint64_t) rel->r_info, o);
11185 bfd_set_error (bfd_error_bad_value);
11186 return FALSE;
11187 }
11188
11189 while (h->root.type == bfd_link_hash_indirect
11190 || h->root.type == bfd_link_hash_warning)
11191 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11192
11193 s_type = h->type;
11194
11195 /* If a plugin symbol is referenced from a non-IR file,
11196 mark the symbol as undefined. Note that the
11197 linker may attach linker created dynamic sections
11198 to the plugin bfd. Symbols defined in linker
11199 created sections are not plugin symbols. */
11200 if ((h->root.non_ir_ref_regular
11201 || h->root.non_ir_ref_dynamic)
11202 && (h->root.type == bfd_link_hash_defined
11203 || h->root.type == bfd_link_hash_defweak)
11204 && (h->root.u.def.section->flags
11205 & SEC_LINKER_CREATED) == 0
11206 && h->root.u.def.section->owner != NULL
11207 && (h->root.u.def.section->owner->flags
11208 & BFD_PLUGIN) != 0)
11209 {
11210 h->root.type = bfd_link_hash_undefined;
11211 h->root.u.undef.abfd = h->root.u.def.section->owner;
11212 }
11213
11214 ps = NULL;
11215 if (h->root.type == bfd_link_hash_defined
11216 || h->root.type == bfd_link_hash_defweak)
11217 ps = &h->root.u.def.section;
11218
11219 sym_name = h->root.root.string;
11220 }
11221 else
11222 {
11223 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11224
11225 s_type = ELF_ST_TYPE (sym->st_info);
11226 ps = &flinfo->sections[r_symndx];
11227 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11228 sym, *ps);
11229 }
11230
11231 if ((s_type == STT_RELC || s_type == STT_SRELC)
11232 && !bfd_link_relocatable (flinfo->info))
11233 {
11234 bfd_vma val;
11235 bfd_vma dot = (rel->r_offset
11236 + o->output_offset + o->output_section->vma);
11237 #ifdef DEBUG
11238 printf ("Encountered a complex symbol!");
11239 printf (" (input_bfd %s, section %s, reloc %ld\n",
11240 bfd_get_filename (input_bfd), o->name,
11241 (long) (rel - internal_relocs));
11242 printf (" symbol: idx %8.8lx, name %s\n",
11243 r_symndx, sym_name);
11244 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11245 (unsigned long) rel->r_info,
11246 (unsigned long) rel->r_offset);
11247 #endif
11248 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11249 isymbuf, locsymcount, s_type == STT_SRELC))
11250 return FALSE;
11251
11252 /* Symbol evaluated OK. Update to absolute value. */
11253 set_symbol_value (input_bfd, isymbuf, locsymcount,
11254 r_symndx, val);
11255 continue;
11256 }
11257
11258 if (action_discarded != -1 && ps != NULL)
11259 {
11260 /* Complain if the definition comes from a
11261 discarded section. */
11262 if ((sec = *ps) != NULL && discarded_section (sec))
11263 {
11264 BFD_ASSERT (r_symndx != STN_UNDEF);
11265 if (action_discarded & COMPLAIN)
11266 (*flinfo->info->callbacks->einfo)
11267 /* xgettext:c-format */
11268 (_("%X`%s' referenced in section `%pA' of %pB: "
11269 "defined in discarded section `%pA' of %pB\n"),
11270 sym_name, o, input_bfd, sec, sec->owner);
11271
11272 /* Try to do the best we can to support buggy old
11273 versions of gcc. Pretend that the symbol is
11274 really defined in the kept linkonce section.
11275 FIXME: This is quite broken. Modifying the
11276 symbol here means we will be changing all later
11277 uses of the symbol, not just in this section. */
11278 if (action_discarded & PRETEND)
11279 {
11280 asection *kept;
11281
11282 kept = _bfd_elf_check_kept_section (sec,
11283 flinfo->info);
11284 if (kept != NULL)
11285 {
11286 *ps = kept;
11287 continue;
11288 }
11289 }
11290 }
11291 }
11292 }
11293
11294 /* Relocate the section by invoking a back end routine.
11295
11296 The back end routine is responsible for adjusting the
11297 section contents as necessary, and (if using Rela relocs
11298 and generating a relocatable output file) adjusting the
11299 reloc addend as necessary.
11300
11301 The back end routine does not have to worry about setting
11302 the reloc address or the reloc symbol index.
11303
11304 The back end routine is given a pointer to the swapped in
11305 internal symbols, and can access the hash table entries
11306 for the external symbols via elf_sym_hashes (input_bfd).
11307
11308 When generating relocatable output, the back end routine
11309 must handle STB_LOCAL/STT_SECTION symbols specially. The
11310 output symbol is going to be a section symbol
11311 corresponding to the output section, which will require
11312 the addend to be adjusted. */
11313
11314 ret = (*relocate_section) (output_bfd, flinfo->info,
11315 input_bfd, o, contents,
11316 internal_relocs,
11317 isymbuf,
11318 flinfo->sections);
11319 if (!ret)
11320 return FALSE;
11321
11322 if (ret == 2
11323 || bfd_link_relocatable (flinfo->info)
11324 || flinfo->info->emitrelocations)
11325 {
11326 Elf_Internal_Rela *irela;
11327 Elf_Internal_Rela *irelaend, *irelamid;
11328 bfd_vma last_offset;
11329 struct elf_link_hash_entry **rel_hash;
11330 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11331 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11332 unsigned int next_erel;
11333 bfd_boolean rela_normal;
11334 struct bfd_elf_section_data *esdi, *esdo;
11335
11336 esdi = elf_section_data (o);
11337 esdo = elf_section_data (o->output_section);
11338 rela_normal = FALSE;
11339
11340 /* Adjust the reloc addresses and symbol indices. */
11341
11342 irela = internal_relocs;
11343 irelaend = irela + o->reloc_count;
11344 rel_hash = esdo->rel.hashes + esdo->rel.count;
11345 /* We start processing the REL relocs, if any. When we reach
11346 IRELAMID in the loop, we switch to the RELA relocs. */
11347 irelamid = irela;
11348 if (esdi->rel.hdr != NULL)
11349 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11350 * bed->s->int_rels_per_ext_rel);
11351 rel_hash_list = rel_hash;
11352 rela_hash_list = NULL;
11353 last_offset = o->output_offset;
11354 if (!bfd_link_relocatable (flinfo->info))
11355 last_offset += o->output_section->vma;
11356 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11357 {
11358 unsigned long r_symndx;
11359 asection *sec;
11360 Elf_Internal_Sym sym;
11361
11362 if (next_erel == bed->s->int_rels_per_ext_rel)
11363 {
11364 rel_hash++;
11365 next_erel = 0;
11366 }
11367
11368 if (irela == irelamid)
11369 {
11370 rel_hash = esdo->rela.hashes + esdo->rela.count;
11371 rela_hash_list = rel_hash;
11372 rela_normal = bed->rela_normal;
11373 }
11374
11375 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11376 flinfo->info, o,
11377 irela->r_offset);
11378 if (irela->r_offset >= (bfd_vma) -2)
11379 {
11380 /* This is a reloc for a deleted entry or somesuch.
11381 Turn it into an R_*_NONE reloc, at the same
11382 offset as the last reloc. elf_eh_frame.c and
11383 bfd_elf_discard_info rely on reloc offsets
11384 being ordered. */
11385 irela->r_offset = last_offset;
11386 irela->r_info = 0;
11387 irela->r_addend = 0;
11388 continue;
11389 }
11390
11391 irela->r_offset += o->output_offset;
11392
11393 /* Relocs in an executable have to be virtual addresses. */
11394 if (!bfd_link_relocatable (flinfo->info))
11395 irela->r_offset += o->output_section->vma;
11396
11397 last_offset = irela->r_offset;
11398
11399 r_symndx = irela->r_info >> r_sym_shift;
11400 if (r_symndx == STN_UNDEF)
11401 continue;
11402
11403 if (r_symndx >= locsymcount
11404 || (elf_bad_symtab (input_bfd)
11405 && flinfo->sections[r_symndx] == NULL))
11406 {
11407 struct elf_link_hash_entry *rh;
11408 unsigned long indx;
11409
11410 /* This is a reloc against a global symbol. We
11411 have not yet output all the local symbols, so
11412 we do not know the symbol index of any global
11413 symbol. We set the rel_hash entry for this
11414 reloc to point to the global hash table entry
11415 for this symbol. The symbol index is then
11416 set at the end of bfd_elf_final_link. */
11417 indx = r_symndx - extsymoff;
11418 rh = elf_sym_hashes (input_bfd)[indx];
11419 while (rh->root.type == bfd_link_hash_indirect
11420 || rh->root.type == bfd_link_hash_warning)
11421 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11422
11423 /* Setting the index to -2 tells
11424 elf_link_output_extsym that this symbol is
11425 used by a reloc. */
11426 BFD_ASSERT (rh->indx < 0);
11427 rh->indx = -2;
11428 *rel_hash = rh;
11429
11430 continue;
11431 }
11432
11433 /* This is a reloc against a local symbol. */
11434
11435 *rel_hash = NULL;
11436 sym = isymbuf[r_symndx];
11437 sec = flinfo->sections[r_symndx];
11438 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11439 {
11440 /* I suppose the backend ought to fill in the
11441 section of any STT_SECTION symbol against a
11442 processor specific section. */
11443 r_symndx = STN_UNDEF;
11444 if (bfd_is_abs_section (sec))
11445 ;
11446 else if (sec == NULL || sec->owner == NULL)
11447 {
11448 bfd_set_error (bfd_error_bad_value);
11449 return FALSE;
11450 }
11451 else
11452 {
11453 asection *osec = sec->output_section;
11454
11455 /* If we have discarded a section, the output
11456 section will be the absolute section. In
11457 case of discarded SEC_MERGE sections, use
11458 the kept section. relocate_section should
11459 have already handled discarded linkonce
11460 sections. */
11461 if (bfd_is_abs_section (osec)
11462 && sec->kept_section != NULL
11463 && sec->kept_section->output_section != NULL)
11464 {
11465 osec = sec->kept_section->output_section;
11466 irela->r_addend -= osec->vma;
11467 }
11468
11469 if (!bfd_is_abs_section (osec))
11470 {
11471 r_symndx = osec->target_index;
11472 if (r_symndx == STN_UNDEF)
11473 {
11474 irela->r_addend += osec->vma;
11475 osec = _bfd_nearby_section (output_bfd, osec,
11476 osec->vma);
11477 irela->r_addend -= osec->vma;
11478 r_symndx = osec->target_index;
11479 }
11480 }
11481 }
11482
11483 /* Adjust the addend according to where the
11484 section winds up in the output section. */
11485 if (rela_normal)
11486 irela->r_addend += sec->output_offset;
11487 }
11488 else
11489 {
11490 if (flinfo->indices[r_symndx] == -1)
11491 {
11492 unsigned long shlink;
11493 const char *name;
11494 asection *osec;
11495 long indx;
11496
11497 if (flinfo->info->strip == strip_all)
11498 {
11499 /* You can't do ld -r -s. */
11500 bfd_set_error (bfd_error_invalid_operation);
11501 return FALSE;
11502 }
11503
11504 /* This symbol was skipped earlier, but
11505 since it is needed by a reloc, we
11506 must output it now. */
11507 shlink = symtab_hdr->sh_link;
11508 name = (bfd_elf_string_from_elf_section
11509 (input_bfd, shlink, sym.st_name));
11510 if (name == NULL)
11511 return FALSE;
11512
11513 osec = sec->output_section;
11514 sym.st_shndx =
11515 _bfd_elf_section_from_bfd_section (output_bfd,
11516 osec);
11517 if (sym.st_shndx == SHN_BAD)
11518 return FALSE;
11519
11520 sym.st_value += sec->output_offset;
11521 if (!bfd_link_relocatable (flinfo->info))
11522 {
11523 sym.st_value += osec->vma;
11524 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11525 {
11526 struct elf_link_hash_table *htab
11527 = elf_hash_table (flinfo->info);
11528
11529 /* STT_TLS symbols are relative to PT_TLS
11530 segment base. */
11531 if (htab->tls_sec != NULL)
11532 sym.st_value -= htab->tls_sec->vma;
11533 else
11534 sym.st_info
11535 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11536 STT_NOTYPE);
11537 }
11538 }
11539
11540 indx = bfd_get_symcount (output_bfd);
11541 ret = elf_link_output_symstrtab (flinfo, name,
11542 &sym, sec,
11543 NULL);
11544 if (ret == 0)
11545 return FALSE;
11546 else if (ret == 1)
11547 flinfo->indices[r_symndx] = indx;
11548 else
11549 abort ();
11550 }
11551
11552 r_symndx = flinfo->indices[r_symndx];
11553 }
11554
11555 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11556 | (irela->r_info & r_type_mask));
11557 }
11558
11559 /* Swap out the relocs. */
11560 input_rel_hdr = esdi->rel.hdr;
11561 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11562 {
11563 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11564 input_rel_hdr,
11565 internal_relocs,
11566 rel_hash_list))
11567 return FALSE;
11568 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11569 * bed->s->int_rels_per_ext_rel);
11570 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11571 }
11572
11573 input_rela_hdr = esdi->rela.hdr;
11574 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11575 {
11576 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11577 input_rela_hdr,
11578 internal_relocs,
11579 rela_hash_list))
11580 return FALSE;
11581 }
11582 }
11583 }
11584
11585 /* Write out the modified section contents. */
11586 if (bed->elf_backend_write_section
11587 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11588 contents))
11589 {
11590 /* Section written out. */
11591 }
11592 else switch (o->sec_info_type)
11593 {
11594 case SEC_INFO_TYPE_STABS:
11595 if (! (_bfd_write_section_stabs
11596 (output_bfd,
11597 &elf_hash_table (flinfo->info)->stab_info,
11598 o, &elf_section_data (o)->sec_info, contents)))
11599 return FALSE;
11600 break;
11601 case SEC_INFO_TYPE_MERGE:
11602 if (! _bfd_write_merged_section (output_bfd, o,
11603 elf_section_data (o)->sec_info))
11604 return FALSE;
11605 break;
11606 case SEC_INFO_TYPE_EH_FRAME:
11607 {
11608 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11609 o, contents))
11610 return FALSE;
11611 }
11612 break;
11613 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11614 {
11615 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11616 flinfo->info,
11617 o, contents))
11618 return FALSE;
11619 }
11620 break;
11621 default:
11622 {
11623 if (! (o->flags & SEC_EXCLUDE))
11624 {
11625 file_ptr offset = (file_ptr) o->output_offset;
11626 bfd_size_type todo = o->size;
11627
11628 offset *= bfd_octets_per_byte (output_bfd, o);
11629
11630 if ((o->flags & SEC_ELF_REVERSE_COPY))
11631 {
11632 /* Reverse-copy input section to output. */
11633 do
11634 {
11635 todo -= address_size;
11636 if (! bfd_set_section_contents (output_bfd,
11637 o->output_section,
11638 contents + todo,
11639 offset,
11640 address_size))
11641 return FALSE;
11642 if (todo == 0)
11643 break;
11644 offset += address_size;
11645 }
11646 while (1);
11647 }
11648 else if (! bfd_set_section_contents (output_bfd,
11649 o->output_section,
11650 contents,
11651 offset, todo))
11652 return FALSE;
11653 }
11654 }
11655 break;
11656 }
11657 }
11658
11659 return TRUE;
11660 }
11661
11662 /* Generate a reloc when linking an ELF file. This is a reloc
11663 requested by the linker, and does not come from any input file. This
11664 is used to build constructor and destructor tables when linking
11665 with -Ur. */
11666
11667 static bfd_boolean
11668 elf_reloc_link_order (bfd *output_bfd,
11669 struct bfd_link_info *info,
11670 asection *output_section,
11671 struct bfd_link_order *link_order)
11672 {
11673 reloc_howto_type *howto;
11674 long indx;
11675 bfd_vma offset;
11676 bfd_vma addend;
11677 struct bfd_elf_section_reloc_data *reldata;
11678 struct elf_link_hash_entry **rel_hash_ptr;
11679 Elf_Internal_Shdr *rel_hdr;
11680 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11681 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11682 bfd_byte *erel;
11683 unsigned int i;
11684 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11685
11686 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11687 if (howto == NULL)
11688 {
11689 bfd_set_error (bfd_error_bad_value);
11690 return FALSE;
11691 }
11692
11693 addend = link_order->u.reloc.p->addend;
11694
11695 if (esdo->rel.hdr)
11696 reldata = &esdo->rel;
11697 else if (esdo->rela.hdr)
11698 reldata = &esdo->rela;
11699 else
11700 {
11701 reldata = NULL;
11702 BFD_ASSERT (0);
11703 }
11704
11705 /* Figure out the symbol index. */
11706 rel_hash_ptr = reldata->hashes + reldata->count;
11707 if (link_order->type == bfd_section_reloc_link_order)
11708 {
11709 indx = link_order->u.reloc.p->u.section->target_index;
11710 BFD_ASSERT (indx != 0);
11711 *rel_hash_ptr = NULL;
11712 }
11713 else
11714 {
11715 struct elf_link_hash_entry *h;
11716
11717 /* Treat a reloc against a defined symbol as though it were
11718 actually against the section. */
11719 h = ((struct elf_link_hash_entry *)
11720 bfd_wrapped_link_hash_lookup (output_bfd, info,
11721 link_order->u.reloc.p->u.name,
11722 FALSE, FALSE, TRUE));
11723 if (h != NULL
11724 && (h->root.type == bfd_link_hash_defined
11725 || h->root.type == bfd_link_hash_defweak))
11726 {
11727 asection *section;
11728
11729 section = h->root.u.def.section;
11730 indx = section->output_section->target_index;
11731 *rel_hash_ptr = NULL;
11732 /* It seems that we ought to add the symbol value to the
11733 addend here, but in practice it has already been added
11734 because it was passed to constructor_callback. */
11735 addend += section->output_section->vma + section->output_offset;
11736 }
11737 else if (h != NULL)
11738 {
11739 /* Setting the index to -2 tells elf_link_output_extsym that
11740 this symbol is used by a reloc. */
11741 h->indx = -2;
11742 *rel_hash_ptr = h;
11743 indx = 0;
11744 }
11745 else
11746 {
11747 (*info->callbacks->unattached_reloc)
11748 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11749 indx = 0;
11750 }
11751 }
11752
11753 /* If this is an inplace reloc, we must write the addend into the
11754 object file. */
11755 if (howto->partial_inplace && addend != 0)
11756 {
11757 bfd_size_type size;
11758 bfd_reloc_status_type rstat;
11759 bfd_byte *buf;
11760 bfd_boolean ok;
11761 const char *sym_name;
11762 bfd_size_type octets;
11763
11764 size = (bfd_size_type) bfd_get_reloc_size (howto);
11765 buf = (bfd_byte *) bfd_zmalloc (size);
11766 if (buf == NULL && size != 0)
11767 return FALSE;
11768 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11769 switch (rstat)
11770 {
11771 case bfd_reloc_ok:
11772 break;
11773
11774 default:
11775 case bfd_reloc_outofrange:
11776 abort ();
11777
11778 case bfd_reloc_overflow:
11779 if (link_order->type == bfd_section_reloc_link_order)
11780 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11781 else
11782 sym_name = link_order->u.reloc.p->u.name;
11783 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11784 howto->name, addend, NULL, NULL,
11785 (bfd_vma) 0);
11786 break;
11787 }
11788
11789 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11790 output_section);
11791 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11792 octets, size);
11793 free (buf);
11794 if (! ok)
11795 return FALSE;
11796 }
11797
11798 /* The address of a reloc is relative to the section in a
11799 relocatable file, and is a virtual address in an executable
11800 file. */
11801 offset = link_order->offset;
11802 if (! bfd_link_relocatable (info))
11803 offset += output_section->vma;
11804
11805 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11806 {
11807 irel[i].r_offset = offset;
11808 irel[i].r_info = 0;
11809 irel[i].r_addend = 0;
11810 }
11811 if (bed->s->arch_size == 32)
11812 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11813 else
11814 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11815
11816 rel_hdr = reldata->hdr;
11817 erel = rel_hdr->contents;
11818 if (rel_hdr->sh_type == SHT_REL)
11819 {
11820 erel += reldata->count * bed->s->sizeof_rel;
11821 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11822 }
11823 else
11824 {
11825 irel[0].r_addend = addend;
11826 erel += reldata->count * bed->s->sizeof_rela;
11827 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11828 }
11829
11830 ++reldata->count;
11831
11832 return TRUE;
11833 }
11834
11835
11836 /* Compare two sections based on the locations of the sections they are
11837 linked to. Used by elf_fixup_link_order. */
11838
11839 static int
11840 compare_link_order (const void *a, const void *b)
11841 {
11842 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11843 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11844 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11845 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11846 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11847 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11848
11849 if (apos < bpos)
11850 return -1;
11851 if (apos > bpos)
11852 return 1;
11853
11854 /* The only way we should get matching LMAs is when the first of two
11855 sections has zero size. */
11856 if (asec->size < bsec->size)
11857 return -1;
11858 if (asec->size > bsec->size)
11859 return 1;
11860
11861 /* If they are both zero size then they almost certainly have the same
11862 VMA and thus are not ordered with respect to each other. Test VMA
11863 anyway, and fall back to id to make the result reproducible across
11864 qsort implementations. */
11865 apos = asec->output_section->vma + asec->output_offset;
11866 bpos = bsec->output_section->vma + bsec->output_offset;
11867 if (apos < bpos)
11868 return -1;
11869 if (apos > bpos)
11870 return 1;
11871
11872 return asec->id - bsec->id;
11873 }
11874
11875
11876 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11877 order as their linked sections. Returns false if this could not be done
11878 because an output section includes both ordered and unordered
11879 sections. Ideally we'd do this in the linker proper. */
11880
11881 static bfd_boolean
11882 elf_fixup_link_order (bfd *abfd, asection *o)
11883 {
11884 size_t seen_linkorder;
11885 size_t seen_other;
11886 size_t n;
11887 struct bfd_link_order *p;
11888 bfd *sub;
11889 struct bfd_link_order **sections;
11890 asection *other_sec, *linkorder_sec;
11891 bfd_vma offset; /* Octets. */
11892
11893 other_sec = NULL;
11894 linkorder_sec = NULL;
11895 seen_other = 0;
11896 seen_linkorder = 0;
11897 for (p = o->map_head.link_order; p != NULL; p = p->next)
11898 {
11899 if (p->type == bfd_indirect_link_order)
11900 {
11901 asection *s = p->u.indirect.section;
11902 sub = s->owner;
11903 if ((s->flags & SEC_LINKER_CREATED) == 0
11904 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11905 && elf_section_data (s) != NULL
11906 && elf_linked_to_section (s) != NULL)
11907 {
11908 seen_linkorder++;
11909 linkorder_sec = s;
11910 }
11911 else
11912 {
11913 seen_other++;
11914 other_sec = s;
11915 }
11916 }
11917 else
11918 seen_other++;
11919
11920 if (seen_other && seen_linkorder)
11921 {
11922 if (other_sec && linkorder_sec)
11923 _bfd_error_handler
11924 /* xgettext:c-format */
11925 (_("%pA has both ordered [`%pA' in %pB] "
11926 "and unordered [`%pA' in %pB] sections"),
11927 o, linkorder_sec, linkorder_sec->owner,
11928 other_sec, other_sec->owner);
11929 else
11930 _bfd_error_handler
11931 (_("%pA has both ordered and unordered sections"), o);
11932 bfd_set_error (bfd_error_bad_value);
11933 return FALSE;
11934 }
11935 }
11936
11937 if (!seen_linkorder)
11938 return TRUE;
11939
11940 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11941 if (sections == NULL)
11942 return FALSE;
11943
11944 seen_linkorder = 0;
11945 for (p = o->map_head.link_order; p != NULL; p = p->next)
11946 sections[seen_linkorder++] = p;
11947
11948 /* Sort the input sections in the order of their linked section. */
11949 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11950
11951 /* Change the offsets of the sections. */
11952 offset = 0;
11953 for (n = 0; n < seen_linkorder; n++)
11954 {
11955 bfd_vma mask;
11956 asection *s = sections[n]->u.indirect.section;
11957 unsigned int opb = bfd_octets_per_byte (abfd, s);
11958
11959 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11960 offset = (offset + ~mask) & mask;
11961 sections[n]->offset = s->output_offset = offset / opb;
11962 offset += sections[n]->size;
11963 }
11964
11965 free (sections);
11966 return TRUE;
11967 }
11968
11969 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11970 Returns TRUE upon success, FALSE otherwise. */
11971
11972 static bfd_boolean
11973 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11974 {
11975 bfd_boolean ret = FALSE;
11976 bfd *implib_bfd;
11977 const struct elf_backend_data *bed;
11978 flagword flags;
11979 enum bfd_architecture arch;
11980 unsigned int mach;
11981 asymbol **sympp = NULL;
11982 long symsize;
11983 long symcount;
11984 long src_count;
11985 elf_symbol_type *osymbuf;
11986 size_t amt;
11987
11988 implib_bfd = info->out_implib_bfd;
11989 bed = get_elf_backend_data (abfd);
11990
11991 if (!bfd_set_format (implib_bfd, bfd_object))
11992 return FALSE;
11993
11994 /* Use flag from executable but make it a relocatable object. */
11995 flags = bfd_get_file_flags (abfd);
11996 flags &= ~HAS_RELOC;
11997 if (!bfd_set_start_address (implib_bfd, 0)
11998 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11999 return FALSE;
12000
12001 /* Copy architecture of output file to import library file. */
12002 arch = bfd_get_arch (abfd);
12003 mach = bfd_get_mach (abfd);
12004 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12005 && (abfd->target_defaulted
12006 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12007 return FALSE;
12008
12009 /* Get symbol table size. */
12010 symsize = bfd_get_symtab_upper_bound (abfd);
12011 if (symsize < 0)
12012 return FALSE;
12013
12014 /* Read in the symbol table. */
12015 sympp = (asymbol **) bfd_malloc (symsize);
12016 if (sympp == NULL)
12017 return FALSE;
12018
12019 symcount = bfd_canonicalize_symtab (abfd, sympp);
12020 if (symcount < 0)
12021 goto free_sym_buf;
12022
12023 /* Allow the BFD backend to copy any private header data it
12024 understands from the output BFD to the import library BFD. */
12025 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12026 goto free_sym_buf;
12027
12028 /* Filter symbols to appear in the import library. */
12029 if (bed->elf_backend_filter_implib_symbols)
12030 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12031 symcount);
12032 else
12033 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12034 if (symcount == 0)
12035 {
12036 bfd_set_error (bfd_error_no_symbols);
12037 _bfd_error_handler (_("%pB: no symbol found for import library"),
12038 implib_bfd);
12039 goto free_sym_buf;
12040 }
12041
12042
12043 /* Make symbols absolute. */
12044 amt = symcount * sizeof (*osymbuf);
12045 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12046 if (osymbuf == NULL)
12047 goto free_sym_buf;
12048
12049 for (src_count = 0; src_count < symcount; src_count++)
12050 {
12051 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12052 sizeof (*osymbuf));
12053 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12054 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12055 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12056 osymbuf[src_count].internal_elf_sym.st_value =
12057 osymbuf[src_count].symbol.value;
12058 sympp[src_count] = &osymbuf[src_count].symbol;
12059 }
12060
12061 bfd_set_symtab (implib_bfd, sympp, symcount);
12062
12063 /* Allow the BFD backend to copy any private data it understands
12064 from the output BFD to the import library BFD. This is done last
12065 to permit the routine to look at the filtered symbol table. */
12066 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12067 goto free_sym_buf;
12068
12069 if (!bfd_close (implib_bfd))
12070 goto free_sym_buf;
12071
12072 ret = TRUE;
12073
12074 free_sym_buf:
12075 free (sympp);
12076 return ret;
12077 }
12078
12079 static void
12080 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12081 {
12082 asection *o;
12083
12084 if (flinfo->symstrtab != NULL)
12085 _bfd_elf_strtab_free (flinfo->symstrtab);
12086 free (flinfo->contents);
12087 free (flinfo->external_relocs);
12088 free (flinfo->internal_relocs);
12089 free (flinfo->external_syms);
12090 free (flinfo->locsym_shndx);
12091 free (flinfo->internal_syms);
12092 free (flinfo->indices);
12093 free (flinfo->sections);
12094 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12095 free (flinfo->symshndxbuf);
12096 for (o = obfd->sections; o != NULL; o = o->next)
12097 {
12098 struct bfd_elf_section_data *esdo = elf_section_data (o);
12099 free (esdo->rel.hashes);
12100 free (esdo->rela.hashes);
12101 }
12102 }
12103
12104 /* Do the final step of an ELF link. */
12105
12106 bfd_boolean
12107 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12108 {
12109 bfd_boolean dynamic;
12110 bfd_boolean emit_relocs;
12111 bfd *dynobj;
12112 struct elf_final_link_info flinfo;
12113 asection *o;
12114 struct bfd_link_order *p;
12115 bfd *sub;
12116 bfd_size_type max_contents_size;
12117 bfd_size_type max_external_reloc_size;
12118 bfd_size_type max_internal_reloc_count;
12119 bfd_size_type max_sym_count;
12120 bfd_size_type max_sym_shndx_count;
12121 Elf_Internal_Sym elfsym;
12122 unsigned int i;
12123 Elf_Internal_Shdr *symtab_hdr;
12124 Elf_Internal_Shdr *symtab_shndx_hdr;
12125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12126 struct elf_outext_info eoinfo;
12127 bfd_boolean merged;
12128 size_t relativecount = 0;
12129 asection *reldyn = 0;
12130 bfd_size_type amt;
12131 asection *attr_section = NULL;
12132 bfd_vma attr_size = 0;
12133 const char *std_attrs_section;
12134 struct elf_link_hash_table *htab = elf_hash_table (info);
12135 bfd_boolean sections_removed;
12136 bfd_boolean ret;
12137
12138 if (!is_elf_hash_table (htab))
12139 return FALSE;
12140
12141 if (bfd_link_pic (info))
12142 abfd->flags |= DYNAMIC;
12143
12144 dynamic = htab->dynamic_sections_created;
12145 dynobj = htab->dynobj;
12146
12147 emit_relocs = (bfd_link_relocatable (info)
12148 || info->emitrelocations);
12149
12150 memset (&flinfo, 0, sizeof (flinfo));
12151 flinfo.info = info;
12152 flinfo.output_bfd = abfd;
12153 flinfo.symstrtab = _bfd_elf_strtab_init ();
12154 if (flinfo.symstrtab == NULL)
12155 return FALSE;
12156
12157 if (! dynamic)
12158 {
12159 flinfo.hash_sec = NULL;
12160 flinfo.symver_sec = NULL;
12161 }
12162 else
12163 {
12164 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12165 /* Note that dynsym_sec can be NULL (on VMS). */
12166 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12167 /* Note that it is OK if symver_sec is NULL. */
12168 }
12169
12170 if (info->unique_symbol
12171 && !bfd_hash_table_init (&flinfo.local_hash_table,
12172 local_hash_newfunc,
12173 sizeof (struct local_hash_entry)))
12174 return FALSE;
12175
12176 /* The object attributes have been merged. Remove the input
12177 sections from the link, and set the contents of the output
12178 section. */
12179 sections_removed = FALSE;
12180 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12181 for (o = abfd->sections; o != NULL; o = o->next)
12182 {
12183 bfd_boolean remove_section = FALSE;
12184
12185 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12186 || strcmp (o->name, ".gnu.attributes") == 0)
12187 {
12188 for (p = o->map_head.link_order; p != NULL; p = p->next)
12189 {
12190 asection *input_section;
12191
12192 if (p->type != bfd_indirect_link_order)
12193 continue;
12194 input_section = p->u.indirect.section;
12195 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12196 elf_link_input_bfd ignores this section. */
12197 input_section->flags &= ~SEC_HAS_CONTENTS;
12198 }
12199
12200 attr_size = bfd_elf_obj_attr_size (abfd);
12201 bfd_set_section_size (o, attr_size);
12202 /* Skip this section later on. */
12203 o->map_head.link_order = NULL;
12204 if (attr_size)
12205 attr_section = o;
12206 else
12207 remove_section = TRUE;
12208 }
12209 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12210 {
12211 /* Remove empty group section from linker output. */
12212 remove_section = TRUE;
12213 }
12214 if (remove_section)
12215 {
12216 o->flags |= SEC_EXCLUDE;
12217 bfd_section_list_remove (abfd, o);
12218 abfd->section_count--;
12219 sections_removed = TRUE;
12220 }
12221 }
12222 if (sections_removed)
12223 _bfd_fix_excluded_sec_syms (abfd, info);
12224
12225 /* Count up the number of relocations we will output for each output
12226 section, so that we know the sizes of the reloc sections. We
12227 also figure out some maximum sizes. */
12228 max_contents_size = 0;
12229 max_external_reloc_size = 0;
12230 max_internal_reloc_count = 0;
12231 max_sym_count = 0;
12232 max_sym_shndx_count = 0;
12233 merged = FALSE;
12234 for (o = abfd->sections; o != NULL; o = o->next)
12235 {
12236 struct bfd_elf_section_data *esdo = elf_section_data (o);
12237 o->reloc_count = 0;
12238
12239 for (p = o->map_head.link_order; p != NULL; p = p->next)
12240 {
12241 unsigned int reloc_count = 0;
12242 unsigned int additional_reloc_count = 0;
12243 struct bfd_elf_section_data *esdi = NULL;
12244
12245 if (p->type == bfd_section_reloc_link_order
12246 || p->type == bfd_symbol_reloc_link_order)
12247 reloc_count = 1;
12248 else if (p->type == bfd_indirect_link_order)
12249 {
12250 asection *sec;
12251
12252 sec = p->u.indirect.section;
12253
12254 /* Mark all sections which are to be included in the
12255 link. This will normally be every section. We need
12256 to do this so that we can identify any sections which
12257 the linker has decided to not include. */
12258 sec->linker_mark = TRUE;
12259
12260 if (sec->flags & SEC_MERGE)
12261 merged = TRUE;
12262
12263 if (sec->rawsize > max_contents_size)
12264 max_contents_size = sec->rawsize;
12265 if (sec->size > max_contents_size)
12266 max_contents_size = sec->size;
12267
12268 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12269 && (sec->owner->flags & DYNAMIC) == 0)
12270 {
12271 size_t sym_count;
12272
12273 /* We are interested in just local symbols, not all
12274 symbols. */
12275 if (elf_bad_symtab (sec->owner))
12276 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12277 / bed->s->sizeof_sym);
12278 else
12279 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12280
12281 if (sym_count > max_sym_count)
12282 max_sym_count = sym_count;
12283
12284 if (sym_count > max_sym_shndx_count
12285 && elf_symtab_shndx_list (sec->owner) != NULL)
12286 max_sym_shndx_count = sym_count;
12287
12288 if (esdo->this_hdr.sh_type == SHT_REL
12289 || esdo->this_hdr.sh_type == SHT_RELA)
12290 /* Some backends use reloc_count in relocation sections
12291 to count particular types of relocs. Of course,
12292 reloc sections themselves can't have relocations. */
12293 ;
12294 else if (emit_relocs)
12295 {
12296 reloc_count = sec->reloc_count;
12297 if (bed->elf_backend_count_additional_relocs)
12298 {
12299 int c;
12300 c = (*bed->elf_backend_count_additional_relocs) (sec);
12301 additional_reloc_count += c;
12302 }
12303 }
12304 else if (bed->elf_backend_count_relocs)
12305 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12306
12307 esdi = elf_section_data (sec);
12308
12309 if ((sec->flags & SEC_RELOC) != 0)
12310 {
12311 size_t ext_size = 0;
12312
12313 if (esdi->rel.hdr != NULL)
12314 ext_size = esdi->rel.hdr->sh_size;
12315 if (esdi->rela.hdr != NULL)
12316 ext_size += esdi->rela.hdr->sh_size;
12317
12318 if (ext_size > max_external_reloc_size)
12319 max_external_reloc_size = ext_size;
12320 if (sec->reloc_count > max_internal_reloc_count)
12321 max_internal_reloc_count = sec->reloc_count;
12322 }
12323 }
12324 }
12325
12326 if (reloc_count == 0)
12327 continue;
12328
12329 reloc_count += additional_reloc_count;
12330 o->reloc_count += reloc_count;
12331
12332 if (p->type == bfd_indirect_link_order && emit_relocs)
12333 {
12334 if (esdi->rel.hdr)
12335 {
12336 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12337 esdo->rel.count += additional_reloc_count;
12338 }
12339 if (esdi->rela.hdr)
12340 {
12341 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12342 esdo->rela.count += additional_reloc_count;
12343 }
12344 }
12345 else
12346 {
12347 if (o->use_rela_p)
12348 esdo->rela.count += reloc_count;
12349 else
12350 esdo->rel.count += reloc_count;
12351 }
12352 }
12353
12354 if (o->reloc_count > 0)
12355 o->flags |= SEC_RELOC;
12356 else
12357 {
12358 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12359 set it (this is probably a bug) and if it is set
12360 assign_section_numbers will create a reloc section. */
12361 o->flags &=~ SEC_RELOC;
12362 }
12363
12364 /* If the SEC_ALLOC flag is not set, force the section VMA to
12365 zero. This is done in elf_fake_sections as well, but forcing
12366 the VMA to 0 here will ensure that relocs against these
12367 sections are handled correctly. */
12368 if ((o->flags & SEC_ALLOC) == 0
12369 && ! o->user_set_vma)
12370 o->vma = 0;
12371 }
12372
12373 if (! bfd_link_relocatable (info) && merged)
12374 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12375
12376 /* Figure out the file positions for everything but the symbol table
12377 and the relocs. We set symcount to force assign_section_numbers
12378 to create a symbol table. */
12379 abfd->symcount = info->strip != strip_all || emit_relocs;
12380 BFD_ASSERT (! abfd->output_has_begun);
12381 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12382 goto error_return;
12383
12384 /* Set sizes, and assign file positions for reloc sections. */
12385 for (o = abfd->sections; o != NULL; o = o->next)
12386 {
12387 struct bfd_elf_section_data *esdo = elf_section_data (o);
12388 if ((o->flags & SEC_RELOC) != 0)
12389 {
12390 if (esdo->rel.hdr
12391 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12392 goto error_return;
12393
12394 if (esdo->rela.hdr
12395 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12396 goto error_return;
12397 }
12398
12399 /* _bfd_elf_compute_section_file_positions makes temporary use
12400 of target_index. Reset it. */
12401 o->target_index = 0;
12402
12403 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12404 to count upwards while actually outputting the relocations. */
12405 esdo->rel.count = 0;
12406 esdo->rela.count = 0;
12407
12408 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12409 && !bfd_section_is_ctf (o))
12410 {
12411 /* Cache the section contents so that they can be compressed
12412 later. Use bfd_malloc since it will be freed by
12413 bfd_compress_section_contents. */
12414 unsigned char *contents = esdo->this_hdr.contents;
12415 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12416 abort ();
12417 contents
12418 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12419 if (contents == NULL)
12420 goto error_return;
12421 esdo->this_hdr.contents = contents;
12422 }
12423 }
12424
12425 /* We have now assigned file positions for all the sections except .symtab,
12426 .strtab, and non-loaded reloc and compressed debugging sections. We start
12427 the .symtab section at the current file position, and write directly to it.
12428 We build the .strtab section in memory. */
12429 abfd->symcount = 0;
12430 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12431 /* sh_name is set in prep_headers. */
12432 symtab_hdr->sh_type = SHT_SYMTAB;
12433 /* sh_flags, sh_addr and sh_size all start off zero. */
12434 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12435 /* sh_link is set in assign_section_numbers. */
12436 /* sh_info is set below. */
12437 /* sh_offset is set just below. */
12438 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12439
12440 if (max_sym_count < 20)
12441 max_sym_count = 20;
12442 htab->strtabsize = max_sym_count;
12443 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12444 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12445 if (htab->strtab == NULL)
12446 goto error_return;
12447 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12448 flinfo.symshndxbuf
12449 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12450 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12451
12452 if (info->strip != strip_all || emit_relocs)
12453 {
12454 bfd_boolean name_local_sections;
12455 const char *name;
12456
12457 file_ptr off = elf_next_file_pos (abfd);
12458
12459 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12460
12461 /* Note that at this point elf_next_file_pos (abfd) is
12462 incorrect. We do not yet know the size of the .symtab section.
12463 We correct next_file_pos below, after we do know the size. */
12464
12465 /* Start writing out the symbol table. The first symbol is always a
12466 dummy symbol. */
12467 elfsym.st_value = 0;
12468 elfsym.st_size = 0;
12469 elfsym.st_info = 0;
12470 elfsym.st_other = 0;
12471 elfsym.st_shndx = SHN_UNDEF;
12472 elfsym.st_target_internal = 0;
12473 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12474 bfd_und_section_ptr, NULL) != 1)
12475 goto error_return;
12476
12477 /* Output a symbol for each section. We output these even if we are
12478 discarding local symbols, since they are used for relocs. These
12479 symbols usually have no names. We store the index of each one in
12480 the index field of the section, so that we can find it again when
12481 outputting relocs. */
12482
12483 name_local_sections
12484 = (bed->elf_backend_name_local_section_symbols
12485 && bed->elf_backend_name_local_section_symbols (abfd));
12486
12487 name = NULL;
12488 elfsym.st_size = 0;
12489 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12490 elfsym.st_other = 0;
12491 elfsym.st_value = 0;
12492 elfsym.st_target_internal = 0;
12493 for (i = 1; i < elf_numsections (abfd); i++)
12494 {
12495 o = bfd_section_from_elf_index (abfd, i);
12496 if (o != NULL)
12497 {
12498 o->target_index = bfd_get_symcount (abfd);
12499 elfsym.st_shndx = i;
12500 if (!bfd_link_relocatable (info))
12501 elfsym.st_value = o->vma;
12502 if (name_local_sections)
12503 name = o->name;
12504 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12505 NULL) != 1)
12506 goto error_return;
12507 }
12508 }
12509 }
12510
12511 /* On some targets like Irix 5 the symbol split between local and global
12512 ones recorded in the sh_info field needs to be done between section
12513 and all other symbols. */
12514 if (bed->elf_backend_elfsym_local_is_section
12515 && bed->elf_backend_elfsym_local_is_section (abfd))
12516 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12517
12518 /* Allocate some memory to hold information read in from the input
12519 files. */
12520 if (max_contents_size != 0)
12521 {
12522 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12523 if (flinfo.contents == NULL)
12524 goto error_return;
12525 }
12526
12527 if (max_external_reloc_size != 0)
12528 {
12529 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12530 if (flinfo.external_relocs == NULL)
12531 goto error_return;
12532 }
12533
12534 if (max_internal_reloc_count != 0)
12535 {
12536 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12537 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12538 if (flinfo.internal_relocs == NULL)
12539 goto error_return;
12540 }
12541
12542 if (max_sym_count != 0)
12543 {
12544 amt = max_sym_count * bed->s->sizeof_sym;
12545 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12546 if (flinfo.external_syms == NULL)
12547 goto error_return;
12548
12549 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12550 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12551 if (flinfo.internal_syms == NULL)
12552 goto error_return;
12553
12554 amt = max_sym_count * sizeof (long);
12555 flinfo.indices = (long int *) bfd_malloc (amt);
12556 if (flinfo.indices == NULL)
12557 goto error_return;
12558
12559 amt = max_sym_count * sizeof (asection *);
12560 flinfo.sections = (asection **) bfd_malloc (amt);
12561 if (flinfo.sections == NULL)
12562 goto error_return;
12563 }
12564
12565 if (max_sym_shndx_count != 0)
12566 {
12567 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12568 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12569 if (flinfo.locsym_shndx == NULL)
12570 goto error_return;
12571 }
12572
12573 if (htab->tls_sec)
12574 {
12575 bfd_vma base, end = 0; /* Both bytes. */
12576 asection *sec;
12577
12578 for (sec = htab->tls_sec;
12579 sec && (sec->flags & SEC_THREAD_LOCAL);
12580 sec = sec->next)
12581 {
12582 bfd_size_type size = sec->size;
12583 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12584
12585 if (size == 0
12586 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12587 {
12588 struct bfd_link_order *ord = sec->map_tail.link_order;
12589
12590 if (ord != NULL)
12591 size = ord->offset * opb + ord->size;
12592 }
12593 end = sec->vma + size / opb;
12594 }
12595 base = htab->tls_sec->vma;
12596 /* Only align end of TLS section if static TLS doesn't have special
12597 alignment requirements. */
12598 if (bed->static_tls_alignment == 1)
12599 end = align_power (end, htab->tls_sec->alignment_power);
12600 htab->tls_size = end - base;
12601 }
12602
12603 /* Reorder SHF_LINK_ORDER sections. */
12604 for (o = abfd->sections; o != NULL; o = o->next)
12605 {
12606 if (!elf_fixup_link_order (abfd, o))
12607 return FALSE;
12608 }
12609
12610 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12611 return FALSE;
12612
12613 /* Since ELF permits relocations to be against local symbols, we
12614 must have the local symbols available when we do the relocations.
12615 Since we would rather only read the local symbols once, and we
12616 would rather not keep them in memory, we handle all the
12617 relocations for a single input file at the same time.
12618
12619 Unfortunately, there is no way to know the total number of local
12620 symbols until we have seen all of them, and the local symbol
12621 indices precede the global symbol indices. This means that when
12622 we are generating relocatable output, and we see a reloc against
12623 a global symbol, we can not know the symbol index until we have
12624 finished examining all the local symbols to see which ones we are
12625 going to output. To deal with this, we keep the relocations in
12626 memory, and don't output them until the end of the link. This is
12627 an unfortunate waste of memory, but I don't see a good way around
12628 it. Fortunately, it only happens when performing a relocatable
12629 link, which is not the common case. FIXME: If keep_memory is set
12630 we could write the relocs out and then read them again; I don't
12631 know how bad the memory loss will be. */
12632
12633 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12634 sub->output_has_begun = FALSE;
12635 for (o = abfd->sections; o != NULL; o = o->next)
12636 {
12637 for (p = o->map_head.link_order; p != NULL; p = p->next)
12638 {
12639 if (p->type == bfd_indirect_link_order
12640 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12641 == bfd_target_elf_flavour)
12642 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12643 {
12644 if (! sub->output_has_begun)
12645 {
12646 if (! elf_link_input_bfd (&flinfo, sub))
12647 goto error_return;
12648 sub->output_has_begun = TRUE;
12649 }
12650 }
12651 else if (p->type == bfd_section_reloc_link_order
12652 || p->type == bfd_symbol_reloc_link_order)
12653 {
12654 if (! elf_reloc_link_order (abfd, info, o, p))
12655 goto error_return;
12656 }
12657 else
12658 {
12659 if (! _bfd_default_link_order (abfd, info, o, p))
12660 {
12661 if (p->type == bfd_indirect_link_order
12662 && (bfd_get_flavour (sub)
12663 == bfd_target_elf_flavour)
12664 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12665 != bed->s->elfclass))
12666 {
12667 const char *iclass, *oclass;
12668
12669 switch (bed->s->elfclass)
12670 {
12671 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12672 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12673 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12674 default: abort ();
12675 }
12676
12677 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12678 {
12679 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12680 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12681 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12682 default: abort ();
12683 }
12684
12685 bfd_set_error (bfd_error_wrong_format);
12686 _bfd_error_handler
12687 /* xgettext:c-format */
12688 (_("%pB: file class %s incompatible with %s"),
12689 sub, iclass, oclass);
12690 }
12691
12692 goto error_return;
12693 }
12694 }
12695 }
12696 }
12697
12698 /* Free symbol buffer if needed. */
12699 if (!info->reduce_memory_overheads)
12700 {
12701 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12702 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12703 {
12704 free (elf_tdata (sub)->symbuf);
12705 elf_tdata (sub)->symbuf = NULL;
12706 }
12707 }
12708
12709 ret = TRUE;
12710
12711 /* Output any global symbols that got converted to local in a
12712 version script or due to symbol visibility. We do this in a
12713 separate step since ELF requires all local symbols to appear
12714 prior to any global symbols. FIXME: We should only do this if
12715 some global symbols were, in fact, converted to become local.
12716 FIXME: Will this work correctly with the Irix 5 linker? */
12717 eoinfo.failed = FALSE;
12718 eoinfo.flinfo = &flinfo;
12719 eoinfo.localsyms = TRUE;
12720 eoinfo.file_sym_done = FALSE;
12721 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12722 if (eoinfo.failed)
12723 {
12724 ret = FALSE;
12725 goto return_local_hash_table;
12726 }
12727
12728 /* If backend needs to output some local symbols not present in the hash
12729 table, do it now. */
12730 if (bed->elf_backend_output_arch_local_syms
12731 && (info->strip != strip_all || emit_relocs))
12732 {
12733 typedef int (*out_sym_func)
12734 (void *, const char *, Elf_Internal_Sym *, asection *,
12735 struct elf_link_hash_entry *);
12736
12737 if (! ((*bed->elf_backend_output_arch_local_syms)
12738 (abfd, info, &flinfo,
12739 (out_sym_func) elf_link_output_symstrtab)))
12740 {
12741 ret = FALSE;
12742 goto return_local_hash_table;
12743 }
12744 }
12745
12746 /* That wrote out all the local symbols. Finish up the symbol table
12747 with the global symbols. Even if we want to strip everything we
12748 can, we still need to deal with those global symbols that got
12749 converted to local in a version script. */
12750
12751 /* The sh_info field records the index of the first non local symbol. */
12752 if (!symtab_hdr->sh_info)
12753 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12754
12755 if (dynamic
12756 && htab->dynsym != NULL
12757 && htab->dynsym->output_section != bfd_abs_section_ptr)
12758 {
12759 Elf_Internal_Sym sym;
12760 bfd_byte *dynsym = htab->dynsym->contents;
12761
12762 o = htab->dynsym->output_section;
12763 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12764
12765 /* Write out the section symbols for the output sections. */
12766 if (bfd_link_pic (info)
12767 || htab->is_relocatable_executable)
12768 {
12769 asection *s;
12770
12771 sym.st_size = 0;
12772 sym.st_name = 0;
12773 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12774 sym.st_other = 0;
12775 sym.st_target_internal = 0;
12776
12777 for (s = abfd->sections; s != NULL; s = s->next)
12778 {
12779 int indx;
12780 bfd_byte *dest;
12781 long dynindx;
12782
12783 dynindx = elf_section_data (s)->dynindx;
12784 if (dynindx <= 0)
12785 continue;
12786 indx = elf_section_data (s)->this_idx;
12787 BFD_ASSERT (indx > 0);
12788 sym.st_shndx = indx;
12789 if (! check_dynsym (abfd, &sym))
12790 {
12791 ret = FALSE;
12792 goto return_local_hash_table;
12793 }
12794 sym.st_value = s->vma;
12795 dest = dynsym + dynindx * bed->s->sizeof_sym;
12796
12797 /* Inform the linker of the addition of this symbol. */
12798
12799 if (info->callbacks->ctf_new_dynsym)
12800 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12801
12802 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12803 }
12804 }
12805
12806 /* Write out the local dynsyms. */
12807 if (htab->dynlocal)
12808 {
12809 struct elf_link_local_dynamic_entry *e;
12810 for (e = htab->dynlocal; e ; e = e->next)
12811 {
12812 asection *s;
12813 bfd_byte *dest;
12814
12815 /* Copy the internal symbol and turn off visibility.
12816 Note that we saved a word of storage and overwrote
12817 the original st_name with the dynstr_index. */
12818 sym = e->isym;
12819 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12820 sym.st_shndx = SHN_UNDEF;
12821
12822 s = bfd_section_from_elf_index (e->input_bfd,
12823 e->isym.st_shndx);
12824 if (s != NULL
12825 && s->output_section != NULL
12826 && elf_section_data (s->output_section) != NULL)
12827 {
12828 sym.st_shndx =
12829 elf_section_data (s->output_section)->this_idx;
12830 if (! check_dynsym (abfd, &sym))
12831 {
12832 ret = FALSE;
12833 goto return_local_hash_table;
12834 }
12835 sym.st_value = (s->output_section->vma
12836 + s->output_offset
12837 + e->isym.st_value);
12838 }
12839
12840 /* Inform the linker of the addition of this symbol. */
12841
12842 if (info->callbacks->ctf_new_dynsym)
12843 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12844
12845 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12846 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12847 }
12848 }
12849 }
12850
12851 /* We get the global symbols from the hash table. */
12852 eoinfo.failed = FALSE;
12853 eoinfo.localsyms = FALSE;
12854 eoinfo.flinfo = &flinfo;
12855 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12856 if (eoinfo.failed)
12857 {
12858 ret = FALSE;
12859 goto return_local_hash_table;
12860 }
12861
12862 /* If backend needs to output some symbols not present in the hash
12863 table, do it now. */
12864 if (bed->elf_backend_output_arch_syms
12865 && (info->strip != strip_all || emit_relocs))
12866 {
12867 typedef int (*out_sym_func)
12868 (void *, const char *, Elf_Internal_Sym *, asection *,
12869 struct elf_link_hash_entry *);
12870
12871 if (! ((*bed->elf_backend_output_arch_syms)
12872 (abfd, info, &flinfo,
12873 (out_sym_func) elf_link_output_symstrtab)))
12874 {
12875 ret = FALSE;
12876 goto return_local_hash_table;
12877 }
12878 }
12879
12880 /* Finalize the .strtab section. */
12881 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12882
12883 /* Swap out the .strtab section. */
12884 if (!elf_link_swap_symbols_out (&flinfo))
12885 {
12886 ret = FALSE;
12887 goto return_local_hash_table;
12888 }
12889
12890 /* Now we know the size of the symtab section. */
12891 if (bfd_get_symcount (abfd) > 0)
12892 {
12893 /* Finish up and write out the symbol string table (.strtab)
12894 section. */
12895 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12896 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12897
12898 if (elf_symtab_shndx_list (abfd))
12899 {
12900 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12901
12902 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12903 {
12904 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12905 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12906 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12907 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12908 symtab_shndx_hdr->sh_size = amt;
12909
12910 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12911 off, TRUE);
12912
12913 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12914 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12915 {
12916 ret = FALSE;
12917 goto return_local_hash_table;
12918 }
12919 }
12920 }
12921
12922 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12923 /* sh_name was set in prep_headers. */
12924 symstrtab_hdr->sh_type = SHT_STRTAB;
12925 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12926 symstrtab_hdr->sh_addr = 0;
12927 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12928 symstrtab_hdr->sh_entsize = 0;
12929 symstrtab_hdr->sh_link = 0;
12930 symstrtab_hdr->sh_info = 0;
12931 /* sh_offset is set just below. */
12932 symstrtab_hdr->sh_addralign = 1;
12933
12934 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12935 off, TRUE);
12936 elf_next_file_pos (abfd) = off;
12937
12938 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12939 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12940 {
12941 ret = FALSE;
12942 goto return_local_hash_table;
12943 }
12944 }
12945
12946 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12947 {
12948 _bfd_error_handler (_("%pB: failed to generate import library"),
12949 info->out_implib_bfd);
12950 ret = FALSE;
12951 goto return_local_hash_table;
12952 }
12953
12954 /* Adjust the relocs to have the correct symbol indices. */
12955 for (o = abfd->sections; o != NULL; o = o->next)
12956 {
12957 struct bfd_elf_section_data *esdo = elf_section_data (o);
12958 bfd_boolean sort;
12959
12960 if ((o->flags & SEC_RELOC) == 0)
12961 continue;
12962
12963 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12964 if (esdo->rel.hdr != NULL
12965 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12966 {
12967 ret = FALSE;
12968 goto return_local_hash_table;
12969 }
12970 if (esdo->rela.hdr != NULL
12971 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12972 {
12973 ret = FALSE;
12974 goto return_local_hash_table;
12975 }
12976
12977 /* Set the reloc_count field to 0 to prevent write_relocs from
12978 trying to swap the relocs out itself. */
12979 o->reloc_count = 0;
12980 }
12981
12982 if (dynamic && info->combreloc && dynobj != NULL)
12983 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12984
12985 /* If we are linking against a dynamic object, or generating a
12986 shared library, finish up the dynamic linking information. */
12987 if (dynamic)
12988 {
12989 bfd_byte *dyncon, *dynconend;
12990
12991 /* Fix up .dynamic entries. */
12992 o = bfd_get_linker_section (dynobj, ".dynamic");
12993 BFD_ASSERT (o != NULL);
12994
12995 dyncon = o->contents;
12996 dynconend = o->contents + o->size;
12997 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12998 {
12999 Elf_Internal_Dyn dyn;
13000 const char *name;
13001 unsigned int type;
13002 bfd_size_type sh_size;
13003 bfd_vma sh_addr;
13004
13005 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13006
13007 switch (dyn.d_tag)
13008 {
13009 default:
13010 continue;
13011 case DT_NULL:
13012 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13013 {
13014 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13015 {
13016 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13017 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13018 default: continue;
13019 }
13020 dyn.d_un.d_val = relativecount;
13021 relativecount = 0;
13022 break;
13023 }
13024 continue;
13025
13026 case DT_INIT:
13027 name = info->init_function;
13028 goto get_sym;
13029 case DT_FINI:
13030 name = info->fini_function;
13031 get_sym:
13032 {
13033 struct elf_link_hash_entry *h;
13034
13035 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
13036 if (h != NULL
13037 && (h->root.type == bfd_link_hash_defined
13038 || h->root.type == bfd_link_hash_defweak))
13039 {
13040 dyn.d_un.d_ptr = h->root.u.def.value;
13041 o = h->root.u.def.section;
13042 if (o->output_section != NULL)
13043 dyn.d_un.d_ptr += (o->output_section->vma
13044 + o->output_offset);
13045 else
13046 {
13047 /* The symbol is imported from another shared
13048 library and does not apply to this one. */
13049 dyn.d_un.d_ptr = 0;
13050 }
13051 break;
13052 }
13053 }
13054 continue;
13055
13056 case DT_PREINIT_ARRAYSZ:
13057 name = ".preinit_array";
13058 goto get_out_size;
13059 case DT_INIT_ARRAYSZ:
13060 name = ".init_array";
13061 goto get_out_size;
13062 case DT_FINI_ARRAYSZ:
13063 name = ".fini_array";
13064 get_out_size:
13065 o = bfd_get_section_by_name (abfd, name);
13066 if (o == NULL)
13067 {
13068 _bfd_error_handler
13069 (_("could not find section %s"), name);
13070 goto error_return;
13071 }
13072 if (o->size == 0)
13073 _bfd_error_handler
13074 (_("warning: %s section has zero size"), name);
13075 dyn.d_un.d_val = o->size;
13076 break;
13077
13078 case DT_PREINIT_ARRAY:
13079 name = ".preinit_array";
13080 goto get_out_vma;
13081 case DT_INIT_ARRAY:
13082 name = ".init_array";
13083 goto get_out_vma;
13084 case DT_FINI_ARRAY:
13085 name = ".fini_array";
13086 get_out_vma:
13087 o = bfd_get_section_by_name (abfd, name);
13088 goto do_vma;
13089
13090 case DT_HASH:
13091 name = ".hash";
13092 goto get_vma;
13093 case DT_GNU_HASH:
13094 name = ".gnu.hash";
13095 goto get_vma;
13096 case DT_STRTAB:
13097 name = ".dynstr";
13098 goto get_vma;
13099 case DT_SYMTAB:
13100 name = ".dynsym";
13101 goto get_vma;
13102 case DT_VERDEF:
13103 name = ".gnu.version_d";
13104 goto get_vma;
13105 case DT_VERNEED:
13106 name = ".gnu.version_r";
13107 goto get_vma;
13108 case DT_VERSYM:
13109 name = ".gnu.version";
13110 get_vma:
13111 o = bfd_get_linker_section (dynobj, name);
13112 do_vma:
13113 if (o == NULL || bfd_is_abs_section (o->output_section))
13114 {
13115 _bfd_error_handler
13116 (_("could not find section %s"), name);
13117 goto error_return;
13118 }
13119 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13120 {
13121 _bfd_error_handler
13122 (_("warning: section '%s' is being made into a note"), name);
13123 bfd_set_error (bfd_error_nonrepresentable_section);
13124 goto error_return;
13125 }
13126 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13127 break;
13128
13129 case DT_REL:
13130 case DT_RELA:
13131 case DT_RELSZ:
13132 case DT_RELASZ:
13133 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13134 type = SHT_REL;
13135 else
13136 type = SHT_RELA;
13137 sh_size = 0;
13138 sh_addr = 0;
13139 for (i = 1; i < elf_numsections (abfd); i++)
13140 {
13141 Elf_Internal_Shdr *hdr;
13142
13143 hdr = elf_elfsections (abfd)[i];
13144 if (hdr->sh_type == type
13145 && (hdr->sh_flags & SHF_ALLOC) != 0)
13146 {
13147 sh_size += hdr->sh_size;
13148 if (sh_addr == 0
13149 || sh_addr > hdr->sh_addr)
13150 sh_addr = hdr->sh_addr;
13151 }
13152 }
13153
13154 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13155 {
13156 unsigned int opb = bfd_octets_per_byte (abfd, o);
13157
13158 /* Don't count procedure linkage table relocs in the
13159 overall reloc count. */
13160 sh_size -= htab->srelplt->size;
13161 if (sh_size == 0)
13162 /* If the size is zero, make the address zero too.
13163 This is to avoid a glibc bug. If the backend
13164 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13165 zero, then we'll put DT_RELA at the end of
13166 DT_JMPREL. glibc will interpret the end of
13167 DT_RELA matching the end of DT_JMPREL as the
13168 case where DT_RELA includes DT_JMPREL, and for
13169 LD_BIND_NOW will decide that processing DT_RELA
13170 will process the PLT relocs too. Net result:
13171 No PLT relocs applied. */
13172 sh_addr = 0;
13173
13174 /* If .rela.plt is the first .rela section, exclude
13175 it from DT_RELA. */
13176 else if (sh_addr == (htab->srelplt->output_section->vma
13177 + htab->srelplt->output_offset) * opb)
13178 sh_addr += htab->srelplt->size;
13179 }
13180
13181 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13182 dyn.d_un.d_val = sh_size;
13183 else
13184 dyn.d_un.d_ptr = sh_addr;
13185 break;
13186 }
13187 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13188 }
13189 }
13190
13191 /* If we have created any dynamic sections, then output them. */
13192 if (dynobj != NULL)
13193 {
13194 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13195 goto error_return;
13196
13197 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13198 if (bfd_link_textrel_check (info)
13199 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13200 {
13201 bfd_byte *dyncon, *dynconend;
13202
13203 dyncon = o->contents;
13204 dynconend = o->contents + o->size;
13205 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13206 {
13207 Elf_Internal_Dyn dyn;
13208
13209 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13210
13211 if (dyn.d_tag == DT_TEXTREL)
13212 {
13213 if (info->textrel_check == textrel_check_error)
13214 info->callbacks->einfo
13215 (_("%P%X: read-only segment has dynamic relocations\n"));
13216 else if (bfd_link_dll (info))
13217 info->callbacks->einfo
13218 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13219 else
13220 info->callbacks->einfo
13221 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13222 break;
13223 }
13224 }
13225 }
13226
13227 for (o = dynobj->sections; o != NULL; o = o->next)
13228 {
13229 if ((o->flags & SEC_HAS_CONTENTS) == 0
13230 || o->size == 0
13231 || o->output_section == bfd_abs_section_ptr)
13232 continue;
13233 if ((o->flags & SEC_LINKER_CREATED) == 0)
13234 {
13235 /* At this point, we are only interested in sections
13236 created by _bfd_elf_link_create_dynamic_sections. */
13237 continue;
13238 }
13239 if (htab->stab_info.stabstr == o)
13240 continue;
13241 if (htab->eh_info.hdr_sec == o)
13242 continue;
13243 if (strcmp (o->name, ".dynstr") != 0)
13244 {
13245 bfd_size_type octets = ((file_ptr) o->output_offset
13246 * bfd_octets_per_byte (abfd, o));
13247 if (!bfd_set_section_contents (abfd, o->output_section,
13248 o->contents, octets, o->size))
13249 goto error_return;
13250 }
13251 else
13252 {
13253 /* The contents of the .dynstr section are actually in a
13254 stringtab. */
13255 file_ptr off;
13256
13257 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13258 if (bfd_seek (abfd, off, SEEK_SET) != 0
13259 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13260 goto error_return;
13261 }
13262 }
13263 }
13264
13265 if (!info->resolve_section_groups)
13266 {
13267 bfd_boolean failed = FALSE;
13268
13269 BFD_ASSERT (bfd_link_relocatable (info));
13270 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13271 if (failed)
13272 goto error_return;
13273 }
13274
13275 /* If we have optimized stabs strings, output them. */
13276 if (htab->stab_info.stabstr != NULL)
13277 {
13278 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13279 goto error_return;
13280 }
13281
13282 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13283 goto error_return;
13284
13285 if (info->callbacks->emit_ctf)
13286 info->callbacks->emit_ctf ();
13287
13288 elf_final_link_free (abfd, &flinfo);
13289
13290 if (attr_section)
13291 {
13292 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13293 if (contents == NULL)
13294 {
13295 /* Bail out and fail. */
13296 ret = FALSE;
13297 goto return_local_hash_table;
13298 }
13299 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13300 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13301 free (contents);
13302 }
13303
13304 return_local_hash_table:
13305 if (info->unique_symbol)
13306 bfd_hash_table_free (&flinfo.local_hash_table);
13307 return ret;
13308
13309 error_return:
13310 elf_final_link_free (abfd, &flinfo);
13311 ret = FALSE;
13312 goto return_local_hash_table;
13313 }
13314 \f
13315 /* Initialize COOKIE for input bfd ABFD. */
13316
13317 static bfd_boolean
13318 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13319 struct bfd_link_info *info, bfd *abfd)
13320 {
13321 Elf_Internal_Shdr *symtab_hdr;
13322 const struct elf_backend_data *bed;
13323
13324 bed = get_elf_backend_data (abfd);
13325 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13326
13327 cookie->abfd = abfd;
13328 cookie->sym_hashes = elf_sym_hashes (abfd);
13329 cookie->bad_symtab = elf_bad_symtab (abfd);
13330 if (cookie->bad_symtab)
13331 {
13332 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13333 cookie->extsymoff = 0;
13334 }
13335 else
13336 {
13337 cookie->locsymcount = symtab_hdr->sh_info;
13338 cookie->extsymoff = symtab_hdr->sh_info;
13339 }
13340
13341 if (bed->s->arch_size == 32)
13342 cookie->r_sym_shift = 8;
13343 else
13344 cookie->r_sym_shift = 32;
13345
13346 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13347 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13348 {
13349 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13350 cookie->locsymcount, 0,
13351 NULL, NULL, NULL);
13352 if (cookie->locsyms == NULL)
13353 {
13354 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13355 return FALSE;
13356 }
13357 if (info->keep_memory)
13358 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13359 }
13360 return TRUE;
13361 }
13362
13363 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13364
13365 static void
13366 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13367 {
13368 Elf_Internal_Shdr *symtab_hdr;
13369
13370 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13371 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13372 free (cookie->locsyms);
13373 }
13374
13375 /* Initialize the relocation information in COOKIE for input section SEC
13376 of input bfd ABFD. */
13377
13378 static bfd_boolean
13379 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13380 struct bfd_link_info *info, bfd *abfd,
13381 asection *sec)
13382 {
13383 if (sec->reloc_count == 0)
13384 {
13385 cookie->rels = NULL;
13386 cookie->relend = NULL;
13387 }
13388 else
13389 {
13390 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13391 info->keep_memory);
13392 if (cookie->rels == NULL)
13393 return FALSE;
13394 cookie->rel = cookie->rels;
13395 cookie->relend = cookie->rels + sec->reloc_count;
13396 }
13397 cookie->rel = cookie->rels;
13398 return TRUE;
13399 }
13400
13401 /* Free the memory allocated by init_reloc_cookie_rels,
13402 if appropriate. */
13403
13404 static void
13405 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13406 asection *sec)
13407 {
13408 if (elf_section_data (sec)->relocs != cookie->rels)
13409 free (cookie->rels);
13410 }
13411
13412 /* Initialize the whole of COOKIE for input section SEC. */
13413
13414 static bfd_boolean
13415 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13416 struct bfd_link_info *info,
13417 asection *sec)
13418 {
13419 if (!init_reloc_cookie (cookie, info, sec->owner))
13420 goto error1;
13421 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13422 goto error2;
13423 return TRUE;
13424
13425 error2:
13426 fini_reloc_cookie (cookie, sec->owner);
13427 error1:
13428 return FALSE;
13429 }
13430
13431 /* Free the memory allocated by init_reloc_cookie_for_section,
13432 if appropriate. */
13433
13434 static void
13435 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13436 asection *sec)
13437 {
13438 fini_reloc_cookie_rels (cookie, sec);
13439 fini_reloc_cookie (cookie, sec->owner);
13440 }
13441 \f
13442 /* Garbage collect unused sections. */
13443
13444 /* Default gc_mark_hook. */
13445
13446 asection *
13447 _bfd_elf_gc_mark_hook (asection *sec,
13448 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13449 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13450 struct elf_link_hash_entry *h,
13451 Elf_Internal_Sym *sym)
13452 {
13453 if (h != NULL)
13454 {
13455 switch (h->root.type)
13456 {
13457 case bfd_link_hash_defined:
13458 case bfd_link_hash_defweak:
13459 return h->root.u.def.section;
13460
13461 case bfd_link_hash_common:
13462 return h->root.u.c.p->section;
13463
13464 default:
13465 break;
13466 }
13467 }
13468 else
13469 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13470
13471 return NULL;
13472 }
13473
13474 /* Return the debug definition section. */
13475
13476 static asection *
13477 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13478 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13479 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13480 struct elf_link_hash_entry *h,
13481 Elf_Internal_Sym *sym)
13482 {
13483 if (h != NULL)
13484 {
13485 /* Return the global debug definition section. */
13486 if ((h->root.type == bfd_link_hash_defined
13487 || h->root.type == bfd_link_hash_defweak)
13488 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13489 return h->root.u.def.section;
13490 }
13491 else
13492 {
13493 /* Return the local debug definition section. */
13494 asection *isec = bfd_section_from_elf_index (sec->owner,
13495 sym->st_shndx);
13496 if ((isec->flags & SEC_DEBUGGING) != 0)
13497 return isec;
13498 }
13499
13500 return NULL;
13501 }
13502
13503 /* COOKIE->rel describes a relocation against section SEC, which is
13504 a section we've decided to keep. Return the section that contains
13505 the relocation symbol, or NULL if no section contains it. */
13506
13507 asection *
13508 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13509 elf_gc_mark_hook_fn gc_mark_hook,
13510 struct elf_reloc_cookie *cookie,
13511 bfd_boolean *start_stop)
13512 {
13513 unsigned long r_symndx;
13514 struct elf_link_hash_entry *h, *hw;
13515
13516 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13517 if (r_symndx == STN_UNDEF)
13518 return NULL;
13519
13520 if (r_symndx >= cookie->locsymcount
13521 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13522 {
13523 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13524 if (h == NULL)
13525 {
13526 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13527 sec->owner);
13528 return NULL;
13529 }
13530 while (h->root.type == bfd_link_hash_indirect
13531 || h->root.type == bfd_link_hash_warning)
13532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13533 h->mark = 1;
13534 /* Keep all aliases of the symbol too. If an object symbol
13535 needs to be copied into .dynbss then all of its aliases
13536 should be present as dynamic symbols, not just the one used
13537 on the copy relocation. */
13538 hw = h;
13539 while (hw->is_weakalias)
13540 {
13541 hw = hw->u.alias;
13542 hw->mark = 1;
13543 }
13544
13545 if (start_stop != NULL)
13546 {
13547 /* To work around a glibc bug, mark XXX input sections
13548 when there is a reference to __start_XXX or __stop_XXX
13549 symbols. */
13550 if (h->start_stop)
13551 {
13552 asection *s = h->u2.start_stop_section;
13553 *start_stop = !s->gc_mark;
13554 return s;
13555 }
13556 }
13557
13558 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13559 }
13560
13561 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13562 &cookie->locsyms[r_symndx]);
13563 }
13564
13565 /* COOKIE->rel describes a relocation against section SEC, which is
13566 a section we've decided to keep. Mark the section that contains
13567 the relocation symbol. */
13568
13569 bfd_boolean
13570 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13571 asection *sec,
13572 elf_gc_mark_hook_fn gc_mark_hook,
13573 struct elf_reloc_cookie *cookie)
13574 {
13575 asection *rsec;
13576 bfd_boolean start_stop = FALSE;
13577
13578 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13579 while (rsec != NULL)
13580 {
13581 if (!rsec->gc_mark)
13582 {
13583 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13584 || (rsec->owner->flags & DYNAMIC) != 0)
13585 rsec->gc_mark = 1;
13586 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13587 return FALSE;
13588 }
13589 if (!start_stop)
13590 break;
13591 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13592 }
13593 return TRUE;
13594 }
13595
13596 /* The mark phase of garbage collection. For a given section, mark
13597 it and any sections in this section's group, and all the sections
13598 which define symbols to which it refers. */
13599
13600 bfd_boolean
13601 _bfd_elf_gc_mark (struct bfd_link_info *info,
13602 asection *sec,
13603 elf_gc_mark_hook_fn gc_mark_hook)
13604 {
13605 bfd_boolean ret;
13606 asection *group_sec, *eh_frame;
13607
13608 sec->gc_mark = 1;
13609
13610 /* Mark all the sections in the group. */
13611 group_sec = elf_section_data (sec)->next_in_group;
13612 if (group_sec && !group_sec->gc_mark)
13613 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13614 return FALSE;
13615
13616 /* Look through the section relocs. */
13617 ret = TRUE;
13618 eh_frame = elf_eh_frame_section (sec->owner);
13619 if ((sec->flags & SEC_RELOC) != 0
13620 && sec->reloc_count > 0
13621 && sec != eh_frame)
13622 {
13623 struct elf_reloc_cookie cookie;
13624
13625 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13626 ret = FALSE;
13627 else
13628 {
13629 for (; cookie.rel < cookie.relend; cookie.rel++)
13630 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13631 {
13632 ret = FALSE;
13633 break;
13634 }
13635 fini_reloc_cookie_for_section (&cookie, sec);
13636 }
13637 }
13638
13639 if (ret && eh_frame && elf_fde_list (sec))
13640 {
13641 struct elf_reloc_cookie cookie;
13642
13643 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13644 ret = FALSE;
13645 else
13646 {
13647 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13648 gc_mark_hook, &cookie))
13649 ret = FALSE;
13650 fini_reloc_cookie_for_section (&cookie, eh_frame);
13651 }
13652 }
13653
13654 eh_frame = elf_section_eh_frame_entry (sec);
13655 if (ret && eh_frame && !eh_frame->gc_mark)
13656 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13657 ret = FALSE;
13658
13659 return ret;
13660 }
13661
13662 /* Scan and mark sections in a special or debug section group. */
13663
13664 static void
13665 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13666 {
13667 /* Point to first section of section group. */
13668 asection *ssec;
13669 /* Used to iterate the section group. */
13670 asection *msec;
13671
13672 bfd_boolean is_special_grp = TRUE;
13673 bfd_boolean is_debug_grp = TRUE;
13674
13675 /* First scan to see if group contains any section other than debug
13676 and special section. */
13677 ssec = msec = elf_next_in_group (grp);
13678 do
13679 {
13680 if ((msec->flags & SEC_DEBUGGING) == 0)
13681 is_debug_grp = FALSE;
13682
13683 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13684 is_special_grp = FALSE;
13685
13686 msec = elf_next_in_group (msec);
13687 }
13688 while (msec != ssec);
13689
13690 /* If this is a pure debug section group or pure special section group,
13691 keep all sections in this group. */
13692 if (is_debug_grp || is_special_grp)
13693 {
13694 do
13695 {
13696 msec->gc_mark = 1;
13697 msec = elf_next_in_group (msec);
13698 }
13699 while (msec != ssec);
13700 }
13701 }
13702
13703 /* Keep debug and special sections. */
13704
13705 bfd_boolean
13706 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13707 elf_gc_mark_hook_fn mark_hook)
13708 {
13709 bfd *ibfd;
13710
13711 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13712 {
13713 asection *isec;
13714 bfd_boolean some_kept;
13715 bfd_boolean debug_frag_seen;
13716 bfd_boolean has_kept_debug_info;
13717
13718 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13719 continue;
13720 isec = ibfd->sections;
13721 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13722 continue;
13723
13724 /* Ensure all linker created sections are kept,
13725 see if any other section is already marked,
13726 and note if we have any fragmented debug sections. */
13727 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13728 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13729 {
13730 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13731 isec->gc_mark = 1;
13732 else if (isec->gc_mark
13733 && (isec->flags & SEC_ALLOC) != 0
13734 && elf_section_type (isec) != SHT_NOTE)
13735 some_kept = TRUE;
13736 else
13737 {
13738 /* Since all sections, except for backend specific ones,
13739 have been garbage collected, call mark_hook on this
13740 section if any of its linked-to sections is marked. */
13741 asection *linked_to_sec = elf_linked_to_section (isec);
13742 for (; linked_to_sec != NULL;
13743 linked_to_sec = elf_linked_to_section (linked_to_sec))
13744 if (linked_to_sec->gc_mark)
13745 {
13746 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13747 return FALSE;
13748 break;
13749 }
13750 }
13751
13752 if (!debug_frag_seen
13753 && (isec->flags & SEC_DEBUGGING)
13754 && CONST_STRNEQ (isec->name, ".debug_line."))
13755 debug_frag_seen = TRUE;
13756 else if (strcmp (bfd_section_name (isec),
13757 "__patchable_function_entries") == 0
13758 && elf_linked_to_section (isec) == NULL)
13759 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13760 "need linked-to section "
13761 "for --gc-sections\n"),
13762 isec->owner, isec);
13763 }
13764
13765 /* If no non-note alloc section in this file will be kept, then
13766 we can toss out the debug and special sections. */
13767 if (!some_kept)
13768 continue;
13769
13770 /* Keep debug and special sections like .comment when they are
13771 not part of a group. Also keep section groups that contain
13772 just debug sections or special sections. NB: Sections with
13773 linked-to section has been handled above. */
13774 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13775 {
13776 if ((isec->flags & SEC_GROUP) != 0)
13777 _bfd_elf_gc_mark_debug_special_section_group (isec);
13778 else if (((isec->flags & SEC_DEBUGGING) != 0
13779 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13780 && elf_next_in_group (isec) == NULL
13781 && elf_linked_to_section (isec) == NULL)
13782 isec->gc_mark = 1;
13783 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13784 has_kept_debug_info = TRUE;
13785 }
13786
13787 /* Look for CODE sections which are going to be discarded,
13788 and find and discard any fragmented debug sections which
13789 are associated with that code section. */
13790 if (debug_frag_seen)
13791 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13792 if ((isec->flags & SEC_CODE) != 0
13793 && isec->gc_mark == 0)
13794 {
13795 unsigned int ilen;
13796 asection *dsec;
13797
13798 ilen = strlen (isec->name);
13799
13800 /* Association is determined by the name of the debug
13801 section containing the name of the code section as
13802 a suffix. For example .debug_line.text.foo is a
13803 debug section associated with .text.foo. */
13804 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13805 {
13806 unsigned int dlen;
13807
13808 if (dsec->gc_mark == 0
13809 || (dsec->flags & SEC_DEBUGGING) == 0)
13810 continue;
13811
13812 dlen = strlen (dsec->name);
13813
13814 if (dlen > ilen
13815 && strncmp (dsec->name + (dlen - ilen),
13816 isec->name, ilen) == 0)
13817 dsec->gc_mark = 0;
13818 }
13819 }
13820
13821 /* Mark debug sections referenced by kept debug sections. */
13822 if (has_kept_debug_info)
13823 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13824 if (isec->gc_mark
13825 && (isec->flags & SEC_DEBUGGING) != 0)
13826 if (!_bfd_elf_gc_mark (info, isec,
13827 elf_gc_mark_debug_section))
13828 return FALSE;
13829 }
13830 return TRUE;
13831 }
13832
13833 static bfd_boolean
13834 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13835 {
13836 bfd *sub;
13837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13838
13839 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13840 {
13841 asection *o;
13842
13843 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13844 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13845 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13846 continue;
13847 o = sub->sections;
13848 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13849 continue;
13850
13851 for (o = sub->sections; o != NULL; o = o->next)
13852 {
13853 /* When any section in a section group is kept, we keep all
13854 sections in the section group. If the first member of
13855 the section group is excluded, we will also exclude the
13856 group section. */
13857 if (o->flags & SEC_GROUP)
13858 {
13859 asection *first = elf_next_in_group (o);
13860 o->gc_mark = first->gc_mark;
13861 }
13862
13863 if (o->gc_mark)
13864 continue;
13865
13866 /* Skip sweeping sections already excluded. */
13867 if (o->flags & SEC_EXCLUDE)
13868 continue;
13869
13870 /* Since this is early in the link process, it is simple
13871 to remove a section from the output. */
13872 o->flags |= SEC_EXCLUDE;
13873
13874 if (info->print_gc_sections && o->size != 0)
13875 /* xgettext:c-format */
13876 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13877 o, sub);
13878 }
13879 }
13880
13881 return TRUE;
13882 }
13883
13884 /* Propagate collected vtable information. This is called through
13885 elf_link_hash_traverse. */
13886
13887 static bfd_boolean
13888 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13889 {
13890 /* Those that are not vtables. */
13891 if (h->start_stop
13892 || h->u2.vtable == NULL
13893 || h->u2.vtable->parent == NULL)
13894 return TRUE;
13895
13896 /* Those vtables that do not have parents, we cannot merge. */
13897 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13898 return TRUE;
13899
13900 /* If we've already been done, exit. */
13901 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13902 return TRUE;
13903
13904 /* Make sure the parent's table is up to date. */
13905 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13906
13907 if (h->u2.vtable->used == NULL)
13908 {
13909 /* None of this table's entries were referenced. Re-use the
13910 parent's table. */
13911 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13912 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13913 }
13914 else
13915 {
13916 size_t n;
13917 bfd_boolean *cu, *pu;
13918
13919 /* Or the parent's entries into ours. */
13920 cu = h->u2.vtable->used;
13921 cu[-1] = TRUE;
13922 pu = h->u2.vtable->parent->u2.vtable->used;
13923 if (pu != NULL)
13924 {
13925 const struct elf_backend_data *bed;
13926 unsigned int log_file_align;
13927
13928 bed = get_elf_backend_data (h->root.u.def.section->owner);
13929 log_file_align = bed->s->log_file_align;
13930 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13931 while (n--)
13932 {
13933 if (*pu)
13934 *cu = TRUE;
13935 pu++;
13936 cu++;
13937 }
13938 }
13939 }
13940
13941 return TRUE;
13942 }
13943
13944 static bfd_boolean
13945 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13946 {
13947 asection *sec;
13948 bfd_vma hstart, hend;
13949 Elf_Internal_Rela *relstart, *relend, *rel;
13950 const struct elf_backend_data *bed;
13951 unsigned int log_file_align;
13952
13953 /* Take care of both those symbols that do not describe vtables as
13954 well as those that are not loaded. */
13955 if (h->start_stop
13956 || h->u2.vtable == NULL
13957 || h->u2.vtable->parent == NULL)
13958 return TRUE;
13959
13960 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13961 || h->root.type == bfd_link_hash_defweak);
13962
13963 sec = h->root.u.def.section;
13964 hstart = h->root.u.def.value;
13965 hend = hstart + h->size;
13966
13967 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13968 if (!relstart)
13969 return *(bfd_boolean *) okp = FALSE;
13970 bed = get_elf_backend_data (sec->owner);
13971 log_file_align = bed->s->log_file_align;
13972
13973 relend = relstart + sec->reloc_count;
13974
13975 for (rel = relstart; rel < relend; ++rel)
13976 if (rel->r_offset >= hstart && rel->r_offset < hend)
13977 {
13978 /* If the entry is in use, do nothing. */
13979 if (h->u2.vtable->used
13980 && (rel->r_offset - hstart) < h->u2.vtable->size)
13981 {
13982 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13983 if (h->u2.vtable->used[entry])
13984 continue;
13985 }
13986 /* Otherwise, kill it. */
13987 rel->r_offset = rel->r_info = rel->r_addend = 0;
13988 }
13989
13990 return TRUE;
13991 }
13992
13993 /* Mark sections containing dynamically referenced symbols. When
13994 building shared libraries, we must assume that any visible symbol is
13995 referenced. */
13996
13997 bfd_boolean
13998 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13999 {
14000 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14001 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14002
14003 if ((h->root.type == bfd_link_hash_defined
14004 || h->root.type == bfd_link_hash_defweak)
14005 && ((h->ref_dynamic && !h->forced_local)
14006 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14007 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14008 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14009 && (!bfd_link_executable (info)
14010 || info->gc_keep_exported
14011 || info->export_dynamic
14012 || (h->dynamic
14013 && d != NULL
14014 && (*d->match) (&d->head, NULL, h->root.root.string)))
14015 && (h->versioned >= versioned
14016 || !bfd_hide_sym_by_version (info->version_info,
14017 h->root.root.string)))))
14018 h->root.u.def.section->flags |= SEC_KEEP;
14019
14020 return TRUE;
14021 }
14022
14023 /* Keep all sections containing symbols undefined on the command-line,
14024 and the section containing the entry symbol. */
14025
14026 void
14027 _bfd_elf_gc_keep (struct bfd_link_info *info)
14028 {
14029 struct bfd_sym_chain *sym;
14030
14031 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14032 {
14033 struct elf_link_hash_entry *h;
14034
14035 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14036 FALSE, FALSE, FALSE);
14037
14038 if (h != NULL
14039 && (h->root.type == bfd_link_hash_defined
14040 || h->root.type == bfd_link_hash_defweak)
14041 && !bfd_is_const_section (h->root.u.def.section))
14042 h->root.u.def.section->flags |= SEC_KEEP;
14043 }
14044 }
14045
14046 bfd_boolean
14047 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14048 struct bfd_link_info *info)
14049 {
14050 bfd *ibfd = info->input_bfds;
14051
14052 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14053 {
14054 asection *sec;
14055 struct elf_reloc_cookie cookie;
14056
14057 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14058 continue;
14059 sec = ibfd->sections;
14060 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14061 continue;
14062
14063 if (!init_reloc_cookie (&cookie, info, ibfd))
14064 return FALSE;
14065
14066 for (sec = ibfd->sections; sec; sec = sec->next)
14067 {
14068 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
14069 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14070 {
14071 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14072 fini_reloc_cookie_rels (&cookie, sec);
14073 }
14074 }
14075 }
14076 return TRUE;
14077 }
14078
14079 /* Do mark and sweep of unused sections. */
14080
14081 bfd_boolean
14082 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14083 {
14084 bfd_boolean ok = TRUE;
14085 bfd *sub;
14086 elf_gc_mark_hook_fn gc_mark_hook;
14087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14088 struct elf_link_hash_table *htab;
14089
14090 if (!bed->can_gc_sections
14091 || !is_elf_hash_table (info->hash))
14092 {
14093 _bfd_error_handler(_("warning: gc-sections option ignored"));
14094 return TRUE;
14095 }
14096
14097 bed->gc_keep (info);
14098 htab = elf_hash_table (info);
14099
14100 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14101 at the .eh_frame section if we can mark the FDEs individually. */
14102 for (sub = info->input_bfds;
14103 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14104 sub = sub->link.next)
14105 {
14106 asection *sec;
14107 struct elf_reloc_cookie cookie;
14108
14109 sec = sub->sections;
14110 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14111 continue;
14112 sec = bfd_get_section_by_name (sub, ".eh_frame");
14113 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14114 {
14115 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14116 if (elf_section_data (sec)->sec_info
14117 && (sec->flags & SEC_LINKER_CREATED) == 0)
14118 elf_eh_frame_section (sub) = sec;
14119 fini_reloc_cookie_for_section (&cookie, sec);
14120 sec = bfd_get_next_section_by_name (NULL, sec);
14121 }
14122 }
14123
14124 /* Apply transitive closure to the vtable entry usage info. */
14125 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14126 if (!ok)
14127 return FALSE;
14128
14129 /* Kill the vtable relocations that were not used. */
14130 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14131 if (!ok)
14132 return FALSE;
14133
14134 /* Mark dynamically referenced symbols. */
14135 if (htab->dynamic_sections_created || info->gc_keep_exported)
14136 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14137
14138 /* Grovel through relocs to find out who stays ... */
14139 gc_mark_hook = bed->gc_mark_hook;
14140 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14141 {
14142 asection *o;
14143
14144 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14145 || elf_object_id (sub) != elf_hash_table_id (htab)
14146 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14147 continue;
14148
14149 o = sub->sections;
14150 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14151 continue;
14152
14153 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14154 Also treat note sections as a root, if the section is not part
14155 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14156 well as FINI_ARRAY sections for ld -r. */
14157 for (o = sub->sections; o != NULL; o = o->next)
14158 if (!o->gc_mark
14159 && (o->flags & SEC_EXCLUDE) == 0
14160 && ((o->flags & SEC_KEEP) != 0
14161 || (bfd_link_relocatable (info)
14162 && ((elf_section_data (o)->this_hdr.sh_type
14163 == SHT_PREINIT_ARRAY)
14164 || (elf_section_data (o)->this_hdr.sh_type
14165 == SHT_INIT_ARRAY)
14166 || (elf_section_data (o)->this_hdr.sh_type
14167 == SHT_FINI_ARRAY)))
14168 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14169 && elf_next_in_group (o) == NULL
14170 && elf_linked_to_section (o) == NULL)
14171 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14172 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14173 {
14174 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14175 return FALSE;
14176 }
14177 }
14178
14179 /* Allow the backend to mark additional target specific sections. */
14180 bed->gc_mark_extra_sections (info, gc_mark_hook);
14181
14182 /* ... and mark SEC_EXCLUDE for those that go. */
14183 return elf_gc_sweep (abfd, info);
14184 }
14185 \f
14186 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14187
14188 bfd_boolean
14189 bfd_elf_gc_record_vtinherit (bfd *abfd,
14190 asection *sec,
14191 struct elf_link_hash_entry *h,
14192 bfd_vma offset)
14193 {
14194 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14195 struct elf_link_hash_entry **search, *child;
14196 size_t extsymcount;
14197 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14198
14199 /* The sh_info field of the symtab header tells us where the
14200 external symbols start. We don't care about the local symbols at
14201 this point. */
14202 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14203 if (!elf_bad_symtab (abfd))
14204 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14205
14206 sym_hashes = elf_sym_hashes (abfd);
14207 sym_hashes_end = sym_hashes + extsymcount;
14208
14209 /* Hunt down the child symbol, which is in this section at the same
14210 offset as the relocation. */
14211 for (search = sym_hashes; search != sym_hashes_end; ++search)
14212 {
14213 if ((child = *search) != NULL
14214 && (child->root.type == bfd_link_hash_defined
14215 || child->root.type == bfd_link_hash_defweak)
14216 && child->root.u.def.section == sec
14217 && child->root.u.def.value == offset)
14218 goto win;
14219 }
14220
14221 /* xgettext:c-format */
14222 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14223 abfd, sec, (uint64_t) offset);
14224 bfd_set_error (bfd_error_invalid_operation);
14225 return FALSE;
14226
14227 win:
14228 if (!child->u2.vtable)
14229 {
14230 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14231 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14232 if (!child->u2.vtable)
14233 return FALSE;
14234 }
14235 if (!h)
14236 {
14237 /* This *should* only be the absolute section. It could potentially
14238 be that someone has defined a non-global vtable though, which
14239 would be bad. It isn't worth paging in the local symbols to be
14240 sure though; that case should simply be handled by the assembler. */
14241
14242 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14243 }
14244 else
14245 child->u2.vtable->parent = h;
14246
14247 return TRUE;
14248 }
14249
14250 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14251
14252 bfd_boolean
14253 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14254 struct elf_link_hash_entry *h,
14255 bfd_vma addend)
14256 {
14257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14258 unsigned int log_file_align = bed->s->log_file_align;
14259
14260 if (!h)
14261 {
14262 /* xgettext:c-format */
14263 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14264 abfd, sec);
14265 bfd_set_error (bfd_error_bad_value);
14266 return FALSE;
14267 }
14268
14269 if (!h->u2.vtable)
14270 {
14271 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14272 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14273 if (!h->u2.vtable)
14274 return FALSE;
14275 }
14276
14277 if (addend >= h->u2.vtable->size)
14278 {
14279 size_t size, bytes, file_align;
14280 bfd_boolean *ptr = h->u2.vtable->used;
14281
14282 /* While the symbol is undefined, we have to be prepared to handle
14283 a zero size. */
14284 file_align = 1 << log_file_align;
14285 if (h->root.type == bfd_link_hash_undefined)
14286 size = addend + file_align;
14287 else
14288 {
14289 size = h->size;
14290 if (addend >= size)
14291 {
14292 /* Oops! We've got a reference past the defined end of
14293 the table. This is probably a bug -- shall we warn? */
14294 size = addend + file_align;
14295 }
14296 }
14297 size = (size + file_align - 1) & -file_align;
14298
14299 /* Allocate one extra entry for use as a "done" flag for the
14300 consolidation pass. */
14301 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14302
14303 if (ptr)
14304 {
14305 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14306
14307 if (ptr != NULL)
14308 {
14309 size_t oldbytes;
14310
14311 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14312 * sizeof (bfd_boolean));
14313 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14314 }
14315 }
14316 else
14317 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14318
14319 if (ptr == NULL)
14320 return FALSE;
14321
14322 /* And arrange for that done flag to be at index -1. */
14323 h->u2.vtable->used = ptr + 1;
14324 h->u2.vtable->size = size;
14325 }
14326
14327 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14328
14329 return TRUE;
14330 }
14331
14332 /* Map an ELF section header flag to its corresponding string. */
14333 typedef struct
14334 {
14335 char *flag_name;
14336 flagword flag_value;
14337 } elf_flags_to_name_table;
14338
14339 static elf_flags_to_name_table elf_flags_to_names [] =
14340 {
14341 { "SHF_WRITE", SHF_WRITE },
14342 { "SHF_ALLOC", SHF_ALLOC },
14343 { "SHF_EXECINSTR", SHF_EXECINSTR },
14344 { "SHF_MERGE", SHF_MERGE },
14345 { "SHF_STRINGS", SHF_STRINGS },
14346 { "SHF_INFO_LINK", SHF_INFO_LINK},
14347 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14348 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14349 { "SHF_GROUP", SHF_GROUP },
14350 { "SHF_TLS", SHF_TLS },
14351 { "SHF_MASKOS", SHF_MASKOS },
14352 { "SHF_EXCLUDE", SHF_EXCLUDE },
14353 };
14354
14355 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14356 bfd_boolean
14357 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14358 struct flag_info *flaginfo,
14359 asection *section)
14360 {
14361 const bfd_vma sh_flags = elf_section_flags (section);
14362
14363 if (!flaginfo->flags_initialized)
14364 {
14365 bfd *obfd = info->output_bfd;
14366 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14367 struct flag_info_list *tf = flaginfo->flag_list;
14368 int with_hex = 0;
14369 int without_hex = 0;
14370
14371 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14372 {
14373 unsigned i;
14374 flagword (*lookup) (char *);
14375
14376 lookup = bed->elf_backend_lookup_section_flags_hook;
14377 if (lookup != NULL)
14378 {
14379 flagword hexval = (*lookup) ((char *) tf->name);
14380
14381 if (hexval != 0)
14382 {
14383 if (tf->with == with_flags)
14384 with_hex |= hexval;
14385 else if (tf->with == without_flags)
14386 without_hex |= hexval;
14387 tf->valid = TRUE;
14388 continue;
14389 }
14390 }
14391 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14392 {
14393 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14394 {
14395 if (tf->with == with_flags)
14396 with_hex |= elf_flags_to_names[i].flag_value;
14397 else if (tf->with == without_flags)
14398 without_hex |= elf_flags_to_names[i].flag_value;
14399 tf->valid = TRUE;
14400 break;
14401 }
14402 }
14403 if (!tf->valid)
14404 {
14405 info->callbacks->einfo
14406 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14407 return FALSE;
14408 }
14409 }
14410 flaginfo->flags_initialized = TRUE;
14411 flaginfo->only_with_flags |= with_hex;
14412 flaginfo->not_with_flags |= without_hex;
14413 }
14414
14415 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14416 return FALSE;
14417
14418 if ((flaginfo->not_with_flags & sh_flags) != 0)
14419 return FALSE;
14420
14421 return TRUE;
14422 }
14423
14424 struct alloc_got_off_arg {
14425 bfd_vma gotoff;
14426 struct bfd_link_info *info;
14427 };
14428
14429 /* We need a special top-level link routine to convert got reference counts
14430 to real got offsets. */
14431
14432 static bfd_boolean
14433 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14434 {
14435 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14436 bfd *obfd = gofarg->info->output_bfd;
14437 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14438
14439 if (h->got.refcount > 0)
14440 {
14441 h->got.offset = gofarg->gotoff;
14442 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14443 }
14444 else
14445 h->got.offset = (bfd_vma) -1;
14446
14447 return TRUE;
14448 }
14449
14450 /* And an accompanying bit to work out final got entry offsets once
14451 we're done. Should be called from final_link. */
14452
14453 bfd_boolean
14454 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14455 struct bfd_link_info *info)
14456 {
14457 bfd *i;
14458 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14459 bfd_vma gotoff;
14460 struct alloc_got_off_arg gofarg;
14461
14462 BFD_ASSERT (abfd == info->output_bfd);
14463
14464 if (! is_elf_hash_table (info->hash))
14465 return FALSE;
14466
14467 /* The GOT offset is relative to the .got section, but the GOT header is
14468 put into the .got.plt section, if the backend uses it. */
14469 if (bed->want_got_plt)
14470 gotoff = 0;
14471 else
14472 gotoff = bed->got_header_size;
14473
14474 /* Do the local .got entries first. */
14475 for (i = info->input_bfds; i; i = i->link.next)
14476 {
14477 bfd_signed_vma *local_got;
14478 size_t j, locsymcount;
14479 Elf_Internal_Shdr *symtab_hdr;
14480
14481 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14482 continue;
14483
14484 local_got = elf_local_got_refcounts (i);
14485 if (!local_got)
14486 continue;
14487
14488 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14489 if (elf_bad_symtab (i))
14490 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14491 else
14492 locsymcount = symtab_hdr->sh_info;
14493
14494 for (j = 0; j < locsymcount; ++j)
14495 {
14496 if (local_got[j] > 0)
14497 {
14498 local_got[j] = gotoff;
14499 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14500 }
14501 else
14502 local_got[j] = (bfd_vma) -1;
14503 }
14504 }
14505
14506 /* Then the global .got entries. .plt refcounts are handled by
14507 adjust_dynamic_symbol */
14508 gofarg.gotoff = gotoff;
14509 gofarg.info = info;
14510 elf_link_hash_traverse (elf_hash_table (info),
14511 elf_gc_allocate_got_offsets,
14512 &gofarg);
14513 return TRUE;
14514 }
14515
14516 /* Many folk need no more in the way of final link than this, once
14517 got entry reference counting is enabled. */
14518
14519 bfd_boolean
14520 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14521 {
14522 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14523 return FALSE;
14524
14525 /* Invoke the regular ELF backend linker to do all the work. */
14526 return bfd_elf_final_link (abfd, info);
14527 }
14528
14529 bfd_boolean
14530 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14531 {
14532 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14533
14534 if (rcookie->bad_symtab)
14535 rcookie->rel = rcookie->rels;
14536
14537 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14538 {
14539 unsigned long r_symndx;
14540
14541 if (! rcookie->bad_symtab)
14542 if (rcookie->rel->r_offset > offset)
14543 return FALSE;
14544 if (rcookie->rel->r_offset != offset)
14545 continue;
14546
14547 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14548 if (r_symndx == STN_UNDEF)
14549 return TRUE;
14550
14551 if (r_symndx >= rcookie->locsymcount
14552 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14553 {
14554 struct elf_link_hash_entry *h;
14555
14556 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14557
14558 while (h->root.type == bfd_link_hash_indirect
14559 || h->root.type == bfd_link_hash_warning)
14560 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14561
14562 if ((h->root.type == bfd_link_hash_defined
14563 || h->root.type == bfd_link_hash_defweak)
14564 && (h->root.u.def.section->owner != rcookie->abfd
14565 || h->root.u.def.section->kept_section != NULL
14566 || discarded_section (h->root.u.def.section)))
14567 return TRUE;
14568 }
14569 else
14570 {
14571 /* It's not a relocation against a global symbol,
14572 but it could be a relocation against a local
14573 symbol for a discarded section. */
14574 asection *isec;
14575 Elf_Internal_Sym *isym;
14576
14577 /* Need to: get the symbol; get the section. */
14578 isym = &rcookie->locsyms[r_symndx];
14579 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14580 if (isec != NULL
14581 && (isec->kept_section != NULL
14582 || discarded_section (isec)))
14583 return TRUE;
14584 }
14585 return FALSE;
14586 }
14587 return FALSE;
14588 }
14589
14590 /* Discard unneeded references to discarded sections.
14591 Returns -1 on error, 1 if any section's size was changed, 0 if
14592 nothing changed. This function assumes that the relocations are in
14593 sorted order, which is true for all known assemblers. */
14594
14595 int
14596 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14597 {
14598 struct elf_reloc_cookie cookie;
14599 asection *o;
14600 bfd *abfd;
14601 int changed = 0;
14602
14603 if (info->traditional_format
14604 || !is_elf_hash_table (info->hash))
14605 return 0;
14606
14607 o = bfd_get_section_by_name (output_bfd, ".stab");
14608 if (o != NULL)
14609 {
14610 asection *i;
14611
14612 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14613 {
14614 if (i->size == 0
14615 || i->reloc_count == 0
14616 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14617 continue;
14618
14619 abfd = i->owner;
14620 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14621 continue;
14622
14623 if (!init_reloc_cookie_for_section (&cookie, info, i))
14624 return -1;
14625
14626 if (_bfd_discard_section_stabs (abfd, i,
14627 elf_section_data (i)->sec_info,
14628 bfd_elf_reloc_symbol_deleted_p,
14629 &cookie))
14630 changed = 1;
14631
14632 fini_reloc_cookie_for_section (&cookie, i);
14633 }
14634 }
14635
14636 o = NULL;
14637 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14638 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14639 if (o != NULL)
14640 {
14641 asection *i;
14642 int eh_changed = 0;
14643 unsigned int eh_alignment; /* Octets. */
14644
14645 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14646 {
14647 if (i->size == 0)
14648 continue;
14649
14650 abfd = i->owner;
14651 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14652 continue;
14653
14654 if (!init_reloc_cookie_for_section (&cookie, info, i))
14655 return -1;
14656
14657 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14658 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14659 bfd_elf_reloc_symbol_deleted_p,
14660 &cookie))
14661 {
14662 eh_changed = 1;
14663 if (i->size != i->rawsize)
14664 changed = 1;
14665 }
14666
14667 fini_reloc_cookie_for_section (&cookie, i);
14668 }
14669
14670 eh_alignment = ((1 << o->alignment_power)
14671 * bfd_octets_per_byte (output_bfd, o));
14672 /* Skip over zero terminator, and prevent empty sections from
14673 adding alignment padding at the end. */
14674 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14675 if (i->size == 0)
14676 i->flags |= SEC_EXCLUDE;
14677 else if (i->size > 4)
14678 break;
14679 /* The last non-empty eh_frame section doesn't need padding. */
14680 if (i != NULL)
14681 i = i->map_tail.s;
14682 /* Any prior sections must pad the last FDE out to the output
14683 section alignment. Otherwise we might have zero padding
14684 between sections, which would be seen as a terminator. */
14685 for (; i != NULL; i = i->map_tail.s)
14686 if (i->size == 4)
14687 /* All but the last zero terminator should have been removed. */
14688 BFD_FAIL ();
14689 else
14690 {
14691 bfd_size_type size
14692 = (i->size + eh_alignment - 1) & -eh_alignment;
14693 if (i->size != size)
14694 {
14695 i->size = size;
14696 changed = 1;
14697 eh_changed = 1;
14698 }
14699 }
14700 if (eh_changed)
14701 elf_link_hash_traverse (elf_hash_table (info),
14702 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14703 }
14704
14705 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14706 {
14707 const struct elf_backend_data *bed;
14708 asection *s;
14709
14710 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14711 continue;
14712 s = abfd->sections;
14713 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14714 continue;
14715
14716 bed = get_elf_backend_data (abfd);
14717
14718 if (bed->elf_backend_discard_info != NULL)
14719 {
14720 if (!init_reloc_cookie (&cookie, info, abfd))
14721 return -1;
14722
14723 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14724 changed = 1;
14725
14726 fini_reloc_cookie (&cookie, abfd);
14727 }
14728 }
14729
14730 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14731 _bfd_elf_end_eh_frame_parsing (info);
14732
14733 if (info->eh_frame_hdr_type
14734 && !bfd_link_relocatable (info)
14735 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14736 changed = 1;
14737
14738 return changed;
14739 }
14740
14741 bfd_boolean
14742 _bfd_elf_section_already_linked (bfd *abfd,
14743 asection *sec,
14744 struct bfd_link_info *info)
14745 {
14746 flagword flags;
14747 const char *name, *key;
14748 struct bfd_section_already_linked *l;
14749 struct bfd_section_already_linked_hash_entry *already_linked_list;
14750
14751 if (sec->output_section == bfd_abs_section_ptr)
14752 return FALSE;
14753
14754 flags = sec->flags;
14755
14756 /* Return if it isn't a linkonce section. A comdat group section
14757 also has SEC_LINK_ONCE set. */
14758 if ((flags & SEC_LINK_ONCE) == 0)
14759 return FALSE;
14760
14761 /* Don't put group member sections on our list of already linked
14762 sections. They are handled as a group via their group section. */
14763 if (elf_sec_group (sec) != NULL)
14764 return FALSE;
14765
14766 /* For a SHT_GROUP section, use the group signature as the key. */
14767 name = sec->name;
14768 if ((flags & SEC_GROUP) != 0
14769 && elf_next_in_group (sec) != NULL
14770 && elf_group_name (elf_next_in_group (sec)) != NULL)
14771 key = elf_group_name (elf_next_in_group (sec));
14772 else
14773 {
14774 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14775 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14776 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14777 key++;
14778 else
14779 /* Must be a user linkonce section that doesn't follow gcc's
14780 naming convention. In this case we won't be matching
14781 single member groups. */
14782 key = name;
14783 }
14784
14785 already_linked_list = bfd_section_already_linked_table_lookup (key);
14786
14787 for (l = already_linked_list->entry; l != NULL; l = l->next)
14788 {
14789 /* We may have 2 different types of sections on the list: group
14790 sections with a signature of <key> (<key> is some string),
14791 and linkonce sections named .gnu.linkonce.<type>.<key>.
14792 Match like sections. LTO plugin sections are an exception.
14793 They are always named .gnu.linkonce.t.<key> and match either
14794 type of section. */
14795 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14796 && ((flags & SEC_GROUP) != 0
14797 || strcmp (name, l->sec->name) == 0))
14798 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14799 || (sec->owner->flags & BFD_PLUGIN) != 0)
14800 {
14801 /* The section has already been linked. See if we should
14802 issue a warning. */
14803 if (!_bfd_handle_already_linked (sec, l, info))
14804 return FALSE;
14805
14806 if (flags & SEC_GROUP)
14807 {
14808 asection *first = elf_next_in_group (sec);
14809 asection *s = first;
14810
14811 while (s != NULL)
14812 {
14813 s->output_section = bfd_abs_section_ptr;
14814 /* Record which group discards it. */
14815 s->kept_section = l->sec;
14816 s = elf_next_in_group (s);
14817 /* These lists are circular. */
14818 if (s == first)
14819 break;
14820 }
14821 }
14822
14823 return TRUE;
14824 }
14825 }
14826
14827 /* A single member comdat group section may be discarded by a
14828 linkonce section and vice versa. */
14829 if ((flags & SEC_GROUP) != 0)
14830 {
14831 asection *first = elf_next_in_group (sec);
14832
14833 if (first != NULL && elf_next_in_group (first) == first)
14834 /* Check this single member group against linkonce sections. */
14835 for (l = already_linked_list->entry; l != NULL; l = l->next)
14836 if ((l->sec->flags & SEC_GROUP) == 0
14837 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14838 {
14839 first->output_section = bfd_abs_section_ptr;
14840 first->kept_section = l->sec;
14841 sec->output_section = bfd_abs_section_ptr;
14842 break;
14843 }
14844 }
14845 else
14846 /* Check this linkonce section against single member groups. */
14847 for (l = already_linked_list->entry; l != NULL; l = l->next)
14848 if (l->sec->flags & SEC_GROUP)
14849 {
14850 asection *first = elf_next_in_group (l->sec);
14851
14852 if (first != NULL
14853 && elf_next_in_group (first) == first
14854 && bfd_elf_match_symbols_in_sections (first, sec, info))
14855 {
14856 sec->output_section = bfd_abs_section_ptr;
14857 sec->kept_section = first;
14858 break;
14859 }
14860 }
14861
14862 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14863 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14864 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14865 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14866 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14867 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14868 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14869 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14870 The reverse order cannot happen as there is never a bfd with only the
14871 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14872 matter as here were are looking only for cross-bfd sections. */
14873
14874 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14875 for (l = already_linked_list->entry; l != NULL; l = l->next)
14876 if ((l->sec->flags & SEC_GROUP) == 0
14877 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14878 {
14879 if (abfd != l->sec->owner)
14880 sec->output_section = bfd_abs_section_ptr;
14881 break;
14882 }
14883
14884 /* This is the first section with this name. Record it. */
14885 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14886 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14887 return sec->output_section == bfd_abs_section_ptr;
14888 }
14889
14890 bfd_boolean
14891 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14892 {
14893 return sym->st_shndx == SHN_COMMON;
14894 }
14895
14896 unsigned int
14897 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14898 {
14899 return SHN_COMMON;
14900 }
14901
14902 asection *
14903 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14904 {
14905 return bfd_com_section_ptr;
14906 }
14907
14908 bfd_vma
14909 _bfd_elf_default_got_elt_size (bfd *abfd,
14910 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14911 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14912 bfd *ibfd ATTRIBUTE_UNUSED,
14913 unsigned long symndx ATTRIBUTE_UNUSED)
14914 {
14915 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14916 return bed->s->arch_size / 8;
14917 }
14918
14919 /* Routines to support the creation of dynamic relocs. */
14920
14921 /* Returns the name of the dynamic reloc section associated with SEC. */
14922
14923 static const char *
14924 get_dynamic_reloc_section_name (bfd * abfd,
14925 asection * sec,
14926 bfd_boolean is_rela)
14927 {
14928 char *name;
14929 const char *old_name = bfd_section_name (sec);
14930 const char *prefix = is_rela ? ".rela" : ".rel";
14931
14932 if (old_name == NULL)
14933 return NULL;
14934
14935 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14936 sprintf (name, "%s%s", prefix, old_name);
14937
14938 return name;
14939 }
14940
14941 /* Returns the dynamic reloc section associated with SEC.
14942 If necessary compute the name of the dynamic reloc section based
14943 on SEC's name (looked up in ABFD's string table) and the setting
14944 of IS_RELA. */
14945
14946 asection *
14947 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14948 asection * sec,
14949 bfd_boolean is_rela)
14950 {
14951 asection * reloc_sec = elf_section_data (sec)->sreloc;
14952
14953 if (reloc_sec == NULL)
14954 {
14955 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14956
14957 if (name != NULL)
14958 {
14959 reloc_sec = bfd_get_linker_section (abfd, name);
14960
14961 if (reloc_sec != NULL)
14962 elf_section_data (sec)->sreloc = reloc_sec;
14963 }
14964 }
14965
14966 return reloc_sec;
14967 }
14968
14969 /* Returns the dynamic reloc section associated with SEC. If the
14970 section does not exist it is created and attached to the DYNOBJ
14971 bfd and stored in the SRELOC field of SEC's elf_section_data
14972 structure.
14973
14974 ALIGNMENT is the alignment for the newly created section and
14975 IS_RELA defines whether the name should be .rela.<SEC's name>
14976 or .rel.<SEC's name>. The section name is looked up in the
14977 string table associated with ABFD. */
14978
14979 asection *
14980 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14981 bfd *dynobj,
14982 unsigned int alignment,
14983 bfd *abfd,
14984 bfd_boolean is_rela)
14985 {
14986 asection * reloc_sec = elf_section_data (sec)->sreloc;
14987
14988 if (reloc_sec == NULL)
14989 {
14990 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14991
14992 if (name == NULL)
14993 return NULL;
14994
14995 reloc_sec = bfd_get_linker_section (dynobj, name);
14996
14997 if (reloc_sec == NULL)
14998 {
14999 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15000 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15001 if ((sec->flags & SEC_ALLOC) != 0)
15002 flags |= SEC_ALLOC | SEC_LOAD;
15003
15004 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15005 if (reloc_sec != NULL)
15006 {
15007 /* _bfd_elf_get_sec_type_attr chooses a section type by
15008 name. Override as it may be wrong, eg. for a user
15009 section named "auto" we'll get ".relauto" which is
15010 seen to be a .rela section. */
15011 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15012 if (!bfd_set_section_alignment (reloc_sec, alignment))
15013 reloc_sec = NULL;
15014 }
15015 }
15016
15017 elf_section_data (sec)->sreloc = reloc_sec;
15018 }
15019
15020 return reloc_sec;
15021 }
15022
15023 /* Copy the ELF symbol type and other attributes for a linker script
15024 assignment from HSRC to HDEST. Generally this should be treated as
15025 if we found a strong non-dynamic definition for HDEST (except that
15026 ld ignores multiple definition errors). */
15027 void
15028 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15029 struct bfd_link_hash_entry *hdest,
15030 struct bfd_link_hash_entry *hsrc)
15031 {
15032 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15033 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15034 Elf_Internal_Sym isym;
15035
15036 ehdest->type = ehsrc->type;
15037 ehdest->target_internal = ehsrc->target_internal;
15038
15039 isym.st_other = ehsrc->other;
15040 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
15041 }
15042
15043 /* Append a RELA relocation REL to section S in BFD. */
15044
15045 void
15046 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15047 {
15048 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15049 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15050 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15051 bed->s->swap_reloca_out (abfd, rel, loc);
15052 }
15053
15054 /* Append a REL relocation REL to section S in BFD. */
15055
15056 void
15057 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15058 {
15059 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15060 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15061 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15062 bed->s->swap_reloc_out (abfd, rel, loc);
15063 }
15064
15065 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15066
15067 struct bfd_link_hash_entry *
15068 bfd_elf_define_start_stop (struct bfd_link_info *info,
15069 const char *symbol, asection *sec)
15070 {
15071 struct elf_link_hash_entry *h;
15072
15073 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15074 FALSE, FALSE, TRUE);
15075 /* NB: Common symbols will be turned into definition later. */
15076 if (h != NULL
15077 && (h->root.type == bfd_link_hash_undefined
15078 || h->root.type == bfd_link_hash_undefweak
15079 || ((h->ref_regular || h->def_dynamic)
15080 && !h->def_regular
15081 && h->root.type != bfd_link_hash_common)))
15082 {
15083 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
15084 h->verinfo.verdef = NULL;
15085 h->root.type = bfd_link_hash_defined;
15086 h->root.u.def.section = sec;
15087 h->root.u.def.value = 0;
15088 h->def_regular = 1;
15089 h->def_dynamic = 0;
15090 h->start_stop = 1;
15091 h->u2.start_stop_section = sec;
15092 if (symbol[0] == '.')
15093 {
15094 /* .startof. and .sizeof. symbols are local. */
15095 const struct elf_backend_data *bed;
15096 bed = get_elf_backend_data (info->output_bfd);
15097 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
15098 }
15099 else
15100 {
15101 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15102 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15103 | info->start_stop_visibility);
15104 if (was_dynamic)
15105 bfd_elf_link_record_dynamic_symbol (info, h);
15106 }
15107 return &h->root;
15108 }
15109 return NULL;
15110 }
15111
15112 /* Find dynamic relocs for H that apply to read-only sections. */
15113
15114 asection *
15115 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15116 {
15117 struct elf_dyn_relocs *p;
15118
15119 for (p = h->dyn_relocs; p != NULL; p = p->next)
15120 {
15121 asection *s = p->sec->output_section;
15122
15123 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15124 return p->sec;
15125 }
15126 return NULL;
15127 }
15128
15129 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15130 read-only sections. */
15131
15132 bfd_boolean
15133 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15134 {
15135 asection *sec;
15136
15137 if (h->root.type == bfd_link_hash_indirect)
15138 return TRUE;
15139
15140 sec = _bfd_elf_readonly_dynrelocs (h);
15141 if (sec != NULL)
15142 {
15143 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15144
15145 info->flags |= DF_TEXTREL;
15146 /* xgettext:c-format */
15147 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15148 "in read-only section `%pA'\n"),
15149 sec->owner, h->root.root.string, sec);
15150
15151 if (bfd_link_textrel_check (info))
15152 /* xgettext:c-format */
15153 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15154 "in read-only section `%pA'\n"),
15155 sec->owner, h->root.root.string, sec);
15156
15157 /* Not an error, just cut short the traversal. */
15158 return FALSE;
15159 }
15160 return TRUE;
15161 }
15162
15163 /* Add dynamic tags. */
15164
15165 bfd_boolean
15166 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15167 bfd_boolean need_dynamic_reloc)
15168 {
15169 struct elf_link_hash_table *htab = elf_hash_table (info);
15170
15171 if (htab->dynamic_sections_created)
15172 {
15173 /* Add some entries to the .dynamic section. We fill in the
15174 values later, in finish_dynamic_sections, but we must add
15175 the entries now so that we get the correct size for the
15176 .dynamic section. The DT_DEBUG entry is filled in by the
15177 dynamic linker and used by the debugger. */
15178 #define add_dynamic_entry(TAG, VAL) \
15179 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15180
15181 const struct elf_backend_data *bed
15182 = get_elf_backend_data (output_bfd);
15183
15184 if (bfd_link_executable (info))
15185 {
15186 if (!add_dynamic_entry (DT_DEBUG, 0))
15187 return FALSE;
15188 }
15189
15190 if (htab->dt_pltgot_required || htab->splt->size != 0)
15191 {
15192 /* DT_PLTGOT is used by prelink even if there is no PLT
15193 relocation. */
15194 if (!add_dynamic_entry (DT_PLTGOT, 0))
15195 return FALSE;
15196 }
15197
15198 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15199 {
15200 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15201 || !add_dynamic_entry (DT_PLTREL,
15202 (bed->rela_plts_and_copies_p
15203 ? DT_RELA : DT_REL))
15204 || !add_dynamic_entry (DT_JMPREL, 0))
15205 return FALSE;
15206 }
15207
15208 if (htab->tlsdesc_plt
15209 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15210 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15211 return FALSE;
15212
15213 if (need_dynamic_reloc)
15214 {
15215 if (bed->rela_plts_and_copies_p)
15216 {
15217 if (!add_dynamic_entry (DT_RELA, 0)
15218 || !add_dynamic_entry (DT_RELASZ, 0)
15219 || !add_dynamic_entry (DT_RELAENT,
15220 bed->s->sizeof_rela))
15221 return FALSE;
15222 }
15223 else
15224 {
15225 if (!add_dynamic_entry (DT_REL, 0)
15226 || !add_dynamic_entry (DT_RELSZ, 0)
15227 || !add_dynamic_entry (DT_RELENT,
15228 bed->s->sizeof_rel))
15229 return FALSE;
15230 }
15231
15232 /* If any dynamic relocs apply to a read-only section,
15233 then we need a DT_TEXTREL entry. */
15234 if ((info->flags & DF_TEXTREL) == 0)
15235 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15236 info);
15237
15238 if ((info->flags & DF_TEXTREL) != 0)
15239 {
15240 if (htab->ifunc_resolvers)
15241 info->callbacks->einfo
15242 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15243 "may result in a segfault at runtime; recompile with %s\n"),
15244 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15245
15246 if (!add_dynamic_entry (DT_TEXTREL, 0))
15247 return FALSE;
15248 }
15249 }
15250 }
15251 #undef add_dynamic_entry
15252
15253 return TRUE;
15254 }