Automatic date update in version.in
[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 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 return FALSE;
181 htab->sgotplt = s;
182 }
183
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
196 if (h == NULL)
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202 \f
203 /* Create a strtab to hold the dynamic symbol names. */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 asection *s;
220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
221 if ((ibfd->flags
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242 }
243
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
250
251 bfd_boolean
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 {
254 flagword flags;
255 asection *s;
256 const struct elf_backend_data *bed;
257 struct elf_link_hash_entry *h;
258
259 if (! is_elf_hash_table (info->hash))
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
267
268 abfd = elf_hash_table (info)->dynobj;
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info) && !info->nointerp)
276 {
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
279 if (s == NULL)
280 return FALSE;
281 }
282
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || !bfd_set_section_alignment (s, 1))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
301 return FALSE;
302
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
305 if (s == NULL
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
307 return FALSE;
308 elf_hash_table (info)->dynsym = s;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
312 if (s == NULL)
313 return FALSE;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
316 if (s == NULL
317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
329 return FALSE;
330
331 if (info->emit_hash)
332 {
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
335 if (s == NULL
336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
342 {
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
345 if (s == NULL
346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367 }
368
369 /* Create dynamic sections when linking against a dynamic object. */
370
371 bfd_boolean
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
373 {
374 flagword flags, pltflags;
375 struct elf_link_hash_entry *h;
376 asection *s;
377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378 struct elf_link_hash_table *htab = elf_hash_table (info);
379
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags = bed->dynamic_sec_flags;
383
384 pltflags = flags;
385 if (bed->plt_not_loaded)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
396 if (s == NULL
397 || !bfd_set_section_alignment (s, bed->plt_alignment))
398 return FALSE;
399 htab->splt = s;
400
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
411
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
416 if (s == NULL
417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
418 return FALSE;
419 htab->srelplt = s;
420
421 if (! _bfd_elf_create_got_section (abfd, info))
422 return FALSE;
423
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 SEC_ALLOC | SEC_LINKER_CREATED);
434 if (s == NULL)
435 return FALSE;
436 htab->sdynbss = s;
437
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 flags);
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
461 if (bfd_link_executable (info))
462 {
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return FALSE;
470 htab->srelbss = s;
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
483 }
484 }
485
486 return TRUE;
487 }
488 \f
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
497 bfd_boolean
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
500 {
501 if (h->dynindx == -1)
502 {
503 struct elf_strtab_hash *dynstr;
504 char *p;
505 const char *name;
506 size_t indx;
507
508 if (h->root.type == bfd_link_hash_defined
509 || h->root.type == bfd_link_hash_defweak)
510 {
511 /* An IR symbol should not be made dynamic. */
512 if (h->root.u.def.section != NULL
513 && h->root.u.def.section->owner != NULL
514 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
515 return TRUE;
516 }
517
518 /* XXX: The ABI draft says the linker must turn hidden and
519 internal symbols into STB_LOCAL symbols when producing the
520 DSO. However, if ld.so honors st_other in the dynamic table,
521 this would not be necessary. */
522 switch (ELF_ST_VISIBILITY (h->other))
523 {
524 case STV_INTERNAL:
525 case STV_HIDDEN:
526 if (h->root.type != bfd_link_hash_undefined
527 && h->root.type != bfd_link_hash_undefweak)
528 {
529 h->forced_local = 1;
530 if (!elf_hash_table (info)->is_relocatable_executable)
531 return TRUE;
532 }
533
534 default:
535 break;
536 }
537
538 h->dynindx = elf_hash_table (info)->dynsymcount;
539 ++elf_hash_table (info)->dynsymcount;
540
541 dynstr = elf_hash_table (info)->dynstr;
542 if (dynstr == NULL)
543 {
544 /* Create a strtab to hold the dynamic symbol names. */
545 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
546 if (dynstr == NULL)
547 return FALSE;
548 }
549
550 /* We don't put any version information in the dynamic string
551 table. */
552 name = h->root.root.string;
553 p = strchr (name, ELF_VER_CHR);
554 if (p != NULL)
555 /* We know that the p points into writable memory. In fact,
556 there are only a few symbols that have read-only names, being
557 those like _GLOBAL_OFFSET_TABLE_ that are created specially
558 by the backends. Most symbols will have names pointing into
559 an ELF string table read from a file, or to objalloc memory. */
560 *p = 0;
561
562 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
563
564 if (p != NULL)
565 *p = ELF_VER_CHR;
566
567 if (indx == (size_t) -1)
568 return FALSE;
569 h->dynstr_index = indx;
570 }
571
572 return TRUE;
573 }
574 \f
575 /* Mark a symbol dynamic. */
576
577 static void
578 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
579 struct elf_link_hash_entry *h,
580 Elf_Internal_Sym *sym)
581 {
582 struct bfd_elf_dynamic_list *d = info->dynamic_list;
583
584 /* It may be called more than once on the same H. */
585 if(h->dynamic || bfd_link_relocatable (info))
586 return;
587
588 if ((info->dynamic_data
589 && (h->type == STT_OBJECT
590 || h->type == STT_COMMON
591 || (sym != NULL
592 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
593 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
594 || (d != NULL
595 && h->non_elf
596 && (*d->match) (&d->head, NULL, h->root.root.string)))
597 {
598 h->dynamic = 1;
599 /* NB: If a symbol is made dynamic by --dynamic-list, it has
600 non-IR reference. */
601 h->root.non_ir_ref_dynamic = 1;
602 }
603 }
604
605 /* Record an assignment to a symbol made by a linker script. We need
606 this in case some dynamic object refers to this symbol. */
607
608 bfd_boolean
609 bfd_elf_record_link_assignment (bfd *output_bfd,
610 struct bfd_link_info *info,
611 const char *name,
612 bfd_boolean provide,
613 bfd_boolean hidden)
614 {
615 struct elf_link_hash_entry *h, *hv;
616 struct elf_link_hash_table *htab;
617 const struct elf_backend_data *bed;
618
619 if (!is_elf_hash_table (info->hash))
620 return TRUE;
621
622 htab = elf_hash_table (info);
623 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
624 if (h == NULL)
625 return provide;
626
627 if (h->root.type == bfd_link_hash_warning)
628 h = (struct elf_link_hash_entry *) h->root.u.i.link;
629
630 if (h->versioned == unknown)
631 {
632 /* Set versioned if symbol version is unknown. */
633 char *version = strrchr (name, ELF_VER_CHR);
634 if (version)
635 {
636 if (version > name && version[-1] != ELF_VER_CHR)
637 h->versioned = versioned_hidden;
638 else
639 h->versioned = versioned;
640 }
641 }
642
643 /* Symbols defined in a linker script but not referenced anywhere
644 else will have non_elf set. */
645 if (h->non_elf)
646 {
647 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
648 h->non_elf = 0;
649 }
650
651 switch (h->root.type)
652 {
653 case bfd_link_hash_defined:
654 case bfd_link_hash_defweak:
655 case bfd_link_hash_common:
656 break;
657 case bfd_link_hash_undefweak:
658 case bfd_link_hash_undefined:
659 /* Since we're defining the symbol, don't let it seem to have not
660 been defined. record_dynamic_symbol and size_dynamic_sections
661 may depend on this. */
662 h->root.type = bfd_link_hash_new;
663 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
664 bfd_link_repair_undef_list (&htab->root);
665 break;
666 case bfd_link_hash_new:
667 break;
668 case bfd_link_hash_indirect:
669 /* We had a versioned symbol in a dynamic library. We make the
670 the versioned symbol point to this one. */
671 bed = get_elf_backend_data (output_bfd);
672 hv = h;
673 while (hv->root.type == bfd_link_hash_indirect
674 || hv->root.type == bfd_link_hash_warning)
675 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
676 /* We don't need to update h->root.u since linker will set them
677 later. */
678 h->root.type = bfd_link_hash_undefined;
679 hv->root.type = bfd_link_hash_indirect;
680 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
681 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
682 break;
683 default:
684 BFD_FAIL ();
685 return FALSE;
686 }
687
688 /* If this symbol is being provided by the linker script, and it is
689 currently defined by a dynamic object, but not by a regular
690 object, then mark it as undefined so that the generic linker will
691 force the correct value. */
692 if (provide
693 && h->def_dynamic
694 && !h->def_regular)
695 h->root.type = bfd_link_hash_undefined;
696
697 /* If this symbol is currently defined by a dynamic object, but not
698 by a regular object, then clear out any version information because
699 the symbol will not be associated with the dynamic object any
700 more. */
701 if (h->def_dynamic && !h->def_regular)
702 h->verinfo.verdef = NULL;
703
704 /* Make sure this symbol is not garbage collected. */
705 h->mark = 1;
706
707 h->def_regular = 1;
708
709 if (hidden)
710 {
711 bed = get_elf_backend_data (output_bfd);
712 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
713 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
714 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
715 }
716
717 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
718 and executables. */
719 if (!bfd_link_relocatable (info)
720 && h->dynindx != -1
721 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
722 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
723 h->forced_local = 1;
724
725 if ((h->def_dynamic
726 || h->ref_dynamic
727 || bfd_link_dll (info)
728 || elf_hash_table (info)->is_relocatable_executable)
729 && !h->forced_local
730 && h->dynindx == -1)
731 {
732 if (! bfd_elf_link_record_dynamic_symbol (info, h))
733 return FALSE;
734
735 /* If this is a weak defined symbol, and we know a corresponding
736 real symbol from the same dynamic object, make sure the real
737 symbol is also made into a dynamic symbol. */
738 if (h->is_weakalias)
739 {
740 struct elf_link_hash_entry *def = weakdef (h);
741
742 if (def->dynindx == -1
743 && !bfd_elf_link_record_dynamic_symbol (info, def))
744 return FALSE;
745 }
746 }
747
748 return TRUE;
749 }
750
751 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
752 success, and 2 on a failure caused by attempting to record a symbol
753 in a discarded section, eg. a discarded link-once section symbol. */
754
755 int
756 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
757 bfd *input_bfd,
758 long input_indx)
759 {
760 size_t amt;
761 struct elf_link_local_dynamic_entry *entry;
762 struct elf_link_hash_table *eht;
763 struct elf_strtab_hash *dynstr;
764 size_t dynstr_index;
765 char *name;
766 Elf_External_Sym_Shndx eshndx;
767 char esym[sizeof (Elf64_External_Sym)];
768
769 if (! is_elf_hash_table (info->hash))
770 return 0;
771
772 /* See if the entry exists already. */
773 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
774 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
775 return 1;
776
777 amt = sizeof (*entry);
778 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
779 if (entry == NULL)
780 return 0;
781
782 /* Go find the symbol, so that we can find it's name. */
783 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
784 1, input_indx, &entry->isym, esym, &eshndx))
785 {
786 bfd_release (input_bfd, entry);
787 return 0;
788 }
789
790 if (entry->isym.st_shndx != SHN_UNDEF
791 && entry->isym.st_shndx < SHN_LORESERVE)
792 {
793 asection *s;
794
795 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
796 if (s == NULL || bfd_is_abs_section (s->output_section))
797 {
798 /* We can still bfd_release here as nothing has done another
799 bfd_alloc. We can't do this later in this function. */
800 bfd_release (input_bfd, entry);
801 return 2;
802 }
803 }
804
805 name = (bfd_elf_string_from_elf_section
806 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
807 entry->isym.st_name));
808
809 dynstr = elf_hash_table (info)->dynstr;
810 if (dynstr == NULL)
811 {
812 /* Create a strtab to hold the dynamic symbol names. */
813 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
814 if (dynstr == NULL)
815 return 0;
816 }
817
818 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
819 if (dynstr_index == (size_t) -1)
820 return 0;
821 entry->isym.st_name = dynstr_index;
822
823 eht = elf_hash_table (info);
824
825 entry->next = eht->dynlocal;
826 eht->dynlocal = entry;
827 entry->input_bfd = input_bfd;
828 entry->input_indx = input_indx;
829 eht->dynsymcount++;
830
831 /* Whatever binding the symbol had before, it's now local. */
832 entry->isym.st_info
833 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
834
835 /* The dynindx will be set at the end of size_dynamic_sections. */
836
837 return 1;
838 }
839
840 /* Return the dynindex of a local dynamic symbol. */
841
842 long
843 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
844 bfd *input_bfd,
845 long input_indx)
846 {
847 struct elf_link_local_dynamic_entry *e;
848
849 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
850 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
851 return e->dynindx;
852 return -1;
853 }
854
855 /* This function is used to renumber the dynamic symbols, if some of
856 them are removed because they are marked as local. This is called
857 via elf_link_hash_traverse. */
858
859 static bfd_boolean
860 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
861 void *data)
862 {
863 size_t *count = (size_t *) data;
864
865 if (h->forced_local)
866 return TRUE;
867
868 if (h->dynindx != -1)
869 h->dynindx = ++(*count);
870
871 return TRUE;
872 }
873
874
875 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
876 STB_LOCAL binding. */
877
878 static bfd_boolean
879 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
880 void *data)
881 {
882 size_t *count = (size_t *) data;
883
884 if (!h->forced_local)
885 return TRUE;
886
887 if (h->dynindx != -1)
888 h->dynindx = ++(*count);
889
890 return TRUE;
891 }
892
893 /* Return true if the dynamic symbol for a given section should be
894 omitted when creating a shared library. */
895 bfd_boolean
896 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
897 struct bfd_link_info *info,
898 asection *p)
899 {
900 struct elf_link_hash_table *htab;
901 asection *ip;
902
903 switch (elf_section_data (p)->this_hdr.sh_type)
904 {
905 case SHT_PROGBITS:
906 case SHT_NOBITS:
907 /* If sh_type is yet undecided, assume it could be
908 SHT_PROGBITS/SHT_NOBITS. */
909 case SHT_NULL:
910 htab = elf_hash_table (info);
911 if (htab->text_index_section != NULL)
912 return p != htab->text_index_section && p != htab->data_index_section;
913
914 return (htab->dynobj != NULL
915 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
916 && ip->output_section == p);
917
918 /* There shouldn't be section relative relocations
919 against any other section. */
920 default:
921 return TRUE;
922 }
923 }
924
925 bfd_boolean
926 _bfd_elf_omit_section_dynsym_all
927 (bfd *output_bfd ATTRIBUTE_UNUSED,
928 struct bfd_link_info *info ATTRIBUTE_UNUSED,
929 asection *p ATTRIBUTE_UNUSED)
930 {
931 return TRUE;
932 }
933
934 /* Assign dynsym indices. In a shared library we generate a section
935 symbol for each output section, which come first. Next come symbols
936 which have been forced to local binding. Then all of the back-end
937 allocated local dynamic syms, followed by the rest of the global
938 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
939 (This prevents the early call before elf_backend_init_index_section
940 and strip_excluded_output_sections setting dynindx for sections
941 that are stripped.) */
942
943 static unsigned long
944 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
945 struct bfd_link_info *info,
946 unsigned long *section_sym_count)
947 {
948 unsigned long dynsymcount = 0;
949 bfd_boolean do_sec = section_sym_count != NULL;
950
951 if (bfd_link_pic (info)
952 || elf_hash_table (info)->is_relocatable_executable)
953 {
954 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
955 asection *p;
956 for (p = output_bfd->sections; p ; p = p->next)
957 if ((p->flags & SEC_EXCLUDE) == 0
958 && (p->flags & SEC_ALLOC) != 0
959 && elf_hash_table (info)->dynamic_relocs
960 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
961 {
962 ++dynsymcount;
963 if (do_sec)
964 elf_section_data (p)->dynindx = dynsymcount;
965 }
966 else if (do_sec)
967 elf_section_data (p)->dynindx = 0;
968 }
969 if (do_sec)
970 *section_sym_count = dynsymcount;
971
972 elf_link_hash_traverse (elf_hash_table (info),
973 elf_link_renumber_local_hash_table_dynsyms,
974 &dynsymcount);
975
976 if (elf_hash_table (info)->dynlocal)
977 {
978 struct elf_link_local_dynamic_entry *p;
979 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
980 p->dynindx = ++dynsymcount;
981 }
982 elf_hash_table (info)->local_dynsymcount = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_hash_table_dynsyms,
986 &dynsymcount);
987
988 /* There is an unused NULL entry at the head of the table which we
989 must account for in our count even if the table is empty since it
990 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
991 .dynamic section. */
992 dynsymcount++;
993
994 elf_hash_table (info)->dynsymcount = dynsymcount;
995 return dynsymcount;
996 }
997
998 /* Merge st_other field. */
999
1000 static void
1001 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1002 const Elf_Internal_Sym *isym, asection *sec,
1003 bfd_boolean definition, bfd_boolean dynamic)
1004 {
1005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1006
1007 /* If st_other has a processor-specific meaning, specific
1008 code might be needed here. */
1009 if (bed->elf_backend_merge_symbol_attribute)
1010 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1011 dynamic);
1012
1013 if (!dynamic)
1014 {
1015 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1016 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1017
1018 /* Keep the most constraining visibility. Leave the remainder
1019 of the st_other field to elf_backend_merge_symbol_attribute. */
1020 if (symvis - 1 < hvis - 1)
1021 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1022 }
1023 else if (definition
1024 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1025 && (sec->flags & SEC_READONLY) == 0)
1026 h->protected_def = 1;
1027 }
1028
1029 /* This function is called when we want to merge a new symbol with an
1030 existing symbol. It handles the various cases which arise when we
1031 find a definition in a dynamic object, or when there is already a
1032 definition in a dynamic object. The new symbol is described by
1033 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1034 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1035 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1036 of an old common symbol. We set OVERRIDE if the old symbol is
1037 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1038 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1039 to change. By OK to change, we mean that we shouldn't warn if the
1040 type or size does change. */
1041
1042 static bfd_boolean
1043 _bfd_elf_merge_symbol (bfd *abfd,
1044 struct bfd_link_info *info,
1045 const char *name,
1046 Elf_Internal_Sym *sym,
1047 asection **psec,
1048 bfd_vma *pvalue,
1049 struct elf_link_hash_entry **sym_hash,
1050 bfd **poldbfd,
1051 bfd_boolean *pold_weak,
1052 unsigned int *pold_alignment,
1053 bfd_boolean *skip,
1054 bfd_boolean *override,
1055 bfd_boolean *type_change_ok,
1056 bfd_boolean *size_change_ok,
1057 bfd_boolean *matched)
1058 {
1059 asection *sec, *oldsec;
1060 struct elf_link_hash_entry *h;
1061 struct elf_link_hash_entry *hi;
1062 struct elf_link_hash_entry *flip;
1063 int bind;
1064 bfd *oldbfd;
1065 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1066 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1067 const struct elf_backend_data *bed;
1068 char *new_version;
1069 bfd_boolean default_sym = *matched;
1070
1071 *skip = FALSE;
1072 *override = FALSE;
1073
1074 sec = *psec;
1075 bind = ELF_ST_BIND (sym->st_info);
1076
1077 if (! bfd_is_und_section (sec))
1078 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1079 else
1080 h = ((struct elf_link_hash_entry *)
1081 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1082 if (h == NULL)
1083 return FALSE;
1084 *sym_hash = h;
1085
1086 bed = get_elf_backend_data (abfd);
1087
1088 /* NEW_VERSION is the symbol version of the new symbol. */
1089 if (h->versioned != unversioned)
1090 {
1091 /* Symbol version is unknown or versioned. */
1092 new_version = strrchr (name, ELF_VER_CHR);
1093 if (new_version)
1094 {
1095 if (h->versioned == unknown)
1096 {
1097 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1098 h->versioned = versioned_hidden;
1099 else
1100 h->versioned = versioned;
1101 }
1102 new_version += 1;
1103 if (new_version[0] == '\0')
1104 new_version = NULL;
1105 }
1106 else
1107 h->versioned = unversioned;
1108 }
1109 else
1110 new_version = NULL;
1111
1112 /* For merging, we only care about real symbols. But we need to make
1113 sure that indirect symbol dynamic flags are updated. */
1114 hi = h;
1115 while (h->root.type == bfd_link_hash_indirect
1116 || h->root.type == bfd_link_hash_warning)
1117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1118
1119 if (!*matched)
1120 {
1121 if (hi == h || h->root.type == bfd_link_hash_new)
1122 *matched = TRUE;
1123 else
1124 {
1125 /* OLD_HIDDEN is true if the existing symbol is only visible
1126 to the symbol with the same symbol version. NEW_HIDDEN is
1127 true if the new symbol is only visible to the symbol with
1128 the same symbol version. */
1129 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1130 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1131 if (!old_hidden && !new_hidden)
1132 /* The new symbol matches the existing symbol if both
1133 aren't hidden. */
1134 *matched = TRUE;
1135 else
1136 {
1137 /* OLD_VERSION is the symbol version of the existing
1138 symbol. */
1139 char *old_version;
1140
1141 if (h->versioned >= versioned)
1142 old_version = strrchr (h->root.root.string,
1143 ELF_VER_CHR) + 1;
1144 else
1145 old_version = NULL;
1146
1147 /* The new symbol matches the existing symbol if they
1148 have the same symbol version. */
1149 *matched = (old_version == new_version
1150 || (old_version != NULL
1151 && new_version != NULL
1152 && strcmp (old_version, new_version) == 0));
1153 }
1154 }
1155 }
1156
1157 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1158 existing symbol. */
1159
1160 oldbfd = NULL;
1161 oldsec = NULL;
1162 switch (h->root.type)
1163 {
1164 default:
1165 break;
1166
1167 case bfd_link_hash_undefined:
1168 case bfd_link_hash_undefweak:
1169 oldbfd = h->root.u.undef.abfd;
1170 break;
1171
1172 case bfd_link_hash_defined:
1173 case bfd_link_hash_defweak:
1174 oldbfd = h->root.u.def.section->owner;
1175 oldsec = h->root.u.def.section;
1176 break;
1177
1178 case bfd_link_hash_common:
1179 oldbfd = h->root.u.c.p->section->owner;
1180 oldsec = h->root.u.c.p->section;
1181 if (pold_alignment)
1182 *pold_alignment = h->root.u.c.p->alignment_power;
1183 break;
1184 }
1185 if (poldbfd && *poldbfd == NULL)
1186 *poldbfd = oldbfd;
1187
1188 /* Differentiate strong and weak symbols. */
1189 newweak = bind == STB_WEAK;
1190 oldweak = (h->root.type == bfd_link_hash_defweak
1191 || h->root.type == bfd_link_hash_undefweak);
1192 if (pold_weak)
1193 *pold_weak = oldweak;
1194
1195 /* We have to check it for every instance since the first few may be
1196 references and not all compilers emit symbol type for undefined
1197 symbols. */
1198 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1199
1200 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1201 respectively, is from a dynamic object. */
1202
1203 newdyn = (abfd->flags & DYNAMIC) != 0;
1204
1205 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1206 syms and defined syms in dynamic libraries respectively.
1207 ref_dynamic on the other hand can be set for a symbol defined in
1208 a dynamic library, and def_dynamic may not be set; When the
1209 definition in a dynamic lib is overridden by a definition in the
1210 executable use of the symbol in the dynamic lib becomes a
1211 reference to the executable symbol. */
1212 if (newdyn)
1213 {
1214 if (bfd_is_und_section (sec))
1215 {
1216 if (bind != STB_WEAK)
1217 {
1218 h->ref_dynamic_nonweak = 1;
1219 hi->ref_dynamic_nonweak = 1;
1220 }
1221 }
1222 else
1223 {
1224 /* Update the existing symbol only if they match. */
1225 if (*matched)
1226 h->dynamic_def = 1;
1227 hi->dynamic_def = 1;
1228 }
1229 }
1230
1231 /* If we just created the symbol, mark it as being an ELF symbol.
1232 Other than that, there is nothing to do--there is no merge issue
1233 with a newly defined symbol--so we just return. */
1234
1235 if (h->root.type == bfd_link_hash_new)
1236 {
1237 h->non_elf = 0;
1238 return TRUE;
1239 }
1240
1241 /* In cases involving weak versioned symbols, we may wind up trying
1242 to merge a symbol with itself. Catch that here, to avoid the
1243 confusion that results if we try to override a symbol with
1244 itself. The additional tests catch cases like
1245 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1246 dynamic object, which we do want to handle here. */
1247 if (abfd == oldbfd
1248 && (newweak || oldweak)
1249 && ((abfd->flags & DYNAMIC) == 0
1250 || !h->def_regular))
1251 return TRUE;
1252
1253 olddyn = FALSE;
1254 if (oldbfd != NULL)
1255 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1256 else if (oldsec != NULL)
1257 {
1258 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1259 indices used by MIPS ELF. */
1260 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1261 }
1262
1263 /* Handle a case where plugin_notice won't be called and thus won't
1264 set the non_ir_ref flags on the first pass over symbols. */
1265 if (oldbfd != NULL
1266 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1267 && newdyn != olddyn)
1268 {
1269 h->root.non_ir_ref_dynamic = TRUE;
1270 hi->root.non_ir_ref_dynamic = TRUE;
1271 }
1272
1273 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1274 respectively, appear to be a definition rather than reference. */
1275
1276 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1277
1278 olddef = (h->root.type != bfd_link_hash_undefined
1279 && h->root.type != bfd_link_hash_undefweak
1280 && h->root.type != bfd_link_hash_common);
1281
1282 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1283 respectively, appear to be a function. */
1284
1285 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1287
1288 oldfunc = (h->type != STT_NOTYPE
1289 && bed->is_function_type (h->type));
1290
1291 if (!(newfunc && oldfunc)
1292 && ELF_ST_TYPE (sym->st_info) != h->type
1293 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1294 && h->type != STT_NOTYPE
1295 && (newdef || bfd_is_com_section (sec))
1296 && (olddef || h->root.type == bfd_link_hash_common))
1297 {
1298 /* If creating a default indirect symbol ("foo" or "foo@") from
1299 a dynamic versioned definition ("foo@@") skip doing so if
1300 there is an existing regular definition with a different
1301 type. We don't want, for example, a "time" variable in the
1302 executable overriding a "time" function in a shared library. */
1303 if (newdyn
1304 && !olddyn)
1305 {
1306 *skip = TRUE;
1307 return TRUE;
1308 }
1309
1310 /* When adding a symbol from a regular object file after we have
1311 created indirect symbols, undo the indirection and any
1312 dynamic state. */
1313 if (hi != h
1314 && !newdyn
1315 && olddyn)
1316 {
1317 h = hi;
1318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1319 h->forced_local = 0;
1320 h->ref_dynamic = 0;
1321 h->def_dynamic = 0;
1322 h->dynamic_def = 0;
1323 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1324 {
1325 h->root.type = bfd_link_hash_undefined;
1326 h->root.u.undef.abfd = abfd;
1327 }
1328 else
1329 {
1330 h->root.type = bfd_link_hash_new;
1331 h->root.u.undef.abfd = NULL;
1332 }
1333 return TRUE;
1334 }
1335 }
1336
1337 /* Check TLS symbols. We don't check undefined symbols introduced
1338 by "ld -u" which have no type (and oldbfd NULL), and we don't
1339 check symbols from plugins because they also have no type. */
1340 if (oldbfd != NULL
1341 && (oldbfd->flags & BFD_PLUGIN) == 0
1342 && (abfd->flags & BFD_PLUGIN) == 0
1343 && ELF_ST_TYPE (sym->st_info) != h->type
1344 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1345 {
1346 bfd *ntbfd, *tbfd;
1347 bfd_boolean ntdef, tdef;
1348 asection *ntsec, *tsec;
1349
1350 if (h->type == STT_TLS)
1351 {
1352 ntbfd = abfd;
1353 ntsec = sec;
1354 ntdef = newdef;
1355 tbfd = oldbfd;
1356 tsec = oldsec;
1357 tdef = olddef;
1358 }
1359 else
1360 {
1361 ntbfd = oldbfd;
1362 ntsec = oldsec;
1363 ntdef = olddef;
1364 tbfd = abfd;
1365 tsec = sec;
1366 tdef = newdef;
1367 }
1368
1369 if (tdef && ntdef)
1370 _bfd_error_handler
1371 /* xgettext:c-format */
1372 (_("%s: TLS definition in %pB section %pA "
1373 "mismatches non-TLS definition in %pB section %pA"),
1374 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1375 else if (!tdef && !ntdef)
1376 _bfd_error_handler
1377 /* xgettext:c-format */
1378 (_("%s: TLS reference in %pB "
1379 "mismatches non-TLS reference in %pB"),
1380 h->root.root.string, tbfd, ntbfd);
1381 else if (tdef)
1382 _bfd_error_handler
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS reference in %pB"),
1386 h->root.root.string, tbfd, tsec, ntbfd);
1387 else
1388 _bfd_error_handler
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS definition in %pB section %pA"),
1392 h->root.root.string, tbfd, ntbfd, ntsec);
1393
1394 bfd_set_error (bfd_error_bad_value);
1395 return FALSE;
1396 }
1397
1398 /* If the old symbol has non-default visibility, we ignore the new
1399 definition from a dynamic object. */
1400 if (newdyn
1401 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1402 && !bfd_is_und_section (sec))
1403 {
1404 *skip = TRUE;
1405 /* Make sure this symbol is dynamic. */
1406 h->ref_dynamic = 1;
1407 hi->ref_dynamic = 1;
1408 /* A protected symbol has external availability. Make sure it is
1409 recorded as dynamic.
1410
1411 FIXME: Should we check type and size for protected symbol? */
1412 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1413 return bfd_elf_link_record_dynamic_symbol (info, h);
1414 else
1415 return TRUE;
1416 }
1417 else if (!newdyn
1418 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1419 && h->def_dynamic)
1420 {
1421 /* If the new symbol with non-default visibility comes from a
1422 relocatable file and the old definition comes from a dynamic
1423 object, we remove the old definition. */
1424 if (hi->root.type == bfd_link_hash_indirect)
1425 {
1426 /* Handle the case where the old dynamic definition is
1427 default versioned. We need to copy the symbol info from
1428 the symbol with default version to the normal one if it
1429 was referenced before. */
1430 if (h->ref_regular)
1431 {
1432 hi->root.type = h->root.type;
1433 h->root.type = bfd_link_hash_indirect;
1434 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1435
1436 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1438 {
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1443 h->ref_dynamic = 0;
1444 }
1445 else
1446 h->ref_dynamic = 1;
1447
1448 h->def_dynamic = 0;
1449 /* FIXME: Should we check type and size for protected symbol? */
1450 h->size = 0;
1451 h->type = 0;
1452
1453 h = hi;
1454 }
1455 else
1456 h = hi;
1457 }
1458
1459 /* If the old symbol was undefined before, then it will still be
1460 on the undefs list. If the new symbol is undefined or
1461 common, we can't make it bfd_link_hash_new here, because new
1462 undefined or common symbols will be added to the undefs list
1463 by _bfd_generic_link_add_one_symbol. Symbols may not be
1464 added twice to the undefs list. Also, if the new symbol is
1465 undefweak then we don't want to lose the strong undef. */
1466 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1467 {
1468 h->root.type = bfd_link_hash_undefined;
1469 h->root.u.undef.abfd = abfd;
1470 }
1471 else
1472 {
1473 h->root.type = bfd_link_hash_new;
1474 h->root.u.undef.abfd = NULL;
1475 }
1476
1477 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1478 {
1479 /* If the new symbol is hidden or internal, completely undo
1480 any dynamic link state. */
1481 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1482 h->forced_local = 0;
1483 h->ref_dynamic = 0;
1484 }
1485 else
1486 h->ref_dynamic = 1;
1487 h->def_dynamic = 0;
1488 /* FIXME: Should we check type and size for protected symbol? */
1489 h->size = 0;
1490 h->type = 0;
1491 return TRUE;
1492 }
1493
1494 /* If a new weak symbol definition comes from a regular file and the
1495 old symbol comes from a dynamic library, we treat the new one as
1496 strong. Similarly, an old weak symbol definition from a regular
1497 file is treated as strong when the new symbol comes from a dynamic
1498 library. Further, an old weak symbol from a dynamic library is
1499 treated as strong if the new symbol is from a dynamic library.
1500 This reflects the way glibc's ld.so works.
1501
1502 Also allow a weak symbol to override a linker script symbol
1503 defined by an early pass over the script. This is done so the
1504 linker knows the symbol is defined in an object file, for the
1505 DEFINED script function.
1506
1507 Do this before setting *type_change_ok or *size_change_ok so that
1508 we warn properly when dynamic library symbols are overridden. */
1509
1510 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1511 newweak = FALSE;
1512 if (olddef && newdyn)
1513 oldweak = FALSE;
1514
1515 /* Allow changes between different types of function symbol. */
1516 if (newfunc && oldfunc)
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the type if either the existing symbol or the
1520 new symbol is weak. A type change is also OK if the old symbol
1521 is undefined and the new symbol is defined. */
1522
1523 if (oldweak
1524 || newweak
1525 || (newdef
1526 && h->root.type == bfd_link_hash_undefined))
1527 *type_change_ok = TRUE;
1528
1529 /* It's OK to change the size if either the existing symbol or the
1530 new symbol is weak, or if the old symbol is undefined. */
1531
1532 if (*type_change_ok
1533 || h->root.type == bfd_link_hash_undefined)
1534 *size_change_ok = TRUE;
1535
1536 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1537 symbol, respectively, appears to be a common symbol in a dynamic
1538 object. If a symbol appears in an uninitialized section, and is
1539 not weak, and is not a function, then it may be a common symbol
1540 which was resolved when the dynamic object was created. We want
1541 to treat such symbols specially, because they raise special
1542 considerations when setting the symbol size: if the symbol
1543 appears as a common symbol in a regular object, and the size in
1544 the regular object is larger, we must make sure that we use the
1545 larger size. This problematic case can always be avoided in C,
1546 but it must be handled correctly when using Fortran shared
1547 libraries.
1548
1549 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1550 likewise for OLDDYNCOMMON and OLDDEF.
1551
1552 Note that this test is just a heuristic, and that it is quite
1553 possible to have an uninitialized symbol in a shared object which
1554 is really a definition, rather than a common symbol. This could
1555 lead to some minor confusion when the symbol really is a common
1556 symbol in some regular object. However, I think it will be
1557 harmless. */
1558
1559 if (newdyn
1560 && newdef
1561 && !newweak
1562 && (sec->flags & SEC_ALLOC) != 0
1563 && (sec->flags & SEC_LOAD) == 0
1564 && sym->st_size > 0
1565 && !newfunc)
1566 newdyncommon = TRUE;
1567 else
1568 newdyncommon = FALSE;
1569
1570 if (olddyn
1571 && olddef
1572 && h->root.type == bfd_link_hash_defined
1573 && h->def_dynamic
1574 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1575 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1576 && h->size > 0
1577 && !oldfunc)
1578 olddyncommon = TRUE;
1579 else
1580 olddyncommon = FALSE;
1581
1582 /* We now know everything about the old and new symbols. We ask the
1583 backend to check if we can merge them. */
1584 if (bed->merge_symbol != NULL)
1585 {
1586 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1587 return FALSE;
1588 sec = *psec;
1589 }
1590
1591 /* There are multiple definitions of a normal symbol. Skip the
1592 default symbol as well as definition from an IR object. */
1593 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1594 && !default_sym && h->def_regular
1595 && !(oldbfd != NULL
1596 && (oldbfd->flags & BFD_PLUGIN) != 0
1597 && (abfd->flags & BFD_PLUGIN) == 0))
1598 {
1599 /* Handle a multiple definition. */
1600 (*info->callbacks->multiple_definition) (info, &h->root,
1601 abfd, sec, *pvalue);
1602 *skip = TRUE;
1603 return TRUE;
1604 }
1605
1606 /* If both the old and the new symbols look like common symbols in a
1607 dynamic object, set the size of the symbol to the larger of the
1608 two. */
1609
1610 if (olddyncommon
1611 && newdyncommon
1612 && sym->st_size != h->size)
1613 {
1614 /* Since we think we have two common symbols, issue a multiple
1615 common warning if desired. Note that we only warn if the
1616 size is different. If the size is the same, we simply let
1617 the old symbol override the new one as normally happens with
1618 symbols defined in dynamic objects. */
1619
1620 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1621 bfd_link_hash_common, sym->st_size);
1622 if (sym->st_size > h->size)
1623 h->size = sym->st_size;
1624
1625 *size_change_ok = TRUE;
1626 }
1627
1628 /* If we are looking at a dynamic object, and we have found a
1629 definition, we need to see if the symbol was already defined by
1630 some other object. If so, we want to use the existing
1631 definition, and we do not want to report a multiple symbol
1632 definition error; we do this by clobbering *PSEC to be
1633 bfd_und_section_ptr.
1634
1635 We treat a common symbol as a definition if the symbol in the
1636 shared library is a function, since common symbols always
1637 represent variables; this can cause confusion in principle, but
1638 any such confusion would seem to indicate an erroneous program or
1639 shared library. We also permit a common symbol in a regular
1640 object to override a weak symbol in a shared object. */
1641
1642 if (newdyn
1643 && newdef
1644 && (olddef
1645 || (h->root.type == bfd_link_hash_common
1646 && (newweak || newfunc))))
1647 {
1648 *override = TRUE;
1649 newdef = FALSE;
1650 newdyncommon = FALSE;
1651
1652 *psec = sec = bfd_und_section_ptr;
1653 *size_change_ok = TRUE;
1654
1655 /* If we get here when the old symbol is a common symbol, then
1656 we are explicitly letting it override a weak symbol or
1657 function in a dynamic object, and we don't want to warn about
1658 a type change. If the old symbol is a defined symbol, a type
1659 change warning may still be appropriate. */
1660
1661 if (h->root.type == bfd_link_hash_common)
1662 *type_change_ok = TRUE;
1663 }
1664
1665 /* Handle the special case of an old common symbol merging with a
1666 new symbol which looks like a common symbol in a shared object.
1667 We change *PSEC and *PVALUE to make the new symbol look like a
1668 common symbol, and let _bfd_generic_link_add_one_symbol do the
1669 right thing. */
1670
1671 if (newdyncommon
1672 && h->root.type == bfd_link_hash_common)
1673 {
1674 *override = TRUE;
1675 newdef = FALSE;
1676 newdyncommon = FALSE;
1677 *pvalue = sym->st_size;
1678 *psec = sec = bed->common_section (oldsec);
1679 *size_change_ok = TRUE;
1680 }
1681
1682 /* Skip weak definitions of symbols that are already defined. */
1683 if (newdef && olddef && newweak)
1684 {
1685 /* Don't skip new non-IR weak syms. */
1686 if (!(oldbfd != NULL
1687 && (oldbfd->flags & BFD_PLUGIN) != 0
1688 && (abfd->flags & BFD_PLUGIN) == 0))
1689 {
1690 newdef = FALSE;
1691 *skip = TRUE;
1692 }
1693
1694 /* Merge st_other. If the symbol already has a dynamic index,
1695 but visibility says it should not be visible, turn it into a
1696 local symbol. */
1697 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1698 if (h->dynindx != -1)
1699 switch (ELF_ST_VISIBILITY (h->other))
1700 {
1701 case STV_INTERNAL:
1702 case STV_HIDDEN:
1703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1704 break;
1705 }
1706 }
1707
1708 /* If the old symbol is from a dynamic object, and the new symbol is
1709 a definition which is not from a dynamic object, then the new
1710 symbol overrides the old symbol. Symbols from regular files
1711 always take precedence over symbols from dynamic objects, even if
1712 they are defined after the dynamic object in the link.
1713
1714 As above, we again permit a common symbol in a regular object to
1715 override a definition in a shared object if the shared object
1716 symbol is a function or is weak. */
1717
1718 flip = NULL;
1719 if (!newdyn
1720 && (newdef
1721 || (bfd_is_com_section (sec)
1722 && (oldweak || oldfunc)))
1723 && olddyn
1724 && olddef
1725 && h->def_dynamic)
1726 {
1727 /* Change the hash table entry to undefined, and let
1728 _bfd_generic_link_add_one_symbol do the right thing with the
1729 new definition. */
1730
1731 h->root.type = bfd_link_hash_undefined;
1732 h->root.u.undef.abfd = h->root.u.def.section->owner;
1733 *size_change_ok = TRUE;
1734
1735 olddef = FALSE;
1736 olddyncommon = FALSE;
1737
1738 /* We again permit a type change when a common symbol may be
1739 overriding a function. */
1740
1741 if (bfd_is_com_section (sec))
1742 {
1743 if (oldfunc)
1744 {
1745 /* If a common symbol overrides a function, make sure
1746 that it isn't defined dynamically nor has type
1747 function. */
1748 h->def_dynamic = 0;
1749 h->type = STT_NOTYPE;
1750 }
1751 *type_change_ok = TRUE;
1752 }
1753
1754 if (hi->root.type == bfd_link_hash_indirect)
1755 flip = hi;
1756 else
1757 /* This union may have been set to be non-NULL when this symbol
1758 was seen in a dynamic object. We must force the union to be
1759 NULL, so that it is correct for a regular symbol. */
1760 h->verinfo.vertree = NULL;
1761 }
1762
1763 /* Handle the special case of a new common symbol merging with an
1764 old symbol that looks like it might be a common symbol defined in
1765 a shared object. Note that we have already handled the case in
1766 which a new common symbol should simply override the definition
1767 in the shared library. */
1768
1769 if (! newdyn
1770 && bfd_is_com_section (sec)
1771 && olddyncommon)
1772 {
1773 /* It would be best if we could set the hash table entry to a
1774 common symbol, but we don't know what to use for the section
1775 or the alignment. */
1776 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1777 bfd_link_hash_common, sym->st_size);
1778
1779 /* If the presumed common symbol in the dynamic object is
1780 larger, pretend that the new symbol has its size. */
1781
1782 if (h->size > *pvalue)
1783 *pvalue = h->size;
1784
1785 /* We need to remember the alignment required by the symbol
1786 in the dynamic object. */
1787 BFD_ASSERT (pold_alignment);
1788 *pold_alignment = h->root.u.def.section->alignment_power;
1789
1790 olddef = FALSE;
1791 olddyncommon = FALSE;
1792
1793 h->root.type = bfd_link_hash_undefined;
1794 h->root.u.undef.abfd = h->root.u.def.section->owner;
1795
1796 *size_change_ok = TRUE;
1797 *type_change_ok = TRUE;
1798
1799 if (hi->root.type == bfd_link_hash_indirect)
1800 flip = hi;
1801 else
1802 h->verinfo.vertree = NULL;
1803 }
1804
1805 if (flip != NULL)
1806 {
1807 /* Handle the case where we had a versioned symbol in a dynamic
1808 library and now find a definition in a normal object. In this
1809 case, we make the versioned symbol point to the normal one. */
1810 flip->root.type = h->root.type;
1811 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1812 h->root.type = bfd_link_hash_indirect;
1813 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1814 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1815 if (h->def_dynamic)
1816 {
1817 h->def_dynamic = 0;
1818 flip->ref_dynamic = 1;
1819 }
1820 }
1821
1822 return TRUE;
1823 }
1824
1825 /* This function is called to create an indirect symbol from the
1826 default for the symbol with the default version if needed. The
1827 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1828 set DYNSYM if the new indirect symbol is dynamic. */
1829
1830 static bfd_boolean
1831 _bfd_elf_add_default_symbol (bfd *abfd,
1832 struct bfd_link_info *info,
1833 struct elf_link_hash_entry *h,
1834 const char *name,
1835 Elf_Internal_Sym *sym,
1836 asection *sec,
1837 bfd_vma value,
1838 bfd **poldbfd,
1839 bfd_boolean *dynsym)
1840 {
1841 bfd_boolean type_change_ok;
1842 bfd_boolean size_change_ok;
1843 bfd_boolean skip;
1844 char *shortname;
1845 struct elf_link_hash_entry *hi;
1846 struct bfd_link_hash_entry *bh;
1847 const struct elf_backend_data *bed;
1848 bfd_boolean collect;
1849 bfd_boolean dynamic;
1850 bfd_boolean override;
1851 char *p;
1852 size_t len, shortlen;
1853 asection *tmp_sec;
1854 bfd_boolean matched;
1855
1856 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1857 return TRUE;
1858
1859 /* If this symbol has a version, and it is the default version, we
1860 create an indirect symbol from the default name to the fully
1861 decorated name. This will cause external references which do not
1862 specify a version to be bound to this version of the symbol. */
1863 p = strchr (name, ELF_VER_CHR);
1864 if (h->versioned == unknown)
1865 {
1866 if (p == NULL)
1867 {
1868 h->versioned = unversioned;
1869 return TRUE;
1870 }
1871 else
1872 {
1873 if (p[1] != ELF_VER_CHR)
1874 {
1875 h->versioned = versioned_hidden;
1876 return TRUE;
1877 }
1878 else
1879 h->versioned = versioned;
1880 }
1881 }
1882 else
1883 {
1884 /* PR ld/19073: We may see an unversioned definition after the
1885 default version. */
1886 if (p == NULL)
1887 return TRUE;
1888 }
1889
1890 bed = get_elf_backend_data (abfd);
1891 collect = bed->collect;
1892 dynamic = (abfd->flags & DYNAMIC) != 0;
1893
1894 shortlen = p - name;
1895 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1896 if (shortname == NULL)
1897 return FALSE;
1898 memcpy (shortname, name, shortlen);
1899 shortname[shortlen] = '\0';
1900
1901 /* We are going to create a new symbol. Merge it with any existing
1902 symbol with this name. For the purposes of the merge, act as
1903 though we were defining the symbol we just defined, although we
1904 actually going to define an indirect symbol. */
1905 type_change_ok = FALSE;
1906 size_change_ok = FALSE;
1907 matched = TRUE;
1908 tmp_sec = sec;
1909 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1910 &hi, poldbfd, NULL, NULL, &skip, &override,
1911 &type_change_ok, &size_change_ok, &matched))
1912 return FALSE;
1913
1914 if (skip)
1915 goto nondefault;
1916
1917 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1918 {
1919 /* If the undecorated symbol will have a version added by a
1920 script different to H, then don't indirect to/from the
1921 undecorated symbol. This isn't ideal because we may not yet
1922 have seen symbol versions, if given by a script on the
1923 command line rather than via --version-script. */
1924 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1925 {
1926 bfd_boolean hide;
1927
1928 hi->verinfo.vertree
1929 = bfd_find_version_for_sym (info->version_info,
1930 hi->root.root.string, &hide);
1931 if (hi->verinfo.vertree != NULL && hide)
1932 {
1933 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1934 goto nondefault;
1935 }
1936 }
1937 if (hi->verinfo.vertree != NULL
1938 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1939 goto nondefault;
1940 }
1941
1942 if (! override)
1943 {
1944 /* Add the default symbol if not performing a relocatable link. */
1945 if (! bfd_link_relocatable (info))
1946 {
1947 bh = &hi->root;
1948 if (bh->type == bfd_link_hash_defined
1949 && bh->u.def.section->owner != NULL
1950 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1951 {
1952 /* Mark the previous definition from IR object as
1953 undefined so that the generic linker will override
1954 it. */
1955 bh->type = bfd_link_hash_undefined;
1956 bh->u.undef.abfd = bh->u.def.section->owner;
1957 }
1958 if (! (_bfd_generic_link_add_one_symbol
1959 (info, abfd, shortname, BSF_INDIRECT,
1960 bfd_ind_section_ptr,
1961 0, name, FALSE, collect, &bh)))
1962 return FALSE;
1963 hi = (struct elf_link_hash_entry *) bh;
1964 }
1965 }
1966 else
1967 {
1968 /* In this case the symbol named SHORTNAME is overriding the
1969 indirect symbol we want to add. We were planning on making
1970 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1971 is the name without a version. NAME is the fully versioned
1972 name, and it is the default version.
1973
1974 Overriding means that we already saw a definition for the
1975 symbol SHORTNAME in a regular object, and it is overriding
1976 the symbol defined in the dynamic object.
1977
1978 When this happens, we actually want to change NAME, the
1979 symbol we just added, to refer to SHORTNAME. This will cause
1980 references to NAME in the shared object to become references
1981 to SHORTNAME in the regular object. This is what we expect
1982 when we override a function in a shared object: that the
1983 references in the shared object will be mapped to the
1984 definition in the regular object. */
1985
1986 while (hi->root.type == bfd_link_hash_indirect
1987 || hi->root.type == bfd_link_hash_warning)
1988 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1989
1990 h->root.type = bfd_link_hash_indirect;
1991 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1992 if (h->def_dynamic)
1993 {
1994 h->def_dynamic = 0;
1995 hi->ref_dynamic = 1;
1996 if (hi->ref_regular
1997 || hi->def_regular)
1998 {
1999 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2000 return FALSE;
2001 }
2002 }
2003
2004 /* Now set HI to H, so that the following code will set the
2005 other fields correctly. */
2006 hi = h;
2007 }
2008
2009 /* Check if HI is a warning symbol. */
2010 if (hi->root.type == bfd_link_hash_warning)
2011 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012
2013 /* If there is a duplicate definition somewhere, then HI may not
2014 point to an indirect symbol. We will have reported an error to
2015 the user in that case. */
2016
2017 if (hi->root.type == bfd_link_hash_indirect)
2018 {
2019 struct elf_link_hash_entry *ht;
2020
2021 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2022 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2023
2024 /* A reference to the SHORTNAME symbol from a dynamic library
2025 will be satisfied by the versioned symbol at runtime. In
2026 effect, we have a reference to the versioned symbol. */
2027 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2028 hi->dynamic_def |= ht->dynamic_def;
2029
2030 /* See if the new flags lead us to realize that the symbol must
2031 be dynamic. */
2032 if (! *dynsym)
2033 {
2034 if (! dynamic)
2035 {
2036 if (! bfd_link_executable (info)
2037 || hi->def_dynamic
2038 || hi->ref_dynamic)
2039 *dynsym = TRUE;
2040 }
2041 else
2042 {
2043 if (hi->ref_regular)
2044 *dynsym = TRUE;
2045 }
2046 }
2047 }
2048
2049 /* We also need to define an indirection from the nondefault version
2050 of the symbol. */
2051
2052 nondefault:
2053 len = strlen (name);
2054 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2055 if (shortname == NULL)
2056 return FALSE;
2057 memcpy (shortname, name, shortlen);
2058 memcpy (shortname + shortlen, p + 1, len - shortlen);
2059
2060 /* Once again, merge with any existing symbol. */
2061 type_change_ok = FALSE;
2062 size_change_ok = FALSE;
2063 tmp_sec = sec;
2064 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2065 &hi, poldbfd, NULL, NULL, &skip, &override,
2066 &type_change_ok, &size_change_ok, &matched))
2067 return FALSE;
2068
2069 if (skip)
2070 return TRUE;
2071
2072 if (override)
2073 {
2074 /* Here SHORTNAME is a versioned name, so we don't expect to see
2075 the type of override we do in the case above unless it is
2076 overridden by a versioned definition. */
2077 if (hi->root.type != bfd_link_hash_defined
2078 && hi->root.type != bfd_link_hash_defweak)
2079 _bfd_error_handler
2080 /* xgettext:c-format */
2081 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2082 abfd, shortname);
2083 }
2084 else
2085 {
2086 bh = &hi->root;
2087 if (! (_bfd_generic_link_add_one_symbol
2088 (info, abfd, shortname, BSF_INDIRECT,
2089 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2090 return FALSE;
2091 hi = (struct elf_link_hash_entry *) bh;
2092
2093 /* If there is a duplicate definition somewhere, then HI may not
2094 point to an indirect symbol. We will have reported an error
2095 to the user in that case. */
2096
2097 if (hi->root.type == bfd_link_hash_indirect)
2098 {
2099 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2100 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2101 hi->dynamic_def |= h->dynamic_def;
2102
2103 /* See if the new flags lead us to realize that the symbol
2104 must be dynamic. */
2105 if (! *dynsym)
2106 {
2107 if (! dynamic)
2108 {
2109 if (! bfd_link_executable (info)
2110 || hi->ref_dynamic)
2111 *dynsym = TRUE;
2112 }
2113 else
2114 {
2115 if (hi->ref_regular)
2116 *dynsym = TRUE;
2117 }
2118 }
2119 }
2120 }
2121
2122 return TRUE;
2123 }
2124 \f
2125 /* This routine is used to export all defined symbols into the dynamic
2126 symbol table. It is called via elf_link_hash_traverse. */
2127
2128 static bfd_boolean
2129 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2130 {
2131 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2132
2133 /* Ignore indirect symbols. These are added by the versioning code. */
2134 if (h->root.type == bfd_link_hash_indirect)
2135 return TRUE;
2136
2137 /* Ignore this if we won't export it. */
2138 if (!eif->info->export_dynamic && !h->dynamic)
2139 return TRUE;
2140
2141 if (h->dynindx == -1
2142 && (h->def_regular || h->ref_regular)
2143 && ! bfd_hide_sym_by_version (eif->info->version_info,
2144 h->root.root.string))
2145 {
2146 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2147 {
2148 eif->failed = TRUE;
2149 return FALSE;
2150 }
2151 }
2152
2153 return TRUE;
2154 }
2155 \f
2156 /* Look through the symbols which are defined in other shared
2157 libraries and referenced here. Update the list of version
2158 dependencies. This will be put into the .gnu.version_r section.
2159 This function is called via elf_link_hash_traverse. */
2160
2161 static bfd_boolean
2162 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2163 void *data)
2164 {
2165 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2166 Elf_Internal_Verneed *t;
2167 Elf_Internal_Vernaux *a;
2168 size_t amt;
2169
2170 /* We only care about symbols defined in shared objects with version
2171 information. */
2172 if (!h->def_dynamic
2173 || h->def_regular
2174 || h->dynindx == -1
2175 || h->verinfo.verdef == NULL
2176 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2177 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2178 return TRUE;
2179
2180 /* See if we already know about this version. */
2181 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2182 t != NULL;
2183 t = t->vn_nextref)
2184 {
2185 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2186 continue;
2187
2188 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2189 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2190 return TRUE;
2191
2192 break;
2193 }
2194
2195 /* This is a new version. Add it to tree we are building. */
2196
2197 if (t == NULL)
2198 {
2199 amt = sizeof *t;
2200 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2201 if (t == NULL)
2202 {
2203 rinfo->failed = TRUE;
2204 return FALSE;
2205 }
2206
2207 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2208 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2209 elf_tdata (rinfo->info->output_bfd)->verref = t;
2210 }
2211
2212 amt = sizeof *a;
2213 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2214 if (a == NULL)
2215 {
2216 rinfo->failed = TRUE;
2217 return FALSE;
2218 }
2219
2220 /* Note that we are copying a string pointer here, and testing it
2221 above. If bfd_elf_string_from_elf_section is ever changed to
2222 discard the string data when low in memory, this will have to be
2223 fixed. */
2224 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2225
2226 a->vna_flags = h->verinfo.verdef->vd_flags;
2227 a->vna_nextptr = t->vn_auxptr;
2228
2229 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2230 ++rinfo->vers;
2231
2232 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2233
2234 t->vn_auxptr = a;
2235
2236 return TRUE;
2237 }
2238
2239 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2240 hidden. Set *T_P to NULL if there is no match. */
2241
2242 static bfd_boolean
2243 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2244 struct elf_link_hash_entry *h,
2245 const char *version_p,
2246 struct bfd_elf_version_tree **t_p,
2247 bfd_boolean *hide)
2248 {
2249 struct bfd_elf_version_tree *t;
2250
2251 /* Look for the version. If we find it, it is no longer weak. */
2252 for (t = info->version_info; t != NULL; t = t->next)
2253 {
2254 if (strcmp (t->name, version_p) == 0)
2255 {
2256 size_t len;
2257 char *alc;
2258 struct bfd_elf_version_expr *d;
2259
2260 len = version_p - h->root.root.string;
2261 alc = (char *) bfd_malloc (len);
2262 if (alc == NULL)
2263 return FALSE;
2264 memcpy (alc, h->root.root.string, len - 1);
2265 alc[len - 1] = '\0';
2266 if (alc[len - 2] == ELF_VER_CHR)
2267 alc[len - 2] = '\0';
2268
2269 h->verinfo.vertree = t;
2270 t->used = TRUE;
2271 d = NULL;
2272
2273 if (t->globals.list != NULL)
2274 d = (*t->match) (&t->globals, NULL, alc);
2275
2276 /* See if there is anything to force this symbol to
2277 local scope. */
2278 if (d == NULL && t->locals.list != NULL)
2279 {
2280 d = (*t->match) (&t->locals, NULL, alc);
2281 if (d != NULL
2282 && h->dynindx != -1
2283 && ! info->export_dynamic)
2284 *hide = TRUE;
2285 }
2286
2287 free (alc);
2288 break;
2289 }
2290 }
2291
2292 *t_p = t;
2293
2294 return TRUE;
2295 }
2296
2297 /* Return TRUE if the symbol H is hidden by version script. */
2298
2299 bfd_boolean
2300 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2301 struct elf_link_hash_entry *h)
2302 {
2303 const char *p;
2304 bfd_boolean hide = FALSE;
2305 const struct elf_backend_data *bed
2306 = get_elf_backend_data (info->output_bfd);
2307
2308 /* Version script only hides symbols defined in regular objects. */
2309 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2310 return TRUE;
2311
2312 p = strchr (h->root.root.string, ELF_VER_CHR);
2313 if (p != NULL && h->verinfo.vertree == NULL)
2314 {
2315 struct bfd_elf_version_tree *t;
2316
2317 ++p;
2318 if (*p == ELF_VER_CHR)
2319 ++p;
2320
2321 if (*p != '\0'
2322 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2323 && hide)
2324 {
2325 if (hide)
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2327 return TRUE;
2328 }
2329 }
2330
2331 /* If we don't have a version for this symbol, see if we can find
2332 something. */
2333 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2334 {
2335 h->verinfo.vertree
2336 = bfd_find_version_for_sym (info->version_info,
2337 h->root.root.string, &hide);
2338 if (h->verinfo.vertree != NULL && hide)
2339 {
2340 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2341 return TRUE;
2342 }
2343 }
2344
2345 return FALSE;
2346 }
2347
2348 /* Figure out appropriate versions for all the symbols. We may not
2349 have the version number script until we have read all of the input
2350 files, so until that point we don't know which symbols should be
2351 local. This function is called via elf_link_hash_traverse. */
2352
2353 static bfd_boolean
2354 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2355 {
2356 struct elf_info_failed *sinfo;
2357 struct bfd_link_info *info;
2358 const struct elf_backend_data *bed;
2359 struct elf_info_failed eif;
2360 char *p;
2361 bfd_boolean hide;
2362
2363 sinfo = (struct elf_info_failed *) data;
2364 info = sinfo->info;
2365
2366 /* Fix the symbol flags. */
2367 eif.failed = FALSE;
2368 eif.info = info;
2369 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2370 {
2371 if (eif.failed)
2372 sinfo->failed = TRUE;
2373 return FALSE;
2374 }
2375
2376 bed = get_elf_backend_data (info->output_bfd);
2377
2378 /* We only need version numbers for symbols defined in regular
2379 objects. */
2380 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2381 {
2382 /* Hide symbols defined in discarded input sections. */
2383 if ((h->root.type == bfd_link_hash_defined
2384 || h->root.type == bfd_link_hash_defweak)
2385 && discarded_section (h->root.u.def.section))
2386 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2387 return TRUE;
2388 }
2389
2390 hide = FALSE;
2391 p = strchr (h->root.root.string, ELF_VER_CHR);
2392 if (p != NULL && h->verinfo.vertree == NULL)
2393 {
2394 struct bfd_elf_version_tree *t;
2395
2396 ++p;
2397 if (*p == ELF_VER_CHR)
2398 ++p;
2399
2400 /* If there is no version string, we can just return out. */
2401 if (*p == '\0')
2402 return TRUE;
2403
2404 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2405 {
2406 sinfo->failed = TRUE;
2407 return FALSE;
2408 }
2409
2410 if (hide)
2411 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2412
2413 /* If we are building an application, we need to create a
2414 version node for this version. */
2415 if (t == NULL && bfd_link_executable (info))
2416 {
2417 struct bfd_elf_version_tree **pp;
2418 int version_index;
2419
2420 /* If we aren't going to export this symbol, we don't need
2421 to worry about it. */
2422 if (h->dynindx == -1)
2423 return TRUE;
2424
2425 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2426 sizeof *t);
2427 if (t == NULL)
2428 {
2429 sinfo->failed = TRUE;
2430 return FALSE;
2431 }
2432
2433 t->name = p;
2434 t->name_indx = (unsigned int) -1;
2435 t->used = TRUE;
2436
2437 version_index = 1;
2438 /* Don't count anonymous version tag. */
2439 if (sinfo->info->version_info != NULL
2440 && sinfo->info->version_info->vernum == 0)
2441 version_index = 0;
2442 for (pp = &sinfo->info->version_info;
2443 *pp != NULL;
2444 pp = &(*pp)->next)
2445 ++version_index;
2446 t->vernum = version_index;
2447
2448 *pp = t;
2449
2450 h->verinfo.vertree = t;
2451 }
2452 else if (t == NULL)
2453 {
2454 /* We could not find the version for a symbol when
2455 generating a shared archive. Return an error. */
2456 _bfd_error_handler
2457 /* xgettext:c-format */
2458 (_("%pB: version node not found for symbol %s"),
2459 info->output_bfd, h->root.root.string);
2460 bfd_set_error (bfd_error_bad_value);
2461 sinfo->failed = TRUE;
2462 return FALSE;
2463 }
2464 }
2465
2466 /* If we don't have a version for this symbol, see if we can find
2467 something. */
2468 if (!hide
2469 && h->verinfo.vertree == NULL
2470 && sinfo->info->version_info != NULL)
2471 {
2472 h->verinfo.vertree
2473 = bfd_find_version_for_sym (sinfo->info->version_info,
2474 h->root.root.string, &hide);
2475 if (h->verinfo.vertree != NULL && hide)
2476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2477 }
2478
2479 return TRUE;
2480 }
2481 \f
2482 /* Read and swap the relocs from the section indicated by SHDR. This
2483 may be either a REL or a RELA section. The relocations are
2484 translated into RELA relocations and stored in INTERNAL_RELOCS,
2485 which should have already been allocated to contain enough space.
2486 The EXTERNAL_RELOCS are a buffer where the external form of the
2487 relocations should be stored.
2488
2489 Returns FALSE if something goes wrong. */
2490
2491 static bfd_boolean
2492 elf_link_read_relocs_from_section (bfd *abfd,
2493 asection *sec,
2494 Elf_Internal_Shdr *shdr,
2495 void *external_relocs,
2496 Elf_Internal_Rela *internal_relocs)
2497 {
2498 const struct elf_backend_data *bed;
2499 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2500 const bfd_byte *erela;
2501 const bfd_byte *erelaend;
2502 Elf_Internal_Rela *irela;
2503 Elf_Internal_Shdr *symtab_hdr;
2504 size_t nsyms;
2505
2506 /* Position ourselves at the start of the section. */
2507 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2508 return FALSE;
2509
2510 /* Read the relocations. */
2511 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2512 return FALSE;
2513
2514 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2515 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2516
2517 bed = get_elf_backend_data (abfd);
2518
2519 /* Convert the external relocations to the internal format. */
2520 if (shdr->sh_entsize == bed->s->sizeof_rel)
2521 swap_in = bed->s->swap_reloc_in;
2522 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2523 swap_in = bed->s->swap_reloca_in;
2524 else
2525 {
2526 bfd_set_error (bfd_error_wrong_format);
2527 return FALSE;
2528 }
2529
2530 erela = (const bfd_byte *) external_relocs;
2531 /* Setting erelaend like this and comparing with <= handles case of
2532 a fuzzed object with sh_size not a multiple of sh_entsize. */
2533 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2534 irela = internal_relocs;
2535 while (erela <= erelaend)
2536 {
2537 bfd_vma r_symndx;
2538
2539 (*swap_in) (abfd, erela, irela);
2540 r_symndx = ELF32_R_SYM (irela->r_info);
2541 if (bed->s->arch_size == 64)
2542 r_symndx >>= 24;
2543 if (nsyms > 0)
2544 {
2545 if ((size_t) r_symndx >= nsyms)
2546 {
2547 _bfd_error_handler
2548 /* xgettext:c-format */
2549 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2550 " for offset %#" PRIx64 " in section `%pA'"),
2551 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2552 (uint64_t) irela->r_offset, sec);
2553 bfd_set_error (bfd_error_bad_value);
2554 return FALSE;
2555 }
2556 }
2557 else if (r_symndx != STN_UNDEF)
2558 {
2559 _bfd_error_handler
2560 /* xgettext:c-format */
2561 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2562 " for offset %#" PRIx64 " in section `%pA'"
2563 " when the object file has no symbol table"),
2564 abfd, (uint64_t) r_symndx,
2565 (uint64_t) irela->r_offset, sec);
2566 bfd_set_error (bfd_error_bad_value);
2567 return FALSE;
2568 }
2569 irela += bed->s->int_rels_per_ext_rel;
2570 erela += shdr->sh_entsize;
2571 }
2572
2573 return TRUE;
2574 }
2575
2576 /* Read and swap the relocs for a section O. They may have been
2577 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2578 not NULL, they are used as buffers to read into. They are known to
2579 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2580 the return value is allocated using either malloc or bfd_alloc,
2581 according to the KEEP_MEMORY argument. If O has two relocation
2582 sections (both REL and RELA relocations), then the REL_HDR
2583 relocations will appear first in INTERNAL_RELOCS, followed by the
2584 RELA_HDR relocations. */
2585
2586 Elf_Internal_Rela *
2587 _bfd_elf_link_read_relocs (bfd *abfd,
2588 asection *o,
2589 void *external_relocs,
2590 Elf_Internal_Rela *internal_relocs,
2591 bfd_boolean keep_memory)
2592 {
2593 void *alloc1 = NULL;
2594 Elf_Internal_Rela *alloc2 = NULL;
2595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2596 struct bfd_elf_section_data *esdo = elf_section_data (o);
2597 Elf_Internal_Rela *internal_rela_relocs;
2598
2599 if (esdo->relocs != NULL)
2600 return esdo->relocs;
2601
2602 if (o->reloc_count == 0)
2603 return NULL;
2604
2605 if (internal_relocs == NULL)
2606 {
2607 bfd_size_type size;
2608
2609 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2610 if (keep_memory)
2611 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2612 else
2613 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2614 if (internal_relocs == NULL)
2615 goto error_return;
2616 }
2617
2618 if (external_relocs == NULL)
2619 {
2620 bfd_size_type size = 0;
2621
2622 if (esdo->rel.hdr)
2623 size += esdo->rel.hdr->sh_size;
2624 if (esdo->rela.hdr)
2625 size += esdo->rela.hdr->sh_size;
2626
2627 alloc1 = bfd_malloc (size);
2628 if (alloc1 == NULL)
2629 goto error_return;
2630 external_relocs = alloc1;
2631 }
2632
2633 internal_rela_relocs = internal_relocs;
2634 if (esdo->rel.hdr)
2635 {
2636 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2637 external_relocs,
2638 internal_relocs))
2639 goto error_return;
2640 external_relocs = (((bfd_byte *) external_relocs)
2641 + esdo->rel.hdr->sh_size);
2642 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2643 * bed->s->int_rels_per_ext_rel);
2644 }
2645
2646 if (esdo->rela.hdr
2647 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2648 external_relocs,
2649 internal_rela_relocs)))
2650 goto error_return;
2651
2652 /* Cache the results for next time, if we can. */
2653 if (keep_memory)
2654 esdo->relocs = internal_relocs;
2655
2656 free (alloc1);
2657
2658 /* Don't free alloc2, since if it was allocated we are passing it
2659 back (under the name of internal_relocs). */
2660
2661 return internal_relocs;
2662
2663 error_return:
2664 free (alloc1);
2665 if (alloc2 != NULL)
2666 {
2667 if (keep_memory)
2668 bfd_release (abfd, alloc2);
2669 else
2670 free (alloc2);
2671 }
2672 return NULL;
2673 }
2674
2675 /* Compute the size of, and allocate space for, REL_HDR which is the
2676 section header for a section containing relocations for O. */
2677
2678 static bfd_boolean
2679 _bfd_elf_link_size_reloc_section (bfd *abfd,
2680 struct bfd_elf_section_reloc_data *reldata)
2681 {
2682 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2683
2684 /* That allows us to calculate the size of the section. */
2685 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2686
2687 /* The contents field must last into write_object_contents, so we
2688 allocate it with bfd_alloc rather than malloc. Also since we
2689 cannot be sure that the contents will actually be filled in,
2690 we zero the allocated space. */
2691 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2692 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2693 return FALSE;
2694
2695 if (reldata->hashes == NULL && reldata->count)
2696 {
2697 struct elf_link_hash_entry **p;
2698
2699 p = ((struct elf_link_hash_entry **)
2700 bfd_zmalloc (reldata->count * sizeof (*p)));
2701 if (p == NULL)
2702 return FALSE;
2703
2704 reldata->hashes = p;
2705 }
2706
2707 return TRUE;
2708 }
2709
2710 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2711 originated from the section given by INPUT_REL_HDR) to the
2712 OUTPUT_BFD. */
2713
2714 bfd_boolean
2715 _bfd_elf_link_output_relocs (bfd *output_bfd,
2716 asection *input_section,
2717 Elf_Internal_Shdr *input_rel_hdr,
2718 Elf_Internal_Rela *internal_relocs,
2719 struct elf_link_hash_entry **rel_hash
2720 ATTRIBUTE_UNUSED)
2721 {
2722 Elf_Internal_Rela *irela;
2723 Elf_Internal_Rela *irelaend;
2724 bfd_byte *erel;
2725 struct bfd_elf_section_reloc_data *output_reldata;
2726 asection *output_section;
2727 const struct elf_backend_data *bed;
2728 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2729 struct bfd_elf_section_data *esdo;
2730
2731 output_section = input_section->output_section;
2732
2733 bed = get_elf_backend_data (output_bfd);
2734 esdo = elf_section_data (output_section);
2735 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2736 {
2737 output_reldata = &esdo->rel;
2738 swap_out = bed->s->swap_reloc_out;
2739 }
2740 else if (esdo->rela.hdr
2741 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2742 {
2743 output_reldata = &esdo->rela;
2744 swap_out = bed->s->swap_reloca_out;
2745 }
2746 else
2747 {
2748 _bfd_error_handler
2749 /* xgettext:c-format */
2750 (_("%pB: relocation size mismatch in %pB section %pA"),
2751 output_bfd, input_section->owner, input_section);
2752 bfd_set_error (bfd_error_wrong_format);
2753 return FALSE;
2754 }
2755
2756 erel = output_reldata->hdr->contents;
2757 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2758 irela = internal_relocs;
2759 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2760 * bed->s->int_rels_per_ext_rel);
2761 while (irela < irelaend)
2762 {
2763 (*swap_out) (output_bfd, irela, erel);
2764 irela += bed->s->int_rels_per_ext_rel;
2765 erel += input_rel_hdr->sh_entsize;
2766 }
2767
2768 /* Bump the counter, so that we know where to add the next set of
2769 relocations. */
2770 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2771
2772 return TRUE;
2773 }
2774 \f
2775 /* Make weak undefined symbols in PIE dynamic. */
2776
2777 bfd_boolean
2778 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2779 struct elf_link_hash_entry *h)
2780 {
2781 if (bfd_link_pie (info)
2782 && h->dynindx == -1
2783 && h->root.type == bfd_link_hash_undefweak)
2784 return bfd_elf_link_record_dynamic_symbol (info, h);
2785
2786 return TRUE;
2787 }
2788
2789 /* Fix up the flags for a symbol. This handles various cases which
2790 can only be fixed after all the input files are seen. This is
2791 currently called by both adjust_dynamic_symbol and
2792 assign_sym_version, which is unnecessary but perhaps more robust in
2793 the face of future changes. */
2794
2795 static bfd_boolean
2796 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2797 struct elf_info_failed *eif)
2798 {
2799 const struct elf_backend_data *bed;
2800
2801 /* If this symbol was mentioned in a non-ELF file, try to set
2802 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2803 permit a non-ELF file to correctly refer to a symbol defined in
2804 an ELF dynamic object. */
2805 if (h->non_elf)
2806 {
2807 while (h->root.type == bfd_link_hash_indirect)
2808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2809
2810 if (h->root.type != bfd_link_hash_defined
2811 && h->root.type != bfd_link_hash_defweak)
2812 {
2813 h->ref_regular = 1;
2814 h->ref_regular_nonweak = 1;
2815 }
2816 else
2817 {
2818 if (h->root.u.def.section->owner != NULL
2819 && (bfd_get_flavour (h->root.u.def.section->owner)
2820 == bfd_target_elf_flavour))
2821 {
2822 h->ref_regular = 1;
2823 h->ref_regular_nonweak = 1;
2824 }
2825 else
2826 h->def_regular = 1;
2827 }
2828
2829 if (h->dynindx == -1
2830 && (h->def_dynamic
2831 || h->ref_dynamic))
2832 {
2833 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2834 {
2835 eif->failed = TRUE;
2836 return FALSE;
2837 }
2838 }
2839 }
2840 else
2841 {
2842 /* Unfortunately, NON_ELF is only correct if the symbol
2843 was first seen in a non-ELF file. Fortunately, if the symbol
2844 was first seen in an ELF file, we're probably OK unless the
2845 symbol was defined in a non-ELF file. Catch that case here.
2846 FIXME: We're still in trouble if the symbol was first seen in
2847 a dynamic object, and then later in a non-ELF regular object. */
2848 if ((h->root.type == bfd_link_hash_defined
2849 || h->root.type == bfd_link_hash_defweak)
2850 && !h->def_regular
2851 && (h->root.u.def.section->owner != NULL
2852 ? (bfd_get_flavour (h->root.u.def.section->owner)
2853 != bfd_target_elf_flavour)
2854 : (bfd_is_abs_section (h->root.u.def.section)
2855 && !h->def_dynamic)))
2856 h->def_regular = 1;
2857 }
2858
2859 /* Backend specific symbol fixup. */
2860 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2861 if (bed->elf_backend_fixup_symbol
2862 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2863 return FALSE;
2864
2865 /* If this is a final link, and the symbol was defined as a common
2866 symbol in a regular object file, and there was no definition in
2867 any dynamic object, then the linker will have allocated space for
2868 the symbol in a common section but the DEF_REGULAR
2869 flag will not have been set. */
2870 if (h->root.type == bfd_link_hash_defined
2871 && !h->def_regular
2872 && h->ref_regular
2873 && !h->def_dynamic
2874 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2875 h->def_regular = 1;
2876
2877 /* Symbols defined in discarded sections shouldn't be dynamic. */
2878 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2879 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2880
2881 /* If a weak undefined symbol has non-default visibility, we also
2882 hide it from the dynamic linker. */
2883 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2884 && h->root.type == bfd_link_hash_undefweak)
2885 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2886
2887 /* A hidden versioned symbol in executable should be forced local if
2888 it is is locally defined, not referenced by shared library and not
2889 exported. */
2890 else if (bfd_link_executable (eif->info)
2891 && h->versioned == versioned_hidden
2892 && !eif->info->export_dynamic
2893 && !h->dynamic
2894 && !h->ref_dynamic
2895 && h->def_regular)
2896 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2897
2898 /* If -Bsymbolic was used (which means to bind references to global
2899 symbols to the definition within the shared object), and this
2900 symbol was defined in a regular object, then it actually doesn't
2901 need a PLT entry. Likewise, if the symbol has non-default
2902 visibility. If the symbol has hidden or internal visibility, we
2903 will force it local. */
2904 else if (h->needs_plt
2905 && bfd_link_pic (eif->info)
2906 && is_elf_hash_table (eif->info->hash)
2907 && (SYMBOLIC_BIND (eif->info, h)
2908 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2909 && h->def_regular)
2910 {
2911 bfd_boolean force_local;
2912
2913 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2914 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2915 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2916 }
2917
2918 /* If this is a weak defined symbol in a dynamic object, and we know
2919 the real definition in the dynamic object, copy interesting flags
2920 over to the real definition. */
2921 if (h->is_weakalias)
2922 {
2923 struct elf_link_hash_entry *def = weakdef (h);
2924
2925 /* If the real definition is defined by a regular object file,
2926 don't do anything special. See the longer description in
2927 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2928 bfd_link_hash_defined as it was when put on the alias list
2929 then it must have originally been a versioned symbol (for
2930 which a non-versioned indirect symbol is created) and later
2931 a definition for the non-versioned symbol is found. In that
2932 case the indirection is flipped with the versioned symbol
2933 becoming an indirect pointing at the non-versioned symbol.
2934 Thus, not an alias any more. */
2935 if (def->def_regular
2936 || def->root.type != bfd_link_hash_defined)
2937 {
2938 h = def;
2939 while ((h = h->u.alias) != def)
2940 h->is_weakalias = 0;
2941 }
2942 else
2943 {
2944 while (h->root.type == bfd_link_hash_indirect)
2945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2946 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2947 || h->root.type == bfd_link_hash_defweak);
2948 BFD_ASSERT (def->def_dynamic);
2949 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2950 }
2951 }
2952
2953 return TRUE;
2954 }
2955
2956 /* Make the backend pick a good value for a dynamic symbol. This is
2957 called via elf_link_hash_traverse, and also calls itself
2958 recursively. */
2959
2960 static bfd_boolean
2961 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2962 {
2963 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2964 struct elf_link_hash_table *htab;
2965 const struct elf_backend_data *bed;
2966
2967 if (! is_elf_hash_table (eif->info->hash))
2968 return FALSE;
2969
2970 /* Ignore indirect symbols. These are added by the versioning code. */
2971 if (h->root.type == bfd_link_hash_indirect)
2972 return TRUE;
2973
2974 /* Fix the symbol flags. */
2975 if (! _bfd_elf_fix_symbol_flags (h, eif))
2976 return FALSE;
2977
2978 htab = elf_hash_table (eif->info);
2979 bed = get_elf_backend_data (htab->dynobj);
2980
2981 if (h->root.type == bfd_link_hash_undefweak)
2982 {
2983 if (eif->info->dynamic_undefined_weak == 0)
2984 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2985 else if (eif->info->dynamic_undefined_weak > 0
2986 && h->ref_regular
2987 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2988 && !bfd_hide_sym_by_version (eif->info->version_info,
2989 h->root.root.string))
2990 {
2991 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2992 {
2993 eif->failed = TRUE;
2994 return FALSE;
2995 }
2996 }
2997 }
2998
2999 /* If this symbol does not require a PLT entry, and it is not
3000 defined by a dynamic object, or is not referenced by a regular
3001 object, ignore it. We do have to handle a weak defined symbol,
3002 even if no regular object refers to it, if we decided to add it
3003 to the dynamic symbol table. FIXME: Do we normally need to worry
3004 about symbols which are defined by one dynamic object and
3005 referenced by another one? */
3006 if (!h->needs_plt
3007 && h->type != STT_GNU_IFUNC
3008 && (h->def_regular
3009 || !h->def_dynamic
3010 || (!h->ref_regular
3011 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3012 {
3013 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3014 return TRUE;
3015 }
3016
3017 /* If we've already adjusted this symbol, don't do it again. This
3018 can happen via a recursive call. */
3019 if (h->dynamic_adjusted)
3020 return TRUE;
3021
3022 /* Don't look at this symbol again. Note that we must set this
3023 after checking the above conditions, because we may look at a
3024 symbol once, decide not to do anything, and then get called
3025 recursively later after REF_REGULAR is set below. */
3026 h->dynamic_adjusted = 1;
3027
3028 /* If this is a weak definition, and we know a real definition, and
3029 the real symbol is not itself defined by a regular object file,
3030 then get a good value for the real definition. We handle the
3031 real symbol first, for the convenience of the backend routine.
3032
3033 Note that there is a confusing case here. If the real definition
3034 is defined by a regular object file, we don't get the real symbol
3035 from the dynamic object, but we do get the weak symbol. If the
3036 processor backend uses a COPY reloc, then if some routine in the
3037 dynamic object changes the real symbol, we will not see that
3038 change in the corresponding weak symbol. This is the way other
3039 ELF linkers work as well, and seems to be a result of the shared
3040 library model.
3041
3042 I will clarify this issue. Most SVR4 shared libraries define the
3043 variable _timezone and define timezone as a weak synonym. The
3044 tzset call changes _timezone. If you write
3045 extern int timezone;
3046 int _timezone = 5;
3047 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3048 you might expect that, since timezone is a synonym for _timezone,
3049 the same number will print both times. However, if the processor
3050 backend uses a COPY reloc, then actually timezone will be copied
3051 into your process image, and, since you define _timezone
3052 yourself, _timezone will not. Thus timezone and _timezone will
3053 wind up at different memory locations. The tzset call will set
3054 _timezone, leaving timezone unchanged. */
3055
3056 if (h->is_weakalias)
3057 {
3058 struct elf_link_hash_entry *def = weakdef (h);
3059
3060 /* If we get to this point, there is an implicit reference to
3061 the alias by a regular object file via the weak symbol H. */
3062 def->ref_regular = 1;
3063
3064 /* Ensure that the backend adjust_dynamic_symbol function sees
3065 the strong alias before H by recursively calling ourselves. */
3066 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3067 return FALSE;
3068 }
3069
3070 /* If a symbol has no type and no size and does not require a PLT
3071 entry, then we are probably about to do the wrong thing here: we
3072 are probably going to create a COPY reloc for an empty object.
3073 This case can arise when a shared object is built with assembly
3074 code, and the assembly code fails to set the symbol type. */
3075 if (h->size == 0
3076 && h->type == STT_NOTYPE
3077 && !h->needs_plt)
3078 _bfd_error_handler
3079 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3080 h->root.root.string);
3081
3082 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3083 {
3084 eif->failed = TRUE;
3085 return FALSE;
3086 }
3087
3088 return TRUE;
3089 }
3090
3091 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3092 DYNBSS. */
3093
3094 bfd_boolean
3095 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3096 struct elf_link_hash_entry *h,
3097 asection *dynbss)
3098 {
3099 unsigned int power_of_two;
3100 bfd_vma mask;
3101 asection *sec = h->root.u.def.section;
3102
3103 /* The section alignment of the definition is the maximum alignment
3104 requirement of symbols defined in the section. Since we don't
3105 know the symbol alignment requirement, we start with the
3106 maximum alignment and check low bits of the symbol address
3107 for the minimum alignment. */
3108 power_of_two = bfd_section_alignment (sec);
3109 mask = ((bfd_vma) 1 << power_of_two) - 1;
3110 while ((h->root.u.def.value & mask) != 0)
3111 {
3112 mask >>= 1;
3113 --power_of_two;
3114 }
3115
3116 if (power_of_two > bfd_section_alignment (dynbss))
3117 {
3118 /* Adjust the section alignment if needed. */
3119 if (!bfd_set_section_alignment (dynbss, power_of_two))
3120 return FALSE;
3121 }
3122
3123 /* We make sure that the symbol will be aligned properly. */
3124 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3125
3126 /* Define the symbol as being at this point in DYNBSS. */
3127 h->root.u.def.section = dynbss;
3128 h->root.u.def.value = dynbss->size;
3129
3130 /* Increment the size of DYNBSS to make room for the symbol. */
3131 dynbss->size += h->size;
3132
3133 /* No error if extern_protected_data is true. */
3134 if (h->protected_def
3135 && (!info->extern_protected_data
3136 || (info->extern_protected_data < 0
3137 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3138 info->callbacks->einfo
3139 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3140 h->root.root.string);
3141
3142 return TRUE;
3143 }
3144
3145 /* Adjust all external symbols pointing into SEC_MERGE sections
3146 to reflect the object merging within the sections. */
3147
3148 static bfd_boolean
3149 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3150 {
3151 asection *sec;
3152
3153 if ((h->root.type == bfd_link_hash_defined
3154 || h->root.type == bfd_link_hash_defweak)
3155 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3156 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3157 {
3158 bfd *output_bfd = (bfd *) data;
3159
3160 h->root.u.def.value =
3161 _bfd_merged_section_offset (output_bfd,
3162 &h->root.u.def.section,
3163 elf_section_data (sec)->sec_info,
3164 h->root.u.def.value);
3165 }
3166
3167 return TRUE;
3168 }
3169
3170 /* Returns false if the symbol referred to by H should be considered
3171 to resolve local to the current module, and true if it should be
3172 considered to bind dynamically. */
3173
3174 bfd_boolean
3175 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3176 struct bfd_link_info *info,
3177 bfd_boolean not_local_protected)
3178 {
3179 bfd_boolean binding_stays_local_p;
3180 const struct elf_backend_data *bed;
3181 struct elf_link_hash_table *hash_table;
3182
3183 if (h == NULL)
3184 return FALSE;
3185
3186 while (h->root.type == bfd_link_hash_indirect
3187 || h->root.type == bfd_link_hash_warning)
3188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3189
3190 /* If it was forced local, then clearly it's not dynamic. */
3191 if (h->dynindx == -1)
3192 return FALSE;
3193 if (h->forced_local)
3194 return FALSE;
3195
3196 /* Identify the cases where name binding rules say that a
3197 visible symbol resolves locally. */
3198 binding_stays_local_p = (bfd_link_executable (info)
3199 || SYMBOLIC_BIND (info, h));
3200
3201 switch (ELF_ST_VISIBILITY (h->other))
3202 {
3203 case STV_INTERNAL:
3204 case STV_HIDDEN:
3205 return FALSE;
3206
3207 case STV_PROTECTED:
3208 hash_table = elf_hash_table (info);
3209 if (!is_elf_hash_table (hash_table))
3210 return FALSE;
3211
3212 bed = get_elf_backend_data (hash_table->dynobj);
3213
3214 /* Proper resolution for function pointer equality may require
3215 that these symbols perhaps be resolved dynamically, even though
3216 we should be resolving them to the current module. */
3217 if (!not_local_protected || !bed->is_function_type (h->type))
3218 binding_stays_local_p = TRUE;
3219 break;
3220
3221 default:
3222 break;
3223 }
3224
3225 /* If it isn't defined locally, then clearly it's dynamic. */
3226 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3227 return TRUE;
3228
3229 /* Otherwise, the symbol is dynamic if binding rules don't tell
3230 us that it remains local. */
3231 return !binding_stays_local_p;
3232 }
3233
3234 /* Return true if the symbol referred to by H should be considered
3235 to resolve local to the current module, and false otherwise. Differs
3236 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3237 undefined symbols. The two functions are virtually identical except
3238 for the place where dynindx == -1 is tested. If that test is true,
3239 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3240 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3241 defined symbols.
3242 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3243 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3244 treatment of undefined weak symbols. For those that do not make
3245 undefined weak symbols dynamic, both functions may return false. */
3246
3247 bfd_boolean
3248 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3249 struct bfd_link_info *info,
3250 bfd_boolean local_protected)
3251 {
3252 const struct elf_backend_data *bed;
3253 struct elf_link_hash_table *hash_table;
3254
3255 /* If it's a local sym, of course we resolve locally. */
3256 if (h == NULL)
3257 return TRUE;
3258
3259 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3260 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3261 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3262 return TRUE;
3263
3264 /* Forced local symbols resolve locally. */
3265 if (h->forced_local)
3266 return TRUE;
3267
3268 /* Common symbols that become definitions don't get the DEF_REGULAR
3269 flag set, so test it first, and don't bail out. */
3270 if (ELF_COMMON_DEF_P (h))
3271 /* Do nothing. */;
3272 /* If we don't have a definition in a regular file, then we can't
3273 resolve locally. The sym is either undefined or dynamic. */
3274 else if (!h->def_regular)
3275 return FALSE;
3276
3277 /* Non-dynamic symbols resolve locally. */
3278 if (h->dynindx == -1)
3279 return TRUE;
3280
3281 /* At this point, we know the symbol is defined and dynamic. In an
3282 executable it must resolve locally, likewise when building symbolic
3283 shared libraries. */
3284 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3285 return TRUE;
3286
3287 /* Now deal with defined dynamic symbols in shared libraries. Ones
3288 with default visibility might not resolve locally. */
3289 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3290 return FALSE;
3291
3292 hash_table = elf_hash_table (info);
3293 if (!is_elf_hash_table (hash_table))
3294 return TRUE;
3295
3296 bed = get_elf_backend_data (hash_table->dynobj);
3297
3298 /* If extern_protected_data is false, STV_PROTECTED non-function
3299 symbols are local. */
3300 if ((!info->extern_protected_data
3301 || (info->extern_protected_data < 0
3302 && !bed->extern_protected_data))
3303 && !bed->is_function_type (h->type))
3304 return TRUE;
3305
3306 /* Function pointer equality tests may require that STV_PROTECTED
3307 symbols be treated as dynamic symbols. If the address of a
3308 function not defined in an executable is set to that function's
3309 plt entry in the executable, then the address of the function in
3310 a shared library must also be the plt entry in the executable. */
3311 return local_protected;
3312 }
3313
3314 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3315 aligned. Returns the first TLS output section. */
3316
3317 struct bfd_section *
3318 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3319 {
3320 struct bfd_section *sec, *tls;
3321 unsigned int align = 0;
3322
3323 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3324 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3325 break;
3326 tls = sec;
3327
3328 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3329 if (sec->alignment_power > align)
3330 align = sec->alignment_power;
3331
3332 elf_hash_table (info)->tls_sec = tls;
3333
3334 /* Ensure the alignment of the first section (usually .tdata) is the largest
3335 alignment, so that the tls segment starts aligned. */
3336 if (tls != NULL)
3337 tls->alignment_power = align;
3338
3339 return tls;
3340 }
3341
3342 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3343 static bfd_boolean
3344 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3345 Elf_Internal_Sym *sym)
3346 {
3347 const struct elf_backend_data *bed;
3348
3349 /* Local symbols do not count, but target specific ones might. */
3350 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3351 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3352 return FALSE;
3353
3354 bed = get_elf_backend_data (abfd);
3355 /* Function symbols do not count. */
3356 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3357 return FALSE;
3358
3359 /* If the section is undefined, then so is the symbol. */
3360 if (sym->st_shndx == SHN_UNDEF)
3361 return FALSE;
3362
3363 /* If the symbol is defined in the common section, then
3364 it is a common definition and so does not count. */
3365 if (bed->common_definition (sym))
3366 return FALSE;
3367
3368 /* If the symbol is in a target specific section then we
3369 must rely upon the backend to tell us what it is. */
3370 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3371 /* FIXME - this function is not coded yet:
3372
3373 return _bfd_is_global_symbol_definition (abfd, sym);
3374
3375 Instead for now assume that the definition is not global,
3376 Even if this is wrong, at least the linker will behave
3377 in the same way that it used to do. */
3378 return FALSE;
3379
3380 return TRUE;
3381 }
3382
3383 /* Search the symbol table of the archive element of the archive ABFD
3384 whose archive map contains a mention of SYMDEF, and determine if
3385 the symbol is defined in this element. */
3386 static bfd_boolean
3387 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3388 {
3389 Elf_Internal_Shdr * hdr;
3390 size_t symcount;
3391 size_t extsymcount;
3392 size_t extsymoff;
3393 Elf_Internal_Sym *isymbuf;
3394 Elf_Internal_Sym *isym;
3395 Elf_Internal_Sym *isymend;
3396 bfd_boolean result;
3397
3398 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3399 if (abfd == NULL)
3400 return FALSE;
3401
3402 if (! bfd_check_format (abfd, bfd_object))
3403 return FALSE;
3404
3405 /* Select the appropriate symbol table. If we don't know if the
3406 object file is an IR object, give linker LTO plugin a chance to
3407 get the correct symbol table. */
3408 if (abfd->plugin_format == bfd_plugin_yes
3409 #if BFD_SUPPORTS_PLUGINS
3410 || (abfd->plugin_format == bfd_plugin_unknown
3411 && bfd_link_plugin_object_p (abfd))
3412 #endif
3413 )
3414 {
3415 /* Use the IR symbol table if the object has been claimed by
3416 plugin. */
3417 abfd = abfd->plugin_dummy_bfd;
3418 hdr = &elf_tdata (abfd)->symtab_hdr;
3419 }
3420 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3421 hdr = &elf_tdata (abfd)->symtab_hdr;
3422 else
3423 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3424
3425 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3426
3427 /* The sh_info field of the symtab header tells us where the
3428 external symbols start. We don't care about the local symbols. */
3429 if (elf_bad_symtab (abfd))
3430 {
3431 extsymcount = symcount;
3432 extsymoff = 0;
3433 }
3434 else
3435 {
3436 extsymcount = symcount - hdr->sh_info;
3437 extsymoff = hdr->sh_info;
3438 }
3439
3440 if (extsymcount == 0)
3441 return FALSE;
3442
3443 /* Read in the symbol table. */
3444 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3445 NULL, NULL, NULL);
3446 if (isymbuf == NULL)
3447 return FALSE;
3448
3449 /* Scan the symbol table looking for SYMDEF. */
3450 result = FALSE;
3451 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3452 {
3453 const char *name;
3454
3455 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3456 isym->st_name);
3457 if (name == NULL)
3458 break;
3459
3460 if (strcmp (name, symdef->name) == 0)
3461 {
3462 result = is_global_data_symbol_definition (abfd, isym);
3463 break;
3464 }
3465 }
3466
3467 free (isymbuf);
3468
3469 return result;
3470 }
3471 \f
3472 /* Add an entry to the .dynamic table. */
3473
3474 bfd_boolean
3475 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3476 bfd_vma tag,
3477 bfd_vma val)
3478 {
3479 struct elf_link_hash_table *hash_table;
3480 const struct elf_backend_data *bed;
3481 asection *s;
3482 bfd_size_type newsize;
3483 bfd_byte *newcontents;
3484 Elf_Internal_Dyn dyn;
3485
3486 hash_table = elf_hash_table (info);
3487 if (! is_elf_hash_table (hash_table))
3488 return FALSE;
3489
3490 if (tag == DT_RELA || tag == DT_REL)
3491 hash_table->dynamic_relocs = TRUE;
3492
3493 bed = get_elf_backend_data (hash_table->dynobj);
3494 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3495 BFD_ASSERT (s != NULL);
3496
3497 newsize = s->size + bed->s->sizeof_dyn;
3498 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3499 if (newcontents == NULL)
3500 return FALSE;
3501
3502 dyn.d_tag = tag;
3503 dyn.d_un.d_val = val;
3504 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3505
3506 s->size = newsize;
3507 s->contents = newcontents;
3508
3509 return TRUE;
3510 }
3511
3512 /* Strip zero-sized dynamic sections. */
3513
3514 bfd_boolean
3515 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3516 {
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s, *sdynamic, **pp;
3520 asection *rela_dyn, *rel_dyn;
3521 Elf_Internal_Dyn dyn;
3522 bfd_byte *extdyn, *next;
3523 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3524 bfd_boolean strip_zero_sized;
3525 bfd_boolean strip_zero_sized_plt;
3526
3527 if (bfd_link_relocatable (info))
3528 return TRUE;
3529
3530 hash_table = elf_hash_table (info);
3531 if (!is_elf_hash_table (hash_table))
3532 return FALSE;
3533
3534 if (!hash_table->dynobj)
3535 return TRUE;
3536
3537 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3538 if (!sdynamic)
3539 return TRUE;
3540
3541 bed = get_elf_backend_data (hash_table->dynobj);
3542 swap_dyn_in = bed->s->swap_dyn_in;
3543
3544 strip_zero_sized = FALSE;
3545 strip_zero_sized_plt = FALSE;
3546
3547 /* Strip zero-sized dynamic sections. */
3548 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3549 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3550 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3551 if (s->size == 0
3552 && (s == rela_dyn
3553 || s == rel_dyn
3554 || s == hash_table->srelplt->output_section
3555 || s == hash_table->splt->output_section))
3556 {
3557 *pp = s->next;
3558 info->output_bfd->section_count--;
3559 strip_zero_sized = TRUE;
3560 if (s == rela_dyn)
3561 s = rela_dyn;
3562 if (s == rel_dyn)
3563 s = rel_dyn;
3564 else if (s == hash_table->splt->output_section)
3565 {
3566 s = hash_table->splt;
3567 strip_zero_sized_plt = TRUE;
3568 }
3569 else
3570 s = hash_table->srelplt;
3571 s->flags |= SEC_EXCLUDE;
3572 s->output_section = bfd_abs_section_ptr;
3573 }
3574 else
3575 pp = &s->next;
3576
3577 if (strip_zero_sized_plt)
3578 for (extdyn = sdynamic->contents;
3579 extdyn < sdynamic->contents + sdynamic->size;
3580 extdyn = next)
3581 {
3582 next = extdyn + bed->s->sizeof_dyn;
3583 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3584 switch (dyn.d_tag)
3585 {
3586 default:
3587 break;
3588 case DT_JMPREL:
3589 case DT_PLTRELSZ:
3590 case DT_PLTREL:
3591 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3592 the procedure linkage table (the .plt section) has been
3593 removed. */
3594 memmove (extdyn, next,
3595 sdynamic->size - (next - sdynamic->contents));
3596 next = extdyn;
3597 }
3598 }
3599
3600 if (strip_zero_sized)
3601 {
3602 /* Regenerate program headers. */
3603 elf_seg_map (info->output_bfd) = NULL;
3604 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3605 }
3606
3607 return TRUE;
3608 }
3609
3610 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3611 1 if a DT_NEEDED tag already exists, and 0 on success. */
3612
3613 int
3614 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3615 {
3616 struct elf_link_hash_table *hash_table;
3617 size_t strindex;
3618 const char *soname;
3619
3620 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3621 return -1;
3622
3623 hash_table = elf_hash_table (info);
3624 soname = elf_dt_name (abfd);
3625 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3626 if (strindex == (size_t) -1)
3627 return -1;
3628
3629 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3630 {
3631 asection *sdyn;
3632 const struct elf_backend_data *bed;
3633 bfd_byte *extdyn;
3634
3635 bed = get_elf_backend_data (hash_table->dynobj);
3636 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3637 if (sdyn != NULL)
3638 for (extdyn = sdyn->contents;
3639 extdyn < sdyn->contents + sdyn->size;
3640 extdyn += bed->s->sizeof_dyn)
3641 {
3642 Elf_Internal_Dyn dyn;
3643
3644 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3645 if (dyn.d_tag == DT_NEEDED
3646 && dyn.d_un.d_val == strindex)
3647 {
3648 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3649 return 1;
3650 }
3651 }
3652 }
3653
3654 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3655 return -1;
3656
3657 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3658 return -1;
3659
3660 return 0;
3661 }
3662
3663 /* Return true if SONAME is on the needed list between NEEDED and STOP
3664 (or the end of list if STOP is NULL), and needed by a library that
3665 will be loaded. */
3666
3667 static bfd_boolean
3668 on_needed_list (const char *soname,
3669 struct bfd_link_needed_list *needed,
3670 struct bfd_link_needed_list *stop)
3671 {
3672 struct bfd_link_needed_list *look;
3673 for (look = needed; look != stop; look = look->next)
3674 if (strcmp (soname, look->name) == 0
3675 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3676 /* If needed by a library that itself is not directly
3677 needed, recursively check whether that library is
3678 indirectly needed. Since we add DT_NEEDED entries to
3679 the end of the list, library dependencies appear after
3680 the library. Therefore search prior to the current
3681 LOOK, preventing possible infinite recursion. */
3682 || on_needed_list (elf_dt_name (look->by), needed, look)))
3683 return TRUE;
3684
3685 return FALSE;
3686 }
3687
3688 /* Sort symbol by value, section, size, and type. */
3689 static int
3690 elf_sort_symbol (const void *arg1, const void *arg2)
3691 {
3692 const struct elf_link_hash_entry *h1;
3693 const struct elf_link_hash_entry *h2;
3694 bfd_signed_vma vdiff;
3695 int sdiff;
3696 const char *n1;
3697 const char *n2;
3698
3699 h1 = *(const struct elf_link_hash_entry **) arg1;
3700 h2 = *(const struct elf_link_hash_entry **) arg2;
3701 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3702 if (vdiff != 0)
3703 return vdiff > 0 ? 1 : -1;
3704
3705 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3706 if (sdiff != 0)
3707 return sdiff;
3708
3709 /* Sort so that sized symbols are selected over zero size symbols. */
3710 vdiff = h1->size - h2->size;
3711 if (vdiff != 0)
3712 return vdiff > 0 ? 1 : -1;
3713
3714 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3715 if (h1->type != h2->type)
3716 return h1->type - h2->type;
3717
3718 /* If symbols are properly sized and typed, and multiple strong
3719 aliases are not defined in a shared library by the user we
3720 shouldn't get here. Unfortunately linker script symbols like
3721 __bss_start sometimes match a user symbol defined at the start of
3722 .bss without proper size and type. We'd like to preference the
3723 user symbol over reserved system symbols. Sort on leading
3724 underscores. */
3725 n1 = h1->root.root.string;
3726 n2 = h2->root.root.string;
3727 while (*n1 == *n2)
3728 {
3729 if (*n1 == 0)
3730 break;
3731 ++n1;
3732 ++n2;
3733 }
3734 if (*n1 == '_')
3735 return -1;
3736 if (*n2 == '_')
3737 return 1;
3738
3739 /* Final sort on name selects user symbols like '_u' over reserved
3740 system symbols like '_Z' and also will avoid qsort instability. */
3741 return *n1 - *n2;
3742 }
3743
3744 /* This function is used to adjust offsets into .dynstr for
3745 dynamic symbols. This is called via elf_link_hash_traverse. */
3746
3747 static bfd_boolean
3748 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3749 {
3750 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3751
3752 if (h->dynindx != -1)
3753 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3754 return TRUE;
3755 }
3756
3757 /* Assign string offsets in .dynstr, update all structures referencing
3758 them. */
3759
3760 static bfd_boolean
3761 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3762 {
3763 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3764 struct elf_link_local_dynamic_entry *entry;
3765 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3766 bfd *dynobj = hash_table->dynobj;
3767 asection *sdyn;
3768 bfd_size_type size;
3769 const struct elf_backend_data *bed;
3770 bfd_byte *extdyn;
3771
3772 _bfd_elf_strtab_finalize (dynstr);
3773 size = _bfd_elf_strtab_size (dynstr);
3774
3775 bed = get_elf_backend_data (dynobj);
3776 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3777 BFD_ASSERT (sdyn != NULL);
3778
3779 /* Update all .dynamic entries referencing .dynstr strings. */
3780 for (extdyn = sdyn->contents;
3781 extdyn < sdyn->contents + sdyn->size;
3782 extdyn += bed->s->sizeof_dyn)
3783 {
3784 Elf_Internal_Dyn dyn;
3785
3786 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3787 switch (dyn.d_tag)
3788 {
3789 case DT_STRSZ:
3790 dyn.d_un.d_val = size;
3791 break;
3792 case DT_NEEDED:
3793 case DT_SONAME:
3794 case DT_RPATH:
3795 case DT_RUNPATH:
3796 case DT_FILTER:
3797 case DT_AUXILIARY:
3798 case DT_AUDIT:
3799 case DT_DEPAUDIT:
3800 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3801 break;
3802 default:
3803 continue;
3804 }
3805 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3806 }
3807
3808 /* Now update local dynamic symbols. */
3809 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3810 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3811 entry->isym.st_name);
3812
3813 /* And the rest of dynamic symbols. */
3814 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3815
3816 /* Adjust version definitions. */
3817 if (elf_tdata (output_bfd)->cverdefs)
3818 {
3819 asection *s;
3820 bfd_byte *p;
3821 size_t i;
3822 Elf_Internal_Verdef def;
3823 Elf_Internal_Verdaux defaux;
3824
3825 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3826 p = s->contents;
3827 do
3828 {
3829 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3830 &def);
3831 p += sizeof (Elf_External_Verdef);
3832 if (def.vd_aux != sizeof (Elf_External_Verdef))
3833 continue;
3834 for (i = 0; i < def.vd_cnt; ++i)
3835 {
3836 _bfd_elf_swap_verdaux_in (output_bfd,
3837 (Elf_External_Verdaux *) p, &defaux);
3838 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3839 defaux.vda_name);
3840 _bfd_elf_swap_verdaux_out (output_bfd,
3841 &defaux, (Elf_External_Verdaux *) p);
3842 p += sizeof (Elf_External_Verdaux);
3843 }
3844 }
3845 while (def.vd_next);
3846 }
3847
3848 /* Adjust version references. */
3849 if (elf_tdata (output_bfd)->verref)
3850 {
3851 asection *s;
3852 bfd_byte *p;
3853 size_t i;
3854 Elf_Internal_Verneed need;
3855 Elf_Internal_Vernaux needaux;
3856
3857 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3858 p = s->contents;
3859 do
3860 {
3861 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3862 &need);
3863 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3864 _bfd_elf_swap_verneed_out (output_bfd, &need,
3865 (Elf_External_Verneed *) p);
3866 p += sizeof (Elf_External_Verneed);
3867 for (i = 0; i < need.vn_cnt; ++i)
3868 {
3869 _bfd_elf_swap_vernaux_in (output_bfd,
3870 (Elf_External_Vernaux *) p, &needaux);
3871 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3872 needaux.vna_name);
3873 _bfd_elf_swap_vernaux_out (output_bfd,
3874 &needaux,
3875 (Elf_External_Vernaux *) p);
3876 p += sizeof (Elf_External_Vernaux);
3877 }
3878 }
3879 while (need.vn_next);
3880 }
3881
3882 return TRUE;
3883 }
3884 \f
3885 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3886 The default is to only match when the INPUT and OUTPUT are exactly
3887 the same target. */
3888
3889 bfd_boolean
3890 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3891 const bfd_target *output)
3892 {
3893 return input == output;
3894 }
3895
3896 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3897 This version is used when different targets for the same architecture
3898 are virtually identical. */
3899
3900 bfd_boolean
3901 _bfd_elf_relocs_compatible (const bfd_target *input,
3902 const bfd_target *output)
3903 {
3904 const struct elf_backend_data *obed, *ibed;
3905
3906 if (input == output)
3907 return TRUE;
3908
3909 ibed = xvec_get_elf_backend_data (input);
3910 obed = xvec_get_elf_backend_data (output);
3911
3912 if (ibed->arch != obed->arch)
3913 return FALSE;
3914
3915 /* If both backends are using this function, deem them compatible. */
3916 return ibed->relocs_compatible == obed->relocs_compatible;
3917 }
3918
3919 /* Make a special call to the linker "notice" function to tell it that
3920 we are about to handle an as-needed lib, or have finished
3921 processing the lib. */
3922
3923 bfd_boolean
3924 _bfd_elf_notice_as_needed (bfd *ibfd,
3925 struct bfd_link_info *info,
3926 enum notice_asneeded_action act)
3927 {
3928 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3929 }
3930
3931 /* Check relocations an ELF object file. */
3932
3933 bfd_boolean
3934 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3935 {
3936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3937 struct elf_link_hash_table *htab = elf_hash_table (info);
3938
3939 /* If this object is the same format as the output object, and it is
3940 not a shared library, then let the backend look through the
3941 relocs.
3942
3943 This is required to build global offset table entries and to
3944 arrange for dynamic relocs. It is not required for the
3945 particular common case of linking non PIC code, even when linking
3946 against shared libraries, but unfortunately there is no way of
3947 knowing whether an object file has been compiled PIC or not.
3948 Looking through the relocs is not particularly time consuming.
3949 The problem is that we must either (1) keep the relocs in memory,
3950 which causes the linker to require additional runtime memory or
3951 (2) read the relocs twice from the input file, which wastes time.
3952 This would be a good case for using mmap.
3953
3954 I have no idea how to handle linking PIC code into a file of a
3955 different format. It probably can't be done. */
3956 if ((abfd->flags & DYNAMIC) == 0
3957 && is_elf_hash_table (htab)
3958 && bed->check_relocs != NULL
3959 && elf_object_id (abfd) == elf_hash_table_id (htab)
3960 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3961 {
3962 asection *o;
3963
3964 for (o = abfd->sections; o != NULL; o = o->next)
3965 {
3966 Elf_Internal_Rela *internal_relocs;
3967 bfd_boolean ok;
3968
3969 /* Don't check relocations in excluded sections. Don't do
3970 anything special with non-loaded, non-alloced sections.
3971 In particular, any relocs in such sections should not
3972 affect GOT and PLT reference counting (ie. we don't
3973 allow them to create GOT or PLT entries), there's no
3974 possibility or desire to optimize TLS relocs, and
3975 there's not much point in propagating relocs to shared
3976 libs that the dynamic linker won't relocate. */
3977 if ((o->flags & SEC_ALLOC) == 0
3978 || (o->flags & SEC_RELOC) == 0
3979 || (o->flags & SEC_EXCLUDE) != 0
3980 || o->reloc_count == 0
3981 || ((info->strip == strip_all || info->strip == strip_debugger)
3982 && (o->flags & SEC_DEBUGGING) != 0)
3983 || bfd_is_abs_section (o->output_section))
3984 continue;
3985
3986 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3987 info->keep_memory);
3988 if (internal_relocs == NULL)
3989 return FALSE;
3990
3991 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3992
3993 if (elf_section_data (o)->relocs != internal_relocs)
3994 free (internal_relocs);
3995
3996 if (! ok)
3997 return FALSE;
3998 }
3999 }
4000
4001 return TRUE;
4002 }
4003
4004 /* Add symbols from an ELF object file to the linker hash table. */
4005
4006 static bfd_boolean
4007 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4008 {
4009 Elf_Internal_Ehdr *ehdr;
4010 Elf_Internal_Shdr *hdr;
4011 size_t symcount;
4012 size_t extsymcount;
4013 size_t extsymoff;
4014 struct elf_link_hash_entry **sym_hash;
4015 bfd_boolean dynamic;
4016 Elf_External_Versym *extversym = NULL;
4017 Elf_External_Versym *extversym_end = NULL;
4018 Elf_External_Versym *ever;
4019 struct elf_link_hash_entry *weaks;
4020 struct elf_link_hash_entry **nondeflt_vers = NULL;
4021 size_t nondeflt_vers_cnt = 0;
4022 Elf_Internal_Sym *isymbuf = NULL;
4023 Elf_Internal_Sym *isym;
4024 Elf_Internal_Sym *isymend;
4025 const struct elf_backend_data *bed;
4026 bfd_boolean add_needed;
4027 struct elf_link_hash_table *htab;
4028 void *alloc_mark = NULL;
4029 struct bfd_hash_entry **old_table = NULL;
4030 unsigned int old_size = 0;
4031 unsigned int old_count = 0;
4032 void *old_tab = NULL;
4033 void *old_ent;
4034 struct bfd_link_hash_entry *old_undefs = NULL;
4035 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4036 void *old_strtab = NULL;
4037 size_t tabsize = 0;
4038 asection *s;
4039 bfd_boolean just_syms;
4040
4041 htab = elf_hash_table (info);
4042 bed = get_elf_backend_data (abfd);
4043
4044 if ((abfd->flags & DYNAMIC) == 0)
4045 dynamic = FALSE;
4046 else
4047 {
4048 dynamic = TRUE;
4049
4050 /* You can't use -r against a dynamic object. Also, there's no
4051 hope of using a dynamic object which does not exactly match
4052 the format of the output file. */
4053 if (bfd_link_relocatable (info)
4054 || !is_elf_hash_table (htab)
4055 || info->output_bfd->xvec != abfd->xvec)
4056 {
4057 if (bfd_link_relocatable (info))
4058 bfd_set_error (bfd_error_invalid_operation);
4059 else
4060 bfd_set_error (bfd_error_wrong_format);
4061 goto error_return;
4062 }
4063 }
4064
4065 ehdr = elf_elfheader (abfd);
4066 if (info->warn_alternate_em
4067 && bed->elf_machine_code != ehdr->e_machine
4068 && ((bed->elf_machine_alt1 != 0
4069 && ehdr->e_machine == bed->elf_machine_alt1)
4070 || (bed->elf_machine_alt2 != 0
4071 && ehdr->e_machine == bed->elf_machine_alt2)))
4072 _bfd_error_handler
4073 /* xgettext:c-format */
4074 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4075 ehdr->e_machine, abfd, bed->elf_machine_code);
4076
4077 /* As a GNU extension, any input sections which are named
4078 .gnu.warning.SYMBOL are treated as warning symbols for the given
4079 symbol. This differs from .gnu.warning sections, which generate
4080 warnings when they are included in an output file. */
4081 /* PR 12761: Also generate this warning when building shared libraries. */
4082 for (s = abfd->sections; s != NULL; s = s->next)
4083 {
4084 const char *name;
4085
4086 name = bfd_section_name (s);
4087 if (CONST_STRNEQ (name, ".gnu.warning."))
4088 {
4089 char *msg;
4090 bfd_size_type sz;
4091
4092 name += sizeof ".gnu.warning." - 1;
4093
4094 /* If this is a shared object, then look up the symbol
4095 in the hash table. If it is there, and it is already
4096 been defined, then we will not be using the entry
4097 from this shared object, so we don't need to warn.
4098 FIXME: If we see the definition in a regular object
4099 later on, we will warn, but we shouldn't. The only
4100 fix is to keep track of what warnings we are supposed
4101 to emit, and then handle them all at the end of the
4102 link. */
4103 if (dynamic)
4104 {
4105 struct elf_link_hash_entry *h;
4106
4107 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4108
4109 /* FIXME: What about bfd_link_hash_common? */
4110 if (h != NULL
4111 && (h->root.type == bfd_link_hash_defined
4112 || h->root.type == bfd_link_hash_defweak))
4113 continue;
4114 }
4115
4116 sz = s->size;
4117 msg = (char *) bfd_alloc (abfd, sz + 1);
4118 if (msg == NULL)
4119 goto error_return;
4120
4121 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4122 goto error_return;
4123
4124 msg[sz] = '\0';
4125
4126 if (! (_bfd_generic_link_add_one_symbol
4127 (info, abfd, name, BSF_WARNING, s, 0, msg,
4128 FALSE, bed->collect, NULL)))
4129 goto error_return;
4130
4131 if (bfd_link_executable (info))
4132 {
4133 /* Clobber the section size so that the warning does
4134 not get copied into the output file. */
4135 s->size = 0;
4136
4137 /* Also set SEC_EXCLUDE, so that symbols defined in
4138 the warning section don't get copied to the output. */
4139 s->flags |= SEC_EXCLUDE;
4140 }
4141 }
4142 }
4143
4144 just_syms = ((s = abfd->sections) != NULL
4145 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4146
4147 add_needed = TRUE;
4148 if (! dynamic)
4149 {
4150 /* If we are creating a shared library, create all the dynamic
4151 sections immediately. We need to attach them to something,
4152 so we attach them to this BFD, provided it is the right
4153 format and is not from ld --just-symbols. Always create the
4154 dynamic sections for -E/--dynamic-list. FIXME: If there
4155 are no input BFD's of the same format as the output, we can't
4156 make a shared library. */
4157 if (!just_syms
4158 && (bfd_link_pic (info)
4159 || (!bfd_link_relocatable (info)
4160 && info->nointerp
4161 && (info->export_dynamic || info->dynamic)))
4162 && is_elf_hash_table (htab)
4163 && info->output_bfd->xvec == abfd->xvec
4164 && !htab->dynamic_sections_created)
4165 {
4166 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4167 goto error_return;
4168 }
4169 }
4170 else if (!is_elf_hash_table (htab))
4171 goto error_return;
4172 else
4173 {
4174 const char *soname = NULL;
4175 char *audit = NULL;
4176 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4177 const Elf_Internal_Phdr *phdr;
4178 struct elf_link_loaded_list *loaded_lib;
4179
4180 /* ld --just-symbols and dynamic objects don't mix very well.
4181 ld shouldn't allow it. */
4182 if (just_syms)
4183 abort ();
4184
4185 /* If this dynamic lib was specified on the command line with
4186 --as-needed in effect, then we don't want to add a DT_NEEDED
4187 tag unless the lib is actually used. Similary for libs brought
4188 in by another lib's DT_NEEDED. When --no-add-needed is used
4189 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4190 any dynamic library in DT_NEEDED tags in the dynamic lib at
4191 all. */
4192 add_needed = (elf_dyn_lib_class (abfd)
4193 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4194 | DYN_NO_NEEDED)) == 0;
4195
4196 s = bfd_get_section_by_name (abfd, ".dynamic");
4197 if (s != NULL)
4198 {
4199 bfd_byte *dynbuf;
4200 bfd_byte *extdyn;
4201 unsigned int elfsec;
4202 unsigned long shlink;
4203
4204 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4205 {
4206 error_free_dyn:
4207 free (dynbuf);
4208 goto error_return;
4209 }
4210
4211 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4212 if (elfsec == SHN_BAD)
4213 goto error_free_dyn;
4214 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4215
4216 for (extdyn = dynbuf;
4217 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4218 extdyn += bed->s->sizeof_dyn)
4219 {
4220 Elf_Internal_Dyn dyn;
4221
4222 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4223 if (dyn.d_tag == DT_SONAME)
4224 {
4225 unsigned int tagv = dyn.d_un.d_val;
4226 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4227 if (soname == NULL)
4228 goto error_free_dyn;
4229 }
4230 if (dyn.d_tag == DT_NEEDED)
4231 {
4232 struct bfd_link_needed_list *n, **pn;
4233 char *fnm, *anm;
4234 unsigned int tagv = dyn.d_un.d_val;
4235 size_t amt = sizeof (struct bfd_link_needed_list);
4236
4237 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4238 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4239 if (n == NULL || fnm == NULL)
4240 goto error_free_dyn;
4241 amt = strlen (fnm) + 1;
4242 anm = (char *) bfd_alloc (abfd, amt);
4243 if (anm == NULL)
4244 goto error_free_dyn;
4245 memcpy (anm, fnm, amt);
4246 n->name = anm;
4247 n->by = abfd;
4248 n->next = NULL;
4249 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4250 ;
4251 *pn = n;
4252 }
4253 if (dyn.d_tag == DT_RUNPATH)
4254 {
4255 struct bfd_link_needed_list *n, **pn;
4256 char *fnm, *anm;
4257 unsigned int tagv = dyn.d_un.d_val;
4258 size_t amt = sizeof (struct bfd_link_needed_list);
4259
4260 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4261 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4262 if (n == NULL || fnm == NULL)
4263 goto error_free_dyn;
4264 amt = strlen (fnm) + 1;
4265 anm = (char *) bfd_alloc (abfd, amt);
4266 if (anm == NULL)
4267 goto error_free_dyn;
4268 memcpy (anm, fnm, amt);
4269 n->name = anm;
4270 n->by = abfd;
4271 n->next = NULL;
4272 for (pn = & runpath;
4273 *pn != NULL;
4274 pn = &(*pn)->next)
4275 ;
4276 *pn = n;
4277 }
4278 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4279 if (!runpath && dyn.d_tag == DT_RPATH)
4280 {
4281 struct bfd_link_needed_list *n, **pn;
4282 char *fnm, *anm;
4283 unsigned int tagv = dyn.d_un.d_val;
4284 size_t amt = sizeof (struct bfd_link_needed_list);
4285
4286 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4287 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4288 if (n == NULL || fnm == NULL)
4289 goto error_free_dyn;
4290 amt = strlen (fnm) + 1;
4291 anm = (char *) bfd_alloc (abfd, amt);
4292 if (anm == NULL)
4293 goto error_free_dyn;
4294 memcpy (anm, fnm, amt);
4295 n->name = anm;
4296 n->by = abfd;
4297 n->next = NULL;
4298 for (pn = & rpath;
4299 *pn != NULL;
4300 pn = &(*pn)->next)
4301 ;
4302 *pn = n;
4303 }
4304 if (dyn.d_tag == DT_AUDIT)
4305 {
4306 unsigned int tagv = dyn.d_un.d_val;
4307 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4308 }
4309 }
4310
4311 free (dynbuf);
4312 }
4313
4314 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4315 frees all more recently bfd_alloc'd blocks as well. */
4316 if (runpath)
4317 rpath = runpath;
4318
4319 if (rpath)
4320 {
4321 struct bfd_link_needed_list **pn;
4322 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4323 ;
4324 *pn = rpath;
4325 }
4326
4327 /* If we have a PT_GNU_RELRO program header, mark as read-only
4328 all sections contained fully therein. This makes relro
4329 shared library sections appear as they will at run-time. */
4330 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4331 while (phdr-- > elf_tdata (abfd)->phdr)
4332 if (phdr->p_type == PT_GNU_RELRO)
4333 {
4334 for (s = abfd->sections; s != NULL; s = s->next)
4335 {
4336 unsigned int opb = bfd_octets_per_byte (abfd, s);
4337
4338 if ((s->flags & SEC_ALLOC) != 0
4339 && s->vma * opb >= phdr->p_vaddr
4340 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4341 s->flags |= SEC_READONLY;
4342 }
4343 break;
4344 }
4345
4346 /* We do not want to include any of the sections in a dynamic
4347 object in the output file. We hack by simply clobbering the
4348 list of sections in the BFD. This could be handled more
4349 cleanly by, say, a new section flag; the existing
4350 SEC_NEVER_LOAD flag is not the one we want, because that one
4351 still implies that the section takes up space in the output
4352 file. */
4353 bfd_section_list_clear (abfd);
4354
4355 /* Find the name to use in a DT_NEEDED entry that refers to this
4356 object. If the object has a DT_SONAME entry, we use it.
4357 Otherwise, if the generic linker stuck something in
4358 elf_dt_name, we use that. Otherwise, we just use the file
4359 name. */
4360 if (soname == NULL || *soname == '\0')
4361 {
4362 soname = elf_dt_name (abfd);
4363 if (soname == NULL || *soname == '\0')
4364 soname = bfd_get_filename (abfd);
4365 }
4366
4367 /* Save the SONAME because sometimes the linker emulation code
4368 will need to know it. */
4369 elf_dt_name (abfd) = soname;
4370
4371 /* If we have already included this dynamic object in the
4372 link, just ignore it. There is no reason to include a
4373 particular dynamic object more than once. */
4374 for (loaded_lib = htab->dyn_loaded;
4375 loaded_lib != NULL;
4376 loaded_lib = loaded_lib->next)
4377 {
4378 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4379 return TRUE;
4380 }
4381
4382 /* Create dynamic sections for backends that require that be done
4383 before setup_gnu_properties. */
4384 if (add_needed
4385 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4386 return FALSE;
4387
4388 /* Save the DT_AUDIT entry for the linker emulation code. */
4389 elf_dt_audit (abfd) = audit;
4390 }
4391
4392 /* If this is a dynamic object, we always link against the .dynsym
4393 symbol table, not the .symtab symbol table. The dynamic linker
4394 will only see the .dynsym symbol table, so there is no reason to
4395 look at .symtab for a dynamic object. */
4396
4397 if (! dynamic || elf_dynsymtab (abfd) == 0)
4398 hdr = &elf_tdata (abfd)->symtab_hdr;
4399 else
4400 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4401
4402 symcount = hdr->sh_size / bed->s->sizeof_sym;
4403
4404 /* The sh_info field of the symtab header tells us where the
4405 external symbols start. We don't care about the local symbols at
4406 this point. */
4407 if (elf_bad_symtab (abfd))
4408 {
4409 extsymcount = symcount;
4410 extsymoff = 0;
4411 }
4412 else
4413 {
4414 extsymcount = symcount - hdr->sh_info;
4415 extsymoff = hdr->sh_info;
4416 }
4417
4418 sym_hash = elf_sym_hashes (abfd);
4419 if (extsymcount != 0)
4420 {
4421 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4422 NULL, NULL, NULL);
4423 if (isymbuf == NULL)
4424 goto error_return;
4425
4426 if (sym_hash == NULL)
4427 {
4428 /* We store a pointer to the hash table entry for each
4429 external symbol. */
4430 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4431 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4432 if (sym_hash == NULL)
4433 goto error_free_sym;
4434 elf_sym_hashes (abfd) = sym_hash;
4435 }
4436 }
4437
4438 if (dynamic)
4439 {
4440 /* Read in any version definitions. */
4441 if (!_bfd_elf_slurp_version_tables (abfd,
4442 info->default_imported_symver))
4443 goto error_free_sym;
4444
4445 /* Read in the symbol versions, but don't bother to convert them
4446 to internal format. */
4447 if (elf_dynversym (abfd) != 0)
4448 {
4449 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4450 bfd_size_type amt = versymhdr->sh_size;
4451
4452 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4453 goto error_free_sym;
4454 extversym = (Elf_External_Versym *)
4455 _bfd_malloc_and_read (abfd, amt, amt);
4456 if (extversym == NULL)
4457 goto error_free_sym;
4458 extversym_end = extversym + amt / sizeof (*extversym);
4459 }
4460 }
4461
4462 /* If we are loading an as-needed shared lib, save the symbol table
4463 state before we start adding symbols. If the lib turns out
4464 to be unneeded, restore the state. */
4465 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4466 {
4467 unsigned int i;
4468 size_t entsize;
4469
4470 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4471 {
4472 struct bfd_hash_entry *p;
4473 struct elf_link_hash_entry *h;
4474
4475 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4476 {
4477 h = (struct elf_link_hash_entry *) p;
4478 entsize += htab->root.table.entsize;
4479 if (h->root.type == bfd_link_hash_warning)
4480 entsize += htab->root.table.entsize;
4481 }
4482 }
4483
4484 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4485 old_tab = bfd_malloc (tabsize + entsize);
4486 if (old_tab == NULL)
4487 goto error_free_vers;
4488
4489 /* Remember the current objalloc pointer, so that all mem for
4490 symbols added can later be reclaimed. */
4491 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4492 if (alloc_mark == NULL)
4493 goto error_free_vers;
4494
4495 /* Make a special call to the linker "notice" function to
4496 tell it that we are about to handle an as-needed lib. */
4497 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4498 goto error_free_vers;
4499
4500 /* Clone the symbol table. Remember some pointers into the
4501 symbol table, and dynamic symbol count. */
4502 old_ent = (char *) old_tab + tabsize;
4503 memcpy (old_tab, htab->root.table.table, tabsize);
4504 old_undefs = htab->root.undefs;
4505 old_undefs_tail = htab->root.undefs_tail;
4506 old_table = htab->root.table.table;
4507 old_size = htab->root.table.size;
4508 old_count = htab->root.table.count;
4509 old_strtab = NULL;
4510 if (htab->dynstr != NULL)
4511 {
4512 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4513 if (old_strtab == NULL)
4514 goto error_free_vers;
4515 }
4516
4517 for (i = 0; i < htab->root.table.size; i++)
4518 {
4519 struct bfd_hash_entry *p;
4520 struct elf_link_hash_entry *h;
4521
4522 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4523 {
4524 memcpy (old_ent, p, htab->root.table.entsize);
4525 old_ent = (char *) old_ent + htab->root.table.entsize;
4526 h = (struct elf_link_hash_entry *) p;
4527 if (h->root.type == bfd_link_hash_warning)
4528 {
4529 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4530 old_ent = (char *) old_ent + htab->root.table.entsize;
4531 }
4532 }
4533 }
4534 }
4535
4536 weaks = NULL;
4537 if (extversym == NULL)
4538 ever = NULL;
4539 else if (extversym + extsymoff < extversym_end)
4540 ever = extversym + extsymoff;
4541 else
4542 {
4543 /* xgettext:c-format */
4544 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4545 abfd, (long) extsymoff,
4546 (long) (extversym_end - extversym) / sizeof (* extversym));
4547 bfd_set_error (bfd_error_bad_value);
4548 goto error_free_vers;
4549 }
4550
4551 if (!bfd_link_relocatable (info)
4552 && abfd->lto_slim_object)
4553 {
4554 _bfd_error_handler
4555 (_("%pB: plugin needed to handle lto object"), abfd);
4556 }
4557
4558 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4559 isym < isymend;
4560 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4561 {
4562 int bind;
4563 bfd_vma value;
4564 asection *sec, *new_sec;
4565 flagword flags;
4566 const char *name;
4567 struct elf_link_hash_entry *h;
4568 struct elf_link_hash_entry *hi;
4569 bfd_boolean definition;
4570 bfd_boolean size_change_ok;
4571 bfd_boolean type_change_ok;
4572 bfd_boolean new_weak;
4573 bfd_boolean old_weak;
4574 bfd_boolean override;
4575 bfd_boolean common;
4576 bfd_boolean discarded;
4577 unsigned int old_alignment;
4578 unsigned int shindex;
4579 bfd *old_bfd;
4580 bfd_boolean matched;
4581
4582 override = FALSE;
4583
4584 flags = BSF_NO_FLAGS;
4585 sec = NULL;
4586 value = isym->st_value;
4587 common = bed->common_definition (isym);
4588 if (common && info->inhibit_common_definition)
4589 {
4590 /* Treat common symbol as undefined for --no-define-common. */
4591 isym->st_shndx = SHN_UNDEF;
4592 common = FALSE;
4593 }
4594 discarded = FALSE;
4595
4596 bind = ELF_ST_BIND (isym->st_info);
4597 switch (bind)
4598 {
4599 case STB_LOCAL:
4600 /* This should be impossible, since ELF requires that all
4601 global symbols follow all local symbols, and that sh_info
4602 point to the first global symbol. Unfortunately, Irix 5
4603 screws this up. */
4604 if (elf_bad_symtab (abfd))
4605 continue;
4606
4607 /* If we aren't prepared to handle locals within the globals
4608 then we'll likely segfault on a NULL symbol hash if the
4609 symbol is ever referenced in relocations. */
4610 shindex = elf_elfheader (abfd)->e_shstrndx;
4611 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4612 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4613 " (>= sh_info of %lu)"),
4614 abfd, name, (long) (isym - isymbuf + extsymoff),
4615 (long) extsymoff);
4616
4617 /* Dynamic object relocations are not processed by ld, so
4618 ld won't run into the problem mentioned above. */
4619 if (dynamic)
4620 continue;
4621 bfd_set_error (bfd_error_bad_value);
4622 goto error_free_vers;
4623
4624 case STB_GLOBAL:
4625 if (isym->st_shndx != SHN_UNDEF && !common)
4626 flags = BSF_GLOBAL;
4627 break;
4628
4629 case STB_WEAK:
4630 flags = BSF_WEAK;
4631 break;
4632
4633 case STB_GNU_UNIQUE:
4634 flags = BSF_GNU_UNIQUE;
4635 break;
4636
4637 default:
4638 /* Leave it up to the processor backend. */
4639 break;
4640 }
4641
4642 if (isym->st_shndx == SHN_UNDEF)
4643 sec = bfd_und_section_ptr;
4644 else if (isym->st_shndx == SHN_ABS)
4645 sec = bfd_abs_section_ptr;
4646 else if (isym->st_shndx == SHN_COMMON)
4647 {
4648 sec = bfd_com_section_ptr;
4649 /* What ELF calls the size we call the value. What ELF
4650 calls the value we call the alignment. */
4651 value = isym->st_size;
4652 }
4653 else
4654 {
4655 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4656 if (sec == NULL)
4657 sec = bfd_abs_section_ptr;
4658 else if (discarded_section (sec))
4659 {
4660 /* Symbols from discarded section are undefined. We keep
4661 its visibility. */
4662 sec = bfd_und_section_ptr;
4663 discarded = TRUE;
4664 isym->st_shndx = SHN_UNDEF;
4665 }
4666 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4667 value -= sec->vma;
4668 }
4669
4670 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4671 isym->st_name);
4672 if (name == NULL)
4673 goto error_free_vers;
4674
4675 if (isym->st_shndx == SHN_COMMON
4676 && (abfd->flags & BFD_PLUGIN) != 0)
4677 {
4678 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4679
4680 if (xc == NULL)
4681 {
4682 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4683 | SEC_EXCLUDE);
4684 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4685 if (xc == NULL)
4686 goto error_free_vers;
4687 }
4688 sec = xc;
4689 }
4690 else if (isym->st_shndx == SHN_COMMON
4691 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4692 && !bfd_link_relocatable (info))
4693 {
4694 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4695
4696 if (tcomm == NULL)
4697 {
4698 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4699 | SEC_LINKER_CREATED);
4700 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4701 if (tcomm == NULL)
4702 goto error_free_vers;
4703 }
4704 sec = tcomm;
4705 }
4706 else if (bed->elf_add_symbol_hook)
4707 {
4708 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4709 &sec, &value))
4710 goto error_free_vers;
4711
4712 /* The hook function sets the name to NULL if this symbol
4713 should be skipped for some reason. */
4714 if (name == NULL)
4715 continue;
4716 }
4717
4718 /* Sanity check that all possibilities were handled. */
4719 if (sec == NULL)
4720 abort ();
4721
4722 /* Silently discard TLS symbols from --just-syms. There's
4723 no way to combine a static TLS block with a new TLS block
4724 for this executable. */
4725 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4726 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4727 continue;
4728
4729 if (bfd_is_und_section (sec)
4730 || bfd_is_com_section (sec))
4731 definition = FALSE;
4732 else
4733 definition = TRUE;
4734
4735 size_change_ok = FALSE;
4736 type_change_ok = bed->type_change_ok;
4737 old_weak = FALSE;
4738 matched = FALSE;
4739 old_alignment = 0;
4740 old_bfd = NULL;
4741 new_sec = sec;
4742
4743 if (is_elf_hash_table (htab))
4744 {
4745 Elf_Internal_Versym iver;
4746 unsigned int vernum = 0;
4747 bfd_boolean skip;
4748
4749 if (ever == NULL)
4750 {
4751 if (info->default_imported_symver)
4752 /* Use the default symbol version created earlier. */
4753 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4754 else
4755 iver.vs_vers = 0;
4756 }
4757 else if (ever >= extversym_end)
4758 {
4759 /* xgettext:c-format */
4760 _bfd_error_handler (_("%pB: not enough version information"),
4761 abfd);
4762 bfd_set_error (bfd_error_bad_value);
4763 goto error_free_vers;
4764 }
4765 else
4766 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4767
4768 vernum = iver.vs_vers & VERSYM_VERSION;
4769
4770 /* If this is a hidden symbol, or if it is not version
4771 1, we append the version name to the symbol name.
4772 However, we do not modify a non-hidden absolute symbol
4773 if it is not a function, because it might be the version
4774 symbol itself. FIXME: What if it isn't? */
4775 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4776 || (vernum > 1
4777 && (!bfd_is_abs_section (sec)
4778 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4779 {
4780 const char *verstr;
4781 size_t namelen, verlen, newlen;
4782 char *newname, *p;
4783
4784 if (isym->st_shndx != SHN_UNDEF)
4785 {
4786 if (vernum > elf_tdata (abfd)->cverdefs)
4787 verstr = NULL;
4788 else if (vernum > 1)
4789 verstr =
4790 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4791 else
4792 verstr = "";
4793
4794 if (verstr == NULL)
4795 {
4796 _bfd_error_handler
4797 /* xgettext:c-format */
4798 (_("%pB: %s: invalid version %u (max %d)"),
4799 abfd, name, vernum,
4800 elf_tdata (abfd)->cverdefs);
4801 bfd_set_error (bfd_error_bad_value);
4802 goto error_free_vers;
4803 }
4804 }
4805 else
4806 {
4807 /* We cannot simply test for the number of
4808 entries in the VERNEED section since the
4809 numbers for the needed versions do not start
4810 at 0. */
4811 Elf_Internal_Verneed *t;
4812
4813 verstr = NULL;
4814 for (t = elf_tdata (abfd)->verref;
4815 t != NULL;
4816 t = t->vn_nextref)
4817 {
4818 Elf_Internal_Vernaux *a;
4819
4820 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4821 {
4822 if (a->vna_other == vernum)
4823 {
4824 verstr = a->vna_nodename;
4825 break;
4826 }
4827 }
4828 if (a != NULL)
4829 break;
4830 }
4831 if (verstr == NULL)
4832 {
4833 _bfd_error_handler
4834 /* xgettext:c-format */
4835 (_("%pB: %s: invalid needed version %d"),
4836 abfd, name, vernum);
4837 bfd_set_error (bfd_error_bad_value);
4838 goto error_free_vers;
4839 }
4840 }
4841
4842 namelen = strlen (name);
4843 verlen = strlen (verstr);
4844 newlen = namelen + verlen + 2;
4845 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4846 && isym->st_shndx != SHN_UNDEF)
4847 ++newlen;
4848
4849 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4850 if (newname == NULL)
4851 goto error_free_vers;
4852 memcpy (newname, name, namelen);
4853 p = newname + namelen;
4854 *p++ = ELF_VER_CHR;
4855 /* If this is a defined non-hidden version symbol,
4856 we add another @ to the name. This indicates the
4857 default version of the symbol. */
4858 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4859 && isym->st_shndx != SHN_UNDEF)
4860 *p++ = ELF_VER_CHR;
4861 memcpy (p, verstr, verlen + 1);
4862
4863 name = newname;
4864 }
4865
4866 /* If this symbol has default visibility and the user has
4867 requested we not re-export it, then mark it as hidden. */
4868 if (!bfd_is_und_section (sec)
4869 && !dynamic
4870 && abfd->no_export
4871 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4872 isym->st_other = (STV_HIDDEN
4873 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4874
4875 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4876 sym_hash, &old_bfd, &old_weak,
4877 &old_alignment, &skip, &override,
4878 &type_change_ok, &size_change_ok,
4879 &matched))
4880 goto error_free_vers;
4881
4882 if (skip)
4883 continue;
4884
4885 /* Override a definition only if the new symbol matches the
4886 existing one. */
4887 if (override && matched)
4888 definition = FALSE;
4889
4890 h = *sym_hash;
4891 while (h->root.type == bfd_link_hash_indirect
4892 || h->root.type == bfd_link_hash_warning)
4893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4894
4895 if (elf_tdata (abfd)->verdef != NULL
4896 && vernum > 1
4897 && definition)
4898 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4899 }
4900
4901 if (! (_bfd_generic_link_add_one_symbol
4902 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4903 (struct bfd_link_hash_entry **) sym_hash)))
4904 goto error_free_vers;
4905
4906 h = *sym_hash;
4907 /* We need to make sure that indirect symbol dynamic flags are
4908 updated. */
4909 hi = h;
4910 while (h->root.type == bfd_link_hash_indirect
4911 || h->root.type == bfd_link_hash_warning)
4912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4913
4914 /* Setting the index to -3 tells elf_link_output_extsym that
4915 this symbol is defined in a discarded section. */
4916 if (discarded)
4917 h->indx = -3;
4918
4919 *sym_hash = h;
4920
4921 new_weak = (flags & BSF_WEAK) != 0;
4922 if (dynamic
4923 && definition
4924 && new_weak
4925 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4926 && is_elf_hash_table (htab)
4927 && h->u.alias == NULL)
4928 {
4929 /* Keep a list of all weak defined non function symbols from
4930 a dynamic object, using the alias field. Later in this
4931 function we will set the alias field to the correct
4932 value. We only put non-function symbols from dynamic
4933 objects on this list, because that happens to be the only
4934 time we need to know the normal symbol corresponding to a
4935 weak symbol, and the information is time consuming to
4936 figure out. If the alias field is not already NULL,
4937 then this symbol was already defined by some previous
4938 dynamic object, and we will be using that previous
4939 definition anyhow. */
4940
4941 h->u.alias = weaks;
4942 weaks = h;
4943 }
4944
4945 /* Set the alignment of a common symbol. */
4946 if ((common || bfd_is_com_section (sec))
4947 && h->root.type == bfd_link_hash_common)
4948 {
4949 unsigned int align;
4950
4951 if (common)
4952 align = bfd_log2 (isym->st_value);
4953 else
4954 {
4955 /* The new symbol is a common symbol in a shared object.
4956 We need to get the alignment from the section. */
4957 align = new_sec->alignment_power;
4958 }
4959 if (align > old_alignment)
4960 h->root.u.c.p->alignment_power = align;
4961 else
4962 h->root.u.c.p->alignment_power = old_alignment;
4963 }
4964
4965 if (is_elf_hash_table (htab))
4966 {
4967 /* Set a flag in the hash table entry indicating the type of
4968 reference or definition we just found. A dynamic symbol
4969 is one which is referenced or defined by both a regular
4970 object and a shared object. */
4971 bfd_boolean dynsym = FALSE;
4972
4973 /* Plugin symbols aren't normal. Don't set def_regular or
4974 ref_regular for them, or make them dynamic. */
4975 if ((abfd->flags & BFD_PLUGIN) != 0)
4976 ;
4977 else if (! dynamic)
4978 {
4979 if (! definition)
4980 {
4981 h->ref_regular = 1;
4982 if (bind != STB_WEAK)
4983 h->ref_regular_nonweak = 1;
4984 }
4985 else
4986 {
4987 h->def_regular = 1;
4988 if (h->def_dynamic)
4989 {
4990 h->def_dynamic = 0;
4991 h->ref_dynamic = 1;
4992 }
4993 }
4994
4995 /* If the indirect symbol has been forced local, don't
4996 make the real symbol dynamic. */
4997 if ((h == hi || !hi->forced_local)
4998 && (bfd_link_dll (info)
4999 || h->def_dynamic
5000 || h->ref_dynamic))
5001 dynsym = TRUE;
5002 }
5003 else
5004 {
5005 if (! definition)
5006 {
5007 h->ref_dynamic = 1;
5008 hi->ref_dynamic = 1;
5009 }
5010 else
5011 {
5012 h->def_dynamic = 1;
5013 hi->def_dynamic = 1;
5014 }
5015
5016 /* If the indirect symbol has been forced local, don't
5017 make the real symbol dynamic. */
5018 if ((h == hi || !hi->forced_local)
5019 && (h->def_regular
5020 || h->ref_regular
5021 || (h->is_weakalias
5022 && weakdef (h)->dynindx != -1)))
5023 dynsym = TRUE;
5024 }
5025
5026 /* Check to see if we need to add an indirect symbol for
5027 the default name. */
5028 if (definition
5029 || (!override && h->root.type == bfd_link_hash_common))
5030 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5031 sec, value, &old_bfd, &dynsym))
5032 goto error_free_vers;
5033
5034 /* Check the alignment when a common symbol is involved. This
5035 can change when a common symbol is overridden by a normal
5036 definition or a common symbol is ignored due to the old
5037 normal definition. We need to make sure the maximum
5038 alignment is maintained. */
5039 if ((old_alignment || common)
5040 && h->root.type != bfd_link_hash_common)
5041 {
5042 unsigned int common_align;
5043 unsigned int normal_align;
5044 unsigned int symbol_align;
5045 bfd *normal_bfd;
5046 bfd *common_bfd;
5047
5048 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5049 || h->root.type == bfd_link_hash_defweak);
5050
5051 symbol_align = ffs (h->root.u.def.value) - 1;
5052 if (h->root.u.def.section->owner != NULL
5053 && (h->root.u.def.section->owner->flags
5054 & (DYNAMIC | BFD_PLUGIN)) == 0)
5055 {
5056 normal_align = h->root.u.def.section->alignment_power;
5057 if (normal_align > symbol_align)
5058 normal_align = symbol_align;
5059 }
5060 else
5061 normal_align = symbol_align;
5062
5063 if (old_alignment)
5064 {
5065 common_align = old_alignment;
5066 common_bfd = old_bfd;
5067 normal_bfd = abfd;
5068 }
5069 else
5070 {
5071 common_align = bfd_log2 (isym->st_value);
5072 common_bfd = abfd;
5073 normal_bfd = old_bfd;
5074 }
5075
5076 if (normal_align < common_align)
5077 {
5078 /* PR binutils/2735 */
5079 if (normal_bfd == NULL)
5080 _bfd_error_handler
5081 /* xgettext:c-format */
5082 (_("warning: alignment %u of common symbol `%s' in %pB is"
5083 " greater than the alignment (%u) of its section %pA"),
5084 1 << common_align, name, common_bfd,
5085 1 << normal_align, h->root.u.def.section);
5086 else
5087 _bfd_error_handler
5088 /* xgettext:c-format */
5089 (_("warning: alignment %u of symbol `%s' in %pB"
5090 " is smaller than %u in %pB"),
5091 1 << normal_align, name, normal_bfd,
5092 1 << common_align, common_bfd);
5093 }
5094 }
5095
5096 /* Remember the symbol size if it isn't undefined. */
5097 if (isym->st_size != 0
5098 && isym->st_shndx != SHN_UNDEF
5099 && (definition || h->size == 0))
5100 {
5101 if (h->size != 0
5102 && h->size != isym->st_size
5103 && ! size_change_ok)
5104 _bfd_error_handler
5105 /* xgettext:c-format */
5106 (_("warning: size of symbol `%s' changed"
5107 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5108 name, (uint64_t) h->size, old_bfd,
5109 (uint64_t) isym->st_size, abfd);
5110
5111 h->size = isym->st_size;
5112 }
5113
5114 /* If this is a common symbol, then we always want H->SIZE
5115 to be the size of the common symbol. The code just above
5116 won't fix the size if a common symbol becomes larger. We
5117 don't warn about a size change here, because that is
5118 covered by --warn-common. Allow changes between different
5119 function types. */
5120 if (h->root.type == bfd_link_hash_common)
5121 h->size = h->root.u.c.size;
5122
5123 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5124 && ((definition && !new_weak)
5125 || (old_weak && h->root.type == bfd_link_hash_common)
5126 || h->type == STT_NOTYPE))
5127 {
5128 unsigned int type = ELF_ST_TYPE (isym->st_info);
5129
5130 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5131 symbol. */
5132 if (type == STT_GNU_IFUNC
5133 && (abfd->flags & DYNAMIC) != 0)
5134 type = STT_FUNC;
5135
5136 if (h->type != type)
5137 {
5138 if (h->type != STT_NOTYPE && ! type_change_ok)
5139 /* xgettext:c-format */
5140 _bfd_error_handler
5141 (_("warning: type of symbol `%s' changed"
5142 " from %d to %d in %pB"),
5143 name, h->type, type, abfd);
5144
5145 h->type = type;
5146 }
5147 }
5148
5149 /* Merge st_other field. */
5150 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5151
5152 /* We don't want to make debug symbol dynamic. */
5153 if (definition
5154 && (sec->flags & SEC_DEBUGGING)
5155 && !bfd_link_relocatable (info))
5156 dynsym = FALSE;
5157
5158 /* Nor should we make plugin symbols dynamic. */
5159 if ((abfd->flags & BFD_PLUGIN) != 0)
5160 dynsym = FALSE;
5161
5162 if (definition)
5163 {
5164 h->target_internal = isym->st_target_internal;
5165 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5166 }
5167
5168 if (definition && !dynamic)
5169 {
5170 char *p = strchr (name, ELF_VER_CHR);
5171 if (p != NULL && p[1] != ELF_VER_CHR)
5172 {
5173 /* Queue non-default versions so that .symver x, x@FOO
5174 aliases can be checked. */
5175 if (!nondeflt_vers)
5176 {
5177 size_t amt = ((isymend - isym + 1)
5178 * sizeof (struct elf_link_hash_entry *));
5179 nondeflt_vers
5180 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5181 if (!nondeflt_vers)
5182 goto error_free_vers;
5183 }
5184 nondeflt_vers[nondeflt_vers_cnt++] = h;
5185 }
5186 }
5187
5188 if (dynsym && h->dynindx == -1)
5189 {
5190 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5191 goto error_free_vers;
5192 if (h->is_weakalias
5193 && weakdef (h)->dynindx == -1)
5194 {
5195 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5196 goto error_free_vers;
5197 }
5198 }
5199 else if (h->dynindx != -1)
5200 /* If the symbol already has a dynamic index, but
5201 visibility says it should not be visible, turn it into
5202 a local symbol. */
5203 switch (ELF_ST_VISIBILITY (h->other))
5204 {
5205 case STV_INTERNAL:
5206 case STV_HIDDEN:
5207 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5208 dynsym = FALSE;
5209 break;
5210 }
5211
5212 if (!add_needed
5213 && matched
5214 && definition
5215 && ((dynsym
5216 && h->ref_regular_nonweak)
5217 || (h->ref_dynamic_nonweak
5218 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5219 && !on_needed_list (elf_dt_name (abfd),
5220 htab->needed, NULL))))
5221 {
5222 const char *soname = elf_dt_name (abfd);
5223
5224 info->callbacks->minfo ("%!", soname, old_bfd,
5225 h->root.root.string);
5226
5227 /* A symbol from a library loaded via DT_NEEDED of some
5228 other library is referenced by a regular object.
5229 Add a DT_NEEDED entry for it. Issue an error if
5230 --no-add-needed is used and the reference was not
5231 a weak one. */
5232 if (old_bfd != NULL
5233 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5234 {
5235 _bfd_error_handler
5236 /* xgettext:c-format */
5237 (_("%pB: undefined reference to symbol '%s'"),
5238 old_bfd, name);
5239 bfd_set_error (bfd_error_missing_dso);
5240 goto error_free_vers;
5241 }
5242
5243 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5244 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5245
5246 /* Create dynamic sections for backends that require
5247 that be done before setup_gnu_properties. */
5248 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5249 return FALSE;
5250 add_needed = TRUE;
5251 }
5252 }
5253 }
5254
5255 if (info->lto_plugin_active
5256 && !bfd_link_relocatable (info)
5257 && (abfd->flags & BFD_PLUGIN) == 0
5258 && !just_syms
5259 && extsymcount)
5260 {
5261 int r_sym_shift;
5262
5263 if (bed->s->arch_size == 32)
5264 r_sym_shift = 8;
5265 else
5266 r_sym_shift = 32;
5267
5268 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5269 referenced in regular objects so that linker plugin will get
5270 the correct symbol resolution. */
5271
5272 sym_hash = elf_sym_hashes (abfd);
5273 for (s = abfd->sections; s != NULL; s = s->next)
5274 {
5275 Elf_Internal_Rela *internal_relocs;
5276 Elf_Internal_Rela *rel, *relend;
5277
5278 /* Don't check relocations in excluded sections. */
5279 if ((s->flags & SEC_RELOC) == 0
5280 || s->reloc_count == 0
5281 || (s->flags & SEC_EXCLUDE) != 0
5282 || ((info->strip == strip_all
5283 || info->strip == strip_debugger)
5284 && (s->flags & SEC_DEBUGGING) != 0))
5285 continue;
5286
5287 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5288 NULL,
5289 info->keep_memory);
5290 if (internal_relocs == NULL)
5291 goto error_free_vers;
5292
5293 rel = internal_relocs;
5294 relend = rel + s->reloc_count;
5295 for ( ; rel < relend; rel++)
5296 {
5297 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5298 struct elf_link_hash_entry *h;
5299
5300 /* Skip local symbols. */
5301 if (r_symndx < extsymoff)
5302 continue;
5303
5304 h = sym_hash[r_symndx - extsymoff];
5305 if (h != NULL)
5306 h->root.non_ir_ref_regular = 1;
5307 }
5308
5309 if (elf_section_data (s)->relocs != internal_relocs)
5310 free (internal_relocs);
5311 }
5312 }
5313
5314 free (extversym);
5315 extversym = NULL;
5316 free (isymbuf);
5317 isymbuf = NULL;
5318
5319 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5320 {
5321 unsigned int i;
5322
5323 /* Restore the symbol table. */
5324 old_ent = (char *) old_tab + tabsize;
5325 memset (elf_sym_hashes (abfd), 0,
5326 extsymcount * sizeof (struct elf_link_hash_entry *));
5327 htab->root.table.table = old_table;
5328 htab->root.table.size = old_size;
5329 htab->root.table.count = old_count;
5330 memcpy (htab->root.table.table, old_tab, tabsize);
5331 htab->root.undefs = old_undefs;
5332 htab->root.undefs_tail = old_undefs_tail;
5333 if (htab->dynstr != NULL)
5334 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5335 free (old_strtab);
5336 old_strtab = NULL;
5337 for (i = 0; i < htab->root.table.size; i++)
5338 {
5339 struct bfd_hash_entry *p;
5340 struct elf_link_hash_entry *h;
5341 bfd_size_type size;
5342 unsigned int alignment_power;
5343 unsigned int non_ir_ref_dynamic;
5344
5345 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5346 {
5347 h = (struct elf_link_hash_entry *) p;
5348 if (h->root.type == bfd_link_hash_warning)
5349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5350
5351 /* Preserve the maximum alignment and size for common
5352 symbols even if this dynamic lib isn't on DT_NEEDED
5353 since it can still be loaded at run time by another
5354 dynamic lib. */
5355 if (h->root.type == bfd_link_hash_common)
5356 {
5357 size = h->root.u.c.size;
5358 alignment_power = h->root.u.c.p->alignment_power;
5359 }
5360 else
5361 {
5362 size = 0;
5363 alignment_power = 0;
5364 }
5365 /* Preserve non_ir_ref_dynamic so that this symbol
5366 will be exported when the dynamic lib becomes needed
5367 in the second pass. */
5368 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5369 memcpy (p, old_ent, htab->root.table.entsize);
5370 old_ent = (char *) old_ent + htab->root.table.entsize;
5371 h = (struct elf_link_hash_entry *) p;
5372 if (h->root.type == bfd_link_hash_warning)
5373 {
5374 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5375 old_ent = (char *) old_ent + htab->root.table.entsize;
5376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5377 }
5378 if (h->root.type == bfd_link_hash_common)
5379 {
5380 if (size > h->root.u.c.size)
5381 h->root.u.c.size = size;
5382 if (alignment_power > h->root.u.c.p->alignment_power)
5383 h->root.u.c.p->alignment_power = alignment_power;
5384 }
5385 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5386 }
5387 }
5388
5389 /* Make a special call to the linker "notice" function to
5390 tell it that symbols added for crefs may need to be removed. */
5391 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5392 goto error_free_vers;
5393
5394 free (old_tab);
5395 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5396 alloc_mark);
5397 free (nondeflt_vers);
5398 return TRUE;
5399 }
5400
5401 if (old_tab != NULL)
5402 {
5403 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5404 goto error_free_vers;
5405 free (old_tab);
5406 old_tab = NULL;
5407 }
5408
5409 /* Now that all the symbols from this input file are created, if
5410 not performing a relocatable link, handle .symver foo, foo@BAR
5411 such that any relocs against foo become foo@BAR. */
5412 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5413 {
5414 size_t cnt, symidx;
5415
5416 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5417 {
5418 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5419 char *shortname, *p;
5420 size_t amt;
5421
5422 p = strchr (h->root.root.string, ELF_VER_CHR);
5423 if (p == NULL
5424 || (h->root.type != bfd_link_hash_defined
5425 && h->root.type != bfd_link_hash_defweak))
5426 continue;
5427
5428 amt = p - h->root.root.string;
5429 shortname = (char *) bfd_malloc (amt + 1);
5430 if (!shortname)
5431 goto error_free_vers;
5432 memcpy (shortname, h->root.root.string, amt);
5433 shortname[amt] = '\0';
5434
5435 hi = (struct elf_link_hash_entry *)
5436 bfd_link_hash_lookup (&htab->root, shortname,
5437 FALSE, FALSE, FALSE);
5438 if (hi != NULL
5439 && hi->root.type == h->root.type
5440 && hi->root.u.def.value == h->root.u.def.value
5441 && hi->root.u.def.section == h->root.u.def.section)
5442 {
5443 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5444 hi->root.type = bfd_link_hash_indirect;
5445 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5446 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5447 sym_hash = elf_sym_hashes (abfd);
5448 if (sym_hash)
5449 for (symidx = 0; symidx < extsymcount; ++symidx)
5450 if (sym_hash[symidx] == hi)
5451 {
5452 sym_hash[symidx] = h;
5453 break;
5454 }
5455 }
5456 free (shortname);
5457 }
5458 free (nondeflt_vers);
5459 nondeflt_vers = NULL;
5460 }
5461
5462 /* Now set the alias field correctly for all the weak defined
5463 symbols we found. The only way to do this is to search all the
5464 symbols. Since we only need the information for non functions in
5465 dynamic objects, that's the only time we actually put anything on
5466 the list WEAKS. We need this information so that if a regular
5467 object refers to a symbol defined weakly in a dynamic object, the
5468 real symbol in the dynamic object is also put in the dynamic
5469 symbols; we also must arrange for both symbols to point to the
5470 same memory location. We could handle the general case of symbol
5471 aliasing, but a general symbol alias can only be generated in
5472 assembler code, handling it correctly would be very time
5473 consuming, and other ELF linkers don't handle general aliasing
5474 either. */
5475 if (weaks != NULL)
5476 {
5477 struct elf_link_hash_entry **hpp;
5478 struct elf_link_hash_entry **hppend;
5479 struct elf_link_hash_entry **sorted_sym_hash;
5480 struct elf_link_hash_entry *h;
5481 size_t sym_count, amt;
5482
5483 /* Since we have to search the whole symbol list for each weak
5484 defined symbol, search time for N weak defined symbols will be
5485 O(N^2). Binary search will cut it down to O(NlogN). */
5486 amt = extsymcount * sizeof (*sorted_sym_hash);
5487 sorted_sym_hash = bfd_malloc (amt);
5488 if (sorted_sym_hash == NULL)
5489 goto error_return;
5490 sym_hash = sorted_sym_hash;
5491 hpp = elf_sym_hashes (abfd);
5492 hppend = hpp + extsymcount;
5493 sym_count = 0;
5494 for (; hpp < hppend; hpp++)
5495 {
5496 h = *hpp;
5497 if (h != NULL
5498 && h->root.type == bfd_link_hash_defined
5499 && !bed->is_function_type (h->type))
5500 {
5501 *sym_hash = h;
5502 sym_hash++;
5503 sym_count++;
5504 }
5505 }
5506
5507 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5508 elf_sort_symbol);
5509
5510 while (weaks != NULL)
5511 {
5512 struct elf_link_hash_entry *hlook;
5513 asection *slook;
5514 bfd_vma vlook;
5515 size_t i, j, idx = 0;
5516
5517 hlook = weaks;
5518 weaks = hlook->u.alias;
5519 hlook->u.alias = NULL;
5520
5521 if (hlook->root.type != bfd_link_hash_defined
5522 && hlook->root.type != bfd_link_hash_defweak)
5523 continue;
5524
5525 slook = hlook->root.u.def.section;
5526 vlook = hlook->root.u.def.value;
5527
5528 i = 0;
5529 j = sym_count;
5530 while (i != j)
5531 {
5532 bfd_signed_vma vdiff;
5533 idx = (i + j) / 2;
5534 h = sorted_sym_hash[idx];
5535 vdiff = vlook - h->root.u.def.value;
5536 if (vdiff < 0)
5537 j = idx;
5538 else if (vdiff > 0)
5539 i = idx + 1;
5540 else
5541 {
5542 int sdiff = slook->id - h->root.u.def.section->id;
5543 if (sdiff < 0)
5544 j = idx;
5545 else if (sdiff > 0)
5546 i = idx + 1;
5547 else
5548 break;
5549 }
5550 }
5551
5552 /* We didn't find a value/section match. */
5553 if (i == j)
5554 continue;
5555
5556 /* With multiple aliases, or when the weak symbol is already
5557 strongly defined, we have multiple matching symbols and
5558 the binary search above may land on any of them. Step
5559 one past the matching symbol(s). */
5560 while (++idx != j)
5561 {
5562 h = sorted_sym_hash[idx];
5563 if (h->root.u.def.section != slook
5564 || h->root.u.def.value != vlook)
5565 break;
5566 }
5567
5568 /* Now look back over the aliases. Since we sorted by size
5569 as well as value and section, we'll choose the one with
5570 the largest size. */
5571 while (idx-- != i)
5572 {
5573 h = sorted_sym_hash[idx];
5574
5575 /* Stop if value or section doesn't match. */
5576 if (h->root.u.def.section != slook
5577 || h->root.u.def.value != vlook)
5578 break;
5579 else if (h != hlook)
5580 {
5581 struct elf_link_hash_entry *t;
5582
5583 hlook->u.alias = h;
5584 hlook->is_weakalias = 1;
5585 t = h;
5586 if (t->u.alias != NULL)
5587 while (t->u.alias != h)
5588 t = t->u.alias;
5589 t->u.alias = hlook;
5590
5591 /* If the weak definition is in the list of dynamic
5592 symbols, make sure the real definition is put
5593 there as well. */
5594 if (hlook->dynindx != -1 && h->dynindx == -1)
5595 {
5596 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5597 {
5598 err_free_sym_hash:
5599 free (sorted_sym_hash);
5600 goto error_return;
5601 }
5602 }
5603
5604 /* If the real definition is in the list of dynamic
5605 symbols, make sure the weak definition is put
5606 there as well. If we don't do this, then the
5607 dynamic loader might not merge the entries for the
5608 real definition and the weak definition. */
5609 if (h->dynindx != -1 && hlook->dynindx == -1)
5610 {
5611 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5612 goto err_free_sym_hash;
5613 }
5614 break;
5615 }
5616 }
5617 }
5618
5619 free (sorted_sym_hash);
5620 }
5621
5622 if (bed->check_directives
5623 && !(*bed->check_directives) (abfd, info))
5624 return FALSE;
5625
5626 /* If this is a non-traditional link, try to optimize the handling
5627 of the .stab/.stabstr sections. */
5628 if (! dynamic
5629 && ! info->traditional_format
5630 && is_elf_hash_table (htab)
5631 && (info->strip != strip_all && info->strip != strip_debugger))
5632 {
5633 asection *stabstr;
5634
5635 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5636 if (stabstr != NULL)
5637 {
5638 bfd_size_type string_offset = 0;
5639 asection *stab;
5640
5641 for (stab = abfd->sections; stab; stab = stab->next)
5642 if (CONST_STRNEQ (stab->name, ".stab")
5643 && (!stab->name[5] ||
5644 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5645 && (stab->flags & SEC_MERGE) == 0
5646 && !bfd_is_abs_section (stab->output_section))
5647 {
5648 struct bfd_elf_section_data *secdata;
5649
5650 secdata = elf_section_data (stab);
5651 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5652 stabstr, &secdata->sec_info,
5653 &string_offset))
5654 goto error_return;
5655 if (secdata->sec_info)
5656 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5657 }
5658 }
5659 }
5660
5661 if (dynamic && add_needed)
5662 {
5663 /* Add this bfd to the loaded list. */
5664 struct elf_link_loaded_list *n;
5665
5666 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5667 if (n == NULL)
5668 goto error_return;
5669 n->abfd = abfd;
5670 n->next = htab->dyn_loaded;
5671 htab->dyn_loaded = n;
5672 }
5673 if (dynamic && !add_needed
5674 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5675 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5676
5677 return TRUE;
5678
5679 error_free_vers:
5680 free (old_tab);
5681 free (old_strtab);
5682 free (nondeflt_vers);
5683 free (extversym);
5684 error_free_sym:
5685 free (isymbuf);
5686 error_return:
5687 return FALSE;
5688 }
5689
5690 /* Return the linker hash table entry of a symbol that might be
5691 satisfied by an archive symbol. Return -1 on error. */
5692
5693 struct elf_link_hash_entry *
5694 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5695 struct bfd_link_info *info,
5696 const char *name)
5697 {
5698 struct elf_link_hash_entry *h;
5699 char *p, *copy;
5700 size_t len, first;
5701
5702 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5703 if (h != NULL)
5704 return h;
5705
5706 /* If this is a default version (the name contains @@), look up the
5707 symbol again with only one `@' as well as without the version.
5708 The effect is that references to the symbol with and without the
5709 version will be matched by the default symbol in the archive. */
5710
5711 p = strchr (name, ELF_VER_CHR);
5712 if (p == NULL || p[1] != ELF_VER_CHR)
5713 return h;
5714
5715 /* First check with only one `@'. */
5716 len = strlen (name);
5717 copy = (char *) bfd_alloc (abfd, len);
5718 if (copy == NULL)
5719 return (struct elf_link_hash_entry *) -1;
5720
5721 first = p - name + 1;
5722 memcpy (copy, name, first);
5723 memcpy (copy + first, name + first + 1, len - first);
5724
5725 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5726 if (h == NULL)
5727 {
5728 /* We also need to check references to the symbol without the
5729 version. */
5730 copy[first - 1] = '\0';
5731 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5732 FALSE, FALSE, TRUE);
5733 }
5734
5735 bfd_release (abfd, copy);
5736 return h;
5737 }
5738
5739 /* Add symbols from an ELF archive file to the linker hash table. We
5740 don't use _bfd_generic_link_add_archive_symbols because we need to
5741 handle versioned symbols.
5742
5743 Fortunately, ELF archive handling is simpler than that done by
5744 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5745 oddities. In ELF, if we find a symbol in the archive map, and the
5746 symbol is currently undefined, we know that we must pull in that
5747 object file.
5748
5749 Unfortunately, we do have to make multiple passes over the symbol
5750 table until nothing further is resolved. */
5751
5752 static bfd_boolean
5753 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5754 {
5755 symindex c;
5756 unsigned char *included = NULL;
5757 carsym *symdefs;
5758 bfd_boolean loop;
5759 size_t amt;
5760 const struct elf_backend_data *bed;
5761 struct elf_link_hash_entry * (*archive_symbol_lookup)
5762 (bfd *, struct bfd_link_info *, const char *);
5763
5764 if (! bfd_has_map (abfd))
5765 {
5766 /* An empty archive is a special case. */
5767 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5768 return TRUE;
5769 bfd_set_error (bfd_error_no_armap);
5770 return FALSE;
5771 }
5772
5773 /* Keep track of all symbols we know to be already defined, and all
5774 files we know to be already included. This is to speed up the
5775 second and subsequent passes. */
5776 c = bfd_ardata (abfd)->symdef_count;
5777 if (c == 0)
5778 return TRUE;
5779 amt = c * sizeof (*included);
5780 included = (unsigned char *) bfd_zmalloc (amt);
5781 if (included == NULL)
5782 return FALSE;
5783
5784 symdefs = bfd_ardata (abfd)->symdefs;
5785 bed = get_elf_backend_data (abfd);
5786 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5787
5788 do
5789 {
5790 file_ptr last;
5791 symindex i;
5792 carsym *symdef;
5793 carsym *symdefend;
5794
5795 loop = FALSE;
5796 last = -1;
5797
5798 symdef = symdefs;
5799 symdefend = symdef + c;
5800 for (i = 0; symdef < symdefend; symdef++, i++)
5801 {
5802 struct elf_link_hash_entry *h;
5803 bfd *element;
5804 struct bfd_link_hash_entry *undefs_tail;
5805 symindex mark;
5806
5807 if (included[i])
5808 continue;
5809 if (symdef->file_offset == last)
5810 {
5811 included[i] = TRUE;
5812 continue;
5813 }
5814
5815 h = archive_symbol_lookup (abfd, info, symdef->name);
5816 if (h == (struct elf_link_hash_entry *) -1)
5817 goto error_return;
5818
5819 if (h == NULL)
5820 continue;
5821
5822 if (h->root.type == bfd_link_hash_undefined)
5823 {
5824 /* If the archive element has already been loaded then one
5825 of the symbols defined by that element might have been
5826 made undefined due to being in a discarded section. */
5827 if (h->indx == -3)
5828 continue;
5829 }
5830 else if (h->root.type == bfd_link_hash_common)
5831 {
5832 /* We currently have a common symbol. The archive map contains
5833 a reference to this symbol, so we may want to include it. We
5834 only want to include it however, if this archive element
5835 contains a definition of the symbol, not just another common
5836 declaration of it.
5837
5838 Unfortunately some archivers (including GNU ar) will put
5839 declarations of common symbols into their archive maps, as
5840 well as real definitions, so we cannot just go by the archive
5841 map alone. Instead we must read in the element's symbol
5842 table and check that to see what kind of symbol definition
5843 this is. */
5844 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5845 continue;
5846 }
5847 else
5848 {
5849 if (h->root.type != bfd_link_hash_undefweak)
5850 /* Symbol must be defined. Don't check it again. */
5851 included[i] = TRUE;
5852 continue;
5853 }
5854
5855 /* We need to include this archive member. */
5856 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5857 if (element == NULL)
5858 goto error_return;
5859
5860 if (! bfd_check_format (element, bfd_object))
5861 goto error_return;
5862
5863 undefs_tail = info->hash->undefs_tail;
5864
5865 if (!(*info->callbacks
5866 ->add_archive_element) (info, element, symdef->name, &element))
5867 continue;
5868 if (!bfd_link_add_symbols (element, info))
5869 goto error_return;
5870
5871 /* If there are any new undefined symbols, we need to make
5872 another pass through the archive in order to see whether
5873 they can be defined. FIXME: This isn't perfect, because
5874 common symbols wind up on undefs_tail and because an
5875 undefined symbol which is defined later on in this pass
5876 does not require another pass. This isn't a bug, but it
5877 does make the code less efficient than it could be. */
5878 if (undefs_tail != info->hash->undefs_tail)
5879 loop = TRUE;
5880
5881 /* Look backward to mark all symbols from this object file
5882 which we have already seen in this pass. */
5883 mark = i;
5884 do
5885 {
5886 included[mark] = TRUE;
5887 if (mark == 0)
5888 break;
5889 --mark;
5890 }
5891 while (symdefs[mark].file_offset == symdef->file_offset);
5892
5893 /* We mark subsequent symbols from this object file as we go
5894 on through the loop. */
5895 last = symdef->file_offset;
5896 }
5897 }
5898 while (loop);
5899
5900 free (included);
5901 return TRUE;
5902
5903 error_return:
5904 free (included);
5905 return FALSE;
5906 }
5907
5908 /* Given an ELF BFD, add symbols to the global hash table as
5909 appropriate. */
5910
5911 bfd_boolean
5912 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5913 {
5914 switch (bfd_get_format (abfd))
5915 {
5916 case bfd_object:
5917 return elf_link_add_object_symbols (abfd, info);
5918 case bfd_archive:
5919 return elf_link_add_archive_symbols (abfd, info);
5920 default:
5921 bfd_set_error (bfd_error_wrong_format);
5922 return FALSE;
5923 }
5924 }
5925 \f
5926 struct hash_codes_info
5927 {
5928 unsigned long *hashcodes;
5929 bfd_boolean error;
5930 };
5931
5932 /* This function will be called though elf_link_hash_traverse to store
5933 all hash value of the exported symbols in an array. */
5934
5935 static bfd_boolean
5936 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5937 {
5938 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5939 const char *name;
5940 unsigned long ha;
5941 char *alc = NULL;
5942
5943 /* Ignore indirect symbols. These are added by the versioning code. */
5944 if (h->dynindx == -1)
5945 return TRUE;
5946
5947 name = h->root.root.string;
5948 if (h->versioned >= versioned)
5949 {
5950 char *p = strchr (name, ELF_VER_CHR);
5951 if (p != NULL)
5952 {
5953 alc = (char *) bfd_malloc (p - name + 1);
5954 if (alc == NULL)
5955 {
5956 inf->error = TRUE;
5957 return FALSE;
5958 }
5959 memcpy (alc, name, p - name);
5960 alc[p - name] = '\0';
5961 name = alc;
5962 }
5963 }
5964
5965 /* Compute the hash value. */
5966 ha = bfd_elf_hash (name);
5967
5968 /* Store the found hash value in the array given as the argument. */
5969 *(inf->hashcodes)++ = ha;
5970
5971 /* And store it in the struct so that we can put it in the hash table
5972 later. */
5973 h->u.elf_hash_value = ha;
5974
5975 free (alc);
5976 return TRUE;
5977 }
5978
5979 struct collect_gnu_hash_codes
5980 {
5981 bfd *output_bfd;
5982 const struct elf_backend_data *bed;
5983 unsigned long int nsyms;
5984 unsigned long int maskbits;
5985 unsigned long int *hashcodes;
5986 unsigned long int *hashval;
5987 unsigned long int *indx;
5988 unsigned long int *counts;
5989 bfd_vma *bitmask;
5990 bfd_byte *contents;
5991 bfd_size_type xlat;
5992 long int min_dynindx;
5993 unsigned long int bucketcount;
5994 unsigned long int symindx;
5995 long int local_indx;
5996 long int shift1, shift2;
5997 unsigned long int mask;
5998 bfd_boolean error;
5999 };
6000
6001 /* This function will be called though elf_link_hash_traverse to store
6002 all hash value of the exported symbols in an array. */
6003
6004 static bfd_boolean
6005 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6006 {
6007 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6008 const char *name;
6009 unsigned long ha;
6010 char *alc = NULL;
6011
6012 /* Ignore indirect symbols. These are added by the versioning code. */
6013 if (h->dynindx == -1)
6014 return TRUE;
6015
6016 /* Ignore also local symbols and undefined symbols. */
6017 if (! (*s->bed->elf_hash_symbol) (h))
6018 return TRUE;
6019
6020 name = h->root.root.string;
6021 if (h->versioned >= versioned)
6022 {
6023 char *p = strchr (name, ELF_VER_CHR);
6024 if (p != NULL)
6025 {
6026 alc = (char *) bfd_malloc (p - name + 1);
6027 if (alc == NULL)
6028 {
6029 s->error = TRUE;
6030 return FALSE;
6031 }
6032 memcpy (alc, name, p - name);
6033 alc[p - name] = '\0';
6034 name = alc;
6035 }
6036 }
6037
6038 /* Compute the hash value. */
6039 ha = bfd_elf_gnu_hash (name);
6040
6041 /* Store the found hash value in the array for compute_bucket_count,
6042 and also for .dynsym reordering purposes. */
6043 s->hashcodes[s->nsyms] = ha;
6044 s->hashval[h->dynindx] = ha;
6045 ++s->nsyms;
6046 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6047 s->min_dynindx = h->dynindx;
6048
6049 free (alc);
6050 return TRUE;
6051 }
6052
6053 /* This function will be called though elf_link_hash_traverse to do
6054 final dynamic symbol renumbering in case of .gnu.hash.
6055 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6056 to the translation table. */
6057
6058 static bfd_boolean
6059 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6060 {
6061 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6062 unsigned long int bucket;
6063 unsigned long int val;
6064
6065 /* Ignore indirect symbols. */
6066 if (h->dynindx == -1)
6067 return TRUE;
6068
6069 /* Ignore also local symbols and undefined symbols. */
6070 if (! (*s->bed->elf_hash_symbol) (h))
6071 {
6072 if (h->dynindx >= s->min_dynindx)
6073 {
6074 if (s->bed->record_xhash_symbol != NULL)
6075 {
6076 (*s->bed->record_xhash_symbol) (h, 0);
6077 s->local_indx++;
6078 }
6079 else
6080 h->dynindx = s->local_indx++;
6081 }
6082 return TRUE;
6083 }
6084
6085 bucket = s->hashval[h->dynindx] % s->bucketcount;
6086 val = (s->hashval[h->dynindx] >> s->shift1)
6087 & ((s->maskbits >> s->shift1) - 1);
6088 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6089 s->bitmask[val]
6090 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6091 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6092 if (s->counts[bucket] == 1)
6093 /* Last element terminates the chain. */
6094 val |= 1;
6095 bfd_put_32 (s->output_bfd, val,
6096 s->contents + (s->indx[bucket] - s->symindx) * 4);
6097 --s->counts[bucket];
6098 if (s->bed->record_xhash_symbol != NULL)
6099 {
6100 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6101
6102 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6103 }
6104 else
6105 h->dynindx = s->indx[bucket]++;
6106 return TRUE;
6107 }
6108
6109 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6110
6111 bfd_boolean
6112 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6113 {
6114 return !(h->forced_local
6115 || h->root.type == bfd_link_hash_undefined
6116 || h->root.type == bfd_link_hash_undefweak
6117 || ((h->root.type == bfd_link_hash_defined
6118 || h->root.type == bfd_link_hash_defweak)
6119 && h->root.u.def.section->output_section == NULL));
6120 }
6121
6122 /* Array used to determine the number of hash table buckets to use
6123 based on the number of symbols there are. If there are fewer than
6124 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6125 fewer than 37 we use 17 buckets, and so forth. We never use more
6126 than 32771 buckets. */
6127
6128 static const size_t elf_buckets[] =
6129 {
6130 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6131 16411, 32771, 0
6132 };
6133
6134 /* Compute bucket count for hashing table. We do not use a static set
6135 of possible tables sizes anymore. Instead we determine for all
6136 possible reasonable sizes of the table the outcome (i.e., the
6137 number of collisions etc) and choose the best solution. The
6138 weighting functions are not too simple to allow the table to grow
6139 without bounds. Instead one of the weighting factors is the size.
6140 Therefore the result is always a good payoff between few collisions
6141 (= short chain lengths) and table size. */
6142 static size_t
6143 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6144 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6145 unsigned long int nsyms,
6146 int gnu_hash)
6147 {
6148 size_t best_size = 0;
6149 unsigned long int i;
6150
6151 /* We have a problem here. The following code to optimize the table
6152 size requires an integer type with more the 32 bits. If
6153 BFD_HOST_U_64_BIT is set we know about such a type. */
6154 #ifdef BFD_HOST_U_64_BIT
6155 if (info->optimize)
6156 {
6157 size_t minsize;
6158 size_t maxsize;
6159 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6160 bfd *dynobj = elf_hash_table (info)->dynobj;
6161 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6162 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6163 unsigned long int *counts;
6164 bfd_size_type amt;
6165 unsigned int no_improvement_count = 0;
6166
6167 /* Possible optimization parameters: if we have NSYMS symbols we say
6168 that the hashing table must at least have NSYMS/4 and at most
6169 2*NSYMS buckets. */
6170 minsize = nsyms / 4;
6171 if (minsize == 0)
6172 minsize = 1;
6173 best_size = maxsize = nsyms * 2;
6174 if (gnu_hash)
6175 {
6176 if (minsize < 2)
6177 minsize = 2;
6178 if ((best_size & 31) == 0)
6179 ++best_size;
6180 }
6181
6182 /* Create array where we count the collisions in. We must use bfd_malloc
6183 since the size could be large. */
6184 amt = maxsize;
6185 amt *= sizeof (unsigned long int);
6186 counts = (unsigned long int *) bfd_malloc (amt);
6187 if (counts == NULL)
6188 return 0;
6189
6190 /* Compute the "optimal" size for the hash table. The criteria is a
6191 minimal chain length. The minor criteria is (of course) the size
6192 of the table. */
6193 for (i = minsize; i < maxsize; ++i)
6194 {
6195 /* Walk through the array of hashcodes and count the collisions. */
6196 BFD_HOST_U_64_BIT max;
6197 unsigned long int j;
6198 unsigned long int fact;
6199
6200 if (gnu_hash && (i & 31) == 0)
6201 continue;
6202
6203 memset (counts, '\0', i * sizeof (unsigned long int));
6204
6205 /* Determine how often each hash bucket is used. */
6206 for (j = 0; j < nsyms; ++j)
6207 ++counts[hashcodes[j] % i];
6208
6209 /* For the weight function we need some information about the
6210 pagesize on the target. This is information need not be 100%
6211 accurate. Since this information is not available (so far) we
6212 define it here to a reasonable default value. If it is crucial
6213 to have a better value some day simply define this value. */
6214 # ifndef BFD_TARGET_PAGESIZE
6215 # define BFD_TARGET_PAGESIZE (4096)
6216 # endif
6217
6218 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6219 and the chains. */
6220 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6221
6222 # if 1
6223 /* Variant 1: optimize for short chains. We add the squares
6224 of all the chain lengths (which favors many small chain
6225 over a few long chains). */
6226 for (j = 0; j < i; ++j)
6227 max += counts[j] * counts[j];
6228
6229 /* This adds penalties for the overall size of the table. */
6230 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6231 max *= fact * fact;
6232 # else
6233 /* Variant 2: Optimize a lot more for small table. Here we
6234 also add squares of the size but we also add penalties for
6235 empty slots (the +1 term). */
6236 for (j = 0; j < i; ++j)
6237 max += (1 + counts[j]) * (1 + counts[j]);
6238
6239 /* The overall size of the table is considered, but not as
6240 strong as in variant 1, where it is squared. */
6241 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6242 max *= fact;
6243 # endif
6244
6245 /* Compare with current best results. */
6246 if (max < best_chlen)
6247 {
6248 best_chlen = max;
6249 best_size = i;
6250 no_improvement_count = 0;
6251 }
6252 /* PR 11843: Avoid futile long searches for the best bucket size
6253 when there are a large number of symbols. */
6254 else if (++no_improvement_count == 100)
6255 break;
6256 }
6257
6258 free (counts);
6259 }
6260 else
6261 #endif /* defined (BFD_HOST_U_64_BIT) */
6262 {
6263 /* This is the fallback solution if no 64bit type is available or if we
6264 are not supposed to spend much time on optimizations. We select the
6265 bucket count using a fixed set of numbers. */
6266 for (i = 0; elf_buckets[i] != 0; i++)
6267 {
6268 best_size = elf_buckets[i];
6269 if (nsyms < elf_buckets[i + 1])
6270 break;
6271 }
6272 if (gnu_hash && best_size < 2)
6273 best_size = 2;
6274 }
6275
6276 return best_size;
6277 }
6278
6279 /* Size any SHT_GROUP section for ld -r. */
6280
6281 bfd_boolean
6282 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6283 {
6284 bfd *ibfd;
6285 asection *s;
6286
6287 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6288 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6289 && (s = ibfd->sections) != NULL
6290 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6291 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6292 return FALSE;
6293 return TRUE;
6294 }
6295
6296 /* Set a default stack segment size. The value in INFO wins. If it
6297 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6298 undefined it is initialized. */
6299
6300 bfd_boolean
6301 bfd_elf_stack_segment_size (bfd *output_bfd,
6302 struct bfd_link_info *info,
6303 const char *legacy_symbol,
6304 bfd_vma default_size)
6305 {
6306 struct elf_link_hash_entry *h = NULL;
6307
6308 /* Look for legacy symbol. */
6309 if (legacy_symbol)
6310 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6311 FALSE, FALSE, FALSE);
6312 if (h && (h->root.type == bfd_link_hash_defined
6313 || h->root.type == bfd_link_hash_defweak)
6314 && h->def_regular
6315 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6316 {
6317 /* The symbol has no type if specified on the command line. */
6318 h->type = STT_OBJECT;
6319 if (info->stacksize)
6320 /* xgettext:c-format */
6321 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6322 output_bfd, legacy_symbol);
6323 else if (h->root.u.def.section != bfd_abs_section_ptr)
6324 /* xgettext:c-format */
6325 _bfd_error_handler (_("%pB: %s not absolute"),
6326 output_bfd, legacy_symbol);
6327 else
6328 info->stacksize = h->root.u.def.value;
6329 }
6330
6331 if (!info->stacksize)
6332 /* If the user didn't set a size, or explicitly inhibit the
6333 size, set it now. */
6334 info->stacksize = default_size;
6335
6336 /* Provide the legacy symbol, if it is referenced. */
6337 if (h && (h->root.type == bfd_link_hash_undefined
6338 || h->root.type == bfd_link_hash_undefweak))
6339 {
6340 struct bfd_link_hash_entry *bh = NULL;
6341
6342 if (!(_bfd_generic_link_add_one_symbol
6343 (info, output_bfd, legacy_symbol,
6344 BSF_GLOBAL, bfd_abs_section_ptr,
6345 info->stacksize >= 0 ? info->stacksize : 0,
6346 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6347 return FALSE;
6348
6349 h = (struct elf_link_hash_entry *) bh;
6350 h->def_regular = 1;
6351 h->type = STT_OBJECT;
6352 }
6353
6354 return TRUE;
6355 }
6356
6357 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6358
6359 struct elf_gc_sweep_symbol_info
6360 {
6361 struct bfd_link_info *info;
6362 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6363 bfd_boolean);
6364 };
6365
6366 static bfd_boolean
6367 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6368 {
6369 if (!h->mark
6370 && (((h->root.type == bfd_link_hash_defined
6371 || h->root.type == bfd_link_hash_defweak)
6372 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6373 && h->root.u.def.section->gc_mark))
6374 || h->root.type == bfd_link_hash_undefined
6375 || h->root.type == bfd_link_hash_undefweak))
6376 {
6377 struct elf_gc_sweep_symbol_info *inf;
6378
6379 inf = (struct elf_gc_sweep_symbol_info *) data;
6380 (*inf->hide_symbol) (inf->info, h, TRUE);
6381 h->def_regular = 0;
6382 h->ref_regular = 0;
6383 h->ref_regular_nonweak = 0;
6384 }
6385
6386 return TRUE;
6387 }
6388
6389 /* Set up the sizes and contents of the ELF dynamic sections. This is
6390 called by the ELF linker emulation before_allocation routine. We
6391 must set the sizes of the sections before the linker sets the
6392 addresses of the various sections. */
6393
6394 bfd_boolean
6395 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6396 const char *soname,
6397 const char *rpath,
6398 const char *filter_shlib,
6399 const char *audit,
6400 const char *depaudit,
6401 const char * const *auxiliary_filters,
6402 struct bfd_link_info *info,
6403 asection **sinterpptr)
6404 {
6405 bfd *dynobj;
6406 const struct elf_backend_data *bed;
6407
6408 *sinterpptr = NULL;
6409
6410 if (!is_elf_hash_table (info->hash))
6411 return TRUE;
6412
6413 dynobj = elf_hash_table (info)->dynobj;
6414
6415 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6416 {
6417 struct bfd_elf_version_tree *verdefs;
6418 struct elf_info_failed asvinfo;
6419 struct bfd_elf_version_tree *t;
6420 struct bfd_elf_version_expr *d;
6421 asection *s;
6422 size_t soname_indx;
6423
6424 /* If we are supposed to export all symbols into the dynamic symbol
6425 table (this is not the normal case), then do so. */
6426 if (info->export_dynamic
6427 || (bfd_link_executable (info) && info->dynamic))
6428 {
6429 struct elf_info_failed eif;
6430
6431 eif.info = info;
6432 eif.failed = FALSE;
6433 elf_link_hash_traverse (elf_hash_table (info),
6434 _bfd_elf_export_symbol,
6435 &eif);
6436 if (eif.failed)
6437 return FALSE;
6438 }
6439
6440 if (soname != NULL)
6441 {
6442 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6443 soname, TRUE);
6444 if (soname_indx == (size_t) -1
6445 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6446 return FALSE;
6447 }
6448 else
6449 soname_indx = (size_t) -1;
6450
6451 /* Make all global versions with definition. */
6452 for (t = info->version_info; t != NULL; t = t->next)
6453 for (d = t->globals.list; d != NULL; d = d->next)
6454 if (!d->symver && d->literal)
6455 {
6456 const char *verstr, *name;
6457 size_t namelen, verlen, newlen;
6458 char *newname, *p, leading_char;
6459 struct elf_link_hash_entry *newh;
6460
6461 leading_char = bfd_get_symbol_leading_char (output_bfd);
6462 name = d->pattern;
6463 namelen = strlen (name) + (leading_char != '\0');
6464 verstr = t->name;
6465 verlen = strlen (verstr);
6466 newlen = namelen + verlen + 3;
6467
6468 newname = (char *) bfd_malloc (newlen);
6469 if (newname == NULL)
6470 return FALSE;
6471 newname[0] = leading_char;
6472 memcpy (newname + (leading_char != '\0'), name, namelen);
6473
6474 /* Check the hidden versioned definition. */
6475 p = newname + namelen;
6476 *p++ = ELF_VER_CHR;
6477 memcpy (p, verstr, verlen + 1);
6478 newh = elf_link_hash_lookup (elf_hash_table (info),
6479 newname, FALSE, FALSE,
6480 FALSE);
6481 if (newh == NULL
6482 || (newh->root.type != bfd_link_hash_defined
6483 && newh->root.type != bfd_link_hash_defweak))
6484 {
6485 /* Check the default versioned definition. */
6486 *p++ = ELF_VER_CHR;
6487 memcpy (p, verstr, verlen + 1);
6488 newh = elf_link_hash_lookup (elf_hash_table (info),
6489 newname, FALSE, FALSE,
6490 FALSE);
6491 }
6492 free (newname);
6493
6494 /* Mark this version if there is a definition and it is
6495 not defined in a shared object. */
6496 if (newh != NULL
6497 && !newh->def_dynamic
6498 && (newh->root.type == bfd_link_hash_defined
6499 || newh->root.type == bfd_link_hash_defweak))
6500 d->symver = 1;
6501 }
6502
6503 /* Attach all the symbols to their version information. */
6504 asvinfo.info = info;
6505 asvinfo.failed = FALSE;
6506
6507 elf_link_hash_traverse (elf_hash_table (info),
6508 _bfd_elf_link_assign_sym_version,
6509 &asvinfo);
6510 if (asvinfo.failed)
6511 return FALSE;
6512
6513 if (!info->allow_undefined_version)
6514 {
6515 /* Check if all global versions have a definition. */
6516 bfd_boolean all_defined = TRUE;
6517 for (t = info->version_info; t != NULL; t = t->next)
6518 for (d = t->globals.list; d != NULL; d = d->next)
6519 if (d->literal && !d->symver && !d->script)
6520 {
6521 _bfd_error_handler
6522 (_("%s: undefined version: %s"),
6523 d->pattern, t->name);
6524 all_defined = FALSE;
6525 }
6526
6527 if (!all_defined)
6528 {
6529 bfd_set_error (bfd_error_bad_value);
6530 return FALSE;
6531 }
6532 }
6533
6534 /* Set up the version definition section. */
6535 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6536 BFD_ASSERT (s != NULL);
6537
6538 /* We may have created additional version definitions if we are
6539 just linking a regular application. */
6540 verdefs = info->version_info;
6541
6542 /* Skip anonymous version tag. */
6543 if (verdefs != NULL && verdefs->vernum == 0)
6544 verdefs = verdefs->next;
6545
6546 if (verdefs == NULL && !info->create_default_symver)
6547 s->flags |= SEC_EXCLUDE;
6548 else
6549 {
6550 unsigned int cdefs;
6551 bfd_size_type size;
6552 bfd_byte *p;
6553 Elf_Internal_Verdef def;
6554 Elf_Internal_Verdaux defaux;
6555 struct bfd_link_hash_entry *bh;
6556 struct elf_link_hash_entry *h;
6557 const char *name;
6558
6559 cdefs = 0;
6560 size = 0;
6561
6562 /* Make space for the base version. */
6563 size += sizeof (Elf_External_Verdef);
6564 size += sizeof (Elf_External_Verdaux);
6565 ++cdefs;
6566
6567 /* Make space for the default version. */
6568 if (info->create_default_symver)
6569 {
6570 size += sizeof (Elf_External_Verdef);
6571 ++cdefs;
6572 }
6573
6574 for (t = verdefs; t != NULL; t = t->next)
6575 {
6576 struct bfd_elf_version_deps *n;
6577
6578 /* Don't emit base version twice. */
6579 if (t->vernum == 0)
6580 continue;
6581
6582 size += sizeof (Elf_External_Verdef);
6583 size += sizeof (Elf_External_Verdaux);
6584 ++cdefs;
6585
6586 for (n = t->deps; n != NULL; n = n->next)
6587 size += sizeof (Elf_External_Verdaux);
6588 }
6589
6590 s->size = size;
6591 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6592 if (s->contents == NULL && s->size != 0)
6593 return FALSE;
6594
6595 /* Fill in the version definition section. */
6596
6597 p = s->contents;
6598
6599 def.vd_version = VER_DEF_CURRENT;
6600 def.vd_flags = VER_FLG_BASE;
6601 def.vd_ndx = 1;
6602 def.vd_cnt = 1;
6603 if (info->create_default_symver)
6604 {
6605 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6606 def.vd_next = sizeof (Elf_External_Verdef);
6607 }
6608 else
6609 {
6610 def.vd_aux = sizeof (Elf_External_Verdef);
6611 def.vd_next = (sizeof (Elf_External_Verdef)
6612 + sizeof (Elf_External_Verdaux));
6613 }
6614
6615 if (soname_indx != (size_t) -1)
6616 {
6617 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6618 soname_indx);
6619 def.vd_hash = bfd_elf_hash (soname);
6620 defaux.vda_name = soname_indx;
6621 name = soname;
6622 }
6623 else
6624 {
6625 size_t indx;
6626
6627 name = lbasename (bfd_get_filename (output_bfd));
6628 def.vd_hash = bfd_elf_hash (name);
6629 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6630 name, FALSE);
6631 if (indx == (size_t) -1)
6632 return FALSE;
6633 defaux.vda_name = indx;
6634 }
6635 defaux.vda_next = 0;
6636
6637 _bfd_elf_swap_verdef_out (output_bfd, &def,
6638 (Elf_External_Verdef *) p);
6639 p += sizeof (Elf_External_Verdef);
6640 if (info->create_default_symver)
6641 {
6642 /* Add a symbol representing this version. */
6643 bh = NULL;
6644 if (! (_bfd_generic_link_add_one_symbol
6645 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6646 0, NULL, FALSE,
6647 get_elf_backend_data (dynobj)->collect, &bh)))
6648 return FALSE;
6649 h = (struct elf_link_hash_entry *) bh;
6650 h->non_elf = 0;
6651 h->def_regular = 1;
6652 h->type = STT_OBJECT;
6653 h->verinfo.vertree = NULL;
6654
6655 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6656 return FALSE;
6657
6658 /* Create a duplicate of the base version with the same
6659 aux block, but different flags. */
6660 def.vd_flags = 0;
6661 def.vd_ndx = 2;
6662 def.vd_aux = sizeof (Elf_External_Verdef);
6663 if (verdefs)
6664 def.vd_next = (sizeof (Elf_External_Verdef)
6665 + sizeof (Elf_External_Verdaux));
6666 else
6667 def.vd_next = 0;
6668 _bfd_elf_swap_verdef_out (output_bfd, &def,
6669 (Elf_External_Verdef *) p);
6670 p += sizeof (Elf_External_Verdef);
6671 }
6672 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6673 (Elf_External_Verdaux *) p);
6674 p += sizeof (Elf_External_Verdaux);
6675
6676 for (t = verdefs; t != NULL; t = t->next)
6677 {
6678 unsigned int cdeps;
6679 struct bfd_elf_version_deps *n;
6680
6681 /* Don't emit the base version twice. */
6682 if (t->vernum == 0)
6683 continue;
6684
6685 cdeps = 0;
6686 for (n = t->deps; n != NULL; n = n->next)
6687 ++cdeps;
6688
6689 /* Add a symbol representing this version. */
6690 bh = NULL;
6691 if (! (_bfd_generic_link_add_one_symbol
6692 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6693 0, NULL, FALSE,
6694 get_elf_backend_data (dynobj)->collect, &bh)))
6695 return FALSE;
6696 h = (struct elf_link_hash_entry *) bh;
6697 h->non_elf = 0;
6698 h->def_regular = 1;
6699 h->type = STT_OBJECT;
6700 h->verinfo.vertree = t;
6701
6702 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6703 return FALSE;
6704
6705 def.vd_version = VER_DEF_CURRENT;
6706 def.vd_flags = 0;
6707 if (t->globals.list == NULL
6708 && t->locals.list == NULL
6709 && ! t->used)
6710 def.vd_flags |= VER_FLG_WEAK;
6711 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6712 def.vd_cnt = cdeps + 1;
6713 def.vd_hash = bfd_elf_hash (t->name);
6714 def.vd_aux = sizeof (Elf_External_Verdef);
6715 def.vd_next = 0;
6716
6717 /* If a basever node is next, it *must* be the last node in
6718 the chain, otherwise Verdef construction breaks. */
6719 if (t->next != NULL && t->next->vernum == 0)
6720 BFD_ASSERT (t->next->next == NULL);
6721
6722 if (t->next != NULL && t->next->vernum != 0)
6723 def.vd_next = (sizeof (Elf_External_Verdef)
6724 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6725
6726 _bfd_elf_swap_verdef_out (output_bfd, &def,
6727 (Elf_External_Verdef *) p);
6728 p += sizeof (Elf_External_Verdef);
6729
6730 defaux.vda_name = h->dynstr_index;
6731 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6732 h->dynstr_index);
6733 defaux.vda_next = 0;
6734 if (t->deps != NULL)
6735 defaux.vda_next = sizeof (Elf_External_Verdaux);
6736 t->name_indx = defaux.vda_name;
6737
6738 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6739 (Elf_External_Verdaux *) p);
6740 p += sizeof (Elf_External_Verdaux);
6741
6742 for (n = t->deps; n != NULL; n = n->next)
6743 {
6744 if (n->version_needed == NULL)
6745 {
6746 /* This can happen if there was an error in the
6747 version script. */
6748 defaux.vda_name = 0;
6749 }
6750 else
6751 {
6752 defaux.vda_name = n->version_needed->name_indx;
6753 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6754 defaux.vda_name);
6755 }
6756 if (n->next == NULL)
6757 defaux.vda_next = 0;
6758 else
6759 defaux.vda_next = sizeof (Elf_External_Verdaux);
6760
6761 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6762 (Elf_External_Verdaux *) p);
6763 p += sizeof (Elf_External_Verdaux);
6764 }
6765 }
6766
6767 elf_tdata (output_bfd)->cverdefs = cdefs;
6768 }
6769 }
6770
6771 bed = get_elf_backend_data (output_bfd);
6772
6773 if (info->gc_sections && bed->can_gc_sections)
6774 {
6775 struct elf_gc_sweep_symbol_info sweep_info;
6776
6777 /* Remove the symbols that were in the swept sections from the
6778 dynamic symbol table. */
6779 sweep_info.info = info;
6780 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6781 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6782 &sweep_info);
6783 }
6784
6785 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6786 {
6787 asection *s;
6788 struct elf_find_verdep_info sinfo;
6789
6790 /* Work out the size of the version reference section. */
6791
6792 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6793 BFD_ASSERT (s != NULL);
6794
6795 sinfo.info = info;
6796 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6797 if (sinfo.vers == 0)
6798 sinfo.vers = 1;
6799 sinfo.failed = FALSE;
6800
6801 elf_link_hash_traverse (elf_hash_table (info),
6802 _bfd_elf_link_find_version_dependencies,
6803 &sinfo);
6804 if (sinfo.failed)
6805 return FALSE;
6806
6807 if (elf_tdata (output_bfd)->verref == NULL)
6808 s->flags |= SEC_EXCLUDE;
6809 else
6810 {
6811 Elf_Internal_Verneed *vn;
6812 unsigned int size;
6813 unsigned int crefs;
6814 bfd_byte *p;
6815
6816 /* Build the version dependency section. */
6817 size = 0;
6818 crefs = 0;
6819 for (vn = elf_tdata (output_bfd)->verref;
6820 vn != NULL;
6821 vn = vn->vn_nextref)
6822 {
6823 Elf_Internal_Vernaux *a;
6824
6825 size += sizeof (Elf_External_Verneed);
6826 ++crefs;
6827 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6828 size += sizeof (Elf_External_Vernaux);
6829 }
6830
6831 s->size = size;
6832 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6833 if (s->contents == NULL)
6834 return FALSE;
6835
6836 p = s->contents;
6837 for (vn = elf_tdata (output_bfd)->verref;
6838 vn != NULL;
6839 vn = vn->vn_nextref)
6840 {
6841 unsigned int caux;
6842 Elf_Internal_Vernaux *a;
6843 size_t indx;
6844
6845 caux = 0;
6846 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6847 ++caux;
6848
6849 vn->vn_version = VER_NEED_CURRENT;
6850 vn->vn_cnt = caux;
6851 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6852 elf_dt_name (vn->vn_bfd) != NULL
6853 ? elf_dt_name (vn->vn_bfd)
6854 : lbasename (bfd_get_filename
6855 (vn->vn_bfd)),
6856 FALSE);
6857 if (indx == (size_t) -1)
6858 return FALSE;
6859 vn->vn_file = indx;
6860 vn->vn_aux = sizeof (Elf_External_Verneed);
6861 if (vn->vn_nextref == NULL)
6862 vn->vn_next = 0;
6863 else
6864 vn->vn_next = (sizeof (Elf_External_Verneed)
6865 + caux * sizeof (Elf_External_Vernaux));
6866
6867 _bfd_elf_swap_verneed_out (output_bfd, vn,
6868 (Elf_External_Verneed *) p);
6869 p += sizeof (Elf_External_Verneed);
6870
6871 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6872 {
6873 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6875 a->vna_nodename, FALSE);
6876 if (indx == (size_t) -1)
6877 return FALSE;
6878 a->vna_name = indx;
6879 if (a->vna_nextptr == NULL)
6880 a->vna_next = 0;
6881 else
6882 a->vna_next = sizeof (Elf_External_Vernaux);
6883
6884 _bfd_elf_swap_vernaux_out (output_bfd, a,
6885 (Elf_External_Vernaux *) p);
6886 p += sizeof (Elf_External_Vernaux);
6887 }
6888 }
6889
6890 elf_tdata (output_bfd)->cverrefs = crefs;
6891 }
6892 }
6893
6894 /* Any syms created from now on start with -1 in
6895 got.refcount/offset and plt.refcount/offset. */
6896 elf_hash_table (info)->init_got_refcount
6897 = elf_hash_table (info)->init_got_offset;
6898 elf_hash_table (info)->init_plt_refcount
6899 = elf_hash_table (info)->init_plt_offset;
6900
6901 if (bfd_link_relocatable (info)
6902 && !_bfd_elf_size_group_sections (info))
6903 return FALSE;
6904
6905 /* The backend may have to create some sections regardless of whether
6906 we're dynamic or not. */
6907 if (bed->elf_backend_always_size_sections
6908 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6909 return FALSE;
6910
6911 /* Determine any GNU_STACK segment requirements, after the backend
6912 has had a chance to set a default segment size. */
6913 if (info->execstack)
6914 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6915 else if (info->noexecstack)
6916 elf_stack_flags (output_bfd) = PF_R | PF_W;
6917 else
6918 {
6919 bfd *inputobj;
6920 asection *notesec = NULL;
6921 int exec = 0;
6922
6923 for (inputobj = info->input_bfds;
6924 inputobj;
6925 inputobj = inputobj->link.next)
6926 {
6927 asection *s;
6928
6929 if (inputobj->flags
6930 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6931 continue;
6932 s = inputobj->sections;
6933 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6934 continue;
6935
6936 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6937 if (s)
6938 {
6939 if (s->flags & SEC_CODE)
6940 exec = PF_X;
6941 notesec = s;
6942 }
6943 else if (bed->default_execstack)
6944 exec = PF_X;
6945 }
6946 if (notesec || info->stacksize > 0)
6947 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6948 if (notesec && exec && bfd_link_relocatable (info)
6949 && notesec->output_section != bfd_abs_section_ptr)
6950 notesec->output_section->flags |= SEC_CODE;
6951 }
6952
6953 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6954 {
6955 struct elf_info_failed eif;
6956 struct elf_link_hash_entry *h;
6957 asection *dynstr;
6958 asection *s;
6959
6960 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6961 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6962
6963 if (info->symbolic)
6964 {
6965 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6966 return FALSE;
6967 info->flags |= DF_SYMBOLIC;
6968 }
6969
6970 if (rpath != NULL)
6971 {
6972 size_t indx;
6973 bfd_vma tag;
6974
6975 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6976 TRUE);
6977 if (indx == (size_t) -1)
6978 return FALSE;
6979
6980 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6981 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6982 return FALSE;
6983 }
6984
6985 if (filter_shlib != NULL)
6986 {
6987 size_t indx;
6988
6989 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6990 filter_shlib, TRUE);
6991 if (indx == (size_t) -1
6992 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6993 return FALSE;
6994 }
6995
6996 if (auxiliary_filters != NULL)
6997 {
6998 const char * const *p;
6999
7000 for (p = auxiliary_filters; *p != NULL; p++)
7001 {
7002 size_t indx;
7003
7004 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7005 *p, TRUE);
7006 if (indx == (size_t) -1
7007 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7008 return FALSE;
7009 }
7010 }
7011
7012 if (audit != NULL)
7013 {
7014 size_t indx;
7015
7016 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7017 TRUE);
7018 if (indx == (size_t) -1
7019 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7020 return FALSE;
7021 }
7022
7023 if (depaudit != NULL)
7024 {
7025 size_t indx;
7026
7027 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7028 TRUE);
7029 if (indx == (size_t) -1
7030 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7031 return FALSE;
7032 }
7033
7034 eif.info = info;
7035 eif.failed = FALSE;
7036
7037 /* Find all symbols which were defined in a dynamic object and make
7038 the backend pick a reasonable value for them. */
7039 elf_link_hash_traverse (elf_hash_table (info),
7040 _bfd_elf_adjust_dynamic_symbol,
7041 &eif);
7042 if (eif.failed)
7043 return FALSE;
7044
7045 /* Add some entries to the .dynamic section. We fill in some of the
7046 values later, in bfd_elf_final_link, but we must add the entries
7047 now so that we know the final size of the .dynamic section. */
7048
7049 /* If there are initialization and/or finalization functions to
7050 call then add the corresponding DT_INIT/DT_FINI entries. */
7051 h = (info->init_function
7052 ? elf_link_hash_lookup (elf_hash_table (info),
7053 info->init_function, FALSE,
7054 FALSE, FALSE)
7055 : NULL);
7056 if (h != NULL
7057 && (h->ref_regular
7058 || h->def_regular))
7059 {
7060 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7061 return FALSE;
7062 }
7063 h = (info->fini_function
7064 ? elf_link_hash_lookup (elf_hash_table (info),
7065 info->fini_function, FALSE,
7066 FALSE, FALSE)
7067 : NULL);
7068 if (h != NULL
7069 && (h->ref_regular
7070 || h->def_regular))
7071 {
7072 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7073 return FALSE;
7074 }
7075
7076 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7077 if (s != NULL && s->linker_has_input)
7078 {
7079 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7080 if (! bfd_link_executable (info))
7081 {
7082 bfd *sub;
7083 asection *o;
7084
7085 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7086 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7087 && (o = sub->sections) != NULL
7088 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7089 for (o = sub->sections; o != NULL; o = o->next)
7090 if (elf_section_data (o)->this_hdr.sh_type
7091 == SHT_PREINIT_ARRAY)
7092 {
7093 _bfd_error_handler
7094 (_("%pB: .preinit_array section is not allowed in DSO"),
7095 sub);
7096 break;
7097 }
7098
7099 bfd_set_error (bfd_error_nonrepresentable_section);
7100 return FALSE;
7101 }
7102
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7104 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7105 return FALSE;
7106 }
7107 s = bfd_get_section_by_name (output_bfd, ".init_array");
7108 if (s != NULL && s->linker_has_input)
7109 {
7110 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7111 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7112 return FALSE;
7113 }
7114 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7115 if (s != NULL && s->linker_has_input)
7116 {
7117 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7118 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7119 return FALSE;
7120 }
7121
7122 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7123 /* If .dynstr is excluded from the link, we don't want any of
7124 these tags. Strictly, we should be checking each section
7125 individually; This quick check covers for the case where
7126 someone does a /DISCARD/ : { *(*) }. */
7127 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7128 {
7129 bfd_size_type strsize;
7130
7131 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7132 if ((info->emit_hash
7133 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7134 || (info->emit_gnu_hash
7135 && (bed->record_xhash_symbol == NULL
7136 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7137 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7138 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7139 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7140 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7141 bed->s->sizeof_sym))
7142 return FALSE;
7143 }
7144 }
7145
7146 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7147 return FALSE;
7148
7149 /* The backend must work out the sizes of all the other dynamic
7150 sections. */
7151 if (dynobj != NULL
7152 && bed->elf_backend_size_dynamic_sections != NULL
7153 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7154 return FALSE;
7155
7156 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7157 {
7158 if (elf_tdata (output_bfd)->cverdefs)
7159 {
7160 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7161
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7163 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7164 return FALSE;
7165 }
7166
7167 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7168 {
7169 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7170 return FALSE;
7171 }
7172 else if (info->flags & DF_BIND_NOW)
7173 {
7174 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7175 return FALSE;
7176 }
7177
7178 if (info->flags_1)
7179 {
7180 if (bfd_link_executable (info))
7181 info->flags_1 &= ~ (DF_1_INITFIRST
7182 | DF_1_NODELETE
7183 | DF_1_NOOPEN);
7184 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7185 return FALSE;
7186 }
7187
7188 if (elf_tdata (output_bfd)->cverrefs)
7189 {
7190 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7191
7192 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7193 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7194 return FALSE;
7195 }
7196
7197 if ((elf_tdata (output_bfd)->cverrefs == 0
7198 && elf_tdata (output_bfd)->cverdefs == 0)
7199 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7200 {
7201 asection *s;
7202
7203 s = bfd_get_linker_section (dynobj, ".gnu.version");
7204 s->flags |= SEC_EXCLUDE;
7205 }
7206 }
7207 return TRUE;
7208 }
7209
7210 /* Find the first non-excluded output section. We'll use its
7211 section symbol for some emitted relocs. */
7212 void
7213 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7214 {
7215 asection *s;
7216 asection *found = NULL;
7217
7218 for (s = output_bfd->sections; s != NULL; s = s->next)
7219 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7220 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7221 {
7222 found = s;
7223 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7224 break;
7225 }
7226 elf_hash_table (info)->text_index_section = found;
7227 }
7228
7229 /* Find two non-excluded output sections, one for code, one for data.
7230 We'll use their section symbols for some emitted relocs. */
7231 void
7232 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7233 {
7234 asection *s;
7235 asection *found = NULL;
7236
7237 /* Data first, since setting text_index_section changes
7238 _bfd_elf_omit_section_dynsym_default. */
7239 for (s = output_bfd->sections; s != NULL; s = s->next)
7240 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7241 && !(s->flags & SEC_READONLY)
7242 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7243 {
7244 found = s;
7245 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7246 break;
7247 }
7248 elf_hash_table (info)->data_index_section = found;
7249
7250 for (s = output_bfd->sections; s != NULL; s = s->next)
7251 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7252 && (s->flags & SEC_READONLY)
7253 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7254 {
7255 found = s;
7256 break;
7257 }
7258 elf_hash_table (info)->text_index_section = found;
7259 }
7260
7261 #define GNU_HASH_SECTION_NAME(bed) \
7262 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7263
7264 bfd_boolean
7265 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7266 {
7267 const struct elf_backend_data *bed;
7268 unsigned long section_sym_count;
7269 bfd_size_type dynsymcount = 0;
7270
7271 if (!is_elf_hash_table (info->hash))
7272 return TRUE;
7273
7274 bed = get_elf_backend_data (output_bfd);
7275 (*bed->elf_backend_init_index_section) (output_bfd, info);
7276
7277 /* Assign dynsym indices. In a shared library we generate a section
7278 symbol for each output section, which come first. Next come all
7279 of the back-end allocated local dynamic syms, followed by the rest
7280 of the global symbols.
7281
7282 This is usually not needed for static binaries, however backends
7283 can request to always do it, e.g. the MIPS backend uses dynamic
7284 symbol counts to lay out GOT, which will be produced in the
7285 presence of GOT relocations even in static binaries (holding fixed
7286 data in that case, to satisfy those relocations). */
7287
7288 if (elf_hash_table (info)->dynamic_sections_created
7289 || bed->always_renumber_dynsyms)
7290 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7291 &section_sym_count);
7292
7293 if (elf_hash_table (info)->dynamic_sections_created)
7294 {
7295 bfd *dynobj;
7296 asection *s;
7297 unsigned int dtagcount;
7298
7299 dynobj = elf_hash_table (info)->dynobj;
7300
7301 /* Work out the size of the symbol version section. */
7302 s = bfd_get_linker_section (dynobj, ".gnu.version");
7303 BFD_ASSERT (s != NULL);
7304 if ((s->flags & SEC_EXCLUDE) == 0)
7305 {
7306 s->size = dynsymcount * sizeof (Elf_External_Versym);
7307 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7308 if (s->contents == NULL)
7309 return FALSE;
7310
7311 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7312 return FALSE;
7313 }
7314
7315 /* Set the size of the .dynsym and .hash sections. We counted
7316 the number of dynamic symbols in elf_link_add_object_symbols.
7317 We will build the contents of .dynsym and .hash when we build
7318 the final symbol table, because until then we do not know the
7319 correct value to give the symbols. We built the .dynstr
7320 section as we went along in elf_link_add_object_symbols. */
7321 s = elf_hash_table (info)->dynsym;
7322 BFD_ASSERT (s != NULL);
7323 s->size = dynsymcount * bed->s->sizeof_sym;
7324
7325 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7326 if (s->contents == NULL)
7327 return FALSE;
7328
7329 /* The first entry in .dynsym is a dummy symbol. Clear all the
7330 section syms, in case we don't output them all. */
7331 ++section_sym_count;
7332 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7333
7334 elf_hash_table (info)->bucketcount = 0;
7335
7336 /* Compute the size of the hashing table. As a side effect this
7337 computes the hash values for all the names we export. */
7338 if (info->emit_hash)
7339 {
7340 unsigned long int *hashcodes;
7341 struct hash_codes_info hashinf;
7342 bfd_size_type amt;
7343 unsigned long int nsyms;
7344 size_t bucketcount;
7345 size_t hash_entry_size;
7346
7347 /* Compute the hash values for all exported symbols. At the same
7348 time store the values in an array so that we could use them for
7349 optimizations. */
7350 amt = dynsymcount * sizeof (unsigned long int);
7351 hashcodes = (unsigned long int *) bfd_malloc (amt);
7352 if (hashcodes == NULL)
7353 return FALSE;
7354 hashinf.hashcodes = hashcodes;
7355 hashinf.error = FALSE;
7356
7357 /* Put all hash values in HASHCODES. */
7358 elf_link_hash_traverse (elf_hash_table (info),
7359 elf_collect_hash_codes, &hashinf);
7360 if (hashinf.error)
7361 {
7362 free (hashcodes);
7363 return FALSE;
7364 }
7365
7366 nsyms = hashinf.hashcodes - hashcodes;
7367 bucketcount
7368 = compute_bucket_count (info, hashcodes, nsyms, 0);
7369 free (hashcodes);
7370
7371 if (bucketcount == 0 && nsyms > 0)
7372 return FALSE;
7373
7374 elf_hash_table (info)->bucketcount = bucketcount;
7375
7376 s = bfd_get_linker_section (dynobj, ".hash");
7377 BFD_ASSERT (s != NULL);
7378 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7379 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7380 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7381 if (s->contents == NULL)
7382 return FALSE;
7383
7384 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7385 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7386 s->contents + hash_entry_size);
7387 }
7388
7389 if (info->emit_gnu_hash)
7390 {
7391 size_t i, cnt;
7392 unsigned char *contents;
7393 struct collect_gnu_hash_codes cinfo;
7394 bfd_size_type amt;
7395 size_t bucketcount;
7396
7397 memset (&cinfo, 0, sizeof (cinfo));
7398
7399 /* Compute the hash values for all exported symbols. At the same
7400 time store the values in an array so that we could use them for
7401 optimizations. */
7402 amt = dynsymcount * 2 * sizeof (unsigned long int);
7403 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7404 if (cinfo.hashcodes == NULL)
7405 return FALSE;
7406
7407 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7408 cinfo.min_dynindx = -1;
7409 cinfo.output_bfd = output_bfd;
7410 cinfo.bed = bed;
7411
7412 /* Put all hash values in HASHCODES. */
7413 elf_link_hash_traverse (elf_hash_table (info),
7414 elf_collect_gnu_hash_codes, &cinfo);
7415 if (cinfo.error)
7416 {
7417 free (cinfo.hashcodes);
7418 return FALSE;
7419 }
7420
7421 bucketcount
7422 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7423
7424 if (bucketcount == 0)
7425 {
7426 free (cinfo.hashcodes);
7427 return FALSE;
7428 }
7429
7430 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7431 BFD_ASSERT (s != NULL);
7432
7433 if (cinfo.nsyms == 0)
7434 {
7435 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7436 BFD_ASSERT (cinfo.min_dynindx == -1);
7437 free (cinfo.hashcodes);
7438 s->size = 5 * 4 + bed->s->arch_size / 8;
7439 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7440 if (contents == NULL)
7441 return FALSE;
7442 s->contents = contents;
7443 /* 1 empty bucket. */
7444 bfd_put_32 (output_bfd, 1, contents);
7445 /* SYMIDX above the special symbol 0. */
7446 bfd_put_32 (output_bfd, 1, contents + 4);
7447 /* Just one word for bitmask. */
7448 bfd_put_32 (output_bfd, 1, contents + 8);
7449 /* Only hash fn bloom filter. */
7450 bfd_put_32 (output_bfd, 0, contents + 12);
7451 /* No hashes are valid - empty bitmask. */
7452 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7453 /* No hashes in the only bucket. */
7454 bfd_put_32 (output_bfd, 0,
7455 contents + 16 + bed->s->arch_size / 8);
7456 }
7457 else
7458 {
7459 unsigned long int maskwords, maskbitslog2, x;
7460 BFD_ASSERT (cinfo.min_dynindx != -1);
7461
7462 x = cinfo.nsyms;
7463 maskbitslog2 = 1;
7464 while ((x >>= 1) != 0)
7465 ++maskbitslog2;
7466 if (maskbitslog2 < 3)
7467 maskbitslog2 = 5;
7468 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7469 maskbitslog2 = maskbitslog2 + 3;
7470 else
7471 maskbitslog2 = maskbitslog2 + 2;
7472 if (bed->s->arch_size == 64)
7473 {
7474 if (maskbitslog2 == 5)
7475 maskbitslog2 = 6;
7476 cinfo.shift1 = 6;
7477 }
7478 else
7479 cinfo.shift1 = 5;
7480 cinfo.mask = (1 << cinfo.shift1) - 1;
7481 cinfo.shift2 = maskbitslog2;
7482 cinfo.maskbits = 1 << maskbitslog2;
7483 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7484 amt = bucketcount * sizeof (unsigned long int) * 2;
7485 amt += maskwords * sizeof (bfd_vma);
7486 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7487 if (cinfo.bitmask == NULL)
7488 {
7489 free (cinfo.hashcodes);
7490 return FALSE;
7491 }
7492
7493 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7494 cinfo.indx = cinfo.counts + bucketcount;
7495 cinfo.symindx = dynsymcount - cinfo.nsyms;
7496 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7497
7498 /* Determine how often each hash bucket is used. */
7499 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7500 for (i = 0; i < cinfo.nsyms; ++i)
7501 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7502
7503 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7504 if (cinfo.counts[i] != 0)
7505 {
7506 cinfo.indx[i] = cnt;
7507 cnt += cinfo.counts[i];
7508 }
7509 BFD_ASSERT (cnt == dynsymcount);
7510 cinfo.bucketcount = bucketcount;
7511 cinfo.local_indx = cinfo.min_dynindx;
7512
7513 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7514 s->size += cinfo.maskbits / 8;
7515 if (bed->record_xhash_symbol != NULL)
7516 s->size += cinfo.nsyms * 4;
7517 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7518 if (contents == NULL)
7519 {
7520 free (cinfo.bitmask);
7521 free (cinfo.hashcodes);
7522 return FALSE;
7523 }
7524
7525 s->contents = contents;
7526 bfd_put_32 (output_bfd, bucketcount, contents);
7527 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7528 bfd_put_32 (output_bfd, maskwords, contents + 8);
7529 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7530 contents += 16 + cinfo.maskbits / 8;
7531
7532 for (i = 0; i < bucketcount; ++i)
7533 {
7534 if (cinfo.counts[i] == 0)
7535 bfd_put_32 (output_bfd, 0, contents);
7536 else
7537 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7538 contents += 4;
7539 }
7540
7541 cinfo.contents = contents;
7542
7543 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7544 /* Renumber dynamic symbols, if populating .gnu.hash section.
7545 If using .MIPS.xhash, populate the translation table. */
7546 elf_link_hash_traverse (elf_hash_table (info),
7547 elf_gnu_hash_process_symidx, &cinfo);
7548
7549 contents = s->contents + 16;
7550 for (i = 0; i < maskwords; ++i)
7551 {
7552 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7553 contents);
7554 contents += bed->s->arch_size / 8;
7555 }
7556
7557 free (cinfo.bitmask);
7558 free (cinfo.hashcodes);
7559 }
7560 }
7561
7562 s = bfd_get_linker_section (dynobj, ".dynstr");
7563 BFD_ASSERT (s != NULL);
7564
7565 elf_finalize_dynstr (output_bfd, info);
7566
7567 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7568
7569 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7570 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7571 return FALSE;
7572 }
7573
7574 return TRUE;
7575 }
7576 \f
7577 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7578
7579 static void
7580 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7581 asection *sec)
7582 {
7583 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7584 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7585 }
7586
7587 /* Finish SHF_MERGE section merging. */
7588
7589 bfd_boolean
7590 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7591 {
7592 bfd *ibfd;
7593 asection *sec;
7594
7595 if (!is_elf_hash_table (info->hash))
7596 return FALSE;
7597
7598 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7599 if ((ibfd->flags & DYNAMIC) == 0
7600 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7601 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7602 == get_elf_backend_data (obfd)->s->elfclass))
7603 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7604 if ((sec->flags & SEC_MERGE) != 0
7605 && !bfd_is_abs_section (sec->output_section))
7606 {
7607 struct bfd_elf_section_data *secdata;
7608
7609 secdata = elf_section_data (sec);
7610 if (! _bfd_add_merge_section (obfd,
7611 &elf_hash_table (info)->merge_info,
7612 sec, &secdata->sec_info))
7613 return FALSE;
7614 else if (secdata->sec_info)
7615 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7616 }
7617
7618 if (elf_hash_table (info)->merge_info != NULL)
7619 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7620 merge_sections_remove_hook);
7621 return TRUE;
7622 }
7623
7624 /* Create an entry in an ELF linker hash table. */
7625
7626 struct bfd_hash_entry *
7627 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7628 struct bfd_hash_table *table,
7629 const char *string)
7630 {
7631 /* Allocate the structure if it has not already been allocated by a
7632 subclass. */
7633 if (entry == NULL)
7634 {
7635 entry = (struct bfd_hash_entry *)
7636 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7637 if (entry == NULL)
7638 return entry;
7639 }
7640
7641 /* Call the allocation method of the superclass. */
7642 entry = _bfd_link_hash_newfunc (entry, table, string);
7643 if (entry != NULL)
7644 {
7645 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7646 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7647
7648 /* Set local fields. */
7649 ret->indx = -1;
7650 ret->dynindx = -1;
7651 ret->got = htab->init_got_refcount;
7652 ret->plt = htab->init_plt_refcount;
7653 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7654 - offsetof (struct elf_link_hash_entry, size)));
7655 /* Assume that we have been called by a non-ELF symbol reader.
7656 This flag is then reset by the code which reads an ELF input
7657 file. This ensures that a symbol created by a non-ELF symbol
7658 reader will have the flag set correctly. */
7659 ret->non_elf = 1;
7660 }
7661
7662 return entry;
7663 }
7664
7665 /* Copy data from an indirect symbol to its direct symbol, hiding the
7666 old indirect symbol. Also used for copying flags to a weakdef. */
7667
7668 void
7669 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7670 struct elf_link_hash_entry *dir,
7671 struct elf_link_hash_entry *ind)
7672 {
7673 struct elf_link_hash_table *htab;
7674
7675 if (ind->dyn_relocs != NULL)
7676 {
7677 if (dir->dyn_relocs != NULL)
7678 {
7679 struct elf_dyn_relocs **pp;
7680 struct elf_dyn_relocs *p;
7681
7682 /* Add reloc counts against the indirect sym to the direct sym
7683 list. Merge any entries against the same section. */
7684 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7685 {
7686 struct elf_dyn_relocs *q;
7687
7688 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7689 if (q->sec == p->sec)
7690 {
7691 q->pc_count += p->pc_count;
7692 q->count += p->count;
7693 *pp = p->next;
7694 break;
7695 }
7696 if (q == NULL)
7697 pp = &p->next;
7698 }
7699 *pp = dir->dyn_relocs;
7700 }
7701
7702 dir->dyn_relocs = ind->dyn_relocs;
7703 ind->dyn_relocs = NULL;
7704 }
7705
7706 /* Copy down any references that we may have already seen to the
7707 symbol which just became indirect. */
7708
7709 if (dir->versioned != versioned_hidden)
7710 dir->ref_dynamic |= ind->ref_dynamic;
7711 dir->ref_regular |= ind->ref_regular;
7712 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7713 dir->non_got_ref |= ind->non_got_ref;
7714 dir->needs_plt |= ind->needs_plt;
7715 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7716
7717 if (ind->root.type != bfd_link_hash_indirect)
7718 return;
7719
7720 /* Copy over the global and procedure linkage table refcount entries.
7721 These may have been already set up by a check_relocs routine. */
7722 htab = elf_hash_table (info);
7723 if (ind->got.refcount > htab->init_got_refcount.refcount)
7724 {
7725 if (dir->got.refcount < 0)
7726 dir->got.refcount = 0;
7727 dir->got.refcount += ind->got.refcount;
7728 ind->got.refcount = htab->init_got_refcount.refcount;
7729 }
7730
7731 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7732 {
7733 if (dir->plt.refcount < 0)
7734 dir->plt.refcount = 0;
7735 dir->plt.refcount += ind->plt.refcount;
7736 ind->plt.refcount = htab->init_plt_refcount.refcount;
7737 }
7738
7739 if (ind->dynindx != -1)
7740 {
7741 if (dir->dynindx != -1)
7742 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7743 dir->dynindx = ind->dynindx;
7744 dir->dynstr_index = ind->dynstr_index;
7745 ind->dynindx = -1;
7746 ind->dynstr_index = 0;
7747 }
7748 }
7749
7750 void
7751 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7752 struct elf_link_hash_entry *h,
7753 bfd_boolean force_local)
7754 {
7755 /* STT_GNU_IFUNC symbol must go through PLT. */
7756 if (h->type != STT_GNU_IFUNC)
7757 {
7758 h->plt = elf_hash_table (info)->init_plt_offset;
7759 h->needs_plt = 0;
7760 }
7761 if (force_local)
7762 {
7763 h->forced_local = 1;
7764 if (h->dynindx != -1)
7765 {
7766 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7767 h->dynstr_index);
7768 h->dynindx = -1;
7769 h->dynstr_index = 0;
7770 }
7771 }
7772 }
7773
7774 /* Hide a symbol. */
7775
7776 void
7777 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7778 struct bfd_link_info *info,
7779 struct bfd_link_hash_entry *h)
7780 {
7781 if (is_elf_hash_table (info->hash))
7782 {
7783 const struct elf_backend_data *bed
7784 = get_elf_backend_data (output_bfd);
7785 struct elf_link_hash_entry *eh
7786 = (struct elf_link_hash_entry *) h;
7787 bed->elf_backend_hide_symbol (info, eh, TRUE);
7788 eh->def_dynamic = 0;
7789 eh->ref_dynamic = 0;
7790 eh->dynamic_def = 0;
7791 }
7792 }
7793
7794 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7795 caller. */
7796
7797 bfd_boolean
7798 _bfd_elf_link_hash_table_init
7799 (struct elf_link_hash_table *table,
7800 bfd *abfd,
7801 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7802 struct bfd_hash_table *,
7803 const char *),
7804 unsigned int entsize,
7805 enum elf_target_id target_id)
7806 {
7807 bfd_boolean ret;
7808 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7809
7810 table->init_got_refcount.refcount = can_refcount - 1;
7811 table->init_plt_refcount.refcount = can_refcount - 1;
7812 table->init_got_offset.offset = -(bfd_vma) 1;
7813 table->init_plt_offset.offset = -(bfd_vma) 1;
7814 /* The first dynamic symbol is a dummy. */
7815 table->dynsymcount = 1;
7816
7817 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7818
7819 table->root.type = bfd_link_elf_hash_table;
7820 table->hash_table_id = target_id;
7821 table->target_os = get_elf_backend_data (abfd)->target_os;
7822
7823 return ret;
7824 }
7825
7826 /* Create an ELF linker hash table. */
7827
7828 struct bfd_link_hash_table *
7829 _bfd_elf_link_hash_table_create (bfd *abfd)
7830 {
7831 struct elf_link_hash_table *ret;
7832 size_t amt = sizeof (struct elf_link_hash_table);
7833
7834 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7835 if (ret == NULL)
7836 return NULL;
7837
7838 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7839 sizeof (struct elf_link_hash_entry),
7840 GENERIC_ELF_DATA))
7841 {
7842 free (ret);
7843 return NULL;
7844 }
7845 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7846
7847 return &ret->root;
7848 }
7849
7850 /* Destroy an ELF linker hash table. */
7851
7852 void
7853 _bfd_elf_link_hash_table_free (bfd *obfd)
7854 {
7855 struct elf_link_hash_table *htab;
7856
7857 htab = (struct elf_link_hash_table *) obfd->link.hash;
7858 if (htab->dynstr != NULL)
7859 _bfd_elf_strtab_free (htab->dynstr);
7860 _bfd_merge_sections_free (htab->merge_info);
7861 _bfd_generic_link_hash_table_free (obfd);
7862 }
7863
7864 /* This is a hook for the ELF emulation code in the generic linker to
7865 tell the backend linker what file name to use for the DT_NEEDED
7866 entry for a dynamic object. */
7867
7868 void
7869 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7870 {
7871 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7872 && bfd_get_format (abfd) == bfd_object)
7873 elf_dt_name (abfd) = name;
7874 }
7875
7876 int
7877 bfd_elf_get_dyn_lib_class (bfd *abfd)
7878 {
7879 int lib_class;
7880 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7881 && bfd_get_format (abfd) == bfd_object)
7882 lib_class = elf_dyn_lib_class (abfd);
7883 else
7884 lib_class = 0;
7885 return lib_class;
7886 }
7887
7888 void
7889 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7890 {
7891 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7892 && bfd_get_format (abfd) == bfd_object)
7893 elf_dyn_lib_class (abfd) = lib_class;
7894 }
7895
7896 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7897 the linker ELF emulation code. */
7898
7899 struct bfd_link_needed_list *
7900 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7901 struct bfd_link_info *info)
7902 {
7903 if (! is_elf_hash_table (info->hash))
7904 return NULL;
7905 return elf_hash_table (info)->needed;
7906 }
7907
7908 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7909 hook for the linker ELF emulation code. */
7910
7911 struct bfd_link_needed_list *
7912 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7913 struct bfd_link_info *info)
7914 {
7915 if (! is_elf_hash_table (info->hash))
7916 return NULL;
7917 return elf_hash_table (info)->runpath;
7918 }
7919
7920 /* Get the name actually used for a dynamic object for a link. This
7921 is the SONAME entry if there is one. Otherwise, it is the string
7922 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7923
7924 const char *
7925 bfd_elf_get_dt_soname (bfd *abfd)
7926 {
7927 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7928 && bfd_get_format (abfd) == bfd_object)
7929 return elf_dt_name (abfd);
7930 return NULL;
7931 }
7932
7933 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7934 the ELF linker emulation code. */
7935
7936 bfd_boolean
7937 bfd_elf_get_bfd_needed_list (bfd *abfd,
7938 struct bfd_link_needed_list **pneeded)
7939 {
7940 asection *s;
7941 bfd_byte *dynbuf = NULL;
7942 unsigned int elfsec;
7943 unsigned long shlink;
7944 bfd_byte *extdyn, *extdynend;
7945 size_t extdynsize;
7946 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7947
7948 *pneeded = NULL;
7949
7950 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7951 || bfd_get_format (abfd) != bfd_object)
7952 return TRUE;
7953
7954 s = bfd_get_section_by_name (abfd, ".dynamic");
7955 if (s == NULL || s->size == 0)
7956 return TRUE;
7957
7958 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7959 goto error_return;
7960
7961 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7962 if (elfsec == SHN_BAD)
7963 goto error_return;
7964
7965 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7966
7967 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7968 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7969
7970 extdyn = dynbuf;
7971 extdynend = extdyn + s->size;
7972 for (; extdyn < extdynend; extdyn += extdynsize)
7973 {
7974 Elf_Internal_Dyn dyn;
7975
7976 (*swap_dyn_in) (abfd, extdyn, &dyn);
7977
7978 if (dyn.d_tag == DT_NULL)
7979 break;
7980
7981 if (dyn.d_tag == DT_NEEDED)
7982 {
7983 const char *string;
7984 struct bfd_link_needed_list *l;
7985 unsigned int tagv = dyn.d_un.d_val;
7986 size_t amt;
7987
7988 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7989 if (string == NULL)
7990 goto error_return;
7991
7992 amt = sizeof *l;
7993 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7994 if (l == NULL)
7995 goto error_return;
7996
7997 l->by = abfd;
7998 l->name = string;
7999 l->next = *pneeded;
8000 *pneeded = l;
8001 }
8002 }
8003
8004 free (dynbuf);
8005
8006 return TRUE;
8007
8008 error_return:
8009 free (dynbuf);
8010 return FALSE;
8011 }
8012
8013 struct elf_symbuf_symbol
8014 {
8015 unsigned long st_name; /* Symbol name, index in string tbl */
8016 unsigned char st_info; /* Type and binding attributes */
8017 unsigned char st_other; /* Visibilty, and target specific */
8018 };
8019
8020 struct elf_symbuf_head
8021 {
8022 struct elf_symbuf_symbol *ssym;
8023 size_t count;
8024 unsigned int st_shndx;
8025 };
8026
8027 struct elf_symbol
8028 {
8029 union
8030 {
8031 Elf_Internal_Sym *isym;
8032 struct elf_symbuf_symbol *ssym;
8033 void *p;
8034 } u;
8035 const char *name;
8036 };
8037
8038 /* Sort references to symbols by ascending section number. */
8039
8040 static int
8041 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8042 {
8043 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8044 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8045
8046 if (s1->st_shndx != s2->st_shndx)
8047 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8048 /* Final sort by the address of the sym in the symbuf ensures
8049 a stable sort. */
8050 if (s1 != s2)
8051 return s1 > s2 ? 1 : -1;
8052 return 0;
8053 }
8054
8055 static int
8056 elf_sym_name_compare (const void *arg1, const void *arg2)
8057 {
8058 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8059 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8060 int ret = strcmp (s1->name, s2->name);
8061 if (ret != 0)
8062 return ret;
8063 if (s1->u.p != s2->u.p)
8064 return s1->u.p > s2->u.p ? 1 : -1;
8065 return 0;
8066 }
8067
8068 static struct elf_symbuf_head *
8069 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8070 {
8071 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8072 struct elf_symbuf_symbol *ssym;
8073 struct elf_symbuf_head *ssymbuf, *ssymhead;
8074 size_t i, shndx_count, total_size, amt;
8075
8076 amt = symcount * sizeof (*indbuf);
8077 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8078 if (indbuf == NULL)
8079 return NULL;
8080
8081 for (ind = indbuf, i = 0; i < symcount; i++)
8082 if (isymbuf[i].st_shndx != SHN_UNDEF)
8083 *ind++ = &isymbuf[i];
8084 indbufend = ind;
8085
8086 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8087 elf_sort_elf_symbol);
8088
8089 shndx_count = 0;
8090 if (indbufend > indbuf)
8091 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8092 if (ind[0]->st_shndx != ind[1]->st_shndx)
8093 shndx_count++;
8094
8095 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8096 + (indbufend - indbuf) * sizeof (*ssym));
8097 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8098 if (ssymbuf == NULL)
8099 {
8100 free (indbuf);
8101 return NULL;
8102 }
8103
8104 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8105 ssymbuf->ssym = NULL;
8106 ssymbuf->count = shndx_count;
8107 ssymbuf->st_shndx = 0;
8108 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8109 {
8110 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8111 {
8112 ssymhead++;
8113 ssymhead->ssym = ssym;
8114 ssymhead->count = 0;
8115 ssymhead->st_shndx = (*ind)->st_shndx;
8116 }
8117 ssym->st_name = (*ind)->st_name;
8118 ssym->st_info = (*ind)->st_info;
8119 ssym->st_other = (*ind)->st_other;
8120 ssymhead->count++;
8121 }
8122 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8123 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8124 == total_size));
8125
8126 free (indbuf);
8127 return ssymbuf;
8128 }
8129
8130 /* Check if 2 sections define the same set of local and global
8131 symbols. */
8132
8133 static bfd_boolean
8134 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8135 struct bfd_link_info *info)
8136 {
8137 bfd *bfd1, *bfd2;
8138 const struct elf_backend_data *bed1, *bed2;
8139 Elf_Internal_Shdr *hdr1, *hdr2;
8140 size_t symcount1, symcount2;
8141 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8142 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8143 Elf_Internal_Sym *isym, *isymend;
8144 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8145 size_t count1, count2, i;
8146 unsigned int shndx1, shndx2;
8147 bfd_boolean result;
8148
8149 bfd1 = sec1->owner;
8150 bfd2 = sec2->owner;
8151
8152 /* Both sections have to be in ELF. */
8153 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8154 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8155 return FALSE;
8156
8157 if (elf_section_type (sec1) != elf_section_type (sec2))
8158 return FALSE;
8159
8160 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8161 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8162 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8163 return FALSE;
8164
8165 bed1 = get_elf_backend_data (bfd1);
8166 bed2 = get_elf_backend_data (bfd2);
8167 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8168 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8169 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8170 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8171
8172 if (symcount1 == 0 || symcount2 == 0)
8173 return FALSE;
8174
8175 result = FALSE;
8176 isymbuf1 = NULL;
8177 isymbuf2 = NULL;
8178 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8179 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8180
8181 if (ssymbuf1 == NULL)
8182 {
8183 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8184 NULL, NULL, NULL);
8185 if (isymbuf1 == NULL)
8186 goto done;
8187
8188 if (info != NULL && !info->reduce_memory_overheads)
8189 {
8190 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8191 elf_tdata (bfd1)->symbuf = ssymbuf1;
8192 }
8193 }
8194
8195 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8196 {
8197 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8198 NULL, NULL, NULL);
8199 if (isymbuf2 == NULL)
8200 goto done;
8201
8202 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8203 {
8204 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8205 elf_tdata (bfd2)->symbuf = ssymbuf2;
8206 }
8207 }
8208
8209 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8210 {
8211 /* Optimized faster version. */
8212 size_t lo, hi, mid;
8213 struct elf_symbol *symp;
8214 struct elf_symbuf_symbol *ssym, *ssymend;
8215
8216 lo = 0;
8217 hi = ssymbuf1->count;
8218 ssymbuf1++;
8219 count1 = 0;
8220 while (lo < hi)
8221 {
8222 mid = (lo + hi) / 2;
8223 if (shndx1 < ssymbuf1[mid].st_shndx)
8224 hi = mid;
8225 else if (shndx1 > ssymbuf1[mid].st_shndx)
8226 lo = mid + 1;
8227 else
8228 {
8229 count1 = ssymbuf1[mid].count;
8230 ssymbuf1 += mid;
8231 break;
8232 }
8233 }
8234
8235 lo = 0;
8236 hi = ssymbuf2->count;
8237 ssymbuf2++;
8238 count2 = 0;
8239 while (lo < hi)
8240 {
8241 mid = (lo + hi) / 2;
8242 if (shndx2 < ssymbuf2[mid].st_shndx)
8243 hi = mid;
8244 else if (shndx2 > ssymbuf2[mid].st_shndx)
8245 lo = mid + 1;
8246 else
8247 {
8248 count2 = ssymbuf2[mid].count;
8249 ssymbuf2 += mid;
8250 break;
8251 }
8252 }
8253
8254 if (count1 == 0 || count2 == 0 || count1 != count2)
8255 goto done;
8256
8257 symtable1
8258 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8259 symtable2
8260 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8261 if (symtable1 == NULL || symtable2 == NULL)
8262 goto done;
8263
8264 symp = symtable1;
8265 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8266 ssym < ssymend; ssym++, symp++)
8267 {
8268 symp->u.ssym = ssym;
8269 symp->name = bfd_elf_string_from_elf_section (bfd1,
8270 hdr1->sh_link,
8271 ssym->st_name);
8272 }
8273
8274 symp = symtable2;
8275 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8276 ssym < ssymend; ssym++, symp++)
8277 {
8278 symp->u.ssym = ssym;
8279 symp->name = bfd_elf_string_from_elf_section (bfd2,
8280 hdr2->sh_link,
8281 ssym->st_name);
8282 }
8283
8284 /* Sort symbol by name. */
8285 qsort (symtable1, count1, sizeof (struct elf_symbol),
8286 elf_sym_name_compare);
8287 qsort (symtable2, count1, sizeof (struct elf_symbol),
8288 elf_sym_name_compare);
8289
8290 for (i = 0; i < count1; i++)
8291 /* Two symbols must have the same binding, type and name. */
8292 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8293 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8294 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8295 goto done;
8296
8297 result = TRUE;
8298 goto done;
8299 }
8300
8301 symtable1 = (struct elf_symbol *)
8302 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8303 symtable2 = (struct elf_symbol *)
8304 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8305 if (symtable1 == NULL || symtable2 == NULL)
8306 goto done;
8307
8308 /* Count definitions in the section. */
8309 count1 = 0;
8310 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8311 if (isym->st_shndx == shndx1)
8312 symtable1[count1++].u.isym = isym;
8313
8314 count2 = 0;
8315 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8316 if (isym->st_shndx == shndx2)
8317 symtable2[count2++].u.isym = isym;
8318
8319 if (count1 == 0 || count2 == 0 || count1 != count2)
8320 goto done;
8321
8322 for (i = 0; i < count1; i++)
8323 symtable1[i].name
8324 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8325 symtable1[i].u.isym->st_name);
8326
8327 for (i = 0; i < count2; i++)
8328 symtable2[i].name
8329 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8330 symtable2[i].u.isym->st_name);
8331
8332 /* Sort symbol by name. */
8333 qsort (symtable1, count1, sizeof (struct elf_symbol),
8334 elf_sym_name_compare);
8335 qsort (symtable2, count1, sizeof (struct elf_symbol),
8336 elf_sym_name_compare);
8337
8338 for (i = 0; i < count1; i++)
8339 /* Two symbols must have the same binding, type and name. */
8340 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8341 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8342 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8343 goto done;
8344
8345 result = TRUE;
8346
8347 done:
8348 free (symtable1);
8349 free (symtable2);
8350 free (isymbuf1);
8351 free (isymbuf2);
8352
8353 return result;
8354 }
8355
8356 /* Return TRUE if 2 section types are compatible. */
8357
8358 bfd_boolean
8359 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8360 bfd *bbfd, const asection *bsec)
8361 {
8362 if (asec == NULL
8363 || bsec == NULL
8364 || abfd->xvec->flavour != bfd_target_elf_flavour
8365 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8366 return TRUE;
8367
8368 return elf_section_type (asec) == elf_section_type (bsec);
8369 }
8370 \f
8371 /* Final phase of ELF linker. */
8372
8373 /* A structure we use to avoid passing large numbers of arguments. */
8374
8375 struct elf_final_link_info
8376 {
8377 /* General link information. */
8378 struct bfd_link_info *info;
8379 /* Output BFD. */
8380 bfd *output_bfd;
8381 /* Symbol string table. */
8382 struct elf_strtab_hash *symstrtab;
8383 /* .hash section. */
8384 asection *hash_sec;
8385 /* symbol version section (.gnu.version). */
8386 asection *symver_sec;
8387 /* Buffer large enough to hold contents of any section. */
8388 bfd_byte *contents;
8389 /* Buffer large enough to hold external relocs of any section. */
8390 void *external_relocs;
8391 /* Buffer large enough to hold internal relocs of any section. */
8392 Elf_Internal_Rela *internal_relocs;
8393 /* Buffer large enough to hold external local symbols of any input
8394 BFD. */
8395 bfd_byte *external_syms;
8396 /* And a buffer for symbol section indices. */
8397 Elf_External_Sym_Shndx *locsym_shndx;
8398 /* Buffer large enough to hold internal local symbols of any input
8399 BFD. */
8400 Elf_Internal_Sym *internal_syms;
8401 /* Array large enough to hold a symbol index for each local symbol
8402 of any input BFD. */
8403 long *indices;
8404 /* Array large enough to hold a section pointer for each local
8405 symbol of any input BFD. */
8406 asection **sections;
8407 /* Buffer for SHT_SYMTAB_SHNDX section. */
8408 Elf_External_Sym_Shndx *symshndxbuf;
8409 /* Number of STT_FILE syms seen. */
8410 size_t filesym_count;
8411 };
8412
8413 /* This struct is used to pass information to elf_link_output_extsym. */
8414
8415 struct elf_outext_info
8416 {
8417 bfd_boolean failed;
8418 bfd_boolean localsyms;
8419 bfd_boolean file_sym_done;
8420 struct elf_final_link_info *flinfo;
8421 };
8422
8423
8424 /* Support for evaluating a complex relocation.
8425
8426 Complex relocations are generalized, self-describing relocations. The
8427 implementation of them consists of two parts: complex symbols, and the
8428 relocations themselves.
8429
8430 The relocations are use a reserved elf-wide relocation type code (R_RELC
8431 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8432 information (start bit, end bit, word width, etc) into the addend. This
8433 information is extracted from CGEN-generated operand tables within gas.
8434
8435 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8436 internal) representing prefix-notation expressions, including but not
8437 limited to those sorts of expressions normally encoded as addends in the
8438 addend field. The symbol mangling format is:
8439
8440 <node> := <literal>
8441 | <unary-operator> ':' <node>
8442 | <binary-operator> ':' <node> ':' <node>
8443 ;
8444
8445 <literal> := 's' <digits=N> ':' <N character symbol name>
8446 | 'S' <digits=N> ':' <N character section name>
8447 | '#' <hexdigits>
8448 ;
8449
8450 <binary-operator> := as in C
8451 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8452
8453 static void
8454 set_symbol_value (bfd *bfd_with_globals,
8455 Elf_Internal_Sym *isymbuf,
8456 size_t locsymcount,
8457 size_t symidx,
8458 bfd_vma val)
8459 {
8460 struct elf_link_hash_entry **sym_hashes;
8461 struct elf_link_hash_entry *h;
8462 size_t extsymoff = locsymcount;
8463
8464 if (symidx < locsymcount)
8465 {
8466 Elf_Internal_Sym *sym;
8467
8468 sym = isymbuf + symidx;
8469 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8470 {
8471 /* It is a local symbol: move it to the
8472 "absolute" section and give it a value. */
8473 sym->st_shndx = SHN_ABS;
8474 sym->st_value = val;
8475 return;
8476 }
8477 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8478 extsymoff = 0;
8479 }
8480
8481 /* It is a global symbol: set its link type
8482 to "defined" and give it a value. */
8483
8484 sym_hashes = elf_sym_hashes (bfd_with_globals);
8485 h = sym_hashes [symidx - extsymoff];
8486 while (h->root.type == bfd_link_hash_indirect
8487 || h->root.type == bfd_link_hash_warning)
8488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8489 h->root.type = bfd_link_hash_defined;
8490 h->root.u.def.value = val;
8491 h->root.u.def.section = bfd_abs_section_ptr;
8492 }
8493
8494 static bfd_boolean
8495 resolve_symbol (const char *name,
8496 bfd *input_bfd,
8497 struct elf_final_link_info *flinfo,
8498 bfd_vma *result,
8499 Elf_Internal_Sym *isymbuf,
8500 size_t locsymcount)
8501 {
8502 Elf_Internal_Sym *sym;
8503 struct bfd_link_hash_entry *global_entry;
8504 const char *candidate = NULL;
8505 Elf_Internal_Shdr *symtab_hdr;
8506 size_t i;
8507
8508 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8509
8510 for (i = 0; i < locsymcount; ++ i)
8511 {
8512 sym = isymbuf + i;
8513
8514 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8515 continue;
8516
8517 candidate = bfd_elf_string_from_elf_section (input_bfd,
8518 symtab_hdr->sh_link,
8519 sym->st_name);
8520 #ifdef DEBUG
8521 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8522 name, candidate, (unsigned long) sym->st_value);
8523 #endif
8524 if (candidate && strcmp (candidate, name) == 0)
8525 {
8526 asection *sec = flinfo->sections [i];
8527
8528 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8529 *result += sec->output_offset + sec->output_section->vma;
8530 #ifdef DEBUG
8531 printf ("Found symbol with value %8.8lx\n",
8532 (unsigned long) *result);
8533 #endif
8534 return TRUE;
8535 }
8536 }
8537
8538 /* Hmm, haven't found it yet. perhaps it is a global. */
8539 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8540 FALSE, FALSE, TRUE);
8541 if (!global_entry)
8542 return FALSE;
8543
8544 if (global_entry->type == bfd_link_hash_defined
8545 || global_entry->type == bfd_link_hash_defweak)
8546 {
8547 *result = (global_entry->u.def.value
8548 + global_entry->u.def.section->output_section->vma
8549 + global_entry->u.def.section->output_offset);
8550 #ifdef DEBUG
8551 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8552 global_entry->root.string, (unsigned long) *result);
8553 #endif
8554 return TRUE;
8555 }
8556
8557 return FALSE;
8558 }
8559
8560 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8561 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8562 names like "foo.end" which is the end address of section "foo". */
8563
8564 static bfd_boolean
8565 resolve_section (const char *name,
8566 asection *sections,
8567 bfd_vma *result,
8568 bfd * abfd)
8569 {
8570 asection *curr;
8571 unsigned int len;
8572
8573 for (curr = sections; curr; curr = curr->next)
8574 if (strcmp (curr->name, name) == 0)
8575 {
8576 *result = curr->vma;
8577 return TRUE;
8578 }
8579
8580 /* Hmm. still haven't found it. try pseudo-section names. */
8581 /* FIXME: This could be coded more efficiently... */
8582 for (curr = sections; curr; curr = curr->next)
8583 {
8584 len = strlen (curr->name);
8585 if (len > strlen (name))
8586 continue;
8587
8588 if (strncmp (curr->name, name, len) == 0)
8589 {
8590 if (strncmp (".end", name + len, 4) == 0)
8591 {
8592 *result = (curr->vma
8593 + curr->size / bfd_octets_per_byte (abfd, curr));
8594 return TRUE;
8595 }
8596
8597 /* Insert more pseudo-section names here, if you like. */
8598 }
8599 }
8600
8601 return FALSE;
8602 }
8603
8604 static void
8605 undefined_reference (const char *reftype, const char *name)
8606 {
8607 /* xgettext:c-format */
8608 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8609 reftype, name);
8610 }
8611
8612 static bfd_boolean
8613 eval_symbol (bfd_vma *result,
8614 const char **symp,
8615 bfd *input_bfd,
8616 struct elf_final_link_info *flinfo,
8617 bfd_vma dot,
8618 Elf_Internal_Sym *isymbuf,
8619 size_t locsymcount,
8620 int signed_p)
8621 {
8622 size_t len;
8623 size_t symlen;
8624 bfd_vma a;
8625 bfd_vma b;
8626 char symbuf[4096];
8627 const char *sym = *symp;
8628 const char *symend;
8629 bfd_boolean symbol_is_section = FALSE;
8630
8631 len = strlen (sym);
8632 symend = sym + len;
8633
8634 if (len < 1 || len > sizeof (symbuf))
8635 {
8636 bfd_set_error (bfd_error_invalid_operation);
8637 return FALSE;
8638 }
8639
8640 switch (* sym)
8641 {
8642 case '.':
8643 *result = dot;
8644 *symp = sym + 1;
8645 return TRUE;
8646
8647 case '#':
8648 ++sym;
8649 *result = strtoul (sym, (char **) symp, 16);
8650 return TRUE;
8651
8652 case 'S':
8653 symbol_is_section = TRUE;
8654 /* Fall through. */
8655 case 's':
8656 ++sym;
8657 symlen = strtol (sym, (char **) symp, 10);
8658 sym = *symp + 1; /* Skip the trailing ':'. */
8659
8660 if (symend < sym || symlen + 1 > sizeof (symbuf))
8661 {
8662 bfd_set_error (bfd_error_invalid_operation);
8663 return FALSE;
8664 }
8665
8666 memcpy (symbuf, sym, symlen);
8667 symbuf[symlen] = '\0';
8668 *symp = sym + symlen;
8669
8670 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8671 the symbol as a section, or vice-versa. so we're pretty liberal in our
8672 interpretation here; section means "try section first", not "must be a
8673 section", and likewise with symbol. */
8674
8675 if (symbol_is_section)
8676 {
8677 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8678 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8679 isymbuf, locsymcount))
8680 {
8681 undefined_reference ("section", symbuf);
8682 return FALSE;
8683 }
8684 }
8685 else
8686 {
8687 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8688 isymbuf, locsymcount)
8689 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8690 result, input_bfd))
8691 {
8692 undefined_reference ("symbol", symbuf);
8693 return FALSE;
8694 }
8695 }
8696
8697 return TRUE;
8698
8699 /* All that remains are operators. */
8700
8701 #define UNARY_OP(op) \
8702 if (strncmp (sym, #op, strlen (#op)) == 0) \
8703 { \
8704 sym += strlen (#op); \
8705 if (*sym == ':') \
8706 ++sym; \
8707 *symp = sym; \
8708 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8709 isymbuf, locsymcount, signed_p)) \
8710 return FALSE; \
8711 if (signed_p) \
8712 *result = op ((bfd_signed_vma) a); \
8713 else \
8714 *result = op a; \
8715 return TRUE; \
8716 }
8717
8718 #define BINARY_OP(op) \
8719 if (strncmp (sym, #op, strlen (#op)) == 0) \
8720 { \
8721 sym += strlen (#op); \
8722 if (*sym == ':') \
8723 ++sym; \
8724 *symp = sym; \
8725 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8726 isymbuf, locsymcount, signed_p)) \
8727 return FALSE; \
8728 ++*symp; \
8729 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8730 isymbuf, locsymcount, signed_p)) \
8731 return FALSE; \
8732 if (signed_p) \
8733 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8734 else \
8735 *result = a op b; \
8736 return TRUE; \
8737 }
8738
8739 default:
8740 UNARY_OP (0-);
8741 BINARY_OP (<<);
8742 BINARY_OP (>>);
8743 BINARY_OP (==);
8744 BINARY_OP (!=);
8745 BINARY_OP (<=);
8746 BINARY_OP (>=);
8747 BINARY_OP (&&);
8748 BINARY_OP (||);
8749 UNARY_OP (~);
8750 UNARY_OP (!);
8751 BINARY_OP (*);
8752 BINARY_OP (/);
8753 BINARY_OP (%);
8754 BINARY_OP (^);
8755 BINARY_OP (|);
8756 BINARY_OP (&);
8757 BINARY_OP (+);
8758 BINARY_OP (-);
8759 BINARY_OP (<);
8760 BINARY_OP (>);
8761 #undef UNARY_OP
8762 #undef BINARY_OP
8763 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8764 bfd_set_error (bfd_error_invalid_operation);
8765 return FALSE;
8766 }
8767 }
8768
8769 static void
8770 put_value (bfd_vma size,
8771 unsigned long chunksz,
8772 bfd *input_bfd,
8773 bfd_vma x,
8774 bfd_byte *location)
8775 {
8776 location += (size - chunksz);
8777
8778 for (; size; size -= chunksz, location -= chunksz)
8779 {
8780 switch (chunksz)
8781 {
8782 case 1:
8783 bfd_put_8 (input_bfd, x, location);
8784 x >>= 8;
8785 break;
8786 case 2:
8787 bfd_put_16 (input_bfd, x, location);
8788 x >>= 16;
8789 break;
8790 case 4:
8791 bfd_put_32 (input_bfd, x, location);
8792 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8793 x >>= 16;
8794 x >>= 16;
8795 break;
8796 #ifdef BFD64
8797 case 8:
8798 bfd_put_64 (input_bfd, x, location);
8799 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8800 x >>= 32;
8801 x >>= 32;
8802 break;
8803 #endif
8804 default:
8805 abort ();
8806 break;
8807 }
8808 }
8809 }
8810
8811 static bfd_vma
8812 get_value (bfd_vma size,
8813 unsigned long chunksz,
8814 bfd *input_bfd,
8815 bfd_byte *location)
8816 {
8817 int shift;
8818 bfd_vma x = 0;
8819
8820 /* Sanity checks. */
8821 BFD_ASSERT (chunksz <= sizeof (x)
8822 && size >= chunksz
8823 && chunksz != 0
8824 && (size % chunksz) == 0
8825 && input_bfd != NULL
8826 && location != NULL);
8827
8828 if (chunksz == sizeof (x))
8829 {
8830 BFD_ASSERT (size == chunksz);
8831
8832 /* Make sure that we do not perform an undefined shift operation.
8833 We know that size == chunksz so there will only be one iteration
8834 of the loop below. */
8835 shift = 0;
8836 }
8837 else
8838 shift = 8 * chunksz;
8839
8840 for (; size; size -= chunksz, location += chunksz)
8841 {
8842 switch (chunksz)
8843 {
8844 case 1:
8845 x = (x << shift) | bfd_get_8 (input_bfd, location);
8846 break;
8847 case 2:
8848 x = (x << shift) | bfd_get_16 (input_bfd, location);
8849 break;
8850 case 4:
8851 x = (x << shift) | bfd_get_32 (input_bfd, location);
8852 break;
8853 #ifdef BFD64
8854 case 8:
8855 x = (x << shift) | bfd_get_64 (input_bfd, location);
8856 break;
8857 #endif
8858 default:
8859 abort ();
8860 }
8861 }
8862 return x;
8863 }
8864
8865 static void
8866 decode_complex_addend (unsigned long *start, /* in bits */
8867 unsigned long *oplen, /* in bits */
8868 unsigned long *len, /* in bits */
8869 unsigned long *wordsz, /* in bytes */
8870 unsigned long *chunksz, /* in bytes */
8871 unsigned long *lsb0_p,
8872 unsigned long *signed_p,
8873 unsigned long *trunc_p,
8874 unsigned long encoded)
8875 {
8876 * start = encoded & 0x3F;
8877 * len = (encoded >> 6) & 0x3F;
8878 * oplen = (encoded >> 12) & 0x3F;
8879 * wordsz = (encoded >> 18) & 0xF;
8880 * chunksz = (encoded >> 22) & 0xF;
8881 * lsb0_p = (encoded >> 27) & 1;
8882 * signed_p = (encoded >> 28) & 1;
8883 * trunc_p = (encoded >> 29) & 1;
8884 }
8885
8886 bfd_reloc_status_type
8887 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8888 asection *input_section,
8889 bfd_byte *contents,
8890 Elf_Internal_Rela *rel,
8891 bfd_vma relocation)
8892 {
8893 bfd_vma shift, x, mask;
8894 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8895 bfd_reloc_status_type r;
8896 bfd_size_type octets;
8897
8898 /* Perform this reloc, since it is complex.
8899 (this is not to say that it necessarily refers to a complex
8900 symbol; merely that it is a self-describing CGEN based reloc.
8901 i.e. the addend has the complete reloc information (bit start, end,
8902 word size, etc) encoded within it.). */
8903
8904 decode_complex_addend (&start, &oplen, &len, &wordsz,
8905 &chunksz, &lsb0_p, &signed_p,
8906 &trunc_p, rel->r_addend);
8907
8908 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8909
8910 if (lsb0_p)
8911 shift = (start + 1) - len;
8912 else
8913 shift = (8 * wordsz) - (start + len);
8914
8915 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8916 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8917
8918 #ifdef DEBUG
8919 printf ("Doing complex reloc: "
8920 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8921 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8922 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8923 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8924 oplen, (unsigned long) x, (unsigned long) mask,
8925 (unsigned long) relocation);
8926 #endif
8927
8928 r = bfd_reloc_ok;
8929 if (! trunc_p)
8930 /* Now do an overflow check. */
8931 r = bfd_check_overflow ((signed_p
8932 ? complain_overflow_signed
8933 : complain_overflow_unsigned),
8934 len, 0, (8 * wordsz),
8935 relocation);
8936
8937 /* Do the deed. */
8938 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8939
8940 #ifdef DEBUG
8941 printf (" relocation: %8.8lx\n"
8942 " shifted mask: %8.8lx\n"
8943 " shifted/masked reloc: %8.8lx\n"
8944 " result: %8.8lx\n",
8945 (unsigned long) relocation, (unsigned long) (mask << shift),
8946 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8947 #endif
8948 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8949 return r;
8950 }
8951
8952 /* Functions to read r_offset from external (target order) reloc
8953 entry. Faster than bfd_getl32 et al, because we let the compiler
8954 know the value is aligned. */
8955
8956 static bfd_vma
8957 ext32l_r_offset (const void *p)
8958 {
8959 union aligned32
8960 {
8961 uint32_t v;
8962 unsigned char c[4];
8963 };
8964 const union aligned32 *a
8965 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8966
8967 uint32_t aval = ( (uint32_t) a->c[0]
8968 | (uint32_t) a->c[1] << 8
8969 | (uint32_t) a->c[2] << 16
8970 | (uint32_t) a->c[3] << 24);
8971 return aval;
8972 }
8973
8974 static bfd_vma
8975 ext32b_r_offset (const void *p)
8976 {
8977 union aligned32
8978 {
8979 uint32_t v;
8980 unsigned char c[4];
8981 };
8982 const union aligned32 *a
8983 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8984
8985 uint32_t aval = ( (uint32_t) a->c[0] << 24
8986 | (uint32_t) a->c[1] << 16
8987 | (uint32_t) a->c[2] << 8
8988 | (uint32_t) a->c[3]);
8989 return aval;
8990 }
8991
8992 #ifdef BFD_HOST_64_BIT
8993 static bfd_vma
8994 ext64l_r_offset (const void *p)
8995 {
8996 union aligned64
8997 {
8998 uint64_t v;
8999 unsigned char c[8];
9000 };
9001 const union aligned64 *a
9002 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9003
9004 uint64_t aval = ( (uint64_t) a->c[0]
9005 | (uint64_t) a->c[1] << 8
9006 | (uint64_t) a->c[2] << 16
9007 | (uint64_t) a->c[3] << 24
9008 | (uint64_t) a->c[4] << 32
9009 | (uint64_t) a->c[5] << 40
9010 | (uint64_t) a->c[6] << 48
9011 | (uint64_t) a->c[7] << 56);
9012 return aval;
9013 }
9014
9015 static bfd_vma
9016 ext64b_r_offset (const void *p)
9017 {
9018 union aligned64
9019 {
9020 uint64_t v;
9021 unsigned char c[8];
9022 };
9023 const union aligned64 *a
9024 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9025
9026 uint64_t aval = ( (uint64_t) a->c[0] << 56
9027 | (uint64_t) a->c[1] << 48
9028 | (uint64_t) a->c[2] << 40
9029 | (uint64_t) a->c[3] << 32
9030 | (uint64_t) a->c[4] << 24
9031 | (uint64_t) a->c[5] << 16
9032 | (uint64_t) a->c[6] << 8
9033 | (uint64_t) a->c[7]);
9034 return aval;
9035 }
9036 #endif
9037
9038 /* When performing a relocatable link, the input relocations are
9039 preserved. But, if they reference global symbols, the indices
9040 referenced must be updated. Update all the relocations found in
9041 RELDATA. */
9042
9043 static bfd_boolean
9044 elf_link_adjust_relocs (bfd *abfd,
9045 asection *sec,
9046 struct bfd_elf_section_reloc_data *reldata,
9047 bfd_boolean sort,
9048 struct bfd_link_info *info)
9049 {
9050 unsigned int i;
9051 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9052 bfd_byte *erela;
9053 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9054 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9055 bfd_vma r_type_mask;
9056 int r_sym_shift;
9057 unsigned int count = reldata->count;
9058 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9059
9060 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9061 {
9062 swap_in = bed->s->swap_reloc_in;
9063 swap_out = bed->s->swap_reloc_out;
9064 }
9065 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9066 {
9067 swap_in = bed->s->swap_reloca_in;
9068 swap_out = bed->s->swap_reloca_out;
9069 }
9070 else
9071 abort ();
9072
9073 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9074 abort ();
9075
9076 if (bed->s->arch_size == 32)
9077 {
9078 r_type_mask = 0xff;
9079 r_sym_shift = 8;
9080 }
9081 else
9082 {
9083 r_type_mask = 0xffffffff;
9084 r_sym_shift = 32;
9085 }
9086
9087 erela = reldata->hdr->contents;
9088 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9089 {
9090 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9091 unsigned int j;
9092
9093 if (*rel_hash == NULL)
9094 continue;
9095
9096 if ((*rel_hash)->indx == -2
9097 && info->gc_sections
9098 && ! info->gc_keep_exported)
9099 {
9100 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9101 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9102 abfd, sec,
9103 (*rel_hash)->root.root.string);
9104 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9105 abfd, sec);
9106 bfd_set_error (bfd_error_invalid_operation);
9107 return FALSE;
9108 }
9109 BFD_ASSERT ((*rel_hash)->indx >= 0);
9110
9111 (*swap_in) (abfd, erela, irela);
9112 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9113 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9114 | (irela[j].r_info & r_type_mask));
9115 (*swap_out) (abfd, irela, erela);
9116 }
9117
9118 if (bed->elf_backend_update_relocs)
9119 (*bed->elf_backend_update_relocs) (sec, reldata);
9120
9121 if (sort && count != 0)
9122 {
9123 bfd_vma (*ext_r_off) (const void *);
9124 bfd_vma r_off;
9125 size_t elt_size;
9126 bfd_byte *base, *end, *p, *loc;
9127 bfd_byte *buf = NULL;
9128
9129 if (bed->s->arch_size == 32)
9130 {
9131 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9132 ext_r_off = ext32l_r_offset;
9133 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9134 ext_r_off = ext32b_r_offset;
9135 else
9136 abort ();
9137 }
9138 else
9139 {
9140 #ifdef BFD_HOST_64_BIT
9141 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9142 ext_r_off = ext64l_r_offset;
9143 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9144 ext_r_off = ext64b_r_offset;
9145 else
9146 #endif
9147 abort ();
9148 }
9149
9150 /* Must use a stable sort here. A modified insertion sort,
9151 since the relocs are mostly sorted already. */
9152 elt_size = reldata->hdr->sh_entsize;
9153 base = reldata->hdr->contents;
9154 end = base + count * elt_size;
9155 if (elt_size > sizeof (Elf64_External_Rela))
9156 abort ();
9157
9158 /* Ensure the first element is lowest. This acts as a sentinel,
9159 speeding the main loop below. */
9160 r_off = (*ext_r_off) (base);
9161 for (p = loc = base; (p += elt_size) < end; )
9162 {
9163 bfd_vma r_off2 = (*ext_r_off) (p);
9164 if (r_off > r_off2)
9165 {
9166 r_off = r_off2;
9167 loc = p;
9168 }
9169 }
9170 if (loc != base)
9171 {
9172 /* Don't just swap *base and *loc as that changes the order
9173 of the original base[0] and base[1] if they happen to
9174 have the same r_offset. */
9175 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9176 memcpy (onebuf, loc, elt_size);
9177 memmove (base + elt_size, base, loc - base);
9178 memcpy (base, onebuf, elt_size);
9179 }
9180
9181 for (p = base + elt_size; (p += elt_size) < end; )
9182 {
9183 /* base to p is sorted, *p is next to insert. */
9184 r_off = (*ext_r_off) (p);
9185 /* Search the sorted region for location to insert. */
9186 loc = p - elt_size;
9187 while (r_off < (*ext_r_off) (loc))
9188 loc -= elt_size;
9189 loc += elt_size;
9190 if (loc != p)
9191 {
9192 /* Chances are there is a run of relocs to insert here,
9193 from one of more input files. Files are not always
9194 linked in order due to the way elf_link_input_bfd is
9195 called. See pr17666. */
9196 size_t sortlen = p - loc;
9197 bfd_vma r_off2 = (*ext_r_off) (loc);
9198 size_t runlen = elt_size;
9199 size_t buf_size = 96 * 1024;
9200 while (p + runlen < end
9201 && (sortlen <= buf_size
9202 || runlen + elt_size <= buf_size)
9203 && r_off2 > (*ext_r_off) (p + runlen))
9204 runlen += elt_size;
9205 if (buf == NULL)
9206 {
9207 buf = bfd_malloc (buf_size);
9208 if (buf == NULL)
9209 return FALSE;
9210 }
9211 if (runlen < sortlen)
9212 {
9213 memcpy (buf, p, runlen);
9214 memmove (loc + runlen, loc, sortlen);
9215 memcpy (loc, buf, runlen);
9216 }
9217 else
9218 {
9219 memcpy (buf, loc, sortlen);
9220 memmove (loc, p, runlen);
9221 memcpy (loc + runlen, buf, sortlen);
9222 }
9223 p += runlen - elt_size;
9224 }
9225 }
9226 /* Hashes are no longer valid. */
9227 free (reldata->hashes);
9228 reldata->hashes = NULL;
9229 free (buf);
9230 }
9231 return TRUE;
9232 }
9233
9234 struct elf_link_sort_rela
9235 {
9236 union {
9237 bfd_vma offset;
9238 bfd_vma sym_mask;
9239 } u;
9240 enum elf_reloc_type_class type;
9241 /* We use this as an array of size int_rels_per_ext_rel. */
9242 Elf_Internal_Rela rela[1];
9243 };
9244
9245 /* qsort stability here and for cmp2 is only an issue if multiple
9246 dynamic relocations are emitted at the same address. But targets
9247 that apply a series of dynamic relocations each operating on the
9248 result of the prior relocation can't use -z combreloc as
9249 implemented anyway. Such schemes tend to be broken by sorting on
9250 symbol index. That leaves dynamic NONE relocs as the only other
9251 case where ld might emit multiple relocs at the same address, and
9252 those are only emitted due to target bugs. */
9253
9254 static int
9255 elf_link_sort_cmp1 (const void *A, const void *B)
9256 {
9257 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9258 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9259 int relativea, relativeb;
9260
9261 relativea = a->type == reloc_class_relative;
9262 relativeb = b->type == reloc_class_relative;
9263
9264 if (relativea < relativeb)
9265 return 1;
9266 if (relativea > relativeb)
9267 return -1;
9268 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9269 return -1;
9270 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9271 return 1;
9272 if (a->rela->r_offset < b->rela->r_offset)
9273 return -1;
9274 if (a->rela->r_offset > b->rela->r_offset)
9275 return 1;
9276 return 0;
9277 }
9278
9279 static int
9280 elf_link_sort_cmp2 (const void *A, const void *B)
9281 {
9282 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9283 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9284
9285 if (a->type < b->type)
9286 return -1;
9287 if (a->type > b->type)
9288 return 1;
9289 if (a->u.offset < b->u.offset)
9290 return -1;
9291 if (a->u.offset > b->u.offset)
9292 return 1;
9293 if (a->rela->r_offset < b->rela->r_offset)
9294 return -1;
9295 if (a->rela->r_offset > b->rela->r_offset)
9296 return 1;
9297 return 0;
9298 }
9299
9300 static size_t
9301 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9302 {
9303 asection *dynamic_relocs;
9304 asection *rela_dyn;
9305 asection *rel_dyn;
9306 bfd_size_type count, size;
9307 size_t i, ret, sort_elt, ext_size;
9308 bfd_byte *sort, *s_non_relative, *p;
9309 struct elf_link_sort_rela *sq;
9310 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9311 int i2e = bed->s->int_rels_per_ext_rel;
9312 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9313 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9314 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9315 struct bfd_link_order *lo;
9316 bfd_vma r_sym_mask;
9317 bfd_boolean use_rela;
9318
9319 /* Find a dynamic reloc section. */
9320 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9321 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9322 if (rela_dyn != NULL && rela_dyn->size > 0
9323 && rel_dyn != NULL && rel_dyn->size > 0)
9324 {
9325 bfd_boolean use_rela_initialised = FALSE;
9326
9327 /* This is just here to stop gcc from complaining.
9328 Its initialization checking code is not perfect. */
9329 use_rela = TRUE;
9330
9331 /* Both sections are present. Examine the sizes
9332 of the indirect sections to help us choose. */
9333 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9334 if (lo->type == bfd_indirect_link_order)
9335 {
9336 asection *o = lo->u.indirect.section;
9337
9338 if ((o->size % bed->s->sizeof_rela) == 0)
9339 {
9340 if ((o->size % bed->s->sizeof_rel) == 0)
9341 /* Section size is divisible by both rel and rela sizes.
9342 It is of no help to us. */
9343 ;
9344 else
9345 {
9346 /* Section size is only divisible by rela. */
9347 if (use_rela_initialised && !use_rela)
9348 {
9349 _bfd_error_handler (_("%pB: unable to sort relocs - "
9350 "they are in more than one size"),
9351 abfd);
9352 bfd_set_error (bfd_error_invalid_operation);
9353 return 0;
9354 }
9355 else
9356 {
9357 use_rela = TRUE;
9358 use_rela_initialised = TRUE;
9359 }
9360 }
9361 }
9362 else if ((o->size % bed->s->sizeof_rel) == 0)
9363 {
9364 /* Section size is only divisible by rel. */
9365 if (use_rela_initialised && use_rela)
9366 {
9367 _bfd_error_handler (_("%pB: unable to sort relocs - "
9368 "they are in more than one size"),
9369 abfd);
9370 bfd_set_error (bfd_error_invalid_operation);
9371 return 0;
9372 }
9373 else
9374 {
9375 use_rela = FALSE;
9376 use_rela_initialised = TRUE;
9377 }
9378 }
9379 else
9380 {
9381 /* The section size is not divisible by either -
9382 something is wrong. */
9383 _bfd_error_handler (_("%pB: unable to sort relocs - "
9384 "they are of an unknown size"), abfd);
9385 bfd_set_error (bfd_error_invalid_operation);
9386 return 0;
9387 }
9388 }
9389
9390 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9391 if (lo->type == bfd_indirect_link_order)
9392 {
9393 asection *o = lo->u.indirect.section;
9394
9395 if ((o->size % bed->s->sizeof_rela) == 0)
9396 {
9397 if ((o->size % bed->s->sizeof_rel) == 0)
9398 /* Section size is divisible by both rel and rela sizes.
9399 It is of no help to us. */
9400 ;
9401 else
9402 {
9403 /* Section size is only divisible by rela. */
9404 if (use_rela_initialised && !use_rela)
9405 {
9406 _bfd_error_handler (_("%pB: unable to sort relocs - "
9407 "they are in more than one size"),
9408 abfd);
9409 bfd_set_error (bfd_error_invalid_operation);
9410 return 0;
9411 }
9412 else
9413 {
9414 use_rela = TRUE;
9415 use_rela_initialised = TRUE;
9416 }
9417 }
9418 }
9419 else if ((o->size % bed->s->sizeof_rel) == 0)
9420 {
9421 /* Section size is only divisible by rel. */
9422 if (use_rela_initialised && use_rela)
9423 {
9424 _bfd_error_handler (_("%pB: unable to sort relocs - "
9425 "they are in more than one size"),
9426 abfd);
9427 bfd_set_error (bfd_error_invalid_operation);
9428 return 0;
9429 }
9430 else
9431 {
9432 use_rela = FALSE;
9433 use_rela_initialised = TRUE;
9434 }
9435 }
9436 else
9437 {
9438 /* The section size is not divisible by either -
9439 something is wrong. */
9440 _bfd_error_handler (_("%pB: unable to sort relocs - "
9441 "they are of an unknown size"), abfd);
9442 bfd_set_error (bfd_error_invalid_operation);
9443 return 0;
9444 }
9445 }
9446
9447 if (! use_rela_initialised)
9448 /* Make a guess. */
9449 use_rela = TRUE;
9450 }
9451 else if (rela_dyn != NULL && rela_dyn->size > 0)
9452 use_rela = TRUE;
9453 else if (rel_dyn != NULL && rel_dyn->size > 0)
9454 use_rela = FALSE;
9455 else
9456 return 0;
9457
9458 if (use_rela)
9459 {
9460 dynamic_relocs = rela_dyn;
9461 ext_size = bed->s->sizeof_rela;
9462 swap_in = bed->s->swap_reloca_in;
9463 swap_out = bed->s->swap_reloca_out;
9464 }
9465 else
9466 {
9467 dynamic_relocs = rel_dyn;
9468 ext_size = bed->s->sizeof_rel;
9469 swap_in = bed->s->swap_reloc_in;
9470 swap_out = bed->s->swap_reloc_out;
9471 }
9472
9473 size = 0;
9474 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9475 if (lo->type == bfd_indirect_link_order)
9476 size += lo->u.indirect.section->size;
9477
9478 if (size != dynamic_relocs->size)
9479 return 0;
9480
9481 sort_elt = (sizeof (struct elf_link_sort_rela)
9482 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9483
9484 count = dynamic_relocs->size / ext_size;
9485 if (count == 0)
9486 return 0;
9487 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9488
9489 if (sort == NULL)
9490 {
9491 (*info->callbacks->warning)
9492 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9493 return 0;
9494 }
9495
9496 if (bed->s->arch_size == 32)
9497 r_sym_mask = ~(bfd_vma) 0xff;
9498 else
9499 r_sym_mask = ~(bfd_vma) 0xffffffff;
9500
9501 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9502 if (lo->type == bfd_indirect_link_order)
9503 {
9504 bfd_byte *erel, *erelend;
9505 asection *o = lo->u.indirect.section;
9506
9507 if (o->contents == NULL && o->size != 0)
9508 {
9509 /* This is a reloc section that is being handled as a normal
9510 section. See bfd_section_from_shdr. We can't combine
9511 relocs in this case. */
9512 free (sort);
9513 return 0;
9514 }
9515 erel = o->contents;
9516 erelend = o->contents + o->size;
9517 p = sort + o->output_offset * opb / ext_size * sort_elt;
9518
9519 while (erel < erelend)
9520 {
9521 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9522
9523 (*swap_in) (abfd, erel, s->rela);
9524 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9525 s->u.sym_mask = r_sym_mask;
9526 p += sort_elt;
9527 erel += ext_size;
9528 }
9529 }
9530
9531 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9532
9533 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9534 {
9535 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9536 if (s->type != reloc_class_relative)
9537 break;
9538 }
9539 ret = i;
9540 s_non_relative = p;
9541
9542 sq = (struct elf_link_sort_rela *) s_non_relative;
9543 for (; i < count; i++, p += sort_elt)
9544 {
9545 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9546 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9547 sq = sp;
9548 sp->u.offset = sq->rela->r_offset;
9549 }
9550
9551 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9552
9553 struct elf_link_hash_table *htab = elf_hash_table (info);
9554 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9555 {
9556 /* We have plt relocs in .rela.dyn. */
9557 sq = (struct elf_link_sort_rela *) sort;
9558 for (i = 0; i < count; i++)
9559 if (sq[count - i - 1].type != reloc_class_plt)
9560 break;
9561 if (i != 0 && htab->srelplt->size == i * ext_size)
9562 {
9563 struct bfd_link_order **plo;
9564 /* Put srelplt link_order last. This is so the output_offset
9565 set in the next loop is correct for DT_JMPREL. */
9566 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9567 if ((*plo)->type == bfd_indirect_link_order
9568 && (*plo)->u.indirect.section == htab->srelplt)
9569 {
9570 lo = *plo;
9571 *plo = lo->next;
9572 }
9573 else
9574 plo = &(*plo)->next;
9575 *plo = lo;
9576 lo->next = NULL;
9577 dynamic_relocs->map_tail.link_order = lo;
9578 }
9579 }
9580
9581 p = sort;
9582 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9583 if (lo->type == bfd_indirect_link_order)
9584 {
9585 bfd_byte *erel, *erelend;
9586 asection *o = lo->u.indirect.section;
9587
9588 erel = o->contents;
9589 erelend = o->contents + o->size;
9590 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9591 while (erel < erelend)
9592 {
9593 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9594 (*swap_out) (abfd, s->rela, erel);
9595 p += sort_elt;
9596 erel += ext_size;
9597 }
9598 }
9599
9600 free (sort);
9601 *psec = dynamic_relocs;
9602 return ret;
9603 }
9604
9605 /* Add a symbol to the output symbol string table. */
9606
9607 static int
9608 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9609 const char *name,
9610 Elf_Internal_Sym *elfsym,
9611 asection *input_sec,
9612 struct elf_link_hash_entry *h)
9613 {
9614 int (*output_symbol_hook)
9615 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9616 struct elf_link_hash_entry *);
9617 struct elf_link_hash_table *hash_table;
9618 const struct elf_backend_data *bed;
9619 bfd_size_type strtabsize;
9620
9621 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9622
9623 bed = get_elf_backend_data (flinfo->output_bfd);
9624 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9625 if (output_symbol_hook != NULL)
9626 {
9627 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9628 if (ret != 1)
9629 return ret;
9630 }
9631
9632 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9633 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9634 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9635 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9636
9637 if (name == NULL
9638 || *name == '\0'
9639 || (input_sec->flags & SEC_EXCLUDE))
9640 elfsym->st_name = (unsigned long) -1;
9641 else
9642 {
9643 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9644 to get the final offset for st_name. */
9645 elfsym->st_name
9646 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9647 name, FALSE);
9648 if (elfsym->st_name == (unsigned long) -1)
9649 return 0;
9650 }
9651
9652 hash_table = elf_hash_table (flinfo->info);
9653 strtabsize = hash_table->strtabsize;
9654 if (strtabsize <= hash_table->strtabcount)
9655 {
9656 strtabsize += strtabsize;
9657 hash_table->strtabsize = strtabsize;
9658 strtabsize *= sizeof (*hash_table->strtab);
9659 hash_table->strtab
9660 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9661 strtabsize);
9662 if (hash_table->strtab == NULL)
9663 return 0;
9664 }
9665 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9666 hash_table->strtab[hash_table->strtabcount].dest_index
9667 = hash_table->strtabcount;
9668 hash_table->strtab[hash_table->strtabcount].destshndx_index
9669 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9670
9671 flinfo->output_bfd->symcount += 1;
9672 hash_table->strtabcount += 1;
9673
9674 return 1;
9675 }
9676
9677 /* Swap symbols out to the symbol table and flush the output symbols to
9678 the file. */
9679
9680 static bfd_boolean
9681 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9682 {
9683 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9684 size_t amt;
9685 size_t i;
9686 const struct elf_backend_data *bed;
9687 bfd_byte *symbuf;
9688 Elf_Internal_Shdr *hdr;
9689 file_ptr pos;
9690 bfd_boolean ret;
9691
9692 if (!hash_table->strtabcount)
9693 return TRUE;
9694
9695 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9696
9697 bed = get_elf_backend_data (flinfo->output_bfd);
9698
9699 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9700 symbuf = (bfd_byte *) bfd_malloc (amt);
9701 if (symbuf == NULL)
9702 return FALSE;
9703
9704 if (flinfo->symshndxbuf)
9705 {
9706 amt = sizeof (Elf_External_Sym_Shndx);
9707 amt *= bfd_get_symcount (flinfo->output_bfd);
9708 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9709 if (flinfo->symshndxbuf == NULL)
9710 {
9711 free (symbuf);
9712 return FALSE;
9713 }
9714 }
9715
9716 for (i = 0; i < hash_table->strtabcount; i++)
9717 {
9718 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9719 if (elfsym->sym.st_name == (unsigned long) -1)
9720 elfsym->sym.st_name = 0;
9721 else
9722 elfsym->sym.st_name
9723 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9724 elfsym->sym.st_name);
9725 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9726 ((bfd_byte *) symbuf
9727 + (elfsym->dest_index
9728 * bed->s->sizeof_sym)),
9729 (flinfo->symshndxbuf
9730 + elfsym->destshndx_index));
9731 }
9732
9733 /* Allow the linker to examine the strtab and symtab now they are
9734 populated. */
9735
9736 if (flinfo->info->callbacks->examine_strtab)
9737 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9738 hash_table->strtabcount,
9739 flinfo->symstrtab);
9740
9741 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9742 pos = hdr->sh_offset + hdr->sh_size;
9743 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9744 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9745 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9746 {
9747 hdr->sh_size += amt;
9748 ret = TRUE;
9749 }
9750 else
9751 ret = FALSE;
9752
9753 free (symbuf);
9754
9755 free (hash_table->strtab);
9756 hash_table->strtab = NULL;
9757
9758 return ret;
9759 }
9760
9761 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9762
9763 static bfd_boolean
9764 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9765 {
9766 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9767 && sym->st_shndx < SHN_LORESERVE)
9768 {
9769 /* The gABI doesn't support dynamic symbols in output sections
9770 beyond 64k. */
9771 _bfd_error_handler
9772 /* xgettext:c-format */
9773 (_("%pB: too many sections: %d (>= %d)"),
9774 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9775 bfd_set_error (bfd_error_nonrepresentable_section);
9776 return FALSE;
9777 }
9778 return TRUE;
9779 }
9780
9781 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9782 allowing an unsatisfied unversioned symbol in the DSO to match a
9783 versioned symbol that would normally require an explicit version.
9784 We also handle the case that a DSO references a hidden symbol
9785 which may be satisfied by a versioned symbol in another DSO. */
9786
9787 static bfd_boolean
9788 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9789 const struct elf_backend_data *bed,
9790 struct elf_link_hash_entry *h)
9791 {
9792 bfd *abfd;
9793 struct elf_link_loaded_list *loaded;
9794
9795 if (!is_elf_hash_table (info->hash))
9796 return FALSE;
9797
9798 /* Check indirect symbol. */
9799 while (h->root.type == bfd_link_hash_indirect)
9800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9801
9802 switch (h->root.type)
9803 {
9804 default:
9805 abfd = NULL;
9806 break;
9807
9808 case bfd_link_hash_undefined:
9809 case bfd_link_hash_undefweak:
9810 abfd = h->root.u.undef.abfd;
9811 if (abfd == NULL
9812 || (abfd->flags & DYNAMIC) == 0
9813 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9814 return FALSE;
9815 break;
9816
9817 case bfd_link_hash_defined:
9818 case bfd_link_hash_defweak:
9819 abfd = h->root.u.def.section->owner;
9820 break;
9821
9822 case bfd_link_hash_common:
9823 abfd = h->root.u.c.p->section->owner;
9824 break;
9825 }
9826 BFD_ASSERT (abfd != NULL);
9827
9828 for (loaded = elf_hash_table (info)->dyn_loaded;
9829 loaded != NULL;
9830 loaded = loaded->next)
9831 {
9832 bfd *input;
9833 Elf_Internal_Shdr *hdr;
9834 size_t symcount;
9835 size_t extsymcount;
9836 size_t extsymoff;
9837 Elf_Internal_Shdr *versymhdr;
9838 Elf_Internal_Sym *isym;
9839 Elf_Internal_Sym *isymend;
9840 Elf_Internal_Sym *isymbuf;
9841 Elf_External_Versym *ever;
9842 Elf_External_Versym *extversym;
9843
9844 input = loaded->abfd;
9845
9846 /* We check each DSO for a possible hidden versioned definition. */
9847 if (input == abfd
9848 || elf_dynversym (input) == 0)
9849 continue;
9850
9851 hdr = &elf_tdata (input)->dynsymtab_hdr;
9852
9853 symcount = hdr->sh_size / bed->s->sizeof_sym;
9854 if (elf_bad_symtab (input))
9855 {
9856 extsymcount = symcount;
9857 extsymoff = 0;
9858 }
9859 else
9860 {
9861 extsymcount = symcount - hdr->sh_info;
9862 extsymoff = hdr->sh_info;
9863 }
9864
9865 if (extsymcount == 0)
9866 continue;
9867
9868 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9869 NULL, NULL, NULL);
9870 if (isymbuf == NULL)
9871 return FALSE;
9872
9873 /* Read in any version definitions. */
9874 versymhdr = &elf_tdata (input)->dynversym_hdr;
9875 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9876 || (extversym = (Elf_External_Versym *)
9877 _bfd_malloc_and_read (input, versymhdr->sh_size,
9878 versymhdr->sh_size)) == NULL)
9879 {
9880 free (isymbuf);
9881 return FALSE;
9882 }
9883
9884 ever = extversym + extsymoff;
9885 isymend = isymbuf + extsymcount;
9886 for (isym = isymbuf; isym < isymend; isym++, ever++)
9887 {
9888 const char *name;
9889 Elf_Internal_Versym iver;
9890 unsigned short version_index;
9891
9892 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9893 || isym->st_shndx == SHN_UNDEF)
9894 continue;
9895
9896 name = bfd_elf_string_from_elf_section (input,
9897 hdr->sh_link,
9898 isym->st_name);
9899 if (strcmp (name, h->root.root.string) != 0)
9900 continue;
9901
9902 _bfd_elf_swap_versym_in (input, ever, &iver);
9903
9904 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9905 && !(h->def_regular
9906 && h->forced_local))
9907 {
9908 /* If we have a non-hidden versioned sym, then it should
9909 have provided a definition for the undefined sym unless
9910 it is defined in a non-shared object and forced local.
9911 */
9912 abort ();
9913 }
9914
9915 version_index = iver.vs_vers & VERSYM_VERSION;
9916 if (version_index == 1 || version_index == 2)
9917 {
9918 /* This is the base or first version. We can use it. */
9919 free (extversym);
9920 free (isymbuf);
9921 return TRUE;
9922 }
9923 }
9924
9925 free (extversym);
9926 free (isymbuf);
9927 }
9928
9929 return FALSE;
9930 }
9931
9932 /* Convert ELF common symbol TYPE. */
9933
9934 static int
9935 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9936 {
9937 /* Commom symbol can only appear in relocatable link. */
9938 if (!bfd_link_relocatable (info))
9939 abort ();
9940 switch (info->elf_stt_common)
9941 {
9942 case unchanged:
9943 break;
9944 case elf_stt_common:
9945 type = STT_COMMON;
9946 break;
9947 case no_elf_stt_common:
9948 type = STT_OBJECT;
9949 break;
9950 }
9951 return type;
9952 }
9953
9954 /* Add an external symbol to the symbol table. This is called from
9955 the hash table traversal routine. When generating a shared object,
9956 we go through the symbol table twice. The first time we output
9957 anything that might have been forced to local scope in a version
9958 script. The second time we output the symbols that are still
9959 global symbols. */
9960
9961 static bfd_boolean
9962 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9963 {
9964 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9965 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9966 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9967 bfd_boolean strip;
9968 Elf_Internal_Sym sym;
9969 asection *input_sec;
9970 const struct elf_backend_data *bed;
9971 long indx;
9972 int ret;
9973 unsigned int type;
9974
9975 if (h->root.type == bfd_link_hash_warning)
9976 {
9977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9978 if (h->root.type == bfd_link_hash_new)
9979 return TRUE;
9980 }
9981
9982 /* Decide whether to output this symbol in this pass. */
9983 if (eoinfo->localsyms)
9984 {
9985 if (!h->forced_local)
9986 return TRUE;
9987 }
9988 else
9989 {
9990 if (h->forced_local)
9991 return TRUE;
9992 }
9993
9994 bed = get_elf_backend_data (flinfo->output_bfd);
9995
9996 if (h->root.type == bfd_link_hash_undefined)
9997 {
9998 /* If we have an undefined symbol reference here then it must have
9999 come from a shared library that is being linked in. (Undefined
10000 references in regular files have already been handled unless
10001 they are in unreferenced sections which are removed by garbage
10002 collection). */
10003 bfd_boolean ignore_undef = FALSE;
10004
10005 /* Some symbols may be special in that the fact that they're
10006 undefined can be safely ignored - let backend determine that. */
10007 if (bed->elf_backend_ignore_undef_symbol)
10008 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10009
10010 /* If we are reporting errors for this situation then do so now. */
10011 if (!ignore_undef
10012 && h->ref_dynamic_nonweak
10013 && (!h->ref_regular || flinfo->info->gc_sections)
10014 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10015 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10016 {
10017 flinfo->info->callbacks->undefined_symbol
10018 (flinfo->info, h->root.root.string,
10019 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10020 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10021 && !flinfo->info->warn_unresolved_syms);
10022 }
10023
10024 /* Strip a global symbol defined in a discarded section. */
10025 if (h->indx == -3)
10026 return TRUE;
10027 }
10028
10029 /* We should also warn if a forced local symbol is referenced from
10030 shared libraries. */
10031 if (bfd_link_executable (flinfo->info)
10032 && h->forced_local
10033 && h->ref_dynamic
10034 && h->def_regular
10035 && !h->dynamic_def
10036 && h->ref_dynamic_nonweak
10037 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10038 {
10039 bfd *def_bfd;
10040 const char *msg;
10041 struct elf_link_hash_entry *hi = h;
10042
10043 /* Check indirect symbol. */
10044 while (hi->root.type == bfd_link_hash_indirect)
10045 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10046
10047 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10048 /* xgettext:c-format */
10049 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10050 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10051 /* xgettext:c-format */
10052 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10053 else
10054 /* xgettext:c-format */
10055 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10056 def_bfd = flinfo->output_bfd;
10057 if (hi->root.u.def.section != bfd_abs_section_ptr)
10058 def_bfd = hi->root.u.def.section->owner;
10059 _bfd_error_handler (msg, flinfo->output_bfd,
10060 h->root.root.string, def_bfd);
10061 bfd_set_error (bfd_error_bad_value);
10062 eoinfo->failed = TRUE;
10063 return FALSE;
10064 }
10065
10066 /* We don't want to output symbols that have never been mentioned by
10067 a regular file, or that we have been told to strip. However, if
10068 h->indx is set to -2, the symbol is used by a reloc and we must
10069 output it. */
10070 strip = FALSE;
10071 if (h->indx == -2)
10072 ;
10073 else if ((h->def_dynamic
10074 || h->ref_dynamic
10075 || h->root.type == bfd_link_hash_new)
10076 && !h->def_regular
10077 && !h->ref_regular)
10078 strip = TRUE;
10079 else if (flinfo->info->strip == strip_all)
10080 strip = TRUE;
10081 else if (flinfo->info->strip == strip_some
10082 && bfd_hash_lookup (flinfo->info->keep_hash,
10083 h->root.root.string, FALSE, FALSE) == NULL)
10084 strip = TRUE;
10085 else if ((h->root.type == bfd_link_hash_defined
10086 || h->root.type == bfd_link_hash_defweak)
10087 && ((flinfo->info->strip_discarded
10088 && discarded_section (h->root.u.def.section))
10089 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10090 && h->root.u.def.section->owner != NULL
10091 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10092 strip = TRUE;
10093 else if ((h->root.type == bfd_link_hash_undefined
10094 || h->root.type == bfd_link_hash_undefweak)
10095 && h->root.u.undef.abfd != NULL
10096 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10097 strip = TRUE;
10098
10099 type = h->type;
10100
10101 /* If we're stripping it, and it's not a dynamic symbol, there's
10102 nothing else to do. However, if it is a forced local symbol or
10103 an ifunc symbol we need to give the backend finish_dynamic_symbol
10104 function a chance to make it dynamic. */
10105 if (strip
10106 && h->dynindx == -1
10107 && type != STT_GNU_IFUNC
10108 && !h->forced_local)
10109 return TRUE;
10110
10111 sym.st_value = 0;
10112 sym.st_size = h->size;
10113 sym.st_other = h->other;
10114 switch (h->root.type)
10115 {
10116 default:
10117 case bfd_link_hash_new:
10118 case bfd_link_hash_warning:
10119 abort ();
10120 return FALSE;
10121
10122 case bfd_link_hash_undefined:
10123 case bfd_link_hash_undefweak:
10124 input_sec = bfd_und_section_ptr;
10125 sym.st_shndx = SHN_UNDEF;
10126 break;
10127
10128 case bfd_link_hash_defined:
10129 case bfd_link_hash_defweak:
10130 {
10131 input_sec = h->root.u.def.section;
10132 if (input_sec->output_section != NULL)
10133 {
10134 sym.st_shndx =
10135 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10136 input_sec->output_section);
10137 if (sym.st_shndx == SHN_BAD)
10138 {
10139 _bfd_error_handler
10140 /* xgettext:c-format */
10141 (_("%pB: could not find output section %pA for input section %pA"),
10142 flinfo->output_bfd, input_sec->output_section, input_sec);
10143 bfd_set_error (bfd_error_nonrepresentable_section);
10144 eoinfo->failed = TRUE;
10145 return FALSE;
10146 }
10147
10148 /* ELF symbols in relocatable files are section relative,
10149 but in nonrelocatable files they are virtual
10150 addresses. */
10151 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10152 if (!bfd_link_relocatable (flinfo->info))
10153 {
10154 sym.st_value += input_sec->output_section->vma;
10155 if (h->type == STT_TLS)
10156 {
10157 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10158 if (tls_sec != NULL)
10159 sym.st_value -= tls_sec->vma;
10160 }
10161 }
10162 }
10163 else
10164 {
10165 BFD_ASSERT (input_sec->owner == NULL
10166 || (input_sec->owner->flags & DYNAMIC) != 0);
10167 sym.st_shndx = SHN_UNDEF;
10168 input_sec = bfd_und_section_ptr;
10169 }
10170 }
10171 break;
10172
10173 case bfd_link_hash_common:
10174 input_sec = h->root.u.c.p->section;
10175 sym.st_shndx = bed->common_section_index (input_sec);
10176 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10177 break;
10178
10179 case bfd_link_hash_indirect:
10180 /* These symbols are created by symbol versioning. They point
10181 to the decorated version of the name. For example, if the
10182 symbol foo@@GNU_1.2 is the default, which should be used when
10183 foo is used with no version, then we add an indirect symbol
10184 foo which points to foo@@GNU_1.2. We ignore these symbols,
10185 since the indirected symbol is already in the hash table. */
10186 return TRUE;
10187 }
10188
10189 if (type == STT_COMMON || type == STT_OBJECT)
10190 switch (h->root.type)
10191 {
10192 case bfd_link_hash_common:
10193 type = elf_link_convert_common_type (flinfo->info, type);
10194 break;
10195 case bfd_link_hash_defined:
10196 case bfd_link_hash_defweak:
10197 if (bed->common_definition (&sym))
10198 type = elf_link_convert_common_type (flinfo->info, type);
10199 else
10200 type = STT_OBJECT;
10201 break;
10202 case bfd_link_hash_undefined:
10203 case bfd_link_hash_undefweak:
10204 break;
10205 default:
10206 abort ();
10207 }
10208
10209 if (h->forced_local)
10210 {
10211 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10212 /* Turn off visibility on local symbol. */
10213 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10214 }
10215 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10216 else if (h->unique_global && h->def_regular)
10217 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10218 else if (h->root.type == bfd_link_hash_undefweak
10219 || h->root.type == bfd_link_hash_defweak)
10220 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10221 else
10222 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10223 sym.st_target_internal = h->target_internal;
10224
10225 /* Give the processor backend a chance to tweak the symbol value,
10226 and also to finish up anything that needs to be done for this
10227 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10228 forced local syms when non-shared is due to a historical quirk.
10229 STT_GNU_IFUNC symbol must go through PLT. */
10230 if ((h->type == STT_GNU_IFUNC
10231 && h->def_regular
10232 && !bfd_link_relocatable (flinfo->info))
10233 || ((h->dynindx != -1
10234 || h->forced_local)
10235 && ((bfd_link_pic (flinfo->info)
10236 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10237 || h->root.type != bfd_link_hash_undefweak))
10238 || !h->forced_local)
10239 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10240 {
10241 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10242 (flinfo->output_bfd, flinfo->info, h, &sym)))
10243 {
10244 eoinfo->failed = TRUE;
10245 return FALSE;
10246 }
10247 }
10248
10249 /* If we are marking the symbol as undefined, and there are no
10250 non-weak references to this symbol from a regular object, then
10251 mark the symbol as weak undefined; if there are non-weak
10252 references, mark the symbol as strong. We can't do this earlier,
10253 because it might not be marked as undefined until the
10254 finish_dynamic_symbol routine gets through with it. */
10255 if (sym.st_shndx == SHN_UNDEF
10256 && h->ref_regular
10257 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10258 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10259 {
10260 int bindtype;
10261 type = ELF_ST_TYPE (sym.st_info);
10262
10263 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10264 if (type == STT_GNU_IFUNC)
10265 type = STT_FUNC;
10266
10267 if (h->ref_regular_nonweak)
10268 bindtype = STB_GLOBAL;
10269 else
10270 bindtype = STB_WEAK;
10271 sym.st_info = ELF_ST_INFO (bindtype, type);
10272 }
10273
10274 /* If this is a symbol defined in a dynamic library, don't use the
10275 symbol size from the dynamic library. Relinking an executable
10276 against a new library may introduce gratuitous changes in the
10277 executable's symbols if we keep the size. */
10278 if (sym.st_shndx == SHN_UNDEF
10279 && !h->def_regular
10280 && h->def_dynamic)
10281 sym.st_size = 0;
10282
10283 /* If a non-weak symbol with non-default visibility is not defined
10284 locally, it is a fatal error. */
10285 if (!bfd_link_relocatable (flinfo->info)
10286 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10287 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10288 && h->root.type == bfd_link_hash_undefined
10289 && !h->def_regular)
10290 {
10291 const char *msg;
10292
10293 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10294 /* xgettext:c-format */
10295 msg = _("%pB: protected symbol `%s' isn't defined");
10296 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10297 /* xgettext:c-format */
10298 msg = _("%pB: internal symbol `%s' isn't defined");
10299 else
10300 /* xgettext:c-format */
10301 msg = _("%pB: hidden symbol `%s' isn't defined");
10302 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10303 bfd_set_error (bfd_error_bad_value);
10304 eoinfo->failed = TRUE;
10305 return FALSE;
10306 }
10307
10308 /* If this symbol should be put in the .dynsym section, then put it
10309 there now. We already know the symbol index. We also fill in
10310 the entry in the .hash section. */
10311 if (h->dynindx != -1
10312 && elf_hash_table (flinfo->info)->dynamic_sections_created
10313 && elf_hash_table (flinfo->info)->dynsym != NULL
10314 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10315 {
10316 bfd_byte *esym;
10317
10318 /* Since there is no version information in the dynamic string,
10319 if there is no version info in symbol version section, we will
10320 have a run-time problem if not linking executable, referenced
10321 by shared library, or not bound locally. */
10322 if (h->verinfo.verdef == NULL
10323 && (!bfd_link_executable (flinfo->info)
10324 || h->ref_dynamic
10325 || !h->def_regular))
10326 {
10327 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10328
10329 if (p && p [1] != '\0')
10330 {
10331 _bfd_error_handler
10332 /* xgettext:c-format */
10333 (_("%pB: no symbol version section for versioned symbol `%s'"),
10334 flinfo->output_bfd, h->root.root.string);
10335 eoinfo->failed = TRUE;
10336 return FALSE;
10337 }
10338 }
10339
10340 sym.st_name = h->dynstr_index;
10341 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10342 + h->dynindx * bed->s->sizeof_sym);
10343 if (!check_dynsym (flinfo->output_bfd, &sym))
10344 {
10345 eoinfo->failed = TRUE;
10346 return FALSE;
10347 }
10348 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10349
10350 if (flinfo->hash_sec != NULL)
10351 {
10352 size_t hash_entry_size;
10353 bfd_byte *bucketpos;
10354 bfd_vma chain;
10355 size_t bucketcount;
10356 size_t bucket;
10357
10358 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10359 bucket = h->u.elf_hash_value % bucketcount;
10360
10361 hash_entry_size
10362 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10363 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10364 + (bucket + 2) * hash_entry_size);
10365 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10366 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10367 bucketpos);
10368 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10369 ((bfd_byte *) flinfo->hash_sec->contents
10370 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10371 }
10372
10373 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10374 {
10375 Elf_Internal_Versym iversym;
10376 Elf_External_Versym *eversym;
10377
10378 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10379 {
10380 if (h->verinfo.verdef == NULL
10381 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10382 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10383 iversym.vs_vers = 0;
10384 else
10385 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10386 }
10387 else
10388 {
10389 if (h->verinfo.vertree == NULL)
10390 iversym.vs_vers = 1;
10391 else
10392 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10393 if (flinfo->info->create_default_symver)
10394 iversym.vs_vers++;
10395 }
10396
10397 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10398 defined locally. */
10399 if (h->versioned == versioned_hidden && h->def_regular)
10400 iversym.vs_vers |= VERSYM_HIDDEN;
10401
10402 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10403 eversym += h->dynindx;
10404 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10405 }
10406 }
10407
10408 /* If the symbol is undefined, and we didn't output it to .dynsym,
10409 strip it from .symtab too. Obviously we can't do this for
10410 relocatable output or when needed for --emit-relocs. */
10411 else if (input_sec == bfd_und_section_ptr
10412 && h->indx != -2
10413 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10414 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10415 && !bfd_link_relocatable (flinfo->info))
10416 return TRUE;
10417
10418 /* Also strip others that we couldn't earlier due to dynamic symbol
10419 processing. */
10420 if (strip)
10421 return TRUE;
10422 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10423 return TRUE;
10424
10425 /* Output a FILE symbol so that following locals are not associated
10426 with the wrong input file. We need one for forced local symbols
10427 if we've seen more than one FILE symbol or when we have exactly
10428 one FILE symbol but global symbols are present in a file other
10429 than the one with the FILE symbol. We also need one if linker
10430 defined symbols are present. In practice these conditions are
10431 always met, so just emit the FILE symbol unconditionally. */
10432 if (eoinfo->localsyms
10433 && !eoinfo->file_sym_done
10434 && eoinfo->flinfo->filesym_count != 0)
10435 {
10436 Elf_Internal_Sym fsym;
10437
10438 memset (&fsym, 0, sizeof (fsym));
10439 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10440 fsym.st_shndx = SHN_ABS;
10441 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10442 bfd_und_section_ptr, NULL))
10443 return FALSE;
10444
10445 eoinfo->file_sym_done = TRUE;
10446 }
10447
10448 indx = bfd_get_symcount (flinfo->output_bfd);
10449 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10450 input_sec, h);
10451 if (ret == 0)
10452 {
10453 eoinfo->failed = TRUE;
10454 return FALSE;
10455 }
10456 else if (ret == 1)
10457 h->indx = indx;
10458 else if (h->indx == -2)
10459 abort();
10460
10461 return TRUE;
10462 }
10463
10464 /* Return TRUE if special handling is done for relocs in SEC against
10465 symbols defined in discarded sections. */
10466
10467 static bfd_boolean
10468 elf_section_ignore_discarded_relocs (asection *sec)
10469 {
10470 const struct elf_backend_data *bed;
10471
10472 switch (sec->sec_info_type)
10473 {
10474 case SEC_INFO_TYPE_STABS:
10475 case SEC_INFO_TYPE_EH_FRAME:
10476 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10477 return TRUE;
10478 default:
10479 break;
10480 }
10481
10482 bed = get_elf_backend_data (sec->owner);
10483 if (bed->elf_backend_ignore_discarded_relocs != NULL
10484 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10485 return TRUE;
10486
10487 return FALSE;
10488 }
10489
10490 /* Return a mask saying how ld should treat relocations in SEC against
10491 symbols defined in discarded sections. If this function returns
10492 COMPLAIN set, ld will issue a warning message. If this function
10493 returns PRETEND set, and the discarded section was link-once and the
10494 same size as the kept link-once section, ld will pretend that the
10495 symbol was actually defined in the kept section. Otherwise ld will
10496 zero the reloc (at least that is the intent, but some cooperation by
10497 the target dependent code is needed, particularly for REL targets). */
10498
10499 unsigned int
10500 _bfd_elf_default_action_discarded (asection *sec)
10501 {
10502 if (sec->flags & SEC_DEBUGGING)
10503 return PRETEND;
10504
10505 if (strcmp (".eh_frame", sec->name) == 0)
10506 return 0;
10507
10508 if (strcmp (".gcc_except_table", sec->name) == 0)
10509 return 0;
10510
10511 return COMPLAIN | PRETEND;
10512 }
10513
10514 /* Find a match between a section and a member of a section group. */
10515
10516 static asection *
10517 match_group_member (asection *sec, asection *group,
10518 struct bfd_link_info *info)
10519 {
10520 asection *first = elf_next_in_group (group);
10521 asection *s = first;
10522
10523 while (s != NULL)
10524 {
10525 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10526 return s;
10527
10528 s = elf_next_in_group (s);
10529 if (s == first)
10530 break;
10531 }
10532
10533 return NULL;
10534 }
10535
10536 /* Check if the kept section of a discarded section SEC can be used
10537 to replace it. Return the replacement if it is OK. Otherwise return
10538 NULL. */
10539
10540 asection *
10541 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10542 {
10543 asection *kept;
10544
10545 kept = sec->kept_section;
10546 if (kept != NULL)
10547 {
10548 if ((kept->flags & SEC_GROUP) != 0)
10549 kept = match_group_member (sec, kept, info);
10550 if (kept != NULL
10551 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10552 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10553 kept = NULL;
10554 sec->kept_section = kept;
10555 }
10556 return kept;
10557 }
10558
10559 /* Link an input file into the linker output file. This function
10560 handles all the sections and relocations of the input file at once.
10561 This is so that we only have to read the local symbols once, and
10562 don't have to keep them in memory. */
10563
10564 static bfd_boolean
10565 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10566 {
10567 int (*relocate_section)
10568 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10569 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10570 bfd *output_bfd;
10571 Elf_Internal_Shdr *symtab_hdr;
10572 size_t locsymcount;
10573 size_t extsymoff;
10574 Elf_Internal_Sym *isymbuf;
10575 Elf_Internal_Sym *isym;
10576 Elf_Internal_Sym *isymend;
10577 long *pindex;
10578 asection **ppsection;
10579 asection *o;
10580 const struct elf_backend_data *bed;
10581 struct elf_link_hash_entry **sym_hashes;
10582 bfd_size_type address_size;
10583 bfd_vma r_type_mask;
10584 int r_sym_shift;
10585 bfd_boolean have_file_sym = FALSE;
10586
10587 output_bfd = flinfo->output_bfd;
10588 bed = get_elf_backend_data (output_bfd);
10589 relocate_section = bed->elf_backend_relocate_section;
10590
10591 /* If this is a dynamic object, we don't want to do anything here:
10592 we don't want the local symbols, and we don't want the section
10593 contents. */
10594 if ((input_bfd->flags & DYNAMIC) != 0)
10595 return TRUE;
10596
10597 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10598 if (elf_bad_symtab (input_bfd))
10599 {
10600 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10601 extsymoff = 0;
10602 }
10603 else
10604 {
10605 locsymcount = symtab_hdr->sh_info;
10606 extsymoff = symtab_hdr->sh_info;
10607 }
10608
10609 /* Read the local symbols. */
10610 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10611 if (isymbuf == NULL && locsymcount != 0)
10612 {
10613 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10614 flinfo->internal_syms,
10615 flinfo->external_syms,
10616 flinfo->locsym_shndx);
10617 if (isymbuf == NULL)
10618 return FALSE;
10619 }
10620
10621 /* Find local symbol sections and adjust values of symbols in
10622 SEC_MERGE sections. Write out those local symbols we know are
10623 going into the output file. */
10624 isymend = isymbuf + locsymcount;
10625 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10626 isym < isymend;
10627 isym++, pindex++, ppsection++)
10628 {
10629 asection *isec;
10630 const char *name;
10631 Elf_Internal_Sym osym;
10632 long indx;
10633 int ret;
10634
10635 *pindex = -1;
10636
10637 if (elf_bad_symtab (input_bfd))
10638 {
10639 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10640 {
10641 *ppsection = NULL;
10642 continue;
10643 }
10644 }
10645
10646 if (isym->st_shndx == SHN_UNDEF)
10647 isec = bfd_und_section_ptr;
10648 else if (isym->st_shndx == SHN_ABS)
10649 isec = bfd_abs_section_ptr;
10650 else if (isym->st_shndx == SHN_COMMON)
10651 isec = bfd_com_section_ptr;
10652 else
10653 {
10654 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10655 if (isec == NULL)
10656 {
10657 /* Don't attempt to output symbols with st_shnx in the
10658 reserved range other than SHN_ABS and SHN_COMMON. */
10659 isec = bfd_und_section_ptr;
10660 }
10661 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10662 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10663 isym->st_value =
10664 _bfd_merged_section_offset (output_bfd, &isec,
10665 elf_section_data (isec)->sec_info,
10666 isym->st_value);
10667 }
10668
10669 *ppsection = isec;
10670
10671 /* Don't output the first, undefined, symbol. In fact, don't
10672 output any undefined local symbol. */
10673 if (isec == bfd_und_section_ptr)
10674 continue;
10675
10676 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10677 {
10678 /* We never output section symbols. Instead, we use the
10679 section symbol of the corresponding section in the output
10680 file. */
10681 continue;
10682 }
10683
10684 /* If we are stripping all symbols, we don't want to output this
10685 one. */
10686 if (flinfo->info->strip == strip_all)
10687 continue;
10688
10689 /* If we are discarding all local symbols, we don't want to
10690 output this one. If we are generating a relocatable output
10691 file, then some of the local symbols may be required by
10692 relocs; we output them below as we discover that they are
10693 needed. */
10694 if (flinfo->info->discard == discard_all)
10695 continue;
10696
10697 /* If this symbol is defined in a section which we are
10698 discarding, we don't need to keep it. */
10699 if (isym->st_shndx != SHN_UNDEF
10700 && isym->st_shndx < SHN_LORESERVE
10701 && isec->output_section == NULL
10702 && flinfo->info->non_contiguous_regions
10703 && flinfo->info->non_contiguous_regions_warnings)
10704 {
10705 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10706 "discards section `%s' from '%s'\n"),
10707 isec->name, bfd_get_filename (isec->owner));
10708 continue;
10709 }
10710
10711 if (isym->st_shndx != SHN_UNDEF
10712 && isym->st_shndx < SHN_LORESERVE
10713 && bfd_section_removed_from_list (output_bfd,
10714 isec->output_section))
10715 continue;
10716
10717 /* Get the name of the symbol. */
10718 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10719 isym->st_name);
10720 if (name == NULL)
10721 return FALSE;
10722
10723 /* See if we are discarding symbols with this name. */
10724 if ((flinfo->info->strip == strip_some
10725 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10726 == NULL))
10727 || (((flinfo->info->discard == discard_sec_merge
10728 && (isec->flags & SEC_MERGE)
10729 && !bfd_link_relocatable (flinfo->info))
10730 || flinfo->info->discard == discard_l)
10731 && bfd_is_local_label_name (input_bfd, name)))
10732 continue;
10733
10734 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10735 {
10736 if (input_bfd->lto_output)
10737 /* -flto puts a temp file name here. This means builds
10738 are not reproducible. Discard the symbol. */
10739 continue;
10740 have_file_sym = TRUE;
10741 flinfo->filesym_count += 1;
10742 }
10743 if (!have_file_sym)
10744 {
10745 /* In the absence of debug info, bfd_find_nearest_line uses
10746 FILE symbols to determine the source file for local
10747 function symbols. Provide a FILE symbol here if input
10748 files lack such, so that their symbols won't be
10749 associated with a previous input file. It's not the
10750 source file, but the best we can do. */
10751 have_file_sym = TRUE;
10752 flinfo->filesym_count += 1;
10753 memset (&osym, 0, sizeof (osym));
10754 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10755 osym.st_shndx = SHN_ABS;
10756 if (!elf_link_output_symstrtab (flinfo,
10757 (input_bfd->lto_output ? NULL
10758 : bfd_get_filename (input_bfd)),
10759 &osym, bfd_abs_section_ptr,
10760 NULL))
10761 return FALSE;
10762 }
10763
10764 osym = *isym;
10765
10766 /* Adjust the section index for the output file. */
10767 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10768 isec->output_section);
10769 if (osym.st_shndx == SHN_BAD)
10770 return FALSE;
10771
10772 /* ELF symbols in relocatable files are section relative, but
10773 in executable files they are virtual addresses. Note that
10774 this code assumes that all ELF sections have an associated
10775 BFD section with a reasonable value for output_offset; below
10776 we assume that they also have a reasonable value for
10777 output_section. Any special sections must be set up to meet
10778 these requirements. */
10779 osym.st_value += isec->output_offset;
10780 if (!bfd_link_relocatable (flinfo->info))
10781 {
10782 osym.st_value += isec->output_section->vma;
10783 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10784 {
10785 /* STT_TLS symbols are relative to PT_TLS segment base. */
10786 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10787 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10788 else
10789 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10790 STT_NOTYPE);
10791 }
10792 }
10793
10794 indx = bfd_get_symcount (output_bfd);
10795 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10796 if (ret == 0)
10797 return FALSE;
10798 else if (ret == 1)
10799 *pindex = indx;
10800 }
10801
10802 if (bed->s->arch_size == 32)
10803 {
10804 r_type_mask = 0xff;
10805 r_sym_shift = 8;
10806 address_size = 4;
10807 }
10808 else
10809 {
10810 r_type_mask = 0xffffffff;
10811 r_sym_shift = 32;
10812 address_size = 8;
10813 }
10814
10815 /* Relocate the contents of each section. */
10816 sym_hashes = elf_sym_hashes (input_bfd);
10817 for (o = input_bfd->sections; o != NULL; o = o->next)
10818 {
10819 bfd_byte *contents;
10820
10821 if (! o->linker_mark)
10822 {
10823 /* This section was omitted from the link. */
10824 continue;
10825 }
10826
10827 if (!flinfo->info->resolve_section_groups
10828 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10829 {
10830 /* Deal with the group signature symbol. */
10831 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10832 unsigned long symndx = sec_data->this_hdr.sh_info;
10833 asection *osec = o->output_section;
10834
10835 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10836 if (symndx >= locsymcount
10837 || (elf_bad_symtab (input_bfd)
10838 && flinfo->sections[symndx] == NULL))
10839 {
10840 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10841 while (h->root.type == bfd_link_hash_indirect
10842 || h->root.type == bfd_link_hash_warning)
10843 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10844 /* Arrange for symbol to be output. */
10845 h->indx = -2;
10846 elf_section_data (osec)->this_hdr.sh_info = -2;
10847 }
10848 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10849 {
10850 /* We'll use the output section target_index. */
10851 asection *sec = flinfo->sections[symndx]->output_section;
10852 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10853 }
10854 else
10855 {
10856 if (flinfo->indices[symndx] == -1)
10857 {
10858 /* Otherwise output the local symbol now. */
10859 Elf_Internal_Sym sym = isymbuf[symndx];
10860 asection *sec = flinfo->sections[symndx]->output_section;
10861 const char *name;
10862 long indx;
10863 int ret;
10864
10865 name = bfd_elf_string_from_elf_section (input_bfd,
10866 symtab_hdr->sh_link,
10867 sym.st_name);
10868 if (name == NULL)
10869 return FALSE;
10870
10871 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10872 sec);
10873 if (sym.st_shndx == SHN_BAD)
10874 return FALSE;
10875
10876 sym.st_value += o->output_offset;
10877
10878 indx = bfd_get_symcount (output_bfd);
10879 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10880 NULL);
10881 if (ret == 0)
10882 return FALSE;
10883 else if (ret == 1)
10884 flinfo->indices[symndx] = indx;
10885 else
10886 abort ();
10887 }
10888 elf_section_data (osec)->this_hdr.sh_info
10889 = flinfo->indices[symndx];
10890 }
10891 }
10892
10893 if ((o->flags & SEC_HAS_CONTENTS) == 0
10894 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10895 continue;
10896
10897 if ((o->flags & SEC_LINKER_CREATED) != 0)
10898 {
10899 /* Section was created by _bfd_elf_link_create_dynamic_sections
10900 or somesuch. */
10901 continue;
10902 }
10903
10904 /* Get the contents of the section. They have been cached by a
10905 relaxation routine. Note that o is a section in an input
10906 file, so the contents field will not have been set by any of
10907 the routines which work on output files. */
10908 if (elf_section_data (o)->this_hdr.contents != NULL)
10909 {
10910 contents = elf_section_data (o)->this_hdr.contents;
10911 if (bed->caches_rawsize
10912 && o->rawsize != 0
10913 && o->rawsize < o->size)
10914 {
10915 memcpy (flinfo->contents, contents, o->rawsize);
10916 contents = flinfo->contents;
10917 }
10918 }
10919 else
10920 {
10921 contents = flinfo->contents;
10922 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10923 return FALSE;
10924 }
10925
10926 if ((o->flags & SEC_RELOC) != 0)
10927 {
10928 Elf_Internal_Rela *internal_relocs;
10929 Elf_Internal_Rela *rel, *relend;
10930 int action_discarded;
10931 int ret;
10932
10933 /* Get the swapped relocs. */
10934 internal_relocs
10935 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10936 flinfo->internal_relocs, FALSE);
10937 if (internal_relocs == NULL
10938 && o->reloc_count > 0)
10939 return FALSE;
10940
10941 /* We need to reverse-copy input .ctors/.dtors sections if
10942 they are placed in .init_array/.finit_array for output. */
10943 if (o->size > address_size
10944 && ((strncmp (o->name, ".ctors", 6) == 0
10945 && strcmp (o->output_section->name,
10946 ".init_array") == 0)
10947 || (strncmp (o->name, ".dtors", 6) == 0
10948 && strcmp (o->output_section->name,
10949 ".fini_array") == 0))
10950 && (o->name[6] == 0 || o->name[6] == '.'))
10951 {
10952 if (o->size * bed->s->int_rels_per_ext_rel
10953 != o->reloc_count * address_size)
10954 {
10955 _bfd_error_handler
10956 /* xgettext:c-format */
10957 (_("error: %pB: size of section %pA is not "
10958 "multiple of address size"),
10959 input_bfd, o);
10960 bfd_set_error (bfd_error_bad_value);
10961 return FALSE;
10962 }
10963 o->flags |= SEC_ELF_REVERSE_COPY;
10964 }
10965
10966 action_discarded = -1;
10967 if (!elf_section_ignore_discarded_relocs (o))
10968 action_discarded = (*bed->action_discarded) (o);
10969
10970 /* Run through the relocs evaluating complex reloc symbols and
10971 looking for relocs against symbols from discarded sections
10972 or section symbols from removed link-once sections.
10973 Complain about relocs against discarded sections. Zero
10974 relocs against removed link-once sections. */
10975
10976 rel = internal_relocs;
10977 relend = rel + o->reloc_count;
10978 for ( ; rel < relend; rel++)
10979 {
10980 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10981 unsigned int s_type;
10982 asection **ps, *sec;
10983 struct elf_link_hash_entry *h = NULL;
10984 const char *sym_name;
10985
10986 if (r_symndx == STN_UNDEF)
10987 continue;
10988
10989 if (r_symndx >= locsymcount
10990 || (elf_bad_symtab (input_bfd)
10991 && flinfo->sections[r_symndx] == NULL))
10992 {
10993 h = sym_hashes[r_symndx - extsymoff];
10994
10995 /* Badly formatted input files can contain relocs that
10996 reference non-existant symbols. Check here so that
10997 we do not seg fault. */
10998 if (h == NULL)
10999 {
11000 _bfd_error_handler
11001 /* xgettext:c-format */
11002 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11003 "that references a non-existent global symbol"),
11004 input_bfd, (uint64_t) rel->r_info, o);
11005 bfd_set_error (bfd_error_bad_value);
11006 return FALSE;
11007 }
11008
11009 while (h->root.type == bfd_link_hash_indirect
11010 || h->root.type == bfd_link_hash_warning)
11011 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11012
11013 s_type = h->type;
11014
11015 /* If a plugin symbol is referenced from a non-IR file,
11016 mark the symbol as undefined. Note that the
11017 linker may attach linker created dynamic sections
11018 to the plugin bfd. Symbols defined in linker
11019 created sections are not plugin symbols. */
11020 if ((h->root.non_ir_ref_regular
11021 || h->root.non_ir_ref_dynamic)
11022 && (h->root.type == bfd_link_hash_defined
11023 || h->root.type == bfd_link_hash_defweak)
11024 && (h->root.u.def.section->flags
11025 & SEC_LINKER_CREATED) == 0
11026 && h->root.u.def.section->owner != NULL
11027 && (h->root.u.def.section->owner->flags
11028 & BFD_PLUGIN) != 0)
11029 {
11030 h->root.type = bfd_link_hash_undefined;
11031 h->root.u.undef.abfd = h->root.u.def.section->owner;
11032 }
11033
11034 ps = NULL;
11035 if (h->root.type == bfd_link_hash_defined
11036 || h->root.type == bfd_link_hash_defweak)
11037 ps = &h->root.u.def.section;
11038
11039 sym_name = h->root.root.string;
11040 }
11041 else
11042 {
11043 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11044
11045 s_type = ELF_ST_TYPE (sym->st_info);
11046 ps = &flinfo->sections[r_symndx];
11047 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11048 sym, *ps);
11049 }
11050
11051 if ((s_type == STT_RELC || s_type == STT_SRELC)
11052 && !bfd_link_relocatable (flinfo->info))
11053 {
11054 bfd_vma val;
11055 bfd_vma dot = (rel->r_offset
11056 + o->output_offset + o->output_section->vma);
11057 #ifdef DEBUG
11058 printf ("Encountered a complex symbol!");
11059 printf (" (input_bfd %s, section %s, reloc %ld\n",
11060 bfd_get_filename (input_bfd), o->name,
11061 (long) (rel - internal_relocs));
11062 printf (" symbol: idx %8.8lx, name %s\n",
11063 r_symndx, sym_name);
11064 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11065 (unsigned long) rel->r_info,
11066 (unsigned long) rel->r_offset);
11067 #endif
11068 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11069 isymbuf, locsymcount, s_type == STT_SRELC))
11070 return FALSE;
11071
11072 /* Symbol evaluated OK. Update to absolute value. */
11073 set_symbol_value (input_bfd, isymbuf, locsymcount,
11074 r_symndx, val);
11075 continue;
11076 }
11077
11078 if (action_discarded != -1 && ps != NULL)
11079 {
11080 /* Complain if the definition comes from a
11081 discarded section. */
11082 if ((sec = *ps) != NULL && discarded_section (sec))
11083 {
11084 BFD_ASSERT (r_symndx != STN_UNDEF);
11085 if (action_discarded & COMPLAIN)
11086 (*flinfo->info->callbacks->einfo)
11087 /* xgettext:c-format */
11088 (_("%X`%s' referenced in section `%pA' of %pB: "
11089 "defined in discarded section `%pA' of %pB\n"),
11090 sym_name, o, input_bfd, sec, sec->owner);
11091
11092 /* Try to do the best we can to support buggy old
11093 versions of gcc. Pretend that the symbol is
11094 really defined in the kept linkonce section.
11095 FIXME: This is quite broken. Modifying the
11096 symbol here means we will be changing all later
11097 uses of the symbol, not just in this section. */
11098 if (action_discarded & PRETEND)
11099 {
11100 asection *kept;
11101
11102 kept = _bfd_elf_check_kept_section (sec,
11103 flinfo->info);
11104 if (kept != NULL)
11105 {
11106 *ps = kept;
11107 continue;
11108 }
11109 }
11110 }
11111 }
11112 }
11113
11114 /* Relocate the section by invoking a back end routine.
11115
11116 The back end routine is responsible for adjusting the
11117 section contents as necessary, and (if using Rela relocs
11118 and generating a relocatable output file) adjusting the
11119 reloc addend as necessary.
11120
11121 The back end routine does not have to worry about setting
11122 the reloc address or the reloc symbol index.
11123
11124 The back end routine is given a pointer to the swapped in
11125 internal symbols, and can access the hash table entries
11126 for the external symbols via elf_sym_hashes (input_bfd).
11127
11128 When generating relocatable output, the back end routine
11129 must handle STB_LOCAL/STT_SECTION symbols specially. The
11130 output symbol is going to be a section symbol
11131 corresponding to the output section, which will require
11132 the addend to be adjusted. */
11133
11134 ret = (*relocate_section) (output_bfd, flinfo->info,
11135 input_bfd, o, contents,
11136 internal_relocs,
11137 isymbuf,
11138 flinfo->sections);
11139 if (!ret)
11140 return FALSE;
11141
11142 if (ret == 2
11143 || bfd_link_relocatable (flinfo->info)
11144 || flinfo->info->emitrelocations)
11145 {
11146 Elf_Internal_Rela *irela;
11147 Elf_Internal_Rela *irelaend, *irelamid;
11148 bfd_vma last_offset;
11149 struct elf_link_hash_entry **rel_hash;
11150 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11151 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11152 unsigned int next_erel;
11153 bfd_boolean rela_normal;
11154 struct bfd_elf_section_data *esdi, *esdo;
11155
11156 esdi = elf_section_data (o);
11157 esdo = elf_section_data (o->output_section);
11158 rela_normal = FALSE;
11159
11160 /* Adjust the reloc addresses and symbol indices. */
11161
11162 irela = internal_relocs;
11163 irelaend = irela + o->reloc_count;
11164 rel_hash = esdo->rel.hashes + esdo->rel.count;
11165 /* We start processing the REL relocs, if any. When we reach
11166 IRELAMID in the loop, we switch to the RELA relocs. */
11167 irelamid = irela;
11168 if (esdi->rel.hdr != NULL)
11169 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11170 * bed->s->int_rels_per_ext_rel);
11171 rel_hash_list = rel_hash;
11172 rela_hash_list = NULL;
11173 last_offset = o->output_offset;
11174 if (!bfd_link_relocatable (flinfo->info))
11175 last_offset += o->output_section->vma;
11176 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11177 {
11178 unsigned long r_symndx;
11179 asection *sec;
11180 Elf_Internal_Sym sym;
11181
11182 if (next_erel == bed->s->int_rels_per_ext_rel)
11183 {
11184 rel_hash++;
11185 next_erel = 0;
11186 }
11187
11188 if (irela == irelamid)
11189 {
11190 rel_hash = esdo->rela.hashes + esdo->rela.count;
11191 rela_hash_list = rel_hash;
11192 rela_normal = bed->rela_normal;
11193 }
11194
11195 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11196 flinfo->info, o,
11197 irela->r_offset);
11198 if (irela->r_offset >= (bfd_vma) -2)
11199 {
11200 /* This is a reloc for a deleted entry or somesuch.
11201 Turn it into an R_*_NONE reloc, at the same
11202 offset as the last reloc. elf_eh_frame.c and
11203 bfd_elf_discard_info rely on reloc offsets
11204 being ordered. */
11205 irela->r_offset = last_offset;
11206 irela->r_info = 0;
11207 irela->r_addend = 0;
11208 continue;
11209 }
11210
11211 irela->r_offset += o->output_offset;
11212
11213 /* Relocs in an executable have to be virtual addresses. */
11214 if (!bfd_link_relocatable (flinfo->info))
11215 irela->r_offset += o->output_section->vma;
11216
11217 last_offset = irela->r_offset;
11218
11219 r_symndx = irela->r_info >> r_sym_shift;
11220 if (r_symndx == STN_UNDEF)
11221 continue;
11222
11223 if (r_symndx >= locsymcount
11224 || (elf_bad_symtab (input_bfd)
11225 && flinfo->sections[r_symndx] == NULL))
11226 {
11227 struct elf_link_hash_entry *rh;
11228 unsigned long indx;
11229
11230 /* This is a reloc against a global symbol. We
11231 have not yet output all the local symbols, so
11232 we do not know the symbol index of any global
11233 symbol. We set the rel_hash entry for this
11234 reloc to point to the global hash table entry
11235 for this symbol. The symbol index is then
11236 set at the end of bfd_elf_final_link. */
11237 indx = r_symndx - extsymoff;
11238 rh = elf_sym_hashes (input_bfd)[indx];
11239 while (rh->root.type == bfd_link_hash_indirect
11240 || rh->root.type == bfd_link_hash_warning)
11241 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11242
11243 /* Setting the index to -2 tells
11244 elf_link_output_extsym that this symbol is
11245 used by a reloc. */
11246 BFD_ASSERT (rh->indx < 0);
11247 rh->indx = -2;
11248 *rel_hash = rh;
11249
11250 continue;
11251 }
11252
11253 /* This is a reloc against a local symbol. */
11254
11255 *rel_hash = NULL;
11256 sym = isymbuf[r_symndx];
11257 sec = flinfo->sections[r_symndx];
11258 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11259 {
11260 /* I suppose the backend ought to fill in the
11261 section of any STT_SECTION symbol against a
11262 processor specific section. */
11263 r_symndx = STN_UNDEF;
11264 if (bfd_is_abs_section (sec))
11265 ;
11266 else if (sec == NULL || sec->owner == NULL)
11267 {
11268 bfd_set_error (bfd_error_bad_value);
11269 return FALSE;
11270 }
11271 else
11272 {
11273 asection *osec = sec->output_section;
11274
11275 /* If we have discarded a section, the output
11276 section will be the absolute section. In
11277 case of discarded SEC_MERGE sections, use
11278 the kept section. relocate_section should
11279 have already handled discarded linkonce
11280 sections. */
11281 if (bfd_is_abs_section (osec)
11282 && sec->kept_section != NULL
11283 && sec->kept_section->output_section != NULL)
11284 {
11285 osec = sec->kept_section->output_section;
11286 irela->r_addend -= osec->vma;
11287 }
11288
11289 if (!bfd_is_abs_section (osec))
11290 {
11291 r_symndx = osec->target_index;
11292 if (r_symndx == STN_UNDEF)
11293 {
11294 irela->r_addend += osec->vma;
11295 osec = _bfd_nearby_section (output_bfd, osec,
11296 osec->vma);
11297 irela->r_addend -= osec->vma;
11298 r_symndx = osec->target_index;
11299 }
11300 }
11301 }
11302
11303 /* Adjust the addend according to where the
11304 section winds up in the output section. */
11305 if (rela_normal)
11306 irela->r_addend += sec->output_offset;
11307 }
11308 else
11309 {
11310 if (flinfo->indices[r_symndx] == -1)
11311 {
11312 unsigned long shlink;
11313 const char *name;
11314 asection *osec;
11315 long indx;
11316
11317 if (flinfo->info->strip == strip_all)
11318 {
11319 /* You can't do ld -r -s. */
11320 bfd_set_error (bfd_error_invalid_operation);
11321 return FALSE;
11322 }
11323
11324 /* This symbol was skipped earlier, but
11325 since it is needed by a reloc, we
11326 must output it now. */
11327 shlink = symtab_hdr->sh_link;
11328 name = (bfd_elf_string_from_elf_section
11329 (input_bfd, shlink, sym.st_name));
11330 if (name == NULL)
11331 return FALSE;
11332
11333 osec = sec->output_section;
11334 sym.st_shndx =
11335 _bfd_elf_section_from_bfd_section (output_bfd,
11336 osec);
11337 if (sym.st_shndx == SHN_BAD)
11338 return FALSE;
11339
11340 sym.st_value += sec->output_offset;
11341 if (!bfd_link_relocatable (flinfo->info))
11342 {
11343 sym.st_value += osec->vma;
11344 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11345 {
11346 struct elf_link_hash_table *htab
11347 = elf_hash_table (flinfo->info);
11348
11349 /* STT_TLS symbols are relative to PT_TLS
11350 segment base. */
11351 if (htab->tls_sec != NULL)
11352 sym.st_value -= htab->tls_sec->vma;
11353 else
11354 sym.st_info
11355 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11356 STT_NOTYPE);
11357 }
11358 }
11359
11360 indx = bfd_get_symcount (output_bfd);
11361 ret = elf_link_output_symstrtab (flinfo, name,
11362 &sym, sec,
11363 NULL);
11364 if (ret == 0)
11365 return FALSE;
11366 else if (ret == 1)
11367 flinfo->indices[r_symndx] = indx;
11368 else
11369 abort ();
11370 }
11371
11372 r_symndx = flinfo->indices[r_symndx];
11373 }
11374
11375 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11376 | (irela->r_info & r_type_mask));
11377 }
11378
11379 /* Swap out the relocs. */
11380 input_rel_hdr = esdi->rel.hdr;
11381 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11382 {
11383 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11384 input_rel_hdr,
11385 internal_relocs,
11386 rel_hash_list))
11387 return FALSE;
11388 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11389 * bed->s->int_rels_per_ext_rel);
11390 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11391 }
11392
11393 input_rela_hdr = esdi->rela.hdr;
11394 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11395 {
11396 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11397 input_rela_hdr,
11398 internal_relocs,
11399 rela_hash_list))
11400 return FALSE;
11401 }
11402 }
11403 }
11404
11405 /* Write out the modified section contents. */
11406 if (bed->elf_backend_write_section
11407 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11408 contents))
11409 {
11410 /* Section written out. */
11411 }
11412 else switch (o->sec_info_type)
11413 {
11414 case SEC_INFO_TYPE_STABS:
11415 if (! (_bfd_write_section_stabs
11416 (output_bfd,
11417 &elf_hash_table (flinfo->info)->stab_info,
11418 o, &elf_section_data (o)->sec_info, contents)))
11419 return FALSE;
11420 break;
11421 case SEC_INFO_TYPE_MERGE:
11422 if (! _bfd_write_merged_section (output_bfd, o,
11423 elf_section_data (o)->sec_info))
11424 return FALSE;
11425 break;
11426 case SEC_INFO_TYPE_EH_FRAME:
11427 {
11428 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11429 o, contents))
11430 return FALSE;
11431 }
11432 break;
11433 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11434 {
11435 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11436 flinfo->info,
11437 o, contents))
11438 return FALSE;
11439 }
11440 break;
11441 default:
11442 {
11443 if (! (o->flags & SEC_EXCLUDE))
11444 {
11445 file_ptr offset = (file_ptr) o->output_offset;
11446 bfd_size_type todo = o->size;
11447
11448 offset *= bfd_octets_per_byte (output_bfd, o);
11449
11450 if ((o->flags & SEC_ELF_REVERSE_COPY))
11451 {
11452 /* Reverse-copy input section to output. */
11453 do
11454 {
11455 todo -= address_size;
11456 if (! bfd_set_section_contents (output_bfd,
11457 o->output_section,
11458 contents + todo,
11459 offset,
11460 address_size))
11461 return FALSE;
11462 if (todo == 0)
11463 break;
11464 offset += address_size;
11465 }
11466 while (1);
11467 }
11468 else if (! bfd_set_section_contents (output_bfd,
11469 o->output_section,
11470 contents,
11471 offset, todo))
11472 return FALSE;
11473 }
11474 }
11475 break;
11476 }
11477 }
11478
11479 return TRUE;
11480 }
11481
11482 /* Generate a reloc when linking an ELF file. This is a reloc
11483 requested by the linker, and does not come from any input file. This
11484 is used to build constructor and destructor tables when linking
11485 with -Ur. */
11486
11487 static bfd_boolean
11488 elf_reloc_link_order (bfd *output_bfd,
11489 struct bfd_link_info *info,
11490 asection *output_section,
11491 struct bfd_link_order *link_order)
11492 {
11493 reloc_howto_type *howto;
11494 long indx;
11495 bfd_vma offset;
11496 bfd_vma addend;
11497 struct bfd_elf_section_reloc_data *reldata;
11498 struct elf_link_hash_entry **rel_hash_ptr;
11499 Elf_Internal_Shdr *rel_hdr;
11500 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11501 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11502 bfd_byte *erel;
11503 unsigned int i;
11504 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11505
11506 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11507 if (howto == NULL)
11508 {
11509 bfd_set_error (bfd_error_bad_value);
11510 return FALSE;
11511 }
11512
11513 addend = link_order->u.reloc.p->addend;
11514
11515 if (esdo->rel.hdr)
11516 reldata = &esdo->rel;
11517 else if (esdo->rela.hdr)
11518 reldata = &esdo->rela;
11519 else
11520 {
11521 reldata = NULL;
11522 BFD_ASSERT (0);
11523 }
11524
11525 /* Figure out the symbol index. */
11526 rel_hash_ptr = reldata->hashes + reldata->count;
11527 if (link_order->type == bfd_section_reloc_link_order)
11528 {
11529 indx = link_order->u.reloc.p->u.section->target_index;
11530 BFD_ASSERT (indx != 0);
11531 *rel_hash_ptr = NULL;
11532 }
11533 else
11534 {
11535 struct elf_link_hash_entry *h;
11536
11537 /* Treat a reloc against a defined symbol as though it were
11538 actually against the section. */
11539 h = ((struct elf_link_hash_entry *)
11540 bfd_wrapped_link_hash_lookup (output_bfd, info,
11541 link_order->u.reloc.p->u.name,
11542 FALSE, FALSE, TRUE));
11543 if (h != NULL
11544 && (h->root.type == bfd_link_hash_defined
11545 || h->root.type == bfd_link_hash_defweak))
11546 {
11547 asection *section;
11548
11549 section = h->root.u.def.section;
11550 indx = section->output_section->target_index;
11551 *rel_hash_ptr = NULL;
11552 /* It seems that we ought to add the symbol value to the
11553 addend here, but in practice it has already been added
11554 because it was passed to constructor_callback. */
11555 addend += section->output_section->vma + section->output_offset;
11556 }
11557 else if (h != NULL)
11558 {
11559 /* Setting the index to -2 tells elf_link_output_extsym that
11560 this symbol is used by a reloc. */
11561 h->indx = -2;
11562 *rel_hash_ptr = h;
11563 indx = 0;
11564 }
11565 else
11566 {
11567 (*info->callbacks->unattached_reloc)
11568 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11569 indx = 0;
11570 }
11571 }
11572
11573 /* If this is an inplace reloc, we must write the addend into the
11574 object file. */
11575 if (howto->partial_inplace && addend != 0)
11576 {
11577 bfd_size_type size;
11578 bfd_reloc_status_type rstat;
11579 bfd_byte *buf;
11580 bfd_boolean ok;
11581 const char *sym_name;
11582 bfd_size_type octets;
11583
11584 size = (bfd_size_type) bfd_get_reloc_size (howto);
11585 buf = (bfd_byte *) bfd_zmalloc (size);
11586 if (buf == NULL && size != 0)
11587 return FALSE;
11588 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11589 switch (rstat)
11590 {
11591 case bfd_reloc_ok:
11592 break;
11593
11594 default:
11595 case bfd_reloc_outofrange:
11596 abort ();
11597
11598 case bfd_reloc_overflow:
11599 if (link_order->type == bfd_section_reloc_link_order)
11600 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11601 else
11602 sym_name = link_order->u.reloc.p->u.name;
11603 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11604 howto->name, addend, NULL, NULL,
11605 (bfd_vma) 0);
11606 break;
11607 }
11608
11609 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11610 output_section);
11611 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11612 octets, size);
11613 free (buf);
11614 if (! ok)
11615 return FALSE;
11616 }
11617
11618 /* The address of a reloc is relative to the section in a
11619 relocatable file, and is a virtual address in an executable
11620 file. */
11621 offset = link_order->offset;
11622 if (! bfd_link_relocatable (info))
11623 offset += output_section->vma;
11624
11625 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11626 {
11627 irel[i].r_offset = offset;
11628 irel[i].r_info = 0;
11629 irel[i].r_addend = 0;
11630 }
11631 if (bed->s->arch_size == 32)
11632 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11633 else
11634 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11635
11636 rel_hdr = reldata->hdr;
11637 erel = rel_hdr->contents;
11638 if (rel_hdr->sh_type == SHT_REL)
11639 {
11640 erel += reldata->count * bed->s->sizeof_rel;
11641 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11642 }
11643 else
11644 {
11645 irel[0].r_addend = addend;
11646 erel += reldata->count * bed->s->sizeof_rela;
11647 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11648 }
11649
11650 ++reldata->count;
11651
11652 return TRUE;
11653 }
11654
11655
11656 /* Compare two sections based on the locations of the sections they are
11657 linked to. Used by elf_fixup_link_order. */
11658
11659 static int
11660 compare_link_order (const void *a, const void *b)
11661 {
11662 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11663 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11664 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11665 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11666 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11667 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11668
11669 if (apos < bpos)
11670 return -1;
11671 if (apos > bpos)
11672 return 1;
11673
11674 /* The only way we should get matching LMAs is when the first of two
11675 sections has zero size. */
11676 if (asec->size < bsec->size)
11677 return -1;
11678 if (asec->size > bsec->size)
11679 return 1;
11680
11681 /* If they are both zero size then they almost certainly have the same
11682 VMA and thus are not ordered with respect to each other. Test VMA
11683 anyway, and fall back to id to make the result reproducible across
11684 qsort implementations. */
11685 apos = asec->output_section->vma + asec->output_offset;
11686 bpos = bsec->output_section->vma + bsec->output_offset;
11687 if (apos < bpos)
11688 return -1;
11689 if (apos > bpos)
11690 return 1;
11691
11692 return asec->id - bsec->id;
11693 }
11694
11695
11696 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11697 order as their linked sections. Returns false if this could not be done
11698 because an output section includes both ordered and unordered
11699 sections. Ideally we'd do this in the linker proper. */
11700
11701 static bfd_boolean
11702 elf_fixup_link_order (bfd *abfd, asection *o)
11703 {
11704 size_t seen_linkorder;
11705 size_t seen_other;
11706 size_t n;
11707 struct bfd_link_order *p;
11708 bfd *sub;
11709 struct bfd_link_order **sections;
11710 asection *other_sec, *linkorder_sec;
11711 bfd_vma offset; /* Octets. */
11712
11713 other_sec = NULL;
11714 linkorder_sec = NULL;
11715 seen_other = 0;
11716 seen_linkorder = 0;
11717 for (p = o->map_head.link_order; p != NULL; p = p->next)
11718 {
11719 if (p->type == bfd_indirect_link_order)
11720 {
11721 asection *s = p->u.indirect.section;
11722 sub = s->owner;
11723 if ((s->flags & SEC_LINKER_CREATED) == 0
11724 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11725 && elf_section_data (s) != NULL
11726 && elf_linked_to_section (s) != NULL)
11727 {
11728 seen_linkorder++;
11729 linkorder_sec = s;
11730 }
11731 else
11732 {
11733 seen_other++;
11734 other_sec = s;
11735 }
11736 }
11737 else
11738 seen_other++;
11739
11740 if (seen_other && seen_linkorder)
11741 {
11742 if (other_sec && linkorder_sec)
11743 _bfd_error_handler
11744 /* xgettext:c-format */
11745 (_("%pA has both ordered [`%pA' in %pB] "
11746 "and unordered [`%pA' in %pB] sections"),
11747 o, linkorder_sec, linkorder_sec->owner,
11748 other_sec, other_sec->owner);
11749 else
11750 _bfd_error_handler
11751 (_("%pA has both ordered and unordered sections"), o);
11752 bfd_set_error (bfd_error_bad_value);
11753 return FALSE;
11754 }
11755 }
11756
11757 if (!seen_linkorder)
11758 return TRUE;
11759
11760 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11761 if (sections == NULL)
11762 return FALSE;
11763
11764 seen_linkorder = 0;
11765 for (p = o->map_head.link_order; p != NULL; p = p->next)
11766 sections[seen_linkorder++] = p;
11767
11768 /* Sort the input sections in the order of their linked section. */
11769 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11770
11771 /* Change the offsets of the sections. */
11772 offset = 0;
11773 for (n = 0; n < seen_linkorder; n++)
11774 {
11775 bfd_vma mask;
11776 asection *s = sections[n]->u.indirect.section;
11777 unsigned int opb = bfd_octets_per_byte (abfd, s);
11778
11779 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11780 offset = (offset + ~mask) & mask;
11781 sections[n]->offset = s->output_offset = offset / opb;
11782 offset += sections[n]->size;
11783 }
11784
11785 free (sections);
11786 return TRUE;
11787 }
11788
11789 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11790 Returns TRUE upon success, FALSE otherwise. */
11791
11792 static bfd_boolean
11793 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11794 {
11795 bfd_boolean ret = FALSE;
11796 bfd *implib_bfd;
11797 const struct elf_backend_data *bed;
11798 flagword flags;
11799 enum bfd_architecture arch;
11800 unsigned int mach;
11801 asymbol **sympp = NULL;
11802 long symsize;
11803 long symcount;
11804 long src_count;
11805 elf_symbol_type *osymbuf;
11806 size_t amt;
11807
11808 implib_bfd = info->out_implib_bfd;
11809 bed = get_elf_backend_data (abfd);
11810
11811 if (!bfd_set_format (implib_bfd, bfd_object))
11812 return FALSE;
11813
11814 /* Use flag from executable but make it a relocatable object. */
11815 flags = bfd_get_file_flags (abfd);
11816 flags &= ~HAS_RELOC;
11817 if (!bfd_set_start_address (implib_bfd, 0)
11818 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11819 return FALSE;
11820
11821 /* Copy architecture of output file to import library file. */
11822 arch = bfd_get_arch (abfd);
11823 mach = bfd_get_mach (abfd);
11824 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11825 && (abfd->target_defaulted
11826 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11827 return FALSE;
11828
11829 /* Get symbol table size. */
11830 symsize = bfd_get_symtab_upper_bound (abfd);
11831 if (symsize < 0)
11832 return FALSE;
11833
11834 /* Read in the symbol table. */
11835 sympp = (asymbol **) bfd_malloc (symsize);
11836 if (sympp == NULL)
11837 return FALSE;
11838
11839 symcount = bfd_canonicalize_symtab (abfd, sympp);
11840 if (symcount < 0)
11841 goto free_sym_buf;
11842
11843 /* Allow the BFD backend to copy any private header data it
11844 understands from the output BFD to the import library BFD. */
11845 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11846 goto free_sym_buf;
11847
11848 /* Filter symbols to appear in the import library. */
11849 if (bed->elf_backend_filter_implib_symbols)
11850 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11851 symcount);
11852 else
11853 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11854 if (symcount == 0)
11855 {
11856 bfd_set_error (bfd_error_no_symbols);
11857 _bfd_error_handler (_("%pB: no symbol found for import library"),
11858 implib_bfd);
11859 goto free_sym_buf;
11860 }
11861
11862
11863 /* Make symbols absolute. */
11864 amt = symcount * sizeof (*osymbuf);
11865 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11866 if (osymbuf == NULL)
11867 goto free_sym_buf;
11868
11869 for (src_count = 0; src_count < symcount; src_count++)
11870 {
11871 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11872 sizeof (*osymbuf));
11873 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11874 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11875 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11876 osymbuf[src_count].internal_elf_sym.st_value =
11877 osymbuf[src_count].symbol.value;
11878 sympp[src_count] = &osymbuf[src_count].symbol;
11879 }
11880
11881 bfd_set_symtab (implib_bfd, sympp, symcount);
11882
11883 /* Allow the BFD backend to copy any private data it understands
11884 from the output BFD to the import library BFD. This is done last
11885 to permit the routine to look at the filtered symbol table. */
11886 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11887 goto free_sym_buf;
11888
11889 if (!bfd_close (implib_bfd))
11890 goto free_sym_buf;
11891
11892 ret = TRUE;
11893
11894 free_sym_buf:
11895 free (sympp);
11896 return ret;
11897 }
11898
11899 static void
11900 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11901 {
11902 asection *o;
11903
11904 if (flinfo->symstrtab != NULL)
11905 _bfd_elf_strtab_free (flinfo->symstrtab);
11906 free (flinfo->contents);
11907 free (flinfo->external_relocs);
11908 free (flinfo->internal_relocs);
11909 free (flinfo->external_syms);
11910 free (flinfo->locsym_shndx);
11911 free (flinfo->internal_syms);
11912 free (flinfo->indices);
11913 free (flinfo->sections);
11914 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11915 free (flinfo->symshndxbuf);
11916 for (o = obfd->sections; o != NULL; o = o->next)
11917 {
11918 struct bfd_elf_section_data *esdo = elf_section_data (o);
11919 free (esdo->rel.hashes);
11920 free (esdo->rela.hashes);
11921 }
11922 }
11923
11924 /* Do the final step of an ELF link. */
11925
11926 bfd_boolean
11927 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11928 {
11929 bfd_boolean dynamic;
11930 bfd_boolean emit_relocs;
11931 bfd *dynobj;
11932 struct elf_final_link_info flinfo;
11933 asection *o;
11934 struct bfd_link_order *p;
11935 bfd *sub;
11936 bfd_size_type max_contents_size;
11937 bfd_size_type max_external_reloc_size;
11938 bfd_size_type max_internal_reloc_count;
11939 bfd_size_type max_sym_count;
11940 bfd_size_type max_sym_shndx_count;
11941 Elf_Internal_Sym elfsym;
11942 unsigned int i;
11943 Elf_Internal_Shdr *symtab_hdr;
11944 Elf_Internal_Shdr *symtab_shndx_hdr;
11945 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11946 struct elf_outext_info eoinfo;
11947 bfd_boolean merged;
11948 size_t relativecount = 0;
11949 asection *reldyn = 0;
11950 bfd_size_type amt;
11951 asection *attr_section = NULL;
11952 bfd_vma attr_size = 0;
11953 const char *std_attrs_section;
11954 struct elf_link_hash_table *htab = elf_hash_table (info);
11955 bfd_boolean sections_removed;
11956
11957 if (!is_elf_hash_table (htab))
11958 return FALSE;
11959
11960 if (bfd_link_pic (info))
11961 abfd->flags |= DYNAMIC;
11962
11963 dynamic = htab->dynamic_sections_created;
11964 dynobj = htab->dynobj;
11965
11966 emit_relocs = (bfd_link_relocatable (info)
11967 || info->emitrelocations);
11968
11969 flinfo.info = info;
11970 flinfo.output_bfd = abfd;
11971 flinfo.symstrtab = _bfd_elf_strtab_init ();
11972 if (flinfo.symstrtab == NULL)
11973 return FALSE;
11974
11975 if (! dynamic)
11976 {
11977 flinfo.hash_sec = NULL;
11978 flinfo.symver_sec = NULL;
11979 }
11980 else
11981 {
11982 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11983 /* Note that dynsym_sec can be NULL (on VMS). */
11984 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11985 /* Note that it is OK if symver_sec is NULL. */
11986 }
11987
11988 flinfo.contents = NULL;
11989 flinfo.external_relocs = NULL;
11990 flinfo.internal_relocs = NULL;
11991 flinfo.external_syms = NULL;
11992 flinfo.locsym_shndx = NULL;
11993 flinfo.internal_syms = NULL;
11994 flinfo.indices = NULL;
11995 flinfo.sections = NULL;
11996 flinfo.symshndxbuf = NULL;
11997 flinfo.filesym_count = 0;
11998
11999 /* The object attributes have been merged. Remove the input
12000 sections from the link, and set the contents of the output
12001 section. */
12002 sections_removed = FALSE;
12003 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12004 for (o = abfd->sections; o != NULL; o = o->next)
12005 {
12006 bfd_boolean remove_section = FALSE;
12007
12008 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12009 || strcmp (o->name, ".gnu.attributes") == 0)
12010 {
12011 for (p = o->map_head.link_order; p != NULL; p = p->next)
12012 {
12013 asection *input_section;
12014
12015 if (p->type != bfd_indirect_link_order)
12016 continue;
12017 input_section = p->u.indirect.section;
12018 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12019 elf_link_input_bfd ignores this section. */
12020 input_section->flags &= ~SEC_HAS_CONTENTS;
12021 }
12022
12023 attr_size = bfd_elf_obj_attr_size (abfd);
12024 bfd_set_section_size (o, attr_size);
12025 /* Skip this section later on. */
12026 o->map_head.link_order = NULL;
12027 if (attr_size)
12028 attr_section = o;
12029 else
12030 remove_section = TRUE;
12031 }
12032 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12033 {
12034 /* Remove empty group section from linker output. */
12035 remove_section = TRUE;
12036 }
12037 if (remove_section)
12038 {
12039 o->flags |= SEC_EXCLUDE;
12040 bfd_section_list_remove (abfd, o);
12041 abfd->section_count--;
12042 sections_removed = TRUE;
12043 }
12044 }
12045 if (sections_removed)
12046 _bfd_fix_excluded_sec_syms (abfd, info);
12047
12048 /* Count up the number of relocations we will output for each output
12049 section, so that we know the sizes of the reloc sections. We
12050 also figure out some maximum sizes. */
12051 max_contents_size = 0;
12052 max_external_reloc_size = 0;
12053 max_internal_reloc_count = 0;
12054 max_sym_count = 0;
12055 max_sym_shndx_count = 0;
12056 merged = FALSE;
12057 for (o = abfd->sections; o != NULL; o = o->next)
12058 {
12059 struct bfd_elf_section_data *esdo = elf_section_data (o);
12060 o->reloc_count = 0;
12061
12062 for (p = o->map_head.link_order; p != NULL; p = p->next)
12063 {
12064 unsigned int reloc_count = 0;
12065 unsigned int additional_reloc_count = 0;
12066 struct bfd_elf_section_data *esdi = NULL;
12067
12068 if (p->type == bfd_section_reloc_link_order
12069 || p->type == bfd_symbol_reloc_link_order)
12070 reloc_count = 1;
12071 else if (p->type == bfd_indirect_link_order)
12072 {
12073 asection *sec;
12074
12075 sec = p->u.indirect.section;
12076
12077 /* Mark all sections which are to be included in the
12078 link. This will normally be every section. We need
12079 to do this so that we can identify any sections which
12080 the linker has decided to not include. */
12081 sec->linker_mark = TRUE;
12082
12083 if (sec->flags & SEC_MERGE)
12084 merged = TRUE;
12085
12086 if (sec->rawsize > max_contents_size)
12087 max_contents_size = sec->rawsize;
12088 if (sec->size > max_contents_size)
12089 max_contents_size = sec->size;
12090
12091 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12092 && (sec->owner->flags & DYNAMIC) == 0)
12093 {
12094 size_t sym_count;
12095
12096 /* We are interested in just local symbols, not all
12097 symbols. */
12098 if (elf_bad_symtab (sec->owner))
12099 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12100 / bed->s->sizeof_sym);
12101 else
12102 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12103
12104 if (sym_count > max_sym_count)
12105 max_sym_count = sym_count;
12106
12107 if (sym_count > max_sym_shndx_count
12108 && elf_symtab_shndx_list (sec->owner) != NULL)
12109 max_sym_shndx_count = sym_count;
12110
12111 if (esdo->this_hdr.sh_type == SHT_REL
12112 || esdo->this_hdr.sh_type == SHT_RELA)
12113 /* Some backends use reloc_count in relocation sections
12114 to count particular types of relocs. Of course,
12115 reloc sections themselves can't have relocations. */
12116 ;
12117 else if (emit_relocs)
12118 {
12119 reloc_count = sec->reloc_count;
12120 if (bed->elf_backend_count_additional_relocs)
12121 {
12122 int c;
12123 c = (*bed->elf_backend_count_additional_relocs) (sec);
12124 additional_reloc_count += c;
12125 }
12126 }
12127 else if (bed->elf_backend_count_relocs)
12128 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12129
12130 esdi = elf_section_data (sec);
12131
12132 if ((sec->flags & SEC_RELOC) != 0)
12133 {
12134 size_t ext_size = 0;
12135
12136 if (esdi->rel.hdr != NULL)
12137 ext_size = esdi->rel.hdr->sh_size;
12138 if (esdi->rela.hdr != NULL)
12139 ext_size += esdi->rela.hdr->sh_size;
12140
12141 if (ext_size > max_external_reloc_size)
12142 max_external_reloc_size = ext_size;
12143 if (sec->reloc_count > max_internal_reloc_count)
12144 max_internal_reloc_count = sec->reloc_count;
12145 }
12146 }
12147 }
12148
12149 if (reloc_count == 0)
12150 continue;
12151
12152 reloc_count += additional_reloc_count;
12153 o->reloc_count += reloc_count;
12154
12155 if (p->type == bfd_indirect_link_order && emit_relocs)
12156 {
12157 if (esdi->rel.hdr)
12158 {
12159 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12160 esdo->rel.count += additional_reloc_count;
12161 }
12162 if (esdi->rela.hdr)
12163 {
12164 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12165 esdo->rela.count += additional_reloc_count;
12166 }
12167 }
12168 else
12169 {
12170 if (o->use_rela_p)
12171 esdo->rela.count += reloc_count;
12172 else
12173 esdo->rel.count += reloc_count;
12174 }
12175 }
12176
12177 if (o->reloc_count > 0)
12178 o->flags |= SEC_RELOC;
12179 else
12180 {
12181 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12182 set it (this is probably a bug) and if it is set
12183 assign_section_numbers will create a reloc section. */
12184 o->flags &=~ SEC_RELOC;
12185 }
12186
12187 /* If the SEC_ALLOC flag is not set, force the section VMA to
12188 zero. This is done in elf_fake_sections as well, but forcing
12189 the VMA to 0 here will ensure that relocs against these
12190 sections are handled correctly. */
12191 if ((o->flags & SEC_ALLOC) == 0
12192 && ! o->user_set_vma)
12193 o->vma = 0;
12194 }
12195
12196 if (! bfd_link_relocatable (info) && merged)
12197 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12198
12199 /* Figure out the file positions for everything but the symbol table
12200 and the relocs. We set symcount to force assign_section_numbers
12201 to create a symbol table. */
12202 abfd->symcount = info->strip != strip_all || emit_relocs;
12203 BFD_ASSERT (! abfd->output_has_begun);
12204 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12205 goto error_return;
12206
12207 /* Set sizes, and assign file positions for reloc sections. */
12208 for (o = abfd->sections; o != NULL; o = o->next)
12209 {
12210 struct bfd_elf_section_data *esdo = elf_section_data (o);
12211 if ((o->flags & SEC_RELOC) != 0)
12212 {
12213 if (esdo->rel.hdr
12214 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12215 goto error_return;
12216
12217 if (esdo->rela.hdr
12218 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12219 goto error_return;
12220 }
12221
12222 /* _bfd_elf_compute_section_file_positions makes temporary use
12223 of target_index. Reset it. */
12224 o->target_index = 0;
12225
12226 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12227 to count upwards while actually outputting the relocations. */
12228 esdo->rel.count = 0;
12229 esdo->rela.count = 0;
12230
12231 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12232 && !bfd_section_is_ctf (o))
12233 {
12234 /* Cache the section contents so that they can be compressed
12235 later. Use bfd_malloc since it will be freed by
12236 bfd_compress_section_contents. */
12237 unsigned char *contents = esdo->this_hdr.contents;
12238 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12239 abort ();
12240 contents
12241 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12242 if (contents == NULL)
12243 goto error_return;
12244 esdo->this_hdr.contents = contents;
12245 }
12246 }
12247
12248 /* We have now assigned file positions for all the sections except .symtab,
12249 .strtab, and non-loaded reloc and compressed debugging sections. We start
12250 the .symtab section at the current file position, and write directly to it.
12251 We build the .strtab section in memory. */
12252 abfd->symcount = 0;
12253 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12254 /* sh_name is set in prep_headers. */
12255 symtab_hdr->sh_type = SHT_SYMTAB;
12256 /* sh_flags, sh_addr and sh_size all start off zero. */
12257 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12258 /* sh_link is set in assign_section_numbers. */
12259 /* sh_info is set below. */
12260 /* sh_offset is set just below. */
12261 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12262
12263 if (max_sym_count < 20)
12264 max_sym_count = 20;
12265 htab->strtabsize = max_sym_count;
12266 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12267 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12268 if (htab->strtab == NULL)
12269 goto error_return;
12270 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12271 flinfo.symshndxbuf
12272 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12273 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12274
12275 if (info->strip != strip_all || emit_relocs)
12276 {
12277 bfd_boolean name_local_sections;
12278 const char *name;
12279
12280 file_ptr off = elf_next_file_pos (abfd);
12281
12282 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12283
12284 /* Note that at this point elf_next_file_pos (abfd) is
12285 incorrect. We do not yet know the size of the .symtab section.
12286 We correct next_file_pos below, after we do know the size. */
12287
12288 /* Start writing out the symbol table. The first symbol is always a
12289 dummy symbol. */
12290 elfsym.st_value = 0;
12291 elfsym.st_size = 0;
12292 elfsym.st_info = 0;
12293 elfsym.st_other = 0;
12294 elfsym.st_shndx = SHN_UNDEF;
12295 elfsym.st_target_internal = 0;
12296 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12297 bfd_und_section_ptr, NULL) != 1)
12298 goto error_return;
12299
12300 /* Output a symbol for each section. We output these even if we are
12301 discarding local symbols, since they are used for relocs. These
12302 symbols usually have no names. We store the index of each one in
12303 the index field of the section, so that we can find it again when
12304 outputting relocs. */
12305
12306 name_local_sections
12307 = (bed->elf_backend_name_local_section_symbols
12308 && bed->elf_backend_name_local_section_symbols (abfd));
12309
12310 name = NULL;
12311 elfsym.st_size = 0;
12312 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12313 elfsym.st_other = 0;
12314 elfsym.st_value = 0;
12315 elfsym.st_target_internal = 0;
12316 for (i = 1; i < elf_numsections (abfd); i++)
12317 {
12318 o = bfd_section_from_elf_index (abfd, i);
12319 if (o != NULL)
12320 {
12321 o->target_index = bfd_get_symcount (abfd);
12322 elfsym.st_shndx = i;
12323 if (!bfd_link_relocatable (info))
12324 elfsym.st_value = o->vma;
12325 if (name_local_sections)
12326 name = o->name;
12327 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12328 NULL) != 1)
12329 goto error_return;
12330 }
12331 }
12332 }
12333
12334 /* On some targets like Irix 5 the symbol split between local and global
12335 ones recorded in the sh_info field needs to be done between section
12336 and all other symbols. */
12337 if (bed->elf_backend_elfsym_local_is_section
12338 && bed->elf_backend_elfsym_local_is_section (abfd))
12339 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12340
12341 /* Allocate some memory to hold information read in from the input
12342 files. */
12343 if (max_contents_size != 0)
12344 {
12345 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12346 if (flinfo.contents == NULL)
12347 goto error_return;
12348 }
12349
12350 if (max_external_reloc_size != 0)
12351 {
12352 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12353 if (flinfo.external_relocs == NULL)
12354 goto error_return;
12355 }
12356
12357 if (max_internal_reloc_count != 0)
12358 {
12359 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12360 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12361 if (flinfo.internal_relocs == NULL)
12362 goto error_return;
12363 }
12364
12365 if (max_sym_count != 0)
12366 {
12367 amt = max_sym_count * bed->s->sizeof_sym;
12368 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12369 if (flinfo.external_syms == NULL)
12370 goto error_return;
12371
12372 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12373 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12374 if (flinfo.internal_syms == NULL)
12375 goto error_return;
12376
12377 amt = max_sym_count * sizeof (long);
12378 flinfo.indices = (long int *) bfd_malloc (amt);
12379 if (flinfo.indices == NULL)
12380 goto error_return;
12381
12382 amt = max_sym_count * sizeof (asection *);
12383 flinfo.sections = (asection **) bfd_malloc (amt);
12384 if (flinfo.sections == NULL)
12385 goto error_return;
12386 }
12387
12388 if (max_sym_shndx_count != 0)
12389 {
12390 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12391 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12392 if (flinfo.locsym_shndx == NULL)
12393 goto error_return;
12394 }
12395
12396 if (htab->tls_sec)
12397 {
12398 bfd_vma base, end = 0; /* Both bytes. */
12399 asection *sec;
12400
12401 for (sec = htab->tls_sec;
12402 sec && (sec->flags & SEC_THREAD_LOCAL);
12403 sec = sec->next)
12404 {
12405 bfd_size_type size = sec->size;
12406 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12407
12408 if (size == 0
12409 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12410 {
12411 struct bfd_link_order *ord = sec->map_tail.link_order;
12412
12413 if (ord != NULL)
12414 size = ord->offset * opb + ord->size;
12415 }
12416 end = sec->vma + size / opb;
12417 }
12418 base = htab->tls_sec->vma;
12419 /* Only align end of TLS section if static TLS doesn't have special
12420 alignment requirements. */
12421 if (bed->static_tls_alignment == 1)
12422 end = align_power (end, htab->tls_sec->alignment_power);
12423 htab->tls_size = end - base;
12424 }
12425
12426 /* Reorder SHF_LINK_ORDER sections. */
12427 for (o = abfd->sections; o != NULL; o = o->next)
12428 {
12429 if (!elf_fixup_link_order (abfd, o))
12430 return FALSE;
12431 }
12432
12433 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12434 return FALSE;
12435
12436 /* Since ELF permits relocations to be against local symbols, we
12437 must have the local symbols available when we do the relocations.
12438 Since we would rather only read the local symbols once, and we
12439 would rather not keep them in memory, we handle all the
12440 relocations for a single input file at the same time.
12441
12442 Unfortunately, there is no way to know the total number of local
12443 symbols until we have seen all of them, and the local symbol
12444 indices precede the global symbol indices. This means that when
12445 we are generating relocatable output, and we see a reloc against
12446 a global symbol, we can not know the symbol index until we have
12447 finished examining all the local symbols to see which ones we are
12448 going to output. To deal with this, we keep the relocations in
12449 memory, and don't output them until the end of the link. This is
12450 an unfortunate waste of memory, but I don't see a good way around
12451 it. Fortunately, it only happens when performing a relocatable
12452 link, which is not the common case. FIXME: If keep_memory is set
12453 we could write the relocs out and then read them again; I don't
12454 know how bad the memory loss will be. */
12455
12456 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12457 sub->output_has_begun = FALSE;
12458 for (o = abfd->sections; o != NULL; o = o->next)
12459 {
12460 for (p = o->map_head.link_order; p != NULL; p = p->next)
12461 {
12462 if (p->type == bfd_indirect_link_order
12463 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12464 == bfd_target_elf_flavour)
12465 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12466 {
12467 if (! sub->output_has_begun)
12468 {
12469 if (! elf_link_input_bfd (&flinfo, sub))
12470 goto error_return;
12471 sub->output_has_begun = TRUE;
12472 }
12473 }
12474 else if (p->type == bfd_section_reloc_link_order
12475 || p->type == bfd_symbol_reloc_link_order)
12476 {
12477 if (! elf_reloc_link_order (abfd, info, o, p))
12478 goto error_return;
12479 }
12480 else
12481 {
12482 if (! _bfd_default_link_order (abfd, info, o, p))
12483 {
12484 if (p->type == bfd_indirect_link_order
12485 && (bfd_get_flavour (sub)
12486 == bfd_target_elf_flavour)
12487 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12488 != bed->s->elfclass))
12489 {
12490 const char *iclass, *oclass;
12491
12492 switch (bed->s->elfclass)
12493 {
12494 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12495 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12496 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12497 default: abort ();
12498 }
12499
12500 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12501 {
12502 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12503 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12504 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12505 default: abort ();
12506 }
12507
12508 bfd_set_error (bfd_error_wrong_format);
12509 _bfd_error_handler
12510 /* xgettext:c-format */
12511 (_("%pB: file class %s incompatible with %s"),
12512 sub, iclass, oclass);
12513 }
12514
12515 goto error_return;
12516 }
12517 }
12518 }
12519 }
12520
12521 /* Free symbol buffer if needed. */
12522 if (!info->reduce_memory_overheads)
12523 {
12524 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12525 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12526 {
12527 free (elf_tdata (sub)->symbuf);
12528 elf_tdata (sub)->symbuf = NULL;
12529 }
12530 }
12531
12532 /* Output any global symbols that got converted to local in a
12533 version script or due to symbol visibility. We do this in a
12534 separate step since ELF requires all local symbols to appear
12535 prior to any global symbols. FIXME: We should only do this if
12536 some global symbols were, in fact, converted to become local.
12537 FIXME: Will this work correctly with the Irix 5 linker? */
12538 eoinfo.failed = FALSE;
12539 eoinfo.flinfo = &flinfo;
12540 eoinfo.localsyms = TRUE;
12541 eoinfo.file_sym_done = FALSE;
12542 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12543 if (eoinfo.failed)
12544 return FALSE;
12545
12546 /* If backend needs to output some local symbols not present in the hash
12547 table, do it now. */
12548 if (bed->elf_backend_output_arch_local_syms
12549 && (info->strip != strip_all || emit_relocs))
12550 {
12551 typedef int (*out_sym_func)
12552 (void *, const char *, Elf_Internal_Sym *, asection *,
12553 struct elf_link_hash_entry *);
12554
12555 if (! ((*bed->elf_backend_output_arch_local_syms)
12556 (abfd, info, &flinfo,
12557 (out_sym_func) elf_link_output_symstrtab)))
12558 return FALSE;
12559 }
12560
12561 /* That wrote out all the local symbols. Finish up the symbol table
12562 with the global symbols. Even if we want to strip everything we
12563 can, we still need to deal with those global symbols that got
12564 converted to local in a version script. */
12565
12566 /* The sh_info field records the index of the first non local symbol. */
12567 if (!symtab_hdr->sh_info)
12568 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12569
12570 if (dynamic
12571 && htab->dynsym != NULL
12572 && htab->dynsym->output_section != bfd_abs_section_ptr)
12573 {
12574 Elf_Internal_Sym sym;
12575 bfd_byte *dynsym = htab->dynsym->contents;
12576
12577 o = htab->dynsym->output_section;
12578 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12579
12580 /* Write out the section symbols for the output sections. */
12581 if (bfd_link_pic (info)
12582 || htab->is_relocatable_executable)
12583 {
12584 asection *s;
12585
12586 sym.st_size = 0;
12587 sym.st_name = 0;
12588 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12589 sym.st_other = 0;
12590 sym.st_target_internal = 0;
12591
12592 for (s = abfd->sections; s != NULL; s = s->next)
12593 {
12594 int indx;
12595 bfd_byte *dest;
12596 long dynindx;
12597
12598 dynindx = elf_section_data (s)->dynindx;
12599 if (dynindx <= 0)
12600 continue;
12601 indx = elf_section_data (s)->this_idx;
12602 BFD_ASSERT (indx > 0);
12603 sym.st_shndx = indx;
12604 if (! check_dynsym (abfd, &sym))
12605 return FALSE;
12606 sym.st_value = s->vma;
12607 dest = dynsym + dynindx * bed->s->sizeof_sym;
12608 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12609 }
12610 }
12611
12612 /* Write out the local dynsyms. */
12613 if (htab->dynlocal)
12614 {
12615 struct elf_link_local_dynamic_entry *e;
12616 for (e = htab->dynlocal; e ; e = e->next)
12617 {
12618 asection *s;
12619 bfd_byte *dest;
12620
12621 /* Copy the internal symbol and turn off visibility.
12622 Note that we saved a word of storage and overwrote
12623 the original st_name with the dynstr_index. */
12624 sym = e->isym;
12625 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12626 sym.st_shndx = SHN_UNDEF;
12627
12628 s = bfd_section_from_elf_index (e->input_bfd,
12629 e->isym.st_shndx);
12630 if (s != NULL
12631 && s->output_section != NULL
12632 && elf_section_data (s->output_section) != NULL)
12633 {
12634 sym.st_shndx =
12635 elf_section_data (s->output_section)->this_idx;
12636 if (! check_dynsym (abfd, &sym))
12637 return FALSE;
12638 sym.st_value = (s->output_section->vma
12639 + s->output_offset
12640 + e->isym.st_value);
12641 }
12642
12643 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12644 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12645 }
12646 }
12647 }
12648
12649 /* We get the global symbols from the hash table. */
12650 eoinfo.failed = FALSE;
12651 eoinfo.localsyms = FALSE;
12652 eoinfo.flinfo = &flinfo;
12653 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12654 if (eoinfo.failed)
12655 return FALSE;
12656
12657 /* If backend needs to output some symbols not present in the hash
12658 table, do it now. */
12659 if (bed->elf_backend_output_arch_syms
12660 && (info->strip != strip_all || emit_relocs))
12661 {
12662 typedef int (*out_sym_func)
12663 (void *, const char *, Elf_Internal_Sym *, asection *,
12664 struct elf_link_hash_entry *);
12665
12666 if (! ((*bed->elf_backend_output_arch_syms)
12667 (abfd, info, &flinfo,
12668 (out_sym_func) elf_link_output_symstrtab)))
12669 return FALSE;
12670 }
12671
12672 /* Finalize the .strtab section. */
12673 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12674
12675 /* Swap out the .strtab section. */
12676 if (!elf_link_swap_symbols_out (&flinfo))
12677 return FALSE;
12678
12679 /* Now we know the size of the symtab section. */
12680 if (bfd_get_symcount (abfd) > 0)
12681 {
12682 /* Finish up and write out the symbol string table (.strtab)
12683 section. */
12684 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12685 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12686
12687 if (elf_symtab_shndx_list (abfd))
12688 {
12689 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12690
12691 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12692 {
12693 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12694 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12695 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12696 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12697 symtab_shndx_hdr->sh_size = amt;
12698
12699 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12700 off, TRUE);
12701
12702 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12703 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12704 return FALSE;
12705 }
12706 }
12707
12708 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12709 /* sh_name was set in prep_headers. */
12710 symstrtab_hdr->sh_type = SHT_STRTAB;
12711 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12712 symstrtab_hdr->sh_addr = 0;
12713 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12714 symstrtab_hdr->sh_entsize = 0;
12715 symstrtab_hdr->sh_link = 0;
12716 symstrtab_hdr->sh_info = 0;
12717 /* sh_offset is set just below. */
12718 symstrtab_hdr->sh_addralign = 1;
12719
12720 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12721 off, TRUE);
12722 elf_next_file_pos (abfd) = off;
12723
12724 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12725 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12726 return FALSE;
12727 }
12728
12729 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12730 {
12731 _bfd_error_handler (_("%pB: failed to generate import library"),
12732 info->out_implib_bfd);
12733 return FALSE;
12734 }
12735
12736 /* Adjust the relocs to have the correct symbol indices. */
12737 for (o = abfd->sections; o != NULL; o = o->next)
12738 {
12739 struct bfd_elf_section_data *esdo = elf_section_data (o);
12740 bfd_boolean sort;
12741
12742 if ((o->flags & SEC_RELOC) == 0)
12743 continue;
12744
12745 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12746 if (esdo->rel.hdr != NULL
12747 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12748 return FALSE;
12749 if (esdo->rela.hdr != NULL
12750 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12751 return FALSE;
12752
12753 /* Set the reloc_count field to 0 to prevent write_relocs from
12754 trying to swap the relocs out itself. */
12755 o->reloc_count = 0;
12756 }
12757
12758 if (dynamic && info->combreloc && dynobj != NULL)
12759 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12760
12761 /* If we are linking against a dynamic object, or generating a
12762 shared library, finish up the dynamic linking information. */
12763 if (dynamic)
12764 {
12765 bfd_byte *dyncon, *dynconend;
12766
12767 /* Fix up .dynamic entries. */
12768 o = bfd_get_linker_section (dynobj, ".dynamic");
12769 BFD_ASSERT (o != NULL);
12770
12771 dyncon = o->contents;
12772 dynconend = o->contents + o->size;
12773 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12774 {
12775 Elf_Internal_Dyn dyn;
12776 const char *name;
12777 unsigned int type;
12778 bfd_size_type sh_size;
12779 bfd_vma sh_addr;
12780
12781 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12782
12783 switch (dyn.d_tag)
12784 {
12785 default:
12786 continue;
12787 case DT_NULL:
12788 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12789 {
12790 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12791 {
12792 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12793 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12794 default: continue;
12795 }
12796 dyn.d_un.d_val = relativecount;
12797 relativecount = 0;
12798 break;
12799 }
12800 continue;
12801
12802 case DT_INIT:
12803 name = info->init_function;
12804 goto get_sym;
12805 case DT_FINI:
12806 name = info->fini_function;
12807 get_sym:
12808 {
12809 struct elf_link_hash_entry *h;
12810
12811 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12812 if (h != NULL
12813 && (h->root.type == bfd_link_hash_defined
12814 || h->root.type == bfd_link_hash_defweak))
12815 {
12816 dyn.d_un.d_ptr = h->root.u.def.value;
12817 o = h->root.u.def.section;
12818 if (o->output_section != NULL)
12819 dyn.d_un.d_ptr += (o->output_section->vma
12820 + o->output_offset);
12821 else
12822 {
12823 /* The symbol is imported from another shared
12824 library and does not apply to this one. */
12825 dyn.d_un.d_ptr = 0;
12826 }
12827 break;
12828 }
12829 }
12830 continue;
12831
12832 case DT_PREINIT_ARRAYSZ:
12833 name = ".preinit_array";
12834 goto get_out_size;
12835 case DT_INIT_ARRAYSZ:
12836 name = ".init_array";
12837 goto get_out_size;
12838 case DT_FINI_ARRAYSZ:
12839 name = ".fini_array";
12840 get_out_size:
12841 o = bfd_get_section_by_name (abfd, name);
12842 if (o == NULL)
12843 {
12844 _bfd_error_handler
12845 (_("could not find section %s"), name);
12846 goto error_return;
12847 }
12848 if (o->size == 0)
12849 _bfd_error_handler
12850 (_("warning: %s section has zero size"), name);
12851 dyn.d_un.d_val = o->size;
12852 break;
12853
12854 case DT_PREINIT_ARRAY:
12855 name = ".preinit_array";
12856 goto get_out_vma;
12857 case DT_INIT_ARRAY:
12858 name = ".init_array";
12859 goto get_out_vma;
12860 case DT_FINI_ARRAY:
12861 name = ".fini_array";
12862 get_out_vma:
12863 o = bfd_get_section_by_name (abfd, name);
12864 goto do_vma;
12865
12866 case DT_HASH:
12867 name = ".hash";
12868 goto get_vma;
12869 case DT_GNU_HASH:
12870 name = ".gnu.hash";
12871 goto get_vma;
12872 case DT_STRTAB:
12873 name = ".dynstr";
12874 goto get_vma;
12875 case DT_SYMTAB:
12876 name = ".dynsym";
12877 goto get_vma;
12878 case DT_VERDEF:
12879 name = ".gnu.version_d";
12880 goto get_vma;
12881 case DT_VERNEED:
12882 name = ".gnu.version_r";
12883 goto get_vma;
12884 case DT_VERSYM:
12885 name = ".gnu.version";
12886 get_vma:
12887 o = bfd_get_linker_section (dynobj, name);
12888 do_vma:
12889 if (o == NULL || bfd_is_abs_section (o->output_section))
12890 {
12891 _bfd_error_handler
12892 (_("could not find section %s"), name);
12893 goto error_return;
12894 }
12895 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12896 {
12897 _bfd_error_handler
12898 (_("warning: section '%s' is being made into a note"), name);
12899 bfd_set_error (bfd_error_nonrepresentable_section);
12900 goto error_return;
12901 }
12902 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12903 break;
12904
12905 case DT_REL:
12906 case DT_RELA:
12907 case DT_RELSZ:
12908 case DT_RELASZ:
12909 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12910 type = SHT_REL;
12911 else
12912 type = SHT_RELA;
12913 sh_size = 0;
12914 sh_addr = 0;
12915 for (i = 1; i < elf_numsections (abfd); i++)
12916 {
12917 Elf_Internal_Shdr *hdr;
12918
12919 hdr = elf_elfsections (abfd)[i];
12920 if (hdr->sh_type == type
12921 && (hdr->sh_flags & SHF_ALLOC) != 0)
12922 {
12923 sh_size += hdr->sh_size;
12924 if (sh_addr == 0
12925 || sh_addr > hdr->sh_addr)
12926 sh_addr = hdr->sh_addr;
12927 }
12928 }
12929
12930 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12931 {
12932 unsigned int opb = bfd_octets_per_byte (abfd, o);
12933
12934 /* Don't count procedure linkage table relocs in the
12935 overall reloc count. */
12936 sh_size -= htab->srelplt->size;
12937 if (sh_size == 0)
12938 /* If the size is zero, make the address zero too.
12939 This is to avoid a glibc bug. If the backend
12940 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12941 zero, then we'll put DT_RELA at the end of
12942 DT_JMPREL. glibc will interpret the end of
12943 DT_RELA matching the end of DT_JMPREL as the
12944 case where DT_RELA includes DT_JMPREL, and for
12945 LD_BIND_NOW will decide that processing DT_RELA
12946 will process the PLT relocs too. Net result:
12947 No PLT relocs applied. */
12948 sh_addr = 0;
12949
12950 /* If .rela.plt is the first .rela section, exclude
12951 it from DT_RELA. */
12952 else if (sh_addr == (htab->srelplt->output_section->vma
12953 + htab->srelplt->output_offset) * opb)
12954 sh_addr += htab->srelplt->size;
12955 }
12956
12957 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12958 dyn.d_un.d_val = sh_size;
12959 else
12960 dyn.d_un.d_ptr = sh_addr;
12961 break;
12962 }
12963 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12964 }
12965 }
12966
12967 /* If we have created any dynamic sections, then output them. */
12968 if (dynobj != NULL)
12969 {
12970 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12971 goto error_return;
12972
12973 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12974 if (bfd_link_textrel_check (info)
12975 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12976 {
12977 bfd_byte *dyncon, *dynconend;
12978
12979 dyncon = o->contents;
12980 dynconend = o->contents + o->size;
12981 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12982 {
12983 Elf_Internal_Dyn dyn;
12984
12985 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12986
12987 if (dyn.d_tag == DT_TEXTREL)
12988 {
12989 if (info->textrel_check == textrel_check_error)
12990 info->callbacks->einfo
12991 (_("%P%X: read-only segment has dynamic relocations\n"));
12992 else if (bfd_link_dll (info))
12993 info->callbacks->einfo
12994 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
12995 else
12996 info->callbacks->einfo
12997 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
12998 break;
12999 }
13000 }
13001 }
13002
13003 for (o = dynobj->sections; o != NULL; o = o->next)
13004 {
13005 if ((o->flags & SEC_HAS_CONTENTS) == 0
13006 || o->size == 0
13007 || o->output_section == bfd_abs_section_ptr)
13008 continue;
13009 if ((o->flags & SEC_LINKER_CREATED) == 0)
13010 {
13011 /* At this point, we are only interested in sections
13012 created by _bfd_elf_link_create_dynamic_sections. */
13013 continue;
13014 }
13015 if (htab->stab_info.stabstr == o)
13016 continue;
13017 if (htab->eh_info.hdr_sec == o)
13018 continue;
13019 if (strcmp (o->name, ".dynstr") != 0)
13020 {
13021 bfd_size_type octets = ((file_ptr) o->output_offset
13022 * bfd_octets_per_byte (abfd, o));
13023 if (!bfd_set_section_contents (abfd, o->output_section,
13024 o->contents, octets, o->size))
13025 goto error_return;
13026 }
13027 else
13028 {
13029 /* The contents of the .dynstr section are actually in a
13030 stringtab. */
13031 file_ptr off;
13032
13033 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13034 if (bfd_seek (abfd, off, SEEK_SET) != 0
13035 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13036 goto error_return;
13037 }
13038 }
13039 }
13040
13041 if (!info->resolve_section_groups)
13042 {
13043 bfd_boolean failed = FALSE;
13044
13045 BFD_ASSERT (bfd_link_relocatable (info));
13046 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13047 if (failed)
13048 goto error_return;
13049 }
13050
13051 /* If we have optimized stabs strings, output them. */
13052 if (htab->stab_info.stabstr != NULL)
13053 {
13054 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13055 goto error_return;
13056 }
13057
13058 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13059 goto error_return;
13060
13061 if (info->callbacks->emit_ctf)
13062 info->callbacks->emit_ctf ();
13063
13064 elf_final_link_free (abfd, &flinfo);
13065
13066 if (attr_section)
13067 {
13068 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13069 if (contents == NULL)
13070 return FALSE; /* Bail out and fail. */
13071 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13072 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13073 free (contents);
13074 }
13075
13076 return TRUE;
13077
13078 error_return:
13079 elf_final_link_free (abfd, &flinfo);
13080 return FALSE;
13081 }
13082 \f
13083 /* Initialize COOKIE for input bfd ABFD. */
13084
13085 static bfd_boolean
13086 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13087 struct bfd_link_info *info, bfd *abfd)
13088 {
13089 Elf_Internal_Shdr *symtab_hdr;
13090 const struct elf_backend_data *bed;
13091
13092 bed = get_elf_backend_data (abfd);
13093 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13094
13095 cookie->abfd = abfd;
13096 cookie->sym_hashes = elf_sym_hashes (abfd);
13097 cookie->bad_symtab = elf_bad_symtab (abfd);
13098 if (cookie->bad_symtab)
13099 {
13100 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13101 cookie->extsymoff = 0;
13102 }
13103 else
13104 {
13105 cookie->locsymcount = symtab_hdr->sh_info;
13106 cookie->extsymoff = symtab_hdr->sh_info;
13107 }
13108
13109 if (bed->s->arch_size == 32)
13110 cookie->r_sym_shift = 8;
13111 else
13112 cookie->r_sym_shift = 32;
13113
13114 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13115 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13116 {
13117 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13118 cookie->locsymcount, 0,
13119 NULL, NULL, NULL);
13120 if (cookie->locsyms == NULL)
13121 {
13122 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13123 return FALSE;
13124 }
13125 if (info->keep_memory)
13126 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13127 }
13128 return TRUE;
13129 }
13130
13131 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13132
13133 static void
13134 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13135 {
13136 Elf_Internal_Shdr *symtab_hdr;
13137
13138 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13139 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13140 free (cookie->locsyms);
13141 }
13142
13143 /* Initialize the relocation information in COOKIE for input section SEC
13144 of input bfd ABFD. */
13145
13146 static bfd_boolean
13147 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13148 struct bfd_link_info *info, bfd *abfd,
13149 asection *sec)
13150 {
13151 if (sec->reloc_count == 0)
13152 {
13153 cookie->rels = NULL;
13154 cookie->relend = NULL;
13155 }
13156 else
13157 {
13158 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13159 info->keep_memory);
13160 if (cookie->rels == NULL)
13161 return FALSE;
13162 cookie->rel = cookie->rels;
13163 cookie->relend = cookie->rels + sec->reloc_count;
13164 }
13165 cookie->rel = cookie->rels;
13166 return TRUE;
13167 }
13168
13169 /* Free the memory allocated by init_reloc_cookie_rels,
13170 if appropriate. */
13171
13172 static void
13173 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13174 asection *sec)
13175 {
13176 if (elf_section_data (sec)->relocs != cookie->rels)
13177 free (cookie->rels);
13178 }
13179
13180 /* Initialize the whole of COOKIE for input section SEC. */
13181
13182 static bfd_boolean
13183 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13184 struct bfd_link_info *info,
13185 asection *sec)
13186 {
13187 if (!init_reloc_cookie (cookie, info, sec->owner))
13188 goto error1;
13189 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13190 goto error2;
13191 return TRUE;
13192
13193 error2:
13194 fini_reloc_cookie (cookie, sec->owner);
13195 error1:
13196 return FALSE;
13197 }
13198
13199 /* Free the memory allocated by init_reloc_cookie_for_section,
13200 if appropriate. */
13201
13202 static void
13203 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13204 asection *sec)
13205 {
13206 fini_reloc_cookie_rels (cookie, sec);
13207 fini_reloc_cookie (cookie, sec->owner);
13208 }
13209 \f
13210 /* Garbage collect unused sections. */
13211
13212 /* Default gc_mark_hook. */
13213
13214 asection *
13215 _bfd_elf_gc_mark_hook (asection *sec,
13216 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13217 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13218 struct elf_link_hash_entry *h,
13219 Elf_Internal_Sym *sym)
13220 {
13221 if (h != NULL)
13222 {
13223 switch (h->root.type)
13224 {
13225 case bfd_link_hash_defined:
13226 case bfd_link_hash_defweak:
13227 return h->root.u.def.section;
13228
13229 case bfd_link_hash_common:
13230 return h->root.u.c.p->section;
13231
13232 default:
13233 break;
13234 }
13235 }
13236 else
13237 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13238
13239 return NULL;
13240 }
13241
13242 /* Return the debug definition section. */
13243
13244 static asection *
13245 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13246 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13247 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13248 struct elf_link_hash_entry *h,
13249 Elf_Internal_Sym *sym)
13250 {
13251 if (h != NULL)
13252 {
13253 /* Return the global debug definition section. */
13254 if ((h->root.type == bfd_link_hash_defined
13255 || h->root.type == bfd_link_hash_defweak)
13256 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13257 return h->root.u.def.section;
13258 }
13259 else
13260 {
13261 /* Return the local debug definition section. */
13262 asection *isec = bfd_section_from_elf_index (sec->owner,
13263 sym->st_shndx);
13264 if ((isec->flags & SEC_DEBUGGING) != 0)
13265 return isec;
13266 }
13267
13268 return NULL;
13269 }
13270
13271 /* COOKIE->rel describes a relocation against section SEC, which is
13272 a section we've decided to keep. Return the section that contains
13273 the relocation symbol, or NULL if no section contains it. */
13274
13275 asection *
13276 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13277 elf_gc_mark_hook_fn gc_mark_hook,
13278 struct elf_reloc_cookie *cookie,
13279 bfd_boolean *start_stop)
13280 {
13281 unsigned long r_symndx;
13282 struct elf_link_hash_entry *h, *hw;
13283
13284 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13285 if (r_symndx == STN_UNDEF)
13286 return NULL;
13287
13288 if (r_symndx >= cookie->locsymcount
13289 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13290 {
13291 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13292 if (h == NULL)
13293 {
13294 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13295 sec->owner);
13296 return NULL;
13297 }
13298 while (h->root.type == bfd_link_hash_indirect
13299 || h->root.type == bfd_link_hash_warning)
13300 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13301 h->mark = 1;
13302 /* Keep all aliases of the symbol too. If an object symbol
13303 needs to be copied into .dynbss then all of its aliases
13304 should be present as dynamic symbols, not just the one used
13305 on the copy relocation. */
13306 hw = h;
13307 while (hw->is_weakalias)
13308 {
13309 hw = hw->u.alias;
13310 hw->mark = 1;
13311 }
13312
13313 if (start_stop != NULL)
13314 {
13315 /* To work around a glibc bug, mark XXX input sections
13316 when there is a reference to __start_XXX or __stop_XXX
13317 symbols. */
13318 if (h->start_stop)
13319 {
13320 asection *s = h->u2.start_stop_section;
13321 *start_stop = !s->gc_mark;
13322 return s;
13323 }
13324 }
13325
13326 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13327 }
13328
13329 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13330 &cookie->locsyms[r_symndx]);
13331 }
13332
13333 /* COOKIE->rel describes a relocation against section SEC, which is
13334 a section we've decided to keep. Mark the section that contains
13335 the relocation symbol. */
13336
13337 bfd_boolean
13338 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13339 asection *sec,
13340 elf_gc_mark_hook_fn gc_mark_hook,
13341 struct elf_reloc_cookie *cookie)
13342 {
13343 asection *rsec;
13344 bfd_boolean start_stop = FALSE;
13345
13346 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13347 while (rsec != NULL)
13348 {
13349 if (!rsec->gc_mark)
13350 {
13351 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13352 || (rsec->owner->flags & DYNAMIC) != 0)
13353 rsec->gc_mark = 1;
13354 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13355 return FALSE;
13356 }
13357 if (!start_stop)
13358 break;
13359 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13360 }
13361 return TRUE;
13362 }
13363
13364 /* The mark phase of garbage collection. For a given section, mark
13365 it and any sections in this section's group, and all the sections
13366 which define symbols to which it refers. */
13367
13368 bfd_boolean
13369 _bfd_elf_gc_mark (struct bfd_link_info *info,
13370 asection *sec,
13371 elf_gc_mark_hook_fn gc_mark_hook)
13372 {
13373 bfd_boolean ret;
13374 asection *group_sec, *eh_frame;
13375
13376 sec->gc_mark = 1;
13377
13378 /* Mark all the sections in the group. */
13379 group_sec = elf_section_data (sec)->next_in_group;
13380 if (group_sec && !group_sec->gc_mark)
13381 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13382 return FALSE;
13383
13384 /* Look through the section relocs. */
13385 ret = TRUE;
13386 eh_frame = elf_eh_frame_section (sec->owner);
13387 if ((sec->flags & SEC_RELOC) != 0
13388 && sec->reloc_count > 0
13389 && sec != eh_frame)
13390 {
13391 struct elf_reloc_cookie cookie;
13392
13393 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13394 ret = FALSE;
13395 else
13396 {
13397 for (; cookie.rel < cookie.relend; cookie.rel++)
13398 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13399 {
13400 ret = FALSE;
13401 break;
13402 }
13403 fini_reloc_cookie_for_section (&cookie, sec);
13404 }
13405 }
13406
13407 if (ret && eh_frame && elf_fde_list (sec))
13408 {
13409 struct elf_reloc_cookie cookie;
13410
13411 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13412 ret = FALSE;
13413 else
13414 {
13415 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13416 gc_mark_hook, &cookie))
13417 ret = FALSE;
13418 fini_reloc_cookie_for_section (&cookie, eh_frame);
13419 }
13420 }
13421
13422 eh_frame = elf_section_eh_frame_entry (sec);
13423 if (ret && eh_frame && !eh_frame->gc_mark)
13424 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13425 ret = FALSE;
13426
13427 return ret;
13428 }
13429
13430 /* Scan and mark sections in a special or debug section group. */
13431
13432 static void
13433 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13434 {
13435 /* Point to first section of section group. */
13436 asection *ssec;
13437 /* Used to iterate the section group. */
13438 asection *msec;
13439
13440 bfd_boolean is_special_grp = TRUE;
13441 bfd_boolean is_debug_grp = TRUE;
13442
13443 /* First scan to see if group contains any section other than debug
13444 and special section. */
13445 ssec = msec = elf_next_in_group (grp);
13446 do
13447 {
13448 if ((msec->flags & SEC_DEBUGGING) == 0)
13449 is_debug_grp = FALSE;
13450
13451 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13452 is_special_grp = FALSE;
13453
13454 msec = elf_next_in_group (msec);
13455 }
13456 while (msec != ssec);
13457
13458 /* If this is a pure debug section group or pure special section group,
13459 keep all sections in this group. */
13460 if (is_debug_grp || is_special_grp)
13461 {
13462 do
13463 {
13464 msec->gc_mark = 1;
13465 msec = elf_next_in_group (msec);
13466 }
13467 while (msec != ssec);
13468 }
13469 }
13470
13471 /* Keep debug and special sections. */
13472
13473 bfd_boolean
13474 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13475 elf_gc_mark_hook_fn mark_hook)
13476 {
13477 bfd *ibfd;
13478
13479 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13480 {
13481 asection *isec;
13482 bfd_boolean some_kept;
13483 bfd_boolean debug_frag_seen;
13484 bfd_boolean has_kept_debug_info;
13485
13486 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13487 continue;
13488 isec = ibfd->sections;
13489 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13490 continue;
13491
13492 /* Ensure all linker created sections are kept,
13493 see if any other section is already marked,
13494 and note if we have any fragmented debug sections. */
13495 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13496 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13497 {
13498 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13499 isec->gc_mark = 1;
13500 else if (isec->gc_mark
13501 && (isec->flags & SEC_ALLOC) != 0
13502 && elf_section_type (isec) != SHT_NOTE)
13503 some_kept = TRUE;
13504 else
13505 {
13506 /* Since all sections, except for backend specific ones,
13507 have been garbage collected, call mark_hook on this
13508 section if any of its linked-to sections is marked. */
13509 asection *linked_to_sec = elf_linked_to_section (isec);
13510 for (; linked_to_sec != NULL;
13511 linked_to_sec = elf_linked_to_section (linked_to_sec))
13512 if (linked_to_sec->gc_mark)
13513 {
13514 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13515 return FALSE;
13516 break;
13517 }
13518 }
13519
13520 if (!debug_frag_seen
13521 && (isec->flags & SEC_DEBUGGING)
13522 && CONST_STRNEQ (isec->name, ".debug_line."))
13523 debug_frag_seen = TRUE;
13524 else if (strcmp (bfd_section_name (isec),
13525 "__patchable_function_entries") == 0
13526 && elf_linked_to_section (isec) == NULL)
13527 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13528 "need linked-to section "
13529 "for --gc-sections\n"),
13530 isec->owner, isec);
13531 }
13532
13533 /* If no non-note alloc section in this file will be kept, then
13534 we can toss out the debug and special sections. */
13535 if (!some_kept)
13536 continue;
13537
13538 /* Keep debug and special sections like .comment when they are
13539 not part of a group. Also keep section groups that contain
13540 just debug sections or special sections. NB: Sections with
13541 linked-to section has been handled above. */
13542 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13543 {
13544 if ((isec->flags & SEC_GROUP) != 0)
13545 _bfd_elf_gc_mark_debug_special_section_group (isec);
13546 else if (((isec->flags & SEC_DEBUGGING) != 0
13547 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13548 && elf_next_in_group (isec) == NULL
13549 && elf_linked_to_section (isec) == NULL)
13550 isec->gc_mark = 1;
13551 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13552 has_kept_debug_info = TRUE;
13553 }
13554
13555 /* Look for CODE sections which are going to be discarded,
13556 and find and discard any fragmented debug sections which
13557 are associated with that code section. */
13558 if (debug_frag_seen)
13559 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13560 if ((isec->flags & SEC_CODE) != 0
13561 && isec->gc_mark == 0)
13562 {
13563 unsigned int ilen;
13564 asection *dsec;
13565
13566 ilen = strlen (isec->name);
13567
13568 /* Association is determined by the name of the debug
13569 section containing the name of the code section as
13570 a suffix. For example .debug_line.text.foo is a
13571 debug section associated with .text.foo. */
13572 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13573 {
13574 unsigned int dlen;
13575
13576 if (dsec->gc_mark == 0
13577 || (dsec->flags & SEC_DEBUGGING) == 0)
13578 continue;
13579
13580 dlen = strlen (dsec->name);
13581
13582 if (dlen > ilen
13583 && strncmp (dsec->name + (dlen - ilen),
13584 isec->name, ilen) == 0)
13585 dsec->gc_mark = 0;
13586 }
13587 }
13588
13589 /* Mark debug sections referenced by kept debug sections. */
13590 if (has_kept_debug_info)
13591 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13592 if (isec->gc_mark
13593 && (isec->flags & SEC_DEBUGGING) != 0)
13594 if (!_bfd_elf_gc_mark (info, isec,
13595 elf_gc_mark_debug_section))
13596 return FALSE;
13597 }
13598 return TRUE;
13599 }
13600
13601 static bfd_boolean
13602 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13603 {
13604 bfd *sub;
13605 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13606
13607 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13608 {
13609 asection *o;
13610
13611 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13612 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13613 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13614 continue;
13615 o = sub->sections;
13616 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13617 continue;
13618
13619 for (o = sub->sections; o != NULL; o = o->next)
13620 {
13621 /* When any section in a section group is kept, we keep all
13622 sections in the section group. If the first member of
13623 the section group is excluded, we will also exclude the
13624 group section. */
13625 if (o->flags & SEC_GROUP)
13626 {
13627 asection *first = elf_next_in_group (o);
13628 o->gc_mark = first->gc_mark;
13629 }
13630
13631 if (o->gc_mark)
13632 continue;
13633
13634 /* Skip sweeping sections already excluded. */
13635 if (o->flags & SEC_EXCLUDE)
13636 continue;
13637
13638 /* Since this is early in the link process, it is simple
13639 to remove a section from the output. */
13640 o->flags |= SEC_EXCLUDE;
13641
13642 if (info->print_gc_sections && o->size != 0)
13643 /* xgettext:c-format */
13644 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13645 o, sub);
13646 }
13647 }
13648
13649 return TRUE;
13650 }
13651
13652 /* Propagate collected vtable information. This is called through
13653 elf_link_hash_traverse. */
13654
13655 static bfd_boolean
13656 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13657 {
13658 /* Those that are not vtables. */
13659 if (h->start_stop
13660 || h->u2.vtable == NULL
13661 || h->u2.vtable->parent == NULL)
13662 return TRUE;
13663
13664 /* Those vtables that do not have parents, we cannot merge. */
13665 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13666 return TRUE;
13667
13668 /* If we've already been done, exit. */
13669 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13670 return TRUE;
13671
13672 /* Make sure the parent's table is up to date. */
13673 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13674
13675 if (h->u2.vtable->used == NULL)
13676 {
13677 /* None of this table's entries were referenced. Re-use the
13678 parent's table. */
13679 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13680 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13681 }
13682 else
13683 {
13684 size_t n;
13685 bfd_boolean *cu, *pu;
13686
13687 /* Or the parent's entries into ours. */
13688 cu = h->u2.vtable->used;
13689 cu[-1] = TRUE;
13690 pu = h->u2.vtable->parent->u2.vtable->used;
13691 if (pu != NULL)
13692 {
13693 const struct elf_backend_data *bed;
13694 unsigned int log_file_align;
13695
13696 bed = get_elf_backend_data (h->root.u.def.section->owner);
13697 log_file_align = bed->s->log_file_align;
13698 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13699 while (n--)
13700 {
13701 if (*pu)
13702 *cu = TRUE;
13703 pu++;
13704 cu++;
13705 }
13706 }
13707 }
13708
13709 return TRUE;
13710 }
13711
13712 static bfd_boolean
13713 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13714 {
13715 asection *sec;
13716 bfd_vma hstart, hend;
13717 Elf_Internal_Rela *relstart, *relend, *rel;
13718 const struct elf_backend_data *bed;
13719 unsigned int log_file_align;
13720
13721 /* Take care of both those symbols that do not describe vtables as
13722 well as those that are not loaded. */
13723 if (h->start_stop
13724 || h->u2.vtable == NULL
13725 || h->u2.vtable->parent == NULL)
13726 return TRUE;
13727
13728 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13729 || h->root.type == bfd_link_hash_defweak);
13730
13731 sec = h->root.u.def.section;
13732 hstart = h->root.u.def.value;
13733 hend = hstart + h->size;
13734
13735 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13736 if (!relstart)
13737 return *(bfd_boolean *) okp = FALSE;
13738 bed = get_elf_backend_data (sec->owner);
13739 log_file_align = bed->s->log_file_align;
13740
13741 relend = relstart + sec->reloc_count;
13742
13743 for (rel = relstart; rel < relend; ++rel)
13744 if (rel->r_offset >= hstart && rel->r_offset < hend)
13745 {
13746 /* If the entry is in use, do nothing. */
13747 if (h->u2.vtable->used
13748 && (rel->r_offset - hstart) < h->u2.vtable->size)
13749 {
13750 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13751 if (h->u2.vtable->used[entry])
13752 continue;
13753 }
13754 /* Otherwise, kill it. */
13755 rel->r_offset = rel->r_info = rel->r_addend = 0;
13756 }
13757
13758 return TRUE;
13759 }
13760
13761 /* Mark sections containing dynamically referenced symbols. When
13762 building shared libraries, we must assume that any visible symbol is
13763 referenced. */
13764
13765 bfd_boolean
13766 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13767 {
13768 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13769 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13770
13771 if ((h->root.type == bfd_link_hash_defined
13772 || h->root.type == bfd_link_hash_defweak)
13773 && ((h->ref_dynamic && !h->forced_local)
13774 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13775 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13776 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13777 && (!bfd_link_executable (info)
13778 || info->gc_keep_exported
13779 || info->export_dynamic
13780 || (h->dynamic
13781 && d != NULL
13782 && (*d->match) (&d->head, NULL, h->root.root.string)))
13783 && (h->versioned >= versioned
13784 || !bfd_hide_sym_by_version (info->version_info,
13785 h->root.root.string)))))
13786 h->root.u.def.section->flags |= SEC_KEEP;
13787
13788 return TRUE;
13789 }
13790
13791 /* Keep all sections containing symbols undefined on the command-line,
13792 and the section containing the entry symbol. */
13793
13794 void
13795 _bfd_elf_gc_keep (struct bfd_link_info *info)
13796 {
13797 struct bfd_sym_chain *sym;
13798
13799 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13800 {
13801 struct elf_link_hash_entry *h;
13802
13803 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13804 FALSE, FALSE, FALSE);
13805
13806 if (h != NULL
13807 && (h->root.type == bfd_link_hash_defined
13808 || h->root.type == bfd_link_hash_defweak)
13809 && !bfd_is_const_section (h->root.u.def.section))
13810 h->root.u.def.section->flags |= SEC_KEEP;
13811 }
13812 }
13813
13814 bfd_boolean
13815 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13816 struct bfd_link_info *info)
13817 {
13818 bfd *ibfd = info->input_bfds;
13819
13820 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13821 {
13822 asection *sec;
13823 struct elf_reloc_cookie cookie;
13824
13825 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13826 continue;
13827 sec = ibfd->sections;
13828 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13829 continue;
13830
13831 if (!init_reloc_cookie (&cookie, info, ibfd))
13832 return FALSE;
13833
13834 for (sec = ibfd->sections; sec; sec = sec->next)
13835 {
13836 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13837 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13838 {
13839 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13840 fini_reloc_cookie_rels (&cookie, sec);
13841 }
13842 }
13843 }
13844 return TRUE;
13845 }
13846
13847 /* Do mark and sweep of unused sections. */
13848
13849 bfd_boolean
13850 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13851 {
13852 bfd_boolean ok = TRUE;
13853 bfd *sub;
13854 elf_gc_mark_hook_fn gc_mark_hook;
13855 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13856 struct elf_link_hash_table *htab;
13857
13858 if (!bed->can_gc_sections
13859 || !is_elf_hash_table (info->hash))
13860 {
13861 _bfd_error_handler(_("warning: gc-sections option ignored"));
13862 return TRUE;
13863 }
13864
13865 bed->gc_keep (info);
13866 htab = elf_hash_table (info);
13867
13868 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13869 at the .eh_frame section if we can mark the FDEs individually. */
13870 for (sub = info->input_bfds;
13871 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13872 sub = sub->link.next)
13873 {
13874 asection *sec;
13875 struct elf_reloc_cookie cookie;
13876
13877 sec = sub->sections;
13878 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13879 continue;
13880 sec = bfd_get_section_by_name (sub, ".eh_frame");
13881 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13882 {
13883 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13884 if (elf_section_data (sec)->sec_info
13885 && (sec->flags & SEC_LINKER_CREATED) == 0)
13886 elf_eh_frame_section (sub) = sec;
13887 fini_reloc_cookie_for_section (&cookie, sec);
13888 sec = bfd_get_next_section_by_name (NULL, sec);
13889 }
13890 }
13891
13892 /* Apply transitive closure to the vtable entry usage info. */
13893 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13894 if (!ok)
13895 return FALSE;
13896
13897 /* Kill the vtable relocations that were not used. */
13898 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13899 if (!ok)
13900 return FALSE;
13901
13902 /* Mark dynamically referenced symbols. */
13903 if (htab->dynamic_sections_created || info->gc_keep_exported)
13904 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13905
13906 /* Grovel through relocs to find out who stays ... */
13907 gc_mark_hook = bed->gc_mark_hook;
13908 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13909 {
13910 asection *o;
13911
13912 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13913 || elf_object_id (sub) != elf_hash_table_id (htab)
13914 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13915 continue;
13916
13917 o = sub->sections;
13918 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13919 continue;
13920
13921 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13922 Also treat note sections as a root, if the section is not part
13923 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13924 well as FINI_ARRAY sections for ld -r. */
13925 for (o = sub->sections; o != NULL; o = o->next)
13926 if (!o->gc_mark
13927 && (o->flags & SEC_EXCLUDE) == 0
13928 && ((o->flags & SEC_KEEP) != 0
13929 || (bfd_link_relocatable (info)
13930 && ((elf_section_data (o)->this_hdr.sh_type
13931 == SHT_PREINIT_ARRAY)
13932 || (elf_section_data (o)->this_hdr.sh_type
13933 == SHT_INIT_ARRAY)
13934 || (elf_section_data (o)->this_hdr.sh_type
13935 == SHT_FINI_ARRAY)))
13936 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13937 && elf_next_in_group (o) == NULL )))
13938 {
13939 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13940 return FALSE;
13941 }
13942 }
13943
13944 /* Allow the backend to mark additional target specific sections. */
13945 bed->gc_mark_extra_sections (info, gc_mark_hook);
13946
13947 /* ... and mark SEC_EXCLUDE for those that go. */
13948 return elf_gc_sweep (abfd, info);
13949 }
13950 \f
13951 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13952
13953 bfd_boolean
13954 bfd_elf_gc_record_vtinherit (bfd *abfd,
13955 asection *sec,
13956 struct elf_link_hash_entry *h,
13957 bfd_vma offset)
13958 {
13959 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13960 struct elf_link_hash_entry **search, *child;
13961 size_t extsymcount;
13962 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13963
13964 /* The sh_info field of the symtab header tells us where the
13965 external symbols start. We don't care about the local symbols at
13966 this point. */
13967 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13968 if (!elf_bad_symtab (abfd))
13969 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13970
13971 sym_hashes = elf_sym_hashes (abfd);
13972 sym_hashes_end = sym_hashes + extsymcount;
13973
13974 /* Hunt down the child symbol, which is in this section at the same
13975 offset as the relocation. */
13976 for (search = sym_hashes; search != sym_hashes_end; ++search)
13977 {
13978 if ((child = *search) != NULL
13979 && (child->root.type == bfd_link_hash_defined
13980 || child->root.type == bfd_link_hash_defweak)
13981 && child->root.u.def.section == sec
13982 && child->root.u.def.value == offset)
13983 goto win;
13984 }
13985
13986 /* xgettext:c-format */
13987 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13988 abfd, sec, (uint64_t) offset);
13989 bfd_set_error (bfd_error_invalid_operation);
13990 return FALSE;
13991
13992 win:
13993 if (!child->u2.vtable)
13994 {
13995 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13996 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13997 if (!child->u2.vtable)
13998 return FALSE;
13999 }
14000 if (!h)
14001 {
14002 /* This *should* only be the absolute section. It could potentially
14003 be that someone has defined a non-global vtable though, which
14004 would be bad. It isn't worth paging in the local symbols to be
14005 sure though; that case should simply be handled by the assembler. */
14006
14007 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14008 }
14009 else
14010 child->u2.vtable->parent = h;
14011
14012 return TRUE;
14013 }
14014
14015 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14016
14017 bfd_boolean
14018 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14019 struct elf_link_hash_entry *h,
14020 bfd_vma addend)
14021 {
14022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14023 unsigned int log_file_align = bed->s->log_file_align;
14024
14025 if (!h)
14026 {
14027 /* xgettext:c-format */
14028 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14029 abfd, sec);
14030 bfd_set_error (bfd_error_bad_value);
14031 return FALSE;
14032 }
14033
14034 if (!h->u2.vtable)
14035 {
14036 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14037 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14038 if (!h->u2.vtable)
14039 return FALSE;
14040 }
14041
14042 if (addend >= h->u2.vtable->size)
14043 {
14044 size_t size, bytes, file_align;
14045 bfd_boolean *ptr = h->u2.vtable->used;
14046
14047 /* While the symbol is undefined, we have to be prepared to handle
14048 a zero size. */
14049 file_align = 1 << log_file_align;
14050 if (h->root.type == bfd_link_hash_undefined)
14051 size = addend + file_align;
14052 else
14053 {
14054 size = h->size;
14055 if (addend >= size)
14056 {
14057 /* Oops! We've got a reference past the defined end of
14058 the table. This is probably a bug -- shall we warn? */
14059 size = addend + file_align;
14060 }
14061 }
14062 size = (size + file_align - 1) & -file_align;
14063
14064 /* Allocate one extra entry for use as a "done" flag for the
14065 consolidation pass. */
14066 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14067
14068 if (ptr)
14069 {
14070 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14071
14072 if (ptr != NULL)
14073 {
14074 size_t oldbytes;
14075
14076 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14077 * sizeof (bfd_boolean));
14078 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14079 }
14080 }
14081 else
14082 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14083
14084 if (ptr == NULL)
14085 return FALSE;
14086
14087 /* And arrange for that done flag to be at index -1. */
14088 h->u2.vtable->used = ptr + 1;
14089 h->u2.vtable->size = size;
14090 }
14091
14092 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14093
14094 return TRUE;
14095 }
14096
14097 /* Map an ELF section header flag to its corresponding string. */
14098 typedef struct
14099 {
14100 char *flag_name;
14101 flagword flag_value;
14102 } elf_flags_to_name_table;
14103
14104 static elf_flags_to_name_table elf_flags_to_names [] =
14105 {
14106 { "SHF_WRITE", SHF_WRITE },
14107 { "SHF_ALLOC", SHF_ALLOC },
14108 { "SHF_EXECINSTR", SHF_EXECINSTR },
14109 { "SHF_MERGE", SHF_MERGE },
14110 { "SHF_STRINGS", SHF_STRINGS },
14111 { "SHF_INFO_LINK", SHF_INFO_LINK},
14112 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14113 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14114 { "SHF_GROUP", SHF_GROUP },
14115 { "SHF_TLS", SHF_TLS },
14116 { "SHF_MASKOS", SHF_MASKOS },
14117 { "SHF_EXCLUDE", SHF_EXCLUDE },
14118 };
14119
14120 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14121 bfd_boolean
14122 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14123 struct flag_info *flaginfo,
14124 asection *section)
14125 {
14126 const bfd_vma sh_flags = elf_section_flags (section);
14127
14128 if (!flaginfo->flags_initialized)
14129 {
14130 bfd *obfd = info->output_bfd;
14131 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14132 struct flag_info_list *tf = flaginfo->flag_list;
14133 int with_hex = 0;
14134 int without_hex = 0;
14135
14136 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14137 {
14138 unsigned i;
14139 flagword (*lookup) (char *);
14140
14141 lookup = bed->elf_backend_lookup_section_flags_hook;
14142 if (lookup != NULL)
14143 {
14144 flagword hexval = (*lookup) ((char *) tf->name);
14145
14146 if (hexval != 0)
14147 {
14148 if (tf->with == with_flags)
14149 with_hex |= hexval;
14150 else if (tf->with == without_flags)
14151 without_hex |= hexval;
14152 tf->valid = TRUE;
14153 continue;
14154 }
14155 }
14156 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14157 {
14158 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14159 {
14160 if (tf->with == with_flags)
14161 with_hex |= elf_flags_to_names[i].flag_value;
14162 else if (tf->with == without_flags)
14163 without_hex |= elf_flags_to_names[i].flag_value;
14164 tf->valid = TRUE;
14165 break;
14166 }
14167 }
14168 if (!tf->valid)
14169 {
14170 info->callbacks->einfo
14171 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14172 return FALSE;
14173 }
14174 }
14175 flaginfo->flags_initialized = TRUE;
14176 flaginfo->only_with_flags |= with_hex;
14177 flaginfo->not_with_flags |= without_hex;
14178 }
14179
14180 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14181 return FALSE;
14182
14183 if ((flaginfo->not_with_flags & sh_flags) != 0)
14184 return FALSE;
14185
14186 return TRUE;
14187 }
14188
14189 struct alloc_got_off_arg {
14190 bfd_vma gotoff;
14191 struct bfd_link_info *info;
14192 };
14193
14194 /* We need a special top-level link routine to convert got reference counts
14195 to real got offsets. */
14196
14197 static bfd_boolean
14198 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14199 {
14200 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14201 bfd *obfd = gofarg->info->output_bfd;
14202 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14203
14204 if (h->got.refcount > 0)
14205 {
14206 h->got.offset = gofarg->gotoff;
14207 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14208 }
14209 else
14210 h->got.offset = (bfd_vma) -1;
14211
14212 return TRUE;
14213 }
14214
14215 /* And an accompanying bit to work out final got entry offsets once
14216 we're done. Should be called from final_link. */
14217
14218 bfd_boolean
14219 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14220 struct bfd_link_info *info)
14221 {
14222 bfd *i;
14223 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14224 bfd_vma gotoff;
14225 struct alloc_got_off_arg gofarg;
14226
14227 BFD_ASSERT (abfd == info->output_bfd);
14228
14229 if (! is_elf_hash_table (info->hash))
14230 return FALSE;
14231
14232 /* The GOT offset is relative to the .got section, but the GOT header is
14233 put into the .got.plt section, if the backend uses it. */
14234 if (bed->want_got_plt)
14235 gotoff = 0;
14236 else
14237 gotoff = bed->got_header_size;
14238
14239 /* Do the local .got entries first. */
14240 for (i = info->input_bfds; i; i = i->link.next)
14241 {
14242 bfd_signed_vma *local_got;
14243 size_t j, locsymcount;
14244 Elf_Internal_Shdr *symtab_hdr;
14245
14246 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14247 continue;
14248
14249 local_got = elf_local_got_refcounts (i);
14250 if (!local_got)
14251 continue;
14252
14253 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14254 if (elf_bad_symtab (i))
14255 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14256 else
14257 locsymcount = symtab_hdr->sh_info;
14258
14259 for (j = 0; j < locsymcount; ++j)
14260 {
14261 if (local_got[j] > 0)
14262 {
14263 local_got[j] = gotoff;
14264 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14265 }
14266 else
14267 local_got[j] = (bfd_vma) -1;
14268 }
14269 }
14270
14271 /* Then the global .got entries. .plt refcounts are handled by
14272 adjust_dynamic_symbol */
14273 gofarg.gotoff = gotoff;
14274 gofarg.info = info;
14275 elf_link_hash_traverse (elf_hash_table (info),
14276 elf_gc_allocate_got_offsets,
14277 &gofarg);
14278 return TRUE;
14279 }
14280
14281 /* Many folk need no more in the way of final link than this, once
14282 got entry reference counting is enabled. */
14283
14284 bfd_boolean
14285 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14286 {
14287 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14288 return FALSE;
14289
14290 /* Invoke the regular ELF backend linker to do all the work. */
14291 return bfd_elf_final_link (abfd, info);
14292 }
14293
14294 bfd_boolean
14295 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14296 {
14297 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14298
14299 if (rcookie->bad_symtab)
14300 rcookie->rel = rcookie->rels;
14301
14302 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14303 {
14304 unsigned long r_symndx;
14305
14306 if (! rcookie->bad_symtab)
14307 if (rcookie->rel->r_offset > offset)
14308 return FALSE;
14309 if (rcookie->rel->r_offset != offset)
14310 continue;
14311
14312 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14313 if (r_symndx == STN_UNDEF)
14314 return TRUE;
14315
14316 if (r_symndx >= rcookie->locsymcount
14317 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14318 {
14319 struct elf_link_hash_entry *h;
14320
14321 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14322
14323 while (h->root.type == bfd_link_hash_indirect
14324 || h->root.type == bfd_link_hash_warning)
14325 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14326
14327 if ((h->root.type == bfd_link_hash_defined
14328 || h->root.type == bfd_link_hash_defweak)
14329 && (h->root.u.def.section->owner != rcookie->abfd
14330 || h->root.u.def.section->kept_section != NULL
14331 || discarded_section (h->root.u.def.section)))
14332 return TRUE;
14333 }
14334 else
14335 {
14336 /* It's not a relocation against a global symbol,
14337 but it could be a relocation against a local
14338 symbol for a discarded section. */
14339 asection *isec;
14340 Elf_Internal_Sym *isym;
14341
14342 /* Need to: get the symbol; get the section. */
14343 isym = &rcookie->locsyms[r_symndx];
14344 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14345 if (isec != NULL
14346 && (isec->kept_section != NULL
14347 || discarded_section (isec)))
14348 return TRUE;
14349 }
14350 return FALSE;
14351 }
14352 return FALSE;
14353 }
14354
14355 /* Discard unneeded references to discarded sections.
14356 Returns -1 on error, 1 if any section's size was changed, 0 if
14357 nothing changed. This function assumes that the relocations are in
14358 sorted order, which is true for all known assemblers. */
14359
14360 int
14361 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14362 {
14363 struct elf_reloc_cookie cookie;
14364 asection *o;
14365 bfd *abfd;
14366 int changed = 0;
14367
14368 if (info->traditional_format
14369 || !is_elf_hash_table (info->hash))
14370 return 0;
14371
14372 o = bfd_get_section_by_name (output_bfd, ".stab");
14373 if (o != NULL)
14374 {
14375 asection *i;
14376
14377 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14378 {
14379 if (i->size == 0
14380 || i->reloc_count == 0
14381 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14382 continue;
14383
14384 abfd = i->owner;
14385 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14386 continue;
14387
14388 if (!init_reloc_cookie_for_section (&cookie, info, i))
14389 return -1;
14390
14391 if (_bfd_discard_section_stabs (abfd, i,
14392 elf_section_data (i)->sec_info,
14393 bfd_elf_reloc_symbol_deleted_p,
14394 &cookie))
14395 changed = 1;
14396
14397 fini_reloc_cookie_for_section (&cookie, i);
14398 }
14399 }
14400
14401 o = NULL;
14402 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14403 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14404 if (o != NULL)
14405 {
14406 asection *i;
14407 int eh_changed = 0;
14408 unsigned int eh_alignment; /* Octets. */
14409
14410 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14411 {
14412 if (i->size == 0)
14413 continue;
14414
14415 abfd = i->owner;
14416 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14417 continue;
14418
14419 if (!init_reloc_cookie_for_section (&cookie, info, i))
14420 return -1;
14421
14422 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14423 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14424 bfd_elf_reloc_symbol_deleted_p,
14425 &cookie))
14426 {
14427 eh_changed = 1;
14428 if (i->size != i->rawsize)
14429 changed = 1;
14430 }
14431
14432 fini_reloc_cookie_for_section (&cookie, i);
14433 }
14434
14435 eh_alignment = ((1 << o->alignment_power)
14436 * bfd_octets_per_byte (output_bfd, o));
14437 /* Skip over zero terminator, and prevent empty sections from
14438 adding alignment padding at the end. */
14439 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14440 if (i->size == 0)
14441 i->flags |= SEC_EXCLUDE;
14442 else if (i->size > 4)
14443 break;
14444 /* The last non-empty eh_frame section doesn't need padding. */
14445 if (i != NULL)
14446 i = i->map_tail.s;
14447 /* Any prior sections must pad the last FDE out to the output
14448 section alignment. Otherwise we might have zero padding
14449 between sections, which would be seen as a terminator. */
14450 for (; i != NULL; i = i->map_tail.s)
14451 if (i->size == 4)
14452 /* All but the last zero terminator should have been removed. */
14453 BFD_FAIL ();
14454 else
14455 {
14456 bfd_size_type size
14457 = (i->size + eh_alignment - 1) & -eh_alignment;
14458 if (i->size != size)
14459 {
14460 i->size = size;
14461 changed = 1;
14462 eh_changed = 1;
14463 }
14464 }
14465 if (eh_changed)
14466 elf_link_hash_traverse (elf_hash_table (info),
14467 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14468 }
14469
14470 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14471 {
14472 const struct elf_backend_data *bed;
14473 asection *s;
14474
14475 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14476 continue;
14477 s = abfd->sections;
14478 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14479 continue;
14480
14481 bed = get_elf_backend_data (abfd);
14482
14483 if (bed->elf_backend_discard_info != NULL)
14484 {
14485 if (!init_reloc_cookie (&cookie, info, abfd))
14486 return -1;
14487
14488 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14489 changed = 1;
14490
14491 fini_reloc_cookie (&cookie, abfd);
14492 }
14493 }
14494
14495 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14496 _bfd_elf_end_eh_frame_parsing (info);
14497
14498 if (info->eh_frame_hdr_type
14499 && !bfd_link_relocatable (info)
14500 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14501 changed = 1;
14502
14503 return changed;
14504 }
14505
14506 bfd_boolean
14507 _bfd_elf_section_already_linked (bfd *abfd,
14508 asection *sec,
14509 struct bfd_link_info *info)
14510 {
14511 flagword flags;
14512 const char *name, *key;
14513 struct bfd_section_already_linked *l;
14514 struct bfd_section_already_linked_hash_entry *already_linked_list;
14515
14516 if (sec->output_section == bfd_abs_section_ptr)
14517 return FALSE;
14518
14519 flags = sec->flags;
14520
14521 /* Return if it isn't a linkonce section. A comdat group section
14522 also has SEC_LINK_ONCE set. */
14523 if ((flags & SEC_LINK_ONCE) == 0)
14524 return FALSE;
14525
14526 /* Don't put group member sections on our list of already linked
14527 sections. They are handled as a group via their group section. */
14528 if (elf_sec_group (sec) != NULL)
14529 return FALSE;
14530
14531 /* For a SHT_GROUP section, use the group signature as the key. */
14532 name = sec->name;
14533 if ((flags & SEC_GROUP) != 0
14534 && elf_next_in_group (sec) != NULL
14535 && elf_group_name (elf_next_in_group (sec)) != NULL)
14536 key = elf_group_name (elf_next_in_group (sec));
14537 else
14538 {
14539 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14540 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14541 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14542 key++;
14543 else
14544 /* Must be a user linkonce section that doesn't follow gcc's
14545 naming convention. In this case we won't be matching
14546 single member groups. */
14547 key = name;
14548 }
14549
14550 already_linked_list = bfd_section_already_linked_table_lookup (key);
14551
14552 for (l = already_linked_list->entry; l != NULL; l = l->next)
14553 {
14554 /* We may have 2 different types of sections on the list: group
14555 sections with a signature of <key> (<key> is some string),
14556 and linkonce sections named .gnu.linkonce.<type>.<key>.
14557 Match like sections. LTO plugin sections are an exception.
14558 They are always named .gnu.linkonce.t.<key> and match either
14559 type of section. */
14560 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14561 && ((flags & SEC_GROUP) != 0
14562 || strcmp (name, l->sec->name) == 0))
14563 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14564 || (sec->owner->flags & BFD_PLUGIN) != 0)
14565 {
14566 /* The section has already been linked. See if we should
14567 issue a warning. */
14568 if (!_bfd_handle_already_linked (sec, l, info))
14569 return FALSE;
14570
14571 if (flags & SEC_GROUP)
14572 {
14573 asection *first = elf_next_in_group (sec);
14574 asection *s = first;
14575
14576 while (s != NULL)
14577 {
14578 s->output_section = bfd_abs_section_ptr;
14579 /* Record which group discards it. */
14580 s->kept_section = l->sec;
14581 s = elf_next_in_group (s);
14582 /* These lists are circular. */
14583 if (s == first)
14584 break;
14585 }
14586 }
14587
14588 return TRUE;
14589 }
14590 }
14591
14592 /* A single member comdat group section may be discarded by a
14593 linkonce section and vice versa. */
14594 if ((flags & SEC_GROUP) != 0)
14595 {
14596 asection *first = elf_next_in_group (sec);
14597
14598 if (first != NULL && elf_next_in_group (first) == first)
14599 /* Check this single member group against linkonce sections. */
14600 for (l = already_linked_list->entry; l != NULL; l = l->next)
14601 if ((l->sec->flags & SEC_GROUP) == 0
14602 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14603 {
14604 first->output_section = bfd_abs_section_ptr;
14605 first->kept_section = l->sec;
14606 sec->output_section = bfd_abs_section_ptr;
14607 break;
14608 }
14609 }
14610 else
14611 /* Check this linkonce section against single member groups. */
14612 for (l = already_linked_list->entry; l != NULL; l = l->next)
14613 if (l->sec->flags & SEC_GROUP)
14614 {
14615 asection *first = elf_next_in_group (l->sec);
14616
14617 if (first != NULL
14618 && elf_next_in_group (first) == first
14619 && bfd_elf_match_symbols_in_sections (first, sec, info))
14620 {
14621 sec->output_section = bfd_abs_section_ptr;
14622 sec->kept_section = first;
14623 break;
14624 }
14625 }
14626
14627 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14628 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14629 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14630 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14631 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14632 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14633 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14634 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14635 The reverse order cannot happen as there is never a bfd with only the
14636 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14637 matter as here were are looking only for cross-bfd sections. */
14638
14639 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14640 for (l = already_linked_list->entry; l != NULL; l = l->next)
14641 if ((l->sec->flags & SEC_GROUP) == 0
14642 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14643 {
14644 if (abfd != l->sec->owner)
14645 sec->output_section = bfd_abs_section_ptr;
14646 break;
14647 }
14648
14649 /* This is the first section with this name. Record it. */
14650 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14651 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14652 return sec->output_section == bfd_abs_section_ptr;
14653 }
14654
14655 bfd_boolean
14656 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14657 {
14658 return sym->st_shndx == SHN_COMMON;
14659 }
14660
14661 unsigned int
14662 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14663 {
14664 return SHN_COMMON;
14665 }
14666
14667 asection *
14668 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14669 {
14670 return bfd_com_section_ptr;
14671 }
14672
14673 bfd_vma
14674 _bfd_elf_default_got_elt_size (bfd *abfd,
14675 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14676 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14677 bfd *ibfd ATTRIBUTE_UNUSED,
14678 unsigned long symndx ATTRIBUTE_UNUSED)
14679 {
14680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14681 return bed->s->arch_size / 8;
14682 }
14683
14684 /* Routines to support the creation of dynamic relocs. */
14685
14686 /* Returns the name of the dynamic reloc section associated with SEC. */
14687
14688 static const char *
14689 get_dynamic_reloc_section_name (bfd * abfd,
14690 asection * sec,
14691 bfd_boolean is_rela)
14692 {
14693 char *name;
14694 const char *old_name = bfd_section_name (sec);
14695 const char *prefix = is_rela ? ".rela" : ".rel";
14696
14697 if (old_name == NULL)
14698 return NULL;
14699
14700 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14701 sprintf (name, "%s%s", prefix, old_name);
14702
14703 return name;
14704 }
14705
14706 /* Returns the dynamic reloc section associated with SEC.
14707 If necessary compute the name of the dynamic reloc section based
14708 on SEC's name (looked up in ABFD's string table) and the setting
14709 of IS_RELA. */
14710
14711 asection *
14712 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14713 asection * sec,
14714 bfd_boolean is_rela)
14715 {
14716 asection * reloc_sec = elf_section_data (sec)->sreloc;
14717
14718 if (reloc_sec == NULL)
14719 {
14720 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14721
14722 if (name != NULL)
14723 {
14724 reloc_sec = bfd_get_linker_section (abfd, name);
14725
14726 if (reloc_sec != NULL)
14727 elf_section_data (sec)->sreloc = reloc_sec;
14728 }
14729 }
14730
14731 return reloc_sec;
14732 }
14733
14734 /* Returns the dynamic reloc section associated with SEC. If the
14735 section does not exist it is created and attached to the DYNOBJ
14736 bfd and stored in the SRELOC field of SEC's elf_section_data
14737 structure.
14738
14739 ALIGNMENT is the alignment for the newly created section and
14740 IS_RELA defines whether the name should be .rela.<SEC's name>
14741 or .rel.<SEC's name>. The section name is looked up in the
14742 string table associated with ABFD. */
14743
14744 asection *
14745 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14746 bfd *dynobj,
14747 unsigned int alignment,
14748 bfd *abfd,
14749 bfd_boolean is_rela)
14750 {
14751 asection * reloc_sec = elf_section_data (sec)->sreloc;
14752
14753 if (reloc_sec == NULL)
14754 {
14755 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14756
14757 if (name == NULL)
14758 return NULL;
14759
14760 reloc_sec = bfd_get_linker_section (dynobj, name);
14761
14762 if (reloc_sec == NULL)
14763 {
14764 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14765 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14766 if ((sec->flags & SEC_ALLOC) != 0)
14767 flags |= SEC_ALLOC | SEC_LOAD;
14768
14769 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14770 if (reloc_sec != NULL)
14771 {
14772 /* _bfd_elf_get_sec_type_attr chooses a section type by
14773 name. Override as it may be wrong, eg. for a user
14774 section named "auto" we'll get ".relauto" which is
14775 seen to be a .rela section. */
14776 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14777 if (!bfd_set_section_alignment (reloc_sec, alignment))
14778 reloc_sec = NULL;
14779 }
14780 }
14781
14782 elf_section_data (sec)->sreloc = reloc_sec;
14783 }
14784
14785 return reloc_sec;
14786 }
14787
14788 /* Copy the ELF symbol type and other attributes for a linker script
14789 assignment from HSRC to HDEST. Generally this should be treated as
14790 if we found a strong non-dynamic definition for HDEST (except that
14791 ld ignores multiple definition errors). */
14792 void
14793 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14794 struct bfd_link_hash_entry *hdest,
14795 struct bfd_link_hash_entry *hsrc)
14796 {
14797 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14798 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14799 Elf_Internal_Sym isym;
14800
14801 ehdest->type = ehsrc->type;
14802 ehdest->target_internal = ehsrc->target_internal;
14803
14804 isym.st_other = ehsrc->other;
14805 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14806 }
14807
14808 /* Append a RELA relocation REL to section S in BFD. */
14809
14810 void
14811 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14812 {
14813 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14814 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14815 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14816 bed->s->swap_reloca_out (abfd, rel, loc);
14817 }
14818
14819 /* Append a REL relocation REL to section S in BFD. */
14820
14821 void
14822 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14823 {
14824 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14825 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14826 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14827 bed->s->swap_reloc_out (abfd, rel, loc);
14828 }
14829
14830 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14831
14832 struct bfd_link_hash_entry *
14833 bfd_elf_define_start_stop (struct bfd_link_info *info,
14834 const char *symbol, asection *sec)
14835 {
14836 struct elf_link_hash_entry *h;
14837
14838 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14839 FALSE, FALSE, TRUE);
14840 /* NB: Common symbols will be turned into definition later. */
14841 if (h != NULL
14842 && (h->root.type == bfd_link_hash_undefined
14843 || h->root.type == bfd_link_hash_undefweak
14844 || ((h->ref_regular || h->def_dynamic)
14845 && !h->def_regular
14846 && h->root.type != bfd_link_hash_common)))
14847 {
14848 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14849 h->verinfo.verdef = NULL;
14850 h->root.type = bfd_link_hash_defined;
14851 h->root.u.def.section = sec;
14852 h->root.u.def.value = 0;
14853 h->def_regular = 1;
14854 h->def_dynamic = 0;
14855 h->start_stop = 1;
14856 h->u2.start_stop_section = sec;
14857 if (symbol[0] == '.')
14858 {
14859 /* .startof. and .sizeof. symbols are local. */
14860 const struct elf_backend_data *bed;
14861 bed = get_elf_backend_data (info->output_bfd);
14862 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14863 }
14864 else
14865 {
14866 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14867 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
14868 | info->start_stop_visibility);
14869 if (was_dynamic)
14870 bfd_elf_link_record_dynamic_symbol (info, h);
14871 }
14872 return &h->root;
14873 }
14874 return NULL;
14875 }
14876
14877 /* Find dynamic relocs for H that apply to read-only sections. */
14878
14879 asection *
14880 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
14881 {
14882 struct elf_dyn_relocs *p;
14883
14884 for (p = h->dyn_relocs; p != NULL; p = p->next)
14885 {
14886 asection *s = p->sec->output_section;
14887
14888 if (s != NULL && (s->flags & SEC_READONLY) != 0)
14889 return p->sec;
14890 }
14891 return NULL;
14892 }
14893
14894 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
14895 read-only sections. */
14896
14897 bfd_boolean
14898 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
14899 {
14900 asection *sec;
14901
14902 if (h->root.type == bfd_link_hash_indirect)
14903 return TRUE;
14904
14905 sec = _bfd_elf_readonly_dynrelocs (h);
14906 if (sec != NULL)
14907 {
14908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14909
14910 info->flags |= DF_TEXTREL;
14911 /* xgettext:c-format */
14912 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
14913 "in read-only section `%pA'\n"),
14914 sec->owner, h->root.root.string, sec);
14915
14916 if (bfd_link_textrel_check (info))
14917 /* xgettext:c-format */
14918 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
14919 "in read-only section `%pA'\n"),
14920 sec->owner, h->root.root.string, sec);
14921
14922 /* Not an error, just cut short the traversal. */
14923 return FALSE;
14924 }
14925 return TRUE;
14926 }
14927
14928 /* Add dynamic tags. */
14929
14930 bfd_boolean
14931 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
14932 bfd_boolean need_dynamic_reloc)
14933 {
14934 struct elf_link_hash_table *htab = elf_hash_table (info);
14935
14936 if (htab->dynamic_sections_created)
14937 {
14938 /* Add some entries to the .dynamic section. We fill in the
14939 values later, in finish_dynamic_sections, but we must add
14940 the entries now so that we get the correct size for the
14941 .dynamic section. The DT_DEBUG entry is filled in by the
14942 dynamic linker and used by the debugger. */
14943 #define add_dynamic_entry(TAG, VAL) \
14944 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14945
14946 const struct elf_backend_data *bed
14947 = get_elf_backend_data (output_bfd);
14948
14949 if (bfd_link_executable (info))
14950 {
14951 if (!add_dynamic_entry (DT_DEBUG, 0))
14952 return FALSE;
14953 }
14954
14955 if (htab->dt_pltgot_required || htab->splt->size != 0)
14956 {
14957 /* DT_PLTGOT is used by prelink even if there is no PLT
14958 relocation. */
14959 if (!add_dynamic_entry (DT_PLTGOT, 0))
14960 return FALSE;
14961 }
14962
14963 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
14964 {
14965 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
14966 || !add_dynamic_entry (DT_PLTREL,
14967 (bed->rela_plts_and_copies_p
14968 ? DT_RELA : DT_REL))
14969 || !add_dynamic_entry (DT_JMPREL, 0))
14970 return FALSE;
14971 }
14972
14973 if (htab->tlsdesc_plt
14974 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
14975 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
14976 return FALSE;
14977
14978 if (need_dynamic_reloc)
14979 {
14980 if (bed->rela_plts_and_copies_p)
14981 {
14982 if (!add_dynamic_entry (DT_RELA, 0)
14983 || !add_dynamic_entry (DT_RELASZ, 0)
14984 || !add_dynamic_entry (DT_RELAENT,
14985 bed->s->sizeof_rela))
14986 return FALSE;
14987 }
14988 else
14989 {
14990 if (!add_dynamic_entry (DT_REL, 0)
14991 || !add_dynamic_entry (DT_RELSZ, 0)
14992 || !add_dynamic_entry (DT_RELENT,
14993 bed->s->sizeof_rel))
14994 return FALSE;
14995 }
14996
14997 /* If any dynamic relocs apply to a read-only section,
14998 then we need a DT_TEXTREL entry. */
14999 if ((info->flags & DF_TEXTREL) == 0)
15000 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15001 info);
15002
15003 if ((info->flags & DF_TEXTREL) != 0)
15004 {
15005 if (htab->ifunc_resolvers)
15006 info->callbacks->einfo
15007 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15008 "may result in a segfault at runtime; recompile with %s\n"),
15009 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15010
15011 if (!add_dynamic_entry (DT_TEXTREL, 0))
15012 return FALSE;
15013 }
15014 }
15015 }
15016 #undef add_dynamic_entry
15017
15018 return TRUE;
15019 }