elflink.c revision 1.16.2.1 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
39 struct 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
48 struct 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
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
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
104 struct 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
145 bfd_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
205 /* Create a strtab to hold the dynamic symbol names. */
207 static bfd_boolean
208 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 {
210 struct elf_link_hash_table *hash_table;
211
212 hash_table = elf_hash_table (info);
213 if (hash_table->dynobj == NULL)
214 {
215 /* We may not set dynobj, an input file holding linker created
216 dynamic sections to abfd, which may be a dynamic object with
217 its own dynamic sections. We need to find a normal input file
218 to hold linker created sections if possible. */
219 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
220 {
221 bfd *ibfd;
222 asection *s;
223 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 if ((ibfd->flags
225 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
226 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
227 && !((s = ibfd->sections) != NULL
228 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
229 {
230 abfd = ibfd;
231 break;
232 }
233 }
234 hash_table->dynobj = abfd;
235 }
236
237 if (hash_table->dynstr == NULL)
238 {
239 hash_table->dynstr = _bfd_elf_strtab_init ();
240 if (hash_table->dynstr == NULL)
241 return FALSE;
242 }
243 return TRUE;
244 }
245
246 /* Create some sections which will be filled in with dynamic linking
247 information. ABFD is an input file which requires dynamic sections
248 to be created. The dynamic sections take up virtual memory space
249 when the final executable is run, so we need to create them before
250 addresses are assigned to the output sections. We work out the
251 actual contents and size of these sections later. */
252
253 bfd_boolean
254 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
255 {
256 flagword flags;
257 asection *s;
258 const struct elf_backend_data *bed;
259 struct elf_link_hash_entry *h;
260
261 if (! is_elf_hash_table (info->hash))
262 return FALSE;
263
264 if (elf_hash_table (info)->dynamic_sections_created)
265 return TRUE;
266
267 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
268 return FALSE;
269
270 abfd = elf_hash_table (info)->dynobj;
271 bed = get_elf_backend_data (abfd);
272
273 flags = bed->dynamic_sec_flags;
274
275 /* A dynamically linked executable has a .interp section, but a
276 shared library does not. */
277 if (bfd_link_executable (info) && !info->nointerp)
278 {
279 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
280 flags | SEC_READONLY);
281 if (s == NULL)
282 return FALSE;
283 }
284
285 /* Create sections to hold version informations. These are removed
286 if they are not needed. */
287 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
288 flags | SEC_READONLY);
289 if (s == NULL
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 return FALSE;
292
293 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
294 flags | SEC_READONLY);
295 if (s == NULL
296 || ! bfd_set_section_alignment (abfd, s, 1))
297 return FALSE;
298
299 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
300 flags | SEC_READONLY);
301 if (s == NULL
302 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
303 return FALSE;
304
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
306 flags | SEC_READONLY);
307 if (s == NULL
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 return FALSE;
310 elf_hash_table (info)->dynsym = s;
311
312 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
313 flags | SEC_READONLY);
314 if (s == NULL)
315 return FALSE;
316
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 if (s == NULL
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 return FALSE;
321
322 /* The special symbol _DYNAMIC is always set to the start of the
323 .dynamic section. We could set _DYNAMIC in a linker script, but we
324 only want to define it if we are, in fact, creating a .dynamic
325 section. We don't want to define it if there is no .dynamic
326 section, since on some ELF platforms the start up code examines it
327 to decide how to initialize the process. */
328 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
329 elf_hash_table (info)->hdynamic = h;
330 if (h == NULL)
331 return FALSE;
332
333 if (info->emit_hash)
334 {
335 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
336 flags | SEC_READONLY);
337 if (s == NULL
338 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 return FALSE;
340 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
341 }
342
343 if (info->emit_gnu_hash)
344 {
345 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
346 flags | SEC_READONLY);
347 if (s == NULL
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 return FALSE;
350 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
351 4 32-bit words followed by variable count of 64-bit words, then
352 variable count of 32-bit words. */
353 if (bed->s->arch_size == 64)
354 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 else
356 elf_section_data (s)->this_hdr.sh_entsize = 4;
357 }
358
359 /* Let the backend create the rest of the sections. This lets the
360 backend set the right flags. The backend will normally create
361 the .got and .plt sections. */
362 if (bed->elf_backend_create_dynamic_sections == NULL
363 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
364 return FALSE;
365
366 elf_hash_table (info)->dynamic_sections_created = TRUE;
367
368 return TRUE;
369 }
370
371 /* Create dynamic sections when linking against a dynamic object. */
372
373 bfd_boolean
374 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 {
376 flagword flags, pltflags;
377 struct elf_link_hash_entry *h;
378 asection *s;
379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
380 struct elf_link_hash_table *htab = elf_hash_table (info);
381
382 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
383 .rel[a].bss sections. */
384 flags = bed->dynamic_sec_flags;
385
386 pltflags = flags;
387 if (bed->plt_not_loaded)
388 /* We do not clear SEC_ALLOC here because we still want the OS to
389 allocate space for the section; it's just that there's nothing
390 to read in from the object file. */
391 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 else
393 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
394 if (bed->plt_readonly)
395 pltflags |= SEC_READONLY;
396
397 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 if (s == NULL
399 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
400 return FALSE;
401 htab->splt = s;
402
403 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 .plt section. */
405 if (bed->want_plt_sym)
406 {
407 h = _bfd_elf_define_linkage_sym (abfd, info, s,
408 "_PROCEDURE_LINKAGE_TABLE_");
409 elf_hash_table (info)->hplt = h;
410 if (h == NULL)
411 return FALSE;
412 }
413
414 s = bfd_make_section_anyway_with_flags (abfd,
415 (bed->rela_plts_and_copies_p
416 ? ".rela.plt" : ".rel.plt"),
417 flags | SEC_READONLY);
418 if (s == NULL
419 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
420 return FALSE;
421 htab->srelplt = s;
422
423 if (! _bfd_elf_create_got_section (abfd, info))
424 return FALSE;
425
426 if (bed->want_dynbss)
427 {
428 /* The .dynbss section is a place to put symbols which are defined
429 by dynamic objects, are referenced by regular objects, and are
430 not functions. We must allocate space for them in the process
431 image and use a R_*_COPY reloc to tell the dynamic linker to
432 initialize them at run time. The linker script puts the .dynbss
433 section into the .bss section of the final image. */
434 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
435 SEC_ALLOC | SEC_LINKER_CREATED);
436 if (s == NULL)
437 return FALSE;
438 htab->sdynbss = s;
439
440 if (bed->want_dynrelro)
441 {
442 /* Similarly, but for symbols that were originally in read-only
443 sections. This section doesn't really need to have contents,
444 but make it like other .data.rel.ro sections. */
445 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
446 flags);
447 if (s == NULL)
448 return FALSE;
449 htab->sdynrelro = s;
450 }
451
452 /* The .rel[a].bss section holds copy relocs. This section is not
453 normally needed. We need to create it here, though, so that the
454 linker will map it to an output section. We can't just create it
455 only if we need it, because we will not know whether we need it
456 until we have seen all the input files, and the first time the
457 main linker code calls BFD after examining all the input files
458 (size_dynamic_sections) the input sections have already been
459 mapped to the output sections. If the section turns out not to
460 be needed, we can discard it later. We will never need this
461 section when generating a shared object, since they do not use
462 copy relocs. */
463 if (bfd_link_executable (info))
464 {
465 s = bfd_make_section_anyway_with_flags (abfd,
466 (bed->rela_plts_and_copies_p
467 ? ".rela.bss" : ".rel.bss"),
468 flags | SEC_READONLY);
469 if (s == NULL
470 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
471 return FALSE;
472 htab->srelbss = s;
473
474 if (bed->want_dynrelro)
475 {
476 s = (bfd_make_section_anyway_with_flags
477 (abfd, (bed->rela_plts_and_copies_p
478 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
479 flags | SEC_READONLY));
480 if (s == NULL
481 || ! bfd_set_section_alignment (abfd, s,
482 bed->s->log_file_align))
483 return FALSE;
484 htab->sreldynrelro = s;
485 }
486 }
487 }
488
489 return TRUE;
490 }
491
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
494 read the input files, since we need to have a list of all of them
495 before we can determine the final sizes of the output sections.
496 Note that we may actually call this function even though we are not
497 going to output any dynamic symbols; in some cases we know that a
498 symbol should be in the dynamic symbol table, but only if there is
499 one. */
500
501 bfd_boolean
502 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
503 struct elf_link_hash_entry *h)
504 {
505 if (h->dynindx == -1)
506 {
507 struct elf_strtab_hash *dynstr;
508 char *p;
509 const char *name;
510 size_t indx;
511
512 /* XXX: The ABI draft says the linker must turn hidden and
513 internal symbols into STB_LOCAL symbols when producing the
514 DSO. However, if ld.so honors st_other in the dynamic table,
515 this would not be necessary. */
516 switch (ELF_ST_VISIBILITY (h->other))
517 {
518 case STV_INTERNAL:
519 case STV_HIDDEN:
520 if (h->root.type != bfd_link_hash_undefined
521 && h->root.type != bfd_link_hash_undefweak)
522 {
523 h->forced_local = 1;
524 if (!elf_hash_table (info)->is_relocatable_executable)
525 return TRUE;
526 }
527
528 default:
529 break;
530 }
531
532 h->dynindx = elf_hash_table (info)->dynsymcount;
533 ++elf_hash_table (info)->dynsymcount;
534
535 dynstr = elf_hash_table (info)->dynstr;
536 if (dynstr == NULL)
537 {
538 /* Create a strtab to hold the dynamic symbol names. */
539 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
540 if (dynstr == NULL)
541 return FALSE;
542 }
543
544 /* We don't put any version information in the dynamic string
545 table. */
546 name = h->root.root.string;
547 p = strchr (name, ELF_VER_CHR);
548 if (p != NULL)
549 /* We know that the p points into writable memory. In fact,
550 there are only a few symbols that have read-only names, being
551 those like _GLOBAL_OFFSET_TABLE_ that are created specially
552 by the backends. Most symbols will have names pointing into
553 an ELF string table read from a file, or to objalloc memory. */
554 *p = 0;
555
556 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
557
558 if (p != NULL)
559 *p = ELF_VER_CHR;
560
561 if (indx == (size_t) -1)
562 return FALSE;
563 h->dynstr_index = indx;
564 }
565
566 return TRUE;
567 }
568
569 /* Mark a symbol dynamic. */
571
572 static void
573 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
574 struct elf_link_hash_entry *h,
575 Elf_Internal_Sym *sym)
576 {
577 struct bfd_elf_dynamic_list *d = info->dynamic_list;
578
579 /* It may be called more than once on the same H. */
580 if(h->dynamic || bfd_link_relocatable (info))
581 return;
582
583 if ((info->dynamic_data
584 && (h->type == STT_OBJECT
585 || h->type == STT_COMMON
586 || (sym != NULL
587 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
588 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
589 || (d != NULL
590 && h->non_elf
591 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 {
593 h->dynamic = 1;
594 /* NB: If a symbol is made dynamic by --dynamic-list, it has
595 non-IR reference. */
596 h->root.non_ir_ref_dynamic = 1;
597 }
598 }
599
600 /* Record an assignment to a symbol made by a linker script. We need
601 this in case some dynamic object refers to this symbol. */
602
603 bfd_boolean
604 bfd_elf_record_link_assignment (bfd *output_bfd,
605 struct bfd_link_info *info,
606 const char *name,
607 bfd_boolean provide,
608 bfd_boolean hidden)
609 {
610 struct elf_link_hash_entry *h, *hv;
611 struct elf_link_hash_table *htab;
612 const struct elf_backend_data *bed;
613
614 if (!is_elf_hash_table (info->hash))
615 return TRUE;
616
617 htab = elf_hash_table (info);
618 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
619 if (h == NULL)
620 return provide;
621
622 if (h->root.type == bfd_link_hash_warning)
623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
624
625 if (h->versioned == unknown)
626 {
627 /* Set versioned if symbol version is unknown. */
628 char *version = strrchr (name, ELF_VER_CHR);
629 if (version)
630 {
631 if (version > name && version[-1] != ELF_VER_CHR)
632 h->versioned = versioned_hidden;
633 else
634 h->versioned = versioned;
635 }
636 }
637
638 /* Symbols defined in a linker script but not referenced anywhere
639 else will have non_elf set. */
640 if (h->non_elf)
641 {
642 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
643 h->non_elf = 0;
644 }
645
646 switch (h->root.type)
647 {
648 case bfd_link_hash_defined:
649 case bfd_link_hash_defweak:
650 case bfd_link_hash_common:
651 break;
652 case bfd_link_hash_undefweak:
653 case bfd_link_hash_undefined:
654 /* Since we're defining the symbol, don't let it seem to have not
655 been defined. record_dynamic_symbol and size_dynamic_sections
656 may depend on this. */
657 h->root.type = bfd_link_hash_new;
658 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
659 bfd_link_repair_undef_list (&htab->root);
660 break;
661 case bfd_link_hash_new:
662 break;
663 case bfd_link_hash_indirect:
664 /* We had a versioned symbol in a dynamic library. We make the
665 the versioned symbol point to this one. */
666 bed = get_elf_backend_data (output_bfd);
667 hv = h;
668 while (hv->root.type == bfd_link_hash_indirect
669 || hv->root.type == bfd_link_hash_warning)
670 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
671 /* We don't need to update h->root.u since linker will set them
672 later. */
673 h->root.type = bfd_link_hash_undefined;
674 hv->root.type = bfd_link_hash_indirect;
675 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
676 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
677 break;
678 default:
679 BFD_FAIL ();
680 return FALSE;
681 }
682
683 /* If this symbol is being provided by the linker script, and it is
684 currently defined by a dynamic object, but not by a regular
685 object, then mark it as undefined so that the generic linker will
686 force the correct value. */
687 if (provide
688 && h->def_dynamic
689 && !h->def_regular)
690 h->root.type = bfd_link_hash_undefined;
691
692 /* If this symbol is not being provided by the linker script, and it is
693 currently defined by a dynamic object, but not by a regular object,
694 then clear out any version information because the symbol will not be
695 associated with the dynamic object any more. */
696 if (!provide
697 && h->def_dynamic
698 && !h->def_regular)
699 h->verinfo.verdef = NULL;
700
701 /* Make sure this symbol is not garbage collected. */
702 h->mark = 1;
703
704 h->def_regular = 1;
705
706 if (hidden)
707 {
708 bed = get_elf_backend_data (output_bfd);
709 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
710 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
711 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
712 }
713
714 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
715 and executables. */
716 if (!bfd_link_relocatable (info)
717 && h->dynindx != -1
718 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
719 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
720 h->forced_local = 1;
721
722 if ((h->def_dynamic
723 || h->ref_dynamic
724 || bfd_link_dll (info)
725 || elf_hash_table (info)->is_relocatable_executable)
726 && !h->forced_local
727 && h->dynindx == -1)
728 {
729 if (! bfd_elf_link_record_dynamic_symbol (info, h))
730 return FALSE;
731
732 /* If this is a weak defined symbol, and we know a corresponding
733 real symbol from the same dynamic object, make sure the real
734 symbol is also made into a dynamic symbol. */
735 if (h->is_weakalias)
736 {
737 struct elf_link_hash_entry *def = weakdef (h);
738
739 if (def->dynindx == -1
740 && !bfd_elf_link_record_dynamic_symbol (info, def))
741 return FALSE;
742 }
743 }
744
745 return TRUE;
746 }
747
748 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
749 success, and 2 on a failure caused by attempting to record a symbol
750 in a discarded section, eg. a discarded link-once section symbol. */
751
752 int
753 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
754 bfd *input_bfd,
755 long input_indx)
756 {
757 bfd_size_type amt;
758 struct elf_link_local_dynamic_entry *entry;
759 struct elf_link_hash_table *eht;
760 struct elf_strtab_hash *dynstr;
761 size_t dynstr_index;
762 char *name;
763 Elf_External_Sym_Shndx eshndx;
764 char esym[sizeof (Elf64_External_Sym)];
765
766 if (! is_elf_hash_table (info->hash))
767 return 0;
768
769 /* See if the entry exists already. */
770 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
771 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
772 return 1;
773
774 amt = sizeof (*entry);
775 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
776 if (entry == NULL)
777 return 0;
778
779 /* Go find the symbol, so that we can find it's name. */
780 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
781 1, input_indx, &entry->isym, esym, &eshndx))
782 {
783 bfd_release (input_bfd, entry);
784 return 0;
785 }
786
787 if (entry->isym.st_shndx != SHN_UNDEF
788 && entry->isym.st_shndx < SHN_LORESERVE)
789 {
790 asection *s;
791
792 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
793 if (s == NULL || bfd_is_abs_section (s->output_section))
794 {
795 /* We can still bfd_release here as nothing has done another
796 bfd_alloc. We can't do this later in this function. */
797 bfd_release (input_bfd, entry);
798 return 2;
799 }
800 }
801
802 name = (bfd_elf_string_from_elf_section
803 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
804 entry->isym.st_name));
805
806 dynstr = elf_hash_table (info)->dynstr;
807 if (dynstr == NULL)
808 {
809 /* Create a strtab to hold the dynamic symbol names. */
810 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
811 if (dynstr == NULL)
812 return 0;
813 }
814
815 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
816 if (dynstr_index == (size_t) -1)
817 return 0;
818 entry->isym.st_name = dynstr_index;
819
820 eht = elf_hash_table (info);
821
822 entry->next = eht->dynlocal;
823 eht->dynlocal = entry;
824 entry->input_bfd = input_bfd;
825 entry->input_indx = input_indx;
826 eht->dynsymcount++;
827
828 /* Whatever binding the symbol had before, it's now local. */
829 entry->isym.st_info
830 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
831
832 /* The dynindx will be set at the end of size_dynamic_sections. */
833
834 return 1;
835 }
836
837 /* Return the dynindex of a local dynamic symbol. */
838
839 long
840 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
841 bfd *input_bfd,
842 long input_indx)
843 {
844 struct elf_link_local_dynamic_entry *e;
845
846 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
847 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
848 return e->dynindx;
849 return -1;
850 }
851
852 /* This function is used to renumber the dynamic symbols, if some of
853 them are removed because they are marked as local. This is called
854 via elf_link_hash_traverse. */
855
856 static bfd_boolean
857 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
858 void *data)
859 {
860 size_t *count = (size_t *) data;
861
862 if (h->forced_local)
863 return TRUE;
864
865 if (h->dynindx != -1)
866 h->dynindx = ++(*count);
867
868 return TRUE;
869 }
870
871
872 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
873 STB_LOCAL binding. */
874
875 static bfd_boolean
876 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
877 void *data)
878 {
879 size_t *count = (size_t *) data;
880
881 if (!h->forced_local)
882 return TRUE;
883
884 if (h->dynindx != -1)
885 h->dynindx = ++(*count);
886
887 return TRUE;
888 }
889
890 /* Return true if the dynamic symbol for a given section should be
891 omitted when creating a shared library. */
892 bfd_boolean
893 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
894 struct bfd_link_info *info,
895 asection *p)
896 {
897 struct elf_link_hash_table *htab;
898 asection *ip;
899
900 switch (elf_section_data (p)->this_hdr.sh_type)
901 {
902 case SHT_PROGBITS:
903 case SHT_NOBITS:
904 /* If sh_type is yet undecided, assume it could be
905 SHT_PROGBITS/SHT_NOBITS. */
906 case SHT_NULL:
907 htab = elf_hash_table (info);
908 if (p == htab->tls_sec)
909 return FALSE;
910
911 if (htab->text_index_section != NULL)
912 return p != htab->text_index_section && p != htab->data_index_section;
913
914 return (htab->dynobj != NULL
915 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
916 && ip->output_section == p);
917
918 /* There shouldn't be section relative relocations
919 against any other section. */
920 default:
921 return TRUE;
922 }
923 }
924
925 bfd_boolean
926 _bfd_elf_omit_section_dynsym_all
927 (bfd *output_bfd ATTRIBUTE_UNUSED,
928 struct bfd_link_info *info ATTRIBUTE_UNUSED,
929 asection *p ATTRIBUTE_UNUSED)
930 {
931 return TRUE;
932 }
933
934 /* Assign dynsym indices. In a shared library we generate a section
935 symbol for each output section, which come first. Next come symbols
936 which have been forced to local binding. Then all of the back-end
937 allocated local dynamic syms, followed by the rest of the global
938 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
939 (This prevents the early call before elf_backend_init_index_section
940 and strip_excluded_output_sections setting dynindx for sections
941 that are stripped.) */
942
943 static unsigned long
944 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
945 struct bfd_link_info *info,
946 unsigned long *section_sym_count)
947 {
948 unsigned long dynsymcount = 0;
949 bfd_boolean do_sec = section_sym_count != NULL;
950
951 if (bfd_link_pic (info)
952 || elf_hash_table (info)->is_relocatable_executable)
953 {
954 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
955 asection *p;
956 for (p = output_bfd->sections; p ; p = p->next)
957 if ((p->flags & SEC_EXCLUDE) == 0
958 && (p->flags & SEC_ALLOC) != 0
959 && elf_hash_table (info)->dynamic_relocs
960 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
961 {
962 ++dynsymcount;
963 if (do_sec)
964 elf_section_data (p)->dynindx = dynsymcount;
965 }
966 else if (do_sec)
967 elf_section_data (p)->dynindx = 0;
968 }
969 if (do_sec)
970 *section_sym_count = dynsymcount;
971
972 elf_link_hash_traverse (elf_hash_table (info),
973 elf_link_renumber_local_hash_table_dynsyms,
974 &dynsymcount);
975
976 if (elf_hash_table (info)->dynlocal)
977 {
978 struct elf_link_local_dynamic_entry *p;
979 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
980 p->dynindx = ++dynsymcount;
981 }
982 elf_hash_table (info)->local_dynsymcount = dynsymcount;
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_hash_table_dynsyms,
986 &dynsymcount);
987
988 /* There is an unused NULL entry at the head of the table which we
989 must account for in our count even if the table is empty since it
990 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
991 .dynamic section. */
992 dynsymcount++;
993
994 elf_hash_table (info)->dynsymcount = dynsymcount;
995 return dynsymcount;
996 }
997
998 /* Merge st_other field. */
999
1000 static void
1001 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1002 const Elf_Internal_Sym *isym, asection *sec,
1003 bfd_boolean definition, bfd_boolean dynamic)
1004 {
1005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1006
1007 /* If st_other has a processor-specific meaning, specific
1008 code might be needed here. */
1009 if (bed->elf_backend_merge_symbol_attribute)
1010 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1011 dynamic);
1012
1013 if (!dynamic)
1014 {
1015 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1016 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1017
1018 /* Keep the most constraining visibility. Leave the remainder
1019 of the st_other field to elf_backend_merge_symbol_attribute. */
1020 if (symvis - 1 < hvis - 1)
1021 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1022 }
1023 else if (definition
1024 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1025 && (sec->flags & SEC_READONLY) == 0)
1026 h->protected_def = 1;
1027 }
1028
1029 /* This function is called when we want to merge a new symbol with an
1030 existing symbol. It handles the various cases which arise when we
1031 find a definition in a dynamic object, or when there is already a
1032 definition in a dynamic object. The new symbol is described by
1033 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1034 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1035 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1036 of an old common symbol. We set OVERRIDE if the old symbol is
1037 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1038 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1039 to change. By OK to change, we mean that we shouldn't warn if the
1040 type or size does change. */
1041
1042 static bfd_boolean
1043 _bfd_elf_merge_symbol (bfd *abfd,
1044 struct bfd_link_info *info,
1045 const char *name,
1046 Elf_Internal_Sym *sym,
1047 asection **psec,
1048 bfd_vma *pvalue,
1049 struct elf_link_hash_entry **sym_hash,
1050 bfd **poldbfd,
1051 bfd_boolean *pold_weak,
1052 unsigned int *pold_alignment,
1053 bfd_boolean *skip,
1054 bfd_boolean *override,
1055 bfd_boolean *type_change_ok,
1056 bfd_boolean *size_change_ok,
1057 bfd_boolean *matched)
1058 {
1059 asection *sec, *oldsec;
1060 struct elf_link_hash_entry *h;
1061 struct elf_link_hash_entry *hi;
1062 struct elf_link_hash_entry *flip;
1063 int bind;
1064 bfd *oldbfd;
1065 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1066 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1067 const struct elf_backend_data *bed;
1068 char *new_version;
1069 bfd_boolean default_sym = *matched;
1070
1071 *skip = FALSE;
1072 *override = FALSE;
1073
1074 sec = *psec;
1075 bind = ELF_ST_BIND (sym->st_info);
1076
1077 if (! bfd_is_und_section (sec))
1078 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1079 else
1080 h = ((struct elf_link_hash_entry *)
1081 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1082 if (h == NULL)
1083 return FALSE;
1084 *sym_hash = h;
1085
1086 bed = get_elf_backend_data (abfd);
1087
1088 /* NEW_VERSION is the symbol version of the new symbol. */
1089 if (h->versioned != unversioned)
1090 {
1091 /* Symbol version is unknown or versioned. */
1092 new_version = strrchr (name, ELF_VER_CHR);
1093 if (new_version)
1094 {
1095 if (h->versioned == unknown)
1096 {
1097 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1098 h->versioned = versioned_hidden;
1099 else
1100 h->versioned = versioned;
1101 }
1102 new_version += 1;
1103 if (new_version[0] == '\0')
1104 new_version = NULL;
1105 }
1106 else
1107 h->versioned = unversioned;
1108 }
1109 else
1110 new_version = NULL;
1111
1112 /* For merging, we only care about real symbols. But we need to make
1113 sure that indirect symbol dynamic flags are updated. */
1114 hi = h;
1115 while (h->root.type == bfd_link_hash_indirect
1116 || h->root.type == bfd_link_hash_warning)
1117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1118
1119 if (!*matched)
1120 {
1121 if (hi == h || h->root.type == bfd_link_hash_new)
1122 *matched = TRUE;
1123 else
1124 {
1125 /* OLD_HIDDEN is true if the existing symbol is only visible
1126 to the symbol with the same symbol version. NEW_HIDDEN is
1127 true if the new symbol is only visible to the symbol with
1128 the same symbol version. */
1129 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1130 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1131 if (!old_hidden && !new_hidden)
1132 /* The new symbol matches the existing symbol if both
1133 aren't hidden. */
1134 *matched = TRUE;
1135 else
1136 {
1137 /* OLD_VERSION is the symbol version of the existing
1138 symbol. */
1139 char *old_version;
1140
1141 if (h->versioned >= versioned)
1142 old_version = strrchr (h->root.root.string,
1143 ELF_VER_CHR) + 1;
1144 else
1145 old_version = NULL;
1146
1147 /* The new symbol matches the existing symbol if they
1148 have the same symbol version. */
1149 *matched = (old_version == new_version
1150 || (old_version != NULL
1151 && new_version != NULL
1152 && strcmp (old_version, new_version) == 0));
1153 }
1154 }
1155 }
1156
1157 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1158 existing symbol. */
1159
1160 oldbfd = NULL;
1161 oldsec = NULL;
1162 switch (h->root.type)
1163 {
1164 default:
1165 break;
1166
1167 case bfd_link_hash_undefined:
1168 case bfd_link_hash_undefweak:
1169 oldbfd = h->root.u.undef.abfd;
1170 break;
1171
1172 case bfd_link_hash_defined:
1173 case bfd_link_hash_defweak:
1174 oldbfd = h->root.u.def.section->owner;
1175 oldsec = h->root.u.def.section;
1176 break;
1177
1178 case bfd_link_hash_common:
1179 oldbfd = h->root.u.c.p->section->owner;
1180 oldsec = h->root.u.c.p->section;
1181 if (pold_alignment)
1182 *pold_alignment = h->root.u.c.p->alignment_power;
1183 break;
1184 }
1185 if (poldbfd && *poldbfd == NULL)
1186 *poldbfd = oldbfd;
1187
1188 /* Differentiate strong and weak symbols. */
1189 newweak = bind == STB_WEAK;
1190 oldweak = (h->root.type == bfd_link_hash_defweak
1191 || h->root.type == bfd_link_hash_undefweak);
1192 if (pold_weak)
1193 *pold_weak = oldweak;
1194
1195 /* We have to check it for every instance since the first few may be
1196 references and not all compilers emit symbol type for undefined
1197 symbols. */
1198 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1199
1200 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1201 respectively, is from a dynamic object. */
1202
1203 newdyn = (abfd->flags & DYNAMIC) != 0;
1204
1205 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1206 syms and defined syms in dynamic libraries respectively.
1207 ref_dynamic on the other hand can be set for a symbol defined in
1208 a dynamic library, and def_dynamic may not be set; When the
1209 definition in a dynamic lib is overridden by a definition in the
1210 executable use of the symbol in the dynamic lib becomes a
1211 reference to the executable symbol. */
1212 if (newdyn)
1213 {
1214 if (bfd_is_und_section (sec))
1215 {
1216 if (bind != STB_WEAK)
1217 {
1218 h->ref_dynamic_nonweak = 1;
1219 hi->ref_dynamic_nonweak = 1;
1220 }
1221 }
1222 else
1223 {
1224 /* Update the existing symbol only if they match. */
1225 if (*matched)
1226 h->dynamic_def = 1;
1227 hi->dynamic_def = 1;
1228 }
1229 }
1230
1231 /* If we just created the symbol, mark it as being an ELF symbol.
1232 Other than that, there is nothing to do--there is no merge issue
1233 with a newly defined symbol--so we just return. */
1234
1235 if (h->root.type == bfd_link_hash_new)
1236 {
1237 h->non_elf = 0;
1238 return TRUE;
1239 }
1240
1241 /* In cases involving weak versioned symbols, we may wind up trying
1242 to merge a symbol with itself. Catch that here, to avoid the
1243 confusion that results if we try to override a symbol with
1244 itself. The additional tests catch cases like
1245 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1246 dynamic object, which we do want to handle here. */
1247 if (abfd == oldbfd
1248 && (newweak || oldweak)
1249 && ((abfd->flags & DYNAMIC) == 0
1250 || !h->def_regular))
1251 return TRUE;
1252
1253 olddyn = FALSE;
1254 if (oldbfd != NULL)
1255 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1256 else if (oldsec != NULL)
1257 {
1258 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1259 indices used by MIPS ELF. */
1260 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1261 }
1262
1263 /* Handle a case where plugin_notice won't be called and thus won't
1264 set the non_ir_ref flags on the first pass over symbols. */
1265 if (oldbfd != NULL
1266 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1267 && newdyn != olddyn)
1268 {
1269 h->root.non_ir_ref_dynamic = TRUE;
1270 hi->root.non_ir_ref_dynamic = TRUE;
1271 }
1272
1273 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1274 respectively, appear to be a definition rather than reference. */
1275
1276 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1277
1278 olddef = (h->root.type != bfd_link_hash_undefined
1279 && h->root.type != bfd_link_hash_undefweak
1280 && h->root.type != bfd_link_hash_common);
1281
1282 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1283 respectively, appear to be a function. */
1284
1285 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1287
1288 oldfunc = (h->type != STT_NOTYPE
1289 && bed->is_function_type (h->type));
1290
1291 if (!(newfunc && oldfunc)
1292 && ELF_ST_TYPE (sym->st_info) != h->type
1293 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1294 && h->type != STT_NOTYPE
1295 && (newdef || bfd_is_com_section (sec))
1296 && (olddef || h->root.type == bfd_link_hash_common))
1297 {
1298 /* If creating a default indirect symbol ("foo" or "foo@") from
1299 a dynamic versioned definition ("foo@@") skip doing so if
1300 there is an existing regular definition with a different
1301 type. We don't want, for example, a "time" variable in the
1302 executable overriding a "time" function in a shared library. */
1303 if (newdyn
1304 && !olddyn)
1305 {
1306 *skip = TRUE;
1307 return TRUE;
1308 }
1309
1310 /* When adding a symbol from a regular object file after we have
1311 created indirect symbols, undo the indirection and any
1312 dynamic state. */
1313 if (hi != h
1314 && !newdyn
1315 && olddyn)
1316 {
1317 h = hi;
1318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1319 h->forced_local = 0;
1320 h->ref_dynamic = 0;
1321 h->def_dynamic = 0;
1322 h->dynamic_def = 0;
1323 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1324 {
1325 h->root.type = bfd_link_hash_undefined;
1326 h->root.u.undef.abfd = abfd;
1327 }
1328 else
1329 {
1330 h->root.type = bfd_link_hash_new;
1331 h->root.u.undef.abfd = NULL;
1332 }
1333 return TRUE;
1334 }
1335 }
1336
1337 /* Check TLS symbols. We don't check undefined symbols introduced
1338 by "ld -u" which have no type (and oldbfd NULL), and we don't
1339 check symbols from plugins because they also have no type. */
1340 if (oldbfd != NULL
1341 && (oldbfd->flags & BFD_PLUGIN) == 0
1342 && (abfd->flags & BFD_PLUGIN) == 0
1343 && ELF_ST_TYPE (sym->st_info) != h->type
1344 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1345 {
1346 bfd *ntbfd, *tbfd;
1347 bfd_boolean ntdef, tdef;
1348 asection *ntsec, *tsec;
1349
1350 if (h->type == STT_TLS)
1351 {
1352 ntbfd = abfd;
1353 ntsec = sec;
1354 ntdef = newdef;
1355 tbfd = oldbfd;
1356 tsec = oldsec;
1357 tdef = olddef;
1358 }
1359 else
1360 {
1361 ntbfd = oldbfd;
1362 ntsec = oldsec;
1363 ntdef = olddef;
1364 tbfd = abfd;
1365 tsec = sec;
1366 tdef = newdef;
1367 }
1368
1369 if (tdef && ntdef)
1370 _bfd_error_handler
1371 /* xgettext:c-format */
1372 (_("%s: TLS definition in %pB section %pA "
1373 "mismatches non-TLS definition in %pB section %pA"),
1374 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1375 else if (!tdef && !ntdef)
1376 _bfd_error_handler
1377 /* xgettext:c-format */
1378 (_("%s: TLS reference in %pB "
1379 "mismatches non-TLS reference in %pB"),
1380 h->root.root.string, tbfd, ntbfd);
1381 else if (tdef)
1382 _bfd_error_handler
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS reference in %pB"),
1386 h->root.root.string, tbfd, tsec, ntbfd);
1387 else
1388 _bfd_error_handler
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS definition in %pB section %pA"),
1392 h->root.root.string, tbfd, ntbfd, ntsec);
1393
1394 bfd_set_error (bfd_error_bad_value);
1395 return FALSE;
1396 }
1397
1398 /* If the old symbol has non-default visibility, we ignore the new
1399 definition from a dynamic object. */
1400 if (newdyn
1401 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1402 && !bfd_is_und_section (sec))
1403 {
1404 *skip = TRUE;
1405 /* Make sure this symbol is dynamic. */
1406 h->ref_dynamic = 1;
1407 hi->ref_dynamic = 1;
1408 /* A protected symbol has external availability. Make sure it is
1409 recorded as dynamic.
1410
1411 FIXME: Should we check type and size for protected symbol? */
1412 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1413 return bfd_elf_link_record_dynamic_symbol (info, h);
1414 else
1415 return TRUE;
1416 }
1417 else if (!newdyn
1418 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1419 && h->def_dynamic)
1420 {
1421 /* If the new symbol with non-default visibility comes from a
1422 relocatable file and the old definition comes from a dynamic
1423 object, we remove the old definition. */
1424 if (hi->root.type == bfd_link_hash_indirect)
1425 {
1426 /* Handle the case where the old dynamic definition is
1427 default versioned. We need to copy the symbol info from
1428 the symbol with default version to the normal one if it
1429 was referenced before. */
1430 if (h->ref_regular)
1431 {
1432 hi->root.type = h->root.type;
1433 h->root.type = bfd_link_hash_indirect;
1434 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1435
1436 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1438 {
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1443 h->ref_dynamic = 0;
1444 }
1445 else
1446 h->ref_dynamic = 1;
1447
1448 h->def_dynamic = 0;
1449 /* FIXME: Should we check type and size for protected symbol? */
1450 h->size = 0;
1451 h->type = 0;
1452
1453 h = hi;
1454 }
1455 else
1456 h = hi;
1457 }
1458
1459 /* If the old symbol was undefined before, then it will still be
1460 on the undefs list. If the new symbol is undefined or
1461 common, we can't make it bfd_link_hash_new here, because new
1462 undefined or common symbols will be added to the undefs list
1463 by _bfd_generic_link_add_one_symbol. Symbols may not be
1464 added twice to the undefs list. Also, if the new symbol is
1465 undefweak then we don't want to lose the strong undef. */
1466 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1467 {
1468 h->root.type = bfd_link_hash_undefined;
1469 h->root.u.undef.abfd = abfd;
1470 }
1471 else
1472 {
1473 h->root.type = bfd_link_hash_new;
1474 h->root.u.undef.abfd = NULL;
1475 }
1476
1477 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1478 {
1479 /* If the new symbol is hidden or internal, completely undo
1480 any dynamic link state. */
1481 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1482 h->forced_local = 0;
1483 h->ref_dynamic = 0;
1484 }
1485 else
1486 h->ref_dynamic = 1;
1487 h->def_dynamic = 0;
1488 /* FIXME: Should we check type and size for protected symbol? */
1489 h->size = 0;
1490 h->type = 0;
1491 return TRUE;
1492 }
1493
1494 /* If a new weak symbol definition comes from a regular file and the
1495 old symbol comes from a dynamic library, we treat the new one as
1496 strong. Similarly, an old weak symbol definition from a regular
1497 file is treated as strong when the new symbol comes from a dynamic
1498 library. Further, an old weak symbol from a dynamic library is
1499 treated as strong if the new symbol is from a dynamic library.
1500 This reflects the way glibc's ld.so works.
1501
1502 Also allow a weak symbol to override a linker script symbol
1503 defined by an early pass over the script. This is done so the
1504 linker knows the symbol is defined in an object file, for the
1505 DEFINED script function.
1506
1507 Do this before setting *type_change_ok or *size_change_ok so that
1508 we warn properly when dynamic library symbols are overridden. */
1509
1510 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1511 newweak = FALSE;
1512 if (olddef && newdyn)
1513 oldweak = FALSE;
1514
1515 /* Allow changes between different types of function symbol. */
1516 if (newfunc && oldfunc)
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the type if either the existing symbol or the
1520 new symbol is weak. A type change is also OK if the old symbol
1521 is undefined and the new symbol is defined. */
1522
1523 if (oldweak
1524 || newweak
1525 || (newdef
1526 && h->root.type == bfd_link_hash_undefined))
1527 *type_change_ok = TRUE;
1528
1529 /* It's OK to change the size if either the existing symbol or the
1530 new symbol is weak, or if the old symbol is undefined. */
1531
1532 if (*type_change_ok
1533 || h->root.type == bfd_link_hash_undefined)
1534 *size_change_ok = TRUE;
1535
1536 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1537 symbol, respectively, appears to be a common symbol in a dynamic
1538 object. If a symbol appears in an uninitialized section, and is
1539 not weak, and is not a function, then it may be a common symbol
1540 which was resolved when the dynamic object was created. We want
1541 to treat such symbols specially, because they raise special
1542 considerations when setting the symbol size: if the symbol
1543 appears as a common symbol in a regular object, and the size in
1544 the regular object is larger, we must make sure that we use the
1545 larger size. This problematic case can always be avoided in C,
1546 but it must be handled correctly when using Fortran shared
1547 libraries.
1548
1549 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1550 likewise for OLDDYNCOMMON and OLDDEF.
1551
1552 Note that this test is just a heuristic, and that it is quite
1553 possible to have an uninitialized symbol in a shared object which
1554 is really a definition, rather than a common symbol. This could
1555 lead to some minor confusion when the symbol really is a common
1556 symbol in some regular object. However, I think it will be
1557 harmless. */
1558
1559 if (newdyn
1560 && newdef
1561 && !newweak
1562 && (sec->flags & SEC_ALLOC) != 0
1563 && (sec->flags & SEC_LOAD) == 0
1564 && sym->st_size > 0
1565 && !newfunc)
1566 newdyncommon = TRUE;
1567 else
1568 newdyncommon = FALSE;
1569
1570 if (olddyn
1571 && olddef
1572 && h->root.type == bfd_link_hash_defined
1573 && h->def_dynamic
1574 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1575 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1576 && h->size > 0
1577 && !oldfunc)
1578 olddyncommon = TRUE;
1579 else
1580 olddyncommon = FALSE;
1581
1582 /* We now know everything about the old and new symbols. We ask the
1583 backend to check if we can merge them. */
1584 if (bed->merge_symbol != NULL)
1585 {
1586 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1587 return FALSE;
1588 sec = *psec;
1589 }
1590
1591 /* There are multiple definitions of a normal symbol. Skip the
1592 default symbol as well as definition from an IR object. */
1593 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1594 && !default_sym && h->def_regular
1595 && !(oldbfd != NULL
1596 && (oldbfd->flags & BFD_PLUGIN) != 0
1597 && (abfd->flags & BFD_PLUGIN) == 0))
1598 {
1599 /* Handle a multiple definition. */
1600 (*info->callbacks->multiple_definition) (info, &h->root,
1601 abfd, sec, *pvalue);
1602 *skip = TRUE;
1603 return TRUE;
1604 }
1605
1606 /* If both the old and the new symbols look like common symbols in a
1607 dynamic object, set the size of the symbol to the larger of the
1608 two. */
1609
1610 if (olddyncommon
1611 && newdyncommon
1612 && sym->st_size != h->size)
1613 {
1614 /* Since we think we have two common symbols, issue a multiple
1615 common warning if desired. Note that we only warn if the
1616 size is different. If the size is the same, we simply let
1617 the old symbol override the new one as normally happens with
1618 symbols defined in dynamic objects. */
1619
1620 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1621 bfd_link_hash_common, sym->st_size);
1622 if (sym->st_size > h->size)
1623 h->size = sym->st_size;
1624
1625 *size_change_ok = TRUE;
1626 }
1627
1628 /* If we are looking at a dynamic object, and we have found a
1629 definition, we need to see if the symbol was already defined by
1630 some other object. If so, we want to use the existing
1631 definition, and we do not want to report a multiple symbol
1632 definition error; we do this by clobbering *PSEC to be
1633 bfd_und_section_ptr.
1634
1635 We treat a common symbol as a definition if the symbol in the
1636 shared library is a function, since common symbols always
1637 represent variables; this can cause confusion in principle, but
1638 any such confusion would seem to indicate an erroneous program or
1639 shared library. We also permit a common symbol in a regular
1640 object to override a weak symbol in a shared object. */
1641
1642 if (newdyn
1643 && newdef
1644 && (olddef
1645 || (h->root.type == bfd_link_hash_common
1646 && (newweak || newfunc))))
1647 {
1648 *override = TRUE;
1649 newdef = FALSE;
1650 newdyncommon = FALSE;
1651
1652 *psec = sec = bfd_und_section_ptr;
1653 *size_change_ok = TRUE;
1654
1655 /* If we get here when the old symbol is a common symbol, then
1656 we are explicitly letting it override a weak symbol or
1657 function in a dynamic object, and we don't want to warn about
1658 a type change. If the old symbol is a defined symbol, a type
1659 change warning may still be appropriate. */
1660
1661 if (h->root.type == bfd_link_hash_common)
1662 *type_change_ok = TRUE;
1663 }
1664
1665 /* Handle the special case of an old common symbol merging with a
1666 new symbol which looks like a common symbol in a shared object.
1667 We change *PSEC and *PVALUE to make the new symbol look like a
1668 common symbol, and let _bfd_generic_link_add_one_symbol do the
1669 right thing. */
1670
1671 if (newdyncommon
1672 && h->root.type == bfd_link_hash_common)
1673 {
1674 *override = TRUE;
1675 newdef = FALSE;
1676 newdyncommon = FALSE;
1677 *pvalue = sym->st_size;
1678 *psec = sec = bed->common_section (oldsec);
1679 *size_change_ok = TRUE;
1680 }
1681
1682 /* Skip weak definitions of symbols that are already defined. */
1683 if (newdef && olddef && newweak)
1684 {
1685 /* Don't skip new non-IR weak syms. */
1686 if (!(oldbfd != NULL
1687 && (oldbfd->flags & BFD_PLUGIN) != 0
1688 && (abfd->flags & BFD_PLUGIN) == 0))
1689 {
1690 newdef = FALSE;
1691 *skip = TRUE;
1692 }
1693
1694 /* Merge st_other. If the symbol already has a dynamic index,
1695 but visibility says it should not be visible, turn it into a
1696 local symbol. */
1697 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1698 if (h->dynindx != -1)
1699 switch (ELF_ST_VISIBILITY (h->other))
1700 {
1701 case STV_INTERNAL:
1702 case STV_HIDDEN:
1703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1704 break;
1705 }
1706 }
1707
1708 /* If the old symbol is from a dynamic object, and the new symbol is
1709 a definition which is not from a dynamic object, then the new
1710 symbol overrides the old symbol. Symbols from regular files
1711 always take precedence over symbols from dynamic objects, even if
1712 they are defined after the dynamic object in the link.
1713
1714 As above, we again permit a common symbol in a regular object to
1715 override a definition in a shared object if the shared object
1716 symbol is a function or is weak. */
1717
1718 flip = NULL;
1719 if (!newdyn
1720 && (newdef
1721 || (bfd_is_com_section (sec)
1722 && (oldweak || oldfunc)))
1723 && olddyn
1724 && olddef
1725 && h->def_dynamic)
1726 {
1727 /* Change the hash table entry to undefined, and let
1728 _bfd_generic_link_add_one_symbol do the right thing with the
1729 new definition. */
1730
1731 h->root.type = bfd_link_hash_undefined;
1732 h->root.u.undef.abfd = h->root.u.def.section->owner;
1733 *size_change_ok = TRUE;
1734
1735 olddef = FALSE;
1736 olddyncommon = FALSE;
1737
1738 /* We again permit a type change when a common symbol may be
1739 overriding a function. */
1740
1741 if (bfd_is_com_section (sec))
1742 {
1743 if (oldfunc)
1744 {
1745 /* If a common symbol overrides a function, make sure
1746 that it isn't defined dynamically nor has type
1747 function. */
1748 h->def_dynamic = 0;
1749 h->type = STT_NOTYPE;
1750 }
1751 *type_change_ok = TRUE;
1752 }
1753
1754 if (hi->root.type == bfd_link_hash_indirect)
1755 flip = hi;
1756 else
1757 /* This union may have been set to be non-NULL when this symbol
1758 was seen in a dynamic object. We must force the union to be
1759 NULL, so that it is correct for a regular symbol. */
1760 h->verinfo.vertree = NULL;
1761 }
1762
1763 /* Handle the special case of a new common symbol merging with an
1764 old symbol that looks like it might be a common symbol defined in
1765 a shared object. Note that we have already handled the case in
1766 which a new common symbol should simply override the definition
1767 in the shared library. */
1768
1769 if (! newdyn
1770 && bfd_is_com_section (sec)
1771 && olddyncommon)
1772 {
1773 /* It would be best if we could set the hash table entry to a
1774 common symbol, but we don't know what to use for the section
1775 or the alignment. */
1776 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1777 bfd_link_hash_common, sym->st_size);
1778
1779 /* If the presumed common symbol in the dynamic object is
1780 larger, pretend that the new symbol has its size. */
1781
1782 if (h->size > *pvalue)
1783 *pvalue = h->size;
1784
1785 /* We need to remember the alignment required by the symbol
1786 in the dynamic object. */
1787 BFD_ASSERT (pold_alignment);
1788 *pold_alignment = h->root.u.def.section->alignment_power;
1789
1790 olddef = FALSE;
1791 olddyncommon = FALSE;
1792
1793 h->root.type = bfd_link_hash_undefined;
1794 h->root.u.undef.abfd = h->root.u.def.section->owner;
1795
1796 *size_change_ok = TRUE;
1797 *type_change_ok = TRUE;
1798
1799 if (hi->root.type == bfd_link_hash_indirect)
1800 flip = hi;
1801 else
1802 h->verinfo.vertree = NULL;
1803 }
1804
1805 if (flip != NULL)
1806 {
1807 /* Handle the case where we had a versioned symbol in a dynamic
1808 library and now find a definition in a normal object. In this
1809 case, we make the versioned symbol point to the normal one. */
1810 flip->root.type = h->root.type;
1811 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1812 h->root.type = bfd_link_hash_indirect;
1813 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1814 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1815 if (h->def_dynamic)
1816 {
1817 h->def_dynamic = 0;
1818 flip->ref_dynamic = 1;
1819 }
1820 }
1821
1822 return TRUE;
1823 }
1824
1825 /* This function is called to create an indirect symbol from the
1826 default for the symbol with the default version if needed. The
1827 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1828 set DYNSYM if the new indirect symbol is dynamic. */
1829
1830 static bfd_boolean
1831 _bfd_elf_add_default_symbol (bfd *abfd,
1832 struct bfd_link_info *info,
1833 struct elf_link_hash_entry *h,
1834 const char *name,
1835 Elf_Internal_Sym *sym,
1836 asection *sec,
1837 bfd_vma value,
1838 bfd **poldbfd,
1839 bfd_boolean *dynsym)
1840 {
1841 bfd_boolean type_change_ok;
1842 bfd_boolean size_change_ok;
1843 bfd_boolean skip;
1844 char *shortname;
1845 struct elf_link_hash_entry *hi;
1846 struct bfd_link_hash_entry *bh;
1847 const struct elf_backend_data *bed;
1848 bfd_boolean collect;
1849 bfd_boolean dynamic;
1850 bfd_boolean override;
1851 char *p;
1852 size_t len, shortlen;
1853 asection *tmp_sec;
1854 bfd_boolean matched;
1855
1856 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1857 return TRUE;
1858
1859 /* If this symbol has a version, and it is the default version, we
1860 create an indirect symbol from the default name to the fully
1861 decorated name. This will cause external references which do not
1862 specify a version to be bound to this version of the symbol. */
1863 p = strchr (name, ELF_VER_CHR);
1864 if (h->versioned == unknown)
1865 {
1866 if (p == NULL)
1867 {
1868 h->versioned = unversioned;
1869 return TRUE;
1870 }
1871 else
1872 {
1873 if (p[1] != ELF_VER_CHR)
1874 {
1875 h->versioned = versioned_hidden;
1876 return TRUE;
1877 }
1878 else
1879 h->versioned = versioned;
1880 }
1881 }
1882 else
1883 {
1884 /* PR ld/19073: We may see an unversioned definition after the
1885 default version. */
1886 if (p == NULL)
1887 return TRUE;
1888 }
1889
1890 bed = get_elf_backend_data (abfd);
1891 collect = bed->collect;
1892 dynamic = (abfd->flags & DYNAMIC) != 0;
1893
1894 shortlen = p - name;
1895 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1896 if (shortname == NULL)
1897 return FALSE;
1898 memcpy (shortname, name, shortlen);
1899 shortname[shortlen] = '\0';
1900
1901 /* We are going to create a new symbol. Merge it with any existing
1902 symbol with this name. For the purposes of the merge, act as
1903 though we were defining the symbol we just defined, although we
1904 actually going to define an indirect symbol. */
1905 type_change_ok = FALSE;
1906 size_change_ok = FALSE;
1907 matched = TRUE;
1908 tmp_sec = sec;
1909 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1910 &hi, poldbfd, NULL, NULL, &skip, &override,
1911 &type_change_ok, &size_change_ok, &matched))
1912 return FALSE;
1913
1914 if (skip)
1915 goto nondefault;
1916
1917 if (hi->def_regular)
1918 {
1919 /* If the undecorated symbol will have a version added by a
1920 script different to H, then don't indirect to/from the
1921 undecorated symbol. This isn't ideal because we may not yet
1922 have seen symbol versions, if given by a script on the
1923 command line rather than via --version-script. */
1924 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1925 {
1926 bfd_boolean hide;
1927
1928 hi->verinfo.vertree
1929 = bfd_find_version_for_sym (info->version_info,
1930 hi->root.root.string, &hide);
1931 if (hi->verinfo.vertree != NULL && hide)
1932 {
1933 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1934 goto nondefault;
1935 }
1936 }
1937 if (hi->verinfo.vertree != NULL
1938 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1939 goto nondefault;
1940 }
1941
1942 if (! override)
1943 {
1944 /* Add the default symbol if not performing a relocatable link. */
1945 if (! bfd_link_relocatable (info))
1946 {
1947 bh = &hi->root;
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982 if (h->def_dynamic)
1983 {
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
1988 {
1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2013
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
2026 if (! bfd_link_executable (info)
2027 || hi->def_dynamic
2028 || hi->ref_dynamic)
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
2033 if (hi->ref_regular)
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
2042 nondefault:
2043 len = strlen (name);
2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 &hi, poldbfd, NULL, NULL, &skip, &override,
2056 &type_change_ok, &size_change_ok, &matched))
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
2069 _bfd_error_handler
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2072 abfd, shortname);
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
2099 if (! bfd_link_executable (info)
2100 || hi->ref_dynamic)
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
2105 if (hi->ref_regular)
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113 }
2114
2115 /* This routine is used to export all defined symbols into the dynamic
2117 symbol table. It is called via elf_link_hash_traverse. */
2118
2119 static bfd_boolean
2120 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2121 {
2122 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2123
2124 /* Ignore indirect symbols. These are added by the versioning code. */
2125 if (h->root.type == bfd_link_hash_indirect)
2126 return TRUE;
2127
2128 /* Ignore this if we won't export it. */
2129 if (!eif->info->export_dynamic && !h->dynamic)
2130 return TRUE;
2131
2132 if (h->dynindx == -1
2133 && (h->def_regular || h->ref_regular)
2134 && ! bfd_hide_sym_by_version (eif->info->version_info,
2135 h->root.root.string))
2136 {
2137 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2138 {
2139 eif->failed = TRUE;
2140 return FALSE;
2141 }
2142 }
2143
2144 return TRUE;
2145 }
2146
2147 /* Look through the symbols which are defined in other shared
2149 libraries and referenced here. Update the list of version
2150 dependencies. This will be put into the .gnu.version_r section.
2151 This function is called via elf_link_hash_traverse. */
2152
2153 static bfd_boolean
2154 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2155 void *data)
2156 {
2157 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2158 Elf_Internal_Verneed *t;
2159 Elf_Internal_Vernaux *a;
2160 bfd_size_type amt;
2161
2162 /* We only care about symbols defined in shared objects with version
2163 information. */
2164 if (!h->def_dynamic
2165 || h->def_regular
2166 || h->dynindx == -1
2167 || h->verinfo.verdef == NULL
2168 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2169 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2170 return TRUE;
2171
2172 /* See if we already know about this version. */
2173 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2174 t != NULL;
2175 t = t->vn_nextref)
2176 {
2177 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2178 continue;
2179
2180 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2181 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2182 return TRUE;
2183
2184 break;
2185 }
2186
2187 /* This is a new version. Add it to tree we are building. */
2188
2189 if (t == NULL)
2190 {
2191 amt = sizeof *t;
2192 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2193 if (t == NULL)
2194 {
2195 rinfo->failed = TRUE;
2196 return FALSE;
2197 }
2198
2199 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2200 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2201 elf_tdata (rinfo->info->output_bfd)->verref = t;
2202 }
2203
2204 amt = sizeof *a;
2205 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2206 if (a == NULL)
2207 {
2208 rinfo->failed = TRUE;
2209 return FALSE;
2210 }
2211
2212 /* Note that we are copying a string pointer here, and testing it
2213 above. If bfd_elf_string_from_elf_section is ever changed to
2214 discard the string data when low in memory, this will have to be
2215 fixed. */
2216 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2217
2218 a->vna_flags = h->verinfo.verdef->vd_flags;
2219 a->vna_nextptr = t->vn_auxptr;
2220
2221 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2222 ++rinfo->vers;
2223
2224 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2225
2226 t->vn_auxptr = a;
2227
2228 return TRUE;
2229 }
2230
2231 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2232 hidden. Set *T_P to NULL if there is no match. */
2233
2234 static bfd_boolean
2235 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2236 struct elf_link_hash_entry *h,
2237 const char *version_p,
2238 struct bfd_elf_version_tree **t_p,
2239 bfd_boolean *hide)
2240 {
2241 struct bfd_elf_version_tree *t;
2242
2243 /* Look for the version. If we find it, it is no longer weak. */
2244 for (t = info->version_info; t != NULL; t = t->next)
2245 {
2246 if (strcmp (t->name, version_p) == 0)
2247 {
2248 size_t len;
2249 char *alc;
2250 struct bfd_elf_version_expr *d;
2251
2252 len = version_p - h->root.root.string;
2253 alc = (char *) bfd_malloc (len);
2254 if (alc == NULL)
2255 return FALSE;
2256 memcpy (alc, h->root.root.string, len - 1);
2257 alc[len - 1] = '\0';
2258 if (alc[len - 2] == ELF_VER_CHR)
2259 alc[len - 2] = '\0';
2260
2261 h->verinfo.vertree = t;
2262 t->used = TRUE;
2263 d = NULL;
2264
2265 if (t->globals.list != NULL)
2266 d = (*t->match) (&t->globals, NULL, alc);
2267
2268 /* See if there is anything to force this symbol to
2269 local scope. */
2270 if (d == NULL && t->locals.list != NULL)
2271 {
2272 d = (*t->match) (&t->locals, NULL, alc);
2273 if (d != NULL
2274 && h->dynindx != -1
2275 && ! info->export_dynamic)
2276 *hide = TRUE;
2277 }
2278
2279 free (alc);
2280 break;
2281 }
2282 }
2283
2284 *t_p = t;
2285
2286 return TRUE;
2287 }
2288
2289 /* Return TRUE if the symbol H is hidden by version script. */
2290
2291 bfd_boolean
2292 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h)
2294 {
2295 const char *p;
2296 bfd_boolean hide = FALSE;
2297 const struct elf_backend_data *bed
2298 = get_elf_backend_data (info->output_bfd);
2299
2300 /* Version script only hides symbols defined in regular objects. */
2301 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2302 return TRUE;
2303
2304 p = strchr (h->root.root.string, ELF_VER_CHR);
2305 if (p != NULL && h->verinfo.vertree == NULL)
2306 {
2307 struct bfd_elf_version_tree *t;
2308
2309 ++p;
2310 if (*p == ELF_VER_CHR)
2311 ++p;
2312
2313 if (*p != '\0'
2314 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2315 && hide)
2316 {
2317 if (hide)
2318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2319 return TRUE;
2320 }
2321 }
2322
2323 /* If we don't have a version for this symbol, see if we can find
2324 something. */
2325 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2326 {
2327 h->verinfo.vertree
2328 = bfd_find_version_for_sym (info->version_info,
2329 h->root.root.string, &hide);
2330 if (h->verinfo.vertree != NULL && hide)
2331 {
2332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2333 return TRUE;
2334 }
2335 }
2336
2337 return FALSE;
2338 }
2339
2340 /* Figure out appropriate versions for all the symbols. We may not
2341 have the version number script until we have read all of the input
2342 files, so until that point we don't know which symbols should be
2343 local. This function is called via elf_link_hash_traverse. */
2344
2345 static bfd_boolean
2346 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2347 {
2348 struct elf_info_failed *sinfo;
2349 struct bfd_link_info *info;
2350 const struct elf_backend_data *bed;
2351 struct elf_info_failed eif;
2352 char *p;
2353 bfd_boolean hide;
2354
2355 sinfo = (struct elf_info_failed *) data;
2356 info = sinfo->info;
2357
2358 /* Fix the symbol flags. */
2359 eif.failed = FALSE;
2360 eif.info = info;
2361 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2362 {
2363 if (eif.failed)
2364 sinfo->failed = TRUE;
2365 return FALSE;
2366 }
2367
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
2370 if (!h->def_regular)
2371 return TRUE;
2372
2373 hide = FALSE;
2374 bed = get_elf_backend_data (info->output_bfd);
2375 p = strchr (h->root.root.string, ELF_VER_CHR);
2376 if (p != NULL && h->verinfo.vertree == NULL)
2377 {
2378 struct bfd_elf_version_tree *t;
2379
2380 ++p;
2381 if (*p == ELF_VER_CHR)
2382 ++p;
2383
2384 /* If there is no version string, we can just return out. */
2385 if (*p == '\0')
2386 return TRUE;
2387
2388 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2389 {
2390 sinfo->failed = TRUE;
2391 return FALSE;
2392 }
2393
2394 if (hide)
2395 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2396
2397 /* If we are building an application, we need to create a
2398 version node for this version. */
2399 if (t == NULL && bfd_link_executable (info))
2400 {
2401 struct bfd_elf_version_tree **pp;
2402 int version_index;
2403
2404 /* If we aren't going to export this symbol, we don't need
2405 to worry about it. */
2406 if (h->dynindx == -1)
2407 return TRUE;
2408
2409 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2410 sizeof *t);
2411 if (t == NULL)
2412 {
2413 sinfo->failed = TRUE;
2414 return FALSE;
2415 }
2416
2417 t->name = p;
2418 t->name_indx = (unsigned int) -1;
2419 t->used = TRUE;
2420
2421 version_index = 1;
2422 /* Don't count anonymous version tag. */
2423 if (sinfo->info->version_info != NULL
2424 && sinfo->info->version_info->vernum == 0)
2425 version_index = 0;
2426 for (pp = &sinfo->info->version_info;
2427 *pp != NULL;
2428 pp = &(*pp)->next)
2429 ++version_index;
2430 t->vernum = version_index;
2431
2432 *pp = t;
2433
2434 h->verinfo.vertree = t;
2435 }
2436 else if (t == NULL)
2437 {
2438 /* We could not find the version for a symbol when
2439 generating a shared archive. Return an error. */
2440 _bfd_error_handler
2441 /* xgettext:c-format */
2442 (_("%pB: version node not found for symbol %s"),
2443 info->output_bfd, h->root.root.string);
2444 bfd_set_error (bfd_error_bad_value);
2445 sinfo->failed = TRUE;
2446 return FALSE;
2447 }
2448 }
2449
2450 /* If we don't have a version for this symbol, see if we can find
2451 something. */
2452 if (!hide
2453 && h->verinfo.vertree == NULL
2454 && sinfo->info->version_info != NULL)
2455 {
2456 h->verinfo.vertree
2457 = bfd_find_version_for_sym (sinfo->info->version_info,
2458 h->root.root.string, &hide);
2459 if (h->verinfo.vertree != NULL && hide)
2460 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2461 }
2462
2463 return TRUE;
2464 }
2465
2466 /* Read and swap the relocs from the section indicated by SHDR. This
2468 may be either a REL or a RELA section. The relocations are
2469 translated into RELA relocations and stored in INTERNAL_RELOCS,
2470 which should have already been allocated to contain enough space.
2471 The EXTERNAL_RELOCS are a buffer where the external form of the
2472 relocations should be stored.
2473
2474 Returns FALSE if something goes wrong. */
2475
2476 static bfd_boolean
2477 elf_link_read_relocs_from_section (bfd *abfd,
2478 asection *sec,
2479 Elf_Internal_Shdr *shdr,
2480 void *external_relocs,
2481 Elf_Internal_Rela *internal_relocs)
2482 {
2483 const struct elf_backend_data *bed;
2484 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2485 const bfd_byte *erela;
2486 const bfd_byte *erelaend;
2487 Elf_Internal_Rela *irela;
2488 Elf_Internal_Shdr *symtab_hdr;
2489 size_t nsyms;
2490
2491 /* Position ourselves at the start of the section. */
2492 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2493 return FALSE;
2494
2495 /* Read the relocations. */
2496 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2497 return FALSE;
2498
2499 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2500 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2501
2502 bed = get_elf_backend_data (abfd);
2503
2504 /* Convert the external relocations to the internal format. */
2505 if (shdr->sh_entsize == bed->s->sizeof_rel)
2506 swap_in = bed->s->swap_reloc_in;
2507 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2508 swap_in = bed->s->swap_reloca_in;
2509 else
2510 {
2511 bfd_set_error (bfd_error_wrong_format);
2512 return FALSE;
2513 }
2514
2515 erela = (const bfd_byte *) external_relocs;
2516 erelaend = erela + shdr->sh_size;
2517 irela = internal_relocs;
2518 while (erela < erelaend)
2519 {
2520 bfd_vma r_symndx;
2521
2522 (*swap_in) (abfd, erela, irela);
2523 r_symndx = ELF32_R_SYM (irela->r_info);
2524 if (bed->s->arch_size == 64)
2525 r_symndx >>= 24;
2526 if (nsyms > 0)
2527 {
2528 if ((size_t) r_symndx >= nsyms)
2529 {
2530 _bfd_error_handler
2531 /* xgettext:c-format */
2532 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2533 " for offset %#" PRIx64 " in section `%pA'"),
2534 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2535 (uint64_t) irela->r_offset, sec);
2536 bfd_set_error (bfd_error_bad_value);
2537 return FALSE;
2538 }
2539 }
2540 else if (r_symndx != STN_UNDEF)
2541 {
2542 _bfd_error_handler
2543 /* xgettext:c-format */
2544 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2545 " for offset %#" PRIx64 " in section `%pA'"
2546 " when the object file has no symbol table"),
2547 abfd, (uint64_t) r_symndx,
2548 (uint64_t) irela->r_offset, sec);
2549 bfd_set_error (bfd_error_bad_value);
2550 return FALSE;
2551 }
2552 irela += bed->s->int_rels_per_ext_rel;
2553 erela += shdr->sh_entsize;
2554 }
2555
2556 return TRUE;
2557 }
2558
2559 /* Read and swap the relocs for a section O. They may have been
2560 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2561 not NULL, they are used as buffers to read into. They are known to
2562 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2563 the return value is allocated using either malloc or bfd_alloc,
2564 according to the KEEP_MEMORY argument. If O has two relocation
2565 sections (both REL and RELA relocations), then the REL_HDR
2566 relocations will appear first in INTERNAL_RELOCS, followed by the
2567 RELA_HDR relocations. */
2568
2569 Elf_Internal_Rela *
2570 _bfd_elf_link_read_relocs (bfd *abfd,
2571 asection *o,
2572 void *external_relocs,
2573 Elf_Internal_Rela *internal_relocs,
2574 bfd_boolean keep_memory)
2575 {
2576 void *alloc1 = NULL;
2577 Elf_Internal_Rela *alloc2 = NULL;
2578 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2579 struct bfd_elf_section_data *esdo = elf_section_data (o);
2580 Elf_Internal_Rela *internal_rela_relocs;
2581
2582 if (esdo->relocs != NULL)
2583 return esdo->relocs;
2584
2585 if (o->reloc_count == 0)
2586 return NULL;
2587
2588 if (internal_relocs == NULL)
2589 {
2590 bfd_size_type size;
2591
2592 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2593 if (keep_memory)
2594 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2595 else
2596 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2597 if (internal_relocs == NULL)
2598 goto error_return;
2599 }
2600
2601 if (external_relocs == NULL)
2602 {
2603 bfd_size_type size = 0;
2604
2605 if (esdo->rel.hdr)
2606 size += esdo->rel.hdr->sh_size;
2607 if (esdo->rela.hdr)
2608 size += esdo->rela.hdr->sh_size;
2609
2610 alloc1 = bfd_malloc (size);
2611 if (alloc1 == NULL)
2612 goto error_return;
2613 external_relocs = alloc1;
2614 }
2615
2616 internal_rela_relocs = internal_relocs;
2617 if (esdo->rel.hdr)
2618 {
2619 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2620 external_relocs,
2621 internal_relocs))
2622 goto error_return;
2623 external_relocs = (((bfd_byte *) external_relocs)
2624 + esdo->rel.hdr->sh_size);
2625 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2626 * bed->s->int_rels_per_ext_rel);
2627 }
2628
2629 if (esdo->rela.hdr
2630 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2631 external_relocs,
2632 internal_rela_relocs)))
2633 goto error_return;
2634
2635 /* Cache the results for next time, if we can. */
2636 if (keep_memory)
2637 esdo->relocs = internal_relocs;
2638
2639 if (alloc1 != NULL)
2640 free (alloc1);
2641
2642 /* Don't free alloc2, since if it was allocated we are passing it
2643 back (under the name of internal_relocs). */
2644
2645 return internal_relocs;
2646
2647 error_return:
2648 if (alloc1 != NULL)
2649 free (alloc1);
2650 if (alloc2 != NULL)
2651 {
2652 if (keep_memory)
2653 bfd_release (abfd, alloc2);
2654 else
2655 free (alloc2);
2656 }
2657 return NULL;
2658 }
2659
2660 /* Compute the size of, and allocate space for, REL_HDR which is the
2661 section header for a section containing relocations for O. */
2662
2663 static bfd_boolean
2664 _bfd_elf_link_size_reloc_section (bfd *abfd,
2665 struct bfd_elf_section_reloc_data *reldata)
2666 {
2667 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2668
2669 /* That allows us to calculate the size of the section. */
2670 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2671
2672 /* The contents field must last into write_object_contents, so we
2673 allocate it with bfd_alloc rather than malloc. Also since we
2674 cannot be sure that the contents will actually be filled in,
2675 we zero the allocated space. */
2676 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2677 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2678 return FALSE;
2679
2680 if (reldata->hashes == NULL && reldata->count)
2681 {
2682 struct elf_link_hash_entry **p;
2683
2684 p = ((struct elf_link_hash_entry **)
2685 bfd_zmalloc (reldata->count * sizeof (*p)));
2686 if (p == NULL)
2687 return FALSE;
2688
2689 reldata->hashes = p;
2690 }
2691
2692 return TRUE;
2693 }
2694
2695 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2696 originated from the section given by INPUT_REL_HDR) to the
2697 OUTPUT_BFD. */
2698
2699 bfd_boolean
2700 _bfd_elf_link_output_relocs (bfd *output_bfd,
2701 asection *input_section,
2702 Elf_Internal_Shdr *input_rel_hdr,
2703 Elf_Internal_Rela *internal_relocs,
2704 struct elf_link_hash_entry **rel_hash
2705 ATTRIBUTE_UNUSED)
2706 {
2707 Elf_Internal_Rela *irela;
2708 Elf_Internal_Rela *irelaend;
2709 bfd_byte *erel;
2710 struct bfd_elf_section_reloc_data *output_reldata;
2711 asection *output_section;
2712 const struct elf_backend_data *bed;
2713 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2714 struct bfd_elf_section_data *esdo;
2715
2716 output_section = input_section->output_section;
2717
2718 bed = get_elf_backend_data (output_bfd);
2719 esdo = elf_section_data (output_section);
2720 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2721 {
2722 output_reldata = &esdo->rel;
2723 swap_out = bed->s->swap_reloc_out;
2724 }
2725 else if (esdo->rela.hdr
2726 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2727 {
2728 output_reldata = &esdo->rela;
2729 swap_out = bed->s->swap_reloca_out;
2730 }
2731 else
2732 {
2733 _bfd_error_handler
2734 /* xgettext:c-format */
2735 (_("%pB: relocation size mismatch in %pB section %pA"),
2736 output_bfd, input_section->owner, input_section);
2737 bfd_set_error (bfd_error_wrong_format);
2738 return FALSE;
2739 }
2740
2741 erel = output_reldata->hdr->contents;
2742 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2743 irela = internal_relocs;
2744 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2745 * bed->s->int_rels_per_ext_rel);
2746 while (irela < irelaend)
2747 {
2748 (*swap_out) (output_bfd, irela, erel);
2749 irela += bed->s->int_rels_per_ext_rel;
2750 erel += input_rel_hdr->sh_entsize;
2751 }
2752
2753 /* Bump the counter, so that we know where to add the next set of
2754 relocations. */
2755 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2756
2757 return TRUE;
2758 }
2759
2760 /* Make weak undefined symbols in PIE dynamic. */
2762
2763 bfd_boolean
2764 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h)
2766 {
2767 if (bfd_link_pie (info)
2768 && h->dynindx == -1
2769 && h->root.type == bfd_link_hash_undefweak)
2770 return bfd_elf_link_record_dynamic_symbol (info, h);
2771
2772 return TRUE;
2773 }
2774
2775 /* Fix up the flags for a symbol. This handles various cases which
2776 can only be fixed after all the input files are seen. This is
2777 currently called by both adjust_dynamic_symbol and
2778 assign_sym_version, which is unnecessary but perhaps more robust in
2779 the face of future changes. */
2780
2781 static bfd_boolean
2782 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2783 struct elf_info_failed *eif)
2784 {
2785 const struct elf_backend_data *bed;
2786
2787 /* If this symbol was mentioned in a non-ELF file, try to set
2788 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2789 permit a non-ELF file to correctly refer to a symbol defined in
2790 an ELF dynamic object. */
2791 if (h->non_elf)
2792 {
2793 while (h->root.type == bfd_link_hash_indirect)
2794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2795
2796 if (h->root.type != bfd_link_hash_defined
2797 && h->root.type != bfd_link_hash_defweak)
2798 {
2799 h->ref_regular = 1;
2800 h->ref_regular_nonweak = 1;
2801 }
2802 else
2803 {
2804 if (h->root.u.def.section->owner != NULL
2805 && (bfd_get_flavour (h->root.u.def.section->owner)
2806 == bfd_target_elf_flavour))
2807 {
2808 h->ref_regular = 1;
2809 h->ref_regular_nonweak = 1;
2810 }
2811 else
2812 h->def_regular = 1;
2813 }
2814
2815 if (h->dynindx == -1
2816 && (h->def_dynamic
2817 || h->ref_dynamic))
2818 {
2819 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2820 {
2821 eif->failed = TRUE;
2822 return FALSE;
2823 }
2824 }
2825 }
2826 else
2827 {
2828 /* Unfortunately, NON_ELF is only correct if the symbol
2829 was first seen in a non-ELF file. Fortunately, if the symbol
2830 was first seen in an ELF file, we're probably OK unless the
2831 symbol was defined in a non-ELF file. Catch that case here.
2832 FIXME: We're still in trouble if the symbol was first seen in
2833 a dynamic object, and then later in a non-ELF regular object. */
2834 if ((h->root.type == bfd_link_hash_defined
2835 || h->root.type == bfd_link_hash_defweak)
2836 && !h->def_regular
2837 && (h->root.u.def.section->owner != NULL
2838 ? (bfd_get_flavour (h->root.u.def.section->owner)
2839 != bfd_target_elf_flavour)
2840 : (bfd_is_abs_section (h->root.u.def.section)
2841 && !h->def_dynamic)))
2842 h->def_regular = 1;
2843 }
2844
2845 /* Backend specific symbol fixup. */
2846 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2847 if (bed->elf_backend_fixup_symbol
2848 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2849 return FALSE;
2850
2851 /* If this is a final link, and the symbol was defined as a common
2852 symbol in a regular object file, and there was no definition in
2853 any dynamic object, then the linker will have allocated space for
2854 the symbol in a common section but the DEF_REGULAR
2855 flag will not have been set. */
2856 if (h->root.type == bfd_link_hash_defined
2857 && !h->def_regular
2858 && h->ref_regular
2859 && !h->def_dynamic
2860 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2861 h->def_regular = 1;
2862
2863 /* Symbols defined in discarded sections shouldn't be dynamic. */
2864 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2865 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2866
2867 /* If a weak undefined symbol has non-default visibility, we also
2868 hide it from the dynamic linker. */
2869 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2870 && h->root.type == bfd_link_hash_undefweak)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* A hidden versioned symbol in executable should be forced local if
2874 it is is locally defined, not referenced by shared library and not
2875 exported. */
2876 else if (bfd_link_executable (eif->info)
2877 && h->versioned == versioned_hidden
2878 && !eif->info->export_dynamic
2879 && !h->dynamic
2880 && !h->ref_dynamic
2881 && h->def_regular)
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2883
2884 /* If -Bsymbolic was used (which means to bind references to global
2885 symbols to the definition within the shared object), and this
2886 symbol was defined in a regular object, then it actually doesn't
2887 need a PLT entry. Likewise, if the symbol has non-default
2888 visibility. If the symbol has hidden or internal visibility, we
2889 will force it local. */
2890 else if (h->needs_plt
2891 && bfd_link_pic (eif->info)
2892 && is_elf_hash_table (eif->info->hash)
2893 && (SYMBOLIC_BIND (eif->info, h)
2894 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2895 && h->def_regular)
2896 {
2897 bfd_boolean force_local;
2898
2899 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2900 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2901 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2902 }
2903
2904 /* If this is a weak defined symbol in a dynamic object, and we know
2905 the real definition in the dynamic object, copy interesting flags
2906 over to the real definition. */
2907 if (h->is_weakalias)
2908 {
2909 struct elf_link_hash_entry *def = weakdef (h);
2910 while (def->root.type == bfd_link_hash_indirect)
2911 def = (struct elf_link_hash_entry *) def->root.u.i.link;
2912
2913 /* If the real definition is defined by a regular object file,
2914 don't do anything special. See the longer description in
2915 _bfd_elf_adjust_dynamic_symbol, below. */
2916 if (def->def_regular)
2917 {
2918 h = def;
2919 while ((h = h->u.alias) != def)
2920 h->is_weakalias = 0;
2921 }
2922 else
2923 {
2924 while (h->root.type == bfd_link_hash_indirect)
2925 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2926 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2927 || h->root.type == bfd_link_hash_defweak);
2928 BFD_ASSERT (def->def_dynamic);
2929 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2930 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2931 }
2932 }
2933
2934 return TRUE;
2935 }
2936
2937 /* Make the backend pick a good value for a dynamic symbol. This is
2938 called via elf_link_hash_traverse, and also calls itself
2939 recursively. */
2940
2941 static bfd_boolean
2942 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2943 {
2944 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2945 struct elf_link_hash_table *htab;
2946 const struct elf_backend_data *bed;
2947
2948 if (! is_elf_hash_table (eif->info->hash))
2949 return FALSE;
2950
2951 /* Ignore indirect symbols. These are added by the versioning code. */
2952 if (h->root.type == bfd_link_hash_indirect)
2953 return TRUE;
2954
2955 /* Fix the symbol flags. */
2956 if (! _bfd_elf_fix_symbol_flags (h, eif))
2957 return FALSE;
2958
2959 htab = elf_hash_table (eif->info);
2960 bed = get_elf_backend_data (htab->dynobj);
2961
2962 if (h->root.type == bfd_link_hash_undefweak)
2963 {
2964 if (eif->info->dynamic_undefined_weak == 0)
2965 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2966 else if (eif->info->dynamic_undefined_weak > 0
2967 && h->ref_regular
2968 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2969 && !bfd_hide_sym_by_version (eif->info->version_info,
2970 h->root.root.string))
2971 {
2972 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2973 {
2974 eif->failed = TRUE;
2975 return FALSE;
2976 }
2977 }
2978 }
2979
2980 /* If this symbol does not require a PLT entry, and it is not
2981 defined by a dynamic object, or is not referenced by a regular
2982 object, ignore it. We do have to handle a weak defined symbol,
2983 even if no regular object refers to it, if we decided to add it
2984 to the dynamic symbol table. FIXME: Do we normally need to worry
2985 about symbols which are defined by one dynamic object and
2986 referenced by another one? */
2987 if (!h->needs_plt
2988 && h->type != STT_GNU_IFUNC
2989 && (h->def_regular
2990 || !h->def_dynamic
2991 || (!h->ref_regular
2992 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2993 {
2994 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2995 return TRUE;
2996 }
2997
2998 /* If we've already adjusted this symbol, don't do it again. This
2999 can happen via a recursive call. */
3000 if (h->dynamic_adjusted)
3001 return TRUE;
3002
3003 /* Don't look at this symbol again. Note that we must set this
3004 after checking the above conditions, because we may look at a
3005 symbol once, decide not to do anything, and then get called
3006 recursively later after REF_REGULAR is set below. */
3007 h->dynamic_adjusted = 1;
3008
3009 /* If this is a weak definition, and we know a real definition, and
3010 the real symbol is not itself defined by a regular object file,
3011 then get a good value for the real definition. We handle the
3012 real symbol first, for the convenience of the backend routine.
3013
3014 Note that there is a confusing case here. If the real definition
3015 is defined by a regular object file, we don't get the real symbol
3016 from the dynamic object, but we do get the weak symbol. If the
3017 processor backend uses a COPY reloc, then if some routine in the
3018 dynamic object changes the real symbol, we will not see that
3019 change in the corresponding weak symbol. This is the way other
3020 ELF linkers work as well, and seems to be a result of the shared
3021 library model.
3022
3023 I will clarify this issue. Most SVR4 shared libraries define the
3024 variable _timezone and define timezone as a weak synonym. The
3025 tzset call changes _timezone. If you write
3026 extern int timezone;
3027 int _timezone = 5;
3028 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3029 you might expect that, since timezone is a synonym for _timezone,
3030 the same number will print both times. However, if the processor
3031 backend uses a COPY reloc, then actually timezone will be copied
3032 into your process image, and, since you define _timezone
3033 yourself, _timezone will not. Thus timezone and _timezone will
3034 wind up at different memory locations. The tzset call will set
3035 _timezone, leaving timezone unchanged. */
3036
3037 if (h->is_weakalias)
3038 {
3039 struct elf_link_hash_entry *def = weakdef (h);
3040
3041 /* If we get to this point, there is an implicit reference to
3042 the alias by a regular object file via the weak symbol H. */
3043 def->ref_regular = 1;
3044
3045 /* Ensure that the backend adjust_dynamic_symbol function sees
3046 the strong alias before H by recursively calling ourselves. */
3047 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3048 return FALSE;
3049 }
3050
3051 /* If a symbol has no type and no size and does not require a PLT
3052 entry, then we are probably about to do the wrong thing here: we
3053 are probably going to create a COPY reloc for an empty object.
3054 This case can arise when a shared object is built with assembly
3055 code, and the assembly code fails to set the symbol type. */
3056 if (h->size == 0
3057 && h->type == STT_NOTYPE
3058 && !h->needs_plt)
3059 _bfd_error_handler
3060 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3061 h->root.root.string);
3062
3063 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3064 {
3065 eif->failed = TRUE;
3066 return FALSE;
3067 }
3068
3069 return TRUE;
3070 }
3071
3072 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3073 DYNBSS. */
3074
3075 bfd_boolean
3076 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3077 struct elf_link_hash_entry *h,
3078 asection *dynbss)
3079 {
3080 unsigned int power_of_two;
3081 bfd_vma mask;
3082 asection *sec = h->root.u.def.section;
3083
3084 /* The section alignment of the definition is the maximum alignment
3085 requirement of symbols defined in the section. Since we don't
3086 know the symbol alignment requirement, we start with the
3087 maximum alignment and check low bits of the symbol address
3088 for the minimum alignment. */
3089 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3090 mask = ((bfd_vma) 1 << power_of_two) - 1;
3091 while ((h->root.u.def.value & mask) != 0)
3092 {
3093 mask >>= 1;
3094 --power_of_two;
3095 }
3096
3097 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3098 dynbss))
3099 {
3100 /* Adjust the section alignment if needed. */
3101 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3102 power_of_two))
3103 return FALSE;
3104 }
3105
3106 /* We make sure that the symbol will be aligned properly. */
3107 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3108
3109 /* Define the symbol as being at this point in DYNBSS. */
3110 h->root.u.def.section = dynbss;
3111 h->root.u.def.value = dynbss->size;
3112
3113 /* Increment the size of DYNBSS to make room for the symbol. */
3114 dynbss->size += h->size;
3115
3116 /* No error if extern_protected_data is true. */
3117 if (h->protected_def
3118 && (!info->extern_protected_data
3119 || (info->extern_protected_data < 0
3120 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3121 info->callbacks->einfo
3122 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3123 h->root.root.string);
3124
3125 return TRUE;
3126 }
3127
3128 /* Adjust all external symbols pointing into SEC_MERGE sections
3129 to reflect the object merging within the sections. */
3130
3131 static bfd_boolean
3132 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3133 {
3134 asection *sec;
3135
3136 if ((h->root.type == bfd_link_hash_defined
3137 || h->root.type == bfd_link_hash_defweak)
3138 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3139 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3140 {
3141 bfd *output_bfd = (bfd *) data;
3142
3143 h->root.u.def.value =
3144 _bfd_merged_section_offset (output_bfd,
3145 &h->root.u.def.section,
3146 elf_section_data (sec)->sec_info,
3147 h->root.u.def.value);
3148 }
3149
3150 return TRUE;
3151 }
3152
3153 /* Returns false if the symbol referred to by H should be considered
3154 to resolve local to the current module, and true if it should be
3155 considered to bind dynamically. */
3156
3157 bfd_boolean
3158 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3159 struct bfd_link_info *info,
3160 bfd_boolean not_local_protected)
3161 {
3162 bfd_boolean binding_stays_local_p;
3163 const struct elf_backend_data *bed;
3164 struct elf_link_hash_table *hash_table;
3165
3166 if (h == NULL)
3167 return FALSE;
3168
3169 while (h->root.type == bfd_link_hash_indirect
3170 || h->root.type == bfd_link_hash_warning)
3171 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3172
3173 /* If it was forced local, then clearly it's not dynamic. */
3174 if (h->dynindx == -1)
3175 return FALSE;
3176 if (h->forced_local)
3177 return FALSE;
3178
3179 /* Identify the cases where name binding rules say that a
3180 visible symbol resolves locally. */
3181 binding_stays_local_p = (bfd_link_executable (info)
3182 || SYMBOLIC_BIND (info, h));
3183
3184 switch (ELF_ST_VISIBILITY (h->other))
3185 {
3186 case STV_INTERNAL:
3187 case STV_HIDDEN:
3188 return FALSE;
3189
3190 case STV_PROTECTED:
3191 hash_table = elf_hash_table (info);
3192 if (!is_elf_hash_table (hash_table))
3193 return FALSE;
3194
3195 bed = get_elf_backend_data (hash_table->dynobj);
3196
3197 /* Proper resolution for function pointer equality may require
3198 that these symbols perhaps be resolved dynamically, even though
3199 we should be resolving them to the current module. */
3200 if (!not_local_protected || !bed->is_function_type (h->type))
3201 binding_stays_local_p = TRUE;
3202 break;
3203
3204 default:
3205 break;
3206 }
3207
3208 /* If it isn't defined locally, then clearly it's dynamic. */
3209 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3210 return TRUE;
3211
3212 /* Otherwise, the symbol is dynamic if binding rules don't tell
3213 us that it remains local. */
3214 return !binding_stays_local_p;
3215 }
3216
3217 /* Return true if the symbol referred to by H should be considered
3218 to resolve local to the current module, and false otherwise. Differs
3219 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3220 undefined symbols. The two functions are virtually identical except
3221 for the place where dynindx == -1 is tested. If that test is true,
3222 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3223 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3224 defined symbols.
3225 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3226 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3227 treatment of undefined weak symbols. For those that do not make
3228 undefined weak symbols dynamic, both functions may return false. */
3229
3230 bfd_boolean
3231 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3232 struct bfd_link_info *info,
3233 bfd_boolean local_protected)
3234 {
3235 const struct elf_backend_data *bed;
3236 struct elf_link_hash_table *hash_table;
3237
3238 /* If it's a local sym, of course we resolve locally. */
3239 if (h == NULL)
3240 return TRUE;
3241
3242 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3243 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3244 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3245 return TRUE;
3246
3247 /* Forced local symbols resolve locally. */
3248 if (h->forced_local)
3249 return TRUE;
3250
3251 /* Common symbols that become definitions don't get the DEF_REGULAR
3252 flag set, so test it first, and don't bail out. */
3253 if (ELF_COMMON_DEF_P (h))
3254 /* Do nothing. */;
3255 /* If we don't have a definition in a regular file, then we can't
3256 resolve locally. The sym is either undefined or dynamic. */
3257 else if (!h->def_regular)
3258 return FALSE;
3259
3260 /* Non-dynamic symbols resolve locally. */
3261 if (h->dynindx == -1)
3262 return TRUE;
3263
3264 /* At this point, we know the symbol is defined and dynamic. In an
3265 executable it must resolve locally, likewise when building symbolic
3266 shared libraries. */
3267 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3268 return TRUE;
3269
3270 /* Now deal with defined dynamic symbols in shared libraries. Ones
3271 with default visibility might not resolve locally. */
3272 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3273 return FALSE;
3274
3275 hash_table = elf_hash_table (info);
3276 if (!is_elf_hash_table (hash_table))
3277 return TRUE;
3278
3279 bed = get_elf_backend_data (hash_table->dynobj);
3280
3281 /* If extern_protected_data is false, STV_PROTECTED non-function
3282 symbols are local. */
3283 if ((!info->extern_protected_data
3284 || (info->extern_protected_data < 0
3285 && !bed->extern_protected_data))
3286 && !bed->is_function_type (h->type))
3287 return TRUE;
3288
3289 /* Function pointer equality tests may require that STV_PROTECTED
3290 symbols be treated as dynamic symbols. If the address of a
3291 function not defined in an executable is set to that function's
3292 plt entry in the executable, then the address of the function in
3293 a shared library must also be the plt entry in the executable. */
3294 return local_protected;
3295 }
3296
3297 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3298 aligned. Returns the first TLS output section. */
3299
3300 struct bfd_section *
3301 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3302 {
3303 struct bfd_section *sec, *tls;
3304 unsigned int align = 0;
3305
3306 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3307 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3308 break;
3309 tls = sec;
3310
3311 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3312 if (sec->alignment_power > align)
3313 align = sec->alignment_power;
3314
3315 elf_hash_table (info)->tls_sec = tls;
3316
3317 /* Ensure the alignment of the first section is the largest alignment,
3318 so that the tls segment starts aligned. */
3319 if (tls != NULL)
3320 tls->alignment_power = align;
3321
3322 return tls;
3323 }
3324
3325 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3326 static bfd_boolean
3327 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3328 Elf_Internal_Sym *sym)
3329 {
3330 const struct elf_backend_data *bed;
3331
3332 /* Local symbols do not count, but target specific ones might. */
3333 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3334 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3335 return FALSE;
3336
3337 bed = get_elf_backend_data (abfd);
3338 /* Function symbols do not count. */
3339 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3340 return FALSE;
3341
3342 /* If the section is undefined, then so is the symbol. */
3343 if (sym->st_shndx == SHN_UNDEF)
3344 return FALSE;
3345
3346 /* If the symbol is defined in the common section, then
3347 it is a common definition and so does not count. */
3348 if (bed->common_definition (sym))
3349 return FALSE;
3350
3351 /* If the symbol is in a target specific section then we
3352 must rely upon the backend to tell us what it is. */
3353 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3354 /* FIXME - this function is not coded yet:
3355
3356 return _bfd_is_global_symbol_definition (abfd, sym);
3357
3358 Instead for now assume that the definition is not global,
3359 Even if this is wrong, at least the linker will behave
3360 in the same way that it used to do. */
3361 return FALSE;
3362
3363 return TRUE;
3364 }
3365
3366 /* Search the symbol table of the archive element of the archive ABFD
3367 whose archive map contains a mention of SYMDEF, and determine if
3368 the symbol is defined in this element. */
3369 static bfd_boolean
3370 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3371 {
3372 Elf_Internal_Shdr * hdr;
3373 size_t symcount;
3374 size_t extsymcount;
3375 size_t extsymoff;
3376 Elf_Internal_Sym *isymbuf;
3377 Elf_Internal_Sym *isym;
3378 Elf_Internal_Sym *isymend;
3379 bfd_boolean result;
3380
3381 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3382 if (abfd == NULL)
3383 return FALSE;
3384
3385 if (! bfd_check_format (abfd, bfd_object))
3386 return FALSE;
3387
3388 /* Select the appropriate symbol table. If we don't know if the
3389 object file is an IR object, give linker LTO plugin a chance to
3390 get the correct symbol table. */
3391 if (abfd->plugin_format == bfd_plugin_yes
3392 #if BFD_SUPPORTS_PLUGINS
3393 || (abfd->plugin_format == bfd_plugin_unknown
3394 && bfd_link_plugin_object_p (abfd))
3395 #endif
3396 )
3397 {
3398 /* Use the IR symbol table if the object has been claimed by
3399 plugin. */
3400 abfd = abfd->plugin_dummy_bfd;
3401 hdr = &elf_tdata (abfd)->symtab_hdr;
3402 }
3403 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3404 hdr = &elf_tdata (abfd)->symtab_hdr;
3405 else
3406 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3407
3408 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3409
3410 /* The sh_info field of the symtab header tells us where the
3411 external symbols start. We don't care about the local symbols. */
3412 if (elf_bad_symtab (abfd))
3413 {
3414 extsymcount = symcount;
3415 extsymoff = 0;
3416 }
3417 else
3418 {
3419 extsymcount = symcount - hdr->sh_info;
3420 extsymoff = hdr->sh_info;
3421 }
3422
3423 if (extsymcount == 0)
3424 return FALSE;
3425
3426 /* Read in the symbol table. */
3427 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3428 NULL, NULL, NULL);
3429 if (isymbuf == NULL)
3430 return FALSE;
3431
3432 /* Scan the symbol table looking for SYMDEF. */
3433 result = FALSE;
3434 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3435 {
3436 const char *name;
3437
3438 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3439 isym->st_name);
3440 if (name == NULL)
3441 break;
3442
3443 if (strcmp (name, symdef->name) == 0)
3444 {
3445 result = is_global_data_symbol_definition (abfd, isym);
3446 break;
3447 }
3448 }
3449
3450 free (isymbuf);
3451
3452 return result;
3453 }
3454
3455 /* Add an entry to the .dynamic table. */
3457
3458 bfd_boolean
3459 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3460 bfd_vma tag,
3461 bfd_vma val)
3462 {
3463 struct elf_link_hash_table *hash_table;
3464 const struct elf_backend_data *bed;
3465 asection *s;
3466 bfd_size_type newsize;
3467 bfd_byte *newcontents;
3468 Elf_Internal_Dyn dyn;
3469
3470 hash_table = elf_hash_table (info);
3471 if (! is_elf_hash_table (hash_table))
3472 return FALSE;
3473
3474 if (tag == DT_RELA || tag == DT_REL)
3475 hash_table->dynamic_relocs = TRUE;
3476
3477 bed = get_elf_backend_data (hash_table->dynobj);
3478 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3479 BFD_ASSERT (s != NULL);
3480
3481 newsize = s->size + bed->s->sizeof_dyn;
3482 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3483 if (newcontents == NULL)
3484 return FALSE;
3485
3486 dyn.d_tag = tag;
3487 dyn.d_un.d_val = val;
3488 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3489
3490 s->size = newsize;
3491 s->contents = newcontents;
3492
3493 return TRUE;
3494 }
3495
3496 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3497 otherwise just check whether one already exists. Returns -1 on error,
3498 1 if a DT_NEEDED tag already exists, and 0 on success. */
3499
3500 static int
3501 elf_add_dt_needed_tag (bfd *abfd,
3502 struct bfd_link_info *info,
3503 const char *soname,
3504 bfd_boolean do_it)
3505 {
3506 struct elf_link_hash_table *hash_table;
3507 size_t strindex;
3508
3509 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3510 return -1;
3511
3512 hash_table = elf_hash_table (info);
3513 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3514 if (strindex == (size_t) -1)
3515 return -1;
3516
3517 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3518 {
3519 asection *sdyn;
3520 const struct elf_backend_data *bed;
3521 bfd_byte *extdyn;
3522
3523 bed = get_elf_backend_data (hash_table->dynobj);
3524 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3525 if (sdyn != NULL)
3526 for (extdyn = sdyn->contents;
3527 extdyn < sdyn->contents + sdyn->size;
3528 extdyn += bed->s->sizeof_dyn)
3529 {
3530 Elf_Internal_Dyn dyn;
3531
3532 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3533 if (dyn.d_tag == DT_NEEDED
3534 && dyn.d_un.d_val == strindex)
3535 {
3536 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3537 return 1;
3538 }
3539 }
3540 }
3541
3542 if (do_it)
3543 {
3544 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3545 return -1;
3546
3547 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3548 return -1;
3549 }
3550 else
3551 /* We were just checking for existence of the tag. */
3552 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3553
3554 return 0;
3555 }
3556
3557 /* Return true if SONAME is on the needed list between NEEDED and STOP
3558 (or the end of list if STOP is NULL), and needed by a library that
3559 will be loaded. */
3560
3561 static bfd_boolean
3562 on_needed_list (const char *soname,
3563 struct bfd_link_needed_list *needed,
3564 struct bfd_link_needed_list *stop)
3565 {
3566 struct bfd_link_needed_list *look;
3567 for (look = needed; look != stop; look = look->next)
3568 if (strcmp (soname, look->name) == 0
3569 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3570 /* If needed by a library that itself is not directly
3571 needed, recursively check whether that library is
3572 indirectly needed. Since we add DT_NEEDED entries to
3573 the end of the list, library dependencies appear after
3574 the library. Therefore search prior to the current
3575 LOOK, preventing possible infinite recursion. */
3576 || on_needed_list (elf_dt_name (look->by), needed, look)))
3577 return TRUE;
3578
3579 return FALSE;
3580 }
3581
3582 /* Sort symbol by value, section, and size. */
3583 static int
3584 elf_sort_symbol (const void *arg1, const void *arg2)
3585 {
3586 const struct elf_link_hash_entry *h1;
3587 const struct elf_link_hash_entry *h2;
3588 bfd_signed_vma vdiff;
3589
3590 h1 = *(const struct elf_link_hash_entry **) arg1;
3591 h2 = *(const struct elf_link_hash_entry **) arg2;
3592 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3593 if (vdiff != 0)
3594 return vdiff > 0 ? 1 : -1;
3595 else
3596 {
3597 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3598 if (sdiff != 0)
3599 return sdiff > 0 ? 1 : -1;
3600 }
3601 vdiff = h1->size - h2->size;
3602 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3603 }
3604
3605 /* This function is used to adjust offsets into .dynstr for
3606 dynamic symbols. This is called via elf_link_hash_traverse. */
3607
3608 static bfd_boolean
3609 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3610 {
3611 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3612
3613 if (h->dynindx != -1)
3614 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3615 return TRUE;
3616 }
3617
3618 /* Assign string offsets in .dynstr, update all structures referencing
3619 them. */
3620
3621 static bfd_boolean
3622 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3623 {
3624 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3625 struct elf_link_local_dynamic_entry *entry;
3626 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3627 bfd *dynobj = hash_table->dynobj;
3628 asection *sdyn;
3629 bfd_size_type size;
3630 const struct elf_backend_data *bed;
3631 bfd_byte *extdyn;
3632
3633 _bfd_elf_strtab_finalize (dynstr);
3634 size = _bfd_elf_strtab_size (dynstr);
3635
3636 bed = get_elf_backend_data (dynobj);
3637 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3638 BFD_ASSERT (sdyn != NULL);
3639
3640 /* Update all .dynamic entries referencing .dynstr strings. */
3641 for (extdyn = sdyn->contents;
3642 extdyn < sdyn->contents + sdyn->size;
3643 extdyn += bed->s->sizeof_dyn)
3644 {
3645 Elf_Internal_Dyn dyn;
3646
3647 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3648 switch (dyn.d_tag)
3649 {
3650 case DT_STRSZ:
3651 dyn.d_un.d_val = size;
3652 break;
3653 case DT_NEEDED:
3654 case DT_SONAME:
3655 case DT_RPATH:
3656 case DT_RUNPATH:
3657 case DT_FILTER:
3658 case DT_AUXILIARY:
3659 case DT_AUDIT:
3660 case DT_DEPAUDIT:
3661 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3662 break;
3663 default:
3664 continue;
3665 }
3666 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3667 }
3668
3669 /* Now update local dynamic symbols. */
3670 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3671 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3672 entry->isym.st_name);
3673
3674 /* And the rest of dynamic symbols. */
3675 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3676
3677 /* Adjust version definitions. */
3678 if (elf_tdata (output_bfd)->cverdefs)
3679 {
3680 asection *s;
3681 bfd_byte *p;
3682 size_t i;
3683 Elf_Internal_Verdef def;
3684 Elf_Internal_Verdaux defaux;
3685
3686 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3687 p = s->contents;
3688 do
3689 {
3690 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3691 &def);
3692 p += sizeof (Elf_External_Verdef);
3693 if (def.vd_aux != sizeof (Elf_External_Verdef))
3694 continue;
3695 for (i = 0; i < def.vd_cnt; ++i)
3696 {
3697 _bfd_elf_swap_verdaux_in (output_bfd,
3698 (Elf_External_Verdaux *) p, &defaux);
3699 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3700 defaux.vda_name);
3701 _bfd_elf_swap_verdaux_out (output_bfd,
3702 &defaux, (Elf_External_Verdaux *) p);
3703 p += sizeof (Elf_External_Verdaux);
3704 }
3705 }
3706 while (def.vd_next);
3707 }
3708
3709 /* Adjust version references. */
3710 if (elf_tdata (output_bfd)->verref)
3711 {
3712 asection *s;
3713 bfd_byte *p;
3714 size_t i;
3715 Elf_Internal_Verneed need;
3716 Elf_Internal_Vernaux needaux;
3717
3718 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3719 p = s->contents;
3720 do
3721 {
3722 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3723 &need);
3724 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3725 _bfd_elf_swap_verneed_out (output_bfd, &need,
3726 (Elf_External_Verneed *) p);
3727 p += sizeof (Elf_External_Verneed);
3728 for (i = 0; i < need.vn_cnt; ++i)
3729 {
3730 _bfd_elf_swap_vernaux_in (output_bfd,
3731 (Elf_External_Vernaux *) p, &needaux);
3732 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3733 needaux.vna_name);
3734 _bfd_elf_swap_vernaux_out (output_bfd,
3735 &needaux,
3736 (Elf_External_Vernaux *) p);
3737 p += sizeof (Elf_External_Vernaux);
3738 }
3739 }
3740 while (need.vn_next);
3741 }
3742
3743 return TRUE;
3744 }
3745
3746 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3748 The default is to only match when the INPUT and OUTPUT are exactly
3749 the same target. */
3750
3751 bfd_boolean
3752 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3753 const bfd_target *output)
3754 {
3755 return input == output;
3756 }
3757
3758 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3759 This version is used when different targets for the same architecture
3760 are virtually identical. */
3761
3762 bfd_boolean
3763 _bfd_elf_relocs_compatible (const bfd_target *input,
3764 const bfd_target *output)
3765 {
3766 const struct elf_backend_data *obed, *ibed;
3767
3768 if (input == output)
3769 return TRUE;
3770
3771 ibed = xvec_get_elf_backend_data (input);
3772 obed = xvec_get_elf_backend_data (output);
3773
3774 if (ibed->arch != obed->arch)
3775 return FALSE;
3776
3777 /* If both backends are using this function, deem them compatible. */
3778 return ibed->relocs_compatible == obed->relocs_compatible;
3779 }
3780
3781 /* Make a special call to the linker "notice" function to tell it that
3782 we are about to handle an as-needed lib, or have finished
3783 processing the lib. */
3784
3785 bfd_boolean
3786 _bfd_elf_notice_as_needed (bfd *ibfd,
3787 struct bfd_link_info *info,
3788 enum notice_asneeded_action act)
3789 {
3790 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3791 }
3792
3793 /* Check relocations an ELF object file. */
3794
3795 bfd_boolean
3796 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3797 {
3798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3799 struct elf_link_hash_table *htab = elf_hash_table (info);
3800
3801 /* If this object is the same format as the output object, and it is
3802 not a shared library, then let the backend look through the
3803 relocs.
3804
3805 This is required to build global offset table entries and to
3806 arrange for dynamic relocs. It is not required for the
3807 particular common case of linking non PIC code, even when linking
3808 against shared libraries, but unfortunately there is no way of
3809 knowing whether an object file has been compiled PIC or not.
3810 Looking through the relocs is not particularly time consuming.
3811 The problem is that we must either (1) keep the relocs in memory,
3812 which causes the linker to require additional runtime memory or
3813 (2) read the relocs twice from the input file, which wastes time.
3814 This would be a good case for using mmap.
3815
3816 I have no idea how to handle linking PIC code into a file of a
3817 different format. It probably can't be done. */
3818 if ((abfd->flags & DYNAMIC) == 0
3819 && is_elf_hash_table (htab)
3820 && bed->check_relocs != NULL
3821 && elf_object_id (abfd) == elf_hash_table_id (htab)
3822 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3823 {
3824 asection *o;
3825
3826 for (o = abfd->sections; o != NULL; o = o->next)
3827 {
3828 Elf_Internal_Rela *internal_relocs;
3829 bfd_boolean ok;
3830
3831 /* Don't check relocations in excluded sections. */
3832 if ((o->flags & SEC_RELOC) == 0
3833 || (o->flags & SEC_EXCLUDE) != 0
3834 || o->reloc_count == 0
3835 || ((info->strip == strip_all || info->strip == strip_debugger)
3836 && (o->flags & SEC_DEBUGGING) != 0)
3837 || bfd_is_abs_section (o->output_section))
3838 continue;
3839
3840 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3841 info->keep_memory);
3842 if (internal_relocs == NULL)
3843 return FALSE;
3844
3845 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3846
3847 if (elf_section_data (o)->relocs != internal_relocs)
3848 free (internal_relocs);
3849
3850 if (! ok)
3851 return FALSE;
3852 }
3853 }
3854
3855 return TRUE;
3856 }
3857
3858 /* Add symbols from an ELF object file to the linker hash table. */
3859
3860 static bfd_boolean
3861 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3862 {
3863 Elf_Internal_Ehdr *ehdr;
3864 Elf_Internal_Shdr *hdr;
3865 size_t symcount;
3866 size_t extsymcount;
3867 size_t extsymoff;
3868 struct elf_link_hash_entry **sym_hash;
3869 bfd_boolean dynamic;
3870 Elf_External_Versym *extversym = NULL;
3871 Elf_External_Versym *ever;
3872 struct elf_link_hash_entry *weaks;
3873 struct elf_link_hash_entry **nondeflt_vers = NULL;
3874 size_t nondeflt_vers_cnt = 0;
3875 Elf_Internal_Sym *isymbuf = NULL;
3876 Elf_Internal_Sym *isym;
3877 Elf_Internal_Sym *isymend;
3878 const struct elf_backend_data *bed;
3879 bfd_boolean add_needed;
3880 struct elf_link_hash_table *htab;
3881 bfd_size_type amt;
3882 void *alloc_mark = NULL;
3883 struct bfd_hash_entry **old_table = NULL;
3884 unsigned int old_size = 0;
3885 unsigned int old_count = 0;
3886 void *old_tab = NULL;
3887 void *old_ent;
3888 struct bfd_link_hash_entry *old_undefs = NULL;
3889 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3890 void *old_strtab = NULL;
3891 size_t tabsize = 0;
3892 asection *s;
3893 bfd_boolean just_syms;
3894
3895 htab = elf_hash_table (info);
3896 bed = get_elf_backend_data (abfd);
3897
3898 if ((abfd->flags & DYNAMIC) == 0)
3899 dynamic = FALSE;
3900 else
3901 {
3902 dynamic = TRUE;
3903
3904 /* You can't use -r against a dynamic object. Also, there's no
3905 hope of using a dynamic object which does not exactly match
3906 the format of the output file. */
3907 if (bfd_link_relocatable (info)
3908 || !is_elf_hash_table (htab)
3909 || info->output_bfd->xvec != abfd->xvec)
3910 {
3911 if (bfd_link_relocatable (info))
3912 bfd_set_error (bfd_error_invalid_operation);
3913 else
3914 bfd_set_error (bfd_error_wrong_format);
3915 goto error_return;
3916 }
3917 }
3918
3919 ehdr = elf_elfheader (abfd);
3920 if (info->warn_alternate_em
3921 && bed->elf_machine_code != ehdr->e_machine
3922 && ((bed->elf_machine_alt1 != 0
3923 && ehdr->e_machine == bed->elf_machine_alt1)
3924 || (bed->elf_machine_alt2 != 0
3925 && ehdr->e_machine == bed->elf_machine_alt2)))
3926 _bfd_error_handler
3927 /* xgettext:c-format */
3928 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3929 ehdr->e_machine, abfd, bed->elf_machine_code);
3930
3931 /* As a GNU extension, any input sections which are named
3932 .gnu.warning.SYMBOL are treated as warning symbols for the given
3933 symbol. This differs from .gnu.warning sections, which generate
3934 warnings when they are included in an output file. */
3935 /* PR 12761: Also generate this warning when building shared libraries. */
3936 for (s = abfd->sections; s != NULL; s = s->next)
3937 {
3938 const char *name;
3939
3940 name = bfd_get_section_name (abfd, s);
3941 if (CONST_STRNEQ (name, ".gnu.warning."))
3942 {
3943 char *msg;
3944 bfd_size_type sz;
3945
3946 name += sizeof ".gnu.warning." - 1;
3947
3948 /* If this is a shared object, then look up the symbol
3949 in the hash table. If it is there, and it is already
3950 been defined, then we will not be using the entry
3951 from this shared object, so we don't need to warn.
3952 FIXME: If we see the definition in a regular object
3953 later on, we will warn, but we shouldn't. The only
3954 fix is to keep track of what warnings we are supposed
3955 to emit, and then handle them all at the end of the
3956 link. */
3957 if (dynamic)
3958 {
3959 struct elf_link_hash_entry *h;
3960
3961 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3962
3963 /* FIXME: What about bfd_link_hash_common? */
3964 if (h != NULL
3965 && (h->root.type == bfd_link_hash_defined
3966 || h->root.type == bfd_link_hash_defweak))
3967 continue;
3968 }
3969
3970 sz = s->size;
3971 msg = (char *) bfd_alloc (abfd, sz + 1);
3972 if (msg == NULL)
3973 goto error_return;
3974
3975 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3976 goto error_return;
3977
3978 msg[sz] = '\0';
3979
3980 if (! (_bfd_generic_link_add_one_symbol
3981 (info, abfd, name, BSF_WARNING, s, 0, msg,
3982 FALSE, bed->collect, NULL)))
3983 goto error_return;
3984
3985 if (bfd_link_executable (info))
3986 {
3987 /* Clobber the section size so that the warning does
3988 not get copied into the output file. */
3989 s->size = 0;
3990
3991 /* Also set SEC_EXCLUDE, so that symbols defined in
3992 the warning section don't get copied to the output. */
3993 s->flags |= SEC_EXCLUDE;
3994 }
3995 }
3996 }
3997
3998 just_syms = ((s = abfd->sections) != NULL
3999 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4000
4001 add_needed = TRUE;
4002 if (! dynamic)
4003 {
4004 /* If we are creating a shared library, create all the dynamic
4005 sections immediately. We need to attach them to something,
4006 so we attach them to this BFD, provided it is the right
4007 format and is not from ld --just-symbols. Always create the
4008 dynamic sections for -E/--dynamic-list. FIXME: If there
4009 are no input BFD's of the same format as the output, we can't
4010 make a shared library. */
4011 if (!just_syms
4012 && (bfd_link_pic (info)
4013 || (!bfd_link_relocatable (info)
4014 && info->nointerp
4015 && (info->export_dynamic || info->dynamic)))
4016 && is_elf_hash_table (htab)
4017 && info->output_bfd->xvec == abfd->xvec
4018 && !htab->dynamic_sections_created)
4019 {
4020 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4021 goto error_return;
4022 }
4023 }
4024 else if (!is_elf_hash_table (htab))
4025 goto error_return;
4026 else
4027 {
4028 const char *soname = NULL;
4029 char *audit = NULL;
4030 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4031 const Elf_Internal_Phdr *phdr;
4032 int ret;
4033
4034 /* ld --just-symbols and dynamic objects don't mix very well.
4035 ld shouldn't allow it. */
4036 if (just_syms)
4037 abort ();
4038
4039 /* If this dynamic lib was specified on the command line with
4040 --as-needed in effect, then we don't want to add a DT_NEEDED
4041 tag unless the lib is actually used. Similary for libs brought
4042 in by another lib's DT_NEEDED. When --no-add-needed is used
4043 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4044 any dynamic library in DT_NEEDED tags in the dynamic lib at
4045 all. */
4046 add_needed = (elf_dyn_lib_class (abfd)
4047 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4048 | DYN_NO_NEEDED)) == 0;
4049
4050 s = bfd_get_section_by_name (abfd, ".dynamic");
4051 if (s != NULL)
4052 {
4053 bfd_byte *dynbuf;
4054 bfd_byte *extdyn;
4055 unsigned int elfsec;
4056 unsigned long shlink;
4057
4058 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4059 {
4060 error_free_dyn:
4061 free (dynbuf);
4062 goto error_return;
4063 }
4064
4065 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4066 if (elfsec == SHN_BAD)
4067 goto error_free_dyn;
4068 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4069
4070 for (extdyn = dynbuf;
4071 extdyn < dynbuf + s->size;
4072 extdyn += bed->s->sizeof_dyn)
4073 {
4074 Elf_Internal_Dyn dyn;
4075
4076 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4077 if (dyn.d_tag == DT_SONAME)
4078 {
4079 unsigned int tagv = dyn.d_un.d_val;
4080 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4081 if (soname == NULL)
4082 goto error_free_dyn;
4083 }
4084 if (dyn.d_tag == DT_NEEDED)
4085 {
4086 struct bfd_link_needed_list *n, **pn;
4087 char *fnm, *anm;
4088 unsigned int tagv = dyn.d_un.d_val;
4089
4090 amt = sizeof (struct bfd_link_needed_list);
4091 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4092 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4093 if (n == NULL || fnm == NULL)
4094 goto error_free_dyn;
4095 amt = strlen (fnm) + 1;
4096 anm = (char *) bfd_alloc (abfd, amt);
4097 if (anm == NULL)
4098 goto error_free_dyn;
4099 memcpy (anm, fnm, amt);
4100 n->name = anm;
4101 n->by = abfd;
4102 n->next = NULL;
4103 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4104 ;
4105 *pn = n;
4106 }
4107 if (dyn.d_tag == DT_RUNPATH)
4108 {
4109 struct bfd_link_needed_list *n, **pn;
4110 char *fnm, *anm;
4111 unsigned int tagv = dyn.d_un.d_val;
4112
4113 amt = sizeof (struct bfd_link_needed_list);
4114 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4115 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4116 if (n == NULL || fnm == NULL)
4117 goto error_free_dyn;
4118 amt = strlen (fnm) + 1;
4119 anm = (char *) bfd_alloc (abfd, amt);
4120 if (anm == NULL)
4121 goto error_free_dyn;
4122 memcpy (anm, fnm, amt);
4123 n->name = anm;
4124 n->by = abfd;
4125 n->next = NULL;
4126 for (pn = & runpath;
4127 *pn != NULL;
4128 pn = &(*pn)->next)
4129 ;
4130 *pn = n;
4131 }
4132 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4133 if (!runpath && dyn.d_tag == DT_RPATH)
4134 {
4135 struct bfd_link_needed_list *n, **pn;
4136 char *fnm, *anm;
4137 unsigned int tagv = dyn.d_un.d_val;
4138
4139 amt = sizeof (struct bfd_link_needed_list);
4140 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4141 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4142 if (n == NULL || fnm == NULL)
4143 goto error_free_dyn;
4144 amt = strlen (fnm) + 1;
4145 anm = (char *) bfd_alloc (abfd, amt);
4146 if (anm == NULL)
4147 goto error_free_dyn;
4148 memcpy (anm, fnm, amt);
4149 n->name = anm;
4150 n->by = abfd;
4151 n->next = NULL;
4152 for (pn = & rpath;
4153 *pn != NULL;
4154 pn = &(*pn)->next)
4155 ;
4156 *pn = n;
4157 }
4158 if (dyn.d_tag == DT_AUDIT)
4159 {
4160 unsigned int tagv = dyn.d_un.d_val;
4161 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4162 }
4163 }
4164
4165 free (dynbuf);
4166 }
4167
4168 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4169 frees all more recently bfd_alloc'd blocks as well. */
4170 if (runpath)
4171 rpath = runpath;
4172
4173 if (rpath)
4174 {
4175 struct bfd_link_needed_list **pn;
4176 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4177 ;
4178 *pn = rpath;
4179 }
4180
4181 /* If we have a PT_GNU_RELRO program header, mark as read-only
4182 all sections contained fully therein. This makes relro
4183 shared library sections appear as they will at run-time. */
4184 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4185 while (--phdr >= elf_tdata (abfd)->phdr)
4186 if (phdr->p_type == PT_GNU_RELRO)
4187 {
4188 for (s = abfd->sections; s != NULL; s = s->next)
4189 if ((s->flags & SEC_ALLOC) != 0
4190 && s->vma >= phdr->p_vaddr
4191 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4192 s->flags |= SEC_READONLY;
4193 break;
4194 }
4195
4196 /* We do not want to include any of the sections in a dynamic
4197 object in the output file. We hack by simply clobbering the
4198 list of sections in the BFD. This could be handled more
4199 cleanly by, say, a new section flag; the existing
4200 SEC_NEVER_LOAD flag is not the one we want, because that one
4201 still implies that the section takes up space in the output
4202 file. */
4203 bfd_section_list_clear (abfd);
4204
4205 /* Find the name to use in a DT_NEEDED entry that refers to this
4206 object. If the object has a DT_SONAME entry, we use it.
4207 Otherwise, if the generic linker stuck something in
4208 elf_dt_name, we use that. Otherwise, we just use the file
4209 name. */
4210 if (soname == NULL || *soname == '\0')
4211 {
4212 soname = elf_dt_name (abfd);
4213 if (soname == NULL || *soname == '\0')
4214 soname = bfd_get_filename (abfd);
4215 }
4216
4217 /* Save the SONAME because sometimes the linker emulation code
4218 will need to know it. */
4219 elf_dt_name (abfd) = soname;
4220
4221 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4222 if (ret < 0)
4223 goto error_return;
4224
4225 /* If we have already included this dynamic object in the
4226 link, just ignore it. There is no reason to include a
4227 particular dynamic object more than once. */
4228 if (ret > 0)
4229 return TRUE;
4230
4231 /* Save the DT_AUDIT entry for the linker emulation code. */
4232 elf_dt_audit (abfd) = audit;
4233 }
4234
4235 /* If this is a dynamic object, we always link against the .dynsym
4236 symbol table, not the .symtab symbol table. The dynamic linker
4237 will only see the .dynsym symbol table, so there is no reason to
4238 look at .symtab for a dynamic object. */
4239
4240 if (! dynamic || elf_dynsymtab (abfd) == 0)
4241 hdr = &elf_tdata (abfd)->symtab_hdr;
4242 else
4243 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4244
4245 symcount = hdr->sh_size / bed->s->sizeof_sym;
4246
4247 /* The sh_info field of the symtab header tells us where the
4248 external symbols start. We don't care about the local symbols at
4249 this point. */
4250 if (elf_bad_symtab (abfd))
4251 {
4252 extsymcount = symcount;
4253 extsymoff = 0;
4254 }
4255 else
4256 {
4257 extsymcount = symcount - hdr->sh_info;
4258 extsymoff = hdr->sh_info;
4259 }
4260
4261 sym_hash = elf_sym_hashes (abfd);
4262 if (extsymcount != 0)
4263 {
4264 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4265 NULL, NULL, NULL);
4266 if (isymbuf == NULL)
4267 goto error_return;
4268
4269 if (sym_hash == NULL)
4270 {
4271 /* We store a pointer to the hash table entry for each
4272 external symbol. */
4273 amt = extsymcount;
4274 amt *= sizeof (struct elf_link_hash_entry *);
4275 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4276 if (sym_hash == NULL)
4277 goto error_free_sym;
4278 elf_sym_hashes (abfd) = sym_hash;
4279 }
4280 }
4281
4282 if (dynamic)
4283 {
4284 /* Read in any version definitions. */
4285 if (!_bfd_elf_slurp_version_tables (abfd,
4286 info->default_imported_symver))
4287 goto error_free_sym;
4288
4289 /* Read in the symbol versions, but don't bother to convert them
4290 to internal format. */
4291 if (elf_dynversym (abfd) != 0)
4292 {
4293 Elf_Internal_Shdr *versymhdr;
4294
4295 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4296 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4297 if (extversym == NULL)
4298 goto error_free_sym;
4299 amt = versymhdr->sh_size;
4300 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4301 || bfd_bread (extversym, amt, abfd) != amt)
4302 goto error_free_vers;
4303 }
4304 }
4305
4306 /* If we are loading an as-needed shared lib, save the symbol table
4307 state before we start adding symbols. If the lib turns out
4308 to be unneeded, restore the state. */
4309 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4310 {
4311 unsigned int i;
4312 size_t entsize;
4313
4314 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4315 {
4316 struct bfd_hash_entry *p;
4317 struct elf_link_hash_entry *h;
4318
4319 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4320 {
4321 h = (struct elf_link_hash_entry *) p;
4322 entsize += htab->root.table.entsize;
4323 if (h->root.type == bfd_link_hash_warning)
4324 entsize += htab->root.table.entsize;
4325 }
4326 }
4327
4328 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4329 old_tab = bfd_malloc (tabsize + entsize);
4330 if (old_tab == NULL)
4331 goto error_free_vers;
4332
4333 /* Remember the current objalloc pointer, so that all mem for
4334 symbols added can later be reclaimed. */
4335 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4336 if (alloc_mark == NULL)
4337 goto error_free_vers;
4338
4339 /* Make a special call to the linker "notice" function to
4340 tell it that we are about to handle an as-needed lib. */
4341 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4342 goto error_free_vers;
4343
4344 /* Clone the symbol table. Remember some pointers into the
4345 symbol table, and dynamic symbol count. */
4346 old_ent = (char *) old_tab + tabsize;
4347 memcpy (old_tab, htab->root.table.table, tabsize);
4348 old_undefs = htab->root.undefs;
4349 old_undefs_tail = htab->root.undefs_tail;
4350 old_table = htab->root.table.table;
4351 old_size = htab->root.table.size;
4352 old_count = htab->root.table.count;
4353 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4354 if (old_strtab == NULL)
4355 goto error_free_vers;
4356
4357 for (i = 0; i < htab->root.table.size; i++)
4358 {
4359 struct bfd_hash_entry *p;
4360 struct elf_link_hash_entry *h;
4361
4362 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4363 {
4364 memcpy (old_ent, p, htab->root.table.entsize);
4365 old_ent = (char *) old_ent + htab->root.table.entsize;
4366 h = (struct elf_link_hash_entry *) p;
4367 if (h->root.type == bfd_link_hash_warning)
4368 {
4369 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4370 old_ent = (char *) old_ent + htab->root.table.entsize;
4371 }
4372 }
4373 }
4374 }
4375
4376 weaks = NULL;
4377 ever = extversym != NULL ? extversym + extsymoff : NULL;
4378 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4379 isym < isymend;
4380 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4381 {
4382 int bind;
4383 bfd_vma value;
4384 asection *sec, *new_sec;
4385 flagword flags;
4386 const char *name;
4387 struct elf_link_hash_entry *h;
4388 struct elf_link_hash_entry *hi;
4389 bfd_boolean definition;
4390 bfd_boolean size_change_ok;
4391 bfd_boolean type_change_ok;
4392 bfd_boolean new_weak;
4393 bfd_boolean old_weak;
4394 bfd_boolean override;
4395 bfd_boolean common;
4396 bfd_boolean discarded;
4397 unsigned int old_alignment;
4398 bfd *old_bfd;
4399 bfd_boolean matched;
4400
4401 override = FALSE;
4402
4403 flags = BSF_NO_FLAGS;
4404 sec = NULL;
4405 value = isym->st_value;
4406 common = bed->common_definition (isym);
4407 if (common && info->inhibit_common_definition)
4408 {
4409 /* Treat common symbol as undefined for --no-define-common. */
4410 isym->st_shndx = SHN_UNDEF;
4411 common = FALSE;
4412 }
4413 discarded = FALSE;
4414
4415 bind = ELF_ST_BIND (isym->st_info);
4416 switch (bind)
4417 {
4418 case STB_LOCAL:
4419 /* This should be impossible, since ELF requires that all
4420 global symbols follow all local symbols, and that sh_info
4421 point to the first global symbol. Unfortunately, Irix 5
4422 screws this up. */
4423 continue;
4424
4425 case STB_GLOBAL:
4426 if (isym->st_shndx != SHN_UNDEF && !common)
4427 flags = BSF_GLOBAL;
4428 break;
4429
4430 case STB_WEAK:
4431 flags = BSF_WEAK;
4432 break;
4433
4434 case STB_GNU_UNIQUE:
4435 flags = BSF_GNU_UNIQUE;
4436 break;
4437
4438 default:
4439 /* Leave it up to the processor backend. */
4440 break;
4441 }
4442
4443 if (isym->st_shndx == SHN_UNDEF)
4444 sec = bfd_und_section_ptr;
4445 else if (isym->st_shndx == SHN_ABS)
4446 sec = bfd_abs_section_ptr;
4447 else if (isym->st_shndx == SHN_COMMON)
4448 {
4449 sec = bfd_com_section_ptr;
4450 /* What ELF calls the size we call the value. What ELF
4451 calls the value we call the alignment. */
4452 value = isym->st_size;
4453 }
4454 else
4455 {
4456 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4457 if (sec == NULL)
4458 sec = bfd_abs_section_ptr;
4459 else if (discarded_section (sec))
4460 {
4461 /* Symbols from discarded section are undefined. We keep
4462 its visibility. */
4463 sec = bfd_und_section_ptr;
4464 discarded = TRUE;
4465 isym->st_shndx = SHN_UNDEF;
4466 }
4467 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4468 value -= sec->vma;
4469 }
4470
4471 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4472 isym->st_name);
4473 if (name == NULL)
4474 goto error_free_vers;
4475
4476 if (isym->st_shndx == SHN_COMMON
4477 && (abfd->flags & BFD_PLUGIN) != 0)
4478 {
4479 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4480
4481 if (xc == NULL)
4482 {
4483 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4484 | SEC_EXCLUDE);
4485 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4486 if (xc == NULL)
4487 goto error_free_vers;
4488 }
4489 sec = xc;
4490 }
4491 else if (isym->st_shndx == SHN_COMMON
4492 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4493 && !bfd_link_relocatable (info))
4494 {
4495 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4496
4497 if (tcomm == NULL)
4498 {
4499 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4500 | SEC_LINKER_CREATED);
4501 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4502 if (tcomm == NULL)
4503 goto error_free_vers;
4504 }
4505 sec = tcomm;
4506 }
4507 else if (bed->elf_add_symbol_hook)
4508 {
4509 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4510 &sec, &value))
4511 goto error_free_vers;
4512
4513 /* The hook function sets the name to NULL if this symbol
4514 should be skipped for some reason. */
4515 if (name == NULL)
4516 continue;
4517 }
4518
4519 /* Sanity check that all possibilities were handled. */
4520 if (sec == NULL)
4521 {
4522 bfd_set_error (bfd_error_bad_value);
4523 goto error_free_vers;
4524 }
4525
4526 /* Silently discard TLS symbols from --just-syms. There's
4527 no way to combine a static TLS block with a new TLS block
4528 for this executable. */
4529 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4530 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4531 continue;
4532
4533 if (bfd_is_und_section (sec)
4534 || bfd_is_com_section (sec))
4535 definition = FALSE;
4536 else
4537 definition = TRUE;
4538
4539 size_change_ok = FALSE;
4540 type_change_ok = bed->type_change_ok;
4541 old_weak = FALSE;
4542 matched = FALSE;
4543 old_alignment = 0;
4544 old_bfd = NULL;
4545 new_sec = sec;
4546
4547 if (is_elf_hash_table (htab))
4548 {
4549 Elf_Internal_Versym iver;
4550 unsigned int vernum = 0;
4551 bfd_boolean skip;
4552
4553 if (ever == NULL)
4554 {
4555 if (info->default_imported_symver)
4556 /* Use the default symbol version created earlier. */
4557 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4558 else
4559 iver.vs_vers = 0;
4560 }
4561 else
4562 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4563
4564 vernum = iver.vs_vers & VERSYM_VERSION;
4565
4566 /* If this is a hidden symbol, or if it is not version
4567 1, we append the version name to the symbol name.
4568 However, we do not modify a non-hidden absolute symbol
4569 if it is not a function, because it might be the version
4570 symbol itself. FIXME: What if it isn't? */
4571 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4572 || (vernum > 1
4573 && (!bfd_is_abs_section (sec)
4574 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4575 {
4576 const char *verstr;
4577 size_t namelen, verlen, newlen;
4578 char *newname, *p;
4579
4580 if (isym->st_shndx != SHN_UNDEF)
4581 {
4582 if (vernum > elf_tdata (abfd)->cverdefs)
4583 verstr = NULL;
4584 else if (vernum > 1)
4585 verstr =
4586 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4587 else
4588 verstr = "";
4589
4590 if (verstr == NULL)
4591 {
4592 _bfd_error_handler
4593 /* xgettext:c-format */
4594 (_("%pB: %s: invalid version %u (max %d)"),
4595 abfd, name, vernum,
4596 elf_tdata (abfd)->cverdefs);
4597 bfd_set_error (bfd_error_bad_value);
4598 goto error_free_vers;
4599 }
4600 }
4601 else
4602 {
4603 /* We cannot simply test for the number of
4604 entries in the VERNEED section since the
4605 numbers for the needed versions do not start
4606 at 0. */
4607 Elf_Internal_Verneed *t;
4608
4609 verstr = NULL;
4610 for (t = elf_tdata (abfd)->verref;
4611 t != NULL;
4612 t = t->vn_nextref)
4613 {
4614 Elf_Internal_Vernaux *a;
4615
4616 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4617 {
4618 if (a->vna_other == vernum)
4619 {
4620 verstr = a->vna_nodename;
4621 break;
4622 }
4623 }
4624 if (a != NULL)
4625 break;
4626 }
4627 if (verstr == NULL)
4628 {
4629 _bfd_error_handler
4630 /* xgettext:c-format */
4631 (_("%pB: %s: invalid needed version %d"),
4632 abfd, name, vernum);
4633 bfd_set_error (bfd_error_bad_value);
4634 goto error_free_vers;
4635 }
4636 }
4637
4638 namelen = strlen (name);
4639 verlen = strlen (verstr);
4640 newlen = namelen + verlen + 2;
4641 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4642 && isym->st_shndx != SHN_UNDEF)
4643 ++newlen;
4644
4645 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4646 if (newname == NULL)
4647 goto error_free_vers;
4648 memcpy (newname, name, namelen);
4649 p = newname + namelen;
4650 *p++ = ELF_VER_CHR;
4651 /* If this is a defined non-hidden version symbol,
4652 we add another @ to the name. This indicates the
4653 default version of the symbol. */
4654 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4655 && isym->st_shndx != SHN_UNDEF)
4656 *p++ = ELF_VER_CHR;
4657 memcpy (p, verstr, verlen + 1);
4658
4659 name = newname;
4660 }
4661
4662 /* If this symbol has default visibility and the user has
4663 requested we not re-export it, then mark it as hidden. */
4664 if (!bfd_is_und_section (sec)
4665 && !dynamic
4666 && abfd->no_export
4667 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4668 isym->st_other = (STV_HIDDEN
4669 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4670
4671 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4672 sym_hash, &old_bfd, &old_weak,
4673 &old_alignment, &skip, &override,
4674 &type_change_ok, &size_change_ok,
4675 &matched))
4676 goto error_free_vers;
4677
4678 if (skip)
4679 continue;
4680
4681 /* Override a definition only if the new symbol matches the
4682 existing one. */
4683 if (override && matched)
4684 definition = FALSE;
4685
4686 h = *sym_hash;
4687 while (h->root.type == bfd_link_hash_indirect
4688 || h->root.type == bfd_link_hash_warning)
4689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4690
4691 if (elf_tdata (abfd)->verdef != NULL
4692 && vernum > 1
4693 && definition)
4694 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4695 }
4696
4697 if (! (_bfd_generic_link_add_one_symbol
4698 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4699 (struct bfd_link_hash_entry **) sym_hash)))
4700 goto error_free_vers;
4701
4702 if ((abfd->flags & DYNAMIC) == 0
4703 && (bfd_get_flavour (info->output_bfd)
4704 == bfd_target_elf_flavour))
4705 {
4706 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4707 elf_tdata (info->output_bfd)->has_gnu_symbols
4708 |= elf_gnu_symbol_ifunc;
4709 if ((flags & BSF_GNU_UNIQUE))
4710 elf_tdata (info->output_bfd)->has_gnu_symbols
4711 |= elf_gnu_symbol_unique;
4712 }
4713
4714 h = *sym_hash;
4715 /* We need to make sure that indirect symbol dynamic flags are
4716 updated. */
4717 hi = h;
4718 while (h->root.type == bfd_link_hash_indirect
4719 || h->root.type == bfd_link_hash_warning)
4720 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4721
4722 /* Setting the index to -3 tells elf_link_output_extsym that
4723 this symbol is defined in a discarded section. */
4724 if (discarded)
4725 h->indx = -3;
4726
4727 *sym_hash = h;
4728
4729 new_weak = (flags & BSF_WEAK) != 0;
4730 if (dynamic
4731 && definition
4732 && new_weak
4733 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4734 && is_elf_hash_table (htab)
4735 && h->u.alias == NULL)
4736 {
4737 /* Keep a list of all weak defined non function symbols from
4738 a dynamic object, using the alias field. Later in this
4739 function we will set the alias field to the correct
4740 value. We only put non-function symbols from dynamic
4741 objects on this list, because that happens to be the only
4742 time we need to know the normal symbol corresponding to a
4743 weak symbol, and the information is time consuming to
4744 figure out. If the alias field is not already NULL,
4745 then this symbol was already defined by some previous
4746 dynamic object, and we will be using that previous
4747 definition anyhow. */
4748
4749 h->u.alias = weaks;
4750 weaks = h;
4751 }
4752
4753 /* Set the alignment of a common symbol. */
4754 if ((common || bfd_is_com_section (sec))
4755 && h->root.type == bfd_link_hash_common)
4756 {
4757 unsigned int align;
4758
4759 if (common)
4760 align = bfd_log2 (isym->st_value);
4761 else
4762 {
4763 /* The new symbol is a common symbol in a shared object.
4764 We need to get the alignment from the section. */
4765 align = new_sec->alignment_power;
4766 }
4767 if (align > old_alignment)
4768 h->root.u.c.p->alignment_power = align;
4769 else
4770 h->root.u.c.p->alignment_power = old_alignment;
4771 }
4772
4773 if (is_elf_hash_table (htab))
4774 {
4775 /* Set a flag in the hash table entry indicating the type of
4776 reference or definition we just found. A dynamic symbol
4777 is one which is referenced or defined by both a regular
4778 object and a shared object. */
4779 bfd_boolean dynsym = FALSE;
4780
4781 /* Plugin symbols aren't normal. Don't set def_regular or
4782 ref_regular for them, or make them dynamic. */
4783 if ((abfd->flags & BFD_PLUGIN) != 0)
4784 ;
4785 else if (! dynamic)
4786 {
4787 if (! definition)
4788 {
4789 h->ref_regular = 1;
4790 if (bind != STB_WEAK)
4791 h->ref_regular_nonweak = 1;
4792 }
4793 else
4794 {
4795 h->def_regular = 1;
4796 if (h->def_dynamic)
4797 {
4798 h->def_dynamic = 0;
4799 h->ref_dynamic = 1;
4800 }
4801 }
4802
4803 /* If the indirect symbol has been forced local, don't
4804 make the real symbol dynamic. */
4805 if ((h == hi || !hi->forced_local)
4806 && (bfd_link_dll (info)
4807 || h->def_dynamic
4808 || h->ref_dynamic))
4809 dynsym = TRUE;
4810 }
4811 else
4812 {
4813 if (! definition)
4814 {
4815 h->ref_dynamic = 1;
4816 hi->ref_dynamic = 1;
4817 }
4818 else
4819 {
4820 h->def_dynamic = 1;
4821 hi->def_dynamic = 1;
4822 }
4823
4824 /* If the indirect symbol has been forced local, don't
4825 make the real symbol dynamic. */
4826 if ((h == hi || !hi->forced_local)
4827 && (h->def_regular
4828 || h->ref_regular
4829 || (h->is_weakalias
4830 && weakdef (h)->dynindx != -1)))
4831 dynsym = TRUE;
4832 }
4833
4834 /* Check to see if we need to add an indirect symbol for
4835 the default name. */
4836 if (definition
4837 || (!override && h->root.type == bfd_link_hash_common))
4838 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4839 sec, value, &old_bfd, &dynsym))
4840 goto error_free_vers;
4841
4842 /* Check the alignment when a common symbol is involved. This
4843 can change when a common symbol is overridden by a normal
4844 definition or a common symbol is ignored due to the old
4845 normal definition. We need to make sure the maximum
4846 alignment is maintained. */
4847 if ((old_alignment || common)
4848 && h->root.type != bfd_link_hash_common)
4849 {
4850 unsigned int common_align;
4851 unsigned int normal_align;
4852 unsigned int symbol_align;
4853 bfd *normal_bfd;
4854 bfd *common_bfd;
4855
4856 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4857 || h->root.type == bfd_link_hash_defweak);
4858
4859 symbol_align = ffs (h->root.u.def.value) - 1;
4860 if (h->root.u.def.section->owner != NULL
4861 && (h->root.u.def.section->owner->flags
4862 & (DYNAMIC | BFD_PLUGIN)) == 0)
4863 {
4864 normal_align = h->root.u.def.section->alignment_power;
4865 if (normal_align > symbol_align)
4866 normal_align = symbol_align;
4867 }
4868 else
4869 normal_align = symbol_align;
4870
4871 if (old_alignment)
4872 {
4873 common_align = old_alignment;
4874 common_bfd = old_bfd;
4875 normal_bfd = abfd;
4876 }
4877 else
4878 {
4879 common_align = bfd_log2 (isym->st_value);
4880 common_bfd = abfd;
4881 normal_bfd = old_bfd;
4882 }
4883
4884 if (normal_align < common_align)
4885 {
4886 /* PR binutils/2735 */
4887 if (normal_bfd == NULL)
4888 _bfd_error_handler
4889 /* xgettext:c-format */
4890 (_("warning: alignment %u of common symbol `%s' in %pB is"
4891 " greater than the alignment (%u) of its section %pA"),
4892 1 << common_align, name, common_bfd,
4893 1 << normal_align, h->root.u.def.section);
4894 else
4895 _bfd_error_handler
4896 /* xgettext:c-format */
4897 (_("warning: alignment %u of symbol `%s' in %pB"
4898 " is smaller than %u in %pB"),
4899 1 << normal_align, name, normal_bfd,
4900 1 << common_align, common_bfd);
4901 }
4902 }
4903
4904 /* Remember the symbol size if it isn't undefined. */
4905 if (isym->st_size != 0
4906 && isym->st_shndx != SHN_UNDEF
4907 && (definition || h->size == 0))
4908 {
4909 if (h->size != 0
4910 && h->size != isym->st_size
4911 && ! size_change_ok)
4912 _bfd_error_handler
4913 /* xgettext:c-format */
4914 (_("warning: size of symbol `%s' changed"
4915 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4916 name, (uint64_t) h->size, old_bfd,
4917 (uint64_t) isym->st_size, abfd);
4918
4919 h->size = isym->st_size;
4920 }
4921
4922 /* If this is a common symbol, then we always want H->SIZE
4923 to be the size of the common symbol. The code just above
4924 won't fix the size if a common symbol becomes larger. We
4925 don't warn about a size change here, because that is
4926 covered by --warn-common. Allow changes between different
4927 function types. */
4928 if (h->root.type == bfd_link_hash_common)
4929 h->size = h->root.u.c.size;
4930
4931 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4932 && ((definition && !new_weak)
4933 || (old_weak && h->root.type == bfd_link_hash_common)
4934 || h->type == STT_NOTYPE))
4935 {
4936 unsigned int type = ELF_ST_TYPE (isym->st_info);
4937
4938 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4939 symbol. */
4940 if (type == STT_GNU_IFUNC
4941 && (abfd->flags & DYNAMIC) != 0)
4942 type = STT_FUNC;
4943
4944 if (h->type != type)
4945 {
4946 if (h->type != STT_NOTYPE && ! type_change_ok)
4947 /* xgettext:c-format */
4948 _bfd_error_handler
4949 (_("warning: type of symbol `%s' changed"
4950 " from %d to %d in %pB"),
4951 name, h->type, type, abfd);
4952
4953 h->type = type;
4954 }
4955 }
4956
4957 /* Merge st_other field. */
4958 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4959
4960 /* We don't want to make debug symbol dynamic. */
4961 if (definition
4962 && (sec->flags & SEC_DEBUGGING)
4963 && !bfd_link_relocatable (info))
4964 dynsym = FALSE;
4965
4966 /* Nor should we make plugin symbols dynamic. */
4967 if ((abfd->flags & BFD_PLUGIN) != 0)
4968 dynsym = FALSE;
4969
4970 if (definition)
4971 {
4972 h->target_internal = isym->st_target_internal;
4973 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4974 }
4975
4976 if (definition && !dynamic)
4977 {
4978 char *p = strchr (name, ELF_VER_CHR);
4979 if (p != NULL && p[1] != ELF_VER_CHR)
4980 {
4981 /* Queue non-default versions so that .symver x, x@FOO
4982 aliases can be checked. */
4983 if (!nondeflt_vers)
4984 {
4985 amt = ((isymend - isym + 1)
4986 * sizeof (struct elf_link_hash_entry *));
4987 nondeflt_vers
4988 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4989 if (!nondeflt_vers)
4990 goto error_free_vers;
4991 }
4992 nondeflt_vers[nondeflt_vers_cnt++] = h;
4993 }
4994 }
4995
4996 if (dynsym && h->dynindx == -1)
4997 {
4998 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4999 goto error_free_vers;
5000 if (h->is_weakalias
5001 && weakdef (h)->dynindx == -1)
5002 {
5003 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5004 goto error_free_vers;
5005 }
5006 }
5007 else if (h->dynindx != -1)
5008 /* If the symbol already has a dynamic index, but
5009 visibility says it should not be visible, turn it into
5010 a local symbol. */
5011 switch (ELF_ST_VISIBILITY (h->other))
5012 {
5013 case STV_INTERNAL:
5014 case STV_HIDDEN:
5015 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5016 dynsym = FALSE;
5017 break;
5018 }
5019
5020 /* Don't add DT_NEEDED for references from the dummy bfd nor
5021 for unmatched symbol. */
5022 if (!add_needed
5023 && matched
5024 && definition
5025 && ((dynsym
5026 && h->ref_regular_nonweak
5027 && (old_bfd == NULL
5028 || (old_bfd->flags & BFD_PLUGIN) == 0))
5029 || (h->ref_dynamic_nonweak
5030 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5031 && !on_needed_list (elf_dt_name (abfd),
5032 htab->needed, NULL))))
5033 {
5034 int ret;
5035 const char *soname = elf_dt_name (abfd);
5036
5037 info->callbacks->minfo ("%!", soname, old_bfd,
5038 h->root.root.string);
5039
5040 /* A symbol from a library loaded via DT_NEEDED of some
5041 other library is referenced by a regular object.
5042 Add a DT_NEEDED entry for it. Issue an error if
5043 --no-add-needed is used and the reference was not
5044 a weak one. */
5045 if (old_bfd != NULL
5046 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5047 {
5048 _bfd_error_handler
5049 /* xgettext:c-format */
5050 (_("%pB: undefined reference to symbol '%s'"),
5051 old_bfd, name);
5052 bfd_set_error (bfd_error_missing_dso);
5053 goto error_free_vers;
5054 }
5055
5056 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5057 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5058
5059 add_needed = TRUE;
5060 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5061 if (ret < 0)
5062 goto error_free_vers;
5063
5064 BFD_ASSERT (ret == 0);
5065 }
5066 }
5067 }
5068
5069 if (info->lto_plugin_active
5070 && !bfd_link_relocatable (info)
5071 && (abfd->flags & BFD_PLUGIN) == 0
5072 && !just_syms
5073 && extsymcount)
5074 {
5075 int r_sym_shift;
5076
5077 if (bed->s->arch_size == 32)
5078 r_sym_shift = 8;
5079 else
5080 r_sym_shift = 32;
5081
5082 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5083 referenced in regular objects so that linker plugin will get
5084 the correct symbol resolution. */
5085
5086 sym_hash = elf_sym_hashes (abfd);
5087 for (s = abfd->sections; s != NULL; s = s->next)
5088 {
5089 Elf_Internal_Rela *internal_relocs;
5090 Elf_Internal_Rela *rel, *relend;
5091
5092 /* Don't check relocations in excluded sections. */
5093 if ((s->flags & SEC_RELOC) == 0
5094 || s->reloc_count == 0
5095 || (s->flags & SEC_EXCLUDE) != 0
5096 || ((info->strip == strip_all
5097 || info->strip == strip_debugger)
5098 && (s->flags & SEC_DEBUGGING) != 0))
5099 continue;
5100
5101 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5102 NULL,
5103 info->keep_memory);
5104 if (internal_relocs == NULL)
5105 goto error_free_vers;
5106
5107 rel = internal_relocs;
5108 relend = rel + s->reloc_count;
5109 for ( ; rel < relend; rel++)
5110 {
5111 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5112 struct elf_link_hash_entry *h;
5113
5114 /* Skip local symbols. */
5115 if (r_symndx < extsymoff)
5116 continue;
5117
5118 h = sym_hash[r_symndx - extsymoff];
5119 if (h != NULL)
5120 h->root.non_ir_ref_regular = 1;
5121 }
5122
5123 if (elf_section_data (s)->relocs != internal_relocs)
5124 free (internal_relocs);
5125 }
5126 }
5127
5128 if (extversym != NULL)
5129 {
5130 free (extversym);
5131 extversym = NULL;
5132 }
5133
5134 if (isymbuf != NULL)
5135 {
5136 free (isymbuf);
5137 isymbuf = NULL;
5138 }
5139
5140 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5141 {
5142 unsigned int i;
5143
5144 /* Restore the symbol table. */
5145 old_ent = (char *) old_tab + tabsize;
5146 memset (elf_sym_hashes (abfd), 0,
5147 extsymcount * sizeof (struct elf_link_hash_entry *));
5148 htab->root.table.table = old_table;
5149 htab->root.table.size = old_size;
5150 htab->root.table.count = old_count;
5151 memcpy (htab->root.table.table, old_tab, tabsize);
5152 htab->root.undefs = old_undefs;
5153 htab->root.undefs_tail = old_undefs_tail;
5154 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5155 free (old_strtab);
5156 old_strtab = NULL;
5157 for (i = 0; i < htab->root.table.size; i++)
5158 {
5159 struct bfd_hash_entry *p;
5160 struct elf_link_hash_entry *h;
5161 bfd_size_type size;
5162 unsigned int alignment_power;
5163 unsigned int non_ir_ref_dynamic;
5164
5165 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5166 {
5167 h = (struct elf_link_hash_entry *) p;
5168 if (h->root.type == bfd_link_hash_warning)
5169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5170
5171 /* Preserve the maximum alignment and size for common
5172 symbols even if this dynamic lib isn't on DT_NEEDED
5173 since it can still be loaded at run time by another
5174 dynamic lib. */
5175 if (h->root.type == bfd_link_hash_common)
5176 {
5177 size = h->root.u.c.size;
5178 alignment_power = h->root.u.c.p->alignment_power;
5179 }
5180 else
5181 {
5182 size = 0;
5183 alignment_power = 0;
5184 }
5185 /* Preserve non_ir_ref_dynamic so that this symbol
5186 will be exported when the dynamic lib becomes needed
5187 in the second pass. */
5188 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5189 memcpy (p, old_ent, htab->root.table.entsize);
5190 old_ent = (char *) old_ent + htab->root.table.entsize;
5191 h = (struct elf_link_hash_entry *) p;
5192 if (h->root.type == bfd_link_hash_warning)
5193 {
5194 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5195 old_ent = (char *) old_ent + htab->root.table.entsize;
5196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5197 }
5198 if (h->root.type == bfd_link_hash_common)
5199 {
5200 if (size > h->root.u.c.size)
5201 h->root.u.c.size = size;
5202 if (alignment_power > h->root.u.c.p->alignment_power)
5203 h->root.u.c.p->alignment_power = alignment_power;
5204 }
5205 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5206 }
5207 }
5208
5209 /* Make a special call to the linker "notice" function to
5210 tell it that symbols added for crefs may need to be removed. */
5211 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5212 goto error_free_vers;
5213
5214 free (old_tab);
5215 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5216 alloc_mark);
5217 if (nondeflt_vers != NULL)
5218 free (nondeflt_vers);
5219 return TRUE;
5220 }
5221
5222 if (old_tab != NULL)
5223 {
5224 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5225 goto error_free_vers;
5226 free (old_tab);
5227 old_tab = NULL;
5228 }
5229
5230 /* Now that all the symbols from this input file are created, if
5231 not performing a relocatable link, handle .symver foo, foo@BAR
5232 such that any relocs against foo become foo@BAR. */
5233 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5234 {
5235 size_t cnt, symidx;
5236
5237 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5238 {
5239 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5240 char *shortname, *p;
5241
5242 p = strchr (h->root.root.string, ELF_VER_CHR);
5243 if (p == NULL
5244 || (h->root.type != bfd_link_hash_defined
5245 && h->root.type != bfd_link_hash_defweak))
5246 continue;
5247
5248 amt = p - h->root.root.string;
5249 shortname = (char *) bfd_malloc (amt + 1);
5250 if (!shortname)
5251 goto error_free_vers;
5252 memcpy (shortname, h->root.root.string, amt);
5253 shortname[amt] = '\0';
5254
5255 hi = (struct elf_link_hash_entry *)
5256 bfd_link_hash_lookup (&htab->root, shortname,
5257 FALSE, FALSE, FALSE);
5258 if (hi != NULL
5259 && hi->root.type == h->root.type
5260 && hi->root.u.def.value == h->root.u.def.value
5261 && hi->root.u.def.section == h->root.u.def.section)
5262 {
5263 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5264 hi->root.type = bfd_link_hash_indirect;
5265 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5266 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5267 sym_hash = elf_sym_hashes (abfd);
5268 if (sym_hash)
5269 for (symidx = 0; symidx < extsymcount; ++symidx)
5270 if (sym_hash[symidx] == hi)
5271 {
5272 sym_hash[symidx] = h;
5273 break;
5274 }
5275 }
5276 free (shortname);
5277 }
5278 free (nondeflt_vers);
5279 nondeflt_vers = NULL;
5280 }
5281
5282 /* Now set the alias field correctly for all the weak defined
5283 symbols we found. The only way to do this is to search all the
5284 symbols. Since we only need the information for non functions in
5285 dynamic objects, that's the only time we actually put anything on
5286 the list WEAKS. We need this information so that if a regular
5287 object refers to a symbol defined weakly in a dynamic object, the
5288 real symbol in the dynamic object is also put in the dynamic
5289 symbols; we also must arrange for both symbols to point to the
5290 same memory location. We could handle the general case of symbol
5291 aliasing, but a general symbol alias can only be generated in
5292 assembler code, handling it correctly would be very time
5293 consuming, and other ELF linkers don't handle general aliasing
5294 either. */
5295 if (weaks != NULL)
5296 {
5297 struct elf_link_hash_entry **hpp;
5298 struct elf_link_hash_entry **hppend;
5299 struct elf_link_hash_entry **sorted_sym_hash;
5300 struct elf_link_hash_entry *h;
5301 size_t sym_count;
5302
5303 /* Since we have to search the whole symbol list for each weak
5304 defined symbol, search time for N weak defined symbols will be
5305 O(N^2). Binary search will cut it down to O(NlogN). */
5306 amt = extsymcount;
5307 amt *= sizeof (struct elf_link_hash_entry *);
5308 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5309 if (sorted_sym_hash == NULL)
5310 goto error_return;
5311 sym_hash = sorted_sym_hash;
5312 hpp = elf_sym_hashes (abfd);
5313 hppend = hpp + extsymcount;
5314 sym_count = 0;
5315 for (; hpp < hppend; hpp++)
5316 {
5317 h = *hpp;
5318 if (h != NULL
5319 && h->root.type == bfd_link_hash_defined
5320 && !bed->is_function_type (h->type))
5321 {
5322 *sym_hash = h;
5323 sym_hash++;
5324 sym_count++;
5325 }
5326 }
5327
5328 qsort (sorted_sym_hash, sym_count,
5329 sizeof (struct elf_link_hash_entry *),
5330 elf_sort_symbol);
5331
5332 while (weaks != NULL)
5333 {
5334 struct elf_link_hash_entry *hlook;
5335 asection *slook;
5336 bfd_vma vlook;
5337 size_t i, j, idx = 0;
5338
5339 hlook = weaks;
5340 weaks = hlook->u.alias;
5341 hlook->u.alias = NULL;
5342
5343 if (hlook->root.type != bfd_link_hash_defined
5344 && hlook->root.type != bfd_link_hash_defweak)
5345 continue;
5346
5347 slook = hlook->root.u.def.section;
5348 vlook = hlook->root.u.def.value;
5349
5350 i = 0;
5351 j = sym_count;
5352 while (i != j)
5353 {
5354 bfd_signed_vma vdiff;
5355 idx = (i + j) / 2;
5356 h = sorted_sym_hash[idx];
5357 vdiff = vlook - h->root.u.def.value;
5358 if (vdiff < 0)
5359 j = idx;
5360 else if (vdiff > 0)
5361 i = idx + 1;
5362 else
5363 {
5364 int sdiff = slook->id - h->root.u.def.section->id;
5365 if (sdiff < 0)
5366 j = idx;
5367 else if (sdiff > 0)
5368 i = idx + 1;
5369 else
5370 break;
5371 }
5372 }
5373
5374 /* We didn't find a value/section match. */
5375 if (i == j)
5376 continue;
5377
5378 /* With multiple aliases, or when the weak symbol is already
5379 strongly defined, we have multiple matching symbols and
5380 the binary search above may land on any of them. Step
5381 one past the matching symbol(s). */
5382 while (++idx != j)
5383 {
5384 h = sorted_sym_hash[idx];
5385 if (h->root.u.def.section != slook
5386 || h->root.u.def.value != vlook)
5387 break;
5388 }
5389
5390 /* Now look back over the aliases. Since we sorted by size
5391 as well as value and section, we'll choose the one with
5392 the largest size. */
5393 while (idx-- != i)
5394 {
5395 h = sorted_sym_hash[idx];
5396
5397 /* Stop if value or section doesn't match. */
5398 if (h->root.u.def.section != slook
5399 || h->root.u.def.value != vlook)
5400 break;
5401 else if (h != hlook)
5402 {
5403 struct elf_link_hash_entry *t;
5404
5405 hlook->u.alias = h;
5406 hlook->is_weakalias = 1;
5407 t = h;
5408 if (t->u.alias != NULL)
5409 while (t->u.alias != h)
5410 t = t->u.alias;
5411 t->u.alias = hlook;
5412
5413 /* If the weak definition is in the list of dynamic
5414 symbols, make sure the real definition is put
5415 there as well. */
5416 if (hlook->dynindx != -1 && h->dynindx == -1)
5417 {
5418 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5419 {
5420 err_free_sym_hash:
5421 free (sorted_sym_hash);
5422 goto error_return;
5423 }
5424 }
5425
5426 /* If the real definition is in the list of dynamic
5427 symbols, make sure the weak definition is put
5428 there as well. If we don't do this, then the
5429 dynamic loader might not merge the entries for the
5430 real definition and the weak definition. */
5431 if (h->dynindx != -1 && hlook->dynindx == -1)
5432 {
5433 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5434 goto err_free_sym_hash;
5435 }
5436 break;
5437 }
5438 }
5439 }
5440
5441 free (sorted_sym_hash);
5442 }
5443
5444 if (bed->check_directives
5445 && !(*bed->check_directives) (abfd, info))
5446 return FALSE;
5447
5448 /* If this is a non-traditional link, try to optimize the handling
5449 of the .stab/.stabstr sections. */
5450 if (! dynamic
5451 && ! info->traditional_format
5452 && is_elf_hash_table (htab)
5453 && (info->strip != strip_all && info->strip != strip_debugger))
5454 {
5455 asection *stabstr;
5456
5457 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5458 if (stabstr != NULL)
5459 {
5460 bfd_size_type string_offset = 0;
5461 asection *stab;
5462
5463 for (stab = abfd->sections; stab; stab = stab->next)
5464 if (CONST_STRNEQ (stab->name, ".stab")
5465 && (!stab->name[5] ||
5466 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5467 && (stab->flags & SEC_MERGE) == 0
5468 && !bfd_is_abs_section (stab->output_section))
5469 {
5470 struct bfd_elf_section_data *secdata;
5471
5472 secdata = elf_section_data (stab);
5473 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5474 stabstr, &secdata->sec_info,
5475 &string_offset))
5476 goto error_return;
5477 if (secdata->sec_info)
5478 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5479 }
5480 }
5481 }
5482
5483 if (is_elf_hash_table (htab) && add_needed)
5484 {
5485 /* Add this bfd to the loaded list. */
5486 struct elf_link_loaded_list *n;
5487
5488 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5489 if (n == NULL)
5490 goto error_return;
5491 n->abfd = abfd;
5492 n->next = htab->loaded;
5493 htab->loaded = n;
5494 }
5495
5496 return TRUE;
5497
5498 error_free_vers:
5499 if (old_tab != NULL)
5500 free (old_tab);
5501 if (old_strtab != NULL)
5502 free (old_strtab);
5503 if (nondeflt_vers != NULL)
5504 free (nondeflt_vers);
5505 if (extversym != NULL)
5506 free (extversym);
5507 error_free_sym:
5508 if (isymbuf != NULL)
5509 free (isymbuf);
5510 error_return:
5511 return FALSE;
5512 }
5513
5514 /* Return the linker hash table entry of a symbol that might be
5515 satisfied by an archive symbol. Return -1 on error. */
5516
5517 struct elf_link_hash_entry *
5518 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5519 struct bfd_link_info *info,
5520 const char *name)
5521 {
5522 struct elf_link_hash_entry *h;
5523 char *p, *copy;
5524 size_t len, first;
5525
5526 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5527 if (h != NULL)
5528 return h;
5529
5530 /* If this is a default version (the name contains @@), look up the
5531 symbol again with only one `@' as well as without the version.
5532 The effect is that references to the symbol with and without the
5533 version will be matched by the default symbol in the archive. */
5534
5535 p = strchr (name, ELF_VER_CHR);
5536 if (p == NULL || p[1] != ELF_VER_CHR)
5537 return h;
5538
5539 /* First check with only one `@'. */
5540 len = strlen (name);
5541 copy = (char *) bfd_alloc (abfd, len);
5542 if (copy == NULL)
5543 return (struct elf_link_hash_entry *) -1;
5544
5545 first = p - name + 1;
5546 memcpy (copy, name, first);
5547 memcpy (copy + first, name + first + 1, len - first);
5548
5549 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5550 if (h == NULL)
5551 {
5552 /* We also need to check references to the symbol without the
5553 version. */
5554 copy[first - 1] = '\0';
5555 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5556 FALSE, FALSE, TRUE);
5557 }
5558
5559 bfd_release (abfd, copy);
5560 return h;
5561 }
5562
5563 /* Add symbols from an ELF archive file to the linker hash table. We
5564 don't use _bfd_generic_link_add_archive_symbols because we need to
5565 handle versioned symbols.
5566
5567 Fortunately, ELF archive handling is simpler than that done by
5568 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5569 oddities. In ELF, if we find a symbol in the archive map, and the
5570 symbol is currently undefined, we know that we must pull in that
5571 object file.
5572
5573 Unfortunately, we do have to make multiple passes over the symbol
5574 table until nothing further is resolved. */
5575
5576 static bfd_boolean
5577 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5578 {
5579 symindex c;
5580 unsigned char *included = NULL;
5581 carsym *symdefs;
5582 bfd_boolean loop;
5583 bfd_size_type amt;
5584 const struct elf_backend_data *bed;
5585 struct elf_link_hash_entry * (*archive_symbol_lookup)
5586 (bfd *, struct bfd_link_info *, const char *);
5587
5588 if (! bfd_has_map (abfd))
5589 {
5590 /* An empty archive is a special case. */
5591 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5592 return TRUE;
5593 bfd_set_error (bfd_error_no_armap);
5594 return FALSE;
5595 }
5596
5597 /* Keep track of all symbols we know to be already defined, and all
5598 files we know to be already included. This is to speed up the
5599 second and subsequent passes. */
5600 c = bfd_ardata (abfd)->symdef_count;
5601 if (c == 0)
5602 return TRUE;
5603 amt = c;
5604 amt *= sizeof (*included);
5605 included = (unsigned char *) bfd_zmalloc (amt);
5606 if (included == NULL)
5607 return FALSE;
5608
5609 symdefs = bfd_ardata (abfd)->symdefs;
5610 bed = get_elf_backend_data (abfd);
5611 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5612
5613 do
5614 {
5615 file_ptr last;
5616 symindex i;
5617 carsym *symdef;
5618 carsym *symdefend;
5619
5620 loop = FALSE;
5621 last = -1;
5622
5623 symdef = symdefs;
5624 symdefend = symdef + c;
5625 for (i = 0; symdef < symdefend; symdef++, i++)
5626 {
5627 struct elf_link_hash_entry *h;
5628 bfd *element;
5629 struct bfd_link_hash_entry *undefs_tail;
5630 symindex mark;
5631
5632 if (included[i])
5633 continue;
5634 if (symdef->file_offset == last)
5635 {
5636 included[i] = TRUE;
5637 continue;
5638 }
5639
5640 h = archive_symbol_lookup (abfd, info, symdef->name);
5641 if (h == (struct elf_link_hash_entry *) -1)
5642 goto error_return;
5643
5644 if (h == NULL)
5645 continue;
5646
5647 if (h->root.type == bfd_link_hash_common)
5648 {
5649 /* We currently have a common symbol. The archive map contains
5650 a reference to this symbol, so we may want to include it. We
5651 only want to include it however, if this archive element
5652 contains a definition of the symbol, not just another common
5653 declaration of it.
5654
5655 Unfortunately some archivers (including GNU ar) will put
5656 declarations of common symbols into their archive maps, as
5657 well as real definitions, so we cannot just go by the archive
5658 map alone. Instead we must read in the element's symbol
5659 table and check that to see what kind of symbol definition
5660 this is. */
5661 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5662 continue;
5663 }
5664 else if (h->root.type != bfd_link_hash_undefined)
5665 {
5666 if (h->root.type != bfd_link_hash_undefweak)
5667 /* Symbol must be defined. Don't check it again. */
5668 included[i] = TRUE;
5669 continue;
5670 }
5671
5672 /* We need to include this archive member. */
5673 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5674 if (element == NULL)
5675 goto error_return;
5676
5677 if (! bfd_check_format (element, bfd_object))
5678 goto error_return;
5679
5680 undefs_tail = info->hash->undefs_tail;
5681
5682 if (!(*info->callbacks
5683 ->add_archive_element) (info, element, symdef->name, &element))
5684 continue;
5685 if (!bfd_link_add_symbols (element, info))
5686 goto error_return;
5687
5688 /* If there are any new undefined symbols, we need to make
5689 another pass through the archive in order to see whether
5690 they can be defined. FIXME: This isn't perfect, because
5691 common symbols wind up on undefs_tail and because an
5692 undefined symbol which is defined later on in this pass
5693 does not require another pass. This isn't a bug, but it
5694 does make the code less efficient than it could be. */
5695 if (undefs_tail != info->hash->undefs_tail)
5696 loop = TRUE;
5697
5698 /* Look backward to mark all symbols from this object file
5699 which we have already seen in this pass. */
5700 mark = i;
5701 do
5702 {
5703 included[mark] = TRUE;
5704 if (mark == 0)
5705 break;
5706 --mark;
5707 }
5708 while (symdefs[mark].file_offset == symdef->file_offset);
5709
5710 /* We mark subsequent symbols from this object file as we go
5711 on through the loop. */
5712 last = symdef->file_offset;
5713 }
5714 }
5715 while (loop);
5716
5717 free (included);
5718
5719 return TRUE;
5720
5721 error_return:
5722 if (included != NULL)
5723 free (included);
5724 return FALSE;
5725 }
5726
5727 /* Given an ELF BFD, add symbols to the global hash table as
5728 appropriate. */
5729
5730 bfd_boolean
5731 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5732 {
5733 switch (bfd_get_format (abfd))
5734 {
5735 case bfd_object:
5736 return elf_link_add_object_symbols (abfd, info);
5737 case bfd_archive:
5738 return elf_link_add_archive_symbols (abfd, info);
5739 default:
5740 bfd_set_error (bfd_error_wrong_format);
5741 return FALSE;
5742 }
5743 }
5744
5745 struct hash_codes_info
5747 {
5748 unsigned long *hashcodes;
5749 bfd_boolean error;
5750 };
5751
5752 /* This function will be called though elf_link_hash_traverse to store
5753 all hash value of the exported symbols in an array. */
5754
5755 static bfd_boolean
5756 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5757 {
5758 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5759 const char *name;
5760 unsigned long ha;
5761 char *alc = NULL;
5762
5763 /* Ignore indirect symbols. These are added by the versioning code. */
5764 if (h->dynindx == -1)
5765 return TRUE;
5766
5767 name = h->root.root.string;
5768 if (h->versioned >= versioned)
5769 {
5770 char *p = strchr (name, ELF_VER_CHR);
5771 if (p != NULL)
5772 {
5773 alc = (char *) bfd_malloc (p - name + 1);
5774 if (alc == NULL)
5775 {
5776 inf->error = TRUE;
5777 return FALSE;
5778 }
5779 memcpy (alc, name, p - name);
5780 alc[p - name] = '\0';
5781 name = alc;
5782 }
5783 }
5784
5785 /* Compute the hash value. */
5786 ha = bfd_elf_hash (name);
5787
5788 /* Store the found hash value in the array given as the argument. */
5789 *(inf->hashcodes)++ = ha;
5790
5791 /* And store it in the struct so that we can put it in the hash table
5792 later. */
5793 h->u.elf_hash_value = ha;
5794
5795 if (alc != NULL)
5796 free (alc);
5797
5798 return TRUE;
5799 }
5800
5801 struct collect_gnu_hash_codes
5802 {
5803 bfd *output_bfd;
5804 const struct elf_backend_data *bed;
5805 unsigned long int nsyms;
5806 unsigned long int maskbits;
5807 unsigned long int *hashcodes;
5808 unsigned long int *hashval;
5809 unsigned long int *indx;
5810 unsigned long int *counts;
5811 bfd_vma *bitmask;
5812 bfd_byte *contents;
5813 long int min_dynindx;
5814 unsigned long int bucketcount;
5815 unsigned long int symindx;
5816 long int local_indx;
5817 long int shift1, shift2;
5818 unsigned long int mask;
5819 bfd_boolean error;
5820 };
5821
5822 /* This function will be called though elf_link_hash_traverse to store
5823 all hash value of the exported symbols in an array. */
5824
5825 static bfd_boolean
5826 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5827 {
5828 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5829 const char *name;
5830 unsigned long ha;
5831 char *alc = NULL;
5832
5833 /* Ignore indirect symbols. These are added by the versioning code. */
5834 if (h->dynindx == -1)
5835 return TRUE;
5836
5837 /* Ignore also local symbols and undefined symbols. */
5838 if (! (*s->bed->elf_hash_symbol) (h))
5839 return TRUE;
5840
5841 name = h->root.root.string;
5842 if (h->versioned >= versioned)
5843 {
5844 char *p = strchr (name, ELF_VER_CHR);
5845 if (p != NULL)
5846 {
5847 alc = (char *) bfd_malloc (p - name + 1);
5848 if (alc == NULL)
5849 {
5850 s->error = TRUE;
5851 return FALSE;
5852 }
5853 memcpy (alc, name, p - name);
5854 alc[p - name] = '\0';
5855 name = alc;
5856 }
5857 }
5858
5859 /* Compute the hash value. */
5860 ha = bfd_elf_gnu_hash (name);
5861
5862 /* Store the found hash value in the array for compute_bucket_count,
5863 and also for .dynsym reordering purposes. */
5864 s->hashcodes[s->nsyms] = ha;
5865 s->hashval[h->dynindx] = ha;
5866 ++s->nsyms;
5867 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5868 s->min_dynindx = h->dynindx;
5869
5870 if (alc != NULL)
5871 free (alc);
5872
5873 return TRUE;
5874 }
5875
5876 /* This function will be called though elf_link_hash_traverse to do
5877 final dynaminc symbol renumbering. */
5878
5879 static bfd_boolean
5880 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5881 {
5882 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5883 unsigned long int bucket;
5884 unsigned long int val;
5885
5886 /* Ignore indirect symbols. */
5887 if (h->dynindx == -1)
5888 return TRUE;
5889
5890 /* Ignore also local symbols and undefined symbols. */
5891 if (! (*s->bed->elf_hash_symbol) (h))
5892 {
5893 if (h->dynindx >= s->min_dynindx)
5894 h->dynindx = s->local_indx++;
5895 return TRUE;
5896 }
5897
5898 bucket = s->hashval[h->dynindx] % s->bucketcount;
5899 val = (s->hashval[h->dynindx] >> s->shift1)
5900 & ((s->maskbits >> s->shift1) - 1);
5901 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5902 s->bitmask[val]
5903 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5904 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5905 if (s->counts[bucket] == 1)
5906 /* Last element terminates the chain. */
5907 val |= 1;
5908 bfd_put_32 (s->output_bfd, val,
5909 s->contents + (s->indx[bucket] - s->symindx) * 4);
5910 --s->counts[bucket];
5911 h->dynindx = s->indx[bucket]++;
5912 return TRUE;
5913 }
5914
5915 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5916
5917 bfd_boolean
5918 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5919 {
5920 return !(h->forced_local
5921 || h->root.type == bfd_link_hash_undefined
5922 || h->root.type == bfd_link_hash_undefweak
5923 || ((h->root.type == bfd_link_hash_defined
5924 || h->root.type == bfd_link_hash_defweak)
5925 && h->root.u.def.section->output_section == NULL));
5926 }
5927
5928 /* Array used to determine the number of hash table buckets to use
5929 based on the number of symbols there are. If there are fewer than
5930 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5931 fewer than 37 we use 17 buckets, and so forth. We never use more
5932 than 32771 buckets. */
5933
5934 static const size_t elf_buckets[] =
5935 {
5936 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5937 16411, 32771, 0
5938 };
5939
5940 /* Compute bucket count for hashing table. We do not use a static set
5941 of possible tables sizes anymore. Instead we determine for all
5942 possible reasonable sizes of the table the outcome (i.e., the
5943 number of collisions etc) and choose the best solution. The
5944 weighting functions are not too simple to allow the table to grow
5945 without bounds. Instead one of the weighting factors is the size.
5946 Therefore the result is always a good payoff between few collisions
5947 (= short chain lengths) and table size. */
5948 static size_t
5949 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5950 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5951 unsigned long int nsyms,
5952 int gnu_hash)
5953 {
5954 size_t best_size = 0;
5955 unsigned long int i;
5956
5957 /* We have a problem here. The following code to optimize the table
5958 size requires an integer type with more the 32 bits. If
5959 BFD_HOST_U_64_BIT is set we know about such a type. */
5960 #ifdef BFD_HOST_U_64_BIT
5961 if (info->optimize)
5962 {
5963 size_t minsize;
5964 size_t maxsize;
5965 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5966 bfd *dynobj = elf_hash_table (info)->dynobj;
5967 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5968 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5969 unsigned long int *counts;
5970 bfd_size_type amt;
5971 unsigned int no_improvement_count = 0;
5972
5973 /* Possible optimization parameters: if we have NSYMS symbols we say
5974 that the hashing table must at least have NSYMS/4 and at most
5975 2*NSYMS buckets. */
5976 minsize = nsyms / 4;
5977 if (minsize == 0)
5978 minsize = 1;
5979 best_size = maxsize = nsyms * 2;
5980 if (gnu_hash)
5981 {
5982 if (minsize < 2)
5983 minsize = 2;
5984 if ((best_size & 31) == 0)
5985 ++best_size;
5986 }
5987
5988 /* Create array where we count the collisions in. We must use bfd_malloc
5989 since the size could be large. */
5990 amt = maxsize;
5991 amt *= sizeof (unsigned long int);
5992 counts = (unsigned long int *) bfd_malloc (amt);
5993 if (counts == NULL)
5994 return 0;
5995
5996 /* Compute the "optimal" size for the hash table. The criteria is a
5997 minimal chain length. The minor criteria is (of course) the size
5998 of the table. */
5999 for (i = minsize; i < maxsize; ++i)
6000 {
6001 /* Walk through the array of hashcodes and count the collisions. */
6002 BFD_HOST_U_64_BIT max;
6003 unsigned long int j;
6004 unsigned long int fact;
6005
6006 if (gnu_hash && (i & 31) == 0)
6007 continue;
6008
6009 memset (counts, '\0', i * sizeof (unsigned long int));
6010
6011 /* Determine how often each hash bucket is used. */
6012 for (j = 0; j < nsyms; ++j)
6013 ++counts[hashcodes[j] % i];
6014
6015 /* For the weight function we need some information about the
6016 pagesize on the target. This is information need not be 100%
6017 accurate. Since this information is not available (so far) we
6018 define it here to a reasonable default value. If it is crucial
6019 to have a better value some day simply define this value. */
6020 # ifndef BFD_TARGET_PAGESIZE
6021 # define BFD_TARGET_PAGESIZE (4096)
6022 # endif
6023
6024 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6025 and the chains. */
6026 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6027
6028 # if 1
6029 /* Variant 1: optimize for short chains. We add the squares
6030 of all the chain lengths (which favors many small chain
6031 over a few long chains). */
6032 for (j = 0; j < i; ++j)
6033 max += counts[j] * counts[j];
6034
6035 /* This adds penalties for the overall size of the table. */
6036 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6037 max *= fact * fact;
6038 # else
6039 /* Variant 2: Optimize a lot more for small table. Here we
6040 also add squares of the size but we also add penalties for
6041 empty slots (the +1 term). */
6042 for (j = 0; j < i; ++j)
6043 max += (1 + counts[j]) * (1 + counts[j]);
6044
6045 /* The overall size of the table is considered, but not as
6046 strong as in variant 1, where it is squared. */
6047 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6048 max *= fact;
6049 # endif
6050
6051 /* Compare with current best results. */
6052 if (max < best_chlen)
6053 {
6054 best_chlen = max;
6055 best_size = i;
6056 no_improvement_count = 0;
6057 }
6058 /* PR 11843: Avoid futile long searches for the best bucket size
6059 when there are a large number of symbols. */
6060 else if (++no_improvement_count == 100)
6061 break;
6062 }
6063
6064 free (counts);
6065 }
6066 else
6067 #endif /* defined (BFD_HOST_U_64_BIT) */
6068 {
6069 /* This is the fallback solution if no 64bit type is available or if we
6070 are not supposed to spend much time on optimizations. We select the
6071 bucket count using a fixed set of numbers. */
6072 for (i = 0; elf_buckets[i] != 0; i++)
6073 {
6074 best_size = elf_buckets[i];
6075 if (nsyms < elf_buckets[i + 1])
6076 break;
6077 }
6078 if (gnu_hash && best_size < 2)
6079 best_size = 2;
6080 }
6081
6082 return best_size;
6083 }
6084
6085 /* Size any SHT_GROUP section for ld -r. */
6086
6087 bfd_boolean
6088 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6089 {
6090 bfd *ibfd;
6091 asection *s;
6092
6093 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6094 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6095 && (s = ibfd->sections) != NULL
6096 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6097 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6098 return FALSE;
6099 return TRUE;
6100 }
6101
6102 /* Set a default stack segment size. The value in INFO wins. If it
6103 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6104 undefined it is initialized. */
6105
6106 bfd_boolean
6107 bfd_elf_stack_segment_size (bfd *output_bfd,
6108 struct bfd_link_info *info,
6109 const char *legacy_symbol,
6110 bfd_vma default_size)
6111 {
6112 struct elf_link_hash_entry *h = NULL;
6113
6114 /* Look for legacy symbol. */
6115 if (legacy_symbol)
6116 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6117 FALSE, FALSE, FALSE);
6118 if (h && (h->root.type == bfd_link_hash_defined
6119 || h->root.type == bfd_link_hash_defweak)
6120 && h->def_regular
6121 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6122 {
6123 /* The symbol has no type if specified on the command line. */
6124 h->type = STT_OBJECT;
6125 if (info->stacksize)
6126 /* xgettext:c-format */
6127 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6128 output_bfd, legacy_symbol);
6129 else if (h->root.u.def.section != bfd_abs_section_ptr)
6130 /* xgettext:c-format */
6131 _bfd_error_handler (_("%pB: %s not absolute"),
6132 output_bfd, legacy_symbol);
6133 else
6134 info->stacksize = h->root.u.def.value;
6135 }
6136
6137 if (!info->stacksize)
6138 /* If the user didn't set a size, or explicitly inhibit the
6139 size, set it now. */
6140 info->stacksize = default_size;
6141
6142 /* Provide the legacy symbol, if it is referenced. */
6143 if (h && (h->root.type == bfd_link_hash_undefined
6144 || h->root.type == bfd_link_hash_undefweak))
6145 {
6146 struct bfd_link_hash_entry *bh = NULL;
6147
6148 if (!(_bfd_generic_link_add_one_symbol
6149 (info, output_bfd, legacy_symbol,
6150 BSF_GLOBAL, bfd_abs_section_ptr,
6151 info->stacksize >= 0 ? info->stacksize : 0,
6152 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6153 return FALSE;
6154
6155 h = (struct elf_link_hash_entry *) bh;
6156 h->def_regular = 1;
6157 h->type = STT_OBJECT;
6158 }
6159
6160 return TRUE;
6161 }
6162
6163 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6164
6165 struct elf_gc_sweep_symbol_info
6166 {
6167 struct bfd_link_info *info;
6168 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6169 bfd_boolean);
6170 };
6171
6172 static bfd_boolean
6173 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6174 {
6175 if (!h->mark
6176 && (((h->root.type == bfd_link_hash_defined
6177 || h->root.type == bfd_link_hash_defweak)
6178 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6179 && h->root.u.def.section->gc_mark))
6180 || h->root.type == bfd_link_hash_undefined
6181 || h->root.type == bfd_link_hash_undefweak))
6182 {
6183 struct elf_gc_sweep_symbol_info *inf;
6184
6185 inf = (struct elf_gc_sweep_symbol_info *) data;
6186 (*inf->hide_symbol) (inf->info, h, TRUE);
6187 h->def_regular = 0;
6188 h->ref_regular = 0;
6189 h->ref_regular_nonweak = 0;
6190 }
6191
6192 return TRUE;
6193 }
6194
6195 /* Set up the sizes and contents of the ELF dynamic sections. This is
6196 called by the ELF linker emulation before_allocation routine. We
6197 must set the sizes of the sections before the linker sets the
6198 addresses of the various sections. */
6199
6200 bfd_boolean
6201 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6202 const char *soname,
6203 const char *rpath,
6204 const char *filter_shlib,
6205 const char *audit,
6206 const char *depaudit,
6207 const char * const *auxiliary_filters,
6208 struct bfd_link_info *info,
6209 asection **sinterpptr)
6210 {
6211 bfd *dynobj;
6212 const struct elf_backend_data *bed;
6213
6214 *sinterpptr = NULL;
6215
6216 if (!is_elf_hash_table (info->hash))
6217 return TRUE;
6218
6219 dynobj = elf_hash_table (info)->dynobj;
6220
6221 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6222 {
6223 struct bfd_elf_version_tree *verdefs;
6224 struct elf_info_failed asvinfo;
6225 struct bfd_elf_version_tree *t;
6226 struct bfd_elf_version_expr *d;
6227 asection *s;
6228 size_t soname_indx;
6229
6230 /* If we are supposed to export all symbols into the dynamic symbol
6231 table (this is not the normal case), then do so. */
6232 if (info->export_dynamic
6233 || (bfd_link_executable (info) && info->dynamic))
6234 {
6235 struct elf_info_failed eif;
6236
6237 eif.info = info;
6238 eif.failed = FALSE;
6239 elf_link_hash_traverse (elf_hash_table (info),
6240 _bfd_elf_export_symbol,
6241 &eif);
6242 if (eif.failed)
6243 return FALSE;
6244 }
6245
6246 if (soname != NULL)
6247 {
6248 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6249 soname, TRUE);
6250 if (soname_indx == (size_t) -1
6251 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6252 return FALSE;
6253 }
6254 else
6255 soname_indx = (size_t) -1;
6256
6257 /* Make all global versions with definition. */
6258 for (t = info->version_info; t != NULL; t = t->next)
6259 for (d = t->globals.list; d != NULL; d = d->next)
6260 if (!d->symver && d->literal)
6261 {
6262 const char *verstr, *name;
6263 size_t namelen, verlen, newlen;
6264 char *newname, *p, leading_char;
6265 struct elf_link_hash_entry *newh;
6266
6267 leading_char = bfd_get_symbol_leading_char (output_bfd);
6268 name = d->pattern;
6269 namelen = strlen (name) + (leading_char != '\0');
6270 verstr = t->name;
6271 verlen = strlen (verstr);
6272 newlen = namelen + verlen + 3;
6273
6274 newname = (char *) bfd_malloc (newlen);
6275 if (newname == NULL)
6276 return FALSE;
6277 newname[0] = leading_char;
6278 memcpy (newname + (leading_char != '\0'), name, namelen);
6279
6280 /* Check the hidden versioned definition. */
6281 p = newname + namelen;
6282 *p++ = ELF_VER_CHR;
6283 memcpy (p, verstr, verlen + 1);
6284 newh = elf_link_hash_lookup (elf_hash_table (info),
6285 newname, FALSE, FALSE,
6286 FALSE);
6287 if (newh == NULL
6288 || (newh->root.type != bfd_link_hash_defined
6289 && newh->root.type != bfd_link_hash_defweak))
6290 {
6291 /* Check the default versioned definition. */
6292 *p++ = ELF_VER_CHR;
6293 memcpy (p, verstr, verlen + 1);
6294 newh = elf_link_hash_lookup (elf_hash_table (info),
6295 newname, FALSE, FALSE,
6296 FALSE);
6297 }
6298 free (newname);
6299
6300 /* Mark this version if there is a definition and it is
6301 not defined in a shared object. */
6302 if (newh != NULL
6303 && !newh->def_dynamic
6304 && (newh->root.type == bfd_link_hash_defined
6305 || newh->root.type == bfd_link_hash_defweak))
6306 d->symver = 1;
6307 }
6308
6309 /* Attach all the symbols to their version information. */
6310 asvinfo.info = info;
6311 asvinfo.failed = FALSE;
6312
6313 elf_link_hash_traverse (elf_hash_table (info),
6314 _bfd_elf_link_assign_sym_version,
6315 &asvinfo);
6316 if (asvinfo.failed)
6317 return FALSE;
6318
6319 if (!info->allow_undefined_version)
6320 {
6321 /* Check if all global versions have a definition. */
6322 bfd_boolean all_defined = TRUE;
6323 for (t = info->version_info; t != NULL; t = t->next)
6324 for (d = t->globals.list; d != NULL; d = d->next)
6325 if (d->literal && !d->symver && !d->script)
6326 {
6327 _bfd_error_handler
6328 (_("%s: undefined version: %s"),
6329 d->pattern, t->name);
6330 all_defined = FALSE;
6331 }
6332
6333 if (!all_defined)
6334 {
6335 bfd_set_error (bfd_error_bad_value);
6336 return FALSE;
6337 }
6338 }
6339
6340 /* Set up the version definition section. */
6341 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6342 BFD_ASSERT (s != NULL);
6343
6344 /* We may have created additional version definitions if we are
6345 just linking a regular application. */
6346 verdefs = info->version_info;
6347
6348 /* Skip anonymous version tag. */
6349 if (verdefs != NULL && verdefs->vernum == 0)
6350 verdefs = verdefs->next;
6351
6352 if (verdefs == NULL && !info->create_default_symver)
6353 s->flags |= SEC_EXCLUDE;
6354 else
6355 {
6356 unsigned int cdefs;
6357 bfd_size_type size;
6358 bfd_byte *p;
6359 Elf_Internal_Verdef def;
6360 Elf_Internal_Verdaux defaux;
6361 struct bfd_link_hash_entry *bh;
6362 struct elf_link_hash_entry *h;
6363 const char *name;
6364
6365 cdefs = 0;
6366 size = 0;
6367
6368 /* Make space for the base version. */
6369 size += sizeof (Elf_External_Verdef);
6370 size += sizeof (Elf_External_Verdaux);
6371 ++cdefs;
6372
6373 /* Make space for the default version. */
6374 if (info->create_default_symver)
6375 {
6376 size += sizeof (Elf_External_Verdef);
6377 ++cdefs;
6378 }
6379
6380 for (t = verdefs; t != NULL; t = t->next)
6381 {
6382 struct bfd_elf_version_deps *n;
6383
6384 /* Don't emit base version twice. */
6385 if (t->vernum == 0)
6386 continue;
6387
6388 size += sizeof (Elf_External_Verdef);
6389 size += sizeof (Elf_External_Verdaux);
6390 ++cdefs;
6391
6392 for (n = t->deps; n != NULL; n = n->next)
6393 size += sizeof (Elf_External_Verdaux);
6394 }
6395
6396 s->size = size;
6397 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6398 if (s->contents == NULL && s->size != 0)
6399 return FALSE;
6400
6401 /* Fill in the version definition section. */
6402
6403 p = s->contents;
6404
6405 def.vd_version = VER_DEF_CURRENT;
6406 def.vd_flags = VER_FLG_BASE;
6407 def.vd_ndx = 1;
6408 def.vd_cnt = 1;
6409 if (info->create_default_symver)
6410 {
6411 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6412 def.vd_next = sizeof (Elf_External_Verdef);
6413 }
6414 else
6415 {
6416 def.vd_aux = sizeof (Elf_External_Verdef);
6417 def.vd_next = (sizeof (Elf_External_Verdef)
6418 + sizeof (Elf_External_Verdaux));
6419 }
6420
6421 if (soname_indx != (size_t) -1)
6422 {
6423 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6424 soname_indx);
6425 def.vd_hash = bfd_elf_hash (soname);
6426 defaux.vda_name = soname_indx;
6427 name = soname;
6428 }
6429 else
6430 {
6431 size_t indx;
6432
6433 name = lbasename (output_bfd->filename);
6434 def.vd_hash = bfd_elf_hash (name);
6435 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6436 name, FALSE);
6437 if (indx == (size_t) -1)
6438 return FALSE;
6439 defaux.vda_name = indx;
6440 }
6441 defaux.vda_next = 0;
6442
6443 _bfd_elf_swap_verdef_out (output_bfd, &def,
6444 (Elf_External_Verdef *) p);
6445 p += sizeof (Elf_External_Verdef);
6446 if (info->create_default_symver)
6447 {
6448 /* Add a symbol representing this version. */
6449 bh = NULL;
6450 if (! (_bfd_generic_link_add_one_symbol
6451 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6452 0, NULL, FALSE,
6453 get_elf_backend_data (dynobj)->collect, &bh)))
6454 return FALSE;
6455 h = (struct elf_link_hash_entry *) bh;
6456 h->non_elf = 0;
6457 h->def_regular = 1;
6458 h->type = STT_OBJECT;
6459 h->verinfo.vertree = NULL;
6460
6461 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6462 return FALSE;
6463
6464 /* Create a duplicate of the base version with the same
6465 aux block, but different flags. */
6466 def.vd_flags = 0;
6467 def.vd_ndx = 2;
6468 def.vd_aux = sizeof (Elf_External_Verdef);
6469 if (verdefs)
6470 def.vd_next = (sizeof (Elf_External_Verdef)
6471 + sizeof (Elf_External_Verdaux));
6472 else
6473 def.vd_next = 0;
6474 _bfd_elf_swap_verdef_out (output_bfd, &def,
6475 (Elf_External_Verdef *) p);
6476 p += sizeof (Elf_External_Verdef);
6477 }
6478 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6479 (Elf_External_Verdaux *) p);
6480 p += sizeof (Elf_External_Verdaux);
6481
6482 for (t = verdefs; t != NULL; t = t->next)
6483 {
6484 unsigned int cdeps;
6485 struct bfd_elf_version_deps *n;
6486
6487 /* Don't emit the base version twice. */
6488 if (t->vernum == 0)
6489 continue;
6490
6491 cdeps = 0;
6492 for (n = t->deps; n != NULL; n = n->next)
6493 ++cdeps;
6494
6495 /* Add a symbol representing this version. */
6496 bh = NULL;
6497 if (! (_bfd_generic_link_add_one_symbol
6498 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6499 0, NULL, FALSE,
6500 get_elf_backend_data (dynobj)->collect, &bh)))
6501 return FALSE;
6502 h = (struct elf_link_hash_entry *) bh;
6503 h->non_elf = 0;
6504 h->def_regular = 1;
6505 h->type = STT_OBJECT;
6506 h->verinfo.vertree = t;
6507
6508 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6509 return FALSE;
6510
6511 def.vd_version = VER_DEF_CURRENT;
6512 def.vd_flags = 0;
6513 if (t->globals.list == NULL
6514 && t->locals.list == NULL
6515 && ! t->used)
6516 def.vd_flags |= VER_FLG_WEAK;
6517 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6518 def.vd_cnt = cdeps + 1;
6519 def.vd_hash = bfd_elf_hash (t->name);
6520 def.vd_aux = sizeof (Elf_External_Verdef);
6521 def.vd_next = 0;
6522
6523 /* If a basever node is next, it *must* be the last node in
6524 the chain, otherwise Verdef construction breaks. */
6525 if (t->next != NULL && t->next->vernum == 0)
6526 BFD_ASSERT (t->next->next == NULL);
6527
6528 if (t->next != NULL && t->next->vernum != 0)
6529 def.vd_next = (sizeof (Elf_External_Verdef)
6530 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6531
6532 _bfd_elf_swap_verdef_out (output_bfd, &def,
6533 (Elf_External_Verdef *) p);
6534 p += sizeof (Elf_External_Verdef);
6535
6536 defaux.vda_name = h->dynstr_index;
6537 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6538 h->dynstr_index);
6539 defaux.vda_next = 0;
6540 if (t->deps != NULL)
6541 defaux.vda_next = sizeof (Elf_External_Verdaux);
6542 t->name_indx = defaux.vda_name;
6543
6544 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6545 (Elf_External_Verdaux *) p);
6546 p += sizeof (Elf_External_Verdaux);
6547
6548 for (n = t->deps; n != NULL; n = n->next)
6549 {
6550 if (n->version_needed == NULL)
6551 {
6552 /* This can happen if there was an error in the
6553 version script. */
6554 defaux.vda_name = 0;
6555 }
6556 else
6557 {
6558 defaux.vda_name = n->version_needed->name_indx;
6559 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6560 defaux.vda_name);
6561 }
6562 if (n->next == NULL)
6563 defaux.vda_next = 0;
6564 else
6565 defaux.vda_next = sizeof (Elf_External_Verdaux);
6566
6567 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6568 (Elf_External_Verdaux *) p);
6569 p += sizeof (Elf_External_Verdaux);
6570 }
6571 }
6572
6573 elf_tdata (output_bfd)->cverdefs = cdefs;
6574 }
6575 }
6576
6577 bed = get_elf_backend_data (output_bfd);
6578
6579 if (info->gc_sections && bed->can_gc_sections)
6580 {
6581 struct elf_gc_sweep_symbol_info sweep_info;
6582
6583 /* Remove the symbols that were in the swept sections from the
6584 dynamic symbol table. */
6585 sweep_info.info = info;
6586 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6587 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6588 &sweep_info);
6589 }
6590
6591 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6592 {
6593 asection *s;
6594 struct elf_find_verdep_info sinfo;
6595
6596 /* Work out the size of the version reference section. */
6597
6598 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6599 BFD_ASSERT (s != NULL);
6600
6601 sinfo.info = info;
6602 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6603 if (sinfo.vers == 0)
6604 sinfo.vers = 1;
6605 sinfo.failed = FALSE;
6606
6607 elf_link_hash_traverse (elf_hash_table (info),
6608 _bfd_elf_link_find_version_dependencies,
6609 &sinfo);
6610 if (sinfo.failed)
6611 return FALSE;
6612
6613 if (elf_tdata (output_bfd)->verref == NULL)
6614 s->flags |= SEC_EXCLUDE;
6615 else
6616 {
6617 Elf_Internal_Verneed *vn;
6618 unsigned int size;
6619 unsigned int crefs;
6620 bfd_byte *p;
6621
6622 /* Build the version dependency section. */
6623 size = 0;
6624 crefs = 0;
6625 for (vn = elf_tdata (output_bfd)->verref;
6626 vn != NULL;
6627 vn = vn->vn_nextref)
6628 {
6629 Elf_Internal_Vernaux *a;
6630
6631 size += sizeof (Elf_External_Verneed);
6632 ++crefs;
6633 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6634 size += sizeof (Elf_External_Vernaux);
6635 }
6636
6637 s->size = size;
6638 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6639 if (s->contents == NULL)
6640 return FALSE;
6641
6642 p = s->contents;
6643 for (vn = elf_tdata (output_bfd)->verref;
6644 vn != NULL;
6645 vn = vn->vn_nextref)
6646 {
6647 unsigned int caux;
6648 Elf_Internal_Vernaux *a;
6649 size_t indx;
6650
6651 caux = 0;
6652 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6653 ++caux;
6654
6655 vn->vn_version = VER_NEED_CURRENT;
6656 vn->vn_cnt = caux;
6657 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6658 elf_dt_name (vn->vn_bfd) != NULL
6659 ? elf_dt_name (vn->vn_bfd)
6660 : lbasename (vn->vn_bfd->filename),
6661 FALSE);
6662 if (indx == (size_t) -1)
6663 return FALSE;
6664 vn->vn_file = indx;
6665 vn->vn_aux = sizeof (Elf_External_Verneed);
6666 if (vn->vn_nextref == NULL)
6667 vn->vn_next = 0;
6668 else
6669 vn->vn_next = (sizeof (Elf_External_Verneed)
6670 + caux * sizeof (Elf_External_Vernaux));
6671
6672 _bfd_elf_swap_verneed_out (output_bfd, vn,
6673 (Elf_External_Verneed *) p);
6674 p += sizeof (Elf_External_Verneed);
6675
6676 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6677 {
6678 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6679 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6680 a->vna_nodename, FALSE);
6681 if (indx == (size_t) -1)
6682 return FALSE;
6683 a->vna_name = indx;
6684 if (a->vna_nextptr == NULL)
6685 a->vna_next = 0;
6686 else
6687 a->vna_next = sizeof (Elf_External_Vernaux);
6688
6689 _bfd_elf_swap_vernaux_out (output_bfd, a,
6690 (Elf_External_Vernaux *) p);
6691 p += sizeof (Elf_External_Vernaux);
6692 }
6693 }
6694
6695 elf_tdata (output_bfd)->cverrefs = crefs;
6696 }
6697 }
6698
6699 /* Any syms created from now on start with -1 in
6700 got.refcount/offset and plt.refcount/offset. */
6701 elf_hash_table (info)->init_got_refcount
6702 = elf_hash_table (info)->init_got_offset;
6703 elf_hash_table (info)->init_plt_refcount
6704 = elf_hash_table (info)->init_plt_offset;
6705
6706 if (bfd_link_relocatable (info)
6707 && !_bfd_elf_size_group_sections (info))
6708 return FALSE;
6709
6710 /* The backend may have to create some sections regardless of whether
6711 we're dynamic or not. */
6712 if (bed->elf_backend_always_size_sections
6713 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6714 return FALSE;
6715
6716 /* Determine any GNU_STACK segment requirements, after the backend
6717 has had a chance to set a default segment size. */
6718 if (info->execstack)
6719 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6720 else if (info->noexecstack)
6721 elf_stack_flags (output_bfd) = PF_R | PF_W;
6722 else
6723 {
6724 bfd *inputobj;
6725 asection *notesec = NULL;
6726 int exec = 0;
6727
6728 for (inputobj = info->input_bfds;
6729 inputobj;
6730 inputobj = inputobj->link.next)
6731 {
6732 asection *s;
6733
6734 if (inputobj->flags
6735 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6736 continue;
6737 s = inputobj->sections;
6738 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6739 continue;
6740
6741 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6742 if (s)
6743 {
6744 if (s->flags & SEC_CODE)
6745 exec = PF_X;
6746 notesec = s;
6747 }
6748 else if (bed->default_execstack)
6749 exec = PF_X;
6750 }
6751 if (notesec || info->stacksize > 0)
6752 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6753 if (notesec && exec && bfd_link_relocatable (info)
6754 && notesec->output_section != bfd_abs_section_ptr)
6755 notesec->output_section->flags |= SEC_CODE;
6756 }
6757
6758 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6759 {
6760 struct elf_info_failed eif;
6761 struct elf_link_hash_entry *h;
6762 asection *dynstr;
6763 asection *s;
6764
6765 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6766 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6767
6768 if (info->symbolic)
6769 {
6770 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6771 return FALSE;
6772 info->flags |= DF_SYMBOLIC;
6773 }
6774
6775 if (rpath != NULL)
6776 {
6777 size_t indx;
6778 bfd_vma tag;
6779
6780 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6781 TRUE);
6782 if (indx == (size_t) -1)
6783 return FALSE;
6784
6785 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6786 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6787 return FALSE;
6788 }
6789
6790 if (filter_shlib != NULL)
6791 {
6792 size_t indx;
6793
6794 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6795 filter_shlib, TRUE);
6796 if (indx == (size_t) -1
6797 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6798 return FALSE;
6799 }
6800
6801 if (auxiliary_filters != NULL)
6802 {
6803 const char * const *p;
6804
6805 for (p = auxiliary_filters; *p != NULL; p++)
6806 {
6807 size_t indx;
6808
6809 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6810 *p, TRUE);
6811 if (indx == (size_t) -1
6812 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6813 return FALSE;
6814 }
6815 }
6816
6817 if (audit != NULL)
6818 {
6819 size_t indx;
6820
6821 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6822 TRUE);
6823 if (indx == (size_t) -1
6824 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6825 return FALSE;
6826 }
6827
6828 if (depaudit != NULL)
6829 {
6830 size_t indx;
6831
6832 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6833 TRUE);
6834 if (indx == (size_t) -1
6835 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6836 return FALSE;
6837 }
6838
6839 eif.info = info;
6840 eif.failed = FALSE;
6841
6842 /* Find all symbols which were defined in a dynamic object and make
6843 the backend pick a reasonable value for them. */
6844 elf_link_hash_traverse (elf_hash_table (info),
6845 _bfd_elf_adjust_dynamic_symbol,
6846 &eif);
6847 if (eif.failed)
6848 return FALSE;
6849
6850 /* Add some entries to the .dynamic section. We fill in some of the
6851 values later, in bfd_elf_final_link, but we must add the entries
6852 now so that we know the final size of the .dynamic section. */
6853
6854 /* If there are initialization and/or finalization functions to
6855 call then add the corresponding DT_INIT/DT_FINI entries. */
6856 h = (info->init_function
6857 ? elf_link_hash_lookup (elf_hash_table (info),
6858 info->init_function, FALSE,
6859 FALSE, FALSE)
6860 : NULL);
6861 if (h != NULL
6862 && (h->ref_regular
6863 || h->def_regular))
6864 {
6865 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6866 return FALSE;
6867 }
6868 h = (info->fini_function
6869 ? elf_link_hash_lookup (elf_hash_table (info),
6870 info->fini_function, FALSE,
6871 FALSE, FALSE)
6872 : NULL);
6873 if (h != NULL
6874 && (h->ref_regular
6875 || h->def_regular))
6876 {
6877 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6878 return FALSE;
6879 }
6880
6881 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6882 if (s != NULL && s->linker_has_input)
6883 {
6884 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6885 if (! bfd_link_executable (info))
6886 {
6887 bfd *sub;
6888 asection *o;
6889
6890 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6891 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6892 && (o = sub->sections) != NULL
6893 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6894 for (o = sub->sections; o != NULL; o = o->next)
6895 if (elf_section_data (o)->this_hdr.sh_type
6896 == SHT_PREINIT_ARRAY)
6897 {
6898 _bfd_error_handler
6899 (_("%pB: .preinit_array section is not allowed in DSO"),
6900 sub);
6901 break;
6902 }
6903
6904 bfd_set_error (bfd_error_nonrepresentable_section);
6905 return FALSE;
6906 }
6907
6908 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6909 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6910 return FALSE;
6911 }
6912 s = bfd_get_section_by_name (output_bfd, ".init_array");
6913 if (s != NULL && s->linker_has_input)
6914 {
6915 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6916 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6917 return FALSE;
6918 }
6919 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6920 if (s != NULL && s->linker_has_input)
6921 {
6922 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6923 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6924 return FALSE;
6925 }
6926
6927 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6928 /* If .dynstr is excluded from the link, we don't want any of
6929 these tags. Strictly, we should be checking each section
6930 individually; This quick check covers for the case where
6931 someone does a /DISCARD/ : { *(*) }. */
6932 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6933 {
6934 bfd_size_type strsize;
6935
6936 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6937 if ((info->emit_hash
6938 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6939 || (info->emit_gnu_hash
6940 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6941 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6942 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6943 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6944 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6945 bed->s->sizeof_sym))
6946 return FALSE;
6947 }
6948 }
6949
6950 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6951 return FALSE;
6952
6953 /* The backend must work out the sizes of all the other dynamic
6954 sections. */
6955 if (dynobj != NULL
6956 && bed->elf_backend_size_dynamic_sections != NULL
6957 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6958 return FALSE;
6959
6960 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6961 {
6962 if (elf_tdata (output_bfd)->cverdefs)
6963 {
6964 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6965
6966 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6967 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6968 return FALSE;
6969 }
6970
6971 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6972 {
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6974 return FALSE;
6975 }
6976 else if (info->flags & DF_BIND_NOW)
6977 {
6978 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6979 return FALSE;
6980 }
6981
6982 if (info->flags_1)
6983 {
6984 if (bfd_link_executable (info))
6985 info->flags_1 &= ~ (DF_1_INITFIRST
6986 | DF_1_NODELETE
6987 | DF_1_NOOPEN);
6988 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6989 return FALSE;
6990 }
6991
6992 if (elf_tdata (output_bfd)->cverrefs)
6993 {
6994 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6995
6996 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6997 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6998 return FALSE;
6999 }
7000
7001 if ((elf_tdata (output_bfd)->cverrefs == 0
7002 && elf_tdata (output_bfd)->cverdefs == 0)
7003 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7004 {
7005 asection *s;
7006
7007 s = bfd_get_linker_section (dynobj, ".gnu.version");
7008 s->flags |= SEC_EXCLUDE;
7009 }
7010 }
7011 return TRUE;
7012 }
7013
7014 /* Find the first non-excluded output section. We'll use its
7015 section symbol for some emitted relocs. */
7016 void
7017 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7018 {
7019 asection *s;
7020
7021 for (s = output_bfd->sections; s != NULL; s = s->next)
7022 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7023 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7024 {
7025 elf_hash_table (info)->text_index_section = s;
7026 break;
7027 }
7028 }
7029
7030 /* Find two non-excluded output sections, one for code, one for data.
7031 We'll use their section symbols for some emitted relocs. */
7032 void
7033 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7034 {
7035 asection *s;
7036
7037 /* Data first, since setting text_index_section changes
7038 _bfd_elf_omit_section_dynsym_default. */
7039 for (s = output_bfd->sections; s != NULL; s = s->next)
7040 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7041 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7042 {
7043 elf_hash_table (info)->data_index_section = s;
7044 break;
7045 }
7046
7047 for (s = output_bfd->sections; s != NULL; s = s->next)
7048 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7049 == (SEC_ALLOC | SEC_READONLY))
7050 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7051 {
7052 elf_hash_table (info)->text_index_section = s;
7053 break;
7054 }
7055
7056 if (elf_hash_table (info)->text_index_section == NULL)
7057 elf_hash_table (info)->text_index_section
7058 = elf_hash_table (info)->data_index_section;
7059 }
7060
7061 bfd_boolean
7062 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7063 {
7064 const struct elf_backend_data *bed;
7065 unsigned long section_sym_count;
7066 bfd_size_type dynsymcount = 0;
7067
7068 if (!is_elf_hash_table (info->hash))
7069 return TRUE;
7070
7071 bed = get_elf_backend_data (output_bfd);
7072 (*bed->elf_backend_init_index_section) (output_bfd, info);
7073
7074 /* Assign dynsym indices. In a shared library we generate a section
7075 symbol for each output section, which come first. Next come all
7076 of the back-end allocated local dynamic syms, followed by the rest
7077 of the global symbols.
7078
7079 This is usually not needed for static binaries, however backends
7080 can request to always do it, e.g. the MIPS backend uses dynamic
7081 symbol counts to lay out GOT, which will be produced in the
7082 presence of GOT relocations even in static binaries (holding fixed
7083 data in that case, to satisfy those relocations). */
7084
7085 if (elf_hash_table (info)->dynamic_sections_created
7086 || bed->always_renumber_dynsyms)
7087 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7088 §ion_sym_count);
7089
7090 if (elf_hash_table (info)->dynamic_sections_created)
7091 {
7092 bfd *dynobj;
7093 asection *s;
7094 unsigned int dtagcount;
7095
7096 dynobj = elf_hash_table (info)->dynobj;
7097
7098 /* Work out the size of the symbol version section. */
7099 s = bfd_get_linker_section (dynobj, ".gnu.version");
7100 BFD_ASSERT (s != NULL);
7101 if ((s->flags & SEC_EXCLUDE) == 0)
7102 {
7103 s->size = dynsymcount * sizeof (Elf_External_Versym);
7104 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7105 if (s->contents == NULL)
7106 return FALSE;
7107
7108 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7109 return FALSE;
7110 }
7111
7112 /* Set the size of the .dynsym and .hash sections. We counted
7113 the number of dynamic symbols in elf_link_add_object_symbols.
7114 We will build the contents of .dynsym and .hash when we build
7115 the final symbol table, because until then we do not know the
7116 correct value to give the symbols. We built the .dynstr
7117 section as we went along in elf_link_add_object_symbols. */
7118 s = elf_hash_table (info)->dynsym;
7119 BFD_ASSERT (s != NULL);
7120 s->size = dynsymcount * bed->s->sizeof_sym;
7121
7122 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7123 if (s->contents == NULL)
7124 return FALSE;
7125
7126 /* The first entry in .dynsym is a dummy symbol. Clear all the
7127 section syms, in case we don't output them all. */
7128 ++section_sym_count;
7129 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7130
7131 elf_hash_table (info)->bucketcount = 0;
7132
7133 /* Compute the size of the hashing table. As a side effect this
7134 computes the hash values for all the names we export. */
7135 if (info->emit_hash)
7136 {
7137 unsigned long int *hashcodes;
7138 struct hash_codes_info hashinf;
7139 bfd_size_type amt;
7140 unsigned long int nsyms;
7141 size_t bucketcount;
7142 size_t hash_entry_size;
7143
7144 /* Compute the hash values for all exported symbols. At the same
7145 time store the values in an array so that we could use them for
7146 optimizations. */
7147 amt = dynsymcount * sizeof (unsigned long int);
7148 hashcodes = (unsigned long int *) bfd_malloc (amt);
7149 if (hashcodes == NULL)
7150 return FALSE;
7151 hashinf.hashcodes = hashcodes;
7152 hashinf.error = FALSE;
7153
7154 /* Put all hash values in HASHCODES. */
7155 elf_link_hash_traverse (elf_hash_table (info),
7156 elf_collect_hash_codes, &hashinf);
7157 if (hashinf.error)
7158 {
7159 free (hashcodes);
7160 return FALSE;
7161 }
7162
7163 nsyms = hashinf.hashcodes - hashcodes;
7164 bucketcount
7165 = compute_bucket_count (info, hashcodes, nsyms, 0);
7166 free (hashcodes);
7167
7168 if (bucketcount == 0 && nsyms > 0)
7169 return FALSE;
7170
7171 elf_hash_table (info)->bucketcount = bucketcount;
7172
7173 s = bfd_get_linker_section (dynobj, ".hash");
7174 BFD_ASSERT (s != NULL);
7175 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7176 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7177 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7178 if (s->contents == NULL)
7179 return FALSE;
7180
7181 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7182 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7183 s->contents + hash_entry_size);
7184 }
7185
7186 if (info->emit_gnu_hash)
7187 {
7188 size_t i, cnt;
7189 unsigned char *contents;
7190 struct collect_gnu_hash_codes cinfo;
7191 bfd_size_type amt;
7192 size_t bucketcount;
7193
7194 memset (&cinfo, 0, sizeof (cinfo));
7195
7196 /* Compute the hash values for all exported symbols. At the same
7197 time store the values in an array so that we could use them for
7198 optimizations. */
7199 amt = dynsymcount * 2 * sizeof (unsigned long int);
7200 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7201 if (cinfo.hashcodes == NULL)
7202 return FALSE;
7203
7204 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7205 cinfo.min_dynindx = -1;
7206 cinfo.output_bfd = output_bfd;
7207 cinfo.bed = bed;
7208
7209 /* Put all hash values in HASHCODES. */
7210 elf_link_hash_traverse (elf_hash_table (info),
7211 elf_collect_gnu_hash_codes, &cinfo);
7212 if (cinfo.error)
7213 {
7214 free (cinfo.hashcodes);
7215 return FALSE;
7216 }
7217
7218 bucketcount
7219 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7220
7221 if (bucketcount == 0)
7222 {
7223 free (cinfo.hashcodes);
7224 return FALSE;
7225 }
7226
7227 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7228 BFD_ASSERT (s != NULL);
7229
7230 if (cinfo.nsyms == 0)
7231 {
7232 /* Empty .gnu.hash section is special. */
7233 BFD_ASSERT (cinfo.min_dynindx == -1);
7234 free (cinfo.hashcodes);
7235 s->size = 5 * 4 + bed->s->arch_size / 8;
7236 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7237 if (contents == NULL)
7238 return FALSE;
7239 s->contents = contents;
7240 /* 1 empty bucket. */
7241 bfd_put_32 (output_bfd, 1, contents);
7242 /* SYMIDX above the special symbol 0. */
7243 bfd_put_32 (output_bfd, 1, contents + 4);
7244 /* Just one word for bitmask. */
7245 bfd_put_32 (output_bfd, 1, contents + 8);
7246 /* Only hash fn bloom filter. */
7247 bfd_put_32 (output_bfd, 0, contents + 12);
7248 /* No hashes are valid - empty bitmask. */
7249 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7250 /* No hashes in the only bucket. */
7251 bfd_put_32 (output_bfd, 0,
7252 contents + 16 + bed->s->arch_size / 8);
7253 }
7254 else
7255 {
7256 unsigned long int maskwords, maskbitslog2, x;
7257 BFD_ASSERT (cinfo.min_dynindx != -1);
7258
7259 x = cinfo.nsyms;
7260 maskbitslog2 = 1;
7261 while ((x >>= 1) != 0)
7262 ++maskbitslog2;
7263 if (maskbitslog2 < 3)
7264 maskbitslog2 = 5;
7265 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7266 maskbitslog2 = maskbitslog2 + 3;
7267 else
7268 maskbitslog2 = maskbitslog2 + 2;
7269 if (bed->s->arch_size == 64)
7270 {
7271 if (maskbitslog2 == 5)
7272 maskbitslog2 = 6;
7273 cinfo.shift1 = 6;
7274 }
7275 else
7276 cinfo.shift1 = 5;
7277 cinfo.mask = (1 << cinfo.shift1) - 1;
7278 cinfo.shift2 = maskbitslog2;
7279 cinfo.maskbits = 1 << maskbitslog2;
7280 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7281 amt = bucketcount * sizeof (unsigned long int) * 2;
7282 amt += maskwords * sizeof (bfd_vma);
7283 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7284 if (cinfo.bitmask == NULL)
7285 {
7286 free (cinfo.hashcodes);
7287 return FALSE;
7288 }
7289
7290 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7291 cinfo.indx = cinfo.counts + bucketcount;
7292 cinfo.symindx = dynsymcount - cinfo.nsyms;
7293 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7294
7295 /* Determine how often each hash bucket is used. */
7296 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7297 for (i = 0; i < cinfo.nsyms; ++i)
7298 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7299
7300 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7301 if (cinfo.counts[i] != 0)
7302 {
7303 cinfo.indx[i] = cnt;
7304 cnt += cinfo.counts[i];
7305 }
7306 BFD_ASSERT (cnt == dynsymcount);
7307 cinfo.bucketcount = bucketcount;
7308 cinfo.local_indx = cinfo.min_dynindx;
7309
7310 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7311 s->size += cinfo.maskbits / 8;
7312 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7313 if (contents == NULL)
7314 {
7315 free (cinfo.bitmask);
7316 free (cinfo.hashcodes);
7317 return FALSE;
7318 }
7319
7320 s->contents = contents;
7321 bfd_put_32 (output_bfd, bucketcount, contents);
7322 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7323 bfd_put_32 (output_bfd, maskwords, contents + 8);
7324 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7325 contents += 16 + cinfo.maskbits / 8;
7326
7327 for (i = 0; i < bucketcount; ++i)
7328 {
7329 if (cinfo.counts[i] == 0)
7330 bfd_put_32 (output_bfd, 0, contents);
7331 else
7332 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7333 contents += 4;
7334 }
7335
7336 cinfo.contents = contents;
7337
7338 /* Renumber dynamic symbols, populate .gnu.hash section. */
7339 elf_link_hash_traverse (elf_hash_table (info),
7340 elf_renumber_gnu_hash_syms, &cinfo);
7341
7342 contents = s->contents + 16;
7343 for (i = 0; i < maskwords; ++i)
7344 {
7345 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7346 contents);
7347 contents += bed->s->arch_size / 8;
7348 }
7349
7350 free (cinfo.bitmask);
7351 free (cinfo.hashcodes);
7352 }
7353 }
7354
7355 s = bfd_get_linker_section (dynobj, ".dynstr");
7356 BFD_ASSERT (s != NULL);
7357
7358 elf_finalize_dynstr (output_bfd, info);
7359
7360 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7361
7362 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7363 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7364 return FALSE;
7365 }
7366
7367 return TRUE;
7368 }
7369
7370 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7372
7373 static void
7374 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7375 asection *sec)
7376 {
7377 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7378 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7379 }
7380
7381 /* Finish SHF_MERGE section merging. */
7382
7383 bfd_boolean
7384 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7385 {
7386 bfd *ibfd;
7387 asection *sec;
7388
7389 if (!is_elf_hash_table (info->hash))
7390 return FALSE;
7391
7392 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7393 if ((ibfd->flags & DYNAMIC) == 0
7394 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7395 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7396 == get_elf_backend_data (obfd)->s->elfclass))
7397 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7398 if ((sec->flags & SEC_MERGE) != 0
7399 && !bfd_is_abs_section (sec->output_section))
7400 {
7401 struct bfd_elf_section_data *secdata;
7402
7403 secdata = elf_section_data (sec);
7404 if (! _bfd_add_merge_section (obfd,
7405 &elf_hash_table (info)->merge_info,
7406 sec, &secdata->sec_info))
7407 return FALSE;
7408 else if (secdata->sec_info)
7409 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7410 }
7411
7412 if (elf_hash_table (info)->merge_info != NULL)
7413 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7414 merge_sections_remove_hook);
7415 return TRUE;
7416 }
7417
7418 /* Create an entry in an ELF linker hash table. */
7419
7420 struct bfd_hash_entry *
7421 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7422 struct bfd_hash_table *table,
7423 const char *string)
7424 {
7425 /* Allocate the structure if it has not already been allocated by a
7426 subclass. */
7427 if (entry == NULL)
7428 {
7429 entry = (struct bfd_hash_entry *)
7430 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7431 if (entry == NULL)
7432 return entry;
7433 }
7434
7435 /* Call the allocation method of the superclass. */
7436 entry = _bfd_link_hash_newfunc (entry, table, string);
7437 if (entry != NULL)
7438 {
7439 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7440 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7441
7442 /* Set local fields. */
7443 ret->indx = -1;
7444 ret->dynindx = -1;
7445 ret->got = htab->init_got_refcount;
7446 ret->plt = htab->init_plt_refcount;
7447 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7448 - offsetof (struct elf_link_hash_entry, size)));
7449 /* Assume that we have been called by a non-ELF symbol reader.
7450 This flag is then reset by the code which reads an ELF input
7451 file. This ensures that a symbol created by a non-ELF symbol
7452 reader will have the flag set correctly. */
7453 ret->non_elf = 1;
7454 }
7455
7456 return entry;
7457 }
7458
7459 /* Copy data from an indirect symbol to its direct symbol, hiding the
7460 old indirect symbol. Also used for copying flags to a weakdef. */
7461
7462 void
7463 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7464 struct elf_link_hash_entry *dir,
7465 struct elf_link_hash_entry *ind)
7466 {
7467 struct elf_link_hash_table *htab;
7468
7469 /* Copy down any references that we may have already seen to the
7470 symbol which just became indirect. */
7471
7472 if (dir->versioned != versioned_hidden)
7473 dir->ref_dynamic |= ind->ref_dynamic;
7474 dir->ref_regular |= ind->ref_regular;
7475 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7476 dir->non_got_ref |= ind->non_got_ref;
7477 dir->needs_plt |= ind->needs_plt;
7478 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7479
7480 if (ind->root.type != bfd_link_hash_indirect)
7481 return;
7482
7483 /* Copy over the global and procedure linkage table refcount entries.
7484 These may have been already set up by a check_relocs routine. */
7485 htab = elf_hash_table (info);
7486 if (ind->got.refcount > htab->init_got_refcount.refcount)
7487 {
7488 if (dir->got.refcount < 0)
7489 dir->got.refcount = 0;
7490 dir->got.refcount += ind->got.refcount;
7491 ind->got.refcount = htab->init_got_refcount.refcount;
7492 }
7493
7494 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7495 {
7496 if (dir->plt.refcount < 0)
7497 dir->plt.refcount = 0;
7498 dir->plt.refcount += ind->plt.refcount;
7499 ind->plt.refcount = htab->init_plt_refcount.refcount;
7500 }
7501
7502 if (ind->dynindx != -1)
7503 {
7504 if (dir->dynindx != -1)
7505 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7506 dir->dynindx = ind->dynindx;
7507 dir->dynstr_index = ind->dynstr_index;
7508 ind->dynindx = -1;
7509 ind->dynstr_index = 0;
7510 }
7511 }
7512
7513 void
7514 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7515 struct elf_link_hash_entry *h,
7516 bfd_boolean force_local)
7517 {
7518 /* STT_GNU_IFUNC symbol must go through PLT. */
7519 if (h->type != STT_GNU_IFUNC)
7520 {
7521 h->plt = elf_hash_table (info)->init_plt_offset;
7522 h->needs_plt = 0;
7523 }
7524 if (force_local)
7525 {
7526 h->forced_local = 1;
7527 if (h->dynindx != -1)
7528 {
7529 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7530 h->dynstr_index);
7531 h->dynindx = -1;
7532 h->dynstr_index = 0;
7533 }
7534 }
7535 }
7536
7537 /* Hide a symbol. */
7538
7539 void
7540 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7541 struct bfd_link_info *info,
7542 struct bfd_link_hash_entry *h)
7543 {
7544 if (is_elf_hash_table (info->hash))
7545 {
7546 const struct elf_backend_data *bed
7547 = get_elf_backend_data (output_bfd);
7548 struct elf_link_hash_entry *eh
7549 = (struct elf_link_hash_entry *) h;
7550 bed->elf_backend_hide_symbol (info, eh, TRUE);
7551 eh->def_dynamic = 0;
7552 eh->ref_dynamic = 0;
7553 eh->dynamic_def = 0;
7554 }
7555 }
7556
7557 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7558 caller. */
7559
7560 bfd_boolean
7561 _bfd_elf_link_hash_table_init
7562 (struct elf_link_hash_table *table,
7563 bfd *abfd,
7564 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7565 struct bfd_hash_table *,
7566 const char *),
7567 unsigned int entsize,
7568 enum elf_target_id target_id)
7569 {
7570 bfd_boolean ret;
7571 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7572
7573 table->init_got_refcount.refcount = can_refcount - 1;
7574 table->init_plt_refcount.refcount = can_refcount - 1;
7575 table->init_got_offset.offset = -(bfd_vma) 1;
7576 table->init_plt_offset.offset = -(bfd_vma) 1;
7577 /* The first dynamic symbol is a dummy. */
7578 table->dynsymcount = 1;
7579
7580 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7581
7582 table->root.type = bfd_link_elf_hash_table;
7583 table->hash_table_id = target_id;
7584
7585 return ret;
7586 }
7587
7588 /* Create an ELF linker hash table. */
7589
7590 struct bfd_link_hash_table *
7591 _bfd_elf_link_hash_table_create (bfd *abfd)
7592 {
7593 struct elf_link_hash_table *ret;
7594 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7595
7596 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7597 if (ret == NULL)
7598 return NULL;
7599
7600 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7601 sizeof (struct elf_link_hash_entry),
7602 GENERIC_ELF_DATA))
7603 {
7604 free (ret);
7605 return NULL;
7606 }
7607 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7608
7609 return &ret->root;
7610 }
7611
7612 /* Destroy an ELF linker hash table. */
7613
7614 void
7615 _bfd_elf_link_hash_table_free (bfd *obfd)
7616 {
7617 struct elf_link_hash_table *htab;
7618
7619 htab = (struct elf_link_hash_table *) obfd->link.hash;
7620 if (htab->dynstr != NULL)
7621 _bfd_elf_strtab_free (htab->dynstr);
7622 _bfd_merge_sections_free (htab->merge_info);
7623 _bfd_generic_link_hash_table_free (obfd);
7624 }
7625
7626 /* This is a hook for the ELF emulation code in the generic linker to
7627 tell the backend linker what file name to use for the DT_NEEDED
7628 entry for a dynamic object. */
7629
7630 void
7631 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7632 {
7633 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7634 && bfd_get_format (abfd) == bfd_object)
7635 elf_dt_name (abfd) = name;
7636 }
7637
7638 int
7639 bfd_elf_get_dyn_lib_class (bfd *abfd)
7640 {
7641 int lib_class;
7642 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7643 && bfd_get_format (abfd) == bfd_object)
7644 lib_class = elf_dyn_lib_class (abfd);
7645 else
7646 lib_class = 0;
7647 return lib_class;
7648 }
7649
7650 void
7651 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7652 {
7653 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7654 && bfd_get_format (abfd) == bfd_object)
7655 elf_dyn_lib_class (abfd) = lib_class;
7656 }
7657
7658 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7659 the linker ELF emulation code. */
7660
7661 struct bfd_link_needed_list *
7662 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7663 struct bfd_link_info *info)
7664 {
7665 if (! is_elf_hash_table (info->hash))
7666 return NULL;
7667 return elf_hash_table (info)->needed;
7668 }
7669
7670 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7671 hook for the linker ELF emulation code. */
7672
7673 struct bfd_link_needed_list *
7674 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7675 struct bfd_link_info *info)
7676 {
7677 if (! is_elf_hash_table (info->hash))
7678 return NULL;
7679 return elf_hash_table (info)->runpath;
7680 }
7681
7682 /* Get the name actually used for a dynamic object for a link. This
7683 is the SONAME entry if there is one. Otherwise, it is the string
7684 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7685
7686 const char *
7687 bfd_elf_get_dt_soname (bfd *abfd)
7688 {
7689 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7690 && bfd_get_format (abfd) == bfd_object)
7691 return elf_dt_name (abfd);
7692 return NULL;
7693 }
7694
7695 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7696 the ELF linker emulation code. */
7697
7698 bfd_boolean
7699 bfd_elf_get_bfd_needed_list (bfd *abfd,
7700 struct bfd_link_needed_list **pneeded)
7701 {
7702 asection *s;
7703 bfd_byte *dynbuf = NULL;
7704 unsigned int elfsec;
7705 unsigned long shlink;
7706 bfd_byte *extdyn, *extdynend;
7707 size_t extdynsize;
7708 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7709
7710 *pneeded = NULL;
7711
7712 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7713 || bfd_get_format (abfd) != bfd_object)
7714 return TRUE;
7715
7716 s = bfd_get_section_by_name (abfd, ".dynamic");
7717 if (s == NULL || s->size == 0)
7718 return TRUE;
7719
7720 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7721 goto error_return;
7722
7723 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7724 if (elfsec == SHN_BAD)
7725 goto error_return;
7726
7727 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7728
7729 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7730 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7731
7732 extdyn = dynbuf;
7733 extdynend = extdyn + s->size;
7734 for (; extdyn < extdynend; extdyn += extdynsize)
7735 {
7736 Elf_Internal_Dyn dyn;
7737
7738 (*swap_dyn_in) (abfd, extdyn, &dyn);
7739
7740 if (dyn.d_tag == DT_NULL)
7741 break;
7742
7743 if (dyn.d_tag == DT_NEEDED)
7744 {
7745 const char *string;
7746 struct bfd_link_needed_list *l;
7747 unsigned int tagv = dyn.d_un.d_val;
7748 bfd_size_type amt;
7749
7750 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7751 if (string == NULL)
7752 goto error_return;
7753
7754 amt = sizeof *l;
7755 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7756 if (l == NULL)
7757 goto error_return;
7758
7759 l->by = abfd;
7760 l->name = string;
7761 l->next = *pneeded;
7762 *pneeded = l;
7763 }
7764 }
7765
7766 free (dynbuf);
7767
7768 return TRUE;
7769
7770 error_return:
7771 if (dynbuf != NULL)
7772 free (dynbuf);
7773 return FALSE;
7774 }
7775
7776 struct elf_symbuf_symbol
7777 {
7778 unsigned long st_name; /* Symbol name, index in string tbl */
7779 unsigned char st_info; /* Type and binding attributes */
7780 unsigned char st_other; /* Visibilty, and target specific */
7781 };
7782
7783 struct elf_symbuf_head
7784 {
7785 struct elf_symbuf_symbol *ssym;
7786 size_t count;
7787 unsigned int st_shndx;
7788 };
7789
7790 struct elf_symbol
7791 {
7792 union
7793 {
7794 Elf_Internal_Sym *isym;
7795 struct elf_symbuf_symbol *ssym;
7796 } u;
7797 const char *name;
7798 };
7799
7800 /* Sort references to symbols by ascending section number. */
7801
7802 static int
7803 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7804 {
7805 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7806 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7807
7808 return s1->st_shndx - s2->st_shndx;
7809 }
7810
7811 static int
7812 elf_sym_name_compare (const void *arg1, const void *arg2)
7813 {
7814 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7815 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7816 return strcmp (s1->name, s2->name);
7817 }
7818
7819 static struct elf_symbuf_head *
7820 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7821 {
7822 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7823 struct elf_symbuf_symbol *ssym;
7824 struct elf_symbuf_head *ssymbuf, *ssymhead;
7825 size_t i, shndx_count, total_size;
7826
7827 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7828 if (indbuf == NULL)
7829 return NULL;
7830
7831 for (ind = indbuf, i = 0; i < symcount; i++)
7832 if (isymbuf[i].st_shndx != SHN_UNDEF)
7833 *ind++ = &isymbuf[i];
7834 indbufend = ind;
7835
7836 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7837 elf_sort_elf_symbol);
7838
7839 shndx_count = 0;
7840 if (indbufend > indbuf)
7841 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7842 if (ind[0]->st_shndx != ind[1]->st_shndx)
7843 shndx_count++;
7844
7845 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7846 + (indbufend - indbuf) * sizeof (*ssym));
7847 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7848 if (ssymbuf == NULL)
7849 {
7850 free (indbuf);
7851 return NULL;
7852 }
7853
7854 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7855 ssymbuf->ssym = NULL;
7856 ssymbuf->count = shndx_count;
7857 ssymbuf->st_shndx = 0;
7858 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7859 {
7860 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7861 {
7862 ssymhead++;
7863 ssymhead->ssym = ssym;
7864 ssymhead->count = 0;
7865 ssymhead->st_shndx = (*ind)->st_shndx;
7866 }
7867 ssym->st_name = (*ind)->st_name;
7868 ssym->st_info = (*ind)->st_info;
7869 ssym->st_other = (*ind)->st_other;
7870 ssymhead->count++;
7871 }
7872 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7873 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7874 == total_size));
7875
7876 free (indbuf);
7877 return ssymbuf;
7878 }
7879
7880 /* Check if 2 sections define the same set of local and global
7881 symbols. */
7882
7883 static bfd_boolean
7884 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7885 struct bfd_link_info *info)
7886 {
7887 bfd *bfd1, *bfd2;
7888 const struct elf_backend_data *bed1, *bed2;
7889 Elf_Internal_Shdr *hdr1, *hdr2;
7890 size_t symcount1, symcount2;
7891 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7892 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7893 Elf_Internal_Sym *isym, *isymend;
7894 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7895 size_t count1, count2, i;
7896 unsigned int shndx1, shndx2;
7897 bfd_boolean result;
7898
7899 bfd1 = sec1->owner;
7900 bfd2 = sec2->owner;
7901
7902 /* Both sections have to be in ELF. */
7903 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7904 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7905 return FALSE;
7906
7907 if (elf_section_type (sec1) != elf_section_type (sec2))
7908 return FALSE;
7909
7910 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7911 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7912 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7913 return FALSE;
7914
7915 bed1 = get_elf_backend_data (bfd1);
7916 bed2 = get_elf_backend_data (bfd2);
7917 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7918 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7919 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7920 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7921
7922 if (symcount1 == 0 || symcount2 == 0)
7923 return FALSE;
7924
7925 result = FALSE;
7926 isymbuf1 = NULL;
7927 isymbuf2 = NULL;
7928 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7929 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7930
7931 if (ssymbuf1 == NULL)
7932 {
7933 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7934 NULL, NULL, NULL);
7935 if (isymbuf1 == NULL)
7936 goto done;
7937
7938 if (!info->reduce_memory_overheads)
7939 elf_tdata (bfd1)->symbuf = ssymbuf1
7940 = elf_create_symbuf (symcount1, isymbuf1);
7941 }
7942
7943 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7944 {
7945 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7946 NULL, NULL, NULL);
7947 if (isymbuf2 == NULL)
7948 goto done;
7949
7950 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7951 elf_tdata (bfd2)->symbuf = ssymbuf2
7952 = elf_create_symbuf (symcount2, isymbuf2);
7953 }
7954
7955 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7956 {
7957 /* Optimized faster version. */
7958 size_t lo, hi, mid;
7959 struct elf_symbol *symp;
7960 struct elf_symbuf_symbol *ssym, *ssymend;
7961
7962 lo = 0;
7963 hi = ssymbuf1->count;
7964 ssymbuf1++;
7965 count1 = 0;
7966 while (lo < hi)
7967 {
7968 mid = (lo + hi) / 2;
7969 if (shndx1 < ssymbuf1[mid].st_shndx)
7970 hi = mid;
7971 else if (shndx1 > ssymbuf1[mid].st_shndx)
7972 lo = mid + 1;
7973 else
7974 {
7975 count1 = ssymbuf1[mid].count;
7976 ssymbuf1 += mid;
7977 break;
7978 }
7979 }
7980
7981 lo = 0;
7982 hi = ssymbuf2->count;
7983 ssymbuf2++;
7984 count2 = 0;
7985 while (lo < hi)
7986 {
7987 mid = (lo + hi) / 2;
7988 if (shndx2 < ssymbuf2[mid].st_shndx)
7989 hi = mid;
7990 else if (shndx2 > ssymbuf2[mid].st_shndx)
7991 lo = mid + 1;
7992 else
7993 {
7994 count2 = ssymbuf2[mid].count;
7995 ssymbuf2 += mid;
7996 break;
7997 }
7998 }
7999
8000 if (count1 == 0 || count2 == 0 || count1 != count2)
8001 goto done;
8002
8003 symtable1
8004 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8005 symtable2
8006 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8007 if (symtable1 == NULL || symtable2 == NULL)
8008 goto done;
8009
8010 symp = symtable1;
8011 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8012 ssym < ssymend; ssym++, symp++)
8013 {
8014 symp->u.ssym = ssym;
8015 symp->name = bfd_elf_string_from_elf_section (bfd1,
8016 hdr1->sh_link,
8017 ssym->st_name);
8018 }
8019
8020 symp = symtable2;
8021 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8022 ssym < ssymend; ssym++, symp++)
8023 {
8024 symp->u.ssym = ssym;
8025 symp->name = bfd_elf_string_from_elf_section (bfd2,
8026 hdr2->sh_link,
8027 ssym->st_name);
8028 }
8029
8030 /* Sort symbol by name. */
8031 qsort (symtable1, count1, sizeof (struct elf_symbol),
8032 elf_sym_name_compare);
8033 qsort (symtable2, count1, sizeof (struct elf_symbol),
8034 elf_sym_name_compare);
8035
8036 for (i = 0; i < count1; i++)
8037 /* Two symbols must have the same binding, type and name. */
8038 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8039 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8040 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8041 goto done;
8042
8043 result = TRUE;
8044 goto done;
8045 }
8046
8047 symtable1 = (struct elf_symbol *)
8048 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8049 symtable2 = (struct elf_symbol *)
8050 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8051 if (symtable1 == NULL || symtable2 == NULL)
8052 goto done;
8053
8054 /* Count definitions in the section. */
8055 count1 = 0;
8056 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8057 if (isym->st_shndx == shndx1)
8058 symtable1[count1++].u.isym = isym;
8059
8060 count2 = 0;
8061 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8062 if (isym->st_shndx == shndx2)
8063 symtable2[count2++].u.isym = isym;
8064
8065 if (count1 == 0 || count2 == 0 || count1 != count2)
8066 goto done;
8067
8068 for (i = 0; i < count1; i++)
8069 symtable1[i].name
8070 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8071 symtable1[i].u.isym->st_name);
8072
8073 for (i = 0; i < count2; i++)
8074 symtable2[i].name
8075 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8076 symtable2[i].u.isym->st_name);
8077
8078 /* Sort symbol by name. */
8079 qsort (symtable1, count1, sizeof (struct elf_symbol),
8080 elf_sym_name_compare);
8081 qsort (symtable2, count1, sizeof (struct elf_symbol),
8082 elf_sym_name_compare);
8083
8084 for (i = 0; i < count1; i++)
8085 /* Two symbols must have the same binding, type and name. */
8086 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8087 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8088 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8089 goto done;
8090
8091 result = TRUE;
8092
8093 done:
8094 if (symtable1)
8095 free (symtable1);
8096 if (symtable2)
8097 free (symtable2);
8098 if (isymbuf1)
8099 free (isymbuf1);
8100 if (isymbuf2)
8101 free (isymbuf2);
8102
8103 return result;
8104 }
8105
8106 /* Return TRUE if 2 section types are compatible. */
8107
8108 bfd_boolean
8109 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8110 bfd *bbfd, const asection *bsec)
8111 {
8112 if (asec == NULL
8113 || bsec == NULL
8114 || abfd->xvec->flavour != bfd_target_elf_flavour
8115 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8116 return TRUE;
8117
8118 return elf_section_type (asec) == elf_section_type (bsec);
8119 }
8120
8121 /* Final phase of ELF linker. */
8123
8124 /* A structure we use to avoid passing large numbers of arguments. */
8125
8126 struct elf_final_link_info
8127 {
8128 /* General link information. */
8129 struct bfd_link_info *info;
8130 /* Output BFD. */
8131 bfd *output_bfd;
8132 /* Symbol string table. */
8133 struct elf_strtab_hash *symstrtab;
8134 /* .hash section. */
8135 asection *hash_sec;
8136 /* symbol version section (.gnu.version). */
8137 asection *symver_sec;
8138 /* Buffer large enough to hold contents of any section. */
8139 bfd_byte *contents;
8140 /* Buffer large enough to hold external relocs of any section. */
8141 void *external_relocs;
8142 /* Buffer large enough to hold internal relocs of any section. */
8143 Elf_Internal_Rela *internal_relocs;
8144 /* Buffer large enough to hold external local symbols of any input
8145 BFD. */
8146 bfd_byte *external_syms;
8147 /* And a buffer for symbol section indices. */
8148 Elf_External_Sym_Shndx *locsym_shndx;
8149 /* Buffer large enough to hold internal local symbols of any input
8150 BFD. */
8151 Elf_Internal_Sym *internal_syms;
8152 /* Array large enough to hold a symbol index for each local symbol
8153 of any input BFD. */
8154 long *indices;
8155 /* Array large enough to hold a section pointer for each local
8156 symbol of any input BFD. */
8157 asection **sections;
8158 /* Buffer for SHT_SYMTAB_SHNDX section. */
8159 Elf_External_Sym_Shndx *symshndxbuf;
8160 /* Number of STT_FILE syms seen. */
8161 size_t filesym_count;
8162 };
8163
8164 /* This struct is used to pass information to elf_link_output_extsym. */
8165
8166 struct elf_outext_info
8167 {
8168 bfd_boolean failed;
8169 bfd_boolean localsyms;
8170 bfd_boolean file_sym_done;
8171 struct elf_final_link_info *flinfo;
8172 };
8173
8174
8175 /* Support for evaluating a complex relocation.
8176
8177 Complex relocations are generalized, self-describing relocations. The
8178 implementation of them consists of two parts: complex symbols, and the
8179 relocations themselves.
8180
8181 The relocations are use a reserved elf-wide relocation type code (R_RELC
8182 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8183 information (start bit, end bit, word width, etc) into the addend. This
8184 information is extracted from CGEN-generated operand tables within gas.
8185
8186 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8187 internal) representing prefix-notation expressions, including but not
8188 limited to those sorts of expressions normally encoded as addends in the
8189 addend field. The symbol mangling format is:
8190
8191 <node> := <literal>
8192 | <unary-operator> ':' <node>
8193 | <binary-operator> ':' <node> ':' <node>
8194 ;
8195
8196 <literal> := 's' <digits=N> ':' <N character symbol name>
8197 | 'S' <digits=N> ':' <N character section name>
8198 | '#' <hexdigits>
8199 ;
8200
8201 <binary-operator> := as in C
8202 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8203
8204 static void
8205 set_symbol_value (bfd *bfd_with_globals,
8206 Elf_Internal_Sym *isymbuf,
8207 size_t locsymcount,
8208 size_t symidx,
8209 bfd_vma val)
8210 {
8211 struct elf_link_hash_entry **sym_hashes;
8212 struct elf_link_hash_entry *h;
8213 size_t extsymoff = locsymcount;
8214
8215 if (symidx < locsymcount)
8216 {
8217 Elf_Internal_Sym *sym;
8218
8219 sym = isymbuf + symidx;
8220 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8221 {
8222 /* It is a local symbol: move it to the
8223 "absolute" section and give it a value. */
8224 sym->st_shndx = SHN_ABS;
8225 sym->st_value = val;
8226 return;
8227 }
8228 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8229 extsymoff = 0;
8230 }
8231
8232 /* It is a global symbol: set its link type
8233 to "defined" and give it a value. */
8234
8235 sym_hashes = elf_sym_hashes (bfd_with_globals);
8236 h = sym_hashes [symidx - extsymoff];
8237 while (h->root.type == bfd_link_hash_indirect
8238 || h->root.type == bfd_link_hash_warning)
8239 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8240 h->root.type = bfd_link_hash_defined;
8241 h->root.u.def.value = val;
8242 h->root.u.def.section = bfd_abs_section_ptr;
8243 }
8244
8245 static bfd_boolean
8246 resolve_symbol (const char *name,
8247 bfd *input_bfd,
8248 struct elf_final_link_info *flinfo,
8249 bfd_vma *result,
8250 Elf_Internal_Sym *isymbuf,
8251 size_t locsymcount)
8252 {
8253 Elf_Internal_Sym *sym;
8254 struct bfd_link_hash_entry *global_entry;
8255 const char *candidate = NULL;
8256 Elf_Internal_Shdr *symtab_hdr;
8257 size_t i;
8258
8259 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8260
8261 for (i = 0; i < locsymcount; ++ i)
8262 {
8263 sym = isymbuf + i;
8264
8265 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8266 continue;
8267
8268 candidate = bfd_elf_string_from_elf_section (input_bfd,
8269 symtab_hdr->sh_link,
8270 sym->st_name);
8271 #ifdef DEBUG
8272 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8273 name, candidate, (unsigned long) sym->st_value);
8274 #endif
8275 if (candidate && strcmp (candidate, name) == 0)
8276 {
8277 asection *sec = flinfo->sections [i];
8278
8279 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8280 *result += sec->output_offset + sec->output_section->vma;
8281 #ifdef DEBUG
8282 printf ("Found symbol with value %8.8lx\n",
8283 (unsigned long) *result);
8284 #endif
8285 return TRUE;
8286 }
8287 }
8288
8289 /* Hmm, haven't found it yet. perhaps it is a global. */
8290 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8291 FALSE, FALSE, TRUE);
8292 if (!global_entry)
8293 return FALSE;
8294
8295 if (global_entry->type == bfd_link_hash_defined
8296 || global_entry->type == bfd_link_hash_defweak)
8297 {
8298 *result = (global_entry->u.def.value
8299 + global_entry->u.def.section->output_section->vma
8300 + global_entry->u.def.section->output_offset);
8301 #ifdef DEBUG
8302 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8303 global_entry->root.string, (unsigned long) *result);
8304 #endif
8305 return TRUE;
8306 }
8307
8308 return FALSE;
8309 }
8310
8311 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8312 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8313 names like "foo.end" which is the end address of section "foo". */
8314
8315 static bfd_boolean
8316 resolve_section (const char *name,
8317 asection *sections,
8318 bfd_vma *result,
8319 bfd * abfd)
8320 {
8321 asection *curr;
8322 unsigned int len;
8323
8324 for (curr = sections; curr; curr = curr->next)
8325 if (strcmp (curr->name, name) == 0)
8326 {
8327 *result = curr->vma;
8328 return TRUE;
8329 }
8330
8331 /* Hmm. still haven't found it. try pseudo-section names. */
8332 /* FIXME: This could be coded more efficiently... */
8333 for (curr = sections; curr; curr = curr->next)
8334 {
8335 len = strlen (curr->name);
8336 if (len > strlen (name))
8337 continue;
8338
8339 if (strncmp (curr->name, name, len) == 0)
8340 {
8341 if (strncmp (".end", name + len, 4) == 0)
8342 {
8343 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8344 return TRUE;
8345 }
8346
8347 /* Insert more pseudo-section names here, if you like. */
8348 }
8349 }
8350
8351 return FALSE;
8352 }
8353
8354 static void
8355 undefined_reference (const char *reftype, const char *name)
8356 {
8357 /* xgettext:c-format */
8358 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8359 reftype, name);
8360 }
8361
8362 static bfd_boolean
8363 eval_symbol (bfd_vma *result,
8364 const char **symp,
8365 bfd *input_bfd,
8366 struct elf_final_link_info *flinfo,
8367 bfd_vma dot,
8368 Elf_Internal_Sym *isymbuf,
8369 size_t locsymcount,
8370 int signed_p)
8371 {
8372 size_t len;
8373 size_t symlen;
8374 bfd_vma a;
8375 bfd_vma b;
8376 char symbuf[4096];
8377 const char *sym = *symp;
8378 const char *symend;
8379 bfd_boolean symbol_is_section = FALSE;
8380
8381 len = strlen (sym);
8382 symend = sym + len;
8383
8384 if (len < 1 || len > sizeof (symbuf))
8385 {
8386 bfd_set_error (bfd_error_invalid_operation);
8387 return FALSE;
8388 }
8389
8390 switch (* sym)
8391 {
8392 case '.':
8393 *result = dot;
8394 *symp = sym + 1;
8395 return TRUE;
8396
8397 case '#':
8398 ++sym;
8399 *result = strtoul (sym, (char **) symp, 16);
8400 return TRUE;
8401
8402 case 'S':
8403 symbol_is_section = TRUE;
8404 /* Fall through. */
8405 case 's':
8406 ++sym;
8407 symlen = strtol (sym, (char **) symp, 10);
8408 sym = *symp + 1; /* Skip the trailing ':'. */
8409
8410 if (symend < sym || symlen + 1 > sizeof (symbuf))
8411 {
8412 bfd_set_error (bfd_error_invalid_operation);
8413 return FALSE;
8414 }
8415
8416 memcpy (symbuf, sym, symlen);
8417 symbuf[symlen] = '\0';
8418 *symp = sym + symlen;
8419
8420 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8421 the symbol as a section, or vice-versa. so we're pretty liberal in our
8422 interpretation here; section means "try section first", not "must be a
8423 section", and likewise with symbol. */
8424
8425 if (symbol_is_section)
8426 {
8427 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8428 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8429 isymbuf, locsymcount))
8430 {
8431 undefined_reference ("section", symbuf);
8432 return FALSE;
8433 }
8434 }
8435 else
8436 {
8437 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8438 isymbuf, locsymcount)
8439 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8440 result, input_bfd))
8441 {
8442 undefined_reference ("symbol", symbuf);
8443 return FALSE;
8444 }
8445 }
8446
8447 return TRUE;
8448
8449 /* All that remains are operators. */
8450
8451 #define UNARY_OP(op) \
8452 if (strncmp (sym, #op, strlen (#op)) == 0) \
8453 { \
8454 sym += strlen (#op); \
8455 if (*sym == ':') \
8456 ++sym; \
8457 *symp = sym; \
8458 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8459 isymbuf, locsymcount, signed_p)) \
8460 return FALSE; \
8461 if (signed_p) \
8462 *result = op ((bfd_signed_vma) a); \
8463 else \
8464 *result = op a; \
8465 return TRUE; \
8466 }
8467
8468 #define BINARY_OP(op) \
8469 if (strncmp (sym, #op, strlen (#op)) == 0) \
8470 { \
8471 sym += strlen (#op); \
8472 if (*sym == ':') \
8473 ++sym; \
8474 *symp = sym; \
8475 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8476 isymbuf, locsymcount, signed_p)) \
8477 return FALSE; \
8478 ++*symp; \
8479 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8480 isymbuf, locsymcount, signed_p)) \
8481 return FALSE; \
8482 if (signed_p) \
8483 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8484 else \
8485 *result = a op b; \
8486 return TRUE; \
8487 }
8488
8489 default:
8490 UNARY_OP (0-);
8491 BINARY_OP (<<);
8492 BINARY_OP (>>);
8493 BINARY_OP (==);
8494 BINARY_OP (!=);
8495 BINARY_OP (<=);
8496 BINARY_OP (>=);
8497 BINARY_OP (&&);
8498 BINARY_OP (||);
8499 UNARY_OP (~);
8500 UNARY_OP (!);
8501 BINARY_OP (*);
8502 BINARY_OP (/);
8503 BINARY_OP (%);
8504 BINARY_OP (^);
8505 BINARY_OP (|);
8506 BINARY_OP (&);
8507 BINARY_OP (+);
8508 BINARY_OP (-);
8509 BINARY_OP (<);
8510 BINARY_OP (>);
8511 #undef UNARY_OP
8512 #undef BINARY_OP
8513 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8514 bfd_set_error (bfd_error_invalid_operation);
8515 return FALSE;
8516 }
8517 }
8518
8519 static void
8520 put_value (bfd_vma size,
8521 unsigned long chunksz,
8522 bfd *input_bfd,
8523 bfd_vma x,
8524 bfd_byte *location)
8525 {
8526 location += (size - chunksz);
8527
8528 for (; size; size -= chunksz, location -= chunksz)
8529 {
8530 switch (chunksz)
8531 {
8532 case 1:
8533 bfd_put_8 (input_bfd, x, location);
8534 x >>= 8;
8535 break;
8536 case 2:
8537 bfd_put_16 (input_bfd, x, location);
8538 x >>= 16;
8539 break;
8540 case 4:
8541 bfd_put_32 (input_bfd, x, location);
8542 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8543 x >>= 16;
8544 x >>= 16;
8545 break;
8546 #ifdef BFD64
8547 case 8:
8548 bfd_put_64 (input_bfd, x, location);
8549 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8550 x >>= 32;
8551 x >>= 32;
8552 break;
8553 #endif
8554 default:
8555 abort ();
8556 break;
8557 }
8558 }
8559 }
8560
8561 static bfd_vma
8562 get_value (bfd_vma size,
8563 unsigned long chunksz,
8564 bfd *input_bfd,
8565 bfd_byte *location)
8566 {
8567 int shift;
8568 bfd_vma x = 0;
8569
8570 /* Sanity checks. */
8571 BFD_ASSERT (chunksz <= sizeof (x)
8572 && size >= chunksz
8573 && chunksz != 0
8574 && (size % chunksz) == 0
8575 && input_bfd != NULL
8576 && location != NULL);
8577
8578 if (chunksz == sizeof (x))
8579 {
8580 BFD_ASSERT (size == chunksz);
8581
8582 /* Make sure that we do not perform an undefined shift operation.
8583 We know that size == chunksz so there will only be one iteration
8584 of the loop below. */
8585 shift = 0;
8586 }
8587 else
8588 shift = 8 * chunksz;
8589
8590 for (; size; size -= chunksz, location += chunksz)
8591 {
8592 switch (chunksz)
8593 {
8594 case 1:
8595 x = (x << shift) | bfd_get_8 (input_bfd, location);
8596 break;
8597 case 2:
8598 x = (x << shift) | bfd_get_16 (input_bfd, location);
8599 break;
8600 case 4:
8601 x = (x << shift) | bfd_get_32 (input_bfd, location);
8602 break;
8603 #ifdef BFD64
8604 case 8:
8605 x = (x << shift) | bfd_get_64 (input_bfd, location);
8606 break;
8607 #endif
8608 default:
8609 abort ();
8610 }
8611 }
8612 return x;
8613 }
8614
8615 static void
8616 decode_complex_addend (unsigned long *start, /* in bits */
8617 unsigned long *oplen, /* in bits */
8618 unsigned long *len, /* in bits */
8619 unsigned long *wordsz, /* in bytes */
8620 unsigned long *chunksz, /* in bytes */
8621 unsigned long *lsb0_p,
8622 unsigned long *signed_p,
8623 unsigned long *trunc_p,
8624 unsigned long encoded)
8625 {
8626 * start = encoded & 0x3F;
8627 * len = (encoded >> 6) & 0x3F;
8628 * oplen = (encoded >> 12) & 0x3F;
8629 * wordsz = (encoded >> 18) & 0xF;
8630 * chunksz = (encoded >> 22) & 0xF;
8631 * lsb0_p = (encoded >> 27) & 1;
8632 * signed_p = (encoded >> 28) & 1;
8633 * trunc_p = (encoded >> 29) & 1;
8634 }
8635
8636 bfd_reloc_status_type
8637 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8638 asection *input_section ATTRIBUTE_UNUSED,
8639 bfd_byte *contents,
8640 Elf_Internal_Rela *rel,
8641 bfd_vma relocation)
8642 {
8643 bfd_vma shift, x, mask;
8644 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8645 bfd_reloc_status_type r;
8646
8647 /* Perform this reloc, since it is complex.
8648 (this is not to say that it necessarily refers to a complex
8649 symbol; merely that it is a self-describing CGEN based reloc.
8650 i.e. the addend has the complete reloc information (bit start, end,
8651 word size, etc) encoded within it.). */
8652
8653 decode_complex_addend (&start, &oplen, &len, &wordsz,
8654 &chunksz, &lsb0_p, &signed_p,
8655 &trunc_p, rel->r_addend);
8656
8657 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8658
8659 if (lsb0_p)
8660 shift = (start + 1) - len;
8661 else
8662 shift = (8 * wordsz) - (start + len);
8663
8664 x = get_value (wordsz, chunksz, input_bfd,
8665 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8666
8667 #ifdef DEBUG
8668 printf ("Doing complex reloc: "
8669 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8670 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8671 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8672 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8673 oplen, (unsigned long) x, (unsigned long) mask,
8674 (unsigned long) relocation);
8675 #endif
8676
8677 r = bfd_reloc_ok;
8678 if (! trunc_p)
8679 /* Now do an overflow check. */
8680 r = bfd_check_overflow ((signed_p
8681 ? complain_overflow_signed
8682 : complain_overflow_unsigned),
8683 len, 0, (8 * wordsz),
8684 relocation);
8685
8686 /* Do the deed. */
8687 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8688
8689 #ifdef DEBUG
8690 printf (" relocation: %8.8lx\n"
8691 " shifted mask: %8.8lx\n"
8692 " shifted/masked reloc: %8.8lx\n"
8693 " result: %8.8lx\n",
8694 (unsigned long) relocation, (unsigned long) (mask << shift),
8695 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8696 #endif
8697 put_value (wordsz, chunksz, input_bfd, x,
8698 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8699 return r;
8700 }
8701
8702 /* Functions to read r_offset from external (target order) reloc
8703 entry. Faster than bfd_getl32 et al, because we let the compiler
8704 know the value is aligned. */
8705
8706 static bfd_vma
8707 ext32l_r_offset (const void *p)
8708 {
8709 union aligned32
8710 {
8711 uint32_t v;
8712 unsigned char c[4];
8713 };
8714 const union aligned32 *a
8715 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8716
8717 uint32_t aval = ( (uint32_t) a->c[0]
8718 | (uint32_t) a->c[1] << 8
8719 | (uint32_t) a->c[2] << 16
8720 | (uint32_t) a->c[3] << 24);
8721 return aval;
8722 }
8723
8724 static bfd_vma
8725 ext32b_r_offset (const void *p)
8726 {
8727 union aligned32
8728 {
8729 uint32_t v;
8730 unsigned char c[4];
8731 };
8732 const union aligned32 *a
8733 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8734
8735 uint32_t aval = ( (uint32_t) a->c[0] << 24
8736 | (uint32_t) a->c[1] << 16
8737 | (uint32_t) a->c[2] << 8
8738 | (uint32_t) a->c[3]);
8739 return aval;
8740 }
8741
8742 #ifdef BFD_HOST_64_BIT
8743 static bfd_vma
8744 ext64l_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]
8755 | (uint64_t) a->c[1] << 8
8756 | (uint64_t) a->c[2] << 16
8757 | (uint64_t) a->c[3] << 24
8758 | (uint64_t) a->c[4] << 32
8759 | (uint64_t) a->c[5] << 40
8760 | (uint64_t) a->c[6] << 48
8761 | (uint64_t) a->c[7] << 56);
8762 return aval;
8763 }
8764
8765 static bfd_vma
8766 ext64b_r_offset (const void *p)
8767 {
8768 union aligned64
8769 {
8770 uint64_t v;
8771 unsigned char c[8];
8772 };
8773 const union aligned64 *a
8774 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8775
8776 uint64_t aval = ( (uint64_t) a->c[0] << 56
8777 | (uint64_t) a->c[1] << 48
8778 | (uint64_t) a->c[2] << 40
8779 | (uint64_t) a->c[3] << 32
8780 | (uint64_t) a->c[4] << 24
8781 | (uint64_t) a->c[5] << 16
8782 | (uint64_t) a->c[6] << 8
8783 | (uint64_t) a->c[7]);
8784 return aval;
8785 }
8786 #endif
8787
8788 /* When performing a relocatable link, the input relocations are
8789 preserved. But, if they reference global symbols, the indices
8790 referenced must be updated. Update all the relocations found in
8791 RELDATA. */
8792
8793 static bfd_boolean
8794 elf_link_adjust_relocs (bfd *abfd,
8795 asection *sec,
8796 struct bfd_elf_section_reloc_data *reldata,
8797 bfd_boolean sort,
8798 struct bfd_link_info *info)
8799 {
8800 unsigned int i;
8801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8802 bfd_byte *erela;
8803 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8804 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8805 bfd_vma r_type_mask;
8806 int r_sym_shift;
8807 unsigned int count = reldata->count;
8808 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8809
8810 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8811 {
8812 swap_in = bed->s->swap_reloc_in;
8813 swap_out = bed->s->swap_reloc_out;
8814 }
8815 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8816 {
8817 swap_in = bed->s->swap_reloca_in;
8818 swap_out = bed->s->swap_reloca_out;
8819 }
8820 else
8821 abort ();
8822
8823 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8824 abort ();
8825
8826 if (bed->s->arch_size == 32)
8827 {
8828 r_type_mask = 0xff;
8829 r_sym_shift = 8;
8830 }
8831 else
8832 {
8833 r_type_mask = 0xffffffff;
8834 r_sym_shift = 32;
8835 }
8836
8837 erela = reldata->hdr->contents;
8838 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8839 {
8840 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8841 unsigned int j;
8842
8843 if (*rel_hash == NULL)
8844 continue;
8845
8846 if ((*rel_hash)->indx == -2
8847 && info->gc_sections
8848 && ! info->gc_keep_exported)
8849 {
8850 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8851 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8852 abfd, sec,
8853 (*rel_hash)->root.root.string);
8854 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8855 abfd, sec);
8856 bfd_set_error (bfd_error_invalid_operation);
8857 return FALSE;
8858 }
8859 BFD_ASSERT ((*rel_hash)->indx >= 0);
8860
8861 (*swap_in) (abfd, erela, irela);
8862 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8863 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8864 | (irela[j].r_info & r_type_mask));
8865 (*swap_out) (abfd, irela, erela);
8866 }
8867
8868 if (bed->elf_backend_update_relocs)
8869 (*bed->elf_backend_update_relocs) (sec, reldata);
8870
8871 if (sort && count != 0)
8872 {
8873 bfd_vma (*ext_r_off) (const void *);
8874 bfd_vma r_off;
8875 size_t elt_size;
8876 bfd_byte *base, *end, *p, *loc;
8877 bfd_byte *buf = NULL;
8878
8879 if (bed->s->arch_size == 32)
8880 {
8881 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8882 ext_r_off = ext32l_r_offset;
8883 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8884 ext_r_off = ext32b_r_offset;
8885 else
8886 abort ();
8887 }
8888 else
8889 {
8890 #ifdef BFD_HOST_64_BIT
8891 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8892 ext_r_off = ext64l_r_offset;
8893 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8894 ext_r_off = ext64b_r_offset;
8895 else
8896 #endif
8897 abort ();
8898 }
8899
8900 /* Must use a stable sort here. A modified insertion sort,
8901 since the relocs are mostly sorted already. */
8902 elt_size = reldata->hdr->sh_entsize;
8903 base = reldata->hdr->contents;
8904 end = base + count * elt_size;
8905 if (elt_size > sizeof (Elf64_External_Rela))
8906 abort ();
8907
8908 /* Ensure the first element is lowest. This acts as a sentinel,
8909 speeding the main loop below. */
8910 r_off = (*ext_r_off) (base);
8911 for (p = loc = base; (p += elt_size) < end; )
8912 {
8913 bfd_vma r_off2 = (*ext_r_off) (p);
8914 if (r_off > r_off2)
8915 {
8916 r_off = r_off2;
8917 loc = p;
8918 }
8919 }
8920 if (loc != base)
8921 {
8922 /* Don't just swap *base and *loc as that changes the order
8923 of the original base[0] and base[1] if they happen to
8924 have the same r_offset. */
8925 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8926 memcpy (onebuf, loc, elt_size);
8927 memmove (base + elt_size, base, loc - base);
8928 memcpy (base, onebuf, elt_size);
8929 }
8930
8931 for (p = base + elt_size; (p += elt_size) < end; )
8932 {
8933 /* base to p is sorted, *p is next to insert. */
8934 r_off = (*ext_r_off) (p);
8935 /* Search the sorted region for location to insert. */
8936 loc = p - elt_size;
8937 while (r_off < (*ext_r_off) (loc))
8938 loc -= elt_size;
8939 loc += elt_size;
8940 if (loc != p)
8941 {
8942 /* Chances are there is a run of relocs to insert here,
8943 from one of more input files. Files are not always
8944 linked in order due to the way elf_link_input_bfd is
8945 called. See pr17666. */
8946 size_t sortlen = p - loc;
8947 bfd_vma r_off2 = (*ext_r_off) (loc);
8948 size_t runlen = elt_size;
8949 size_t buf_size = 96 * 1024;
8950 while (p + runlen < end
8951 && (sortlen <= buf_size
8952 || runlen + elt_size <= buf_size)
8953 && r_off2 > (*ext_r_off) (p + runlen))
8954 runlen += elt_size;
8955 if (buf == NULL)
8956 {
8957 buf = bfd_malloc (buf_size);
8958 if (buf == NULL)
8959 return FALSE;
8960 }
8961 if (runlen < sortlen)
8962 {
8963 memcpy (buf, p, runlen);
8964 memmove (loc + runlen, loc, sortlen);
8965 memcpy (loc, buf, runlen);
8966 }
8967 else
8968 {
8969 memcpy (buf, loc, sortlen);
8970 memmove (loc, p, runlen);
8971 memcpy (loc + runlen, buf, sortlen);
8972 }
8973 p += runlen - elt_size;
8974 }
8975 }
8976 /* Hashes are no longer valid. */
8977 free (reldata->hashes);
8978 reldata->hashes = NULL;
8979 free (buf);
8980 }
8981 return TRUE;
8982 }
8983
8984 struct elf_link_sort_rela
8985 {
8986 union {
8987 bfd_vma offset;
8988 bfd_vma sym_mask;
8989 } u;
8990 enum elf_reloc_type_class type;
8991 /* We use this as an array of size int_rels_per_ext_rel. */
8992 Elf_Internal_Rela rela[1];
8993 };
8994
8995 static int
8996 elf_link_sort_cmp1 (const void *A, const void *B)
8997 {
8998 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8999 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9000 int relativea, relativeb;
9001
9002 relativea = a->type == reloc_class_relative;
9003 relativeb = b->type == reloc_class_relative;
9004
9005 if (relativea < relativeb)
9006 return 1;
9007 if (relativea > relativeb)
9008 return -1;
9009 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9010 return -1;
9011 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9012 return 1;
9013 if (a->rela->r_offset < b->rela->r_offset)
9014 return -1;
9015 if (a->rela->r_offset > b->rela->r_offset)
9016 return 1;
9017 return 0;
9018 }
9019
9020 static int
9021 elf_link_sort_cmp2 (const void *A, const void *B)
9022 {
9023 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9024 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9025
9026 if (a->type < b->type)
9027 return -1;
9028 if (a->type > b->type)
9029 return 1;
9030 if (a->u.offset < b->u.offset)
9031 return -1;
9032 if (a->u.offset > b->u.offset)
9033 return 1;
9034 if (a->rela->r_offset < b->rela->r_offset)
9035 return -1;
9036 if (a->rela->r_offset > b->rela->r_offset)
9037 return 1;
9038 return 0;
9039 }
9040
9041 static size_t
9042 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9043 {
9044 asection *dynamic_relocs;
9045 asection *rela_dyn;
9046 asection *rel_dyn;
9047 bfd_size_type count, size;
9048 size_t i, ret, sort_elt, ext_size;
9049 bfd_byte *sort, *s_non_relative, *p;
9050 struct elf_link_sort_rela *sq;
9051 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9052 int i2e = bed->s->int_rels_per_ext_rel;
9053 unsigned int opb = bfd_octets_per_byte (abfd);
9054 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9055 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9056 struct bfd_link_order *lo;
9057 bfd_vma r_sym_mask;
9058 bfd_boolean use_rela;
9059
9060 /* Find a dynamic reloc section. */
9061 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9062 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9063 if (rela_dyn != NULL && rela_dyn->size > 0
9064 && rel_dyn != NULL && rel_dyn->size > 0)
9065 {
9066 bfd_boolean use_rela_initialised = FALSE;
9067
9068 /* This is just here to stop gcc from complaining.
9069 Its initialization checking code is not perfect. */
9070 use_rela = TRUE;
9071
9072 /* Both sections are present. Examine the sizes
9073 of the indirect sections to help us choose. */
9074 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9075 if (lo->type == bfd_indirect_link_order)
9076 {
9077 asection *o = lo->u.indirect.section;
9078
9079 if ((o->size % bed->s->sizeof_rela) == 0)
9080 {
9081 if ((o->size % bed->s->sizeof_rel) == 0)
9082 /* Section size is divisible by both rel and rela sizes.
9083 It is of no help to us. */
9084 ;
9085 else
9086 {
9087 /* Section size is only divisible by rela. */
9088 if (use_rela_initialised && !use_rela)
9089 {
9090 _bfd_error_handler (_("%pB: unable to sort relocs - "
9091 "they are in more than one size"),
9092 abfd);
9093 bfd_set_error (bfd_error_invalid_operation);
9094 return 0;
9095 }
9096 else
9097 {
9098 use_rela = TRUE;
9099 use_rela_initialised = TRUE;
9100 }
9101 }
9102 }
9103 else if ((o->size % bed->s->sizeof_rel) == 0)
9104 {
9105 /* Section size is only divisible by rel. */
9106 if (use_rela_initialised && use_rela)
9107 {
9108 _bfd_error_handler (_("%pB: unable to sort relocs - "
9109 "they are in more than one size"),
9110 abfd);
9111 bfd_set_error (bfd_error_invalid_operation);
9112 return 0;
9113 }
9114 else
9115 {
9116 use_rela = FALSE;
9117 use_rela_initialised = TRUE;
9118 }
9119 }
9120 else
9121 {
9122 /* The section size is not divisible by either -
9123 something is wrong. */
9124 _bfd_error_handler (_("%pB: unable to sort relocs - "
9125 "they are of an unknown size"), abfd);
9126 bfd_set_error (bfd_error_invalid_operation);
9127 return 0;
9128 }
9129 }
9130
9131 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9132 if (lo->type == bfd_indirect_link_order)
9133 {
9134 asection *o = lo->u.indirect.section;
9135
9136 if ((o->size % bed->s->sizeof_rela) == 0)
9137 {
9138 if ((o->size % bed->s->sizeof_rel) == 0)
9139 /* Section size is divisible by both rel and rela sizes.
9140 It is of no help to us. */
9141 ;
9142 else
9143 {
9144 /* Section size is only divisible by rela. */
9145 if (use_rela_initialised && !use_rela)
9146 {
9147 _bfd_error_handler (_("%pB: unable to sort relocs - "
9148 "they are in more than one size"),
9149 abfd);
9150 bfd_set_error (bfd_error_invalid_operation);
9151 return 0;
9152 }
9153 else
9154 {
9155 use_rela = TRUE;
9156 use_rela_initialised = TRUE;
9157 }
9158 }
9159 }
9160 else if ((o->size % bed->s->sizeof_rel) == 0)
9161 {
9162 /* Section size is only divisible by rel. */
9163 if (use_rela_initialised && use_rela)
9164 {
9165 _bfd_error_handler (_("%pB: unable to sort relocs - "
9166 "they are in more than one size"),
9167 abfd);
9168 bfd_set_error (bfd_error_invalid_operation);
9169 return 0;
9170 }
9171 else
9172 {
9173 use_rela = FALSE;
9174 use_rela_initialised = TRUE;
9175 }
9176 }
9177 else
9178 {
9179 /* The section size is not divisible by either -
9180 something is wrong. */
9181 _bfd_error_handler (_("%pB: unable to sort relocs - "
9182 "they are of an unknown size"), abfd);
9183 bfd_set_error (bfd_error_invalid_operation);
9184 return 0;
9185 }
9186 }
9187
9188 if (! use_rela_initialised)
9189 /* Make a guess. */
9190 use_rela = TRUE;
9191 }
9192 else if (rela_dyn != NULL && rela_dyn->size > 0)
9193 use_rela = TRUE;
9194 else if (rel_dyn != NULL && rel_dyn->size > 0)
9195 use_rela = FALSE;
9196 else
9197 return 0;
9198
9199 if (use_rela)
9200 {
9201 dynamic_relocs = rela_dyn;
9202 ext_size = bed->s->sizeof_rela;
9203 swap_in = bed->s->swap_reloca_in;
9204 swap_out = bed->s->swap_reloca_out;
9205 }
9206 else
9207 {
9208 dynamic_relocs = rel_dyn;
9209 ext_size = bed->s->sizeof_rel;
9210 swap_in = bed->s->swap_reloc_in;
9211 swap_out = bed->s->swap_reloc_out;
9212 }
9213
9214 size = 0;
9215 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9216 if (lo->type == bfd_indirect_link_order)
9217 size += lo->u.indirect.section->size;
9218
9219 if (size != dynamic_relocs->size)
9220 return 0;
9221
9222 sort_elt = (sizeof (struct elf_link_sort_rela)
9223 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9224
9225 count = dynamic_relocs->size / ext_size;
9226 if (count == 0)
9227 return 0;
9228 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9229
9230 if (sort == NULL)
9231 {
9232 (*info->callbacks->warning)
9233 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9234 return 0;
9235 }
9236
9237 if (bed->s->arch_size == 32)
9238 r_sym_mask = ~(bfd_vma) 0xff;
9239 else
9240 r_sym_mask = ~(bfd_vma) 0xffffffff;
9241
9242 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9243 if (lo->type == bfd_indirect_link_order)
9244 {
9245 bfd_byte *erel, *erelend;
9246 asection *o = lo->u.indirect.section;
9247
9248 if (o->contents == NULL && o->size != 0)
9249 {
9250 /* This is a reloc section that is being handled as a normal
9251 section. See bfd_section_from_shdr. We can't combine
9252 relocs in this case. */
9253 free (sort);
9254 return 0;
9255 }
9256 erel = o->contents;
9257 erelend = o->contents + o->size;
9258 p = sort + o->output_offset * opb / ext_size * sort_elt;
9259
9260 while (erel < erelend)
9261 {
9262 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9263
9264 (*swap_in) (abfd, erel, s->rela);
9265 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9266 s->u.sym_mask = r_sym_mask;
9267 p += sort_elt;
9268 erel += ext_size;
9269 }
9270 }
9271
9272 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9273
9274 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9275 {
9276 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9277 if (s->type != reloc_class_relative)
9278 break;
9279 }
9280 ret = i;
9281 s_non_relative = p;
9282
9283 sq = (struct elf_link_sort_rela *) s_non_relative;
9284 for (; i < count; i++, p += sort_elt)
9285 {
9286 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9287 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9288 sq = sp;
9289 sp->u.offset = sq->rela->r_offset;
9290 }
9291
9292 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9293
9294 struct elf_link_hash_table *htab = elf_hash_table (info);
9295 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9296 {
9297 /* We have plt relocs in .rela.dyn. */
9298 sq = (struct elf_link_sort_rela *) sort;
9299 for (i = 0; i < count; i++)
9300 if (sq[count - i - 1].type != reloc_class_plt)
9301 break;
9302 if (i != 0 && htab->srelplt->size == i * ext_size)
9303 {
9304 struct bfd_link_order **plo;
9305 /* Put srelplt link_order last. This is so the output_offset
9306 set in the next loop is correct for DT_JMPREL. */
9307 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9308 if ((*plo)->type == bfd_indirect_link_order
9309 && (*plo)->u.indirect.section == htab->srelplt)
9310 {
9311 lo = *plo;
9312 *plo = lo->next;
9313 }
9314 else
9315 plo = &(*plo)->next;
9316 *plo = lo;
9317 lo->next = NULL;
9318 dynamic_relocs->map_tail.link_order = lo;
9319 }
9320 }
9321
9322 p = sort;
9323 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9324 if (lo->type == bfd_indirect_link_order)
9325 {
9326 bfd_byte *erel, *erelend;
9327 asection *o = lo->u.indirect.section;
9328
9329 erel = o->contents;
9330 erelend = o->contents + o->size;
9331 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9332 while (erel < erelend)
9333 {
9334 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9335 (*swap_out) (abfd, s->rela, erel);
9336 p += sort_elt;
9337 erel += ext_size;
9338 }
9339 }
9340
9341 free (sort);
9342 *psec = dynamic_relocs;
9343 return ret;
9344 }
9345
9346 /* Add a symbol to the output symbol string table. */
9347
9348 static int
9349 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9350 const char *name,
9351 Elf_Internal_Sym *elfsym,
9352 asection *input_sec,
9353 struct elf_link_hash_entry *h)
9354 {
9355 int (*output_symbol_hook)
9356 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9357 struct elf_link_hash_entry *);
9358 struct elf_link_hash_table *hash_table;
9359 const struct elf_backend_data *bed;
9360 bfd_size_type strtabsize;
9361
9362 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9363
9364 bed = get_elf_backend_data (flinfo->output_bfd);
9365 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9366 if (output_symbol_hook != NULL)
9367 {
9368 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9369 if (ret != 1)
9370 return ret;
9371 }
9372
9373 if (name == NULL
9374 || *name == '\0'
9375 || (input_sec->flags & SEC_EXCLUDE))
9376 elfsym->st_name = (unsigned long) -1;
9377 else
9378 {
9379 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9380 to get the final offset for st_name. */
9381 elfsym->st_name
9382 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9383 name, FALSE);
9384 if (elfsym->st_name == (unsigned long) -1)
9385 return 0;
9386 }
9387
9388 hash_table = elf_hash_table (flinfo->info);
9389 strtabsize = hash_table->strtabsize;
9390 if (strtabsize <= hash_table->strtabcount)
9391 {
9392 strtabsize += strtabsize;
9393 hash_table->strtabsize = strtabsize;
9394 strtabsize *= sizeof (*hash_table->strtab);
9395 hash_table->strtab
9396 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9397 strtabsize);
9398 if (hash_table->strtab == NULL)
9399 return 0;
9400 }
9401 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9402 hash_table->strtab[hash_table->strtabcount].dest_index
9403 = hash_table->strtabcount;
9404 hash_table->strtab[hash_table->strtabcount].destshndx_index
9405 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9406
9407 bfd_get_symcount (flinfo->output_bfd) += 1;
9408 hash_table->strtabcount += 1;
9409
9410 return 1;
9411 }
9412
9413 /* Swap symbols out to the symbol table and flush the output symbols to
9414 the file. */
9415
9416 static bfd_boolean
9417 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9418 {
9419 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9420 bfd_size_type amt;
9421 size_t i;
9422 const struct elf_backend_data *bed;
9423 bfd_byte *symbuf;
9424 Elf_Internal_Shdr *hdr;
9425 file_ptr pos;
9426 bfd_boolean ret;
9427
9428 if (!hash_table->strtabcount)
9429 return TRUE;
9430
9431 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9432
9433 bed = get_elf_backend_data (flinfo->output_bfd);
9434
9435 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9436 symbuf = (bfd_byte *) bfd_malloc (amt);
9437 if (symbuf == NULL)
9438 return FALSE;
9439
9440 if (flinfo->symshndxbuf)
9441 {
9442 amt = sizeof (Elf_External_Sym_Shndx);
9443 amt *= bfd_get_symcount (flinfo->output_bfd);
9444 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9445 if (flinfo->symshndxbuf == NULL)
9446 {
9447 free (symbuf);
9448 return FALSE;
9449 }
9450 }
9451
9452 for (i = 0; i < hash_table->strtabcount; i++)
9453 {
9454 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9455 if (elfsym->sym.st_name == (unsigned long) -1)
9456 elfsym->sym.st_name = 0;
9457 else
9458 elfsym->sym.st_name
9459 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9460 elfsym->sym.st_name);
9461 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9462 ((bfd_byte *) symbuf
9463 + (elfsym->dest_index
9464 * bed->s->sizeof_sym)),
9465 (flinfo->symshndxbuf
9466 + elfsym->destshndx_index));
9467 }
9468
9469 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9470 pos = hdr->sh_offset + hdr->sh_size;
9471 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9472 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9473 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9474 {
9475 hdr->sh_size += amt;
9476 ret = TRUE;
9477 }
9478 else
9479 ret = FALSE;
9480
9481 free (symbuf);
9482
9483 free (hash_table->strtab);
9484 hash_table->strtab = NULL;
9485
9486 return ret;
9487 }
9488
9489 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9490
9491 static bfd_boolean
9492 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9493 {
9494 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9495 && sym->st_shndx < SHN_LORESERVE)
9496 {
9497 /* The gABI doesn't support dynamic symbols in output sections
9498 beyond 64k. */
9499 _bfd_error_handler
9500 /* xgettext:c-format */
9501 (_("%pB: too many sections: %d (>= %d)"),
9502 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9503 bfd_set_error (bfd_error_nonrepresentable_section);
9504 return FALSE;
9505 }
9506 return TRUE;
9507 }
9508
9509 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9510 allowing an unsatisfied unversioned symbol in the DSO to match a
9511 versioned symbol that would normally require an explicit version.
9512 We also handle the case that a DSO references a hidden symbol
9513 which may be satisfied by a versioned symbol in another DSO. */
9514
9515 static bfd_boolean
9516 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9517 const struct elf_backend_data *bed,
9518 struct elf_link_hash_entry *h)
9519 {
9520 bfd *abfd;
9521 struct elf_link_loaded_list *loaded;
9522
9523 if (!is_elf_hash_table (info->hash))
9524 return FALSE;
9525
9526 /* Check indirect symbol. */
9527 while (h->root.type == bfd_link_hash_indirect)
9528 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9529
9530 switch (h->root.type)
9531 {
9532 default:
9533 abfd = NULL;
9534 break;
9535
9536 case bfd_link_hash_undefined:
9537 case bfd_link_hash_undefweak:
9538 abfd = h->root.u.undef.abfd;
9539 if (abfd == NULL
9540 || (abfd->flags & DYNAMIC) == 0
9541 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9542 return FALSE;
9543 break;
9544
9545 case bfd_link_hash_defined:
9546 case bfd_link_hash_defweak:
9547 abfd = h->root.u.def.section->owner;
9548 break;
9549
9550 case bfd_link_hash_common:
9551 abfd = h->root.u.c.p->section->owner;
9552 break;
9553 }
9554 BFD_ASSERT (abfd != NULL);
9555
9556 for (loaded = elf_hash_table (info)->loaded;
9557 loaded != NULL;
9558 loaded = loaded->next)
9559 {
9560 bfd *input;
9561 Elf_Internal_Shdr *hdr;
9562 size_t symcount;
9563 size_t extsymcount;
9564 size_t extsymoff;
9565 Elf_Internal_Shdr *versymhdr;
9566 Elf_Internal_Sym *isym;
9567 Elf_Internal_Sym *isymend;
9568 Elf_Internal_Sym *isymbuf;
9569 Elf_External_Versym *ever;
9570 Elf_External_Versym *extversym;
9571
9572 input = loaded->abfd;
9573
9574 /* We check each DSO for a possible hidden versioned definition. */
9575 if (input == abfd
9576 || (input->flags & DYNAMIC) == 0
9577 || elf_dynversym (input) == 0)
9578 continue;
9579
9580 hdr = &elf_tdata (input)->dynsymtab_hdr;
9581
9582 symcount = hdr->sh_size / bed->s->sizeof_sym;
9583 if (elf_bad_symtab (input))
9584 {
9585 extsymcount = symcount;
9586 extsymoff = 0;
9587 }
9588 else
9589 {
9590 extsymcount = symcount - hdr->sh_info;
9591 extsymoff = hdr->sh_info;
9592 }
9593
9594 if (extsymcount == 0)
9595 continue;
9596
9597 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9598 NULL, NULL, NULL);
9599 if (isymbuf == NULL)
9600 return FALSE;
9601
9602 /* Read in any version definitions. */
9603 versymhdr = &elf_tdata (input)->dynversym_hdr;
9604 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9605 if (extversym == NULL)
9606 goto error_ret;
9607
9608 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9609 || (bfd_bread (extversym, versymhdr->sh_size, input)
9610 != versymhdr->sh_size))
9611 {
9612 free (extversym);
9613 error_ret:
9614 free (isymbuf);
9615 return FALSE;
9616 }
9617
9618 ever = extversym + extsymoff;
9619 isymend = isymbuf + extsymcount;
9620 for (isym = isymbuf; isym < isymend; isym++, ever++)
9621 {
9622 const char *name;
9623 Elf_Internal_Versym iver;
9624 unsigned short version_index;
9625
9626 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9627 || isym->st_shndx == SHN_UNDEF)
9628 continue;
9629
9630 name = bfd_elf_string_from_elf_section (input,
9631 hdr->sh_link,
9632 isym->st_name);
9633 if (strcmp (name, h->root.root.string) != 0)
9634 continue;
9635
9636 _bfd_elf_swap_versym_in (input, ever, &iver);
9637
9638 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9639 && !(h->def_regular
9640 && h->forced_local))
9641 {
9642 /* If we have a non-hidden versioned sym, then it should
9643 have provided a definition for the undefined sym unless
9644 it is defined in a non-shared object and forced local.
9645 */
9646 abort ();
9647 }
9648
9649 version_index = iver.vs_vers & VERSYM_VERSION;
9650 if (version_index == 1 || version_index == 2)
9651 {
9652 /* This is the base or first version. We can use it. */
9653 free (extversym);
9654 free (isymbuf);
9655 return TRUE;
9656 }
9657 }
9658
9659 free (extversym);
9660 free (isymbuf);
9661 }
9662
9663 return FALSE;
9664 }
9665
9666 /* Convert ELF common symbol TYPE. */
9667
9668 static int
9669 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9670 {
9671 /* Commom symbol can only appear in relocatable link. */
9672 if (!bfd_link_relocatable (info))
9673 abort ();
9674 switch (info->elf_stt_common)
9675 {
9676 case unchanged:
9677 break;
9678 case elf_stt_common:
9679 type = STT_COMMON;
9680 break;
9681 case no_elf_stt_common:
9682 type = STT_OBJECT;
9683 break;
9684 }
9685 return type;
9686 }
9687
9688 /* Add an external symbol to the symbol table. This is called from
9689 the hash table traversal routine. When generating a shared object,
9690 we go through the symbol table twice. The first time we output
9691 anything that might have been forced to local scope in a version
9692 script. The second time we output the symbols that are still
9693 global symbols. */
9694
9695 static bfd_boolean
9696 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9697 {
9698 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9699 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9700 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9701 bfd_boolean strip;
9702 Elf_Internal_Sym sym;
9703 asection *input_sec;
9704 const struct elf_backend_data *bed;
9705 long indx;
9706 int ret;
9707 unsigned int type;
9708
9709 if (h->root.type == bfd_link_hash_warning)
9710 {
9711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9712 if (h->root.type == bfd_link_hash_new)
9713 return TRUE;
9714 }
9715
9716 /* Decide whether to output this symbol in this pass. */
9717 if (eoinfo->localsyms)
9718 {
9719 if (!h->forced_local)
9720 return TRUE;
9721 }
9722 else
9723 {
9724 if (h->forced_local)
9725 return TRUE;
9726 }
9727
9728 bed = get_elf_backend_data (flinfo->output_bfd);
9729
9730 if (h->root.type == bfd_link_hash_undefined)
9731 {
9732 /* If we have an undefined symbol reference here then it must have
9733 come from a shared library that is being linked in. (Undefined
9734 references in regular files have already been handled unless
9735 they are in unreferenced sections which are removed by garbage
9736 collection). */
9737 bfd_boolean ignore_undef = FALSE;
9738
9739 /* Some symbols may be special in that the fact that they're
9740 undefined can be safely ignored - let backend determine that. */
9741 if (bed->elf_backend_ignore_undef_symbol)
9742 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9743
9744 /* If we are reporting errors for this situation then do so now. */
9745 if (!ignore_undef
9746 && h->ref_dynamic
9747 && (!h->ref_regular || flinfo->info->gc_sections)
9748 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9749 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9750 (*flinfo->info->callbacks->undefined_symbol)
9751 (flinfo->info, h->root.root.string,
9752 h->ref_regular ? NULL : h->root.u.undef.abfd,
9753 NULL, 0,
9754 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9755
9756 /* Strip a global symbol defined in a discarded section. */
9757 if (h->indx == -3)
9758 return TRUE;
9759 }
9760
9761 /* We should also warn if a forced local symbol is referenced from
9762 shared libraries. */
9763 if (bfd_link_executable (flinfo->info)
9764 && h->forced_local
9765 && h->ref_dynamic
9766 && h->def_regular
9767 && !h->dynamic_def
9768 && h->ref_dynamic_nonweak
9769 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9770 {
9771 bfd *def_bfd;
9772 const char *msg;
9773 struct elf_link_hash_entry *hi = h;
9774
9775 /* Check indirect symbol. */
9776 while (hi->root.type == bfd_link_hash_indirect)
9777 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9778
9779 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9780 /* xgettext:c-format */
9781 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9782 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9783 /* xgettext:c-format */
9784 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9785 else
9786 /* xgettext:c-format */
9787 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9788 def_bfd = flinfo->output_bfd;
9789 if (hi->root.u.def.section != bfd_abs_section_ptr)
9790 def_bfd = hi->root.u.def.section->owner;
9791 _bfd_error_handler (msg, flinfo->output_bfd,
9792 h->root.root.string, def_bfd);
9793 bfd_set_error (bfd_error_bad_value);
9794 eoinfo->failed = TRUE;
9795 return FALSE;
9796 }
9797
9798 /* We don't want to output symbols that have never been mentioned by
9799 a regular file, or that we have been told to strip. However, if
9800 h->indx is set to -2, the symbol is used by a reloc and we must
9801 output it. */
9802 strip = FALSE;
9803 if (h->indx == -2)
9804 ;
9805 else if ((h->def_dynamic
9806 || h->ref_dynamic
9807 || h->root.type == bfd_link_hash_new)
9808 && !h->def_regular
9809 && !h->ref_regular)
9810 strip = TRUE;
9811 else if (flinfo->info->strip == strip_all)
9812 strip = TRUE;
9813 else if (flinfo->info->strip == strip_some
9814 && bfd_hash_lookup (flinfo->info->keep_hash,
9815 h->root.root.string, FALSE, FALSE) == NULL)
9816 strip = TRUE;
9817 else if ((h->root.type == bfd_link_hash_defined
9818 || h->root.type == bfd_link_hash_defweak)
9819 && ((flinfo->info->strip_discarded
9820 && discarded_section (h->root.u.def.section))
9821 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9822 && h->root.u.def.section->owner != NULL
9823 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9824 strip = TRUE;
9825 else if ((h->root.type == bfd_link_hash_undefined
9826 || h->root.type == bfd_link_hash_undefweak)
9827 && h->root.u.undef.abfd != NULL
9828 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9829 strip = TRUE;
9830
9831 type = h->type;
9832
9833 /* If we're stripping it, and it's not a dynamic symbol, there's
9834 nothing else to do. However, if it is a forced local symbol or
9835 an ifunc symbol we need to give the backend finish_dynamic_symbol
9836 function a chance to make it dynamic. */
9837 if (strip
9838 && h->dynindx == -1
9839 && type != STT_GNU_IFUNC
9840 && !h->forced_local)
9841 return TRUE;
9842
9843 sym.st_value = 0;
9844 sym.st_size = h->size;
9845 sym.st_other = h->other;
9846 switch (h->root.type)
9847 {
9848 default:
9849 case bfd_link_hash_new:
9850 case bfd_link_hash_warning:
9851 abort ();
9852 return FALSE;
9853
9854 case bfd_link_hash_undefined:
9855 case bfd_link_hash_undefweak:
9856 input_sec = bfd_und_section_ptr;
9857 sym.st_shndx = SHN_UNDEF;
9858 break;
9859
9860 case bfd_link_hash_defined:
9861 case bfd_link_hash_defweak:
9862 {
9863 input_sec = h->root.u.def.section;
9864 if (input_sec->output_section != NULL)
9865 {
9866 sym.st_shndx =
9867 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9868 input_sec->output_section);
9869 if (sym.st_shndx == SHN_BAD)
9870 {
9871 _bfd_error_handler
9872 /* xgettext:c-format */
9873 (_("%pB: could not find output section %pA for input section %pA"),
9874 flinfo->output_bfd, input_sec->output_section, input_sec);
9875 bfd_set_error (bfd_error_nonrepresentable_section);
9876 eoinfo->failed = TRUE;
9877 return FALSE;
9878 }
9879
9880 /* ELF symbols in relocatable files are section relative,
9881 but in nonrelocatable files they are virtual
9882 addresses. */
9883 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9884 if (!bfd_link_relocatable (flinfo->info))
9885 {
9886 sym.st_value += input_sec->output_section->vma;
9887 if (h->type == STT_TLS)
9888 {
9889 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9890 if (tls_sec != NULL)
9891 sym.st_value -= tls_sec->vma;
9892 }
9893 }
9894 }
9895 else
9896 {
9897 BFD_ASSERT (input_sec->owner == NULL
9898 || (input_sec->owner->flags & DYNAMIC) != 0);
9899 sym.st_shndx = SHN_UNDEF;
9900 input_sec = bfd_und_section_ptr;
9901 }
9902 }
9903 break;
9904
9905 case bfd_link_hash_common:
9906 input_sec = h->root.u.c.p->section;
9907 sym.st_shndx = bed->common_section_index (input_sec);
9908 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9909 break;
9910
9911 case bfd_link_hash_indirect:
9912 /* These symbols are created by symbol versioning. They point
9913 to the decorated version of the name. For example, if the
9914 symbol foo@@GNU_1.2 is the default, which should be used when
9915 foo is used with no version, then we add an indirect symbol
9916 foo which points to foo@@GNU_1.2. We ignore these symbols,
9917 since the indirected symbol is already in the hash table. */
9918 return TRUE;
9919 }
9920
9921 if (type == STT_COMMON || type == STT_OBJECT)
9922 switch (h->root.type)
9923 {
9924 case bfd_link_hash_common:
9925 type = elf_link_convert_common_type (flinfo->info, type);
9926 break;
9927 case bfd_link_hash_defined:
9928 case bfd_link_hash_defweak:
9929 if (bed->common_definition (&sym))
9930 type = elf_link_convert_common_type (flinfo->info, type);
9931 else
9932 type = STT_OBJECT;
9933 break;
9934 case bfd_link_hash_undefined:
9935 case bfd_link_hash_undefweak:
9936 break;
9937 default:
9938 abort ();
9939 }
9940
9941 if (h->forced_local)
9942 {
9943 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9944 /* Turn off visibility on local symbol. */
9945 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9946 }
9947 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9948 else if (h->unique_global && h->def_regular)
9949 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9950 else if (h->root.type == bfd_link_hash_undefweak
9951 || h->root.type == bfd_link_hash_defweak)
9952 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9953 else
9954 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9955 sym.st_target_internal = h->target_internal;
9956
9957 /* Give the processor backend a chance to tweak the symbol value,
9958 and also to finish up anything that needs to be done for this
9959 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9960 forced local syms when non-shared is due to a historical quirk.
9961 STT_GNU_IFUNC symbol must go through PLT. */
9962 if ((h->type == STT_GNU_IFUNC
9963 && h->def_regular
9964 && !bfd_link_relocatable (flinfo->info))
9965 || ((h->dynindx != -1
9966 || h->forced_local)
9967 && ((bfd_link_pic (flinfo->info)
9968 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9969 || h->root.type != bfd_link_hash_undefweak))
9970 || !h->forced_local)
9971 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9972 {
9973 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9974 (flinfo->output_bfd, flinfo->info, h, &sym)))
9975 {
9976 eoinfo->failed = TRUE;
9977 return FALSE;
9978 }
9979 }
9980
9981 /* If we are marking the symbol as undefined, and there are no
9982 non-weak references to this symbol from a regular object, then
9983 mark the symbol as weak undefined; if there are non-weak
9984 references, mark the symbol as strong. We can't do this earlier,
9985 because it might not be marked as undefined until the
9986 finish_dynamic_symbol routine gets through with it. */
9987 if (sym.st_shndx == SHN_UNDEF
9988 && h->ref_regular
9989 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9990 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9991 {
9992 int bindtype;
9993 type = ELF_ST_TYPE (sym.st_info);
9994
9995 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9996 if (type == STT_GNU_IFUNC)
9997 type = STT_FUNC;
9998
9999 if (h->ref_regular_nonweak)
10000 bindtype = STB_GLOBAL;
10001 else
10002 bindtype = STB_WEAK;
10003 sym.st_info = ELF_ST_INFO (bindtype, type);
10004 }
10005
10006 /* If this is a symbol defined in a dynamic library, don't use the
10007 symbol size from the dynamic library. Relinking an executable
10008 against a new library may introduce gratuitous changes in the
10009 executable's symbols if we keep the size. */
10010 if (sym.st_shndx == SHN_UNDEF
10011 && !h->def_regular
10012 && h->def_dynamic)
10013 sym.st_size = 0;
10014
10015 /* If a non-weak symbol with non-default visibility is not defined
10016 locally, it is a fatal error. */
10017 if (!bfd_link_relocatable (flinfo->info)
10018 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10019 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10020 && h->root.type == bfd_link_hash_undefined
10021 && !h->def_regular)
10022 {
10023 const char *msg;
10024
10025 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10026 /* xgettext:c-format */
10027 msg = _("%pB: protected symbol `%s' isn't defined");
10028 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10029 /* xgettext:c-format */
10030 msg = _("%pB: internal symbol `%s' isn't defined");
10031 else
10032 /* xgettext:c-format */
10033 msg = _("%pB: hidden symbol `%s' isn't defined");
10034 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10035 bfd_set_error (bfd_error_bad_value);
10036 eoinfo->failed = TRUE;
10037 return FALSE;
10038 }
10039
10040 /* If this symbol should be put in the .dynsym section, then put it
10041 there now. We already know the symbol index. We also fill in
10042 the entry in the .hash section. */
10043 if (elf_hash_table (flinfo->info)->dynsym != NULL
10044 && h->dynindx != -1
10045 && elf_hash_table (flinfo->info)->dynamic_sections_created)
10046 {
10047 bfd_byte *esym;
10048
10049 /* Since there is no version information in the dynamic string,
10050 if there is no version info in symbol version section, we will
10051 have a run-time problem if not linking executable, referenced
10052 by shared library, or not bound locally. */
10053 if (h->verinfo.verdef == NULL
10054 && (!bfd_link_executable (flinfo->info)
10055 || h->ref_dynamic
10056 || !h->def_regular))
10057 {
10058 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10059
10060 if (p && p [1] != '\0')
10061 {
10062 _bfd_error_handler
10063 /* xgettext:c-format */
10064 (_("%pB: no symbol version section for versioned symbol `%s'"),
10065 flinfo->output_bfd, h->root.root.string);
10066 eoinfo->failed = TRUE;
10067 return FALSE;
10068 }
10069 }
10070
10071 sym.st_name = h->dynstr_index;
10072 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10073 + h->dynindx * bed->s->sizeof_sym);
10074 if (!check_dynsym (flinfo->output_bfd, &sym))
10075 {
10076 eoinfo->failed = TRUE;
10077 return FALSE;
10078 }
10079 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10080
10081 if (flinfo->hash_sec != NULL)
10082 {
10083 size_t hash_entry_size;
10084 bfd_byte *bucketpos;
10085 bfd_vma chain;
10086 size_t bucketcount;
10087 size_t bucket;
10088
10089 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10090 bucket = h->u.elf_hash_value % bucketcount;
10091
10092 hash_entry_size
10093 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10094 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10095 + (bucket + 2) * hash_entry_size);
10096 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10097 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10098 bucketpos);
10099 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10100 ((bfd_byte *) flinfo->hash_sec->contents
10101 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10102 }
10103
10104 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10105 {
10106 Elf_Internal_Versym iversym;
10107 Elf_External_Versym *eversym;
10108
10109 if (!h->def_regular)
10110 {
10111 if (h->verinfo.verdef == NULL
10112 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10113 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10114 iversym.vs_vers = 0;
10115 else
10116 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10117 }
10118 else
10119 {
10120 if (h->verinfo.vertree == NULL)
10121 iversym.vs_vers = 1;
10122 else
10123 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10124 if (flinfo->info->create_default_symver)
10125 iversym.vs_vers++;
10126 }
10127
10128 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10129 defined locally. */
10130 if (h->versioned == versioned_hidden && h->def_regular)
10131 iversym.vs_vers |= VERSYM_HIDDEN;
10132
10133 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10134 eversym += h->dynindx;
10135 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10136 }
10137 }
10138
10139 /* If the symbol is undefined, and we didn't output it to .dynsym,
10140 strip it from .symtab too. Obviously we can't do this for
10141 relocatable output or when needed for --emit-relocs. */
10142 else if (input_sec == bfd_und_section_ptr
10143 && h->indx != -2
10144 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10145 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10146 && !bfd_link_relocatable (flinfo->info))
10147 return TRUE;
10148
10149 /* Also strip others that we couldn't earlier due to dynamic symbol
10150 processing. */
10151 if (strip)
10152 return TRUE;
10153 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10154 return TRUE;
10155
10156 /* Output a FILE symbol so that following locals are not associated
10157 with the wrong input file. We need one for forced local symbols
10158 if we've seen more than one FILE symbol or when we have exactly
10159 one FILE symbol but global symbols are present in a file other
10160 than the one with the FILE symbol. We also need one if linker
10161 defined symbols are present. In practice these conditions are
10162 always met, so just emit the FILE symbol unconditionally. */
10163 if (eoinfo->localsyms
10164 && !eoinfo->file_sym_done
10165 && eoinfo->flinfo->filesym_count != 0)
10166 {
10167 Elf_Internal_Sym fsym;
10168
10169 memset (&fsym, 0, sizeof (fsym));
10170 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10171 fsym.st_shndx = SHN_ABS;
10172 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10173 bfd_und_section_ptr, NULL))
10174 return FALSE;
10175
10176 eoinfo->file_sym_done = TRUE;
10177 }
10178
10179 indx = bfd_get_symcount (flinfo->output_bfd);
10180 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10181 input_sec, h);
10182 if (ret == 0)
10183 {
10184 eoinfo->failed = TRUE;
10185 return FALSE;
10186 }
10187 else if (ret == 1)
10188 h->indx = indx;
10189 else if (h->indx == -2)
10190 abort();
10191
10192 return TRUE;
10193 }
10194
10195 /* Return TRUE if special handling is done for relocs in SEC against
10196 symbols defined in discarded sections. */
10197
10198 static bfd_boolean
10199 elf_section_ignore_discarded_relocs (asection *sec)
10200 {
10201 const struct elf_backend_data *bed;
10202
10203 switch (sec->sec_info_type)
10204 {
10205 case SEC_INFO_TYPE_STABS:
10206 case SEC_INFO_TYPE_EH_FRAME:
10207 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10208 return TRUE;
10209 default:
10210 break;
10211 }
10212
10213 bed = get_elf_backend_data (sec->owner);
10214 if (bed->elf_backend_ignore_discarded_relocs != NULL
10215 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10216 return TRUE;
10217
10218 return FALSE;
10219 }
10220
10221 /* Return a mask saying how ld should treat relocations in SEC against
10222 symbols defined in discarded sections. If this function returns
10223 COMPLAIN set, ld will issue a warning message. If this function
10224 returns PRETEND set, and the discarded section was link-once and the
10225 same size as the kept link-once section, ld will pretend that the
10226 symbol was actually defined in the kept section. Otherwise ld will
10227 zero the reloc (at least that is the intent, but some cooperation by
10228 the target dependent code is needed, particularly for REL targets). */
10229
10230 unsigned int
10231 _bfd_elf_default_action_discarded (asection *sec)
10232 {
10233 if (sec->flags & SEC_DEBUGGING)
10234 return PRETEND;
10235
10236 if (strcmp (".eh_frame", sec->name) == 0)
10237 return 0;
10238
10239 if (strcmp (".gcc_except_table", sec->name) == 0)
10240 return 0;
10241
10242 return COMPLAIN | PRETEND;
10243 }
10244
10245 /* Find a match between a section and a member of a section group. */
10246
10247 static asection *
10248 match_group_member (asection *sec, asection *group,
10249 struct bfd_link_info *info)
10250 {
10251 asection *first = elf_next_in_group (group);
10252 asection *s = first;
10253
10254 while (s != NULL)
10255 {
10256 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10257 return s;
10258
10259 s = elf_next_in_group (s);
10260 if (s == first)
10261 break;
10262 }
10263
10264 return NULL;
10265 }
10266
10267 /* Check if the kept section of a discarded section SEC can be used
10268 to replace it. Return the replacement if it is OK. Otherwise return
10269 NULL. */
10270
10271 asection *
10272 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10273 {
10274 asection *kept;
10275
10276 kept = sec->kept_section;
10277 if (kept != NULL)
10278 {
10279 if ((kept->flags & SEC_GROUP) != 0)
10280 kept = match_group_member (sec, kept, info);
10281 if (kept != NULL
10282 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10283 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10284 kept = NULL;
10285 sec->kept_section = kept;
10286 }
10287 return kept;
10288 }
10289
10290 /* Link an input file into the linker output file. This function
10291 handles all the sections and relocations of the input file at once.
10292 This is so that we only have to read the local symbols once, and
10293 don't have to keep them in memory. */
10294
10295 static bfd_boolean
10296 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10297 {
10298 int (*relocate_section)
10299 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10300 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10301 bfd *output_bfd;
10302 Elf_Internal_Shdr *symtab_hdr;
10303 size_t locsymcount;
10304 size_t extsymoff;
10305 Elf_Internal_Sym *isymbuf;
10306 Elf_Internal_Sym *isym;
10307 Elf_Internal_Sym *isymend;
10308 long *pindex;
10309 asection **ppsection;
10310 asection *o;
10311 const struct elf_backend_data *bed;
10312 struct elf_link_hash_entry **sym_hashes;
10313 bfd_size_type address_size;
10314 bfd_vma r_type_mask;
10315 int r_sym_shift;
10316 bfd_boolean have_file_sym = FALSE;
10317
10318 output_bfd = flinfo->output_bfd;
10319 bed = get_elf_backend_data (output_bfd);
10320 relocate_section = bed->elf_backend_relocate_section;
10321
10322 /* If this is a dynamic object, we don't want to do anything here:
10323 we don't want the local symbols, and we don't want the section
10324 contents. */
10325 if ((input_bfd->flags & DYNAMIC) != 0)
10326 return TRUE;
10327
10328 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10329 if (elf_bad_symtab (input_bfd))
10330 {
10331 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10332 extsymoff = 0;
10333 }
10334 else
10335 {
10336 locsymcount = symtab_hdr->sh_info;
10337 extsymoff = symtab_hdr->sh_info;
10338 }
10339
10340 /* Read the local symbols. */
10341 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10342 if (isymbuf == NULL && locsymcount != 0)
10343 {
10344 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10345 flinfo->internal_syms,
10346 flinfo->external_syms,
10347 flinfo->locsym_shndx);
10348 if (isymbuf == NULL)
10349 return FALSE;
10350 }
10351
10352 /* Find local symbol sections and adjust values of symbols in
10353 SEC_MERGE sections. Write out those local symbols we know are
10354 going into the output file. */
10355 isymend = isymbuf + locsymcount;
10356 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10357 isym < isymend;
10358 isym++, pindex++, ppsection++)
10359 {
10360 asection *isec;
10361 const char *name;
10362 Elf_Internal_Sym osym;
10363 long indx;
10364 int ret;
10365
10366 *pindex = -1;
10367
10368 if (elf_bad_symtab (input_bfd))
10369 {
10370 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10371 {
10372 *ppsection = NULL;
10373 continue;
10374 }
10375 }
10376
10377 if (isym->st_shndx == SHN_UNDEF)
10378 isec = bfd_und_section_ptr;
10379 else if (isym->st_shndx == SHN_ABS)
10380 isec = bfd_abs_section_ptr;
10381 else if (isym->st_shndx == SHN_COMMON)
10382 isec = bfd_com_section_ptr;
10383 else
10384 {
10385 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10386 if (isec == NULL)
10387 {
10388 /* Don't attempt to output symbols with st_shnx in the
10389 reserved range other than SHN_ABS and SHN_COMMON. */
10390 *ppsection = NULL;
10391 continue;
10392 }
10393 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10394 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10395 isym->st_value =
10396 _bfd_merged_section_offset (output_bfd, &isec,
10397 elf_section_data (isec)->sec_info,
10398 isym->st_value);
10399 }
10400
10401 *ppsection = isec;
10402
10403 /* Don't output the first, undefined, symbol. In fact, don't
10404 output any undefined local symbol. */
10405 if (isec == bfd_und_section_ptr)
10406 continue;
10407
10408 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10409 {
10410 /* We never output section symbols. Instead, we use the
10411 section symbol of the corresponding section in the output
10412 file. */
10413 continue;
10414 }
10415
10416 /* If we are stripping all symbols, we don't want to output this
10417 one. */
10418 if (flinfo->info->strip == strip_all)
10419 continue;
10420
10421 /* If we are discarding all local symbols, we don't want to
10422 output this one. If we are generating a relocatable output
10423 file, then some of the local symbols may be required by
10424 relocs; we output them below as we discover that they are
10425 needed. */
10426 if (flinfo->info->discard == discard_all)
10427 continue;
10428
10429 /* If this symbol is defined in a section which we are
10430 discarding, we don't need to keep it. */
10431 if (isym->st_shndx != SHN_UNDEF
10432 && isym->st_shndx < SHN_LORESERVE
10433 && bfd_section_removed_from_list (output_bfd,
10434 isec->output_section))
10435 continue;
10436
10437 /* Get the name of the symbol. */
10438 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10439 isym->st_name);
10440 if (name == NULL)
10441 return FALSE;
10442
10443 /* See if we are discarding symbols with this name. */
10444 if ((flinfo->info->strip == strip_some
10445 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10446 == NULL))
10447 || (((flinfo->info->discard == discard_sec_merge
10448 && (isec->flags & SEC_MERGE)
10449 && !bfd_link_relocatable (flinfo->info))
10450 || flinfo->info->discard == discard_l)
10451 && bfd_is_local_label_name (input_bfd, name)))
10452 continue;
10453
10454 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10455 {
10456 if (input_bfd->lto_output)
10457 /* -flto puts a temp file name here. This means builds
10458 are not reproducible. Discard the symbol. */
10459 continue;
10460 have_file_sym = TRUE;
10461 flinfo->filesym_count += 1;
10462 }
10463 if (!have_file_sym)
10464 {
10465 /* In the absence of debug info, bfd_find_nearest_line uses
10466 FILE symbols to determine the source file for local
10467 function symbols. Provide a FILE symbol here if input
10468 files lack such, so that their symbols won't be
10469 associated with a previous input file. It's not the
10470 source file, but the best we can do. */
10471 have_file_sym = TRUE;
10472 flinfo->filesym_count += 1;
10473 memset (&osym, 0, sizeof (osym));
10474 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10475 osym.st_shndx = SHN_ABS;
10476 if (!elf_link_output_symstrtab (flinfo,
10477 (input_bfd->lto_output ? NULL
10478 : input_bfd->filename),
10479 &osym, bfd_abs_section_ptr,
10480 NULL))
10481 return FALSE;
10482 }
10483
10484 osym = *isym;
10485
10486 /* Adjust the section index for the output file. */
10487 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10488 isec->output_section);
10489 if (osym.st_shndx == SHN_BAD)
10490 return FALSE;
10491
10492 /* ELF symbols in relocatable files are section relative, but
10493 in executable files they are virtual addresses. Note that
10494 this code assumes that all ELF sections have an associated
10495 BFD section with a reasonable value for output_offset; below
10496 we assume that they also have a reasonable value for
10497 output_section. Any special sections must be set up to meet
10498 these requirements. */
10499 osym.st_value += isec->output_offset;
10500 if (!bfd_link_relocatable (flinfo->info))
10501 {
10502 osym.st_value += isec->output_section->vma;
10503 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10504 {
10505 /* STT_TLS symbols are relative to PT_TLS segment base. */
10506 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10507 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10508 }
10509 }
10510
10511 indx = bfd_get_symcount (output_bfd);
10512 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10513 if (ret == 0)
10514 return FALSE;
10515 else if (ret == 1)
10516 *pindex = indx;
10517 }
10518
10519 if (bed->s->arch_size == 32)
10520 {
10521 r_type_mask = 0xff;
10522 r_sym_shift = 8;
10523 address_size = 4;
10524 }
10525 else
10526 {
10527 r_type_mask = 0xffffffff;
10528 r_sym_shift = 32;
10529 address_size = 8;
10530 }
10531
10532 /* Relocate the contents of each section. */
10533 sym_hashes = elf_sym_hashes (input_bfd);
10534 for (o = input_bfd->sections; o != NULL; o = o->next)
10535 {
10536 bfd_byte *contents;
10537
10538 if (! o->linker_mark)
10539 {
10540 /* This section was omitted from the link. */
10541 continue;
10542 }
10543
10544 if (!flinfo->info->resolve_section_groups
10545 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10546 {
10547 /* Deal with the group signature symbol. */
10548 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10549 unsigned long symndx = sec_data->this_hdr.sh_info;
10550 asection *osec = o->output_section;
10551
10552 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10553 if (symndx >= locsymcount
10554 || (elf_bad_symtab (input_bfd)
10555 && flinfo->sections[symndx] == NULL))
10556 {
10557 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10558 while (h->root.type == bfd_link_hash_indirect
10559 || h->root.type == bfd_link_hash_warning)
10560 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10561 /* Arrange for symbol to be output. */
10562 h->indx = -2;
10563 elf_section_data (osec)->this_hdr.sh_info = -2;
10564 }
10565 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10566 {
10567 /* We'll use the output section target_index. */
10568 asection *sec = flinfo->sections[symndx]->output_section;
10569 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10570 }
10571 else
10572 {
10573 if (flinfo->indices[symndx] == -1)
10574 {
10575 /* Otherwise output the local symbol now. */
10576 Elf_Internal_Sym sym = isymbuf[symndx];
10577 asection *sec = flinfo->sections[symndx]->output_section;
10578 const char *name;
10579 long indx;
10580 int ret;
10581
10582 name = bfd_elf_string_from_elf_section (input_bfd,
10583 symtab_hdr->sh_link,
10584 sym.st_name);
10585 if (name == NULL)
10586 return FALSE;
10587
10588 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10589 sec);
10590 if (sym.st_shndx == SHN_BAD)
10591 return FALSE;
10592
10593 sym.st_value += o->output_offset;
10594
10595 indx = bfd_get_symcount (output_bfd);
10596 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10597 NULL);
10598 if (ret == 0)
10599 return FALSE;
10600 else if (ret == 1)
10601 flinfo->indices[symndx] = indx;
10602 else
10603 abort ();
10604 }
10605 elf_section_data (osec)->this_hdr.sh_info
10606 = flinfo->indices[symndx];
10607 }
10608 }
10609
10610 if ((o->flags & SEC_HAS_CONTENTS) == 0
10611 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10612 continue;
10613
10614 if ((o->flags & SEC_LINKER_CREATED) != 0)
10615 {
10616 /* Section was created by _bfd_elf_link_create_dynamic_sections
10617 or somesuch. */
10618 continue;
10619 }
10620
10621 /* Get the contents of the section. They have been cached by a
10622 relaxation routine. Note that o is a section in an input
10623 file, so the contents field will not have been set by any of
10624 the routines which work on output files. */
10625 if (elf_section_data (o)->this_hdr.contents != NULL)
10626 {
10627 contents = elf_section_data (o)->this_hdr.contents;
10628 if (bed->caches_rawsize
10629 && o->rawsize != 0
10630 && o->rawsize < o->size)
10631 {
10632 memcpy (flinfo->contents, contents, o->rawsize);
10633 contents = flinfo->contents;
10634 }
10635 }
10636 else
10637 {
10638 contents = flinfo->contents;
10639 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10640 return FALSE;
10641 }
10642
10643 if ((o->flags & SEC_RELOC) != 0)
10644 {
10645 Elf_Internal_Rela *internal_relocs;
10646 Elf_Internal_Rela *rel, *relend;
10647 int action_discarded;
10648 int ret;
10649
10650 /* Get the swapped relocs. */
10651 internal_relocs
10652 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10653 flinfo->internal_relocs, FALSE);
10654 if (internal_relocs == NULL
10655 && o->reloc_count > 0)
10656 return FALSE;
10657
10658 /* We need to reverse-copy input .ctors/.dtors sections if
10659 they are placed in .init_array/.finit_array for output. */
10660 if (o->size > address_size
10661 && ((strncmp (o->name, ".ctors", 6) == 0
10662 && strcmp (o->output_section->name,
10663 ".init_array") == 0)
10664 || (strncmp (o->name, ".dtors", 6) == 0
10665 && strcmp (o->output_section->name,
10666 ".fini_array") == 0))
10667 && (o->name[6] == 0 || o->name[6] == '.'))
10668 {
10669 if (o->size * bed->s->int_rels_per_ext_rel
10670 != o->reloc_count * address_size)
10671 {
10672 _bfd_error_handler
10673 /* xgettext:c-format */
10674 (_("error: %pB: size of section %pA is not "
10675 "multiple of address size"),
10676 input_bfd, o);
10677 bfd_set_error (bfd_error_bad_value);
10678 return FALSE;
10679 }
10680 o->flags |= SEC_ELF_REVERSE_COPY;
10681 }
10682
10683 action_discarded = -1;
10684 if (!elf_section_ignore_discarded_relocs (o))
10685 action_discarded = (*bed->action_discarded) (o);
10686
10687 /* Run through the relocs evaluating complex reloc symbols and
10688 looking for relocs against symbols from discarded sections
10689 or section symbols from removed link-once sections.
10690 Complain about relocs against discarded sections. Zero
10691 relocs against removed link-once sections. */
10692
10693 rel = internal_relocs;
10694 relend = rel + o->reloc_count;
10695 for ( ; rel < relend; rel++)
10696 {
10697 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10698 unsigned int s_type;
10699 asection **ps, *sec;
10700 struct elf_link_hash_entry *h = NULL;
10701 const char *sym_name;
10702
10703 if (r_symndx == STN_UNDEF)
10704 continue;
10705
10706 if (r_symndx >= locsymcount
10707 || (elf_bad_symtab (input_bfd)
10708 && flinfo->sections[r_symndx] == NULL))
10709 {
10710 h = sym_hashes[r_symndx - extsymoff];
10711
10712 /* Badly formatted input files can contain relocs that
10713 reference non-existant symbols. Check here so that
10714 we do not seg fault. */
10715 if (h == NULL)
10716 {
10717 _bfd_error_handler
10718 /* xgettext:c-format */
10719 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10720 "that references a non-existent global symbol"),
10721 input_bfd, (uint64_t) rel->r_info, o);
10722 bfd_set_error (bfd_error_bad_value);
10723 return FALSE;
10724 }
10725
10726 while (h->root.type == bfd_link_hash_indirect
10727 || h->root.type == bfd_link_hash_warning)
10728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10729
10730 s_type = h->type;
10731
10732 /* If a plugin symbol is referenced from a non-IR file,
10733 mark the symbol as undefined. Note that the
10734 linker may attach linker created dynamic sections
10735 to the plugin bfd. Symbols defined in linker
10736 created sections are not plugin symbols. */
10737 if ((h->root.non_ir_ref_regular
10738 || h->root.non_ir_ref_dynamic)
10739 && (h->root.type == bfd_link_hash_defined
10740 || h->root.type == bfd_link_hash_defweak)
10741 && (h->root.u.def.section->flags
10742 & SEC_LINKER_CREATED) == 0
10743 && h->root.u.def.section->owner != NULL
10744 && (h->root.u.def.section->owner->flags
10745 & BFD_PLUGIN) != 0)
10746 {
10747 h->root.type = bfd_link_hash_undefined;
10748 h->root.u.undef.abfd = h->root.u.def.section->owner;
10749 }
10750
10751 ps = NULL;
10752 if (h->root.type == bfd_link_hash_defined
10753 || h->root.type == bfd_link_hash_defweak)
10754 ps = &h->root.u.def.section;
10755
10756 sym_name = h->root.root.string;
10757 }
10758 else
10759 {
10760 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10761
10762 s_type = ELF_ST_TYPE (sym->st_info);
10763 ps = &flinfo->sections[r_symndx];
10764 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10765 sym, *ps);
10766 }
10767
10768 if ((s_type == STT_RELC || s_type == STT_SRELC)
10769 && !bfd_link_relocatable (flinfo->info))
10770 {
10771 bfd_vma val;
10772 bfd_vma dot = (rel->r_offset
10773 + o->output_offset + o->output_section->vma);
10774 #ifdef DEBUG
10775 printf ("Encountered a complex symbol!");
10776 printf (" (input_bfd %s, section %s, reloc %ld\n",
10777 input_bfd->filename, o->name,
10778 (long) (rel - internal_relocs));
10779 printf (" symbol: idx %8.8lx, name %s\n",
10780 r_symndx, sym_name);
10781 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10782 (unsigned long) rel->r_info,
10783 (unsigned long) rel->r_offset);
10784 #endif
10785 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10786 isymbuf, locsymcount, s_type == STT_SRELC))
10787 return FALSE;
10788
10789 /* Symbol evaluated OK. Update to absolute value. */
10790 set_symbol_value (input_bfd, isymbuf, locsymcount,
10791 r_symndx, val);
10792 continue;
10793 }
10794
10795 if (action_discarded != -1 && ps != NULL)
10796 {
10797 /* Complain if the definition comes from a
10798 discarded section. */
10799 if ((sec = *ps) != NULL && discarded_section (sec))
10800 {
10801 BFD_ASSERT (r_symndx != STN_UNDEF);
10802 if (action_discarded & COMPLAIN)
10803 (*flinfo->info->callbacks->einfo)
10804 /* xgettext:c-format */
10805 (_("%X`%s' referenced in section `%pA' of %pB: "
10806 "defined in discarded section `%pA' of %pB\n"),
10807 sym_name, o, input_bfd, sec, sec->owner);
10808
10809 /* Try to do the best we can to support buggy old
10810 versions of gcc. Pretend that the symbol is
10811 really defined in the kept linkonce section.
10812 FIXME: This is quite broken. Modifying the
10813 symbol here means we will be changing all later
10814 uses of the symbol, not just in this section. */
10815 if (action_discarded & PRETEND)
10816 {
10817 asection *kept;
10818
10819 kept = _bfd_elf_check_kept_section (sec,
10820 flinfo->info);
10821 if (kept != NULL)
10822 {
10823 *ps = kept;
10824 continue;
10825 }
10826 }
10827 }
10828 }
10829 }
10830
10831 /* Relocate the section by invoking a back end routine.
10832
10833 The back end routine is responsible for adjusting the
10834 section contents as necessary, and (if using Rela relocs
10835 and generating a relocatable output file) adjusting the
10836 reloc addend as necessary.
10837
10838 The back end routine does not have to worry about setting
10839 the reloc address or the reloc symbol index.
10840
10841 The back end routine is given a pointer to the swapped in
10842 internal symbols, and can access the hash table entries
10843 for the external symbols via elf_sym_hashes (input_bfd).
10844
10845 When generating relocatable output, the back end routine
10846 must handle STB_LOCAL/STT_SECTION symbols specially. The
10847 output symbol is going to be a section symbol
10848 corresponding to the output section, which will require
10849 the addend to be adjusted. */
10850
10851 ret = (*relocate_section) (output_bfd, flinfo->info,
10852 input_bfd, o, contents,
10853 internal_relocs,
10854 isymbuf,
10855 flinfo->sections);
10856 if (!ret)
10857 return FALSE;
10858
10859 if (ret == 2
10860 || bfd_link_relocatable (flinfo->info)
10861 || flinfo->info->emitrelocations)
10862 {
10863 Elf_Internal_Rela *irela;
10864 Elf_Internal_Rela *irelaend, *irelamid;
10865 bfd_vma last_offset;
10866 struct elf_link_hash_entry **rel_hash;
10867 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10868 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10869 unsigned int next_erel;
10870 bfd_boolean rela_normal;
10871 struct bfd_elf_section_data *esdi, *esdo;
10872
10873 esdi = elf_section_data (o);
10874 esdo = elf_section_data (o->output_section);
10875 rela_normal = FALSE;
10876
10877 /* Adjust the reloc addresses and symbol indices. */
10878
10879 irela = internal_relocs;
10880 irelaend = irela + o->reloc_count;
10881 rel_hash = esdo->rel.hashes + esdo->rel.count;
10882 /* We start processing the REL relocs, if any. When we reach
10883 IRELAMID in the loop, we switch to the RELA relocs. */
10884 irelamid = irela;
10885 if (esdi->rel.hdr != NULL)
10886 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10887 * bed->s->int_rels_per_ext_rel);
10888 rel_hash_list = rel_hash;
10889 rela_hash_list = NULL;
10890 last_offset = o->output_offset;
10891 if (!bfd_link_relocatable (flinfo->info))
10892 last_offset += o->output_section->vma;
10893 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10894 {
10895 unsigned long r_symndx;
10896 asection *sec;
10897 Elf_Internal_Sym sym;
10898
10899 if (next_erel == bed->s->int_rels_per_ext_rel)
10900 {
10901 rel_hash++;
10902 next_erel = 0;
10903 }
10904
10905 if (irela == irelamid)
10906 {
10907 rel_hash = esdo->rela.hashes + esdo->rela.count;
10908 rela_hash_list = rel_hash;
10909 rela_normal = bed->rela_normal;
10910 }
10911
10912 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10913 flinfo->info, o,
10914 irela->r_offset);
10915 if (irela->r_offset >= (bfd_vma) -2)
10916 {
10917 /* This is a reloc for a deleted entry or somesuch.
10918 Turn it into an R_*_NONE reloc, at the same
10919 offset as the last reloc. elf_eh_frame.c and
10920 bfd_elf_discard_info rely on reloc offsets
10921 being ordered. */
10922 irela->r_offset = last_offset;
10923 irela->r_info = 0;
10924 irela->r_addend = 0;
10925 continue;
10926 }
10927
10928 irela->r_offset += o->output_offset;
10929
10930 /* Relocs in an executable have to be virtual addresses. */
10931 if (!bfd_link_relocatable (flinfo->info))
10932 irela->r_offset += o->output_section->vma;
10933
10934 last_offset = irela->r_offset;
10935
10936 r_symndx = irela->r_info >> r_sym_shift;
10937 if (r_symndx == STN_UNDEF)
10938 continue;
10939
10940 if (r_symndx >= locsymcount
10941 || (elf_bad_symtab (input_bfd)
10942 && flinfo->sections[r_symndx] == NULL))
10943 {
10944 struct elf_link_hash_entry *rh;
10945 unsigned long indx;
10946
10947 /* This is a reloc against a global symbol. We
10948 have not yet output all the local symbols, so
10949 we do not know the symbol index of any global
10950 symbol. We set the rel_hash entry for this
10951 reloc to point to the global hash table entry
10952 for this symbol. The symbol index is then
10953 set at the end of bfd_elf_final_link. */
10954 indx = r_symndx - extsymoff;
10955 rh = elf_sym_hashes (input_bfd)[indx];
10956 while (rh->root.type == bfd_link_hash_indirect
10957 || rh->root.type == bfd_link_hash_warning)
10958 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10959
10960 /* Setting the index to -2 tells
10961 elf_link_output_extsym that this symbol is
10962 used by a reloc. */
10963 BFD_ASSERT (rh->indx < 0);
10964 rh->indx = -2;
10965 *rel_hash = rh;
10966
10967 continue;
10968 }
10969
10970 /* This is a reloc against a local symbol. */
10971
10972 *rel_hash = NULL;
10973 sym = isymbuf[r_symndx];
10974 sec = flinfo->sections[r_symndx];
10975 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10976 {
10977 /* I suppose the backend ought to fill in the
10978 section of any STT_SECTION symbol against a
10979 processor specific section. */
10980 r_symndx = STN_UNDEF;
10981 if (bfd_is_abs_section (sec))
10982 ;
10983 else if (sec == NULL || sec->owner == NULL)
10984 {
10985 bfd_set_error (bfd_error_bad_value);
10986 return FALSE;
10987 }
10988 else
10989 {
10990 asection *osec = sec->output_section;
10991
10992 /* If we have discarded a section, the output
10993 section will be the absolute section. In
10994 case of discarded SEC_MERGE sections, use
10995 the kept section. relocate_section should
10996 have already handled discarded linkonce
10997 sections. */
10998 if (bfd_is_abs_section (osec)
10999 && sec->kept_section != NULL
11000 && sec->kept_section->output_section != NULL)
11001 {
11002 osec = sec->kept_section->output_section;
11003 irela->r_addend -= osec->vma;
11004 }
11005
11006 if (!bfd_is_abs_section (osec))
11007 {
11008 r_symndx = osec->target_index;
11009 if (r_symndx == STN_UNDEF)
11010 {
11011 irela->r_addend += osec->vma;
11012 osec = _bfd_nearby_section (output_bfd, osec,
11013 osec->vma);
11014 irela->r_addend -= osec->vma;
11015 r_symndx = osec->target_index;
11016 }
11017 }
11018 }
11019
11020 /* Adjust the addend according to where the
11021 section winds up in the output section. */
11022 if (rela_normal)
11023 irela->r_addend += sec->output_offset;
11024 }
11025 else
11026 {
11027 if (flinfo->indices[r_symndx] == -1)
11028 {
11029 unsigned long shlink;
11030 const char *name;
11031 asection *osec;
11032 long indx;
11033
11034 if (flinfo->info->strip == strip_all)
11035 {
11036 /* You can't do ld -r -s. */
11037 bfd_set_error (bfd_error_invalid_operation);
11038 return FALSE;
11039 }
11040
11041 /* This symbol was skipped earlier, but
11042 since it is needed by a reloc, we
11043 must output it now. */
11044 shlink = symtab_hdr->sh_link;
11045 name = (bfd_elf_string_from_elf_section
11046 (input_bfd, shlink, sym.st_name));
11047 if (name == NULL)
11048 return FALSE;
11049
11050 osec = sec->output_section;
11051 sym.st_shndx =
11052 _bfd_elf_section_from_bfd_section (output_bfd,
11053 osec);
11054 if (sym.st_shndx == SHN_BAD)
11055 return FALSE;
11056
11057 sym.st_value += sec->output_offset;
11058 if (!bfd_link_relocatable (flinfo->info))
11059 {
11060 sym.st_value += osec->vma;
11061 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11062 {
11063 /* STT_TLS symbols are relative to PT_TLS
11064 segment base. */
11065 BFD_ASSERT (elf_hash_table (flinfo->info)
11066 ->tls_sec != NULL);
11067 sym.st_value -= (elf_hash_table (flinfo->info)
11068 ->tls_sec->vma);
11069 }
11070 }
11071
11072 indx = bfd_get_symcount (output_bfd);
11073 ret = elf_link_output_symstrtab (flinfo, name,
11074 &sym, sec,
11075 NULL);
11076 if (ret == 0)
11077 return FALSE;
11078 else if (ret == 1)
11079 flinfo->indices[r_symndx] = indx;
11080 else
11081 abort ();
11082 }
11083
11084 r_symndx = flinfo->indices[r_symndx];
11085 }
11086
11087 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11088 | (irela->r_info & r_type_mask));
11089 }
11090
11091 /* Swap out the relocs. */
11092 input_rel_hdr = esdi->rel.hdr;
11093 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11094 {
11095 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11096 input_rel_hdr,
11097 internal_relocs,
11098 rel_hash_list))
11099 return FALSE;
11100 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11101 * bed->s->int_rels_per_ext_rel);
11102 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11103 }
11104
11105 input_rela_hdr = esdi->rela.hdr;
11106 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11107 {
11108 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11109 input_rela_hdr,
11110 internal_relocs,
11111 rela_hash_list))
11112 return FALSE;
11113 }
11114 }
11115 }
11116
11117 /* Write out the modified section contents. */
11118 if (bed->elf_backend_write_section
11119 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11120 contents))
11121 {
11122 /* Section written out. */
11123 }
11124 else switch (o->sec_info_type)
11125 {
11126 case SEC_INFO_TYPE_STABS:
11127 if (! (_bfd_write_section_stabs
11128 (output_bfd,
11129 &elf_hash_table (flinfo->info)->stab_info,
11130 o, &elf_section_data (o)->sec_info, contents)))
11131 return FALSE;
11132 break;
11133 case SEC_INFO_TYPE_MERGE:
11134 if (! _bfd_write_merged_section (output_bfd, o,
11135 elf_section_data (o)->sec_info))
11136 return FALSE;
11137 break;
11138 case SEC_INFO_TYPE_EH_FRAME:
11139 {
11140 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11141 o, contents))
11142 return FALSE;
11143 }
11144 break;
11145 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11146 {
11147 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11148 flinfo->info,
11149 o, contents))
11150 return FALSE;
11151 }
11152 break;
11153 default:
11154 {
11155 if (! (o->flags & SEC_EXCLUDE))
11156 {
11157 file_ptr offset = (file_ptr) o->output_offset;
11158 bfd_size_type todo = o->size;
11159
11160 offset *= bfd_octets_per_byte (output_bfd);
11161
11162 if ((o->flags & SEC_ELF_REVERSE_COPY))
11163 {
11164 /* Reverse-copy input section to output. */
11165 do
11166 {
11167 todo -= address_size;
11168 if (! bfd_set_section_contents (output_bfd,
11169 o->output_section,
11170 contents + todo,
11171 offset,
11172 address_size))
11173 return FALSE;
11174 if (todo == 0)
11175 break;
11176 offset += address_size;
11177 }
11178 while (1);
11179 }
11180 else if (! bfd_set_section_contents (output_bfd,
11181 o->output_section,
11182 contents,
11183 offset, todo))
11184 return FALSE;
11185 }
11186 }
11187 break;
11188 }
11189 }
11190
11191 return TRUE;
11192 }
11193
11194 /* Generate a reloc when linking an ELF file. This is a reloc
11195 requested by the linker, and does not come from any input file. This
11196 is used to build constructor and destructor tables when linking
11197 with -Ur. */
11198
11199 static bfd_boolean
11200 elf_reloc_link_order (bfd *output_bfd,
11201 struct bfd_link_info *info,
11202 asection *output_section,
11203 struct bfd_link_order *link_order)
11204 {
11205 reloc_howto_type *howto;
11206 long indx;
11207 bfd_vma offset;
11208 bfd_vma addend;
11209 struct bfd_elf_section_reloc_data *reldata;
11210 struct elf_link_hash_entry **rel_hash_ptr;
11211 Elf_Internal_Shdr *rel_hdr;
11212 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11213 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11214 bfd_byte *erel;
11215 unsigned int i;
11216 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11217
11218 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11219 if (howto == NULL)
11220 {
11221 bfd_set_error (bfd_error_bad_value);
11222 return FALSE;
11223 }
11224
11225 addend = link_order->u.reloc.p->addend;
11226
11227 if (esdo->rel.hdr)
11228 reldata = &esdo->rel;
11229 else if (esdo->rela.hdr)
11230 reldata = &esdo->rela;
11231 else
11232 {
11233 reldata = NULL;
11234 BFD_ASSERT (0);
11235 }
11236
11237 /* Figure out the symbol index. */
11238 rel_hash_ptr = reldata->hashes + reldata->count;
11239 if (link_order->type == bfd_section_reloc_link_order)
11240 {
11241 indx = link_order->u.reloc.p->u.section->target_index;
11242 BFD_ASSERT (indx != 0);
11243 *rel_hash_ptr = NULL;
11244 }
11245 else
11246 {
11247 struct elf_link_hash_entry *h;
11248
11249 /* Treat a reloc against a defined symbol as though it were
11250 actually against the section. */
11251 h = ((struct elf_link_hash_entry *)
11252 bfd_wrapped_link_hash_lookup (output_bfd, info,
11253 link_order->u.reloc.p->u.name,
11254 FALSE, FALSE, TRUE));
11255 if (h != NULL
11256 && (h->root.type == bfd_link_hash_defined
11257 || h->root.type == bfd_link_hash_defweak))
11258 {
11259 asection *section;
11260
11261 section = h->root.u.def.section;
11262 indx = section->output_section->target_index;
11263 *rel_hash_ptr = NULL;
11264 /* It seems that we ought to add the symbol value to the
11265 addend here, but in practice it has already been added
11266 because it was passed to constructor_callback. */
11267 addend += section->output_section->vma + section->output_offset;
11268 }
11269 else if (h != NULL)
11270 {
11271 /* Setting the index to -2 tells elf_link_output_extsym that
11272 this symbol is used by a reloc. */
11273 h->indx = -2;
11274 *rel_hash_ptr = h;
11275 indx = 0;
11276 }
11277 else
11278 {
11279 (*info->callbacks->unattached_reloc)
11280 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11281 indx = 0;
11282 }
11283 }
11284
11285 /* If this is an inplace reloc, we must write the addend into the
11286 object file. */
11287 if (howto->partial_inplace && addend != 0)
11288 {
11289 bfd_size_type size;
11290 bfd_reloc_status_type rstat;
11291 bfd_byte *buf;
11292 bfd_boolean ok;
11293 const char *sym_name;
11294
11295 size = (bfd_size_type) bfd_get_reloc_size (howto);
11296 buf = (bfd_byte *) bfd_zmalloc (size);
11297 if (buf == NULL && size != 0)
11298 return FALSE;
11299 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11300 switch (rstat)
11301 {
11302 case bfd_reloc_ok:
11303 break;
11304
11305 default:
11306 case bfd_reloc_outofrange:
11307 abort ();
11308
11309 case bfd_reloc_overflow:
11310 if (link_order->type == bfd_section_reloc_link_order)
11311 sym_name = bfd_section_name (output_bfd,
11312 link_order->u.reloc.p->u.section);
11313 else
11314 sym_name = link_order->u.reloc.p->u.name;
11315 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11316 howto->name, addend, NULL, NULL,
11317 (bfd_vma) 0);
11318 break;
11319 }
11320
11321 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11322 link_order->offset
11323 * bfd_octets_per_byte (output_bfd),
11324 size);
11325 free (buf);
11326 if (! ok)
11327 return FALSE;
11328 }
11329
11330 /* The address of a reloc is relative to the section in a
11331 relocatable file, and is a virtual address in an executable
11332 file. */
11333 offset = link_order->offset;
11334 if (! bfd_link_relocatable (info))
11335 offset += output_section->vma;
11336
11337 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11338 {
11339 irel[i].r_offset = offset;
11340 irel[i].r_info = 0;
11341 irel[i].r_addend = 0;
11342 }
11343 if (bed->s->arch_size == 32)
11344 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11345 else
11346 #ifdef BFD64
11347 {
11348 bfd_uint64_t indx64 = indx;
11349 irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
11350 }
11351 #else
11352 BFD_FAIL();
11353 #endif
11354
11355 rel_hdr = reldata->hdr;
11356 erel = rel_hdr->contents;
11357 if (rel_hdr->sh_type == SHT_REL)
11358 {
11359 erel += reldata->count * bed->s->sizeof_rel;
11360 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11361 }
11362 else
11363 {
11364 irel[0].r_addend = addend;
11365 erel += reldata->count * bed->s->sizeof_rela;
11366 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11367 }
11368
11369 ++reldata->count;
11370
11371 return TRUE;
11372 }
11373
11374
11375 /* Get the output vma of the section pointed to by the sh_link field. */
11376
11377 static bfd_vma
11378 elf_get_linked_section_vma (struct bfd_link_order *p)
11379 {
11380 Elf_Internal_Shdr **elf_shdrp;
11381 asection *s;
11382 int elfsec;
11383
11384 s = p->u.indirect.section;
11385 elf_shdrp = elf_elfsections (s->owner);
11386 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11387 elfsec = elf_shdrp[elfsec]->sh_link;
11388 /* PR 290:
11389 The Intel C compiler generates SHT_IA_64_UNWIND with
11390 SHF_LINK_ORDER. But it doesn't set the sh_link or
11391 sh_info fields. Hence we could get the situation
11392 where elfsec is 0. */
11393 if (elfsec == 0)
11394 {
11395 const struct elf_backend_data *bed
11396 = get_elf_backend_data (s->owner);
11397 if (bed->link_order_error_handler)
11398 bed->link_order_error_handler
11399 /* xgettext:c-format */
11400 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11401 return 0;
11402 }
11403 else
11404 {
11405 s = elf_shdrp[elfsec]->bfd_section;
11406 return s->output_section->vma + s->output_offset;
11407 }
11408 }
11409
11410
11411 /* Compare two sections based on the locations of the sections they are
11412 linked to. Used by elf_fixup_link_order. */
11413
11414 static int
11415 compare_link_order (const void * a, const void * b)
11416 {
11417 bfd_vma apos;
11418 bfd_vma bpos;
11419
11420 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11421 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11422 if (apos < bpos)
11423 return -1;
11424 return apos > bpos;
11425 }
11426
11427
11428 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11429 order as their linked sections. Returns false if this could not be done
11430 because an output section includes both ordered and unordered
11431 sections. Ideally we'd do this in the linker proper. */
11432
11433 static bfd_boolean
11434 elf_fixup_link_order (bfd *abfd, asection *o)
11435 {
11436 int seen_linkorder;
11437 int seen_other;
11438 int n;
11439 struct bfd_link_order *p;
11440 bfd *sub;
11441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11442 unsigned elfsec;
11443 struct bfd_link_order **sections;
11444 asection *s, *other_sec, *linkorder_sec;
11445 bfd_vma offset;
11446
11447 other_sec = NULL;
11448 linkorder_sec = NULL;
11449 seen_other = 0;
11450 seen_linkorder = 0;
11451 for (p = o->map_head.link_order; p != NULL; p = p->next)
11452 {
11453 if (p->type == bfd_indirect_link_order)
11454 {
11455 s = p->u.indirect.section;
11456 sub = s->owner;
11457 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11458 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11459 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11460 && elfsec < elf_numsections (sub)
11461 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11462 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11463 {
11464 seen_linkorder++;
11465 linkorder_sec = s;
11466 }
11467 else
11468 {
11469 seen_other++;
11470 other_sec = s;
11471 }
11472 }
11473 else
11474 seen_other++;
11475
11476 if (seen_other && seen_linkorder)
11477 {
11478 if (other_sec && linkorder_sec)
11479 _bfd_error_handler
11480 /* xgettext:c-format */
11481 (_("%pA has both ordered [`%pA' in %pB] "
11482 "and unordered [`%pA' in %pB] sections"),
11483 o, linkorder_sec, linkorder_sec->owner,
11484 other_sec, other_sec->owner);
11485 else
11486 _bfd_error_handler
11487 (_("%pA has both ordered and unordered sections"), o);
11488 bfd_set_error (bfd_error_bad_value);
11489 return FALSE;
11490 }
11491 }
11492
11493 if (!seen_linkorder)
11494 return TRUE;
11495
11496 sections = (struct bfd_link_order **)
11497 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11498 if (sections == NULL)
11499 return FALSE;
11500 seen_linkorder = 0;
11501
11502 for (p = o->map_head.link_order; p != NULL; p = p->next)
11503 {
11504 sections[seen_linkorder++] = p;
11505 }
11506 /* Sort the input sections in the order of their linked section. */
11507 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11508 compare_link_order);
11509
11510 /* Change the offsets of the sections. */
11511 offset = 0;
11512 for (n = 0; n < seen_linkorder; n++)
11513 {
11514 s = sections[n]->u.indirect.section;
11515 offset &= ~(bfd_vma) 0 << s->alignment_power;
11516 s->output_offset = offset / bfd_octets_per_byte (abfd);
11517 sections[n]->offset = offset;
11518 offset += sections[n]->size;
11519 }
11520
11521 free (sections);
11522 return TRUE;
11523 }
11524
11525 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11526 Returns TRUE upon success, FALSE otherwise. */
11527
11528 static bfd_boolean
11529 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11530 {
11531 bfd_boolean ret = FALSE;
11532 bfd *implib_bfd;
11533 const struct elf_backend_data *bed;
11534 flagword flags;
11535 enum bfd_architecture arch;
11536 unsigned int mach;
11537 asymbol **sympp = NULL;
11538 long symsize;
11539 long symcount;
11540 long src_count;
11541 elf_symbol_type *osymbuf;
11542
11543 implib_bfd = info->out_implib_bfd;
11544 bed = get_elf_backend_data (abfd);
11545
11546 if (!bfd_set_format (implib_bfd, bfd_object))
11547 return FALSE;
11548
11549 /* Use flag from executable but make it a relocatable object. */
11550 flags = bfd_get_file_flags (abfd);
11551 flags &= ~HAS_RELOC;
11552 if (!bfd_set_start_address (implib_bfd, 0)
11553 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11554 return FALSE;
11555
11556 /* Copy architecture of output file to import library file. */
11557 arch = bfd_get_arch (abfd);
11558 mach = bfd_get_mach (abfd);
11559 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11560 && (abfd->target_defaulted
11561 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11562 return FALSE;
11563
11564 /* Get symbol table size. */
11565 symsize = bfd_get_symtab_upper_bound (abfd);
11566 if (symsize < 0)
11567 return FALSE;
11568
11569 /* Read in the symbol table. */
11570 sympp = (asymbol **) xmalloc (symsize);
11571 symcount = bfd_canonicalize_symtab (abfd, sympp);
11572 if (symcount < 0)
11573 goto free_sym_buf;
11574
11575 /* Allow the BFD backend to copy any private header data it
11576 understands from the output BFD to the import library BFD. */
11577 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11578 goto free_sym_buf;
11579
11580 /* Filter symbols to appear in the import library. */
11581 if (bed->elf_backend_filter_implib_symbols)
11582 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11583 symcount);
11584 else
11585 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11586 if (symcount == 0)
11587 {
11588 bfd_set_error (bfd_error_no_symbols);
11589 _bfd_error_handler (_("%pB: no symbol found for import library"),
11590 implib_bfd);
11591 goto free_sym_buf;
11592 }
11593
11594
11595 /* Make symbols absolute. */
11596 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11597 sizeof (*osymbuf));
11598 for (src_count = 0; src_count < symcount; src_count++)
11599 {
11600 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11601 sizeof (*osymbuf));
11602 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11603 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11604 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11605 osymbuf[src_count].internal_elf_sym.st_value =
11606 osymbuf[src_count].symbol.value;
11607 sympp[src_count] = &osymbuf[src_count].symbol;
11608 }
11609
11610 bfd_set_symtab (implib_bfd, sympp, symcount);
11611
11612 /* Allow the BFD backend to copy any private data it understands
11613 from the output BFD to the import library BFD. This is done last
11614 to permit the routine to look at the filtered symbol table. */
11615 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11616 goto free_sym_buf;
11617
11618 if (!bfd_close (implib_bfd))
11619 goto free_sym_buf;
11620
11621 ret = TRUE;
11622
11623 free_sym_buf:
11624 free (sympp);
11625 return ret;
11626 }
11627
11628 static void
11629 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11630 {
11631 asection *o;
11632
11633 if (flinfo->symstrtab != NULL)
11634 _bfd_elf_strtab_free (flinfo->symstrtab);
11635 if (flinfo->contents != NULL)
11636 free (flinfo->contents);
11637 if (flinfo->external_relocs != NULL)
11638 free (flinfo->external_relocs);
11639 if (flinfo->internal_relocs != NULL)
11640 free (flinfo->internal_relocs);
11641 if (flinfo->external_syms != NULL)
11642 free (flinfo->external_syms);
11643 if (flinfo->locsym_shndx != NULL)
11644 free (flinfo->locsym_shndx);
11645 if (flinfo->internal_syms != NULL)
11646 free (flinfo->internal_syms);
11647 if (flinfo->indices != NULL)
11648 free (flinfo->indices);
11649 if (flinfo->sections != NULL)
11650 free (flinfo->sections);
11651 if (flinfo->symshndxbuf != NULL)
11652 free (flinfo->symshndxbuf);
11653 for (o = obfd->sections; o != NULL; o = o->next)
11654 {
11655 struct bfd_elf_section_data *esdo = elf_section_data (o);
11656 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11657 free (esdo->rel.hashes);
11658 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11659 free (esdo->rela.hashes);
11660 }
11661 }
11662
11663 /* Do the final step of an ELF link. */
11664
11665 bfd_boolean
11666 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11667 {
11668 bfd_boolean dynamic;
11669 bfd_boolean emit_relocs;
11670 bfd *dynobj;
11671 struct elf_final_link_info flinfo;
11672 asection *o;
11673 struct bfd_link_order *p;
11674 bfd *sub;
11675 bfd_size_type max_contents_size;
11676 bfd_size_type max_external_reloc_size;
11677 bfd_size_type max_internal_reloc_count;
11678 bfd_size_type max_sym_count;
11679 bfd_size_type max_sym_shndx_count;
11680 Elf_Internal_Sym elfsym;
11681 unsigned int i;
11682 Elf_Internal_Shdr *symtab_hdr;
11683 Elf_Internal_Shdr *symtab_shndx_hdr;
11684 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11685 struct elf_outext_info eoinfo;
11686 bfd_boolean merged;
11687 size_t relativecount = 0;
11688 asection *reldyn = 0;
11689 bfd_size_type amt;
11690 asection *attr_section = NULL;
11691 bfd_vma attr_size = 0;
11692 const char *std_attrs_section;
11693 struct elf_link_hash_table *htab = elf_hash_table (info);
11694
11695 if (!is_elf_hash_table (htab))
11696 return FALSE;
11697
11698 if (bfd_link_pic (info))
11699 abfd->flags |= DYNAMIC;
11700
11701 dynamic = htab->dynamic_sections_created;
11702 dynobj = htab->dynobj;
11703
11704 emit_relocs = (bfd_link_relocatable (info)
11705 || info->emitrelocations);
11706
11707 flinfo.info = info;
11708 flinfo.output_bfd = abfd;
11709 flinfo.symstrtab = _bfd_elf_strtab_init ();
11710 if (flinfo.symstrtab == NULL)
11711 return FALSE;
11712
11713 if (! dynamic)
11714 {
11715 flinfo.hash_sec = NULL;
11716 flinfo.symver_sec = NULL;
11717 }
11718 else
11719 {
11720 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11721 /* Note that dynsym_sec can be NULL (on VMS). */
11722 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11723 /* Note that it is OK if symver_sec is NULL. */
11724 }
11725
11726 flinfo.contents = NULL;
11727 flinfo.external_relocs = NULL;
11728 flinfo.internal_relocs = NULL;
11729 flinfo.external_syms = NULL;
11730 flinfo.locsym_shndx = NULL;
11731 flinfo.internal_syms = NULL;
11732 flinfo.indices = NULL;
11733 flinfo.sections = NULL;
11734 flinfo.symshndxbuf = NULL;
11735 flinfo.filesym_count = 0;
11736
11737 /* The object attributes have been merged. Remove the input
11738 sections from the link, and set the contents of the output
11739 secton. */
11740 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11741 for (o = abfd->sections; o != NULL; o = o->next)
11742 {
11743 bfd_boolean remove_section = FALSE;
11744
11745 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11746 || strcmp (o->name, ".gnu.attributes") == 0)
11747 {
11748 for (p = o->map_head.link_order; p != NULL; p = p->next)
11749 {
11750 asection *input_section;
11751
11752 if (p->type != bfd_indirect_link_order)
11753 continue;
11754 input_section = p->u.indirect.section;
11755 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11756 elf_link_input_bfd ignores this section. */
11757 input_section->flags &= ~SEC_HAS_CONTENTS;
11758 }
11759
11760 attr_size = bfd_elf_obj_attr_size (abfd);
11761 bfd_set_section_size (abfd, o, attr_size);
11762 /* Skip this section later on. */
11763 o->map_head.link_order = NULL;
11764 if (attr_size)
11765 attr_section = o;
11766 else
11767 remove_section = TRUE;
11768 }
11769 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11770 {
11771 /* Remove empty group section from linker output. */
11772 remove_section = TRUE;
11773 }
11774 if (remove_section)
11775 {
11776 o->flags |= SEC_EXCLUDE;
11777 bfd_section_list_remove (abfd, o);
11778 abfd->section_count--;
11779 }
11780 }
11781
11782 /* Count up the number of relocations we will output for each output
11783 section, so that we know the sizes of the reloc sections. We
11784 also figure out some maximum sizes. */
11785 max_contents_size = 0;
11786 max_external_reloc_size = 0;
11787 max_internal_reloc_count = 0;
11788 max_sym_count = 0;
11789 max_sym_shndx_count = 0;
11790 merged = FALSE;
11791 for (o = abfd->sections; o != NULL; o = o->next)
11792 {
11793 struct bfd_elf_section_data *esdo = elf_section_data (o);
11794 o->reloc_count = 0;
11795
11796 for (p = o->map_head.link_order; p != NULL; p = p->next)
11797 {
11798 unsigned int reloc_count = 0;
11799 unsigned int additional_reloc_count = 0;
11800 struct bfd_elf_section_data *esdi = NULL;
11801
11802 if (p->type == bfd_section_reloc_link_order
11803 || p->type == bfd_symbol_reloc_link_order)
11804 reloc_count = 1;
11805 else if (p->type == bfd_indirect_link_order)
11806 {
11807 asection *sec;
11808
11809 sec = p->u.indirect.section;
11810
11811 /* Mark all sections which are to be included in the
11812 link. This will normally be every section. We need
11813 to do this so that we can identify any sections which
11814 the linker has decided to not include. */
11815 sec->linker_mark = TRUE;
11816
11817 if (sec->flags & SEC_MERGE)
11818 merged = TRUE;
11819
11820 if (sec->rawsize > max_contents_size)
11821 max_contents_size = sec->rawsize;
11822 if (sec->size > max_contents_size)
11823 max_contents_size = sec->size;
11824
11825 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11826 && (sec->owner->flags & DYNAMIC) == 0)
11827 {
11828 size_t sym_count;
11829
11830 /* We are interested in just local symbols, not all
11831 symbols. */
11832 if (elf_bad_symtab (sec->owner))
11833 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11834 / bed->s->sizeof_sym);
11835 else
11836 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11837
11838 if (sym_count > max_sym_count)
11839 max_sym_count = sym_count;
11840
11841 if (sym_count > max_sym_shndx_count
11842 && elf_symtab_shndx_list (sec->owner) != NULL)
11843 max_sym_shndx_count = sym_count;
11844
11845 if (esdo->this_hdr.sh_type == SHT_REL
11846 || esdo->this_hdr.sh_type == SHT_RELA)
11847 /* Some backends use reloc_count in relocation sections
11848 to count particular types of relocs. Of course,
11849 reloc sections themselves can't have relocations. */
11850 ;
11851 else if (emit_relocs)
11852 {
11853 reloc_count = sec->reloc_count;
11854 if (bed->elf_backend_count_additional_relocs)
11855 {
11856 int c;
11857 c = (*bed->elf_backend_count_additional_relocs) (sec);
11858 additional_reloc_count += c;
11859 }
11860 }
11861 else if (bed->elf_backend_count_relocs)
11862 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11863
11864 esdi = elf_section_data (sec);
11865
11866 if ((sec->flags & SEC_RELOC) != 0)
11867 {
11868 size_t ext_size = 0;
11869
11870 if (esdi->rel.hdr != NULL)
11871 ext_size = esdi->rel.hdr->sh_size;
11872 if (esdi->rela.hdr != NULL)
11873 ext_size += esdi->rela.hdr->sh_size;
11874
11875 if (ext_size > max_external_reloc_size)
11876 max_external_reloc_size = ext_size;
11877 if (sec->reloc_count > max_internal_reloc_count)
11878 max_internal_reloc_count = sec->reloc_count;
11879 }
11880 }
11881 }
11882
11883 if (reloc_count == 0)
11884 continue;
11885
11886 reloc_count += additional_reloc_count;
11887 o->reloc_count += reloc_count;
11888
11889 if (p->type == bfd_indirect_link_order && emit_relocs)
11890 {
11891 if (esdi->rel.hdr)
11892 {
11893 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11894 esdo->rel.count += additional_reloc_count;
11895 }
11896 if (esdi->rela.hdr)
11897 {
11898 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11899 esdo->rela.count += additional_reloc_count;
11900 }
11901 }
11902 else
11903 {
11904 if (o->use_rela_p)
11905 esdo->rela.count += reloc_count;
11906 else
11907 esdo->rel.count += reloc_count;
11908 }
11909 }
11910
11911 if (o->reloc_count > 0)
11912 o->flags |= SEC_RELOC;
11913 else
11914 {
11915 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11916 set it (this is probably a bug) and if it is set
11917 assign_section_numbers will create a reloc section. */
11918 o->flags &=~ SEC_RELOC;
11919 }
11920
11921 /* If the SEC_ALLOC flag is not set, force the section VMA to
11922 zero. This is done in elf_fake_sections as well, but forcing
11923 the VMA to 0 here will ensure that relocs against these
11924 sections are handled correctly. */
11925 if ((o->flags & SEC_ALLOC) == 0
11926 && ! o->user_set_vma)
11927 o->vma = 0;
11928 }
11929
11930 if (! bfd_link_relocatable (info) && merged)
11931 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11932
11933 /* Figure out the file positions for everything but the symbol table
11934 and the relocs. We set symcount to force assign_section_numbers
11935 to create a symbol table. */
11936 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11937 BFD_ASSERT (! abfd->output_has_begun);
11938 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11939 goto error_return;
11940
11941 /* Set sizes, and assign file positions for reloc sections. */
11942 for (o = abfd->sections; o != NULL; o = o->next)
11943 {
11944 struct bfd_elf_section_data *esdo = elf_section_data (o);
11945 if ((o->flags & SEC_RELOC) != 0)
11946 {
11947 if (esdo->rel.hdr
11948 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11949 goto error_return;
11950
11951 if (esdo->rela.hdr
11952 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11953 goto error_return;
11954 }
11955
11956 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11957 to count upwards while actually outputting the relocations. */
11958 esdo->rel.count = 0;
11959 esdo->rela.count = 0;
11960
11961 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11962 {
11963 /* Cache the section contents so that they can be compressed
11964 later. Use bfd_malloc since it will be freed by
11965 bfd_compress_section_contents. */
11966 unsigned char *contents = esdo->this_hdr.contents;
11967 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11968 abort ();
11969 contents
11970 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11971 if (contents == NULL)
11972 goto error_return;
11973 esdo->this_hdr.contents = contents;
11974 }
11975 }
11976
11977 /* We have now assigned file positions for all the sections except
11978 .symtab, .strtab, and non-loaded reloc sections. We start the
11979 .symtab section at the current file position, and write directly
11980 to it. We build the .strtab section in memory. */
11981 bfd_get_symcount (abfd) = 0;
11982 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11983 /* sh_name is set in prep_headers. */
11984 symtab_hdr->sh_type = SHT_SYMTAB;
11985 /* sh_flags, sh_addr and sh_size all start off zero. */
11986 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11987 /* sh_link is set in assign_section_numbers. */
11988 /* sh_info is set below. */
11989 /* sh_offset is set just below. */
11990 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11991
11992 if (max_sym_count < 20)
11993 max_sym_count = 20;
11994 htab->strtabsize = max_sym_count;
11995 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11996 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11997 if (htab->strtab == NULL)
11998 goto error_return;
11999 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12000 flinfo.symshndxbuf
12001 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12002 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12003
12004 if (info->strip != strip_all || emit_relocs)
12005 {
12006 file_ptr off = elf_next_file_pos (abfd);
12007
12008 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12009
12010 /* Note that at this point elf_next_file_pos (abfd) is
12011 incorrect. We do not yet know the size of the .symtab section.
12012 We correct next_file_pos below, after we do know the size. */
12013
12014 /* Start writing out the symbol table. The first symbol is always a
12015 dummy symbol. */
12016 elfsym.st_value = 0;
12017 elfsym.st_size = 0;
12018 elfsym.st_info = 0;
12019 elfsym.st_other = 0;
12020 elfsym.st_shndx = SHN_UNDEF;
12021 elfsym.st_target_internal = 0;
12022 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12023 bfd_und_section_ptr, NULL) != 1)
12024 goto error_return;
12025
12026 /* Output a symbol for each section. We output these even if we are
12027 discarding local symbols, since they are used for relocs. These
12028 symbols have no names. We store the index of each one in the
12029 index field of the section, so that we can find it again when
12030 outputting relocs. */
12031
12032 elfsym.st_size = 0;
12033 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12034 elfsym.st_other = 0;
12035 elfsym.st_value = 0;
12036 elfsym.st_target_internal = 0;
12037 for (i = 1; i < elf_numsections (abfd); i++)
12038 {
12039 o = bfd_section_from_elf_index (abfd, i);
12040 if (o != NULL)
12041 {
12042 o->target_index = bfd_get_symcount (abfd);
12043 elfsym.st_shndx = i;
12044 if (!bfd_link_relocatable (info))
12045 elfsym.st_value = o->vma;
12046 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12047 NULL) != 1)
12048 goto error_return;
12049 }
12050 }
12051 }
12052
12053 /* Allocate some memory to hold information read in from the input
12054 files. */
12055 if (max_contents_size != 0)
12056 {
12057 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12058 if (flinfo.contents == NULL)
12059 goto error_return;
12060 }
12061
12062 if (max_external_reloc_size != 0)
12063 {
12064 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12065 if (flinfo.external_relocs == NULL)
12066 goto error_return;
12067 }
12068
12069 if (max_internal_reloc_count != 0)
12070 {
12071 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12072 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12073 if (flinfo.internal_relocs == NULL)
12074 goto error_return;
12075 }
12076
12077 if (max_sym_count != 0)
12078 {
12079 amt = max_sym_count * bed->s->sizeof_sym;
12080 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12081 if (flinfo.external_syms == NULL)
12082 goto error_return;
12083
12084 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12085 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12086 if (flinfo.internal_syms == NULL)
12087 goto error_return;
12088
12089 amt = max_sym_count * sizeof (long);
12090 flinfo.indices = (long int *) bfd_malloc (amt);
12091 if (flinfo.indices == NULL)
12092 goto error_return;
12093
12094 amt = max_sym_count * sizeof (asection *);
12095 flinfo.sections = (asection **) bfd_malloc (amt);
12096 if (flinfo.sections == NULL)
12097 goto error_return;
12098 }
12099
12100 if (max_sym_shndx_count != 0)
12101 {
12102 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12103 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12104 if (flinfo.locsym_shndx == NULL)
12105 goto error_return;
12106 }
12107
12108 if (htab->tls_sec)
12109 {
12110 bfd_vma base, end = 0;
12111 asection *sec;
12112
12113 for (sec = htab->tls_sec;
12114 sec && (sec->flags & SEC_THREAD_LOCAL);
12115 sec = sec->next)
12116 {
12117 bfd_size_type size = sec->size;
12118
12119 if (size == 0
12120 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12121 {
12122 struct bfd_link_order *ord = sec->map_tail.link_order;
12123
12124 if (ord != NULL)
12125 size = ord->offset + ord->size;
12126 }
12127 end = sec->vma + size;
12128 }
12129 base = htab->tls_sec->vma;
12130 /* Only align end of TLS section if static TLS doesn't have special
12131 alignment requirements. */
12132 if (bed->static_tls_alignment == 1)
12133 end = align_power (end, htab->tls_sec->alignment_power);
12134 htab->tls_size = end - base;
12135 }
12136
12137 /* Reorder SHF_LINK_ORDER sections. */
12138 for (o = abfd->sections; o != NULL; o = o->next)
12139 {
12140 if (!elf_fixup_link_order (abfd, o))
12141 return FALSE;
12142 }
12143
12144 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12145 return FALSE;
12146
12147 /* Since ELF permits relocations to be against local symbols, we
12148 must have the local symbols available when we do the relocations.
12149 Since we would rather only read the local symbols once, and we
12150 would rather not keep them in memory, we handle all the
12151 relocations for a single input file at the same time.
12152
12153 Unfortunately, there is no way to know the total number of local
12154 symbols until we have seen all of them, and the local symbol
12155 indices precede the global symbol indices. This means that when
12156 we are generating relocatable output, and we see a reloc against
12157 a global symbol, we can not know the symbol index until we have
12158 finished examining all the local symbols to see which ones we are
12159 going to output. To deal with this, we keep the relocations in
12160 memory, and don't output them until the end of the link. This is
12161 an unfortunate waste of memory, but I don't see a good way around
12162 it. Fortunately, it only happens when performing a relocatable
12163 link, which is not the common case. FIXME: If keep_memory is set
12164 we could write the relocs out and then read them again; I don't
12165 know how bad the memory loss will be. */
12166
12167 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12168 sub->output_has_begun = FALSE;
12169 for (o = abfd->sections; o != NULL; o = o->next)
12170 {
12171 for (p = o->map_head.link_order; p != NULL; p = p->next)
12172 {
12173 if (p->type == bfd_indirect_link_order
12174 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12175 == bfd_target_elf_flavour)
12176 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12177 {
12178 if (! sub->output_has_begun)
12179 {
12180 if (! elf_link_input_bfd (&flinfo, sub))
12181 goto error_return;
12182 sub->output_has_begun = TRUE;
12183 }
12184 }
12185 else if (p->type == bfd_section_reloc_link_order
12186 || p->type == bfd_symbol_reloc_link_order)
12187 {
12188 if (! elf_reloc_link_order (abfd, info, o, p))
12189 goto error_return;
12190 }
12191 else
12192 {
12193 if (! _bfd_default_link_order (abfd, info, o, p))
12194 {
12195 if (p->type == bfd_indirect_link_order
12196 && (bfd_get_flavour (sub)
12197 == bfd_target_elf_flavour)
12198 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12199 != bed->s->elfclass))
12200 {
12201 const char *iclass, *oclass;
12202
12203 switch (bed->s->elfclass)
12204 {
12205 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12206 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12207 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12208 default: abort ();
12209 }
12210
12211 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12212 {
12213 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12214 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12215 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12216 default: abort ();
12217 }
12218
12219 bfd_set_error (bfd_error_wrong_format);
12220 _bfd_error_handler
12221 /* xgettext:c-format */
12222 (_("%pB: file class %s incompatible with %s"),
12223 sub, iclass, oclass);
12224 }
12225
12226 goto error_return;
12227 }
12228 }
12229 }
12230 }
12231
12232 /* Free symbol buffer if needed. */
12233 if (!info->reduce_memory_overheads)
12234 {
12235 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12236 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12237 && elf_tdata (sub)->symbuf)
12238 {
12239 free (elf_tdata (sub)->symbuf);
12240 elf_tdata (sub)->symbuf = NULL;
12241 }
12242 }
12243
12244 /* Output any global symbols that got converted to local in a
12245 version script or due to symbol visibility. We do this in a
12246 separate step since ELF requires all local symbols to appear
12247 prior to any global symbols. FIXME: We should only do this if
12248 some global symbols were, in fact, converted to become local.
12249 FIXME: Will this work correctly with the Irix 5 linker? */
12250 eoinfo.failed = FALSE;
12251 eoinfo.flinfo = &flinfo;
12252 eoinfo.localsyms = TRUE;
12253 eoinfo.file_sym_done = FALSE;
12254 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12255 if (eoinfo.failed)
12256 return FALSE;
12257
12258 /* If backend needs to output some local symbols not present in the hash
12259 table, do it now. */
12260 if (bed->elf_backend_output_arch_local_syms
12261 && (info->strip != strip_all || emit_relocs))
12262 {
12263 typedef int (*out_sym_func)
12264 (void *, const char *, Elf_Internal_Sym *, asection *,
12265 struct elf_link_hash_entry *);
12266
12267 if (! ((*bed->elf_backend_output_arch_local_syms)
12268 (abfd, info, &flinfo,
12269 (out_sym_func) elf_link_output_symstrtab)))
12270 return FALSE;
12271 }
12272
12273 /* That wrote out all the local symbols. Finish up the symbol table
12274 with the global symbols. Even if we want to strip everything we
12275 can, we still need to deal with those global symbols that got
12276 converted to local in a version script. */
12277
12278 /* The sh_info field records the index of the first non local symbol. */
12279 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12280
12281 if (dynamic
12282 && htab->dynsym != NULL
12283 && htab->dynsym->output_section != bfd_abs_section_ptr)
12284 {
12285 Elf_Internal_Sym sym;
12286 bfd_byte *dynsym = htab->dynsym->contents;
12287
12288 o = htab->dynsym->output_section;
12289 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12290
12291 /* Write out the section symbols for the output sections. */
12292 if (bfd_link_pic (info)
12293 || htab->is_relocatable_executable)
12294 {
12295 asection *s;
12296
12297 sym.st_size = 0;
12298 sym.st_name = 0;
12299 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12300 sym.st_other = 0;
12301 sym.st_target_internal = 0;
12302
12303 for (s = abfd->sections; s != NULL; s = s->next)
12304 {
12305 int indx;
12306 bfd_byte *dest;
12307 long dynindx;
12308
12309 dynindx = elf_section_data (s)->dynindx;
12310 if (dynindx <= 0)
12311 continue;
12312 indx = elf_section_data (s)->this_idx;
12313 BFD_ASSERT (indx > 0);
12314 sym.st_shndx = indx;
12315 if (! check_dynsym (abfd, &sym))
12316 return FALSE;
12317 sym.st_value = s->vma;
12318 dest = dynsym + dynindx * bed->s->sizeof_sym;
12319 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12320 }
12321 }
12322
12323 /* Write out the local dynsyms. */
12324 if (htab->dynlocal)
12325 {
12326 struct elf_link_local_dynamic_entry *e;
12327 for (e = htab->dynlocal; e ; e = e->next)
12328 {
12329 asection *s;
12330 bfd_byte *dest;
12331
12332 /* Copy the internal symbol and turn off visibility.
12333 Note that we saved a word of storage and overwrote
12334 the original st_name with the dynstr_index. */
12335 sym = e->isym;
12336 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12337
12338 s = bfd_section_from_elf_index (e->input_bfd,
12339 e->isym.st_shndx);
12340 if (s != NULL)
12341 {
12342 sym.st_shndx =
12343 elf_section_data (s->output_section)->this_idx;
12344 if (! check_dynsym (abfd, &sym))
12345 return FALSE;
12346 sym.st_value = (s->output_section->vma
12347 + s->output_offset
12348 + e->isym.st_value);
12349 }
12350
12351 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12352 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12353 }
12354 }
12355 }
12356
12357 /* We get the global symbols from the hash table. */
12358 eoinfo.failed = FALSE;
12359 eoinfo.localsyms = FALSE;
12360 eoinfo.flinfo = &flinfo;
12361 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12362 if (eoinfo.failed)
12363 return FALSE;
12364
12365 /* If backend needs to output some symbols not present in the hash
12366 table, do it now. */
12367 if (bed->elf_backend_output_arch_syms
12368 && (info->strip != strip_all || emit_relocs))
12369 {
12370 typedef int (*out_sym_func)
12371 (void *, const char *, Elf_Internal_Sym *, asection *,
12372 struct elf_link_hash_entry *);
12373
12374 if (! ((*bed->elf_backend_output_arch_syms)
12375 (abfd, info, &flinfo,
12376 (out_sym_func) elf_link_output_symstrtab)))
12377 return FALSE;
12378 }
12379
12380 /* Finalize the .strtab section. */
12381 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12382
12383 /* Swap out the .strtab section. */
12384 if (!elf_link_swap_symbols_out (&flinfo))
12385 return FALSE;
12386
12387 /* Now we know the size of the symtab section. */
12388 if (bfd_get_symcount (abfd) > 0)
12389 {
12390 /* Finish up and write out the symbol string table (.strtab)
12391 section. */
12392 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12393 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12394
12395 if (elf_symtab_shndx_list (abfd))
12396 {
12397 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12398
12399 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12400 {
12401 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12402 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12403 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12404 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12405 symtab_shndx_hdr->sh_size = amt;
12406
12407 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12408 off, TRUE);
12409
12410 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12411 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12412 return FALSE;
12413 }
12414 }
12415
12416 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12417 /* sh_name was set in prep_headers. */
12418 symstrtab_hdr->sh_type = SHT_STRTAB;
12419 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12420 symstrtab_hdr->sh_addr = 0;
12421 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12422 symstrtab_hdr->sh_entsize = 0;
12423 symstrtab_hdr->sh_link = 0;
12424 symstrtab_hdr->sh_info = 0;
12425 /* sh_offset is set just below. */
12426 symstrtab_hdr->sh_addralign = 1;
12427
12428 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12429 off, TRUE);
12430 elf_next_file_pos (abfd) = off;
12431
12432 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12433 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12434 return FALSE;
12435 }
12436
12437 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12438 {
12439 _bfd_error_handler (_("%pB: failed to generate import library"),
12440 info->out_implib_bfd);
12441 return FALSE;
12442 }
12443
12444 /* Adjust the relocs to have the correct symbol indices. */
12445 for (o = abfd->sections; o != NULL; o = o->next)
12446 {
12447 struct bfd_elf_section_data *esdo = elf_section_data (o);
12448 bfd_boolean sort;
12449
12450 if ((o->flags & SEC_RELOC) == 0)
12451 continue;
12452
12453 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12454 if (esdo->rel.hdr != NULL
12455 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12456 return FALSE;
12457 if (esdo->rela.hdr != NULL
12458 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12459 return FALSE;
12460
12461 /* Set the reloc_count field to 0 to prevent write_relocs from
12462 trying to swap the relocs out itself. */
12463 o->reloc_count = 0;
12464 }
12465
12466 if (dynamic && info->combreloc && dynobj != NULL)
12467 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12468
12469 /* If we are linking against a dynamic object, or generating a
12470 shared library, finish up the dynamic linking information. */
12471 if (dynamic)
12472 {
12473 bfd_byte *dyncon, *dynconend;
12474
12475 /* Fix up .dynamic entries. */
12476 o = bfd_get_linker_section (dynobj, ".dynamic");
12477 BFD_ASSERT (o != NULL);
12478
12479 dyncon = o->contents;
12480 dynconend = o->contents + o->size;
12481 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12482 {
12483 Elf_Internal_Dyn dyn;
12484 const char *name;
12485 unsigned int type;
12486 bfd_size_type sh_size;
12487 bfd_vma sh_addr;
12488
12489 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12490
12491 switch (dyn.d_tag)
12492 {
12493 default:
12494 continue;
12495 case DT_NULL:
12496 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12497 {
12498 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12499 {
12500 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12501 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12502 default: continue;
12503 }
12504 dyn.d_un.d_val = relativecount;
12505 relativecount = 0;
12506 break;
12507 }
12508 continue;
12509
12510 case DT_INIT:
12511 name = info->init_function;
12512 goto get_sym;
12513 case DT_FINI:
12514 name = info->fini_function;
12515 get_sym:
12516 {
12517 struct elf_link_hash_entry *h;
12518
12519 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12520 if (h != NULL
12521 && (h->root.type == bfd_link_hash_defined
12522 || h->root.type == bfd_link_hash_defweak))
12523 {
12524 dyn.d_un.d_ptr = h->root.u.def.value;
12525 o = h->root.u.def.section;
12526 if (o->output_section != NULL)
12527 dyn.d_un.d_ptr += (o->output_section->vma
12528 + o->output_offset);
12529 else
12530 {
12531 /* The symbol is imported from another shared
12532 library and does not apply to this one. */
12533 dyn.d_un.d_ptr = 0;
12534 }
12535 break;
12536 }
12537 }
12538 continue;
12539
12540 case DT_PREINIT_ARRAYSZ:
12541 name = ".preinit_array";
12542 goto get_out_size;
12543 case DT_INIT_ARRAYSZ:
12544 name = ".init_array";
12545 goto get_out_size;
12546 case DT_FINI_ARRAYSZ:
12547 name = ".fini_array";
12548 get_out_size:
12549 o = bfd_get_section_by_name (abfd, name);
12550 if (o == NULL)
12551 {
12552 _bfd_error_handler
12553 (_("could not find section %s"), name);
12554 goto error_return;
12555 }
12556 if (o->size == 0)
12557 _bfd_error_handler
12558 (_("warning: %s section has zero size"), name);
12559 dyn.d_un.d_val = o->size;
12560 break;
12561
12562 case DT_PREINIT_ARRAY:
12563 name = ".preinit_array";
12564 goto get_out_vma;
12565 case DT_INIT_ARRAY:
12566 name = ".init_array";
12567 goto get_out_vma;
12568 case DT_FINI_ARRAY:
12569 name = ".fini_array";
12570 get_out_vma:
12571 o = bfd_get_section_by_name (abfd, name);
12572 goto do_vma;
12573
12574 case DT_HASH:
12575 name = ".hash";
12576 goto get_vma;
12577 case DT_GNU_HASH:
12578 name = ".gnu.hash";
12579 goto get_vma;
12580 case DT_STRTAB:
12581 name = ".dynstr";
12582 goto get_vma;
12583 case DT_SYMTAB:
12584 name = ".dynsym";
12585 goto get_vma;
12586 case DT_VERDEF:
12587 name = ".gnu.version_d";
12588 goto get_vma;
12589 case DT_VERNEED:
12590 name = ".gnu.version_r";
12591 goto get_vma;
12592 case DT_VERSYM:
12593 name = ".gnu.version";
12594 get_vma:
12595 o = bfd_get_linker_section (dynobj, name);
12596 do_vma:
12597 if (o == NULL || bfd_is_abs_section (o->output_section))
12598 {
12599 _bfd_error_handler
12600 (_("could not find section %s"), name);
12601 goto error_return;
12602 }
12603 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12604 {
12605 _bfd_error_handler
12606 (_("warning: section '%s' is being made into a note"), name);
12607 bfd_set_error (bfd_error_nonrepresentable_section);
12608 goto error_return;
12609 }
12610 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12611 break;
12612
12613 case DT_REL:
12614 case DT_RELA:
12615 case DT_RELSZ:
12616 case DT_RELASZ:
12617 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12618 type = SHT_REL;
12619 else
12620 type = SHT_RELA;
12621 sh_size = 0;
12622 sh_addr = 0;
12623 for (i = 1; i < elf_numsections (abfd); i++)
12624 {
12625 Elf_Internal_Shdr *hdr;
12626
12627 hdr = elf_elfsections (abfd)[i];
12628 if (hdr->sh_type == type
12629 && (hdr->sh_flags & SHF_ALLOC) != 0)
12630 {
12631 sh_size += hdr->sh_size;
12632 if (sh_addr == 0
12633 || sh_addr > hdr->sh_addr)
12634 sh_addr = hdr->sh_addr;
12635 }
12636 }
12637
12638 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12639 {
12640 /* Don't count procedure linkage table relocs in the
12641 overall reloc count. */
12642 sh_size -= htab->srelplt->size;
12643 if (sh_size == 0)
12644 /* If the size is zero, make the address zero too.
12645 This is to avoid a glibc bug. If the backend
12646 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12647 zero, then we'll put DT_RELA at the end of
12648 DT_JMPREL. glibc will interpret the end of
12649 DT_RELA matching the end of DT_JMPREL as the
12650 case where DT_RELA includes DT_JMPREL, and for
12651 LD_BIND_NOW will decide that processing DT_RELA
12652 will process the PLT relocs too. Net result:
12653 No PLT relocs applied. */
12654 sh_addr = 0;
12655
12656 /* If .rela.plt is the first .rela section, exclude
12657 it from DT_RELA. */
12658 else if (sh_addr == (htab->srelplt->output_section->vma
12659 + htab->srelplt->output_offset))
12660 sh_addr += htab->srelplt->size;
12661 }
12662
12663 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12664 dyn.d_un.d_val = sh_size;
12665 else
12666 dyn.d_un.d_ptr = sh_addr;
12667 break;
12668 }
12669 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12670 }
12671 }
12672
12673 /* If we have created any dynamic sections, then output them. */
12674 if (dynobj != NULL)
12675 {
12676 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12677 goto error_return;
12678
12679 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12680 if (((info->warn_shared_textrel && bfd_link_pic (info))
12681 || info->error_textrel)
12682 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12683 {
12684 bfd_byte *dyncon, *dynconend;
12685
12686 dyncon = o->contents;
12687 dynconend = o->contents + o->size;
12688 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12689 {
12690 Elf_Internal_Dyn dyn;
12691
12692 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12693
12694 if (dyn.d_tag == DT_TEXTREL)
12695 {
12696 if (info->error_textrel)
12697 info->callbacks->einfo
12698 (_("%P%X: read-only segment has dynamic relocations\n"));
12699 else
12700 info->callbacks->einfo
12701 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12702 break;
12703 }
12704 }
12705 }
12706
12707 for (o = dynobj->sections; o != NULL; o = o->next)
12708 {
12709 if ((o->flags & SEC_HAS_CONTENTS) == 0
12710 || o->size == 0
12711 || o->output_section == bfd_abs_section_ptr)
12712 continue;
12713 if ((o->flags & SEC_LINKER_CREATED) == 0)
12714 {
12715 /* At this point, we are only interested in sections
12716 created by _bfd_elf_link_create_dynamic_sections. */
12717 continue;
12718 }
12719 if (htab->stab_info.stabstr == o)
12720 continue;
12721 if (htab->eh_info.hdr_sec == o)
12722 continue;
12723 if (strcmp (o->name, ".dynstr") != 0)
12724 {
12725 if (! bfd_set_section_contents (abfd, o->output_section,
12726 o->contents,
12727 (file_ptr) o->output_offset
12728 * bfd_octets_per_byte (abfd),
12729 o->size))
12730 goto error_return;
12731 }
12732 else
12733 {
12734 /* The contents of the .dynstr section are actually in a
12735 stringtab. */
12736 file_ptr off;
12737
12738 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12739 if (bfd_seek (abfd, off, SEEK_SET) != 0
12740 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12741 goto error_return;
12742 }
12743 }
12744 }
12745
12746 if (!info->resolve_section_groups)
12747 {
12748 bfd_boolean failed = FALSE;
12749
12750 BFD_ASSERT (bfd_link_relocatable (info));
12751 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12752 if (failed)
12753 goto error_return;
12754 }
12755
12756 /* If we have optimized stabs strings, output them. */
12757 if (htab->stab_info.stabstr != NULL)
12758 {
12759 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12760 goto error_return;
12761 }
12762
12763 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12764 goto error_return;
12765
12766 elf_final_link_free (abfd, &flinfo);
12767
12768 elf_linker (abfd) = TRUE;
12769
12770 if (attr_section)
12771 {
12772 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12773 if (contents == NULL)
12774 return FALSE; /* Bail out and fail. */
12775 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12776 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12777 free (contents);
12778 }
12779
12780 return TRUE;
12781
12782 error_return:
12783 elf_final_link_free (abfd, &flinfo);
12784 return FALSE;
12785 }
12786
12787 /* Initialize COOKIE for input bfd ABFD. */
12789
12790 static bfd_boolean
12791 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12792 struct bfd_link_info *info, bfd *abfd)
12793 {
12794 Elf_Internal_Shdr *symtab_hdr;
12795 const struct elf_backend_data *bed;
12796
12797 bed = get_elf_backend_data (abfd);
12798 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12799
12800 cookie->abfd = abfd;
12801 cookie->sym_hashes = elf_sym_hashes (abfd);
12802 cookie->bad_symtab = elf_bad_symtab (abfd);
12803 if (cookie->bad_symtab)
12804 {
12805 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12806 cookie->extsymoff = 0;
12807 }
12808 else
12809 {
12810 cookie->locsymcount = symtab_hdr->sh_info;
12811 cookie->extsymoff = symtab_hdr->sh_info;
12812 }
12813
12814 if (bed->s->arch_size == 32)
12815 cookie->r_sym_shift = 8;
12816 else
12817 cookie->r_sym_shift = 32;
12818
12819 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12820 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12821 {
12822 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12823 cookie->locsymcount, 0,
12824 NULL, NULL, NULL);
12825 if (cookie->locsyms == NULL)
12826 {
12827 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12828 return FALSE;
12829 }
12830 if (info->keep_memory)
12831 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12832 }
12833 return TRUE;
12834 }
12835
12836 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12837
12838 static void
12839 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12840 {
12841 Elf_Internal_Shdr *symtab_hdr;
12842
12843 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12844 if (cookie->locsyms != NULL
12845 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12846 free (cookie->locsyms);
12847 }
12848
12849 /* Initialize the relocation information in COOKIE for input section SEC
12850 of input bfd ABFD. */
12851
12852 static bfd_boolean
12853 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12854 struct bfd_link_info *info, bfd *abfd,
12855 asection *sec)
12856 {
12857 if (sec->reloc_count == 0)
12858 {
12859 cookie->rels = NULL;
12860 cookie->relend = NULL;
12861 }
12862 else
12863 {
12864 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12865 info->keep_memory);
12866 if (cookie->rels == NULL)
12867 return FALSE;
12868 cookie->rel = cookie->rels;
12869 cookie->relend = cookie->rels + sec->reloc_count;
12870 }
12871 cookie->rel = cookie->rels;
12872 return TRUE;
12873 }
12874
12875 /* Free the memory allocated by init_reloc_cookie_rels,
12876 if appropriate. */
12877
12878 static void
12879 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12880 asection *sec)
12881 {
12882 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12883 free (cookie->rels);
12884 }
12885
12886 /* Initialize the whole of COOKIE for input section SEC. */
12887
12888 static bfd_boolean
12889 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12890 struct bfd_link_info *info,
12891 asection *sec)
12892 {
12893 if (!init_reloc_cookie (cookie, info, sec->owner))
12894 goto error1;
12895 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12896 goto error2;
12897 return TRUE;
12898
12899 error2:
12900 fini_reloc_cookie (cookie, sec->owner);
12901 error1:
12902 return FALSE;
12903 }
12904
12905 /* Free the memory allocated by init_reloc_cookie_for_section,
12906 if appropriate. */
12907
12908 static void
12909 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12910 asection *sec)
12911 {
12912 fini_reloc_cookie_rels (cookie, sec);
12913 fini_reloc_cookie (cookie, sec->owner);
12914 }
12915
12916 /* Garbage collect unused sections. */
12918
12919 /* Default gc_mark_hook. */
12920
12921 asection *
12922 _bfd_elf_gc_mark_hook (asection *sec,
12923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12924 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12925 struct elf_link_hash_entry *h,
12926 Elf_Internal_Sym *sym)
12927 {
12928 if (h != NULL)
12929 {
12930 switch (h->root.type)
12931 {
12932 case bfd_link_hash_defined:
12933 case bfd_link_hash_defweak:
12934 return h->root.u.def.section;
12935
12936 case bfd_link_hash_common:
12937 return h->root.u.c.p->section;
12938
12939 default:
12940 break;
12941 }
12942 }
12943 else
12944 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12945
12946 return NULL;
12947 }
12948
12949 /* Return the debug definition section. */
12950
12951 static asection *
12952 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12953 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12954 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12955 struct elf_link_hash_entry *h,
12956 Elf_Internal_Sym *sym)
12957 {
12958 if (h != NULL)
12959 {
12960 /* Return the global debug definition section. */
12961 if ((h->root.type == bfd_link_hash_defined
12962 || h->root.type == bfd_link_hash_defweak)
12963 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12964 return h->root.u.def.section;
12965 }
12966 else
12967 {
12968 /* Return the local debug definition section. */
12969 asection *isec = bfd_section_from_elf_index (sec->owner,
12970 sym->st_shndx);
12971 if ((isec->flags & SEC_DEBUGGING) != 0)
12972 return isec;
12973 }
12974
12975 return NULL;
12976 }
12977
12978 /* COOKIE->rel describes a relocation against section SEC, which is
12979 a section we've decided to keep. Return the section that contains
12980 the relocation symbol, or NULL if no section contains it. */
12981
12982 asection *
12983 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12984 elf_gc_mark_hook_fn gc_mark_hook,
12985 struct elf_reloc_cookie *cookie,
12986 bfd_boolean *start_stop)
12987 {
12988 unsigned long r_symndx;
12989 struct elf_link_hash_entry *h;
12990
12991 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12992 if (r_symndx == STN_UNDEF)
12993 return NULL;
12994
12995 if (r_symndx >= cookie->locsymcount
12996 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12997 {
12998 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12999 if (h == NULL)
13000 {
13001 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13002 sec->owner);
13003 return NULL;
13004 }
13005 while (h->root.type == bfd_link_hash_indirect
13006 || h->root.type == bfd_link_hash_warning)
13007 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13008 h->mark = 1;
13009 /* If this symbol is weak and there is a non-weak definition, we
13010 keep the non-weak definition because many backends put
13011 dynamic reloc info on the non-weak definition for code
13012 handling copy relocs. */
13013 if (h->is_weakalias)
13014 weakdef (h)->mark = 1;
13015
13016 if (start_stop != NULL)
13017 {
13018 /* To work around a glibc bug, mark XXX input sections
13019 when there is a reference to __start_XXX or __stop_XXX
13020 symbols. */
13021 if (h->start_stop)
13022 {
13023 asection *s = h->u2.start_stop_section;
13024 *start_stop = !s->gc_mark;
13025 return s;
13026 }
13027 }
13028
13029 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13030 }
13031
13032 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13033 &cookie->locsyms[r_symndx]);
13034 }
13035
13036 /* COOKIE->rel describes a relocation against section SEC, which is
13037 a section we've decided to keep. Mark the section that contains
13038 the relocation symbol. */
13039
13040 bfd_boolean
13041 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13042 asection *sec,
13043 elf_gc_mark_hook_fn gc_mark_hook,
13044 struct elf_reloc_cookie *cookie)
13045 {
13046 asection *rsec;
13047 bfd_boolean start_stop = FALSE;
13048
13049 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13050 while (rsec != NULL)
13051 {
13052 if (!rsec->gc_mark)
13053 {
13054 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13055 || (rsec->owner->flags & DYNAMIC) != 0)
13056 rsec->gc_mark = 1;
13057 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13058 return FALSE;
13059 }
13060 if (!start_stop)
13061 break;
13062 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13063 }
13064 return TRUE;
13065 }
13066
13067 /* The mark phase of garbage collection. For a given section, mark
13068 it and any sections in this section's group, and all the sections
13069 which define symbols to which it refers. */
13070
13071 bfd_boolean
13072 _bfd_elf_gc_mark (struct bfd_link_info *info,
13073 asection *sec,
13074 elf_gc_mark_hook_fn gc_mark_hook)
13075 {
13076 bfd_boolean ret;
13077 asection *group_sec, *eh_frame;
13078
13079 sec->gc_mark = 1;
13080
13081 /* Mark all the sections in the group. */
13082 group_sec = elf_section_data (sec)->next_in_group;
13083 if (group_sec && !group_sec->gc_mark)
13084 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13085 return FALSE;
13086
13087 /* Look through the section relocs. */
13088 ret = TRUE;
13089 eh_frame = elf_eh_frame_section (sec->owner);
13090 if ((sec->flags & SEC_RELOC) != 0
13091 && sec->reloc_count > 0
13092 && sec != eh_frame)
13093 {
13094 struct elf_reloc_cookie cookie;
13095
13096 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13097 ret = FALSE;
13098 else
13099 {
13100 for (; cookie.rel < cookie.relend; cookie.rel++)
13101 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13102 {
13103 ret = FALSE;
13104 break;
13105 }
13106 fini_reloc_cookie_for_section (&cookie, sec);
13107 }
13108 }
13109
13110 if (ret && eh_frame && elf_fde_list (sec))
13111 {
13112 struct elf_reloc_cookie cookie;
13113
13114 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13115 ret = FALSE;
13116 else
13117 {
13118 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13119 gc_mark_hook, &cookie))
13120 ret = FALSE;
13121 fini_reloc_cookie_for_section (&cookie, eh_frame);
13122 }
13123 }
13124
13125 eh_frame = elf_section_eh_frame_entry (sec);
13126 if (ret && eh_frame && !eh_frame->gc_mark)
13127 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13128 ret = FALSE;
13129
13130 return ret;
13131 }
13132
13133 /* Scan and mark sections in a special or debug section group. */
13134
13135 static void
13136 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13137 {
13138 /* Point to first section of section group. */
13139 asection *ssec;
13140 /* Used to iterate the section group. */
13141 asection *msec;
13142
13143 bfd_boolean is_special_grp = TRUE;
13144 bfd_boolean is_debug_grp = TRUE;
13145
13146 /* First scan to see if group contains any section other than debug
13147 and special section. */
13148 ssec = msec = elf_next_in_group (grp);
13149 do
13150 {
13151 if ((msec->flags & SEC_DEBUGGING) == 0)
13152 is_debug_grp = FALSE;
13153
13154 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13155 is_special_grp = FALSE;
13156
13157 msec = elf_next_in_group (msec);
13158 }
13159 while (msec != ssec);
13160
13161 /* If this is a pure debug section group or pure special section group,
13162 keep all sections in this group. */
13163 if (is_debug_grp || is_special_grp)
13164 {
13165 do
13166 {
13167 msec->gc_mark = 1;
13168 msec = elf_next_in_group (msec);
13169 }
13170 while (msec != ssec);
13171 }
13172 }
13173
13174 /* Keep debug and special sections. */
13175
13176 bfd_boolean
13177 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13178 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13179 {
13180 bfd *ibfd;
13181
13182 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13183 {
13184 asection *isec;
13185 bfd_boolean some_kept;
13186 bfd_boolean debug_frag_seen;
13187 bfd_boolean has_kept_debug_info;
13188
13189 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13190 continue;
13191 isec = ibfd->sections;
13192 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13193 continue;
13194
13195 /* Ensure all linker created sections are kept,
13196 see if any other section is already marked,
13197 and note if we have any fragmented debug sections. */
13198 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13199 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13200 {
13201 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13202 isec->gc_mark = 1;
13203 else if (isec->gc_mark
13204 && (isec->flags & SEC_ALLOC) != 0
13205 && elf_section_type (isec) != SHT_NOTE)
13206 some_kept = TRUE;
13207
13208 if (!debug_frag_seen
13209 && (isec->flags & SEC_DEBUGGING)
13210 && CONST_STRNEQ (isec->name, ".debug_line."))
13211 debug_frag_seen = TRUE;
13212 }
13213
13214 /* If no non-note alloc section in this file will be kept, then
13215 we can toss out the debug and special sections. */
13216 if (!some_kept)
13217 continue;
13218
13219 /* Keep debug and special sections like .comment when they are
13220 not part of a group. Also keep section groups that contain
13221 just debug sections or special sections. */
13222 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13223 {
13224 if ((isec->flags & SEC_GROUP) != 0)
13225 _bfd_elf_gc_mark_debug_special_section_group (isec);
13226 else if (((isec->flags & SEC_DEBUGGING) != 0
13227 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13228 && elf_next_in_group (isec) == NULL)
13229 isec->gc_mark = 1;
13230 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13231 has_kept_debug_info = TRUE;
13232 }
13233
13234 /* Look for CODE sections which are going to be discarded,
13235 and find and discard any fragmented debug sections which
13236 are associated with that code section. */
13237 if (debug_frag_seen)
13238 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13239 if ((isec->flags & SEC_CODE) != 0
13240 && isec->gc_mark == 0)
13241 {
13242 unsigned int ilen;
13243 asection *dsec;
13244
13245 ilen = strlen (isec->name);
13246
13247 /* Association is determined by the name of the debug
13248 section containing the name of the code section as
13249 a suffix. For example .debug_line.text.foo is a
13250 debug section associated with .text.foo. */
13251 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13252 {
13253 unsigned int dlen;
13254
13255 if (dsec->gc_mark == 0
13256 || (dsec->flags & SEC_DEBUGGING) == 0)
13257 continue;
13258
13259 dlen = strlen (dsec->name);
13260
13261 if (dlen > ilen
13262 && strncmp (dsec->name + (dlen - ilen),
13263 isec->name, ilen) == 0)
13264 dsec->gc_mark = 0;
13265 }
13266 }
13267
13268 /* Mark debug sections referenced by kept debug sections. */
13269 if (has_kept_debug_info)
13270 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13271 if (isec->gc_mark
13272 && (isec->flags & SEC_DEBUGGING) != 0)
13273 if (!_bfd_elf_gc_mark (info, isec,
13274 elf_gc_mark_debug_section))
13275 return FALSE;
13276 }
13277 return TRUE;
13278 }
13279
13280 static bfd_boolean
13281 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13282 {
13283 bfd *sub;
13284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13285
13286 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13287 {
13288 asection *o;
13289
13290 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13291 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13292 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13293 continue;
13294 o = sub->sections;
13295 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13296 continue;
13297
13298 for (o = sub->sections; o != NULL; o = o->next)
13299 {
13300 /* When any section in a section group is kept, we keep all
13301 sections in the section group. If the first member of
13302 the section group is excluded, we will also exclude the
13303 group section. */
13304 if (o->flags & SEC_GROUP)
13305 {
13306 asection *first = elf_next_in_group (o);
13307 o->gc_mark = first->gc_mark;
13308 }
13309
13310 if (o->gc_mark)
13311 continue;
13312
13313 /* Skip sweeping sections already excluded. */
13314 if (o->flags & SEC_EXCLUDE)
13315 continue;
13316
13317 /* Since this is early in the link process, it is simple
13318 to remove a section from the output. */
13319 o->flags |= SEC_EXCLUDE;
13320
13321 if (info->print_gc_sections && o->size != 0)
13322 /* xgettext:c-format */
13323 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13324 o, sub);
13325 }
13326 }
13327
13328 return TRUE;
13329 }
13330
13331 /* Propagate collected vtable information. This is called through
13332 elf_link_hash_traverse. */
13333
13334 static bfd_boolean
13335 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13336 {
13337 /* Those that are not vtables. */
13338 if (h->start_stop
13339 || h->u2.vtable == NULL
13340 || h->u2.vtable->parent == NULL)
13341 return TRUE;
13342
13343 /* Those vtables that do not have parents, we cannot merge. */
13344 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13345 return TRUE;
13346
13347 /* If we've already been done, exit. */
13348 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13349 return TRUE;
13350
13351 /* Make sure the parent's table is up to date. */
13352 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13353
13354 if (h->u2.vtable->used == NULL)
13355 {
13356 /* None of this table's entries were referenced. Re-use the
13357 parent's table. */
13358 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13359 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13360 }
13361 else
13362 {
13363 size_t n;
13364 bfd_boolean *cu, *pu;
13365
13366 /* Or the parent's entries into ours. */
13367 cu = h->u2.vtable->used;
13368 cu[-1] = TRUE;
13369 pu = h->u2.vtable->parent->u2.vtable->used;
13370 if (pu != NULL)
13371 {
13372 const struct elf_backend_data *bed;
13373 unsigned int log_file_align;
13374
13375 bed = get_elf_backend_data (h->root.u.def.section->owner);
13376 log_file_align = bed->s->log_file_align;
13377 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13378 while (n--)
13379 {
13380 if (*pu)
13381 *cu = TRUE;
13382 pu++;
13383 cu++;
13384 }
13385 }
13386 }
13387
13388 return TRUE;
13389 }
13390
13391 static bfd_boolean
13392 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13393 {
13394 asection *sec;
13395 bfd_vma hstart, hend;
13396 Elf_Internal_Rela *relstart, *relend, *rel;
13397 const struct elf_backend_data *bed;
13398 unsigned int log_file_align;
13399
13400 /* Take care of both those symbols that do not describe vtables as
13401 well as those that are not loaded. */
13402 if (h->start_stop
13403 || h->u2.vtable == NULL
13404 || h->u2.vtable->parent == NULL)
13405 return TRUE;
13406
13407 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13408 || h->root.type == bfd_link_hash_defweak);
13409
13410 sec = h->root.u.def.section;
13411 hstart = h->root.u.def.value;
13412 hend = hstart + h->size;
13413
13414 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13415 if (!relstart)
13416 return *(bfd_boolean *) okp = FALSE;
13417 bed = get_elf_backend_data (sec->owner);
13418 log_file_align = bed->s->log_file_align;
13419
13420 relend = relstart + sec->reloc_count;
13421
13422 for (rel = relstart; rel < relend; ++rel)
13423 if (rel->r_offset >= hstart && rel->r_offset < hend)
13424 {
13425 /* If the entry is in use, do nothing. */
13426 if (h->u2.vtable->used
13427 && (rel->r_offset - hstart) < h->u2.vtable->size)
13428 {
13429 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13430 if (h->u2.vtable->used[entry])
13431 continue;
13432 }
13433 /* Otherwise, kill it. */
13434 rel->r_offset = rel->r_info = rel->r_addend = 0;
13435 }
13436
13437 return TRUE;
13438 }
13439
13440 /* Mark sections containing dynamically referenced symbols. When
13441 building shared libraries, we must assume that any visible symbol is
13442 referenced. */
13443
13444 bfd_boolean
13445 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13446 {
13447 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13448 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13449
13450 if ((h->root.type == bfd_link_hash_defined
13451 || h->root.type == bfd_link_hash_defweak)
13452 && ((h->ref_dynamic && !h->forced_local)
13453 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13454 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13455 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13456 && (!bfd_link_executable (info)
13457 || info->gc_keep_exported
13458 || info->export_dynamic
13459 || (h->dynamic
13460 && d != NULL
13461 && (*d->match) (&d->head, NULL, h->root.root.string)))
13462 && (h->versioned >= versioned
13463 || !bfd_hide_sym_by_version (info->version_info,
13464 h->root.root.string)))))
13465 h->root.u.def.section->flags |= SEC_KEEP;
13466
13467 return TRUE;
13468 }
13469
13470 /* Keep all sections containing symbols undefined on the command-line,
13471 and the section containing the entry symbol. */
13472
13473 void
13474 _bfd_elf_gc_keep (struct bfd_link_info *info)
13475 {
13476 struct bfd_sym_chain *sym;
13477
13478 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13479 {
13480 struct elf_link_hash_entry *h;
13481
13482 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13483 FALSE, FALSE, FALSE);
13484
13485 if (h != NULL
13486 && (h->root.type == bfd_link_hash_defined
13487 || h->root.type == bfd_link_hash_defweak)
13488 && !bfd_is_abs_section (h->root.u.def.section)
13489 && !bfd_is_und_section (h->root.u.def.section))
13490 h->root.u.def.section->flags |= SEC_KEEP;
13491 }
13492 }
13493
13494 bfd_boolean
13495 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13496 struct bfd_link_info *info)
13497 {
13498 bfd *ibfd = info->input_bfds;
13499
13500 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13501 {
13502 asection *sec;
13503 struct elf_reloc_cookie cookie;
13504
13505 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13506 continue;
13507 sec = ibfd->sections;
13508 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13509 continue;
13510
13511 if (!init_reloc_cookie (&cookie, info, ibfd))
13512 return FALSE;
13513
13514 for (sec = ibfd->sections; sec; sec = sec->next)
13515 {
13516 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13517 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13518 {
13519 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13520 fini_reloc_cookie_rels (&cookie, sec);
13521 }
13522 }
13523 }
13524 return TRUE;
13525 }
13526
13527 /* Do mark and sweep of unused sections. */
13528
13529 bfd_boolean
13530 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13531 {
13532 bfd_boolean ok = TRUE;
13533 bfd *sub;
13534 elf_gc_mark_hook_fn gc_mark_hook;
13535 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13536 struct elf_link_hash_table *htab;
13537
13538 if (!bed->can_gc_sections
13539 || !is_elf_hash_table (info->hash))
13540 {
13541 _bfd_error_handler(_("warning: gc-sections option ignored"));
13542 return TRUE;
13543 }
13544
13545 bed->gc_keep (info);
13546 htab = elf_hash_table (info);
13547
13548 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13549 at the .eh_frame section if we can mark the FDEs individually. */
13550 for (sub = info->input_bfds;
13551 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13552 sub = sub->link.next)
13553 {
13554 asection *sec;
13555 struct elf_reloc_cookie cookie;
13556
13557 sec = sub->sections;
13558 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13559 continue;
13560 sec = bfd_get_section_by_name (sub, ".eh_frame");
13561 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13562 {
13563 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13564 if (elf_section_data (sec)->sec_info
13565 && (sec->flags & SEC_LINKER_CREATED) == 0)
13566 elf_eh_frame_section (sub) = sec;
13567 fini_reloc_cookie_for_section (&cookie, sec);
13568 sec = bfd_get_next_section_by_name (NULL, sec);
13569 }
13570 }
13571
13572 /* Apply transitive closure to the vtable entry usage info. */
13573 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13574 if (!ok)
13575 return FALSE;
13576
13577 /* Kill the vtable relocations that were not used. */
13578 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13579 if (!ok)
13580 return FALSE;
13581
13582 /* Mark dynamically referenced symbols. */
13583 if (htab->dynamic_sections_created || info->gc_keep_exported)
13584 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13585
13586 /* Grovel through relocs to find out who stays ... */
13587 gc_mark_hook = bed->gc_mark_hook;
13588 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13589 {
13590 asection *o;
13591
13592 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13593 || elf_object_id (sub) != elf_hash_table_id (htab)
13594 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13595 continue;
13596
13597 o = sub->sections;
13598 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13599 continue;
13600
13601 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13602 Also treat note sections as a root, if the section is not part
13603 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13604 well as FINI_ARRAY sections for ld -r. */
13605 for (o = sub->sections; o != NULL; o = o->next)
13606 if (!o->gc_mark
13607 && (o->flags & SEC_EXCLUDE) == 0
13608 && ((o->flags & SEC_KEEP) != 0
13609 || (bfd_link_relocatable (info)
13610 && ((elf_section_data (o)->this_hdr.sh_type
13611 == SHT_PREINIT_ARRAY)
13612 || (elf_section_data (o)->this_hdr.sh_type
13613 == SHT_INIT_ARRAY)
13614 || (elf_section_data (o)->this_hdr.sh_type
13615 == SHT_FINI_ARRAY)))
13616 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13617 && elf_next_in_group (o) == NULL )))
13618 {
13619 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13620 return FALSE;
13621 }
13622 }
13623
13624 /* Allow the backend to mark additional target specific sections. */
13625 bed->gc_mark_extra_sections (info, gc_mark_hook);
13626
13627 /* ... and mark SEC_EXCLUDE for those that go. */
13628 return elf_gc_sweep (abfd, info);
13629 }
13630
13631 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13633
13634 bfd_boolean
13635 bfd_elf_gc_record_vtinherit (bfd *abfd,
13636 asection *sec,
13637 struct elf_link_hash_entry *h,
13638 bfd_vma offset)
13639 {
13640 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13641 struct elf_link_hash_entry **search, *child;
13642 size_t extsymcount;
13643 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13644
13645 /* The sh_info field of the symtab header tells us where the
13646 external symbols start. We don't care about the local symbols at
13647 this point. */
13648 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13649 if (!elf_bad_symtab (abfd))
13650 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13651
13652 sym_hashes = elf_sym_hashes (abfd);
13653 sym_hashes_end = sym_hashes + extsymcount;
13654
13655 /* Hunt down the child symbol, which is in this section at the same
13656 offset as the relocation. */
13657 for (search = sym_hashes; search != sym_hashes_end; ++search)
13658 {
13659 if ((child = *search) != NULL
13660 && (child->root.type == bfd_link_hash_defined
13661 || child->root.type == bfd_link_hash_defweak)
13662 && child->root.u.def.section == sec
13663 && child->root.u.def.value == offset)
13664 goto win;
13665 }
13666
13667 /* xgettext:c-format */
13668 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13669 abfd, sec, (uint64_t) offset);
13670 bfd_set_error (bfd_error_invalid_operation);
13671 return FALSE;
13672
13673 win:
13674 if (!child->u2.vtable)
13675 {
13676 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13677 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13678 if (!child->u2.vtable)
13679 return FALSE;
13680 }
13681 if (!h)
13682 {
13683 /* This *should* only be the absolute section. It could potentially
13684 be that someone has defined a non-global vtable though, which
13685 would be bad. It isn't worth paging in the local symbols to be
13686 sure though; that case should simply be handled by the assembler. */
13687
13688 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13689 }
13690 else
13691 child->u2.vtable->parent = h;
13692
13693 return TRUE;
13694 }
13695
13696 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13697
13698 bfd_boolean
13699 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13700 asection *sec ATTRIBUTE_UNUSED,
13701 struct elf_link_hash_entry *h,
13702 bfd_vma addend)
13703 {
13704 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13705 unsigned int log_file_align = bed->s->log_file_align;
13706
13707 if (!h->u2.vtable)
13708 {
13709 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13710 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13711 if (!h->u2.vtable)
13712 return FALSE;
13713 }
13714
13715 if (addend >= h->u2.vtable->size)
13716 {
13717 size_t size, bytes, file_align;
13718 bfd_boolean *ptr = h->u2.vtable->used;
13719
13720 /* While the symbol is undefined, we have to be prepared to handle
13721 a zero size. */
13722 file_align = 1 << log_file_align;
13723 if (h->root.type == bfd_link_hash_undefined)
13724 size = addend + file_align;
13725 else
13726 {
13727 size = h->size;
13728 if (addend >= size)
13729 {
13730 /* Oops! We've got a reference past the defined end of
13731 the table. This is probably a bug -- shall we warn? */
13732 size = addend + file_align;
13733 }
13734 }
13735 size = (size + file_align - 1) & -file_align;
13736
13737 /* Allocate one extra entry for use as a "done" flag for the
13738 consolidation pass. */
13739 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13740
13741 if (ptr)
13742 {
13743 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13744
13745 if (ptr != NULL)
13746 {
13747 size_t oldbytes;
13748
13749 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13750 * sizeof (bfd_boolean));
13751 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13752 }
13753 }
13754 else
13755 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13756
13757 if (ptr == NULL)
13758 return FALSE;
13759
13760 /* And arrange for that done flag to be at index -1. */
13761 h->u2.vtable->used = ptr + 1;
13762 h->u2.vtable->size = size;
13763 }
13764
13765 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13766
13767 return TRUE;
13768 }
13769
13770 /* Map an ELF section header flag to its corresponding string. */
13771 typedef struct
13772 {
13773 char *flag_name;
13774 flagword flag_value;
13775 } elf_flags_to_name_table;
13776
13777 static elf_flags_to_name_table elf_flags_to_names [] =
13778 {
13779 { "SHF_WRITE", SHF_WRITE },
13780 { "SHF_ALLOC", SHF_ALLOC },
13781 { "SHF_EXECINSTR", SHF_EXECINSTR },
13782 { "SHF_MERGE", SHF_MERGE },
13783 { "SHF_STRINGS", SHF_STRINGS },
13784 { "SHF_INFO_LINK", SHF_INFO_LINK},
13785 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13786 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13787 { "SHF_GROUP", SHF_GROUP },
13788 { "SHF_TLS", SHF_TLS },
13789 { "SHF_MASKOS", SHF_MASKOS },
13790 { "SHF_EXCLUDE", SHF_EXCLUDE },
13791 };
13792
13793 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13794 bfd_boolean
13795 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13796 struct flag_info *flaginfo,
13797 asection *section)
13798 {
13799 const bfd_vma sh_flags = elf_section_flags (section);
13800
13801 if (!flaginfo->flags_initialized)
13802 {
13803 bfd *obfd = info->output_bfd;
13804 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13805 struct flag_info_list *tf = flaginfo->flag_list;
13806 int with_hex = 0;
13807 int without_hex = 0;
13808
13809 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13810 {
13811 unsigned i;
13812 flagword (*lookup) (char *);
13813
13814 lookup = bed->elf_backend_lookup_section_flags_hook;
13815 if (lookup != NULL)
13816 {
13817 flagword hexval = (*lookup) ((char *) tf->name);
13818
13819 if (hexval != 0)
13820 {
13821 if (tf->with == with_flags)
13822 with_hex |= hexval;
13823 else if (tf->with == without_flags)
13824 without_hex |= hexval;
13825 tf->valid = TRUE;
13826 continue;
13827 }
13828 }
13829 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13830 {
13831 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13832 {
13833 if (tf->with == with_flags)
13834 with_hex |= elf_flags_to_names[i].flag_value;
13835 else if (tf->with == without_flags)
13836 without_hex |= elf_flags_to_names[i].flag_value;
13837 tf->valid = TRUE;
13838 break;
13839 }
13840 }
13841 if (!tf->valid)
13842 {
13843 info->callbacks->einfo
13844 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13845 return FALSE;
13846 }
13847 }
13848 flaginfo->flags_initialized = TRUE;
13849 flaginfo->only_with_flags |= with_hex;
13850 flaginfo->not_with_flags |= without_hex;
13851 }
13852
13853 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13854 return FALSE;
13855
13856 if ((flaginfo->not_with_flags & sh_flags) != 0)
13857 return FALSE;
13858
13859 return TRUE;
13860 }
13861
13862 struct alloc_got_off_arg {
13863 bfd_vma gotoff;
13864 struct bfd_link_info *info;
13865 };
13866
13867 /* We need a special top-level link routine to convert got reference counts
13868 to real got offsets. */
13869
13870 static bfd_boolean
13871 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13872 {
13873 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13874 bfd *obfd = gofarg->info->output_bfd;
13875 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13876
13877 if (h->got.refcount > 0)
13878 {
13879 h->got.offset = gofarg->gotoff;
13880 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13881 }
13882 else
13883 h->got.offset = (bfd_vma) -1;
13884
13885 return TRUE;
13886 }
13887
13888 /* And an accompanying bit to work out final got entry offsets once
13889 we're done. Should be called from final_link. */
13890
13891 bfd_boolean
13892 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13893 struct bfd_link_info *info)
13894 {
13895 bfd *i;
13896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13897 bfd_vma gotoff;
13898 struct alloc_got_off_arg gofarg;
13899
13900 BFD_ASSERT (abfd == info->output_bfd);
13901
13902 if (! is_elf_hash_table (info->hash))
13903 return FALSE;
13904
13905 /* The GOT offset is relative to the .got section, but the GOT header is
13906 put into the .got.plt section, if the backend uses it. */
13907 if (bed->want_got_plt)
13908 gotoff = 0;
13909 else
13910 gotoff = bed->got_header_size;
13911
13912 /* Do the local .got entries first. */
13913 for (i = info->input_bfds; i; i = i->link.next)
13914 {
13915 bfd_signed_vma *local_got;
13916 size_t j, locsymcount;
13917 Elf_Internal_Shdr *symtab_hdr;
13918
13919 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13920 continue;
13921
13922 local_got = elf_local_got_refcounts (i);
13923 if (!local_got)
13924 continue;
13925
13926 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13927 if (elf_bad_symtab (i))
13928 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13929 else
13930 locsymcount = symtab_hdr->sh_info;
13931
13932 for (j = 0; j < locsymcount; ++j)
13933 {
13934 if (local_got[j] > 0)
13935 {
13936 local_got[j] = gotoff;
13937 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13938 }
13939 else
13940 local_got[j] = (bfd_vma) -1;
13941 }
13942 }
13943
13944 /* Then the global .got entries. .plt refcounts are handled by
13945 adjust_dynamic_symbol */
13946 gofarg.gotoff = gotoff;
13947 gofarg.info = info;
13948 elf_link_hash_traverse (elf_hash_table (info),
13949 elf_gc_allocate_got_offsets,
13950 &gofarg);
13951 return TRUE;
13952 }
13953
13954 /* Many folk need no more in the way of final link than this, once
13955 got entry reference counting is enabled. */
13956
13957 bfd_boolean
13958 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13959 {
13960 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13961 return FALSE;
13962
13963 /* Invoke the regular ELF backend linker to do all the work. */
13964 return bfd_elf_final_link (abfd, info);
13965 }
13966
13967 bfd_boolean
13968 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13969 {
13970 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13971
13972 if (rcookie->bad_symtab)
13973 rcookie->rel = rcookie->rels;
13974
13975 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13976 {
13977 unsigned long r_symndx;
13978
13979 if (! rcookie->bad_symtab)
13980 if (rcookie->rel->r_offset > offset)
13981 return FALSE;
13982 if (rcookie->rel->r_offset != offset)
13983 continue;
13984
13985 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13986 if (r_symndx == STN_UNDEF)
13987 return TRUE;
13988
13989 if (r_symndx >= rcookie->locsymcount
13990 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13991 {
13992 struct elf_link_hash_entry *h;
13993
13994 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13995
13996 while (h->root.type == bfd_link_hash_indirect
13997 || h->root.type == bfd_link_hash_warning)
13998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13999
14000 if ((h->root.type == bfd_link_hash_defined
14001 || h->root.type == bfd_link_hash_defweak)
14002 && (h->root.u.def.section->owner != rcookie->abfd
14003 || h->root.u.def.section->kept_section != NULL
14004 || discarded_section (h->root.u.def.section)))
14005 return TRUE;
14006 }
14007 else
14008 {
14009 /* It's not a relocation against a global symbol,
14010 but it could be a relocation against a local
14011 symbol for a discarded section. */
14012 asection *isec;
14013 Elf_Internal_Sym *isym;
14014
14015 /* Need to: get the symbol; get the section. */
14016 isym = &rcookie->locsyms[r_symndx];
14017 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14018 if (isec != NULL
14019 && (isec->kept_section != NULL
14020 || discarded_section (isec)))
14021 return TRUE;
14022 }
14023 return FALSE;
14024 }
14025 return FALSE;
14026 }
14027
14028 /* Discard unneeded references to discarded sections.
14029 Returns -1 on error, 1 if any section's size was changed, 0 if
14030 nothing changed. This function assumes that the relocations are in
14031 sorted order, which is true for all known assemblers. */
14032
14033 int
14034 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14035 {
14036 struct elf_reloc_cookie cookie;
14037 asection *o;
14038 bfd *abfd;
14039 int changed = 0;
14040
14041 if (info->traditional_format
14042 || !is_elf_hash_table (info->hash))
14043 return 0;
14044
14045 o = bfd_get_section_by_name (output_bfd, ".stab");
14046 if (o != NULL)
14047 {
14048 asection *i;
14049
14050 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14051 {
14052 if (i->size == 0
14053 || i->reloc_count == 0
14054 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14055 continue;
14056
14057 abfd = i->owner;
14058 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14059 continue;
14060
14061 if (!init_reloc_cookie_for_section (&cookie, info, i))
14062 return -1;
14063
14064 if (_bfd_discard_section_stabs (abfd, i,
14065 elf_section_data (i)->sec_info,
14066 bfd_elf_reloc_symbol_deleted_p,
14067 &cookie))
14068 changed = 1;
14069
14070 fini_reloc_cookie_for_section (&cookie, i);
14071 }
14072 }
14073
14074 o = NULL;
14075 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14076 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14077 if (o != NULL)
14078 {
14079 asection *i;
14080 int eh_changed = 0;
14081 unsigned int eh_alignment;
14082
14083 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14084 {
14085 if (i->size == 0)
14086 continue;
14087
14088 abfd = i->owner;
14089 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14090 continue;
14091
14092 if (!init_reloc_cookie_for_section (&cookie, info, i))
14093 return -1;
14094
14095 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14096 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14097 bfd_elf_reloc_symbol_deleted_p,
14098 &cookie))
14099 {
14100 eh_changed = 1;
14101 if (i->size != i->rawsize)
14102 changed = 1;
14103 }
14104
14105 fini_reloc_cookie_for_section (&cookie, i);
14106 }
14107
14108 eh_alignment = 1 << o->alignment_power;
14109 /* Skip over zero terminator, and prevent empty sections from
14110 adding alignment padding at the end. */
14111 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14112 if (i->size == 0)
14113 i->flags |= SEC_EXCLUDE;
14114 else if (i->size > 4)
14115 break;
14116 /* The last non-empty eh_frame section doesn't need padding. */
14117 if (i != NULL)
14118 i = i->map_tail.s;
14119 /* Any prior sections must pad the last FDE out to the output
14120 section alignment. Otherwise we might have zero padding
14121 between sections, which would be seen as a terminator. */
14122 for (; i != NULL; i = i->map_tail.s)
14123 if (i->size == 4)
14124 /* All but the last zero terminator should have been removed. */
14125 BFD_FAIL ();
14126 else
14127 {
14128 bfd_size_type size
14129 = (i->size + eh_alignment - 1) & -eh_alignment;
14130 if (i->size != size)
14131 {
14132 i->size = size;
14133 changed = 1;
14134 eh_changed = 1;
14135 }
14136 }
14137 if (eh_changed)
14138 elf_link_hash_traverse (elf_hash_table (info),
14139 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14140 }
14141
14142 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14143 {
14144 const struct elf_backend_data *bed;
14145 asection *s;
14146
14147 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14148 continue;
14149 s = abfd->sections;
14150 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14151 continue;
14152
14153 bed = get_elf_backend_data (abfd);
14154
14155 if (bed->elf_backend_discard_info != NULL)
14156 {
14157 if (!init_reloc_cookie (&cookie, info, abfd))
14158 return -1;
14159
14160 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14161 changed = 1;
14162
14163 fini_reloc_cookie (&cookie, abfd);
14164 }
14165 }
14166
14167 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14168 _bfd_elf_end_eh_frame_parsing (info);
14169
14170 if (info->eh_frame_hdr_type
14171 && !bfd_link_relocatable (info)
14172 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14173 changed = 1;
14174
14175 return changed;
14176 }
14177
14178 bfd_boolean
14179 _bfd_elf_section_already_linked (bfd *abfd,
14180 asection *sec,
14181 struct bfd_link_info *info)
14182 {
14183 flagword flags;
14184 const char *name, *key;
14185 struct bfd_section_already_linked *l;
14186 struct bfd_section_already_linked_hash_entry *already_linked_list;
14187
14188 if (sec->output_section == bfd_abs_section_ptr)
14189 return FALSE;
14190
14191 flags = sec->flags;
14192
14193 /* Return if it isn't a linkonce section. A comdat group section
14194 also has SEC_LINK_ONCE set. */
14195 if ((flags & SEC_LINK_ONCE) == 0)
14196 return FALSE;
14197
14198 /* Don't put group member sections on our list of already linked
14199 sections. They are handled as a group via their group section. */
14200 if (elf_sec_group (sec) != NULL)
14201 return FALSE;
14202
14203 /* For a SHT_GROUP section, use the group signature as the key. */
14204 name = sec->name;
14205 if ((flags & SEC_GROUP) != 0
14206 && elf_next_in_group (sec) != NULL
14207 && elf_group_name (elf_next_in_group (sec)) != NULL)
14208 key = elf_group_name (elf_next_in_group (sec));
14209 else
14210 {
14211 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14212 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14213 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14214 key++;
14215 else
14216 /* Must be a user linkonce section that doesn't follow gcc's
14217 naming convention. In this case we won't be matching
14218 single member groups. */
14219 key = name;
14220 }
14221
14222 already_linked_list = bfd_section_already_linked_table_lookup (key);
14223
14224 for (l = already_linked_list->entry; l != NULL; l = l->next)
14225 {
14226 /* We may have 2 different types of sections on the list: group
14227 sections with a signature of <key> (<key> is some string),
14228 and linkonce sections named .gnu.linkonce.<type>.<key>.
14229 Match like sections. LTO plugin sections are an exception.
14230 They are always named .gnu.linkonce.t.<key> and match either
14231 type of section. */
14232 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14233 && ((flags & SEC_GROUP) != 0
14234 || strcmp (name, l->sec->name) == 0))
14235 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14236 {
14237 /* The section has already been linked. See if we should
14238 issue a warning. */
14239 if (!_bfd_handle_already_linked (sec, l, info))
14240 return FALSE;
14241
14242 if (flags & SEC_GROUP)
14243 {
14244 asection *first = elf_next_in_group (sec);
14245 asection *s = first;
14246
14247 while (s != NULL)
14248 {
14249 s->output_section = bfd_abs_section_ptr;
14250 /* Record which group discards it. */
14251 s->kept_section = l->sec;
14252 s = elf_next_in_group (s);
14253 /* These lists are circular. */
14254 if (s == first)
14255 break;
14256 }
14257 }
14258
14259 return TRUE;
14260 }
14261 }
14262
14263 /* A single member comdat group section may be discarded by a
14264 linkonce section and vice versa. */
14265 if ((flags & SEC_GROUP) != 0)
14266 {
14267 asection *first = elf_next_in_group (sec);
14268
14269 if (first != NULL && elf_next_in_group (first) == first)
14270 /* Check this single member group against linkonce sections. */
14271 for (l = already_linked_list->entry; l != NULL; l = l->next)
14272 if ((l->sec->flags & SEC_GROUP) == 0
14273 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14274 {
14275 first->output_section = bfd_abs_section_ptr;
14276 first->kept_section = l->sec;
14277 sec->output_section = bfd_abs_section_ptr;
14278 break;
14279 }
14280 }
14281 else
14282 /* Check this linkonce section against single member groups. */
14283 for (l = already_linked_list->entry; l != NULL; l = l->next)
14284 if (l->sec->flags & SEC_GROUP)
14285 {
14286 asection *first = elf_next_in_group (l->sec);
14287
14288 if (first != NULL
14289 && elf_next_in_group (first) == first
14290 && bfd_elf_match_symbols_in_sections (first, sec, info))
14291 {
14292 sec->output_section = bfd_abs_section_ptr;
14293 sec->kept_section = first;
14294 break;
14295 }
14296 }
14297
14298 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14299 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14300 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14301 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14302 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14303 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14304 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14305 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14306 The reverse order cannot happen as there is never a bfd with only the
14307 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14308 matter as here were are looking only for cross-bfd sections. */
14309
14310 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14311 for (l = already_linked_list->entry; l != NULL; l = l->next)
14312 if ((l->sec->flags & SEC_GROUP) == 0
14313 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14314 {
14315 if (abfd != l->sec->owner)
14316 sec->output_section = bfd_abs_section_ptr;
14317 break;
14318 }
14319
14320 /* This is the first section with this name. Record it. */
14321 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14322 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14323 return sec->output_section == bfd_abs_section_ptr;
14324 }
14325
14326 bfd_boolean
14327 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14328 {
14329 return sym->st_shndx == SHN_COMMON;
14330 }
14331
14332 unsigned int
14333 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14334 {
14335 return SHN_COMMON;
14336 }
14337
14338 asection *
14339 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14340 {
14341 return bfd_com_section_ptr;
14342 }
14343
14344 bfd_vma
14345 _bfd_elf_default_got_elt_size (bfd *abfd,
14346 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14347 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14348 bfd *ibfd ATTRIBUTE_UNUSED,
14349 unsigned long symndx ATTRIBUTE_UNUSED)
14350 {
14351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14352 return bed->s->arch_size / 8;
14353 }
14354
14355 /* Routines to support the creation of dynamic relocs. */
14356
14357 /* Returns the name of the dynamic reloc section associated with SEC. */
14358
14359 static const char *
14360 get_dynamic_reloc_section_name (bfd * abfd,
14361 asection * sec,
14362 bfd_boolean is_rela)
14363 {
14364 char *name;
14365 const char *old_name = bfd_get_section_name (NULL, sec);
14366 const char *prefix = is_rela ? ".rela" : ".rel";
14367
14368 if (old_name == NULL)
14369 return NULL;
14370
14371 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14372 sprintf (name, "%s%s", prefix, old_name);
14373
14374 return name;
14375 }
14376
14377 /* Returns the dynamic reloc section associated with SEC.
14378 If necessary compute the name of the dynamic reloc section based
14379 on SEC's name (looked up in ABFD's string table) and the setting
14380 of IS_RELA. */
14381
14382 asection *
14383 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14384 asection * sec,
14385 bfd_boolean is_rela)
14386 {
14387 asection * reloc_sec = elf_section_data (sec)->sreloc;
14388
14389 if (reloc_sec == NULL)
14390 {
14391 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14392
14393 if (name != NULL)
14394 {
14395 reloc_sec = bfd_get_linker_section (abfd, name);
14396
14397 if (reloc_sec != NULL)
14398 elf_section_data (sec)->sreloc = reloc_sec;
14399 }
14400 }
14401
14402 return reloc_sec;
14403 }
14404
14405 /* Returns the dynamic reloc section associated with SEC. If the
14406 section does not exist it is created and attached to the DYNOBJ
14407 bfd and stored in the SRELOC field of SEC's elf_section_data
14408 structure.
14409
14410 ALIGNMENT is the alignment for the newly created section and
14411 IS_RELA defines whether the name should be .rela.<SEC's name>
14412 or .rel.<SEC's name>. The section name is looked up in the
14413 string table associated with ABFD. */
14414
14415 asection *
14416 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14417 bfd *dynobj,
14418 unsigned int alignment,
14419 bfd *abfd,
14420 bfd_boolean is_rela)
14421 {
14422 asection * reloc_sec = elf_section_data (sec)->sreloc;
14423
14424 if (reloc_sec == NULL)
14425 {
14426 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14427
14428 if (name == NULL)
14429 return NULL;
14430
14431 reloc_sec = bfd_get_linker_section (dynobj, name);
14432
14433 if (reloc_sec == NULL)
14434 {
14435 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14436 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14437 if ((sec->flags & SEC_ALLOC) != 0)
14438 flags |= SEC_ALLOC | SEC_LOAD;
14439
14440 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14441 if (reloc_sec != NULL)
14442 {
14443 /* _bfd_elf_get_sec_type_attr chooses a section type by
14444 name. Override as it may be wrong, eg. for a user
14445 section named "auto" we'll get ".relauto" which is
14446 seen to be a .rela section. */
14447 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14448 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14449 reloc_sec = NULL;
14450 }
14451 }
14452
14453 elf_section_data (sec)->sreloc = reloc_sec;
14454 }
14455
14456 return reloc_sec;
14457 }
14458
14459 /* Copy the ELF symbol type and other attributes for a linker script
14460 assignment from HSRC to HDEST. Generally this should be treated as
14461 if we found a strong non-dynamic definition for HDEST (except that
14462 ld ignores multiple definition errors). */
14463 void
14464 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14465 struct bfd_link_hash_entry *hdest,
14466 struct bfd_link_hash_entry *hsrc)
14467 {
14468 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14469 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14470 Elf_Internal_Sym isym;
14471
14472 ehdest->type = ehsrc->type;
14473 ehdest->target_internal = ehsrc->target_internal;
14474
14475 isym.st_other = ehsrc->other;
14476 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14477 }
14478
14479 /* Append a RELA relocation REL to section S in BFD. */
14480
14481 void
14482 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14483 {
14484 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14485 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14486 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14487 bed->s->swap_reloca_out (abfd, rel, loc);
14488 }
14489
14490 /* Append a REL relocation REL to section S in BFD. */
14491
14492 void
14493 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14494 {
14495 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14496 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14497 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14498 bed->s->swap_reloc_out (abfd, rel, loc);
14499 }
14500
14501 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14502
14503 struct bfd_link_hash_entry *
14504 bfd_elf_define_start_stop (struct bfd_link_info *info,
14505 const char *symbol, asection *sec)
14506 {
14507 struct elf_link_hash_entry *h;
14508
14509 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14510 FALSE, FALSE, TRUE);
14511 if (h != NULL
14512 && (h->root.type == bfd_link_hash_undefined
14513 || h->root.type == bfd_link_hash_undefweak
14514 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14515 {
14516 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14517 h->root.type = bfd_link_hash_defined;
14518 h->root.u.def.section = sec;
14519 h->root.u.def.value = 0;
14520 h->def_regular = 1;
14521 h->def_dynamic = 0;
14522 h->start_stop = 1;
14523 h->u2.start_stop_section = sec;
14524 if (symbol[0] == '.')
14525 {
14526 /* .startof. and .sizeof. symbols are local. */
14527 const struct elf_backend_data *bed;
14528 bed = get_elf_backend_data (info->output_bfd);
14529 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14530 }
14531 else
14532 {
14533 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14534 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14535 if (was_dynamic)
14536 bfd_elf_link_record_dynamic_symbol (info, h);
14537 }
14538 return &h->root;
14539 }
14540 return NULL;
14541 }
14542