Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elflink.c
... / ...
CommitLineData
1/* ELF linking support for BFD.
2 Copyright (C) 1995-2018 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 bh = &h->root;
123 }
124 else
125 bh = NULL;
126
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
134 h->def_regular = 1;
135 h->non_elf = 0;
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 return h;
143}
144
145bfd_boolean
146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147{
148 flagword flags;
149 asection *s;
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
153
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
156 return TRUE;
157
158 flags = bed->dynamic_sec_flags;
159
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
169
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
176 if (bed->want_got_plt)
177 {
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 if (s == NULL
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
182 return FALSE;
183 htab->sgotplt = s;
184 }
185
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
198 if (h == NULL)
199 return FALSE;
200 }
201
202 return TRUE;
203}
204\f
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 asection *s;
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 if ((ibfd->flags
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
251
252bfd_boolean
253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
254{
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
259
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
268
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
277 {
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
282 }
283
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374{
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
380
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
384
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
485 }
486 }
487
488 return TRUE;
489}
490\f
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
499bfd_boolean
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
502{
503 if (h->dynindx == -1)
504 {
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
509
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
520 {
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
524 }
525
526 default:
527 break;
528 }
529
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
540 }
541
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
558
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
562 }
563
564 return TRUE;
565}
566\f
567/* Mark a symbol dynamic. */
568
569static void
570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
573{
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
575
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
579
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
595}
596
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
603 const char *name,
604 bfd_boolean provide,
605 bfd_boolean hidden)
606{
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
610
611 if (!is_elf_hash_table (info->hash))
612 return TRUE;
613
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
616 if (h == NULL)
617 return provide;
618
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
643 switch (h->root.type)
644 {
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
657 break;
658 case bfd_link_hash_new:
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
675 default:
676 BFD_FAIL ();
677 return FALSE;
678 }
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
685 && h->def_dynamic
686 && !h->def_regular)
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
693 if (!provide
694 && h->def_dynamic
695 && !h->def_regular)
696 h->verinfo.verdef = NULL;
697
698 /* Make sure this symbol is not garbage collected. */
699 h->mark = 1;
700
701 h->def_regular = 1;
702
703 if (hidden)
704 {
705 bed = get_elf_backend_data (output_bfd);
706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
708 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
709 }
710
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
712 and executables. */
713 if (!bfd_link_relocatable (info)
714 && h->dynindx != -1
715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
717 h->forced_local = 1;
718
719 if ((h->def_dynamic
720 || h->ref_dynamic
721 || bfd_link_dll (info)
722 || elf_hash_table (info)->is_relocatable_executable)
723 && !h->forced_local
724 && h->dynindx == -1)
725 {
726 if (! bfd_elf_link_record_dynamic_symbol (info, h))
727 return FALSE;
728
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
732 if (h->is_weakalias)
733 {
734 struct elf_link_hash_entry *def = weakdef (h);
735
736 if (def->dynindx == -1
737 && !bfd_elf_link_record_dynamic_symbol (info, def))
738 return FALSE;
739 }
740 }
741
742 return TRUE;
743}
744
745/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
748
749int
750bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
751 bfd *input_bfd,
752 long input_indx)
753{
754 bfd_size_type amt;
755 struct elf_link_local_dynamic_entry *entry;
756 struct elf_link_hash_table *eht;
757 struct elf_strtab_hash *dynstr;
758 size_t dynstr_index;
759 char *name;
760 Elf_External_Sym_Shndx eshndx;
761 char esym[sizeof (Elf64_External_Sym)];
762
763 if (! is_elf_hash_table (info->hash))
764 return 0;
765
766 /* See if the entry exists already. */
767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
769 return 1;
770
771 amt = sizeof (*entry);
772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
773 if (entry == NULL)
774 return 0;
775
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
778 1, input_indx, &entry->isym, esym, &eshndx))
779 {
780 bfd_release (input_bfd, entry);
781 return 0;
782 }
783
784 if (entry->isym.st_shndx != SHN_UNDEF
785 && entry->isym.st_shndx < SHN_LORESERVE)
786 {
787 asection *s;
788
789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
790 if (s == NULL || bfd_is_abs_section (s->output_section))
791 {
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd, entry);
795 return 2;
796 }
797 }
798
799 name = (bfd_elf_string_from_elf_section
800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
801 entry->isym.st_name));
802
803 dynstr = elf_hash_table (info)->dynstr;
804 if (dynstr == NULL)
805 {
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
808 if (dynstr == NULL)
809 return 0;
810 }
811
812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
813 if (dynstr_index == (size_t) -1)
814 return 0;
815 entry->isym.st_name = dynstr_index;
816
817 eht = elf_hash_table (info);
818
819 entry->next = eht->dynlocal;
820 eht->dynlocal = entry;
821 entry->input_bfd = input_bfd;
822 entry->input_indx = input_indx;
823 eht->dynsymcount++;
824
825 /* Whatever binding the symbol had before, it's now local. */
826 entry->isym.st_info
827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
828
829 /* The dynindx will be set at the end of size_dynamic_sections. */
830
831 return 1;
832}
833
834/* Return the dynindex of a local dynamic symbol. */
835
836long
837_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
838 bfd *input_bfd,
839 long input_indx)
840{
841 struct elf_link_local_dynamic_entry *e;
842
843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
844 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
845 return e->dynindx;
846 return -1;
847}
848
849/* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
852
853static bfd_boolean
854elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 void *data)
856{
857 size_t *count = (size_t *) data;
858
859 if (h->forced_local)
860 return TRUE;
861
862 if (h->dynindx != -1)
863 h->dynindx = ++(*count);
864
865 return TRUE;
866}
867
868
869/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
871
872static bfd_boolean
873elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
874 void *data)
875{
876 size_t *count = (size_t *) data;
877
878 if (!h->forced_local)
879 return TRUE;
880
881 if (h->dynindx != -1)
882 h->dynindx = ++(*count);
883
884 return TRUE;
885}
886
887/* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
889bfd_boolean
890_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
891 struct bfd_link_info *info,
892 asection *p)
893{
894 struct elf_link_hash_table *htab;
895 asection *ip;
896
897 switch (elf_section_data (p)->this_hdr.sh_type)
898 {
899 case SHT_PROGBITS:
900 case SHT_NOBITS:
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
903 case SHT_NULL:
904 htab = elf_hash_table (info);
905 if (p == htab->tls_sec)
906 return FALSE;
907
908 if (htab->text_index_section != NULL)
909 return p != htab->text_index_section && p != htab->data_index_section;
910
911 return (htab->dynobj != NULL
912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
913 && ip->output_section == p);
914
915 /* There shouldn't be section relative relocations
916 against any other section. */
917 default:
918 return TRUE;
919 }
920}
921
922bfd_boolean
923_bfd_elf_omit_section_dynsym_all
924 (bfd *output_bfd ATTRIBUTE_UNUSED,
925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
926 asection *p ATTRIBUTE_UNUSED)
927{
928 return TRUE;
929}
930
931/* Assign dynsym indices. In a shared library we generate a section
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
939
940static unsigned long
941_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
942 struct bfd_link_info *info,
943 unsigned long *section_sym_count)
944{
945 unsigned long dynsymcount = 0;
946 bfd_boolean do_sec = section_sym_count != NULL;
947
948 if (bfd_link_pic (info)
949 || elf_hash_table (info)->is_relocatable_executable)
950 {
951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
952 asection *p;
953 for (p = output_bfd->sections; p ; p = p->next)
954 if ((p->flags & SEC_EXCLUDE) == 0
955 && (p->flags & SEC_ALLOC) != 0
956 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
957 {
958 ++dynsymcount;
959 if (do_sec)
960 elf_section_data (p)->dynindx = dynsymcount;
961 }
962 else if (do_sec)
963 elf_section_data (p)->dynindx = 0;
964 }
965 if (do_sec)
966 *section_sym_count = dynsymcount;
967
968 elf_link_hash_traverse (elf_hash_table (info),
969 elf_link_renumber_local_hash_table_dynsyms,
970 &dynsymcount);
971
972 if (elf_hash_table (info)->dynlocal)
973 {
974 struct elf_link_local_dynamic_entry *p;
975 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
976 p->dynindx = ++dynsymcount;
977 }
978 elf_hash_table (info)->local_dynsymcount = dynsymcount;
979
980 elf_link_hash_traverse (elf_hash_table (info),
981 elf_link_renumber_hash_table_dynsyms,
982 &dynsymcount);
983
984 /* There is an unused NULL entry at the head of the table which we
985 must account for in our count even if the table is empty since it
986 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
987 .dynamic section. */
988 dynsymcount++;
989
990 elf_hash_table (info)->dynsymcount = dynsymcount;
991 return dynsymcount;
992}
993
994/* Merge st_other field. */
995
996static void
997elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
998 const Elf_Internal_Sym *isym, asection *sec,
999 bfd_boolean definition, bfd_boolean dynamic)
1000{
1001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1002
1003 /* If st_other has a processor-specific meaning, specific
1004 code might be needed here. */
1005 if (bed->elf_backend_merge_symbol_attribute)
1006 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1007 dynamic);
1008
1009 if (!dynamic)
1010 {
1011 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1012 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1013
1014 /* Keep the most constraining visibility. Leave the remainder
1015 of the st_other field to elf_backend_merge_symbol_attribute. */
1016 if (symvis - 1 < hvis - 1)
1017 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1018 }
1019 else if (definition
1020 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1021 && (sec->flags & SEC_READONLY) == 0)
1022 h->protected_def = 1;
1023}
1024
1025/* This function is called when we want to merge a new symbol with an
1026 existing symbol. It handles the various cases which arise when we
1027 find a definition in a dynamic object, or when there is already a
1028 definition in a dynamic object. The new symbol is described by
1029 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1030 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1031 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1032 of an old common symbol. We set OVERRIDE if the old symbol is
1033 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1034 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1035 to change. By OK to change, we mean that we shouldn't warn if the
1036 type or size does change. */
1037
1038static bfd_boolean
1039_bfd_elf_merge_symbol (bfd *abfd,
1040 struct bfd_link_info *info,
1041 const char *name,
1042 Elf_Internal_Sym *sym,
1043 asection **psec,
1044 bfd_vma *pvalue,
1045 struct elf_link_hash_entry **sym_hash,
1046 bfd **poldbfd,
1047 bfd_boolean *pold_weak,
1048 unsigned int *pold_alignment,
1049 bfd_boolean *skip,
1050 bfd_boolean *override,
1051 bfd_boolean *type_change_ok,
1052 bfd_boolean *size_change_ok,
1053 bfd_boolean *matched)
1054{
1055 asection *sec, *oldsec;
1056 struct elf_link_hash_entry *h;
1057 struct elf_link_hash_entry *hi;
1058 struct elf_link_hash_entry *flip;
1059 int bind;
1060 bfd *oldbfd;
1061 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1062 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1063 const struct elf_backend_data *bed;
1064 char *new_version;
1065 bfd_boolean default_sym = *matched;
1066
1067 *skip = FALSE;
1068 *override = FALSE;
1069
1070 sec = *psec;
1071 bind = ELF_ST_BIND (sym->st_info);
1072
1073 if (! bfd_is_und_section (sec))
1074 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1075 else
1076 h = ((struct elf_link_hash_entry *)
1077 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1078 if (h == NULL)
1079 return FALSE;
1080 *sym_hash = h;
1081
1082 bed = get_elf_backend_data (abfd);
1083
1084 /* NEW_VERSION is the symbol version of the new symbol. */
1085 if (h->versioned != unversioned)
1086 {
1087 /* Symbol version is unknown or versioned. */
1088 new_version = strrchr (name, ELF_VER_CHR);
1089 if (new_version)
1090 {
1091 if (h->versioned == unknown)
1092 {
1093 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1094 h->versioned = versioned_hidden;
1095 else
1096 h->versioned = versioned;
1097 }
1098 new_version += 1;
1099 if (new_version[0] == '\0')
1100 new_version = NULL;
1101 }
1102 else
1103 h->versioned = unversioned;
1104 }
1105 else
1106 new_version = NULL;
1107
1108 /* For merging, we only care about real symbols. But we need to make
1109 sure that indirect symbol dynamic flags are updated. */
1110 hi = h;
1111 while (h->root.type == bfd_link_hash_indirect
1112 || h->root.type == bfd_link_hash_warning)
1113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1114
1115 if (!*matched)
1116 {
1117 if (hi == h || h->root.type == bfd_link_hash_new)
1118 *matched = TRUE;
1119 else
1120 {
1121 /* OLD_HIDDEN is true if the existing symbol is only visible
1122 to the symbol with the same symbol version. NEW_HIDDEN is
1123 true if the new symbol is only visible to the symbol with
1124 the same symbol version. */
1125 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1126 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1127 if (!old_hidden && !new_hidden)
1128 /* The new symbol matches the existing symbol if both
1129 aren't hidden. */
1130 *matched = TRUE;
1131 else
1132 {
1133 /* OLD_VERSION is the symbol version of the existing
1134 symbol. */
1135 char *old_version;
1136
1137 if (h->versioned >= versioned)
1138 old_version = strrchr (h->root.root.string,
1139 ELF_VER_CHR) + 1;
1140 else
1141 old_version = NULL;
1142
1143 /* The new symbol matches the existing symbol if they
1144 have the same symbol version. */
1145 *matched = (old_version == new_version
1146 || (old_version != NULL
1147 && new_version != NULL
1148 && strcmp (old_version, new_version) == 0));
1149 }
1150 }
1151 }
1152
1153 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1154 existing symbol. */
1155
1156 oldbfd = NULL;
1157 oldsec = NULL;
1158 switch (h->root.type)
1159 {
1160 default:
1161 break;
1162
1163 case bfd_link_hash_undefined:
1164 case bfd_link_hash_undefweak:
1165 oldbfd = h->root.u.undef.abfd;
1166 break;
1167
1168 case bfd_link_hash_defined:
1169 case bfd_link_hash_defweak:
1170 oldbfd = h->root.u.def.section->owner;
1171 oldsec = h->root.u.def.section;
1172 break;
1173
1174 case bfd_link_hash_common:
1175 oldbfd = h->root.u.c.p->section->owner;
1176 oldsec = h->root.u.c.p->section;
1177 if (pold_alignment)
1178 *pold_alignment = h->root.u.c.p->alignment_power;
1179 break;
1180 }
1181 if (poldbfd && *poldbfd == NULL)
1182 *poldbfd = oldbfd;
1183
1184 /* Differentiate strong and weak symbols. */
1185 newweak = bind == STB_WEAK;
1186 oldweak = (h->root.type == bfd_link_hash_defweak
1187 || h->root.type == bfd_link_hash_undefweak);
1188 if (pold_weak)
1189 *pold_weak = oldweak;
1190
1191 /* We have to check it for every instance since the first few may be
1192 references and not all compilers emit symbol type for undefined
1193 symbols. */
1194 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1195
1196 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1197 respectively, is from a dynamic object. */
1198
1199 newdyn = (abfd->flags & DYNAMIC) != 0;
1200
1201 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1202 syms and defined syms in dynamic libraries respectively.
1203 ref_dynamic on the other hand can be set for a symbol defined in
1204 a dynamic library, and def_dynamic may not be set; When the
1205 definition in a dynamic lib is overridden by a definition in the
1206 executable use of the symbol in the dynamic lib becomes a
1207 reference to the executable symbol. */
1208 if (newdyn)
1209 {
1210 if (bfd_is_und_section (sec))
1211 {
1212 if (bind != STB_WEAK)
1213 {
1214 h->ref_dynamic_nonweak = 1;
1215 hi->ref_dynamic_nonweak = 1;
1216 }
1217 }
1218 else
1219 {
1220 /* Update the existing symbol only if they match. */
1221 if (*matched)
1222 h->dynamic_def = 1;
1223 hi->dynamic_def = 1;
1224 }
1225 }
1226
1227 /* If we just created the symbol, mark it as being an ELF symbol.
1228 Other than that, there is nothing to do--there is no merge issue
1229 with a newly defined symbol--so we just return. */
1230
1231 if (h->root.type == bfd_link_hash_new)
1232 {
1233 h->non_elf = 0;
1234 return TRUE;
1235 }
1236
1237 /* In cases involving weak versioned symbols, we may wind up trying
1238 to merge a symbol with itself. Catch that here, to avoid the
1239 confusion that results if we try to override a symbol with
1240 itself. The additional tests catch cases like
1241 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1242 dynamic object, which we do want to handle here. */
1243 if (abfd == oldbfd
1244 && (newweak || oldweak)
1245 && ((abfd->flags & DYNAMIC) == 0
1246 || !h->def_regular))
1247 return TRUE;
1248
1249 olddyn = FALSE;
1250 if (oldbfd != NULL)
1251 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1252 else if (oldsec != NULL)
1253 {
1254 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1255 indices used by MIPS ELF. */
1256 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1257 }
1258
1259 /* Handle a case where plugin_notice won't be called and thus won't
1260 set the non_ir_ref flags on the first pass over symbols. */
1261 if (oldbfd != NULL
1262 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1263 && newdyn != olddyn)
1264 {
1265 h->root.non_ir_ref_dynamic = TRUE;
1266 hi->root.non_ir_ref_dynamic = TRUE;
1267 }
1268
1269 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1270 respectively, appear to be a definition rather than reference. */
1271
1272 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1273
1274 olddef = (h->root.type != bfd_link_hash_undefined
1275 && h->root.type != bfd_link_hash_undefweak
1276 && h->root.type != bfd_link_hash_common);
1277
1278 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1279 respectively, appear to be a function. */
1280
1281 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1282 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1283
1284 oldfunc = (h->type != STT_NOTYPE
1285 && bed->is_function_type (h->type));
1286
1287 if (!(newfunc && oldfunc)
1288 && ELF_ST_TYPE (sym->st_info) != h->type
1289 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1290 && h->type != STT_NOTYPE
1291 && (newdef || bfd_is_com_section (sec))
1292 && (olddef || h->root.type == bfd_link_hash_common))
1293 {
1294 /* If creating a default indirect symbol ("foo" or "foo@") from
1295 a dynamic versioned definition ("foo@@") skip doing so if
1296 there is an existing regular definition with a different
1297 type. We don't want, for example, a "time" variable in the
1298 executable overriding a "time" function in a shared library. */
1299 if (newdyn
1300 && !olddyn)
1301 {
1302 *skip = TRUE;
1303 return TRUE;
1304 }
1305
1306 /* When adding a symbol from a regular object file after we have
1307 created indirect symbols, undo the indirection and any
1308 dynamic state. */
1309 if (hi != h
1310 && !newdyn
1311 && olddyn)
1312 {
1313 h = hi;
1314 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1315 h->forced_local = 0;
1316 h->ref_dynamic = 0;
1317 h->def_dynamic = 0;
1318 h->dynamic_def = 0;
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 {
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329 return TRUE;
1330 }
1331 }
1332
1333 /* Check TLS symbols. We don't check undefined symbols introduced
1334 by "ld -u" which have no type (and oldbfd NULL), and we don't
1335 check symbols from plugins because they also have no type. */
1336 if (oldbfd != NULL
1337 && (oldbfd->flags & BFD_PLUGIN) == 0
1338 && (abfd->flags & BFD_PLUGIN) == 0
1339 && ELF_ST_TYPE (sym->st_info) != h->type
1340 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1341 {
1342 bfd *ntbfd, *tbfd;
1343 bfd_boolean ntdef, tdef;
1344 asection *ntsec, *tsec;
1345
1346 if (h->type == STT_TLS)
1347 {
1348 ntbfd = abfd;
1349 ntsec = sec;
1350 ntdef = newdef;
1351 tbfd = oldbfd;
1352 tsec = oldsec;
1353 tdef = olddef;
1354 }
1355 else
1356 {
1357 ntbfd = oldbfd;
1358 ntsec = oldsec;
1359 ntdef = olddef;
1360 tbfd = abfd;
1361 tsec = sec;
1362 tdef = newdef;
1363 }
1364
1365 if (tdef && ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS definition in %pB section %pA "
1369 "mismatches non-TLS definition in %pB section %pA"),
1370 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1371 else if (!tdef && !ntdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS reference in %pB "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, ntbfd);
1377 else if (tdef)
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS definition in %pB section %pA "
1381 "mismatches non-TLS reference in %pB"),
1382 h->root.root.string, tbfd, tsec, ntbfd);
1383 else
1384 _bfd_error_handler
1385 /* xgettext:c-format */
1386 (_("%s: TLS reference in %pB "
1387 "mismatches non-TLS definition in %pB section %pA"),
1388 h->root.root.string, tbfd, ntbfd, ntsec);
1389
1390 bfd_set_error (bfd_error_bad_value);
1391 return FALSE;
1392 }
1393
1394 /* If the old symbol has non-default visibility, we ignore the new
1395 definition from a dynamic object. */
1396 if (newdyn
1397 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1398 && !bfd_is_und_section (sec))
1399 {
1400 *skip = TRUE;
1401 /* Make sure this symbol is dynamic. */
1402 h->ref_dynamic = 1;
1403 hi->ref_dynamic = 1;
1404 /* A protected symbol has external availability. Make sure it is
1405 recorded as dynamic.
1406
1407 FIXME: Should we check type and size for protected symbol? */
1408 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1409 return bfd_elf_link_record_dynamic_symbol (info, h);
1410 else
1411 return TRUE;
1412 }
1413 else if (!newdyn
1414 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1415 && h->def_dynamic)
1416 {
1417 /* If the new symbol with non-default visibility comes from a
1418 relocatable file and the old definition comes from a dynamic
1419 object, we remove the old definition. */
1420 if (hi->root.type == bfd_link_hash_indirect)
1421 {
1422 /* Handle the case where the old dynamic definition is
1423 default versioned. We need to copy the symbol info from
1424 the symbol with default version to the normal one if it
1425 was referenced before. */
1426 if (h->ref_regular)
1427 {
1428 hi->root.type = h->root.type;
1429 h->root.type = bfd_link_hash_indirect;
1430 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1431
1432 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1433 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1434 {
1435 /* If the new symbol is hidden or internal, completely undo
1436 any dynamic link state. */
1437 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1438 h->forced_local = 0;
1439 h->ref_dynamic = 0;
1440 }
1441 else
1442 h->ref_dynamic = 1;
1443
1444 h->def_dynamic = 0;
1445 /* FIXME: Should we check type and size for protected symbol? */
1446 h->size = 0;
1447 h->type = 0;
1448
1449 h = hi;
1450 }
1451 else
1452 h = hi;
1453 }
1454
1455 /* If the old symbol was undefined before, then it will still be
1456 on the undefs list. If the new symbol is undefined or
1457 common, we can't make it bfd_link_hash_new here, because new
1458 undefined or common symbols will be added to the undefs list
1459 by _bfd_generic_link_add_one_symbol. Symbols may not be
1460 added twice to the undefs list. Also, if the new symbol is
1461 undefweak then we don't want to lose the strong undef. */
1462 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1463 {
1464 h->root.type = bfd_link_hash_undefined;
1465 h->root.u.undef.abfd = abfd;
1466 }
1467 else
1468 {
1469 h->root.type = bfd_link_hash_new;
1470 h->root.u.undef.abfd = NULL;
1471 }
1472
1473 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1474 {
1475 /* If the new symbol is hidden or internal, completely undo
1476 any dynamic link state. */
1477 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1478 h->forced_local = 0;
1479 h->ref_dynamic = 0;
1480 }
1481 else
1482 h->ref_dynamic = 1;
1483 h->def_dynamic = 0;
1484 /* FIXME: Should we check type and size for protected symbol? */
1485 h->size = 0;
1486 h->type = 0;
1487 return TRUE;
1488 }
1489
1490 /* If a new weak symbol definition comes from a regular file and the
1491 old symbol comes from a dynamic library, we treat the new one as
1492 strong. Similarly, an old weak symbol definition from a regular
1493 file is treated as strong when the new symbol comes from a dynamic
1494 library. Further, an old weak symbol from a dynamic library is
1495 treated as strong if the new symbol is from a dynamic library.
1496 This reflects the way glibc's ld.so works.
1497
1498 Also allow a weak symbol to override a linker script symbol
1499 defined by an early pass over the script. This is done so the
1500 linker knows the symbol is defined in an object file, for the
1501 DEFINED script function.
1502
1503 Do this before setting *type_change_ok or *size_change_ok so that
1504 we warn properly when dynamic library symbols are overridden. */
1505
1506 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1507 newweak = FALSE;
1508 if (olddef && newdyn)
1509 oldweak = FALSE;
1510
1511 /* Allow changes between different types of function symbol. */
1512 if (newfunc && oldfunc)
1513 *type_change_ok = TRUE;
1514
1515 /* It's OK to change the type if either the existing symbol or the
1516 new symbol is weak. A type change is also OK if the old symbol
1517 is undefined and the new symbol is defined. */
1518
1519 if (oldweak
1520 || newweak
1521 || (newdef
1522 && h->root.type == bfd_link_hash_undefined))
1523 *type_change_ok = TRUE;
1524
1525 /* It's OK to change the size if either the existing symbol or the
1526 new symbol is weak, or if the old symbol is undefined. */
1527
1528 if (*type_change_ok
1529 || h->root.type == bfd_link_hash_undefined)
1530 *size_change_ok = TRUE;
1531
1532 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1533 symbol, respectively, appears to be a common symbol in a dynamic
1534 object. If a symbol appears in an uninitialized section, and is
1535 not weak, and is not a function, then it may be a common symbol
1536 which was resolved when the dynamic object was created. We want
1537 to treat such symbols specially, because they raise special
1538 considerations when setting the symbol size: if the symbol
1539 appears as a common symbol in a regular object, and the size in
1540 the regular object is larger, we must make sure that we use the
1541 larger size. This problematic case can always be avoided in C,
1542 but it must be handled correctly when using Fortran shared
1543 libraries.
1544
1545 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1546 likewise for OLDDYNCOMMON and OLDDEF.
1547
1548 Note that this test is just a heuristic, and that it is quite
1549 possible to have an uninitialized symbol in a shared object which
1550 is really a definition, rather than a common symbol. This could
1551 lead to some minor confusion when the symbol really is a common
1552 symbol in some regular object. However, I think it will be
1553 harmless. */
1554
1555 if (newdyn
1556 && newdef
1557 && !newweak
1558 && (sec->flags & SEC_ALLOC) != 0
1559 && (sec->flags & SEC_LOAD) == 0
1560 && sym->st_size > 0
1561 && !newfunc)
1562 newdyncommon = TRUE;
1563 else
1564 newdyncommon = FALSE;
1565
1566 if (olddyn
1567 && olddef
1568 && h->root.type == bfd_link_hash_defined
1569 && h->def_dynamic
1570 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1571 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1572 && h->size > 0
1573 && !oldfunc)
1574 olddyncommon = TRUE;
1575 else
1576 olddyncommon = FALSE;
1577
1578 /* We now know everything about the old and new symbols. We ask the
1579 backend to check if we can merge them. */
1580 if (bed->merge_symbol != NULL)
1581 {
1582 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1583 return FALSE;
1584 sec = *psec;
1585 }
1586
1587 /* There are multiple definitions of a normal symbol. Skip the
1588 default symbol as well as definition from an IR object. */
1589 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1590 && !default_sym && h->def_regular
1591 && !(oldbfd != NULL
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
1593 && (abfd->flags & BFD_PLUGIN) == 0))
1594 {
1595 /* Handle a multiple definition. */
1596 (*info->callbacks->multiple_definition) (info, &h->root,
1597 abfd, sec, *pvalue);
1598 *skip = TRUE;
1599 return TRUE;
1600 }
1601
1602 /* If both the old and the new symbols look like common symbols in a
1603 dynamic object, set the size of the symbol to the larger of the
1604 two. */
1605
1606 if (olddyncommon
1607 && newdyncommon
1608 && sym->st_size != h->size)
1609 {
1610 /* Since we think we have two common symbols, issue a multiple
1611 common warning if desired. Note that we only warn if the
1612 size is different. If the size is the same, we simply let
1613 the old symbol override the new one as normally happens with
1614 symbols defined in dynamic objects. */
1615
1616 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1617 bfd_link_hash_common, sym->st_size);
1618 if (sym->st_size > h->size)
1619 h->size = sym->st_size;
1620
1621 *size_change_ok = TRUE;
1622 }
1623
1624 /* If we are looking at a dynamic object, and we have found a
1625 definition, we need to see if the symbol was already defined by
1626 some other object. If so, we want to use the existing
1627 definition, and we do not want to report a multiple symbol
1628 definition error; we do this by clobbering *PSEC to be
1629 bfd_und_section_ptr.
1630
1631 We treat a common symbol as a definition if the symbol in the
1632 shared library is a function, since common symbols always
1633 represent variables; this can cause confusion in principle, but
1634 any such confusion would seem to indicate an erroneous program or
1635 shared library. We also permit a common symbol in a regular
1636 object to override a weak symbol in a shared object. */
1637
1638 if (newdyn
1639 && newdef
1640 && (olddef
1641 || (h->root.type == bfd_link_hash_common
1642 && (newweak || newfunc))))
1643 {
1644 *override = TRUE;
1645 newdef = FALSE;
1646 newdyncommon = FALSE;
1647
1648 *psec = sec = bfd_und_section_ptr;
1649 *size_change_ok = TRUE;
1650
1651 /* If we get here when the old symbol is a common symbol, then
1652 we are explicitly letting it override a weak symbol or
1653 function in a dynamic object, and we don't want to warn about
1654 a type change. If the old symbol is a defined symbol, a type
1655 change warning may still be appropriate. */
1656
1657 if (h->root.type == bfd_link_hash_common)
1658 *type_change_ok = TRUE;
1659 }
1660
1661 /* Handle the special case of an old common symbol merging with a
1662 new symbol which looks like a common symbol in a shared object.
1663 We change *PSEC and *PVALUE to make the new symbol look like a
1664 common symbol, and let _bfd_generic_link_add_one_symbol do the
1665 right thing. */
1666
1667 if (newdyncommon
1668 && h->root.type == bfd_link_hash_common)
1669 {
1670 *override = TRUE;
1671 newdef = FALSE;
1672 newdyncommon = FALSE;
1673 *pvalue = sym->st_size;
1674 *psec = sec = bed->common_section (oldsec);
1675 *size_change_ok = TRUE;
1676 }
1677
1678 /* Skip weak definitions of symbols that are already defined. */
1679 if (newdef && olddef && newweak)
1680 {
1681 /* Don't skip new non-IR weak syms. */
1682 if (!(oldbfd != NULL
1683 && (oldbfd->flags & BFD_PLUGIN) != 0
1684 && (abfd->flags & BFD_PLUGIN) == 0))
1685 {
1686 newdef = FALSE;
1687 *skip = TRUE;
1688 }
1689
1690 /* Merge st_other. If the symbol already has a dynamic index,
1691 but visibility says it should not be visible, turn it into a
1692 local symbol. */
1693 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1694 if (h->dynindx != -1)
1695 switch (ELF_ST_VISIBILITY (h->other))
1696 {
1697 case STV_INTERNAL:
1698 case STV_HIDDEN:
1699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1700 break;
1701 }
1702 }
1703
1704 /* If the old symbol is from a dynamic object, and the new symbol is
1705 a definition which is not from a dynamic object, then the new
1706 symbol overrides the old symbol. Symbols from regular files
1707 always take precedence over symbols from dynamic objects, even if
1708 they are defined after the dynamic object in the link.
1709
1710 As above, we again permit a common symbol in a regular object to
1711 override a definition in a shared object if the shared object
1712 symbol is a function or is weak. */
1713
1714 flip = NULL;
1715 if (!newdyn
1716 && (newdef
1717 || (bfd_is_com_section (sec)
1718 && (oldweak || oldfunc)))
1719 && olddyn
1720 && olddef
1721 && h->def_dynamic)
1722 {
1723 /* Change the hash table entry to undefined, and let
1724 _bfd_generic_link_add_one_symbol do the right thing with the
1725 new definition. */
1726
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729 *size_change_ok = TRUE;
1730
1731 olddef = FALSE;
1732 olddyncommon = FALSE;
1733
1734 /* We again permit a type change when a common symbol may be
1735 overriding a function. */
1736
1737 if (bfd_is_com_section (sec))
1738 {
1739 if (oldfunc)
1740 {
1741 /* If a common symbol overrides a function, make sure
1742 that it isn't defined dynamically nor has type
1743 function. */
1744 h->def_dynamic = 0;
1745 h->type = STT_NOTYPE;
1746 }
1747 *type_change_ok = TRUE;
1748 }
1749
1750 if (hi->root.type == bfd_link_hash_indirect)
1751 flip = hi;
1752 else
1753 /* This union may have been set to be non-NULL when this symbol
1754 was seen in a dynamic object. We must force the union to be
1755 NULL, so that it is correct for a regular symbol. */
1756 h->verinfo.vertree = NULL;
1757 }
1758
1759 /* Handle the special case of a new common symbol merging with an
1760 old symbol that looks like it might be a common symbol defined in
1761 a shared object. Note that we have already handled the case in
1762 which a new common symbol should simply override the definition
1763 in the shared library. */
1764
1765 if (! newdyn
1766 && bfd_is_com_section (sec)
1767 && olddyncommon)
1768 {
1769 /* It would be best if we could set the hash table entry to a
1770 common symbol, but we don't know what to use for the section
1771 or the alignment. */
1772 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1773 bfd_link_hash_common, sym->st_size);
1774
1775 /* If the presumed common symbol in the dynamic object is
1776 larger, pretend that the new symbol has its size. */
1777
1778 if (h->size > *pvalue)
1779 *pvalue = h->size;
1780
1781 /* We need to remember the alignment required by the symbol
1782 in the dynamic object. */
1783 BFD_ASSERT (pold_alignment);
1784 *pold_alignment = h->root.u.def.section->alignment_power;
1785
1786 olddef = FALSE;
1787 olddyncommon = FALSE;
1788
1789 h->root.type = bfd_link_hash_undefined;
1790 h->root.u.undef.abfd = h->root.u.def.section->owner;
1791
1792 *size_change_ok = TRUE;
1793 *type_change_ok = TRUE;
1794
1795 if (hi->root.type == bfd_link_hash_indirect)
1796 flip = hi;
1797 else
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 if (flip != NULL)
1802 {
1803 /* Handle the case where we had a versioned symbol in a dynamic
1804 library and now find a definition in a normal object. In this
1805 case, we make the versioned symbol point to the normal one. */
1806 flip->root.type = h->root.type;
1807 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1810 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1811 if (h->def_dynamic)
1812 {
1813 h->def_dynamic = 0;
1814 flip->ref_dynamic = 1;
1815 }
1816 }
1817
1818 return TRUE;
1819}
1820
1821/* This function is called to create an indirect symbol from the
1822 default for the symbol with the default version if needed. The
1823 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1824 set DYNSYM if the new indirect symbol is dynamic. */
1825
1826static bfd_boolean
1827_bfd_elf_add_default_symbol (bfd *abfd,
1828 struct bfd_link_info *info,
1829 struct elf_link_hash_entry *h,
1830 const char *name,
1831 Elf_Internal_Sym *sym,
1832 asection *sec,
1833 bfd_vma value,
1834 bfd **poldbfd,
1835 bfd_boolean *dynsym)
1836{
1837 bfd_boolean type_change_ok;
1838 bfd_boolean size_change_ok;
1839 bfd_boolean skip;
1840 char *shortname;
1841 struct elf_link_hash_entry *hi;
1842 struct bfd_link_hash_entry *bh;
1843 const struct elf_backend_data *bed;
1844 bfd_boolean collect;
1845 bfd_boolean dynamic;
1846 bfd_boolean override;
1847 char *p;
1848 size_t len, shortlen;
1849 asection *tmp_sec;
1850 bfd_boolean matched;
1851
1852 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1853 return TRUE;
1854
1855 /* If this symbol has a version, and it is the default version, we
1856 create an indirect symbol from the default name to the fully
1857 decorated name. This will cause external references which do not
1858 specify a version to be bound to this version of the symbol. */
1859 p = strchr (name, ELF_VER_CHR);
1860 if (h->versioned == unknown)
1861 {
1862 if (p == NULL)
1863 {
1864 h->versioned = unversioned;
1865 return TRUE;
1866 }
1867 else
1868 {
1869 if (p[1] != ELF_VER_CHR)
1870 {
1871 h->versioned = versioned_hidden;
1872 return TRUE;
1873 }
1874 else
1875 h->versioned = versioned;
1876 }
1877 }
1878 else
1879 {
1880 /* PR ld/19073: We may see an unversioned definition after the
1881 default version. */
1882 if (p == NULL)
1883 return TRUE;
1884 }
1885
1886 bed = get_elf_backend_data (abfd);
1887 collect = bed->collect;
1888 dynamic = (abfd->flags & DYNAMIC) != 0;
1889
1890 shortlen = p - name;
1891 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1892 if (shortname == NULL)
1893 return FALSE;
1894 memcpy (shortname, name, shortlen);
1895 shortname[shortlen] = '\0';
1896
1897 /* We are going to create a new symbol. Merge it with any existing
1898 symbol with this name. For the purposes of the merge, act as
1899 though we were defining the symbol we just defined, although we
1900 actually going to define an indirect symbol. */
1901 type_change_ok = FALSE;
1902 size_change_ok = FALSE;
1903 matched = TRUE;
1904 tmp_sec = sec;
1905 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1906 &hi, poldbfd, NULL, NULL, &skip, &override,
1907 &type_change_ok, &size_change_ok, &matched))
1908 return FALSE;
1909
1910 if (skip)
1911 goto nondefault;
1912
1913 if (hi->def_regular)
1914 {
1915 /* If the undecorated symbol will have a version added by a
1916 script different to H, then don't indirect to/from the
1917 undecorated symbol. This isn't ideal because we may not yet
1918 have seen symbol versions, if given by a script on the
1919 command line rather than via --version-script. */
1920 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1921 {
1922 bfd_boolean hide;
1923
1924 hi->verinfo.vertree
1925 = bfd_find_version_for_sym (info->version_info,
1926 hi->root.root.string, &hide);
1927 if (hi->verinfo.vertree != NULL && hide)
1928 {
1929 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1930 goto nondefault;
1931 }
1932 }
1933 if (hi->verinfo.vertree != NULL
1934 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1935 goto nondefault;
1936 }
1937
1938 if (! override)
1939 {
1940 /* Add the default symbol if not performing a relocatable link. */
1941 if (! bfd_link_relocatable (info))
1942 {
1943 bh = &hi->root;
1944 if (! (_bfd_generic_link_add_one_symbol
1945 (info, abfd, shortname, BSF_INDIRECT,
1946 bfd_ind_section_ptr,
1947 0, name, FALSE, collect, &bh)))
1948 return FALSE;
1949 hi = (struct elf_link_hash_entry *) bh;
1950 }
1951 }
1952 else
1953 {
1954 /* In this case the symbol named SHORTNAME is overriding the
1955 indirect symbol we want to add. We were planning on making
1956 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1957 is the name without a version. NAME is the fully versioned
1958 name, and it is the default version.
1959
1960 Overriding means that we already saw a definition for the
1961 symbol SHORTNAME in a regular object, and it is overriding
1962 the symbol defined in the dynamic object.
1963
1964 When this happens, we actually want to change NAME, the
1965 symbol we just added, to refer to SHORTNAME. This will cause
1966 references to NAME in the shared object to become references
1967 to SHORTNAME in the regular object. This is what we expect
1968 when we override a function in a shared object: that the
1969 references in the shared object will be mapped to the
1970 definition in the regular object. */
1971
1972 while (hi->root.type == bfd_link_hash_indirect
1973 || hi->root.type == bfd_link_hash_warning)
1974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1975
1976 h->root.type = bfd_link_hash_indirect;
1977 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1978 if (h->def_dynamic)
1979 {
1980 h->def_dynamic = 0;
1981 hi->ref_dynamic = 1;
1982 if (hi->ref_regular
1983 || hi->def_regular)
1984 {
1985 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1986 return FALSE;
1987 }
1988 }
1989
1990 /* Now set HI to H, so that the following code will set the
1991 other fields correctly. */
1992 hi = h;
1993 }
1994
1995 /* Check if HI is a warning symbol. */
1996 if (hi->root.type == bfd_link_hash_warning)
1997 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1998
1999 /* If there is a duplicate definition somewhere, then HI may not
2000 point to an indirect symbol. We will have reported an error to
2001 the user in that case. */
2002
2003 if (hi->root.type == bfd_link_hash_indirect)
2004 {
2005 struct elf_link_hash_entry *ht;
2006
2007 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2009
2010 /* A reference to the SHORTNAME symbol from a dynamic library
2011 will be satisfied by the versioned symbol at runtime. In
2012 effect, we have a reference to the versioned symbol. */
2013 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2014 hi->dynamic_def |= ht->dynamic_def;
2015
2016 /* See if the new flags lead us to realize that the symbol must
2017 be dynamic. */
2018 if (! *dynsym)
2019 {
2020 if (! dynamic)
2021 {
2022 if (! bfd_link_executable (info)
2023 || hi->def_dynamic
2024 || hi->ref_dynamic)
2025 *dynsym = TRUE;
2026 }
2027 else
2028 {
2029 if (hi->ref_regular)
2030 *dynsym = TRUE;
2031 }
2032 }
2033 }
2034
2035 /* We also need to define an indirection from the nondefault version
2036 of the symbol. */
2037
2038nondefault:
2039 len = strlen (name);
2040 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2041 if (shortname == NULL)
2042 return FALSE;
2043 memcpy (shortname, name, shortlen);
2044 memcpy (shortname + shortlen, p + 1, len - shortlen);
2045
2046 /* Once again, merge with any existing symbol. */
2047 type_change_ok = FALSE;
2048 size_change_ok = FALSE;
2049 tmp_sec = sec;
2050 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2051 &hi, poldbfd, NULL, NULL, &skip, &override,
2052 &type_change_ok, &size_change_ok, &matched))
2053 return FALSE;
2054
2055 if (skip)
2056 return TRUE;
2057
2058 if (override)
2059 {
2060 /* Here SHORTNAME is a versioned name, so we don't expect to see
2061 the type of override we do in the case above unless it is
2062 overridden by a versioned definition. */
2063 if (hi->root.type != bfd_link_hash_defined
2064 && hi->root.type != bfd_link_hash_defweak)
2065 _bfd_error_handler
2066 /* xgettext:c-format */
2067 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2068 abfd, shortname);
2069 }
2070 else
2071 {
2072 bh = &hi->root;
2073 if (! (_bfd_generic_link_add_one_symbol
2074 (info, abfd, shortname, BSF_INDIRECT,
2075 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2076 return FALSE;
2077 hi = (struct elf_link_hash_entry *) bh;
2078
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error
2081 to the user in that case. */
2082
2083 if (hi->root.type == bfd_link_hash_indirect)
2084 {
2085 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2086 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2087 hi->dynamic_def |= h->dynamic_def;
2088
2089 /* See if the new flags lead us to realize that the symbol
2090 must be dynamic. */
2091 if (! *dynsym)
2092 {
2093 if (! dynamic)
2094 {
2095 if (! bfd_link_executable (info)
2096 || hi->ref_dynamic)
2097 *dynsym = TRUE;
2098 }
2099 else
2100 {
2101 if (hi->ref_regular)
2102 *dynsym = TRUE;
2103 }
2104 }
2105 }
2106 }
2107
2108 return TRUE;
2109}
2110\f
2111/* This routine is used to export all defined symbols into the dynamic
2112 symbol table. It is called via elf_link_hash_traverse. */
2113
2114static bfd_boolean
2115_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2116{
2117 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2118
2119 /* Ignore indirect symbols. These are added by the versioning code. */
2120 if (h->root.type == bfd_link_hash_indirect)
2121 return TRUE;
2122
2123 /* Ignore this if we won't export it. */
2124 if (!eif->info->export_dynamic && !h->dynamic)
2125 return TRUE;
2126
2127 if (h->dynindx == -1
2128 && (h->def_regular || h->ref_regular)
2129 && ! bfd_hide_sym_by_version (eif->info->version_info,
2130 h->root.root.string))
2131 {
2132 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2133 {
2134 eif->failed = TRUE;
2135 return FALSE;
2136 }
2137 }
2138
2139 return TRUE;
2140}
2141\f
2142/* Look through the symbols which are defined in other shared
2143 libraries and referenced here. Update the list of version
2144 dependencies. This will be put into the .gnu.version_r section.
2145 This function is called via elf_link_hash_traverse. */
2146
2147static bfd_boolean
2148_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2149 void *data)
2150{
2151 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2152 Elf_Internal_Verneed *t;
2153 Elf_Internal_Vernaux *a;
2154 bfd_size_type amt;
2155
2156 /* We only care about symbols defined in shared objects with version
2157 information. */
2158 if (!h->def_dynamic
2159 || h->def_regular
2160 || h->dynindx == -1
2161 || h->verinfo.verdef == NULL
2162 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2163 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2164 return TRUE;
2165
2166 /* See if we already know about this version. */
2167 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2168 t != NULL;
2169 t = t->vn_nextref)
2170 {
2171 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2172 continue;
2173
2174 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2175 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2176 return TRUE;
2177
2178 break;
2179 }
2180
2181 /* This is a new version. Add it to tree we are building. */
2182
2183 if (t == NULL)
2184 {
2185 amt = sizeof *t;
2186 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2187 if (t == NULL)
2188 {
2189 rinfo->failed = TRUE;
2190 return FALSE;
2191 }
2192
2193 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2194 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2195 elf_tdata (rinfo->info->output_bfd)->verref = t;
2196 }
2197
2198 amt = sizeof *a;
2199 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2200 if (a == NULL)
2201 {
2202 rinfo->failed = TRUE;
2203 return FALSE;
2204 }
2205
2206 /* Note that we are copying a string pointer here, and testing it
2207 above. If bfd_elf_string_from_elf_section is ever changed to
2208 discard the string data when low in memory, this will have to be
2209 fixed. */
2210 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2211
2212 a->vna_flags = h->verinfo.verdef->vd_flags;
2213 a->vna_nextptr = t->vn_auxptr;
2214
2215 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2216 ++rinfo->vers;
2217
2218 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2219
2220 t->vn_auxptr = a;
2221
2222 return TRUE;
2223}
2224
2225/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2226 hidden. Set *T_P to NULL if there is no match. */
2227
2228static bfd_boolean
2229_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2230 struct elf_link_hash_entry *h,
2231 const char *version_p,
2232 struct bfd_elf_version_tree **t_p,
2233 bfd_boolean *hide)
2234{
2235 struct bfd_elf_version_tree *t;
2236
2237 /* Look for the version. If we find it, it is no longer weak. */
2238 for (t = info->version_info; t != NULL; t = t->next)
2239 {
2240 if (strcmp (t->name, version_p) == 0)
2241 {
2242 size_t len;
2243 char *alc;
2244 struct bfd_elf_version_expr *d;
2245
2246 len = version_p - h->root.root.string;
2247 alc = (char *) bfd_malloc (len);
2248 if (alc == NULL)
2249 return FALSE;
2250 memcpy (alc, h->root.root.string, len - 1);
2251 alc[len - 1] = '\0';
2252 if (alc[len - 2] == ELF_VER_CHR)
2253 alc[len - 2] = '\0';
2254
2255 h->verinfo.vertree = t;
2256 t->used = TRUE;
2257 d = NULL;
2258
2259 if (t->globals.list != NULL)
2260 d = (*t->match) (&t->globals, NULL, alc);
2261
2262 /* See if there is anything to force this symbol to
2263 local scope. */
2264 if (d == NULL && t->locals.list != NULL)
2265 {
2266 d = (*t->match) (&t->locals, NULL, alc);
2267 if (d != NULL
2268 && h->dynindx != -1
2269 && ! info->export_dynamic)
2270 *hide = TRUE;
2271 }
2272
2273 free (alc);
2274 break;
2275 }
2276 }
2277
2278 *t_p = t;
2279
2280 return TRUE;
2281}
2282
2283/* Return TRUE if the symbol H is hidden by version script. */
2284
2285bfd_boolean
2286_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2287 struct elf_link_hash_entry *h)
2288{
2289 const char *p;
2290 bfd_boolean hide = FALSE;
2291 const struct elf_backend_data *bed
2292 = get_elf_backend_data (info->output_bfd);
2293
2294 /* Version script only hides symbols defined in regular objects. */
2295 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2296 return TRUE;
2297
2298 p = strchr (h->root.root.string, ELF_VER_CHR);
2299 if (p != NULL && h->verinfo.vertree == NULL)
2300 {
2301 struct bfd_elf_version_tree *t;
2302
2303 ++p;
2304 if (*p == ELF_VER_CHR)
2305 ++p;
2306
2307 if (*p != '\0'
2308 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2309 && hide)
2310 {
2311 if (hide)
2312 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2313 return TRUE;
2314 }
2315 }
2316
2317 /* If we don't have a version for this symbol, see if we can find
2318 something. */
2319 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2320 {
2321 h->verinfo.vertree
2322 = bfd_find_version_for_sym (info->version_info,
2323 h->root.root.string, &hide);
2324 if (h->verinfo.vertree != NULL && hide)
2325 {
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2327 return TRUE;
2328 }
2329 }
2330
2331 return FALSE;
2332}
2333
2334/* Figure out appropriate versions for all the symbols. We may not
2335 have the version number script until we have read all of the input
2336 files, so until that point we don't know which symbols should be
2337 local. This function is called via elf_link_hash_traverse. */
2338
2339static bfd_boolean
2340_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2341{
2342 struct elf_info_failed *sinfo;
2343 struct bfd_link_info *info;
2344 const struct elf_backend_data *bed;
2345 struct elf_info_failed eif;
2346 char *p;
2347 bfd_boolean hide;
2348
2349 sinfo = (struct elf_info_failed *) data;
2350 info = sinfo->info;
2351
2352 /* Fix the symbol flags. */
2353 eif.failed = FALSE;
2354 eif.info = info;
2355 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2356 {
2357 if (eif.failed)
2358 sinfo->failed = TRUE;
2359 return FALSE;
2360 }
2361
2362 /* We only need version numbers for symbols defined in regular
2363 objects. */
2364 if (!h->def_regular)
2365 return TRUE;
2366
2367 hide = FALSE;
2368 bed = get_elf_backend_data (info->output_bfd);
2369 p = strchr (h->root.root.string, ELF_VER_CHR);
2370 if (p != NULL && h->verinfo.vertree == NULL)
2371 {
2372 struct bfd_elf_version_tree *t;
2373
2374 ++p;
2375 if (*p == ELF_VER_CHR)
2376 ++p;
2377
2378 /* If there is no version string, we can just return out. */
2379 if (*p == '\0')
2380 return TRUE;
2381
2382 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2383 {
2384 sinfo->failed = TRUE;
2385 return FALSE;
2386 }
2387
2388 if (hide)
2389 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2390
2391 /* If we are building an application, we need to create a
2392 version node for this version. */
2393 if (t == NULL && bfd_link_executable (info))
2394 {
2395 struct bfd_elf_version_tree **pp;
2396 int version_index;
2397
2398 /* If we aren't going to export this symbol, we don't need
2399 to worry about it. */
2400 if (h->dynindx == -1)
2401 return TRUE;
2402
2403 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2404 sizeof *t);
2405 if (t == NULL)
2406 {
2407 sinfo->failed = TRUE;
2408 return FALSE;
2409 }
2410
2411 t->name = p;
2412 t->name_indx = (unsigned int) -1;
2413 t->used = TRUE;
2414
2415 version_index = 1;
2416 /* Don't count anonymous version tag. */
2417 if (sinfo->info->version_info != NULL
2418 && sinfo->info->version_info->vernum == 0)
2419 version_index = 0;
2420 for (pp = &sinfo->info->version_info;
2421 *pp != NULL;
2422 pp = &(*pp)->next)
2423 ++version_index;
2424 t->vernum = version_index;
2425
2426 *pp = t;
2427
2428 h->verinfo.vertree = t;
2429 }
2430 else if (t == NULL)
2431 {
2432 /* We could not find the version for a symbol when
2433 generating a shared archive. Return an error. */
2434 _bfd_error_handler
2435 /* xgettext:c-format */
2436 (_("%pB: version node not found for symbol %s"),
2437 info->output_bfd, h->root.root.string);
2438 bfd_set_error (bfd_error_bad_value);
2439 sinfo->failed = TRUE;
2440 return FALSE;
2441 }
2442 }
2443
2444 /* If we don't have a version for this symbol, see if we can find
2445 something. */
2446 if (!hide
2447 && h->verinfo.vertree == NULL
2448 && sinfo->info->version_info != NULL)
2449 {
2450 h->verinfo.vertree
2451 = bfd_find_version_for_sym (sinfo->info->version_info,
2452 h->root.root.string, &hide);
2453 if (h->verinfo.vertree != NULL && hide)
2454 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2455 }
2456
2457 return TRUE;
2458}
2459\f
2460/* Read and swap the relocs from the section indicated by SHDR. This
2461 may be either a REL or a RELA section. The relocations are
2462 translated into RELA relocations and stored in INTERNAL_RELOCS,
2463 which should have already been allocated to contain enough space.
2464 The EXTERNAL_RELOCS are a buffer where the external form of the
2465 relocations should be stored.
2466
2467 Returns FALSE if something goes wrong. */
2468
2469static bfd_boolean
2470elf_link_read_relocs_from_section (bfd *abfd,
2471 asection *sec,
2472 Elf_Internal_Shdr *shdr,
2473 void *external_relocs,
2474 Elf_Internal_Rela *internal_relocs)
2475{
2476 const struct elf_backend_data *bed;
2477 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2478 const bfd_byte *erela;
2479 const bfd_byte *erelaend;
2480 Elf_Internal_Rela *irela;
2481 Elf_Internal_Shdr *symtab_hdr;
2482 size_t nsyms;
2483
2484 /* Position ourselves at the start of the section. */
2485 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2486 return FALSE;
2487
2488 /* Read the relocations. */
2489 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2490 return FALSE;
2491
2492 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2493 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2494
2495 bed = get_elf_backend_data (abfd);
2496
2497 /* Convert the external relocations to the internal format. */
2498 if (shdr->sh_entsize == bed->s->sizeof_rel)
2499 swap_in = bed->s->swap_reloc_in;
2500 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2501 swap_in = bed->s->swap_reloca_in;
2502 else
2503 {
2504 bfd_set_error (bfd_error_wrong_format);
2505 return FALSE;
2506 }
2507
2508 erela = (const bfd_byte *) external_relocs;
2509 erelaend = erela + shdr->sh_size;
2510 irela = internal_relocs;
2511 while (erela < erelaend)
2512 {
2513 bfd_vma r_symndx;
2514
2515 (*swap_in) (abfd, erela, irela);
2516 r_symndx = ELF32_R_SYM (irela->r_info);
2517 if (bed->s->arch_size == 64)
2518 r_symndx >>= 24;
2519 if (nsyms > 0)
2520 {
2521 if ((size_t) r_symndx >= nsyms)
2522 {
2523 _bfd_error_handler
2524 /* xgettext:c-format */
2525 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2526 " for offset %#" PRIx64 " in section `%pA'"),
2527 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2528 (uint64_t) irela->r_offset, sec);
2529 bfd_set_error (bfd_error_bad_value);
2530 return FALSE;
2531 }
2532 }
2533 else if (r_symndx != STN_UNDEF)
2534 {
2535 _bfd_error_handler
2536 /* xgettext:c-format */
2537 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2538 " for offset %#" PRIx64 " in section `%pA'"
2539 " when the object file has no symbol table"),
2540 abfd, (uint64_t) r_symndx,
2541 (uint64_t) irela->r_offset, sec);
2542 bfd_set_error (bfd_error_bad_value);
2543 return FALSE;
2544 }
2545 irela += bed->s->int_rels_per_ext_rel;
2546 erela += shdr->sh_entsize;
2547 }
2548
2549 return TRUE;
2550}
2551
2552/* Read and swap the relocs for a section O. They may have been
2553 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2554 not NULL, they are used as buffers to read into. They are known to
2555 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2556 the return value is allocated using either malloc or bfd_alloc,
2557 according to the KEEP_MEMORY argument. If O has two relocation
2558 sections (both REL and RELA relocations), then the REL_HDR
2559 relocations will appear first in INTERNAL_RELOCS, followed by the
2560 RELA_HDR relocations. */
2561
2562Elf_Internal_Rela *
2563_bfd_elf_link_read_relocs (bfd *abfd,
2564 asection *o,
2565 void *external_relocs,
2566 Elf_Internal_Rela *internal_relocs,
2567 bfd_boolean keep_memory)
2568{
2569 void *alloc1 = NULL;
2570 Elf_Internal_Rela *alloc2 = NULL;
2571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2572 struct bfd_elf_section_data *esdo = elf_section_data (o);
2573 Elf_Internal_Rela *internal_rela_relocs;
2574
2575 if (esdo->relocs != NULL)
2576 return esdo->relocs;
2577
2578 if (o->reloc_count == 0)
2579 return NULL;
2580
2581 if (internal_relocs == NULL)
2582 {
2583 bfd_size_type size;
2584
2585 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2586 if (keep_memory)
2587 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2588 else
2589 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2590 if (internal_relocs == NULL)
2591 goto error_return;
2592 }
2593
2594 if (external_relocs == NULL)
2595 {
2596 bfd_size_type size = 0;
2597
2598 if (esdo->rel.hdr)
2599 size += esdo->rel.hdr->sh_size;
2600 if (esdo->rela.hdr)
2601 size += esdo->rela.hdr->sh_size;
2602
2603 alloc1 = bfd_malloc (size);
2604 if (alloc1 == NULL)
2605 goto error_return;
2606 external_relocs = alloc1;
2607 }
2608
2609 internal_rela_relocs = internal_relocs;
2610 if (esdo->rel.hdr)
2611 {
2612 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2613 external_relocs,
2614 internal_relocs))
2615 goto error_return;
2616 external_relocs = (((bfd_byte *) external_relocs)
2617 + esdo->rel.hdr->sh_size);
2618 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2619 * bed->s->int_rels_per_ext_rel);
2620 }
2621
2622 if (esdo->rela.hdr
2623 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2624 external_relocs,
2625 internal_rela_relocs)))
2626 goto error_return;
2627
2628 /* Cache the results for next time, if we can. */
2629 if (keep_memory)
2630 esdo->relocs = internal_relocs;
2631
2632 if (alloc1 != NULL)
2633 free (alloc1);
2634
2635 /* Don't free alloc2, since if it was allocated we are passing it
2636 back (under the name of internal_relocs). */
2637
2638 return internal_relocs;
2639
2640 error_return:
2641 if (alloc1 != NULL)
2642 free (alloc1);
2643 if (alloc2 != NULL)
2644 {
2645 if (keep_memory)
2646 bfd_release (abfd, alloc2);
2647 else
2648 free (alloc2);
2649 }
2650 return NULL;
2651}
2652
2653/* Compute the size of, and allocate space for, REL_HDR which is the
2654 section header for a section containing relocations for O. */
2655
2656static bfd_boolean
2657_bfd_elf_link_size_reloc_section (bfd *abfd,
2658 struct bfd_elf_section_reloc_data *reldata)
2659{
2660 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2661
2662 /* That allows us to calculate the size of the section. */
2663 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2664
2665 /* The contents field must last into write_object_contents, so we
2666 allocate it with bfd_alloc rather than malloc. Also since we
2667 cannot be sure that the contents will actually be filled in,
2668 we zero the allocated space. */
2669 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2670 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2671 return FALSE;
2672
2673 if (reldata->hashes == NULL && reldata->count)
2674 {
2675 struct elf_link_hash_entry **p;
2676
2677 p = ((struct elf_link_hash_entry **)
2678 bfd_zmalloc (reldata->count * sizeof (*p)));
2679 if (p == NULL)
2680 return FALSE;
2681
2682 reldata->hashes = p;
2683 }
2684
2685 return TRUE;
2686}
2687
2688/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2689 originated from the section given by INPUT_REL_HDR) to the
2690 OUTPUT_BFD. */
2691
2692bfd_boolean
2693_bfd_elf_link_output_relocs (bfd *output_bfd,
2694 asection *input_section,
2695 Elf_Internal_Shdr *input_rel_hdr,
2696 Elf_Internal_Rela *internal_relocs,
2697 struct elf_link_hash_entry **rel_hash
2698 ATTRIBUTE_UNUSED)
2699{
2700 Elf_Internal_Rela *irela;
2701 Elf_Internal_Rela *irelaend;
2702 bfd_byte *erel;
2703 struct bfd_elf_section_reloc_data *output_reldata;
2704 asection *output_section;
2705 const struct elf_backend_data *bed;
2706 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2707 struct bfd_elf_section_data *esdo;
2708
2709 output_section = input_section->output_section;
2710
2711 bed = get_elf_backend_data (output_bfd);
2712 esdo = elf_section_data (output_section);
2713 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2714 {
2715 output_reldata = &esdo->rel;
2716 swap_out = bed->s->swap_reloc_out;
2717 }
2718 else if (esdo->rela.hdr
2719 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2720 {
2721 output_reldata = &esdo->rela;
2722 swap_out = bed->s->swap_reloca_out;
2723 }
2724 else
2725 {
2726 _bfd_error_handler
2727 /* xgettext:c-format */
2728 (_("%pB: relocation size mismatch in %pB section %pA"),
2729 output_bfd, input_section->owner, input_section);
2730 bfd_set_error (bfd_error_wrong_format);
2731 return FALSE;
2732 }
2733
2734 erel = output_reldata->hdr->contents;
2735 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2736 irela = internal_relocs;
2737 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2738 * bed->s->int_rels_per_ext_rel);
2739 while (irela < irelaend)
2740 {
2741 (*swap_out) (output_bfd, irela, erel);
2742 irela += bed->s->int_rels_per_ext_rel;
2743 erel += input_rel_hdr->sh_entsize;
2744 }
2745
2746 /* Bump the counter, so that we know where to add the next set of
2747 relocations. */
2748 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2749
2750 return TRUE;
2751}
2752\f
2753/* Make weak undefined symbols in PIE dynamic. */
2754
2755bfd_boolean
2756_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2757 struct elf_link_hash_entry *h)
2758{
2759 if (bfd_link_pie (info)
2760 && h->dynindx == -1
2761 && h->root.type == bfd_link_hash_undefweak)
2762 return bfd_elf_link_record_dynamic_symbol (info, h);
2763
2764 return TRUE;
2765}
2766
2767/* Fix up the flags for a symbol. This handles various cases which
2768 can only be fixed after all the input files are seen. This is
2769 currently called by both adjust_dynamic_symbol and
2770 assign_sym_version, which is unnecessary but perhaps more robust in
2771 the face of future changes. */
2772
2773static bfd_boolean
2774_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2775 struct elf_info_failed *eif)
2776{
2777 const struct elf_backend_data *bed;
2778
2779 /* If this symbol was mentioned in a non-ELF file, try to set
2780 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2781 permit a non-ELF file to correctly refer to a symbol defined in
2782 an ELF dynamic object. */
2783 if (h->non_elf)
2784 {
2785 while (h->root.type == bfd_link_hash_indirect)
2786 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2787
2788 if (h->root.type != bfd_link_hash_defined
2789 && h->root.type != bfd_link_hash_defweak)
2790 {
2791 h->ref_regular = 1;
2792 h->ref_regular_nonweak = 1;
2793 }
2794 else
2795 {
2796 if (h->root.u.def.section->owner != NULL
2797 && (bfd_get_flavour (h->root.u.def.section->owner)
2798 == bfd_target_elf_flavour))
2799 {
2800 h->ref_regular = 1;
2801 h->ref_regular_nonweak = 1;
2802 }
2803 else
2804 h->def_regular = 1;
2805 }
2806
2807 if (h->dynindx == -1
2808 && (h->def_dynamic
2809 || h->ref_dynamic))
2810 {
2811 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2812 {
2813 eif->failed = TRUE;
2814 return FALSE;
2815 }
2816 }
2817 }
2818 else
2819 {
2820 /* Unfortunately, NON_ELF is only correct if the symbol
2821 was first seen in a non-ELF file. Fortunately, if the symbol
2822 was first seen in an ELF file, we're probably OK unless the
2823 symbol was defined in a non-ELF file. Catch that case here.
2824 FIXME: We're still in trouble if the symbol was first seen in
2825 a dynamic object, and then later in a non-ELF regular object. */
2826 if ((h->root.type == bfd_link_hash_defined
2827 || h->root.type == bfd_link_hash_defweak)
2828 && !h->def_regular
2829 && (h->root.u.def.section->owner != NULL
2830 ? (bfd_get_flavour (h->root.u.def.section->owner)
2831 != bfd_target_elf_flavour)
2832 : (bfd_is_abs_section (h->root.u.def.section)
2833 && !h->def_dynamic)))
2834 h->def_regular = 1;
2835 }
2836
2837 /* Backend specific symbol fixup. */
2838 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2839 if (bed->elf_backend_fixup_symbol
2840 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2841 return FALSE;
2842
2843 /* If this is a final link, and the symbol was defined as a common
2844 symbol in a regular object file, and there was no definition in
2845 any dynamic object, then the linker will have allocated space for
2846 the symbol in a common section but the DEF_REGULAR
2847 flag will not have been set. */
2848 if (h->root.type == bfd_link_hash_defined
2849 && !h->def_regular
2850 && h->ref_regular
2851 && !h->def_dynamic
2852 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2853 h->def_regular = 1;
2854
2855 /* If a weak undefined symbol has non-default visibility, we also
2856 hide it from the dynamic linker. */
2857 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2858 && h->root.type == bfd_link_hash_undefweak)
2859 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2860
2861 /* A hidden versioned symbol in executable should be forced local if
2862 it is is locally defined, not referenced by shared library and not
2863 exported. */
2864 else if (bfd_link_executable (eif->info)
2865 && h->versioned == versioned_hidden
2866 && !eif->info->export_dynamic
2867 && !h->dynamic
2868 && !h->ref_dynamic
2869 && h->def_regular)
2870 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2871
2872 /* If -Bsymbolic was used (which means to bind references to global
2873 symbols to the definition within the shared object), and this
2874 symbol was defined in a regular object, then it actually doesn't
2875 need a PLT entry. Likewise, if the symbol has non-default
2876 visibility. If the symbol has hidden or internal visibility, we
2877 will force it local. */
2878 else if (h->needs_plt
2879 && bfd_link_pic (eif->info)
2880 && is_elf_hash_table (eif->info->hash)
2881 && (SYMBOLIC_BIND (eif->info, h)
2882 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2883 && h->def_regular)
2884 {
2885 bfd_boolean force_local;
2886
2887 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2888 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2889 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2890 }
2891
2892 /* If this is a weak defined symbol in a dynamic object, and we know
2893 the real definition in the dynamic object, copy interesting flags
2894 over to the real definition. */
2895 if (h->is_weakalias)
2896 {
2897 struct elf_link_hash_entry *def = weakdef (h);
2898
2899 /* If the real definition is defined by a regular object file,
2900 don't do anything special. See the longer description in
2901 _bfd_elf_adjust_dynamic_symbol, below. */
2902 if (def->def_regular)
2903 {
2904 h = def;
2905 while ((h = h->u.alias) != def)
2906 h->is_weakalias = 0;
2907 }
2908 else
2909 {
2910 while (h->root.type == bfd_link_hash_indirect)
2911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2912 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2913 || h->root.type == bfd_link_hash_defweak);
2914 BFD_ASSERT (def->def_dynamic);
2915 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2916 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2917 }
2918 }
2919
2920 return TRUE;
2921}
2922
2923/* Make the backend pick a good value for a dynamic symbol. This is
2924 called via elf_link_hash_traverse, and also calls itself
2925 recursively. */
2926
2927static bfd_boolean
2928_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2929{
2930 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2931 struct elf_link_hash_table *htab;
2932 const struct elf_backend_data *bed;
2933
2934 if (! is_elf_hash_table (eif->info->hash))
2935 return FALSE;
2936
2937 /* Ignore indirect symbols. These are added by the versioning code. */
2938 if (h->root.type == bfd_link_hash_indirect)
2939 return TRUE;
2940
2941 /* Fix the symbol flags. */
2942 if (! _bfd_elf_fix_symbol_flags (h, eif))
2943 return FALSE;
2944
2945 htab = elf_hash_table (eif->info);
2946 bed = get_elf_backend_data (htab->dynobj);
2947
2948 if (h->root.type == bfd_link_hash_undefweak)
2949 {
2950 if (eif->info->dynamic_undefined_weak == 0)
2951 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2952 else if (eif->info->dynamic_undefined_weak > 0
2953 && h->ref_regular
2954 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2955 && !bfd_hide_sym_by_version (eif->info->version_info,
2956 h->root.root.string))
2957 {
2958 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2959 {
2960 eif->failed = TRUE;
2961 return FALSE;
2962 }
2963 }
2964 }
2965
2966 /* If this symbol does not require a PLT entry, and it is not
2967 defined by a dynamic object, or is not referenced by a regular
2968 object, ignore it. We do have to handle a weak defined symbol,
2969 even if no regular object refers to it, if we decided to add it
2970 to the dynamic symbol table. FIXME: Do we normally need to worry
2971 about symbols which are defined by one dynamic object and
2972 referenced by another one? */
2973 if (!h->needs_plt
2974 && h->type != STT_GNU_IFUNC
2975 && (h->def_regular
2976 || !h->def_dynamic
2977 || (!h->ref_regular
2978 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2979 {
2980 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2981 return TRUE;
2982 }
2983
2984 /* If we've already adjusted this symbol, don't do it again. This
2985 can happen via a recursive call. */
2986 if (h->dynamic_adjusted)
2987 return TRUE;
2988
2989 /* Don't look at this symbol again. Note that we must set this
2990 after checking the above conditions, because we may look at a
2991 symbol once, decide not to do anything, and then get called
2992 recursively later after REF_REGULAR is set below. */
2993 h->dynamic_adjusted = 1;
2994
2995 /* If this is a weak definition, and we know a real definition, and
2996 the real symbol is not itself defined by a regular object file,
2997 then get a good value for the real definition. We handle the
2998 real symbol first, for the convenience of the backend routine.
2999
3000 Note that there is a confusing case here. If the real definition
3001 is defined by a regular object file, we don't get the real symbol
3002 from the dynamic object, but we do get the weak symbol. If the
3003 processor backend uses a COPY reloc, then if some routine in the
3004 dynamic object changes the real symbol, we will not see that
3005 change in the corresponding weak symbol. This is the way other
3006 ELF linkers work as well, and seems to be a result of the shared
3007 library model.
3008
3009 I will clarify this issue. Most SVR4 shared libraries define the
3010 variable _timezone and define timezone as a weak synonym. The
3011 tzset call changes _timezone. If you write
3012 extern int timezone;
3013 int _timezone = 5;
3014 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3015 you might expect that, since timezone is a synonym for _timezone,
3016 the same number will print both times. However, if the processor
3017 backend uses a COPY reloc, then actually timezone will be copied
3018 into your process image, and, since you define _timezone
3019 yourself, _timezone will not. Thus timezone and _timezone will
3020 wind up at different memory locations. The tzset call will set
3021 _timezone, leaving timezone unchanged. */
3022
3023 if (h->is_weakalias)
3024 {
3025 struct elf_link_hash_entry *def = weakdef (h);
3026
3027 /* If we get to this point, there is an implicit reference to
3028 the alias by a regular object file via the weak symbol H. */
3029 def->ref_regular = 1;
3030
3031 /* Ensure that the backend adjust_dynamic_symbol function sees
3032 the strong alias before H by recursively calling ourselves. */
3033 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3034 return FALSE;
3035 }
3036
3037 /* If a symbol has no type and no size and does not require a PLT
3038 entry, then we are probably about to do the wrong thing here: we
3039 are probably going to create a COPY reloc for an empty object.
3040 This case can arise when a shared object is built with assembly
3041 code, and the assembly code fails to set the symbol type. */
3042 if (h->size == 0
3043 && h->type == STT_NOTYPE
3044 && !h->needs_plt)
3045 _bfd_error_handler
3046 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3047 h->root.root.string);
3048
3049 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3050 {
3051 eif->failed = TRUE;
3052 return FALSE;
3053 }
3054
3055 return TRUE;
3056}
3057
3058/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3059 DYNBSS. */
3060
3061bfd_boolean
3062_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3063 struct elf_link_hash_entry *h,
3064 asection *dynbss)
3065{
3066 unsigned int power_of_two;
3067 bfd_vma mask;
3068 asection *sec = h->root.u.def.section;
3069
3070 /* The section alignment of the definition is the maximum alignment
3071 requirement of symbols defined in the section. Since we don't
3072 know the symbol alignment requirement, we start with the
3073 maximum alignment and check low bits of the symbol address
3074 for the minimum alignment. */
3075 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3076 mask = ((bfd_vma) 1 << power_of_two) - 1;
3077 while ((h->root.u.def.value & mask) != 0)
3078 {
3079 mask >>= 1;
3080 --power_of_two;
3081 }
3082
3083 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3084 dynbss))
3085 {
3086 /* Adjust the section alignment if needed. */
3087 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3088 power_of_two))
3089 return FALSE;
3090 }
3091
3092 /* We make sure that the symbol will be aligned properly. */
3093 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3094
3095 /* Define the symbol as being at this point in DYNBSS. */
3096 h->root.u.def.section = dynbss;
3097 h->root.u.def.value = dynbss->size;
3098
3099 /* Increment the size of DYNBSS to make room for the symbol. */
3100 dynbss->size += h->size;
3101
3102 /* No error if extern_protected_data is true. */
3103 if (h->protected_def
3104 && (!info->extern_protected_data
3105 || (info->extern_protected_data < 0
3106 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3107 info->callbacks->einfo
3108 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3109 h->root.root.string);
3110
3111 return TRUE;
3112}
3113
3114/* Adjust all external symbols pointing into SEC_MERGE sections
3115 to reflect the object merging within the sections. */
3116
3117static bfd_boolean
3118_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3119{
3120 asection *sec;
3121
3122 if ((h->root.type == bfd_link_hash_defined
3123 || h->root.type == bfd_link_hash_defweak)
3124 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3125 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3126 {
3127 bfd *output_bfd = (bfd *) data;
3128
3129 h->root.u.def.value =
3130 _bfd_merged_section_offset (output_bfd,
3131 &h->root.u.def.section,
3132 elf_section_data (sec)->sec_info,
3133 h->root.u.def.value);
3134 }
3135
3136 return TRUE;
3137}
3138
3139/* Returns false if the symbol referred to by H should be considered
3140 to resolve local to the current module, and true if it should be
3141 considered to bind dynamically. */
3142
3143bfd_boolean
3144_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3145 struct bfd_link_info *info,
3146 bfd_boolean not_local_protected)
3147{
3148 bfd_boolean binding_stays_local_p;
3149 const struct elf_backend_data *bed;
3150 struct elf_link_hash_table *hash_table;
3151
3152 if (h == NULL)
3153 return FALSE;
3154
3155 while (h->root.type == bfd_link_hash_indirect
3156 || h->root.type == bfd_link_hash_warning)
3157 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3158
3159 /* If it was forced local, then clearly it's not dynamic. */
3160 if (h->dynindx == -1)
3161 return FALSE;
3162 if (h->forced_local)
3163 return FALSE;
3164
3165 /* Identify the cases where name binding rules say that a
3166 visible symbol resolves locally. */
3167 binding_stays_local_p = (bfd_link_executable (info)
3168 || SYMBOLIC_BIND (info, h));
3169
3170 switch (ELF_ST_VISIBILITY (h->other))
3171 {
3172 case STV_INTERNAL:
3173 case STV_HIDDEN:
3174 return FALSE;
3175
3176 case STV_PROTECTED:
3177 hash_table = elf_hash_table (info);
3178 if (!is_elf_hash_table (hash_table))
3179 return FALSE;
3180
3181 bed = get_elf_backend_data (hash_table->dynobj);
3182
3183 /* Proper resolution for function pointer equality may require
3184 that these symbols perhaps be resolved dynamically, even though
3185 we should be resolving them to the current module. */
3186 if (!not_local_protected || !bed->is_function_type (h->type))
3187 binding_stays_local_p = TRUE;
3188 break;
3189
3190 default:
3191 break;
3192 }
3193
3194 /* If it isn't defined locally, then clearly it's dynamic. */
3195 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3196 return TRUE;
3197
3198 /* Otherwise, the symbol is dynamic if binding rules don't tell
3199 us that it remains local. */
3200 return !binding_stays_local_p;
3201}
3202
3203/* Return true if the symbol referred to by H should be considered
3204 to resolve local to the current module, and false otherwise. Differs
3205 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3206 undefined symbols. The two functions are virtually identical except
3207 for the place where dynindx == -1 is tested. If that test is true,
3208 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3209 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3210 defined symbols.
3211 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3212 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3213 treatment of undefined weak symbols. For those that do not make
3214 undefined weak symbols dynamic, both functions may return false. */
3215
3216bfd_boolean
3217_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3218 struct bfd_link_info *info,
3219 bfd_boolean local_protected)
3220{
3221 const struct elf_backend_data *bed;
3222 struct elf_link_hash_table *hash_table;
3223
3224 /* If it's a local sym, of course we resolve locally. */
3225 if (h == NULL)
3226 return TRUE;
3227
3228 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3229 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3230 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3231 return TRUE;
3232
3233 /* Forced local symbols resolve locally. */
3234 if (h->forced_local)
3235 return TRUE;
3236
3237 /* Common symbols that become definitions don't get the DEF_REGULAR
3238 flag set, so test it first, and don't bail out. */
3239 if (ELF_COMMON_DEF_P (h))
3240 /* Do nothing. */;
3241 /* If we don't have a definition in a regular file, then we can't
3242 resolve locally. The sym is either undefined or dynamic. */
3243 else if (!h->def_regular)
3244 return FALSE;
3245
3246 /* Non-dynamic symbols resolve locally. */
3247 if (h->dynindx == -1)
3248 return TRUE;
3249
3250 /* At this point, we know the symbol is defined and dynamic. In an
3251 executable it must resolve locally, likewise when building symbolic
3252 shared libraries. */
3253 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3254 return TRUE;
3255
3256 /* Now deal with defined dynamic symbols in shared libraries. Ones
3257 with default visibility might not resolve locally. */
3258 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3259 return FALSE;
3260
3261 hash_table = elf_hash_table (info);
3262 if (!is_elf_hash_table (hash_table))
3263 return TRUE;
3264
3265 bed = get_elf_backend_data (hash_table->dynobj);
3266
3267 /* If extern_protected_data is false, STV_PROTECTED non-function
3268 symbols are local. */
3269 if ((!info->extern_protected_data
3270 || (info->extern_protected_data < 0
3271 && !bed->extern_protected_data))
3272 && !bed->is_function_type (h->type))
3273 return TRUE;
3274
3275 /* Function pointer equality tests may require that STV_PROTECTED
3276 symbols be treated as dynamic symbols. If the address of a
3277 function not defined in an executable is set to that function's
3278 plt entry in the executable, then the address of the function in
3279 a shared library must also be the plt entry in the executable. */
3280 return local_protected;
3281}
3282
3283/* Caches some TLS segment info, and ensures that the TLS segment vma is
3284 aligned. Returns the first TLS output section. */
3285
3286struct bfd_section *
3287_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3288{
3289 struct bfd_section *sec, *tls;
3290 unsigned int align = 0;
3291
3292 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3293 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3294 break;
3295 tls = sec;
3296
3297 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3298 if (sec->alignment_power > align)
3299 align = sec->alignment_power;
3300
3301 elf_hash_table (info)->tls_sec = tls;
3302
3303 /* Ensure the alignment of the first section is the largest alignment,
3304 so that the tls segment starts aligned. */
3305 if (tls != NULL)
3306 tls->alignment_power = align;
3307
3308 return tls;
3309}
3310
3311/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3312static bfd_boolean
3313is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3314 Elf_Internal_Sym *sym)
3315{
3316 const struct elf_backend_data *bed;
3317
3318 /* Local symbols do not count, but target specific ones might. */
3319 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3320 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3321 return FALSE;
3322
3323 bed = get_elf_backend_data (abfd);
3324 /* Function symbols do not count. */
3325 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3326 return FALSE;
3327
3328 /* If the section is undefined, then so is the symbol. */
3329 if (sym->st_shndx == SHN_UNDEF)
3330 return FALSE;
3331
3332 /* If the symbol is defined in the common section, then
3333 it is a common definition and so does not count. */
3334 if (bed->common_definition (sym))
3335 return FALSE;
3336
3337 /* If the symbol is in a target specific section then we
3338 must rely upon the backend to tell us what it is. */
3339 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3340 /* FIXME - this function is not coded yet:
3341
3342 return _bfd_is_global_symbol_definition (abfd, sym);
3343
3344 Instead for now assume that the definition is not global,
3345 Even if this is wrong, at least the linker will behave
3346 in the same way that it used to do. */
3347 return FALSE;
3348
3349 return TRUE;
3350}
3351
3352/* Search the symbol table of the archive element of the archive ABFD
3353 whose archive map contains a mention of SYMDEF, and determine if
3354 the symbol is defined in this element. */
3355static bfd_boolean
3356elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3357{
3358 Elf_Internal_Shdr * hdr;
3359 size_t symcount;
3360 size_t extsymcount;
3361 size_t extsymoff;
3362 Elf_Internal_Sym *isymbuf;
3363 Elf_Internal_Sym *isym;
3364 Elf_Internal_Sym *isymend;
3365 bfd_boolean result;
3366
3367 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3368 if (abfd == NULL)
3369 return FALSE;
3370
3371 if (! bfd_check_format (abfd, bfd_object))
3372 return FALSE;
3373
3374 /* Select the appropriate symbol table. If we don't know if the
3375 object file is an IR object, give linker LTO plugin a chance to
3376 get the correct symbol table. */
3377 if (abfd->plugin_format == bfd_plugin_yes
3378#if BFD_SUPPORTS_PLUGINS
3379 || (abfd->plugin_format == bfd_plugin_unknown
3380 && bfd_link_plugin_object_p (abfd))
3381#endif
3382 )
3383 {
3384 /* Use the IR symbol table if the object has been claimed by
3385 plugin. */
3386 abfd = abfd->plugin_dummy_bfd;
3387 hdr = &elf_tdata (abfd)->symtab_hdr;
3388 }
3389 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3390 hdr = &elf_tdata (abfd)->symtab_hdr;
3391 else
3392 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3393
3394 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3395
3396 /* The sh_info field of the symtab header tells us where the
3397 external symbols start. We don't care about the local symbols. */
3398 if (elf_bad_symtab (abfd))
3399 {
3400 extsymcount = symcount;
3401 extsymoff = 0;
3402 }
3403 else
3404 {
3405 extsymcount = symcount - hdr->sh_info;
3406 extsymoff = hdr->sh_info;
3407 }
3408
3409 if (extsymcount == 0)
3410 return FALSE;
3411
3412 /* Read in the symbol table. */
3413 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3414 NULL, NULL, NULL);
3415 if (isymbuf == NULL)
3416 return FALSE;
3417
3418 /* Scan the symbol table looking for SYMDEF. */
3419 result = FALSE;
3420 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3421 {
3422 const char *name;
3423
3424 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3425 isym->st_name);
3426 if (name == NULL)
3427 break;
3428
3429 if (strcmp (name, symdef->name) == 0)
3430 {
3431 result = is_global_data_symbol_definition (abfd, isym);
3432 break;
3433 }
3434 }
3435
3436 free (isymbuf);
3437
3438 return result;
3439}
3440\f
3441/* Add an entry to the .dynamic table. */
3442
3443bfd_boolean
3444_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3445 bfd_vma tag,
3446 bfd_vma val)
3447{
3448 struct elf_link_hash_table *hash_table;
3449 const struct elf_backend_data *bed;
3450 asection *s;
3451 bfd_size_type newsize;
3452 bfd_byte *newcontents;
3453 Elf_Internal_Dyn dyn;
3454
3455 hash_table = elf_hash_table (info);
3456 if (! is_elf_hash_table (hash_table))
3457 return FALSE;
3458
3459 bed = get_elf_backend_data (hash_table->dynobj);
3460 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3461 BFD_ASSERT (s != NULL);
3462
3463 newsize = s->size + bed->s->sizeof_dyn;
3464 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3465 if (newcontents == NULL)
3466 return FALSE;
3467
3468 dyn.d_tag = tag;
3469 dyn.d_un.d_val = val;
3470 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3471
3472 s->size = newsize;
3473 s->contents = newcontents;
3474
3475 return TRUE;
3476}
3477
3478/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3479 otherwise just check whether one already exists. Returns -1 on error,
3480 1 if a DT_NEEDED tag already exists, and 0 on success. */
3481
3482static int
3483elf_add_dt_needed_tag (bfd *abfd,
3484 struct bfd_link_info *info,
3485 const char *soname,
3486 bfd_boolean do_it)
3487{
3488 struct elf_link_hash_table *hash_table;
3489 size_t strindex;
3490
3491 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3492 return -1;
3493
3494 hash_table = elf_hash_table (info);
3495 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3496 if (strindex == (size_t) -1)
3497 return -1;
3498
3499 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3500 {
3501 asection *sdyn;
3502 const struct elf_backend_data *bed;
3503 bfd_byte *extdyn;
3504
3505 bed = get_elf_backend_data (hash_table->dynobj);
3506 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3507 if (sdyn != NULL)
3508 for (extdyn = sdyn->contents;
3509 extdyn < sdyn->contents + sdyn->size;
3510 extdyn += bed->s->sizeof_dyn)
3511 {
3512 Elf_Internal_Dyn dyn;
3513
3514 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3515 if (dyn.d_tag == DT_NEEDED
3516 && dyn.d_un.d_val == strindex)
3517 {
3518 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3519 return 1;
3520 }
3521 }
3522 }
3523
3524 if (do_it)
3525 {
3526 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3527 return -1;
3528
3529 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3530 return -1;
3531 }
3532 else
3533 /* We were just checking for existence of the tag. */
3534 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3535
3536 return 0;
3537}
3538
3539/* Return true if SONAME is on the needed list between NEEDED and STOP
3540 (or the end of list if STOP is NULL), and needed by a library that
3541 will be loaded. */
3542
3543static bfd_boolean
3544on_needed_list (const char *soname,
3545 struct bfd_link_needed_list *needed,
3546 struct bfd_link_needed_list *stop)
3547{
3548 struct bfd_link_needed_list *look;
3549 for (look = needed; look != stop; look = look->next)
3550 if (strcmp (soname, look->name) == 0
3551 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3552 /* If needed by a library that itself is not directly
3553 needed, recursively check whether that library is
3554 indirectly needed. Since we add DT_NEEDED entries to
3555 the end of the list, library dependencies appear after
3556 the library. Therefore search prior to the current
3557 LOOK, preventing possible infinite recursion. */
3558 || on_needed_list (elf_dt_name (look->by), needed, look)))
3559 return TRUE;
3560
3561 return FALSE;
3562}
3563
3564/* Sort symbol by value, section, and size. */
3565static int
3566elf_sort_symbol (const void *arg1, const void *arg2)
3567{
3568 const struct elf_link_hash_entry *h1;
3569 const struct elf_link_hash_entry *h2;
3570 bfd_signed_vma vdiff;
3571
3572 h1 = *(const struct elf_link_hash_entry **) arg1;
3573 h2 = *(const struct elf_link_hash_entry **) arg2;
3574 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3575 if (vdiff != 0)
3576 return vdiff > 0 ? 1 : -1;
3577 else
3578 {
3579 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3580 if (sdiff != 0)
3581 return sdiff > 0 ? 1 : -1;
3582 }
3583 vdiff = h1->size - h2->size;
3584 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3585}
3586
3587/* This function is used to adjust offsets into .dynstr for
3588 dynamic symbols. This is called via elf_link_hash_traverse. */
3589
3590static bfd_boolean
3591elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3592{
3593 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3594
3595 if (h->dynindx != -1)
3596 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3597 return TRUE;
3598}
3599
3600/* Assign string offsets in .dynstr, update all structures referencing
3601 them. */
3602
3603static bfd_boolean
3604elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3605{
3606 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3607 struct elf_link_local_dynamic_entry *entry;
3608 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3609 bfd *dynobj = hash_table->dynobj;
3610 asection *sdyn;
3611 bfd_size_type size;
3612 const struct elf_backend_data *bed;
3613 bfd_byte *extdyn;
3614
3615 _bfd_elf_strtab_finalize (dynstr);
3616 size = _bfd_elf_strtab_size (dynstr);
3617
3618 bed = get_elf_backend_data (dynobj);
3619 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3620 BFD_ASSERT (sdyn != NULL);
3621
3622 /* Update all .dynamic entries referencing .dynstr strings. */
3623 for (extdyn = sdyn->contents;
3624 extdyn < sdyn->contents + sdyn->size;
3625 extdyn += bed->s->sizeof_dyn)
3626 {
3627 Elf_Internal_Dyn dyn;
3628
3629 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3630 switch (dyn.d_tag)
3631 {
3632 case DT_STRSZ:
3633 dyn.d_un.d_val = size;
3634 break;
3635 case DT_NEEDED:
3636 case DT_SONAME:
3637 case DT_RPATH:
3638 case DT_RUNPATH:
3639 case DT_FILTER:
3640 case DT_AUXILIARY:
3641 case DT_AUDIT:
3642 case DT_DEPAUDIT:
3643 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3644 break;
3645 default:
3646 continue;
3647 }
3648 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3649 }
3650
3651 /* Now update local dynamic symbols. */
3652 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3653 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3654 entry->isym.st_name);
3655
3656 /* And the rest of dynamic symbols. */
3657 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3658
3659 /* Adjust version definitions. */
3660 if (elf_tdata (output_bfd)->cverdefs)
3661 {
3662 asection *s;
3663 bfd_byte *p;
3664 size_t i;
3665 Elf_Internal_Verdef def;
3666 Elf_Internal_Verdaux defaux;
3667
3668 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3669 p = s->contents;
3670 do
3671 {
3672 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3673 &def);
3674 p += sizeof (Elf_External_Verdef);
3675 if (def.vd_aux != sizeof (Elf_External_Verdef))
3676 continue;
3677 for (i = 0; i < def.vd_cnt; ++i)
3678 {
3679 _bfd_elf_swap_verdaux_in (output_bfd,
3680 (Elf_External_Verdaux *) p, &defaux);
3681 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3682 defaux.vda_name);
3683 _bfd_elf_swap_verdaux_out (output_bfd,
3684 &defaux, (Elf_External_Verdaux *) p);
3685 p += sizeof (Elf_External_Verdaux);
3686 }
3687 }
3688 while (def.vd_next);
3689 }
3690
3691 /* Adjust version references. */
3692 if (elf_tdata (output_bfd)->verref)
3693 {
3694 asection *s;
3695 bfd_byte *p;
3696 size_t i;
3697 Elf_Internal_Verneed need;
3698 Elf_Internal_Vernaux needaux;
3699
3700 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3701 p = s->contents;
3702 do
3703 {
3704 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3705 &need);
3706 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3707 _bfd_elf_swap_verneed_out (output_bfd, &need,
3708 (Elf_External_Verneed *) p);
3709 p += sizeof (Elf_External_Verneed);
3710 for (i = 0; i < need.vn_cnt; ++i)
3711 {
3712 _bfd_elf_swap_vernaux_in (output_bfd,
3713 (Elf_External_Vernaux *) p, &needaux);
3714 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3715 needaux.vna_name);
3716 _bfd_elf_swap_vernaux_out (output_bfd,
3717 &needaux,
3718 (Elf_External_Vernaux *) p);
3719 p += sizeof (Elf_External_Vernaux);
3720 }
3721 }
3722 while (need.vn_next);
3723 }
3724
3725 return TRUE;
3726}
3727\f
3728/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3729 The default is to only match when the INPUT and OUTPUT are exactly
3730 the same target. */
3731
3732bfd_boolean
3733_bfd_elf_default_relocs_compatible (const bfd_target *input,
3734 const bfd_target *output)
3735{
3736 return input == output;
3737}
3738
3739/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3740 This version is used when different targets for the same architecture
3741 are virtually identical. */
3742
3743bfd_boolean
3744_bfd_elf_relocs_compatible (const bfd_target *input,
3745 const bfd_target *output)
3746{
3747 const struct elf_backend_data *obed, *ibed;
3748
3749 if (input == output)
3750 return TRUE;
3751
3752 ibed = xvec_get_elf_backend_data (input);
3753 obed = xvec_get_elf_backend_data (output);
3754
3755 if (ibed->arch != obed->arch)
3756 return FALSE;
3757
3758 /* If both backends are using this function, deem them compatible. */
3759 return ibed->relocs_compatible == obed->relocs_compatible;
3760}
3761
3762/* Make a special call to the linker "notice" function to tell it that
3763 we are about to handle an as-needed lib, or have finished
3764 processing the lib. */
3765
3766bfd_boolean
3767_bfd_elf_notice_as_needed (bfd *ibfd,
3768 struct bfd_link_info *info,
3769 enum notice_asneeded_action act)
3770{
3771 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3772}
3773
3774/* Check relocations an ELF object file. */
3775
3776bfd_boolean
3777_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3778{
3779 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3780 struct elf_link_hash_table *htab = elf_hash_table (info);
3781
3782 /* If this object is the same format as the output object, and it is
3783 not a shared library, then let the backend look through the
3784 relocs.
3785
3786 This is required to build global offset table entries and to
3787 arrange for dynamic relocs. It is not required for the
3788 particular common case of linking non PIC code, even when linking
3789 against shared libraries, but unfortunately there is no way of
3790 knowing whether an object file has been compiled PIC or not.
3791 Looking through the relocs is not particularly time consuming.
3792 The problem is that we must either (1) keep the relocs in memory,
3793 which causes the linker to require additional runtime memory or
3794 (2) read the relocs twice from the input file, which wastes time.
3795 This would be a good case for using mmap.
3796
3797 I have no idea how to handle linking PIC code into a file of a
3798 different format. It probably can't be done. */
3799 if ((abfd->flags & DYNAMIC) == 0
3800 && is_elf_hash_table (htab)
3801 && bed->check_relocs != NULL
3802 && elf_object_id (abfd) == elf_hash_table_id (htab)
3803 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3804 {
3805 asection *o;
3806
3807 for (o = abfd->sections; o != NULL; o = o->next)
3808 {
3809 Elf_Internal_Rela *internal_relocs;
3810 bfd_boolean ok;
3811
3812 /* Don't check relocations in excluded sections. */
3813 if ((o->flags & SEC_RELOC) == 0
3814 || (o->flags & SEC_EXCLUDE) != 0
3815 || o->reloc_count == 0
3816 || ((info->strip == strip_all || info->strip == strip_debugger)
3817 && (o->flags & SEC_DEBUGGING) != 0)
3818 || bfd_is_abs_section (o->output_section))
3819 continue;
3820
3821 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3822 info->keep_memory);
3823 if (internal_relocs == NULL)
3824 return FALSE;
3825
3826 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3827
3828 if (elf_section_data (o)->relocs != internal_relocs)
3829 free (internal_relocs);
3830
3831 if (! ok)
3832 return FALSE;
3833 }
3834 }
3835
3836 return TRUE;
3837}
3838
3839/* Add symbols from an ELF object file to the linker hash table. */
3840
3841static bfd_boolean
3842elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3843{
3844 Elf_Internal_Ehdr *ehdr;
3845 Elf_Internal_Shdr *hdr;
3846 size_t symcount;
3847 size_t extsymcount;
3848 size_t extsymoff;
3849 struct elf_link_hash_entry **sym_hash;
3850 bfd_boolean dynamic;
3851 Elf_External_Versym *extversym = NULL;
3852 Elf_External_Versym *ever;
3853 struct elf_link_hash_entry *weaks;
3854 struct elf_link_hash_entry **nondeflt_vers = NULL;
3855 size_t nondeflt_vers_cnt = 0;
3856 Elf_Internal_Sym *isymbuf = NULL;
3857 Elf_Internal_Sym *isym;
3858 Elf_Internal_Sym *isymend;
3859 const struct elf_backend_data *bed;
3860 bfd_boolean add_needed;
3861 struct elf_link_hash_table *htab;
3862 bfd_size_type amt;
3863 void *alloc_mark = NULL;
3864 struct bfd_hash_entry **old_table = NULL;
3865 unsigned int old_size = 0;
3866 unsigned int old_count = 0;
3867 void *old_tab = NULL;
3868 void *old_ent;
3869 struct bfd_link_hash_entry *old_undefs = NULL;
3870 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3871 void *old_strtab = NULL;
3872 size_t tabsize = 0;
3873 asection *s;
3874 bfd_boolean just_syms;
3875
3876 htab = elf_hash_table (info);
3877 bed = get_elf_backend_data (abfd);
3878
3879 if ((abfd->flags & DYNAMIC) == 0)
3880 dynamic = FALSE;
3881 else
3882 {
3883 dynamic = TRUE;
3884
3885 /* You can't use -r against a dynamic object. Also, there's no
3886 hope of using a dynamic object which does not exactly match
3887 the format of the output file. */
3888 if (bfd_link_relocatable (info)
3889 || !is_elf_hash_table (htab)
3890 || info->output_bfd->xvec != abfd->xvec)
3891 {
3892 if (bfd_link_relocatable (info))
3893 bfd_set_error (bfd_error_invalid_operation);
3894 else
3895 bfd_set_error (bfd_error_wrong_format);
3896 goto error_return;
3897 }
3898 }
3899
3900 ehdr = elf_elfheader (abfd);
3901 if (info->warn_alternate_em
3902 && bed->elf_machine_code != ehdr->e_machine
3903 && ((bed->elf_machine_alt1 != 0
3904 && ehdr->e_machine == bed->elf_machine_alt1)
3905 || (bed->elf_machine_alt2 != 0
3906 && ehdr->e_machine == bed->elf_machine_alt2)))
3907 _bfd_error_handler
3908 /* xgettext:c-format */
3909 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3910 ehdr->e_machine, abfd, bed->elf_machine_code);
3911
3912 /* As a GNU extension, any input sections which are named
3913 .gnu.warning.SYMBOL are treated as warning symbols for the given
3914 symbol. This differs from .gnu.warning sections, which generate
3915 warnings when they are included in an output file. */
3916 /* PR 12761: Also generate this warning when building shared libraries. */
3917 for (s = abfd->sections; s != NULL; s = s->next)
3918 {
3919 const char *name;
3920
3921 name = bfd_get_section_name (abfd, s);
3922 if (CONST_STRNEQ (name, ".gnu.warning."))
3923 {
3924 char *msg;
3925 bfd_size_type sz;
3926
3927 name += sizeof ".gnu.warning." - 1;
3928
3929 /* If this is a shared object, then look up the symbol
3930 in the hash table. If it is there, and it is already
3931 been defined, then we will not be using the entry
3932 from this shared object, so we don't need to warn.
3933 FIXME: If we see the definition in a regular object
3934 later on, we will warn, but we shouldn't. The only
3935 fix is to keep track of what warnings we are supposed
3936 to emit, and then handle them all at the end of the
3937 link. */
3938 if (dynamic)
3939 {
3940 struct elf_link_hash_entry *h;
3941
3942 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3943
3944 /* FIXME: What about bfd_link_hash_common? */
3945 if (h != NULL
3946 && (h->root.type == bfd_link_hash_defined
3947 || h->root.type == bfd_link_hash_defweak))
3948 continue;
3949 }
3950
3951 sz = s->size;
3952 msg = (char *) bfd_alloc (abfd, sz + 1);
3953 if (msg == NULL)
3954 goto error_return;
3955
3956 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3957 goto error_return;
3958
3959 msg[sz] = '\0';
3960
3961 if (! (_bfd_generic_link_add_one_symbol
3962 (info, abfd, name, BSF_WARNING, s, 0, msg,
3963 FALSE, bed->collect, NULL)))
3964 goto error_return;
3965
3966 if (bfd_link_executable (info))
3967 {
3968 /* Clobber the section size so that the warning does
3969 not get copied into the output file. */
3970 s->size = 0;
3971
3972 /* Also set SEC_EXCLUDE, so that symbols defined in
3973 the warning section don't get copied to the output. */
3974 s->flags |= SEC_EXCLUDE;
3975 }
3976 }
3977 }
3978
3979 just_syms = ((s = abfd->sections) != NULL
3980 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3981
3982 add_needed = TRUE;
3983 if (! dynamic)
3984 {
3985 /* If we are creating a shared library, create all the dynamic
3986 sections immediately. We need to attach them to something,
3987 so we attach them to this BFD, provided it is the right
3988 format and is not from ld --just-symbols. Always create the
3989 dynamic sections for -E/--dynamic-list. FIXME: If there
3990 are no input BFD's of the same format as the output, we can't
3991 make a shared library. */
3992 if (!just_syms
3993 && (bfd_link_pic (info)
3994 || (!bfd_link_relocatable (info)
3995 && info->nointerp
3996 && (info->export_dynamic || info->dynamic)))
3997 && is_elf_hash_table (htab)
3998 && info->output_bfd->xvec == abfd->xvec
3999 && !htab->dynamic_sections_created)
4000 {
4001 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4002 goto error_return;
4003 }
4004 }
4005 else if (!is_elf_hash_table (htab))
4006 goto error_return;
4007 else
4008 {
4009 const char *soname = NULL;
4010 char *audit = NULL;
4011 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4012 const Elf_Internal_Phdr *phdr;
4013 int ret;
4014
4015 /* ld --just-symbols and dynamic objects don't mix very well.
4016 ld shouldn't allow it. */
4017 if (just_syms)
4018 abort ();
4019
4020 /* If this dynamic lib was specified on the command line with
4021 --as-needed in effect, then we don't want to add a DT_NEEDED
4022 tag unless the lib is actually used. Similary for libs brought
4023 in by another lib's DT_NEEDED. When --no-add-needed is used
4024 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4025 any dynamic library in DT_NEEDED tags in the dynamic lib at
4026 all. */
4027 add_needed = (elf_dyn_lib_class (abfd)
4028 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4029 | DYN_NO_NEEDED)) == 0;
4030
4031 s = bfd_get_section_by_name (abfd, ".dynamic");
4032 if (s != NULL)
4033 {
4034 bfd_byte *dynbuf;
4035 bfd_byte *extdyn;
4036 unsigned int elfsec;
4037 unsigned long shlink;
4038
4039 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4040 {
4041error_free_dyn:
4042 free (dynbuf);
4043 goto error_return;
4044 }
4045
4046 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4047 if (elfsec == SHN_BAD)
4048 goto error_free_dyn;
4049 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4050
4051 for (extdyn = dynbuf;
4052 extdyn < dynbuf + s->size;
4053 extdyn += bed->s->sizeof_dyn)
4054 {
4055 Elf_Internal_Dyn dyn;
4056
4057 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4058 if (dyn.d_tag == DT_SONAME)
4059 {
4060 unsigned int tagv = dyn.d_un.d_val;
4061 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4062 if (soname == NULL)
4063 goto error_free_dyn;
4064 }
4065 if (dyn.d_tag == DT_NEEDED)
4066 {
4067 struct bfd_link_needed_list *n, **pn;
4068 char *fnm, *anm;
4069 unsigned int tagv = dyn.d_un.d_val;
4070
4071 amt = sizeof (struct bfd_link_needed_list);
4072 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4073 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4074 if (n == NULL || fnm == NULL)
4075 goto error_free_dyn;
4076 amt = strlen (fnm) + 1;
4077 anm = (char *) bfd_alloc (abfd, amt);
4078 if (anm == NULL)
4079 goto error_free_dyn;
4080 memcpy (anm, fnm, amt);
4081 n->name = anm;
4082 n->by = abfd;
4083 n->next = NULL;
4084 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4085 ;
4086 *pn = n;
4087 }
4088 if (dyn.d_tag == DT_RUNPATH)
4089 {
4090 struct bfd_link_needed_list *n, **pn;
4091 char *fnm, *anm;
4092 unsigned int tagv = dyn.d_un.d_val;
4093
4094 amt = sizeof (struct bfd_link_needed_list);
4095 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4096 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4097 if (n == NULL || fnm == NULL)
4098 goto error_free_dyn;
4099 amt = strlen (fnm) + 1;
4100 anm = (char *) bfd_alloc (abfd, amt);
4101 if (anm == NULL)
4102 goto error_free_dyn;
4103 memcpy (anm, fnm, amt);
4104 n->name = anm;
4105 n->by = abfd;
4106 n->next = NULL;
4107 for (pn = & runpath;
4108 *pn != NULL;
4109 pn = &(*pn)->next)
4110 ;
4111 *pn = n;
4112 }
4113 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4114 if (!runpath && dyn.d_tag == DT_RPATH)
4115 {
4116 struct bfd_link_needed_list *n, **pn;
4117 char *fnm, *anm;
4118 unsigned int tagv = dyn.d_un.d_val;
4119
4120 amt = sizeof (struct bfd_link_needed_list);
4121 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4122 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4123 if (n == NULL || fnm == NULL)
4124 goto error_free_dyn;
4125 amt = strlen (fnm) + 1;
4126 anm = (char *) bfd_alloc (abfd, amt);
4127 if (anm == NULL)
4128 goto error_free_dyn;
4129 memcpy (anm, fnm, amt);
4130 n->name = anm;
4131 n->by = abfd;
4132 n->next = NULL;
4133 for (pn = & rpath;
4134 *pn != NULL;
4135 pn = &(*pn)->next)
4136 ;
4137 *pn = n;
4138 }
4139 if (dyn.d_tag == DT_AUDIT)
4140 {
4141 unsigned int tagv = dyn.d_un.d_val;
4142 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4143 }
4144 }
4145
4146 free (dynbuf);
4147 }
4148
4149 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4150 frees all more recently bfd_alloc'd blocks as well. */
4151 if (runpath)
4152 rpath = runpath;
4153
4154 if (rpath)
4155 {
4156 struct bfd_link_needed_list **pn;
4157 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4158 ;
4159 *pn = rpath;
4160 }
4161
4162 /* If we have a PT_GNU_RELRO program header, mark as read-only
4163 all sections contained fully therein. This makes relro
4164 shared library sections appear as they will at run-time. */
4165 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4166 while (--phdr >= elf_tdata (abfd)->phdr)
4167 if (phdr->p_type == PT_GNU_RELRO)
4168 {
4169 for (s = abfd->sections; s != NULL; s = s->next)
4170 if ((s->flags & SEC_ALLOC) != 0
4171 && s->vma >= phdr->p_vaddr
4172 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4173 s->flags |= SEC_READONLY;
4174 break;
4175 }
4176
4177 /* We do not want to include any of the sections in a dynamic
4178 object in the output file. We hack by simply clobbering the
4179 list of sections in the BFD. This could be handled more
4180 cleanly by, say, a new section flag; the existing
4181 SEC_NEVER_LOAD flag is not the one we want, because that one
4182 still implies that the section takes up space in the output
4183 file. */
4184 bfd_section_list_clear (abfd);
4185
4186 /* Find the name to use in a DT_NEEDED entry that refers to this
4187 object. If the object has a DT_SONAME entry, we use it.
4188 Otherwise, if the generic linker stuck something in
4189 elf_dt_name, we use that. Otherwise, we just use the file
4190 name. */
4191 if (soname == NULL || *soname == '\0')
4192 {
4193 soname = elf_dt_name (abfd);
4194 if (soname == NULL || *soname == '\0')
4195 soname = bfd_get_filename (abfd);
4196 }
4197
4198 /* Save the SONAME because sometimes the linker emulation code
4199 will need to know it. */
4200 elf_dt_name (abfd) = soname;
4201
4202 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4203 if (ret < 0)
4204 goto error_return;
4205
4206 /* If we have already included this dynamic object in the
4207 link, just ignore it. There is no reason to include a
4208 particular dynamic object more than once. */
4209 if (ret > 0)
4210 return TRUE;
4211
4212 /* Save the DT_AUDIT entry for the linker emulation code. */
4213 elf_dt_audit (abfd) = audit;
4214 }
4215
4216 /* If this is a dynamic object, we always link against the .dynsym
4217 symbol table, not the .symtab symbol table. The dynamic linker
4218 will only see the .dynsym symbol table, so there is no reason to
4219 look at .symtab for a dynamic object. */
4220
4221 if (! dynamic || elf_dynsymtab (abfd) == 0)
4222 hdr = &elf_tdata (abfd)->symtab_hdr;
4223 else
4224 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4225
4226 symcount = hdr->sh_size / bed->s->sizeof_sym;
4227
4228 /* The sh_info field of the symtab header tells us where the
4229 external symbols start. We don't care about the local symbols at
4230 this point. */
4231 if (elf_bad_symtab (abfd))
4232 {
4233 extsymcount = symcount;
4234 extsymoff = 0;
4235 }
4236 else
4237 {
4238 extsymcount = symcount - hdr->sh_info;
4239 extsymoff = hdr->sh_info;
4240 }
4241
4242 sym_hash = elf_sym_hashes (abfd);
4243 if (extsymcount != 0)
4244 {
4245 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4246 NULL, NULL, NULL);
4247 if (isymbuf == NULL)
4248 goto error_return;
4249
4250 if (sym_hash == NULL)
4251 {
4252 /* We store a pointer to the hash table entry for each
4253 external symbol. */
4254 amt = extsymcount;
4255 amt *= sizeof (struct elf_link_hash_entry *);
4256 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4257 if (sym_hash == NULL)
4258 goto error_free_sym;
4259 elf_sym_hashes (abfd) = sym_hash;
4260 }
4261 }
4262
4263 if (dynamic)
4264 {
4265 /* Read in any version definitions. */
4266 if (!_bfd_elf_slurp_version_tables (abfd,
4267 info->default_imported_symver))
4268 goto error_free_sym;
4269
4270 /* Read in the symbol versions, but don't bother to convert them
4271 to internal format. */
4272 if (elf_dynversym (abfd) != 0)
4273 {
4274 Elf_Internal_Shdr *versymhdr;
4275
4276 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4277 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4278 if (extversym == NULL)
4279 goto error_free_sym;
4280 amt = versymhdr->sh_size;
4281 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4282 || bfd_bread (extversym, amt, abfd) != amt)
4283 goto error_free_vers;
4284 }
4285 }
4286
4287 /* If we are loading an as-needed shared lib, save the symbol table
4288 state before we start adding symbols. If the lib turns out
4289 to be unneeded, restore the state. */
4290 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4291 {
4292 unsigned int i;
4293 size_t entsize;
4294
4295 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4296 {
4297 struct bfd_hash_entry *p;
4298 struct elf_link_hash_entry *h;
4299
4300 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4301 {
4302 h = (struct elf_link_hash_entry *) p;
4303 entsize += htab->root.table.entsize;
4304 if (h->root.type == bfd_link_hash_warning)
4305 entsize += htab->root.table.entsize;
4306 }
4307 }
4308
4309 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4310 old_tab = bfd_malloc (tabsize + entsize);
4311 if (old_tab == NULL)
4312 goto error_free_vers;
4313
4314 /* Remember the current objalloc pointer, so that all mem for
4315 symbols added can later be reclaimed. */
4316 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4317 if (alloc_mark == NULL)
4318 goto error_free_vers;
4319
4320 /* Make a special call to the linker "notice" function to
4321 tell it that we are about to handle an as-needed lib. */
4322 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4323 goto error_free_vers;
4324
4325 /* Clone the symbol table. Remember some pointers into the
4326 symbol table, and dynamic symbol count. */
4327 old_ent = (char *) old_tab + tabsize;
4328 memcpy (old_tab, htab->root.table.table, tabsize);
4329 old_undefs = htab->root.undefs;
4330 old_undefs_tail = htab->root.undefs_tail;
4331 old_table = htab->root.table.table;
4332 old_size = htab->root.table.size;
4333 old_count = htab->root.table.count;
4334 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4335 if (old_strtab == NULL)
4336 goto error_free_vers;
4337
4338 for (i = 0; i < htab->root.table.size; i++)
4339 {
4340 struct bfd_hash_entry *p;
4341 struct elf_link_hash_entry *h;
4342
4343 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4344 {
4345 memcpy (old_ent, p, htab->root.table.entsize);
4346 old_ent = (char *) old_ent + htab->root.table.entsize;
4347 h = (struct elf_link_hash_entry *) p;
4348 if (h->root.type == bfd_link_hash_warning)
4349 {
4350 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4351 old_ent = (char *) old_ent + htab->root.table.entsize;
4352 }
4353 }
4354 }
4355 }
4356
4357 weaks = NULL;
4358 ever = extversym != NULL ? extversym + extsymoff : NULL;
4359 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4360 isym < isymend;
4361 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4362 {
4363 int bind;
4364 bfd_vma value;
4365 asection *sec, *new_sec;
4366 flagword flags;
4367 const char *name;
4368 struct elf_link_hash_entry *h;
4369 struct elf_link_hash_entry *hi;
4370 bfd_boolean definition;
4371 bfd_boolean size_change_ok;
4372 bfd_boolean type_change_ok;
4373 bfd_boolean new_weak;
4374 bfd_boolean old_weak;
4375 bfd_boolean override;
4376 bfd_boolean common;
4377 bfd_boolean discarded;
4378 unsigned int old_alignment;
4379 bfd *old_bfd;
4380 bfd_boolean matched;
4381
4382 override = FALSE;
4383
4384 flags = BSF_NO_FLAGS;
4385 sec = NULL;
4386 value = isym->st_value;
4387 common = bed->common_definition (isym);
4388 if (common && info->inhibit_common_definition)
4389 {
4390 /* Treat common symbol as undefined for --no-define-common. */
4391 isym->st_shndx = SHN_UNDEF;
4392 common = FALSE;
4393 }
4394 discarded = FALSE;
4395
4396 bind = ELF_ST_BIND (isym->st_info);
4397 switch (bind)
4398 {
4399 case STB_LOCAL:
4400 /* This should be impossible, since ELF requires that all
4401 global symbols follow all local symbols, and that sh_info
4402 point to the first global symbol. Unfortunately, Irix 5
4403 screws this up. */
4404 continue;
4405
4406 case STB_GLOBAL:
4407 if (isym->st_shndx != SHN_UNDEF && !common)
4408 flags = BSF_GLOBAL;
4409 break;
4410
4411 case STB_WEAK:
4412 flags = BSF_WEAK;
4413 break;
4414
4415 case STB_GNU_UNIQUE:
4416 flags = BSF_GNU_UNIQUE;
4417 break;
4418
4419 default:
4420 /* Leave it up to the processor backend. */
4421 break;
4422 }
4423
4424 if (isym->st_shndx == SHN_UNDEF)
4425 sec = bfd_und_section_ptr;
4426 else if (isym->st_shndx == SHN_ABS)
4427 sec = bfd_abs_section_ptr;
4428 else if (isym->st_shndx == SHN_COMMON)
4429 {
4430 sec = bfd_com_section_ptr;
4431 /* What ELF calls the size we call the value. What ELF
4432 calls the value we call the alignment. */
4433 value = isym->st_size;
4434 }
4435 else
4436 {
4437 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4438 if (sec == NULL)
4439 sec = bfd_abs_section_ptr;
4440 else if (discarded_section (sec))
4441 {
4442 /* Symbols from discarded section are undefined. We keep
4443 its visibility. */
4444 sec = bfd_und_section_ptr;
4445 discarded = TRUE;
4446 isym->st_shndx = SHN_UNDEF;
4447 }
4448 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4449 value -= sec->vma;
4450 }
4451
4452 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4453 isym->st_name);
4454 if (name == NULL)
4455 goto error_free_vers;
4456
4457 if (isym->st_shndx == SHN_COMMON
4458 && (abfd->flags & BFD_PLUGIN) != 0)
4459 {
4460 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4461
4462 if (xc == NULL)
4463 {
4464 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4465 | SEC_EXCLUDE);
4466 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4467 if (xc == NULL)
4468 goto error_free_vers;
4469 }
4470 sec = xc;
4471 }
4472 else if (isym->st_shndx == SHN_COMMON
4473 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4474 && !bfd_link_relocatable (info))
4475 {
4476 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4477
4478 if (tcomm == NULL)
4479 {
4480 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4481 | SEC_LINKER_CREATED);
4482 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4483 if (tcomm == NULL)
4484 goto error_free_vers;
4485 }
4486 sec = tcomm;
4487 }
4488 else if (bed->elf_add_symbol_hook)
4489 {
4490 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4491 &sec, &value))
4492 goto error_free_vers;
4493
4494 /* The hook function sets the name to NULL if this symbol
4495 should be skipped for some reason. */
4496 if (name == NULL)
4497 continue;
4498 }
4499
4500 /* Sanity check that all possibilities were handled. */
4501 if (sec == NULL)
4502 {
4503 bfd_set_error (bfd_error_bad_value);
4504 goto error_free_vers;
4505 }
4506
4507 /* Silently discard TLS symbols from --just-syms. There's
4508 no way to combine a static TLS block with a new TLS block
4509 for this executable. */
4510 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4511 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4512 continue;
4513
4514 if (bfd_is_und_section (sec)
4515 || bfd_is_com_section (sec))
4516 definition = FALSE;
4517 else
4518 definition = TRUE;
4519
4520 size_change_ok = FALSE;
4521 type_change_ok = bed->type_change_ok;
4522 old_weak = FALSE;
4523 matched = FALSE;
4524 old_alignment = 0;
4525 old_bfd = NULL;
4526 new_sec = sec;
4527
4528 if (is_elf_hash_table (htab))
4529 {
4530 Elf_Internal_Versym iver;
4531 unsigned int vernum = 0;
4532 bfd_boolean skip;
4533
4534 if (ever == NULL)
4535 {
4536 if (info->default_imported_symver)
4537 /* Use the default symbol version created earlier. */
4538 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4539 else
4540 iver.vs_vers = 0;
4541 }
4542 else
4543 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4544
4545 vernum = iver.vs_vers & VERSYM_VERSION;
4546
4547 /* If this is a hidden symbol, or if it is not version
4548 1, we append the version name to the symbol name.
4549 However, we do not modify a non-hidden absolute symbol
4550 if it is not a function, because it might be the version
4551 symbol itself. FIXME: What if it isn't? */
4552 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4553 || (vernum > 1
4554 && (!bfd_is_abs_section (sec)
4555 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4556 {
4557 const char *verstr;
4558 size_t namelen, verlen, newlen;
4559 char *newname, *p;
4560
4561 if (isym->st_shndx != SHN_UNDEF)
4562 {
4563 if (vernum > elf_tdata (abfd)->cverdefs)
4564 verstr = NULL;
4565 else if (vernum > 1)
4566 verstr =
4567 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4568 else
4569 verstr = "";
4570
4571 if (verstr == NULL)
4572 {
4573 _bfd_error_handler
4574 /* xgettext:c-format */
4575 (_("%pB: %s: invalid version %u (max %d)"),
4576 abfd, name, vernum,
4577 elf_tdata (abfd)->cverdefs);
4578 bfd_set_error (bfd_error_bad_value);
4579 goto error_free_vers;
4580 }
4581 }
4582 else
4583 {
4584 /* We cannot simply test for the number of
4585 entries in the VERNEED section since the
4586 numbers for the needed versions do not start
4587 at 0. */
4588 Elf_Internal_Verneed *t;
4589
4590 verstr = NULL;
4591 for (t = elf_tdata (abfd)->verref;
4592 t != NULL;
4593 t = t->vn_nextref)
4594 {
4595 Elf_Internal_Vernaux *a;
4596
4597 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4598 {
4599 if (a->vna_other == vernum)
4600 {
4601 verstr = a->vna_nodename;
4602 break;
4603 }
4604 }
4605 if (a != NULL)
4606 break;
4607 }
4608 if (verstr == NULL)
4609 {
4610 _bfd_error_handler
4611 /* xgettext:c-format */
4612 (_("%pB: %s: invalid needed version %d"),
4613 abfd, name, vernum);
4614 bfd_set_error (bfd_error_bad_value);
4615 goto error_free_vers;
4616 }
4617 }
4618
4619 namelen = strlen (name);
4620 verlen = strlen (verstr);
4621 newlen = namelen + verlen + 2;
4622 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4623 && isym->st_shndx != SHN_UNDEF)
4624 ++newlen;
4625
4626 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4627 if (newname == NULL)
4628 goto error_free_vers;
4629 memcpy (newname, name, namelen);
4630 p = newname + namelen;
4631 *p++ = ELF_VER_CHR;
4632 /* If this is a defined non-hidden version symbol,
4633 we add another @ to the name. This indicates the
4634 default version of the symbol. */
4635 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4636 && isym->st_shndx != SHN_UNDEF)
4637 *p++ = ELF_VER_CHR;
4638 memcpy (p, verstr, verlen + 1);
4639
4640 name = newname;
4641 }
4642
4643 /* If this symbol has default visibility and the user has
4644 requested we not re-export it, then mark it as hidden. */
4645 if (!bfd_is_und_section (sec)
4646 && !dynamic
4647 && abfd->no_export
4648 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4649 isym->st_other = (STV_HIDDEN
4650 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4651
4652 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4653 sym_hash, &old_bfd, &old_weak,
4654 &old_alignment, &skip, &override,
4655 &type_change_ok, &size_change_ok,
4656 &matched))
4657 goto error_free_vers;
4658
4659 if (skip)
4660 continue;
4661
4662 /* Override a definition only if the new symbol matches the
4663 existing one. */
4664 if (override && matched)
4665 definition = FALSE;
4666
4667 h = *sym_hash;
4668 while (h->root.type == bfd_link_hash_indirect
4669 || h->root.type == bfd_link_hash_warning)
4670 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4671
4672 if (elf_tdata (abfd)->verdef != NULL
4673 && vernum > 1
4674 && definition)
4675 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4676 }
4677
4678 if (! (_bfd_generic_link_add_one_symbol
4679 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4680 (struct bfd_link_hash_entry **) sym_hash)))
4681 goto error_free_vers;
4682
4683 if ((abfd->flags & DYNAMIC) == 0
4684 && (bfd_get_flavour (info->output_bfd)
4685 == bfd_target_elf_flavour))
4686 {
4687 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4688 elf_tdata (info->output_bfd)->has_gnu_symbols
4689 |= elf_gnu_symbol_ifunc;
4690 if ((flags & BSF_GNU_UNIQUE))
4691 elf_tdata (info->output_bfd)->has_gnu_symbols
4692 |= elf_gnu_symbol_unique;
4693 }
4694
4695 h = *sym_hash;
4696 /* We need to make sure that indirect symbol dynamic flags are
4697 updated. */
4698 hi = h;
4699 while (h->root.type == bfd_link_hash_indirect
4700 || h->root.type == bfd_link_hash_warning)
4701 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4702
4703 /* Setting the index to -3 tells elf_link_output_extsym that
4704 this symbol is defined in a discarded section. */
4705 if (discarded)
4706 h->indx = -3;
4707
4708 *sym_hash = h;
4709
4710 new_weak = (flags & BSF_WEAK) != 0;
4711 if (dynamic
4712 && definition
4713 && new_weak
4714 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4715 && is_elf_hash_table (htab)
4716 && h->u.alias == NULL)
4717 {
4718 /* Keep a list of all weak defined non function symbols from
4719 a dynamic object, using the alias field. Later in this
4720 function we will set the alias field to the correct
4721 value. We only put non-function symbols from dynamic
4722 objects on this list, because that happens to be the only
4723 time we need to know the normal symbol corresponding to a
4724 weak symbol, and the information is time consuming to
4725 figure out. If the alias field is not already NULL,
4726 then this symbol was already defined by some previous
4727 dynamic object, and we will be using that previous
4728 definition anyhow. */
4729
4730 h->u.alias = weaks;
4731 weaks = h;
4732 }
4733
4734 /* Set the alignment of a common symbol. */
4735 if ((common || bfd_is_com_section (sec))
4736 && h->root.type == bfd_link_hash_common)
4737 {
4738 unsigned int align;
4739
4740 if (common)
4741 align = bfd_log2 (isym->st_value);
4742 else
4743 {
4744 /* The new symbol is a common symbol in a shared object.
4745 We need to get the alignment from the section. */
4746 align = new_sec->alignment_power;
4747 }
4748 if (align > old_alignment)
4749 h->root.u.c.p->alignment_power = align;
4750 else
4751 h->root.u.c.p->alignment_power = old_alignment;
4752 }
4753
4754 if (is_elf_hash_table (htab))
4755 {
4756 /* Set a flag in the hash table entry indicating the type of
4757 reference or definition we just found. A dynamic symbol
4758 is one which is referenced or defined by both a regular
4759 object and a shared object. */
4760 bfd_boolean dynsym = FALSE;
4761
4762 /* Plugin symbols aren't normal. Don't set def_regular or
4763 ref_regular for them, or make them dynamic. */
4764 if ((abfd->flags & BFD_PLUGIN) != 0)
4765 ;
4766 else if (! dynamic)
4767 {
4768 if (! definition)
4769 {
4770 h->ref_regular = 1;
4771 if (bind != STB_WEAK)
4772 h->ref_regular_nonweak = 1;
4773 }
4774 else
4775 {
4776 h->def_regular = 1;
4777 if (h->def_dynamic)
4778 {
4779 h->def_dynamic = 0;
4780 h->ref_dynamic = 1;
4781 }
4782 }
4783
4784 /* If the indirect symbol has been forced local, don't
4785 make the real symbol dynamic. */
4786 if ((h == hi || !hi->forced_local)
4787 && (bfd_link_dll (info)
4788 || h->def_dynamic
4789 || h->ref_dynamic))
4790 dynsym = TRUE;
4791 }
4792 else
4793 {
4794 if (! definition)
4795 {
4796 h->ref_dynamic = 1;
4797 hi->ref_dynamic = 1;
4798 }
4799 else
4800 {
4801 h->def_dynamic = 1;
4802 hi->def_dynamic = 1;
4803 }
4804
4805 /* If the indirect symbol has been forced local, don't
4806 make the real symbol dynamic. */
4807 if ((h == hi || !hi->forced_local)
4808 && (h->def_regular
4809 || h->ref_regular
4810 || (h->is_weakalias
4811 && weakdef (h)->dynindx != -1)))
4812 dynsym = TRUE;
4813 }
4814
4815 /* Check to see if we need to add an indirect symbol for
4816 the default name. */
4817 if (definition
4818 || (!override && h->root.type == bfd_link_hash_common))
4819 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4820 sec, value, &old_bfd, &dynsym))
4821 goto error_free_vers;
4822
4823 /* Check the alignment when a common symbol is involved. This
4824 can change when a common symbol is overridden by a normal
4825 definition or a common symbol is ignored due to the old
4826 normal definition. We need to make sure the maximum
4827 alignment is maintained. */
4828 if ((old_alignment || common)
4829 && h->root.type != bfd_link_hash_common)
4830 {
4831 unsigned int common_align;
4832 unsigned int normal_align;
4833 unsigned int symbol_align;
4834 bfd *normal_bfd;
4835 bfd *common_bfd;
4836
4837 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4838 || h->root.type == bfd_link_hash_defweak);
4839
4840 symbol_align = ffs (h->root.u.def.value) - 1;
4841 if (h->root.u.def.section->owner != NULL
4842 && (h->root.u.def.section->owner->flags
4843 & (DYNAMIC | BFD_PLUGIN)) == 0)
4844 {
4845 normal_align = h->root.u.def.section->alignment_power;
4846 if (normal_align > symbol_align)
4847 normal_align = symbol_align;
4848 }
4849 else
4850 normal_align = symbol_align;
4851
4852 if (old_alignment)
4853 {
4854 common_align = old_alignment;
4855 common_bfd = old_bfd;
4856 normal_bfd = abfd;
4857 }
4858 else
4859 {
4860 common_align = bfd_log2 (isym->st_value);
4861 common_bfd = abfd;
4862 normal_bfd = old_bfd;
4863 }
4864
4865 if (normal_align < common_align)
4866 {
4867 /* PR binutils/2735 */
4868 if (normal_bfd == NULL)
4869 _bfd_error_handler
4870 /* xgettext:c-format */
4871 (_("warning: alignment %u of common symbol `%s' in %pB is"
4872 " greater than the alignment (%u) of its section %pA"),
4873 1 << common_align, name, common_bfd,
4874 1 << normal_align, h->root.u.def.section);
4875 else
4876 _bfd_error_handler
4877 /* xgettext:c-format */
4878 (_("warning: alignment %u of symbol `%s' in %pB"
4879 " is smaller than %u in %pB"),
4880 1 << normal_align, name, normal_bfd,
4881 1 << common_align, common_bfd);
4882 }
4883 }
4884
4885 /* Remember the symbol size if it isn't undefined. */
4886 if (isym->st_size != 0
4887 && isym->st_shndx != SHN_UNDEF
4888 && (definition || h->size == 0))
4889 {
4890 if (h->size != 0
4891 && h->size != isym->st_size
4892 && ! size_change_ok)
4893 _bfd_error_handler
4894 /* xgettext:c-format */
4895 (_("warning: size of symbol `%s' changed"
4896 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4897 name, (uint64_t) h->size, old_bfd,
4898 (uint64_t) isym->st_size, abfd);
4899
4900 h->size = isym->st_size;
4901 }
4902
4903 /* If this is a common symbol, then we always want H->SIZE
4904 to be the size of the common symbol. The code just above
4905 won't fix the size if a common symbol becomes larger. We
4906 don't warn about a size change here, because that is
4907 covered by --warn-common. Allow changes between different
4908 function types. */
4909 if (h->root.type == bfd_link_hash_common)
4910 h->size = h->root.u.c.size;
4911
4912 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4913 && ((definition && !new_weak)
4914 || (old_weak && h->root.type == bfd_link_hash_common)
4915 || h->type == STT_NOTYPE))
4916 {
4917 unsigned int type = ELF_ST_TYPE (isym->st_info);
4918
4919 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4920 symbol. */
4921 if (type == STT_GNU_IFUNC
4922 && (abfd->flags & DYNAMIC) != 0)
4923 type = STT_FUNC;
4924
4925 if (h->type != type)
4926 {
4927 if (h->type != STT_NOTYPE && ! type_change_ok)
4928 /* xgettext:c-format */
4929 _bfd_error_handler
4930 (_("warning: type of symbol `%s' changed"
4931 " from %d to %d in %pB"),
4932 name, h->type, type, abfd);
4933
4934 h->type = type;
4935 }
4936 }
4937
4938 /* Merge st_other field. */
4939 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4940
4941 /* We don't want to make debug symbol dynamic. */
4942 if (definition
4943 && (sec->flags & SEC_DEBUGGING)
4944 && !bfd_link_relocatable (info))
4945 dynsym = FALSE;
4946
4947 /* Nor should we make plugin symbols dynamic. */
4948 if ((abfd->flags & BFD_PLUGIN) != 0)
4949 dynsym = FALSE;
4950
4951 if (definition)
4952 {
4953 h->target_internal = isym->st_target_internal;
4954 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4955 }
4956
4957 if (definition && !dynamic)
4958 {
4959 char *p = strchr (name, ELF_VER_CHR);
4960 if (p != NULL && p[1] != ELF_VER_CHR)
4961 {
4962 /* Queue non-default versions so that .symver x, x@FOO
4963 aliases can be checked. */
4964 if (!nondeflt_vers)
4965 {
4966 amt = ((isymend - isym + 1)
4967 * sizeof (struct elf_link_hash_entry *));
4968 nondeflt_vers
4969 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4970 if (!nondeflt_vers)
4971 goto error_free_vers;
4972 }
4973 nondeflt_vers[nondeflt_vers_cnt++] = h;
4974 }
4975 }
4976
4977 if (dynsym && h->dynindx == -1)
4978 {
4979 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4980 goto error_free_vers;
4981 if (h->is_weakalias
4982 && weakdef (h)->dynindx == -1)
4983 {
4984 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4985 goto error_free_vers;
4986 }
4987 }
4988 else if (h->dynindx != -1)
4989 /* If the symbol already has a dynamic index, but
4990 visibility says it should not be visible, turn it into
4991 a local symbol. */
4992 switch (ELF_ST_VISIBILITY (h->other))
4993 {
4994 case STV_INTERNAL:
4995 case STV_HIDDEN:
4996 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4997 dynsym = FALSE;
4998 break;
4999 }
5000
5001 /* Don't add DT_NEEDED for references from the dummy bfd nor
5002 for unmatched symbol. */
5003 if (!add_needed
5004 && matched
5005 && definition
5006 && ((dynsym
5007 && h->ref_regular_nonweak
5008 && (old_bfd == NULL
5009 || (old_bfd->flags & BFD_PLUGIN) == 0))
5010 || (h->ref_dynamic_nonweak
5011 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5012 && !on_needed_list (elf_dt_name (abfd),
5013 htab->needed, NULL))))
5014 {
5015 int ret;
5016 const char *soname = elf_dt_name (abfd);
5017
5018 info->callbacks->minfo ("%!", soname, old_bfd,
5019 h->root.root.string);
5020
5021 /* A symbol from a library loaded via DT_NEEDED of some
5022 other library is referenced by a regular object.
5023 Add a DT_NEEDED entry for it. Issue an error if
5024 --no-add-needed is used and the reference was not
5025 a weak one. */
5026 if (old_bfd != NULL
5027 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5028 {
5029 _bfd_error_handler
5030 /* xgettext:c-format */
5031 (_("%pB: undefined reference to symbol '%s'"),
5032 old_bfd, name);
5033 bfd_set_error (bfd_error_missing_dso);
5034 goto error_free_vers;
5035 }
5036
5037 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5038 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5039
5040 add_needed = TRUE;
5041 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5042 if (ret < 0)
5043 goto error_free_vers;
5044
5045 BFD_ASSERT (ret == 0);
5046 }
5047 }
5048 }
5049
5050 if (info->lto_plugin_active
5051 && !bfd_link_relocatable (info)
5052 && (abfd->flags & BFD_PLUGIN) == 0
5053 && !just_syms
5054 && extsymcount)
5055 {
5056 int r_sym_shift;
5057
5058 if (bed->s->arch_size == 32)
5059 r_sym_shift = 8;
5060 else
5061 r_sym_shift = 32;
5062
5063 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5064 referenced in regular objects so that linker plugin will get
5065 the correct symbol resolution. */
5066
5067 sym_hash = elf_sym_hashes (abfd);
5068 for (s = abfd->sections; s != NULL; s = s->next)
5069 {
5070 Elf_Internal_Rela *internal_relocs;
5071 Elf_Internal_Rela *rel, *relend;
5072
5073 /* Don't check relocations in excluded sections. */
5074 if ((s->flags & SEC_RELOC) == 0
5075 || s->reloc_count == 0
5076 || (s->flags & SEC_EXCLUDE) != 0
5077 || ((info->strip == strip_all
5078 || info->strip == strip_debugger)
5079 && (s->flags & SEC_DEBUGGING) != 0))
5080 continue;
5081
5082 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5083 NULL,
5084 info->keep_memory);
5085 if (internal_relocs == NULL)
5086 goto error_free_vers;
5087
5088 rel = internal_relocs;
5089 relend = rel + s->reloc_count;
5090 for ( ; rel < relend; rel++)
5091 {
5092 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5093 struct elf_link_hash_entry *h;
5094
5095 /* Skip local symbols. */
5096 if (r_symndx < extsymoff)
5097 continue;
5098
5099 h = sym_hash[r_symndx - extsymoff];
5100 if (h != NULL)
5101 h->root.non_ir_ref_regular = 1;
5102 }
5103
5104 if (elf_section_data (s)->relocs != internal_relocs)
5105 free (internal_relocs);
5106 }
5107 }
5108
5109 if (extversym != NULL)
5110 {
5111 free (extversym);
5112 extversym = NULL;
5113 }
5114
5115 if (isymbuf != NULL)
5116 {
5117 free (isymbuf);
5118 isymbuf = NULL;
5119 }
5120
5121 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5122 {
5123 unsigned int i;
5124
5125 /* Restore the symbol table. */
5126 old_ent = (char *) old_tab + tabsize;
5127 memset (elf_sym_hashes (abfd), 0,
5128 extsymcount * sizeof (struct elf_link_hash_entry *));
5129 htab->root.table.table = old_table;
5130 htab->root.table.size = old_size;
5131 htab->root.table.count = old_count;
5132 memcpy (htab->root.table.table, old_tab, tabsize);
5133 htab->root.undefs = old_undefs;
5134 htab->root.undefs_tail = old_undefs_tail;
5135 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5136 free (old_strtab);
5137 old_strtab = NULL;
5138 for (i = 0; i < htab->root.table.size; i++)
5139 {
5140 struct bfd_hash_entry *p;
5141 struct elf_link_hash_entry *h;
5142 bfd_size_type size;
5143 unsigned int alignment_power;
5144 unsigned int non_ir_ref_dynamic;
5145
5146 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5147 {
5148 h = (struct elf_link_hash_entry *) p;
5149 if (h->root.type == bfd_link_hash_warning)
5150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5151
5152 /* Preserve the maximum alignment and size for common
5153 symbols even if this dynamic lib isn't on DT_NEEDED
5154 since it can still be loaded at run time by another
5155 dynamic lib. */
5156 if (h->root.type == bfd_link_hash_common)
5157 {
5158 size = h->root.u.c.size;
5159 alignment_power = h->root.u.c.p->alignment_power;
5160 }
5161 else
5162 {
5163 size = 0;
5164 alignment_power = 0;
5165 }
5166 /* Preserve non_ir_ref_dynamic so that this symbol
5167 will be exported when the dynamic lib becomes needed
5168 in the second pass. */
5169 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5170 memcpy (p, old_ent, htab->root.table.entsize);
5171 old_ent = (char *) old_ent + htab->root.table.entsize;
5172 h = (struct elf_link_hash_entry *) p;
5173 if (h->root.type == bfd_link_hash_warning)
5174 {
5175 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5176 old_ent = (char *) old_ent + htab->root.table.entsize;
5177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5178 }
5179 if (h->root.type == bfd_link_hash_common)
5180 {
5181 if (size > h->root.u.c.size)
5182 h->root.u.c.size = size;
5183 if (alignment_power > h->root.u.c.p->alignment_power)
5184 h->root.u.c.p->alignment_power = alignment_power;
5185 }
5186 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5187 }
5188 }
5189
5190 /* Make a special call to the linker "notice" function to
5191 tell it that symbols added for crefs may need to be removed. */
5192 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5193 goto error_free_vers;
5194
5195 free (old_tab);
5196 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5197 alloc_mark);
5198 if (nondeflt_vers != NULL)
5199 free (nondeflt_vers);
5200 return TRUE;
5201 }
5202
5203 if (old_tab != NULL)
5204 {
5205 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5206 goto error_free_vers;
5207 free (old_tab);
5208 old_tab = NULL;
5209 }
5210
5211 /* Now that all the symbols from this input file are created, if
5212 not performing a relocatable link, handle .symver foo, foo@BAR
5213 such that any relocs against foo become foo@BAR. */
5214 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5215 {
5216 size_t cnt, symidx;
5217
5218 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5219 {
5220 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5221 char *shortname, *p;
5222
5223 p = strchr (h->root.root.string, ELF_VER_CHR);
5224 if (p == NULL
5225 || (h->root.type != bfd_link_hash_defined
5226 && h->root.type != bfd_link_hash_defweak))
5227 continue;
5228
5229 amt = p - h->root.root.string;
5230 shortname = (char *) bfd_malloc (amt + 1);
5231 if (!shortname)
5232 goto error_free_vers;
5233 memcpy (shortname, h->root.root.string, amt);
5234 shortname[amt] = '\0';
5235
5236 hi = (struct elf_link_hash_entry *)
5237 bfd_link_hash_lookup (&htab->root, shortname,
5238 FALSE, FALSE, FALSE);
5239 if (hi != NULL
5240 && hi->root.type == h->root.type
5241 && hi->root.u.def.value == h->root.u.def.value
5242 && hi->root.u.def.section == h->root.u.def.section)
5243 {
5244 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5245 hi->root.type = bfd_link_hash_indirect;
5246 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5247 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5248 sym_hash = elf_sym_hashes (abfd);
5249 if (sym_hash)
5250 for (symidx = 0; symidx < extsymcount; ++symidx)
5251 if (sym_hash[symidx] == hi)
5252 {
5253 sym_hash[symidx] = h;
5254 break;
5255 }
5256 }
5257 free (shortname);
5258 }
5259 free (nondeflt_vers);
5260 nondeflt_vers = NULL;
5261 }
5262
5263 /* Now set the alias field correctly for all the weak defined
5264 symbols we found. The only way to do this is to search all the
5265 symbols. Since we only need the information for non functions in
5266 dynamic objects, that's the only time we actually put anything on
5267 the list WEAKS. We need this information so that if a regular
5268 object refers to a symbol defined weakly in a dynamic object, the
5269 real symbol in the dynamic object is also put in the dynamic
5270 symbols; we also must arrange for both symbols to point to the
5271 same memory location. We could handle the general case of symbol
5272 aliasing, but a general symbol alias can only be generated in
5273 assembler code, handling it correctly would be very time
5274 consuming, and other ELF linkers don't handle general aliasing
5275 either. */
5276 if (weaks != NULL)
5277 {
5278 struct elf_link_hash_entry **hpp;
5279 struct elf_link_hash_entry **hppend;
5280 struct elf_link_hash_entry **sorted_sym_hash;
5281 struct elf_link_hash_entry *h;
5282 size_t sym_count;
5283
5284 /* Since we have to search the whole symbol list for each weak
5285 defined symbol, search time for N weak defined symbols will be
5286 O(N^2). Binary search will cut it down to O(NlogN). */
5287 amt = extsymcount;
5288 amt *= sizeof (struct elf_link_hash_entry *);
5289 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5290 if (sorted_sym_hash == NULL)
5291 goto error_return;
5292 sym_hash = sorted_sym_hash;
5293 hpp = elf_sym_hashes (abfd);
5294 hppend = hpp + extsymcount;
5295 sym_count = 0;
5296 for (; hpp < hppend; hpp++)
5297 {
5298 h = *hpp;
5299 if (h != NULL
5300 && h->root.type == bfd_link_hash_defined
5301 && !bed->is_function_type (h->type))
5302 {
5303 *sym_hash = h;
5304 sym_hash++;
5305 sym_count++;
5306 }
5307 }
5308
5309 qsort (sorted_sym_hash, sym_count,
5310 sizeof (struct elf_link_hash_entry *),
5311 elf_sort_symbol);
5312
5313 while (weaks != NULL)
5314 {
5315 struct elf_link_hash_entry *hlook;
5316 asection *slook;
5317 bfd_vma vlook;
5318 size_t i, j, idx = 0;
5319
5320 hlook = weaks;
5321 weaks = hlook->u.alias;
5322 hlook->u.alias = NULL;
5323
5324 if (hlook->root.type != bfd_link_hash_defined
5325 && hlook->root.type != bfd_link_hash_defweak)
5326 continue;
5327
5328 slook = hlook->root.u.def.section;
5329 vlook = hlook->root.u.def.value;
5330
5331 i = 0;
5332 j = sym_count;
5333 while (i != j)
5334 {
5335 bfd_signed_vma vdiff;
5336 idx = (i + j) / 2;
5337 h = sorted_sym_hash[idx];
5338 vdiff = vlook - h->root.u.def.value;
5339 if (vdiff < 0)
5340 j = idx;
5341 else if (vdiff > 0)
5342 i = idx + 1;
5343 else
5344 {
5345 int sdiff = slook->id - h->root.u.def.section->id;
5346 if (sdiff < 0)
5347 j = idx;
5348 else if (sdiff > 0)
5349 i = idx + 1;
5350 else
5351 break;
5352 }
5353 }
5354
5355 /* We didn't find a value/section match. */
5356 if (i == j)
5357 continue;
5358
5359 /* With multiple aliases, or when the weak symbol is already
5360 strongly defined, we have multiple matching symbols and
5361 the binary search above may land on any of them. Step
5362 one past the matching symbol(s). */
5363 while (++idx != j)
5364 {
5365 h = sorted_sym_hash[idx];
5366 if (h->root.u.def.section != slook
5367 || h->root.u.def.value != vlook)
5368 break;
5369 }
5370
5371 /* Now look back over the aliases. Since we sorted by size
5372 as well as value and section, we'll choose the one with
5373 the largest size. */
5374 while (idx-- != i)
5375 {
5376 h = sorted_sym_hash[idx];
5377
5378 /* Stop if value or section doesn't match. */
5379 if (h->root.u.def.section != slook
5380 || h->root.u.def.value != vlook)
5381 break;
5382 else if (h != hlook)
5383 {
5384 struct elf_link_hash_entry *t;
5385
5386 hlook->u.alias = h;
5387 hlook->is_weakalias = 1;
5388 t = h;
5389 if (t->u.alias != NULL)
5390 while (t->u.alias != h)
5391 t = t->u.alias;
5392 t->u.alias = hlook;
5393
5394 /* If the weak definition is in the list of dynamic
5395 symbols, make sure the real definition is put
5396 there as well. */
5397 if (hlook->dynindx != -1 && h->dynindx == -1)
5398 {
5399 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5400 {
5401 err_free_sym_hash:
5402 free (sorted_sym_hash);
5403 goto error_return;
5404 }
5405 }
5406
5407 /* If the real definition is in the list of dynamic
5408 symbols, make sure the weak definition is put
5409 there as well. If we don't do this, then the
5410 dynamic loader might not merge the entries for the
5411 real definition and the weak definition. */
5412 if (h->dynindx != -1 && hlook->dynindx == -1)
5413 {
5414 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5415 goto err_free_sym_hash;
5416 }
5417 break;
5418 }
5419 }
5420 }
5421
5422 free (sorted_sym_hash);
5423 }
5424
5425 if (bed->check_directives
5426 && !(*bed->check_directives) (abfd, info))
5427 return FALSE;
5428
5429 /* If this is a non-traditional link, try to optimize the handling
5430 of the .stab/.stabstr sections. */
5431 if (! dynamic
5432 && ! info->traditional_format
5433 && is_elf_hash_table (htab)
5434 && (info->strip != strip_all && info->strip != strip_debugger))
5435 {
5436 asection *stabstr;
5437
5438 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5439 if (stabstr != NULL)
5440 {
5441 bfd_size_type string_offset = 0;
5442 asection *stab;
5443
5444 for (stab = abfd->sections; stab; stab = stab->next)
5445 if (CONST_STRNEQ (stab->name, ".stab")
5446 && (!stab->name[5] ||
5447 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5448 && (stab->flags & SEC_MERGE) == 0
5449 && !bfd_is_abs_section (stab->output_section))
5450 {
5451 struct bfd_elf_section_data *secdata;
5452
5453 secdata = elf_section_data (stab);
5454 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5455 stabstr, &secdata->sec_info,
5456 &string_offset))
5457 goto error_return;
5458 if (secdata->sec_info)
5459 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5460 }
5461 }
5462 }
5463
5464 if (is_elf_hash_table (htab) && add_needed)
5465 {
5466 /* Add this bfd to the loaded list. */
5467 struct elf_link_loaded_list *n;
5468
5469 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5470 if (n == NULL)
5471 goto error_return;
5472 n->abfd = abfd;
5473 n->next = htab->loaded;
5474 htab->loaded = n;
5475 }
5476
5477 return TRUE;
5478
5479 error_free_vers:
5480 if (old_tab != NULL)
5481 free (old_tab);
5482 if (old_strtab != NULL)
5483 free (old_strtab);
5484 if (nondeflt_vers != NULL)
5485 free (nondeflt_vers);
5486 if (extversym != NULL)
5487 free (extversym);
5488 error_free_sym:
5489 if (isymbuf != NULL)
5490 free (isymbuf);
5491 error_return:
5492 return FALSE;
5493}
5494
5495/* Return the linker hash table entry of a symbol that might be
5496 satisfied by an archive symbol. Return -1 on error. */
5497
5498struct elf_link_hash_entry *
5499_bfd_elf_archive_symbol_lookup (bfd *abfd,
5500 struct bfd_link_info *info,
5501 const char *name)
5502{
5503 struct elf_link_hash_entry *h;
5504 char *p, *copy;
5505 size_t len, first;
5506
5507 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5508 if (h != NULL)
5509 return h;
5510
5511 /* If this is a default version (the name contains @@), look up the
5512 symbol again with only one `@' as well as without the version.
5513 The effect is that references to the symbol with and without the
5514 version will be matched by the default symbol in the archive. */
5515
5516 p = strchr (name, ELF_VER_CHR);
5517 if (p == NULL || p[1] != ELF_VER_CHR)
5518 return h;
5519
5520 /* First check with only one `@'. */
5521 len = strlen (name);
5522 copy = (char *) bfd_alloc (abfd, len);
5523 if (copy == NULL)
5524 return (struct elf_link_hash_entry *) -1;
5525
5526 first = p - name + 1;
5527 memcpy (copy, name, first);
5528 memcpy (copy + first, name + first + 1, len - first);
5529
5530 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5531 if (h == NULL)
5532 {
5533 /* We also need to check references to the symbol without the
5534 version. */
5535 copy[first - 1] = '\0';
5536 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5537 FALSE, FALSE, TRUE);
5538 }
5539
5540 bfd_release (abfd, copy);
5541 return h;
5542}
5543
5544/* Add symbols from an ELF archive file to the linker hash table. We
5545 don't use _bfd_generic_link_add_archive_symbols because we need to
5546 handle versioned symbols.
5547
5548 Fortunately, ELF archive handling is simpler than that done by
5549 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5550 oddities. In ELF, if we find a symbol in the archive map, and the
5551 symbol is currently undefined, we know that we must pull in that
5552 object file.
5553
5554 Unfortunately, we do have to make multiple passes over the symbol
5555 table until nothing further is resolved. */
5556
5557static bfd_boolean
5558elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5559{
5560 symindex c;
5561 unsigned char *included = NULL;
5562 carsym *symdefs;
5563 bfd_boolean loop;
5564 bfd_size_type amt;
5565 const struct elf_backend_data *bed;
5566 struct elf_link_hash_entry * (*archive_symbol_lookup)
5567 (bfd *, struct bfd_link_info *, const char *);
5568
5569 if (! bfd_has_map (abfd))
5570 {
5571 /* An empty archive is a special case. */
5572 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5573 return TRUE;
5574 bfd_set_error (bfd_error_no_armap);
5575 return FALSE;
5576 }
5577
5578 /* Keep track of all symbols we know to be already defined, and all
5579 files we know to be already included. This is to speed up the
5580 second and subsequent passes. */
5581 c = bfd_ardata (abfd)->symdef_count;
5582 if (c == 0)
5583 return TRUE;
5584 amt = c;
5585 amt *= sizeof (*included);
5586 included = (unsigned char *) bfd_zmalloc (amt);
5587 if (included == NULL)
5588 return FALSE;
5589
5590 symdefs = bfd_ardata (abfd)->symdefs;
5591 bed = get_elf_backend_data (abfd);
5592 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5593
5594 do
5595 {
5596 file_ptr last;
5597 symindex i;
5598 carsym *symdef;
5599 carsym *symdefend;
5600
5601 loop = FALSE;
5602 last = -1;
5603
5604 symdef = symdefs;
5605 symdefend = symdef + c;
5606 for (i = 0; symdef < symdefend; symdef++, i++)
5607 {
5608 struct elf_link_hash_entry *h;
5609 bfd *element;
5610 struct bfd_link_hash_entry *undefs_tail;
5611 symindex mark;
5612
5613 if (included[i])
5614 continue;
5615 if (symdef->file_offset == last)
5616 {
5617 included[i] = TRUE;
5618 continue;
5619 }
5620
5621 h = archive_symbol_lookup (abfd, info, symdef->name);
5622 if (h == (struct elf_link_hash_entry *) -1)
5623 goto error_return;
5624
5625 if (h == NULL)
5626 continue;
5627
5628 if (h->root.type == bfd_link_hash_common)
5629 {
5630 /* We currently have a common symbol. The archive map contains
5631 a reference to this symbol, so we may want to include it. We
5632 only want to include it however, if this archive element
5633 contains a definition of the symbol, not just another common
5634 declaration of it.
5635
5636 Unfortunately some archivers (including GNU ar) will put
5637 declarations of common symbols into their archive maps, as
5638 well as real definitions, so we cannot just go by the archive
5639 map alone. Instead we must read in the element's symbol
5640 table and check that to see what kind of symbol definition
5641 this is. */
5642 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5643 continue;
5644 }
5645 else if (h->root.type != bfd_link_hash_undefined)
5646 {
5647 if (h->root.type != bfd_link_hash_undefweak)
5648 /* Symbol must be defined. Don't check it again. */
5649 included[i] = TRUE;
5650 continue;
5651 }
5652
5653 /* We need to include this archive member. */
5654 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5655 if (element == NULL)
5656 goto error_return;
5657
5658 if (! bfd_check_format (element, bfd_object))
5659 goto error_return;
5660
5661 undefs_tail = info->hash->undefs_tail;
5662
5663 if (!(*info->callbacks
5664 ->add_archive_element) (info, element, symdef->name, &element))
5665 continue;
5666 if (!bfd_link_add_symbols (element, info))
5667 goto error_return;
5668
5669 /* If there are any new undefined symbols, we need to make
5670 another pass through the archive in order to see whether
5671 they can be defined. FIXME: This isn't perfect, because
5672 common symbols wind up on undefs_tail and because an
5673 undefined symbol which is defined later on in this pass
5674 does not require another pass. This isn't a bug, but it
5675 does make the code less efficient than it could be. */
5676 if (undefs_tail != info->hash->undefs_tail)
5677 loop = TRUE;
5678
5679 /* Look backward to mark all symbols from this object file
5680 which we have already seen in this pass. */
5681 mark = i;
5682 do
5683 {
5684 included[mark] = TRUE;
5685 if (mark == 0)
5686 break;
5687 --mark;
5688 }
5689 while (symdefs[mark].file_offset == symdef->file_offset);
5690
5691 /* We mark subsequent symbols from this object file as we go
5692 on through the loop. */
5693 last = symdef->file_offset;
5694 }
5695 }
5696 while (loop);
5697
5698 free (included);
5699
5700 return TRUE;
5701
5702 error_return:
5703 if (included != NULL)
5704 free (included);
5705 return FALSE;
5706}
5707
5708/* Given an ELF BFD, add symbols to the global hash table as
5709 appropriate. */
5710
5711bfd_boolean
5712bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5713{
5714 switch (bfd_get_format (abfd))
5715 {
5716 case bfd_object:
5717 return elf_link_add_object_symbols (abfd, info);
5718 case bfd_archive:
5719 return elf_link_add_archive_symbols (abfd, info);
5720 default:
5721 bfd_set_error (bfd_error_wrong_format);
5722 return FALSE;
5723 }
5724}
5725\f
5726struct hash_codes_info
5727{
5728 unsigned long *hashcodes;
5729 bfd_boolean error;
5730};
5731
5732/* This function will be called though elf_link_hash_traverse to store
5733 all hash value of the exported symbols in an array. */
5734
5735static bfd_boolean
5736elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5737{
5738 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5739 const char *name;
5740 unsigned long ha;
5741 char *alc = NULL;
5742
5743 /* Ignore indirect symbols. These are added by the versioning code. */
5744 if (h->dynindx == -1)
5745 return TRUE;
5746
5747 name = h->root.root.string;
5748 if (h->versioned >= versioned)
5749 {
5750 char *p = strchr (name, ELF_VER_CHR);
5751 if (p != NULL)
5752 {
5753 alc = (char *) bfd_malloc (p - name + 1);
5754 if (alc == NULL)
5755 {
5756 inf->error = TRUE;
5757 return FALSE;
5758 }
5759 memcpy (alc, name, p - name);
5760 alc[p - name] = '\0';
5761 name = alc;
5762 }
5763 }
5764
5765 /* Compute the hash value. */
5766 ha = bfd_elf_hash (name);
5767
5768 /* Store the found hash value in the array given as the argument. */
5769 *(inf->hashcodes)++ = ha;
5770
5771 /* And store it in the struct so that we can put it in the hash table
5772 later. */
5773 h->u.elf_hash_value = ha;
5774
5775 if (alc != NULL)
5776 free (alc);
5777
5778 return TRUE;
5779}
5780
5781struct collect_gnu_hash_codes
5782{
5783 bfd *output_bfd;
5784 const struct elf_backend_data *bed;
5785 unsigned long int nsyms;
5786 unsigned long int maskbits;
5787 unsigned long int *hashcodes;
5788 unsigned long int *hashval;
5789 unsigned long int *indx;
5790 unsigned long int *counts;
5791 bfd_vma *bitmask;
5792 bfd_byte *contents;
5793 long int min_dynindx;
5794 unsigned long int bucketcount;
5795 unsigned long int symindx;
5796 long int local_indx;
5797 long int shift1, shift2;
5798 unsigned long int mask;
5799 bfd_boolean error;
5800};
5801
5802/* This function will be called though elf_link_hash_traverse to store
5803 all hash value of the exported symbols in an array. */
5804
5805static bfd_boolean
5806elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5807{
5808 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5809 const char *name;
5810 unsigned long ha;
5811 char *alc = NULL;
5812
5813 /* Ignore indirect symbols. These are added by the versioning code. */
5814 if (h->dynindx == -1)
5815 return TRUE;
5816
5817 /* Ignore also local symbols and undefined symbols. */
5818 if (! (*s->bed->elf_hash_symbol) (h))
5819 return TRUE;
5820
5821 name = h->root.root.string;
5822 if (h->versioned >= versioned)
5823 {
5824 char *p = strchr (name, ELF_VER_CHR);
5825 if (p != NULL)
5826 {
5827 alc = (char *) bfd_malloc (p - name + 1);
5828 if (alc == NULL)
5829 {
5830 s->error = TRUE;
5831 return FALSE;
5832 }
5833 memcpy (alc, name, p - name);
5834 alc[p - name] = '\0';
5835 name = alc;
5836 }
5837 }
5838
5839 /* Compute the hash value. */
5840 ha = bfd_elf_gnu_hash (name);
5841
5842 /* Store the found hash value in the array for compute_bucket_count,
5843 and also for .dynsym reordering purposes. */
5844 s->hashcodes[s->nsyms] = ha;
5845 s->hashval[h->dynindx] = ha;
5846 ++s->nsyms;
5847 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5848 s->min_dynindx = h->dynindx;
5849
5850 if (alc != NULL)
5851 free (alc);
5852
5853 return TRUE;
5854}
5855
5856/* This function will be called though elf_link_hash_traverse to do
5857 final dynaminc symbol renumbering. */
5858
5859static bfd_boolean
5860elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5861{
5862 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5863 unsigned long int bucket;
5864 unsigned long int val;
5865
5866 /* Ignore indirect symbols. */
5867 if (h->dynindx == -1)
5868 return TRUE;
5869
5870 /* Ignore also local symbols and undefined symbols. */
5871 if (! (*s->bed->elf_hash_symbol) (h))
5872 {
5873 if (h->dynindx >= s->min_dynindx)
5874 h->dynindx = s->local_indx++;
5875 return TRUE;
5876 }
5877
5878 bucket = s->hashval[h->dynindx] % s->bucketcount;
5879 val = (s->hashval[h->dynindx] >> s->shift1)
5880 & ((s->maskbits >> s->shift1) - 1);
5881 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5882 s->bitmask[val]
5883 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5884 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5885 if (s->counts[bucket] == 1)
5886 /* Last element terminates the chain. */
5887 val |= 1;
5888 bfd_put_32 (s->output_bfd, val,
5889 s->contents + (s->indx[bucket] - s->symindx) * 4);
5890 --s->counts[bucket];
5891 h->dynindx = s->indx[bucket]++;
5892 return TRUE;
5893}
5894
5895/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5896
5897bfd_boolean
5898_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5899{
5900 return !(h->forced_local
5901 || h->root.type == bfd_link_hash_undefined
5902 || h->root.type == bfd_link_hash_undefweak
5903 || ((h->root.type == bfd_link_hash_defined
5904 || h->root.type == bfd_link_hash_defweak)
5905 && h->root.u.def.section->output_section == NULL));
5906}
5907
5908/* Array used to determine the number of hash table buckets to use
5909 based on the number of symbols there are. If there are fewer than
5910 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5911 fewer than 37 we use 17 buckets, and so forth. We never use more
5912 than 32771 buckets. */
5913
5914static const size_t elf_buckets[] =
5915{
5916 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5917 16411, 32771, 0
5918};
5919
5920/* Compute bucket count for hashing table. We do not use a static set
5921 of possible tables sizes anymore. Instead we determine for all
5922 possible reasonable sizes of the table the outcome (i.e., the
5923 number of collisions etc) and choose the best solution. The
5924 weighting functions are not too simple to allow the table to grow
5925 without bounds. Instead one of the weighting factors is the size.
5926 Therefore the result is always a good payoff between few collisions
5927 (= short chain lengths) and table size. */
5928static size_t
5929compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5930 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5931 unsigned long int nsyms,
5932 int gnu_hash)
5933{
5934 size_t best_size = 0;
5935 unsigned long int i;
5936
5937 /* We have a problem here. The following code to optimize the table
5938 size requires an integer type with more the 32 bits. If
5939 BFD_HOST_U_64_BIT is set we know about such a type. */
5940#ifdef BFD_HOST_U_64_BIT
5941 if (info->optimize)
5942 {
5943 size_t minsize;
5944 size_t maxsize;
5945 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5946 bfd *dynobj = elf_hash_table (info)->dynobj;
5947 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5948 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5949 unsigned long int *counts;
5950 bfd_size_type amt;
5951 unsigned int no_improvement_count = 0;
5952
5953 /* Possible optimization parameters: if we have NSYMS symbols we say
5954 that the hashing table must at least have NSYMS/4 and at most
5955 2*NSYMS buckets. */
5956 minsize = nsyms / 4;
5957 if (minsize == 0)
5958 minsize = 1;
5959 best_size = maxsize = nsyms * 2;
5960 if (gnu_hash)
5961 {
5962 if (minsize < 2)
5963 minsize = 2;
5964 if ((best_size & 31) == 0)
5965 ++best_size;
5966 }
5967
5968 /* Create array where we count the collisions in. We must use bfd_malloc
5969 since the size could be large. */
5970 amt = maxsize;
5971 amt *= sizeof (unsigned long int);
5972 counts = (unsigned long int *) bfd_malloc (amt);
5973 if (counts == NULL)
5974 return 0;
5975
5976 /* Compute the "optimal" size for the hash table. The criteria is a
5977 minimal chain length. The minor criteria is (of course) the size
5978 of the table. */
5979 for (i = minsize; i < maxsize; ++i)
5980 {
5981 /* Walk through the array of hashcodes and count the collisions. */
5982 BFD_HOST_U_64_BIT max;
5983 unsigned long int j;
5984 unsigned long int fact;
5985
5986 if (gnu_hash && (i & 31) == 0)
5987 continue;
5988
5989 memset (counts, '\0', i * sizeof (unsigned long int));
5990
5991 /* Determine how often each hash bucket is used. */
5992 for (j = 0; j < nsyms; ++j)
5993 ++counts[hashcodes[j] % i];
5994
5995 /* For the weight function we need some information about the
5996 pagesize on the target. This is information need not be 100%
5997 accurate. Since this information is not available (so far) we
5998 define it here to a reasonable default value. If it is crucial
5999 to have a better value some day simply define this value. */
6000# ifndef BFD_TARGET_PAGESIZE
6001# define BFD_TARGET_PAGESIZE (4096)
6002# endif
6003
6004 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6005 and the chains. */
6006 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6007
6008# if 1
6009 /* Variant 1: optimize for short chains. We add the squares
6010 of all the chain lengths (which favors many small chain
6011 over a few long chains). */
6012 for (j = 0; j < i; ++j)
6013 max += counts[j] * counts[j];
6014
6015 /* This adds penalties for the overall size of the table. */
6016 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6017 max *= fact * fact;
6018# else
6019 /* Variant 2: Optimize a lot more for small table. Here we
6020 also add squares of the size but we also add penalties for
6021 empty slots (the +1 term). */
6022 for (j = 0; j < i; ++j)
6023 max += (1 + counts[j]) * (1 + counts[j]);
6024
6025 /* The overall size of the table is considered, but not as
6026 strong as in variant 1, where it is squared. */
6027 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6028 max *= fact;
6029# endif
6030
6031 /* Compare with current best results. */
6032 if (max < best_chlen)
6033 {
6034 best_chlen = max;
6035 best_size = i;
6036 no_improvement_count = 0;
6037 }
6038 /* PR 11843: Avoid futile long searches for the best bucket size
6039 when there are a large number of symbols. */
6040 else if (++no_improvement_count == 100)
6041 break;
6042 }
6043
6044 free (counts);
6045 }
6046 else
6047#endif /* defined (BFD_HOST_U_64_BIT) */
6048 {
6049 /* This is the fallback solution if no 64bit type is available or if we
6050 are not supposed to spend much time on optimizations. We select the
6051 bucket count using a fixed set of numbers. */
6052 for (i = 0; elf_buckets[i] != 0; i++)
6053 {
6054 best_size = elf_buckets[i];
6055 if (nsyms < elf_buckets[i + 1])
6056 break;
6057 }
6058 if (gnu_hash && best_size < 2)
6059 best_size = 2;
6060 }
6061
6062 return best_size;
6063}
6064
6065/* Size any SHT_GROUP section for ld -r. */
6066
6067bfd_boolean
6068_bfd_elf_size_group_sections (struct bfd_link_info *info)
6069{
6070 bfd *ibfd;
6071 asection *s;
6072
6073 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6074 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6075 && (s = ibfd->sections) != NULL
6076 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6077 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6078 return FALSE;
6079 return TRUE;
6080}
6081
6082/* Set a default stack segment size. The value in INFO wins. If it
6083 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6084 undefined it is initialized. */
6085
6086bfd_boolean
6087bfd_elf_stack_segment_size (bfd *output_bfd,
6088 struct bfd_link_info *info,
6089 const char *legacy_symbol,
6090 bfd_vma default_size)
6091{
6092 struct elf_link_hash_entry *h = NULL;
6093
6094 /* Look for legacy symbol. */
6095 if (legacy_symbol)
6096 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6097 FALSE, FALSE, FALSE);
6098 if (h && (h->root.type == bfd_link_hash_defined
6099 || h->root.type == bfd_link_hash_defweak)
6100 && h->def_regular
6101 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6102 {
6103 /* The symbol has no type if specified on the command line. */
6104 h->type = STT_OBJECT;
6105 if (info->stacksize)
6106 /* xgettext:c-format */
6107 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6108 output_bfd, legacy_symbol);
6109 else if (h->root.u.def.section != bfd_abs_section_ptr)
6110 /* xgettext:c-format */
6111 _bfd_error_handler (_("%pB: %s not absolute"),
6112 output_bfd, legacy_symbol);
6113 else
6114 info->stacksize = h->root.u.def.value;
6115 }
6116
6117 if (!info->stacksize)
6118 /* If the user didn't set a size, or explicitly inhibit the
6119 size, set it now. */
6120 info->stacksize = default_size;
6121
6122 /* Provide the legacy symbol, if it is referenced. */
6123 if (h && (h->root.type == bfd_link_hash_undefined
6124 || h->root.type == bfd_link_hash_undefweak))
6125 {
6126 struct bfd_link_hash_entry *bh = NULL;
6127
6128 if (!(_bfd_generic_link_add_one_symbol
6129 (info, output_bfd, legacy_symbol,
6130 BSF_GLOBAL, bfd_abs_section_ptr,
6131 info->stacksize >= 0 ? info->stacksize : 0,
6132 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6133 return FALSE;
6134
6135 h = (struct elf_link_hash_entry *) bh;
6136 h->def_regular = 1;
6137 h->type = STT_OBJECT;
6138 }
6139
6140 return TRUE;
6141}
6142
6143/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6144
6145struct elf_gc_sweep_symbol_info
6146{
6147 struct bfd_link_info *info;
6148 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6149 bfd_boolean);
6150};
6151
6152static bfd_boolean
6153elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6154{
6155 if (!h->mark
6156 && (((h->root.type == bfd_link_hash_defined
6157 || h->root.type == bfd_link_hash_defweak)
6158 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6159 && h->root.u.def.section->gc_mark))
6160 || h->root.type == bfd_link_hash_undefined
6161 || h->root.type == bfd_link_hash_undefweak))
6162 {
6163 struct elf_gc_sweep_symbol_info *inf;
6164
6165 inf = (struct elf_gc_sweep_symbol_info *) data;
6166 (*inf->hide_symbol) (inf->info, h, TRUE);
6167 h->def_regular = 0;
6168 h->ref_regular = 0;
6169 h->ref_regular_nonweak = 0;
6170 }
6171
6172 return TRUE;
6173}
6174
6175/* Set up the sizes and contents of the ELF dynamic sections. This is
6176 called by the ELF linker emulation before_allocation routine. We
6177 must set the sizes of the sections before the linker sets the
6178 addresses of the various sections. */
6179
6180bfd_boolean
6181bfd_elf_size_dynamic_sections (bfd *output_bfd,
6182 const char *soname,
6183 const char *rpath,
6184 const char *filter_shlib,
6185 const char *audit,
6186 const char *depaudit,
6187 const char * const *auxiliary_filters,
6188 struct bfd_link_info *info,
6189 asection **sinterpptr)
6190{
6191 bfd *dynobj;
6192 const struct elf_backend_data *bed;
6193
6194 *sinterpptr = NULL;
6195
6196 if (!is_elf_hash_table (info->hash))
6197 return TRUE;
6198
6199 dynobj = elf_hash_table (info)->dynobj;
6200
6201 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6202 {
6203 struct bfd_elf_version_tree *verdefs;
6204 struct elf_info_failed asvinfo;
6205 struct bfd_elf_version_tree *t;
6206 struct bfd_elf_version_expr *d;
6207 asection *s;
6208 size_t soname_indx;
6209
6210 /* If we are supposed to export all symbols into the dynamic symbol
6211 table (this is not the normal case), then do so. */
6212 if (info->export_dynamic
6213 || (bfd_link_executable (info) && info->dynamic))
6214 {
6215 struct elf_info_failed eif;
6216
6217 eif.info = info;
6218 eif.failed = FALSE;
6219 elf_link_hash_traverse (elf_hash_table (info),
6220 _bfd_elf_export_symbol,
6221 &eif);
6222 if (eif.failed)
6223 return FALSE;
6224 }
6225
6226 if (soname != NULL)
6227 {
6228 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6229 soname, TRUE);
6230 if (soname_indx == (size_t) -1
6231 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6232 return FALSE;
6233 }
6234 else
6235 soname_indx = (size_t) -1;
6236
6237 /* Make all global versions with definition. */
6238 for (t = info->version_info; t != NULL; t = t->next)
6239 for (d = t->globals.list; d != NULL; d = d->next)
6240 if (!d->symver && d->literal)
6241 {
6242 const char *verstr, *name;
6243 size_t namelen, verlen, newlen;
6244 char *newname, *p, leading_char;
6245 struct elf_link_hash_entry *newh;
6246
6247 leading_char = bfd_get_symbol_leading_char (output_bfd);
6248 name = d->pattern;
6249 namelen = strlen (name) + (leading_char != '\0');
6250 verstr = t->name;
6251 verlen = strlen (verstr);
6252 newlen = namelen + verlen + 3;
6253
6254 newname = (char *) bfd_malloc (newlen);
6255 if (newname == NULL)
6256 return FALSE;
6257 newname[0] = leading_char;
6258 memcpy (newname + (leading_char != '\0'), name, namelen);
6259
6260 /* Check the hidden versioned definition. */
6261 p = newname + namelen;
6262 *p++ = ELF_VER_CHR;
6263 memcpy (p, verstr, verlen + 1);
6264 newh = elf_link_hash_lookup (elf_hash_table (info),
6265 newname, FALSE, FALSE,
6266 FALSE);
6267 if (newh == NULL
6268 || (newh->root.type != bfd_link_hash_defined
6269 && newh->root.type != bfd_link_hash_defweak))
6270 {
6271 /* Check the default versioned definition. */
6272 *p++ = ELF_VER_CHR;
6273 memcpy (p, verstr, verlen + 1);
6274 newh = elf_link_hash_lookup (elf_hash_table (info),
6275 newname, FALSE, FALSE,
6276 FALSE);
6277 }
6278 free (newname);
6279
6280 /* Mark this version if there is a definition and it is
6281 not defined in a shared object. */
6282 if (newh != NULL
6283 && !newh->def_dynamic
6284 && (newh->root.type == bfd_link_hash_defined
6285 || newh->root.type == bfd_link_hash_defweak))
6286 d->symver = 1;
6287 }
6288
6289 /* Attach all the symbols to their version information. */
6290 asvinfo.info = info;
6291 asvinfo.failed = FALSE;
6292
6293 elf_link_hash_traverse (elf_hash_table (info),
6294 _bfd_elf_link_assign_sym_version,
6295 &asvinfo);
6296 if (asvinfo.failed)
6297 return FALSE;
6298
6299 if (!info->allow_undefined_version)
6300 {
6301 /* Check if all global versions have a definition. */
6302 bfd_boolean all_defined = TRUE;
6303 for (t = info->version_info; t != NULL; t = t->next)
6304 for (d = t->globals.list; d != NULL; d = d->next)
6305 if (d->literal && !d->symver && !d->script)
6306 {
6307 _bfd_error_handler
6308 (_("%s: undefined version: %s"),
6309 d->pattern, t->name);
6310 all_defined = FALSE;
6311 }
6312
6313 if (!all_defined)
6314 {
6315 bfd_set_error (bfd_error_bad_value);
6316 return FALSE;
6317 }
6318 }
6319
6320 /* Set up the version definition section. */
6321 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6322 BFD_ASSERT (s != NULL);
6323
6324 /* We may have created additional version definitions if we are
6325 just linking a regular application. */
6326 verdefs = info->version_info;
6327
6328 /* Skip anonymous version tag. */
6329 if (verdefs != NULL && verdefs->vernum == 0)
6330 verdefs = verdefs->next;
6331
6332 if (verdefs == NULL && !info->create_default_symver)
6333 s->flags |= SEC_EXCLUDE;
6334 else
6335 {
6336 unsigned int cdefs;
6337 bfd_size_type size;
6338 bfd_byte *p;
6339 Elf_Internal_Verdef def;
6340 Elf_Internal_Verdaux defaux;
6341 struct bfd_link_hash_entry *bh;
6342 struct elf_link_hash_entry *h;
6343 const char *name;
6344
6345 cdefs = 0;
6346 size = 0;
6347
6348 /* Make space for the base version. */
6349 size += sizeof (Elf_External_Verdef);
6350 size += sizeof (Elf_External_Verdaux);
6351 ++cdefs;
6352
6353 /* Make space for the default version. */
6354 if (info->create_default_symver)
6355 {
6356 size += sizeof (Elf_External_Verdef);
6357 ++cdefs;
6358 }
6359
6360 for (t = verdefs; t != NULL; t = t->next)
6361 {
6362 struct bfd_elf_version_deps *n;
6363
6364 /* Don't emit base version twice. */
6365 if (t->vernum == 0)
6366 continue;
6367
6368 size += sizeof (Elf_External_Verdef);
6369 size += sizeof (Elf_External_Verdaux);
6370 ++cdefs;
6371
6372 for (n = t->deps; n != NULL; n = n->next)
6373 size += sizeof (Elf_External_Verdaux);
6374 }
6375
6376 s->size = size;
6377 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6378 if (s->contents == NULL && s->size != 0)
6379 return FALSE;
6380
6381 /* Fill in the version definition section. */
6382
6383 p = s->contents;
6384
6385 def.vd_version = VER_DEF_CURRENT;
6386 def.vd_flags = VER_FLG_BASE;
6387 def.vd_ndx = 1;
6388 def.vd_cnt = 1;
6389 if (info->create_default_symver)
6390 {
6391 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6392 def.vd_next = sizeof (Elf_External_Verdef);
6393 }
6394 else
6395 {
6396 def.vd_aux = sizeof (Elf_External_Verdef);
6397 def.vd_next = (sizeof (Elf_External_Verdef)
6398 + sizeof (Elf_External_Verdaux));
6399 }
6400
6401 if (soname_indx != (size_t) -1)
6402 {
6403 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6404 soname_indx);
6405 def.vd_hash = bfd_elf_hash (soname);
6406 defaux.vda_name = soname_indx;
6407 name = soname;
6408 }
6409 else
6410 {
6411 size_t indx;
6412
6413 name = lbasename (output_bfd->filename);
6414 def.vd_hash = bfd_elf_hash (name);
6415 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6416 name, FALSE);
6417 if (indx == (size_t) -1)
6418 return FALSE;
6419 defaux.vda_name = indx;
6420 }
6421 defaux.vda_next = 0;
6422
6423 _bfd_elf_swap_verdef_out (output_bfd, &def,
6424 (Elf_External_Verdef *) p);
6425 p += sizeof (Elf_External_Verdef);
6426 if (info->create_default_symver)
6427 {
6428 /* Add a symbol representing this version. */
6429 bh = NULL;
6430 if (! (_bfd_generic_link_add_one_symbol
6431 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6432 0, NULL, FALSE,
6433 get_elf_backend_data (dynobj)->collect, &bh)))
6434 return FALSE;
6435 h = (struct elf_link_hash_entry *) bh;
6436 h->non_elf = 0;
6437 h->def_regular = 1;
6438 h->type = STT_OBJECT;
6439 h->verinfo.vertree = NULL;
6440
6441 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6442 return FALSE;
6443
6444 /* Create a duplicate of the base version with the same
6445 aux block, but different flags. */
6446 def.vd_flags = 0;
6447 def.vd_ndx = 2;
6448 def.vd_aux = sizeof (Elf_External_Verdef);
6449 if (verdefs)
6450 def.vd_next = (sizeof (Elf_External_Verdef)
6451 + sizeof (Elf_External_Verdaux));
6452 else
6453 def.vd_next = 0;
6454 _bfd_elf_swap_verdef_out (output_bfd, &def,
6455 (Elf_External_Verdef *) p);
6456 p += sizeof (Elf_External_Verdef);
6457 }
6458 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6459 (Elf_External_Verdaux *) p);
6460 p += sizeof (Elf_External_Verdaux);
6461
6462 for (t = verdefs; t != NULL; t = t->next)
6463 {
6464 unsigned int cdeps;
6465 struct bfd_elf_version_deps *n;
6466
6467 /* Don't emit the base version twice. */
6468 if (t->vernum == 0)
6469 continue;
6470
6471 cdeps = 0;
6472 for (n = t->deps; n != NULL; n = n->next)
6473 ++cdeps;
6474
6475 /* Add a symbol representing this version. */
6476 bh = NULL;
6477 if (! (_bfd_generic_link_add_one_symbol
6478 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6479 0, NULL, FALSE,
6480 get_elf_backend_data (dynobj)->collect, &bh)))
6481 return FALSE;
6482 h = (struct elf_link_hash_entry *) bh;
6483 h->non_elf = 0;
6484 h->def_regular = 1;
6485 h->type = STT_OBJECT;
6486 h->verinfo.vertree = t;
6487
6488 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6489 return FALSE;
6490
6491 def.vd_version = VER_DEF_CURRENT;
6492 def.vd_flags = 0;
6493 if (t->globals.list == NULL
6494 && t->locals.list == NULL
6495 && ! t->used)
6496 def.vd_flags |= VER_FLG_WEAK;
6497 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6498 def.vd_cnt = cdeps + 1;
6499 def.vd_hash = bfd_elf_hash (t->name);
6500 def.vd_aux = sizeof (Elf_External_Verdef);
6501 def.vd_next = 0;
6502
6503 /* If a basever node is next, it *must* be the last node in
6504 the chain, otherwise Verdef construction breaks. */
6505 if (t->next != NULL && t->next->vernum == 0)
6506 BFD_ASSERT (t->next->next == NULL);
6507
6508 if (t->next != NULL && t->next->vernum != 0)
6509 def.vd_next = (sizeof (Elf_External_Verdef)
6510 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6511
6512 _bfd_elf_swap_verdef_out (output_bfd, &def,
6513 (Elf_External_Verdef *) p);
6514 p += sizeof (Elf_External_Verdef);
6515
6516 defaux.vda_name = h->dynstr_index;
6517 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6518 h->dynstr_index);
6519 defaux.vda_next = 0;
6520 if (t->deps != NULL)
6521 defaux.vda_next = sizeof (Elf_External_Verdaux);
6522 t->name_indx = defaux.vda_name;
6523
6524 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6525 (Elf_External_Verdaux *) p);
6526 p += sizeof (Elf_External_Verdaux);
6527
6528 for (n = t->deps; n != NULL; n = n->next)
6529 {
6530 if (n->version_needed == NULL)
6531 {
6532 /* This can happen if there was an error in the
6533 version script. */
6534 defaux.vda_name = 0;
6535 }
6536 else
6537 {
6538 defaux.vda_name = n->version_needed->name_indx;
6539 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6540 defaux.vda_name);
6541 }
6542 if (n->next == NULL)
6543 defaux.vda_next = 0;
6544 else
6545 defaux.vda_next = sizeof (Elf_External_Verdaux);
6546
6547 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6548 (Elf_External_Verdaux *) p);
6549 p += sizeof (Elf_External_Verdaux);
6550 }
6551 }
6552
6553 elf_tdata (output_bfd)->cverdefs = cdefs;
6554 }
6555 }
6556
6557 bed = get_elf_backend_data (output_bfd);
6558
6559 if (info->gc_sections && bed->can_gc_sections)
6560 {
6561 struct elf_gc_sweep_symbol_info sweep_info;
6562
6563 /* Remove the symbols that were in the swept sections from the
6564 dynamic symbol table. */
6565 sweep_info.info = info;
6566 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6567 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6568 &sweep_info);
6569 }
6570
6571 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6572 {
6573 asection *s;
6574 struct elf_find_verdep_info sinfo;
6575
6576 /* Work out the size of the version reference section. */
6577
6578 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6579 BFD_ASSERT (s != NULL);
6580
6581 sinfo.info = info;
6582 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6583 if (sinfo.vers == 0)
6584 sinfo.vers = 1;
6585 sinfo.failed = FALSE;
6586
6587 elf_link_hash_traverse (elf_hash_table (info),
6588 _bfd_elf_link_find_version_dependencies,
6589 &sinfo);
6590 if (sinfo.failed)
6591 return FALSE;
6592
6593 if (elf_tdata (output_bfd)->verref == NULL)
6594 s->flags |= SEC_EXCLUDE;
6595 else
6596 {
6597 Elf_Internal_Verneed *vn;
6598 unsigned int size;
6599 unsigned int crefs;
6600 bfd_byte *p;
6601
6602 /* Build the version dependency section. */
6603 size = 0;
6604 crefs = 0;
6605 for (vn = elf_tdata (output_bfd)->verref;
6606 vn != NULL;
6607 vn = vn->vn_nextref)
6608 {
6609 Elf_Internal_Vernaux *a;
6610
6611 size += sizeof (Elf_External_Verneed);
6612 ++crefs;
6613 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6614 size += sizeof (Elf_External_Vernaux);
6615 }
6616
6617 s->size = size;
6618 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6619 if (s->contents == NULL)
6620 return FALSE;
6621
6622 p = s->contents;
6623 for (vn = elf_tdata (output_bfd)->verref;
6624 vn != NULL;
6625 vn = vn->vn_nextref)
6626 {
6627 unsigned int caux;
6628 Elf_Internal_Vernaux *a;
6629 size_t indx;
6630
6631 caux = 0;
6632 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6633 ++caux;
6634
6635 vn->vn_version = VER_NEED_CURRENT;
6636 vn->vn_cnt = caux;
6637 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6638 elf_dt_name (vn->vn_bfd) != NULL
6639 ? elf_dt_name (vn->vn_bfd)
6640 : lbasename (vn->vn_bfd->filename),
6641 FALSE);
6642 if (indx == (size_t) -1)
6643 return FALSE;
6644 vn->vn_file = indx;
6645 vn->vn_aux = sizeof (Elf_External_Verneed);
6646 if (vn->vn_nextref == NULL)
6647 vn->vn_next = 0;
6648 else
6649 vn->vn_next = (sizeof (Elf_External_Verneed)
6650 + caux * sizeof (Elf_External_Vernaux));
6651
6652 _bfd_elf_swap_verneed_out (output_bfd, vn,
6653 (Elf_External_Verneed *) p);
6654 p += sizeof (Elf_External_Verneed);
6655
6656 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6657 {
6658 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6659 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6660 a->vna_nodename, FALSE);
6661 if (indx == (size_t) -1)
6662 return FALSE;
6663 a->vna_name = indx;
6664 if (a->vna_nextptr == NULL)
6665 a->vna_next = 0;
6666 else
6667 a->vna_next = sizeof (Elf_External_Vernaux);
6668
6669 _bfd_elf_swap_vernaux_out (output_bfd, a,
6670 (Elf_External_Vernaux *) p);
6671 p += sizeof (Elf_External_Vernaux);
6672 }
6673 }
6674
6675 elf_tdata (output_bfd)->cverrefs = crefs;
6676 }
6677 }
6678
6679 /* Any syms created from now on start with -1 in
6680 got.refcount/offset and plt.refcount/offset. */
6681 elf_hash_table (info)->init_got_refcount
6682 = elf_hash_table (info)->init_got_offset;
6683 elf_hash_table (info)->init_plt_refcount
6684 = elf_hash_table (info)->init_plt_offset;
6685
6686 if (bfd_link_relocatable (info)
6687 && !_bfd_elf_size_group_sections (info))
6688 return FALSE;
6689
6690 /* The backend may have to create some sections regardless of whether
6691 we're dynamic or not. */
6692 if (bed->elf_backend_always_size_sections
6693 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6694 return FALSE;
6695
6696 /* Determine any GNU_STACK segment requirements, after the backend
6697 has had a chance to set a default segment size. */
6698 if (info->execstack)
6699 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6700 else if (info->noexecstack)
6701 elf_stack_flags (output_bfd) = PF_R | PF_W;
6702 else
6703 {
6704 bfd *inputobj;
6705 asection *notesec = NULL;
6706 int exec = 0;
6707
6708 for (inputobj = info->input_bfds;
6709 inputobj;
6710 inputobj = inputobj->link.next)
6711 {
6712 asection *s;
6713
6714 if (inputobj->flags
6715 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6716 continue;
6717 s = inputobj->sections;
6718 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6719 continue;
6720
6721 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6722 if (s)
6723 {
6724 if (s->flags & SEC_CODE)
6725 exec = PF_X;
6726 notesec = s;
6727 }
6728 else if (bed->default_execstack)
6729 exec = PF_X;
6730 }
6731 if (notesec || info->stacksize > 0)
6732 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6733 if (notesec && exec && bfd_link_relocatable (info)
6734 && notesec->output_section != bfd_abs_section_ptr)
6735 notesec->output_section->flags |= SEC_CODE;
6736 }
6737
6738 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6739 {
6740 struct elf_info_failed eif;
6741 struct elf_link_hash_entry *h;
6742 asection *dynstr;
6743 asection *s;
6744
6745 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6746 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6747
6748 if (info->symbolic)
6749 {
6750 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6751 return FALSE;
6752 info->flags |= DF_SYMBOLIC;
6753 }
6754
6755 if (rpath != NULL)
6756 {
6757 size_t indx;
6758 bfd_vma tag;
6759
6760 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6761 TRUE);
6762 if (indx == (size_t) -1)
6763 return FALSE;
6764
6765 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6766 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6767 return FALSE;
6768 }
6769
6770 if (filter_shlib != NULL)
6771 {
6772 size_t indx;
6773
6774 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6775 filter_shlib, TRUE);
6776 if (indx == (size_t) -1
6777 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6778 return FALSE;
6779 }
6780
6781 if (auxiliary_filters != NULL)
6782 {
6783 const char * const *p;
6784
6785 for (p = auxiliary_filters; *p != NULL; p++)
6786 {
6787 size_t indx;
6788
6789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6790 *p, TRUE);
6791 if (indx == (size_t) -1
6792 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6793 return FALSE;
6794 }
6795 }
6796
6797 if (audit != NULL)
6798 {
6799 size_t indx;
6800
6801 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6802 TRUE);
6803 if (indx == (size_t) -1
6804 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6805 return FALSE;
6806 }
6807
6808 if (depaudit != NULL)
6809 {
6810 size_t indx;
6811
6812 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6813 TRUE);
6814 if (indx == (size_t) -1
6815 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6816 return FALSE;
6817 }
6818
6819 eif.info = info;
6820 eif.failed = FALSE;
6821
6822 /* Find all symbols which were defined in a dynamic object and make
6823 the backend pick a reasonable value for them. */
6824 elf_link_hash_traverse (elf_hash_table (info),
6825 _bfd_elf_adjust_dynamic_symbol,
6826 &eif);
6827 if (eif.failed)
6828 return FALSE;
6829
6830 /* Add some entries to the .dynamic section. We fill in some of the
6831 values later, in bfd_elf_final_link, but we must add the entries
6832 now so that we know the final size of the .dynamic section. */
6833
6834 /* If there are initialization and/or finalization functions to
6835 call then add the corresponding DT_INIT/DT_FINI entries. */
6836 h = (info->init_function
6837 ? elf_link_hash_lookup (elf_hash_table (info),
6838 info->init_function, FALSE,
6839 FALSE, FALSE)
6840 : NULL);
6841 if (h != NULL
6842 && (h->ref_regular
6843 || h->def_regular))
6844 {
6845 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6846 return FALSE;
6847 }
6848 h = (info->fini_function
6849 ? elf_link_hash_lookup (elf_hash_table (info),
6850 info->fini_function, FALSE,
6851 FALSE, FALSE)
6852 : NULL);
6853 if (h != NULL
6854 && (h->ref_regular
6855 || h->def_regular))
6856 {
6857 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6858 return FALSE;
6859 }
6860
6861 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6862 if (s != NULL && s->linker_has_input)
6863 {
6864 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6865 if (! bfd_link_executable (info))
6866 {
6867 bfd *sub;
6868 asection *o;
6869
6870 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6871 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6872 && (o = sub->sections) != NULL
6873 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6874 for (o = sub->sections; o != NULL; o = o->next)
6875 if (elf_section_data (o)->this_hdr.sh_type
6876 == SHT_PREINIT_ARRAY)
6877 {
6878 _bfd_error_handler
6879 (_("%pB: .preinit_array section is not allowed in DSO"),
6880 sub);
6881 break;
6882 }
6883
6884 bfd_set_error (bfd_error_nonrepresentable_section);
6885 return FALSE;
6886 }
6887
6888 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6889 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6890 return FALSE;
6891 }
6892 s = bfd_get_section_by_name (output_bfd, ".init_array");
6893 if (s != NULL && s->linker_has_input)
6894 {
6895 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6896 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6897 return FALSE;
6898 }
6899 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6900 if (s != NULL && s->linker_has_input)
6901 {
6902 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6903 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6904 return FALSE;
6905 }
6906
6907 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6908 /* If .dynstr is excluded from the link, we don't want any of
6909 these tags. Strictly, we should be checking each section
6910 individually; This quick check covers for the case where
6911 someone does a /DISCARD/ : { *(*) }. */
6912 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6913 {
6914 bfd_size_type strsize;
6915
6916 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6917 if ((info->emit_hash
6918 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6919 || (info->emit_gnu_hash
6920 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6921 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6922 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6923 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6924 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6925 bed->s->sizeof_sym))
6926 return FALSE;
6927 }
6928 }
6929
6930 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6931 return FALSE;
6932
6933 /* The backend must work out the sizes of all the other dynamic
6934 sections. */
6935 if (dynobj != NULL
6936 && bed->elf_backend_size_dynamic_sections != NULL
6937 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6938 return FALSE;
6939
6940 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6941 {
6942 if (elf_tdata (output_bfd)->cverdefs)
6943 {
6944 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6945
6946 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6947 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6948 return FALSE;
6949 }
6950
6951 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6952 {
6953 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6954 return FALSE;
6955 }
6956 else if (info->flags & DF_BIND_NOW)
6957 {
6958 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6959 return FALSE;
6960 }
6961
6962 if (info->flags_1)
6963 {
6964 if (bfd_link_executable (info))
6965 info->flags_1 &= ~ (DF_1_INITFIRST
6966 | DF_1_NODELETE
6967 | DF_1_NOOPEN);
6968 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6969 return FALSE;
6970 }
6971
6972 if (elf_tdata (output_bfd)->cverrefs)
6973 {
6974 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6975
6976 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6977 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6978 return FALSE;
6979 }
6980
6981 if ((elf_tdata (output_bfd)->cverrefs == 0
6982 && elf_tdata (output_bfd)->cverdefs == 0)
6983 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6984 {
6985 asection *s;
6986
6987 s = bfd_get_linker_section (dynobj, ".gnu.version");
6988 s->flags |= SEC_EXCLUDE;
6989 }
6990 }
6991 return TRUE;
6992}
6993
6994/* Find the first non-excluded output section. We'll use its
6995 section symbol for some emitted relocs. */
6996void
6997_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6998{
6999 asection *s;
7000
7001 for (s = output_bfd->sections; s != NULL; s = s->next)
7002 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7003 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7004 {
7005 elf_hash_table (info)->text_index_section = s;
7006 break;
7007 }
7008}
7009
7010/* Find two non-excluded output sections, one for code, one for data.
7011 We'll use their section symbols for some emitted relocs. */
7012void
7013_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7014{
7015 asection *s;
7016
7017 /* Data first, since setting text_index_section changes
7018 _bfd_elf_link_omit_section_dynsym. */
7019 for (s = output_bfd->sections; s != NULL; s = s->next)
7020 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7021 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7022 {
7023 elf_hash_table (info)->data_index_section = s;
7024 break;
7025 }
7026
7027 for (s = output_bfd->sections; s != NULL; s = s->next)
7028 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7029 == (SEC_ALLOC | SEC_READONLY))
7030 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7031 {
7032 elf_hash_table (info)->text_index_section = s;
7033 break;
7034 }
7035
7036 if (elf_hash_table (info)->text_index_section == NULL)
7037 elf_hash_table (info)->text_index_section
7038 = elf_hash_table (info)->data_index_section;
7039}
7040
7041bfd_boolean
7042bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7043{
7044 const struct elf_backend_data *bed;
7045 unsigned long section_sym_count;
7046 bfd_size_type dynsymcount = 0;
7047
7048 if (!is_elf_hash_table (info->hash))
7049 return TRUE;
7050
7051 bed = get_elf_backend_data (output_bfd);
7052 (*bed->elf_backend_init_index_section) (output_bfd, info);
7053
7054 /* Assign dynsym indices. In a shared library we generate a section
7055 symbol for each output section, which come first. Next come all
7056 of the back-end allocated local dynamic syms, followed by the rest
7057 of the global symbols.
7058
7059 This is usually not needed for static binaries, however backends
7060 can request to always do it, e.g. the MIPS backend uses dynamic
7061 symbol counts to lay out GOT, which will be produced in the
7062 presence of GOT relocations even in static binaries (holding fixed
7063 data in that case, to satisfy those relocations). */
7064
7065 if (elf_hash_table (info)->dynamic_sections_created
7066 || bed->always_renumber_dynsyms)
7067 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7068 &section_sym_count);
7069
7070 if (elf_hash_table (info)->dynamic_sections_created)
7071 {
7072 bfd *dynobj;
7073 asection *s;
7074 unsigned int dtagcount;
7075
7076 dynobj = elf_hash_table (info)->dynobj;
7077
7078 /* Work out the size of the symbol version section. */
7079 s = bfd_get_linker_section (dynobj, ".gnu.version");
7080 BFD_ASSERT (s != NULL);
7081 if ((s->flags & SEC_EXCLUDE) == 0)
7082 {
7083 s->size = dynsymcount * sizeof (Elf_External_Versym);
7084 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7085 if (s->contents == NULL)
7086 return FALSE;
7087
7088 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7089 return FALSE;
7090 }
7091
7092 /* Set the size of the .dynsym and .hash sections. We counted
7093 the number of dynamic symbols in elf_link_add_object_symbols.
7094 We will build the contents of .dynsym and .hash when we build
7095 the final symbol table, because until then we do not know the
7096 correct value to give the symbols. We built the .dynstr
7097 section as we went along in elf_link_add_object_symbols. */
7098 s = elf_hash_table (info)->dynsym;
7099 BFD_ASSERT (s != NULL);
7100 s->size = dynsymcount * bed->s->sizeof_sym;
7101
7102 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7103 if (s->contents == NULL)
7104 return FALSE;
7105
7106 /* The first entry in .dynsym is a dummy symbol. Clear all the
7107 section syms, in case we don't output them all. */
7108 ++section_sym_count;
7109 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7110
7111 elf_hash_table (info)->bucketcount = 0;
7112
7113 /* Compute the size of the hashing table. As a side effect this
7114 computes the hash values for all the names we export. */
7115 if (info->emit_hash)
7116 {
7117 unsigned long int *hashcodes;
7118 struct hash_codes_info hashinf;
7119 bfd_size_type amt;
7120 unsigned long int nsyms;
7121 size_t bucketcount;
7122 size_t hash_entry_size;
7123
7124 /* Compute the hash values for all exported symbols. At the same
7125 time store the values in an array so that we could use them for
7126 optimizations. */
7127 amt = dynsymcount * sizeof (unsigned long int);
7128 hashcodes = (unsigned long int *) bfd_malloc (amt);
7129 if (hashcodes == NULL)
7130 return FALSE;
7131 hashinf.hashcodes = hashcodes;
7132 hashinf.error = FALSE;
7133
7134 /* Put all hash values in HASHCODES. */
7135 elf_link_hash_traverse (elf_hash_table (info),
7136 elf_collect_hash_codes, &hashinf);
7137 if (hashinf.error)
7138 {
7139 free (hashcodes);
7140 return FALSE;
7141 }
7142
7143 nsyms = hashinf.hashcodes - hashcodes;
7144 bucketcount
7145 = compute_bucket_count (info, hashcodes, nsyms, 0);
7146 free (hashcodes);
7147
7148 if (bucketcount == 0 && nsyms > 0)
7149 return FALSE;
7150
7151 elf_hash_table (info)->bucketcount = bucketcount;
7152
7153 s = bfd_get_linker_section (dynobj, ".hash");
7154 BFD_ASSERT (s != NULL);
7155 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7156 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7157 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7158 if (s->contents == NULL)
7159 return FALSE;
7160
7161 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7162 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7163 s->contents + hash_entry_size);
7164 }
7165
7166 if (info->emit_gnu_hash)
7167 {
7168 size_t i, cnt;
7169 unsigned char *contents;
7170 struct collect_gnu_hash_codes cinfo;
7171 bfd_size_type amt;
7172 size_t bucketcount;
7173
7174 memset (&cinfo, 0, sizeof (cinfo));
7175
7176 /* Compute the hash values for all exported symbols. At the same
7177 time store the values in an array so that we could use them for
7178 optimizations. */
7179 amt = dynsymcount * 2 * sizeof (unsigned long int);
7180 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7181 if (cinfo.hashcodes == NULL)
7182 return FALSE;
7183
7184 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7185 cinfo.min_dynindx = -1;
7186 cinfo.output_bfd = output_bfd;
7187 cinfo.bed = bed;
7188
7189 /* Put all hash values in HASHCODES. */
7190 elf_link_hash_traverse (elf_hash_table (info),
7191 elf_collect_gnu_hash_codes, &cinfo);
7192 if (cinfo.error)
7193 {
7194 free (cinfo.hashcodes);
7195 return FALSE;
7196 }
7197
7198 bucketcount
7199 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7200
7201 if (bucketcount == 0)
7202 {
7203 free (cinfo.hashcodes);
7204 return FALSE;
7205 }
7206
7207 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7208 BFD_ASSERT (s != NULL);
7209
7210 if (cinfo.nsyms == 0)
7211 {
7212 /* Empty .gnu.hash section is special. */
7213 BFD_ASSERT (cinfo.min_dynindx == -1);
7214 free (cinfo.hashcodes);
7215 s->size = 5 * 4 + bed->s->arch_size / 8;
7216 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7217 if (contents == NULL)
7218 return FALSE;
7219 s->contents = contents;
7220 /* 1 empty bucket. */
7221 bfd_put_32 (output_bfd, 1, contents);
7222 /* SYMIDX above the special symbol 0. */
7223 bfd_put_32 (output_bfd, 1, contents + 4);
7224 /* Just one word for bitmask. */
7225 bfd_put_32 (output_bfd, 1, contents + 8);
7226 /* Only hash fn bloom filter. */
7227 bfd_put_32 (output_bfd, 0, contents + 12);
7228 /* No hashes are valid - empty bitmask. */
7229 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7230 /* No hashes in the only bucket. */
7231 bfd_put_32 (output_bfd, 0,
7232 contents + 16 + bed->s->arch_size / 8);
7233 }
7234 else
7235 {
7236 unsigned long int maskwords, maskbitslog2, x;
7237 BFD_ASSERT (cinfo.min_dynindx != -1);
7238
7239 x = cinfo.nsyms;
7240 maskbitslog2 = 1;
7241 while ((x >>= 1) != 0)
7242 ++maskbitslog2;
7243 if (maskbitslog2 < 3)
7244 maskbitslog2 = 5;
7245 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7246 maskbitslog2 = maskbitslog2 + 3;
7247 else
7248 maskbitslog2 = maskbitslog2 + 2;
7249 if (bed->s->arch_size == 64)
7250 {
7251 if (maskbitslog2 == 5)
7252 maskbitslog2 = 6;
7253 cinfo.shift1 = 6;
7254 }
7255 else
7256 cinfo.shift1 = 5;
7257 cinfo.mask = (1 << cinfo.shift1) - 1;
7258 cinfo.shift2 = maskbitslog2;
7259 cinfo.maskbits = 1 << maskbitslog2;
7260 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7261 amt = bucketcount * sizeof (unsigned long int) * 2;
7262 amt += maskwords * sizeof (bfd_vma);
7263 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7264 if (cinfo.bitmask == NULL)
7265 {
7266 free (cinfo.hashcodes);
7267 return FALSE;
7268 }
7269
7270 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7271 cinfo.indx = cinfo.counts + bucketcount;
7272 cinfo.symindx = dynsymcount - cinfo.nsyms;
7273 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7274
7275 /* Determine how often each hash bucket is used. */
7276 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7277 for (i = 0; i < cinfo.nsyms; ++i)
7278 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7279
7280 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7281 if (cinfo.counts[i] != 0)
7282 {
7283 cinfo.indx[i] = cnt;
7284 cnt += cinfo.counts[i];
7285 }
7286 BFD_ASSERT (cnt == dynsymcount);
7287 cinfo.bucketcount = bucketcount;
7288 cinfo.local_indx = cinfo.min_dynindx;
7289
7290 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7291 s->size += cinfo.maskbits / 8;
7292 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7293 if (contents == NULL)
7294 {
7295 free (cinfo.bitmask);
7296 free (cinfo.hashcodes);
7297 return FALSE;
7298 }
7299
7300 s->contents = contents;
7301 bfd_put_32 (output_bfd, bucketcount, contents);
7302 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7303 bfd_put_32 (output_bfd, maskwords, contents + 8);
7304 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7305 contents += 16 + cinfo.maskbits / 8;
7306
7307 for (i = 0; i < bucketcount; ++i)
7308 {
7309 if (cinfo.counts[i] == 0)
7310 bfd_put_32 (output_bfd, 0, contents);
7311 else
7312 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7313 contents += 4;
7314 }
7315
7316 cinfo.contents = contents;
7317
7318 /* Renumber dynamic symbols, populate .gnu.hash section. */
7319 elf_link_hash_traverse (elf_hash_table (info),
7320 elf_renumber_gnu_hash_syms, &cinfo);
7321
7322 contents = s->contents + 16;
7323 for (i = 0; i < maskwords; ++i)
7324 {
7325 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7326 contents);
7327 contents += bed->s->arch_size / 8;
7328 }
7329
7330 free (cinfo.bitmask);
7331 free (cinfo.hashcodes);
7332 }
7333 }
7334
7335 s = bfd_get_linker_section (dynobj, ".dynstr");
7336 BFD_ASSERT (s != NULL);
7337
7338 elf_finalize_dynstr (output_bfd, info);
7339
7340 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7341
7342 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7343 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7344 return FALSE;
7345 }
7346
7347 return TRUE;
7348}
7349\f
7350/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7351
7352static void
7353merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7354 asection *sec)
7355{
7356 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7357 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7358}
7359
7360/* Finish SHF_MERGE section merging. */
7361
7362bfd_boolean
7363_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7364{
7365 bfd *ibfd;
7366 asection *sec;
7367
7368 if (!is_elf_hash_table (info->hash))
7369 return FALSE;
7370
7371 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7372 if ((ibfd->flags & DYNAMIC) == 0
7373 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7374 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7375 == get_elf_backend_data (obfd)->s->elfclass))
7376 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7377 if ((sec->flags & SEC_MERGE) != 0
7378 && !bfd_is_abs_section (sec->output_section))
7379 {
7380 struct bfd_elf_section_data *secdata;
7381
7382 secdata = elf_section_data (sec);
7383 if (! _bfd_add_merge_section (obfd,
7384 &elf_hash_table (info)->merge_info,
7385 sec, &secdata->sec_info))
7386 return FALSE;
7387 else if (secdata->sec_info)
7388 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7389 }
7390
7391 if (elf_hash_table (info)->merge_info != NULL)
7392 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7393 merge_sections_remove_hook);
7394 return TRUE;
7395}
7396
7397/* Create an entry in an ELF linker hash table. */
7398
7399struct bfd_hash_entry *
7400_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7401 struct bfd_hash_table *table,
7402 const char *string)
7403{
7404 /* Allocate the structure if it has not already been allocated by a
7405 subclass. */
7406 if (entry == NULL)
7407 {
7408 entry = (struct bfd_hash_entry *)
7409 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7410 if (entry == NULL)
7411 return entry;
7412 }
7413
7414 /* Call the allocation method of the superclass. */
7415 entry = _bfd_link_hash_newfunc (entry, table, string);
7416 if (entry != NULL)
7417 {
7418 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7419 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7420
7421 /* Set local fields. */
7422 ret->indx = -1;
7423 ret->dynindx = -1;
7424 ret->got = htab->init_got_refcount;
7425 ret->plt = htab->init_plt_refcount;
7426 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7427 - offsetof (struct elf_link_hash_entry, size)));
7428 /* Assume that we have been called by a non-ELF symbol reader.
7429 This flag is then reset by the code which reads an ELF input
7430 file. This ensures that a symbol created by a non-ELF symbol
7431 reader will have the flag set correctly. */
7432 ret->non_elf = 1;
7433 }
7434
7435 return entry;
7436}
7437
7438/* Copy data from an indirect symbol to its direct symbol, hiding the
7439 old indirect symbol. Also used for copying flags to a weakdef. */
7440
7441void
7442_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7443 struct elf_link_hash_entry *dir,
7444 struct elf_link_hash_entry *ind)
7445{
7446 struct elf_link_hash_table *htab;
7447
7448 /* Copy down any references that we may have already seen to the
7449 symbol which just became indirect. */
7450
7451 if (dir->versioned != versioned_hidden)
7452 dir->ref_dynamic |= ind->ref_dynamic;
7453 dir->ref_regular |= ind->ref_regular;
7454 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7455 dir->non_got_ref |= ind->non_got_ref;
7456 dir->needs_plt |= ind->needs_plt;
7457 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7458
7459 if (ind->root.type != bfd_link_hash_indirect)
7460 return;
7461
7462 /* Copy over the global and procedure linkage table refcount entries.
7463 These may have been already set up by a check_relocs routine. */
7464 htab = elf_hash_table (info);
7465 if (ind->got.refcount > htab->init_got_refcount.refcount)
7466 {
7467 if (dir->got.refcount < 0)
7468 dir->got.refcount = 0;
7469 dir->got.refcount += ind->got.refcount;
7470 ind->got.refcount = htab->init_got_refcount.refcount;
7471 }
7472
7473 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7474 {
7475 if (dir->plt.refcount < 0)
7476 dir->plt.refcount = 0;
7477 dir->plt.refcount += ind->plt.refcount;
7478 ind->plt.refcount = htab->init_plt_refcount.refcount;
7479 }
7480
7481 if (ind->dynindx != -1)
7482 {
7483 if (dir->dynindx != -1)
7484 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7485 dir->dynindx = ind->dynindx;
7486 dir->dynstr_index = ind->dynstr_index;
7487 ind->dynindx = -1;
7488 ind->dynstr_index = 0;
7489 }
7490}
7491
7492void
7493_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7494 struct elf_link_hash_entry *h,
7495 bfd_boolean force_local)
7496{
7497 /* STT_GNU_IFUNC symbol must go through PLT. */
7498 if (h->type != STT_GNU_IFUNC)
7499 {
7500 h->plt = elf_hash_table (info)->init_plt_offset;
7501 h->needs_plt = 0;
7502 }
7503 if (force_local)
7504 {
7505 h->forced_local = 1;
7506 if (h->dynindx != -1)
7507 {
7508 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7509 h->dynstr_index);
7510 h->dynindx = -1;
7511 h->dynstr_index = 0;
7512 }
7513 }
7514}
7515
7516/* Hide a symbol. */
7517
7518void
7519_bfd_elf_link_hide_symbol (bfd *output_bfd,
7520 struct bfd_link_info *info,
7521 struct bfd_link_hash_entry *h)
7522{
7523 if (is_elf_hash_table (info->hash))
7524 {
7525 const struct elf_backend_data *bed
7526 = get_elf_backend_data (output_bfd);
7527 struct elf_link_hash_entry *eh
7528 = (struct elf_link_hash_entry *) h;
7529 bed->elf_backend_hide_symbol (info, eh, TRUE);
7530 eh->def_dynamic = 0;
7531 eh->ref_dynamic = 0;
7532 eh->dynamic_def = 0;
7533 }
7534}
7535
7536/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7537 caller. */
7538
7539bfd_boolean
7540_bfd_elf_link_hash_table_init
7541 (struct elf_link_hash_table *table,
7542 bfd *abfd,
7543 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7544 struct bfd_hash_table *,
7545 const char *),
7546 unsigned int entsize,
7547 enum elf_target_id target_id)
7548{
7549 bfd_boolean ret;
7550 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7551
7552 table->init_got_refcount.refcount = can_refcount - 1;
7553 table->init_plt_refcount.refcount = can_refcount - 1;
7554 table->init_got_offset.offset = -(bfd_vma) 1;
7555 table->init_plt_offset.offset = -(bfd_vma) 1;
7556 /* The first dynamic symbol is a dummy. */
7557 table->dynsymcount = 1;
7558
7559 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7560
7561 table->root.type = bfd_link_elf_hash_table;
7562 table->hash_table_id = target_id;
7563
7564 return ret;
7565}
7566
7567/* Create an ELF linker hash table. */
7568
7569struct bfd_link_hash_table *
7570_bfd_elf_link_hash_table_create (bfd *abfd)
7571{
7572 struct elf_link_hash_table *ret;
7573 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7574
7575 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7576 if (ret == NULL)
7577 return NULL;
7578
7579 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7580 sizeof (struct elf_link_hash_entry),
7581 GENERIC_ELF_DATA))
7582 {
7583 free (ret);
7584 return NULL;
7585 }
7586 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7587
7588 return &ret->root;
7589}
7590
7591/* Destroy an ELF linker hash table. */
7592
7593void
7594_bfd_elf_link_hash_table_free (bfd *obfd)
7595{
7596 struct elf_link_hash_table *htab;
7597
7598 htab = (struct elf_link_hash_table *) obfd->link.hash;
7599 if (htab->dynstr != NULL)
7600 _bfd_elf_strtab_free (htab->dynstr);
7601 _bfd_merge_sections_free (htab->merge_info);
7602 _bfd_generic_link_hash_table_free (obfd);
7603}
7604
7605/* This is a hook for the ELF emulation code in the generic linker to
7606 tell the backend linker what file name to use for the DT_NEEDED
7607 entry for a dynamic object. */
7608
7609void
7610bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7611{
7612 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7613 && bfd_get_format (abfd) == bfd_object)
7614 elf_dt_name (abfd) = name;
7615}
7616
7617int
7618bfd_elf_get_dyn_lib_class (bfd *abfd)
7619{
7620 int lib_class;
7621 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7622 && bfd_get_format (abfd) == bfd_object)
7623 lib_class = elf_dyn_lib_class (abfd);
7624 else
7625 lib_class = 0;
7626 return lib_class;
7627}
7628
7629void
7630bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7631{
7632 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7633 && bfd_get_format (abfd) == bfd_object)
7634 elf_dyn_lib_class (abfd) = lib_class;
7635}
7636
7637/* Get the list of DT_NEEDED entries for a link. This is a hook for
7638 the linker ELF emulation code. */
7639
7640struct bfd_link_needed_list *
7641bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7642 struct bfd_link_info *info)
7643{
7644 if (! is_elf_hash_table (info->hash))
7645 return NULL;
7646 return elf_hash_table (info)->needed;
7647}
7648
7649/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7650 hook for the linker ELF emulation code. */
7651
7652struct bfd_link_needed_list *
7653bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7654 struct bfd_link_info *info)
7655{
7656 if (! is_elf_hash_table (info->hash))
7657 return NULL;
7658 return elf_hash_table (info)->runpath;
7659}
7660
7661/* Get the name actually used for a dynamic object for a link. This
7662 is the SONAME entry if there is one. Otherwise, it is the string
7663 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7664
7665const char *
7666bfd_elf_get_dt_soname (bfd *abfd)
7667{
7668 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7669 && bfd_get_format (abfd) == bfd_object)
7670 return elf_dt_name (abfd);
7671 return NULL;
7672}
7673
7674/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7675 the ELF linker emulation code. */
7676
7677bfd_boolean
7678bfd_elf_get_bfd_needed_list (bfd *abfd,
7679 struct bfd_link_needed_list **pneeded)
7680{
7681 asection *s;
7682 bfd_byte *dynbuf = NULL;
7683 unsigned int elfsec;
7684 unsigned long shlink;
7685 bfd_byte *extdyn, *extdynend;
7686 size_t extdynsize;
7687 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7688
7689 *pneeded = NULL;
7690
7691 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7692 || bfd_get_format (abfd) != bfd_object)
7693 return TRUE;
7694
7695 s = bfd_get_section_by_name (abfd, ".dynamic");
7696 if (s == NULL || s->size == 0)
7697 return TRUE;
7698
7699 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7700 goto error_return;
7701
7702 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7703 if (elfsec == SHN_BAD)
7704 goto error_return;
7705
7706 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7707
7708 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7709 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7710
7711 extdyn = dynbuf;
7712 extdynend = extdyn + s->size;
7713 for (; extdyn < extdynend; extdyn += extdynsize)
7714 {
7715 Elf_Internal_Dyn dyn;
7716
7717 (*swap_dyn_in) (abfd, extdyn, &dyn);
7718
7719 if (dyn.d_tag == DT_NULL)
7720 break;
7721
7722 if (dyn.d_tag == DT_NEEDED)
7723 {
7724 const char *string;
7725 struct bfd_link_needed_list *l;
7726 unsigned int tagv = dyn.d_un.d_val;
7727 bfd_size_type amt;
7728
7729 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7730 if (string == NULL)
7731 goto error_return;
7732
7733 amt = sizeof *l;
7734 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7735 if (l == NULL)
7736 goto error_return;
7737
7738 l->by = abfd;
7739 l->name = string;
7740 l->next = *pneeded;
7741 *pneeded = l;
7742 }
7743 }
7744
7745 free (dynbuf);
7746
7747 return TRUE;
7748
7749 error_return:
7750 if (dynbuf != NULL)
7751 free (dynbuf);
7752 return FALSE;
7753}
7754
7755struct elf_symbuf_symbol
7756{
7757 unsigned long st_name; /* Symbol name, index in string tbl */
7758 unsigned char st_info; /* Type and binding attributes */
7759 unsigned char st_other; /* Visibilty, and target specific */
7760};
7761
7762struct elf_symbuf_head
7763{
7764 struct elf_symbuf_symbol *ssym;
7765 size_t count;
7766 unsigned int st_shndx;
7767};
7768
7769struct elf_symbol
7770{
7771 union
7772 {
7773 Elf_Internal_Sym *isym;
7774 struct elf_symbuf_symbol *ssym;
7775 } u;
7776 const char *name;
7777};
7778
7779/* Sort references to symbols by ascending section number. */
7780
7781static int
7782elf_sort_elf_symbol (const void *arg1, const void *arg2)
7783{
7784 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7785 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7786
7787 return s1->st_shndx - s2->st_shndx;
7788}
7789
7790static int
7791elf_sym_name_compare (const void *arg1, const void *arg2)
7792{
7793 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7794 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7795 return strcmp (s1->name, s2->name);
7796}
7797
7798static struct elf_symbuf_head *
7799elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7800{
7801 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7802 struct elf_symbuf_symbol *ssym;
7803 struct elf_symbuf_head *ssymbuf, *ssymhead;
7804 size_t i, shndx_count, total_size;
7805
7806 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7807 if (indbuf == NULL)
7808 return NULL;
7809
7810 for (ind = indbuf, i = 0; i < symcount; i++)
7811 if (isymbuf[i].st_shndx != SHN_UNDEF)
7812 *ind++ = &isymbuf[i];
7813 indbufend = ind;
7814
7815 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7816 elf_sort_elf_symbol);
7817
7818 shndx_count = 0;
7819 if (indbufend > indbuf)
7820 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7821 if (ind[0]->st_shndx != ind[1]->st_shndx)
7822 shndx_count++;
7823
7824 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7825 + (indbufend - indbuf) * sizeof (*ssym));
7826 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7827 if (ssymbuf == NULL)
7828 {
7829 free (indbuf);
7830 return NULL;
7831 }
7832
7833 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7834 ssymbuf->ssym = NULL;
7835 ssymbuf->count = shndx_count;
7836 ssymbuf->st_shndx = 0;
7837 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7838 {
7839 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7840 {
7841 ssymhead++;
7842 ssymhead->ssym = ssym;
7843 ssymhead->count = 0;
7844 ssymhead->st_shndx = (*ind)->st_shndx;
7845 }
7846 ssym->st_name = (*ind)->st_name;
7847 ssym->st_info = (*ind)->st_info;
7848 ssym->st_other = (*ind)->st_other;
7849 ssymhead->count++;
7850 }
7851 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7852 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7853 == total_size));
7854
7855 free (indbuf);
7856 return ssymbuf;
7857}
7858
7859/* Check if 2 sections define the same set of local and global
7860 symbols. */
7861
7862static bfd_boolean
7863bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7864 struct bfd_link_info *info)
7865{
7866 bfd *bfd1, *bfd2;
7867 const struct elf_backend_data *bed1, *bed2;
7868 Elf_Internal_Shdr *hdr1, *hdr2;
7869 size_t symcount1, symcount2;
7870 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7871 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7872 Elf_Internal_Sym *isym, *isymend;
7873 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7874 size_t count1, count2, i;
7875 unsigned int shndx1, shndx2;
7876 bfd_boolean result;
7877
7878 bfd1 = sec1->owner;
7879 bfd2 = sec2->owner;
7880
7881 /* Both sections have to be in ELF. */
7882 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7883 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7884 return FALSE;
7885
7886 if (elf_section_type (sec1) != elf_section_type (sec2))
7887 return FALSE;
7888
7889 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7890 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7891 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7892 return FALSE;
7893
7894 bed1 = get_elf_backend_data (bfd1);
7895 bed2 = get_elf_backend_data (bfd2);
7896 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7897 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7898 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7899 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7900
7901 if (symcount1 == 0 || symcount2 == 0)
7902 return FALSE;
7903
7904 result = FALSE;
7905 isymbuf1 = NULL;
7906 isymbuf2 = NULL;
7907 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7908 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7909
7910 if (ssymbuf1 == NULL)
7911 {
7912 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7913 NULL, NULL, NULL);
7914 if (isymbuf1 == NULL)
7915 goto done;
7916
7917 if (!info->reduce_memory_overheads)
7918 elf_tdata (bfd1)->symbuf = ssymbuf1
7919 = elf_create_symbuf (symcount1, isymbuf1);
7920 }
7921
7922 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7923 {
7924 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7925 NULL, NULL, NULL);
7926 if (isymbuf2 == NULL)
7927 goto done;
7928
7929 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7930 elf_tdata (bfd2)->symbuf = ssymbuf2
7931 = elf_create_symbuf (symcount2, isymbuf2);
7932 }
7933
7934 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7935 {
7936 /* Optimized faster version. */
7937 size_t lo, hi, mid;
7938 struct elf_symbol *symp;
7939 struct elf_symbuf_symbol *ssym, *ssymend;
7940
7941 lo = 0;
7942 hi = ssymbuf1->count;
7943 ssymbuf1++;
7944 count1 = 0;
7945 while (lo < hi)
7946 {
7947 mid = (lo + hi) / 2;
7948 if (shndx1 < ssymbuf1[mid].st_shndx)
7949 hi = mid;
7950 else if (shndx1 > ssymbuf1[mid].st_shndx)
7951 lo = mid + 1;
7952 else
7953 {
7954 count1 = ssymbuf1[mid].count;
7955 ssymbuf1 += mid;
7956 break;
7957 }
7958 }
7959
7960 lo = 0;
7961 hi = ssymbuf2->count;
7962 ssymbuf2++;
7963 count2 = 0;
7964 while (lo < hi)
7965 {
7966 mid = (lo + hi) / 2;
7967 if (shndx2 < ssymbuf2[mid].st_shndx)
7968 hi = mid;
7969 else if (shndx2 > ssymbuf2[mid].st_shndx)
7970 lo = mid + 1;
7971 else
7972 {
7973 count2 = ssymbuf2[mid].count;
7974 ssymbuf2 += mid;
7975 break;
7976 }
7977 }
7978
7979 if (count1 == 0 || count2 == 0 || count1 != count2)
7980 goto done;
7981
7982 symtable1
7983 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7984 symtable2
7985 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7986 if (symtable1 == NULL || symtable2 == NULL)
7987 goto done;
7988
7989 symp = symtable1;
7990 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7991 ssym < ssymend; ssym++, symp++)
7992 {
7993 symp->u.ssym = ssym;
7994 symp->name = bfd_elf_string_from_elf_section (bfd1,
7995 hdr1->sh_link,
7996 ssym->st_name);
7997 }
7998
7999 symp = symtable2;
8000 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8001 ssym < ssymend; ssym++, symp++)
8002 {
8003 symp->u.ssym = ssym;
8004 symp->name = bfd_elf_string_from_elf_section (bfd2,
8005 hdr2->sh_link,
8006 ssym->st_name);
8007 }
8008
8009 /* Sort symbol by name. */
8010 qsort (symtable1, count1, sizeof (struct elf_symbol),
8011 elf_sym_name_compare);
8012 qsort (symtable2, count1, sizeof (struct elf_symbol),
8013 elf_sym_name_compare);
8014
8015 for (i = 0; i < count1; i++)
8016 /* Two symbols must have the same binding, type and name. */
8017 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8018 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8019 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8020 goto done;
8021
8022 result = TRUE;
8023 goto done;
8024 }
8025
8026 symtable1 = (struct elf_symbol *)
8027 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8028 symtable2 = (struct elf_symbol *)
8029 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8030 if (symtable1 == NULL || symtable2 == NULL)
8031 goto done;
8032
8033 /* Count definitions in the section. */
8034 count1 = 0;
8035 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8036 if (isym->st_shndx == shndx1)
8037 symtable1[count1++].u.isym = isym;
8038
8039 count2 = 0;
8040 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8041 if (isym->st_shndx == shndx2)
8042 symtable2[count2++].u.isym = isym;
8043
8044 if (count1 == 0 || count2 == 0 || count1 != count2)
8045 goto done;
8046
8047 for (i = 0; i < count1; i++)
8048 symtable1[i].name
8049 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8050 symtable1[i].u.isym->st_name);
8051
8052 for (i = 0; i < count2; i++)
8053 symtable2[i].name
8054 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8055 symtable2[i].u.isym->st_name);
8056
8057 /* Sort symbol by name. */
8058 qsort (symtable1, count1, sizeof (struct elf_symbol),
8059 elf_sym_name_compare);
8060 qsort (symtable2, count1, sizeof (struct elf_symbol),
8061 elf_sym_name_compare);
8062
8063 for (i = 0; i < count1; i++)
8064 /* Two symbols must have the same binding, type and name. */
8065 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8066 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8067 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8068 goto done;
8069
8070 result = TRUE;
8071
8072done:
8073 if (symtable1)
8074 free (symtable1);
8075 if (symtable2)
8076 free (symtable2);
8077 if (isymbuf1)
8078 free (isymbuf1);
8079 if (isymbuf2)
8080 free (isymbuf2);
8081
8082 return result;
8083}
8084
8085/* Return TRUE if 2 section types are compatible. */
8086
8087bfd_boolean
8088_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8089 bfd *bbfd, const asection *bsec)
8090{
8091 if (asec == NULL
8092 || bsec == NULL
8093 || abfd->xvec->flavour != bfd_target_elf_flavour
8094 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8095 return TRUE;
8096
8097 return elf_section_type (asec) == elf_section_type (bsec);
8098}
8099\f
8100/* Final phase of ELF linker. */
8101
8102/* A structure we use to avoid passing large numbers of arguments. */
8103
8104struct elf_final_link_info
8105{
8106 /* General link information. */
8107 struct bfd_link_info *info;
8108 /* Output BFD. */
8109 bfd *output_bfd;
8110 /* Symbol string table. */
8111 struct elf_strtab_hash *symstrtab;
8112 /* .hash section. */
8113 asection *hash_sec;
8114 /* symbol version section (.gnu.version). */
8115 asection *symver_sec;
8116 /* Buffer large enough to hold contents of any section. */
8117 bfd_byte *contents;
8118 /* Buffer large enough to hold external relocs of any section. */
8119 void *external_relocs;
8120 /* Buffer large enough to hold internal relocs of any section. */
8121 Elf_Internal_Rela *internal_relocs;
8122 /* Buffer large enough to hold external local symbols of any input
8123 BFD. */
8124 bfd_byte *external_syms;
8125 /* And a buffer for symbol section indices. */
8126 Elf_External_Sym_Shndx *locsym_shndx;
8127 /* Buffer large enough to hold internal local symbols of any input
8128 BFD. */
8129 Elf_Internal_Sym *internal_syms;
8130 /* Array large enough to hold a symbol index for each local symbol
8131 of any input BFD. */
8132 long *indices;
8133 /* Array large enough to hold a section pointer for each local
8134 symbol of any input BFD. */
8135 asection **sections;
8136 /* Buffer for SHT_SYMTAB_SHNDX section. */
8137 Elf_External_Sym_Shndx *symshndxbuf;
8138 /* Number of STT_FILE syms seen. */
8139 size_t filesym_count;
8140};
8141
8142/* This struct is used to pass information to elf_link_output_extsym. */
8143
8144struct elf_outext_info
8145{
8146 bfd_boolean failed;
8147 bfd_boolean localsyms;
8148 bfd_boolean file_sym_done;
8149 struct elf_final_link_info *flinfo;
8150};
8151
8152
8153/* Support for evaluating a complex relocation.
8154
8155 Complex relocations are generalized, self-describing relocations. The
8156 implementation of them consists of two parts: complex symbols, and the
8157 relocations themselves.
8158
8159 The relocations are use a reserved elf-wide relocation type code (R_RELC
8160 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8161 information (start bit, end bit, word width, etc) into the addend. This
8162 information is extracted from CGEN-generated operand tables within gas.
8163
8164 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8165 internal) representing prefix-notation expressions, including but not
8166 limited to those sorts of expressions normally encoded as addends in the
8167 addend field. The symbol mangling format is:
8168
8169 <node> := <literal>
8170 | <unary-operator> ':' <node>
8171 | <binary-operator> ':' <node> ':' <node>
8172 ;
8173
8174 <literal> := 's' <digits=N> ':' <N character symbol name>
8175 | 'S' <digits=N> ':' <N character section name>
8176 | '#' <hexdigits>
8177 ;
8178
8179 <binary-operator> := as in C
8180 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8181
8182static void
8183set_symbol_value (bfd *bfd_with_globals,
8184 Elf_Internal_Sym *isymbuf,
8185 size_t locsymcount,
8186 size_t symidx,
8187 bfd_vma val)
8188{
8189 struct elf_link_hash_entry **sym_hashes;
8190 struct elf_link_hash_entry *h;
8191 size_t extsymoff = locsymcount;
8192
8193 if (symidx < locsymcount)
8194 {
8195 Elf_Internal_Sym *sym;
8196
8197 sym = isymbuf + symidx;
8198 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8199 {
8200 /* It is a local symbol: move it to the
8201 "absolute" section and give it a value. */
8202 sym->st_shndx = SHN_ABS;
8203 sym->st_value = val;
8204 return;
8205 }
8206 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8207 extsymoff = 0;
8208 }
8209
8210 /* It is a global symbol: set its link type
8211 to "defined" and give it a value. */
8212
8213 sym_hashes = elf_sym_hashes (bfd_with_globals);
8214 h = sym_hashes [symidx - extsymoff];
8215 while (h->root.type == bfd_link_hash_indirect
8216 || h->root.type == bfd_link_hash_warning)
8217 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8218 h->root.type = bfd_link_hash_defined;
8219 h->root.u.def.value = val;
8220 h->root.u.def.section = bfd_abs_section_ptr;
8221}
8222
8223static bfd_boolean
8224resolve_symbol (const char *name,
8225 bfd *input_bfd,
8226 struct elf_final_link_info *flinfo,
8227 bfd_vma *result,
8228 Elf_Internal_Sym *isymbuf,
8229 size_t locsymcount)
8230{
8231 Elf_Internal_Sym *sym;
8232 struct bfd_link_hash_entry *global_entry;
8233 const char *candidate = NULL;
8234 Elf_Internal_Shdr *symtab_hdr;
8235 size_t i;
8236
8237 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8238
8239 for (i = 0; i < locsymcount; ++ i)
8240 {
8241 sym = isymbuf + i;
8242
8243 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8244 continue;
8245
8246 candidate = bfd_elf_string_from_elf_section (input_bfd,
8247 symtab_hdr->sh_link,
8248 sym->st_name);
8249#ifdef DEBUG
8250 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8251 name, candidate, (unsigned long) sym->st_value);
8252#endif
8253 if (candidate && strcmp (candidate, name) == 0)
8254 {
8255 asection *sec = flinfo->sections [i];
8256
8257 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8258 *result += sec->output_offset + sec->output_section->vma;
8259#ifdef DEBUG
8260 printf ("Found symbol with value %8.8lx\n",
8261 (unsigned long) *result);
8262#endif
8263 return TRUE;
8264 }
8265 }
8266
8267 /* Hmm, haven't found it yet. perhaps it is a global. */
8268 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8269 FALSE, FALSE, TRUE);
8270 if (!global_entry)
8271 return FALSE;
8272
8273 if (global_entry->type == bfd_link_hash_defined
8274 || global_entry->type == bfd_link_hash_defweak)
8275 {
8276 *result = (global_entry->u.def.value
8277 + global_entry->u.def.section->output_section->vma
8278 + global_entry->u.def.section->output_offset);
8279#ifdef DEBUG
8280 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8281 global_entry->root.string, (unsigned long) *result);
8282#endif
8283 return TRUE;
8284 }
8285
8286 return FALSE;
8287}
8288
8289/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8290 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8291 names like "foo.end" which is the end address of section "foo". */
8292
8293static bfd_boolean
8294resolve_section (const char *name,
8295 asection *sections,
8296 bfd_vma *result,
8297 bfd * abfd)
8298{
8299 asection *curr;
8300 unsigned int len;
8301
8302 for (curr = sections; curr; curr = curr->next)
8303 if (strcmp (curr->name, name) == 0)
8304 {
8305 *result = curr->vma;
8306 return TRUE;
8307 }
8308
8309 /* Hmm. still haven't found it. try pseudo-section names. */
8310 /* FIXME: This could be coded more efficiently... */
8311 for (curr = sections; curr; curr = curr->next)
8312 {
8313 len = strlen (curr->name);
8314 if (len > strlen (name))
8315 continue;
8316
8317 if (strncmp (curr->name, name, len) == 0)
8318 {
8319 if (strncmp (".end", name + len, 4) == 0)
8320 {
8321 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8322 return TRUE;
8323 }
8324
8325 /* Insert more pseudo-section names here, if you like. */
8326 }
8327 }
8328
8329 return FALSE;
8330}
8331
8332static void
8333undefined_reference (const char *reftype, const char *name)
8334{
8335 /* xgettext:c-format */
8336 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8337 reftype, name);
8338}
8339
8340static bfd_boolean
8341eval_symbol (bfd_vma *result,
8342 const char **symp,
8343 bfd *input_bfd,
8344 struct elf_final_link_info *flinfo,
8345 bfd_vma dot,
8346 Elf_Internal_Sym *isymbuf,
8347 size_t locsymcount,
8348 int signed_p)
8349{
8350 size_t len;
8351 size_t symlen;
8352 bfd_vma a;
8353 bfd_vma b;
8354 char symbuf[4096];
8355 const char *sym = *symp;
8356 const char *symend;
8357 bfd_boolean symbol_is_section = FALSE;
8358
8359 len = strlen (sym);
8360 symend = sym + len;
8361
8362 if (len < 1 || len > sizeof (symbuf))
8363 {
8364 bfd_set_error (bfd_error_invalid_operation);
8365 return FALSE;
8366 }
8367
8368 switch (* sym)
8369 {
8370 case '.':
8371 *result = dot;
8372 *symp = sym + 1;
8373 return TRUE;
8374
8375 case '#':
8376 ++sym;
8377 *result = strtoul (sym, (char **) symp, 16);
8378 return TRUE;
8379
8380 case 'S':
8381 symbol_is_section = TRUE;
8382 /* Fall through. */
8383 case 's':
8384 ++sym;
8385 symlen = strtol (sym, (char **) symp, 10);
8386 sym = *symp + 1; /* Skip the trailing ':'. */
8387
8388 if (symend < sym || symlen + 1 > sizeof (symbuf))
8389 {
8390 bfd_set_error (bfd_error_invalid_operation);
8391 return FALSE;
8392 }
8393
8394 memcpy (symbuf, sym, symlen);
8395 symbuf[symlen] = '\0';
8396 *symp = sym + symlen;
8397
8398 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8399 the symbol as a section, or vice-versa. so we're pretty liberal in our
8400 interpretation here; section means "try section first", not "must be a
8401 section", and likewise with symbol. */
8402
8403 if (symbol_is_section)
8404 {
8405 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8406 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8407 isymbuf, locsymcount))
8408 {
8409 undefined_reference ("section", symbuf);
8410 return FALSE;
8411 }
8412 }
8413 else
8414 {
8415 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8416 isymbuf, locsymcount)
8417 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8418 result, input_bfd))
8419 {
8420 undefined_reference ("symbol", symbuf);
8421 return FALSE;
8422 }
8423 }
8424
8425 return TRUE;
8426
8427 /* All that remains are operators. */
8428
8429#define UNARY_OP(op) \
8430 if (strncmp (sym, #op, strlen (#op)) == 0) \
8431 { \
8432 sym += strlen (#op); \
8433 if (*sym == ':') \
8434 ++sym; \
8435 *symp = sym; \
8436 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8437 isymbuf, locsymcount, signed_p)) \
8438 return FALSE; \
8439 if (signed_p) \
8440 *result = op ((bfd_signed_vma) a); \
8441 else \
8442 *result = op a; \
8443 return TRUE; \
8444 }
8445
8446#define BINARY_OP(op) \
8447 if (strncmp (sym, #op, strlen (#op)) == 0) \
8448 { \
8449 sym += strlen (#op); \
8450 if (*sym == ':') \
8451 ++sym; \
8452 *symp = sym; \
8453 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8454 isymbuf, locsymcount, signed_p)) \
8455 return FALSE; \
8456 ++*symp; \
8457 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8458 isymbuf, locsymcount, signed_p)) \
8459 return FALSE; \
8460 if (signed_p) \
8461 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8462 else \
8463 *result = a op b; \
8464 return TRUE; \
8465 }
8466
8467 default:
8468 UNARY_OP (0-);
8469 BINARY_OP (<<);
8470 BINARY_OP (>>);
8471 BINARY_OP (==);
8472 BINARY_OP (!=);
8473 BINARY_OP (<=);
8474 BINARY_OP (>=);
8475 BINARY_OP (&&);
8476 BINARY_OP (||);
8477 UNARY_OP (~);
8478 UNARY_OP (!);
8479 BINARY_OP (*);
8480 BINARY_OP (/);
8481 BINARY_OP (%);
8482 BINARY_OP (^);
8483 BINARY_OP (|);
8484 BINARY_OP (&);
8485 BINARY_OP (+);
8486 BINARY_OP (-);
8487 BINARY_OP (<);
8488 BINARY_OP (>);
8489#undef UNARY_OP
8490#undef BINARY_OP
8491 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8492 bfd_set_error (bfd_error_invalid_operation);
8493 return FALSE;
8494 }
8495}
8496
8497static void
8498put_value (bfd_vma size,
8499 unsigned long chunksz,
8500 bfd *input_bfd,
8501 bfd_vma x,
8502 bfd_byte *location)
8503{
8504 location += (size - chunksz);
8505
8506 for (; size; size -= chunksz, location -= chunksz)
8507 {
8508 switch (chunksz)
8509 {
8510 case 1:
8511 bfd_put_8 (input_bfd, x, location);
8512 x >>= 8;
8513 break;
8514 case 2:
8515 bfd_put_16 (input_bfd, x, location);
8516 x >>= 16;
8517 break;
8518 case 4:
8519 bfd_put_32 (input_bfd, x, location);
8520 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8521 x >>= 16;
8522 x >>= 16;
8523 break;
8524#ifdef BFD64
8525 case 8:
8526 bfd_put_64 (input_bfd, x, location);
8527 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8528 x >>= 32;
8529 x >>= 32;
8530 break;
8531#endif
8532 default:
8533 abort ();
8534 break;
8535 }
8536 }
8537}
8538
8539static bfd_vma
8540get_value (bfd_vma size,
8541 unsigned long chunksz,
8542 bfd *input_bfd,
8543 bfd_byte *location)
8544{
8545 int shift;
8546 bfd_vma x = 0;
8547
8548 /* Sanity checks. */
8549 BFD_ASSERT (chunksz <= sizeof (x)
8550 && size >= chunksz
8551 && chunksz != 0
8552 && (size % chunksz) == 0
8553 && input_bfd != NULL
8554 && location != NULL);
8555
8556 if (chunksz == sizeof (x))
8557 {
8558 BFD_ASSERT (size == chunksz);
8559
8560 /* Make sure that we do not perform an undefined shift operation.
8561 We know that size == chunksz so there will only be one iteration
8562 of the loop below. */
8563 shift = 0;
8564 }
8565 else
8566 shift = 8 * chunksz;
8567
8568 for (; size; size -= chunksz, location += chunksz)
8569 {
8570 switch (chunksz)
8571 {
8572 case 1:
8573 x = (x << shift) | bfd_get_8 (input_bfd, location);
8574 break;
8575 case 2:
8576 x = (x << shift) | bfd_get_16 (input_bfd, location);
8577 break;
8578 case 4:
8579 x = (x << shift) | bfd_get_32 (input_bfd, location);
8580 break;
8581#ifdef BFD64
8582 case 8:
8583 x = (x << shift) | bfd_get_64 (input_bfd, location);
8584 break;
8585#endif
8586 default:
8587 abort ();
8588 }
8589 }
8590 return x;
8591}
8592
8593static void
8594decode_complex_addend (unsigned long *start, /* in bits */
8595 unsigned long *oplen, /* in bits */
8596 unsigned long *len, /* in bits */
8597 unsigned long *wordsz, /* in bytes */
8598 unsigned long *chunksz, /* in bytes */
8599 unsigned long *lsb0_p,
8600 unsigned long *signed_p,
8601 unsigned long *trunc_p,
8602 unsigned long encoded)
8603{
8604 * start = encoded & 0x3F;
8605 * len = (encoded >> 6) & 0x3F;
8606 * oplen = (encoded >> 12) & 0x3F;
8607 * wordsz = (encoded >> 18) & 0xF;
8608 * chunksz = (encoded >> 22) & 0xF;
8609 * lsb0_p = (encoded >> 27) & 1;
8610 * signed_p = (encoded >> 28) & 1;
8611 * trunc_p = (encoded >> 29) & 1;
8612}
8613
8614bfd_reloc_status_type
8615bfd_elf_perform_complex_relocation (bfd *input_bfd,
8616 asection *input_section ATTRIBUTE_UNUSED,
8617 bfd_byte *contents,
8618 Elf_Internal_Rela *rel,
8619 bfd_vma relocation)
8620{
8621 bfd_vma shift, x, mask;
8622 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8623 bfd_reloc_status_type r;
8624
8625 /* Perform this reloc, since it is complex.
8626 (this is not to say that it necessarily refers to a complex
8627 symbol; merely that it is a self-describing CGEN based reloc.
8628 i.e. the addend has the complete reloc information (bit start, end,
8629 word size, etc) encoded within it.). */
8630
8631 decode_complex_addend (&start, &oplen, &len, &wordsz,
8632 &chunksz, &lsb0_p, &signed_p,
8633 &trunc_p, rel->r_addend);
8634
8635 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8636
8637 if (lsb0_p)
8638 shift = (start + 1) - len;
8639 else
8640 shift = (8 * wordsz) - (start + len);
8641
8642 x = get_value (wordsz, chunksz, input_bfd,
8643 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8644
8645#ifdef DEBUG
8646 printf ("Doing complex reloc: "
8647 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8648 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8649 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8650 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8651 oplen, (unsigned long) x, (unsigned long) mask,
8652 (unsigned long) relocation);
8653#endif
8654
8655 r = bfd_reloc_ok;
8656 if (! trunc_p)
8657 /* Now do an overflow check. */
8658 r = bfd_check_overflow ((signed_p
8659 ? complain_overflow_signed
8660 : complain_overflow_unsigned),
8661 len, 0, (8 * wordsz),
8662 relocation);
8663
8664 /* Do the deed. */
8665 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8666
8667#ifdef DEBUG
8668 printf (" relocation: %8.8lx\n"
8669 " shifted mask: %8.8lx\n"
8670 " shifted/masked reloc: %8.8lx\n"
8671 " result: %8.8lx\n",
8672 (unsigned long) relocation, (unsigned long) (mask << shift),
8673 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8674#endif
8675 put_value (wordsz, chunksz, input_bfd, x,
8676 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8677 return r;
8678}
8679
8680/* Functions to read r_offset from external (target order) reloc
8681 entry. Faster than bfd_getl32 et al, because we let the compiler
8682 know the value is aligned. */
8683
8684static bfd_vma
8685ext32l_r_offset (const void *p)
8686{
8687 union aligned32
8688 {
8689 uint32_t v;
8690 unsigned char c[4];
8691 };
8692 const union aligned32 *a
8693 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8694
8695 uint32_t aval = ( (uint32_t) a->c[0]
8696 | (uint32_t) a->c[1] << 8
8697 | (uint32_t) a->c[2] << 16
8698 | (uint32_t) a->c[3] << 24);
8699 return aval;
8700}
8701
8702static bfd_vma
8703ext32b_r_offset (const void *p)
8704{
8705 union aligned32
8706 {
8707 uint32_t v;
8708 unsigned char c[4];
8709 };
8710 const union aligned32 *a
8711 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8712
8713 uint32_t aval = ( (uint32_t) a->c[0] << 24
8714 | (uint32_t) a->c[1] << 16
8715 | (uint32_t) a->c[2] << 8
8716 | (uint32_t) a->c[3]);
8717 return aval;
8718}
8719
8720#ifdef BFD_HOST_64_BIT
8721static bfd_vma
8722ext64l_r_offset (const void *p)
8723{
8724 union aligned64
8725 {
8726 uint64_t v;
8727 unsigned char c[8];
8728 };
8729 const union aligned64 *a
8730 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8731
8732 uint64_t aval = ( (uint64_t) a->c[0]
8733 | (uint64_t) a->c[1] << 8
8734 | (uint64_t) a->c[2] << 16
8735 | (uint64_t) a->c[3] << 24
8736 | (uint64_t) a->c[4] << 32
8737 | (uint64_t) a->c[5] << 40
8738 | (uint64_t) a->c[6] << 48
8739 | (uint64_t) a->c[7] << 56);
8740 return aval;
8741}
8742
8743static bfd_vma
8744ext64b_r_offset (const void *p)
8745{
8746 union aligned64
8747 {
8748 uint64_t v;
8749 unsigned char c[8];
8750 };
8751 const union aligned64 *a
8752 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8753
8754 uint64_t aval = ( (uint64_t) a->c[0] << 56
8755 | (uint64_t) a->c[1] << 48
8756 | (uint64_t) a->c[2] << 40
8757 | (uint64_t) a->c[3] << 32
8758 | (uint64_t) a->c[4] << 24
8759 | (uint64_t) a->c[5] << 16
8760 | (uint64_t) a->c[6] << 8
8761 | (uint64_t) a->c[7]);
8762 return aval;
8763}
8764#endif
8765
8766/* When performing a relocatable link, the input relocations are
8767 preserved. But, if they reference global symbols, the indices
8768 referenced must be updated. Update all the relocations found in
8769 RELDATA. */
8770
8771static bfd_boolean
8772elf_link_adjust_relocs (bfd *abfd,
8773 asection *sec,
8774 struct bfd_elf_section_reloc_data *reldata,
8775 bfd_boolean sort,
8776 struct bfd_link_info *info)
8777{
8778 unsigned int i;
8779 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8780 bfd_byte *erela;
8781 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8782 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8783 bfd_vma r_type_mask;
8784 int r_sym_shift;
8785 unsigned int count = reldata->count;
8786 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8787
8788 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8789 {
8790 swap_in = bed->s->swap_reloc_in;
8791 swap_out = bed->s->swap_reloc_out;
8792 }
8793 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8794 {
8795 swap_in = bed->s->swap_reloca_in;
8796 swap_out = bed->s->swap_reloca_out;
8797 }
8798 else
8799 abort ();
8800
8801 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8802 abort ();
8803
8804 if (bed->s->arch_size == 32)
8805 {
8806 r_type_mask = 0xff;
8807 r_sym_shift = 8;
8808 }
8809 else
8810 {
8811 r_type_mask = 0xffffffff;
8812 r_sym_shift = 32;
8813 }
8814
8815 erela = reldata->hdr->contents;
8816 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8817 {
8818 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8819 unsigned int j;
8820
8821 if (*rel_hash == NULL)
8822 continue;
8823
8824 if ((*rel_hash)->indx == -2
8825 && info->gc_sections
8826 && ! info->gc_keep_exported)
8827 {
8828 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8829 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8830 abfd, sec,
8831 (*rel_hash)->root.root.string);
8832 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8833 abfd, sec);
8834 bfd_set_error (bfd_error_invalid_operation);
8835 return FALSE;
8836 }
8837 BFD_ASSERT ((*rel_hash)->indx >= 0);
8838
8839 (*swap_in) (abfd, erela, irela);
8840 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8841 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8842 | (irela[j].r_info & r_type_mask));
8843 (*swap_out) (abfd, irela, erela);
8844 }
8845
8846 if (bed->elf_backend_update_relocs)
8847 (*bed->elf_backend_update_relocs) (sec, reldata);
8848
8849 if (sort && count != 0)
8850 {
8851 bfd_vma (*ext_r_off) (const void *);
8852 bfd_vma r_off;
8853 size_t elt_size;
8854 bfd_byte *base, *end, *p, *loc;
8855 bfd_byte *buf = NULL;
8856
8857 if (bed->s->arch_size == 32)
8858 {
8859 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8860 ext_r_off = ext32l_r_offset;
8861 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8862 ext_r_off = ext32b_r_offset;
8863 else
8864 abort ();
8865 }
8866 else
8867 {
8868#ifdef BFD_HOST_64_BIT
8869 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8870 ext_r_off = ext64l_r_offset;
8871 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8872 ext_r_off = ext64b_r_offset;
8873 else
8874#endif
8875 abort ();
8876 }
8877
8878 /* Must use a stable sort here. A modified insertion sort,
8879 since the relocs are mostly sorted already. */
8880 elt_size = reldata->hdr->sh_entsize;
8881 base = reldata->hdr->contents;
8882 end = base + count * elt_size;
8883 if (elt_size > sizeof (Elf64_External_Rela))
8884 abort ();
8885
8886 /* Ensure the first element is lowest. This acts as a sentinel,
8887 speeding the main loop below. */
8888 r_off = (*ext_r_off) (base);
8889 for (p = loc = base; (p += elt_size) < end; )
8890 {
8891 bfd_vma r_off2 = (*ext_r_off) (p);
8892 if (r_off > r_off2)
8893 {
8894 r_off = r_off2;
8895 loc = p;
8896 }
8897 }
8898 if (loc != base)
8899 {
8900 /* Don't just swap *base and *loc as that changes the order
8901 of the original base[0] and base[1] if they happen to
8902 have the same r_offset. */
8903 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8904 memcpy (onebuf, loc, elt_size);
8905 memmove (base + elt_size, base, loc - base);
8906 memcpy (base, onebuf, elt_size);
8907 }
8908
8909 for (p = base + elt_size; (p += elt_size) < end; )
8910 {
8911 /* base to p is sorted, *p is next to insert. */
8912 r_off = (*ext_r_off) (p);
8913 /* Search the sorted region for location to insert. */
8914 loc = p - elt_size;
8915 while (r_off < (*ext_r_off) (loc))
8916 loc -= elt_size;
8917 loc += elt_size;
8918 if (loc != p)
8919 {
8920 /* Chances are there is a run of relocs to insert here,
8921 from one of more input files. Files are not always
8922 linked in order due to the way elf_link_input_bfd is
8923 called. See pr17666. */
8924 size_t sortlen = p - loc;
8925 bfd_vma r_off2 = (*ext_r_off) (loc);
8926 size_t runlen = elt_size;
8927 size_t buf_size = 96 * 1024;
8928 while (p + runlen < end
8929 && (sortlen <= buf_size
8930 || runlen + elt_size <= buf_size)
8931 && r_off2 > (*ext_r_off) (p + runlen))
8932 runlen += elt_size;
8933 if (buf == NULL)
8934 {
8935 buf = bfd_malloc (buf_size);
8936 if (buf == NULL)
8937 return FALSE;
8938 }
8939 if (runlen < sortlen)
8940 {
8941 memcpy (buf, p, runlen);
8942 memmove (loc + runlen, loc, sortlen);
8943 memcpy (loc, buf, runlen);
8944 }
8945 else
8946 {
8947 memcpy (buf, loc, sortlen);
8948 memmove (loc, p, runlen);
8949 memcpy (loc + runlen, buf, sortlen);
8950 }
8951 p += runlen - elt_size;
8952 }
8953 }
8954 /* Hashes are no longer valid. */
8955 free (reldata->hashes);
8956 reldata->hashes = NULL;
8957 free (buf);
8958 }
8959 return TRUE;
8960}
8961
8962struct elf_link_sort_rela
8963{
8964 union {
8965 bfd_vma offset;
8966 bfd_vma sym_mask;
8967 } u;
8968 enum elf_reloc_type_class type;
8969 /* We use this as an array of size int_rels_per_ext_rel. */
8970 Elf_Internal_Rela rela[1];
8971};
8972
8973static int
8974elf_link_sort_cmp1 (const void *A, const void *B)
8975{
8976 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8977 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8978 int relativea, relativeb;
8979
8980 relativea = a->type == reloc_class_relative;
8981 relativeb = b->type == reloc_class_relative;
8982
8983 if (relativea < relativeb)
8984 return 1;
8985 if (relativea > relativeb)
8986 return -1;
8987 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8988 return -1;
8989 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8990 return 1;
8991 if (a->rela->r_offset < b->rela->r_offset)
8992 return -1;
8993 if (a->rela->r_offset > b->rela->r_offset)
8994 return 1;
8995 return 0;
8996}
8997
8998static int
8999elf_link_sort_cmp2 (const void *A, const void *B)
9000{
9001 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9002 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9003
9004 if (a->type < b->type)
9005 return -1;
9006 if (a->type > b->type)
9007 return 1;
9008 if (a->u.offset < b->u.offset)
9009 return -1;
9010 if (a->u.offset > b->u.offset)
9011 return 1;
9012 if (a->rela->r_offset < b->rela->r_offset)
9013 return -1;
9014 if (a->rela->r_offset > b->rela->r_offset)
9015 return 1;
9016 return 0;
9017}
9018
9019static size_t
9020elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9021{
9022 asection *dynamic_relocs;
9023 asection *rela_dyn;
9024 asection *rel_dyn;
9025 bfd_size_type count, size;
9026 size_t i, ret, sort_elt, ext_size;
9027 bfd_byte *sort, *s_non_relative, *p;
9028 struct elf_link_sort_rela *sq;
9029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9030 int i2e = bed->s->int_rels_per_ext_rel;
9031 unsigned int opb = bfd_octets_per_byte (abfd);
9032 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9033 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9034 struct bfd_link_order *lo;
9035 bfd_vma r_sym_mask;
9036 bfd_boolean use_rela;
9037
9038 /* Find a dynamic reloc section. */
9039 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9040 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9041 if (rela_dyn != NULL && rela_dyn->size > 0
9042 && rel_dyn != NULL && rel_dyn->size > 0)
9043 {
9044 bfd_boolean use_rela_initialised = FALSE;
9045
9046 /* This is just here to stop gcc from complaining.
9047 Its initialization checking code is not perfect. */
9048 use_rela = TRUE;
9049
9050 /* Both sections are present. Examine the sizes
9051 of the indirect sections to help us choose. */
9052 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9053 if (lo->type == bfd_indirect_link_order)
9054 {
9055 asection *o = lo->u.indirect.section;
9056
9057 if ((o->size % bed->s->sizeof_rela) == 0)
9058 {
9059 if ((o->size % bed->s->sizeof_rel) == 0)
9060 /* Section size is divisible by both rel and rela sizes.
9061 It is of no help to us. */
9062 ;
9063 else
9064 {
9065 /* Section size is only divisible by rela. */
9066 if (use_rela_initialised && !use_rela)
9067 {
9068 _bfd_error_handler (_("%pB: unable to sort relocs - "
9069 "they are in more than one size"),
9070 abfd);
9071 bfd_set_error (bfd_error_invalid_operation);
9072 return 0;
9073 }
9074 else
9075 {
9076 use_rela = TRUE;
9077 use_rela_initialised = TRUE;
9078 }
9079 }
9080 }
9081 else if ((o->size % bed->s->sizeof_rel) == 0)
9082 {
9083 /* Section size is only divisible by rel. */
9084 if (use_rela_initialised && use_rela)
9085 {
9086 _bfd_error_handler (_("%pB: unable to sort relocs - "
9087 "they are in more than one size"),
9088 abfd);
9089 bfd_set_error (bfd_error_invalid_operation);
9090 return 0;
9091 }
9092 else
9093 {
9094 use_rela = FALSE;
9095 use_rela_initialised = TRUE;
9096 }
9097 }
9098 else
9099 {
9100 /* The section size is not divisible by either -
9101 something is wrong. */
9102 _bfd_error_handler (_("%pB: unable to sort relocs - "
9103 "they are of an unknown size"), abfd);
9104 bfd_set_error (bfd_error_invalid_operation);
9105 return 0;
9106 }
9107 }
9108
9109 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9110 if (lo->type == bfd_indirect_link_order)
9111 {
9112 asection *o = lo->u.indirect.section;
9113
9114 if ((o->size % bed->s->sizeof_rela) == 0)
9115 {
9116 if ((o->size % bed->s->sizeof_rel) == 0)
9117 /* Section size is divisible by both rel and rela sizes.
9118 It is of no help to us. */
9119 ;
9120 else
9121 {
9122 /* Section size is only divisible by rela. */
9123 if (use_rela_initialised && !use_rela)
9124 {
9125 _bfd_error_handler (_("%pB: unable to sort relocs - "
9126 "they are in more than one size"),
9127 abfd);
9128 bfd_set_error (bfd_error_invalid_operation);
9129 return 0;
9130 }
9131 else
9132 {
9133 use_rela = TRUE;
9134 use_rela_initialised = TRUE;
9135 }
9136 }
9137 }
9138 else if ((o->size % bed->s->sizeof_rel) == 0)
9139 {
9140 /* Section size is only divisible by rel. */
9141 if (use_rela_initialised && use_rela)
9142 {
9143 _bfd_error_handler (_("%pB: unable to sort relocs - "
9144 "they are in more than one size"),
9145 abfd);
9146 bfd_set_error (bfd_error_invalid_operation);
9147 return 0;
9148 }
9149 else
9150 {
9151 use_rela = FALSE;
9152 use_rela_initialised = TRUE;
9153 }
9154 }
9155 else
9156 {
9157 /* The section size is not divisible by either -
9158 something is wrong. */
9159 _bfd_error_handler (_("%pB: unable to sort relocs - "
9160 "they are of an unknown size"), abfd);
9161 bfd_set_error (bfd_error_invalid_operation);
9162 return 0;
9163 }
9164 }
9165
9166 if (! use_rela_initialised)
9167 /* Make a guess. */
9168 use_rela = TRUE;
9169 }
9170 else if (rela_dyn != NULL && rela_dyn->size > 0)
9171 use_rela = TRUE;
9172 else if (rel_dyn != NULL && rel_dyn->size > 0)
9173 use_rela = FALSE;
9174 else
9175 return 0;
9176
9177 if (use_rela)
9178 {
9179 dynamic_relocs = rela_dyn;
9180 ext_size = bed->s->sizeof_rela;
9181 swap_in = bed->s->swap_reloca_in;
9182 swap_out = bed->s->swap_reloca_out;
9183 }
9184 else
9185 {
9186 dynamic_relocs = rel_dyn;
9187 ext_size = bed->s->sizeof_rel;
9188 swap_in = bed->s->swap_reloc_in;
9189 swap_out = bed->s->swap_reloc_out;
9190 }
9191
9192 size = 0;
9193 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9194 if (lo->type == bfd_indirect_link_order)
9195 size += lo->u.indirect.section->size;
9196
9197 if (size != dynamic_relocs->size)
9198 return 0;
9199
9200 sort_elt = (sizeof (struct elf_link_sort_rela)
9201 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9202
9203 count = dynamic_relocs->size / ext_size;
9204 if (count == 0)
9205 return 0;
9206 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9207
9208 if (sort == NULL)
9209 {
9210 (*info->callbacks->warning)
9211 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9212 return 0;
9213 }
9214
9215 if (bed->s->arch_size == 32)
9216 r_sym_mask = ~(bfd_vma) 0xff;
9217 else
9218 r_sym_mask = ~(bfd_vma) 0xffffffff;
9219
9220 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9221 if (lo->type == bfd_indirect_link_order)
9222 {
9223 bfd_byte *erel, *erelend;
9224 asection *o = lo->u.indirect.section;
9225
9226 if (o->contents == NULL && o->size != 0)
9227 {
9228 /* This is a reloc section that is being handled as a normal
9229 section. See bfd_section_from_shdr. We can't combine
9230 relocs in this case. */
9231 free (sort);
9232 return 0;
9233 }
9234 erel = o->contents;
9235 erelend = o->contents + o->size;
9236 p = sort + o->output_offset * opb / ext_size * sort_elt;
9237
9238 while (erel < erelend)
9239 {
9240 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9241
9242 (*swap_in) (abfd, erel, s->rela);
9243 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9244 s->u.sym_mask = r_sym_mask;
9245 p += sort_elt;
9246 erel += ext_size;
9247 }
9248 }
9249
9250 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9251
9252 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9253 {
9254 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9255 if (s->type != reloc_class_relative)
9256 break;
9257 }
9258 ret = i;
9259 s_non_relative = p;
9260
9261 sq = (struct elf_link_sort_rela *) s_non_relative;
9262 for (; i < count; i++, p += sort_elt)
9263 {
9264 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9265 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9266 sq = sp;
9267 sp->u.offset = sq->rela->r_offset;
9268 }
9269
9270 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9271
9272 struct elf_link_hash_table *htab = elf_hash_table (info);
9273 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9274 {
9275 /* We have plt relocs in .rela.dyn. */
9276 sq = (struct elf_link_sort_rela *) sort;
9277 for (i = 0; i < count; i++)
9278 if (sq[count - i - 1].type != reloc_class_plt)
9279 break;
9280 if (i != 0 && htab->srelplt->size == i * ext_size)
9281 {
9282 struct bfd_link_order **plo;
9283 /* Put srelplt link_order last. This is so the output_offset
9284 set in the next loop is correct for DT_JMPREL. */
9285 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9286 if ((*plo)->type == bfd_indirect_link_order
9287 && (*plo)->u.indirect.section == htab->srelplt)
9288 {
9289 lo = *plo;
9290 *plo = lo->next;
9291 }
9292 else
9293 plo = &(*plo)->next;
9294 *plo = lo;
9295 lo->next = NULL;
9296 dynamic_relocs->map_tail.link_order = lo;
9297 }
9298 }
9299
9300 p = sort;
9301 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9302 if (lo->type == bfd_indirect_link_order)
9303 {
9304 bfd_byte *erel, *erelend;
9305 asection *o = lo->u.indirect.section;
9306
9307 erel = o->contents;
9308 erelend = o->contents + o->size;
9309 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9310 while (erel < erelend)
9311 {
9312 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9313 (*swap_out) (abfd, s->rela, erel);
9314 p += sort_elt;
9315 erel += ext_size;
9316 }
9317 }
9318
9319 free (sort);
9320 *psec = dynamic_relocs;
9321 return ret;
9322}
9323
9324/* Add a symbol to the output symbol string table. */
9325
9326static int
9327elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9328 const char *name,
9329 Elf_Internal_Sym *elfsym,
9330 asection *input_sec,
9331 struct elf_link_hash_entry *h)
9332{
9333 int (*output_symbol_hook)
9334 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9335 struct elf_link_hash_entry *);
9336 struct elf_link_hash_table *hash_table;
9337 const struct elf_backend_data *bed;
9338 bfd_size_type strtabsize;
9339
9340 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9341
9342 bed = get_elf_backend_data (flinfo->output_bfd);
9343 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9344 if (output_symbol_hook != NULL)
9345 {
9346 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9347 if (ret != 1)
9348 return ret;
9349 }
9350
9351 if (name == NULL
9352 || *name == '\0'
9353 || (input_sec->flags & SEC_EXCLUDE))
9354 elfsym->st_name = (unsigned long) -1;
9355 else
9356 {
9357 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9358 to get the final offset for st_name. */
9359 elfsym->st_name
9360 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9361 name, FALSE);
9362 if (elfsym->st_name == (unsigned long) -1)
9363 return 0;
9364 }
9365
9366 hash_table = elf_hash_table (flinfo->info);
9367 strtabsize = hash_table->strtabsize;
9368 if (strtabsize <= hash_table->strtabcount)
9369 {
9370 strtabsize += strtabsize;
9371 hash_table->strtabsize = strtabsize;
9372 strtabsize *= sizeof (*hash_table->strtab);
9373 hash_table->strtab
9374 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9375 strtabsize);
9376 if (hash_table->strtab == NULL)
9377 return 0;
9378 }
9379 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9380 hash_table->strtab[hash_table->strtabcount].dest_index
9381 = hash_table->strtabcount;
9382 hash_table->strtab[hash_table->strtabcount].destshndx_index
9383 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9384
9385 bfd_get_symcount (flinfo->output_bfd) += 1;
9386 hash_table->strtabcount += 1;
9387
9388 return 1;
9389}
9390
9391/* Swap symbols out to the symbol table and flush the output symbols to
9392 the file. */
9393
9394static bfd_boolean
9395elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9396{
9397 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9398 bfd_size_type amt;
9399 size_t i;
9400 const struct elf_backend_data *bed;
9401 bfd_byte *symbuf;
9402 Elf_Internal_Shdr *hdr;
9403 file_ptr pos;
9404 bfd_boolean ret;
9405
9406 if (!hash_table->strtabcount)
9407 return TRUE;
9408
9409 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9410
9411 bed = get_elf_backend_data (flinfo->output_bfd);
9412
9413 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9414 symbuf = (bfd_byte *) bfd_malloc (amt);
9415 if (symbuf == NULL)
9416 return FALSE;
9417
9418 if (flinfo->symshndxbuf)
9419 {
9420 amt = sizeof (Elf_External_Sym_Shndx);
9421 amt *= bfd_get_symcount (flinfo->output_bfd);
9422 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9423 if (flinfo->symshndxbuf == NULL)
9424 {
9425 free (symbuf);
9426 return FALSE;
9427 }
9428 }
9429
9430 for (i = 0; i < hash_table->strtabcount; i++)
9431 {
9432 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9433 if (elfsym->sym.st_name == (unsigned long) -1)
9434 elfsym->sym.st_name = 0;
9435 else
9436 elfsym->sym.st_name
9437 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9438 elfsym->sym.st_name);
9439 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9440 ((bfd_byte *) symbuf
9441 + (elfsym->dest_index
9442 * bed->s->sizeof_sym)),
9443 (flinfo->symshndxbuf
9444 + elfsym->destshndx_index));
9445 }
9446
9447 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9448 pos = hdr->sh_offset + hdr->sh_size;
9449 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9450 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9451 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9452 {
9453 hdr->sh_size += amt;
9454 ret = TRUE;
9455 }
9456 else
9457 ret = FALSE;
9458
9459 free (symbuf);
9460
9461 free (hash_table->strtab);
9462 hash_table->strtab = NULL;
9463
9464 return ret;
9465}
9466
9467/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9468
9469static bfd_boolean
9470check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9471{
9472 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9473 && sym->st_shndx < SHN_LORESERVE)
9474 {
9475 /* The gABI doesn't support dynamic symbols in output sections
9476 beyond 64k. */
9477 _bfd_error_handler
9478 /* xgettext:c-format */
9479 (_("%pB: too many sections: %d (>= %d)"),
9480 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9481 bfd_set_error (bfd_error_nonrepresentable_section);
9482 return FALSE;
9483 }
9484 return TRUE;
9485}
9486
9487/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9488 allowing an unsatisfied unversioned symbol in the DSO to match a
9489 versioned symbol that would normally require an explicit version.
9490 We also handle the case that a DSO references a hidden symbol
9491 which may be satisfied by a versioned symbol in another DSO. */
9492
9493static bfd_boolean
9494elf_link_check_versioned_symbol (struct bfd_link_info *info,
9495 const struct elf_backend_data *bed,
9496 struct elf_link_hash_entry *h)
9497{
9498 bfd *abfd;
9499 struct elf_link_loaded_list *loaded;
9500
9501 if (!is_elf_hash_table (info->hash))
9502 return FALSE;
9503
9504 /* Check indirect symbol. */
9505 while (h->root.type == bfd_link_hash_indirect)
9506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9507
9508 switch (h->root.type)
9509 {
9510 default:
9511 abfd = NULL;
9512 break;
9513
9514 case bfd_link_hash_undefined:
9515 case bfd_link_hash_undefweak:
9516 abfd = h->root.u.undef.abfd;
9517 if (abfd == NULL
9518 || (abfd->flags & DYNAMIC) == 0
9519 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9520 return FALSE;
9521 break;
9522
9523 case bfd_link_hash_defined:
9524 case bfd_link_hash_defweak:
9525 abfd = h->root.u.def.section->owner;
9526 break;
9527
9528 case bfd_link_hash_common:
9529 abfd = h->root.u.c.p->section->owner;
9530 break;
9531 }
9532 BFD_ASSERT (abfd != NULL);
9533
9534 for (loaded = elf_hash_table (info)->loaded;
9535 loaded != NULL;
9536 loaded = loaded->next)
9537 {
9538 bfd *input;
9539 Elf_Internal_Shdr *hdr;
9540 size_t symcount;
9541 size_t extsymcount;
9542 size_t extsymoff;
9543 Elf_Internal_Shdr *versymhdr;
9544 Elf_Internal_Sym *isym;
9545 Elf_Internal_Sym *isymend;
9546 Elf_Internal_Sym *isymbuf;
9547 Elf_External_Versym *ever;
9548 Elf_External_Versym *extversym;
9549
9550 input = loaded->abfd;
9551
9552 /* We check each DSO for a possible hidden versioned definition. */
9553 if (input == abfd
9554 || (input->flags & DYNAMIC) == 0
9555 || elf_dynversym (input) == 0)
9556 continue;
9557
9558 hdr = &elf_tdata (input)->dynsymtab_hdr;
9559
9560 symcount = hdr->sh_size / bed->s->sizeof_sym;
9561 if (elf_bad_symtab (input))
9562 {
9563 extsymcount = symcount;
9564 extsymoff = 0;
9565 }
9566 else
9567 {
9568 extsymcount = symcount - hdr->sh_info;
9569 extsymoff = hdr->sh_info;
9570 }
9571
9572 if (extsymcount == 0)
9573 continue;
9574
9575 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9576 NULL, NULL, NULL);
9577 if (isymbuf == NULL)
9578 return FALSE;
9579
9580 /* Read in any version definitions. */
9581 versymhdr = &elf_tdata (input)->dynversym_hdr;
9582 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9583 if (extversym == NULL)
9584 goto error_ret;
9585
9586 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9587 || (bfd_bread (extversym, versymhdr->sh_size, input)
9588 != versymhdr->sh_size))
9589 {
9590 free (extversym);
9591 error_ret:
9592 free (isymbuf);
9593 return FALSE;
9594 }
9595
9596 ever = extversym + extsymoff;
9597 isymend = isymbuf + extsymcount;
9598 for (isym = isymbuf; isym < isymend; isym++, ever++)
9599 {
9600 const char *name;
9601 Elf_Internal_Versym iver;
9602 unsigned short version_index;
9603
9604 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9605 || isym->st_shndx == SHN_UNDEF)
9606 continue;
9607
9608 name = bfd_elf_string_from_elf_section (input,
9609 hdr->sh_link,
9610 isym->st_name);
9611 if (strcmp (name, h->root.root.string) != 0)
9612 continue;
9613
9614 _bfd_elf_swap_versym_in (input, ever, &iver);
9615
9616 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9617 && !(h->def_regular
9618 && h->forced_local))
9619 {
9620 /* If we have a non-hidden versioned sym, then it should
9621 have provided a definition for the undefined sym unless
9622 it is defined in a non-shared object and forced local.
9623 */
9624 abort ();
9625 }
9626
9627 version_index = iver.vs_vers & VERSYM_VERSION;
9628 if (version_index == 1 || version_index == 2)
9629 {
9630 /* This is the base or first version. We can use it. */
9631 free (extversym);
9632 free (isymbuf);
9633 return TRUE;
9634 }
9635 }
9636
9637 free (extversym);
9638 free (isymbuf);
9639 }
9640
9641 return FALSE;
9642}
9643
9644/* Convert ELF common symbol TYPE. */
9645
9646static int
9647elf_link_convert_common_type (struct bfd_link_info *info, int type)
9648{
9649 /* Commom symbol can only appear in relocatable link. */
9650 if (!bfd_link_relocatable (info))
9651 abort ();
9652 switch (info->elf_stt_common)
9653 {
9654 case unchanged:
9655 break;
9656 case elf_stt_common:
9657 type = STT_COMMON;
9658 break;
9659 case no_elf_stt_common:
9660 type = STT_OBJECT;
9661 break;
9662 }
9663 return type;
9664}
9665
9666/* Add an external symbol to the symbol table. This is called from
9667 the hash table traversal routine. When generating a shared object,
9668 we go through the symbol table twice. The first time we output
9669 anything that might have been forced to local scope in a version
9670 script. The second time we output the symbols that are still
9671 global symbols. */
9672
9673static bfd_boolean
9674elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9675{
9676 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9677 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9678 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9679 bfd_boolean strip;
9680 Elf_Internal_Sym sym;
9681 asection *input_sec;
9682 const struct elf_backend_data *bed;
9683 long indx;
9684 int ret;
9685 unsigned int type;
9686
9687 if (h->root.type == bfd_link_hash_warning)
9688 {
9689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9690 if (h->root.type == bfd_link_hash_new)
9691 return TRUE;
9692 }
9693
9694 /* Decide whether to output this symbol in this pass. */
9695 if (eoinfo->localsyms)
9696 {
9697 if (!h->forced_local)
9698 return TRUE;
9699 }
9700 else
9701 {
9702 if (h->forced_local)
9703 return TRUE;
9704 }
9705
9706 bed = get_elf_backend_data (flinfo->output_bfd);
9707
9708 if (h->root.type == bfd_link_hash_undefined)
9709 {
9710 /* If we have an undefined symbol reference here then it must have
9711 come from a shared library that is being linked in. (Undefined
9712 references in regular files have already been handled unless
9713 they are in unreferenced sections which are removed by garbage
9714 collection). */
9715 bfd_boolean ignore_undef = FALSE;
9716
9717 /* Some symbols may be special in that the fact that they're
9718 undefined can be safely ignored - let backend determine that. */
9719 if (bed->elf_backend_ignore_undef_symbol)
9720 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9721
9722 /* If we are reporting errors for this situation then do so now. */
9723 if (!ignore_undef
9724 && h->ref_dynamic
9725 && (!h->ref_regular || flinfo->info->gc_sections)
9726 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9727 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9728 (*flinfo->info->callbacks->undefined_symbol)
9729 (flinfo->info, h->root.root.string,
9730 h->ref_regular ? NULL : h->root.u.undef.abfd,
9731 NULL, 0,
9732 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9733
9734 /* Strip a global symbol defined in a discarded section. */
9735 if (h->indx == -3)
9736 return TRUE;
9737 }
9738
9739 /* We should also warn if a forced local symbol is referenced from
9740 shared libraries. */
9741 if (bfd_link_executable (flinfo->info)
9742 && h->forced_local
9743 && h->ref_dynamic
9744 && h->def_regular
9745 && !h->dynamic_def
9746 && h->ref_dynamic_nonweak
9747 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9748 {
9749 bfd *def_bfd;
9750 const char *msg;
9751 struct elf_link_hash_entry *hi = h;
9752
9753 /* Check indirect symbol. */
9754 while (hi->root.type == bfd_link_hash_indirect)
9755 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9756
9757 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9758 /* xgettext:c-format */
9759 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9760 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9761 /* xgettext:c-format */
9762 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9763 else
9764 /* xgettext:c-format */
9765 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9766 def_bfd = flinfo->output_bfd;
9767 if (hi->root.u.def.section != bfd_abs_section_ptr)
9768 def_bfd = hi->root.u.def.section->owner;
9769 _bfd_error_handler (msg, flinfo->output_bfd,
9770 h->root.root.string, def_bfd);
9771 bfd_set_error (bfd_error_bad_value);
9772 eoinfo->failed = TRUE;
9773 return FALSE;
9774 }
9775
9776 /* We don't want to output symbols that have never been mentioned by
9777 a regular file, or that we have been told to strip. However, if
9778 h->indx is set to -2, the symbol is used by a reloc and we must
9779 output it. */
9780 strip = FALSE;
9781 if (h->indx == -2)
9782 ;
9783 else if ((h->def_dynamic
9784 || h->ref_dynamic
9785 || h->root.type == bfd_link_hash_new)
9786 && !h->def_regular
9787 && !h->ref_regular)
9788 strip = TRUE;
9789 else if (flinfo->info->strip == strip_all)
9790 strip = TRUE;
9791 else if (flinfo->info->strip == strip_some
9792 && bfd_hash_lookup (flinfo->info->keep_hash,
9793 h->root.root.string, FALSE, FALSE) == NULL)
9794 strip = TRUE;
9795 else if ((h->root.type == bfd_link_hash_defined
9796 || h->root.type == bfd_link_hash_defweak)
9797 && ((flinfo->info->strip_discarded
9798 && discarded_section (h->root.u.def.section))
9799 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9800 && h->root.u.def.section->owner != NULL
9801 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9802 strip = TRUE;
9803 else if ((h->root.type == bfd_link_hash_undefined
9804 || h->root.type == bfd_link_hash_undefweak)
9805 && h->root.u.undef.abfd != NULL
9806 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9807 strip = TRUE;
9808
9809 type = h->type;
9810
9811 /* If we're stripping it, and it's not a dynamic symbol, there's
9812 nothing else to do. However, if it is a forced local symbol or
9813 an ifunc symbol we need to give the backend finish_dynamic_symbol
9814 function a chance to make it dynamic. */
9815 if (strip
9816 && h->dynindx == -1
9817 && type != STT_GNU_IFUNC
9818 && !h->forced_local)
9819 return TRUE;
9820
9821 sym.st_value = 0;
9822 sym.st_size = h->size;
9823 sym.st_other = h->other;
9824 switch (h->root.type)
9825 {
9826 default:
9827 case bfd_link_hash_new:
9828 case bfd_link_hash_warning:
9829 abort ();
9830 return FALSE;
9831
9832 case bfd_link_hash_undefined:
9833 case bfd_link_hash_undefweak:
9834 input_sec = bfd_und_section_ptr;
9835 sym.st_shndx = SHN_UNDEF;
9836 break;
9837
9838 case bfd_link_hash_defined:
9839 case bfd_link_hash_defweak:
9840 {
9841 input_sec = h->root.u.def.section;
9842 if (input_sec->output_section != NULL)
9843 {
9844 sym.st_shndx =
9845 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9846 input_sec->output_section);
9847 if (sym.st_shndx == SHN_BAD)
9848 {
9849 _bfd_error_handler
9850 /* xgettext:c-format */
9851 (_("%pB: could not find output section %pA for input section %pA"),
9852 flinfo->output_bfd, input_sec->output_section, input_sec);
9853 bfd_set_error (bfd_error_nonrepresentable_section);
9854 eoinfo->failed = TRUE;
9855 return FALSE;
9856 }
9857
9858 /* ELF symbols in relocatable files are section relative,
9859 but in nonrelocatable files they are virtual
9860 addresses. */
9861 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9862 if (!bfd_link_relocatable (flinfo->info))
9863 {
9864 sym.st_value += input_sec->output_section->vma;
9865 if (h->type == STT_TLS)
9866 {
9867 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9868 if (tls_sec != NULL)
9869 sym.st_value -= tls_sec->vma;
9870 }
9871 }
9872 }
9873 else
9874 {
9875 BFD_ASSERT (input_sec->owner == NULL
9876 || (input_sec->owner->flags & DYNAMIC) != 0);
9877 sym.st_shndx = SHN_UNDEF;
9878 input_sec = bfd_und_section_ptr;
9879 }
9880 }
9881 break;
9882
9883 case bfd_link_hash_common:
9884 input_sec = h->root.u.c.p->section;
9885 sym.st_shndx = bed->common_section_index (input_sec);
9886 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9887 break;
9888
9889 case bfd_link_hash_indirect:
9890 /* These symbols are created by symbol versioning. They point
9891 to the decorated version of the name. For example, if the
9892 symbol foo@@GNU_1.2 is the default, which should be used when
9893 foo is used with no version, then we add an indirect symbol
9894 foo which points to foo@@GNU_1.2. We ignore these symbols,
9895 since the indirected symbol is already in the hash table. */
9896 return TRUE;
9897 }
9898
9899 if (type == STT_COMMON || type == STT_OBJECT)
9900 switch (h->root.type)
9901 {
9902 case bfd_link_hash_common:
9903 type = elf_link_convert_common_type (flinfo->info, type);
9904 break;
9905 case bfd_link_hash_defined:
9906 case bfd_link_hash_defweak:
9907 if (bed->common_definition (&sym))
9908 type = elf_link_convert_common_type (flinfo->info, type);
9909 else
9910 type = STT_OBJECT;
9911 break;
9912 case bfd_link_hash_undefined:
9913 case bfd_link_hash_undefweak:
9914 break;
9915 default:
9916 abort ();
9917 }
9918
9919 if (h->forced_local)
9920 {
9921 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9922 /* Turn off visibility on local symbol. */
9923 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9924 }
9925 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9926 else if (h->unique_global && h->def_regular)
9927 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9928 else if (h->root.type == bfd_link_hash_undefweak
9929 || h->root.type == bfd_link_hash_defweak)
9930 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9931 else
9932 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9933 sym.st_target_internal = h->target_internal;
9934
9935 /* Give the processor backend a chance to tweak the symbol value,
9936 and also to finish up anything that needs to be done for this
9937 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9938 forced local syms when non-shared is due to a historical quirk.
9939 STT_GNU_IFUNC symbol must go through PLT. */
9940 if ((h->type == STT_GNU_IFUNC
9941 && h->def_regular
9942 && !bfd_link_relocatable (flinfo->info))
9943 || ((h->dynindx != -1
9944 || h->forced_local)
9945 && ((bfd_link_pic (flinfo->info)
9946 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9947 || h->root.type != bfd_link_hash_undefweak))
9948 || !h->forced_local)
9949 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9950 {
9951 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9952 (flinfo->output_bfd, flinfo->info, h, &sym)))
9953 {
9954 eoinfo->failed = TRUE;
9955 return FALSE;
9956 }
9957 }
9958
9959 /* If we are marking the symbol as undefined, and there are no
9960 non-weak references to this symbol from a regular object, then
9961 mark the symbol as weak undefined; if there are non-weak
9962 references, mark the symbol as strong. We can't do this earlier,
9963 because it might not be marked as undefined until the
9964 finish_dynamic_symbol routine gets through with it. */
9965 if (sym.st_shndx == SHN_UNDEF
9966 && h->ref_regular
9967 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9968 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9969 {
9970 int bindtype;
9971 type = ELF_ST_TYPE (sym.st_info);
9972
9973 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9974 if (type == STT_GNU_IFUNC)
9975 type = STT_FUNC;
9976
9977 if (h->ref_regular_nonweak)
9978 bindtype = STB_GLOBAL;
9979 else
9980 bindtype = STB_WEAK;
9981 sym.st_info = ELF_ST_INFO (bindtype, type);
9982 }
9983
9984 /* If this is a symbol defined in a dynamic library, don't use the
9985 symbol size from the dynamic library. Relinking an executable
9986 against a new library may introduce gratuitous changes in the
9987 executable's symbols if we keep the size. */
9988 if (sym.st_shndx == SHN_UNDEF
9989 && !h->def_regular
9990 && h->def_dynamic)
9991 sym.st_size = 0;
9992
9993 /* If a non-weak symbol with non-default visibility is not defined
9994 locally, it is a fatal error. */
9995 if (!bfd_link_relocatable (flinfo->info)
9996 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9997 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9998 && h->root.type == bfd_link_hash_undefined
9999 && !h->def_regular)
10000 {
10001 const char *msg;
10002
10003 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10004 /* xgettext:c-format */
10005 msg = _("%pB: protected symbol `%s' isn't defined");
10006 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10007 /* xgettext:c-format */
10008 msg = _("%pB: internal symbol `%s' isn't defined");
10009 else
10010 /* xgettext:c-format */
10011 msg = _("%pB: hidden symbol `%s' isn't defined");
10012 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10013 bfd_set_error (bfd_error_bad_value);
10014 eoinfo->failed = TRUE;
10015 return FALSE;
10016 }
10017
10018 /* If this symbol should be put in the .dynsym section, then put it
10019 there now. We already know the symbol index. We also fill in
10020 the entry in the .hash section. */
10021 if (elf_hash_table (flinfo->info)->dynsym != NULL
10022 && h->dynindx != -1
10023 && elf_hash_table (flinfo->info)->dynamic_sections_created)
10024 {
10025 bfd_byte *esym;
10026
10027 /* Since there is no version information in the dynamic string,
10028 if there is no version info in symbol version section, we will
10029 have a run-time problem if not linking executable, referenced
10030 by shared library, or not bound locally. */
10031 if (h->verinfo.verdef == NULL
10032 && (!bfd_link_executable (flinfo->info)
10033 || h->ref_dynamic
10034 || !h->def_regular))
10035 {
10036 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10037
10038 if (p && p [1] != '\0')
10039 {
10040 _bfd_error_handler
10041 /* xgettext:c-format */
10042 (_("%pB: no symbol version section for versioned symbol `%s'"),
10043 flinfo->output_bfd, h->root.root.string);
10044 eoinfo->failed = TRUE;
10045 return FALSE;
10046 }
10047 }
10048
10049 sym.st_name = h->dynstr_index;
10050 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10051 + h->dynindx * bed->s->sizeof_sym);
10052 if (!check_dynsym (flinfo->output_bfd, &sym))
10053 {
10054 eoinfo->failed = TRUE;
10055 return FALSE;
10056 }
10057 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10058
10059 if (flinfo->hash_sec != NULL)
10060 {
10061 size_t hash_entry_size;
10062 bfd_byte *bucketpos;
10063 bfd_vma chain;
10064 size_t bucketcount;
10065 size_t bucket;
10066
10067 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10068 bucket = h->u.elf_hash_value % bucketcount;
10069
10070 hash_entry_size
10071 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10072 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10073 + (bucket + 2) * hash_entry_size);
10074 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10075 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10076 bucketpos);
10077 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10078 ((bfd_byte *) flinfo->hash_sec->contents
10079 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10080 }
10081
10082 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10083 {
10084 Elf_Internal_Versym iversym;
10085 Elf_External_Versym *eversym;
10086
10087 if (!h->def_regular)
10088 {
10089 if (h->verinfo.verdef == NULL
10090 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10091 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10092 iversym.vs_vers = 0;
10093 else
10094 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10095 }
10096 else
10097 {
10098 if (h->verinfo.vertree == NULL)
10099 iversym.vs_vers = 1;
10100 else
10101 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10102 if (flinfo->info->create_default_symver)
10103 iversym.vs_vers++;
10104 }
10105
10106 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10107 defined locally. */
10108 if (h->versioned == versioned_hidden && h->def_regular)
10109 iversym.vs_vers |= VERSYM_HIDDEN;
10110
10111 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10112 eversym += h->dynindx;
10113 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10114 }
10115 }
10116
10117 /* If the symbol is undefined, and we didn't output it to .dynsym,
10118 strip it from .symtab too. Obviously we can't do this for
10119 relocatable output or when needed for --emit-relocs. */
10120 else if (input_sec == bfd_und_section_ptr
10121 && h->indx != -2
10122 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10123 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10124 && !bfd_link_relocatable (flinfo->info))
10125 return TRUE;
10126
10127 /* Also strip others that we couldn't earlier due to dynamic symbol
10128 processing. */
10129 if (strip)
10130 return TRUE;
10131 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10132 return TRUE;
10133
10134 /* Output a FILE symbol so that following locals are not associated
10135 with the wrong input file. We need one for forced local symbols
10136 if we've seen more than one FILE symbol or when we have exactly
10137 one FILE symbol but global symbols are present in a file other
10138 than the one with the FILE symbol. We also need one if linker
10139 defined symbols are present. In practice these conditions are
10140 always met, so just emit the FILE symbol unconditionally. */
10141 if (eoinfo->localsyms
10142 && !eoinfo->file_sym_done
10143 && eoinfo->flinfo->filesym_count != 0)
10144 {
10145 Elf_Internal_Sym fsym;
10146
10147 memset (&fsym, 0, sizeof (fsym));
10148 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10149 fsym.st_shndx = SHN_ABS;
10150 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10151 bfd_und_section_ptr, NULL))
10152 return FALSE;
10153
10154 eoinfo->file_sym_done = TRUE;
10155 }
10156
10157 indx = bfd_get_symcount (flinfo->output_bfd);
10158 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10159 input_sec, h);
10160 if (ret == 0)
10161 {
10162 eoinfo->failed = TRUE;
10163 return FALSE;
10164 }
10165 else if (ret == 1)
10166 h->indx = indx;
10167 else if (h->indx == -2)
10168 abort();
10169
10170 return TRUE;
10171}
10172
10173/* Return TRUE if special handling is done for relocs in SEC against
10174 symbols defined in discarded sections. */
10175
10176static bfd_boolean
10177elf_section_ignore_discarded_relocs (asection *sec)
10178{
10179 const struct elf_backend_data *bed;
10180
10181 switch (sec->sec_info_type)
10182 {
10183 case SEC_INFO_TYPE_STABS:
10184 case SEC_INFO_TYPE_EH_FRAME:
10185 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10186 return TRUE;
10187 default:
10188 break;
10189 }
10190
10191 bed = get_elf_backend_data (sec->owner);
10192 if (bed->elf_backend_ignore_discarded_relocs != NULL
10193 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10194 return TRUE;
10195
10196 return FALSE;
10197}
10198
10199/* Return a mask saying how ld should treat relocations in SEC against
10200 symbols defined in discarded sections. If this function returns
10201 COMPLAIN set, ld will issue a warning message. If this function
10202 returns PRETEND set, and the discarded section was link-once and the
10203 same size as the kept link-once section, ld will pretend that the
10204 symbol was actually defined in the kept section. Otherwise ld will
10205 zero the reloc (at least that is the intent, but some cooperation by
10206 the target dependent code is needed, particularly for REL targets). */
10207
10208unsigned int
10209_bfd_elf_default_action_discarded (asection *sec)
10210{
10211 if (sec->flags & SEC_DEBUGGING)
10212 return PRETEND;
10213
10214 if (strcmp (".eh_frame", sec->name) == 0)
10215 return 0;
10216
10217 if (strcmp (".gcc_except_table", sec->name) == 0)
10218 return 0;
10219
10220 return COMPLAIN | PRETEND;
10221}
10222
10223/* Find a match between a section and a member of a section group. */
10224
10225static asection *
10226match_group_member (asection *sec, asection *group,
10227 struct bfd_link_info *info)
10228{
10229 asection *first = elf_next_in_group (group);
10230 asection *s = first;
10231
10232 while (s != NULL)
10233 {
10234 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10235 return s;
10236
10237 s = elf_next_in_group (s);
10238 if (s == first)
10239 break;
10240 }
10241
10242 return NULL;
10243}
10244
10245/* Check if the kept section of a discarded section SEC can be used
10246 to replace it. Return the replacement if it is OK. Otherwise return
10247 NULL. */
10248
10249asection *
10250_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10251{
10252 asection *kept;
10253
10254 kept = sec->kept_section;
10255 if (kept != NULL)
10256 {
10257 if ((kept->flags & SEC_GROUP) != 0)
10258 kept = match_group_member (sec, kept, info);
10259 if (kept != NULL
10260 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10261 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10262 kept = NULL;
10263 sec->kept_section = kept;
10264 }
10265 return kept;
10266}
10267
10268/* Link an input file into the linker output file. This function
10269 handles all the sections and relocations of the input file at once.
10270 This is so that we only have to read the local symbols once, and
10271 don't have to keep them in memory. */
10272
10273static bfd_boolean
10274elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10275{
10276 int (*relocate_section)
10277 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10278 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10279 bfd *output_bfd;
10280 Elf_Internal_Shdr *symtab_hdr;
10281 size_t locsymcount;
10282 size_t extsymoff;
10283 Elf_Internal_Sym *isymbuf;
10284 Elf_Internal_Sym *isym;
10285 Elf_Internal_Sym *isymend;
10286 long *pindex;
10287 asection **ppsection;
10288 asection *o;
10289 const struct elf_backend_data *bed;
10290 struct elf_link_hash_entry **sym_hashes;
10291 bfd_size_type address_size;
10292 bfd_vma r_type_mask;
10293 int r_sym_shift;
10294 bfd_boolean have_file_sym = FALSE;
10295
10296 output_bfd = flinfo->output_bfd;
10297 bed = get_elf_backend_data (output_bfd);
10298 relocate_section = bed->elf_backend_relocate_section;
10299
10300 /* If this is a dynamic object, we don't want to do anything here:
10301 we don't want the local symbols, and we don't want the section
10302 contents. */
10303 if ((input_bfd->flags & DYNAMIC) != 0)
10304 return TRUE;
10305
10306 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10307 if (elf_bad_symtab (input_bfd))
10308 {
10309 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10310 extsymoff = 0;
10311 }
10312 else
10313 {
10314 locsymcount = symtab_hdr->sh_info;
10315 extsymoff = symtab_hdr->sh_info;
10316 }
10317
10318 /* Read the local symbols. */
10319 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10320 if (isymbuf == NULL && locsymcount != 0)
10321 {
10322 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10323 flinfo->internal_syms,
10324 flinfo->external_syms,
10325 flinfo->locsym_shndx);
10326 if (isymbuf == NULL)
10327 return FALSE;
10328 }
10329
10330 /* Find local symbol sections and adjust values of symbols in
10331 SEC_MERGE sections. Write out those local symbols we know are
10332 going into the output file. */
10333 isymend = isymbuf + locsymcount;
10334 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10335 isym < isymend;
10336 isym++, pindex++, ppsection++)
10337 {
10338 asection *isec;
10339 const char *name;
10340 Elf_Internal_Sym osym;
10341 long indx;
10342 int ret;
10343
10344 *pindex = -1;
10345
10346 if (elf_bad_symtab (input_bfd))
10347 {
10348 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10349 {
10350 *ppsection = NULL;
10351 continue;
10352 }
10353 }
10354
10355 if (isym->st_shndx == SHN_UNDEF)
10356 isec = bfd_und_section_ptr;
10357 else if (isym->st_shndx == SHN_ABS)
10358 isec = bfd_abs_section_ptr;
10359 else if (isym->st_shndx == SHN_COMMON)
10360 isec = bfd_com_section_ptr;
10361 else
10362 {
10363 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10364 if (isec == NULL)
10365 {
10366 /* Don't attempt to output symbols with st_shnx in the
10367 reserved range other than SHN_ABS and SHN_COMMON. */
10368 *ppsection = NULL;
10369 continue;
10370 }
10371 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10372 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10373 isym->st_value =
10374 _bfd_merged_section_offset (output_bfd, &isec,
10375 elf_section_data (isec)->sec_info,
10376 isym->st_value);
10377 }
10378
10379 *ppsection = isec;
10380
10381 /* Don't output the first, undefined, symbol. In fact, don't
10382 output any undefined local symbol. */
10383 if (isec == bfd_und_section_ptr)
10384 continue;
10385
10386 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10387 {
10388 /* We never output section symbols. Instead, we use the
10389 section symbol of the corresponding section in the output
10390 file. */
10391 continue;
10392 }
10393
10394 /* If we are stripping all symbols, we don't want to output this
10395 one. */
10396 if (flinfo->info->strip == strip_all)
10397 continue;
10398
10399 /* If we are discarding all local symbols, we don't want to
10400 output this one. If we are generating a relocatable output
10401 file, then some of the local symbols may be required by
10402 relocs; we output them below as we discover that they are
10403 needed. */
10404 if (flinfo->info->discard == discard_all)
10405 continue;
10406
10407 /* If this symbol is defined in a section which we are
10408 discarding, we don't need to keep it. */
10409 if (isym->st_shndx != SHN_UNDEF
10410 && isym->st_shndx < SHN_LORESERVE
10411 && bfd_section_removed_from_list (output_bfd,
10412 isec->output_section))
10413 continue;
10414
10415 /* Get the name of the symbol. */
10416 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10417 isym->st_name);
10418 if (name == NULL)
10419 return FALSE;
10420
10421 /* See if we are discarding symbols with this name. */
10422 if ((flinfo->info->strip == strip_some
10423 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10424 == NULL))
10425 || (((flinfo->info->discard == discard_sec_merge
10426 && (isec->flags & SEC_MERGE)
10427 && !bfd_link_relocatable (flinfo->info))
10428 || flinfo->info->discard == discard_l)
10429 && bfd_is_local_label_name (input_bfd, name)))
10430 continue;
10431
10432 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10433 {
10434 if (input_bfd->lto_output)
10435 /* -flto puts a temp file name here. This means builds
10436 are not reproducible. Discard the symbol. */
10437 continue;
10438 have_file_sym = TRUE;
10439 flinfo->filesym_count += 1;
10440 }
10441 if (!have_file_sym)
10442 {
10443 /* In the absence of debug info, bfd_find_nearest_line uses
10444 FILE symbols to determine the source file for local
10445 function symbols. Provide a FILE symbol here if input
10446 files lack such, so that their symbols won't be
10447 associated with a previous input file. It's not the
10448 source file, but the best we can do. */
10449 have_file_sym = TRUE;
10450 flinfo->filesym_count += 1;
10451 memset (&osym, 0, sizeof (osym));
10452 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10453 osym.st_shndx = SHN_ABS;
10454 if (!elf_link_output_symstrtab (flinfo,
10455 (input_bfd->lto_output ? NULL
10456 : input_bfd->filename),
10457 &osym, bfd_abs_section_ptr,
10458 NULL))
10459 return FALSE;
10460 }
10461
10462 osym = *isym;
10463
10464 /* Adjust the section index for the output file. */
10465 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10466 isec->output_section);
10467 if (osym.st_shndx == SHN_BAD)
10468 return FALSE;
10469
10470 /* ELF symbols in relocatable files are section relative, but
10471 in executable files they are virtual addresses. Note that
10472 this code assumes that all ELF sections have an associated
10473 BFD section with a reasonable value for output_offset; below
10474 we assume that they also have a reasonable value for
10475 output_section. Any special sections must be set up to meet
10476 these requirements. */
10477 osym.st_value += isec->output_offset;
10478 if (!bfd_link_relocatable (flinfo->info))
10479 {
10480 osym.st_value += isec->output_section->vma;
10481 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10482 {
10483 /* STT_TLS symbols are relative to PT_TLS segment base. */
10484 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10485 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10486 }
10487 }
10488
10489 indx = bfd_get_symcount (output_bfd);
10490 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10491 if (ret == 0)
10492 return FALSE;
10493 else if (ret == 1)
10494 *pindex = indx;
10495 }
10496
10497 if (bed->s->arch_size == 32)
10498 {
10499 r_type_mask = 0xff;
10500 r_sym_shift = 8;
10501 address_size = 4;
10502 }
10503 else
10504 {
10505 r_type_mask = 0xffffffff;
10506 r_sym_shift = 32;
10507 address_size = 8;
10508 }
10509
10510 /* Relocate the contents of each section. */
10511 sym_hashes = elf_sym_hashes (input_bfd);
10512 for (o = input_bfd->sections; o != NULL; o = o->next)
10513 {
10514 bfd_byte *contents;
10515
10516 if (! o->linker_mark)
10517 {
10518 /* This section was omitted from the link. */
10519 continue;
10520 }
10521
10522 if (!flinfo->info->resolve_section_groups
10523 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10524 {
10525 /* Deal with the group signature symbol. */
10526 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10527 unsigned long symndx = sec_data->this_hdr.sh_info;
10528 asection *osec = o->output_section;
10529
10530 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10531 if (symndx >= locsymcount
10532 || (elf_bad_symtab (input_bfd)
10533 && flinfo->sections[symndx] == NULL))
10534 {
10535 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10536 while (h->root.type == bfd_link_hash_indirect
10537 || h->root.type == bfd_link_hash_warning)
10538 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10539 /* Arrange for symbol to be output. */
10540 h->indx = -2;
10541 elf_section_data (osec)->this_hdr.sh_info = -2;
10542 }
10543 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10544 {
10545 /* We'll use the output section target_index. */
10546 asection *sec = flinfo->sections[symndx]->output_section;
10547 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10548 }
10549 else
10550 {
10551 if (flinfo->indices[symndx] == -1)
10552 {
10553 /* Otherwise output the local symbol now. */
10554 Elf_Internal_Sym sym = isymbuf[symndx];
10555 asection *sec = flinfo->sections[symndx]->output_section;
10556 const char *name;
10557 long indx;
10558 int ret;
10559
10560 name = bfd_elf_string_from_elf_section (input_bfd,
10561 symtab_hdr->sh_link,
10562 sym.st_name);
10563 if (name == NULL)
10564 return FALSE;
10565
10566 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10567 sec);
10568 if (sym.st_shndx == SHN_BAD)
10569 return FALSE;
10570
10571 sym.st_value += o->output_offset;
10572
10573 indx = bfd_get_symcount (output_bfd);
10574 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10575 NULL);
10576 if (ret == 0)
10577 return FALSE;
10578 else if (ret == 1)
10579 flinfo->indices[symndx] = indx;
10580 else
10581 abort ();
10582 }
10583 elf_section_data (osec)->this_hdr.sh_info
10584 = flinfo->indices[symndx];
10585 }
10586 }
10587
10588 if ((o->flags & SEC_HAS_CONTENTS) == 0
10589 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10590 continue;
10591
10592 if ((o->flags & SEC_LINKER_CREATED) != 0)
10593 {
10594 /* Section was created by _bfd_elf_link_create_dynamic_sections
10595 or somesuch. */
10596 continue;
10597 }
10598
10599 /* Get the contents of the section. They have been cached by a
10600 relaxation routine. Note that o is a section in an input
10601 file, so the contents field will not have been set by any of
10602 the routines which work on output files. */
10603 if (elf_section_data (o)->this_hdr.contents != NULL)
10604 {
10605 contents = elf_section_data (o)->this_hdr.contents;
10606 if (bed->caches_rawsize
10607 && o->rawsize != 0
10608 && o->rawsize < o->size)
10609 {
10610 memcpy (flinfo->contents, contents, o->rawsize);
10611 contents = flinfo->contents;
10612 }
10613 }
10614 else
10615 {
10616 contents = flinfo->contents;
10617 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10618 return FALSE;
10619 }
10620
10621 if ((o->flags & SEC_RELOC) != 0)
10622 {
10623 Elf_Internal_Rela *internal_relocs;
10624 Elf_Internal_Rela *rel, *relend;
10625 int action_discarded;
10626 int ret;
10627
10628 /* Get the swapped relocs. */
10629 internal_relocs
10630 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10631 flinfo->internal_relocs, FALSE);
10632 if (internal_relocs == NULL
10633 && o->reloc_count > 0)
10634 return FALSE;
10635
10636 /* We need to reverse-copy input .ctors/.dtors sections if
10637 they are placed in .init_array/.finit_array for output. */
10638 if (o->size > address_size
10639 && ((strncmp (o->name, ".ctors", 6) == 0
10640 && strcmp (o->output_section->name,
10641 ".init_array") == 0)
10642 || (strncmp (o->name, ".dtors", 6) == 0
10643 && strcmp (o->output_section->name,
10644 ".fini_array") == 0))
10645 && (o->name[6] == 0 || o->name[6] == '.'))
10646 {
10647 if (o->size * bed->s->int_rels_per_ext_rel
10648 != o->reloc_count * address_size)
10649 {
10650 _bfd_error_handler
10651 /* xgettext:c-format */
10652 (_("error: %pB: size of section %pA is not "
10653 "multiple of address size"),
10654 input_bfd, o);
10655 bfd_set_error (bfd_error_bad_value);
10656 return FALSE;
10657 }
10658 o->flags |= SEC_ELF_REVERSE_COPY;
10659 }
10660
10661 action_discarded = -1;
10662 if (!elf_section_ignore_discarded_relocs (o))
10663 action_discarded = (*bed->action_discarded) (o);
10664
10665 /* Run through the relocs evaluating complex reloc symbols and
10666 looking for relocs against symbols from discarded sections
10667 or section symbols from removed link-once sections.
10668 Complain about relocs against discarded sections. Zero
10669 relocs against removed link-once sections. */
10670
10671 rel = internal_relocs;
10672 relend = rel + o->reloc_count;
10673 for ( ; rel < relend; rel++)
10674 {
10675 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10676 unsigned int s_type;
10677 asection **ps, *sec;
10678 struct elf_link_hash_entry *h = NULL;
10679 const char *sym_name;
10680
10681 if (r_symndx == STN_UNDEF)
10682 continue;
10683
10684 if (r_symndx >= locsymcount
10685 || (elf_bad_symtab (input_bfd)
10686 && flinfo->sections[r_symndx] == NULL))
10687 {
10688 h = sym_hashes[r_symndx - extsymoff];
10689
10690 /* Badly formatted input files can contain relocs that
10691 reference non-existant symbols. Check here so that
10692 we do not seg fault. */
10693 if (h == NULL)
10694 {
10695 _bfd_error_handler
10696 /* xgettext:c-format */
10697 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10698 "that references a non-existent global symbol"),
10699 input_bfd, (uint64_t) rel->r_info, o);
10700 bfd_set_error (bfd_error_bad_value);
10701 return FALSE;
10702 }
10703
10704 while (h->root.type == bfd_link_hash_indirect
10705 || h->root.type == bfd_link_hash_warning)
10706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10707
10708 s_type = h->type;
10709
10710 /* If a plugin symbol is referenced from a non-IR file,
10711 mark the symbol as undefined. Note that the
10712 linker may attach linker created dynamic sections
10713 to the plugin bfd. Symbols defined in linker
10714 created sections are not plugin symbols. */
10715 if ((h->root.non_ir_ref_regular
10716 || h->root.non_ir_ref_dynamic)
10717 && (h->root.type == bfd_link_hash_defined
10718 || h->root.type == bfd_link_hash_defweak)
10719 && (h->root.u.def.section->flags
10720 & SEC_LINKER_CREATED) == 0
10721 && h->root.u.def.section->owner != NULL
10722 && (h->root.u.def.section->owner->flags
10723 & BFD_PLUGIN) != 0)
10724 {
10725 h->root.type = bfd_link_hash_undefined;
10726 h->root.u.undef.abfd = h->root.u.def.section->owner;
10727 }
10728
10729 ps = NULL;
10730 if (h->root.type == bfd_link_hash_defined
10731 || h->root.type == bfd_link_hash_defweak)
10732 ps = &h->root.u.def.section;
10733
10734 sym_name = h->root.root.string;
10735 }
10736 else
10737 {
10738 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10739
10740 s_type = ELF_ST_TYPE (sym->st_info);
10741 ps = &flinfo->sections[r_symndx];
10742 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10743 sym, *ps);
10744 }
10745
10746 if ((s_type == STT_RELC || s_type == STT_SRELC)
10747 && !bfd_link_relocatable (flinfo->info))
10748 {
10749 bfd_vma val;
10750 bfd_vma dot = (rel->r_offset
10751 + o->output_offset + o->output_section->vma);
10752#ifdef DEBUG
10753 printf ("Encountered a complex symbol!");
10754 printf (" (input_bfd %s, section %s, reloc %ld\n",
10755 input_bfd->filename, o->name,
10756 (long) (rel - internal_relocs));
10757 printf (" symbol: idx %8.8lx, name %s\n",
10758 r_symndx, sym_name);
10759 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10760 (unsigned long) rel->r_info,
10761 (unsigned long) rel->r_offset);
10762#endif
10763 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10764 isymbuf, locsymcount, s_type == STT_SRELC))
10765 return FALSE;
10766
10767 /* Symbol evaluated OK. Update to absolute value. */
10768 set_symbol_value (input_bfd, isymbuf, locsymcount,
10769 r_symndx, val);
10770 continue;
10771 }
10772
10773 if (action_discarded != -1 && ps != NULL)
10774 {
10775 /* Complain if the definition comes from a
10776 discarded section. */
10777 if ((sec = *ps) != NULL && discarded_section (sec))
10778 {
10779 BFD_ASSERT (r_symndx != STN_UNDEF);
10780 if (action_discarded & COMPLAIN)
10781 (*flinfo->info->callbacks->einfo)
10782 /* xgettext:c-format */
10783 (_("%X`%s' referenced in section `%pA' of %pB: "
10784 "defined in discarded section `%pA' of %pB\n"),
10785 sym_name, o, input_bfd, sec, sec->owner);
10786
10787 /* Try to do the best we can to support buggy old
10788 versions of gcc. Pretend that the symbol is
10789 really defined in the kept linkonce section.
10790 FIXME: This is quite broken. Modifying the
10791 symbol here means we will be changing all later
10792 uses of the symbol, not just in this section. */
10793 if (action_discarded & PRETEND)
10794 {
10795 asection *kept;
10796
10797 kept = _bfd_elf_check_kept_section (sec,
10798 flinfo->info);
10799 if (kept != NULL)
10800 {
10801 *ps = kept;
10802 continue;
10803 }
10804 }
10805 }
10806 }
10807 }
10808
10809 /* Relocate the section by invoking a back end routine.
10810
10811 The back end routine is responsible for adjusting the
10812 section contents as necessary, and (if using Rela relocs
10813 and generating a relocatable output file) adjusting the
10814 reloc addend as necessary.
10815
10816 The back end routine does not have to worry about setting
10817 the reloc address or the reloc symbol index.
10818
10819 The back end routine is given a pointer to the swapped in
10820 internal symbols, and can access the hash table entries
10821 for the external symbols via elf_sym_hashes (input_bfd).
10822
10823 When generating relocatable output, the back end routine
10824 must handle STB_LOCAL/STT_SECTION symbols specially. The
10825 output symbol is going to be a section symbol
10826 corresponding to the output section, which will require
10827 the addend to be adjusted. */
10828
10829 ret = (*relocate_section) (output_bfd, flinfo->info,
10830 input_bfd, o, contents,
10831 internal_relocs,
10832 isymbuf,
10833 flinfo->sections);
10834 if (!ret)
10835 return FALSE;
10836
10837 if (ret == 2
10838 || bfd_link_relocatable (flinfo->info)
10839 || flinfo->info->emitrelocations)
10840 {
10841 Elf_Internal_Rela *irela;
10842 Elf_Internal_Rela *irelaend, *irelamid;
10843 bfd_vma last_offset;
10844 struct elf_link_hash_entry **rel_hash;
10845 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10846 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10847 unsigned int next_erel;
10848 bfd_boolean rela_normal;
10849 struct bfd_elf_section_data *esdi, *esdo;
10850
10851 esdi = elf_section_data (o);
10852 esdo = elf_section_data (o->output_section);
10853 rela_normal = FALSE;
10854
10855 /* Adjust the reloc addresses and symbol indices. */
10856
10857 irela = internal_relocs;
10858 irelaend = irela + o->reloc_count;
10859 rel_hash = esdo->rel.hashes + esdo->rel.count;
10860 /* We start processing the REL relocs, if any. When we reach
10861 IRELAMID in the loop, we switch to the RELA relocs. */
10862 irelamid = irela;
10863 if (esdi->rel.hdr != NULL)
10864 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10865 * bed->s->int_rels_per_ext_rel);
10866 rel_hash_list = rel_hash;
10867 rela_hash_list = NULL;
10868 last_offset = o->output_offset;
10869 if (!bfd_link_relocatable (flinfo->info))
10870 last_offset += o->output_section->vma;
10871 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10872 {
10873 unsigned long r_symndx;
10874 asection *sec;
10875 Elf_Internal_Sym sym;
10876
10877 if (next_erel == bed->s->int_rels_per_ext_rel)
10878 {
10879 rel_hash++;
10880 next_erel = 0;
10881 }
10882
10883 if (irela == irelamid)
10884 {
10885 rel_hash = esdo->rela.hashes + esdo->rela.count;
10886 rela_hash_list = rel_hash;
10887 rela_normal = bed->rela_normal;
10888 }
10889
10890 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10891 flinfo->info, o,
10892 irela->r_offset);
10893 if (irela->r_offset >= (bfd_vma) -2)
10894 {
10895 /* This is a reloc for a deleted entry or somesuch.
10896 Turn it into an R_*_NONE reloc, at the same
10897 offset as the last reloc. elf_eh_frame.c and
10898 bfd_elf_discard_info rely on reloc offsets
10899 being ordered. */
10900 irela->r_offset = last_offset;
10901 irela->r_info = 0;
10902 irela->r_addend = 0;
10903 continue;
10904 }
10905
10906 irela->r_offset += o->output_offset;
10907
10908 /* Relocs in an executable have to be virtual addresses. */
10909 if (!bfd_link_relocatable (flinfo->info))
10910 irela->r_offset += o->output_section->vma;
10911
10912 last_offset = irela->r_offset;
10913
10914 r_symndx = irela->r_info >> r_sym_shift;
10915 if (r_symndx == STN_UNDEF)
10916 continue;
10917
10918 if (r_symndx >= locsymcount
10919 || (elf_bad_symtab (input_bfd)
10920 && flinfo->sections[r_symndx] == NULL))
10921 {
10922 struct elf_link_hash_entry *rh;
10923 unsigned long indx;
10924
10925 /* This is a reloc against a global symbol. We
10926 have not yet output all the local symbols, so
10927 we do not know the symbol index of any global
10928 symbol. We set the rel_hash entry for this
10929 reloc to point to the global hash table entry
10930 for this symbol. The symbol index is then
10931 set at the end of bfd_elf_final_link. */
10932 indx = r_symndx - extsymoff;
10933 rh = elf_sym_hashes (input_bfd)[indx];
10934 while (rh->root.type == bfd_link_hash_indirect
10935 || rh->root.type == bfd_link_hash_warning)
10936 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10937
10938 /* Setting the index to -2 tells
10939 elf_link_output_extsym that this symbol is
10940 used by a reloc. */
10941 BFD_ASSERT (rh->indx < 0);
10942 rh->indx = -2;
10943 *rel_hash = rh;
10944
10945 continue;
10946 }
10947
10948 /* This is a reloc against a local symbol. */
10949
10950 *rel_hash = NULL;
10951 sym = isymbuf[r_symndx];
10952 sec = flinfo->sections[r_symndx];
10953 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10954 {
10955 /* I suppose the backend ought to fill in the
10956 section of any STT_SECTION symbol against a
10957 processor specific section. */
10958 r_symndx = STN_UNDEF;
10959 if (bfd_is_abs_section (sec))
10960 ;
10961 else if (sec == NULL || sec->owner == NULL)
10962 {
10963 bfd_set_error (bfd_error_bad_value);
10964 return FALSE;
10965 }
10966 else
10967 {
10968 asection *osec = sec->output_section;
10969
10970 /* If we have discarded a section, the output
10971 section will be the absolute section. In
10972 case of discarded SEC_MERGE sections, use
10973 the kept section. relocate_section should
10974 have already handled discarded linkonce
10975 sections. */
10976 if (bfd_is_abs_section (osec)
10977 && sec->kept_section != NULL
10978 && sec->kept_section->output_section != NULL)
10979 {
10980 osec = sec->kept_section->output_section;
10981 irela->r_addend -= osec->vma;
10982 }
10983
10984 if (!bfd_is_abs_section (osec))
10985 {
10986 r_symndx = osec->target_index;
10987 if (r_symndx == STN_UNDEF)
10988 {
10989 irela->r_addend += osec->vma;
10990 osec = _bfd_nearby_section (output_bfd, osec,
10991 osec->vma);
10992 irela->r_addend -= osec->vma;
10993 r_symndx = osec->target_index;
10994 }
10995 }
10996 }
10997
10998 /* Adjust the addend according to where the
10999 section winds up in the output section. */
11000 if (rela_normal)
11001 irela->r_addend += sec->output_offset;
11002 }
11003 else
11004 {
11005 if (flinfo->indices[r_symndx] == -1)
11006 {
11007 unsigned long shlink;
11008 const char *name;
11009 asection *osec;
11010 long indx;
11011
11012 if (flinfo->info->strip == strip_all)
11013 {
11014 /* You can't do ld -r -s. */
11015 bfd_set_error (bfd_error_invalid_operation);
11016 return FALSE;
11017 }
11018
11019 /* This symbol was skipped earlier, but
11020 since it is needed by a reloc, we
11021 must output it now. */
11022 shlink = symtab_hdr->sh_link;
11023 name = (bfd_elf_string_from_elf_section
11024 (input_bfd, shlink, sym.st_name));
11025 if (name == NULL)
11026 return FALSE;
11027
11028 osec = sec->output_section;
11029 sym.st_shndx =
11030 _bfd_elf_section_from_bfd_section (output_bfd,
11031 osec);
11032 if (sym.st_shndx == SHN_BAD)
11033 return FALSE;
11034
11035 sym.st_value += sec->output_offset;
11036 if (!bfd_link_relocatable (flinfo->info))
11037 {
11038 sym.st_value += osec->vma;
11039 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11040 {
11041 /* STT_TLS symbols are relative to PT_TLS
11042 segment base. */
11043 BFD_ASSERT (elf_hash_table (flinfo->info)
11044 ->tls_sec != NULL);
11045 sym.st_value -= (elf_hash_table (flinfo->info)
11046 ->tls_sec->vma);
11047 }
11048 }
11049
11050 indx = bfd_get_symcount (output_bfd);
11051 ret = elf_link_output_symstrtab (flinfo, name,
11052 &sym, sec,
11053 NULL);
11054 if (ret == 0)
11055 return FALSE;
11056 else if (ret == 1)
11057 flinfo->indices[r_symndx] = indx;
11058 else
11059 abort ();
11060 }
11061
11062 r_symndx = flinfo->indices[r_symndx];
11063 }
11064
11065 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11066 | (irela->r_info & r_type_mask));
11067 }
11068
11069 /* Swap out the relocs. */
11070 input_rel_hdr = esdi->rel.hdr;
11071 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11072 {
11073 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11074 input_rel_hdr,
11075 internal_relocs,
11076 rel_hash_list))
11077 return FALSE;
11078 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11079 * bed->s->int_rels_per_ext_rel);
11080 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11081 }
11082
11083 input_rela_hdr = esdi->rela.hdr;
11084 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11085 {
11086 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11087 input_rela_hdr,
11088 internal_relocs,
11089 rela_hash_list))
11090 return FALSE;
11091 }
11092 }
11093 }
11094
11095 /* Write out the modified section contents. */
11096 if (bed->elf_backend_write_section
11097 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11098 contents))
11099 {
11100 /* Section written out. */
11101 }
11102 else switch (o->sec_info_type)
11103 {
11104 case SEC_INFO_TYPE_STABS:
11105 if (! (_bfd_write_section_stabs
11106 (output_bfd,
11107 &elf_hash_table (flinfo->info)->stab_info,
11108 o, &elf_section_data (o)->sec_info, contents)))
11109 return FALSE;
11110 break;
11111 case SEC_INFO_TYPE_MERGE:
11112 if (! _bfd_write_merged_section (output_bfd, o,
11113 elf_section_data (o)->sec_info))
11114 return FALSE;
11115 break;
11116 case SEC_INFO_TYPE_EH_FRAME:
11117 {
11118 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11119 o, contents))
11120 return FALSE;
11121 }
11122 break;
11123 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11124 {
11125 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11126 flinfo->info,
11127 o, contents))
11128 return FALSE;
11129 }
11130 break;
11131 default:
11132 {
11133 if (! (o->flags & SEC_EXCLUDE))
11134 {
11135 file_ptr offset = (file_ptr) o->output_offset;
11136 bfd_size_type todo = o->size;
11137
11138 offset *= bfd_octets_per_byte (output_bfd);
11139
11140 if ((o->flags & SEC_ELF_REVERSE_COPY))
11141 {
11142 /* Reverse-copy input section to output. */
11143 do
11144 {
11145 todo -= address_size;
11146 if (! bfd_set_section_contents (output_bfd,
11147 o->output_section,
11148 contents + todo,
11149 offset,
11150 address_size))
11151 return FALSE;
11152 if (todo == 0)
11153 break;
11154 offset += address_size;
11155 }
11156 while (1);
11157 }
11158 else if (! bfd_set_section_contents (output_bfd,
11159 o->output_section,
11160 contents,
11161 offset, todo))
11162 return FALSE;
11163 }
11164 }
11165 break;
11166 }
11167 }
11168
11169 return TRUE;
11170}
11171
11172/* Generate a reloc when linking an ELF file. This is a reloc
11173 requested by the linker, and does not come from any input file. This
11174 is used to build constructor and destructor tables when linking
11175 with -Ur. */
11176
11177static bfd_boolean
11178elf_reloc_link_order (bfd *output_bfd,
11179 struct bfd_link_info *info,
11180 asection *output_section,
11181 struct bfd_link_order *link_order)
11182{
11183 reloc_howto_type *howto;
11184 long indx;
11185 bfd_vma offset;
11186 bfd_vma addend;
11187 struct bfd_elf_section_reloc_data *reldata;
11188 struct elf_link_hash_entry **rel_hash_ptr;
11189 Elf_Internal_Shdr *rel_hdr;
11190 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11191 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11192 bfd_byte *erel;
11193 unsigned int i;
11194 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11195
11196 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11197 if (howto == NULL)
11198 {
11199 bfd_set_error (bfd_error_bad_value);
11200 return FALSE;
11201 }
11202
11203 addend = link_order->u.reloc.p->addend;
11204
11205 if (esdo->rel.hdr)
11206 reldata = &esdo->rel;
11207 else if (esdo->rela.hdr)
11208 reldata = &esdo->rela;
11209 else
11210 {
11211 reldata = NULL;
11212 BFD_ASSERT (0);
11213 }
11214
11215 /* Figure out the symbol index. */
11216 rel_hash_ptr = reldata->hashes + reldata->count;
11217 if (link_order->type == bfd_section_reloc_link_order)
11218 {
11219 indx = link_order->u.reloc.p->u.section->target_index;
11220 BFD_ASSERT (indx != 0);
11221 *rel_hash_ptr = NULL;
11222 }
11223 else
11224 {
11225 struct elf_link_hash_entry *h;
11226
11227 /* Treat a reloc against a defined symbol as though it were
11228 actually against the section. */
11229 h = ((struct elf_link_hash_entry *)
11230 bfd_wrapped_link_hash_lookup (output_bfd, info,
11231 link_order->u.reloc.p->u.name,
11232 FALSE, FALSE, TRUE));
11233 if (h != NULL
11234 && (h->root.type == bfd_link_hash_defined
11235 || h->root.type == bfd_link_hash_defweak))
11236 {
11237 asection *section;
11238
11239 section = h->root.u.def.section;
11240 indx = section->output_section->target_index;
11241 *rel_hash_ptr = NULL;
11242 /* It seems that we ought to add the symbol value to the
11243 addend here, but in practice it has already been added
11244 because it was passed to constructor_callback. */
11245 addend += section->output_section->vma + section->output_offset;
11246 }
11247 else if (h != NULL)
11248 {
11249 /* Setting the index to -2 tells elf_link_output_extsym that
11250 this symbol is used by a reloc. */
11251 h->indx = -2;
11252 *rel_hash_ptr = h;
11253 indx = 0;
11254 }
11255 else
11256 {
11257 (*info->callbacks->unattached_reloc)
11258 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11259 indx = 0;
11260 }
11261 }
11262
11263 /* If this is an inplace reloc, we must write the addend into the
11264 object file. */
11265 if (howto->partial_inplace && addend != 0)
11266 {
11267 bfd_size_type size;
11268 bfd_reloc_status_type rstat;
11269 bfd_byte *buf;
11270 bfd_boolean ok;
11271 const char *sym_name;
11272
11273 size = (bfd_size_type) bfd_get_reloc_size (howto);
11274 buf = (bfd_byte *) bfd_zmalloc (size);
11275 if (buf == NULL && size != 0)
11276 return FALSE;
11277 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11278 switch (rstat)
11279 {
11280 case bfd_reloc_ok:
11281 break;
11282
11283 default:
11284 case bfd_reloc_outofrange:
11285 abort ();
11286
11287 case bfd_reloc_overflow:
11288 if (link_order->type == bfd_section_reloc_link_order)
11289 sym_name = bfd_section_name (output_bfd,
11290 link_order->u.reloc.p->u.section);
11291 else
11292 sym_name = link_order->u.reloc.p->u.name;
11293 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11294 howto->name, addend, NULL, NULL,
11295 (bfd_vma) 0);
11296 break;
11297 }
11298
11299 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11300 link_order->offset
11301 * bfd_octets_per_byte (output_bfd),
11302 size);
11303 free (buf);
11304 if (! ok)
11305 return FALSE;
11306 }
11307
11308 /* The address of a reloc is relative to the section in a
11309 relocatable file, and is a virtual address in an executable
11310 file. */
11311 offset = link_order->offset;
11312 if (! bfd_link_relocatable (info))
11313 offset += output_section->vma;
11314
11315 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11316 {
11317 irel[i].r_offset = offset;
11318 irel[i].r_info = 0;
11319 irel[i].r_addend = 0;
11320 }
11321 if (bed->s->arch_size == 32)
11322 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11323 else
11324 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11325
11326 rel_hdr = reldata->hdr;
11327 erel = rel_hdr->contents;
11328 if (rel_hdr->sh_type == SHT_REL)
11329 {
11330 erel += reldata->count * bed->s->sizeof_rel;
11331 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11332 }
11333 else
11334 {
11335 irel[0].r_addend = addend;
11336 erel += reldata->count * bed->s->sizeof_rela;
11337 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11338 }
11339
11340 ++reldata->count;
11341
11342 return TRUE;
11343}
11344
11345
11346/* Get the output vma of the section pointed to by the sh_link field. */
11347
11348static bfd_vma
11349elf_get_linked_section_vma (struct bfd_link_order *p)
11350{
11351 Elf_Internal_Shdr **elf_shdrp;
11352 asection *s;
11353 int elfsec;
11354
11355 s = p->u.indirect.section;
11356 elf_shdrp = elf_elfsections (s->owner);
11357 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11358 elfsec = elf_shdrp[elfsec]->sh_link;
11359 /* PR 290:
11360 The Intel C compiler generates SHT_IA_64_UNWIND with
11361 SHF_LINK_ORDER. But it doesn't set the sh_link or
11362 sh_info fields. Hence we could get the situation
11363 where elfsec is 0. */
11364 if (elfsec == 0)
11365 {
11366 const struct elf_backend_data *bed
11367 = get_elf_backend_data (s->owner);
11368 if (bed->link_order_error_handler)
11369 bed->link_order_error_handler
11370 /* xgettext:c-format */
11371 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11372 return 0;
11373 }
11374 else
11375 {
11376 s = elf_shdrp[elfsec]->bfd_section;
11377 return s->output_section->vma + s->output_offset;
11378 }
11379}
11380
11381
11382/* Compare two sections based on the locations of the sections they are
11383 linked to. Used by elf_fixup_link_order. */
11384
11385static int
11386compare_link_order (const void * a, const void * b)
11387{
11388 bfd_vma apos;
11389 bfd_vma bpos;
11390
11391 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11392 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11393 if (apos < bpos)
11394 return -1;
11395 return apos > bpos;
11396}
11397
11398
11399/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11400 order as their linked sections. Returns false if this could not be done
11401 because an output section includes both ordered and unordered
11402 sections. Ideally we'd do this in the linker proper. */
11403
11404static bfd_boolean
11405elf_fixup_link_order (bfd *abfd, asection *o)
11406{
11407 int seen_linkorder;
11408 int seen_other;
11409 int n;
11410 struct bfd_link_order *p;
11411 bfd *sub;
11412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11413 unsigned elfsec;
11414 struct bfd_link_order **sections;
11415 asection *s, *other_sec, *linkorder_sec;
11416 bfd_vma offset;
11417
11418 other_sec = NULL;
11419 linkorder_sec = NULL;
11420 seen_other = 0;
11421 seen_linkorder = 0;
11422 for (p = o->map_head.link_order; p != NULL; p = p->next)
11423 {
11424 if (p->type == bfd_indirect_link_order)
11425 {
11426 s = p->u.indirect.section;
11427 sub = s->owner;
11428 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11429 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11430 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11431 && elfsec < elf_numsections (sub)
11432 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11433 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11434 {
11435 seen_linkorder++;
11436 linkorder_sec = s;
11437 }
11438 else
11439 {
11440 seen_other++;
11441 other_sec = s;
11442 }
11443 }
11444 else
11445 seen_other++;
11446
11447 if (seen_other && seen_linkorder)
11448 {
11449 if (other_sec && linkorder_sec)
11450 _bfd_error_handler
11451 /* xgettext:c-format */
11452 (_("%pA has both ordered [`%pA' in %pB] "
11453 "and unordered [`%pA' in %pB] sections"),
11454 o, linkorder_sec, linkorder_sec->owner,
11455 other_sec, other_sec->owner);
11456 else
11457 _bfd_error_handler
11458 (_("%pA has both ordered and unordered sections"), o);
11459 bfd_set_error (bfd_error_bad_value);
11460 return FALSE;
11461 }
11462 }
11463
11464 if (!seen_linkorder)
11465 return TRUE;
11466
11467 sections = (struct bfd_link_order **)
11468 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11469 if (sections == NULL)
11470 return FALSE;
11471 seen_linkorder = 0;
11472
11473 for (p = o->map_head.link_order; p != NULL; p = p->next)
11474 {
11475 sections[seen_linkorder++] = p;
11476 }
11477 /* Sort the input sections in the order of their linked section. */
11478 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11479 compare_link_order);
11480
11481 /* Change the offsets of the sections. */
11482 offset = 0;
11483 for (n = 0; n < seen_linkorder; n++)
11484 {
11485 s = sections[n]->u.indirect.section;
11486 offset &= ~(bfd_vma) 0 << s->alignment_power;
11487 s->output_offset = offset / bfd_octets_per_byte (abfd);
11488 sections[n]->offset = offset;
11489 offset += sections[n]->size;
11490 }
11491
11492 free (sections);
11493 return TRUE;
11494}
11495
11496/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11497 Returns TRUE upon success, FALSE otherwise. */
11498
11499static bfd_boolean
11500elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11501{
11502 bfd_boolean ret = FALSE;
11503 bfd *implib_bfd;
11504 const struct elf_backend_data *bed;
11505 flagword flags;
11506 enum bfd_architecture arch;
11507 unsigned int mach;
11508 asymbol **sympp = NULL;
11509 long symsize;
11510 long symcount;
11511 long src_count;
11512 elf_symbol_type *osymbuf;
11513
11514 implib_bfd = info->out_implib_bfd;
11515 bed = get_elf_backend_data (abfd);
11516
11517 if (!bfd_set_format (implib_bfd, bfd_object))
11518 return FALSE;
11519
11520 /* Use flag from executable but make it a relocatable object. */
11521 flags = bfd_get_file_flags (abfd);
11522 flags &= ~HAS_RELOC;
11523 if (!bfd_set_start_address (implib_bfd, 0)
11524 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11525 return FALSE;
11526
11527 /* Copy architecture of output file to import library file. */
11528 arch = bfd_get_arch (abfd);
11529 mach = bfd_get_mach (abfd);
11530 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11531 && (abfd->target_defaulted
11532 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11533 return FALSE;
11534
11535 /* Get symbol table size. */
11536 symsize = bfd_get_symtab_upper_bound (abfd);
11537 if (symsize < 0)
11538 return FALSE;
11539
11540 /* Read in the symbol table. */
11541 sympp = (asymbol **) xmalloc (symsize);
11542 symcount = bfd_canonicalize_symtab (abfd, sympp);
11543 if (symcount < 0)
11544 goto free_sym_buf;
11545
11546 /* Allow the BFD backend to copy any private header data it
11547 understands from the output BFD to the import library BFD. */
11548 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11549 goto free_sym_buf;
11550
11551 /* Filter symbols to appear in the import library. */
11552 if (bed->elf_backend_filter_implib_symbols)
11553 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11554 symcount);
11555 else
11556 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11557 if (symcount == 0)
11558 {
11559 bfd_set_error (bfd_error_no_symbols);
11560 _bfd_error_handler (_("%pB: no symbol found for import library"),
11561 implib_bfd);
11562 goto free_sym_buf;
11563 }
11564
11565
11566 /* Make symbols absolute. */
11567 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11568 sizeof (*osymbuf));
11569 for (src_count = 0; src_count < symcount; src_count++)
11570 {
11571 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11572 sizeof (*osymbuf));
11573 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11574 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11575 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11576 osymbuf[src_count].internal_elf_sym.st_value =
11577 osymbuf[src_count].symbol.value;
11578 sympp[src_count] = &osymbuf[src_count].symbol;
11579 }
11580
11581 bfd_set_symtab (implib_bfd, sympp, symcount);
11582
11583 /* Allow the BFD backend to copy any private data it understands
11584 from the output BFD to the import library BFD. This is done last
11585 to permit the routine to look at the filtered symbol table. */
11586 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11587 goto free_sym_buf;
11588
11589 if (!bfd_close (implib_bfd))
11590 goto free_sym_buf;
11591
11592 ret = TRUE;
11593
11594free_sym_buf:
11595 free (sympp);
11596 return ret;
11597}
11598
11599static void
11600elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11601{
11602 asection *o;
11603
11604 if (flinfo->symstrtab != NULL)
11605 _bfd_elf_strtab_free (flinfo->symstrtab);
11606 if (flinfo->contents != NULL)
11607 free (flinfo->contents);
11608 if (flinfo->external_relocs != NULL)
11609 free (flinfo->external_relocs);
11610 if (flinfo->internal_relocs != NULL)
11611 free (flinfo->internal_relocs);
11612 if (flinfo->external_syms != NULL)
11613 free (flinfo->external_syms);
11614 if (flinfo->locsym_shndx != NULL)
11615 free (flinfo->locsym_shndx);
11616 if (flinfo->internal_syms != NULL)
11617 free (flinfo->internal_syms);
11618 if (flinfo->indices != NULL)
11619 free (flinfo->indices);
11620 if (flinfo->sections != NULL)
11621 free (flinfo->sections);
11622 if (flinfo->symshndxbuf != NULL)
11623 free (flinfo->symshndxbuf);
11624 for (o = obfd->sections; o != NULL; o = o->next)
11625 {
11626 struct bfd_elf_section_data *esdo = elf_section_data (o);
11627 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11628 free (esdo->rel.hashes);
11629 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11630 free (esdo->rela.hashes);
11631 }
11632}
11633
11634/* Do the final step of an ELF link. */
11635
11636bfd_boolean
11637bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11638{
11639 bfd_boolean dynamic;
11640 bfd_boolean emit_relocs;
11641 bfd *dynobj;
11642 struct elf_final_link_info flinfo;
11643 asection *o;
11644 struct bfd_link_order *p;
11645 bfd *sub;
11646 bfd_size_type max_contents_size;
11647 bfd_size_type max_external_reloc_size;
11648 bfd_size_type max_internal_reloc_count;
11649 bfd_size_type max_sym_count;
11650 bfd_size_type max_sym_shndx_count;
11651 Elf_Internal_Sym elfsym;
11652 unsigned int i;
11653 Elf_Internal_Shdr *symtab_hdr;
11654 Elf_Internal_Shdr *symtab_shndx_hdr;
11655 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11656 struct elf_outext_info eoinfo;
11657 bfd_boolean merged;
11658 size_t relativecount = 0;
11659 asection *reldyn = 0;
11660 bfd_size_type amt;
11661 asection *attr_section = NULL;
11662 bfd_vma attr_size = 0;
11663 const char *std_attrs_section;
11664 struct elf_link_hash_table *htab = elf_hash_table (info);
11665
11666 if (!is_elf_hash_table (htab))
11667 return FALSE;
11668
11669 if (bfd_link_pic (info))
11670 abfd->flags |= DYNAMIC;
11671
11672 dynamic = htab->dynamic_sections_created;
11673 dynobj = htab->dynobj;
11674
11675 emit_relocs = (bfd_link_relocatable (info)
11676 || info->emitrelocations);
11677
11678 flinfo.info = info;
11679 flinfo.output_bfd = abfd;
11680 flinfo.symstrtab = _bfd_elf_strtab_init ();
11681 if (flinfo.symstrtab == NULL)
11682 return FALSE;
11683
11684 if (! dynamic)
11685 {
11686 flinfo.hash_sec = NULL;
11687 flinfo.symver_sec = NULL;
11688 }
11689 else
11690 {
11691 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11692 /* Note that dynsym_sec can be NULL (on VMS). */
11693 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11694 /* Note that it is OK if symver_sec is NULL. */
11695 }
11696
11697 flinfo.contents = NULL;
11698 flinfo.external_relocs = NULL;
11699 flinfo.internal_relocs = NULL;
11700 flinfo.external_syms = NULL;
11701 flinfo.locsym_shndx = NULL;
11702 flinfo.internal_syms = NULL;
11703 flinfo.indices = NULL;
11704 flinfo.sections = NULL;
11705 flinfo.symshndxbuf = NULL;
11706 flinfo.filesym_count = 0;
11707
11708 /* The object attributes have been merged. Remove the input
11709 sections from the link, and set the contents of the output
11710 secton. */
11711 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11712 for (o = abfd->sections; o != NULL; o = o->next)
11713 {
11714 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11715 || strcmp (o->name, ".gnu.attributes") == 0)
11716 {
11717 for (p = o->map_head.link_order; p != NULL; p = p->next)
11718 {
11719 asection *input_section;
11720
11721 if (p->type != bfd_indirect_link_order)
11722 continue;
11723 input_section = p->u.indirect.section;
11724 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11725 elf_link_input_bfd ignores this section. */
11726 input_section->flags &= ~SEC_HAS_CONTENTS;
11727 }
11728
11729 attr_size = bfd_elf_obj_attr_size (abfd);
11730 if (attr_size)
11731 {
11732 bfd_set_section_size (abfd, o, attr_size);
11733 attr_section = o;
11734 /* Skip this section later on. */
11735 o->map_head.link_order = NULL;
11736 }
11737 else
11738 o->flags |= SEC_EXCLUDE;
11739 }
11740 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11741 {
11742 /* Remove empty group section from linker output. */
11743 o->flags |= SEC_EXCLUDE;
11744 bfd_section_list_remove (abfd, o);
11745 abfd->section_count--;
11746 }
11747 }
11748
11749 /* Count up the number of relocations we will output for each output
11750 section, so that we know the sizes of the reloc sections. We
11751 also figure out some maximum sizes. */
11752 max_contents_size = 0;
11753 max_external_reloc_size = 0;
11754 max_internal_reloc_count = 0;
11755 max_sym_count = 0;
11756 max_sym_shndx_count = 0;
11757 merged = FALSE;
11758 for (o = abfd->sections; o != NULL; o = o->next)
11759 {
11760 struct bfd_elf_section_data *esdo = elf_section_data (o);
11761 o->reloc_count = 0;
11762
11763 for (p = o->map_head.link_order; p != NULL; p = p->next)
11764 {
11765 unsigned int reloc_count = 0;
11766 unsigned int additional_reloc_count = 0;
11767 struct bfd_elf_section_data *esdi = NULL;
11768
11769 if (p->type == bfd_section_reloc_link_order
11770 || p->type == bfd_symbol_reloc_link_order)
11771 reloc_count = 1;
11772 else if (p->type == bfd_indirect_link_order)
11773 {
11774 asection *sec;
11775
11776 sec = p->u.indirect.section;
11777
11778 /* Mark all sections which are to be included in the
11779 link. This will normally be every section. We need
11780 to do this so that we can identify any sections which
11781 the linker has decided to not include. */
11782 sec->linker_mark = TRUE;
11783
11784 if (sec->flags & SEC_MERGE)
11785 merged = TRUE;
11786
11787 if (sec->rawsize > max_contents_size)
11788 max_contents_size = sec->rawsize;
11789 if (sec->size > max_contents_size)
11790 max_contents_size = sec->size;
11791
11792 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11793 && (sec->owner->flags & DYNAMIC) == 0)
11794 {
11795 size_t sym_count;
11796
11797 /* We are interested in just local symbols, not all
11798 symbols. */
11799 if (elf_bad_symtab (sec->owner))
11800 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11801 / bed->s->sizeof_sym);
11802 else
11803 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11804
11805 if (sym_count > max_sym_count)
11806 max_sym_count = sym_count;
11807
11808 if (sym_count > max_sym_shndx_count
11809 && elf_symtab_shndx_list (sec->owner) != NULL)
11810 max_sym_shndx_count = sym_count;
11811
11812 if (esdo->this_hdr.sh_type == SHT_REL
11813 || esdo->this_hdr.sh_type == SHT_RELA)
11814 /* Some backends use reloc_count in relocation sections
11815 to count particular types of relocs. Of course,
11816 reloc sections themselves can't have relocations. */
11817 ;
11818 else if (emit_relocs)
11819 {
11820 reloc_count = sec->reloc_count;
11821 if (bed->elf_backend_count_additional_relocs)
11822 {
11823 int c;
11824 c = (*bed->elf_backend_count_additional_relocs) (sec);
11825 additional_reloc_count += c;
11826 }
11827 }
11828 else if (bed->elf_backend_count_relocs)
11829 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11830
11831 esdi = elf_section_data (sec);
11832
11833 if ((sec->flags & SEC_RELOC) != 0)
11834 {
11835 size_t ext_size = 0;
11836
11837 if (esdi->rel.hdr != NULL)
11838 ext_size = esdi->rel.hdr->sh_size;
11839 if (esdi->rela.hdr != NULL)
11840 ext_size += esdi->rela.hdr->sh_size;
11841
11842 if (ext_size > max_external_reloc_size)
11843 max_external_reloc_size = ext_size;
11844 if (sec->reloc_count > max_internal_reloc_count)
11845 max_internal_reloc_count = sec->reloc_count;
11846 }
11847 }
11848 }
11849
11850 if (reloc_count == 0)
11851 continue;
11852
11853 reloc_count += additional_reloc_count;
11854 o->reloc_count += reloc_count;
11855
11856 if (p->type == bfd_indirect_link_order && emit_relocs)
11857 {
11858 if (esdi->rel.hdr)
11859 {
11860 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11861 esdo->rel.count += additional_reloc_count;
11862 }
11863 if (esdi->rela.hdr)
11864 {
11865 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11866 esdo->rela.count += additional_reloc_count;
11867 }
11868 }
11869 else
11870 {
11871 if (o->use_rela_p)
11872 esdo->rela.count += reloc_count;
11873 else
11874 esdo->rel.count += reloc_count;
11875 }
11876 }
11877
11878 if (o->reloc_count > 0)
11879 o->flags |= SEC_RELOC;
11880 else
11881 {
11882 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11883 set it (this is probably a bug) and if it is set
11884 assign_section_numbers will create a reloc section. */
11885 o->flags &=~ SEC_RELOC;
11886 }
11887
11888 /* If the SEC_ALLOC flag is not set, force the section VMA to
11889 zero. This is done in elf_fake_sections as well, but forcing
11890 the VMA to 0 here will ensure that relocs against these
11891 sections are handled correctly. */
11892 if ((o->flags & SEC_ALLOC) == 0
11893 && ! o->user_set_vma)
11894 o->vma = 0;
11895 }
11896
11897 if (! bfd_link_relocatable (info) && merged)
11898 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11899
11900 /* Figure out the file positions for everything but the symbol table
11901 and the relocs. We set symcount to force assign_section_numbers
11902 to create a symbol table. */
11903 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11904 BFD_ASSERT (! abfd->output_has_begun);
11905 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11906 goto error_return;
11907
11908 /* Set sizes, and assign file positions for reloc sections. */
11909 for (o = abfd->sections; o != NULL; o = o->next)
11910 {
11911 struct bfd_elf_section_data *esdo = elf_section_data (o);
11912 if ((o->flags & SEC_RELOC) != 0)
11913 {
11914 if (esdo->rel.hdr
11915 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11916 goto error_return;
11917
11918 if (esdo->rela.hdr
11919 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11920 goto error_return;
11921 }
11922
11923 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11924 to count upwards while actually outputting the relocations. */
11925 esdo->rel.count = 0;
11926 esdo->rela.count = 0;
11927
11928 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11929 {
11930 /* Cache the section contents so that they can be compressed
11931 later. Use bfd_malloc since it will be freed by
11932 bfd_compress_section_contents. */
11933 unsigned char *contents = esdo->this_hdr.contents;
11934 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11935 abort ();
11936 contents
11937 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11938 if (contents == NULL)
11939 goto error_return;
11940 esdo->this_hdr.contents = contents;
11941 }
11942 }
11943
11944 /* We have now assigned file positions for all the sections except
11945 .symtab, .strtab, and non-loaded reloc sections. We start the
11946 .symtab section at the current file position, and write directly
11947 to it. We build the .strtab section in memory. */
11948 bfd_get_symcount (abfd) = 0;
11949 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11950 /* sh_name is set in prep_headers. */
11951 symtab_hdr->sh_type = SHT_SYMTAB;
11952 /* sh_flags, sh_addr and sh_size all start off zero. */
11953 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11954 /* sh_link is set in assign_section_numbers. */
11955 /* sh_info is set below. */
11956 /* sh_offset is set just below. */
11957 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11958
11959 if (max_sym_count < 20)
11960 max_sym_count = 20;
11961 htab->strtabsize = max_sym_count;
11962 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11963 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11964 if (htab->strtab == NULL)
11965 goto error_return;
11966 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11967 flinfo.symshndxbuf
11968 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11969 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11970
11971 if (info->strip != strip_all || emit_relocs)
11972 {
11973 file_ptr off = elf_next_file_pos (abfd);
11974
11975 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11976
11977 /* Note that at this point elf_next_file_pos (abfd) is
11978 incorrect. We do not yet know the size of the .symtab section.
11979 We correct next_file_pos below, after we do know the size. */
11980
11981 /* Start writing out the symbol table. The first symbol is always a
11982 dummy symbol. */
11983 elfsym.st_value = 0;
11984 elfsym.st_size = 0;
11985 elfsym.st_info = 0;
11986 elfsym.st_other = 0;
11987 elfsym.st_shndx = SHN_UNDEF;
11988 elfsym.st_target_internal = 0;
11989 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11990 bfd_und_section_ptr, NULL) != 1)
11991 goto error_return;
11992
11993 /* Output a symbol for each section. We output these even if we are
11994 discarding local symbols, since they are used for relocs. These
11995 symbols have no names. We store the index of each one in the
11996 index field of the section, so that we can find it again when
11997 outputting relocs. */
11998
11999 elfsym.st_size = 0;
12000 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12001 elfsym.st_other = 0;
12002 elfsym.st_value = 0;
12003 elfsym.st_target_internal = 0;
12004 for (i = 1; i < elf_numsections (abfd); i++)
12005 {
12006 o = bfd_section_from_elf_index (abfd, i);
12007 if (o != NULL)
12008 {
12009 o->target_index = bfd_get_symcount (abfd);
12010 elfsym.st_shndx = i;
12011 if (!bfd_link_relocatable (info))
12012 elfsym.st_value = o->vma;
12013 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12014 NULL) != 1)
12015 goto error_return;
12016 }
12017 }
12018 }
12019
12020 /* Allocate some memory to hold information read in from the input
12021 files. */
12022 if (max_contents_size != 0)
12023 {
12024 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12025 if (flinfo.contents == NULL)
12026 goto error_return;
12027 }
12028
12029 if (max_external_reloc_size != 0)
12030 {
12031 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12032 if (flinfo.external_relocs == NULL)
12033 goto error_return;
12034 }
12035
12036 if (max_internal_reloc_count != 0)
12037 {
12038 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12039 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12040 if (flinfo.internal_relocs == NULL)
12041 goto error_return;
12042 }
12043
12044 if (max_sym_count != 0)
12045 {
12046 amt = max_sym_count * bed->s->sizeof_sym;
12047 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12048 if (flinfo.external_syms == NULL)
12049 goto error_return;
12050
12051 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12052 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12053 if (flinfo.internal_syms == NULL)
12054 goto error_return;
12055
12056 amt = max_sym_count * sizeof (long);
12057 flinfo.indices = (long int *) bfd_malloc (amt);
12058 if (flinfo.indices == NULL)
12059 goto error_return;
12060
12061 amt = max_sym_count * sizeof (asection *);
12062 flinfo.sections = (asection **) bfd_malloc (amt);
12063 if (flinfo.sections == NULL)
12064 goto error_return;
12065 }
12066
12067 if (max_sym_shndx_count != 0)
12068 {
12069 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12070 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12071 if (flinfo.locsym_shndx == NULL)
12072 goto error_return;
12073 }
12074
12075 if (htab->tls_sec)
12076 {
12077 bfd_vma base, end = 0;
12078 asection *sec;
12079
12080 for (sec = htab->tls_sec;
12081 sec && (sec->flags & SEC_THREAD_LOCAL);
12082 sec = sec->next)
12083 {
12084 bfd_size_type size = sec->size;
12085
12086 if (size == 0
12087 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12088 {
12089 struct bfd_link_order *ord = sec->map_tail.link_order;
12090
12091 if (ord != NULL)
12092 size = ord->offset + ord->size;
12093 }
12094 end = sec->vma + size;
12095 }
12096 base = htab->tls_sec->vma;
12097 /* Only align end of TLS section if static TLS doesn't have special
12098 alignment requirements. */
12099 if (bed->static_tls_alignment == 1)
12100 end = align_power (end, htab->tls_sec->alignment_power);
12101 htab->tls_size = end - base;
12102 }
12103
12104 /* Reorder SHF_LINK_ORDER sections. */
12105 for (o = abfd->sections; o != NULL; o = o->next)
12106 {
12107 if (!elf_fixup_link_order (abfd, o))
12108 return FALSE;
12109 }
12110
12111 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12112 return FALSE;
12113
12114 /* Since ELF permits relocations to be against local symbols, we
12115 must have the local symbols available when we do the relocations.
12116 Since we would rather only read the local symbols once, and we
12117 would rather not keep them in memory, we handle all the
12118 relocations for a single input file at the same time.
12119
12120 Unfortunately, there is no way to know the total number of local
12121 symbols until we have seen all of them, and the local symbol
12122 indices precede the global symbol indices. This means that when
12123 we are generating relocatable output, and we see a reloc against
12124 a global symbol, we can not know the symbol index until we have
12125 finished examining all the local symbols to see which ones we are
12126 going to output. To deal with this, we keep the relocations in
12127 memory, and don't output them until the end of the link. This is
12128 an unfortunate waste of memory, but I don't see a good way around
12129 it. Fortunately, it only happens when performing a relocatable
12130 link, which is not the common case. FIXME: If keep_memory is set
12131 we could write the relocs out and then read them again; I don't
12132 know how bad the memory loss will be. */
12133
12134 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12135 sub->output_has_begun = FALSE;
12136 for (o = abfd->sections; o != NULL; o = o->next)
12137 {
12138 for (p = o->map_head.link_order; p != NULL; p = p->next)
12139 {
12140 if (p->type == bfd_indirect_link_order
12141 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12142 == bfd_target_elf_flavour)
12143 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12144 {
12145 if (! sub->output_has_begun)
12146 {
12147 if (! elf_link_input_bfd (&flinfo, sub))
12148 goto error_return;
12149 sub->output_has_begun = TRUE;
12150 }
12151 }
12152 else if (p->type == bfd_section_reloc_link_order
12153 || p->type == bfd_symbol_reloc_link_order)
12154 {
12155 if (! elf_reloc_link_order (abfd, info, o, p))
12156 goto error_return;
12157 }
12158 else
12159 {
12160 if (! _bfd_default_link_order (abfd, info, o, p))
12161 {
12162 if (p->type == bfd_indirect_link_order
12163 && (bfd_get_flavour (sub)
12164 == bfd_target_elf_flavour)
12165 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12166 != bed->s->elfclass))
12167 {
12168 const char *iclass, *oclass;
12169
12170 switch (bed->s->elfclass)
12171 {
12172 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12173 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12174 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12175 default: abort ();
12176 }
12177
12178 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12179 {
12180 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12181 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12182 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12183 default: abort ();
12184 }
12185
12186 bfd_set_error (bfd_error_wrong_format);
12187 _bfd_error_handler
12188 /* xgettext:c-format */
12189 (_("%pB: file class %s incompatible with %s"),
12190 sub, iclass, oclass);
12191 }
12192
12193 goto error_return;
12194 }
12195 }
12196 }
12197 }
12198
12199 /* Free symbol buffer if needed. */
12200 if (!info->reduce_memory_overheads)
12201 {
12202 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12203 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12204 && elf_tdata (sub)->symbuf)
12205 {
12206 free (elf_tdata (sub)->symbuf);
12207 elf_tdata (sub)->symbuf = NULL;
12208 }
12209 }
12210
12211 /* Output any global symbols that got converted to local in a
12212 version script or due to symbol visibility. We do this in a
12213 separate step since ELF requires all local symbols to appear
12214 prior to any global symbols. FIXME: We should only do this if
12215 some global symbols were, in fact, converted to become local.
12216 FIXME: Will this work correctly with the Irix 5 linker? */
12217 eoinfo.failed = FALSE;
12218 eoinfo.flinfo = &flinfo;
12219 eoinfo.localsyms = TRUE;
12220 eoinfo.file_sym_done = FALSE;
12221 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12222 if (eoinfo.failed)
12223 return FALSE;
12224
12225 /* If backend needs to output some local symbols not present in the hash
12226 table, do it now. */
12227 if (bed->elf_backend_output_arch_local_syms
12228 && (info->strip != strip_all || emit_relocs))
12229 {
12230 typedef int (*out_sym_func)
12231 (void *, const char *, Elf_Internal_Sym *, asection *,
12232 struct elf_link_hash_entry *);
12233
12234 if (! ((*bed->elf_backend_output_arch_local_syms)
12235 (abfd, info, &flinfo,
12236 (out_sym_func) elf_link_output_symstrtab)))
12237 return FALSE;
12238 }
12239
12240 /* That wrote out all the local symbols. Finish up the symbol table
12241 with the global symbols. Even if we want to strip everything we
12242 can, we still need to deal with those global symbols that got
12243 converted to local in a version script. */
12244
12245 /* The sh_info field records the index of the first non local symbol. */
12246 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12247
12248 if (dynamic
12249 && htab->dynsym != NULL
12250 && htab->dynsym->output_section != bfd_abs_section_ptr)
12251 {
12252 Elf_Internal_Sym sym;
12253 bfd_byte *dynsym = htab->dynsym->contents;
12254
12255 o = htab->dynsym->output_section;
12256 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12257
12258 /* Write out the section symbols for the output sections. */
12259 if (bfd_link_pic (info)
12260 || htab->is_relocatable_executable)
12261 {
12262 asection *s;
12263
12264 sym.st_size = 0;
12265 sym.st_name = 0;
12266 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12267 sym.st_other = 0;
12268 sym.st_target_internal = 0;
12269
12270 for (s = abfd->sections; s != NULL; s = s->next)
12271 {
12272 int indx;
12273 bfd_byte *dest;
12274 long dynindx;
12275
12276 dynindx = elf_section_data (s)->dynindx;
12277 if (dynindx <= 0)
12278 continue;
12279 indx = elf_section_data (s)->this_idx;
12280 BFD_ASSERT (indx > 0);
12281 sym.st_shndx = indx;
12282 if (! check_dynsym (abfd, &sym))
12283 return FALSE;
12284 sym.st_value = s->vma;
12285 dest = dynsym + dynindx * bed->s->sizeof_sym;
12286 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12287 }
12288 }
12289
12290 /* Write out the local dynsyms. */
12291 if (htab->dynlocal)
12292 {
12293 struct elf_link_local_dynamic_entry *e;
12294 for (e = htab->dynlocal; e ; e = e->next)
12295 {
12296 asection *s;
12297 bfd_byte *dest;
12298
12299 /* Copy the internal symbol and turn off visibility.
12300 Note that we saved a word of storage and overwrote
12301 the original st_name with the dynstr_index. */
12302 sym = e->isym;
12303 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12304
12305 s = bfd_section_from_elf_index (e->input_bfd,
12306 e->isym.st_shndx);
12307 if (s != NULL)
12308 {
12309 sym.st_shndx =
12310 elf_section_data (s->output_section)->this_idx;
12311 if (! check_dynsym (abfd, &sym))
12312 return FALSE;
12313 sym.st_value = (s->output_section->vma
12314 + s->output_offset
12315 + e->isym.st_value);
12316 }
12317
12318 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12319 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12320 }
12321 }
12322 }
12323
12324 /* We get the global symbols from the hash table. */
12325 eoinfo.failed = FALSE;
12326 eoinfo.localsyms = FALSE;
12327 eoinfo.flinfo = &flinfo;
12328 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12329 if (eoinfo.failed)
12330 return FALSE;
12331
12332 /* If backend needs to output some symbols not present in the hash
12333 table, do it now. */
12334 if (bed->elf_backend_output_arch_syms
12335 && (info->strip != strip_all || emit_relocs))
12336 {
12337 typedef int (*out_sym_func)
12338 (void *, const char *, Elf_Internal_Sym *, asection *,
12339 struct elf_link_hash_entry *);
12340
12341 if (! ((*bed->elf_backend_output_arch_syms)
12342 (abfd, info, &flinfo,
12343 (out_sym_func) elf_link_output_symstrtab)))
12344 return FALSE;
12345 }
12346
12347 /* Finalize the .strtab section. */
12348 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12349
12350 /* Swap out the .strtab section. */
12351 if (!elf_link_swap_symbols_out (&flinfo))
12352 return FALSE;
12353
12354 /* Now we know the size of the symtab section. */
12355 if (bfd_get_symcount (abfd) > 0)
12356 {
12357 /* Finish up and write out the symbol string table (.strtab)
12358 section. */
12359 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12360 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12361
12362 if (elf_symtab_shndx_list (abfd))
12363 {
12364 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12365
12366 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12367 {
12368 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12369 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12370 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12371 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12372 symtab_shndx_hdr->sh_size = amt;
12373
12374 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12375 off, TRUE);
12376
12377 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12378 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12379 return FALSE;
12380 }
12381 }
12382
12383 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12384 /* sh_name was set in prep_headers. */
12385 symstrtab_hdr->sh_type = SHT_STRTAB;
12386 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12387 symstrtab_hdr->sh_addr = 0;
12388 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12389 symstrtab_hdr->sh_entsize = 0;
12390 symstrtab_hdr->sh_link = 0;
12391 symstrtab_hdr->sh_info = 0;
12392 /* sh_offset is set just below. */
12393 symstrtab_hdr->sh_addralign = 1;
12394
12395 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12396 off, TRUE);
12397 elf_next_file_pos (abfd) = off;
12398
12399 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12400 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12401 return FALSE;
12402 }
12403
12404 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12405 {
12406 _bfd_error_handler (_("%pB: failed to generate import library"),
12407 info->out_implib_bfd);
12408 return FALSE;
12409 }
12410
12411 /* Adjust the relocs to have the correct symbol indices. */
12412 for (o = abfd->sections; o != NULL; o = o->next)
12413 {
12414 struct bfd_elf_section_data *esdo = elf_section_data (o);
12415 bfd_boolean sort;
12416
12417 if ((o->flags & SEC_RELOC) == 0)
12418 continue;
12419
12420 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12421 if (esdo->rel.hdr != NULL
12422 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12423 return FALSE;
12424 if (esdo->rela.hdr != NULL
12425 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12426 return FALSE;
12427
12428 /* Set the reloc_count field to 0 to prevent write_relocs from
12429 trying to swap the relocs out itself. */
12430 o->reloc_count = 0;
12431 }
12432
12433 if (dynamic && info->combreloc && dynobj != NULL)
12434 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12435
12436 /* If we are linking against a dynamic object, or generating a
12437 shared library, finish up the dynamic linking information. */
12438 if (dynamic)
12439 {
12440 bfd_byte *dyncon, *dynconend;
12441
12442 /* Fix up .dynamic entries. */
12443 o = bfd_get_linker_section (dynobj, ".dynamic");
12444 BFD_ASSERT (o != NULL);
12445
12446 dyncon = o->contents;
12447 dynconend = o->contents + o->size;
12448 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12449 {
12450 Elf_Internal_Dyn dyn;
12451 const char *name;
12452 unsigned int type;
12453 bfd_size_type sh_size;
12454 bfd_vma sh_addr;
12455
12456 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12457
12458 switch (dyn.d_tag)
12459 {
12460 default:
12461 continue;
12462 case DT_NULL:
12463 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12464 {
12465 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12466 {
12467 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12468 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12469 default: continue;
12470 }
12471 dyn.d_un.d_val = relativecount;
12472 relativecount = 0;
12473 break;
12474 }
12475 continue;
12476
12477 case DT_INIT:
12478 name = info->init_function;
12479 goto get_sym;
12480 case DT_FINI:
12481 name = info->fini_function;
12482 get_sym:
12483 {
12484 struct elf_link_hash_entry *h;
12485
12486 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12487 if (h != NULL
12488 && (h->root.type == bfd_link_hash_defined
12489 || h->root.type == bfd_link_hash_defweak))
12490 {
12491 dyn.d_un.d_ptr = h->root.u.def.value;
12492 o = h->root.u.def.section;
12493 if (o->output_section != NULL)
12494 dyn.d_un.d_ptr += (o->output_section->vma
12495 + o->output_offset);
12496 else
12497 {
12498 /* The symbol is imported from another shared
12499 library and does not apply to this one. */
12500 dyn.d_un.d_ptr = 0;
12501 }
12502 break;
12503 }
12504 }
12505 continue;
12506
12507 case DT_PREINIT_ARRAYSZ:
12508 name = ".preinit_array";
12509 goto get_out_size;
12510 case DT_INIT_ARRAYSZ:
12511 name = ".init_array";
12512 goto get_out_size;
12513 case DT_FINI_ARRAYSZ:
12514 name = ".fini_array";
12515 get_out_size:
12516 o = bfd_get_section_by_name (abfd, name);
12517 if (o == NULL)
12518 {
12519 _bfd_error_handler
12520 (_("could not find section %s"), name);
12521 goto error_return;
12522 }
12523 if (o->size == 0)
12524 _bfd_error_handler
12525 (_("warning: %s section has zero size"), name);
12526 dyn.d_un.d_val = o->size;
12527 break;
12528
12529 case DT_PREINIT_ARRAY:
12530 name = ".preinit_array";
12531 goto get_out_vma;
12532 case DT_INIT_ARRAY:
12533 name = ".init_array";
12534 goto get_out_vma;
12535 case DT_FINI_ARRAY:
12536 name = ".fini_array";
12537 get_out_vma:
12538 o = bfd_get_section_by_name (abfd, name);
12539 goto do_vma;
12540
12541 case DT_HASH:
12542 name = ".hash";
12543 goto get_vma;
12544 case DT_GNU_HASH:
12545 name = ".gnu.hash";
12546 goto get_vma;
12547 case DT_STRTAB:
12548 name = ".dynstr";
12549 goto get_vma;
12550 case DT_SYMTAB:
12551 name = ".dynsym";
12552 goto get_vma;
12553 case DT_VERDEF:
12554 name = ".gnu.version_d";
12555 goto get_vma;
12556 case DT_VERNEED:
12557 name = ".gnu.version_r";
12558 goto get_vma;
12559 case DT_VERSYM:
12560 name = ".gnu.version";
12561 get_vma:
12562 o = bfd_get_linker_section (dynobj, name);
12563 do_vma:
12564 if (o == NULL || bfd_is_abs_section (o->output_section))
12565 {
12566 _bfd_error_handler
12567 (_("could not find section %s"), name);
12568 goto error_return;
12569 }
12570 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12571 {
12572 _bfd_error_handler
12573 (_("warning: section '%s' is being made into a note"), name);
12574 bfd_set_error (bfd_error_nonrepresentable_section);
12575 goto error_return;
12576 }
12577 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12578 break;
12579
12580 case DT_REL:
12581 case DT_RELA:
12582 case DT_RELSZ:
12583 case DT_RELASZ:
12584 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12585 type = SHT_REL;
12586 else
12587 type = SHT_RELA;
12588 sh_size = 0;
12589 sh_addr = 0;
12590 for (i = 1; i < elf_numsections (abfd); i++)
12591 {
12592 Elf_Internal_Shdr *hdr;
12593
12594 hdr = elf_elfsections (abfd)[i];
12595 if (hdr->sh_type == type
12596 && (hdr->sh_flags & SHF_ALLOC) != 0)
12597 {
12598 sh_size += hdr->sh_size;
12599 if (sh_addr == 0
12600 || sh_addr > hdr->sh_addr)
12601 sh_addr = hdr->sh_addr;
12602 }
12603 }
12604
12605 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12606 {
12607 /* Don't count procedure linkage table relocs in the
12608 overall reloc count. */
12609 sh_size -= htab->srelplt->size;
12610 if (sh_size == 0)
12611 /* If the size is zero, make the address zero too.
12612 This is to avoid a glibc bug. If the backend
12613 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12614 zero, then we'll put DT_RELA at the end of
12615 DT_JMPREL. glibc will interpret the end of
12616 DT_RELA matching the end of DT_JMPREL as the
12617 case where DT_RELA includes DT_JMPREL, and for
12618 LD_BIND_NOW will decide that processing DT_RELA
12619 will process the PLT relocs too. Net result:
12620 No PLT relocs applied. */
12621 sh_addr = 0;
12622
12623 /* If .rela.plt is the first .rela section, exclude
12624 it from DT_RELA. */
12625 else if (sh_addr == (htab->srelplt->output_section->vma
12626 + htab->srelplt->output_offset))
12627 sh_addr += htab->srelplt->size;
12628 }
12629
12630 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12631 dyn.d_un.d_val = sh_size;
12632 else
12633 dyn.d_un.d_ptr = sh_addr;
12634 break;
12635 }
12636 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12637 }
12638 }
12639
12640 /* If we have created any dynamic sections, then output them. */
12641 if (dynobj != NULL)
12642 {
12643 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12644 goto error_return;
12645
12646 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12647 if (((info->warn_shared_textrel && bfd_link_pic (info))
12648 || info->error_textrel)
12649 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12650 {
12651 bfd_byte *dyncon, *dynconend;
12652
12653 dyncon = o->contents;
12654 dynconend = o->contents + o->size;
12655 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12656 {
12657 Elf_Internal_Dyn dyn;
12658
12659 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12660
12661 if (dyn.d_tag == DT_TEXTREL)
12662 {
12663 if (info->error_textrel)
12664 info->callbacks->einfo
12665 (_("%P%X: read-only segment has dynamic relocations\n"));
12666 else
12667 info->callbacks->einfo
12668 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12669 break;
12670 }
12671 }
12672 }
12673
12674 for (o = dynobj->sections; o != NULL; o = o->next)
12675 {
12676 if ((o->flags & SEC_HAS_CONTENTS) == 0
12677 || o->size == 0
12678 || o->output_section == bfd_abs_section_ptr)
12679 continue;
12680 if ((o->flags & SEC_LINKER_CREATED) == 0)
12681 {
12682 /* At this point, we are only interested in sections
12683 created by _bfd_elf_link_create_dynamic_sections. */
12684 continue;
12685 }
12686 if (htab->stab_info.stabstr == o)
12687 continue;
12688 if (htab->eh_info.hdr_sec == o)
12689 continue;
12690 if (strcmp (o->name, ".dynstr") != 0)
12691 {
12692 if (! bfd_set_section_contents (abfd, o->output_section,
12693 o->contents,
12694 (file_ptr) o->output_offset
12695 * bfd_octets_per_byte (abfd),
12696 o->size))
12697 goto error_return;
12698 }
12699 else
12700 {
12701 /* The contents of the .dynstr section are actually in a
12702 stringtab. */
12703 file_ptr off;
12704
12705 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12706 if (bfd_seek (abfd, off, SEEK_SET) != 0
12707 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12708 goto error_return;
12709 }
12710 }
12711 }
12712
12713 if (!info->resolve_section_groups)
12714 {
12715 bfd_boolean failed = FALSE;
12716
12717 BFD_ASSERT (bfd_link_relocatable (info));
12718 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12719 if (failed)
12720 goto error_return;
12721 }
12722
12723 /* If we have optimized stabs strings, output them. */
12724 if (htab->stab_info.stabstr != NULL)
12725 {
12726 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12727 goto error_return;
12728 }
12729
12730 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12731 goto error_return;
12732
12733 elf_final_link_free (abfd, &flinfo);
12734
12735 elf_linker (abfd) = TRUE;
12736
12737 if (attr_section)
12738 {
12739 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12740 if (contents == NULL)
12741 return FALSE; /* Bail out and fail. */
12742 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12743 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12744 free (contents);
12745 }
12746
12747 return TRUE;
12748
12749 error_return:
12750 elf_final_link_free (abfd, &flinfo);
12751 return FALSE;
12752}
12753\f
12754/* Initialize COOKIE for input bfd ABFD. */
12755
12756static bfd_boolean
12757init_reloc_cookie (struct elf_reloc_cookie *cookie,
12758 struct bfd_link_info *info, bfd *abfd)
12759{
12760 Elf_Internal_Shdr *symtab_hdr;
12761 const struct elf_backend_data *bed;
12762
12763 bed = get_elf_backend_data (abfd);
12764 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12765
12766 cookie->abfd = abfd;
12767 cookie->sym_hashes = elf_sym_hashes (abfd);
12768 cookie->bad_symtab = elf_bad_symtab (abfd);
12769 if (cookie->bad_symtab)
12770 {
12771 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12772 cookie->extsymoff = 0;
12773 }
12774 else
12775 {
12776 cookie->locsymcount = symtab_hdr->sh_info;
12777 cookie->extsymoff = symtab_hdr->sh_info;
12778 }
12779
12780 if (bed->s->arch_size == 32)
12781 cookie->r_sym_shift = 8;
12782 else
12783 cookie->r_sym_shift = 32;
12784
12785 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12786 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12787 {
12788 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12789 cookie->locsymcount, 0,
12790 NULL, NULL, NULL);
12791 if (cookie->locsyms == NULL)
12792 {
12793 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12794 return FALSE;
12795 }
12796 if (info->keep_memory)
12797 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12798 }
12799 return TRUE;
12800}
12801
12802/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12803
12804static void
12805fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12806{
12807 Elf_Internal_Shdr *symtab_hdr;
12808
12809 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12810 if (cookie->locsyms != NULL
12811 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12812 free (cookie->locsyms);
12813}
12814
12815/* Initialize the relocation information in COOKIE for input section SEC
12816 of input bfd ABFD. */
12817
12818static bfd_boolean
12819init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12820 struct bfd_link_info *info, bfd *abfd,
12821 asection *sec)
12822{
12823 if (sec->reloc_count == 0)
12824 {
12825 cookie->rels = NULL;
12826 cookie->relend = NULL;
12827 }
12828 else
12829 {
12830 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12831 info->keep_memory);
12832 if (cookie->rels == NULL)
12833 return FALSE;
12834 cookie->rel = cookie->rels;
12835 cookie->relend = cookie->rels + sec->reloc_count;
12836 }
12837 cookie->rel = cookie->rels;
12838 return TRUE;
12839}
12840
12841/* Free the memory allocated by init_reloc_cookie_rels,
12842 if appropriate. */
12843
12844static void
12845fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12846 asection *sec)
12847{
12848 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12849 free (cookie->rels);
12850}
12851
12852/* Initialize the whole of COOKIE for input section SEC. */
12853
12854static bfd_boolean
12855init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12856 struct bfd_link_info *info,
12857 asection *sec)
12858{
12859 if (!init_reloc_cookie (cookie, info, sec->owner))
12860 goto error1;
12861 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12862 goto error2;
12863 return TRUE;
12864
12865 error2:
12866 fini_reloc_cookie (cookie, sec->owner);
12867 error1:
12868 return FALSE;
12869}
12870
12871/* Free the memory allocated by init_reloc_cookie_for_section,
12872 if appropriate. */
12873
12874static void
12875fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12876 asection *sec)
12877{
12878 fini_reloc_cookie_rels (cookie, sec);
12879 fini_reloc_cookie (cookie, sec->owner);
12880}
12881\f
12882/* Garbage collect unused sections. */
12883
12884/* Default gc_mark_hook. */
12885
12886asection *
12887_bfd_elf_gc_mark_hook (asection *sec,
12888 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12889 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12890 struct elf_link_hash_entry *h,
12891 Elf_Internal_Sym *sym)
12892{
12893 if (h != NULL)
12894 {
12895 switch (h->root.type)
12896 {
12897 case bfd_link_hash_defined:
12898 case bfd_link_hash_defweak:
12899 return h->root.u.def.section;
12900
12901 case bfd_link_hash_common:
12902 return h->root.u.c.p->section;
12903
12904 default:
12905 break;
12906 }
12907 }
12908 else
12909 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12910
12911 return NULL;
12912}
12913
12914/* Return the debug definition section. */
12915
12916static asection *
12917elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12919 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12920 struct elf_link_hash_entry *h,
12921 Elf_Internal_Sym *sym)
12922{
12923 if (h != NULL)
12924 {
12925 /* Return the global debug definition section. */
12926 if ((h->root.type == bfd_link_hash_defined
12927 || h->root.type == bfd_link_hash_defweak)
12928 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12929 return h->root.u.def.section;
12930 }
12931 else
12932 {
12933 /* Return the local debug definition section. */
12934 asection *isec = bfd_section_from_elf_index (sec->owner,
12935 sym->st_shndx);
12936 if ((isec->flags & SEC_DEBUGGING) != 0)
12937 return isec;
12938 }
12939
12940 return NULL;
12941}
12942
12943/* COOKIE->rel describes a relocation against section SEC, which is
12944 a section we've decided to keep. Return the section that contains
12945 the relocation symbol, or NULL if no section contains it. */
12946
12947asection *
12948_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12949 elf_gc_mark_hook_fn gc_mark_hook,
12950 struct elf_reloc_cookie *cookie,
12951 bfd_boolean *start_stop)
12952{
12953 unsigned long r_symndx;
12954 struct elf_link_hash_entry *h;
12955
12956 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12957 if (r_symndx == STN_UNDEF)
12958 return NULL;
12959
12960 if (r_symndx >= cookie->locsymcount
12961 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12962 {
12963 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12964 if (h == NULL)
12965 {
12966 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12967 sec->owner);
12968 return NULL;
12969 }
12970 while (h->root.type == bfd_link_hash_indirect
12971 || h->root.type == bfd_link_hash_warning)
12972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12973 h->mark = 1;
12974 /* If this symbol is weak and there is a non-weak definition, we
12975 keep the non-weak definition because many backends put
12976 dynamic reloc info on the non-weak definition for code
12977 handling copy relocs. */
12978 if (h->is_weakalias)
12979 weakdef (h)->mark = 1;
12980
12981 if (start_stop != NULL)
12982 {
12983 /* To work around a glibc bug, mark XXX input sections
12984 when there is a reference to __start_XXX or __stop_XXX
12985 symbols. */
12986 if (h->start_stop)
12987 {
12988 asection *s = h->u2.start_stop_section;
12989 *start_stop = !s->gc_mark;
12990 return s;
12991 }
12992 }
12993
12994 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12995 }
12996
12997 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12998 &cookie->locsyms[r_symndx]);
12999}
13000
13001/* COOKIE->rel describes a relocation against section SEC, which is
13002 a section we've decided to keep. Mark the section that contains
13003 the relocation symbol. */
13004
13005bfd_boolean
13006_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13007 asection *sec,
13008 elf_gc_mark_hook_fn gc_mark_hook,
13009 struct elf_reloc_cookie *cookie)
13010{
13011 asection *rsec;
13012 bfd_boolean start_stop = FALSE;
13013
13014 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13015 while (rsec != NULL)
13016 {
13017 if (!rsec->gc_mark)
13018 {
13019 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13020 || (rsec->owner->flags & DYNAMIC) != 0)
13021 rsec->gc_mark = 1;
13022 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13023 return FALSE;
13024 }
13025 if (!start_stop)
13026 break;
13027 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13028 }
13029 return TRUE;
13030}
13031
13032/* The mark phase of garbage collection. For a given section, mark
13033 it and any sections in this section's group, and all the sections
13034 which define symbols to which it refers. */
13035
13036bfd_boolean
13037_bfd_elf_gc_mark (struct bfd_link_info *info,
13038 asection *sec,
13039 elf_gc_mark_hook_fn gc_mark_hook)
13040{
13041 bfd_boolean ret;
13042 asection *group_sec, *eh_frame;
13043
13044 sec->gc_mark = 1;
13045
13046 /* Mark all the sections in the group. */
13047 group_sec = elf_section_data (sec)->next_in_group;
13048 if (group_sec && !group_sec->gc_mark)
13049 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13050 return FALSE;
13051
13052 /* Look through the section relocs. */
13053 ret = TRUE;
13054 eh_frame = elf_eh_frame_section (sec->owner);
13055 if ((sec->flags & SEC_RELOC) != 0
13056 && sec->reloc_count > 0
13057 && sec != eh_frame)
13058 {
13059 struct elf_reloc_cookie cookie;
13060
13061 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13062 ret = FALSE;
13063 else
13064 {
13065 for (; cookie.rel < cookie.relend; cookie.rel++)
13066 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13067 {
13068 ret = FALSE;
13069 break;
13070 }
13071 fini_reloc_cookie_for_section (&cookie, sec);
13072 }
13073 }
13074
13075 if (ret && eh_frame && elf_fde_list (sec))
13076 {
13077 struct elf_reloc_cookie cookie;
13078
13079 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13080 ret = FALSE;
13081 else
13082 {
13083 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13084 gc_mark_hook, &cookie))
13085 ret = FALSE;
13086 fini_reloc_cookie_for_section (&cookie, eh_frame);
13087 }
13088 }
13089
13090 eh_frame = elf_section_eh_frame_entry (sec);
13091 if (ret && eh_frame && !eh_frame->gc_mark)
13092 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13093 ret = FALSE;
13094
13095 return ret;
13096}
13097
13098/* Scan and mark sections in a special or debug section group. */
13099
13100static void
13101_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13102{
13103 /* Point to first section of section group. */
13104 asection *ssec;
13105 /* Used to iterate the section group. */
13106 asection *msec;
13107
13108 bfd_boolean is_special_grp = TRUE;
13109 bfd_boolean is_debug_grp = TRUE;
13110
13111 /* First scan to see if group contains any section other than debug
13112 and special section. */
13113 ssec = msec = elf_next_in_group (grp);
13114 do
13115 {
13116 if ((msec->flags & SEC_DEBUGGING) == 0)
13117 is_debug_grp = FALSE;
13118
13119 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13120 is_special_grp = FALSE;
13121
13122 msec = elf_next_in_group (msec);
13123 }
13124 while (msec != ssec);
13125
13126 /* If this is a pure debug section group or pure special section group,
13127 keep all sections in this group. */
13128 if (is_debug_grp || is_special_grp)
13129 {
13130 do
13131 {
13132 msec->gc_mark = 1;
13133 msec = elf_next_in_group (msec);
13134 }
13135 while (msec != ssec);
13136 }
13137}
13138
13139/* Keep debug and special sections. */
13140
13141bfd_boolean
13142_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13143 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13144{
13145 bfd *ibfd;
13146
13147 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13148 {
13149 asection *isec;
13150 bfd_boolean some_kept;
13151 bfd_boolean debug_frag_seen;
13152 bfd_boolean has_kept_debug_info;
13153
13154 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13155 continue;
13156 isec = ibfd->sections;
13157 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13158 continue;
13159
13160 /* Ensure all linker created sections are kept,
13161 see if any other section is already marked,
13162 and note if we have any fragmented debug sections. */
13163 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13164 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13165 {
13166 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13167 isec->gc_mark = 1;
13168 else if (isec->gc_mark
13169 && (isec->flags & SEC_ALLOC) != 0
13170 && elf_section_type (isec) != SHT_NOTE)
13171 some_kept = TRUE;
13172
13173 if (!debug_frag_seen
13174 && (isec->flags & SEC_DEBUGGING)
13175 && CONST_STRNEQ (isec->name, ".debug_line."))
13176 debug_frag_seen = TRUE;
13177 }
13178
13179 /* If no non-note alloc section in this file will be kept, then
13180 we can toss out the debug and special sections. */
13181 if (!some_kept)
13182 continue;
13183
13184 /* Keep debug and special sections like .comment when they are
13185 not part of a group. Also keep section groups that contain
13186 just debug sections or special sections. */
13187 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13188 {
13189 if ((isec->flags & SEC_GROUP) != 0)
13190 _bfd_elf_gc_mark_debug_special_section_group (isec);
13191 else if (((isec->flags & SEC_DEBUGGING) != 0
13192 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13193 && elf_next_in_group (isec) == NULL)
13194 isec->gc_mark = 1;
13195 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13196 has_kept_debug_info = TRUE;
13197 }
13198
13199 /* Look for CODE sections which are going to be discarded,
13200 and find and discard any fragmented debug sections which
13201 are associated with that code section. */
13202 if (debug_frag_seen)
13203 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13204 if ((isec->flags & SEC_CODE) != 0
13205 && isec->gc_mark == 0)
13206 {
13207 unsigned int ilen;
13208 asection *dsec;
13209
13210 ilen = strlen (isec->name);
13211
13212 /* Association is determined by the name of the debug
13213 section containing the name of the code section as
13214 a suffix. For example .debug_line.text.foo is a
13215 debug section associated with .text.foo. */
13216 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13217 {
13218 unsigned int dlen;
13219
13220 if (dsec->gc_mark == 0
13221 || (dsec->flags & SEC_DEBUGGING) == 0)
13222 continue;
13223
13224 dlen = strlen (dsec->name);
13225
13226 if (dlen > ilen
13227 && strncmp (dsec->name + (dlen - ilen),
13228 isec->name, ilen) == 0)
13229 dsec->gc_mark = 0;
13230 }
13231 }
13232
13233 /* Mark debug sections referenced by kept debug sections. */
13234 if (has_kept_debug_info)
13235 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13236 if (isec->gc_mark
13237 && (isec->flags & SEC_DEBUGGING) != 0)
13238 if (!_bfd_elf_gc_mark (info, isec,
13239 elf_gc_mark_debug_section))
13240 return FALSE;
13241 }
13242 return TRUE;
13243}
13244
13245static bfd_boolean
13246elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13247{
13248 bfd *sub;
13249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13250
13251 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13252 {
13253 asection *o;
13254
13255 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13256 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13257 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13258 continue;
13259 o = sub->sections;
13260 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13261 continue;
13262
13263 for (o = sub->sections; o != NULL; o = o->next)
13264 {
13265 /* When any section in a section group is kept, we keep all
13266 sections in the section group. If the first member of
13267 the section group is excluded, we will also exclude the
13268 group section. */
13269 if (o->flags & SEC_GROUP)
13270 {
13271 asection *first = elf_next_in_group (o);
13272 o->gc_mark = first->gc_mark;
13273 }
13274
13275 if (o->gc_mark)
13276 continue;
13277
13278 /* Skip sweeping sections already excluded. */
13279 if (o->flags & SEC_EXCLUDE)
13280 continue;
13281
13282 /* Since this is early in the link process, it is simple
13283 to remove a section from the output. */
13284 o->flags |= SEC_EXCLUDE;
13285
13286 if (info->print_gc_sections && o->size != 0)
13287 /* xgettext:c-format */
13288 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13289 o, sub);
13290 }
13291 }
13292
13293 return TRUE;
13294}
13295
13296/* Propagate collected vtable information. This is called through
13297 elf_link_hash_traverse. */
13298
13299static bfd_boolean
13300elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13301{
13302 /* Those that are not vtables. */
13303 if (h->start_stop
13304 || h->u2.vtable == NULL
13305 || h->u2.vtable->parent == NULL)
13306 return TRUE;
13307
13308 /* Those vtables that do not have parents, we cannot merge. */
13309 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13310 return TRUE;
13311
13312 /* If we've already been done, exit. */
13313 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13314 return TRUE;
13315
13316 /* Make sure the parent's table is up to date. */
13317 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13318
13319 if (h->u2.vtable->used == NULL)
13320 {
13321 /* None of this table's entries were referenced. Re-use the
13322 parent's table. */
13323 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13324 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13325 }
13326 else
13327 {
13328 size_t n;
13329 bfd_boolean *cu, *pu;
13330
13331 /* Or the parent's entries into ours. */
13332 cu = h->u2.vtable->used;
13333 cu[-1] = TRUE;
13334 pu = h->u2.vtable->parent->u2.vtable->used;
13335 if (pu != NULL)
13336 {
13337 const struct elf_backend_data *bed;
13338 unsigned int log_file_align;
13339
13340 bed = get_elf_backend_data (h->root.u.def.section->owner);
13341 log_file_align = bed->s->log_file_align;
13342 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13343 while (n--)
13344 {
13345 if (*pu)
13346 *cu = TRUE;
13347 pu++;
13348 cu++;
13349 }
13350 }
13351 }
13352
13353 return TRUE;
13354}
13355
13356static bfd_boolean
13357elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13358{
13359 asection *sec;
13360 bfd_vma hstart, hend;
13361 Elf_Internal_Rela *relstart, *relend, *rel;
13362 const struct elf_backend_data *bed;
13363 unsigned int log_file_align;
13364
13365 /* Take care of both those symbols that do not describe vtables as
13366 well as those that are not loaded. */
13367 if (h->start_stop
13368 || h->u2.vtable == NULL
13369 || h->u2.vtable->parent == NULL)
13370 return TRUE;
13371
13372 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13373 || h->root.type == bfd_link_hash_defweak);
13374
13375 sec = h->root.u.def.section;
13376 hstart = h->root.u.def.value;
13377 hend = hstart + h->size;
13378
13379 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13380 if (!relstart)
13381 return *(bfd_boolean *) okp = FALSE;
13382 bed = get_elf_backend_data (sec->owner);
13383 log_file_align = bed->s->log_file_align;
13384
13385 relend = relstart + sec->reloc_count;
13386
13387 for (rel = relstart; rel < relend; ++rel)
13388 if (rel->r_offset >= hstart && rel->r_offset < hend)
13389 {
13390 /* If the entry is in use, do nothing. */
13391 if (h->u2.vtable->used
13392 && (rel->r_offset - hstart) < h->u2.vtable->size)
13393 {
13394 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13395 if (h->u2.vtable->used[entry])
13396 continue;
13397 }
13398 /* Otherwise, kill it. */
13399 rel->r_offset = rel->r_info = rel->r_addend = 0;
13400 }
13401
13402 return TRUE;
13403}
13404
13405/* Mark sections containing dynamically referenced symbols. When
13406 building shared libraries, we must assume that any visible symbol is
13407 referenced. */
13408
13409bfd_boolean
13410bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13411{
13412 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13413 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13414
13415 if ((h->root.type == bfd_link_hash_defined
13416 || h->root.type == bfd_link_hash_defweak)
13417 && ((h->ref_dynamic && !h->forced_local)
13418 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13419 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13420 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13421 && (!bfd_link_executable (info)
13422 || info->gc_keep_exported
13423 || info->export_dynamic
13424 || (h->dynamic
13425 && d != NULL
13426 && (*d->match) (&d->head, NULL, h->root.root.string)))
13427 && (h->versioned >= versioned
13428 || !bfd_hide_sym_by_version (info->version_info,
13429 h->root.root.string)))))
13430 h->root.u.def.section->flags |= SEC_KEEP;
13431
13432 return TRUE;
13433}
13434
13435/* Keep all sections containing symbols undefined on the command-line,
13436 and the section containing the entry symbol. */
13437
13438void
13439_bfd_elf_gc_keep (struct bfd_link_info *info)
13440{
13441 struct bfd_sym_chain *sym;
13442
13443 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13444 {
13445 struct elf_link_hash_entry *h;
13446
13447 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13448 FALSE, FALSE, FALSE);
13449
13450 if (h != NULL
13451 && (h->root.type == bfd_link_hash_defined
13452 || h->root.type == bfd_link_hash_defweak)
13453 && !bfd_is_abs_section (h->root.u.def.section)
13454 && !bfd_is_und_section (h->root.u.def.section))
13455 h->root.u.def.section->flags |= SEC_KEEP;
13456 }
13457}
13458
13459bfd_boolean
13460bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13461 struct bfd_link_info *info)
13462{
13463 bfd *ibfd = info->input_bfds;
13464
13465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13466 {
13467 asection *sec;
13468 struct elf_reloc_cookie cookie;
13469
13470 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13471 continue;
13472 sec = ibfd->sections;
13473 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13474 continue;
13475
13476 if (!init_reloc_cookie (&cookie, info, ibfd))
13477 return FALSE;
13478
13479 for (sec = ibfd->sections; sec; sec = sec->next)
13480 {
13481 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13482 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13483 {
13484 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13485 fini_reloc_cookie_rels (&cookie, sec);
13486 }
13487 }
13488 }
13489 return TRUE;
13490}
13491
13492/* Do mark and sweep of unused sections. */
13493
13494bfd_boolean
13495bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13496{
13497 bfd_boolean ok = TRUE;
13498 bfd *sub;
13499 elf_gc_mark_hook_fn gc_mark_hook;
13500 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13501 struct elf_link_hash_table *htab;
13502
13503 if (!bed->can_gc_sections
13504 || !is_elf_hash_table (info->hash))
13505 {
13506 _bfd_error_handler(_("warning: gc-sections option ignored"));
13507 return TRUE;
13508 }
13509
13510 bed->gc_keep (info);
13511 htab = elf_hash_table (info);
13512
13513 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13514 at the .eh_frame section if we can mark the FDEs individually. */
13515 for (sub = info->input_bfds;
13516 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13517 sub = sub->link.next)
13518 {
13519 asection *sec;
13520 struct elf_reloc_cookie cookie;
13521
13522 sec = sub->sections;
13523 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13524 continue;
13525 sec = bfd_get_section_by_name (sub, ".eh_frame");
13526 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13527 {
13528 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13529 if (elf_section_data (sec)->sec_info
13530 && (sec->flags & SEC_LINKER_CREATED) == 0)
13531 elf_eh_frame_section (sub) = sec;
13532 fini_reloc_cookie_for_section (&cookie, sec);
13533 sec = bfd_get_next_section_by_name (NULL, sec);
13534 }
13535 }
13536
13537 /* Apply transitive closure to the vtable entry usage info. */
13538 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13539 if (!ok)
13540 return FALSE;
13541
13542 /* Kill the vtable relocations that were not used. */
13543 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13544 if (!ok)
13545 return FALSE;
13546
13547 /* Mark dynamically referenced symbols. */
13548 if (htab->dynamic_sections_created || info->gc_keep_exported)
13549 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13550
13551 /* Grovel through relocs to find out who stays ... */
13552 gc_mark_hook = bed->gc_mark_hook;
13553 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13554 {
13555 asection *o;
13556
13557 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13558 || elf_object_id (sub) != elf_hash_table_id (htab)
13559 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13560 continue;
13561
13562 o = sub->sections;
13563 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13564 continue;
13565
13566 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13567 Also treat note sections as a root, if the section is not part
13568 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13569 well as FINI_ARRAY sections for ld -r. */
13570 for (o = sub->sections; o != NULL; o = o->next)
13571 if (!o->gc_mark
13572 && (o->flags & SEC_EXCLUDE) == 0
13573 && ((o->flags & SEC_KEEP) != 0
13574 || (bfd_link_relocatable (info)
13575 && ((elf_section_data (o)->this_hdr.sh_type
13576 == SHT_PREINIT_ARRAY)
13577 || (elf_section_data (o)->this_hdr.sh_type
13578 == SHT_INIT_ARRAY)
13579 || (elf_section_data (o)->this_hdr.sh_type
13580 == SHT_FINI_ARRAY)))
13581 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13582 && elf_next_in_group (o) == NULL )))
13583 {
13584 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13585 return FALSE;
13586 }
13587 }
13588
13589 /* Allow the backend to mark additional target specific sections. */
13590 bed->gc_mark_extra_sections (info, gc_mark_hook);
13591
13592 /* ... and mark SEC_EXCLUDE for those that go. */
13593 return elf_gc_sweep (abfd, info);
13594}
13595\f
13596/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13597
13598bfd_boolean
13599bfd_elf_gc_record_vtinherit (bfd *abfd,
13600 asection *sec,
13601 struct elf_link_hash_entry *h,
13602 bfd_vma offset)
13603{
13604 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13605 struct elf_link_hash_entry **search, *child;
13606 size_t extsymcount;
13607 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13608
13609 /* The sh_info field of the symtab header tells us where the
13610 external symbols start. We don't care about the local symbols at
13611 this point. */
13612 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13613 if (!elf_bad_symtab (abfd))
13614 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13615
13616 sym_hashes = elf_sym_hashes (abfd);
13617 sym_hashes_end = sym_hashes + extsymcount;
13618
13619 /* Hunt down the child symbol, which is in this section at the same
13620 offset as the relocation. */
13621 for (search = sym_hashes; search != sym_hashes_end; ++search)
13622 {
13623 if ((child = *search) != NULL
13624 && (child->root.type == bfd_link_hash_defined
13625 || child->root.type == bfd_link_hash_defweak)
13626 && child->root.u.def.section == sec
13627 && child->root.u.def.value == offset)
13628 goto win;
13629 }
13630
13631 /* xgettext:c-format */
13632 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13633 abfd, sec, (uint64_t) offset);
13634 bfd_set_error (bfd_error_invalid_operation);
13635 return FALSE;
13636
13637 win:
13638 if (!child->u2.vtable)
13639 {
13640 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13641 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13642 if (!child->u2.vtable)
13643 return FALSE;
13644 }
13645 if (!h)
13646 {
13647 /* This *should* only be the absolute section. It could potentially
13648 be that someone has defined a non-global vtable though, which
13649 would be bad. It isn't worth paging in the local symbols to be
13650 sure though; that case should simply be handled by the assembler. */
13651
13652 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13653 }
13654 else
13655 child->u2.vtable->parent = h;
13656
13657 return TRUE;
13658}
13659
13660/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13661
13662bfd_boolean
13663bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13664 asection *sec ATTRIBUTE_UNUSED,
13665 struct elf_link_hash_entry *h,
13666 bfd_vma addend)
13667{
13668 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13669 unsigned int log_file_align = bed->s->log_file_align;
13670
13671 if (!h->u2.vtable)
13672 {
13673 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13674 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13675 if (!h->u2.vtable)
13676 return FALSE;
13677 }
13678
13679 if (addend >= h->u2.vtable->size)
13680 {
13681 size_t size, bytes, file_align;
13682 bfd_boolean *ptr = h->u2.vtable->used;
13683
13684 /* While the symbol is undefined, we have to be prepared to handle
13685 a zero size. */
13686 file_align = 1 << log_file_align;
13687 if (h->root.type == bfd_link_hash_undefined)
13688 size = addend + file_align;
13689 else
13690 {
13691 size = h->size;
13692 if (addend >= size)
13693 {
13694 /* Oops! We've got a reference past the defined end of
13695 the table. This is probably a bug -- shall we warn? */
13696 size = addend + file_align;
13697 }
13698 }
13699 size = (size + file_align - 1) & -file_align;
13700
13701 /* Allocate one extra entry for use as a "done" flag for the
13702 consolidation pass. */
13703 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13704
13705 if (ptr)
13706 {
13707 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13708
13709 if (ptr != NULL)
13710 {
13711 size_t oldbytes;
13712
13713 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13714 * sizeof (bfd_boolean));
13715 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13716 }
13717 }
13718 else
13719 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13720
13721 if (ptr == NULL)
13722 return FALSE;
13723
13724 /* And arrange for that done flag to be at index -1. */
13725 h->u2.vtable->used = ptr + 1;
13726 h->u2.vtable->size = size;
13727 }
13728
13729 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13730
13731 return TRUE;
13732}
13733
13734/* Map an ELF section header flag to its corresponding string. */
13735typedef struct
13736{
13737 char *flag_name;
13738 flagword flag_value;
13739} elf_flags_to_name_table;
13740
13741static elf_flags_to_name_table elf_flags_to_names [] =
13742{
13743 { "SHF_WRITE", SHF_WRITE },
13744 { "SHF_ALLOC", SHF_ALLOC },
13745 { "SHF_EXECINSTR", SHF_EXECINSTR },
13746 { "SHF_MERGE", SHF_MERGE },
13747 { "SHF_STRINGS", SHF_STRINGS },
13748 { "SHF_INFO_LINK", SHF_INFO_LINK},
13749 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13750 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13751 { "SHF_GROUP", SHF_GROUP },
13752 { "SHF_TLS", SHF_TLS },
13753 { "SHF_MASKOS", SHF_MASKOS },
13754 { "SHF_EXCLUDE", SHF_EXCLUDE },
13755};
13756
13757/* Returns TRUE if the section is to be included, otherwise FALSE. */
13758bfd_boolean
13759bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13760 struct flag_info *flaginfo,
13761 asection *section)
13762{
13763 const bfd_vma sh_flags = elf_section_flags (section);
13764
13765 if (!flaginfo->flags_initialized)
13766 {
13767 bfd *obfd = info->output_bfd;
13768 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13769 struct flag_info_list *tf = flaginfo->flag_list;
13770 int with_hex = 0;
13771 int without_hex = 0;
13772
13773 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13774 {
13775 unsigned i;
13776 flagword (*lookup) (char *);
13777
13778 lookup = bed->elf_backend_lookup_section_flags_hook;
13779 if (lookup != NULL)
13780 {
13781 flagword hexval = (*lookup) ((char *) tf->name);
13782
13783 if (hexval != 0)
13784 {
13785 if (tf->with == with_flags)
13786 with_hex |= hexval;
13787 else if (tf->with == without_flags)
13788 without_hex |= hexval;
13789 tf->valid = TRUE;
13790 continue;
13791 }
13792 }
13793 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13794 {
13795 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13796 {
13797 if (tf->with == with_flags)
13798 with_hex |= elf_flags_to_names[i].flag_value;
13799 else if (tf->with == without_flags)
13800 without_hex |= elf_flags_to_names[i].flag_value;
13801 tf->valid = TRUE;
13802 break;
13803 }
13804 }
13805 if (!tf->valid)
13806 {
13807 info->callbacks->einfo
13808 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13809 return FALSE;
13810 }
13811 }
13812 flaginfo->flags_initialized = TRUE;
13813 flaginfo->only_with_flags |= with_hex;
13814 flaginfo->not_with_flags |= without_hex;
13815 }
13816
13817 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13818 return FALSE;
13819
13820 if ((flaginfo->not_with_flags & sh_flags) != 0)
13821 return FALSE;
13822
13823 return TRUE;
13824}
13825
13826struct alloc_got_off_arg {
13827 bfd_vma gotoff;
13828 struct bfd_link_info *info;
13829};
13830
13831/* We need a special top-level link routine to convert got reference counts
13832 to real got offsets. */
13833
13834static bfd_boolean
13835elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13836{
13837 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13838 bfd *obfd = gofarg->info->output_bfd;
13839 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13840
13841 if (h->got.refcount > 0)
13842 {
13843 h->got.offset = gofarg->gotoff;
13844 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13845 }
13846 else
13847 h->got.offset = (bfd_vma) -1;
13848
13849 return TRUE;
13850}
13851
13852/* And an accompanying bit to work out final got entry offsets once
13853 we're done. Should be called from final_link. */
13854
13855bfd_boolean
13856bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13857 struct bfd_link_info *info)
13858{
13859 bfd *i;
13860 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13861 bfd_vma gotoff;
13862 struct alloc_got_off_arg gofarg;
13863
13864 BFD_ASSERT (abfd == info->output_bfd);
13865
13866 if (! is_elf_hash_table (info->hash))
13867 return FALSE;
13868
13869 /* The GOT offset is relative to the .got section, but the GOT header is
13870 put into the .got.plt section, if the backend uses it. */
13871 if (bed->want_got_plt)
13872 gotoff = 0;
13873 else
13874 gotoff = bed->got_header_size;
13875
13876 /* Do the local .got entries first. */
13877 for (i = info->input_bfds; i; i = i->link.next)
13878 {
13879 bfd_signed_vma *local_got;
13880 size_t j, locsymcount;
13881 Elf_Internal_Shdr *symtab_hdr;
13882
13883 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13884 continue;
13885
13886 local_got = elf_local_got_refcounts (i);
13887 if (!local_got)
13888 continue;
13889
13890 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13891 if (elf_bad_symtab (i))
13892 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13893 else
13894 locsymcount = symtab_hdr->sh_info;
13895
13896 for (j = 0; j < locsymcount; ++j)
13897 {
13898 if (local_got[j] > 0)
13899 {
13900 local_got[j] = gotoff;
13901 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13902 }
13903 else
13904 local_got[j] = (bfd_vma) -1;
13905 }
13906 }
13907
13908 /* Then the global .got entries. .plt refcounts are handled by
13909 adjust_dynamic_symbol */
13910 gofarg.gotoff = gotoff;
13911 gofarg.info = info;
13912 elf_link_hash_traverse (elf_hash_table (info),
13913 elf_gc_allocate_got_offsets,
13914 &gofarg);
13915 return TRUE;
13916}
13917
13918/* Many folk need no more in the way of final link than this, once
13919 got entry reference counting is enabled. */
13920
13921bfd_boolean
13922bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13923{
13924 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13925 return FALSE;
13926
13927 /* Invoke the regular ELF backend linker to do all the work. */
13928 return bfd_elf_final_link (abfd, info);
13929}
13930
13931bfd_boolean
13932bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13933{
13934 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13935
13936 if (rcookie->bad_symtab)
13937 rcookie->rel = rcookie->rels;
13938
13939 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13940 {
13941 unsigned long r_symndx;
13942
13943 if (! rcookie->bad_symtab)
13944 if (rcookie->rel->r_offset > offset)
13945 return FALSE;
13946 if (rcookie->rel->r_offset != offset)
13947 continue;
13948
13949 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13950 if (r_symndx == STN_UNDEF)
13951 return TRUE;
13952
13953 if (r_symndx >= rcookie->locsymcount
13954 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13955 {
13956 struct elf_link_hash_entry *h;
13957
13958 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13959
13960 while (h->root.type == bfd_link_hash_indirect
13961 || h->root.type == bfd_link_hash_warning)
13962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13963
13964 if ((h->root.type == bfd_link_hash_defined
13965 || h->root.type == bfd_link_hash_defweak)
13966 && (h->root.u.def.section->owner != rcookie->abfd
13967 || h->root.u.def.section->kept_section != NULL
13968 || discarded_section (h->root.u.def.section)))
13969 return TRUE;
13970 }
13971 else
13972 {
13973 /* It's not a relocation against a global symbol,
13974 but it could be a relocation against a local
13975 symbol for a discarded section. */
13976 asection *isec;
13977 Elf_Internal_Sym *isym;
13978
13979 /* Need to: get the symbol; get the section. */
13980 isym = &rcookie->locsyms[r_symndx];
13981 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13982 if (isec != NULL
13983 && (isec->kept_section != NULL
13984 || discarded_section (isec)))
13985 return TRUE;
13986 }
13987 return FALSE;
13988 }
13989 return FALSE;
13990}
13991
13992/* Discard unneeded references to discarded sections.
13993 Returns -1 on error, 1 if any section's size was changed, 0 if
13994 nothing changed. This function assumes that the relocations are in
13995 sorted order, which is true for all known assemblers. */
13996
13997int
13998bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13999{
14000 struct elf_reloc_cookie cookie;
14001 asection *o;
14002 bfd *abfd;
14003 int changed = 0;
14004
14005 if (info->traditional_format
14006 || !is_elf_hash_table (info->hash))
14007 return 0;
14008
14009 o = bfd_get_section_by_name (output_bfd, ".stab");
14010 if (o != NULL)
14011 {
14012 asection *i;
14013
14014 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14015 {
14016 if (i->size == 0
14017 || i->reloc_count == 0
14018 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14019 continue;
14020
14021 abfd = i->owner;
14022 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14023 continue;
14024
14025 if (!init_reloc_cookie_for_section (&cookie, info, i))
14026 return -1;
14027
14028 if (_bfd_discard_section_stabs (abfd, i,
14029 elf_section_data (i)->sec_info,
14030 bfd_elf_reloc_symbol_deleted_p,
14031 &cookie))
14032 changed = 1;
14033
14034 fini_reloc_cookie_for_section (&cookie, i);
14035 }
14036 }
14037
14038 o = NULL;
14039 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14040 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14041 if (o != NULL)
14042 {
14043 asection *i;
14044 int eh_changed = 0;
14045 unsigned int eh_alignment;
14046
14047 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14048 {
14049 if (i->size == 0)
14050 continue;
14051
14052 abfd = i->owner;
14053 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14054 continue;
14055
14056 if (!init_reloc_cookie_for_section (&cookie, info, i))
14057 return -1;
14058
14059 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14060 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14061 bfd_elf_reloc_symbol_deleted_p,
14062 &cookie))
14063 {
14064 eh_changed = 1;
14065 if (i->size != i->rawsize)
14066 changed = 1;
14067 }
14068
14069 fini_reloc_cookie_for_section (&cookie, i);
14070 }
14071
14072 eh_alignment = 1 << o->alignment_power;
14073 /* Skip over zero terminator, and prevent empty sections from
14074 adding alignment padding at the end. */
14075 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14076 if (i->size == 0)
14077 i->flags |= SEC_EXCLUDE;
14078 else if (i->size > 4)
14079 break;
14080 /* The last non-empty eh_frame section doesn't need padding. */
14081 if (i != NULL)
14082 i = i->map_tail.s;
14083 /* Any prior sections must pad the last FDE out to the output
14084 section alignment. Otherwise we might have zero padding
14085 between sections, which would be seen as a terminator. */
14086 for (; i != NULL; i = i->map_tail.s)
14087 if (i->size == 4)
14088 /* All but the last zero terminator should have been removed. */
14089 BFD_FAIL ();
14090 else
14091 {
14092 bfd_size_type size
14093 = (i->size + eh_alignment - 1) & -eh_alignment;
14094 if (i->size != size)
14095 {
14096 i->size = size;
14097 changed = 1;
14098 eh_changed = 1;
14099 }
14100 }
14101 if (eh_changed)
14102 elf_link_hash_traverse (elf_hash_table (info),
14103 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14104 }
14105
14106 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14107 {
14108 const struct elf_backend_data *bed;
14109 asection *s;
14110
14111 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14112 continue;
14113 s = abfd->sections;
14114 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14115 continue;
14116
14117 bed = get_elf_backend_data (abfd);
14118
14119 if (bed->elf_backend_discard_info != NULL)
14120 {
14121 if (!init_reloc_cookie (&cookie, info, abfd))
14122 return -1;
14123
14124 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14125 changed = 1;
14126
14127 fini_reloc_cookie (&cookie, abfd);
14128 }
14129 }
14130
14131 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14132 _bfd_elf_end_eh_frame_parsing (info);
14133
14134 if (info->eh_frame_hdr_type
14135 && !bfd_link_relocatable (info)
14136 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14137 changed = 1;
14138
14139 return changed;
14140}
14141
14142bfd_boolean
14143_bfd_elf_section_already_linked (bfd *abfd,
14144 asection *sec,
14145 struct bfd_link_info *info)
14146{
14147 flagword flags;
14148 const char *name, *key;
14149 struct bfd_section_already_linked *l;
14150 struct bfd_section_already_linked_hash_entry *already_linked_list;
14151
14152 if (sec->output_section == bfd_abs_section_ptr)
14153 return FALSE;
14154
14155 flags = sec->flags;
14156
14157 /* Return if it isn't a linkonce section. A comdat group section
14158 also has SEC_LINK_ONCE set. */
14159 if ((flags & SEC_LINK_ONCE) == 0)
14160 return FALSE;
14161
14162 /* Don't put group member sections on our list of already linked
14163 sections. They are handled as a group via their group section. */
14164 if (elf_sec_group (sec) != NULL)
14165 return FALSE;
14166
14167 /* For a SHT_GROUP section, use the group signature as the key. */
14168 name = sec->name;
14169 if ((flags & SEC_GROUP) != 0
14170 && elf_next_in_group (sec) != NULL
14171 && elf_group_name (elf_next_in_group (sec)) != NULL)
14172 key = elf_group_name (elf_next_in_group (sec));
14173 else
14174 {
14175 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14176 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14177 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14178 key++;
14179 else
14180 /* Must be a user linkonce section that doesn't follow gcc's
14181 naming convention. In this case we won't be matching
14182 single member groups. */
14183 key = name;
14184 }
14185
14186 already_linked_list = bfd_section_already_linked_table_lookup (key);
14187
14188 for (l = already_linked_list->entry; l != NULL; l = l->next)
14189 {
14190 /* We may have 2 different types of sections on the list: group
14191 sections with a signature of <key> (<key> is some string),
14192 and linkonce sections named .gnu.linkonce.<type>.<key>.
14193 Match like sections. LTO plugin sections are an exception.
14194 They are always named .gnu.linkonce.t.<key> and match either
14195 type of section. */
14196 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14197 && ((flags & SEC_GROUP) != 0
14198 || strcmp (name, l->sec->name) == 0))
14199 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14200 {
14201 /* The section has already been linked. See if we should
14202 issue a warning. */
14203 if (!_bfd_handle_already_linked (sec, l, info))
14204 return FALSE;
14205
14206 if (flags & SEC_GROUP)
14207 {
14208 asection *first = elf_next_in_group (sec);
14209 asection *s = first;
14210
14211 while (s != NULL)
14212 {
14213 s->output_section = bfd_abs_section_ptr;
14214 /* Record which group discards it. */
14215 s->kept_section = l->sec;
14216 s = elf_next_in_group (s);
14217 /* These lists are circular. */
14218 if (s == first)
14219 break;
14220 }
14221 }
14222
14223 return TRUE;
14224 }
14225 }
14226
14227 /* A single member comdat group section may be discarded by a
14228 linkonce section and vice versa. */
14229 if ((flags & SEC_GROUP) != 0)
14230 {
14231 asection *first = elf_next_in_group (sec);
14232
14233 if (first != NULL && elf_next_in_group (first) == first)
14234 /* Check this single member group against linkonce sections. */
14235 for (l = already_linked_list->entry; l != NULL; l = l->next)
14236 if ((l->sec->flags & SEC_GROUP) == 0
14237 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14238 {
14239 first->output_section = bfd_abs_section_ptr;
14240 first->kept_section = l->sec;
14241 sec->output_section = bfd_abs_section_ptr;
14242 break;
14243 }
14244 }
14245 else
14246 /* Check this linkonce section against single member groups. */
14247 for (l = already_linked_list->entry; l != NULL; l = l->next)
14248 if (l->sec->flags & SEC_GROUP)
14249 {
14250 asection *first = elf_next_in_group (l->sec);
14251
14252 if (first != NULL
14253 && elf_next_in_group (first) == first
14254 && bfd_elf_match_symbols_in_sections (first, sec, info))
14255 {
14256 sec->output_section = bfd_abs_section_ptr;
14257 sec->kept_section = first;
14258 break;
14259 }
14260 }
14261
14262 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14263 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14264 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14265 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14266 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14267 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14268 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14269 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14270 The reverse order cannot happen as there is never a bfd with only the
14271 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14272 matter as here were are looking only for cross-bfd sections. */
14273
14274 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14275 for (l = already_linked_list->entry; l != NULL; l = l->next)
14276 if ((l->sec->flags & SEC_GROUP) == 0
14277 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14278 {
14279 if (abfd != l->sec->owner)
14280 sec->output_section = bfd_abs_section_ptr;
14281 break;
14282 }
14283
14284 /* This is the first section with this name. Record it. */
14285 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14286 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14287 return sec->output_section == bfd_abs_section_ptr;
14288}
14289
14290bfd_boolean
14291_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14292{
14293 return sym->st_shndx == SHN_COMMON;
14294}
14295
14296unsigned int
14297_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14298{
14299 return SHN_COMMON;
14300}
14301
14302asection *
14303_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14304{
14305 return bfd_com_section_ptr;
14306}
14307
14308bfd_vma
14309_bfd_elf_default_got_elt_size (bfd *abfd,
14310 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14311 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14312 bfd *ibfd ATTRIBUTE_UNUSED,
14313 unsigned long symndx ATTRIBUTE_UNUSED)
14314{
14315 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14316 return bed->s->arch_size / 8;
14317}
14318
14319/* Routines to support the creation of dynamic relocs. */
14320
14321/* Returns the name of the dynamic reloc section associated with SEC. */
14322
14323static const char *
14324get_dynamic_reloc_section_name (bfd * abfd,
14325 asection * sec,
14326 bfd_boolean is_rela)
14327{
14328 char *name;
14329 const char *old_name = bfd_get_section_name (NULL, sec);
14330 const char *prefix = is_rela ? ".rela" : ".rel";
14331
14332 if (old_name == NULL)
14333 return NULL;
14334
14335 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14336 sprintf (name, "%s%s", prefix, old_name);
14337
14338 return name;
14339}
14340
14341/* Returns the dynamic reloc section associated with SEC.
14342 If necessary compute the name of the dynamic reloc section based
14343 on SEC's name (looked up in ABFD's string table) and the setting
14344 of IS_RELA. */
14345
14346asection *
14347_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14348 asection * sec,
14349 bfd_boolean is_rela)
14350{
14351 asection * reloc_sec = elf_section_data (sec)->sreloc;
14352
14353 if (reloc_sec == NULL)
14354 {
14355 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14356
14357 if (name != NULL)
14358 {
14359 reloc_sec = bfd_get_linker_section (abfd, name);
14360
14361 if (reloc_sec != NULL)
14362 elf_section_data (sec)->sreloc = reloc_sec;
14363 }
14364 }
14365
14366 return reloc_sec;
14367}
14368
14369/* Returns the dynamic reloc section associated with SEC. If the
14370 section does not exist it is created and attached to the DYNOBJ
14371 bfd and stored in the SRELOC field of SEC's elf_section_data
14372 structure.
14373
14374 ALIGNMENT is the alignment for the newly created section and
14375 IS_RELA defines whether the name should be .rela.<SEC's name>
14376 or .rel.<SEC's name>. The section name is looked up in the
14377 string table associated with ABFD. */
14378
14379asection *
14380_bfd_elf_make_dynamic_reloc_section (asection *sec,
14381 bfd *dynobj,
14382 unsigned int alignment,
14383 bfd *abfd,
14384 bfd_boolean is_rela)
14385{
14386 asection * reloc_sec = elf_section_data (sec)->sreloc;
14387
14388 if (reloc_sec == NULL)
14389 {
14390 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14391
14392 if (name == NULL)
14393 return NULL;
14394
14395 reloc_sec = bfd_get_linker_section (dynobj, name);
14396
14397 if (reloc_sec == NULL)
14398 {
14399 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14400 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14401 if ((sec->flags & SEC_ALLOC) != 0)
14402 flags |= SEC_ALLOC | SEC_LOAD;
14403
14404 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14405 if (reloc_sec != NULL)
14406 {
14407 /* _bfd_elf_get_sec_type_attr chooses a section type by
14408 name. Override as it may be wrong, eg. for a user
14409 section named "auto" we'll get ".relauto" which is
14410 seen to be a .rela section. */
14411 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14412 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14413 reloc_sec = NULL;
14414 }
14415 }
14416
14417 elf_section_data (sec)->sreloc = reloc_sec;
14418 }
14419
14420 return reloc_sec;
14421}
14422
14423/* Copy the ELF symbol type and other attributes for a linker script
14424 assignment from HSRC to HDEST. Generally this should be treated as
14425 if we found a strong non-dynamic definition for HDEST (except that
14426 ld ignores multiple definition errors). */
14427void
14428_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14429 struct bfd_link_hash_entry *hdest,
14430 struct bfd_link_hash_entry *hsrc)
14431{
14432 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14433 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14434 Elf_Internal_Sym isym;
14435
14436 ehdest->type = ehsrc->type;
14437 ehdest->target_internal = ehsrc->target_internal;
14438
14439 isym.st_other = ehsrc->other;
14440 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14441}
14442
14443/* Append a RELA relocation REL to section S in BFD. */
14444
14445void
14446elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14447{
14448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14449 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14450 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14451 bed->s->swap_reloca_out (abfd, rel, loc);
14452}
14453
14454/* Append a REL relocation REL to section S in BFD. */
14455
14456void
14457elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14458{
14459 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14460 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14461 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14462 bed->s->swap_reloc_out (abfd, rel, loc);
14463}
14464
14465/* Define __start, __stop, .startof. or .sizeof. symbol. */
14466
14467struct bfd_link_hash_entry *
14468bfd_elf_define_start_stop (struct bfd_link_info *info,
14469 const char *symbol, asection *sec)
14470{
14471 struct elf_link_hash_entry *h;
14472
14473 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14474 FALSE, FALSE, TRUE);
14475 if (h != NULL
14476 && (h->root.type == bfd_link_hash_undefined
14477 || h->root.type == bfd_link_hash_undefweak
14478 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14479 {
14480 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14481 h->root.type = bfd_link_hash_defined;
14482 h->root.u.def.section = sec;
14483 h->root.u.def.value = 0;
14484 h->def_regular = 1;
14485 h->def_dynamic = 0;
14486 h->start_stop = 1;
14487 h->u2.start_stop_section = sec;
14488 if (symbol[0] == '.')
14489 {
14490 /* .startof. and .sizeof. symbols are local. */
14491 const struct elf_backend_data *bed;
14492 bed = get_elf_backend_data (info->output_bfd);
14493 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14494 }
14495 else
14496 {
14497 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14498 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14499 if (was_dynamic)
14500 bfd_elf_link_record_dynamic_symbol (info, h);
14501 }
14502 return &h->root;
14503 }
14504 return NULL;
14505}
This page took 0.071364 seconds and 4 git commands to generate.