elflink.c constify
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #ifndef CHAR_BIT
39 #define CHAR_BIT 8
40 #endif
41
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45 struct elf_info_failed
46 {
47 struct bfd_link_info *info;
48 bfd_boolean failed;
49 };
50
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54 struct elf_find_verdep_info
55 {
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62 };
63
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
67 asection *
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71 {
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
86 return h->root.u.def.section;
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106 }
107
108 /* Define a symbol in a dynamic linkage section. */
109
110 struct elf_link_hash_entry *
111 _bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115 {
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
128 bh = &h->root;
129 }
130 else
131 bh = NULL;
132
133 bed = get_elf_backend_data (abfd);
134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
135 sec, 0, NULL, FALSE, bed->collect,
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
139 BFD_ASSERT (h != NULL);
140 h->def_regular = 1;
141 h->non_elf = 0;
142 h->root.linker_def = 1;
143 h->type = STT_OBJECT;
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
146
147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
148 return h;
149 }
150
151 bfd_boolean
152 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
153 {
154 flagword flags;
155 asection *s;
156 struct elf_link_hash_entry *h;
157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
158 struct elf_link_hash_table *htab = elf_hash_table (info);
159
160 /* This function may be called more than once. */
161 if (htab->sgot != NULL)
162 return TRUE;
163
164 flags = bed->dynamic_sec_flags;
165
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
171 if (s == NULL
172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
173 return FALSE;
174 htab->srelgot = s;
175
176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
177 if (s == NULL
178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
179 return FALSE;
180 htab->sgot = s;
181
182 if (bed->want_got_plt)
183 {
184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
185 if (s == NULL
186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
187 return FALSE;
188 htab->sgotplt = s;
189 }
190
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info)->hgot = h;
203 if (h == NULL)
204 return FALSE;
205 }
206
207 return TRUE;
208 }
209 \f
210 /* Create a strtab to hold the dynamic symbol names. */
211 static bfd_boolean
212 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213 {
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
226 asection *s;
227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
228 if ((ibfd->flags
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249 }
250
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
257
258 bfd_boolean
259 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
260 {
261 flagword flags;
262 asection *s;
263 const struct elf_backend_data *bed;
264 struct elf_link_hash_entry *h;
265
266 if (! is_elf_hash_table (info->hash))
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
274
275 abfd = elf_hash_table (info)->dynobj;
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info) && !info->nointerp)
283 {
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
288 }
289
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || !bfd_set_section_alignment (s, 1))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 return FALSE;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
312 if (s == NULL
313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
314 return FALSE;
315 elf_hash_table (info)->dynsym = s;
316
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
319 if (s == NULL)
320 return FALSE;
321
322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
323 if (s == NULL
324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
336 return FALSE;
337
338 if (info->emit_hash)
339 {
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
342 if (s == NULL
343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
349 {
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
352 if (s == NULL
353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374 }
375
376 /* Create dynamic sections when linking against a dynamic object. */
377
378 bfd_boolean
379 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
380 {
381 flagword flags, pltflags;
382 struct elf_link_hash_entry *h;
383 asection *s;
384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
385 struct elf_link_hash_table *htab = elf_hash_table (info);
386
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags = bed->dynamic_sec_flags;
390
391 pltflags = flags;
392 if (bed->plt_not_loaded)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
403 if (s == NULL
404 || !bfd_set_section_alignment (s, bed->plt_alignment))
405 return FALSE;
406 htab->splt = s;
407
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
418
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
423 if (s == NULL
424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
425 return FALSE;
426 htab->srelplt = s;
427
428 if (! _bfd_elf_create_got_section (abfd, info))
429 return FALSE;
430
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
440 SEC_ALLOC | SEC_LINKER_CREATED);
441 if (s == NULL)
442 return FALSE;
443 htab->sdynbss = s;
444
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 flags);
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
468 if (bfd_link_executable (info))
469 {
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
474 if (s == NULL
475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
476 return FALSE;
477 htab->srelbss = s;
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
490 }
491 }
492
493 return TRUE;
494 }
495 \f
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
504 bfd_boolean
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
507 {
508 if (h->dynindx == -1)
509 {
510 struct elf_strtab_hash *dynstr;
511 char *p;
512 const char *name;
513 size_t indx;
514
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
535 {
536 h->forced_local = 1;
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
539 }
540
541 default:
542 break;
543 }
544
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
547
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
550 {
551 /* Create a strtab to hold the dynamic symbol names. */
552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
553 if (dynstr == NULL)
554 return FALSE;
555 }
556
557 /* We don't put any version information in the dynamic string
558 table. */
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
568
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
570
571 if (p != NULL)
572 *p = ELF_VER_CHR;
573
574 if (indx == (size_t) -1)
575 return FALSE;
576 h->dynstr_index = indx;
577 }
578
579 return TRUE;
580 }
581 \f
582 /* Mark a symbol dynamic. */
583
584 static void
585 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
588 {
589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
590
591 /* It may be called more than once on the same H. */
592 if(h->dynamic || bfd_link_relocatable (info))
593 return;
594
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
597 || h->type == STT_COMMON
598 || (sym != NULL
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
601 || (d != NULL
602 && h->non_elf
603 && (*d->match) (&d->head, NULL, h->root.root.string)))
604 {
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
609 }
610 }
611
612 /* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
614
615 bfd_boolean
616 bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
618 const char *name,
619 bfd_boolean provide,
620 bfd_boolean hidden)
621 {
622 struct elf_link_hash_entry *h, *hv;
623 struct elf_link_hash_table *htab;
624 const struct elf_backend_data *bed;
625
626 if (!is_elf_hash_table (info->hash))
627 return TRUE;
628
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
631 if (h == NULL)
632 return provide;
633
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
636
637 if (h->versioned == unknown)
638 {
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
642 {
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
647 }
648 }
649
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
653 {
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
656 }
657
658 switch (h->root.type)
659 {
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
669 h->root.type = bfd_link_hash_new;
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
672 break;
673 case bfd_link_hash_new:
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
677 the versioned symbol point to this one. */
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
690 default:
691 BFD_FAIL ();
692 return FALSE;
693 }
694
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
700 && h->def_dynamic
701 && !h->def_regular)
702 h->root.type = bfd_link_hash_undefined;
703
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
709 h->verinfo.verdef = NULL;
710
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
713
714 h->def_regular = 1;
715
716 if (hidden)
717 {
718 bed = get_elf_backend_data (output_bfd);
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
722 }
723
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
726 if (!bfd_link_relocatable (info)
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
731
732 if ((h->def_dynamic
733 || h->ref_dynamic
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
736 && !h->forced_local
737 && h->dynindx == -1)
738 {
739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
740 return FALSE;
741
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
745 if (h->is_weakalias)
746 {
747 struct elf_link_hash_entry *def = weakdef (h);
748
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
751 return FALSE;
752 }
753 }
754
755 return TRUE;
756 }
757
758 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
761
762 int
763 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
766 {
767 size_t amt;
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
771 size_t dynstr_index;
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
775
776 if (! is_elf_hash_table (info->hash))
777 return 0;
778
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
783
784 amt = sizeof (*entry);
785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
786 if (entry == NULL)
787 return 0;
788
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
791 1, input_indx, &entry->isym, esym, &eshndx))
792 {
793 bfd_release (input_bfd, entry);
794 return 0;
795 }
796
797 if (entry->isym.st_shndx != SHN_UNDEF
798 && entry->isym.st_shndx < SHN_LORESERVE)
799 {
800 asection *s;
801
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
804 {
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
809 }
810 }
811
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
815
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
818 {
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
823 }
824
825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
826 if (dynstr_index == (size_t) -1)
827 return 0;
828 entry->isym.st_name = dynstr_index;
829
830 eht = elf_hash_table (info);
831
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
837
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
841
842 /* The dynindx will be set at the end of size_dynamic_sections. */
843
844 return 1;
845 }
846
847 /* Return the dynindex of a local dynamic symbol. */
848
849 long
850 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
853 {
854 struct elf_link_local_dynamic_entry *e;
855
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
860 }
861
862 /* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
865
866 static bfd_boolean
867 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869 {
870 size_t *count = (size_t *) data;
871
872 if (h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879 }
880
881
882 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
884
885 static bfd_boolean
886 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888 {
889 size_t *count = (size_t *) data;
890
891 if (!h->forced_local)
892 return TRUE;
893
894 if (h->dynindx != -1)
895 h->dynindx = ++(*count);
896
897 return TRUE;
898 }
899
900 /* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902 bfd_boolean
903 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
906 {
907 struct elf_link_hash_table *htab;
908 asection *ip;
909
910 switch (elf_section_data (p)->this_hdr.sh_type)
911 {
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
917 htab = elf_hash_table (info);
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
920
921 return (htab->dynobj != NULL
922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
923 && ip->output_section == p);
924
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
929 }
930 }
931
932 bfd_boolean
933 _bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
937 {
938 return TRUE;
939 }
940
941 /* Assign dynsym indices. In a shared library we generate a section
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
949
950 static unsigned long
951 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
954 {
955 unsigned long dynsymcount = 0;
956 bfd_boolean do_sec = section_sym_count != NULL;
957
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
960 {
961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
964 if ((p->flags & SEC_EXCLUDE) == 0
965 && (p->flags & SEC_ALLOC) != 0
966 && elf_hash_table (info)->dynamic_relocs
967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
968 {
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
972 }
973 else if (do_sec)
974 elf_section_data (p)->dynindx = 0;
975 }
976 if (do_sec)
977 *section_sym_count = dynsymcount;
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
982
983 if (elf_hash_table (info)->dynlocal)
984 {
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
988 }
989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
990
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
994
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
1000
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
1003 }
1004
1005 /* Merge st_other field. */
1006
1007 static void
1008 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1009 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 {
2082 if (!dynamic
2083 && h->root.type == bfd_link_hash_defweak
2084 && hi->root.type == bfd_link_hash_defined)
2085 {
2086 /* We are handling a weak sym@@ver and attempting to define
2087 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2088 new weak sym@ver because there is already a strong sym@ver.
2089 However, sym@ver and sym@@ver are really the same symbol.
2090 The existing strong sym@ver ought to override sym@@ver. */
2091 h->root.type = bfd_link_hash_defined;
2092 h->root.u.def.section = hi->root.u.def.section;
2093 h->root.u.def.value = hi->root.u.def.value;
2094 hi->root.type = bfd_link_hash_indirect;
2095 hi->root.u.i.link = &h->root;
2096 }
2097 else
2098 return TRUE;
2099 }
2100 else if (override)
2101 {
2102 /* Here SHORTNAME is a versioned name, so we don't expect to see
2103 the type of override we do in the case above unless it is
2104 overridden by a versioned definition. */
2105 if (hi->root.type != bfd_link_hash_defined
2106 && hi->root.type != bfd_link_hash_defweak)
2107 _bfd_error_handler
2108 /* xgettext:c-format */
2109 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2110 abfd, shortname);
2111 return TRUE;
2112 }
2113 else
2114 {
2115 bh = &hi->root;
2116 if (! (_bfd_generic_link_add_one_symbol
2117 (info, abfd, shortname, BSF_INDIRECT,
2118 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2119 return FALSE;
2120 hi = (struct elf_link_hash_entry *) bh;
2121 }
2122
2123 /* If there is a duplicate definition somewhere, then HI may not
2124 point to an indirect symbol. We will have reported an error
2125 to the user in that case. */
2126 if (hi->root.type == bfd_link_hash_indirect)
2127 {
2128 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2129 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2130 hi->dynamic_def |= h->dynamic_def;
2131
2132 /* If we first saw a reference to @VER symbol with
2133 non-default visibility, merge that visibility to the
2134 @@VER symbol. */
2135 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2136
2137 /* See if the new flags lead us to realize that the symbol
2138 must be dynamic. */
2139 if (! *dynsym)
2140 {
2141 if (! dynamic)
2142 {
2143 if (! bfd_link_executable (info)
2144 || hi->ref_dynamic)
2145 *dynsym = TRUE;
2146 }
2147 else
2148 {
2149 if (hi->ref_regular)
2150 *dynsym = TRUE;
2151 }
2152 }
2153 }
2154
2155 return TRUE;
2156 }
2157 \f
2158 /* This routine is used to export all defined symbols into the dynamic
2159 symbol table. It is called via elf_link_hash_traverse. */
2160
2161 static bfd_boolean
2162 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2163 {
2164 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2165
2166 /* Ignore indirect symbols. These are added by the versioning code. */
2167 if (h->root.type == bfd_link_hash_indirect)
2168 return TRUE;
2169
2170 /* Ignore this if we won't export it. */
2171 if (!eif->info->export_dynamic && !h->dynamic)
2172 return TRUE;
2173
2174 if (h->dynindx == -1
2175 && (h->def_regular || h->ref_regular)
2176 && ! bfd_hide_sym_by_version (eif->info->version_info,
2177 h->root.root.string))
2178 {
2179 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2180 {
2181 eif->failed = TRUE;
2182 return FALSE;
2183 }
2184 }
2185
2186 return TRUE;
2187 }
2188 \f
2189 /* Look through the symbols which are defined in other shared
2190 libraries and referenced here. Update the list of version
2191 dependencies. This will be put into the .gnu.version_r section.
2192 This function is called via elf_link_hash_traverse. */
2193
2194 static bfd_boolean
2195 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2196 void *data)
2197 {
2198 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2199 Elf_Internal_Verneed *t;
2200 Elf_Internal_Vernaux *a;
2201 size_t amt;
2202
2203 /* We only care about symbols defined in shared objects with version
2204 information. */
2205 if (!h->def_dynamic
2206 || h->def_regular
2207 || h->dynindx == -1
2208 || h->verinfo.verdef == NULL
2209 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2210 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2211 return TRUE;
2212
2213 /* See if we already know about this version. */
2214 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2215 t != NULL;
2216 t = t->vn_nextref)
2217 {
2218 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2219 continue;
2220
2221 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2222 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2223 return TRUE;
2224
2225 break;
2226 }
2227
2228 /* This is a new version. Add it to tree we are building. */
2229
2230 if (t == NULL)
2231 {
2232 amt = sizeof *t;
2233 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2234 if (t == NULL)
2235 {
2236 rinfo->failed = TRUE;
2237 return FALSE;
2238 }
2239
2240 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2241 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2242 elf_tdata (rinfo->info->output_bfd)->verref = t;
2243 }
2244
2245 amt = sizeof *a;
2246 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2247 if (a == NULL)
2248 {
2249 rinfo->failed = TRUE;
2250 return FALSE;
2251 }
2252
2253 /* Note that we are copying a string pointer here, and testing it
2254 above. If bfd_elf_string_from_elf_section is ever changed to
2255 discard the string data when low in memory, this will have to be
2256 fixed. */
2257 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2258
2259 a->vna_flags = h->verinfo.verdef->vd_flags;
2260 a->vna_nextptr = t->vn_auxptr;
2261
2262 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2263 ++rinfo->vers;
2264
2265 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2266
2267 t->vn_auxptr = a;
2268
2269 return TRUE;
2270 }
2271
2272 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2273 hidden. Set *T_P to NULL if there is no match. */
2274
2275 static bfd_boolean
2276 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2277 struct elf_link_hash_entry *h,
2278 const char *version_p,
2279 struct bfd_elf_version_tree **t_p,
2280 bfd_boolean *hide)
2281 {
2282 struct bfd_elf_version_tree *t;
2283
2284 /* Look for the version. If we find it, it is no longer weak. */
2285 for (t = info->version_info; t != NULL; t = t->next)
2286 {
2287 if (strcmp (t->name, version_p) == 0)
2288 {
2289 size_t len;
2290 char *alc;
2291 struct bfd_elf_version_expr *d;
2292
2293 len = version_p - h->root.root.string;
2294 alc = (char *) bfd_malloc (len);
2295 if (alc == NULL)
2296 return FALSE;
2297 memcpy (alc, h->root.root.string, len - 1);
2298 alc[len - 1] = '\0';
2299 if (alc[len - 2] == ELF_VER_CHR)
2300 alc[len - 2] = '\0';
2301
2302 h->verinfo.vertree = t;
2303 t->used = TRUE;
2304 d = NULL;
2305
2306 if (t->globals.list != NULL)
2307 d = (*t->match) (&t->globals, NULL, alc);
2308
2309 /* See if there is anything to force this symbol to
2310 local scope. */
2311 if (d == NULL && t->locals.list != NULL)
2312 {
2313 d = (*t->match) (&t->locals, NULL, alc);
2314 if (d != NULL
2315 && h->dynindx != -1
2316 && ! info->export_dynamic)
2317 *hide = TRUE;
2318 }
2319
2320 free (alc);
2321 break;
2322 }
2323 }
2324
2325 *t_p = t;
2326
2327 return TRUE;
2328 }
2329
2330 /* Return TRUE if the symbol H is hidden by version script. */
2331
2332 bfd_boolean
2333 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2334 struct elf_link_hash_entry *h)
2335 {
2336 const char *p;
2337 bfd_boolean hide = FALSE;
2338 const struct elf_backend_data *bed
2339 = get_elf_backend_data (info->output_bfd);
2340
2341 /* Version script only hides symbols defined in regular objects. */
2342 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2343 return TRUE;
2344
2345 p = strchr (h->root.root.string, ELF_VER_CHR);
2346 if (p != NULL && h->verinfo.vertree == NULL)
2347 {
2348 struct bfd_elf_version_tree *t;
2349
2350 ++p;
2351 if (*p == ELF_VER_CHR)
2352 ++p;
2353
2354 if (*p != '\0'
2355 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2356 && hide)
2357 {
2358 if (hide)
2359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2360 return TRUE;
2361 }
2362 }
2363
2364 /* If we don't have a version for this symbol, see if we can find
2365 something. */
2366 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2367 {
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2372 {
2373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2374 return TRUE;
2375 }
2376 }
2377
2378 return FALSE;
2379 }
2380
2381 /* Figure out appropriate versions for all the symbols. We may not
2382 have the version number script until we have read all of the input
2383 files, so until that point we don't know which symbols should be
2384 local. This function is called via elf_link_hash_traverse. */
2385
2386 static bfd_boolean
2387 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2388 {
2389 struct elf_info_failed *sinfo;
2390 struct bfd_link_info *info;
2391 const struct elf_backend_data *bed;
2392 struct elf_info_failed eif;
2393 char *p;
2394 bfd_boolean hide;
2395
2396 sinfo = (struct elf_info_failed *) data;
2397 info = sinfo->info;
2398
2399 /* Fix the symbol flags. */
2400 eif.failed = FALSE;
2401 eif.info = info;
2402 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2403 {
2404 if (eif.failed)
2405 sinfo->failed = TRUE;
2406 return FALSE;
2407 }
2408
2409 bed = get_elf_backend_data (info->output_bfd);
2410
2411 /* We only need version numbers for symbols defined in regular
2412 objects. */
2413 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2414 {
2415 /* Hide symbols defined in discarded input sections. */
2416 if ((h->root.type == bfd_link_hash_defined
2417 || h->root.type == bfd_link_hash_defweak)
2418 && discarded_section (h->root.u.def.section))
2419 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2420 return TRUE;
2421 }
2422
2423 hide = FALSE;
2424 p = strchr (h->root.root.string, ELF_VER_CHR);
2425 if (p != NULL && h->verinfo.vertree == NULL)
2426 {
2427 struct bfd_elf_version_tree *t;
2428
2429 ++p;
2430 if (*p == ELF_VER_CHR)
2431 ++p;
2432
2433 /* If there is no version string, we can just return out. */
2434 if (*p == '\0')
2435 return TRUE;
2436
2437 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2438 {
2439 sinfo->failed = TRUE;
2440 return FALSE;
2441 }
2442
2443 if (hide)
2444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2445
2446 /* If we are building an application, we need to create a
2447 version node for this version. */
2448 if (t == NULL && bfd_link_executable (info))
2449 {
2450 struct bfd_elf_version_tree **pp;
2451 int version_index;
2452
2453 /* If we aren't going to export this symbol, we don't need
2454 to worry about it. */
2455 if (h->dynindx == -1)
2456 return TRUE;
2457
2458 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2459 sizeof *t);
2460 if (t == NULL)
2461 {
2462 sinfo->failed = TRUE;
2463 return FALSE;
2464 }
2465
2466 t->name = p;
2467 t->name_indx = (unsigned int) -1;
2468 t->used = TRUE;
2469
2470 version_index = 1;
2471 /* Don't count anonymous version tag. */
2472 if (sinfo->info->version_info != NULL
2473 && sinfo->info->version_info->vernum == 0)
2474 version_index = 0;
2475 for (pp = &sinfo->info->version_info;
2476 *pp != NULL;
2477 pp = &(*pp)->next)
2478 ++version_index;
2479 t->vernum = version_index;
2480
2481 *pp = t;
2482
2483 h->verinfo.vertree = t;
2484 }
2485 else if (t == NULL)
2486 {
2487 /* We could not find the version for a symbol when
2488 generating a shared archive. Return an error. */
2489 _bfd_error_handler
2490 /* xgettext:c-format */
2491 (_("%pB: version node not found for symbol %s"),
2492 info->output_bfd, h->root.root.string);
2493 bfd_set_error (bfd_error_bad_value);
2494 sinfo->failed = TRUE;
2495 return FALSE;
2496 }
2497 }
2498
2499 /* If we don't have a version for this symbol, see if we can find
2500 something. */
2501 if (!hide
2502 && h->verinfo.vertree == NULL
2503 && sinfo->info->version_info != NULL)
2504 {
2505 h->verinfo.vertree
2506 = bfd_find_version_for_sym (sinfo->info->version_info,
2507 h->root.root.string, &hide);
2508 if (h->verinfo.vertree != NULL && hide)
2509 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2510 }
2511
2512 return TRUE;
2513 }
2514 \f
2515 /* Read and swap the relocs from the section indicated by SHDR. This
2516 may be either a REL or a RELA section. The relocations are
2517 translated into RELA relocations and stored in INTERNAL_RELOCS,
2518 which should have already been allocated to contain enough space.
2519 The EXTERNAL_RELOCS are a buffer where the external form of the
2520 relocations should be stored.
2521
2522 Returns FALSE if something goes wrong. */
2523
2524 static bfd_boolean
2525 elf_link_read_relocs_from_section (bfd *abfd,
2526 asection *sec,
2527 Elf_Internal_Shdr *shdr,
2528 void *external_relocs,
2529 Elf_Internal_Rela *internal_relocs)
2530 {
2531 const struct elf_backend_data *bed;
2532 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2533 const bfd_byte *erela;
2534 const bfd_byte *erelaend;
2535 Elf_Internal_Rela *irela;
2536 Elf_Internal_Shdr *symtab_hdr;
2537 size_t nsyms;
2538
2539 /* Position ourselves at the start of the section. */
2540 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2541 return FALSE;
2542
2543 /* Read the relocations. */
2544 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2545 return FALSE;
2546
2547 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2548 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2549
2550 bed = get_elf_backend_data (abfd);
2551
2552 /* Convert the external relocations to the internal format. */
2553 if (shdr->sh_entsize == bed->s->sizeof_rel)
2554 swap_in = bed->s->swap_reloc_in;
2555 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2556 swap_in = bed->s->swap_reloca_in;
2557 else
2558 {
2559 bfd_set_error (bfd_error_wrong_format);
2560 return FALSE;
2561 }
2562
2563 erela = (const bfd_byte *) external_relocs;
2564 /* Setting erelaend like this and comparing with <= handles case of
2565 a fuzzed object with sh_size not a multiple of sh_entsize. */
2566 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2567 irela = internal_relocs;
2568 while (erela <= erelaend)
2569 {
2570 bfd_vma r_symndx;
2571
2572 (*swap_in) (abfd, erela, irela);
2573 r_symndx = ELF32_R_SYM (irela->r_info);
2574 if (bed->s->arch_size == 64)
2575 r_symndx >>= 24;
2576 if (nsyms > 0)
2577 {
2578 if ((size_t) r_symndx >= nsyms)
2579 {
2580 _bfd_error_handler
2581 /* xgettext:c-format */
2582 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2583 " for offset %#" PRIx64 " in section `%pA'"),
2584 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2585 (uint64_t) irela->r_offset, sec);
2586 bfd_set_error (bfd_error_bad_value);
2587 return FALSE;
2588 }
2589 }
2590 else if (r_symndx != STN_UNDEF)
2591 {
2592 _bfd_error_handler
2593 /* xgettext:c-format */
2594 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2595 " for offset %#" PRIx64 " in section `%pA'"
2596 " when the object file has no symbol table"),
2597 abfd, (uint64_t) r_symndx,
2598 (uint64_t) irela->r_offset, sec);
2599 bfd_set_error (bfd_error_bad_value);
2600 return FALSE;
2601 }
2602 irela += bed->s->int_rels_per_ext_rel;
2603 erela += shdr->sh_entsize;
2604 }
2605
2606 return TRUE;
2607 }
2608
2609 /* Read and swap the relocs for a section O. They may have been
2610 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2611 not NULL, they are used as buffers to read into. They are known to
2612 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2613 the return value is allocated using either malloc or bfd_alloc,
2614 according to the KEEP_MEMORY argument. If O has two relocation
2615 sections (both REL and RELA relocations), then the REL_HDR
2616 relocations will appear first in INTERNAL_RELOCS, followed by the
2617 RELA_HDR relocations. */
2618
2619 Elf_Internal_Rela *
2620 _bfd_elf_link_read_relocs (bfd *abfd,
2621 asection *o,
2622 void *external_relocs,
2623 Elf_Internal_Rela *internal_relocs,
2624 bfd_boolean keep_memory)
2625 {
2626 void *alloc1 = NULL;
2627 Elf_Internal_Rela *alloc2 = NULL;
2628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2629 struct bfd_elf_section_data *esdo = elf_section_data (o);
2630 Elf_Internal_Rela *internal_rela_relocs;
2631
2632 if (esdo->relocs != NULL)
2633 return esdo->relocs;
2634
2635 if (o->reloc_count == 0)
2636 return NULL;
2637
2638 if (internal_relocs == NULL)
2639 {
2640 bfd_size_type size;
2641
2642 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2643 if (keep_memory)
2644 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2645 else
2646 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2647 if (internal_relocs == NULL)
2648 goto error_return;
2649 }
2650
2651 if (external_relocs == NULL)
2652 {
2653 bfd_size_type size = 0;
2654
2655 if (esdo->rel.hdr)
2656 size += esdo->rel.hdr->sh_size;
2657 if (esdo->rela.hdr)
2658 size += esdo->rela.hdr->sh_size;
2659
2660 alloc1 = bfd_malloc (size);
2661 if (alloc1 == NULL)
2662 goto error_return;
2663 external_relocs = alloc1;
2664 }
2665
2666 internal_rela_relocs = internal_relocs;
2667 if (esdo->rel.hdr)
2668 {
2669 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2670 external_relocs,
2671 internal_relocs))
2672 goto error_return;
2673 external_relocs = (((bfd_byte *) external_relocs)
2674 + esdo->rel.hdr->sh_size);
2675 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2676 * bed->s->int_rels_per_ext_rel);
2677 }
2678
2679 if (esdo->rela.hdr
2680 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2681 external_relocs,
2682 internal_rela_relocs)))
2683 goto error_return;
2684
2685 /* Cache the results for next time, if we can. */
2686 if (keep_memory)
2687 esdo->relocs = internal_relocs;
2688
2689 free (alloc1);
2690
2691 /* Don't free alloc2, since if it was allocated we are passing it
2692 back (under the name of internal_relocs). */
2693
2694 return internal_relocs;
2695
2696 error_return:
2697 free (alloc1);
2698 if (alloc2 != NULL)
2699 {
2700 if (keep_memory)
2701 bfd_release (abfd, alloc2);
2702 else
2703 free (alloc2);
2704 }
2705 return NULL;
2706 }
2707
2708 /* Compute the size of, and allocate space for, REL_HDR which is the
2709 section header for a section containing relocations for O. */
2710
2711 static bfd_boolean
2712 _bfd_elf_link_size_reloc_section (bfd *abfd,
2713 struct bfd_elf_section_reloc_data *reldata)
2714 {
2715 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2716
2717 /* That allows us to calculate the size of the section. */
2718 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2719
2720 /* The contents field must last into write_object_contents, so we
2721 allocate it with bfd_alloc rather than malloc. Also since we
2722 cannot be sure that the contents will actually be filled in,
2723 we zero the allocated space. */
2724 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2725 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2726 return FALSE;
2727
2728 if (reldata->hashes == NULL && reldata->count)
2729 {
2730 struct elf_link_hash_entry **p;
2731
2732 p = ((struct elf_link_hash_entry **)
2733 bfd_zmalloc (reldata->count * sizeof (*p)));
2734 if (p == NULL)
2735 return FALSE;
2736
2737 reldata->hashes = p;
2738 }
2739
2740 return TRUE;
2741 }
2742
2743 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2744 originated from the section given by INPUT_REL_HDR) to the
2745 OUTPUT_BFD. */
2746
2747 bfd_boolean
2748 _bfd_elf_link_output_relocs (bfd *output_bfd,
2749 asection *input_section,
2750 Elf_Internal_Shdr *input_rel_hdr,
2751 Elf_Internal_Rela *internal_relocs,
2752 struct elf_link_hash_entry **rel_hash
2753 ATTRIBUTE_UNUSED)
2754 {
2755 Elf_Internal_Rela *irela;
2756 Elf_Internal_Rela *irelaend;
2757 bfd_byte *erel;
2758 struct bfd_elf_section_reloc_data *output_reldata;
2759 asection *output_section;
2760 const struct elf_backend_data *bed;
2761 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2762 struct bfd_elf_section_data *esdo;
2763
2764 output_section = input_section->output_section;
2765
2766 bed = get_elf_backend_data (output_bfd);
2767 esdo = elf_section_data (output_section);
2768 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2769 {
2770 output_reldata = &esdo->rel;
2771 swap_out = bed->s->swap_reloc_out;
2772 }
2773 else if (esdo->rela.hdr
2774 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2775 {
2776 output_reldata = &esdo->rela;
2777 swap_out = bed->s->swap_reloca_out;
2778 }
2779 else
2780 {
2781 _bfd_error_handler
2782 /* xgettext:c-format */
2783 (_("%pB: relocation size mismatch in %pB section %pA"),
2784 output_bfd, input_section->owner, input_section);
2785 bfd_set_error (bfd_error_wrong_format);
2786 return FALSE;
2787 }
2788
2789 erel = output_reldata->hdr->contents;
2790 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2791 irela = internal_relocs;
2792 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2793 * bed->s->int_rels_per_ext_rel);
2794 while (irela < irelaend)
2795 {
2796 (*swap_out) (output_bfd, irela, erel);
2797 irela += bed->s->int_rels_per_ext_rel;
2798 erel += input_rel_hdr->sh_entsize;
2799 }
2800
2801 /* Bump the counter, so that we know where to add the next set of
2802 relocations. */
2803 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2804
2805 return TRUE;
2806 }
2807 \f
2808 /* Make weak undefined symbols in PIE dynamic. */
2809
2810 bfd_boolean
2811 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2812 struct elf_link_hash_entry *h)
2813 {
2814 if (bfd_link_pie (info)
2815 && h->dynindx == -1
2816 && h->root.type == bfd_link_hash_undefweak)
2817 return bfd_elf_link_record_dynamic_symbol (info, h);
2818
2819 return TRUE;
2820 }
2821
2822 /* Fix up the flags for a symbol. This handles various cases which
2823 can only be fixed after all the input files are seen. This is
2824 currently called by both adjust_dynamic_symbol and
2825 assign_sym_version, which is unnecessary but perhaps more robust in
2826 the face of future changes. */
2827
2828 static bfd_boolean
2829 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2830 struct elf_info_failed *eif)
2831 {
2832 const struct elf_backend_data *bed;
2833
2834 /* If this symbol was mentioned in a non-ELF file, try to set
2835 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2836 permit a non-ELF file to correctly refer to a symbol defined in
2837 an ELF dynamic object. */
2838 if (h->non_elf)
2839 {
2840 while (h->root.type == bfd_link_hash_indirect)
2841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2842
2843 if (h->root.type != bfd_link_hash_defined
2844 && h->root.type != bfd_link_hash_defweak)
2845 {
2846 h->ref_regular = 1;
2847 h->ref_regular_nonweak = 1;
2848 }
2849 else
2850 {
2851 if (h->root.u.def.section->owner != NULL
2852 && (bfd_get_flavour (h->root.u.def.section->owner)
2853 == bfd_target_elf_flavour))
2854 {
2855 h->ref_regular = 1;
2856 h->ref_regular_nonweak = 1;
2857 }
2858 else
2859 h->def_regular = 1;
2860 }
2861
2862 if (h->dynindx == -1
2863 && (h->def_dynamic
2864 || h->ref_dynamic))
2865 {
2866 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2867 {
2868 eif->failed = TRUE;
2869 return FALSE;
2870 }
2871 }
2872 }
2873 else
2874 {
2875 /* Unfortunately, NON_ELF is only correct if the symbol
2876 was first seen in a non-ELF file. Fortunately, if the symbol
2877 was first seen in an ELF file, we're probably OK unless the
2878 symbol was defined in a non-ELF file. Catch that case here.
2879 FIXME: We're still in trouble if the symbol was first seen in
2880 a dynamic object, and then later in a non-ELF regular object. */
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
2883 && !h->def_regular
2884 && (h->root.u.def.section->owner != NULL
2885 ? (bfd_get_flavour (h->root.u.def.section->owner)
2886 != bfd_target_elf_flavour)
2887 : (bfd_is_abs_section (h->root.u.def.section)
2888 && !h->def_dynamic)))
2889 h->def_regular = 1;
2890 }
2891
2892 /* Backend specific symbol fixup. */
2893 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2894 if (bed->elf_backend_fixup_symbol
2895 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2896 return FALSE;
2897
2898 /* If this is a final link, and the symbol was defined as a common
2899 symbol in a regular object file, and there was no definition in
2900 any dynamic object, then the linker will have allocated space for
2901 the symbol in a common section but the DEF_REGULAR
2902 flag will not have been set. */
2903 if (h->root.type == bfd_link_hash_defined
2904 && !h->def_regular
2905 && h->ref_regular
2906 && !h->def_dynamic
2907 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2908 h->def_regular = 1;
2909
2910 /* Symbols defined in discarded sections shouldn't be dynamic. */
2911 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2913
2914 /* If a weak undefined symbol has non-default visibility, we also
2915 hide it from the dynamic linker. */
2916 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2917 && h->root.type == bfd_link_hash_undefweak)
2918 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2919
2920 /* A hidden versioned symbol in executable should be forced local if
2921 it is is locally defined, not referenced by shared library and not
2922 exported. */
2923 else if (bfd_link_executable (eif->info)
2924 && h->versioned == versioned_hidden
2925 && !eif->info->export_dynamic
2926 && !h->dynamic
2927 && !h->ref_dynamic
2928 && h->def_regular)
2929 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2930
2931 /* If -Bsymbolic was used (which means to bind references to global
2932 symbols to the definition within the shared object), and this
2933 symbol was defined in a regular object, then it actually doesn't
2934 need a PLT entry. Likewise, if the symbol has non-default
2935 visibility. If the symbol has hidden or internal visibility, we
2936 will force it local. */
2937 else if (h->needs_plt
2938 && bfd_link_pic (eif->info)
2939 && is_elf_hash_table (eif->info->hash)
2940 && (SYMBOLIC_BIND (eif->info, h)
2941 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2942 && h->def_regular)
2943 {
2944 bfd_boolean force_local;
2945
2946 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2947 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2948 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2949 }
2950
2951 /* If this is a weak defined symbol in a dynamic object, and we know
2952 the real definition in the dynamic object, copy interesting flags
2953 over to the real definition. */
2954 if (h->is_weakalias)
2955 {
2956 struct elf_link_hash_entry *def = weakdef (h);
2957
2958 /* If the real definition is defined by a regular object file,
2959 don't do anything special. See the longer description in
2960 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2961 bfd_link_hash_defined as it was when put on the alias list
2962 then it must have originally been a versioned symbol (for
2963 which a non-versioned indirect symbol is created) and later
2964 a definition for the non-versioned symbol is found. In that
2965 case the indirection is flipped with the versioned symbol
2966 becoming an indirect pointing at the non-versioned symbol.
2967 Thus, not an alias any more. */
2968 if (def->def_regular
2969 || def->root.type != bfd_link_hash_defined)
2970 {
2971 h = def;
2972 while ((h = h->u.alias) != def)
2973 h->is_weakalias = 0;
2974 }
2975 else
2976 {
2977 while (h->root.type == bfd_link_hash_indirect)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2979 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2980 || h->root.type == bfd_link_hash_defweak);
2981 BFD_ASSERT (def->def_dynamic);
2982 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2983 }
2984 }
2985
2986 return TRUE;
2987 }
2988
2989 /* Make the backend pick a good value for a dynamic symbol. This is
2990 called via elf_link_hash_traverse, and also calls itself
2991 recursively. */
2992
2993 static bfd_boolean
2994 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2995 {
2996 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2997 struct elf_link_hash_table *htab;
2998 const struct elf_backend_data *bed;
2999
3000 if (! is_elf_hash_table (eif->info->hash))
3001 return FALSE;
3002
3003 /* Ignore indirect symbols. These are added by the versioning code. */
3004 if (h->root.type == bfd_link_hash_indirect)
3005 return TRUE;
3006
3007 /* Fix the symbol flags. */
3008 if (! _bfd_elf_fix_symbol_flags (h, eif))
3009 return FALSE;
3010
3011 htab = elf_hash_table (eif->info);
3012 bed = get_elf_backend_data (htab->dynobj);
3013
3014 if (h->root.type == bfd_link_hash_undefweak)
3015 {
3016 if (eif->info->dynamic_undefined_weak == 0)
3017 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
3018 else if (eif->info->dynamic_undefined_weak > 0
3019 && h->ref_regular
3020 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3021 && !bfd_hide_sym_by_version (eif->info->version_info,
3022 h->root.root.string))
3023 {
3024 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3025 {
3026 eif->failed = TRUE;
3027 return FALSE;
3028 }
3029 }
3030 }
3031
3032 /* If this symbol does not require a PLT entry, and it is not
3033 defined by a dynamic object, or is not referenced by a regular
3034 object, ignore it. We do have to handle a weak defined symbol,
3035 even if no regular object refers to it, if we decided to add it
3036 to the dynamic symbol table. FIXME: Do we normally need to worry
3037 about symbols which are defined by one dynamic object and
3038 referenced by another one? */
3039 if (!h->needs_plt
3040 && h->type != STT_GNU_IFUNC
3041 && (h->def_regular
3042 || !h->def_dynamic
3043 || (!h->ref_regular
3044 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3045 {
3046 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3047 return TRUE;
3048 }
3049
3050 /* If we've already adjusted this symbol, don't do it again. This
3051 can happen via a recursive call. */
3052 if (h->dynamic_adjusted)
3053 return TRUE;
3054
3055 /* Don't look at this symbol again. Note that we must set this
3056 after checking the above conditions, because we may look at a
3057 symbol once, decide not to do anything, and then get called
3058 recursively later after REF_REGULAR is set below. */
3059 h->dynamic_adjusted = 1;
3060
3061 /* If this is a weak definition, and we know a real definition, and
3062 the real symbol is not itself defined by a regular object file,
3063 then get a good value for the real definition. We handle the
3064 real symbol first, for the convenience of the backend routine.
3065
3066 Note that there is a confusing case here. If the real definition
3067 is defined by a regular object file, we don't get the real symbol
3068 from the dynamic object, but we do get the weak symbol. If the
3069 processor backend uses a COPY reloc, then if some routine in the
3070 dynamic object changes the real symbol, we will not see that
3071 change in the corresponding weak symbol. This is the way other
3072 ELF linkers work as well, and seems to be a result of the shared
3073 library model.
3074
3075 I will clarify this issue. Most SVR4 shared libraries define the
3076 variable _timezone and define timezone as a weak synonym. The
3077 tzset call changes _timezone. If you write
3078 extern int timezone;
3079 int _timezone = 5;
3080 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3081 you might expect that, since timezone is a synonym for _timezone,
3082 the same number will print both times. However, if the processor
3083 backend uses a COPY reloc, then actually timezone will be copied
3084 into your process image, and, since you define _timezone
3085 yourself, _timezone will not. Thus timezone and _timezone will
3086 wind up at different memory locations. The tzset call will set
3087 _timezone, leaving timezone unchanged. */
3088
3089 if (h->is_weakalias)
3090 {
3091 struct elf_link_hash_entry *def = weakdef (h);
3092
3093 /* If we get to this point, there is an implicit reference to
3094 the alias by a regular object file via the weak symbol H. */
3095 def->ref_regular = 1;
3096
3097 /* Ensure that the backend adjust_dynamic_symbol function sees
3098 the strong alias before H by recursively calling ourselves. */
3099 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3100 return FALSE;
3101 }
3102
3103 /* If a symbol has no type and no size and does not require a PLT
3104 entry, then we are probably about to do the wrong thing here: we
3105 are probably going to create a COPY reloc for an empty object.
3106 This case can arise when a shared object is built with assembly
3107 code, and the assembly code fails to set the symbol type. */
3108 if (h->size == 0
3109 && h->type == STT_NOTYPE
3110 && !h->needs_plt)
3111 _bfd_error_handler
3112 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3113 h->root.root.string);
3114
3115 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3116 {
3117 eif->failed = TRUE;
3118 return FALSE;
3119 }
3120
3121 return TRUE;
3122 }
3123
3124 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3125 DYNBSS. */
3126
3127 bfd_boolean
3128 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3129 struct elf_link_hash_entry *h,
3130 asection *dynbss)
3131 {
3132 unsigned int power_of_two;
3133 bfd_vma mask;
3134 asection *sec = h->root.u.def.section;
3135
3136 /* The section alignment of the definition is the maximum alignment
3137 requirement of symbols defined in the section. Since we don't
3138 know the symbol alignment requirement, we start with the
3139 maximum alignment and check low bits of the symbol address
3140 for the minimum alignment. */
3141 power_of_two = bfd_section_alignment (sec);
3142 mask = ((bfd_vma) 1 << power_of_two) - 1;
3143 while ((h->root.u.def.value & mask) != 0)
3144 {
3145 mask >>= 1;
3146 --power_of_two;
3147 }
3148
3149 if (power_of_two > bfd_section_alignment (dynbss))
3150 {
3151 /* Adjust the section alignment if needed. */
3152 if (!bfd_set_section_alignment (dynbss, power_of_two))
3153 return FALSE;
3154 }
3155
3156 /* We make sure that the symbol will be aligned properly. */
3157 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3158
3159 /* Define the symbol as being at this point in DYNBSS. */
3160 h->root.u.def.section = dynbss;
3161 h->root.u.def.value = dynbss->size;
3162
3163 /* Increment the size of DYNBSS to make room for the symbol. */
3164 dynbss->size += h->size;
3165
3166 /* No error if extern_protected_data is true. */
3167 if (h->protected_def
3168 && (!info->extern_protected_data
3169 || (info->extern_protected_data < 0
3170 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3171 info->callbacks->einfo
3172 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3173 h->root.root.string);
3174
3175 return TRUE;
3176 }
3177
3178 /* Adjust all external symbols pointing into SEC_MERGE sections
3179 to reflect the object merging within the sections. */
3180
3181 static bfd_boolean
3182 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3183 {
3184 asection *sec;
3185
3186 if ((h->root.type == bfd_link_hash_defined
3187 || h->root.type == bfd_link_hash_defweak)
3188 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3189 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3190 {
3191 bfd *output_bfd = (bfd *) data;
3192
3193 h->root.u.def.value =
3194 _bfd_merged_section_offset (output_bfd,
3195 &h->root.u.def.section,
3196 elf_section_data (sec)->sec_info,
3197 h->root.u.def.value);
3198 }
3199
3200 return TRUE;
3201 }
3202
3203 /* Returns false if the symbol referred to by H should be considered
3204 to resolve local to the current module, and true if it should be
3205 considered to bind dynamically. */
3206
3207 bfd_boolean
3208 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3209 struct bfd_link_info *info,
3210 bfd_boolean not_local_protected)
3211 {
3212 bfd_boolean binding_stays_local_p;
3213 const struct elf_backend_data *bed;
3214 struct elf_link_hash_table *hash_table;
3215
3216 if (h == NULL)
3217 return FALSE;
3218
3219 while (h->root.type == bfd_link_hash_indirect
3220 || h->root.type == bfd_link_hash_warning)
3221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3222
3223 /* If it was forced local, then clearly it's not dynamic. */
3224 if (h->dynindx == -1)
3225 return FALSE;
3226 if (h->forced_local)
3227 return FALSE;
3228
3229 /* Identify the cases where name binding rules say that a
3230 visible symbol resolves locally. */
3231 binding_stays_local_p = (bfd_link_executable (info)
3232 || SYMBOLIC_BIND (info, h));
3233
3234 switch (ELF_ST_VISIBILITY (h->other))
3235 {
3236 case STV_INTERNAL:
3237 case STV_HIDDEN:
3238 return FALSE;
3239
3240 case STV_PROTECTED:
3241 hash_table = elf_hash_table (info);
3242 if (!is_elf_hash_table (hash_table))
3243 return FALSE;
3244
3245 bed = get_elf_backend_data (hash_table->dynobj);
3246
3247 /* Proper resolution for function pointer equality may require
3248 that these symbols perhaps be resolved dynamically, even though
3249 we should be resolving them to the current module. */
3250 if (!not_local_protected || !bed->is_function_type (h->type))
3251 binding_stays_local_p = TRUE;
3252 break;
3253
3254 default:
3255 break;
3256 }
3257
3258 /* If it isn't defined locally, then clearly it's dynamic. */
3259 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3260 return TRUE;
3261
3262 /* Otherwise, the symbol is dynamic if binding rules don't tell
3263 us that it remains local. */
3264 return !binding_stays_local_p;
3265 }
3266
3267 /* Return true if the symbol referred to by H should be considered
3268 to resolve local to the current module, and false otherwise. Differs
3269 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3270 undefined symbols. The two functions are virtually identical except
3271 for the place where dynindx == -1 is tested. If that test is true,
3272 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3273 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3274 defined symbols.
3275 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3276 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3277 treatment of undefined weak symbols. For those that do not make
3278 undefined weak symbols dynamic, both functions may return false. */
3279
3280 bfd_boolean
3281 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3282 struct bfd_link_info *info,
3283 bfd_boolean local_protected)
3284 {
3285 const struct elf_backend_data *bed;
3286 struct elf_link_hash_table *hash_table;
3287
3288 /* If it's a local sym, of course we resolve locally. */
3289 if (h == NULL)
3290 return TRUE;
3291
3292 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3293 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3294 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3295 return TRUE;
3296
3297 /* Forced local symbols resolve locally. */
3298 if (h->forced_local)
3299 return TRUE;
3300
3301 /* Common symbols that become definitions don't get the DEF_REGULAR
3302 flag set, so test it first, and don't bail out. */
3303 if (ELF_COMMON_DEF_P (h))
3304 /* Do nothing. */;
3305 /* If we don't have a definition in a regular file, then we can't
3306 resolve locally. The sym is either undefined or dynamic. */
3307 else if (!h->def_regular)
3308 return FALSE;
3309
3310 /* Non-dynamic symbols resolve locally. */
3311 if (h->dynindx == -1)
3312 return TRUE;
3313
3314 /* At this point, we know the symbol is defined and dynamic. In an
3315 executable it must resolve locally, likewise when building symbolic
3316 shared libraries. */
3317 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3318 return TRUE;
3319
3320 /* Now deal with defined dynamic symbols in shared libraries. Ones
3321 with default visibility might not resolve locally. */
3322 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3323 return FALSE;
3324
3325 hash_table = elf_hash_table (info);
3326 if (!is_elf_hash_table (hash_table))
3327 return TRUE;
3328
3329 bed = get_elf_backend_data (hash_table->dynobj);
3330
3331 /* If extern_protected_data is false, STV_PROTECTED non-function
3332 symbols are local. */
3333 if ((!info->extern_protected_data
3334 || (info->extern_protected_data < 0
3335 && !bed->extern_protected_data))
3336 && !bed->is_function_type (h->type))
3337 return TRUE;
3338
3339 /* Function pointer equality tests may require that STV_PROTECTED
3340 symbols be treated as dynamic symbols. If the address of a
3341 function not defined in an executable is set to that function's
3342 plt entry in the executable, then the address of the function in
3343 a shared library must also be the plt entry in the executable. */
3344 return local_protected;
3345 }
3346
3347 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3348 aligned. Returns the first TLS output section. */
3349
3350 struct bfd_section *
3351 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3352 {
3353 struct bfd_section *sec, *tls;
3354 unsigned int align = 0;
3355
3356 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3357 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3358 break;
3359 tls = sec;
3360
3361 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3362 if (sec->alignment_power > align)
3363 align = sec->alignment_power;
3364
3365 elf_hash_table (info)->tls_sec = tls;
3366
3367 /* Ensure the alignment of the first section (usually .tdata) is the largest
3368 alignment, so that the tls segment starts aligned. */
3369 if (tls != NULL)
3370 tls->alignment_power = align;
3371
3372 return tls;
3373 }
3374
3375 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3376 static bfd_boolean
3377 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3378 Elf_Internal_Sym *sym)
3379 {
3380 const struct elf_backend_data *bed;
3381
3382 /* Local symbols do not count, but target specific ones might. */
3383 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3384 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3385 return FALSE;
3386
3387 bed = get_elf_backend_data (abfd);
3388 /* Function symbols do not count. */
3389 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3390 return FALSE;
3391
3392 /* If the section is undefined, then so is the symbol. */
3393 if (sym->st_shndx == SHN_UNDEF)
3394 return FALSE;
3395
3396 /* If the symbol is defined in the common section, then
3397 it is a common definition and so does not count. */
3398 if (bed->common_definition (sym))
3399 return FALSE;
3400
3401 /* If the symbol is in a target specific section then we
3402 must rely upon the backend to tell us what it is. */
3403 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3404 /* FIXME - this function is not coded yet:
3405
3406 return _bfd_is_global_symbol_definition (abfd, sym);
3407
3408 Instead for now assume that the definition is not global,
3409 Even if this is wrong, at least the linker will behave
3410 in the same way that it used to do. */
3411 return FALSE;
3412
3413 return TRUE;
3414 }
3415
3416 /* Search the symbol table of the archive element of the archive ABFD
3417 whose archive map contains a mention of SYMDEF, and determine if
3418 the symbol is defined in this element. */
3419 static bfd_boolean
3420 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3421 {
3422 Elf_Internal_Shdr * hdr;
3423 size_t symcount;
3424 size_t extsymcount;
3425 size_t extsymoff;
3426 Elf_Internal_Sym *isymbuf;
3427 Elf_Internal_Sym *isym;
3428 Elf_Internal_Sym *isymend;
3429 bfd_boolean result;
3430
3431 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3432 if (abfd == NULL)
3433 return FALSE;
3434
3435 if (! bfd_check_format (abfd, bfd_object))
3436 return FALSE;
3437
3438 /* Select the appropriate symbol table. If we don't know if the
3439 object file is an IR object, give linker LTO plugin a chance to
3440 get the correct symbol table. */
3441 if (abfd->plugin_format == bfd_plugin_yes
3442 #if BFD_SUPPORTS_PLUGINS
3443 || (abfd->plugin_format == bfd_plugin_unknown
3444 && bfd_link_plugin_object_p (abfd))
3445 #endif
3446 )
3447 {
3448 /* Use the IR symbol table if the object has been claimed by
3449 plugin. */
3450 abfd = abfd->plugin_dummy_bfd;
3451 hdr = &elf_tdata (abfd)->symtab_hdr;
3452 }
3453 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3454 hdr = &elf_tdata (abfd)->symtab_hdr;
3455 else
3456 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3457
3458 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3459
3460 /* The sh_info field of the symtab header tells us where the
3461 external symbols start. We don't care about the local symbols. */
3462 if (elf_bad_symtab (abfd))
3463 {
3464 extsymcount = symcount;
3465 extsymoff = 0;
3466 }
3467 else
3468 {
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3471 }
3472
3473 if (extsymcount == 0)
3474 return FALSE;
3475
3476 /* Read in the symbol table. */
3477 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3478 NULL, NULL, NULL);
3479 if (isymbuf == NULL)
3480 return FALSE;
3481
3482 /* Scan the symbol table looking for SYMDEF. */
3483 result = FALSE;
3484 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3485 {
3486 const char *name;
3487
3488 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3489 isym->st_name);
3490 if (name == NULL)
3491 break;
3492
3493 if (strcmp (name, symdef->name) == 0)
3494 {
3495 result = is_global_data_symbol_definition (abfd, isym);
3496 break;
3497 }
3498 }
3499
3500 free (isymbuf);
3501
3502 return result;
3503 }
3504 \f
3505 /* Add an entry to the .dynamic table. */
3506
3507 bfd_boolean
3508 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3509 bfd_vma tag,
3510 bfd_vma val)
3511 {
3512 struct elf_link_hash_table *hash_table;
3513 const struct elf_backend_data *bed;
3514 asection *s;
3515 bfd_size_type newsize;
3516 bfd_byte *newcontents;
3517 Elf_Internal_Dyn dyn;
3518
3519 hash_table = elf_hash_table (info);
3520 if (! is_elf_hash_table (hash_table))
3521 return FALSE;
3522
3523 if (tag == DT_RELA || tag == DT_REL)
3524 hash_table->dynamic_relocs = TRUE;
3525
3526 bed = get_elf_backend_data (hash_table->dynobj);
3527 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3528 BFD_ASSERT (s != NULL);
3529
3530 newsize = s->size + bed->s->sizeof_dyn;
3531 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3532 if (newcontents == NULL)
3533 return FALSE;
3534
3535 dyn.d_tag = tag;
3536 dyn.d_un.d_val = val;
3537 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3538
3539 s->size = newsize;
3540 s->contents = newcontents;
3541
3542 return TRUE;
3543 }
3544
3545 /* Strip zero-sized dynamic sections. */
3546
3547 bfd_boolean
3548 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3549 {
3550 struct elf_link_hash_table *hash_table;
3551 const struct elf_backend_data *bed;
3552 asection *s, *sdynamic, **pp;
3553 asection *rela_dyn, *rel_dyn;
3554 Elf_Internal_Dyn dyn;
3555 bfd_byte *extdyn, *next;
3556 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3557 bfd_boolean strip_zero_sized;
3558 bfd_boolean strip_zero_sized_plt;
3559
3560 if (bfd_link_relocatable (info))
3561 return TRUE;
3562
3563 hash_table = elf_hash_table (info);
3564 if (!is_elf_hash_table (hash_table))
3565 return FALSE;
3566
3567 if (!hash_table->dynobj)
3568 return TRUE;
3569
3570 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3571 if (!sdynamic)
3572 return TRUE;
3573
3574 bed = get_elf_backend_data (hash_table->dynobj);
3575 swap_dyn_in = bed->s->swap_dyn_in;
3576
3577 strip_zero_sized = FALSE;
3578 strip_zero_sized_plt = FALSE;
3579
3580 /* Strip zero-sized dynamic sections. */
3581 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3582 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3583 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3584 if (s->size == 0
3585 && (s == rela_dyn
3586 || s == rel_dyn
3587 || s == hash_table->srelplt->output_section
3588 || s == hash_table->splt->output_section))
3589 {
3590 *pp = s->next;
3591 info->output_bfd->section_count--;
3592 strip_zero_sized = TRUE;
3593 if (s == rela_dyn)
3594 s = rela_dyn;
3595 if (s == rel_dyn)
3596 s = rel_dyn;
3597 else if (s == hash_table->splt->output_section)
3598 {
3599 s = hash_table->splt;
3600 strip_zero_sized_plt = TRUE;
3601 }
3602 else
3603 s = hash_table->srelplt;
3604 s->flags |= SEC_EXCLUDE;
3605 s->output_section = bfd_abs_section_ptr;
3606 }
3607 else
3608 pp = &s->next;
3609
3610 if (strip_zero_sized_plt)
3611 for (extdyn = sdynamic->contents;
3612 extdyn < sdynamic->contents + sdynamic->size;
3613 extdyn = next)
3614 {
3615 next = extdyn + bed->s->sizeof_dyn;
3616 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3617 switch (dyn.d_tag)
3618 {
3619 default:
3620 break;
3621 case DT_JMPREL:
3622 case DT_PLTRELSZ:
3623 case DT_PLTREL:
3624 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3625 the procedure linkage table (the .plt section) has been
3626 removed. */
3627 memmove (extdyn, next,
3628 sdynamic->size - (next - sdynamic->contents));
3629 next = extdyn;
3630 }
3631 }
3632
3633 if (strip_zero_sized)
3634 {
3635 /* Regenerate program headers. */
3636 elf_seg_map (info->output_bfd) = NULL;
3637 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3638 }
3639
3640 return TRUE;
3641 }
3642
3643 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3644 1 if a DT_NEEDED tag already exists, and 0 on success. */
3645
3646 int
3647 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3648 {
3649 struct elf_link_hash_table *hash_table;
3650 size_t strindex;
3651 const char *soname;
3652
3653 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3654 return -1;
3655
3656 hash_table = elf_hash_table (info);
3657 soname = elf_dt_name (abfd);
3658 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3659 if (strindex == (size_t) -1)
3660 return -1;
3661
3662 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3663 {
3664 asection *sdyn;
3665 const struct elf_backend_data *bed;
3666 bfd_byte *extdyn;
3667
3668 bed = get_elf_backend_data (hash_table->dynobj);
3669 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3670 if (sdyn != NULL)
3671 for (extdyn = sdyn->contents;
3672 extdyn < sdyn->contents + sdyn->size;
3673 extdyn += bed->s->sizeof_dyn)
3674 {
3675 Elf_Internal_Dyn dyn;
3676
3677 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3678 if (dyn.d_tag == DT_NEEDED
3679 && dyn.d_un.d_val == strindex)
3680 {
3681 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3682 return 1;
3683 }
3684 }
3685 }
3686
3687 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3688 return -1;
3689
3690 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3691 return -1;
3692
3693 return 0;
3694 }
3695
3696 /* Return true if SONAME is on the needed list between NEEDED and STOP
3697 (or the end of list if STOP is NULL), and needed by a library that
3698 will be loaded. */
3699
3700 static bfd_boolean
3701 on_needed_list (const char *soname,
3702 struct bfd_link_needed_list *needed,
3703 struct bfd_link_needed_list *stop)
3704 {
3705 struct bfd_link_needed_list *look;
3706 for (look = needed; look != stop; look = look->next)
3707 if (strcmp (soname, look->name) == 0
3708 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3709 /* If needed by a library that itself is not directly
3710 needed, recursively check whether that library is
3711 indirectly needed. Since we add DT_NEEDED entries to
3712 the end of the list, library dependencies appear after
3713 the library. Therefore search prior to the current
3714 LOOK, preventing possible infinite recursion. */
3715 || on_needed_list (elf_dt_name (look->by), needed, look)))
3716 return TRUE;
3717
3718 return FALSE;
3719 }
3720
3721 /* Sort symbol by value, section, size, and type. */
3722 static int
3723 elf_sort_symbol (const void *arg1, const void *arg2)
3724 {
3725 const struct elf_link_hash_entry *h1;
3726 const struct elf_link_hash_entry *h2;
3727 bfd_signed_vma vdiff;
3728 int sdiff;
3729 const char *n1;
3730 const char *n2;
3731
3732 h1 = *(const struct elf_link_hash_entry **) arg1;
3733 h2 = *(const struct elf_link_hash_entry **) arg2;
3734 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3735 if (vdiff != 0)
3736 return vdiff > 0 ? 1 : -1;
3737
3738 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3739 if (sdiff != 0)
3740 return sdiff;
3741
3742 /* Sort so that sized symbols are selected over zero size symbols. */
3743 vdiff = h1->size - h2->size;
3744 if (vdiff != 0)
3745 return vdiff > 0 ? 1 : -1;
3746
3747 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3748 if (h1->type != h2->type)
3749 return h1->type - h2->type;
3750
3751 /* If symbols are properly sized and typed, and multiple strong
3752 aliases are not defined in a shared library by the user we
3753 shouldn't get here. Unfortunately linker script symbols like
3754 __bss_start sometimes match a user symbol defined at the start of
3755 .bss without proper size and type. We'd like to preference the
3756 user symbol over reserved system symbols. Sort on leading
3757 underscores. */
3758 n1 = h1->root.root.string;
3759 n2 = h2->root.root.string;
3760 while (*n1 == *n2)
3761 {
3762 if (*n1 == 0)
3763 break;
3764 ++n1;
3765 ++n2;
3766 }
3767 if (*n1 == '_')
3768 return -1;
3769 if (*n2 == '_')
3770 return 1;
3771
3772 /* Final sort on name selects user symbols like '_u' over reserved
3773 system symbols like '_Z' and also will avoid qsort instability. */
3774 return *n1 - *n2;
3775 }
3776
3777 /* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3779
3780 static bfd_boolean
3781 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3782 {
3783 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3784
3785 if (h->dynindx != -1)
3786 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3787 return TRUE;
3788 }
3789
3790 /* Assign string offsets in .dynstr, update all structures referencing
3791 them. */
3792
3793 static bfd_boolean
3794 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3795 {
3796 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3797 struct elf_link_local_dynamic_entry *entry;
3798 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3799 bfd *dynobj = hash_table->dynobj;
3800 asection *sdyn;
3801 bfd_size_type size;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3804
3805 _bfd_elf_strtab_finalize (dynstr);
3806 size = _bfd_elf_strtab_size (dynstr);
3807
3808 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3809
3810 if (info->callbacks->examine_strtab)
3811 info->callbacks->examine_strtab (dynstr);
3812
3813 bed = get_elf_backend_data (dynobj);
3814 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3815 BFD_ASSERT (sdyn != NULL);
3816
3817 /* Update all .dynamic entries referencing .dynstr strings. */
3818 for (extdyn = sdyn->contents;
3819 extdyn < sdyn->contents + sdyn->size;
3820 extdyn += bed->s->sizeof_dyn)
3821 {
3822 Elf_Internal_Dyn dyn;
3823
3824 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3825 switch (dyn.d_tag)
3826 {
3827 case DT_STRSZ:
3828 dyn.d_un.d_val = size;
3829 break;
3830 case DT_NEEDED:
3831 case DT_SONAME:
3832 case DT_RPATH:
3833 case DT_RUNPATH:
3834 case DT_FILTER:
3835 case DT_AUXILIARY:
3836 case DT_AUDIT:
3837 case DT_DEPAUDIT:
3838 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3839 break;
3840 default:
3841 continue;
3842 }
3843 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3844 }
3845
3846 /* Now update local dynamic symbols. */
3847 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3848 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3849 entry->isym.st_name);
3850
3851 /* And the rest of dynamic symbols. */
3852 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3853
3854 /* Adjust version definitions. */
3855 if (elf_tdata (output_bfd)->cverdefs)
3856 {
3857 asection *s;
3858 bfd_byte *p;
3859 size_t i;
3860 Elf_Internal_Verdef def;
3861 Elf_Internal_Verdaux defaux;
3862
3863 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3864 p = s->contents;
3865 do
3866 {
3867 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3868 &def);
3869 p += sizeof (Elf_External_Verdef);
3870 if (def.vd_aux != sizeof (Elf_External_Verdef))
3871 continue;
3872 for (i = 0; i < def.vd_cnt; ++i)
3873 {
3874 _bfd_elf_swap_verdaux_in (output_bfd,
3875 (Elf_External_Verdaux *) p, &defaux);
3876 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3877 defaux.vda_name);
3878 _bfd_elf_swap_verdaux_out (output_bfd,
3879 &defaux, (Elf_External_Verdaux *) p);
3880 p += sizeof (Elf_External_Verdaux);
3881 }
3882 }
3883 while (def.vd_next);
3884 }
3885
3886 /* Adjust version references. */
3887 if (elf_tdata (output_bfd)->verref)
3888 {
3889 asection *s;
3890 bfd_byte *p;
3891 size_t i;
3892 Elf_Internal_Verneed need;
3893 Elf_Internal_Vernaux needaux;
3894
3895 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3896 p = s->contents;
3897 do
3898 {
3899 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3900 &need);
3901 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3902 _bfd_elf_swap_verneed_out (output_bfd, &need,
3903 (Elf_External_Verneed *) p);
3904 p += sizeof (Elf_External_Verneed);
3905 for (i = 0; i < need.vn_cnt; ++i)
3906 {
3907 _bfd_elf_swap_vernaux_in (output_bfd,
3908 (Elf_External_Vernaux *) p, &needaux);
3909 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3910 needaux.vna_name);
3911 _bfd_elf_swap_vernaux_out (output_bfd,
3912 &needaux,
3913 (Elf_External_Vernaux *) p);
3914 p += sizeof (Elf_External_Vernaux);
3915 }
3916 }
3917 while (need.vn_next);
3918 }
3919
3920 return TRUE;
3921 }
3922 \f
3923 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3924 The default is to only match when the INPUT and OUTPUT are exactly
3925 the same target. */
3926
3927 bfd_boolean
3928 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3929 const bfd_target *output)
3930 {
3931 return input == output;
3932 }
3933
3934 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3935 This version is used when different targets for the same architecture
3936 are virtually identical. */
3937
3938 bfd_boolean
3939 _bfd_elf_relocs_compatible (const bfd_target *input,
3940 const bfd_target *output)
3941 {
3942 const struct elf_backend_data *obed, *ibed;
3943
3944 if (input == output)
3945 return TRUE;
3946
3947 ibed = xvec_get_elf_backend_data (input);
3948 obed = xvec_get_elf_backend_data (output);
3949
3950 if (ibed->arch != obed->arch)
3951 return FALSE;
3952
3953 /* If both backends are using this function, deem them compatible. */
3954 return ibed->relocs_compatible == obed->relocs_compatible;
3955 }
3956
3957 /* Make a special call to the linker "notice" function to tell it that
3958 we are about to handle an as-needed lib, or have finished
3959 processing the lib. */
3960
3961 bfd_boolean
3962 _bfd_elf_notice_as_needed (bfd *ibfd,
3963 struct bfd_link_info *info,
3964 enum notice_asneeded_action act)
3965 {
3966 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3967 }
3968
3969 /* Check relocations an ELF object file. */
3970
3971 bfd_boolean
3972 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3973 {
3974 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3975 struct elf_link_hash_table *htab = elf_hash_table (info);
3976
3977 /* If this object is the same format as the output object, and it is
3978 not a shared library, then let the backend look through the
3979 relocs.
3980
3981 This is required to build global offset table entries and to
3982 arrange for dynamic relocs. It is not required for the
3983 particular common case of linking non PIC code, even when linking
3984 against shared libraries, but unfortunately there is no way of
3985 knowing whether an object file has been compiled PIC or not.
3986 Looking through the relocs is not particularly time consuming.
3987 The problem is that we must either (1) keep the relocs in memory,
3988 which causes the linker to require additional runtime memory or
3989 (2) read the relocs twice from the input file, which wastes time.
3990 This would be a good case for using mmap.
3991
3992 I have no idea how to handle linking PIC code into a file of a
3993 different format. It probably can't be done. */
3994 if ((abfd->flags & DYNAMIC) == 0
3995 && is_elf_hash_table (htab)
3996 && bed->check_relocs != NULL
3997 && elf_object_id (abfd) == elf_hash_table_id (htab)
3998 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3999 {
4000 asection *o;
4001
4002 for (o = abfd->sections; o != NULL; o = o->next)
4003 {
4004 Elf_Internal_Rela *internal_relocs;
4005 bfd_boolean ok;
4006
4007 /* Don't check relocations in excluded sections. Don't do
4008 anything special with non-loaded, non-alloced sections.
4009 In particular, any relocs in such sections should not
4010 affect GOT and PLT reference counting (ie. we don't
4011 allow them to create GOT or PLT entries), there's no
4012 possibility or desire to optimize TLS relocs, and
4013 there's not much point in propagating relocs to shared
4014 libs that the dynamic linker won't relocate. */
4015 if ((o->flags & SEC_ALLOC) == 0
4016 || (o->flags & SEC_RELOC) == 0
4017 || (o->flags & SEC_EXCLUDE) != 0
4018 || o->reloc_count == 0
4019 || ((info->strip == strip_all || info->strip == strip_debugger)
4020 && (o->flags & SEC_DEBUGGING) != 0)
4021 || bfd_is_abs_section (o->output_section))
4022 continue;
4023
4024 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4025 info->keep_memory);
4026 if (internal_relocs == NULL)
4027 return FALSE;
4028
4029 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4030
4031 if (elf_section_data (o)->relocs != internal_relocs)
4032 free (internal_relocs);
4033
4034 if (! ok)
4035 return FALSE;
4036 }
4037 }
4038
4039 return TRUE;
4040 }
4041
4042 /* Add symbols from an ELF object file to the linker hash table. */
4043
4044 static bfd_boolean
4045 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4046 {
4047 Elf_Internal_Ehdr *ehdr;
4048 Elf_Internal_Shdr *hdr;
4049 size_t symcount;
4050 size_t extsymcount;
4051 size_t extsymoff;
4052 struct elf_link_hash_entry **sym_hash;
4053 bfd_boolean dynamic;
4054 Elf_External_Versym *extversym = NULL;
4055 Elf_External_Versym *extversym_end = NULL;
4056 Elf_External_Versym *ever;
4057 struct elf_link_hash_entry *weaks;
4058 struct elf_link_hash_entry **nondeflt_vers = NULL;
4059 size_t nondeflt_vers_cnt = 0;
4060 Elf_Internal_Sym *isymbuf = NULL;
4061 Elf_Internal_Sym *isym;
4062 Elf_Internal_Sym *isymend;
4063 const struct elf_backend_data *bed;
4064 bfd_boolean add_needed;
4065 struct elf_link_hash_table *htab;
4066 void *alloc_mark = NULL;
4067 struct bfd_hash_entry **old_table = NULL;
4068 unsigned int old_size = 0;
4069 unsigned int old_count = 0;
4070 void *old_tab = NULL;
4071 void *old_ent;
4072 struct bfd_link_hash_entry *old_undefs = NULL;
4073 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4074 void *old_strtab = NULL;
4075 size_t tabsize = 0;
4076 asection *s;
4077 bfd_boolean just_syms;
4078
4079 htab = elf_hash_table (info);
4080 bed = get_elf_backend_data (abfd);
4081
4082 if ((abfd->flags & DYNAMIC) == 0)
4083 dynamic = FALSE;
4084 else
4085 {
4086 dynamic = TRUE;
4087
4088 /* You can't use -r against a dynamic object. Also, there's no
4089 hope of using a dynamic object which does not exactly match
4090 the format of the output file. */
4091 if (bfd_link_relocatable (info)
4092 || !is_elf_hash_table (htab)
4093 || info->output_bfd->xvec != abfd->xvec)
4094 {
4095 if (bfd_link_relocatable (info))
4096 bfd_set_error (bfd_error_invalid_operation);
4097 else
4098 bfd_set_error (bfd_error_wrong_format);
4099 goto error_return;
4100 }
4101 }
4102
4103 ehdr = elf_elfheader (abfd);
4104 if (info->warn_alternate_em
4105 && bed->elf_machine_code != ehdr->e_machine
4106 && ((bed->elf_machine_alt1 != 0
4107 && ehdr->e_machine == bed->elf_machine_alt1)
4108 || (bed->elf_machine_alt2 != 0
4109 && ehdr->e_machine == bed->elf_machine_alt2)))
4110 _bfd_error_handler
4111 /* xgettext:c-format */
4112 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4113 ehdr->e_machine, abfd, bed->elf_machine_code);
4114
4115 /* As a GNU extension, any input sections which are named
4116 .gnu.warning.SYMBOL are treated as warning symbols for the given
4117 symbol. This differs from .gnu.warning sections, which generate
4118 warnings when they are included in an output file. */
4119 /* PR 12761: Also generate this warning when building shared libraries. */
4120 for (s = abfd->sections; s != NULL; s = s->next)
4121 {
4122 const char *name;
4123
4124 name = bfd_section_name (s);
4125 if (CONST_STRNEQ (name, ".gnu.warning."))
4126 {
4127 char *msg;
4128 bfd_size_type sz;
4129
4130 name += sizeof ".gnu.warning." - 1;
4131
4132 /* If this is a shared object, then look up the symbol
4133 in the hash table. If it is there, and it is already
4134 been defined, then we will not be using the entry
4135 from this shared object, so we don't need to warn.
4136 FIXME: If we see the definition in a regular object
4137 later on, we will warn, but we shouldn't. The only
4138 fix is to keep track of what warnings we are supposed
4139 to emit, and then handle them all at the end of the
4140 link. */
4141 if (dynamic)
4142 {
4143 struct elf_link_hash_entry *h;
4144
4145 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4146
4147 /* FIXME: What about bfd_link_hash_common? */
4148 if (h != NULL
4149 && (h->root.type == bfd_link_hash_defined
4150 || h->root.type == bfd_link_hash_defweak))
4151 continue;
4152 }
4153
4154 sz = s->size;
4155 msg = (char *) bfd_alloc (abfd, sz + 1);
4156 if (msg == NULL)
4157 goto error_return;
4158
4159 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4160 goto error_return;
4161
4162 msg[sz] = '\0';
4163
4164 if (! (_bfd_generic_link_add_one_symbol
4165 (info, abfd, name, BSF_WARNING, s, 0, msg,
4166 FALSE, bed->collect, NULL)))
4167 goto error_return;
4168
4169 if (bfd_link_executable (info))
4170 {
4171 /* Clobber the section size so that the warning does
4172 not get copied into the output file. */
4173 s->size = 0;
4174
4175 /* Also set SEC_EXCLUDE, so that symbols defined in
4176 the warning section don't get copied to the output. */
4177 s->flags |= SEC_EXCLUDE;
4178 }
4179 }
4180 }
4181
4182 just_syms = ((s = abfd->sections) != NULL
4183 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4184
4185 add_needed = TRUE;
4186 if (! dynamic)
4187 {
4188 /* If we are creating a shared library, create all the dynamic
4189 sections immediately. We need to attach them to something,
4190 so we attach them to this BFD, provided it is the right
4191 format and is not from ld --just-symbols. Always create the
4192 dynamic sections for -E/--dynamic-list. FIXME: If there
4193 are no input BFD's of the same format as the output, we can't
4194 make a shared library. */
4195 if (!just_syms
4196 && (bfd_link_pic (info)
4197 || (!bfd_link_relocatable (info)
4198 && info->nointerp
4199 && (info->export_dynamic || info->dynamic)))
4200 && is_elf_hash_table (htab)
4201 && info->output_bfd->xvec == abfd->xvec
4202 && !htab->dynamic_sections_created)
4203 {
4204 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4205 goto error_return;
4206 }
4207 }
4208 else if (!is_elf_hash_table (htab))
4209 goto error_return;
4210 else
4211 {
4212 const char *soname = NULL;
4213 char *audit = NULL;
4214 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4215 const Elf_Internal_Phdr *phdr;
4216 struct elf_link_loaded_list *loaded_lib;
4217
4218 /* ld --just-symbols and dynamic objects don't mix very well.
4219 ld shouldn't allow it. */
4220 if (just_syms)
4221 abort ();
4222
4223 /* If this dynamic lib was specified on the command line with
4224 --as-needed in effect, then we don't want to add a DT_NEEDED
4225 tag unless the lib is actually used. Similary for libs brought
4226 in by another lib's DT_NEEDED. When --no-add-needed is used
4227 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4228 any dynamic library in DT_NEEDED tags in the dynamic lib at
4229 all. */
4230 add_needed = (elf_dyn_lib_class (abfd)
4231 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4232 | DYN_NO_NEEDED)) == 0;
4233
4234 s = bfd_get_section_by_name (abfd, ".dynamic");
4235 if (s != NULL)
4236 {
4237 bfd_byte *dynbuf;
4238 bfd_byte *extdyn;
4239 unsigned int elfsec;
4240 unsigned long shlink;
4241
4242 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4243 {
4244 error_free_dyn:
4245 free (dynbuf);
4246 goto error_return;
4247 }
4248
4249 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4250 if (elfsec == SHN_BAD)
4251 goto error_free_dyn;
4252 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4253
4254 for (extdyn = dynbuf;
4255 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4256 extdyn += bed->s->sizeof_dyn)
4257 {
4258 Elf_Internal_Dyn dyn;
4259
4260 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4261 if (dyn.d_tag == DT_SONAME)
4262 {
4263 unsigned int tagv = dyn.d_un.d_val;
4264 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4265 if (soname == NULL)
4266 goto error_free_dyn;
4267 }
4268 if (dyn.d_tag == DT_NEEDED)
4269 {
4270 struct bfd_link_needed_list *n, **pn;
4271 char *fnm, *anm;
4272 unsigned int tagv = dyn.d_un.d_val;
4273 size_t amt = sizeof (struct bfd_link_needed_list);
4274
4275 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4276 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4277 if (n == NULL || fnm == NULL)
4278 goto error_free_dyn;
4279 amt = strlen (fnm) + 1;
4280 anm = (char *) bfd_alloc (abfd, amt);
4281 if (anm == NULL)
4282 goto error_free_dyn;
4283 memcpy (anm, fnm, amt);
4284 n->name = anm;
4285 n->by = abfd;
4286 n->next = NULL;
4287 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4288 ;
4289 *pn = n;
4290 }
4291 if (dyn.d_tag == DT_RUNPATH)
4292 {
4293 struct bfd_link_needed_list *n, **pn;
4294 char *fnm, *anm;
4295 unsigned int tagv = dyn.d_un.d_val;
4296 size_t amt = sizeof (struct bfd_link_needed_list);
4297
4298 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4299 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4300 if (n == NULL || fnm == NULL)
4301 goto error_free_dyn;
4302 amt = strlen (fnm) + 1;
4303 anm = (char *) bfd_alloc (abfd, amt);
4304 if (anm == NULL)
4305 goto error_free_dyn;
4306 memcpy (anm, fnm, amt);
4307 n->name = anm;
4308 n->by = abfd;
4309 n->next = NULL;
4310 for (pn = & runpath;
4311 *pn != NULL;
4312 pn = &(*pn)->next)
4313 ;
4314 *pn = n;
4315 }
4316 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4317 if (!runpath && dyn.d_tag == DT_RPATH)
4318 {
4319 struct bfd_link_needed_list *n, **pn;
4320 char *fnm, *anm;
4321 unsigned int tagv = dyn.d_un.d_val;
4322 size_t amt = sizeof (struct bfd_link_needed_list);
4323
4324 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4325 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4326 if (n == NULL || fnm == NULL)
4327 goto error_free_dyn;
4328 amt = strlen (fnm) + 1;
4329 anm = (char *) bfd_alloc (abfd, amt);
4330 if (anm == NULL)
4331 goto error_free_dyn;
4332 memcpy (anm, fnm, amt);
4333 n->name = anm;
4334 n->by = abfd;
4335 n->next = NULL;
4336 for (pn = & rpath;
4337 *pn != NULL;
4338 pn = &(*pn)->next)
4339 ;
4340 *pn = n;
4341 }
4342 if (dyn.d_tag == DT_AUDIT)
4343 {
4344 unsigned int tagv = dyn.d_un.d_val;
4345 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4346 }
4347 }
4348
4349 free (dynbuf);
4350 }
4351
4352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4353 frees all more recently bfd_alloc'd blocks as well. */
4354 if (runpath)
4355 rpath = runpath;
4356
4357 if (rpath)
4358 {
4359 struct bfd_link_needed_list **pn;
4360 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4361 ;
4362 *pn = rpath;
4363 }
4364
4365 /* If we have a PT_GNU_RELRO program header, mark as read-only
4366 all sections contained fully therein. This makes relro
4367 shared library sections appear as they will at run-time. */
4368 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4369 while (phdr-- > elf_tdata (abfd)->phdr)
4370 if (phdr->p_type == PT_GNU_RELRO)
4371 {
4372 for (s = abfd->sections; s != NULL; s = s->next)
4373 {
4374 unsigned int opb = bfd_octets_per_byte (abfd, s);
4375
4376 if ((s->flags & SEC_ALLOC) != 0
4377 && s->vma * opb >= phdr->p_vaddr
4378 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4379 s->flags |= SEC_READONLY;
4380 }
4381 break;
4382 }
4383
4384 /* We do not want to include any of the sections in a dynamic
4385 object in the output file. We hack by simply clobbering the
4386 list of sections in the BFD. This could be handled more
4387 cleanly by, say, a new section flag; the existing
4388 SEC_NEVER_LOAD flag is not the one we want, because that one
4389 still implies that the section takes up space in the output
4390 file. */
4391 bfd_section_list_clear (abfd);
4392
4393 /* Find the name to use in a DT_NEEDED entry that refers to this
4394 object. If the object has a DT_SONAME entry, we use it.
4395 Otherwise, if the generic linker stuck something in
4396 elf_dt_name, we use that. Otherwise, we just use the file
4397 name. */
4398 if (soname == NULL || *soname == '\0')
4399 {
4400 soname = elf_dt_name (abfd);
4401 if (soname == NULL || *soname == '\0')
4402 soname = bfd_get_filename (abfd);
4403 }
4404
4405 /* Save the SONAME because sometimes the linker emulation code
4406 will need to know it. */
4407 elf_dt_name (abfd) = soname;
4408
4409 /* If we have already included this dynamic object in the
4410 link, just ignore it. There is no reason to include a
4411 particular dynamic object more than once. */
4412 for (loaded_lib = htab->dyn_loaded;
4413 loaded_lib != NULL;
4414 loaded_lib = loaded_lib->next)
4415 {
4416 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4417 return TRUE;
4418 }
4419
4420 /* Create dynamic sections for backends that require that be done
4421 before setup_gnu_properties. */
4422 if (add_needed
4423 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4424 return FALSE;
4425
4426 /* Save the DT_AUDIT entry for the linker emulation code. */
4427 elf_dt_audit (abfd) = audit;
4428 }
4429
4430 /* If this is a dynamic object, we always link against the .dynsym
4431 symbol table, not the .symtab symbol table. The dynamic linker
4432 will only see the .dynsym symbol table, so there is no reason to
4433 look at .symtab for a dynamic object. */
4434
4435 if (! dynamic || elf_dynsymtab (abfd) == 0)
4436 hdr = &elf_tdata (abfd)->symtab_hdr;
4437 else
4438 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4439
4440 symcount = hdr->sh_size / bed->s->sizeof_sym;
4441
4442 /* The sh_info field of the symtab header tells us where the
4443 external symbols start. We don't care about the local symbols at
4444 this point. */
4445 if (elf_bad_symtab (abfd))
4446 {
4447 extsymcount = symcount;
4448 extsymoff = 0;
4449 }
4450 else
4451 {
4452 extsymcount = symcount - hdr->sh_info;
4453 extsymoff = hdr->sh_info;
4454 }
4455
4456 sym_hash = elf_sym_hashes (abfd);
4457 if (extsymcount != 0)
4458 {
4459 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4460 NULL, NULL, NULL);
4461 if (isymbuf == NULL)
4462 goto error_return;
4463
4464 if (sym_hash == NULL)
4465 {
4466 /* We store a pointer to the hash table entry for each
4467 external symbol. */
4468 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4469 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4470 if (sym_hash == NULL)
4471 goto error_free_sym;
4472 elf_sym_hashes (abfd) = sym_hash;
4473 }
4474 }
4475
4476 if (dynamic)
4477 {
4478 /* Read in any version definitions. */
4479 if (!_bfd_elf_slurp_version_tables (abfd,
4480 info->default_imported_symver))
4481 goto error_free_sym;
4482
4483 /* Read in the symbol versions, but don't bother to convert them
4484 to internal format. */
4485 if (elf_dynversym (abfd) != 0)
4486 {
4487 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4488 bfd_size_type amt = versymhdr->sh_size;
4489
4490 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4491 goto error_free_sym;
4492 extversym = (Elf_External_Versym *)
4493 _bfd_malloc_and_read (abfd, amt, amt);
4494 if (extversym == NULL)
4495 goto error_free_sym;
4496 extversym_end = extversym + amt / sizeof (*extversym);
4497 }
4498 }
4499
4500 /* If we are loading an as-needed shared lib, save the symbol table
4501 state before we start adding symbols. If the lib turns out
4502 to be unneeded, restore the state. */
4503 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4504 {
4505 unsigned int i;
4506 size_t entsize;
4507
4508 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4509 {
4510 struct bfd_hash_entry *p;
4511 struct elf_link_hash_entry *h;
4512
4513 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4514 {
4515 h = (struct elf_link_hash_entry *) p;
4516 entsize += htab->root.table.entsize;
4517 if (h->root.type == bfd_link_hash_warning)
4518 {
4519 entsize += htab->root.table.entsize;
4520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4521 }
4522 if (h->root.type == bfd_link_hash_common)
4523 entsize += sizeof (*h->root.u.c.p);
4524 }
4525 }
4526
4527 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4528 old_tab = bfd_malloc (tabsize + entsize);
4529 if (old_tab == NULL)
4530 goto error_free_vers;
4531
4532 /* Remember the current objalloc pointer, so that all mem for
4533 symbols added can later be reclaimed. */
4534 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4535 if (alloc_mark == NULL)
4536 goto error_free_vers;
4537
4538 /* Make a special call to the linker "notice" function to
4539 tell it that we are about to handle an as-needed lib. */
4540 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4541 goto error_free_vers;
4542
4543 /* Clone the symbol table. Remember some pointers into the
4544 symbol table, and dynamic symbol count. */
4545 old_ent = (char *) old_tab + tabsize;
4546 memcpy (old_tab, htab->root.table.table, tabsize);
4547 old_undefs = htab->root.undefs;
4548 old_undefs_tail = htab->root.undefs_tail;
4549 old_table = htab->root.table.table;
4550 old_size = htab->root.table.size;
4551 old_count = htab->root.table.count;
4552 old_strtab = NULL;
4553 if (htab->dynstr != NULL)
4554 {
4555 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4556 if (old_strtab == NULL)
4557 goto error_free_vers;
4558 }
4559
4560 for (i = 0; i < htab->root.table.size; i++)
4561 {
4562 struct bfd_hash_entry *p;
4563 struct elf_link_hash_entry *h;
4564
4565 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4566 {
4567 h = (struct elf_link_hash_entry *) p;
4568 memcpy (old_ent, h, htab->root.table.entsize);
4569 old_ent = (char *) old_ent + htab->root.table.entsize;
4570 if (h->root.type == bfd_link_hash_warning)
4571 {
4572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4573 memcpy (old_ent, h, htab->root.table.entsize);
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
4575 }
4576 if (h->root.type == bfd_link_hash_common)
4577 {
4578 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4579 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4580 }
4581 }
4582 }
4583 }
4584
4585 weaks = NULL;
4586 if (extversym == NULL)
4587 ever = NULL;
4588 else if (extversym + extsymoff < extversym_end)
4589 ever = extversym + extsymoff;
4590 else
4591 {
4592 /* xgettext:c-format */
4593 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4594 abfd, (long) extsymoff,
4595 (long) (extversym_end - extversym) / sizeof (* extversym));
4596 bfd_set_error (bfd_error_bad_value);
4597 goto error_free_vers;
4598 }
4599
4600 if (!bfd_link_relocatable (info)
4601 && abfd->lto_slim_object)
4602 {
4603 _bfd_error_handler
4604 (_("%pB: plugin needed to handle lto object"), abfd);
4605 }
4606
4607 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4608 isym < isymend;
4609 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4610 {
4611 int bind;
4612 bfd_vma value;
4613 asection *sec, *new_sec;
4614 flagword flags;
4615 const char *name;
4616 struct elf_link_hash_entry *h;
4617 struct elf_link_hash_entry *hi;
4618 bfd_boolean definition;
4619 bfd_boolean size_change_ok;
4620 bfd_boolean type_change_ok;
4621 bfd_boolean new_weak;
4622 bfd_boolean old_weak;
4623 bfd *override;
4624 bfd_boolean common;
4625 bfd_boolean discarded;
4626 unsigned int old_alignment;
4627 unsigned int shindex;
4628 bfd *old_bfd;
4629 bfd_boolean matched;
4630
4631 override = NULL;
4632
4633 flags = BSF_NO_FLAGS;
4634 sec = NULL;
4635 value = isym->st_value;
4636 common = bed->common_definition (isym);
4637 if (common && info->inhibit_common_definition)
4638 {
4639 /* Treat common symbol as undefined for --no-define-common. */
4640 isym->st_shndx = SHN_UNDEF;
4641 common = FALSE;
4642 }
4643 discarded = FALSE;
4644
4645 bind = ELF_ST_BIND (isym->st_info);
4646 switch (bind)
4647 {
4648 case STB_LOCAL:
4649 /* This should be impossible, since ELF requires that all
4650 global symbols follow all local symbols, and that sh_info
4651 point to the first global symbol. Unfortunately, Irix 5
4652 screws this up. */
4653 if (elf_bad_symtab (abfd))
4654 continue;
4655
4656 /* If we aren't prepared to handle locals within the globals
4657 then we'll likely segfault on a NULL symbol hash if the
4658 symbol is ever referenced in relocations. */
4659 shindex = elf_elfheader (abfd)->e_shstrndx;
4660 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4661 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4662 " (>= sh_info of %lu)"),
4663 abfd, name, (long) (isym - isymbuf + extsymoff),
4664 (long) extsymoff);
4665
4666 /* Dynamic object relocations are not processed by ld, so
4667 ld won't run into the problem mentioned above. */
4668 if (dynamic)
4669 continue;
4670 bfd_set_error (bfd_error_bad_value);
4671 goto error_free_vers;
4672
4673 case STB_GLOBAL:
4674 if (isym->st_shndx != SHN_UNDEF && !common)
4675 flags = BSF_GLOBAL;
4676 break;
4677
4678 case STB_WEAK:
4679 flags = BSF_WEAK;
4680 break;
4681
4682 case STB_GNU_UNIQUE:
4683 flags = BSF_GNU_UNIQUE;
4684 break;
4685
4686 default:
4687 /* Leave it up to the processor backend. */
4688 break;
4689 }
4690
4691 if (isym->st_shndx == SHN_UNDEF)
4692 sec = bfd_und_section_ptr;
4693 else if (isym->st_shndx == SHN_ABS)
4694 sec = bfd_abs_section_ptr;
4695 else if (isym->st_shndx == SHN_COMMON)
4696 {
4697 sec = bfd_com_section_ptr;
4698 /* What ELF calls the size we call the value. What ELF
4699 calls the value we call the alignment. */
4700 value = isym->st_size;
4701 }
4702 else
4703 {
4704 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4705 if (sec == NULL)
4706 sec = bfd_abs_section_ptr;
4707 else if (discarded_section (sec))
4708 {
4709 /* Symbols from discarded section are undefined. We keep
4710 its visibility. */
4711 sec = bfd_und_section_ptr;
4712 discarded = TRUE;
4713 isym->st_shndx = SHN_UNDEF;
4714 }
4715 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4716 value -= sec->vma;
4717 }
4718
4719 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4720 isym->st_name);
4721 if (name == NULL)
4722 goto error_free_vers;
4723
4724 if (isym->st_shndx == SHN_COMMON
4725 && (abfd->flags & BFD_PLUGIN) != 0)
4726 {
4727 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4728
4729 if (xc == NULL)
4730 {
4731 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4732 | SEC_EXCLUDE);
4733 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4734 if (xc == NULL)
4735 goto error_free_vers;
4736 }
4737 sec = xc;
4738 }
4739 else if (isym->st_shndx == SHN_COMMON
4740 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4741 && !bfd_link_relocatable (info))
4742 {
4743 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4744
4745 if (tcomm == NULL)
4746 {
4747 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4748 | SEC_LINKER_CREATED);
4749 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4750 if (tcomm == NULL)
4751 goto error_free_vers;
4752 }
4753 sec = tcomm;
4754 }
4755 else if (bed->elf_add_symbol_hook)
4756 {
4757 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4758 &sec, &value))
4759 goto error_free_vers;
4760
4761 /* The hook function sets the name to NULL if this symbol
4762 should be skipped for some reason. */
4763 if (name == NULL)
4764 continue;
4765 }
4766
4767 /* Sanity check that all possibilities were handled. */
4768 if (sec == NULL)
4769 abort ();
4770
4771 /* Silently discard TLS symbols from --just-syms. There's
4772 no way to combine a static TLS block with a new TLS block
4773 for this executable. */
4774 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4775 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4776 continue;
4777
4778 if (bfd_is_und_section (sec)
4779 || bfd_is_com_section (sec))
4780 definition = FALSE;
4781 else
4782 definition = TRUE;
4783
4784 size_change_ok = FALSE;
4785 type_change_ok = bed->type_change_ok;
4786 old_weak = FALSE;
4787 matched = FALSE;
4788 old_alignment = 0;
4789 old_bfd = NULL;
4790 new_sec = sec;
4791
4792 if (is_elf_hash_table (htab))
4793 {
4794 Elf_Internal_Versym iver;
4795 unsigned int vernum = 0;
4796 bfd_boolean skip;
4797
4798 if (ever == NULL)
4799 {
4800 if (info->default_imported_symver)
4801 /* Use the default symbol version created earlier. */
4802 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4803 else
4804 iver.vs_vers = 0;
4805 }
4806 else if (ever >= extversym_end)
4807 {
4808 /* xgettext:c-format */
4809 _bfd_error_handler (_("%pB: not enough version information"),
4810 abfd);
4811 bfd_set_error (bfd_error_bad_value);
4812 goto error_free_vers;
4813 }
4814 else
4815 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4816
4817 vernum = iver.vs_vers & VERSYM_VERSION;
4818
4819 /* If this is a hidden symbol, or if it is not version
4820 1, we append the version name to the symbol name.
4821 However, we do not modify a non-hidden absolute symbol
4822 if it is not a function, because it might be the version
4823 symbol itself. FIXME: What if it isn't? */
4824 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4825 || (vernum > 1
4826 && (!bfd_is_abs_section (sec)
4827 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4828 {
4829 const char *verstr;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4832
4833 if (isym->st_shndx != SHN_UNDEF)
4834 {
4835 if (vernum > elf_tdata (abfd)->cverdefs)
4836 verstr = NULL;
4837 else if (vernum > 1)
4838 verstr =
4839 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4840 else
4841 verstr = "";
4842
4843 if (verstr == NULL)
4844 {
4845 _bfd_error_handler
4846 /* xgettext:c-format */
4847 (_("%pB: %s: invalid version %u (max %d)"),
4848 abfd, name, vernum,
4849 elf_tdata (abfd)->cverdefs);
4850 bfd_set_error (bfd_error_bad_value);
4851 goto error_free_vers;
4852 }
4853 }
4854 else
4855 {
4856 /* We cannot simply test for the number of
4857 entries in the VERNEED section since the
4858 numbers for the needed versions do not start
4859 at 0. */
4860 Elf_Internal_Verneed *t;
4861
4862 verstr = NULL;
4863 for (t = elf_tdata (abfd)->verref;
4864 t != NULL;
4865 t = t->vn_nextref)
4866 {
4867 Elf_Internal_Vernaux *a;
4868
4869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4870 {
4871 if (a->vna_other == vernum)
4872 {
4873 verstr = a->vna_nodename;
4874 break;
4875 }
4876 }
4877 if (a != NULL)
4878 break;
4879 }
4880 if (verstr == NULL)
4881 {
4882 _bfd_error_handler
4883 /* xgettext:c-format */
4884 (_("%pB: %s: invalid needed version %d"),
4885 abfd, name, vernum);
4886 bfd_set_error (bfd_error_bad_value);
4887 goto error_free_vers;
4888 }
4889 }
4890
4891 namelen = strlen (name);
4892 verlen = strlen (verstr);
4893 newlen = namelen + verlen + 2;
4894 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4895 && isym->st_shndx != SHN_UNDEF)
4896 ++newlen;
4897
4898 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4899 if (newname == NULL)
4900 goto error_free_vers;
4901 memcpy (newname, name, namelen);
4902 p = newname + namelen;
4903 *p++ = ELF_VER_CHR;
4904 /* If this is a defined non-hidden version symbol,
4905 we add another @ to the name. This indicates the
4906 default version of the symbol. */
4907 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4908 && isym->st_shndx != SHN_UNDEF)
4909 *p++ = ELF_VER_CHR;
4910 memcpy (p, verstr, verlen + 1);
4911
4912 name = newname;
4913 }
4914
4915 /* If this symbol has default visibility and the user has
4916 requested we not re-export it, then mark it as hidden. */
4917 if (!bfd_is_und_section (sec)
4918 && !dynamic
4919 && abfd->no_export
4920 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4921 isym->st_other = (STV_HIDDEN
4922 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4923
4924 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4925 sym_hash, &old_bfd, &old_weak,
4926 &old_alignment, &skip, &override,
4927 &type_change_ok, &size_change_ok,
4928 &matched))
4929 goto error_free_vers;
4930
4931 if (skip)
4932 continue;
4933
4934 /* Override a definition only if the new symbol matches the
4935 existing one. */
4936 if (override && matched)
4937 definition = FALSE;
4938
4939 h = *sym_hash;
4940 while (h->root.type == bfd_link_hash_indirect
4941 || h->root.type == bfd_link_hash_warning)
4942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4943
4944 if (elf_tdata (abfd)->verdef != NULL
4945 && vernum > 1
4946 && definition)
4947 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4948 }
4949
4950 if (! (_bfd_generic_link_add_one_symbol
4951 (info, override ? override : abfd, name, flags, sec, value,
4952 NULL, FALSE, bed->collect,
4953 (struct bfd_link_hash_entry **) sym_hash)))
4954 goto error_free_vers;
4955
4956 h = *sym_hash;
4957 /* We need to make sure that indirect symbol dynamic flags are
4958 updated. */
4959 hi = h;
4960 while (h->root.type == bfd_link_hash_indirect
4961 || h->root.type == bfd_link_hash_warning)
4962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4963
4964 /* Setting the index to -3 tells elf_link_output_extsym that
4965 this symbol is defined in a discarded section. */
4966 if (discarded)
4967 h->indx = -3;
4968
4969 *sym_hash = h;
4970
4971 new_weak = (flags & BSF_WEAK) != 0;
4972 if (dynamic
4973 && definition
4974 && new_weak
4975 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4976 && is_elf_hash_table (htab)
4977 && h->u.alias == NULL)
4978 {
4979 /* Keep a list of all weak defined non function symbols from
4980 a dynamic object, using the alias field. Later in this
4981 function we will set the alias field to the correct
4982 value. We only put non-function symbols from dynamic
4983 objects on this list, because that happens to be the only
4984 time we need to know the normal symbol corresponding to a
4985 weak symbol, and the information is time consuming to
4986 figure out. If the alias field is not already NULL,
4987 then this symbol was already defined by some previous
4988 dynamic object, and we will be using that previous
4989 definition anyhow. */
4990
4991 h->u.alias = weaks;
4992 weaks = h;
4993 }
4994
4995 /* Set the alignment of a common symbol. */
4996 if ((common || bfd_is_com_section (sec))
4997 && h->root.type == bfd_link_hash_common)
4998 {
4999 unsigned int align;
5000
5001 if (common)
5002 align = bfd_log2 (isym->st_value);
5003 else
5004 {
5005 /* The new symbol is a common symbol in a shared object.
5006 We need to get the alignment from the section. */
5007 align = new_sec->alignment_power;
5008 }
5009 if (align > old_alignment)
5010 h->root.u.c.p->alignment_power = align;
5011 else
5012 h->root.u.c.p->alignment_power = old_alignment;
5013 }
5014
5015 if (is_elf_hash_table (htab))
5016 {
5017 /* Set a flag in the hash table entry indicating the type of
5018 reference or definition we just found. A dynamic symbol
5019 is one which is referenced or defined by both a regular
5020 object and a shared object. */
5021 bfd_boolean dynsym = FALSE;
5022
5023 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5024 if ((abfd->flags & BFD_PLUGIN) != 0)
5025 ;
5026 else if (!dynamic)
5027 {
5028 if (! definition)
5029 {
5030 h->ref_regular = 1;
5031 if (bind != STB_WEAK)
5032 h->ref_regular_nonweak = 1;
5033 }
5034 else
5035 {
5036 h->def_regular = 1;
5037 if (h->def_dynamic)
5038 {
5039 h->def_dynamic = 0;
5040 h->ref_dynamic = 1;
5041 }
5042 }
5043 }
5044 else
5045 {
5046 if (! definition)
5047 {
5048 h->ref_dynamic = 1;
5049 hi->ref_dynamic = 1;
5050 }
5051 else
5052 {
5053 h->def_dynamic = 1;
5054 hi->def_dynamic = 1;
5055 }
5056 }
5057
5058 /* If an indirect symbol has been forced local, don't
5059 make the real symbol dynamic. */
5060 if (h != hi && hi->forced_local)
5061 ;
5062 else if (!dynamic)
5063 {
5064 if (bfd_link_dll (info)
5065 || h->def_dynamic
5066 || h->ref_dynamic)
5067 dynsym = TRUE;
5068 }
5069 else
5070 {
5071 if (h->def_regular
5072 || h->ref_regular
5073 || (h->is_weakalias
5074 && weakdef (h)->dynindx != -1))
5075 dynsym = TRUE;
5076 }
5077
5078 /* Check to see if we need to add an indirect symbol for
5079 the default name. */
5080 if ((definition
5081 || (!override && h->root.type == bfd_link_hash_common))
5082 && !(hi != h
5083 && hi->versioned == versioned_hidden))
5084 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5085 sec, value, &old_bfd, &dynsym))
5086 goto error_free_vers;
5087
5088 /* Check the alignment when a common symbol is involved. This
5089 can change when a common symbol is overridden by a normal
5090 definition or a common symbol is ignored due to the old
5091 normal definition. We need to make sure the maximum
5092 alignment is maintained. */
5093 if ((old_alignment || common)
5094 && h->root.type != bfd_link_hash_common)
5095 {
5096 unsigned int common_align;
5097 unsigned int normal_align;
5098 unsigned int symbol_align;
5099 bfd *normal_bfd;
5100 bfd *common_bfd;
5101
5102 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5103 || h->root.type == bfd_link_hash_defweak);
5104
5105 symbol_align = ffs (h->root.u.def.value) - 1;
5106 if (h->root.u.def.section->owner != NULL
5107 && (h->root.u.def.section->owner->flags
5108 & (DYNAMIC | BFD_PLUGIN)) == 0)
5109 {
5110 normal_align = h->root.u.def.section->alignment_power;
5111 if (normal_align > symbol_align)
5112 normal_align = symbol_align;
5113 }
5114 else
5115 normal_align = symbol_align;
5116
5117 if (old_alignment)
5118 {
5119 common_align = old_alignment;
5120 common_bfd = old_bfd;
5121 normal_bfd = abfd;
5122 }
5123 else
5124 {
5125 common_align = bfd_log2 (isym->st_value);
5126 common_bfd = abfd;
5127 normal_bfd = old_bfd;
5128 }
5129
5130 if (normal_align < common_align)
5131 {
5132 /* PR binutils/2735 */
5133 if (normal_bfd == NULL)
5134 _bfd_error_handler
5135 /* xgettext:c-format */
5136 (_("warning: alignment %u of common symbol `%s' in %pB is"
5137 " greater than the alignment (%u) of its section %pA"),
5138 1 << common_align, name, common_bfd,
5139 1 << normal_align, h->root.u.def.section);
5140 else
5141 _bfd_error_handler
5142 /* xgettext:c-format */
5143 (_("warning: alignment %u of symbol `%s' in %pB"
5144 " is smaller than %u in %pB"),
5145 1 << normal_align, name, normal_bfd,
5146 1 << common_align, common_bfd);
5147 }
5148 }
5149
5150 /* Remember the symbol size if it isn't undefined. */
5151 if (isym->st_size != 0
5152 && isym->st_shndx != SHN_UNDEF
5153 && (definition || h->size == 0))
5154 {
5155 if (h->size != 0
5156 && h->size != isym->st_size
5157 && ! size_change_ok)
5158 _bfd_error_handler
5159 /* xgettext:c-format */
5160 (_("warning: size of symbol `%s' changed"
5161 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5162 name, (uint64_t) h->size, old_bfd,
5163 (uint64_t) isym->st_size, abfd);
5164
5165 h->size = isym->st_size;
5166 }
5167
5168 /* If this is a common symbol, then we always want H->SIZE
5169 to be the size of the common symbol. The code just above
5170 won't fix the size if a common symbol becomes larger. We
5171 don't warn about a size change here, because that is
5172 covered by --warn-common. Allow changes between different
5173 function types. */
5174 if (h->root.type == bfd_link_hash_common)
5175 h->size = h->root.u.c.size;
5176
5177 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5178 && ((definition && !new_weak)
5179 || (old_weak && h->root.type == bfd_link_hash_common)
5180 || h->type == STT_NOTYPE))
5181 {
5182 unsigned int type = ELF_ST_TYPE (isym->st_info);
5183
5184 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5185 symbol. */
5186 if (type == STT_GNU_IFUNC
5187 && (abfd->flags & DYNAMIC) != 0)
5188 type = STT_FUNC;
5189
5190 if (h->type != type)
5191 {
5192 if (h->type != STT_NOTYPE && ! type_change_ok)
5193 /* xgettext:c-format */
5194 _bfd_error_handler
5195 (_("warning: type of symbol `%s' changed"
5196 " from %d to %d in %pB"),
5197 name, h->type, type, abfd);
5198
5199 h->type = type;
5200 }
5201 }
5202
5203 /* Merge st_other field. */
5204 elf_merge_st_other (abfd, h, isym->st_other, sec,
5205 definition, dynamic);
5206
5207 /* We don't want to make debug symbol dynamic. */
5208 if (definition
5209 && (sec->flags & SEC_DEBUGGING)
5210 && !bfd_link_relocatable (info))
5211 dynsym = FALSE;
5212
5213 /* Nor should we make plugin symbols dynamic. */
5214 if ((abfd->flags & BFD_PLUGIN) != 0)
5215 dynsym = FALSE;
5216
5217 if (definition)
5218 {
5219 h->target_internal = isym->st_target_internal;
5220 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5221 }
5222
5223 if (definition && !dynamic)
5224 {
5225 char *p = strchr (name, ELF_VER_CHR);
5226 if (p != NULL && p[1] != ELF_VER_CHR)
5227 {
5228 /* Queue non-default versions so that .symver x, x@FOO
5229 aliases can be checked. */
5230 if (!nondeflt_vers)
5231 {
5232 size_t amt = ((isymend - isym + 1)
5233 * sizeof (struct elf_link_hash_entry *));
5234 nondeflt_vers
5235 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5236 if (!nondeflt_vers)
5237 goto error_free_vers;
5238 }
5239 nondeflt_vers[nondeflt_vers_cnt++] = h;
5240 }
5241 }
5242
5243 if (dynsym && h->dynindx == -1)
5244 {
5245 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5246 goto error_free_vers;
5247 if (h->is_weakalias
5248 && weakdef (h)->dynindx == -1)
5249 {
5250 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5251 goto error_free_vers;
5252 }
5253 }
5254 else if (h->dynindx != -1)
5255 /* If the symbol already has a dynamic index, but
5256 visibility says it should not be visible, turn it into
5257 a local symbol. */
5258 switch (ELF_ST_VISIBILITY (h->other))
5259 {
5260 case STV_INTERNAL:
5261 case STV_HIDDEN:
5262 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5263 dynsym = FALSE;
5264 break;
5265 }
5266
5267 if (!add_needed
5268 && matched
5269 && definition
5270 && ((dynsym
5271 && h->ref_regular_nonweak)
5272 || (old_bfd != NULL
5273 && (old_bfd->flags & BFD_PLUGIN) != 0
5274 && bind != STB_WEAK)
5275 || (h->ref_dynamic_nonweak
5276 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5277 && !on_needed_list (elf_dt_name (abfd),
5278 htab->needed, NULL))))
5279 {
5280 const char *soname = elf_dt_name (abfd);
5281
5282 info->callbacks->minfo ("%!", soname, old_bfd,
5283 h->root.root.string);
5284
5285 /* A symbol from a library loaded via DT_NEEDED of some
5286 other library is referenced by a regular object.
5287 Add a DT_NEEDED entry for it. Issue an error if
5288 --no-add-needed is used and the reference was not
5289 a weak one. */
5290 if (old_bfd != NULL
5291 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5292 {
5293 _bfd_error_handler
5294 /* xgettext:c-format */
5295 (_("%pB: undefined reference to symbol '%s'"),
5296 old_bfd, name);
5297 bfd_set_error (bfd_error_missing_dso);
5298 goto error_free_vers;
5299 }
5300
5301 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5302 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5303
5304 /* Create dynamic sections for backends that require
5305 that be done before setup_gnu_properties. */
5306 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5307 return FALSE;
5308 add_needed = TRUE;
5309 }
5310 }
5311 }
5312
5313 if (info->lto_plugin_active
5314 && !bfd_link_relocatable (info)
5315 && (abfd->flags & BFD_PLUGIN) == 0
5316 && !just_syms
5317 && extsymcount)
5318 {
5319 int r_sym_shift;
5320
5321 if (bed->s->arch_size == 32)
5322 r_sym_shift = 8;
5323 else
5324 r_sym_shift = 32;
5325
5326 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5327 referenced in regular objects so that linker plugin will get
5328 the correct symbol resolution. */
5329
5330 sym_hash = elf_sym_hashes (abfd);
5331 for (s = abfd->sections; s != NULL; s = s->next)
5332 {
5333 Elf_Internal_Rela *internal_relocs;
5334 Elf_Internal_Rela *rel, *relend;
5335
5336 /* Don't check relocations in excluded sections. */
5337 if ((s->flags & SEC_RELOC) == 0
5338 || s->reloc_count == 0
5339 || (s->flags & SEC_EXCLUDE) != 0
5340 || ((info->strip == strip_all
5341 || info->strip == strip_debugger)
5342 && (s->flags & SEC_DEBUGGING) != 0))
5343 continue;
5344
5345 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5346 NULL,
5347 info->keep_memory);
5348 if (internal_relocs == NULL)
5349 goto error_free_vers;
5350
5351 rel = internal_relocs;
5352 relend = rel + s->reloc_count;
5353 for ( ; rel < relend; rel++)
5354 {
5355 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5356 struct elf_link_hash_entry *h;
5357
5358 /* Skip local symbols. */
5359 if (r_symndx < extsymoff)
5360 continue;
5361
5362 h = sym_hash[r_symndx - extsymoff];
5363 if (h != NULL)
5364 h->root.non_ir_ref_regular = 1;
5365 }
5366
5367 if (elf_section_data (s)->relocs != internal_relocs)
5368 free (internal_relocs);
5369 }
5370 }
5371
5372 free (extversym);
5373 extversym = NULL;
5374 free (isymbuf);
5375 isymbuf = NULL;
5376
5377 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5378 {
5379 unsigned int i;
5380
5381 /* Restore the symbol table. */
5382 old_ent = (char *) old_tab + tabsize;
5383 memset (elf_sym_hashes (abfd), 0,
5384 extsymcount * sizeof (struct elf_link_hash_entry *));
5385 htab->root.table.table = old_table;
5386 htab->root.table.size = old_size;
5387 htab->root.table.count = old_count;
5388 memcpy (htab->root.table.table, old_tab, tabsize);
5389 htab->root.undefs = old_undefs;
5390 htab->root.undefs_tail = old_undefs_tail;
5391 if (htab->dynstr != NULL)
5392 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5393 free (old_strtab);
5394 old_strtab = NULL;
5395 for (i = 0; i < htab->root.table.size; i++)
5396 {
5397 struct bfd_hash_entry *p;
5398 struct elf_link_hash_entry *h;
5399 unsigned int non_ir_ref_dynamic;
5400
5401 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5402 {
5403 /* Preserve non_ir_ref_dynamic so that this symbol
5404 will be exported when the dynamic lib becomes needed
5405 in the second pass. */
5406 h = (struct elf_link_hash_entry *) p;
5407 if (h->root.type == bfd_link_hash_warning)
5408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5409 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5410
5411 h = (struct elf_link_hash_entry *) p;
5412 memcpy (h, old_ent, htab->root.table.entsize);
5413 old_ent = (char *) old_ent + htab->root.table.entsize;
5414 if (h->root.type == bfd_link_hash_warning)
5415 {
5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
5419 }
5420 if (h->root.type == bfd_link_hash_common)
5421 {
5422 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5423 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5424 }
5425 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5426 }
5427 }
5428
5429 /* Make a special call to the linker "notice" function to
5430 tell it that symbols added for crefs may need to be removed. */
5431 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5432 goto error_free_vers;
5433
5434 free (old_tab);
5435 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5436 alloc_mark);
5437 free (nondeflt_vers);
5438 return TRUE;
5439 }
5440
5441 if (old_tab != NULL)
5442 {
5443 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5444 goto error_free_vers;
5445 free (old_tab);
5446 old_tab = NULL;
5447 }
5448
5449 /* Now that all the symbols from this input file are created, if
5450 not performing a relocatable link, handle .symver foo, foo@BAR
5451 such that any relocs against foo become foo@BAR. */
5452 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5453 {
5454 size_t cnt, symidx;
5455
5456 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5457 {
5458 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5459 char *shortname, *p;
5460 size_t amt;
5461
5462 p = strchr (h->root.root.string, ELF_VER_CHR);
5463 if (p == NULL
5464 || (h->root.type != bfd_link_hash_defined
5465 && h->root.type != bfd_link_hash_defweak))
5466 continue;
5467
5468 amt = p - h->root.root.string;
5469 shortname = (char *) bfd_malloc (amt + 1);
5470 if (!shortname)
5471 goto error_free_vers;
5472 memcpy (shortname, h->root.root.string, amt);
5473 shortname[amt] = '\0';
5474
5475 hi = (struct elf_link_hash_entry *)
5476 bfd_link_hash_lookup (&htab->root, shortname,
5477 FALSE, FALSE, FALSE);
5478 if (hi != NULL
5479 && hi->root.type == h->root.type
5480 && hi->root.u.def.value == h->root.u.def.value
5481 && hi->root.u.def.section == h->root.u.def.section)
5482 {
5483 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5484 hi->root.type = bfd_link_hash_indirect;
5485 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5486 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5487 sym_hash = elf_sym_hashes (abfd);
5488 if (sym_hash)
5489 for (symidx = 0; symidx < extsymcount; ++symidx)
5490 if (sym_hash[symidx] == hi)
5491 {
5492 sym_hash[symidx] = h;
5493 break;
5494 }
5495 }
5496 free (shortname);
5497 }
5498 free (nondeflt_vers);
5499 nondeflt_vers = NULL;
5500 }
5501
5502 /* Now set the alias field correctly for all the weak defined
5503 symbols we found. The only way to do this is to search all the
5504 symbols. Since we only need the information for non functions in
5505 dynamic objects, that's the only time we actually put anything on
5506 the list WEAKS. We need this information so that if a regular
5507 object refers to a symbol defined weakly in a dynamic object, the
5508 real symbol in the dynamic object is also put in the dynamic
5509 symbols; we also must arrange for both symbols to point to the
5510 same memory location. We could handle the general case of symbol
5511 aliasing, but a general symbol alias can only be generated in
5512 assembler code, handling it correctly would be very time
5513 consuming, and other ELF linkers don't handle general aliasing
5514 either. */
5515 if (weaks != NULL)
5516 {
5517 struct elf_link_hash_entry **hpp;
5518 struct elf_link_hash_entry **hppend;
5519 struct elf_link_hash_entry **sorted_sym_hash;
5520 struct elf_link_hash_entry *h;
5521 size_t sym_count, amt;
5522
5523 /* Since we have to search the whole symbol list for each weak
5524 defined symbol, search time for N weak defined symbols will be
5525 O(N^2). Binary search will cut it down to O(NlogN). */
5526 amt = extsymcount * sizeof (*sorted_sym_hash);
5527 sorted_sym_hash = bfd_malloc (amt);
5528 if (sorted_sym_hash == NULL)
5529 goto error_return;
5530 sym_hash = sorted_sym_hash;
5531 hpp = elf_sym_hashes (abfd);
5532 hppend = hpp + extsymcount;
5533 sym_count = 0;
5534 for (; hpp < hppend; hpp++)
5535 {
5536 h = *hpp;
5537 if (h != NULL
5538 && h->root.type == bfd_link_hash_defined
5539 && !bed->is_function_type (h->type))
5540 {
5541 *sym_hash = h;
5542 sym_hash++;
5543 sym_count++;
5544 }
5545 }
5546
5547 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5548 elf_sort_symbol);
5549
5550 while (weaks != NULL)
5551 {
5552 struct elf_link_hash_entry *hlook;
5553 asection *slook;
5554 bfd_vma vlook;
5555 size_t i, j, idx = 0;
5556
5557 hlook = weaks;
5558 weaks = hlook->u.alias;
5559 hlook->u.alias = NULL;
5560
5561 if (hlook->root.type != bfd_link_hash_defined
5562 && hlook->root.type != bfd_link_hash_defweak)
5563 continue;
5564
5565 slook = hlook->root.u.def.section;
5566 vlook = hlook->root.u.def.value;
5567
5568 i = 0;
5569 j = sym_count;
5570 while (i != j)
5571 {
5572 bfd_signed_vma vdiff;
5573 idx = (i + j) / 2;
5574 h = sorted_sym_hash[idx];
5575 vdiff = vlook - h->root.u.def.value;
5576 if (vdiff < 0)
5577 j = idx;
5578 else if (vdiff > 0)
5579 i = idx + 1;
5580 else
5581 {
5582 int sdiff = slook->id - h->root.u.def.section->id;
5583 if (sdiff < 0)
5584 j = idx;
5585 else if (sdiff > 0)
5586 i = idx + 1;
5587 else
5588 break;
5589 }
5590 }
5591
5592 /* We didn't find a value/section match. */
5593 if (i == j)
5594 continue;
5595
5596 /* With multiple aliases, or when the weak symbol is already
5597 strongly defined, we have multiple matching symbols and
5598 the binary search above may land on any of them. Step
5599 one past the matching symbol(s). */
5600 while (++idx != j)
5601 {
5602 h = sorted_sym_hash[idx];
5603 if (h->root.u.def.section != slook
5604 || h->root.u.def.value != vlook)
5605 break;
5606 }
5607
5608 /* Now look back over the aliases. Since we sorted by size
5609 as well as value and section, we'll choose the one with
5610 the largest size. */
5611 while (idx-- != i)
5612 {
5613 h = sorted_sym_hash[idx];
5614
5615 /* Stop if value or section doesn't match. */
5616 if (h->root.u.def.section != slook
5617 || h->root.u.def.value != vlook)
5618 break;
5619 else if (h != hlook)
5620 {
5621 struct elf_link_hash_entry *t;
5622
5623 hlook->u.alias = h;
5624 hlook->is_weakalias = 1;
5625 t = h;
5626 if (t->u.alias != NULL)
5627 while (t->u.alias != h)
5628 t = t->u.alias;
5629 t->u.alias = hlook;
5630
5631 /* If the weak definition is in the list of dynamic
5632 symbols, make sure the real definition is put
5633 there as well. */
5634 if (hlook->dynindx != -1 && h->dynindx == -1)
5635 {
5636 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5637 {
5638 err_free_sym_hash:
5639 free (sorted_sym_hash);
5640 goto error_return;
5641 }
5642 }
5643
5644 /* If the real definition is in the list of dynamic
5645 symbols, make sure the weak definition is put
5646 there as well. If we don't do this, then the
5647 dynamic loader might not merge the entries for the
5648 real definition and the weak definition. */
5649 if (h->dynindx != -1 && hlook->dynindx == -1)
5650 {
5651 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5652 goto err_free_sym_hash;
5653 }
5654 break;
5655 }
5656 }
5657 }
5658
5659 free (sorted_sym_hash);
5660 }
5661
5662 if (bed->check_directives
5663 && !(*bed->check_directives) (abfd, info))
5664 return FALSE;
5665
5666 /* If this is a non-traditional link, try to optimize the handling
5667 of the .stab/.stabstr sections. */
5668 if (! dynamic
5669 && ! info->traditional_format
5670 && is_elf_hash_table (htab)
5671 && (info->strip != strip_all && info->strip != strip_debugger))
5672 {
5673 asection *stabstr;
5674
5675 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5676 if (stabstr != NULL)
5677 {
5678 bfd_size_type string_offset = 0;
5679 asection *stab;
5680
5681 for (stab = abfd->sections; stab; stab = stab->next)
5682 if (CONST_STRNEQ (stab->name, ".stab")
5683 && (!stab->name[5] ||
5684 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5685 && (stab->flags & SEC_MERGE) == 0
5686 && !bfd_is_abs_section (stab->output_section))
5687 {
5688 struct bfd_elf_section_data *secdata;
5689
5690 secdata = elf_section_data (stab);
5691 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5692 stabstr, &secdata->sec_info,
5693 &string_offset))
5694 goto error_return;
5695 if (secdata->sec_info)
5696 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5697 }
5698 }
5699 }
5700
5701 if (dynamic && add_needed)
5702 {
5703 /* Add this bfd to the loaded list. */
5704 struct elf_link_loaded_list *n;
5705
5706 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5707 if (n == NULL)
5708 goto error_return;
5709 n->abfd = abfd;
5710 n->next = htab->dyn_loaded;
5711 htab->dyn_loaded = n;
5712 }
5713 if (dynamic && !add_needed
5714 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5715 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5716
5717 return TRUE;
5718
5719 error_free_vers:
5720 free (old_tab);
5721 free (old_strtab);
5722 free (nondeflt_vers);
5723 free (extversym);
5724 error_free_sym:
5725 free (isymbuf);
5726 error_return:
5727 return FALSE;
5728 }
5729
5730 /* Return the linker hash table entry of a symbol that might be
5731 satisfied by an archive symbol. Return -1 on error. */
5732
5733 struct elf_link_hash_entry *
5734 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5735 struct bfd_link_info *info,
5736 const char *name)
5737 {
5738 struct elf_link_hash_entry *h;
5739 char *p, *copy;
5740 size_t len, first;
5741
5742 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5743 if (h != NULL)
5744 return h;
5745
5746 /* If this is a default version (the name contains @@), look up the
5747 symbol again with only one `@' as well as without the version.
5748 The effect is that references to the symbol with and without the
5749 version will be matched by the default symbol in the archive. */
5750
5751 p = strchr (name, ELF_VER_CHR);
5752 if (p == NULL || p[1] != ELF_VER_CHR)
5753 return h;
5754
5755 /* First check with only one `@'. */
5756 len = strlen (name);
5757 copy = (char *) bfd_alloc (abfd, len);
5758 if (copy == NULL)
5759 return (struct elf_link_hash_entry *) -1;
5760
5761 first = p - name + 1;
5762 memcpy (copy, name, first);
5763 memcpy (copy + first, name + first + 1, len - first);
5764
5765 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5766 if (h == NULL)
5767 {
5768 /* We also need to check references to the symbol without the
5769 version. */
5770 copy[first - 1] = '\0';
5771 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5772 FALSE, FALSE, TRUE);
5773 }
5774
5775 bfd_release (abfd, copy);
5776 return h;
5777 }
5778
5779 /* Add symbols from an ELF archive file to the linker hash table. We
5780 don't use _bfd_generic_link_add_archive_symbols because we need to
5781 handle versioned symbols.
5782
5783 Fortunately, ELF archive handling is simpler than that done by
5784 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5785 oddities. In ELF, if we find a symbol in the archive map, and the
5786 symbol is currently undefined, we know that we must pull in that
5787 object file.
5788
5789 Unfortunately, we do have to make multiple passes over the symbol
5790 table until nothing further is resolved. */
5791
5792 static bfd_boolean
5793 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5794 {
5795 symindex c;
5796 unsigned char *included = NULL;
5797 carsym *symdefs;
5798 bfd_boolean loop;
5799 size_t amt;
5800 const struct elf_backend_data *bed;
5801 struct elf_link_hash_entry * (*archive_symbol_lookup)
5802 (bfd *, struct bfd_link_info *, const char *);
5803
5804 if (! bfd_has_map (abfd))
5805 {
5806 /* An empty archive is a special case. */
5807 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5808 return TRUE;
5809 bfd_set_error (bfd_error_no_armap);
5810 return FALSE;
5811 }
5812
5813 /* Keep track of all symbols we know to be already defined, and all
5814 files we know to be already included. This is to speed up the
5815 second and subsequent passes. */
5816 c = bfd_ardata (abfd)->symdef_count;
5817 if (c == 0)
5818 return TRUE;
5819 amt = c * sizeof (*included);
5820 included = (unsigned char *) bfd_zmalloc (amt);
5821 if (included == NULL)
5822 return FALSE;
5823
5824 symdefs = bfd_ardata (abfd)->symdefs;
5825 bed = get_elf_backend_data (abfd);
5826 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5827
5828 do
5829 {
5830 file_ptr last;
5831 symindex i;
5832 carsym *symdef;
5833 carsym *symdefend;
5834
5835 loop = FALSE;
5836 last = -1;
5837
5838 symdef = symdefs;
5839 symdefend = symdef + c;
5840 for (i = 0; symdef < symdefend; symdef++, i++)
5841 {
5842 struct elf_link_hash_entry *h;
5843 bfd *element;
5844 struct bfd_link_hash_entry *undefs_tail;
5845 symindex mark;
5846
5847 if (included[i])
5848 continue;
5849 if (symdef->file_offset == last)
5850 {
5851 included[i] = TRUE;
5852 continue;
5853 }
5854
5855 h = archive_symbol_lookup (abfd, info, symdef->name);
5856 if (h == (struct elf_link_hash_entry *) -1)
5857 goto error_return;
5858
5859 if (h == NULL)
5860 continue;
5861
5862 if (h->root.type == bfd_link_hash_undefined)
5863 {
5864 /* If the archive element has already been loaded then one
5865 of the symbols defined by that element might have been
5866 made undefined due to being in a discarded section. */
5867 if (h->indx == -3)
5868 continue;
5869 }
5870 else if (h->root.type == bfd_link_hash_common)
5871 {
5872 /* We currently have a common symbol. The archive map contains
5873 a reference to this symbol, so we may want to include it. We
5874 only want to include it however, if this archive element
5875 contains a definition of the symbol, not just another common
5876 declaration of it.
5877
5878 Unfortunately some archivers (including GNU ar) will put
5879 declarations of common symbols into their archive maps, as
5880 well as real definitions, so we cannot just go by the archive
5881 map alone. Instead we must read in the element's symbol
5882 table and check that to see what kind of symbol definition
5883 this is. */
5884 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5885 continue;
5886 }
5887 else
5888 {
5889 if (h->root.type != bfd_link_hash_undefweak)
5890 /* Symbol must be defined. Don't check it again. */
5891 included[i] = TRUE;
5892 continue;
5893 }
5894
5895 /* We need to include this archive member. */
5896 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5897 if (element == NULL)
5898 goto error_return;
5899
5900 if (! bfd_check_format (element, bfd_object))
5901 goto error_return;
5902
5903 undefs_tail = info->hash->undefs_tail;
5904
5905 if (!(*info->callbacks
5906 ->add_archive_element) (info, element, symdef->name, &element))
5907 continue;
5908 if (!bfd_link_add_symbols (element, info))
5909 goto error_return;
5910
5911 /* If there are any new undefined symbols, we need to make
5912 another pass through the archive in order to see whether
5913 they can be defined. FIXME: This isn't perfect, because
5914 common symbols wind up on undefs_tail and because an
5915 undefined symbol which is defined later on in this pass
5916 does not require another pass. This isn't a bug, but it
5917 does make the code less efficient than it could be. */
5918 if (undefs_tail != info->hash->undefs_tail)
5919 loop = TRUE;
5920
5921 /* Look backward to mark all symbols from this object file
5922 which we have already seen in this pass. */
5923 mark = i;
5924 do
5925 {
5926 included[mark] = TRUE;
5927 if (mark == 0)
5928 break;
5929 --mark;
5930 }
5931 while (symdefs[mark].file_offset == symdef->file_offset);
5932
5933 /* We mark subsequent symbols from this object file as we go
5934 on through the loop. */
5935 last = symdef->file_offset;
5936 }
5937 }
5938 while (loop);
5939
5940 free (included);
5941 return TRUE;
5942
5943 error_return:
5944 free (included);
5945 return FALSE;
5946 }
5947
5948 /* Given an ELF BFD, add symbols to the global hash table as
5949 appropriate. */
5950
5951 bfd_boolean
5952 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5953 {
5954 switch (bfd_get_format (abfd))
5955 {
5956 case bfd_object:
5957 return elf_link_add_object_symbols (abfd, info);
5958 case bfd_archive:
5959 return elf_link_add_archive_symbols (abfd, info);
5960 default:
5961 bfd_set_error (bfd_error_wrong_format);
5962 return FALSE;
5963 }
5964 }
5965 \f
5966 struct hash_codes_info
5967 {
5968 unsigned long *hashcodes;
5969 bfd_boolean error;
5970 };
5971
5972 /* This function will be called though elf_link_hash_traverse to store
5973 all hash value of the exported symbols in an array. */
5974
5975 static bfd_boolean
5976 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5977 {
5978 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5979 const char *name;
5980 unsigned long ha;
5981 char *alc = NULL;
5982
5983 /* Ignore indirect symbols. These are added by the versioning code. */
5984 if (h->dynindx == -1)
5985 return TRUE;
5986
5987 name = h->root.root.string;
5988 if (h->versioned >= versioned)
5989 {
5990 char *p = strchr (name, ELF_VER_CHR);
5991 if (p != NULL)
5992 {
5993 alc = (char *) bfd_malloc (p - name + 1);
5994 if (alc == NULL)
5995 {
5996 inf->error = TRUE;
5997 return FALSE;
5998 }
5999 memcpy (alc, name, p - name);
6000 alc[p - name] = '\0';
6001 name = alc;
6002 }
6003 }
6004
6005 /* Compute the hash value. */
6006 ha = bfd_elf_hash (name);
6007
6008 /* Store the found hash value in the array given as the argument. */
6009 *(inf->hashcodes)++ = ha;
6010
6011 /* And store it in the struct so that we can put it in the hash table
6012 later. */
6013 h->u.elf_hash_value = ha;
6014
6015 free (alc);
6016 return TRUE;
6017 }
6018
6019 struct collect_gnu_hash_codes
6020 {
6021 bfd *output_bfd;
6022 const struct elf_backend_data *bed;
6023 unsigned long int nsyms;
6024 unsigned long int maskbits;
6025 unsigned long int *hashcodes;
6026 unsigned long int *hashval;
6027 unsigned long int *indx;
6028 unsigned long int *counts;
6029 bfd_vma *bitmask;
6030 bfd_byte *contents;
6031 bfd_size_type xlat;
6032 long int min_dynindx;
6033 unsigned long int bucketcount;
6034 unsigned long int symindx;
6035 long int local_indx;
6036 long int shift1, shift2;
6037 unsigned long int mask;
6038 bfd_boolean error;
6039 };
6040
6041 /* This function will be called though elf_link_hash_traverse to store
6042 all hash value of the exported symbols in an array. */
6043
6044 static bfd_boolean
6045 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6046 {
6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6048 const char *name;
6049 unsigned long ha;
6050 char *alc = NULL;
6051
6052 /* Ignore indirect symbols. These are added by the versioning code. */
6053 if (h->dynindx == -1)
6054 return TRUE;
6055
6056 /* Ignore also local symbols and undefined symbols. */
6057 if (! (*s->bed->elf_hash_symbol) (h))
6058 return TRUE;
6059
6060 name = h->root.root.string;
6061 if (h->versioned >= versioned)
6062 {
6063 char *p = strchr (name, ELF_VER_CHR);
6064 if (p != NULL)
6065 {
6066 alc = (char *) bfd_malloc (p - name + 1);
6067 if (alc == NULL)
6068 {
6069 s->error = TRUE;
6070 return FALSE;
6071 }
6072 memcpy (alc, name, p - name);
6073 alc[p - name] = '\0';
6074 name = alc;
6075 }
6076 }
6077
6078 /* Compute the hash value. */
6079 ha = bfd_elf_gnu_hash (name);
6080
6081 /* Store the found hash value in the array for compute_bucket_count,
6082 and also for .dynsym reordering purposes. */
6083 s->hashcodes[s->nsyms] = ha;
6084 s->hashval[h->dynindx] = ha;
6085 ++s->nsyms;
6086 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6087 s->min_dynindx = h->dynindx;
6088
6089 free (alc);
6090 return TRUE;
6091 }
6092
6093 /* This function will be called though elf_link_hash_traverse to do
6094 final dynamic symbol renumbering in case of .gnu.hash.
6095 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6096 to the translation table. */
6097
6098 static bfd_boolean
6099 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6100 {
6101 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6102 unsigned long int bucket;
6103 unsigned long int val;
6104
6105 /* Ignore indirect symbols. */
6106 if (h->dynindx == -1)
6107 return TRUE;
6108
6109 /* Ignore also local symbols and undefined symbols. */
6110 if (! (*s->bed->elf_hash_symbol) (h))
6111 {
6112 if (h->dynindx >= s->min_dynindx)
6113 {
6114 if (s->bed->record_xhash_symbol != NULL)
6115 {
6116 (*s->bed->record_xhash_symbol) (h, 0);
6117 s->local_indx++;
6118 }
6119 else
6120 h->dynindx = s->local_indx++;
6121 }
6122 return TRUE;
6123 }
6124
6125 bucket = s->hashval[h->dynindx] % s->bucketcount;
6126 val = (s->hashval[h->dynindx] >> s->shift1)
6127 & ((s->maskbits >> s->shift1) - 1);
6128 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6129 s->bitmask[val]
6130 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6131 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6132 if (s->counts[bucket] == 1)
6133 /* Last element terminates the chain. */
6134 val |= 1;
6135 bfd_put_32 (s->output_bfd, val,
6136 s->contents + (s->indx[bucket] - s->symindx) * 4);
6137 --s->counts[bucket];
6138 if (s->bed->record_xhash_symbol != NULL)
6139 {
6140 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6141
6142 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6143 }
6144 else
6145 h->dynindx = s->indx[bucket]++;
6146 return TRUE;
6147 }
6148
6149 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6150
6151 bfd_boolean
6152 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6153 {
6154 return !(h->forced_local
6155 || h->root.type == bfd_link_hash_undefined
6156 || h->root.type == bfd_link_hash_undefweak
6157 || ((h->root.type == bfd_link_hash_defined
6158 || h->root.type == bfd_link_hash_defweak)
6159 && h->root.u.def.section->output_section == NULL));
6160 }
6161
6162 /* Array used to determine the number of hash table buckets to use
6163 based on the number of symbols there are. If there are fewer than
6164 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6165 fewer than 37 we use 17 buckets, and so forth. We never use more
6166 than 32771 buckets. */
6167
6168 static const size_t elf_buckets[] =
6169 {
6170 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6171 16411, 32771, 0
6172 };
6173
6174 /* Compute bucket count for hashing table. We do not use a static set
6175 of possible tables sizes anymore. Instead we determine for all
6176 possible reasonable sizes of the table the outcome (i.e., the
6177 number of collisions etc) and choose the best solution. The
6178 weighting functions are not too simple to allow the table to grow
6179 without bounds. Instead one of the weighting factors is the size.
6180 Therefore the result is always a good payoff between few collisions
6181 (= short chain lengths) and table size. */
6182 static size_t
6183 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6184 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6185 unsigned long int nsyms,
6186 int gnu_hash)
6187 {
6188 size_t best_size = 0;
6189 unsigned long int i;
6190
6191 /* We have a problem here. The following code to optimize the table
6192 size requires an integer type with more the 32 bits. If
6193 BFD_HOST_U_64_BIT is set we know about such a type. */
6194 #ifdef BFD_HOST_U_64_BIT
6195 if (info->optimize)
6196 {
6197 size_t minsize;
6198 size_t maxsize;
6199 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6200 bfd *dynobj = elf_hash_table (info)->dynobj;
6201 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6202 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6203 unsigned long int *counts;
6204 bfd_size_type amt;
6205 unsigned int no_improvement_count = 0;
6206
6207 /* Possible optimization parameters: if we have NSYMS symbols we say
6208 that the hashing table must at least have NSYMS/4 and at most
6209 2*NSYMS buckets. */
6210 minsize = nsyms / 4;
6211 if (minsize == 0)
6212 minsize = 1;
6213 best_size = maxsize = nsyms * 2;
6214 if (gnu_hash)
6215 {
6216 if (minsize < 2)
6217 minsize = 2;
6218 if ((best_size & 31) == 0)
6219 ++best_size;
6220 }
6221
6222 /* Create array where we count the collisions in. We must use bfd_malloc
6223 since the size could be large. */
6224 amt = maxsize;
6225 amt *= sizeof (unsigned long int);
6226 counts = (unsigned long int *) bfd_malloc (amt);
6227 if (counts == NULL)
6228 return 0;
6229
6230 /* Compute the "optimal" size for the hash table. The criteria is a
6231 minimal chain length. The minor criteria is (of course) the size
6232 of the table. */
6233 for (i = minsize; i < maxsize; ++i)
6234 {
6235 /* Walk through the array of hashcodes and count the collisions. */
6236 BFD_HOST_U_64_BIT max;
6237 unsigned long int j;
6238 unsigned long int fact;
6239
6240 if (gnu_hash && (i & 31) == 0)
6241 continue;
6242
6243 memset (counts, '\0', i * sizeof (unsigned long int));
6244
6245 /* Determine how often each hash bucket is used. */
6246 for (j = 0; j < nsyms; ++j)
6247 ++counts[hashcodes[j] % i];
6248
6249 /* For the weight function we need some information about the
6250 pagesize on the target. This is information need not be 100%
6251 accurate. Since this information is not available (so far) we
6252 define it here to a reasonable default value. If it is crucial
6253 to have a better value some day simply define this value. */
6254 # ifndef BFD_TARGET_PAGESIZE
6255 # define BFD_TARGET_PAGESIZE (4096)
6256 # endif
6257
6258 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6259 and the chains. */
6260 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6261
6262 # if 1
6263 /* Variant 1: optimize for short chains. We add the squares
6264 of all the chain lengths (which favors many small chain
6265 over a few long chains). */
6266 for (j = 0; j < i; ++j)
6267 max += counts[j] * counts[j];
6268
6269 /* This adds penalties for the overall size of the table. */
6270 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6271 max *= fact * fact;
6272 # else
6273 /* Variant 2: Optimize a lot more for small table. Here we
6274 also add squares of the size but we also add penalties for
6275 empty slots (the +1 term). */
6276 for (j = 0; j < i; ++j)
6277 max += (1 + counts[j]) * (1 + counts[j]);
6278
6279 /* The overall size of the table is considered, but not as
6280 strong as in variant 1, where it is squared. */
6281 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6282 max *= fact;
6283 # endif
6284
6285 /* Compare with current best results. */
6286 if (max < best_chlen)
6287 {
6288 best_chlen = max;
6289 best_size = i;
6290 no_improvement_count = 0;
6291 }
6292 /* PR 11843: Avoid futile long searches for the best bucket size
6293 when there are a large number of symbols. */
6294 else if (++no_improvement_count == 100)
6295 break;
6296 }
6297
6298 free (counts);
6299 }
6300 else
6301 #endif /* defined (BFD_HOST_U_64_BIT) */
6302 {
6303 /* This is the fallback solution if no 64bit type is available or if we
6304 are not supposed to spend much time on optimizations. We select the
6305 bucket count using a fixed set of numbers. */
6306 for (i = 0; elf_buckets[i] != 0; i++)
6307 {
6308 best_size = elf_buckets[i];
6309 if (nsyms < elf_buckets[i + 1])
6310 break;
6311 }
6312 if (gnu_hash && best_size < 2)
6313 best_size = 2;
6314 }
6315
6316 return best_size;
6317 }
6318
6319 /* Size any SHT_GROUP section for ld -r. */
6320
6321 bfd_boolean
6322 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6323 {
6324 bfd *ibfd;
6325 asection *s;
6326
6327 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6328 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6329 && (s = ibfd->sections) != NULL
6330 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6331 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6332 return FALSE;
6333 return TRUE;
6334 }
6335
6336 /* Set a default stack segment size. The value in INFO wins. If it
6337 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6338 undefined it is initialized. */
6339
6340 bfd_boolean
6341 bfd_elf_stack_segment_size (bfd *output_bfd,
6342 struct bfd_link_info *info,
6343 const char *legacy_symbol,
6344 bfd_vma default_size)
6345 {
6346 struct elf_link_hash_entry *h = NULL;
6347
6348 /* Look for legacy symbol. */
6349 if (legacy_symbol)
6350 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6351 FALSE, FALSE, FALSE);
6352 if (h && (h->root.type == bfd_link_hash_defined
6353 || h->root.type == bfd_link_hash_defweak)
6354 && h->def_regular
6355 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6356 {
6357 /* The symbol has no type if specified on the command line. */
6358 h->type = STT_OBJECT;
6359 if (info->stacksize)
6360 /* xgettext:c-format */
6361 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6362 output_bfd, legacy_symbol);
6363 else if (h->root.u.def.section != bfd_abs_section_ptr)
6364 /* xgettext:c-format */
6365 _bfd_error_handler (_("%pB: %s not absolute"),
6366 output_bfd, legacy_symbol);
6367 else
6368 info->stacksize = h->root.u.def.value;
6369 }
6370
6371 if (!info->stacksize)
6372 /* If the user didn't set a size, or explicitly inhibit the
6373 size, set it now. */
6374 info->stacksize = default_size;
6375
6376 /* Provide the legacy symbol, if it is referenced. */
6377 if (h && (h->root.type == bfd_link_hash_undefined
6378 || h->root.type == bfd_link_hash_undefweak))
6379 {
6380 struct bfd_link_hash_entry *bh = NULL;
6381
6382 if (!(_bfd_generic_link_add_one_symbol
6383 (info, output_bfd, legacy_symbol,
6384 BSF_GLOBAL, bfd_abs_section_ptr,
6385 info->stacksize >= 0 ? info->stacksize : 0,
6386 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6387 return FALSE;
6388
6389 h = (struct elf_link_hash_entry *) bh;
6390 h->def_regular = 1;
6391 h->type = STT_OBJECT;
6392 }
6393
6394 return TRUE;
6395 }
6396
6397 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6398
6399 struct elf_gc_sweep_symbol_info
6400 {
6401 struct bfd_link_info *info;
6402 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6403 bfd_boolean);
6404 };
6405
6406 static bfd_boolean
6407 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6408 {
6409 if (!h->mark
6410 && (((h->root.type == bfd_link_hash_defined
6411 || h->root.type == bfd_link_hash_defweak)
6412 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6413 && h->root.u.def.section->gc_mark))
6414 || h->root.type == bfd_link_hash_undefined
6415 || h->root.type == bfd_link_hash_undefweak))
6416 {
6417 struct elf_gc_sweep_symbol_info *inf;
6418
6419 inf = (struct elf_gc_sweep_symbol_info *) data;
6420 (*inf->hide_symbol) (inf->info, h, TRUE);
6421 h->def_regular = 0;
6422 h->ref_regular = 0;
6423 h->ref_regular_nonweak = 0;
6424 }
6425
6426 return TRUE;
6427 }
6428
6429 /* Set up the sizes and contents of the ELF dynamic sections. This is
6430 called by the ELF linker emulation before_allocation routine. We
6431 must set the sizes of the sections before the linker sets the
6432 addresses of the various sections. */
6433
6434 bfd_boolean
6435 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6436 const char *soname,
6437 const char *rpath,
6438 const char *filter_shlib,
6439 const char *audit,
6440 const char *depaudit,
6441 const char * const *auxiliary_filters,
6442 struct bfd_link_info *info,
6443 asection **sinterpptr)
6444 {
6445 bfd *dynobj;
6446 const struct elf_backend_data *bed;
6447
6448 *sinterpptr = NULL;
6449
6450 if (!is_elf_hash_table (info->hash))
6451 return TRUE;
6452
6453 dynobj = elf_hash_table (info)->dynobj;
6454
6455 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6456 {
6457 struct bfd_elf_version_tree *verdefs;
6458 struct elf_info_failed asvinfo;
6459 struct bfd_elf_version_tree *t;
6460 struct bfd_elf_version_expr *d;
6461 asection *s;
6462 size_t soname_indx;
6463
6464 /* If we are supposed to export all symbols into the dynamic symbol
6465 table (this is not the normal case), then do so. */
6466 if (info->export_dynamic
6467 || (bfd_link_executable (info) && info->dynamic))
6468 {
6469 struct elf_info_failed eif;
6470
6471 eif.info = info;
6472 eif.failed = FALSE;
6473 elf_link_hash_traverse (elf_hash_table (info),
6474 _bfd_elf_export_symbol,
6475 &eif);
6476 if (eif.failed)
6477 return FALSE;
6478 }
6479
6480 if (soname != NULL)
6481 {
6482 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6483 soname, TRUE);
6484 if (soname_indx == (size_t) -1
6485 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6486 return FALSE;
6487 }
6488 else
6489 soname_indx = (size_t) -1;
6490
6491 /* Make all global versions with definition. */
6492 for (t = info->version_info; t != NULL; t = t->next)
6493 for (d = t->globals.list; d != NULL; d = d->next)
6494 if (!d->symver && d->literal)
6495 {
6496 const char *verstr, *name;
6497 size_t namelen, verlen, newlen;
6498 char *newname, *p, leading_char;
6499 struct elf_link_hash_entry *newh;
6500
6501 leading_char = bfd_get_symbol_leading_char (output_bfd);
6502 name = d->pattern;
6503 namelen = strlen (name) + (leading_char != '\0');
6504 verstr = t->name;
6505 verlen = strlen (verstr);
6506 newlen = namelen + verlen + 3;
6507
6508 newname = (char *) bfd_malloc (newlen);
6509 if (newname == NULL)
6510 return FALSE;
6511 newname[0] = leading_char;
6512 memcpy (newname + (leading_char != '\0'), name, namelen);
6513
6514 /* Check the hidden versioned definition. */
6515 p = newname + namelen;
6516 *p++ = ELF_VER_CHR;
6517 memcpy (p, verstr, verlen + 1);
6518 newh = elf_link_hash_lookup (elf_hash_table (info),
6519 newname, FALSE, FALSE,
6520 FALSE);
6521 if (newh == NULL
6522 || (newh->root.type != bfd_link_hash_defined
6523 && newh->root.type != bfd_link_hash_defweak))
6524 {
6525 /* Check the default versioned definition. */
6526 *p++ = ELF_VER_CHR;
6527 memcpy (p, verstr, verlen + 1);
6528 newh = elf_link_hash_lookup (elf_hash_table (info),
6529 newname, FALSE, FALSE,
6530 FALSE);
6531 }
6532 free (newname);
6533
6534 /* Mark this version if there is a definition and it is
6535 not defined in a shared object. */
6536 if (newh != NULL
6537 && !newh->def_dynamic
6538 && (newh->root.type == bfd_link_hash_defined
6539 || newh->root.type == bfd_link_hash_defweak))
6540 d->symver = 1;
6541 }
6542
6543 /* Attach all the symbols to their version information. */
6544 asvinfo.info = info;
6545 asvinfo.failed = FALSE;
6546
6547 elf_link_hash_traverse (elf_hash_table (info),
6548 _bfd_elf_link_assign_sym_version,
6549 &asvinfo);
6550 if (asvinfo.failed)
6551 return FALSE;
6552
6553 if (!info->allow_undefined_version)
6554 {
6555 /* Check if all global versions have a definition. */
6556 bfd_boolean all_defined = TRUE;
6557 for (t = info->version_info; t != NULL; t = t->next)
6558 for (d = t->globals.list; d != NULL; d = d->next)
6559 if (d->literal && !d->symver && !d->script)
6560 {
6561 _bfd_error_handler
6562 (_("%s: undefined version: %s"),
6563 d->pattern, t->name);
6564 all_defined = FALSE;
6565 }
6566
6567 if (!all_defined)
6568 {
6569 bfd_set_error (bfd_error_bad_value);
6570 return FALSE;
6571 }
6572 }
6573
6574 /* Set up the version definition section. */
6575 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6576 BFD_ASSERT (s != NULL);
6577
6578 /* We may have created additional version definitions if we are
6579 just linking a regular application. */
6580 verdefs = info->version_info;
6581
6582 /* Skip anonymous version tag. */
6583 if (verdefs != NULL && verdefs->vernum == 0)
6584 verdefs = verdefs->next;
6585
6586 if (verdefs == NULL && !info->create_default_symver)
6587 s->flags |= SEC_EXCLUDE;
6588 else
6589 {
6590 unsigned int cdefs;
6591 bfd_size_type size;
6592 bfd_byte *p;
6593 Elf_Internal_Verdef def;
6594 Elf_Internal_Verdaux defaux;
6595 struct bfd_link_hash_entry *bh;
6596 struct elf_link_hash_entry *h;
6597 const char *name;
6598
6599 cdefs = 0;
6600 size = 0;
6601
6602 /* Make space for the base version. */
6603 size += sizeof (Elf_External_Verdef);
6604 size += sizeof (Elf_External_Verdaux);
6605 ++cdefs;
6606
6607 /* Make space for the default version. */
6608 if (info->create_default_symver)
6609 {
6610 size += sizeof (Elf_External_Verdef);
6611 ++cdefs;
6612 }
6613
6614 for (t = verdefs; t != NULL; t = t->next)
6615 {
6616 struct bfd_elf_version_deps *n;
6617
6618 /* Don't emit base version twice. */
6619 if (t->vernum == 0)
6620 continue;
6621
6622 size += sizeof (Elf_External_Verdef);
6623 size += sizeof (Elf_External_Verdaux);
6624 ++cdefs;
6625
6626 for (n = t->deps; n != NULL; n = n->next)
6627 size += sizeof (Elf_External_Verdaux);
6628 }
6629
6630 s->size = size;
6631 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6632 if (s->contents == NULL && s->size != 0)
6633 return FALSE;
6634
6635 /* Fill in the version definition section. */
6636
6637 p = s->contents;
6638
6639 def.vd_version = VER_DEF_CURRENT;
6640 def.vd_flags = VER_FLG_BASE;
6641 def.vd_ndx = 1;
6642 def.vd_cnt = 1;
6643 if (info->create_default_symver)
6644 {
6645 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6646 def.vd_next = sizeof (Elf_External_Verdef);
6647 }
6648 else
6649 {
6650 def.vd_aux = sizeof (Elf_External_Verdef);
6651 def.vd_next = (sizeof (Elf_External_Verdef)
6652 + sizeof (Elf_External_Verdaux));
6653 }
6654
6655 if (soname_indx != (size_t) -1)
6656 {
6657 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6658 soname_indx);
6659 def.vd_hash = bfd_elf_hash (soname);
6660 defaux.vda_name = soname_indx;
6661 name = soname;
6662 }
6663 else
6664 {
6665 size_t indx;
6666
6667 name = lbasename (bfd_get_filename (output_bfd));
6668 def.vd_hash = bfd_elf_hash (name);
6669 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6670 name, FALSE);
6671 if (indx == (size_t) -1)
6672 return FALSE;
6673 defaux.vda_name = indx;
6674 }
6675 defaux.vda_next = 0;
6676
6677 _bfd_elf_swap_verdef_out (output_bfd, &def,
6678 (Elf_External_Verdef *) p);
6679 p += sizeof (Elf_External_Verdef);
6680 if (info->create_default_symver)
6681 {
6682 /* Add a symbol representing this version. */
6683 bh = NULL;
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6686 0, NULL, FALSE,
6687 get_elf_backend_data (dynobj)->collect, &bh)))
6688 return FALSE;
6689 h = (struct elf_link_hash_entry *) bh;
6690 h->non_elf = 0;
6691 h->def_regular = 1;
6692 h->type = STT_OBJECT;
6693 h->verinfo.vertree = NULL;
6694
6695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6696 return FALSE;
6697
6698 /* Create a duplicate of the base version with the same
6699 aux block, but different flags. */
6700 def.vd_flags = 0;
6701 def.vd_ndx = 2;
6702 def.vd_aux = sizeof (Elf_External_Verdef);
6703 if (verdefs)
6704 def.vd_next = (sizeof (Elf_External_Verdef)
6705 + sizeof (Elf_External_Verdaux));
6706 else
6707 def.vd_next = 0;
6708 _bfd_elf_swap_verdef_out (output_bfd, &def,
6709 (Elf_External_Verdef *) p);
6710 p += sizeof (Elf_External_Verdef);
6711 }
6712 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6713 (Elf_External_Verdaux *) p);
6714 p += sizeof (Elf_External_Verdaux);
6715
6716 for (t = verdefs; t != NULL; t = t->next)
6717 {
6718 unsigned int cdeps;
6719 struct bfd_elf_version_deps *n;
6720
6721 /* Don't emit the base version twice. */
6722 if (t->vernum == 0)
6723 continue;
6724
6725 cdeps = 0;
6726 for (n = t->deps; n != NULL; n = n->next)
6727 ++cdeps;
6728
6729 /* Add a symbol representing this version. */
6730 bh = NULL;
6731 if (! (_bfd_generic_link_add_one_symbol
6732 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6733 0, NULL, FALSE,
6734 get_elf_backend_data (dynobj)->collect, &bh)))
6735 return FALSE;
6736 h = (struct elf_link_hash_entry *) bh;
6737 h->non_elf = 0;
6738 h->def_regular = 1;
6739 h->type = STT_OBJECT;
6740 h->verinfo.vertree = t;
6741
6742 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6743 return FALSE;
6744
6745 def.vd_version = VER_DEF_CURRENT;
6746 def.vd_flags = 0;
6747 if (t->globals.list == NULL
6748 && t->locals.list == NULL
6749 && ! t->used)
6750 def.vd_flags |= VER_FLG_WEAK;
6751 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6752 def.vd_cnt = cdeps + 1;
6753 def.vd_hash = bfd_elf_hash (t->name);
6754 def.vd_aux = sizeof (Elf_External_Verdef);
6755 def.vd_next = 0;
6756
6757 /* If a basever node is next, it *must* be the last node in
6758 the chain, otherwise Verdef construction breaks. */
6759 if (t->next != NULL && t->next->vernum == 0)
6760 BFD_ASSERT (t->next->next == NULL);
6761
6762 if (t->next != NULL && t->next->vernum != 0)
6763 def.vd_next = (sizeof (Elf_External_Verdef)
6764 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6765
6766 _bfd_elf_swap_verdef_out (output_bfd, &def,
6767 (Elf_External_Verdef *) p);
6768 p += sizeof (Elf_External_Verdef);
6769
6770 defaux.vda_name = h->dynstr_index;
6771 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6772 h->dynstr_index);
6773 defaux.vda_next = 0;
6774 if (t->deps != NULL)
6775 defaux.vda_next = sizeof (Elf_External_Verdaux);
6776 t->name_indx = defaux.vda_name;
6777
6778 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6779 (Elf_External_Verdaux *) p);
6780 p += sizeof (Elf_External_Verdaux);
6781
6782 for (n = t->deps; n != NULL; n = n->next)
6783 {
6784 if (n->version_needed == NULL)
6785 {
6786 /* This can happen if there was an error in the
6787 version script. */
6788 defaux.vda_name = 0;
6789 }
6790 else
6791 {
6792 defaux.vda_name = n->version_needed->name_indx;
6793 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6794 defaux.vda_name);
6795 }
6796 if (n->next == NULL)
6797 defaux.vda_next = 0;
6798 else
6799 defaux.vda_next = sizeof (Elf_External_Verdaux);
6800
6801 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6802 (Elf_External_Verdaux *) p);
6803 p += sizeof (Elf_External_Verdaux);
6804 }
6805 }
6806
6807 elf_tdata (output_bfd)->cverdefs = cdefs;
6808 }
6809 }
6810
6811 bed = get_elf_backend_data (output_bfd);
6812
6813 if (info->gc_sections && bed->can_gc_sections)
6814 {
6815 struct elf_gc_sweep_symbol_info sweep_info;
6816
6817 /* Remove the symbols that were in the swept sections from the
6818 dynamic symbol table. */
6819 sweep_info.info = info;
6820 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6821 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6822 &sweep_info);
6823 }
6824
6825 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6826 {
6827 asection *s;
6828 struct elf_find_verdep_info sinfo;
6829
6830 /* Work out the size of the version reference section. */
6831
6832 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6833 BFD_ASSERT (s != NULL);
6834
6835 sinfo.info = info;
6836 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6837 if (sinfo.vers == 0)
6838 sinfo.vers = 1;
6839 sinfo.failed = FALSE;
6840
6841 elf_link_hash_traverse (elf_hash_table (info),
6842 _bfd_elf_link_find_version_dependencies,
6843 &sinfo);
6844 if (sinfo.failed)
6845 return FALSE;
6846
6847 if (elf_tdata (output_bfd)->verref == NULL)
6848 s->flags |= SEC_EXCLUDE;
6849 else
6850 {
6851 Elf_Internal_Verneed *vn;
6852 unsigned int size;
6853 unsigned int crefs;
6854 bfd_byte *p;
6855
6856 /* Build the version dependency section. */
6857 size = 0;
6858 crefs = 0;
6859 for (vn = elf_tdata (output_bfd)->verref;
6860 vn != NULL;
6861 vn = vn->vn_nextref)
6862 {
6863 Elf_Internal_Vernaux *a;
6864
6865 size += sizeof (Elf_External_Verneed);
6866 ++crefs;
6867 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6868 size += sizeof (Elf_External_Vernaux);
6869 }
6870
6871 s->size = size;
6872 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6873 if (s->contents == NULL)
6874 return FALSE;
6875
6876 p = s->contents;
6877 for (vn = elf_tdata (output_bfd)->verref;
6878 vn != NULL;
6879 vn = vn->vn_nextref)
6880 {
6881 unsigned int caux;
6882 Elf_Internal_Vernaux *a;
6883 size_t indx;
6884
6885 caux = 0;
6886 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6887 ++caux;
6888
6889 vn->vn_version = VER_NEED_CURRENT;
6890 vn->vn_cnt = caux;
6891 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6892 elf_dt_name (vn->vn_bfd) != NULL
6893 ? elf_dt_name (vn->vn_bfd)
6894 : lbasename (bfd_get_filename
6895 (vn->vn_bfd)),
6896 FALSE);
6897 if (indx == (size_t) -1)
6898 return FALSE;
6899 vn->vn_file = indx;
6900 vn->vn_aux = sizeof (Elf_External_Verneed);
6901 if (vn->vn_nextref == NULL)
6902 vn->vn_next = 0;
6903 else
6904 vn->vn_next = (sizeof (Elf_External_Verneed)
6905 + caux * sizeof (Elf_External_Vernaux));
6906
6907 _bfd_elf_swap_verneed_out (output_bfd, vn,
6908 (Elf_External_Verneed *) p);
6909 p += sizeof (Elf_External_Verneed);
6910
6911 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6912 {
6913 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6914 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6915 a->vna_nodename, FALSE);
6916 if (indx == (size_t) -1)
6917 return FALSE;
6918 a->vna_name = indx;
6919 if (a->vna_nextptr == NULL)
6920 a->vna_next = 0;
6921 else
6922 a->vna_next = sizeof (Elf_External_Vernaux);
6923
6924 _bfd_elf_swap_vernaux_out (output_bfd, a,
6925 (Elf_External_Vernaux *) p);
6926 p += sizeof (Elf_External_Vernaux);
6927 }
6928 }
6929
6930 elf_tdata (output_bfd)->cverrefs = crefs;
6931 }
6932 }
6933
6934 /* Any syms created from now on start with -1 in
6935 got.refcount/offset and plt.refcount/offset. */
6936 elf_hash_table (info)->init_got_refcount
6937 = elf_hash_table (info)->init_got_offset;
6938 elf_hash_table (info)->init_plt_refcount
6939 = elf_hash_table (info)->init_plt_offset;
6940
6941 if (bfd_link_relocatable (info)
6942 && !_bfd_elf_size_group_sections (info))
6943 return FALSE;
6944
6945 /* The backend may have to create some sections regardless of whether
6946 we're dynamic or not. */
6947 if (bed->elf_backend_always_size_sections
6948 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6949 return FALSE;
6950
6951 /* Determine any GNU_STACK segment requirements, after the backend
6952 has had a chance to set a default segment size. */
6953 if (info->execstack)
6954 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6955 else if (info->noexecstack)
6956 elf_stack_flags (output_bfd) = PF_R | PF_W;
6957 else
6958 {
6959 bfd *inputobj;
6960 asection *notesec = NULL;
6961 int exec = 0;
6962
6963 for (inputobj = info->input_bfds;
6964 inputobj;
6965 inputobj = inputobj->link.next)
6966 {
6967 asection *s;
6968
6969 if (inputobj->flags
6970 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6971 continue;
6972 s = inputobj->sections;
6973 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6974 continue;
6975
6976 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6977 if (s)
6978 {
6979 if (s->flags & SEC_CODE)
6980 exec = PF_X;
6981 notesec = s;
6982 }
6983 else if (bed->default_execstack)
6984 exec = PF_X;
6985 }
6986 if (notesec || info->stacksize > 0)
6987 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6988 if (notesec && exec && bfd_link_relocatable (info)
6989 && notesec->output_section != bfd_abs_section_ptr)
6990 notesec->output_section->flags |= SEC_CODE;
6991 }
6992
6993 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6994 {
6995 struct elf_info_failed eif;
6996 struct elf_link_hash_entry *h;
6997 asection *dynstr;
6998 asection *s;
6999
7000 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7001 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7002
7003 if (info->symbolic)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7006 return FALSE;
7007 info->flags |= DF_SYMBOLIC;
7008 }
7009
7010 if (rpath != NULL)
7011 {
7012 size_t indx;
7013 bfd_vma tag;
7014
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7016 TRUE);
7017 if (indx == (size_t) -1)
7018 return FALSE;
7019
7020 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7021 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7022 return FALSE;
7023 }
7024
7025 if (filter_shlib != NULL)
7026 {
7027 size_t indx;
7028
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7030 filter_shlib, TRUE);
7031 if (indx == (size_t) -1
7032 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7033 return FALSE;
7034 }
7035
7036 if (auxiliary_filters != NULL)
7037 {
7038 const char * const *p;
7039
7040 for (p = auxiliary_filters; *p != NULL; p++)
7041 {
7042 size_t indx;
7043
7044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7045 *p, TRUE);
7046 if (indx == (size_t) -1
7047 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7048 return FALSE;
7049 }
7050 }
7051
7052 if (audit != NULL)
7053 {
7054 size_t indx;
7055
7056 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7057 TRUE);
7058 if (indx == (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7060 return FALSE;
7061 }
7062
7063 if (depaudit != NULL)
7064 {
7065 size_t indx;
7066
7067 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7068 TRUE);
7069 if (indx == (size_t) -1
7070 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7071 return FALSE;
7072 }
7073
7074 eif.info = info;
7075 eif.failed = FALSE;
7076
7077 /* Find all symbols which were defined in a dynamic object and make
7078 the backend pick a reasonable value for them. */
7079 elf_link_hash_traverse (elf_hash_table (info),
7080 _bfd_elf_adjust_dynamic_symbol,
7081 &eif);
7082 if (eif.failed)
7083 return FALSE;
7084
7085 /* Add some entries to the .dynamic section. We fill in some of the
7086 values later, in bfd_elf_final_link, but we must add the entries
7087 now so that we know the final size of the .dynamic section. */
7088
7089 /* If there are initialization and/or finalization functions to
7090 call then add the corresponding DT_INIT/DT_FINI entries. */
7091 h = (info->init_function
7092 ? elf_link_hash_lookup (elf_hash_table (info),
7093 info->init_function, FALSE,
7094 FALSE, FALSE)
7095 : NULL);
7096 if (h != NULL
7097 && (h->ref_regular
7098 || h->def_regular))
7099 {
7100 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7101 return FALSE;
7102 }
7103 h = (info->fini_function
7104 ? elf_link_hash_lookup (elf_hash_table (info),
7105 info->fini_function, FALSE,
7106 FALSE, FALSE)
7107 : NULL);
7108 if (h != NULL
7109 && (h->ref_regular
7110 || h->def_regular))
7111 {
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7113 return FALSE;
7114 }
7115
7116 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7117 if (s != NULL && s->linker_has_input)
7118 {
7119 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7120 if (! bfd_link_executable (info))
7121 {
7122 bfd *sub;
7123 asection *o;
7124
7125 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7126 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7127 && (o = sub->sections) != NULL
7128 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7129 for (o = sub->sections; o != NULL; o = o->next)
7130 if (elf_section_data (o)->this_hdr.sh_type
7131 == SHT_PREINIT_ARRAY)
7132 {
7133 _bfd_error_handler
7134 (_("%pB: .preinit_array section is not allowed in DSO"),
7135 sub);
7136 break;
7137 }
7138
7139 bfd_set_error (bfd_error_nonrepresentable_section);
7140 return FALSE;
7141 }
7142
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7144 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7145 return FALSE;
7146 }
7147 s = bfd_get_section_by_name (output_bfd, ".init_array");
7148 if (s != NULL && s->linker_has_input)
7149 {
7150 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7151 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7152 return FALSE;
7153 }
7154 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7155 if (s != NULL && s->linker_has_input)
7156 {
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7159 return FALSE;
7160 }
7161
7162 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7163 /* If .dynstr is excluded from the link, we don't want any of
7164 these tags. Strictly, we should be checking each section
7165 individually; This quick check covers for the case where
7166 someone does a /DISCARD/ : { *(*) }. */
7167 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7168 {
7169 bfd_size_type strsize;
7170
7171 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7172 if ((info->emit_hash
7173 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7174 || (info->emit_gnu_hash
7175 && (bed->record_xhash_symbol == NULL
7176 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7177 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7178 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7180 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7181 bed->s->sizeof_sym)
7182 || (info->gnu_flags_1
7183 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7184 info->gnu_flags_1)))
7185 return FALSE;
7186 }
7187 }
7188
7189 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7190 return FALSE;
7191
7192 /* The backend must work out the sizes of all the other dynamic
7193 sections. */
7194 if (dynobj != NULL
7195 && bed->elf_backend_size_dynamic_sections != NULL
7196 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7197 return FALSE;
7198
7199 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7200 {
7201 if (elf_tdata (output_bfd)->cverdefs)
7202 {
7203 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7204
7205 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7206 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7207 return FALSE;
7208 }
7209
7210 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7211 {
7212 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7213 return FALSE;
7214 }
7215 else if (info->flags & DF_BIND_NOW)
7216 {
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7218 return FALSE;
7219 }
7220
7221 if (info->flags_1)
7222 {
7223 if (bfd_link_executable (info))
7224 info->flags_1 &= ~ (DF_1_INITFIRST
7225 | DF_1_NODELETE
7226 | DF_1_NOOPEN);
7227 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7228 return FALSE;
7229 }
7230
7231 if (elf_tdata (output_bfd)->cverrefs)
7232 {
7233 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7234
7235 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7236 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7237 return FALSE;
7238 }
7239
7240 if ((elf_tdata (output_bfd)->cverrefs == 0
7241 && elf_tdata (output_bfd)->cverdefs == 0)
7242 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7243 {
7244 asection *s;
7245
7246 s = bfd_get_linker_section (dynobj, ".gnu.version");
7247 s->flags |= SEC_EXCLUDE;
7248 }
7249 }
7250 return TRUE;
7251 }
7252
7253 /* Find the first non-excluded output section. We'll use its
7254 section symbol for some emitted relocs. */
7255 void
7256 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7257 {
7258 asection *s;
7259 asection *found = NULL;
7260
7261 for (s = output_bfd->sections; s != NULL; s = s->next)
7262 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
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)->text_index_section = found;
7270 }
7271
7272 /* Find two non-excluded output sections, one for code, one for data.
7273 We'll use their section symbols for some emitted relocs. */
7274 void
7275 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7276 {
7277 asection *s;
7278 asection *found = NULL;
7279
7280 /* Data first, since setting text_index_section changes
7281 _bfd_elf_omit_section_dynsym_default. */
7282 for (s = output_bfd->sections; s != NULL; s = s->next)
7283 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7284 && !(s->flags & SEC_READONLY)
7285 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7286 {
7287 found = s;
7288 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7289 break;
7290 }
7291 elf_hash_table (info)->data_index_section = found;
7292
7293 for (s = output_bfd->sections; s != NULL; s = s->next)
7294 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7295 && (s->flags & SEC_READONLY)
7296 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7297 {
7298 found = s;
7299 break;
7300 }
7301 elf_hash_table (info)->text_index_section = found;
7302 }
7303
7304 #define GNU_HASH_SECTION_NAME(bed) \
7305 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7306
7307 bfd_boolean
7308 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7309 {
7310 const struct elf_backend_data *bed;
7311 unsigned long section_sym_count;
7312 bfd_size_type dynsymcount = 0;
7313
7314 if (!is_elf_hash_table (info->hash))
7315 return TRUE;
7316
7317 bed = get_elf_backend_data (output_bfd);
7318 (*bed->elf_backend_init_index_section) (output_bfd, info);
7319
7320 /* Assign dynsym indices. In a shared library we generate a section
7321 symbol for each output section, which come first. Next come all
7322 of the back-end allocated local dynamic syms, followed by the rest
7323 of the global symbols.
7324
7325 This is usually not needed for static binaries, however backends
7326 can request to always do it, e.g. the MIPS backend uses dynamic
7327 symbol counts to lay out GOT, which will be produced in the
7328 presence of GOT relocations even in static binaries (holding fixed
7329 data in that case, to satisfy those relocations). */
7330
7331 if (elf_hash_table (info)->dynamic_sections_created
7332 || bed->always_renumber_dynsyms)
7333 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7334 &section_sym_count);
7335
7336 if (elf_hash_table (info)->dynamic_sections_created)
7337 {
7338 bfd *dynobj;
7339 asection *s;
7340 unsigned int dtagcount;
7341
7342 dynobj = elf_hash_table (info)->dynobj;
7343
7344 /* Work out the size of the symbol version section. */
7345 s = bfd_get_linker_section (dynobj, ".gnu.version");
7346 BFD_ASSERT (s != NULL);
7347 if ((s->flags & SEC_EXCLUDE) == 0)
7348 {
7349 s->size = dynsymcount * sizeof (Elf_External_Versym);
7350 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7351 if (s->contents == NULL)
7352 return FALSE;
7353
7354 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7355 return FALSE;
7356 }
7357
7358 /* Set the size of the .dynsym and .hash sections. We counted
7359 the number of dynamic symbols in elf_link_add_object_symbols.
7360 We will build the contents of .dynsym and .hash when we build
7361 the final symbol table, because until then we do not know the
7362 correct value to give the symbols. We built the .dynstr
7363 section as we went along in elf_link_add_object_symbols. */
7364 s = elf_hash_table (info)->dynsym;
7365 BFD_ASSERT (s != NULL);
7366 s->size = dynsymcount * bed->s->sizeof_sym;
7367
7368 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7369 if (s->contents == NULL)
7370 return FALSE;
7371
7372 /* The first entry in .dynsym is a dummy symbol. Clear all the
7373 section syms, in case we don't output them all. */
7374 ++section_sym_count;
7375 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7376
7377 elf_hash_table (info)->bucketcount = 0;
7378
7379 /* Compute the size of the hashing table. As a side effect this
7380 computes the hash values for all the names we export. */
7381 if (info->emit_hash)
7382 {
7383 unsigned long int *hashcodes;
7384 struct hash_codes_info hashinf;
7385 bfd_size_type amt;
7386 unsigned long int nsyms;
7387 size_t bucketcount;
7388 size_t hash_entry_size;
7389
7390 /* Compute the hash values for all exported symbols. At the same
7391 time store the values in an array so that we could use them for
7392 optimizations. */
7393 amt = dynsymcount * sizeof (unsigned long int);
7394 hashcodes = (unsigned long int *) bfd_malloc (amt);
7395 if (hashcodes == NULL)
7396 return FALSE;
7397 hashinf.hashcodes = hashcodes;
7398 hashinf.error = FALSE;
7399
7400 /* Put all hash values in HASHCODES. */
7401 elf_link_hash_traverse (elf_hash_table (info),
7402 elf_collect_hash_codes, &hashinf);
7403 if (hashinf.error)
7404 {
7405 free (hashcodes);
7406 return FALSE;
7407 }
7408
7409 nsyms = hashinf.hashcodes - hashcodes;
7410 bucketcount
7411 = compute_bucket_count (info, hashcodes, nsyms, 0);
7412 free (hashcodes);
7413
7414 if (bucketcount == 0 && nsyms > 0)
7415 return FALSE;
7416
7417 elf_hash_table (info)->bucketcount = bucketcount;
7418
7419 s = bfd_get_linker_section (dynobj, ".hash");
7420 BFD_ASSERT (s != NULL);
7421 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7422 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7423 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7424 if (s->contents == NULL)
7425 return FALSE;
7426
7427 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7428 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7429 s->contents + hash_entry_size);
7430 }
7431
7432 if (info->emit_gnu_hash)
7433 {
7434 size_t i, cnt;
7435 unsigned char *contents;
7436 struct collect_gnu_hash_codes cinfo;
7437 bfd_size_type amt;
7438 size_t bucketcount;
7439
7440 memset (&cinfo, 0, sizeof (cinfo));
7441
7442 /* Compute the hash values for all exported symbols. At the same
7443 time store the values in an array so that we could use them for
7444 optimizations. */
7445 amt = dynsymcount * 2 * sizeof (unsigned long int);
7446 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7447 if (cinfo.hashcodes == NULL)
7448 return FALSE;
7449
7450 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7451 cinfo.min_dynindx = -1;
7452 cinfo.output_bfd = output_bfd;
7453 cinfo.bed = bed;
7454
7455 /* Put all hash values in HASHCODES. */
7456 elf_link_hash_traverse (elf_hash_table (info),
7457 elf_collect_gnu_hash_codes, &cinfo);
7458 if (cinfo.error)
7459 {
7460 free (cinfo.hashcodes);
7461 return FALSE;
7462 }
7463
7464 bucketcount
7465 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7466
7467 if (bucketcount == 0)
7468 {
7469 free (cinfo.hashcodes);
7470 return FALSE;
7471 }
7472
7473 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7474 BFD_ASSERT (s != NULL);
7475
7476 if (cinfo.nsyms == 0)
7477 {
7478 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7479 BFD_ASSERT (cinfo.min_dynindx == -1);
7480 free (cinfo.hashcodes);
7481 s->size = 5 * 4 + bed->s->arch_size / 8;
7482 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7483 if (contents == NULL)
7484 return FALSE;
7485 s->contents = contents;
7486 /* 1 empty bucket. */
7487 bfd_put_32 (output_bfd, 1, contents);
7488 /* SYMIDX above the special symbol 0. */
7489 bfd_put_32 (output_bfd, 1, contents + 4);
7490 /* Just one word for bitmask. */
7491 bfd_put_32 (output_bfd, 1, contents + 8);
7492 /* Only hash fn bloom filter. */
7493 bfd_put_32 (output_bfd, 0, contents + 12);
7494 /* No hashes are valid - empty bitmask. */
7495 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7496 /* No hashes in the only bucket. */
7497 bfd_put_32 (output_bfd, 0,
7498 contents + 16 + bed->s->arch_size / 8);
7499 }
7500 else
7501 {
7502 unsigned long int maskwords, maskbitslog2, x;
7503 BFD_ASSERT (cinfo.min_dynindx != -1);
7504
7505 x = cinfo.nsyms;
7506 maskbitslog2 = 1;
7507 while ((x >>= 1) != 0)
7508 ++maskbitslog2;
7509 if (maskbitslog2 < 3)
7510 maskbitslog2 = 5;
7511 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7512 maskbitslog2 = maskbitslog2 + 3;
7513 else
7514 maskbitslog2 = maskbitslog2 + 2;
7515 if (bed->s->arch_size == 64)
7516 {
7517 if (maskbitslog2 == 5)
7518 maskbitslog2 = 6;
7519 cinfo.shift1 = 6;
7520 }
7521 else
7522 cinfo.shift1 = 5;
7523 cinfo.mask = (1 << cinfo.shift1) - 1;
7524 cinfo.shift2 = maskbitslog2;
7525 cinfo.maskbits = 1 << maskbitslog2;
7526 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7527 amt = bucketcount * sizeof (unsigned long int) * 2;
7528 amt += maskwords * sizeof (bfd_vma);
7529 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7530 if (cinfo.bitmask == NULL)
7531 {
7532 free (cinfo.hashcodes);
7533 return FALSE;
7534 }
7535
7536 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7537 cinfo.indx = cinfo.counts + bucketcount;
7538 cinfo.symindx = dynsymcount - cinfo.nsyms;
7539 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7540
7541 /* Determine how often each hash bucket is used. */
7542 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7543 for (i = 0; i < cinfo.nsyms; ++i)
7544 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7545
7546 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7547 if (cinfo.counts[i] != 0)
7548 {
7549 cinfo.indx[i] = cnt;
7550 cnt += cinfo.counts[i];
7551 }
7552 BFD_ASSERT (cnt == dynsymcount);
7553 cinfo.bucketcount = bucketcount;
7554 cinfo.local_indx = cinfo.min_dynindx;
7555
7556 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7557 s->size += cinfo.maskbits / 8;
7558 if (bed->record_xhash_symbol != NULL)
7559 s->size += cinfo.nsyms * 4;
7560 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7561 if (contents == NULL)
7562 {
7563 free (cinfo.bitmask);
7564 free (cinfo.hashcodes);
7565 return FALSE;
7566 }
7567
7568 s->contents = contents;
7569 bfd_put_32 (output_bfd, bucketcount, contents);
7570 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7571 bfd_put_32 (output_bfd, maskwords, contents + 8);
7572 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7573 contents += 16 + cinfo.maskbits / 8;
7574
7575 for (i = 0; i < bucketcount; ++i)
7576 {
7577 if (cinfo.counts[i] == 0)
7578 bfd_put_32 (output_bfd, 0, contents);
7579 else
7580 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7581 contents += 4;
7582 }
7583
7584 cinfo.contents = contents;
7585
7586 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7587 /* Renumber dynamic symbols, if populating .gnu.hash section.
7588 If using .MIPS.xhash, populate the translation table. */
7589 elf_link_hash_traverse (elf_hash_table (info),
7590 elf_gnu_hash_process_symidx, &cinfo);
7591
7592 contents = s->contents + 16;
7593 for (i = 0; i < maskwords; ++i)
7594 {
7595 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7596 contents);
7597 contents += bed->s->arch_size / 8;
7598 }
7599
7600 free (cinfo.bitmask);
7601 free (cinfo.hashcodes);
7602 }
7603 }
7604
7605 s = bfd_get_linker_section (dynobj, ".dynstr");
7606 BFD_ASSERT (s != NULL);
7607
7608 elf_finalize_dynstr (output_bfd, info);
7609
7610 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7611
7612 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7613 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7614 return FALSE;
7615 }
7616
7617 return TRUE;
7618 }
7619 \f
7620 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7621
7622 static void
7623 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7624 asection *sec)
7625 {
7626 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7627 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7628 }
7629
7630 /* Finish SHF_MERGE section merging. */
7631
7632 bfd_boolean
7633 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7634 {
7635 bfd *ibfd;
7636 asection *sec;
7637
7638 if (!is_elf_hash_table (info->hash))
7639 return FALSE;
7640
7641 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7642 if ((ibfd->flags & DYNAMIC) == 0
7643 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7644 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7645 == get_elf_backend_data (obfd)->s->elfclass))
7646 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7647 if ((sec->flags & SEC_MERGE) != 0
7648 && !bfd_is_abs_section (sec->output_section))
7649 {
7650 struct bfd_elf_section_data *secdata;
7651
7652 secdata = elf_section_data (sec);
7653 if (! _bfd_add_merge_section (obfd,
7654 &elf_hash_table (info)->merge_info,
7655 sec, &secdata->sec_info))
7656 return FALSE;
7657 else if (secdata->sec_info)
7658 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7659 }
7660
7661 if (elf_hash_table (info)->merge_info != NULL)
7662 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7663 merge_sections_remove_hook);
7664 return TRUE;
7665 }
7666
7667 /* Create an entry in an ELF linker hash table. */
7668
7669 struct bfd_hash_entry *
7670 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7671 struct bfd_hash_table *table,
7672 const char *string)
7673 {
7674 /* Allocate the structure if it has not already been allocated by a
7675 subclass. */
7676 if (entry == NULL)
7677 {
7678 entry = (struct bfd_hash_entry *)
7679 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7680 if (entry == NULL)
7681 return entry;
7682 }
7683
7684 /* Call the allocation method of the superclass. */
7685 entry = _bfd_link_hash_newfunc (entry, table, string);
7686 if (entry != NULL)
7687 {
7688 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7689 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7690
7691 /* Set local fields. */
7692 ret->indx = -1;
7693 ret->dynindx = -1;
7694 ret->got = htab->init_got_refcount;
7695 ret->plt = htab->init_plt_refcount;
7696 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7697 - offsetof (struct elf_link_hash_entry, size)));
7698 /* Assume that we have been called by a non-ELF symbol reader.
7699 This flag is then reset by the code which reads an ELF input
7700 file. This ensures that a symbol created by a non-ELF symbol
7701 reader will have the flag set correctly. */
7702 ret->non_elf = 1;
7703 }
7704
7705 return entry;
7706 }
7707
7708 /* Copy data from an indirect symbol to its direct symbol, hiding the
7709 old indirect symbol. Also used for copying flags to a weakdef. */
7710
7711 void
7712 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7713 struct elf_link_hash_entry *dir,
7714 struct elf_link_hash_entry *ind)
7715 {
7716 struct elf_link_hash_table *htab;
7717
7718 if (ind->dyn_relocs != NULL)
7719 {
7720 if (dir->dyn_relocs != NULL)
7721 {
7722 struct elf_dyn_relocs **pp;
7723 struct elf_dyn_relocs *p;
7724
7725 /* Add reloc counts against the indirect sym to the direct sym
7726 list. Merge any entries against the same section. */
7727 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7728 {
7729 struct elf_dyn_relocs *q;
7730
7731 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7732 if (q->sec == p->sec)
7733 {
7734 q->pc_count += p->pc_count;
7735 q->count += p->count;
7736 *pp = p->next;
7737 break;
7738 }
7739 if (q == NULL)
7740 pp = &p->next;
7741 }
7742 *pp = dir->dyn_relocs;
7743 }
7744
7745 dir->dyn_relocs = ind->dyn_relocs;
7746 ind->dyn_relocs = NULL;
7747 }
7748
7749 /* Copy down any references that we may have already seen to the
7750 symbol which just became indirect. */
7751
7752 if (dir->versioned != versioned_hidden)
7753 dir->ref_dynamic |= ind->ref_dynamic;
7754 dir->ref_regular |= ind->ref_regular;
7755 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7756 dir->non_got_ref |= ind->non_got_ref;
7757 dir->needs_plt |= ind->needs_plt;
7758 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7759
7760 if (ind->root.type != bfd_link_hash_indirect)
7761 return;
7762
7763 /* Copy over the global and procedure linkage table refcount entries.
7764 These may have been already set up by a check_relocs routine. */
7765 htab = elf_hash_table (info);
7766 if (ind->got.refcount > htab->init_got_refcount.refcount)
7767 {
7768 if (dir->got.refcount < 0)
7769 dir->got.refcount = 0;
7770 dir->got.refcount += ind->got.refcount;
7771 ind->got.refcount = htab->init_got_refcount.refcount;
7772 }
7773
7774 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7775 {
7776 if (dir->plt.refcount < 0)
7777 dir->plt.refcount = 0;
7778 dir->plt.refcount += ind->plt.refcount;
7779 ind->plt.refcount = htab->init_plt_refcount.refcount;
7780 }
7781
7782 if (ind->dynindx != -1)
7783 {
7784 if (dir->dynindx != -1)
7785 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7786 dir->dynindx = ind->dynindx;
7787 dir->dynstr_index = ind->dynstr_index;
7788 ind->dynindx = -1;
7789 ind->dynstr_index = 0;
7790 }
7791 }
7792
7793 void
7794 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7795 struct elf_link_hash_entry *h,
7796 bfd_boolean force_local)
7797 {
7798 /* STT_GNU_IFUNC symbol must go through PLT. */
7799 if (h->type != STT_GNU_IFUNC)
7800 {
7801 h->plt = elf_hash_table (info)->init_plt_offset;
7802 h->needs_plt = 0;
7803 }
7804 if (force_local)
7805 {
7806 h->forced_local = 1;
7807 if (h->dynindx != -1)
7808 {
7809 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7810 h->dynstr_index);
7811 h->dynindx = -1;
7812 h->dynstr_index = 0;
7813 }
7814 }
7815 }
7816
7817 /* Hide a symbol. */
7818
7819 void
7820 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7821 struct bfd_link_info *info,
7822 struct bfd_link_hash_entry *h)
7823 {
7824 if (is_elf_hash_table (info->hash))
7825 {
7826 const struct elf_backend_data *bed
7827 = get_elf_backend_data (output_bfd);
7828 struct elf_link_hash_entry *eh
7829 = (struct elf_link_hash_entry *) h;
7830 bed->elf_backend_hide_symbol (info, eh, TRUE);
7831 eh->def_dynamic = 0;
7832 eh->ref_dynamic = 0;
7833 eh->dynamic_def = 0;
7834 }
7835 }
7836
7837 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7838 caller. */
7839
7840 bfd_boolean
7841 _bfd_elf_link_hash_table_init
7842 (struct elf_link_hash_table *table,
7843 bfd *abfd,
7844 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7845 struct bfd_hash_table *,
7846 const char *),
7847 unsigned int entsize,
7848 enum elf_target_id target_id)
7849 {
7850 bfd_boolean ret;
7851 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7852
7853 table->init_got_refcount.refcount = can_refcount - 1;
7854 table->init_plt_refcount.refcount = can_refcount - 1;
7855 table->init_got_offset.offset = -(bfd_vma) 1;
7856 table->init_plt_offset.offset = -(bfd_vma) 1;
7857 /* The first dynamic symbol is a dummy. */
7858 table->dynsymcount = 1;
7859
7860 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7861
7862 table->root.type = bfd_link_elf_hash_table;
7863 table->hash_table_id = target_id;
7864 table->target_os = get_elf_backend_data (abfd)->target_os;
7865
7866 return ret;
7867 }
7868
7869 /* Create an ELF linker hash table. */
7870
7871 struct bfd_link_hash_table *
7872 _bfd_elf_link_hash_table_create (bfd *abfd)
7873 {
7874 struct elf_link_hash_table *ret;
7875 size_t amt = sizeof (struct elf_link_hash_table);
7876
7877 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7878 if (ret == NULL)
7879 return NULL;
7880
7881 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7882 sizeof (struct elf_link_hash_entry),
7883 GENERIC_ELF_DATA))
7884 {
7885 free (ret);
7886 return NULL;
7887 }
7888 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7889
7890 return &ret->root;
7891 }
7892
7893 /* Destroy an ELF linker hash table. */
7894
7895 void
7896 _bfd_elf_link_hash_table_free (bfd *obfd)
7897 {
7898 struct elf_link_hash_table *htab;
7899
7900 htab = (struct elf_link_hash_table *) obfd->link.hash;
7901 if (htab->dynstr != NULL)
7902 _bfd_elf_strtab_free (htab->dynstr);
7903 _bfd_merge_sections_free (htab->merge_info);
7904 _bfd_generic_link_hash_table_free (obfd);
7905 }
7906
7907 /* This is a hook for the ELF emulation code in the generic linker to
7908 tell the backend linker what file name to use for the DT_NEEDED
7909 entry for a dynamic object. */
7910
7911 void
7912 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7913 {
7914 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7915 && bfd_get_format (abfd) == bfd_object)
7916 elf_dt_name (abfd) = name;
7917 }
7918
7919 int
7920 bfd_elf_get_dyn_lib_class (bfd *abfd)
7921 {
7922 int lib_class;
7923 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7924 && bfd_get_format (abfd) == bfd_object)
7925 lib_class = elf_dyn_lib_class (abfd);
7926 else
7927 lib_class = 0;
7928 return lib_class;
7929 }
7930
7931 void
7932 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7933 {
7934 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7935 && bfd_get_format (abfd) == bfd_object)
7936 elf_dyn_lib_class (abfd) = lib_class;
7937 }
7938
7939 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7940 the linker ELF emulation code. */
7941
7942 struct bfd_link_needed_list *
7943 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7944 struct bfd_link_info *info)
7945 {
7946 if (! is_elf_hash_table (info->hash))
7947 return NULL;
7948 return elf_hash_table (info)->needed;
7949 }
7950
7951 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7952 hook for the linker ELF emulation code. */
7953
7954 struct bfd_link_needed_list *
7955 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7956 struct bfd_link_info *info)
7957 {
7958 if (! is_elf_hash_table (info->hash))
7959 return NULL;
7960 return elf_hash_table (info)->runpath;
7961 }
7962
7963 /* Get the name actually used for a dynamic object for a link. This
7964 is the SONAME entry if there is one. Otherwise, it is the string
7965 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7966
7967 const char *
7968 bfd_elf_get_dt_soname (bfd *abfd)
7969 {
7970 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7971 && bfd_get_format (abfd) == bfd_object)
7972 return elf_dt_name (abfd);
7973 return NULL;
7974 }
7975
7976 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7977 the ELF linker emulation code. */
7978
7979 bfd_boolean
7980 bfd_elf_get_bfd_needed_list (bfd *abfd,
7981 struct bfd_link_needed_list **pneeded)
7982 {
7983 asection *s;
7984 bfd_byte *dynbuf = NULL;
7985 unsigned int elfsec;
7986 unsigned long shlink;
7987 bfd_byte *extdyn, *extdynend;
7988 size_t extdynsize;
7989 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7990
7991 *pneeded = NULL;
7992
7993 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7994 || bfd_get_format (abfd) != bfd_object)
7995 return TRUE;
7996
7997 s = bfd_get_section_by_name (abfd, ".dynamic");
7998 if (s == NULL || s->size == 0)
7999 return TRUE;
8000
8001 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8002 goto error_return;
8003
8004 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8005 if (elfsec == SHN_BAD)
8006 goto error_return;
8007
8008 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8009
8010 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8011 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8012
8013 extdyn = dynbuf;
8014 extdynend = extdyn + s->size;
8015 for (; extdyn < extdynend; extdyn += extdynsize)
8016 {
8017 Elf_Internal_Dyn dyn;
8018
8019 (*swap_dyn_in) (abfd, extdyn, &dyn);
8020
8021 if (dyn.d_tag == DT_NULL)
8022 break;
8023
8024 if (dyn.d_tag == DT_NEEDED)
8025 {
8026 const char *string;
8027 struct bfd_link_needed_list *l;
8028 unsigned int tagv = dyn.d_un.d_val;
8029 size_t amt;
8030
8031 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8032 if (string == NULL)
8033 goto error_return;
8034
8035 amt = sizeof *l;
8036 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8037 if (l == NULL)
8038 goto error_return;
8039
8040 l->by = abfd;
8041 l->name = string;
8042 l->next = *pneeded;
8043 *pneeded = l;
8044 }
8045 }
8046
8047 free (dynbuf);
8048
8049 return TRUE;
8050
8051 error_return:
8052 free (dynbuf);
8053 return FALSE;
8054 }
8055
8056 struct elf_symbuf_symbol
8057 {
8058 unsigned long st_name; /* Symbol name, index in string tbl */
8059 unsigned char st_info; /* Type and binding attributes */
8060 unsigned char st_other; /* Visibilty, and target specific */
8061 };
8062
8063 struct elf_symbuf_head
8064 {
8065 struct elf_symbuf_symbol *ssym;
8066 size_t count;
8067 unsigned int st_shndx;
8068 };
8069
8070 struct elf_symbol
8071 {
8072 union
8073 {
8074 Elf_Internal_Sym *isym;
8075 struct elf_symbuf_symbol *ssym;
8076 void *p;
8077 } u;
8078 const char *name;
8079 };
8080
8081 /* Sort references to symbols by ascending section number. */
8082
8083 static int
8084 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8085 {
8086 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8087 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8088
8089 if (s1->st_shndx != s2->st_shndx)
8090 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8091 /* Final sort by the address of the sym in the symbuf ensures
8092 a stable sort. */
8093 if (s1 != s2)
8094 return s1 > s2 ? 1 : -1;
8095 return 0;
8096 }
8097
8098 static int
8099 elf_sym_name_compare (const void *arg1, const void *arg2)
8100 {
8101 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8102 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8103 int ret = strcmp (s1->name, s2->name);
8104 if (ret != 0)
8105 return ret;
8106 if (s1->u.p != s2->u.p)
8107 return s1->u.p > s2->u.p ? 1 : -1;
8108 return 0;
8109 }
8110
8111 static struct elf_symbuf_head *
8112 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8113 {
8114 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8115 struct elf_symbuf_symbol *ssym;
8116 struct elf_symbuf_head *ssymbuf, *ssymhead;
8117 size_t i, shndx_count, total_size, amt;
8118
8119 amt = symcount * sizeof (*indbuf);
8120 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8121 if (indbuf == NULL)
8122 return NULL;
8123
8124 for (ind = indbuf, i = 0; i < symcount; i++)
8125 if (isymbuf[i].st_shndx != SHN_UNDEF)
8126 *ind++ = &isymbuf[i];
8127 indbufend = ind;
8128
8129 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8130 elf_sort_elf_symbol);
8131
8132 shndx_count = 0;
8133 if (indbufend > indbuf)
8134 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8135 if (ind[0]->st_shndx != ind[1]->st_shndx)
8136 shndx_count++;
8137
8138 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8139 + (indbufend - indbuf) * sizeof (*ssym));
8140 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8141 if (ssymbuf == NULL)
8142 {
8143 free (indbuf);
8144 return NULL;
8145 }
8146
8147 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8148 ssymbuf->ssym = NULL;
8149 ssymbuf->count = shndx_count;
8150 ssymbuf->st_shndx = 0;
8151 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8152 {
8153 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8154 {
8155 ssymhead++;
8156 ssymhead->ssym = ssym;
8157 ssymhead->count = 0;
8158 ssymhead->st_shndx = (*ind)->st_shndx;
8159 }
8160 ssym->st_name = (*ind)->st_name;
8161 ssym->st_info = (*ind)->st_info;
8162 ssym->st_other = (*ind)->st_other;
8163 ssymhead->count++;
8164 }
8165 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8166 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8167 == total_size));
8168
8169 free (indbuf);
8170 return ssymbuf;
8171 }
8172
8173 /* Check if 2 sections define the same set of local and global
8174 symbols. */
8175
8176 static bfd_boolean
8177 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8178 struct bfd_link_info *info)
8179 {
8180 bfd *bfd1, *bfd2;
8181 const struct elf_backend_data *bed1, *bed2;
8182 Elf_Internal_Shdr *hdr1, *hdr2;
8183 size_t symcount1, symcount2;
8184 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8185 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8186 Elf_Internal_Sym *isym, *isymend;
8187 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8188 size_t count1, count2, i;
8189 unsigned int shndx1, shndx2;
8190 bfd_boolean result;
8191
8192 bfd1 = sec1->owner;
8193 bfd2 = sec2->owner;
8194
8195 /* Both sections have to be in ELF. */
8196 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8197 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8198 return FALSE;
8199
8200 if (elf_section_type (sec1) != elf_section_type (sec2))
8201 return FALSE;
8202
8203 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8204 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8205 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8206 return FALSE;
8207
8208 bed1 = get_elf_backend_data (bfd1);
8209 bed2 = get_elf_backend_data (bfd2);
8210 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8211 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8212 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8213 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8214
8215 if (symcount1 == 0 || symcount2 == 0)
8216 return FALSE;
8217
8218 result = FALSE;
8219 isymbuf1 = NULL;
8220 isymbuf2 = NULL;
8221 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8222 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8223
8224 if (ssymbuf1 == NULL)
8225 {
8226 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8227 NULL, NULL, NULL);
8228 if (isymbuf1 == NULL)
8229 goto done;
8230
8231 if (info != NULL && !info->reduce_memory_overheads)
8232 {
8233 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8234 elf_tdata (bfd1)->symbuf = ssymbuf1;
8235 }
8236 }
8237
8238 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8239 {
8240 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8241 NULL, NULL, NULL);
8242 if (isymbuf2 == NULL)
8243 goto done;
8244
8245 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8246 {
8247 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8248 elf_tdata (bfd2)->symbuf = ssymbuf2;
8249 }
8250 }
8251
8252 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8253 {
8254 /* Optimized faster version. */
8255 size_t lo, hi, mid;
8256 struct elf_symbol *symp;
8257 struct elf_symbuf_symbol *ssym, *ssymend;
8258
8259 lo = 0;
8260 hi = ssymbuf1->count;
8261 ssymbuf1++;
8262 count1 = 0;
8263 while (lo < hi)
8264 {
8265 mid = (lo + hi) / 2;
8266 if (shndx1 < ssymbuf1[mid].st_shndx)
8267 hi = mid;
8268 else if (shndx1 > ssymbuf1[mid].st_shndx)
8269 lo = mid + 1;
8270 else
8271 {
8272 count1 = ssymbuf1[mid].count;
8273 ssymbuf1 += mid;
8274 break;
8275 }
8276 }
8277
8278 lo = 0;
8279 hi = ssymbuf2->count;
8280 ssymbuf2++;
8281 count2 = 0;
8282 while (lo < hi)
8283 {
8284 mid = (lo + hi) / 2;
8285 if (shndx2 < ssymbuf2[mid].st_shndx)
8286 hi = mid;
8287 else if (shndx2 > ssymbuf2[mid].st_shndx)
8288 lo = mid + 1;
8289 else
8290 {
8291 count2 = ssymbuf2[mid].count;
8292 ssymbuf2 += mid;
8293 break;
8294 }
8295 }
8296
8297 if (count1 == 0 || count2 == 0 || count1 != count2)
8298 goto done;
8299
8300 symtable1
8301 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8302 symtable2
8303 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8304 if (symtable1 == NULL || symtable2 == NULL)
8305 goto done;
8306
8307 symp = symtable1;
8308 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8309 ssym < ssymend; ssym++, symp++)
8310 {
8311 symp->u.ssym = ssym;
8312 symp->name = bfd_elf_string_from_elf_section (bfd1,
8313 hdr1->sh_link,
8314 ssym->st_name);
8315 }
8316
8317 symp = symtable2;
8318 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8319 ssym < ssymend; ssym++, symp++)
8320 {
8321 symp->u.ssym = ssym;
8322 symp->name = bfd_elf_string_from_elf_section (bfd2,
8323 hdr2->sh_link,
8324 ssym->st_name);
8325 }
8326
8327 /* Sort symbol by name. */
8328 qsort (symtable1, count1, sizeof (struct elf_symbol),
8329 elf_sym_name_compare);
8330 qsort (symtable2, count1, sizeof (struct elf_symbol),
8331 elf_sym_name_compare);
8332
8333 for (i = 0; i < count1; i++)
8334 /* Two symbols must have the same binding, type and name. */
8335 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8336 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8337 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8338 goto done;
8339
8340 result = TRUE;
8341 goto done;
8342 }
8343
8344 symtable1 = (struct elf_symbol *)
8345 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8346 symtable2 = (struct elf_symbol *)
8347 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8348 if (symtable1 == NULL || symtable2 == NULL)
8349 goto done;
8350
8351 /* Count definitions in the section. */
8352 count1 = 0;
8353 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8354 if (isym->st_shndx == shndx1)
8355 symtable1[count1++].u.isym = isym;
8356
8357 count2 = 0;
8358 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8359 if (isym->st_shndx == shndx2)
8360 symtable2[count2++].u.isym = isym;
8361
8362 if (count1 == 0 || count2 == 0 || count1 != count2)
8363 goto done;
8364
8365 for (i = 0; i < count1; i++)
8366 symtable1[i].name
8367 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8368 symtable1[i].u.isym->st_name);
8369
8370 for (i = 0; i < count2; i++)
8371 symtable2[i].name
8372 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8373 symtable2[i].u.isym->st_name);
8374
8375 /* Sort symbol by name. */
8376 qsort (symtable1, count1, sizeof (struct elf_symbol),
8377 elf_sym_name_compare);
8378 qsort (symtable2, count1, sizeof (struct elf_symbol),
8379 elf_sym_name_compare);
8380
8381 for (i = 0; i < count1; i++)
8382 /* Two symbols must have the same binding, type and name. */
8383 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8384 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8385 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8386 goto done;
8387
8388 result = TRUE;
8389
8390 done:
8391 free (symtable1);
8392 free (symtable2);
8393 free (isymbuf1);
8394 free (isymbuf2);
8395
8396 return result;
8397 }
8398
8399 /* Return TRUE if 2 section types are compatible. */
8400
8401 bfd_boolean
8402 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8403 bfd *bbfd, const asection *bsec)
8404 {
8405 if (asec == NULL
8406 || bsec == NULL
8407 || abfd->xvec->flavour != bfd_target_elf_flavour
8408 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8409 return TRUE;
8410
8411 return elf_section_type (asec) == elf_section_type (bsec);
8412 }
8413 \f
8414 /* Final phase of ELF linker. */
8415
8416 /* A structure we use to avoid passing large numbers of arguments. */
8417
8418 struct elf_final_link_info
8419 {
8420 /* General link information. */
8421 struct bfd_link_info *info;
8422 /* Output BFD. */
8423 bfd *output_bfd;
8424 /* Symbol string table. */
8425 struct elf_strtab_hash *symstrtab;
8426 /* .hash section. */
8427 asection *hash_sec;
8428 /* symbol version section (.gnu.version). */
8429 asection *symver_sec;
8430 /* Buffer large enough to hold contents of any section. */
8431 bfd_byte *contents;
8432 /* Buffer large enough to hold external relocs of any section. */
8433 void *external_relocs;
8434 /* Buffer large enough to hold internal relocs of any section. */
8435 Elf_Internal_Rela *internal_relocs;
8436 /* Buffer large enough to hold external local symbols of any input
8437 BFD. */
8438 bfd_byte *external_syms;
8439 /* And a buffer for symbol section indices. */
8440 Elf_External_Sym_Shndx *locsym_shndx;
8441 /* Buffer large enough to hold internal local symbols of any input
8442 BFD. */
8443 Elf_Internal_Sym *internal_syms;
8444 /* Array large enough to hold a symbol index for each local symbol
8445 of any input BFD. */
8446 long *indices;
8447 /* Array large enough to hold a section pointer for each local
8448 symbol of any input BFD. */
8449 asection **sections;
8450 /* Buffer for SHT_SYMTAB_SHNDX section. */
8451 Elf_External_Sym_Shndx *symshndxbuf;
8452 /* Number of STT_FILE syms seen. */
8453 size_t filesym_count;
8454 /* Local symbol hash table. */
8455 struct bfd_hash_table local_hash_table;
8456 };
8457
8458 struct local_hash_entry
8459 {
8460 /* Base hash table entry structure. */
8461 struct bfd_hash_entry root;
8462 /* Size of the local symbol name. */
8463 size_t size;
8464 /* Number of the duplicated local symbol names. */
8465 long count;
8466 };
8467
8468 /* Create an entry in the local symbol hash table. */
8469
8470 static struct bfd_hash_entry *
8471 local_hash_newfunc (struct bfd_hash_entry *entry,
8472 struct bfd_hash_table *table,
8473 const char *string)
8474 {
8475
8476 /* Allocate the structure if it has not already been allocated by a
8477 subclass. */
8478 if (entry == NULL)
8479 {
8480 entry = bfd_hash_allocate (table,
8481 sizeof (struct local_hash_entry));
8482 if (entry == NULL)
8483 return entry;
8484 }
8485
8486 /* Call the allocation method of the superclass. */
8487 entry = bfd_hash_newfunc (entry, table, string);
8488 if (entry != NULL)
8489 {
8490 ((struct local_hash_entry *) entry)->count = 0;
8491 ((struct local_hash_entry *) entry)->size = 0;
8492 }
8493
8494 return entry;
8495 }
8496
8497 /* This struct is used to pass information to elf_link_output_extsym. */
8498
8499 struct elf_outext_info
8500 {
8501 bfd_boolean failed;
8502 bfd_boolean localsyms;
8503 bfd_boolean file_sym_done;
8504 struct elf_final_link_info *flinfo;
8505 };
8506
8507
8508 /* Support for evaluating a complex relocation.
8509
8510 Complex relocations are generalized, self-describing relocations. The
8511 implementation of them consists of two parts: complex symbols, and the
8512 relocations themselves.
8513
8514 The relocations use a reserved elf-wide relocation type code (R_RELC
8515 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8516 information (start bit, end bit, word width, etc) into the addend. This
8517 information is extracted from CGEN-generated operand tables within gas.
8518
8519 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8520 internal) representing prefix-notation expressions, including but not
8521 limited to those sorts of expressions normally encoded as addends in the
8522 addend field. The symbol mangling format is:
8523
8524 <node> := <literal>
8525 | <unary-operator> ':' <node>
8526 | <binary-operator> ':' <node> ':' <node>
8527 ;
8528
8529 <literal> := 's' <digits=N> ':' <N character symbol name>
8530 | 'S' <digits=N> ':' <N character section name>
8531 | '#' <hexdigits>
8532 ;
8533
8534 <binary-operator> := as in C
8535 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8536
8537 static void
8538 set_symbol_value (bfd *bfd_with_globals,
8539 Elf_Internal_Sym *isymbuf,
8540 size_t locsymcount,
8541 size_t symidx,
8542 bfd_vma val)
8543 {
8544 struct elf_link_hash_entry **sym_hashes;
8545 struct elf_link_hash_entry *h;
8546 size_t extsymoff = locsymcount;
8547
8548 if (symidx < locsymcount)
8549 {
8550 Elf_Internal_Sym *sym;
8551
8552 sym = isymbuf + symidx;
8553 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8554 {
8555 /* It is a local symbol: move it to the
8556 "absolute" section and give it a value. */
8557 sym->st_shndx = SHN_ABS;
8558 sym->st_value = val;
8559 return;
8560 }
8561 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8562 extsymoff = 0;
8563 }
8564
8565 /* It is a global symbol: set its link type
8566 to "defined" and give it a value. */
8567
8568 sym_hashes = elf_sym_hashes (bfd_with_globals);
8569 h = sym_hashes [symidx - extsymoff];
8570 while (h->root.type == bfd_link_hash_indirect
8571 || h->root.type == bfd_link_hash_warning)
8572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8573 h->root.type = bfd_link_hash_defined;
8574 h->root.u.def.value = val;
8575 h->root.u.def.section = bfd_abs_section_ptr;
8576 }
8577
8578 static bfd_boolean
8579 resolve_symbol (const char *name,
8580 bfd *input_bfd,
8581 struct elf_final_link_info *flinfo,
8582 bfd_vma *result,
8583 Elf_Internal_Sym *isymbuf,
8584 size_t locsymcount)
8585 {
8586 Elf_Internal_Sym *sym;
8587 struct bfd_link_hash_entry *global_entry;
8588 const char *candidate = NULL;
8589 Elf_Internal_Shdr *symtab_hdr;
8590 size_t i;
8591
8592 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8593
8594 for (i = 0; i < locsymcount; ++ i)
8595 {
8596 sym = isymbuf + i;
8597
8598 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8599 continue;
8600
8601 candidate = bfd_elf_string_from_elf_section (input_bfd,
8602 symtab_hdr->sh_link,
8603 sym->st_name);
8604 #ifdef DEBUG
8605 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8606 name, candidate, (unsigned long) sym->st_value);
8607 #endif
8608 if (candidate && strcmp (candidate, name) == 0)
8609 {
8610 asection *sec = flinfo->sections [i];
8611
8612 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8613 *result += sec->output_offset + sec->output_section->vma;
8614 #ifdef DEBUG
8615 printf ("Found symbol with value %8.8lx\n",
8616 (unsigned long) *result);
8617 #endif
8618 return TRUE;
8619 }
8620 }
8621
8622 /* Hmm, haven't found it yet. perhaps it is a global. */
8623 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8624 FALSE, FALSE, TRUE);
8625 if (!global_entry)
8626 return FALSE;
8627
8628 if (global_entry->type == bfd_link_hash_defined
8629 || global_entry->type == bfd_link_hash_defweak)
8630 {
8631 *result = (global_entry->u.def.value
8632 + global_entry->u.def.section->output_section->vma
8633 + global_entry->u.def.section->output_offset);
8634 #ifdef DEBUG
8635 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8636 global_entry->root.string, (unsigned long) *result);
8637 #endif
8638 return TRUE;
8639 }
8640
8641 return FALSE;
8642 }
8643
8644 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8645 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8646 names like "foo.end" which is the end address of section "foo". */
8647
8648 static bfd_boolean
8649 resolve_section (const char *name,
8650 asection *sections,
8651 bfd_vma *result,
8652 bfd * abfd)
8653 {
8654 asection *curr;
8655 unsigned int len;
8656
8657 for (curr = sections; curr; curr = curr->next)
8658 if (strcmp (curr->name, name) == 0)
8659 {
8660 *result = curr->vma;
8661 return TRUE;
8662 }
8663
8664 /* Hmm. still haven't found it. try pseudo-section names. */
8665 /* FIXME: This could be coded more efficiently... */
8666 for (curr = sections; curr; curr = curr->next)
8667 {
8668 len = strlen (curr->name);
8669 if (len > strlen (name))
8670 continue;
8671
8672 if (strncmp (curr->name, name, len) == 0)
8673 {
8674 if (strncmp (".end", name + len, 4) == 0)
8675 {
8676 *result = (curr->vma
8677 + curr->size / bfd_octets_per_byte (abfd, curr));
8678 return TRUE;
8679 }
8680
8681 /* Insert more pseudo-section names here, if you like. */
8682 }
8683 }
8684
8685 return FALSE;
8686 }
8687
8688 static void
8689 undefined_reference (const char *reftype, const char *name)
8690 {
8691 /* xgettext:c-format */
8692 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8693 reftype, name);
8694 bfd_set_error (bfd_error_bad_value);
8695 }
8696
8697 static bfd_boolean
8698 eval_symbol (bfd_vma *result,
8699 const char **symp,
8700 bfd *input_bfd,
8701 struct elf_final_link_info *flinfo,
8702 bfd_vma dot,
8703 Elf_Internal_Sym *isymbuf,
8704 size_t locsymcount,
8705 int signed_p)
8706 {
8707 size_t len;
8708 size_t symlen;
8709 bfd_vma a;
8710 bfd_vma b;
8711 char symbuf[4096];
8712 const char *sym = *symp;
8713 const char *symend;
8714 bfd_boolean symbol_is_section = FALSE;
8715
8716 len = strlen (sym);
8717 symend = sym + len;
8718
8719 if (len < 1 || len > sizeof (symbuf))
8720 {
8721 bfd_set_error (bfd_error_invalid_operation);
8722 return FALSE;
8723 }
8724
8725 switch (* sym)
8726 {
8727 case '.':
8728 *result = dot;
8729 *symp = sym + 1;
8730 return TRUE;
8731
8732 case '#':
8733 ++sym;
8734 *result = strtoul (sym, (char **) symp, 16);
8735 return TRUE;
8736
8737 case 'S':
8738 symbol_is_section = TRUE;
8739 /* Fall through. */
8740 case 's':
8741 ++sym;
8742 symlen = strtol (sym, (char **) symp, 10);
8743 sym = *symp + 1; /* Skip the trailing ':'. */
8744
8745 if (symend < sym || symlen + 1 > sizeof (symbuf))
8746 {
8747 bfd_set_error (bfd_error_invalid_operation);
8748 return FALSE;
8749 }
8750
8751 memcpy (symbuf, sym, symlen);
8752 symbuf[symlen] = '\0';
8753 *symp = sym + symlen;
8754
8755 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8756 the symbol as a section, or vice-versa. so we're pretty liberal in our
8757 interpretation here; section means "try section first", not "must be a
8758 section", and likewise with symbol. */
8759
8760 if (symbol_is_section)
8761 {
8762 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8763 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8764 isymbuf, locsymcount))
8765 {
8766 undefined_reference ("section", symbuf);
8767 return FALSE;
8768 }
8769 }
8770 else
8771 {
8772 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8773 isymbuf, locsymcount)
8774 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8775 result, input_bfd))
8776 {
8777 undefined_reference ("symbol", symbuf);
8778 return FALSE;
8779 }
8780 }
8781
8782 return TRUE;
8783
8784 /* All that remains are operators. */
8785
8786 #define UNARY_OP(op) \
8787 if (strncmp (sym, #op, strlen (#op)) == 0) \
8788 { \
8789 sym += strlen (#op); \
8790 if (*sym == ':') \
8791 ++sym; \
8792 *symp = sym; \
8793 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8794 isymbuf, locsymcount, signed_p)) \
8795 return FALSE; \
8796 if (signed_p) \
8797 *result = op ((bfd_signed_vma) a); \
8798 else \
8799 *result = op a; \
8800 return TRUE; \
8801 }
8802
8803 #define BINARY_OP_HEAD(op) \
8804 if (strncmp (sym, #op, strlen (#op)) == 0) \
8805 { \
8806 sym += strlen (#op); \
8807 if (*sym == ':') \
8808 ++sym; \
8809 *symp = sym; \
8810 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8811 isymbuf, locsymcount, signed_p)) \
8812 return FALSE; \
8813 ++*symp; \
8814 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8815 isymbuf, locsymcount, signed_p)) \
8816 return FALSE;
8817 #define BINARY_OP_TAIL(op) \
8818 if (signed_p) \
8819 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8820 else \
8821 *result = a op b; \
8822 return TRUE; \
8823 }
8824 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8825
8826 default:
8827 UNARY_OP (0-);
8828 BINARY_OP_HEAD (<<);
8829 if (b >= sizeof (a) * CHAR_BIT)
8830 {
8831 *result = 0;
8832 return TRUE;
8833 }
8834 signed_p = 0;
8835 BINARY_OP_TAIL (<<);
8836 BINARY_OP_HEAD (>>);
8837 if (b >= sizeof (a) * CHAR_BIT)
8838 {
8839 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8840 return TRUE;
8841 }
8842 BINARY_OP_TAIL (>>);
8843 BINARY_OP (==);
8844 BINARY_OP (!=);
8845 BINARY_OP (<=);
8846 BINARY_OP (>=);
8847 BINARY_OP (&&);
8848 BINARY_OP (||);
8849 UNARY_OP (~);
8850 UNARY_OP (!);
8851 BINARY_OP (*);
8852 BINARY_OP_HEAD (/);
8853 if (b == 0)
8854 {
8855 _bfd_error_handler (_("division by zero"));
8856 bfd_set_error (bfd_error_bad_value);
8857 return FALSE;
8858 }
8859 BINARY_OP_TAIL (/);
8860 BINARY_OP_HEAD (%);
8861 if (b == 0)
8862 {
8863 _bfd_error_handler (_("division by zero"));
8864 bfd_set_error (bfd_error_bad_value);
8865 return FALSE;
8866 }
8867 BINARY_OP_TAIL (%);
8868 BINARY_OP (^);
8869 BINARY_OP (|);
8870 BINARY_OP (&);
8871 BINARY_OP (+);
8872 BINARY_OP (-);
8873 BINARY_OP (<);
8874 BINARY_OP (>);
8875 #undef UNARY_OP
8876 #undef BINARY_OP
8877 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8878 bfd_set_error (bfd_error_invalid_operation);
8879 return FALSE;
8880 }
8881 }
8882
8883 static void
8884 put_value (bfd_vma size,
8885 unsigned long chunksz,
8886 bfd *input_bfd,
8887 bfd_vma x,
8888 bfd_byte *location)
8889 {
8890 location += (size - chunksz);
8891
8892 for (; size; size -= chunksz, location -= chunksz)
8893 {
8894 switch (chunksz)
8895 {
8896 case 1:
8897 bfd_put_8 (input_bfd, x, location);
8898 x >>= 8;
8899 break;
8900 case 2:
8901 bfd_put_16 (input_bfd, x, location);
8902 x >>= 16;
8903 break;
8904 case 4:
8905 bfd_put_32 (input_bfd, x, location);
8906 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8907 x >>= 16;
8908 x >>= 16;
8909 break;
8910 #ifdef BFD64
8911 case 8:
8912 bfd_put_64 (input_bfd, x, location);
8913 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8914 x >>= 32;
8915 x >>= 32;
8916 break;
8917 #endif
8918 default:
8919 abort ();
8920 break;
8921 }
8922 }
8923 }
8924
8925 static bfd_vma
8926 get_value (bfd_vma size,
8927 unsigned long chunksz,
8928 bfd *input_bfd,
8929 bfd_byte *location)
8930 {
8931 int shift;
8932 bfd_vma x = 0;
8933
8934 /* Sanity checks. */
8935 BFD_ASSERT (chunksz <= sizeof (x)
8936 && size >= chunksz
8937 && chunksz != 0
8938 && (size % chunksz) == 0
8939 && input_bfd != NULL
8940 && location != NULL);
8941
8942 if (chunksz == sizeof (x))
8943 {
8944 BFD_ASSERT (size == chunksz);
8945
8946 /* Make sure that we do not perform an undefined shift operation.
8947 We know that size == chunksz so there will only be one iteration
8948 of the loop below. */
8949 shift = 0;
8950 }
8951 else
8952 shift = 8 * chunksz;
8953
8954 for (; size; size -= chunksz, location += chunksz)
8955 {
8956 switch (chunksz)
8957 {
8958 case 1:
8959 x = (x << shift) | bfd_get_8 (input_bfd, location);
8960 break;
8961 case 2:
8962 x = (x << shift) | bfd_get_16 (input_bfd, location);
8963 break;
8964 case 4:
8965 x = (x << shift) | bfd_get_32 (input_bfd, location);
8966 break;
8967 #ifdef BFD64
8968 case 8:
8969 x = (x << shift) | bfd_get_64 (input_bfd, location);
8970 break;
8971 #endif
8972 default:
8973 abort ();
8974 }
8975 }
8976 return x;
8977 }
8978
8979 static void
8980 decode_complex_addend (unsigned long *start, /* in bits */
8981 unsigned long *oplen, /* in bits */
8982 unsigned long *len, /* in bits */
8983 unsigned long *wordsz, /* in bytes */
8984 unsigned long *chunksz, /* in bytes */
8985 unsigned long *lsb0_p,
8986 unsigned long *signed_p,
8987 unsigned long *trunc_p,
8988 unsigned long encoded)
8989 {
8990 * start = encoded & 0x3F;
8991 * len = (encoded >> 6) & 0x3F;
8992 * oplen = (encoded >> 12) & 0x3F;
8993 * wordsz = (encoded >> 18) & 0xF;
8994 * chunksz = (encoded >> 22) & 0xF;
8995 * lsb0_p = (encoded >> 27) & 1;
8996 * signed_p = (encoded >> 28) & 1;
8997 * trunc_p = (encoded >> 29) & 1;
8998 }
8999
9000 bfd_reloc_status_type
9001 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9002 asection *input_section,
9003 bfd_byte *contents,
9004 Elf_Internal_Rela *rel,
9005 bfd_vma relocation)
9006 {
9007 bfd_vma shift, x, mask;
9008 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9009 bfd_reloc_status_type r;
9010 bfd_size_type octets;
9011
9012 /* Perform this reloc, since it is complex.
9013 (this is not to say that it necessarily refers to a complex
9014 symbol; merely that it is a self-describing CGEN based reloc.
9015 i.e. the addend has the complete reloc information (bit start, end,
9016 word size, etc) encoded within it.). */
9017
9018 decode_complex_addend (&start, &oplen, &len, &wordsz,
9019 &chunksz, &lsb0_p, &signed_p,
9020 &trunc_p, rel->r_addend);
9021
9022 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9023
9024 if (lsb0_p)
9025 shift = (start + 1) - len;
9026 else
9027 shift = (8 * wordsz) - (start + len);
9028
9029 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9030 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9031
9032 #ifdef DEBUG
9033 printf ("Doing complex reloc: "
9034 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9035 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9036 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9037 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9038 oplen, (unsigned long) x, (unsigned long) mask,
9039 (unsigned long) relocation);
9040 #endif
9041
9042 r = bfd_reloc_ok;
9043 if (! trunc_p)
9044 /* Now do an overflow check. */
9045 r = bfd_check_overflow ((signed_p
9046 ? complain_overflow_signed
9047 : complain_overflow_unsigned),
9048 len, 0, (8 * wordsz),
9049 relocation);
9050
9051 /* Do the deed. */
9052 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9053
9054 #ifdef DEBUG
9055 printf (" relocation: %8.8lx\n"
9056 " shifted mask: %8.8lx\n"
9057 " shifted/masked reloc: %8.8lx\n"
9058 " result: %8.8lx\n",
9059 (unsigned long) relocation, (unsigned long) (mask << shift),
9060 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9061 #endif
9062 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9063 return r;
9064 }
9065
9066 /* Functions to read r_offset from external (target order) reloc
9067 entry. Faster than bfd_getl32 et al, because we let the compiler
9068 know the value is aligned. */
9069
9070 static bfd_vma
9071 ext32l_r_offset (const void *p)
9072 {
9073 union aligned32
9074 {
9075 uint32_t v;
9076 unsigned char c[4];
9077 };
9078 const union aligned32 *a
9079 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9080
9081 uint32_t aval = ( (uint32_t) a->c[0]
9082 | (uint32_t) a->c[1] << 8
9083 | (uint32_t) a->c[2] << 16
9084 | (uint32_t) a->c[3] << 24);
9085 return aval;
9086 }
9087
9088 static bfd_vma
9089 ext32b_r_offset (const void *p)
9090 {
9091 union aligned32
9092 {
9093 uint32_t v;
9094 unsigned char c[4];
9095 };
9096 const union aligned32 *a
9097 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9098
9099 uint32_t aval = ( (uint32_t) a->c[0] << 24
9100 | (uint32_t) a->c[1] << 16
9101 | (uint32_t) a->c[2] << 8
9102 | (uint32_t) a->c[3]);
9103 return aval;
9104 }
9105
9106 #ifdef BFD_HOST_64_BIT
9107 static bfd_vma
9108 ext64l_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]
9119 | (uint64_t) a->c[1] << 8
9120 | (uint64_t) a->c[2] << 16
9121 | (uint64_t) a->c[3] << 24
9122 | (uint64_t) a->c[4] << 32
9123 | (uint64_t) a->c[5] << 40
9124 | (uint64_t) a->c[6] << 48
9125 | (uint64_t) a->c[7] << 56);
9126 return aval;
9127 }
9128
9129 static bfd_vma
9130 ext64b_r_offset (const void *p)
9131 {
9132 union aligned64
9133 {
9134 uint64_t v;
9135 unsigned char c[8];
9136 };
9137 const union aligned64 *a
9138 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9139
9140 uint64_t aval = ( (uint64_t) a->c[0] << 56
9141 | (uint64_t) a->c[1] << 48
9142 | (uint64_t) a->c[2] << 40
9143 | (uint64_t) a->c[3] << 32
9144 | (uint64_t) a->c[4] << 24
9145 | (uint64_t) a->c[5] << 16
9146 | (uint64_t) a->c[6] << 8
9147 | (uint64_t) a->c[7]);
9148 return aval;
9149 }
9150 #endif
9151
9152 /* When performing a relocatable link, the input relocations are
9153 preserved. But, if they reference global symbols, the indices
9154 referenced must be updated. Update all the relocations found in
9155 RELDATA. */
9156
9157 static bfd_boolean
9158 elf_link_adjust_relocs (bfd *abfd,
9159 asection *sec,
9160 struct bfd_elf_section_reloc_data *reldata,
9161 bfd_boolean sort,
9162 struct bfd_link_info *info)
9163 {
9164 unsigned int i;
9165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9166 bfd_byte *erela;
9167 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9168 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9169 bfd_vma r_type_mask;
9170 int r_sym_shift;
9171 unsigned int count = reldata->count;
9172 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9173
9174 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9175 {
9176 swap_in = bed->s->swap_reloc_in;
9177 swap_out = bed->s->swap_reloc_out;
9178 }
9179 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9180 {
9181 swap_in = bed->s->swap_reloca_in;
9182 swap_out = bed->s->swap_reloca_out;
9183 }
9184 else
9185 abort ();
9186
9187 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9188 abort ();
9189
9190 if (bed->s->arch_size == 32)
9191 {
9192 r_type_mask = 0xff;
9193 r_sym_shift = 8;
9194 }
9195 else
9196 {
9197 r_type_mask = 0xffffffff;
9198 r_sym_shift = 32;
9199 }
9200
9201 erela = reldata->hdr->contents;
9202 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9203 {
9204 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9205 unsigned int j;
9206
9207 if (*rel_hash == NULL)
9208 continue;
9209
9210 if ((*rel_hash)->indx == -2
9211 && info->gc_sections
9212 && ! info->gc_keep_exported)
9213 {
9214 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9215 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9216 abfd, sec,
9217 (*rel_hash)->root.root.string);
9218 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9219 abfd, sec);
9220 bfd_set_error (bfd_error_invalid_operation);
9221 return FALSE;
9222 }
9223 BFD_ASSERT ((*rel_hash)->indx >= 0);
9224
9225 (*swap_in) (abfd, erela, irela);
9226 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9227 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9228 | (irela[j].r_info & r_type_mask));
9229 (*swap_out) (abfd, irela, erela);
9230 }
9231
9232 if (bed->elf_backend_update_relocs)
9233 (*bed->elf_backend_update_relocs) (sec, reldata);
9234
9235 if (sort && count != 0)
9236 {
9237 bfd_vma (*ext_r_off) (const void *);
9238 bfd_vma r_off;
9239 size_t elt_size;
9240 bfd_byte *base, *end, *p, *loc;
9241 bfd_byte *buf = NULL;
9242
9243 if (bed->s->arch_size == 32)
9244 {
9245 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9246 ext_r_off = ext32l_r_offset;
9247 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9248 ext_r_off = ext32b_r_offset;
9249 else
9250 abort ();
9251 }
9252 else
9253 {
9254 #ifdef BFD_HOST_64_BIT
9255 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9256 ext_r_off = ext64l_r_offset;
9257 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9258 ext_r_off = ext64b_r_offset;
9259 else
9260 #endif
9261 abort ();
9262 }
9263
9264 /* Must use a stable sort here. A modified insertion sort,
9265 since the relocs are mostly sorted already. */
9266 elt_size = reldata->hdr->sh_entsize;
9267 base = reldata->hdr->contents;
9268 end = base + count * elt_size;
9269 if (elt_size > sizeof (Elf64_External_Rela))
9270 abort ();
9271
9272 /* Ensure the first element is lowest. This acts as a sentinel,
9273 speeding the main loop below. */
9274 r_off = (*ext_r_off) (base);
9275 for (p = loc = base; (p += elt_size) < end; )
9276 {
9277 bfd_vma r_off2 = (*ext_r_off) (p);
9278 if (r_off > r_off2)
9279 {
9280 r_off = r_off2;
9281 loc = p;
9282 }
9283 }
9284 if (loc != base)
9285 {
9286 /* Don't just swap *base and *loc as that changes the order
9287 of the original base[0] and base[1] if they happen to
9288 have the same r_offset. */
9289 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9290 memcpy (onebuf, loc, elt_size);
9291 memmove (base + elt_size, base, loc - base);
9292 memcpy (base, onebuf, elt_size);
9293 }
9294
9295 for (p = base + elt_size; (p += elt_size) < end; )
9296 {
9297 /* base to p is sorted, *p is next to insert. */
9298 r_off = (*ext_r_off) (p);
9299 /* Search the sorted region for location to insert. */
9300 loc = p - elt_size;
9301 while (r_off < (*ext_r_off) (loc))
9302 loc -= elt_size;
9303 loc += elt_size;
9304 if (loc != p)
9305 {
9306 /* Chances are there is a run of relocs to insert here,
9307 from one of more input files. Files are not always
9308 linked in order due to the way elf_link_input_bfd is
9309 called. See pr17666. */
9310 size_t sortlen = p - loc;
9311 bfd_vma r_off2 = (*ext_r_off) (loc);
9312 size_t runlen = elt_size;
9313 size_t buf_size = 96 * 1024;
9314 while (p + runlen < end
9315 && (sortlen <= buf_size
9316 || runlen + elt_size <= buf_size)
9317 && r_off2 > (*ext_r_off) (p + runlen))
9318 runlen += elt_size;
9319 if (buf == NULL)
9320 {
9321 buf = bfd_malloc (buf_size);
9322 if (buf == NULL)
9323 return FALSE;
9324 }
9325 if (runlen < sortlen)
9326 {
9327 memcpy (buf, p, runlen);
9328 memmove (loc + runlen, loc, sortlen);
9329 memcpy (loc, buf, runlen);
9330 }
9331 else
9332 {
9333 memcpy (buf, loc, sortlen);
9334 memmove (loc, p, runlen);
9335 memcpy (loc + runlen, buf, sortlen);
9336 }
9337 p += runlen - elt_size;
9338 }
9339 }
9340 /* Hashes are no longer valid. */
9341 free (reldata->hashes);
9342 reldata->hashes = NULL;
9343 free (buf);
9344 }
9345 return TRUE;
9346 }
9347
9348 struct elf_link_sort_rela
9349 {
9350 union {
9351 bfd_vma offset;
9352 bfd_vma sym_mask;
9353 } u;
9354 enum elf_reloc_type_class type;
9355 /* We use this as an array of size int_rels_per_ext_rel. */
9356 Elf_Internal_Rela rela[1];
9357 };
9358
9359 /* qsort stability here and for cmp2 is only an issue if multiple
9360 dynamic relocations are emitted at the same address. But targets
9361 that apply a series of dynamic relocations each operating on the
9362 result of the prior relocation can't use -z combreloc as
9363 implemented anyway. Such schemes tend to be broken by sorting on
9364 symbol index. That leaves dynamic NONE relocs as the only other
9365 case where ld might emit multiple relocs at the same address, and
9366 those are only emitted due to target bugs. */
9367
9368 static int
9369 elf_link_sort_cmp1 (const void *A, const void *B)
9370 {
9371 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9372 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9373 int relativea, relativeb;
9374
9375 relativea = a->type == reloc_class_relative;
9376 relativeb = b->type == reloc_class_relative;
9377
9378 if (relativea < relativeb)
9379 return 1;
9380 if (relativea > relativeb)
9381 return -1;
9382 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9383 return -1;
9384 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9385 return 1;
9386 if (a->rela->r_offset < b->rela->r_offset)
9387 return -1;
9388 if (a->rela->r_offset > b->rela->r_offset)
9389 return 1;
9390 return 0;
9391 }
9392
9393 static int
9394 elf_link_sort_cmp2 (const void *A, const void *B)
9395 {
9396 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9397 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9398
9399 if (a->type < b->type)
9400 return -1;
9401 if (a->type > b->type)
9402 return 1;
9403 if (a->u.offset < b->u.offset)
9404 return -1;
9405 if (a->u.offset > b->u.offset)
9406 return 1;
9407 if (a->rela->r_offset < b->rela->r_offset)
9408 return -1;
9409 if (a->rela->r_offset > b->rela->r_offset)
9410 return 1;
9411 return 0;
9412 }
9413
9414 static size_t
9415 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9416 {
9417 asection *dynamic_relocs;
9418 asection *rela_dyn;
9419 asection *rel_dyn;
9420 bfd_size_type count, size;
9421 size_t i, ret, sort_elt, ext_size;
9422 bfd_byte *sort, *s_non_relative, *p;
9423 struct elf_link_sort_rela *sq;
9424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9425 int i2e = bed->s->int_rels_per_ext_rel;
9426 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9427 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9428 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9429 struct bfd_link_order *lo;
9430 bfd_vma r_sym_mask;
9431 bfd_boolean use_rela;
9432
9433 /* Find a dynamic reloc section. */
9434 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9435 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9436 if (rela_dyn != NULL && rela_dyn->size > 0
9437 && rel_dyn != NULL && rel_dyn->size > 0)
9438 {
9439 bfd_boolean use_rela_initialised = FALSE;
9440
9441 /* This is just here to stop gcc from complaining.
9442 Its initialization checking code is not perfect. */
9443 use_rela = TRUE;
9444
9445 /* Both sections are present. Examine the sizes
9446 of the indirect sections to help us choose. */
9447 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9448 if (lo->type == bfd_indirect_link_order)
9449 {
9450 asection *o = lo->u.indirect.section;
9451
9452 if ((o->size % bed->s->sizeof_rela) == 0)
9453 {
9454 if ((o->size % bed->s->sizeof_rel) == 0)
9455 /* Section size is divisible by both rel and rela sizes.
9456 It is of no help to us. */
9457 ;
9458 else
9459 {
9460 /* Section size is only divisible by rela. */
9461 if (use_rela_initialised && !use_rela)
9462 {
9463 _bfd_error_handler (_("%pB: unable to sort relocs - "
9464 "they are in more than one size"),
9465 abfd);
9466 bfd_set_error (bfd_error_invalid_operation);
9467 return 0;
9468 }
9469 else
9470 {
9471 use_rela = TRUE;
9472 use_rela_initialised = TRUE;
9473 }
9474 }
9475 }
9476 else if ((o->size % bed->s->sizeof_rel) == 0)
9477 {
9478 /* Section size is only divisible by rel. */
9479 if (use_rela_initialised && use_rela)
9480 {
9481 _bfd_error_handler (_("%pB: unable to sort relocs - "
9482 "they are in more than one size"),
9483 abfd);
9484 bfd_set_error (bfd_error_invalid_operation);
9485 return 0;
9486 }
9487 else
9488 {
9489 use_rela = FALSE;
9490 use_rela_initialised = TRUE;
9491 }
9492 }
9493 else
9494 {
9495 /* The section size is not divisible by either -
9496 something is wrong. */
9497 _bfd_error_handler (_("%pB: unable to sort relocs - "
9498 "they are of an unknown size"), abfd);
9499 bfd_set_error (bfd_error_invalid_operation);
9500 return 0;
9501 }
9502 }
9503
9504 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9505 if (lo->type == bfd_indirect_link_order)
9506 {
9507 asection *o = lo->u.indirect.section;
9508
9509 if ((o->size % bed->s->sizeof_rela) == 0)
9510 {
9511 if ((o->size % bed->s->sizeof_rel) == 0)
9512 /* Section size is divisible by both rel and rela sizes.
9513 It is of no help to us. */
9514 ;
9515 else
9516 {
9517 /* Section size is only divisible by rela. */
9518 if (use_rela_initialised && !use_rela)
9519 {
9520 _bfd_error_handler (_("%pB: unable to sort relocs - "
9521 "they are in more than one size"),
9522 abfd);
9523 bfd_set_error (bfd_error_invalid_operation);
9524 return 0;
9525 }
9526 else
9527 {
9528 use_rela = TRUE;
9529 use_rela_initialised = TRUE;
9530 }
9531 }
9532 }
9533 else if ((o->size % bed->s->sizeof_rel) == 0)
9534 {
9535 /* Section size is only divisible by rel. */
9536 if (use_rela_initialised && use_rela)
9537 {
9538 _bfd_error_handler (_("%pB: unable to sort relocs - "
9539 "they are in more than one size"),
9540 abfd);
9541 bfd_set_error (bfd_error_invalid_operation);
9542 return 0;
9543 }
9544 else
9545 {
9546 use_rela = FALSE;
9547 use_rela_initialised = TRUE;
9548 }
9549 }
9550 else
9551 {
9552 /* The section size is not divisible by either -
9553 something is wrong. */
9554 _bfd_error_handler (_("%pB: unable to sort relocs - "
9555 "they are of an unknown size"), abfd);
9556 bfd_set_error (bfd_error_invalid_operation);
9557 return 0;
9558 }
9559 }
9560
9561 if (! use_rela_initialised)
9562 /* Make a guess. */
9563 use_rela = TRUE;
9564 }
9565 else if (rela_dyn != NULL && rela_dyn->size > 0)
9566 use_rela = TRUE;
9567 else if (rel_dyn != NULL && rel_dyn->size > 0)
9568 use_rela = FALSE;
9569 else
9570 return 0;
9571
9572 if (use_rela)
9573 {
9574 dynamic_relocs = rela_dyn;
9575 ext_size = bed->s->sizeof_rela;
9576 swap_in = bed->s->swap_reloca_in;
9577 swap_out = bed->s->swap_reloca_out;
9578 }
9579 else
9580 {
9581 dynamic_relocs = rel_dyn;
9582 ext_size = bed->s->sizeof_rel;
9583 swap_in = bed->s->swap_reloc_in;
9584 swap_out = bed->s->swap_reloc_out;
9585 }
9586
9587 size = 0;
9588 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9589 if (lo->type == bfd_indirect_link_order)
9590 size += lo->u.indirect.section->size;
9591
9592 if (size != dynamic_relocs->size)
9593 return 0;
9594
9595 sort_elt = (sizeof (struct elf_link_sort_rela)
9596 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9597
9598 count = dynamic_relocs->size / ext_size;
9599 if (count == 0)
9600 return 0;
9601 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9602
9603 if (sort == NULL)
9604 {
9605 (*info->callbacks->warning)
9606 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9607 return 0;
9608 }
9609
9610 if (bed->s->arch_size == 32)
9611 r_sym_mask = ~(bfd_vma) 0xff;
9612 else
9613 r_sym_mask = ~(bfd_vma) 0xffffffff;
9614
9615 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9616 if (lo->type == bfd_indirect_link_order)
9617 {
9618 bfd_byte *erel, *erelend;
9619 asection *o = lo->u.indirect.section;
9620
9621 if (o->contents == NULL && o->size != 0)
9622 {
9623 /* This is a reloc section that is being handled as a normal
9624 section. See bfd_section_from_shdr. We can't combine
9625 relocs in this case. */
9626 free (sort);
9627 return 0;
9628 }
9629 erel = o->contents;
9630 erelend = o->contents + o->size;
9631 p = sort + o->output_offset * opb / ext_size * sort_elt;
9632
9633 while (erel < erelend)
9634 {
9635 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9636
9637 (*swap_in) (abfd, erel, s->rela);
9638 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9639 s->u.sym_mask = r_sym_mask;
9640 p += sort_elt;
9641 erel += ext_size;
9642 }
9643 }
9644
9645 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9646
9647 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9648 {
9649 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9650 if (s->type != reloc_class_relative)
9651 break;
9652 }
9653 ret = i;
9654 s_non_relative = p;
9655
9656 sq = (struct elf_link_sort_rela *) s_non_relative;
9657 for (; i < count; i++, p += sort_elt)
9658 {
9659 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9660 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9661 sq = sp;
9662 sp->u.offset = sq->rela->r_offset;
9663 }
9664
9665 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9666
9667 struct elf_link_hash_table *htab = elf_hash_table (info);
9668 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9669 {
9670 /* We have plt relocs in .rela.dyn. */
9671 sq = (struct elf_link_sort_rela *) sort;
9672 for (i = 0; i < count; i++)
9673 if (sq[count - i - 1].type != reloc_class_plt)
9674 break;
9675 if (i != 0 && htab->srelplt->size == i * ext_size)
9676 {
9677 struct bfd_link_order **plo;
9678 /* Put srelplt link_order last. This is so the output_offset
9679 set in the next loop is correct for DT_JMPREL. */
9680 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9681 if ((*plo)->type == bfd_indirect_link_order
9682 && (*plo)->u.indirect.section == htab->srelplt)
9683 {
9684 lo = *plo;
9685 *plo = lo->next;
9686 }
9687 else
9688 plo = &(*plo)->next;
9689 *plo = lo;
9690 lo->next = NULL;
9691 dynamic_relocs->map_tail.link_order = lo;
9692 }
9693 }
9694
9695 p = sort;
9696 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9697 if (lo->type == bfd_indirect_link_order)
9698 {
9699 bfd_byte *erel, *erelend;
9700 asection *o = lo->u.indirect.section;
9701
9702 erel = o->contents;
9703 erelend = o->contents + o->size;
9704 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9705 while (erel < erelend)
9706 {
9707 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9708 (*swap_out) (abfd, s->rela, erel);
9709 p += sort_elt;
9710 erel += ext_size;
9711 }
9712 }
9713
9714 free (sort);
9715 *psec = dynamic_relocs;
9716 return ret;
9717 }
9718
9719 /* Add a symbol to the output symbol string table. */
9720
9721 static int
9722 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9723 const char *name,
9724 Elf_Internal_Sym *elfsym,
9725 asection *input_sec,
9726 struct elf_link_hash_entry *h)
9727 {
9728 int (*output_symbol_hook)
9729 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9730 struct elf_link_hash_entry *);
9731 struct elf_link_hash_table *hash_table;
9732 const struct elf_backend_data *bed;
9733 bfd_size_type strtabsize;
9734
9735 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9736
9737 bed = get_elf_backend_data (flinfo->output_bfd);
9738 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9739 if (output_symbol_hook != NULL)
9740 {
9741 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9742 if (ret != 1)
9743 return ret;
9744 }
9745
9746 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9747 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9748 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9749 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9750
9751 if (name == NULL
9752 || *name == '\0'
9753 || (input_sec->flags & SEC_EXCLUDE))
9754 elfsym->st_name = (unsigned long) -1;
9755 else
9756 {
9757 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9758 to get the final offset for st_name. */
9759 char *versioned_name = (char *) name;
9760 if (h != NULL)
9761 {
9762 if (h->versioned == versioned && h->def_dynamic)
9763 {
9764 /* Keep only one '@' for versioned symbols defined in
9765 shared objects. */
9766 char *version = strrchr (name, ELF_VER_CHR);
9767 char *base_end = strchr (name, ELF_VER_CHR);
9768 if (version != base_end)
9769 {
9770 size_t base_len;
9771 size_t len = strlen (name);
9772 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9773 if (versioned_name == NULL)
9774 return 0;
9775 base_len = base_end - name;
9776 memcpy (versioned_name, name, base_len);
9777 memcpy (versioned_name + base_len, version,
9778 len - base_len);
9779 }
9780 }
9781 }
9782 else if (flinfo->info->unique_symbol
9783 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9784 {
9785 struct local_hash_entry *lh;
9786 switch (ELF_ST_TYPE (elfsym->st_info))
9787 {
9788 case STT_FILE:
9789 case STT_SECTION:
9790 break;
9791 default:
9792 lh = (struct local_hash_entry *) bfd_hash_lookup
9793 (&flinfo->local_hash_table, name, TRUE, FALSE);
9794 if (lh == NULL)
9795 return 0;
9796 if (lh->count)
9797 {
9798 /* Append ".COUNT" to duplicated local symbols. */
9799 size_t count_len;
9800 size_t base_len = lh->size;
9801 char buf[30];
9802 sprintf (buf, "%lx", lh->count);
9803 if (!base_len)
9804 {
9805 base_len = strlen (name);
9806 lh->size = base_len;
9807 }
9808 count_len = strlen (buf);
9809 versioned_name = bfd_alloc (flinfo->output_bfd,
9810 base_len + count_len + 2);
9811 if (versioned_name == NULL)
9812 return 0;
9813 memcpy (versioned_name, name, base_len);
9814 versioned_name[base_len] = '.';
9815 memcpy (versioned_name + base_len + 1, buf,
9816 count_len + 1);
9817 }
9818 lh->count++;
9819 break;
9820 }
9821 }
9822 elfsym->st_name
9823 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9824 versioned_name, FALSE);
9825 if (elfsym->st_name == (unsigned long) -1)
9826 return 0;
9827 }
9828
9829 hash_table = elf_hash_table (flinfo->info);
9830 strtabsize = hash_table->strtabsize;
9831 if (strtabsize <= hash_table->strtabcount)
9832 {
9833 strtabsize += strtabsize;
9834 hash_table->strtabsize = strtabsize;
9835 strtabsize *= sizeof (*hash_table->strtab);
9836 hash_table->strtab
9837 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9838 strtabsize);
9839 if (hash_table->strtab == NULL)
9840 return 0;
9841 }
9842 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9843 hash_table->strtab[hash_table->strtabcount].dest_index
9844 = hash_table->strtabcount;
9845 hash_table->strtab[hash_table->strtabcount].destshndx_index
9846 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9847
9848 flinfo->output_bfd->symcount += 1;
9849 hash_table->strtabcount += 1;
9850
9851 return 1;
9852 }
9853
9854 /* Swap symbols out to the symbol table and flush the output symbols to
9855 the file. */
9856
9857 static bfd_boolean
9858 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9859 {
9860 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9861 size_t amt;
9862 size_t i;
9863 const struct elf_backend_data *bed;
9864 bfd_byte *symbuf;
9865 Elf_Internal_Shdr *hdr;
9866 file_ptr pos;
9867 bfd_boolean ret;
9868
9869 if (!hash_table->strtabcount)
9870 return TRUE;
9871
9872 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9873
9874 bed = get_elf_backend_data (flinfo->output_bfd);
9875
9876 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9877 symbuf = (bfd_byte *) bfd_malloc (amt);
9878 if (symbuf == NULL)
9879 return FALSE;
9880
9881 if (flinfo->symshndxbuf)
9882 {
9883 amt = sizeof (Elf_External_Sym_Shndx);
9884 amt *= bfd_get_symcount (flinfo->output_bfd);
9885 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9886 if (flinfo->symshndxbuf == NULL)
9887 {
9888 free (symbuf);
9889 return FALSE;
9890 }
9891 }
9892
9893 /* Now swap out the symbols. */
9894 for (i = 0; i < hash_table->strtabcount; i++)
9895 {
9896 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9897 if (elfsym->sym.st_name == (unsigned long) -1)
9898 elfsym->sym.st_name = 0;
9899 else
9900 elfsym->sym.st_name
9901 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9902 elfsym->sym.st_name);
9903
9904 /* Inform the linker of the addition of this symbol. */
9905
9906 if (flinfo->info->callbacks->ctf_new_symbol)
9907 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9908 &elfsym->sym);
9909
9910 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9911 ((bfd_byte *) symbuf
9912 + (elfsym->dest_index
9913 * bed->s->sizeof_sym)),
9914 (flinfo->symshndxbuf
9915 + elfsym->destshndx_index));
9916 }
9917
9918 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9919 pos = hdr->sh_offset + hdr->sh_size;
9920 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9921 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9922 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9923 {
9924 hdr->sh_size += amt;
9925 ret = TRUE;
9926 }
9927 else
9928 ret = FALSE;
9929
9930 free (symbuf);
9931
9932 free (hash_table->strtab);
9933 hash_table->strtab = NULL;
9934
9935 return ret;
9936 }
9937
9938 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9939
9940 static bfd_boolean
9941 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9942 {
9943 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9944 && sym->st_shndx < SHN_LORESERVE)
9945 {
9946 /* The gABI doesn't support dynamic symbols in output sections
9947 beyond 64k. */
9948 _bfd_error_handler
9949 /* xgettext:c-format */
9950 (_("%pB: too many sections: %d (>= %d)"),
9951 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9952 bfd_set_error (bfd_error_nonrepresentable_section);
9953 return FALSE;
9954 }
9955 return TRUE;
9956 }
9957
9958 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9959 allowing an unsatisfied unversioned symbol in the DSO to match a
9960 versioned symbol that would normally require an explicit version.
9961 We also handle the case that a DSO references a hidden symbol
9962 which may be satisfied by a versioned symbol in another DSO. */
9963
9964 static bfd_boolean
9965 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9966 const struct elf_backend_data *bed,
9967 struct elf_link_hash_entry *h)
9968 {
9969 bfd *abfd;
9970 struct elf_link_loaded_list *loaded;
9971
9972 if (!is_elf_hash_table (info->hash))
9973 return FALSE;
9974
9975 /* Check indirect symbol. */
9976 while (h->root.type == bfd_link_hash_indirect)
9977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9978
9979 switch (h->root.type)
9980 {
9981 default:
9982 abfd = NULL;
9983 break;
9984
9985 case bfd_link_hash_undefined:
9986 case bfd_link_hash_undefweak:
9987 abfd = h->root.u.undef.abfd;
9988 if (abfd == NULL
9989 || (abfd->flags & DYNAMIC) == 0
9990 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9991 return FALSE;
9992 break;
9993
9994 case bfd_link_hash_defined:
9995 case bfd_link_hash_defweak:
9996 abfd = h->root.u.def.section->owner;
9997 break;
9998
9999 case bfd_link_hash_common:
10000 abfd = h->root.u.c.p->section->owner;
10001 break;
10002 }
10003 BFD_ASSERT (abfd != NULL);
10004
10005 for (loaded = elf_hash_table (info)->dyn_loaded;
10006 loaded != NULL;
10007 loaded = loaded->next)
10008 {
10009 bfd *input;
10010 Elf_Internal_Shdr *hdr;
10011 size_t symcount;
10012 size_t extsymcount;
10013 size_t extsymoff;
10014 Elf_Internal_Shdr *versymhdr;
10015 Elf_Internal_Sym *isym;
10016 Elf_Internal_Sym *isymend;
10017 Elf_Internal_Sym *isymbuf;
10018 Elf_External_Versym *ever;
10019 Elf_External_Versym *extversym;
10020
10021 input = loaded->abfd;
10022
10023 /* We check each DSO for a possible hidden versioned definition. */
10024 if (input == abfd
10025 || elf_dynversym (input) == 0)
10026 continue;
10027
10028 hdr = &elf_tdata (input)->dynsymtab_hdr;
10029
10030 symcount = hdr->sh_size / bed->s->sizeof_sym;
10031 if (elf_bad_symtab (input))
10032 {
10033 extsymcount = symcount;
10034 extsymoff = 0;
10035 }
10036 else
10037 {
10038 extsymcount = symcount - hdr->sh_info;
10039 extsymoff = hdr->sh_info;
10040 }
10041
10042 if (extsymcount == 0)
10043 continue;
10044
10045 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10046 NULL, NULL, NULL);
10047 if (isymbuf == NULL)
10048 return FALSE;
10049
10050 /* Read in any version definitions. */
10051 versymhdr = &elf_tdata (input)->dynversym_hdr;
10052 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10053 || (extversym = (Elf_External_Versym *)
10054 _bfd_malloc_and_read (input, versymhdr->sh_size,
10055 versymhdr->sh_size)) == NULL)
10056 {
10057 free (isymbuf);
10058 return FALSE;
10059 }
10060
10061 ever = extversym + extsymoff;
10062 isymend = isymbuf + extsymcount;
10063 for (isym = isymbuf; isym < isymend; isym++, ever++)
10064 {
10065 const char *name;
10066 Elf_Internal_Versym iver;
10067 unsigned short version_index;
10068
10069 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10070 || isym->st_shndx == SHN_UNDEF)
10071 continue;
10072
10073 name = bfd_elf_string_from_elf_section (input,
10074 hdr->sh_link,
10075 isym->st_name);
10076 if (strcmp (name, h->root.root.string) != 0)
10077 continue;
10078
10079 _bfd_elf_swap_versym_in (input, ever, &iver);
10080
10081 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10082 && !(h->def_regular
10083 && h->forced_local))
10084 {
10085 /* If we have a non-hidden versioned sym, then it should
10086 have provided a definition for the undefined sym unless
10087 it is defined in a non-shared object and forced local.
10088 */
10089 abort ();
10090 }
10091
10092 version_index = iver.vs_vers & VERSYM_VERSION;
10093 if (version_index == 1 || version_index == 2)
10094 {
10095 /* This is the base or first version. We can use it. */
10096 free (extversym);
10097 free (isymbuf);
10098 return TRUE;
10099 }
10100 }
10101
10102 free (extversym);
10103 free (isymbuf);
10104 }
10105
10106 return FALSE;
10107 }
10108
10109 /* Convert ELF common symbol TYPE. */
10110
10111 static int
10112 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10113 {
10114 /* Commom symbol can only appear in relocatable link. */
10115 if (!bfd_link_relocatable (info))
10116 abort ();
10117 switch (info->elf_stt_common)
10118 {
10119 case unchanged:
10120 break;
10121 case elf_stt_common:
10122 type = STT_COMMON;
10123 break;
10124 case no_elf_stt_common:
10125 type = STT_OBJECT;
10126 break;
10127 }
10128 return type;
10129 }
10130
10131 /* Add an external symbol to the symbol table. This is called from
10132 the hash table traversal routine. When generating a shared object,
10133 we go through the symbol table twice. The first time we output
10134 anything that might have been forced to local scope in a version
10135 script. The second time we output the symbols that are still
10136 global symbols. */
10137
10138 static bfd_boolean
10139 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10140 {
10141 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10142 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10143 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10144 bfd_boolean strip;
10145 Elf_Internal_Sym sym;
10146 asection *input_sec;
10147 const struct elf_backend_data *bed;
10148 long indx;
10149 int ret;
10150 unsigned int type;
10151
10152 if (h->root.type == bfd_link_hash_warning)
10153 {
10154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10155 if (h->root.type == bfd_link_hash_new)
10156 return TRUE;
10157 }
10158
10159 /* Decide whether to output this symbol in this pass. */
10160 if (eoinfo->localsyms)
10161 {
10162 if (!h->forced_local)
10163 return TRUE;
10164 }
10165 else
10166 {
10167 if (h->forced_local)
10168 return TRUE;
10169 }
10170
10171 bed = get_elf_backend_data (flinfo->output_bfd);
10172
10173 if (h->root.type == bfd_link_hash_undefined)
10174 {
10175 /* If we have an undefined symbol reference here then it must have
10176 come from a shared library that is being linked in. (Undefined
10177 references in regular files have already been handled unless
10178 they are in unreferenced sections which are removed by garbage
10179 collection). */
10180 bfd_boolean ignore_undef = FALSE;
10181
10182 /* Some symbols may be special in that the fact that they're
10183 undefined can be safely ignored - let backend determine that. */
10184 if (bed->elf_backend_ignore_undef_symbol)
10185 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10186
10187 /* If we are reporting errors for this situation then do so now. */
10188 if (!ignore_undef
10189 && h->ref_dynamic_nonweak
10190 && (!h->ref_regular || flinfo->info->gc_sections)
10191 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10192 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10193 {
10194 flinfo->info->callbacks->undefined_symbol
10195 (flinfo->info, h->root.root.string,
10196 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10197 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10198 && !flinfo->info->warn_unresolved_syms);
10199 }
10200
10201 /* Strip a global symbol defined in a discarded section. */
10202 if (h->indx == -3)
10203 return TRUE;
10204 }
10205
10206 /* We should also warn if a forced local symbol is referenced from
10207 shared libraries. */
10208 if (bfd_link_executable (flinfo->info)
10209 && h->forced_local
10210 && h->ref_dynamic
10211 && h->def_regular
10212 && !h->dynamic_def
10213 && h->ref_dynamic_nonweak
10214 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10215 {
10216 bfd *def_bfd;
10217 const char *msg;
10218 struct elf_link_hash_entry *hi = h;
10219
10220 /* Check indirect symbol. */
10221 while (hi->root.type == bfd_link_hash_indirect)
10222 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10223
10224 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10225 /* xgettext:c-format */
10226 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10227 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10228 /* xgettext:c-format */
10229 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10230 else
10231 /* xgettext:c-format */
10232 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10233 def_bfd = flinfo->output_bfd;
10234 if (hi->root.u.def.section != bfd_abs_section_ptr)
10235 def_bfd = hi->root.u.def.section->owner;
10236 _bfd_error_handler (msg, flinfo->output_bfd,
10237 h->root.root.string, def_bfd);
10238 bfd_set_error (bfd_error_bad_value);
10239 eoinfo->failed = TRUE;
10240 return FALSE;
10241 }
10242
10243 /* We don't want to output symbols that have never been mentioned by
10244 a regular file, or that we have been told to strip. However, if
10245 h->indx is set to -2, the symbol is used by a reloc and we must
10246 output it. */
10247 strip = FALSE;
10248 if (h->indx == -2)
10249 ;
10250 else if ((h->def_dynamic
10251 || h->ref_dynamic
10252 || h->root.type == bfd_link_hash_new)
10253 && !h->def_regular
10254 && !h->ref_regular)
10255 strip = TRUE;
10256 else if (flinfo->info->strip == strip_all)
10257 strip = TRUE;
10258 else if (flinfo->info->strip == strip_some
10259 && bfd_hash_lookup (flinfo->info->keep_hash,
10260 h->root.root.string, FALSE, FALSE) == NULL)
10261 strip = TRUE;
10262 else if ((h->root.type == bfd_link_hash_defined
10263 || h->root.type == bfd_link_hash_defweak)
10264 && ((flinfo->info->strip_discarded
10265 && discarded_section (h->root.u.def.section))
10266 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10267 && h->root.u.def.section->owner != NULL
10268 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10269 strip = TRUE;
10270 else if ((h->root.type == bfd_link_hash_undefined
10271 || h->root.type == bfd_link_hash_undefweak)
10272 && h->root.u.undef.abfd != NULL
10273 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10274 strip = TRUE;
10275
10276 type = h->type;
10277
10278 /* If we're stripping it, and it's not a dynamic symbol, there's
10279 nothing else to do. However, if it is a forced local symbol or
10280 an ifunc symbol we need to give the backend finish_dynamic_symbol
10281 function a chance to make it dynamic. */
10282 if (strip
10283 && h->dynindx == -1
10284 && type != STT_GNU_IFUNC
10285 && !h->forced_local)
10286 return TRUE;
10287
10288 sym.st_value = 0;
10289 sym.st_size = h->size;
10290 sym.st_other = h->other;
10291 switch (h->root.type)
10292 {
10293 default:
10294 case bfd_link_hash_new:
10295 case bfd_link_hash_warning:
10296 abort ();
10297 return FALSE;
10298
10299 case bfd_link_hash_undefined:
10300 case bfd_link_hash_undefweak:
10301 input_sec = bfd_und_section_ptr;
10302 sym.st_shndx = SHN_UNDEF;
10303 break;
10304
10305 case bfd_link_hash_defined:
10306 case bfd_link_hash_defweak:
10307 {
10308 input_sec = h->root.u.def.section;
10309 if (input_sec->output_section != NULL)
10310 {
10311 sym.st_shndx =
10312 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10313 input_sec->output_section);
10314 if (sym.st_shndx == SHN_BAD)
10315 {
10316 _bfd_error_handler
10317 /* xgettext:c-format */
10318 (_("%pB: could not find output section %pA for input section %pA"),
10319 flinfo->output_bfd, input_sec->output_section, input_sec);
10320 bfd_set_error (bfd_error_nonrepresentable_section);
10321 eoinfo->failed = TRUE;
10322 return FALSE;
10323 }
10324
10325 /* ELF symbols in relocatable files are section relative,
10326 but in nonrelocatable files they are virtual
10327 addresses. */
10328 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10329 if (!bfd_link_relocatable (flinfo->info))
10330 {
10331 sym.st_value += input_sec->output_section->vma;
10332 if (h->type == STT_TLS)
10333 {
10334 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10335 if (tls_sec != NULL)
10336 sym.st_value -= tls_sec->vma;
10337 }
10338 }
10339 }
10340 else
10341 {
10342 BFD_ASSERT (input_sec->owner == NULL
10343 || (input_sec->owner->flags & DYNAMIC) != 0);
10344 sym.st_shndx = SHN_UNDEF;
10345 input_sec = bfd_und_section_ptr;
10346 }
10347 }
10348 break;
10349
10350 case bfd_link_hash_common:
10351 input_sec = h->root.u.c.p->section;
10352 sym.st_shndx = bed->common_section_index (input_sec);
10353 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10354 break;
10355
10356 case bfd_link_hash_indirect:
10357 /* These symbols are created by symbol versioning. They point
10358 to the decorated version of the name. For example, if the
10359 symbol foo@@GNU_1.2 is the default, which should be used when
10360 foo is used with no version, then we add an indirect symbol
10361 foo which points to foo@@GNU_1.2. We ignore these symbols,
10362 since the indirected symbol is already in the hash table. */
10363 return TRUE;
10364 }
10365
10366 if (type == STT_COMMON || type == STT_OBJECT)
10367 switch (h->root.type)
10368 {
10369 case bfd_link_hash_common:
10370 type = elf_link_convert_common_type (flinfo->info, type);
10371 break;
10372 case bfd_link_hash_defined:
10373 case bfd_link_hash_defweak:
10374 if (bed->common_definition (&sym))
10375 type = elf_link_convert_common_type (flinfo->info, type);
10376 else
10377 type = STT_OBJECT;
10378 break;
10379 case bfd_link_hash_undefined:
10380 case bfd_link_hash_undefweak:
10381 break;
10382 default:
10383 abort ();
10384 }
10385
10386 if (h->forced_local)
10387 {
10388 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10389 /* Turn off visibility on local symbol. */
10390 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10391 }
10392 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10393 else if (h->unique_global && h->def_regular)
10394 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10395 else if (h->root.type == bfd_link_hash_undefweak
10396 || h->root.type == bfd_link_hash_defweak)
10397 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10398 else
10399 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10400 sym.st_target_internal = h->target_internal;
10401
10402 /* Give the processor backend a chance to tweak the symbol value,
10403 and also to finish up anything that needs to be done for this
10404 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10405 forced local syms when non-shared is due to a historical quirk.
10406 STT_GNU_IFUNC symbol must go through PLT. */
10407 if ((h->type == STT_GNU_IFUNC
10408 && h->def_regular
10409 && !bfd_link_relocatable (flinfo->info))
10410 || ((h->dynindx != -1
10411 || h->forced_local)
10412 && ((bfd_link_pic (flinfo->info)
10413 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10414 || h->root.type != bfd_link_hash_undefweak))
10415 || !h->forced_local)
10416 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10417 {
10418 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10419 (flinfo->output_bfd, flinfo->info, h, &sym)))
10420 {
10421 eoinfo->failed = TRUE;
10422 return FALSE;
10423 }
10424 }
10425
10426 /* If we are marking the symbol as undefined, and there are no
10427 non-weak references to this symbol from a regular object, then
10428 mark the symbol as weak undefined; if there are non-weak
10429 references, mark the symbol as strong. We can't do this earlier,
10430 because it might not be marked as undefined until the
10431 finish_dynamic_symbol routine gets through with it. */
10432 if (sym.st_shndx == SHN_UNDEF
10433 && h->ref_regular
10434 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10435 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10436 {
10437 int bindtype;
10438 type = ELF_ST_TYPE (sym.st_info);
10439
10440 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10441 if (type == STT_GNU_IFUNC)
10442 type = STT_FUNC;
10443
10444 if (h->ref_regular_nonweak)
10445 bindtype = STB_GLOBAL;
10446 else
10447 bindtype = STB_WEAK;
10448 sym.st_info = ELF_ST_INFO (bindtype, type);
10449 }
10450
10451 /* If this is a symbol defined in a dynamic library, don't use the
10452 symbol size from the dynamic library. Relinking an executable
10453 against a new library may introduce gratuitous changes in the
10454 executable's symbols if we keep the size. */
10455 if (sym.st_shndx == SHN_UNDEF
10456 && !h->def_regular
10457 && h->def_dynamic)
10458 sym.st_size = 0;
10459
10460 /* If a non-weak symbol with non-default visibility is not defined
10461 locally, it is a fatal error. */
10462 if (!bfd_link_relocatable (flinfo->info)
10463 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10464 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10465 && h->root.type == bfd_link_hash_undefined
10466 && !h->def_regular)
10467 {
10468 const char *msg;
10469
10470 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10471 /* xgettext:c-format */
10472 msg = _("%pB: protected symbol `%s' isn't defined");
10473 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10474 /* xgettext:c-format */
10475 msg = _("%pB: internal symbol `%s' isn't defined");
10476 else
10477 /* xgettext:c-format */
10478 msg = _("%pB: hidden symbol `%s' isn't defined");
10479 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10480 bfd_set_error (bfd_error_bad_value);
10481 eoinfo->failed = TRUE;
10482 return FALSE;
10483 }
10484
10485 /* If this symbol should be put in the .dynsym section, then put it
10486 there now. We already know the symbol index. We also fill in
10487 the entry in the .hash section. */
10488 if (h->dynindx != -1
10489 && elf_hash_table (flinfo->info)->dynamic_sections_created
10490 && elf_hash_table (flinfo->info)->dynsym != NULL
10491 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10492 {
10493 bfd_byte *esym;
10494
10495 /* Since there is no version information in the dynamic string,
10496 if there is no version info in symbol version section, we will
10497 have a run-time problem if not linking executable, referenced
10498 by shared library, or not bound locally. */
10499 if (h->verinfo.verdef == NULL
10500 && (!bfd_link_executable (flinfo->info)
10501 || h->ref_dynamic
10502 || !h->def_regular))
10503 {
10504 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10505
10506 if (p && p [1] != '\0')
10507 {
10508 _bfd_error_handler
10509 /* xgettext:c-format */
10510 (_("%pB: no symbol version section for versioned symbol `%s'"),
10511 flinfo->output_bfd, h->root.root.string);
10512 eoinfo->failed = TRUE;
10513 return FALSE;
10514 }
10515 }
10516
10517 sym.st_name = h->dynstr_index;
10518 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10519 + h->dynindx * bed->s->sizeof_sym);
10520 if (!check_dynsym (flinfo->output_bfd, &sym))
10521 {
10522 eoinfo->failed = TRUE;
10523 return FALSE;
10524 }
10525
10526 /* Inform the linker of the addition of this symbol. */
10527
10528 if (flinfo->info->callbacks->ctf_new_dynsym)
10529 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10530
10531 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10532
10533 if (flinfo->hash_sec != NULL)
10534 {
10535 size_t hash_entry_size;
10536 bfd_byte *bucketpos;
10537 bfd_vma chain;
10538 size_t bucketcount;
10539 size_t bucket;
10540
10541 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10542 bucket = h->u.elf_hash_value % bucketcount;
10543
10544 hash_entry_size
10545 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10546 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10547 + (bucket + 2) * hash_entry_size);
10548 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10549 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10550 bucketpos);
10551 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10552 ((bfd_byte *) flinfo->hash_sec->contents
10553 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10554 }
10555
10556 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10557 {
10558 Elf_Internal_Versym iversym;
10559 Elf_External_Versym *eversym;
10560
10561 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10562 {
10563 if (h->verinfo.verdef == NULL
10564 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10565 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10566 iversym.vs_vers = 0;
10567 else
10568 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10569 }
10570 else
10571 {
10572 if (h->verinfo.vertree == NULL)
10573 iversym.vs_vers = 1;
10574 else
10575 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10576 if (flinfo->info->create_default_symver)
10577 iversym.vs_vers++;
10578 }
10579
10580 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10581 defined locally. */
10582 if (h->versioned == versioned_hidden && h->def_regular)
10583 iversym.vs_vers |= VERSYM_HIDDEN;
10584
10585 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10586 eversym += h->dynindx;
10587 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10588 }
10589 }
10590
10591 /* If the symbol is undefined, and we didn't output it to .dynsym,
10592 strip it from .symtab too. Obviously we can't do this for
10593 relocatable output or when needed for --emit-relocs. */
10594 else if (input_sec == bfd_und_section_ptr
10595 && h->indx != -2
10596 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10597 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10598 && !bfd_link_relocatable (flinfo->info))
10599 return TRUE;
10600
10601 /* Also strip others that we couldn't earlier due to dynamic symbol
10602 processing. */
10603 if (strip)
10604 return TRUE;
10605 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10606 return TRUE;
10607
10608 /* Output a FILE symbol so that following locals are not associated
10609 with the wrong input file. We need one for forced local symbols
10610 if we've seen more than one FILE symbol or when we have exactly
10611 one FILE symbol but global symbols are present in a file other
10612 than the one with the FILE symbol. We also need one if linker
10613 defined symbols are present. In practice these conditions are
10614 always met, so just emit the FILE symbol unconditionally. */
10615 if (eoinfo->localsyms
10616 && !eoinfo->file_sym_done
10617 && eoinfo->flinfo->filesym_count != 0)
10618 {
10619 Elf_Internal_Sym fsym;
10620
10621 memset (&fsym, 0, sizeof (fsym));
10622 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10623 fsym.st_shndx = SHN_ABS;
10624 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10625 bfd_und_section_ptr, NULL))
10626 return FALSE;
10627
10628 eoinfo->file_sym_done = TRUE;
10629 }
10630
10631 indx = bfd_get_symcount (flinfo->output_bfd);
10632 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10633 input_sec, h);
10634 if (ret == 0)
10635 {
10636 eoinfo->failed = TRUE;
10637 return FALSE;
10638 }
10639 else if (ret == 1)
10640 h->indx = indx;
10641 else if (h->indx == -2)
10642 abort();
10643
10644 return TRUE;
10645 }
10646
10647 /* Return TRUE if special handling is done for relocs in SEC against
10648 symbols defined in discarded sections. */
10649
10650 static bfd_boolean
10651 elf_section_ignore_discarded_relocs (asection *sec)
10652 {
10653 const struct elf_backend_data *bed;
10654
10655 switch (sec->sec_info_type)
10656 {
10657 case SEC_INFO_TYPE_STABS:
10658 case SEC_INFO_TYPE_EH_FRAME:
10659 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10660 return TRUE;
10661 default:
10662 break;
10663 }
10664
10665 bed = get_elf_backend_data (sec->owner);
10666 if (bed->elf_backend_ignore_discarded_relocs != NULL
10667 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10668 return TRUE;
10669
10670 return FALSE;
10671 }
10672
10673 /* Return a mask saying how ld should treat relocations in SEC against
10674 symbols defined in discarded sections. If this function returns
10675 COMPLAIN set, ld will issue a warning message. If this function
10676 returns PRETEND set, and the discarded section was link-once and the
10677 same size as the kept link-once section, ld will pretend that the
10678 symbol was actually defined in the kept section. Otherwise ld will
10679 zero the reloc (at least that is the intent, but some cooperation by
10680 the target dependent code is needed, particularly for REL targets). */
10681
10682 unsigned int
10683 _bfd_elf_default_action_discarded (asection *sec)
10684 {
10685 if (sec->flags & SEC_DEBUGGING)
10686 return PRETEND;
10687
10688 if (strcmp (".eh_frame", sec->name) == 0)
10689 return 0;
10690
10691 if (strcmp (".gcc_except_table", sec->name) == 0)
10692 return 0;
10693
10694 return COMPLAIN | PRETEND;
10695 }
10696
10697 /* Find a match between a section and a member of a section group. */
10698
10699 static asection *
10700 match_group_member (asection *sec, asection *group,
10701 struct bfd_link_info *info)
10702 {
10703 asection *first = elf_next_in_group (group);
10704 asection *s = first;
10705
10706 while (s != NULL)
10707 {
10708 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10709 return s;
10710
10711 s = elf_next_in_group (s);
10712 if (s == first)
10713 break;
10714 }
10715
10716 return NULL;
10717 }
10718
10719 /* Check if the kept section of a discarded section SEC can be used
10720 to replace it. Return the replacement if it is OK. Otherwise return
10721 NULL. */
10722
10723 asection *
10724 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10725 {
10726 asection *kept;
10727
10728 kept = sec->kept_section;
10729 if (kept != NULL)
10730 {
10731 if ((kept->flags & SEC_GROUP) != 0)
10732 kept = match_group_member (sec, kept, info);
10733 if (kept != NULL)
10734 {
10735 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10736 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10737 kept = NULL;
10738 else
10739 {
10740 /* Get the real kept section. */
10741 asection *next;
10742 for (next = kept->kept_section;
10743 next != NULL;
10744 next = next->kept_section)
10745 kept = next;
10746 }
10747 }
10748 sec->kept_section = kept;
10749 }
10750 return kept;
10751 }
10752
10753 /* Link an input file into the linker output file. This function
10754 handles all the sections and relocations of the input file at once.
10755 This is so that we only have to read the local symbols once, and
10756 don't have to keep them in memory. */
10757
10758 static bfd_boolean
10759 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10760 {
10761 int (*relocate_section)
10762 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10763 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10764 bfd *output_bfd;
10765 Elf_Internal_Shdr *symtab_hdr;
10766 size_t locsymcount;
10767 size_t extsymoff;
10768 Elf_Internal_Sym *isymbuf;
10769 Elf_Internal_Sym *isym;
10770 Elf_Internal_Sym *isymend;
10771 long *pindex;
10772 asection **ppsection;
10773 asection *o;
10774 const struct elf_backend_data *bed;
10775 struct elf_link_hash_entry **sym_hashes;
10776 bfd_size_type address_size;
10777 bfd_vma r_type_mask;
10778 int r_sym_shift;
10779 bfd_boolean have_file_sym = FALSE;
10780
10781 output_bfd = flinfo->output_bfd;
10782 bed = get_elf_backend_data (output_bfd);
10783 relocate_section = bed->elf_backend_relocate_section;
10784
10785 /* If this is a dynamic object, we don't want to do anything here:
10786 we don't want the local symbols, and we don't want the section
10787 contents. */
10788 if ((input_bfd->flags & DYNAMIC) != 0)
10789 return TRUE;
10790
10791 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10792 if (elf_bad_symtab (input_bfd))
10793 {
10794 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10795 extsymoff = 0;
10796 }
10797 else
10798 {
10799 locsymcount = symtab_hdr->sh_info;
10800 extsymoff = symtab_hdr->sh_info;
10801 }
10802
10803 /* Enable GNU OSABI features in the output BFD that are used in the input
10804 BFD. */
10805 if (bed->elf_osabi == ELFOSABI_NONE
10806 || bed->elf_osabi == ELFOSABI_GNU
10807 || bed->elf_osabi == ELFOSABI_FREEBSD)
10808 elf_tdata (output_bfd)->has_gnu_osabi
10809 |= elf_tdata (input_bfd)->has_gnu_osabi;
10810
10811 /* Read the local symbols. */
10812 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10813 if (isymbuf == NULL && locsymcount != 0)
10814 {
10815 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10816 flinfo->internal_syms,
10817 flinfo->external_syms,
10818 flinfo->locsym_shndx);
10819 if (isymbuf == NULL)
10820 return FALSE;
10821 }
10822
10823 /* Find local symbol sections and adjust values of symbols in
10824 SEC_MERGE sections. Write out those local symbols we know are
10825 going into the output file. */
10826 isymend = isymbuf + locsymcount;
10827 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10828 isym < isymend;
10829 isym++, pindex++, ppsection++)
10830 {
10831 asection *isec;
10832 const char *name;
10833 Elf_Internal_Sym osym;
10834 long indx;
10835 int ret;
10836
10837 *pindex = -1;
10838
10839 if (elf_bad_symtab (input_bfd))
10840 {
10841 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10842 {
10843 *ppsection = NULL;
10844 continue;
10845 }
10846 }
10847
10848 if (isym->st_shndx == SHN_UNDEF)
10849 isec = bfd_und_section_ptr;
10850 else if (isym->st_shndx == SHN_ABS)
10851 isec = bfd_abs_section_ptr;
10852 else if (isym->st_shndx == SHN_COMMON)
10853 isec = bfd_com_section_ptr;
10854 else
10855 {
10856 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10857 if (isec == NULL)
10858 {
10859 /* Don't attempt to output symbols with st_shnx in the
10860 reserved range other than SHN_ABS and SHN_COMMON. */
10861 isec = bfd_und_section_ptr;
10862 }
10863 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10864 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10865 isym->st_value =
10866 _bfd_merged_section_offset (output_bfd, &isec,
10867 elf_section_data (isec)->sec_info,
10868 isym->st_value);
10869 }
10870
10871 *ppsection = isec;
10872
10873 /* Don't output the first, undefined, symbol. In fact, don't
10874 output any undefined local symbol. */
10875 if (isec == bfd_und_section_ptr)
10876 continue;
10877
10878 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10879 {
10880 /* We never output section symbols. Instead, we use the
10881 section symbol of the corresponding section in the output
10882 file. */
10883 continue;
10884 }
10885
10886 /* If we are stripping all symbols, we don't want to output this
10887 one. */
10888 if (flinfo->info->strip == strip_all)
10889 continue;
10890
10891 /* If we are discarding all local symbols, we don't want to
10892 output this one. If we are generating a relocatable output
10893 file, then some of the local symbols may be required by
10894 relocs; we output them below as we discover that they are
10895 needed. */
10896 if (flinfo->info->discard == discard_all)
10897 continue;
10898
10899 /* If this symbol is defined in a section which we are
10900 discarding, we don't need to keep it. */
10901 if (isym->st_shndx != SHN_UNDEF
10902 && isym->st_shndx < SHN_LORESERVE
10903 && isec->output_section == NULL
10904 && flinfo->info->non_contiguous_regions
10905 && flinfo->info->non_contiguous_regions_warnings)
10906 {
10907 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10908 "discards section `%s' from '%s'\n"),
10909 isec->name, bfd_get_filename (isec->owner));
10910 continue;
10911 }
10912
10913 if (isym->st_shndx != SHN_UNDEF
10914 && isym->st_shndx < SHN_LORESERVE
10915 && bfd_section_removed_from_list (output_bfd,
10916 isec->output_section))
10917 continue;
10918
10919 /* Get the name of the symbol. */
10920 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10921 isym->st_name);
10922 if (name == NULL)
10923 return FALSE;
10924
10925 /* See if we are discarding symbols with this name. */
10926 if ((flinfo->info->strip == strip_some
10927 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10928 == NULL))
10929 || (((flinfo->info->discard == discard_sec_merge
10930 && (isec->flags & SEC_MERGE)
10931 && !bfd_link_relocatable (flinfo->info))
10932 || flinfo->info->discard == discard_l)
10933 && bfd_is_local_label_name (input_bfd, name)))
10934 continue;
10935
10936 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10937 {
10938 if (input_bfd->lto_output)
10939 /* -flto puts a temp file name here. This means builds
10940 are not reproducible. Discard the symbol. */
10941 continue;
10942 have_file_sym = TRUE;
10943 flinfo->filesym_count += 1;
10944 }
10945 if (!have_file_sym)
10946 {
10947 /* In the absence of debug info, bfd_find_nearest_line uses
10948 FILE symbols to determine the source file for local
10949 function symbols. Provide a FILE symbol here if input
10950 files lack such, so that their symbols won't be
10951 associated with a previous input file. It's not the
10952 source file, but the best we can do. */
10953 have_file_sym = TRUE;
10954 flinfo->filesym_count += 1;
10955 memset (&osym, 0, sizeof (osym));
10956 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10957 osym.st_shndx = SHN_ABS;
10958 if (!elf_link_output_symstrtab (flinfo,
10959 (input_bfd->lto_output ? NULL
10960 : bfd_get_filename (input_bfd)),
10961 &osym, bfd_abs_section_ptr,
10962 NULL))
10963 return FALSE;
10964 }
10965
10966 osym = *isym;
10967
10968 /* Adjust the section index for the output file. */
10969 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10970 isec->output_section);
10971 if (osym.st_shndx == SHN_BAD)
10972 return FALSE;
10973
10974 /* ELF symbols in relocatable files are section relative, but
10975 in executable files they are virtual addresses. Note that
10976 this code assumes that all ELF sections have an associated
10977 BFD section with a reasonable value for output_offset; below
10978 we assume that they also have a reasonable value for
10979 output_section. Any special sections must be set up to meet
10980 these requirements. */
10981 osym.st_value += isec->output_offset;
10982 if (!bfd_link_relocatable (flinfo->info))
10983 {
10984 osym.st_value += isec->output_section->vma;
10985 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10986 {
10987 /* STT_TLS symbols are relative to PT_TLS segment base. */
10988 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10989 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10990 else
10991 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10992 STT_NOTYPE);
10993 }
10994 }
10995
10996 indx = bfd_get_symcount (output_bfd);
10997 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10998 if (ret == 0)
10999 return FALSE;
11000 else if (ret == 1)
11001 *pindex = indx;
11002 }
11003
11004 if (bed->s->arch_size == 32)
11005 {
11006 r_type_mask = 0xff;
11007 r_sym_shift = 8;
11008 address_size = 4;
11009 }
11010 else
11011 {
11012 r_type_mask = 0xffffffff;
11013 r_sym_shift = 32;
11014 address_size = 8;
11015 }
11016
11017 /* Relocate the contents of each section. */
11018 sym_hashes = elf_sym_hashes (input_bfd);
11019 for (o = input_bfd->sections; o != NULL; o = o->next)
11020 {
11021 bfd_byte *contents;
11022
11023 if (! o->linker_mark)
11024 {
11025 /* This section was omitted from the link. */
11026 continue;
11027 }
11028
11029 if (!flinfo->info->resolve_section_groups
11030 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11031 {
11032 /* Deal with the group signature symbol. */
11033 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11034 unsigned long symndx = sec_data->this_hdr.sh_info;
11035 asection *osec = o->output_section;
11036
11037 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11038 if (symndx >= locsymcount
11039 || (elf_bad_symtab (input_bfd)
11040 && flinfo->sections[symndx] == NULL))
11041 {
11042 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11043 while (h->root.type == bfd_link_hash_indirect
11044 || h->root.type == bfd_link_hash_warning)
11045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11046 /* Arrange for symbol to be output. */
11047 h->indx = -2;
11048 elf_section_data (osec)->this_hdr.sh_info = -2;
11049 }
11050 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11051 {
11052 /* We'll use the output section target_index. */
11053 asection *sec = flinfo->sections[symndx]->output_section;
11054 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11055 }
11056 else
11057 {
11058 if (flinfo->indices[symndx] == -1)
11059 {
11060 /* Otherwise output the local symbol now. */
11061 Elf_Internal_Sym sym = isymbuf[symndx];
11062 asection *sec = flinfo->sections[symndx]->output_section;
11063 const char *name;
11064 long indx;
11065 int ret;
11066
11067 name = bfd_elf_string_from_elf_section (input_bfd,
11068 symtab_hdr->sh_link,
11069 sym.st_name);
11070 if (name == NULL)
11071 return FALSE;
11072
11073 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11074 sec);
11075 if (sym.st_shndx == SHN_BAD)
11076 return FALSE;
11077
11078 sym.st_value += o->output_offset;
11079
11080 indx = bfd_get_symcount (output_bfd);
11081 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11082 NULL);
11083 if (ret == 0)
11084 return FALSE;
11085 else if (ret == 1)
11086 flinfo->indices[symndx] = indx;
11087 else
11088 abort ();
11089 }
11090 elf_section_data (osec)->this_hdr.sh_info
11091 = flinfo->indices[symndx];
11092 }
11093 }
11094
11095 if ((o->flags & SEC_HAS_CONTENTS) == 0
11096 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11097 continue;
11098
11099 if ((o->flags & SEC_LINKER_CREATED) != 0)
11100 {
11101 /* Section was created by _bfd_elf_link_create_dynamic_sections
11102 or somesuch. */
11103 continue;
11104 }
11105
11106 /* Get the contents of the section. They have been cached by a
11107 relaxation routine. Note that o is a section in an input
11108 file, so the contents field will not have been set by any of
11109 the routines which work on output files. */
11110 if (elf_section_data (o)->this_hdr.contents != NULL)
11111 {
11112 contents = elf_section_data (o)->this_hdr.contents;
11113 if (bed->caches_rawsize
11114 && o->rawsize != 0
11115 && o->rawsize < o->size)
11116 {
11117 memcpy (flinfo->contents, contents, o->rawsize);
11118 contents = flinfo->contents;
11119 }
11120 }
11121 else
11122 {
11123 contents = flinfo->contents;
11124 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11125 return FALSE;
11126 }
11127
11128 if ((o->flags & SEC_RELOC) != 0)
11129 {
11130 Elf_Internal_Rela *internal_relocs;
11131 Elf_Internal_Rela *rel, *relend;
11132 int action_discarded;
11133 int ret;
11134
11135 /* Get the swapped relocs. */
11136 internal_relocs
11137 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11138 flinfo->internal_relocs, FALSE);
11139 if (internal_relocs == NULL
11140 && o->reloc_count > 0)
11141 return FALSE;
11142
11143 /* We need to reverse-copy input .ctors/.dtors sections if
11144 they are placed in .init_array/.finit_array for output. */
11145 if (o->size > address_size
11146 && ((strncmp (o->name, ".ctors", 6) == 0
11147 && strcmp (o->output_section->name,
11148 ".init_array") == 0)
11149 || (strncmp (o->name, ".dtors", 6) == 0
11150 && strcmp (o->output_section->name,
11151 ".fini_array") == 0))
11152 && (o->name[6] == 0 || o->name[6] == '.'))
11153 {
11154 if (o->size * bed->s->int_rels_per_ext_rel
11155 != o->reloc_count * address_size)
11156 {
11157 _bfd_error_handler
11158 /* xgettext:c-format */
11159 (_("error: %pB: size of section %pA is not "
11160 "multiple of address size"),
11161 input_bfd, o);
11162 bfd_set_error (bfd_error_bad_value);
11163 return FALSE;
11164 }
11165 o->flags |= SEC_ELF_REVERSE_COPY;
11166 }
11167
11168 action_discarded = -1;
11169 if (!elf_section_ignore_discarded_relocs (o))
11170 action_discarded = (*bed->action_discarded) (o);
11171
11172 /* Run through the relocs evaluating complex reloc symbols and
11173 looking for relocs against symbols from discarded sections
11174 or section symbols from removed link-once sections.
11175 Complain about relocs against discarded sections. Zero
11176 relocs against removed link-once sections. */
11177
11178 rel = internal_relocs;
11179 relend = rel + o->reloc_count;
11180 for ( ; rel < relend; rel++)
11181 {
11182 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11183 unsigned int s_type;
11184 asection **ps, *sec;
11185 struct elf_link_hash_entry *h = NULL;
11186 const char *sym_name;
11187
11188 if (r_symndx == STN_UNDEF)
11189 continue;
11190
11191 if (r_symndx >= locsymcount
11192 || (elf_bad_symtab (input_bfd)
11193 && flinfo->sections[r_symndx] == NULL))
11194 {
11195 h = sym_hashes[r_symndx - extsymoff];
11196
11197 /* Badly formatted input files can contain relocs that
11198 reference non-existant symbols. Check here so that
11199 we do not seg fault. */
11200 if (h == NULL)
11201 {
11202 _bfd_error_handler
11203 /* xgettext:c-format */
11204 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11205 "that references a non-existent global symbol"),
11206 input_bfd, (uint64_t) rel->r_info, o);
11207 bfd_set_error (bfd_error_bad_value);
11208 return FALSE;
11209 }
11210
11211 while (h->root.type == bfd_link_hash_indirect
11212 || h->root.type == bfd_link_hash_warning)
11213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11214
11215 s_type = h->type;
11216
11217 /* If a plugin symbol is referenced from a non-IR file,
11218 mark the symbol as undefined. Note that the
11219 linker may attach linker created dynamic sections
11220 to the plugin bfd. Symbols defined in linker
11221 created sections are not plugin symbols. */
11222 if ((h->root.non_ir_ref_regular
11223 || h->root.non_ir_ref_dynamic)
11224 && (h->root.type == bfd_link_hash_defined
11225 || h->root.type == bfd_link_hash_defweak)
11226 && (h->root.u.def.section->flags
11227 & SEC_LINKER_CREATED) == 0
11228 && h->root.u.def.section->owner != NULL
11229 && (h->root.u.def.section->owner->flags
11230 & BFD_PLUGIN) != 0)
11231 {
11232 h->root.type = bfd_link_hash_undefined;
11233 h->root.u.undef.abfd = h->root.u.def.section->owner;
11234 }
11235
11236 ps = NULL;
11237 if (h->root.type == bfd_link_hash_defined
11238 || h->root.type == bfd_link_hash_defweak)
11239 ps = &h->root.u.def.section;
11240
11241 sym_name = h->root.root.string;
11242 }
11243 else
11244 {
11245 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11246
11247 s_type = ELF_ST_TYPE (sym->st_info);
11248 ps = &flinfo->sections[r_symndx];
11249 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11250 sym, *ps);
11251 }
11252
11253 if ((s_type == STT_RELC || s_type == STT_SRELC)
11254 && !bfd_link_relocatable (flinfo->info))
11255 {
11256 bfd_vma val;
11257 bfd_vma dot = (rel->r_offset
11258 + o->output_offset + o->output_section->vma);
11259 #ifdef DEBUG
11260 printf ("Encountered a complex symbol!");
11261 printf (" (input_bfd %s, section %s, reloc %ld\n",
11262 bfd_get_filename (input_bfd), o->name,
11263 (long) (rel - internal_relocs));
11264 printf (" symbol: idx %8.8lx, name %s\n",
11265 r_symndx, sym_name);
11266 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11267 (unsigned long) rel->r_info,
11268 (unsigned long) rel->r_offset);
11269 #endif
11270 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11271 isymbuf, locsymcount, s_type == STT_SRELC))
11272 return FALSE;
11273
11274 /* Symbol evaluated OK. Update to absolute value. */
11275 set_symbol_value (input_bfd, isymbuf, locsymcount,
11276 r_symndx, val);
11277 continue;
11278 }
11279
11280 if (action_discarded != -1 && ps != NULL)
11281 {
11282 /* Complain if the definition comes from a
11283 discarded section. */
11284 if ((sec = *ps) != NULL && discarded_section (sec))
11285 {
11286 BFD_ASSERT (r_symndx != STN_UNDEF);
11287 if (action_discarded & COMPLAIN)
11288 (*flinfo->info->callbacks->einfo)
11289 /* xgettext:c-format */
11290 (_("%X`%s' referenced in section `%pA' of %pB: "
11291 "defined in discarded section `%pA' of %pB\n"),
11292 sym_name, o, input_bfd, sec, sec->owner);
11293
11294 /* Try to do the best we can to support buggy old
11295 versions of gcc. Pretend that the symbol is
11296 really defined in the kept linkonce section.
11297 FIXME: This is quite broken. Modifying the
11298 symbol here means we will be changing all later
11299 uses of the symbol, not just in this section. */
11300 if (action_discarded & PRETEND)
11301 {
11302 asection *kept;
11303
11304 kept = _bfd_elf_check_kept_section (sec,
11305 flinfo->info);
11306 if (kept != NULL)
11307 {
11308 *ps = kept;
11309 continue;
11310 }
11311 }
11312 }
11313 }
11314 }
11315
11316 /* Relocate the section by invoking a back end routine.
11317
11318 The back end routine is responsible for adjusting the
11319 section contents as necessary, and (if using Rela relocs
11320 and generating a relocatable output file) adjusting the
11321 reloc addend as necessary.
11322
11323 The back end routine does not have to worry about setting
11324 the reloc address or the reloc symbol index.
11325
11326 The back end routine is given a pointer to the swapped in
11327 internal symbols, and can access the hash table entries
11328 for the external symbols via elf_sym_hashes (input_bfd).
11329
11330 When generating relocatable output, the back end routine
11331 must handle STB_LOCAL/STT_SECTION symbols specially. The
11332 output symbol is going to be a section symbol
11333 corresponding to the output section, which will require
11334 the addend to be adjusted. */
11335
11336 ret = (*relocate_section) (output_bfd, flinfo->info,
11337 input_bfd, o, contents,
11338 internal_relocs,
11339 isymbuf,
11340 flinfo->sections);
11341 if (!ret)
11342 return FALSE;
11343
11344 if (ret == 2
11345 || bfd_link_relocatable (flinfo->info)
11346 || flinfo->info->emitrelocations)
11347 {
11348 Elf_Internal_Rela *irela;
11349 Elf_Internal_Rela *irelaend, *irelamid;
11350 bfd_vma last_offset;
11351 struct elf_link_hash_entry **rel_hash;
11352 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11353 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11354 unsigned int next_erel;
11355 bfd_boolean rela_normal;
11356 struct bfd_elf_section_data *esdi, *esdo;
11357
11358 esdi = elf_section_data (o);
11359 esdo = elf_section_data (o->output_section);
11360 rela_normal = FALSE;
11361
11362 /* Adjust the reloc addresses and symbol indices. */
11363
11364 irela = internal_relocs;
11365 irelaend = irela + o->reloc_count;
11366 rel_hash = esdo->rel.hashes + esdo->rel.count;
11367 /* We start processing the REL relocs, if any. When we reach
11368 IRELAMID in the loop, we switch to the RELA relocs. */
11369 irelamid = irela;
11370 if (esdi->rel.hdr != NULL)
11371 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11372 * bed->s->int_rels_per_ext_rel);
11373 rel_hash_list = rel_hash;
11374 rela_hash_list = NULL;
11375 last_offset = o->output_offset;
11376 if (!bfd_link_relocatable (flinfo->info))
11377 last_offset += o->output_section->vma;
11378 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11379 {
11380 unsigned long r_symndx;
11381 asection *sec;
11382 Elf_Internal_Sym sym;
11383
11384 if (next_erel == bed->s->int_rels_per_ext_rel)
11385 {
11386 rel_hash++;
11387 next_erel = 0;
11388 }
11389
11390 if (irela == irelamid)
11391 {
11392 rel_hash = esdo->rela.hashes + esdo->rela.count;
11393 rela_hash_list = rel_hash;
11394 rela_normal = bed->rela_normal;
11395 }
11396
11397 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11398 flinfo->info, o,
11399 irela->r_offset);
11400 if (irela->r_offset >= (bfd_vma) -2)
11401 {
11402 /* This is a reloc for a deleted entry or somesuch.
11403 Turn it into an R_*_NONE reloc, at the same
11404 offset as the last reloc. elf_eh_frame.c and
11405 bfd_elf_discard_info rely on reloc offsets
11406 being ordered. */
11407 irela->r_offset = last_offset;
11408 irela->r_info = 0;
11409 irela->r_addend = 0;
11410 continue;
11411 }
11412
11413 irela->r_offset += o->output_offset;
11414
11415 /* Relocs in an executable have to be virtual addresses. */
11416 if (!bfd_link_relocatable (flinfo->info))
11417 irela->r_offset += o->output_section->vma;
11418
11419 last_offset = irela->r_offset;
11420
11421 r_symndx = irela->r_info >> r_sym_shift;
11422 if (r_symndx == STN_UNDEF)
11423 continue;
11424
11425 if (r_symndx >= locsymcount
11426 || (elf_bad_symtab (input_bfd)
11427 && flinfo->sections[r_symndx] == NULL))
11428 {
11429 struct elf_link_hash_entry *rh;
11430 unsigned long indx;
11431
11432 /* This is a reloc against a global symbol. We
11433 have not yet output all the local symbols, so
11434 we do not know the symbol index of any global
11435 symbol. We set the rel_hash entry for this
11436 reloc to point to the global hash table entry
11437 for this symbol. The symbol index is then
11438 set at the end of bfd_elf_final_link. */
11439 indx = r_symndx - extsymoff;
11440 rh = elf_sym_hashes (input_bfd)[indx];
11441 while (rh->root.type == bfd_link_hash_indirect
11442 || rh->root.type == bfd_link_hash_warning)
11443 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11444
11445 /* Setting the index to -2 tells
11446 elf_link_output_extsym that this symbol is
11447 used by a reloc. */
11448 BFD_ASSERT (rh->indx < 0);
11449 rh->indx = -2;
11450 *rel_hash = rh;
11451
11452 continue;
11453 }
11454
11455 /* This is a reloc against a local symbol. */
11456
11457 *rel_hash = NULL;
11458 sym = isymbuf[r_symndx];
11459 sec = flinfo->sections[r_symndx];
11460 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11461 {
11462 /* I suppose the backend ought to fill in the
11463 section of any STT_SECTION symbol against a
11464 processor specific section. */
11465 r_symndx = STN_UNDEF;
11466 if (bfd_is_abs_section (sec))
11467 ;
11468 else if (sec == NULL || sec->owner == NULL)
11469 {
11470 bfd_set_error (bfd_error_bad_value);
11471 return FALSE;
11472 }
11473 else
11474 {
11475 asection *osec = sec->output_section;
11476
11477 /* If we have discarded a section, the output
11478 section will be the absolute section. In
11479 case of discarded SEC_MERGE sections, use
11480 the kept section. relocate_section should
11481 have already handled discarded linkonce
11482 sections. */
11483 if (bfd_is_abs_section (osec)
11484 && sec->kept_section != NULL
11485 && sec->kept_section->output_section != NULL)
11486 {
11487 osec = sec->kept_section->output_section;
11488 irela->r_addend -= osec->vma;
11489 }
11490
11491 if (!bfd_is_abs_section (osec))
11492 {
11493 r_symndx = osec->target_index;
11494 if (r_symndx == STN_UNDEF)
11495 {
11496 irela->r_addend += osec->vma;
11497 osec = _bfd_nearby_section (output_bfd, osec,
11498 osec->vma);
11499 irela->r_addend -= osec->vma;
11500 r_symndx = osec->target_index;
11501 }
11502 }
11503 }
11504
11505 /* Adjust the addend according to where the
11506 section winds up in the output section. */
11507 if (rela_normal)
11508 irela->r_addend += sec->output_offset;
11509 }
11510 else
11511 {
11512 if (flinfo->indices[r_symndx] == -1)
11513 {
11514 unsigned long shlink;
11515 const char *name;
11516 asection *osec;
11517 long indx;
11518
11519 if (flinfo->info->strip == strip_all)
11520 {
11521 /* You can't do ld -r -s. */
11522 bfd_set_error (bfd_error_invalid_operation);
11523 return FALSE;
11524 }
11525
11526 /* This symbol was skipped earlier, but
11527 since it is needed by a reloc, we
11528 must output it now. */
11529 shlink = symtab_hdr->sh_link;
11530 name = (bfd_elf_string_from_elf_section
11531 (input_bfd, shlink, sym.st_name));
11532 if (name == NULL)
11533 return FALSE;
11534
11535 osec = sec->output_section;
11536 sym.st_shndx =
11537 _bfd_elf_section_from_bfd_section (output_bfd,
11538 osec);
11539 if (sym.st_shndx == SHN_BAD)
11540 return FALSE;
11541
11542 sym.st_value += sec->output_offset;
11543 if (!bfd_link_relocatable (flinfo->info))
11544 {
11545 sym.st_value += osec->vma;
11546 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11547 {
11548 struct elf_link_hash_table *htab
11549 = elf_hash_table (flinfo->info);
11550
11551 /* STT_TLS symbols are relative to PT_TLS
11552 segment base. */
11553 if (htab->tls_sec != NULL)
11554 sym.st_value -= htab->tls_sec->vma;
11555 else
11556 sym.st_info
11557 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11558 STT_NOTYPE);
11559 }
11560 }
11561
11562 indx = bfd_get_symcount (output_bfd);
11563 ret = elf_link_output_symstrtab (flinfo, name,
11564 &sym, sec,
11565 NULL);
11566 if (ret == 0)
11567 return FALSE;
11568 else if (ret == 1)
11569 flinfo->indices[r_symndx] = indx;
11570 else
11571 abort ();
11572 }
11573
11574 r_symndx = flinfo->indices[r_symndx];
11575 }
11576
11577 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11578 | (irela->r_info & r_type_mask));
11579 }
11580
11581 /* Swap out the relocs. */
11582 input_rel_hdr = esdi->rel.hdr;
11583 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11584 {
11585 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11586 input_rel_hdr,
11587 internal_relocs,
11588 rel_hash_list))
11589 return FALSE;
11590 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11591 * bed->s->int_rels_per_ext_rel);
11592 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11593 }
11594
11595 input_rela_hdr = esdi->rela.hdr;
11596 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11597 {
11598 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11599 input_rela_hdr,
11600 internal_relocs,
11601 rela_hash_list))
11602 return FALSE;
11603 }
11604 }
11605 }
11606
11607 /* Write out the modified section contents. */
11608 if (bed->elf_backend_write_section
11609 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11610 contents))
11611 {
11612 /* Section written out. */
11613 }
11614 else switch (o->sec_info_type)
11615 {
11616 case SEC_INFO_TYPE_STABS:
11617 if (! (_bfd_write_section_stabs
11618 (output_bfd,
11619 &elf_hash_table (flinfo->info)->stab_info,
11620 o, &elf_section_data (o)->sec_info, contents)))
11621 return FALSE;
11622 break;
11623 case SEC_INFO_TYPE_MERGE:
11624 if (! _bfd_write_merged_section (output_bfd, o,
11625 elf_section_data (o)->sec_info))
11626 return FALSE;
11627 break;
11628 case SEC_INFO_TYPE_EH_FRAME:
11629 {
11630 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11631 o, contents))
11632 return FALSE;
11633 }
11634 break;
11635 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11636 {
11637 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11638 flinfo->info,
11639 o, contents))
11640 return FALSE;
11641 }
11642 break;
11643 default:
11644 {
11645 if (! (o->flags & SEC_EXCLUDE))
11646 {
11647 file_ptr offset = (file_ptr) o->output_offset;
11648 bfd_size_type todo = o->size;
11649
11650 offset *= bfd_octets_per_byte (output_bfd, o);
11651
11652 if ((o->flags & SEC_ELF_REVERSE_COPY))
11653 {
11654 /* Reverse-copy input section to output. */
11655 do
11656 {
11657 todo -= address_size;
11658 if (! bfd_set_section_contents (output_bfd,
11659 o->output_section,
11660 contents + todo,
11661 offset,
11662 address_size))
11663 return FALSE;
11664 if (todo == 0)
11665 break;
11666 offset += address_size;
11667 }
11668 while (1);
11669 }
11670 else if (! bfd_set_section_contents (output_bfd,
11671 o->output_section,
11672 contents,
11673 offset, todo))
11674 return FALSE;
11675 }
11676 }
11677 break;
11678 }
11679 }
11680
11681 return TRUE;
11682 }
11683
11684 /* Generate a reloc when linking an ELF file. This is a reloc
11685 requested by the linker, and does not come from any input file. This
11686 is used to build constructor and destructor tables when linking
11687 with -Ur. */
11688
11689 static bfd_boolean
11690 elf_reloc_link_order (bfd *output_bfd,
11691 struct bfd_link_info *info,
11692 asection *output_section,
11693 struct bfd_link_order *link_order)
11694 {
11695 reloc_howto_type *howto;
11696 long indx;
11697 bfd_vma offset;
11698 bfd_vma addend;
11699 struct bfd_elf_section_reloc_data *reldata;
11700 struct elf_link_hash_entry **rel_hash_ptr;
11701 Elf_Internal_Shdr *rel_hdr;
11702 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11703 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11704 bfd_byte *erel;
11705 unsigned int i;
11706 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11707
11708 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11709 if (howto == NULL)
11710 {
11711 bfd_set_error (bfd_error_bad_value);
11712 return FALSE;
11713 }
11714
11715 addend = link_order->u.reloc.p->addend;
11716
11717 if (esdo->rel.hdr)
11718 reldata = &esdo->rel;
11719 else if (esdo->rela.hdr)
11720 reldata = &esdo->rela;
11721 else
11722 {
11723 reldata = NULL;
11724 BFD_ASSERT (0);
11725 }
11726
11727 /* Figure out the symbol index. */
11728 rel_hash_ptr = reldata->hashes + reldata->count;
11729 if (link_order->type == bfd_section_reloc_link_order)
11730 {
11731 indx = link_order->u.reloc.p->u.section->target_index;
11732 BFD_ASSERT (indx != 0);
11733 *rel_hash_ptr = NULL;
11734 }
11735 else
11736 {
11737 struct elf_link_hash_entry *h;
11738
11739 /* Treat a reloc against a defined symbol as though it were
11740 actually against the section. */
11741 h = ((struct elf_link_hash_entry *)
11742 bfd_wrapped_link_hash_lookup (output_bfd, info,
11743 link_order->u.reloc.p->u.name,
11744 FALSE, FALSE, TRUE));
11745 if (h != NULL
11746 && (h->root.type == bfd_link_hash_defined
11747 || h->root.type == bfd_link_hash_defweak))
11748 {
11749 asection *section;
11750
11751 section = h->root.u.def.section;
11752 indx = section->output_section->target_index;
11753 *rel_hash_ptr = NULL;
11754 /* It seems that we ought to add the symbol value to the
11755 addend here, but in practice it has already been added
11756 because it was passed to constructor_callback. */
11757 addend += section->output_section->vma + section->output_offset;
11758 }
11759 else if (h != NULL)
11760 {
11761 /* Setting the index to -2 tells elf_link_output_extsym that
11762 this symbol is used by a reloc. */
11763 h->indx = -2;
11764 *rel_hash_ptr = h;
11765 indx = 0;
11766 }
11767 else
11768 {
11769 (*info->callbacks->unattached_reloc)
11770 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11771 indx = 0;
11772 }
11773 }
11774
11775 /* If this is an inplace reloc, we must write the addend into the
11776 object file. */
11777 if (howto->partial_inplace && addend != 0)
11778 {
11779 bfd_size_type size;
11780 bfd_reloc_status_type rstat;
11781 bfd_byte *buf;
11782 bfd_boolean ok;
11783 const char *sym_name;
11784 bfd_size_type octets;
11785
11786 size = (bfd_size_type) bfd_get_reloc_size (howto);
11787 buf = (bfd_byte *) bfd_zmalloc (size);
11788 if (buf == NULL && size != 0)
11789 return FALSE;
11790 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11791 switch (rstat)
11792 {
11793 case bfd_reloc_ok:
11794 break;
11795
11796 default:
11797 case bfd_reloc_outofrange:
11798 abort ();
11799
11800 case bfd_reloc_overflow:
11801 if (link_order->type == bfd_section_reloc_link_order)
11802 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11803 else
11804 sym_name = link_order->u.reloc.p->u.name;
11805 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11806 howto->name, addend, NULL, NULL,
11807 (bfd_vma) 0);
11808 break;
11809 }
11810
11811 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11812 output_section);
11813 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11814 octets, size);
11815 free (buf);
11816 if (! ok)
11817 return FALSE;
11818 }
11819
11820 /* The address of a reloc is relative to the section in a
11821 relocatable file, and is a virtual address in an executable
11822 file. */
11823 offset = link_order->offset;
11824 if (! bfd_link_relocatable (info))
11825 offset += output_section->vma;
11826
11827 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11828 {
11829 irel[i].r_offset = offset;
11830 irel[i].r_info = 0;
11831 irel[i].r_addend = 0;
11832 }
11833 if (bed->s->arch_size == 32)
11834 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11835 else
11836 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11837
11838 rel_hdr = reldata->hdr;
11839 erel = rel_hdr->contents;
11840 if (rel_hdr->sh_type == SHT_REL)
11841 {
11842 erel += reldata->count * bed->s->sizeof_rel;
11843 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11844 }
11845 else
11846 {
11847 irel[0].r_addend = addend;
11848 erel += reldata->count * bed->s->sizeof_rela;
11849 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11850 }
11851
11852 ++reldata->count;
11853
11854 return TRUE;
11855 }
11856
11857
11858 /* Compare two sections based on the locations of the sections they are
11859 linked to. Used by elf_fixup_link_order. */
11860
11861 static int
11862 compare_link_order (const void *a, const void *b)
11863 {
11864 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11865 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11866 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11867 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11868 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11869 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11870
11871 if (apos < bpos)
11872 return -1;
11873 if (apos > bpos)
11874 return 1;
11875
11876 /* The only way we should get matching LMAs is when the first of two
11877 sections has zero size. */
11878 if (asec->size < bsec->size)
11879 return -1;
11880 if (asec->size > bsec->size)
11881 return 1;
11882
11883 /* If they are both zero size then they almost certainly have the same
11884 VMA and thus are not ordered with respect to each other. Test VMA
11885 anyway, and fall back to id to make the result reproducible across
11886 qsort implementations. */
11887 apos = asec->output_section->vma + asec->output_offset;
11888 bpos = bsec->output_section->vma + bsec->output_offset;
11889 if (apos < bpos)
11890 return -1;
11891 if (apos > bpos)
11892 return 1;
11893
11894 return asec->id - bsec->id;
11895 }
11896
11897
11898 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11899 order as their linked sections. Returns false if this could not be done
11900 because an output section includes both ordered and unordered
11901 sections. Ideally we'd do this in the linker proper. */
11902
11903 static bfd_boolean
11904 elf_fixup_link_order (bfd *abfd, asection *o)
11905 {
11906 size_t seen_linkorder;
11907 size_t seen_other;
11908 size_t n;
11909 struct bfd_link_order *p;
11910 bfd *sub;
11911 struct bfd_link_order **sections;
11912 asection *other_sec, *linkorder_sec;
11913 bfd_vma offset; /* Octets. */
11914
11915 other_sec = NULL;
11916 linkorder_sec = NULL;
11917 seen_other = 0;
11918 seen_linkorder = 0;
11919 for (p = o->map_head.link_order; p != NULL; p = p->next)
11920 {
11921 if (p->type == bfd_indirect_link_order)
11922 {
11923 asection *s = p->u.indirect.section;
11924 sub = s->owner;
11925 if ((s->flags & SEC_LINKER_CREATED) == 0
11926 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11927 && elf_section_data (s) != NULL
11928 && elf_linked_to_section (s) != NULL)
11929 {
11930 seen_linkorder++;
11931 linkorder_sec = s;
11932 }
11933 else
11934 {
11935 seen_other++;
11936 other_sec = s;
11937 }
11938 }
11939 else
11940 seen_other++;
11941
11942 if (seen_other && seen_linkorder)
11943 {
11944 if (other_sec && linkorder_sec)
11945 _bfd_error_handler
11946 /* xgettext:c-format */
11947 (_("%pA has both ordered [`%pA' in %pB] "
11948 "and unordered [`%pA' in %pB] sections"),
11949 o, linkorder_sec, linkorder_sec->owner,
11950 other_sec, other_sec->owner);
11951 else
11952 _bfd_error_handler
11953 (_("%pA has both ordered and unordered sections"), o);
11954 bfd_set_error (bfd_error_bad_value);
11955 return FALSE;
11956 }
11957 }
11958
11959 if (!seen_linkorder)
11960 return TRUE;
11961
11962 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11963 if (sections == NULL)
11964 return FALSE;
11965
11966 seen_linkorder = 0;
11967 for (p = o->map_head.link_order; p != NULL; p = p->next)
11968 sections[seen_linkorder++] = p;
11969
11970 /* Sort the input sections in the order of their linked section. */
11971 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11972
11973 /* Change the offsets of the sections. */
11974 offset = 0;
11975 for (n = 0; n < seen_linkorder; n++)
11976 {
11977 bfd_vma mask;
11978 asection *s = sections[n]->u.indirect.section;
11979 unsigned int opb = bfd_octets_per_byte (abfd, s);
11980
11981 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11982 offset = (offset + ~mask) & mask;
11983 sections[n]->offset = s->output_offset = offset / opb;
11984 offset += sections[n]->size;
11985 }
11986
11987 free (sections);
11988 return TRUE;
11989 }
11990
11991 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11992 Returns TRUE upon success, FALSE otherwise. */
11993
11994 static bfd_boolean
11995 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11996 {
11997 bfd_boolean ret = FALSE;
11998 bfd *implib_bfd;
11999 const struct elf_backend_data *bed;
12000 flagword flags;
12001 enum bfd_architecture arch;
12002 unsigned int mach;
12003 asymbol **sympp = NULL;
12004 long symsize;
12005 long symcount;
12006 long src_count;
12007 elf_symbol_type *osymbuf;
12008 size_t amt;
12009
12010 implib_bfd = info->out_implib_bfd;
12011 bed = get_elf_backend_data (abfd);
12012
12013 if (!bfd_set_format (implib_bfd, bfd_object))
12014 return FALSE;
12015
12016 /* Use flag from executable but make it a relocatable object. */
12017 flags = bfd_get_file_flags (abfd);
12018 flags &= ~HAS_RELOC;
12019 if (!bfd_set_start_address (implib_bfd, 0)
12020 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12021 return FALSE;
12022
12023 /* Copy architecture of output file to import library file. */
12024 arch = bfd_get_arch (abfd);
12025 mach = bfd_get_mach (abfd);
12026 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12027 && (abfd->target_defaulted
12028 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12029 return FALSE;
12030
12031 /* Get symbol table size. */
12032 symsize = bfd_get_symtab_upper_bound (abfd);
12033 if (symsize < 0)
12034 return FALSE;
12035
12036 /* Read in the symbol table. */
12037 sympp = (asymbol **) bfd_malloc (symsize);
12038 if (sympp == NULL)
12039 return FALSE;
12040
12041 symcount = bfd_canonicalize_symtab (abfd, sympp);
12042 if (symcount < 0)
12043 goto free_sym_buf;
12044
12045 /* Allow the BFD backend to copy any private header data it
12046 understands from the output BFD to the import library BFD. */
12047 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12048 goto free_sym_buf;
12049
12050 /* Filter symbols to appear in the import library. */
12051 if (bed->elf_backend_filter_implib_symbols)
12052 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12053 symcount);
12054 else
12055 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12056 if (symcount == 0)
12057 {
12058 bfd_set_error (bfd_error_no_symbols);
12059 _bfd_error_handler (_("%pB: no symbol found for import library"),
12060 implib_bfd);
12061 goto free_sym_buf;
12062 }
12063
12064
12065 /* Make symbols absolute. */
12066 amt = symcount * sizeof (*osymbuf);
12067 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12068 if (osymbuf == NULL)
12069 goto free_sym_buf;
12070
12071 for (src_count = 0; src_count < symcount; src_count++)
12072 {
12073 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12074 sizeof (*osymbuf));
12075 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12076 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12077 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12078 osymbuf[src_count].internal_elf_sym.st_value =
12079 osymbuf[src_count].symbol.value;
12080 sympp[src_count] = &osymbuf[src_count].symbol;
12081 }
12082
12083 bfd_set_symtab (implib_bfd, sympp, symcount);
12084
12085 /* Allow the BFD backend to copy any private data it understands
12086 from the output BFD to the import library BFD. This is done last
12087 to permit the routine to look at the filtered symbol table. */
12088 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12089 goto free_sym_buf;
12090
12091 if (!bfd_close (implib_bfd))
12092 goto free_sym_buf;
12093
12094 ret = TRUE;
12095
12096 free_sym_buf:
12097 free (sympp);
12098 return ret;
12099 }
12100
12101 static void
12102 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12103 {
12104 asection *o;
12105
12106 if (flinfo->symstrtab != NULL)
12107 _bfd_elf_strtab_free (flinfo->symstrtab);
12108 free (flinfo->contents);
12109 free (flinfo->external_relocs);
12110 free (flinfo->internal_relocs);
12111 free (flinfo->external_syms);
12112 free (flinfo->locsym_shndx);
12113 free (flinfo->internal_syms);
12114 free (flinfo->indices);
12115 free (flinfo->sections);
12116 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12117 free (flinfo->symshndxbuf);
12118 for (o = obfd->sections; o != NULL; o = o->next)
12119 {
12120 struct bfd_elf_section_data *esdo = elf_section_data (o);
12121 free (esdo->rel.hashes);
12122 free (esdo->rela.hashes);
12123 }
12124 }
12125
12126 /* Do the final step of an ELF link. */
12127
12128 bfd_boolean
12129 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12130 {
12131 bfd_boolean dynamic;
12132 bfd_boolean emit_relocs;
12133 bfd *dynobj;
12134 struct elf_final_link_info flinfo;
12135 asection *o;
12136 struct bfd_link_order *p;
12137 bfd *sub;
12138 bfd_size_type max_contents_size;
12139 bfd_size_type max_external_reloc_size;
12140 bfd_size_type max_internal_reloc_count;
12141 bfd_size_type max_sym_count;
12142 bfd_size_type max_sym_shndx_count;
12143 Elf_Internal_Sym elfsym;
12144 unsigned int i;
12145 Elf_Internal_Shdr *symtab_hdr;
12146 Elf_Internal_Shdr *symtab_shndx_hdr;
12147 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12148 struct elf_outext_info eoinfo;
12149 bfd_boolean merged;
12150 size_t relativecount = 0;
12151 asection *reldyn = 0;
12152 bfd_size_type amt;
12153 asection *attr_section = NULL;
12154 bfd_vma attr_size = 0;
12155 const char *std_attrs_section;
12156 struct elf_link_hash_table *htab = elf_hash_table (info);
12157 bfd_boolean sections_removed;
12158 bfd_boolean ret;
12159
12160 if (!is_elf_hash_table (htab))
12161 return FALSE;
12162
12163 if (bfd_link_pic (info))
12164 abfd->flags |= DYNAMIC;
12165
12166 dynamic = htab->dynamic_sections_created;
12167 dynobj = htab->dynobj;
12168
12169 emit_relocs = (bfd_link_relocatable (info)
12170 || info->emitrelocations);
12171
12172 memset (&flinfo, 0, sizeof (flinfo));
12173 flinfo.info = info;
12174 flinfo.output_bfd = abfd;
12175 flinfo.symstrtab = _bfd_elf_strtab_init ();
12176 if (flinfo.symstrtab == NULL)
12177 return FALSE;
12178
12179 if (! dynamic)
12180 {
12181 flinfo.hash_sec = NULL;
12182 flinfo.symver_sec = NULL;
12183 }
12184 else
12185 {
12186 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12187 /* Note that dynsym_sec can be NULL (on VMS). */
12188 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12189 /* Note that it is OK if symver_sec is NULL. */
12190 }
12191
12192 if (info->unique_symbol
12193 && !bfd_hash_table_init (&flinfo.local_hash_table,
12194 local_hash_newfunc,
12195 sizeof (struct local_hash_entry)))
12196 return FALSE;
12197
12198 /* The object attributes have been merged. Remove the input
12199 sections from the link, and set the contents of the output
12200 section. */
12201 sections_removed = FALSE;
12202 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12203 for (o = abfd->sections; o != NULL; o = o->next)
12204 {
12205 bfd_boolean remove_section = FALSE;
12206
12207 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12208 || strcmp (o->name, ".gnu.attributes") == 0)
12209 {
12210 for (p = o->map_head.link_order; p != NULL; p = p->next)
12211 {
12212 asection *input_section;
12213
12214 if (p->type != bfd_indirect_link_order)
12215 continue;
12216 input_section = p->u.indirect.section;
12217 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12218 elf_link_input_bfd ignores this section. */
12219 input_section->flags &= ~SEC_HAS_CONTENTS;
12220 }
12221
12222 attr_size = bfd_elf_obj_attr_size (abfd);
12223 bfd_set_section_size (o, attr_size);
12224 /* Skip this section later on. */
12225 o->map_head.link_order = NULL;
12226 if (attr_size)
12227 attr_section = o;
12228 else
12229 remove_section = TRUE;
12230 }
12231 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12232 {
12233 /* Remove empty group section from linker output. */
12234 remove_section = TRUE;
12235 }
12236 if (remove_section)
12237 {
12238 o->flags |= SEC_EXCLUDE;
12239 bfd_section_list_remove (abfd, o);
12240 abfd->section_count--;
12241 sections_removed = TRUE;
12242 }
12243 }
12244 if (sections_removed)
12245 _bfd_fix_excluded_sec_syms (abfd, info);
12246
12247 /* Count up the number of relocations we will output for each output
12248 section, so that we know the sizes of the reloc sections. We
12249 also figure out some maximum sizes. */
12250 max_contents_size = 0;
12251 max_external_reloc_size = 0;
12252 max_internal_reloc_count = 0;
12253 max_sym_count = 0;
12254 max_sym_shndx_count = 0;
12255 merged = FALSE;
12256 for (o = abfd->sections; o != NULL; o = o->next)
12257 {
12258 struct bfd_elf_section_data *esdo = elf_section_data (o);
12259 o->reloc_count = 0;
12260
12261 for (p = o->map_head.link_order; p != NULL; p = p->next)
12262 {
12263 unsigned int reloc_count = 0;
12264 unsigned int additional_reloc_count = 0;
12265 struct bfd_elf_section_data *esdi = NULL;
12266
12267 if (p->type == bfd_section_reloc_link_order
12268 || p->type == bfd_symbol_reloc_link_order)
12269 reloc_count = 1;
12270 else if (p->type == bfd_indirect_link_order)
12271 {
12272 asection *sec;
12273
12274 sec = p->u.indirect.section;
12275
12276 /* Mark all sections which are to be included in the
12277 link. This will normally be every section. We need
12278 to do this so that we can identify any sections which
12279 the linker has decided to not include. */
12280 sec->linker_mark = TRUE;
12281
12282 if (sec->flags & SEC_MERGE)
12283 merged = TRUE;
12284
12285 if (sec->rawsize > max_contents_size)
12286 max_contents_size = sec->rawsize;
12287 if (sec->size > max_contents_size)
12288 max_contents_size = sec->size;
12289
12290 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12291 && (sec->owner->flags & DYNAMIC) == 0)
12292 {
12293 size_t sym_count;
12294
12295 /* We are interested in just local symbols, not all
12296 symbols. */
12297 if (elf_bad_symtab (sec->owner))
12298 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12299 / bed->s->sizeof_sym);
12300 else
12301 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12302
12303 if (sym_count > max_sym_count)
12304 max_sym_count = sym_count;
12305
12306 if (sym_count > max_sym_shndx_count
12307 && elf_symtab_shndx_list (sec->owner) != NULL)
12308 max_sym_shndx_count = sym_count;
12309
12310 if (esdo->this_hdr.sh_type == SHT_REL
12311 || esdo->this_hdr.sh_type == SHT_RELA)
12312 /* Some backends use reloc_count in relocation sections
12313 to count particular types of relocs. Of course,
12314 reloc sections themselves can't have relocations. */
12315 ;
12316 else if (emit_relocs)
12317 {
12318 reloc_count = sec->reloc_count;
12319 if (bed->elf_backend_count_additional_relocs)
12320 {
12321 int c;
12322 c = (*bed->elf_backend_count_additional_relocs) (sec);
12323 additional_reloc_count += c;
12324 }
12325 }
12326 else if (bed->elf_backend_count_relocs)
12327 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12328
12329 esdi = elf_section_data (sec);
12330
12331 if ((sec->flags & SEC_RELOC) != 0)
12332 {
12333 size_t ext_size = 0;
12334
12335 if (esdi->rel.hdr != NULL)
12336 ext_size = esdi->rel.hdr->sh_size;
12337 if (esdi->rela.hdr != NULL)
12338 ext_size += esdi->rela.hdr->sh_size;
12339
12340 if (ext_size > max_external_reloc_size)
12341 max_external_reloc_size = ext_size;
12342 if (sec->reloc_count > max_internal_reloc_count)
12343 max_internal_reloc_count = sec->reloc_count;
12344 }
12345 }
12346 }
12347
12348 if (reloc_count == 0)
12349 continue;
12350
12351 reloc_count += additional_reloc_count;
12352 o->reloc_count += reloc_count;
12353
12354 if (p->type == bfd_indirect_link_order && emit_relocs)
12355 {
12356 if (esdi->rel.hdr)
12357 {
12358 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12359 esdo->rel.count += additional_reloc_count;
12360 }
12361 if (esdi->rela.hdr)
12362 {
12363 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12364 esdo->rela.count += additional_reloc_count;
12365 }
12366 }
12367 else
12368 {
12369 if (o->use_rela_p)
12370 esdo->rela.count += reloc_count;
12371 else
12372 esdo->rel.count += reloc_count;
12373 }
12374 }
12375
12376 if (o->reloc_count > 0)
12377 o->flags |= SEC_RELOC;
12378 else
12379 {
12380 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12381 set it (this is probably a bug) and if it is set
12382 assign_section_numbers will create a reloc section. */
12383 o->flags &=~ SEC_RELOC;
12384 }
12385
12386 /* If the SEC_ALLOC flag is not set, force the section VMA to
12387 zero. This is done in elf_fake_sections as well, but forcing
12388 the VMA to 0 here will ensure that relocs against these
12389 sections are handled correctly. */
12390 if ((o->flags & SEC_ALLOC) == 0
12391 && ! o->user_set_vma)
12392 o->vma = 0;
12393 }
12394
12395 if (! bfd_link_relocatable (info) && merged)
12396 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12397
12398 /* Figure out the file positions for everything but the symbol table
12399 and the relocs. We set symcount to force assign_section_numbers
12400 to create a symbol table. */
12401 abfd->symcount = info->strip != strip_all || emit_relocs;
12402 BFD_ASSERT (! abfd->output_has_begun);
12403 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12404 goto error_return;
12405
12406 /* Set sizes, and assign file positions for reloc sections. */
12407 for (o = abfd->sections; o != NULL; o = o->next)
12408 {
12409 struct bfd_elf_section_data *esdo = elf_section_data (o);
12410 if ((o->flags & SEC_RELOC) != 0)
12411 {
12412 if (esdo->rel.hdr
12413 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12414 goto error_return;
12415
12416 if (esdo->rela.hdr
12417 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12418 goto error_return;
12419 }
12420
12421 /* _bfd_elf_compute_section_file_positions makes temporary use
12422 of target_index. Reset it. */
12423 o->target_index = 0;
12424
12425 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12426 to count upwards while actually outputting the relocations. */
12427 esdo->rel.count = 0;
12428 esdo->rela.count = 0;
12429
12430 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12431 && !bfd_section_is_ctf (o))
12432 {
12433 /* Cache the section contents so that they can be compressed
12434 later. Use bfd_malloc since it will be freed by
12435 bfd_compress_section_contents. */
12436 unsigned char *contents = esdo->this_hdr.contents;
12437 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12438 abort ();
12439 contents
12440 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12441 if (contents == NULL)
12442 goto error_return;
12443 esdo->this_hdr.contents = contents;
12444 }
12445 }
12446
12447 /* We have now assigned file positions for all the sections except .symtab,
12448 .strtab, and non-loaded reloc and compressed debugging sections. We start
12449 the .symtab section at the current file position, and write directly to it.
12450 We build the .strtab section in memory. */
12451 abfd->symcount = 0;
12452 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12453 /* sh_name is set in prep_headers. */
12454 symtab_hdr->sh_type = SHT_SYMTAB;
12455 /* sh_flags, sh_addr and sh_size all start off zero. */
12456 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12457 /* sh_link is set in assign_section_numbers. */
12458 /* sh_info is set below. */
12459 /* sh_offset is set just below. */
12460 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12461
12462 if (max_sym_count < 20)
12463 max_sym_count = 20;
12464 htab->strtabsize = max_sym_count;
12465 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12466 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12467 if (htab->strtab == NULL)
12468 goto error_return;
12469 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12470 flinfo.symshndxbuf
12471 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12472 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12473
12474 if (info->strip != strip_all || emit_relocs)
12475 {
12476 bfd_boolean name_local_sections;
12477 const char *name;
12478
12479 file_ptr off = elf_next_file_pos (abfd);
12480
12481 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12482
12483 /* Note that at this point elf_next_file_pos (abfd) is
12484 incorrect. We do not yet know the size of the .symtab section.
12485 We correct next_file_pos below, after we do know the size. */
12486
12487 /* Start writing out the symbol table. The first symbol is always a
12488 dummy symbol. */
12489 elfsym.st_value = 0;
12490 elfsym.st_size = 0;
12491 elfsym.st_info = 0;
12492 elfsym.st_other = 0;
12493 elfsym.st_shndx = SHN_UNDEF;
12494 elfsym.st_target_internal = 0;
12495 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12496 bfd_und_section_ptr, NULL) != 1)
12497 goto error_return;
12498
12499 /* Output a symbol for each section. We output these even if we are
12500 discarding local symbols, since they are used for relocs. These
12501 symbols usually have no names. We store the index of each one in
12502 the index field of the section, so that we can find it again when
12503 outputting relocs. */
12504
12505 name_local_sections
12506 = (bed->elf_backend_name_local_section_symbols
12507 && bed->elf_backend_name_local_section_symbols (abfd));
12508
12509 name = NULL;
12510 elfsym.st_size = 0;
12511 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12512 elfsym.st_other = 0;
12513 elfsym.st_value = 0;
12514 elfsym.st_target_internal = 0;
12515 for (i = 1; i < elf_numsections (abfd); i++)
12516 {
12517 o = bfd_section_from_elf_index (abfd, i);
12518 if (o != NULL)
12519 {
12520 o->target_index = bfd_get_symcount (abfd);
12521 elfsym.st_shndx = i;
12522 if (!bfd_link_relocatable (info))
12523 elfsym.st_value = o->vma;
12524 if (name_local_sections)
12525 name = o->name;
12526 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12527 NULL) != 1)
12528 goto error_return;
12529 }
12530 }
12531 }
12532
12533 /* On some targets like Irix 5 the symbol split between local and global
12534 ones recorded in the sh_info field needs to be done between section
12535 and all other symbols. */
12536 if (bed->elf_backend_elfsym_local_is_section
12537 && bed->elf_backend_elfsym_local_is_section (abfd))
12538 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12539
12540 /* Allocate some memory to hold information read in from the input
12541 files. */
12542 if (max_contents_size != 0)
12543 {
12544 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12545 if (flinfo.contents == NULL)
12546 goto error_return;
12547 }
12548
12549 if (max_external_reloc_size != 0)
12550 {
12551 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12552 if (flinfo.external_relocs == NULL)
12553 goto error_return;
12554 }
12555
12556 if (max_internal_reloc_count != 0)
12557 {
12558 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12559 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12560 if (flinfo.internal_relocs == NULL)
12561 goto error_return;
12562 }
12563
12564 if (max_sym_count != 0)
12565 {
12566 amt = max_sym_count * bed->s->sizeof_sym;
12567 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12568 if (flinfo.external_syms == NULL)
12569 goto error_return;
12570
12571 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12572 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12573 if (flinfo.internal_syms == NULL)
12574 goto error_return;
12575
12576 amt = max_sym_count * sizeof (long);
12577 flinfo.indices = (long int *) bfd_malloc (amt);
12578 if (flinfo.indices == NULL)
12579 goto error_return;
12580
12581 amt = max_sym_count * sizeof (asection *);
12582 flinfo.sections = (asection **) bfd_malloc (amt);
12583 if (flinfo.sections == NULL)
12584 goto error_return;
12585 }
12586
12587 if (max_sym_shndx_count != 0)
12588 {
12589 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12590 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12591 if (flinfo.locsym_shndx == NULL)
12592 goto error_return;
12593 }
12594
12595 if (htab->tls_sec)
12596 {
12597 bfd_vma base, end = 0; /* Both bytes. */
12598 asection *sec;
12599
12600 for (sec = htab->tls_sec;
12601 sec && (sec->flags & SEC_THREAD_LOCAL);
12602 sec = sec->next)
12603 {
12604 bfd_size_type size = sec->size;
12605 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12606
12607 if (size == 0
12608 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12609 {
12610 struct bfd_link_order *ord = sec->map_tail.link_order;
12611
12612 if (ord != NULL)
12613 size = ord->offset * opb + ord->size;
12614 }
12615 end = sec->vma + size / opb;
12616 }
12617 base = htab->tls_sec->vma;
12618 /* Only align end of TLS section if static TLS doesn't have special
12619 alignment requirements. */
12620 if (bed->static_tls_alignment == 1)
12621 end = align_power (end, htab->tls_sec->alignment_power);
12622 htab->tls_size = end - base;
12623 }
12624
12625 /* Reorder SHF_LINK_ORDER sections. */
12626 for (o = abfd->sections; o != NULL; o = o->next)
12627 {
12628 if (!elf_fixup_link_order (abfd, o))
12629 return FALSE;
12630 }
12631
12632 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12633 return FALSE;
12634
12635 /* Since ELF permits relocations to be against local symbols, we
12636 must have the local symbols available when we do the relocations.
12637 Since we would rather only read the local symbols once, and we
12638 would rather not keep them in memory, we handle all the
12639 relocations for a single input file at the same time.
12640
12641 Unfortunately, there is no way to know the total number of local
12642 symbols until we have seen all of them, and the local symbol
12643 indices precede the global symbol indices. This means that when
12644 we are generating relocatable output, and we see a reloc against
12645 a global symbol, we can not know the symbol index until we have
12646 finished examining all the local symbols to see which ones we are
12647 going to output. To deal with this, we keep the relocations in
12648 memory, and don't output them until the end of the link. This is
12649 an unfortunate waste of memory, but I don't see a good way around
12650 it. Fortunately, it only happens when performing a relocatable
12651 link, which is not the common case. FIXME: If keep_memory is set
12652 we could write the relocs out and then read them again; I don't
12653 know how bad the memory loss will be. */
12654
12655 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12656 sub->output_has_begun = FALSE;
12657 for (o = abfd->sections; o != NULL; o = o->next)
12658 {
12659 for (p = o->map_head.link_order; p != NULL; p = p->next)
12660 {
12661 if (p->type == bfd_indirect_link_order
12662 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12663 == bfd_target_elf_flavour)
12664 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12665 {
12666 if (! sub->output_has_begun)
12667 {
12668 if (! elf_link_input_bfd (&flinfo, sub))
12669 goto error_return;
12670 sub->output_has_begun = TRUE;
12671 }
12672 }
12673 else if (p->type == bfd_section_reloc_link_order
12674 || p->type == bfd_symbol_reloc_link_order)
12675 {
12676 if (! elf_reloc_link_order (abfd, info, o, p))
12677 goto error_return;
12678 }
12679 else
12680 {
12681 if (! _bfd_default_link_order (abfd, info, o, p))
12682 {
12683 if (p->type == bfd_indirect_link_order
12684 && (bfd_get_flavour (sub)
12685 == bfd_target_elf_flavour)
12686 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12687 != bed->s->elfclass))
12688 {
12689 const char *iclass, *oclass;
12690
12691 switch (bed->s->elfclass)
12692 {
12693 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12694 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12695 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12696 default: abort ();
12697 }
12698
12699 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12700 {
12701 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12702 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12703 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12704 default: abort ();
12705 }
12706
12707 bfd_set_error (bfd_error_wrong_format);
12708 _bfd_error_handler
12709 /* xgettext:c-format */
12710 (_("%pB: file class %s incompatible with %s"),
12711 sub, iclass, oclass);
12712 }
12713
12714 goto error_return;
12715 }
12716 }
12717 }
12718 }
12719
12720 /* Free symbol buffer if needed. */
12721 if (!info->reduce_memory_overheads)
12722 {
12723 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12724 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12725 {
12726 free (elf_tdata (sub)->symbuf);
12727 elf_tdata (sub)->symbuf = NULL;
12728 }
12729 }
12730
12731 ret = TRUE;
12732
12733 /* Output any global symbols that got converted to local in a
12734 version script or due to symbol visibility. We do this in a
12735 separate step since ELF requires all local symbols to appear
12736 prior to any global symbols. FIXME: We should only do this if
12737 some global symbols were, in fact, converted to become local.
12738 FIXME: Will this work correctly with the Irix 5 linker? */
12739 eoinfo.failed = FALSE;
12740 eoinfo.flinfo = &flinfo;
12741 eoinfo.localsyms = TRUE;
12742 eoinfo.file_sym_done = FALSE;
12743 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12744 if (eoinfo.failed)
12745 {
12746 ret = FALSE;
12747 goto return_local_hash_table;
12748 }
12749
12750 /* If backend needs to output some local symbols not present in the hash
12751 table, do it now. */
12752 if (bed->elf_backend_output_arch_local_syms
12753 && (info->strip != strip_all || emit_relocs))
12754 {
12755 typedef int (*out_sym_func)
12756 (void *, const char *, Elf_Internal_Sym *, asection *,
12757 struct elf_link_hash_entry *);
12758
12759 if (! ((*bed->elf_backend_output_arch_local_syms)
12760 (abfd, info, &flinfo,
12761 (out_sym_func) elf_link_output_symstrtab)))
12762 {
12763 ret = FALSE;
12764 goto return_local_hash_table;
12765 }
12766 }
12767
12768 /* That wrote out all the local symbols. Finish up the symbol table
12769 with the global symbols. Even if we want to strip everything we
12770 can, we still need to deal with those global symbols that got
12771 converted to local in a version script. */
12772
12773 /* The sh_info field records the index of the first non local symbol. */
12774 if (!symtab_hdr->sh_info)
12775 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12776
12777 if (dynamic
12778 && htab->dynsym != NULL
12779 && htab->dynsym->output_section != bfd_abs_section_ptr)
12780 {
12781 Elf_Internal_Sym sym;
12782 bfd_byte *dynsym = htab->dynsym->contents;
12783
12784 o = htab->dynsym->output_section;
12785 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12786
12787 /* Write out the section symbols for the output sections. */
12788 if (bfd_link_pic (info)
12789 || htab->is_relocatable_executable)
12790 {
12791 asection *s;
12792
12793 sym.st_size = 0;
12794 sym.st_name = 0;
12795 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12796 sym.st_other = 0;
12797 sym.st_target_internal = 0;
12798
12799 for (s = abfd->sections; s != NULL; s = s->next)
12800 {
12801 int indx;
12802 bfd_byte *dest;
12803 long dynindx;
12804
12805 dynindx = elf_section_data (s)->dynindx;
12806 if (dynindx <= 0)
12807 continue;
12808 indx = elf_section_data (s)->this_idx;
12809 BFD_ASSERT (indx > 0);
12810 sym.st_shndx = indx;
12811 if (! check_dynsym (abfd, &sym))
12812 {
12813 ret = FALSE;
12814 goto return_local_hash_table;
12815 }
12816 sym.st_value = s->vma;
12817 dest = dynsym + dynindx * bed->s->sizeof_sym;
12818
12819 /* Inform the linker of the addition of this symbol. */
12820
12821 if (info->callbacks->ctf_new_dynsym)
12822 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12823
12824 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12825 }
12826 }
12827
12828 /* Write out the local dynsyms. */
12829 if (htab->dynlocal)
12830 {
12831 struct elf_link_local_dynamic_entry *e;
12832 for (e = htab->dynlocal; e ; e = e->next)
12833 {
12834 asection *s;
12835 bfd_byte *dest;
12836
12837 /* Copy the internal symbol and turn off visibility.
12838 Note that we saved a word of storage and overwrote
12839 the original st_name with the dynstr_index. */
12840 sym = e->isym;
12841 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12842 sym.st_shndx = SHN_UNDEF;
12843
12844 s = bfd_section_from_elf_index (e->input_bfd,
12845 e->isym.st_shndx);
12846 if (s != NULL
12847 && s->output_section != NULL
12848 && elf_section_data (s->output_section) != NULL)
12849 {
12850 sym.st_shndx =
12851 elf_section_data (s->output_section)->this_idx;
12852 if (! check_dynsym (abfd, &sym))
12853 {
12854 ret = FALSE;
12855 goto return_local_hash_table;
12856 }
12857 sym.st_value = (s->output_section->vma
12858 + s->output_offset
12859 + e->isym.st_value);
12860 }
12861
12862 /* Inform the linker of the addition of this symbol. */
12863
12864 if (info->callbacks->ctf_new_dynsym)
12865 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12866
12867 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12868 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12869 }
12870 }
12871 }
12872
12873 /* We get the global symbols from the hash table. */
12874 eoinfo.failed = FALSE;
12875 eoinfo.localsyms = FALSE;
12876 eoinfo.flinfo = &flinfo;
12877 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12878 if (eoinfo.failed)
12879 {
12880 ret = FALSE;
12881 goto return_local_hash_table;
12882 }
12883
12884 /* If backend needs to output some symbols not present in the hash
12885 table, do it now. */
12886 if (bed->elf_backend_output_arch_syms
12887 && (info->strip != strip_all || emit_relocs))
12888 {
12889 typedef int (*out_sym_func)
12890 (void *, const char *, Elf_Internal_Sym *, asection *,
12891 struct elf_link_hash_entry *);
12892
12893 if (! ((*bed->elf_backend_output_arch_syms)
12894 (abfd, info, &flinfo,
12895 (out_sym_func) elf_link_output_symstrtab)))
12896 {
12897 ret = FALSE;
12898 goto return_local_hash_table;
12899 }
12900 }
12901
12902 /* Finalize the .strtab section. */
12903 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12904
12905 /* Swap out the .strtab section. */
12906 if (!elf_link_swap_symbols_out (&flinfo))
12907 {
12908 ret = FALSE;
12909 goto return_local_hash_table;
12910 }
12911
12912 /* Now we know the size of the symtab section. */
12913 if (bfd_get_symcount (abfd) > 0)
12914 {
12915 /* Finish up and write out the symbol string table (.strtab)
12916 section. */
12917 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12918 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12919
12920 if (elf_symtab_shndx_list (abfd))
12921 {
12922 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12923
12924 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12925 {
12926 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12927 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12928 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12929 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12930 symtab_shndx_hdr->sh_size = amt;
12931
12932 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12933 off, TRUE);
12934
12935 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12936 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12937 {
12938 ret = FALSE;
12939 goto return_local_hash_table;
12940 }
12941 }
12942 }
12943
12944 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12945 /* sh_name was set in prep_headers. */
12946 symstrtab_hdr->sh_type = SHT_STRTAB;
12947 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12948 symstrtab_hdr->sh_addr = 0;
12949 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12950 symstrtab_hdr->sh_entsize = 0;
12951 symstrtab_hdr->sh_link = 0;
12952 symstrtab_hdr->sh_info = 0;
12953 /* sh_offset is set just below. */
12954 symstrtab_hdr->sh_addralign = 1;
12955
12956 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12957 off, TRUE);
12958 elf_next_file_pos (abfd) = off;
12959
12960 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12961 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12962 {
12963 ret = FALSE;
12964 goto return_local_hash_table;
12965 }
12966 }
12967
12968 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12969 {
12970 _bfd_error_handler (_("%pB: failed to generate import library"),
12971 info->out_implib_bfd);
12972 ret = FALSE;
12973 goto return_local_hash_table;
12974 }
12975
12976 /* Adjust the relocs to have the correct symbol indices. */
12977 for (o = abfd->sections; o != NULL; o = o->next)
12978 {
12979 struct bfd_elf_section_data *esdo = elf_section_data (o);
12980 bfd_boolean sort;
12981
12982 if ((o->flags & SEC_RELOC) == 0)
12983 continue;
12984
12985 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12986 if (esdo->rel.hdr != NULL
12987 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12988 {
12989 ret = FALSE;
12990 goto return_local_hash_table;
12991 }
12992 if (esdo->rela.hdr != NULL
12993 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12994 {
12995 ret = FALSE;
12996 goto return_local_hash_table;
12997 }
12998
12999 /* Set the reloc_count field to 0 to prevent write_relocs from
13000 trying to swap the relocs out itself. */
13001 o->reloc_count = 0;
13002 }
13003
13004 if (dynamic && info->combreloc && dynobj != NULL)
13005 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13006
13007 /* If we are linking against a dynamic object, or generating a
13008 shared library, finish up the dynamic linking information. */
13009 if (dynamic)
13010 {
13011 bfd_byte *dyncon, *dynconend;
13012
13013 /* Fix up .dynamic entries. */
13014 o = bfd_get_linker_section (dynobj, ".dynamic");
13015 BFD_ASSERT (o != NULL);
13016
13017 dyncon = o->contents;
13018 dynconend = o->contents + o->size;
13019 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13020 {
13021 Elf_Internal_Dyn dyn;
13022 const char *name;
13023 unsigned int type;
13024 bfd_size_type sh_size;
13025 bfd_vma sh_addr;
13026
13027 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13028
13029 switch (dyn.d_tag)
13030 {
13031 default:
13032 continue;
13033 case DT_NULL:
13034 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13035 {
13036 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13037 {
13038 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13039 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13040 default: continue;
13041 }
13042 dyn.d_un.d_val = relativecount;
13043 relativecount = 0;
13044 break;
13045 }
13046 continue;
13047
13048 case DT_INIT:
13049 name = info->init_function;
13050 goto get_sym;
13051 case DT_FINI:
13052 name = info->fini_function;
13053 get_sym:
13054 {
13055 struct elf_link_hash_entry *h;
13056
13057 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
13058 if (h != NULL
13059 && (h->root.type == bfd_link_hash_defined
13060 || h->root.type == bfd_link_hash_defweak))
13061 {
13062 dyn.d_un.d_ptr = h->root.u.def.value;
13063 o = h->root.u.def.section;
13064 if (o->output_section != NULL)
13065 dyn.d_un.d_ptr += (o->output_section->vma
13066 + o->output_offset);
13067 else
13068 {
13069 /* The symbol is imported from another shared
13070 library and does not apply to this one. */
13071 dyn.d_un.d_ptr = 0;
13072 }
13073 break;
13074 }
13075 }
13076 continue;
13077
13078 case DT_PREINIT_ARRAYSZ:
13079 name = ".preinit_array";
13080 goto get_out_size;
13081 case DT_INIT_ARRAYSZ:
13082 name = ".init_array";
13083 goto get_out_size;
13084 case DT_FINI_ARRAYSZ:
13085 name = ".fini_array";
13086 get_out_size:
13087 o = bfd_get_section_by_name (abfd, name);
13088 if (o == NULL)
13089 {
13090 _bfd_error_handler
13091 (_("could not find section %s"), name);
13092 goto error_return;
13093 }
13094 if (o->size == 0)
13095 _bfd_error_handler
13096 (_("warning: %s section has zero size"), name);
13097 dyn.d_un.d_val = o->size;
13098 break;
13099
13100 case DT_PREINIT_ARRAY:
13101 name = ".preinit_array";
13102 goto get_out_vma;
13103 case DT_INIT_ARRAY:
13104 name = ".init_array";
13105 goto get_out_vma;
13106 case DT_FINI_ARRAY:
13107 name = ".fini_array";
13108 get_out_vma:
13109 o = bfd_get_section_by_name (abfd, name);
13110 goto do_vma;
13111
13112 case DT_HASH:
13113 name = ".hash";
13114 goto get_vma;
13115 case DT_GNU_HASH:
13116 name = ".gnu.hash";
13117 goto get_vma;
13118 case DT_STRTAB:
13119 name = ".dynstr";
13120 goto get_vma;
13121 case DT_SYMTAB:
13122 name = ".dynsym";
13123 goto get_vma;
13124 case DT_VERDEF:
13125 name = ".gnu.version_d";
13126 goto get_vma;
13127 case DT_VERNEED:
13128 name = ".gnu.version_r";
13129 goto get_vma;
13130 case DT_VERSYM:
13131 name = ".gnu.version";
13132 get_vma:
13133 o = bfd_get_linker_section (dynobj, name);
13134 do_vma:
13135 if (o == NULL || bfd_is_abs_section (o->output_section))
13136 {
13137 _bfd_error_handler
13138 (_("could not find section %s"), name);
13139 goto error_return;
13140 }
13141 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13142 {
13143 _bfd_error_handler
13144 (_("warning: section '%s' is being made into a note"), name);
13145 bfd_set_error (bfd_error_nonrepresentable_section);
13146 goto error_return;
13147 }
13148 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13149 break;
13150
13151 case DT_REL:
13152 case DT_RELA:
13153 case DT_RELSZ:
13154 case DT_RELASZ:
13155 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13156 type = SHT_REL;
13157 else
13158 type = SHT_RELA;
13159 sh_size = 0;
13160 sh_addr = 0;
13161 for (i = 1; i < elf_numsections (abfd); i++)
13162 {
13163 Elf_Internal_Shdr *hdr;
13164
13165 hdr = elf_elfsections (abfd)[i];
13166 if (hdr->sh_type == type
13167 && (hdr->sh_flags & SHF_ALLOC) != 0)
13168 {
13169 sh_size += hdr->sh_size;
13170 if (sh_addr == 0
13171 || sh_addr > hdr->sh_addr)
13172 sh_addr = hdr->sh_addr;
13173 }
13174 }
13175
13176 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13177 {
13178 unsigned int opb = bfd_octets_per_byte (abfd, o);
13179
13180 /* Don't count procedure linkage table relocs in the
13181 overall reloc count. */
13182 sh_size -= htab->srelplt->size;
13183 if (sh_size == 0)
13184 /* If the size is zero, make the address zero too.
13185 This is to avoid a glibc bug. If the backend
13186 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13187 zero, then we'll put DT_RELA at the end of
13188 DT_JMPREL. glibc will interpret the end of
13189 DT_RELA matching the end of DT_JMPREL as the
13190 case where DT_RELA includes DT_JMPREL, and for
13191 LD_BIND_NOW will decide that processing DT_RELA
13192 will process the PLT relocs too. Net result:
13193 No PLT relocs applied. */
13194 sh_addr = 0;
13195
13196 /* If .rela.plt is the first .rela section, exclude
13197 it from DT_RELA. */
13198 else if (sh_addr == (htab->srelplt->output_section->vma
13199 + htab->srelplt->output_offset) * opb)
13200 sh_addr += htab->srelplt->size;
13201 }
13202
13203 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13204 dyn.d_un.d_val = sh_size;
13205 else
13206 dyn.d_un.d_ptr = sh_addr;
13207 break;
13208 }
13209 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13210 }
13211 }
13212
13213 /* If we have created any dynamic sections, then output them. */
13214 if (dynobj != NULL)
13215 {
13216 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13217 goto error_return;
13218
13219 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13220 if (bfd_link_textrel_check (info)
13221 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13222 {
13223 bfd_byte *dyncon, *dynconend;
13224
13225 dyncon = o->contents;
13226 dynconend = o->contents + o->size;
13227 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13228 {
13229 Elf_Internal_Dyn dyn;
13230
13231 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13232
13233 if (dyn.d_tag == DT_TEXTREL)
13234 {
13235 if (info->textrel_check == textrel_check_error)
13236 info->callbacks->einfo
13237 (_("%P%X: read-only segment has dynamic relocations\n"));
13238 else if (bfd_link_dll (info))
13239 info->callbacks->einfo
13240 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13241 else
13242 info->callbacks->einfo
13243 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13244 break;
13245 }
13246 }
13247 }
13248
13249 for (o = dynobj->sections; o != NULL; o = o->next)
13250 {
13251 if ((o->flags & SEC_HAS_CONTENTS) == 0
13252 || o->size == 0
13253 || o->output_section == bfd_abs_section_ptr)
13254 continue;
13255 if ((o->flags & SEC_LINKER_CREATED) == 0)
13256 {
13257 /* At this point, we are only interested in sections
13258 created by _bfd_elf_link_create_dynamic_sections. */
13259 continue;
13260 }
13261 if (htab->stab_info.stabstr == o)
13262 continue;
13263 if (htab->eh_info.hdr_sec == o)
13264 continue;
13265 if (strcmp (o->name, ".dynstr") != 0)
13266 {
13267 bfd_size_type octets = ((file_ptr) o->output_offset
13268 * bfd_octets_per_byte (abfd, o));
13269 if (!bfd_set_section_contents (abfd, o->output_section,
13270 o->contents, octets, o->size))
13271 goto error_return;
13272 }
13273 else
13274 {
13275 /* The contents of the .dynstr section are actually in a
13276 stringtab. */
13277 file_ptr off;
13278
13279 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13280 if (bfd_seek (abfd, off, SEEK_SET) != 0
13281 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13282 goto error_return;
13283 }
13284 }
13285 }
13286
13287 if (!info->resolve_section_groups)
13288 {
13289 bfd_boolean failed = FALSE;
13290
13291 BFD_ASSERT (bfd_link_relocatable (info));
13292 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13293 if (failed)
13294 goto error_return;
13295 }
13296
13297 /* If we have optimized stabs strings, output them. */
13298 if (htab->stab_info.stabstr != NULL)
13299 {
13300 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13301 goto error_return;
13302 }
13303
13304 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13305 goto error_return;
13306
13307 if (info->callbacks->emit_ctf)
13308 info->callbacks->emit_ctf ();
13309
13310 elf_final_link_free (abfd, &flinfo);
13311
13312 if (attr_section)
13313 {
13314 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13315 if (contents == NULL)
13316 {
13317 /* Bail out and fail. */
13318 ret = FALSE;
13319 goto return_local_hash_table;
13320 }
13321 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13322 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13323 free (contents);
13324 }
13325
13326 return_local_hash_table:
13327 if (info->unique_symbol)
13328 bfd_hash_table_free (&flinfo.local_hash_table);
13329 return ret;
13330
13331 error_return:
13332 elf_final_link_free (abfd, &flinfo);
13333 ret = FALSE;
13334 goto return_local_hash_table;
13335 }
13336 \f
13337 /* Initialize COOKIE for input bfd ABFD. */
13338
13339 static bfd_boolean
13340 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13341 struct bfd_link_info *info, bfd *abfd)
13342 {
13343 Elf_Internal_Shdr *symtab_hdr;
13344 const struct elf_backend_data *bed;
13345
13346 bed = get_elf_backend_data (abfd);
13347 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13348
13349 cookie->abfd = abfd;
13350 cookie->sym_hashes = elf_sym_hashes (abfd);
13351 cookie->bad_symtab = elf_bad_symtab (abfd);
13352 if (cookie->bad_symtab)
13353 {
13354 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13355 cookie->extsymoff = 0;
13356 }
13357 else
13358 {
13359 cookie->locsymcount = symtab_hdr->sh_info;
13360 cookie->extsymoff = symtab_hdr->sh_info;
13361 }
13362
13363 if (bed->s->arch_size == 32)
13364 cookie->r_sym_shift = 8;
13365 else
13366 cookie->r_sym_shift = 32;
13367
13368 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13369 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13370 {
13371 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13372 cookie->locsymcount, 0,
13373 NULL, NULL, NULL);
13374 if (cookie->locsyms == NULL)
13375 {
13376 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13377 return FALSE;
13378 }
13379 if (info->keep_memory)
13380 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13381 }
13382 return TRUE;
13383 }
13384
13385 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13386
13387 static void
13388 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13389 {
13390 Elf_Internal_Shdr *symtab_hdr;
13391
13392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13393 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13394 free (cookie->locsyms);
13395 }
13396
13397 /* Initialize the relocation information in COOKIE for input section SEC
13398 of input bfd ABFD. */
13399
13400 static bfd_boolean
13401 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13402 struct bfd_link_info *info, bfd *abfd,
13403 asection *sec)
13404 {
13405 if (sec->reloc_count == 0)
13406 {
13407 cookie->rels = NULL;
13408 cookie->relend = NULL;
13409 }
13410 else
13411 {
13412 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13413 info->keep_memory);
13414 if (cookie->rels == NULL)
13415 return FALSE;
13416 cookie->rel = cookie->rels;
13417 cookie->relend = cookie->rels + sec->reloc_count;
13418 }
13419 cookie->rel = cookie->rels;
13420 return TRUE;
13421 }
13422
13423 /* Free the memory allocated by init_reloc_cookie_rels,
13424 if appropriate. */
13425
13426 static void
13427 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13428 asection *sec)
13429 {
13430 if (elf_section_data (sec)->relocs != cookie->rels)
13431 free (cookie->rels);
13432 }
13433
13434 /* Initialize the whole of COOKIE for input section SEC. */
13435
13436 static bfd_boolean
13437 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13438 struct bfd_link_info *info,
13439 asection *sec)
13440 {
13441 if (!init_reloc_cookie (cookie, info, sec->owner))
13442 goto error1;
13443 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13444 goto error2;
13445 return TRUE;
13446
13447 error2:
13448 fini_reloc_cookie (cookie, sec->owner);
13449 error1:
13450 return FALSE;
13451 }
13452
13453 /* Free the memory allocated by init_reloc_cookie_for_section,
13454 if appropriate. */
13455
13456 static void
13457 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13458 asection *sec)
13459 {
13460 fini_reloc_cookie_rels (cookie, sec);
13461 fini_reloc_cookie (cookie, sec->owner);
13462 }
13463 \f
13464 /* Garbage collect unused sections. */
13465
13466 /* Default gc_mark_hook. */
13467
13468 asection *
13469 _bfd_elf_gc_mark_hook (asection *sec,
13470 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13471 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13472 struct elf_link_hash_entry *h,
13473 Elf_Internal_Sym *sym)
13474 {
13475 if (h != NULL)
13476 {
13477 switch (h->root.type)
13478 {
13479 case bfd_link_hash_defined:
13480 case bfd_link_hash_defweak:
13481 return h->root.u.def.section;
13482
13483 case bfd_link_hash_common:
13484 return h->root.u.c.p->section;
13485
13486 default:
13487 break;
13488 }
13489 }
13490 else
13491 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13492
13493 return NULL;
13494 }
13495
13496 /* Return the debug definition section. */
13497
13498 static asection *
13499 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13500 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13501 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13502 struct elf_link_hash_entry *h,
13503 Elf_Internal_Sym *sym)
13504 {
13505 if (h != NULL)
13506 {
13507 /* Return the global debug definition section. */
13508 if ((h->root.type == bfd_link_hash_defined
13509 || h->root.type == bfd_link_hash_defweak)
13510 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13511 return h->root.u.def.section;
13512 }
13513 else
13514 {
13515 /* Return the local debug definition section. */
13516 asection *isec = bfd_section_from_elf_index (sec->owner,
13517 sym->st_shndx);
13518 if ((isec->flags & SEC_DEBUGGING) != 0)
13519 return isec;
13520 }
13521
13522 return NULL;
13523 }
13524
13525 /* COOKIE->rel describes a relocation against section SEC, which is
13526 a section we've decided to keep. Return the section that contains
13527 the relocation symbol, or NULL if no section contains it. */
13528
13529 asection *
13530 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13531 elf_gc_mark_hook_fn gc_mark_hook,
13532 struct elf_reloc_cookie *cookie,
13533 bfd_boolean *start_stop)
13534 {
13535 unsigned long r_symndx;
13536 struct elf_link_hash_entry *h, *hw;
13537
13538 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13539 if (r_symndx == STN_UNDEF)
13540 return NULL;
13541
13542 if (r_symndx >= cookie->locsymcount
13543 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13544 {
13545 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13546 if (h == NULL)
13547 {
13548 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13549 sec->owner);
13550 return NULL;
13551 }
13552 while (h->root.type == bfd_link_hash_indirect
13553 || h->root.type == bfd_link_hash_warning)
13554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13555 h->mark = 1;
13556 /* Keep all aliases of the symbol too. If an object symbol
13557 needs to be copied into .dynbss then all of its aliases
13558 should be present as dynamic symbols, not just the one used
13559 on the copy relocation. */
13560 hw = h;
13561 while (hw->is_weakalias)
13562 {
13563 hw = hw->u.alias;
13564 hw->mark = 1;
13565 }
13566
13567 if (start_stop != NULL)
13568 {
13569 /* To work around a glibc bug, mark XXX input sections
13570 when there is a reference to __start_XXX or __stop_XXX
13571 symbols. */
13572 if (h->start_stop)
13573 {
13574 asection *s = h->u2.start_stop_section;
13575 *start_stop = !s->gc_mark;
13576 return s;
13577 }
13578 }
13579
13580 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13581 }
13582
13583 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13584 &cookie->locsyms[r_symndx]);
13585 }
13586
13587 /* COOKIE->rel describes a relocation against section SEC, which is
13588 a section we've decided to keep. Mark the section that contains
13589 the relocation symbol. */
13590
13591 bfd_boolean
13592 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13593 asection *sec,
13594 elf_gc_mark_hook_fn gc_mark_hook,
13595 struct elf_reloc_cookie *cookie)
13596 {
13597 asection *rsec;
13598 bfd_boolean start_stop = FALSE;
13599
13600 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13601 while (rsec != NULL)
13602 {
13603 if (!rsec->gc_mark)
13604 {
13605 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13606 || (rsec->owner->flags & DYNAMIC) != 0)
13607 rsec->gc_mark = 1;
13608 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13609 return FALSE;
13610 }
13611 if (!start_stop)
13612 break;
13613 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13614 }
13615 return TRUE;
13616 }
13617
13618 /* The mark phase of garbage collection. For a given section, mark
13619 it and any sections in this section's group, and all the sections
13620 which define symbols to which it refers. */
13621
13622 bfd_boolean
13623 _bfd_elf_gc_mark (struct bfd_link_info *info,
13624 asection *sec,
13625 elf_gc_mark_hook_fn gc_mark_hook)
13626 {
13627 bfd_boolean ret;
13628 asection *group_sec, *eh_frame;
13629
13630 sec->gc_mark = 1;
13631
13632 /* Mark all the sections in the group. */
13633 group_sec = elf_section_data (sec)->next_in_group;
13634 if (group_sec && !group_sec->gc_mark)
13635 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13636 return FALSE;
13637
13638 /* Look through the section relocs. */
13639 ret = TRUE;
13640 eh_frame = elf_eh_frame_section (sec->owner);
13641 if ((sec->flags & SEC_RELOC) != 0
13642 && sec->reloc_count > 0
13643 && sec != eh_frame)
13644 {
13645 struct elf_reloc_cookie cookie;
13646
13647 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13648 ret = FALSE;
13649 else
13650 {
13651 for (; cookie.rel < cookie.relend; cookie.rel++)
13652 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13653 {
13654 ret = FALSE;
13655 break;
13656 }
13657 fini_reloc_cookie_for_section (&cookie, sec);
13658 }
13659 }
13660
13661 if (ret && eh_frame && elf_fde_list (sec))
13662 {
13663 struct elf_reloc_cookie cookie;
13664
13665 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13666 ret = FALSE;
13667 else
13668 {
13669 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13670 gc_mark_hook, &cookie))
13671 ret = FALSE;
13672 fini_reloc_cookie_for_section (&cookie, eh_frame);
13673 }
13674 }
13675
13676 eh_frame = elf_section_eh_frame_entry (sec);
13677 if (ret && eh_frame && !eh_frame->gc_mark)
13678 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13679 ret = FALSE;
13680
13681 return ret;
13682 }
13683
13684 /* Scan and mark sections in a special or debug section group. */
13685
13686 static void
13687 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13688 {
13689 /* Point to first section of section group. */
13690 asection *ssec;
13691 /* Used to iterate the section group. */
13692 asection *msec;
13693
13694 bfd_boolean is_special_grp = TRUE;
13695 bfd_boolean is_debug_grp = TRUE;
13696
13697 /* First scan to see if group contains any section other than debug
13698 and special section. */
13699 ssec = msec = elf_next_in_group (grp);
13700 do
13701 {
13702 if ((msec->flags & SEC_DEBUGGING) == 0)
13703 is_debug_grp = FALSE;
13704
13705 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13706 is_special_grp = FALSE;
13707
13708 msec = elf_next_in_group (msec);
13709 }
13710 while (msec != ssec);
13711
13712 /* If this is a pure debug section group or pure special section group,
13713 keep all sections in this group. */
13714 if (is_debug_grp || is_special_grp)
13715 {
13716 do
13717 {
13718 msec->gc_mark = 1;
13719 msec = elf_next_in_group (msec);
13720 }
13721 while (msec != ssec);
13722 }
13723 }
13724
13725 /* Keep debug and special sections. */
13726
13727 bfd_boolean
13728 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13729 elf_gc_mark_hook_fn mark_hook)
13730 {
13731 bfd *ibfd;
13732
13733 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13734 {
13735 asection *isec;
13736 bfd_boolean some_kept;
13737 bfd_boolean debug_frag_seen;
13738 bfd_boolean has_kept_debug_info;
13739
13740 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13741 continue;
13742 isec = ibfd->sections;
13743 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13744 continue;
13745
13746 /* Ensure all linker created sections are kept,
13747 see if any other section is already marked,
13748 and note if we have any fragmented debug sections. */
13749 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13750 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13751 {
13752 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13753 isec->gc_mark = 1;
13754 else if (isec->gc_mark
13755 && (isec->flags & SEC_ALLOC) != 0
13756 && elf_section_type (isec) != SHT_NOTE)
13757 some_kept = TRUE;
13758 else
13759 {
13760 /* Since all sections, except for backend specific ones,
13761 have been garbage collected, call mark_hook on this
13762 section if any of its linked-to sections is marked. */
13763 asection *linked_to_sec = elf_linked_to_section (isec);
13764 for (; linked_to_sec != NULL;
13765 linked_to_sec = elf_linked_to_section (linked_to_sec))
13766 if (linked_to_sec->gc_mark)
13767 {
13768 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13769 return FALSE;
13770 break;
13771 }
13772 }
13773
13774 if (!debug_frag_seen
13775 && (isec->flags & SEC_DEBUGGING)
13776 && CONST_STRNEQ (isec->name, ".debug_line."))
13777 debug_frag_seen = TRUE;
13778 else if (strcmp (bfd_section_name (isec),
13779 "__patchable_function_entries") == 0
13780 && elf_linked_to_section (isec) == NULL)
13781 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13782 "need linked-to section "
13783 "for --gc-sections\n"),
13784 isec->owner, isec);
13785 }
13786
13787 /* If no non-note alloc section in this file will be kept, then
13788 we can toss out the debug and special sections. */
13789 if (!some_kept)
13790 continue;
13791
13792 /* Keep debug and special sections like .comment when they are
13793 not part of a group. Also keep section groups that contain
13794 just debug sections or special sections. NB: Sections with
13795 linked-to section has been handled above. */
13796 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13797 {
13798 if ((isec->flags & SEC_GROUP) != 0)
13799 _bfd_elf_gc_mark_debug_special_section_group (isec);
13800 else if (((isec->flags & SEC_DEBUGGING) != 0
13801 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13802 && elf_next_in_group (isec) == NULL
13803 && elf_linked_to_section (isec) == NULL)
13804 isec->gc_mark = 1;
13805 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13806 has_kept_debug_info = TRUE;
13807 }
13808
13809 /* Look for CODE sections which are going to be discarded,
13810 and find and discard any fragmented debug sections which
13811 are associated with that code section. */
13812 if (debug_frag_seen)
13813 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13814 if ((isec->flags & SEC_CODE) != 0
13815 && isec->gc_mark == 0)
13816 {
13817 unsigned int ilen;
13818 asection *dsec;
13819
13820 ilen = strlen (isec->name);
13821
13822 /* Association is determined by the name of the debug
13823 section containing the name of the code section as
13824 a suffix. For example .debug_line.text.foo is a
13825 debug section associated with .text.foo. */
13826 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13827 {
13828 unsigned int dlen;
13829
13830 if (dsec->gc_mark == 0
13831 || (dsec->flags & SEC_DEBUGGING) == 0)
13832 continue;
13833
13834 dlen = strlen (dsec->name);
13835
13836 if (dlen > ilen
13837 && strncmp (dsec->name + (dlen - ilen),
13838 isec->name, ilen) == 0)
13839 dsec->gc_mark = 0;
13840 }
13841 }
13842
13843 /* Mark debug sections referenced by kept debug sections. */
13844 if (has_kept_debug_info)
13845 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13846 if (isec->gc_mark
13847 && (isec->flags & SEC_DEBUGGING) != 0)
13848 if (!_bfd_elf_gc_mark (info, isec,
13849 elf_gc_mark_debug_section))
13850 return FALSE;
13851 }
13852 return TRUE;
13853 }
13854
13855 static bfd_boolean
13856 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13857 {
13858 bfd *sub;
13859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13860
13861 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13862 {
13863 asection *o;
13864
13865 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13866 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13867 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13868 continue;
13869 o = sub->sections;
13870 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13871 continue;
13872
13873 for (o = sub->sections; o != NULL; o = o->next)
13874 {
13875 /* When any section in a section group is kept, we keep all
13876 sections in the section group. If the first member of
13877 the section group is excluded, we will also exclude the
13878 group section. */
13879 if (o->flags & SEC_GROUP)
13880 {
13881 asection *first = elf_next_in_group (o);
13882 o->gc_mark = first->gc_mark;
13883 }
13884
13885 if (o->gc_mark)
13886 continue;
13887
13888 /* Skip sweeping sections already excluded. */
13889 if (o->flags & SEC_EXCLUDE)
13890 continue;
13891
13892 /* Since this is early in the link process, it is simple
13893 to remove a section from the output. */
13894 o->flags |= SEC_EXCLUDE;
13895
13896 if (info->print_gc_sections && o->size != 0)
13897 /* xgettext:c-format */
13898 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13899 o, sub);
13900 }
13901 }
13902
13903 return TRUE;
13904 }
13905
13906 /* Propagate collected vtable information. This is called through
13907 elf_link_hash_traverse. */
13908
13909 static bfd_boolean
13910 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13911 {
13912 /* Those that are not vtables. */
13913 if (h->start_stop
13914 || h->u2.vtable == NULL
13915 || h->u2.vtable->parent == NULL)
13916 return TRUE;
13917
13918 /* Those vtables that do not have parents, we cannot merge. */
13919 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13920 return TRUE;
13921
13922 /* If we've already been done, exit. */
13923 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13924 return TRUE;
13925
13926 /* Make sure the parent's table is up to date. */
13927 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13928
13929 if (h->u2.vtable->used == NULL)
13930 {
13931 /* None of this table's entries were referenced. Re-use the
13932 parent's table. */
13933 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13934 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13935 }
13936 else
13937 {
13938 size_t n;
13939 bfd_boolean *cu, *pu;
13940
13941 /* Or the parent's entries into ours. */
13942 cu = h->u2.vtable->used;
13943 cu[-1] = TRUE;
13944 pu = h->u2.vtable->parent->u2.vtable->used;
13945 if (pu != NULL)
13946 {
13947 const struct elf_backend_data *bed;
13948 unsigned int log_file_align;
13949
13950 bed = get_elf_backend_data (h->root.u.def.section->owner);
13951 log_file_align = bed->s->log_file_align;
13952 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13953 while (n--)
13954 {
13955 if (*pu)
13956 *cu = TRUE;
13957 pu++;
13958 cu++;
13959 }
13960 }
13961 }
13962
13963 return TRUE;
13964 }
13965
13966 static bfd_boolean
13967 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13968 {
13969 asection *sec;
13970 bfd_vma hstart, hend;
13971 Elf_Internal_Rela *relstart, *relend, *rel;
13972 const struct elf_backend_data *bed;
13973 unsigned int log_file_align;
13974
13975 /* Take care of both those symbols that do not describe vtables as
13976 well as those that are not loaded. */
13977 if (h->start_stop
13978 || h->u2.vtable == NULL
13979 || h->u2.vtable->parent == NULL)
13980 return TRUE;
13981
13982 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13983 || h->root.type == bfd_link_hash_defweak);
13984
13985 sec = h->root.u.def.section;
13986 hstart = h->root.u.def.value;
13987 hend = hstart + h->size;
13988
13989 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13990 if (!relstart)
13991 return *(bfd_boolean *) okp = FALSE;
13992 bed = get_elf_backend_data (sec->owner);
13993 log_file_align = bed->s->log_file_align;
13994
13995 relend = relstart + sec->reloc_count;
13996
13997 for (rel = relstart; rel < relend; ++rel)
13998 if (rel->r_offset >= hstart && rel->r_offset < hend)
13999 {
14000 /* If the entry is in use, do nothing. */
14001 if (h->u2.vtable->used
14002 && (rel->r_offset - hstart) < h->u2.vtable->size)
14003 {
14004 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14005 if (h->u2.vtable->used[entry])
14006 continue;
14007 }
14008 /* Otherwise, kill it. */
14009 rel->r_offset = rel->r_info = rel->r_addend = 0;
14010 }
14011
14012 return TRUE;
14013 }
14014
14015 /* Mark sections containing dynamically referenced symbols. When
14016 building shared libraries, we must assume that any visible symbol is
14017 referenced. */
14018
14019 bfd_boolean
14020 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14021 {
14022 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14023 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14024
14025 if ((h->root.type == bfd_link_hash_defined
14026 || h->root.type == bfd_link_hash_defweak)
14027 && ((h->ref_dynamic && !h->forced_local)
14028 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14029 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14030 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14031 && (!bfd_link_executable (info)
14032 || info->gc_keep_exported
14033 || info->export_dynamic
14034 || (h->dynamic
14035 && d != NULL
14036 && (*d->match) (&d->head, NULL, h->root.root.string)))
14037 && (h->versioned >= versioned
14038 || !bfd_hide_sym_by_version (info->version_info,
14039 h->root.root.string)))))
14040 h->root.u.def.section->flags |= SEC_KEEP;
14041
14042 return TRUE;
14043 }
14044
14045 /* Keep all sections containing symbols undefined on the command-line,
14046 and the section containing the entry symbol. */
14047
14048 void
14049 _bfd_elf_gc_keep (struct bfd_link_info *info)
14050 {
14051 struct bfd_sym_chain *sym;
14052
14053 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14054 {
14055 struct elf_link_hash_entry *h;
14056
14057 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14058 FALSE, FALSE, FALSE);
14059
14060 if (h != NULL
14061 && (h->root.type == bfd_link_hash_defined
14062 || h->root.type == bfd_link_hash_defweak)
14063 && !bfd_is_const_section (h->root.u.def.section))
14064 h->root.u.def.section->flags |= SEC_KEEP;
14065 }
14066 }
14067
14068 bfd_boolean
14069 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14070 struct bfd_link_info *info)
14071 {
14072 bfd *ibfd = info->input_bfds;
14073
14074 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14075 {
14076 asection *sec;
14077 struct elf_reloc_cookie cookie;
14078
14079 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14080 continue;
14081 sec = ibfd->sections;
14082 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14083 continue;
14084
14085 if (!init_reloc_cookie (&cookie, info, ibfd))
14086 return FALSE;
14087
14088 for (sec = ibfd->sections; sec; sec = sec->next)
14089 {
14090 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
14091 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14092 {
14093 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14094 fini_reloc_cookie_rels (&cookie, sec);
14095 }
14096 }
14097 }
14098 return TRUE;
14099 }
14100
14101 /* Do mark and sweep of unused sections. */
14102
14103 bfd_boolean
14104 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14105 {
14106 bfd_boolean ok = TRUE;
14107 bfd *sub;
14108 elf_gc_mark_hook_fn gc_mark_hook;
14109 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14110 struct elf_link_hash_table *htab;
14111
14112 if (!bed->can_gc_sections
14113 || !is_elf_hash_table (info->hash))
14114 {
14115 _bfd_error_handler(_("warning: gc-sections option ignored"));
14116 return TRUE;
14117 }
14118
14119 bed->gc_keep (info);
14120 htab = elf_hash_table (info);
14121
14122 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14123 at the .eh_frame section if we can mark the FDEs individually. */
14124 for (sub = info->input_bfds;
14125 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14126 sub = sub->link.next)
14127 {
14128 asection *sec;
14129 struct elf_reloc_cookie cookie;
14130
14131 sec = sub->sections;
14132 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14133 continue;
14134 sec = bfd_get_section_by_name (sub, ".eh_frame");
14135 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14136 {
14137 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14138 if (elf_section_data (sec)->sec_info
14139 && (sec->flags & SEC_LINKER_CREATED) == 0)
14140 elf_eh_frame_section (sub) = sec;
14141 fini_reloc_cookie_for_section (&cookie, sec);
14142 sec = bfd_get_next_section_by_name (NULL, sec);
14143 }
14144 }
14145
14146 /* Apply transitive closure to the vtable entry usage info. */
14147 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14148 if (!ok)
14149 return FALSE;
14150
14151 /* Kill the vtable relocations that were not used. */
14152 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14153 if (!ok)
14154 return FALSE;
14155
14156 /* Mark dynamically referenced symbols. */
14157 if (htab->dynamic_sections_created || info->gc_keep_exported)
14158 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14159
14160 /* Grovel through relocs to find out who stays ... */
14161 gc_mark_hook = bed->gc_mark_hook;
14162 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14163 {
14164 asection *o;
14165
14166 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14167 || elf_object_id (sub) != elf_hash_table_id (htab)
14168 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14169 continue;
14170
14171 o = sub->sections;
14172 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14173 continue;
14174
14175 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14176 Also treat note sections as a root, if the section is not part
14177 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14178 well as FINI_ARRAY sections for ld -r. */
14179 for (o = sub->sections; o != NULL; o = o->next)
14180 if (!o->gc_mark
14181 && (o->flags & SEC_EXCLUDE) == 0
14182 && ((o->flags & SEC_KEEP) != 0
14183 || (bfd_link_relocatable (info)
14184 && ((elf_section_data (o)->this_hdr.sh_type
14185 == SHT_PREINIT_ARRAY)
14186 || (elf_section_data (o)->this_hdr.sh_type
14187 == SHT_INIT_ARRAY)
14188 || (elf_section_data (o)->this_hdr.sh_type
14189 == SHT_FINI_ARRAY)))
14190 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14191 && elf_next_in_group (o) == NULL
14192 && elf_linked_to_section (o) == NULL)
14193 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14194 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14195 {
14196 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14197 return FALSE;
14198 }
14199 }
14200
14201 /* Allow the backend to mark additional target specific sections. */
14202 bed->gc_mark_extra_sections (info, gc_mark_hook);
14203
14204 /* ... and mark SEC_EXCLUDE for those that go. */
14205 return elf_gc_sweep (abfd, info);
14206 }
14207 \f
14208 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14209
14210 bfd_boolean
14211 bfd_elf_gc_record_vtinherit (bfd *abfd,
14212 asection *sec,
14213 struct elf_link_hash_entry *h,
14214 bfd_vma offset)
14215 {
14216 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14217 struct elf_link_hash_entry **search, *child;
14218 size_t extsymcount;
14219 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14220
14221 /* The sh_info field of the symtab header tells us where the
14222 external symbols start. We don't care about the local symbols at
14223 this point. */
14224 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14225 if (!elf_bad_symtab (abfd))
14226 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14227
14228 sym_hashes = elf_sym_hashes (abfd);
14229 sym_hashes_end = sym_hashes + extsymcount;
14230
14231 /* Hunt down the child symbol, which is in this section at the same
14232 offset as the relocation. */
14233 for (search = sym_hashes; search != sym_hashes_end; ++search)
14234 {
14235 if ((child = *search) != NULL
14236 && (child->root.type == bfd_link_hash_defined
14237 || child->root.type == bfd_link_hash_defweak)
14238 && child->root.u.def.section == sec
14239 && child->root.u.def.value == offset)
14240 goto win;
14241 }
14242
14243 /* xgettext:c-format */
14244 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14245 abfd, sec, (uint64_t) offset);
14246 bfd_set_error (bfd_error_invalid_operation);
14247 return FALSE;
14248
14249 win:
14250 if (!child->u2.vtable)
14251 {
14252 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14253 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14254 if (!child->u2.vtable)
14255 return FALSE;
14256 }
14257 if (!h)
14258 {
14259 /* This *should* only be the absolute section. It could potentially
14260 be that someone has defined a non-global vtable though, which
14261 would be bad. It isn't worth paging in the local symbols to be
14262 sure though; that case should simply be handled by the assembler. */
14263
14264 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14265 }
14266 else
14267 child->u2.vtable->parent = h;
14268
14269 return TRUE;
14270 }
14271
14272 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14273
14274 bfd_boolean
14275 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14276 struct elf_link_hash_entry *h,
14277 bfd_vma addend)
14278 {
14279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14280 unsigned int log_file_align = bed->s->log_file_align;
14281
14282 if (!h)
14283 {
14284 /* xgettext:c-format */
14285 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14286 abfd, sec);
14287 bfd_set_error (bfd_error_bad_value);
14288 return FALSE;
14289 }
14290
14291 if (!h->u2.vtable)
14292 {
14293 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14294 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14295 if (!h->u2.vtable)
14296 return FALSE;
14297 }
14298
14299 if (addend >= h->u2.vtable->size)
14300 {
14301 size_t size, bytes, file_align;
14302 bfd_boolean *ptr = h->u2.vtable->used;
14303
14304 /* While the symbol is undefined, we have to be prepared to handle
14305 a zero size. */
14306 file_align = 1 << log_file_align;
14307 if (h->root.type == bfd_link_hash_undefined)
14308 size = addend + file_align;
14309 else
14310 {
14311 size = h->size;
14312 if (addend >= size)
14313 {
14314 /* Oops! We've got a reference past the defined end of
14315 the table. This is probably a bug -- shall we warn? */
14316 size = addend + file_align;
14317 }
14318 }
14319 size = (size + file_align - 1) & -file_align;
14320
14321 /* Allocate one extra entry for use as a "done" flag for the
14322 consolidation pass. */
14323 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14324
14325 if (ptr)
14326 {
14327 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14328
14329 if (ptr != NULL)
14330 {
14331 size_t oldbytes;
14332
14333 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14334 * sizeof (bfd_boolean));
14335 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14336 }
14337 }
14338 else
14339 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14340
14341 if (ptr == NULL)
14342 return FALSE;
14343
14344 /* And arrange for that done flag to be at index -1. */
14345 h->u2.vtable->used = ptr + 1;
14346 h->u2.vtable->size = size;
14347 }
14348
14349 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14350
14351 return TRUE;
14352 }
14353
14354 /* Map an ELF section header flag to its corresponding string. */
14355 typedef struct
14356 {
14357 char *flag_name;
14358 flagword flag_value;
14359 } elf_flags_to_name_table;
14360
14361 static const elf_flags_to_name_table elf_flags_to_names [] =
14362 {
14363 { "SHF_WRITE", SHF_WRITE },
14364 { "SHF_ALLOC", SHF_ALLOC },
14365 { "SHF_EXECINSTR", SHF_EXECINSTR },
14366 { "SHF_MERGE", SHF_MERGE },
14367 { "SHF_STRINGS", SHF_STRINGS },
14368 { "SHF_INFO_LINK", SHF_INFO_LINK},
14369 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14370 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14371 { "SHF_GROUP", SHF_GROUP },
14372 { "SHF_TLS", SHF_TLS },
14373 { "SHF_MASKOS", SHF_MASKOS },
14374 { "SHF_EXCLUDE", SHF_EXCLUDE },
14375 };
14376
14377 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14378 bfd_boolean
14379 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14380 struct flag_info *flaginfo,
14381 asection *section)
14382 {
14383 const bfd_vma sh_flags = elf_section_flags (section);
14384
14385 if (!flaginfo->flags_initialized)
14386 {
14387 bfd *obfd = info->output_bfd;
14388 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14389 struct flag_info_list *tf = flaginfo->flag_list;
14390 int with_hex = 0;
14391 int without_hex = 0;
14392
14393 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14394 {
14395 unsigned i;
14396 flagword (*lookup) (char *);
14397
14398 lookup = bed->elf_backend_lookup_section_flags_hook;
14399 if (lookup != NULL)
14400 {
14401 flagword hexval = (*lookup) ((char *) tf->name);
14402
14403 if (hexval != 0)
14404 {
14405 if (tf->with == with_flags)
14406 with_hex |= hexval;
14407 else if (tf->with == without_flags)
14408 without_hex |= hexval;
14409 tf->valid = TRUE;
14410 continue;
14411 }
14412 }
14413 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14414 {
14415 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14416 {
14417 if (tf->with == with_flags)
14418 with_hex |= elf_flags_to_names[i].flag_value;
14419 else if (tf->with == without_flags)
14420 without_hex |= elf_flags_to_names[i].flag_value;
14421 tf->valid = TRUE;
14422 break;
14423 }
14424 }
14425 if (!tf->valid)
14426 {
14427 info->callbacks->einfo
14428 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14429 return FALSE;
14430 }
14431 }
14432 flaginfo->flags_initialized = TRUE;
14433 flaginfo->only_with_flags |= with_hex;
14434 flaginfo->not_with_flags |= without_hex;
14435 }
14436
14437 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14438 return FALSE;
14439
14440 if ((flaginfo->not_with_flags & sh_flags) != 0)
14441 return FALSE;
14442
14443 return TRUE;
14444 }
14445
14446 struct alloc_got_off_arg {
14447 bfd_vma gotoff;
14448 struct bfd_link_info *info;
14449 };
14450
14451 /* We need a special top-level link routine to convert got reference counts
14452 to real got offsets. */
14453
14454 static bfd_boolean
14455 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14456 {
14457 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14458 bfd *obfd = gofarg->info->output_bfd;
14459 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14460
14461 if (h->got.refcount > 0)
14462 {
14463 h->got.offset = gofarg->gotoff;
14464 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14465 }
14466 else
14467 h->got.offset = (bfd_vma) -1;
14468
14469 return TRUE;
14470 }
14471
14472 /* And an accompanying bit to work out final got entry offsets once
14473 we're done. Should be called from final_link. */
14474
14475 bfd_boolean
14476 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14477 struct bfd_link_info *info)
14478 {
14479 bfd *i;
14480 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14481 bfd_vma gotoff;
14482 struct alloc_got_off_arg gofarg;
14483
14484 BFD_ASSERT (abfd == info->output_bfd);
14485
14486 if (! is_elf_hash_table (info->hash))
14487 return FALSE;
14488
14489 /* The GOT offset is relative to the .got section, but the GOT header is
14490 put into the .got.plt section, if the backend uses it. */
14491 if (bed->want_got_plt)
14492 gotoff = 0;
14493 else
14494 gotoff = bed->got_header_size;
14495
14496 /* Do the local .got entries first. */
14497 for (i = info->input_bfds; i; i = i->link.next)
14498 {
14499 bfd_signed_vma *local_got;
14500 size_t j, locsymcount;
14501 Elf_Internal_Shdr *symtab_hdr;
14502
14503 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14504 continue;
14505
14506 local_got = elf_local_got_refcounts (i);
14507 if (!local_got)
14508 continue;
14509
14510 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14511 if (elf_bad_symtab (i))
14512 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14513 else
14514 locsymcount = symtab_hdr->sh_info;
14515
14516 for (j = 0; j < locsymcount; ++j)
14517 {
14518 if (local_got[j] > 0)
14519 {
14520 local_got[j] = gotoff;
14521 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14522 }
14523 else
14524 local_got[j] = (bfd_vma) -1;
14525 }
14526 }
14527
14528 /* Then the global .got entries. .plt refcounts are handled by
14529 adjust_dynamic_symbol */
14530 gofarg.gotoff = gotoff;
14531 gofarg.info = info;
14532 elf_link_hash_traverse (elf_hash_table (info),
14533 elf_gc_allocate_got_offsets,
14534 &gofarg);
14535 return TRUE;
14536 }
14537
14538 /* Many folk need no more in the way of final link than this, once
14539 got entry reference counting is enabled. */
14540
14541 bfd_boolean
14542 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14543 {
14544 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14545 return FALSE;
14546
14547 /* Invoke the regular ELF backend linker to do all the work. */
14548 return bfd_elf_final_link (abfd, info);
14549 }
14550
14551 bfd_boolean
14552 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14553 {
14554 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14555
14556 if (rcookie->bad_symtab)
14557 rcookie->rel = rcookie->rels;
14558
14559 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14560 {
14561 unsigned long r_symndx;
14562
14563 if (! rcookie->bad_symtab)
14564 if (rcookie->rel->r_offset > offset)
14565 return FALSE;
14566 if (rcookie->rel->r_offset != offset)
14567 continue;
14568
14569 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14570 if (r_symndx == STN_UNDEF)
14571 return TRUE;
14572
14573 if (r_symndx >= rcookie->locsymcount
14574 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14575 {
14576 struct elf_link_hash_entry *h;
14577
14578 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14579
14580 while (h->root.type == bfd_link_hash_indirect
14581 || h->root.type == bfd_link_hash_warning)
14582 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14583
14584 if ((h->root.type == bfd_link_hash_defined
14585 || h->root.type == bfd_link_hash_defweak)
14586 && (h->root.u.def.section->owner != rcookie->abfd
14587 || h->root.u.def.section->kept_section != NULL
14588 || discarded_section (h->root.u.def.section)))
14589 return TRUE;
14590 }
14591 else
14592 {
14593 /* It's not a relocation against a global symbol,
14594 but it could be a relocation against a local
14595 symbol for a discarded section. */
14596 asection *isec;
14597 Elf_Internal_Sym *isym;
14598
14599 /* Need to: get the symbol; get the section. */
14600 isym = &rcookie->locsyms[r_symndx];
14601 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14602 if (isec != NULL
14603 && (isec->kept_section != NULL
14604 || discarded_section (isec)))
14605 return TRUE;
14606 }
14607 return FALSE;
14608 }
14609 return FALSE;
14610 }
14611
14612 /* Discard unneeded references to discarded sections.
14613 Returns -1 on error, 1 if any section's size was changed, 0 if
14614 nothing changed. This function assumes that the relocations are in
14615 sorted order, which is true for all known assemblers. */
14616
14617 int
14618 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14619 {
14620 struct elf_reloc_cookie cookie;
14621 asection *o;
14622 bfd *abfd;
14623 int changed = 0;
14624
14625 if (info->traditional_format
14626 || !is_elf_hash_table (info->hash))
14627 return 0;
14628
14629 o = bfd_get_section_by_name (output_bfd, ".stab");
14630 if (o != NULL)
14631 {
14632 asection *i;
14633
14634 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14635 {
14636 if (i->size == 0
14637 || i->reloc_count == 0
14638 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14639 continue;
14640
14641 abfd = i->owner;
14642 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14643 continue;
14644
14645 if (!init_reloc_cookie_for_section (&cookie, info, i))
14646 return -1;
14647
14648 if (_bfd_discard_section_stabs (abfd, i,
14649 elf_section_data (i)->sec_info,
14650 bfd_elf_reloc_symbol_deleted_p,
14651 &cookie))
14652 changed = 1;
14653
14654 fini_reloc_cookie_for_section (&cookie, i);
14655 }
14656 }
14657
14658 o = NULL;
14659 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14660 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14661 if (o != NULL)
14662 {
14663 asection *i;
14664 int eh_changed = 0;
14665 unsigned int eh_alignment; /* Octets. */
14666
14667 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14668 {
14669 if (i->size == 0)
14670 continue;
14671
14672 abfd = i->owner;
14673 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14674 continue;
14675
14676 if (!init_reloc_cookie_for_section (&cookie, info, i))
14677 return -1;
14678
14679 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14680 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14681 bfd_elf_reloc_symbol_deleted_p,
14682 &cookie))
14683 {
14684 eh_changed = 1;
14685 if (i->size != i->rawsize)
14686 changed = 1;
14687 }
14688
14689 fini_reloc_cookie_for_section (&cookie, i);
14690 }
14691
14692 eh_alignment = ((1 << o->alignment_power)
14693 * bfd_octets_per_byte (output_bfd, o));
14694 /* Skip over zero terminator, and prevent empty sections from
14695 adding alignment padding at the end. */
14696 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14697 if (i->size == 0)
14698 i->flags |= SEC_EXCLUDE;
14699 else if (i->size > 4)
14700 break;
14701 /* The last non-empty eh_frame section doesn't need padding. */
14702 if (i != NULL)
14703 i = i->map_tail.s;
14704 /* Any prior sections must pad the last FDE out to the output
14705 section alignment. Otherwise we might have zero padding
14706 between sections, which would be seen as a terminator. */
14707 for (; i != NULL; i = i->map_tail.s)
14708 if (i->size == 4)
14709 /* All but the last zero terminator should have been removed. */
14710 BFD_FAIL ();
14711 else
14712 {
14713 bfd_size_type size
14714 = (i->size + eh_alignment - 1) & -eh_alignment;
14715 if (i->size != size)
14716 {
14717 i->size = size;
14718 changed = 1;
14719 eh_changed = 1;
14720 }
14721 }
14722 if (eh_changed)
14723 elf_link_hash_traverse (elf_hash_table (info),
14724 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14725 }
14726
14727 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14728 {
14729 const struct elf_backend_data *bed;
14730 asection *s;
14731
14732 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14733 continue;
14734 s = abfd->sections;
14735 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14736 continue;
14737
14738 bed = get_elf_backend_data (abfd);
14739
14740 if (bed->elf_backend_discard_info != NULL)
14741 {
14742 if (!init_reloc_cookie (&cookie, info, abfd))
14743 return -1;
14744
14745 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14746 changed = 1;
14747
14748 fini_reloc_cookie (&cookie, abfd);
14749 }
14750 }
14751
14752 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14753 _bfd_elf_end_eh_frame_parsing (info);
14754
14755 if (info->eh_frame_hdr_type
14756 && !bfd_link_relocatable (info)
14757 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14758 changed = 1;
14759
14760 return changed;
14761 }
14762
14763 bfd_boolean
14764 _bfd_elf_section_already_linked (bfd *abfd,
14765 asection *sec,
14766 struct bfd_link_info *info)
14767 {
14768 flagword flags;
14769 const char *name, *key;
14770 struct bfd_section_already_linked *l;
14771 struct bfd_section_already_linked_hash_entry *already_linked_list;
14772
14773 if (sec->output_section == bfd_abs_section_ptr)
14774 return FALSE;
14775
14776 flags = sec->flags;
14777
14778 /* Return if it isn't a linkonce section. A comdat group section
14779 also has SEC_LINK_ONCE set. */
14780 if ((flags & SEC_LINK_ONCE) == 0)
14781 return FALSE;
14782
14783 /* Don't put group member sections on our list of already linked
14784 sections. They are handled as a group via their group section. */
14785 if (elf_sec_group (sec) != NULL)
14786 return FALSE;
14787
14788 /* For a SHT_GROUP section, use the group signature as the key. */
14789 name = sec->name;
14790 if ((flags & SEC_GROUP) != 0
14791 && elf_next_in_group (sec) != NULL
14792 && elf_group_name (elf_next_in_group (sec)) != NULL)
14793 key = elf_group_name (elf_next_in_group (sec));
14794 else
14795 {
14796 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14797 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14798 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14799 key++;
14800 else
14801 /* Must be a user linkonce section that doesn't follow gcc's
14802 naming convention. In this case we won't be matching
14803 single member groups. */
14804 key = name;
14805 }
14806
14807 already_linked_list = bfd_section_already_linked_table_lookup (key);
14808
14809 for (l = already_linked_list->entry; l != NULL; l = l->next)
14810 {
14811 /* We may have 2 different types of sections on the list: group
14812 sections with a signature of <key> (<key> is some string),
14813 and linkonce sections named .gnu.linkonce.<type>.<key>.
14814 Match like sections. LTO plugin sections are an exception.
14815 They are always named .gnu.linkonce.t.<key> and match either
14816 type of section. */
14817 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14818 && ((flags & SEC_GROUP) != 0
14819 || strcmp (name, l->sec->name) == 0))
14820 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14821 || (sec->owner->flags & BFD_PLUGIN) != 0)
14822 {
14823 /* The section has already been linked. See if we should
14824 issue a warning. */
14825 if (!_bfd_handle_already_linked (sec, l, info))
14826 return FALSE;
14827
14828 if (flags & SEC_GROUP)
14829 {
14830 asection *first = elf_next_in_group (sec);
14831 asection *s = first;
14832
14833 while (s != NULL)
14834 {
14835 s->output_section = bfd_abs_section_ptr;
14836 /* Record which group discards it. */
14837 s->kept_section = l->sec;
14838 s = elf_next_in_group (s);
14839 /* These lists are circular. */
14840 if (s == first)
14841 break;
14842 }
14843 }
14844
14845 return TRUE;
14846 }
14847 }
14848
14849 /* A single member comdat group section may be discarded by a
14850 linkonce section and vice versa. */
14851 if ((flags & SEC_GROUP) != 0)
14852 {
14853 asection *first = elf_next_in_group (sec);
14854
14855 if (first != NULL && elf_next_in_group (first) == first)
14856 /* Check this single member group against linkonce sections. */
14857 for (l = already_linked_list->entry; l != NULL; l = l->next)
14858 if ((l->sec->flags & SEC_GROUP) == 0
14859 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14860 {
14861 first->output_section = bfd_abs_section_ptr;
14862 first->kept_section = l->sec;
14863 sec->output_section = bfd_abs_section_ptr;
14864 break;
14865 }
14866 }
14867 else
14868 /* Check this linkonce section against single member groups. */
14869 for (l = already_linked_list->entry; l != NULL; l = l->next)
14870 if (l->sec->flags & SEC_GROUP)
14871 {
14872 asection *first = elf_next_in_group (l->sec);
14873
14874 if (first != NULL
14875 && elf_next_in_group (first) == first
14876 && bfd_elf_match_symbols_in_sections (first, sec, info))
14877 {
14878 sec->output_section = bfd_abs_section_ptr;
14879 sec->kept_section = first;
14880 break;
14881 }
14882 }
14883
14884 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14885 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14886 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14887 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14888 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14889 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14890 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14891 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14892 The reverse order cannot happen as there is never a bfd with only the
14893 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14894 matter as here were are looking only for cross-bfd sections. */
14895
14896 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14897 for (l = already_linked_list->entry; l != NULL; l = l->next)
14898 if ((l->sec->flags & SEC_GROUP) == 0
14899 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14900 {
14901 if (abfd != l->sec->owner)
14902 sec->output_section = bfd_abs_section_ptr;
14903 break;
14904 }
14905
14906 /* This is the first section with this name. Record it. */
14907 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14908 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14909 return sec->output_section == bfd_abs_section_ptr;
14910 }
14911
14912 bfd_boolean
14913 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14914 {
14915 return sym->st_shndx == SHN_COMMON;
14916 }
14917
14918 unsigned int
14919 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14920 {
14921 return SHN_COMMON;
14922 }
14923
14924 asection *
14925 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14926 {
14927 return bfd_com_section_ptr;
14928 }
14929
14930 bfd_vma
14931 _bfd_elf_default_got_elt_size (bfd *abfd,
14932 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14933 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14934 bfd *ibfd ATTRIBUTE_UNUSED,
14935 unsigned long symndx ATTRIBUTE_UNUSED)
14936 {
14937 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14938 return bed->s->arch_size / 8;
14939 }
14940
14941 /* Routines to support the creation of dynamic relocs. */
14942
14943 /* Returns the name of the dynamic reloc section associated with SEC. */
14944
14945 static const char *
14946 get_dynamic_reloc_section_name (bfd * abfd,
14947 asection * sec,
14948 bfd_boolean is_rela)
14949 {
14950 char *name;
14951 const char *old_name = bfd_section_name (sec);
14952 const char *prefix = is_rela ? ".rela" : ".rel";
14953
14954 if (old_name == NULL)
14955 return NULL;
14956
14957 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14958 sprintf (name, "%s%s", prefix, old_name);
14959
14960 return name;
14961 }
14962
14963 /* Returns the dynamic reloc section associated with SEC.
14964 If necessary compute the name of the dynamic reloc section based
14965 on SEC's name (looked up in ABFD's string table) and the setting
14966 of IS_RELA. */
14967
14968 asection *
14969 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14970 asection * sec,
14971 bfd_boolean is_rela)
14972 {
14973 asection * reloc_sec = elf_section_data (sec)->sreloc;
14974
14975 if (reloc_sec == NULL)
14976 {
14977 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14978
14979 if (name != NULL)
14980 {
14981 reloc_sec = bfd_get_linker_section (abfd, name);
14982
14983 if (reloc_sec != NULL)
14984 elf_section_data (sec)->sreloc = reloc_sec;
14985 }
14986 }
14987
14988 return reloc_sec;
14989 }
14990
14991 /* Returns the dynamic reloc section associated with SEC. If the
14992 section does not exist it is created and attached to the DYNOBJ
14993 bfd and stored in the SRELOC field of SEC's elf_section_data
14994 structure.
14995
14996 ALIGNMENT is the alignment for the newly created section and
14997 IS_RELA defines whether the name should be .rela.<SEC's name>
14998 or .rel.<SEC's name>. The section name is looked up in the
14999 string table associated with ABFD. */
15000
15001 asection *
15002 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15003 bfd *dynobj,
15004 unsigned int alignment,
15005 bfd *abfd,
15006 bfd_boolean is_rela)
15007 {
15008 asection * reloc_sec = elf_section_data (sec)->sreloc;
15009
15010 if (reloc_sec == NULL)
15011 {
15012 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15013
15014 if (name == NULL)
15015 return NULL;
15016
15017 reloc_sec = bfd_get_linker_section (dynobj, name);
15018
15019 if (reloc_sec == NULL)
15020 {
15021 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15022 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15023 if ((sec->flags & SEC_ALLOC) != 0)
15024 flags |= SEC_ALLOC | SEC_LOAD;
15025
15026 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15027 if (reloc_sec != NULL)
15028 {
15029 /* _bfd_elf_get_sec_type_attr chooses a section type by
15030 name. Override as it may be wrong, eg. for a user
15031 section named "auto" we'll get ".relauto" which is
15032 seen to be a .rela section. */
15033 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15034 if (!bfd_set_section_alignment (reloc_sec, alignment))
15035 reloc_sec = NULL;
15036 }
15037 }
15038
15039 elf_section_data (sec)->sreloc = reloc_sec;
15040 }
15041
15042 return reloc_sec;
15043 }
15044
15045 /* Copy the ELF symbol type and other attributes for a linker script
15046 assignment from HSRC to HDEST. Generally this should be treated as
15047 if we found a strong non-dynamic definition for HDEST (except that
15048 ld ignores multiple definition errors). */
15049 void
15050 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15051 struct bfd_link_hash_entry *hdest,
15052 struct bfd_link_hash_entry *hsrc)
15053 {
15054 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15055 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15056 Elf_Internal_Sym isym;
15057
15058 ehdest->type = ehsrc->type;
15059 ehdest->target_internal = ehsrc->target_internal;
15060
15061 isym.st_other = ehsrc->other;
15062 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
15063 }
15064
15065 /* Append a RELA relocation REL to section S in BFD. */
15066
15067 void
15068 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15069 {
15070 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15071 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15072 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15073 bed->s->swap_reloca_out (abfd, rel, loc);
15074 }
15075
15076 /* Append a REL relocation REL to section S in BFD. */
15077
15078 void
15079 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15080 {
15081 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15082 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15083 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15084 bed->s->swap_reloc_out (abfd, rel, loc);
15085 }
15086
15087 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15088
15089 struct bfd_link_hash_entry *
15090 bfd_elf_define_start_stop (struct bfd_link_info *info,
15091 const char *symbol, asection *sec)
15092 {
15093 struct elf_link_hash_entry *h;
15094
15095 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15096 FALSE, FALSE, TRUE);
15097 /* NB: Common symbols will be turned into definition later. */
15098 if (h != NULL
15099 && (h->root.type == bfd_link_hash_undefined
15100 || h->root.type == bfd_link_hash_undefweak
15101 || ((h->ref_regular || h->def_dynamic)
15102 && !h->def_regular
15103 && h->root.type != bfd_link_hash_common)))
15104 {
15105 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
15106 h->verinfo.verdef = NULL;
15107 h->root.type = bfd_link_hash_defined;
15108 h->root.u.def.section = sec;
15109 h->root.u.def.value = 0;
15110 h->def_regular = 1;
15111 h->def_dynamic = 0;
15112 h->start_stop = 1;
15113 h->u2.start_stop_section = sec;
15114 if (symbol[0] == '.')
15115 {
15116 /* .startof. and .sizeof. symbols are local. */
15117 const struct elf_backend_data *bed;
15118 bed = get_elf_backend_data (info->output_bfd);
15119 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
15120 }
15121 else
15122 {
15123 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15124 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15125 | info->start_stop_visibility);
15126 if (was_dynamic)
15127 bfd_elf_link_record_dynamic_symbol (info, h);
15128 }
15129 return &h->root;
15130 }
15131 return NULL;
15132 }
15133
15134 /* Find dynamic relocs for H that apply to read-only sections. */
15135
15136 asection *
15137 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15138 {
15139 struct elf_dyn_relocs *p;
15140
15141 for (p = h->dyn_relocs; p != NULL; p = p->next)
15142 {
15143 asection *s = p->sec->output_section;
15144
15145 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15146 return p->sec;
15147 }
15148 return NULL;
15149 }
15150
15151 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15152 read-only sections. */
15153
15154 bfd_boolean
15155 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15156 {
15157 asection *sec;
15158
15159 if (h->root.type == bfd_link_hash_indirect)
15160 return TRUE;
15161
15162 sec = _bfd_elf_readonly_dynrelocs (h);
15163 if (sec != NULL)
15164 {
15165 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15166
15167 info->flags |= DF_TEXTREL;
15168 /* xgettext:c-format */
15169 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15170 "in read-only section `%pA'\n"),
15171 sec->owner, h->root.root.string, sec);
15172
15173 if (bfd_link_textrel_check (info))
15174 /* xgettext:c-format */
15175 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15176 "in read-only section `%pA'\n"),
15177 sec->owner, h->root.root.string, sec);
15178
15179 /* Not an error, just cut short the traversal. */
15180 return FALSE;
15181 }
15182 return TRUE;
15183 }
15184
15185 /* Add dynamic tags. */
15186
15187 bfd_boolean
15188 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15189 bfd_boolean need_dynamic_reloc)
15190 {
15191 struct elf_link_hash_table *htab = elf_hash_table (info);
15192
15193 if (htab->dynamic_sections_created)
15194 {
15195 /* Add some entries to the .dynamic section. We fill in the
15196 values later, in finish_dynamic_sections, but we must add
15197 the entries now so that we get the correct size for the
15198 .dynamic section. The DT_DEBUG entry is filled in by the
15199 dynamic linker and used by the debugger. */
15200 #define add_dynamic_entry(TAG, VAL) \
15201 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15202
15203 const struct elf_backend_data *bed
15204 = get_elf_backend_data (output_bfd);
15205
15206 if (bfd_link_executable (info))
15207 {
15208 if (!add_dynamic_entry (DT_DEBUG, 0))
15209 return FALSE;
15210 }
15211
15212 if (htab->dt_pltgot_required || htab->splt->size != 0)
15213 {
15214 /* DT_PLTGOT is used by prelink even if there is no PLT
15215 relocation. */
15216 if (!add_dynamic_entry (DT_PLTGOT, 0))
15217 return FALSE;
15218 }
15219
15220 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15221 {
15222 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15223 || !add_dynamic_entry (DT_PLTREL,
15224 (bed->rela_plts_and_copies_p
15225 ? DT_RELA : DT_REL))
15226 || !add_dynamic_entry (DT_JMPREL, 0))
15227 return FALSE;
15228 }
15229
15230 if (htab->tlsdesc_plt
15231 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15232 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15233 return FALSE;
15234
15235 if (need_dynamic_reloc)
15236 {
15237 if (bed->rela_plts_and_copies_p)
15238 {
15239 if (!add_dynamic_entry (DT_RELA, 0)
15240 || !add_dynamic_entry (DT_RELASZ, 0)
15241 || !add_dynamic_entry (DT_RELAENT,
15242 bed->s->sizeof_rela))
15243 return FALSE;
15244 }
15245 else
15246 {
15247 if (!add_dynamic_entry (DT_REL, 0)
15248 || !add_dynamic_entry (DT_RELSZ, 0)
15249 || !add_dynamic_entry (DT_RELENT,
15250 bed->s->sizeof_rel))
15251 return FALSE;
15252 }
15253
15254 /* If any dynamic relocs apply to a read-only section,
15255 then we need a DT_TEXTREL entry. */
15256 if ((info->flags & DF_TEXTREL) == 0)
15257 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15258 info);
15259
15260 if ((info->flags & DF_TEXTREL) != 0)
15261 {
15262 if (htab->ifunc_resolvers)
15263 info->callbacks->einfo
15264 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15265 "may result in a segfault at runtime; recompile with %s\n"),
15266 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15267
15268 if (!add_dynamic_entry (DT_TEXTREL, 0))
15269 return FALSE;
15270 }
15271 }
15272 }
15273 #undef add_dynamic_entry
15274
15275 return TRUE;
15276 }
This page took 0.358719 seconds and 5 git commands to generate.