elflink.c revision 1.18 1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 return FALSE;
181 htab->sgotplt = s;
182 }
183
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
196 if (h == NULL)
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202
203 /* Create a strtab to hold the dynamic symbol names. */
205 static bfd_boolean
206 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207 {
208 struct elf_link_hash_table *hash_table;
209
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
212 {
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 {
219 bfd *ibfd;
220 asection *s;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243 }
244
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
251
252 bfd_boolean
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
254 {
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
259
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
268
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
277 {
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
282 }
283
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || !bfd_set_section_alignment (s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || !bfd_set_section_alignment (s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || !bfd_set_section_alignment (s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || !bfd_set_section_alignment (s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || !bfd_set_section_alignment (s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
343 {
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || !bfd_set_section_alignment (s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368 }
369
370 /* Create dynamic sections when linking against a dynamic object. */
371
372 bfd_boolean
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374 {
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
380
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
384
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || !bfd_set_section_alignment (s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || !bfd_set_section_alignment (s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || !bfd_set_section_alignment (s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || !bfd_set_section_alignment (s, bed->s->log_file_align))
481 return FALSE;
482 htab->sreldynrelro = s;
483 }
484 }
485 }
486
487 return TRUE;
488 }
489
490 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
499 bfd_boolean
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
502 {
503 if (h->dynindx == -1)
504 {
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
509
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
520 {
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
524 }
525
526 default:
527 break;
528 }
529
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
540 }
541
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
558
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
562 }
563
564 return TRUE;
565 }
566
567 /* Mark a symbol dynamic. */
569
570 static void
571 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
572 struct elf_link_hash_entry *h,
573 Elf_Internal_Sym *sym)
574 {
575 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576
577 /* It may be called more than once on the same H. */
578 if(h->dynamic || bfd_link_relocatable (info))
579 return;
580
581 if ((info->dynamic_data
582 && (h->type == STT_OBJECT
583 || h->type == STT_COMMON
584 || (sym != NULL
585 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
586 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
587 || (d != NULL
588 && h->non_elf
589 && (*d->match) (&d->head, NULL, h->root.root.string)))
590 {
591 h->dynamic = 1;
592 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 non-IR reference. */
594 h->root.non_ir_ref_dynamic = 1;
595 }
596 }
597
598 /* Record an assignment to a symbol made by a linker script. We need
599 this in case some dynamic object refers to this symbol. */
600
601 bfd_boolean
602 bfd_elf_record_link_assignment (bfd *output_bfd,
603 struct bfd_link_info *info,
604 const char *name,
605 bfd_boolean provide,
606 bfd_boolean hidden)
607 {
608 struct elf_link_hash_entry *h, *hv;
609 struct elf_link_hash_table *htab;
610 const struct elf_backend_data *bed;
611
612 if (!is_elf_hash_table (info->hash))
613 return TRUE;
614
615 htab = elf_hash_table (info);
616 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
617 if (h == NULL)
618 return provide;
619
620 if (h->root.type == bfd_link_hash_warning)
621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
622
623 if (h->versioned == unknown)
624 {
625 /* Set versioned if symbol version is unknown. */
626 char *version = strrchr (name, ELF_VER_CHR);
627 if (version)
628 {
629 if (version > name && version[-1] != ELF_VER_CHR)
630 h->versioned = versioned_hidden;
631 else
632 h->versioned = versioned;
633 }
634 }
635
636 /* Symbols defined in a linker script but not referenced anywhere
637 else will have non_elf set. */
638 if (h->non_elf)
639 {
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 h->non_elf = 0;
642 }
643
644 switch (h->root.type)
645 {
646 case bfd_link_hash_defined:
647 case bfd_link_hash_defweak:
648 case bfd_link_hash_common:
649 break;
650 case bfd_link_hash_undefweak:
651 case bfd_link_hash_undefined:
652 /* Since we're defining the symbol, don't let it seem to have not
653 been defined. record_dynamic_symbol and size_dynamic_sections
654 may depend on this. */
655 h->root.type = bfd_link_hash_new;
656 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
657 bfd_link_repair_undef_list (&htab->root);
658 break;
659 case bfd_link_hash_new:
660 break;
661 case bfd_link_hash_indirect:
662 /* We had a versioned symbol in a dynamic library. We make the
663 the versioned symbol point to this one. */
664 bed = get_elf_backend_data (output_bfd);
665 hv = h;
666 while (hv->root.type == bfd_link_hash_indirect
667 || hv->root.type == bfd_link_hash_warning)
668 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
669 /* We don't need to update h->root.u since linker will set them
670 later. */
671 h->root.type = bfd_link_hash_undefined;
672 hv->root.type = bfd_link_hash_indirect;
673 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
674 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 break;
676 default:
677 BFD_FAIL ();
678 return FALSE;
679 }
680
681 /* If this symbol is being provided by the linker script, and it is
682 currently defined by a dynamic object, but not by a regular
683 object, then mark it as undefined so that the generic linker will
684 force the correct value. */
685 if (provide
686 && h->def_dynamic
687 && !h->def_regular)
688 h->root.type = bfd_link_hash_undefined;
689
690 /* If this symbol is currently defined by a dynamic object, but not
691 by a regular object, then clear out any version information because
692 the symbol will not be associated with the dynamic object any
693 more. */
694 if (h->def_dynamic && !h->def_regular)
695 h->verinfo.verdef = NULL;
696
697 /* Make sure this symbol is not garbage collected. */
698 h->mark = 1;
699
700 h->def_regular = 1;
701
702 if (hidden)
703 {
704 bed = get_elf_backend_data (output_bfd);
705 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
706 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
707 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
708 }
709
710 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
711 and executables. */
712 if (!bfd_link_relocatable (info)
713 && h->dynindx != -1
714 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
715 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 h->forced_local = 1;
717
718 if ((h->def_dynamic
719 || h->ref_dynamic
720 || bfd_link_dll (info)
721 || elf_hash_table (info)->is_relocatable_executable)
722 && !h->forced_local
723 && h->dynindx == -1)
724 {
725 if (! bfd_elf_link_record_dynamic_symbol (info, h))
726 return FALSE;
727
728 /* If this is a weak defined symbol, and we know a corresponding
729 real symbol from the same dynamic object, make sure the real
730 symbol is also made into a dynamic symbol. */
731 if (h->is_weakalias)
732 {
733 struct elf_link_hash_entry *def = weakdef (h);
734
735 if (def->dynindx == -1
736 && !bfd_elf_link_record_dynamic_symbol (info, def))
737 return FALSE;
738 }
739 }
740
741 return TRUE;
742 }
743
744 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
745 success, and 2 on a failure caused by attempting to record a symbol
746 in a discarded section, eg. a discarded link-once section symbol. */
747
748 int
749 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
752 {
753 bfd_size_type amt;
754 struct elf_link_local_dynamic_entry *entry;
755 struct elf_link_hash_table *eht;
756 struct elf_strtab_hash *dynstr;
757 size_t dynstr_index;
758 char *name;
759 Elf_External_Sym_Shndx eshndx;
760 char esym[sizeof (Elf64_External_Sym)];
761
762 if (! is_elf_hash_table (info->hash))
763 return 0;
764
765 /* See if the entry exists already. */
766 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
767 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
768 return 1;
769
770 amt = sizeof (*entry);
771 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
772 if (entry == NULL)
773 return 0;
774
775 /* Go find the symbol, so that we can find it's name. */
776 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
777 1, input_indx, &entry->isym, esym, &eshndx))
778 {
779 bfd_release (input_bfd, entry);
780 return 0;
781 }
782
783 if (entry->isym.st_shndx != SHN_UNDEF
784 && entry->isym.st_shndx < SHN_LORESERVE)
785 {
786 asection *s;
787
788 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
789 if (s == NULL || bfd_is_abs_section (s->output_section))
790 {
791 /* We can still bfd_release here as nothing has done another
792 bfd_alloc. We can't do this later in this function. */
793 bfd_release (input_bfd, entry);
794 return 2;
795 }
796 }
797
798 name = (bfd_elf_string_from_elf_section
799 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
800 entry->isym.st_name));
801
802 dynstr = elf_hash_table (info)->dynstr;
803 if (dynstr == NULL)
804 {
805 /* Create a strtab to hold the dynamic symbol names. */
806 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
807 if (dynstr == NULL)
808 return 0;
809 }
810
811 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
812 if (dynstr_index == (size_t) -1)
813 return 0;
814 entry->isym.st_name = dynstr_index;
815
816 eht = elf_hash_table (info);
817
818 entry->next = eht->dynlocal;
819 eht->dynlocal = entry;
820 entry->input_bfd = input_bfd;
821 entry->input_indx = input_indx;
822 eht->dynsymcount++;
823
824 /* Whatever binding the symbol had before, it's now local. */
825 entry->isym.st_info
826 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
827
828 /* The dynindx will be set at the end of size_dynamic_sections. */
829
830 return 1;
831 }
832
833 /* Return the dynindex of a local dynamic symbol. */
834
835 long
836 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
837 bfd *input_bfd,
838 long input_indx)
839 {
840 struct elf_link_local_dynamic_entry *e;
841
842 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
843 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
844 return e->dynindx;
845 return -1;
846 }
847
848 /* This function is used to renumber the dynamic symbols, if some of
849 them are removed because they are marked as local. This is called
850 via elf_link_hash_traverse. */
851
852 static bfd_boolean
853 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
854 void *data)
855 {
856 size_t *count = (size_t *) data;
857
858 if (h->forced_local)
859 return TRUE;
860
861 if (h->dynindx != -1)
862 h->dynindx = ++(*count);
863
864 return TRUE;
865 }
866
867
868 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
869 STB_LOCAL binding. */
870
871 static bfd_boolean
872 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (!h->forced_local)
878 return TRUE;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return TRUE;
884 }
885
886 /* Return true if the dynamic symbol for a given section should be
887 omitted when creating a shared library. */
888 bfd_boolean
889 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
890 struct bfd_link_info *info,
891 asection *p)
892 {
893 struct elf_link_hash_table *htab;
894 asection *ip;
895
896 switch (elf_section_data (p)->this_hdr.sh_type)
897 {
898 case SHT_PROGBITS:
899 case SHT_NOBITS:
900 /* If sh_type is yet undecided, assume it could be
901 SHT_PROGBITS/SHT_NOBITS. */
902 case SHT_NULL:
903 htab = elf_hash_table (info);
904 if (htab->text_index_section != NULL)
905 return p != htab->text_index_section && p != htab->data_index_section;
906
907 return (htab->dynobj != NULL
908 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
909 && ip->output_section == p);
910
911 /* There shouldn't be section relative relocations
912 against any other section. */
913 default:
914 return TRUE;
915 }
916 }
917
918 bfd_boolean
919 _bfd_elf_omit_section_dynsym_all
920 (bfd *output_bfd ATTRIBUTE_UNUSED,
921 struct bfd_link_info *info ATTRIBUTE_UNUSED,
922 asection *p ATTRIBUTE_UNUSED)
923 {
924 return TRUE;
925 }
926
927 /* Assign dynsym indices. In a shared library we generate a section
928 symbol for each output section, which come first. Next come symbols
929 which have been forced to local binding. Then all of the back-end
930 allocated local dynamic syms, followed by the rest of the global
931 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
932 (This prevents the early call before elf_backend_init_index_section
933 and strip_excluded_output_sections setting dynindx for sections
934 that are stripped.) */
935
936 static unsigned long
937 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
938 struct bfd_link_info *info,
939 unsigned long *section_sym_count)
940 {
941 unsigned long dynsymcount = 0;
942 bfd_boolean do_sec = section_sym_count != NULL;
943
944 if (bfd_link_pic (info)
945 || elf_hash_table (info)->is_relocatable_executable)
946 {
947 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
948 asection *p;
949 for (p = output_bfd->sections; p ; p = p->next)
950 if ((p->flags & SEC_EXCLUDE) == 0
951 && (p->flags & SEC_ALLOC) != 0
952 && elf_hash_table (info)->dynamic_relocs
953 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
954 {
955 ++dynsymcount;
956 if (do_sec)
957 elf_section_data (p)->dynindx = dynsymcount;
958 }
959 else if (do_sec)
960 elf_section_data (p)->dynindx = 0;
961 }
962 if (do_sec)
963 *section_sym_count = dynsymcount;
964
965 elf_link_hash_traverse (elf_hash_table (info),
966 elf_link_renumber_local_hash_table_dynsyms,
967 &dynsymcount);
968
969 if (elf_hash_table (info)->dynlocal)
970 {
971 struct elf_link_local_dynamic_entry *p;
972 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
973 p->dynindx = ++dynsymcount;
974 }
975 elf_hash_table (info)->local_dynsymcount = dynsymcount;
976
977 elf_link_hash_traverse (elf_hash_table (info),
978 elf_link_renumber_hash_table_dynsyms,
979 &dynsymcount);
980
981 /* There is an unused NULL entry at the head of the table which we
982 must account for in our count even if the table is empty since it
983 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
984 .dynamic section. */
985 dynsymcount++;
986
987 elf_hash_table (info)->dynsymcount = dynsymcount;
988 return dynsymcount;
989 }
990
991 /* Merge st_other field. */
992
993 static void
994 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
995 const Elf_Internal_Sym *isym, asection *sec,
996 bfd_boolean definition, bfd_boolean dynamic)
997 {
998 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
999
1000 /* If st_other has a processor-specific meaning, specific
1001 code might be needed here. */
1002 if (bed->elf_backend_merge_symbol_attribute)
1003 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1004 dynamic);
1005
1006 if (!dynamic)
1007 {
1008 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1009 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1010
1011 /* Keep the most constraining visibility. Leave the remainder
1012 of the st_other field to elf_backend_merge_symbol_attribute. */
1013 if (symvis - 1 < hvis - 1)
1014 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1015 }
1016 else if (definition
1017 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1018 && (sec->flags & SEC_READONLY) == 0)
1019 h->protected_def = 1;
1020 }
1021
1022 /* This function is called when we want to merge a new symbol with an
1023 existing symbol. It handles the various cases which arise when we
1024 find a definition in a dynamic object, or when there is already a
1025 definition in a dynamic object. The new symbol is described by
1026 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1027 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1028 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1029 of an old common symbol. We set OVERRIDE if the old symbol is
1030 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1031 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1032 to change. By OK to change, we mean that we shouldn't warn if the
1033 type or size does change. */
1034
1035 static bfd_boolean
1036 _bfd_elf_merge_symbol (bfd *abfd,
1037 struct bfd_link_info *info,
1038 const char *name,
1039 Elf_Internal_Sym *sym,
1040 asection **psec,
1041 bfd_vma *pvalue,
1042 struct elf_link_hash_entry **sym_hash,
1043 bfd **poldbfd,
1044 bfd_boolean *pold_weak,
1045 unsigned int *pold_alignment,
1046 bfd_boolean *skip,
1047 bfd_boolean *override,
1048 bfd_boolean *type_change_ok,
1049 bfd_boolean *size_change_ok,
1050 bfd_boolean *matched)
1051 {
1052 asection *sec, *oldsec;
1053 struct elf_link_hash_entry *h;
1054 struct elf_link_hash_entry *hi;
1055 struct elf_link_hash_entry *flip;
1056 int bind;
1057 bfd *oldbfd;
1058 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1059 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1060 const struct elf_backend_data *bed;
1061 char *new_version;
1062 bfd_boolean default_sym = *matched;
1063
1064 *skip = FALSE;
1065 *override = FALSE;
1066
1067 sec = *psec;
1068 bind = ELF_ST_BIND (sym->st_info);
1069
1070 if (! bfd_is_und_section (sec))
1071 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1072 else
1073 h = ((struct elf_link_hash_entry *)
1074 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1075 if (h == NULL)
1076 return FALSE;
1077 *sym_hash = h;
1078
1079 bed = get_elf_backend_data (abfd);
1080
1081 /* NEW_VERSION is the symbol version of the new symbol. */
1082 if (h->versioned != unversioned)
1083 {
1084 /* Symbol version is unknown or versioned. */
1085 new_version = strrchr (name, ELF_VER_CHR);
1086 if (new_version)
1087 {
1088 if (h->versioned == unknown)
1089 {
1090 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1091 h->versioned = versioned_hidden;
1092 else
1093 h->versioned = versioned;
1094 }
1095 new_version += 1;
1096 if (new_version[0] == '\0')
1097 new_version = NULL;
1098 }
1099 else
1100 h->versioned = unversioned;
1101 }
1102 else
1103 new_version = NULL;
1104
1105 /* For merging, we only care about real symbols. But we need to make
1106 sure that indirect symbol dynamic flags are updated. */
1107 hi = h;
1108 while (h->root.type == bfd_link_hash_indirect
1109 || h->root.type == bfd_link_hash_warning)
1110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1111
1112 if (!*matched)
1113 {
1114 if (hi == h || h->root.type == bfd_link_hash_new)
1115 *matched = TRUE;
1116 else
1117 {
1118 /* OLD_HIDDEN is true if the existing symbol is only visible
1119 to the symbol with the same symbol version. NEW_HIDDEN is
1120 true if the new symbol is only visible to the symbol with
1121 the same symbol version. */
1122 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1123 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1124 if (!old_hidden && !new_hidden)
1125 /* The new symbol matches the existing symbol if both
1126 aren't hidden. */
1127 *matched = TRUE;
1128 else
1129 {
1130 /* OLD_VERSION is the symbol version of the existing
1131 symbol. */
1132 char *old_version;
1133
1134 if (h->versioned >= versioned)
1135 old_version = strrchr (h->root.root.string,
1136 ELF_VER_CHR) + 1;
1137 else
1138 old_version = NULL;
1139
1140 /* The new symbol matches the existing symbol if they
1141 have the same symbol version. */
1142 *matched = (old_version == new_version
1143 || (old_version != NULL
1144 && new_version != NULL
1145 && strcmp (old_version, new_version) == 0));
1146 }
1147 }
1148 }
1149
1150 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1151 existing symbol. */
1152
1153 oldbfd = NULL;
1154 oldsec = NULL;
1155 switch (h->root.type)
1156 {
1157 default:
1158 break;
1159
1160 case bfd_link_hash_undefined:
1161 case bfd_link_hash_undefweak:
1162 oldbfd = h->root.u.undef.abfd;
1163 break;
1164
1165 case bfd_link_hash_defined:
1166 case bfd_link_hash_defweak:
1167 oldbfd = h->root.u.def.section->owner;
1168 oldsec = h->root.u.def.section;
1169 break;
1170
1171 case bfd_link_hash_common:
1172 oldbfd = h->root.u.c.p->section->owner;
1173 oldsec = h->root.u.c.p->section;
1174 if (pold_alignment)
1175 *pold_alignment = h->root.u.c.p->alignment_power;
1176 break;
1177 }
1178 if (poldbfd && *poldbfd == NULL)
1179 *poldbfd = oldbfd;
1180
1181 /* Differentiate strong and weak symbols. */
1182 newweak = bind == STB_WEAK;
1183 oldweak = (h->root.type == bfd_link_hash_defweak
1184 || h->root.type == bfd_link_hash_undefweak);
1185 if (pold_weak)
1186 *pold_weak = oldweak;
1187
1188 /* We have to check it for every instance since the first few may be
1189 references and not all compilers emit symbol type for undefined
1190 symbols. */
1191 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1192
1193 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1194 respectively, is from a dynamic object. */
1195
1196 newdyn = (abfd->flags & DYNAMIC) != 0;
1197
1198 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1199 syms and defined syms in dynamic libraries respectively.
1200 ref_dynamic on the other hand can be set for a symbol defined in
1201 a dynamic library, and def_dynamic may not be set; When the
1202 definition in a dynamic lib is overridden by a definition in the
1203 executable use of the symbol in the dynamic lib becomes a
1204 reference to the executable symbol. */
1205 if (newdyn)
1206 {
1207 if (bfd_is_und_section (sec))
1208 {
1209 if (bind != STB_WEAK)
1210 {
1211 h->ref_dynamic_nonweak = 1;
1212 hi->ref_dynamic_nonweak = 1;
1213 }
1214 }
1215 else
1216 {
1217 /* Update the existing symbol only if they match. */
1218 if (*matched)
1219 h->dynamic_def = 1;
1220 hi->dynamic_def = 1;
1221 }
1222 }
1223
1224 /* If we just created the symbol, mark it as being an ELF symbol.
1225 Other than that, there is nothing to do--there is no merge issue
1226 with a newly defined symbol--so we just return. */
1227
1228 if (h->root.type == bfd_link_hash_new)
1229 {
1230 h->non_elf = 0;
1231 return TRUE;
1232 }
1233
1234 /* In cases involving weak versioned symbols, we may wind up trying
1235 to merge a symbol with itself. Catch that here, to avoid the
1236 confusion that results if we try to override a symbol with
1237 itself. The additional tests catch cases like
1238 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1239 dynamic object, which we do want to handle here. */
1240 if (abfd == oldbfd
1241 && (newweak || oldweak)
1242 && ((abfd->flags & DYNAMIC) == 0
1243 || !h->def_regular))
1244 return TRUE;
1245
1246 olddyn = FALSE;
1247 if (oldbfd != NULL)
1248 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1249 else if (oldsec != NULL)
1250 {
1251 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1252 indices used by MIPS ELF. */
1253 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1254 }
1255
1256 /* Handle a case where plugin_notice won't be called and thus won't
1257 set the non_ir_ref flags on the first pass over symbols. */
1258 if (oldbfd != NULL
1259 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1260 && newdyn != olddyn)
1261 {
1262 h->root.non_ir_ref_dynamic = TRUE;
1263 hi->root.non_ir_ref_dynamic = TRUE;
1264 }
1265
1266 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1267 respectively, appear to be a definition rather than reference. */
1268
1269 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1270
1271 olddef = (h->root.type != bfd_link_hash_undefined
1272 && h->root.type != bfd_link_hash_undefweak
1273 && h->root.type != bfd_link_hash_common);
1274
1275 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1276 respectively, appear to be a function. */
1277
1278 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1279 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1280
1281 oldfunc = (h->type != STT_NOTYPE
1282 && bed->is_function_type (h->type));
1283
1284 if (!(newfunc && oldfunc)
1285 && ELF_ST_TYPE (sym->st_info) != h->type
1286 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1287 && h->type != STT_NOTYPE
1288 && (newdef || bfd_is_com_section (sec))
1289 && (olddef || h->root.type == bfd_link_hash_common))
1290 {
1291 /* If creating a default indirect symbol ("foo" or "foo@") from
1292 a dynamic versioned definition ("foo@@") skip doing so if
1293 there is an existing regular definition with a different
1294 type. We don't want, for example, a "time" variable in the
1295 executable overriding a "time" function in a shared library. */
1296 if (newdyn
1297 && !olddyn)
1298 {
1299 *skip = TRUE;
1300 return TRUE;
1301 }
1302
1303 /* When adding a symbol from a regular object file after we have
1304 created indirect symbols, undo the indirection and any
1305 dynamic state. */
1306 if (hi != h
1307 && !newdyn
1308 && olddyn)
1309 {
1310 h = hi;
1311 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1312 h->forced_local = 0;
1313 h->ref_dynamic = 0;
1314 h->def_dynamic = 0;
1315 h->dynamic_def = 0;
1316 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1317 {
1318 h->root.type = bfd_link_hash_undefined;
1319 h->root.u.undef.abfd = abfd;
1320 }
1321 else
1322 {
1323 h->root.type = bfd_link_hash_new;
1324 h->root.u.undef.abfd = NULL;
1325 }
1326 return TRUE;
1327 }
1328 }
1329
1330 /* Check TLS symbols. We don't check undefined symbols introduced
1331 by "ld -u" which have no type (and oldbfd NULL), and we don't
1332 check symbols from plugins because they also have no type. */
1333 if (oldbfd != NULL
1334 && (oldbfd->flags & BFD_PLUGIN) == 0
1335 && (abfd->flags & BFD_PLUGIN) == 0
1336 && ELF_ST_TYPE (sym->st_info) != h->type
1337 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1338 {
1339 bfd *ntbfd, *tbfd;
1340 bfd_boolean ntdef, tdef;
1341 asection *ntsec, *tsec;
1342
1343 if (h->type == STT_TLS)
1344 {
1345 ntbfd = abfd;
1346 ntsec = sec;
1347 ntdef = newdef;
1348 tbfd = oldbfd;
1349 tsec = oldsec;
1350 tdef = olddef;
1351 }
1352 else
1353 {
1354 ntbfd = oldbfd;
1355 ntsec = oldsec;
1356 ntdef = olddef;
1357 tbfd = abfd;
1358 tsec = sec;
1359 tdef = newdef;
1360 }
1361
1362 if (tdef && ntdef)
1363 _bfd_error_handler
1364 /* xgettext:c-format */
1365 (_("%s: TLS definition in %pB section %pA "
1366 "mismatches non-TLS definition in %pB section %pA"),
1367 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1368 else if (!tdef && !ntdef)
1369 _bfd_error_handler
1370 /* xgettext:c-format */
1371 (_("%s: TLS reference in %pB "
1372 "mismatches non-TLS reference in %pB"),
1373 h->root.root.string, tbfd, ntbfd);
1374 else if (tdef)
1375 _bfd_error_handler
1376 /* xgettext:c-format */
1377 (_("%s: TLS definition in %pB section %pA "
1378 "mismatches non-TLS reference in %pB"),
1379 h->root.root.string, tbfd, tsec, ntbfd);
1380 else
1381 _bfd_error_handler
1382 /* xgettext:c-format */
1383 (_("%s: TLS reference in %pB "
1384 "mismatches non-TLS definition in %pB section %pA"),
1385 h->root.root.string, tbfd, ntbfd, ntsec);
1386
1387 bfd_set_error (bfd_error_bad_value);
1388 return FALSE;
1389 }
1390
1391 /* If the old symbol has non-default visibility, we ignore the new
1392 definition from a dynamic object. */
1393 if (newdyn
1394 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1395 && !bfd_is_und_section (sec))
1396 {
1397 *skip = TRUE;
1398 /* Make sure this symbol is dynamic. */
1399 h->ref_dynamic = 1;
1400 hi->ref_dynamic = 1;
1401 /* A protected symbol has external availability. Make sure it is
1402 recorded as dynamic.
1403
1404 FIXME: Should we check type and size for protected symbol? */
1405 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1406 return bfd_elf_link_record_dynamic_symbol (info, h);
1407 else
1408 return TRUE;
1409 }
1410 else if (!newdyn
1411 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1412 && h->def_dynamic)
1413 {
1414 /* If the new symbol with non-default visibility comes from a
1415 relocatable file and the old definition comes from a dynamic
1416 object, we remove the old definition. */
1417 if (hi->root.type == bfd_link_hash_indirect)
1418 {
1419 /* Handle the case where the old dynamic definition is
1420 default versioned. We need to copy the symbol info from
1421 the symbol with default version to the normal one if it
1422 was referenced before. */
1423 if (h->ref_regular)
1424 {
1425 hi->root.type = h->root.type;
1426 h->root.type = bfd_link_hash_indirect;
1427 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1428
1429 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1430 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1431 {
1432 /* If the new symbol is hidden or internal, completely undo
1433 any dynamic link state. */
1434 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1435 h->forced_local = 0;
1436 h->ref_dynamic = 0;
1437 }
1438 else
1439 h->ref_dynamic = 1;
1440
1441 h->def_dynamic = 0;
1442 /* FIXME: Should we check type and size for protected symbol? */
1443 h->size = 0;
1444 h->type = 0;
1445
1446 h = hi;
1447 }
1448 else
1449 h = hi;
1450 }
1451
1452 /* If the old symbol was undefined before, then it will still be
1453 on the undefs list. If the new symbol is undefined or
1454 common, we can't make it bfd_link_hash_new here, because new
1455 undefined or common symbols will be added to the undefs list
1456 by _bfd_generic_link_add_one_symbol. Symbols may not be
1457 added twice to the undefs list. Also, if the new symbol is
1458 undefweak then we don't want to lose the strong undef. */
1459 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1460 {
1461 h->root.type = bfd_link_hash_undefined;
1462 h->root.u.undef.abfd = abfd;
1463 }
1464 else
1465 {
1466 h->root.type = bfd_link_hash_new;
1467 h->root.u.undef.abfd = NULL;
1468 }
1469
1470 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1471 {
1472 /* If the new symbol is hidden or internal, completely undo
1473 any dynamic link state. */
1474 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1475 h->forced_local = 0;
1476 h->ref_dynamic = 0;
1477 }
1478 else
1479 h->ref_dynamic = 1;
1480 h->def_dynamic = 0;
1481 /* FIXME: Should we check type and size for protected symbol? */
1482 h->size = 0;
1483 h->type = 0;
1484 return TRUE;
1485 }
1486
1487 /* If a new weak symbol definition comes from a regular file and the
1488 old symbol comes from a dynamic library, we treat the new one as
1489 strong. Similarly, an old weak symbol definition from a regular
1490 file is treated as strong when the new symbol comes from a dynamic
1491 library. Further, an old weak symbol from a dynamic library is
1492 treated as strong if the new symbol is from a dynamic library.
1493 This reflects the way glibc's ld.so works.
1494
1495 Also allow a weak symbol to override a linker script symbol
1496 defined by an early pass over the script. This is done so the
1497 linker knows the symbol is defined in an object file, for the
1498 DEFINED script function.
1499
1500 Do this before setting *type_change_ok or *size_change_ok so that
1501 we warn properly when dynamic library symbols are overridden. */
1502
1503 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1504 newweak = FALSE;
1505 if (olddef && newdyn)
1506 oldweak = FALSE;
1507
1508 /* Allow changes between different types of function symbol. */
1509 if (newfunc && oldfunc)
1510 *type_change_ok = TRUE;
1511
1512 /* It's OK to change the type if either the existing symbol or the
1513 new symbol is weak. A type change is also OK if the old symbol
1514 is undefined and the new symbol is defined. */
1515
1516 if (oldweak
1517 || newweak
1518 || (newdef
1519 && h->root.type == bfd_link_hash_undefined))
1520 *type_change_ok = TRUE;
1521
1522 /* It's OK to change the size if either the existing symbol or the
1523 new symbol is weak, or if the old symbol is undefined. */
1524
1525 if (*type_change_ok
1526 || h->root.type == bfd_link_hash_undefined)
1527 *size_change_ok = TRUE;
1528
1529 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1530 symbol, respectively, appears to be a common symbol in a dynamic
1531 object. If a symbol appears in an uninitialized section, and is
1532 not weak, and is not a function, then it may be a common symbol
1533 which was resolved when the dynamic object was created. We want
1534 to treat such symbols specially, because they raise special
1535 considerations when setting the symbol size: if the symbol
1536 appears as a common symbol in a regular object, and the size in
1537 the regular object is larger, we must make sure that we use the
1538 larger size. This problematic case can always be avoided in C,
1539 but it must be handled correctly when using Fortran shared
1540 libraries.
1541
1542 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1543 likewise for OLDDYNCOMMON and OLDDEF.
1544
1545 Note that this test is just a heuristic, and that it is quite
1546 possible to have an uninitialized symbol in a shared object which
1547 is really a definition, rather than a common symbol. This could
1548 lead to some minor confusion when the symbol really is a common
1549 symbol in some regular object. However, I think it will be
1550 harmless. */
1551
1552 if (newdyn
1553 && newdef
1554 && !newweak
1555 && (sec->flags & SEC_ALLOC) != 0
1556 && (sec->flags & SEC_LOAD) == 0
1557 && sym->st_size > 0
1558 && !newfunc)
1559 newdyncommon = TRUE;
1560 else
1561 newdyncommon = FALSE;
1562
1563 if (olddyn
1564 && olddef
1565 && h->root.type == bfd_link_hash_defined
1566 && h->def_dynamic
1567 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1568 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1569 && h->size > 0
1570 && !oldfunc)
1571 olddyncommon = TRUE;
1572 else
1573 olddyncommon = FALSE;
1574
1575 /* We now know everything about the old and new symbols. We ask the
1576 backend to check if we can merge them. */
1577 if (bed->merge_symbol != NULL)
1578 {
1579 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1580 return FALSE;
1581 sec = *psec;
1582 }
1583
1584 /* There are multiple definitions of a normal symbol. Skip the
1585 default symbol as well as definition from an IR object. */
1586 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1587 && !default_sym && h->def_regular
1588 && !(oldbfd != NULL
1589 && (oldbfd->flags & BFD_PLUGIN) != 0
1590 && (abfd->flags & BFD_PLUGIN) == 0))
1591 {
1592 /* Handle a multiple definition. */
1593 (*info->callbacks->multiple_definition) (info, &h->root,
1594 abfd, sec, *pvalue);
1595 *skip = TRUE;
1596 return TRUE;
1597 }
1598
1599 /* If both the old and the new symbols look like common symbols in a
1600 dynamic object, set the size of the symbol to the larger of the
1601 two. */
1602
1603 if (olddyncommon
1604 && newdyncommon
1605 && sym->st_size != h->size)
1606 {
1607 /* Since we think we have two common symbols, issue a multiple
1608 common warning if desired. Note that we only warn if the
1609 size is different. If the size is the same, we simply let
1610 the old symbol override the new one as normally happens with
1611 symbols defined in dynamic objects. */
1612
1613 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1614 bfd_link_hash_common, sym->st_size);
1615 if (sym->st_size > h->size)
1616 h->size = sym->st_size;
1617
1618 *size_change_ok = TRUE;
1619 }
1620
1621 /* If we are looking at a dynamic object, and we have found a
1622 definition, we need to see if the symbol was already defined by
1623 some other object. If so, we want to use the existing
1624 definition, and we do not want to report a multiple symbol
1625 definition error; we do this by clobbering *PSEC to be
1626 bfd_und_section_ptr.
1627
1628 We treat a common symbol as a definition if the symbol in the
1629 shared library is a function, since common symbols always
1630 represent variables; this can cause confusion in principle, but
1631 any such confusion would seem to indicate an erroneous program or
1632 shared library. We also permit a common symbol in a regular
1633 object to override a weak symbol in a shared object. */
1634
1635 if (newdyn
1636 && newdef
1637 && (olddef
1638 || (h->root.type == bfd_link_hash_common
1639 && (newweak || newfunc))))
1640 {
1641 *override = TRUE;
1642 newdef = FALSE;
1643 newdyncommon = FALSE;
1644
1645 *psec = sec = bfd_und_section_ptr;
1646 *size_change_ok = TRUE;
1647
1648 /* If we get here when the old symbol is a common symbol, then
1649 we are explicitly letting it override a weak symbol or
1650 function in a dynamic object, and we don't want to warn about
1651 a type change. If the old symbol is a defined symbol, a type
1652 change warning may still be appropriate. */
1653
1654 if (h->root.type == bfd_link_hash_common)
1655 *type_change_ok = TRUE;
1656 }
1657
1658 /* Handle the special case of an old common symbol merging with a
1659 new symbol which looks like a common symbol in a shared object.
1660 We change *PSEC and *PVALUE to make the new symbol look like a
1661 common symbol, and let _bfd_generic_link_add_one_symbol do the
1662 right thing. */
1663
1664 if (newdyncommon
1665 && h->root.type == bfd_link_hash_common)
1666 {
1667 *override = TRUE;
1668 newdef = FALSE;
1669 newdyncommon = FALSE;
1670 *pvalue = sym->st_size;
1671 *psec = sec = bed->common_section (oldsec);
1672 *size_change_ok = TRUE;
1673 }
1674
1675 /* Skip weak definitions of symbols that are already defined. */
1676 if (newdef && olddef && newweak)
1677 {
1678 /* Don't skip new non-IR weak syms. */
1679 if (!(oldbfd != NULL
1680 && (oldbfd->flags & BFD_PLUGIN) != 0
1681 && (abfd->flags & BFD_PLUGIN) == 0))
1682 {
1683 newdef = FALSE;
1684 *skip = TRUE;
1685 }
1686
1687 /* Merge st_other. If the symbol already has a dynamic index,
1688 but visibility says it should not be visible, turn it into a
1689 local symbol. */
1690 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1691 if (h->dynindx != -1)
1692 switch (ELF_ST_VISIBILITY (h->other))
1693 {
1694 case STV_INTERNAL:
1695 case STV_HIDDEN:
1696 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1697 break;
1698 }
1699 }
1700
1701 /* If the old symbol is from a dynamic object, and the new symbol is
1702 a definition which is not from a dynamic object, then the new
1703 symbol overrides the old symbol. Symbols from regular files
1704 always take precedence over symbols from dynamic objects, even if
1705 they are defined after the dynamic object in the link.
1706
1707 As above, we again permit a common symbol in a regular object to
1708 override a definition in a shared object if the shared object
1709 symbol is a function or is weak. */
1710
1711 flip = NULL;
1712 if (!newdyn
1713 && (newdef
1714 || (bfd_is_com_section (sec)
1715 && (oldweak || oldfunc)))
1716 && olddyn
1717 && olddef
1718 && h->def_dynamic)
1719 {
1720 /* Change the hash table entry to undefined, and let
1721 _bfd_generic_link_add_one_symbol do the right thing with the
1722 new definition. */
1723
1724 h->root.type = bfd_link_hash_undefined;
1725 h->root.u.undef.abfd = h->root.u.def.section->owner;
1726 *size_change_ok = TRUE;
1727
1728 olddef = FALSE;
1729 olddyncommon = FALSE;
1730
1731 /* We again permit a type change when a common symbol may be
1732 overriding a function. */
1733
1734 if (bfd_is_com_section (sec))
1735 {
1736 if (oldfunc)
1737 {
1738 /* If a common symbol overrides a function, make sure
1739 that it isn't defined dynamically nor has type
1740 function. */
1741 h->def_dynamic = 0;
1742 h->type = STT_NOTYPE;
1743 }
1744 *type_change_ok = TRUE;
1745 }
1746
1747 if (hi->root.type == bfd_link_hash_indirect)
1748 flip = hi;
1749 else
1750 /* This union may have been set to be non-NULL when this symbol
1751 was seen in a dynamic object. We must force the union to be
1752 NULL, so that it is correct for a regular symbol. */
1753 h->verinfo.vertree = NULL;
1754 }
1755
1756 /* Handle the special case of a new common symbol merging with an
1757 old symbol that looks like it might be a common symbol defined in
1758 a shared object. Note that we have already handled the case in
1759 which a new common symbol should simply override the definition
1760 in the shared library. */
1761
1762 if (! newdyn
1763 && bfd_is_com_section (sec)
1764 && olddyncommon)
1765 {
1766 /* It would be best if we could set the hash table entry to a
1767 common symbol, but we don't know what to use for the section
1768 or the alignment. */
1769 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1770 bfd_link_hash_common, sym->st_size);
1771
1772 /* If the presumed common symbol in the dynamic object is
1773 larger, pretend that the new symbol has its size. */
1774
1775 if (h->size > *pvalue)
1776 *pvalue = h->size;
1777
1778 /* We need to remember the alignment required by the symbol
1779 in the dynamic object. */
1780 BFD_ASSERT (pold_alignment);
1781 *pold_alignment = h->root.u.def.section->alignment_power;
1782
1783 olddef = FALSE;
1784 olddyncommon = FALSE;
1785
1786 h->root.type = bfd_link_hash_undefined;
1787 h->root.u.undef.abfd = h->root.u.def.section->owner;
1788
1789 *size_change_ok = TRUE;
1790 *type_change_ok = TRUE;
1791
1792 if (hi->root.type == bfd_link_hash_indirect)
1793 flip = hi;
1794 else
1795 h->verinfo.vertree = NULL;
1796 }
1797
1798 if (flip != NULL)
1799 {
1800 /* Handle the case where we had a versioned symbol in a dynamic
1801 library and now find a definition in a normal object. In this
1802 case, we make the versioned symbol point to the normal one. */
1803 flip->root.type = h->root.type;
1804 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1805 h->root.type = bfd_link_hash_indirect;
1806 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1807 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1808 if (h->def_dynamic)
1809 {
1810 h->def_dynamic = 0;
1811 flip->ref_dynamic = 1;
1812 }
1813 }
1814
1815 return TRUE;
1816 }
1817
1818 /* This function is called to create an indirect symbol from the
1819 default for the symbol with the default version if needed. The
1820 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1821 set DYNSYM if the new indirect symbol is dynamic. */
1822
1823 static bfd_boolean
1824 _bfd_elf_add_default_symbol (bfd *abfd,
1825 struct bfd_link_info *info,
1826 struct elf_link_hash_entry *h,
1827 const char *name,
1828 Elf_Internal_Sym *sym,
1829 asection *sec,
1830 bfd_vma value,
1831 bfd **poldbfd,
1832 bfd_boolean *dynsym)
1833 {
1834 bfd_boolean type_change_ok;
1835 bfd_boolean size_change_ok;
1836 bfd_boolean skip;
1837 char *shortname;
1838 struct elf_link_hash_entry *hi;
1839 struct bfd_link_hash_entry *bh;
1840 const struct elf_backend_data *bed;
1841 bfd_boolean collect;
1842 bfd_boolean dynamic;
1843 bfd_boolean override;
1844 char *p;
1845 size_t len, shortlen;
1846 asection *tmp_sec;
1847 bfd_boolean matched;
1848
1849 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1850 return TRUE;
1851
1852 /* If this symbol has a version, and it is the default version, we
1853 create an indirect symbol from the default name to the fully
1854 decorated name. This will cause external references which do not
1855 specify a version to be bound to this version of the symbol. */
1856 p = strchr (name, ELF_VER_CHR);
1857 if (h->versioned == unknown)
1858 {
1859 if (p == NULL)
1860 {
1861 h->versioned = unversioned;
1862 return TRUE;
1863 }
1864 else
1865 {
1866 if (p[1] != ELF_VER_CHR)
1867 {
1868 h->versioned = versioned_hidden;
1869 return TRUE;
1870 }
1871 else
1872 h->versioned = versioned;
1873 }
1874 }
1875 else
1876 {
1877 /* PR ld/19073: We may see an unversioned definition after the
1878 default version. */
1879 if (p == NULL)
1880 return TRUE;
1881 }
1882
1883 bed = get_elf_backend_data (abfd);
1884 collect = bed->collect;
1885 dynamic = (abfd->flags & DYNAMIC) != 0;
1886
1887 shortlen = p - name;
1888 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1889 if (shortname == NULL)
1890 return FALSE;
1891 memcpy (shortname, name, shortlen);
1892 shortname[shortlen] = '\0';
1893
1894 /* We are going to create a new symbol. Merge it with any existing
1895 symbol with this name. For the purposes of the merge, act as
1896 though we were defining the symbol we just defined, although we
1897 actually going to define an indirect symbol. */
1898 type_change_ok = FALSE;
1899 size_change_ok = FALSE;
1900 matched = TRUE;
1901 tmp_sec = sec;
1902 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1903 &hi, poldbfd, NULL, NULL, &skip, &override,
1904 &type_change_ok, &size_change_ok, &matched))
1905 return FALSE;
1906
1907 if (skip)
1908 goto nondefault;
1909
1910 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1911 {
1912 /* If the undecorated symbol will have a version added by a
1913 script different to H, then don't indirect to/from the
1914 undecorated symbol. This isn't ideal because we may not yet
1915 have seen symbol versions, if given by a script on the
1916 command line rather than via --version-script. */
1917 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1918 {
1919 bfd_boolean hide;
1920
1921 hi->verinfo.vertree
1922 = bfd_find_version_for_sym (info->version_info,
1923 hi->root.root.string, &hide);
1924 if (hi->verinfo.vertree != NULL && hide)
1925 {
1926 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1927 goto nondefault;
1928 }
1929 }
1930 if (hi->verinfo.vertree != NULL
1931 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1932 goto nondefault;
1933 }
1934
1935 if (! override)
1936 {
1937 /* Add the default symbol if not performing a relocatable link. */
1938 if (! bfd_link_relocatable (info))
1939 {
1940 bh = &hi->root;
1941 if (bh->type == bfd_link_hash_defined
1942 && bh->u.def.section->owner != NULL
1943 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1944 {
1945 /* Mark the previous definition from IR object as
1946 undefined so that the generic linker will override
1947 it. */
1948 bh->type = bfd_link_hash_undefined;
1949 bh->u.undef.abfd = bh->u.def.section->owner;
1950 }
1951 if (! (_bfd_generic_link_add_one_symbol
1952 (info, abfd, shortname, BSF_INDIRECT,
1953 bfd_ind_section_ptr,
1954 0, name, FALSE, collect, &bh)))
1955 return FALSE;
1956 hi = (struct elf_link_hash_entry *) bh;
1957 }
1958 }
1959 else
1960 {
1961 /* In this case the symbol named SHORTNAME is overriding the
1962 indirect symbol we want to add. We were planning on making
1963 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1964 is the name without a version. NAME is the fully versioned
1965 name, and it is the default version.
1966
1967 Overriding means that we already saw a definition for the
1968 symbol SHORTNAME in a regular object, and it is overriding
1969 the symbol defined in the dynamic object.
1970
1971 When this happens, we actually want to change NAME, the
1972 symbol we just added, to refer to SHORTNAME. This will cause
1973 references to NAME in the shared object to become references
1974 to SHORTNAME in the regular object. This is what we expect
1975 when we override a function in a shared object: that the
1976 references in the shared object will be mapped to the
1977 definition in the regular object. */
1978
1979 while (hi->root.type == bfd_link_hash_indirect
1980 || hi->root.type == bfd_link_hash_warning)
1981 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1982
1983 h->root.type = bfd_link_hash_indirect;
1984 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1985 if (h->def_dynamic)
1986 {
1987 h->def_dynamic = 0;
1988 hi->ref_dynamic = 1;
1989 if (hi->ref_regular
1990 || hi->def_regular)
1991 {
1992 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1993 return FALSE;
1994 }
1995 }
1996
1997 /* Now set HI to H, so that the following code will set the
1998 other fields correctly. */
1999 hi = h;
2000 }
2001
2002 /* Check if HI is a warning symbol. */
2003 if (hi->root.type == bfd_link_hash_warning)
2004 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2005
2006 /* If there is a duplicate definition somewhere, then HI may not
2007 point to an indirect symbol. We will have reported an error to
2008 the user in that case. */
2009
2010 if (hi->root.type == bfd_link_hash_indirect)
2011 {
2012 struct elf_link_hash_entry *ht;
2013
2014 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2015 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2016
2017 /* A reference to the SHORTNAME symbol from a dynamic library
2018 will be satisfied by the versioned symbol at runtime. In
2019 effect, we have a reference to the versioned symbol. */
2020 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2021 hi->dynamic_def |= ht->dynamic_def;
2022
2023 /* See if the new flags lead us to realize that the symbol must
2024 be dynamic. */
2025 if (! *dynsym)
2026 {
2027 if (! dynamic)
2028 {
2029 if (! bfd_link_executable (info)
2030 || hi->def_dynamic
2031 || hi->ref_dynamic)
2032 *dynsym = TRUE;
2033 }
2034 else
2035 {
2036 if (hi->ref_regular)
2037 *dynsym = TRUE;
2038 }
2039 }
2040 }
2041
2042 /* We also need to define an indirection from the nondefault version
2043 of the symbol. */
2044
2045 nondefault:
2046 len = strlen (name);
2047 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2048 if (shortname == NULL)
2049 return FALSE;
2050 memcpy (shortname, name, shortlen);
2051 memcpy (shortname + shortlen, p + 1, len - shortlen);
2052
2053 /* Once again, merge with any existing symbol. */
2054 type_change_ok = FALSE;
2055 size_change_ok = FALSE;
2056 tmp_sec = sec;
2057 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2058 &hi, poldbfd, NULL, NULL, &skip, &override,
2059 &type_change_ok, &size_change_ok, &matched))
2060 return FALSE;
2061
2062 if (skip)
2063 return TRUE;
2064
2065 if (override)
2066 {
2067 /* Here SHORTNAME is a versioned name, so we don't expect to see
2068 the type of override we do in the case above unless it is
2069 overridden by a versioned definition. */
2070 if (hi->root.type != bfd_link_hash_defined
2071 && hi->root.type != bfd_link_hash_defweak)
2072 _bfd_error_handler
2073 /* xgettext:c-format */
2074 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2075 abfd, shortname);
2076 }
2077 else
2078 {
2079 bh = &hi->root;
2080 if (! (_bfd_generic_link_add_one_symbol
2081 (info, abfd, shortname, BSF_INDIRECT,
2082 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2083 return FALSE;
2084 hi = (struct elf_link_hash_entry *) bh;
2085
2086 /* If there is a duplicate definition somewhere, then HI may not
2087 point to an indirect symbol. We will have reported an error
2088 to the user in that case. */
2089
2090 if (hi->root.type == bfd_link_hash_indirect)
2091 {
2092 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2093 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2094 hi->dynamic_def |= h->dynamic_def;
2095
2096 /* See if the new flags lead us to realize that the symbol
2097 must be dynamic. */
2098 if (! *dynsym)
2099 {
2100 if (! dynamic)
2101 {
2102 if (! bfd_link_executable (info)
2103 || hi->ref_dynamic)
2104 *dynsym = TRUE;
2105 }
2106 else
2107 {
2108 if (hi->ref_regular)
2109 *dynsym = TRUE;
2110 }
2111 }
2112 }
2113 }
2114
2115 return TRUE;
2116 }
2117
2118 /* This routine is used to export all defined symbols into the dynamic
2120 symbol table. It is called via elf_link_hash_traverse. */
2121
2122 static bfd_boolean
2123 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2124 {
2125 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2126
2127 /* Ignore indirect symbols. These are added by the versioning code. */
2128 if (h->root.type == bfd_link_hash_indirect)
2129 return TRUE;
2130
2131 /* Ignore this if we won't export it. */
2132 if (!eif->info->export_dynamic && !h->dynamic)
2133 return TRUE;
2134
2135 if (h->dynindx == -1
2136 && (h->def_regular || h->ref_regular)
2137 && ! bfd_hide_sym_by_version (eif->info->version_info,
2138 h->root.root.string))
2139 {
2140 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2141 {
2142 eif->failed = TRUE;
2143 return FALSE;
2144 }
2145 }
2146
2147 return TRUE;
2148 }
2149
2150 /* Look through the symbols which are defined in other shared
2152 libraries and referenced here. Update the list of version
2153 dependencies. This will be put into the .gnu.version_r section.
2154 This function is called via elf_link_hash_traverse. */
2155
2156 static bfd_boolean
2157 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2158 void *data)
2159 {
2160 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2161 Elf_Internal_Verneed *t;
2162 Elf_Internal_Vernaux *a;
2163 bfd_size_type amt;
2164
2165 /* We only care about symbols defined in shared objects with version
2166 information. */
2167 if (!h->def_dynamic
2168 || h->def_regular
2169 || h->dynindx == -1
2170 || h->verinfo.verdef == NULL
2171 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2172 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2173 return TRUE;
2174
2175 /* See if we already know about this version. */
2176 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2177 t != NULL;
2178 t = t->vn_nextref)
2179 {
2180 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2181 continue;
2182
2183 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2184 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2185 return TRUE;
2186
2187 break;
2188 }
2189
2190 /* This is a new version. Add it to tree we are building. */
2191
2192 if (t == NULL)
2193 {
2194 amt = sizeof *t;
2195 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2196 if (t == NULL)
2197 {
2198 rinfo->failed = TRUE;
2199 return FALSE;
2200 }
2201
2202 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2203 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2204 elf_tdata (rinfo->info->output_bfd)->verref = t;
2205 }
2206
2207 amt = sizeof *a;
2208 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2209 if (a == NULL)
2210 {
2211 rinfo->failed = TRUE;
2212 return FALSE;
2213 }
2214
2215 /* Note that we are copying a string pointer here, and testing it
2216 above. If bfd_elf_string_from_elf_section is ever changed to
2217 discard the string data when low in memory, this will have to be
2218 fixed. */
2219 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2220
2221 a->vna_flags = h->verinfo.verdef->vd_flags;
2222 a->vna_nextptr = t->vn_auxptr;
2223
2224 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2225 ++rinfo->vers;
2226
2227 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2228
2229 t->vn_auxptr = a;
2230
2231 return TRUE;
2232 }
2233
2234 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2235 hidden. Set *T_P to NULL if there is no match. */
2236
2237 static bfd_boolean
2238 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2239 struct elf_link_hash_entry *h,
2240 const char *version_p,
2241 struct bfd_elf_version_tree **t_p,
2242 bfd_boolean *hide)
2243 {
2244 struct bfd_elf_version_tree *t;
2245
2246 /* Look for the version. If we find it, it is no longer weak. */
2247 for (t = info->version_info; t != NULL; t = t->next)
2248 {
2249 if (strcmp (t->name, version_p) == 0)
2250 {
2251 size_t len;
2252 char *alc;
2253 struct bfd_elf_version_expr *d;
2254
2255 len = version_p - h->root.root.string;
2256 alc = (char *) bfd_malloc (len);
2257 if (alc == NULL)
2258 return FALSE;
2259 memcpy (alc, h->root.root.string, len - 1);
2260 alc[len - 1] = '\0';
2261 if (alc[len - 2] == ELF_VER_CHR)
2262 alc[len - 2] = '\0';
2263
2264 h->verinfo.vertree = t;
2265 t->used = TRUE;
2266 d = NULL;
2267
2268 if (t->globals.list != NULL)
2269 d = (*t->match) (&t->globals, NULL, alc);
2270
2271 /* See if there is anything to force this symbol to
2272 local scope. */
2273 if (d == NULL && t->locals.list != NULL)
2274 {
2275 d = (*t->match) (&t->locals, NULL, alc);
2276 if (d != NULL
2277 && h->dynindx != -1
2278 && ! info->export_dynamic)
2279 *hide = TRUE;
2280 }
2281
2282 free (alc);
2283 break;
2284 }
2285 }
2286
2287 *t_p = t;
2288
2289 return TRUE;
2290 }
2291
2292 /* Return TRUE if the symbol H is hidden by version script. */
2293
2294 bfd_boolean
2295 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h)
2297 {
2298 const char *p;
2299 bfd_boolean hide = FALSE;
2300 const struct elf_backend_data *bed
2301 = get_elf_backend_data (info->output_bfd);
2302
2303 /* Version script only hides symbols defined in regular objects. */
2304 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2305 return TRUE;
2306
2307 p = strchr (h->root.root.string, ELF_VER_CHR);
2308 if (p != NULL && h->verinfo.vertree == NULL)
2309 {
2310 struct bfd_elf_version_tree *t;
2311
2312 ++p;
2313 if (*p == ELF_VER_CHR)
2314 ++p;
2315
2316 if (*p != '\0'
2317 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2318 && hide)
2319 {
2320 if (hide)
2321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2322 return TRUE;
2323 }
2324 }
2325
2326 /* If we don't have a version for this symbol, see if we can find
2327 something. */
2328 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2329 {
2330 h->verinfo.vertree
2331 = bfd_find_version_for_sym (info->version_info,
2332 h->root.root.string, &hide);
2333 if (h->verinfo.vertree != NULL && hide)
2334 {
2335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2336 return TRUE;
2337 }
2338 }
2339
2340 return FALSE;
2341 }
2342
2343 /* Figure out appropriate versions for all the symbols. We may not
2344 have the version number script until we have read all of the input
2345 files, so until that point we don't know which symbols should be
2346 local. This function is called via elf_link_hash_traverse. */
2347
2348 static bfd_boolean
2349 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2350 {
2351 struct elf_info_failed *sinfo;
2352 struct bfd_link_info *info;
2353 const struct elf_backend_data *bed;
2354 struct elf_info_failed eif;
2355 char *p;
2356 bfd_boolean hide;
2357
2358 sinfo = (struct elf_info_failed *) data;
2359 info = sinfo->info;
2360
2361 /* Fix the symbol flags. */
2362 eif.failed = FALSE;
2363 eif.info = info;
2364 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2365 {
2366 if (eif.failed)
2367 sinfo->failed = TRUE;
2368 return FALSE;
2369 }
2370
2371 bed = get_elf_backend_data (info->output_bfd);
2372
2373 /* We only need version numbers for symbols defined in regular
2374 objects. */
2375 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2376 {
2377 /* Hide symbols defined in discarded input sections. */
2378 if ((h->root.type == bfd_link_hash_defined
2379 || h->root.type == bfd_link_hash_defweak)
2380 && discarded_section (h->root.u.def.section))
2381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2382 return TRUE;
2383 }
2384
2385 hide = FALSE;
2386 p = strchr (h->root.root.string, ELF_VER_CHR);
2387 if (p != NULL && h->verinfo.vertree == NULL)
2388 {
2389 struct bfd_elf_version_tree *t;
2390
2391 ++p;
2392 if (*p == ELF_VER_CHR)
2393 ++p;
2394
2395 /* If there is no version string, we can just return out. */
2396 if (*p == '\0')
2397 return TRUE;
2398
2399 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2400 {
2401 sinfo->failed = TRUE;
2402 return FALSE;
2403 }
2404
2405 if (hide)
2406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2407
2408 /* If we are building an application, we need to create a
2409 version node for this version. */
2410 if (t == NULL && bfd_link_executable (info))
2411 {
2412 struct bfd_elf_version_tree **pp;
2413 int version_index;
2414
2415 /* If we aren't going to export this symbol, we don't need
2416 to worry about it. */
2417 if (h->dynindx == -1)
2418 return TRUE;
2419
2420 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2421 sizeof *t);
2422 if (t == NULL)
2423 {
2424 sinfo->failed = TRUE;
2425 return FALSE;
2426 }
2427
2428 t->name = p;
2429 t->name_indx = (unsigned int) -1;
2430 t->used = TRUE;
2431
2432 version_index = 1;
2433 /* Don't count anonymous version tag. */
2434 if (sinfo->info->version_info != NULL
2435 && sinfo->info->version_info->vernum == 0)
2436 version_index = 0;
2437 for (pp = &sinfo->info->version_info;
2438 *pp != NULL;
2439 pp = &(*pp)->next)
2440 ++version_index;
2441 t->vernum = version_index;
2442
2443 *pp = t;
2444
2445 h->verinfo.vertree = t;
2446 }
2447 else if (t == NULL)
2448 {
2449 /* We could not find the version for a symbol when
2450 generating a shared archive. Return an error. */
2451 _bfd_error_handler
2452 /* xgettext:c-format */
2453 (_("%pB: version node not found for symbol %s"),
2454 info->output_bfd, h->root.root.string);
2455 bfd_set_error (bfd_error_bad_value);
2456 sinfo->failed = TRUE;
2457 return FALSE;
2458 }
2459 }
2460
2461 /* If we don't have a version for this symbol, see if we can find
2462 something. */
2463 if (!hide
2464 && h->verinfo.vertree == NULL
2465 && sinfo->info->version_info != NULL)
2466 {
2467 h->verinfo.vertree
2468 = bfd_find_version_for_sym (sinfo->info->version_info,
2469 h->root.root.string, &hide);
2470 if (h->verinfo.vertree != NULL && hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2472 }
2473
2474 return TRUE;
2475 }
2476
2477 /* Read and swap the relocs from the section indicated by SHDR. This
2479 may be either a REL or a RELA section. The relocations are
2480 translated into RELA relocations and stored in INTERNAL_RELOCS,
2481 which should have already been allocated to contain enough space.
2482 The EXTERNAL_RELOCS are a buffer where the external form of the
2483 relocations should be stored.
2484
2485 Returns FALSE if something goes wrong. */
2486
2487 static bfd_boolean
2488 elf_link_read_relocs_from_section (bfd *abfd,
2489 asection *sec,
2490 Elf_Internal_Shdr *shdr,
2491 void *external_relocs,
2492 Elf_Internal_Rela *internal_relocs)
2493 {
2494 const struct elf_backend_data *bed;
2495 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2496 const bfd_byte *erela;
2497 const bfd_byte *erelaend;
2498 Elf_Internal_Rela *irela;
2499 Elf_Internal_Shdr *symtab_hdr;
2500 size_t nsyms;
2501
2502 /* Position ourselves at the start of the section. */
2503 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2504 return FALSE;
2505
2506 /* Read the relocations. */
2507 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2508 return FALSE;
2509
2510 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2511 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2512
2513 bed = get_elf_backend_data (abfd);
2514
2515 /* Convert the external relocations to the internal format. */
2516 if (shdr->sh_entsize == bed->s->sizeof_rel)
2517 swap_in = bed->s->swap_reloc_in;
2518 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2519 swap_in = bed->s->swap_reloca_in;
2520 else
2521 {
2522 bfd_set_error (bfd_error_wrong_format);
2523 return FALSE;
2524 }
2525
2526 erela = (const bfd_byte *) external_relocs;
2527 /* Setting erelaend like this and comparing with <= handles case of
2528 a fuzzed object with sh_size not a multiple of sh_entsize. */
2529 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2530 irela = internal_relocs;
2531 while (erela <= erelaend)
2532 {
2533 bfd_vma r_symndx;
2534
2535 (*swap_in) (abfd, erela, irela);
2536 r_symndx = ELF32_R_SYM (irela->r_info);
2537 if (bed->s->arch_size == 64)
2538 r_symndx >>= 24;
2539 if (nsyms > 0)
2540 {
2541 if ((size_t) r_symndx >= nsyms)
2542 {
2543 _bfd_error_handler
2544 /* xgettext:c-format */
2545 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2546 " for offset %#" PRIx64 " in section `%pA'"),
2547 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2548 (uint64_t) irela->r_offset, sec);
2549 bfd_set_error (bfd_error_bad_value);
2550 return FALSE;
2551 }
2552 }
2553 else if (r_symndx != STN_UNDEF)
2554 {
2555 _bfd_error_handler
2556 /* xgettext:c-format */
2557 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2558 " for offset %#" PRIx64 " in section `%pA'"
2559 " when the object file has no symbol table"),
2560 abfd, (uint64_t) r_symndx,
2561 (uint64_t) irela->r_offset, sec);
2562 bfd_set_error (bfd_error_bad_value);
2563 return FALSE;
2564 }
2565 irela += bed->s->int_rels_per_ext_rel;
2566 erela += shdr->sh_entsize;
2567 }
2568
2569 return TRUE;
2570 }
2571
2572 /* Read and swap the relocs for a section O. They may have been
2573 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2574 not NULL, they are used as buffers to read into. They are known to
2575 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2576 the return value is allocated using either malloc or bfd_alloc,
2577 according to the KEEP_MEMORY argument. If O has two relocation
2578 sections (both REL and RELA relocations), then the REL_HDR
2579 relocations will appear first in INTERNAL_RELOCS, followed by the
2580 RELA_HDR relocations. */
2581
2582 Elf_Internal_Rela *
2583 _bfd_elf_link_read_relocs (bfd *abfd,
2584 asection *o,
2585 void *external_relocs,
2586 Elf_Internal_Rela *internal_relocs,
2587 bfd_boolean keep_memory)
2588 {
2589 void *alloc1 = NULL;
2590 Elf_Internal_Rela *alloc2 = NULL;
2591 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2592 struct bfd_elf_section_data *esdo = elf_section_data (o);
2593 Elf_Internal_Rela *internal_rela_relocs;
2594
2595 if (esdo->relocs != NULL)
2596 return esdo->relocs;
2597
2598 if (o->reloc_count == 0)
2599 return NULL;
2600
2601 if (internal_relocs == NULL)
2602 {
2603 bfd_size_type size;
2604
2605 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2606 if (keep_memory)
2607 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2608 else
2609 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2610 if (internal_relocs == NULL)
2611 goto error_return;
2612 }
2613
2614 if (external_relocs == NULL)
2615 {
2616 bfd_size_type size = 0;
2617
2618 if (esdo->rel.hdr)
2619 size += esdo->rel.hdr->sh_size;
2620 if (esdo->rela.hdr)
2621 size += esdo->rela.hdr->sh_size;
2622
2623 alloc1 = bfd_malloc (size);
2624 if (alloc1 == NULL)
2625 goto error_return;
2626 external_relocs = alloc1;
2627 }
2628
2629 internal_rela_relocs = internal_relocs;
2630 if (esdo->rel.hdr)
2631 {
2632 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2633 external_relocs,
2634 internal_relocs))
2635 goto error_return;
2636 external_relocs = (((bfd_byte *) external_relocs)
2637 + esdo->rel.hdr->sh_size);
2638 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2639 * bed->s->int_rels_per_ext_rel);
2640 }
2641
2642 if (esdo->rela.hdr
2643 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2644 external_relocs,
2645 internal_rela_relocs)))
2646 goto error_return;
2647
2648 /* Cache the results for next time, if we can. */
2649 if (keep_memory)
2650 esdo->relocs = internal_relocs;
2651
2652 if (alloc1 != NULL)
2653 free (alloc1);
2654
2655 /* Don't free alloc2, since if it was allocated we are passing it
2656 back (under the name of internal_relocs). */
2657
2658 return internal_relocs;
2659
2660 error_return:
2661 if (alloc1 != NULL)
2662 free (alloc1);
2663 if (alloc2 != NULL)
2664 {
2665 if (keep_memory)
2666 bfd_release (abfd, alloc2);
2667 else
2668 free (alloc2);
2669 }
2670 return NULL;
2671 }
2672
2673 /* Compute the size of, and allocate space for, REL_HDR which is the
2674 section header for a section containing relocations for O. */
2675
2676 static bfd_boolean
2677 _bfd_elf_link_size_reloc_section (bfd *abfd,
2678 struct bfd_elf_section_reloc_data *reldata)
2679 {
2680 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2681
2682 /* That allows us to calculate the size of the section. */
2683 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2684
2685 /* The contents field must last into write_object_contents, so we
2686 allocate it with bfd_alloc rather than malloc. Also since we
2687 cannot be sure that the contents will actually be filled in,
2688 we zero the allocated space. */
2689 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2690 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2691 return FALSE;
2692
2693 if (reldata->hashes == NULL && reldata->count)
2694 {
2695 struct elf_link_hash_entry **p;
2696
2697 p = ((struct elf_link_hash_entry **)
2698 bfd_zmalloc (reldata->count * sizeof (*p)));
2699 if (p == NULL)
2700 return FALSE;
2701
2702 reldata->hashes = p;
2703 }
2704
2705 return TRUE;
2706 }
2707
2708 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2709 originated from the section given by INPUT_REL_HDR) to the
2710 OUTPUT_BFD. */
2711
2712 bfd_boolean
2713 _bfd_elf_link_output_relocs (bfd *output_bfd,
2714 asection *input_section,
2715 Elf_Internal_Shdr *input_rel_hdr,
2716 Elf_Internal_Rela *internal_relocs,
2717 struct elf_link_hash_entry **rel_hash
2718 ATTRIBUTE_UNUSED)
2719 {
2720 Elf_Internal_Rela *irela;
2721 Elf_Internal_Rela *irelaend;
2722 bfd_byte *erel;
2723 struct bfd_elf_section_reloc_data *output_reldata;
2724 asection *output_section;
2725 const struct elf_backend_data *bed;
2726 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2727 struct bfd_elf_section_data *esdo;
2728
2729 output_section = input_section->output_section;
2730
2731 bed = get_elf_backend_data (output_bfd);
2732 esdo = elf_section_data (output_section);
2733 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734 {
2735 output_reldata = &esdo->rel;
2736 swap_out = bed->s->swap_reloc_out;
2737 }
2738 else if (esdo->rela.hdr
2739 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2740 {
2741 output_reldata = &esdo->rela;
2742 swap_out = bed->s->swap_reloca_out;
2743 }
2744 else
2745 {
2746 _bfd_error_handler
2747 /* xgettext:c-format */
2748 (_("%pB: relocation size mismatch in %pB section %pA"),
2749 output_bfd, input_section->owner, input_section);
2750 bfd_set_error (bfd_error_wrong_format);
2751 return FALSE;
2752 }
2753
2754 erel = output_reldata->hdr->contents;
2755 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2756 irela = internal_relocs;
2757 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2758 * bed->s->int_rels_per_ext_rel);
2759 while (irela < irelaend)
2760 {
2761 (*swap_out) (output_bfd, irela, erel);
2762 irela += bed->s->int_rels_per_ext_rel;
2763 erel += input_rel_hdr->sh_entsize;
2764 }
2765
2766 /* Bump the counter, so that we know where to add the next set of
2767 relocations. */
2768 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2769
2770 return TRUE;
2771 }
2772
2773 /* Make weak undefined symbols in PIE dynamic. */
2775
2776 bfd_boolean
2777 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2778 struct elf_link_hash_entry *h)
2779 {
2780 if (bfd_link_pie (info)
2781 && h->dynindx == -1
2782 && h->root.type == bfd_link_hash_undefweak)
2783 return bfd_elf_link_record_dynamic_symbol (info, h);
2784
2785 return TRUE;
2786 }
2787
2788 /* Fix up the flags for a symbol. This handles various cases which
2789 can only be fixed after all the input files are seen. This is
2790 currently called by both adjust_dynamic_symbol and
2791 assign_sym_version, which is unnecessary but perhaps more robust in
2792 the face of future changes. */
2793
2794 static bfd_boolean
2795 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2796 struct elf_info_failed *eif)
2797 {
2798 const struct elf_backend_data *bed;
2799
2800 /* If this symbol was mentioned in a non-ELF file, try to set
2801 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2802 permit a non-ELF file to correctly refer to a symbol defined in
2803 an ELF dynamic object. */
2804 if (h->non_elf)
2805 {
2806 while (h->root.type == bfd_link_hash_indirect)
2807 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2808
2809 if (h->root.type != bfd_link_hash_defined
2810 && h->root.type != bfd_link_hash_defweak)
2811 {
2812 h->ref_regular = 1;
2813 h->ref_regular_nonweak = 1;
2814 }
2815 else
2816 {
2817 if (h->root.u.def.section->owner != NULL
2818 && (bfd_get_flavour (h->root.u.def.section->owner)
2819 == bfd_target_elf_flavour))
2820 {
2821 h->ref_regular = 1;
2822 h->ref_regular_nonweak = 1;
2823 }
2824 else
2825 h->def_regular = 1;
2826 }
2827
2828 if (h->dynindx == -1
2829 && (h->def_dynamic
2830 || h->ref_dynamic))
2831 {
2832 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2833 {
2834 eif->failed = TRUE;
2835 return FALSE;
2836 }
2837 }
2838 }
2839 else
2840 {
2841 /* Unfortunately, NON_ELF is only correct if the symbol
2842 was first seen in a non-ELF file. Fortunately, if the symbol
2843 was first seen in an ELF file, we're probably OK unless the
2844 symbol was defined in a non-ELF file. Catch that case here.
2845 FIXME: We're still in trouble if the symbol was first seen in
2846 a dynamic object, and then later in a non-ELF regular object. */
2847 if ((h->root.type == bfd_link_hash_defined
2848 || h->root.type == bfd_link_hash_defweak)
2849 && !h->def_regular
2850 && (h->root.u.def.section->owner != NULL
2851 ? (bfd_get_flavour (h->root.u.def.section->owner)
2852 != bfd_target_elf_flavour)
2853 : (bfd_is_abs_section (h->root.u.def.section)
2854 && !h->def_dynamic)))
2855 h->def_regular = 1;
2856 }
2857
2858 /* Backend specific symbol fixup. */
2859 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2860 if (bed->elf_backend_fixup_symbol
2861 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2862 return FALSE;
2863
2864 /* If this is a final link, and the symbol was defined as a common
2865 symbol in a regular object file, and there was no definition in
2866 any dynamic object, then the linker will have allocated space for
2867 the symbol in a common section but the DEF_REGULAR
2868 flag will not have been set. */
2869 if (h->root.type == bfd_link_hash_defined
2870 && !h->def_regular
2871 && h->ref_regular
2872 && !h->def_dynamic
2873 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2874 h->def_regular = 1;
2875
2876 /* Symbols defined in discarded sections shouldn't be dynamic. */
2877 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2878 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2879
2880 /* If a weak undefined symbol has non-default visibility, we also
2881 hide it from the dynamic linker. */
2882 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2883 && h->root.type == bfd_link_hash_undefweak)
2884 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2885
2886 /* A hidden versioned symbol in executable should be forced local if
2887 it is is locally defined, not referenced by shared library and not
2888 exported. */
2889 else if (bfd_link_executable (eif->info)
2890 && h->versioned == versioned_hidden
2891 && !eif->info->export_dynamic
2892 && !h->dynamic
2893 && !h->ref_dynamic
2894 && h->def_regular)
2895 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2896
2897 /* If -Bsymbolic was used (which means to bind references to global
2898 symbols to the definition within the shared object), and this
2899 symbol was defined in a regular object, then it actually doesn't
2900 need a PLT entry. Likewise, if the symbol has non-default
2901 visibility. If the symbol has hidden or internal visibility, we
2902 will force it local. */
2903 else if (h->needs_plt
2904 && bfd_link_pic (eif->info)
2905 && is_elf_hash_table (eif->info->hash)
2906 && (SYMBOLIC_BIND (eif->info, h)
2907 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2908 && h->def_regular)
2909 {
2910 bfd_boolean force_local;
2911
2912 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2913 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2914 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2915 }
2916
2917 /* If this is a weak defined symbol in a dynamic object, and we know
2918 the real definition in the dynamic object, copy interesting flags
2919 over to the real definition. */
2920 if (h->is_weakalias)
2921 {
2922 struct elf_link_hash_entry *def = weakdef (h);
2923 while (def->root.type == bfd_link_hash_indirect)
2924 def = (struct elf_link_hash_entry *) def->root.u.i.link;
2925
2926 /* If the real definition is defined by a regular object file,
2927 don't do anything special. See the longer description in
2928 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2929 bfd_link_hash_defined as it was when put on the alias list
2930 then it must have originally been a versioned symbol (for
2931 which a non-versioned indirect symbol is created) and later
2932 a definition for the non-versioned symbol is found. In that
2933 case the indirection is flipped with the versioned symbol
2934 becoming an indirect pointing at the non-versioned symbol.
2935 Thus, not an alias any more. */
2936 if (def->def_regular
2937 || def->root.type != bfd_link_hash_defined)
2938 {
2939 h = def;
2940 while ((h = h->u.alias) != def)
2941 h->is_weakalias = 0;
2942 }
2943 else
2944 {
2945 while (h->root.type == bfd_link_hash_indirect)
2946 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2947 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2948 || h->root.type == bfd_link_hash_defweak);
2949 BFD_ASSERT (def->def_dynamic);
2950 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2951 }
2952 }
2953
2954 return TRUE;
2955 }
2956
2957 /* Make the backend pick a good value for a dynamic symbol. This is
2958 called via elf_link_hash_traverse, and also calls itself
2959 recursively. */
2960
2961 static bfd_boolean
2962 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2963 {
2964 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2965 struct elf_link_hash_table *htab;
2966 const struct elf_backend_data *bed;
2967
2968 if (! is_elf_hash_table (eif->info->hash))
2969 return FALSE;
2970
2971 /* Ignore indirect symbols. These are added by the versioning code. */
2972 if (h->root.type == bfd_link_hash_indirect)
2973 return TRUE;
2974
2975 /* Fix the symbol flags. */
2976 if (! _bfd_elf_fix_symbol_flags (h, eif))
2977 return FALSE;
2978
2979 htab = elf_hash_table (eif->info);
2980 bed = get_elf_backend_data (htab->dynobj);
2981
2982 if (h->root.type == bfd_link_hash_undefweak)
2983 {
2984 if (eif->info->dynamic_undefined_weak == 0)
2985 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2986 else if (eif->info->dynamic_undefined_weak > 0
2987 && h->ref_regular
2988 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2989 && !bfd_hide_sym_by_version (eif->info->version_info,
2990 h->root.root.string))
2991 {
2992 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2993 {
2994 eif->failed = TRUE;
2995 return FALSE;
2996 }
2997 }
2998 }
2999
3000 /* If this symbol does not require a PLT entry, and it is not
3001 defined by a dynamic object, or is not referenced by a regular
3002 object, ignore it. We do have to handle a weak defined symbol,
3003 even if no regular object refers to it, if we decided to add it
3004 to the dynamic symbol table. FIXME: Do we normally need to worry
3005 about symbols which are defined by one dynamic object and
3006 referenced by another one? */
3007 if (!h->needs_plt
3008 && h->type != STT_GNU_IFUNC
3009 && (h->def_regular
3010 || !h->def_dynamic
3011 || (!h->ref_regular
3012 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3013 {
3014 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3015 return TRUE;
3016 }
3017
3018 /* If we've already adjusted this symbol, don't do it again. This
3019 can happen via a recursive call. */
3020 if (h->dynamic_adjusted)
3021 return TRUE;
3022
3023 /* Don't look at this symbol again. Note that we must set this
3024 after checking the above conditions, because we may look at a
3025 symbol once, decide not to do anything, and then get called
3026 recursively later after REF_REGULAR is set below. */
3027 h->dynamic_adjusted = 1;
3028
3029 /* If this is a weak definition, and we know a real definition, and
3030 the real symbol is not itself defined by a regular object file,
3031 then get a good value for the real definition. We handle the
3032 real symbol first, for the convenience of the backend routine.
3033
3034 Note that there is a confusing case here. If the real definition
3035 is defined by a regular object file, we don't get the real symbol
3036 from the dynamic object, but we do get the weak symbol. If the
3037 processor backend uses a COPY reloc, then if some routine in the
3038 dynamic object changes the real symbol, we will not see that
3039 change in the corresponding weak symbol. This is the way other
3040 ELF linkers work as well, and seems to be a result of the shared
3041 library model.
3042
3043 I will clarify this issue. Most SVR4 shared libraries define the
3044 variable _timezone and define timezone as a weak synonym. The
3045 tzset call changes _timezone. If you write
3046 extern int timezone;
3047 int _timezone = 5;
3048 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3049 you might expect that, since timezone is a synonym for _timezone,
3050 the same number will print both times. However, if the processor
3051 backend uses a COPY reloc, then actually timezone will be copied
3052 into your process image, and, since you define _timezone
3053 yourself, _timezone will not. Thus timezone and _timezone will
3054 wind up at different memory locations. The tzset call will set
3055 _timezone, leaving timezone unchanged. */
3056
3057 if (h->is_weakalias)
3058 {
3059 struct elf_link_hash_entry *def = weakdef (h);
3060
3061 /* If we get to this point, there is an implicit reference to
3062 the alias by a regular object file via the weak symbol H. */
3063 def->ref_regular = 1;
3064
3065 /* Ensure that the backend adjust_dynamic_symbol function sees
3066 the strong alias before H by recursively calling ourselves. */
3067 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3068 return FALSE;
3069 }
3070
3071 /* If a symbol has no type and no size and does not require a PLT
3072 entry, then we are probably about to do the wrong thing here: we
3073 are probably going to create a COPY reloc for an empty object.
3074 This case can arise when a shared object is built with assembly
3075 code, and the assembly code fails to set the symbol type. */
3076 if (h->size == 0
3077 && h->type == STT_NOTYPE
3078 && !h->needs_plt)
3079 _bfd_error_handler
3080 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3081 h->root.root.string);
3082
3083 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3084 {
3085 eif->failed = TRUE;
3086 return FALSE;
3087 }
3088
3089 return TRUE;
3090 }
3091
3092 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3093 DYNBSS. */
3094
3095 bfd_boolean
3096 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3097 struct elf_link_hash_entry *h,
3098 asection *dynbss)
3099 {
3100 unsigned int power_of_two;
3101 bfd_vma mask;
3102 asection *sec = h->root.u.def.section;
3103
3104 /* The section alignment of the definition is the maximum alignment
3105 requirement of symbols defined in the section. Since we don't
3106 know the symbol alignment requirement, we start with the
3107 maximum alignment and check low bits of the symbol address
3108 for the minimum alignment. */
3109 power_of_two = bfd_section_alignment (sec);
3110 mask = ((bfd_vma) 1 << power_of_two) - 1;
3111 while ((h->root.u.def.value & mask) != 0)
3112 {
3113 mask >>= 1;
3114 --power_of_two;
3115 }
3116
3117 if (power_of_two > bfd_section_alignment (dynbss))
3118 {
3119 /* Adjust the section alignment if needed. */
3120 if (!bfd_set_section_alignment (dynbss, power_of_two))
3121 return FALSE;
3122 }
3123
3124 /* We make sure that the symbol will be aligned properly. */
3125 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3126
3127 /* Define the symbol as being at this point in DYNBSS. */
3128 h->root.u.def.section = dynbss;
3129 h->root.u.def.value = dynbss->size;
3130
3131 /* Increment the size of DYNBSS to make room for the symbol. */
3132 dynbss->size += h->size;
3133
3134 /* No error if extern_protected_data is true. */
3135 if (h->protected_def
3136 && (!info->extern_protected_data
3137 || (info->extern_protected_data < 0
3138 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3139 info->callbacks->einfo
3140 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3141 h->root.root.string);
3142
3143 return TRUE;
3144 }
3145
3146 /* Adjust all external symbols pointing into SEC_MERGE sections
3147 to reflect the object merging within the sections. */
3148
3149 static bfd_boolean
3150 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3151 {
3152 asection *sec;
3153
3154 if ((h->root.type == bfd_link_hash_defined
3155 || h->root.type == bfd_link_hash_defweak)
3156 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3157 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3158 {
3159 bfd *output_bfd = (bfd *) data;
3160
3161 h->root.u.def.value =
3162 _bfd_merged_section_offset (output_bfd,
3163 &h->root.u.def.section,
3164 elf_section_data (sec)->sec_info,
3165 h->root.u.def.value);
3166 }
3167
3168 return TRUE;
3169 }
3170
3171 /* Returns false if the symbol referred to by H should be considered
3172 to resolve local to the current module, and true if it should be
3173 considered to bind dynamically. */
3174
3175 bfd_boolean
3176 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3177 struct bfd_link_info *info,
3178 bfd_boolean not_local_protected)
3179 {
3180 bfd_boolean binding_stays_local_p;
3181 const struct elf_backend_data *bed;
3182 struct elf_link_hash_table *hash_table;
3183
3184 if (h == NULL)
3185 return FALSE;
3186
3187 while (h->root.type == bfd_link_hash_indirect
3188 || h->root.type == bfd_link_hash_warning)
3189 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3190
3191 /* If it was forced local, then clearly it's not dynamic. */
3192 if (h->dynindx == -1)
3193 return FALSE;
3194 if (h->forced_local)
3195 return FALSE;
3196
3197 /* Identify the cases where name binding rules say that a
3198 visible symbol resolves locally. */
3199 binding_stays_local_p = (bfd_link_executable (info)
3200 || SYMBOLIC_BIND (info, h));
3201
3202 switch (ELF_ST_VISIBILITY (h->other))
3203 {
3204 case STV_INTERNAL:
3205 case STV_HIDDEN:
3206 return FALSE;
3207
3208 case STV_PROTECTED:
3209 hash_table = elf_hash_table (info);
3210 if (!is_elf_hash_table (hash_table))
3211 return FALSE;
3212
3213 bed = get_elf_backend_data (hash_table->dynobj);
3214
3215 /* Proper resolution for function pointer equality may require
3216 that these symbols perhaps be resolved dynamically, even though
3217 we should be resolving them to the current module. */
3218 if (!not_local_protected || !bed->is_function_type (h->type))
3219 binding_stays_local_p = TRUE;
3220 break;
3221
3222 default:
3223 break;
3224 }
3225
3226 /* If it isn't defined locally, then clearly it's dynamic. */
3227 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3228 return TRUE;
3229
3230 /* Otherwise, the symbol is dynamic if binding rules don't tell
3231 us that it remains local. */
3232 return !binding_stays_local_p;
3233 }
3234
3235 /* Return true if the symbol referred to by H should be considered
3236 to resolve local to the current module, and false otherwise. Differs
3237 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3238 undefined symbols. The two functions are virtually identical except
3239 for the place where dynindx == -1 is tested. If that test is true,
3240 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3241 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3242 defined symbols.
3243 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3244 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3245 treatment of undefined weak symbols. For those that do not make
3246 undefined weak symbols dynamic, both functions may return false. */
3247
3248 bfd_boolean
3249 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3250 struct bfd_link_info *info,
3251 bfd_boolean local_protected)
3252 {
3253 const struct elf_backend_data *bed;
3254 struct elf_link_hash_table *hash_table;
3255
3256 /* If it's a local sym, of course we resolve locally. */
3257 if (h == NULL)
3258 return TRUE;
3259
3260 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3261 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3262 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3263 return TRUE;
3264
3265 /* Forced local symbols resolve locally. */
3266 if (h->forced_local)
3267 return TRUE;
3268
3269 /* Common symbols that become definitions don't get the DEF_REGULAR
3270 flag set, so test it first, and don't bail out. */
3271 if (ELF_COMMON_DEF_P (h))
3272 /* Do nothing. */;
3273 /* If we don't have a definition in a regular file, then we can't
3274 resolve locally. The sym is either undefined or dynamic. */
3275 else if (!h->def_regular)
3276 return FALSE;
3277
3278 /* Non-dynamic symbols resolve locally. */
3279 if (h->dynindx == -1)
3280 return TRUE;
3281
3282 /* At this point, we know the symbol is defined and dynamic. In an
3283 executable it must resolve locally, likewise when building symbolic
3284 shared libraries. */
3285 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3286 return TRUE;
3287
3288 /* Now deal with defined dynamic symbols in shared libraries. Ones
3289 with default visibility might not resolve locally. */
3290 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3291 return FALSE;
3292
3293 hash_table = elf_hash_table (info);
3294 if (!is_elf_hash_table (hash_table))
3295 return TRUE;
3296
3297 bed = get_elf_backend_data (hash_table->dynobj);
3298
3299 /* If extern_protected_data is false, STV_PROTECTED non-function
3300 symbols are local. */
3301 if ((!info->extern_protected_data
3302 || (info->extern_protected_data < 0
3303 && !bed->extern_protected_data))
3304 && !bed->is_function_type (h->type))
3305 return TRUE;
3306
3307 /* Function pointer equality tests may require that STV_PROTECTED
3308 symbols be treated as dynamic symbols. If the address of a
3309 function not defined in an executable is set to that function's
3310 plt entry in the executable, then the address of the function in
3311 a shared library must also be the plt entry in the executable. */
3312 return local_protected;
3313 }
3314
3315 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3316 aligned. Returns the first TLS output section. */
3317
3318 struct bfd_section *
3319 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3320 {
3321 struct bfd_section *sec, *tls;
3322 unsigned int align = 0;
3323
3324 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3325 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3326 break;
3327 tls = sec;
3328
3329 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3330 if (sec->alignment_power > align)
3331 align = sec->alignment_power;
3332
3333 elf_hash_table (info)->tls_sec = tls;
3334
3335 /* Ensure the alignment of the first section is the largest alignment,
3336 so that the tls segment starts aligned. */
3337 if (tls != NULL)
3338 tls->alignment_power = align;
3339
3340 return tls;
3341 }
3342
3343 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3344 static bfd_boolean
3345 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3346 Elf_Internal_Sym *sym)
3347 {
3348 const struct elf_backend_data *bed;
3349
3350 /* Local symbols do not count, but target specific ones might. */
3351 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3352 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3353 return FALSE;
3354
3355 bed = get_elf_backend_data (abfd);
3356 /* Function symbols do not count. */
3357 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3358 return FALSE;
3359
3360 /* If the section is undefined, then so is the symbol. */
3361 if (sym->st_shndx == SHN_UNDEF)
3362 return FALSE;
3363
3364 /* If the symbol is defined in the common section, then
3365 it is a common definition and so does not count. */
3366 if (bed->common_definition (sym))
3367 return FALSE;
3368
3369 /* If the symbol is in a target specific section then we
3370 must rely upon the backend to tell us what it is. */
3371 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3372 /* FIXME - this function is not coded yet:
3373
3374 return _bfd_is_global_symbol_definition (abfd, sym);
3375
3376 Instead for now assume that the definition is not global,
3377 Even if this is wrong, at least the linker will behave
3378 in the same way that it used to do. */
3379 return FALSE;
3380
3381 return TRUE;
3382 }
3383
3384 /* Search the symbol table of the archive element of the archive ABFD
3385 whose archive map contains a mention of SYMDEF, and determine if
3386 the symbol is defined in this element. */
3387 static bfd_boolean
3388 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3389 {
3390 Elf_Internal_Shdr * hdr;
3391 size_t symcount;
3392 size_t extsymcount;
3393 size_t extsymoff;
3394 Elf_Internal_Sym *isymbuf;
3395 Elf_Internal_Sym *isym;
3396 Elf_Internal_Sym *isymend;
3397 bfd_boolean result;
3398
3399 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3400 if (abfd == NULL)
3401 return FALSE;
3402
3403 if (! bfd_check_format (abfd, bfd_object))
3404 return FALSE;
3405
3406 /* Select the appropriate symbol table. If we don't know if the
3407 object file is an IR object, give linker LTO plugin a chance to
3408 get the correct symbol table. */
3409 if (abfd->plugin_format == bfd_plugin_yes
3410 #if BFD_SUPPORTS_PLUGINS
3411 || (abfd->plugin_format == bfd_plugin_unknown
3412 && bfd_link_plugin_object_p (abfd))
3413 #endif
3414 )
3415 {
3416 /* Use the IR symbol table if the object has been claimed by
3417 plugin. */
3418 abfd = abfd->plugin_dummy_bfd;
3419 hdr = &elf_tdata (abfd)->symtab_hdr;
3420 }
3421 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3422 hdr = &elf_tdata (abfd)->symtab_hdr;
3423 else
3424 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3425
3426 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3427
3428 /* The sh_info field of the symtab header tells us where the
3429 external symbols start. We don't care about the local symbols. */
3430 if (elf_bad_symtab (abfd))
3431 {
3432 extsymcount = symcount;
3433 extsymoff = 0;
3434 }
3435 else
3436 {
3437 extsymcount = symcount - hdr->sh_info;
3438 extsymoff = hdr->sh_info;
3439 }
3440
3441 if (extsymcount == 0)
3442 return FALSE;
3443
3444 /* Read in the symbol table. */
3445 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3446 NULL, NULL, NULL);
3447 if (isymbuf == NULL)
3448 return FALSE;
3449
3450 /* Scan the symbol table looking for SYMDEF. */
3451 result = FALSE;
3452 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3453 {
3454 const char *name;
3455
3456 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3457 isym->st_name);
3458 if (name == NULL)
3459 break;
3460
3461 if (strcmp (name, symdef->name) == 0)
3462 {
3463 result = is_global_data_symbol_definition (abfd, isym);
3464 break;
3465 }
3466 }
3467
3468 free (isymbuf);
3469
3470 return result;
3471 }
3472
3473 /* Add an entry to the .dynamic table. */
3475
3476 bfd_boolean
3477 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3478 bfd_vma tag,
3479 bfd_vma val)
3480 {
3481 struct elf_link_hash_table *hash_table;
3482 const struct elf_backend_data *bed;
3483 asection *s;
3484 bfd_size_type newsize;
3485 bfd_byte *newcontents;
3486 Elf_Internal_Dyn dyn;
3487
3488 hash_table = elf_hash_table (info);
3489 if (! is_elf_hash_table (hash_table))
3490 return FALSE;
3491
3492 if (tag == DT_RELA || tag == DT_REL)
3493 hash_table->dynamic_relocs = TRUE;
3494
3495 bed = get_elf_backend_data (hash_table->dynobj);
3496 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3497 BFD_ASSERT (s != NULL);
3498
3499 newsize = s->size + bed->s->sizeof_dyn;
3500 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3501 if (newcontents == NULL)
3502 return FALSE;
3503
3504 dyn.d_tag = tag;
3505 dyn.d_un.d_val = val;
3506 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3507
3508 s->size = newsize;
3509 s->contents = newcontents;
3510
3511 return TRUE;
3512 }
3513
3514 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3515 otherwise just check whether one already exists. Returns -1 on error,
3516 1 if a DT_NEEDED tag already exists, and 0 on success. */
3517
3518 static int
3519 elf_add_dt_needed_tag (bfd *abfd,
3520 struct bfd_link_info *info,
3521 const char *soname,
3522 bfd_boolean do_it)
3523 {
3524 struct elf_link_hash_table *hash_table;
3525 size_t strindex;
3526
3527 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3528 return -1;
3529
3530 hash_table = elf_hash_table (info);
3531 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3532 if (strindex == (size_t) -1)
3533 return -1;
3534
3535 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3536 {
3537 asection *sdyn;
3538 const struct elf_backend_data *bed;
3539 bfd_byte *extdyn;
3540
3541 bed = get_elf_backend_data (hash_table->dynobj);
3542 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3543 if (sdyn != NULL)
3544 for (extdyn = sdyn->contents;
3545 extdyn < sdyn->contents + sdyn->size;
3546 extdyn += bed->s->sizeof_dyn)
3547 {
3548 Elf_Internal_Dyn dyn;
3549
3550 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3551 if (dyn.d_tag == DT_NEEDED
3552 && dyn.d_un.d_val == strindex)
3553 {
3554 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3555 return 1;
3556 }
3557 }
3558 }
3559
3560 if (do_it)
3561 {
3562 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3563 return -1;
3564
3565 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3566 return -1;
3567 }
3568 else
3569 /* We were just checking for existence of the tag. */
3570 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3571
3572 return 0;
3573 }
3574
3575 /* Return true if SONAME is on the needed list between NEEDED and STOP
3576 (or the end of list if STOP is NULL), and needed by a library that
3577 will be loaded. */
3578
3579 static bfd_boolean
3580 on_needed_list (const char *soname,
3581 struct bfd_link_needed_list *needed,
3582 struct bfd_link_needed_list *stop)
3583 {
3584 struct bfd_link_needed_list *look;
3585 for (look = needed; look != stop; look = look->next)
3586 if (strcmp (soname, look->name) == 0
3587 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3588 /* If needed by a library that itself is not directly
3589 needed, recursively check whether that library is
3590 indirectly needed. Since we add DT_NEEDED entries to
3591 the end of the list, library dependencies appear after
3592 the library. Therefore search prior to the current
3593 LOOK, preventing possible infinite recursion. */
3594 || on_needed_list (elf_dt_name (look->by), needed, look)))
3595 return TRUE;
3596
3597 return FALSE;
3598 }
3599
3600 /* Sort symbol by value, section, size, and type. */
3601 static int
3602 elf_sort_symbol (const void *arg1, const void *arg2)
3603 {
3604 const struct elf_link_hash_entry *h1;
3605 const struct elf_link_hash_entry *h2;
3606 bfd_signed_vma vdiff;
3607 int sdiff;
3608 const char *n1;
3609 const char *n2;
3610
3611 h1 = *(const struct elf_link_hash_entry **) arg1;
3612 h2 = *(const struct elf_link_hash_entry **) arg2;
3613 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3614 if (vdiff != 0)
3615 return vdiff > 0 ? 1 : -1;
3616
3617 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3618 if (sdiff != 0)
3619 return sdiff;
3620
3621 /* Sort so that sized symbols are selected over zero size symbols. */
3622 vdiff = h1->size - h2->size;
3623 if (vdiff != 0)
3624 return vdiff > 0 ? 1 : -1;
3625
3626 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3627 if (h1->type != h2->type)
3628 return h1->type - h2->type;
3629
3630 /* If symbols are properly sized and typed, and multiple strong
3631 aliases are not defined in a shared library by the user we
3632 shouldn't get here. Unfortunately linker script symbols like
3633 __bss_start sometimes match a user symbol defined at the start of
3634 .bss without proper size and type. We'd like to preference the
3635 user symbol over reserved system symbols. Sort on leading
3636 underscores. */
3637 n1 = h1->root.root.string;
3638 n2 = h2->root.root.string;
3639 while (*n1 == *n2)
3640 {
3641 if (*n1 == 0)
3642 break;
3643 ++n1;
3644 ++n2;
3645 }
3646 if (*n1 == '_')
3647 return -1;
3648 if (*n2 == '_')
3649 return 1;
3650
3651 /* Final sort on name selects user symbols like '_u' over reserved
3652 system symbols like '_Z' and also will avoid qsort instability. */
3653 return *n1 - *n2;
3654 }
3655
3656 /* This function is used to adjust offsets into .dynstr for
3657 dynamic symbols. This is called via elf_link_hash_traverse. */
3658
3659 static bfd_boolean
3660 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3661 {
3662 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3663
3664 if (h->dynindx != -1)
3665 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3666 return TRUE;
3667 }
3668
3669 /* Assign string offsets in .dynstr, update all structures referencing
3670 them. */
3671
3672 static bfd_boolean
3673 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3674 {
3675 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3676 struct elf_link_local_dynamic_entry *entry;
3677 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3678 bfd *dynobj = hash_table->dynobj;
3679 asection *sdyn;
3680 bfd_size_type size;
3681 const struct elf_backend_data *bed;
3682 bfd_byte *extdyn;
3683
3684 _bfd_elf_strtab_finalize (dynstr);
3685 size = _bfd_elf_strtab_size (dynstr);
3686
3687 bed = get_elf_backend_data (dynobj);
3688 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3689 BFD_ASSERT (sdyn != NULL);
3690
3691 /* Update all .dynamic entries referencing .dynstr strings. */
3692 for (extdyn = sdyn->contents;
3693 extdyn < sdyn->contents + sdyn->size;
3694 extdyn += bed->s->sizeof_dyn)
3695 {
3696 Elf_Internal_Dyn dyn;
3697
3698 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3699 switch (dyn.d_tag)
3700 {
3701 case DT_STRSZ:
3702 dyn.d_un.d_val = size;
3703 break;
3704 case DT_NEEDED:
3705 case DT_SONAME:
3706 case DT_RPATH:
3707 case DT_RUNPATH:
3708 case DT_FILTER:
3709 case DT_AUXILIARY:
3710 case DT_AUDIT:
3711 case DT_DEPAUDIT:
3712 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3713 break;
3714 default:
3715 continue;
3716 }
3717 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3718 }
3719
3720 /* Now update local dynamic symbols. */
3721 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3722 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3723 entry->isym.st_name);
3724
3725 /* And the rest of dynamic symbols. */
3726 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3727
3728 /* Adjust version definitions. */
3729 if (elf_tdata (output_bfd)->cverdefs)
3730 {
3731 asection *s;
3732 bfd_byte *p;
3733 size_t i;
3734 Elf_Internal_Verdef def;
3735 Elf_Internal_Verdaux defaux;
3736
3737 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3738 p = s->contents;
3739 do
3740 {
3741 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3742 &def);
3743 p += sizeof (Elf_External_Verdef);
3744 if (def.vd_aux != sizeof (Elf_External_Verdef))
3745 continue;
3746 for (i = 0; i < def.vd_cnt; ++i)
3747 {
3748 _bfd_elf_swap_verdaux_in (output_bfd,
3749 (Elf_External_Verdaux *) p, &defaux);
3750 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3751 defaux.vda_name);
3752 _bfd_elf_swap_verdaux_out (output_bfd,
3753 &defaux, (Elf_External_Verdaux *) p);
3754 p += sizeof (Elf_External_Verdaux);
3755 }
3756 }
3757 while (def.vd_next);
3758 }
3759
3760 /* Adjust version references. */
3761 if (elf_tdata (output_bfd)->verref)
3762 {
3763 asection *s;
3764 bfd_byte *p;
3765 size_t i;
3766 Elf_Internal_Verneed need;
3767 Elf_Internal_Vernaux needaux;
3768
3769 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3770 p = s->contents;
3771 do
3772 {
3773 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3774 &need);
3775 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3776 _bfd_elf_swap_verneed_out (output_bfd, &need,
3777 (Elf_External_Verneed *) p);
3778 p += sizeof (Elf_External_Verneed);
3779 for (i = 0; i < need.vn_cnt; ++i)
3780 {
3781 _bfd_elf_swap_vernaux_in (output_bfd,
3782 (Elf_External_Vernaux *) p, &needaux);
3783 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3784 needaux.vna_name);
3785 _bfd_elf_swap_vernaux_out (output_bfd,
3786 &needaux,
3787 (Elf_External_Vernaux *) p);
3788 p += sizeof (Elf_External_Vernaux);
3789 }
3790 }
3791 while (need.vn_next);
3792 }
3793
3794 return TRUE;
3795 }
3796
3797 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3799 The default is to only match when the INPUT and OUTPUT are exactly
3800 the same target. */
3801
3802 bfd_boolean
3803 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3804 const bfd_target *output)
3805 {
3806 return input == output;
3807 }
3808
3809 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3810 This version is used when different targets for the same architecture
3811 are virtually identical. */
3812
3813 bfd_boolean
3814 _bfd_elf_relocs_compatible (const bfd_target *input,
3815 const bfd_target *output)
3816 {
3817 const struct elf_backend_data *obed, *ibed;
3818
3819 if (input == output)
3820 return TRUE;
3821
3822 ibed = xvec_get_elf_backend_data (input);
3823 obed = xvec_get_elf_backend_data (output);
3824
3825 if (ibed->arch != obed->arch)
3826 return FALSE;
3827
3828 /* If both backends are using this function, deem them compatible. */
3829 return ibed->relocs_compatible == obed->relocs_compatible;
3830 }
3831
3832 /* Make a special call to the linker "notice" function to tell it that
3833 we are about to handle an as-needed lib, or have finished
3834 processing the lib. */
3835
3836 bfd_boolean
3837 _bfd_elf_notice_as_needed (bfd *ibfd,
3838 struct bfd_link_info *info,
3839 enum notice_asneeded_action act)
3840 {
3841 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3842 }
3843
3844 /* Check relocations an ELF object file. */
3845
3846 bfd_boolean
3847 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3848 {
3849 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3850 struct elf_link_hash_table *htab = elf_hash_table (info);
3851
3852 /* If this object is the same format as the output object, and it is
3853 not a shared library, then let the backend look through the
3854 relocs.
3855
3856 This is required to build global offset table entries and to
3857 arrange for dynamic relocs. It is not required for the
3858 particular common case of linking non PIC code, even when linking
3859 against shared libraries, but unfortunately there is no way of
3860 knowing whether an object file has been compiled PIC or not.
3861 Looking through the relocs is not particularly time consuming.
3862 The problem is that we must either (1) keep the relocs in memory,
3863 which causes the linker to require additional runtime memory or
3864 (2) read the relocs twice from the input file, which wastes time.
3865 This would be a good case for using mmap.
3866
3867 I have no idea how to handle linking PIC code into a file of a
3868 different format. It probably can't be done. */
3869 if ((abfd->flags & DYNAMIC) == 0
3870 && is_elf_hash_table (htab)
3871 && bed->check_relocs != NULL
3872 && elf_object_id (abfd) == elf_hash_table_id (htab)
3873 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3874 {
3875 asection *o;
3876
3877 for (o = abfd->sections; o != NULL; o = o->next)
3878 {
3879 Elf_Internal_Rela *internal_relocs;
3880 bfd_boolean ok;
3881
3882 /* Don't check relocations in excluded sections. */
3883 if ((o->flags & SEC_RELOC) == 0
3884 || (o->flags & SEC_EXCLUDE) != 0
3885 || o->reloc_count == 0
3886 || ((info->strip == strip_all || info->strip == strip_debugger)
3887 && (o->flags & SEC_DEBUGGING) != 0)
3888 || bfd_is_abs_section (o->output_section))
3889 continue;
3890
3891 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3892 info->keep_memory);
3893 if (internal_relocs == NULL)
3894 return FALSE;
3895
3896 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3897
3898 if (elf_section_data (o)->relocs != internal_relocs)
3899 free (internal_relocs);
3900
3901 if (! ok)
3902 return FALSE;
3903 }
3904 }
3905
3906 return TRUE;
3907 }
3908
3909 /* Add symbols from an ELF object file to the linker hash table. */
3910
3911 static bfd_boolean
3912 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3913 {
3914 Elf_Internal_Ehdr *ehdr;
3915 Elf_Internal_Shdr *hdr;
3916 size_t symcount;
3917 size_t extsymcount;
3918 size_t extsymoff;
3919 struct elf_link_hash_entry **sym_hash;
3920 bfd_boolean dynamic;
3921 Elf_External_Versym *extversym = NULL;
3922 Elf_External_Versym *extversym_end = NULL;
3923 Elf_External_Versym *ever;
3924 struct elf_link_hash_entry *weaks;
3925 struct elf_link_hash_entry **nondeflt_vers = NULL;
3926 size_t nondeflt_vers_cnt = 0;
3927 Elf_Internal_Sym *isymbuf = NULL;
3928 Elf_Internal_Sym *isym;
3929 Elf_Internal_Sym *isymend;
3930 const struct elf_backend_data *bed;
3931 bfd_boolean add_needed;
3932 struct elf_link_hash_table *htab;
3933 bfd_size_type amt;
3934 void *alloc_mark = NULL;
3935 struct bfd_hash_entry **old_table = NULL;
3936 unsigned int old_size = 0;
3937 unsigned int old_count = 0;
3938 void *old_tab = NULL;
3939 void *old_ent;
3940 struct bfd_link_hash_entry *old_undefs = NULL;
3941 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3942 void *old_strtab = NULL;
3943 size_t tabsize = 0;
3944 asection *s;
3945 bfd_boolean just_syms;
3946
3947 htab = elf_hash_table (info);
3948 bed = get_elf_backend_data (abfd);
3949
3950 if ((abfd->flags & DYNAMIC) == 0)
3951 dynamic = FALSE;
3952 else
3953 {
3954 dynamic = TRUE;
3955
3956 /* You can't use -r against a dynamic object. Also, there's no
3957 hope of using a dynamic object which does not exactly match
3958 the format of the output file. */
3959 if (bfd_link_relocatable (info)
3960 || !is_elf_hash_table (htab)
3961 || info->output_bfd->xvec != abfd->xvec)
3962 {
3963 if (bfd_link_relocatable (info))
3964 bfd_set_error (bfd_error_invalid_operation);
3965 else
3966 bfd_set_error (bfd_error_wrong_format);
3967 goto error_return;
3968 }
3969 }
3970
3971 ehdr = elf_elfheader (abfd);
3972 if (info->warn_alternate_em
3973 && bed->elf_machine_code != ehdr->e_machine
3974 && ((bed->elf_machine_alt1 != 0
3975 && ehdr->e_machine == bed->elf_machine_alt1)
3976 || (bed->elf_machine_alt2 != 0
3977 && ehdr->e_machine == bed->elf_machine_alt2)))
3978 _bfd_error_handler
3979 /* xgettext:c-format */
3980 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3981 ehdr->e_machine, abfd, bed->elf_machine_code);
3982
3983 /* As a GNU extension, any input sections which are named
3984 .gnu.warning.SYMBOL are treated as warning symbols for the given
3985 symbol. This differs from .gnu.warning sections, which generate
3986 warnings when they are included in an output file. */
3987 /* PR 12761: Also generate this warning when building shared libraries. */
3988 for (s = abfd->sections; s != NULL; s = s->next)
3989 {
3990 const char *name;
3991
3992 name = bfd_section_name (s);
3993 if (CONST_STRNEQ (name, ".gnu.warning."))
3994 {
3995 char *msg;
3996 bfd_size_type sz;
3997
3998 name += sizeof ".gnu.warning." - 1;
3999
4000 /* If this is a shared object, then look up the symbol
4001 in the hash table. If it is there, and it is already
4002 been defined, then we will not be using the entry
4003 from this shared object, so we don't need to warn.
4004 FIXME: If we see the definition in a regular object
4005 later on, we will warn, but we shouldn't. The only
4006 fix is to keep track of what warnings we are supposed
4007 to emit, and then handle them all at the end of the
4008 link. */
4009 if (dynamic)
4010 {
4011 struct elf_link_hash_entry *h;
4012
4013 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4014
4015 /* FIXME: What about bfd_link_hash_common? */
4016 if (h != NULL
4017 && (h->root.type == bfd_link_hash_defined
4018 || h->root.type == bfd_link_hash_defweak))
4019 continue;
4020 }
4021
4022 sz = s->size;
4023 msg = (char *) bfd_alloc (abfd, sz + 1);
4024 if (msg == NULL)
4025 goto error_return;
4026
4027 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4028 goto error_return;
4029
4030 msg[sz] = '\0';
4031
4032 if (! (_bfd_generic_link_add_one_symbol
4033 (info, abfd, name, BSF_WARNING, s, 0, msg,
4034 FALSE, bed->collect, NULL)))
4035 goto error_return;
4036
4037 if (bfd_link_executable (info))
4038 {
4039 /* Clobber the section size so that the warning does
4040 not get copied into the output file. */
4041 s->size = 0;
4042
4043 /* Also set SEC_EXCLUDE, so that symbols defined in
4044 the warning section don't get copied to the output. */
4045 s->flags |= SEC_EXCLUDE;
4046 }
4047 }
4048 }
4049
4050 just_syms = ((s = abfd->sections) != NULL
4051 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4052
4053 add_needed = TRUE;
4054 if (! dynamic)
4055 {
4056 /* If we are creating a shared library, create all the dynamic
4057 sections immediately. We need to attach them to something,
4058 so we attach them to this BFD, provided it is the right
4059 format and is not from ld --just-symbols. Always create the
4060 dynamic sections for -E/--dynamic-list. FIXME: If there
4061 are no input BFD's of the same format as the output, we can't
4062 make a shared library. */
4063 if (!just_syms
4064 && (bfd_link_pic (info)
4065 || (!bfd_link_relocatable (info)
4066 && info->nointerp
4067 && (info->export_dynamic || info->dynamic)))
4068 && is_elf_hash_table (htab)
4069 && info->output_bfd->xvec == abfd->xvec
4070 && !htab->dynamic_sections_created)
4071 {
4072 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4073 goto error_return;
4074 }
4075 }
4076 else if (!is_elf_hash_table (htab))
4077 goto error_return;
4078 else
4079 {
4080 const char *soname = NULL;
4081 char *audit = NULL;
4082 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4083 const Elf_Internal_Phdr *phdr;
4084 int ret;
4085
4086 /* ld --just-symbols and dynamic objects don't mix very well.
4087 ld shouldn't allow it. */
4088 if (just_syms)
4089 abort ();
4090
4091 /* If this dynamic lib was specified on the command line with
4092 --as-needed in effect, then we don't want to add a DT_NEEDED
4093 tag unless the lib is actually used. Similary for libs brought
4094 in by another lib's DT_NEEDED. When --no-add-needed is used
4095 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4096 any dynamic library in DT_NEEDED tags in the dynamic lib at
4097 all. */
4098 add_needed = (elf_dyn_lib_class (abfd)
4099 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4100 | DYN_NO_NEEDED)) == 0;
4101
4102 s = bfd_get_section_by_name (abfd, ".dynamic");
4103 if (s != NULL)
4104 {
4105 bfd_byte *dynbuf;
4106 bfd_byte *extdyn;
4107 unsigned int elfsec;
4108 unsigned long shlink;
4109
4110 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4111 {
4112 error_free_dyn:
4113 free (dynbuf);
4114 goto error_return;
4115 }
4116
4117 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4118 if (elfsec == SHN_BAD)
4119 goto error_free_dyn;
4120 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4121
4122 for (extdyn = dynbuf;
4123 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4124 extdyn += bed->s->sizeof_dyn)
4125 {
4126 Elf_Internal_Dyn dyn;
4127
4128 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4129 if (dyn.d_tag == DT_SONAME)
4130 {
4131 unsigned int tagv = dyn.d_un.d_val;
4132 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4133 if (soname == NULL)
4134 goto error_free_dyn;
4135 }
4136 if (dyn.d_tag == DT_NEEDED)
4137 {
4138 struct bfd_link_needed_list *n, **pn;
4139 char *fnm, *anm;
4140 unsigned int tagv = dyn.d_un.d_val;
4141
4142 amt = sizeof (struct bfd_link_needed_list);
4143 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4144 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4145 if (n == NULL || fnm == NULL)
4146 goto error_free_dyn;
4147 amt = strlen (fnm) + 1;
4148 anm = (char *) bfd_alloc (abfd, amt);
4149 if (anm == NULL)
4150 goto error_free_dyn;
4151 memcpy (anm, fnm, amt);
4152 n->name = anm;
4153 n->by = abfd;
4154 n->next = NULL;
4155 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4156 ;
4157 *pn = n;
4158 }
4159 if (dyn.d_tag == DT_RUNPATH)
4160 {
4161 struct bfd_link_needed_list *n, **pn;
4162 char *fnm, *anm;
4163 unsigned int tagv = dyn.d_un.d_val;
4164
4165 amt = sizeof (struct bfd_link_needed_list);
4166 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4167 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4168 if (n == NULL || fnm == NULL)
4169 goto error_free_dyn;
4170 amt = strlen (fnm) + 1;
4171 anm = (char *) bfd_alloc (abfd, amt);
4172 if (anm == NULL)
4173 goto error_free_dyn;
4174 memcpy (anm, fnm, amt);
4175 n->name = anm;
4176 n->by = abfd;
4177 n->next = NULL;
4178 for (pn = & runpath;
4179 *pn != NULL;
4180 pn = &(*pn)->next)
4181 ;
4182 *pn = n;
4183 }
4184 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4185 if (!runpath && dyn.d_tag == DT_RPATH)
4186 {
4187 struct bfd_link_needed_list *n, **pn;
4188 char *fnm, *anm;
4189 unsigned int tagv = dyn.d_un.d_val;
4190
4191 amt = sizeof (struct bfd_link_needed_list);
4192 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4193 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4194 if (n == NULL || fnm == NULL)
4195 goto error_free_dyn;
4196 amt = strlen (fnm) + 1;
4197 anm = (char *) bfd_alloc (abfd, amt);
4198 if (anm == NULL)
4199 goto error_free_dyn;
4200 memcpy (anm, fnm, amt);
4201 n->name = anm;
4202 n->by = abfd;
4203 n->next = NULL;
4204 for (pn = & rpath;
4205 *pn != NULL;
4206 pn = &(*pn)->next)
4207 ;
4208 *pn = n;
4209 }
4210 if (dyn.d_tag == DT_AUDIT)
4211 {
4212 unsigned int tagv = dyn.d_un.d_val;
4213 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4214 }
4215 }
4216
4217 free (dynbuf);
4218 }
4219
4220 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4221 frees all more recently bfd_alloc'd blocks as well. */
4222 if (runpath)
4223 rpath = runpath;
4224
4225 if (rpath)
4226 {
4227 struct bfd_link_needed_list **pn;
4228 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4229 ;
4230 *pn = rpath;
4231 }
4232
4233 /* If we have a PT_GNU_RELRO program header, mark as read-only
4234 all sections contained fully therein. This makes relro
4235 shared library sections appear as they will at run-time. */
4236 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4237 while (phdr-- > elf_tdata (abfd)->phdr)
4238 if (phdr->p_type == PT_GNU_RELRO)
4239 {
4240 for (s = abfd->sections; s != NULL; s = s->next)
4241 if ((s->flags & SEC_ALLOC) != 0
4242 && s->vma >= phdr->p_vaddr
4243 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4244 s->flags |= SEC_READONLY;
4245 break;
4246 }
4247
4248 /* We do not want to include any of the sections in a dynamic
4249 object in the output file. We hack by simply clobbering the
4250 list of sections in the BFD. This could be handled more
4251 cleanly by, say, a new section flag; the existing
4252 SEC_NEVER_LOAD flag is not the one we want, because that one
4253 still implies that the section takes up space in the output
4254 file. */
4255 bfd_section_list_clear (abfd);
4256
4257 /* Find the name to use in a DT_NEEDED entry that refers to this
4258 object. If the object has a DT_SONAME entry, we use it.
4259 Otherwise, if the generic linker stuck something in
4260 elf_dt_name, we use that. Otherwise, we just use the file
4261 name. */
4262 if (soname == NULL || *soname == '\0')
4263 {
4264 soname = elf_dt_name (abfd);
4265 if (soname == NULL || *soname == '\0')
4266 soname = bfd_get_filename (abfd);
4267 }
4268
4269 /* Save the SONAME because sometimes the linker emulation code
4270 will need to know it. */
4271 elf_dt_name (abfd) = soname;
4272
4273 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4274 if (ret < 0)
4275 goto error_return;
4276
4277 /* If we have already included this dynamic object in the
4278 link, just ignore it. There is no reason to include a
4279 particular dynamic object more than once. */
4280 if (ret > 0)
4281 return TRUE;
4282
4283 /* Save the DT_AUDIT entry for the linker emulation code. */
4284 elf_dt_audit (abfd) = audit;
4285 }
4286
4287 /* If this is a dynamic object, we always link against the .dynsym
4288 symbol table, not the .symtab symbol table. The dynamic linker
4289 will only see the .dynsym symbol table, so there is no reason to
4290 look at .symtab for a dynamic object. */
4291
4292 if (! dynamic || elf_dynsymtab (abfd) == 0)
4293 hdr = &elf_tdata (abfd)->symtab_hdr;
4294 else
4295 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4296
4297 symcount = hdr->sh_size / bed->s->sizeof_sym;
4298
4299 /* The sh_info field of the symtab header tells us where the
4300 external symbols start. We don't care about the local symbols at
4301 this point. */
4302 if (elf_bad_symtab (abfd))
4303 {
4304 extsymcount = symcount;
4305 extsymoff = 0;
4306 }
4307 else
4308 {
4309 extsymcount = symcount - hdr->sh_info;
4310 extsymoff = hdr->sh_info;
4311 }
4312
4313 sym_hash = elf_sym_hashes (abfd);
4314 if (extsymcount != 0)
4315 {
4316 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4317 NULL, NULL, NULL);
4318 if (isymbuf == NULL)
4319 goto error_return;
4320
4321 if (sym_hash == NULL)
4322 {
4323 /* We store a pointer to the hash table entry for each
4324 external symbol. */
4325 amt = extsymcount;
4326 amt *= sizeof (struct elf_link_hash_entry *);
4327 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4328 if (sym_hash == NULL)
4329 goto error_free_sym;
4330 elf_sym_hashes (abfd) = sym_hash;
4331 }
4332 }
4333
4334 if (dynamic)
4335 {
4336 /* Read in any version definitions. */
4337 if (!_bfd_elf_slurp_version_tables (abfd,
4338 info->default_imported_symver))
4339 goto error_free_sym;
4340
4341 /* Read in the symbol versions, but don't bother to convert them
4342 to internal format. */
4343 if (elf_dynversym (abfd) != 0)
4344 {
4345 Elf_Internal_Shdr *versymhdr;
4346
4347 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4348 amt = versymhdr->sh_size;
4349 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4350 if (extversym == NULL)
4351 goto error_free_sym;
4352 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4353 || bfd_bread (extversym, amt, abfd) != amt)
4354 goto error_free_vers;
4355 extversym_end = extversym + (amt / sizeof (* extversym));
4356 }
4357 }
4358
4359 /* If we are loading an as-needed shared lib, save the symbol table
4360 state before we start adding symbols. If the lib turns out
4361 to be unneeded, restore the state. */
4362 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4363 {
4364 unsigned int i;
4365 size_t entsize;
4366
4367 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4368 {
4369 struct bfd_hash_entry *p;
4370 struct elf_link_hash_entry *h;
4371
4372 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4373 {
4374 h = (struct elf_link_hash_entry *) p;
4375 entsize += htab->root.table.entsize;
4376 if (h->root.type == bfd_link_hash_warning)
4377 entsize += htab->root.table.entsize;
4378 }
4379 }
4380
4381 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4382 old_tab = bfd_malloc (tabsize + entsize);
4383 if (old_tab == NULL)
4384 goto error_free_vers;
4385
4386 /* Remember the current objalloc pointer, so that all mem for
4387 symbols added can later be reclaimed. */
4388 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4389 if (alloc_mark == NULL)
4390 goto error_free_vers;
4391
4392 /* Make a special call to the linker "notice" function to
4393 tell it that we are about to handle an as-needed lib. */
4394 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4395 goto error_free_vers;
4396
4397 /* Clone the symbol table. Remember some pointers into the
4398 symbol table, and dynamic symbol count. */
4399 old_ent = (char *) old_tab + tabsize;
4400 memcpy (old_tab, htab->root.table.table, tabsize);
4401 old_undefs = htab->root.undefs;
4402 old_undefs_tail = htab->root.undefs_tail;
4403 old_table = htab->root.table.table;
4404 old_size = htab->root.table.size;
4405 old_count = htab->root.table.count;
4406 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4407 if (old_strtab == NULL)
4408 goto error_free_vers;
4409
4410 for (i = 0; i < htab->root.table.size; i++)
4411 {
4412 struct bfd_hash_entry *p;
4413 struct elf_link_hash_entry *h;
4414
4415 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4416 {
4417 memcpy (old_ent, p, htab->root.table.entsize);
4418 old_ent = (char *) old_ent + htab->root.table.entsize;
4419 h = (struct elf_link_hash_entry *) p;
4420 if (h->root.type == bfd_link_hash_warning)
4421 {
4422 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4423 old_ent = (char *) old_ent + htab->root.table.entsize;
4424 }
4425 }
4426 }
4427 }
4428
4429 weaks = NULL;
4430 if (extversym == NULL)
4431 ever = NULL;
4432 else if (extversym + extsymoff < extversym_end)
4433 ever = extversym + extsymoff;
4434 else
4435 {
4436 /* xgettext:c-format */
4437 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4438 abfd, (long) extsymoff,
4439 (long) (extversym_end - extversym) / sizeof (* extversym));
4440 bfd_set_error (bfd_error_bad_value);
4441 goto error_free_vers;
4442 }
4443
4444 if (!bfd_link_relocatable (info)
4445 && abfd->lto_slim_object)
4446 {
4447 _bfd_error_handler
4448 (_("%pB: plugin needed to handle lto object"), abfd);
4449 }
4450
4451 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4452 isym < isymend;
4453 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4454 {
4455 int bind;
4456 bfd_vma value;
4457 asection *sec, *new_sec;
4458 flagword flags;
4459 const char *name;
4460 struct elf_link_hash_entry *h;
4461 struct elf_link_hash_entry *hi;
4462 bfd_boolean definition;
4463 bfd_boolean size_change_ok;
4464 bfd_boolean type_change_ok;
4465 bfd_boolean new_weak;
4466 bfd_boolean old_weak;
4467 bfd_boolean override;
4468 bfd_boolean common;
4469 bfd_boolean discarded;
4470 unsigned int old_alignment;
4471 unsigned int shindex;
4472 bfd *old_bfd;
4473 bfd_boolean matched;
4474
4475 override = FALSE;
4476
4477 flags = BSF_NO_FLAGS;
4478 sec = NULL;
4479 value = isym->st_value;
4480 common = bed->common_definition (isym);
4481 if (common && info->inhibit_common_definition)
4482 {
4483 /* Treat common symbol as undefined for --no-define-common. */
4484 isym->st_shndx = SHN_UNDEF;
4485 common = FALSE;
4486 }
4487 discarded = FALSE;
4488
4489 bind = ELF_ST_BIND (isym->st_info);
4490 switch (bind)
4491 {
4492 case STB_LOCAL:
4493 /* This should be impossible, since ELF requires that all
4494 global symbols follow all local symbols, and that sh_info
4495 point to the first global symbol. Unfortunately, Irix 5
4496 screws this up. */
4497 if (elf_bad_symtab (abfd))
4498 continue;
4499
4500 /* If we aren't prepared to handle locals within the globals
4501 then we'll likely segfault on a NULL symbol hash if the
4502 symbol is ever referenced in relocations. */
4503 shindex = elf_elfheader (abfd)->e_shstrndx;
4504 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4505 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4506 " (>= sh_info of %lu)"),
4507 abfd, name, (long) (isym - isymbuf + extsymoff),
4508 (long) extsymoff);
4509
4510 /* Dynamic object relocations are not processed by ld, so
4511 ld won't run into the problem mentioned above. */
4512 if (dynamic)
4513 continue;
4514 bfd_set_error (bfd_error_bad_value);
4515 goto error_free_vers;
4516
4517 case STB_GLOBAL:
4518 if (isym->st_shndx != SHN_UNDEF && !common)
4519 flags = BSF_GLOBAL;
4520 break;
4521
4522 case STB_WEAK:
4523 flags = BSF_WEAK;
4524 break;
4525
4526 case STB_GNU_UNIQUE:
4527 flags = BSF_GNU_UNIQUE;
4528 break;
4529
4530 default:
4531 /* Leave it up to the processor backend. */
4532 break;
4533 }
4534
4535 if (isym->st_shndx == SHN_UNDEF)
4536 sec = bfd_und_section_ptr;
4537 else if (isym->st_shndx == SHN_ABS)
4538 sec = bfd_abs_section_ptr;
4539 else if (isym->st_shndx == SHN_COMMON)
4540 {
4541 sec = bfd_com_section_ptr;
4542 /* What ELF calls the size we call the value. What ELF
4543 calls the value we call the alignment. */
4544 value = isym->st_size;
4545 }
4546 else
4547 {
4548 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4549 if (sec == NULL)
4550 sec = bfd_abs_section_ptr;
4551 else if (discarded_section (sec))
4552 {
4553 /* Symbols from discarded section are undefined. We keep
4554 its visibility. */
4555 sec = bfd_und_section_ptr;
4556 discarded = TRUE;
4557 isym->st_shndx = SHN_UNDEF;
4558 }
4559 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4560 value -= sec->vma;
4561 }
4562
4563 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4564 isym->st_name);
4565 if (name == NULL)
4566 goto error_free_vers;
4567
4568 if (isym->st_shndx == SHN_COMMON
4569 && (abfd->flags & BFD_PLUGIN) != 0)
4570 {
4571 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4572
4573 if (xc == NULL)
4574 {
4575 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4576 | SEC_EXCLUDE);
4577 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4578 if (xc == NULL)
4579 goto error_free_vers;
4580 }
4581 sec = xc;
4582 }
4583 else if (isym->st_shndx == SHN_COMMON
4584 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4585 && !bfd_link_relocatable (info))
4586 {
4587 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4588
4589 if (tcomm == NULL)
4590 {
4591 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4592 | SEC_LINKER_CREATED);
4593 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4594 if (tcomm == NULL)
4595 goto error_free_vers;
4596 }
4597 sec = tcomm;
4598 }
4599 else if (bed->elf_add_symbol_hook)
4600 {
4601 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4602 &sec, &value))
4603 goto error_free_vers;
4604
4605 /* The hook function sets the name to NULL if this symbol
4606 should be skipped for some reason. */
4607 if (name == NULL)
4608 continue;
4609 }
4610
4611 /* Sanity check that all possibilities were handled. */
4612 if (sec == NULL)
4613 abort ();
4614
4615 /* Silently discard TLS symbols from --just-syms. There's
4616 no way to combine a static TLS block with a new TLS block
4617 for this executable. */
4618 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4619 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4620 continue;
4621
4622 if (bfd_is_und_section (sec)
4623 || bfd_is_com_section (sec))
4624 definition = FALSE;
4625 else
4626 definition = TRUE;
4627
4628 size_change_ok = FALSE;
4629 type_change_ok = bed->type_change_ok;
4630 old_weak = FALSE;
4631 matched = FALSE;
4632 old_alignment = 0;
4633 old_bfd = NULL;
4634 new_sec = sec;
4635
4636 if (is_elf_hash_table (htab))
4637 {
4638 Elf_Internal_Versym iver;
4639 unsigned int vernum = 0;
4640 bfd_boolean skip;
4641
4642 if (ever == NULL)
4643 {
4644 if (info->default_imported_symver)
4645 /* Use the default symbol version created earlier. */
4646 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4647 else
4648 iver.vs_vers = 0;
4649 }
4650 else if (ever >= extversym_end)
4651 {
4652 /* xgettext:c-format */
4653 _bfd_error_handler (_("%pB: not enough version information"),
4654 abfd);
4655 bfd_set_error (bfd_error_bad_value);
4656 goto error_free_vers;
4657 }
4658 else
4659 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4660
4661 vernum = iver.vs_vers & VERSYM_VERSION;
4662
4663 /* If this is a hidden symbol, or if it is not version
4664 1, we append the version name to the symbol name.
4665 However, we do not modify a non-hidden absolute symbol
4666 if it is not a function, because it might be the version
4667 symbol itself. FIXME: What if it isn't? */
4668 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4669 || (vernum > 1
4670 && (!bfd_is_abs_section (sec)
4671 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4672 {
4673 const char *verstr;
4674 size_t namelen, verlen, newlen;
4675 char *newname, *p;
4676
4677 if (isym->st_shndx != SHN_UNDEF)
4678 {
4679 if (vernum > elf_tdata (abfd)->cverdefs)
4680 verstr = NULL;
4681 else if (vernum > 1)
4682 verstr =
4683 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4684 else
4685 verstr = "";
4686
4687 if (verstr == NULL)
4688 {
4689 _bfd_error_handler
4690 /* xgettext:c-format */
4691 (_("%pB: %s: invalid version %u (max %d)"),
4692 abfd, name, vernum,
4693 elf_tdata (abfd)->cverdefs);
4694 bfd_set_error (bfd_error_bad_value);
4695 goto error_free_vers;
4696 }
4697 }
4698 else
4699 {
4700 /* We cannot simply test for the number of
4701 entries in the VERNEED section since the
4702 numbers for the needed versions do not start
4703 at 0. */
4704 Elf_Internal_Verneed *t;
4705
4706 verstr = NULL;
4707 for (t = elf_tdata (abfd)->verref;
4708 t != NULL;
4709 t = t->vn_nextref)
4710 {
4711 Elf_Internal_Vernaux *a;
4712
4713 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4714 {
4715 if (a->vna_other == vernum)
4716 {
4717 verstr = a->vna_nodename;
4718 break;
4719 }
4720 }
4721 if (a != NULL)
4722 break;
4723 }
4724 if (verstr == NULL)
4725 {
4726 _bfd_error_handler
4727 /* xgettext:c-format */
4728 (_("%pB: %s: invalid needed version %d"),
4729 abfd, name, vernum);
4730 bfd_set_error (bfd_error_bad_value);
4731 goto error_free_vers;
4732 }
4733 }
4734
4735 namelen = strlen (name);
4736 verlen = strlen (verstr);
4737 newlen = namelen + verlen + 2;
4738 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4739 && isym->st_shndx != SHN_UNDEF)
4740 ++newlen;
4741
4742 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4743 if (newname == NULL)
4744 goto error_free_vers;
4745 memcpy (newname, name, namelen);
4746 p = newname + namelen;
4747 *p++ = ELF_VER_CHR;
4748 /* If this is a defined non-hidden version symbol,
4749 we add another @ to the name. This indicates the
4750 default version of the symbol. */
4751 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4752 && isym->st_shndx != SHN_UNDEF)
4753 *p++ = ELF_VER_CHR;
4754 memcpy (p, verstr, verlen + 1);
4755
4756 name = newname;
4757 }
4758
4759 /* If this symbol has default visibility and the user has
4760 requested we not re-export it, then mark it as hidden. */
4761 if (!bfd_is_und_section (sec)
4762 && !dynamic
4763 && abfd->no_export
4764 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4765 isym->st_other = (STV_HIDDEN
4766 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4767
4768 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4769 sym_hash, &old_bfd, &old_weak,
4770 &old_alignment, &skip, &override,
4771 &type_change_ok, &size_change_ok,
4772 &matched))
4773 goto error_free_vers;
4774
4775 if (skip)
4776 continue;
4777
4778 /* Override a definition only if the new symbol matches the
4779 existing one. */
4780 if (override && matched)
4781 definition = FALSE;
4782
4783 h = *sym_hash;
4784 while (h->root.type == bfd_link_hash_indirect
4785 || h->root.type == bfd_link_hash_warning)
4786 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4787
4788 if (elf_tdata (abfd)->verdef != NULL
4789 && vernum > 1
4790 && definition)
4791 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4792 }
4793
4794 if (! (_bfd_generic_link_add_one_symbol
4795 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4796 (struct bfd_link_hash_entry **) sym_hash)))
4797 goto error_free_vers;
4798
4799 h = *sym_hash;
4800 /* We need to make sure that indirect symbol dynamic flags are
4801 updated. */
4802 hi = h;
4803 while (h->root.type == bfd_link_hash_indirect
4804 || h->root.type == bfd_link_hash_warning)
4805 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4806
4807 /* Setting the index to -3 tells elf_link_output_extsym that
4808 this symbol is defined in a discarded section. */
4809 if (discarded)
4810 h->indx = -3;
4811
4812 *sym_hash = h;
4813
4814 new_weak = (flags & BSF_WEAK) != 0;
4815 if (dynamic
4816 && definition
4817 && new_weak
4818 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4819 && is_elf_hash_table (htab)
4820 && h->u.alias == NULL)
4821 {
4822 /* Keep a list of all weak defined non function symbols from
4823 a dynamic object, using the alias field. Later in this
4824 function we will set the alias field to the correct
4825 value. We only put non-function symbols from dynamic
4826 objects on this list, because that happens to be the only
4827 time we need to know the normal symbol corresponding to a
4828 weak symbol, and the information is time consuming to
4829 figure out. If the alias field is not already NULL,
4830 then this symbol was already defined by some previous
4831 dynamic object, and we will be using that previous
4832 definition anyhow. */
4833
4834 h->u.alias = weaks;
4835 weaks = h;
4836 }
4837
4838 /* Set the alignment of a common symbol. */
4839 if ((common || bfd_is_com_section (sec))
4840 && h->root.type == bfd_link_hash_common)
4841 {
4842 unsigned int align;
4843
4844 if (common)
4845 align = bfd_log2 (isym->st_value);
4846 else
4847 {
4848 /* The new symbol is a common symbol in a shared object.
4849 We need to get the alignment from the section. */
4850 align = new_sec->alignment_power;
4851 }
4852 if (align > old_alignment)
4853 h->root.u.c.p->alignment_power = align;
4854 else
4855 h->root.u.c.p->alignment_power = old_alignment;
4856 }
4857
4858 if (is_elf_hash_table (htab))
4859 {
4860 /* Set a flag in the hash table entry indicating the type of
4861 reference or definition we just found. A dynamic symbol
4862 is one which is referenced or defined by both a regular
4863 object and a shared object. */
4864 bfd_boolean dynsym = FALSE;
4865
4866 /* Plugin symbols aren't normal. Don't set def_regular or
4867 ref_regular for them, or make them dynamic. */
4868 if ((abfd->flags & BFD_PLUGIN) != 0)
4869 ;
4870 else if (! dynamic)
4871 {
4872 if (! definition)
4873 {
4874 h->ref_regular = 1;
4875 if (bind != STB_WEAK)
4876 h->ref_regular_nonweak = 1;
4877 }
4878 else
4879 {
4880 h->def_regular = 1;
4881 if (h->def_dynamic)
4882 {
4883 h->def_dynamic = 0;
4884 h->ref_dynamic = 1;
4885 }
4886 }
4887
4888 /* If the indirect symbol has been forced local, don't
4889 make the real symbol dynamic. */
4890 if ((h == hi || !hi->forced_local)
4891 && (bfd_link_dll (info)
4892 || h->def_dynamic
4893 || h->ref_dynamic))
4894 dynsym = TRUE;
4895 }
4896 else
4897 {
4898 if (! definition)
4899 {
4900 h->ref_dynamic = 1;
4901 hi->ref_dynamic = 1;
4902 }
4903 else
4904 {
4905 h->def_dynamic = 1;
4906 hi->def_dynamic = 1;
4907 }
4908
4909 /* If the indirect symbol has been forced local, don't
4910 make the real symbol dynamic. */
4911 if ((h == hi || !hi->forced_local)
4912 && (h->def_regular
4913 || h->ref_regular
4914 || (h->is_weakalias
4915 && weakdef (h)->dynindx != -1)))
4916 dynsym = TRUE;
4917 }
4918
4919 /* Check to see if we need to add an indirect symbol for
4920 the default name. */
4921 if (definition
4922 || (!override && h->root.type == bfd_link_hash_common))
4923 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4924 sec, value, &old_bfd, &dynsym))
4925 goto error_free_vers;
4926
4927 /* Check the alignment when a common symbol is involved. This
4928 can change when a common symbol is overridden by a normal
4929 definition or a common symbol is ignored due to the old
4930 normal definition. We need to make sure the maximum
4931 alignment is maintained. */
4932 if ((old_alignment || common)
4933 && h->root.type != bfd_link_hash_common)
4934 {
4935 unsigned int common_align;
4936 unsigned int normal_align;
4937 unsigned int symbol_align;
4938 bfd *normal_bfd;
4939 bfd *common_bfd;
4940
4941 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4942 || h->root.type == bfd_link_hash_defweak);
4943
4944 symbol_align = ffs (h->root.u.def.value) - 1;
4945 if (h->root.u.def.section->owner != NULL
4946 && (h->root.u.def.section->owner->flags
4947 & (DYNAMIC | BFD_PLUGIN)) == 0)
4948 {
4949 normal_align = h->root.u.def.section->alignment_power;
4950 if (normal_align > symbol_align)
4951 normal_align = symbol_align;
4952 }
4953 else
4954 normal_align = symbol_align;
4955
4956 if (old_alignment)
4957 {
4958 common_align = old_alignment;
4959 common_bfd = old_bfd;
4960 normal_bfd = abfd;
4961 }
4962 else
4963 {
4964 common_align = bfd_log2 (isym->st_value);
4965 common_bfd = abfd;
4966 normal_bfd = old_bfd;
4967 }
4968
4969 if (normal_align < common_align)
4970 {
4971 /* PR binutils/2735 */
4972 if (normal_bfd == NULL)
4973 _bfd_error_handler
4974 /* xgettext:c-format */
4975 (_("warning: alignment %u of common symbol `%s' in %pB is"
4976 " greater than the alignment (%u) of its section %pA"),
4977 1 << common_align, name, common_bfd,
4978 1 << normal_align, h->root.u.def.section);
4979 else
4980 _bfd_error_handler
4981 /* xgettext:c-format */
4982 (_("warning: alignment %u of symbol `%s' in %pB"
4983 " is smaller than %u in %pB"),
4984 1 << normal_align, name, normal_bfd,
4985 1 << common_align, common_bfd);
4986 }
4987 }
4988
4989 /* Remember the symbol size if it isn't undefined. */
4990 if (isym->st_size != 0
4991 && isym->st_shndx != SHN_UNDEF
4992 && (definition || h->size == 0))
4993 {
4994 if (h->size != 0
4995 && h->size != isym->st_size
4996 && ! size_change_ok)
4997 _bfd_error_handler
4998 /* xgettext:c-format */
4999 (_("warning: size of symbol `%s' changed"
5000 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5001 name, (uint64_t) h->size, old_bfd,
5002 (uint64_t) isym->st_size, abfd);
5003
5004 h->size = isym->st_size;
5005 }
5006
5007 /* If this is a common symbol, then we always want H->SIZE
5008 to be the size of the common symbol. The code just above
5009 won't fix the size if a common symbol becomes larger. We
5010 don't warn about a size change here, because that is
5011 covered by --warn-common. Allow changes between different
5012 function types. */
5013 if (h->root.type == bfd_link_hash_common)
5014 h->size = h->root.u.c.size;
5015
5016 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5017 && ((definition && !new_weak)
5018 || (old_weak && h->root.type == bfd_link_hash_common)
5019 || h->type == STT_NOTYPE))
5020 {
5021 unsigned int type = ELF_ST_TYPE (isym->st_info);
5022
5023 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5024 symbol. */
5025 if (type == STT_GNU_IFUNC
5026 && (abfd->flags & DYNAMIC) != 0)
5027 type = STT_FUNC;
5028
5029 if (h->type != type)
5030 {
5031 if (h->type != STT_NOTYPE && ! type_change_ok)
5032 /* xgettext:c-format */
5033 _bfd_error_handler
5034 (_("warning: type of symbol `%s' changed"
5035 " from %d to %d in %pB"),
5036 name, h->type, type, abfd);
5037
5038 h->type = type;
5039 }
5040 }
5041
5042 /* Merge st_other field. */
5043 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5044
5045 /* We don't want to make debug symbol dynamic. */
5046 if (definition
5047 && (sec->flags & SEC_DEBUGGING)
5048 && !bfd_link_relocatable (info))
5049 dynsym = FALSE;
5050
5051 /* Nor should we make plugin symbols dynamic. */
5052 if ((abfd->flags & BFD_PLUGIN) != 0)
5053 dynsym = FALSE;
5054
5055 if (definition)
5056 {
5057 h->target_internal = isym->st_target_internal;
5058 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5059 }
5060
5061 if (definition && !dynamic)
5062 {
5063 char *p = strchr (name, ELF_VER_CHR);
5064 if (p != NULL && p[1] != ELF_VER_CHR)
5065 {
5066 /* Queue non-default versions so that .symver x, x@FOO
5067 aliases can be checked. */
5068 if (!nondeflt_vers)
5069 {
5070 amt = ((isymend - isym + 1)
5071 * sizeof (struct elf_link_hash_entry *));
5072 nondeflt_vers
5073 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5074 if (!nondeflt_vers)
5075 goto error_free_vers;
5076 }
5077 nondeflt_vers[nondeflt_vers_cnt++] = h;
5078 }
5079 }
5080
5081 if (dynsym && h->dynindx == -1)
5082 {
5083 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5084 goto error_free_vers;
5085 if (h->is_weakalias
5086 && weakdef (h)->dynindx == -1)
5087 {
5088 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5089 goto error_free_vers;
5090 }
5091 }
5092 else if (h->dynindx != -1)
5093 /* If the symbol already has a dynamic index, but
5094 visibility says it should not be visible, turn it into
5095 a local symbol. */
5096 switch (ELF_ST_VISIBILITY (h->other))
5097 {
5098 case STV_INTERNAL:
5099 case STV_HIDDEN:
5100 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5101 dynsym = FALSE;
5102 break;
5103 }
5104
5105 /* Don't add DT_NEEDED for references from the dummy bfd nor
5106 for unmatched symbol. */
5107 if (!add_needed
5108 && matched
5109 && definition
5110 && ((dynsym
5111 && h->ref_regular_nonweak
5112 && (old_bfd == NULL
5113 || (old_bfd->flags & BFD_PLUGIN) == 0))
5114 || (h->ref_dynamic_nonweak
5115 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5116 && !on_needed_list (elf_dt_name (abfd),
5117 htab->needed, NULL))))
5118 {
5119 int ret;
5120 const char *soname = elf_dt_name (abfd);
5121
5122 info->callbacks->minfo ("%!", soname, old_bfd,
5123 h->root.root.string);
5124
5125 /* A symbol from a library loaded via DT_NEEDED of some
5126 other library is referenced by a regular object.
5127 Add a DT_NEEDED entry for it. Issue an error if
5128 --no-add-needed is used and the reference was not
5129 a weak one. */
5130 if (old_bfd != NULL
5131 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5132 {
5133 _bfd_error_handler
5134 /* xgettext:c-format */
5135 (_("%pB: undefined reference to symbol '%s'"),
5136 old_bfd, name);
5137 bfd_set_error (bfd_error_missing_dso);
5138 goto error_free_vers;
5139 }
5140
5141 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5142 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5143
5144 add_needed = TRUE;
5145 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5146 if (ret < 0)
5147 goto error_free_vers;
5148
5149 BFD_ASSERT (ret == 0);
5150 }
5151 }
5152 }
5153
5154 if (info->lto_plugin_active
5155 && !bfd_link_relocatable (info)
5156 && (abfd->flags & BFD_PLUGIN) == 0
5157 && !just_syms
5158 && extsymcount)
5159 {
5160 int r_sym_shift;
5161
5162 if (bed->s->arch_size == 32)
5163 r_sym_shift = 8;
5164 else
5165 r_sym_shift = 32;
5166
5167 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5168 referenced in regular objects so that linker plugin will get
5169 the correct symbol resolution. */
5170
5171 sym_hash = elf_sym_hashes (abfd);
5172 for (s = abfd->sections; s != NULL; s = s->next)
5173 {
5174 Elf_Internal_Rela *internal_relocs;
5175 Elf_Internal_Rela *rel, *relend;
5176
5177 /* Don't check relocations in excluded sections. */
5178 if ((s->flags & SEC_RELOC) == 0
5179 || s->reloc_count == 0
5180 || (s->flags & SEC_EXCLUDE) != 0
5181 || ((info->strip == strip_all
5182 || info->strip == strip_debugger)
5183 && (s->flags & SEC_DEBUGGING) != 0))
5184 continue;
5185
5186 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5187 NULL,
5188 info->keep_memory);
5189 if (internal_relocs == NULL)
5190 goto error_free_vers;
5191
5192 rel = internal_relocs;
5193 relend = rel + s->reloc_count;
5194 for ( ; rel < relend; rel++)
5195 {
5196 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5197 struct elf_link_hash_entry *h;
5198
5199 /* Skip local symbols. */
5200 if (r_symndx < extsymoff)
5201 continue;
5202
5203 h = sym_hash[r_symndx - extsymoff];
5204 if (h != NULL)
5205 h->root.non_ir_ref_regular = 1;
5206 }
5207
5208 if (elf_section_data (s)->relocs != internal_relocs)
5209 free (internal_relocs);
5210 }
5211 }
5212
5213 if (extversym != NULL)
5214 {
5215 free (extversym);
5216 extversym = NULL;
5217 }
5218
5219 if (isymbuf != NULL)
5220 {
5221 free (isymbuf);
5222 isymbuf = NULL;
5223 }
5224
5225 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5226 {
5227 unsigned int i;
5228
5229 /* Restore the symbol table. */
5230 old_ent = (char *) old_tab + tabsize;
5231 memset (elf_sym_hashes (abfd), 0,
5232 extsymcount * sizeof (struct elf_link_hash_entry *));
5233 htab->root.table.table = old_table;
5234 htab->root.table.size = old_size;
5235 htab->root.table.count = old_count;
5236 memcpy (htab->root.table.table, old_tab, tabsize);
5237 htab->root.undefs = old_undefs;
5238 htab->root.undefs_tail = old_undefs_tail;
5239 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5240 free (old_strtab);
5241 old_strtab = NULL;
5242 for (i = 0; i < htab->root.table.size; i++)
5243 {
5244 struct bfd_hash_entry *p;
5245 struct elf_link_hash_entry *h;
5246 bfd_size_type size;
5247 unsigned int alignment_power;
5248 unsigned int non_ir_ref_dynamic;
5249
5250 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5251 {
5252 h = (struct elf_link_hash_entry *) p;
5253 if (h->root.type == bfd_link_hash_warning)
5254 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5255
5256 /* Preserve the maximum alignment and size for common
5257 symbols even if this dynamic lib isn't on DT_NEEDED
5258 since it can still be loaded at run time by another
5259 dynamic lib. */
5260 if (h->root.type == bfd_link_hash_common)
5261 {
5262 size = h->root.u.c.size;
5263 alignment_power = h->root.u.c.p->alignment_power;
5264 }
5265 else
5266 {
5267 size = 0;
5268 alignment_power = 0;
5269 }
5270 /* Preserve non_ir_ref_dynamic so that this symbol
5271 will be exported when the dynamic lib becomes needed
5272 in the second pass. */
5273 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5274 memcpy (p, old_ent, htab->root.table.entsize);
5275 old_ent = (char *) old_ent + htab->root.table.entsize;
5276 h = (struct elf_link_hash_entry *) p;
5277 if (h->root.type == bfd_link_hash_warning)
5278 {
5279 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5280 old_ent = (char *) old_ent + htab->root.table.entsize;
5281 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5282 }
5283 if (h->root.type == bfd_link_hash_common)
5284 {
5285 if (size > h->root.u.c.size)
5286 h->root.u.c.size = size;
5287 if (alignment_power > h->root.u.c.p->alignment_power)
5288 h->root.u.c.p->alignment_power = alignment_power;
5289 }
5290 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5291 }
5292 }
5293
5294 /* Make a special call to the linker "notice" function to
5295 tell it that symbols added for crefs may need to be removed. */
5296 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5297 goto error_free_vers;
5298
5299 free (old_tab);
5300 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5301 alloc_mark);
5302 if (nondeflt_vers != NULL)
5303 free (nondeflt_vers);
5304 return TRUE;
5305 }
5306
5307 if (old_tab != NULL)
5308 {
5309 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5310 goto error_free_vers;
5311 free (old_tab);
5312 old_tab = NULL;
5313 }
5314
5315 /* Now that all the symbols from this input file are created, if
5316 not performing a relocatable link, handle .symver foo, foo@BAR
5317 such that any relocs against foo become foo@BAR. */
5318 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5319 {
5320 size_t cnt, symidx;
5321
5322 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5323 {
5324 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5325 char *shortname, *p;
5326
5327 p = strchr (h->root.root.string, ELF_VER_CHR);
5328 if (p == NULL
5329 || (h->root.type != bfd_link_hash_defined
5330 && h->root.type != bfd_link_hash_defweak))
5331 continue;
5332
5333 amt = p - h->root.root.string;
5334 shortname = (char *) bfd_malloc (amt + 1);
5335 if (!shortname)
5336 goto error_free_vers;
5337 memcpy (shortname, h->root.root.string, amt);
5338 shortname[amt] = '\0';
5339
5340 hi = (struct elf_link_hash_entry *)
5341 bfd_link_hash_lookup (&htab->root, shortname,
5342 FALSE, FALSE, FALSE);
5343 if (hi != NULL
5344 && hi->root.type == h->root.type
5345 && hi->root.u.def.value == h->root.u.def.value
5346 && hi->root.u.def.section == h->root.u.def.section)
5347 {
5348 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5349 hi->root.type = bfd_link_hash_indirect;
5350 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5351 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5352 sym_hash = elf_sym_hashes (abfd);
5353 if (sym_hash)
5354 for (symidx = 0; symidx < extsymcount; ++symidx)
5355 if (sym_hash[symidx] == hi)
5356 {
5357 sym_hash[symidx] = h;
5358 break;
5359 }
5360 }
5361 free (shortname);
5362 }
5363 free (nondeflt_vers);
5364 nondeflt_vers = NULL;
5365 }
5366
5367 /* Now set the alias field correctly for all the weak defined
5368 symbols we found. The only way to do this is to search all the
5369 symbols. Since we only need the information for non functions in
5370 dynamic objects, that's the only time we actually put anything on
5371 the list WEAKS. We need this information so that if a regular
5372 object refers to a symbol defined weakly in a dynamic object, the
5373 real symbol in the dynamic object is also put in the dynamic
5374 symbols; we also must arrange for both symbols to point to the
5375 same memory location. We could handle the general case of symbol
5376 aliasing, but a general symbol alias can only be generated in
5377 assembler code, handling it correctly would be very time
5378 consuming, and other ELF linkers don't handle general aliasing
5379 either. */
5380 if (weaks != NULL)
5381 {
5382 struct elf_link_hash_entry **hpp;
5383 struct elf_link_hash_entry **hppend;
5384 struct elf_link_hash_entry **sorted_sym_hash;
5385 struct elf_link_hash_entry *h;
5386 size_t sym_count;
5387
5388 /* Since we have to search the whole symbol list for each weak
5389 defined symbol, search time for N weak defined symbols will be
5390 O(N^2). Binary search will cut it down to O(NlogN). */
5391 amt = extsymcount;
5392 amt *= sizeof (*sorted_sym_hash);
5393 sorted_sym_hash = bfd_malloc (amt);
5394 if (sorted_sym_hash == NULL)
5395 goto error_return;
5396 sym_hash = sorted_sym_hash;
5397 hpp = elf_sym_hashes (abfd);
5398 hppend = hpp + extsymcount;
5399 sym_count = 0;
5400 for (; hpp < hppend; hpp++)
5401 {
5402 h = *hpp;
5403 if (h != NULL
5404 && h->root.type == bfd_link_hash_defined
5405 && !bed->is_function_type (h->type))
5406 {
5407 *sym_hash = h;
5408 sym_hash++;
5409 sym_count++;
5410 }
5411 }
5412
5413 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5414 elf_sort_symbol);
5415
5416 while (weaks != NULL)
5417 {
5418 struct elf_link_hash_entry *hlook;
5419 asection *slook;
5420 bfd_vma vlook;
5421 size_t i, j, idx = 0;
5422
5423 hlook = weaks;
5424 weaks = hlook->u.alias;
5425 hlook->u.alias = NULL;
5426
5427 if (hlook->root.type != bfd_link_hash_defined
5428 && hlook->root.type != bfd_link_hash_defweak)
5429 continue;
5430
5431 slook = hlook->root.u.def.section;
5432 vlook = hlook->root.u.def.value;
5433
5434 i = 0;
5435 j = sym_count;
5436 while (i != j)
5437 {
5438 bfd_signed_vma vdiff;
5439 idx = (i + j) / 2;
5440 h = sorted_sym_hash[idx];
5441 vdiff = vlook - h->root.u.def.value;
5442 if (vdiff < 0)
5443 j = idx;
5444 else if (vdiff > 0)
5445 i = idx + 1;
5446 else
5447 {
5448 int sdiff = slook->id - h->root.u.def.section->id;
5449 if (sdiff < 0)
5450 j = idx;
5451 else if (sdiff > 0)
5452 i = idx + 1;
5453 else
5454 break;
5455 }
5456 }
5457
5458 /* We didn't find a value/section match. */
5459 if (i == j)
5460 continue;
5461
5462 /* With multiple aliases, or when the weak symbol is already
5463 strongly defined, we have multiple matching symbols and
5464 the binary search above may land on any of them. Step
5465 one past the matching symbol(s). */
5466 while (++idx != j)
5467 {
5468 h = sorted_sym_hash[idx];
5469 if (h->root.u.def.section != slook
5470 || h->root.u.def.value != vlook)
5471 break;
5472 }
5473
5474 /* Now look back over the aliases. Since we sorted by size
5475 as well as value and section, we'll choose the one with
5476 the largest size. */
5477 while (idx-- != i)
5478 {
5479 h = sorted_sym_hash[idx];
5480
5481 /* Stop if value or section doesn't match. */
5482 if (h->root.u.def.section != slook
5483 || h->root.u.def.value != vlook)
5484 break;
5485 else if (h != hlook)
5486 {
5487 struct elf_link_hash_entry *t;
5488
5489 hlook->u.alias = h;
5490 hlook->is_weakalias = 1;
5491 t = h;
5492 if (t->u.alias != NULL)
5493 while (t->u.alias != h)
5494 t = t->u.alias;
5495 t->u.alias = hlook;
5496
5497 /* If the weak definition is in the list of dynamic
5498 symbols, make sure the real definition is put
5499 there as well. */
5500 if (hlook->dynindx != -1 && h->dynindx == -1)
5501 {
5502 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5503 {
5504 err_free_sym_hash:
5505 free (sorted_sym_hash);
5506 goto error_return;
5507 }
5508 }
5509
5510 /* If the real definition is in the list of dynamic
5511 symbols, make sure the weak definition is put
5512 there as well. If we don't do this, then the
5513 dynamic loader might not merge the entries for the
5514 real definition and the weak definition. */
5515 if (h->dynindx != -1 && hlook->dynindx == -1)
5516 {
5517 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5518 goto err_free_sym_hash;
5519 }
5520 break;
5521 }
5522 }
5523 }
5524
5525 free (sorted_sym_hash);
5526 }
5527
5528 if (bed->check_directives
5529 && !(*bed->check_directives) (abfd, info))
5530 return FALSE;
5531
5532 /* If this is a non-traditional link, try to optimize the handling
5533 of the .stab/.stabstr sections. */
5534 if (! dynamic
5535 && ! info->traditional_format
5536 && is_elf_hash_table (htab)
5537 && (info->strip != strip_all && info->strip != strip_debugger))
5538 {
5539 asection *stabstr;
5540
5541 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5542 if (stabstr != NULL)
5543 {
5544 bfd_size_type string_offset = 0;
5545 asection *stab;
5546
5547 for (stab = abfd->sections; stab; stab = stab->next)
5548 if (CONST_STRNEQ (stab->name, ".stab")
5549 && (!stab->name[5] ||
5550 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5551 && (stab->flags & SEC_MERGE) == 0
5552 && !bfd_is_abs_section (stab->output_section))
5553 {
5554 struct bfd_elf_section_data *secdata;
5555
5556 secdata = elf_section_data (stab);
5557 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5558 stabstr, &secdata->sec_info,
5559 &string_offset))
5560 goto error_return;
5561 if (secdata->sec_info)
5562 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5563 }
5564 }
5565 }
5566
5567 if (is_elf_hash_table (htab) && add_needed)
5568 {
5569 /* Add this bfd to the loaded list. */
5570 struct elf_link_loaded_list *n;
5571
5572 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5573 if (n == NULL)
5574 goto error_return;
5575 n->abfd = abfd;
5576 n->next = htab->loaded;
5577 htab->loaded = n;
5578 }
5579
5580 return TRUE;
5581
5582 error_free_vers:
5583 if (old_tab != NULL)
5584 free (old_tab);
5585 if (old_strtab != NULL)
5586 free (old_strtab);
5587 if (nondeflt_vers != NULL)
5588 free (nondeflt_vers);
5589 if (extversym != NULL)
5590 free (extversym);
5591 error_free_sym:
5592 if (isymbuf != NULL)
5593 free (isymbuf);
5594 error_return:
5595 return FALSE;
5596 }
5597
5598 /* Return the linker hash table entry of a symbol that might be
5599 satisfied by an archive symbol. Return -1 on error. */
5600
5601 struct elf_link_hash_entry *
5602 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5603 struct bfd_link_info *info,
5604 const char *name)
5605 {
5606 struct elf_link_hash_entry *h;
5607 char *p, *copy;
5608 size_t len, first;
5609
5610 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5611 if (h != NULL)
5612 return h;
5613
5614 /* If this is a default version (the name contains @@), look up the
5615 symbol again with only one `@' as well as without the version.
5616 The effect is that references to the symbol with and without the
5617 version will be matched by the default symbol in the archive. */
5618
5619 p = strchr (name, ELF_VER_CHR);
5620 if (p == NULL || p[1] != ELF_VER_CHR)
5621 return h;
5622
5623 /* First check with only one `@'. */
5624 len = strlen (name);
5625 copy = (char *) bfd_alloc (abfd, len);
5626 if (copy == NULL)
5627 return (struct elf_link_hash_entry *) -1;
5628
5629 first = p - name + 1;
5630 memcpy (copy, name, first);
5631 memcpy (copy + first, name + first + 1, len - first);
5632
5633 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5634 if (h == NULL)
5635 {
5636 /* We also need to check references to the symbol without the
5637 version. */
5638 copy[first - 1] = '\0';
5639 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5640 FALSE, FALSE, TRUE);
5641 }
5642
5643 bfd_release (abfd, copy);
5644 return h;
5645 }
5646
5647 /* Add symbols from an ELF archive file to the linker hash table. We
5648 don't use _bfd_generic_link_add_archive_symbols because we need to
5649 handle versioned symbols.
5650
5651 Fortunately, ELF archive handling is simpler than that done by
5652 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5653 oddities. In ELF, if we find a symbol in the archive map, and the
5654 symbol is currently undefined, we know that we must pull in that
5655 object file.
5656
5657 Unfortunately, we do have to make multiple passes over the symbol
5658 table until nothing further is resolved. */
5659
5660 static bfd_boolean
5661 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5662 {
5663 symindex c;
5664 unsigned char *included = NULL;
5665 carsym *symdefs;
5666 bfd_boolean loop;
5667 bfd_size_type amt;
5668 const struct elf_backend_data *bed;
5669 struct elf_link_hash_entry * (*archive_symbol_lookup)
5670 (bfd *, struct bfd_link_info *, const char *);
5671
5672 if (! bfd_has_map (abfd))
5673 {
5674 /* An empty archive is a special case. */
5675 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5676 return TRUE;
5677 bfd_set_error (bfd_error_no_armap);
5678 return FALSE;
5679 }
5680
5681 /* Keep track of all symbols we know to be already defined, and all
5682 files we know to be already included. This is to speed up the
5683 second and subsequent passes. */
5684 c = bfd_ardata (abfd)->symdef_count;
5685 if (c == 0)
5686 return TRUE;
5687 amt = c;
5688 amt *= sizeof (*included);
5689 included = (unsigned char *) bfd_zmalloc (amt);
5690 if (included == NULL)
5691 return FALSE;
5692
5693 symdefs = bfd_ardata (abfd)->symdefs;
5694 bed = get_elf_backend_data (abfd);
5695 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5696
5697 do
5698 {
5699 file_ptr last;
5700 symindex i;
5701 carsym *symdef;
5702 carsym *symdefend;
5703
5704 loop = FALSE;
5705 last = -1;
5706
5707 symdef = symdefs;
5708 symdefend = symdef + c;
5709 for (i = 0; symdef < symdefend; symdef++, i++)
5710 {
5711 struct elf_link_hash_entry *h;
5712 bfd *element;
5713 struct bfd_link_hash_entry *undefs_tail;
5714 symindex mark;
5715
5716 if (included[i])
5717 continue;
5718 if (symdef->file_offset == last)
5719 {
5720 included[i] = TRUE;
5721 continue;
5722 }
5723
5724 h = archive_symbol_lookup (abfd, info, symdef->name);
5725 if (h == (struct elf_link_hash_entry *) -1)
5726 goto error_return;
5727
5728 if (h == NULL)
5729 continue;
5730
5731 if (h->root.type == bfd_link_hash_common)
5732 {
5733 /* We currently have a common symbol. The archive map contains
5734 a reference to this symbol, so we may want to include it. We
5735 only want to include it however, if this archive element
5736 contains a definition of the symbol, not just another common
5737 declaration of it.
5738
5739 Unfortunately some archivers (including GNU ar) will put
5740 declarations of common symbols into their archive maps, as
5741 well as real definitions, so we cannot just go by the archive
5742 map alone. Instead we must read in the element's symbol
5743 table and check that to see what kind of symbol definition
5744 this is. */
5745 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5746 continue;
5747 }
5748 else if (h->root.type != bfd_link_hash_undefined)
5749 {
5750 if (h->root.type != bfd_link_hash_undefweak)
5751 /* Symbol must be defined. Don't check it again. */
5752 included[i] = TRUE;
5753 continue;
5754 }
5755
5756 /* We need to include this archive member. */
5757 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5758 if (element == NULL)
5759 goto error_return;
5760
5761 if (! bfd_check_format (element, bfd_object))
5762 goto error_return;
5763
5764 undefs_tail = info->hash->undefs_tail;
5765
5766 if (!(*info->callbacks
5767 ->add_archive_element) (info, element, symdef->name, &element))
5768 continue;
5769 if (!bfd_link_add_symbols (element, info))
5770 goto error_return;
5771
5772 /* If there are any new undefined symbols, we need to make
5773 another pass through the archive in order to see whether
5774 they can be defined. FIXME: This isn't perfect, because
5775 common symbols wind up on undefs_tail and because an
5776 undefined symbol which is defined later on in this pass
5777 does not require another pass. This isn't a bug, but it
5778 does make the code less efficient than it could be. */
5779 if (undefs_tail != info->hash->undefs_tail)
5780 loop = TRUE;
5781
5782 /* Look backward to mark all symbols from this object file
5783 which we have already seen in this pass. */
5784 mark = i;
5785 do
5786 {
5787 included[mark] = TRUE;
5788 if (mark == 0)
5789 break;
5790 --mark;
5791 }
5792 while (symdefs[mark].file_offset == symdef->file_offset);
5793
5794 /* We mark subsequent symbols from this object file as we go
5795 on through the loop. */
5796 last = symdef->file_offset;
5797 }
5798 }
5799 while (loop);
5800
5801 free (included);
5802
5803 return TRUE;
5804
5805 error_return:
5806 if (included != NULL)
5807 free (included);
5808 return FALSE;
5809 }
5810
5811 /* Given an ELF BFD, add symbols to the global hash table as
5812 appropriate. */
5813
5814 bfd_boolean
5815 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5816 {
5817 switch (bfd_get_format (abfd))
5818 {
5819 case bfd_object:
5820 return elf_link_add_object_symbols (abfd, info);
5821 case bfd_archive:
5822 return elf_link_add_archive_symbols (abfd, info);
5823 default:
5824 bfd_set_error (bfd_error_wrong_format);
5825 return FALSE;
5826 }
5827 }
5828
5829 struct hash_codes_info
5831 {
5832 unsigned long *hashcodes;
5833 bfd_boolean error;
5834 };
5835
5836 /* This function will be called though elf_link_hash_traverse to store
5837 all hash value of the exported symbols in an array. */
5838
5839 static bfd_boolean
5840 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5841 {
5842 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5843 const char *name;
5844 unsigned long ha;
5845 char *alc = NULL;
5846
5847 /* Ignore indirect symbols. These are added by the versioning code. */
5848 if (h->dynindx == -1)
5849 return TRUE;
5850
5851 name = h->root.root.string;
5852 if (h->versioned >= versioned)
5853 {
5854 char *p = strchr (name, ELF_VER_CHR);
5855 if (p != NULL)
5856 {
5857 alc = (char *) bfd_malloc (p - name + 1);
5858 if (alc == NULL)
5859 {
5860 inf->error = TRUE;
5861 return FALSE;
5862 }
5863 memcpy (alc, name, p - name);
5864 alc[p - name] = '\0';
5865 name = alc;
5866 }
5867 }
5868
5869 /* Compute the hash value. */
5870 ha = bfd_elf_hash (name);
5871
5872 /* Store the found hash value in the array given as the argument. */
5873 *(inf->hashcodes)++ = ha;
5874
5875 /* And store it in the struct so that we can put it in the hash table
5876 later. */
5877 h->u.elf_hash_value = ha;
5878
5879 if (alc != NULL)
5880 free (alc);
5881
5882 return TRUE;
5883 }
5884
5885 struct collect_gnu_hash_codes
5886 {
5887 bfd *output_bfd;
5888 const struct elf_backend_data *bed;
5889 unsigned long int nsyms;
5890 unsigned long int maskbits;
5891 unsigned long int *hashcodes;
5892 unsigned long int *hashval;
5893 unsigned long int *indx;
5894 unsigned long int *counts;
5895 bfd_vma *bitmask;
5896 bfd_byte *contents;
5897 bfd_size_type xlat;
5898 long int min_dynindx;
5899 unsigned long int bucketcount;
5900 unsigned long int symindx;
5901 long int local_indx;
5902 long int shift1, shift2;
5903 unsigned long int mask;
5904 bfd_boolean error;
5905 };
5906
5907 /* This function will be called though elf_link_hash_traverse to store
5908 all hash value of the exported symbols in an array. */
5909
5910 static bfd_boolean
5911 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5912 {
5913 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5914 const char *name;
5915 unsigned long ha;
5916 char *alc = NULL;
5917
5918 /* Ignore indirect symbols. These are added by the versioning code. */
5919 if (h->dynindx == -1)
5920 return TRUE;
5921
5922 /* Ignore also local symbols and undefined symbols. */
5923 if (! (*s->bed->elf_hash_symbol) (h))
5924 return TRUE;
5925
5926 name = h->root.root.string;
5927 if (h->versioned >= versioned)
5928 {
5929 char *p = strchr (name, ELF_VER_CHR);
5930 if (p != NULL)
5931 {
5932 alc = (char *) bfd_malloc (p - name + 1);
5933 if (alc == NULL)
5934 {
5935 s->error = TRUE;
5936 return FALSE;
5937 }
5938 memcpy (alc, name, p - name);
5939 alc[p - name] = '\0';
5940 name = alc;
5941 }
5942 }
5943
5944 /* Compute the hash value. */
5945 ha = bfd_elf_gnu_hash (name);
5946
5947 /* Store the found hash value in the array for compute_bucket_count,
5948 and also for .dynsym reordering purposes. */
5949 s->hashcodes[s->nsyms] = ha;
5950 s->hashval[h->dynindx] = ha;
5951 ++s->nsyms;
5952 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5953 s->min_dynindx = h->dynindx;
5954
5955 if (alc != NULL)
5956 free (alc);
5957
5958 return TRUE;
5959 }
5960
5961 /* This function will be called though elf_link_hash_traverse to do
5962 final dynamic symbol renumbering in case of .gnu.hash.
5963 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5964 to the translation table. */
5965
5966 static bfd_boolean
5967 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5968 {
5969 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5970 unsigned long int bucket;
5971 unsigned long int val;
5972
5973 /* Ignore indirect symbols. */
5974 if (h->dynindx == -1)
5975 return TRUE;
5976
5977 /* Ignore also local symbols and undefined symbols. */
5978 if (! (*s->bed->elf_hash_symbol) (h))
5979 {
5980 if (h->dynindx >= s->min_dynindx)
5981 {
5982 if (s->bed->record_xhash_symbol != NULL)
5983 {
5984 (*s->bed->record_xhash_symbol) (h, 0);
5985 s->local_indx++;
5986 }
5987 else
5988 h->dynindx = s->local_indx++;
5989 }
5990 return TRUE;
5991 }
5992
5993 bucket = s->hashval[h->dynindx] % s->bucketcount;
5994 val = (s->hashval[h->dynindx] >> s->shift1)
5995 & ((s->maskbits >> s->shift1) - 1);
5996 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5997 s->bitmask[val]
5998 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5999 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6000 if (s->counts[bucket] == 1)
6001 /* Last element terminates the chain. */
6002 val |= 1;
6003 bfd_put_32 (s->output_bfd, val,
6004 s->contents + (s->indx[bucket] - s->symindx) * 4);
6005 --s->counts[bucket];
6006 if (s->bed->record_xhash_symbol != NULL)
6007 {
6008 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6009
6010 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6011 }
6012 else
6013 h->dynindx = s->indx[bucket]++;
6014 return TRUE;
6015 }
6016
6017 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6018
6019 bfd_boolean
6020 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6021 {
6022 return !(h->forced_local
6023 || h->root.type == bfd_link_hash_undefined
6024 || h->root.type == bfd_link_hash_undefweak
6025 || ((h->root.type == bfd_link_hash_defined
6026 || h->root.type == bfd_link_hash_defweak)
6027 && h->root.u.def.section->output_section == NULL));
6028 }
6029
6030 /* Array used to determine the number of hash table buckets to use
6031 based on the number of symbols there are. If there are fewer than
6032 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6033 fewer than 37 we use 17 buckets, and so forth. We never use more
6034 than 32771 buckets. */
6035
6036 static const size_t elf_buckets[] =
6037 {
6038 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6039 16411, 32771, 0
6040 };
6041
6042 /* Compute bucket count for hashing table. We do not use a static set
6043 of possible tables sizes anymore. Instead we determine for all
6044 possible reasonable sizes of the table the outcome (i.e., the
6045 number of collisions etc) and choose the best solution. The
6046 weighting functions are not too simple to allow the table to grow
6047 without bounds. Instead one of the weighting factors is the size.
6048 Therefore the result is always a good payoff between few collisions
6049 (= short chain lengths) and table size. */
6050 static size_t
6051 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6052 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6053 unsigned long int nsyms,
6054 int gnu_hash)
6055 {
6056 size_t best_size = 0;
6057 unsigned long int i;
6058
6059 /* We have a problem here. The following code to optimize the table
6060 size requires an integer type with more the 32 bits. If
6061 BFD_HOST_U_64_BIT is set we know about such a type. */
6062 #ifdef BFD_HOST_U_64_BIT
6063 if (info->optimize)
6064 {
6065 size_t minsize;
6066 size_t maxsize;
6067 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6068 bfd *dynobj = elf_hash_table (info)->dynobj;
6069 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6070 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6071 unsigned long int *counts;
6072 bfd_size_type amt;
6073 unsigned int no_improvement_count = 0;
6074
6075 /* Possible optimization parameters: if we have NSYMS symbols we say
6076 that the hashing table must at least have NSYMS/4 and at most
6077 2*NSYMS buckets. */
6078 minsize = nsyms / 4;
6079 if (minsize == 0)
6080 minsize = 1;
6081 best_size = maxsize = nsyms * 2;
6082 if (gnu_hash)
6083 {
6084 if (minsize < 2)
6085 minsize = 2;
6086 if ((best_size & 31) == 0)
6087 ++best_size;
6088 }
6089
6090 /* Create array where we count the collisions in. We must use bfd_malloc
6091 since the size could be large. */
6092 amt = maxsize;
6093 amt *= sizeof (unsigned long int);
6094 counts = (unsigned long int *) bfd_malloc (amt);
6095 if (counts == NULL)
6096 return 0;
6097
6098 /* Compute the "optimal" size for the hash table. The criteria is a
6099 minimal chain length. The minor criteria is (of course) the size
6100 of the table. */
6101 for (i = minsize; i < maxsize; ++i)
6102 {
6103 /* Walk through the array of hashcodes and count the collisions. */
6104 BFD_HOST_U_64_BIT max;
6105 unsigned long int j;
6106 unsigned long int fact;
6107
6108 if (gnu_hash && (i & 31) == 0)
6109 continue;
6110
6111 memset (counts, '\0', i * sizeof (unsigned long int));
6112
6113 /* Determine how often each hash bucket is used. */
6114 for (j = 0; j < nsyms; ++j)
6115 ++counts[hashcodes[j] % i];
6116
6117 /* For the weight function we need some information about the
6118 pagesize on the target. This is information need not be 100%
6119 accurate. Since this information is not available (so far) we
6120 define it here to a reasonable default value. If it is crucial
6121 to have a better value some day simply define this value. */
6122 # ifndef BFD_TARGET_PAGESIZE
6123 # define BFD_TARGET_PAGESIZE (4096)
6124 # endif
6125
6126 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6127 and the chains. */
6128 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6129
6130 # if 1
6131 /* Variant 1: optimize for short chains. We add the squares
6132 of all the chain lengths (which favors many small chain
6133 over a few long chains). */
6134 for (j = 0; j < i; ++j)
6135 max += counts[j] * counts[j];
6136
6137 /* This adds penalties for the overall size of the table. */
6138 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6139 max *= fact * fact;
6140 # else
6141 /* Variant 2: Optimize a lot more for small table. Here we
6142 also add squares of the size but we also add penalties for
6143 empty slots (the +1 term). */
6144 for (j = 0; j < i; ++j)
6145 max += (1 + counts[j]) * (1 + counts[j]);
6146
6147 /* The overall size of the table is considered, but not as
6148 strong as in variant 1, where it is squared. */
6149 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6150 max *= fact;
6151 # endif
6152
6153 /* Compare with current best results. */
6154 if (max < best_chlen)
6155 {
6156 best_chlen = max;
6157 best_size = i;
6158 no_improvement_count = 0;
6159 }
6160 /* PR 11843: Avoid futile long searches for the best bucket size
6161 when there are a large number of symbols. */
6162 else if (++no_improvement_count == 100)
6163 break;
6164 }
6165
6166 free (counts);
6167 }
6168 else
6169 #endif /* defined (BFD_HOST_U_64_BIT) */
6170 {
6171 /* This is the fallback solution if no 64bit type is available or if we
6172 are not supposed to spend much time on optimizations. We select the
6173 bucket count using a fixed set of numbers. */
6174 for (i = 0; elf_buckets[i] != 0; i++)
6175 {
6176 best_size = elf_buckets[i];
6177 if (nsyms < elf_buckets[i + 1])
6178 break;
6179 }
6180 if (gnu_hash && best_size < 2)
6181 best_size = 2;
6182 }
6183
6184 return best_size;
6185 }
6186
6187 /* Size any SHT_GROUP section for ld -r. */
6188
6189 bfd_boolean
6190 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6191 {
6192 bfd *ibfd;
6193 asection *s;
6194
6195 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6196 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6197 && (s = ibfd->sections) != NULL
6198 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6199 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6200 return FALSE;
6201 return TRUE;
6202 }
6203
6204 /* Set a default stack segment size. The value in INFO wins. If it
6205 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6206 undefined it is initialized. */
6207
6208 bfd_boolean
6209 bfd_elf_stack_segment_size (bfd *output_bfd,
6210 struct bfd_link_info *info,
6211 const char *legacy_symbol,
6212 bfd_vma default_size)
6213 {
6214 struct elf_link_hash_entry *h = NULL;
6215
6216 /* Look for legacy symbol. */
6217 if (legacy_symbol)
6218 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6219 FALSE, FALSE, FALSE);
6220 if (h && (h->root.type == bfd_link_hash_defined
6221 || h->root.type == bfd_link_hash_defweak)
6222 && h->def_regular
6223 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6224 {
6225 /* The symbol has no type if specified on the command line. */
6226 h->type = STT_OBJECT;
6227 if (info->stacksize)
6228 /* xgettext:c-format */
6229 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6230 output_bfd, legacy_symbol);
6231 else if (h->root.u.def.section != bfd_abs_section_ptr)
6232 /* xgettext:c-format */
6233 _bfd_error_handler (_("%pB: %s not absolute"),
6234 output_bfd, legacy_symbol);
6235 else
6236 info->stacksize = h->root.u.def.value;
6237 }
6238
6239 if (!info->stacksize)
6240 /* If the user didn't set a size, or explicitly inhibit the
6241 size, set it now. */
6242 info->stacksize = default_size;
6243
6244 /* Provide the legacy symbol, if it is referenced. */
6245 if (h && (h->root.type == bfd_link_hash_undefined
6246 || h->root.type == bfd_link_hash_undefweak))
6247 {
6248 struct bfd_link_hash_entry *bh = NULL;
6249
6250 if (!(_bfd_generic_link_add_one_symbol
6251 (info, output_bfd, legacy_symbol,
6252 BSF_GLOBAL, bfd_abs_section_ptr,
6253 info->stacksize >= 0 ? info->stacksize : 0,
6254 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6255 return FALSE;
6256
6257 h = (struct elf_link_hash_entry *) bh;
6258 h->def_regular = 1;
6259 h->type = STT_OBJECT;
6260 }
6261
6262 return TRUE;
6263 }
6264
6265 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6266
6267 struct elf_gc_sweep_symbol_info
6268 {
6269 struct bfd_link_info *info;
6270 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6271 bfd_boolean);
6272 };
6273
6274 static bfd_boolean
6275 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6276 {
6277 if (!h->mark
6278 && (((h->root.type == bfd_link_hash_defined
6279 || h->root.type == bfd_link_hash_defweak)
6280 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6281 && h->root.u.def.section->gc_mark))
6282 || h->root.type == bfd_link_hash_undefined
6283 || h->root.type == bfd_link_hash_undefweak))
6284 {
6285 struct elf_gc_sweep_symbol_info *inf;
6286
6287 inf = (struct elf_gc_sweep_symbol_info *) data;
6288 (*inf->hide_symbol) (inf->info, h, TRUE);
6289 h->def_regular = 0;
6290 h->ref_regular = 0;
6291 h->ref_regular_nonweak = 0;
6292 }
6293
6294 return TRUE;
6295 }
6296
6297 /* Set up the sizes and contents of the ELF dynamic sections. This is
6298 called by the ELF linker emulation before_allocation routine. We
6299 must set the sizes of the sections before the linker sets the
6300 addresses of the various sections. */
6301
6302 bfd_boolean
6303 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6304 const char *soname,
6305 const char *rpath,
6306 const char *filter_shlib,
6307 const char *audit,
6308 const char *depaudit,
6309 const char * const *auxiliary_filters,
6310 struct bfd_link_info *info,
6311 asection **sinterpptr)
6312 {
6313 bfd *dynobj;
6314 const struct elf_backend_data *bed;
6315
6316 *sinterpptr = NULL;
6317
6318 if (!is_elf_hash_table (info->hash))
6319 return TRUE;
6320
6321 dynobj = elf_hash_table (info)->dynobj;
6322
6323 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6324 {
6325 struct bfd_elf_version_tree *verdefs;
6326 struct elf_info_failed asvinfo;
6327 struct bfd_elf_version_tree *t;
6328 struct bfd_elf_version_expr *d;
6329 asection *s;
6330 size_t soname_indx;
6331
6332 /* If we are supposed to export all symbols into the dynamic symbol
6333 table (this is not the normal case), then do so. */
6334 if (info->export_dynamic
6335 || (bfd_link_executable (info) && info->dynamic))
6336 {
6337 struct elf_info_failed eif;
6338
6339 eif.info = info;
6340 eif.failed = FALSE;
6341 elf_link_hash_traverse (elf_hash_table (info),
6342 _bfd_elf_export_symbol,
6343 &eif);
6344 if (eif.failed)
6345 return FALSE;
6346 }
6347
6348 if (soname != NULL)
6349 {
6350 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6351 soname, TRUE);
6352 if (soname_indx == (size_t) -1
6353 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6354 return FALSE;
6355 }
6356 else
6357 soname_indx = (size_t) -1;
6358
6359 /* Make all global versions with definition. */
6360 for (t = info->version_info; t != NULL; t = t->next)
6361 for (d = t->globals.list; d != NULL; d = d->next)
6362 if (!d->symver && d->literal)
6363 {
6364 const char *verstr, *name;
6365 size_t namelen, verlen, newlen;
6366 char *newname, *p, leading_char;
6367 struct elf_link_hash_entry *newh;
6368
6369 leading_char = bfd_get_symbol_leading_char (output_bfd);
6370 name = d->pattern;
6371 namelen = strlen (name) + (leading_char != '\0');
6372 verstr = t->name;
6373 verlen = strlen (verstr);
6374 newlen = namelen + verlen + 3;
6375
6376 newname = (char *) bfd_malloc (newlen);
6377 if (newname == NULL)
6378 return FALSE;
6379 newname[0] = leading_char;
6380 memcpy (newname + (leading_char != '\0'), name, namelen);
6381
6382 /* Check the hidden versioned definition. */
6383 p = newname + namelen;
6384 *p++ = ELF_VER_CHR;
6385 memcpy (p, verstr, verlen + 1);
6386 newh = elf_link_hash_lookup (elf_hash_table (info),
6387 newname, FALSE, FALSE,
6388 FALSE);
6389 if (newh == NULL
6390 || (newh->root.type != bfd_link_hash_defined
6391 && newh->root.type != bfd_link_hash_defweak))
6392 {
6393 /* Check the default versioned definition. */
6394 *p++ = ELF_VER_CHR;
6395 memcpy (p, verstr, verlen + 1);
6396 newh = elf_link_hash_lookup (elf_hash_table (info),
6397 newname, FALSE, FALSE,
6398 FALSE);
6399 }
6400 free (newname);
6401
6402 /* Mark this version if there is a definition and it is
6403 not defined in a shared object. */
6404 if (newh != NULL
6405 && !newh->def_dynamic
6406 && (newh->root.type == bfd_link_hash_defined
6407 || newh->root.type == bfd_link_hash_defweak))
6408 d->symver = 1;
6409 }
6410
6411 /* Attach all the symbols to their version information. */
6412 asvinfo.info = info;
6413 asvinfo.failed = FALSE;
6414
6415 elf_link_hash_traverse (elf_hash_table (info),
6416 _bfd_elf_link_assign_sym_version,
6417 &asvinfo);
6418 if (asvinfo.failed)
6419 return FALSE;
6420
6421 if (!info->allow_undefined_version)
6422 {
6423 /* Check if all global versions have a definition. */
6424 bfd_boolean all_defined = TRUE;
6425 for (t = info->version_info; t != NULL; t = t->next)
6426 for (d = t->globals.list; d != NULL; d = d->next)
6427 if (d->literal && !d->symver && !d->script)
6428 {
6429 _bfd_error_handler
6430 (_("%s: undefined version: %s"),
6431 d->pattern, t->name);
6432 all_defined = FALSE;
6433 }
6434
6435 if (!all_defined)
6436 {
6437 bfd_set_error (bfd_error_bad_value);
6438 return FALSE;
6439 }
6440 }
6441
6442 /* Set up the version definition section. */
6443 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6444 BFD_ASSERT (s != NULL);
6445
6446 /* We may have created additional version definitions if we are
6447 just linking a regular application. */
6448 verdefs = info->version_info;
6449
6450 /* Skip anonymous version tag. */
6451 if (verdefs != NULL && verdefs->vernum == 0)
6452 verdefs = verdefs->next;
6453
6454 if (verdefs == NULL && !info->create_default_symver)
6455 s->flags |= SEC_EXCLUDE;
6456 else
6457 {
6458 unsigned int cdefs;
6459 bfd_size_type size;
6460 bfd_byte *p;
6461 Elf_Internal_Verdef def;
6462 Elf_Internal_Verdaux defaux;
6463 struct bfd_link_hash_entry *bh;
6464 struct elf_link_hash_entry *h;
6465 const char *name;
6466
6467 cdefs = 0;
6468 size = 0;
6469
6470 /* Make space for the base version. */
6471 size += sizeof (Elf_External_Verdef);
6472 size += sizeof (Elf_External_Verdaux);
6473 ++cdefs;
6474
6475 /* Make space for the default version. */
6476 if (info->create_default_symver)
6477 {
6478 size += sizeof (Elf_External_Verdef);
6479 ++cdefs;
6480 }
6481
6482 for (t = verdefs; t != NULL; t = t->next)
6483 {
6484 struct bfd_elf_version_deps *n;
6485
6486 /* Don't emit base version twice. */
6487 if (t->vernum == 0)
6488 continue;
6489
6490 size += sizeof (Elf_External_Verdef);
6491 size += sizeof (Elf_External_Verdaux);
6492 ++cdefs;
6493
6494 for (n = t->deps; n != NULL; n = n->next)
6495 size += sizeof (Elf_External_Verdaux);
6496 }
6497
6498 s->size = size;
6499 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6500 if (s->contents == NULL && s->size != 0)
6501 return FALSE;
6502
6503 /* Fill in the version definition section. */
6504
6505 p = s->contents;
6506
6507 def.vd_version = VER_DEF_CURRENT;
6508 def.vd_flags = VER_FLG_BASE;
6509 def.vd_ndx = 1;
6510 def.vd_cnt = 1;
6511 if (info->create_default_symver)
6512 {
6513 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6514 def.vd_next = sizeof (Elf_External_Verdef);
6515 }
6516 else
6517 {
6518 def.vd_aux = sizeof (Elf_External_Verdef);
6519 def.vd_next = (sizeof (Elf_External_Verdef)
6520 + sizeof (Elf_External_Verdaux));
6521 }
6522
6523 if (soname_indx != (size_t) -1)
6524 {
6525 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6526 soname_indx);
6527 def.vd_hash = bfd_elf_hash (soname);
6528 defaux.vda_name = soname_indx;
6529 name = soname;
6530 }
6531 else
6532 {
6533 size_t indx;
6534
6535 name = lbasename (output_bfd->filename);
6536 def.vd_hash = bfd_elf_hash (name);
6537 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6538 name, FALSE);
6539 if (indx == (size_t) -1)
6540 return FALSE;
6541 defaux.vda_name = indx;
6542 }
6543 defaux.vda_next = 0;
6544
6545 _bfd_elf_swap_verdef_out (output_bfd, &def,
6546 (Elf_External_Verdef *) p);
6547 p += sizeof (Elf_External_Verdef);
6548 if (info->create_default_symver)
6549 {
6550 /* Add a symbol representing this version. */
6551 bh = NULL;
6552 if (! (_bfd_generic_link_add_one_symbol
6553 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6554 0, NULL, FALSE,
6555 get_elf_backend_data (dynobj)->collect, &bh)))
6556 return FALSE;
6557 h = (struct elf_link_hash_entry *) bh;
6558 h->non_elf = 0;
6559 h->def_regular = 1;
6560 h->type = STT_OBJECT;
6561 h->verinfo.vertree = NULL;
6562
6563 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6564 return FALSE;
6565
6566 /* Create a duplicate of the base version with the same
6567 aux block, but different flags. */
6568 def.vd_flags = 0;
6569 def.vd_ndx = 2;
6570 def.vd_aux = sizeof (Elf_External_Verdef);
6571 if (verdefs)
6572 def.vd_next = (sizeof (Elf_External_Verdef)
6573 + sizeof (Elf_External_Verdaux));
6574 else
6575 def.vd_next = 0;
6576 _bfd_elf_swap_verdef_out (output_bfd, &def,
6577 (Elf_External_Verdef *) p);
6578 p += sizeof (Elf_External_Verdef);
6579 }
6580 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6581 (Elf_External_Verdaux *) p);
6582 p += sizeof (Elf_External_Verdaux);
6583
6584 for (t = verdefs; t != NULL; t = t->next)
6585 {
6586 unsigned int cdeps;
6587 struct bfd_elf_version_deps *n;
6588
6589 /* Don't emit the base version twice. */
6590 if (t->vernum == 0)
6591 continue;
6592
6593 cdeps = 0;
6594 for (n = t->deps; n != NULL; n = n->next)
6595 ++cdeps;
6596
6597 /* Add a symbol representing this version. */
6598 bh = NULL;
6599 if (! (_bfd_generic_link_add_one_symbol
6600 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6601 0, NULL, FALSE,
6602 get_elf_backend_data (dynobj)->collect, &bh)))
6603 return FALSE;
6604 h = (struct elf_link_hash_entry *) bh;
6605 h->non_elf = 0;
6606 h->def_regular = 1;
6607 h->type = STT_OBJECT;
6608 h->verinfo.vertree = t;
6609
6610 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6611 return FALSE;
6612
6613 def.vd_version = VER_DEF_CURRENT;
6614 def.vd_flags = 0;
6615 if (t->globals.list == NULL
6616 && t->locals.list == NULL
6617 && ! t->used)
6618 def.vd_flags |= VER_FLG_WEAK;
6619 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6620 def.vd_cnt = cdeps + 1;
6621 def.vd_hash = bfd_elf_hash (t->name);
6622 def.vd_aux = sizeof (Elf_External_Verdef);
6623 def.vd_next = 0;
6624
6625 /* If a basever node is next, it *must* be the last node in
6626 the chain, otherwise Verdef construction breaks. */
6627 if (t->next != NULL && t->next->vernum == 0)
6628 BFD_ASSERT (t->next->next == NULL);
6629
6630 if (t->next != NULL && t->next->vernum != 0)
6631 def.vd_next = (sizeof (Elf_External_Verdef)
6632 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6633
6634 _bfd_elf_swap_verdef_out (output_bfd, &def,
6635 (Elf_External_Verdef *) p);
6636 p += sizeof (Elf_External_Verdef);
6637
6638 defaux.vda_name = h->dynstr_index;
6639 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6640 h->dynstr_index);
6641 defaux.vda_next = 0;
6642 if (t->deps != NULL)
6643 defaux.vda_next = sizeof (Elf_External_Verdaux);
6644 t->name_indx = defaux.vda_name;
6645
6646 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6647 (Elf_External_Verdaux *) p);
6648 p += sizeof (Elf_External_Verdaux);
6649
6650 for (n = t->deps; n != NULL; n = n->next)
6651 {
6652 if (n->version_needed == NULL)
6653 {
6654 /* This can happen if there was an error in the
6655 version script. */
6656 defaux.vda_name = 0;
6657 }
6658 else
6659 {
6660 defaux.vda_name = n->version_needed->name_indx;
6661 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6662 defaux.vda_name);
6663 }
6664 if (n->next == NULL)
6665 defaux.vda_next = 0;
6666 else
6667 defaux.vda_next = sizeof (Elf_External_Verdaux);
6668
6669 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6670 (Elf_External_Verdaux *) p);
6671 p += sizeof (Elf_External_Verdaux);
6672 }
6673 }
6674
6675 elf_tdata (output_bfd)->cverdefs = cdefs;
6676 }
6677 }
6678
6679 bed = get_elf_backend_data (output_bfd);
6680
6681 if (info->gc_sections && bed->can_gc_sections)
6682 {
6683 struct elf_gc_sweep_symbol_info sweep_info;
6684
6685 /* Remove the symbols that were in the swept sections from the
6686 dynamic symbol table. */
6687 sweep_info.info = info;
6688 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6689 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6690 &sweep_info);
6691 }
6692
6693 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6694 {
6695 asection *s;
6696 struct elf_find_verdep_info sinfo;
6697
6698 /* Work out the size of the version reference section. */
6699
6700 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6701 BFD_ASSERT (s != NULL);
6702
6703 sinfo.info = info;
6704 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6705 if (sinfo.vers == 0)
6706 sinfo.vers = 1;
6707 sinfo.failed = FALSE;
6708
6709 elf_link_hash_traverse (elf_hash_table (info),
6710 _bfd_elf_link_find_version_dependencies,
6711 &sinfo);
6712 if (sinfo.failed)
6713 return FALSE;
6714
6715 if (elf_tdata (output_bfd)->verref == NULL)
6716 s->flags |= SEC_EXCLUDE;
6717 else
6718 {
6719 Elf_Internal_Verneed *vn;
6720 unsigned int size;
6721 unsigned int crefs;
6722 bfd_byte *p;
6723
6724 /* Build the version dependency section. */
6725 size = 0;
6726 crefs = 0;
6727 for (vn = elf_tdata (output_bfd)->verref;
6728 vn != NULL;
6729 vn = vn->vn_nextref)
6730 {
6731 Elf_Internal_Vernaux *a;
6732
6733 size += sizeof (Elf_External_Verneed);
6734 ++crefs;
6735 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6736 size += sizeof (Elf_External_Vernaux);
6737 }
6738
6739 s->size = size;
6740 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6741 if (s->contents == NULL)
6742 return FALSE;
6743
6744 p = s->contents;
6745 for (vn = elf_tdata (output_bfd)->verref;
6746 vn != NULL;
6747 vn = vn->vn_nextref)
6748 {
6749 unsigned int caux;
6750 Elf_Internal_Vernaux *a;
6751 size_t indx;
6752
6753 caux = 0;
6754 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6755 ++caux;
6756
6757 vn->vn_version = VER_NEED_CURRENT;
6758 vn->vn_cnt = caux;
6759 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6760 elf_dt_name (vn->vn_bfd) != NULL
6761 ? elf_dt_name (vn->vn_bfd)
6762 : lbasename (vn->vn_bfd->filename),
6763 FALSE);
6764 if (indx == (size_t) -1)
6765 return FALSE;
6766 vn->vn_file = indx;
6767 vn->vn_aux = sizeof (Elf_External_Verneed);
6768 if (vn->vn_nextref == NULL)
6769 vn->vn_next = 0;
6770 else
6771 vn->vn_next = (sizeof (Elf_External_Verneed)
6772 + caux * sizeof (Elf_External_Vernaux));
6773
6774 _bfd_elf_swap_verneed_out (output_bfd, vn,
6775 (Elf_External_Verneed *) p);
6776 p += sizeof (Elf_External_Verneed);
6777
6778 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6779 {
6780 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6781 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6782 a->vna_nodename, FALSE);
6783 if (indx == (size_t) -1)
6784 return FALSE;
6785 a->vna_name = indx;
6786 if (a->vna_nextptr == NULL)
6787 a->vna_next = 0;
6788 else
6789 a->vna_next = sizeof (Elf_External_Vernaux);
6790
6791 _bfd_elf_swap_vernaux_out (output_bfd, a,
6792 (Elf_External_Vernaux *) p);
6793 p += sizeof (Elf_External_Vernaux);
6794 }
6795 }
6796
6797 elf_tdata (output_bfd)->cverrefs = crefs;
6798 }
6799 }
6800
6801 /* Any syms created from now on start with -1 in
6802 got.refcount/offset and plt.refcount/offset. */
6803 elf_hash_table (info)->init_got_refcount
6804 = elf_hash_table (info)->init_got_offset;
6805 elf_hash_table (info)->init_plt_refcount
6806 = elf_hash_table (info)->init_plt_offset;
6807
6808 if (bfd_link_relocatable (info)
6809 && !_bfd_elf_size_group_sections (info))
6810 return FALSE;
6811
6812 /* The backend may have to create some sections regardless of whether
6813 we're dynamic or not. */
6814 if (bed->elf_backend_always_size_sections
6815 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6816 return FALSE;
6817
6818 /* Determine any GNU_STACK segment requirements, after the backend
6819 has had a chance to set a default segment size. */
6820 if (info->execstack)
6821 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6822 else if (info->noexecstack)
6823 elf_stack_flags (output_bfd) = PF_R | PF_W;
6824 else
6825 {
6826 bfd *inputobj;
6827 asection *notesec = NULL;
6828 int exec = 0;
6829
6830 for (inputobj = info->input_bfds;
6831 inputobj;
6832 inputobj = inputobj->link.next)
6833 {
6834 asection *s;
6835
6836 if (inputobj->flags
6837 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6838 continue;
6839 s = inputobj->sections;
6840 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6841 continue;
6842
6843 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6844 if (s)
6845 {
6846 if (s->flags & SEC_CODE)
6847 exec = PF_X;
6848 notesec = s;
6849 }
6850 else if (bed->default_execstack)
6851 exec = PF_X;
6852 }
6853 if (notesec || info->stacksize > 0)
6854 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6855 if (notesec && exec && bfd_link_relocatable (info)
6856 && notesec->output_section != bfd_abs_section_ptr)
6857 notesec->output_section->flags |= SEC_CODE;
6858 }
6859
6860 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6861 {
6862 struct elf_info_failed eif;
6863 struct elf_link_hash_entry *h;
6864 asection *dynstr;
6865 asection *s;
6866
6867 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6868 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6869
6870 if (info->symbolic)
6871 {
6872 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6873 return FALSE;
6874 info->flags |= DF_SYMBOLIC;
6875 }
6876
6877 if (rpath != NULL)
6878 {
6879 size_t indx;
6880 bfd_vma tag;
6881
6882 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6883 TRUE);
6884 if (indx == (size_t) -1)
6885 return FALSE;
6886
6887 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6888 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6889 return FALSE;
6890 }
6891
6892 if (filter_shlib != NULL)
6893 {
6894 size_t indx;
6895
6896 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6897 filter_shlib, TRUE);
6898 if (indx == (size_t) -1
6899 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6900 return FALSE;
6901 }
6902
6903 if (auxiliary_filters != NULL)
6904 {
6905 const char * const *p;
6906
6907 for (p = auxiliary_filters; *p != NULL; p++)
6908 {
6909 size_t indx;
6910
6911 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6912 *p, TRUE);
6913 if (indx == (size_t) -1
6914 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6915 return FALSE;
6916 }
6917 }
6918
6919 if (audit != NULL)
6920 {
6921 size_t indx;
6922
6923 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6924 TRUE);
6925 if (indx == (size_t) -1
6926 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6927 return FALSE;
6928 }
6929
6930 if (depaudit != NULL)
6931 {
6932 size_t indx;
6933
6934 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6935 TRUE);
6936 if (indx == (size_t) -1
6937 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6938 return FALSE;
6939 }
6940
6941 eif.info = info;
6942 eif.failed = FALSE;
6943
6944 /* Find all symbols which were defined in a dynamic object and make
6945 the backend pick a reasonable value for them. */
6946 elf_link_hash_traverse (elf_hash_table (info),
6947 _bfd_elf_adjust_dynamic_symbol,
6948 &eif);
6949 if (eif.failed)
6950 return FALSE;
6951
6952 /* Add some entries to the .dynamic section. We fill in some of the
6953 values later, in bfd_elf_final_link, but we must add the entries
6954 now so that we know the final size of the .dynamic section. */
6955
6956 /* If there are initialization and/or finalization functions to
6957 call then add the corresponding DT_INIT/DT_FINI entries. */
6958 h = (info->init_function
6959 ? elf_link_hash_lookup (elf_hash_table (info),
6960 info->init_function, FALSE,
6961 FALSE, FALSE)
6962 : NULL);
6963 if (h != NULL
6964 && (h->ref_regular
6965 || h->def_regular))
6966 {
6967 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6968 return FALSE;
6969 }
6970 h = (info->fini_function
6971 ? elf_link_hash_lookup (elf_hash_table (info),
6972 info->fini_function, FALSE,
6973 FALSE, FALSE)
6974 : NULL);
6975 if (h != NULL
6976 && (h->ref_regular
6977 || h->def_regular))
6978 {
6979 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6980 return FALSE;
6981 }
6982
6983 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6984 if (s != NULL && s->linker_has_input)
6985 {
6986 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6987 if (! bfd_link_executable (info))
6988 {
6989 bfd *sub;
6990 asection *o;
6991
6992 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6993 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6994 && (o = sub->sections) != NULL
6995 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6996 for (o = sub->sections; o != NULL; o = o->next)
6997 if (elf_section_data (o)->this_hdr.sh_type
6998 == SHT_PREINIT_ARRAY)
6999 {
7000 _bfd_error_handler
7001 (_("%pB: .preinit_array section is not allowed in DSO"),
7002 sub);
7003 break;
7004 }
7005
7006 bfd_set_error (bfd_error_nonrepresentable_section);
7007 return FALSE;
7008 }
7009
7010 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7011 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7012 return FALSE;
7013 }
7014 s = bfd_get_section_by_name (output_bfd, ".init_array");
7015 if (s != NULL && s->linker_has_input)
7016 {
7017 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7018 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7019 return FALSE;
7020 }
7021 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7022 if (s != NULL && s->linker_has_input)
7023 {
7024 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7025 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7026 return FALSE;
7027 }
7028
7029 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7030 /* If .dynstr is excluded from the link, we don't want any of
7031 these tags. Strictly, we should be checking each section
7032 individually; This quick check covers for the case where
7033 someone does a /DISCARD/ : { *(*) }. */
7034 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7035 {
7036 bfd_size_type strsize;
7037
7038 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7039 if ((info->emit_hash
7040 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7041 || (info->emit_gnu_hash
7042 && (bed->record_xhash_symbol == NULL
7043 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7044 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7045 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7046 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7047 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7048 bed->s->sizeof_sym))
7049 return FALSE;
7050 }
7051 }
7052
7053 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7054 return FALSE;
7055
7056 /* The backend must work out the sizes of all the other dynamic
7057 sections. */
7058 if (dynobj != NULL
7059 && bed->elf_backend_size_dynamic_sections != NULL
7060 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7061 return FALSE;
7062
7063 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7064 {
7065 if (elf_tdata (output_bfd)->cverdefs)
7066 {
7067 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7068
7069 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7070 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7071 return FALSE;
7072 }
7073
7074 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7075 {
7076 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7077 return FALSE;
7078 }
7079 else if (info->flags & DF_BIND_NOW)
7080 {
7081 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7082 return FALSE;
7083 }
7084
7085 if (info->flags_1)
7086 {
7087 if (bfd_link_executable (info))
7088 info->flags_1 &= ~ (DF_1_INITFIRST
7089 | DF_1_NODELETE
7090 | DF_1_NOOPEN);
7091 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7092 return FALSE;
7093 }
7094
7095 if (elf_tdata (output_bfd)->cverrefs)
7096 {
7097 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7098
7099 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7100 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7101 return FALSE;
7102 }
7103
7104 if ((elf_tdata (output_bfd)->cverrefs == 0
7105 && elf_tdata (output_bfd)->cverdefs == 0)
7106 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7107 {
7108 asection *s;
7109
7110 s = bfd_get_linker_section (dynobj, ".gnu.version");
7111 s->flags |= SEC_EXCLUDE;
7112 }
7113 }
7114 return TRUE;
7115 }
7116
7117 /* Find the first non-excluded output section. We'll use its
7118 section symbol for some emitted relocs. */
7119 void
7120 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7121 {
7122 asection *s;
7123 asection *found = NULL;
7124
7125 for (s = output_bfd->sections; s != NULL; s = s->next)
7126 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7127 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7128 {
7129 found = s;
7130 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7131 break;
7132 }
7133 elf_hash_table (info)->text_index_section = found;
7134 }
7135
7136 /* Find two non-excluded output sections, one for code, one for data.
7137 We'll use their section symbols for some emitted relocs. */
7138 void
7139 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7140 {
7141 asection *s;
7142 asection *found = NULL;
7143
7144 /* Data first, since setting text_index_section changes
7145 _bfd_elf_omit_section_dynsym_default. */
7146 for (s = output_bfd->sections; s != NULL; s = s->next)
7147 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7148 && !(s->flags & SEC_READONLY)
7149 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7150 {
7151 found = s;
7152 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7153 break;
7154 }
7155 elf_hash_table (info)->data_index_section = found;
7156
7157 for (s = output_bfd->sections; s != NULL; s = s->next)
7158 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7159 && (s->flags & SEC_READONLY)
7160 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7161 {
7162 found = s;
7163 break;
7164 }
7165 elf_hash_table (info)->text_index_section = found;
7166 }
7167
7168 #define GNU_HASH_SECTION_NAME(bed) \
7169 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7170
7171 bfd_boolean
7172 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7173 {
7174 const struct elf_backend_data *bed;
7175 unsigned long section_sym_count;
7176 bfd_size_type dynsymcount = 0;
7177
7178 if (!is_elf_hash_table (info->hash))
7179 return TRUE;
7180
7181 bed = get_elf_backend_data (output_bfd);
7182 (*bed->elf_backend_init_index_section) (output_bfd, info);
7183
7184 /* Assign dynsym indices. In a shared library we generate a section
7185 symbol for each output section, which come first. Next come all
7186 of the back-end allocated local dynamic syms, followed by the rest
7187 of the global symbols.
7188
7189 This is usually not needed for static binaries, however backends
7190 can request to always do it, e.g. the MIPS backend uses dynamic
7191 symbol counts to lay out GOT, which will be produced in the
7192 presence of GOT relocations even in static binaries (holding fixed
7193 data in that case, to satisfy those relocations). */
7194
7195 if (elf_hash_table (info)->dynamic_sections_created
7196 || bed->always_renumber_dynsyms)
7197 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7198 §ion_sym_count);
7199
7200 if (elf_hash_table (info)->dynamic_sections_created)
7201 {
7202 bfd *dynobj;
7203 asection *s;
7204 unsigned int dtagcount;
7205
7206 dynobj = elf_hash_table (info)->dynobj;
7207
7208 /* Work out the size of the symbol version section. */
7209 s = bfd_get_linker_section (dynobj, ".gnu.version");
7210 BFD_ASSERT (s != NULL);
7211 if ((s->flags & SEC_EXCLUDE) == 0)
7212 {
7213 s->size = dynsymcount * sizeof (Elf_External_Versym);
7214 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7215 if (s->contents == NULL)
7216 return FALSE;
7217
7218 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7219 return FALSE;
7220 }
7221
7222 /* Set the size of the .dynsym and .hash sections. We counted
7223 the number of dynamic symbols in elf_link_add_object_symbols.
7224 We will build the contents of .dynsym and .hash when we build
7225 the final symbol table, because until then we do not know the
7226 correct value to give the symbols. We built the .dynstr
7227 section as we went along in elf_link_add_object_symbols. */
7228 s = elf_hash_table (info)->dynsym;
7229 BFD_ASSERT (s != NULL);
7230 s->size = dynsymcount * bed->s->sizeof_sym;
7231
7232 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7233 if (s->contents == NULL)
7234 return FALSE;
7235
7236 /* The first entry in .dynsym is a dummy symbol. Clear all the
7237 section syms, in case we don't output them all. */
7238 ++section_sym_count;
7239 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7240
7241 elf_hash_table (info)->bucketcount = 0;
7242
7243 /* Compute the size of the hashing table. As a side effect this
7244 computes the hash values for all the names we export. */
7245 if (info->emit_hash)
7246 {
7247 unsigned long int *hashcodes;
7248 struct hash_codes_info hashinf;
7249 bfd_size_type amt;
7250 unsigned long int nsyms;
7251 size_t bucketcount;
7252 size_t hash_entry_size;
7253
7254 /* Compute the hash values for all exported symbols. At the same
7255 time store the values in an array so that we could use them for
7256 optimizations. */
7257 amt = dynsymcount * sizeof (unsigned long int);
7258 hashcodes = (unsigned long int *) bfd_malloc (amt);
7259 if (hashcodes == NULL)
7260 return FALSE;
7261 hashinf.hashcodes = hashcodes;
7262 hashinf.error = FALSE;
7263
7264 /* Put all hash values in HASHCODES. */
7265 elf_link_hash_traverse (elf_hash_table (info),
7266 elf_collect_hash_codes, &hashinf);
7267 if (hashinf.error)
7268 {
7269 free (hashcodes);
7270 return FALSE;
7271 }
7272
7273 nsyms = hashinf.hashcodes - hashcodes;
7274 bucketcount
7275 = compute_bucket_count (info, hashcodes, nsyms, 0);
7276 free (hashcodes);
7277
7278 if (bucketcount == 0 && nsyms > 0)
7279 return FALSE;
7280
7281 elf_hash_table (info)->bucketcount = bucketcount;
7282
7283 s = bfd_get_linker_section (dynobj, ".hash");
7284 BFD_ASSERT (s != NULL);
7285 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7286 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7287 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7288 if (s->contents == NULL)
7289 return FALSE;
7290
7291 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7292 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7293 s->contents + hash_entry_size);
7294 }
7295
7296 if (info->emit_gnu_hash)
7297 {
7298 size_t i, cnt;
7299 unsigned char *contents;
7300 struct collect_gnu_hash_codes cinfo;
7301 bfd_size_type amt;
7302 size_t bucketcount;
7303
7304 memset (&cinfo, 0, sizeof (cinfo));
7305
7306 /* Compute the hash values for all exported symbols. At the same
7307 time store the values in an array so that we could use them for
7308 optimizations. */
7309 amt = dynsymcount * 2 * sizeof (unsigned long int);
7310 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7311 if (cinfo.hashcodes == NULL)
7312 return FALSE;
7313
7314 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7315 cinfo.min_dynindx = -1;
7316 cinfo.output_bfd = output_bfd;
7317 cinfo.bed = bed;
7318
7319 /* Put all hash values in HASHCODES. */
7320 elf_link_hash_traverse (elf_hash_table (info),
7321 elf_collect_gnu_hash_codes, &cinfo);
7322 if (cinfo.error)
7323 {
7324 free (cinfo.hashcodes);
7325 return FALSE;
7326 }
7327
7328 bucketcount
7329 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7330
7331 if (bucketcount == 0)
7332 {
7333 free (cinfo.hashcodes);
7334 return FALSE;
7335 }
7336
7337 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7338 BFD_ASSERT (s != NULL);
7339
7340 if (cinfo.nsyms == 0)
7341 {
7342 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7343 BFD_ASSERT (cinfo.min_dynindx == -1);
7344 free (cinfo.hashcodes);
7345 s->size = 5 * 4 + bed->s->arch_size / 8;
7346 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7347 if (contents == NULL)
7348 return FALSE;
7349 s->contents = contents;
7350 /* 1 empty bucket. */
7351 bfd_put_32 (output_bfd, 1, contents);
7352 /* SYMIDX above the special symbol 0. */
7353 bfd_put_32 (output_bfd, 1, contents + 4);
7354 /* Just one word for bitmask. */
7355 bfd_put_32 (output_bfd, 1, contents + 8);
7356 /* Only hash fn bloom filter. */
7357 bfd_put_32 (output_bfd, 0, contents + 12);
7358 /* No hashes are valid - empty bitmask. */
7359 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7360 /* No hashes in the only bucket. */
7361 bfd_put_32 (output_bfd, 0,
7362 contents + 16 + bed->s->arch_size / 8);
7363 }
7364 else
7365 {
7366 unsigned long int maskwords, maskbitslog2, x;
7367 BFD_ASSERT (cinfo.min_dynindx != -1);
7368
7369 x = cinfo.nsyms;
7370 maskbitslog2 = 1;
7371 while ((x >>= 1) != 0)
7372 ++maskbitslog2;
7373 if (maskbitslog2 < 3)
7374 maskbitslog2 = 5;
7375 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7376 maskbitslog2 = maskbitslog2 + 3;
7377 else
7378 maskbitslog2 = maskbitslog2 + 2;
7379 if (bed->s->arch_size == 64)
7380 {
7381 if (maskbitslog2 == 5)
7382 maskbitslog2 = 6;
7383 cinfo.shift1 = 6;
7384 }
7385 else
7386 cinfo.shift1 = 5;
7387 cinfo.mask = (1 << cinfo.shift1) - 1;
7388 cinfo.shift2 = maskbitslog2;
7389 cinfo.maskbits = 1 << maskbitslog2;
7390 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7391 amt = bucketcount * sizeof (unsigned long int) * 2;
7392 amt += maskwords * sizeof (bfd_vma);
7393 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7394 if (cinfo.bitmask == NULL)
7395 {
7396 free (cinfo.hashcodes);
7397 return FALSE;
7398 }
7399
7400 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7401 cinfo.indx = cinfo.counts + bucketcount;
7402 cinfo.symindx = dynsymcount - cinfo.nsyms;
7403 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7404
7405 /* Determine how often each hash bucket is used. */
7406 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7407 for (i = 0; i < cinfo.nsyms; ++i)
7408 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7409
7410 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7411 if (cinfo.counts[i] != 0)
7412 {
7413 cinfo.indx[i] = cnt;
7414 cnt += cinfo.counts[i];
7415 }
7416 BFD_ASSERT (cnt == dynsymcount);
7417 cinfo.bucketcount = bucketcount;
7418 cinfo.local_indx = cinfo.min_dynindx;
7419
7420 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7421 s->size += cinfo.maskbits / 8;
7422 if (bed->record_xhash_symbol != NULL)
7423 s->size += cinfo.nsyms * 4;
7424 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7425 if (contents == NULL)
7426 {
7427 free (cinfo.bitmask);
7428 free (cinfo.hashcodes);
7429 return FALSE;
7430 }
7431
7432 s->contents = contents;
7433 bfd_put_32 (output_bfd, bucketcount, contents);
7434 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7435 bfd_put_32 (output_bfd, maskwords, contents + 8);
7436 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7437 contents += 16 + cinfo.maskbits / 8;
7438
7439 for (i = 0; i < bucketcount; ++i)
7440 {
7441 if (cinfo.counts[i] == 0)
7442 bfd_put_32 (output_bfd, 0, contents);
7443 else
7444 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7445 contents += 4;
7446 }
7447
7448 cinfo.contents = contents;
7449
7450 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7451 /* Renumber dynamic symbols, if populating .gnu.hash section.
7452 If using .MIPS.xhash, populate the translation table. */
7453 elf_link_hash_traverse (elf_hash_table (info),
7454 elf_gnu_hash_process_symidx, &cinfo);
7455
7456 contents = s->contents + 16;
7457 for (i = 0; i < maskwords; ++i)
7458 {
7459 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7460 contents);
7461 contents += bed->s->arch_size / 8;
7462 }
7463
7464 free (cinfo.bitmask);
7465 free (cinfo.hashcodes);
7466 }
7467 }
7468
7469 s = bfd_get_linker_section (dynobj, ".dynstr");
7470 BFD_ASSERT (s != NULL);
7471
7472 elf_finalize_dynstr (output_bfd, info);
7473
7474 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7475
7476 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7477 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7478 return FALSE;
7479 }
7480
7481 return TRUE;
7482 }
7483
7484 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7486
7487 static void
7488 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7489 asection *sec)
7490 {
7491 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7492 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7493 }
7494
7495 /* Finish SHF_MERGE section merging. */
7496
7497 bfd_boolean
7498 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7499 {
7500 bfd *ibfd;
7501 asection *sec;
7502
7503 if (!is_elf_hash_table (info->hash))
7504 return FALSE;
7505
7506 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7507 if ((ibfd->flags & DYNAMIC) == 0
7508 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7509 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7510 == get_elf_backend_data (obfd)->s->elfclass))
7511 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7512 if ((sec->flags & SEC_MERGE) != 0
7513 && !bfd_is_abs_section (sec->output_section))
7514 {
7515 struct bfd_elf_section_data *secdata;
7516
7517 secdata = elf_section_data (sec);
7518 if (! _bfd_add_merge_section (obfd,
7519 &elf_hash_table (info)->merge_info,
7520 sec, &secdata->sec_info))
7521 return FALSE;
7522 else if (secdata->sec_info)
7523 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7524 }
7525
7526 if (elf_hash_table (info)->merge_info != NULL)
7527 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7528 merge_sections_remove_hook);
7529 return TRUE;
7530 }
7531
7532 /* Create an entry in an ELF linker hash table. */
7533
7534 struct bfd_hash_entry *
7535 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7536 struct bfd_hash_table *table,
7537 const char *string)
7538 {
7539 /* Allocate the structure if it has not already been allocated by a
7540 subclass. */
7541 if (entry == NULL)
7542 {
7543 entry = (struct bfd_hash_entry *)
7544 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7545 if (entry == NULL)
7546 return entry;
7547 }
7548
7549 /* Call the allocation method of the superclass. */
7550 entry = _bfd_link_hash_newfunc (entry, table, string);
7551 if (entry != NULL)
7552 {
7553 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7554 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7555
7556 /* Set local fields. */
7557 ret->indx = -1;
7558 ret->dynindx = -1;
7559 ret->got = htab->init_got_refcount;
7560 ret->plt = htab->init_plt_refcount;
7561 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7562 - offsetof (struct elf_link_hash_entry, size)));
7563 /* Assume that we have been called by a non-ELF symbol reader.
7564 This flag is then reset by the code which reads an ELF input
7565 file. This ensures that a symbol created by a non-ELF symbol
7566 reader will have the flag set correctly. */
7567 ret->non_elf = 1;
7568 }
7569
7570 return entry;
7571 }
7572
7573 /* Copy data from an indirect symbol to its direct symbol, hiding the
7574 old indirect symbol. Also used for copying flags to a weakdef. */
7575
7576 void
7577 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7578 struct elf_link_hash_entry *dir,
7579 struct elf_link_hash_entry *ind)
7580 {
7581 struct elf_link_hash_table *htab;
7582
7583 /* Copy down any references that we may have already seen to the
7584 symbol which just became indirect. */
7585
7586 if (dir->versioned != versioned_hidden)
7587 dir->ref_dynamic |= ind->ref_dynamic;
7588 dir->ref_regular |= ind->ref_regular;
7589 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7590 dir->non_got_ref |= ind->non_got_ref;
7591 dir->needs_plt |= ind->needs_plt;
7592 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7593
7594 if (ind->root.type != bfd_link_hash_indirect)
7595 return;
7596
7597 /* Copy over the global and procedure linkage table refcount entries.
7598 These may have been already set up by a check_relocs routine. */
7599 htab = elf_hash_table (info);
7600 if (ind->got.refcount > htab->init_got_refcount.refcount)
7601 {
7602 if (dir->got.refcount < 0)
7603 dir->got.refcount = 0;
7604 dir->got.refcount += ind->got.refcount;
7605 ind->got.refcount = htab->init_got_refcount.refcount;
7606 }
7607
7608 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7609 {
7610 if (dir->plt.refcount < 0)
7611 dir->plt.refcount = 0;
7612 dir->plt.refcount += ind->plt.refcount;
7613 ind->plt.refcount = htab->init_plt_refcount.refcount;
7614 }
7615
7616 if (ind->dynindx != -1)
7617 {
7618 if (dir->dynindx != -1)
7619 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7620 dir->dynindx = ind->dynindx;
7621 dir->dynstr_index = ind->dynstr_index;
7622 ind->dynindx = -1;
7623 ind->dynstr_index = 0;
7624 }
7625 }
7626
7627 void
7628 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7629 struct elf_link_hash_entry *h,
7630 bfd_boolean force_local)
7631 {
7632 /* STT_GNU_IFUNC symbol must go through PLT. */
7633 if (h->type != STT_GNU_IFUNC)
7634 {
7635 h->plt = elf_hash_table (info)->init_plt_offset;
7636 h->needs_plt = 0;
7637 }
7638 if (force_local)
7639 {
7640 h->forced_local = 1;
7641 if (h->dynindx != -1)
7642 {
7643 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7644 h->dynstr_index);
7645 h->dynindx = -1;
7646 h->dynstr_index = 0;
7647 }
7648 }
7649 }
7650
7651 /* Hide a symbol. */
7652
7653 void
7654 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7655 struct bfd_link_info *info,
7656 struct bfd_link_hash_entry *h)
7657 {
7658 if (is_elf_hash_table (info->hash))
7659 {
7660 const struct elf_backend_data *bed
7661 = get_elf_backend_data (output_bfd);
7662 struct elf_link_hash_entry *eh
7663 = (struct elf_link_hash_entry *) h;
7664 bed->elf_backend_hide_symbol (info, eh, TRUE);
7665 eh->def_dynamic = 0;
7666 eh->ref_dynamic = 0;
7667 eh->dynamic_def = 0;
7668 }
7669 }
7670
7671 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7672 caller. */
7673
7674 bfd_boolean
7675 _bfd_elf_link_hash_table_init
7676 (struct elf_link_hash_table *table,
7677 bfd *abfd,
7678 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7679 struct bfd_hash_table *,
7680 const char *),
7681 unsigned int entsize,
7682 enum elf_target_id target_id)
7683 {
7684 bfd_boolean ret;
7685 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7686
7687 table->init_got_refcount.refcount = can_refcount - 1;
7688 table->init_plt_refcount.refcount = can_refcount - 1;
7689 table->init_got_offset.offset = -(bfd_vma) 1;
7690 table->init_plt_offset.offset = -(bfd_vma) 1;
7691 /* The first dynamic symbol is a dummy. */
7692 table->dynsymcount = 1;
7693
7694 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7695
7696 table->root.type = bfd_link_elf_hash_table;
7697 table->hash_table_id = target_id;
7698
7699 return ret;
7700 }
7701
7702 /* Create an ELF linker hash table. */
7703
7704 struct bfd_link_hash_table *
7705 _bfd_elf_link_hash_table_create (bfd *abfd)
7706 {
7707 struct elf_link_hash_table *ret;
7708 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7709
7710 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7711 if (ret == NULL)
7712 return NULL;
7713
7714 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7715 sizeof (struct elf_link_hash_entry),
7716 GENERIC_ELF_DATA))
7717 {
7718 free (ret);
7719 return NULL;
7720 }
7721 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7722
7723 return &ret->root;
7724 }
7725
7726 /* Destroy an ELF linker hash table. */
7727
7728 void
7729 _bfd_elf_link_hash_table_free (bfd *obfd)
7730 {
7731 struct elf_link_hash_table *htab;
7732
7733 htab = (struct elf_link_hash_table *) obfd->link.hash;
7734 if (htab->dynstr != NULL)
7735 _bfd_elf_strtab_free (htab->dynstr);
7736 _bfd_merge_sections_free (htab->merge_info);
7737 _bfd_generic_link_hash_table_free (obfd);
7738 }
7739
7740 /* This is a hook for the ELF emulation code in the generic linker to
7741 tell the backend linker what file name to use for the DT_NEEDED
7742 entry for a dynamic object. */
7743
7744 void
7745 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7746 {
7747 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7748 && bfd_get_format (abfd) == bfd_object)
7749 elf_dt_name (abfd) = name;
7750 }
7751
7752 int
7753 bfd_elf_get_dyn_lib_class (bfd *abfd)
7754 {
7755 int lib_class;
7756 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7757 && bfd_get_format (abfd) == bfd_object)
7758 lib_class = elf_dyn_lib_class (abfd);
7759 else
7760 lib_class = 0;
7761 return lib_class;
7762 }
7763
7764 void
7765 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7766 {
7767 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7768 && bfd_get_format (abfd) == bfd_object)
7769 elf_dyn_lib_class (abfd) = lib_class;
7770 }
7771
7772 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7773 the linker ELF emulation code. */
7774
7775 struct bfd_link_needed_list *
7776 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7777 struct bfd_link_info *info)
7778 {
7779 if (! is_elf_hash_table (info->hash))
7780 return NULL;
7781 return elf_hash_table (info)->needed;
7782 }
7783
7784 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7785 hook for the linker ELF emulation code. */
7786
7787 struct bfd_link_needed_list *
7788 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7789 struct bfd_link_info *info)
7790 {
7791 if (! is_elf_hash_table (info->hash))
7792 return NULL;
7793 return elf_hash_table (info)->runpath;
7794 }
7795
7796 /* Get the name actually used for a dynamic object for a link. This
7797 is the SONAME entry if there is one. Otherwise, it is the string
7798 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7799
7800 const char *
7801 bfd_elf_get_dt_soname (bfd *abfd)
7802 {
7803 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7804 && bfd_get_format (abfd) == bfd_object)
7805 return elf_dt_name (abfd);
7806 return NULL;
7807 }
7808
7809 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7810 the ELF linker emulation code. */
7811
7812 bfd_boolean
7813 bfd_elf_get_bfd_needed_list (bfd *abfd,
7814 struct bfd_link_needed_list **pneeded)
7815 {
7816 asection *s;
7817 bfd_byte *dynbuf = NULL;
7818 unsigned int elfsec;
7819 unsigned long shlink;
7820 bfd_byte *extdyn, *extdynend;
7821 size_t extdynsize;
7822 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7823
7824 *pneeded = NULL;
7825
7826 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7827 || bfd_get_format (abfd) != bfd_object)
7828 return TRUE;
7829
7830 s = bfd_get_section_by_name (abfd, ".dynamic");
7831 if (s == NULL || s->size == 0)
7832 return TRUE;
7833
7834 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7835 goto error_return;
7836
7837 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7838 if (elfsec == SHN_BAD)
7839 goto error_return;
7840
7841 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7842
7843 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7844 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7845
7846 extdyn = dynbuf;
7847 extdynend = extdyn + s->size;
7848 for (; extdyn < extdynend; extdyn += extdynsize)
7849 {
7850 Elf_Internal_Dyn dyn;
7851
7852 (*swap_dyn_in) (abfd, extdyn, &dyn);
7853
7854 if (dyn.d_tag == DT_NULL)
7855 break;
7856
7857 if (dyn.d_tag == DT_NEEDED)
7858 {
7859 const char *string;
7860 struct bfd_link_needed_list *l;
7861 unsigned int tagv = dyn.d_un.d_val;
7862 bfd_size_type amt;
7863
7864 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7865 if (string == NULL)
7866 goto error_return;
7867
7868 amt = sizeof *l;
7869 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7870 if (l == NULL)
7871 goto error_return;
7872
7873 l->by = abfd;
7874 l->name = string;
7875 l->next = *pneeded;
7876 *pneeded = l;
7877 }
7878 }
7879
7880 free (dynbuf);
7881
7882 return TRUE;
7883
7884 error_return:
7885 if (dynbuf != NULL)
7886 free (dynbuf);
7887 return FALSE;
7888 }
7889
7890 struct elf_symbuf_symbol
7891 {
7892 unsigned long st_name; /* Symbol name, index in string tbl */
7893 unsigned char st_info; /* Type and binding attributes */
7894 unsigned char st_other; /* Visibilty, and target specific */
7895 };
7896
7897 struct elf_symbuf_head
7898 {
7899 struct elf_symbuf_symbol *ssym;
7900 size_t count;
7901 unsigned int st_shndx;
7902 };
7903
7904 struct elf_symbol
7905 {
7906 union
7907 {
7908 Elf_Internal_Sym *isym;
7909 struct elf_symbuf_symbol *ssym;
7910 void *p;
7911 } u;
7912 const char *name;
7913 };
7914
7915 /* Sort references to symbols by ascending section number. */
7916
7917 static int
7918 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7919 {
7920 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7921 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7922
7923 if (s1->st_shndx != s2->st_shndx)
7924 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7925 /* Final sort by the address of the sym in the symbuf ensures
7926 a stable sort. */
7927 if (s1 != s2)
7928 return s1 > s2 ? 1 : -1;
7929 return 0;
7930 }
7931
7932 static int
7933 elf_sym_name_compare (const void *arg1, const void *arg2)
7934 {
7935 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7936 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7937 int ret = strcmp (s1->name, s2->name);
7938 if (ret != 0)
7939 return ret;
7940 if (s1->u.p != s2->u.p)
7941 return s1->u.p > s2->u.p ? 1 : -1;
7942 return 0;
7943 }
7944
7945 static struct elf_symbuf_head *
7946 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7947 {
7948 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7949 struct elf_symbuf_symbol *ssym;
7950 struct elf_symbuf_head *ssymbuf, *ssymhead;
7951 size_t i, shndx_count, total_size;
7952
7953 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7954 if (indbuf == NULL)
7955 return NULL;
7956
7957 for (ind = indbuf, i = 0; i < symcount; i++)
7958 if (isymbuf[i].st_shndx != SHN_UNDEF)
7959 *ind++ = &isymbuf[i];
7960 indbufend = ind;
7961
7962 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7963 elf_sort_elf_symbol);
7964
7965 shndx_count = 0;
7966 if (indbufend > indbuf)
7967 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7968 if (ind[0]->st_shndx != ind[1]->st_shndx)
7969 shndx_count++;
7970
7971 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7972 + (indbufend - indbuf) * sizeof (*ssym));
7973 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7974 if (ssymbuf == NULL)
7975 {
7976 free (indbuf);
7977 return NULL;
7978 }
7979
7980 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7981 ssymbuf->ssym = NULL;
7982 ssymbuf->count = shndx_count;
7983 ssymbuf->st_shndx = 0;
7984 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7985 {
7986 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7987 {
7988 ssymhead++;
7989 ssymhead->ssym = ssym;
7990 ssymhead->count = 0;
7991 ssymhead->st_shndx = (*ind)->st_shndx;
7992 }
7993 ssym->st_name = (*ind)->st_name;
7994 ssym->st_info = (*ind)->st_info;
7995 ssym->st_other = (*ind)->st_other;
7996 ssymhead->count++;
7997 }
7998 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7999 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8000 == total_size));
8001
8002 free (indbuf);
8003 return ssymbuf;
8004 }
8005
8006 /* Check if 2 sections define the same set of local and global
8007 symbols. */
8008
8009 static bfd_boolean
8010 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8011 struct bfd_link_info *info)
8012 {
8013 bfd *bfd1, *bfd2;
8014 const struct elf_backend_data *bed1, *bed2;
8015 Elf_Internal_Shdr *hdr1, *hdr2;
8016 size_t symcount1, symcount2;
8017 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8018 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8019 Elf_Internal_Sym *isym, *isymend;
8020 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8021 size_t count1, count2, i;
8022 unsigned int shndx1, shndx2;
8023 bfd_boolean result;
8024
8025 bfd1 = sec1->owner;
8026 bfd2 = sec2->owner;
8027
8028 /* Both sections have to be in ELF. */
8029 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8030 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8031 return FALSE;
8032
8033 if (elf_section_type (sec1) != elf_section_type (sec2))
8034 return FALSE;
8035
8036 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8037 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8038 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8039 return FALSE;
8040
8041 bed1 = get_elf_backend_data (bfd1);
8042 bed2 = get_elf_backend_data (bfd2);
8043 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8044 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8045 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8046 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8047
8048 if (symcount1 == 0 || symcount2 == 0)
8049 return FALSE;
8050
8051 result = FALSE;
8052 isymbuf1 = NULL;
8053 isymbuf2 = NULL;
8054 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8055 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8056
8057 if (ssymbuf1 == NULL)
8058 {
8059 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8060 NULL, NULL, NULL);
8061 if (isymbuf1 == NULL)
8062 goto done;
8063
8064 if (!info->reduce_memory_overheads)
8065 {
8066 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8067 elf_tdata (bfd1)->symbuf = ssymbuf1;
8068 }
8069 }
8070
8071 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8072 {
8073 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8074 NULL, NULL, NULL);
8075 if (isymbuf2 == NULL)
8076 goto done;
8077
8078 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8079 {
8080 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8081 elf_tdata (bfd2)->symbuf = ssymbuf2;
8082 }
8083 }
8084
8085 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8086 {
8087 /* Optimized faster version. */
8088 size_t lo, hi, mid;
8089 struct elf_symbol *symp;
8090 struct elf_symbuf_symbol *ssym, *ssymend;
8091
8092 lo = 0;
8093 hi = ssymbuf1->count;
8094 ssymbuf1++;
8095 count1 = 0;
8096 while (lo < hi)
8097 {
8098 mid = (lo + hi) / 2;
8099 if (shndx1 < ssymbuf1[mid].st_shndx)
8100 hi = mid;
8101 else if (shndx1 > ssymbuf1[mid].st_shndx)
8102 lo = mid + 1;
8103 else
8104 {
8105 count1 = ssymbuf1[mid].count;
8106 ssymbuf1 += mid;
8107 break;
8108 }
8109 }
8110
8111 lo = 0;
8112 hi = ssymbuf2->count;
8113 ssymbuf2++;
8114 count2 = 0;
8115 while (lo < hi)
8116 {
8117 mid = (lo + hi) / 2;
8118 if (shndx2 < ssymbuf2[mid].st_shndx)
8119 hi = mid;
8120 else if (shndx2 > ssymbuf2[mid].st_shndx)
8121 lo = mid + 1;
8122 else
8123 {
8124 count2 = ssymbuf2[mid].count;
8125 ssymbuf2 += mid;
8126 break;
8127 }
8128 }
8129
8130 if (count1 == 0 || count2 == 0 || count1 != count2)
8131 goto done;
8132
8133 symtable1
8134 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8135 symtable2
8136 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8137 if (symtable1 == NULL || symtable2 == NULL)
8138 goto done;
8139
8140 symp = symtable1;
8141 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8142 ssym < ssymend; ssym++, symp++)
8143 {
8144 symp->u.ssym = ssym;
8145 symp->name = bfd_elf_string_from_elf_section (bfd1,
8146 hdr1->sh_link,
8147 ssym->st_name);
8148 }
8149
8150 symp = symtable2;
8151 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8152 ssym < ssymend; ssym++, symp++)
8153 {
8154 symp->u.ssym = ssym;
8155 symp->name = bfd_elf_string_from_elf_section (bfd2,
8156 hdr2->sh_link,
8157 ssym->st_name);
8158 }
8159
8160 /* Sort symbol by name. */
8161 qsort (symtable1, count1, sizeof (struct elf_symbol),
8162 elf_sym_name_compare);
8163 qsort (symtable2, count1, sizeof (struct elf_symbol),
8164 elf_sym_name_compare);
8165
8166 for (i = 0; i < count1; i++)
8167 /* Two symbols must have the same binding, type and name. */
8168 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8169 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8170 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8171 goto done;
8172
8173 result = TRUE;
8174 goto done;
8175 }
8176
8177 symtable1 = (struct elf_symbol *)
8178 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8179 symtable2 = (struct elf_symbol *)
8180 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8181 if (symtable1 == NULL || symtable2 == NULL)
8182 goto done;
8183
8184 /* Count definitions in the section. */
8185 count1 = 0;
8186 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8187 if (isym->st_shndx == shndx1)
8188 symtable1[count1++].u.isym = isym;
8189
8190 count2 = 0;
8191 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8192 if (isym->st_shndx == shndx2)
8193 symtable2[count2++].u.isym = isym;
8194
8195 if (count1 == 0 || count2 == 0 || count1 != count2)
8196 goto done;
8197
8198 for (i = 0; i < count1; i++)
8199 symtable1[i].name
8200 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8201 symtable1[i].u.isym->st_name);
8202
8203 for (i = 0; i < count2; i++)
8204 symtable2[i].name
8205 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8206 symtable2[i].u.isym->st_name);
8207
8208 /* Sort symbol by name. */
8209 qsort (symtable1, count1, sizeof (struct elf_symbol),
8210 elf_sym_name_compare);
8211 qsort (symtable2, count1, sizeof (struct elf_symbol),
8212 elf_sym_name_compare);
8213
8214 for (i = 0; i < count1; i++)
8215 /* Two symbols must have the same binding, type and name. */
8216 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8217 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8218 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8219 goto done;
8220
8221 result = TRUE;
8222
8223 done:
8224 if (symtable1)
8225 free (symtable1);
8226 if (symtable2)
8227 free (symtable2);
8228 if (isymbuf1)
8229 free (isymbuf1);
8230 if (isymbuf2)
8231 free (isymbuf2);
8232
8233 return result;
8234 }
8235
8236 /* Return TRUE if 2 section types are compatible. */
8237
8238 bfd_boolean
8239 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8240 bfd *bbfd, const asection *bsec)
8241 {
8242 if (asec == NULL
8243 || bsec == NULL
8244 || abfd->xvec->flavour != bfd_target_elf_flavour
8245 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8246 return TRUE;
8247
8248 return elf_section_type (asec) == elf_section_type (bsec);
8249 }
8250
8251 /* Final phase of ELF linker. */
8253
8254 /* A structure we use to avoid passing large numbers of arguments. */
8255
8256 struct elf_final_link_info
8257 {
8258 /* General link information. */
8259 struct bfd_link_info *info;
8260 /* Output BFD. */
8261 bfd *output_bfd;
8262 /* Symbol string table. */
8263 struct elf_strtab_hash *symstrtab;
8264 /* .hash section. */
8265 asection *hash_sec;
8266 /* symbol version section (.gnu.version). */
8267 asection *symver_sec;
8268 /* Buffer large enough to hold contents of any section. */
8269 bfd_byte *contents;
8270 /* Buffer large enough to hold external relocs of any section. */
8271 void *external_relocs;
8272 /* Buffer large enough to hold internal relocs of any section. */
8273 Elf_Internal_Rela *internal_relocs;
8274 /* Buffer large enough to hold external local symbols of any input
8275 BFD. */
8276 bfd_byte *external_syms;
8277 /* And a buffer for symbol section indices. */
8278 Elf_External_Sym_Shndx *locsym_shndx;
8279 /* Buffer large enough to hold internal local symbols of any input
8280 BFD. */
8281 Elf_Internal_Sym *internal_syms;
8282 /* Array large enough to hold a symbol index for each local symbol
8283 of any input BFD. */
8284 long *indices;
8285 /* Array large enough to hold a section pointer for each local
8286 symbol of any input BFD. */
8287 asection **sections;
8288 /* Buffer for SHT_SYMTAB_SHNDX section. */
8289 Elf_External_Sym_Shndx *symshndxbuf;
8290 /* Number of STT_FILE syms seen. */
8291 size_t filesym_count;
8292 };
8293
8294 /* This struct is used to pass information to elf_link_output_extsym. */
8295
8296 struct elf_outext_info
8297 {
8298 bfd_boolean failed;
8299 bfd_boolean localsyms;
8300 bfd_boolean file_sym_done;
8301 struct elf_final_link_info *flinfo;
8302 };
8303
8304
8305 /* Support for evaluating a complex relocation.
8306
8307 Complex relocations are generalized, self-describing relocations. The
8308 implementation of them consists of two parts: complex symbols, and the
8309 relocations themselves.
8310
8311 The relocations are use a reserved elf-wide relocation type code (R_RELC
8312 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8313 information (start bit, end bit, word width, etc) into the addend. This
8314 information is extracted from CGEN-generated operand tables within gas.
8315
8316 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8317 internal) representing prefix-notation expressions, including but not
8318 limited to those sorts of expressions normally encoded as addends in the
8319 addend field. The symbol mangling format is:
8320
8321 <node> := <literal>
8322 | <unary-operator> ':' <node>
8323 | <binary-operator> ':' <node> ':' <node>
8324 ;
8325
8326 <literal> := 's' <digits=N> ':' <N character symbol name>
8327 | 'S' <digits=N> ':' <N character section name>
8328 | '#' <hexdigits>
8329 ;
8330
8331 <binary-operator> := as in C
8332 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8333
8334 static void
8335 set_symbol_value (bfd *bfd_with_globals,
8336 Elf_Internal_Sym *isymbuf,
8337 size_t locsymcount,
8338 size_t symidx,
8339 bfd_vma val)
8340 {
8341 struct elf_link_hash_entry **sym_hashes;
8342 struct elf_link_hash_entry *h;
8343 size_t extsymoff = locsymcount;
8344
8345 if (symidx < locsymcount)
8346 {
8347 Elf_Internal_Sym *sym;
8348
8349 sym = isymbuf + symidx;
8350 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8351 {
8352 /* It is a local symbol: move it to the
8353 "absolute" section and give it a value. */
8354 sym->st_shndx = SHN_ABS;
8355 sym->st_value = val;
8356 return;
8357 }
8358 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8359 extsymoff = 0;
8360 }
8361
8362 /* It is a global symbol: set its link type
8363 to "defined" and give it a value. */
8364
8365 sym_hashes = elf_sym_hashes (bfd_with_globals);
8366 h = sym_hashes [symidx - extsymoff];
8367 while (h->root.type == bfd_link_hash_indirect
8368 || h->root.type == bfd_link_hash_warning)
8369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8370 h->root.type = bfd_link_hash_defined;
8371 h->root.u.def.value = val;
8372 h->root.u.def.section = bfd_abs_section_ptr;
8373 }
8374
8375 static bfd_boolean
8376 resolve_symbol (const char *name,
8377 bfd *input_bfd,
8378 struct elf_final_link_info *flinfo,
8379 bfd_vma *result,
8380 Elf_Internal_Sym *isymbuf,
8381 size_t locsymcount)
8382 {
8383 Elf_Internal_Sym *sym;
8384 struct bfd_link_hash_entry *global_entry;
8385 const char *candidate = NULL;
8386 Elf_Internal_Shdr *symtab_hdr;
8387 size_t i;
8388
8389 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8390
8391 for (i = 0; i < locsymcount; ++ i)
8392 {
8393 sym = isymbuf + i;
8394
8395 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8396 continue;
8397
8398 candidate = bfd_elf_string_from_elf_section (input_bfd,
8399 symtab_hdr->sh_link,
8400 sym->st_name);
8401 #ifdef DEBUG
8402 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8403 name, candidate, (unsigned long) sym->st_value);
8404 #endif
8405 if (candidate && strcmp (candidate, name) == 0)
8406 {
8407 asection *sec = flinfo->sections [i];
8408
8409 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8410 *result += sec->output_offset + sec->output_section->vma;
8411 #ifdef DEBUG
8412 printf ("Found symbol with value %8.8lx\n",
8413 (unsigned long) *result);
8414 #endif
8415 return TRUE;
8416 }
8417 }
8418
8419 /* Hmm, haven't found it yet. perhaps it is a global. */
8420 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8421 FALSE, FALSE, TRUE);
8422 if (!global_entry)
8423 return FALSE;
8424
8425 if (global_entry->type == bfd_link_hash_defined
8426 || global_entry->type == bfd_link_hash_defweak)
8427 {
8428 *result = (global_entry->u.def.value
8429 + global_entry->u.def.section->output_section->vma
8430 + global_entry->u.def.section->output_offset);
8431 #ifdef DEBUG
8432 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8433 global_entry->root.string, (unsigned long) *result);
8434 #endif
8435 return TRUE;
8436 }
8437
8438 return FALSE;
8439 }
8440
8441 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8442 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8443 names like "foo.end" which is the end address of section "foo". */
8444
8445 static bfd_boolean
8446 resolve_section (const char *name,
8447 asection *sections,
8448 bfd_vma *result,
8449 bfd * abfd)
8450 {
8451 asection *curr;
8452 unsigned int len;
8453
8454 for (curr = sections; curr; curr = curr->next)
8455 if (strcmp (curr->name, name) == 0)
8456 {
8457 *result = curr->vma;
8458 return TRUE;
8459 }
8460
8461 /* Hmm. still haven't found it. try pseudo-section names. */
8462 /* FIXME: This could be coded more efficiently... */
8463 for (curr = sections; curr; curr = curr->next)
8464 {
8465 len = strlen (curr->name);
8466 if (len > strlen (name))
8467 continue;
8468
8469 if (strncmp (curr->name, name, len) == 0)
8470 {
8471 if (strncmp (".end", name + len, 4) == 0)
8472 {
8473 *result = (curr->vma
8474 + curr->size / bfd_octets_per_byte (abfd, curr));
8475 return TRUE;
8476 }
8477
8478 /* Insert more pseudo-section names here, if you like. */
8479 }
8480 }
8481
8482 return FALSE;
8483 }
8484
8485 static void
8486 undefined_reference (const char *reftype, const char *name)
8487 {
8488 /* xgettext:c-format */
8489 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8490 reftype, name);
8491 }
8492
8493 static bfd_boolean
8494 eval_symbol (bfd_vma *result,
8495 const char **symp,
8496 bfd *input_bfd,
8497 struct elf_final_link_info *flinfo,
8498 bfd_vma dot,
8499 Elf_Internal_Sym *isymbuf,
8500 size_t locsymcount,
8501 int signed_p)
8502 {
8503 size_t len;
8504 size_t symlen;
8505 bfd_vma a;
8506 bfd_vma b;
8507 char symbuf[4096];
8508 const char *sym = *symp;
8509 const char *symend;
8510 bfd_boolean symbol_is_section = FALSE;
8511
8512 len = strlen (sym);
8513 symend = sym + len;
8514
8515 if (len < 1 || len > sizeof (symbuf))
8516 {
8517 bfd_set_error (bfd_error_invalid_operation);
8518 return FALSE;
8519 }
8520
8521 switch (* sym)
8522 {
8523 case '.':
8524 *result = dot;
8525 *symp = sym + 1;
8526 return TRUE;
8527
8528 case '#':
8529 ++sym;
8530 *result = strtoul (sym, (char **) symp, 16);
8531 return TRUE;
8532
8533 case 'S':
8534 symbol_is_section = TRUE;
8535 /* Fall through. */
8536 case 's':
8537 ++sym;
8538 symlen = strtol (sym, (char **) symp, 10);
8539 sym = *symp + 1; /* Skip the trailing ':'. */
8540
8541 if (symend < sym || symlen + 1 > sizeof (symbuf))
8542 {
8543 bfd_set_error (bfd_error_invalid_operation);
8544 return FALSE;
8545 }
8546
8547 memcpy (symbuf, sym, symlen);
8548 symbuf[symlen] = '\0';
8549 *symp = sym + symlen;
8550
8551 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8552 the symbol as a section, or vice-versa. so we're pretty liberal in our
8553 interpretation here; section means "try section first", not "must be a
8554 section", and likewise with symbol. */
8555
8556 if (symbol_is_section)
8557 {
8558 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8559 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8560 isymbuf, locsymcount))
8561 {
8562 undefined_reference ("section", symbuf);
8563 return FALSE;
8564 }
8565 }
8566 else
8567 {
8568 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8569 isymbuf, locsymcount)
8570 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8571 result, input_bfd))
8572 {
8573 undefined_reference ("symbol", symbuf);
8574 return FALSE;
8575 }
8576 }
8577
8578 return TRUE;
8579
8580 /* All that remains are operators. */
8581
8582 #define UNARY_OP(op) \
8583 if (strncmp (sym, #op, strlen (#op)) == 0) \
8584 { \
8585 sym += strlen (#op); \
8586 if (*sym == ':') \
8587 ++sym; \
8588 *symp = sym; \
8589 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8590 isymbuf, locsymcount, signed_p)) \
8591 return FALSE; \
8592 if (signed_p) \
8593 *result = op ((bfd_signed_vma) a); \
8594 else \
8595 *result = op a; \
8596 return TRUE; \
8597 }
8598
8599 #define BINARY_OP(op) \
8600 if (strncmp (sym, #op, strlen (#op)) == 0) \
8601 { \
8602 sym += strlen (#op); \
8603 if (*sym == ':') \
8604 ++sym; \
8605 *symp = sym; \
8606 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8607 isymbuf, locsymcount, signed_p)) \
8608 return FALSE; \
8609 ++*symp; \
8610 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8611 isymbuf, locsymcount, signed_p)) \
8612 return FALSE; \
8613 if (signed_p) \
8614 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8615 else \
8616 *result = a op b; \
8617 return TRUE; \
8618 }
8619
8620 default:
8621 UNARY_OP (0-);
8622 BINARY_OP (<<);
8623 BINARY_OP (>>);
8624 BINARY_OP (==);
8625 BINARY_OP (!=);
8626 BINARY_OP (<=);
8627 BINARY_OP (>=);
8628 BINARY_OP (&&);
8629 BINARY_OP (||);
8630 UNARY_OP (~);
8631 UNARY_OP (!);
8632 BINARY_OP (*);
8633 BINARY_OP (/);
8634 BINARY_OP (%);
8635 BINARY_OP (^);
8636 BINARY_OP (|);
8637 BINARY_OP (&);
8638 BINARY_OP (+);
8639 BINARY_OP (-);
8640 BINARY_OP (<);
8641 BINARY_OP (>);
8642 #undef UNARY_OP
8643 #undef BINARY_OP
8644 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8645 bfd_set_error (bfd_error_invalid_operation);
8646 return FALSE;
8647 }
8648 }
8649
8650 static void
8651 put_value (bfd_vma size,
8652 unsigned long chunksz,
8653 bfd *input_bfd,
8654 bfd_vma x,
8655 bfd_byte *location)
8656 {
8657 location += (size - chunksz);
8658
8659 for (; size; size -= chunksz, location -= chunksz)
8660 {
8661 switch (chunksz)
8662 {
8663 case 1:
8664 bfd_put_8 (input_bfd, x, location);
8665 x >>= 8;
8666 break;
8667 case 2:
8668 bfd_put_16 (input_bfd, x, location);
8669 x >>= 16;
8670 break;
8671 case 4:
8672 bfd_put_32 (input_bfd, x, location);
8673 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8674 x >>= 16;
8675 x >>= 16;
8676 break;
8677 #ifdef BFD64
8678 case 8:
8679 bfd_put_64 (input_bfd, x, location);
8680 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8681 x >>= 32;
8682 x >>= 32;
8683 break;
8684 #endif
8685 default:
8686 abort ();
8687 break;
8688 }
8689 }
8690 }
8691
8692 static bfd_vma
8693 get_value (bfd_vma size,
8694 unsigned long chunksz,
8695 bfd *input_bfd,
8696 bfd_byte *location)
8697 {
8698 int shift;
8699 bfd_vma x = 0;
8700
8701 /* Sanity checks. */
8702 BFD_ASSERT (chunksz <= sizeof (x)
8703 && size >= chunksz
8704 && chunksz != 0
8705 && (size % chunksz) == 0
8706 && input_bfd != NULL
8707 && location != NULL);
8708
8709 if (chunksz == sizeof (x))
8710 {
8711 BFD_ASSERT (size == chunksz);
8712
8713 /* Make sure that we do not perform an undefined shift operation.
8714 We know that size == chunksz so there will only be one iteration
8715 of the loop below. */
8716 shift = 0;
8717 }
8718 else
8719 shift = 8 * chunksz;
8720
8721 for (; size; size -= chunksz, location += chunksz)
8722 {
8723 switch (chunksz)
8724 {
8725 case 1:
8726 x = (x << shift) | bfd_get_8 (input_bfd, location);
8727 break;
8728 case 2:
8729 x = (x << shift) | bfd_get_16 (input_bfd, location);
8730 break;
8731 case 4:
8732 x = (x << shift) | bfd_get_32 (input_bfd, location);
8733 break;
8734 #ifdef BFD64
8735 case 8:
8736 x = (x << shift) | bfd_get_64 (input_bfd, location);
8737 break;
8738 #endif
8739 default:
8740 abort ();
8741 }
8742 }
8743 return x;
8744 }
8745
8746 static void
8747 decode_complex_addend (unsigned long *start, /* in bits */
8748 unsigned long *oplen, /* in bits */
8749 unsigned long *len, /* in bits */
8750 unsigned long *wordsz, /* in bytes */
8751 unsigned long *chunksz, /* in bytes */
8752 unsigned long *lsb0_p,
8753 unsigned long *signed_p,
8754 unsigned long *trunc_p,
8755 unsigned long encoded)
8756 {
8757 * start = encoded & 0x3F;
8758 * len = (encoded >> 6) & 0x3F;
8759 * oplen = (encoded >> 12) & 0x3F;
8760 * wordsz = (encoded >> 18) & 0xF;
8761 * chunksz = (encoded >> 22) & 0xF;
8762 * lsb0_p = (encoded >> 27) & 1;
8763 * signed_p = (encoded >> 28) & 1;
8764 * trunc_p = (encoded >> 29) & 1;
8765 }
8766
8767 bfd_reloc_status_type
8768 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8769 asection *input_section,
8770 bfd_byte *contents,
8771 Elf_Internal_Rela *rel,
8772 bfd_vma relocation)
8773 {
8774 bfd_vma shift, x, mask;
8775 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8776 bfd_reloc_status_type r;
8777 bfd_size_type octets;
8778
8779 /* Perform this reloc, since it is complex.
8780 (this is not to say that it necessarily refers to a complex
8781 symbol; merely that it is a self-describing CGEN based reloc.
8782 i.e. the addend has the complete reloc information (bit start, end,
8783 word size, etc) encoded within it.). */
8784
8785 decode_complex_addend (&start, &oplen, &len, &wordsz,
8786 &chunksz, &lsb0_p, &signed_p,
8787 &trunc_p, rel->r_addend);
8788
8789 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8790
8791 if (lsb0_p)
8792 shift = (start + 1) - len;
8793 else
8794 shift = (8 * wordsz) - (start + len);
8795
8796 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8797 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8798
8799 #ifdef DEBUG
8800 printf ("Doing complex reloc: "
8801 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8802 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8803 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8804 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8805 oplen, (unsigned long) x, (unsigned long) mask,
8806 (unsigned long) relocation);
8807 #endif
8808
8809 r = bfd_reloc_ok;
8810 if (! trunc_p)
8811 /* Now do an overflow check. */
8812 r = bfd_check_overflow ((signed_p
8813 ? complain_overflow_signed
8814 : complain_overflow_unsigned),
8815 len, 0, (8 * wordsz),
8816 relocation);
8817
8818 /* Do the deed. */
8819 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8820
8821 #ifdef DEBUG
8822 printf (" relocation: %8.8lx\n"
8823 " shifted mask: %8.8lx\n"
8824 " shifted/masked reloc: %8.8lx\n"
8825 " result: %8.8lx\n",
8826 (unsigned long) relocation, (unsigned long) (mask << shift),
8827 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8828 #endif
8829 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8830 return r;
8831 }
8832
8833 /* Functions to read r_offset from external (target order) reloc
8834 entry. Faster than bfd_getl32 et al, because we let the compiler
8835 know the value is aligned. */
8836
8837 static bfd_vma
8838 ext32l_r_offset (const void *p)
8839 {
8840 union aligned32
8841 {
8842 uint32_t v;
8843 unsigned char c[4];
8844 };
8845 const union aligned32 *a
8846 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8847
8848 uint32_t aval = ( (uint32_t) a->c[0]
8849 | (uint32_t) a->c[1] << 8
8850 | (uint32_t) a->c[2] << 16
8851 | (uint32_t) a->c[3] << 24);
8852 return aval;
8853 }
8854
8855 static bfd_vma
8856 ext32b_r_offset (const void *p)
8857 {
8858 union aligned32
8859 {
8860 uint32_t v;
8861 unsigned char c[4];
8862 };
8863 const union aligned32 *a
8864 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8865
8866 uint32_t aval = ( (uint32_t) a->c[0] << 24
8867 | (uint32_t) a->c[1] << 16
8868 | (uint32_t) a->c[2] << 8
8869 | (uint32_t) a->c[3]);
8870 return aval;
8871 }
8872
8873 #ifdef BFD_HOST_64_BIT
8874 static bfd_vma
8875 ext64l_r_offset (const void *p)
8876 {
8877 union aligned64
8878 {
8879 uint64_t v;
8880 unsigned char c[8];
8881 };
8882 const union aligned64 *a
8883 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8884
8885 uint64_t aval = ( (uint64_t) a->c[0]
8886 | (uint64_t) a->c[1] << 8
8887 | (uint64_t) a->c[2] << 16
8888 | (uint64_t) a->c[3] << 24
8889 | (uint64_t) a->c[4] << 32
8890 | (uint64_t) a->c[5] << 40
8891 | (uint64_t) a->c[6] << 48
8892 | (uint64_t) a->c[7] << 56);
8893 return aval;
8894 }
8895
8896 static bfd_vma
8897 ext64b_r_offset (const void *p)
8898 {
8899 union aligned64
8900 {
8901 uint64_t v;
8902 unsigned char c[8];
8903 };
8904 const union aligned64 *a
8905 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8906
8907 uint64_t aval = ( (uint64_t) a->c[0] << 56
8908 | (uint64_t) a->c[1] << 48
8909 | (uint64_t) a->c[2] << 40
8910 | (uint64_t) a->c[3] << 32
8911 | (uint64_t) a->c[4] << 24
8912 | (uint64_t) a->c[5] << 16
8913 | (uint64_t) a->c[6] << 8
8914 | (uint64_t) a->c[7]);
8915 return aval;
8916 }
8917 #endif
8918
8919 /* When performing a relocatable link, the input relocations are
8920 preserved. But, if they reference global symbols, the indices
8921 referenced must be updated. Update all the relocations found in
8922 RELDATA. */
8923
8924 static bfd_boolean
8925 elf_link_adjust_relocs (bfd *abfd,
8926 asection *sec,
8927 struct bfd_elf_section_reloc_data *reldata,
8928 bfd_boolean sort,
8929 struct bfd_link_info *info)
8930 {
8931 unsigned int i;
8932 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8933 bfd_byte *erela;
8934 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8935 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8936 bfd_vma r_type_mask;
8937 int r_sym_shift;
8938 unsigned int count = reldata->count;
8939 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8940
8941 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8942 {
8943 swap_in = bed->s->swap_reloc_in;
8944 swap_out = bed->s->swap_reloc_out;
8945 }
8946 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8947 {
8948 swap_in = bed->s->swap_reloca_in;
8949 swap_out = bed->s->swap_reloca_out;
8950 }
8951 else
8952 abort ();
8953
8954 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8955 abort ();
8956
8957 if (bed->s->arch_size == 32)
8958 {
8959 r_type_mask = 0xff;
8960 r_sym_shift = 8;
8961 }
8962 else
8963 {
8964 r_type_mask = 0xffffffff;
8965 r_sym_shift = 32;
8966 }
8967
8968 erela = reldata->hdr->contents;
8969 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8970 {
8971 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8972 unsigned int j;
8973
8974 if (*rel_hash == NULL)
8975 continue;
8976
8977 if ((*rel_hash)->indx == -2
8978 && info->gc_sections
8979 && ! info->gc_keep_exported)
8980 {
8981 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8982 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8983 abfd, sec,
8984 (*rel_hash)->root.root.string);
8985 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8986 abfd, sec);
8987 bfd_set_error (bfd_error_invalid_operation);
8988 return FALSE;
8989 }
8990 BFD_ASSERT ((*rel_hash)->indx >= 0);
8991
8992 (*swap_in) (abfd, erela, irela);
8993 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8994 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8995 | (irela[j].r_info & r_type_mask));
8996 (*swap_out) (abfd, irela, erela);
8997 }
8998
8999 if (bed->elf_backend_update_relocs)
9000 (*bed->elf_backend_update_relocs) (sec, reldata);
9001
9002 if (sort && count != 0)
9003 {
9004 bfd_vma (*ext_r_off) (const void *);
9005 bfd_vma r_off;
9006 size_t elt_size;
9007 bfd_byte *base, *end, *p, *loc;
9008 bfd_byte *buf = NULL;
9009
9010 if (bed->s->arch_size == 32)
9011 {
9012 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9013 ext_r_off = ext32l_r_offset;
9014 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9015 ext_r_off = ext32b_r_offset;
9016 else
9017 abort ();
9018 }
9019 else
9020 {
9021 #ifdef BFD_HOST_64_BIT
9022 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9023 ext_r_off = ext64l_r_offset;
9024 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9025 ext_r_off = ext64b_r_offset;
9026 else
9027 #endif
9028 abort ();
9029 }
9030
9031 /* Must use a stable sort here. A modified insertion sort,
9032 since the relocs are mostly sorted already. */
9033 elt_size = reldata->hdr->sh_entsize;
9034 base = reldata->hdr->contents;
9035 end = base + count * elt_size;
9036 if (elt_size > sizeof (Elf64_External_Rela))
9037 abort ();
9038
9039 /* Ensure the first element is lowest. This acts as a sentinel,
9040 speeding the main loop below. */
9041 r_off = (*ext_r_off) (base);
9042 for (p = loc = base; (p += elt_size) < end; )
9043 {
9044 bfd_vma r_off2 = (*ext_r_off) (p);
9045 if (r_off > r_off2)
9046 {
9047 r_off = r_off2;
9048 loc = p;
9049 }
9050 }
9051 if (loc != base)
9052 {
9053 /* Don't just swap *base and *loc as that changes the order
9054 of the original base[0] and base[1] if they happen to
9055 have the same r_offset. */
9056 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9057 memcpy (onebuf, loc, elt_size);
9058 memmove (base + elt_size, base, loc - base);
9059 memcpy (base, onebuf, elt_size);
9060 }
9061
9062 for (p = base + elt_size; (p += elt_size) < end; )
9063 {
9064 /* base to p is sorted, *p is next to insert. */
9065 r_off = (*ext_r_off) (p);
9066 /* Search the sorted region for location to insert. */
9067 loc = p - elt_size;
9068 while (r_off < (*ext_r_off) (loc))
9069 loc -= elt_size;
9070 loc += elt_size;
9071 if (loc != p)
9072 {
9073 /* Chances are there is a run of relocs to insert here,
9074 from one of more input files. Files are not always
9075 linked in order due to the way elf_link_input_bfd is
9076 called. See pr17666. */
9077 size_t sortlen = p - loc;
9078 bfd_vma r_off2 = (*ext_r_off) (loc);
9079 size_t runlen = elt_size;
9080 size_t buf_size = 96 * 1024;
9081 while (p + runlen < end
9082 && (sortlen <= buf_size
9083 || runlen + elt_size <= buf_size)
9084 && r_off2 > (*ext_r_off) (p + runlen))
9085 runlen += elt_size;
9086 if (buf == NULL)
9087 {
9088 buf = bfd_malloc (buf_size);
9089 if (buf == NULL)
9090 return FALSE;
9091 }
9092 if (runlen < sortlen)
9093 {
9094 memcpy (buf, p, runlen);
9095 memmove (loc + runlen, loc, sortlen);
9096 memcpy (loc, buf, runlen);
9097 }
9098 else
9099 {
9100 memcpy (buf, loc, sortlen);
9101 memmove (loc, p, runlen);
9102 memcpy (loc + runlen, buf, sortlen);
9103 }
9104 p += runlen - elt_size;
9105 }
9106 }
9107 /* Hashes are no longer valid. */
9108 free (reldata->hashes);
9109 reldata->hashes = NULL;
9110 free (buf);
9111 }
9112 return TRUE;
9113 }
9114
9115 struct elf_link_sort_rela
9116 {
9117 union {
9118 bfd_vma offset;
9119 bfd_vma sym_mask;
9120 } u;
9121 enum elf_reloc_type_class type;
9122 /* We use this as an array of size int_rels_per_ext_rel. */
9123 Elf_Internal_Rela rela[1];
9124 };
9125
9126 /* qsort stability here and for cmp2 is only an issue if multiple
9127 dynamic relocations are emitted at the same address. But targets
9128 that apply a series of dynamic relocations each operating on the
9129 result of the prior relocation can't use -z combreloc as
9130 implemented anyway. Such schemes tend to be broken by sorting on
9131 symbol index. That leaves dynamic NONE relocs as the only other
9132 case where ld might emit multiple relocs at the same address, and
9133 those are only emitted due to target bugs. */
9134
9135 static int
9136 elf_link_sort_cmp1 (const void *A, const void *B)
9137 {
9138 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9139 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9140 int relativea, relativeb;
9141
9142 relativea = a->type == reloc_class_relative;
9143 relativeb = b->type == reloc_class_relative;
9144
9145 if (relativea < relativeb)
9146 return 1;
9147 if (relativea > relativeb)
9148 return -1;
9149 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9150 return -1;
9151 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9152 return 1;
9153 if (a->rela->r_offset < b->rela->r_offset)
9154 return -1;
9155 if (a->rela->r_offset > b->rela->r_offset)
9156 return 1;
9157 return 0;
9158 }
9159
9160 static int
9161 elf_link_sort_cmp2 (const void *A, const void *B)
9162 {
9163 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9164 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9165
9166 if (a->type < b->type)
9167 return -1;
9168 if (a->type > b->type)
9169 return 1;
9170 if (a->u.offset < b->u.offset)
9171 return -1;
9172 if (a->u.offset > b->u.offset)
9173 return 1;
9174 if (a->rela->r_offset < b->rela->r_offset)
9175 return -1;
9176 if (a->rela->r_offset > b->rela->r_offset)
9177 return 1;
9178 return 0;
9179 }
9180
9181 static size_t
9182 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9183 {
9184 asection *dynamic_relocs;
9185 asection *rela_dyn;
9186 asection *rel_dyn;
9187 bfd_size_type count, size;
9188 size_t i, ret, sort_elt, ext_size;
9189 bfd_byte *sort, *s_non_relative, *p;
9190 struct elf_link_sort_rela *sq;
9191 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9192 int i2e = bed->s->int_rels_per_ext_rel;
9193 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9194 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9195 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9196 struct bfd_link_order *lo;
9197 bfd_vma r_sym_mask;
9198 bfd_boolean use_rela;
9199
9200 /* Find a dynamic reloc section. */
9201 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9202 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9203 if (rela_dyn != NULL && rela_dyn->size > 0
9204 && rel_dyn != NULL && rel_dyn->size > 0)
9205 {
9206 bfd_boolean use_rela_initialised = FALSE;
9207
9208 /* This is just here to stop gcc from complaining.
9209 Its initialization checking code is not perfect. */
9210 use_rela = TRUE;
9211
9212 /* Both sections are present. Examine the sizes
9213 of the indirect sections to help us choose. */
9214 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9215 if (lo->type == bfd_indirect_link_order)
9216 {
9217 asection *o = lo->u.indirect.section;
9218
9219 if ((o->size % bed->s->sizeof_rela) == 0)
9220 {
9221 if ((o->size % bed->s->sizeof_rel) == 0)
9222 /* Section size is divisible by both rel and rela sizes.
9223 It is of no help to us. */
9224 ;
9225 else
9226 {
9227 /* Section size is only divisible by rela. */
9228 if (use_rela_initialised && !use_rela)
9229 {
9230 _bfd_error_handler (_("%pB: unable to sort relocs - "
9231 "they are in more than one size"),
9232 abfd);
9233 bfd_set_error (bfd_error_invalid_operation);
9234 return 0;
9235 }
9236 else
9237 {
9238 use_rela = TRUE;
9239 use_rela_initialised = TRUE;
9240 }
9241 }
9242 }
9243 else if ((o->size % bed->s->sizeof_rel) == 0)
9244 {
9245 /* Section size is only divisible by rel. */
9246 if (use_rela_initialised && use_rela)
9247 {
9248 _bfd_error_handler (_("%pB: unable to sort relocs - "
9249 "they are in more than one size"),
9250 abfd);
9251 bfd_set_error (bfd_error_invalid_operation);
9252 return 0;
9253 }
9254 else
9255 {
9256 use_rela = FALSE;
9257 use_rela_initialised = TRUE;
9258 }
9259 }
9260 else
9261 {
9262 /* The section size is not divisible by either -
9263 something is wrong. */
9264 _bfd_error_handler (_("%pB: unable to sort relocs - "
9265 "they are of an unknown size"), abfd);
9266 bfd_set_error (bfd_error_invalid_operation);
9267 return 0;
9268 }
9269 }
9270
9271 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9272 if (lo->type == bfd_indirect_link_order)
9273 {
9274 asection *o = lo->u.indirect.section;
9275
9276 if ((o->size % bed->s->sizeof_rela) == 0)
9277 {
9278 if ((o->size % bed->s->sizeof_rel) == 0)
9279 /* Section size is divisible by both rel and rela sizes.
9280 It is of no help to us. */
9281 ;
9282 else
9283 {
9284 /* Section size is only divisible by rela. */
9285 if (use_rela_initialised && !use_rela)
9286 {
9287 _bfd_error_handler (_("%pB: unable to sort relocs - "
9288 "they are in more than one size"),
9289 abfd);
9290 bfd_set_error (bfd_error_invalid_operation);
9291 return 0;
9292 }
9293 else
9294 {
9295 use_rela = TRUE;
9296 use_rela_initialised = TRUE;
9297 }
9298 }
9299 }
9300 else if ((o->size % bed->s->sizeof_rel) == 0)
9301 {
9302 /* Section size is only divisible by rel. */
9303 if (use_rela_initialised && use_rela)
9304 {
9305 _bfd_error_handler (_("%pB: unable to sort relocs - "
9306 "they are in more than one size"),
9307 abfd);
9308 bfd_set_error (bfd_error_invalid_operation);
9309 return 0;
9310 }
9311 else
9312 {
9313 use_rela = FALSE;
9314 use_rela_initialised = TRUE;
9315 }
9316 }
9317 else
9318 {
9319 /* The section size is not divisible by either -
9320 something is wrong. */
9321 _bfd_error_handler (_("%pB: unable to sort relocs - "
9322 "they are of an unknown size"), abfd);
9323 bfd_set_error (bfd_error_invalid_operation);
9324 return 0;
9325 }
9326 }
9327
9328 if (! use_rela_initialised)
9329 /* Make a guess. */
9330 use_rela = TRUE;
9331 }
9332 else if (rela_dyn != NULL && rela_dyn->size > 0)
9333 use_rela = TRUE;
9334 else if (rel_dyn != NULL && rel_dyn->size > 0)
9335 use_rela = FALSE;
9336 else
9337 return 0;
9338
9339 if (use_rela)
9340 {
9341 dynamic_relocs = rela_dyn;
9342 ext_size = bed->s->sizeof_rela;
9343 swap_in = bed->s->swap_reloca_in;
9344 swap_out = bed->s->swap_reloca_out;
9345 }
9346 else
9347 {
9348 dynamic_relocs = rel_dyn;
9349 ext_size = bed->s->sizeof_rel;
9350 swap_in = bed->s->swap_reloc_in;
9351 swap_out = bed->s->swap_reloc_out;
9352 }
9353
9354 size = 0;
9355 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9356 if (lo->type == bfd_indirect_link_order)
9357 size += lo->u.indirect.section->size;
9358
9359 if (size != dynamic_relocs->size)
9360 return 0;
9361
9362 sort_elt = (sizeof (struct elf_link_sort_rela)
9363 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9364
9365 count = dynamic_relocs->size / ext_size;
9366 if (count == 0)
9367 return 0;
9368 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9369
9370 if (sort == NULL)
9371 {
9372 (*info->callbacks->warning)
9373 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9374 return 0;
9375 }
9376
9377 if (bed->s->arch_size == 32)
9378 r_sym_mask = ~(bfd_vma) 0xff;
9379 else
9380 r_sym_mask = ~(bfd_vma) 0xffffffff;
9381
9382 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9383 if (lo->type == bfd_indirect_link_order)
9384 {
9385 bfd_byte *erel, *erelend;
9386 asection *o = lo->u.indirect.section;
9387
9388 if (o->contents == NULL && o->size != 0)
9389 {
9390 /* This is a reloc section that is being handled as a normal
9391 section. See bfd_section_from_shdr. We can't combine
9392 relocs in this case. */
9393 free (sort);
9394 return 0;
9395 }
9396 erel = o->contents;
9397 erelend = o->contents + o->size;
9398 p = sort + o->output_offset * opb / ext_size * sort_elt;
9399
9400 while (erel < erelend)
9401 {
9402 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9403
9404 (*swap_in) (abfd, erel, s->rela);
9405 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9406 s->u.sym_mask = r_sym_mask;
9407 p += sort_elt;
9408 erel += ext_size;
9409 }
9410 }
9411
9412 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9413
9414 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9415 {
9416 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9417 if (s->type != reloc_class_relative)
9418 break;
9419 }
9420 ret = i;
9421 s_non_relative = p;
9422
9423 sq = (struct elf_link_sort_rela *) s_non_relative;
9424 for (; i < count; i++, p += sort_elt)
9425 {
9426 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9427 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9428 sq = sp;
9429 sp->u.offset = sq->rela->r_offset;
9430 }
9431
9432 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9433
9434 struct elf_link_hash_table *htab = elf_hash_table (info);
9435 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9436 {
9437 /* We have plt relocs in .rela.dyn. */
9438 sq = (struct elf_link_sort_rela *) sort;
9439 for (i = 0; i < count; i++)
9440 if (sq[count - i - 1].type != reloc_class_plt)
9441 break;
9442 if (i != 0 && htab->srelplt->size == i * ext_size)
9443 {
9444 struct bfd_link_order **plo;
9445 /* Put srelplt link_order last. This is so the output_offset
9446 set in the next loop is correct for DT_JMPREL. */
9447 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9448 if ((*plo)->type == bfd_indirect_link_order
9449 && (*plo)->u.indirect.section == htab->srelplt)
9450 {
9451 lo = *plo;
9452 *plo = lo->next;
9453 }
9454 else
9455 plo = &(*plo)->next;
9456 *plo = lo;
9457 lo->next = NULL;
9458 dynamic_relocs->map_tail.link_order = lo;
9459 }
9460 }
9461
9462 p = sort;
9463 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9464 if (lo->type == bfd_indirect_link_order)
9465 {
9466 bfd_byte *erel, *erelend;
9467 asection *o = lo->u.indirect.section;
9468
9469 erel = o->contents;
9470 erelend = o->contents + o->size;
9471 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9472 while (erel < erelend)
9473 {
9474 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9475 (*swap_out) (abfd, s->rela, erel);
9476 p += sort_elt;
9477 erel += ext_size;
9478 }
9479 }
9480
9481 free (sort);
9482 *psec = dynamic_relocs;
9483 return ret;
9484 }
9485
9486 /* Add a symbol to the output symbol string table. */
9487
9488 static int
9489 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9490 const char *name,
9491 Elf_Internal_Sym *elfsym,
9492 asection *input_sec,
9493 struct elf_link_hash_entry *h)
9494 {
9495 int (*output_symbol_hook)
9496 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9497 struct elf_link_hash_entry *);
9498 struct elf_link_hash_table *hash_table;
9499 const struct elf_backend_data *bed;
9500 bfd_size_type strtabsize;
9501
9502 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9503
9504 bed = get_elf_backend_data (flinfo->output_bfd);
9505 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9506 if (output_symbol_hook != NULL)
9507 {
9508 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9509 if (ret != 1)
9510 return ret;
9511 }
9512
9513 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9514 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9515 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9516 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9517
9518 if (name == NULL
9519 || *name == '\0'
9520 || (input_sec->flags & SEC_EXCLUDE))
9521 elfsym->st_name = (unsigned long) -1;
9522 else
9523 {
9524 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9525 to get the final offset for st_name. */
9526 elfsym->st_name
9527 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9528 name, FALSE);
9529 if (elfsym->st_name == (unsigned long) -1)
9530 return 0;
9531 }
9532
9533 hash_table = elf_hash_table (flinfo->info);
9534 strtabsize = hash_table->strtabsize;
9535 if (strtabsize <= hash_table->strtabcount)
9536 {
9537 strtabsize += strtabsize;
9538 hash_table->strtabsize = strtabsize;
9539 strtabsize *= sizeof (*hash_table->strtab);
9540 hash_table->strtab
9541 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9542 strtabsize);
9543 if (hash_table->strtab == NULL)
9544 return 0;
9545 }
9546 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9547 hash_table->strtab[hash_table->strtabcount].dest_index
9548 = hash_table->strtabcount;
9549 hash_table->strtab[hash_table->strtabcount].destshndx_index
9550 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9551
9552 flinfo->output_bfd->symcount += 1;
9553 hash_table->strtabcount += 1;
9554
9555 return 1;
9556 }
9557
9558 /* Swap symbols out to the symbol table and flush the output symbols to
9559 the file. */
9560
9561 static bfd_boolean
9562 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9563 {
9564 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9565 bfd_size_type amt;
9566 size_t i;
9567 const struct elf_backend_data *bed;
9568 bfd_byte *symbuf;
9569 Elf_Internal_Shdr *hdr;
9570 file_ptr pos;
9571 bfd_boolean ret;
9572
9573 if (!hash_table->strtabcount)
9574 return TRUE;
9575
9576 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9577
9578 bed = get_elf_backend_data (flinfo->output_bfd);
9579
9580 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9581 symbuf = (bfd_byte *) bfd_malloc (amt);
9582 if (symbuf == NULL)
9583 return FALSE;
9584
9585 if (flinfo->symshndxbuf)
9586 {
9587 amt = sizeof (Elf_External_Sym_Shndx);
9588 amt *= bfd_get_symcount (flinfo->output_bfd);
9589 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9590 if (flinfo->symshndxbuf == NULL)
9591 {
9592 free (symbuf);
9593 return FALSE;
9594 }
9595 }
9596
9597 for (i = 0; i < hash_table->strtabcount; i++)
9598 {
9599 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9600 if (elfsym->sym.st_name == (unsigned long) -1)
9601 elfsym->sym.st_name = 0;
9602 else
9603 elfsym->sym.st_name
9604 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9605 elfsym->sym.st_name);
9606 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9607 ((bfd_byte *) symbuf
9608 + (elfsym->dest_index
9609 * bed->s->sizeof_sym)),
9610 (flinfo->symshndxbuf
9611 + elfsym->destshndx_index));
9612 }
9613
9614 /* Allow the linker to examine the strtab and symtab now they are
9615 populated. */
9616
9617 if (flinfo->info->callbacks->examine_strtab)
9618 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9619 hash_table->strtabcount,
9620 flinfo->symstrtab);
9621
9622 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9623 pos = hdr->sh_offset + hdr->sh_size;
9624 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9625 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9626 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9627 {
9628 hdr->sh_size += amt;
9629 ret = TRUE;
9630 }
9631 else
9632 ret = FALSE;
9633
9634 free (symbuf);
9635
9636 free (hash_table->strtab);
9637 hash_table->strtab = NULL;
9638
9639 return ret;
9640 }
9641
9642 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9643
9644 static bfd_boolean
9645 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9646 {
9647 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9648 && sym->st_shndx < SHN_LORESERVE)
9649 {
9650 /* The gABI doesn't support dynamic symbols in output sections
9651 beyond 64k. */
9652 _bfd_error_handler
9653 /* xgettext:c-format */
9654 (_("%pB: too many sections: %d (>= %d)"),
9655 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9656 bfd_set_error (bfd_error_nonrepresentable_section);
9657 return FALSE;
9658 }
9659 return TRUE;
9660 }
9661
9662 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9663 allowing an unsatisfied unversioned symbol in the DSO to match a
9664 versioned symbol that would normally require an explicit version.
9665 We also handle the case that a DSO references a hidden symbol
9666 which may be satisfied by a versioned symbol in another DSO. */
9667
9668 static bfd_boolean
9669 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9670 const struct elf_backend_data *bed,
9671 struct elf_link_hash_entry *h)
9672 {
9673 bfd *abfd;
9674 struct elf_link_loaded_list *loaded;
9675
9676 if (!is_elf_hash_table (info->hash))
9677 return FALSE;
9678
9679 /* Check indirect symbol. */
9680 while (h->root.type == bfd_link_hash_indirect)
9681 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9682
9683 switch (h->root.type)
9684 {
9685 default:
9686 abfd = NULL;
9687 break;
9688
9689 case bfd_link_hash_undefined:
9690 case bfd_link_hash_undefweak:
9691 abfd = h->root.u.undef.abfd;
9692 if (abfd == NULL
9693 || (abfd->flags & DYNAMIC) == 0
9694 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9695 return FALSE;
9696 break;
9697
9698 case bfd_link_hash_defined:
9699 case bfd_link_hash_defweak:
9700 abfd = h->root.u.def.section->owner;
9701 break;
9702
9703 case bfd_link_hash_common:
9704 abfd = h->root.u.c.p->section->owner;
9705 break;
9706 }
9707 BFD_ASSERT (abfd != NULL);
9708
9709 for (loaded = elf_hash_table (info)->loaded;
9710 loaded != NULL;
9711 loaded = loaded->next)
9712 {
9713 bfd *input;
9714 Elf_Internal_Shdr *hdr;
9715 size_t symcount;
9716 size_t extsymcount;
9717 size_t extsymoff;
9718 Elf_Internal_Shdr *versymhdr;
9719 Elf_Internal_Sym *isym;
9720 Elf_Internal_Sym *isymend;
9721 Elf_Internal_Sym *isymbuf;
9722 Elf_External_Versym *ever;
9723 Elf_External_Versym *extversym;
9724
9725 input = loaded->abfd;
9726
9727 /* We check each DSO for a possible hidden versioned definition. */
9728 if (input == abfd
9729 || (input->flags & DYNAMIC) == 0
9730 || elf_dynversym (input) == 0)
9731 continue;
9732
9733 hdr = &elf_tdata (input)->dynsymtab_hdr;
9734
9735 symcount = hdr->sh_size / bed->s->sizeof_sym;
9736 if (elf_bad_symtab (input))
9737 {
9738 extsymcount = symcount;
9739 extsymoff = 0;
9740 }
9741 else
9742 {
9743 extsymcount = symcount - hdr->sh_info;
9744 extsymoff = hdr->sh_info;
9745 }
9746
9747 if (extsymcount == 0)
9748 continue;
9749
9750 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9751 NULL, NULL, NULL);
9752 if (isymbuf == NULL)
9753 return FALSE;
9754
9755 /* Read in any version definitions. */
9756 versymhdr = &elf_tdata (input)->dynversym_hdr;
9757 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9758 if (extversym == NULL)
9759 goto error_ret;
9760
9761 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9762 || (bfd_bread (extversym, versymhdr->sh_size, input)
9763 != versymhdr->sh_size))
9764 {
9765 free (extversym);
9766 error_ret:
9767 free (isymbuf);
9768 return FALSE;
9769 }
9770
9771 ever = extversym + extsymoff;
9772 isymend = isymbuf + extsymcount;
9773 for (isym = isymbuf; isym < isymend; isym++, ever++)
9774 {
9775 const char *name;
9776 Elf_Internal_Versym iver;
9777 unsigned short version_index;
9778
9779 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9780 || isym->st_shndx == SHN_UNDEF)
9781 continue;
9782
9783 name = bfd_elf_string_from_elf_section (input,
9784 hdr->sh_link,
9785 isym->st_name);
9786 if (strcmp (name, h->root.root.string) != 0)
9787 continue;
9788
9789 _bfd_elf_swap_versym_in (input, ever, &iver);
9790
9791 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9792 && !(h->def_regular
9793 && h->forced_local))
9794 {
9795 /* If we have a non-hidden versioned sym, then it should
9796 have provided a definition for the undefined sym unless
9797 it is defined in a non-shared object and forced local.
9798 */
9799 abort ();
9800 }
9801
9802 version_index = iver.vs_vers & VERSYM_VERSION;
9803 if (version_index == 1 || version_index == 2)
9804 {
9805 /* This is the base or first version. We can use it. */
9806 free (extversym);
9807 free (isymbuf);
9808 return TRUE;
9809 }
9810 }
9811
9812 free (extversym);
9813 free (isymbuf);
9814 }
9815
9816 return FALSE;
9817 }
9818
9819 /* Convert ELF common symbol TYPE. */
9820
9821 static int
9822 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9823 {
9824 /* Commom symbol can only appear in relocatable link. */
9825 if (!bfd_link_relocatable (info))
9826 abort ();
9827 switch (info->elf_stt_common)
9828 {
9829 case unchanged:
9830 break;
9831 case elf_stt_common:
9832 type = STT_COMMON;
9833 break;
9834 case no_elf_stt_common:
9835 type = STT_OBJECT;
9836 break;
9837 }
9838 return type;
9839 }
9840
9841 /* Add an external symbol to the symbol table. This is called from
9842 the hash table traversal routine. When generating a shared object,
9843 we go through the symbol table twice. The first time we output
9844 anything that might have been forced to local scope in a version
9845 script. The second time we output the symbols that are still
9846 global symbols. */
9847
9848 static bfd_boolean
9849 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9850 {
9851 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9852 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9853 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9854 bfd_boolean strip;
9855 Elf_Internal_Sym sym;
9856 asection *input_sec;
9857 const struct elf_backend_data *bed;
9858 long indx;
9859 int ret;
9860 unsigned int type;
9861
9862 if (h->root.type == bfd_link_hash_warning)
9863 {
9864 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9865 if (h->root.type == bfd_link_hash_new)
9866 return TRUE;
9867 }
9868
9869 /* Decide whether to output this symbol in this pass. */
9870 if (eoinfo->localsyms)
9871 {
9872 if (!h->forced_local)
9873 return TRUE;
9874 }
9875 else
9876 {
9877 if (h->forced_local)
9878 return TRUE;
9879 }
9880
9881 bed = get_elf_backend_data (flinfo->output_bfd);
9882
9883 if (h->root.type == bfd_link_hash_undefined)
9884 {
9885 /* If we have an undefined symbol reference here then it must have
9886 come from a shared library that is being linked in. (Undefined
9887 references in regular files have already been handled unless
9888 they are in unreferenced sections which are removed by garbage
9889 collection). */
9890 bfd_boolean ignore_undef = FALSE;
9891
9892 /* Some symbols may be special in that the fact that they're
9893 undefined can be safely ignored - let backend determine that. */
9894 if (bed->elf_backend_ignore_undef_symbol)
9895 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9896
9897 /* If we are reporting errors for this situation then do so now. */
9898 if (!ignore_undef
9899 && h->ref_dynamic_nonweak
9900 && (!h->ref_regular || flinfo->info->gc_sections)
9901 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9902 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9903 (*flinfo->info->callbacks->undefined_symbol)
9904 (flinfo->info, h->root.root.string,
9905 h->ref_regular ? NULL : h->root.u.undef.abfd,
9906 NULL, 0,
9907 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9908
9909 /* Strip a global symbol defined in a discarded section. */
9910 if (h->indx == -3)
9911 return TRUE;
9912 }
9913
9914 /* We should also warn if a forced local symbol is referenced from
9915 shared libraries. */
9916 if (bfd_link_executable (flinfo->info)
9917 && h->forced_local
9918 && h->ref_dynamic
9919 && h->def_regular
9920 && !h->dynamic_def
9921 && h->ref_dynamic_nonweak
9922 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9923 {
9924 bfd *def_bfd;
9925 const char *msg;
9926 struct elf_link_hash_entry *hi = h;
9927
9928 /* Check indirect symbol. */
9929 while (hi->root.type == bfd_link_hash_indirect)
9930 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9931
9932 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9933 /* xgettext:c-format */
9934 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9935 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9936 /* xgettext:c-format */
9937 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9938 else
9939 /* xgettext:c-format */
9940 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9941 def_bfd = flinfo->output_bfd;
9942 if (hi->root.u.def.section != bfd_abs_section_ptr)
9943 def_bfd = hi->root.u.def.section->owner;
9944 _bfd_error_handler (msg, flinfo->output_bfd,
9945 h->root.root.string, def_bfd);
9946 bfd_set_error (bfd_error_bad_value);
9947 eoinfo->failed = TRUE;
9948 return FALSE;
9949 }
9950
9951 /* We don't want to output symbols that have never been mentioned by
9952 a regular file, or that we have been told to strip. However, if
9953 h->indx is set to -2, the symbol is used by a reloc and we must
9954 output it. */
9955 strip = FALSE;
9956 if (h->indx == -2)
9957 ;
9958 else if ((h->def_dynamic
9959 || h->ref_dynamic
9960 || h->root.type == bfd_link_hash_new)
9961 && !h->def_regular
9962 && !h->ref_regular)
9963 strip = TRUE;
9964 else if (flinfo->info->strip == strip_all)
9965 strip = TRUE;
9966 else if (flinfo->info->strip == strip_some
9967 && bfd_hash_lookup (flinfo->info->keep_hash,
9968 h->root.root.string, FALSE, FALSE) == NULL)
9969 strip = TRUE;
9970 else if ((h->root.type == bfd_link_hash_defined
9971 || h->root.type == bfd_link_hash_defweak)
9972 && ((flinfo->info->strip_discarded
9973 && discarded_section (h->root.u.def.section))
9974 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9975 && h->root.u.def.section->owner != NULL
9976 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9977 strip = TRUE;
9978 else if ((h->root.type == bfd_link_hash_undefined
9979 || h->root.type == bfd_link_hash_undefweak)
9980 && h->root.u.undef.abfd != NULL
9981 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9982 strip = TRUE;
9983
9984 type = h->type;
9985
9986 /* If we're stripping it, and it's not a dynamic symbol, there's
9987 nothing else to do. However, if it is a forced local symbol or
9988 an ifunc symbol we need to give the backend finish_dynamic_symbol
9989 function a chance to make it dynamic. */
9990 if (strip
9991 && h->dynindx == -1
9992 && type != STT_GNU_IFUNC
9993 && !h->forced_local)
9994 return TRUE;
9995
9996 sym.st_value = 0;
9997 sym.st_size = h->size;
9998 sym.st_other = h->other;
9999 switch (h->root.type)
10000 {
10001 default:
10002 case bfd_link_hash_new:
10003 case bfd_link_hash_warning:
10004 abort ();
10005 return FALSE;
10006
10007 case bfd_link_hash_undefined:
10008 case bfd_link_hash_undefweak:
10009 input_sec = bfd_und_section_ptr;
10010 sym.st_shndx = SHN_UNDEF;
10011 break;
10012
10013 case bfd_link_hash_defined:
10014 case bfd_link_hash_defweak:
10015 {
10016 input_sec = h->root.u.def.section;
10017 if (input_sec->output_section != NULL)
10018 {
10019 sym.st_shndx =
10020 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10021 input_sec->output_section);
10022 if (sym.st_shndx == SHN_BAD)
10023 {
10024 _bfd_error_handler
10025 /* xgettext:c-format */
10026 (_("%pB: could not find output section %pA for input section %pA"),
10027 flinfo->output_bfd, input_sec->output_section, input_sec);
10028 bfd_set_error (bfd_error_nonrepresentable_section);
10029 eoinfo->failed = TRUE;
10030 return FALSE;
10031 }
10032
10033 /* ELF symbols in relocatable files are section relative,
10034 but in nonrelocatable files they are virtual
10035 addresses. */
10036 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10037 if (!bfd_link_relocatable (flinfo->info))
10038 {
10039 sym.st_value += input_sec->output_section->vma;
10040 if (h->type == STT_TLS)
10041 {
10042 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10043 if (tls_sec != NULL)
10044 sym.st_value -= tls_sec->vma;
10045 }
10046 }
10047 }
10048 else
10049 {
10050 BFD_ASSERT (input_sec->owner == NULL
10051 || (input_sec->owner->flags & DYNAMIC) != 0);
10052 sym.st_shndx = SHN_UNDEF;
10053 input_sec = bfd_und_section_ptr;
10054 }
10055 }
10056 break;
10057
10058 case bfd_link_hash_common:
10059 input_sec = h->root.u.c.p->section;
10060 sym.st_shndx = bed->common_section_index (input_sec);
10061 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10062 break;
10063
10064 case bfd_link_hash_indirect:
10065 /* These symbols are created by symbol versioning. They point
10066 to the decorated version of the name. For example, if the
10067 symbol foo@@GNU_1.2 is the default, which should be used when
10068 foo is used with no version, then we add an indirect symbol
10069 foo which points to foo@@GNU_1.2. We ignore these symbols,
10070 since the indirected symbol is already in the hash table. */
10071 return TRUE;
10072 }
10073
10074 if (type == STT_COMMON || type == STT_OBJECT)
10075 switch (h->root.type)
10076 {
10077 case bfd_link_hash_common:
10078 type = elf_link_convert_common_type (flinfo->info, type);
10079 break;
10080 case bfd_link_hash_defined:
10081 case bfd_link_hash_defweak:
10082 if (bed->common_definition (&sym))
10083 type = elf_link_convert_common_type (flinfo->info, type);
10084 else
10085 type = STT_OBJECT;
10086 break;
10087 case bfd_link_hash_undefined:
10088 case bfd_link_hash_undefweak:
10089 break;
10090 default:
10091 abort ();
10092 }
10093
10094 if (h->forced_local)
10095 {
10096 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10097 /* Turn off visibility on local symbol. */
10098 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10099 }
10100 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10101 else if (h->unique_global && h->def_regular)
10102 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10103 else if (h->root.type == bfd_link_hash_undefweak
10104 || h->root.type == bfd_link_hash_defweak)
10105 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10106 else
10107 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10108 sym.st_target_internal = h->target_internal;
10109
10110 /* Give the processor backend a chance to tweak the symbol value,
10111 and also to finish up anything that needs to be done for this
10112 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10113 forced local syms when non-shared is due to a historical quirk.
10114 STT_GNU_IFUNC symbol must go through PLT. */
10115 if ((h->type == STT_GNU_IFUNC
10116 && h->def_regular
10117 && !bfd_link_relocatable (flinfo->info))
10118 || ((h->dynindx != -1
10119 || h->forced_local)
10120 && ((bfd_link_pic (flinfo->info)
10121 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10122 || h->root.type != bfd_link_hash_undefweak))
10123 || !h->forced_local)
10124 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10125 {
10126 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10127 (flinfo->output_bfd, flinfo->info, h, &sym)))
10128 {
10129 eoinfo->failed = TRUE;
10130 return FALSE;
10131 }
10132 }
10133
10134 /* If we are marking the symbol as undefined, and there are no
10135 non-weak references to this symbol from a regular object, then
10136 mark the symbol as weak undefined; if there are non-weak
10137 references, mark the symbol as strong. We can't do this earlier,
10138 because it might not be marked as undefined until the
10139 finish_dynamic_symbol routine gets through with it. */
10140 if (sym.st_shndx == SHN_UNDEF
10141 && h->ref_regular
10142 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10143 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10144 {
10145 int bindtype;
10146 type = ELF_ST_TYPE (sym.st_info);
10147
10148 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10149 if (type == STT_GNU_IFUNC)
10150 type = STT_FUNC;
10151
10152 if (h->ref_regular_nonweak)
10153 bindtype = STB_GLOBAL;
10154 else
10155 bindtype = STB_WEAK;
10156 sym.st_info = ELF_ST_INFO (bindtype, type);
10157 }
10158
10159 /* If this is a symbol defined in a dynamic library, don't use the
10160 symbol size from the dynamic library. Relinking an executable
10161 against a new library may introduce gratuitous changes in the
10162 executable's symbols if we keep the size. */
10163 if (sym.st_shndx == SHN_UNDEF
10164 && !h->def_regular
10165 && h->def_dynamic)
10166 sym.st_size = 0;
10167
10168 /* If a non-weak symbol with non-default visibility is not defined
10169 locally, it is a fatal error. */
10170 if (!bfd_link_relocatable (flinfo->info)
10171 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10172 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10173 && h->root.type == bfd_link_hash_undefined
10174 && !h->def_regular)
10175 {
10176 const char *msg;
10177
10178 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10179 /* xgettext:c-format */
10180 msg = _("%pB: protected symbol `%s' isn't defined");
10181 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10182 /* xgettext:c-format */
10183 msg = _("%pB: internal symbol `%s' isn't defined");
10184 else
10185 /* xgettext:c-format */
10186 msg = _("%pB: hidden symbol `%s' isn't defined");
10187 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10188 bfd_set_error (bfd_error_bad_value);
10189 eoinfo->failed = TRUE;
10190 return FALSE;
10191 }
10192
10193 /* If this symbol should be put in the .dynsym section, then put it
10194 there now. We already know the symbol index. We also fill in
10195 the entry in the .hash section. */
10196 if (h->dynindx != -1
10197 && elf_hash_table (flinfo->info)->dynamic_sections_created
10198 && elf_hash_table (flinfo->info)->dynsym != NULL
10199 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10200 {
10201 bfd_byte *esym;
10202
10203 /* Since there is no version information in the dynamic string,
10204 if there is no version info in symbol version section, we will
10205 have a run-time problem if not linking executable, referenced
10206 by shared library, or not bound locally. */
10207 if (h->verinfo.verdef == NULL
10208 && (!bfd_link_executable (flinfo->info)
10209 || h->ref_dynamic
10210 || !h->def_regular))
10211 {
10212 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10213
10214 if (p && p [1] != '\0')
10215 {
10216 _bfd_error_handler
10217 /* xgettext:c-format */
10218 (_("%pB: no symbol version section for versioned symbol `%s'"),
10219 flinfo->output_bfd, h->root.root.string);
10220 eoinfo->failed = TRUE;
10221 return FALSE;
10222 }
10223 }
10224
10225 sym.st_name = h->dynstr_index;
10226 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10227 + h->dynindx * bed->s->sizeof_sym);
10228 if (!check_dynsym (flinfo->output_bfd, &sym))
10229 {
10230 eoinfo->failed = TRUE;
10231 return FALSE;
10232 }
10233 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10234
10235 if (flinfo->hash_sec != NULL)
10236 {
10237 size_t hash_entry_size;
10238 bfd_byte *bucketpos;
10239 bfd_vma chain;
10240 size_t bucketcount;
10241 size_t bucket;
10242
10243 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10244 bucket = h->u.elf_hash_value % bucketcount;
10245
10246 hash_entry_size
10247 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10248 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10249 + (bucket + 2) * hash_entry_size);
10250 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10251 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10252 bucketpos);
10253 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10254 ((bfd_byte *) flinfo->hash_sec->contents
10255 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10256 }
10257
10258 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10259 {
10260 Elf_Internal_Versym iversym;
10261 Elf_External_Versym *eversym;
10262
10263 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10264 {
10265 if (h->verinfo.verdef == NULL
10266 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10267 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10268 iversym.vs_vers = 0;
10269 else
10270 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10271 }
10272 else
10273 {
10274 if (h->verinfo.vertree == NULL)
10275 iversym.vs_vers = 1;
10276 else
10277 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10278 if (flinfo->info->create_default_symver)
10279 iversym.vs_vers++;
10280 }
10281
10282 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10283 defined locally. */
10284 if (h->versioned == versioned_hidden && h->def_regular)
10285 iversym.vs_vers |= VERSYM_HIDDEN;
10286
10287 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10288 eversym += h->dynindx;
10289 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10290 }
10291 }
10292
10293 /* If the symbol is undefined, and we didn't output it to .dynsym,
10294 strip it from .symtab too. Obviously we can't do this for
10295 relocatable output or when needed for --emit-relocs. */
10296 else if (input_sec == bfd_und_section_ptr
10297 && h->indx != -2
10298 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10299 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10300 && !bfd_link_relocatable (flinfo->info))
10301 return TRUE;
10302
10303 /* Also strip others that we couldn't earlier due to dynamic symbol
10304 processing. */
10305 if (strip)
10306 return TRUE;
10307 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10308 return TRUE;
10309
10310 /* Output a FILE symbol so that following locals are not associated
10311 with the wrong input file. We need one for forced local symbols
10312 if we've seen more than one FILE symbol or when we have exactly
10313 one FILE symbol but global symbols are present in a file other
10314 than the one with the FILE symbol. We also need one if linker
10315 defined symbols are present. In practice these conditions are
10316 always met, so just emit the FILE symbol unconditionally. */
10317 if (eoinfo->localsyms
10318 && !eoinfo->file_sym_done
10319 && eoinfo->flinfo->filesym_count != 0)
10320 {
10321 Elf_Internal_Sym fsym;
10322
10323 memset (&fsym, 0, sizeof (fsym));
10324 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10325 fsym.st_shndx = SHN_ABS;
10326 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10327 bfd_und_section_ptr, NULL))
10328 return FALSE;
10329
10330 eoinfo->file_sym_done = TRUE;
10331 }
10332
10333 indx = bfd_get_symcount (flinfo->output_bfd);
10334 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10335 input_sec, h);
10336 if (ret == 0)
10337 {
10338 eoinfo->failed = TRUE;
10339 return FALSE;
10340 }
10341 else if (ret == 1)
10342 h->indx = indx;
10343 else if (h->indx == -2)
10344 abort();
10345
10346 return TRUE;
10347 }
10348
10349 /* Return TRUE if special handling is done for relocs in SEC against
10350 symbols defined in discarded sections. */
10351
10352 static bfd_boolean
10353 elf_section_ignore_discarded_relocs (asection *sec)
10354 {
10355 const struct elf_backend_data *bed;
10356
10357 switch (sec->sec_info_type)
10358 {
10359 case SEC_INFO_TYPE_STABS:
10360 case SEC_INFO_TYPE_EH_FRAME:
10361 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10362 return TRUE;
10363 default:
10364 break;
10365 }
10366
10367 bed = get_elf_backend_data (sec->owner);
10368 if (bed->elf_backend_ignore_discarded_relocs != NULL
10369 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10370 return TRUE;
10371
10372 return FALSE;
10373 }
10374
10375 /* Return a mask saying how ld should treat relocations in SEC against
10376 symbols defined in discarded sections. If this function returns
10377 COMPLAIN set, ld will issue a warning message. If this function
10378 returns PRETEND set, and the discarded section was link-once and the
10379 same size as the kept link-once section, ld will pretend that the
10380 symbol was actually defined in the kept section. Otherwise ld will
10381 zero the reloc (at least that is the intent, but some cooperation by
10382 the target dependent code is needed, particularly for REL targets). */
10383
10384 unsigned int
10385 _bfd_elf_default_action_discarded (asection *sec)
10386 {
10387 if (sec->flags & SEC_DEBUGGING)
10388 return PRETEND;
10389
10390 if (strcmp (".eh_frame", sec->name) == 0)
10391 return 0;
10392
10393 if (strcmp (".gcc_except_table", sec->name) == 0)
10394 return 0;
10395
10396 return COMPLAIN | PRETEND;
10397 }
10398
10399 /* Find a match between a section and a member of a section group. */
10400
10401 static asection *
10402 match_group_member (asection *sec, asection *group,
10403 struct bfd_link_info *info)
10404 {
10405 asection *first = elf_next_in_group (group);
10406 asection *s = first;
10407
10408 while (s != NULL)
10409 {
10410 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10411 return s;
10412
10413 s = elf_next_in_group (s);
10414 if (s == first)
10415 break;
10416 }
10417
10418 return NULL;
10419 }
10420
10421 /* Check if the kept section of a discarded section SEC can be used
10422 to replace it. Return the replacement if it is OK. Otherwise return
10423 NULL. */
10424
10425 asection *
10426 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10427 {
10428 asection *kept;
10429
10430 kept = sec->kept_section;
10431 if (kept != NULL)
10432 {
10433 if ((kept->flags & SEC_GROUP) != 0)
10434 kept = match_group_member (sec, kept, info);
10435 if (kept != NULL
10436 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10437 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10438 kept = NULL;
10439 sec->kept_section = kept;
10440 }
10441 return kept;
10442 }
10443
10444 /* Link an input file into the linker output file. This function
10445 handles all the sections and relocations of the input file at once.
10446 This is so that we only have to read the local symbols once, and
10447 don't have to keep them in memory. */
10448
10449 static bfd_boolean
10450 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10451 {
10452 int (*relocate_section)
10453 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10454 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10455 bfd *output_bfd;
10456 Elf_Internal_Shdr *symtab_hdr;
10457 size_t locsymcount;
10458 size_t extsymoff;
10459 Elf_Internal_Sym *isymbuf;
10460 Elf_Internal_Sym *isym;
10461 Elf_Internal_Sym *isymend;
10462 long *pindex;
10463 asection **ppsection;
10464 asection *o;
10465 const struct elf_backend_data *bed;
10466 struct elf_link_hash_entry **sym_hashes;
10467 bfd_size_type address_size;
10468 bfd_vma r_type_mask;
10469 int r_sym_shift;
10470 bfd_boolean have_file_sym = FALSE;
10471
10472 output_bfd = flinfo->output_bfd;
10473 bed = get_elf_backend_data (output_bfd);
10474 relocate_section = bed->elf_backend_relocate_section;
10475
10476 /* If this is a dynamic object, we don't want to do anything here:
10477 we don't want the local symbols, and we don't want the section
10478 contents. */
10479 if ((input_bfd->flags & DYNAMIC) != 0)
10480 return TRUE;
10481
10482 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10483 if (elf_bad_symtab (input_bfd))
10484 {
10485 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10486 extsymoff = 0;
10487 }
10488 else
10489 {
10490 locsymcount = symtab_hdr->sh_info;
10491 extsymoff = symtab_hdr->sh_info;
10492 }
10493
10494 /* Read the local symbols. */
10495 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10496 if (isymbuf == NULL && locsymcount != 0)
10497 {
10498 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10499 flinfo->internal_syms,
10500 flinfo->external_syms,
10501 flinfo->locsym_shndx);
10502 if (isymbuf == NULL)
10503 return FALSE;
10504 }
10505
10506 /* Find local symbol sections and adjust values of symbols in
10507 SEC_MERGE sections. Write out those local symbols we know are
10508 going into the output file. */
10509 isymend = isymbuf + locsymcount;
10510 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10511 isym < isymend;
10512 isym++, pindex++, ppsection++)
10513 {
10514 asection *isec;
10515 const char *name;
10516 Elf_Internal_Sym osym;
10517 long indx;
10518 int ret;
10519
10520 *pindex = -1;
10521
10522 if (elf_bad_symtab (input_bfd))
10523 {
10524 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10525 {
10526 *ppsection = NULL;
10527 continue;
10528 }
10529 }
10530
10531 if (isym->st_shndx == SHN_UNDEF)
10532 isec = bfd_und_section_ptr;
10533 else if (isym->st_shndx == SHN_ABS)
10534 isec = bfd_abs_section_ptr;
10535 else if (isym->st_shndx == SHN_COMMON)
10536 isec = bfd_com_section_ptr;
10537 else
10538 {
10539 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10540 if (isec == NULL)
10541 {
10542 /* Don't attempt to output symbols with st_shnx in the
10543 reserved range other than SHN_ABS and SHN_COMMON. */
10544 isec = bfd_und_section_ptr;
10545 }
10546 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10547 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10548 isym->st_value =
10549 _bfd_merged_section_offset (output_bfd, &isec,
10550 elf_section_data (isec)->sec_info,
10551 isym->st_value);
10552 }
10553
10554 *ppsection = isec;
10555
10556 /* Don't output the first, undefined, symbol. In fact, don't
10557 output any undefined local symbol. */
10558 if (isec == bfd_und_section_ptr)
10559 continue;
10560
10561 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10562 {
10563 /* We never output section symbols. Instead, we use the
10564 section symbol of the corresponding section in the output
10565 file. */
10566 continue;
10567 }
10568
10569 /* If we are stripping all symbols, we don't want to output this
10570 one. */
10571 if (flinfo->info->strip == strip_all)
10572 continue;
10573
10574 /* If we are discarding all local symbols, we don't want to
10575 output this one. If we are generating a relocatable output
10576 file, then some of the local symbols may be required by
10577 relocs; we output them below as we discover that they are
10578 needed. */
10579 if (flinfo->info->discard == discard_all)
10580 continue;
10581
10582 /* If this symbol is defined in a section which we are
10583 discarding, we don't need to keep it. */
10584 if (isym->st_shndx != SHN_UNDEF
10585 && isym->st_shndx < SHN_LORESERVE
10586 && bfd_section_removed_from_list (output_bfd,
10587 isec->output_section))
10588 continue;
10589
10590 /* Get the name of the symbol. */
10591 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10592 isym->st_name);
10593 if (name == NULL)
10594 return FALSE;
10595
10596 /* See if we are discarding symbols with this name. */
10597 if ((flinfo->info->strip == strip_some
10598 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10599 == NULL))
10600 || (((flinfo->info->discard == discard_sec_merge
10601 && (isec->flags & SEC_MERGE)
10602 && !bfd_link_relocatable (flinfo->info))
10603 || flinfo->info->discard == discard_l)
10604 && bfd_is_local_label_name (input_bfd, name)))
10605 continue;
10606
10607 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10608 {
10609 if (input_bfd->lto_output)
10610 /* -flto puts a temp file name here. This means builds
10611 are not reproducible. Discard the symbol. */
10612 continue;
10613 have_file_sym = TRUE;
10614 flinfo->filesym_count += 1;
10615 }
10616 if (!have_file_sym)
10617 {
10618 /* In the absence of debug info, bfd_find_nearest_line uses
10619 FILE symbols to determine the source file for local
10620 function symbols. Provide a FILE symbol here if input
10621 files lack such, so that their symbols won't be
10622 associated with a previous input file. It's not the
10623 source file, but the best we can do. */
10624 have_file_sym = TRUE;
10625 flinfo->filesym_count += 1;
10626 memset (&osym, 0, sizeof (osym));
10627 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10628 osym.st_shndx = SHN_ABS;
10629 if (!elf_link_output_symstrtab (flinfo,
10630 (input_bfd->lto_output ? NULL
10631 : input_bfd->filename),
10632 &osym, bfd_abs_section_ptr,
10633 NULL))
10634 return FALSE;
10635 }
10636
10637 osym = *isym;
10638
10639 /* Adjust the section index for the output file. */
10640 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10641 isec->output_section);
10642 if (osym.st_shndx == SHN_BAD)
10643 return FALSE;
10644
10645 /* ELF symbols in relocatable files are section relative, but
10646 in executable files they are virtual addresses. Note that
10647 this code assumes that all ELF sections have an associated
10648 BFD section with a reasonable value for output_offset; below
10649 we assume that they also have a reasonable value for
10650 output_section. Any special sections must be set up to meet
10651 these requirements. */
10652 osym.st_value += isec->output_offset;
10653 if (!bfd_link_relocatable (flinfo->info))
10654 {
10655 osym.st_value += isec->output_section->vma;
10656 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10657 {
10658 /* STT_TLS symbols are relative to PT_TLS segment base. */
10659 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10660 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10661 else
10662 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10663 STT_NOTYPE);
10664 }
10665 }
10666
10667 indx = bfd_get_symcount (output_bfd);
10668 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10669 if (ret == 0)
10670 return FALSE;
10671 else if (ret == 1)
10672 *pindex = indx;
10673 }
10674
10675 if (bed->s->arch_size == 32)
10676 {
10677 r_type_mask = 0xff;
10678 r_sym_shift = 8;
10679 address_size = 4;
10680 }
10681 else
10682 {
10683 r_type_mask = 0xffffffff;
10684 r_sym_shift = 32;
10685 address_size = 8;
10686 }
10687
10688 /* Relocate the contents of each section. */
10689 sym_hashes = elf_sym_hashes (input_bfd);
10690 for (o = input_bfd->sections; o != NULL; o = o->next)
10691 {
10692 bfd_byte *contents;
10693
10694 if (! o->linker_mark)
10695 {
10696 /* This section was omitted from the link. */
10697 continue;
10698 }
10699
10700 if (!flinfo->info->resolve_section_groups
10701 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10702 {
10703 /* Deal with the group signature symbol. */
10704 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10705 unsigned long symndx = sec_data->this_hdr.sh_info;
10706 asection *osec = o->output_section;
10707
10708 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10709 if (symndx >= locsymcount
10710 || (elf_bad_symtab (input_bfd)
10711 && flinfo->sections[symndx] == NULL))
10712 {
10713 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10714 while (h->root.type == bfd_link_hash_indirect
10715 || h->root.type == bfd_link_hash_warning)
10716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10717 /* Arrange for symbol to be output. */
10718 h->indx = -2;
10719 elf_section_data (osec)->this_hdr.sh_info = -2;
10720 }
10721 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10722 {
10723 /* We'll use the output section target_index. */
10724 asection *sec = flinfo->sections[symndx]->output_section;
10725 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10726 }
10727 else
10728 {
10729 if (flinfo->indices[symndx] == -1)
10730 {
10731 /* Otherwise output the local symbol now. */
10732 Elf_Internal_Sym sym = isymbuf[symndx];
10733 asection *sec = flinfo->sections[symndx]->output_section;
10734 const char *name;
10735 long indx;
10736 int ret;
10737
10738 name = bfd_elf_string_from_elf_section (input_bfd,
10739 symtab_hdr->sh_link,
10740 sym.st_name);
10741 if (name == NULL)
10742 return FALSE;
10743
10744 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10745 sec);
10746 if (sym.st_shndx == SHN_BAD)
10747 return FALSE;
10748
10749 sym.st_value += o->output_offset;
10750
10751 indx = bfd_get_symcount (output_bfd);
10752 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10753 NULL);
10754 if (ret == 0)
10755 return FALSE;
10756 else if (ret == 1)
10757 flinfo->indices[symndx] = indx;
10758 else
10759 abort ();
10760 }
10761 elf_section_data (osec)->this_hdr.sh_info
10762 = flinfo->indices[symndx];
10763 }
10764 }
10765
10766 if ((o->flags & SEC_HAS_CONTENTS) == 0
10767 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10768 continue;
10769
10770 if ((o->flags & SEC_LINKER_CREATED) != 0)
10771 {
10772 /* Section was created by _bfd_elf_link_create_dynamic_sections
10773 or somesuch. */
10774 continue;
10775 }
10776
10777 /* Get the contents of the section. They have been cached by a
10778 relaxation routine. Note that o is a section in an input
10779 file, so the contents field will not have been set by any of
10780 the routines which work on output files. */
10781 if (elf_section_data (o)->this_hdr.contents != NULL)
10782 {
10783 contents = elf_section_data (o)->this_hdr.contents;
10784 if (bed->caches_rawsize
10785 && o->rawsize != 0
10786 && o->rawsize < o->size)
10787 {
10788 memcpy (flinfo->contents, contents, o->rawsize);
10789 contents = flinfo->contents;
10790 }
10791 }
10792 else
10793 {
10794 contents = flinfo->contents;
10795 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10796 return FALSE;
10797 }
10798
10799 if ((o->flags & SEC_RELOC) != 0)
10800 {
10801 Elf_Internal_Rela *internal_relocs;
10802 Elf_Internal_Rela *rel, *relend;
10803 int action_discarded;
10804 int ret;
10805
10806 /* Get the swapped relocs. */
10807 internal_relocs
10808 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10809 flinfo->internal_relocs, FALSE);
10810 if (internal_relocs == NULL
10811 && o->reloc_count > 0)
10812 return FALSE;
10813
10814 /* We need to reverse-copy input .ctors/.dtors sections if
10815 they are placed in .init_array/.finit_array for output. */
10816 if (o->size > address_size
10817 && ((strncmp (o->name, ".ctors", 6) == 0
10818 && strcmp (o->output_section->name,
10819 ".init_array") == 0)
10820 || (strncmp (o->name, ".dtors", 6) == 0
10821 && strcmp (o->output_section->name,
10822 ".fini_array") == 0))
10823 && (o->name[6] == 0 || o->name[6] == '.'))
10824 {
10825 if (o->size * bed->s->int_rels_per_ext_rel
10826 != o->reloc_count * address_size)
10827 {
10828 _bfd_error_handler
10829 /* xgettext:c-format */
10830 (_("error: %pB: size of section %pA is not "
10831 "multiple of address size"),
10832 input_bfd, o);
10833 bfd_set_error (bfd_error_bad_value);
10834 return FALSE;
10835 }
10836 o->flags |= SEC_ELF_REVERSE_COPY;
10837 }
10838
10839 action_discarded = -1;
10840 if (!elf_section_ignore_discarded_relocs (o))
10841 action_discarded = (*bed->action_discarded) (o);
10842
10843 /* Run through the relocs evaluating complex reloc symbols and
10844 looking for relocs against symbols from discarded sections
10845 or section symbols from removed link-once sections.
10846 Complain about relocs against discarded sections. Zero
10847 relocs against removed link-once sections. */
10848
10849 rel = internal_relocs;
10850 relend = rel + o->reloc_count;
10851 for ( ; rel < relend; rel++)
10852 {
10853 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10854 unsigned int s_type;
10855 asection **ps, *sec;
10856 struct elf_link_hash_entry *h = NULL;
10857 const char *sym_name;
10858
10859 if (r_symndx == STN_UNDEF)
10860 continue;
10861
10862 if (r_symndx >= locsymcount
10863 || (elf_bad_symtab (input_bfd)
10864 && flinfo->sections[r_symndx] == NULL))
10865 {
10866 h = sym_hashes[r_symndx - extsymoff];
10867
10868 /* Badly formatted input files can contain relocs that
10869 reference non-existant symbols. Check here so that
10870 we do not seg fault. */
10871 if (h == NULL)
10872 {
10873 _bfd_error_handler
10874 /* xgettext:c-format */
10875 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10876 "that references a non-existent global symbol"),
10877 input_bfd, (uint64_t) rel->r_info, o);
10878 bfd_set_error (bfd_error_bad_value);
10879 return FALSE;
10880 }
10881
10882 while (h->root.type == bfd_link_hash_indirect
10883 || h->root.type == bfd_link_hash_warning)
10884 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10885
10886 s_type = h->type;
10887
10888 /* If a plugin symbol is referenced from a non-IR file,
10889 mark the symbol as undefined. Note that the
10890 linker may attach linker created dynamic sections
10891 to the plugin bfd. Symbols defined in linker
10892 created sections are not plugin symbols. */
10893 if ((h->root.non_ir_ref_regular
10894 || h->root.non_ir_ref_dynamic)
10895 && (h->root.type == bfd_link_hash_defined
10896 || h->root.type == bfd_link_hash_defweak)
10897 && (h->root.u.def.section->flags
10898 & SEC_LINKER_CREATED) == 0
10899 && h->root.u.def.section->owner != NULL
10900 && (h->root.u.def.section->owner->flags
10901 & BFD_PLUGIN) != 0)
10902 {
10903 h->root.type = bfd_link_hash_undefined;
10904 h->root.u.undef.abfd = h->root.u.def.section->owner;
10905 }
10906
10907 ps = NULL;
10908 if (h->root.type == bfd_link_hash_defined
10909 || h->root.type == bfd_link_hash_defweak)
10910 ps = &h->root.u.def.section;
10911
10912 sym_name = h->root.root.string;
10913 }
10914 else
10915 {
10916 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10917
10918 s_type = ELF_ST_TYPE (sym->st_info);
10919 ps = &flinfo->sections[r_symndx];
10920 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10921 sym, *ps);
10922 }
10923
10924 if ((s_type == STT_RELC || s_type == STT_SRELC)
10925 && !bfd_link_relocatable (flinfo->info))
10926 {
10927 bfd_vma val;
10928 bfd_vma dot = (rel->r_offset
10929 + o->output_offset + o->output_section->vma);
10930 #ifdef DEBUG
10931 printf ("Encountered a complex symbol!");
10932 printf (" (input_bfd %s, section %s, reloc %ld\n",
10933 input_bfd->filename, o->name,
10934 (long) (rel - internal_relocs));
10935 printf (" symbol: idx %8.8lx, name %s\n",
10936 r_symndx, sym_name);
10937 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10938 (unsigned long) rel->r_info,
10939 (unsigned long) rel->r_offset);
10940 #endif
10941 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10942 isymbuf, locsymcount, s_type == STT_SRELC))
10943 return FALSE;
10944
10945 /* Symbol evaluated OK. Update to absolute value. */
10946 set_symbol_value (input_bfd, isymbuf, locsymcount,
10947 r_symndx, val);
10948 continue;
10949 }
10950
10951 if (action_discarded != -1 && ps != NULL)
10952 {
10953 /* Complain if the definition comes from a
10954 discarded section. */
10955 if ((sec = *ps) != NULL && discarded_section (sec))
10956 {
10957 BFD_ASSERT (r_symndx != STN_UNDEF);
10958 if (action_discarded & COMPLAIN)
10959 (*flinfo->info->callbacks->einfo)
10960 /* xgettext:c-format */
10961 (_("%X`%s' referenced in section `%pA' of %pB: "
10962 "defined in discarded section `%pA' of %pB\n"),
10963 sym_name, o, input_bfd, sec, sec->owner);
10964
10965 /* Try to do the best we can to support buggy old
10966 versions of gcc. Pretend that the symbol is
10967 really defined in the kept linkonce section.
10968 FIXME: This is quite broken. Modifying the
10969 symbol here means we will be changing all later
10970 uses of the symbol, not just in this section. */
10971 if (action_discarded & PRETEND)
10972 {
10973 asection *kept;
10974
10975 kept = _bfd_elf_check_kept_section (sec,
10976 flinfo->info);
10977 if (kept != NULL)
10978 {
10979 *ps = kept;
10980 continue;
10981 }
10982 }
10983 }
10984 }
10985 }
10986
10987 /* Relocate the section by invoking a back end routine.
10988
10989 The back end routine is responsible for adjusting the
10990 section contents as necessary, and (if using Rela relocs
10991 and generating a relocatable output file) adjusting the
10992 reloc addend as necessary.
10993
10994 The back end routine does not have to worry about setting
10995 the reloc address or the reloc symbol index.
10996
10997 The back end routine is given a pointer to the swapped in
10998 internal symbols, and can access the hash table entries
10999 for the external symbols via elf_sym_hashes (input_bfd).
11000
11001 When generating relocatable output, the back end routine
11002 must handle STB_LOCAL/STT_SECTION symbols specially. The
11003 output symbol is going to be a section symbol
11004 corresponding to the output section, which will require
11005 the addend to be adjusted. */
11006
11007 ret = (*relocate_section) (output_bfd, flinfo->info,
11008 input_bfd, o, contents,
11009 internal_relocs,
11010 isymbuf,
11011 flinfo->sections);
11012 if (!ret)
11013 return FALSE;
11014
11015 if (ret == 2
11016 || bfd_link_relocatable (flinfo->info)
11017 || flinfo->info->emitrelocations)
11018 {
11019 Elf_Internal_Rela *irela;
11020 Elf_Internal_Rela *irelaend, *irelamid;
11021 bfd_vma last_offset;
11022 struct elf_link_hash_entry **rel_hash;
11023 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11024 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11025 unsigned int next_erel;
11026 bfd_boolean rela_normal;
11027 struct bfd_elf_section_data *esdi, *esdo;
11028
11029 esdi = elf_section_data (o);
11030 esdo = elf_section_data (o->output_section);
11031 rela_normal = FALSE;
11032
11033 /* Adjust the reloc addresses and symbol indices. */
11034
11035 irela = internal_relocs;
11036 irelaend = irela + o->reloc_count;
11037 rel_hash = esdo->rel.hashes + esdo->rel.count;
11038 /* We start processing the REL relocs, if any. When we reach
11039 IRELAMID in the loop, we switch to the RELA relocs. */
11040 irelamid = irela;
11041 if (esdi->rel.hdr != NULL)
11042 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11043 * bed->s->int_rels_per_ext_rel);
11044 rel_hash_list = rel_hash;
11045 rela_hash_list = NULL;
11046 last_offset = o->output_offset;
11047 if (!bfd_link_relocatable (flinfo->info))
11048 last_offset += o->output_section->vma;
11049 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11050 {
11051 unsigned long r_symndx;
11052 asection *sec;
11053 Elf_Internal_Sym sym;
11054
11055 if (next_erel == bed->s->int_rels_per_ext_rel)
11056 {
11057 rel_hash++;
11058 next_erel = 0;
11059 }
11060
11061 if (irela == irelamid)
11062 {
11063 rel_hash = esdo->rela.hashes + esdo->rela.count;
11064 rela_hash_list = rel_hash;
11065 rela_normal = bed->rela_normal;
11066 }
11067
11068 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11069 flinfo->info, o,
11070 irela->r_offset);
11071 if (irela->r_offset >= (bfd_vma) -2)
11072 {
11073 /* This is a reloc for a deleted entry or somesuch.
11074 Turn it into an R_*_NONE reloc, at the same
11075 offset as the last reloc. elf_eh_frame.c and
11076 bfd_elf_discard_info rely on reloc offsets
11077 being ordered. */
11078 irela->r_offset = last_offset;
11079 irela->r_info = 0;
11080 irela->r_addend = 0;
11081 continue;
11082 }
11083
11084 irela->r_offset += o->output_offset;
11085
11086 /* Relocs in an executable have to be virtual addresses. */
11087 if (!bfd_link_relocatable (flinfo->info))
11088 irela->r_offset += o->output_section->vma;
11089
11090 last_offset = irela->r_offset;
11091
11092 r_symndx = irela->r_info >> r_sym_shift;
11093 if (r_symndx == STN_UNDEF)
11094 continue;
11095
11096 if (r_symndx >= locsymcount
11097 || (elf_bad_symtab (input_bfd)
11098 && flinfo->sections[r_symndx] == NULL))
11099 {
11100 struct elf_link_hash_entry *rh;
11101 unsigned long indx;
11102
11103 /* This is a reloc against a global symbol. We
11104 have not yet output all the local symbols, so
11105 we do not know the symbol index of any global
11106 symbol. We set the rel_hash entry for this
11107 reloc to point to the global hash table entry
11108 for this symbol. The symbol index is then
11109 set at the end of bfd_elf_final_link. */
11110 indx = r_symndx - extsymoff;
11111 rh = elf_sym_hashes (input_bfd)[indx];
11112 while (rh->root.type == bfd_link_hash_indirect
11113 || rh->root.type == bfd_link_hash_warning)
11114 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11115
11116 /* Setting the index to -2 tells
11117 elf_link_output_extsym that this symbol is
11118 used by a reloc. */
11119 BFD_ASSERT (rh->indx < 0);
11120 rh->indx = -2;
11121 *rel_hash = rh;
11122
11123 continue;
11124 }
11125
11126 /* This is a reloc against a local symbol. */
11127
11128 *rel_hash = NULL;
11129 sym = isymbuf[r_symndx];
11130 sec = flinfo->sections[r_symndx];
11131 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11132 {
11133 /* I suppose the backend ought to fill in the
11134 section of any STT_SECTION symbol against a
11135 processor specific section. */
11136 r_symndx = STN_UNDEF;
11137 if (bfd_is_abs_section (sec))
11138 ;
11139 else if (sec == NULL || sec->owner == NULL)
11140 {
11141 bfd_set_error (bfd_error_bad_value);
11142 return FALSE;
11143 }
11144 else
11145 {
11146 asection *osec = sec->output_section;
11147
11148 /* If we have discarded a section, the output
11149 section will be the absolute section. In
11150 case of discarded SEC_MERGE sections, use
11151 the kept section. relocate_section should
11152 have already handled discarded linkonce
11153 sections. */
11154 if (bfd_is_abs_section (osec)
11155 && sec->kept_section != NULL
11156 && sec->kept_section->output_section != NULL)
11157 {
11158 osec = sec->kept_section->output_section;
11159 irela->r_addend -= osec->vma;
11160 }
11161
11162 if (!bfd_is_abs_section (osec))
11163 {
11164 r_symndx = osec->target_index;
11165 if (r_symndx == STN_UNDEF)
11166 {
11167 irela->r_addend += osec->vma;
11168 osec = _bfd_nearby_section (output_bfd, osec,
11169 osec->vma);
11170 irela->r_addend -= osec->vma;
11171 r_symndx = osec->target_index;
11172 }
11173 }
11174 }
11175
11176 /* Adjust the addend according to where the
11177 section winds up in the output section. */
11178 if (rela_normal)
11179 irela->r_addend += sec->output_offset;
11180 }
11181 else
11182 {
11183 if (flinfo->indices[r_symndx] == -1)
11184 {
11185 unsigned long shlink;
11186 const char *name;
11187 asection *osec;
11188 long indx;
11189
11190 if (flinfo->info->strip == strip_all)
11191 {
11192 /* You can't do ld -r -s. */
11193 bfd_set_error (bfd_error_invalid_operation);
11194 return FALSE;
11195 }
11196
11197 /* This symbol was skipped earlier, but
11198 since it is needed by a reloc, we
11199 must output it now. */
11200 shlink = symtab_hdr->sh_link;
11201 name = (bfd_elf_string_from_elf_section
11202 (input_bfd, shlink, sym.st_name));
11203 if (name == NULL)
11204 return FALSE;
11205
11206 osec = sec->output_section;
11207 sym.st_shndx =
11208 _bfd_elf_section_from_bfd_section (output_bfd,
11209 osec);
11210 if (sym.st_shndx == SHN_BAD)
11211 return FALSE;
11212
11213 sym.st_value += sec->output_offset;
11214 if (!bfd_link_relocatable (flinfo->info))
11215 {
11216 sym.st_value += osec->vma;
11217 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11218 {
11219 struct elf_link_hash_table *htab
11220 = elf_hash_table (flinfo->info);
11221
11222 /* STT_TLS symbols are relative to PT_TLS
11223 segment base. */
11224 if (htab->tls_sec != NULL)
11225 sym.st_value -= htab->tls_sec->vma;
11226 else
11227 sym.st_info
11228 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11229 STT_NOTYPE);
11230 }
11231 }
11232
11233 indx = bfd_get_symcount (output_bfd);
11234 ret = elf_link_output_symstrtab (flinfo, name,
11235 &sym, sec,
11236 NULL);
11237 if (ret == 0)
11238 return FALSE;
11239 else if (ret == 1)
11240 flinfo->indices[r_symndx] = indx;
11241 else
11242 abort ();
11243 }
11244
11245 r_symndx = flinfo->indices[r_symndx];
11246 }
11247
11248 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11249 | (irela->r_info & r_type_mask));
11250 }
11251
11252 /* Swap out the relocs. */
11253 input_rel_hdr = esdi->rel.hdr;
11254 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11255 {
11256 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11257 input_rel_hdr,
11258 internal_relocs,
11259 rel_hash_list))
11260 return FALSE;
11261 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11262 * bed->s->int_rels_per_ext_rel);
11263 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11264 }
11265
11266 input_rela_hdr = esdi->rela.hdr;
11267 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11268 {
11269 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11270 input_rela_hdr,
11271 internal_relocs,
11272 rela_hash_list))
11273 return FALSE;
11274 }
11275 }
11276 }
11277
11278 /* Write out the modified section contents. */
11279 if (bed->elf_backend_write_section
11280 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11281 contents))
11282 {
11283 /* Section written out. */
11284 }
11285 else switch (o->sec_info_type)
11286 {
11287 case SEC_INFO_TYPE_STABS:
11288 if (! (_bfd_write_section_stabs
11289 (output_bfd,
11290 &elf_hash_table (flinfo->info)->stab_info,
11291 o, &elf_section_data (o)->sec_info, contents)))
11292 return FALSE;
11293 break;
11294 case SEC_INFO_TYPE_MERGE:
11295 if (! _bfd_write_merged_section (output_bfd, o,
11296 elf_section_data (o)->sec_info))
11297 return FALSE;
11298 break;
11299 case SEC_INFO_TYPE_EH_FRAME:
11300 {
11301 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11302 o, contents))
11303 return FALSE;
11304 }
11305 break;
11306 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11307 {
11308 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11309 flinfo->info,
11310 o, contents))
11311 return FALSE;
11312 }
11313 break;
11314 default:
11315 {
11316 if (! (o->flags & SEC_EXCLUDE))
11317 {
11318 file_ptr offset = (file_ptr) o->output_offset;
11319 bfd_size_type todo = o->size;
11320
11321 offset *= bfd_octets_per_byte (output_bfd, o);
11322
11323 if ((o->flags & SEC_ELF_REVERSE_COPY))
11324 {
11325 /* Reverse-copy input section to output. */
11326 do
11327 {
11328 todo -= address_size;
11329 if (! bfd_set_section_contents (output_bfd,
11330 o->output_section,
11331 contents + todo,
11332 offset,
11333 address_size))
11334 return FALSE;
11335 if (todo == 0)
11336 break;
11337 offset += address_size;
11338 }
11339 while (1);
11340 }
11341 else if (! bfd_set_section_contents (output_bfd,
11342 o->output_section,
11343 contents,
11344 offset, todo))
11345 return FALSE;
11346 }
11347 }
11348 break;
11349 }
11350 }
11351
11352 return TRUE;
11353 }
11354
11355 /* Generate a reloc when linking an ELF file. This is a reloc
11356 requested by the linker, and does not come from any input file. This
11357 is used to build constructor and destructor tables when linking
11358 with -Ur. */
11359
11360 static bfd_boolean
11361 elf_reloc_link_order (bfd *output_bfd,
11362 struct bfd_link_info *info,
11363 asection *output_section,
11364 struct bfd_link_order *link_order)
11365 {
11366 reloc_howto_type *howto;
11367 long indx;
11368 bfd_vma offset;
11369 bfd_vma addend;
11370 struct bfd_elf_section_reloc_data *reldata;
11371 struct elf_link_hash_entry **rel_hash_ptr;
11372 Elf_Internal_Shdr *rel_hdr;
11373 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11374 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11375 bfd_byte *erel;
11376 unsigned int i;
11377 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11378
11379 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11380 if (howto == NULL)
11381 {
11382 bfd_set_error (bfd_error_bad_value);
11383 return FALSE;
11384 }
11385
11386 addend = link_order->u.reloc.p->addend;
11387
11388 if (esdo->rel.hdr)
11389 reldata = &esdo->rel;
11390 else if (esdo->rela.hdr)
11391 reldata = &esdo->rela;
11392 else
11393 {
11394 reldata = NULL;
11395 BFD_ASSERT (0);
11396 }
11397
11398 /* Figure out the symbol index. */
11399 rel_hash_ptr = reldata->hashes + reldata->count;
11400 if (link_order->type == bfd_section_reloc_link_order)
11401 {
11402 indx = link_order->u.reloc.p->u.section->target_index;
11403 BFD_ASSERT (indx != 0);
11404 *rel_hash_ptr = NULL;
11405 }
11406 else
11407 {
11408 struct elf_link_hash_entry *h;
11409
11410 /* Treat a reloc against a defined symbol as though it were
11411 actually against the section. */
11412 h = ((struct elf_link_hash_entry *)
11413 bfd_wrapped_link_hash_lookup (output_bfd, info,
11414 link_order->u.reloc.p->u.name,
11415 FALSE, FALSE, TRUE));
11416 if (h != NULL
11417 && (h->root.type == bfd_link_hash_defined
11418 || h->root.type == bfd_link_hash_defweak))
11419 {
11420 asection *section;
11421
11422 section = h->root.u.def.section;
11423 indx = section->output_section->target_index;
11424 *rel_hash_ptr = NULL;
11425 /* It seems that we ought to add the symbol value to the
11426 addend here, but in practice it has already been added
11427 because it was passed to constructor_callback. */
11428 addend += section->output_section->vma + section->output_offset;
11429 }
11430 else if (h != NULL)
11431 {
11432 /* Setting the index to -2 tells elf_link_output_extsym that
11433 this symbol is used by a reloc. */
11434 h->indx = -2;
11435 *rel_hash_ptr = h;
11436 indx = 0;
11437 }
11438 else
11439 {
11440 (*info->callbacks->unattached_reloc)
11441 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11442 indx = 0;
11443 }
11444 }
11445
11446 /* If this is an inplace reloc, we must write the addend into the
11447 object file. */
11448 if (howto->partial_inplace && addend != 0)
11449 {
11450 bfd_size_type size;
11451 bfd_reloc_status_type rstat;
11452 bfd_byte *buf;
11453 bfd_boolean ok;
11454 const char *sym_name;
11455 bfd_size_type octets;
11456
11457 size = (bfd_size_type) bfd_get_reloc_size (howto);
11458 buf = (bfd_byte *) bfd_zmalloc (size);
11459 if (buf == NULL && size != 0)
11460 return FALSE;
11461 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11462 switch (rstat)
11463 {
11464 case bfd_reloc_ok:
11465 break;
11466
11467 default:
11468 case bfd_reloc_outofrange:
11469 abort ();
11470
11471 case bfd_reloc_overflow:
11472 if (link_order->type == bfd_section_reloc_link_order)
11473 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11474 else
11475 sym_name = link_order->u.reloc.p->u.name;
11476 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11477 howto->name, addend, NULL, NULL,
11478 (bfd_vma) 0);
11479 break;
11480 }
11481
11482 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11483 output_section);
11484 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11485 octets, size);
11486 free (buf);
11487 if (! ok)
11488 return FALSE;
11489 }
11490
11491 /* The address of a reloc is relative to the section in a
11492 relocatable file, and is a virtual address in an executable
11493 file. */
11494 offset = link_order->offset;
11495 if (! bfd_link_relocatable (info))
11496 offset += output_section->vma;
11497
11498 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11499 {
11500 irel[i].r_offset = offset;
11501 irel[i].r_info = 0;
11502 irel[i].r_addend = 0;
11503 }
11504 if (bed->s->arch_size == 32)
11505 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11506 else
11507 #ifdef BFD64
11508 {
11509 bfd_uint64_t indx64 = indx;
11510 irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
11511 }
11512 #else
11513 BFD_FAIL();
11514 #endif
11515
11516 rel_hdr = reldata->hdr;
11517 erel = rel_hdr->contents;
11518 if (rel_hdr->sh_type == SHT_REL)
11519 {
11520 erel += reldata->count * bed->s->sizeof_rel;
11521 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11522 }
11523 else
11524 {
11525 irel[0].r_addend = addend;
11526 erel += reldata->count * bed->s->sizeof_rela;
11527 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11528 }
11529
11530 ++reldata->count;
11531
11532 return TRUE;
11533 }
11534
11535
11536 /* Compare two sections based on the locations of the sections they are
11537 linked to. Used by elf_fixup_link_order. */
11538
11539 static int
11540 compare_link_order (const void *a, const void *b)
11541 {
11542 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11543 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11544 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11545 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11546 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11547 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11548
11549 if (apos < bpos)
11550 return -1;
11551 if (apos > bpos)
11552 return 1;
11553
11554 /* The only way we should get matching LMAs is when the first of two
11555 sections has zero size. */
11556 if (asec->size < bsec->size)
11557 return -1;
11558 if (asec->size > bsec->size)
11559 return 1;
11560
11561 /* If they are both zero size then they almost certainly have the same
11562 VMA and thus are not ordered with respect to each other. Test VMA
11563 anyway, and fall back to id to make the result reproducible across
11564 qsort implementations. */
11565 apos = asec->output_section->vma + asec->output_offset;
11566 bpos = bsec->output_section->vma + bsec->output_offset;
11567 if (apos < bpos)
11568 return -1;
11569 if (apos > bpos)
11570 return 1;
11571
11572 return asec->id - bsec->id;
11573 }
11574
11575
11576 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11577 order as their linked sections. Returns false if this could not be done
11578 because an output section includes both ordered and unordered
11579 sections. Ideally we'd do this in the linker proper. */
11580
11581 static bfd_boolean
11582 elf_fixup_link_order (bfd *abfd, asection *o)
11583 {
11584 size_t seen_linkorder;
11585 size_t seen_other;
11586 size_t n;
11587 struct bfd_link_order *p;
11588 bfd *sub;
11589 struct bfd_link_order **sections;
11590 asection *s, *other_sec, *linkorder_sec;
11591 bfd_vma offset;
11592
11593 other_sec = NULL;
11594 linkorder_sec = NULL;
11595 seen_other = 0;
11596 seen_linkorder = 0;
11597 for (p = o->map_head.link_order; p != NULL; p = p->next)
11598 {
11599 if (p->type == bfd_indirect_link_order)
11600 {
11601 s = p->u.indirect.section;
11602 sub = s->owner;
11603 if ((s->flags & SEC_LINKER_CREATED) == 0
11604 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11605 && elf_section_data (s) != NULL
11606 && elf_linked_to_section (s) != NULL)
11607 {
11608 seen_linkorder++;
11609 linkorder_sec = s;
11610 }
11611 else
11612 {
11613 seen_other++;
11614 other_sec = s;
11615 }
11616 }
11617 else
11618 seen_other++;
11619
11620 if (seen_other && seen_linkorder)
11621 {
11622 if (other_sec && linkorder_sec)
11623 _bfd_error_handler
11624 /* xgettext:c-format */
11625 (_("%pA has both ordered [`%pA' in %pB] "
11626 "and unordered [`%pA' in %pB] sections"),
11627 o, linkorder_sec, linkorder_sec->owner,
11628 other_sec, other_sec->owner);
11629 else
11630 _bfd_error_handler
11631 (_("%pA has both ordered and unordered sections"), o);
11632 bfd_set_error (bfd_error_bad_value);
11633 return FALSE;
11634 }
11635 }
11636
11637 if (!seen_linkorder)
11638 return TRUE;
11639
11640 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11641 if (sections == NULL)
11642 return FALSE;
11643
11644 seen_linkorder = 0;
11645 for (p = o->map_head.link_order; p != NULL; p = p->next)
11646 sections[seen_linkorder++] = p;
11647
11648 /* Sort the input sections in the order of their linked section. */
11649 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11650
11651 /* Change the offsets of the sections. */
11652 offset = 0;
11653 for (n = 0; n < seen_linkorder; n++)
11654 {
11655 bfd_vma mask;
11656 s = sections[n]->u.indirect.section;
11657 mask = ~(bfd_vma) 0 << s->alignment_power;
11658 offset = (offset + ~mask) & mask;
11659 s->output_offset = offset / bfd_octets_per_byte (abfd, s);
11660 sections[n]->offset = offset;
11661 offset += sections[n]->size;
11662 }
11663
11664 free (sections);
11665 return TRUE;
11666 }
11667
11668 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11669 Returns TRUE upon success, FALSE otherwise. */
11670
11671 static bfd_boolean
11672 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11673 {
11674 bfd_boolean ret = FALSE;
11675 bfd *implib_bfd;
11676 const struct elf_backend_data *bed;
11677 flagword flags;
11678 enum bfd_architecture arch;
11679 unsigned int mach;
11680 asymbol **sympp = NULL;
11681 long symsize;
11682 long symcount;
11683 long src_count;
11684 elf_symbol_type *osymbuf;
11685
11686 implib_bfd = info->out_implib_bfd;
11687 bed = get_elf_backend_data (abfd);
11688
11689 if (!bfd_set_format (implib_bfd, bfd_object))
11690 return FALSE;
11691
11692 /* Use flag from executable but make it a relocatable object. */
11693 flags = bfd_get_file_flags (abfd);
11694 flags &= ~HAS_RELOC;
11695 if (!bfd_set_start_address (implib_bfd, 0)
11696 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11697 return FALSE;
11698
11699 /* Copy architecture of output file to import library file. */
11700 arch = bfd_get_arch (abfd);
11701 mach = bfd_get_mach (abfd);
11702 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11703 && (abfd->target_defaulted
11704 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11705 return FALSE;
11706
11707 /* Get symbol table size. */
11708 symsize = bfd_get_symtab_upper_bound (abfd);
11709 if (symsize < 0)
11710 return FALSE;
11711
11712 /* Read in the symbol table. */
11713 sympp = (asymbol **) bfd_malloc (symsize);
11714 if (sympp == NULL)
11715 return FALSE;
11716
11717 symcount = bfd_canonicalize_symtab (abfd, sympp);
11718 if (symcount < 0)
11719 goto free_sym_buf;
11720
11721 /* Allow the BFD backend to copy any private header data it
11722 understands from the output BFD to the import library BFD. */
11723 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11724 goto free_sym_buf;
11725
11726 /* Filter symbols to appear in the import library. */
11727 if (bed->elf_backend_filter_implib_symbols)
11728 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11729 symcount);
11730 else
11731 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11732 if (symcount == 0)
11733 {
11734 bfd_set_error (bfd_error_no_symbols);
11735 _bfd_error_handler (_("%pB: no symbol found for import library"),
11736 implib_bfd);
11737 goto free_sym_buf;
11738 }
11739
11740
11741 /* Make symbols absolute. */
11742 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11743 sizeof (*osymbuf));
11744 if (osymbuf == NULL)
11745 goto free_sym_buf;
11746
11747 for (src_count = 0; src_count < symcount; src_count++)
11748 {
11749 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11750 sizeof (*osymbuf));
11751 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11752 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11753 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11754 osymbuf[src_count].internal_elf_sym.st_value =
11755 osymbuf[src_count].symbol.value;
11756 sympp[src_count] = &osymbuf[src_count].symbol;
11757 }
11758
11759 bfd_set_symtab (implib_bfd, sympp, symcount);
11760
11761 /* Allow the BFD backend to copy any private data it understands
11762 from the output BFD to the import library BFD. This is done last
11763 to permit the routine to look at the filtered symbol table. */
11764 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11765 goto free_sym_buf;
11766
11767 if (!bfd_close (implib_bfd))
11768 goto free_sym_buf;
11769
11770 ret = TRUE;
11771
11772 free_sym_buf:
11773 free (sympp);
11774 return ret;
11775 }
11776
11777 static void
11778 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11779 {
11780 asection *o;
11781
11782 if (flinfo->symstrtab != NULL)
11783 _bfd_elf_strtab_free (flinfo->symstrtab);
11784 if (flinfo->contents != NULL)
11785 free (flinfo->contents);
11786 if (flinfo->external_relocs != NULL)
11787 free (flinfo->external_relocs);
11788 if (flinfo->internal_relocs != NULL)
11789 free (flinfo->internal_relocs);
11790 if (flinfo->external_syms != NULL)
11791 free (flinfo->external_syms);
11792 if (flinfo->locsym_shndx != NULL)
11793 free (flinfo->locsym_shndx);
11794 if (flinfo->internal_syms != NULL)
11795 free (flinfo->internal_syms);
11796 if (flinfo->indices != NULL)
11797 free (flinfo->indices);
11798 if (flinfo->sections != NULL)
11799 free (flinfo->sections);
11800 if (flinfo->symshndxbuf != NULL
11801 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11802 free (flinfo->symshndxbuf);
11803 for (o = obfd->sections; o != NULL; o = o->next)
11804 {
11805 struct bfd_elf_section_data *esdo = elf_section_data (o);
11806 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11807 free (esdo->rel.hashes);
11808 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11809 free (esdo->rela.hashes);
11810 }
11811 }
11812
11813 /* Do the final step of an ELF link. */
11814
11815 bfd_boolean
11816 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11817 {
11818 bfd_boolean dynamic;
11819 bfd_boolean emit_relocs;
11820 bfd *dynobj;
11821 struct elf_final_link_info flinfo;
11822 asection *o;
11823 struct bfd_link_order *p;
11824 bfd *sub;
11825 bfd_size_type max_contents_size;
11826 bfd_size_type max_external_reloc_size;
11827 bfd_size_type max_internal_reloc_count;
11828 bfd_size_type max_sym_count;
11829 bfd_size_type max_sym_shndx_count;
11830 Elf_Internal_Sym elfsym;
11831 unsigned int i;
11832 Elf_Internal_Shdr *symtab_hdr;
11833 Elf_Internal_Shdr *symtab_shndx_hdr;
11834 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11835 struct elf_outext_info eoinfo;
11836 bfd_boolean merged;
11837 size_t relativecount = 0;
11838 asection *reldyn = 0;
11839 bfd_size_type amt;
11840 asection *attr_section = NULL;
11841 bfd_vma attr_size = 0;
11842 const char *std_attrs_section;
11843 struct elf_link_hash_table *htab = elf_hash_table (info);
11844 bfd_boolean sections_removed;
11845
11846 if (!is_elf_hash_table (htab))
11847 return FALSE;
11848
11849 if (bfd_link_pic (info))
11850 abfd->flags |= DYNAMIC;
11851
11852 dynamic = htab->dynamic_sections_created;
11853 dynobj = htab->dynobj;
11854
11855 emit_relocs = (bfd_link_relocatable (info)
11856 || info->emitrelocations);
11857
11858 flinfo.info = info;
11859 flinfo.output_bfd = abfd;
11860 flinfo.symstrtab = _bfd_elf_strtab_init ();
11861 if (flinfo.symstrtab == NULL)
11862 return FALSE;
11863
11864 if (! dynamic)
11865 {
11866 flinfo.hash_sec = NULL;
11867 flinfo.symver_sec = NULL;
11868 }
11869 else
11870 {
11871 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11872 /* Note that dynsym_sec can be NULL (on VMS). */
11873 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11874 /* Note that it is OK if symver_sec is NULL. */
11875 }
11876
11877 flinfo.contents = NULL;
11878 flinfo.external_relocs = NULL;
11879 flinfo.internal_relocs = NULL;
11880 flinfo.external_syms = NULL;
11881 flinfo.locsym_shndx = NULL;
11882 flinfo.internal_syms = NULL;
11883 flinfo.indices = NULL;
11884 flinfo.sections = NULL;
11885 flinfo.symshndxbuf = NULL;
11886 flinfo.filesym_count = 0;
11887
11888 /* The object attributes have been merged. Remove the input
11889 sections from the link, and set the contents of the output
11890 section. */
11891 sections_removed = FALSE;
11892 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11893 for (o = abfd->sections; o != NULL; o = o->next)
11894 {
11895 bfd_boolean remove_section = FALSE;
11896
11897 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11898 || strcmp (o->name, ".gnu.attributes") == 0)
11899 {
11900 for (p = o->map_head.link_order; p != NULL; p = p->next)
11901 {
11902 asection *input_section;
11903
11904 if (p->type != bfd_indirect_link_order)
11905 continue;
11906 input_section = p->u.indirect.section;
11907 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11908 elf_link_input_bfd ignores this section. */
11909 input_section->flags &= ~SEC_HAS_CONTENTS;
11910 }
11911
11912 attr_size = bfd_elf_obj_attr_size (abfd);
11913 bfd_set_section_size (o, attr_size);
11914 /* Skip this section later on. */
11915 o->map_head.link_order = NULL;
11916 if (attr_size)
11917 attr_section = o;
11918 else
11919 remove_section = TRUE;
11920 }
11921 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11922 {
11923 /* Remove empty group section from linker output. */
11924 remove_section = TRUE;
11925 }
11926 if (remove_section)
11927 {
11928 o->flags |= SEC_EXCLUDE;
11929 bfd_section_list_remove (abfd, o);
11930 abfd->section_count--;
11931 sections_removed = TRUE;
11932 }
11933 }
11934 if (sections_removed)
11935 _bfd_fix_excluded_sec_syms (abfd, info);
11936
11937 /* Count up the number of relocations we will output for each output
11938 section, so that we know the sizes of the reloc sections. We
11939 also figure out some maximum sizes. */
11940 max_contents_size = 0;
11941 max_external_reloc_size = 0;
11942 max_internal_reloc_count = 0;
11943 max_sym_count = 0;
11944 max_sym_shndx_count = 0;
11945 merged = FALSE;
11946 for (o = abfd->sections; o != NULL; o = o->next)
11947 {
11948 struct bfd_elf_section_data *esdo = elf_section_data (o);
11949 o->reloc_count = 0;
11950
11951 for (p = o->map_head.link_order; p != NULL; p = p->next)
11952 {
11953 unsigned int reloc_count = 0;
11954 unsigned int additional_reloc_count = 0;
11955 struct bfd_elf_section_data *esdi = NULL;
11956
11957 if (p->type == bfd_section_reloc_link_order
11958 || p->type == bfd_symbol_reloc_link_order)
11959 reloc_count = 1;
11960 else if (p->type == bfd_indirect_link_order)
11961 {
11962 asection *sec;
11963
11964 sec = p->u.indirect.section;
11965
11966 /* Mark all sections which are to be included in the
11967 link. This will normally be every section. We need
11968 to do this so that we can identify any sections which
11969 the linker has decided to not include. */
11970 sec->linker_mark = TRUE;
11971
11972 if (sec->flags & SEC_MERGE)
11973 merged = TRUE;
11974
11975 if (sec->rawsize > max_contents_size)
11976 max_contents_size = sec->rawsize;
11977 if (sec->size > max_contents_size)
11978 max_contents_size = sec->size;
11979
11980 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11981 && (sec->owner->flags & DYNAMIC) == 0)
11982 {
11983 size_t sym_count;
11984
11985 /* We are interested in just local symbols, not all
11986 symbols. */
11987 if (elf_bad_symtab (sec->owner))
11988 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11989 / bed->s->sizeof_sym);
11990 else
11991 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11992
11993 if (sym_count > max_sym_count)
11994 max_sym_count = sym_count;
11995
11996 if (sym_count > max_sym_shndx_count
11997 && elf_symtab_shndx_list (sec->owner) != NULL)
11998 max_sym_shndx_count = sym_count;
11999
12000 if (esdo->this_hdr.sh_type == SHT_REL
12001 || esdo->this_hdr.sh_type == SHT_RELA)
12002 /* Some backends use reloc_count in relocation sections
12003 to count particular types of relocs. Of course,
12004 reloc sections themselves can't have relocations. */
12005 ;
12006 else if (emit_relocs)
12007 {
12008 reloc_count = sec->reloc_count;
12009 if (bed->elf_backend_count_additional_relocs)
12010 {
12011 int c;
12012 c = (*bed->elf_backend_count_additional_relocs) (sec);
12013 additional_reloc_count += c;
12014 }
12015 }
12016 else if (bed->elf_backend_count_relocs)
12017 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12018
12019 esdi = elf_section_data (sec);
12020
12021 if ((sec->flags & SEC_RELOC) != 0)
12022 {
12023 size_t ext_size = 0;
12024
12025 if (esdi->rel.hdr != NULL)
12026 ext_size = esdi->rel.hdr->sh_size;
12027 if (esdi->rela.hdr != NULL)
12028 ext_size += esdi->rela.hdr->sh_size;
12029
12030 if (ext_size > max_external_reloc_size)
12031 max_external_reloc_size = ext_size;
12032 if (sec->reloc_count > max_internal_reloc_count)
12033 max_internal_reloc_count = sec->reloc_count;
12034 }
12035 }
12036 }
12037
12038 if (reloc_count == 0)
12039 continue;
12040
12041 reloc_count += additional_reloc_count;
12042 o->reloc_count += reloc_count;
12043
12044 if (p->type == bfd_indirect_link_order && emit_relocs)
12045 {
12046 if (esdi->rel.hdr)
12047 {
12048 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12049 esdo->rel.count += additional_reloc_count;
12050 }
12051 if (esdi->rela.hdr)
12052 {
12053 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12054 esdo->rela.count += additional_reloc_count;
12055 }
12056 }
12057 else
12058 {
12059 if (o->use_rela_p)
12060 esdo->rela.count += reloc_count;
12061 else
12062 esdo->rel.count += reloc_count;
12063 }
12064 }
12065
12066 if (o->reloc_count > 0)
12067 o->flags |= SEC_RELOC;
12068 else
12069 {
12070 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12071 set it (this is probably a bug) and if it is set
12072 assign_section_numbers will create a reloc section. */
12073 o->flags &=~ SEC_RELOC;
12074 }
12075
12076 /* If the SEC_ALLOC flag is not set, force the section VMA to
12077 zero. This is done in elf_fake_sections as well, but forcing
12078 the VMA to 0 here will ensure that relocs against these
12079 sections are handled correctly. */
12080 if ((o->flags & SEC_ALLOC) == 0
12081 && ! o->user_set_vma)
12082 o->vma = 0;
12083 }
12084
12085 if (! bfd_link_relocatable (info) && merged)
12086 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12087
12088 /* Figure out the file positions for everything but the symbol table
12089 and the relocs. We set symcount to force assign_section_numbers
12090 to create a symbol table. */
12091 abfd->symcount = info->strip != strip_all || emit_relocs;
12092 BFD_ASSERT (! abfd->output_has_begun);
12093 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12094 goto error_return;
12095
12096 /* Set sizes, and assign file positions for reloc sections. */
12097 for (o = abfd->sections; o != NULL; o = o->next)
12098 {
12099 struct bfd_elf_section_data *esdo = elf_section_data (o);
12100 if ((o->flags & SEC_RELOC) != 0)
12101 {
12102 if (esdo->rel.hdr
12103 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12104 goto error_return;
12105
12106 if (esdo->rela.hdr
12107 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12108 goto error_return;
12109 }
12110
12111 /* _bfd_elf_compute_section_file_positions makes temporary use
12112 of target_index. Reset it. */
12113 o->target_index = 0;
12114
12115 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12116 to count upwards while actually outputting the relocations. */
12117 esdo->rel.count = 0;
12118 esdo->rela.count = 0;
12119
12120 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12121 && !bfd_section_is_ctf (o))
12122 {
12123 /* Cache the section contents so that they can be compressed
12124 later. Use bfd_malloc since it will be freed by
12125 bfd_compress_section_contents. */
12126 unsigned char *contents = esdo->this_hdr.contents;
12127 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12128 abort ();
12129 contents
12130 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12131 if (contents == NULL)
12132 goto error_return;
12133 esdo->this_hdr.contents = contents;
12134 }
12135 }
12136
12137 /* We have now assigned file positions for all the sections except .symtab,
12138 .strtab, and non-loaded reloc and compressed debugging sections. We start
12139 the .symtab section at the current file position, and write directly to it.
12140 We build the .strtab section in memory. */
12141 abfd->symcount = 0;
12142 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12143 /* sh_name is set in prep_headers. */
12144 symtab_hdr->sh_type = SHT_SYMTAB;
12145 /* sh_flags, sh_addr and sh_size all start off zero. */
12146 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12147 /* sh_link is set in assign_section_numbers. */
12148 /* sh_info is set below. */
12149 /* sh_offset is set just below. */
12150 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12151
12152 if (max_sym_count < 20)
12153 max_sym_count = 20;
12154 htab->strtabsize = max_sym_count;
12155 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12156 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12157 if (htab->strtab == NULL)
12158 goto error_return;
12159 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12160 flinfo.symshndxbuf
12161 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12162 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12163
12164 if (info->strip != strip_all || emit_relocs)
12165 {
12166 file_ptr off = elf_next_file_pos (abfd);
12167
12168 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12169
12170 /* Note that at this point elf_next_file_pos (abfd) is
12171 incorrect. We do not yet know the size of the .symtab section.
12172 We correct next_file_pos below, after we do know the size. */
12173
12174 /* Start writing out the symbol table. The first symbol is always a
12175 dummy symbol. */
12176 elfsym.st_value = 0;
12177 elfsym.st_size = 0;
12178 elfsym.st_info = 0;
12179 elfsym.st_other = 0;
12180 elfsym.st_shndx = SHN_UNDEF;
12181 elfsym.st_target_internal = 0;
12182 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12183 bfd_und_section_ptr, NULL) != 1)
12184 goto error_return;
12185
12186 /* Output a symbol for each section. We output these even if we are
12187 discarding local symbols, since they are used for relocs. These
12188 symbols have no names. We store the index of each one in the
12189 index field of the section, so that we can find it again when
12190 outputting relocs. */
12191
12192 elfsym.st_size = 0;
12193 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12194 elfsym.st_other = 0;
12195 elfsym.st_value = 0;
12196 elfsym.st_target_internal = 0;
12197 for (i = 1; i < elf_numsections (abfd); i++)
12198 {
12199 o = bfd_section_from_elf_index (abfd, i);
12200 if (o != NULL)
12201 {
12202 o->target_index = bfd_get_symcount (abfd);
12203 elfsym.st_shndx = i;
12204 if (!bfd_link_relocatable (info))
12205 elfsym.st_value = o->vma;
12206 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12207 NULL) != 1)
12208 goto error_return;
12209 }
12210 }
12211 }
12212
12213 /* Allocate some memory to hold information read in from the input
12214 files. */
12215 if (max_contents_size != 0)
12216 {
12217 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12218 if (flinfo.contents == NULL)
12219 goto error_return;
12220 }
12221
12222 if (max_external_reloc_size != 0)
12223 {
12224 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12225 if (flinfo.external_relocs == NULL)
12226 goto error_return;
12227 }
12228
12229 if (max_internal_reloc_count != 0)
12230 {
12231 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12232 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12233 if (flinfo.internal_relocs == NULL)
12234 goto error_return;
12235 }
12236
12237 if (max_sym_count != 0)
12238 {
12239 amt = max_sym_count * bed->s->sizeof_sym;
12240 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12241 if (flinfo.external_syms == NULL)
12242 goto error_return;
12243
12244 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12245 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12246 if (flinfo.internal_syms == NULL)
12247 goto error_return;
12248
12249 amt = max_sym_count * sizeof (long);
12250 flinfo.indices = (long int *) bfd_malloc (amt);
12251 if (flinfo.indices == NULL)
12252 goto error_return;
12253
12254 amt = max_sym_count * sizeof (asection *);
12255 flinfo.sections = (asection **) bfd_malloc (amt);
12256 if (flinfo.sections == NULL)
12257 goto error_return;
12258 }
12259
12260 if (max_sym_shndx_count != 0)
12261 {
12262 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12263 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12264 if (flinfo.locsym_shndx == NULL)
12265 goto error_return;
12266 }
12267
12268 if (htab->tls_sec)
12269 {
12270 bfd_vma base, end = 0;
12271 asection *sec;
12272
12273 for (sec = htab->tls_sec;
12274 sec && (sec->flags & SEC_THREAD_LOCAL);
12275 sec = sec->next)
12276 {
12277 bfd_size_type size = sec->size;
12278
12279 if (size == 0
12280 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12281 {
12282 struct bfd_link_order *ord = sec->map_tail.link_order;
12283
12284 if (ord != NULL)
12285 size = ord->offset + ord->size;
12286 }
12287 end = sec->vma + size;
12288 }
12289 base = htab->tls_sec->vma;
12290 /* Only align end of TLS section if static TLS doesn't have special
12291 alignment requirements. */
12292 if (bed->static_tls_alignment == 1)
12293 end = align_power (end, htab->tls_sec->alignment_power);
12294 htab->tls_size = end - base;
12295 }
12296
12297 /* Reorder SHF_LINK_ORDER sections. */
12298 for (o = abfd->sections; o != NULL; o = o->next)
12299 {
12300 if (!elf_fixup_link_order (abfd, o))
12301 return FALSE;
12302 }
12303
12304 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12305 return FALSE;
12306
12307 /* Since ELF permits relocations to be against local symbols, we
12308 must have the local symbols available when we do the relocations.
12309 Since we would rather only read the local symbols once, and we
12310 would rather not keep them in memory, we handle all the
12311 relocations for a single input file at the same time.
12312
12313 Unfortunately, there is no way to know the total number of local
12314 symbols until we have seen all of them, and the local symbol
12315 indices precede the global symbol indices. This means that when
12316 we are generating relocatable output, and we see a reloc against
12317 a global symbol, we can not know the symbol index until we have
12318 finished examining all the local symbols to see which ones we are
12319 going to output. To deal with this, we keep the relocations in
12320 memory, and don't output them until the end of the link. This is
12321 an unfortunate waste of memory, but I don't see a good way around
12322 it. Fortunately, it only happens when performing a relocatable
12323 link, which is not the common case. FIXME: If keep_memory is set
12324 we could write the relocs out and then read them again; I don't
12325 know how bad the memory loss will be. */
12326
12327 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12328 sub->output_has_begun = FALSE;
12329 for (o = abfd->sections; o != NULL; o = o->next)
12330 {
12331 for (p = o->map_head.link_order; p != NULL; p = p->next)
12332 {
12333 if (p->type == bfd_indirect_link_order
12334 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12335 == bfd_target_elf_flavour)
12336 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12337 {
12338 if (! sub->output_has_begun)
12339 {
12340 if (! elf_link_input_bfd (&flinfo, sub))
12341 goto error_return;
12342 sub->output_has_begun = TRUE;
12343 }
12344 }
12345 else if (p->type == bfd_section_reloc_link_order
12346 || p->type == bfd_symbol_reloc_link_order)
12347 {
12348 if (! elf_reloc_link_order (abfd, info, o, p))
12349 goto error_return;
12350 }
12351 else
12352 {
12353 if (! _bfd_default_link_order (abfd, info, o, p))
12354 {
12355 if (p->type == bfd_indirect_link_order
12356 && (bfd_get_flavour (sub)
12357 == bfd_target_elf_flavour)
12358 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12359 != bed->s->elfclass))
12360 {
12361 const char *iclass, *oclass;
12362
12363 switch (bed->s->elfclass)
12364 {
12365 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12366 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12367 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12368 default: abort ();
12369 }
12370
12371 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12372 {
12373 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12374 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12375 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12376 default: abort ();
12377 }
12378
12379 bfd_set_error (bfd_error_wrong_format);
12380 _bfd_error_handler
12381 /* xgettext:c-format */
12382 (_("%pB: file class %s incompatible with %s"),
12383 sub, iclass, oclass);
12384 }
12385
12386 goto error_return;
12387 }
12388 }
12389 }
12390 }
12391
12392 /* Free symbol buffer if needed. */
12393 if (!info->reduce_memory_overheads)
12394 {
12395 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12396 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12397 && elf_tdata (sub)->symbuf)
12398 {
12399 free (elf_tdata (sub)->symbuf);
12400 elf_tdata (sub)->symbuf = NULL;
12401 }
12402 }
12403
12404 /* Output any global symbols that got converted to local in a
12405 version script or due to symbol visibility. We do this in a
12406 separate step since ELF requires all local symbols to appear
12407 prior to any global symbols. FIXME: We should only do this if
12408 some global symbols were, in fact, converted to become local.
12409 FIXME: Will this work correctly with the Irix 5 linker? */
12410 eoinfo.failed = FALSE;
12411 eoinfo.flinfo = &flinfo;
12412 eoinfo.localsyms = TRUE;
12413 eoinfo.file_sym_done = FALSE;
12414 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12415 if (eoinfo.failed)
12416 return FALSE;
12417
12418 /* If backend needs to output some local symbols not present in the hash
12419 table, do it now. */
12420 if (bed->elf_backend_output_arch_local_syms
12421 && (info->strip != strip_all || emit_relocs))
12422 {
12423 typedef int (*out_sym_func)
12424 (void *, const char *, Elf_Internal_Sym *, asection *,
12425 struct elf_link_hash_entry *);
12426
12427 if (! ((*bed->elf_backend_output_arch_local_syms)
12428 (abfd, info, &flinfo,
12429 (out_sym_func) elf_link_output_symstrtab)))
12430 return FALSE;
12431 }
12432
12433 /* That wrote out all the local symbols. Finish up the symbol table
12434 with the global symbols. Even if we want to strip everything we
12435 can, we still need to deal with those global symbols that got
12436 converted to local in a version script. */
12437
12438 /* The sh_info field records the index of the first non local symbol. */
12439 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12440
12441 if (dynamic
12442 && htab->dynsym != NULL
12443 && htab->dynsym->output_section != bfd_abs_section_ptr)
12444 {
12445 Elf_Internal_Sym sym;
12446 bfd_byte *dynsym = htab->dynsym->contents;
12447
12448 o = htab->dynsym->output_section;
12449 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12450
12451 /* Write out the section symbols for the output sections. */
12452 if (bfd_link_pic (info)
12453 || htab->is_relocatable_executable)
12454 {
12455 asection *s;
12456
12457 sym.st_size = 0;
12458 sym.st_name = 0;
12459 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12460 sym.st_other = 0;
12461 sym.st_target_internal = 0;
12462
12463 for (s = abfd->sections; s != NULL; s = s->next)
12464 {
12465 int indx;
12466 bfd_byte *dest;
12467 long dynindx;
12468
12469 dynindx = elf_section_data (s)->dynindx;
12470 if (dynindx <= 0)
12471 continue;
12472 indx = elf_section_data (s)->this_idx;
12473 BFD_ASSERT (indx > 0);
12474 sym.st_shndx = indx;
12475 if (! check_dynsym (abfd, &sym))
12476 return FALSE;
12477 sym.st_value = s->vma;
12478 dest = dynsym + dynindx * bed->s->sizeof_sym;
12479 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12480 }
12481 }
12482
12483 /* Write out the local dynsyms. */
12484 if (htab->dynlocal)
12485 {
12486 struct elf_link_local_dynamic_entry *e;
12487 for (e = htab->dynlocal; e ; e = e->next)
12488 {
12489 asection *s;
12490 bfd_byte *dest;
12491
12492 /* Copy the internal symbol and turn off visibility.
12493 Note that we saved a word of storage and overwrote
12494 the original st_name with the dynstr_index. */
12495 sym = e->isym;
12496 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12497
12498 s = bfd_section_from_elf_index (e->input_bfd,
12499 e->isym.st_shndx);
12500 if (s != NULL)
12501 {
12502 sym.st_shndx =
12503 elf_section_data (s->output_section)->this_idx;
12504 if (! check_dynsym (abfd, &sym))
12505 return FALSE;
12506 sym.st_value = (s->output_section->vma
12507 + s->output_offset
12508 + e->isym.st_value);
12509 }
12510
12511 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12512 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12513 }
12514 }
12515 }
12516
12517 /* We get the global symbols from the hash table. */
12518 eoinfo.failed = FALSE;
12519 eoinfo.localsyms = FALSE;
12520 eoinfo.flinfo = &flinfo;
12521 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12522 if (eoinfo.failed)
12523 return FALSE;
12524
12525 /* If backend needs to output some symbols not present in the hash
12526 table, do it now. */
12527 if (bed->elf_backend_output_arch_syms
12528 && (info->strip != strip_all || emit_relocs))
12529 {
12530 typedef int (*out_sym_func)
12531 (void *, const char *, Elf_Internal_Sym *, asection *,
12532 struct elf_link_hash_entry *);
12533
12534 if (! ((*bed->elf_backend_output_arch_syms)
12535 (abfd, info, &flinfo,
12536 (out_sym_func) elf_link_output_symstrtab)))
12537 return FALSE;
12538 }
12539
12540 /* Finalize the .strtab section. */
12541 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12542
12543 /* Swap out the .strtab section. */
12544 if (!elf_link_swap_symbols_out (&flinfo))
12545 return FALSE;
12546
12547 /* Now we know the size of the symtab section. */
12548 if (bfd_get_symcount (abfd) > 0)
12549 {
12550 /* Finish up and write out the symbol string table (.strtab)
12551 section. */
12552 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12553 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12554
12555 if (elf_symtab_shndx_list (abfd))
12556 {
12557 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12558
12559 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12560 {
12561 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12562 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12563 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12564 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12565 symtab_shndx_hdr->sh_size = amt;
12566
12567 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12568 off, TRUE);
12569
12570 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12571 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12572 return FALSE;
12573 }
12574 }
12575
12576 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12577 /* sh_name was set in prep_headers. */
12578 symstrtab_hdr->sh_type = SHT_STRTAB;
12579 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12580 symstrtab_hdr->sh_addr = 0;
12581 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12582 symstrtab_hdr->sh_entsize = 0;
12583 symstrtab_hdr->sh_link = 0;
12584 symstrtab_hdr->sh_info = 0;
12585 /* sh_offset is set just below. */
12586 symstrtab_hdr->sh_addralign = 1;
12587
12588 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12589 off, TRUE);
12590 elf_next_file_pos (abfd) = off;
12591
12592 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12593 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12594 return FALSE;
12595 }
12596
12597 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12598 {
12599 _bfd_error_handler (_("%pB: failed to generate import library"),
12600 info->out_implib_bfd);
12601 return FALSE;
12602 }
12603
12604 /* Adjust the relocs to have the correct symbol indices. */
12605 for (o = abfd->sections; o != NULL; o = o->next)
12606 {
12607 struct bfd_elf_section_data *esdo = elf_section_data (o);
12608 bfd_boolean sort;
12609
12610 if ((o->flags & SEC_RELOC) == 0)
12611 continue;
12612
12613 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12614 if (esdo->rel.hdr != NULL
12615 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12616 return FALSE;
12617 if (esdo->rela.hdr != NULL
12618 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12619 return FALSE;
12620
12621 /* Set the reloc_count field to 0 to prevent write_relocs from
12622 trying to swap the relocs out itself. */
12623 o->reloc_count = 0;
12624 }
12625
12626 if (dynamic && info->combreloc && dynobj != NULL)
12627 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12628
12629 /* If we are linking against a dynamic object, or generating a
12630 shared library, finish up the dynamic linking information. */
12631 if (dynamic)
12632 {
12633 bfd_byte *dyncon, *dynconend;
12634
12635 /* Fix up .dynamic entries. */
12636 o = bfd_get_linker_section (dynobj, ".dynamic");
12637 BFD_ASSERT (o != NULL);
12638
12639 dyncon = o->contents;
12640 dynconend = o->contents + o->size;
12641 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12642 {
12643 Elf_Internal_Dyn dyn;
12644 const char *name;
12645 unsigned int type;
12646 bfd_size_type sh_size;
12647 bfd_vma sh_addr;
12648
12649 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12650
12651 switch (dyn.d_tag)
12652 {
12653 default:
12654 continue;
12655 case DT_NULL:
12656 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12657 {
12658 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12659 {
12660 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12661 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12662 default: continue;
12663 }
12664 dyn.d_un.d_val = relativecount;
12665 relativecount = 0;
12666 break;
12667 }
12668 continue;
12669
12670 case DT_INIT:
12671 name = info->init_function;
12672 goto get_sym;
12673 case DT_FINI:
12674 name = info->fini_function;
12675 get_sym:
12676 {
12677 struct elf_link_hash_entry *h;
12678
12679 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12680 if (h != NULL
12681 && (h->root.type == bfd_link_hash_defined
12682 || h->root.type == bfd_link_hash_defweak))
12683 {
12684 dyn.d_un.d_ptr = h->root.u.def.value;
12685 o = h->root.u.def.section;
12686 if (o->output_section != NULL)
12687 dyn.d_un.d_ptr += (o->output_section->vma
12688 + o->output_offset);
12689 else
12690 {
12691 /* The symbol is imported from another shared
12692 library and does not apply to this one. */
12693 dyn.d_un.d_ptr = 0;
12694 }
12695 break;
12696 }
12697 }
12698 continue;
12699
12700 case DT_PREINIT_ARRAYSZ:
12701 name = ".preinit_array";
12702 goto get_out_size;
12703 case DT_INIT_ARRAYSZ:
12704 name = ".init_array";
12705 goto get_out_size;
12706 case DT_FINI_ARRAYSZ:
12707 name = ".fini_array";
12708 get_out_size:
12709 o = bfd_get_section_by_name (abfd, name);
12710 if (o == NULL)
12711 {
12712 _bfd_error_handler
12713 (_("could not find section %s"), name);
12714 goto error_return;
12715 }
12716 if (o->size == 0)
12717 _bfd_error_handler
12718 (_("warning: %s section has zero size"), name);
12719 dyn.d_un.d_val = o->size;
12720 break;
12721
12722 case DT_PREINIT_ARRAY:
12723 name = ".preinit_array";
12724 goto get_out_vma;
12725 case DT_INIT_ARRAY:
12726 name = ".init_array";
12727 goto get_out_vma;
12728 case DT_FINI_ARRAY:
12729 name = ".fini_array";
12730 get_out_vma:
12731 o = bfd_get_section_by_name (abfd, name);
12732 goto do_vma;
12733
12734 case DT_HASH:
12735 name = ".hash";
12736 goto get_vma;
12737 case DT_GNU_HASH:
12738 name = ".gnu.hash";
12739 goto get_vma;
12740 case DT_STRTAB:
12741 name = ".dynstr";
12742 goto get_vma;
12743 case DT_SYMTAB:
12744 name = ".dynsym";
12745 goto get_vma;
12746 case DT_VERDEF:
12747 name = ".gnu.version_d";
12748 goto get_vma;
12749 case DT_VERNEED:
12750 name = ".gnu.version_r";
12751 goto get_vma;
12752 case DT_VERSYM:
12753 name = ".gnu.version";
12754 get_vma:
12755 o = bfd_get_linker_section (dynobj, name);
12756 do_vma:
12757 if (o == NULL || bfd_is_abs_section (o->output_section))
12758 {
12759 _bfd_error_handler
12760 (_("could not find section %s"), name);
12761 goto error_return;
12762 }
12763 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12764 {
12765 _bfd_error_handler
12766 (_("warning: section '%s' is being made into a note"), name);
12767 bfd_set_error (bfd_error_nonrepresentable_section);
12768 goto error_return;
12769 }
12770 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12771 break;
12772
12773 case DT_REL:
12774 case DT_RELA:
12775 case DT_RELSZ:
12776 case DT_RELASZ:
12777 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12778 type = SHT_REL;
12779 else
12780 type = SHT_RELA;
12781 sh_size = 0;
12782 sh_addr = 0;
12783 for (i = 1; i < elf_numsections (abfd); i++)
12784 {
12785 Elf_Internal_Shdr *hdr;
12786
12787 hdr = elf_elfsections (abfd)[i];
12788 if (hdr->sh_type == type
12789 && (hdr->sh_flags & SHF_ALLOC) != 0)
12790 {
12791 sh_size += hdr->sh_size;
12792 if (sh_addr == 0
12793 || sh_addr > hdr->sh_addr)
12794 sh_addr = hdr->sh_addr;
12795 }
12796 }
12797
12798 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12799 {
12800 /* Don't count procedure linkage table relocs in the
12801 overall reloc count. */
12802 sh_size -= htab->srelplt->size;
12803 if (sh_size == 0)
12804 /* If the size is zero, make the address zero too.
12805 This is to avoid a glibc bug. If the backend
12806 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12807 zero, then we'll put DT_RELA at the end of
12808 DT_JMPREL. glibc will interpret the end of
12809 DT_RELA matching the end of DT_JMPREL as the
12810 case where DT_RELA includes DT_JMPREL, and for
12811 LD_BIND_NOW will decide that processing DT_RELA
12812 will process the PLT relocs too. Net result:
12813 No PLT relocs applied. */
12814 sh_addr = 0;
12815
12816 /* If .rela.plt is the first .rela section, exclude
12817 it from DT_RELA. */
12818 else if (sh_addr == (htab->srelplt->output_section->vma
12819 + htab->srelplt->output_offset))
12820 sh_addr += htab->srelplt->size;
12821 }
12822
12823 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12824 dyn.d_un.d_val = sh_size;
12825 else
12826 dyn.d_un.d_ptr = sh_addr;
12827 break;
12828 }
12829 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12830 }
12831 }
12832
12833 /* If we have created any dynamic sections, then output them. */
12834 if (dynobj != NULL)
12835 {
12836 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12837 goto error_return;
12838
12839 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12840 if (((info->warn_shared_textrel && bfd_link_pic (info))
12841 || info->error_textrel)
12842 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12843 {
12844 bfd_byte *dyncon, *dynconend;
12845
12846 dyncon = o->contents;
12847 dynconend = o->contents + o->size;
12848 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12849 {
12850 Elf_Internal_Dyn dyn;
12851
12852 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12853
12854 if (dyn.d_tag == DT_TEXTREL)
12855 {
12856 if (info->error_textrel)
12857 info->callbacks->einfo
12858 (_("%P%X: read-only segment has dynamic relocations\n"));
12859 else
12860 info->callbacks->einfo
12861 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12862 break;
12863 }
12864 }
12865 }
12866
12867 for (o = dynobj->sections; o != NULL; o = o->next)
12868 {
12869 if ((o->flags & SEC_HAS_CONTENTS) == 0
12870 || o->size == 0
12871 || o->output_section == bfd_abs_section_ptr)
12872 continue;
12873 if ((o->flags & SEC_LINKER_CREATED) == 0)
12874 {
12875 /* At this point, we are only interested in sections
12876 created by _bfd_elf_link_create_dynamic_sections. */
12877 continue;
12878 }
12879 if (htab->stab_info.stabstr == o)
12880 continue;
12881 if (htab->eh_info.hdr_sec == o)
12882 continue;
12883 if (strcmp (o->name, ".dynstr") != 0)
12884 {
12885 bfd_size_type octets = ((file_ptr) o->output_offset
12886 * bfd_octets_per_byte (abfd, o));
12887 if (!bfd_set_section_contents (abfd, o->output_section,
12888 o->contents, octets, o->size))
12889 goto error_return;
12890 }
12891 else
12892 {
12893 /* The contents of the .dynstr section are actually in a
12894 stringtab. */
12895 file_ptr off;
12896
12897 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12898 if (bfd_seek (abfd, off, SEEK_SET) != 0
12899 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12900 goto error_return;
12901 }
12902 }
12903 }
12904
12905 if (!info->resolve_section_groups)
12906 {
12907 bfd_boolean failed = FALSE;
12908
12909 BFD_ASSERT (bfd_link_relocatable (info));
12910 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12911 if (failed)
12912 goto error_return;
12913 }
12914
12915 /* If we have optimized stabs strings, output them. */
12916 if (htab->stab_info.stabstr != NULL)
12917 {
12918 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12919 goto error_return;
12920 }
12921
12922 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12923 goto error_return;
12924
12925 if (info->callbacks->emit_ctf)
12926 info->callbacks->emit_ctf ();
12927
12928 elf_final_link_free (abfd, &flinfo);
12929
12930 if (attr_section)
12931 {
12932 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12933 if (contents == NULL)
12934 return FALSE; /* Bail out and fail. */
12935 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12936 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12937 free (contents);
12938 }
12939
12940 return TRUE;
12941
12942 error_return:
12943 elf_final_link_free (abfd, &flinfo);
12944 return FALSE;
12945 }
12946
12947 /* Initialize COOKIE for input bfd ABFD. */
12949
12950 static bfd_boolean
12951 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12952 struct bfd_link_info *info, bfd *abfd)
12953 {
12954 Elf_Internal_Shdr *symtab_hdr;
12955 const struct elf_backend_data *bed;
12956
12957 bed = get_elf_backend_data (abfd);
12958 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12959
12960 cookie->abfd = abfd;
12961 cookie->sym_hashes = elf_sym_hashes (abfd);
12962 cookie->bad_symtab = elf_bad_symtab (abfd);
12963 if (cookie->bad_symtab)
12964 {
12965 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12966 cookie->extsymoff = 0;
12967 }
12968 else
12969 {
12970 cookie->locsymcount = symtab_hdr->sh_info;
12971 cookie->extsymoff = symtab_hdr->sh_info;
12972 }
12973
12974 if (bed->s->arch_size == 32)
12975 cookie->r_sym_shift = 8;
12976 else
12977 cookie->r_sym_shift = 32;
12978
12979 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12980 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12981 {
12982 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12983 cookie->locsymcount, 0,
12984 NULL, NULL, NULL);
12985 if (cookie->locsyms == NULL)
12986 {
12987 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12988 return FALSE;
12989 }
12990 if (info->keep_memory)
12991 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12992 }
12993 return TRUE;
12994 }
12995
12996 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12997
12998 static void
12999 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13000 {
13001 Elf_Internal_Shdr *symtab_hdr;
13002
13003 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13004 if (cookie->locsyms != NULL
13005 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13006 free (cookie->locsyms);
13007 }
13008
13009 /* Initialize the relocation information in COOKIE for input section SEC
13010 of input bfd ABFD. */
13011
13012 static bfd_boolean
13013 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13014 struct bfd_link_info *info, bfd *abfd,
13015 asection *sec)
13016 {
13017 if (sec->reloc_count == 0)
13018 {
13019 cookie->rels = NULL;
13020 cookie->relend = NULL;
13021 }
13022 else
13023 {
13024 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13025 info->keep_memory);
13026 if (cookie->rels == NULL)
13027 return FALSE;
13028 cookie->rel = cookie->rels;
13029 cookie->relend = cookie->rels + sec->reloc_count;
13030 }
13031 cookie->rel = cookie->rels;
13032 return TRUE;
13033 }
13034
13035 /* Free the memory allocated by init_reloc_cookie_rels,
13036 if appropriate. */
13037
13038 static void
13039 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13040 asection *sec)
13041 {
13042 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13043 free (cookie->rels);
13044 }
13045
13046 /* Initialize the whole of COOKIE for input section SEC. */
13047
13048 static bfd_boolean
13049 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13050 struct bfd_link_info *info,
13051 asection *sec)
13052 {
13053 if (!init_reloc_cookie (cookie, info, sec->owner))
13054 goto error1;
13055 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13056 goto error2;
13057 return TRUE;
13058
13059 error2:
13060 fini_reloc_cookie (cookie, sec->owner);
13061 error1:
13062 return FALSE;
13063 }
13064
13065 /* Free the memory allocated by init_reloc_cookie_for_section,
13066 if appropriate. */
13067
13068 static void
13069 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13070 asection *sec)
13071 {
13072 fini_reloc_cookie_rels (cookie, sec);
13073 fini_reloc_cookie (cookie, sec->owner);
13074 }
13075
13076 /* Garbage collect unused sections. */
13078
13079 /* Default gc_mark_hook. */
13080
13081 asection *
13082 _bfd_elf_gc_mark_hook (asection *sec,
13083 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13084 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13085 struct elf_link_hash_entry *h,
13086 Elf_Internal_Sym *sym)
13087 {
13088 if (h != NULL)
13089 {
13090 switch (h->root.type)
13091 {
13092 case bfd_link_hash_defined:
13093 case bfd_link_hash_defweak:
13094 return h->root.u.def.section;
13095
13096 case bfd_link_hash_common:
13097 return h->root.u.c.p->section;
13098
13099 default:
13100 break;
13101 }
13102 }
13103 else
13104 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13105
13106 return NULL;
13107 }
13108
13109 /* Return the debug definition section. */
13110
13111 static asection *
13112 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13113 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13114 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13115 struct elf_link_hash_entry *h,
13116 Elf_Internal_Sym *sym)
13117 {
13118 if (h != NULL)
13119 {
13120 /* Return the global debug definition section. */
13121 if ((h->root.type == bfd_link_hash_defined
13122 || h->root.type == bfd_link_hash_defweak)
13123 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13124 return h->root.u.def.section;
13125 }
13126 else
13127 {
13128 /* Return the local debug definition section. */
13129 asection *isec = bfd_section_from_elf_index (sec->owner,
13130 sym->st_shndx);
13131 if ((isec->flags & SEC_DEBUGGING) != 0)
13132 return isec;
13133 }
13134
13135 return NULL;
13136 }
13137
13138 /* COOKIE->rel describes a relocation against section SEC, which is
13139 a section we've decided to keep. Return the section that contains
13140 the relocation symbol, or NULL if no section contains it. */
13141
13142 asection *
13143 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13144 elf_gc_mark_hook_fn gc_mark_hook,
13145 struct elf_reloc_cookie *cookie,
13146 bfd_boolean *start_stop)
13147 {
13148 unsigned long r_symndx;
13149 struct elf_link_hash_entry *h, *hw;
13150
13151 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13152 if (r_symndx == STN_UNDEF)
13153 return NULL;
13154
13155 if (r_symndx >= cookie->locsymcount
13156 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13157 {
13158 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13159 if (h == NULL)
13160 {
13161 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13162 sec->owner);
13163 return NULL;
13164 }
13165 while (h->root.type == bfd_link_hash_indirect
13166 || h->root.type == bfd_link_hash_warning)
13167 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13168 h->mark = 1;
13169 /* Keep all aliases of the symbol too. If an object symbol
13170 needs to be copied into .dynbss then all of its aliases
13171 should be present as dynamic symbols, not just the one used
13172 on the copy relocation. */
13173 hw = h;
13174 while (hw->is_weakalias)
13175 {
13176 hw = hw->u.alias;
13177 hw->mark = 1;
13178 }
13179
13180 if (start_stop != NULL)
13181 {
13182 /* To work around a glibc bug, mark XXX input sections
13183 when there is a reference to __start_XXX or __stop_XXX
13184 symbols. */
13185 if (h->start_stop)
13186 {
13187 asection *s = h->u2.start_stop_section;
13188 *start_stop = !s->gc_mark;
13189 return s;
13190 }
13191 }
13192
13193 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13194 }
13195
13196 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13197 &cookie->locsyms[r_symndx]);
13198 }
13199
13200 /* COOKIE->rel describes a relocation against section SEC, which is
13201 a section we've decided to keep. Mark the section that contains
13202 the relocation symbol. */
13203
13204 bfd_boolean
13205 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13206 asection *sec,
13207 elf_gc_mark_hook_fn gc_mark_hook,
13208 struct elf_reloc_cookie *cookie)
13209 {
13210 asection *rsec;
13211 bfd_boolean start_stop = FALSE;
13212
13213 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13214 while (rsec != NULL)
13215 {
13216 if (!rsec->gc_mark)
13217 {
13218 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13219 || (rsec->owner->flags & DYNAMIC) != 0)
13220 rsec->gc_mark = 1;
13221 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13222 return FALSE;
13223 }
13224 if (!start_stop)
13225 break;
13226 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13227 }
13228 return TRUE;
13229 }
13230
13231 /* The mark phase of garbage collection. For a given section, mark
13232 it and any sections in this section's group, and all the sections
13233 which define symbols to which it refers. */
13234
13235 bfd_boolean
13236 _bfd_elf_gc_mark (struct bfd_link_info *info,
13237 asection *sec,
13238 elf_gc_mark_hook_fn gc_mark_hook)
13239 {
13240 bfd_boolean ret;
13241 asection *group_sec, *eh_frame;
13242
13243 sec->gc_mark = 1;
13244
13245 /* Mark all the sections in the group. */
13246 group_sec = elf_section_data (sec)->next_in_group;
13247 if (group_sec && !group_sec->gc_mark)
13248 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13249 return FALSE;
13250
13251 /* Look through the section relocs. */
13252 ret = TRUE;
13253 eh_frame = elf_eh_frame_section (sec->owner);
13254 if ((sec->flags & SEC_RELOC) != 0
13255 && sec->reloc_count > 0
13256 && sec != eh_frame)
13257 {
13258 struct elf_reloc_cookie cookie;
13259
13260 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13261 ret = FALSE;
13262 else
13263 {
13264 for (; cookie.rel < cookie.relend; cookie.rel++)
13265 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13266 {
13267 ret = FALSE;
13268 break;
13269 }
13270 fini_reloc_cookie_for_section (&cookie, sec);
13271 }
13272 }
13273
13274 if (ret && eh_frame && elf_fde_list (sec))
13275 {
13276 struct elf_reloc_cookie cookie;
13277
13278 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13279 ret = FALSE;
13280 else
13281 {
13282 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13283 gc_mark_hook, &cookie))
13284 ret = FALSE;
13285 fini_reloc_cookie_for_section (&cookie, eh_frame);
13286 }
13287 }
13288
13289 eh_frame = elf_section_eh_frame_entry (sec);
13290 if (ret && eh_frame && !eh_frame->gc_mark)
13291 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13292 ret = FALSE;
13293
13294 return ret;
13295 }
13296
13297 /* Scan and mark sections in a special or debug section group. */
13298
13299 static void
13300 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13301 {
13302 /* Point to first section of section group. */
13303 asection *ssec;
13304 /* Used to iterate the section group. */
13305 asection *msec;
13306
13307 bfd_boolean is_special_grp = TRUE;
13308 bfd_boolean is_debug_grp = TRUE;
13309
13310 /* First scan to see if group contains any section other than debug
13311 and special section. */
13312 ssec = msec = elf_next_in_group (grp);
13313 do
13314 {
13315 if ((msec->flags & SEC_DEBUGGING) == 0)
13316 is_debug_grp = FALSE;
13317
13318 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13319 is_special_grp = FALSE;
13320
13321 msec = elf_next_in_group (msec);
13322 }
13323 while (msec != ssec);
13324
13325 /* If this is a pure debug section group or pure special section group,
13326 keep all sections in this group. */
13327 if (is_debug_grp || is_special_grp)
13328 {
13329 do
13330 {
13331 msec->gc_mark = 1;
13332 msec = elf_next_in_group (msec);
13333 }
13334 while (msec != ssec);
13335 }
13336 }
13337
13338 /* Keep debug and special sections. */
13339
13340 bfd_boolean
13341 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13342 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13343 {
13344 bfd *ibfd;
13345
13346 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13347 {
13348 asection *isec;
13349 bfd_boolean some_kept;
13350 bfd_boolean debug_frag_seen;
13351 bfd_boolean has_kept_debug_info;
13352
13353 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13354 continue;
13355 isec = ibfd->sections;
13356 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13357 continue;
13358
13359 /* Ensure all linker created sections are kept,
13360 see if any other section is already marked,
13361 and note if we have any fragmented debug sections. */
13362 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13363 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13364 {
13365 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13366 isec->gc_mark = 1;
13367 else if (isec->gc_mark
13368 && (isec->flags & SEC_ALLOC) != 0
13369 && elf_section_type (isec) != SHT_NOTE)
13370 some_kept = TRUE;
13371
13372 if (!debug_frag_seen
13373 && (isec->flags & SEC_DEBUGGING)
13374 && CONST_STRNEQ (isec->name, ".debug_line."))
13375 debug_frag_seen = TRUE;
13376 }
13377
13378 /* If no non-note alloc section in this file will be kept, then
13379 we can toss out the debug and special sections. */
13380 if (!some_kept)
13381 continue;
13382
13383 /* Keep debug and special sections like .comment when they are
13384 not part of a group. Also keep section groups that contain
13385 just debug sections or special sections. */
13386 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13387 {
13388 if ((isec->flags & SEC_GROUP) != 0)
13389 _bfd_elf_gc_mark_debug_special_section_group (isec);
13390 else if (((isec->flags & SEC_DEBUGGING) != 0
13391 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13392 && elf_next_in_group (isec) == NULL)
13393 isec->gc_mark = 1;
13394 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13395 has_kept_debug_info = TRUE;
13396 }
13397
13398 /* Look for CODE sections which are going to be discarded,
13399 and find and discard any fragmented debug sections which
13400 are associated with that code section. */
13401 if (debug_frag_seen)
13402 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13403 if ((isec->flags & SEC_CODE) != 0
13404 && isec->gc_mark == 0)
13405 {
13406 unsigned int ilen;
13407 asection *dsec;
13408
13409 ilen = strlen (isec->name);
13410
13411 /* Association is determined by the name of the debug
13412 section containing the name of the code section as
13413 a suffix. For example .debug_line.text.foo is a
13414 debug section associated with .text.foo. */
13415 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13416 {
13417 unsigned int dlen;
13418
13419 if (dsec->gc_mark == 0
13420 || (dsec->flags & SEC_DEBUGGING) == 0)
13421 continue;
13422
13423 dlen = strlen (dsec->name);
13424
13425 if (dlen > ilen
13426 && strncmp (dsec->name + (dlen - ilen),
13427 isec->name, ilen) == 0)
13428 dsec->gc_mark = 0;
13429 }
13430 }
13431
13432 /* Mark debug sections referenced by kept debug sections. */
13433 if (has_kept_debug_info)
13434 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13435 if (isec->gc_mark
13436 && (isec->flags & SEC_DEBUGGING) != 0)
13437 if (!_bfd_elf_gc_mark (info, isec,
13438 elf_gc_mark_debug_section))
13439 return FALSE;
13440 }
13441 return TRUE;
13442 }
13443
13444 static bfd_boolean
13445 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13446 {
13447 bfd *sub;
13448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13449
13450 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13451 {
13452 asection *o;
13453
13454 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13455 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13456 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13457 continue;
13458 o = sub->sections;
13459 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13460 continue;
13461
13462 for (o = sub->sections; o != NULL; o = o->next)
13463 {
13464 /* When any section in a section group is kept, we keep all
13465 sections in the section group. If the first member of
13466 the section group is excluded, we will also exclude the
13467 group section. */
13468 if (o->flags & SEC_GROUP)
13469 {
13470 asection *first = elf_next_in_group (o);
13471 o->gc_mark = first->gc_mark;
13472 }
13473
13474 if (o->gc_mark)
13475 continue;
13476
13477 /* Skip sweeping sections already excluded. */
13478 if (o->flags & SEC_EXCLUDE)
13479 continue;
13480
13481 /* Since this is early in the link process, it is simple
13482 to remove a section from the output. */
13483 o->flags |= SEC_EXCLUDE;
13484
13485 if (info->print_gc_sections && o->size != 0)
13486 /* xgettext:c-format */
13487 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13488 o, sub);
13489 }
13490 }
13491
13492 return TRUE;
13493 }
13494
13495 /* Propagate collected vtable information. This is called through
13496 elf_link_hash_traverse. */
13497
13498 static bfd_boolean
13499 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13500 {
13501 /* Those that are not vtables. */
13502 if (h->start_stop
13503 || h->u2.vtable == NULL
13504 || h->u2.vtable->parent == NULL)
13505 return TRUE;
13506
13507 /* Those vtables that do not have parents, we cannot merge. */
13508 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13509 return TRUE;
13510
13511 /* If we've already been done, exit. */
13512 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13513 return TRUE;
13514
13515 /* Make sure the parent's table is up to date. */
13516 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13517
13518 if (h->u2.vtable->used == NULL)
13519 {
13520 /* None of this table's entries were referenced. Re-use the
13521 parent's table. */
13522 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13523 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13524 }
13525 else
13526 {
13527 size_t n;
13528 bfd_boolean *cu, *pu;
13529
13530 /* Or the parent's entries into ours. */
13531 cu = h->u2.vtable->used;
13532 cu[-1] = TRUE;
13533 pu = h->u2.vtable->parent->u2.vtable->used;
13534 if (pu != NULL)
13535 {
13536 const struct elf_backend_data *bed;
13537 unsigned int log_file_align;
13538
13539 bed = get_elf_backend_data (h->root.u.def.section->owner);
13540 log_file_align = bed->s->log_file_align;
13541 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13542 while (n--)
13543 {
13544 if (*pu)
13545 *cu = TRUE;
13546 pu++;
13547 cu++;
13548 }
13549 }
13550 }
13551
13552 return TRUE;
13553 }
13554
13555 static bfd_boolean
13556 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13557 {
13558 asection *sec;
13559 bfd_vma hstart, hend;
13560 Elf_Internal_Rela *relstart, *relend, *rel;
13561 const struct elf_backend_data *bed;
13562 unsigned int log_file_align;
13563
13564 /* Take care of both those symbols that do not describe vtables as
13565 well as those that are not loaded. */
13566 if (h->start_stop
13567 || h->u2.vtable == NULL
13568 || h->u2.vtable->parent == NULL)
13569 return TRUE;
13570
13571 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13572 || h->root.type == bfd_link_hash_defweak);
13573
13574 sec = h->root.u.def.section;
13575 hstart = h->root.u.def.value;
13576 hend = hstart + h->size;
13577
13578 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13579 if (!relstart)
13580 return *(bfd_boolean *) okp = FALSE;
13581 bed = get_elf_backend_data (sec->owner);
13582 log_file_align = bed->s->log_file_align;
13583
13584 relend = relstart + sec->reloc_count;
13585
13586 for (rel = relstart; rel < relend; ++rel)
13587 if (rel->r_offset >= hstart && rel->r_offset < hend)
13588 {
13589 /* If the entry is in use, do nothing. */
13590 if (h->u2.vtable->used
13591 && (rel->r_offset - hstart) < h->u2.vtable->size)
13592 {
13593 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13594 if (h->u2.vtable->used[entry])
13595 continue;
13596 }
13597 /* Otherwise, kill it. */
13598 rel->r_offset = rel->r_info = rel->r_addend = 0;
13599 }
13600
13601 return TRUE;
13602 }
13603
13604 /* Mark sections containing dynamically referenced symbols. When
13605 building shared libraries, we must assume that any visible symbol is
13606 referenced. */
13607
13608 bfd_boolean
13609 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13610 {
13611 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13612 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13613
13614 if ((h->root.type == bfd_link_hash_defined
13615 || h->root.type == bfd_link_hash_defweak)
13616 && ((h->ref_dynamic && !h->forced_local)
13617 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13618 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13619 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13620 && (!bfd_link_executable (info)
13621 || info->gc_keep_exported
13622 || info->export_dynamic
13623 || (h->dynamic
13624 && d != NULL
13625 && (*d->match) (&d->head, NULL, h->root.root.string)))
13626 && (h->versioned >= versioned
13627 || !bfd_hide_sym_by_version (info->version_info,
13628 h->root.root.string)))))
13629 h->root.u.def.section->flags |= SEC_KEEP;
13630
13631 return TRUE;
13632 }
13633
13634 /* Keep all sections containing symbols undefined on the command-line,
13635 and the section containing the entry symbol. */
13636
13637 void
13638 _bfd_elf_gc_keep (struct bfd_link_info *info)
13639 {
13640 struct bfd_sym_chain *sym;
13641
13642 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13643 {
13644 struct elf_link_hash_entry *h;
13645
13646 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13647 FALSE, FALSE, FALSE);
13648
13649 if (h != NULL
13650 && (h->root.type == bfd_link_hash_defined
13651 || h->root.type == bfd_link_hash_defweak)
13652 && !bfd_is_abs_section (h->root.u.def.section)
13653 && !bfd_is_und_section (h->root.u.def.section))
13654 h->root.u.def.section->flags |= SEC_KEEP;
13655 }
13656 }
13657
13658 bfd_boolean
13659 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13660 struct bfd_link_info *info)
13661 {
13662 bfd *ibfd = info->input_bfds;
13663
13664 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13665 {
13666 asection *sec;
13667 struct elf_reloc_cookie cookie;
13668
13669 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13670 continue;
13671 sec = ibfd->sections;
13672 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13673 continue;
13674
13675 if (!init_reloc_cookie (&cookie, info, ibfd))
13676 return FALSE;
13677
13678 for (sec = ibfd->sections; sec; sec = sec->next)
13679 {
13680 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13681 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13682 {
13683 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13684 fini_reloc_cookie_rels (&cookie, sec);
13685 }
13686 }
13687 }
13688 return TRUE;
13689 }
13690
13691 /* Do mark and sweep of unused sections. */
13692
13693 bfd_boolean
13694 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13695 {
13696 bfd_boolean ok = TRUE;
13697 bfd *sub;
13698 elf_gc_mark_hook_fn gc_mark_hook;
13699 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13700 struct elf_link_hash_table *htab;
13701
13702 if (!bed->can_gc_sections
13703 || !is_elf_hash_table (info->hash))
13704 {
13705 _bfd_error_handler(_("warning: gc-sections option ignored"));
13706 return TRUE;
13707 }
13708
13709 bed->gc_keep (info);
13710 htab = elf_hash_table (info);
13711
13712 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13713 at the .eh_frame section if we can mark the FDEs individually. */
13714 for (sub = info->input_bfds;
13715 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13716 sub = sub->link.next)
13717 {
13718 asection *sec;
13719 struct elf_reloc_cookie cookie;
13720
13721 sec = sub->sections;
13722 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13723 continue;
13724 sec = bfd_get_section_by_name (sub, ".eh_frame");
13725 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13726 {
13727 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13728 if (elf_section_data (sec)->sec_info
13729 && (sec->flags & SEC_LINKER_CREATED) == 0)
13730 elf_eh_frame_section (sub) = sec;
13731 fini_reloc_cookie_for_section (&cookie, sec);
13732 sec = bfd_get_next_section_by_name (NULL, sec);
13733 }
13734 }
13735
13736 /* Apply transitive closure to the vtable entry usage info. */
13737 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13738 if (!ok)
13739 return FALSE;
13740
13741 /* Kill the vtable relocations that were not used. */
13742 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13743 if (!ok)
13744 return FALSE;
13745
13746 /* Mark dynamically referenced symbols. */
13747 if (htab->dynamic_sections_created || info->gc_keep_exported)
13748 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13749
13750 /* Grovel through relocs to find out who stays ... */
13751 gc_mark_hook = bed->gc_mark_hook;
13752 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13753 {
13754 asection *o;
13755
13756 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13757 || elf_object_id (sub) != elf_hash_table_id (htab)
13758 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13759 continue;
13760
13761 o = sub->sections;
13762 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13763 continue;
13764
13765 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13766 Also treat note sections as a root, if the section is not part
13767 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13768 well as FINI_ARRAY sections for ld -r. */
13769 for (o = sub->sections; o != NULL; o = o->next)
13770 if (!o->gc_mark
13771 && (o->flags & SEC_EXCLUDE) == 0
13772 && ((o->flags & SEC_KEEP) != 0
13773 || (bfd_link_relocatable (info)
13774 && ((elf_section_data (o)->this_hdr.sh_type
13775 == SHT_PREINIT_ARRAY)
13776 || (elf_section_data (o)->this_hdr.sh_type
13777 == SHT_INIT_ARRAY)
13778 || (elf_section_data (o)->this_hdr.sh_type
13779 == SHT_FINI_ARRAY)))
13780 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13781 && elf_next_in_group (o) == NULL )))
13782 {
13783 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13784 return FALSE;
13785 }
13786 }
13787
13788 /* Allow the backend to mark additional target specific sections. */
13789 bed->gc_mark_extra_sections (info, gc_mark_hook);
13790
13791 /* ... and mark SEC_EXCLUDE for those that go. */
13792 return elf_gc_sweep (abfd, info);
13793 }
13794
13795 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13797
13798 bfd_boolean
13799 bfd_elf_gc_record_vtinherit (bfd *abfd,
13800 asection *sec,
13801 struct elf_link_hash_entry *h,
13802 bfd_vma offset)
13803 {
13804 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13805 struct elf_link_hash_entry **search, *child;
13806 size_t extsymcount;
13807 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13808
13809 /* The sh_info field of the symtab header tells us where the
13810 external symbols start. We don't care about the local symbols at
13811 this point. */
13812 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13813 if (!elf_bad_symtab (abfd))
13814 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13815
13816 sym_hashes = elf_sym_hashes (abfd);
13817 sym_hashes_end = sym_hashes + extsymcount;
13818
13819 /* Hunt down the child symbol, which is in this section at the same
13820 offset as the relocation. */
13821 for (search = sym_hashes; search != sym_hashes_end; ++search)
13822 {
13823 if ((child = *search) != NULL
13824 && (child->root.type == bfd_link_hash_defined
13825 || child->root.type == bfd_link_hash_defweak)
13826 && child->root.u.def.section == sec
13827 && child->root.u.def.value == offset)
13828 goto win;
13829 }
13830
13831 /* xgettext:c-format */
13832 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13833 abfd, sec, (uint64_t) offset);
13834 bfd_set_error (bfd_error_invalid_operation);
13835 return FALSE;
13836
13837 win:
13838 if (!child->u2.vtable)
13839 {
13840 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13841 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13842 if (!child->u2.vtable)
13843 return FALSE;
13844 }
13845 if (!h)
13846 {
13847 /* This *should* only be the absolute section. It could potentially
13848 be that someone has defined a non-global vtable though, which
13849 would be bad. It isn't worth paging in the local symbols to be
13850 sure though; that case should simply be handled by the assembler. */
13851
13852 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13853 }
13854 else
13855 child->u2.vtable->parent = h;
13856
13857 return TRUE;
13858 }
13859
13860 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13861
13862 bfd_boolean
13863 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13864 struct elf_link_hash_entry *h,
13865 bfd_vma addend)
13866 {
13867 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13868 unsigned int log_file_align = bed->s->log_file_align;
13869
13870 if (!h)
13871 {
13872 /* xgettext:c-format */
13873 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13874 abfd, sec);
13875 bfd_set_error (bfd_error_bad_value);
13876 return FALSE;
13877 }
13878
13879 if (!h->u2.vtable)
13880 {
13881 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13882 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13883 if (!h->u2.vtable)
13884 return FALSE;
13885 }
13886
13887 if (addend >= h->u2.vtable->size)
13888 {
13889 size_t size, bytes, file_align;
13890 bfd_boolean *ptr = h->u2.vtable->used;
13891
13892 /* While the symbol is undefined, we have to be prepared to handle
13893 a zero size. */
13894 file_align = 1 << log_file_align;
13895 if (h->root.type == bfd_link_hash_undefined)
13896 size = addend + file_align;
13897 else
13898 {
13899 size = h->size;
13900 if (addend >= size)
13901 {
13902 /* Oops! We've got a reference past the defined end of
13903 the table. This is probably a bug -- shall we warn? */
13904 size = addend + file_align;
13905 }
13906 }
13907 size = (size + file_align - 1) & -file_align;
13908
13909 /* Allocate one extra entry for use as a "done" flag for the
13910 consolidation pass. */
13911 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13912
13913 if (ptr)
13914 {
13915 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13916
13917 if (ptr != NULL)
13918 {
13919 size_t oldbytes;
13920
13921 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13922 * sizeof (bfd_boolean));
13923 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13924 }
13925 }
13926 else
13927 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13928
13929 if (ptr == NULL)
13930 return FALSE;
13931
13932 /* And arrange for that done flag to be at index -1. */
13933 h->u2.vtable->used = ptr + 1;
13934 h->u2.vtable->size = size;
13935 }
13936
13937 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13938
13939 return TRUE;
13940 }
13941
13942 /* Map an ELF section header flag to its corresponding string. */
13943 typedef struct
13944 {
13945 char *flag_name;
13946 flagword flag_value;
13947 } elf_flags_to_name_table;
13948
13949 static elf_flags_to_name_table elf_flags_to_names [] =
13950 {
13951 { "SHF_WRITE", SHF_WRITE },
13952 { "SHF_ALLOC", SHF_ALLOC },
13953 { "SHF_EXECINSTR", SHF_EXECINSTR },
13954 { "SHF_MERGE", SHF_MERGE },
13955 { "SHF_STRINGS", SHF_STRINGS },
13956 { "SHF_INFO_LINK", SHF_INFO_LINK},
13957 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13958 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13959 { "SHF_GROUP", SHF_GROUP },
13960 { "SHF_TLS", SHF_TLS },
13961 { "SHF_MASKOS", SHF_MASKOS },
13962 { "SHF_EXCLUDE", SHF_EXCLUDE },
13963 };
13964
13965 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13966 bfd_boolean
13967 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13968 struct flag_info *flaginfo,
13969 asection *section)
13970 {
13971 const bfd_vma sh_flags = elf_section_flags (section);
13972
13973 if (!flaginfo->flags_initialized)
13974 {
13975 bfd *obfd = info->output_bfd;
13976 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13977 struct flag_info_list *tf = flaginfo->flag_list;
13978 int with_hex = 0;
13979 int without_hex = 0;
13980
13981 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13982 {
13983 unsigned i;
13984 flagword (*lookup) (char *);
13985
13986 lookup = bed->elf_backend_lookup_section_flags_hook;
13987 if (lookup != NULL)
13988 {
13989 flagword hexval = (*lookup) ((char *) tf->name);
13990
13991 if (hexval != 0)
13992 {
13993 if (tf->with == with_flags)
13994 with_hex |= hexval;
13995 else if (tf->with == without_flags)
13996 without_hex |= hexval;
13997 tf->valid = TRUE;
13998 continue;
13999 }
14000 }
14001 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14002 {
14003 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14004 {
14005 if (tf->with == with_flags)
14006 with_hex |= elf_flags_to_names[i].flag_value;
14007 else if (tf->with == without_flags)
14008 without_hex |= elf_flags_to_names[i].flag_value;
14009 tf->valid = TRUE;
14010 break;
14011 }
14012 }
14013 if (!tf->valid)
14014 {
14015 info->callbacks->einfo
14016 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14017 return FALSE;
14018 }
14019 }
14020 flaginfo->flags_initialized = TRUE;
14021 flaginfo->only_with_flags |= with_hex;
14022 flaginfo->not_with_flags |= without_hex;
14023 }
14024
14025 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14026 return FALSE;
14027
14028 if ((flaginfo->not_with_flags & sh_flags) != 0)
14029 return FALSE;
14030
14031 return TRUE;
14032 }
14033
14034 struct alloc_got_off_arg {
14035 bfd_vma gotoff;
14036 struct bfd_link_info *info;
14037 };
14038
14039 /* We need a special top-level link routine to convert got reference counts
14040 to real got offsets. */
14041
14042 static bfd_boolean
14043 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14044 {
14045 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14046 bfd *obfd = gofarg->info->output_bfd;
14047 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14048
14049 if (h->got.refcount > 0)
14050 {
14051 h->got.offset = gofarg->gotoff;
14052 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14053 }
14054 else
14055 h->got.offset = (bfd_vma) -1;
14056
14057 return TRUE;
14058 }
14059
14060 /* And an accompanying bit to work out final got entry offsets once
14061 we're done. Should be called from final_link. */
14062
14063 bfd_boolean
14064 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14065 struct bfd_link_info *info)
14066 {
14067 bfd *i;
14068 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14069 bfd_vma gotoff;
14070 struct alloc_got_off_arg gofarg;
14071
14072 BFD_ASSERT (abfd == info->output_bfd);
14073
14074 if (! is_elf_hash_table (info->hash))
14075 return FALSE;
14076
14077 /* The GOT offset is relative to the .got section, but the GOT header is
14078 put into the .got.plt section, if the backend uses it. */
14079 if (bed->want_got_plt)
14080 gotoff = 0;
14081 else
14082 gotoff = bed->got_header_size;
14083
14084 /* Do the local .got entries first. */
14085 for (i = info->input_bfds; i; i = i->link.next)
14086 {
14087 bfd_signed_vma *local_got;
14088 size_t j, locsymcount;
14089 Elf_Internal_Shdr *symtab_hdr;
14090
14091 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14092 continue;
14093
14094 local_got = elf_local_got_refcounts (i);
14095 if (!local_got)
14096 continue;
14097
14098 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14099 if (elf_bad_symtab (i))
14100 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14101 else
14102 locsymcount = symtab_hdr->sh_info;
14103
14104 for (j = 0; j < locsymcount; ++j)
14105 {
14106 if (local_got[j] > 0)
14107 {
14108 local_got[j] = gotoff;
14109 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14110 }
14111 else
14112 local_got[j] = (bfd_vma) -1;
14113 }
14114 }
14115
14116 /* Then the global .got entries. .plt refcounts are handled by
14117 adjust_dynamic_symbol */
14118 gofarg.gotoff = gotoff;
14119 gofarg.info = info;
14120 elf_link_hash_traverse (elf_hash_table (info),
14121 elf_gc_allocate_got_offsets,
14122 &gofarg);
14123 return TRUE;
14124 }
14125
14126 /* Many folk need no more in the way of final link than this, once
14127 got entry reference counting is enabled. */
14128
14129 bfd_boolean
14130 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14131 {
14132 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14133 return FALSE;
14134
14135 /* Invoke the regular ELF backend linker to do all the work. */
14136 return bfd_elf_final_link (abfd, info);
14137 }
14138
14139 bfd_boolean
14140 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14141 {
14142 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14143
14144 if (rcookie->bad_symtab)
14145 rcookie->rel = rcookie->rels;
14146
14147 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14148 {
14149 unsigned long r_symndx;
14150
14151 if (! rcookie->bad_symtab)
14152 if (rcookie->rel->r_offset > offset)
14153 return FALSE;
14154 if (rcookie->rel->r_offset != offset)
14155 continue;
14156
14157 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14158 if (r_symndx == STN_UNDEF)
14159 return TRUE;
14160
14161 if (r_symndx >= rcookie->locsymcount
14162 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14163 {
14164 struct elf_link_hash_entry *h;
14165
14166 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14167
14168 while (h->root.type == bfd_link_hash_indirect
14169 || h->root.type == bfd_link_hash_warning)
14170 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14171
14172 if ((h->root.type == bfd_link_hash_defined
14173 || h->root.type == bfd_link_hash_defweak)
14174 && (h->root.u.def.section->owner != rcookie->abfd
14175 || h->root.u.def.section->kept_section != NULL
14176 || discarded_section (h->root.u.def.section)))
14177 return TRUE;
14178 }
14179 else
14180 {
14181 /* It's not a relocation against a global symbol,
14182 but it could be a relocation against a local
14183 symbol for a discarded section. */
14184 asection *isec;
14185 Elf_Internal_Sym *isym;
14186
14187 /* Need to: get the symbol; get the section. */
14188 isym = &rcookie->locsyms[r_symndx];
14189 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14190 if (isec != NULL
14191 && (isec->kept_section != NULL
14192 || discarded_section (isec)))
14193 return TRUE;
14194 }
14195 return FALSE;
14196 }
14197 return FALSE;
14198 }
14199
14200 /* Discard unneeded references to discarded sections.
14201 Returns -1 on error, 1 if any section's size was changed, 0 if
14202 nothing changed. This function assumes that the relocations are in
14203 sorted order, which is true for all known assemblers. */
14204
14205 int
14206 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14207 {
14208 struct elf_reloc_cookie cookie;
14209 asection *o;
14210 bfd *abfd;
14211 int changed = 0;
14212
14213 if (info->traditional_format
14214 || !is_elf_hash_table (info->hash))
14215 return 0;
14216
14217 o = bfd_get_section_by_name (output_bfd, ".stab");
14218 if (o != NULL)
14219 {
14220 asection *i;
14221
14222 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14223 {
14224 if (i->size == 0
14225 || i->reloc_count == 0
14226 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14227 continue;
14228
14229 abfd = i->owner;
14230 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14231 continue;
14232
14233 if (!init_reloc_cookie_for_section (&cookie, info, i))
14234 return -1;
14235
14236 if (_bfd_discard_section_stabs (abfd, i,
14237 elf_section_data (i)->sec_info,
14238 bfd_elf_reloc_symbol_deleted_p,
14239 &cookie))
14240 changed = 1;
14241
14242 fini_reloc_cookie_for_section (&cookie, i);
14243 }
14244 }
14245
14246 o = NULL;
14247 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14248 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14249 if (o != NULL)
14250 {
14251 asection *i;
14252 int eh_changed = 0;
14253 unsigned int eh_alignment;
14254
14255 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14256 {
14257 if (i->size == 0)
14258 continue;
14259
14260 abfd = i->owner;
14261 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14262 continue;
14263
14264 if (!init_reloc_cookie_for_section (&cookie, info, i))
14265 return -1;
14266
14267 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14268 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14269 bfd_elf_reloc_symbol_deleted_p,
14270 &cookie))
14271 {
14272 eh_changed = 1;
14273 if (i->size != i->rawsize)
14274 changed = 1;
14275 }
14276
14277 fini_reloc_cookie_for_section (&cookie, i);
14278 }
14279
14280 eh_alignment = 1 << o->alignment_power;
14281 /* Skip over zero terminator, and prevent empty sections from
14282 adding alignment padding at the end. */
14283 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14284 if (i->size == 0)
14285 i->flags |= SEC_EXCLUDE;
14286 else if (i->size > 4)
14287 break;
14288 /* The last non-empty eh_frame section doesn't need padding. */
14289 if (i != NULL)
14290 i = i->map_tail.s;
14291 /* Any prior sections must pad the last FDE out to the output
14292 section alignment. Otherwise we might have zero padding
14293 between sections, which would be seen as a terminator. */
14294 for (; i != NULL; i = i->map_tail.s)
14295 if (i->size == 4)
14296 /* All but the last zero terminator should have been removed. */
14297 BFD_FAIL ();
14298 else
14299 {
14300 bfd_size_type size
14301 = (i->size + eh_alignment - 1) & -eh_alignment;
14302 if (i->size != size)
14303 {
14304 i->size = size;
14305 changed = 1;
14306 eh_changed = 1;
14307 }
14308 }
14309 if (eh_changed)
14310 elf_link_hash_traverse (elf_hash_table (info),
14311 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14312 }
14313
14314 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14315 {
14316 const struct elf_backend_data *bed;
14317 asection *s;
14318
14319 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14320 continue;
14321 s = abfd->sections;
14322 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14323 continue;
14324
14325 bed = get_elf_backend_data (abfd);
14326
14327 if (bed->elf_backend_discard_info != NULL)
14328 {
14329 if (!init_reloc_cookie (&cookie, info, abfd))
14330 return -1;
14331
14332 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14333 changed = 1;
14334
14335 fini_reloc_cookie (&cookie, abfd);
14336 }
14337 }
14338
14339 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14340 _bfd_elf_end_eh_frame_parsing (info);
14341
14342 if (info->eh_frame_hdr_type
14343 && !bfd_link_relocatable (info)
14344 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14345 changed = 1;
14346
14347 return changed;
14348 }
14349
14350 bfd_boolean
14351 _bfd_elf_section_already_linked (bfd *abfd,
14352 asection *sec,
14353 struct bfd_link_info *info)
14354 {
14355 flagword flags;
14356 const char *name, *key;
14357 struct bfd_section_already_linked *l;
14358 struct bfd_section_already_linked_hash_entry *already_linked_list;
14359
14360 if (sec->output_section == bfd_abs_section_ptr)
14361 return FALSE;
14362
14363 flags = sec->flags;
14364
14365 /* Return if it isn't a linkonce section. A comdat group section
14366 also has SEC_LINK_ONCE set. */
14367 if ((flags & SEC_LINK_ONCE) == 0)
14368 return FALSE;
14369
14370 /* Don't put group member sections on our list of already linked
14371 sections. They are handled as a group via their group section. */
14372 if (elf_sec_group (sec) != NULL)
14373 return FALSE;
14374
14375 /* For a SHT_GROUP section, use the group signature as the key. */
14376 name = sec->name;
14377 if ((flags & SEC_GROUP) != 0
14378 && elf_next_in_group (sec) != NULL
14379 && elf_group_name (elf_next_in_group (sec)) != NULL)
14380 key = elf_group_name (elf_next_in_group (sec));
14381 else
14382 {
14383 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14384 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14385 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14386 key++;
14387 else
14388 /* Must be a user linkonce section that doesn't follow gcc's
14389 naming convention. In this case we won't be matching
14390 single member groups. */
14391 key = name;
14392 }
14393
14394 already_linked_list = bfd_section_already_linked_table_lookup (key);
14395
14396 for (l = already_linked_list->entry; l != NULL; l = l->next)
14397 {
14398 /* We may have 2 different types of sections on the list: group
14399 sections with a signature of <key> (<key> is some string),
14400 and linkonce sections named .gnu.linkonce.<type>.<key>.
14401 Match like sections. LTO plugin sections are an exception.
14402 They are always named .gnu.linkonce.t.<key> and match either
14403 type of section. */
14404 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14405 && ((flags & SEC_GROUP) != 0
14406 || strcmp (name, l->sec->name) == 0))
14407 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14408 {
14409 /* The section has already been linked. See if we should
14410 issue a warning. */
14411 if (!_bfd_handle_already_linked (sec, l, info))
14412 return FALSE;
14413
14414 if (flags & SEC_GROUP)
14415 {
14416 asection *first = elf_next_in_group (sec);
14417 asection *s = first;
14418
14419 while (s != NULL)
14420 {
14421 s->output_section = bfd_abs_section_ptr;
14422 /* Record which group discards it. */
14423 s->kept_section = l->sec;
14424 s = elf_next_in_group (s);
14425 /* These lists are circular. */
14426 if (s == first)
14427 break;
14428 }
14429 }
14430
14431 return TRUE;
14432 }
14433 }
14434
14435 /* A single member comdat group section may be discarded by a
14436 linkonce section and vice versa. */
14437 if ((flags & SEC_GROUP) != 0)
14438 {
14439 asection *first = elf_next_in_group (sec);
14440
14441 if (first != NULL && elf_next_in_group (first) == first)
14442 /* Check this single member group against linkonce sections. */
14443 for (l = already_linked_list->entry; l != NULL; l = l->next)
14444 if ((l->sec->flags & SEC_GROUP) == 0
14445 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14446 {
14447 first->output_section = bfd_abs_section_ptr;
14448 first->kept_section = l->sec;
14449 sec->output_section = bfd_abs_section_ptr;
14450 break;
14451 }
14452 }
14453 else
14454 /* Check this linkonce section against single member groups. */
14455 for (l = already_linked_list->entry; l != NULL; l = l->next)
14456 if (l->sec->flags & SEC_GROUP)
14457 {
14458 asection *first = elf_next_in_group (l->sec);
14459
14460 if (first != NULL
14461 && elf_next_in_group (first) == first
14462 && bfd_elf_match_symbols_in_sections (first, sec, info))
14463 {
14464 sec->output_section = bfd_abs_section_ptr;
14465 sec->kept_section = first;
14466 break;
14467 }
14468 }
14469
14470 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14471 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14472 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14473 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14474 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14475 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14476 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14477 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14478 The reverse order cannot happen as there is never a bfd with only the
14479 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14480 matter as here were are looking only for cross-bfd sections. */
14481
14482 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14483 for (l = already_linked_list->entry; l != NULL; l = l->next)
14484 if ((l->sec->flags & SEC_GROUP) == 0
14485 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14486 {
14487 if (abfd != l->sec->owner)
14488 sec->output_section = bfd_abs_section_ptr;
14489 break;
14490 }
14491
14492 /* This is the first section with this name. Record it. */
14493 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14494 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14495 return sec->output_section == bfd_abs_section_ptr;
14496 }
14497
14498 bfd_boolean
14499 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14500 {
14501 return sym->st_shndx == SHN_COMMON;
14502 }
14503
14504 unsigned int
14505 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14506 {
14507 return SHN_COMMON;
14508 }
14509
14510 asection *
14511 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14512 {
14513 return bfd_com_section_ptr;
14514 }
14515
14516 bfd_vma
14517 _bfd_elf_default_got_elt_size (bfd *abfd,
14518 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14519 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14520 bfd *ibfd ATTRIBUTE_UNUSED,
14521 unsigned long symndx ATTRIBUTE_UNUSED)
14522 {
14523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14524 return bed->s->arch_size / 8;
14525 }
14526
14527 /* Routines to support the creation of dynamic relocs. */
14528
14529 /* Returns the name of the dynamic reloc section associated with SEC. */
14530
14531 static const char *
14532 get_dynamic_reloc_section_name (bfd * abfd,
14533 asection * sec,
14534 bfd_boolean is_rela)
14535 {
14536 char *name;
14537 const char *old_name = bfd_section_name (sec);
14538 const char *prefix = is_rela ? ".rela" : ".rel";
14539
14540 if (old_name == NULL)
14541 return NULL;
14542
14543 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14544 sprintf (name, "%s%s", prefix, old_name);
14545
14546 return name;
14547 }
14548
14549 /* Returns the dynamic reloc section associated with SEC.
14550 If necessary compute the name of the dynamic reloc section based
14551 on SEC's name (looked up in ABFD's string table) and the setting
14552 of IS_RELA. */
14553
14554 asection *
14555 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14556 asection * sec,
14557 bfd_boolean is_rela)
14558 {
14559 asection * reloc_sec = elf_section_data (sec)->sreloc;
14560
14561 if (reloc_sec == NULL)
14562 {
14563 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14564
14565 if (name != NULL)
14566 {
14567 reloc_sec = bfd_get_linker_section (abfd, name);
14568
14569 if (reloc_sec != NULL)
14570 elf_section_data (sec)->sreloc = reloc_sec;
14571 }
14572 }
14573
14574 return reloc_sec;
14575 }
14576
14577 /* Returns the dynamic reloc section associated with SEC. If the
14578 section does not exist it is created and attached to the DYNOBJ
14579 bfd and stored in the SRELOC field of SEC's elf_section_data
14580 structure.
14581
14582 ALIGNMENT is the alignment for the newly created section and
14583 IS_RELA defines whether the name should be .rela.<SEC's name>
14584 or .rel.<SEC's name>. The section name is looked up in the
14585 string table associated with ABFD. */
14586
14587 asection *
14588 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14589 bfd *dynobj,
14590 unsigned int alignment,
14591 bfd *abfd,
14592 bfd_boolean is_rela)
14593 {
14594 asection * reloc_sec = elf_section_data (sec)->sreloc;
14595
14596 if (reloc_sec == NULL)
14597 {
14598 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14599
14600 if (name == NULL)
14601 return NULL;
14602
14603 reloc_sec = bfd_get_linker_section (dynobj, name);
14604
14605 if (reloc_sec == NULL)
14606 {
14607 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14608 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14609 if ((sec->flags & SEC_ALLOC) != 0)
14610 flags |= SEC_ALLOC | SEC_LOAD;
14611
14612 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14613 if (reloc_sec != NULL)
14614 {
14615 /* _bfd_elf_get_sec_type_attr chooses a section type by
14616 name. Override as it may be wrong, eg. for a user
14617 section named "auto" we'll get ".relauto" which is
14618 seen to be a .rela section. */
14619 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14620 if (!bfd_set_section_alignment (reloc_sec, alignment))
14621 reloc_sec = NULL;
14622 }
14623 }
14624
14625 elf_section_data (sec)->sreloc = reloc_sec;
14626 }
14627
14628 return reloc_sec;
14629 }
14630
14631 /* Copy the ELF symbol type and other attributes for a linker script
14632 assignment from HSRC to HDEST. Generally this should be treated as
14633 if we found a strong non-dynamic definition for HDEST (except that
14634 ld ignores multiple definition errors). */
14635 void
14636 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14637 struct bfd_link_hash_entry *hdest,
14638 struct bfd_link_hash_entry *hsrc)
14639 {
14640 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14641 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14642 Elf_Internal_Sym isym;
14643
14644 ehdest->type = ehsrc->type;
14645 ehdest->target_internal = ehsrc->target_internal;
14646
14647 isym.st_other = ehsrc->other;
14648 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14649 }
14650
14651 /* Append a RELA relocation REL to section S in BFD. */
14652
14653 void
14654 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14655 {
14656 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14657 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14658 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14659 bed->s->swap_reloca_out (abfd, rel, loc);
14660 }
14661
14662 /* Append a REL relocation REL to section S in BFD. */
14663
14664 void
14665 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14666 {
14667 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14668 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14669 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14670 bed->s->swap_reloc_out (abfd, rel, loc);
14671 }
14672
14673 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14674
14675 struct bfd_link_hash_entry *
14676 bfd_elf_define_start_stop (struct bfd_link_info *info,
14677 const char *symbol, asection *sec)
14678 {
14679 struct elf_link_hash_entry *h;
14680
14681 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14682 FALSE, FALSE, TRUE);
14683 if (h != NULL
14684 && (h->root.type == bfd_link_hash_undefined
14685 || h->root.type == bfd_link_hash_undefweak
14686 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14687 {
14688 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14689 h->root.type = bfd_link_hash_defined;
14690 h->root.u.def.section = sec;
14691 h->root.u.def.value = 0;
14692 h->def_regular = 1;
14693 h->def_dynamic = 0;
14694 h->start_stop = 1;
14695 h->u2.start_stop_section = sec;
14696 if (symbol[0] == '.')
14697 {
14698 /* .startof. and .sizeof. symbols are local. */
14699 const struct elf_backend_data *bed;
14700 bed = get_elf_backend_data (info->output_bfd);
14701 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14702 }
14703 else
14704 {
14705 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14706 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14707 if (was_dynamic)
14708 bfd_elf_link_record_dynamic_symbol (info, h);
14709 }
14710 return &h->root;
14711 }
14712 return NULL;
14713 }
14714