link_hash_copy_indirect and symbol flags
[deliverable/binutils-gdb.git] / bfd / elflink.c
... / ...
CommitLineData
1/* ELF linking support for BFD.
2 Copyright (C) 1995-2016 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 "bfd_stdint.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
28#include "safe-ctype.h"
29#include "libiberty.h"
30#include "objalloc.h"
31#if BFD_SUPPORTS_PLUGINS
32#include "plugin-api.h"
33#include "plugin.h"
34#endif
35
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
132 h->non_elf = 0;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 return h;
140}
141
142bfd_boolean
143_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
144{
145 flagword flags;
146 asection *s;
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
150
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200}
201\f
202/* Create a strtab to hold the dynamic symbol names. */
203static bfd_boolean
204_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205{
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236}
237
238/* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245bfd_boolean
246_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247{
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361}
362
363/* Create dynamic sections when linking against a dynamic object. */
364
365bfd_boolean
366_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367{
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430 htab->sdynbss = s;
431
432 if (bed->want_dynrelro)
433 {
434 /* Similarly, but for symbols that were originally in read-only
435 sections. */
436 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
437 (SEC_ALLOC | SEC_READONLY
438 | SEC_HAS_CONTENTS
439 | SEC_LINKER_CREATED));
440 if (s == NULL)
441 return FALSE;
442 htab->sdynrelro = s;
443 }
444
445 /* The .rel[a].bss section holds copy relocs. This section is not
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
455 copy relocs. */
456 if (bfd_link_executable (info))
457 {
458 s = bfd_make_section_anyway_with_flags (abfd,
459 (bed->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags | SEC_READONLY);
462 if (s == NULL
463 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
464 return FALSE;
465 htab->srelbss = s;
466
467 if (bed->want_dynrelro)
468 {
469 s = (bfd_make_section_anyway_with_flags
470 (abfd, (bed->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags | SEC_READONLY));
473 if (s == NULL
474 || ! bfd_set_section_alignment (abfd, s,
475 bed->s->log_file_align))
476 return FALSE;
477 htab->sreldynrelro = s;
478 }
479 }
480 }
481
482 return TRUE;
483}
484\f
485/* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
491 one. */
492
493bfd_boolean
494bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
495 struct elf_link_hash_entry *h)
496{
497 if (h->dynindx == -1)
498 {
499 struct elf_strtab_hash *dynstr;
500 char *p;
501 const char *name;
502 size_t indx;
503
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h->other))
509 {
510 case STV_INTERNAL:
511 case STV_HIDDEN:
512 if (h->root.type != bfd_link_hash_undefined
513 && h->root.type != bfd_link_hash_undefweak)
514 {
515 h->forced_local = 1;
516 if (!elf_hash_table (info)->is_relocatable_executable)
517 return TRUE;
518 }
519
520 default:
521 break;
522 }
523
524 h->dynindx = elf_hash_table (info)->dynsymcount;
525 ++elf_hash_table (info)->dynsymcount;
526
527 dynstr = elf_hash_table (info)->dynstr;
528 if (dynstr == NULL)
529 {
530 /* Create a strtab to hold the dynamic symbol names. */
531 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
532 if (dynstr == NULL)
533 return FALSE;
534 }
535
536 /* We don't put any version information in the dynamic string
537 table. */
538 name = h->root.root.string;
539 p = strchr (name, ELF_VER_CHR);
540 if (p != NULL)
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
546 *p = 0;
547
548 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
549
550 if (p != NULL)
551 *p = ELF_VER_CHR;
552
553 if (indx == (size_t) -1)
554 return FALSE;
555 h->dynstr_index = indx;
556 }
557
558 return TRUE;
559}
560\f
561/* Mark a symbol dynamic. */
562
563static void
564bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
565 struct elf_link_hash_entry *h,
566 Elf_Internal_Sym *sym)
567{
568 struct bfd_elf_dynamic_list *d = info->dynamic_list;
569
570 /* It may be called more than once on the same H. */
571 if(h->dynamic || bfd_link_relocatable (info))
572 return;
573
574 if ((info->dynamic_data
575 && (h->type == STT_OBJECT
576 || h->type == STT_COMMON
577 || (sym != NULL
578 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
579 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
580 || (d != NULL
581 && h->root.type == bfd_link_hash_new
582 && (*d->match) (&d->head, NULL, h->root.root.string)))
583 h->dynamic = 1;
584}
585
586/* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
588
589bfd_boolean
590bfd_elf_record_link_assignment (bfd *output_bfd,
591 struct bfd_link_info *info,
592 const char *name,
593 bfd_boolean provide,
594 bfd_boolean hidden)
595{
596 struct elf_link_hash_entry *h, *hv;
597 struct elf_link_hash_table *htab;
598 const struct elf_backend_data *bed;
599
600 if (!is_elf_hash_table (info->hash))
601 return TRUE;
602
603 htab = elf_hash_table (info);
604 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
605 if (h == NULL)
606 return provide;
607
608 if (h->root.type == bfd_link_hash_warning)
609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
610
611 if (h->versioned == unknown)
612 {
613 /* Set versioned if symbol version is unknown. */
614 char *version = strrchr (name, ELF_VER_CHR);
615 if (version)
616 {
617 if (version > name && version[-1] != ELF_VER_CHR)
618 h->versioned = versioned_hidden;
619 else
620 h->versioned = versioned;
621 }
622 }
623
624 switch (h->root.type)
625 {
626 case bfd_link_hash_defined:
627 case bfd_link_hash_defweak:
628 case bfd_link_hash_common:
629 break;
630 case bfd_link_hash_undefweak:
631 case bfd_link_hash_undefined:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
635 h->root.type = bfd_link_hash_new;
636 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
637 bfd_link_repair_undef_list (&htab->root);
638 break;
639 case bfd_link_hash_new:
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 h->non_elf = 0;
642 break;
643 case bfd_link_hash_indirect:
644 /* We had a versioned symbol in a dynamic library. We make the
645 the versioned symbol point to this one. */
646 bed = get_elf_backend_data (output_bfd);
647 hv = h;
648 while (hv->root.type == bfd_link_hash_indirect
649 || hv->root.type == bfd_link_hash_warning)
650 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
651 /* We don't need to update h->root.u since linker will set them
652 later. */
653 h->root.type = bfd_link_hash_undefined;
654 hv->root.type = bfd_link_hash_indirect;
655 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
656 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
657 break;
658 default:
659 BFD_FAIL ();
660 return FALSE;
661 }
662
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
667 if (provide
668 && h->def_dynamic
669 && !h->def_regular)
670 h->root.type = bfd_link_hash_undefined;
671
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
674 then clear out any version information because the symbol will not be
675 associated with the dynamic object any more. */
676 if (!provide
677 && h->def_dynamic
678 && !h->def_regular)
679 h->verinfo.verdef = NULL;
680
681 h->def_regular = 1;
682
683 if (hidden)
684 {
685 bed = get_elf_backend_data (output_bfd);
686 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
687 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
688 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
689 }
690
691 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
692 and executables. */
693 if (!bfd_link_relocatable (info)
694 && h->dynindx != -1
695 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
696 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
697 h->forced_local = 1;
698
699 if ((h->def_dynamic
700 || h->ref_dynamic
701 || bfd_link_dll (info)
702 || elf_hash_table (info)->is_relocatable_executable)
703 && h->dynindx == -1)
704 {
705 if (! bfd_elf_link_record_dynamic_symbol (info, h))
706 return FALSE;
707
708 /* If this is a weak defined symbol, and we know a corresponding
709 real symbol from the same dynamic object, make sure the real
710 symbol is also made into a dynamic symbol. */
711 if (h->u.weakdef != NULL
712 && h->u.weakdef->dynindx == -1)
713 {
714 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
715 return FALSE;
716 }
717 }
718
719 return TRUE;
720}
721
722/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
723 success, and 2 on a failure caused by attempting to record a symbol
724 in a discarded section, eg. a discarded link-once section symbol. */
725
726int
727bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
728 bfd *input_bfd,
729 long input_indx)
730{
731 bfd_size_type amt;
732 struct elf_link_local_dynamic_entry *entry;
733 struct elf_link_hash_table *eht;
734 struct elf_strtab_hash *dynstr;
735 size_t dynstr_index;
736 char *name;
737 Elf_External_Sym_Shndx eshndx;
738 char esym[sizeof (Elf64_External_Sym)];
739
740 if (! is_elf_hash_table (info->hash))
741 return 0;
742
743 /* See if the entry exists already. */
744 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
745 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
746 return 1;
747
748 amt = sizeof (*entry);
749 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
750 if (entry == NULL)
751 return 0;
752
753 /* Go find the symbol, so that we can find it's name. */
754 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
755 1, input_indx, &entry->isym, esym, &eshndx))
756 {
757 bfd_release (input_bfd, entry);
758 return 0;
759 }
760
761 if (entry->isym.st_shndx != SHN_UNDEF
762 && entry->isym.st_shndx < SHN_LORESERVE)
763 {
764 asection *s;
765
766 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
767 if (s == NULL || bfd_is_abs_section (s->output_section))
768 {
769 /* We can still bfd_release here as nothing has done another
770 bfd_alloc. We can't do this later in this function. */
771 bfd_release (input_bfd, entry);
772 return 2;
773 }
774 }
775
776 name = (bfd_elf_string_from_elf_section
777 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
778 entry->isym.st_name));
779
780 dynstr = elf_hash_table (info)->dynstr;
781 if (dynstr == NULL)
782 {
783 /* Create a strtab to hold the dynamic symbol names. */
784 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
785 if (dynstr == NULL)
786 return 0;
787 }
788
789 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
790 if (dynstr_index == (size_t) -1)
791 return 0;
792 entry->isym.st_name = dynstr_index;
793
794 eht = elf_hash_table (info);
795
796 entry->next = eht->dynlocal;
797 eht->dynlocal = entry;
798 entry->input_bfd = input_bfd;
799 entry->input_indx = input_indx;
800 eht->dynsymcount++;
801
802 /* Whatever binding the symbol had before, it's now local. */
803 entry->isym.st_info
804 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
805
806 /* The dynindx will be set at the end of size_dynamic_sections. */
807
808 return 1;
809}
810
811/* Return the dynindex of a local dynamic symbol. */
812
813long
814_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
815 bfd *input_bfd,
816 long input_indx)
817{
818 struct elf_link_local_dynamic_entry *e;
819
820 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
821 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
822 return e->dynindx;
823 return -1;
824}
825
826/* This function is used to renumber the dynamic symbols, if some of
827 them are removed because they are marked as local. This is called
828 via elf_link_hash_traverse. */
829
830static bfd_boolean
831elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
832 void *data)
833{
834 size_t *count = (size_t *) data;
835
836 if (h->forced_local)
837 return TRUE;
838
839 if (h->dynindx != -1)
840 h->dynindx = ++(*count);
841
842 return TRUE;
843}
844
845
846/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
847 STB_LOCAL binding. */
848
849static bfd_boolean
850elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
852{
853 size_t *count = (size_t *) data;
854
855 if (!h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862}
863
864/* Return true if the dynamic symbol for a given section should be
865 omitted when creating a shared library. */
866bfd_boolean
867_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
868 struct bfd_link_info *info,
869 asection *p)
870{
871 struct elf_link_hash_table *htab;
872 asection *ip;
873
874 switch (elf_section_data (p)->this_hdr.sh_type)
875 {
876 case SHT_PROGBITS:
877 case SHT_NOBITS:
878 /* If sh_type is yet undecided, assume it could be
879 SHT_PROGBITS/SHT_NOBITS. */
880 case SHT_NULL:
881 htab = elf_hash_table (info);
882 if (p == htab->tls_sec)
883 return FALSE;
884
885 if (htab->text_index_section != NULL)
886 return p != htab->text_index_section && p != htab->data_index_section;
887
888 return (htab->dynobj != NULL
889 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
890 && ip->output_section == p);
891
892 /* There shouldn't be section relative relocations
893 against any other section. */
894 default:
895 return TRUE;
896 }
897}
898
899/* Assign dynsym indices. In a shared library we generate a section
900 symbol for each output section, which come first. Next come symbols
901 which have been forced to local binding. Then all of the back-end
902 allocated local dynamic syms, followed by the rest of the global
903 symbols. */
904
905static unsigned long
906_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
907 struct bfd_link_info *info,
908 unsigned long *section_sym_count)
909{
910 unsigned long dynsymcount = 0;
911
912 if (bfd_link_pic (info)
913 || elf_hash_table (info)->is_relocatable_executable)
914 {
915 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
916 asection *p;
917 for (p = output_bfd->sections; p ; p = p->next)
918 if ((p->flags & SEC_EXCLUDE) == 0
919 && (p->flags & SEC_ALLOC) != 0
920 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
921 elf_section_data (p)->dynindx = ++dynsymcount;
922 else
923 elf_section_data (p)->dynindx = 0;
924 }
925 *section_sym_count = dynsymcount;
926
927 elf_link_hash_traverse (elf_hash_table (info),
928 elf_link_renumber_local_hash_table_dynsyms,
929 &dynsymcount);
930
931 if (elf_hash_table (info)->dynlocal)
932 {
933 struct elf_link_local_dynamic_entry *p;
934 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
935 p->dynindx = ++dynsymcount;
936 }
937 elf_hash_table (info)->local_dynsymcount = dynsymcount;
938
939 elf_link_hash_traverse (elf_hash_table (info),
940 elf_link_renumber_hash_table_dynsyms,
941 &dynsymcount);
942
943 /* There is an unused NULL entry at the head of the table which we
944 must account for in our count even if the table is empty since it
945 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
946 .dynamic section. */
947 dynsymcount++;
948
949 elf_hash_table (info)->dynsymcount = dynsymcount;
950 return dynsymcount;
951}
952
953/* Merge st_other field. */
954
955static void
956elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
957 const Elf_Internal_Sym *isym, asection *sec,
958 bfd_boolean definition, bfd_boolean dynamic)
959{
960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
961
962 /* If st_other has a processor-specific meaning, specific
963 code might be needed here. */
964 if (bed->elf_backend_merge_symbol_attribute)
965 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
966 dynamic);
967
968 if (!dynamic)
969 {
970 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
971 unsigned hvis = ELF_ST_VISIBILITY (h->other);
972
973 /* Keep the most constraining visibility. Leave the remainder
974 of the st_other field to elf_backend_merge_symbol_attribute. */
975 if (symvis - 1 < hvis - 1)
976 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
977 }
978 else if (definition
979 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
980 && (sec->flags & SEC_READONLY) == 0)
981 h->protected_def = 1;
982}
983
984/* This function is called when we want to merge a new symbol with an
985 existing symbol. It handles the various cases which arise when we
986 find a definition in a dynamic object, or when there is already a
987 definition in a dynamic object. The new symbol is described by
988 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
989 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
990 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
991 of an old common symbol. We set OVERRIDE if the old symbol is
992 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
993 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
994 to change. By OK to change, we mean that we shouldn't warn if the
995 type or size does change. */
996
997static bfd_boolean
998_bfd_elf_merge_symbol (bfd *abfd,
999 struct bfd_link_info *info,
1000 const char *name,
1001 Elf_Internal_Sym *sym,
1002 asection **psec,
1003 bfd_vma *pvalue,
1004 struct elf_link_hash_entry **sym_hash,
1005 bfd **poldbfd,
1006 bfd_boolean *pold_weak,
1007 unsigned int *pold_alignment,
1008 bfd_boolean *skip,
1009 bfd_boolean *override,
1010 bfd_boolean *type_change_ok,
1011 bfd_boolean *size_change_ok,
1012 bfd_boolean *matched)
1013{
1014 asection *sec, *oldsec;
1015 struct elf_link_hash_entry *h;
1016 struct elf_link_hash_entry *hi;
1017 struct elf_link_hash_entry *flip;
1018 int bind;
1019 bfd *oldbfd;
1020 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1021 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1022 const struct elf_backend_data *bed;
1023 char *new_version;
1024
1025 *skip = FALSE;
1026 *override = FALSE;
1027
1028 sec = *psec;
1029 bind = ELF_ST_BIND (sym->st_info);
1030
1031 if (! bfd_is_und_section (sec))
1032 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1033 else
1034 h = ((struct elf_link_hash_entry *)
1035 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1036 if (h == NULL)
1037 return FALSE;
1038 *sym_hash = h;
1039
1040 bed = get_elf_backend_data (abfd);
1041
1042 /* NEW_VERSION is the symbol version of the new symbol. */
1043 if (h->versioned != unversioned)
1044 {
1045 /* Symbol version is unknown or versioned. */
1046 new_version = strrchr (name, ELF_VER_CHR);
1047 if (new_version)
1048 {
1049 if (h->versioned == unknown)
1050 {
1051 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1052 h->versioned = versioned_hidden;
1053 else
1054 h->versioned = versioned;
1055 }
1056 new_version += 1;
1057 if (new_version[0] == '\0')
1058 new_version = NULL;
1059 }
1060 else
1061 h->versioned = unversioned;
1062 }
1063 else
1064 new_version = NULL;
1065
1066 /* For merging, we only care about real symbols. But we need to make
1067 sure that indirect symbol dynamic flags are updated. */
1068 hi = h;
1069 while (h->root.type == bfd_link_hash_indirect
1070 || h->root.type == bfd_link_hash_warning)
1071 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1072
1073 if (!*matched)
1074 {
1075 if (hi == h || h->root.type == bfd_link_hash_new)
1076 *matched = TRUE;
1077 else
1078 {
1079 /* OLD_HIDDEN is true if the existing symbol is only visible
1080 to the symbol with the same symbol version. NEW_HIDDEN is
1081 true if the new symbol is only visible to the symbol with
1082 the same symbol version. */
1083 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1084 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1085 if (!old_hidden && !new_hidden)
1086 /* The new symbol matches the existing symbol if both
1087 aren't hidden. */
1088 *matched = TRUE;
1089 else
1090 {
1091 /* OLD_VERSION is the symbol version of the existing
1092 symbol. */
1093 char *old_version;
1094
1095 if (h->versioned >= versioned)
1096 old_version = strrchr (h->root.root.string,
1097 ELF_VER_CHR) + 1;
1098 else
1099 old_version = NULL;
1100
1101 /* The new symbol matches the existing symbol if they
1102 have the same symbol version. */
1103 *matched = (old_version == new_version
1104 || (old_version != NULL
1105 && new_version != NULL
1106 && strcmp (old_version, new_version) == 0));
1107 }
1108 }
1109 }
1110
1111 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1112 existing symbol. */
1113
1114 oldbfd = NULL;
1115 oldsec = NULL;
1116 switch (h->root.type)
1117 {
1118 default:
1119 break;
1120
1121 case bfd_link_hash_undefined:
1122 case bfd_link_hash_undefweak:
1123 oldbfd = h->root.u.undef.abfd;
1124 break;
1125
1126 case bfd_link_hash_defined:
1127 case bfd_link_hash_defweak:
1128 oldbfd = h->root.u.def.section->owner;
1129 oldsec = h->root.u.def.section;
1130 break;
1131
1132 case bfd_link_hash_common:
1133 oldbfd = h->root.u.c.p->section->owner;
1134 oldsec = h->root.u.c.p->section;
1135 if (pold_alignment)
1136 *pold_alignment = h->root.u.c.p->alignment_power;
1137 break;
1138 }
1139 if (poldbfd && *poldbfd == NULL)
1140 *poldbfd = oldbfd;
1141
1142 /* Differentiate strong and weak symbols. */
1143 newweak = bind == STB_WEAK;
1144 oldweak = (h->root.type == bfd_link_hash_defweak
1145 || h->root.type == bfd_link_hash_undefweak);
1146 if (pold_weak)
1147 *pold_weak = oldweak;
1148
1149 /* This code is for coping with dynamic objects, and is only useful
1150 if we are doing an ELF link. */
1151 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1152 return TRUE;
1153
1154 /* We have to check it for every instance since the first few may be
1155 references and not all compilers emit symbol type for undefined
1156 symbols. */
1157 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1158
1159 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1160 respectively, is from a dynamic object. */
1161
1162 newdyn = (abfd->flags & DYNAMIC) != 0;
1163
1164 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1165 syms and defined syms in dynamic libraries respectively.
1166 ref_dynamic on the other hand can be set for a symbol defined in
1167 a dynamic library, and def_dynamic may not be set; When the
1168 definition in a dynamic lib is overridden by a definition in the
1169 executable use of the symbol in the dynamic lib becomes a
1170 reference to the executable symbol. */
1171 if (newdyn)
1172 {
1173 if (bfd_is_und_section (sec))
1174 {
1175 if (bind != STB_WEAK)
1176 {
1177 h->ref_dynamic_nonweak = 1;
1178 hi->ref_dynamic_nonweak = 1;
1179 }
1180 }
1181 else
1182 {
1183 /* Update the existing symbol only if they match. */
1184 if (*matched)
1185 h->dynamic_def = 1;
1186 hi->dynamic_def = 1;
1187 }
1188 }
1189
1190 /* If we just created the symbol, mark it as being an ELF symbol.
1191 Other than that, there is nothing to do--there is no merge issue
1192 with a newly defined symbol--so we just return. */
1193
1194 if (h->root.type == bfd_link_hash_new)
1195 {
1196 h->non_elf = 0;
1197 return TRUE;
1198 }
1199
1200 /* In cases involving weak versioned symbols, we may wind up trying
1201 to merge a symbol with itself. Catch that here, to avoid the
1202 confusion that results if we try to override a symbol with
1203 itself. The additional tests catch cases like
1204 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1205 dynamic object, which we do want to handle here. */
1206 if (abfd == oldbfd
1207 && (newweak || oldweak)
1208 && ((abfd->flags & DYNAMIC) == 0
1209 || !h->def_regular))
1210 return TRUE;
1211
1212 olddyn = FALSE;
1213 if (oldbfd != NULL)
1214 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1215 else if (oldsec != NULL)
1216 {
1217 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1218 indices used by MIPS ELF. */
1219 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1220 }
1221
1222 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1223 respectively, appear to be a definition rather than reference. */
1224
1225 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1226
1227 olddef = (h->root.type != bfd_link_hash_undefined
1228 && h->root.type != bfd_link_hash_undefweak
1229 && h->root.type != bfd_link_hash_common);
1230
1231 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1232 respectively, appear to be a function. */
1233
1234 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1235 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1236
1237 oldfunc = (h->type != STT_NOTYPE
1238 && bed->is_function_type (h->type));
1239
1240 /* If creating a default indirect symbol ("foo" or "foo@") from a
1241 dynamic versioned definition ("foo@@") skip doing so if there is
1242 an existing regular definition with a different type. We don't
1243 want, for example, a "time" variable in the executable overriding
1244 a "time" function in a shared library. */
1245 if (pold_alignment == NULL
1246 && newdyn
1247 && newdef
1248 && !olddyn
1249 && (olddef || h->root.type == bfd_link_hash_common)
1250 && ELF_ST_TYPE (sym->st_info) != h->type
1251 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1252 && h->type != STT_NOTYPE
1253 && !(newfunc && oldfunc))
1254 {
1255 *skip = TRUE;
1256 return TRUE;
1257 }
1258
1259 /* Check TLS symbols. We don't check undefined symbols introduced
1260 by "ld -u" which have no type (and oldbfd NULL), and we don't
1261 check symbols from plugins because they also have no type. */
1262 if (oldbfd != NULL
1263 && (oldbfd->flags & BFD_PLUGIN) == 0
1264 && (abfd->flags & BFD_PLUGIN) == 0
1265 && ELF_ST_TYPE (sym->st_info) != h->type
1266 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1267 {
1268 bfd *ntbfd, *tbfd;
1269 bfd_boolean ntdef, tdef;
1270 asection *ntsec, *tsec;
1271
1272 if (h->type == STT_TLS)
1273 {
1274 ntbfd = abfd;
1275 ntsec = sec;
1276 ntdef = newdef;
1277 tbfd = oldbfd;
1278 tsec = oldsec;
1279 tdef = olddef;
1280 }
1281 else
1282 {
1283 ntbfd = oldbfd;
1284 ntsec = oldsec;
1285 ntdef = olddef;
1286 tbfd = abfd;
1287 tsec = sec;
1288 tdef = newdef;
1289 }
1290
1291 if (tdef && ntdef)
1292 _bfd_error_handler
1293 /* xgettext:c-format */
1294 (_("%s: TLS definition in %B section %A "
1295 "mismatches non-TLS definition in %B section %A"),
1296 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1297 else if (!tdef && !ntdef)
1298 _bfd_error_handler
1299 /* xgettext:c-format */
1300 (_("%s: TLS reference in %B "
1301 "mismatches non-TLS reference in %B"),
1302 tbfd, ntbfd, h->root.root.string);
1303 else if (tdef)
1304 _bfd_error_handler
1305 /* xgettext:c-format */
1306 (_("%s: TLS definition in %B section %A "
1307 "mismatches non-TLS reference in %B"),
1308 tbfd, tsec, ntbfd, h->root.root.string);
1309 else
1310 _bfd_error_handler
1311 /* xgettext:c-format */
1312 (_("%s: TLS reference in %B "
1313 "mismatches non-TLS definition in %B section %A"),
1314 tbfd, ntbfd, ntsec, h->root.root.string);
1315
1316 bfd_set_error (bfd_error_bad_value);
1317 return FALSE;
1318 }
1319
1320 /* If the old symbol has non-default visibility, we ignore the new
1321 definition from a dynamic object. */
1322 if (newdyn
1323 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1324 && !bfd_is_und_section (sec))
1325 {
1326 *skip = TRUE;
1327 /* Make sure this symbol is dynamic. */
1328 h->ref_dynamic = 1;
1329 hi->ref_dynamic = 1;
1330 /* A protected symbol has external availability. Make sure it is
1331 recorded as dynamic.
1332
1333 FIXME: Should we check type and size for protected symbol? */
1334 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1335 return bfd_elf_link_record_dynamic_symbol (info, h);
1336 else
1337 return TRUE;
1338 }
1339 else if (!newdyn
1340 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1341 && h->def_dynamic)
1342 {
1343 /* If the new symbol with non-default visibility comes from a
1344 relocatable file and the old definition comes from a dynamic
1345 object, we remove the old definition. */
1346 if (hi->root.type == bfd_link_hash_indirect)
1347 {
1348 /* Handle the case where the old dynamic definition is
1349 default versioned. We need to copy the symbol info from
1350 the symbol with default version to the normal one if it
1351 was referenced before. */
1352 if (h->ref_regular)
1353 {
1354 hi->root.type = h->root.type;
1355 h->root.type = bfd_link_hash_indirect;
1356 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1357
1358 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1359 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1360 {
1361 /* If the new symbol is hidden or internal, completely undo
1362 any dynamic link state. */
1363 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1364 h->forced_local = 0;
1365 h->ref_dynamic = 0;
1366 }
1367 else
1368 h->ref_dynamic = 1;
1369
1370 h->def_dynamic = 0;
1371 /* FIXME: Should we check type and size for protected symbol? */
1372 h->size = 0;
1373 h->type = 0;
1374
1375 h = hi;
1376 }
1377 else
1378 h = hi;
1379 }
1380
1381 /* If the old symbol was undefined before, then it will still be
1382 on the undefs list. If the new symbol is undefined or
1383 common, we can't make it bfd_link_hash_new here, because new
1384 undefined or common symbols will be added to the undefs list
1385 by _bfd_generic_link_add_one_symbol. Symbols may not be
1386 added twice to the undefs list. Also, if the new symbol is
1387 undefweak then we don't want to lose the strong undef. */
1388 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1389 {
1390 h->root.type = bfd_link_hash_undefined;
1391 h->root.u.undef.abfd = abfd;
1392 }
1393 else
1394 {
1395 h->root.type = bfd_link_hash_new;
1396 h->root.u.undef.abfd = NULL;
1397 }
1398
1399 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1400 {
1401 /* If the new symbol is hidden or internal, completely undo
1402 any dynamic link state. */
1403 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1404 h->forced_local = 0;
1405 h->ref_dynamic = 0;
1406 }
1407 else
1408 h->ref_dynamic = 1;
1409 h->def_dynamic = 0;
1410 /* FIXME: Should we check type and size for protected symbol? */
1411 h->size = 0;
1412 h->type = 0;
1413 return TRUE;
1414 }
1415
1416 /* If a new weak symbol definition comes from a regular file and the
1417 old symbol comes from a dynamic library, we treat the new one as
1418 strong. Similarly, an old weak symbol definition from a regular
1419 file is treated as strong when the new symbol comes from a dynamic
1420 library. Further, an old weak symbol from a dynamic library is
1421 treated as strong if the new symbol is from a dynamic library.
1422 This reflects the way glibc's ld.so works.
1423
1424 Do this before setting *type_change_ok or *size_change_ok so that
1425 we warn properly when dynamic library symbols are overridden. */
1426
1427 if (newdef && !newdyn && olddyn)
1428 newweak = FALSE;
1429 if (olddef && newdyn)
1430 oldweak = FALSE;
1431
1432 /* Allow changes between different types of function symbol. */
1433 if (newfunc && oldfunc)
1434 *type_change_ok = TRUE;
1435
1436 /* It's OK to change the type if either the existing symbol or the
1437 new symbol is weak. A type change is also OK if the old symbol
1438 is undefined and the new symbol is defined. */
1439
1440 if (oldweak
1441 || newweak
1442 || (newdef
1443 && h->root.type == bfd_link_hash_undefined))
1444 *type_change_ok = TRUE;
1445
1446 /* It's OK to change the size if either the existing symbol or the
1447 new symbol is weak, or if the old symbol is undefined. */
1448
1449 if (*type_change_ok
1450 || h->root.type == bfd_link_hash_undefined)
1451 *size_change_ok = TRUE;
1452
1453 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1454 symbol, respectively, appears to be a common symbol in a dynamic
1455 object. If a symbol appears in an uninitialized section, and is
1456 not weak, and is not a function, then it may be a common symbol
1457 which was resolved when the dynamic object was created. We want
1458 to treat such symbols specially, because they raise special
1459 considerations when setting the symbol size: if the symbol
1460 appears as a common symbol in a regular object, and the size in
1461 the regular object is larger, we must make sure that we use the
1462 larger size. This problematic case can always be avoided in C,
1463 but it must be handled correctly when using Fortran shared
1464 libraries.
1465
1466 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1467 likewise for OLDDYNCOMMON and OLDDEF.
1468
1469 Note that this test is just a heuristic, and that it is quite
1470 possible to have an uninitialized symbol in a shared object which
1471 is really a definition, rather than a common symbol. This could
1472 lead to some minor confusion when the symbol really is a common
1473 symbol in some regular object. However, I think it will be
1474 harmless. */
1475
1476 if (newdyn
1477 && newdef
1478 && !newweak
1479 && (sec->flags & SEC_ALLOC) != 0
1480 && (sec->flags & SEC_LOAD) == 0
1481 && sym->st_size > 0
1482 && !newfunc)
1483 newdyncommon = TRUE;
1484 else
1485 newdyncommon = FALSE;
1486
1487 if (olddyn
1488 && olddef
1489 && h->root.type == bfd_link_hash_defined
1490 && h->def_dynamic
1491 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1492 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1493 && h->size > 0
1494 && !oldfunc)
1495 olddyncommon = TRUE;
1496 else
1497 olddyncommon = FALSE;
1498
1499 /* We now know everything about the old and new symbols. We ask the
1500 backend to check if we can merge them. */
1501 if (bed->merge_symbol != NULL)
1502 {
1503 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1504 return FALSE;
1505 sec = *psec;
1506 }
1507
1508 /* If both the old and the new symbols look like common symbols in a
1509 dynamic object, set the size of the symbol to the larger of the
1510 two. */
1511
1512 if (olddyncommon
1513 && newdyncommon
1514 && sym->st_size != h->size)
1515 {
1516 /* Since we think we have two common symbols, issue a multiple
1517 common warning if desired. Note that we only warn if the
1518 size is different. If the size is the same, we simply let
1519 the old symbol override the new one as normally happens with
1520 symbols defined in dynamic objects. */
1521
1522 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1523 bfd_link_hash_common, sym->st_size);
1524 if (sym->st_size > h->size)
1525 h->size = sym->st_size;
1526
1527 *size_change_ok = TRUE;
1528 }
1529
1530 /* If we are looking at a dynamic object, and we have found a
1531 definition, we need to see if the symbol was already defined by
1532 some other object. If so, we want to use the existing
1533 definition, and we do not want to report a multiple symbol
1534 definition error; we do this by clobbering *PSEC to be
1535 bfd_und_section_ptr.
1536
1537 We treat a common symbol as a definition if the symbol in the
1538 shared library is a function, since common symbols always
1539 represent variables; this can cause confusion in principle, but
1540 any such confusion would seem to indicate an erroneous program or
1541 shared library. We also permit a common symbol in a regular
1542 object to override a weak symbol in a shared object. A common
1543 symbol in executable also overrides a symbol in a shared object. */
1544
1545 if (newdyn
1546 && newdef
1547 && (olddef
1548 || (h->root.type == bfd_link_hash_common
1549 && (newweak
1550 || newfunc
1551 || (!olddyn && bfd_link_executable (info))))))
1552 {
1553 *override = TRUE;
1554 newdef = FALSE;
1555 newdyncommon = FALSE;
1556
1557 *psec = sec = bfd_und_section_ptr;
1558 *size_change_ok = TRUE;
1559
1560 /* If we get here when the old symbol is a common symbol, then
1561 we are explicitly letting it override a weak symbol or
1562 function in a dynamic object, and we don't want to warn about
1563 a type change. If the old symbol is a defined symbol, a type
1564 change warning may still be appropriate. */
1565
1566 if (h->root.type == bfd_link_hash_common)
1567 *type_change_ok = TRUE;
1568 }
1569
1570 /* Handle the special case of an old common symbol merging with a
1571 new symbol which looks like a common symbol in a shared object.
1572 We change *PSEC and *PVALUE to make the new symbol look like a
1573 common symbol, and let _bfd_generic_link_add_one_symbol do the
1574 right thing. */
1575
1576 if (newdyncommon
1577 && h->root.type == bfd_link_hash_common)
1578 {
1579 *override = TRUE;
1580 newdef = FALSE;
1581 newdyncommon = FALSE;
1582 *pvalue = sym->st_size;
1583 *psec = sec = bed->common_section (oldsec);
1584 *size_change_ok = TRUE;
1585 }
1586
1587 /* Skip weak definitions of symbols that are already defined. */
1588 if (newdef && olddef && newweak)
1589 {
1590 /* Don't skip new non-IR weak syms. */
1591 if (!(oldbfd != NULL
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
1593 && (abfd->flags & BFD_PLUGIN) == 0))
1594 {
1595 newdef = FALSE;
1596 *skip = TRUE;
1597 }
1598
1599 /* Merge st_other. If the symbol already has a dynamic index,
1600 but visibility says it should not be visible, turn it into a
1601 local symbol. */
1602 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1603 if (h->dynindx != -1)
1604 switch (ELF_ST_VISIBILITY (h->other))
1605 {
1606 case STV_INTERNAL:
1607 case STV_HIDDEN:
1608 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1609 break;
1610 }
1611 }
1612
1613 /* If the old symbol is from a dynamic object, and the new symbol is
1614 a definition which is not from a dynamic object, then the new
1615 symbol overrides the old symbol. Symbols from regular files
1616 always take precedence over symbols from dynamic objects, even if
1617 they are defined after the dynamic object in the link.
1618
1619 As above, we again permit a common symbol in a regular object to
1620 override a definition in a shared object if the shared object
1621 symbol is a function or is weak. */
1622
1623 flip = NULL;
1624 if (!newdyn
1625 && (newdef
1626 || (bfd_is_com_section (sec)
1627 && (oldweak || oldfunc)))
1628 && olddyn
1629 && olddef
1630 && h->def_dynamic)
1631 {
1632 /* Change the hash table entry to undefined, and let
1633 _bfd_generic_link_add_one_symbol do the right thing with the
1634 new definition. */
1635
1636 h->root.type = bfd_link_hash_undefined;
1637 h->root.u.undef.abfd = h->root.u.def.section->owner;
1638 *size_change_ok = TRUE;
1639
1640 olddef = FALSE;
1641 olddyncommon = FALSE;
1642
1643 /* We again permit a type change when a common symbol may be
1644 overriding a function. */
1645
1646 if (bfd_is_com_section (sec))
1647 {
1648 if (oldfunc)
1649 {
1650 /* If a common symbol overrides a function, make sure
1651 that it isn't defined dynamically nor has type
1652 function. */
1653 h->def_dynamic = 0;
1654 h->type = STT_NOTYPE;
1655 }
1656 *type_change_ok = TRUE;
1657 }
1658
1659 if (hi->root.type == bfd_link_hash_indirect)
1660 flip = hi;
1661 else
1662 /* This union may have been set to be non-NULL when this symbol
1663 was seen in a dynamic object. We must force the union to be
1664 NULL, so that it is correct for a regular symbol. */
1665 h->verinfo.vertree = NULL;
1666 }
1667
1668 /* Handle the special case of a new common symbol merging with an
1669 old symbol that looks like it might be a common symbol defined in
1670 a shared object. Note that we have already handled the case in
1671 which a new common symbol should simply override the definition
1672 in the shared library. */
1673
1674 if (! newdyn
1675 && bfd_is_com_section (sec)
1676 && olddyncommon)
1677 {
1678 /* It would be best if we could set the hash table entry to a
1679 common symbol, but we don't know what to use for the section
1680 or the alignment. */
1681 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1682 bfd_link_hash_common, sym->st_size);
1683
1684 /* If the presumed common symbol in the dynamic object is
1685 larger, pretend that the new symbol has its size. */
1686
1687 if (h->size > *pvalue)
1688 *pvalue = h->size;
1689
1690 /* We need to remember the alignment required by the symbol
1691 in the dynamic object. */
1692 BFD_ASSERT (pold_alignment);
1693 *pold_alignment = h->root.u.def.section->alignment_power;
1694
1695 olddef = FALSE;
1696 olddyncommon = FALSE;
1697
1698 h->root.type = bfd_link_hash_undefined;
1699 h->root.u.undef.abfd = h->root.u.def.section->owner;
1700
1701 *size_change_ok = TRUE;
1702 *type_change_ok = TRUE;
1703
1704 if (hi->root.type == bfd_link_hash_indirect)
1705 flip = hi;
1706 else
1707 h->verinfo.vertree = NULL;
1708 }
1709
1710 if (flip != NULL)
1711 {
1712 /* Handle the case where we had a versioned symbol in a dynamic
1713 library and now find a definition in a normal object. In this
1714 case, we make the versioned symbol point to the normal one. */
1715 flip->root.type = h->root.type;
1716 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1717 h->root.type = bfd_link_hash_indirect;
1718 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1719 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1720 if (h->def_dynamic)
1721 {
1722 h->def_dynamic = 0;
1723 flip->ref_dynamic = 1;
1724 }
1725 }
1726
1727 return TRUE;
1728}
1729
1730/* This function is called to create an indirect symbol from the
1731 default for the symbol with the default version if needed. The
1732 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1733 set DYNSYM if the new indirect symbol is dynamic. */
1734
1735static bfd_boolean
1736_bfd_elf_add_default_symbol (bfd *abfd,
1737 struct bfd_link_info *info,
1738 struct elf_link_hash_entry *h,
1739 const char *name,
1740 Elf_Internal_Sym *sym,
1741 asection *sec,
1742 bfd_vma value,
1743 bfd **poldbfd,
1744 bfd_boolean *dynsym)
1745{
1746 bfd_boolean type_change_ok;
1747 bfd_boolean size_change_ok;
1748 bfd_boolean skip;
1749 char *shortname;
1750 struct elf_link_hash_entry *hi;
1751 struct bfd_link_hash_entry *bh;
1752 const struct elf_backend_data *bed;
1753 bfd_boolean collect;
1754 bfd_boolean dynamic;
1755 bfd_boolean override;
1756 char *p;
1757 size_t len, shortlen;
1758 asection *tmp_sec;
1759 bfd_boolean matched;
1760
1761 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1762 return TRUE;
1763
1764 /* If this symbol has a version, and it is the default version, we
1765 create an indirect symbol from the default name to the fully
1766 decorated name. This will cause external references which do not
1767 specify a version to be bound to this version of the symbol. */
1768 p = strchr (name, ELF_VER_CHR);
1769 if (h->versioned == unknown)
1770 {
1771 if (p == NULL)
1772 {
1773 h->versioned = unversioned;
1774 return TRUE;
1775 }
1776 else
1777 {
1778 if (p[1] != ELF_VER_CHR)
1779 {
1780 h->versioned = versioned_hidden;
1781 return TRUE;
1782 }
1783 else
1784 h->versioned = versioned;
1785 }
1786 }
1787 else
1788 {
1789 /* PR ld/19073: We may see an unversioned definition after the
1790 default version. */
1791 if (p == NULL)
1792 return TRUE;
1793 }
1794
1795 bed = get_elf_backend_data (abfd);
1796 collect = bed->collect;
1797 dynamic = (abfd->flags & DYNAMIC) != 0;
1798
1799 shortlen = p - name;
1800 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1801 if (shortname == NULL)
1802 return FALSE;
1803 memcpy (shortname, name, shortlen);
1804 shortname[shortlen] = '\0';
1805
1806 /* We are going to create a new symbol. Merge it with any existing
1807 symbol with this name. For the purposes of the merge, act as
1808 though we were defining the symbol we just defined, although we
1809 actually going to define an indirect symbol. */
1810 type_change_ok = FALSE;
1811 size_change_ok = FALSE;
1812 matched = TRUE;
1813 tmp_sec = sec;
1814 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1815 &hi, poldbfd, NULL, NULL, &skip, &override,
1816 &type_change_ok, &size_change_ok, &matched))
1817 return FALSE;
1818
1819 if (skip)
1820 goto nondefault;
1821
1822 if (hi->def_regular)
1823 {
1824 /* If the undecorated symbol will have a version added by a
1825 script different to H, then don't indirect to/from the
1826 undecorated symbol. This isn't ideal because we may not yet
1827 have seen symbol versions, if given by a script on the
1828 command line rather than via --version-script. */
1829 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1830 {
1831 bfd_boolean hide;
1832
1833 hi->verinfo.vertree
1834 = bfd_find_version_for_sym (info->version_info,
1835 hi->root.root.string, &hide);
1836 if (hi->verinfo.vertree != NULL && hide)
1837 {
1838 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1839 goto nondefault;
1840 }
1841 }
1842 if (hi->verinfo.vertree != NULL
1843 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1844 goto nondefault;
1845 }
1846
1847 if (! override)
1848 {
1849 /* Add the default symbol if not performing a relocatable link. */
1850 if (! bfd_link_relocatable (info))
1851 {
1852 bh = &hi->root;
1853 if (! (_bfd_generic_link_add_one_symbol
1854 (info, abfd, shortname, BSF_INDIRECT,
1855 bfd_ind_section_ptr,
1856 0, name, FALSE, collect, &bh)))
1857 return FALSE;
1858 hi = (struct elf_link_hash_entry *) bh;
1859 }
1860 }
1861 else
1862 {
1863 /* In this case the symbol named SHORTNAME is overriding the
1864 indirect symbol we want to add. We were planning on making
1865 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1866 is the name without a version. NAME is the fully versioned
1867 name, and it is the default version.
1868
1869 Overriding means that we already saw a definition for the
1870 symbol SHORTNAME in a regular object, and it is overriding
1871 the symbol defined in the dynamic object.
1872
1873 When this happens, we actually want to change NAME, the
1874 symbol we just added, to refer to SHORTNAME. This will cause
1875 references to NAME in the shared object to become references
1876 to SHORTNAME in the regular object. This is what we expect
1877 when we override a function in a shared object: that the
1878 references in the shared object will be mapped to the
1879 definition in the regular object. */
1880
1881 while (hi->root.type == bfd_link_hash_indirect
1882 || hi->root.type == bfd_link_hash_warning)
1883 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1884
1885 h->root.type = bfd_link_hash_indirect;
1886 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1887 if (h->def_dynamic)
1888 {
1889 h->def_dynamic = 0;
1890 hi->ref_dynamic = 1;
1891 if (hi->ref_regular
1892 || hi->def_regular)
1893 {
1894 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1895 return FALSE;
1896 }
1897 }
1898
1899 /* Now set HI to H, so that the following code will set the
1900 other fields correctly. */
1901 hi = h;
1902 }
1903
1904 /* Check if HI is a warning symbol. */
1905 if (hi->root.type == bfd_link_hash_warning)
1906 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1907
1908 /* If there is a duplicate definition somewhere, then HI may not
1909 point to an indirect symbol. We will have reported an error to
1910 the user in that case. */
1911
1912 if (hi->root.type == bfd_link_hash_indirect)
1913 {
1914 struct elf_link_hash_entry *ht;
1915
1916 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1917 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1918
1919 /* A reference to the SHORTNAME symbol from a dynamic library
1920 will be satisfied by the versioned symbol at runtime. In
1921 effect, we have a reference to the versioned symbol. */
1922 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1923 hi->dynamic_def |= ht->dynamic_def;
1924
1925 /* See if the new flags lead us to realize that the symbol must
1926 be dynamic. */
1927 if (! *dynsym)
1928 {
1929 if (! dynamic)
1930 {
1931 if (! bfd_link_executable (info)
1932 || hi->def_dynamic
1933 || hi->ref_dynamic)
1934 *dynsym = TRUE;
1935 }
1936 else
1937 {
1938 if (hi->ref_regular)
1939 *dynsym = TRUE;
1940 }
1941 }
1942 }
1943
1944 /* We also need to define an indirection from the nondefault version
1945 of the symbol. */
1946
1947nondefault:
1948 len = strlen (name);
1949 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1950 if (shortname == NULL)
1951 return FALSE;
1952 memcpy (shortname, name, shortlen);
1953 memcpy (shortname + shortlen, p + 1, len - shortlen);
1954
1955 /* Once again, merge with any existing symbol. */
1956 type_change_ok = FALSE;
1957 size_change_ok = FALSE;
1958 tmp_sec = sec;
1959 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1960 &hi, poldbfd, NULL, NULL, &skip, &override,
1961 &type_change_ok, &size_change_ok, &matched))
1962 return FALSE;
1963
1964 if (skip)
1965 return TRUE;
1966
1967 if (override)
1968 {
1969 /* Here SHORTNAME is a versioned name, so we don't expect to see
1970 the type of override we do in the case above unless it is
1971 overridden by a versioned definition. */
1972 if (hi->root.type != bfd_link_hash_defined
1973 && hi->root.type != bfd_link_hash_defweak)
1974 _bfd_error_handler
1975 /* xgettext:c-format */
1976 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1977 abfd, shortname);
1978 }
1979 else
1980 {
1981 bh = &hi->root;
1982 if (! (_bfd_generic_link_add_one_symbol
1983 (info, abfd, shortname, BSF_INDIRECT,
1984 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1985 return FALSE;
1986 hi = (struct elf_link_hash_entry *) bh;
1987
1988 /* If there is a duplicate definition somewhere, then HI may not
1989 point to an indirect symbol. We will have reported an error
1990 to the user in that case. */
1991
1992 if (hi->root.type == bfd_link_hash_indirect)
1993 {
1994 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1995 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1996 hi->dynamic_def |= h->dynamic_def;
1997
1998 /* See if the new flags lead us to realize that the symbol
1999 must be dynamic. */
2000 if (! *dynsym)
2001 {
2002 if (! dynamic)
2003 {
2004 if (! bfd_link_executable (info)
2005 || hi->ref_dynamic)
2006 *dynsym = TRUE;
2007 }
2008 else
2009 {
2010 if (hi->ref_regular)
2011 *dynsym = TRUE;
2012 }
2013 }
2014 }
2015 }
2016
2017 return TRUE;
2018}
2019\f
2020/* This routine is used to export all defined symbols into the dynamic
2021 symbol table. It is called via elf_link_hash_traverse. */
2022
2023static bfd_boolean
2024_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2025{
2026 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2027
2028 /* Ignore indirect symbols. These are added by the versioning code. */
2029 if (h->root.type == bfd_link_hash_indirect)
2030 return TRUE;
2031
2032 /* Ignore this if we won't export it. */
2033 if (!eif->info->export_dynamic && !h->dynamic)
2034 return TRUE;
2035
2036 if (h->dynindx == -1
2037 && (h->def_regular || h->ref_regular)
2038 && ! bfd_hide_sym_by_version (eif->info->version_info,
2039 h->root.root.string))
2040 {
2041 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2042 {
2043 eif->failed = TRUE;
2044 return FALSE;
2045 }
2046 }
2047
2048 return TRUE;
2049}
2050\f
2051/* Look through the symbols which are defined in other shared
2052 libraries and referenced here. Update the list of version
2053 dependencies. This will be put into the .gnu.version_r section.
2054 This function is called via elf_link_hash_traverse. */
2055
2056static bfd_boolean
2057_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2058 void *data)
2059{
2060 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2061 Elf_Internal_Verneed *t;
2062 Elf_Internal_Vernaux *a;
2063 bfd_size_type amt;
2064
2065 /* We only care about symbols defined in shared objects with version
2066 information. */
2067 if (!h->def_dynamic
2068 || h->def_regular
2069 || h->dynindx == -1
2070 || h->verinfo.verdef == NULL
2071 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2072 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2073 return TRUE;
2074
2075 /* See if we already know about this version. */
2076 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2077 t != NULL;
2078 t = t->vn_nextref)
2079 {
2080 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2081 continue;
2082
2083 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2084 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2085 return TRUE;
2086
2087 break;
2088 }
2089
2090 /* This is a new version. Add it to tree we are building. */
2091
2092 if (t == NULL)
2093 {
2094 amt = sizeof *t;
2095 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2096 if (t == NULL)
2097 {
2098 rinfo->failed = TRUE;
2099 return FALSE;
2100 }
2101
2102 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2103 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2104 elf_tdata (rinfo->info->output_bfd)->verref = t;
2105 }
2106
2107 amt = sizeof *a;
2108 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2109 if (a == NULL)
2110 {
2111 rinfo->failed = TRUE;
2112 return FALSE;
2113 }
2114
2115 /* Note that we are copying a string pointer here, and testing it
2116 above. If bfd_elf_string_from_elf_section is ever changed to
2117 discard the string data when low in memory, this will have to be
2118 fixed. */
2119 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2120
2121 a->vna_flags = h->verinfo.verdef->vd_flags;
2122 a->vna_nextptr = t->vn_auxptr;
2123
2124 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2125 ++rinfo->vers;
2126
2127 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2128
2129 t->vn_auxptr = a;
2130
2131 return TRUE;
2132}
2133
2134/* Figure out appropriate versions for all the symbols. We may not
2135 have the version number script until we have read all of the input
2136 files, so until that point we don't know which symbols should be
2137 local. This function is called via elf_link_hash_traverse. */
2138
2139static bfd_boolean
2140_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2141{
2142 struct elf_info_failed *sinfo;
2143 struct bfd_link_info *info;
2144 const struct elf_backend_data *bed;
2145 struct elf_info_failed eif;
2146 char *p;
2147
2148 sinfo = (struct elf_info_failed *) data;
2149 info = sinfo->info;
2150
2151 /* Fix the symbol flags. */
2152 eif.failed = FALSE;
2153 eif.info = info;
2154 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2155 {
2156 if (eif.failed)
2157 sinfo->failed = TRUE;
2158 return FALSE;
2159 }
2160
2161 /* We only need version numbers for symbols defined in regular
2162 objects. */
2163 if (!h->def_regular)
2164 return TRUE;
2165
2166 bed = get_elf_backend_data (info->output_bfd);
2167 p = strchr (h->root.root.string, ELF_VER_CHR);
2168 if (p != NULL && h->verinfo.vertree == NULL)
2169 {
2170 struct bfd_elf_version_tree *t;
2171
2172 ++p;
2173 if (*p == ELF_VER_CHR)
2174 ++p;
2175
2176 /* If there is no version string, we can just return out. */
2177 if (*p == '\0')
2178 return TRUE;
2179
2180 /* Look for the version. If we find it, it is no longer weak. */
2181 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2182 {
2183 if (strcmp (t->name, p) == 0)
2184 {
2185 size_t len;
2186 char *alc;
2187 struct bfd_elf_version_expr *d;
2188
2189 len = p - h->root.root.string;
2190 alc = (char *) bfd_malloc (len);
2191 if (alc == NULL)
2192 {
2193 sinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196 memcpy (alc, h->root.root.string, len - 1);
2197 alc[len - 1] = '\0';
2198 if (alc[len - 2] == ELF_VER_CHR)
2199 alc[len - 2] = '\0';
2200
2201 h->verinfo.vertree = t;
2202 t->used = TRUE;
2203 d = NULL;
2204
2205 if (t->globals.list != NULL)
2206 d = (*t->match) (&t->globals, NULL, alc);
2207
2208 /* See if there is anything to force this symbol to
2209 local scope. */
2210 if (d == NULL && t->locals.list != NULL)
2211 {
2212 d = (*t->match) (&t->locals, NULL, alc);
2213 if (d != NULL
2214 && h->dynindx != -1
2215 && ! info->export_dynamic)
2216 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2217 }
2218
2219 free (alc);
2220 break;
2221 }
2222 }
2223
2224 /* If we are building an application, we need to create a
2225 version node for this version. */
2226 if (t == NULL && bfd_link_executable (info))
2227 {
2228 struct bfd_elf_version_tree **pp;
2229 int version_index;
2230
2231 /* If we aren't going to export this symbol, we don't need
2232 to worry about it. */
2233 if (h->dynindx == -1)
2234 return TRUE;
2235
2236 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2237 sizeof *t);
2238 if (t == NULL)
2239 {
2240 sinfo->failed = TRUE;
2241 return FALSE;
2242 }
2243
2244 t->name = p;
2245 t->name_indx = (unsigned int) -1;
2246 t->used = TRUE;
2247
2248 version_index = 1;
2249 /* Don't count anonymous version tag. */
2250 if (sinfo->info->version_info != NULL
2251 && sinfo->info->version_info->vernum == 0)
2252 version_index = 0;
2253 for (pp = &sinfo->info->version_info;
2254 *pp != NULL;
2255 pp = &(*pp)->next)
2256 ++version_index;
2257 t->vernum = version_index;
2258
2259 *pp = t;
2260
2261 h->verinfo.vertree = t;
2262 }
2263 else if (t == NULL)
2264 {
2265 /* We could not find the version for a symbol when
2266 generating a shared archive. Return an error. */
2267 _bfd_error_handler
2268 /* xgettext:c-format */
2269 (_("%B: version node not found for symbol %s"),
2270 info->output_bfd, h->root.root.string);
2271 bfd_set_error (bfd_error_bad_value);
2272 sinfo->failed = TRUE;
2273 return FALSE;
2274 }
2275 }
2276
2277 /* If we don't have a version for this symbol, see if we can find
2278 something. */
2279 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2280 {
2281 bfd_boolean hide;
2282
2283 h->verinfo.vertree
2284 = bfd_find_version_for_sym (sinfo->info->version_info,
2285 h->root.root.string, &hide);
2286 if (h->verinfo.vertree != NULL && hide)
2287 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2288 }
2289
2290 return TRUE;
2291}
2292\f
2293/* Read and swap the relocs from the section indicated by SHDR. This
2294 may be either a REL or a RELA section. The relocations are
2295 translated into RELA relocations and stored in INTERNAL_RELOCS,
2296 which should have already been allocated to contain enough space.
2297 The EXTERNAL_RELOCS are a buffer where the external form of the
2298 relocations should be stored.
2299
2300 Returns FALSE if something goes wrong. */
2301
2302static bfd_boolean
2303elf_link_read_relocs_from_section (bfd *abfd,
2304 asection *sec,
2305 Elf_Internal_Shdr *shdr,
2306 void *external_relocs,
2307 Elf_Internal_Rela *internal_relocs)
2308{
2309 const struct elf_backend_data *bed;
2310 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2311 const bfd_byte *erela;
2312 const bfd_byte *erelaend;
2313 Elf_Internal_Rela *irela;
2314 Elf_Internal_Shdr *symtab_hdr;
2315 size_t nsyms;
2316
2317 /* Position ourselves at the start of the section. */
2318 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2319 return FALSE;
2320
2321 /* Read the relocations. */
2322 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2323 return FALSE;
2324
2325 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2326 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2327
2328 bed = get_elf_backend_data (abfd);
2329
2330 /* Convert the external relocations to the internal format. */
2331 if (shdr->sh_entsize == bed->s->sizeof_rel)
2332 swap_in = bed->s->swap_reloc_in;
2333 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2334 swap_in = bed->s->swap_reloca_in;
2335 else
2336 {
2337 bfd_set_error (bfd_error_wrong_format);
2338 return FALSE;
2339 }
2340
2341 erela = (const bfd_byte *) external_relocs;
2342 erelaend = erela + shdr->sh_size;
2343 irela = internal_relocs;
2344 while (erela < erelaend)
2345 {
2346 bfd_vma r_symndx;
2347
2348 (*swap_in) (abfd, erela, irela);
2349 r_symndx = ELF32_R_SYM (irela->r_info);
2350 if (bed->s->arch_size == 64)
2351 r_symndx >>= 24;
2352 if (nsyms > 0)
2353 {
2354 if ((size_t) r_symndx >= nsyms)
2355 {
2356 _bfd_error_handler
2357 /* xgettext:c-format */
2358 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2359 " for offset 0x%lx in section `%A'"),
2360 abfd, sec,
2361 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2362 bfd_set_error (bfd_error_bad_value);
2363 return FALSE;
2364 }
2365 }
2366 else if (r_symndx != STN_UNDEF)
2367 {
2368 _bfd_error_handler
2369 /* xgettext:c-format */
2370 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2371 " when the object file has no symbol table"),
2372 abfd, sec,
2373 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2374 bfd_set_error (bfd_error_bad_value);
2375 return FALSE;
2376 }
2377 irela += bed->s->int_rels_per_ext_rel;
2378 erela += shdr->sh_entsize;
2379 }
2380
2381 return TRUE;
2382}
2383
2384/* Read and swap the relocs for a section O. They may have been
2385 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2386 not NULL, they are used as buffers to read into. They are known to
2387 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2388 the return value is allocated using either malloc or bfd_alloc,
2389 according to the KEEP_MEMORY argument. If O has two relocation
2390 sections (both REL and RELA relocations), then the REL_HDR
2391 relocations will appear first in INTERNAL_RELOCS, followed by the
2392 RELA_HDR relocations. */
2393
2394Elf_Internal_Rela *
2395_bfd_elf_link_read_relocs (bfd *abfd,
2396 asection *o,
2397 void *external_relocs,
2398 Elf_Internal_Rela *internal_relocs,
2399 bfd_boolean keep_memory)
2400{
2401 void *alloc1 = NULL;
2402 Elf_Internal_Rela *alloc2 = NULL;
2403 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2404 struct bfd_elf_section_data *esdo = elf_section_data (o);
2405 Elf_Internal_Rela *internal_rela_relocs;
2406
2407 if (esdo->relocs != NULL)
2408 return esdo->relocs;
2409
2410 if (o->reloc_count == 0)
2411 return NULL;
2412
2413 if (internal_relocs == NULL)
2414 {
2415 bfd_size_type size;
2416
2417 size = o->reloc_count;
2418 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2419 if (keep_memory)
2420 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2421 else
2422 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2423 if (internal_relocs == NULL)
2424 goto error_return;
2425 }
2426
2427 if (external_relocs == NULL)
2428 {
2429 bfd_size_type size = 0;
2430
2431 if (esdo->rel.hdr)
2432 size += esdo->rel.hdr->sh_size;
2433 if (esdo->rela.hdr)
2434 size += esdo->rela.hdr->sh_size;
2435
2436 alloc1 = bfd_malloc (size);
2437 if (alloc1 == NULL)
2438 goto error_return;
2439 external_relocs = alloc1;
2440 }
2441
2442 internal_rela_relocs = internal_relocs;
2443 if (esdo->rel.hdr)
2444 {
2445 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2446 external_relocs,
2447 internal_relocs))
2448 goto error_return;
2449 external_relocs = (((bfd_byte *) external_relocs)
2450 + esdo->rel.hdr->sh_size);
2451 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2452 * bed->s->int_rels_per_ext_rel);
2453 }
2454
2455 if (esdo->rela.hdr
2456 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2457 external_relocs,
2458 internal_rela_relocs)))
2459 goto error_return;
2460
2461 /* Cache the results for next time, if we can. */
2462 if (keep_memory)
2463 esdo->relocs = internal_relocs;
2464
2465 if (alloc1 != NULL)
2466 free (alloc1);
2467
2468 /* Don't free alloc2, since if it was allocated we are passing it
2469 back (under the name of internal_relocs). */
2470
2471 return internal_relocs;
2472
2473 error_return:
2474 if (alloc1 != NULL)
2475 free (alloc1);
2476 if (alloc2 != NULL)
2477 {
2478 if (keep_memory)
2479 bfd_release (abfd, alloc2);
2480 else
2481 free (alloc2);
2482 }
2483 return NULL;
2484}
2485
2486/* Compute the size of, and allocate space for, REL_HDR which is the
2487 section header for a section containing relocations for O. */
2488
2489static bfd_boolean
2490_bfd_elf_link_size_reloc_section (bfd *abfd,
2491 struct bfd_elf_section_reloc_data *reldata)
2492{
2493 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2494
2495 /* That allows us to calculate the size of the section. */
2496 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2497
2498 /* The contents field must last into write_object_contents, so we
2499 allocate it with bfd_alloc rather than malloc. Also since we
2500 cannot be sure that the contents will actually be filled in,
2501 we zero the allocated space. */
2502 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2503 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2504 return FALSE;
2505
2506 if (reldata->hashes == NULL && reldata->count)
2507 {
2508 struct elf_link_hash_entry **p;
2509
2510 p = ((struct elf_link_hash_entry **)
2511 bfd_zmalloc (reldata->count * sizeof (*p)));
2512 if (p == NULL)
2513 return FALSE;
2514
2515 reldata->hashes = p;
2516 }
2517
2518 return TRUE;
2519}
2520
2521/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2522 originated from the section given by INPUT_REL_HDR) to the
2523 OUTPUT_BFD. */
2524
2525bfd_boolean
2526_bfd_elf_link_output_relocs (bfd *output_bfd,
2527 asection *input_section,
2528 Elf_Internal_Shdr *input_rel_hdr,
2529 Elf_Internal_Rela *internal_relocs,
2530 struct elf_link_hash_entry **rel_hash
2531 ATTRIBUTE_UNUSED)
2532{
2533 Elf_Internal_Rela *irela;
2534 Elf_Internal_Rela *irelaend;
2535 bfd_byte *erel;
2536 struct bfd_elf_section_reloc_data *output_reldata;
2537 asection *output_section;
2538 const struct elf_backend_data *bed;
2539 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2540 struct bfd_elf_section_data *esdo;
2541
2542 output_section = input_section->output_section;
2543
2544 bed = get_elf_backend_data (output_bfd);
2545 esdo = elf_section_data (output_section);
2546 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2547 {
2548 output_reldata = &esdo->rel;
2549 swap_out = bed->s->swap_reloc_out;
2550 }
2551 else if (esdo->rela.hdr
2552 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2553 {
2554 output_reldata = &esdo->rela;
2555 swap_out = bed->s->swap_reloca_out;
2556 }
2557 else
2558 {
2559 _bfd_error_handler
2560 /* xgettext:c-format */
2561 (_("%B: relocation size mismatch in %B section %A"),
2562 output_bfd, input_section->owner, input_section);
2563 bfd_set_error (bfd_error_wrong_format);
2564 return FALSE;
2565 }
2566
2567 erel = output_reldata->hdr->contents;
2568 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2569 irela = internal_relocs;
2570 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2571 * bed->s->int_rels_per_ext_rel);
2572 while (irela < irelaend)
2573 {
2574 (*swap_out) (output_bfd, irela, erel);
2575 irela += bed->s->int_rels_per_ext_rel;
2576 erel += input_rel_hdr->sh_entsize;
2577 }
2578
2579 /* Bump the counter, so that we know where to add the next set of
2580 relocations. */
2581 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2582
2583 return TRUE;
2584}
2585\f
2586/* Make weak undefined symbols in PIE dynamic. */
2587
2588bfd_boolean
2589_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2590 struct elf_link_hash_entry *h)
2591{
2592 if (bfd_link_pie (info)
2593 && h->dynindx == -1
2594 && h->root.type == bfd_link_hash_undefweak)
2595 return bfd_elf_link_record_dynamic_symbol (info, h);
2596
2597 return TRUE;
2598}
2599
2600/* Fix up the flags for a symbol. This handles various cases which
2601 can only be fixed after all the input files are seen. This is
2602 currently called by both adjust_dynamic_symbol and
2603 assign_sym_version, which is unnecessary but perhaps more robust in
2604 the face of future changes. */
2605
2606static bfd_boolean
2607_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2608 struct elf_info_failed *eif)
2609{
2610 const struct elf_backend_data *bed;
2611
2612 /* If this symbol was mentioned in a non-ELF file, try to set
2613 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2614 permit a non-ELF file to correctly refer to a symbol defined in
2615 an ELF dynamic object. */
2616 if (h->non_elf)
2617 {
2618 while (h->root.type == bfd_link_hash_indirect)
2619 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2620
2621 if (h->root.type != bfd_link_hash_defined
2622 && h->root.type != bfd_link_hash_defweak)
2623 {
2624 h->ref_regular = 1;
2625 h->ref_regular_nonweak = 1;
2626 }
2627 else
2628 {
2629 if (h->root.u.def.section->owner != NULL
2630 && (bfd_get_flavour (h->root.u.def.section->owner)
2631 == bfd_target_elf_flavour))
2632 {
2633 h->ref_regular = 1;
2634 h->ref_regular_nonweak = 1;
2635 }
2636 else
2637 h->def_regular = 1;
2638 }
2639
2640 if (h->dynindx == -1
2641 && (h->def_dynamic
2642 || h->ref_dynamic))
2643 {
2644 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2645 {
2646 eif->failed = TRUE;
2647 return FALSE;
2648 }
2649 }
2650 }
2651 else
2652 {
2653 /* Unfortunately, NON_ELF is only correct if the symbol
2654 was first seen in a non-ELF file. Fortunately, if the symbol
2655 was first seen in an ELF file, we're probably OK unless the
2656 symbol was defined in a non-ELF file. Catch that case here.
2657 FIXME: We're still in trouble if the symbol was first seen in
2658 a dynamic object, and then later in a non-ELF regular object. */
2659 if ((h->root.type == bfd_link_hash_defined
2660 || h->root.type == bfd_link_hash_defweak)
2661 && !h->def_regular
2662 && (h->root.u.def.section->owner != NULL
2663 ? (bfd_get_flavour (h->root.u.def.section->owner)
2664 != bfd_target_elf_flavour)
2665 : (bfd_is_abs_section (h->root.u.def.section)
2666 && !h->def_dynamic)))
2667 h->def_regular = 1;
2668 }
2669
2670 /* Backend specific symbol fixup. */
2671 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2672 if (bed->elf_backend_fixup_symbol
2673 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2674 return FALSE;
2675
2676 /* If this is a final link, and the symbol was defined as a common
2677 symbol in a regular object file, and there was no definition in
2678 any dynamic object, then the linker will have allocated space for
2679 the symbol in a common section but the DEF_REGULAR
2680 flag will not have been set. */
2681 if (h->root.type == bfd_link_hash_defined
2682 && !h->def_regular
2683 && h->ref_regular
2684 && !h->def_dynamic
2685 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2686 h->def_regular = 1;
2687
2688 /* If a weak undefined symbol has non-default visibility, we also
2689 hide it from the dynamic linker. */
2690 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2691 && h->root.type == bfd_link_hash_undefweak)
2692 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2693
2694 /* A hidden versioned symbol in executable should be forced local if
2695 it is is locally defined, not referenced by shared library and not
2696 exported. */
2697 else if (bfd_link_executable (eif->info)
2698 && h->versioned == versioned_hidden
2699 && !eif->info->export_dynamic
2700 && !h->dynamic
2701 && !h->ref_dynamic
2702 && h->def_regular)
2703 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2704
2705 /* If -Bsymbolic was used (which means to bind references to global
2706 symbols to the definition within the shared object), and this
2707 symbol was defined in a regular object, then it actually doesn't
2708 need a PLT entry. Likewise, if the symbol has non-default
2709 visibility. If the symbol has hidden or internal visibility, we
2710 will force it local. */
2711 else if (h->needs_plt
2712 && bfd_link_pic (eif->info)
2713 && is_elf_hash_table (eif->info->hash)
2714 && (SYMBOLIC_BIND (eif->info, h)
2715 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2716 && h->def_regular)
2717 {
2718 bfd_boolean force_local;
2719
2720 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2721 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2722 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2723 }
2724
2725 /* If this is a weak defined symbol in a dynamic object, and we know
2726 the real definition in the dynamic object, copy interesting flags
2727 over to the real definition. */
2728 if (h->u.weakdef != NULL)
2729 {
2730 /* If the real definition is defined by a regular object file,
2731 don't do anything special. See the longer description in
2732 _bfd_elf_adjust_dynamic_symbol, below. */
2733 if (h->u.weakdef->def_regular)
2734 h->u.weakdef = NULL;
2735 else
2736 {
2737 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2738
2739 while (h->root.type == bfd_link_hash_indirect)
2740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2741
2742 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak);
2744 BFD_ASSERT (weakdef->def_dynamic);
2745 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2746 || weakdef->root.type == bfd_link_hash_defweak);
2747 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2748 }
2749 }
2750
2751 return TRUE;
2752}
2753
2754/* Make the backend pick a good value for a dynamic symbol. This is
2755 called via elf_link_hash_traverse, and also calls itself
2756 recursively. */
2757
2758static bfd_boolean
2759_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2760{
2761 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2762 bfd *dynobj;
2763 const struct elf_backend_data *bed;
2764
2765 if (! is_elf_hash_table (eif->info->hash))
2766 return FALSE;
2767
2768 /* Ignore indirect symbols. These are added by the versioning code. */
2769 if (h->root.type == bfd_link_hash_indirect)
2770 return TRUE;
2771
2772 /* Fix the symbol flags. */
2773 if (! _bfd_elf_fix_symbol_flags (h, eif))
2774 return FALSE;
2775
2776 /* If this symbol does not require a PLT entry, and it is not
2777 defined by a dynamic object, or is not referenced by a regular
2778 object, ignore it. We do have to handle a weak defined symbol,
2779 even if no regular object refers to it, if we decided to add it
2780 to the dynamic symbol table. FIXME: Do we normally need to worry
2781 about symbols which are defined by one dynamic object and
2782 referenced by another one? */
2783 if (!h->needs_plt
2784 && h->type != STT_GNU_IFUNC
2785 && (h->def_regular
2786 || !h->def_dynamic
2787 || (!h->ref_regular
2788 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2789 {
2790 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2791 return TRUE;
2792 }
2793
2794 /* If we've already adjusted this symbol, don't do it again. This
2795 can happen via a recursive call. */
2796 if (h->dynamic_adjusted)
2797 return TRUE;
2798
2799 /* Don't look at this symbol again. Note that we must set this
2800 after checking the above conditions, because we may look at a
2801 symbol once, decide not to do anything, and then get called
2802 recursively later after REF_REGULAR is set below. */
2803 h->dynamic_adjusted = 1;
2804
2805 /* If this is a weak definition, and we know a real definition, and
2806 the real symbol is not itself defined by a regular object file,
2807 then get a good value for the real definition. We handle the
2808 real symbol first, for the convenience of the backend routine.
2809
2810 Note that there is a confusing case here. If the real definition
2811 is defined by a regular object file, we don't get the real symbol
2812 from the dynamic object, but we do get the weak symbol. If the
2813 processor backend uses a COPY reloc, then if some routine in the
2814 dynamic object changes the real symbol, we will not see that
2815 change in the corresponding weak symbol. This is the way other
2816 ELF linkers work as well, and seems to be a result of the shared
2817 library model.
2818
2819 I will clarify this issue. Most SVR4 shared libraries define the
2820 variable _timezone and define timezone as a weak synonym. The
2821 tzset call changes _timezone. If you write
2822 extern int timezone;
2823 int _timezone = 5;
2824 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2825 you might expect that, since timezone is a synonym for _timezone,
2826 the same number will print both times. However, if the processor
2827 backend uses a COPY reloc, then actually timezone will be copied
2828 into your process image, and, since you define _timezone
2829 yourself, _timezone will not. Thus timezone and _timezone will
2830 wind up at different memory locations. The tzset call will set
2831 _timezone, leaving timezone unchanged. */
2832
2833 if (h->u.weakdef != NULL)
2834 {
2835 /* If we get to this point, there is an implicit reference to
2836 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2837 h->u.weakdef->ref_regular = 1;
2838
2839 /* Ensure that the backend adjust_dynamic_symbol function sees
2840 H->U.WEAKDEF before H by recursively calling ourselves. */
2841 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2842 return FALSE;
2843 }
2844
2845 /* If a symbol has no type and no size and does not require a PLT
2846 entry, then we are probably about to do the wrong thing here: we
2847 are probably going to create a COPY reloc for an empty object.
2848 This case can arise when a shared object is built with assembly
2849 code, and the assembly code fails to set the symbol type. */
2850 if (h->size == 0
2851 && h->type == STT_NOTYPE
2852 && !h->needs_plt)
2853 _bfd_error_handler
2854 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2855 h->root.root.string);
2856
2857 dynobj = elf_hash_table (eif->info)->dynobj;
2858 bed = get_elf_backend_data (dynobj);
2859
2860 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2861 {
2862 eif->failed = TRUE;
2863 return FALSE;
2864 }
2865
2866 return TRUE;
2867}
2868
2869/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2870 DYNBSS. */
2871
2872bfd_boolean
2873_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2874 struct elf_link_hash_entry *h,
2875 asection *dynbss)
2876{
2877 unsigned int power_of_two;
2878 bfd_vma mask;
2879 asection *sec = h->root.u.def.section;
2880
2881 /* The section aligment of definition is the maximum alignment
2882 requirement of symbols defined in the section. Since we don't
2883 know the symbol alignment requirement, we start with the
2884 maximum alignment and check low bits of the symbol address
2885 for the minimum alignment. */
2886 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2887 mask = ((bfd_vma) 1 << power_of_two) - 1;
2888 while ((h->root.u.def.value & mask) != 0)
2889 {
2890 mask >>= 1;
2891 --power_of_two;
2892 }
2893
2894 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2895 dynbss))
2896 {
2897 /* Adjust the section alignment if needed. */
2898 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2899 power_of_two))
2900 return FALSE;
2901 }
2902
2903 /* We make sure that the symbol will be aligned properly. */
2904 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2905
2906 /* Define the symbol as being at this point in DYNBSS. */
2907 h->root.u.def.section = dynbss;
2908 h->root.u.def.value = dynbss->size;
2909
2910 /* Increment the size of DYNBSS to make room for the symbol. */
2911 dynbss->size += h->size;
2912
2913 /* No error if extern_protected_data is true. */
2914 if (h->protected_def
2915 && (!info->extern_protected_data
2916 || (info->extern_protected_data < 0
2917 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2918 info->callbacks->einfo
2919 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2920 h->root.root.string);
2921
2922 return TRUE;
2923}
2924
2925/* Adjust all external symbols pointing into SEC_MERGE sections
2926 to reflect the object merging within the sections. */
2927
2928static bfd_boolean
2929_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2930{
2931 asection *sec;
2932
2933 if ((h->root.type == bfd_link_hash_defined
2934 || h->root.type == bfd_link_hash_defweak)
2935 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2936 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2937 {
2938 bfd *output_bfd = (bfd *) data;
2939
2940 h->root.u.def.value =
2941 _bfd_merged_section_offset (output_bfd,
2942 &h->root.u.def.section,
2943 elf_section_data (sec)->sec_info,
2944 h->root.u.def.value);
2945 }
2946
2947 return TRUE;
2948}
2949
2950/* Returns false if the symbol referred to by H should be considered
2951 to resolve local to the current module, and true if it should be
2952 considered to bind dynamically. */
2953
2954bfd_boolean
2955_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2956 struct bfd_link_info *info,
2957 bfd_boolean not_local_protected)
2958{
2959 bfd_boolean binding_stays_local_p;
2960 const struct elf_backend_data *bed;
2961 struct elf_link_hash_table *hash_table;
2962
2963 if (h == NULL)
2964 return FALSE;
2965
2966 while (h->root.type == bfd_link_hash_indirect
2967 || h->root.type == bfd_link_hash_warning)
2968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2969
2970 /* If it was forced local, then clearly it's not dynamic. */
2971 if (h->dynindx == -1)
2972 return FALSE;
2973 if (h->forced_local)
2974 return FALSE;
2975
2976 /* Identify the cases where name binding rules say that a
2977 visible symbol resolves locally. */
2978 binding_stays_local_p = (bfd_link_executable (info)
2979 || SYMBOLIC_BIND (info, h));
2980
2981 switch (ELF_ST_VISIBILITY (h->other))
2982 {
2983 case STV_INTERNAL:
2984 case STV_HIDDEN:
2985 return FALSE;
2986
2987 case STV_PROTECTED:
2988 hash_table = elf_hash_table (info);
2989 if (!is_elf_hash_table (hash_table))
2990 return FALSE;
2991
2992 bed = get_elf_backend_data (hash_table->dynobj);
2993
2994 /* Proper resolution for function pointer equality may require
2995 that these symbols perhaps be resolved dynamically, even though
2996 we should be resolving them to the current module. */
2997 if (!not_local_protected || !bed->is_function_type (h->type))
2998 binding_stays_local_p = TRUE;
2999 break;
3000
3001 default:
3002 break;
3003 }
3004
3005 /* If it isn't defined locally, then clearly it's dynamic. */
3006 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3007 return TRUE;
3008
3009 /* Otherwise, the symbol is dynamic if binding rules don't tell
3010 us that it remains local. */
3011 return !binding_stays_local_p;
3012}
3013
3014/* Return true if the symbol referred to by H should be considered
3015 to resolve local to the current module, and false otherwise. Differs
3016 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3017 undefined symbols. The two functions are virtually identical except
3018 for the place where forced_local and dynindx == -1 are tested. If
3019 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3020 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3021 the symbol is local only for defined symbols.
3022 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3023 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3024 treatment of undefined weak symbols. For those that do not make
3025 undefined weak symbols dynamic, both functions may return false. */
3026
3027bfd_boolean
3028_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3029 struct bfd_link_info *info,
3030 bfd_boolean local_protected)
3031{
3032 const struct elf_backend_data *bed;
3033 struct elf_link_hash_table *hash_table;
3034
3035 /* If it's a local sym, of course we resolve locally. */
3036 if (h == NULL)
3037 return TRUE;
3038
3039 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3040 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3041 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3042 return TRUE;
3043
3044 /* Common symbols that become definitions don't get the DEF_REGULAR
3045 flag set, so test it first, and don't bail out. */
3046 if (ELF_COMMON_DEF_P (h))
3047 /* Do nothing. */;
3048 /* If we don't have a definition in a regular file, then we can't
3049 resolve locally. The sym is either undefined or dynamic. */
3050 else if (!h->def_regular)
3051 return FALSE;
3052
3053 /* Forced local symbols resolve locally. */
3054 if (h->forced_local)
3055 return TRUE;
3056
3057 /* As do non-dynamic symbols. */
3058 if (h->dynindx == -1)
3059 return TRUE;
3060
3061 /* At this point, we know the symbol is defined and dynamic. In an
3062 executable it must resolve locally, likewise when building symbolic
3063 shared libraries. */
3064 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3065 return TRUE;
3066
3067 /* Now deal with defined dynamic symbols in shared libraries. Ones
3068 with default visibility might not resolve locally. */
3069 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3070 return FALSE;
3071
3072 hash_table = elf_hash_table (info);
3073 if (!is_elf_hash_table (hash_table))
3074 return TRUE;
3075
3076 bed = get_elf_backend_data (hash_table->dynobj);
3077
3078 /* If extern_protected_data is false, STV_PROTECTED non-function
3079 symbols are local. */
3080 if ((!info->extern_protected_data
3081 || (info->extern_protected_data < 0
3082 && !bed->extern_protected_data))
3083 && !bed->is_function_type (h->type))
3084 return TRUE;
3085
3086 /* Function pointer equality tests may require that STV_PROTECTED
3087 symbols be treated as dynamic symbols. If the address of a
3088 function not defined in an executable is set to that function's
3089 plt entry in the executable, then the address of the function in
3090 a shared library must also be the plt entry in the executable. */
3091 return local_protected;
3092}
3093
3094/* Caches some TLS segment info, and ensures that the TLS segment vma is
3095 aligned. Returns the first TLS output section. */
3096
3097struct bfd_section *
3098_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3099{
3100 struct bfd_section *sec, *tls;
3101 unsigned int align = 0;
3102
3103 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3104 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3105 break;
3106 tls = sec;
3107
3108 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3109 if (sec->alignment_power > align)
3110 align = sec->alignment_power;
3111
3112 elf_hash_table (info)->tls_sec = tls;
3113
3114 /* Ensure the alignment of the first section is the largest alignment,
3115 so that the tls segment starts aligned. */
3116 if (tls != NULL)
3117 tls->alignment_power = align;
3118
3119 return tls;
3120}
3121
3122/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3123static bfd_boolean
3124is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3125 Elf_Internal_Sym *sym)
3126{
3127 const struct elf_backend_data *bed;
3128
3129 /* Local symbols do not count, but target specific ones might. */
3130 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3131 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3132 return FALSE;
3133
3134 bed = get_elf_backend_data (abfd);
3135 /* Function symbols do not count. */
3136 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3137 return FALSE;
3138
3139 /* If the section is undefined, then so is the symbol. */
3140 if (sym->st_shndx == SHN_UNDEF)
3141 return FALSE;
3142
3143 /* If the symbol is defined in the common section, then
3144 it is a common definition and so does not count. */
3145 if (bed->common_definition (sym))
3146 return FALSE;
3147
3148 /* If the symbol is in a target specific section then we
3149 must rely upon the backend to tell us what it is. */
3150 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3151 /* FIXME - this function is not coded yet:
3152
3153 return _bfd_is_global_symbol_definition (abfd, sym);
3154
3155 Instead for now assume that the definition is not global,
3156 Even if this is wrong, at least the linker will behave
3157 in the same way that it used to do. */
3158 return FALSE;
3159
3160 return TRUE;
3161}
3162
3163/* Search the symbol table of the archive element of the archive ABFD
3164 whose archive map contains a mention of SYMDEF, and determine if
3165 the symbol is defined in this element. */
3166static bfd_boolean
3167elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3168{
3169 Elf_Internal_Shdr * hdr;
3170 size_t symcount;
3171 size_t extsymcount;
3172 size_t extsymoff;
3173 Elf_Internal_Sym *isymbuf;
3174 Elf_Internal_Sym *isym;
3175 Elf_Internal_Sym *isymend;
3176 bfd_boolean result;
3177
3178 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3179 if (abfd == NULL)
3180 return FALSE;
3181
3182 if (! bfd_check_format (abfd, bfd_object))
3183 return FALSE;
3184
3185 /* Select the appropriate symbol table. If we don't know if the
3186 object file is an IR object, give linker LTO plugin a chance to
3187 get the correct symbol table. */
3188 if (abfd->plugin_format == bfd_plugin_yes
3189#if BFD_SUPPORTS_PLUGINS
3190 || (abfd->plugin_format == bfd_plugin_unknown
3191 && bfd_link_plugin_object_p (abfd))
3192#endif
3193 )
3194 {
3195 /* Use the IR symbol table if the object has been claimed by
3196 plugin. */
3197 abfd = abfd->plugin_dummy_bfd;
3198 hdr = &elf_tdata (abfd)->symtab_hdr;
3199 }
3200 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3201 hdr = &elf_tdata (abfd)->symtab_hdr;
3202 else
3203 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3204
3205 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3206
3207 /* The sh_info field of the symtab header tells us where the
3208 external symbols start. We don't care about the local symbols. */
3209 if (elf_bad_symtab (abfd))
3210 {
3211 extsymcount = symcount;
3212 extsymoff = 0;
3213 }
3214 else
3215 {
3216 extsymcount = symcount - hdr->sh_info;
3217 extsymoff = hdr->sh_info;
3218 }
3219
3220 if (extsymcount == 0)
3221 return FALSE;
3222
3223 /* Read in the symbol table. */
3224 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3225 NULL, NULL, NULL);
3226 if (isymbuf == NULL)
3227 return FALSE;
3228
3229 /* Scan the symbol table looking for SYMDEF. */
3230 result = FALSE;
3231 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3232 {
3233 const char *name;
3234
3235 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3236 isym->st_name);
3237 if (name == NULL)
3238 break;
3239
3240 if (strcmp (name, symdef->name) == 0)
3241 {
3242 result = is_global_data_symbol_definition (abfd, isym);
3243 break;
3244 }
3245 }
3246
3247 free (isymbuf);
3248
3249 return result;
3250}
3251\f
3252/* Add an entry to the .dynamic table. */
3253
3254bfd_boolean
3255_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3256 bfd_vma tag,
3257 bfd_vma val)
3258{
3259 struct elf_link_hash_table *hash_table;
3260 const struct elf_backend_data *bed;
3261 asection *s;
3262 bfd_size_type newsize;
3263 bfd_byte *newcontents;
3264 Elf_Internal_Dyn dyn;
3265
3266 hash_table = elf_hash_table (info);
3267 if (! is_elf_hash_table (hash_table))
3268 return FALSE;
3269
3270 bed = get_elf_backend_data (hash_table->dynobj);
3271 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3272 BFD_ASSERT (s != NULL);
3273
3274 newsize = s->size + bed->s->sizeof_dyn;
3275 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3276 if (newcontents == NULL)
3277 return FALSE;
3278
3279 dyn.d_tag = tag;
3280 dyn.d_un.d_val = val;
3281 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3282
3283 s->size = newsize;
3284 s->contents = newcontents;
3285
3286 return TRUE;
3287}
3288
3289/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3290 otherwise just check whether one already exists. Returns -1 on error,
3291 1 if a DT_NEEDED tag already exists, and 0 on success. */
3292
3293static int
3294elf_add_dt_needed_tag (bfd *abfd,
3295 struct bfd_link_info *info,
3296 const char *soname,
3297 bfd_boolean do_it)
3298{
3299 struct elf_link_hash_table *hash_table;
3300 size_t strindex;
3301
3302 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3303 return -1;
3304
3305 hash_table = elf_hash_table (info);
3306 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3307 if (strindex == (size_t) -1)
3308 return -1;
3309
3310 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3311 {
3312 asection *sdyn;
3313 const struct elf_backend_data *bed;
3314 bfd_byte *extdyn;
3315
3316 bed = get_elf_backend_data (hash_table->dynobj);
3317 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3318 if (sdyn != NULL)
3319 for (extdyn = sdyn->contents;
3320 extdyn < sdyn->contents + sdyn->size;
3321 extdyn += bed->s->sizeof_dyn)
3322 {
3323 Elf_Internal_Dyn dyn;
3324
3325 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3326 if (dyn.d_tag == DT_NEEDED
3327 && dyn.d_un.d_val == strindex)
3328 {
3329 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3330 return 1;
3331 }
3332 }
3333 }
3334
3335 if (do_it)
3336 {
3337 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3338 return -1;
3339
3340 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3341 return -1;
3342 }
3343 else
3344 /* We were just checking for existence of the tag. */
3345 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3346
3347 return 0;
3348}
3349
3350/* Return true if SONAME is on the needed list between NEEDED and STOP
3351 (or the end of list if STOP is NULL), and needed by a library that
3352 will be loaded. */
3353
3354static bfd_boolean
3355on_needed_list (const char *soname,
3356 struct bfd_link_needed_list *needed,
3357 struct bfd_link_needed_list *stop)
3358{
3359 struct bfd_link_needed_list *look;
3360 for (look = needed; look != stop; look = look->next)
3361 if (strcmp (soname, look->name) == 0
3362 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3363 /* If needed by a library that itself is not directly
3364 needed, recursively check whether that library is
3365 indirectly needed. Since we add DT_NEEDED entries to
3366 the end of the list, library dependencies appear after
3367 the library. Therefore search prior to the current
3368 LOOK, preventing possible infinite recursion. */
3369 || on_needed_list (elf_dt_name (look->by), needed, look)))
3370 return TRUE;
3371
3372 return FALSE;
3373}
3374
3375/* Sort symbol by value, section, and size. */
3376static int
3377elf_sort_symbol (const void *arg1, const void *arg2)
3378{
3379 const struct elf_link_hash_entry *h1;
3380 const struct elf_link_hash_entry *h2;
3381 bfd_signed_vma vdiff;
3382
3383 h1 = *(const struct elf_link_hash_entry **) arg1;
3384 h2 = *(const struct elf_link_hash_entry **) arg2;
3385 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3386 if (vdiff != 0)
3387 return vdiff > 0 ? 1 : -1;
3388 else
3389 {
3390 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3391 if (sdiff != 0)
3392 return sdiff > 0 ? 1 : -1;
3393 }
3394 vdiff = h1->size - h2->size;
3395 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3396}
3397
3398/* This function is used to adjust offsets into .dynstr for
3399 dynamic symbols. This is called via elf_link_hash_traverse. */
3400
3401static bfd_boolean
3402elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3403{
3404 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3405
3406 if (h->dynindx != -1)
3407 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3408 return TRUE;
3409}
3410
3411/* Assign string offsets in .dynstr, update all structures referencing
3412 them. */
3413
3414static bfd_boolean
3415elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3416{
3417 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3418 struct elf_link_local_dynamic_entry *entry;
3419 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3420 bfd *dynobj = hash_table->dynobj;
3421 asection *sdyn;
3422 bfd_size_type size;
3423 const struct elf_backend_data *bed;
3424 bfd_byte *extdyn;
3425
3426 _bfd_elf_strtab_finalize (dynstr);
3427 size = _bfd_elf_strtab_size (dynstr);
3428
3429 bed = get_elf_backend_data (dynobj);
3430 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3431 BFD_ASSERT (sdyn != NULL);
3432
3433 /* Update all .dynamic entries referencing .dynstr strings. */
3434 for (extdyn = sdyn->contents;
3435 extdyn < sdyn->contents + sdyn->size;
3436 extdyn += bed->s->sizeof_dyn)
3437 {
3438 Elf_Internal_Dyn dyn;
3439
3440 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3441 switch (dyn.d_tag)
3442 {
3443 case DT_STRSZ:
3444 dyn.d_un.d_val = size;
3445 break;
3446 case DT_NEEDED:
3447 case DT_SONAME:
3448 case DT_RPATH:
3449 case DT_RUNPATH:
3450 case DT_FILTER:
3451 case DT_AUXILIARY:
3452 case DT_AUDIT:
3453 case DT_DEPAUDIT:
3454 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3455 break;
3456 default:
3457 continue;
3458 }
3459 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3460 }
3461
3462 /* Now update local dynamic symbols. */
3463 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3464 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3465 entry->isym.st_name);
3466
3467 /* And the rest of dynamic symbols. */
3468 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3469
3470 /* Adjust version definitions. */
3471 if (elf_tdata (output_bfd)->cverdefs)
3472 {
3473 asection *s;
3474 bfd_byte *p;
3475 size_t i;
3476 Elf_Internal_Verdef def;
3477 Elf_Internal_Verdaux defaux;
3478
3479 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3480 p = s->contents;
3481 do
3482 {
3483 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3484 &def);
3485 p += sizeof (Elf_External_Verdef);
3486 if (def.vd_aux != sizeof (Elf_External_Verdef))
3487 continue;
3488 for (i = 0; i < def.vd_cnt; ++i)
3489 {
3490 _bfd_elf_swap_verdaux_in (output_bfd,
3491 (Elf_External_Verdaux *) p, &defaux);
3492 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3493 defaux.vda_name);
3494 _bfd_elf_swap_verdaux_out (output_bfd,
3495 &defaux, (Elf_External_Verdaux *) p);
3496 p += sizeof (Elf_External_Verdaux);
3497 }
3498 }
3499 while (def.vd_next);
3500 }
3501
3502 /* Adjust version references. */
3503 if (elf_tdata (output_bfd)->verref)
3504 {
3505 asection *s;
3506 bfd_byte *p;
3507 size_t i;
3508 Elf_Internal_Verneed need;
3509 Elf_Internal_Vernaux needaux;
3510
3511 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3512 p = s->contents;
3513 do
3514 {
3515 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3516 &need);
3517 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3518 _bfd_elf_swap_verneed_out (output_bfd, &need,
3519 (Elf_External_Verneed *) p);
3520 p += sizeof (Elf_External_Verneed);
3521 for (i = 0; i < need.vn_cnt; ++i)
3522 {
3523 _bfd_elf_swap_vernaux_in (output_bfd,
3524 (Elf_External_Vernaux *) p, &needaux);
3525 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3526 needaux.vna_name);
3527 _bfd_elf_swap_vernaux_out (output_bfd,
3528 &needaux,
3529 (Elf_External_Vernaux *) p);
3530 p += sizeof (Elf_External_Vernaux);
3531 }
3532 }
3533 while (need.vn_next);
3534 }
3535
3536 return TRUE;
3537}
3538\f
3539/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3540 The default is to only match when the INPUT and OUTPUT are exactly
3541 the same target. */
3542
3543bfd_boolean
3544_bfd_elf_default_relocs_compatible (const bfd_target *input,
3545 const bfd_target *output)
3546{
3547 return input == output;
3548}
3549
3550/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3551 This version is used when different targets for the same architecture
3552 are virtually identical. */
3553
3554bfd_boolean
3555_bfd_elf_relocs_compatible (const bfd_target *input,
3556 const bfd_target *output)
3557{
3558 const struct elf_backend_data *obed, *ibed;
3559
3560 if (input == output)
3561 return TRUE;
3562
3563 ibed = xvec_get_elf_backend_data (input);
3564 obed = xvec_get_elf_backend_data (output);
3565
3566 if (ibed->arch != obed->arch)
3567 return FALSE;
3568
3569 /* If both backends are using this function, deem them compatible. */
3570 return ibed->relocs_compatible == obed->relocs_compatible;
3571}
3572
3573/* Make a special call to the linker "notice" function to tell it that
3574 we are about to handle an as-needed lib, or have finished
3575 processing the lib. */
3576
3577bfd_boolean
3578_bfd_elf_notice_as_needed (bfd *ibfd,
3579 struct bfd_link_info *info,
3580 enum notice_asneeded_action act)
3581{
3582 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3583}
3584
3585/* Check relocations an ELF object file. */
3586
3587bfd_boolean
3588_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3589{
3590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3591 struct elf_link_hash_table *htab = elf_hash_table (info);
3592
3593 /* If this object is the same format as the output object, and it is
3594 not a shared library, then let the backend look through the
3595 relocs.
3596
3597 This is required to build global offset table entries and to
3598 arrange for dynamic relocs. It is not required for the
3599 particular common case of linking non PIC code, even when linking
3600 against shared libraries, but unfortunately there is no way of
3601 knowing whether an object file has been compiled PIC or not.
3602 Looking through the relocs is not particularly time consuming.
3603 The problem is that we must either (1) keep the relocs in memory,
3604 which causes the linker to require additional runtime memory or
3605 (2) read the relocs twice from the input file, which wastes time.
3606 This would be a good case for using mmap.
3607
3608 I have no idea how to handle linking PIC code into a file of a
3609 different format. It probably can't be done. */
3610 if ((abfd->flags & DYNAMIC) == 0
3611 && is_elf_hash_table (htab)
3612 && bed->check_relocs != NULL
3613 && elf_object_id (abfd) == elf_hash_table_id (htab)
3614 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3615 {
3616 asection *o;
3617
3618 for (o = abfd->sections; o != NULL; o = o->next)
3619 {
3620 Elf_Internal_Rela *internal_relocs;
3621 bfd_boolean ok;
3622
3623 /* Don't check relocations in excluded sections. */
3624 if ((o->flags & SEC_RELOC) == 0
3625 || (o->flags & SEC_EXCLUDE) != 0
3626 || o->reloc_count == 0
3627 || ((info->strip == strip_all || info->strip == strip_debugger)
3628 && (o->flags & SEC_DEBUGGING) != 0)
3629 || bfd_is_abs_section (o->output_section))
3630 continue;
3631
3632 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3633 info->keep_memory);
3634 if (internal_relocs == NULL)
3635 return FALSE;
3636
3637 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3638
3639 if (elf_section_data (o)->relocs != internal_relocs)
3640 free (internal_relocs);
3641
3642 if (! ok)
3643 return FALSE;
3644 }
3645 }
3646
3647 return TRUE;
3648}
3649
3650/* Add symbols from an ELF object file to the linker hash table. */
3651
3652static bfd_boolean
3653elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3654{
3655 Elf_Internal_Ehdr *ehdr;
3656 Elf_Internal_Shdr *hdr;
3657 size_t symcount;
3658 size_t extsymcount;
3659 size_t extsymoff;
3660 struct elf_link_hash_entry **sym_hash;
3661 bfd_boolean dynamic;
3662 Elf_External_Versym *extversym = NULL;
3663 Elf_External_Versym *ever;
3664 struct elf_link_hash_entry *weaks;
3665 struct elf_link_hash_entry **nondeflt_vers = NULL;
3666 size_t nondeflt_vers_cnt = 0;
3667 Elf_Internal_Sym *isymbuf = NULL;
3668 Elf_Internal_Sym *isym;
3669 Elf_Internal_Sym *isymend;
3670 const struct elf_backend_data *bed;
3671 bfd_boolean add_needed;
3672 struct elf_link_hash_table *htab;
3673 bfd_size_type amt;
3674 void *alloc_mark = NULL;
3675 struct bfd_hash_entry **old_table = NULL;
3676 unsigned int old_size = 0;
3677 unsigned int old_count = 0;
3678 void *old_tab = NULL;
3679 void *old_ent;
3680 struct bfd_link_hash_entry *old_undefs = NULL;
3681 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3682 void *old_strtab = NULL;
3683 size_t tabsize = 0;
3684 asection *s;
3685 bfd_boolean just_syms;
3686
3687 htab = elf_hash_table (info);
3688 bed = get_elf_backend_data (abfd);
3689
3690 if ((abfd->flags & DYNAMIC) == 0)
3691 dynamic = FALSE;
3692 else
3693 {
3694 dynamic = TRUE;
3695
3696 /* You can't use -r against a dynamic object. Also, there's no
3697 hope of using a dynamic object which does not exactly match
3698 the format of the output file. */
3699 if (bfd_link_relocatable (info)
3700 || !is_elf_hash_table (htab)
3701 || info->output_bfd->xvec != abfd->xvec)
3702 {
3703 if (bfd_link_relocatable (info))
3704 bfd_set_error (bfd_error_invalid_operation);
3705 else
3706 bfd_set_error (bfd_error_wrong_format);
3707 goto error_return;
3708 }
3709 }
3710
3711 ehdr = elf_elfheader (abfd);
3712 if (info->warn_alternate_em
3713 && bed->elf_machine_code != ehdr->e_machine
3714 && ((bed->elf_machine_alt1 != 0
3715 && ehdr->e_machine == bed->elf_machine_alt1)
3716 || (bed->elf_machine_alt2 != 0
3717 && ehdr->e_machine == bed->elf_machine_alt2)))
3718 info->callbacks->einfo
3719 /* xgettext:c-format */
3720 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3721 ehdr->e_machine, abfd, bed->elf_machine_code);
3722
3723 /* As a GNU extension, any input sections which are named
3724 .gnu.warning.SYMBOL are treated as warning symbols for the given
3725 symbol. This differs from .gnu.warning sections, which generate
3726 warnings when they are included in an output file. */
3727 /* PR 12761: Also generate this warning when building shared libraries. */
3728 for (s = abfd->sections; s != NULL; s = s->next)
3729 {
3730 const char *name;
3731
3732 name = bfd_get_section_name (abfd, s);
3733 if (CONST_STRNEQ (name, ".gnu.warning."))
3734 {
3735 char *msg;
3736 bfd_size_type sz;
3737
3738 name += sizeof ".gnu.warning." - 1;
3739
3740 /* If this is a shared object, then look up the symbol
3741 in the hash table. If it is there, and it is already
3742 been defined, then we will not be using the entry
3743 from this shared object, so we don't need to warn.
3744 FIXME: If we see the definition in a regular object
3745 later on, we will warn, but we shouldn't. The only
3746 fix is to keep track of what warnings we are supposed
3747 to emit, and then handle them all at the end of the
3748 link. */
3749 if (dynamic)
3750 {
3751 struct elf_link_hash_entry *h;
3752
3753 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3754
3755 /* FIXME: What about bfd_link_hash_common? */
3756 if (h != NULL
3757 && (h->root.type == bfd_link_hash_defined
3758 || h->root.type == bfd_link_hash_defweak))
3759 continue;
3760 }
3761
3762 sz = s->size;
3763 msg = (char *) bfd_alloc (abfd, sz + 1);
3764 if (msg == NULL)
3765 goto error_return;
3766
3767 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3768 goto error_return;
3769
3770 msg[sz] = '\0';
3771
3772 if (! (_bfd_generic_link_add_one_symbol
3773 (info, abfd, name, BSF_WARNING, s, 0, msg,
3774 FALSE, bed->collect, NULL)))
3775 goto error_return;
3776
3777 if (bfd_link_executable (info))
3778 {
3779 /* Clobber the section size so that the warning does
3780 not get copied into the output file. */
3781 s->size = 0;
3782
3783 /* Also set SEC_EXCLUDE, so that symbols defined in
3784 the warning section don't get copied to the output. */
3785 s->flags |= SEC_EXCLUDE;
3786 }
3787 }
3788 }
3789
3790 just_syms = ((s = abfd->sections) != NULL
3791 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3792
3793 add_needed = TRUE;
3794 if (! dynamic)
3795 {
3796 /* If we are creating a shared library, create all the dynamic
3797 sections immediately. We need to attach them to something,
3798 so we attach them to this BFD, provided it is the right
3799 format and is not from ld --just-symbols. Always create the
3800 dynamic sections for -E/--dynamic-list. FIXME: If there
3801 are no input BFD's of the same format as the output, we can't
3802 make a shared library. */
3803 if (!just_syms
3804 && (bfd_link_pic (info)
3805 || (!bfd_link_relocatable (info)
3806 && (info->export_dynamic || info->dynamic)))
3807 && is_elf_hash_table (htab)
3808 && info->output_bfd->xvec == abfd->xvec
3809 && !htab->dynamic_sections_created)
3810 {
3811 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3812 goto error_return;
3813 }
3814 }
3815 else if (!is_elf_hash_table (htab))
3816 goto error_return;
3817 else
3818 {
3819 const char *soname = NULL;
3820 char *audit = NULL;
3821 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3822 const Elf_Internal_Phdr *phdr;
3823 int ret;
3824
3825 /* ld --just-symbols and dynamic objects don't mix very well.
3826 ld shouldn't allow it. */
3827 if (just_syms)
3828 abort ();
3829
3830 /* If this dynamic lib was specified on the command line with
3831 --as-needed in effect, then we don't want to add a DT_NEEDED
3832 tag unless the lib is actually used. Similary for libs brought
3833 in by another lib's DT_NEEDED. When --no-add-needed is used
3834 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3835 any dynamic library in DT_NEEDED tags in the dynamic lib at
3836 all. */
3837 add_needed = (elf_dyn_lib_class (abfd)
3838 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3839 | DYN_NO_NEEDED)) == 0;
3840
3841 s = bfd_get_section_by_name (abfd, ".dynamic");
3842 if (s != NULL)
3843 {
3844 bfd_byte *dynbuf;
3845 bfd_byte *extdyn;
3846 unsigned int elfsec;
3847 unsigned long shlink;
3848
3849 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3850 {
3851error_free_dyn:
3852 free (dynbuf);
3853 goto error_return;
3854 }
3855
3856 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3857 if (elfsec == SHN_BAD)
3858 goto error_free_dyn;
3859 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3860
3861 for (extdyn = dynbuf;
3862 extdyn < dynbuf + s->size;
3863 extdyn += bed->s->sizeof_dyn)
3864 {
3865 Elf_Internal_Dyn dyn;
3866
3867 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3868 if (dyn.d_tag == DT_SONAME)
3869 {
3870 unsigned int tagv = dyn.d_un.d_val;
3871 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3872 if (soname == NULL)
3873 goto error_free_dyn;
3874 }
3875 if (dyn.d_tag == DT_NEEDED)
3876 {
3877 struct bfd_link_needed_list *n, **pn;
3878 char *fnm, *anm;
3879 unsigned int tagv = dyn.d_un.d_val;
3880
3881 amt = sizeof (struct bfd_link_needed_list);
3882 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3883 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3884 if (n == NULL || fnm == NULL)
3885 goto error_free_dyn;
3886 amt = strlen (fnm) + 1;
3887 anm = (char *) bfd_alloc (abfd, amt);
3888 if (anm == NULL)
3889 goto error_free_dyn;
3890 memcpy (anm, fnm, amt);
3891 n->name = anm;
3892 n->by = abfd;
3893 n->next = NULL;
3894 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3895 ;
3896 *pn = n;
3897 }
3898 if (dyn.d_tag == DT_RUNPATH)
3899 {
3900 struct bfd_link_needed_list *n, **pn;
3901 char *fnm, *anm;
3902 unsigned int tagv = dyn.d_un.d_val;
3903
3904 amt = sizeof (struct bfd_link_needed_list);
3905 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3906 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3907 if (n == NULL || fnm == NULL)
3908 goto error_free_dyn;
3909 amt = strlen (fnm) + 1;
3910 anm = (char *) bfd_alloc (abfd, amt);
3911 if (anm == NULL)
3912 goto error_free_dyn;
3913 memcpy (anm, fnm, amt);
3914 n->name = anm;
3915 n->by = abfd;
3916 n->next = NULL;
3917 for (pn = & runpath;
3918 *pn != NULL;
3919 pn = &(*pn)->next)
3920 ;
3921 *pn = n;
3922 }
3923 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3924 if (!runpath && dyn.d_tag == DT_RPATH)
3925 {
3926 struct bfd_link_needed_list *n, **pn;
3927 char *fnm, *anm;
3928 unsigned int tagv = dyn.d_un.d_val;
3929
3930 amt = sizeof (struct bfd_link_needed_list);
3931 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3932 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3933 if (n == NULL || fnm == NULL)
3934 goto error_free_dyn;
3935 amt = strlen (fnm) + 1;
3936 anm = (char *) bfd_alloc (abfd, amt);
3937 if (anm == NULL)
3938 goto error_free_dyn;
3939 memcpy (anm, fnm, amt);
3940 n->name = anm;
3941 n->by = abfd;
3942 n->next = NULL;
3943 for (pn = & rpath;
3944 *pn != NULL;
3945 pn = &(*pn)->next)
3946 ;
3947 *pn = n;
3948 }
3949 if (dyn.d_tag == DT_AUDIT)
3950 {
3951 unsigned int tagv = dyn.d_un.d_val;
3952 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3953 }
3954 }
3955
3956 free (dynbuf);
3957 }
3958
3959 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3960 frees all more recently bfd_alloc'd blocks as well. */
3961 if (runpath)
3962 rpath = runpath;
3963
3964 if (rpath)
3965 {
3966 struct bfd_link_needed_list **pn;
3967 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3968 ;
3969 *pn = rpath;
3970 }
3971
3972 /* If we have a PT_GNU_RELRO program header, mark as read-only
3973 all sections contained fully therein. This makes relro
3974 shared library sections appear as they will at run-time. */
3975 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3976 while (--phdr >= elf_tdata (abfd)->phdr)
3977 if (phdr->p_type == PT_GNU_RELRO)
3978 {
3979 for (s = abfd->sections; s != NULL; s = s->next)
3980 if ((s->flags & SEC_ALLOC) != 0
3981 && s->vma >= phdr->p_vaddr
3982 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3983 s->flags |= SEC_READONLY;
3984 break;
3985 }
3986
3987 /* We do not want to include any of the sections in a dynamic
3988 object in the output file. We hack by simply clobbering the
3989 list of sections in the BFD. This could be handled more
3990 cleanly by, say, a new section flag; the existing
3991 SEC_NEVER_LOAD flag is not the one we want, because that one
3992 still implies that the section takes up space in the output
3993 file. */
3994 bfd_section_list_clear (abfd);
3995
3996 /* Find the name to use in a DT_NEEDED entry that refers to this
3997 object. If the object has a DT_SONAME entry, we use it.
3998 Otherwise, if the generic linker stuck something in
3999 elf_dt_name, we use that. Otherwise, we just use the file
4000 name. */
4001 if (soname == NULL || *soname == '\0')
4002 {
4003 soname = elf_dt_name (abfd);
4004 if (soname == NULL || *soname == '\0')
4005 soname = bfd_get_filename (abfd);
4006 }
4007
4008 /* Save the SONAME because sometimes the linker emulation code
4009 will need to know it. */
4010 elf_dt_name (abfd) = soname;
4011
4012 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4013 if (ret < 0)
4014 goto error_return;
4015
4016 /* If we have already included this dynamic object in the
4017 link, just ignore it. There is no reason to include a
4018 particular dynamic object more than once. */
4019 if (ret > 0)
4020 return TRUE;
4021
4022 /* Save the DT_AUDIT entry for the linker emulation code. */
4023 elf_dt_audit (abfd) = audit;
4024 }
4025
4026 /* If this is a dynamic object, we always link against the .dynsym
4027 symbol table, not the .symtab symbol table. The dynamic linker
4028 will only see the .dynsym symbol table, so there is no reason to
4029 look at .symtab for a dynamic object. */
4030
4031 if (! dynamic || elf_dynsymtab (abfd) == 0)
4032 hdr = &elf_tdata (abfd)->symtab_hdr;
4033 else
4034 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4035
4036 symcount = hdr->sh_size / bed->s->sizeof_sym;
4037
4038 /* The sh_info field of the symtab header tells us where the
4039 external symbols start. We don't care about the local symbols at
4040 this point. */
4041 if (elf_bad_symtab (abfd))
4042 {
4043 extsymcount = symcount;
4044 extsymoff = 0;
4045 }
4046 else
4047 {
4048 extsymcount = symcount - hdr->sh_info;
4049 extsymoff = hdr->sh_info;
4050 }
4051
4052 sym_hash = elf_sym_hashes (abfd);
4053 if (extsymcount != 0)
4054 {
4055 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4056 NULL, NULL, NULL);
4057 if (isymbuf == NULL)
4058 goto error_return;
4059
4060 if (sym_hash == NULL)
4061 {
4062 /* We store a pointer to the hash table entry for each
4063 external symbol. */
4064 amt = extsymcount;
4065 amt *= sizeof (struct elf_link_hash_entry *);
4066 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4067 if (sym_hash == NULL)
4068 goto error_free_sym;
4069 elf_sym_hashes (abfd) = sym_hash;
4070 }
4071 }
4072
4073 if (dynamic)
4074 {
4075 /* Read in any version definitions. */
4076 if (!_bfd_elf_slurp_version_tables (abfd,
4077 info->default_imported_symver))
4078 goto error_free_sym;
4079
4080 /* Read in the symbol versions, but don't bother to convert them
4081 to internal format. */
4082 if (elf_dynversym (abfd) != 0)
4083 {
4084 Elf_Internal_Shdr *versymhdr;
4085
4086 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4087 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4088 if (extversym == NULL)
4089 goto error_free_sym;
4090 amt = versymhdr->sh_size;
4091 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4092 || bfd_bread (extversym, amt, abfd) != amt)
4093 goto error_free_vers;
4094 }
4095 }
4096
4097 /* If we are loading an as-needed shared lib, save the symbol table
4098 state before we start adding symbols. If the lib turns out
4099 to be unneeded, restore the state. */
4100 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4101 {
4102 unsigned int i;
4103 size_t entsize;
4104
4105 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4106 {
4107 struct bfd_hash_entry *p;
4108 struct elf_link_hash_entry *h;
4109
4110 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4111 {
4112 h = (struct elf_link_hash_entry *) p;
4113 entsize += htab->root.table.entsize;
4114 if (h->root.type == bfd_link_hash_warning)
4115 entsize += htab->root.table.entsize;
4116 }
4117 }
4118
4119 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4120 old_tab = bfd_malloc (tabsize + entsize);
4121 if (old_tab == NULL)
4122 goto error_free_vers;
4123
4124 /* Remember the current objalloc pointer, so that all mem for
4125 symbols added can later be reclaimed. */
4126 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4127 if (alloc_mark == NULL)
4128 goto error_free_vers;
4129
4130 /* Make a special call to the linker "notice" function to
4131 tell it that we are about to handle an as-needed lib. */
4132 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4133 goto error_free_vers;
4134
4135 /* Clone the symbol table. Remember some pointers into the
4136 symbol table, and dynamic symbol count. */
4137 old_ent = (char *) old_tab + tabsize;
4138 memcpy (old_tab, htab->root.table.table, tabsize);
4139 old_undefs = htab->root.undefs;
4140 old_undefs_tail = htab->root.undefs_tail;
4141 old_table = htab->root.table.table;
4142 old_size = htab->root.table.size;
4143 old_count = htab->root.table.count;
4144 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4145 if (old_strtab == NULL)
4146 goto error_free_vers;
4147
4148 for (i = 0; i < htab->root.table.size; i++)
4149 {
4150 struct bfd_hash_entry *p;
4151 struct elf_link_hash_entry *h;
4152
4153 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4154 {
4155 memcpy (old_ent, p, htab->root.table.entsize);
4156 old_ent = (char *) old_ent + htab->root.table.entsize;
4157 h = (struct elf_link_hash_entry *) p;
4158 if (h->root.type == bfd_link_hash_warning)
4159 {
4160 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4161 old_ent = (char *) old_ent + htab->root.table.entsize;
4162 }
4163 }
4164 }
4165 }
4166
4167 weaks = NULL;
4168 ever = extversym != NULL ? extversym + extsymoff : NULL;
4169 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4170 isym < isymend;
4171 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4172 {
4173 int bind;
4174 bfd_vma value;
4175 asection *sec, *new_sec;
4176 flagword flags;
4177 const char *name;
4178 struct elf_link_hash_entry *h;
4179 struct elf_link_hash_entry *hi;
4180 bfd_boolean definition;
4181 bfd_boolean size_change_ok;
4182 bfd_boolean type_change_ok;
4183 bfd_boolean new_weakdef;
4184 bfd_boolean new_weak;
4185 bfd_boolean old_weak;
4186 bfd_boolean override;
4187 bfd_boolean common;
4188 bfd_boolean discarded;
4189 unsigned int old_alignment;
4190 bfd *old_bfd;
4191 bfd_boolean matched;
4192
4193 override = FALSE;
4194
4195 flags = BSF_NO_FLAGS;
4196 sec = NULL;
4197 value = isym->st_value;
4198 common = bed->common_definition (isym);
4199 discarded = FALSE;
4200
4201 bind = ELF_ST_BIND (isym->st_info);
4202 switch (bind)
4203 {
4204 case STB_LOCAL:
4205 /* This should be impossible, since ELF requires that all
4206 global symbols follow all local symbols, and that sh_info
4207 point to the first global symbol. Unfortunately, Irix 5
4208 screws this up. */
4209 continue;
4210
4211 case STB_GLOBAL:
4212 if (isym->st_shndx != SHN_UNDEF && !common)
4213 flags = BSF_GLOBAL;
4214 break;
4215
4216 case STB_WEAK:
4217 flags = BSF_WEAK;
4218 break;
4219
4220 case STB_GNU_UNIQUE:
4221 flags = BSF_GNU_UNIQUE;
4222 break;
4223
4224 default:
4225 /* Leave it up to the processor backend. */
4226 break;
4227 }
4228
4229 if (isym->st_shndx == SHN_UNDEF)
4230 sec = bfd_und_section_ptr;
4231 else if (isym->st_shndx == SHN_ABS)
4232 sec = bfd_abs_section_ptr;
4233 else if (isym->st_shndx == SHN_COMMON)
4234 {
4235 sec = bfd_com_section_ptr;
4236 /* What ELF calls the size we call the value. What ELF
4237 calls the value we call the alignment. */
4238 value = isym->st_size;
4239 }
4240 else
4241 {
4242 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4243 if (sec == NULL)
4244 sec = bfd_abs_section_ptr;
4245 else if (discarded_section (sec))
4246 {
4247 /* Symbols from discarded section are undefined. We keep
4248 its visibility. */
4249 sec = bfd_und_section_ptr;
4250 discarded = TRUE;
4251 isym->st_shndx = SHN_UNDEF;
4252 }
4253 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4254 value -= sec->vma;
4255 }
4256
4257 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4258 isym->st_name);
4259 if (name == NULL)
4260 goto error_free_vers;
4261
4262 if (isym->st_shndx == SHN_COMMON
4263 && (abfd->flags & BFD_PLUGIN) != 0)
4264 {
4265 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4266
4267 if (xc == NULL)
4268 {
4269 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4270 | SEC_EXCLUDE);
4271 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4272 if (xc == NULL)
4273 goto error_free_vers;
4274 }
4275 sec = xc;
4276 }
4277 else if (isym->st_shndx == SHN_COMMON
4278 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4279 && !bfd_link_relocatable (info))
4280 {
4281 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4282
4283 if (tcomm == NULL)
4284 {
4285 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4286 | SEC_LINKER_CREATED);
4287 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4288 if (tcomm == NULL)
4289 goto error_free_vers;
4290 }
4291 sec = tcomm;
4292 }
4293 else if (bed->elf_add_symbol_hook)
4294 {
4295 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4296 &sec, &value))
4297 goto error_free_vers;
4298
4299 /* The hook function sets the name to NULL if this symbol
4300 should be skipped for some reason. */
4301 if (name == NULL)
4302 continue;
4303 }
4304
4305 /* Sanity check that all possibilities were handled. */
4306 if (sec == NULL)
4307 {
4308 bfd_set_error (bfd_error_bad_value);
4309 goto error_free_vers;
4310 }
4311
4312 /* Silently discard TLS symbols from --just-syms. There's
4313 no way to combine a static TLS block with a new TLS block
4314 for this executable. */
4315 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4316 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4317 continue;
4318
4319 if (bfd_is_und_section (sec)
4320 || bfd_is_com_section (sec))
4321 definition = FALSE;
4322 else
4323 definition = TRUE;
4324
4325 size_change_ok = FALSE;
4326 type_change_ok = bed->type_change_ok;
4327 old_weak = FALSE;
4328 matched = FALSE;
4329 old_alignment = 0;
4330 old_bfd = NULL;
4331 new_sec = sec;
4332
4333 if (is_elf_hash_table (htab))
4334 {
4335 Elf_Internal_Versym iver;
4336 unsigned int vernum = 0;
4337 bfd_boolean skip;
4338
4339 if (ever == NULL)
4340 {
4341 if (info->default_imported_symver)
4342 /* Use the default symbol version created earlier. */
4343 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4344 else
4345 iver.vs_vers = 0;
4346 }
4347 else
4348 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4349
4350 vernum = iver.vs_vers & VERSYM_VERSION;
4351
4352 /* If this is a hidden symbol, or if it is not version
4353 1, we append the version name to the symbol name.
4354 However, we do not modify a non-hidden absolute symbol
4355 if it is not a function, because it might be the version
4356 symbol itself. FIXME: What if it isn't? */
4357 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4358 || (vernum > 1
4359 && (!bfd_is_abs_section (sec)
4360 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4361 {
4362 const char *verstr;
4363 size_t namelen, verlen, newlen;
4364 char *newname, *p;
4365
4366 if (isym->st_shndx != SHN_UNDEF)
4367 {
4368 if (vernum > elf_tdata (abfd)->cverdefs)
4369 verstr = NULL;
4370 else if (vernum > 1)
4371 verstr =
4372 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4373 else
4374 verstr = "";
4375
4376 if (verstr == NULL)
4377 {
4378 _bfd_error_handler
4379 /* xgettext:c-format */
4380 (_("%B: %s: invalid version %u (max %d)"),
4381 abfd, name, vernum,
4382 elf_tdata (abfd)->cverdefs);
4383 bfd_set_error (bfd_error_bad_value);
4384 goto error_free_vers;
4385 }
4386 }
4387 else
4388 {
4389 /* We cannot simply test for the number of
4390 entries in the VERNEED section since the
4391 numbers for the needed versions do not start
4392 at 0. */
4393 Elf_Internal_Verneed *t;
4394
4395 verstr = NULL;
4396 for (t = elf_tdata (abfd)->verref;
4397 t != NULL;
4398 t = t->vn_nextref)
4399 {
4400 Elf_Internal_Vernaux *a;
4401
4402 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4403 {
4404 if (a->vna_other == vernum)
4405 {
4406 verstr = a->vna_nodename;
4407 break;
4408 }
4409 }
4410 if (a != NULL)
4411 break;
4412 }
4413 if (verstr == NULL)
4414 {
4415 _bfd_error_handler
4416 /* xgettext:c-format */
4417 (_("%B: %s: invalid needed version %d"),
4418 abfd, name, vernum);
4419 bfd_set_error (bfd_error_bad_value);
4420 goto error_free_vers;
4421 }
4422 }
4423
4424 namelen = strlen (name);
4425 verlen = strlen (verstr);
4426 newlen = namelen + verlen + 2;
4427 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4428 && isym->st_shndx != SHN_UNDEF)
4429 ++newlen;
4430
4431 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4432 if (newname == NULL)
4433 goto error_free_vers;
4434 memcpy (newname, name, namelen);
4435 p = newname + namelen;
4436 *p++ = ELF_VER_CHR;
4437 /* If this is a defined non-hidden version symbol,
4438 we add another @ to the name. This indicates the
4439 default version of the symbol. */
4440 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4441 && isym->st_shndx != SHN_UNDEF)
4442 *p++ = ELF_VER_CHR;
4443 memcpy (p, verstr, verlen + 1);
4444
4445 name = newname;
4446 }
4447
4448 /* If this symbol has default visibility and the user has
4449 requested we not re-export it, then mark it as hidden. */
4450 if (!bfd_is_und_section (sec)
4451 && !dynamic
4452 && abfd->no_export
4453 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4454 isym->st_other = (STV_HIDDEN
4455 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4456
4457 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4458 sym_hash, &old_bfd, &old_weak,
4459 &old_alignment, &skip, &override,
4460 &type_change_ok, &size_change_ok,
4461 &matched))
4462 goto error_free_vers;
4463
4464 if (skip)
4465 continue;
4466
4467 /* Override a definition only if the new symbol matches the
4468 existing one. */
4469 if (override && matched)
4470 definition = FALSE;
4471
4472 h = *sym_hash;
4473 while (h->root.type == bfd_link_hash_indirect
4474 || h->root.type == bfd_link_hash_warning)
4475 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4476
4477 if (elf_tdata (abfd)->verdef != NULL
4478 && vernum > 1
4479 && definition)
4480 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4481 }
4482
4483 if (! (_bfd_generic_link_add_one_symbol
4484 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4485 (struct bfd_link_hash_entry **) sym_hash)))
4486 goto error_free_vers;
4487
4488 if ((flags & BSF_GNU_UNIQUE)
4489 && (abfd->flags & DYNAMIC) == 0
4490 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4491 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4492
4493 h = *sym_hash;
4494 /* We need to make sure that indirect symbol dynamic flags are
4495 updated. */
4496 hi = h;
4497 while (h->root.type == bfd_link_hash_indirect
4498 || h->root.type == bfd_link_hash_warning)
4499 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4500
4501 /* Setting the index to -3 tells elf_link_output_extsym that
4502 this symbol is defined in a discarded section. */
4503 if (discarded)
4504 h->indx = -3;
4505
4506 *sym_hash = h;
4507
4508 new_weak = (flags & BSF_WEAK) != 0;
4509 new_weakdef = FALSE;
4510 if (dynamic
4511 && definition
4512 && new_weak
4513 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4514 && is_elf_hash_table (htab)
4515 && h->u.weakdef == NULL)
4516 {
4517 /* Keep a list of all weak defined non function symbols from
4518 a dynamic object, using the weakdef field. Later in this
4519 function we will set the weakdef field to the correct
4520 value. We only put non-function symbols from dynamic
4521 objects on this list, because that happens to be the only
4522 time we need to know the normal symbol corresponding to a
4523 weak symbol, and the information is time consuming to
4524 figure out. If the weakdef field is not already NULL,
4525 then this symbol was already defined by some previous
4526 dynamic object, and we will be using that previous
4527 definition anyhow. */
4528
4529 h->u.weakdef = weaks;
4530 weaks = h;
4531 new_weakdef = TRUE;
4532 }
4533
4534 /* Set the alignment of a common symbol. */
4535 if ((common || bfd_is_com_section (sec))
4536 && h->root.type == bfd_link_hash_common)
4537 {
4538 unsigned int align;
4539
4540 if (common)
4541 align = bfd_log2 (isym->st_value);
4542 else
4543 {
4544 /* The new symbol is a common symbol in a shared object.
4545 We need to get the alignment from the section. */
4546 align = new_sec->alignment_power;
4547 }
4548 if (align > old_alignment)
4549 h->root.u.c.p->alignment_power = align;
4550 else
4551 h->root.u.c.p->alignment_power = old_alignment;
4552 }
4553
4554 if (is_elf_hash_table (htab))
4555 {
4556 /* Set a flag in the hash table entry indicating the type of
4557 reference or definition we just found. A dynamic symbol
4558 is one which is referenced or defined by both a regular
4559 object and a shared object. */
4560 bfd_boolean dynsym = FALSE;
4561
4562 /* Plugin symbols aren't normal. Don't set def_regular or
4563 ref_regular for them, or make them dynamic. */
4564 if ((abfd->flags & BFD_PLUGIN) != 0)
4565 ;
4566 else if (! dynamic)
4567 {
4568 if (! definition)
4569 {
4570 h->ref_regular = 1;
4571 if (bind != STB_WEAK)
4572 h->ref_regular_nonweak = 1;
4573 }
4574 else
4575 {
4576 h->def_regular = 1;
4577 if (h->def_dynamic)
4578 {
4579 h->def_dynamic = 0;
4580 h->ref_dynamic = 1;
4581 }
4582 }
4583
4584 /* If the indirect symbol has been forced local, don't
4585 make the real symbol dynamic. */
4586 if ((h == hi || !hi->forced_local)
4587 && (bfd_link_dll (info)
4588 || h->def_dynamic
4589 || h->ref_dynamic))
4590 dynsym = TRUE;
4591 }
4592 else
4593 {
4594 if (! definition)
4595 {
4596 h->ref_dynamic = 1;
4597 hi->ref_dynamic = 1;
4598 }
4599 else
4600 {
4601 h->def_dynamic = 1;
4602 hi->def_dynamic = 1;
4603 }
4604
4605 /* If the indirect symbol has been forced local, don't
4606 make the real symbol dynamic. */
4607 if ((h == hi || !hi->forced_local)
4608 && (h->def_regular
4609 || h->ref_regular
4610 || (h->u.weakdef != NULL
4611 && ! new_weakdef
4612 && h->u.weakdef->dynindx != -1)))
4613 dynsym = TRUE;
4614 }
4615
4616 /* Check to see if we need to add an indirect symbol for
4617 the default name. */
4618 if (definition
4619 || (!override && h->root.type == bfd_link_hash_common))
4620 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4621 sec, value, &old_bfd, &dynsym))
4622 goto error_free_vers;
4623
4624 /* Check the alignment when a common symbol is involved. This
4625 can change when a common symbol is overridden by a normal
4626 definition or a common symbol is ignored due to the old
4627 normal definition. We need to make sure the maximum
4628 alignment is maintained. */
4629 if ((old_alignment || common)
4630 && h->root.type != bfd_link_hash_common)
4631 {
4632 unsigned int common_align;
4633 unsigned int normal_align;
4634 unsigned int symbol_align;
4635 bfd *normal_bfd;
4636 bfd *common_bfd;
4637
4638 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4639 || h->root.type == bfd_link_hash_defweak);
4640
4641 symbol_align = ffs (h->root.u.def.value) - 1;
4642 if (h->root.u.def.section->owner != NULL
4643 && (h->root.u.def.section->owner->flags
4644 & (DYNAMIC | BFD_PLUGIN)) == 0)
4645 {
4646 normal_align = h->root.u.def.section->alignment_power;
4647 if (normal_align > symbol_align)
4648 normal_align = symbol_align;
4649 }
4650 else
4651 normal_align = symbol_align;
4652
4653 if (old_alignment)
4654 {
4655 common_align = old_alignment;
4656 common_bfd = old_bfd;
4657 normal_bfd = abfd;
4658 }
4659 else
4660 {
4661 common_align = bfd_log2 (isym->st_value);
4662 common_bfd = abfd;
4663 normal_bfd = old_bfd;
4664 }
4665
4666 if (normal_align < common_align)
4667 {
4668 /* PR binutils/2735 */
4669 if (normal_bfd == NULL)
4670 _bfd_error_handler
4671 /* xgettext:c-format */
4672 (_("Warning: alignment %u of common symbol `%s' in %B is"
4673 " greater than the alignment (%u) of its section %A"),
4674 common_bfd, h->root.u.def.section,
4675 1 << common_align, name, 1 << normal_align);
4676 else
4677 _bfd_error_handler
4678 /* xgettext:c-format */
4679 (_("Warning: alignment %u of symbol `%s' in %B"
4680 " is smaller than %u in %B"),
4681 normal_bfd, common_bfd,
4682 1 << normal_align, name, 1 << common_align);
4683 }
4684 }
4685
4686 /* Remember the symbol size if it isn't undefined. */
4687 if (isym->st_size != 0
4688 && isym->st_shndx != SHN_UNDEF
4689 && (definition || h->size == 0))
4690 {
4691 if (h->size != 0
4692 && h->size != isym->st_size
4693 && ! size_change_ok)
4694 _bfd_error_handler
4695 /* xgettext:c-format */
4696 (_("Warning: size of symbol `%s' changed"
4697 " from %lu in %B to %lu in %B"),
4698 old_bfd, abfd,
4699 name, (unsigned long) h->size,
4700 (unsigned long) isym->st_size);
4701
4702 h->size = isym->st_size;
4703 }
4704
4705 /* If this is a common symbol, then we always want H->SIZE
4706 to be the size of the common symbol. The code just above
4707 won't fix the size if a common symbol becomes larger. We
4708 don't warn about a size change here, because that is
4709 covered by --warn-common. Allow changes between different
4710 function types. */
4711 if (h->root.type == bfd_link_hash_common)
4712 h->size = h->root.u.c.size;
4713
4714 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4715 && ((definition && !new_weak)
4716 || (old_weak && h->root.type == bfd_link_hash_common)
4717 || h->type == STT_NOTYPE))
4718 {
4719 unsigned int type = ELF_ST_TYPE (isym->st_info);
4720
4721 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4722 symbol. */
4723 if (type == STT_GNU_IFUNC
4724 && (abfd->flags & DYNAMIC) != 0)
4725 type = STT_FUNC;
4726
4727 if (h->type != type)
4728 {
4729 if (h->type != STT_NOTYPE && ! type_change_ok)
4730 /* xgettext:c-format */
4731 _bfd_error_handler
4732 (_("Warning: type of symbol `%s' changed"
4733 " from %d to %d in %B"),
4734 abfd, name, h->type, type);
4735
4736 h->type = type;
4737 }
4738 }
4739
4740 /* Merge st_other field. */
4741 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4742
4743 /* We don't want to make debug symbol dynamic. */
4744 if (definition
4745 && (sec->flags & SEC_DEBUGGING)
4746 && !bfd_link_relocatable (info))
4747 dynsym = FALSE;
4748
4749 /* Nor should we make plugin symbols dynamic. */
4750 if ((abfd->flags & BFD_PLUGIN) != 0)
4751 dynsym = FALSE;
4752
4753 if (definition)
4754 {
4755 h->target_internal = isym->st_target_internal;
4756 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4757 }
4758
4759 if (definition && !dynamic)
4760 {
4761 char *p = strchr (name, ELF_VER_CHR);
4762 if (p != NULL && p[1] != ELF_VER_CHR)
4763 {
4764 /* Queue non-default versions so that .symver x, x@FOO
4765 aliases can be checked. */
4766 if (!nondeflt_vers)
4767 {
4768 amt = ((isymend - isym + 1)
4769 * sizeof (struct elf_link_hash_entry *));
4770 nondeflt_vers
4771 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4772 if (!nondeflt_vers)
4773 goto error_free_vers;
4774 }
4775 nondeflt_vers[nondeflt_vers_cnt++] = h;
4776 }
4777 }
4778
4779 if (dynsym && h->dynindx == -1)
4780 {
4781 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4782 goto error_free_vers;
4783 if (h->u.weakdef != NULL
4784 && ! new_weakdef
4785 && h->u.weakdef->dynindx == -1)
4786 {
4787 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4788 goto error_free_vers;
4789 }
4790 }
4791 else if (h->dynindx != -1)
4792 /* If the symbol already has a dynamic index, but
4793 visibility says it should not be visible, turn it into
4794 a local symbol. */
4795 switch (ELF_ST_VISIBILITY (h->other))
4796 {
4797 case STV_INTERNAL:
4798 case STV_HIDDEN:
4799 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4800 dynsym = FALSE;
4801 break;
4802 }
4803
4804 /* Don't add DT_NEEDED for references from the dummy bfd nor
4805 for unmatched symbol. */
4806 if (!add_needed
4807 && matched
4808 && definition
4809 && ((dynsym
4810 && h->ref_regular_nonweak
4811 && (old_bfd == NULL
4812 || (old_bfd->flags & BFD_PLUGIN) == 0))
4813 || (h->ref_dynamic_nonweak
4814 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4815 && !on_needed_list (elf_dt_name (abfd),
4816 htab->needed, NULL))))
4817 {
4818 int ret;
4819 const char *soname = elf_dt_name (abfd);
4820
4821 info->callbacks->minfo ("%!", soname, old_bfd,
4822 h->root.root.string);
4823
4824 /* A symbol from a library loaded via DT_NEEDED of some
4825 other library is referenced by a regular object.
4826 Add a DT_NEEDED entry for it. Issue an error if
4827 --no-add-needed is used and the reference was not
4828 a weak one. */
4829 if (old_bfd != NULL
4830 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4831 {
4832 _bfd_error_handler
4833 /* xgettext:c-format */
4834 (_("%B: undefined reference to symbol '%s'"),
4835 old_bfd, name);
4836 bfd_set_error (bfd_error_missing_dso);
4837 goto error_free_vers;
4838 }
4839
4840 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4841 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4842
4843 add_needed = TRUE;
4844 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4845 if (ret < 0)
4846 goto error_free_vers;
4847
4848 BFD_ASSERT (ret == 0);
4849 }
4850 }
4851 }
4852
4853 if (extversym != NULL)
4854 {
4855 free (extversym);
4856 extversym = NULL;
4857 }
4858
4859 if (isymbuf != NULL)
4860 {
4861 free (isymbuf);
4862 isymbuf = NULL;
4863 }
4864
4865 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4866 {
4867 unsigned int i;
4868
4869 /* Restore the symbol table. */
4870 old_ent = (char *) old_tab + tabsize;
4871 memset (elf_sym_hashes (abfd), 0,
4872 extsymcount * sizeof (struct elf_link_hash_entry *));
4873 htab->root.table.table = old_table;
4874 htab->root.table.size = old_size;
4875 htab->root.table.count = old_count;
4876 memcpy (htab->root.table.table, old_tab, tabsize);
4877 htab->root.undefs = old_undefs;
4878 htab->root.undefs_tail = old_undefs_tail;
4879 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4880 free (old_strtab);
4881 old_strtab = NULL;
4882 for (i = 0; i < htab->root.table.size; i++)
4883 {
4884 struct bfd_hash_entry *p;
4885 struct elf_link_hash_entry *h;
4886 bfd_size_type size;
4887 unsigned int alignment_power;
4888
4889 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4890 {
4891 h = (struct elf_link_hash_entry *) p;
4892 if (h->root.type == bfd_link_hash_warning)
4893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4894
4895 /* Preserve the maximum alignment and size for common
4896 symbols even if this dynamic lib isn't on DT_NEEDED
4897 since it can still be loaded at run time by another
4898 dynamic lib. */
4899 if (h->root.type == bfd_link_hash_common)
4900 {
4901 size = h->root.u.c.size;
4902 alignment_power = h->root.u.c.p->alignment_power;
4903 }
4904 else
4905 {
4906 size = 0;
4907 alignment_power = 0;
4908 }
4909 memcpy (p, old_ent, htab->root.table.entsize);
4910 old_ent = (char *) old_ent + htab->root.table.entsize;
4911 h = (struct elf_link_hash_entry *) p;
4912 if (h->root.type == bfd_link_hash_warning)
4913 {
4914 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4915 old_ent = (char *) old_ent + htab->root.table.entsize;
4916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4917 }
4918 if (h->root.type == bfd_link_hash_common)
4919 {
4920 if (size > h->root.u.c.size)
4921 h->root.u.c.size = size;
4922 if (alignment_power > h->root.u.c.p->alignment_power)
4923 h->root.u.c.p->alignment_power = alignment_power;
4924 }
4925 }
4926 }
4927
4928 /* Make a special call to the linker "notice" function to
4929 tell it that symbols added for crefs may need to be removed. */
4930 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4931 goto error_free_vers;
4932
4933 free (old_tab);
4934 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4935 alloc_mark);
4936 if (nondeflt_vers != NULL)
4937 free (nondeflt_vers);
4938 return TRUE;
4939 }
4940
4941 if (old_tab != NULL)
4942 {
4943 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4944 goto error_free_vers;
4945 free (old_tab);
4946 old_tab = NULL;
4947 }
4948
4949 /* Now that all the symbols from this input file are created, if
4950 not performing a relocatable link, handle .symver foo, foo@BAR
4951 such that any relocs against foo become foo@BAR. */
4952 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4953 {
4954 size_t cnt, symidx;
4955
4956 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4957 {
4958 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4959 char *shortname, *p;
4960
4961 p = strchr (h->root.root.string, ELF_VER_CHR);
4962 if (p == NULL
4963 || (h->root.type != bfd_link_hash_defined
4964 && h->root.type != bfd_link_hash_defweak))
4965 continue;
4966
4967 amt = p - h->root.root.string;
4968 shortname = (char *) bfd_malloc (amt + 1);
4969 if (!shortname)
4970 goto error_free_vers;
4971 memcpy (shortname, h->root.root.string, amt);
4972 shortname[amt] = '\0';
4973
4974 hi = (struct elf_link_hash_entry *)
4975 bfd_link_hash_lookup (&htab->root, shortname,
4976 FALSE, FALSE, FALSE);
4977 if (hi != NULL
4978 && hi->root.type == h->root.type
4979 && hi->root.u.def.value == h->root.u.def.value
4980 && hi->root.u.def.section == h->root.u.def.section)
4981 {
4982 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4983 hi->root.type = bfd_link_hash_indirect;
4984 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4985 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4986 sym_hash = elf_sym_hashes (abfd);
4987 if (sym_hash)
4988 for (symidx = 0; symidx < extsymcount; ++symidx)
4989 if (sym_hash[symidx] == hi)
4990 {
4991 sym_hash[symidx] = h;
4992 break;
4993 }
4994 }
4995 free (shortname);
4996 }
4997 free (nondeflt_vers);
4998 nondeflt_vers = NULL;
4999 }
5000
5001 /* Now set the weakdefs field correctly for all the weak defined
5002 symbols we found. The only way to do this is to search all the
5003 symbols. Since we only need the information for non functions in
5004 dynamic objects, that's the only time we actually put anything on
5005 the list WEAKS. We need this information so that if a regular
5006 object refers to a symbol defined weakly in a dynamic object, the
5007 real symbol in the dynamic object is also put in the dynamic
5008 symbols; we also must arrange for both symbols to point to the
5009 same memory location. We could handle the general case of symbol
5010 aliasing, but a general symbol alias can only be generated in
5011 assembler code, handling it correctly would be very time
5012 consuming, and other ELF linkers don't handle general aliasing
5013 either. */
5014 if (weaks != NULL)
5015 {
5016 struct elf_link_hash_entry **hpp;
5017 struct elf_link_hash_entry **hppend;
5018 struct elf_link_hash_entry **sorted_sym_hash;
5019 struct elf_link_hash_entry *h;
5020 size_t sym_count;
5021
5022 /* Since we have to search the whole symbol list for each weak
5023 defined symbol, search time for N weak defined symbols will be
5024 O(N^2). Binary search will cut it down to O(NlogN). */
5025 amt = extsymcount;
5026 amt *= sizeof (struct elf_link_hash_entry *);
5027 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5028 if (sorted_sym_hash == NULL)
5029 goto error_return;
5030 sym_hash = sorted_sym_hash;
5031 hpp = elf_sym_hashes (abfd);
5032 hppend = hpp + extsymcount;
5033 sym_count = 0;
5034 for (; hpp < hppend; hpp++)
5035 {
5036 h = *hpp;
5037 if (h != NULL
5038 && h->root.type == bfd_link_hash_defined
5039 && !bed->is_function_type (h->type))
5040 {
5041 *sym_hash = h;
5042 sym_hash++;
5043 sym_count++;
5044 }
5045 }
5046
5047 qsort (sorted_sym_hash, sym_count,
5048 sizeof (struct elf_link_hash_entry *),
5049 elf_sort_symbol);
5050
5051 while (weaks != NULL)
5052 {
5053 struct elf_link_hash_entry *hlook;
5054 asection *slook;
5055 bfd_vma vlook;
5056 size_t i, j, idx = 0;
5057
5058 hlook = weaks;
5059 weaks = hlook->u.weakdef;
5060 hlook->u.weakdef = NULL;
5061
5062 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5063 || hlook->root.type == bfd_link_hash_defweak
5064 || hlook->root.type == bfd_link_hash_common
5065 || hlook->root.type == bfd_link_hash_indirect);
5066 slook = hlook->root.u.def.section;
5067 vlook = hlook->root.u.def.value;
5068
5069 i = 0;
5070 j = sym_count;
5071 while (i != j)
5072 {
5073 bfd_signed_vma vdiff;
5074 idx = (i + j) / 2;
5075 h = sorted_sym_hash[idx];
5076 vdiff = vlook - h->root.u.def.value;
5077 if (vdiff < 0)
5078 j = idx;
5079 else if (vdiff > 0)
5080 i = idx + 1;
5081 else
5082 {
5083 int sdiff = slook->id - h->root.u.def.section->id;
5084 if (sdiff < 0)
5085 j = idx;
5086 else if (sdiff > 0)
5087 i = idx + 1;
5088 else
5089 break;
5090 }
5091 }
5092
5093 /* We didn't find a value/section match. */
5094 if (i == j)
5095 continue;
5096
5097 /* With multiple aliases, or when the weak symbol is already
5098 strongly defined, we have multiple matching symbols and
5099 the binary search above may land on any of them. Step
5100 one past the matching symbol(s). */
5101 while (++idx != j)
5102 {
5103 h = sorted_sym_hash[idx];
5104 if (h->root.u.def.section != slook
5105 || h->root.u.def.value != vlook)
5106 break;
5107 }
5108
5109 /* Now look back over the aliases. Since we sorted by size
5110 as well as value and section, we'll choose the one with
5111 the largest size. */
5112 while (idx-- != i)
5113 {
5114 h = sorted_sym_hash[idx];
5115
5116 /* Stop if value or section doesn't match. */
5117 if (h->root.u.def.section != slook
5118 || h->root.u.def.value != vlook)
5119 break;
5120 else if (h != hlook)
5121 {
5122 hlook->u.weakdef = h;
5123
5124 /* If the weak definition is in the list of dynamic
5125 symbols, make sure the real definition is put
5126 there as well. */
5127 if (hlook->dynindx != -1 && h->dynindx == -1)
5128 {
5129 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5130 {
5131 err_free_sym_hash:
5132 free (sorted_sym_hash);
5133 goto error_return;
5134 }
5135 }
5136
5137 /* If the real definition is in the list of dynamic
5138 symbols, make sure the weak definition is put
5139 there as well. If we don't do this, then the
5140 dynamic loader might not merge the entries for the
5141 real definition and the weak definition. */
5142 if (h->dynindx != -1 && hlook->dynindx == -1)
5143 {
5144 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5145 goto err_free_sym_hash;
5146 }
5147 break;
5148 }
5149 }
5150 }
5151
5152 free (sorted_sym_hash);
5153 }
5154
5155 if (bed->check_directives
5156 && !(*bed->check_directives) (abfd, info))
5157 return FALSE;
5158
5159 if (!info->check_relocs_after_open_input
5160 && !_bfd_elf_link_check_relocs (abfd, info))
5161 return FALSE;
5162
5163 /* If this is a non-traditional link, try to optimize the handling
5164 of the .stab/.stabstr sections. */
5165 if (! dynamic
5166 && ! info->traditional_format
5167 && is_elf_hash_table (htab)
5168 && (info->strip != strip_all && info->strip != strip_debugger))
5169 {
5170 asection *stabstr;
5171
5172 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5173 if (stabstr != NULL)
5174 {
5175 bfd_size_type string_offset = 0;
5176 asection *stab;
5177
5178 for (stab = abfd->sections; stab; stab = stab->next)
5179 if (CONST_STRNEQ (stab->name, ".stab")
5180 && (!stab->name[5] ||
5181 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5182 && (stab->flags & SEC_MERGE) == 0
5183 && !bfd_is_abs_section (stab->output_section))
5184 {
5185 struct bfd_elf_section_data *secdata;
5186
5187 secdata = elf_section_data (stab);
5188 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5189 stabstr, &secdata->sec_info,
5190 &string_offset))
5191 goto error_return;
5192 if (secdata->sec_info)
5193 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5194 }
5195 }
5196 }
5197
5198 if (is_elf_hash_table (htab) && add_needed)
5199 {
5200 /* Add this bfd to the loaded list. */
5201 struct elf_link_loaded_list *n;
5202
5203 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5204 if (n == NULL)
5205 goto error_return;
5206 n->abfd = abfd;
5207 n->next = htab->loaded;
5208 htab->loaded = n;
5209 }
5210
5211 return TRUE;
5212
5213 error_free_vers:
5214 if (old_tab != NULL)
5215 free (old_tab);
5216 if (old_strtab != NULL)
5217 free (old_strtab);
5218 if (nondeflt_vers != NULL)
5219 free (nondeflt_vers);
5220 if (extversym != NULL)
5221 free (extversym);
5222 error_free_sym:
5223 if (isymbuf != NULL)
5224 free (isymbuf);
5225 error_return:
5226 return FALSE;
5227}
5228
5229/* Return the linker hash table entry of a symbol that might be
5230 satisfied by an archive symbol. Return -1 on error. */
5231
5232struct elf_link_hash_entry *
5233_bfd_elf_archive_symbol_lookup (bfd *abfd,
5234 struct bfd_link_info *info,
5235 const char *name)
5236{
5237 struct elf_link_hash_entry *h;
5238 char *p, *copy;
5239 size_t len, first;
5240
5241 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5242 if (h != NULL)
5243 return h;
5244
5245 /* If this is a default version (the name contains @@), look up the
5246 symbol again with only one `@' as well as without the version.
5247 The effect is that references to the symbol with and without the
5248 version will be matched by the default symbol in the archive. */
5249
5250 p = strchr (name, ELF_VER_CHR);
5251 if (p == NULL || p[1] != ELF_VER_CHR)
5252 return h;
5253
5254 /* First check with only one `@'. */
5255 len = strlen (name);
5256 copy = (char *) bfd_alloc (abfd, len);
5257 if (copy == NULL)
5258 return (struct elf_link_hash_entry *) 0 - 1;
5259
5260 first = p - name + 1;
5261 memcpy (copy, name, first);
5262 memcpy (copy + first, name + first + 1, len - first);
5263
5264 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5265 if (h == NULL)
5266 {
5267 /* We also need to check references to the symbol without the
5268 version. */
5269 copy[first - 1] = '\0';
5270 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5271 FALSE, FALSE, TRUE);
5272 }
5273
5274 bfd_release (abfd, copy);
5275 return h;
5276}
5277
5278/* Add symbols from an ELF archive file to the linker hash table. We
5279 don't use _bfd_generic_link_add_archive_symbols because we need to
5280 handle versioned symbols.
5281
5282 Fortunately, ELF archive handling is simpler than that done by
5283 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5284 oddities. In ELF, if we find a symbol in the archive map, and the
5285 symbol is currently undefined, we know that we must pull in that
5286 object file.
5287
5288 Unfortunately, we do have to make multiple passes over the symbol
5289 table until nothing further is resolved. */
5290
5291static bfd_boolean
5292elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5293{
5294 symindex c;
5295 unsigned char *included = NULL;
5296 carsym *symdefs;
5297 bfd_boolean loop;
5298 bfd_size_type amt;
5299 const struct elf_backend_data *bed;
5300 struct elf_link_hash_entry * (*archive_symbol_lookup)
5301 (bfd *, struct bfd_link_info *, const char *);
5302
5303 if (! bfd_has_map (abfd))
5304 {
5305 /* An empty archive is a special case. */
5306 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5307 return TRUE;
5308 bfd_set_error (bfd_error_no_armap);
5309 return FALSE;
5310 }
5311
5312 /* Keep track of all symbols we know to be already defined, and all
5313 files we know to be already included. This is to speed up the
5314 second and subsequent passes. */
5315 c = bfd_ardata (abfd)->symdef_count;
5316 if (c == 0)
5317 return TRUE;
5318 amt = c;
5319 amt *= sizeof (*included);
5320 included = (unsigned char *) bfd_zmalloc (amt);
5321 if (included == NULL)
5322 return FALSE;
5323
5324 symdefs = bfd_ardata (abfd)->symdefs;
5325 bed = get_elf_backend_data (abfd);
5326 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5327
5328 do
5329 {
5330 file_ptr last;
5331 symindex i;
5332 carsym *symdef;
5333 carsym *symdefend;
5334
5335 loop = FALSE;
5336 last = -1;
5337
5338 symdef = symdefs;
5339 symdefend = symdef + c;
5340 for (i = 0; symdef < symdefend; symdef++, i++)
5341 {
5342 struct elf_link_hash_entry *h;
5343 bfd *element;
5344 struct bfd_link_hash_entry *undefs_tail;
5345 symindex mark;
5346
5347 if (included[i])
5348 continue;
5349 if (symdef->file_offset == last)
5350 {
5351 included[i] = TRUE;
5352 continue;
5353 }
5354
5355 h = archive_symbol_lookup (abfd, info, symdef->name);
5356 if (h == (struct elf_link_hash_entry *) 0 - 1)
5357 goto error_return;
5358
5359 if (h == NULL)
5360 continue;
5361
5362 if (h->root.type == bfd_link_hash_common)
5363 {
5364 /* We currently have a common symbol. The archive map contains
5365 a reference to this symbol, so we may want to include it. We
5366 only want to include it however, if this archive element
5367 contains a definition of the symbol, not just another common
5368 declaration of it.
5369
5370 Unfortunately some archivers (including GNU ar) will put
5371 declarations of common symbols into their archive maps, as
5372 well as real definitions, so we cannot just go by the archive
5373 map alone. Instead we must read in the element's symbol
5374 table and check that to see what kind of symbol definition
5375 this is. */
5376 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5377 continue;
5378 }
5379 else if (h->root.type != bfd_link_hash_undefined)
5380 {
5381 if (h->root.type != bfd_link_hash_undefweak)
5382 /* Symbol must be defined. Don't check it again. */
5383 included[i] = TRUE;
5384 continue;
5385 }
5386
5387 /* We need to include this archive member. */
5388 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5389 if (element == NULL)
5390 goto error_return;
5391
5392 if (! bfd_check_format (element, bfd_object))
5393 goto error_return;
5394
5395 undefs_tail = info->hash->undefs_tail;
5396
5397 if (!(*info->callbacks
5398 ->add_archive_element) (info, element, symdef->name, &element))
5399 continue;
5400 if (!bfd_link_add_symbols (element, info))
5401 goto error_return;
5402
5403 /* If there are any new undefined symbols, we need to make
5404 another pass through the archive in order to see whether
5405 they can be defined. FIXME: This isn't perfect, because
5406 common symbols wind up on undefs_tail and because an
5407 undefined symbol which is defined later on in this pass
5408 does not require another pass. This isn't a bug, but it
5409 does make the code less efficient than it could be. */
5410 if (undefs_tail != info->hash->undefs_tail)
5411 loop = TRUE;
5412
5413 /* Look backward to mark all symbols from this object file
5414 which we have already seen in this pass. */
5415 mark = i;
5416 do
5417 {
5418 included[mark] = TRUE;
5419 if (mark == 0)
5420 break;
5421 --mark;
5422 }
5423 while (symdefs[mark].file_offset == symdef->file_offset);
5424
5425 /* We mark subsequent symbols from this object file as we go
5426 on through the loop. */
5427 last = symdef->file_offset;
5428 }
5429 }
5430 while (loop);
5431
5432 free (included);
5433
5434 return TRUE;
5435
5436 error_return:
5437 if (included != NULL)
5438 free (included);
5439 return FALSE;
5440}
5441
5442/* Given an ELF BFD, add symbols to the global hash table as
5443 appropriate. */
5444
5445bfd_boolean
5446bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5447{
5448 switch (bfd_get_format (abfd))
5449 {
5450 case bfd_object:
5451 return elf_link_add_object_symbols (abfd, info);
5452 case bfd_archive:
5453 return elf_link_add_archive_symbols (abfd, info);
5454 default:
5455 bfd_set_error (bfd_error_wrong_format);
5456 return FALSE;
5457 }
5458}
5459\f
5460struct hash_codes_info
5461{
5462 unsigned long *hashcodes;
5463 bfd_boolean error;
5464};
5465
5466/* This function will be called though elf_link_hash_traverse to store
5467 all hash value of the exported symbols in an array. */
5468
5469static bfd_boolean
5470elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5471{
5472 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5473 const char *name;
5474 unsigned long ha;
5475 char *alc = NULL;
5476
5477 /* Ignore indirect symbols. These are added by the versioning code. */
5478 if (h->dynindx == -1)
5479 return TRUE;
5480
5481 name = h->root.root.string;
5482 if (h->versioned >= versioned)
5483 {
5484 char *p = strchr (name, ELF_VER_CHR);
5485 if (p != NULL)
5486 {
5487 alc = (char *) bfd_malloc (p - name + 1);
5488 if (alc == NULL)
5489 {
5490 inf->error = TRUE;
5491 return FALSE;
5492 }
5493 memcpy (alc, name, p - name);
5494 alc[p - name] = '\0';
5495 name = alc;
5496 }
5497 }
5498
5499 /* Compute the hash value. */
5500 ha = bfd_elf_hash (name);
5501
5502 /* Store the found hash value in the array given as the argument. */
5503 *(inf->hashcodes)++ = ha;
5504
5505 /* And store it in the struct so that we can put it in the hash table
5506 later. */
5507 h->u.elf_hash_value = ha;
5508
5509 if (alc != NULL)
5510 free (alc);
5511
5512 return TRUE;
5513}
5514
5515struct collect_gnu_hash_codes
5516{
5517 bfd *output_bfd;
5518 const struct elf_backend_data *bed;
5519 unsigned long int nsyms;
5520 unsigned long int maskbits;
5521 unsigned long int *hashcodes;
5522 unsigned long int *hashval;
5523 unsigned long int *indx;
5524 unsigned long int *counts;
5525 bfd_vma *bitmask;
5526 bfd_byte *contents;
5527 long int min_dynindx;
5528 unsigned long int bucketcount;
5529 unsigned long int symindx;
5530 long int local_indx;
5531 long int shift1, shift2;
5532 unsigned long int mask;
5533 bfd_boolean error;
5534};
5535
5536/* This function will be called though elf_link_hash_traverse to store
5537 all hash value of the exported symbols in an array. */
5538
5539static bfd_boolean
5540elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5541{
5542 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5543 const char *name;
5544 unsigned long ha;
5545 char *alc = NULL;
5546
5547 /* Ignore indirect symbols. These are added by the versioning code. */
5548 if (h->dynindx == -1)
5549 return TRUE;
5550
5551 /* Ignore also local symbols and undefined symbols. */
5552 if (! (*s->bed->elf_hash_symbol) (h))
5553 return TRUE;
5554
5555 name = h->root.root.string;
5556 if (h->versioned >= versioned)
5557 {
5558 char *p = strchr (name, ELF_VER_CHR);
5559 if (p != NULL)
5560 {
5561 alc = (char *) bfd_malloc (p - name + 1);
5562 if (alc == NULL)
5563 {
5564 s->error = TRUE;
5565 return FALSE;
5566 }
5567 memcpy (alc, name, p - name);
5568 alc[p - name] = '\0';
5569 name = alc;
5570 }
5571 }
5572
5573 /* Compute the hash value. */
5574 ha = bfd_elf_gnu_hash (name);
5575
5576 /* Store the found hash value in the array for compute_bucket_count,
5577 and also for .dynsym reordering purposes. */
5578 s->hashcodes[s->nsyms] = ha;
5579 s->hashval[h->dynindx] = ha;
5580 ++s->nsyms;
5581 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5582 s->min_dynindx = h->dynindx;
5583
5584 if (alc != NULL)
5585 free (alc);
5586
5587 return TRUE;
5588}
5589
5590/* This function will be called though elf_link_hash_traverse to do
5591 final dynaminc symbol renumbering. */
5592
5593static bfd_boolean
5594elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5595{
5596 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5597 unsigned long int bucket;
5598 unsigned long int val;
5599
5600 /* Ignore indirect symbols. */
5601 if (h->dynindx == -1)
5602 return TRUE;
5603
5604 /* Ignore also local symbols and undefined symbols. */
5605 if (! (*s->bed->elf_hash_symbol) (h))
5606 {
5607 if (h->dynindx >= s->min_dynindx)
5608 h->dynindx = s->local_indx++;
5609 return TRUE;
5610 }
5611
5612 bucket = s->hashval[h->dynindx] % s->bucketcount;
5613 val = (s->hashval[h->dynindx] >> s->shift1)
5614 & ((s->maskbits >> s->shift1) - 1);
5615 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5616 s->bitmask[val]
5617 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5618 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5619 if (s->counts[bucket] == 1)
5620 /* Last element terminates the chain. */
5621 val |= 1;
5622 bfd_put_32 (s->output_bfd, val,
5623 s->contents + (s->indx[bucket] - s->symindx) * 4);
5624 --s->counts[bucket];
5625 h->dynindx = s->indx[bucket]++;
5626 return TRUE;
5627}
5628
5629/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5630
5631bfd_boolean
5632_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5633{
5634 return !(h->forced_local
5635 || h->root.type == bfd_link_hash_undefined
5636 || h->root.type == bfd_link_hash_undefweak
5637 || ((h->root.type == bfd_link_hash_defined
5638 || h->root.type == bfd_link_hash_defweak)
5639 && h->root.u.def.section->output_section == NULL));
5640}
5641
5642/* Array used to determine the number of hash table buckets to use
5643 based on the number of symbols there are. If there are fewer than
5644 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5645 fewer than 37 we use 17 buckets, and so forth. We never use more
5646 than 32771 buckets. */
5647
5648static const size_t elf_buckets[] =
5649{
5650 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5651 16411, 32771, 0
5652};
5653
5654/* Compute bucket count for hashing table. We do not use a static set
5655 of possible tables sizes anymore. Instead we determine for all
5656 possible reasonable sizes of the table the outcome (i.e., the
5657 number of collisions etc) and choose the best solution. The
5658 weighting functions are not too simple to allow the table to grow
5659 without bounds. Instead one of the weighting factors is the size.
5660 Therefore the result is always a good payoff between few collisions
5661 (= short chain lengths) and table size. */
5662static size_t
5663compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5664 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5665 unsigned long int nsyms,
5666 int gnu_hash)
5667{
5668 size_t best_size = 0;
5669 unsigned long int i;
5670
5671 /* We have a problem here. The following code to optimize the table
5672 size requires an integer type with more the 32 bits. If
5673 BFD_HOST_U_64_BIT is set we know about such a type. */
5674#ifdef BFD_HOST_U_64_BIT
5675 if (info->optimize)
5676 {
5677 size_t minsize;
5678 size_t maxsize;
5679 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5680 bfd *dynobj = elf_hash_table (info)->dynobj;
5681 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5682 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5683 unsigned long int *counts;
5684 bfd_size_type amt;
5685 unsigned int no_improvement_count = 0;
5686
5687 /* Possible optimization parameters: if we have NSYMS symbols we say
5688 that the hashing table must at least have NSYMS/4 and at most
5689 2*NSYMS buckets. */
5690 minsize = nsyms / 4;
5691 if (minsize == 0)
5692 minsize = 1;
5693 best_size = maxsize = nsyms * 2;
5694 if (gnu_hash)
5695 {
5696 if (minsize < 2)
5697 minsize = 2;
5698 if ((best_size & 31) == 0)
5699 ++best_size;
5700 }
5701
5702 /* Create array where we count the collisions in. We must use bfd_malloc
5703 since the size could be large. */
5704 amt = maxsize;
5705 amt *= sizeof (unsigned long int);
5706 counts = (unsigned long int *) bfd_malloc (amt);
5707 if (counts == NULL)
5708 return 0;
5709
5710 /* Compute the "optimal" size for the hash table. The criteria is a
5711 minimal chain length. The minor criteria is (of course) the size
5712 of the table. */
5713 for (i = minsize; i < maxsize; ++i)
5714 {
5715 /* Walk through the array of hashcodes and count the collisions. */
5716 BFD_HOST_U_64_BIT max;
5717 unsigned long int j;
5718 unsigned long int fact;
5719
5720 if (gnu_hash && (i & 31) == 0)
5721 continue;
5722
5723 memset (counts, '\0', i * sizeof (unsigned long int));
5724
5725 /* Determine how often each hash bucket is used. */
5726 for (j = 0; j < nsyms; ++j)
5727 ++counts[hashcodes[j] % i];
5728
5729 /* For the weight function we need some information about the
5730 pagesize on the target. This is information need not be 100%
5731 accurate. Since this information is not available (so far) we
5732 define it here to a reasonable default value. If it is crucial
5733 to have a better value some day simply define this value. */
5734# ifndef BFD_TARGET_PAGESIZE
5735# define BFD_TARGET_PAGESIZE (4096)
5736# endif
5737
5738 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5739 and the chains. */
5740 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5741
5742# if 1
5743 /* Variant 1: optimize for short chains. We add the squares
5744 of all the chain lengths (which favors many small chain
5745 over a few long chains). */
5746 for (j = 0; j < i; ++j)
5747 max += counts[j] * counts[j];
5748
5749 /* This adds penalties for the overall size of the table. */
5750 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5751 max *= fact * fact;
5752# else
5753 /* Variant 2: Optimize a lot more for small table. Here we
5754 also add squares of the size but we also add penalties for
5755 empty slots (the +1 term). */
5756 for (j = 0; j < i; ++j)
5757 max += (1 + counts[j]) * (1 + counts[j]);
5758
5759 /* The overall size of the table is considered, but not as
5760 strong as in variant 1, where it is squared. */
5761 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5762 max *= fact;
5763# endif
5764
5765 /* Compare with current best results. */
5766 if (max < best_chlen)
5767 {
5768 best_chlen = max;
5769 best_size = i;
5770 no_improvement_count = 0;
5771 }
5772 /* PR 11843: Avoid futile long searches for the best bucket size
5773 when there are a large number of symbols. */
5774 else if (++no_improvement_count == 100)
5775 break;
5776 }
5777
5778 free (counts);
5779 }
5780 else
5781#endif /* defined (BFD_HOST_U_64_BIT) */
5782 {
5783 /* This is the fallback solution if no 64bit type is available or if we
5784 are not supposed to spend much time on optimizations. We select the
5785 bucket count using a fixed set of numbers. */
5786 for (i = 0; elf_buckets[i] != 0; i++)
5787 {
5788 best_size = elf_buckets[i];
5789 if (nsyms < elf_buckets[i + 1])
5790 break;
5791 }
5792 if (gnu_hash && best_size < 2)
5793 best_size = 2;
5794 }
5795
5796 return best_size;
5797}
5798
5799/* Size any SHT_GROUP section for ld -r. */
5800
5801bfd_boolean
5802_bfd_elf_size_group_sections (struct bfd_link_info *info)
5803{
5804 bfd *ibfd;
5805
5806 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5807 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5808 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5809 return FALSE;
5810 return TRUE;
5811}
5812
5813/* Set a default stack segment size. The value in INFO wins. If it
5814 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5815 undefined it is initialized. */
5816
5817bfd_boolean
5818bfd_elf_stack_segment_size (bfd *output_bfd,
5819 struct bfd_link_info *info,
5820 const char *legacy_symbol,
5821 bfd_vma default_size)
5822{
5823 struct elf_link_hash_entry *h = NULL;
5824
5825 /* Look for legacy symbol. */
5826 if (legacy_symbol)
5827 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5828 FALSE, FALSE, FALSE);
5829 if (h && (h->root.type == bfd_link_hash_defined
5830 || h->root.type == bfd_link_hash_defweak)
5831 && h->def_regular
5832 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5833 {
5834 /* The symbol has no type if specified on the command line. */
5835 h->type = STT_OBJECT;
5836 if (info->stacksize)
5837 /* xgettext:c-format */
5838 _bfd_error_handler (_("%B: stack size specified and %s set"),
5839 output_bfd, legacy_symbol);
5840 else if (h->root.u.def.section != bfd_abs_section_ptr)
5841 /* xgettext:c-format */
5842 _bfd_error_handler (_("%B: %s not absolute"),
5843 output_bfd, legacy_symbol);
5844 else
5845 info->stacksize = h->root.u.def.value;
5846 }
5847
5848 if (!info->stacksize)
5849 /* If the user didn't set a size, or explicitly inhibit the
5850 size, set it now. */
5851 info->stacksize = default_size;
5852
5853 /* Provide the legacy symbol, if it is referenced. */
5854 if (h && (h->root.type == bfd_link_hash_undefined
5855 || h->root.type == bfd_link_hash_undefweak))
5856 {
5857 struct bfd_link_hash_entry *bh = NULL;
5858
5859 if (!(_bfd_generic_link_add_one_symbol
5860 (info, output_bfd, legacy_symbol,
5861 BSF_GLOBAL, bfd_abs_section_ptr,
5862 info->stacksize >= 0 ? info->stacksize : 0,
5863 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5864 return FALSE;
5865
5866 h = (struct elf_link_hash_entry *) bh;
5867 h->def_regular = 1;
5868 h->type = STT_OBJECT;
5869 }
5870
5871 return TRUE;
5872}
5873
5874/* Set up the sizes and contents of the ELF dynamic sections. This is
5875 called by the ELF linker emulation before_allocation routine. We
5876 must set the sizes of the sections before the linker sets the
5877 addresses of the various sections. */
5878
5879bfd_boolean
5880bfd_elf_size_dynamic_sections (bfd *output_bfd,
5881 const char *soname,
5882 const char *rpath,
5883 const char *filter_shlib,
5884 const char *audit,
5885 const char *depaudit,
5886 const char * const *auxiliary_filters,
5887 struct bfd_link_info *info,
5888 asection **sinterpptr)
5889{
5890 size_t soname_indx;
5891 bfd *dynobj;
5892 const struct elf_backend_data *bed;
5893 struct elf_info_failed asvinfo;
5894
5895 *sinterpptr = NULL;
5896
5897 soname_indx = (size_t) -1;
5898
5899 if (!is_elf_hash_table (info->hash))
5900 return TRUE;
5901
5902 bed = get_elf_backend_data (output_bfd);
5903
5904 /* Any syms created from now on start with -1 in
5905 got.refcount/offset and plt.refcount/offset. */
5906 elf_hash_table (info)->init_got_refcount
5907 = elf_hash_table (info)->init_got_offset;
5908 elf_hash_table (info)->init_plt_refcount
5909 = elf_hash_table (info)->init_plt_offset;
5910
5911 if (bfd_link_relocatable (info)
5912 && !_bfd_elf_size_group_sections (info))
5913 return FALSE;
5914
5915 /* The backend may have to create some sections regardless of whether
5916 we're dynamic or not. */
5917 if (bed->elf_backend_always_size_sections
5918 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5919 return FALSE;
5920
5921 /* Determine any GNU_STACK segment requirements, after the backend
5922 has had a chance to set a default segment size. */
5923 if (info->execstack)
5924 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5925 else if (info->noexecstack)
5926 elf_stack_flags (output_bfd) = PF_R | PF_W;
5927 else
5928 {
5929 bfd *inputobj;
5930 asection *notesec = NULL;
5931 int exec = 0;
5932
5933 for (inputobj = info->input_bfds;
5934 inputobj;
5935 inputobj = inputobj->link.next)
5936 {
5937 asection *s;
5938
5939 if (inputobj->flags
5940 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5941 continue;
5942 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5943 if (s)
5944 {
5945 if (s->flags & SEC_CODE)
5946 exec = PF_X;
5947 notesec = s;
5948 }
5949 else if (bed->default_execstack)
5950 exec = PF_X;
5951 }
5952 if (notesec || info->stacksize > 0)
5953 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5954 if (notesec && exec && bfd_link_relocatable (info)
5955 && notesec->output_section != bfd_abs_section_ptr)
5956 notesec->output_section->flags |= SEC_CODE;
5957 }
5958
5959 dynobj = elf_hash_table (info)->dynobj;
5960
5961 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5962 {
5963 struct elf_info_failed eif;
5964 struct elf_link_hash_entry *h;
5965 asection *dynstr;
5966 struct bfd_elf_version_tree *t;
5967 struct bfd_elf_version_expr *d;
5968 asection *s;
5969 bfd_boolean all_defined;
5970
5971 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5972 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5973
5974 if (soname != NULL)
5975 {
5976 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5977 soname, TRUE);
5978 if (soname_indx == (size_t) -1
5979 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5980 return FALSE;
5981 }
5982
5983 if (info->symbolic)
5984 {
5985 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5986 return FALSE;
5987 info->flags |= DF_SYMBOLIC;
5988 }
5989
5990 if (rpath != NULL)
5991 {
5992 size_t indx;
5993 bfd_vma tag;
5994
5995 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5996 TRUE);
5997 if (indx == (size_t) -1)
5998 return FALSE;
5999
6000 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6001 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6002 return FALSE;
6003 }
6004
6005 if (filter_shlib != NULL)
6006 {
6007 size_t indx;
6008
6009 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6010 filter_shlib, TRUE);
6011 if (indx == (size_t) -1
6012 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6013 return FALSE;
6014 }
6015
6016 if (auxiliary_filters != NULL)
6017 {
6018 const char * const *p;
6019
6020 for (p = auxiliary_filters; *p != NULL; p++)
6021 {
6022 size_t indx;
6023
6024 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6025 *p, TRUE);
6026 if (indx == (size_t) -1
6027 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6028 return FALSE;
6029 }
6030 }
6031
6032 if (audit != NULL)
6033 {
6034 size_t indx;
6035
6036 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6037 TRUE);
6038 if (indx == (size_t) -1
6039 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6040 return FALSE;
6041 }
6042
6043 if (depaudit != NULL)
6044 {
6045 size_t indx;
6046
6047 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6048 TRUE);
6049 if (indx == (size_t) -1
6050 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6051 return FALSE;
6052 }
6053
6054 eif.info = info;
6055 eif.failed = FALSE;
6056
6057 /* If we are supposed to export all symbols into the dynamic symbol
6058 table (this is not the normal case), then do so. */
6059 if (info->export_dynamic
6060 || (bfd_link_executable (info) && info->dynamic))
6061 {
6062 elf_link_hash_traverse (elf_hash_table (info),
6063 _bfd_elf_export_symbol,
6064 &eif);
6065 if (eif.failed)
6066 return FALSE;
6067 }
6068
6069 /* Make all global versions with definition. */
6070 for (t = info->version_info; t != NULL; t = t->next)
6071 for (d = t->globals.list; d != NULL; d = d->next)
6072 if (!d->symver && d->literal)
6073 {
6074 const char *verstr, *name;
6075 size_t namelen, verlen, newlen;
6076 char *newname, *p, leading_char;
6077 struct elf_link_hash_entry *newh;
6078
6079 leading_char = bfd_get_symbol_leading_char (output_bfd);
6080 name = d->pattern;
6081 namelen = strlen (name) + (leading_char != '\0');
6082 verstr = t->name;
6083 verlen = strlen (verstr);
6084 newlen = namelen + verlen + 3;
6085
6086 newname = (char *) bfd_malloc (newlen);
6087 if (newname == NULL)
6088 return FALSE;
6089 newname[0] = leading_char;
6090 memcpy (newname + (leading_char != '\0'), name, namelen);
6091
6092 /* Check the hidden versioned definition. */
6093 p = newname + namelen;
6094 *p++ = ELF_VER_CHR;
6095 memcpy (p, verstr, verlen + 1);
6096 newh = elf_link_hash_lookup (elf_hash_table (info),
6097 newname, FALSE, FALSE,
6098 FALSE);
6099 if (newh == NULL
6100 || (newh->root.type != bfd_link_hash_defined
6101 && newh->root.type != bfd_link_hash_defweak))
6102 {
6103 /* Check the default versioned definition. */
6104 *p++ = ELF_VER_CHR;
6105 memcpy (p, verstr, verlen + 1);
6106 newh = elf_link_hash_lookup (elf_hash_table (info),
6107 newname, FALSE, FALSE,
6108 FALSE);
6109 }
6110 free (newname);
6111
6112 /* Mark this version if there is a definition and it is
6113 not defined in a shared object. */
6114 if (newh != NULL
6115 && !newh->def_dynamic
6116 && (newh->root.type == bfd_link_hash_defined
6117 || newh->root.type == bfd_link_hash_defweak))
6118 d->symver = 1;
6119 }
6120
6121 /* Attach all the symbols to their version information. */
6122 asvinfo.info = info;
6123 asvinfo.failed = FALSE;
6124
6125 elf_link_hash_traverse (elf_hash_table (info),
6126 _bfd_elf_link_assign_sym_version,
6127 &asvinfo);
6128 if (asvinfo.failed)
6129 return FALSE;
6130
6131 if (!info->allow_undefined_version)
6132 {
6133 /* Check if all global versions have a definition. */
6134 all_defined = TRUE;
6135 for (t = info->version_info; t != NULL; t = t->next)
6136 for (d = t->globals.list; d != NULL; d = d->next)
6137 if (d->literal && !d->symver && !d->script)
6138 {
6139 _bfd_error_handler
6140 (_("%s: undefined version: %s"),
6141 d->pattern, t->name);
6142 all_defined = FALSE;
6143 }
6144
6145 if (!all_defined)
6146 {
6147 bfd_set_error (bfd_error_bad_value);
6148 return FALSE;
6149 }
6150 }
6151
6152 /* Find all symbols which were defined in a dynamic object and make
6153 the backend pick a reasonable value for them. */
6154 elf_link_hash_traverse (elf_hash_table (info),
6155 _bfd_elf_adjust_dynamic_symbol,
6156 &eif);
6157 if (eif.failed)
6158 return FALSE;
6159
6160 /* Add some entries to the .dynamic section. We fill in some of the
6161 values later, in bfd_elf_final_link, but we must add the entries
6162 now so that we know the final size of the .dynamic section. */
6163
6164 /* If there are initialization and/or finalization functions to
6165 call then add the corresponding DT_INIT/DT_FINI entries. */
6166 h = (info->init_function
6167 ? elf_link_hash_lookup (elf_hash_table (info),
6168 info->init_function, FALSE,
6169 FALSE, FALSE)
6170 : NULL);
6171 if (h != NULL
6172 && (h->ref_regular
6173 || h->def_regular))
6174 {
6175 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6176 return FALSE;
6177 }
6178 h = (info->fini_function
6179 ? elf_link_hash_lookup (elf_hash_table (info),
6180 info->fini_function, FALSE,
6181 FALSE, FALSE)
6182 : NULL);
6183 if (h != NULL
6184 && (h->ref_regular
6185 || h->def_regular))
6186 {
6187 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6188 return FALSE;
6189 }
6190
6191 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6192 if (s != NULL && s->linker_has_input)
6193 {
6194 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6195 if (! bfd_link_executable (info))
6196 {
6197 bfd *sub;
6198 asection *o;
6199
6200 for (sub = info->input_bfds; sub != NULL;
6201 sub = sub->link.next)
6202 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6203 for (o = sub->sections; o != NULL; o = o->next)
6204 if (elf_section_data (o)->this_hdr.sh_type
6205 == SHT_PREINIT_ARRAY)
6206 {
6207 _bfd_error_handler
6208 (_("%B: .preinit_array section is not allowed in DSO"),
6209 sub);
6210 break;
6211 }
6212
6213 bfd_set_error (bfd_error_nonrepresentable_section);
6214 return FALSE;
6215 }
6216
6217 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6218 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6219 return FALSE;
6220 }
6221 s = bfd_get_section_by_name (output_bfd, ".init_array");
6222 if (s != NULL && s->linker_has_input)
6223 {
6224 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6225 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6226 return FALSE;
6227 }
6228 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6229 if (s != NULL && s->linker_has_input)
6230 {
6231 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6232 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6233 return FALSE;
6234 }
6235
6236 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6237 /* If .dynstr is excluded from the link, we don't want any of
6238 these tags. Strictly, we should be checking each section
6239 individually; This quick check covers for the case where
6240 someone does a /DISCARD/ : { *(*) }. */
6241 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6242 {
6243 bfd_size_type strsize;
6244
6245 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6246 if ((info->emit_hash
6247 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6248 || (info->emit_gnu_hash
6249 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6250 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6251 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6252 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6253 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6254 bed->s->sizeof_sym))
6255 return FALSE;
6256 }
6257 }
6258
6259 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6260 return FALSE;
6261
6262 /* The backend must work out the sizes of all the other dynamic
6263 sections. */
6264 if (dynobj != NULL
6265 && bed->elf_backend_size_dynamic_sections != NULL
6266 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6267 return FALSE;
6268
6269 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6270 {
6271 unsigned long section_sym_count;
6272 struct bfd_elf_version_tree *verdefs;
6273 asection *s;
6274
6275 /* Set up the version definition section. */
6276 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6277 BFD_ASSERT (s != NULL);
6278
6279 /* We may have created additional version definitions if we are
6280 just linking a regular application. */
6281 verdefs = info->version_info;
6282
6283 /* Skip anonymous version tag. */
6284 if (verdefs != NULL && verdefs->vernum == 0)
6285 verdefs = verdefs->next;
6286
6287 if (verdefs == NULL && !info->create_default_symver)
6288 s->flags |= SEC_EXCLUDE;
6289 else
6290 {
6291 unsigned int cdefs;
6292 bfd_size_type size;
6293 struct bfd_elf_version_tree *t;
6294 bfd_byte *p;
6295 Elf_Internal_Verdef def;
6296 Elf_Internal_Verdaux defaux;
6297 struct bfd_link_hash_entry *bh;
6298 struct elf_link_hash_entry *h;
6299 const char *name;
6300
6301 cdefs = 0;
6302 size = 0;
6303
6304 /* Make space for the base version. */
6305 size += sizeof (Elf_External_Verdef);
6306 size += sizeof (Elf_External_Verdaux);
6307 ++cdefs;
6308
6309 /* Make space for the default version. */
6310 if (info->create_default_symver)
6311 {
6312 size += sizeof (Elf_External_Verdef);
6313 ++cdefs;
6314 }
6315
6316 for (t = verdefs; t != NULL; t = t->next)
6317 {
6318 struct bfd_elf_version_deps *n;
6319
6320 /* Don't emit base version twice. */
6321 if (t->vernum == 0)
6322 continue;
6323
6324 size += sizeof (Elf_External_Verdef);
6325 size += sizeof (Elf_External_Verdaux);
6326 ++cdefs;
6327
6328 for (n = t->deps; n != NULL; n = n->next)
6329 size += sizeof (Elf_External_Verdaux);
6330 }
6331
6332 s->size = size;
6333 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6334 if (s->contents == NULL && s->size != 0)
6335 return FALSE;
6336
6337 /* Fill in the version definition section. */
6338
6339 p = s->contents;
6340
6341 def.vd_version = VER_DEF_CURRENT;
6342 def.vd_flags = VER_FLG_BASE;
6343 def.vd_ndx = 1;
6344 def.vd_cnt = 1;
6345 if (info->create_default_symver)
6346 {
6347 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6348 def.vd_next = sizeof (Elf_External_Verdef);
6349 }
6350 else
6351 {
6352 def.vd_aux = sizeof (Elf_External_Verdef);
6353 def.vd_next = (sizeof (Elf_External_Verdef)
6354 + sizeof (Elf_External_Verdaux));
6355 }
6356
6357 if (soname_indx != (size_t) -1)
6358 {
6359 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6360 soname_indx);
6361 def.vd_hash = bfd_elf_hash (soname);
6362 defaux.vda_name = soname_indx;
6363 name = soname;
6364 }
6365 else
6366 {
6367 size_t indx;
6368
6369 name = lbasename (output_bfd->filename);
6370 def.vd_hash = bfd_elf_hash (name);
6371 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6372 name, FALSE);
6373 if (indx == (size_t) -1)
6374 return FALSE;
6375 defaux.vda_name = indx;
6376 }
6377 defaux.vda_next = 0;
6378
6379 _bfd_elf_swap_verdef_out (output_bfd, &def,
6380 (Elf_External_Verdef *) p);
6381 p += sizeof (Elf_External_Verdef);
6382 if (info->create_default_symver)
6383 {
6384 /* Add a symbol representing this version. */
6385 bh = NULL;
6386 if (! (_bfd_generic_link_add_one_symbol
6387 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6388 0, NULL, FALSE,
6389 get_elf_backend_data (dynobj)->collect, &bh)))
6390 return FALSE;
6391 h = (struct elf_link_hash_entry *) bh;
6392 h->non_elf = 0;
6393 h->def_regular = 1;
6394 h->type = STT_OBJECT;
6395 h->verinfo.vertree = NULL;
6396
6397 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6398 return FALSE;
6399
6400 /* Create a duplicate of the base version with the same
6401 aux block, but different flags. */
6402 def.vd_flags = 0;
6403 def.vd_ndx = 2;
6404 def.vd_aux = sizeof (Elf_External_Verdef);
6405 if (verdefs)
6406 def.vd_next = (sizeof (Elf_External_Verdef)
6407 + sizeof (Elf_External_Verdaux));
6408 else
6409 def.vd_next = 0;
6410 _bfd_elf_swap_verdef_out (output_bfd, &def,
6411 (Elf_External_Verdef *) p);
6412 p += sizeof (Elf_External_Verdef);
6413 }
6414 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6415 (Elf_External_Verdaux *) p);
6416 p += sizeof (Elf_External_Verdaux);
6417
6418 for (t = verdefs; t != NULL; t = t->next)
6419 {
6420 unsigned int cdeps;
6421 struct bfd_elf_version_deps *n;
6422
6423 /* Don't emit the base version twice. */
6424 if (t->vernum == 0)
6425 continue;
6426
6427 cdeps = 0;
6428 for (n = t->deps; n != NULL; n = n->next)
6429 ++cdeps;
6430
6431 /* Add a symbol representing this version. */
6432 bh = NULL;
6433 if (! (_bfd_generic_link_add_one_symbol
6434 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6435 0, NULL, FALSE,
6436 get_elf_backend_data (dynobj)->collect, &bh)))
6437 return FALSE;
6438 h = (struct elf_link_hash_entry *) bh;
6439 h->non_elf = 0;
6440 h->def_regular = 1;
6441 h->type = STT_OBJECT;
6442 h->verinfo.vertree = t;
6443
6444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6445 return FALSE;
6446
6447 def.vd_version = VER_DEF_CURRENT;
6448 def.vd_flags = 0;
6449 if (t->globals.list == NULL
6450 && t->locals.list == NULL
6451 && ! t->used)
6452 def.vd_flags |= VER_FLG_WEAK;
6453 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6454 def.vd_cnt = cdeps + 1;
6455 def.vd_hash = bfd_elf_hash (t->name);
6456 def.vd_aux = sizeof (Elf_External_Verdef);
6457 def.vd_next = 0;
6458
6459 /* If a basever node is next, it *must* be the last node in
6460 the chain, otherwise Verdef construction breaks. */
6461 if (t->next != NULL && t->next->vernum == 0)
6462 BFD_ASSERT (t->next->next == NULL);
6463
6464 if (t->next != NULL && t->next->vernum != 0)
6465 def.vd_next = (sizeof (Elf_External_Verdef)
6466 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6467
6468 _bfd_elf_swap_verdef_out (output_bfd, &def,
6469 (Elf_External_Verdef *) p);
6470 p += sizeof (Elf_External_Verdef);
6471
6472 defaux.vda_name = h->dynstr_index;
6473 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6474 h->dynstr_index);
6475 defaux.vda_next = 0;
6476 if (t->deps != NULL)
6477 defaux.vda_next = sizeof (Elf_External_Verdaux);
6478 t->name_indx = defaux.vda_name;
6479
6480 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6481 (Elf_External_Verdaux *) p);
6482 p += sizeof (Elf_External_Verdaux);
6483
6484 for (n = t->deps; n != NULL; n = n->next)
6485 {
6486 if (n->version_needed == NULL)
6487 {
6488 /* This can happen if there was an error in the
6489 version script. */
6490 defaux.vda_name = 0;
6491 }
6492 else
6493 {
6494 defaux.vda_name = n->version_needed->name_indx;
6495 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6496 defaux.vda_name);
6497 }
6498 if (n->next == NULL)
6499 defaux.vda_next = 0;
6500 else
6501 defaux.vda_next = sizeof (Elf_External_Verdaux);
6502
6503 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6504 (Elf_External_Verdaux *) p);
6505 p += sizeof (Elf_External_Verdaux);
6506 }
6507 }
6508
6509 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6510 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6511 return FALSE;
6512
6513 elf_tdata (output_bfd)->cverdefs = cdefs;
6514 }
6515
6516 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6517 {
6518 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6519 return FALSE;
6520 }
6521 else if (info->flags & DF_BIND_NOW)
6522 {
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6524 return FALSE;
6525 }
6526
6527 if (info->flags_1)
6528 {
6529 if (bfd_link_executable (info))
6530 info->flags_1 &= ~ (DF_1_INITFIRST
6531 | DF_1_NODELETE
6532 | DF_1_NOOPEN);
6533 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6534 return FALSE;
6535 }
6536
6537 /* Work out the size of the version reference section. */
6538
6539 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6540 BFD_ASSERT (s != NULL);
6541 {
6542 struct elf_find_verdep_info sinfo;
6543
6544 sinfo.info = info;
6545 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6546 if (sinfo.vers == 0)
6547 sinfo.vers = 1;
6548 sinfo.failed = FALSE;
6549
6550 elf_link_hash_traverse (elf_hash_table (info),
6551 _bfd_elf_link_find_version_dependencies,
6552 &sinfo);
6553 if (sinfo.failed)
6554 return FALSE;
6555
6556 if (elf_tdata (output_bfd)->verref == NULL)
6557 s->flags |= SEC_EXCLUDE;
6558 else
6559 {
6560 Elf_Internal_Verneed *t;
6561 unsigned int size;
6562 unsigned int crefs;
6563 bfd_byte *p;
6564
6565 /* Build the version dependency section. */
6566 size = 0;
6567 crefs = 0;
6568 for (t = elf_tdata (output_bfd)->verref;
6569 t != NULL;
6570 t = t->vn_nextref)
6571 {
6572 Elf_Internal_Vernaux *a;
6573
6574 size += sizeof (Elf_External_Verneed);
6575 ++crefs;
6576 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6577 size += sizeof (Elf_External_Vernaux);
6578 }
6579
6580 s->size = size;
6581 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6582 if (s->contents == NULL)
6583 return FALSE;
6584
6585 p = s->contents;
6586 for (t = elf_tdata (output_bfd)->verref;
6587 t != NULL;
6588 t = t->vn_nextref)
6589 {
6590 unsigned int caux;
6591 Elf_Internal_Vernaux *a;
6592 size_t indx;
6593
6594 caux = 0;
6595 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6596 ++caux;
6597
6598 t->vn_version = VER_NEED_CURRENT;
6599 t->vn_cnt = caux;
6600 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6601 elf_dt_name (t->vn_bfd) != NULL
6602 ? elf_dt_name (t->vn_bfd)
6603 : lbasename (t->vn_bfd->filename),
6604 FALSE);
6605 if (indx == (size_t) -1)
6606 return FALSE;
6607 t->vn_file = indx;
6608 t->vn_aux = sizeof (Elf_External_Verneed);
6609 if (t->vn_nextref == NULL)
6610 t->vn_next = 0;
6611 else
6612 t->vn_next = (sizeof (Elf_External_Verneed)
6613 + caux * sizeof (Elf_External_Vernaux));
6614
6615 _bfd_elf_swap_verneed_out (output_bfd, t,
6616 (Elf_External_Verneed *) p);
6617 p += sizeof (Elf_External_Verneed);
6618
6619 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6620 {
6621 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6622 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6623 a->vna_nodename, FALSE);
6624 if (indx == (size_t) -1)
6625 return FALSE;
6626 a->vna_name = indx;
6627 if (a->vna_nextptr == NULL)
6628 a->vna_next = 0;
6629 else
6630 a->vna_next = sizeof (Elf_External_Vernaux);
6631
6632 _bfd_elf_swap_vernaux_out (output_bfd, a,
6633 (Elf_External_Vernaux *) p);
6634 p += sizeof (Elf_External_Vernaux);
6635 }
6636 }
6637
6638 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6639 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6640 return FALSE;
6641
6642 elf_tdata (output_bfd)->cverrefs = crefs;
6643 }
6644 }
6645
6646 if ((elf_tdata (output_bfd)->cverrefs == 0
6647 && elf_tdata (output_bfd)->cverdefs == 0)
6648 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6649 &section_sym_count) == 0)
6650 {
6651 s = bfd_get_linker_section (dynobj, ".gnu.version");
6652 s->flags |= SEC_EXCLUDE;
6653 }
6654 }
6655 return TRUE;
6656}
6657
6658/* Find the first non-excluded output section. We'll use its
6659 section symbol for some emitted relocs. */
6660void
6661_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6662{
6663 asection *s;
6664
6665 for (s = output_bfd->sections; s != NULL; s = s->next)
6666 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6667 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6668 {
6669 elf_hash_table (info)->text_index_section = s;
6670 break;
6671 }
6672}
6673
6674/* Find two non-excluded output sections, one for code, one for data.
6675 We'll use their section symbols for some emitted relocs. */
6676void
6677_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6678{
6679 asection *s;
6680
6681 /* Data first, since setting text_index_section changes
6682 _bfd_elf_link_omit_section_dynsym. */
6683 for (s = output_bfd->sections; s != NULL; s = s->next)
6684 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6685 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6686 {
6687 elf_hash_table (info)->data_index_section = s;
6688 break;
6689 }
6690
6691 for (s = output_bfd->sections; s != NULL; s = s->next)
6692 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6693 == (SEC_ALLOC | SEC_READONLY))
6694 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6695 {
6696 elf_hash_table (info)->text_index_section = s;
6697 break;
6698 }
6699
6700 if (elf_hash_table (info)->text_index_section == NULL)
6701 elf_hash_table (info)->text_index_section
6702 = elf_hash_table (info)->data_index_section;
6703}
6704
6705bfd_boolean
6706bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6707{
6708 const struct elf_backend_data *bed;
6709
6710 if (!is_elf_hash_table (info->hash))
6711 return TRUE;
6712
6713 bed = get_elf_backend_data (output_bfd);
6714 (*bed->elf_backend_init_index_section) (output_bfd, info);
6715
6716 if (elf_hash_table (info)->dynamic_sections_created)
6717 {
6718 bfd *dynobj;
6719 asection *s;
6720 bfd_size_type dynsymcount;
6721 unsigned long section_sym_count;
6722 unsigned int dtagcount;
6723
6724 dynobj = elf_hash_table (info)->dynobj;
6725
6726 /* Assign dynsym indicies. In a shared library we generate a
6727 section symbol for each output section, which come first.
6728 Next come all of the back-end allocated local dynamic syms,
6729 followed by the rest of the global symbols. */
6730
6731 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6732 &section_sym_count);
6733
6734 /* Work out the size of the symbol version section. */
6735 s = bfd_get_linker_section (dynobj, ".gnu.version");
6736 BFD_ASSERT (s != NULL);
6737 if ((s->flags & SEC_EXCLUDE) == 0)
6738 {
6739 s->size = dynsymcount * sizeof (Elf_External_Versym);
6740 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6741 if (s->contents == NULL)
6742 return FALSE;
6743
6744 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6745 return FALSE;
6746 }
6747
6748 /* Set the size of the .dynsym and .hash sections. We counted
6749 the number of dynamic symbols in elf_link_add_object_symbols.
6750 We will build the contents of .dynsym and .hash when we build
6751 the final symbol table, because until then we do not know the
6752 correct value to give the symbols. We built the .dynstr
6753 section as we went along in elf_link_add_object_symbols. */
6754 s = elf_hash_table (info)->dynsym;
6755 BFD_ASSERT (s != NULL);
6756 s->size = dynsymcount * bed->s->sizeof_sym;
6757
6758 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6759 if (s->contents == NULL)
6760 return FALSE;
6761
6762 /* The first entry in .dynsym is a dummy symbol. Clear all the
6763 section syms, in case we don't output them all. */
6764 ++section_sym_count;
6765 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6766
6767 elf_hash_table (info)->bucketcount = 0;
6768
6769 /* Compute the size of the hashing table. As a side effect this
6770 computes the hash values for all the names we export. */
6771 if (info->emit_hash)
6772 {
6773 unsigned long int *hashcodes;
6774 struct hash_codes_info hashinf;
6775 bfd_size_type amt;
6776 unsigned long int nsyms;
6777 size_t bucketcount;
6778 size_t hash_entry_size;
6779
6780 /* Compute the hash values for all exported symbols. At the same
6781 time store the values in an array so that we could use them for
6782 optimizations. */
6783 amt = dynsymcount * sizeof (unsigned long int);
6784 hashcodes = (unsigned long int *) bfd_malloc (amt);
6785 if (hashcodes == NULL)
6786 return FALSE;
6787 hashinf.hashcodes = hashcodes;
6788 hashinf.error = FALSE;
6789
6790 /* Put all hash values in HASHCODES. */
6791 elf_link_hash_traverse (elf_hash_table (info),
6792 elf_collect_hash_codes, &hashinf);
6793 if (hashinf.error)
6794 {
6795 free (hashcodes);
6796 return FALSE;
6797 }
6798
6799 nsyms = hashinf.hashcodes - hashcodes;
6800 bucketcount
6801 = compute_bucket_count (info, hashcodes, nsyms, 0);
6802 free (hashcodes);
6803
6804 if (bucketcount == 0)
6805 return FALSE;
6806
6807 elf_hash_table (info)->bucketcount = bucketcount;
6808
6809 s = bfd_get_linker_section (dynobj, ".hash");
6810 BFD_ASSERT (s != NULL);
6811 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6812 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6813 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6814 if (s->contents == NULL)
6815 return FALSE;
6816
6817 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6818 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6819 s->contents + hash_entry_size);
6820 }
6821
6822 if (info->emit_gnu_hash)
6823 {
6824 size_t i, cnt;
6825 unsigned char *contents;
6826 struct collect_gnu_hash_codes cinfo;
6827 bfd_size_type amt;
6828 size_t bucketcount;
6829
6830 memset (&cinfo, 0, sizeof (cinfo));
6831
6832 /* Compute the hash values for all exported symbols. At the same
6833 time store the values in an array so that we could use them for
6834 optimizations. */
6835 amt = dynsymcount * 2 * sizeof (unsigned long int);
6836 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6837 if (cinfo.hashcodes == NULL)
6838 return FALSE;
6839
6840 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6841 cinfo.min_dynindx = -1;
6842 cinfo.output_bfd = output_bfd;
6843 cinfo.bed = bed;
6844
6845 /* Put all hash values in HASHCODES. */
6846 elf_link_hash_traverse (elf_hash_table (info),
6847 elf_collect_gnu_hash_codes, &cinfo);
6848 if (cinfo.error)
6849 {
6850 free (cinfo.hashcodes);
6851 return FALSE;
6852 }
6853
6854 bucketcount
6855 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6856
6857 if (bucketcount == 0)
6858 {
6859 free (cinfo.hashcodes);
6860 return FALSE;
6861 }
6862
6863 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6864 BFD_ASSERT (s != NULL);
6865
6866 if (cinfo.nsyms == 0)
6867 {
6868 /* Empty .gnu.hash section is special. */
6869 BFD_ASSERT (cinfo.min_dynindx == -1);
6870 free (cinfo.hashcodes);
6871 s->size = 5 * 4 + bed->s->arch_size / 8;
6872 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6873 if (contents == NULL)
6874 return FALSE;
6875 s->contents = contents;
6876 /* 1 empty bucket. */
6877 bfd_put_32 (output_bfd, 1, contents);
6878 /* SYMIDX above the special symbol 0. */
6879 bfd_put_32 (output_bfd, 1, contents + 4);
6880 /* Just one word for bitmask. */
6881 bfd_put_32 (output_bfd, 1, contents + 8);
6882 /* Only hash fn bloom filter. */
6883 bfd_put_32 (output_bfd, 0, contents + 12);
6884 /* No hashes are valid - empty bitmask. */
6885 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6886 /* No hashes in the only bucket. */
6887 bfd_put_32 (output_bfd, 0,
6888 contents + 16 + bed->s->arch_size / 8);
6889 }
6890 else
6891 {
6892 unsigned long int maskwords, maskbitslog2, x;
6893 BFD_ASSERT (cinfo.min_dynindx != -1);
6894
6895 x = cinfo.nsyms;
6896 maskbitslog2 = 1;
6897 while ((x >>= 1) != 0)
6898 ++maskbitslog2;
6899 if (maskbitslog2 < 3)
6900 maskbitslog2 = 5;
6901 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6902 maskbitslog2 = maskbitslog2 + 3;
6903 else
6904 maskbitslog2 = maskbitslog2 + 2;
6905 if (bed->s->arch_size == 64)
6906 {
6907 if (maskbitslog2 == 5)
6908 maskbitslog2 = 6;
6909 cinfo.shift1 = 6;
6910 }
6911 else
6912 cinfo.shift1 = 5;
6913 cinfo.mask = (1 << cinfo.shift1) - 1;
6914 cinfo.shift2 = maskbitslog2;
6915 cinfo.maskbits = 1 << maskbitslog2;
6916 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6917 amt = bucketcount * sizeof (unsigned long int) * 2;
6918 amt += maskwords * sizeof (bfd_vma);
6919 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6920 if (cinfo.bitmask == NULL)
6921 {
6922 free (cinfo.hashcodes);
6923 return FALSE;
6924 }
6925
6926 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6927 cinfo.indx = cinfo.counts + bucketcount;
6928 cinfo.symindx = dynsymcount - cinfo.nsyms;
6929 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6930
6931 /* Determine how often each hash bucket is used. */
6932 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6933 for (i = 0; i < cinfo.nsyms; ++i)
6934 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6935
6936 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6937 if (cinfo.counts[i] != 0)
6938 {
6939 cinfo.indx[i] = cnt;
6940 cnt += cinfo.counts[i];
6941 }
6942 BFD_ASSERT (cnt == dynsymcount);
6943 cinfo.bucketcount = bucketcount;
6944 cinfo.local_indx = cinfo.min_dynindx;
6945
6946 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6947 s->size += cinfo.maskbits / 8;
6948 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6949 if (contents == NULL)
6950 {
6951 free (cinfo.bitmask);
6952 free (cinfo.hashcodes);
6953 return FALSE;
6954 }
6955
6956 s->contents = contents;
6957 bfd_put_32 (output_bfd, bucketcount, contents);
6958 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6959 bfd_put_32 (output_bfd, maskwords, contents + 8);
6960 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6961 contents += 16 + cinfo.maskbits / 8;
6962
6963 for (i = 0; i < bucketcount; ++i)
6964 {
6965 if (cinfo.counts[i] == 0)
6966 bfd_put_32 (output_bfd, 0, contents);
6967 else
6968 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6969 contents += 4;
6970 }
6971
6972 cinfo.contents = contents;
6973
6974 /* Renumber dynamic symbols, populate .gnu.hash section. */
6975 elf_link_hash_traverse (elf_hash_table (info),
6976 elf_renumber_gnu_hash_syms, &cinfo);
6977
6978 contents = s->contents + 16;
6979 for (i = 0; i < maskwords; ++i)
6980 {
6981 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6982 contents);
6983 contents += bed->s->arch_size / 8;
6984 }
6985
6986 free (cinfo.bitmask);
6987 free (cinfo.hashcodes);
6988 }
6989 }
6990
6991 s = bfd_get_linker_section (dynobj, ".dynstr");
6992 BFD_ASSERT (s != NULL);
6993
6994 elf_finalize_dynstr (output_bfd, info);
6995
6996 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6997
6998 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6999 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7000 return FALSE;
7001 }
7002
7003 return TRUE;
7004}
7005\f
7006/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7007
7008static void
7009merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7010 asection *sec)
7011{
7012 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7013 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7014}
7015
7016/* Finish SHF_MERGE section merging. */
7017
7018bfd_boolean
7019_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7020{
7021 bfd *ibfd;
7022 asection *sec;
7023
7024 if (!is_elf_hash_table (info->hash))
7025 return FALSE;
7026
7027 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7028 if ((ibfd->flags & DYNAMIC) == 0
7029 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7030 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7031 == get_elf_backend_data (obfd)->s->elfclass))
7032 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7033 if ((sec->flags & SEC_MERGE) != 0
7034 && !bfd_is_abs_section (sec->output_section))
7035 {
7036 struct bfd_elf_section_data *secdata;
7037
7038 secdata = elf_section_data (sec);
7039 if (! _bfd_add_merge_section (obfd,
7040 &elf_hash_table (info)->merge_info,
7041 sec, &secdata->sec_info))
7042 return FALSE;
7043 else if (secdata->sec_info)
7044 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7045 }
7046
7047 if (elf_hash_table (info)->merge_info != NULL)
7048 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7049 merge_sections_remove_hook);
7050 return TRUE;
7051}
7052
7053/* Create an entry in an ELF linker hash table. */
7054
7055struct bfd_hash_entry *
7056_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7057 struct bfd_hash_table *table,
7058 const char *string)
7059{
7060 /* Allocate the structure if it has not already been allocated by a
7061 subclass. */
7062 if (entry == NULL)
7063 {
7064 entry = (struct bfd_hash_entry *)
7065 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7066 if (entry == NULL)
7067 return entry;
7068 }
7069
7070 /* Call the allocation method of the superclass. */
7071 entry = _bfd_link_hash_newfunc (entry, table, string);
7072 if (entry != NULL)
7073 {
7074 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7075 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7076
7077 /* Set local fields. */
7078 ret->indx = -1;
7079 ret->dynindx = -1;
7080 ret->got = htab->init_got_refcount;
7081 ret->plt = htab->init_plt_refcount;
7082 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7083 - offsetof (struct elf_link_hash_entry, size)));
7084 /* Assume that we have been called by a non-ELF symbol reader.
7085 This flag is then reset by the code which reads an ELF input
7086 file. This ensures that a symbol created by a non-ELF symbol
7087 reader will have the flag set correctly. */
7088 ret->non_elf = 1;
7089 }
7090
7091 return entry;
7092}
7093
7094/* Copy data from an indirect symbol to its direct symbol, hiding the
7095 old indirect symbol. Also used for copying flags to a weakdef. */
7096
7097void
7098_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7099 struct elf_link_hash_entry *dir,
7100 struct elf_link_hash_entry *ind)
7101{
7102 struct elf_link_hash_table *htab;
7103
7104 /* Copy down any references that we may have already seen to the
7105 symbol which just became indirect. */
7106
7107 if (dir->versioned != versioned_hidden)
7108 dir->ref_dynamic |= ind->ref_dynamic;
7109 dir->ref_regular |= ind->ref_regular;
7110 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7111 dir->non_got_ref |= ind->non_got_ref;
7112 dir->needs_plt |= ind->needs_plt;
7113 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7114
7115 if (ind->root.type != bfd_link_hash_indirect)
7116 return;
7117
7118 /* Copy over the global and procedure linkage table refcount entries.
7119 These may have been already set up by a check_relocs routine. */
7120 htab = elf_hash_table (info);
7121 if (ind->got.refcount > htab->init_got_refcount.refcount)
7122 {
7123 if (dir->got.refcount < 0)
7124 dir->got.refcount = 0;
7125 dir->got.refcount += ind->got.refcount;
7126 ind->got.refcount = htab->init_got_refcount.refcount;
7127 }
7128
7129 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7130 {
7131 if (dir->plt.refcount < 0)
7132 dir->plt.refcount = 0;
7133 dir->plt.refcount += ind->plt.refcount;
7134 ind->plt.refcount = htab->init_plt_refcount.refcount;
7135 }
7136
7137 if (ind->dynindx != -1)
7138 {
7139 if (dir->dynindx != -1)
7140 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7141 dir->dynindx = ind->dynindx;
7142 dir->dynstr_index = ind->dynstr_index;
7143 ind->dynindx = -1;
7144 ind->dynstr_index = 0;
7145 }
7146}
7147
7148void
7149_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7150 struct elf_link_hash_entry *h,
7151 bfd_boolean force_local)
7152{
7153 /* STT_GNU_IFUNC symbol must go through PLT. */
7154 if (h->type != STT_GNU_IFUNC)
7155 {
7156 h->plt = elf_hash_table (info)->init_plt_offset;
7157 h->needs_plt = 0;
7158 }
7159 if (force_local)
7160 {
7161 h->forced_local = 1;
7162 if (h->dynindx != -1)
7163 {
7164 h->dynindx = -1;
7165 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7166 h->dynstr_index);
7167 }
7168 }
7169}
7170
7171/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7172 caller. */
7173
7174bfd_boolean
7175_bfd_elf_link_hash_table_init
7176 (struct elf_link_hash_table *table,
7177 bfd *abfd,
7178 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7179 struct bfd_hash_table *,
7180 const char *),
7181 unsigned int entsize,
7182 enum elf_target_id target_id)
7183{
7184 bfd_boolean ret;
7185 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7186
7187 table->init_got_refcount.refcount = can_refcount - 1;
7188 table->init_plt_refcount.refcount = can_refcount - 1;
7189 table->init_got_offset.offset = -(bfd_vma) 1;
7190 table->init_plt_offset.offset = -(bfd_vma) 1;
7191 /* The first dynamic symbol is a dummy. */
7192 table->dynsymcount = 1;
7193
7194 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7195
7196 table->root.type = bfd_link_elf_hash_table;
7197 table->hash_table_id = target_id;
7198
7199 return ret;
7200}
7201
7202/* Create an ELF linker hash table. */
7203
7204struct bfd_link_hash_table *
7205_bfd_elf_link_hash_table_create (bfd *abfd)
7206{
7207 struct elf_link_hash_table *ret;
7208 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7209
7210 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7211 if (ret == NULL)
7212 return NULL;
7213
7214 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7215 sizeof (struct elf_link_hash_entry),
7216 GENERIC_ELF_DATA))
7217 {
7218 free (ret);
7219 return NULL;
7220 }
7221 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7222
7223 return &ret->root;
7224}
7225
7226/* Destroy an ELF linker hash table. */
7227
7228void
7229_bfd_elf_link_hash_table_free (bfd *obfd)
7230{
7231 struct elf_link_hash_table *htab;
7232
7233 htab = (struct elf_link_hash_table *) obfd->link.hash;
7234 if (htab->dynstr != NULL)
7235 _bfd_elf_strtab_free (htab->dynstr);
7236 _bfd_merge_sections_free (htab->merge_info);
7237 _bfd_generic_link_hash_table_free (obfd);
7238}
7239
7240/* This is a hook for the ELF emulation code in the generic linker to
7241 tell the backend linker what file name to use for the DT_NEEDED
7242 entry for a dynamic object. */
7243
7244void
7245bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7246{
7247 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7248 && bfd_get_format (abfd) == bfd_object)
7249 elf_dt_name (abfd) = name;
7250}
7251
7252int
7253bfd_elf_get_dyn_lib_class (bfd *abfd)
7254{
7255 int lib_class;
7256 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7257 && bfd_get_format (abfd) == bfd_object)
7258 lib_class = elf_dyn_lib_class (abfd);
7259 else
7260 lib_class = 0;
7261 return lib_class;
7262}
7263
7264void
7265bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7266{
7267 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7268 && bfd_get_format (abfd) == bfd_object)
7269 elf_dyn_lib_class (abfd) = lib_class;
7270}
7271
7272/* Get the list of DT_NEEDED entries for a link. This is a hook for
7273 the linker ELF emulation code. */
7274
7275struct bfd_link_needed_list *
7276bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7277 struct bfd_link_info *info)
7278{
7279 if (! is_elf_hash_table (info->hash))
7280 return NULL;
7281 return elf_hash_table (info)->needed;
7282}
7283
7284/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7285 hook for the linker ELF emulation code. */
7286
7287struct bfd_link_needed_list *
7288bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7289 struct bfd_link_info *info)
7290{
7291 if (! is_elf_hash_table (info->hash))
7292 return NULL;
7293 return elf_hash_table (info)->runpath;
7294}
7295
7296/* Get the name actually used for a dynamic object for a link. This
7297 is the SONAME entry if there is one. Otherwise, it is the string
7298 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7299
7300const char *
7301bfd_elf_get_dt_soname (bfd *abfd)
7302{
7303 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7304 && bfd_get_format (abfd) == bfd_object)
7305 return elf_dt_name (abfd);
7306 return NULL;
7307}
7308
7309/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7310 the ELF linker emulation code. */
7311
7312bfd_boolean
7313bfd_elf_get_bfd_needed_list (bfd *abfd,
7314 struct bfd_link_needed_list **pneeded)
7315{
7316 asection *s;
7317 bfd_byte *dynbuf = NULL;
7318 unsigned int elfsec;
7319 unsigned long shlink;
7320 bfd_byte *extdyn, *extdynend;
7321 size_t extdynsize;
7322 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7323
7324 *pneeded = NULL;
7325
7326 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7327 || bfd_get_format (abfd) != bfd_object)
7328 return TRUE;
7329
7330 s = bfd_get_section_by_name (abfd, ".dynamic");
7331 if (s == NULL || s->size == 0)
7332 return TRUE;
7333
7334 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7335 goto error_return;
7336
7337 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7338 if (elfsec == SHN_BAD)
7339 goto error_return;
7340
7341 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7342
7343 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7344 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7345
7346 extdyn = dynbuf;
7347 extdynend = extdyn + s->size;
7348 for (; extdyn < extdynend; extdyn += extdynsize)
7349 {
7350 Elf_Internal_Dyn dyn;
7351
7352 (*swap_dyn_in) (abfd, extdyn, &dyn);
7353
7354 if (dyn.d_tag == DT_NULL)
7355 break;
7356
7357 if (dyn.d_tag == DT_NEEDED)
7358 {
7359 const char *string;
7360 struct bfd_link_needed_list *l;
7361 unsigned int tagv = dyn.d_un.d_val;
7362 bfd_size_type amt;
7363
7364 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7365 if (string == NULL)
7366 goto error_return;
7367
7368 amt = sizeof *l;
7369 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7370 if (l == NULL)
7371 goto error_return;
7372
7373 l->by = abfd;
7374 l->name = string;
7375 l->next = *pneeded;
7376 *pneeded = l;
7377 }
7378 }
7379
7380 free (dynbuf);
7381
7382 return TRUE;
7383
7384 error_return:
7385 if (dynbuf != NULL)
7386 free (dynbuf);
7387 return FALSE;
7388}
7389
7390struct elf_symbuf_symbol
7391{
7392 unsigned long st_name; /* Symbol name, index in string tbl */
7393 unsigned char st_info; /* Type and binding attributes */
7394 unsigned char st_other; /* Visibilty, and target specific */
7395};
7396
7397struct elf_symbuf_head
7398{
7399 struct elf_symbuf_symbol *ssym;
7400 size_t count;
7401 unsigned int st_shndx;
7402};
7403
7404struct elf_symbol
7405{
7406 union
7407 {
7408 Elf_Internal_Sym *isym;
7409 struct elf_symbuf_symbol *ssym;
7410 } u;
7411 const char *name;
7412};
7413
7414/* Sort references to symbols by ascending section number. */
7415
7416static int
7417elf_sort_elf_symbol (const void *arg1, const void *arg2)
7418{
7419 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7420 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7421
7422 return s1->st_shndx - s2->st_shndx;
7423}
7424
7425static int
7426elf_sym_name_compare (const void *arg1, const void *arg2)
7427{
7428 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7429 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7430 return strcmp (s1->name, s2->name);
7431}
7432
7433static struct elf_symbuf_head *
7434elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7435{
7436 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7437 struct elf_symbuf_symbol *ssym;
7438 struct elf_symbuf_head *ssymbuf, *ssymhead;
7439 size_t i, shndx_count, total_size;
7440
7441 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7442 if (indbuf == NULL)
7443 return NULL;
7444
7445 for (ind = indbuf, i = 0; i < symcount; i++)
7446 if (isymbuf[i].st_shndx != SHN_UNDEF)
7447 *ind++ = &isymbuf[i];
7448 indbufend = ind;
7449
7450 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7451 elf_sort_elf_symbol);
7452
7453 shndx_count = 0;
7454 if (indbufend > indbuf)
7455 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7456 if (ind[0]->st_shndx != ind[1]->st_shndx)
7457 shndx_count++;
7458
7459 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7460 + (indbufend - indbuf) * sizeof (*ssym));
7461 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7462 if (ssymbuf == NULL)
7463 {
7464 free (indbuf);
7465 return NULL;
7466 }
7467
7468 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7469 ssymbuf->ssym = NULL;
7470 ssymbuf->count = shndx_count;
7471 ssymbuf->st_shndx = 0;
7472 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7473 {
7474 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7475 {
7476 ssymhead++;
7477 ssymhead->ssym = ssym;
7478 ssymhead->count = 0;
7479 ssymhead->st_shndx = (*ind)->st_shndx;
7480 }
7481 ssym->st_name = (*ind)->st_name;
7482 ssym->st_info = (*ind)->st_info;
7483 ssym->st_other = (*ind)->st_other;
7484 ssymhead->count++;
7485 }
7486 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7487 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7488 == total_size));
7489
7490 free (indbuf);
7491 return ssymbuf;
7492}
7493
7494/* Check if 2 sections define the same set of local and global
7495 symbols. */
7496
7497static bfd_boolean
7498bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7499 struct bfd_link_info *info)
7500{
7501 bfd *bfd1, *bfd2;
7502 const struct elf_backend_data *bed1, *bed2;
7503 Elf_Internal_Shdr *hdr1, *hdr2;
7504 size_t symcount1, symcount2;
7505 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7506 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7507 Elf_Internal_Sym *isym, *isymend;
7508 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7509 size_t count1, count2, i;
7510 unsigned int shndx1, shndx2;
7511 bfd_boolean result;
7512
7513 bfd1 = sec1->owner;
7514 bfd2 = sec2->owner;
7515
7516 /* Both sections have to be in ELF. */
7517 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7518 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7519 return FALSE;
7520
7521 if (elf_section_type (sec1) != elf_section_type (sec2))
7522 return FALSE;
7523
7524 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7525 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7526 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7527 return FALSE;
7528
7529 bed1 = get_elf_backend_data (bfd1);
7530 bed2 = get_elf_backend_data (bfd2);
7531 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7532 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7533 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7534 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7535
7536 if (symcount1 == 0 || symcount2 == 0)
7537 return FALSE;
7538
7539 result = FALSE;
7540 isymbuf1 = NULL;
7541 isymbuf2 = NULL;
7542 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7543 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7544
7545 if (ssymbuf1 == NULL)
7546 {
7547 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7548 NULL, NULL, NULL);
7549 if (isymbuf1 == NULL)
7550 goto done;
7551
7552 if (!info->reduce_memory_overheads)
7553 elf_tdata (bfd1)->symbuf = ssymbuf1
7554 = elf_create_symbuf (symcount1, isymbuf1);
7555 }
7556
7557 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7558 {
7559 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7560 NULL, NULL, NULL);
7561 if (isymbuf2 == NULL)
7562 goto done;
7563
7564 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7565 elf_tdata (bfd2)->symbuf = ssymbuf2
7566 = elf_create_symbuf (symcount2, isymbuf2);
7567 }
7568
7569 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7570 {
7571 /* Optimized faster version. */
7572 size_t lo, hi, mid;
7573 struct elf_symbol *symp;
7574 struct elf_symbuf_symbol *ssym, *ssymend;
7575
7576 lo = 0;
7577 hi = ssymbuf1->count;
7578 ssymbuf1++;
7579 count1 = 0;
7580 while (lo < hi)
7581 {
7582 mid = (lo + hi) / 2;
7583 if (shndx1 < ssymbuf1[mid].st_shndx)
7584 hi = mid;
7585 else if (shndx1 > ssymbuf1[mid].st_shndx)
7586 lo = mid + 1;
7587 else
7588 {
7589 count1 = ssymbuf1[mid].count;
7590 ssymbuf1 += mid;
7591 break;
7592 }
7593 }
7594
7595 lo = 0;
7596 hi = ssymbuf2->count;
7597 ssymbuf2++;
7598 count2 = 0;
7599 while (lo < hi)
7600 {
7601 mid = (lo + hi) / 2;
7602 if (shndx2 < ssymbuf2[mid].st_shndx)
7603 hi = mid;
7604 else if (shndx2 > ssymbuf2[mid].st_shndx)
7605 lo = mid + 1;
7606 else
7607 {
7608 count2 = ssymbuf2[mid].count;
7609 ssymbuf2 += mid;
7610 break;
7611 }
7612 }
7613
7614 if (count1 == 0 || count2 == 0 || count1 != count2)
7615 goto done;
7616
7617 symtable1
7618 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7619 symtable2
7620 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7621 if (symtable1 == NULL || symtable2 == NULL)
7622 goto done;
7623
7624 symp = symtable1;
7625 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7626 ssym < ssymend; ssym++, symp++)
7627 {
7628 symp->u.ssym = ssym;
7629 symp->name = bfd_elf_string_from_elf_section (bfd1,
7630 hdr1->sh_link,
7631 ssym->st_name);
7632 }
7633
7634 symp = symtable2;
7635 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7636 ssym < ssymend; ssym++, symp++)
7637 {
7638 symp->u.ssym = ssym;
7639 symp->name = bfd_elf_string_from_elf_section (bfd2,
7640 hdr2->sh_link,
7641 ssym->st_name);
7642 }
7643
7644 /* Sort symbol by name. */
7645 qsort (symtable1, count1, sizeof (struct elf_symbol),
7646 elf_sym_name_compare);
7647 qsort (symtable2, count1, sizeof (struct elf_symbol),
7648 elf_sym_name_compare);
7649
7650 for (i = 0; i < count1; i++)
7651 /* Two symbols must have the same binding, type and name. */
7652 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7653 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7654 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7655 goto done;
7656
7657 result = TRUE;
7658 goto done;
7659 }
7660
7661 symtable1 = (struct elf_symbol *)
7662 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7663 symtable2 = (struct elf_symbol *)
7664 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7665 if (symtable1 == NULL || symtable2 == NULL)
7666 goto done;
7667
7668 /* Count definitions in the section. */
7669 count1 = 0;
7670 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7671 if (isym->st_shndx == shndx1)
7672 symtable1[count1++].u.isym = isym;
7673
7674 count2 = 0;
7675 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7676 if (isym->st_shndx == shndx2)
7677 symtable2[count2++].u.isym = isym;
7678
7679 if (count1 == 0 || count2 == 0 || count1 != count2)
7680 goto done;
7681
7682 for (i = 0; i < count1; i++)
7683 symtable1[i].name
7684 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7685 symtable1[i].u.isym->st_name);
7686
7687 for (i = 0; i < count2; i++)
7688 symtable2[i].name
7689 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7690 symtable2[i].u.isym->st_name);
7691
7692 /* Sort symbol by name. */
7693 qsort (symtable1, count1, sizeof (struct elf_symbol),
7694 elf_sym_name_compare);
7695 qsort (symtable2, count1, sizeof (struct elf_symbol),
7696 elf_sym_name_compare);
7697
7698 for (i = 0; i < count1; i++)
7699 /* Two symbols must have the same binding, type and name. */
7700 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7701 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7702 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7703 goto done;
7704
7705 result = TRUE;
7706
7707done:
7708 if (symtable1)
7709 free (symtable1);
7710 if (symtable2)
7711 free (symtable2);
7712 if (isymbuf1)
7713 free (isymbuf1);
7714 if (isymbuf2)
7715 free (isymbuf2);
7716
7717 return result;
7718}
7719
7720/* Return TRUE if 2 section types are compatible. */
7721
7722bfd_boolean
7723_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7724 bfd *bbfd, const asection *bsec)
7725{
7726 if (asec == NULL
7727 || bsec == NULL
7728 || abfd->xvec->flavour != bfd_target_elf_flavour
7729 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7730 return TRUE;
7731
7732 return elf_section_type (asec) == elf_section_type (bsec);
7733}
7734\f
7735/* Final phase of ELF linker. */
7736
7737/* A structure we use to avoid passing large numbers of arguments. */
7738
7739struct elf_final_link_info
7740{
7741 /* General link information. */
7742 struct bfd_link_info *info;
7743 /* Output BFD. */
7744 bfd *output_bfd;
7745 /* Symbol string table. */
7746 struct elf_strtab_hash *symstrtab;
7747 /* .hash section. */
7748 asection *hash_sec;
7749 /* symbol version section (.gnu.version). */
7750 asection *symver_sec;
7751 /* Buffer large enough to hold contents of any section. */
7752 bfd_byte *contents;
7753 /* Buffer large enough to hold external relocs of any section. */
7754 void *external_relocs;
7755 /* Buffer large enough to hold internal relocs of any section. */
7756 Elf_Internal_Rela *internal_relocs;
7757 /* Buffer large enough to hold external local symbols of any input
7758 BFD. */
7759 bfd_byte *external_syms;
7760 /* And a buffer for symbol section indices. */
7761 Elf_External_Sym_Shndx *locsym_shndx;
7762 /* Buffer large enough to hold internal local symbols of any input
7763 BFD. */
7764 Elf_Internal_Sym *internal_syms;
7765 /* Array large enough to hold a symbol index for each local symbol
7766 of any input BFD. */
7767 long *indices;
7768 /* Array large enough to hold a section pointer for each local
7769 symbol of any input BFD. */
7770 asection **sections;
7771 /* Buffer for SHT_SYMTAB_SHNDX section. */
7772 Elf_External_Sym_Shndx *symshndxbuf;
7773 /* Number of STT_FILE syms seen. */
7774 size_t filesym_count;
7775};
7776
7777/* This struct is used to pass information to elf_link_output_extsym. */
7778
7779struct elf_outext_info
7780{
7781 bfd_boolean failed;
7782 bfd_boolean localsyms;
7783 bfd_boolean file_sym_done;
7784 struct elf_final_link_info *flinfo;
7785};
7786
7787
7788/* Support for evaluating a complex relocation.
7789
7790 Complex relocations are generalized, self-describing relocations. The
7791 implementation of them consists of two parts: complex symbols, and the
7792 relocations themselves.
7793
7794 The relocations are use a reserved elf-wide relocation type code (R_RELC
7795 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7796 information (start bit, end bit, word width, etc) into the addend. This
7797 information is extracted from CGEN-generated operand tables within gas.
7798
7799 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7800 internal) representing prefix-notation expressions, including but not
7801 limited to those sorts of expressions normally encoded as addends in the
7802 addend field. The symbol mangling format is:
7803
7804 <node> := <literal>
7805 | <unary-operator> ':' <node>
7806 | <binary-operator> ':' <node> ':' <node>
7807 ;
7808
7809 <literal> := 's' <digits=N> ':' <N character symbol name>
7810 | 'S' <digits=N> ':' <N character section name>
7811 | '#' <hexdigits>
7812 ;
7813
7814 <binary-operator> := as in C
7815 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7816
7817static void
7818set_symbol_value (bfd *bfd_with_globals,
7819 Elf_Internal_Sym *isymbuf,
7820 size_t locsymcount,
7821 size_t symidx,
7822 bfd_vma val)
7823{
7824 struct elf_link_hash_entry **sym_hashes;
7825 struct elf_link_hash_entry *h;
7826 size_t extsymoff = locsymcount;
7827
7828 if (symidx < locsymcount)
7829 {
7830 Elf_Internal_Sym *sym;
7831
7832 sym = isymbuf + symidx;
7833 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7834 {
7835 /* It is a local symbol: move it to the
7836 "absolute" section and give it a value. */
7837 sym->st_shndx = SHN_ABS;
7838 sym->st_value = val;
7839 return;
7840 }
7841 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7842 extsymoff = 0;
7843 }
7844
7845 /* It is a global symbol: set its link type
7846 to "defined" and give it a value. */
7847
7848 sym_hashes = elf_sym_hashes (bfd_with_globals);
7849 h = sym_hashes [symidx - extsymoff];
7850 while (h->root.type == bfd_link_hash_indirect
7851 || h->root.type == bfd_link_hash_warning)
7852 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7853 h->root.type = bfd_link_hash_defined;
7854 h->root.u.def.value = val;
7855 h->root.u.def.section = bfd_abs_section_ptr;
7856}
7857
7858static bfd_boolean
7859resolve_symbol (const char *name,
7860 bfd *input_bfd,
7861 struct elf_final_link_info *flinfo,
7862 bfd_vma *result,
7863 Elf_Internal_Sym *isymbuf,
7864 size_t locsymcount)
7865{
7866 Elf_Internal_Sym *sym;
7867 struct bfd_link_hash_entry *global_entry;
7868 const char *candidate = NULL;
7869 Elf_Internal_Shdr *symtab_hdr;
7870 size_t i;
7871
7872 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7873
7874 for (i = 0; i < locsymcount; ++ i)
7875 {
7876 sym = isymbuf + i;
7877
7878 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7879 continue;
7880
7881 candidate = bfd_elf_string_from_elf_section (input_bfd,
7882 symtab_hdr->sh_link,
7883 sym->st_name);
7884#ifdef DEBUG
7885 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7886 name, candidate, (unsigned long) sym->st_value);
7887#endif
7888 if (candidate && strcmp (candidate, name) == 0)
7889 {
7890 asection *sec = flinfo->sections [i];
7891
7892 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7893 *result += sec->output_offset + sec->output_section->vma;
7894#ifdef DEBUG
7895 printf ("Found symbol with value %8.8lx\n",
7896 (unsigned long) *result);
7897#endif
7898 return TRUE;
7899 }
7900 }
7901
7902 /* Hmm, haven't found it yet. perhaps it is a global. */
7903 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7904 FALSE, FALSE, TRUE);
7905 if (!global_entry)
7906 return FALSE;
7907
7908 if (global_entry->type == bfd_link_hash_defined
7909 || global_entry->type == bfd_link_hash_defweak)
7910 {
7911 *result = (global_entry->u.def.value
7912 + global_entry->u.def.section->output_section->vma
7913 + global_entry->u.def.section->output_offset);
7914#ifdef DEBUG
7915 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7916 global_entry->root.string, (unsigned long) *result);
7917#endif
7918 return TRUE;
7919 }
7920
7921 return FALSE;
7922}
7923
7924/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7925 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7926 names like "foo.end" which is the end address of section "foo". */
7927
7928static bfd_boolean
7929resolve_section (const char *name,
7930 asection *sections,
7931 bfd_vma *result,
7932 bfd * abfd)
7933{
7934 asection *curr;
7935 unsigned int len;
7936
7937 for (curr = sections; curr; curr = curr->next)
7938 if (strcmp (curr->name, name) == 0)
7939 {
7940 *result = curr->vma;
7941 return TRUE;
7942 }
7943
7944 /* Hmm. still haven't found it. try pseudo-section names. */
7945 /* FIXME: This could be coded more efficiently... */
7946 for (curr = sections; curr; curr = curr->next)
7947 {
7948 len = strlen (curr->name);
7949 if (len > strlen (name))
7950 continue;
7951
7952 if (strncmp (curr->name, name, len) == 0)
7953 {
7954 if (strncmp (".end", name + len, 4) == 0)
7955 {
7956 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7957 return TRUE;
7958 }
7959
7960 /* Insert more pseudo-section names here, if you like. */
7961 }
7962 }
7963
7964 return FALSE;
7965}
7966
7967static void
7968undefined_reference (const char *reftype, const char *name)
7969{
7970 /* xgettext:c-format */
7971 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7972 reftype, name);
7973}
7974
7975static bfd_boolean
7976eval_symbol (bfd_vma *result,
7977 const char **symp,
7978 bfd *input_bfd,
7979 struct elf_final_link_info *flinfo,
7980 bfd_vma dot,
7981 Elf_Internal_Sym *isymbuf,
7982 size_t locsymcount,
7983 int signed_p)
7984{
7985 size_t len;
7986 size_t symlen;
7987 bfd_vma a;
7988 bfd_vma b;
7989 char symbuf[4096];
7990 const char *sym = *symp;
7991 const char *symend;
7992 bfd_boolean symbol_is_section = FALSE;
7993
7994 len = strlen (sym);
7995 symend = sym + len;
7996
7997 if (len < 1 || len > sizeof (symbuf))
7998 {
7999 bfd_set_error (bfd_error_invalid_operation);
8000 return FALSE;
8001 }
8002
8003 switch (* sym)
8004 {
8005 case '.':
8006 *result = dot;
8007 *symp = sym + 1;
8008 return TRUE;
8009
8010 case '#':
8011 ++sym;
8012 *result = strtoul (sym, (char **) symp, 16);
8013 return TRUE;
8014
8015 case 'S':
8016 symbol_is_section = TRUE;
8017 /* Fall through. */
8018 case 's':
8019 ++sym;
8020 symlen = strtol (sym, (char **) symp, 10);
8021 sym = *symp + 1; /* Skip the trailing ':'. */
8022
8023 if (symend < sym || symlen + 1 > sizeof (symbuf))
8024 {
8025 bfd_set_error (bfd_error_invalid_operation);
8026 return FALSE;
8027 }
8028
8029 memcpy (symbuf, sym, symlen);
8030 symbuf[symlen] = '\0';
8031 *symp = sym + symlen;
8032
8033 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8034 the symbol as a section, or vice-versa. so we're pretty liberal in our
8035 interpretation here; section means "try section first", not "must be a
8036 section", and likewise with symbol. */
8037
8038 if (symbol_is_section)
8039 {
8040 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8041 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8042 isymbuf, locsymcount))
8043 {
8044 undefined_reference ("section", symbuf);
8045 return FALSE;
8046 }
8047 }
8048 else
8049 {
8050 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8051 isymbuf, locsymcount)
8052 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8053 result, input_bfd))
8054 {
8055 undefined_reference ("symbol", symbuf);
8056 return FALSE;
8057 }
8058 }
8059
8060 return TRUE;
8061
8062 /* All that remains are operators. */
8063
8064#define UNARY_OP(op) \
8065 if (strncmp (sym, #op, strlen (#op)) == 0) \
8066 { \
8067 sym += strlen (#op); \
8068 if (*sym == ':') \
8069 ++sym; \
8070 *symp = sym; \
8071 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8072 isymbuf, locsymcount, signed_p)) \
8073 return FALSE; \
8074 if (signed_p) \
8075 *result = op ((bfd_signed_vma) a); \
8076 else \
8077 *result = op a; \
8078 return TRUE; \
8079 }
8080
8081#define BINARY_OP(op) \
8082 if (strncmp (sym, #op, strlen (#op)) == 0) \
8083 { \
8084 sym += strlen (#op); \
8085 if (*sym == ':') \
8086 ++sym; \
8087 *symp = sym; \
8088 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8089 isymbuf, locsymcount, signed_p)) \
8090 return FALSE; \
8091 ++*symp; \
8092 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8093 isymbuf, locsymcount, signed_p)) \
8094 return FALSE; \
8095 if (signed_p) \
8096 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8097 else \
8098 *result = a op b; \
8099 return TRUE; \
8100 }
8101
8102 default:
8103 UNARY_OP (0-);
8104 BINARY_OP (<<);
8105 BINARY_OP (>>);
8106 BINARY_OP (==);
8107 BINARY_OP (!=);
8108 BINARY_OP (<=);
8109 BINARY_OP (>=);
8110 BINARY_OP (&&);
8111 BINARY_OP (||);
8112 UNARY_OP (~);
8113 UNARY_OP (!);
8114 BINARY_OP (*);
8115 BINARY_OP (/);
8116 BINARY_OP (%);
8117 BINARY_OP (^);
8118 BINARY_OP (|);
8119 BINARY_OP (&);
8120 BINARY_OP (+);
8121 BINARY_OP (-);
8122 BINARY_OP (<);
8123 BINARY_OP (>);
8124#undef UNARY_OP
8125#undef BINARY_OP
8126 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8127 bfd_set_error (bfd_error_invalid_operation);
8128 return FALSE;
8129 }
8130}
8131
8132static void
8133put_value (bfd_vma size,
8134 unsigned long chunksz,
8135 bfd *input_bfd,
8136 bfd_vma x,
8137 bfd_byte *location)
8138{
8139 location += (size - chunksz);
8140
8141 for (; size; size -= chunksz, location -= chunksz)
8142 {
8143 switch (chunksz)
8144 {
8145 case 1:
8146 bfd_put_8 (input_bfd, x, location);
8147 x >>= 8;
8148 break;
8149 case 2:
8150 bfd_put_16 (input_bfd, x, location);
8151 x >>= 16;
8152 break;
8153 case 4:
8154 bfd_put_32 (input_bfd, x, location);
8155 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8156 x >>= 16;
8157 x >>= 16;
8158 break;
8159#ifdef BFD64
8160 case 8:
8161 bfd_put_64 (input_bfd, x, location);
8162 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8163 x >>= 32;
8164 x >>= 32;
8165 break;
8166#endif
8167 default:
8168 abort ();
8169 break;
8170 }
8171 }
8172}
8173
8174static bfd_vma
8175get_value (bfd_vma size,
8176 unsigned long chunksz,
8177 bfd *input_bfd,
8178 bfd_byte *location)
8179{
8180 int shift;
8181 bfd_vma x = 0;
8182
8183 /* Sanity checks. */
8184 BFD_ASSERT (chunksz <= sizeof (x)
8185 && size >= chunksz
8186 && chunksz != 0
8187 && (size % chunksz) == 0
8188 && input_bfd != NULL
8189 && location != NULL);
8190
8191 if (chunksz == sizeof (x))
8192 {
8193 BFD_ASSERT (size == chunksz);
8194
8195 /* Make sure that we do not perform an undefined shift operation.
8196 We know that size == chunksz so there will only be one iteration
8197 of the loop below. */
8198 shift = 0;
8199 }
8200 else
8201 shift = 8 * chunksz;
8202
8203 for (; size; size -= chunksz, location += chunksz)
8204 {
8205 switch (chunksz)
8206 {
8207 case 1:
8208 x = (x << shift) | bfd_get_8 (input_bfd, location);
8209 break;
8210 case 2:
8211 x = (x << shift) | bfd_get_16 (input_bfd, location);
8212 break;
8213 case 4:
8214 x = (x << shift) | bfd_get_32 (input_bfd, location);
8215 break;
8216#ifdef BFD64
8217 case 8:
8218 x = (x << shift) | bfd_get_64 (input_bfd, location);
8219 break;
8220#endif
8221 default:
8222 abort ();
8223 }
8224 }
8225 return x;
8226}
8227
8228static void
8229decode_complex_addend (unsigned long *start, /* in bits */
8230 unsigned long *oplen, /* in bits */
8231 unsigned long *len, /* in bits */
8232 unsigned long *wordsz, /* in bytes */
8233 unsigned long *chunksz, /* in bytes */
8234 unsigned long *lsb0_p,
8235 unsigned long *signed_p,
8236 unsigned long *trunc_p,
8237 unsigned long encoded)
8238{
8239 * start = encoded & 0x3F;
8240 * len = (encoded >> 6) & 0x3F;
8241 * oplen = (encoded >> 12) & 0x3F;
8242 * wordsz = (encoded >> 18) & 0xF;
8243 * chunksz = (encoded >> 22) & 0xF;
8244 * lsb0_p = (encoded >> 27) & 1;
8245 * signed_p = (encoded >> 28) & 1;
8246 * trunc_p = (encoded >> 29) & 1;
8247}
8248
8249bfd_reloc_status_type
8250bfd_elf_perform_complex_relocation (bfd *input_bfd,
8251 asection *input_section ATTRIBUTE_UNUSED,
8252 bfd_byte *contents,
8253 Elf_Internal_Rela *rel,
8254 bfd_vma relocation)
8255{
8256 bfd_vma shift, x, mask;
8257 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8258 bfd_reloc_status_type r;
8259
8260 /* Perform this reloc, since it is complex.
8261 (this is not to say that it necessarily refers to a complex
8262 symbol; merely that it is a self-describing CGEN based reloc.
8263 i.e. the addend has the complete reloc information (bit start, end,
8264 word size, etc) encoded within it.). */
8265
8266 decode_complex_addend (&start, &oplen, &len, &wordsz,
8267 &chunksz, &lsb0_p, &signed_p,
8268 &trunc_p, rel->r_addend);
8269
8270 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8271
8272 if (lsb0_p)
8273 shift = (start + 1) - len;
8274 else
8275 shift = (8 * wordsz) - (start + len);
8276
8277 x = get_value (wordsz, chunksz, input_bfd,
8278 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8279
8280#ifdef DEBUG
8281 printf ("Doing complex reloc: "
8282 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8283 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8284 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8285 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8286 oplen, (unsigned long) x, (unsigned long) mask,
8287 (unsigned long) relocation);
8288#endif
8289
8290 r = bfd_reloc_ok;
8291 if (! trunc_p)
8292 /* Now do an overflow check. */
8293 r = bfd_check_overflow ((signed_p
8294 ? complain_overflow_signed
8295 : complain_overflow_unsigned),
8296 len, 0, (8 * wordsz),
8297 relocation);
8298
8299 /* Do the deed. */
8300 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8301
8302#ifdef DEBUG
8303 printf (" relocation: %8.8lx\n"
8304 " shifted mask: %8.8lx\n"
8305 " shifted/masked reloc: %8.8lx\n"
8306 " result: %8.8lx\n",
8307 (unsigned long) relocation, (unsigned long) (mask << shift),
8308 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8309#endif
8310 put_value (wordsz, chunksz, input_bfd, x,
8311 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8312 return r;
8313}
8314
8315/* Functions to read r_offset from external (target order) reloc
8316 entry. Faster than bfd_getl32 et al, because we let the compiler
8317 know the value is aligned. */
8318
8319static bfd_vma
8320ext32l_r_offset (const void *p)
8321{
8322 union aligned32
8323 {
8324 uint32_t v;
8325 unsigned char c[4];
8326 };
8327 const union aligned32 *a
8328 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8329
8330 uint32_t aval = ( (uint32_t) a->c[0]
8331 | (uint32_t) a->c[1] << 8
8332 | (uint32_t) a->c[2] << 16
8333 | (uint32_t) a->c[3] << 24);
8334 return aval;
8335}
8336
8337static bfd_vma
8338ext32b_r_offset (const void *p)
8339{
8340 union aligned32
8341 {
8342 uint32_t v;
8343 unsigned char c[4];
8344 };
8345 const union aligned32 *a
8346 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8347
8348 uint32_t aval = ( (uint32_t) a->c[0] << 24
8349 | (uint32_t) a->c[1] << 16
8350 | (uint32_t) a->c[2] << 8
8351 | (uint32_t) a->c[3]);
8352 return aval;
8353}
8354
8355#ifdef BFD_HOST_64_BIT
8356static bfd_vma
8357ext64l_r_offset (const void *p)
8358{
8359 union aligned64
8360 {
8361 uint64_t v;
8362 unsigned char c[8];
8363 };
8364 const union aligned64 *a
8365 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8366
8367 uint64_t aval = ( (uint64_t) a->c[0]
8368 | (uint64_t) a->c[1] << 8
8369 | (uint64_t) a->c[2] << 16
8370 | (uint64_t) a->c[3] << 24
8371 | (uint64_t) a->c[4] << 32
8372 | (uint64_t) a->c[5] << 40
8373 | (uint64_t) a->c[6] << 48
8374 | (uint64_t) a->c[7] << 56);
8375 return aval;
8376}
8377
8378static bfd_vma
8379ext64b_r_offset (const void *p)
8380{
8381 union aligned64
8382 {
8383 uint64_t v;
8384 unsigned char c[8];
8385 };
8386 const union aligned64 *a
8387 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8388
8389 uint64_t aval = ( (uint64_t) a->c[0] << 56
8390 | (uint64_t) a->c[1] << 48
8391 | (uint64_t) a->c[2] << 40
8392 | (uint64_t) a->c[3] << 32
8393 | (uint64_t) a->c[4] << 24
8394 | (uint64_t) a->c[5] << 16
8395 | (uint64_t) a->c[6] << 8
8396 | (uint64_t) a->c[7]);
8397 return aval;
8398}
8399#endif
8400
8401/* When performing a relocatable link, the input relocations are
8402 preserved. But, if they reference global symbols, the indices
8403 referenced must be updated. Update all the relocations found in
8404 RELDATA. */
8405
8406static bfd_boolean
8407elf_link_adjust_relocs (bfd *abfd,
8408 asection *sec,
8409 struct bfd_elf_section_reloc_data *reldata,
8410 bfd_boolean sort)
8411{
8412 unsigned int i;
8413 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8414 bfd_byte *erela;
8415 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8416 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8417 bfd_vma r_type_mask;
8418 int r_sym_shift;
8419 unsigned int count = reldata->count;
8420 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8421
8422 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8423 {
8424 swap_in = bed->s->swap_reloc_in;
8425 swap_out = bed->s->swap_reloc_out;
8426 }
8427 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8428 {
8429 swap_in = bed->s->swap_reloca_in;
8430 swap_out = bed->s->swap_reloca_out;
8431 }
8432 else
8433 abort ();
8434
8435 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8436 abort ();
8437
8438 if (bed->s->arch_size == 32)
8439 {
8440 r_type_mask = 0xff;
8441 r_sym_shift = 8;
8442 }
8443 else
8444 {
8445 r_type_mask = 0xffffffff;
8446 r_sym_shift = 32;
8447 }
8448
8449 erela = reldata->hdr->contents;
8450 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8451 {
8452 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8453 unsigned int j;
8454
8455 if (*rel_hash == NULL)
8456 continue;
8457
8458 BFD_ASSERT ((*rel_hash)->indx >= 0);
8459
8460 (*swap_in) (abfd, erela, irela);
8461 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8462 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8463 | (irela[j].r_info & r_type_mask));
8464 (*swap_out) (abfd, irela, erela);
8465 }
8466
8467 if (bed->elf_backend_update_relocs)
8468 (*bed->elf_backend_update_relocs) (sec, reldata);
8469
8470 if (sort && count != 0)
8471 {
8472 bfd_vma (*ext_r_off) (const void *);
8473 bfd_vma r_off;
8474 size_t elt_size;
8475 bfd_byte *base, *end, *p, *loc;
8476 bfd_byte *buf = NULL;
8477
8478 if (bed->s->arch_size == 32)
8479 {
8480 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8481 ext_r_off = ext32l_r_offset;
8482 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8483 ext_r_off = ext32b_r_offset;
8484 else
8485 abort ();
8486 }
8487 else
8488 {
8489#ifdef BFD_HOST_64_BIT
8490 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8491 ext_r_off = ext64l_r_offset;
8492 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8493 ext_r_off = ext64b_r_offset;
8494 else
8495#endif
8496 abort ();
8497 }
8498
8499 /* Must use a stable sort here. A modified insertion sort,
8500 since the relocs are mostly sorted already. */
8501 elt_size = reldata->hdr->sh_entsize;
8502 base = reldata->hdr->contents;
8503 end = base + count * elt_size;
8504 if (elt_size > sizeof (Elf64_External_Rela))
8505 abort ();
8506
8507 /* Ensure the first element is lowest. This acts as a sentinel,
8508 speeding the main loop below. */
8509 r_off = (*ext_r_off) (base);
8510 for (p = loc = base; (p += elt_size) < end; )
8511 {
8512 bfd_vma r_off2 = (*ext_r_off) (p);
8513 if (r_off > r_off2)
8514 {
8515 r_off = r_off2;
8516 loc = p;
8517 }
8518 }
8519 if (loc != base)
8520 {
8521 /* Don't just swap *base and *loc as that changes the order
8522 of the original base[0] and base[1] if they happen to
8523 have the same r_offset. */
8524 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8525 memcpy (onebuf, loc, elt_size);
8526 memmove (base + elt_size, base, loc - base);
8527 memcpy (base, onebuf, elt_size);
8528 }
8529
8530 for (p = base + elt_size; (p += elt_size) < end; )
8531 {
8532 /* base to p is sorted, *p is next to insert. */
8533 r_off = (*ext_r_off) (p);
8534 /* Search the sorted region for location to insert. */
8535 loc = p - elt_size;
8536 while (r_off < (*ext_r_off) (loc))
8537 loc -= elt_size;
8538 loc += elt_size;
8539 if (loc != p)
8540 {
8541 /* Chances are there is a run of relocs to insert here,
8542 from one of more input files. Files are not always
8543 linked in order due to the way elf_link_input_bfd is
8544 called. See pr17666. */
8545 size_t sortlen = p - loc;
8546 bfd_vma r_off2 = (*ext_r_off) (loc);
8547 size_t runlen = elt_size;
8548 size_t buf_size = 96 * 1024;
8549 while (p + runlen < end
8550 && (sortlen <= buf_size
8551 || runlen + elt_size <= buf_size)
8552 && r_off2 > (*ext_r_off) (p + runlen))
8553 runlen += elt_size;
8554 if (buf == NULL)
8555 {
8556 buf = bfd_malloc (buf_size);
8557 if (buf == NULL)
8558 return FALSE;
8559 }
8560 if (runlen < sortlen)
8561 {
8562 memcpy (buf, p, runlen);
8563 memmove (loc + runlen, loc, sortlen);
8564 memcpy (loc, buf, runlen);
8565 }
8566 else
8567 {
8568 memcpy (buf, loc, sortlen);
8569 memmove (loc, p, runlen);
8570 memcpy (loc + runlen, buf, sortlen);
8571 }
8572 p += runlen - elt_size;
8573 }
8574 }
8575 /* Hashes are no longer valid. */
8576 free (reldata->hashes);
8577 reldata->hashes = NULL;
8578 free (buf);
8579 }
8580 return TRUE;
8581}
8582
8583struct elf_link_sort_rela
8584{
8585 union {
8586 bfd_vma offset;
8587 bfd_vma sym_mask;
8588 } u;
8589 enum elf_reloc_type_class type;
8590 /* We use this as an array of size int_rels_per_ext_rel. */
8591 Elf_Internal_Rela rela[1];
8592};
8593
8594static int
8595elf_link_sort_cmp1 (const void *A, const void *B)
8596{
8597 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8598 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8599 int relativea, relativeb;
8600
8601 relativea = a->type == reloc_class_relative;
8602 relativeb = b->type == reloc_class_relative;
8603
8604 if (relativea < relativeb)
8605 return 1;
8606 if (relativea > relativeb)
8607 return -1;
8608 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8609 return -1;
8610 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8611 return 1;
8612 if (a->rela->r_offset < b->rela->r_offset)
8613 return -1;
8614 if (a->rela->r_offset > b->rela->r_offset)
8615 return 1;
8616 return 0;
8617}
8618
8619static int
8620elf_link_sort_cmp2 (const void *A, const void *B)
8621{
8622 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8623 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8624
8625 if (a->type < b->type)
8626 return -1;
8627 if (a->type > b->type)
8628 return 1;
8629 if (a->u.offset < b->u.offset)
8630 return -1;
8631 if (a->u.offset > b->u.offset)
8632 return 1;
8633 if (a->rela->r_offset < b->rela->r_offset)
8634 return -1;
8635 if (a->rela->r_offset > b->rela->r_offset)
8636 return 1;
8637 return 0;
8638}
8639
8640static size_t
8641elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8642{
8643 asection *dynamic_relocs;
8644 asection *rela_dyn;
8645 asection *rel_dyn;
8646 bfd_size_type count, size;
8647 size_t i, ret, sort_elt, ext_size;
8648 bfd_byte *sort, *s_non_relative, *p;
8649 struct elf_link_sort_rela *sq;
8650 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8651 int i2e = bed->s->int_rels_per_ext_rel;
8652 unsigned int opb = bfd_octets_per_byte (abfd);
8653 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8654 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8655 struct bfd_link_order *lo;
8656 bfd_vma r_sym_mask;
8657 bfd_boolean use_rela;
8658
8659 /* Find a dynamic reloc section. */
8660 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8661 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8662 if (rela_dyn != NULL && rela_dyn->size > 0
8663 && rel_dyn != NULL && rel_dyn->size > 0)
8664 {
8665 bfd_boolean use_rela_initialised = FALSE;
8666
8667 /* This is just here to stop gcc from complaining.
8668 Its initialization checking code is not perfect. */
8669 use_rela = TRUE;
8670
8671 /* Both sections are present. Examine the sizes
8672 of the indirect sections to help us choose. */
8673 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8674 if (lo->type == bfd_indirect_link_order)
8675 {
8676 asection *o = lo->u.indirect.section;
8677
8678 if ((o->size % bed->s->sizeof_rela) == 0)
8679 {
8680 if ((o->size % bed->s->sizeof_rel) == 0)
8681 /* Section size is divisible by both rel and rela sizes.
8682 It is of no help to us. */
8683 ;
8684 else
8685 {
8686 /* Section size is only divisible by rela. */
8687 if (use_rela_initialised && (use_rela == FALSE))
8688 {
8689 _bfd_error_handler (_("%B: Unable to sort relocs - "
8690 "they are in more than one size"),
8691 abfd);
8692 bfd_set_error (bfd_error_invalid_operation);
8693 return 0;
8694 }
8695 else
8696 {
8697 use_rela = TRUE;
8698 use_rela_initialised = TRUE;
8699 }
8700 }
8701 }
8702 else if ((o->size % bed->s->sizeof_rel) == 0)
8703 {
8704 /* Section size is only divisible by rel. */
8705 if (use_rela_initialised && (use_rela == TRUE))
8706 {
8707 _bfd_error_handler (_("%B: Unable to sort relocs - "
8708 "they are in more than one size"),
8709 abfd);
8710 bfd_set_error (bfd_error_invalid_operation);
8711 return 0;
8712 }
8713 else
8714 {
8715 use_rela = FALSE;
8716 use_rela_initialised = TRUE;
8717 }
8718 }
8719 else
8720 {
8721 /* The section size is not divisible by either -
8722 something is wrong. */
8723 _bfd_error_handler (_("%B: Unable to sort relocs - "
8724 "they are of an unknown size"), abfd);
8725 bfd_set_error (bfd_error_invalid_operation);
8726 return 0;
8727 }
8728 }
8729
8730 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8731 if (lo->type == bfd_indirect_link_order)
8732 {
8733 asection *o = lo->u.indirect.section;
8734
8735 if ((o->size % bed->s->sizeof_rela) == 0)
8736 {
8737 if ((o->size % bed->s->sizeof_rel) == 0)
8738 /* Section size is divisible by both rel and rela sizes.
8739 It is of no help to us. */
8740 ;
8741 else
8742 {
8743 /* Section size is only divisible by rela. */
8744 if (use_rela_initialised && (use_rela == FALSE))
8745 {
8746 _bfd_error_handler (_("%B: Unable to sort relocs - "
8747 "they are in more than one size"),
8748 abfd);
8749 bfd_set_error (bfd_error_invalid_operation);
8750 return 0;
8751 }
8752 else
8753 {
8754 use_rela = TRUE;
8755 use_rela_initialised = TRUE;
8756 }
8757 }
8758 }
8759 else if ((o->size % bed->s->sizeof_rel) == 0)
8760 {
8761 /* Section size is only divisible by rel. */
8762 if (use_rela_initialised && (use_rela == TRUE))
8763 {
8764 _bfd_error_handler (_("%B: Unable to sort relocs - "
8765 "they are in more than one size"),
8766 abfd);
8767 bfd_set_error (bfd_error_invalid_operation);
8768 return 0;
8769 }
8770 else
8771 {
8772 use_rela = FALSE;
8773 use_rela_initialised = TRUE;
8774 }
8775 }
8776 else
8777 {
8778 /* The section size is not divisible by either -
8779 something is wrong. */
8780 _bfd_error_handler (_("%B: Unable to sort relocs - "
8781 "they are of an unknown size"), abfd);
8782 bfd_set_error (bfd_error_invalid_operation);
8783 return 0;
8784 }
8785 }
8786
8787 if (! use_rela_initialised)
8788 /* Make a guess. */
8789 use_rela = TRUE;
8790 }
8791 else if (rela_dyn != NULL && rela_dyn->size > 0)
8792 use_rela = TRUE;
8793 else if (rel_dyn != NULL && rel_dyn->size > 0)
8794 use_rela = FALSE;
8795 else
8796 return 0;
8797
8798 if (use_rela)
8799 {
8800 dynamic_relocs = rela_dyn;
8801 ext_size = bed->s->sizeof_rela;
8802 swap_in = bed->s->swap_reloca_in;
8803 swap_out = bed->s->swap_reloca_out;
8804 }
8805 else
8806 {
8807 dynamic_relocs = rel_dyn;
8808 ext_size = bed->s->sizeof_rel;
8809 swap_in = bed->s->swap_reloc_in;
8810 swap_out = bed->s->swap_reloc_out;
8811 }
8812
8813 size = 0;
8814 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8815 if (lo->type == bfd_indirect_link_order)
8816 size += lo->u.indirect.section->size;
8817
8818 if (size != dynamic_relocs->size)
8819 return 0;
8820
8821 sort_elt = (sizeof (struct elf_link_sort_rela)
8822 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8823
8824 count = dynamic_relocs->size / ext_size;
8825 if (count == 0)
8826 return 0;
8827 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8828
8829 if (sort == NULL)
8830 {
8831 (*info->callbacks->warning)
8832 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8833 return 0;
8834 }
8835
8836 if (bed->s->arch_size == 32)
8837 r_sym_mask = ~(bfd_vma) 0xff;
8838 else
8839 r_sym_mask = ~(bfd_vma) 0xffffffff;
8840
8841 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8842 if (lo->type == bfd_indirect_link_order)
8843 {
8844 bfd_byte *erel, *erelend;
8845 asection *o = lo->u.indirect.section;
8846
8847 if (o->contents == NULL && o->size != 0)
8848 {
8849 /* This is a reloc section that is being handled as a normal
8850 section. See bfd_section_from_shdr. We can't combine
8851 relocs in this case. */
8852 free (sort);
8853 return 0;
8854 }
8855 erel = o->contents;
8856 erelend = o->contents + o->size;
8857 p = sort + o->output_offset * opb / ext_size * sort_elt;
8858
8859 while (erel < erelend)
8860 {
8861 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8862
8863 (*swap_in) (abfd, erel, s->rela);
8864 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8865 s->u.sym_mask = r_sym_mask;
8866 p += sort_elt;
8867 erel += ext_size;
8868 }
8869 }
8870
8871 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8872
8873 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8874 {
8875 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8876 if (s->type != reloc_class_relative)
8877 break;
8878 }
8879 ret = i;
8880 s_non_relative = p;
8881
8882 sq = (struct elf_link_sort_rela *) s_non_relative;
8883 for (; i < count; i++, p += sort_elt)
8884 {
8885 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8886 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8887 sq = sp;
8888 sp->u.offset = sq->rela->r_offset;
8889 }
8890
8891 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8892
8893 struct elf_link_hash_table *htab = elf_hash_table (info);
8894 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8895 {
8896 /* We have plt relocs in .rela.dyn. */
8897 sq = (struct elf_link_sort_rela *) sort;
8898 for (i = 0; i < count; i++)
8899 if (sq[count - i - 1].type != reloc_class_plt)
8900 break;
8901 if (i != 0 && htab->srelplt->size == i * ext_size)
8902 {
8903 struct bfd_link_order **plo;
8904 /* Put srelplt link_order last. This is so the output_offset
8905 set in the next loop is correct for DT_JMPREL. */
8906 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8907 if ((*plo)->type == bfd_indirect_link_order
8908 && (*plo)->u.indirect.section == htab->srelplt)
8909 {
8910 lo = *plo;
8911 *plo = lo->next;
8912 }
8913 else
8914 plo = &(*plo)->next;
8915 *plo = lo;
8916 lo->next = NULL;
8917 dynamic_relocs->map_tail.link_order = lo;
8918 }
8919 }
8920
8921 p = sort;
8922 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8923 if (lo->type == bfd_indirect_link_order)
8924 {
8925 bfd_byte *erel, *erelend;
8926 asection *o = lo->u.indirect.section;
8927
8928 erel = o->contents;
8929 erelend = o->contents + o->size;
8930 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8931 while (erel < erelend)
8932 {
8933 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8934 (*swap_out) (abfd, s->rela, erel);
8935 p += sort_elt;
8936 erel += ext_size;
8937 }
8938 }
8939
8940 free (sort);
8941 *psec = dynamic_relocs;
8942 return ret;
8943}
8944
8945/* Add a symbol to the output symbol string table. */
8946
8947static int
8948elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8949 const char *name,
8950 Elf_Internal_Sym *elfsym,
8951 asection *input_sec,
8952 struct elf_link_hash_entry *h)
8953{
8954 int (*output_symbol_hook)
8955 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8956 struct elf_link_hash_entry *);
8957 struct elf_link_hash_table *hash_table;
8958 const struct elf_backend_data *bed;
8959 bfd_size_type strtabsize;
8960
8961 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8962
8963 bed = get_elf_backend_data (flinfo->output_bfd);
8964 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8965 if (output_symbol_hook != NULL)
8966 {
8967 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8968 if (ret != 1)
8969 return ret;
8970 }
8971
8972 if (name == NULL
8973 || *name == '\0'
8974 || (input_sec->flags & SEC_EXCLUDE))
8975 elfsym->st_name = (unsigned long) -1;
8976 else
8977 {
8978 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8979 to get the final offset for st_name. */
8980 elfsym->st_name
8981 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8982 name, FALSE);
8983 if (elfsym->st_name == (unsigned long) -1)
8984 return 0;
8985 }
8986
8987 hash_table = elf_hash_table (flinfo->info);
8988 strtabsize = hash_table->strtabsize;
8989 if (strtabsize <= hash_table->strtabcount)
8990 {
8991 strtabsize += strtabsize;
8992 hash_table->strtabsize = strtabsize;
8993 strtabsize *= sizeof (*hash_table->strtab);
8994 hash_table->strtab
8995 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8996 strtabsize);
8997 if (hash_table->strtab == NULL)
8998 return 0;
8999 }
9000 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9001 hash_table->strtab[hash_table->strtabcount].dest_index
9002 = hash_table->strtabcount;
9003 hash_table->strtab[hash_table->strtabcount].destshndx_index
9004 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9005
9006 bfd_get_symcount (flinfo->output_bfd) += 1;
9007 hash_table->strtabcount += 1;
9008
9009 return 1;
9010}
9011
9012/* Swap symbols out to the symbol table and flush the output symbols to
9013 the file. */
9014
9015static bfd_boolean
9016elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9017{
9018 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9019 bfd_size_type amt;
9020 size_t i;
9021 const struct elf_backend_data *bed;
9022 bfd_byte *symbuf;
9023 Elf_Internal_Shdr *hdr;
9024 file_ptr pos;
9025 bfd_boolean ret;
9026
9027 if (!hash_table->strtabcount)
9028 return TRUE;
9029
9030 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9031
9032 bed = get_elf_backend_data (flinfo->output_bfd);
9033
9034 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9035 symbuf = (bfd_byte *) bfd_malloc (amt);
9036 if (symbuf == NULL)
9037 return FALSE;
9038
9039 if (flinfo->symshndxbuf)
9040 {
9041 amt = sizeof (Elf_External_Sym_Shndx);
9042 amt *= bfd_get_symcount (flinfo->output_bfd);
9043 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9044 if (flinfo->symshndxbuf == NULL)
9045 {
9046 free (symbuf);
9047 return FALSE;
9048 }
9049 }
9050
9051 for (i = 0; i < hash_table->strtabcount; i++)
9052 {
9053 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9054 if (elfsym->sym.st_name == (unsigned long) -1)
9055 elfsym->sym.st_name = 0;
9056 else
9057 elfsym->sym.st_name
9058 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9059 elfsym->sym.st_name);
9060 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9061 ((bfd_byte *) symbuf
9062 + (elfsym->dest_index
9063 * bed->s->sizeof_sym)),
9064 (flinfo->symshndxbuf
9065 + elfsym->destshndx_index));
9066 }
9067
9068 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9069 pos = hdr->sh_offset + hdr->sh_size;
9070 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9071 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9072 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9073 {
9074 hdr->sh_size += amt;
9075 ret = TRUE;
9076 }
9077 else
9078 ret = FALSE;
9079
9080 free (symbuf);
9081
9082 free (hash_table->strtab);
9083 hash_table->strtab = NULL;
9084
9085 return ret;
9086}
9087
9088/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9089
9090static bfd_boolean
9091check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9092{
9093 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9094 && sym->st_shndx < SHN_LORESERVE)
9095 {
9096 /* The gABI doesn't support dynamic symbols in output sections
9097 beyond 64k. */
9098 _bfd_error_handler
9099 /* xgettext:c-format */
9100 (_("%B: Too many sections: %d (>= %d)"),
9101 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9102 bfd_set_error (bfd_error_nonrepresentable_section);
9103 return FALSE;
9104 }
9105 return TRUE;
9106}
9107
9108/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9109 allowing an unsatisfied unversioned symbol in the DSO to match a
9110 versioned symbol that would normally require an explicit version.
9111 We also handle the case that a DSO references a hidden symbol
9112 which may be satisfied by a versioned symbol in another DSO. */
9113
9114static bfd_boolean
9115elf_link_check_versioned_symbol (struct bfd_link_info *info,
9116 const struct elf_backend_data *bed,
9117 struct elf_link_hash_entry *h)
9118{
9119 bfd *abfd;
9120 struct elf_link_loaded_list *loaded;
9121
9122 if (!is_elf_hash_table (info->hash))
9123 return FALSE;
9124
9125 /* Check indirect symbol. */
9126 while (h->root.type == bfd_link_hash_indirect)
9127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9128
9129 switch (h->root.type)
9130 {
9131 default:
9132 abfd = NULL;
9133 break;
9134
9135 case bfd_link_hash_undefined:
9136 case bfd_link_hash_undefweak:
9137 abfd = h->root.u.undef.abfd;
9138 if (abfd == NULL
9139 || (abfd->flags & DYNAMIC) == 0
9140 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9141 return FALSE;
9142 break;
9143
9144 case bfd_link_hash_defined:
9145 case bfd_link_hash_defweak:
9146 abfd = h->root.u.def.section->owner;
9147 break;
9148
9149 case bfd_link_hash_common:
9150 abfd = h->root.u.c.p->section->owner;
9151 break;
9152 }
9153 BFD_ASSERT (abfd != NULL);
9154
9155 for (loaded = elf_hash_table (info)->loaded;
9156 loaded != NULL;
9157 loaded = loaded->next)
9158 {
9159 bfd *input;
9160 Elf_Internal_Shdr *hdr;
9161 size_t symcount;
9162 size_t extsymcount;
9163 size_t extsymoff;
9164 Elf_Internal_Shdr *versymhdr;
9165 Elf_Internal_Sym *isym;
9166 Elf_Internal_Sym *isymend;
9167 Elf_Internal_Sym *isymbuf;
9168 Elf_External_Versym *ever;
9169 Elf_External_Versym *extversym;
9170
9171 input = loaded->abfd;
9172
9173 /* We check each DSO for a possible hidden versioned definition. */
9174 if (input == abfd
9175 || (input->flags & DYNAMIC) == 0
9176 || elf_dynversym (input) == 0)
9177 continue;
9178
9179 hdr = &elf_tdata (input)->dynsymtab_hdr;
9180
9181 symcount = hdr->sh_size / bed->s->sizeof_sym;
9182 if (elf_bad_symtab (input))
9183 {
9184 extsymcount = symcount;
9185 extsymoff = 0;
9186 }
9187 else
9188 {
9189 extsymcount = symcount - hdr->sh_info;
9190 extsymoff = hdr->sh_info;
9191 }
9192
9193 if (extsymcount == 0)
9194 continue;
9195
9196 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9197 NULL, NULL, NULL);
9198 if (isymbuf == NULL)
9199 return FALSE;
9200
9201 /* Read in any version definitions. */
9202 versymhdr = &elf_tdata (input)->dynversym_hdr;
9203 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9204 if (extversym == NULL)
9205 goto error_ret;
9206
9207 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9208 || (bfd_bread (extversym, versymhdr->sh_size, input)
9209 != versymhdr->sh_size))
9210 {
9211 free (extversym);
9212 error_ret:
9213 free (isymbuf);
9214 return FALSE;
9215 }
9216
9217 ever = extversym + extsymoff;
9218 isymend = isymbuf + extsymcount;
9219 for (isym = isymbuf; isym < isymend; isym++, ever++)
9220 {
9221 const char *name;
9222 Elf_Internal_Versym iver;
9223 unsigned short version_index;
9224
9225 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9226 || isym->st_shndx == SHN_UNDEF)
9227 continue;
9228
9229 name = bfd_elf_string_from_elf_section (input,
9230 hdr->sh_link,
9231 isym->st_name);
9232 if (strcmp (name, h->root.root.string) != 0)
9233 continue;
9234
9235 _bfd_elf_swap_versym_in (input, ever, &iver);
9236
9237 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9238 && !(h->def_regular
9239 && h->forced_local))
9240 {
9241 /* If we have a non-hidden versioned sym, then it should
9242 have provided a definition for the undefined sym unless
9243 it is defined in a non-shared object and forced local.
9244 */
9245 abort ();
9246 }
9247
9248 version_index = iver.vs_vers & VERSYM_VERSION;
9249 if (version_index == 1 || version_index == 2)
9250 {
9251 /* This is the base or first version. We can use it. */
9252 free (extversym);
9253 free (isymbuf);
9254 return TRUE;
9255 }
9256 }
9257
9258 free (extversym);
9259 free (isymbuf);
9260 }
9261
9262 return FALSE;
9263}
9264
9265/* Convert ELF common symbol TYPE. */
9266
9267static int
9268elf_link_convert_common_type (struct bfd_link_info *info, int type)
9269{
9270 /* Commom symbol can only appear in relocatable link. */
9271 if (!bfd_link_relocatable (info))
9272 abort ();
9273 switch (info->elf_stt_common)
9274 {
9275 case unchanged:
9276 break;
9277 case elf_stt_common:
9278 type = STT_COMMON;
9279 break;
9280 case no_elf_stt_common:
9281 type = STT_OBJECT;
9282 break;
9283 }
9284 return type;
9285}
9286
9287/* Add an external symbol to the symbol table. This is called from
9288 the hash table traversal routine. When generating a shared object,
9289 we go through the symbol table twice. The first time we output
9290 anything that might have been forced to local scope in a version
9291 script. The second time we output the symbols that are still
9292 global symbols. */
9293
9294static bfd_boolean
9295elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9296{
9297 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9298 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9299 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9300 bfd_boolean strip;
9301 Elf_Internal_Sym sym;
9302 asection *input_sec;
9303 const struct elf_backend_data *bed;
9304 long indx;
9305 int ret;
9306 unsigned int type;
9307
9308 if (h->root.type == bfd_link_hash_warning)
9309 {
9310 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9311 if (h->root.type == bfd_link_hash_new)
9312 return TRUE;
9313 }
9314
9315 /* Decide whether to output this symbol in this pass. */
9316 if (eoinfo->localsyms)
9317 {
9318 if (!h->forced_local)
9319 return TRUE;
9320 }
9321 else
9322 {
9323 if (h->forced_local)
9324 return TRUE;
9325 }
9326
9327 bed = get_elf_backend_data (flinfo->output_bfd);
9328
9329 if (h->root.type == bfd_link_hash_undefined)
9330 {
9331 /* If we have an undefined symbol reference here then it must have
9332 come from a shared library that is being linked in. (Undefined
9333 references in regular files have already been handled unless
9334 they are in unreferenced sections which are removed by garbage
9335 collection). */
9336 bfd_boolean ignore_undef = FALSE;
9337
9338 /* Some symbols may be special in that the fact that they're
9339 undefined can be safely ignored - let backend determine that. */
9340 if (bed->elf_backend_ignore_undef_symbol)
9341 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9342
9343 /* If we are reporting errors for this situation then do so now. */
9344 if (!ignore_undef
9345 && h->ref_dynamic
9346 && (!h->ref_regular || flinfo->info->gc_sections)
9347 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9348 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9349 (*flinfo->info->callbacks->undefined_symbol)
9350 (flinfo->info, h->root.root.string,
9351 h->ref_regular ? NULL : h->root.u.undef.abfd,
9352 NULL, 0,
9353 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9354
9355 /* Strip a global symbol defined in a discarded section. */
9356 if (h->indx == -3)
9357 return TRUE;
9358 }
9359
9360 /* We should also warn if a forced local symbol is referenced from
9361 shared libraries. */
9362 if (bfd_link_executable (flinfo->info)
9363 && h->forced_local
9364 && h->ref_dynamic
9365 && h->def_regular
9366 && !h->dynamic_def
9367 && h->ref_dynamic_nonweak
9368 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9369 {
9370 bfd *def_bfd;
9371 const char *msg;
9372 struct elf_link_hash_entry *hi = h;
9373
9374 /* Check indirect symbol. */
9375 while (hi->root.type == bfd_link_hash_indirect)
9376 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9377
9378 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9379 /* xgettext:c-format */
9380 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9381 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9382 /* xgettext:c-format */
9383 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9384 else
9385 /* xgettext:c-format */
9386 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9387 def_bfd = flinfo->output_bfd;
9388 if (hi->root.u.def.section != bfd_abs_section_ptr)
9389 def_bfd = hi->root.u.def.section->owner;
9390 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9391 h->root.root.string);
9392 bfd_set_error (bfd_error_bad_value);
9393 eoinfo->failed = TRUE;
9394 return FALSE;
9395 }
9396
9397 /* We don't want to output symbols that have never been mentioned by
9398 a regular file, or that we have been told to strip. However, if
9399 h->indx is set to -2, the symbol is used by a reloc and we must
9400 output it. */
9401 strip = FALSE;
9402 if (h->indx == -2)
9403 ;
9404 else if ((h->def_dynamic
9405 || h->ref_dynamic
9406 || h->root.type == bfd_link_hash_new)
9407 && !h->def_regular
9408 && !h->ref_regular)
9409 strip = TRUE;
9410 else if (flinfo->info->strip == strip_all)
9411 strip = TRUE;
9412 else if (flinfo->info->strip == strip_some
9413 && bfd_hash_lookup (flinfo->info->keep_hash,
9414 h->root.root.string, FALSE, FALSE) == NULL)
9415 strip = TRUE;
9416 else if ((h->root.type == bfd_link_hash_defined
9417 || h->root.type == bfd_link_hash_defweak)
9418 && ((flinfo->info->strip_discarded
9419 && discarded_section (h->root.u.def.section))
9420 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9421 && h->root.u.def.section->owner != NULL
9422 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9423 strip = TRUE;
9424 else if ((h->root.type == bfd_link_hash_undefined
9425 || h->root.type == bfd_link_hash_undefweak)
9426 && h->root.u.undef.abfd != NULL
9427 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9428 strip = TRUE;
9429
9430 type = h->type;
9431
9432 /* If we're stripping it, and it's not a dynamic symbol, there's
9433 nothing else to do. However, if it is a forced local symbol or
9434 an ifunc symbol we need to give the backend finish_dynamic_symbol
9435 function a chance to make it dynamic. */
9436 if (strip
9437 && h->dynindx == -1
9438 && type != STT_GNU_IFUNC
9439 && !h->forced_local)
9440 return TRUE;
9441
9442 sym.st_value = 0;
9443 sym.st_size = h->size;
9444 sym.st_other = h->other;
9445 switch (h->root.type)
9446 {
9447 default:
9448 case bfd_link_hash_new:
9449 case bfd_link_hash_warning:
9450 abort ();
9451 return FALSE;
9452
9453 case bfd_link_hash_undefined:
9454 case bfd_link_hash_undefweak:
9455 input_sec = bfd_und_section_ptr;
9456 sym.st_shndx = SHN_UNDEF;
9457 break;
9458
9459 case bfd_link_hash_defined:
9460 case bfd_link_hash_defweak:
9461 {
9462 input_sec = h->root.u.def.section;
9463 if (input_sec->output_section != NULL)
9464 {
9465 sym.st_shndx =
9466 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9467 input_sec->output_section);
9468 if (sym.st_shndx == SHN_BAD)
9469 {
9470 _bfd_error_handler
9471 /* xgettext:c-format */
9472 (_("%B: could not find output section %A for input section %A"),
9473 flinfo->output_bfd, input_sec->output_section, input_sec);
9474 bfd_set_error (bfd_error_nonrepresentable_section);
9475 eoinfo->failed = TRUE;
9476 return FALSE;
9477 }
9478
9479 /* ELF symbols in relocatable files are section relative,
9480 but in nonrelocatable files they are virtual
9481 addresses. */
9482 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9483 if (!bfd_link_relocatable (flinfo->info))
9484 {
9485 sym.st_value += input_sec->output_section->vma;
9486 if (h->type == STT_TLS)
9487 {
9488 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9489 if (tls_sec != NULL)
9490 sym.st_value -= tls_sec->vma;
9491 }
9492 }
9493 }
9494 else
9495 {
9496 BFD_ASSERT (input_sec->owner == NULL
9497 || (input_sec->owner->flags & DYNAMIC) != 0);
9498 sym.st_shndx = SHN_UNDEF;
9499 input_sec = bfd_und_section_ptr;
9500 }
9501 }
9502 break;
9503
9504 case bfd_link_hash_common:
9505 input_sec = h->root.u.c.p->section;
9506 sym.st_shndx = bed->common_section_index (input_sec);
9507 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9508 break;
9509
9510 case bfd_link_hash_indirect:
9511 /* These symbols are created by symbol versioning. They point
9512 to the decorated version of the name. For example, if the
9513 symbol foo@@GNU_1.2 is the default, which should be used when
9514 foo is used with no version, then we add an indirect symbol
9515 foo which points to foo@@GNU_1.2. We ignore these symbols,
9516 since the indirected symbol is already in the hash table. */
9517 return TRUE;
9518 }
9519
9520 if (type == STT_COMMON || type == STT_OBJECT)
9521 switch (h->root.type)
9522 {
9523 case bfd_link_hash_common:
9524 type = elf_link_convert_common_type (flinfo->info, type);
9525 break;
9526 case bfd_link_hash_defined:
9527 case bfd_link_hash_defweak:
9528 if (bed->common_definition (&sym))
9529 type = elf_link_convert_common_type (flinfo->info, type);
9530 else
9531 type = STT_OBJECT;
9532 break;
9533 case bfd_link_hash_undefined:
9534 case bfd_link_hash_undefweak:
9535 break;
9536 default:
9537 abort ();
9538 }
9539
9540 if (h->forced_local)
9541 {
9542 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9543 /* Turn off visibility on local symbol. */
9544 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9545 }
9546 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9547 else if (h->unique_global && h->def_regular)
9548 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9549 else if (h->root.type == bfd_link_hash_undefweak
9550 || h->root.type == bfd_link_hash_defweak)
9551 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9552 else
9553 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9554 sym.st_target_internal = h->target_internal;
9555
9556 /* Give the processor backend a chance to tweak the symbol value,
9557 and also to finish up anything that needs to be done for this
9558 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9559 forced local syms when non-shared is due to a historical quirk.
9560 STT_GNU_IFUNC symbol must go through PLT. */
9561 if ((h->type == STT_GNU_IFUNC
9562 && h->def_regular
9563 && !bfd_link_relocatable (flinfo->info))
9564 || ((h->dynindx != -1
9565 || h->forced_local)
9566 && ((bfd_link_pic (flinfo->info)
9567 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9568 || h->root.type != bfd_link_hash_undefweak))
9569 || !h->forced_local)
9570 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9571 {
9572 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9573 (flinfo->output_bfd, flinfo->info, h, &sym)))
9574 {
9575 eoinfo->failed = TRUE;
9576 return FALSE;
9577 }
9578 }
9579
9580 /* If we are marking the symbol as undefined, and there are no
9581 non-weak references to this symbol from a regular object, then
9582 mark the symbol as weak undefined; if there are non-weak
9583 references, mark the symbol as strong. We can't do this earlier,
9584 because it might not be marked as undefined until the
9585 finish_dynamic_symbol routine gets through with it. */
9586 if (sym.st_shndx == SHN_UNDEF
9587 && h->ref_regular
9588 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9589 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9590 {
9591 int bindtype;
9592 type = ELF_ST_TYPE (sym.st_info);
9593
9594 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9595 if (type == STT_GNU_IFUNC)
9596 type = STT_FUNC;
9597
9598 if (h->ref_regular_nonweak)
9599 bindtype = STB_GLOBAL;
9600 else
9601 bindtype = STB_WEAK;
9602 sym.st_info = ELF_ST_INFO (bindtype, type);
9603 }
9604
9605 /* If this is a symbol defined in a dynamic library, don't use the
9606 symbol size from the dynamic library. Relinking an executable
9607 against a new library may introduce gratuitous changes in the
9608 executable's symbols if we keep the size. */
9609 if (sym.st_shndx == SHN_UNDEF
9610 && !h->def_regular
9611 && h->def_dynamic)
9612 sym.st_size = 0;
9613
9614 /* If a non-weak symbol with non-default visibility is not defined
9615 locally, it is a fatal error. */
9616 if (!bfd_link_relocatable (flinfo->info)
9617 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9618 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9619 && h->root.type == bfd_link_hash_undefined
9620 && !h->def_regular)
9621 {
9622 const char *msg;
9623
9624 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9625 /* xgettext:c-format */
9626 msg = _("%B: protected symbol `%s' isn't defined");
9627 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9628 /* xgettext:c-format */
9629 msg = _("%B: internal symbol `%s' isn't defined");
9630 else
9631 /* xgettext:c-format */
9632 msg = _("%B: hidden symbol `%s' isn't defined");
9633 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9634 bfd_set_error (bfd_error_bad_value);
9635 eoinfo->failed = TRUE;
9636 return FALSE;
9637 }
9638
9639 /* If this symbol should be put in the .dynsym section, then put it
9640 there now. We already know the symbol index. We also fill in
9641 the entry in the .hash section. */
9642 if (elf_hash_table (flinfo->info)->dynsym != NULL
9643 && h->dynindx != -1
9644 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9645 {
9646 bfd_byte *esym;
9647
9648 /* Since there is no version information in the dynamic string,
9649 if there is no version info in symbol version section, we will
9650 have a run-time problem if not linking executable, referenced
9651 by shared library, or not bound locally. */
9652 if (h->verinfo.verdef == NULL
9653 && (!bfd_link_executable (flinfo->info)
9654 || h->ref_dynamic
9655 || !h->def_regular))
9656 {
9657 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9658
9659 if (p && p [1] != '\0')
9660 {
9661 _bfd_error_handler
9662 /* xgettext:c-format */
9663 (_("%B: No symbol version section for versioned symbol `%s'"),
9664 flinfo->output_bfd, h->root.root.string);
9665 eoinfo->failed = TRUE;
9666 return FALSE;
9667 }
9668 }
9669
9670 sym.st_name = h->dynstr_index;
9671 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9672 + h->dynindx * bed->s->sizeof_sym);
9673 if (!check_dynsym (flinfo->output_bfd, &sym))
9674 {
9675 eoinfo->failed = TRUE;
9676 return FALSE;
9677 }
9678 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9679
9680 if (flinfo->hash_sec != NULL)
9681 {
9682 size_t hash_entry_size;
9683 bfd_byte *bucketpos;
9684 bfd_vma chain;
9685 size_t bucketcount;
9686 size_t bucket;
9687
9688 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9689 bucket = h->u.elf_hash_value % bucketcount;
9690
9691 hash_entry_size
9692 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9693 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9694 + (bucket + 2) * hash_entry_size);
9695 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9696 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9697 bucketpos);
9698 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9699 ((bfd_byte *) flinfo->hash_sec->contents
9700 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9701 }
9702
9703 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9704 {
9705 Elf_Internal_Versym iversym;
9706 Elf_External_Versym *eversym;
9707
9708 if (!h->def_regular)
9709 {
9710 if (h->verinfo.verdef == NULL
9711 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9712 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9713 iversym.vs_vers = 0;
9714 else
9715 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9716 }
9717 else
9718 {
9719 if (h->verinfo.vertree == NULL)
9720 iversym.vs_vers = 1;
9721 else
9722 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9723 if (flinfo->info->create_default_symver)
9724 iversym.vs_vers++;
9725 }
9726
9727 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9728 defined locally. */
9729 if (h->versioned == versioned_hidden && h->def_regular)
9730 iversym.vs_vers |= VERSYM_HIDDEN;
9731
9732 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9733 eversym += h->dynindx;
9734 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9735 }
9736 }
9737
9738 /* If the symbol is undefined, and we didn't output it to .dynsym,
9739 strip it from .symtab too. Obviously we can't do this for
9740 relocatable output or when needed for --emit-relocs. */
9741 else if (input_sec == bfd_und_section_ptr
9742 && h->indx != -2
9743 && !bfd_link_relocatable (flinfo->info))
9744 return TRUE;
9745 /* Also strip others that we couldn't earlier due to dynamic symbol
9746 processing. */
9747 if (strip)
9748 return TRUE;
9749 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9750 return TRUE;
9751
9752 /* Output a FILE symbol so that following locals are not associated
9753 with the wrong input file. We need one for forced local symbols
9754 if we've seen more than one FILE symbol or when we have exactly
9755 one FILE symbol but global symbols are present in a file other
9756 than the one with the FILE symbol. We also need one if linker
9757 defined symbols are present. In practice these conditions are
9758 always met, so just emit the FILE symbol unconditionally. */
9759 if (eoinfo->localsyms
9760 && !eoinfo->file_sym_done
9761 && eoinfo->flinfo->filesym_count != 0)
9762 {
9763 Elf_Internal_Sym fsym;
9764
9765 memset (&fsym, 0, sizeof (fsym));
9766 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9767 fsym.st_shndx = SHN_ABS;
9768 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9769 bfd_und_section_ptr, NULL))
9770 return FALSE;
9771
9772 eoinfo->file_sym_done = TRUE;
9773 }
9774
9775 indx = bfd_get_symcount (flinfo->output_bfd);
9776 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9777 input_sec, h);
9778 if (ret == 0)
9779 {
9780 eoinfo->failed = TRUE;
9781 return FALSE;
9782 }
9783 else if (ret == 1)
9784 h->indx = indx;
9785 else if (h->indx == -2)
9786 abort();
9787
9788 return TRUE;
9789}
9790
9791/* Return TRUE if special handling is done for relocs in SEC against
9792 symbols defined in discarded sections. */
9793
9794static bfd_boolean
9795elf_section_ignore_discarded_relocs (asection *sec)
9796{
9797 const struct elf_backend_data *bed;
9798
9799 switch (sec->sec_info_type)
9800 {
9801 case SEC_INFO_TYPE_STABS:
9802 case SEC_INFO_TYPE_EH_FRAME:
9803 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9804 return TRUE;
9805 default:
9806 break;
9807 }
9808
9809 bed = get_elf_backend_data (sec->owner);
9810 if (bed->elf_backend_ignore_discarded_relocs != NULL
9811 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9812 return TRUE;
9813
9814 return FALSE;
9815}
9816
9817/* Return a mask saying how ld should treat relocations in SEC against
9818 symbols defined in discarded sections. If this function returns
9819 COMPLAIN set, ld will issue a warning message. If this function
9820 returns PRETEND set, and the discarded section was link-once and the
9821 same size as the kept link-once section, ld will pretend that the
9822 symbol was actually defined in the kept section. Otherwise ld will
9823 zero the reloc (at least that is the intent, but some cooperation by
9824 the target dependent code is needed, particularly for REL targets). */
9825
9826unsigned int
9827_bfd_elf_default_action_discarded (asection *sec)
9828{
9829 if (sec->flags & SEC_DEBUGGING)
9830 return PRETEND;
9831
9832 if (strcmp (".eh_frame", sec->name) == 0)
9833 return 0;
9834
9835 if (strcmp (".gcc_except_table", sec->name) == 0)
9836 return 0;
9837
9838 return COMPLAIN | PRETEND;
9839}
9840
9841/* Find a match between a section and a member of a section group. */
9842
9843static asection *
9844match_group_member (asection *sec, asection *group,
9845 struct bfd_link_info *info)
9846{
9847 asection *first = elf_next_in_group (group);
9848 asection *s = first;
9849
9850 while (s != NULL)
9851 {
9852 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9853 return s;
9854
9855 s = elf_next_in_group (s);
9856 if (s == first)
9857 break;
9858 }
9859
9860 return NULL;
9861}
9862
9863/* Check if the kept section of a discarded section SEC can be used
9864 to replace it. Return the replacement if it is OK. Otherwise return
9865 NULL. */
9866
9867asection *
9868_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9869{
9870 asection *kept;
9871
9872 kept = sec->kept_section;
9873 if (kept != NULL)
9874 {
9875 if ((kept->flags & SEC_GROUP) != 0)
9876 kept = match_group_member (sec, kept, info);
9877 if (kept != NULL
9878 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9879 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9880 kept = NULL;
9881 sec->kept_section = kept;
9882 }
9883 return kept;
9884}
9885
9886/* Link an input file into the linker output file. This function
9887 handles all the sections and relocations of the input file at once.
9888 This is so that we only have to read the local symbols once, and
9889 don't have to keep them in memory. */
9890
9891static bfd_boolean
9892elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9893{
9894 int (*relocate_section)
9895 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9896 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9897 bfd *output_bfd;
9898 Elf_Internal_Shdr *symtab_hdr;
9899 size_t locsymcount;
9900 size_t extsymoff;
9901 Elf_Internal_Sym *isymbuf;
9902 Elf_Internal_Sym *isym;
9903 Elf_Internal_Sym *isymend;
9904 long *pindex;
9905 asection **ppsection;
9906 asection *o;
9907 const struct elf_backend_data *bed;
9908 struct elf_link_hash_entry **sym_hashes;
9909 bfd_size_type address_size;
9910 bfd_vma r_type_mask;
9911 int r_sym_shift;
9912 bfd_boolean have_file_sym = FALSE;
9913
9914 output_bfd = flinfo->output_bfd;
9915 bed = get_elf_backend_data (output_bfd);
9916 relocate_section = bed->elf_backend_relocate_section;
9917
9918 /* If this is a dynamic object, we don't want to do anything here:
9919 we don't want the local symbols, and we don't want the section
9920 contents. */
9921 if ((input_bfd->flags & DYNAMIC) != 0)
9922 return TRUE;
9923
9924 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9925 if (elf_bad_symtab (input_bfd))
9926 {
9927 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9928 extsymoff = 0;
9929 }
9930 else
9931 {
9932 locsymcount = symtab_hdr->sh_info;
9933 extsymoff = symtab_hdr->sh_info;
9934 }
9935
9936 /* Read the local symbols. */
9937 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9938 if (isymbuf == NULL && locsymcount != 0)
9939 {
9940 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9941 flinfo->internal_syms,
9942 flinfo->external_syms,
9943 flinfo->locsym_shndx);
9944 if (isymbuf == NULL)
9945 return FALSE;
9946 }
9947
9948 /* Find local symbol sections and adjust values of symbols in
9949 SEC_MERGE sections. Write out those local symbols we know are
9950 going into the output file. */
9951 isymend = isymbuf + locsymcount;
9952 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9953 isym < isymend;
9954 isym++, pindex++, ppsection++)
9955 {
9956 asection *isec;
9957 const char *name;
9958 Elf_Internal_Sym osym;
9959 long indx;
9960 int ret;
9961
9962 *pindex = -1;
9963
9964 if (elf_bad_symtab (input_bfd))
9965 {
9966 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9967 {
9968 *ppsection = NULL;
9969 continue;
9970 }
9971 }
9972
9973 if (isym->st_shndx == SHN_UNDEF)
9974 isec = bfd_und_section_ptr;
9975 else if (isym->st_shndx == SHN_ABS)
9976 isec = bfd_abs_section_ptr;
9977 else if (isym->st_shndx == SHN_COMMON)
9978 isec = bfd_com_section_ptr;
9979 else
9980 {
9981 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9982 if (isec == NULL)
9983 {
9984 /* Don't attempt to output symbols with st_shnx in the
9985 reserved range other than SHN_ABS and SHN_COMMON. */
9986 *ppsection = NULL;
9987 continue;
9988 }
9989 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9990 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9991 isym->st_value =
9992 _bfd_merged_section_offset (output_bfd, &isec,
9993 elf_section_data (isec)->sec_info,
9994 isym->st_value);
9995 }
9996
9997 *ppsection = isec;
9998
9999 /* Don't output the first, undefined, symbol. In fact, don't
10000 output any undefined local symbol. */
10001 if (isec == bfd_und_section_ptr)
10002 continue;
10003
10004 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10005 {
10006 /* We never output section symbols. Instead, we use the
10007 section symbol of the corresponding section in the output
10008 file. */
10009 continue;
10010 }
10011
10012 /* If we are stripping all symbols, we don't want to output this
10013 one. */
10014 if (flinfo->info->strip == strip_all)
10015 continue;
10016
10017 /* If we are discarding all local symbols, we don't want to
10018 output this one. If we are generating a relocatable output
10019 file, then some of the local symbols may be required by
10020 relocs; we output them below as we discover that they are
10021 needed. */
10022 if (flinfo->info->discard == discard_all)
10023 continue;
10024
10025 /* If this symbol is defined in a section which we are
10026 discarding, we don't need to keep it. */
10027 if (isym->st_shndx != SHN_UNDEF
10028 && isym->st_shndx < SHN_LORESERVE
10029 && bfd_section_removed_from_list (output_bfd,
10030 isec->output_section))
10031 continue;
10032
10033 /* Get the name of the symbol. */
10034 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10035 isym->st_name);
10036 if (name == NULL)
10037 return FALSE;
10038
10039 /* See if we are discarding symbols with this name. */
10040 if ((flinfo->info->strip == strip_some
10041 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10042 == NULL))
10043 || (((flinfo->info->discard == discard_sec_merge
10044 && (isec->flags & SEC_MERGE)
10045 && !bfd_link_relocatable (flinfo->info))
10046 || flinfo->info->discard == discard_l)
10047 && bfd_is_local_label_name (input_bfd, name)))
10048 continue;
10049
10050 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10051 {
10052 if (input_bfd->lto_output)
10053 /* -flto puts a temp file name here. This means builds
10054 are not reproducible. Discard the symbol. */
10055 continue;
10056 have_file_sym = TRUE;
10057 flinfo->filesym_count += 1;
10058 }
10059 if (!have_file_sym)
10060 {
10061 /* In the absence of debug info, bfd_find_nearest_line uses
10062 FILE symbols to determine the source file for local
10063 function symbols. Provide a FILE symbol here if input
10064 files lack such, so that their symbols won't be
10065 associated with a previous input file. It's not the
10066 source file, but the best we can do. */
10067 have_file_sym = TRUE;
10068 flinfo->filesym_count += 1;
10069 memset (&osym, 0, sizeof (osym));
10070 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10071 osym.st_shndx = SHN_ABS;
10072 if (!elf_link_output_symstrtab (flinfo,
10073 (input_bfd->lto_output ? NULL
10074 : input_bfd->filename),
10075 &osym, bfd_abs_section_ptr,
10076 NULL))
10077 return FALSE;
10078 }
10079
10080 osym = *isym;
10081
10082 /* Adjust the section index for the output file. */
10083 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10084 isec->output_section);
10085 if (osym.st_shndx == SHN_BAD)
10086 return FALSE;
10087
10088 /* ELF symbols in relocatable files are section relative, but
10089 in executable files they are virtual addresses. Note that
10090 this code assumes that all ELF sections have an associated
10091 BFD section with a reasonable value for output_offset; below
10092 we assume that they also have a reasonable value for
10093 output_section. Any special sections must be set up to meet
10094 these requirements. */
10095 osym.st_value += isec->output_offset;
10096 if (!bfd_link_relocatable (flinfo->info))
10097 {
10098 osym.st_value += isec->output_section->vma;
10099 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10100 {
10101 /* STT_TLS symbols are relative to PT_TLS segment base. */
10102 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10103 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10104 }
10105 }
10106
10107 indx = bfd_get_symcount (output_bfd);
10108 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10109 if (ret == 0)
10110 return FALSE;
10111 else if (ret == 1)
10112 *pindex = indx;
10113 }
10114
10115 if (bed->s->arch_size == 32)
10116 {
10117 r_type_mask = 0xff;
10118 r_sym_shift = 8;
10119 address_size = 4;
10120 }
10121 else
10122 {
10123 r_type_mask = 0xffffffff;
10124 r_sym_shift = 32;
10125 address_size = 8;
10126 }
10127
10128 /* Relocate the contents of each section. */
10129 sym_hashes = elf_sym_hashes (input_bfd);
10130 for (o = input_bfd->sections; o != NULL; o = o->next)
10131 {
10132 bfd_byte *contents;
10133
10134 if (! o->linker_mark)
10135 {
10136 /* This section was omitted from the link. */
10137 continue;
10138 }
10139
10140 if (bfd_link_relocatable (flinfo->info)
10141 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10142 {
10143 /* Deal with the group signature symbol. */
10144 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10145 unsigned long symndx = sec_data->this_hdr.sh_info;
10146 asection *osec = o->output_section;
10147
10148 if (symndx >= locsymcount
10149 || (elf_bad_symtab (input_bfd)
10150 && flinfo->sections[symndx] == NULL))
10151 {
10152 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10153 while (h->root.type == bfd_link_hash_indirect
10154 || h->root.type == bfd_link_hash_warning)
10155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10156 /* Arrange for symbol to be output. */
10157 h->indx = -2;
10158 elf_section_data (osec)->this_hdr.sh_info = -2;
10159 }
10160 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10161 {
10162 /* We'll use the output section target_index. */
10163 asection *sec = flinfo->sections[symndx]->output_section;
10164 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10165 }
10166 else
10167 {
10168 if (flinfo->indices[symndx] == -1)
10169 {
10170 /* Otherwise output the local symbol now. */
10171 Elf_Internal_Sym sym = isymbuf[symndx];
10172 asection *sec = flinfo->sections[symndx]->output_section;
10173 const char *name;
10174 long indx;
10175 int ret;
10176
10177 name = bfd_elf_string_from_elf_section (input_bfd,
10178 symtab_hdr->sh_link,
10179 sym.st_name);
10180 if (name == NULL)
10181 return FALSE;
10182
10183 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10184 sec);
10185 if (sym.st_shndx == SHN_BAD)
10186 return FALSE;
10187
10188 sym.st_value += o->output_offset;
10189
10190 indx = bfd_get_symcount (output_bfd);
10191 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10192 NULL);
10193 if (ret == 0)
10194 return FALSE;
10195 else if (ret == 1)
10196 flinfo->indices[symndx] = indx;
10197 else
10198 abort ();
10199 }
10200 elf_section_data (osec)->this_hdr.sh_info
10201 = flinfo->indices[symndx];
10202 }
10203 }
10204
10205 if ((o->flags & SEC_HAS_CONTENTS) == 0
10206 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10207 continue;
10208
10209 if ((o->flags & SEC_LINKER_CREATED) != 0)
10210 {
10211 /* Section was created by _bfd_elf_link_create_dynamic_sections
10212 or somesuch. */
10213 continue;
10214 }
10215
10216 /* Get the contents of the section. They have been cached by a
10217 relaxation routine. Note that o is a section in an input
10218 file, so the contents field will not have been set by any of
10219 the routines which work on output files. */
10220 if (elf_section_data (o)->this_hdr.contents != NULL)
10221 {
10222 contents = elf_section_data (o)->this_hdr.contents;
10223 if (bed->caches_rawsize
10224 && o->rawsize != 0
10225 && o->rawsize < o->size)
10226 {
10227 memcpy (flinfo->contents, contents, o->rawsize);
10228 contents = flinfo->contents;
10229 }
10230 }
10231 else
10232 {
10233 contents = flinfo->contents;
10234 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10235 return FALSE;
10236 }
10237
10238 if ((o->flags & SEC_RELOC) != 0)
10239 {
10240 Elf_Internal_Rela *internal_relocs;
10241 Elf_Internal_Rela *rel, *relend;
10242 int action_discarded;
10243 int ret;
10244
10245 /* Get the swapped relocs. */
10246 internal_relocs
10247 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10248 flinfo->internal_relocs, FALSE);
10249 if (internal_relocs == NULL
10250 && o->reloc_count > 0)
10251 return FALSE;
10252
10253 /* We need to reverse-copy input .ctors/.dtors sections if
10254 they are placed in .init_array/.finit_array for output. */
10255 if (o->size > address_size
10256 && ((strncmp (o->name, ".ctors", 6) == 0
10257 && strcmp (o->output_section->name,
10258 ".init_array") == 0)
10259 || (strncmp (o->name, ".dtors", 6) == 0
10260 && strcmp (o->output_section->name,
10261 ".fini_array") == 0))
10262 && (o->name[6] == 0 || o->name[6] == '.'))
10263 {
10264 if (o->size != o->reloc_count * address_size)
10265 {
10266 _bfd_error_handler
10267 /* xgettext:c-format */
10268 (_("error: %B: size of section %A is not "
10269 "multiple of address size"),
10270 input_bfd, o);
10271 bfd_set_error (bfd_error_on_input);
10272 return FALSE;
10273 }
10274 o->flags |= SEC_ELF_REVERSE_COPY;
10275 }
10276
10277 action_discarded = -1;
10278 if (!elf_section_ignore_discarded_relocs (o))
10279 action_discarded = (*bed->action_discarded) (o);
10280
10281 /* Run through the relocs evaluating complex reloc symbols and
10282 looking for relocs against symbols from discarded sections
10283 or section symbols from removed link-once sections.
10284 Complain about relocs against discarded sections. Zero
10285 relocs against removed link-once sections. */
10286
10287 rel = internal_relocs;
10288 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10289 for ( ; rel < relend; rel++)
10290 {
10291 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10292 unsigned int s_type;
10293 asection **ps, *sec;
10294 struct elf_link_hash_entry *h = NULL;
10295 const char *sym_name;
10296
10297 if (r_symndx == STN_UNDEF)
10298 continue;
10299
10300 if (r_symndx >= locsymcount
10301 || (elf_bad_symtab (input_bfd)
10302 && flinfo->sections[r_symndx] == NULL))
10303 {
10304 h = sym_hashes[r_symndx - extsymoff];
10305
10306 /* Badly formatted input files can contain relocs that
10307 reference non-existant symbols. Check here so that
10308 we do not seg fault. */
10309 if (h == NULL)
10310 {
10311 char buffer [32];
10312
10313 sprintf_vma (buffer, rel->r_info);
10314 _bfd_error_handler
10315 /* xgettext:c-format */
10316 (_("error: %B contains a reloc (0x%s) for section %A "
10317 "that references a non-existent global symbol"),
10318 input_bfd, o, buffer);
10319 bfd_set_error (bfd_error_bad_value);
10320 return FALSE;
10321 }
10322
10323 while (h->root.type == bfd_link_hash_indirect
10324 || h->root.type == bfd_link_hash_warning)
10325 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10326
10327 s_type = h->type;
10328
10329 /* If a plugin symbol is referenced from a non-IR file,
10330 mark the symbol as undefined. Note that the
10331 linker may attach linker created dynamic sections
10332 to the plugin bfd. Symbols defined in linker
10333 created sections are not plugin symbols. */
10334 if (h->root.non_ir_ref
10335 && (h->root.type == bfd_link_hash_defined
10336 || h->root.type == bfd_link_hash_defweak)
10337 && (h->root.u.def.section->flags
10338 & SEC_LINKER_CREATED) == 0
10339 && h->root.u.def.section->owner != NULL
10340 && (h->root.u.def.section->owner->flags
10341 & BFD_PLUGIN) != 0)
10342 {
10343 h->root.type = bfd_link_hash_undefined;
10344 h->root.u.undef.abfd = h->root.u.def.section->owner;
10345 }
10346
10347 ps = NULL;
10348 if (h->root.type == bfd_link_hash_defined
10349 || h->root.type == bfd_link_hash_defweak)
10350 ps = &h->root.u.def.section;
10351
10352 sym_name = h->root.root.string;
10353 }
10354 else
10355 {
10356 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10357
10358 s_type = ELF_ST_TYPE (sym->st_info);
10359 ps = &flinfo->sections[r_symndx];
10360 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10361 sym, *ps);
10362 }
10363
10364 if ((s_type == STT_RELC || s_type == STT_SRELC)
10365 && !bfd_link_relocatable (flinfo->info))
10366 {
10367 bfd_vma val;
10368 bfd_vma dot = (rel->r_offset
10369 + o->output_offset + o->output_section->vma);
10370#ifdef DEBUG
10371 printf ("Encountered a complex symbol!");
10372 printf (" (input_bfd %s, section %s, reloc %ld\n",
10373 input_bfd->filename, o->name,
10374 (long) (rel - internal_relocs));
10375 printf (" symbol: idx %8.8lx, name %s\n",
10376 r_symndx, sym_name);
10377 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10378 (unsigned long) rel->r_info,
10379 (unsigned long) rel->r_offset);
10380#endif
10381 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10382 isymbuf, locsymcount, s_type == STT_SRELC))
10383 return FALSE;
10384
10385 /* Symbol evaluated OK. Update to absolute value. */
10386 set_symbol_value (input_bfd, isymbuf, locsymcount,
10387 r_symndx, val);
10388 continue;
10389 }
10390
10391 if (action_discarded != -1 && ps != NULL)
10392 {
10393 /* Complain if the definition comes from a
10394 discarded section. */
10395 if ((sec = *ps) != NULL && discarded_section (sec))
10396 {
10397 BFD_ASSERT (r_symndx != STN_UNDEF);
10398 if (action_discarded & COMPLAIN)
10399 (*flinfo->info->callbacks->einfo)
10400 /* xgettext:c-format */
10401 (_("%X`%s' referenced in section `%A' of %B: "
10402 "defined in discarded section `%A' of %B\n"),
10403 sym_name, o, input_bfd, sec, sec->owner);
10404
10405 /* Try to do the best we can to support buggy old
10406 versions of gcc. Pretend that the symbol is
10407 really defined in the kept linkonce section.
10408 FIXME: This is quite broken. Modifying the
10409 symbol here means we will be changing all later
10410 uses of the symbol, not just in this section. */
10411 if (action_discarded & PRETEND)
10412 {
10413 asection *kept;
10414
10415 kept = _bfd_elf_check_kept_section (sec,
10416 flinfo->info);
10417 if (kept != NULL)
10418 {
10419 *ps = kept;
10420 continue;
10421 }
10422 }
10423 }
10424 }
10425 }
10426
10427 /* Relocate the section by invoking a back end routine.
10428
10429 The back end routine is responsible for adjusting the
10430 section contents as necessary, and (if using Rela relocs
10431 and generating a relocatable output file) adjusting the
10432 reloc addend as necessary.
10433
10434 The back end routine does not have to worry about setting
10435 the reloc address or the reloc symbol index.
10436
10437 The back end routine is given a pointer to the swapped in
10438 internal symbols, and can access the hash table entries
10439 for the external symbols via elf_sym_hashes (input_bfd).
10440
10441 When generating relocatable output, the back end routine
10442 must handle STB_LOCAL/STT_SECTION symbols specially. The
10443 output symbol is going to be a section symbol
10444 corresponding to the output section, which will require
10445 the addend to be adjusted. */
10446
10447 ret = (*relocate_section) (output_bfd, flinfo->info,
10448 input_bfd, o, contents,
10449 internal_relocs,
10450 isymbuf,
10451 flinfo->sections);
10452 if (!ret)
10453 return FALSE;
10454
10455 if (ret == 2
10456 || bfd_link_relocatable (flinfo->info)
10457 || flinfo->info->emitrelocations)
10458 {
10459 Elf_Internal_Rela *irela;
10460 Elf_Internal_Rela *irelaend, *irelamid;
10461 bfd_vma last_offset;
10462 struct elf_link_hash_entry **rel_hash;
10463 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10464 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10465 unsigned int next_erel;
10466 bfd_boolean rela_normal;
10467 struct bfd_elf_section_data *esdi, *esdo;
10468
10469 esdi = elf_section_data (o);
10470 esdo = elf_section_data (o->output_section);
10471 rela_normal = FALSE;
10472
10473 /* Adjust the reloc addresses and symbol indices. */
10474
10475 irela = internal_relocs;
10476 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10477 rel_hash = esdo->rel.hashes + esdo->rel.count;
10478 /* We start processing the REL relocs, if any. When we reach
10479 IRELAMID in the loop, we switch to the RELA relocs. */
10480 irelamid = irela;
10481 if (esdi->rel.hdr != NULL)
10482 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10483 * bed->s->int_rels_per_ext_rel);
10484 rel_hash_list = rel_hash;
10485 rela_hash_list = NULL;
10486 last_offset = o->output_offset;
10487 if (!bfd_link_relocatable (flinfo->info))
10488 last_offset += o->output_section->vma;
10489 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10490 {
10491 unsigned long r_symndx;
10492 asection *sec;
10493 Elf_Internal_Sym sym;
10494
10495 if (next_erel == bed->s->int_rels_per_ext_rel)
10496 {
10497 rel_hash++;
10498 next_erel = 0;
10499 }
10500
10501 if (irela == irelamid)
10502 {
10503 rel_hash = esdo->rela.hashes + esdo->rela.count;
10504 rela_hash_list = rel_hash;
10505 rela_normal = bed->rela_normal;
10506 }
10507
10508 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10509 flinfo->info, o,
10510 irela->r_offset);
10511 if (irela->r_offset >= (bfd_vma) -2)
10512 {
10513 /* This is a reloc for a deleted entry or somesuch.
10514 Turn it into an R_*_NONE reloc, at the same
10515 offset as the last reloc. elf_eh_frame.c and
10516 bfd_elf_discard_info rely on reloc offsets
10517 being ordered. */
10518 irela->r_offset = last_offset;
10519 irela->r_info = 0;
10520 irela->r_addend = 0;
10521 continue;
10522 }
10523
10524 irela->r_offset += o->output_offset;
10525
10526 /* Relocs in an executable have to be virtual addresses. */
10527 if (!bfd_link_relocatable (flinfo->info))
10528 irela->r_offset += o->output_section->vma;
10529
10530 last_offset = irela->r_offset;
10531
10532 r_symndx = irela->r_info >> r_sym_shift;
10533 if (r_symndx == STN_UNDEF)
10534 continue;
10535
10536 if (r_symndx >= locsymcount
10537 || (elf_bad_symtab (input_bfd)
10538 && flinfo->sections[r_symndx] == NULL))
10539 {
10540 struct elf_link_hash_entry *rh;
10541 unsigned long indx;
10542
10543 /* This is a reloc against a global symbol. We
10544 have not yet output all the local symbols, so
10545 we do not know the symbol index of any global
10546 symbol. We set the rel_hash entry for this
10547 reloc to point to the global hash table entry
10548 for this symbol. The symbol index is then
10549 set at the end of bfd_elf_final_link. */
10550 indx = r_symndx - extsymoff;
10551 rh = elf_sym_hashes (input_bfd)[indx];
10552 while (rh->root.type == bfd_link_hash_indirect
10553 || rh->root.type == bfd_link_hash_warning)
10554 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10555
10556 /* Setting the index to -2 tells
10557 elf_link_output_extsym that this symbol is
10558 used by a reloc. */
10559 BFD_ASSERT (rh->indx < 0);
10560 rh->indx = -2;
10561
10562 *rel_hash = rh;
10563
10564 continue;
10565 }
10566
10567 /* This is a reloc against a local symbol. */
10568
10569 *rel_hash = NULL;
10570 sym = isymbuf[r_symndx];
10571 sec = flinfo->sections[r_symndx];
10572 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10573 {
10574 /* I suppose the backend ought to fill in the
10575 section of any STT_SECTION symbol against a
10576 processor specific section. */
10577 r_symndx = STN_UNDEF;
10578 if (bfd_is_abs_section (sec))
10579 ;
10580 else if (sec == NULL || sec->owner == NULL)
10581 {
10582 bfd_set_error (bfd_error_bad_value);
10583 return FALSE;
10584 }
10585 else
10586 {
10587 asection *osec = sec->output_section;
10588
10589 /* If we have discarded a section, the output
10590 section will be the absolute section. In
10591 case of discarded SEC_MERGE sections, use
10592 the kept section. relocate_section should
10593 have already handled discarded linkonce
10594 sections. */
10595 if (bfd_is_abs_section (osec)
10596 && sec->kept_section != NULL
10597 && sec->kept_section->output_section != NULL)
10598 {
10599 osec = sec->kept_section->output_section;
10600 irela->r_addend -= osec->vma;
10601 }
10602
10603 if (!bfd_is_abs_section (osec))
10604 {
10605 r_symndx = osec->target_index;
10606 if (r_symndx == STN_UNDEF)
10607 {
10608 irela->r_addend += osec->vma;
10609 osec = _bfd_nearby_section (output_bfd, osec,
10610 osec->vma);
10611 irela->r_addend -= osec->vma;
10612 r_symndx = osec->target_index;
10613 }
10614 }
10615 }
10616
10617 /* Adjust the addend according to where the
10618 section winds up in the output section. */
10619 if (rela_normal)
10620 irela->r_addend += sec->output_offset;
10621 }
10622 else
10623 {
10624 if (flinfo->indices[r_symndx] == -1)
10625 {
10626 unsigned long shlink;
10627 const char *name;
10628 asection *osec;
10629 long indx;
10630
10631 if (flinfo->info->strip == strip_all)
10632 {
10633 /* You can't do ld -r -s. */
10634 bfd_set_error (bfd_error_invalid_operation);
10635 return FALSE;
10636 }
10637
10638 /* This symbol was skipped earlier, but
10639 since it is needed by a reloc, we
10640 must output it now. */
10641 shlink = symtab_hdr->sh_link;
10642 name = (bfd_elf_string_from_elf_section
10643 (input_bfd, shlink, sym.st_name));
10644 if (name == NULL)
10645 return FALSE;
10646
10647 osec = sec->output_section;
10648 sym.st_shndx =
10649 _bfd_elf_section_from_bfd_section (output_bfd,
10650 osec);
10651 if (sym.st_shndx == SHN_BAD)
10652 return FALSE;
10653
10654 sym.st_value += sec->output_offset;
10655 if (!bfd_link_relocatable (flinfo->info))
10656 {
10657 sym.st_value += osec->vma;
10658 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10659 {
10660 /* STT_TLS symbols are relative to PT_TLS
10661 segment base. */
10662 BFD_ASSERT (elf_hash_table (flinfo->info)
10663 ->tls_sec != NULL);
10664 sym.st_value -= (elf_hash_table (flinfo->info)
10665 ->tls_sec->vma);
10666 }
10667 }
10668
10669 indx = bfd_get_symcount (output_bfd);
10670 ret = elf_link_output_symstrtab (flinfo, name,
10671 &sym, sec,
10672 NULL);
10673 if (ret == 0)
10674 return FALSE;
10675 else if (ret == 1)
10676 flinfo->indices[r_symndx] = indx;
10677 else
10678 abort ();
10679 }
10680
10681 r_symndx = flinfo->indices[r_symndx];
10682 }
10683
10684 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10685 | (irela->r_info & r_type_mask));
10686 }
10687
10688 /* Swap out the relocs. */
10689 input_rel_hdr = esdi->rel.hdr;
10690 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10691 {
10692 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10693 input_rel_hdr,
10694 internal_relocs,
10695 rel_hash_list))
10696 return FALSE;
10697 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10698 * bed->s->int_rels_per_ext_rel);
10699 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10700 }
10701
10702 input_rela_hdr = esdi->rela.hdr;
10703 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10704 {
10705 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10706 input_rela_hdr,
10707 internal_relocs,
10708 rela_hash_list))
10709 return FALSE;
10710 }
10711 }
10712 }
10713
10714 /* Write out the modified section contents. */
10715 if (bed->elf_backend_write_section
10716 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10717 contents))
10718 {
10719 /* Section written out. */
10720 }
10721 else switch (o->sec_info_type)
10722 {
10723 case SEC_INFO_TYPE_STABS:
10724 if (! (_bfd_write_section_stabs
10725 (output_bfd,
10726 &elf_hash_table (flinfo->info)->stab_info,
10727 o, &elf_section_data (o)->sec_info, contents)))
10728 return FALSE;
10729 break;
10730 case SEC_INFO_TYPE_MERGE:
10731 if (! _bfd_write_merged_section (output_bfd, o,
10732 elf_section_data (o)->sec_info))
10733 return FALSE;
10734 break;
10735 case SEC_INFO_TYPE_EH_FRAME:
10736 {
10737 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10738 o, contents))
10739 return FALSE;
10740 }
10741 break;
10742 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10743 {
10744 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10745 flinfo->info,
10746 o, contents))
10747 return FALSE;
10748 }
10749 break;
10750 default:
10751 {
10752 if (! (o->flags & SEC_EXCLUDE))
10753 {
10754 file_ptr offset = (file_ptr) o->output_offset;
10755 bfd_size_type todo = o->size;
10756
10757 offset *= bfd_octets_per_byte (output_bfd);
10758
10759 if ((o->flags & SEC_ELF_REVERSE_COPY))
10760 {
10761 /* Reverse-copy input section to output. */
10762 do
10763 {
10764 todo -= address_size;
10765 if (! bfd_set_section_contents (output_bfd,
10766 o->output_section,
10767 contents + todo,
10768 offset,
10769 address_size))
10770 return FALSE;
10771 if (todo == 0)
10772 break;
10773 offset += address_size;
10774 }
10775 while (1);
10776 }
10777 else if (! bfd_set_section_contents (output_bfd,
10778 o->output_section,
10779 contents,
10780 offset, todo))
10781 return FALSE;
10782 }
10783 }
10784 break;
10785 }
10786 }
10787
10788 return TRUE;
10789}
10790
10791/* Generate a reloc when linking an ELF file. This is a reloc
10792 requested by the linker, and does not come from any input file. This
10793 is used to build constructor and destructor tables when linking
10794 with -Ur. */
10795
10796static bfd_boolean
10797elf_reloc_link_order (bfd *output_bfd,
10798 struct bfd_link_info *info,
10799 asection *output_section,
10800 struct bfd_link_order *link_order)
10801{
10802 reloc_howto_type *howto;
10803 long indx;
10804 bfd_vma offset;
10805 bfd_vma addend;
10806 struct bfd_elf_section_reloc_data *reldata;
10807 struct elf_link_hash_entry **rel_hash_ptr;
10808 Elf_Internal_Shdr *rel_hdr;
10809 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10810 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10811 bfd_byte *erel;
10812 unsigned int i;
10813 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10814
10815 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10816 if (howto == NULL)
10817 {
10818 bfd_set_error (bfd_error_bad_value);
10819 return FALSE;
10820 }
10821
10822 addend = link_order->u.reloc.p->addend;
10823
10824 if (esdo->rel.hdr)
10825 reldata = &esdo->rel;
10826 else if (esdo->rela.hdr)
10827 reldata = &esdo->rela;
10828 else
10829 {
10830 reldata = NULL;
10831 BFD_ASSERT (0);
10832 }
10833
10834 /* Figure out the symbol index. */
10835 rel_hash_ptr = reldata->hashes + reldata->count;
10836 if (link_order->type == bfd_section_reloc_link_order)
10837 {
10838 indx = link_order->u.reloc.p->u.section->target_index;
10839 BFD_ASSERT (indx != 0);
10840 *rel_hash_ptr = NULL;
10841 }
10842 else
10843 {
10844 struct elf_link_hash_entry *h;
10845
10846 /* Treat a reloc against a defined symbol as though it were
10847 actually against the section. */
10848 h = ((struct elf_link_hash_entry *)
10849 bfd_wrapped_link_hash_lookup (output_bfd, info,
10850 link_order->u.reloc.p->u.name,
10851 FALSE, FALSE, TRUE));
10852 if (h != NULL
10853 && (h->root.type == bfd_link_hash_defined
10854 || h->root.type == bfd_link_hash_defweak))
10855 {
10856 asection *section;
10857
10858 section = h->root.u.def.section;
10859 indx = section->output_section->target_index;
10860 *rel_hash_ptr = NULL;
10861 /* It seems that we ought to add the symbol value to the
10862 addend here, but in practice it has already been added
10863 because it was passed to constructor_callback. */
10864 addend += section->output_section->vma + section->output_offset;
10865 }
10866 else if (h != NULL)
10867 {
10868 /* Setting the index to -2 tells elf_link_output_extsym that
10869 this symbol is used by a reloc. */
10870 h->indx = -2;
10871 *rel_hash_ptr = h;
10872 indx = 0;
10873 }
10874 else
10875 {
10876 (*info->callbacks->unattached_reloc)
10877 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10878 indx = 0;
10879 }
10880 }
10881
10882 /* If this is an inplace reloc, we must write the addend into the
10883 object file. */
10884 if (howto->partial_inplace && addend != 0)
10885 {
10886 bfd_size_type size;
10887 bfd_reloc_status_type rstat;
10888 bfd_byte *buf;
10889 bfd_boolean ok;
10890 const char *sym_name;
10891
10892 size = (bfd_size_type) bfd_get_reloc_size (howto);
10893 buf = (bfd_byte *) bfd_zmalloc (size);
10894 if (buf == NULL && size != 0)
10895 return FALSE;
10896 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10897 switch (rstat)
10898 {
10899 case bfd_reloc_ok:
10900 break;
10901
10902 default:
10903 case bfd_reloc_outofrange:
10904 abort ();
10905
10906 case bfd_reloc_overflow:
10907 if (link_order->type == bfd_section_reloc_link_order)
10908 sym_name = bfd_section_name (output_bfd,
10909 link_order->u.reloc.p->u.section);
10910 else
10911 sym_name = link_order->u.reloc.p->u.name;
10912 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10913 howto->name, addend, NULL, NULL,
10914 (bfd_vma) 0);
10915 break;
10916 }
10917
10918 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10919 link_order->offset
10920 * bfd_octets_per_byte (output_bfd),
10921 size);
10922 free (buf);
10923 if (! ok)
10924 return FALSE;
10925 }
10926
10927 /* The address of a reloc is relative to the section in a
10928 relocatable file, and is a virtual address in an executable
10929 file. */
10930 offset = link_order->offset;
10931 if (! bfd_link_relocatable (info))
10932 offset += output_section->vma;
10933
10934 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10935 {
10936 irel[i].r_offset = offset;
10937 irel[i].r_info = 0;
10938 irel[i].r_addend = 0;
10939 }
10940 if (bed->s->arch_size == 32)
10941 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10942 else
10943 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10944
10945 rel_hdr = reldata->hdr;
10946 erel = rel_hdr->contents;
10947 if (rel_hdr->sh_type == SHT_REL)
10948 {
10949 erel += reldata->count * bed->s->sizeof_rel;
10950 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10951 }
10952 else
10953 {
10954 irel[0].r_addend = addend;
10955 erel += reldata->count * bed->s->sizeof_rela;
10956 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10957 }
10958
10959 ++reldata->count;
10960
10961 return TRUE;
10962}
10963
10964
10965/* Get the output vma of the section pointed to by the sh_link field. */
10966
10967static bfd_vma
10968elf_get_linked_section_vma (struct bfd_link_order *p)
10969{
10970 Elf_Internal_Shdr **elf_shdrp;
10971 asection *s;
10972 int elfsec;
10973
10974 s = p->u.indirect.section;
10975 elf_shdrp = elf_elfsections (s->owner);
10976 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10977 elfsec = elf_shdrp[elfsec]->sh_link;
10978 /* PR 290:
10979 The Intel C compiler generates SHT_IA_64_UNWIND with
10980 SHF_LINK_ORDER. But it doesn't set the sh_link or
10981 sh_info fields. Hence we could get the situation
10982 where elfsec is 0. */
10983 if (elfsec == 0)
10984 {
10985 const struct elf_backend_data *bed
10986 = get_elf_backend_data (s->owner);
10987 if (bed->link_order_error_handler)
10988 bed->link_order_error_handler
10989 /* xgettext:c-format */
10990 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10991 return 0;
10992 }
10993 else
10994 {
10995 s = elf_shdrp[elfsec]->bfd_section;
10996 return s->output_section->vma + s->output_offset;
10997 }
10998}
10999
11000
11001/* Compare two sections based on the locations of the sections they are
11002 linked to. Used by elf_fixup_link_order. */
11003
11004static int
11005compare_link_order (const void * a, const void * b)
11006{
11007 bfd_vma apos;
11008 bfd_vma bpos;
11009
11010 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11011 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11012 if (apos < bpos)
11013 return -1;
11014 return apos > bpos;
11015}
11016
11017
11018/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11019 order as their linked sections. Returns false if this could not be done
11020 because an output section includes both ordered and unordered
11021 sections. Ideally we'd do this in the linker proper. */
11022
11023static bfd_boolean
11024elf_fixup_link_order (bfd *abfd, asection *o)
11025{
11026 int seen_linkorder;
11027 int seen_other;
11028 int n;
11029 struct bfd_link_order *p;
11030 bfd *sub;
11031 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11032 unsigned elfsec;
11033 struct bfd_link_order **sections;
11034 asection *s, *other_sec, *linkorder_sec;
11035 bfd_vma offset;
11036
11037 other_sec = NULL;
11038 linkorder_sec = NULL;
11039 seen_other = 0;
11040 seen_linkorder = 0;
11041 for (p = o->map_head.link_order; p != NULL; p = p->next)
11042 {
11043 if (p->type == bfd_indirect_link_order)
11044 {
11045 s = p->u.indirect.section;
11046 sub = s->owner;
11047 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11048 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11049 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11050 && elfsec < elf_numsections (sub)
11051 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11052 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11053 {
11054 seen_linkorder++;
11055 linkorder_sec = s;
11056 }
11057 else
11058 {
11059 seen_other++;
11060 other_sec = s;
11061 }
11062 }
11063 else
11064 seen_other++;
11065
11066 if (seen_other && seen_linkorder)
11067 {
11068 if (other_sec && linkorder_sec)
11069 _bfd_error_handler
11070 /* xgettext:c-format */
11071 (_("%A has both ordered [`%A' in %B] "
11072 "and unordered [`%A' in %B] sections"),
11073 o, linkorder_sec,
11074 linkorder_sec->owner, other_sec,
11075 other_sec->owner);
11076 else
11077 _bfd_error_handler
11078 (_("%A has both ordered and unordered sections"), o);
11079 bfd_set_error (bfd_error_bad_value);
11080 return FALSE;
11081 }
11082 }
11083
11084 if (!seen_linkorder)
11085 return TRUE;
11086
11087 sections = (struct bfd_link_order **)
11088 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11089 if (sections == NULL)
11090 return FALSE;
11091 seen_linkorder = 0;
11092
11093 for (p = o->map_head.link_order; p != NULL; p = p->next)
11094 {
11095 sections[seen_linkorder++] = p;
11096 }
11097 /* Sort the input sections in the order of their linked section. */
11098 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11099 compare_link_order);
11100
11101 /* Change the offsets of the sections. */
11102 offset = 0;
11103 for (n = 0; n < seen_linkorder; n++)
11104 {
11105 s = sections[n]->u.indirect.section;
11106 offset &= ~(bfd_vma) 0 << s->alignment_power;
11107 s->output_offset = offset / bfd_octets_per_byte (abfd);
11108 sections[n]->offset = offset;
11109 offset += sections[n]->size;
11110 }
11111
11112 free (sections);
11113 return TRUE;
11114}
11115
11116/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11117 Returns TRUE upon success, FALSE otherwise. */
11118
11119static bfd_boolean
11120elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11121{
11122 bfd_boolean ret = FALSE;
11123 bfd *implib_bfd;
11124 const struct elf_backend_data *bed;
11125 flagword flags;
11126 enum bfd_architecture arch;
11127 unsigned int mach;
11128 asymbol **sympp = NULL;
11129 long symsize;
11130 long symcount;
11131 long src_count;
11132 elf_symbol_type *osymbuf;
11133
11134 implib_bfd = info->out_implib_bfd;
11135 bed = get_elf_backend_data (abfd);
11136
11137 if (!bfd_set_format (implib_bfd, bfd_object))
11138 return FALSE;
11139
11140 flags = bfd_get_file_flags (abfd);
11141 flags &= ~HAS_RELOC;
11142 if (!bfd_set_start_address (implib_bfd, 0)
11143 || !bfd_set_file_flags (implib_bfd, flags))
11144 return FALSE;
11145
11146 /* Copy architecture of output file to import library file. */
11147 arch = bfd_get_arch (abfd);
11148 mach = bfd_get_mach (abfd);
11149 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11150 && (abfd->target_defaulted
11151 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11152 return FALSE;
11153
11154 /* Get symbol table size. */
11155 symsize = bfd_get_symtab_upper_bound (abfd);
11156 if (symsize < 0)
11157 return FALSE;
11158
11159 /* Read in the symbol table. */
11160 sympp = (asymbol **) xmalloc (symsize);
11161 symcount = bfd_canonicalize_symtab (abfd, sympp);
11162 if (symcount < 0)
11163 goto free_sym_buf;
11164
11165 /* Allow the BFD backend to copy any private header data it
11166 understands from the output BFD to the import library BFD. */
11167 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11168 goto free_sym_buf;
11169
11170 /* Filter symbols to appear in the import library. */
11171 if (bed->elf_backend_filter_implib_symbols)
11172 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11173 symcount);
11174 else
11175 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11176 if (symcount == 0)
11177 {
11178 bfd_set_error (bfd_error_no_symbols);
11179 _bfd_error_handler (_("%B: no symbol found for import library"),
11180 implib_bfd);
11181 goto free_sym_buf;
11182 }
11183
11184
11185 /* Make symbols absolute. */
11186 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11187 sizeof (*osymbuf));
11188 for (src_count = 0; src_count < symcount; src_count++)
11189 {
11190 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11191 sizeof (*osymbuf));
11192 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11193 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11194 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11195 osymbuf[src_count].internal_elf_sym.st_value =
11196 osymbuf[src_count].symbol.value;
11197 sympp[src_count] = &osymbuf[src_count].symbol;
11198 }
11199
11200 bfd_set_symtab (implib_bfd, sympp, symcount);
11201
11202 /* Allow the BFD backend to copy any private data it understands
11203 from the output BFD to the import library BFD. This is done last
11204 to permit the routine to look at the filtered symbol table. */
11205 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11206 goto free_sym_buf;
11207
11208 if (!bfd_close (implib_bfd))
11209 goto free_sym_buf;
11210
11211 ret = TRUE;
11212
11213free_sym_buf:
11214 free (sympp);
11215 return ret;
11216}
11217
11218static void
11219elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11220{
11221 asection *o;
11222
11223 if (flinfo->symstrtab != NULL)
11224 _bfd_elf_strtab_free (flinfo->symstrtab);
11225 if (flinfo->contents != NULL)
11226 free (flinfo->contents);
11227 if (flinfo->external_relocs != NULL)
11228 free (flinfo->external_relocs);
11229 if (flinfo->internal_relocs != NULL)
11230 free (flinfo->internal_relocs);
11231 if (flinfo->external_syms != NULL)
11232 free (flinfo->external_syms);
11233 if (flinfo->locsym_shndx != NULL)
11234 free (flinfo->locsym_shndx);
11235 if (flinfo->internal_syms != NULL)
11236 free (flinfo->internal_syms);
11237 if (flinfo->indices != NULL)
11238 free (flinfo->indices);
11239 if (flinfo->sections != NULL)
11240 free (flinfo->sections);
11241 if (flinfo->symshndxbuf != NULL)
11242 free (flinfo->symshndxbuf);
11243 for (o = obfd->sections; o != NULL; o = o->next)
11244 {
11245 struct bfd_elf_section_data *esdo = elf_section_data (o);
11246 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11247 free (esdo->rel.hashes);
11248 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11249 free (esdo->rela.hashes);
11250 }
11251}
11252
11253/* Do the final step of an ELF link. */
11254
11255bfd_boolean
11256bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11257{
11258 bfd_boolean dynamic;
11259 bfd_boolean emit_relocs;
11260 bfd *dynobj;
11261 struct elf_final_link_info flinfo;
11262 asection *o;
11263 struct bfd_link_order *p;
11264 bfd *sub;
11265 bfd_size_type max_contents_size;
11266 bfd_size_type max_external_reloc_size;
11267 bfd_size_type max_internal_reloc_count;
11268 bfd_size_type max_sym_count;
11269 bfd_size_type max_sym_shndx_count;
11270 Elf_Internal_Sym elfsym;
11271 unsigned int i;
11272 Elf_Internal_Shdr *symtab_hdr;
11273 Elf_Internal_Shdr *symtab_shndx_hdr;
11274 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11275 struct elf_outext_info eoinfo;
11276 bfd_boolean merged;
11277 size_t relativecount = 0;
11278 asection *reldyn = 0;
11279 bfd_size_type amt;
11280 asection *attr_section = NULL;
11281 bfd_vma attr_size = 0;
11282 const char *std_attrs_section;
11283 struct elf_link_hash_table *htab = elf_hash_table (info);
11284
11285 if (!is_elf_hash_table (htab))
11286 return FALSE;
11287
11288 if (bfd_link_pic (info))
11289 abfd->flags |= DYNAMIC;
11290
11291 dynamic = htab->dynamic_sections_created;
11292 dynobj = htab->dynobj;
11293
11294 emit_relocs = (bfd_link_relocatable (info)
11295 || info->emitrelocations);
11296
11297 flinfo.info = info;
11298 flinfo.output_bfd = abfd;
11299 flinfo.symstrtab = _bfd_elf_strtab_init ();
11300 if (flinfo.symstrtab == NULL)
11301 return FALSE;
11302
11303 if (! dynamic)
11304 {
11305 flinfo.hash_sec = NULL;
11306 flinfo.symver_sec = NULL;
11307 }
11308 else
11309 {
11310 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11311 /* Note that dynsym_sec can be NULL (on VMS). */
11312 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11313 /* Note that it is OK if symver_sec is NULL. */
11314 }
11315
11316 flinfo.contents = NULL;
11317 flinfo.external_relocs = NULL;
11318 flinfo.internal_relocs = NULL;
11319 flinfo.external_syms = NULL;
11320 flinfo.locsym_shndx = NULL;
11321 flinfo.internal_syms = NULL;
11322 flinfo.indices = NULL;
11323 flinfo.sections = NULL;
11324 flinfo.symshndxbuf = NULL;
11325 flinfo.filesym_count = 0;
11326
11327 /* The object attributes have been merged. Remove the input
11328 sections from the link, and set the contents of the output
11329 secton. */
11330 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11331 for (o = abfd->sections; o != NULL; o = o->next)
11332 {
11333 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11334 || strcmp (o->name, ".gnu.attributes") == 0)
11335 {
11336 for (p = o->map_head.link_order; p != NULL; p = p->next)
11337 {
11338 asection *input_section;
11339
11340 if (p->type != bfd_indirect_link_order)
11341 continue;
11342 input_section = p->u.indirect.section;
11343 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11344 elf_link_input_bfd ignores this section. */
11345 input_section->flags &= ~SEC_HAS_CONTENTS;
11346 }
11347
11348 attr_size = bfd_elf_obj_attr_size (abfd);
11349 if (attr_size)
11350 {
11351 bfd_set_section_size (abfd, o, attr_size);
11352 attr_section = o;
11353 /* Skip this section later on. */
11354 o->map_head.link_order = NULL;
11355 }
11356 else
11357 o->flags |= SEC_EXCLUDE;
11358 }
11359 }
11360
11361 /* Count up the number of relocations we will output for each output
11362 section, so that we know the sizes of the reloc sections. We
11363 also figure out some maximum sizes. */
11364 max_contents_size = 0;
11365 max_external_reloc_size = 0;
11366 max_internal_reloc_count = 0;
11367 max_sym_count = 0;
11368 max_sym_shndx_count = 0;
11369 merged = FALSE;
11370 for (o = abfd->sections; o != NULL; o = o->next)
11371 {
11372 struct bfd_elf_section_data *esdo = elf_section_data (o);
11373 o->reloc_count = 0;
11374
11375 for (p = o->map_head.link_order; p != NULL; p = p->next)
11376 {
11377 unsigned int reloc_count = 0;
11378 unsigned int additional_reloc_count = 0;
11379 struct bfd_elf_section_data *esdi = NULL;
11380
11381 if (p->type == bfd_section_reloc_link_order
11382 || p->type == bfd_symbol_reloc_link_order)
11383 reloc_count = 1;
11384 else if (p->type == bfd_indirect_link_order)
11385 {
11386 asection *sec;
11387
11388 sec = p->u.indirect.section;
11389
11390 /* Mark all sections which are to be included in the
11391 link. This will normally be every section. We need
11392 to do this so that we can identify any sections which
11393 the linker has decided to not include. */
11394 sec->linker_mark = TRUE;
11395
11396 if (sec->flags & SEC_MERGE)
11397 merged = TRUE;
11398
11399 if (sec->rawsize > max_contents_size)
11400 max_contents_size = sec->rawsize;
11401 if (sec->size > max_contents_size)
11402 max_contents_size = sec->size;
11403
11404 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11405 && (sec->owner->flags & DYNAMIC) == 0)
11406 {
11407 size_t sym_count;
11408
11409 /* We are interested in just local symbols, not all
11410 symbols. */
11411 if (elf_bad_symtab (sec->owner))
11412 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11413 / bed->s->sizeof_sym);
11414 else
11415 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11416
11417 if (sym_count > max_sym_count)
11418 max_sym_count = sym_count;
11419
11420 if (sym_count > max_sym_shndx_count
11421 && elf_symtab_shndx_list (sec->owner) != NULL)
11422 max_sym_shndx_count = sym_count;
11423
11424 if (esdo->this_hdr.sh_type == SHT_REL
11425 || esdo->this_hdr.sh_type == SHT_RELA)
11426 /* Some backends use reloc_count in relocation sections
11427 to count particular types of relocs. Of course,
11428 reloc sections themselves can't have relocations. */
11429 ;
11430 else if (emit_relocs)
11431 {
11432 reloc_count = sec->reloc_count;
11433 if (bed->elf_backend_count_additional_relocs)
11434 {
11435 int c;
11436 c = (*bed->elf_backend_count_additional_relocs) (sec);
11437 additional_reloc_count += c;
11438 }
11439 }
11440 else if (bed->elf_backend_count_relocs)
11441 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11442
11443 esdi = elf_section_data (sec);
11444
11445 if ((sec->flags & SEC_RELOC) != 0)
11446 {
11447 size_t ext_size = 0;
11448
11449 if (esdi->rel.hdr != NULL)
11450 ext_size = esdi->rel.hdr->sh_size;
11451 if (esdi->rela.hdr != NULL)
11452 ext_size += esdi->rela.hdr->sh_size;
11453
11454 if (ext_size > max_external_reloc_size)
11455 max_external_reloc_size = ext_size;
11456 if (sec->reloc_count > max_internal_reloc_count)
11457 max_internal_reloc_count = sec->reloc_count;
11458 }
11459 }
11460 }
11461
11462 if (reloc_count == 0)
11463 continue;
11464
11465 reloc_count += additional_reloc_count;
11466 o->reloc_count += reloc_count;
11467
11468 if (p->type == bfd_indirect_link_order && emit_relocs)
11469 {
11470 if (esdi->rel.hdr)
11471 {
11472 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11473 esdo->rel.count += additional_reloc_count;
11474 }
11475 if (esdi->rela.hdr)
11476 {
11477 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11478 esdo->rela.count += additional_reloc_count;
11479 }
11480 }
11481 else
11482 {
11483 if (o->use_rela_p)
11484 esdo->rela.count += reloc_count;
11485 else
11486 esdo->rel.count += reloc_count;
11487 }
11488 }
11489
11490 if (o->reloc_count > 0)
11491 o->flags |= SEC_RELOC;
11492 else
11493 {
11494 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11495 set it (this is probably a bug) and if it is set
11496 assign_section_numbers will create a reloc section. */
11497 o->flags &=~ SEC_RELOC;
11498 }
11499
11500 /* If the SEC_ALLOC flag is not set, force the section VMA to
11501 zero. This is done in elf_fake_sections as well, but forcing
11502 the VMA to 0 here will ensure that relocs against these
11503 sections are handled correctly. */
11504 if ((o->flags & SEC_ALLOC) == 0
11505 && ! o->user_set_vma)
11506 o->vma = 0;
11507 }
11508
11509 if (! bfd_link_relocatable (info) && merged)
11510 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11511
11512 /* Figure out the file positions for everything but the symbol table
11513 and the relocs. We set symcount to force assign_section_numbers
11514 to create a symbol table. */
11515 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11516 BFD_ASSERT (! abfd->output_has_begun);
11517 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11518 goto error_return;
11519
11520 /* Set sizes, and assign file positions for reloc sections. */
11521 for (o = abfd->sections; o != NULL; o = o->next)
11522 {
11523 struct bfd_elf_section_data *esdo = elf_section_data (o);
11524 if ((o->flags & SEC_RELOC) != 0)
11525 {
11526 if (esdo->rel.hdr
11527 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11528 goto error_return;
11529
11530 if (esdo->rela.hdr
11531 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11532 goto error_return;
11533 }
11534
11535 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11536 to count upwards while actually outputting the relocations. */
11537 esdo->rel.count = 0;
11538 esdo->rela.count = 0;
11539
11540 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11541 {
11542 /* Cache the section contents so that they can be compressed
11543 later. Use bfd_malloc since it will be freed by
11544 bfd_compress_section_contents. */
11545 unsigned char *contents = esdo->this_hdr.contents;
11546 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11547 abort ();
11548 contents
11549 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11550 if (contents == NULL)
11551 goto error_return;
11552 esdo->this_hdr.contents = contents;
11553 }
11554 }
11555
11556 /* We have now assigned file positions for all the sections except
11557 .symtab, .strtab, and non-loaded reloc sections. We start the
11558 .symtab section at the current file position, and write directly
11559 to it. We build the .strtab section in memory. */
11560 bfd_get_symcount (abfd) = 0;
11561 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11562 /* sh_name is set in prep_headers. */
11563 symtab_hdr->sh_type = SHT_SYMTAB;
11564 /* sh_flags, sh_addr and sh_size all start off zero. */
11565 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11566 /* sh_link is set in assign_section_numbers. */
11567 /* sh_info is set below. */
11568 /* sh_offset is set just below. */
11569 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11570
11571 if (max_sym_count < 20)
11572 max_sym_count = 20;
11573 htab->strtabsize = max_sym_count;
11574 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11575 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11576 if (htab->strtab == NULL)
11577 goto error_return;
11578 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11579 flinfo.symshndxbuf
11580 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11581 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11582
11583 if (info->strip != strip_all || emit_relocs)
11584 {
11585 file_ptr off = elf_next_file_pos (abfd);
11586
11587 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11588
11589 /* Note that at this point elf_next_file_pos (abfd) is
11590 incorrect. We do not yet know the size of the .symtab section.
11591 We correct next_file_pos below, after we do know the size. */
11592
11593 /* Start writing out the symbol table. The first symbol is always a
11594 dummy symbol. */
11595 elfsym.st_value = 0;
11596 elfsym.st_size = 0;
11597 elfsym.st_info = 0;
11598 elfsym.st_other = 0;
11599 elfsym.st_shndx = SHN_UNDEF;
11600 elfsym.st_target_internal = 0;
11601 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11602 bfd_und_section_ptr, NULL) != 1)
11603 goto error_return;
11604
11605 /* Output a symbol for each section. We output these even if we are
11606 discarding local symbols, since they are used for relocs. These
11607 symbols have no names. We store the index of each one in the
11608 index field of the section, so that we can find it again when
11609 outputting relocs. */
11610
11611 elfsym.st_size = 0;
11612 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11613 elfsym.st_other = 0;
11614 elfsym.st_value = 0;
11615 elfsym.st_target_internal = 0;
11616 for (i = 1; i < elf_numsections (abfd); i++)
11617 {
11618 o = bfd_section_from_elf_index (abfd, i);
11619 if (o != NULL)
11620 {
11621 o->target_index = bfd_get_symcount (abfd);
11622 elfsym.st_shndx = i;
11623 if (!bfd_link_relocatable (info))
11624 elfsym.st_value = o->vma;
11625 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11626 NULL) != 1)
11627 goto error_return;
11628 }
11629 }
11630 }
11631
11632 /* Allocate some memory to hold information read in from the input
11633 files. */
11634 if (max_contents_size != 0)
11635 {
11636 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11637 if (flinfo.contents == NULL)
11638 goto error_return;
11639 }
11640
11641 if (max_external_reloc_size != 0)
11642 {
11643 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11644 if (flinfo.external_relocs == NULL)
11645 goto error_return;
11646 }
11647
11648 if (max_internal_reloc_count != 0)
11649 {
11650 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11651 amt *= sizeof (Elf_Internal_Rela);
11652 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11653 if (flinfo.internal_relocs == NULL)
11654 goto error_return;
11655 }
11656
11657 if (max_sym_count != 0)
11658 {
11659 amt = max_sym_count * bed->s->sizeof_sym;
11660 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11661 if (flinfo.external_syms == NULL)
11662 goto error_return;
11663
11664 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11665 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11666 if (flinfo.internal_syms == NULL)
11667 goto error_return;
11668
11669 amt = max_sym_count * sizeof (long);
11670 flinfo.indices = (long int *) bfd_malloc (amt);
11671 if (flinfo.indices == NULL)
11672 goto error_return;
11673
11674 amt = max_sym_count * sizeof (asection *);
11675 flinfo.sections = (asection **) bfd_malloc (amt);
11676 if (flinfo.sections == NULL)
11677 goto error_return;
11678 }
11679
11680 if (max_sym_shndx_count != 0)
11681 {
11682 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11683 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11684 if (flinfo.locsym_shndx == NULL)
11685 goto error_return;
11686 }
11687
11688 if (htab->tls_sec)
11689 {
11690 bfd_vma base, end = 0;
11691 asection *sec;
11692
11693 for (sec = htab->tls_sec;
11694 sec && (sec->flags & SEC_THREAD_LOCAL);
11695 sec = sec->next)
11696 {
11697 bfd_size_type size = sec->size;
11698
11699 if (size == 0
11700 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11701 {
11702 struct bfd_link_order *ord = sec->map_tail.link_order;
11703
11704 if (ord != NULL)
11705 size = ord->offset + ord->size;
11706 }
11707 end = sec->vma + size;
11708 }
11709 base = htab->tls_sec->vma;
11710 /* Only align end of TLS section if static TLS doesn't have special
11711 alignment requirements. */
11712 if (bed->static_tls_alignment == 1)
11713 end = align_power (end, htab->tls_sec->alignment_power);
11714 htab->tls_size = end - base;
11715 }
11716
11717 /* Reorder SHF_LINK_ORDER sections. */
11718 for (o = abfd->sections; o != NULL; o = o->next)
11719 {
11720 if (!elf_fixup_link_order (abfd, o))
11721 return FALSE;
11722 }
11723
11724 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11725 return FALSE;
11726
11727 /* Since ELF permits relocations to be against local symbols, we
11728 must have the local symbols available when we do the relocations.
11729 Since we would rather only read the local symbols once, and we
11730 would rather not keep them in memory, we handle all the
11731 relocations for a single input file at the same time.
11732
11733 Unfortunately, there is no way to know the total number of local
11734 symbols until we have seen all of them, and the local symbol
11735 indices precede the global symbol indices. This means that when
11736 we are generating relocatable output, and we see a reloc against
11737 a global symbol, we can not know the symbol index until we have
11738 finished examining all the local symbols to see which ones we are
11739 going to output. To deal with this, we keep the relocations in
11740 memory, and don't output them until the end of the link. This is
11741 an unfortunate waste of memory, but I don't see a good way around
11742 it. Fortunately, it only happens when performing a relocatable
11743 link, which is not the common case. FIXME: If keep_memory is set
11744 we could write the relocs out and then read them again; I don't
11745 know how bad the memory loss will be. */
11746
11747 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11748 sub->output_has_begun = FALSE;
11749 for (o = abfd->sections; o != NULL; o = o->next)
11750 {
11751 for (p = o->map_head.link_order; p != NULL; p = p->next)
11752 {
11753 if (p->type == bfd_indirect_link_order
11754 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11755 == bfd_target_elf_flavour)
11756 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11757 {
11758 if (! sub->output_has_begun)
11759 {
11760 if (! elf_link_input_bfd (&flinfo, sub))
11761 goto error_return;
11762 sub->output_has_begun = TRUE;
11763 }
11764 }
11765 else if (p->type == bfd_section_reloc_link_order
11766 || p->type == bfd_symbol_reloc_link_order)
11767 {
11768 if (! elf_reloc_link_order (abfd, info, o, p))
11769 goto error_return;
11770 }
11771 else
11772 {
11773 if (! _bfd_default_link_order (abfd, info, o, p))
11774 {
11775 if (p->type == bfd_indirect_link_order
11776 && (bfd_get_flavour (sub)
11777 == bfd_target_elf_flavour)
11778 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11779 != bed->s->elfclass))
11780 {
11781 const char *iclass, *oclass;
11782
11783 switch (bed->s->elfclass)
11784 {
11785 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11786 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11787 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11788 default: abort ();
11789 }
11790
11791 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11792 {
11793 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11794 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11795 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11796 default: abort ();
11797 }
11798
11799 bfd_set_error (bfd_error_wrong_format);
11800 _bfd_error_handler
11801 /* xgettext:c-format */
11802 (_("%B: file class %s incompatible with %s"),
11803 sub, iclass, oclass);
11804 }
11805
11806 goto error_return;
11807 }
11808 }
11809 }
11810 }
11811
11812 /* Free symbol buffer if needed. */
11813 if (!info->reduce_memory_overheads)
11814 {
11815 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11816 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11817 && elf_tdata (sub)->symbuf)
11818 {
11819 free (elf_tdata (sub)->symbuf);
11820 elf_tdata (sub)->symbuf = NULL;
11821 }
11822 }
11823
11824 /* Output any global symbols that got converted to local in a
11825 version script or due to symbol visibility. We do this in a
11826 separate step since ELF requires all local symbols to appear
11827 prior to any global symbols. FIXME: We should only do this if
11828 some global symbols were, in fact, converted to become local.
11829 FIXME: Will this work correctly with the Irix 5 linker? */
11830 eoinfo.failed = FALSE;
11831 eoinfo.flinfo = &flinfo;
11832 eoinfo.localsyms = TRUE;
11833 eoinfo.file_sym_done = FALSE;
11834 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11835 if (eoinfo.failed)
11836 return FALSE;
11837
11838 /* If backend needs to output some local symbols not present in the hash
11839 table, do it now. */
11840 if (bed->elf_backend_output_arch_local_syms
11841 && (info->strip != strip_all || emit_relocs))
11842 {
11843 typedef int (*out_sym_func)
11844 (void *, const char *, Elf_Internal_Sym *, asection *,
11845 struct elf_link_hash_entry *);
11846
11847 if (! ((*bed->elf_backend_output_arch_local_syms)
11848 (abfd, info, &flinfo,
11849 (out_sym_func) elf_link_output_symstrtab)))
11850 return FALSE;
11851 }
11852
11853 /* That wrote out all the local symbols. Finish up the symbol table
11854 with the global symbols. Even if we want to strip everything we
11855 can, we still need to deal with those global symbols that got
11856 converted to local in a version script. */
11857
11858 /* The sh_info field records the index of the first non local symbol. */
11859 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11860
11861 if (dynamic
11862 && htab->dynsym != NULL
11863 && htab->dynsym->output_section != bfd_abs_section_ptr)
11864 {
11865 Elf_Internal_Sym sym;
11866 bfd_byte *dynsym = htab->dynsym->contents;
11867
11868 o = htab->dynsym->output_section;
11869 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11870
11871 /* Write out the section symbols for the output sections. */
11872 if (bfd_link_pic (info)
11873 || htab->is_relocatable_executable)
11874 {
11875 asection *s;
11876
11877 sym.st_size = 0;
11878 sym.st_name = 0;
11879 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11880 sym.st_other = 0;
11881 sym.st_target_internal = 0;
11882
11883 for (s = abfd->sections; s != NULL; s = s->next)
11884 {
11885 int indx;
11886 bfd_byte *dest;
11887 long dynindx;
11888
11889 dynindx = elf_section_data (s)->dynindx;
11890 if (dynindx <= 0)
11891 continue;
11892 indx = elf_section_data (s)->this_idx;
11893 BFD_ASSERT (indx > 0);
11894 sym.st_shndx = indx;
11895 if (! check_dynsym (abfd, &sym))
11896 return FALSE;
11897 sym.st_value = s->vma;
11898 dest = dynsym + dynindx * bed->s->sizeof_sym;
11899 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11900 }
11901 }
11902
11903 /* Write out the local dynsyms. */
11904 if (htab->dynlocal)
11905 {
11906 struct elf_link_local_dynamic_entry *e;
11907 for (e = htab->dynlocal; e ; e = e->next)
11908 {
11909 asection *s;
11910 bfd_byte *dest;
11911
11912 /* Copy the internal symbol and turn off visibility.
11913 Note that we saved a word of storage and overwrote
11914 the original st_name with the dynstr_index. */
11915 sym = e->isym;
11916 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11917
11918 s = bfd_section_from_elf_index (e->input_bfd,
11919 e->isym.st_shndx);
11920 if (s != NULL)
11921 {
11922 sym.st_shndx =
11923 elf_section_data (s->output_section)->this_idx;
11924 if (! check_dynsym (abfd, &sym))
11925 return FALSE;
11926 sym.st_value = (s->output_section->vma
11927 + s->output_offset
11928 + e->isym.st_value);
11929 }
11930
11931 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11932 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11933 }
11934 }
11935 }
11936
11937 /* We get the global symbols from the hash table. */
11938 eoinfo.failed = FALSE;
11939 eoinfo.localsyms = FALSE;
11940 eoinfo.flinfo = &flinfo;
11941 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11942 if (eoinfo.failed)
11943 return FALSE;
11944
11945 /* If backend needs to output some symbols not present in the hash
11946 table, do it now. */
11947 if (bed->elf_backend_output_arch_syms
11948 && (info->strip != strip_all || emit_relocs))
11949 {
11950 typedef int (*out_sym_func)
11951 (void *, const char *, Elf_Internal_Sym *, asection *,
11952 struct elf_link_hash_entry *);
11953
11954 if (! ((*bed->elf_backend_output_arch_syms)
11955 (abfd, info, &flinfo,
11956 (out_sym_func) elf_link_output_symstrtab)))
11957 return FALSE;
11958 }
11959
11960 /* Finalize the .strtab section. */
11961 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11962
11963 /* Swap out the .strtab section. */
11964 if (!elf_link_swap_symbols_out (&flinfo))
11965 return FALSE;
11966
11967 /* Now we know the size of the symtab section. */
11968 if (bfd_get_symcount (abfd) > 0)
11969 {
11970 /* Finish up and write out the symbol string table (.strtab)
11971 section. */
11972 Elf_Internal_Shdr *symstrtab_hdr;
11973 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11974
11975 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11976 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11977 {
11978 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11979 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11980 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11981 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11982 symtab_shndx_hdr->sh_size = amt;
11983
11984 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11985 off, TRUE);
11986
11987 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11988 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11989 return FALSE;
11990 }
11991
11992 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11993 /* sh_name was set in prep_headers. */
11994 symstrtab_hdr->sh_type = SHT_STRTAB;
11995 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11996 symstrtab_hdr->sh_addr = 0;
11997 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11998 symstrtab_hdr->sh_entsize = 0;
11999 symstrtab_hdr->sh_link = 0;
12000 symstrtab_hdr->sh_info = 0;
12001 /* sh_offset is set just below. */
12002 symstrtab_hdr->sh_addralign = 1;
12003
12004 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12005 off, TRUE);
12006 elf_next_file_pos (abfd) = off;
12007
12008 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12009 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12010 return FALSE;
12011 }
12012
12013 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12014 {
12015 _bfd_error_handler (_("%B: failed to generate import library"),
12016 info->out_implib_bfd);
12017 return FALSE;
12018 }
12019
12020 /* Adjust the relocs to have the correct symbol indices. */
12021 for (o = abfd->sections; o != NULL; o = o->next)
12022 {
12023 struct bfd_elf_section_data *esdo = elf_section_data (o);
12024 bfd_boolean sort;
12025 if ((o->flags & SEC_RELOC) == 0)
12026 continue;
12027
12028 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12029 if (esdo->rel.hdr != NULL
12030 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
12031 return FALSE;
12032 if (esdo->rela.hdr != NULL
12033 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
12034 return FALSE;
12035
12036 /* Set the reloc_count field to 0 to prevent write_relocs from
12037 trying to swap the relocs out itself. */
12038 o->reloc_count = 0;
12039 }
12040
12041 if (dynamic && info->combreloc && dynobj != NULL)
12042 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12043
12044 /* If we are linking against a dynamic object, or generating a
12045 shared library, finish up the dynamic linking information. */
12046 if (dynamic)
12047 {
12048 bfd_byte *dyncon, *dynconend;
12049
12050 /* Fix up .dynamic entries. */
12051 o = bfd_get_linker_section (dynobj, ".dynamic");
12052 BFD_ASSERT (o != NULL);
12053
12054 dyncon = o->contents;
12055 dynconend = o->contents + o->size;
12056 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12057 {
12058 Elf_Internal_Dyn dyn;
12059 const char *name;
12060 unsigned int type;
12061
12062 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12063
12064 switch (dyn.d_tag)
12065 {
12066 default:
12067 continue;
12068 case DT_NULL:
12069 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12070 {
12071 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12072 {
12073 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12074 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12075 default: continue;
12076 }
12077 dyn.d_un.d_val = relativecount;
12078 relativecount = 0;
12079 break;
12080 }
12081 continue;
12082
12083 case DT_INIT:
12084 name = info->init_function;
12085 goto get_sym;
12086 case DT_FINI:
12087 name = info->fini_function;
12088 get_sym:
12089 {
12090 struct elf_link_hash_entry *h;
12091
12092 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12093 if (h != NULL
12094 && (h->root.type == bfd_link_hash_defined
12095 || h->root.type == bfd_link_hash_defweak))
12096 {
12097 dyn.d_un.d_ptr = h->root.u.def.value;
12098 o = h->root.u.def.section;
12099 if (o->output_section != NULL)
12100 dyn.d_un.d_ptr += (o->output_section->vma
12101 + o->output_offset);
12102 else
12103 {
12104 /* The symbol is imported from another shared
12105 library and does not apply to this one. */
12106 dyn.d_un.d_ptr = 0;
12107 }
12108 break;
12109 }
12110 }
12111 continue;
12112
12113 case DT_PREINIT_ARRAYSZ:
12114 name = ".preinit_array";
12115 goto get_out_size;
12116 case DT_INIT_ARRAYSZ:
12117 name = ".init_array";
12118 goto get_out_size;
12119 case DT_FINI_ARRAYSZ:
12120 name = ".fini_array";
12121 get_out_size:
12122 o = bfd_get_section_by_name (abfd, name);
12123 if (o == NULL)
12124 {
12125 _bfd_error_handler
12126 (_("could not find section %s"), name);
12127 goto error_return;
12128 }
12129 if (o->size == 0)
12130 _bfd_error_handler
12131 (_("warning: %s section has zero size"), name);
12132 dyn.d_un.d_val = o->size;
12133 break;
12134
12135 case DT_PREINIT_ARRAY:
12136 name = ".preinit_array";
12137 goto get_out_vma;
12138 case DT_INIT_ARRAY:
12139 name = ".init_array";
12140 goto get_out_vma;
12141 case DT_FINI_ARRAY:
12142 name = ".fini_array";
12143 get_out_vma:
12144 o = bfd_get_section_by_name (abfd, name);
12145 goto do_vma;
12146
12147 case DT_HASH:
12148 name = ".hash";
12149 goto get_vma;
12150 case DT_GNU_HASH:
12151 name = ".gnu.hash";
12152 goto get_vma;
12153 case DT_STRTAB:
12154 name = ".dynstr";
12155 goto get_vma;
12156 case DT_SYMTAB:
12157 name = ".dynsym";
12158 goto get_vma;
12159 case DT_VERDEF:
12160 name = ".gnu.version_d";
12161 goto get_vma;
12162 case DT_VERNEED:
12163 name = ".gnu.version_r";
12164 goto get_vma;
12165 case DT_VERSYM:
12166 name = ".gnu.version";
12167 get_vma:
12168 o = bfd_get_linker_section (dynobj, name);
12169 do_vma:
12170 if (o == NULL)
12171 {
12172 _bfd_error_handler
12173 (_("could not find section %s"), name);
12174 goto error_return;
12175 }
12176 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12177 {
12178 _bfd_error_handler
12179 (_("warning: section '%s' is being made into a note"), name);
12180 bfd_set_error (bfd_error_nonrepresentable_section);
12181 goto error_return;
12182 }
12183 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12184 break;
12185
12186 case DT_REL:
12187 case DT_RELA:
12188 case DT_RELSZ:
12189 case DT_RELASZ:
12190 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12191 type = SHT_REL;
12192 else
12193 type = SHT_RELA;
12194 dyn.d_un.d_val = 0;
12195 dyn.d_un.d_ptr = 0;
12196 for (i = 1; i < elf_numsections (abfd); i++)
12197 {
12198 Elf_Internal_Shdr *hdr;
12199
12200 hdr = elf_elfsections (abfd)[i];
12201 if (hdr->sh_type == type
12202 && (hdr->sh_flags & SHF_ALLOC) != 0)
12203 {
12204 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12205 dyn.d_un.d_val += hdr->sh_size;
12206 else
12207 {
12208 if (dyn.d_un.d_ptr == 0
12209 || hdr->sh_addr < dyn.d_un.d_ptr)
12210 dyn.d_un.d_ptr = hdr->sh_addr;
12211 }
12212 }
12213 }
12214 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12215 {
12216 /* Don't count procedure linkage table relocs in the
12217 overall reloc count. */
12218 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12219 dyn.d_un.d_val -= htab->srelplt->size;
12220 /* If .rela.plt is the first .rela section, exclude
12221 it from DT_RELA. */
12222 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12223 + htab->srelplt->output_offset))
12224 dyn.d_un.d_ptr += htab->srelplt->size;
12225 }
12226 break;
12227 }
12228 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12229 }
12230 }
12231
12232 /* If we have created any dynamic sections, then output them. */
12233 if (dynobj != NULL)
12234 {
12235 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12236 goto error_return;
12237
12238 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12239 if (((info->warn_shared_textrel && bfd_link_pic (info))
12240 || info->error_textrel)
12241 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12242 {
12243 bfd_byte *dyncon, *dynconend;
12244
12245 dyncon = o->contents;
12246 dynconend = o->contents + o->size;
12247 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12248 {
12249 Elf_Internal_Dyn dyn;
12250
12251 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12252
12253 if (dyn.d_tag == DT_TEXTREL)
12254 {
12255 if (info->error_textrel)
12256 info->callbacks->einfo
12257 (_("%P%X: read-only segment has dynamic relocations.\n"));
12258 else
12259 info->callbacks->einfo
12260 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12261 break;
12262 }
12263 }
12264 }
12265
12266 for (o = dynobj->sections; o != NULL; o = o->next)
12267 {
12268 if ((o->flags & SEC_HAS_CONTENTS) == 0
12269 || o->size == 0
12270 || o->output_section == bfd_abs_section_ptr)
12271 continue;
12272 if ((o->flags & SEC_LINKER_CREATED) == 0)
12273 {
12274 /* At this point, we are only interested in sections
12275 created by _bfd_elf_link_create_dynamic_sections. */
12276 continue;
12277 }
12278 if (htab->stab_info.stabstr == o)
12279 continue;
12280 if (htab->eh_info.hdr_sec == o)
12281 continue;
12282 if (strcmp (o->name, ".dynstr") != 0)
12283 {
12284 if (! bfd_set_section_contents (abfd, o->output_section,
12285 o->contents,
12286 (file_ptr) o->output_offset
12287 * bfd_octets_per_byte (abfd),
12288 o->size))
12289 goto error_return;
12290 }
12291 else
12292 {
12293 /* The contents of the .dynstr section are actually in a
12294 stringtab. */
12295 file_ptr off;
12296
12297 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12298 if (bfd_seek (abfd, off, SEEK_SET) != 0
12299 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12300 goto error_return;
12301 }
12302 }
12303 }
12304
12305 if (bfd_link_relocatable (info))
12306 {
12307 bfd_boolean failed = FALSE;
12308
12309 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12310 if (failed)
12311 goto error_return;
12312 }
12313
12314 /* If we have optimized stabs strings, output them. */
12315 if (htab->stab_info.stabstr != NULL)
12316 {
12317 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12318 goto error_return;
12319 }
12320
12321 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12322 goto error_return;
12323
12324 elf_final_link_free (abfd, &flinfo);
12325
12326 elf_linker (abfd) = TRUE;
12327
12328 if (attr_section)
12329 {
12330 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12331 if (contents == NULL)
12332 return FALSE; /* Bail out and fail. */
12333 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12334 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12335 free (contents);
12336 }
12337
12338 return TRUE;
12339
12340 error_return:
12341 elf_final_link_free (abfd, &flinfo);
12342 return FALSE;
12343}
12344\f
12345/* Initialize COOKIE for input bfd ABFD. */
12346
12347static bfd_boolean
12348init_reloc_cookie (struct elf_reloc_cookie *cookie,
12349 struct bfd_link_info *info, bfd *abfd)
12350{
12351 Elf_Internal_Shdr *symtab_hdr;
12352 const struct elf_backend_data *bed;
12353
12354 bed = get_elf_backend_data (abfd);
12355 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12356
12357 cookie->abfd = abfd;
12358 cookie->sym_hashes = elf_sym_hashes (abfd);
12359 cookie->bad_symtab = elf_bad_symtab (abfd);
12360 if (cookie->bad_symtab)
12361 {
12362 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12363 cookie->extsymoff = 0;
12364 }
12365 else
12366 {
12367 cookie->locsymcount = symtab_hdr->sh_info;
12368 cookie->extsymoff = symtab_hdr->sh_info;
12369 }
12370
12371 if (bed->s->arch_size == 32)
12372 cookie->r_sym_shift = 8;
12373 else
12374 cookie->r_sym_shift = 32;
12375
12376 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12377 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12378 {
12379 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12380 cookie->locsymcount, 0,
12381 NULL, NULL, NULL);
12382 if (cookie->locsyms == NULL)
12383 {
12384 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12385 return FALSE;
12386 }
12387 if (info->keep_memory)
12388 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12389 }
12390 return TRUE;
12391}
12392
12393/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12394
12395static void
12396fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12397{
12398 Elf_Internal_Shdr *symtab_hdr;
12399
12400 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12401 if (cookie->locsyms != NULL
12402 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12403 free (cookie->locsyms);
12404}
12405
12406/* Initialize the relocation information in COOKIE for input section SEC
12407 of input bfd ABFD. */
12408
12409static bfd_boolean
12410init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12411 struct bfd_link_info *info, bfd *abfd,
12412 asection *sec)
12413{
12414 const struct elf_backend_data *bed;
12415
12416 if (sec->reloc_count == 0)
12417 {
12418 cookie->rels = NULL;
12419 cookie->relend = NULL;
12420 }
12421 else
12422 {
12423 bed = get_elf_backend_data (abfd);
12424
12425 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12426 info->keep_memory);
12427 if (cookie->rels == NULL)
12428 return FALSE;
12429 cookie->rel = cookie->rels;
12430 cookie->relend = (cookie->rels
12431 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12432 }
12433 cookie->rel = cookie->rels;
12434 return TRUE;
12435}
12436
12437/* Free the memory allocated by init_reloc_cookie_rels,
12438 if appropriate. */
12439
12440static void
12441fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12442 asection *sec)
12443{
12444 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12445 free (cookie->rels);
12446}
12447
12448/* Initialize the whole of COOKIE for input section SEC. */
12449
12450static bfd_boolean
12451init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12452 struct bfd_link_info *info,
12453 asection *sec)
12454{
12455 if (!init_reloc_cookie (cookie, info, sec->owner))
12456 goto error1;
12457 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12458 goto error2;
12459 return TRUE;
12460
12461 error2:
12462 fini_reloc_cookie (cookie, sec->owner);
12463 error1:
12464 return FALSE;
12465}
12466
12467/* Free the memory allocated by init_reloc_cookie_for_section,
12468 if appropriate. */
12469
12470static void
12471fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12472 asection *sec)
12473{
12474 fini_reloc_cookie_rels (cookie, sec);
12475 fini_reloc_cookie (cookie, sec->owner);
12476}
12477\f
12478/* Garbage collect unused sections. */
12479
12480/* Default gc_mark_hook. */
12481
12482asection *
12483_bfd_elf_gc_mark_hook (asection *sec,
12484 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12485 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12486 struct elf_link_hash_entry *h,
12487 Elf_Internal_Sym *sym)
12488{
12489 if (h != NULL)
12490 {
12491 switch (h->root.type)
12492 {
12493 case bfd_link_hash_defined:
12494 case bfd_link_hash_defweak:
12495 return h->root.u.def.section;
12496
12497 case bfd_link_hash_common:
12498 return h->root.u.c.p->section;
12499
12500 default:
12501 break;
12502 }
12503 }
12504 else
12505 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12506
12507 return NULL;
12508}
12509
12510/* For undefined __start_<name> and __stop_<name> symbols, return the
12511 first input section matching <name>. Return NULL otherwise. */
12512
12513asection *
12514_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12515 struct elf_link_hash_entry *h)
12516{
12517 asection *s;
12518 const char *sec_name;
12519
12520 if (h->root.type != bfd_link_hash_undefined
12521 && h->root.type != bfd_link_hash_undefweak)
12522 return NULL;
12523
12524 s = h->root.u.undef.section;
12525 if (s != NULL)
12526 {
12527 if (s == (asection *) 0 - 1)
12528 return NULL;
12529 return s;
12530 }
12531
12532 sec_name = NULL;
12533 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12534 sec_name = h->root.root.string + 8;
12535 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12536 sec_name = h->root.root.string + 7;
12537
12538 if (sec_name != NULL && *sec_name != '\0')
12539 {
12540 bfd *i;
12541
12542 for (i = info->input_bfds; i != NULL; i = i->link.next)
12543 {
12544 s = bfd_get_section_by_name (i, sec_name);
12545 if (s != NULL)
12546 {
12547 h->root.u.undef.section = s;
12548 break;
12549 }
12550 }
12551 }
12552
12553 if (s == NULL)
12554 h->root.u.undef.section = (asection *) 0 - 1;
12555
12556 return s;
12557}
12558
12559/* COOKIE->rel describes a relocation against section SEC, which is
12560 a section we've decided to keep. Return the section that contains
12561 the relocation symbol, or NULL if no section contains it. */
12562
12563asection *
12564_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12565 elf_gc_mark_hook_fn gc_mark_hook,
12566 struct elf_reloc_cookie *cookie,
12567 bfd_boolean *start_stop)
12568{
12569 unsigned long r_symndx;
12570 struct elf_link_hash_entry *h;
12571
12572 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12573 if (r_symndx == STN_UNDEF)
12574 return NULL;
12575
12576 if (r_symndx >= cookie->locsymcount
12577 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12578 {
12579 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12580 if (h == NULL)
12581 {
12582 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12583 sec->owner);
12584 return NULL;
12585 }
12586 while (h->root.type == bfd_link_hash_indirect
12587 || h->root.type == bfd_link_hash_warning)
12588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12589 h->mark = 1;
12590 /* If this symbol is weak and there is a non-weak definition, we
12591 keep the non-weak definition because many backends put
12592 dynamic reloc info on the non-weak definition for code
12593 handling copy relocs. */
12594 if (h->u.weakdef != NULL)
12595 h->u.weakdef->mark = 1;
12596
12597 if (start_stop != NULL)
12598 {
12599 /* To work around a glibc bug, mark all XXX input sections
12600 when there is an as yet undefined reference to __start_XXX
12601 or __stop_XXX symbols. The linker will later define such
12602 symbols for orphan input sections that have a name
12603 representable as a C identifier. */
12604 asection *s = _bfd_elf_is_start_stop (info, h);
12605
12606 if (s != NULL)
12607 {
12608 *start_stop = !s->gc_mark;
12609 return s;
12610 }
12611 }
12612
12613 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12614 }
12615
12616 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12617 &cookie->locsyms[r_symndx]);
12618}
12619
12620/* COOKIE->rel describes a relocation against section SEC, which is
12621 a section we've decided to keep. Mark the section that contains
12622 the relocation symbol. */
12623
12624bfd_boolean
12625_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12626 asection *sec,
12627 elf_gc_mark_hook_fn gc_mark_hook,
12628 struct elf_reloc_cookie *cookie)
12629{
12630 asection *rsec;
12631 bfd_boolean start_stop = FALSE;
12632
12633 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12634 while (rsec != NULL)
12635 {
12636 if (!rsec->gc_mark)
12637 {
12638 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12639 || (rsec->owner->flags & DYNAMIC) != 0)
12640 rsec->gc_mark = 1;
12641 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12642 return FALSE;
12643 }
12644 if (!start_stop)
12645 break;
12646 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12647 }
12648 return TRUE;
12649}
12650
12651/* The mark phase of garbage collection. For a given section, mark
12652 it and any sections in this section's group, and all the sections
12653 which define symbols to which it refers. */
12654
12655bfd_boolean
12656_bfd_elf_gc_mark (struct bfd_link_info *info,
12657 asection *sec,
12658 elf_gc_mark_hook_fn gc_mark_hook)
12659{
12660 bfd_boolean ret;
12661 asection *group_sec, *eh_frame;
12662
12663 sec->gc_mark = 1;
12664
12665 /* Mark all the sections in the group. */
12666 group_sec = elf_section_data (sec)->next_in_group;
12667 if (group_sec && !group_sec->gc_mark)
12668 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12669 return FALSE;
12670
12671 /* Look through the section relocs. */
12672 ret = TRUE;
12673 eh_frame = elf_eh_frame_section (sec->owner);
12674 if ((sec->flags & SEC_RELOC) != 0
12675 && sec->reloc_count > 0
12676 && sec != eh_frame)
12677 {
12678 struct elf_reloc_cookie cookie;
12679
12680 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12681 ret = FALSE;
12682 else
12683 {
12684 for (; cookie.rel < cookie.relend; cookie.rel++)
12685 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12686 {
12687 ret = FALSE;
12688 break;
12689 }
12690 fini_reloc_cookie_for_section (&cookie, sec);
12691 }
12692 }
12693
12694 if (ret && eh_frame && elf_fde_list (sec))
12695 {
12696 struct elf_reloc_cookie cookie;
12697
12698 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12699 ret = FALSE;
12700 else
12701 {
12702 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12703 gc_mark_hook, &cookie))
12704 ret = FALSE;
12705 fini_reloc_cookie_for_section (&cookie, eh_frame);
12706 }
12707 }
12708
12709 eh_frame = elf_section_eh_frame_entry (sec);
12710 if (ret && eh_frame && !eh_frame->gc_mark)
12711 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12712 ret = FALSE;
12713
12714 return ret;
12715}
12716
12717/* Scan and mark sections in a special or debug section group. */
12718
12719static void
12720_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12721{
12722 /* Point to first section of section group. */
12723 asection *ssec;
12724 /* Used to iterate the section group. */
12725 asection *msec;
12726
12727 bfd_boolean is_special_grp = TRUE;
12728 bfd_boolean is_debug_grp = TRUE;
12729
12730 /* First scan to see if group contains any section other than debug
12731 and special section. */
12732 ssec = msec = elf_next_in_group (grp);
12733 do
12734 {
12735 if ((msec->flags & SEC_DEBUGGING) == 0)
12736 is_debug_grp = FALSE;
12737
12738 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12739 is_special_grp = FALSE;
12740
12741 msec = elf_next_in_group (msec);
12742 }
12743 while (msec != ssec);
12744
12745 /* If this is a pure debug section group or pure special section group,
12746 keep all sections in this group. */
12747 if (is_debug_grp || is_special_grp)
12748 {
12749 do
12750 {
12751 msec->gc_mark = 1;
12752 msec = elf_next_in_group (msec);
12753 }
12754 while (msec != ssec);
12755 }
12756}
12757
12758/* Keep debug and special sections. */
12759
12760bfd_boolean
12761_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12762 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12763{
12764 bfd *ibfd;
12765
12766 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12767 {
12768 asection *isec;
12769 bfd_boolean some_kept;
12770 bfd_boolean debug_frag_seen;
12771
12772 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12773 continue;
12774
12775 /* Ensure all linker created sections are kept,
12776 see if any other section is already marked,
12777 and note if we have any fragmented debug sections. */
12778 debug_frag_seen = some_kept = FALSE;
12779 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12780 {
12781 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12782 isec->gc_mark = 1;
12783 else if (isec->gc_mark)
12784 some_kept = TRUE;
12785
12786 if (debug_frag_seen == FALSE
12787 && (isec->flags & SEC_DEBUGGING)
12788 && CONST_STRNEQ (isec->name, ".debug_line."))
12789 debug_frag_seen = TRUE;
12790 }
12791
12792 /* If no section in this file will be kept, then we can
12793 toss out the debug and special sections. */
12794 if (!some_kept)
12795 continue;
12796
12797 /* Keep debug and special sections like .comment when they are
12798 not part of a group. Also keep section groups that contain
12799 just debug sections or special sections. */
12800 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12801 {
12802 if ((isec->flags & SEC_GROUP) != 0)
12803 _bfd_elf_gc_mark_debug_special_section_group (isec);
12804 else if (((isec->flags & SEC_DEBUGGING) != 0
12805 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12806 && elf_next_in_group (isec) == NULL)
12807 isec->gc_mark = 1;
12808 }
12809
12810 if (! debug_frag_seen)
12811 continue;
12812
12813 /* Look for CODE sections which are going to be discarded,
12814 and find and discard any fragmented debug sections which
12815 are associated with that code section. */
12816 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12817 if ((isec->flags & SEC_CODE) != 0
12818 && isec->gc_mark == 0)
12819 {
12820 unsigned int ilen;
12821 asection *dsec;
12822
12823 ilen = strlen (isec->name);
12824
12825 /* Association is determined by the name of the debug section
12826 containing the name of the code section as a suffix. For
12827 example .debug_line.text.foo is a debug section associated
12828 with .text.foo. */
12829 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12830 {
12831 unsigned int dlen;
12832
12833 if (dsec->gc_mark == 0
12834 || (dsec->flags & SEC_DEBUGGING) == 0)
12835 continue;
12836
12837 dlen = strlen (dsec->name);
12838
12839 if (dlen > ilen
12840 && strncmp (dsec->name + (dlen - ilen),
12841 isec->name, ilen) == 0)
12842 {
12843 dsec->gc_mark = 0;
12844 }
12845 }
12846 }
12847 }
12848 return TRUE;
12849}
12850
12851/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12852
12853struct elf_gc_sweep_symbol_info
12854{
12855 struct bfd_link_info *info;
12856 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12857 bfd_boolean);
12858};
12859
12860static bfd_boolean
12861elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12862{
12863 if (!h->mark
12864 && (((h->root.type == bfd_link_hash_defined
12865 || h->root.type == bfd_link_hash_defweak)
12866 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12867 && h->root.u.def.section->gc_mark))
12868 || h->root.type == bfd_link_hash_undefined
12869 || h->root.type == bfd_link_hash_undefweak))
12870 {
12871 struct elf_gc_sweep_symbol_info *inf;
12872
12873 inf = (struct elf_gc_sweep_symbol_info *) data;
12874 (*inf->hide_symbol) (inf->info, h, TRUE);
12875 h->def_regular = 0;
12876 h->ref_regular = 0;
12877 h->ref_regular_nonweak = 0;
12878 }
12879
12880 return TRUE;
12881}
12882
12883/* The sweep phase of garbage collection. Remove all garbage sections. */
12884
12885typedef bfd_boolean (*gc_sweep_hook_fn)
12886 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12887
12888static bfd_boolean
12889elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12890{
12891 bfd *sub;
12892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12893 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12894 unsigned long section_sym_count;
12895 struct elf_gc_sweep_symbol_info sweep_info;
12896
12897 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12898 {
12899 asection *o;
12900
12901 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12902 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12903 continue;
12904
12905 for (o = sub->sections; o != NULL; o = o->next)
12906 {
12907 /* When any section in a section group is kept, we keep all
12908 sections in the section group. If the first member of
12909 the section group is excluded, we will also exclude the
12910 group section. */
12911 if (o->flags & SEC_GROUP)
12912 {
12913 asection *first = elf_next_in_group (o);
12914 o->gc_mark = first->gc_mark;
12915 }
12916
12917 if (o->gc_mark)
12918 continue;
12919
12920 /* Skip sweeping sections already excluded. */
12921 if (o->flags & SEC_EXCLUDE)
12922 continue;
12923
12924 /* Since this is early in the link process, it is simple
12925 to remove a section from the output. */
12926 o->flags |= SEC_EXCLUDE;
12927
12928 if (info->print_gc_sections && o->size != 0)
12929 /* xgettext:c-format */
12930 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12931
12932 /* But we also have to update some of the relocation
12933 info we collected before. */
12934 if (gc_sweep_hook
12935 && (o->flags & SEC_RELOC) != 0
12936 && o->reloc_count != 0
12937 && !((info->strip == strip_all || info->strip == strip_debugger)
12938 && (o->flags & SEC_DEBUGGING) != 0)
12939 && !bfd_is_abs_section (o->output_section))
12940 {
12941 Elf_Internal_Rela *internal_relocs;
12942 bfd_boolean r;
12943
12944 internal_relocs
12945 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12946 info->keep_memory);
12947 if (internal_relocs == NULL)
12948 return FALSE;
12949
12950 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12951
12952 if (elf_section_data (o)->relocs != internal_relocs)
12953 free (internal_relocs);
12954
12955 if (!r)
12956 return FALSE;
12957 }
12958 }
12959 }
12960
12961 /* Remove the symbols that were in the swept sections from the dynamic
12962 symbol table. GCFIXME: Anyone know how to get them out of the
12963 static symbol table as well? */
12964 sweep_info.info = info;
12965 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12966 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12967 &sweep_info);
12968
12969 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12970 return TRUE;
12971}
12972
12973/* Propagate collected vtable information. This is called through
12974 elf_link_hash_traverse. */
12975
12976static bfd_boolean
12977elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12978{
12979 /* Those that are not vtables. */
12980 if (h->vtable == NULL || h->vtable->parent == NULL)
12981 return TRUE;
12982
12983 /* Those vtables that do not have parents, we cannot merge. */
12984 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12985 return TRUE;
12986
12987 /* If we've already been done, exit. */
12988 if (h->vtable->used && h->vtable->used[-1])
12989 return TRUE;
12990
12991 /* Make sure the parent's table is up to date. */
12992 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12993
12994 if (h->vtable->used == NULL)
12995 {
12996 /* None of this table's entries were referenced. Re-use the
12997 parent's table. */
12998 h->vtable->used = h->vtable->parent->vtable->used;
12999 h->vtable->size = h->vtable->parent->vtable->size;
13000 }
13001 else
13002 {
13003 size_t n;
13004 bfd_boolean *cu, *pu;
13005
13006 /* Or the parent's entries into ours. */
13007 cu = h->vtable->used;
13008 cu[-1] = TRUE;
13009 pu = h->vtable->parent->vtable->used;
13010 if (pu != NULL)
13011 {
13012 const struct elf_backend_data *bed;
13013 unsigned int log_file_align;
13014
13015 bed = get_elf_backend_data (h->root.u.def.section->owner);
13016 log_file_align = bed->s->log_file_align;
13017 n = h->vtable->parent->vtable->size >> log_file_align;
13018 while (n--)
13019 {
13020 if (*pu)
13021 *cu = TRUE;
13022 pu++;
13023 cu++;
13024 }
13025 }
13026 }
13027
13028 return TRUE;
13029}
13030
13031static bfd_boolean
13032elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13033{
13034 asection *sec;
13035 bfd_vma hstart, hend;
13036 Elf_Internal_Rela *relstart, *relend, *rel;
13037 const struct elf_backend_data *bed;
13038 unsigned int log_file_align;
13039
13040 /* Take care of both those symbols that do not describe vtables as
13041 well as those that are not loaded. */
13042 if (h->vtable == NULL || h->vtable->parent == NULL)
13043 return TRUE;
13044
13045 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13046 || h->root.type == bfd_link_hash_defweak);
13047
13048 sec = h->root.u.def.section;
13049 hstart = h->root.u.def.value;
13050 hend = hstart + h->size;
13051
13052 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13053 if (!relstart)
13054 return *(bfd_boolean *) okp = FALSE;
13055 bed = get_elf_backend_data (sec->owner);
13056 log_file_align = bed->s->log_file_align;
13057
13058 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13059
13060 for (rel = relstart; rel < relend; ++rel)
13061 if (rel->r_offset >= hstart && rel->r_offset < hend)
13062 {
13063 /* If the entry is in use, do nothing. */
13064 if (h->vtable->used
13065 && (rel->r_offset - hstart) < h->vtable->size)
13066 {
13067 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13068 if (h->vtable->used[entry])
13069 continue;
13070 }
13071 /* Otherwise, kill it. */
13072 rel->r_offset = rel->r_info = rel->r_addend = 0;
13073 }
13074
13075 return TRUE;
13076}
13077
13078/* Mark sections containing dynamically referenced symbols. When
13079 building shared libraries, we must assume that any visible symbol is
13080 referenced. */
13081
13082bfd_boolean
13083bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13084{
13085 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13086 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13087
13088 if ((h->root.type == bfd_link_hash_defined
13089 || h->root.type == bfd_link_hash_defweak)
13090 && (h->ref_dynamic
13091 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13092 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13093 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13094 && (!bfd_link_executable (info)
13095 || info->gc_keep_exported
13096 || info->export_dynamic
13097 || (h->dynamic
13098 && d != NULL
13099 && (*d->match) (&d->head, NULL, h->root.root.string)))
13100 && (h->versioned >= versioned
13101 || !bfd_hide_sym_by_version (info->version_info,
13102 h->root.root.string)))))
13103 h->root.u.def.section->flags |= SEC_KEEP;
13104
13105 return TRUE;
13106}
13107
13108/* Keep all sections containing symbols undefined on the command-line,
13109 and the section containing the entry symbol. */
13110
13111void
13112_bfd_elf_gc_keep (struct bfd_link_info *info)
13113{
13114 struct bfd_sym_chain *sym;
13115
13116 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13117 {
13118 struct elf_link_hash_entry *h;
13119
13120 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13121 FALSE, FALSE, FALSE);
13122
13123 if (h != NULL
13124 && (h->root.type == bfd_link_hash_defined
13125 || h->root.type == bfd_link_hash_defweak)
13126 && !bfd_is_abs_section (h->root.u.def.section)
13127 && !bfd_is_und_section (h->root.u.def.section))
13128 h->root.u.def.section->flags |= SEC_KEEP;
13129 }
13130}
13131
13132bfd_boolean
13133bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13134 struct bfd_link_info *info)
13135{
13136 bfd *ibfd = info->input_bfds;
13137
13138 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13139 {
13140 asection *sec;
13141 struct elf_reloc_cookie cookie;
13142
13143 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13144 continue;
13145
13146 if (!init_reloc_cookie (&cookie, info, ibfd))
13147 return FALSE;
13148
13149 for (sec = ibfd->sections; sec; sec = sec->next)
13150 {
13151 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13152 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13153 {
13154 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13155 fini_reloc_cookie_rels (&cookie, sec);
13156 }
13157 }
13158 }
13159 return TRUE;
13160}
13161
13162/* Do mark and sweep of unused sections. */
13163
13164bfd_boolean
13165bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13166{
13167 bfd_boolean ok = TRUE;
13168 bfd *sub;
13169 elf_gc_mark_hook_fn gc_mark_hook;
13170 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13171 struct elf_link_hash_table *htab;
13172
13173 if (!bed->can_gc_sections
13174 || !is_elf_hash_table (info->hash))
13175 {
13176 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13177 return TRUE;
13178 }
13179
13180 bed->gc_keep (info);
13181 htab = elf_hash_table (info);
13182
13183 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13184 at the .eh_frame section if we can mark the FDEs individually. */
13185 for (sub = info->input_bfds;
13186 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13187 sub = sub->link.next)
13188 {
13189 asection *sec;
13190 struct elf_reloc_cookie cookie;
13191
13192 sec = bfd_get_section_by_name (sub, ".eh_frame");
13193 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13194 {
13195 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13196 if (elf_section_data (sec)->sec_info
13197 && (sec->flags & SEC_LINKER_CREATED) == 0)
13198 elf_eh_frame_section (sub) = sec;
13199 fini_reloc_cookie_for_section (&cookie, sec);
13200 sec = bfd_get_next_section_by_name (NULL, sec);
13201 }
13202 }
13203
13204 /* Apply transitive closure to the vtable entry usage info. */
13205 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13206 if (!ok)
13207 return FALSE;
13208
13209 /* Kill the vtable relocations that were not used. */
13210 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13211 if (!ok)
13212 return FALSE;
13213
13214 /* Mark dynamically referenced symbols. */
13215 if (htab->dynamic_sections_created || info->gc_keep_exported)
13216 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13217
13218 /* Grovel through relocs to find out who stays ... */
13219 gc_mark_hook = bed->gc_mark_hook;
13220 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13221 {
13222 asection *o;
13223
13224 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13225 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13226 continue;
13227
13228 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13229 Also treat note sections as a root, if the section is not part
13230 of a group. */
13231 for (o = sub->sections; o != NULL; o = o->next)
13232 if (!o->gc_mark
13233 && (o->flags & SEC_EXCLUDE) == 0
13234 && ((o->flags & SEC_KEEP) != 0
13235 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13236 && elf_next_in_group (o) == NULL )))
13237 {
13238 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13239 return FALSE;
13240 }
13241 }
13242
13243 /* Allow the backend to mark additional target specific sections. */
13244 bed->gc_mark_extra_sections (info, gc_mark_hook);
13245
13246 /* ... and mark SEC_EXCLUDE for those that go. */
13247 return elf_gc_sweep (abfd, info);
13248}
13249\f
13250/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13251
13252bfd_boolean
13253bfd_elf_gc_record_vtinherit (bfd *abfd,
13254 asection *sec,
13255 struct elf_link_hash_entry *h,
13256 bfd_vma offset)
13257{
13258 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13259 struct elf_link_hash_entry **search, *child;
13260 size_t extsymcount;
13261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13262
13263 /* The sh_info field of the symtab header tells us where the
13264 external symbols start. We don't care about the local symbols at
13265 this point. */
13266 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13267 if (!elf_bad_symtab (abfd))
13268 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13269
13270 sym_hashes = elf_sym_hashes (abfd);
13271 sym_hashes_end = sym_hashes + extsymcount;
13272
13273 /* Hunt down the child symbol, which is in this section at the same
13274 offset as the relocation. */
13275 for (search = sym_hashes; search != sym_hashes_end; ++search)
13276 {
13277 if ((child = *search) != NULL
13278 && (child->root.type == bfd_link_hash_defined
13279 || child->root.type == bfd_link_hash_defweak)
13280 && child->root.u.def.section == sec
13281 && child->root.u.def.value == offset)
13282 goto win;
13283 }
13284
13285 /* xgettext:c-format */
13286 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13287 abfd, sec, (unsigned long) offset);
13288 bfd_set_error (bfd_error_invalid_operation);
13289 return FALSE;
13290
13291 win:
13292 if (!child->vtable)
13293 {
13294 child->vtable = ((struct elf_link_virtual_table_entry *)
13295 bfd_zalloc (abfd, sizeof (*child->vtable)));
13296 if (!child->vtable)
13297 return FALSE;
13298 }
13299 if (!h)
13300 {
13301 /* This *should* only be the absolute section. It could potentially
13302 be that someone has defined a non-global vtable though, which
13303 would be bad. It isn't worth paging in the local symbols to be
13304 sure though; that case should simply be handled by the assembler. */
13305
13306 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13307 }
13308 else
13309 child->vtable->parent = h;
13310
13311 return TRUE;
13312}
13313
13314/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13315
13316bfd_boolean
13317bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13318 asection *sec ATTRIBUTE_UNUSED,
13319 struct elf_link_hash_entry *h,
13320 bfd_vma addend)
13321{
13322 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13323 unsigned int log_file_align = bed->s->log_file_align;
13324
13325 if (!h->vtable)
13326 {
13327 h->vtable = ((struct elf_link_virtual_table_entry *)
13328 bfd_zalloc (abfd, sizeof (*h->vtable)));
13329 if (!h->vtable)
13330 return FALSE;
13331 }
13332
13333 if (addend >= h->vtable->size)
13334 {
13335 size_t size, bytes, file_align;
13336 bfd_boolean *ptr = h->vtable->used;
13337
13338 /* While the symbol is undefined, we have to be prepared to handle
13339 a zero size. */
13340 file_align = 1 << log_file_align;
13341 if (h->root.type == bfd_link_hash_undefined)
13342 size = addend + file_align;
13343 else
13344 {
13345 size = h->size;
13346 if (addend >= size)
13347 {
13348 /* Oops! We've got a reference past the defined end of
13349 the table. This is probably a bug -- shall we warn? */
13350 size = addend + file_align;
13351 }
13352 }
13353 size = (size + file_align - 1) & -file_align;
13354
13355 /* Allocate one extra entry for use as a "done" flag for the
13356 consolidation pass. */
13357 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13358
13359 if (ptr)
13360 {
13361 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13362
13363 if (ptr != NULL)
13364 {
13365 size_t oldbytes;
13366
13367 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13368 * sizeof (bfd_boolean));
13369 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13370 }
13371 }
13372 else
13373 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13374
13375 if (ptr == NULL)
13376 return FALSE;
13377
13378 /* And arrange for that done flag to be at index -1. */
13379 h->vtable->used = ptr + 1;
13380 h->vtable->size = size;
13381 }
13382
13383 h->vtable->used[addend >> log_file_align] = TRUE;
13384
13385 return TRUE;
13386}
13387
13388/* Map an ELF section header flag to its corresponding string. */
13389typedef struct
13390{
13391 char *flag_name;
13392 flagword flag_value;
13393} elf_flags_to_name_table;
13394
13395static elf_flags_to_name_table elf_flags_to_names [] =
13396{
13397 { "SHF_WRITE", SHF_WRITE },
13398 { "SHF_ALLOC", SHF_ALLOC },
13399 { "SHF_EXECINSTR", SHF_EXECINSTR },
13400 { "SHF_MERGE", SHF_MERGE },
13401 { "SHF_STRINGS", SHF_STRINGS },
13402 { "SHF_INFO_LINK", SHF_INFO_LINK},
13403 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13404 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13405 { "SHF_GROUP", SHF_GROUP },
13406 { "SHF_TLS", SHF_TLS },
13407 { "SHF_MASKOS", SHF_MASKOS },
13408 { "SHF_EXCLUDE", SHF_EXCLUDE },
13409};
13410
13411/* Returns TRUE if the section is to be included, otherwise FALSE. */
13412bfd_boolean
13413bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13414 struct flag_info *flaginfo,
13415 asection *section)
13416{
13417 const bfd_vma sh_flags = elf_section_flags (section);
13418
13419 if (!flaginfo->flags_initialized)
13420 {
13421 bfd *obfd = info->output_bfd;
13422 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13423 struct flag_info_list *tf = flaginfo->flag_list;
13424 int with_hex = 0;
13425 int without_hex = 0;
13426
13427 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13428 {
13429 unsigned i;
13430 flagword (*lookup) (char *);
13431
13432 lookup = bed->elf_backend_lookup_section_flags_hook;
13433 if (lookup != NULL)
13434 {
13435 flagword hexval = (*lookup) ((char *) tf->name);
13436
13437 if (hexval != 0)
13438 {
13439 if (tf->with == with_flags)
13440 with_hex |= hexval;
13441 else if (tf->with == without_flags)
13442 without_hex |= hexval;
13443 tf->valid = TRUE;
13444 continue;
13445 }
13446 }
13447 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13448 {
13449 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13450 {
13451 if (tf->with == with_flags)
13452 with_hex |= elf_flags_to_names[i].flag_value;
13453 else if (tf->with == without_flags)
13454 without_hex |= elf_flags_to_names[i].flag_value;
13455 tf->valid = TRUE;
13456 break;
13457 }
13458 }
13459 if (!tf->valid)
13460 {
13461 info->callbacks->einfo
13462 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13463 return FALSE;
13464 }
13465 }
13466 flaginfo->flags_initialized = TRUE;
13467 flaginfo->only_with_flags |= with_hex;
13468 flaginfo->not_with_flags |= without_hex;
13469 }
13470
13471 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13472 return FALSE;
13473
13474 if ((flaginfo->not_with_flags & sh_flags) != 0)
13475 return FALSE;
13476
13477 return TRUE;
13478}
13479
13480struct alloc_got_off_arg {
13481 bfd_vma gotoff;
13482 struct bfd_link_info *info;
13483};
13484
13485/* We need a special top-level link routine to convert got reference counts
13486 to real got offsets. */
13487
13488static bfd_boolean
13489elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13490{
13491 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13492 bfd *obfd = gofarg->info->output_bfd;
13493 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13494
13495 if (h->got.refcount > 0)
13496 {
13497 h->got.offset = gofarg->gotoff;
13498 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13499 }
13500 else
13501 h->got.offset = (bfd_vma) -1;
13502
13503 return TRUE;
13504}
13505
13506/* And an accompanying bit to work out final got entry offsets once
13507 we're done. Should be called from final_link. */
13508
13509bfd_boolean
13510bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13511 struct bfd_link_info *info)
13512{
13513 bfd *i;
13514 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13515 bfd_vma gotoff;
13516 struct alloc_got_off_arg gofarg;
13517
13518 BFD_ASSERT (abfd == info->output_bfd);
13519
13520 if (! is_elf_hash_table (info->hash))
13521 return FALSE;
13522
13523 /* The GOT offset is relative to the .got section, but the GOT header is
13524 put into the .got.plt section, if the backend uses it. */
13525 if (bed->want_got_plt)
13526 gotoff = 0;
13527 else
13528 gotoff = bed->got_header_size;
13529
13530 /* Do the local .got entries first. */
13531 for (i = info->input_bfds; i; i = i->link.next)
13532 {
13533 bfd_signed_vma *local_got;
13534 size_t j, locsymcount;
13535 Elf_Internal_Shdr *symtab_hdr;
13536
13537 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13538 continue;
13539
13540 local_got = elf_local_got_refcounts (i);
13541 if (!local_got)
13542 continue;
13543
13544 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13545 if (elf_bad_symtab (i))
13546 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13547 else
13548 locsymcount = symtab_hdr->sh_info;
13549
13550 for (j = 0; j < locsymcount; ++j)
13551 {
13552 if (local_got[j] > 0)
13553 {
13554 local_got[j] = gotoff;
13555 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13556 }
13557 else
13558 local_got[j] = (bfd_vma) -1;
13559 }
13560 }
13561
13562 /* Then the global .got entries. .plt refcounts are handled by
13563 adjust_dynamic_symbol */
13564 gofarg.gotoff = gotoff;
13565 gofarg.info = info;
13566 elf_link_hash_traverse (elf_hash_table (info),
13567 elf_gc_allocate_got_offsets,
13568 &gofarg);
13569 return TRUE;
13570}
13571
13572/* Many folk need no more in the way of final link than this, once
13573 got entry reference counting is enabled. */
13574
13575bfd_boolean
13576bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13577{
13578 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13579 return FALSE;
13580
13581 /* Invoke the regular ELF backend linker to do all the work. */
13582 return bfd_elf_final_link (abfd, info);
13583}
13584
13585bfd_boolean
13586bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13587{
13588 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13589
13590 if (rcookie->bad_symtab)
13591 rcookie->rel = rcookie->rels;
13592
13593 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13594 {
13595 unsigned long r_symndx;
13596
13597 if (! rcookie->bad_symtab)
13598 if (rcookie->rel->r_offset > offset)
13599 return FALSE;
13600 if (rcookie->rel->r_offset != offset)
13601 continue;
13602
13603 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13604 if (r_symndx == STN_UNDEF)
13605 return TRUE;
13606
13607 if (r_symndx >= rcookie->locsymcount
13608 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13609 {
13610 struct elf_link_hash_entry *h;
13611
13612 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13613
13614 while (h->root.type == bfd_link_hash_indirect
13615 || h->root.type == bfd_link_hash_warning)
13616 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13617
13618 if ((h->root.type == bfd_link_hash_defined
13619 || h->root.type == bfd_link_hash_defweak)
13620 && (h->root.u.def.section->owner != rcookie->abfd
13621 || h->root.u.def.section->kept_section != NULL
13622 || discarded_section (h->root.u.def.section)))
13623 return TRUE;
13624 }
13625 else
13626 {
13627 /* It's not a relocation against a global symbol,
13628 but it could be a relocation against a local
13629 symbol for a discarded section. */
13630 asection *isec;
13631 Elf_Internal_Sym *isym;
13632
13633 /* Need to: get the symbol; get the section. */
13634 isym = &rcookie->locsyms[r_symndx];
13635 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13636 if (isec != NULL
13637 && (isec->kept_section != NULL
13638 || discarded_section (isec)))
13639 return TRUE;
13640 }
13641 return FALSE;
13642 }
13643 return FALSE;
13644}
13645
13646/* Discard unneeded references to discarded sections.
13647 Returns -1 on error, 1 if any section's size was changed, 0 if
13648 nothing changed. This function assumes that the relocations are in
13649 sorted order, which is true for all known assemblers. */
13650
13651int
13652bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13653{
13654 struct elf_reloc_cookie cookie;
13655 asection *o;
13656 bfd *abfd;
13657 int changed = 0;
13658
13659 if (info->traditional_format
13660 || !is_elf_hash_table (info->hash))
13661 return 0;
13662
13663 o = bfd_get_section_by_name (output_bfd, ".stab");
13664 if (o != NULL)
13665 {
13666 asection *i;
13667
13668 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13669 {
13670 if (i->size == 0
13671 || i->reloc_count == 0
13672 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13673 continue;
13674
13675 abfd = i->owner;
13676 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13677 continue;
13678
13679 if (!init_reloc_cookie_for_section (&cookie, info, i))
13680 return -1;
13681
13682 if (_bfd_discard_section_stabs (abfd, i,
13683 elf_section_data (i)->sec_info,
13684 bfd_elf_reloc_symbol_deleted_p,
13685 &cookie))
13686 changed = 1;
13687
13688 fini_reloc_cookie_for_section (&cookie, i);
13689 }
13690 }
13691
13692 o = NULL;
13693 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13694 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13695 if (o != NULL)
13696 {
13697 asection *i;
13698
13699 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13700 {
13701 if (i->size == 0)
13702 continue;
13703
13704 abfd = i->owner;
13705 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13706 continue;
13707
13708 if (!init_reloc_cookie_for_section (&cookie, info, i))
13709 return -1;
13710
13711 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13712 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13713 bfd_elf_reloc_symbol_deleted_p,
13714 &cookie))
13715 changed = 1;
13716
13717 fini_reloc_cookie_for_section (&cookie, i);
13718 }
13719 }
13720
13721 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13722 {
13723 const struct elf_backend_data *bed;
13724
13725 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13726 continue;
13727
13728 bed = get_elf_backend_data (abfd);
13729
13730 if (bed->elf_backend_discard_info != NULL)
13731 {
13732 if (!init_reloc_cookie (&cookie, info, abfd))
13733 return -1;
13734
13735 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13736 changed = 1;
13737
13738 fini_reloc_cookie (&cookie, abfd);
13739 }
13740 }
13741
13742 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13743 _bfd_elf_end_eh_frame_parsing (info);
13744
13745 if (info->eh_frame_hdr_type
13746 && !bfd_link_relocatable (info)
13747 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13748 changed = 1;
13749
13750 return changed;
13751}
13752
13753bfd_boolean
13754_bfd_elf_section_already_linked (bfd *abfd,
13755 asection *sec,
13756 struct bfd_link_info *info)
13757{
13758 flagword flags;
13759 const char *name, *key;
13760 struct bfd_section_already_linked *l;
13761 struct bfd_section_already_linked_hash_entry *already_linked_list;
13762
13763 if (sec->output_section == bfd_abs_section_ptr)
13764 return FALSE;
13765
13766 flags = sec->flags;
13767
13768 /* Return if it isn't a linkonce section. A comdat group section
13769 also has SEC_LINK_ONCE set. */
13770 if ((flags & SEC_LINK_ONCE) == 0)
13771 return FALSE;
13772
13773 /* Don't put group member sections on our list of already linked
13774 sections. They are handled as a group via their group section. */
13775 if (elf_sec_group (sec) != NULL)
13776 return FALSE;
13777
13778 /* For a SHT_GROUP section, use the group signature as the key. */
13779 name = sec->name;
13780 if ((flags & SEC_GROUP) != 0
13781 && elf_next_in_group (sec) != NULL
13782 && elf_group_name (elf_next_in_group (sec)) != NULL)
13783 key = elf_group_name (elf_next_in_group (sec));
13784 else
13785 {
13786 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13787 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13788 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13789 key++;
13790 else
13791 /* Must be a user linkonce section that doesn't follow gcc's
13792 naming convention. In this case we won't be matching
13793 single member groups. */
13794 key = name;
13795 }
13796
13797 already_linked_list = bfd_section_already_linked_table_lookup (key);
13798
13799 for (l = already_linked_list->entry; l != NULL; l = l->next)
13800 {
13801 /* We may have 2 different types of sections on the list: group
13802 sections with a signature of <key> (<key> is some string),
13803 and linkonce sections named .gnu.linkonce.<type>.<key>.
13804 Match like sections. LTO plugin sections are an exception.
13805 They are always named .gnu.linkonce.t.<key> and match either
13806 type of section. */
13807 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13808 && ((flags & SEC_GROUP) != 0
13809 || strcmp (name, l->sec->name) == 0))
13810 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13811 {
13812 /* The section has already been linked. See if we should
13813 issue a warning. */
13814 if (!_bfd_handle_already_linked (sec, l, info))
13815 return FALSE;
13816
13817 if (flags & SEC_GROUP)
13818 {
13819 asection *first = elf_next_in_group (sec);
13820 asection *s = first;
13821
13822 while (s != NULL)
13823 {
13824 s->output_section = bfd_abs_section_ptr;
13825 /* Record which group discards it. */
13826 s->kept_section = l->sec;
13827 s = elf_next_in_group (s);
13828 /* These lists are circular. */
13829 if (s == first)
13830 break;
13831 }
13832 }
13833
13834 return TRUE;
13835 }
13836 }
13837
13838 /* A single member comdat group section may be discarded by a
13839 linkonce section and vice versa. */
13840 if ((flags & SEC_GROUP) != 0)
13841 {
13842 asection *first = elf_next_in_group (sec);
13843
13844 if (first != NULL && elf_next_in_group (first) == first)
13845 /* Check this single member group against linkonce sections. */
13846 for (l = already_linked_list->entry; l != NULL; l = l->next)
13847 if ((l->sec->flags & SEC_GROUP) == 0
13848 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13849 {
13850 first->output_section = bfd_abs_section_ptr;
13851 first->kept_section = l->sec;
13852 sec->output_section = bfd_abs_section_ptr;
13853 break;
13854 }
13855 }
13856 else
13857 /* Check this linkonce section against single member groups. */
13858 for (l = already_linked_list->entry; l != NULL; l = l->next)
13859 if (l->sec->flags & SEC_GROUP)
13860 {
13861 asection *first = elf_next_in_group (l->sec);
13862
13863 if (first != NULL
13864 && elf_next_in_group (first) == first
13865 && bfd_elf_match_symbols_in_sections (first, sec, info))
13866 {
13867 sec->output_section = bfd_abs_section_ptr;
13868 sec->kept_section = first;
13869 break;
13870 }
13871 }
13872
13873 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13874 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13875 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13876 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13877 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13878 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13879 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13880 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13881 The reverse order cannot happen as there is never a bfd with only the
13882 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13883 matter as here were are looking only for cross-bfd sections. */
13884
13885 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13886 for (l = already_linked_list->entry; l != NULL; l = l->next)
13887 if ((l->sec->flags & SEC_GROUP) == 0
13888 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13889 {
13890 if (abfd != l->sec->owner)
13891 sec->output_section = bfd_abs_section_ptr;
13892 break;
13893 }
13894
13895 /* This is the first section with this name. Record it. */
13896 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13897 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13898 return sec->output_section == bfd_abs_section_ptr;
13899}
13900
13901bfd_boolean
13902_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13903{
13904 return sym->st_shndx == SHN_COMMON;
13905}
13906
13907unsigned int
13908_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13909{
13910 return SHN_COMMON;
13911}
13912
13913asection *
13914_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13915{
13916 return bfd_com_section_ptr;
13917}
13918
13919bfd_vma
13920_bfd_elf_default_got_elt_size (bfd *abfd,
13921 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13922 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13923 bfd *ibfd ATTRIBUTE_UNUSED,
13924 unsigned long symndx ATTRIBUTE_UNUSED)
13925{
13926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13927 return bed->s->arch_size / 8;
13928}
13929
13930/* Routines to support the creation of dynamic relocs. */
13931
13932/* Returns the name of the dynamic reloc section associated with SEC. */
13933
13934static const char *
13935get_dynamic_reloc_section_name (bfd * abfd,
13936 asection * sec,
13937 bfd_boolean is_rela)
13938{
13939 char *name;
13940 const char *old_name = bfd_get_section_name (NULL, sec);
13941 const char *prefix = is_rela ? ".rela" : ".rel";
13942
13943 if (old_name == NULL)
13944 return NULL;
13945
13946 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13947 sprintf (name, "%s%s", prefix, old_name);
13948
13949 return name;
13950}
13951
13952/* Returns the dynamic reloc section associated with SEC.
13953 If necessary compute the name of the dynamic reloc section based
13954 on SEC's name (looked up in ABFD's string table) and the setting
13955 of IS_RELA. */
13956
13957asection *
13958_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13959 asection * sec,
13960 bfd_boolean is_rela)
13961{
13962 asection * reloc_sec = elf_section_data (sec)->sreloc;
13963
13964 if (reloc_sec == NULL)
13965 {
13966 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13967
13968 if (name != NULL)
13969 {
13970 reloc_sec = bfd_get_linker_section (abfd, name);
13971
13972 if (reloc_sec != NULL)
13973 elf_section_data (sec)->sreloc = reloc_sec;
13974 }
13975 }
13976
13977 return reloc_sec;
13978}
13979
13980/* Returns the dynamic reloc section associated with SEC. If the
13981 section does not exist it is created and attached to the DYNOBJ
13982 bfd and stored in the SRELOC field of SEC's elf_section_data
13983 structure.
13984
13985 ALIGNMENT is the alignment for the newly created section and
13986 IS_RELA defines whether the name should be .rela.<SEC's name>
13987 or .rel.<SEC's name>. The section name is looked up in the
13988 string table associated with ABFD. */
13989
13990asection *
13991_bfd_elf_make_dynamic_reloc_section (asection *sec,
13992 bfd *dynobj,
13993 unsigned int alignment,
13994 bfd *abfd,
13995 bfd_boolean is_rela)
13996{
13997 asection * reloc_sec = elf_section_data (sec)->sreloc;
13998
13999 if (reloc_sec == NULL)
14000 {
14001 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14002
14003 if (name == NULL)
14004 return NULL;
14005
14006 reloc_sec = bfd_get_linker_section (dynobj, name);
14007
14008 if (reloc_sec == NULL)
14009 {
14010 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14011 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14012 if ((sec->flags & SEC_ALLOC) != 0)
14013 flags |= SEC_ALLOC | SEC_LOAD;
14014
14015 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14016 if (reloc_sec != NULL)
14017 {
14018 /* _bfd_elf_get_sec_type_attr chooses a section type by
14019 name. Override as it may be wrong, eg. for a user
14020 section named "auto" we'll get ".relauto" which is
14021 seen to be a .rela section. */
14022 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14023 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14024 reloc_sec = NULL;
14025 }
14026 }
14027
14028 elf_section_data (sec)->sreloc = reloc_sec;
14029 }
14030
14031 return reloc_sec;
14032}
14033
14034/* Copy the ELF symbol type and other attributes for a linker script
14035 assignment from HSRC to HDEST. Generally this should be treated as
14036 if we found a strong non-dynamic definition for HDEST (except that
14037 ld ignores multiple definition errors). */
14038void
14039_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14040 struct bfd_link_hash_entry *hdest,
14041 struct bfd_link_hash_entry *hsrc)
14042{
14043 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14044 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14045 Elf_Internal_Sym isym;
14046
14047 ehdest->type = ehsrc->type;
14048 ehdest->target_internal = ehsrc->target_internal;
14049
14050 isym.st_other = ehsrc->other;
14051 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14052}
14053
14054/* Append a RELA relocation REL to section S in BFD. */
14055
14056void
14057elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14058{
14059 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14060 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14061 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14062 bed->s->swap_reloca_out (abfd, rel, loc);
14063}
14064
14065/* Append a REL relocation REL to section S in BFD. */
14066
14067void
14068elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14069{
14070 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14071 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14072 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14073 bed->s->swap_reloc_out (abfd, rel, loc);
14074}
This page took 0.346332 seconds and 4 git commands to generate.