elflink.c revision 1.13.12.3 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 h->dynamic = 1;
593 }
594
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598 bfd_boolean
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
601 const char *name,
602 bfd_boolean provide,
603 bfd_boolean hidden)
604 {
605 struct elf_link_hash_entry *h, *hv;
606 struct elf_link_hash_table *htab;
607 const struct elf_backend_data *bed;
608
609 if (!is_elf_hash_table (info->hash))
610 return TRUE;
611
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h == NULL)
615 return provide;
616
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
641 switch (h->root.type)
642 {
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h->root.type = bfd_link_hash_new;
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
655 break;
656 case bfd_link_hash_new:
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
673 default:
674 BFD_FAIL ();
675 return FALSE;
676 }
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
683 && h->def_dynamic
684 && !h->def_regular)
685 h->root.type = bfd_link_hash_undefined;
686
687 /* If this symbol is not being provided by the linker script, and it is
688 currently defined by a dynamic object, but not by a regular object,
689 then clear out any version information because the symbol will not be
690 associated with the dynamic object any more. */
691 if (!provide
692 && h->def_dynamic
693 && !h->def_regular)
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
698
699 h->def_regular = 1;
700
701 if (hidden)
702 {
703 bed = get_elf_backend_data (output_bfd);
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
711 if (!bfd_link_relocatable (info)
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
717 if ((h->def_dynamic
718 || h->ref_dynamic
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
721 && h->dynindx == -1)
722 {
723 if (! bfd_elf_link_record_dynamic_symbol (info, h))
724 return FALSE;
725
726 /* If this is a weak defined symbol, and we know a corresponding
727 real symbol from the same dynamic object, make sure the real
728 symbol is also made into a dynamic symbol. */
729 if (h->is_weakalias)
730 {
731 struct elf_link_hash_entry *def = weakdef (h);
732
733 if (def->dynindx == -1
734 && !bfd_elf_link_record_dynamic_symbol (info, def))
735 return FALSE;
736 }
737 }
738
739 return TRUE;
740 }
741
742 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
743 success, and 2 on a failure caused by attempting to record a symbol
744 in a discarded section, eg. a discarded link-once section symbol. */
745
746 int
747 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
748 bfd *input_bfd,
749 long input_indx)
750 {
751 bfd_size_type amt;
752 struct elf_link_local_dynamic_entry *entry;
753 struct elf_link_hash_table *eht;
754 struct elf_strtab_hash *dynstr;
755 size_t dynstr_index;
756 char *name;
757 Elf_External_Sym_Shndx eshndx;
758 char esym[sizeof (Elf64_External_Sym)];
759
760 if (! is_elf_hash_table (info->hash))
761 return 0;
762
763 /* See if the entry exists already. */
764 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
765 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
766 return 1;
767
768 amt = sizeof (*entry);
769 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
770 if (entry == NULL)
771 return 0;
772
773 /* Go find the symbol, so that we can find it's name. */
774 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
775 1, input_indx, &entry->isym, esym, &eshndx))
776 {
777 bfd_release (input_bfd, entry);
778 return 0;
779 }
780
781 if (entry->isym.st_shndx != SHN_UNDEF
782 && entry->isym.st_shndx < SHN_LORESERVE)
783 {
784 asection *s;
785
786 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
787 if (s == NULL || bfd_is_abs_section (s->output_section))
788 {
789 /* We can still bfd_release here as nothing has done another
790 bfd_alloc. We can't do this later in this function. */
791 bfd_release (input_bfd, entry);
792 return 2;
793 }
794 }
795
796 name = (bfd_elf_string_from_elf_section
797 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
798 entry->isym.st_name));
799
800 dynstr = elf_hash_table (info)->dynstr;
801 if (dynstr == NULL)
802 {
803 /* Create a strtab to hold the dynamic symbol names. */
804 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
805 if (dynstr == NULL)
806 return 0;
807 }
808
809 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
810 if (dynstr_index == (size_t) -1)
811 return 0;
812 entry->isym.st_name = dynstr_index;
813
814 eht = elf_hash_table (info);
815
816 entry->next = eht->dynlocal;
817 eht->dynlocal = entry;
818 entry->input_bfd = input_bfd;
819 entry->input_indx = input_indx;
820 eht->dynsymcount++;
821
822 /* Whatever binding the symbol had before, it's now local. */
823 entry->isym.st_info
824 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
825
826 /* The dynindx will be set at the end of size_dynamic_sections. */
827
828 return 1;
829 }
830
831 /* Return the dynindex of a local dynamic symbol. */
832
833 long
834 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
835 bfd *input_bfd,
836 long input_indx)
837 {
838 struct elf_link_local_dynamic_entry *e;
839
840 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
841 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
842 return e->dynindx;
843 return -1;
844 }
845
846 /* This function is used to renumber the dynamic symbols, if some of
847 them are removed because they are marked as local. This is called
848 via elf_link_hash_traverse. */
849
850 static bfd_boolean
851 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
852 void *data)
853 {
854 size_t *count = (size_t *) data;
855
856 if (h->forced_local)
857 return TRUE;
858
859 if (h->dynindx != -1)
860 h->dynindx = ++(*count);
861
862 return TRUE;
863 }
864
865
866 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
867 STB_LOCAL binding. */
868
869 static bfd_boolean
870 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
871 void *data)
872 {
873 size_t *count = (size_t *) data;
874
875 if (!h->forced_local)
876 return TRUE;
877
878 if (h->dynindx != -1)
879 h->dynindx = ++(*count);
880
881 return TRUE;
882 }
883
884 /* Return true if the dynamic symbol for a given section should be
885 omitted when creating a shared library. */
886 bfd_boolean
887 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
888 struct bfd_link_info *info,
889 asection *p)
890 {
891 struct elf_link_hash_table *htab;
892 asection *ip;
893
894 switch (elf_section_data (p)->this_hdr.sh_type)
895 {
896 case SHT_PROGBITS:
897 case SHT_NOBITS:
898 /* If sh_type is yet undecided, assume it could be
899 SHT_PROGBITS/SHT_NOBITS. */
900 case SHT_NULL:
901 htab = elf_hash_table (info);
902 if (p == htab->tls_sec)
903 return FALSE;
904
905 if (htab->text_index_section != NULL)
906 return p != htab->text_index_section && p != htab->data_index_section;
907
908 return (htab->dynobj != NULL
909 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
910 && ip->output_section == p);
911
912 /* There shouldn't be section relative relocations
913 against any other section. */
914 default:
915 return TRUE;
916 }
917 }
918
919 /* Assign dynsym indices. In a shared library we generate a section
920 symbol for each output section, which come first. Next come symbols
921 which have been forced to local binding. Then all of the back-end
922 allocated local dynamic syms, followed by the rest of the global
923 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
924 (This prevents the early call before elf_backend_init_index_section
925 and strip_excluded_output_sections setting dynindx for sections
926 that are stripped.) */
927
928 static unsigned long
929 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
930 struct bfd_link_info *info,
931 unsigned long *section_sym_count)
932 {
933 unsigned long dynsymcount = 0;
934 bfd_boolean do_sec = section_sym_count != NULL;
935
936 if (bfd_link_pic (info)
937 || elf_hash_table (info)->is_relocatable_executable)
938 {
939 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
940 asection *p;
941 for (p = output_bfd->sections; p ; p = p->next)
942 if ((p->flags & SEC_EXCLUDE) == 0
943 && (p->flags & SEC_ALLOC) != 0
944 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
945 {
946 ++dynsymcount;
947 if (do_sec)
948 elf_section_data (p)->dynindx = dynsymcount;
949 }
950 else if (do_sec)
951 elf_section_data (p)->dynindx = 0;
952 }
953 if (do_sec)
954 *section_sym_count = dynsymcount;
955
956 elf_link_hash_traverse (elf_hash_table (info),
957 elf_link_renumber_local_hash_table_dynsyms,
958 &dynsymcount);
959
960 if (elf_hash_table (info)->dynlocal)
961 {
962 struct elf_link_local_dynamic_entry *p;
963 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
964 p->dynindx = ++dynsymcount;
965 }
966 elf_hash_table (info)->local_dynsymcount = dynsymcount;
967
968 elf_link_hash_traverse (elf_hash_table (info),
969 elf_link_renumber_hash_table_dynsyms,
970 &dynsymcount);
971
972 /* There is an unused NULL entry at the head of the table which we
973 must account for in our count even if the table is empty since it
974 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
975 .dynamic section. */
976 dynsymcount++;
977
978 elf_hash_table (info)->dynsymcount = dynsymcount;
979 return dynsymcount;
980 }
981
982 /* Merge st_other field. */
983
984 static void
985 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
986 const Elf_Internal_Sym *isym, asection *sec,
987 bfd_boolean definition, bfd_boolean dynamic)
988 {
989 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
990
991 /* If st_other has a processor-specific meaning, specific
992 code might be needed here. */
993 if (bed->elf_backend_merge_symbol_attribute)
994 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
995 dynamic);
996
997 if (!dynamic)
998 {
999 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1000 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1001
1002 /* Keep the most constraining visibility. Leave the remainder
1003 of the st_other field to elf_backend_merge_symbol_attribute. */
1004 if (symvis - 1 < hvis - 1)
1005 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1006 }
1007 else if (definition
1008 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1009 && (sec->flags & SEC_READONLY) == 0)
1010 h->protected_def = 1;
1011 }
1012
1013 /* This function is called when we want to merge a new symbol with an
1014 existing symbol. It handles the various cases which arise when we
1015 find a definition in a dynamic object, or when there is already a
1016 definition in a dynamic object. The new symbol is described by
1017 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1018 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1019 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1020 of an old common symbol. We set OVERRIDE if the old symbol is
1021 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1022 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1023 to change. By OK to change, we mean that we shouldn't warn if the
1024 type or size does change. */
1025
1026 static bfd_boolean
1027 _bfd_elf_merge_symbol (bfd *abfd,
1028 struct bfd_link_info *info,
1029 const char *name,
1030 Elf_Internal_Sym *sym,
1031 asection **psec,
1032 bfd_vma *pvalue,
1033 struct elf_link_hash_entry **sym_hash,
1034 bfd **poldbfd,
1035 bfd_boolean *pold_weak,
1036 unsigned int *pold_alignment,
1037 bfd_boolean *skip,
1038 bfd_boolean *override,
1039 bfd_boolean *type_change_ok,
1040 bfd_boolean *size_change_ok,
1041 bfd_boolean *matched)
1042 {
1043 asection *sec, *oldsec;
1044 struct elf_link_hash_entry *h;
1045 struct elf_link_hash_entry *hi;
1046 struct elf_link_hash_entry *flip;
1047 int bind;
1048 bfd *oldbfd;
1049 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1050 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1051 const struct elf_backend_data *bed;
1052 char *new_version;
1053 bfd_boolean default_sym = *matched;
1054
1055 *skip = FALSE;
1056 *override = FALSE;
1057
1058 sec = *psec;
1059 bind = ELF_ST_BIND (sym->st_info);
1060
1061 if (! bfd_is_und_section (sec))
1062 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1063 else
1064 h = ((struct elf_link_hash_entry *)
1065 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1066 if (h == NULL)
1067 return FALSE;
1068 *sym_hash = h;
1069
1070 bed = get_elf_backend_data (abfd);
1071
1072 /* NEW_VERSION is the symbol version of the new symbol. */
1073 if (h->versioned != unversioned)
1074 {
1075 /* Symbol version is unknown or versioned. */
1076 new_version = strrchr (name, ELF_VER_CHR);
1077 if (new_version)
1078 {
1079 if (h->versioned == unknown)
1080 {
1081 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1082 h->versioned = versioned_hidden;
1083 else
1084 h->versioned = versioned;
1085 }
1086 new_version += 1;
1087 if (new_version[0] == '\0')
1088 new_version = NULL;
1089 }
1090 else
1091 h->versioned = unversioned;
1092 }
1093 else
1094 new_version = NULL;
1095
1096 /* For merging, we only care about real symbols. But we need to make
1097 sure that indirect symbol dynamic flags are updated. */
1098 hi = h;
1099 while (h->root.type == bfd_link_hash_indirect
1100 || h->root.type == bfd_link_hash_warning)
1101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1102
1103 if (!*matched)
1104 {
1105 if (hi == h || h->root.type == bfd_link_hash_new)
1106 *matched = TRUE;
1107 else
1108 {
1109 /* OLD_HIDDEN is true if the existing symbol is only visible
1110 to the symbol with the same symbol version. NEW_HIDDEN is
1111 true if the new symbol is only visible to the symbol with
1112 the same symbol version. */
1113 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1114 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1115 if (!old_hidden && !new_hidden)
1116 /* The new symbol matches the existing symbol if both
1117 aren't hidden. */
1118 *matched = TRUE;
1119 else
1120 {
1121 /* OLD_VERSION is the symbol version of the existing
1122 symbol. */
1123 char *old_version;
1124
1125 if (h->versioned >= versioned)
1126 old_version = strrchr (h->root.root.string,
1127 ELF_VER_CHR) + 1;
1128 else
1129 old_version = NULL;
1130
1131 /* The new symbol matches the existing symbol if they
1132 have the same symbol version. */
1133 *matched = (old_version == new_version
1134 || (old_version != NULL
1135 && new_version != NULL
1136 && strcmp (old_version, new_version) == 0));
1137 }
1138 }
1139 }
1140
1141 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1142 existing symbol. */
1143
1144 oldbfd = NULL;
1145 oldsec = NULL;
1146 switch (h->root.type)
1147 {
1148 default:
1149 break;
1150
1151 case bfd_link_hash_undefined:
1152 case bfd_link_hash_undefweak:
1153 oldbfd = h->root.u.undef.abfd;
1154 break;
1155
1156 case bfd_link_hash_defined:
1157 case bfd_link_hash_defweak:
1158 oldbfd = h->root.u.def.section->owner;
1159 oldsec = h->root.u.def.section;
1160 break;
1161
1162 case bfd_link_hash_common:
1163 oldbfd = h->root.u.c.p->section->owner;
1164 oldsec = h->root.u.c.p->section;
1165 if (pold_alignment)
1166 *pold_alignment = h->root.u.c.p->alignment_power;
1167 break;
1168 }
1169 if (poldbfd && *poldbfd == NULL)
1170 *poldbfd = oldbfd;
1171
1172 /* Differentiate strong and weak symbols. */
1173 newweak = bind == STB_WEAK;
1174 oldweak = (h->root.type == bfd_link_hash_defweak
1175 || h->root.type == bfd_link_hash_undefweak);
1176 if (pold_weak)
1177 *pold_weak = oldweak;
1178
1179 /* We have to check it for every instance since the first few may be
1180 references and not all compilers emit symbol type for undefined
1181 symbols. */
1182 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1183
1184 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1185 respectively, is from a dynamic object. */
1186
1187 newdyn = (abfd->flags & DYNAMIC) != 0;
1188
1189 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1190 syms and defined syms in dynamic libraries respectively.
1191 ref_dynamic on the other hand can be set for a symbol defined in
1192 a dynamic library, and def_dynamic may not be set; When the
1193 definition in a dynamic lib is overridden by a definition in the
1194 executable use of the symbol in the dynamic lib becomes a
1195 reference to the executable symbol. */
1196 if (newdyn)
1197 {
1198 if (bfd_is_und_section (sec))
1199 {
1200 if (bind != STB_WEAK)
1201 {
1202 h->ref_dynamic_nonweak = 1;
1203 hi->ref_dynamic_nonweak = 1;
1204 }
1205 }
1206 else
1207 {
1208 /* Update the existing symbol only if they match. */
1209 if (*matched)
1210 h->dynamic_def = 1;
1211 hi->dynamic_def = 1;
1212 }
1213 }
1214
1215 /* If we just created the symbol, mark it as being an ELF symbol.
1216 Other than that, there is nothing to do--there is no merge issue
1217 with a newly defined symbol--so we just return. */
1218
1219 if (h->root.type == bfd_link_hash_new)
1220 {
1221 h->non_elf = 0;
1222 return TRUE;
1223 }
1224
1225 /* In cases involving weak versioned symbols, we may wind up trying
1226 to merge a symbol with itself. Catch that here, to avoid the
1227 confusion that results if we try to override a symbol with
1228 itself. The additional tests catch cases like
1229 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1230 dynamic object, which we do want to handle here. */
1231 if (abfd == oldbfd
1232 && (newweak || oldweak)
1233 && ((abfd->flags & DYNAMIC) == 0
1234 || !h->def_regular))
1235 return TRUE;
1236
1237 olddyn = FALSE;
1238 if (oldbfd != NULL)
1239 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1240 else if (oldsec != NULL)
1241 {
1242 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1243 indices used by MIPS ELF. */
1244 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1245 }
1246
1247 /* Handle a case where plugin_notice won't be called and thus won't
1248 set the non_ir_ref flags on the first pass over symbols. */
1249 if (oldbfd != NULL
1250 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1251 && newdyn != olddyn)
1252 {
1253 h->root.non_ir_ref_dynamic = TRUE;
1254 hi->root.non_ir_ref_dynamic = TRUE;
1255 }
1256
1257 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1258 respectively, appear to be a definition rather than reference. */
1259
1260 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1261
1262 olddef = (h->root.type != bfd_link_hash_undefined
1263 && h->root.type != bfd_link_hash_undefweak
1264 && h->root.type != bfd_link_hash_common);
1265
1266 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1267 respectively, appear to be a function. */
1268
1269 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1270 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1271
1272 oldfunc = (h->type != STT_NOTYPE
1273 && bed->is_function_type (h->type));
1274
1275 if (!(newfunc && oldfunc)
1276 && ELF_ST_TYPE (sym->st_info) != h->type
1277 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1278 && h->type != STT_NOTYPE
1279 && (newdef || bfd_is_com_section (sec))
1280 && (olddef || h->root.type == bfd_link_hash_common))
1281 {
1282 /* If creating a default indirect symbol ("foo" or "foo@") from
1283 a dynamic versioned definition ("foo@@") skip doing so if
1284 there is an existing regular definition with a different
1285 type. We don't want, for example, a "time" variable in the
1286 executable overriding a "time" function in a shared library. */
1287 if (newdyn
1288 && !olddyn)
1289 {
1290 *skip = TRUE;
1291 return TRUE;
1292 }
1293
1294 /* When adding a symbol from a regular object file after we have
1295 created indirect symbols, undo the indirection and any
1296 dynamic state. */
1297 if (hi != h
1298 && !newdyn
1299 && olddyn)
1300 {
1301 h = hi;
1302 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1303 h->forced_local = 0;
1304 h->ref_dynamic = 0;
1305 h->def_dynamic = 0;
1306 h->dynamic_def = 0;
1307 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1308 {
1309 h->root.type = bfd_link_hash_undefined;
1310 h->root.u.undef.abfd = abfd;
1311 }
1312 else
1313 {
1314 h->root.type = bfd_link_hash_new;
1315 h->root.u.undef.abfd = NULL;
1316 }
1317 return TRUE;
1318 }
1319 }
1320
1321 /* Check TLS symbols. We don't check undefined symbols introduced
1322 by "ld -u" which have no type (and oldbfd NULL), and we don't
1323 check symbols from plugins because they also have no type. */
1324 if (oldbfd != NULL
1325 && (oldbfd->flags & BFD_PLUGIN) == 0
1326 && (abfd->flags & BFD_PLUGIN) == 0
1327 && ELF_ST_TYPE (sym->st_info) != h->type
1328 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1329 {
1330 bfd *ntbfd, *tbfd;
1331 bfd_boolean ntdef, tdef;
1332 asection *ntsec, *tsec;
1333
1334 if (h->type == STT_TLS)
1335 {
1336 ntbfd = abfd;
1337 ntsec = sec;
1338 ntdef = newdef;
1339 tbfd = oldbfd;
1340 tsec = oldsec;
1341 tdef = olddef;
1342 }
1343 else
1344 {
1345 ntbfd = oldbfd;
1346 ntsec = oldsec;
1347 ntdef = olddef;
1348 tbfd = abfd;
1349 tsec = sec;
1350 tdef = newdef;
1351 }
1352
1353 if (tdef && ntdef)
1354 _bfd_error_handler
1355 /* xgettext:c-format */
1356 (_("%s: TLS definition in %B section %A "
1357 "mismatches non-TLS definition in %B section %A"),
1358 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1359 else if (!tdef && !ntdef)
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS reference in %B "
1363 "mismatches non-TLS reference in %B"),
1364 h->root.root.string, tbfd, ntbfd);
1365 else if (tdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS definition in %B section %A "
1369 "mismatches non-TLS reference in %B"),
1370 h->root.root.string, tbfd, tsec, ntbfd);
1371 else
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS reference in %B "
1375 "mismatches non-TLS definition in %B section %A"),
1376 h->root.root.string, tbfd, ntbfd, ntsec);
1377
1378 bfd_set_error (bfd_error_bad_value);
1379 return FALSE;
1380 }
1381
1382 /* If the old symbol has non-default visibility, we ignore the new
1383 definition from a dynamic object. */
1384 if (newdyn
1385 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1386 && !bfd_is_und_section (sec))
1387 {
1388 *skip = TRUE;
1389 /* Make sure this symbol is dynamic. */
1390 h->ref_dynamic = 1;
1391 hi->ref_dynamic = 1;
1392 /* A protected symbol has external availability. Make sure it is
1393 recorded as dynamic.
1394
1395 FIXME: Should we check type and size for protected symbol? */
1396 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1397 return bfd_elf_link_record_dynamic_symbol (info, h);
1398 else
1399 return TRUE;
1400 }
1401 else if (!newdyn
1402 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1403 && h->def_dynamic)
1404 {
1405 /* If the new symbol with non-default visibility comes from a
1406 relocatable file and the old definition comes from a dynamic
1407 object, we remove the old definition. */
1408 if (hi->root.type == bfd_link_hash_indirect)
1409 {
1410 /* Handle the case where the old dynamic definition is
1411 default versioned. We need to copy the symbol info from
1412 the symbol with default version to the normal one if it
1413 was referenced before. */
1414 if (h->ref_regular)
1415 {
1416 hi->root.type = h->root.type;
1417 h->root.type = bfd_link_hash_indirect;
1418 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1419
1420 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1421 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1422 {
1423 /* If the new symbol is hidden or internal, completely undo
1424 any dynamic link state. */
1425 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1426 h->forced_local = 0;
1427 h->ref_dynamic = 0;
1428 }
1429 else
1430 h->ref_dynamic = 1;
1431
1432 h->def_dynamic = 0;
1433 /* FIXME: Should we check type and size for protected symbol? */
1434 h->size = 0;
1435 h->type = 0;
1436
1437 h = hi;
1438 }
1439 else
1440 h = hi;
1441 }
1442
1443 /* If the old symbol was undefined before, then it will still be
1444 on the undefs list. If the new symbol is undefined or
1445 common, we can't make it bfd_link_hash_new here, because new
1446 undefined or common symbols will be added to the undefs list
1447 by _bfd_generic_link_add_one_symbol. Symbols may not be
1448 added twice to the undefs list. Also, if the new symbol is
1449 undefweak then we don't want to lose the strong undef. */
1450 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1451 {
1452 h->root.type = bfd_link_hash_undefined;
1453 h->root.u.undef.abfd = abfd;
1454 }
1455 else
1456 {
1457 h->root.type = bfd_link_hash_new;
1458 h->root.u.undef.abfd = NULL;
1459 }
1460
1461 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1462 {
1463 /* If the new symbol is hidden or internal, completely undo
1464 any dynamic link state. */
1465 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1466 h->forced_local = 0;
1467 h->ref_dynamic = 0;
1468 }
1469 else
1470 h->ref_dynamic = 1;
1471 h->def_dynamic = 0;
1472 /* FIXME: Should we check type and size for protected symbol? */
1473 h->size = 0;
1474 h->type = 0;
1475 return TRUE;
1476 }
1477
1478 /* If a new weak symbol definition comes from a regular file and the
1479 old symbol comes from a dynamic library, we treat the new one as
1480 strong. Similarly, an old weak symbol definition from a regular
1481 file is treated as strong when the new symbol comes from a dynamic
1482 library. Further, an old weak symbol from a dynamic library is
1483 treated as strong if the new symbol is from a dynamic library.
1484 This reflects the way glibc's ld.so works.
1485
1486 Also allow a weak symbol to override a linker script symbol
1487 defined by an early pass over the script. This is done so the
1488 linker knows the symbol is defined in an object file, for the
1489 DEFINED script function.
1490
1491 Do this before setting *type_change_ok or *size_change_ok so that
1492 we warn properly when dynamic library symbols are overridden. */
1493
1494 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1495 newweak = FALSE;
1496 if (olddef && newdyn)
1497 oldweak = FALSE;
1498
1499 /* Allow changes between different types of function symbol. */
1500 if (newfunc && oldfunc)
1501 *type_change_ok = TRUE;
1502
1503 /* It's OK to change the type if either the existing symbol or the
1504 new symbol is weak. A type change is also OK if the old symbol
1505 is undefined and the new symbol is defined. */
1506
1507 if (oldweak
1508 || newweak
1509 || (newdef
1510 && h->root.type == bfd_link_hash_undefined))
1511 *type_change_ok = TRUE;
1512
1513 /* It's OK to change the size if either the existing symbol or the
1514 new symbol is weak, or if the old symbol is undefined. */
1515
1516 if (*type_change_ok
1517 || h->root.type == bfd_link_hash_undefined)
1518 *size_change_ok = TRUE;
1519
1520 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1521 symbol, respectively, appears to be a common symbol in a dynamic
1522 object. If a symbol appears in an uninitialized section, and is
1523 not weak, and is not a function, then it may be a common symbol
1524 which was resolved when the dynamic object was created. We want
1525 to treat such symbols specially, because they raise special
1526 considerations when setting the symbol size: if the symbol
1527 appears as a common symbol in a regular object, and the size in
1528 the regular object is larger, we must make sure that we use the
1529 larger size. This problematic case can always be avoided in C,
1530 but it must be handled correctly when using Fortran shared
1531 libraries.
1532
1533 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1534 likewise for OLDDYNCOMMON and OLDDEF.
1535
1536 Note that this test is just a heuristic, and that it is quite
1537 possible to have an uninitialized symbol in a shared object which
1538 is really a definition, rather than a common symbol. This could
1539 lead to some minor confusion when the symbol really is a common
1540 symbol in some regular object. However, I think it will be
1541 harmless. */
1542
1543 if (newdyn
1544 && newdef
1545 && !newweak
1546 && (sec->flags & SEC_ALLOC) != 0
1547 && (sec->flags & SEC_LOAD) == 0
1548 && sym->st_size > 0
1549 && !newfunc)
1550 newdyncommon = TRUE;
1551 else
1552 newdyncommon = FALSE;
1553
1554 if (olddyn
1555 && olddef
1556 && h->root.type == bfd_link_hash_defined
1557 && h->def_dynamic
1558 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1559 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1560 && h->size > 0
1561 && !oldfunc)
1562 olddyncommon = TRUE;
1563 else
1564 olddyncommon = FALSE;
1565
1566 /* We now know everything about the old and new symbols. We ask the
1567 backend to check if we can merge them. */
1568 if (bed->merge_symbol != NULL)
1569 {
1570 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1571 return FALSE;
1572 sec = *psec;
1573 }
1574
1575 /* There are multiple definitions of a normal symbol. Skip the
1576 default symbol as well as definition from an IR object. */
1577 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1578 && !default_sym && h->def_regular
1579 && !(oldbfd != NULL
1580 && (oldbfd->flags & BFD_PLUGIN) != 0
1581 && (abfd->flags & BFD_PLUGIN) == 0))
1582 {
1583 /* Handle a multiple definition. */
1584 (*info->callbacks->multiple_definition) (info, &h->root,
1585 abfd, sec, *pvalue);
1586 *skip = TRUE;
1587 return TRUE;
1588 }
1589
1590 /* If both the old and the new symbols look like common symbols in a
1591 dynamic object, set the size of the symbol to the larger of the
1592 two. */
1593
1594 if (olddyncommon
1595 && newdyncommon
1596 && sym->st_size != h->size)
1597 {
1598 /* Since we think we have two common symbols, issue a multiple
1599 common warning if desired. Note that we only warn if the
1600 size is different. If the size is the same, we simply let
1601 the old symbol override the new one as normally happens with
1602 symbols defined in dynamic objects. */
1603
1604 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1605 bfd_link_hash_common, sym->st_size);
1606 if (sym->st_size > h->size)
1607 h->size = sym->st_size;
1608
1609 *size_change_ok = TRUE;
1610 }
1611
1612 /* If we are looking at a dynamic object, and we have found a
1613 definition, we need to see if the symbol was already defined by
1614 some other object. If so, we want to use the existing
1615 definition, and we do not want to report a multiple symbol
1616 definition error; we do this by clobbering *PSEC to be
1617 bfd_und_section_ptr.
1618
1619 We treat a common symbol as a definition if the symbol in the
1620 shared library is a function, since common symbols always
1621 represent variables; this can cause confusion in principle, but
1622 any such confusion would seem to indicate an erroneous program or
1623 shared library. We also permit a common symbol in a regular
1624 object to override a weak symbol in a shared object. */
1625
1626 if (newdyn
1627 && newdef
1628 && (olddef
1629 || (h->root.type == bfd_link_hash_common
1630 && (newweak || newfunc))))
1631 {
1632 *override = TRUE;
1633 newdef = FALSE;
1634 newdyncommon = FALSE;
1635
1636 *psec = sec = bfd_und_section_ptr;
1637 *size_change_ok = TRUE;
1638
1639 /* If we get here when the old symbol is a common symbol, then
1640 we are explicitly letting it override a weak symbol or
1641 function in a dynamic object, and we don't want to warn about
1642 a type change. If the old symbol is a defined symbol, a type
1643 change warning may still be appropriate. */
1644
1645 if (h->root.type == bfd_link_hash_common)
1646 *type_change_ok = TRUE;
1647 }
1648
1649 /* Handle the special case of an old common symbol merging with a
1650 new symbol which looks like a common symbol in a shared object.
1651 We change *PSEC and *PVALUE to make the new symbol look like a
1652 common symbol, and let _bfd_generic_link_add_one_symbol do the
1653 right thing. */
1654
1655 if (newdyncommon
1656 && h->root.type == bfd_link_hash_common)
1657 {
1658 *override = TRUE;
1659 newdef = FALSE;
1660 newdyncommon = FALSE;
1661 *pvalue = sym->st_size;
1662 *psec = sec = bed->common_section (oldsec);
1663 *size_change_ok = TRUE;
1664 }
1665
1666 /* Skip weak definitions of symbols that are already defined. */
1667 if (newdef && olddef && newweak)
1668 {
1669 /* Don't skip new non-IR weak syms. */
1670 if (!(oldbfd != NULL
1671 && (oldbfd->flags & BFD_PLUGIN) != 0
1672 && (abfd->flags & BFD_PLUGIN) == 0))
1673 {
1674 newdef = FALSE;
1675 *skip = TRUE;
1676 }
1677
1678 /* Merge st_other. If the symbol already has a dynamic index,
1679 but visibility says it should not be visible, turn it into a
1680 local symbol. */
1681 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1682 if (h->dynindx != -1)
1683 switch (ELF_ST_VISIBILITY (h->other))
1684 {
1685 case STV_INTERNAL:
1686 case STV_HIDDEN:
1687 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1688 break;
1689 }
1690 }
1691
1692 /* If the old symbol is from a dynamic object, and the new symbol is
1693 a definition which is not from a dynamic object, then the new
1694 symbol overrides the old symbol. Symbols from regular files
1695 always take precedence over symbols from dynamic objects, even if
1696 they are defined after the dynamic object in the link.
1697
1698 As above, we again permit a common symbol in a regular object to
1699 override a definition in a shared object if the shared object
1700 symbol is a function or is weak. */
1701
1702 flip = NULL;
1703 if (!newdyn
1704 && (newdef
1705 || (bfd_is_com_section (sec)
1706 && (oldweak || oldfunc)))
1707 && olddyn
1708 && olddef
1709 && h->def_dynamic)
1710 {
1711 /* Change the hash table entry to undefined, and let
1712 _bfd_generic_link_add_one_symbol do the right thing with the
1713 new definition. */
1714
1715 h->root.type = bfd_link_hash_undefined;
1716 h->root.u.undef.abfd = h->root.u.def.section->owner;
1717 *size_change_ok = TRUE;
1718
1719 olddef = FALSE;
1720 olddyncommon = FALSE;
1721
1722 /* We again permit a type change when a common symbol may be
1723 overriding a function. */
1724
1725 if (bfd_is_com_section (sec))
1726 {
1727 if (oldfunc)
1728 {
1729 /* If a common symbol overrides a function, make sure
1730 that it isn't defined dynamically nor has type
1731 function. */
1732 h->def_dynamic = 0;
1733 h->type = STT_NOTYPE;
1734 }
1735 *type_change_ok = TRUE;
1736 }
1737
1738 if (hi->root.type == bfd_link_hash_indirect)
1739 flip = hi;
1740 else
1741 /* This union may have been set to be non-NULL when this symbol
1742 was seen in a dynamic object. We must force the union to be
1743 NULL, so that it is correct for a regular symbol. */
1744 h->verinfo.vertree = NULL;
1745 }
1746
1747 /* Handle the special case of a new common symbol merging with an
1748 old symbol that looks like it might be a common symbol defined in
1749 a shared object. Note that we have already handled the case in
1750 which a new common symbol should simply override the definition
1751 in the shared library. */
1752
1753 if (! newdyn
1754 && bfd_is_com_section (sec)
1755 && olddyncommon)
1756 {
1757 /* It would be best if we could set the hash table entry to a
1758 common symbol, but we don't know what to use for the section
1759 or the alignment. */
1760 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1761 bfd_link_hash_common, sym->st_size);
1762
1763 /* If the presumed common symbol in the dynamic object is
1764 larger, pretend that the new symbol has its size. */
1765
1766 if (h->size > *pvalue)
1767 *pvalue = h->size;
1768
1769 /* We need to remember the alignment required by the symbol
1770 in the dynamic object. */
1771 BFD_ASSERT (pold_alignment);
1772 *pold_alignment = h->root.u.def.section->alignment_power;
1773
1774 olddef = FALSE;
1775 olddyncommon = FALSE;
1776
1777 h->root.type = bfd_link_hash_undefined;
1778 h->root.u.undef.abfd = h->root.u.def.section->owner;
1779
1780 *size_change_ok = TRUE;
1781 *type_change_ok = TRUE;
1782
1783 if (hi->root.type == bfd_link_hash_indirect)
1784 flip = hi;
1785 else
1786 h->verinfo.vertree = NULL;
1787 }
1788
1789 if (flip != NULL)
1790 {
1791 /* Handle the case where we had a versioned symbol in a dynamic
1792 library and now find a definition in a normal object. In this
1793 case, we make the versioned symbol point to the normal one. */
1794 flip->root.type = h->root.type;
1795 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1796 h->root.type = bfd_link_hash_indirect;
1797 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1798 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1799 if (h->def_dynamic)
1800 {
1801 h->def_dynamic = 0;
1802 flip->ref_dynamic = 1;
1803 }
1804 }
1805
1806 return TRUE;
1807 }
1808
1809 /* This function is called to create an indirect symbol from the
1810 default for the symbol with the default version if needed. The
1811 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1812 set DYNSYM if the new indirect symbol is dynamic. */
1813
1814 static bfd_boolean
1815 _bfd_elf_add_default_symbol (bfd *abfd,
1816 struct bfd_link_info *info,
1817 struct elf_link_hash_entry *h,
1818 const char *name,
1819 Elf_Internal_Sym *sym,
1820 asection *sec,
1821 bfd_vma value,
1822 bfd **poldbfd,
1823 bfd_boolean *dynsym)
1824 {
1825 bfd_boolean type_change_ok;
1826 bfd_boolean size_change_ok;
1827 bfd_boolean skip;
1828 char *shortname;
1829 struct elf_link_hash_entry *hi;
1830 struct bfd_link_hash_entry *bh;
1831 const struct elf_backend_data *bed;
1832 bfd_boolean collect;
1833 bfd_boolean dynamic;
1834 bfd_boolean override;
1835 char *p;
1836 size_t len, shortlen;
1837 asection *tmp_sec;
1838 bfd_boolean matched;
1839
1840 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1841 return TRUE;
1842
1843 /* If this symbol has a version, and it is the default version, we
1844 create an indirect symbol from the default name to the fully
1845 decorated name. This will cause external references which do not
1846 specify a version to be bound to this version of the symbol. */
1847 p = strchr (name, ELF_VER_CHR);
1848 if (h->versioned == unknown)
1849 {
1850 if (p == NULL)
1851 {
1852 h->versioned = unversioned;
1853 return TRUE;
1854 }
1855 else
1856 {
1857 if (p[1] != ELF_VER_CHR)
1858 {
1859 h->versioned = versioned_hidden;
1860 return TRUE;
1861 }
1862 else
1863 h->versioned = versioned;
1864 }
1865 }
1866 else
1867 {
1868 /* PR ld/19073: We may see an unversioned definition after the
1869 default version. */
1870 if (p == NULL)
1871 return TRUE;
1872 }
1873
1874 bed = get_elf_backend_data (abfd);
1875 collect = bed->collect;
1876 dynamic = (abfd->flags & DYNAMIC) != 0;
1877
1878 shortlen = p - name;
1879 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1880 if (shortname == NULL)
1881 return FALSE;
1882 memcpy (shortname, name, shortlen);
1883 shortname[shortlen] = '\0';
1884
1885 /* We are going to create a new symbol. Merge it with any existing
1886 symbol with this name. For the purposes of the merge, act as
1887 though we were defining the symbol we just defined, although we
1888 actually going to define an indirect symbol. */
1889 type_change_ok = FALSE;
1890 size_change_ok = FALSE;
1891 matched = TRUE;
1892 tmp_sec = sec;
1893 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1894 &hi, poldbfd, NULL, NULL, &skip, &override,
1895 &type_change_ok, &size_change_ok, &matched))
1896 return FALSE;
1897
1898 if (skip)
1899 goto nondefault;
1900
1901 if (hi->def_regular)
1902 {
1903 /* If the undecorated symbol will have a version added by a
1904 script different to H, then don't indirect to/from the
1905 undecorated symbol. This isn't ideal because we may not yet
1906 have seen symbol versions, if given by a script on the
1907 command line rather than via --version-script. */
1908 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1909 {
1910 bfd_boolean hide;
1911
1912 hi->verinfo.vertree
1913 = bfd_find_version_for_sym (info->version_info,
1914 hi->root.root.string, &hide);
1915 if (hi->verinfo.vertree != NULL && hide)
1916 {
1917 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1918 goto nondefault;
1919 }
1920 }
1921 if (hi->verinfo.vertree != NULL
1922 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1923 goto nondefault;
1924 }
1925
1926 if (! override)
1927 {
1928 /* Add the default symbol if not performing a relocatable link. */
1929 if (! bfd_link_relocatable (info))
1930 {
1931 bh = &hi->root;
1932 if (! (_bfd_generic_link_add_one_symbol
1933 (info, abfd, shortname, BSF_INDIRECT,
1934 bfd_ind_section_ptr,
1935 0, name, FALSE, collect, &bh)))
1936 return FALSE;
1937 hi = (struct elf_link_hash_entry *) bh;
1938 }
1939 }
1940 else
1941 {
1942 /* In this case the symbol named SHORTNAME is overriding the
1943 indirect symbol we want to add. We were planning on making
1944 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1945 is the name without a version. NAME is the fully versioned
1946 name, and it is the default version.
1947
1948 Overriding means that we already saw a definition for the
1949 symbol SHORTNAME in a regular object, and it is overriding
1950 the symbol defined in the dynamic object.
1951
1952 When this happens, we actually want to change NAME, the
1953 symbol we just added, to refer to SHORTNAME. This will cause
1954 references to NAME in the shared object to become references
1955 to SHORTNAME in the regular object. This is what we expect
1956 when we override a function in a shared object: that the
1957 references in the shared object will be mapped to the
1958 definition in the regular object. */
1959
1960 while (hi->root.type == bfd_link_hash_indirect
1961 || hi->root.type == bfd_link_hash_warning)
1962 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1963
1964 h->root.type = bfd_link_hash_indirect;
1965 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1966 if (h->def_dynamic)
1967 {
1968 h->def_dynamic = 0;
1969 hi->ref_dynamic = 1;
1970 if (hi->ref_regular
1971 || hi->def_regular)
1972 {
1973 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1974 return FALSE;
1975 }
1976 }
1977
1978 /* Now set HI to H, so that the following code will set the
1979 other fields correctly. */
1980 hi = h;
1981 }
1982
1983 /* Check if HI is a warning symbol. */
1984 if (hi->root.type == bfd_link_hash_warning)
1985 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1986
1987 /* If there is a duplicate definition somewhere, then HI may not
1988 point to an indirect symbol. We will have reported an error to
1989 the user in that case. */
1990
1991 if (hi->root.type == bfd_link_hash_indirect)
1992 {
1993 struct elf_link_hash_entry *ht;
1994
1995 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1997
1998 /* A reference to the SHORTNAME symbol from a dynamic library
1999 will be satisfied by the versioned symbol at runtime. In
2000 effect, we have a reference to the versioned symbol. */
2001 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2002 hi->dynamic_def |= ht->dynamic_def;
2003
2004 /* See if the new flags lead us to realize that the symbol must
2005 be dynamic. */
2006 if (! *dynsym)
2007 {
2008 if (! dynamic)
2009 {
2010 if (! bfd_link_executable (info)
2011 || hi->def_dynamic
2012 || hi->ref_dynamic)
2013 *dynsym = TRUE;
2014 }
2015 else
2016 {
2017 if (hi->ref_regular)
2018 *dynsym = TRUE;
2019 }
2020 }
2021 }
2022
2023 /* We also need to define an indirection from the nondefault version
2024 of the symbol. */
2025
2026 nondefault:
2027 len = strlen (name);
2028 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2029 if (shortname == NULL)
2030 return FALSE;
2031 memcpy (shortname, name, shortlen);
2032 memcpy (shortname + shortlen, p + 1, len - shortlen);
2033
2034 /* Once again, merge with any existing symbol. */
2035 type_change_ok = FALSE;
2036 size_change_ok = FALSE;
2037 tmp_sec = sec;
2038 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2039 &hi, poldbfd, NULL, NULL, &skip, &override,
2040 &type_change_ok, &size_change_ok, &matched))
2041 return FALSE;
2042
2043 if (skip)
2044 return TRUE;
2045
2046 if (override)
2047 {
2048 /* Here SHORTNAME is a versioned name, so we don't expect to see
2049 the type of override we do in the case above unless it is
2050 overridden by a versioned definition. */
2051 if (hi->root.type != bfd_link_hash_defined
2052 && hi->root.type != bfd_link_hash_defweak)
2053 _bfd_error_handler
2054 /* xgettext:c-format */
2055 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2056 abfd, shortname);
2057 }
2058 else
2059 {
2060 bh = &hi->root;
2061 if (! (_bfd_generic_link_add_one_symbol
2062 (info, abfd, shortname, BSF_INDIRECT,
2063 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2064 return FALSE;
2065 hi = (struct elf_link_hash_entry *) bh;
2066
2067 /* If there is a duplicate definition somewhere, then HI may not
2068 point to an indirect symbol. We will have reported an error
2069 to the user in that case. */
2070
2071 if (hi->root.type == bfd_link_hash_indirect)
2072 {
2073 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2074 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2075 hi->dynamic_def |= h->dynamic_def;
2076
2077 /* See if the new flags lead us to realize that the symbol
2078 must be dynamic. */
2079 if (! *dynsym)
2080 {
2081 if (! dynamic)
2082 {
2083 if (! bfd_link_executable (info)
2084 || hi->ref_dynamic)
2085 *dynsym = TRUE;
2086 }
2087 else
2088 {
2089 if (hi->ref_regular)
2090 *dynsym = TRUE;
2091 }
2092 }
2093 }
2094 }
2095
2096 return TRUE;
2097 }
2098
2099 /* This routine is used to export all defined symbols into the dynamic
2101 symbol table. It is called via elf_link_hash_traverse. */
2102
2103 static bfd_boolean
2104 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2105 {
2106 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2107
2108 /* Ignore indirect symbols. These are added by the versioning code. */
2109 if (h->root.type == bfd_link_hash_indirect)
2110 return TRUE;
2111
2112 /* Ignore this if we won't export it. */
2113 if (!eif->info->export_dynamic && !h->dynamic)
2114 return TRUE;
2115
2116 if (h->dynindx == -1
2117 && (h->def_regular || h->ref_regular)
2118 && ! bfd_hide_sym_by_version (eif->info->version_info,
2119 h->root.root.string))
2120 {
2121 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2122 {
2123 eif->failed = TRUE;
2124 return FALSE;
2125 }
2126 }
2127
2128 return TRUE;
2129 }
2130
2131 /* Look through the symbols which are defined in other shared
2133 libraries and referenced here. Update the list of version
2134 dependencies. This will be put into the .gnu.version_r section.
2135 This function is called via elf_link_hash_traverse. */
2136
2137 static bfd_boolean
2138 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2139 void *data)
2140 {
2141 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2142 Elf_Internal_Verneed *t;
2143 Elf_Internal_Vernaux *a;
2144 bfd_size_type amt;
2145
2146 /* We only care about symbols defined in shared objects with version
2147 information. */
2148 if (!h->def_dynamic
2149 || h->def_regular
2150 || h->dynindx == -1
2151 || h->verinfo.verdef == NULL
2152 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2153 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2154 return TRUE;
2155
2156 /* See if we already know about this version. */
2157 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2158 t != NULL;
2159 t = t->vn_nextref)
2160 {
2161 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2162 continue;
2163
2164 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2165 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2166 return TRUE;
2167
2168 break;
2169 }
2170
2171 /* This is a new version. Add it to tree we are building. */
2172
2173 if (t == NULL)
2174 {
2175 amt = sizeof *t;
2176 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2177 if (t == NULL)
2178 {
2179 rinfo->failed = TRUE;
2180 return FALSE;
2181 }
2182
2183 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2184 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2185 elf_tdata (rinfo->info->output_bfd)->verref = t;
2186 }
2187
2188 amt = sizeof *a;
2189 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2190 if (a == NULL)
2191 {
2192 rinfo->failed = TRUE;
2193 return FALSE;
2194 }
2195
2196 /* Note that we are copying a string pointer here, and testing it
2197 above. If bfd_elf_string_from_elf_section is ever changed to
2198 discard the string data when low in memory, this will have to be
2199 fixed. */
2200 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2201
2202 a->vna_flags = h->verinfo.verdef->vd_flags;
2203 a->vna_nextptr = t->vn_auxptr;
2204
2205 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2206 ++rinfo->vers;
2207
2208 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2209
2210 t->vn_auxptr = a;
2211
2212 return TRUE;
2213 }
2214
2215 /* Figure out appropriate versions for all the symbols. We may not
2216 have the version number script until we have read all of the input
2217 files, so until that point we don't know which symbols should be
2218 local. This function is called via elf_link_hash_traverse. */
2219
2220 static bfd_boolean
2221 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2222 {
2223 struct elf_info_failed *sinfo;
2224 struct bfd_link_info *info;
2225 const struct elf_backend_data *bed;
2226 struct elf_info_failed eif;
2227 char *p;
2228
2229 sinfo = (struct elf_info_failed *) data;
2230 info = sinfo->info;
2231
2232 /* Fix the symbol flags. */
2233 eif.failed = FALSE;
2234 eif.info = info;
2235 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2236 {
2237 if (eif.failed)
2238 sinfo->failed = TRUE;
2239 return FALSE;
2240 }
2241
2242 /* We only need version numbers for symbols defined in regular
2243 objects. */
2244 if (!h->def_regular)
2245 return TRUE;
2246
2247 bed = get_elf_backend_data (info->output_bfd);
2248 p = strchr (h->root.root.string, ELF_VER_CHR);
2249 if (p != NULL && h->verinfo.vertree == NULL)
2250 {
2251 struct bfd_elf_version_tree *t;
2252
2253 ++p;
2254 if (*p == ELF_VER_CHR)
2255 ++p;
2256
2257 /* If there is no version string, we can just return out. */
2258 if (*p == '\0')
2259 return TRUE;
2260
2261 /* Look for the version. If we find it, it is no longer weak. */
2262 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2263 {
2264 if (strcmp (t->name, p) == 0)
2265 {
2266 size_t len;
2267 char *alc;
2268 struct bfd_elf_version_expr *d;
2269
2270 len = p - h->root.root.string;
2271 alc = (char *) bfd_malloc (len);
2272 if (alc == NULL)
2273 {
2274 sinfo->failed = TRUE;
2275 return FALSE;
2276 }
2277 memcpy (alc, h->root.root.string, len - 1);
2278 alc[len - 1] = '\0';
2279 if (alc[len - 2] == ELF_VER_CHR)
2280 alc[len - 2] = '\0';
2281
2282 h->verinfo.vertree = t;
2283 t->used = TRUE;
2284 d = NULL;
2285
2286 if (t->globals.list != NULL)
2287 d = (*t->match) (&t->globals, NULL, alc);
2288
2289 /* See if there is anything to force this symbol to
2290 local scope. */
2291 if (d == NULL && t->locals.list != NULL)
2292 {
2293 d = (*t->match) (&t->locals, NULL, alc);
2294 if (d != NULL
2295 && h->dynindx != -1
2296 && ! info->export_dynamic)
2297 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2298 }
2299
2300 free (alc);
2301 break;
2302 }
2303 }
2304
2305 /* If we are building an application, we need to create a
2306 version node for this version. */
2307 if (t == NULL && bfd_link_executable (info))
2308 {
2309 struct bfd_elf_version_tree **pp;
2310 int version_index;
2311
2312 /* If we aren't going to export this symbol, we don't need
2313 to worry about it. */
2314 if (h->dynindx == -1)
2315 return TRUE;
2316
2317 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2318 sizeof *t);
2319 if (t == NULL)
2320 {
2321 sinfo->failed = TRUE;
2322 return FALSE;
2323 }
2324
2325 t->name = p;
2326 t->name_indx = (unsigned int) -1;
2327 t->used = TRUE;
2328
2329 version_index = 1;
2330 /* Don't count anonymous version tag. */
2331 if (sinfo->info->version_info != NULL
2332 && sinfo->info->version_info->vernum == 0)
2333 version_index = 0;
2334 for (pp = &sinfo->info->version_info;
2335 *pp != NULL;
2336 pp = &(*pp)->next)
2337 ++version_index;
2338 t->vernum = version_index;
2339
2340 *pp = t;
2341
2342 h->verinfo.vertree = t;
2343 }
2344 else if (t == NULL)
2345 {
2346 /* We could not find the version for a symbol when
2347 generating a shared archive. Return an error. */
2348 _bfd_error_handler
2349 /* xgettext:c-format */
2350 (_("%B: version node not found for symbol %s"),
2351 info->output_bfd, h->root.root.string);
2352 bfd_set_error (bfd_error_bad_value);
2353 sinfo->failed = TRUE;
2354 return FALSE;
2355 }
2356 }
2357
2358 /* If we don't have a version for this symbol, see if we can find
2359 something. */
2360 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2361 {
2362 bfd_boolean hide;
2363
2364 h->verinfo.vertree
2365 = bfd_find_version_for_sym (sinfo->info->version_info,
2366 h->root.root.string, &hide);
2367 if (h->verinfo.vertree != NULL && hide)
2368 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2369 }
2370
2371 return TRUE;
2372 }
2373
2374 /* Read and swap the relocs from the section indicated by SHDR. This
2376 may be either a REL or a RELA section. The relocations are
2377 translated into RELA relocations and stored in INTERNAL_RELOCS,
2378 which should have already been allocated to contain enough space.
2379 The EXTERNAL_RELOCS are a buffer where the external form of the
2380 relocations should be stored.
2381
2382 Returns FALSE if something goes wrong. */
2383
2384 static bfd_boolean
2385 elf_link_read_relocs_from_section (bfd *abfd,
2386 asection *sec,
2387 Elf_Internal_Shdr *shdr,
2388 void *external_relocs,
2389 Elf_Internal_Rela *internal_relocs)
2390 {
2391 const struct elf_backend_data *bed;
2392 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2393 const bfd_byte *erela;
2394 const bfd_byte *erelaend;
2395 Elf_Internal_Rela *irela;
2396 Elf_Internal_Shdr *symtab_hdr;
2397 size_t nsyms;
2398
2399 /* Position ourselves at the start of the section. */
2400 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2401 return FALSE;
2402
2403 /* Read the relocations. */
2404 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2405 return FALSE;
2406
2407 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2408 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2409
2410 bed = get_elf_backend_data (abfd);
2411
2412 /* Convert the external relocations to the internal format. */
2413 if (shdr->sh_entsize == bed->s->sizeof_rel)
2414 swap_in = bed->s->swap_reloc_in;
2415 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2416 swap_in = bed->s->swap_reloca_in;
2417 else
2418 {
2419 bfd_set_error (bfd_error_wrong_format);
2420 return FALSE;
2421 }
2422
2423 erela = (const bfd_byte *) external_relocs;
2424 erelaend = erela + shdr->sh_size;
2425 irela = internal_relocs;
2426 while (erela < erelaend)
2427 {
2428 bfd_vma r_symndx;
2429
2430 (*swap_in) (abfd, erela, irela);
2431 r_symndx = ELF32_R_SYM (irela->r_info);
2432 if (bed->s->arch_size == 64)
2433 r_symndx >>= 24;
2434 if (nsyms > 0)
2435 {
2436 if ((size_t) r_symndx >= nsyms)
2437 {
2438 _bfd_error_handler
2439 /* xgettext:c-format */
2440 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2441 " for offset %#Lx in section `%A'"),
2442 abfd, r_symndx, (unsigned long) nsyms,
2443 irela->r_offset, sec);
2444 bfd_set_error (bfd_error_bad_value);
2445 return FALSE;
2446 }
2447 }
2448 else if (r_symndx != STN_UNDEF)
2449 {
2450 _bfd_error_handler
2451 /* xgettext:c-format */
2452 (_("%B: non-zero symbol index (%#Lx)"
2453 " for offset %#Lx in section `%A'"
2454 " when the object file has no symbol table"),
2455 abfd, r_symndx,
2456 irela->r_offset, sec);
2457 bfd_set_error (bfd_error_bad_value);
2458 return FALSE;
2459 }
2460 irela += bed->s->int_rels_per_ext_rel;
2461 erela += shdr->sh_entsize;
2462 }
2463
2464 return TRUE;
2465 }
2466
2467 /* Read and swap the relocs for a section O. They may have been
2468 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2469 not NULL, they are used as buffers to read into. They are known to
2470 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2471 the return value is allocated using either malloc or bfd_alloc,
2472 according to the KEEP_MEMORY argument. If O has two relocation
2473 sections (both REL and RELA relocations), then the REL_HDR
2474 relocations will appear first in INTERNAL_RELOCS, followed by the
2475 RELA_HDR relocations. */
2476
2477 Elf_Internal_Rela *
2478 _bfd_elf_link_read_relocs (bfd *abfd,
2479 asection *o,
2480 void *external_relocs,
2481 Elf_Internal_Rela *internal_relocs,
2482 bfd_boolean keep_memory)
2483 {
2484 void *alloc1 = NULL;
2485 Elf_Internal_Rela *alloc2 = NULL;
2486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2487 struct bfd_elf_section_data *esdo = elf_section_data (o);
2488 Elf_Internal_Rela *internal_rela_relocs;
2489
2490 if (esdo->relocs != NULL)
2491 return esdo->relocs;
2492
2493 if (o->reloc_count == 0)
2494 return NULL;
2495
2496 if (internal_relocs == NULL)
2497 {
2498 bfd_size_type size;
2499
2500 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2501 if (keep_memory)
2502 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2503 else
2504 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2505 if (internal_relocs == NULL)
2506 goto error_return;
2507 }
2508
2509 if (external_relocs == NULL)
2510 {
2511 bfd_size_type size = 0;
2512
2513 if (esdo->rel.hdr)
2514 size += esdo->rel.hdr->sh_size;
2515 if (esdo->rela.hdr)
2516 size += esdo->rela.hdr->sh_size;
2517
2518 alloc1 = bfd_malloc (size);
2519 if (alloc1 == NULL)
2520 goto error_return;
2521 external_relocs = alloc1;
2522 }
2523
2524 internal_rela_relocs = internal_relocs;
2525 if (esdo->rel.hdr)
2526 {
2527 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2528 external_relocs,
2529 internal_relocs))
2530 goto error_return;
2531 external_relocs = (((bfd_byte *) external_relocs)
2532 + esdo->rel.hdr->sh_size);
2533 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2534 * bed->s->int_rels_per_ext_rel);
2535 }
2536
2537 if (esdo->rela.hdr
2538 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2539 external_relocs,
2540 internal_rela_relocs)))
2541 goto error_return;
2542
2543 /* Cache the results for next time, if we can. */
2544 if (keep_memory)
2545 esdo->relocs = internal_relocs;
2546
2547 if (alloc1 != NULL)
2548 free (alloc1);
2549
2550 /* Don't free alloc2, since if it was allocated we are passing it
2551 back (under the name of internal_relocs). */
2552
2553 return internal_relocs;
2554
2555 error_return:
2556 if (alloc1 != NULL)
2557 free (alloc1);
2558 if (alloc2 != NULL)
2559 {
2560 if (keep_memory)
2561 bfd_release (abfd, alloc2);
2562 else
2563 free (alloc2);
2564 }
2565 return NULL;
2566 }
2567
2568 /* Compute the size of, and allocate space for, REL_HDR which is the
2569 section header for a section containing relocations for O. */
2570
2571 static bfd_boolean
2572 _bfd_elf_link_size_reloc_section (bfd *abfd,
2573 struct bfd_elf_section_reloc_data *reldata)
2574 {
2575 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2576
2577 /* That allows us to calculate the size of the section. */
2578 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2579
2580 /* The contents field must last into write_object_contents, so we
2581 allocate it with bfd_alloc rather than malloc. Also since we
2582 cannot be sure that the contents will actually be filled in,
2583 we zero the allocated space. */
2584 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2585 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2586 return FALSE;
2587
2588 if (reldata->hashes == NULL && reldata->count)
2589 {
2590 struct elf_link_hash_entry **p;
2591
2592 p = ((struct elf_link_hash_entry **)
2593 bfd_zmalloc (reldata->count * sizeof (*p)));
2594 if (p == NULL)
2595 return FALSE;
2596
2597 reldata->hashes = p;
2598 }
2599
2600 return TRUE;
2601 }
2602
2603 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2604 originated from the section given by INPUT_REL_HDR) to the
2605 OUTPUT_BFD. */
2606
2607 bfd_boolean
2608 _bfd_elf_link_output_relocs (bfd *output_bfd,
2609 asection *input_section,
2610 Elf_Internal_Shdr *input_rel_hdr,
2611 Elf_Internal_Rela *internal_relocs,
2612 struct elf_link_hash_entry **rel_hash
2613 ATTRIBUTE_UNUSED)
2614 {
2615 Elf_Internal_Rela *irela;
2616 Elf_Internal_Rela *irelaend;
2617 bfd_byte *erel;
2618 struct bfd_elf_section_reloc_data *output_reldata;
2619 asection *output_section;
2620 const struct elf_backend_data *bed;
2621 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2622 struct bfd_elf_section_data *esdo;
2623
2624 output_section = input_section->output_section;
2625
2626 bed = get_elf_backend_data (output_bfd);
2627 esdo = elf_section_data (output_section);
2628 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2629 {
2630 output_reldata = &esdo->rel;
2631 swap_out = bed->s->swap_reloc_out;
2632 }
2633 else if (esdo->rela.hdr
2634 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2635 {
2636 output_reldata = &esdo->rela;
2637 swap_out = bed->s->swap_reloca_out;
2638 }
2639 else
2640 {
2641 _bfd_error_handler
2642 /* xgettext:c-format */
2643 (_("%B: relocation size mismatch in %B section %A"),
2644 output_bfd, input_section->owner, input_section);
2645 bfd_set_error (bfd_error_wrong_format);
2646 return FALSE;
2647 }
2648
2649 erel = output_reldata->hdr->contents;
2650 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2651 irela = internal_relocs;
2652 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2653 * bed->s->int_rels_per_ext_rel);
2654 while (irela < irelaend)
2655 {
2656 (*swap_out) (output_bfd, irela, erel);
2657 irela += bed->s->int_rels_per_ext_rel;
2658 erel += input_rel_hdr->sh_entsize;
2659 }
2660
2661 /* Bump the counter, so that we know where to add the next set of
2662 relocations. */
2663 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2664
2665 return TRUE;
2666 }
2667
2668 /* Make weak undefined symbols in PIE dynamic. */
2670
2671 bfd_boolean
2672 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2673 struct elf_link_hash_entry *h)
2674 {
2675 if (bfd_link_pie (info)
2676 && h->dynindx == -1
2677 && h->root.type == bfd_link_hash_undefweak)
2678 return bfd_elf_link_record_dynamic_symbol (info, h);
2679
2680 return TRUE;
2681 }
2682
2683 /* Fix up the flags for a symbol. This handles various cases which
2684 can only be fixed after all the input files are seen. This is
2685 currently called by both adjust_dynamic_symbol and
2686 assign_sym_version, which is unnecessary but perhaps more robust in
2687 the face of future changes. */
2688
2689 static bfd_boolean
2690 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2691 struct elf_info_failed *eif)
2692 {
2693 const struct elf_backend_data *bed;
2694
2695 /* If this symbol was mentioned in a non-ELF file, try to set
2696 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2697 permit a non-ELF file to correctly refer to a symbol defined in
2698 an ELF dynamic object. */
2699 if (h->non_elf)
2700 {
2701 while (h->root.type == bfd_link_hash_indirect)
2702 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2703
2704 if (h->root.type != bfd_link_hash_defined
2705 && h->root.type != bfd_link_hash_defweak)
2706 {
2707 h->ref_regular = 1;
2708 h->ref_regular_nonweak = 1;
2709 }
2710 else
2711 {
2712 if (h->root.u.def.section->owner != NULL
2713 && (bfd_get_flavour (h->root.u.def.section->owner)
2714 == bfd_target_elf_flavour))
2715 {
2716 h->ref_regular = 1;
2717 h->ref_regular_nonweak = 1;
2718 }
2719 else
2720 h->def_regular = 1;
2721 }
2722
2723 if (h->dynindx == -1
2724 && (h->def_dynamic
2725 || h->ref_dynamic))
2726 {
2727 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2728 {
2729 eif->failed = TRUE;
2730 return FALSE;
2731 }
2732 }
2733 }
2734 else
2735 {
2736 /* Unfortunately, NON_ELF is only correct if the symbol
2737 was first seen in a non-ELF file. Fortunately, if the symbol
2738 was first seen in an ELF file, we're probably OK unless the
2739 symbol was defined in a non-ELF file. Catch that case here.
2740 FIXME: We're still in trouble if the symbol was first seen in
2741 a dynamic object, and then later in a non-ELF regular object. */
2742 if ((h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak)
2744 && !h->def_regular
2745 && (h->root.u.def.section->owner != NULL
2746 ? (bfd_get_flavour (h->root.u.def.section->owner)
2747 != bfd_target_elf_flavour)
2748 : (bfd_is_abs_section (h->root.u.def.section)
2749 && !h->def_dynamic)))
2750 h->def_regular = 1;
2751 }
2752
2753 /* Backend specific symbol fixup. */
2754 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2755 if (bed->elf_backend_fixup_symbol
2756 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2757 return FALSE;
2758
2759 /* If this is a final link, and the symbol was defined as a common
2760 symbol in a regular object file, and there was no definition in
2761 any dynamic object, then the linker will have allocated space for
2762 the symbol in a common section but the DEF_REGULAR
2763 flag will not have been set. */
2764 if (h->root.type == bfd_link_hash_defined
2765 && !h->def_regular
2766 && h->ref_regular
2767 && !h->def_dynamic
2768 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2769 h->def_regular = 1;
2770
2771 /* If a weak undefined symbol has non-default visibility, we also
2772 hide it from the dynamic linker. */
2773 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2774 && h->root.type == bfd_link_hash_undefweak)
2775 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2776
2777 /* A hidden versioned symbol in executable should be forced local if
2778 it is is locally defined, not referenced by shared library and not
2779 exported. */
2780 else if (bfd_link_executable (eif->info)
2781 && h->versioned == versioned_hidden
2782 && !eif->info->export_dynamic
2783 && !h->dynamic
2784 && !h->ref_dynamic
2785 && h->def_regular)
2786 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2787
2788 /* If -Bsymbolic was used (which means to bind references to global
2789 symbols to the definition within the shared object), and this
2790 symbol was defined in a regular object, then it actually doesn't
2791 need a PLT entry. Likewise, if the symbol has non-default
2792 visibility. If the symbol has hidden or internal visibility, we
2793 will force it local. */
2794 else if (h->needs_plt
2795 && bfd_link_pic (eif->info)
2796 && is_elf_hash_table (eif->info->hash)
2797 && (SYMBOLIC_BIND (eif->info, h)
2798 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2799 && h->def_regular)
2800 {
2801 bfd_boolean force_local;
2802
2803 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2804 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2805 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2806 }
2807
2808 /* If this is a weak defined symbol in a dynamic object, and we know
2809 the real definition in the dynamic object, copy interesting flags
2810 over to the real definition. */
2811 if (h->is_weakalias)
2812 {
2813 struct elf_link_hash_entry *def = weakdef (h);
2814 while (def->root.type == bfd_link_hash_indirect)
2815 def = (struct elf_link_hash_entry *) def->root.u.i.link;
2816
2817 /* If the real definition is defined by a regular object file,
2818 don't do anything special. See the longer description in
2819 _bfd_elf_adjust_dynamic_symbol, below. */
2820 if (def->def_regular)
2821 {
2822 h = def;
2823 while ((h = h->u.alias) != def)
2824 h->is_weakalias = 0;
2825 }
2826 else
2827 {
2828 while (h->root.type == bfd_link_hash_indirect)
2829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2830 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2831 || h->root.type == bfd_link_hash_defweak);
2832 BFD_ASSERT (def->def_dynamic);
2833 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2834 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2835 }
2836 }
2837
2838 return TRUE;
2839 }
2840
2841 /* Make the backend pick a good value for a dynamic symbol. This is
2842 called via elf_link_hash_traverse, and also calls itself
2843 recursively. */
2844
2845 static bfd_boolean
2846 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2847 {
2848 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2849 struct elf_link_hash_table *htab;
2850 const struct elf_backend_data *bed;
2851
2852 if (! is_elf_hash_table (eif->info->hash))
2853 return FALSE;
2854
2855 /* Ignore indirect symbols. These are added by the versioning code. */
2856 if (h->root.type == bfd_link_hash_indirect)
2857 return TRUE;
2858
2859 /* Fix the symbol flags. */
2860 if (! _bfd_elf_fix_symbol_flags (h, eif))
2861 return FALSE;
2862
2863 htab = elf_hash_table (eif->info);
2864 bed = get_elf_backend_data (htab->dynobj);
2865
2866 if (h->root.type == bfd_link_hash_undefweak)
2867 {
2868 if (eif->info->dynamic_undefined_weak == 0)
2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870 else if (eif->info->dynamic_undefined_weak > 0
2871 && h->ref_regular
2872 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2873 && !bfd_hide_sym_by_version (eif->info->version_info,
2874 h->root.root.string))
2875 {
2876 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2877 {
2878 eif->failed = TRUE;
2879 return FALSE;
2880 }
2881 }
2882 }
2883
2884 /* If this symbol does not require a PLT entry, and it is not
2885 defined by a dynamic object, or is not referenced by a regular
2886 object, ignore it. We do have to handle a weak defined symbol,
2887 even if no regular object refers to it, if we decided to add it
2888 to the dynamic symbol table. FIXME: Do we normally need to worry
2889 about symbols which are defined by one dynamic object and
2890 referenced by another one? */
2891 if (!h->needs_plt
2892 && h->type != STT_GNU_IFUNC
2893 && (h->def_regular
2894 || !h->def_dynamic
2895 || (!h->ref_regular
2896 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2897 {
2898 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2899 return TRUE;
2900 }
2901
2902 /* If we've already adjusted this symbol, don't do it again. This
2903 can happen via a recursive call. */
2904 if (h->dynamic_adjusted)
2905 return TRUE;
2906
2907 /* Don't look at this symbol again. Note that we must set this
2908 after checking the above conditions, because we may look at a
2909 symbol once, decide not to do anything, and then get called
2910 recursively later after REF_REGULAR is set below. */
2911 h->dynamic_adjusted = 1;
2912
2913 /* If this is a weak definition, and we know a real definition, and
2914 the real symbol is not itself defined by a regular object file,
2915 then get a good value for the real definition. We handle the
2916 real symbol first, for the convenience of the backend routine.
2917
2918 Note that there is a confusing case here. If the real definition
2919 is defined by a regular object file, we don't get the real symbol
2920 from the dynamic object, but we do get the weak symbol. If the
2921 processor backend uses a COPY reloc, then if some routine in the
2922 dynamic object changes the real symbol, we will not see that
2923 change in the corresponding weak symbol. This is the way other
2924 ELF linkers work as well, and seems to be a result of the shared
2925 library model.
2926
2927 I will clarify this issue. Most SVR4 shared libraries define the
2928 variable _timezone and define timezone as a weak synonym. The
2929 tzset call changes _timezone. If you write
2930 extern int timezone;
2931 int _timezone = 5;
2932 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 you might expect that, since timezone is a synonym for _timezone,
2934 the same number will print both times. However, if the processor
2935 backend uses a COPY reloc, then actually timezone will be copied
2936 into your process image, and, since you define _timezone
2937 yourself, _timezone will not. Thus timezone and _timezone will
2938 wind up at different memory locations. The tzset call will set
2939 _timezone, leaving timezone unchanged. */
2940
2941 if (h->is_weakalias)
2942 {
2943 struct elf_link_hash_entry *def = weakdef (h);
2944
2945 /* If we get to this point, there is an implicit reference to
2946 the alias by a regular object file via the weak symbol H. */
2947 def->ref_regular = 1;
2948
2949 /* Ensure that the backend adjust_dynamic_symbol function sees
2950 the strong alias before H by recursively calling ourselves. */
2951 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2952 return FALSE;
2953 }
2954
2955 /* If a symbol has no type and no size and does not require a PLT
2956 entry, then we are probably about to do the wrong thing here: we
2957 are probably going to create a COPY reloc for an empty object.
2958 This case can arise when a shared object is built with assembly
2959 code, and the assembly code fails to set the symbol type. */
2960 if (h->size == 0
2961 && h->type == STT_NOTYPE
2962 && !h->needs_plt)
2963 _bfd_error_handler
2964 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 h->root.root.string);
2966
2967 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2968 {
2969 eif->failed = TRUE;
2970 return FALSE;
2971 }
2972
2973 return TRUE;
2974 }
2975
2976 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2977 DYNBSS. */
2978
2979 bfd_boolean
2980 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2981 struct elf_link_hash_entry *h,
2982 asection *dynbss)
2983 {
2984 unsigned int power_of_two;
2985 bfd_vma mask;
2986 asection *sec = h->root.u.def.section;
2987
2988 /* The section alignment of the definition is the maximum alignment
2989 requirement of symbols defined in the section. Since we don't
2990 know the symbol alignment requirement, we start with the
2991 maximum alignment and check low bits of the symbol address
2992 for the minimum alignment. */
2993 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2994 mask = ((bfd_vma) 1 << power_of_two) - 1;
2995 while ((h->root.u.def.value & mask) != 0)
2996 {
2997 mask >>= 1;
2998 --power_of_two;
2999 }
3000
3001 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3002 dynbss))
3003 {
3004 /* Adjust the section alignment if needed. */
3005 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3006 power_of_two))
3007 return FALSE;
3008 }
3009
3010 /* We make sure that the symbol will be aligned properly. */
3011 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3012
3013 /* Define the symbol as being at this point in DYNBSS. */
3014 h->root.u.def.section = dynbss;
3015 h->root.u.def.value = dynbss->size;
3016
3017 /* Increment the size of DYNBSS to make room for the symbol. */
3018 dynbss->size += h->size;
3019
3020 /* No error if extern_protected_data is true. */
3021 if (h->protected_def
3022 && (!info->extern_protected_data
3023 || (info->extern_protected_data < 0
3024 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3025 info->callbacks->einfo
3026 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3027 h->root.root.string);
3028
3029 return TRUE;
3030 }
3031
3032 /* Adjust all external symbols pointing into SEC_MERGE sections
3033 to reflect the object merging within the sections. */
3034
3035 static bfd_boolean
3036 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3037 {
3038 asection *sec;
3039
3040 if ((h->root.type == bfd_link_hash_defined
3041 || h->root.type == bfd_link_hash_defweak)
3042 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3043 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3044 {
3045 bfd *output_bfd = (bfd *) data;
3046
3047 h->root.u.def.value =
3048 _bfd_merged_section_offset (output_bfd,
3049 &h->root.u.def.section,
3050 elf_section_data (sec)->sec_info,
3051 h->root.u.def.value);
3052 }
3053
3054 return TRUE;
3055 }
3056
3057 /* Returns false if the symbol referred to by H should be considered
3058 to resolve local to the current module, and true if it should be
3059 considered to bind dynamically. */
3060
3061 bfd_boolean
3062 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3063 struct bfd_link_info *info,
3064 bfd_boolean not_local_protected)
3065 {
3066 bfd_boolean binding_stays_local_p;
3067 const struct elf_backend_data *bed;
3068 struct elf_link_hash_table *hash_table;
3069
3070 if (h == NULL)
3071 return FALSE;
3072
3073 while (h->root.type == bfd_link_hash_indirect
3074 || h->root.type == bfd_link_hash_warning)
3075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3076
3077 /* If it was forced local, then clearly it's not dynamic. */
3078 if (h->dynindx == -1)
3079 return FALSE;
3080 if (h->forced_local)
3081 return FALSE;
3082
3083 /* Identify the cases where name binding rules say that a
3084 visible symbol resolves locally. */
3085 binding_stays_local_p = (bfd_link_executable (info)
3086 || SYMBOLIC_BIND (info, h));
3087
3088 switch (ELF_ST_VISIBILITY (h->other))
3089 {
3090 case STV_INTERNAL:
3091 case STV_HIDDEN:
3092 return FALSE;
3093
3094 case STV_PROTECTED:
3095 hash_table = elf_hash_table (info);
3096 if (!is_elf_hash_table (hash_table))
3097 return FALSE;
3098
3099 bed = get_elf_backend_data (hash_table->dynobj);
3100
3101 /* Proper resolution for function pointer equality may require
3102 that these symbols perhaps be resolved dynamically, even though
3103 we should be resolving them to the current module. */
3104 if (!not_local_protected || !bed->is_function_type (h->type))
3105 binding_stays_local_p = TRUE;
3106 break;
3107
3108 default:
3109 break;
3110 }
3111
3112 /* If it isn't defined locally, then clearly it's dynamic. */
3113 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3114 return TRUE;
3115
3116 /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 us that it remains local. */
3118 return !binding_stays_local_p;
3119 }
3120
3121 /* Return true if the symbol referred to by H should be considered
3122 to resolve local to the current module, and false otherwise. Differs
3123 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3124 undefined symbols. The two functions are virtually identical except
3125 for the place where dynindx == -1 is tested. If that test is true,
3126 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3128 defined symbols.
3129 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 treatment of undefined weak symbols. For those that do not make
3132 undefined weak symbols dynamic, both functions may return false. */
3133
3134 bfd_boolean
3135 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3136 struct bfd_link_info *info,
3137 bfd_boolean local_protected)
3138 {
3139 const struct elf_backend_data *bed;
3140 struct elf_link_hash_table *hash_table;
3141
3142 /* If it's a local sym, of course we resolve locally. */
3143 if (h == NULL)
3144 return TRUE;
3145
3146 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3148 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3149 return TRUE;
3150
3151 /* Forced local symbols resolve locally. */
3152 if (h->forced_local)
3153 return TRUE;
3154
3155 /* Common symbols that become definitions don't get the DEF_REGULAR
3156 flag set, so test it first, and don't bail out. */
3157 if (ELF_COMMON_DEF_P (h))
3158 /* Do nothing. */;
3159 /* If we don't have a definition in a regular file, then we can't
3160 resolve locally. The sym is either undefined or dynamic. */
3161 else if (!h->def_regular)
3162 return FALSE;
3163
3164 /* Non-dynamic symbols resolve locally. */
3165 if (h->dynindx == -1)
3166 return TRUE;
3167
3168 /* At this point, we know the symbol is defined and dynamic. In an
3169 executable it must resolve locally, likewise when building symbolic
3170 shared libraries. */
3171 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3172 return TRUE;
3173
3174 /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 with default visibility might not resolve locally. */
3176 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3177 return FALSE;
3178
3179 hash_table = elf_hash_table (info);
3180 if (!is_elf_hash_table (hash_table))
3181 return TRUE;
3182
3183 bed = get_elf_backend_data (hash_table->dynobj);
3184
3185 /* If extern_protected_data is false, STV_PROTECTED non-function
3186 symbols are local. */
3187 if ((!info->extern_protected_data
3188 || (info->extern_protected_data < 0
3189 && !bed->extern_protected_data))
3190 && !bed->is_function_type (h->type))
3191 return TRUE;
3192
3193 /* Function pointer equality tests may require that STV_PROTECTED
3194 symbols be treated as dynamic symbols. If the address of a
3195 function not defined in an executable is set to that function's
3196 plt entry in the executable, then the address of the function in
3197 a shared library must also be the plt entry in the executable. */
3198 return local_protected;
3199 }
3200
3201 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 aligned. Returns the first TLS output section. */
3203
3204 struct bfd_section *
3205 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3206 {
3207 struct bfd_section *sec, *tls;
3208 unsigned int align = 0;
3209
3210 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3211 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3212 break;
3213 tls = sec;
3214
3215 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3216 if (sec->alignment_power > align)
3217 align = sec->alignment_power;
3218
3219 elf_hash_table (info)->tls_sec = tls;
3220
3221 /* Ensure the alignment of the first section is the largest alignment,
3222 so that the tls segment starts aligned. */
3223 if (tls != NULL)
3224 tls->alignment_power = align;
3225
3226 return tls;
3227 }
3228
3229 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3230 static bfd_boolean
3231 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3232 Elf_Internal_Sym *sym)
3233 {
3234 const struct elf_backend_data *bed;
3235
3236 /* Local symbols do not count, but target specific ones might. */
3237 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3238 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3239 return FALSE;
3240
3241 bed = get_elf_backend_data (abfd);
3242 /* Function symbols do not count. */
3243 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3244 return FALSE;
3245
3246 /* If the section is undefined, then so is the symbol. */
3247 if (sym->st_shndx == SHN_UNDEF)
3248 return FALSE;
3249
3250 /* If the symbol is defined in the common section, then
3251 it is a common definition and so does not count. */
3252 if (bed->common_definition (sym))
3253 return FALSE;
3254
3255 /* If the symbol is in a target specific section then we
3256 must rely upon the backend to tell us what it is. */
3257 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3258 /* FIXME - this function is not coded yet:
3259
3260 return _bfd_is_global_symbol_definition (abfd, sym);
3261
3262 Instead for now assume that the definition is not global,
3263 Even if this is wrong, at least the linker will behave
3264 in the same way that it used to do. */
3265 return FALSE;
3266
3267 return TRUE;
3268 }
3269
3270 /* Search the symbol table of the archive element of the archive ABFD
3271 whose archive map contains a mention of SYMDEF, and determine if
3272 the symbol is defined in this element. */
3273 static bfd_boolean
3274 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3275 {
3276 Elf_Internal_Shdr * hdr;
3277 size_t symcount;
3278 size_t extsymcount;
3279 size_t extsymoff;
3280 Elf_Internal_Sym *isymbuf;
3281 Elf_Internal_Sym *isym;
3282 Elf_Internal_Sym *isymend;
3283 bfd_boolean result;
3284
3285 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3286 if (abfd == NULL)
3287 return FALSE;
3288
3289 if (! bfd_check_format (abfd, bfd_object))
3290 return FALSE;
3291
3292 /* Select the appropriate symbol table. If we don't know if the
3293 object file is an IR object, give linker LTO plugin a chance to
3294 get the correct symbol table. */
3295 if (abfd->plugin_format == bfd_plugin_yes
3296 #if BFD_SUPPORTS_PLUGINS
3297 || (abfd->plugin_format == bfd_plugin_unknown
3298 && bfd_link_plugin_object_p (abfd))
3299 #endif
3300 )
3301 {
3302 /* Use the IR symbol table if the object has been claimed by
3303 plugin. */
3304 abfd = abfd->plugin_dummy_bfd;
3305 hdr = &elf_tdata (abfd)->symtab_hdr;
3306 }
3307 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3308 hdr = &elf_tdata (abfd)->symtab_hdr;
3309 else
3310 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3311
3312 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3313
3314 /* The sh_info field of the symtab header tells us where the
3315 external symbols start. We don't care about the local symbols. */
3316 if (elf_bad_symtab (abfd))
3317 {
3318 extsymcount = symcount;
3319 extsymoff = 0;
3320 }
3321 else
3322 {
3323 extsymcount = symcount - hdr->sh_info;
3324 extsymoff = hdr->sh_info;
3325 }
3326
3327 if (extsymcount == 0)
3328 return FALSE;
3329
3330 /* Read in the symbol table. */
3331 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3332 NULL, NULL, NULL);
3333 if (isymbuf == NULL)
3334 return FALSE;
3335
3336 /* Scan the symbol table looking for SYMDEF. */
3337 result = FALSE;
3338 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3339 {
3340 const char *name;
3341
3342 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3343 isym->st_name);
3344 if (name == NULL)
3345 break;
3346
3347 if (strcmp (name, symdef->name) == 0)
3348 {
3349 result = is_global_data_symbol_definition (abfd, isym);
3350 break;
3351 }
3352 }
3353
3354 free (isymbuf);
3355
3356 return result;
3357 }
3358
3359 /* Add an entry to the .dynamic table. */
3361
3362 bfd_boolean
3363 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3364 bfd_vma tag,
3365 bfd_vma val)
3366 {
3367 struct elf_link_hash_table *hash_table;
3368 const struct elf_backend_data *bed;
3369 asection *s;
3370 bfd_size_type newsize;
3371 bfd_byte *newcontents;
3372 Elf_Internal_Dyn dyn;
3373
3374 hash_table = elf_hash_table (info);
3375 if (! is_elf_hash_table (hash_table))
3376 return FALSE;
3377
3378 bed = get_elf_backend_data (hash_table->dynobj);
3379 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3380 BFD_ASSERT (s != NULL);
3381
3382 newsize = s->size + bed->s->sizeof_dyn;
3383 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3384 if (newcontents == NULL)
3385 return FALSE;
3386
3387 dyn.d_tag = tag;
3388 dyn.d_un.d_val = val;
3389 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3390
3391 s->size = newsize;
3392 s->contents = newcontents;
3393
3394 return TRUE;
3395 }
3396
3397 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3398 otherwise just check whether one already exists. Returns -1 on error,
3399 1 if a DT_NEEDED tag already exists, and 0 on success. */
3400
3401 static int
3402 elf_add_dt_needed_tag (bfd *abfd,
3403 struct bfd_link_info *info,
3404 const char *soname,
3405 bfd_boolean do_it)
3406 {
3407 struct elf_link_hash_table *hash_table;
3408 size_t strindex;
3409
3410 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3411 return -1;
3412
3413 hash_table = elf_hash_table (info);
3414 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3415 if (strindex == (size_t) -1)
3416 return -1;
3417
3418 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3419 {
3420 asection *sdyn;
3421 const struct elf_backend_data *bed;
3422 bfd_byte *extdyn;
3423
3424 bed = get_elf_backend_data (hash_table->dynobj);
3425 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3426 if (sdyn != NULL)
3427 for (extdyn = sdyn->contents;
3428 extdyn < sdyn->contents + sdyn->size;
3429 extdyn += bed->s->sizeof_dyn)
3430 {
3431 Elf_Internal_Dyn dyn;
3432
3433 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3434 if (dyn.d_tag == DT_NEEDED
3435 && dyn.d_un.d_val == strindex)
3436 {
3437 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3438 return 1;
3439 }
3440 }
3441 }
3442
3443 if (do_it)
3444 {
3445 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3446 return -1;
3447
3448 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3449 return -1;
3450 }
3451 else
3452 /* We were just checking for existence of the tag. */
3453 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3454
3455 return 0;
3456 }
3457
3458 /* Return true if SONAME is on the needed list between NEEDED and STOP
3459 (or the end of list if STOP is NULL), and needed by a library that
3460 will be loaded. */
3461
3462 static bfd_boolean
3463 on_needed_list (const char *soname,
3464 struct bfd_link_needed_list *needed,
3465 struct bfd_link_needed_list *stop)
3466 {
3467 struct bfd_link_needed_list *look;
3468 for (look = needed; look != stop; look = look->next)
3469 if (strcmp (soname, look->name) == 0
3470 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3471 /* If needed by a library that itself is not directly
3472 needed, recursively check whether that library is
3473 indirectly needed. Since we add DT_NEEDED entries to
3474 the end of the list, library dependencies appear after
3475 the library. Therefore search prior to the current
3476 LOOK, preventing possible infinite recursion. */
3477 || on_needed_list (elf_dt_name (look->by), needed, look)))
3478 return TRUE;
3479
3480 return FALSE;
3481 }
3482
3483 /* Sort symbol by value, section, and size. */
3484 static int
3485 elf_sort_symbol (const void *arg1, const void *arg2)
3486 {
3487 const struct elf_link_hash_entry *h1;
3488 const struct elf_link_hash_entry *h2;
3489 bfd_signed_vma vdiff;
3490
3491 h1 = *(const struct elf_link_hash_entry **) arg1;
3492 h2 = *(const struct elf_link_hash_entry **) arg2;
3493 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3494 if (vdiff != 0)
3495 return vdiff > 0 ? 1 : -1;
3496 else
3497 {
3498 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3499 if (sdiff != 0)
3500 return sdiff > 0 ? 1 : -1;
3501 }
3502 vdiff = h1->size - h2->size;
3503 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3504 }
3505
3506 /* This function is used to adjust offsets into .dynstr for
3507 dynamic symbols. This is called via elf_link_hash_traverse. */
3508
3509 static bfd_boolean
3510 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3511 {
3512 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3513
3514 if (h->dynindx != -1)
3515 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3516 return TRUE;
3517 }
3518
3519 /* Assign string offsets in .dynstr, update all structures referencing
3520 them. */
3521
3522 static bfd_boolean
3523 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3524 {
3525 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3526 struct elf_link_local_dynamic_entry *entry;
3527 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3528 bfd *dynobj = hash_table->dynobj;
3529 asection *sdyn;
3530 bfd_size_type size;
3531 const struct elf_backend_data *bed;
3532 bfd_byte *extdyn;
3533
3534 _bfd_elf_strtab_finalize (dynstr);
3535 size = _bfd_elf_strtab_size (dynstr);
3536
3537 bed = get_elf_backend_data (dynobj);
3538 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3539 BFD_ASSERT (sdyn != NULL);
3540
3541 /* Update all .dynamic entries referencing .dynstr strings. */
3542 for (extdyn = sdyn->contents;
3543 extdyn < sdyn->contents + sdyn->size;
3544 extdyn += bed->s->sizeof_dyn)
3545 {
3546 Elf_Internal_Dyn dyn;
3547
3548 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3549 switch (dyn.d_tag)
3550 {
3551 case DT_STRSZ:
3552 dyn.d_un.d_val = size;
3553 break;
3554 case DT_NEEDED:
3555 case DT_SONAME:
3556 case DT_RPATH:
3557 case DT_RUNPATH:
3558 case DT_FILTER:
3559 case DT_AUXILIARY:
3560 case DT_AUDIT:
3561 case DT_DEPAUDIT:
3562 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3563 break;
3564 default:
3565 continue;
3566 }
3567 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3568 }
3569
3570 /* Now update local dynamic symbols. */
3571 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3572 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3573 entry->isym.st_name);
3574
3575 /* And the rest of dynamic symbols. */
3576 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3577
3578 /* Adjust version definitions. */
3579 if (elf_tdata (output_bfd)->cverdefs)
3580 {
3581 asection *s;
3582 bfd_byte *p;
3583 size_t i;
3584 Elf_Internal_Verdef def;
3585 Elf_Internal_Verdaux defaux;
3586
3587 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3588 p = s->contents;
3589 do
3590 {
3591 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3592 &def);
3593 p += sizeof (Elf_External_Verdef);
3594 if (def.vd_aux != sizeof (Elf_External_Verdef))
3595 continue;
3596 for (i = 0; i < def.vd_cnt; ++i)
3597 {
3598 _bfd_elf_swap_verdaux_in (output_bfd,
3599 (Elf_External_Verdaux *) p, &defaux);
3600 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3601 defaux.vda_name);
3602 _bfd_elf_swap_verdaux_out (output_bfd,
3603 &defaux, (Elf_External_Verdaux *) p);
3604 p += sizeof (Elf_External_Verdaux);
3605 }
3606 }
3607 while (def.vd_next);
3608 }
3609
3610 /* Adjust version references. */
3611 if (elf_tdata (output_bfd)->verref)
3612 {
3613 asection *s;
3614 bfd_byte *p;
3615 size_t i;
3616 Elf_Internal_Verneed need;
3617 Elf_Internal_Vernaux needaux;
3618
3619 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3620 p = s->contents;
3621 do
3622 {
3623 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3624 &need);
3625 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3626 _bfd_elf_swap_verneed_out (output_bfd, &need,
3627 (Elf_External_Verneed *) p);
3628 p += sizeof (Elf_External_Verneed);
3629 for (i = 0; i < need.vn_cnt; ++i)
3630 {
3631 _bfd_elf_swap_vernaux_in (output_bfd,
3632 (Elf_External_Vernaux *) p, &needaux);
3633 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3634 needaux.vna_name);
3635 _bfd_elf_swap_vernaux_out (output_bfd,
3636 &needaux,
3637 (Elf_External_Vernaux *) p);
3638 p += sizeof (Elf_External_Vernaux);
3639 }
3640 }
3641 while (need.vn_next);
3642 }
3643
3644 return TRUE;
3645 }
3646
3647 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3649 The default is to only match when the INPUT and OUTPUT are exactly
3650 the same target. */
3651
3652 bfd_boolean
3653 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3654 const bfd_target *output)
3655 {
3656 return input == output;
3657 }
3658
3659 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3660 This version is used when different targets for the same architecture
3661 are virtually identical. */
3662
3663 bfd_boolean
3664 _bfd_elf_relocs_compatible (const bfd_target *input,
3665 const bfd_target *output)
3666 {
3667 const struct elf_backend_data *obed, *ibed;
3668
3669 if (input == output)
3670 return TRUE;
3671
3672 ibed = xvec_get_elf_backend_data (input);
3673 obed = xvec_get_elf_backend_data (output);
3674
3675 if (ibed->arch != obed->arch)
3676 return FALSE;
3677
3678 /* If both backends are using this function, deem them compatible. */
3679 return ibed->relocs_compatible == obed->relocs_compatible;
3680 }
3681
3682 /* Make a special call to the linker "notice" function to tell it that
3683 we are about to handle an as-needed lib, or have finished
3684 processing the lib. */
3685
3686 bfd_boolean
3687 _bfd_elf_notice_as_needed (bfd *ibfd,
3688 struct bfd_link_info *info,
3689 enum notice_asneeded_action act)
3690 {
3691 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3692 }
3693
3694 /* Check relocations an ELF object file. */
3695
3696 bfd_boolean
3697 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3698 {
3699 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3700 struct elf_link_hash_table *htab = elf_hash_table (info);
3701
3702 /* If this object is the same format as the output object, and it is
3703 not a shared library, then let the backend look through the
3704 relocs.
3705
3706 This is required to build global offset table entries and to
3707 arrange for dynamic relocs. It is not required for the
3708 particular common case of linking non PIC code, even when linking
3709 against shared libraries, but unfortunately there is no way of
3710 knowing whether an object file has been compiled PIC or not.
3711 Looking through the relocs is not particularly time consuming.
3712 The problem is that we must either (1) keep the relocs in memory,
3713 which causes the linker to require additional runtime memory or
3714 (2) read the relocs twice from the input file, which wastes time.
3715 This would be a good case for using mmap.
3716
3717 I have no idea how to handle linking PIC code into a file of a
3718 different format. It probably can't be done. */
3719 if ((abfd->flags & DYNAMIC) == 0
3720 && is_elf_hash_table (htab)
3721 && bed->check_relocs != NULL
3722 && elf_object_id (abfd) == elf_hash_table_id (htab)
3723 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3724 {
3725 asection *o;
3726
3727 for (o = abfd->sections; o != NULL; o = o->next)
3728 {
3729 Elf_Internal_Rela *internal_relocs;
3730 bfd_boolean ok;
3731
3732 /* Don't check relocations in excluded sections. */
3733 if ((o->flags & SEC_RELOC) == 0
3734 || (o->flags & SEC_EXCLUDE) != 0
3735 || o->reloc_count == 0
3736 || ((info->strip == strip_all || info->strip == strip_debugger)
3737 && (o->flags & SEC_DEBUGGING) != 0)
3738 || bfd_is_abs_section (o->output_section))
3739 continue;
3740
3741 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3742 info->keep_memory);
3743 if (internal_relocs == NULL)
3744 return FALSE;
3745
3746 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3747
3748 if (elf_section_data (o)->relocs != internal_relocs)
3749 free (internal_relocs);
3750
3751 if (! ok)
3752 return FALSE;
3753 }
3754 }
3755
3756 return TRUE;
3757 }
3758
3759 /* Add symbols from an ELF object file to the linker hash table. */
3760
3761 static bfd_boolean
3762 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3763 {
3764 Elf_Internal_Ehdr *ehdr;
3765 Elf_Internal_Shdr *hdr;
3766 size_t symcount;
3767 size_t extsymcount;
3768 size_t extsymoff;
3769 struct elf_link_hash_entry **sym_hash;
3770 bfd_boolean dynamic;
3771 Elf_External_Versym *extversym = NULL;
3772 Elf_External_Versym *ever;
3773 struct elf_link_hash_entry *weaks;
3774 struct elf_link_hash_entry **nondeflt_vers = NULL;
3775 size_t nondeflt_vers_cnt = 0;
3776 Elf_Internal_Sym *isymbuf = NULL;
3777 Elf_Internal_Sym *isym;
3778 Elf_Internal_Sym *isymend;
3779 const struct elf_backend_data *bed;
3780 bfd_boolean add_needed;
3781 struct elf_link_hash_table *htab;
3782 bfd_size_type amt;
3783 void *alloc_mark = NULL;
3784 struct bfd_hash_entry **old_table = NULL;
3785 unsigned int old_size = 0;
3786 unsigned int old_count = 0;
3787 void *old_tab = NULL;
3788 void *old_ent;
3789 struct bfd_link_hash_entry *old_undefs = NULL;
3790 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3791 void *old_strtab = NULL;
3792 size_t tabsize = 0;
3793 asection *s;
3794 bfd_boolean just_syms;
3795
3796 htab = elf_hash_table (info);
3797 bed = get_elf_backend_data (abfd);
3798
3799 if ((abfd->flags & DYNAMIC) == 0)
3800 dynamic = FALSE;
3801 else
3802 {
3803 dynamic = TRUE;
3804
3805 /* You can't use -r against a dynamic object. Also, there's no
3806 hope of using a dynamic object which does not exactly match
3807 the format of the output file. */
3808 if (bfd_link_relocatable (info)
3809 || !is_elf_hash_table (htab)
3810 || info->output_bfd->xvec != abfd->xvec)
3811 {
3812 if (bfd_link_relocatable (info))
3813 bfd_set_error (bfd_error_invalid_operation);
3814 else
3815 bfd_set_error (bfd_error_wrong_format);
3816 goto error_return;
3817 }
3818 }
3819
3820 ehdr = elf_elfheader (abfd);
3821 if (info->warn_alternate_em
3822 && bed->elf_machine_code != ehdr->e_machine
3823 && ((bed->elf_machine_alt1 != 0
3824 && ehdr->e_machine == bed->elf_machine_alt1)
3825 || (bed->elf_machine_alt2 != 0
3826 && ehdr->e_machine == bed->elf_machine_alt2)))
3827 info->callbacks->einfo
3828 /* xgettext:c-format */
3829 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3830 ehdr->e_machine, abfd, bed->elf_machine_code);
3831
3832 /* As a GNU extension, any input sections which are named
3833 .gnu.warning.SYMBOL are treated as warning symbols for the given
3834 symbol. This differs from .gnu.warning sections, which generate
3835 warnings when they are included in an output file. */
3836 /* PR 12761: Also generate this warning when building shared libraries. */
3837 for (s = abfd->sections; s != NULL; s = s->next)
3838 {
3839 const char *name;
3840
3841 name = bfd_get_section_name (abfd, s);
3842 if (CONST_STRNEQ (name, ".gnu.warning."))
3843 {
3844 char *msg;
3845 bfd_size_type sz;
3846
3847 name += sizeof ".gnu.warning." - 1;
3848
3849 /* If this is a shared object, then look up the symbol
3850 in the hash table. If it is there, and it is already
3851 been defined, then we will not be using the entry
3852 from this shared object, so we don't need to warn.
3853 FIXME: If we see the definition in a regular object
3854 later on, we will warn, but we shouldn't. The only
3855 fix is to keep track of what warnings we are supposed
3856 to emit, and then handle them all at the end of the
3857 link. */
3858 if (dynamic)
3859 {
3860 struct elf_link_hash_entry *h;
3861
3862 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3863
3864 /* FIXME: What about bfd_link_hash_common? */
3865 if (h != NULL
3866 && (h->root.type == bfd_link_hash_defined
3867 || h->root.type == bfd_link_hash_defweak))
3868 continue;
3869 }
3870
3871 sz = s->size;
3872 msg = (char *) bfd_alloc (abfd, sz + 1);
3873 if (msg == NULL)
3874 goto error_return;
3875
3876 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3877 goto error_return;
3878
3879 msg[sz] = '\0';
3880
3881 if (! (_bfd_generic_link_add_one_symbol
3882 (info, abfd, name, BSF_WARNING, s, 0, msg,
3883 FALSE, bed->collect, NULL)))
3884 goto error_return;
3885
3886 if (bfd_link_executable (info))
3887 {
3888 /* Clobber the section size so that the warning does
3889 not get copied into the output file. */
3890 s->size = 0;
3891
3892 /* Also set SEC_EXCLUDE, so that symbols defined in
3893 the warning section don't get copied to the output. */
3894 s->flags |= SEC_EXCLUDE;
3895 }
3896 }
3897 }
3898
3899 just_syms = ((s = abfd->sections) != NULL
3900 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3901
3902 add_needed = TRUE;
3903 if (! dynamic)
3904 {
3905 /* If we are creating a shared library, create all the dynamic
3906 sections immediately. We need to attach them to something,
3907 so we attach them to this BFD, provided it is the right
3908 format and is not from ld --just-symbols. Always create the
3909 dynamic sections for -E/--dynamic-list. FIXME: If there
3910 are no input BFD's of the same format as the output, we can't
3911 make a shared library. */
3912 if (!just_syms
3913 && (bfd_link_pic (info)
3914 || (!bfd_link_relocatable (info)
3915 && info->nointerp
3916 && (info->export_dynamic || info->dynamic)))
3917 && is_elf_hash_table (htab)
3918 && info->output_bfd->xvec == abfd->xvec
3919 && !htab->dynamic_sections_created)
3920 {
3921 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3922 goto error_return;
3923 }
3924 }
3925 else if (!is_elf_hash_table (htab))
3926 goto error_return;
3927 else
3928 {
3929 const char *soname = NULL;
3930 char *audit = NULL;
3931 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3932 const Elf_Internal_Phdr *phdr;
3933 int ret;
3934
3935 /* ld --just-symbols and dynamic objects don't mix very well.
3936 ld shouldn't allow it. */
3937 if (just_syms)
3938 abort ();
3939
3940 /* If this dynamic lib was specified on the command line with
3941 --as-needed in effect, then we don't want to add a DT_NEEDED
3942 tag unless the lib is actually used. Similary for libs brought
3943 in by another lib's DT_NEEDED. When --no-add-needed is used
3944 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3945 any dynamic library in DT_NEEDED tags in the dynamic lib at
3946 all. */
3947 add_needed = (elf_dyn_lib_class (abfd)
3948 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3949 | DYN_NO_NEEDED)) == 0;
3950
3951 s = bfd_get_section_by_name (abfd, ".dynamic");
3952 if (s != NULL)
3953 {
3954 bfd_byte *dynbuf;
3955 bfd_byte *extdyn;
3956 unsigned int elfsec;
3957 unsigned long shlink;
3958
3959 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3960 {
3961 error_free_dyn:
3962 free (dynbuf);
3963 goto error_return;
3964 }
3965
3966 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3967 if (elfsec == SHN_BAD)
3968 goto error_free_dyn;
3969 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3970
3971 for (extdyn = dynbuf;
3972 extdyn < dynbuf + s->size;
3973 extdyn += bed->s->sizeof_dyn)
3974 {
3975 Elf_Internal_Dyn dyn;
3976
3977 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3978 if (dyn.d_tag == DT_SONAME)
3979 {
3980 unsigned int tagv = dyn.d_un.d_val;
3981 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3982 if (soname == NULL)
3983 goto error_free_dyn;
3984 }
3985 if (dyn.d_tag == DT_NEEDED)
3986 {
3987 struct bfd_link_needed_list *n, **pn;
3988 char *fnm, *anm;
3989 unsigned int tagv = dyn.d_un.d_val;
3990
3991 amt = sizeof (struct bfd_link_needed_list);
3992 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3993 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3994 if (n == NULL || fnm == NULL)
3995 goto error_free_dyn;
3996 amt = strlen (fnm) + 1;
3997 anm = (char *) bfd_alloc (abfd, amt);
3998 if (anm == NULL)
3999 goto error_free_dyn;
4000 memcpy (anm, fnm, amt);
4001 n->name = anm;
4002 n->by = abfd;
4003 n->next = NULL;
4004 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4005 ;
4006 *pn = n;
4007 }
4008 if (dyn.d_tag == DT_RUNPATH)
4009 {
4010 struct bfd_link_needed_list *n, **pn;
4011 char *fnm, *anm;
4012 unsigned int tagv = dyn.d_un.d_val;
4013
4014 amt = sizeof (struct bfd_link_needed_list);
4015 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4016 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4017 if (n == NULL || fnm == NULL)
4018 goto error_free_dyn;
4019 amt = strlen (fnm) + 1;
4020 anm = (char *) bfd_alloc (abfd, amt);
4021 if (anm == NULL)
4022 goto error_free_dyn;
4023 memcpy (anm, fnm, amt);
4024 n->name = anm;
4025 n->by = abfd;
4026 n->next = NULL;
4027 for (pn = & runpath;
4028 *pn != NULL;
4029 pn = &(*pn)->next)
4030 ;
4031 *pn = n;
4032 }
4033 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4034 if (!runpath && dyn.d_tag == DT_RPATH)
4035 {
4036 struct bfd_link_needed_list *n, **pn;
4037 char *fnm, *anm;
4038 unsigned int tagv = dyn.d_un.d_val;
4039
4040 amt = sizeof (struct bfd_link_needed_list);
4041 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4042 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4043 if (n == NULL || fnm == NULL)
4044 goto error_free_dyn;
4045 amt = strlen (fnm) + 1;
4046 anm = (char *) bfd_alloc (abfd, amt);
4047 if (anm == NULL)
4048 goto error_free_dyn;
4049 memcpy (anm, fnm, amt);
4050 n->name = anm;
4051 n->by = abfd;
4052 n->next = NULL;
4053 for (pn = & rpath;
4054 *pn != NULL;
4055 pn = &(*pn)->next)
4056 ;
4057 *pn = n;
4058 }
4059 if (dyn.d_tag == DT_AUDIT)
4060 {
4061 unsigned int tagv = dyn.d_un.d_val;
4062 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4063 }
4064 }
4065
4066 free (dynbuf);
4067 }
4068
4069 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4070 frees all more recently bfd_alloc'd blocks as well. */
4071 if (runpath)
4072 rpath = runpath;
4073
4074 if (rpath)
4075 {
4076 struct bfd_link_needed_list **pn;
4077 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4078 ;
4079 *pn = rpath;
4080 }
4081
4082 /* If we have a PT_GNU_RELRO program header, mark as read-only
4083 all sections contained fully therein. This makes relro
4084 shared library sections appear as they will at run-time. */
4085 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4086 while (--phdr >= elf_tdata (abfd)->phdr)
4087 if (phdr->p_type == PT_GNU_RELRO)
4088 {
4089 for (s = abfd->sections; s != NULL; s = s->next)
4090 if ((s->flags & SEC_ALLOC) != 0
4091 && s->vma >= phdr->p_vaddr
4092 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4093 s->flags |= SEC_READONLY;
4094 break;
4095 }
4096
4097 /* We do not want to include any of the sections in a dynamic
4098 object in the output file. We hack by simply clobbering the
4099 list of sections in the BFD. This could be handled more
4100 cleanly by, say, a new section flag; the existing
4101 SEC_NEVER_LOAD flag is not the one we want, because that one
4102 still implies that the section takes up space in the output
4103 file. */
4104 bfd_section_list_clear (abfd);
4105
4106 /* Find the name to use in a DT_NEEDED entry that refers to this
4107 object. If the object has a DT_SONAME entry, we use it.
4108 Otherwise, if the generic linker stuck something in
4109 elf_dt_name, we use that. Otherwise, we just use the file
4110 name. */
4111 if (soname == NULL || *soname == '\0')
4112 {
4113 soname = elf_dt_name (abfd);
4114 if (soname == NULL || *soname == '\0')
4115 soname = bfd_get_filename (abfd);
4116 }
4117
4118 /* Save the SONAME because sometimes the linker emulation code
4119 will need to know it. */
4120 elf_dt_name (abfd) = soname;
4121
4122 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4123 if (ret < 0)
4124 goto error_return;
4125
4126 /* If we have already included this dynamic object in the
4127 link, just ignore it. There is no reason to include a
4128 particular dynamic object more than once. */
4129 if (ret > 0)
4130 return TRUE;
4131
4132 /* Save the DT_AUDIT entry for the linker emulation code. */
4133 elf_dt_audit (abfd) = audit;
4134 }
4135
4136 /* If this is a dynamic object, we always link against the .dynsym
4137 symbol table, not the .symtab symbol table. The dynamic linker
4138 will only see the .dynsym symbol table, so there is no reason to
4139 look at .symtab for a dynamic object. */
4140
4141 if (! dynamic || elf_dynsymtab (abfd) == 0)
4142 hdr = &elf_tdata (abfd)->symtab_hdr;
4143 else
4144 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4145
4146 symcount = hdr->sh_size / bed->s->sizeof_sym;
4147
4148 /* The sh_info field of the symtab header tells us where the
4149 external symbols start. We don't care about the local symbols at
4150 this point. */
4151 if (elf_bad_symtab (abfd))
4152 {
4153 extsymcount = symcount;
4154 extsymoff = 0;
4155 }
4156 else
4157 {
4158 extsymcount = symcount - hdr->sh_info;
4159 extsymoff = hdr->sh_info;
4160 }
4161
4162 sym_hash = elf_sym_hashes (abfd);
4163 if (extsymcount != 0)
4164 {
4165 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4166 NULL, NULL, NULL);
4167 if (isymbuf == NULL)
4168 goto error_return;
4169
4170 if (sym_hash == NULL)
4171 {
4172 /* We store a pointer to the hash table entry for each
4173 external symbol. */
4174 amt = extsymcount;
4175 amt *= sizeof (struct elf_link_hash_entry *);
4176 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4177 if (sym_hash == NULL)
4178 goto error_free_sym;
4179 elf_sym_hashes (abfd) = sym_hash;
4180 }
4181 }
4182
4183 if (dynamic)
4184 {
4185 /* Read in any version definitions. */
4186 if (!_bfd_elf_slurp_version_tables (abfd,
4187 info->default_imported_symver))
4188 goto error_free_sym;
4189
4190 /* Read in the symbol versions, but don't bother to convert them
4191 to internal format. */
4192 if (elf_dynversym (abfd) != 0)
4193 {
4194 Elf_Internal_Shdr *versymhdr;
4195
4196 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4197 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4198 if (extversym == NULL)
4199 goto error_free_sym;
4200 amt = versymhdr->sh_size;
4201 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4202 || bfd_bread (extversym, amt, abfd) != amt)
4203 goto error_free_vers;
4204 }
4205 }
4206
4207 /* If we are loading an as-needed shared lib, save the symbol table
4208 state before we start adding symbols. If the lib turns out
4209 to be unneeded, restore the state. */
4210 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4211 {
4212 unsigned int i;
4213 size_t entsize;
4214
4215 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4216 {
4217 struct bfd_hash_entry *p;
4218 struct elf_link_hash_entry *h;
4219
4220 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4221 {
4222 h = (struct elf_link_hash_entry *) p;
4223 entsize += htab->root.table.entsize;
4224 if (h->root.type == bfd_link_hash_warning)
4225 entsize += htab->root.table.entsize;
4226 }
4227 }
4228
4229 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4230 old_tab = bfd_malloc (tabsize + entsize);
4231 if (old_tab == NULL)
4232 goto error_free_vers;
4233
4234 /* Remember the current objalloc pointer, so that all mem for
4235 symbols added can later be reclaimed. */
4236 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4237 if (alloc_mark == NULL)
4238 goto error_free_vers;
4239
4240 /* Make a special call to the linker "notice" function to
4241 tell it that we are about to handle an as-needed lib. */
4242 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4243 goto error_free_vers;
4244
4245 /* Clone the symbol table. Remember some pointers into the
4246 symbol table, and dynamic symbol count. */
4247 old_ent = (char *) old_tab + tabsize;
4248 memcpy (old_tab, htab->root.table.table, tabsize);
4249 old_undefs = htab->root.undefs;
4250 old_undefs_tail = htab->root.undefs_tail;
4251 old_table = htab->root.table.table;
4252 old_size = htab->root.table.size;
4253 old_count = htab->root.table.count;
4254 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4255 if (old_strtab == NULL)
4256 goto error_free_vers;
4257
4258 for (i = 0; i < htab->root.table.size; i++)
4259 {
4260 struct bfd_hash_entry *p;
4261 struct elf_link_hash_entry *h;
4262
4263 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4264 {
4265 memcpy (old_ent, p, htab->root.table.entsize);
4266 old_ent = (char *) old_ent + htab->root.table.entsize;
4267 h = (struct elf_link_hash_entry *) p;
4268 if (h->root.type == bfd_link_hash_warning)
4269 {
4270 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4271 old_ent = (char *) old_ent + htab->root.table.entsize;
4272 }
4273 }
4274 }
4275 }
4276
4277 weaks = NULL;
4278 ever = extversym != NULL ? extversym + extsymoff : NULL;
4279 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4280 isym < isymend;
4281 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4282 {
4283 int bind;
4284 bfd_vma value;
4285 asection *sec, *new_sec;
4286 flagword flags;
4287 const char *name;
4288 struct elf_link_hash_entry *h;
4289 struct elf_link_hash_entry *hi;
4290 bfd_boolean definition;
4291 bfd_boolean size_change_ok;
4292 bfd_boolean type_change_ok;
4293 bfd_boolean new_weak;
4294 bfd_boolean old_weak;
4295 bfd_boolean override;
4296 bfd_boolean common;
4297 bfd_boolean discarded;
4298 unsigned int old_alignment;
4299 bfd *old_bfd;
4300 bfd_boolean matched;
4301
4302 override = FALSE;
4303
4304 flags = BSF_NO_FLAGS;
4305 sec = NULL;
4306 value = isym->st_value;
4307 common = bed->common_definition (isym);
4308 if (common && info->inhibit_common_definition)
4309 {
4310 /* Treat common symbol as undefined for --no-define-common. */
4311 isym->st_shndx = SHN_UNDEF;
4312 common = FALSE;
4313 }
4314 discarded = FALSE;
4315
4316 bind = ELF_ST_BIND (isym->st_info);
4317 switch (bind)
4318 {
4319 case STB_LOCAL:
4320 /* This should be impossible, since ELF requires that all
4321 global symbols follow all local symbols, and that sh_info
4322 point to the first global symbol. Unfortunately, Irix 5
4323 screws this up. */
4324 continue;
4325
4326 case STB_GLOBAL:
4327 if (isym->st_shndx != SHN_UNDEF && !common)
4328 flags = BSF_GLOBAL;
4329 break;
4330
4331 case STB_WEAK:
4332 flags = BSF_WEAK;
4333 break;
4334
4335 case STB_GNU_UNIQUE:
4336 flags = BSF_GNU_UNIQUE;
4337 break;
4338
4339 default:
4340 /* Leave it up to the processor backend. */
4341 break;
4342 }
4343
4344 if (isym->st_shndx == SHN_UNDEF)
4345 sec = bfd_und_section_ptr;
4346 else if (isym->st_shndx == SHN_ABS)
4347 sec = bfd_abs_section_ptr;
4348 else if (isym->st_shndx == SHN_COMMON)
4349 {
4350 sec = bfd_com_section_ptr;
4351 /* What ELF calls the size we call the value. What ELF
4352 calls the value we call the alignment. */
4353 value = isym->st_size;
4354 }
4355 else
4356 {
4357 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4358 if (sec == NULL)
4359 sec = bfd_abs_section_ptr;
4360 else if (discarded_section (sec))
4361 {
4362 /* Symbols from discarded section are undefined. We keep
4363 its visibility. */
4364 sec = bfd_und_section_ptr;
4365 discarded = TRUE;
4366 isym->st_shndx = SHN_UNDEF;
4367 }
4368 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4369 value -= sec->vma;
4370 }
4371
4372 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4373 isym->st_name);
4374 if (name == NULL)
4375 goto error_free_vers;
4376
4377 if (isym->st_shndx == SHN_COMMON
4378 && (abfd->flags & BFD_PLUGIN) != 0)
4379 {
4380 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4381
4382 if (xc == NULL)
4383 {
4384 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4385 | SEC_EXCLUDE);
4386 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4387 if (xc == NULL)
4388 goto error_free_vers;
4389 }
4390 sec = xc;
4391 }
4392 else if (isym->st_shndx == SHN_COMMON
4393 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4394 && !bfd_link_relocatable (info))
4395 {
4396 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4397
4398 if (tcomm == NULL)
4399 {
4400 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4401 | SEC_LINKER_CREATED);
4402 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4403 if (tcomm == NULL)
4404 goto error_free_vers;
4405 }
4406 sec = tcomm;
4407 }
4408 else if (bed->elf_add_symbol_hook)
4409 {
4410 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4411 &sec, &value))
4412 goto error_free_vers;
4413
4414 /* The hook function sets the name to NULL if this symbol
4415 should be skipped for some reason. */
4416 if (name == NULL)
4417 continue;
4418 }
4419
4420 /* Sanity check that all possibilities were handled. */
4421 if (sec == NULL)
4422 {
4423 bfd_set_error (bfd_error_bad_value);
4424 goto error_free_vers;
4425 }
4426
4427 /* Silently discard TLS symbols from --just-syms. There's
4428 no way to combine a static TLS block with a new TLS block
4429 for this executable. */
4430 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4431 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4432 continue;
4433
4434 if (bfd_is_und_section (sec)
4435 || bfd_is_com_section (sec))
4436 definition = FALSE;
4437 else
4438 definition = TRUE;
4439
4440 size_change_ok = FALSE;
4441 type_change_ok = bed->type_change_ok;
4442 old_weak = FALSE;
4443 matched = FALSE;
4444 old_alignment = 0;
4445 old_bfd = NULL;
4446 new_sec = sec;
4447
4448 if (is_elf_hash_table (htab))
4449 {
4450 Elf_Internal_Versym iver;
4451 unsigned int vernum = 0;
4452 bfd_boolean skip;
4453
4454 if (ever == NULL)
4455 {
4456 if (info->default_imported_symver)
4457 /* Use the default symbol version created earlier. */
4458 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4459 else
4460 iver.vs_vers = 0;
4461 }
4462 else
4463 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4464
4465 vernum = iver.vs_vers & VERSYM_VERSION;
4466
4467 /* If this is a hidden symbol, or if it is not version
4468 1, we append the version name to the symbol name.
4469 However, we do not modify a non-hidden absolute symbol
4470 if it is not a function, because it might be the version
4471 symbol itself. FIXME: What if it isn't? */
4472 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4473 || (vernum > 1
4474 && (!bfd_is_abs_section (sec)
4475 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4476 {
4477 const char *verstr;
4478 size_t namelen, verlen, newlen;
4479 char *newname, *p;
4480
4481 if (isym->st_shndx != SHN_UNDEF)
4482 {
4483 if (vernum > elf_tdata (abfd)->cverdefs)
4484 verstr = NULL;
4485 else if (vernum > 1)
4486 verstr =
4487 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4488 else
4489 verstr = "";
4490
4491 if (verstr == NULL)
4492 {
4493 _bfd_error_handler
4494 /* xgettext:c-format */
4495 (_("%B: %s: invalid version %u (max %d)"),
4496 abfd, name, vernum,
4497 elf_tdata (abfd)->cverdefs);
4498 bfd_set_error (bfd_error_bad_value);
4499 goto error_free_vers;
4500 }
4501 }
4502 else
4503 {
4504 /* We cannot simply test for the number of
4505 entries in the VERNEED section since the
4506 numbers for the needed versions do not start
4507 at 0. */
4508 Elf_Internal_Verneed *t;
4509
4510 verstr = NULL;
4511 for (t = elf_tdata (abfd)->verref;
4512 t != NULL;
4513 t = t->vn_nextref)
4514 {
4515 Elf_Internal_Vernaux *a;
4516
4517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4518 {
4519 if (a->vna_other == vernum)
4520 {
4521 verstr = a->vna_nodename;
4522 break;
4523 }
4524 }
4525 if (a != NULL)
4526 break;
4527 }
4528 if (verstr == NULL)
4529 {
4530 _bfd_error_handler
4531 /* xgettext:c-format */
4532 (_("%B: %s: invalid needed version %d"),
4533 abfd, name, vernum);
4534 bfd_set_error (bfd_error_bad_value);
4535 goto error_free_vers;
4536 }
4537 }
4538
4539 namelen = strlen (name);
4540 verlen = strlen (verstr);
4541 newlen = namelen + verlen + 2;
4542 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4543 && isym->st_shndx != SHN_UNDEF)
4544 ++newlen;
4545
4546 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4547 if (newname == NULL)
4548 goto error_free_vers;
4549 memcpy (newname, name, namelen);
4550 p = newname + namelen;
4551 *p++ = ELF_VER_CHR;
4552 /* If this is a defined non-hidden version symbol,
4553 we add another @ to the name. This indicates the
4554 default version of the symbol. */
4555 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4556 && isym->st_shndx != SHN_UNDEF)
4557 *p++ = ELF_VER_CHR;
4558 memcpy (p, verstr, verlen + 1);
4559
4560 name = newname;
4561 }
4562
4563 /* If this symbol has default visibility and the user has
4564 requested we not re-export it, then mark it as hidden. */
4565 if (!bfd_is_und_section (sec)
4566 && !dynamic
4567 && abfd->no_export
4568 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4569 isym->st_other = (STV_HIDDEN
4570 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4571
4572 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4573 sym_hash, &old_bfd, &old_weak,
4574 &old_alignment, &skip, &override,
4575 &type_change_ok, &size_change_ok,
4576 &matched))
4577 goto error_free_vers;
4578
4579 if (skip)
4580 continue;
4581
4582 /* Override a definition only if the new symbol matches the
4583 existing one. */
4584 if (override && matched)
4585 definition = FALSE;
4586
4587 h = *sym_hash;
4588 while (h->root.type == bfd_link_hash_indirect
4589 || h->root.type == bfd_link_hash_warning)
4590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4591
4592 if (elf_tdata (abfd)->verdef != NULL
4593 && vernum > 1
4594 && definition)
4595 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4596 }
4597
4598 if (! (_bfd_generic_link_add_one_symbol
4599 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4600 (struct bfd_link_hash_entry **) sym_hash)))
4601 goto error_free_vers;
4602
4603 if ((flags & BSF_GNU_UNIQUE)
4604 && (abfd->flags & DYNAMIC) == 0
4605 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4606 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4607
4608 h = *sym_hash;
4609 /* We need to make sure that indirect symbol dynamic flags are
4610 updated. */
4611 hi = h;
4612 while (h->root.type == bfd_link_hash_indirect
4613 || h->root.type == bfd_link_hash_warning)
4614 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4615
4616 /* Setting the index to -3 tells elf_link_output_extsym that
4617 this symbol is defined in a discarded section. */
4618 if (discarded)
4619 h->indx = -3;
4620
4621 *sym_hash = h;
4622
4623 new_weak = (flags & BSF_WEAK) != 0;
4624 if (dynamic
4625 && definition
4626 && new_weak
4627 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4628 && is_elf_hash_table (htab)
4629 && h->u.alias == NULL)
4630 {
4631 /* Keep a list of all weak defined non function symbols from
4632 a dynamic object, using the alias field. Later in this
4633 function we will set the alias field to the correct
4634 value. We only put non-function symbols from dynamic
4635 objects on this list, because that happens to be the only
4636 time we need to know the normal symbol corresponding to a
4637 weak symbol, and the information is time consuming to
4638 figure out. If the alias field is not already NULL,
4639 then this symbol was already defined by some previous
4640 dynamic object, and we will be using that previous
4641 definition anyhow. */
4642
4643 h->u.alias = weaks;
4644 weaks = h;
4645 }
4646
4647 /* Set the alignment of a common symbol. */
4648 if ((common || bfd_is_com_section (sec))
4649 && h->root.type == bfd_link_hash_common)
4650 {
4651 unsigned int align;
4652
4653 if (common)
4654 align = bfd_log2 (isym->st_value);
4655 else
4656 {
4657 /* The new symbol is a common symbol in a shared object.
4658 We need to get the alignment from the section. */
4659 align = new_sec->alignment_power;
4660 }
4661 if (align > old_alignment)
4662 h->root.u.c.p->alignment_power = align;
4663 else
4664 h->root.u.c.p->alignment_power = old_alignment;
4665 }
4666
4667 if (is_elf_hash_table (htab))
4668 {
4669 /* Set a flag in the hash table entry indicating the type of
4670 reference or definition we just found. A dynamic symbol
4671 is one which is referenced or defined by both a regular
4672 object and a shared object. */
4673 bfd_boolean dynsym = FALSE;
4674
4675 /* Plugin symbols aren't normal. Don't set def_regular or
4676 ref_regular for them, or make them dynamic. */
4677 if ((abfd->flags & BFD_PLUGIN) != 0)
4678 ;
4679 else if (! dynamic)
4680 {
4681 if (! definition)
4682 {
4683 h->ref_regular = 1;
4684 if (bind != STB_WEAK)
4685 h->ref_regular_nonweak = 1;
4686 }
4687 else
4688 {
4689 h->def_regular = 1;
4690 if (h->def_dynamic)
4691 {
4692 h->def_dynamic = 0;
4693 h->ref_dynamic = 1;
4694 }
4695 }
4696
4697 /* If the indirect symbol has been forced local, don't
4698 make the real symbol dynamic. */
4699 if ((h == hi || !hi->forced_local)
4700 && (bfd_link_dll (info)
4701 || h->def_dynamic
4702 || h->ref_dynamic))
4703 dynsym = TRUE;
4704 }
4705 else
4706 {
4707 if (! definition)
4708 {
4709 h->ref_dynamic = 1;
4710 hi->ref_dynamic = 1;
4711 }
4712 else
4713 {
4714 h->def_dynamic = 1;
4715 hi->def_dynamic = 1;
4716 }
4717
4718 /* If the indirect symbol has been forced local, don't
4719 make the real symbol dynamic. */
4720 if ((h == hi || !hi->forced_local)
4721 && (h->def_regular
4722 || h->ref_regular
4723 || (h->is_weakalias
4724 && weakdef (h)->dynindx != -1)))
4725 dynsym = TRUE;
4726 }
4727
4728 /* Check to see if we need to add an indirect symbol for
4729 the default name. */
4730 if (definition
4731 || (!override && h->root.type == bfd_link_hash_common))
4732 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4733 sec, value, &old_bfd, &dynsym))
4734 goto error_free_vers;
4735
4736 /* Check the alignment when a common symbol is involved. This
4737 can change when a common symbol is overridden by a normal
4738 definition or a common symbol is ignored due to the old
4739 normal definition. We need to make sure the maximum
4740 alignment is maintained. */
4741 if ((old_alignment || common)
4742 && h->root.type != bfd_link_hash_common)
4743 {
4744 unsigned int common_align;
4745 unsigned int normal_align;
4746 unsigned int symbol_align;
4747 bfd *normal_bfd;
4748 bfd *common_bfd;
4749
4750 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4751 || h->root.type == bfd_link_hash_defweak);
4752
4753 symbol_align = ffs (h->root.u.def.value) - 1;
4754 if (h->root.u.def.section->owner != NULL
4755 && (h->root.u.def.section->owner->flags
4756 & (DYNAMIC | BFD_PLUGIN)) == 0)
4757 {
4758 normal_align = h->root.u.def.section->alignment_power;
4759 if (normal_align > symbol_align)
4760 normal_align = symbol_align;
4761 }
4762 else
4763 normal_align = symbol_align;
4764
4765 if (old_alignment)
4766 {
4767 common_align = old_alignment;
4768 common_bfd = old_bfd;
4769 normal_bfd = abfd;
4770 }
4771 else
4772 {
4773 common_align = bfd_log2 (isym->st_value);
4774 common_bfd = abfd;
4775 normal_bfd = old_bfd;
4776 }
4777
4778 if (normal_align < common_align)
4779 {
4780 /* PR binutils/2735 */
4781 if (normal_bfd == NULL)
4782 _bfd_error_handler
4783 /* xgettext:c-format */
4784 (_("Warning: alignment %u of common symbol `%s' in %B is"
4785 " greater than the alignment (%u) of its section %A"),
4786 1 << common_align, name, common_bfd,
4787 1 << normal_align, h->root.u.def.section);
4788 else
4789 _bfd_error_handler
4790 /* xgettext:c-format */
4791 (_("Warning: alignment %u of symbol `%s' in %B"
4792 " is smaller than %u in %B"),
4793 1 << normal_align, name, normal_bfd,
4794 1 << common_align, common_bfd);
4795 }
4796 }
4797
4798 /* Remember the symbol size if it isn't undefined. */
4799 if (isym->st_size != 0
4800 && isym->st_shndx != SHN_UNDEF
4801 && (definition || h->size == 0))
4802 {
4803 if (h->size != 0
4804 && h->size != isym->st_size
4805 && ! size_change_ok)
4806 _bfd_error_handler
4807 /* xgettext:c-format */
4808 (_("Warning: size of symbol `%s' changed"
4809 " from %Lu in %B to %Lu in %B"),
4810 name, h->size, old_bfd, isym->st_size, abfd);
4811
4812 h->size = isym->st_size;
4813 }
4814
4815 /* If this is a common symbol, then we always want H->SIZE
4816 to be the size of the common symbol. The code just above
4817 won't fix the size if a common symbol becomes larger. We
4818 don't warn about a size change here, because that is
4819 covered by --warn-common. Allow changes between different
4820 function types. */
4821 if (h->root.type == bfd_link_hash_common)
4822 h->size = h->root.u.c.size;
4823
4824 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4825 && ((definition && !new_weak)
4826 || (old_weak && h->root.type == bfd_link_hash_common)
4827 || h->type == STT_NOTYPE))
4828 {
4829 unsigned int type = ELF_ST_TYPE (isym->st_info);
4830
4831 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4832 symbol. */
4833 if (type == STT_GNU_IFUNC
4834 && (abfd->flags & DYNAMIC) != 0)
4835 type = STT_FUNC;
4836
4837 if (h->type != type)
4838 {
4839 if (h->type != STT_NOTYPE && ! type_change_ok)
4840 /* xgettext:c-format */
4841 _bfd_error_handler
4842 (_("Warning: type of symbol `%s' changed"
4843 " from %d to %d in %B"),
4844 name, h->type, type, abfd);
4845
4846 h->type = type;
4847 }
4848 }
4849
4850 /* Merge st_other field. */
4851 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4852
4853 /* We don't want to make debug symbol dynamic. */
4854 if (definition
4855 && (sec->flags & SEC_DEBUGGING)
4856 && !bfd_link_relocatable (info))
4857 dynsym = FALSE;
4858
4859 /* Nor should we make plugin symbols dynamic. */
4860 if ((abfd->flags & BFD_PLUGIN) != 0)
4861 dynsym = FALSE;
4862
4863 if (definition)
4864 {
4865 h->target_internal = isym->st_target_internal;
4866 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4867 }
4868
4869 if (definition && !dynamic)
4870 {
4871 char *p = strchr (name, ELF_VER_CHR);
4872 if (p != NULL && p[1] != ELF_VER_CHR)
4873 {
4874 /* Queue non-default versions so that .symver x, x@FOO
4875 aliases can be checked. */
4876 if (!nondeflt_vers)
4877 {
4878 amt = ((isymend - isym + 1)
4879 * sizeof (struct elf_link_hash_entry *));
4880 nondeflt_vers
4881 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4882 if (!nondeflt_vers)
4883 goto error_free_vers;
4884 }
4885 nondeflt_vers[nondeflt_vers_cnt++] = h;
4886 }
4887 }
4888
4889 if (dynsym && h->dynindx == -1)
4890 {
4891 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4892 goto error_free_vers;
4893 if (h->is_weakalias
4894 && weakdef (h)->dynindx == -1)
4895 {
4896 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4897 goto error_free_vers;
4898 }
4899 }
4900 else if (h->dynindx != -1)
4901 /* If the symbol already has a dynamic index, but
4902 visibility says it should not be visible, turn it into
4903 a local symbol. */
4904 switch (ELF_ST_VISIBILITY (h->other))
4905 {
4906 case STV_INTERNAL:
4907 case STV_HIDDEN:
4908 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4909 dynsym = FALSE;
4910 break;
4911 }
4912
4913 /* Don't add DT_NEEDED for references from the dummy bfd nor
4914 for unmatched symbol. */
4915 if (!add_needed
4916 && matched
4917 && definition
4918 && ((dynsym
4919 && h->ref_regular_nonweak
4920 && (old_bfd == NULL
4921 || (old_bfd->flags & BFD_PLUGIN) == 0))
4922 || (h->ref_dynamic_nonweak
4923 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4924 && !on_needed_list (elf_dt_name (abfd),
4925 htab->needed, NULL))))
4926 {
4927 int ret;
4928 const char *soname = elf_dt_name (abfd);
4929
4930 info->callbacks->minfo ("%!", soname, old_bfd,
4931 h->root.root.string);
4932
4933 /* A symbol from a library loaded via DT_NEEDED of some
4934 other library is referenced by a regular object.
4935 Add a DT_NEEDED entry for it. Issue an error if
4936 --no-add-needed is used and the reference was not
4937 a weak one. */
4938 if (old_bfd != NULL
4939 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4940 {
4941 _bfd_error_handler
4942 /* xgettext:c-format */
4943 (_("%B: undefined reference to symbol '%s'"),
4944 old_bfd, name);
4945 bfd_set_error (bfd_error_missing_dso);
4946 goto error_free_vers;
4947 }
4948
4949 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4950 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4951
4952 add_needed = TRUE;
4953 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4954 if (ret < 0)
4955 goto error_free_vers;
4956
4957 BFD_ASSERT (ret == 0);
4958 }
4959 }
4960 }
4961
4962 if (info->lto_plugin_active
4963 && !bfd_link_relocatable (info)
4964 && (abfd->flags & BFD_PLUGIN) == 0
4965 && !just_syms
4966 && extsymcount)
4967 {
4968 int r_sym_shift;
4969
4970 if (bed->s->arch_size == 32)
4971 r_sym_shift = 8;
4972 else
4973 r_sym_shift = 32;
4974
4975 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4976 referenced in regular objects so that linker plugin will get
4977 the correct symbol resolution. */
4978
4979 sym_hash = elf_sym_hashes (abfd);
4980 for (s = abfd->sections; s != NULL; s = s->next)
4981 {
4982 Elf_Internal_Rela *internal_relocs;
4983 Elf_Internal_Rela *rel, *relend;
4984
4985 /* Don't check relocations in excluded sections. */
4986 if ((s->flags & SEC_RELOC) == 0
4987 || s->reloc_count == 0
4988 || (s->flags & SEC_EXCLUDE) != 0
4989 || ((info->strip == strip_all
4990 || info->strip == strip_debugger)
4991 && (s->flags & SEC_DEBUGGING) != 0))
4992 continue;
4993
4994 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4995 NULL,
4996 info->keep_memory);
4997 if (internal_relocs == NULL)
4998 goto error_free_vers;
4999
5000 rel = internal_relocs;
5001 relend = rel + s->reloc_count;
5002 for ( ; rel < relend; rel++)
5003 {
5004 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5005 struct elf_link_hash_entry *h;
5006
5007 /* Skip local symbols. */
5008 if (r_symndx < extsymoff)
5009 continue;
5010
5011 h = sym_hash[r_symndx - extsymoff];
5012 if (h != NULL)
5013 h->root.non_ir_ref_regular = 1;
5014 }
5015
5016 if (elf_section_data (s)->relocs != internal_relocs)
5017 free (internal_relocs);
5018 }
5019 }
5020
5021 if (extversym != NULL)
5022 {
5023 free (extversym);
5024 extversym = NULL;
5025 }
5026
5027 if (isymbuf != NULL)
5028 {
5029 free (isymbuf);
5030 isymbuf = NULL;
5031 }
5032
5033 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5034 {
5035 unsigned int i;
5036
5037 /* Restore the symbol table. */
5038 old_ent = (char *) old_tab + tabsize;
5039 memset (elf_sym_hashes (abfd), 0,
5040 extsymcount * sizeof (struct elf_link_hash_entry *));
5041 htab->root.table.table = old_table;
5042 htab->root.table.size = old_size;
5043 htab->root.table.count = old_count;
5044 memcpy (htab->root.table.table, old_tab, tabsize);
5045 htab->root.undefs = old_undefs;
5046 htab->root.undefs_tail = old_undefs_tail;
5047 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5048 free (old_strtab);
5049 old_strtab = NULL;
5050 for (i = 0; i < htab->root.table.size; i++)
5051 {
5052 struct bfd_hash_entry *p;
5053 struct elf_link_hash_entry *h;
5054 bfd_size_type size;
5055 unsigned int alignment_power;
5056 unsigned int non_ir_ref_dynamic;
5057
5058 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5059 {
5060 h = (struct elf_link_hash_entry *) p;
5061 if (h->root.type == bfd_link_hash_warning)
5062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5063
5064 /* Preserve the maximum alignment and size for common
5065 symbols even if this dynamic lib isn't on DT_NEEDED
5066 since it can still be loaded at run time by another
5067 dynamic lib. */
5068 if (h->root.type == bfd_link_hash_common)
5069 {
5070 size = h->root.u.c.size;
5071 alignment_power = h->root.u.c.p->alignment_power;
5072 }
5073 else
5074 {
5075 size = 0;
5076 alignment_power = 0;
5077 }
5078 /* Preserve non_ir_ref_dynamic so that this symbol
5079 will be exported when the dynamic lib becomes needed
5080 in the second pass. */
5081 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5082 memcpy (p, old_ent, htab->root.table.entsize);
5083 old_ent = (char *) old_ent + htab->root.table.entsize;
5084 h = (struct elf_link_hash_entry *) p;
5085 if (h->root.type == bfd_link_hash_warning)
5086 {
5087 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5088 old_ent = (char *) old_ent + htab->root.table.entsize;
5089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5090 }
5091 if (h->root.type == bfd_link_hash_common)
5092 {
5093 if (size > h->root.u.c.size)
5094 h->root.u.c.size = size;
5095 if (alignment_power > h->root.u.c.p->alignment_power)
5096 h->root.u.c.p->alignment_power = alignment_power;
5097 }
5098 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5099 }
5100 }
5101
5102 /* Make a special call to the linker "notice" function to
5103 tell it that symbols added for crefs may need to be removed. */
5104 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5105 goto error_free_vers;
5106
5107 free (old_tab);
5108 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5109 alloc_mark);
5110 if (nondeflt_vers != NULL)
5111 free (nondeflt_vers);
5112 return TRUE;
5113 }
5114
5115 if (old_tab != NULL)
5116 {
5117 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5118 goto error_free_vers;
5119 free (old_tab);
5120 old_tab = NULL;
5121 }
5122
5123 /* Now that all the symbols from this input file are created, if
5124 not performing a relocatable link, handle .symver foo, foo@BAR
5125 such that any relocs against foo become foo@BAR. */
5126 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5127 {
5128 size_t cnt, symidx;
5129
5130 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5131 {
5132 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5133 char *shortname, *p;
5134
5135 p = strchr (h->root.root.string, ELF_VER_CHR);
5136 if (p == NULL
5137 || (h->root.type != bfd_link_hash_defined
5138 && h->root.type != bfd_link_hash_defweak))
5139 continue;
5140
5141 amt = p - h->root.root.string;
5142 shortname = (char *) bfd_malloc (amt + 1);
5143 if (!shortname)
5144 goto error_free_vers;
5145 memcpy (shortname, h->root.root.string, amt);
5146 shortname[amt] = '\0';
5147
5148 hi = (struct elf_link_hash_entry *)
5149 bfd_link_hash_lookup (&htab->root, shortname,
5150 FALSE, FALSE, FALSE);
5151 if (hi != NULL
5152 && hi->root.type == h->root.type
5153 && hi->root.u.def.value == h->root.u.def.value
5154 && hi->root.u.def.section == h->root.u.def.section)
5155 {
5156 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5157 hi->root.type = bfd_link_hash_indirect;
5158 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5159 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5160 sym_hash = elf_sym_hashes (abfd);
5161 if (sym_hash)
5162 for (symidx = 0; symidx < extsymcount; ++symidx)
5163 if (sym_hash[symidx] == hi)
5164 {
5165 sym_hash[symidx] = h;
5166 break;
5167 }
5168 }
5169 free (shortname);
5170 }
5171 free (nondeflt_vers);
5172 nondeflt_vers = NULL;
5173 }
5174
5175 /* Now set the alias field correctly for all the weak defined
5176 symbols we found. The only way to do this is to search all the
5177 symbols. Since we only need the information for non functions in
5178 dynamic objects, that's the only time we actually put anything on
5179 the list WEAKS. We need this information so that if a regular
5180 object refers to a symbol defined weakly in a dynamic object, the
5181 real symbol in the dynamic object is also put in the dynamic
5182 symbols; we also must arrange for both symbols to point to the
5183 same memory location. We could handle the general case of symbol
5184 aliasing, but a general symbol alias can only be generated in
5185 assembler code, handling it correctly would be very time
5186 consuming, and other ELF linkers don't handle general aliasing
5187 either. */
5188 if (weaks != NULL)
5189 {
5190 struct elf_link_hash_entry **hpp;
5191 struct elf_link_hash_entry **hppend;
5192 struct elf_link_hash_entry **sorted_sym_hash;
5193 struct elf_link_hash_entry *h;
5194 size_t sym_count;
5195
5196 /* Since we have to search the whole symbol list for each weak
5197 defined symbol, search time for N weak defined symbols will be
5198 O(N^2). Binary search will cut it down to O(NlogN). */
5199 amt = extsymcount;
5200 amt *= sizeof (struct elf_link_hash_entry *);
5201 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5202 if (sorted_sym_hash == NULL)
5203 goto error_return;
5204 sym_hash = sorted_sym_hash;
5205 hpp = elf_sym_hashes (abfd);
5206 hppend = hpp + extsymcount;
5207 sym_count = 0;
5208 for (; hpp < hppend; hpp++)
5209 {
5210 h = *hpp;
5211 if (h != NULL
5212 && h->root.type == bfd_link_hash_defined
5213 && !bed->is_function_type (h->type))
5214 {
5215 *sym_hash = h;
5216 sym_hash++;
5217 sym_count++;
5218 }
5219 }
5220
5221 qsort (sorted_sym_hash, sym_count,
5222 sizeof (struct elf_link_hash_entry *),
5223 elf_sort_symbol);
5224
5225 while (weaks != NULL)
5226 {
5227 struct elf_link_hash_entry *hlook;
5228 asection *slook;
5229 bfd_vma vlook;
5230 size_t i, j, idx = 0;
5231
5232 hlook = weaks;
5233 weaks = hlook->u.alias;
5234 hlook->u.alias = NULL;
5235
5236 if (hlook->root.type != bfd_link_hash_defined
5237 && hlook->root.type != bfd_link_hash_defweak)
5238 continue;
5239
5240 slook = hlook->root.u.def.section;
5241 vlook = hlook->root.u.def.value;
5242
5243 i = 0;
5244 j = sym_count;
5245 while (i != j)
5246 {
5247 bfd_signed_vma vdiff;
5248 idx = (i + j) / 2;
5249 h = sorted_sym_hash[idx];
5250 vdiff = vlook - h->root.u.def.value;
5251 if (vdiff < 0)
5252 j = idx;
5253 else if (vdiff > 0)
5254 i = idx + 1;
5255 else
5256 {
5257 int sdiff = slook->id - h->root.u.def.section->id;
5258 if (sdiff < 0)
5259 j = idx;
5260 else if (sdiff > 0)
5261 i = idx + 1;
5262 else
5263 break;
5264 }
5265 }
5266
5267 /* We didn't find a value/section match. */
5268 if (i == j)
5269 continue;
5270
5271 /* With multiple aliases, or when the weak symbol is already
5272 strongly defined, we have multiple matching symbols and
5273 the binary search above may land on any of them. Step
5274 one past the matching symbol(s). */
5275 while (++idx != j)
5276 {
5277 h = sorted_sym_hash[idx];
5278 if (h->root.u.def.section != slook
5279 || h->root.u.def.value != vlook)
5280 break;
5281 }
5282
5283 /* Now look back over the aliases. Since we sorted by size
5284 as well as value and section, we'll choose the one with
5285 the largest size. */
5286 while (idx-- != i)
5287 {
5288 h = sorted_sym_hash[idx];
5289
5290 /* Stop if value or section doesn't match. */
5291 if (h->root.u.def.section != slook
5292 || h->root.u.def.value != vlook)
5293 break;
5294 else if (h != hlook)
5295 {
5296 struct elf_link_hash_entry *t;
5297
5298 hlook->u.alias = h;
5299 hlook->is_weakalias = 1;
5300 t = h;
5301 if (t->u.alias != NULL)
5302 while (t->u.alias != h)
5303 t = t->u.alias;
5304 t->u.alias = hlook;
5305
5306 /* If the weak definition is in the list of dynamic
5307 symbols, make sure the real definition is put
5308 there as well. */
5309 if (hlook->dynindx != -1 && h->dynindx == -1)
5310 {
5311 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5312 {
5313 err_free_sym_hash:
5314 free (sorted_sym_hash);
5315 goto error_return;
5316 }
5317 }
5318
5319 /* If the real definition is in the list of dynamic
5320 symbols, make sure the weak definition is put
5321 there as well. If we don't do this, then the
5322 dynamic loader might not merge the entries for the
5323 real definition and the weak definition. */
5324 if (h->dynindx != -1 && hlook->dynindx == -1)
5325 {
5326 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5327 goto err_free_sym_hash;
5328 }
5329 break;
5330 }
5331 }
5332 }
5333
5334 free (sorted_sym_hash);
5335 }
5336
5337 if (bed->check_directives
5338 && !(*bed->check_directives) (abfd, info))
5339 return FALSE;
5340
5341 /* If this is a non-traditional link, try to optimize the handling
5342 of the .stab/.stabstr sections. */
5343 if (! dynamic
5344 && ! info->traditional_format
5345 && is_elf_hash_table (htab)
5346 && (info->strip != strip_all && info->strip != strip_debugger))
5347 {
5348 asection *stabstr;
5349
5350 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5351 if (stabstr != NULL)
5352 {
5353 bfd_size_type string_offset = 0;
5354 asection *stab;
5355
5356 for (stab = abfd->sections; stab; stab = stab->next)
5357 if (CONST_STRNEQ (stab->name, ".stab")
5358 && (!stab->name[5] ||
5359 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5360 && (stab->flags & SEC_MERGE) == 0
5361 && !bfd_is_abs_section (stab->output_section))
5362 {
5363 struct bfd_elf_section_data *secdata;
5364
5365 secdata = elf_section_data (stab);
5366 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5367 stabstr, &secdata->sec_info,
5368 &string_offset))
5369 goto error_return;
5370 if (secdata->sec_info)
5371 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5372 }
5373 }
5374 }
5375
5376 if (is_elf_hash_table (htab) && add_needed)
5377 {
5378 /* Add this bfd to the loaded list. */
5379 struct elf_link_loaded_list *n;
5380
5381 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5382 if (n == NULL)
5383 goto error_return;
5384 n->abfd = abfd;
5385 n->next = htab->loaded;
5386 htab->loaded = n;
5387 }
5388
5389 return TRUE;
5390
5391 error_free_vers:
5392 if (old_tab != NULL)
5393 free (old_tab);
5394 if (old_strtab != NULL)
5395 free (old_strtab);
5396 if (nondeflt_vers != NULL)
5397 free (nondeflt_vers);
5398 if (extversym != NULL)
5399 free (extversym);
5400 error_free_sym:
5401 if (isymbuf != NULL)
5402 free (isymbuf);
5403 error_return:
5404 return FALSE;
5405 }
5406
5407 /* Return the linker hash table entry of a symbol that might be
5408 satisfied by an archive symbol. Return -1 on error. */
5409
5410 struct elf_link_hash_entry *
5411 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5412 struct bfd_link_info *info,
5413 const char *name)
5414 {
5415 struct elf_link_hash_entry *h;
5416 char *p, *copy;
5417 size_t len, first;
5418
5419 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5420 if (h != NULL)
5421 return h;
5422
5423 /* If this is a default version (the name contains @@), look up the
5424 symbol again with only one `@' as well as without the version.
5425 The effect is that references to the symbol with and without the
5426 version will be matched by the default symbol in the archive. */
5427
5428 p = strchr (name, ELF_VER_CHR);
5429 if (p == NULL || p[1] != ELF_VER_CHR)
5430 return h;
5431
5432 /* First check with only one `@'. */
5433 len = strlen (name);
5434 copy = (char *) bfd_alloc (abfd, len);
5435 if (copy == NULL)
5436 return (struct elf_link_hash_entry *) 0 - 1;
5437
5438 first = p - name + 1;
5439 memcpy (copy, name, first);
5440 memcpy (copy + first, name + first + 1, len - first);
5441
5442 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5443 if (h == NULL)
5444 {
5445 /* We also need to check references to the symbol without the
5446 version. */
5447 copy[first - 1] = '\0';
5448 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5449 FALSE, FALSE, TRUE);
5450 }
5451
5452 bfd_release (abfd, copy);
5453 return h;
5454 }
5455
5456 /* Add symbols from an ELF archive file to the linker hash table. We
5457 don't use _bfd_generic_link_add_archive_symbols because we need to
5458 handle versioned symbols.
5459
5460 Fortunately, ELF archive handling is simpler than that done by
5461 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5462 oddities. In ELF, if we find a symbol in the archive map, and the
5463 symbol is currently undefined, we know that we must pull in that
5464 object file.
5465
5466 Unfortunately, we do have to make multiple passes over the symbol
5467 table until nothing further is resolved. */
5468
5469 static bfd_boolean
5470 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5471 {
5472 symindex c;
5473 unsigned char *included = NULL;
5474 carsym *symdefs;
5475 bfd_boolean loop;
5476 bfd_size_type amt;
5477 const struct elf_backend_data *bed;
5478 struct elf_link_hash_entry * (*archive_symbol_lookup)
5479 (bfd *, struct bfd_link_info *, const char *);
5480
5481 if (! bfd_has_map (abfd))
5482 {
5483 /* An empty archive is a special case. */
5484 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5485 return TRUE;
5486 bfd_set_error (bfd_error_no_armap);
5487 return FALSE;
5488 }
5489
5490 /* Keep track of all symbols we know to be already defined, and all
5491 files we know to be already included. This is to speed up the
5492 second and subsequent passes. */
5493 c = bfd_ardata (abfd)->symdef_count;
5494 if (c == 0)
5495 return TRUE;
5496 amt = c;
5497 amt *= sizeof (*included);
5498 included = (unsigned char *) bfd_zmalloc (amt);
5499 if (included == NULL)
5500 return FALSE;
5501
5502 symdefs = bfd_ardata (abfd)->symdefs;
5503 bed = get_elf_backend_data (abfd);
5504 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5505
5506 do
5507 {
5508 file_ptr last;
5509 symindex i;
5510 carsym *symdef;
5511 carsym *symdefend;
5512
5513 loop = FALSE;
5514 last = -1;
5515
5516 symdef = symdefs;
5517 symdefend = symdef + c;
5518 for (i = 0; symdef < symdefend; symdef++, i++)
5519 {
5520 struct elf_link_hash_entry *h;
5521 bfd *element;
5522 struct bfd_link_hash_entry *undefs_tail;
5523 symindex mark;
5524
5525 if (included[i])
5526 continue;
5527 if (symdef->file_offset == last)
5528 {
5529 included[i] = TRUE;
5530 continue;
5531 }
5532
5533 h = archive_symbol_lookup (abfd, info, symdef->name);
5534 if (h == (struct elf_link_hash_entry *) 0 - 1)
5535 goto error_return;
5536
5537 if (h == NULL)
5538 continue;
5539
5540 if (h->root.type == bfd_link_hash_common)
5541 {
5542 /* We currently have a common symbol. The archive map contains
5543 a reference to this symbol, so we may want to include it. We
5544 only want to include it however, if this archive element
5545 contains a definition of the symbol, not just another common
5546 declaration of it.
5547
5548 Unfortunately some archivers (including GNU ar) will put
5549 declarations of common symbols into their archive maps, as
5550 well as real definitions, so we cannot just go by the archive
5551 map alone. Instead we must read in the element's symbol
5552 table and check that to see what kind of symbol definition
5553 this is. */
5554 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5555 continue;
5556 }
5557 else if (h->root.type != bfd_link_hash_undefined)
5558 {
5559 if (h->root.type != bfd_link_hash_undefweak)
5560 /* Symbol must be defined. Don't check it again. */
5561 included[i] = TRUE;
5562 continue;
5563 }
5564
5565 /* We need to include this archive member. */
5566 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5567 if (element == NULL)
5568 goto error_return;
5569
5570 if (! bfd_check_format (element, bfd_object))
5571 goto error_return;
5572
5573 undefs_tail = info->hash->undefs_tail;
5574
5575 if (!(*info->callbacks
5576 ->add_archive_element) (info, element, symdef->name, &element))
5577 continue;
5578 if (!bfd_link_add_symbols (element, info))
5579 goto error_return;
5580
5581 /* If there are any new undefined symbols, we need to make
5582 another pass through the archive in order to see whether
5583 they can be defined. FIXME: This isn't perfect, because
5584 common symbols wind up on undefs_tail and because an
5585 undefined symbol which is defined later on in this pass
5586 does not require another pass. This isn't a bug, but it
5587 does make the code less efficient than it could be. */
5588 if (undefs_tail != info->hash->undefs_tail)
5589 loop = TRUE;
5590
5591 /* Look backward to mark all symbols from this object file
5592 which we have already seen in this pass. */
5593 mark = i;
5594 do
5595 {
5596 included[mark] = TRUE;
5597 if (mark == 0)
5598 break;
5599 --mark;
5600 }
5601 while (symdefs[mark].file_offset == symdef->file_offset);
5602
5603 /* We mark subsequent symbols from this object file as we go
5604 on through the loop. */
5605 last = symdef->file_offset;
5606 }
5607 }
5608 while (loop);
5609
5610 free (included);
5611
5612 return TRUE;
5613
5614 error_return:
5615 if (included != NULL)
5616 free (included);
5617 return FALSE;
5618 }
5619
5620 /* Given an ELF BFD, add symbols to the global hash table as
5621 appropriate. */
5622
5623 bfd_boolean
5624 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5625 {
5626 switch (bfd_get_format (abfd))
5627 {
5628 case bfd_object:
5629 return elf_link_add_object_symbols (abfd, info);
5630 case bfd_archive:
5631 return elf_link_add_archive_symbols (abfd, info);
5632 default:
5633 bfd_set_error (bfd_error_wrong_format);
5634 return FALSE;
5635 }
5636 }
5637
5638 struct hash_codes_info
5640 {
5641 unsigned long *hashcodes;
5642 bfd_boolean error;
5643 };
5644
5645 /* This function will be called though elf_link_hash_traverse to store
5646 all hash value of the exported symbols in an array. */
5647
5648 static bfd_boolean
5649 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5650 {
5651 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5652 const char *name;
5653 unsigned long ha;
5654 char *alc = NULL;
5655
5656 /* Ignore indirect symbols. These are added by the versioning code. */
5657 if (h->dynindx == -1)
5658 return TRUE;
5659
5660 name = h->root.root.string;
5661 if (h->versioned >= versioned)
5662 {
5663 char *p = strchr (name, ELF_VER_CHR);
5664 if (p != NULL)
5665 {
5666 alc = (char *) bfd_malloc (p - name + 1);
5667 if (alc == NULL)
5668 {
5669 inf->error = TRUE;
5670 return FALSE;
5671 }
5672 memcpy (alc, name, p - name);
5673 alc[p - name] = '\0';
5674 name = alc;
5675 }
5676 }
5677
5678 /* Compute the hash value. */
5679 ha = bfd_elf_hash (name);
5680
5681 /* Store the found hash value in the array given as the argument. */
5682 *(inf->hashcodes)++ = ha;
5683
5684 /* And store it in the struct so that we can put it in the hash table
5685 later. */
5686 h->u.elf_hash_value = ha;
5687
5688 if (alc != NULL)
5689 free (alc);
5690
5691 return TRUE;
5692 }
5693
5694 struct collect_gnu_hash_codes
5695 {
5696 bfd *output_bfd;
5697 const struct elf_backend_data *bed;
5698 unsigned long int nsyms;
5699 unsigned long int maskbits;
5700 unsigned long int *hashcodes;
5701 unsigned long int *hashval;
5702 unsigned long int *indx;
5703 unsigned long int *counts;
5704 bfd_vma *bitmask;
5705 bfd_byte *contents;
5706 long int min_dynindx;
5707 unsigned long int bucketcount;
5708 unsigned long int symindx;
5709 long int local_indx;
5710 long int shift1, shift2;
5711 unsigned long int mask;
5712 bfd_boolean error;
5713 };
5714
5715 /* This function will be called though elf_link_hash_traverse to store
5716 all hash value of the exported symbols in an array. */
5717
5718 static bfd_boolean
5719 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5720 {
5721 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5722 const char *name;
5723 unsigned long ha;
5724 char *alc = NULL;
5725
5726 /* Ignore indirect symbols. These are added by the versioning code. */
5727 if (h->dynindx == -1)
5728 return TRUE;
5729
5730 /* Ignore also local symbols and undefined symbols. */
5731 if (! (*s->bed->elf_hash_symbol) (h))
5732 return TRUE;
5733
5734 name = h->root.root.string;
5735 if (h->versioned >= versioned)
5736 {
5737 char *p = strchr (name, ELF_VER_CHR);
5738 if (p != NULL)
5739 {
5740 alc = (char *) bfd_malloc (p - name + 1);
5741 if (alc == NULL)
5742 {
5743 s->error = TRUE;
5744 return FALSE;
5745 }
5746 memcpy (alc, name, p - name);
5747 alc[p - name] = '\0';
5748 name = alc;
5749 }
5750 }
5751
5752 /* Compute the hash value. */
5753 ha = bfd_elf_gnu_hash (name);
5754
5755 /* Store the found hash value in the array for compute_bucket_count,
5756 and also for .dynsym reordering purposes. */
5757 s->hashcodes[s->nsyms] = ha;
5758 s->hashval[h->dynindx] = ha;
5759 ++s->nsyms;
5760 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5761 s->min_dynindx = h->dynindx;
5762
5763 if (alc != NULL)
5764 free (alc);
5765
5766 return TRUE;
5767 }
5768
5769 /* This function will be called though elf_link_hash_traverse to do
5770 final dynaminc symbol renumbering. */
5771
5772 static bfd_boolean
5773 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5774 {
5775 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5776 unsigned long int bucket;
5777 unsigned long int val;
5778
5779 /* Ignore indirect symbols. */
5780 if (h->dynindx == -1)
5781 return TRUE;
5782
5783 /* Ignore also local symbols and undefined symbols. */
5784 if (! (*s->bed->elf_hash_symbol) (h))
5785 {
5786 if (h->dynindx >= s->min_dynindx)
5787 h->dynindx = s->local_indx++;
5788 return TRUE;
5789 }
5790
5791 bucket = s->hashval[h->dynindx] % s->bucketcount;
5792 val = (s->hashval[h->dynindx] >> s->shift1)
5793 & ((s->maskbits >> s->shift1) - 1);
5794 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5795 s->bitmask[val]
5796 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5797 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5798 if (s->counts[bucket] == 1)
5799 /* Last element terminates the chain. */
5800 val |= 1;
5801 bfd_put_32 (s->output_bfd, val,
5802 s->contents + (s->indx[bucket] - s->symindx) * 4);
5803 --s->counts[bucket];
5804 h->dynindx = s->indx[bucket]++;
5805 return TRUE;
5806 }
5807
5808 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5809
5810 bfd_boolean
5811 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5812 {
5813 return !(h->forced_local
5814 || h->root.type == bfd_link_hash_undefined
5815 || h->root.type == bfd_link_hash_undefweak
5816 || ((h->root.type == bfd_link_hash_defined
5817 || h->root.type == bfd_link_hash_defweak)
5818 && h->root.u.def.section->output_section == NULL));
5819 }
5820
5821 /* Array used to determine the number of hash table buckets to use
5822 based on the number of symbols there are. If there are fewer than
5823 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5824 fewer than 37 we use 17 buckets, and so forth. We never use more
5825 than 32771 buckets. */
5826
5827 static const size_t elf_buckets[] =
5828 {
5829 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5830 16411, 32771, 0
5831 };
5832
5833 /* Compute bucket count for hashing table. We do not use a static set
5834 of possible tables sizes anymore. Instead we determine for all
5835 possible reasonable sizes of the table the outcome (i.e., the
5836 number of collisions etc) and choose the best solution. The
5837 weighting functions are not too simple to allow the table to grow
5838 without bounds. Instead one of the weighting factors is the size.
5839 Therefore the result is always a good payoff between few collisions
5840 (= short chain lengths) and table size. */
5841 static size_t
5842 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5843 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5844 unsigned long int nsyms,
5845 int gnu_hash)
5846 {
5847 size_t best_size = 0;
5848 unsigned long int i;
5849
5850 /* We have a problem here. The following code to optimize the table
5851 size requires an integer type with more the 32 bits. If
5852 BFD_HOST_U_64_BIT is set we know about such a type. */
5853 #ifdef BFD_HOST_U_64_BIT
5854 if (info->optimize)
5855 {
5856 size_t minsize;
5857 size_t maxsize;
5858 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5859 bfd *dynobj = elf_hash_table (info)->dynobj;
5860 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5861 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5862 unsigned long int *counts;
5863 bfd_size_type amt;
5864 unsigned int no_improvement_count = 0;
5865
5866 /* Possible optimization parameters: if we have NSYMS symbols we say
5867 that the hashing table must at least have NSYMS/4 and at most
5868 2*NSYMS buckets. */
5869 minsize = nsyms / 4;
5870 if (minsize == 0)
5871 minsize = 1;
5872 best_size = maxsize = nsyms * 2;
5873 if (gnu_hash)
5874 {
5875 if (minsize < 2)
5876 minsize = 2;
5877 if ((best_size & 31) == 0)
5878 ++best_size;
5879 }
5880
5881 /* Create array where we count the collisions in. We must use bfd_malloc
5882 since the size could be large. */
5883 amt = maxsize;
5884 amt *= sizeof (unsigned long int);
5885 counts = (unsigned long int *) bfd_malloc (amt);
5886 if (counts == NULL)
5887 return 0;
5888
5889 /* Compute the "optimal" size for the hash table. The criteria is a
5890 minimal chain length. The minor criteria is (of course) the size
5891 of the table. */
5892 for (i = minsize; i < maxsize; ++i)
5893 {
5894 /* Walk through the array of hashcodes and count the collisions. */
5895 BFD_HOST_U_64_BIT max;
5896 unsigned long int j;
5897 unsigned long int fact;
5898
5899 if (gnu_hash && (i & 31) == 0)
5900 continue;
5901
5902 memset (counts, '\0', i * sizeof (unsigned long int));
5903
5904 /* Determine how often each hash bucket is used. */
5905 for (j = 0; j < nsyms; ++j)
5906 ++counts[hashcodes[j] % i];
5907
5908 /* For the weight function we need some information about the
5909 pagesize on the target. This is information need not be 100%
5910 accurate. Since this information is not available (so far) we
5911 define it here to a reasonable default value. If it is crucial
5912 to have a better value some day simply define this value. */
5913 # ifndef BFD_TARGET_PAGESIZE
5914 # define BFD_TARGET_PAGESIZE (4096)
5915 # endif
5916
5917 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5918 and the chains. */
5919 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5920
5921 # if 1
5922 /* Variant 1: optimize for short chains. We add the squares
5923 of all the chain lengths (which favors many small chain
5924 over a few long chains). */
5925 for (j = 0; j < i; ++j)
5926 max += counts[j] * counts[j];
5927
5928 /* This adds penalties for the overall size of the table. */
5929 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5930 max *= fact * fact;
5931 # else
5932 /* Variant 2: Optimize a lot more for small table. Here we
5933 also add squares of the size but we also add penalties for
5934 empty slots (the +1 term). */
5935 for (j = 0; j < i; ++j)
5936 max += (1 + counts[j]) * (1 + counts[j]);
5937
5938 /* The overall size of the table is considered, but not as
5939 strong as in variant 1, where it is squared. */
5940 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5941 max *= fact;
5942 # endif
5943
5944 /* Compare with current best results. */
5945 if (max < best_chlen)
5946 {
5947 best_chlen = max;
5948 best_size = i;
5949 no_improvement_count = 0;
5950 }
5951 /* PR 11843: Avoid futile long searches for the best bucket size
5952 when there are a large number of symbols. */
5953 else if (++no_improvement_count == 100)
5954 break;
5955 }
5956
5957 free (counts);
5958 }
5959 else
5960 #endif /* defined (BFD_HOST_U_64_BIT) */
5961 {
5962 /* This is the fallback solution if no 64bit type is available or if we
5963 are not supposed to spend much time on optimizations. We select the
5964 bucket count using a fixed set of numbers. */
5965 for (i = 0; elf_buckets[i] != 0; i++)
5966 {
5967 best_size = elf_buckets[i];
5968 if (nsyms < elf_buckets[i + 1])
5969 break;
5970 }
5971 if (gnu_hash && best_size < 2)
5972 best_size = 2;
5973 }
5974
5975 return best_size;
5976 }
5977
5978 /* Size any SHT_GROUP section for ld -r. */
5979
5980 bfd_boolean
5981 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5982 {
5983 bfd *ibfd;
5984 asection *s;
5985
5986 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5987 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5988 && (s = ibfd->sections) != NULL
5989 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5990 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5991 return FALSE;
5992 return TRUE;
5993 }
5994
5995 /* Set a default stack segment size. The value in INFO wins. If it
5996 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5997 undefined it is initialized. */
5998
5999 bfd_boolean
6000 bfd_elf_stack_segment_size (bfd *output_bfd,
6001 struct bfd_link_info *info,
6002 const char *legacy_symbol,
6003 bfd_vma default_size)
6004 {
6005 struct elf_link_hash_entry *h = NULL;
6006
6007 /* Look for legacy symbol. */
6008 if (legacy_symbol)
6009 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6010 FALSE, FALSE, FALSE);
6011 if (h && (h->root.type == bfd_link_hash_defined
6012 || h->root.type == bfd_link_hash_defweak)
6013 && h->def_regular
6014 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6015 {
6016 /* The symbol has no type if specified on the command line. */
6017 h->type = STT_OBJECT;
6018 if (info->stacksize)
6019 /* xgettext:c-format */
6020 _bfd_error_handler (_("%B: stack size specified and %s set"),
6021 output_bfd, legacy_symbol);
6022 else if (h->root.u.def.section != bfd_abs_section_ptr)
6023 /* xgettext:c-format */
6024 _bfd_error_handler (_("%B: %s not absolute"),
6025 output_bfd, legacy_symbol);
6026 else
6027 info->stacksize = h->root.u.def.value;
6028 }
6029
6030 if (!info->stacksize)
6031 /* If the user didn't set a size, or explicitly inhibit the
6032 size, set it now. */
6033 info->stacksize = default_size;
6034
6035 /* Provide the legacy symbol, if it is referenced. */
6036 if (h && (h->root.type == bfd_link_hash_undefined
6037 || h->root.type == bfd_link_hash_undefweak))
6038 {
6039 struct bfd_link_hash_entry *bh = NULL;
6040
6041 if (!(_bfd_generic_link_add_one_symbol
6042 (info, output_bfd, legacy_symbol,
6043 BSF_GLOBAL, bfd_abs_section_ptr,
6044 info->stacksize >= 0 ? info->stacksize : 0,
6045 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6046 return FALSE;
6047
6048 h = (struct elf_link_hash_entry *) bh;
6049 h->def_regular = 1;
6050 h->type = STT_OBJECT;
6051 }
6052
6053 return TRUE;
6054 }
6055
6056 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6057
6058 struct elf_gc_sweep_symbol_info
6059 {
6060 struct bfd_link_info *info;
6061 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6062 bfd_boolean);
6063 };
6064
6065 static bfd_boolean
6066 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6067 {
6068 if (!h->mark
6069 && (((h->root.type == bfd_link_hash_defined
6070 || h->root.type == bfd_link_hash_defweak)
6071 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6072 && h->root.u.def.section->gc_mark))
6073 || h->root.type == bfd_link_hash_undefined
6074 || h->root.type == bfd_link_hash_undefweak))
6075 {
6076 struct elf_gc_sweep_symbol_info *inf;
6077
6078 inf = (struct elf_gc_sweep_symbol_info *) data;
6079 (*inf->hide_symbol) (inf->info, h, TRUE);
6080 h->def_regular = 0;
6081 h->ref_regular = 0;
6082 h->ref_regular_nonweak = 0;
6083 }
6084
6085 return TRUE;
6086 }
6087
6088 /* Set up the sizes and contents of the ELF dynamic sections. This is
6089 called by the ELF linker emulation before_allocation routine. We
6090 must set the sizes of the sections before the linker sets the
6091 addresses of the various sections. */
6092
6093 bfd_boolean
6094 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6095 const char *soname,
6096 const char *rpath,
6097 const char *filter_shlib,
6098 const char *audit,
6099 const char *depaudit,
6100 const char * const *auxiliary_filters,
6101 struct bfd_link_info *info,
6102 asection **sinterpptr)
6103 {
6104 bfd *dynobj;
6105 const struct elf_backend_data *bed;
6106
6107 *sinterpptr = NULL;
6108
6109 if (!is_elf_hash_table (info->hash))
6110 return TRUE;
6111
6112 dynobj = elf_hash_table (info)->dynobj;
6113
6114 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6115 {
6116 struct bfd_elf_version_tree *verdefs;
6117 struct elf_info_failed asvinfo;
6118 struct bfd_elf_version_tree *t;
6119 struct bfd_elf_version_expr *d;
6120 asection *s;
6121 size_t soname_indx;
6122
6123 /* If we are supposed to export all symbols into the dynamic symbol
6124 table (this is not the normal case), then do so. */
6125 if (info->export_dynamic
6126 || (bfd_link_executable (info) && info->dynamic))
6127 {
6128 struct elf_info_failed eif;
6129
6130 eif.info = info;
6131 eif.failed = FALSE;
6132 elf_link_hash_traverse (elf_hash_table (info),
6133 _bfd_elf_export_symbol,
6134 &eif);
6135 if (eif.failed)
6136 return FALSE;
6137 }
6138
6139 if (soname != NULL)
6140 {
6141 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6142 soname, TRUE);
6143 if (soname_indx == (size_t) -1
6144 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6145 return FALSE;
6146 }
6147 else
6148 soname_indx = (size_t) -1;
6149
6150 /* Make all global versions with definition. */
6151 for (t = info->version_info; t != NULL; t = t->next)
6152 for (d = t->globals.list; d != NULL; d = d->next)
6153 if (!d->symver && d->literal)
6154 {
6155 const char *verstr, *name;
6156 size_t namelen, verlen, newlen;
6157 char *newname, *p, leading_char;
6158 struct elf_link_hash_entry *newh;
6159
6160 leading_char = bfd_get_symbol_leading_char (output_bfd);
6161 name = d->pattern;
6162 namelen = strlen (name) + (leading_char != '\0');
6163 verstr = t->name;
6164 verlen = strlen (verstr);
6165 newlen = namelen + verlen + 3;
6166
6167 newname = (char *) bfd_malloc (newlen);
6168 if (newname == NULL)
6169 return FALSE;
6170 newname[0] = leading_char;
6171 memcpy (newname + (leading_char != '\0'), name, namelen);
6172
6173 /* Check the hidden versioned definition. */
6174 p = newname + namelen;
6175 *p++ = ELF_VER_CHR;
6176 memcpy (p, verstr, verlen + 1);
6177 newh = elf_link_hash_lookup (elf_hash_table (info),
6178 newname, FALSE, FALSE,
6179 FALSE);
6180 if (newh == NULL
6181 || (newh->root.type != bfd_link_hash_defined
6182 && newh->root.type != bfd_link_hash_defweak))
6183 {
6184 /* Check the default versioned definition. */
6185 *p++ = ELF_VER_CHR;
6186 memcpy (p, verstr, verlen + 1);
6187 newh = elf_link_hash_lookup (elf_hash_table (info),
6188 newname, FALSE, FALSE,
6189 FALSE);
6190 }
6191 free (newname);
6192
6193 /* Mark this version if there is a definition and it is
6194 not defined in a shared object. */
6195 if (newh != NULL
6196 && !newh->def_dynamic
6197 && (newh->root.type == bfd_link_hash_defined
6198 || newh->root.type == bfd_link_hash_defweak))
6199 d->symver = 1;
6200 }
6201
6202 /* Attach all the symbols to their version information. */
6203 asvinfo.info = info;
6204 asvinfo.failed = FALSE;
6205
6206 elf_link_hash_traverse (elf_hash_table (info),
6207 _bfd_elf_link_assign_sym_version,
6208 &asvinfo);
6209 if (asvinfo.failed)
6210 return FALSE;
6211
6212 if (!info->allow_undefined_version)
6213 {
6214 /* Check if all global versions have a definition. */
6215 bfd_boolean all_defined = TRUE;
6216 for (t = info->version_info; t != NULL; t = t->next)
6217 for (d = t->globals.list; d != NULL; d = d->next)
6218 if (d->literal && !d->symver && !d->script)
6219 {
6220 _bfd_error_handler
6221 (_("%s: undefined version: %s"),
6222 d->pattern, t->name);
6223 all_defined = FALSE;
6224 }
6225
6226 if (!all_defined)
6227 {
6228 bfd_set_error (bfd_error_bad_value);
6229 return FALSE;
6230 }
6231 }
6232
6233 /* Set up the version definition section. */
6234 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6235 BFD_ASSERT (s != NULL);
6236
6237 /* We may have created additional version definitions if we are
6238 just linking a regular application. */
6239 verdefs = info->version_info;
6240
6241 /* Skip anonymous version tag. */
6242 if (verdefs != NULL && verdefs->vernum == 0)
6243 verdefs = verdefs->next;
6244
6245 if (verdefs == NULL && !info->create_default_symver)
6246 s->flags |= SEC_EXCLUDE;
6247 else
6248 {
6249 unsigned int cdefs;
6250 bfd_size_type size;
6251 bfd_byte *p;
6252 Elf_Internal_Verdef def;
6253 Elf_Internal_Verdaux defaux;
6254 struct bfd_link_hash_entry *bh;
6255 struct elf_link_hash_entry *h;
6256 const char *name;
6257
6258 cdefs = 0;
6259 size = 0;
6260
6261 /* Make space for the base version. */
6262 size += sizeof (Elf_External_Verdef);
6263 size += sizeof (Elf_External_Verdaux);
6264 ++cdefs;
6265
6266 /* Make space for the default version. */
6267 if (info->create_default_symver)
6268 {
6269 size += sizeof (Elf_External_Verdef);
6270 ++cdefs;
6271 }
6272
6273 for (t = verdefs; t != NULL; t = t->next)
6274 {
6275 struct bfd_elf_version_deps *n;
6276
6277 /* Don't emit base version twice. */
6278 if (t->vernum == 0)
6279 continue;
6280
6281 size += sizeof (Elf_External_Verdef);
6282 size += sizeof (Elf_External_Verdaux);
6283 ++cdefs;
6284
6285 for (n = t->deps; n != NULL; n = n->next)
6286 size += sizeof (Elf_External_Verdaux);
6287 }
6288
6289 s->size = size;
6290 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6291 if (s->contents == NULL && s->size != 0)
6292 return FALSE;
6293
6294 /* Fill in the version definition section. */
6295
6296 p = s->contents;
6297
6298 def.vd_version = VER_DEF_CURRENT;
6299 def.vd_flags = VER_FLG_BASE;
6300 def.vd_ndx = 1;
6301 def.vd_cnt = 1;
6302 if (info->create_default_symver)
6303 {
6304 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6305 def.vd_next = sizeof (Elf_External_Verdef);
6306 }
6307 else
6308 {
6309 def.vd_aux = sizeof (Elf_External_Verdef);
6310 def.vd_next = (sizeof (Elf_External_Verdef)
6311 + sizeof (Elf_External_Verdaux));
6312 }
6313
6314 if (soname_indx != (size_t) -1)
6315 {
6316 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6317 soname_indx);
6318 def.vd_hash = bfd_elf_hash (soname);
6319 defaux.vda_name = soname_indx;
6320 name = soname;
6321 }
6322 else
6323 {
6324 size_t indx;
6325
6326 name = lbasename (output_bfd->filename);
6327 def.vd_hash = bfd_elf_hash (name);
6328 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6329 name, FALSE);
6330 if (indx == (size_t) -1)
6331 return FALSE;
6332 defaux.vda_name = indx;
6333 }
6334 defaux.vda_next = 0;
6335
6336 _bfd_elf_swap_verdef_out (output_bfd, &def,
6337 (Elf_External_Verdef *) p);
6338 p += sizeof (Elf_External_Verdef);
6339 if (info->create_default_symver)
6340 {
6341 /* Add a symbol representing this version. */
6342 bh = NULL;
6343 if (! (_bfd_generic_link_add_one_symbol
6344 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6345 0, NULL, FALSE,
6346 get_elf_backend_data (dynobj)->collect, &bh)))
6347 return FALSE;
6348 h = (struct elf_link_hash_entry *) bh;
6349 h->non_elf = 0;
6350 h->def_regular = 1;
6351 h->type = STT_OBJECT;
6352 h->verinfo.vertree = NULL;
6353
6354 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6355 return FALSE;
6356
6357 /* Create a duplicate of the base version with the same
6358 aux block, but different flags. */
6359 def.vd_flags = 0;
6360 def.vd_ndx = 2;
6361 def.vd_aux = sizeof (Elf_External_Verdef);
6362 if (verdefs)
6363 def.vd_next = (sizeof (Elf_External_Verdef)
6364 + sizeof (Elf_External_Verdaux));
6365 else
6366 def.vd_next = 0;
6367 _bfd_elf_swap_verdef_out (output_bfd, &def,
6368 (Elf_External_Verdef *) p);
6369 p += sizeof (Elf_External_Verdef);
6370 }
6371 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6372 (Elf_External_Verdaux *) p);
6373 p += sizeof (Elf_External_Verdaux);
6374
6375 for (t = verdefs; t != NULL; t = t->next)
6376 {
6377 unsigned int cdeps;
6378 struct bfd_elf_version_deps *n;
6379
6380 /* Don't emit the base version twice. */
6381 if (t->vernum == 0)
6382 continue;
6383
6384 cdeps = 0;
6385 for (n = t->deps; n != NULL; n = n->next)
6386 ++cdeps;
6387
6388 /* Add a symbol representing this version. */
6389 bh = NULL;
6390 if (! (_bfd_generic_link_add_one_symbol
6391 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6392 0, NULL, FALSE,
6393 get_elf_backend_data (dynobj)->collect, &bh)))
6394 return FALSE;
6395 h = (struct elf_link_hash_entry *) bh;
6396 h->non_elf = 0;
6397 h->def_regular = 1;
6398 h->type = STT_OBJECT;
6399 h->verinfo.vertree = t;
6400
6401 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6402 return FALSE;
6403
6404 def.vd_version = VER_DEF_CURRENT;
6405 def.vd_flags = 0;
6406 if (t->globals.list == NULL
6407 && t->locals.list == NULL
6408 && ! t->used)
6409 def.vd_flags |= VER_FLG_WEAK;
6410 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6411 def.vd_cnt = cdeps + 1;
6412 def.vd_hash = bfd_elf_hash (t->name);
6413 def.vd_aux = sizeof (Elf_External_Verdef);
6414 def.vd_next = 0;
6415
6416 /* If a basever node is next, it *must* be the last node in
6417 the chain, otherwise Verdef construction breaks. */
6418 if (t->next != NULL && t->next->vernum == 0)
6419 BFD_ASSERT (t->next->next == NULL);
6420
6421 if (t->next != NULL && t->next->vernum != 0)
6422 def.vd_next = (sizeof (Elf_External_Verdef)
6423 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6424
6425 _bfd_elf_swap_verdef_out (output_bfd, &def,
6426 (Elf_External_Verdef *) p);
6427 p += sizeof (Elf_External_Verdef);
6428
6429 defaux.vda_name = h->dynstr_index;
6430 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6431 h->dynstr_index);
6432 defaux.vda_next = 0;
6433 if (t->deps != NULL)
6434 defaux.vda_next = sizeof (Elf_External_Verdaux);
6435 t->name_indx = defaux.vda_name;
6436
6437 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6438 (Elf_External_Verdaux *) p);
6439 p += sizeof (Elf_External_Verdaux);
6440
6441 for (n = t->deps; n != NULL; n = n->next)
6442 {
6443 if (n->version_needed == NULL)
6444 {
6445 /* This can happen if there was an error in the
6446 version script. */
6447 defaux.vda_name = 0;
6448 }
6449 else
6450 {
6451 defaux.vda_name = n->version_needed->name_indx;
6452 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6453 defaux.vda_name);
6454 }
6455 if (n->next == NULL)
6456 defaux.vda_next = 0;
6457 else
6458 defaux.vda_next = sizeof (Elf_External_Verdaux);
6459
6460 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6461 (Elf_External_Verdaux *) p);
6462 p += sizeof (Elf_External_Verdaux);
6463 }
6464 }
6465
6466 elf_tdata (output_bfd)->cverdefs = cdefs;
6467 }
6468 }
6469
6470 bed = get_elf_backend_data (output_bfd);
6471
6472 if (info->gc_sections && bed->can_gc_sections)
6473 {
6474 struct elf_gc_sweep_symbol_info sweep_info;
6475
6476 /* Remove the symbols that were in the swept sections from the
6477 dynamic symbol table. */
6478 sweep_info.info = info;
6479 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6480 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6481 &sweep_info);
6482 }
6483
6484 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6485 {
6486 asection *s;
6487 struct elf_find_verdep_info sinfo;
6488
6489 /* Work out the size of the version reference section. */
6490
6491 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6492 BFD_ASSERT (s != NULL);
6493
6494 sinfo.info = info;
6495 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6496 if (sinfo.vers == 0)
6497 sinfo.vers = 1;
6498 sinfo.failed = FALSE;
6499
6500 elf_link_hash_traverse (elf_hash_table (info),
6501 _bfd_elf_link_find_version_dependencies,
6502 &sinfo);
6503 if (sinfo.failed)
6504 return FALSE;
6505
6506 if (elf_tdata (output_bfd)->verref == NULL)
6507 s->flags |= SEC_EXCLUDE;
6508 else
6509 {
6510 Elf_Internal_Verneed *vn;
6511 unsigned int size;
6512 unsigned int crefs;
6513 bfd_byte *p;
6514
6515 /* Build the version dependency section. */
6516 size = 0;
6517 crefs = 0;
6518 for (vn = elf_tdata (output_bfd)->verref;
6519 vn != NULL;
6520 vn = vn->vn_nextref)
6521 {
6522 Elf_Internal_Vernaux *a;
6523
6524 size += sizeof (Elf_External_Verneed);
6525 ++crefs;
6526 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6527 size += sizeof (Elf_External_Vernaux);
6528 }
6529
6530 s->size = size;
6531 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6532 if (s->contents == NULL)
6533 return FALSE;
6534
6535 p = s->contents;
6536 for (vn = elf_tdata (output_bfd)->verref;
6537 vn != NULL;
6538 vn = vn->vn_nextref)
6539 {
6540 unsigned int caux;
6541 Elf_Internal_Vernaux *a;
6542 size_t indx;
6543
6544 caux = 0;
6545 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6546 ++caux;
6547
6548 vn->vn_version = VER_NEED_CURRENT;
6549 vn->vn_cnt = caux;
6550 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6551 elf_dt_name (vn->vn_bfd) != NULL
6552 ? elf_dt_name (vn->vn_bfd)
6553 : lbasename (vn->vn_bfd->filename),
6554 FALSE);
6555 if (indx == (size_t) -1)
6556 return FALSE;
6557 vn->vn_file = indx;
6558 vn->vn_aux = sizeof (Elf_External_Verneed);
6559 if (vn->vn_nextref == NULL)
6560 vn->vn_next = 0;
6561 else
6562 vn->vn_next = (sizeof (Elf_External_Verneed)
6563 + caux * sizeof (Elf_External_Vernaux));
6564
6565 _bfd_elf_swap_verneed_out (output_bfd, vn,
6566 (Elf_External_Verneed *) p);
6567 p += sizeof (Elf_External_Verneed);
6568
6569 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6570 {
6571 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6572 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6573 a->vna_nodename, FALSE);
6574 if (indx == (size_t) -1)
6575 return FALSE;
6576 a->vna_name = indx;
6577 if (a->vna_nextptr == NULL)
6578 a->vna_next = 0;
6579 else
6580 a->vna_next = sizeof (Elf_External_Vernaux);
6581
6582 _bfd_elf_swap_vernaux_out (output_bfd, a,
6583 (Elf_External_Vernaux *) p);
6584 p += sizeof (Elf_External_Vernaux);
6585 }
6586 }
6587
6588 elf_tdata (output_bfd)->cverrefs = crefs;
6589 }
6590 }
6591
6592 /* Any syms created from now on start with -1 in
6593 got.refcount/offset and plt.refcount/offset. */
6594 elf_hash_table (info)->init_got_refcount
6595 = elf_hash_table (info)->init_got_offset;
6596 elf_hash_table (info)->init_plt_refcount
6597 = elf_hash_table (info)->init_plt_offset;
6598
6599 if (bfd_link_relocatable (info)
6600 && !_bfd_elf_size_group_sections (info))
6601 return FALSE;
6602
6603 /* The backend may have to create some sections regardless of whether
6604 we're dynamic or not. */
6605 if (bed->elf_backend_always_size_sections
6606 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6607 return FALSE;
6608
6609 /* Determine any GNU_STACK segment requirements, after the backend
6610 has had a chance to set a default segment size. */
6611 if (info->execstack)
6612 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6613 else if (info->noexecstack)
6614 elf_stack_flags (output_bfd) = PF_R | PF_W;
6615 else
6616 {
6617 bfd *inputobj;
6618 asection *notesec = NULL;
6619 int exec = 0;
6620
6621 for (inputobj = info->input_bfds;
6622 inputobj;
6623 inputobj = inputobj->link.next)
6624 {
6625 asection *s;
6626
6627 if (inputobj->flags
6628 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6629 continue;
6630 s = inputobj->sections;
6631 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6632 continue;
6633
6634 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6635 if (s)
6636 {
6637 if (s->flags & SEC_CODE)
6638 exec = PF_X;
6639 notesec = s;
6640 }
6641 else if (bed->default_execstack)
6642 exec = PF_X;
6643 }
6644 if (notesec || info->stacksize > 0)
6645 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6646 if (notesec && exec && bfd_link_relocatable (info)
6647 && notesec->output_section != bfd_abs_section_ptr)
6648 notesec->output_section->flags |= SEC_CODE;
6649 }
6650
6651 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6652 {
6653 struct elf_info_failed eif;
6654 struct elf_link_hash_entry *h;
6655 asection *dynstr;
6656 asection *s;
6657
6658 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6659 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6660
6661 if (info->symbolic)
6662 {
6663 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6664 return FALSE;
6665 info->flags |= DF_SYMBOLIC;
6666 }
6667
6668 if (rpath != NULL)
6669 {
6670 size_t indx;
6671 bfd_vma tag;
6672
6673 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6674 TRUE);
6675 if (indx == (size_t) -1)
6676 return FALSE;
6677
6678 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6679 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6680 return FALSE;
6681 }
6682
6683 if (filter_shlib != NULL)
6684 {
6685 size_t indx;
6686
6687 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6688 filter_shlib, TRUE);
6689 if (indx == (size_t) -1
6690 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6691 return FALSE;
6692 }
6693
6694 if (auxiliary_filters != NULL)
6695 {
6696 const char * const *p;
6697
6698 for (p = auxiliary_filters; *p != NULL; p++)
6699 {
6700 size_t indx;
6701
6702 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6703 *p, TRUE);
6704 if (indx == (size_t) -1
6705 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6706 return FALSE;
6707 }
6708 }
6709
6710 if (audit != NULL)
6711 {
6712 size_t indx;
6713
6714 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6715 TRUE);
6716 if (indx == (size_t) -1
6717 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6718 return FALSE;
6719 }
6720
6721 if (depaudit != NULL)
6722 {
6723 size_t indx;
6724
6725 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6726 TRUE);
6727 if (indx == (size_t) -1
6728 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6729 return FALSE;
6730 }
6731
6732 eif.info = info;
6733 eif.failed = FALSE;
6734
6735 /* Find all symbols which were defined in a dynamic object and make
6736 the backend pick a reasonable value for them. */
6737 elf_link_hash_traverse (elf_hash_table (info),
6738 _bfd_elf_adjust_dynamic_symbol,
6739 &eif);
6740 if (eif.failed)
6741 return FALSE;
6742
6743 /* Add some entries to the .dynamic section. We fill in some of the
6744 values later, in bfd_elf_final_link, but we must add the entries
6745 now so that we know the final size of the .dynamic section. */
6746
6747 /* If there are initialization and/or finalization functions to
6748 call then add the corresponding DT_INIT/DT_FINI entries. */
6749 h = (info->init_function
6750 ? elf_link_hash_lookup (elf_hash_table (info),
6751 info->init_function, FALSE,
6752 FALSE, FALSE)
6753 : NULL);
6754 if (h != NULL
6755 && (h->ref_regular
6756 || h->def_regular))
6757 {
6758 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6759 return FALSE;
6760 }
6761 h = (info->fini_function
6762 ? elf_link_hash_lookup (elf_hash_table (info),
6763 info->fini_function, FALSE,
6764 FALSE, FALSE)
6765 : NULL);
6766 if (h != NULL
6767 && (h->ref_regular
6768 || h->def_regular))
6769 {
6770 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6771 return FALSE;
6772 }
6773
6774 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6775 if (s != NULL && s->linker_has_input)
6776 {
6777 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6778 if (! bfd_link_executable (info))
6779 {
6780 bfd *sub;
6781 asection *o;
6782
6783 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6784 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6785 && (o = sub->sections) != NULL
6786 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6787 for (o = sub->sections; o != NULL; o = o->next)
6788 if (elf_section_data (o)->this_hdr.sh_type
6789 == SHT_PREINIT_ARRAY)
6790 {
6791 _bfd_error_handler
6792 (_("%B: .preinit_array section is not allowed in DSO"),
6793 sub);
6794 break;
6795 }
6796
6797 bfd_set_error (bfd_error_nonrepresentable_section);
6798 return FALSE;
6799 }
6800
6801 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6802 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6803 return FALSE;
6804 }
6805 s = bfd_get_section_by_name (output_bfd, ".init_array");
6806 if (s != NULL && s->linker_has_input)
6807 {
6808 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6809 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6810 return FALSE;
6811 }
6812 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6813 if (s != NULL && s->linker_has_input)
6814 {
6815 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6816 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6817 return FALSE;
6818 }
6819
6820 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6821 /* If .dynstr is excluded from the link, we don't want any of
6822 these tags. Strictly, we should be checking each section
6823 individually; This quick check covers for the case where
6824 someone does a /DISCARD/ : { *(*) }. */
6825 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6826 {
6827 bfd_size_type strsize;
6828
6829 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6830 if ((info->emit_hash
6831 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6832 || (info->emit_gnu_hash
6833 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6834 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6835 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6836 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6837 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6838 bed->s->sizeof_sym))
6839 return FALSE;
6840 }
6841 }
6842
6843 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6844 return FALSE;
6845
6846 /* The backend must work out the sizes of all the other dynamic
6847 sections. */
6848 if (dynobj != NULL
6849 && bed->elf_backend_size_dynamic_sections != NULL
6850 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6851 return FALSE;
6852
6853 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6854 {
6855 if (elf_tdata (output_bfd)->cverdefs)
6856 {
6857 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6858
6859 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6860 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6861 return FALSE;
6862 }
6863
6864 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6865 {
6866 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6867 return FALSE;
6868 }
6869 else if (info->flags & DF_BIND_NOW)
6870 {
6871 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6872 return FALSE;
6873 }
6874
6875 if (info->flags_1)
6876 {
6877 if (bfd_link_executable (info))
6878 info->flags_1 &= ~ (DF_1_INITFIRST
6879 | DF_1_NODELETE
6880 | DF_1_NOOPEN);
6881 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6882 return FALSE;
6883 }
6884
6885 if (elf_tdata (output_bfd)->cverrefs)
6886 {
6887 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6888
6889 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6890 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6891 return FALSE;
6892 }
6893
6894 if ((elf_tdata (output_bfd)->cverrefs == 0
6895 && elf_tdata (output_bfd)->cverdefs == 0)
6896 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6897 {
6898 asection *s;
6899
6900 s = bfd_get_linker_section (dynobj, ".gnu.version");
6901 s->flags |= SEC_EXCLUDE;
6902 }
6903 }
6904 return TRUE;
6905 }
6906
6907 /* Find the first non-excluded output section. We'll use its
6908 section symbol for some emitted relocs. */
6909 void
6910 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6911 {
6912 asection *s;
6913
6914 for (s = output_bfd->sections; s != NULL; s = s->next)
6915 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6916 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6917 {
6918 elf_hash_table (info)->text_index_section = s;
6919 break;
6920 }
6921 }
6922
6923 /* Find two non-excluded output sections, one for code, one for data.
6924 We'll use their section symbols for some emitted relocs. */
6925 void
6926 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6927 {
6928 asection *s;
6929
6930 /* Data first, since setting text_index_section changes
6931 _bfd_elf_link_omit_section_dynsym. */
6932 for (s = output_bfd->sections; s != NULL; s = s->next)
6933 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6934 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6935 {
6936 elf_hash_table (info)->data_index_section = s;
6937 break;
6938 }
6939
6940 for (s = output_bfd->sections; s != NULL; s = s->next)
6941 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6942 == (SEC_ALLOC | SEC_READONLY))
6943 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6944 {
6945 elf_hash_table (info)->text_index_section = s;
6946 break;
6947 }
6948
6949 if (elf_hash_table (info)->text_index_section == NULL)
6950 elf_hash_table (info)->text_index_section
6951 = elf_hash_table (info)->data_index_section;
6952 }
6953
6954 bfd_boolean
6955 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6956 {
6957 const struct elf_backend_data *bed;
6958 unsigned long section_sym_count;
6959 bfd_size_type dynsymcount = 0;
6960
6961 if (!is_elf_hash_table (info->hash))
6962 return TRUE;
6963
6964 bed = get_elf_backend_data (output_bfd);
6965 (*bed->elf_backend_init_index_section) (output_bfd, info);
6966
6967 /* Assign dynsym indices. In a shared library we generate a section
6968 symbol for each output section, which come first. Next come all
6969 of the back-end allocated local dynamic syms, followed by the rest
6970 of the global symbols.
6971
6972 This is usually not needed for static binaries, however backends
6973 can request to always do it, e.g. the MIPS backend uses dynamic
6974 symbol counts to lay out GOT, which will be produced in the
6975 presence of GOT relocations even in static binaries (holding fixed
6976 data in that case, to satisfy those relocations). */
6977
6978 if (elf_hash_table (info)->dynamic_sections_created
6979 || bed->always_renumber_dynsyms)
6980 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6981 §ion_sym_count);
6982
6983 if (elf_hash_table (info)->dynamic_sections_created)
6984 {
6985 bfd *dynobj;
6986 asection *s;
6987 unsigned int dtagcount;
6988
6989 dynobj = elf_hash_table (info)->dynobj;
6990
6991 /* Work out the size of the symbol version section. */
6992 s = bfd_get_linker_section (dynobj, ".gnu.version");
6993 BFD_ASSERT (s != NULL);
6994 if ((s->flags & SEC_EXCLUDE) == 0)
6995 {
6996 s->size = dynsymcount * sizeof (Elf_External_Versym);
6997 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6998 if (s->contents == NULL)
6999 return FALSE;
7000
7001 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7002 return FALSE;
7003 }
7004
7005 /* Set the size of the .dynsym and .hash sections. We counted
7006 the number of dynamic symbols in elf_link_add_object_symbols.
7007 We will build the contents of .dynsym and .hash when we build
7008 the final symbol table, because until then we do not know the
7009 correct value to give the symbols. We built the .dynstr
7010 section as we went along in elf_link_add_object_symbols. */
7011 s = elf_hash_table (info)->dynsym;
7012 BFD_ASSERT (s != NULL);
7013 s->size = dynsymcount * bed->s->sizeof_sym;
7014
7015 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7016 if (s->contents == NULL)
7017 return FALSE;
7018
7019 /* The first entry in .dynsym is a dummy symbol. Clear all the
7020 section syms, in case we don't output them all. */
7021 ++section_sym_count;
7022 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7023
7024 elf_hash_table (info)->bucketcount = 0;
7025
7026 /* Compute the size of the hashing table. As a side effect this
7027 computes the hash values for all the names we export. */
7028 if (info->emit_hash)
7029 {
7030 unsigned long int *hashcodes;
7031 struct hash_codes_info hashinf;
7032 bfd_size_type amt;
7033 unsigned long int nsyms;
7034 size_t bucketcount;
7035 size_t hash_entry_size;
7036
7037 /* Compute the hash values for all exported symbols. At the same
7038 time store the values in an array so that we could use them for
7039 optimizations. */
7040 amt = dynsymcount * sizeof (unsigned long int);
7041 hashcodes = (unsigned long int *) bfd_malloc (amt);
7042 if (hashcodes == NULL)
7043 return FALSE;
7044 hashinf.hashcodes = hashcodes;
7045 hashinf.error = FALSE;
7046
7047 /* Put all hash values in HASHCODES. */
7048 elf_link_hash_traverse (elf_hash_table (info),
7049 elf_collect_hash_codes, &hashinf);
7050 if (hashinf.error)
7051 {
7052 free (hashcodes);
7053 return FALSE;
7054 }
7055
7056 nsyms = hashinf.hashcodes - hashcodes;
7057 bucketcount
7058 = compute_bucket_count (info, hashcodes, nsyms, 0);
7059 free (hashcodes);
7060
7061 if (bucketcount == 0 && nsyms > 0)
7062 return FALSE;
7063
7064 elf_hash_table (info)->bucketcount = bucketcount;
7065
7066 s = bfd_get_linker_section (dynobj, ".hash");
7067 BFD_ASSERT (s != NULL);
7068 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7069 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7070 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7071 if (s->contents == NULL)
7072 return FALSE;
7073
7074 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7075 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7076 s->contents + hash_entry_size);
7077 }
7078
7079 if (info->emit_gnu_hash)
7080 {
7081 size_t i, cnt;
7082 unsigned char *contents;
7083 struct collect_gnu_hash_codes cinfo;
7084 bfd_size_type amt;
7085 size_t bucketcount;
7086
7087 memset (&cinfo, 0, sizeof (cinfo));
7088
7089 /* Compute the hash values for all exported symbols. At the same
7090 time store the values in an array so that we could use them for
7091 optimizations. */
7092 amt = dynsymcount * 2 * sizeof (unsigned long int);
7093 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7094 if (cinfo.hashcodes == NULL)
7095 return FALSE;
7096
7097 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7098 cinfo.min_dynindx = -1;
7099 cinfo.output_bfd = output_bfd;
7100 cinfo.bed = bed;
7101
7102 /* Put all hash values in HASHCODES. */
7103 elf_link_hash_traverse (elf_hash_table (info),
7104 elf_collect_gnu_hash_codes, &cinfo);
7105 if (cinfo.error)
7106 {
7107 free (cinfo.hashcodes);
7108 return FALSE;
7109 }
7110
7111 bucketcount
7112 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7113
7114 if (bucketcount == 0)
7115 {
7116 free (cinfo.hashcodes);
7117 return FALSE;
7118 }
7119
7120 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7121 BFD_ASSERT (s != NULL);
7122
7123 if (cinfo.nsyms == 0)
7124 {
7125 /* Empty .gnu.hash section is special. */
7126 BFD_ASSERT (cinfo.min_dynindx == -1);
7127 free (cinfo.hashcodes);
7128 s->size = 5 * 4 + bed->s->arch_size / 8;
7129 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7130 if (contents == NULL)
7131 return FALSE;
7132 s->contents = contents;
7133 /* 1 empty bucket. */
7134 bfd_put_32 (output_bfd, 1, contents);
7135 /* SYMIDX above the special symbol 0. */
7136 bfd_put_32 (output_bfd, 1, contents + 4);
7137 /* Just one word for bitmask. */
7138 bfd_put_32 (output_bfd, 1, contents + 8);
7139 /* Only hash fn bloom filter. */
7140 bfd_put_32 (output_bfd, 0, contents + 12);
7141 /* No hashes are valid - empty bitmask. */
7142 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7143 /* No hashes in the only bucket. */
7144 bfd_put_32 (output_bfd, 0,
7145 contents + 16 + bed->s->arch_size / 8);
7146 }
7147 else
7148 {
7149 unsigned long int maskwords, maskbitslog2, x;
7150 BFD_ASSERT (cinfo.min_dynindx != -1);
7151
7152 x = cinfo.nsyms;
7153 maskbitslog2 = 1;
7154 while ((x >>= 1) != 0)
7155 ++maskbitslog2;
7156 if (maskbitslog2 < 3)
7157 maskbitslog2 = 5;
7158 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7159 maskbitslog2 = maskbitslog2 + 3;
7160 else
7161 maskbitslog2 = maskbitslog2 + 2;
7162 if (bed->s->arch_size == 64)
7163 {
7164 if (maskbitslog2 == 5)
7165 maskbitslog2 = 6;
7166 cinfo.shift1 = 6;
7167 }
7168 else
7169 cinfo.shift1 = 5;
7170 cinfo.mask = (1 << cinfo.shift1) - 1;
7171 cinfo.shift2 = maskbitslog2;
7172 cinfo.maskbits = 1 << maskbitslog2;
7173 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7174 amt = bucketcount * sizeof (unsigned long int) * 2;
7175 amt += maskwords * sizeof (bfd_vma);
7176 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7177 if (cinfo.bitmask == NULL)
7178 {
7179 free (cinfo.hashcodes);
7180 return FALSE;
7181 }
7182
7183 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7184 cinfo.indx = cinfo.counts + bucketcount;
7185 cinfo.symindx = dynsymcount - cinfo.nsyms;
7186 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7187
7188 /* Determine how often each hash bucket is used. */
7189 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7190 for (i = 0; i < cinfo.nsyms; ++i)
7191 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7192
7193 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7194 if (cinfo.counts[i] != 0)
7195 {
7196 cinfo.indx[i] = cnt;
7197 cnt += cinfo.counts[i];
7198 }
7199 BFD_ASSERT (cnt == dynsymcount);
7200 cinfo.bucketcount = bucketcount;
7201 cinfo.local_indx = cinfo.min_dynindx;
7202
7203 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7204 s->size += cinfo.maskbits / 8;
7205 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7206 if (contents == NULL)
7207 {
7208 free (cinfo.bitmask);
7209 free (cinfo.hashcodes);
7210 return FALSE;
7211 }
7212
7213 s->contents = contents;
7214 bfd_put_32 (output_bfd, bucketcount, contents);
7215 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7216 bfd_put_32 (output_bfd, maskwords, contents + 8);
7217 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7218 contents += 16 + cinfo.maskbits / 8;
7219
7220 for (i = 0; i < bucketcount; ++i)
7221 {
7222 if (cinfo.counts[i] == 0)
7223 bfd_put_32 (output_bfd, 0, contents);
7224 else
7225 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7226 contents += 4;
7227 }
7228
7229 cinfo.contents = contents;
7230
7231 /* Renumber dynamic symbols, populate .gnu.hash section. */
7232 elf_link_hash_traverse (elf_hash_table (info),
7233 elf_renumber_gnu_hash_syms, &cinfo);
7234
7235 contents = s->contents + 16;
7236 for (i = 0; i < maskwords; ++i)
7237 {
7238 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7239 contents);
7240 contents += bed->s->arch_size / 8;
7241 }
7242
7243 free (cinfo.bitmask);
7244 free (cinfo.hashcodes);
7245 }
7246 }
7247
7248 s = bfd_get_linker_section (dynobj, ".dynstr");
7249 BFD_ASSERT (s != NULL);
7250
7251 elf_finalize_dynstr (output_bfd, info);
7252
7253 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7254
7255 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7256 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7257 return FALSE;
7258 }
7259
7260 return TRUE;
7261 }
7262
7263 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7265
7266 static void
7267 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7268 asection *sec)
7269 {
7270 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7271 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7272 }
7273
7274 /* Finish SHF_MERGE section merging. */
7275
7276 bfd_boolean
7277 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7278 {
7279 bfd *ibfd;
7280 asection *sec;
7281
7282 if (!is_elf_hash_table (info->hash))
7283 return FALSE;
7284
7285 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7286 if ((ibfd->flags & DYNAMIC) == 0
7287 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7288 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7289 == get_elf_backend_data (obfd)->s->elfclass))
7290 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7291 if ((sec->flags & SEC_MERGE) != 0
7292 && !bfd_is_abs_section (sec->output_section))
7293 {
7294 struct bfd_elf_section_data *secdata;
7295
7296 secdata = elf_section_data (sec);
7297 if (! _bfd_add_merge_section (obfd,
7298 &elf_hash_table (info)->merge_info,
7299 sec, &secdata->sec_info))
7300 return FALSE;
7301 else if (secdata->sec_info)
7302 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7303 }
7304
7305 if (elf_hash_table (info)->merge_info != NULL)
7306 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7307 merge_sections_remove_hook);
7308 return TRUE;
7309 }
7310
7311 /* Create an entry in an ELF linker hash table. */
7312
7313 struct bfd_hash_entry *
7314 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7315 struct bfd_hash_table *table,
7316 const char *string)
7317 {
7318 /* Allocate the structure if it has not already been allocated by a
7319 subclass. */
7320 if (entry == NULL)
7321 {
7322 entry = (struct bfd_hash_entry *)
7323 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7324 if (entry == NULL)
7325 return entry;
7326 }
7327
7328 /* Call the allocation method of the superclass. */
7329 entry = _bfd_link_hash_newfunc (entry, table, string);
7330 if (entry != NULL)
7331 {
7332 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7333 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7334
7335 /* Set local fields. */
7336 ret->indx = -1;
7337 ret->dynindx = -1;
7338 ret->got = htab->init_got_refcount;
7339 ret->plt = htab->init_plt_refcount;
7340 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7341 - offsetof (struct elf_link_hash_entry, size)));
7342 /* Assume that we have been called by a non-ELF symbol reader.
7343 This flag is then reset by the code which reads an ELF input
7344 file. This ensures that a symbol created by a non-ELF symbol
7345 reader will have the flag set correctly. */
7346 ret->non_elf = 1;
7347 }
7348
7349 return entry;
7350 }
7351
7352 /* Copy data from an indirect symbol to its direct symbol, hiding the
7353 old indirect symbol. Also used for copying flags to a weakdef. */
7354
7355 void
7356 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7357 struct elf_link_hash_entry *dir,
7358 struct elf_link_hash_entry *ind)
7359 {
7360 struct elf_link_hash_table *htab;
7361
7362 /* Copy down any references that we may have already seen to the
7363 symbol which just became indirect. */
7364
7365 if (dir->versioned != versioned_hidden)
7366 dir->ref_dynamic |= ind->ref_dynamic;
7367 dir->ref_regular |= ind->ref_regular;
7368 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7369 dir->non_got_ref |= ind->non_got_ref;
7370 dir->needs_plt |= ind->needs_plt;
7371 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7372
7373 if (ind->root.type != bfd_link_hash_indirect)
7374 return;
7375
7376 /* Copy over the global and procedure linkage table refcount entries.
7377 These may have been already set up by a check_relocs routine. */
7378 htab = elf_hash_table (info);
7379 if (ind->got.refcount > htab->init_got_refcount.refcount)
7380 {
7381 if (dir->got.refcount < 0)
7382 dir->got.refcount = 0;
7383 dir->got.refcount += ind->got.refcount;
7384 ind->got.refcount = htab->init_got_refcount.refcount;
7385 }
7386
7387 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7388 {
7389 if (dir->plt.refcount < 0)
7390 dir->plt.refcount = 0;
7391 dir->plt.refcount += ind->plt.refcount;
7392 ind->plt.refcount = htab->init_plt_refcount.refcount;
7393 }
7394
7395 if (ind->dynindx != -1)
7396 {
7397 if (dir->dynindx != -1)
7398 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7399 dir->dynindx = ind->dynindx;
7400 dir->dynstr_index = ind->dynstr_index;
7401 ind->dynindx = -1;
7402 ind->dynstr_index = 0;
7403 }
7404 }
7405
7406 void
7407 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7408 struct elf_link_hash_entry *h,
7409 bfd_boolean force_local)
7410 {
7411 /* STT_GNU_IFUNC symbol must go through PLT. */
7412 if (h->type != STT_GNU_IFUNC)
7413 {
7414 h->plt = elf_hash_table (info)->init_plt_offset;
7415 h->needs_plt = 0;
7416 }
7417 if (force_local)
7418 {
7419 h->forced_local = 1;
7420 if (h->dynindx != -1)
7421 {
7422 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7423 h->dynstr_index);
7424 h->dynindx = -1;
7425 h->dynstr_index = 0;
7426 }
7427 }
7428 }
7429
7430 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7431 caller. */
7432
7433 bfd_boolean
7434 _bfd_elf_link_hash_table_init
7435 (struct elf_link_hash_table *table,
7436 bfd *abfd,
7437 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7438 struct bfd_hash_table *,
7439 const char *),
7440 unsigned int entsize,
7441 enum elf_target_id target_id)
7442 {
7443 bfd_boolean ret;
7444 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7445
7446 table->init_got_refcount.refcount = can_refcount - 1;
7447 table->init_plt_refcount.refcount = can_refcount - 1;
7448 table->init_got_offset.offset = -(bfd_vma) 1;
7449 table->init_plt_offset.offset = -(bfd_vma) 1;
7450 /* The first dynamic symbol is a dummy. */
7451 table->dynsymcount = 1;
7452
7453 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7454
7455 table->root.type = bfd_link_elf_hash_table;
7456 table->hash_table_id = target_id;
7457
7458 return ret;
7459 }
7460
7461 /* Create an ELF linker hash table. */
7462
7463 struct bfd_link_hash_table *
7464 _bfd_elf_link_hash_table_create (bfd *abfd)
7465 {
7466 struct elf_link_hash_table *ret;
7467 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7468
7469 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7470 if (ret == NULL)
7471 return NULL;
7472
7473 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7474 sizeof (struct elf_link_hash_entry),
7475 GENERIC_ELF_DATA))
7476 {
7477 free (ret);
7478 return NULL;
7479 }
7480 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7481
7482 return &ret->root;
7483 }
7484
7485 /* Destroy an ELF linker hash table. */
7486
7487 void
7488 _bfd_elf_link_hash_table_free (bfd *obfd)
7489 {
7490 struct elf_link_hash_table *htab;
7491
7492 htab = (struct elf_link_hash_table *) obfd->link.hash;
7493 if (htab->dynstr != NULL)
7494 _bfd_elf_strtab_free (htab->dynstr);
7495 _bfd_merge_sections_free (htab->merge_info);
7496 _bfd_generic_link_hash_table_free (obfd);
7497 }
7498
7499 /* This is a hook for the ELF emulation code in the generic linker to
7500 tell the backend linker what file name to use for the DT_NEEDED
7501 entry for a dynamic object. */
7502
7503 void
7504 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7505 {
7506 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7507 && bfd_get_format (abfd) == bfd_object)
7508 elf_dt_name (abfd) = name;
7509 }
7510
7511 int
7512 bfd_elf_get_dyn_lib_class (bfd *abfd)
7513 {
7514 int lib_class;
7515 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7516 && bfd_get_format (abfd) == bfd_object)
7517 lib_class = elf_dyn_lib_class (abfd);
7518 else
7519 lib_class = 0;
7520 return lib_class;
7521 }
7522
7523 void
7524 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7525 {
7526 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7527 && bfd_get_format (abfd) == bfd_object)
7528 elf_dyn_lib_class (abfd) = lib_class;
7529 }
7530
7531 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7532 the linker ELF emulation code. */
7533
7534 struct bfd_link_needed_list *
7535 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7536 struct bfd_link_info *info)
7537 {
7538 if (! is_elf_hash_table (info->hash))
7539 return NULL;
7540 return elf_hash_table (info)->needed;
7541 }
7542
7543 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7544 hook for the linker ELF emulation code. */
7545
7546 struct bfd_link_needed_list *
7547 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7548 struct bfd_link_info *info)
7549 {
7550 if (! is_elf_hash_table (info->hash))
7551 return NULL;
7552 return elf_hash_table (info)->runpath;
7553 }
7554
7555 /* Get the name actually used for a dynamic object for a link. This
7556 is the SONAME entry if there is one. Otherwise, it is the string
7557 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7558
7559 const char *
7560 bfd_elf_get_dt_soname (bfd *abfd)
7561 {
7562 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7563 && bfd_get_format (abfd) == bfd_object)
7564 return elf_dt_name (abfd);
7565 return NULL;
7566 }
7567
7568 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7569 the ELF linker emulation code. */
7570
7571 bfd_boolean
7572 bfd_elf_get_bfd_needed_list (bfd *abfd,
7573 struct bfd_link_needed_list **pneeded)
7574 {
7575 asection *s;
7576 bfd_byte *dynbuf = NULL;
7577 unsigned int elfsec;
7578 unsigned long shlink;
7579 bfd_byte *extdyn, *extdynend;
7580 size_t extdynsize;
7581 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7582
7583 *pneeded = NULL;
7584
7585 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7586 || bfd_get_format (abfd) != bfd_object)
7587 return TRUE;
7588
7589 s = bfd_get_section_by_name (abfd, ".dynamic");
7590 if (s == NULL || s->size == 0)
7591 return TRUE;
7592
7593 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7594 goto error_return;
7595
7596 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7597 if (elfsec == SHN_BAD)
7598 goto error_return;
7599
7600 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7601
7602 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7603 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7604
7605 extdyn = dynbuf;
7606 extdynend = extdyn + s->size;
7607 for (; extdyn < extdynend; extdyn += extdynsize)
7608 {
7609 Elf_Internal_Dyn dyn;
7610
7611 (*swap_dyn_in) (abfd, extdyn, &dyn);
7612
7613 if (dyn.d_tag == DT_NULL)
7614 break;
7615
7616 if (dyn.d_tag == DT_NEEDED)
7617 {
7618 const char *string;
7619 struct bfd_link_needed_list *l;
7620 unsigned int tagv = dyn.d_un.d_val;
7621 bfd_size_type amt;
7622
7623 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7624 if (string == NULL)
7625 goto error_return;
7626
7627 amt = sizeof *l;
7628 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7629 if (l == NULL)
7630 goto error_return;
7631
7632 l->by = abfd;
7633 l->name = string;
7634 l->next = *pneeded;
7635 *pneeded = l;
7636 }
7637 }
7638
7639 free (dynbuf);
7640
7641 return TRUE;
7642
7643 error_return:
7644 if (dynbuf != NULL)
7645 free (dynbuf);
7646 return FALSE;
7647 }
7648
7649 struct elf_symbuf_symbol
7650 {
7651 unsigned long st_name; /* Symbol name, index in string tbl */
7652 unsigned char st_info; /* Type and binding attributes */
7653 unsigned char st_other; /* Visibilty, and target specific */
7654 };
7655
7656 struct elf_symbuf_head
7657 {
7658 struct elf_symbuf_symbol *ssym;
7659 size_t count;
7660 unsigned int st_shndx;
7661 };
7662
7663 struct elf_symbol
7664 {
7665 union
7666 {
7667 Elf_Internal_Sym *isym;
7668 struct elf_symbuf_symbol *ssym;
7669 } u;
7670 const char *name;
7671 };
7672
7673 /* Sort references to symbols by ascending section number. */
7674
7675 static int
7676 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7677 {
7678 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7679 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7680
7681 return s1->st_shndx - s2->st_shndx;
7682 }
7683
7684 static int
7685 elf_sym_name_compare (const void *arg1, const void *arg2)
7686 {
7687 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7688 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7689 return strcmp (s1->name, s2->name);
7690 }
7691
7692 static struct elf_symbuf_head *
7693 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7694 {
7695 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7696 struct elf_symbuf_symbol *ssym;
7697 struct elf_symbuf_head *ssymbuf, *ssymhead;
7698 size_t i, shndx_count, total_size;
7699
7700 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7701 if (indbuf == NULL)
7702 return NULL;
7703
7704 for (ind = indbuf, i = 0; i < symcount; i++)
7705 if (isymbuf[i].st_shndx != SHN_UNDEF)
7706 *ind++ = &isymbuf[i];
7707 indbufend = ind;
7708
7709 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7710 elf_sort_elf_symbol);
7711
7712 shndx_count = 0;
7713 if (indbufend > indbuf)
7714 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7715 if (ind[0]->st_shndx != ind[1]->st_shndx)
7716 shndx_count++;
7717
7718 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7719 + (indbufend - indbuf) * sizeof (*ssym));
7720 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7721 if (ssymbuf == NULL)
7722 {
7723 free (indbuf);
7724 return NULL;
7725 }
7726
7727 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7728 ssymbuf->ssym = NULL;
7729 ssymbuf->count = shndx_count;
7730 ssymbuf->st_shndx = 0;
7731 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7732 {
7733 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7734 {
7735 ssymhead++;
7736 ssymhead->ssym = ssym;
7737 ssymhead->count = 0;
7738 ssymhead->st_shndx = (*ind)->st_shndx;
7739 }
7740 ssym->st_name = (*ind)->st_name;
7741 ssym->st_info = (*ind)->st_info;
7742 ssym->st_other = (*ind)->st_other;
7743 ssymhead->count++;
7744 }
7745 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7746 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7747 == total_size));
7748
7749 free (indbuf);
7750 return ssymbuf;
7751 }
7752
7753 /* Check if 2 sections define the same set of local and global
7754 symbols. */
7755
7756 static bfd_boolean
7757 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7758 struct bfd_link_info *info)
7759 {
7760 bfd *bfd1, *bfd2;
7761 const struct elf_backend_data *bed1, *bed2;
7762 Elf_Internal_Shdr *hdr1, *hdr2;
7763 size_t symcount1, symcount2;
7764 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7765 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7766 Elf_Internal_Sym *isym, *isymend;
7767 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7768 size_t count1, count2, i;
7769 unsigned int shndx1, shndx2;
7770 bfd_boolean result;
7771
7772 bfd1 = sec1->owner;
7773 bfd2 = sec2->owner;
7774
7775 /* Both sections have to be in ELF. */
7776 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7777 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7778 return FALSE;
7779
7780 if (elf_section_type (sec1) != elf_section_type (sec2))
7781 return FALSE;
7782
7783 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7784 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7785 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7786 return FALSE;
7787
7788 bed1 = get_elf_backend_data (bfd1);
7789 bed2 = get_elf_backend_data (bfd2);
7790 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7791 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7792 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7793 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7794
7795 if (symcount1 == 0 || symcount2 == 0)
7796 return FALSE;
7797
7798 result = FALSE;
7799 isymbuf1 = NULL;
7800 isymbuf2 = NULL;
7801 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7802 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7803
7804 if (ssymbuf1 == NULL)
7805 {
7806 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7807 NULL, NULL, NULL);
7808 if (isymbuf1 == NULL)
7809 goto done;
7810
7811 if (!info->reduce_memory_overheads)
7812 elf_tdata (bfd1)->symbuf = ssymbuf1
7813 = elf_create_symbuf (symcount1, isymbuf1);
7814 }
7815
7816 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7817 {
7818 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7819 NULL, NULL, NULL);
7820 if (isymbuf2 == NULL)
7821 goto done;
7822
7823 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7824 elf_tdata (bfd2)->symbuf = ssymbuf2
7825 = elf_create_symbuf (symcount2, isymbuf2);
7826 }
7827
7828 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7829 {
7830 /* Optimized faster version. */
7831 size_t lo, hi, mid;
7832 struct elf_symbol *symp;
7833 struct elf_symbuf_symbol *ssym, *ssymend;
7834
7835 lo = 0;
7836 hi = ssymbuf1->count;
7837 ssymbuf1++;
7838 count1 = 0;
7839 while (lo < hi)
7840 {
7841 mid = (lo + hi) / 2;
7842 if (shndx1 < ssymbuf1[mid].st_shndx)
7843 hi = mid;
7844 else if (shndx1 > ssymbuf1[mid].st_shndx)
7845 lo = mid + 1;
7846 else
7847 {
7848 count1 = ssymbuf1[mid].count;
7849 ssymbuf1 += mid;
7850 break;
7851 }
7852 }
7853
7854 lo = 0;
7855 hi = ssymbuf2->count;
7856 ssymbuf2++;
7857 count2 = 0;
7858 while (lo < hi)
7859 {
7860 mid = (lo + hi) / 2;
7861 if (shndx2 < ssymbuf2[mid].st_shndx)
7862 hi = mid;
7863 else if (shndx2 > ssymbuf2[mid].st_shndx)
7864 lo = mid + 1;
7865 else
7866 {
7867 count2 = ssymbuf2[mid].count;
7868 ssymbuf2 += mid;
7869 break;
7870 }
7871 }
7872
7873 if (count1 == 0 || count2 == 0 || count1 != count2)
7874 goto done;
7875
7876 symtable1
7877 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7878 symtable2
7879 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7880 if (symtable1 == NULL || symtable2 == NULL)
7881 goto done;
7882
7883 symp = symtable1;
7884 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7885 ssym < ssymend; ssym++, symp++)
7886 {
7887 symp->u.ssym = ssym;
7888 symp->name = bfd_elf_string_from_elf_section (bfd1,
7889 hdr1->sh_link,
7890 ssym->st_name);
7891 }
7892
7893 symp = symtable2;
7894 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7895 ssym < ssymend; ssym++, symp++)
7896 {
7897 symp->u.ssym = ssym;
7898 symp->name = bfd_elf_string_from_elf_section (bfd2,
7899 hdr2->sh_link,
7900 ssym->st_name);
7901 }
7902
7903 /* Sort symbol by name. */
7904 qsort (symtable1, count1, sizeof (struct elf_symbol),
7905 elf_sym_name_compare);
7906 qsort (symtable2, count1, sizeof (struct elf_symbol),
7907 elf_sym_name_compare);
7908
7909 for (i = 0; i < count1; i++)
7910 /* Two symbols must have the same binding, type and name. */
7911 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7912 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7913 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7914 goto done;
7915
7916 result = TRUE;
7917 goto done;
7918 }
7919
7920 symtable1 = (struct elf_symbol *)
7921 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7922 symtable2 = (struct elf_symbol *)
7923 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7924 if (symtable1 == NULL || symtable2 == NULL)
7925 goto done;
7926
7927 /* Count definitions in the section. */
7928 count1 = 0;
7929 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7930 if (isym->st_shndx == shndx1)
7931 symtable1[count1++].u.isym = isym;
7932
7933 count2 = 0;
7934 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7935 if (isym->st_shndx == shndx2)
7936 symtable2[count2++].u.isym = isym;
7937
7938 if (count1 == 0 || count2 == 0 || count1 != count2)
7939 goto done;
7940
7941 for (i = 0; i < count1; i++)
7942 symtable1[i].name
7943 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7944 symtable1[i].u.isym->st_name);
7945
7946 for (i = 0; i < count2; i++)
7947 symtable2[i].name
7948 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7949 symtable2[i].u.isym->st_name);
7950
7951 /* Sort symbol by name. */
7952 qsort (symtable1, count1, sizeof (struct elf_symbol),
7953 elf_sym_name_compare);
7954 qsort (symtable2, count1, sizeof (struct elf_symbol),
7955 elf_sym_name_compare);
7956
7957 for (i = 0; i < count1; i++)
7958 /* Two symbols must have the same binding, type and name. */
7959 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7960 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7961 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7962 goto done;
7963
7964 result = TRUE;
7965
7966 done:
7967 if (symtable1)
7968 free (symtable1);
7969 if (symtable2)
7970 free (symtable2);
7971 if (isymbuf1)
7972 free (isymbuf1);
7973 if (isymbuf2)
7974 free (isymbuf2);
7975
7976 return result;
7977 }
7978
7979 /* Return TRUE if 2 section types are compatible. */
7980
7981 bfd_boolean
7982 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7983 bfd *bbfd, const asection *bsec)
7984 {
7985 if (asec == NULL
7986 || bsec == NULL
7987 || abfd->xvec->flavour != bfd_target_elf_flavour
7988 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7989 return TRUE;
7990
7991 return elf_section_type (asec) == elf_section_type (bsec);
7992 }
7993
7994 /* Final phase of ELF linker. */
7996
7997 /* A structure we use to avoid passing large numbers of arguments. */
7998
7999 struct elf_final_link_info
8000 {
8001 /* General link information. */
8002 struct bfd_link_info *info;
8003 /* Output BFD. */
8004 bfd *output_bfd;
8005 /* Symbol string table. */
8006 struct elf_strtab_hash *symstrtab;
8007 /* .hash section. */
8008 asection *hash_sec;
8009 /* symbol version section (.gnu.version). */
8010 asection *symver_sec;
8011 /* Buffer large enough to hold contents of any section. */
8012 bfd_byte *contents;
8013 /* Buffer large enough to hold external relocs of any section. */
8014 void *external_relocs;
8015 /* Buffer large enough to hold internal relocs of any section. */
8016 Elf_Internal_Rela *internal_relocs;
8017 /* Buffer large enough to hold external local symbols of any input
8018 BFD. */
8019 bfd_byte *external_syms;
8020 /* And a buffer for symbol section indices. */
8021 Elf_External_Sym_Shndx *locsym_shndx;
8022 /* Buffer large enough to hold internal local symbols of any input
8023 BFD. */
8024 Elf_Internal_Sym *internal_syms;
8025 /* Array large enough to hold a symbol index for each local symbol
8026 of any input BFD. */
8027 long *indices;
8028 /* Array large enough to hold a section pointer for each local
8029 symbol of any input BFD. */
8030 asection **sections;
8031 /* Buffer for SHT_SYMTAB_SHNDX section. */
8032 Elf_External_Sym_Shndx *symshndxbuf;
8033 /* Number of STT_FILE syms seen. */
8034 size_t filesym_count;
8035 };
8036
8037 /* This struct is used to pass information to elf_link_output_extsym. */
8038
8039 struct elf_outext_info
8040 {
8041 bfd_boolean failed;
8042 bfd_boolean localsyms;
8043 bfd_boolean file_sym_done;
8044 struct elf_final_link_info *flinfo;
8045 };
8046
8047
8048 /* Support for evaluating a complex relocation.
8049
8050 Complex relocations are generalized, self-describing relocations. The
8051 implementation of them consists of two parts: complex symbols, and the
8052 relocations themselves.
8053
8054 The relocations are use a reserved elf-wide relocation type code (R_RELC
8055 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8056 information (start bit, end bit, word width, etc) into the addend. This
8057 information is extracted from CGEN-generated operand tables within gas.
8058
8059 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8060 internal) representing prefix-notation expressions, including but not
8061 limited to those sorts of expressions normally encoded as addends in the
8062 addend field. The symbol mangling format is:
8063
8064 <node> := <literal>
8065 | <unary-operator> ':' <node>
8066 | <binary-operator> ':' <node> ':' <node>
8067 ;
8068
8069 <literal> := 's' <digits=N> ':' <N character symbol name>
8070 | 'S' <digits=N> ':' <N character section name>
8071 | '#' <hexdigits>
8072 ;
8073
8074 <binary-operator> := as in C
8075 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8076
8077 static void
8078 set_symbol_value (bfd *bfd_with_globals,
8079 Elf_Internal_Sym *isymbuf,
8080 size_t locsymcount,
8081 size_t symidx,
8082 bfd_vma val)
8083 {
8084 struct elf_link_hash_entry **sym_hashes;
8085 struct elf_link_hash_entry *h;
8086 size_t extsymoff = locsymcount;
8087
8088 if (symidx < locsymcount)
8089 {
8090 Elf_Internal_Sym *sym;
8091
8092 sym = isymbuf + symidx;
8093 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8094 {
8095 /* It is a local symbol: move it to the
8096 "absolute" section and give it a value. */
8097 sym->st_shndx = SHN_ABS;
8098 sym->st_value = val;
8099 return;
8100 }
8101 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8102 extsymoff = 0;
8103 }
8104
8105 /* It is a global symbol: set its link type
8106 to "defined" and give it a value. */
8107
8108 sym_hashes = elf_sym_hashes (bfd_with_globals);
8109 h = sym_hashes [symidx - extsymoff];
8110 while (h->root.type == bfd_link_hash_indirect
8111 || h->root.type == bfd_link_hash_warning)
8112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8113 h->root.type = bfd_link_hash_defined;
8114 h->root.u.def.value = val;
8115 h->root.u.def.section = bfd_abs_section_ptr;
8116 }
8117
8118 static bfd_boolean
8119 resolve_symbol (const char *name,
8120 bfd *input_bfd,
8121 struct elf_final_link_info *flinfo,
8122 bfd_vma *result,
8123 Elf_Internal_Sym *isymbuf,
8124 size_t locsymcount)
8125 {
8126 Elf_Internal_Sym *sym;
8127 struct bfd_link_hash_entry *global_entry;
8128 const char *candidate = NULL;
8129 Elf_Internal_Shdr *symtab_hdr;
8130 size_t i;
8131
8132 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8133
8134 for (i = 0; i < locsymcount; ++ i)
8135 {
8136 sym = isymbuf + i;
8137
8138 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8139 continue;
8140
8141 candidate = bfd_elf_string_from_elf_section (input_bfd,
8142 symtab_hdr->sh_link,
8143 sym->st_name);
8144 #ifdef DEBUG
8145 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8146 name, candidate, (unsigned long) sym->st_value);
8147 #endif
8148 if (candidate && strcmp (candidate, name) == 0)
8149 {
8150 asection *sec = flinfo->sections [i];
8151
8152 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8153 *result += sec->output_offset + sec->output_section->vma;
8154 #ifdef DEBUG
8155 printf ("Found symbol with value %8.8lx\n",
8156 (unsigned long) *result);
8157 #endif
8158 return TRUE;
8159 }
8160 }
8161
8162 /* Hmm, haven't found it yet. perhaps it is a global. */
8163 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8164 FALSE, FALSE, TRUE);
8165 if (!global_entry)
8166 return FALSE;
8167
8168 if (global_entry->type == bfd_link_hash_defined
8169 || global_entry->type == bfd_link_hash_defweak)
8170 {
8171 *result = (global_entry->u.def.value
8172 + global_entry->u.def.section->output_section->vma
8173 + global_entry->u.def.section->output_offset);
8174 #ifdef DEBUG
8175 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8176 global_entry->root.string, (unsigned long) *result);
8177 #endif
8178 return TRUE;
8179 }
8180
8181 return FALSE;
8182 }
8183
8184 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8185 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8186 names like "foo.end" which is the end address of section "foo". */
8187
8188 static bfd_boolean
8189 resolve_section (const char *name,
8190 asection *sections,
8191 bfd_vma *result,
8192 bfd * abfd)
8193 {
8194 asection *curr;
8195 unsigned int len;
8196
8197 for (curr = sections; curr; curr = curr->next)
8198 if (strcmp (curr->name, name) == 0)
8199 {
8200 *result = curr->vma;
8201 return TRUE;
8202 }
8203
8204 /* Hmm. still haven't found it. try pseudo-section names. */
8205 /* FIXME: This could be coded more efficiently... */
8206 for (curr = sections; curr; curr = curr->next)
8207 {
8208 len = strlen (curr->name);
8209 if (len > strlen (name))
8210 continue;
8211
8212 if (strncmp (curr->name, name, len) == 0)
8213 {
8214 if (strncmp (".end", name + len, 4) == 0)
8215 {
8216 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8217 return TRUE;
8218 }
8219
8220 /* Insert more pseudo-section names here, if you like. */
8221 }
8222 }
8223
8224 return FALSE;
8225 }
8226
8227 static void
8228 undefined_reference (const char *reftype, const char *name)
8229 {
8230 /* xgettext:c-format */
8231 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8232 reftype, name);
8233 }
8234
8235 static bfd_boolean
8236 eval_symbol (bfd_vma *result,
8237 const char **symp,
8238 bfd *input_bfd,
8239 struct elf_final_link_info *flinfo,
8240 bfd_vma dot,
8241 Elf_Internal_Sym *isymbuf,
8242 size_t locsymcount,
8243 int signed_p)
8244 {
8245 size_t len;
8246 size_t symlen;
8247 bfd_vma a;
8248 bfd_vma b;
8249 char symbuf[4096];
8250 const char *sym = *symp;
8251 const char *symend;
8252 bfd_boolean symbol_is_section = FALSE;
8253
8254 len = strlen (sym);
8255 symend = sym + len;
8256
8257 if (len < 1 || len > sizeof (symbuf))
8258 {
8259 bfd_set_error (bfd_error_invalid_operation);
8260 return FALSE;
8261 }
8262
8263 switch (* sym)
8264 {
8265 case '.':
8266 *result = dot;
8267 *symp = sym + 1;
8268 return TRUE;
8269
8270 case '#':
8271 ++sym;
8272 *result = strtoul (sym, (char **) symp, 16);
8273 return TRUE;
8274
8275 case 'S':
8276 symbol_is_section = TRUE;
8277 /* Fall through. */
8278 case 's':
8279 ++sym;
8280 symlen = strtol (sym, (char **) symp, 10);
8281 sym = *symp + 1; /* Skip the trailing ':'. */
8282
8283 if (symend < sym || symlen + 1 > sizeof (symbuf))
8284 {
8285 bfd_set_error (bfd_error_invalid_operation);
8286 return FALSE;
8287 }
8288
8289 memcpy (symbuf, sym, symlen);
8290 symbuf[symlen] = '\0';
8291 *symp = sym + symlen;
8292
8293 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8294 the symbol as a section, or vice-versa. so we're pretty liberal in our
8295 interpretation here; section means "try section first", not "must be a
8296 section", and likewise with symbol. */
8297
8298 if (symbol_is_section)
8299 {
8300 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8301 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8302 isymbuf, locsymcount))
8303 {
8304 undefined_reference ("section", symbuf);
8305 return FALSE;
8306 }
8307 }
8308 else
8309 {
8310 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8311 isymbuf, locsymcount)
8312 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8313 result, input_bfd))
8314 {
8315 undefined_reference ("symbol", symbuf);
8316 return FALSE;
8317 }
8318 }
8319
8320 return TRUE;
8321
8322 /* All that remains are operators. */
8323
8324 #define UNARY_OP(op) \
8325 if (strncmp (sym, #op, strlen (#op)) == 0) \
8326 { \
8327 sym += strlen (#op); \
8328 if (*sym == ':') \
8329 ++sym; \
8330 *symp = sym; \
8331 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8332 isymbuf, locsymcount, signed_p)) \
8333 return FALSE; \
8334 if (signed_p) \
8335 *result = op ((bfd_signed_vma) a); \
8336 else \
8337 *result = op a; \
8338 return TRUE; \
8339 }
8340
8341 #define BINARY_OP(op) \
8342 if (strncmp (sym, #op, strlen (#op)) == 0) \
8343 { \
8344 sym += strlen (#op); \
8345 if (*sym == ':') \
8346 ++sym; \
8347 *symp = sym; \
8348 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8349 isymbuf, locsymcount, signed_p)) \
8350 return FALSE; \
8351 ++*symp; \
8352 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8353 isymbuf, locsymcount, signed_p)) \
8354 return FALSE; \
8355 if (signed_p) \
8356 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8357 else \
8358 *result = a op b; \
8359 return TRUE; \
8360 }
8361
8362 default:
8363 UNARY_OP (0-);
8364 BINARY_OP (<<);
8365 BINARY_OP (>>);
8366 BINARY_OP (==);
8367 BINARY_OP (!=);
8368 BINARY_OP (<=);
8369 BINARY_OP (>=);
8370 BINARY_OP (&&);
8371 BINARY_OP (||);
8372 UNARY_OP (~);
8373 UNARY_OP (!);
8374 BINARY_OP (*);
8375 BINARY_OP (/);
8376 BINARY_OP (%);
8377 BINARY_OP (^);
8378 BINARY_OP (|);
8379 BINARY_OP (&);
8380 BINARY_OP (+);
8381 BINARY_OP (-);
8382 BINARY_OP (<);
8383 BINARY_OP (>);
8384 #undef UNARY_OP
8385 #undef BINARY_OP
8386 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8387 bfd_set_error (bfd_error_invalid_operation);
8388 return FALSE;
8389 }
8390 }
8391
8392 static void
8393 put_value (bfd_vma size,
8394 unsigned long chunksz,
8395 bfd *input_bfd,
8396 bfd_vma x,
8397 bfd_byte *location)
8398 {
8399 location += (size - chunksz);
8400
8401 for (; size; size -= chunksz, location -= chunksz)
8402 {
8403 switch (chunksz)
8404 {
8405 case 1:
8406 bfd_put_8 (input_bfd, x, location);
8407 x >>= 8;
8408 break;
8409 case 2:
8410 bfd_put_16 (input_bfd, x, location);
8411 x >>= 16;
8412 break;
8413 case 4:
8414 bfd_put_32 (input_bfd, x, location);
8415 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8416 x >>= 16;
8417 x >>= 16;
8418 break;
8419 #ifdef BFD64
8420 case 8:
8421 bfd_put_64 (input_bfd, x, location);
8422 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8423 x >>= 32;
8424 x >>= 32;
8425 break;
8426 #endif
8427 default:
8428 abort ();
8429 break;
8430 }
8431 }
8432 }
8433
8434 static bfd_vma
8435 get_value (bfd_vma size,
8436 unsigned long chunksz,
8437 bfd *input_bfd,
8438 bfd_byte *location)
8439 {
8440 int shift;
8441 bfd_vma x = 0;
8442
8443 /* Sanity checks. */
8444 BFD_ASSERT (chunksz <= sizeof (x)
8445 && size >= chunksz
8446 && chunksz != 0
8447 && (size % chunksz) == 0
8448 && input_bfd != NULL
8449 && location != NULL);
8450
8451 if (chunksz == sizeof (x))
8452 {
8453 BFD_ASSERT (size == chunksz);
8454
8455 /* Make sure that we do not perform an undefined shift operation.
8456 We know that size == chunksz so there will only be one iteration
8457 of the loop below. */
8458 shift = 0;
8459 }
8460 else
8461 shift = 8 * chunksz;
8462
8463 for (; size; size -= chunksz, location += chunksz)
8464 {
8465 switch (chunksz)
8466 {
8467 case 1:
8468 x = (x << shift) | bfd_get_8 (input_bfd, location);
8469 break;
8470 case 2:
8471 x = (x << shift) | bfd_get_16 (input_bfd, location);
8472 break;
8473 case 4:
8474 x = (x << shift) | bfd_get_32 (input_bfd, location);
8475 break;
8476 #ifdef BFD64
8477 case 8:
8478 x = (x << shift) | bfd_get_64 (input_bfd, location);
8479 break;
8480 #endif
8481 default:
8482 abort ();
8483 }
8484 }
8485 return x;
8486 }
8487
8488 static void
8489 decode_complex_addend (unsigned long *start, /* in bits */
8490 unsigned long *oplen, /* in bits */
8491 unsigned long *len, /* in bits */
8492 unsigned long *wordsz, /* in bytes */
8493 unsigned long *chunksz, /* in bytes */
8494 unsigned long *lsb0_p,
8495 unsigned long *signed_p,
8496 unsigned long *trunc_p,
8497 unsigned long encoded)
8498 {
8499 * start = encoded & 0x3F;
8500 * len = (encoded >> 6) & 0x3F;
8501 * oplen = (encoded >> 12) & 0x3F;
8502 * wordsz = (encoded >> 18) & 0xF;
8503 * chunksz = (encoded >> 22) & 0xF;
8504 * lsb0_p = (encoded >> 27) & 1;
8505 * signed_p = (encoded >> 28) & 1;
8506 * trunc_p = (encoded >> 29) & 1;
8507 }
8508
8509 bfd_reloc_status_type
8510 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8511 asection *input_section ATTRIBUTE_UNUSED,
8512 bfd_byte *contents,
8513 Elf_Internal_Rela *rel,
8514 bfd_vma relocation)
8515 {
8516 bfd_vma shift, x, mask;
8517 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8518 bfd_reloc_status_type r;
8519
8520 /* Perform this reloc, since it is complex.
8521 (this is not to say that it necessarily refers to a complex
8522 symbol; merely that it is a self-describing CGEN based reloc.
8523 i.e. the addend has the complete reloc information (bit start, end,
8524 word size, etc) encoded within it.). */
8525
8526 decode_complex_addend (&start, &oplen, &len, &wordsz,
8527 &chunksz, &lsb0_p, &signed_p,
8528 &trunc_p, rel->r_addend);
8529
8530 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8531
8532 if (lsb0_p)
8533 shift = (start + 1) - len;
8534 else
8535 shift = (8 * wordsz) - (start + len);
8536
8537 x = get_value (wordsz, chunksz, input_bfd,
8538 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8539
8540 #ifdef DEBUG
8541 printf ("Doing complex reloc: "
8542 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8543 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8544 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8545 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8546 oplen, (unsigned long) x, (unsigned long) mask,
8547 (unsigned long) relocation);
8548 #endif
8549
8550 r = bfd_reloc_ok;
8551 if (! trunc_p)
8552 /* Now do an overflow check. */
8553 r = bfd_check_overflow ((signed_p
8554 ? complain_overflow_signed
8555 : complain_overflow_unsigned),
8556 len, 0, (8 * wordsz),
8557 relocation);
8558
8559 /* Do the deed. */
8560 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8561
8562 #ifdef DEBUG
8563 printf (" relocation: %8.8lx\n"
8564 " shifted mask: %8.8lx\n"
8565 " shifted/masked reloc: %8.8lx\n"
8566 " result: %8.8lx\n",
8567 (unsigned long) relocation, (unsigned long) (mask << shift),
8568 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8569 #endif
8570 put_value (wordsz, chunksz, input_bfd, x,
8571 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8572 return r;
8573 }
8574
8575 /* Functions to read r_offset from external (target order) reloc
8576 entry. Faster than bfd_getl32 et al, because we let the compiler
8577 know the value is aligned. */
8578
8579 static bfd_vma
8580 ext32l_r_offset (const void *p)
8581 {
8582 union aligned32
8583 {
8584 uint32_t v;
8585 unsigned char c[4];
8586 };
8587 const union aligned32 *a
8588 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8589
8590 uint32_t aval = ( (uint32_t) a->c[0]
8591 | (uint32_t) a->c[1] << 8
8592 | (uint32_t) a->c[2] << 16
8593 | (uint32_t) a->c[3] << 24);
8594 return aval;
8595 }
8596
8597 static bfd_vma
8598 ext32b_r_offset (const void *p)
8599 {
8600 union aligned32
8601 {
8602 uint32_t v;
8603 unsigned char c[4];
8604 };
8605 const union aligned32 *a
8606 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8607
8608 uint32_t aval = ( (uint32_t) a->c[0] << 24
8609 | (uint32_t) a->c[1] << 16
8610 | (uint32_t) a->c[2] << 8
8611 | (uint32_t) a->c[3]);
8612 return aval;
8613 }
8614
8615 #ifdef BFD_HOST_64_BIT
8616 static bfd_vma
8617 ext64l_r_offset (const void *p)
8618 {
8619 union aligned64
8620 {
8621 uint64_t v;
8622 unsigned char c[8];
8623 };
8624 const union aligned64 *a
8625 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8626
8627 uint64_t aval = ( (uint64_t) a->c[0]
8628 | (uint64_t) a->c[1] << 8
8629 | (uint64_t) a->c[2] << 16
8630 | (uint64_t) a->c[3] << 24
8631 | (uint64_t) a->c[4] << 32
8632 | (uint64_t) a->c[5] << 40
8633 | (uint64_t) a->c[6] << 48
8634 | (uint64_t) a->c[7] << 56);
8635 return aval;
8636 }
8637
8638 static bfd_vma
8639 ext64b_r_offset (const void *p)
8640 {
8641 union aligned64
8642 {
8643 uint64_t v;
8644 unsigned char c[8];
8645 };
8646 const union aligned64 *a
8647 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8648
8649 uint64_t aval = ( (uint64_t) a->c[0] << 56
8650 | (uint64_t) a->c[1] << 48
8651 | (uint64_t) a->c[2] << 40
8652 | (uint64_t) a->c[3] << 32
8653 | (uint64_t) a->c[4] << 24
8654 | (uint64_t) a->c[5] << 16
8655 | (uint64_t) a->c[6] << 8
8656 | (uint64_t) a->c[7]);
8657 return aval;
8658 }
8659 #endif
8660
8661 /* When performing a relocatable link, the input relocations are
8662 preserved. But, if they reference global symbols, the indices
8663 referenced must be updated. Update all the relocations found in
8664 RELDATA. */
8665
8666 static bfd_boolean
8667 elf_link_adjust_relocs (bfd *abfd,
8668 asection *sec,
8669 struct bfd_elf_section_reloc_data *reldata,
8670 bfd_boolean sort,
8671 struct bfd_link_info *info)
8672 {
8673 unsigned int i;
8674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8675 bfd_byte *erela;
8676 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8677 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8678 bfd_vma r_type_mask;
8679 int r_sym_shift;
8680 unsigned int count = reldata->count;
8681 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8682
8683 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8684 {
8685 swap_in = bed->s->swap_reloc_in;
8686 swap_out = bed->s->swap_reloc_out;
8687 }
8688 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8689 {
8690 swap_in = bed->s->swap_reloca_in;
8691 swap_out = bed->s->swap_reloca_out;
8692 }
8693 else
8694 abort ();
8695
8696 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8697 abort ();
8698
8699 if (bed->s->arch_size == 32)
8700 {
8701 r_type_mask = 0xff;
8702 r_sym_shift = 8;
8703 }
8704 else
8705 {
8706 r_type_mask = 0xffffffff;
8707 r_sym_shift = 32;
8708 }
8709
8710 erela = reldata->hdr->contents;
8711 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8712 {
8713 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8714 unsigned int j;
8715
8716 if (*rel_hash == NULL)
8717 continue;
8718
8719 if ((*rel_hash)->indx == -2
8720 && info->gc_sections
8721 && ! info->gc_keep_exported)
8722 {
8723 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8724 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8725 abfd, sec,
8726 (*rel_hash)->root.root.string);
8727 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8728 abfd, sec);
8729 bfd_set_error (bfd_error_invalid_operation);
8730 return FALSE;
8731 }
8732 BFD_ASSERT ((*rel_hash)->indx >= 0);
8733
8734 (*swap_in) (abfd, erela, irela);
8735 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8736 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8737 | (irela[j].r_info & r_type_mask));
8738 (*swap_out) (abfd, irela, erela);
8739 }
8740
8741 if (bed->elf_backend_update_relocs)
8742 (*bed->elf_backend_update_relocs) (sec, reldata);
8743
8744 if (sort && count != 0)
8745 {
8746 bfd_vma (*ext_r_off) (const void *);
8747 bfd_vma r_off;
8748 size_t elt_size;
8749 bfd_byte *base, *end, *p, *loc;
8750 bfd_byte *buf = NULL;
8751
8752 if (bed->s->arch_size == 32)
8753 {
8754 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8755 ext_r_off = ext32l_r_offset;
8756 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8757 ext_r_off = ext32b_r_offset;
8758 else
8759 abort ();
8760 }
8761 else
8762 {
8763 #ifdef BFD_HOST_64_BIT
8764 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8765 ext_r_off = ext64l_r_offset;
8766 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8767 ext_r_off = ext64b_r_offset;
8768 else
8769 #endif
8770 abort ();
8771 }
8772
8773 /* Must use a stable sort here. A modified insertion sort,
8774 since the relocs are mostly sorted already. */
8775 elt_size = reldata->hdr->sh_entsize;
8776 base = reldata->hdr->contents;
8777 end = base + count * elt_size;
8778 if (elt_size > sizeof (Elf64_External_Rela))
8779 abort ();
8780
8781 /* Ensure the first element is lowest. This acts as a sentinel,
8782 speeding the main loop below. */
8783 r_off = (*ext_r_off) (base);
8784 for (p = loc = base; (p += elt_size) < end; )
8785 {
8786 bfd_vma r_off2 = (*ext_r_off) (p);
8787 if (r_off > r_off2)
8788 {
8789 r_off = r_off2;
8790 loc = p;
8791 }
8792 }
8793 if (loc != base)
8794 {
8795 /* Don't just swap *base and *loc as that changes the order
8796 of the original base[0] and base[1] if they happen to
8797 have the same r_offset. */
8798 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8799 memcpy (onebuf, loc, elt_size);
8800 memmove (base + elt_size, base, loc - base);
8801 memcpy (base, onebuf, elt_size);
8802 }
8803
8804 for (p = base + elt_size; (p += elt_size) < end; )
8805 {
8806 /* base to p is sorted, *p is next to insert. */
8807 r_off = (*ext_r_off) (p);
8808 /* Search the sorted region for location to insert. */
8809 loc = p - elt_size;
8810 while (r_off < (*ext_r_off) (loc))
8811 loc -= elt_size;
8812 loc += elt_size;
8813 if (loc != p)
8814 {
8815 /* Chances are there is a run of relocs to insert here,
8816 from one of more input files. Files are not always
8817 linked in order due to the way elf_link_input_bfd is
8818 called. See pr17666. */
8819 size_t sortlen = p - loc;
8820 bfd_vma r_off2 = (*ext_r_off) (loc);
8821 size_t runlen = elt_size;
8822 size_t buf_size = 96 * 1024;
8823 while (p + runlen < end
8824 && (sortlen <= buf_size
8825 || runlen + elt_size <= buf_size)
8826 && r_off2 > (*ext_r_off) (p + runlen))
8827 runlen += elt_size;
8828 if (buf == NULL)
8829 {
8830 buf = bfd_malloc (buf_size);
8831 if (buf == NULL)
8832 return FALSE;
8833 }
8834 if (runlen < sortlen)
8835 {
8836 memcpy (buf, p, runlen);
8837 memmove (loc + runlen, loc, sortlen);
8838 memcpy (loc, buf, runlen);
8839 }
8840 else
8841 {
8842 memcpy (buf, loc, sortlen);
8843 memmove (loc, p, runlen);
8844 memcpy (loc + runlen, buf, sortlen);
8845 }
8846 p += runlen - elt_size;
8847 }
8848 }
8849 /* Hashes are no longer valid. */
8850 free (reldata->hashes);
8851 reldata->hashes = NULL;
8852 free (buf);
8853 }
8854 return TRUE;
8855 }
8856
8857 struct elf_link_sort_rela
8858 {
8859 union {
8860 bfd_vma offset;
8861 bfd_vma sym_mask;
8862 } u;
8863 enum elf_reloc_type_class type;
8864 /* We use this as an array of size int_rels_per_ext_rel. */
8865 Elf_Internal_Rela rela[1];
8866 };
8867
8868 static int
8869 elf_link_sort_cmp1 (const void *A, const void *B)
8870 {
8871 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8872 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8873 int relativea, relativeb;
8874
8875 relativea = a->type == reloc_class_relative;
8876 relativeb = b->type == reloc_class_relative;
8877
8878 if (relativea < relativeb)
8879 return 1;
8880 if (relativea > relativeb)
8881 return -1;
8882 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8883 return -1;
8884 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8885 return 1;
8886 if (a->rela->r_offset < b->rela->r_offset)
8887 return -1;
8888 if (a->rela->r_offset > b->rela->r_offset)
8889 return 1;
8890 return 0;
8891 }
8892
8893 static int
8894 elf_link_sort_cmp2 (const void *A, const void *B)
8895 {
8896 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8897 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8898
8899 if (a->type < b->type)
8900 return -1;
8901 if (a->type > b->type)
8902 return 1;
8903 if (a->u.offset < b->u.offset)
8904 return -1;
8905 if (a->u.offset > b->u.offset)
8906 return 1;
8907 if (a->rela->r_offset < b->rela->r_offset)
8908 return -1;
8909 if (a->rela->r_offset > b->rela->r_offset)
8910 return 1;
8911 return 0;
8912 }
8913
8914 static size_t
8915 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8916 {
8917 asection *dynamic_relocs;
8918 asection *rela_dyn;
8919 asection *rel_dyn;
8920 bfd_size_type count, size;
8921 size_t i, ret, sort_elt, ext_size;
8922 bfd_byte *sort, *s_non_relative, *p;
8923 struct elf_link_sort_rela *sq;
8924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8925 int i2e = bed->s->int_rels_per_ext_rel;
8926 unsigned int opb = bfd_octets_per_byte (abfd);
8927 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8928 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8929 struct bfd_link_order *lo;
8930 bfd_vma r_sym_mask;
8931 bfd_boolean use_rela;
8932
8933 /* Find a dynamic reloc section. */
8934 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8935 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8936 if (rela_dyn != NULL && rela_dyn->size > 0
8937 && rel_dyn != NULL && rel_dyn->size > 0)
8938 {
8939 bfd_boolean use_rela_initialised = FALSE;
8940
8941 /* This is just here to stop gcc from complaining.
8942 Its initialization checking code is not perfect. */
8943 use_rela = TRUE;
8944
8945 /* Both sections are present. Examine the sizes
8946 of the indirect sections to help us choose. */
8947 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8948 if (lo->type == bfd_indirect_link_order)
8949 {
8950 asection *o = lo->u.indirect.section;
8951
8952 if ((o->size % bed->s->sizeof_rela) == 0)
8953 {
8954 if ((o->size % bed->s->sizeof_rel) == 0)
8955 /* Section size is divisible by both rel and rela sizes.
8956 It is of no help to us. */
8957 ;
8958 else
8959 {
8960 /* Section size is only divisible by rela. */
8961 if (use_rela_initialised && !use_rela)
8962 {
8963 _bfd_error_handler (_("%B: Unable to sort relocs - "
8964 "they are in more than one size"),
8965 abfd);
8966 bfd_set_error (bfd_error_invalid_operation);
8967 return 0;
8968 }
8969 else
8970 {
8971 use_rela = TRUE;
8972 use_rela_initialised = TRUE;
8973 }
8974 }
8975 }
8976 else if ((o->size % bed->s->sizeof_rel) == 0)
8977 {
8978 /* Section size is only divisible by rel. */
8979 if (use_rela_initialised && use_rela)
8980 {
8981 _bfd_error_handler (_("%B: Unable to sort relocs - "
8982 "they are in more than one size"),
8983 abfd);
8984 bfd_set_error (bfd_error_invalid_operation);
8985 return 0;
8986 }
8987 else
8988 {
8989 use_rela = FALSE;
8990 use_rela_initialised = TRUE;
8991 }
8992 }
8993 else
8994 {
8995 /* The section size is not divisible by either -
8996 something is wrong. */
8997 _bfd_error_handler (_("%B: Unable to sort relocs - "
8998 "they are of an unknown size"), abfd);
8999 bfd_set_error (bfd_error_invalid_operation);
9000 return 0;
9001 }
9002 }
9003
9004 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9005 if (lo->type == bfd_indirect_link_order)
9006 {
9007 asection *o = lo->u.indirect.section;
9008
9009 if ((o->size % bed->s->sizeof_rela) == 0)
9010 {
9011 if ((o->size % bed->s->sizeof_rel) == 0)
9012 /* Section size is divisible by both rel and rela sizes.
9013 It is of no help to us. */
9014 ;
9015 else
9016 {
9017 /* Section size is only divisible by rela. */
9018 if (use_rela_initialised && !use_rela)
9019 {
9020 _bfd_error_handler (_("%B: Unable to sort relocs - "
9021 "they are in more than one size"),
9022 abfd);
9023 bfd_set_error (bfd_error_invalid_operation);
9024 return 0;
9025 }
9026 else
9027 {
9028 use_rela = TRUE;
9029 use_rela_initialised = TRUE;
9030 }
9031 }
9032 }
9033 else if ((o->size % bed->s->sizeof_rel) == 0)
9034 {
9035 /* Section size is only divisible by rel. */
9036 if (use_rela_initialised && use_rela)
9037 {
9038 _bfd_error_handler (_("%B: Unable to sort relocs - "
9039 "they are in more than one size"),
9040 abfd);
9041 bfd_set_error (bfd_error_invalid_operation);
9042 return 0;
9043 }
9044 else
9045 {
9046 use_rela = FALSE;
9047 use_rela_initialised = TRUE;
9048 }
9049 }
9050 else
9051 {
9052 /* The section size is not divisible by either -
9053 something is wrong. */
9054 _bfd_error_handler (_("%B: Unable to sort relocs - "
9055 "they are of an unknown size"), abfd);
9056 bfd_set_error (bfd_error_invalid_operation);
9057 return 0;
9058 }
9059 }
9060
9061 if (! use_rela_initialised)
9062 /* Make a guess. */
9063 use_rela = TRUE;
9064 }
9065 else if (rela_dyn != NULL && rela_dyn->size > 0)
9066 use_rela = TRUE;
9067 else if (rel_dyn != NULL && rel_dyn->size > 0)
9068 use_rela = FALSE;
9069 else
9070 return 0;
9071
9072 if (use_rela)
9073 {
9074 dynamic_relocs = rela_dyn;
9075 ext_size = bed->s->sizeof_rela;
9076 swap_in = bed->s->swap_reloca_in;
9077 swap_out = bed->s->swap_reloca_out;
9078 }
9079 else
9080 {
9081 dynamic_relocs = rel_dyn;
9082 ext_size = bed->s->sizeof_rel;
9083 swap_in = bed->s->swap_reloc_in;
9084 swap_out = bed->s->swap_reloc_out;
9085 }
9086
9087 size = 0;
9088 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9089 if (lo->type == bfd_indirect_link_order)
9090 size += lo->u.indirect.section->size;
9091
9092 if (size != dynamic_relocs->size)
9093 return 0;
9094
9095 sort_elt = (sizeof (struct elf_link_sort_rela)
9096 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9097
9098 count = dynamic_relocs->size / ext_size;
9099 if (count == 0)
9100 return 0;
9101 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9102
9103 if (sort == NULL)
9104 {
9105 (*info->callbacks->warning)
9106 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9107 return 0;
9108 }
9109
9110 if (bed->s->arch_size == 32)
9111 r_sym_mask = ~(bfd_vma) 0xff;
9112 else
9113 r_sym_mask = ~(bfd_vma) 0xffffffff;
9114
9115 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9116 if (lo->type == bfd_indirect_link_order)
9117 {
9118 bfd_byte *erel, *erelend;
9119 asection *o = lo->u.indirect.section;
9120
9121 if (o->contents == NULL && o->size != 0)
9122 {
9123 /* This is a reloc section that is being handled as a normal
9124 section. See bfd_section_from_shdr. We can't combine
9125 relocs in this case. */
9126 free (sort);
9127 return 0;
9128 }
9129 erel = o->contents;
9130 erelend = o->contents + o->size;
9131 p = sort + o->output_offset * opb / ext_size * sort_elt;
9132
9133 while (erel < erelend)
9134 {
9135 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9136
9137 (*swap_in) (abfd, erel, s->rela);
9138 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9139 s->u.sym_mask = r_sym_mask;
9140 p += sort_elt;
9141 erel += ext_size;
9142 }
9143 }
9144
9145 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9146
9147 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9148 {
9149 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9150 if (s->type != reloc_class_relative)
9151 break;
9152 }
9153 ret = i;
9154 s_non_relative = p;
9155
9156 sq = (struct elf_link_sort_rela *) s_non_relative;
9157 for (; i < count; i++, p += sort_elt)
9158 {
9159 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9160 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9161 sq = sp;
9162 sp->u.offset = sq->rela->r_offset;
9163 }
9164
9165 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9166
9167 struct elf_link_hash_table *htab = elf_hash_table (info);
9168 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9169 {
9170 /* We have plt relocs in .rela.dyn. */
9171 sq = (struct elf_link_sort_rela *) sort;
9172 for (i = 0; i < count; i++)
9173 if (sq[count - i - 1].type != reloc_class_plt)
9174 break;
9175 if (i != 0 && htab->srelplt->size == i * ext_size)
9176 {
9177 struct bfd_link_order **plo;
9178 /* Put srelplt link_order last. This is so the output_offset
9179 set in the next loop is correct for DT_JMPREL. */
9180 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9181 if ((*plo)->type == bfd_indirect_link_order
9182 && (*plo)->u.indirect.section == htab->srelplt)
9183 {
9184 lo = *plo;
9185 *plo = lo->next;
9186 }
9187 else
9188 plo = &(*plo)->next;
9189 *plo = lo;
9190 lo->next = NULL;
9191 dynamic_relocs->map_tail.link_order = lo;
9192 }
9193 }
9194
9195 p = sort;
9196 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9197 if (lo->type == bfd_indirect_link_order)
9198 {
9199 bfd_byte *erel, *erelend;
9200 asection *o = lo->u.indirect.section;
9201
9202 erel = o->contents;
9203 erelend = o->contents + o->size;
9204 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9205 while (erel < erelend)
9206 {
9207 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9208 (*swap_out) (abfd, s->rela, erel);
9209 p += sort_elt;
9210 erel += ext_size;
9211 }
9212 }
9213
9214 free (sort);
9215 *psec = dynamic_relocs;
9216 return ret;
9217 }
9218
9219 /* Add a symbol to the output symbol string table. */
9220
9221 static int
9222 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9223 const char *name,
9224 Elf_Internal_Sym *elfsym,
9225 asection *input_sec,
9226 struct elf_link_hash_entry *h)
9227 {
9228 int (*output_symbol_hook)
9229 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9230 struct elf_link_hash_entry *);
9231 struct elf_link_hash_table *hash_table;
9232 const struct elf_backend_data *bed;
9233 bfd_size_type strtabsize;
9234
9235 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9236
9237 bed = get_elf_backend_data (flinfo->output_bfd);
9238 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9239 if (output_symbol_hook != NULL)
9240 {
9241 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9242 if (ret != 1)
9243 return ret;
9244 }
9245
9246 if (name == NULL
9247 || *name == '\0'
9248 || (input_sec->flags & SEC_EXCLUDE))
9249 elfsym->st_name = (unsigned long) -1;
9250 else
9251 {
9252 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9253 to get the final offset for st_name. */
9254 elfsym->st_name
9255 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9256 name, FALSE);
9257 if (elfsym->st_name == (unsigned long) -1)
9258 return 0;
9259 }
9260
9261 hash_table = elf_hash_table (flinfo->info);
9262 strtabsize = hash_table->strtabsize;
9263 if (strtabsize <= hash_table->strtabcount)
9264 {
9265 strtabsize += strtabsize;
9266 hash_table->strtabsize = strtabsize;
9267 strtabsize *= sizeof (*hash_table->strtab);
9268 hash_table->strtab
9269 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9270 strtabsize);
9271 if (hash_table->strtab == NULL)
9272 return 0;
9273 }
9274 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9275 hash_table->strtab[hash_table->strtabcount].dest_index
9276 = hash_table->strtabcount;
9277 hash_table->strtab[hash_table->strtabcount].destshndx_index
9278 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9279
9280 bfd_get_symcount (flinfo->output_bfd) += 1;
9281 hash_table->strtabcount += 1;
9282
9283 return 1;
9284 }
9285
9286 /* Swap symbols out to the symbol table and flush the output symbols to
9287 the file. */
9288
9289 static bfd_boolean
9290 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9291 {
9292 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9293 bfd_size_type amt;
9294 size_t i;
9295 const struct elf_backend_data *bed;
9296 bfd_byte *symbuf;
9297 Elf_Internal_Shdr *hdr;
9298 file_ptr pos;
9299 bfd_boolean ret;
9300
9301 if (!hash_table->strtabcount)
9302 return TRUE;
9303
9304 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9305
9306 bed = get_elf_backend_data (flinfo->output_bfd);
9307
9308 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9309 symbuf = (bfd_byte *) bfd_malloc (amt);
9310 if (symbuf == NULL)
9311 return FALSE;
9312
9313 if (flinfo->symshndxbuf)
9314 {
9315 amt = sizeof (Elf_External_Sym_Shndx);
9316 amt *= bfd_get_symcount (flinfo->output_bfd);
9317 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9318 if (flinfo->symshndxbuf == NULL)
9319 {
9320 free (symbuf);
9321 return FALSE;
9322 }
9323 }
9324
9325 for (i = 0; i < hash_table->strtabcount; i++)
9326 {
9327 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9328 if (elfsym->sym.st_name == (unsigned long) -1)
9329 elfsym->sym.st_name = 0;
9330 else
9331 elfsym->sym.st_name
9332 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9333 elfsym->sym.st_name);
9334 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9335 ((bfd_byte *) symbuf
9336 + (elfsym->dest_index
9337 * bed->s->sizeof_sym)),
9338 (flinfo->symshndxbuf
9339 + elfsym->destshndx_index));
9340 }
9341
9342 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9343 pos = hdr->sh_offset + hdr->sh_size;
9344 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9345 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9346 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9347 {
9348 hdr->sh_size += amt;
9349 ret = TRUE;
9350 }
9351 else
9352 ret = FALSE;
9353
9354 free (symbuf);
9355
9356 free (hash_table->strtab);
9357 hash_table->strtab = NULL;
9358
9359 return ret;
9360 }
9361
9362 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9363
9364 static bfd_boolean
9365 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9366 {
9367 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9368 && sym->st_shndx < SHN_LORESERVE)
9369 {
9370 /* The gABI doesn't support dynamic symbols in output sections
9371 beyond 64k. */
9372 _bfd_error_handler
9373 /* xgettext:c-format */
9374 (_("%B: Too many sections: %d (>= %d)"),
9375 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9376 bfd_set_error (bfd_error_nonrepresentable_section);
9377 return FALSE;
9378 }
9379 return TRUE;
9380 }
9381
9382 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9383 allowing an unsatisfied unversioned symbol in the DSO to match a
9384 versioned symbol that would normally require an explicit version.
9385 We also handle the case that a DSO references a hidden symbol
9386 which may be satisfied by a versioned symbol in another DSO. */
9387
9388 static bfd_boolean
9389 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9390 const struct elf_backend_data *bed,
9391 struct elf_link_hash_entry *h)
9392 {
9393 bfd *abfd;
9394 struct elf_link_loaded_list *loaded;
9395
9396 if (!is_elf_hash_table (info->hash))
9397 return FALSE;
9398
9399 /* Check indirect symbol. */
9400 while (h->root.type == bfd_link_hash_indirect)
9401 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9402
9403 switch (h->root.type)
9404 {
9405 default:
9406 abfd = NULL;
9407 break;
9408
9409 case bfd_link_hash_undefined:
9410 case bfd_link_hash_undefweak:
9411 abfd = h->root.u.undef.abfd;
9412 if (abfd == NULL
9413 || (abfd->flags & DYNAMIC) == 0
9414 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9415 return FALSE;
9416 break;
9417
9418 case bfd_link_hash_defined:
9419 case bfd_link_hash_defweak:
9420 abfd = h->root.u.def.section->owner;
9421 break;
9422
9423 case bfd_link_hash_common:
9424 abfd = h->root.u.c.p->section->owner;
9425 break;
9426 }
9427 BFD_ASSERT (abfd != NULL);
9428
9429 for (loaded = elf_hash_table (info)->loaded;
9430 loaded != NULL;
9431 loaded = loaded->next)
9432 {
9433 bfd *input;
9434 Elf_Internal_Shdr *hdr;
9435 size_t symcount;
9436 size_t extsymcount;
9437 size_t extsymoff;
9438 Elf_Internal_Shdr *versymhdr;
9439 Elf_Internal_Sym *isym;
9440 Elf_Internal_Sym *isymend;
9441 Elf_Internal_Sym *isymbuf;
9442 Elf_External_Versym *ever;
9443 Elf_External_Versym *extversym;
9444
9445 input = loaded->abfd;
9446
9447 /* We check each DSO for a possible hidden versioned definition. */
9448 if (input == abfd
9449 || (input->flags & DYNAMIC) == 0
9450 || elf_dynversym (input) == 0)
9451 continue;
9452
9453 hdr = &elf_tdata (input)->dynsymtab_hdr;
9454
9455 symcount = hdr->sh_size / bed->s->sizeof_sym;
9456 if (elf_bad_symtab (input))
9457 {
9458 extsymcount = symcount;
9459 extsymoff = 0;
9460 }
9461 else
9462 {
9463 extsymcount = symcount - hdr->sh_info;
9464 extsymoff = hdr->sh_info;
9465 }
9466
9467 if (extsymcount == 0)
9468 continue;
9469
9470 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9471 NULL, NULL, NULL);
9472 if (isymbuf == NULL)
9473 return FALSE;
9474
9475 /* Read in any version definitions. */
9476 versymhdr = &elf_tdata (input)->dynversym_hdr;
9477 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9478 if (extversym == NULL)
9479 goto error_ret;
9480
9481 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9482 || (bfd_bread (extversym, versymhdr->sh_size, input)
9483 != versymhdr->sh_size))
9484 {
9485 free (extversym);
9486 error_ret:
9487 free (isymbuf);
9488 return FALSE;
9489 }
9490
9491 ever = extversym + extsymoff;
9492 isymend = isymbuf + extsymcount;
9493 for (isym = isymbuf; isym < isymend; isym++, ever++)
9494 {
9495 const char *name;
9496 Elf_Internal_Versym iver;
9497 unsigned short version_index;
9498
9499 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9500 || isym->st_shndx == SHN_UNDEF)
9501 continue;
9502
9503 name = bfd_elf_string_from_elf_section (input,
9504 hdr->sh_link,
9505 isym->st_name);
9506 if (strcmp (name, h->root.root.string) != 0)
9507 continue;
9508
9509 _bfd_elf_swap_versym_in (input, ever, &iver);
9510
9511 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9512 && !(h->def_regular
9513 && h->forced_local))
9514 {
9515 /* If we have a non-hidden versioned sym, then it should
9516 have provided a definition for the undefined sym unless
9517 it is defined in a non-shared object and forced local.
9518 */
9519 abort ();
9520 }
9521
9522 version_index = iver.vs_vers & VERSYM_VERSION;
9523 if (version_index == 1 || version_index == 2)
9524 {
9525 /* This is the base or first version. We can use it. */
9526 free (extversym);
9527 free (isymbuf);
9528 return TRUE;
9529 }
9530 }
9531
9532 free (extversym);
9533 free (isymbuf);
9534 }
9535
9536 return FALSE;
9537 }
9538
9539 /* Convert ELF common symbol TYPE. */
9540
9541 static int
9542 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9543 {
9544 /* Commom symbol can only appear in relocatable link. */
9545 if (!bfd_link_relocatable (info))
9546 abort ();
9547 switch (info->elf_stt_common)
9548 {
9549 case unchanged:
9550 break;
9551 case elf_stt_common:
9552 type = STT_COMMON;
9553 break;
9554 case no_elf_stt_common:
9555 type = STT_OBJECT;
9556 break;
9557 }
9558 return type;
9559 }
9560
9561 /* Add an external symbol to the symbol table. This is called from
9562 the hash table traversal routine. When generating a shared object,
9563 we go through the symbol table twice. The first time we output
9564 anything that might have been forced to local scope in a version
9565 script. The second time we output the symbols that are still
9566 global symbols. */
9567
9568 static bfd_boolean
9569 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9570 {
9571 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9572 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9573 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9574 bfd_boolean strip;
9575 Elf_Internal_Sym sym;
9576 asection *input_sec;
9577 const struct elf_backend_data *bed;
9578 long indx;
9579 int ret;
9580 unsigned int type;
9581
9582 if (h->root.type == bfd_link_hash_warning)
9583 {
9584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9585 if (h->root.type == bfd_link_hash_new)
9586 return TRUE;
9587 }
9588
9589 /* Decide whether to output this symbol in this pass. */
9590 if (eoinfo->localsyms)
9591 {
9592 if (!h->forced_local)
9593 return TRUE;
9594 }
9595 else
9596 {
9597 if (h->forced_local)
9598 return TRUE;
9599 }
9600
9601 bed = get_elf_backend_data (flinfo->output_bfd);
9602
9603 if (h->root.type == bfd_link_hash_undefined)
9604 {
9605 /* If we have an undefined symbol reference here then it must have
9606 come from a shared library that is being linked in. (Undefined
9607 references in regular files have already been handled unless
9608 they are in unreferenced sections which are removed by garbage
9609 collection). */
9610 bfd_boolean ignore_undef = FALSE;
9611
9612 /* Some symbols may be special in that the fact that they're
9613 undefined can be safely ignored - let backend determine that. */
9614 if (bed->elf_backend_ignore_undef_symbol)
9615 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9616
9617 /* If we are reporting errors for this situation then do so now. */
9618 if (!ignore_undef
9619 && h->ref_dynamic
9620 && (!h->ref_regular || flinfo->info->gc_sections)
9621 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9622 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9623 (*flinfo->info->callbacks->undefined_symbol)
9624 (flinfo->info, h->root.root.string,
9625 h->ref_regular ? NULL : h->root.u.undef.abfd,
9626 NULL, 0,
9627 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9628
9629 /* Strip a global symbol defined in a discarded section. */
9630 if (h->indx == -3)
9631 return TRUE;
9632 }
9633
9634 /* We should also warn if a forced local symbol is referenced from
9635 shared libraries. */
9636 if (bfd_link_executable (flinfo->info)
9637 && h->forced_local
9638 && h->ref_dynamic
9639 && h->def_regular
9640 && !h->dynamic_def
9641 && h->ref_dynamic_nonweak
9642 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9643 {
9644 bfd *def_bfd;
9645 const char *msg;
9646 struct elf_link_hash_entry *hi = h;
9647
9648 /* Check indirect symbol. */
9649 while (hi->root.type == bfd_link_hash_indirect)
9650 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9651
9652 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9653 /* xgettext:c-format */
9654 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9655 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9656 /* xgettext:c-format */
9657 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9658 else
9659 /* xgettext:c-format */
9660 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9661 def_bfd = flinfo->output_bfd;
9662 if (hi->root.u.def.section != bfd_abs_section_ptr)
9663 def_bfd = hi->root.u.def.section->owner;
9664 _bfd_error_handler (msg, flinfo->output_bfd,
9665 h->root.root.string, def_bfd);
9666 bfd_set_error (bfd_error_bad_value);
9667 eoinfo->failed = TRUE;
9668 return FALSE;
9669 }
9670
9671 /* We don't want to output symbols that have never been mentioned by
9672 a regular file, or that we have been told to strip. However, if
9673 h->indx is set to -2, the symbol is used by a reloc and we must
9674 output it. */
9675 strip = FALSE;
9676 if (h->indx == -2)
9677 ;
9678 else if ((h->def_dynamic
9679 || h->ref_dynamic
9680 || h->root.type == bfd_link_hash_new)
9681 && !h->def_regular
9682 && !h->ref_regular)
9683 strip = TRUE;
9684 else if (flinfo->info->strip == strip_all)
9685 strip = TRUE;
9686 else if (flinfo->info->strip == strip_some
9687 && bfd_hash_lookup (flinfo->info->keep_hash,
9688 h->root.root.string, FALSE, FALSE) == NULL)
9689 strip = TRUE;
9690 else if ((h->root.type == bfd_link_hash_defined
9691 || h->root.type == bfd_link_hash_defweak)
9692 && ((flinfo->info->strip_discarded
9693 && discarded_section (h->root.u.def.section))
9694 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9695 && h->root.u.def.section->owner != NULL
9696 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9697 strip = TRUE;
9698 else if ((h->root.type == bfd_link_hash_undefined
9699 || h->root.type == bfd_link_hash_undefweak)
9700 && h->root.u.undef.abfd != NULL
9701 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9702 strip = TRUE;
9703
9704 type = h->type;
9705
9706 /* If we're stripping it, and it's not a dynamic symbol, there's
9707 nothing else to do. However, if it is a forced local symbol or
9708 an ifunc symbol we need to give the backend finish_dynamic_symbol
9709 function a chance to make it dynamic. */
9710 if (strip
9711 && h->dynindx == -1
9712 && type != STT_GNU_IFUNC
9713 && !h->forced_local)
9714 return TRUE;
9715
9716 sym.st_value = 0;
9717 sym.st_size = h->size;
9718 sym.st_other = h->other;
9719 switch (h->root.type)
9720 {
9721 default:
9722 case bfd_link_hash_new:
9723 case bfd_link_hash_warning:
9724 abort ();
9725 return FALSE;
9726
9727 case bfd_link_hash_undefined:
9728 case bfd_link_hash_undefweak:
9729 input_sec = bfd_und_section_ptr;
9730 sym.st_shndx = SHN_UNDEF;
9731 break;
9732
9733 case bfd_link_hash_defined:
9734 case bfd_link_hash_defweak:
9735 {
9736 input_sec = h->root.u.def.section;
9737 if (input_sec->output_section != NULL)
9738 {
9739 sym.st_shndx =
9740 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9741 input_sec->output_section);
9742 if (sym.st_shndx == SHN_BAD)
9743 {
9744 _bfd_error_handler
9745 /* xgettext:c-format */
9746 (_("%B: could not find output section %A for input section %A"),
9747 flinfo->output_bfd, input_sec->output_section, input_sec);
9748 bfd_set_error (bfd_error_nonrepresentable_section);
9749 eoinfo->failed = TRUE;
9750 return FALSE;
9751 }
9752
9753 /* ELF symbols in relocatable files are section relative,
9754 but in nonrelocatable files they are virtual
9755 addresses. */
9756 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9757 if (!bfd_link_relocatable (flinfo->info))
9758 {
9759 sym.st_value += input_sec->output_section->vma;
9760 if (h->type == STT_TLS)
9761 {
9762 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9763 if (tls_sec != NULL)
9764 sym.st_value -= tls_sec->vma;
9765 }
9766 }
9767 }
9768 else
9769 {
9770 BFD_ASSERT (input_sec->owner == NULL
9771 || (input_sec->owner->flags & DYNAMIC) != 0);
9772 sym.st_shndx = SHN_UNDEF;
9773 input_sec = bfd_und_section_ptr;
9774 }
9775 }
9776 break;
9777
9778 case bfd_link_hash_common:
9779 input_sec = h->root.u.c.p->section;
9780 sym.st_shndx = bed->common_section_index (input_sec);
9781 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9782 break;
9783
9784 case bfd_link_hash_indirect:
9785 /* These symbols are created by symbol versioning. They point
9786 to the decorated version of the name. For example, if the
9787 symbol foo@@GNU_1.2 is the default, which should be used when
9788 foo is used with no version, then we add an indirect symbol
9789 foo which points to foo@@GNU_1.2. We ignore these symbols,
9790 since the indirected symbol is already in the hash table. */
9791 return TRUE;
9792 }
9793
9794 if (type == STT_COMMON || type == STT_OBJECT)
9795 switch (h->root.type)
9796 {
9797 case bfd_link_hash_common:
9798 type = elf_link_convert_common_type (flinfo->info, type);
9799 break;
9800 case bfd_link_hash_defined:
9801 case bfd_link_hash_defweak:
9802 if (bed->common_definition (&sym))
9803 type = elf_link_convert_common_type (flinfo->info, type);
9804 else
9805 type = STT_OBJECT;
9806 break;
9807 case bfd_link_hash_undefined:
9808 case bfd_link_hash_undefweak:
9809 break;
9810 default:
9811 abort ();
9812 }
9813
9814 if (h->forced_local)
9815 {
9816 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9817 /* Turn off visibility on local symbol. */
9818 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9819 }
9820 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9821 else if (h->unique_global && h->def_regular)
9822 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9823 else if (h->root.type == bfd_link_hash_undefweak
9824 || h->root.type == bfd_link_hash_defweak)
9825 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9826 else
9827 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9828 sym.st_target_internal = h->target_internal;
9829
9830 /* Give the processor backend a chance to tweak the symbol value,
9831 and also to finish up anything that needs to be done for this
9832 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9833 forced local syms when non-shared is due to a historical quirk.
9834 STT_GNU_IFUNC symbol must go through PLT. */
9835 if ((h->type == STT_GNU_IFUNC
9836 && h->def_regular
9837 && !bfd_link_relocatable (flinfo->info))
9838 || ((h->dynindx != -1
9839 || h->forced_local)
9840 && ((bfd_link_pic (flinfo->info)
9841 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9842 || h->root.type != bfd_link_hash_undefweak))
9843 || !h->forced_local)
9844 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9845 {
9846 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9847 (flinfo->output_bfd, flinfo->info, h, &sym)))
9848 {
9849 eoinfo->failed = TRUE;
9850 return FALSE;
9851 }
9852 }
9853
9854 /* If we are marking the symbol as undefined, and there are no
9855 non-weak references to this symbol from a regular object, then
9856 mark the symbol as weak undefined; if there are non-weak
9857 references, mark the symbol as strong. We can't do this earlier,
9858 because it might not be marked as undefined until the
9859 finish_dynamic_symbol routine gets through with it. */
9860 if (sym.st_shndx == SHN_UNDEF
9861 && h->ref_regular
9862 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9863 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9864 {
9865 int bindtype;
9866 type = ELF_ST_TYPE (sym.st_info);
9867
9868 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9869 if (type == STT_GNU_IFUNC)
9870 type = STT_FUNC;
9871
9872 if (h->ref_regular_nonweak)
9873 bindtype = STB_GLOBAL;
9874 else
9875 bindtype = STB_WEAK;
9876 sym.st_info = ELF_ST_INFO (bindtype, type);
9877 }
9878
9879 /* If this is a symbol defined in a dynamic library, don't use the
9880 symbol size from the dynamic library. Relinking an executable
9881 against a new library may introduce gratuitous changes in the
9882 executable's symbols if we keep the size. */
9883 if (sym.st_shndx == SHN_UNDEF
9884 && !h->def_regular
9885 && h->def_dynamic)
9886 sym.st_size = 0;
9887
9888 /* If a non-weak symbol with non-default visibility is not defined
9889 locally, it is a fatal error. */
9890 if (!bfd_link_relocatable (flinfo->info)
9891 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9892 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9893 && h->root.type == bfd_link_hash_undefined
9894 && !h->def_regular)
9895 {
9896 const char *msg;
9897
9898 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9899 /* xgettext:c-format */
9900 msg = _("%B: protected symbol `%s' isn't defined");
9901 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9902 /* xgettext:c-format */
9903 msg = _("%B: internal symbol `%s' isn't defined");
9904 else
9905 /* xgettext:c-format */
9906 msg = _("%B: hidden symbol `%s' isn't defined");
9907 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9908 bfd_set_error (bfd_error_bad_value);
9909 eoinfo->failed = TRUE;
9910 return FALSE;
9911 }
9912
9913 /* If this symbol should be put in the .dynsym section, then put it
9914 there now. We already know the symbol index. We also fill in
9915 the entry in the .hash section. */
9916 if (elf_hash_table (flinfo->info)->dynsym != NULL
9917 && h->dynindx != -1
9918 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9919 {
9920 bfd_byte *esym;
9921
9922 /* Since there is no version information in the dynamic string,
9923 if there is no version info in symbol version section, we will
9924 have a run-time problem if not linking executable, referenced
9925 by shared library, or not bound locally. */
9926 if (h->verinfo.verdef == NULL
9927 && (!bfd_link_executable (flinfo->info)
9928 || h->ref_dynamic
9929 || !h->def_regular))
9930 {
9931 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9932
9933 if (p && p [1] != '\0')
9934 {
9935 _bfd_error_handler
9936 /* xgettext:c-format */
9937 (_("%B: No symbol version section for versioned symbol `%s'"),
9938 flinfo->output_bfd, h->root.root.string);
9939 eoinfo->failed = TRUE;
9940 return FALSE;
9941 }
9942 }
9943
9944 sym.st_name = h->dynstr_index;
9945 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9946 + h->dynindx * bed->s->sizeof_sym);
9947 if (!check_dynsym (flinfo->output_bfd, &sym))
9948 {
9949 eoinfo->failed = TRUE;
9950 return FALSE;
9951 }
9952 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9953
9954 if (flinfo->hash_sec != NULL)
9955 {
9956 size_t hash_entry_size;
9957 bfd_byte *bucketpos;
9958 bfd_vma chain;
9959 size_t bucketcount;
9960 size_t bucket;
9961
9962 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9963 bucket = h->u.elf_hash_value % bucketcount;
9964
9965 hash_entry_size
9966 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9967 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9968 + (bucket + 2) * hash_entry_size);
9969 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9970 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9971 bucketpos);
9972 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9973 ((bfd_byte *) flinfo->hash_sec->contents
9974 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9975 }
9976
9977 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9978 {
9979 Elf_Internal_Versym iversym;
9980 Elf_External_Versym *eversym;
9981
9982 if (!h->def_regular)
9983 {
9984 if (h->verinfo.verdef == NULL
9985 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9986 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9987 iversym.vs_vers = 0;
9988 else
9989 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9990 }
9991 else
9992 {
9993 if (h->verinfo.vertree == NULL)
9994 iversym.vs_vers = 1;
9995 else
9996 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9997 if (flinfo->info->create_default_symver)
9998 iversym.vs_vers++;
9999 }
10000
10001 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10002 defined locally. */
10003 if (h->versioned == versioned_hidden && h->def_regular)
10004 iversym.vs_vers |= VERSYM_HIDDEN;
10005
10006 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10007 eversym += h->dynindx;
10008 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10009 }
10010 }
10011
10012 /* If the symbol is undefined, and we didn't output it to .dynsym,
10013 strip it from .symtab too. Obviously we can't do this for
10014 relocatable output or when needed for --emit-relocs. */
10015 else if (input_sec == bfd_und_section_ptr
10016 && h->indx != -2
10017 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10018 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10019 && !bfd_link_relocatable (flinfo->info))
10020 return TRUE;
10021
10022 /* Also strip others that we couldn't earlier due to dynamic symbol
10023 processing. */
10024 if (strip)
10025 return TRUE;
10026 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10027 return TRUE;
10028
10029 /* Output a FILE symbol so that following locals are not associated
10030 with the wrong input file. We need one for forced local symbols
10031 if we've seen more than one FILE symbol or when we have exactly
10032 one FILE symbol but global symbols are present in a file other
10033 than the one with the FILE symbol. We also need one if linker
10034 defined symbols are present. In practice these conditions are
10035 always met, so just emit the FILE symbol unconditionally. */
10036 if (eoinfo->localsyms
10037 && !eoinfo->file_sym_done
10038 && eoinfo->flinfo->filesym_count != 0)
10039 {
10040 Elf_Internal_Sym fsym;
10041
10042 memset (&fsym, 0, sizeof (fsym));
10043 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10044 fsym.st_shndx = SHN_ABS;
10045 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10046 bfd_und_section_ptr, NULL))
10047 return FALSE;
10048
10049 eoinfo->file_sym_done = TRUE;
10050 }
10051
10052 indx = bfd_get_symcount (flinfo->output_bfd);
10053 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10054 input_sec, h);
10055 if (ret == 0)
10056 {
10057 eoinfo->failed = TRUE;
10058 return FALSE;
10059 }
10060 else if (ret == 1)
10061 h->indx = indx;
10062 else if (h->indx == -2)
10063 abort();
10064
10065 return TRUE;
10066 }
10067
10068 /* Return TRUE if special handling is done for relocs in SEC against
10069 symbols defined in discarded sections. */
10070
10071 static bfd_boolean
10072 elf_section_ignore_discarded_relocs (asection *sec)
10073 {
10074 const struct elf_backend_data *bed;
10075
10076 switch (sec->sec_info_type)
10077 {
10078 case SEC_INFO_TYPE_STABS:
10079 case SEC_INFO_TYPE_EH_FRAME:
10080 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10081 return TRUE;
10082 default:
10083 break;
10084 }
10085
10086 bed = get_elf_backend_data (sec->owner);
10087 if (bed->elf_backend_ignore_discarded_relocs != NULL
10088 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10089 return TRUE;
10090
10091 return FALSE;
10092 }
10093
10094 /* Return a mask saying how ld should treat relocations in SEC against
10095 symbols defined in discarded sections. If this function returns
10096 COMPLAIN set, ld will issue a warning message. If this function
10097 returns PRETEND set, and the discarded section was link-once and the
10098 same size as the kept link-once section, ld will pretend that the
10099 symbol was actually defined in the kept section. Otherwise ld will
10100 zero the reloc (at least that is the intent, but some cooperation by
10101 the target dependent code is needed, particularly for REL targets). */
10102
10103 unsigned int
10104 _bfd_elf_default_action_discarded (asection *sec)
10105 {
10106 if (sec->flags & SEC_DEBUGGING)
10107 return PRETEND;
10108
10109 if (strcmp (".eh_frame", sec->name) == 0)
10110 return 0;
10111
10112 if (strcmp (".gcc_except_table", sec->name) == 0)
10113 return 0;
10114
10115 return COMPLAIN | PRETEND;
10116 }
10117
10118 /* Find a match between a section and a member of a section group. */
10119
10120 static asection *
10121 match_group_member (asection *sec, asection *group,
10122 struct bfd_link_info *info)
10123 {
10124 asection *first = elf_next_in_group (group);
10125 asection *s = first;
10126
10127 while (s != NULL)
10128 {
10129 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10130 return s;
10131
10132 s = elf_next_in_group (s);
10133 if (s == first)
10134 break;
10135 }
10136
10137 return NULL;
10138 }
10139
10140 /* Check if the kept section of a discarded section SEC can be used
10141 to replace it. Return the replacement if it is OK. Otherwise return
10142 NULL. */
10143
10144 asection *
10145 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10146 {
10147 asection *kept;
10148
10149 kept = sec->kept_section;
10150 if (kept != NULL)
10151 {
10152 if ((kept->flags & SEC_GROUP) != 0)
10153 kept = match_group_member (sec, kept, info);
10154 if (kept != NULL
10155 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10156 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10157 kept = NULL;
10158 sec->kept_section = kept;
10159 }
10160 return kept;
10161 }
10162
10163 /* Link an input file into the linker output file. This function
10164 handles all the sections and relocations of the input file at once.
10165 This is so that we only have to read the local symbols once, and
10166 don't have to keep them in memory. */
10167
10168 static bfd_boolean
10169 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10170 {
10171 int (*relocate_section)
10172 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10173 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10174 bfd *output_bfd;
10175 Elf_Internal_Shdr *symtab_hdr;
10176 size_t locsymcount;
10177 size_t extsymoff;
10178 Elf_Internal_Sym *isymbuf;
10179 Elf_Internal_Sym *isym;
10180 Elf_Internal_Sym *isymend;
10181 long *pindex;
10182 asection **ppsection;
10183 asection *o;
10184 const struct elf_backend_data *bed;
10185 struct elf_link_hash_entry **sym_hashes;
10186 bfd_size_type address_size;
10187 bfd_vma r_type_mask;
10188 int r_sym_shift;
10189 bfd_boolean have_file_sym = FALSE;
10190
10191 output_bfd = flinfo->output_bfd;
10192 bed = get_elf_backend_data (output_bfd);
10193 relocate_section = bed->elf_backend_relocate_section;
10194
10195 /* If this is a dynamic object, we don't want to do anything here:
10196 we don't want the local symbols, and we don't want the section
10197 contents. */
10198 if ((input_bfd->flags & DYNAMIC) != 0)
10199 return TRUE;
10200
10201 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10202 if (elf_bad_symtab (input_bfd))
10203 {
10204 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10205 extsymoff = 0;
10206 }
10207 else
10208 {
10209 locsymcount = symtab_hdr->sh_info;
10210 extsymoff = symtab_hdr->sh_info;
10211 }
10212
10213 /* Read the local symbols. */
10214 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10215 if (isymbuf == NULL && locsymcount != 0)
10216 {
10217 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10218 flinfo->internal_syms,
10219 flinfo->external_syms,
10220 flinfo->locsym_shndx);
10221 if (isymbuf == NULL)
10222 return FALSE;
10223 }
10224
10225 /* Find local symbol sections and adjust values of symbols in
10226 SEC_MERGE sections. Write out those local symbols we know are
10227 going into the output file. */
10228 isymend = isymbuf + locsymcount;
10229 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10230 isym < isymend;
10231 isym++, pindex++, ppsection++)
10232 {
10233 asection *isec;
10234 const char *name;
10235 Elf_Internal_Sym osym;
10236 long indx;
10237 int ret;
10238
10239 *pindex = -1;
10240
10241 if (elf_bad_symtab (input_bfd))
10242 {
10243 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10244 {
10245 *ppsection = NULL;
10246 continue;
10247 }
10248 }
10249
10250 if (isym->st_shndx == SHN_UNDEF)
10251 isec = bfd_und_section_ptr;
10252 else if (isym->st_shndx == SHN_ABS)
10253 isec = bfd_abs_section_ptr;
10254 else if (isym->st_shndx == SHN_COMMON)
10255 isec = bfd_com_section_ptr;
10256 else
10257 {
10258 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10259 if (isec == NULL)
10260 {
10261 /* Don't attempt to output symbols with st_shnx in the
10262 reserved range other than SHN_ABS and SHN_COMMON. */
10263 *ppsection = NULL;
10264 continue;
10265 }
10266 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10267 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10268 isym->st_value =
10269 _bfd_merged_section_offset (output_bfd, &isec,
10270 elf_section_data (isec)->sec_info,
10271 isym->st_value);
10272 }
10273
10274 *ppsection = isec;
10275
10276 /* Don't output the first, undefined, symbol. In fact, don't
10277 output any undefined local symbol. */
10278 if (isec == bfd_und_section_ptr)
10279 continue;
10280
10281 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10282 {
10283 /* We never output section symbols. Instead, we use the
10284 section symbol of the corresponding section in the output
10285 file. */
10286 continue;
10287 }
10288
10289 /* If we are stripping all symbols, we don't want to output this
10290 one. */
10291 if (flinfo->info->strip == strip_all)
10292 continue;
10293
10294 /* If we are discarding all local symbols, we don't want to
10295 output this one. If we are generating a relocatable output
10296 file, then some of the local symbols may be required by
10297 relocs; we output them below as we discover that they are
10298 needed. */
10299 if (flinfo->info->discard == discard_all)
10300 continue;
10301
10302 /* If this symbol is defined in a section which we are
10303 discarding, we don't need to keep it. */
10304 if (isym->st_shndx != SHN_UNDEF
10305 && isym->st_shndx < SHN_LORESERVE
10306 && bfd_section_removed_from_list (output_bfd,
10307 isec->output_section))
10308 continue;
10309
10310 /* Get the name of the symbol. */
10311 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10312 isym->st_name);
10313 if (name == NULL)
10314 return FALSE;
10315
10316 /* See if we are discarding symbols with this name. */
10317 if ((flinfo->info->strip == strip_some
10318 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10319 == NULL))
10320 || (((flinfo->info->discard == discard_sec_merge
10321 && (isec->flags & SEC_MERGE)
10322 && !bfd_link_relocatable (flinfo->info))
10323 || flinfo->info->discard == discard_l)
10324 && bfd_is_local_label_name (input_bfd, name)))
10325 continue;
10326
10327 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10328 {
10329 if (input_bfd->lto_output)
10330 /* -flto puts a temp file name here. This means builds
10331 are not reproducible. Discard the symbol. */
10332 continue;
10333 have_file_sym = TRUE;
10334 flinfo->filesym_count += 1;
10335 }
10336 if (!have_file_sym)
10337 {
10338 /* In the absence of debug info, bfd_find_nearest_line uses
10339 FILE symbols to determine the source file for local
10340 function symbols. Provide a FILE symbol here if input
10341 files lack such, so that their symbols won't be
10342 associated with a previous input file. It's not the
10343 source file, but the best we can do. */
10344 have_file_sym = TRUE;
10345 flinfo->filesym_count += 1;
10346 memset (&osym, 0, sizeof (osym));
10347 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10348 osym.st_shndx = SHN_ABS;
10349 if (!elf_link_output_symstrtab (flinfo,
10350 (input_bfd->lto_output ? NULL
10351 : input_bfd->filename),
10352 &osym, bfd_abs_section_ptr,
10353 NULL))
10354 return FALSE;
10355 }
10356
10357 osym = *isym;
10358
10359 /* Adjust the section index for the output file. */
10360 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10361 isec->output_section);
10362 if (osym.st_shndx == SHN_BAD)
10363 return FALSE;
10364
10365 /* ELF symbols in relocatable files are section relative, but
10366 in executable files they are virtual addresses. Note that
10367 this code assumes that all ELF sections have an associated
10368 BFD section with a reasonable value for output_offset; below
10369 we assume that they also have a reasonable value for
10370 output_section. Any special sections must be set up to meet
10371 these requirements. */
10372 osym.st_value += isec->output_offset;
10373 if (!bfd_link_relocatable (flinfo->info))
10374 {
10375 osym.st_value += isec->output_section->vma;
10376 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10377 {
10378 /* STT_TLS symbols are relative to PT_TLS segment base. */
10379 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10380 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10381 }
10382 }
10383
10384 indx = bfd_get_symcount (output_bfd);
10385 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10386 if (ret == 0)
10387 return FALSE;
10388 else if (ret == 1)
10389 *pindex = indx;
10390 }
10391
10392 if (bed->s->arch_size == 32)
10393 {
10394 r_type_mask = 0xff;
10395 r_sym_shift = 8;
10396 address_size = 4;
10397 }
10398 else
10399 {
10400 r_type_mask = 0xffffffff;
10401 r_sym_shift = 32;
10402 address_size = 8;
10403 }
10404
10405 /* Relocate the contents of each section. */
10406 sym_hashes = elf_sym_hashes (input_bfd);
10407 for (o = input_bfd->sections; o != NULL; o = o->next)
10408 {
10409 bfd_byte *contents;
10410
10411 if (! o->linker_mark)
10412 {
10413 /* This section was omitted from the link. */
10414 continue;
10415 }
10416
10417 if (!flinfo->info->resolve_section_groups
10418 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10419 {
10420 /* Deal with the group signature symbol. */
10421 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10422 unsigned long symndx = sec_data->this_hdr.sh_info;
10423 asection *osec = o->output_section;
10424
10425 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10426 if (symndx >= locsymcount
10427 || (elf_bad_symtab (input_bfd)
10428 && flinfo->sections[symndx] == NULL))
10429 {
10430 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10431 while (h->root.type == bfd_link_hash_indirect
10432 || h->root.type == bfd_link_hash_warning)
10433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10434 /* Arrange for symbol to be output. */
10435 h->indx = -2;
10436 elf_section_data (osec)->this_hdr.sh_info = -2;
10437 }
10438 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10439 {
10440 /* We'll use the output section target_index. */
10441 asection *sec = flinfo->sections[symndx]->output_section;
10442 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10443 }
10444 else
10445 {
10446 if (flinfo->indices[symndx] == -1)
10447 {
10448 /* Otherwise output the local symbol now. */
10449 Elf_Internal_Sym sym = isymbuf[symndx];
10450 asection *sec = flinfo->sections[symndx]->output_section;
10451 const char *name;
10452 long indx;
10453 int ret;
10454
10455 name = bfd_elf_string_from_elf_section (input_bfd,
10456 symtab_hdr->sh_link,
10457 sym.st_name);
10458 if (name == NULL)
10459 return FALSE;
10460
10461 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10462 sec);
10463 if (sym.st_shndx == SHN_BAD)
10464 return FALSE;
10465
10466 sym.st_value += o->output_offset;
10467
10468 indx = bfd_get_symcount (output_bfd);
10469 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10470 NULL);
10471 if (ret == 0)
10472 return FALSE;
10473 else if (ret == 1)
10474 flinfo->indices[symndx] = indx;
10475 else
10476 abort ();
10477 }
10478 elf_section_data (osec)->this_hdr.sh_info
10479 = flinfo->indices[symndx];
10480 }
10481 }
10482
10483 if ((o->flags & SEC_HAS_CONTENTS) == 0
10484 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10485 continue;
10486
10487 if ((o->flags & SEC_LINKER_CREATED) != 0)
10488 {
10489 /* Section was created by _bfd_elf_link_create_dynamic_sections
10490 or somesuch. */
10491 continue;
10492 }
10493
10494 /* Get the contents of the section. They have been cached by a
10495 relaxation routine. Note that o is a section in an input
10496 file, so the contents field will not have been set by any of
10497 the routines which work on output files. */
10498 if (elf_section_data (o)->this_hdr.contents != NULL)
10499 {
10500 contents = elf_section_data (o)->this_hdr.contents;
10501 if (bed->caches_rawsize
10502 && o->rawsize != 0
10503 && o->rawsize < o->size)
10504 {
10505 memcpy (flinfo->contents, contents, o->rawsize);
10506 contents = flinfo->contents;
10507 }
10508 }
10509 else
10510 {
10511 contents = flinfo->contents;
10512 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10513 return FALSE;
10514 }
10515
10516 if ((o->flags & SEC_RELOC) != 0)
10517 {
10518 Elf_Internal_Rela *internal_relocs;
10519 Elf_Internal_Rela *rel, *relend;
10520 int action_discarded;
10521 int ret;
10522
10523 /* Get the swapped relocs. */
10524 internal_relocs
10525 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10526 flinfo->internal_relocs, FALSE);
10527 if (internal_relocs == NULL
10528 && o->reloc_count > 0)
10529 return FALSE;
10530
10531 /* We need to reverse-copy input .ctors/.dtors sections if
10532 they are placed in .init_array/.finit_array for output. */
10533 if (o->size > address_size
10534 && ((strncmp (o->name, ".ctors", 6) == 0
10535 && strcmp (o->output_section->name,
10536 ".init_array") == 0)
10537 || (strncmp (o->name, ".dtors", 6) == 0
10538 && strcmp (o->output_section->name,
10539 ".fini_array") == 0))
10540 && (o->name[6] == 0 || o->name[6] == '.'))
10541 {
10542 if (o->size * bed->s->int_rels_per_ext_rel
10543 != o->reloc_count * address_size)
10544 {
10545 _bfd_error_handler
10546 /* xgettext:c-format */
10547 (_("error: %B: size of section %A is not "
10548 "multiple of address size"),
10549 input_bfd, o);
10550 bfd_set_error (bfd_error_bad_value);
10551 return FALSE;
10552 }
10553 o->flags |= SEC_ELF_REVERSE_COPY;
10554 }
10555
10556 action_discarded = -1;
10557 if (!elf_section_ignore_discarded_relocs (o))
10558 action_discarded = (*bed->action_discarded) (o);
10559
10560 /* Run through the relocs evaluating complex reloc symbols and
10561 looking for relocs against symbols from discarded sections
10562 or section symbols from removed link-once sections.
10563 Complain about relocs against discarded sections. Zero
10564 relocs against removed link-once sections. */
10565
10566 rel = internal_relocs;
10567 relend = rel + o->reloc_count;
10568 for ( ; rel < relend; rel++)
10569 {
10570 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10571 unsigned int s_type;
10572 asection **ps, *sec;
10573 struct elf_link_hash_entry *h = NULL;
10574 const char *sym_name;
10575
10576 if (r_symndx == STN_UNDEF)
10577 continue;
10578
10579 if (r_symndx >= locsymcount
10580 || (elf_bad_symtab (input_bfd)
10581 && flinfo->sections[r_symndx] == NULL))
10582 {
10583 h = sym_hashes[r_symndx - extsymoff];
10584
10585 /* Badly formatted input files can contain relocs that
10586 reference non-existant symbols. Check here so that
10587 we do not seg fault. */
10588 if (h == NULL)
10589 {
10590 _bfd_error_handler
10591 /* xgettext:c-format */
10592 (_("error: %B contains a reloc (%#Lx) for section %A "
10593 "that references a non-existent global symbol"),
10594 input_bfd, rel->r_info, o);
10595 bfd_set_error (bfd_error_bad_value);
10596 return FALSE;
10597 }
10598
10599 while (h->root.type == bfd_link_hash_indirect
10600 || h->root.type == bfd_link_hash_warning)
10601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10602
10603 s_type = h->type;
10604
10605 /* If a plugin symbol is referenced from a non-IR file,
10606 mark the symbol as undefined. Note that the
10607 linker may attach linker created dynamic sections
10608 to the plugin bfd. Symbols defined in linker
10609 created sections are not plugin symbols. */
10610 if ((h->root.non_ir_ref_regular
10611 || h->root.non_ir_ref_dynamic)
10612 && (h->root.type == bfd_link_hash_defined
10613 || h->root.type == bfd_link_hash_defweak)
10614 && (h->root.u.def.section->flags
10615 & SEC_LINKER_CREATED) == 0
10616 && h->root.u.def.section->owner != NULL
10617 && (h->root.u.def.section->owner->flags
10618 & BFD_PLUGIN) != 0)
10619 {
10620 h->root.type = bfd_link_hash_undefined;
10621 h->root.u.undef.abfd = h->root.u.def.section->owner;
10622 }
10623
10624 ps = NULL;
10625 if (h->root.type == bfd_link_hash_defined
10626 || h->root.type == bfd_link_hash_defweak)
10627 ps = &h->root.u.def.section;
10628
10629 sym_name = h->root.root.string;
10630 }
10631 else
10632 {
10633 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10634
10635 s_type = ELF_ST_TYPE (sym->st_info);
10636 ps = &flinfo->sections[r_symndx];
10637 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10638 sym, *ps);
10639 }
10640
10641 if ((s_type == STT_RELC || s_type == STT_SRELC)
10642 && !bfd_link_relocatable (flinfo->info))
10643 {
10644 bfd_vma val;
10645 bfd_vma dot = (rel->r_offset
10646 + o->output_offset + o->output_section->vma);
10647 #ifdef DEBUG
10648 printf ("Encountered a complex symbol!");
10649 printf (" (input_bfd %s, section %s, reloc %ld\n",
10650 input_bfd->filename, o->name,
10651 (long) (rel - internal_relocs));
10652 printf (" symbol: idx %8.8lx, name %s\n",
10653 r_symndx, sym_name);
10654 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10655 (unsigned long) rel->r_info,
10656 (unsigned long) rel->r_offset);
10657 #endif
10658 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10659 isymbuf, locsymcount, s_type == STT_SRELC))
10660 return FALSE;
10661
10662 /* Symbol evaluated OK. Update to absolute value. */
10663 set_symbol_value (input_bfd, isymbuf, locsymcount,
10664 r_symndx, val);
10665 continue;
10666 }
10667
10668 if (action_discarded != -1 && ps != NULL)
10669 {
10670 /* Complain if the definition comes from a
10671 discarded section. */
10672 if ((sec = *ps) != NULL && discarded_section (sec))
10673 {
10674 BFD_ASSERT (r_symndx != STN_UNDEF);
10675 if (action_discarded & COMPLAIN)
10676 (*flinfo->info->callbacks->einfo)
10677 /* xgettext:c-format */
10678 (_("%X`%s' referenced in section `%A' of %B: "
10679 "defined in discarded section `%A' of %B\n"),
10680 sym_name, o, input_bfd, sec, sec->owner);
10681
10682 /* Try to do the best we can to support buggy old
10683 versions of gcc. Pretend that the symbol is
10684 really defined in the kept linkonce section.
10685 FIXME: This is quite broken. Modifying the
10686 symbol here means we will be changing all later
10687 uses of the symbol, not just in this section. */
10688 if (action_discarded & PRETEND)
10689 {
10690 asection *kept;
10691
10692 kept = _bfd_elf_check_kept_section (sec,
10693 flinfo->info);
10694 if (kept != NULL)
10695 {
10696 *ps = kept;
10697 continue;
10698 }
10699 }
10700 }
10701 }
10702 }
10703
10704 /* Relocate the section by invoking a back end routine.
10705
10706 The back end routine is responsible for adjusting the
10707 section contents as necessary, and (if using Rela relocs
10708 and generating a relocatable output file) adjusting the
10709 reloc addend as necessary.
10710
10711 The back end routine does not have to worry about setting
10712 the reloc address or the reloc symbol index.
10713
10714 The back end routine is given a pointer to the swapped in
10715 internal symbols, and can access the hash table entries
10716 for the external symbols via elf_sym_hashes (input_bfd).
10717
10718 When generating relocatable output, the back end routine
10719 must handle STB_LOCAL/STT_SECTION symbols specially. The
10720 output symbol is going to be a section symbol
10721 corresponding to the output section, which will require
10722 the addend to be adjusted. */
10723
10724 ret = (*relocate_section) (output_bfd, flinfo->info,
10725 input_bfd, o, contents,
10726 internal_relocs,
10727 isymbuf,
10728 flinfo->sections);
10729 if (!ret)
10730 return FALSE;
10731
10732 if (ret == 2
10733 || bfd_link_relocatable (flinfo->info)
10734 || flinfo->info->emitrelocations)
10735 {
10736 Elf_Internal_Rela *irela;
10737 Elf_Internal_Rela *irelaend, *irelamid;
10738 bfd_vma last_offset;
10739 struct elf_link_hash_entry **rel_hash;
10740 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10741 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10742 unsigned int next_erel;
10743 bfd_boolean rela_normal;
10744 struct bfd_elf_section_data *esdi, *esdo;
10745
10746 esdi = elf_section_data (o);
10747 esdo = elf_section_data (o->output_section);
10748 rela_normal = FALSE;
10749
10750 /* Adjust the reloc addresses and symbol indices. */
10751
10752 irela = internal_relocs;
10753 irelaend = irela + o->reloc_count;
10754 rel_hash = esdo->rel.hashes + esdo->rel.count;
10755 /* We start processing the REL relocs, if any. When we reach
10756 IRELAMID in the loop, we switch to the RELA relocs. */
10757 irelamid = irela;
10758 if (esdi->rel.hdr != NULL)
10759 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10760 * bed->s->int_rels_per_ext_rel);
10761 rel_hash_list = rel_hash;
10762 rela_hash_list = NULL;
10763 last_offset = o->output_offset;
10764 if (!bfd_link_relocatable (flinfo->info))
10765 last_offset += o->output_section->vma;
10766 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10767 {
10768 unsigned long r_symndx;
10769 asection *sec;
10770 Elf_Internal_Sym sym;
10771
10772 if (next_erel == bed->s->int_rels_per_ext_rel)
10773 {
10774 rel_hash++;
10775 next_erel = 0;
10776 }
10777
10778 if (irela == irelamid)
10779 {
10780 rel_hash = esdo->rela.hashes + esdo->rela.count;
10781 rela_hash_list = rel_hash;
10782 rela_normal = bed->rela_normal;
10783 }
10784
10785 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10786 flinfo->info, o,
10787 irela->r_offset);
10788 if (irela->r_offset >= (bfd_vma) -2)
10789 {
10790 /* This is a reloc for a deleted entry or somesuch.
10791 Turn it into an R_*_NONE reloc, at the same
10792 offset as the last reloc. elf_eh_frame.c and
10793 bfd_elf_discard_info rely on reloc offsets
10794 being ordered. */
10795 irela->r_offset = last_offset;
10796 irela->r_info = 0;
10797 irela->r_addend = 0;
10798 continue;
10799 }
10800
10801 irela->r_offset += o->output_offset;
10802
10803 /* Relocs in an executable have to be virtual addresses. */
10804 if (!bfd_link_relocatable (flinfo->info))
10805 irela->r_offset += o->output_section->vma;
10806
10807 last_offset = irela->r_offset;
10808
10809 r_symndx = irela->r_info >> r_sym_shift;
10810 if (r_symndx == STN_UNDEF)
10811 continue;
10812
10813 if (r_symndx >= locsymcount
10814 || (elf_bad_symtab (input_bfd)
10815 && flinfo->sections[r_symndx] == NULL))
10816 {
10817 struct elf_link_hash_entry *rh;
10818 unsigned long indx;
10819
10820 /* This is a reloc against a global symbol. We
10821 have not yet output all the local symbols, so
10822 we do not know the symbol index of any global
10823 symbol. We set the rel_hash entry for this
10824 reloc to point to the global hash table entry
10825 for this symbol. The symbol index is then
10826 set at the end of bfd_elf_final_link. */
10827 indx = r_symndx - extsymoff;
10828 rh = elf_sym_hashes (input_bfd)[indx];
10829 while (rh->root.type == bfd_link_hash_indirect
10830 || rh->root.type == bfd_link_hash_warning)
10831 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10832
10833 /* Setting the index to -2 tells
10834 elf_link_output_extsym that this symbol is
10835 used by a reloc. */
10836 BFD_ASSERT (rh->indx < 0);
10837 rh->indx = -2;
10838 *rel_hash = rh;
10839
10840 continue;
10841 }
10842
10843 /* This is a reloc against a local symbol. */
10844
10845 *rel_hash = NULL;
10846 sym = isymbuf[r_symndx];
10847 sec = flinfo->sections[r_symndx];
10848 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10849 {
10850 /* I suppose the backend ought to fill in the
10851 section of any STT_SECTION symbol against a
10852 processor specific section. */
10853 r_symndx = STN_UNDEF;
10854 if (bfd_is_abs_section (sec))
10855 ;
10856 else if (sec == NULL || sec->owner == NULL)
10857 {
10858 bfd_set_error (bfd_error_bad_value);
10859 return FALSE;
10860 }
10861 else
10862 {
10863 asection *osec = sec->output_section;
10864
10865 /* If we have discarded a section, the output
10866 section will be the absolute section. In
10867 case of discarded SEC_MERGE sections, use
10868 the kept section. relocate_section should
10869 have already handled discarded linkonce
10870 sections. */
10871 if (bfd_is_abs_section (osec)
10872 && sec->kept_section != NULL
10873 && sec->kept_section->output_section != NULL)
10874 {
10875 osec = sec->kept_section->output_section;
10876 irela->r_addend -= osec->vma;
10877 }
10878
10879 if (!bfd_is_abs_section (osec))
10880 {
10881 r_symndx = osec->target_index;
10882 if (r_symndx == STN_UNDEF)
10883 {
10884 irela->r_addend += osec->vma;
10885 osec = _bfd_nearby_section (output_bfd, osec,
10886 osec->vma);
10887 irela->r_addend -= osec->vma;
10888 r_symndx = osec->target_index;
10889 }
10890 }
10891 }
10892
10893 /* Adjust the addend according to where the
10894 section winds up in the output section. */
10895 if (rela_normal)
10896 irela->r_addend += sec->output_offset;
10897 }
10898 else
10899 {
10900 if (flinfo->indices[r_symndx] == -1)
10901 {
10902 unsigned long shlink;
10903 const char *name;
10904 asection *osec;
10905 long indx;
10906
10907 if (flinfo->info->strip == strip_all)
10908 {
10909 /* You can't do ld -r -s. */
10910 bfd_set_error (bfd_error_invalid_operation);
10911 return FALSE;
10912 }
10913
10914 /* This symbol was skipped earlier, but
10915 since it is needed by a reloc, we
10916 must output it now. */
10917 shlink = symtab_hdr->sh_link;
10918 name = (bfd_elf_string_from_elf_section
10919 (input_bfd, shlink, sym.st_name));
10920 if (name == NULL)
10921 return FALSE;
10922
10923 osec = sec->output_section;
10924 sym.st_shndx =
10925 _bfd_elf_section_from_bfd_section (output_bfd,
10926 osec);
10927 if (sym.st_shndx == SHN_BAD)
10928 return FALSE;
10929
10930 sym.st_value += sec->output_offset;
10931 if (!bfd_link_relocatable (flinfo->info))
10932 {
10933 sym.st_value += osec->vma;
10934 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10935 {
10936 /* STT_TLS symbols are relative to PT_TLS
10937 segment base. */
10938 BFD_ASSERT (elf_hash_table (flinfo->info)
10939 ->tls_sec != NULL);
10940 sym.st_value -= (elf_hash_table (flinfo->info)
10941 ->tls_sec->vma);
10942 }
10943 }
10944
10945 indx = bfd_get_symcount (output_bfd);
10946 ret = elf_link_output_symstrtab (flinfo, name,
10947 &sym, sec,
10948 NULL);
10949 if (ret == 0)
10950 return FALSE;
10951 else if (ret == 1)
10952 flinfo->indices[r_symndx] = indx;
10953 else
10954 abort ();
10955 }
10956
10957 r_symndx = flinfo->indices[r_symndx];
10958 }
10959
10960 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10961 | (irela->r_info & r_type_mask));
10962 }
10963
10964 /* Swap out the relocs. */
10965 input_rel_hdr = esdi->rel.hdr;
10966 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10967 {
10968 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10969 input_rel_hdr,
10970 internal_relocs,
10971 rel_hash_list))
10972 return FALSE;
10973 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10974 * bed->s->int_rels_per_ext_rel);
10975 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10976 }
10977
10978 input_rela_hdr = esdi->rela.hdr;
10979 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10980 {
10981 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10982 input_rela_hdr,
10983 internal_relocs,
10984 rela_hash_list))
10985 return FALSE;
10986 }
10987 }
10988 }
10989
10990 /* Write out the modified section contents. */
10991 if (bed->elf_backend_write_section
10992 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10993 contents))
10994 {
10995 /* Section written out. */
10996 }
10997 else switch (o->sec_info_type)
10998 {
10999 case SEC_INFO_TYPE_STABS:
11000 if (! (_bfd_write_section_stabs
11001 (output_bfd,
11002 &elf_hash_table (flinfo->info)->stab_info,
11003 o, &elf_section_data (o)->sec_info, contents)))
11004 return FALSE;
11005 break;
11006 case SEC_INFO_TYPE_MERGE:
11007 if (! _bfd_write_merged_section (output_bfd, o,
11008 elf_section_data (o)->sec_info))
11009 return FALSE;
11010 break;
11011 case SEC_INFO_TYPE_EH_FRAME:
11012 {
11013 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11014 o, contents))
11015 return FALSE;
11016 }
11017 break;
11018 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11019 {
11020 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11021 flinfo->info,
11022 o, contents))
11023 return FALSE;
11024 }
11025 break;
11026 default:
11027 {
11028 if (! (o->flags & SEC_EXCLUDE))
11029 {
11030 file_ptr offset = (file_ptr) o->output_offset;
11031 bfd_size_type todo = o->size;
11032
11033 offset *= bfd_octets_per_byte (output_bfd);
11034
11035 if ((o->flags & SEC_ELF_REVERSE_COPY))
11036 {
11037 /* Reverse-copy input section to output. */
11038 do
11039 {
11040 todo -= address_size;
11041 if (! bfd_set_section_contents (output_bfd,
11042 o->output_section,
11043 contents + todo,
11044 offset,
11045 address_size))
11046 return FALSE;
11047 if (todo == 0)
11048 break;
11049 offset += address_size;
11050 }
11051 while (1);
11052 }
11053 else if (! bfd_set_section_contents (output_bfd,
11054 o->output_section,
11055 contents,
11056 offset, todo))
11057 return FALSE;
11058 }
11059 }
11060 break;
11061 }
11062 }
11063
11064 return TRUE;
11065 }
11066
11067 /* Generate a reloc when linking an ELF file. This is a reloc
11068 requested by the linker, and does not come from any input file. This
11069 is used to build constructor and destructor tables when linking
11070 with -Ur. */
11071
11072 static bfd_boolean
11073 elf_reloc_link_order (bfd *output_bfd,
11074 struct bfd_link_info *info,
11075 asection *output_section,
11076 struct bfd_link_order *link_order)
11077 {
11078 reloc_howto_type *howto;
11079 long indx;
11080 bfd_vma offset;
11081 bfd_vma addend;
11082 struct bfd_elf_section_reloc_data *reldata;
11083 struct elf_link_hash_entry **rel_hash_ptr;
11084 Elf_Internal_Shdr *rel_hdr;
11085 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11086 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11087 bfd_byte *erel;
11088 unsigned int i;
11089 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11090
11091 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11092 if (howto == NULL)
11093 {
11094 bfd_set_error (bfd_error_bad_value);
11095 return FALSE;
11096 }
11097
11098 addend = link_order->u.reloc.p->addend;
11099
11100 if (esdo->rel.hdr)
11101 reldata = &esdo->rel;
11102 else if (esdo->rela.hdr)
11103 reldata = &esdo->rela;
11104 else
11105 {
11106 reldata = NULL;
11107 BFD_ASSERT (0);
11108 }
11109
11110 /* Figure out the symbol index. */
11111 rel_hash_ptr = reldata->hashes + reldata->count;
11112 if (link_order->type == bfd_section_reloc_link_order)
11113 {
11114 indx = link_order->u.reloc.p->u.section->target_index;
11115 BFD_ASSERT (indx != 0);
11116 *rel_hash_ptr = NULL;
11117 }
11118 else
11119 {
11120 struct elf_link_hash_entry *h;
11121
11122 /* Treat a reloc against a defined symbol as though it were
11123 actually against the section. */
11124 h = ((struct elf_link_hash_entry *)
11125 bfd_wrapped_link_hash_lookup (output_bfd, info,
11126 link_order->u.reloc.p->u.name,
11127 FALSE, FALSE, TRUE));
11128 if (h != NULL
11129 && (h->root.type == bfd_link_hash_defined
11130 || h->root.type == bfd_link_hash_defweak))
11131 {
11132 asection *section;
11133
11134 section = h->root.u.def.section;
11135 indx = section->output_section->target_index;
11136 *rel_hash_ptr = NULL;
11137 /* It seems that we ought to add the symbol value to the
11138 addend here, but in practice it has already been added
11139 because it was passed to constructor_callback. */
11140 addend += section->output_section->vma + section->output_offset;
11141 }
11142 else if (h != NULL)
11143 {
11144 /* Setting the index to -2 tells elf_link_output_extsym that
11145 this symbol is used by a reloc. */
11146 h->indx = -2;
11147 *rel_hash_ptr = h;
11148 indx = 0;
11149 }
11150 else
11151 {
11152 (*info->callbacks->unattached_reloc)
11153 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11154 indx = 0;
11155 }
11156 }
11157
11158 /* If this is an inplace reloc, we must write the addend into the
11159 object file. */
11160 if (howto->partial_inplace && addend != 0)
11161 {
11162 bfd_size_type size;
11163 bfd_reloc_status_type rstat;
11164 bfd_byte *buf;
11165 bfd_boolean ok;
11166 const char *sym_name;
11167
11168 size = (bfd_size_type) bfd_get_reloc_size (howto);
11169 buf = (bfd_byte *) bfd_zmalloc (size);
11170 if (buf == NULL && size != 0)
11171 return FALSE;
11172 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11173 switch (rstat)
11174 {
11175 case bfd_reloc_ok:
11176 break;
11177
11178 default:
11179 case bfd_reloc_outofrange:
11180 abort ();
11181
11182 case bfd_reloc_overflow:
11183 if (link_order->type == bfd_section_reloc_link_order)
11184 sym_name = bfd_section_name (output_bfd,
11185 link_order->u.reloc.p->u.section);
11186 else
11187 sym_name = link_order->u.reloc.p->u.name;
11188 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11189 howto->name, addend, NULL, NULL,
11190 (bfd_vma) 0);
11191 break;
11192 }
11193
11194 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11195 link_order->offset
11196 * bfd_octets_per_byte (output_bfd),
11197 size);
11198 free (buf);
11199 if (! ok)
11200 return FALSE;
11201 }
11202
11203 /* The address of a reloc is relative to the section in a
11204 relocatable file, and is a virtual address in an executable
11205 file. */
11206 offset = link_order->offset;
11207 if (! bfd_link_relocatable (info))
11208 offset += output_section->vma;
11209
11210 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11211 {
11212 irel[i].r_offset = offset;
11213 irel[i].r_info = 0;
11214 irel[i].r_addend = 0;
11215 }
11216 if (bed->s->arch_size == 32)
11217 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11218 else
11219 #ifdef BFD64
11220 {
11221 bfd_uint64_t indx64 = indx;
11222 irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
11223 }
11224 #else
11225 BFD_FAIL();
11226 #endif
11227
11228 rel_hdr = reldata->hdr;
11229 erel = rel_hdr->contents;
11230 if (rel_hdr->sh_type == SHT_REL)
11231 {
11232 erel += reldata->count * bed->s->sizeof_rel;
11233 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11234 }
11235 else
11236 {
11237 irel[0].r_addend = addend;
11238 erel += reldata->count * bed->s->sizeof_rela;
11239 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11240 }
11241
11242 ++reldata->count;
11243
11244 return TRUE;
11245 }
11246
11247
11248 /* Get the output vma of the section pointed to by the sh_link field. */
11249
11250 static bfd_vma
11251 elf_get_linked_section_vma (struct bfd_link_order *p)
11252 {
11253 Elf_Internal_Shdr **elf_shdrp;
11254 asection *s;
11255 int elfsec;
11256
11257 s = p->u.indirect.section;
11258 elf_shdrp = elf_elfsections (s->owner);
11259 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11260 elfsec = elf_shdrp[elfsec]->sh_link;
11261 /* PR 290:
11262 The Intel C compiler generates SHT_IA_64_UNWIND with
11263 SHF_LINK_ORDER. But it doesn't set the sh_link or
11264 sh_info fields. Hence we could get the situation
11265 where elfsec is 0. */
11266 if (elfsec == 0)
11267 {
11268 const struct elf_backend_data *bed
11269 = get_elf_backend_data (s->owner);
11270 if (bed->link_order_error_handler)
11271 bed->link_order_error_handler
11272 /* xgettext:c-format */
11273 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11274 return 0;
11275 }
11276 else
11277 {
11278 s = elf_shdrp[elfsec]->bfd_section;
11279 return s->output_section->vma + s->output_offset;
11280 }
11281 }
11282
11283
11284 /* Compare two sections based on the locations of the sections they are
11285 linked to. Used by elf_fixup_link_order. */
11286
11287 static int
11288 compare_link_order (const void * a, const void * b)
11289 {
11290 bfd_vma apos;
11291 bfd_vma bpos;
11292
11293 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11294 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11295 if (apos < bpos)
11296 return -1;
11297 return apos > bpos;
11298 }
11299
11300
11301 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11302 order as their linked sections. Returns false if this could not be done
11303 because an output section includes both ordered and unordered
11304 sections. Ideally we'd do this in the linker proper. */
11305
11306 static bfd_boolean
11307 elf_fixup_link_order (bfd *abfd, asection *o)
11308 {
11309 int seen_linkorder;
11310 int seen_other;
11311 int n;
11312 struct bfd_link_order *p;
11313 bfd *sub;
11314 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11315 unsigned elfsec;
11316 struct bfd_link_order **sections;
11317 asection *s, *other_sec, *linkorder_sec;
11318 bfd_vma offset;
11319
11320 other_sec = NULL;
11321 linkorder_sec = NULL;
11322 seen_other = 0;
11323 seen_linkorder = 0;
11324 for (p = o->map_head.link_order; p != NULL; p = p->next)
11325 {
11326 if (p->type == bfd_indirect_link_order)
11327 {
11328 s = p->u.indirect.section;
11329 sub = s->owner;
11330 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11331 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11332 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11333 && elfsec < elf_numsections (sub)
11334 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11335 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11336 {
11337 seen_linkorder++;
11338 linkorder_sec = s;
11339 }
11340 else
11341 {
11342 seen_other++;
11343 other_sec = s;
11344 }
11345 }
11346 else
11347 seen_other++;
11348
11349 if (seen_other && seen_linkorder)
11350 {
11351 if (other_sec && linkorder_sec)
11352 _bfd_error_handler
11353 /* xgettext:c-format */
11354 (_("%A has both ordered [`%A' in %B] "
11355 "and unordered [`%A' in %B] sections"),
11356 o, linkorder_sec, linkorder_sec->owner,
11357 other_sec, other_sec->owner);
11358 else
11359 _bfd_error_handler
11360 (_("%A has both ordered and unordered sections"), o);
11361 bfd_set_error (bfd_error_bad_value);
11362 return FALSE;
11363 }
11364 }
11365
11366 if (!seen_linkorder)
11367 return TRUE;
11368
11369 sections = (struct bfd_link_order **)
11370 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11371 if (sections == NULL)
11372 return FALSE;
11373 seen_linkorder = 0;
11374
11375 for (p = o->map_head.link_order; p != NULL; p = p->next)
11376 {
11377 sections[seen_linkorder++] = p;
11378 }
11379 /* Sort the input sections in the order of their linked section. */
11380 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11381 compare_link_order);
11382
11383 /* Change the offsets of the sections. */
11384 offset = 0;
11385 for (n = 0; n < seen_linkorder; n++)
11386 {
11387 s = sections[n]->u.indirect.section;
11388 offset &= ~(bfd_vma) 0 << s->alignment_power;
11389 s->output_offset = offset / bfd_octets_per_byte (abfd);
11390 sections[n]->offset = offset;
11391 offset += sections[n]->size;
11392 }
11393
11394 free (sections);
11395 return TRUE;
11396 }
11397
11398 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11399 Returns TRUE upon success, FALSE otherwise. */
11400
11401 static bfd_boolean
11402 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11403 {
11404 bfd_boolean ret = FALSE;
11405 bfd *implib_bfd;
11406 const struct elf_backend_data *bed;
11407 flagword flags;
11408 enum bfd_architecture arch;
11409 unsigned int mach;
11410 asymbol **sympp = NULL;
11411 long symsize;
11412 long symcount;
11413 long src_count;
11414 elf_symbol_type *osymbuf;
11415
11416 implib_bfd = info->out_implib_bfd;
11417 bed = get_elf_backend_data (abfd);
11418
11419 if (!bfd_set_format (implib_bfd, bfd_object))
11420 return FALSE;
11421
11422 /* Use flag from executable but make it a relocatable object. */
11423 flags = bfd_get_file_flags (abfd);
11424 flags &= ~HAS_RELOC;
11425 if (!bfd_set_start_address (implib_bfd, 0)
11426 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11427 return FALSE;
11428
11429 /* Copy architecture of output file to import library file. */
11430 arch = bfd_get_arch (abfd);
11431 mach = bfd_get_mach (abfd);
11432 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11433 && (abfd->target_defaulted
11434 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11435 return FALSE;
11436
11437 /* Get symbol table size. */
11438 symsize = bfd_get_symtab_upper_bound (abfd);
11439 if (symsize < 0)
11440 return FALSE;
11441
11442 /* Read in the symbol table. */
11443 sympp = (asymbol **) xmalloc (symsize);
11444 symcount = bfd_canonicalize_symtab (abfd, sympp);
11445 if (symcount < 0)
11446 goto free_sym_buf;
11447
11448 /* Allow the BFD backend to copy any private header data it
11449 understands from the output BFD to the import library BFD. */
11450 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11451 goto free_sym_buf;
11452
11453 /* Filter symbols to appear in the import library. */
11454 if (bed->elf_backend_filter_implib_symbols)
11455 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11456 symcount);
11457 else
11458 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11459 if (symcount == 0)
11460 {
11461 bfd_set_error (bfd_error_no_symbols);
11462 _bfd_error_handler (_("%B: no symbol found for import library"),
11463 implib_bfd);
11464 goto free_sym_buf;
11465 }
11466
11467
11468 /* Make symbols absolute. */
11469 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11470 sizeof (*osymbuf));
11471 for (src_count = 0; src_count < symcount; src_count++)
11472 {
11473 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11474 sizeof (*osymbuf));
11475 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11476 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11477 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11478 osymbuf[src_count].internal_elf_sym.st_value =
11479 osymbuf[src_count].symbol.value;
11480 sympp[src_count] = &osymbuf[src_count].symbol;
11481 }
11482
11483 bfd_set_symtab (implib_bfd, sympp, symcount);
11484
11485 /* Allow the BFD backend to copy any private data it understands
11486 from the output BFD to the import library BFD. This is done last
11487 to permit the routine to look at the filtered symbol table. */
11488 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11489 goto free_sym_buf;
11490
11491 if (!bfd_close (implib_bfd))
11492 goto free_sym_buf;
11493
11494 ret = TRUE;
11495
11496 free_sym_buf:
11497 free (sympp);
11498 return ret;
11499 }
11500
11501 static void
11502 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11503 {
11504 asection *o;
11505
11506 if (flinfo->symstrtab != NULL)
11507 _bfd_elf_strtab_free (flinfo->symstrtab);
11508 if (flinfo->contents != NULL)
11509 free (flinfo->contents);
11510 if (flinfo->external_relocs != NULL)
11511 free (flinfo->external_relocs);
11512 if (flinfo->internal_relocs != NULL)
11513 free (flinfo->internal_relocs);
11514 if (flinfo->external_syms != NULL)
11515 free (flinfo->external_syms);
11516 if (flinfo->locsym_shndx != NULL)
11517 free (flinfo->locsym_shndx);
11518 if (flinfo->internal_syms != NULL)
11519 free (flinfo->internal_syms);
11520 if (flinfo->indices != NULL)
11521 free (flinfo->indices);
11522 if (flinfo->sections != NULL)
11523 free (flinfo->sections);
11524 if (flinfo->symshndxbuf != NULL)
11525 free (flinfo->symshndxbuf);
11526 for (o = obfd->sections; o != NULL; o = o->next)
11527 {
11528 struct bfd_elf_section_data *esdo = elf_section_data (o);
11529 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11530 free (esdo->rel.hashes);
11531 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11532 free (esdo->rela.hashes);
11533 }
11534 }
11535
11536 /* Do the final step of an ELF link. */
11537
11538 bfd_boolean
11539 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11540 {
11541 bfd_boolean dynamic;
11542 bfd_boolean emit_relocs;
11543 bfd *dynobj;
11544 struct elf_final_link_info flinfo;
11545 asection *o;
11546 struct bfd_link_order *p;
11547 bfd *sub;
11548 bfd_size_type max_contents_size;
11549 bfd_size_type max_external_reloc_size;
11550 bfd_size_type max_internal_reloc_count;
11551 bfd_size_type max_sym_count;
11552 bfd_size_type max_sym_shndx_count;
11553 Elf_Internal_Sym elfsym;
11554 unsigned int i;
11555 Elf_Internal_Shdr *symtab_hdr;
11556 Elf_Internal_Shdr *symtab_shndx_hdr;
11557 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11558 struct elf_outext_info eoinfo;
11559 bfd_boolean merged;
11560 size_t relativecount = 0;
11561 asection *reldyn = 0;
11562 bfd_size_type amt;
11563 asection *attr_section = NULL;
11564 bfd_vma attr_size = 0;
11565 const char *std_attrs_section;
11566 struct elf_link_hash_table *htab = elf_hash_table (info);
11567
11568 if (!is_elf_hash_table (htab))
11569 return FALSE;
11570
11571 if (bfd_link_pic (info))
11572 abfd->flags |= DYNAMIC;
11573
11574 dynamic = htab->dynamic_sections_created;
11575 dynobj = htab->dynobj;
11576
11577 emit_relocs = (bfd_link_relocatable (info)
11578 || info->emitrelocations);
11579
11580 flinfo.info = info;
11581 flinfo.output_bfd = abfd;
11582 flinfo.symstrtab = _bfd_elf_strtab_init ();
11583 if (flinfo.symstrtab == NULL)
11584 return FALSE;
11585
11586 if (! dynamic)
11587 {
11588 flinfo.hash_sec = NULL;
11589 flinfo.symver_sec = NULL;
11590 }
11591 else
11592 {
11593 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11594 /* Note that dynsym_sec can be NULL (on VMS). */
11595 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11596 /* Note that it is OK if symver_sec is NULL. */
11597 }
11598
11599 flinfo.contents = NULL;
11600 flinfo.external_relocs = NULL;
11601 flinfo.internal_relocs = NULL;
11602 flinfo.external_syms = NULL;
11603 flinfo.locsym_shndx = NULL;
11604 flinfo.internal_syms = NULL;
11605 flinfo.indices = NULL;
11606 flinfo.sections = NULL;
11607 flinfo.symshndxbuf = NULL;
11608 flinfo.filesym_count = 0;
11609
11610 /* The object attributes have been merged. Remove the input
11611 sections from the link, and set the contents of the output
11612 secton. */
11613 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11614 for (o = abfd->sections; o != NULL; o = o->next)
11615 {
11616 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11617 || strcmp (o->name, ".gnu.attributes") == 0)
11618 {
11619 for (p = o->map_head.link_order; p != NULL; p = p->next)
11620 {
11621 asection *input_section;
11622
11623 if (p->type != bfd_indirect_link_order)
11624 continue;
11625 input_section = p->u.indirect.section;
11626 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11627 elf_link_input_bfd ignores this section. */
11628 input_section->flags &= ~SEC_HAS_CONTENTS;
11629 }
11630
11631 attr_size = bfd_elf_obj_attr_size (abfd);
11632 if (attr_size)
11633 {
11634 bfd_set_section_size (abfd, o, attr_size);
11635 attr_section = o;
11636 /* Skip this section later on. */
11637 o->map_head.link_order = NULL;
11638 }
11639 else
11640 o->flags |= SEC_EXCLUDE;
11641 }
11642 }
11643
11644 /* Count up the number of relocations we will output for each output
11645 section, so that we know the sizes of the reloc sections. We
11646 also figure out some maximum sizes. */
11647 max_contents_size = 0;
11648 max_external_reloc_size = 0;
11649 max_internal_reloc_count = 0;
11650 max_sym_count = 0;
11651 max_sym_shndx_count = 0;
11652 merged = FALSE;
11653 for (o = abfd->sections; o != NULL; o = o->next)
11654 {
11655 struct bfd_elf_section_data *esdo = elf_section_data (o);
11656 o->reloc_count = 0;
11657
11658 for (p = o->map_head.link_order; p != NULL; p = p->next)
11659 {
11660 unsigned int reloc_count = 0;
11661 unsigned int additional_reloc_count = 0;
11662 struct bfd_elf_section_data *esdi = NULL;
11663
11664 if (p->type == bfd_section_reloc_link_order
11665 || p->type == bfd_symbol_reloc_link_order)
11666 reloc_count = 1;
11667 else if (p->type == bfd_indirect_link_order)
11668 {
11669 asection *sec;
11670
11671 sec = p->u.indirect.section;
11672
11673 /* Mark all sections which are to be included in the
11674 link. This will normally be every section. We need
11675 to do this so that we can identify any sections which
11676 the linker has decided to not include. */
11677 sec->linker_mark = TRUE;
11678
11679 if (sec->flags & SEC_MERGE)
11680 merged = TRUE;
11681
11682 if (sec->rawsize > max_contents_size)
11683 max_contents_size = sec->rawsize;
11684 if (sec->size > max_contents_size)
11685 max_contents_size = sec->size;
11686
11687 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11688 && (sec->owner->flags & DYNAMIC) == 0)
11689 {
11690 size_t sym_count;
11691
11692 /* We are interested in just local symbols, not all
11693 symbols. */
11694 if (elf_bad_symtab (sec->owner))
11695 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11696 / bed->s->sizeof_sym);
11697 else
11698 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11699
11700 if (sym_count > max_sym_count)
11701 max_sym_count = sym_count;
11702
11703 if (sym_count > max_sym_shndx_count
11704 && elf_symtab_shndx_list (sec->owner) != NULL)
11705 max_sym_shndx_count = sym_count;
11706
11707 if (esdo->this_hdr.sh_type == SHT_REL
11708 || esdo->this_hdr.sh_type == SHT_RELA)
11709 /* Some backends use reloc_count in relocation sections
11710 to count particular types of relocs. Of course,
11711 reloc sections themselves can't have relocations. */
11712 ;
11713 else if (emit_relocs)
11714 {
11715 reloc_count = sec->reloc_count;
11716 if (bed->elf_backend_count_additional_relocs)
11717 {
11718 int c;
11719 c = (*bed->elf_backend_count_additional_relocs) (sec);
11720 additional_reloc_count += c;
11721 }
11722 }
11723 else if (bed->elf_backend_count_relocs)
11724 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11725
11726 esdi = elf_section_data (sec);
11727
11728 if ((sec->flags & SEC_RELOC) != 0)
11729 {
11730 size_t ext_size = 0;
11731
11732 if (esdi->rel.hdr != NULL)
11733 ext_size = esdi->rel.hdr->sh_size;
11734 if (esdi->rela.hdr != NULL)
11735 ext_size += esdi->rela.hdr->sh_size;
11736
11737 if (ext_size > max_external_reloc_size)
11738 max_external_reloc_size = ext_size;
11739 if (sec->reloc_count > max_internal_reloc_count)
11740 max_internal_reloc_count = sec->reloc_count;
11741 }
11742 }
11743 }
11744
11745 if (reloc_count == 0)
11746 continue;
11747
11748 reloc_count += additional_reloc_count;
11749 o->reloc_count += reloc_count;
11750
11751 if (p->type == bfd_indirect_link_order && emit_relocs)
11752 {
11753 if (esdi->rel.hdr)
11754 {
11755 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11756 esdo->rel.count += additional_reloc_count;
11757 }
11758 if (esdi->rela.hdr)
11759 {
11760 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11761 esdo->rela.count += additional_reloc_count;
11762 }
11763 }
11764 else
11765 {
11766 if (o->use_rela_p)
11767 esdo->rela.count += reloc_count;
11768 else
11769 esdo->rel.count += reloc_count;
11770 }
11771 }
11772
11773 if (o->reloc_count > 0)
11774 o->flags |= SEC_RELOC;
11775 else
11776 {
11777 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11778 set it (this is probably a bug) and if it is set
11779 assign_section_numbers will create a reloc section. */
11780 o->flags &=~ SEC_RELOC;
11781 }
11782
11783 /* If the SEC_ALLOC flag is not set, force the section VMA to
11784 zero. This is done in elf_fake_sections as well, but forcing
11785 the VMA to 0 here will ensure that relocs against these
11786 sections are handled correctly. */
11787 if ((o->flags & SEC_ALLOC) == 0
11788 && ! o->user_set_vma)
11789 o->vma = 0;
11790 }
11791
11792 if (! bfd_link_relocatable (info) && merged)
11793 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11794
11795 /* Figure out the file positions for everything but the symbol table
11796 and the relocs. We set symcount to force assign_section_numbers
11797 to create a symbol table. */
11798 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11799 BFD_ASSERT (! abfd->output_has_begun);
11800 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11801 goto error_return;
11802
11803 /* Set sizes, and assign file positions for reloc sections. */
11804 for (o = abfd->sections; o != NULL; o = o->next)
11805 {
11806 struct bfd_elf_section_data *esdo = elf_section_data (o);
11807 if ((o->flags & SEC_RELOC) != 0)
11808 {
11809 if (esdo->rel.hdr
11810 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11811 goto error_return;
11812
11813 if (esdo->rela.hdr
11814 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11815 goto error_return;
11816 }
11817
11818 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11819 to count upwards while actually outputting the relocations. */
11820 esdo->rel.count = 0;
11821 esdo->rela.count = 0;
11822
11823 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11824 {
11825 /* Cache the section contents so that they can be compressed
11826 later. Use bfd_malloc since it will be freed by
11827 bfd_compress_section_contents. */
11828 unsigned char *contents = esdo->this_hdr.contents;
11829 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11830 abort ();
11831 contents
11832 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11833 if (contents == NULL)
11834 goto error_return;
11835 esdo->this_hdr.contents = contents;
11836 }
11837 }
11838
11839 /* We have now assigned file positions for all the sections except
11840 .symtab, .strtab, and non-loaded reloc sections. We start the
11841 .symtab section at the current file position, and write directly
11842 to it. We build the .strtab section in memory. */
11843 bfd_get_symcount (abfd) = 0;
11844 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11845 /* sh_name is set in prep_headers. */
11846 symtab_hdr->sh_type = SHT_SYMTAB;
11847 /* sh_flags, sh_addr and sh_size all start off zero. */
11848 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11849 /* sh_link is set in assign_section_numbers. */
11850 /* sh_info is set below. */
11851 /* sh_offset is set just below. */
11852 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11853
11854 if (max_sym_count < 20)
11855 max_sym_count = 20;
11856 htab->strtabsize = max_sym_count;
11857 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11858 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11859 if (htab->strtab == NULL)
11860 goto error_return;
11861 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11862 flinfo.symshndxbuf
11863 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11864 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11865
11866 if (info->strip != strip_all || emit_relocs)
11867 {
11868 file_ptr off = elf_next_file_pos (abfd);
11869
11870 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11871
11872 /* Note that at this point elf_next_file_pos (abfd) is
11873 incorrect. We do not yet know the size of the .symtab section.
11874 We correct next_file_pos below, after we do know the size. */
11875
11876 /* Start writing out the symbol table. The first symbol is always a
11877 dummy symbol. */
11878 elfsym.st_value = 0;
11879 elfsym.st_size = 0;
11880 elfsym.st_info = 0;
11881 elfsym.st_other = 0;
11882 elfsym.st_shndx = SHN_UNDEF;
11883 elfsym.st_target_internal = 0;
11884 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11885 bfd_und_section_ptr, NULL) != 1)
11886 goto error_return;
11887
11888 /* Output a symbol for each section. We output these even if we are
11889 discarding local symbols, since they are used for relocs. These
11890 symbols have no names. We store the index of each one in the
11891 index field of the section, so that we can find it again when
11892 outputting relocs. */
11893
11894 elfsym.st_size = 0;
11895 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11896 elfsym.st_other = 0;
11897 elfsym.st_value = 0;
11898 elfsym.st_target_internal = 0;
11899 for (i = 1; i < elf_numsections (abfd); i++)
11900 {
11901 o = bfd_section_from_elf_index (abfd, i);
11902 if (o != NULL)
11903 {
11904 o->target_index = bfd_get_symcount (abfd);
11905 elfsym.st_shndx = i;
11906 if (!bfd_link_relocatable (info))
11907 elfsym.st_value = o->vma;
11908 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11909 NULL) != 1)
11910 goto error_return;
11911 }
11912 }
11913 }
11914
11915 /* Allocate some memory to hold information read in from the input
11916 files. */
11917 if (max_contents_size != 0)
11918 {
11919 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11920 if (flinfo.contents == NULL)
11921 goto error_return;
11922 }
11923
11924 if (max_external_reloc_size != 0)
11925 {
11926 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11927 if (flinfo.external_relocs == NULL)
11928 goto error_return;
11929 }
11930
11931 if (max_internal_reloc_count != 0)
11932 {
11933 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11934 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11935 if (flinfo.internal_relocs == NULL)
11936 goto error_return;
11937 }
11938
11939 if (max_sym_count != 0)
11940 {
11941 amt = max_sym_count * bed->s->sizeof_sym;
11942 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11943 if (flinfo.external_syms == NULL)
11944 goto error_return;
11945
11946 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11947 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11948 if (flinfo.internal_syms == NULL)
11949 goto error_return;
11950
11951 amt = max_sym_count * sizeof (long);
11952 flinfo.indices = (long int *) bfd_malloc (amt);
11953 if (flinfo.indices == NULL)
11954 goto error_return;
11955
11956 amt = max_sym_count * sizeof (asection *);
11957 flinfo.sections = (asection **) bfd_malloc (amt);
11958 if (flinfo.sections == NULL)
11959 goto error_return;
11960 }
11961
11962 if (max_sym_shndx_count != 0)
11963 {
11964 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11965 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11966 if (flinfo.locsym_shndx == NULL)
11967 goto error_return;
11968 }
11969
11970 if (htab->tls_sec)
11971 {
11972 bfd_vma base, end = 0;
11973 asection *sec;
11974
11975 for (sec = htab->tls_sec;
11976 sec && (sec->flags & SEC_THREAD_LOCAL);
11977 sec = sec->next)
11978 {
11979 bfd_size_type size = sec->size;
11980
11981 if (size == 0
11982 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11983 {
11984 struct bfd_link_order *ord = sec->map_tail.link_order;
11985
11986 if (ord != NULL)
11987 size = ord->offset + ord->size;
11988 }
11989 end = sec->vma + size;
11990 }
11991 base = htab->tls_sec->vma;
11992 /* Only align end of TLS section if static TLS doesn't have special
11993 alignment requirements. */
11994 if (bed->static_tls_alignment == 1)
11995 end = align_power (end, htab->tls_sec->alignment_power);
11996 htab->tls_size = end - base;
11997 }
11998
11999 /* Reorder SHF_LINK_ORDER sections. */
12000 for (o = abfd->sections; o != NULL; o = o->next)
12001 {
12002 if (!elf_fixup_link_order (abfd, o))
12003 return FALSE;
12004 }
12005
12006 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12007 return FALSE;
12008
12009 /* Since ELF permits relocations to be against local symbols, we
12010 must have the local symbols available when we do the relocations.
12011 Since we would rather only read the local symbols once, and we
12012 would rather not keep them in memory, we handle all the
12013 relocations for a single input file at the same time.
12014
12015 Unfortunately, there is no way to know the total number of local
12016 symbols until we have seen all of them, and the local symbol
12017 indices precede the global symbol indices. This means that when
12018 we are generating relocatable output, and we see a reloc against
12019 a global symbol, we can not know the symbol index until we have
12020 finished examining all the local symbols to see which ones we are
12021 going to output. To deal with this, we keep the relocations in
12022 memory, and don't output them until the end of the link. This is
12023 an unfortunate waste of memory, but I don't see a good way around
12024 it. Fortunately, it only happens when performing a relocatable
12025 link, which is not the common case. FIXME: If keep_memory is set
12026 we could write the relocs out and then read them again; I don't
12027 know how bad the memory loss will be. */
12028
12029 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12030 sub->output_has_begun = FALSE;
12031 for (o = abfd->sections; o != NULL; o = o->next)
12032 {
12033 for (p = o->map_head.link_order; p != NULL; p = p->next)
12034 {
12035 if (p->type == bfd_indirect_link_order
12036 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12037 == bfd_target_elf_flavour)
12038 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12039 {
12040 if (! sub->output_has_begun)
12041 {
12042 if (! elf_link_input_bfd (&flinfo, sub))
12043 goto error_return;
12044 sub->output_has_begun = TRUE;
12045 }
12046 }
12047 else if (p->type == bfd_section_reloc_link_order
12048 || p->type == bfd_symbol_reloc_link_order)
12049 {
12050 if (! elf_reloc_link_order (abfd, info, o, p))
12051 goto error_return;
12052 }
12053 else
12054 {
12055 if (! _bfd_default_link_order (abfd, info, o, p))
12056 {
12057 if (p->type == bfd_indirect_link_order
12058 && (bfd_get_flavour (sub)
12059 == bfd_target_elf_flavour)
12060 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12061 != bed->s->elfclass))
12062 {
12063 const char *iclass, *oclass;
12064
12065 switch (bed->s->elfclass)
12066 {
12067 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12068 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12069 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12070 default: abort ();
12071 }
12072
12073 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12074 {
12075 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12076 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12077 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12078 default: abort ();
12079 }
12080
12081 bfd_set_error (bfd_error_wrong_format);
12082 _bfd_error_handler
12083 /* xgettext:c-format */
12084 (_("%B: file class %s incompatible with %s"),
12085 sub, iclass, oclass);
12086 }
12087
12088 goto error_return;
12089 }
12090 }
12091 }
12092 }
12093
12094 /* Free symbol buffer if needed. */
12095 if (!info->reduce_memory_overheads)
12096 {
12097 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12098 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12099 && elf_tdata (sub)->symbuf)
12100 {
12101 free (elf_tdata (sub)->symbuf);
12102 elf_tdata (sub)->symbuf = NULL;
12103 }
12104 }
12105
12106 /* Output any global symbols that got converted to local in a
12107 version script or due to symbol visibility. We do this in a
12108 separate step since ELF requires all local symbols to appear
12109 prior to any global symbols. FIXME: We should only do this if
12110 some global symbols were, in fact, converted to become local.
12111 FIXME: Will this work correctly with the Irix 5 linker? */
12112 eoinfo.failed = FALSE;
12113 eoinfo.flinfo = &flinfo;
12114 eoinfo.localsyms = TRUE;
12115 eoinfo.file_sym_done = FALSE;
12116 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12117 if (eoinfo.failed)
12118 return FALSE;
12119
12120 /* If backend needs to output some local symbols not present in the hash
12121 table, do it now. */
12122 if (bed->elf_backend_output_arch_local_syms
12123 && (info->strip != strip_all || emit_relocs))
12124 {
12125 typedef int (*out_sym_func)
12126 (void *, const char *, Elf_Internal_Sym *, asection *,
12127 struct elf_link_hash_entry *);
12128
12129 if (! ((*bed->elf_backend_output_arch_local_syms)
12130 (abfd, info, &flinfo,
12131 (out_sym_func) elf_link_output_symstrtab)))
12132 return FALSE;
12133 }
12134
12135 /* That wrote out all the local symbols. Finish up the symbol table
12136 with the global symbols. Even if we want to strip everything we
12137 can, we still need to deal with those global symbols that got
12138 converted to local in a version script. */
12139
12140 /* The sh_info field records the index of the first non local symbol. */
12141 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12142
12143 if (dynamic
12144 && htab->dynsym != NULL
12145 && htab->dynsym->output_section != bfd_abs_section_ptr)
12146 {
12147 Elf_Internal_Sym sym;
12148 bfd_byte *dynsym = htab->dynsym->contents;
12149
12150 o = htab->dynsym->output_section;
12151 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12152
12153 /* Write out the section symbols for the output sections. */
12154 if (bfd_link_pic (info)
12155 || htab->is_relocatable_executable)
12156 {
12157 asection *s;
12158
12159 sym.st_size = 0;
12160 sym.st_name = 0;
12161 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12162 sym.st_other = 0;
12163 sym.st_target_internal = 0;
12164
12165 for (s = abfd->sections; s != NULL; s = s->next)
12166 {
12167 int indx;
12168 bfd_byte *dest;
12169 long dynindx;
12170
12171 dynindx = elf_section_data (s)->dynindx;
12172 if (dynindx <= 0)
12173 continue;
12174 indx = elf_section_data (s)->this_idx;
12175 BFD_ASSERT (indx > 0);
12176 sym.st_shndx = indx;
12177 if (! check_dynsym (abfd, &sym))
12178 return FALSE;
12179 sym.st_value = s->vma;
12180 dest = dynsym + dynindx * bed->s->sizeof_sym;
12181 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12182 }
12183 }
12184
12185 /* Write out the local dynsyms. */
12186 if (htab->dynlocal)
12187 {
12188 struct elf_link_local_dynamic_entry *e;
12189 for (e = htab->dynlocal; e ; e = e->next)
12190 {
12191 asection *s;
12192 bfd_byte *dest;
12193
12194 /* Copy the internal symbol and turn off visibility.
12195 Note that we saved a word of storage and overwrote
12196 the original st_name with the dynstr_index. */
12197 sym = e->isym;
12198 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12199
12200 s = bfd_section_from_elf_index (e->input_bfd,
12201 e->isym.st_shndx);
12202 if (s != NULL)
12203 {
12204 sym.st_shndx =
12205 elf_section_data (s->output_section)->this_idx;
12206 if (! check_dynsym (abfd, &sym))
12207 return FALSE;
12208 sym.st_value = (s->output_section->vma
12209 + s->output_offset
12210 + e->isym.st_value);
12211 }
12212
12213 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12214 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12215 }
12216 }
12217 }
12218
12219 /* We get the global symbols from the hash table. */
12220 eoinfo.failed = FALSE;
12221 eoinfo.localsyms = FALSE;
12222 eoinfo.flinfo = &flinfo;
12223 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12224 if (eoinfo.failed)
12225 return FALSE;
12226
12227 /* If backend needs to output some symbols not present in the hash
12228 table, do it now. */
12229 if (bed->elf_backend_output_arch_syms
12230 && (info->strip != strip_all || emit_relocs))
12231 {
12232 typedef int (*out_sym_func)
12233 (void *, const char *, Elf_Internal_Sym *, asection *,
12234 struct elf_link_hash_entry *);
12235
12236 if (! ((*bed->elf_backend_output_arch_syms)
12237 (abfd, info, &flinfo,
12238 (out_sym_func) elf_link_output_symstrtab)))
12239 return FALSE;
12240 }
12241
12242 /* Finalize the .strtab section. */
12243 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12244
12245 /* Swap out the .strtab section. */
12246 if (!elf_link_swap_symbols_out (&flinfo))
12247 return FALSE;
12248
12249 /* Now we know the size of the symtab section. */
12250 if (bfd_get_symcount (abfd) > 0)
12251 {
12252 /* Finish up and write out the symbol string table (.strtab)
12253 section. */
12254 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12255 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12256
12257 if (elf_symtab_shndx_list (abfd))
12258 {
12259 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12260
12261 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12262 {
12263 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12264 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12265 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12266 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12267 symtab_shndx_hdr->sh_size = amt;
12268
12269 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12270 off, TRUE);
12271
12272 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12273 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12274 return FALSE;
12275 }
12276 }
12277
12278 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12279 /* sh_name was set in prep_headers. */
12280 symstrtab_hdr->sh_type = SHT_STRTAB;
12281 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12282 symstrtab_hdr->sh_addr = 0;
12283 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12284 symstrtab_hdr->sh_entsize = 0;
12285 symstrtab_hdr->sh_link = 0;
12286 symstrtab_hdr->sh_info = 0;
12287 /* sh_offset is set just below. */
12288 symstrtab_hdr->sh_addralign = 1;
12289
12290 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12291 off, TRUE);
12292 elf_next_file_pos (abfd) = off;
12293
12294 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12295 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12296 return FALSE;
12297 }
12298
12299 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12300 {
12301 _bfd_error_handler (_("%B: failed to generate import library"),
12302 info->out_implib_bfd);
12303 return FALSE;
12304 }
12305
12306 /* Adjust the relocs to have the correct symbol indices. */
12307 for (o = abfd->sections; o != NULL; o = o->next)
12308 {
12309 struct bfd_elf_section_data *esdo = elf_section_data (o);
12310 bfd_boolean sort;
12311
12312 if ((o->flags & SEC_RELOC) == 0)
12313 continue;
12314
12315 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12316 if (esdo->rel.hdr != NULL
12317 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12318 return FALSE;
12319 if (esdo->rela.hdr != NULL
12320 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12321 return FALSE;
12322
12323 /* Set the reloc_count field to 0 to prevent write_relocs from
12324 trying to swap the relocs out itself. */
12325 o->reloc_count = 0;
12326 }
12327
12328 if (dynamic && info->combreloc && dynobj != NULL)
12329 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12330
12331 /* If we are linking against a dynamic object, or generating a
12332 shared library, finish up the dynamic linking information. */
12333 if (dynamic)
12334 {
12335 bfd_byte *dyncon, *dynconend;
12336
12337 /* Fix up .dynamic entries. */
12338 o = bfd_get_linker_section (dynobj, ".dynamic");
12339 BFD_ASSERT (o != NULL);
12340
12341 dyncon = o->contents;
12342 dynconend = o->contents + o->size;
12343 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12344 {
12345 Elf_Internal_Dyn dyn;
12346 const char *name;
12347 unsigned int type;
12348 bfd_size_type sh_size;
12349 bfd_vma sh_addr;
12350
12351 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12352
12353 switch (dyn.d_tag)
12354 {
12355 default:
12356 continue;
12357 case DT_NULL:
12358 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12359 {
12360 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12361 {
12362 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12363 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12364 default: continue;
12365 }
12366 dyn.d_un.d_val = relativecount;
12367 relativecount = 0;
12368 break;
12369 }
12370 continue;
12371
12372 case DT_INIT:
12373 name = info->init_function;
12374 goto get_sym;
12375 case DT_FINI:
12376 name = info->fini_function;
12377 get_sym:
12378 {
12379 struct elf_link_hash_entry *h;
12380
12381 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12382 if (h != NULL
12383 && (h->root.type == bfd_link_hash_defined
12384 || h->root.type == bfd_link_hash_defweak))
12385 {
12386 dyn.d_un.d_ptr = h->root.u.def.value;
12387 o = h->root.u.def.section;
12388 if (o->output_section != NULL)
12389 dyn.d_un.d_ptr += (o->output_section->vma
12390 + o->output_offset);
12391 else
12392 {
12393 /* The symbol is imported from another shared
12394 library and does not apply to this one. */
12395 dyn.d_un.d_ptr = 0;
12396 }
12397 break;
12398 }
12399 }
12400 continue;
12401
12402 case DT_PREINIT_ARRAYSZ:
12403 name = ".preinit_array";
12404 goto get_out_size;
12405 case DT_INIT_ARRAYSZ:
12406 name = ".init_array";
12407 goto get_out_size;
12408 case DT_FINI_ARRAYSZ:
12409 name = ".fini_array";
12410 get_out_size:
12411 o = bfd_get_section_by_name (abfd, name);
12412 if (o == NULL)
12413 {
12414 _bfd_error_handler
12415 (_("could not find section %s"), name);
12416 goto error_return;
12417 }
12418 if (o->size == 0)
12419 _bfd_error_handler
12420 (_("warning: %s section has zero size"), name);
12421 dyn.d_un.d_val = o->size;
12422 break;
12423
12424 case DT_PREINIT_ARRAY:
12425 name = ".preinit_array";
12426 goto get_out_vma;
12427 case DT_INIT_ARRAY:
12428 name = ".init_array";
12429 goto get_out_vma;
12430 case DT_FINI_ARRAY:
12431 name = ".fini_array";
12432 get_out_vma:
12433 o = bfd_get_section_by_name (abfd, name);
12434 goto do_vma;
12435
12436 case DT_HASH:
12437 name = ".hash";
12438 goto get_vma;
12439 case DT_GNU_HASH:
12440 name = ".gnu.hash";
12441 goto get_vma;
12442 case DT_STRTAB:
12443 name = ".dynstr";
12444 goto get_vma;
12445 case DT_SYMTAB:
12446 name = ".dynsym";
12447 goto get_vma;
12448 case DT_VERDEF:
12449 name = ".gnu.version_d";
12450 goto get_vma;
12451 case DT_VERNEED:
12452 name = ".gnu.version_r";
12453 goto get_vma;
12454 case DT_VERSYM:
12455 name = ".gnu.version";
12456 get_vma:
12457 o = bfd_get_linker_section (dynobj, name);
12458 do_vma:
12459 if (o == NULL || bfd_is_abs_section (o->output_section))
12460 {
12461 _bfd_error_handler
12462 (_("could not find section %s"), name);
12463 goto error_return;
12464 }
12465 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12466 {
12467 _bfd_error_handler
12468 (_("warning: section '%s' is being made into a note"), name);
12469 bfd_set_error (bfd_error_nonrepresentable_section);
12470 goto error_return;
12471 }
12472 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12473 break;
12474
12475 case DT_REL:
12476 case DT_RELA:
12477 case DT_RELSZ:
12478 case DT_RELASZ:
12479 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12480 type = SHT_REL;
12481 else
12482 type = SHT_RELA;
12483 sh_size = 0;
12484 sh_addr = 0;
12485 for (i = 1; i < elf_numsections (abfd); i++)
12486 {
12487 Elf_Internal_Shdr *hdr;
12488
12489 hdr = elf_elfsections (abfd)[i];
12490 if (hdr->sh_type == type
12491 && (hdr->sh_flags & SHF_ALLOC) != 0)
12492 {
12493 sh_size += hdr->sh_size;
12494 if (sh_addr == 0
12495 || sh_addr > hdr->sh_addr)
12496 sh_addr = hdr->sh_addr;
12497 }
12498 }
12499
12500 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12501 {
12502 /* Don't count procedure linkage table relocs in the
12503 overall reloc count. */
12504 sh_size -= htab->srelplt->size;
12505 if (sh_size == 0)
12506 /* If the size is zero, make the address zero too.
12507 This is to avoid a glibc bug. If the backend
12508 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12509 zero, then we'll put DT_RELA at the end of
12510 DT_JMPREL. glibc will interpret the end of
12511 DT_RELA matching the end of DT_JMPREL as the
12512 case where DT_RELA includes DT_JMPREL, and for
12513 LD_BIND_NOW will decide that processing DT_RELA
12514 will process the PLT relocs too. Net result:
12515 No PLT relocs applied. */
12516 sh_addr = 0;
12517
12518 /* If .rela.plt is the first .rela section, exclude
12519 it from DT_RELA. */
12520 else if (sh_addr == (htab->srelplt->output_section->vma
12521 + htab->srelplt->output_offset))
12522 sh_addr += htab->srelplt->size;
12523 }
12524
12525 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12526 dyn.d_un.d_val = sh_size;
12527 else
12528 dyn.d_un.d_ptr = sh_addr;
12529 break;
12530 }
12531 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12532 }
12533 }
12534
12535 /* If we have created any dynamic sections, then output them. */
12536 if (dynobj != NULL)
12537 {
12538 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12539 goto error_return;
12540
12541 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12542 if (((info->warn_shared_textrel && bfd_link_pic (info))
12543 || info->error_textrel)
12544 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12545 {
12546 bfd_byte *dyncon, *dynconend;
12547
12548 dyncon = o->contents;
12549 dynconend = o->contents + o->size;
12550 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12551 {
12552 Elf_Internal_Dyn dyn;
12553
12554 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12555
12556 if (dyn.d_tag == DT_TEXTREL)
12557 {
12558 if (info->error_textrel)
12559 info->callbacks->einfo
12560 (_("%P%X: read-only segment has dynamic relocations.\n"));
12561 else
12562 info->callbacks->einfo
12563 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12564 break;
12565 }
12566 }
12567 }
12568
12569 for (o = dynobj->sections; o != NULL; o = o->next)
12570 {
12571 if ((o->flags & SEC_HAS_CONTENTS) == 0
12572 || o->size == 0
12573 || o->output_section == bfd_abs_section_ptr)
12574 continue;
12575 if ((o->flags & SEC_LINKER_CREATED) == 0)
12576 {
12577 /* At this point, we are only interested in sections
12578 created by _bfd_elf_link_create_dynamic_sections. */
12579 continue;
12580 }
12581 if (htab->stab_info.stabstr == o)
12582 continue;
12583 if (htab->eh_info.hdr_sec == o)
12584 continue;
12585 if (strcmp (o->name, ".dynstr") != 0)
12586 {
12587 if (! bfd_set_section_contents (abfd, o->output_section,
12588 o->contents,
12589 (file_ptr) o->output_offset
12590 * bfd_octets_per_byte (abfd),
12591 o->size))
12592 goto error_return;
12593 }
12594 else
12595 {
12596 /* The contents of the .dynstr section are actually in a
12597 stringtab. */
12598 file_ptr off;
12599
12600 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12601 if (bfd_seek (abfd, off, SEEK_SET) != 0
12602 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12603 goto error_return;
12604 }
12605 }
12606 }
12607
12608 if (!info->resolve_section_groups)
12609 {
12610 bfd_boolean failed = FALSE;
12611
12612 BFD_ASSERT (bfd_link_relocatable (info));
12613 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12614 if (failed)
12615 goto error_return;
12616 }
12617
12618 /* If we have optimized stabs strings, output them. */
12619 if (htab->stab_info.stabstr != NULL)
12620 {
12621 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12622 goto error_return;
12623 }
12624
12625 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12626 goto error_return;
12627
12628 elf_final_link_free (abfd, &flinfo);
12629
12630 elf_linker (abfd) = TRUE;
12631
12632 if (attr_section)
12633 {
12634 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12635 if (contents == NULL)
12636 return FALSE; /* Bail out and fail. */
12637 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12638 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12639 free (contents);
12640 }
12641
12642 return TRUE;
12643
12644 error_return:
12645 elf_final_link_free (abfd, &flinfo);
12646 return FALSE;
12647 }
12648
12649 /* Initialize COOKIE for input bfd ABFD. */
12651
12652 static bfd_boolean
12653 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12654 struct bfd_link_info *info, bfd *abfd)
12655 {
12656 Elf_Internal_Shdr *symtab_hdr;
12657 const struct elf_backend_data *bed;
12658
12659 bed = get_elf_backend_data (abfd);
12660 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12661
12662 cookie->abfd = abfd;
12663 cookie->sym_hashes = elf_sym_hashes (abfd);
12664 cookie->bad_symtab = elf_bad_symtab (abfd);
12665 if (cookie->bad_symtab)
12666 {
12667 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12668 cookie->extsymoff = 0;
12669 }
12670 else
12671 {
12672 cookie->locsymcount = symtab_hdr->sh_info;
12673 cookie->extsymoff = symtab_hdr->sh_info;
12674 }
12675
12676 if (bed->s->arch_size == 32)
12677 cookie->r_sym_shift = 8;
12678 else
12679 cookie->r_sym_shift = 32;
12680
12681 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12682 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12683 {
12684 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12685 cookie->locsymcount, 0,
12686 NULL, NULL, NULL);
12687 if (cookie->locsyms == NULL)
12688 {
12689 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12690 return FALSE;
12691 }
12692 if (info->keep_memory)
12693 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12694 }
12695 return TRUE;
12696 }
12697
12698 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12699
12700 static void
12701 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12702 {
12703 Elf_Internal_Shdr *symtab_hdr;
12704
12705 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12706 if (cookie->locsyms != NULL
12707 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12708 free (cookie->locsyms);
12709 }
12710
12711 /* Initialize the relocation information in COOKIE for input section SEC
12712 of input bfd ABFD. */
12713
12714 static bfd_boolean
12715 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12716 struct bfd_link_info *info, bfd *abfd,
12717 asection *sec)
12718 {
12719 if (sec->reloc_count == 0)
12720 {
12721 cookie->rels = NULL;
12722 cookie->relend = NULL;
12723 }
12724 else
12725 {
12726 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12727 info->keep_memory);
12728 if (cookie->rels == NULL)
12729 return FALSE;
12730 cookie->rel = cookie->rels;
12731 cookie->relend = cookie->rels + sec->reloc_count;
12732 }
12733 cookie->rel = cookie->rels;
12734 return TRUE;
12735 }
12736
12737 /* Free the memory allocated by init_reloc_cookie_rels,
12738 if appropriate. */
12739
12740 static void
12741 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12742 asection *sec)
12743 {
12744 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12745 free (cookie->rels);
12746 }
12747
12748 /* Initialize the whole of COOKIE for input section SEC. */
12749
12750 static bfd_boolean
12751 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12752 struct bfd_link_info *info,
12753 asection *sec)
12754 {
12755 if (!init_reloc_cookie (cookie, info, sec->owner))
12756 goto error1;
12757 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12758 goto error2;
12759 return TRUE;
12760
12761 error2:
12762 fini_reloc_cookie (cookie, sec->owner);
12763 error1:
12764 return FALSE;
12765 }
12766
12767 /* Free the memory allocated by init_reloc_cookie_for_section,
12768 if appropriate. */
12769
12770 static void
12771 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12772 asection *sec)
12773 {
12774 fini_reloc_cookie_rels (cookie, sec);
12775 fini_reloc_cookie (cookie, sec->owner);
12776 }
12777
12778 /* Garbage collect unused sections. */
12780
12781 /* Default gc_mark_hook. */
12782
12783 asection *
12784 _bfd_elf_gc_mark_hook (asection *sec,
12785 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12786 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12787 struct elf_link_hash_entry *h,
12788 Elf_Internal_Sym *sym)
12789 {
12790 if (h != NULL)
12791 {
12792 switch (h->root.type)
12793 {
12794 case bfd_link_hash_defined:
12795 case bfd_link_hash_defweak:
12796 return h->root.u.def.section;
12797
12798 case bfd_link_hash_common:
12799 return h->root.u.c.p->section;
12800
12801 default:
12802 break;
12803 }
12804 }
12805 else
12806 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12807
12808 return NULL;
12809 }
12810
12811 /* Return the global debug definition section. */
12812
12813 static asection *
12814 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12815 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12816 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12817 struct elf_link_hash_entry *h,
12818 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12819 {
12820 if (h != NULL
12821 && (h->root.type == bfd_link_hash_defined
12822 || h->root.type == bfd_link_hash_defweak)
12823 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12824 return h->root.u.def.section;
12825
12826 return NULL;
12827 }
12828
12829 /* COOKIE->rel describes a relocation against section SEC, which is
12830 a section we've decided to keep. Return the section that contains
12831 the relocation symbol, or NULL if no section contains it. */
12832
12833 asection *
12834 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12835 elf_gc_mark_hook_fn gc_mark_hook,
12836 struct elf_reloc_cookie *cookie,
12837 bfd_boolean *start_stop)
12838 {
12839 unsigned long r_symndx;
12840 struct elf_link_hash_entry *h;
12841
12842 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12843 if (r_symndx == STN_UNDEF)
12844 return NULL;
12845
12846 if (r_symndx >= cookie->locsymcount
12847 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12848 {
12849 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12850 if (h == NULL)
12851 {
12852 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12853 sec->owner);
12854 return NULL;
12855 }
12856 while (h->root.type == bfd_link_hash_indirect
12857 || h->root.type == bfd_link_hash_warning)
12858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12859 h->mark = 1;
12860 /* If this symbol is weak and there is a non-weak definition, we
12861 keep the non-weak definition because many backends put
12862 dynamic reloc info on the non-weak definition for code
12863 handling copy relocs. */
12864 if (h->is_weakalias)
12865 weakdef (h)->mark = 1;
12866
12867 if (start_stop != NULL)
12868 {
12869 /* To work around a glibc bug, mark XXX input sections
12870 when there is a reference to __start_XXX or __stop_XXX
12871 symbols. */
12872 if (h->start_stop)
12873 {
12874 asection *s = h->u2.start_stop_section;
12875 *start_stop = !s->gc_mark;
12876 return s;
12877 }
12878 }
12879
12880 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12881 }
12882
12883 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12884 &cookie->locsyms[r_symndx]);
12885 }
12886
12887 /* COOKIE->rel describes a relocation against section SEC, which is
12888 a section we've decided to keep. Mark the section that contains
12889 the relocation symbol. */
12890
12891 bfd_boolean
12892 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12893 asection *sec,
12894 elf_gc_mark_hook_fn gc_mark_hook,
12895 struct elf_reloc_cookie *cookie)
12896 {
12897 asection *rsec;
12898 bfd_boolean start_stop = FALSE;
12899
12900 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12901 while (rsec != NULL)
12902 {
12903 if (!rsec->gc_mark)
12904 {
12905 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12906 || (rsec->owner->flags & DYNAMIC) != 0)
12907 rsec->gc_mark = 1;
12908 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12909 return FALSE;
12910 }
12911 if (!start_stop)
12912 break;
12913 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12914 }
12915 return TRUE;
12916 }
12917
12918 /* The mark phase of garbage collection. For a given section, mark
12919 it and any sections in this section's group, and all the sections
12920 which define symbols to which it refers. */
12921
12922 bfd_boolean
12923 _bfd_elf_gc_mark (struct bfd_link_info *info,
12924 asection *sec,
12925 elf_gc_mark_hook_fn gc_mark_hook)
12926 {
12927 bfd_boolean ret;
12928 asection *group_sec, *eh_frame;
12929
12930 sec->gc_mark = 1;
12931
12932 /* Mark all the sections in the group. */
12933 group_sec = elf_section_data (sec)->next_in_group;
12934 if (group_sec && !group_sec->gc_mark)
12935 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12936 return FALSE;
12937
12938 /* Look through the section relocs. */
12939 ret = TRUE;
12940 eh_frame = elf_eh_frame_section (sec->owner);
12941 if ((sec->flags & SEC_RELOC) != 0
12942 && sec->reloc_count > 0
12943 && sec != eh_frame)
12944 {
12945 struct elf_reloc_cookie cookie;
12946
12947 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12948 ret = FALSE;
12949 else
12950 {
12951 for (; cookie.rel < cookie.relend; cookie.rel++)
12952 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12953 {
12954 ret = FALSE;
12955 break;
12956 }
12957 fini_reloc_cookie_for_section (&cookie, sec);
12958 }
12959 }
12960
12961 if (ret && eh_frame && elf_fde_list (sec))
12962 {
12963 struct elf_reloc_cookie cookie;
12964
12965 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12966 ret = FALSE;
12967 else
12968 {
12969 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12970 gc_mark_hook, &cookie))
12971 ret = FALSE;
12972 fini_reloc_cookie_for_section (&cookie, eh_frame);
12973 }
12974 }
12975
12976 eh_frame = elf_section_eh_frame_entry (sec);
12977 if (ret && eh_frame && !eh_frame->gc_mark)
12978 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12979 ret = FALSE;
12980
12981 return ret;
12982 }
12983
12984 /* Scan and mark sections in a special or debug section group. */
12985
12986 static void
12987 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12988 {
12989 /* Point to first section of section group. */
12990 asection *ssec;
12991 /* Used to iterate the section group. */
12992 asection *msec;
12993
12994 bfd_boolean is_special_grp = TRUE;
12995 bfd_boolean is_debug_grp = TRUE;
12996
12997 /* First scan to see if group contains any section other than debug
12998 and special section. */
12999 ssec = msec = elf_next_in_group (grp);
13000 do
13001 {
13002 if ((msec->flags & SEC_DEBUGGING) == 0)
13003 is_debug_grp = FALSE;
13004
13005 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13006 is_special_grp = FALSE;
13007
13008 msec = elf_next_in_group (msec);
13009 }
13010 while (msec != ssec);
13011
13012 /* If this is a pure debug section group or pure special section group,
13013 keep all sections in this group. */
13014 if (is_debug_grp || is_special_grp)
13015 {
13016 do
13017 {
13018 msec->gc_mark = 1;
13019 msec = elf_next_in_group (msec);
13020 }
13021 while (msec != ssec);
13022 }
13023 }
13024
13025 /* Keep debug and special sections. */
13026
13027 bfd_boolean
13028 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13029 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13030 {
13031 bfd *ibfd;
13032
13033 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13034 {
13035 asection *isec;
13036 bfd_boolean some_kept;
13037 bfd_boolean debug_frag_seen;
13038 bfd_boolean has_kept_debug_info;
13039
13040 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13041 continue;
13042 isec = ibfd->sections;
13043 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13044 continue;
13045
13046 /* Ensure all linker created sections are kept,
13047 see if any other section is already marked,
13048 and note if we have any fragmented debug sections. */
13049 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13050 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13051 {
13052 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13053 isec->gc_mark = 1;
13054 else if (isec->gc_mark
13055 && (isec->flags & SEC_ALLOC) != 0
13056 && elf_section_type (isec) != SHT_NOTE)
13057 some_kept = TRUE;
13058
13059 if (!debug_frag_seen
13060 && (isec->flags & SEC_DEBUGGING)
13061 && CONST_STRNEQ (isec->name, ".debug_line."))
13062 debug_frag_seen = TRUE;
13063 }
13064
13065 /* If no non-note alloc section in this file will be kept, then
13066 we can toss out the debug and special sections. */
13067 if (!some_kept)
13068 continue;
13069
13070 /* Keep debug and special sections like .comment when they are
13071 not part of a group. Also keep section groups that contain
13072 just debug sections or special sections. */
13073 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13074 {
13075 if ((isec->flags & SEC_GROUP) != 0)
13076 _bfd_elf_gc_mark_debug_special_section_group (isec);
13077 else if (((isec->flags & SEC_DEBUGGING) != 0
13078 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13079 && elf_next_in_group (isec) == NULL)
13080 isec->gc_mark = 1;
13081 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13082 has_kept_debug_info = TRUE;
13083 }
13084
13085 /* Look for CODE sections which are going to be discarded,
13086 and find and discard any fragmented debug sections which
13087 are associated with that code section. */
13088 if (debug_frag_seen)
13089 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13090 if ((isec->flags & SEC_CODE) != 0
13091 && isec->gc_mark == 0)
13092 {
13093 unsigned int ilen;
13094 asection *dsec;
13095
13096 ilen = strlen (isec->name);
13097
13098 /* Association is determined by the name of the debug
13099 section containing the name of the code section as
13100 a suffix. For example .debug_line.text.foo is a
13101 debug section associated with .text.foo. */
13102 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13103 {
13104 unsigned int dlen;
13105
13106 if (dsec->gc_mark == 0
13107 || (dsec->flags & SEC_DEBUGGING) == 0)
13108 continue;
13109
13110 dlen = strlen (dsec->name);
13111
13112 if (dlen > ilen
13113 && strncmp (dsec->name + (dlen - ilen),
13114 isec->name, ilen) == 0)
13115 dsec->gc_mark = 0;
13116 }
13117 }
13118
13119 /* Mark debug sections referenced by kept debug sections. */
13120 if (has_kept_debug_info)
13121 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13122 if (isec->gc_mark
13123 && (isec->flags & SEC_DEBUGGING) != 0)
13124 if (!_bfd_elf_gc_mark (info, isec,
13125 elf_gc_mark_debug_section))
13126 return FALSE;
13127 }
13128 return TRUE;
13129 }
13130
13131 static bfd_boolean
13132 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13133 {
13134 bfd *sub;
13135 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13136
13137 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13138 {
13139 asection *o;
13140
13141 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13142 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13143 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13144 continue;
13145 o = sub->sections;
13146 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13147 continue;
13148
13149 for (o = sub->sections; o != NULL; o = o->next)
13150 {
13151 /* When any section in a section group is kept, we keep all
13152 sections in the section group. If the first member of
13153 the section group is excluded, we will also exclude the
13154 group section. */
13155 if (o->flags & SEC_GROUP)
13156 {
13157 asection *first = elf_next_in_group (o);
13158 o->gc_mark = first->gc_mark;
13159 }
13160
13161 if (o->gc_mark)
13162 continue;
13163
13164 /* Skip sweeping sections already excluded. */
13165 if (o->flags & SEC_EXCLUDE)
13166 continue;
13167
13168 /* Since this is early in the link process, it is simple
13169 to remove a section from the output. */
13170 o->flags |= SEC_EXCLUDE;
13171
13172 if (info->print_gc_sections && o->size != 0)
13173 /* xgettext:c-format */
13174 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13175 o, sub);
13176 }
13177 }
13178
13179 return TRUE;
13180 }
13181
13182 /* Propagate collected vtable information. This is called through
13183 elf_link_hash_traverse. */
13184
13185 static bfd_boolean
13186 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13187 {
13188 /* Those that are not vtables. */
13189 if (h->start_stop
13190 || h->u2.vtable == NULL
13191 || h->u2.vtable->parent == NULL)
13192 return TRUE;
13193
13194 /* Those vtables that do not have parents, we cannot merge. */
13195 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13196 return TRUE;
13197
13198 /* If we've already been done, exit. */
13199 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13200 return TRUE;
13201
13202 /* Make sure the parent's table is up to date. */
13203 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13204
13205 if (h->u2.vtable->used == NULL)
13206 {
13207 /* None of this table's entries were referenced. Re-use the
13208 parent's table. */
13209 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13210 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13211 }
13212 else
13213 {
13214 size_t n;
13215 bfd_boolean *cu, *pu;
13216
13217 /* Or the parent's entries into ours. */
13218 cu = h->u2.vtable->used;
13219 cu[-1] = TRUE;
13220 pu = h->u2.vtable->parent->u2.vtable->used;
13221 if (pu != NULL)
13222 {
13223 const struct elf_backend_data *bed;
13224 unsigned int log_file_align;
13225
13226 bed = get_elf_backend_data (h->root.u.def.section->owner);
13227 log_file_align = bed->s->log_file_align;
13228 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13229 while (n--)
13230 {
13231 if (*pu)
13232 *cu = TRUE;
13233 pu++;
13234 cu++;
13235 }
13236 }
13237 }
13238
13239 return TRUE;
13240 }
13241
13242 static bfd_boolean
13243 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13244 {
13245 asection *sec;
13246 bfd_vma hstart, hend;
13247 Elf_Internal_Rela *relstart, *relend, *rel;
13248 const struct elf_backend_data *bed;
13249 unsigned int log_file_align;
13250
13251 /* Take care of both those symbols that do not describe vtables as
13252 well as those that are not loaded. */
13253 if (h->start_stop
13254 || h->u2.vtable == NULL
13255 || h->u2.vtable->parent == NULL)
13256 return TRUE;
13257
13258 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13259 || h->root.type == bfd_link_hash_defweak);
13260
13261 sec = h->root.u.def.section;
13262 hstart = h->root.u.def.value;
13263 hend = hstart + h->size;
13264
13265 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13266 if (!relstart)
13267 return *(bfd_boolean *) okp = FALSE;
13268 bed = get_elf_backend_data (sec->owner);
13269 log_file_align = bed->s->log_file_align;
13270
13271 relend = relstart + sec->reloc_count;
13272
13273 for (rel = relstart; rel < relend; ++rel)
13274 if (rel->r_offset >= hstart && rel->r_offset < hend)
13275 {
13276 /* If the entry is in use, do nothing. */
13277 if (h->u2.vtable->used
13278 && (rel->r_offset - hstart) < h->u2.vtable->size)
13279 {
13280 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13281 if (h->u2.vtable->used[entry])
13282 continue;
13283 }
13284 /* Otherwise, kill it. */
13285 rel->r_offset = rel->r_info = rel->r_addend = 0;
13286 }
13287
13288 return TRUE;
13289 }
13290
13291 /* Mark sections containing dynamically referenced symbols. When
13292 building shared libraries, we must assume that any visible symbol is
13293 referenced. */
13294
13295 bfd_boolean
13296 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13297 {
13298 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13299 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13300
13301 if ((h->root.type == bfd_link_hash_defined
13302 || h->root.type == bfd_link_hash_defweak)
13303 && ((h->ref_dynamic && !h->forced_local)
13304 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13305 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13306 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13307 && (!bfd_link_executable (info)
13308 || info->gc_keep_exported
13309 || info->export_dynamic
13310 || (h->dynamic
13311 && d != NULL
13312 && (*d->match) (&d->head, NULL, h->root.root.string)))
13313 && (h->versioned >= versioned
13314 || !bfd_hide_sym_by_version (info->version_info,
13315 h->root.root.string)))))
13316 h->root.u.def.section->flags |= SEC_KEEP;
13317
13318 return TRUE;
13319 }
13320
13321 /* Keep all sections containing symbols undefined on the command-line,
13322 and the section containing the entry symbol. */
13323
13324 void
13325 _bfd_elf_gc_keep (struct bfd_link_info *info)
13326 {
13327 struct bfd_sym_chain *sym;
13328
13329 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13330 {
13331 struct elf_link_hash_entry *h;
13332
13333 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13334 FALSE, FALSE, FALSE);
13335
13336 if (h != NULL
13337 && (h->root.type == bfd_link_hash_defined
13338 || h->root.type == bfd_link_hash_defweak)
13339 && !bfd_is_abs_section (h->root.u.def.section)
13340 && !bfd_is_und_section (h->root.u.def.section))
13341 h->root.u.def.section->flags |= SEC_KEEP;
13342 }
13343 }
13344
13345 bfd_boolean
13346 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13347 struct bfd_link_info *info)
13348 {
13349 bfd *ibfd = info->input_bfds;
13350
13351 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13352 {
13353 asection *sec;
13354 struct elf_reloc_cookie cookie;
13355
13356 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13357 continue;
13358 sec = ibfd->sections;
13359 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13360 continue;
13361
13362 if (!init_reloc_cookie (&cookie, info, ibfd))
13363 return FALSE;
13364
13365 for (sec = ibfd->sections; sec; sec = sec->next)
13366 {
13367 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13368 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13369 {
13370 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13371 fini_reloc_cookie_rels (&cookie, sec);
13372 }
13373 }
13374 }
13375 return TRUE;
13376 }
13377
13378 /* Do mark and sweep of unused sections. */
13379
13380 bfd_boolean
13381 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13382 {
13383 bfd_boolean ok = TRUE;
13384 bfd *sub;
13385 elf_gc_mark_hook_fn gc_mark_hook;
13386 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13387 struct elf_link_hash_table *htab;
13388
13389 if (!bed->can_gc_sections
13390 || !is_elf_hash_table (info->hash))
13391 {
13392 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13393 return TRUE;
13394 }
13395
13396 bed->gc_keep (info);
13397 htab = elf_hash_table (info);
13398
13399 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13400 at the .eh_frame section if we can mark the FDEs individually. */
13401 for (sub = info->input_bfds;
13402 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13403 sub = sub->link.next)
13404 {
13405 asection *sec;
13406 struct elf_reloc_cookie cookie;
13407
13408 sec = sub->sections;
13409 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13410 continue;
13411 sec = bfd_get_section_by_name (sub, ".eh_frame");
13412 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13413 {
13414 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13415 if (elf_section_data (sec)->sec_info
13416 && (sec->flags & SEC_LINKER_CREATED) == 0)
13417 elf_eh_frame_section (sub) = sec;
13418 fini_reloc_cookie_for_section (&cookie, sec);
13419 sec = bfd_get_next_section_by_name (NULL, sec);
13420 }
13421 }
13422
13423 /* Apply transitive closure to the vtable entry usage info. */
13424 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13425 if (!ok)
13426 return FALSE;
13427
13428 /* Kill the vtable relocations that were not used. */
13429 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13430 if (!ok)
13431 return FALSE;
13432
13433 /* Mark dynamically referenced symbols. */
13434 if (htab->dynamic_sections_created || info->gc_keep_exported)
13435 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13436
13437 /* Grovel through relocs to find out who stays ... */
13438 gc_mark_hook = bed->gc_mark_hook;
13439 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13440 {
13441 asection *o;
13442
13443 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13444 || elf_object_id (sub) != elf_hash_table_id (htab)
13445 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13446 continue;
13447
13448 o = sub->sections;
13449 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13450 continue;
13451
13452 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13453 Also treat note sections as a root, if the section is not part
13454 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13455 well as FINI_ARRAY sections for ld -r. */
13456 for (o = sub->sections; o != NULL; o = o->next)
13457 if (!o->gc_mark
13458 && (o->flags & SEC_EXCLUDE) == 0
13459 && ((o->flags & SEC_KEEP) != 0
13460 || (bfd_link_relocatable (info)
13461 && ((elf_section_data (o)->this_hdr.sh_type
13462 == SHT_PREINIT_ARRAY)
13463 || (elf_section_data (o)->this_hdr.sh_type
13464 == SHT_INIT_ARRAY)
13465 || (elf_section_data (o)->this_hdr.sh_type
13466 == SHT_FINI_ARRAY)))
13467 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13468 && elf_next_in_group (o) == NULL )))
13469 {
13470 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13471 return FALSE;
13472 }
13473 }
13474
13475 /* Allow the backend to mark additional target specific sections. */
13476 bed->gc_mark_extra_sections (info, gc_mark_hook);
13477
13478 /* ... and mark SEC_EXCLUDE for those that go. */
13479 return elf_gc_sweep (abfd, info);
13480 }
13481
13482 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13484
13485 bfd_boolean
13486 bfd_elf_gc_record_vtinherit (bfd *abfd,
13487 asection *sec,
13488 struct elf_link_hash_entry *h,
13489 bfd_vma offset)
13490 {
13491 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13492 struct elf_link_hash_entry **search, *child;
13493 size_t extsymcount;
13494 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13495
13496 /* The sh_info field of the symtab header tells us where the
13497 external symbols start. We don't care about the local symbols at
13498 this point. */
13499 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13500 if (!elf_bad_symtab (abfd))
13501 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13502
13503 sym_hashes = elf_sym_hashes (abfd);
13504 sym_hashes_end = sym_hashes + extsymcount;
13505
13506 /* Hunt down the child symbol, which is in this section at the same
13507 offset as the relocation. */
13508 for (search = sym_hashes; search != sym_hashes_end; ++search)
13509 {
13510 if ((child = *search) != NULL
13511 && (child->root.type == bfd_link_hash_defined
13512 || child->root.type == bfd_link_hash_defweak)
13513 && child->root.u.def.section == sec
13514 && child->root.u.def.value == offset)
13515 goto win;
13516 }
13517
13518 /* xgettext:c-format */
13519 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13520 abfd, sec, offset);
13521 bfd_set_error (bfd_error_invalid_operation);
13522 return FALSE;
13523
13524 win:
13525 if (!child->u2.vtable)
13526 {
13527 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13528 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13529 if (!child->u2.vtable)
13530 return FALSE;
13531 }
13532 if (!h)
13533 {
13534 /* This *should* only be the absolute section. It could potentially
13535 be that someone has defined a non-global vtable though, which
13536 would be bad. It isn't worth paging in the local symbols to be
13537 sure though; that case should simply be handled by the assembler. */
13538
13539 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13540 }
13541 else
13542 child->u2.vtable->parent = h;
13543
13544 return TRUE;
13545 }
13546
13547 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13548
13549 bfd_boolean
13550 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13551 asection *sec ATTRIBUTE_UNUSED,
13552 struct elf_link_hash_entry *h,
13553 bfd_vma addend)
13554 {
13555 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13556 unsigned int log_file_align = bed->s->log_file_align;
13557
13558 if (!h->u2.vtable)
13559 {
13560 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13561 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13562 if (!h->u2.vtable)
13563 return FALSE;
13564 }
13565
13566 if (addend >= h->u2.vtable->size)
13567 {
13568 size_t size, bytes, file_align;
13569 bfd_boolean *ptr = h->u2.vtable->used;
13570
13571 /* While the symbol is undefined, we have to be prepared to handle
13572 a zero size. */
13573 file_align = 1 << log_file_align;
13574 if (h->root.type == bfd_link_hash_undefined)
13575 size = addend + file_align;
13576 else
13577 {
13578 size = h->size;
13579 if (addend >= size)
13580 {
13581 /* Oops! We've got a reference past the defined end of
13582 the table. This is probably a bug -- shall we warn? */
13583 size = addend + file_align;
13584 }
13585 }
13586 size = (size + file_align - 1) & -file_align;
13587
13588 /* Allocate one extra entry for use as a "done" flag for the
13589 consolidation pass. */
13590 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13591
13592 if (ptr)
13593 {
13594 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13595
13596 if (ptr != NULL)
13597 {
13598 size_t oldbytes;
13599
13600 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13601 * sizeof (bfd_boolean));
13602 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13603 }
13604 }
13605 else
13606 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13607
13608 if (ptr == NULL)
13609 return FALSE;
13610
13611 /* And arrange for that done flag to be at index -1. */
13612 h->u2.vtable->used = ptr + 1;
13613 h->u2.vtable->size = size;
13614 }
13615
13616 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13617
13618 return TRUE;
13619 }
13620
13621 /* Map an ELF section header flag to its corresponding string. */
13622 typedef struct
13623 {
13624 char *flag_name;
13625 flagword flag_value;
13626 } elf_flags_to_name_table;
13627
13628 static elf_flags_to_name_table elf_flags_to_names [] =
13629 {
13630 { "SHF_WRITE", SHF_WRITE },
13631 { "SHF_ALLOC", SHF_ALLOC },
13632 { "SHF_EXECINSTR", SHF_EXECINSTR },
13633 { "SHF_MERGE", SHF_MERGE },
13634 { "SHF_STRINGS", SHF_STRINGS },
13635 { "SHF_INFO_LINK", SHF_INFO_LINK},
13636 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13637 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13638 { "SHF_GROUP", SHF_GROUP },
13639 { "SHF_TLS", SHF_TLS },
13640 { "SHF_MASKOS", SHF_MASKOS },
13641 { "SHF_EXCLUDE", SHF_EXCLUDE },
13642 };
13643
13644 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13645 bfd_boolean
13646 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13647 struct flag_info *flaginfo,
13648 asection *section)
13649 {
13650 const bfd_vma sh_flags = elf_section_flags (section);
13651
13652 if (!flaginfo->flags_initialized)
13653 {
13654 bfd *obfd = info->output_bfd;
13655 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13656 struct flag_info_list *tf = flaginfo->flag_list;
13657 int with_hex = 0;
13658 int without_hex = 0;
13659
13660 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13661 {
13662 unsigned i;
13663 flagword (*lookup) (char *);
13664
13665 lookup = bed->elf_backend_lookup_section_flags_hook;
13666 if (lookup != NULL)
13667 {
13668 flagword hexval = (*lookup) ((char *) tf->name);
13669
13670 if (hexval != 0)
13671 {
13672 if (tf->with == with_flags)
13673 with_hex |= hexval;
13674 else if (tf->with == without_flags)
13675 without_hex |= hexval;
13676 tf->valid = TRUE;
13677 continue;
13678 }
13679 }
13680 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13681 {
13682 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13683 {
13684 if (tf->with == with_flags)
13685 with_hex |= elf_flags_to_names[i].flag_value;
13686 else if (tf->with == without_flags)
13687 without_hex |= elf_flags_to_names[i].flag_value;
13688 tf->valid = TRUE;
13689 break;
13690 }
13691 }
13692 if (!tf->valid)
13693 {
13694 info->callbacks->einfo
13695 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13696 return FALSE;
13697 }
13698 }
13699 flaginfo->flags_initialized = TRUE;
13700 flaginfo->only_with_flags |= with_hex;
13701 flaginfo->not_with_flags |= without_hex;
13702 }
13703
13704 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13705 return FALSE;
13706
13707 if ((flaginfo->not_with_flags & sh_flags) != 0)
13708 return FALSE;
13709
13710 return TRUE;
13711 }
13712
13713 struct alloc_got_off_arg {
13714 bfd_vma gotoff;
13715 struct bfd_link_info *info;
13716 };
13717
13718 /* We need a special top-level link routine to convert got reference counts
13719 to real got offsets. */
13720
13721 static bfd_boolean
13722 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13723 {
13724 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13725 bfd *obfd = gofarg->info->output_bfd;
13726 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13727
13728 if (h->got.refcount > 0)
13729 {
13730 h->got.offset = gofarg->gotoff;
13731 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13732 }
13733 else
13734 h->got.offset = (bfd_vma) -1;
13735
13736 return TRUE;
13737 }
13738
13739 /* And an accompanying bit to work out final got entry offsets once
13740 we're done. Should be called from final_link. */
13741
13742 bfd_boolean
13743 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13744 struct bfd_link_info *info)
13745 {
13746 bfd *i;
13747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13748 bfd_vma gotoff;
13749 struct alloc_got_off_arg gofarg;
13750
13751 BFD_ASSERT (abfd == info->output_bfd);
13752
13753 if (! is_elf_hash_table (info->hash))
13754 return FALSE;
13755
13756 /* The GOT offset is relative to the .got section, but the GOT header is
13757 put into the .got.plt section, if the backend uses it. */
13758 if (bed->want_got_plt)
13759 gotoff = 0;
13760 else
13761 gotoff = bed->got_header_size;
13762
13763 /* Do the local .got entries first. */
13764 for (i = info->input_bfds; i; i = i->link.next)
13765 {
13766 bfd_signed_vma *local_got;
13767 size_t j, locsymcount;
13768 Elf_Internal_Shdr *symtab_hdr;
13769
13770 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13771 continue;
13772
13773 local_got = elf_local_got_refcounts (i);
13774 if (!local_got)
13775 continue;
13776
13777 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13778 if (elf_bad_symtab (i))
13779 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13780 else
13781 locsymcount = symtab_hdr->sh_info;
13782
13783 for (j = 0; j < locsymcount; ++j)
13784 {
13785 if (local_got[j] > 0)
13786 {
13787 local_got[j] = gotoff;
13788 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13789 }
13790 else
13791 local_got[j] = (bfd_vma) -1;
13792 }
13793 }
13794
13795 /* Then the global .got entries. .plt refcounts are handled by
13796 adjust_dynamic_symbol */
13797 gofarg.gotoff = gotoff;
13798 gofarg.info = info;
13799 elf_link_hash_traverse (elf_hash_table (info),
13800 elf_gc_allocate_got_offsets,
13801 &gofarg);
13802 return TRUE;
13803 }
13804
13805 /* Many folk need no more in the way of final link than this, once
13806 got entry reference counting is enabled. */
13807
13808 bfd_boolean
13809 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13810 {
13811 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13812 return FALSE;
13813
13814 /* Invoke the regular ELF backend linker to do all the work. */
13815 return bfd_elf_final_link (abfd, info);
13816 }
13817
13818 bfd_boolean
13819 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13820 {
13821 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13822
13823 if (rcookie->bad_symtab)
13824 rcookie->rel = rcookie->rels;
13825
13826 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13827 {
13828 unsigned long r_symndx;
13829
13830 if (! rcookie->bad_symtab)
13831 if (rcookie->rel->r_offset > offset)
13832 return FALSE;
13833 if (rcookie->rel->r_offset != offset)
13834 continue;
13835
13836 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13837 if (r_symndx == STN_UNDEF)
13838 return TRUE;
13839
13840 if (r_symndx >= rcookie->locsymcount
13841 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13842 {
13843 struct elf_link_hash_entry *h;
13844
13845 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13846
13847 while (h->root.type == bfd_link_hash_indirect
13848 || h->root.type == bfd_link_hash_warning)
13849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13850
13851 if ((h->root.type == bfd_link_hash_defined
13852 || h->root.type == bfd_link_hash_defweak)
13853 && (h->root.u.def.section->owner != rcookie->abfd
13854 || h->root.u.def.section->kept_section != NULL
13855 || discarded_section (h->root.u.def.section)))
13856 return TRUE;
13857 }
13858 else
13859 {
13860 /* It's not a relocation against a global symbol,
13861 but it could be a relocation against a local
13862 symbol for a discarded section. */
13863 asection *isec;
13864 Elf_Internal_Sym *isym;
13865
13866 /* Need to: get the symbol; get the section. */
13867 isym = &rcookie->locsyms[r_symndx];
13868 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13869 if (isec != NULL
13870 && (isec->kept_section != NULL
13871 || discarded_section (isec)))
13872 return TRUE;
13873 }
13874 return FALSE;
13875 }
13876 return FALSE;
13877 }
13878
13879 /* Discard unneeded references to discarded sections.
13880 Returns -1 on error, 1 if any section's size was changed, 0 if
13881 nothing changed. This function assumes that the relocations are in
13882 sorted order, which is true for all known assemblers. */
13883
13884 int
13885 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13886 {
13887 struct elf_reloc_cookie cookie;
13888 asection *o;
13889 bfd *abfd;
13890 int changed = 0;
13891
13892 if (info->traditional_format
13893 || !is_elf_hash_table (info->hash))
13894 return 0;
13895
13896 o = bfd_get_section_by_name (output_bfd, ".stab");
13897 if (o != NULL)
13898 {
13899 asection *i;
13900
13901 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13902 {
13903 if (i->size == 0
13904 || i->reloc_count == 0
13905 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13906 continue;
13907
13908 abfd = i->owner;
13909 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13910 continue;
13911
13912 if (!init_reloc_cookie_for_section (&cookie, info, i))
13913 return -1;
13914
13915 if (_bfd_discard_section_stabs (abfd, i,
13916 elf_section_data (i)->sec_info,
13917 bfd_elf_reloc_symbol_deleted_p,
13918 &cookie))
13919 changed = 1;
13920
13921 fini_reloc_cookie_for_section (&cookie, i);
13922 }
13923 }
13924
13925 o = NULL;
13926 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13927 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13928 if (o != NULL)
13929 {
13930 asection *i;
13931 int eh_changed = 0;
13932 unsigned int eh_alignment;
13933
13934 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13935 {
13936 if (i->size == 0)
13937 continue;
13938
13939 abfd = i->owner;
13940 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13941 continue;
13942
13943 if (!init_reloc_cookie_for_section (&cookie, info, i))
13944 return -1;
13945
13946 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13947 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13948 bfd_elf_reloc_symbol_deleted_p,
13949 &cookie))
13950 {
13951 eh_changed = 1;
13952 if (i->size != i->rawsize)
13953 changed = 1;
13954 }
13955
13956 fini_reloc_cookie_for_section (&cookie, i);
13957 }
13958
13959 eh_alignment = 1 << o->alignment_power;
13960 /* Skip over zero terminator, and prevent empty sections from
13961 adding alignment padding at the end. */
13962 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13963 if (i->size == 0)
13964 i->flags |= SEC_EXCLUDE;
13965 else if (i->size > 4)
13966 break;
13967 /* The last non-empty eh_frame section doesn't need padding. */
13968 if (i != NULL)
13969 i = i->map_tail.s;
13970 /* Any prior sections must pad the last FDE out to the output
13971 section alignment. Otherwise we might have zero padding
13972 between sections, which would be seen as a terminator. */
13973 for (; i != NULL; i = i->map_tail.s)
13974 if (i->size == 4)
13975 /* All but the last zero terminator should have been removed. */
13976 BFD_FAIL ();
13977 else
13978 {
13979 bfd_size_type size
13980 = (i->size + eh_alignment - 1) & -eh_alignment;
13981 if (i->size != size)
13982 {
13983 i->size = size;
13984 changed = 1;
13985 eh_changed = 1;
13986 }
13987 }
13988 if (eh_changed)
13989 elf_link_hash_traverse (elf_hash_table (info),
13990 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13991 }
13992
13993 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13994 {
13995 const struct elf_backend_data *bed;
13996 asection *s;
13997
13998 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13999 continue;
14000 s = abfd->sections;
14001 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14002 continue;
14003
14004 bed = get_elf_backend_data (abfd);
14005
14006 if (bed->elf_backend_discard_info != NULL)
14007 {
14008 if (!init_reloc_cookie (&cookie, info, abfd))
14009 return -1;
14010
14011 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14012 changed = 1;
14013
14014 fini_reloc_cookie (&cookie, abfd);
14015 }
14016 }
14017
14018 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14019 _bfd_elf_end_eh_frame_parsing (info);
14020
14021 if (info->eh_frame_hdr_type
14022 && !bfd_link_relocatable (info)
14023 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14024 changed = 1;
14025
14026 return changed;
14027 }
14028
14029 bfd_boolean
14030 _bfd_elf_section_already_linked (bfd *abfd,
14031 asection *sec,
14032 struct bfd_link_info *info)
14033 {
14034 flagword flags;
14035 const char *name, *key;
14036 struct bfd_section_already_linked *l;
14037 struct bfd_section_already_linked_hash_entry *already_linked_list;
14038
14039 if (sec->output_section == bfd_abs_section_ptr)
14040 return FALSE;
14041
14042 flags = sec->flags;
14043
14044 /* Return if it isn't a linkonce section. A comdat group section
14045 also has SEC_LINK_ONCE set. */
14046 if ((flags & SEC_LINK_ONCE) == 0)
14047 return FALSE;
14048
14049 /* Don't put group member sections on our list of already linked
14050 sections. They are handled as a group via their group section. */
14051 if (elf_sec_group (sec) != NULL)
14052 return FALSE;
14053
14054 /* For a SHT_GROUP section, use the group signature as the key. */
14055 name = sec->name;
14056 if ((flags & SEC_GROUP) != 0
14057 && elf_next_in_group (sec) != NULL
14058 && elf_group_name (elf_next_in_group (sec)) != NULL)
14059 key = elf_group_name (elf_next_in_group (sec));
14060 else
14061 {
14062 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14063 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14064 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14065 key++;
14066 else
14067 /* Must be a user linkonce section that doesn't follow gcc's
14068 naming convention. In this case we won't be matching
14069 single member groups. */
14070 key = name;
14071 }
14072
14073 already_linked_list = bfd_section_already_linked_table_lookup (key);
14074
14075 for (l = already_linked_list->entry; l != NULL; l = l->next)
14076 {
14077 /* We may have 2 different types of sections on the list: group
14078 sections with a signature of <key> (<key> is some string),
14079 and linkonce sections named .gnu.linkonce.<type>.<key>.
14080 Match like sections. LTO plugin sections are an exception.
14081 They are always named .gnu.linkonce.t.<key> and match either
14082 type of section. */
14083 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14084 && ((flags & SEC_GROUP) != 0
14085 || strcmp (name, l->sec->name) == 0))
14086 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14087 {
14088 /* The section has already been linked. See if we should
14089 issue a warning. */
14090 if (!_bfd_handle_already_linked (sec, l, info))
14091 return FALSE;
14092
14093 if (flags & SEC_GROUP)
14094 {
14095 asection *first = elf_next_in_group (sec);
14096 asection *s = first;
14097
14098 while (s != NULL)
14099 {
14100 s->output_section = bfd_abs_section_ptr;
14101 /* Record which group discards it. */
14102 s->kept_section = l->sec;
14103 s = elf_next_in_group (s);
14104 /* These lists are circular. */
14105 if (s == first)
14106 break;
14107 }
14108 }
14109
14110 return TRUE;
14111 }
14112 }
14113
14114 /* A single member comdat group section may be discarded by a
14115 linkonce section and vice versa. */
14116 if ((flags & SEC_GROUP) != 0)
14117 {
14118 asection *first = elf_next_in_group (sec);
14119
14120 if (first != NULL && elf_next_in_group (first) == first)
14121 /* Check this single member group against linkonce sections. */
14122 for (l = already_linked_list->entry; l != NULL; l = l->next)
14123 if ((l->sec->flags & SEC_GROUP) == 0
14124 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14125 {
14126 first->output_section = bfd_abs_section_ptr;
14127 first->kept_section = l->sec;
14128 sec->output_section = bfd_abs_section_ptr;
14129 break;
14130 }
14131 }
14132 else
14133 /* Check this linkonce section against single member groups. */
14134 for (l = already_linked_list->entry; l != NULL; l = l->next)
14135 if (l->sec->flags & SEC_GROUP)
14136 {
14137 asection *first = elf_next_in_group (l->sec);
14138
14139 if (first != NULL
14140 && elf_next_in_group (first) == first
14141 && bfd_elf_match_symbols_in_sections (first, sec, info))
14142 {
14143 sec->output_section = bfd_abs_section_ptr;
14144 sec->kept_section = first;
14145 break;
14146 }
14147 }
14148
14149 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14150 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14151 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14152 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14153 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14154 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14155 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14156 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14157 The reverse order cannot happen as there is never a bfd with only the
14158 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14159 matter as here were are looking only for cross-bfd sections. */
14160
14161 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14162 for (l = already_linked_list->entry; l != NULL; l = l->next)
14163 if ((l->sec->flags & SEC_GROUP) == 0
14164 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14165 {
14166 if (abfd != l->sec->owner)
14167 sec->output_section = bfd_abs_section_ptr;
14168 break;
14169 }
14170
14171 /* This is the first section with this name. Record it. */
14172 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14173 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14174 return sec->output_section == bfd_abs_section_ptr;
14175 }
14176
14177 bfd_boolean
14178 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14179 {
14180 return sym->st_shndx == SHN_COMMON;
14181 }
14182
14183 unsigned int
14184 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14185 {
14186 return SHN_COMMON;
14187 }
14188
14189 asection *
14190 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14191 {
14192 return bfd_com_section_ptr;
14193 }
14194
14195 bfd_vma
14196 _bfd_elf_default_got_elt_size (bfd *abfd,
14197 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14198 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14199 bfd *ibfd ATTRIBUTE_UNUSED,
14200 unsigned long symndx ATTRIBUTE_UNUSED)
14201 {
14202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14203 return bed->s->arch_size / 8;
14204 }
14205
14206 /* Routines to support the creation of dynamic relocs. */
14207
14208 /* Returns the name of the dynamic reloc section associated with SEC. */
14209
14210 static const char *
14211 get_dynamic_reloc_section_name (bfd * abfd,
14212 asection * sec,
14213 bfd_boolean is_rela)
14214 {
14215 char *name;
14216 const char *old_name = bfd_get_section_name (NULL, sec);
14217 const char *prefix = is_rela ? ".rela" : ".rel";
14218
14219 if (old_name == NULL)
14220 return NULL;
14221
14222 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14223 sprintf (name, "%s%s", prefix, old_name);
14224
14225 return name;
14226 }
14227
14228 /* Returns the dynamic reloc section associated with SEC.
14229 If necessary compute the name of the dynamic reloc section based
14230 on SEC's name (looked up in ABFD's string table) and the setting
14231 of IS_RELA. */
14232
14233 asection *
14234 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14235 asection * sec,
14236 bfd_boolean is_rela)
14237 {
14238 asection * reloc_sec = elf_section_data (sec)->sreloc;
14239
14240 if (reloc_sec == NULL)
14241 {
14242 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14243
14244 if (name != NULL)
14245 {
14246 reloc_sec = bfd_get_linker_section (abfd, name);
14247
14248 if (reloc_sec != NULL)
14249 elf_section_data (sec)->sreloc = reloc_sec;
14250 }
14251 }
14252
14253 return reloc_sec;
14254 }
14255
14256 /* Returns the dynamic reloc section associated with SEC. If the
14257 section does not exist it is created and attached to the DYNOBJ
14258 bfd and stored in the SRELOC field of SEC's elf_section_data
14259 structure.
14260
14261 ALIGNMENT is the alignment for the newly created section and
14262 IS_RELA defines whether the name should be .rela.<SEC's name>
14263 or .rel.<SEC's name>. The section name is looked up in the
14264 string table associated with ABFD. */
14265
14266 asection *
14267 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14268 bfd *dynobj,
14269 unsigned int alignment,
14270 bfd *abfd,
14271 bfd_boolean is_rela)
14272 {
14273 asection * reloc_sec = elf_section_data (sec)->sreloc;
14274
14275 if (reloc_sec == NULL)
14276 {
14277 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14278
14279 if (name == NULL)
14280 return NULL;
14281
14282 reloc_sec = bfd_get_linker_section (dynobj, name);
14283
14284 if (reloc_sec == NULL)
14285 {
14286 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14287 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14288 if ((sec->flags & SEC_ALLOC) != 0)
14289 flags |= SEC_ALLOC | SEC_LOAD;
14290
14291 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14292 if (reloc_sec != NULL)
14293 {
14294 /* _bfd_elf_get_sec_type_attr chooses a section type by
14295 name. Override as it may be wrong, eg. for a user
14296 section named "auto" we'll get ".relauto" which is
14297 seen to be a .rela section. */
14298 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14299 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14300 reloc_sec = NULL;
14301 }
14302 }
14303
14304 elf_section_data (sec)->sreloc = reloc_sec;
14305 }
14306
14307 return reloc_sec;
14308 }
14309
14310 /* Copy the ELF symbol type and other attributes for a linker script
14311 assignment from HSRC to HDEST. Generally this should be treated as
14312 if we found a strong non-dynamic definition for HDEST (except that
14313 ld ignores multiple definition errors). */
14314 void
14315 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14316 struct bfd_link_hash_entry *hdest,
14317 struct bfd_link_hash_entry *hsrc)
14318 {
14319 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14320 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14321 Elf_Internal_Sym isym;
14322
14323 ehdest->type = ehsrc->type;
14324 ehdest->target_internal = ehsrc->target_internal;
14325
14326 isym.st_other = ehsrc->other;
14327 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14328 }
14329
14330 /* Append a RELA relocation REL to section S in BFD. */
14331
14332 void
14333 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14334 {
14335 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14336 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14337 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14338 bed->s->swap_reloca_out (abfd, rel, loc);
14339 }
14340
14341 /* Append a REL relocation REL to section S in BFD. */
14342
14343 void
14344 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14345 {
14346 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14347 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14348 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14349 bed->s->swap_reloc_out (abfd, rel, loc);
14350 }
14351
14352 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14353
14354 struct bfd_link_hash_entry *
14355 bfd_elf_define_start_stop (struct bfd_link_info *info,
14356 const char *symbol, asection *sec)
14357 {
14358 struct elf_link_hash_entry *h;
14359
14360 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14361 FALSE, FALSE, TRUE);
14362 if (h != NULL
14363 && (h->root.type == bfd_link_hash_undefined
14364 || h->root.type == bfd_link_hash_undefweak
14365 || (h->ref_regular && !h->def_regular)))
14366 {
14367 h->root.type = bfd_link_hash_defined;
14368 h->root.u.def.section = sec;
14369 h->root.u.def.value = 0;
14370 h->def_regular = 1;
14371 h->def_dynamic = 0;
14372 h->start_stop = 1;
14373 h->u2.start_stop_section = sec;
14374 if (symbol[0] == '.')
14375 {
14376 /* .startof. and .sizeof. symbols are local. */
14377 const struct elf_backend_data *bed;
14378 bed = get_elf_backend_data (info->output_bfd);
14379 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14380 }
14381 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14382 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14383 return &h->root;
14384 }
14385 return NULL;
14386 }
14387