PR24623, DWARF errors
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2019 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 (abfd, 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 (abfd, 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 (abfd, s,
180 bed->s->log_file_align))
181 return FALSE;
182 htab->sgotplt = s;
183 }
184
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
187
188 if (bed->want_got_sym)
189 {
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
196 elf_hash_table (info)->hgot = h;
197 if (h == NULL)
198 return FALSE;
199 }
200
201 return TRUE;
202 }
203 \f
204 /* 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 (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash && 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 (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368 }
369
370 /* Create dynamic sections when linking against a dynamic object. */
371
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 (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
485 }
486 }
487
488 return TRUE;
489 }
490 \f
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
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 \f
567 /* Mark a symbol dynamic. */
568
569 static void
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
573 {
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
575
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
579
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
595 }
596
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600 bfd_boolean
601 bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
603 const char *name,
604 bfd_boolean provide,
605 bfd_boolean hidden)
606 {
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
610
611 if (!is_elf_hash_table (info->hash))
612 return TRUE;
613
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
616 if (h == NULL)
617 return provide;
618
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
643 switch (h->root.type)
644 {
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
657 break;
658 case bfd_link_hash_new:
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
675 default:
676 BFD_FAIL ();
677 return FALSE;
678 }
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
685 && h->def_dynamic
686 && !h->def_regular)
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
698
699 h->def_regular = 1;
700
701 if (hidden)
702 {
703 bed = get_elf_backend_data (output_bfd);
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
711 if (!bfd_link_relocatable (info)
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
717 if ((h->def_dynamic
718 || h->ref_dynamic
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
721 && !h->forced_local
722 && h->dynindx == -1)
723 {
724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
730 if (h->is_weakalias)
731 {
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741 }
742
743 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747 int
748 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
751 {
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
756 size_t dynstr_index;
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
761 if (! is_elf_hash_table (info->hash))
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
776 1, input_indx, &entry->isym, esym, &eshndx))
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
783 && entry->isym.st_shndx < SHN_LORESERVE)
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
811 if (dynstr_index == (size_t) -1)
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830 }
831
832 /* Return the dynindex of a local dynamic symbol. */
833
834 long
835 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
838 {
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845 }
846
847 /* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
851 static bfd_boolean
852 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
854 {
855 size_t *count = (size_t *) data;
856
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864 }
865
866
867 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870 static bfd_boolean
871 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873 {
874 size_t *count = (size_t *) data;
875
876 if (!h->forced_local)
877 return TRUE;
878
879 if (h->dynindx != -1)
880 h->dynindx = ++(*count);
881
882 return TRUE;
883 }
884
885 /* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887 bfd_boolean
888 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
891 {
892 struct elf_link_hash_table *htab;
893 asection *ip;
894
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
902 htab = elf_hash_table (info);
903 if (htab->text_index_section != NULL)
904 return p != htab->text_index_section && p != htab->data_index_section;
905
906 return (htab->dynobj != NULL
907 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
908 && ip->output_section == p);
909
910 /* There shouldn't be section relative relocations
911 against any other section. */
912 default:
913 return TRUE;
914 }
915 }
916
917 bfd_boolean
918 _bfd_elf_omit_section_dynsym_all
919 (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info ATTRIBUTE_UNUSED,
921 asection *p ATTRIBUTE_UNUSED)
922 {
923 return TRUE;
924 }
925
926 /* Assign dynsym indices. In a shared library we generate a section
927 symbol for each output section, which come first. Next come symbols
928 which have been forced to local binding. Then all of the back-end
929 allocated local dynamic syms, followed by the rest of the global
930 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
931 (This prevents the early call before elf_backend_init_index_section
932 and strip_excluded_output_sections setting dynindx for sections
933 that are stripped.) */
934
935 static unsigned long
936 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
937 struct bfd_link_info *info,
938 unsigned long *section_sym_count)
939 {
940 unsigned long dynsymcount = 0;
941 bfd_boolean do_sec = section_sym_count != NULL;
942
943 if (bfd_link_pic (info)
944 || elf_hash_table (info)->is_relocatable_executable)
945 {
946 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947 asection *p;
948 for (p = output_bfd->sections; p ; p = p->next)
949 if ((p->flags & SEC_EXCLUDE) == 0
950 && (p->flags & SEC_ALLOC) != 0
951 && elf_hash_table (info)->dynamic_relocs
952 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
953 {
954 ++dynsymcount;
955 if (do_sec)
956 elf_section_data (p)->dynindx = dynsymcount;
957 }
958 else if (do_sec)
959 elf_section_data (p)->dynindx = 0;
960 }
961 if (do_sec)
962 *section_sym_count = dynsymcount;
963
964 elf_link_hash_traverse (elf_hash_table (info),
965 elf_link_renumber_local_hash_table_dynsyms,
966 &dynsymcount);
967
968 if (elf_hash_table (info)->dynlocal)
969 {
970 struct elf_link_local_dynamic_entry *p;
971 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
972 p->dynindx = ++dynsymcount;
973 }
974 elf_hash_table (info)->local_dynsymcount = dynsymcount;
975
976 elf_link_hash_traverse (elf_hash_table (info),
977 elf_link_renumber_hash_table_dynsyms,
978 &dynsymcount);
979
980 /* There is an unused NULL entry at the head of the table which we
981 must account for in our count even if the table is empty since it
982 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
983 .dynamic section. */
984 dynsymcount++;
985
986 elf_hash_table (info)->dynsymcount = dynsymcount;
987 return dynsymcount;
988 }
989
990 /* Merge st_other field. */
991
992 static void
993 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
994 const Elf_Internal_Sym *isym, asection *sec,
995 bfd_boolean definition, bfd_boolean dynamic)
996 {
997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
998
999 /* If st_other has a processor-specific meaning, specific
1000 code might be needed here. */
1001 if (bed->elf_backend_merge_symbol_attribute)
1002 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1003 dynamic);
1004
1005 if (!dynamic)
1006 {
1007 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1008 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1009
1010 /* Keep the most constraining visibility. Leave the remainder
1011 of the st_other field to elf_backend_merge_symbol_attribute. */
1012 if (symvis - 1 < hvis - 1)
1013 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1014 }
1015 else if (definition
1016 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1017 && (sec->flags & SEC_READONLY) == 0)
1018 h->protected_def = 1;
1019 }
1020
1021 /* This function is called when we want to merge a new symbol with an
1022 existing symbol. It handles the various cases which arise when we
1023 find a definition in a dynamic object, or when there is already a
1024 definition in a dynamic object. The new symbol is described by
1025 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1026 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1027 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1028 of an old common symbol. We set OVERRIDE if the old symbol is
1029 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1030 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1031 to change. By OK to change, we mean that we shouldn't warn if the
1032 type or size does change. */
1033
1034 static bfd_boolean
1035 _bfd_elf_merge_symbol (bfd *abfd,
1036 struct bfd_link_info *info,
1037 const char *name,
1038 Elf_Internal_Sym *sym,
1039 asection **psec,
1040 bfd_vma *pvalue,
1041 struct elf_link_hash_entry **sym_hash,
1042 bfd **poldbfd,
1043 bfd_boolean *pold_weak,
1044 unsigned int *pold_alignment,
1045 bfd_boolean *skip,
1046 bfd_boolean *override,
1047 bfd_boolean *type_change_ok,
1048 bfd_boolean *size_change_ok,
1049 bfd_boolean *matched)
1050 {
1051 asection *sec, *oldsec;
1052 struct elf_link_hash_entry *h;
1053 struct elf_link_hash_entry *hi;
1054 struct elf_link_hash_entry *flip;
1055 int bind;
1056 bfd *oldbfd;
1057 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1058 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1059 const struct elf_backend_data *bed;
1060 char *new_version;
1061 bfd_boolean default_sym = *matched;
1062
1063 *skip = FALSE;
1064 *override = FALSE;
1065
1066 sec = *psec;
1067 bind = ELF_ST_BIND (sym->st_info);
1068
1069 if (! bfd_is_und_section (sec))
1070 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1071 else
1072 h = ((struct elf_link_hash_entry *)
1073 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1074 if (h == NULL)
1075 return FALSE;
1076 *sym_hash = h;
1077
1078 bed = get_elf_backend_data (abfd);
1079
1080 /* NEW_VERSION is the symbol version of the new symbol. */
1081 if (h->versioned != unversioned)
1082 {
1083 /* Symbol version is unknown or versioned. */
1084 new_version = strrchr (name, ELF_VER_CHR);
1085 if (new_version)
1086 {
1087 if (h->versioned == unknown)
1088 {
1089 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1090 h->versioned = versioned_hidden;
1091 else
1092 h->versioned = versioned;
1093 }
1094 new_version += 1;
1095 if (new_version[0] == '\0')
1096 new_version = NULL;
1097 }
1098 else
1099 h->versioned = unversioned;
1100 }
1101 else
1102 new_version = NULL;
1103
1104 /* For merging, we only care about real symbols. But we need to make
1105 sure that indirect symbol dynamic flags are updated. */
1106 hi = h;
1107 while (h->root.type == bfd_link_hash_indirect
1108 || h->root.type == bfd_link_hash_warning)
1109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1110
1111 if (!*matched)
1112 {
1113 if (hi == h || h->root.type == bfd_link_hash_new)
1114 *matched = TRUE;
1115 else
1116 {
1117 /* OLD_HIDDEN is true if the existing symbol is only visible
1118 to the symbol with the same symbol version. NEW_HIDDEN is
1119 true if the new symbol is only visible to the symbol with
1120 the same symbol version. */
1121 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1122 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1123 if (!old_hidden && !new_hidden)
1124 /* The new symbol matches the existing symbol if both
1125 aren't hidden. */
1126 *matched = TRUE;
1127 else
1128 {
1129 /* OLD_VERSION is the symbol version of the existing
1130 symbol. */
1131 char *old_version;
1132
1133 if (h->versioned >= versioned)
1134 old_version = strrchr (h->root.root.string,
1135 ELF_VER_CHR) + 1;
1136 else
1137 old_version = NULL;
1138
1139 /* The new symbol matches the existing symbol if they
1140 have the same symbol version. */
1141 *matched = (old_version == new_version
1142 || (old_version != NULL
1143 && new_version != NULL
1144 && strcmp (old_version, new_version) == 0));
1145 }
1146 }
1147 }
1148
1149 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1150 existing symbol. */
1151
1152 oldbfd = NULL;
1153 oldsec = NULL;
1154 switch (h->root.type)
1155 {
1156 default:
1157 break;
1158
1159 case bfd_link_hash_undefined:
1160 case bfd_link_hash_undefweak:
1161 oldbfd = h->root.u.undef.abfd;
1162 break;
1163
1164 case bfd_link_hash_defined:
1165 case bfd_link_hash_defweak:
1166 oldbfd = h->root.u.def.section->owner;
1167 oldsec = h->root.u.def.section;
1168 break;
1169
1170 case bfd_link_hash_common:
1171 oldbfd = h->root.u.c.p->section->owner;
1172 oldsec = h->root.u.c.p->section;
1173 if (pold_alignment)
1174 *pold_alignment = h->root.u.c.p->alignment_power;
1175 break;
1176 }
1177 if (poldbfd && *poldbfd == NULL)
1178 *poldbfd = oldbfd;
1179
1180 /* Differentiate strong and weak symbols. */
1181 newweak = bind == STB_WEAK;
1182 oldweak = (h->root.type == bfd_link_hash_defweak
1183 || h->root.type == bfd_link_hash_undefweak);
1184 if (pold_weak)
1185 *pold_weak = oldweak;
1186
1187 /* We have to check it for every instance since the first few may be
1188 references and not all compilers emit symbol type for undefined
1189 symbols. */
1190 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1191
1192 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1193 respectively, is from a dynamic object. */
1194
1195 newdyn = (abfd->flags & DYNAMIC) != 0;
1196
1197 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1198 syms and defined syms in dynamic libraries respectively.
1199 ref_dynamic on the other hand can be set for a symbol defined in
1200 a dynamic library, and def_dynamic may not be set; When the
1201 definition in a dynamic lib is overridden by a definition in the
1202 executable use of the symbol in the dynamic lib becomes a
1203 reference to the executable symbol. */
1204 if (newdyn)
1205 {
1206 if (bfd_is_und_section (sec))
1207 {
1208 if (bind != STB_WEAK)
1209 {
1210 h->ref_dynamic_nonweak = 1;
1211 hi->ref_dynamic_nonweak = 1;
1212 }
1213 }
1214 else
1215 {
1216 /* Update the existing symbol only if they match. */
1217 if (*matched)
1218 h->dynamic_def = 1;
1219 hi->dynamic_def = 1;
1220 }
1221 }
1222
1223 /* If we just created the symbol, mark it as being an ELF symbol.
1224 Other than that, there is nothing to do--there is no merge issue
1225 with a newly defined symbol--so we just return. */
1226
1227 if (h->root.type == bfd_link_hash_new)
1228 {
1229 h->non_elf = 0;
1230 return TRUE;
1231 }
1232
1233 /* In cases involving weak versioned symbols, we may wind up trying
1234 to merge a symbol with itself. Catch that here, to avoid the
1235 confusion that results if we try to override a symbol with
1236 itself. The additional tests catch cases like
1237 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1238 dynamic object, which we do want to handle here. */
1239 if (abfd == oldbfd
1240 && (newweak || oldweak)
1241 && ((abfd->flags & DYNAMIC) == 0
1242 || !h->def_regular))
1243 return TRUE;
1244
1245 olddyn = FALSE;
1246 if (oldbfd != NULL)
1247 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1248 else if (oldsec != NULL)
1249 {
1250 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1251 indices used by MIPS ELF. */
1252 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1253 }
1254
1255 /* Handle a case where plugin_notice won't be called and thus won't
1256 set the non_ir_ref flags on the first pass over symbols. */
1257 if (oldbfd != NULL
1258 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1259 && newdyn != olddyn)
1260 {
1261 h->root.non_ir_ref_dynamic = TRUE;
1262 hi->root.non_ir_ref_dynamic = TRUE;
1263 }
1264
1265 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1266 respectively, appear to be a definition rather than reference. */
1267
1268 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1269
1270 olddef = (h->root.type != bfd_link_hash_undefined
1271 && h->root.type != bfd_link_hash_undefweak
1272 && h->root.type != bfd_link_hash_common);
1273
1274 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1275 respectively, appear to be a function. */
1276
1277 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1278 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1279
1280 oldfunc = (h->type != STT_NOTYPE
1281 && bed->is_function_type (h->type));
1282
1283 if (!(newfunc && oldfunc)
1284 && ELF_ST_TYPE (sym->st_info) != h->type
1285 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && h->type != STT_NOTYPE
1287 && (newdef || bfd_is_com_section (sec))
1288 && (olddef || h->root.type == bfd_link_hash_common))
1289 {
1290 /* If creating a default indirect symbol ("foo" or "foo@") from
1291 a dynamic versioned definition ("foo@@") skip doing so if
1292 there is an existing regular definition with a different
1293 type. We don't want, for example, a "time" variable in the
1294 executable overriding a "time" function in a shared library. */
1295 if (newdyn
1296 && !olddyn)
1297 {
1298 *skip = TRUE;
1299 return TRUE;
1300 }
1301
1302 /* When adding a symbol from a regular object file after we have
1303 created indirect symbols, undo the indirection and any
1304 dynamic state. */
1305 if (hi != h
1306 && !newdyn
1307 && olddyn)
1308 {
1309 h = hi;
1310 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1311 h->forced_local = 0;
1312 h->ref_dynamic = 0;
1313 h->def_dynamic = 0;
1314 h->dynamic_def = 0;
1315 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1316 {
1317 h->root.type = bfd_link_hash_undefined;
1318 h->root.u.undef.abfd = abfd;
1319 }
1320 else
1321 {
1322 h->root.type = bfd_link_hash_new;
1323 h->root.u.undef.abfd = NULL;
1324 }
1325 return TRUE;
1326 }
1327 }
1328
1329 /* Check TLS symbols. We don't check undefined symbols introduced
1330 by "ld -u" which have no type (and oldbfd NULL), and we don't
1331 check symbols from plugins because they also have no type. */
1332 if (oldbfd != NULL
1333 && (oldbfd->flags & BFD_PLUGIN) == 0
1334 && (abfd->flags & BFD_PLUGIN) == 0
1335 && ELF_ST_TYPE (sym->st_info) != h->type
1336 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1337 {
1338 bfd *ntbfd, *tbfd;
1339 bfd_boolean ntdef, tdef;
1340 asection *ntsec, *tsec;
1341
1342 if (h->type == STT_TLS)
1343 {
1344 ntbfd = abfd;
1345 ntsec = sec;
1346 ntdef = newdef;
1347 tbfd = oldbfd;
1348 tsec = oldsec;
1349 tdef = olddef;
1350 }
1351 else
1352 {
1353 ntbfd = oldbfd;
1354 ntsec = oldsec;
1355 ntdef = olddef;
1356 tbfd = abfd;
1357 tsec = sec;
1358 tdef = newdef;
1359 }
1360
1361 if (tdef && ntdef)
1362 _bfd_error_handler
1363 /* xgettext:c-format */
1364 (_("%s: TLS definition in %pB section %pA "
1365 "mismatches non-TLS definition in %pB section %pA"),
1366 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1367 else if (!tdef && !ntdef)
1368 _bfd_error_handler
1369 /* xgettext:c-format */
1370 (_("%s: TLS reference in %pB "
1371 "mismatches non-TLS reference in %pB"),
1372 h->root.root.string, tbfd, ntbfd);
1373 else if (tdef)
1374 _bfd_error_handler
1375 /* xgettext:c-format */
1376 (_("%s: TLS definition in %pB section %pA "
1377 "mismatches non-TLS reference in %pB"),
1378 h->root.root.string, tbfd, tsec, ntbfd);
1379 else
1380 _bfd_error_handler
1381 /* xgettext:c-format */
1382 (_("%s: TLS reference in %pB "
1383 "mismatches non-TLS definition in %pB section %pA"),
1384 h->root.root.string, tbfd, ntbfd, ntsec);
1385
1386 bfd_set_error (bfd_error_bad_value);
1387 return FALSE;
1388 }
1389
1390 /* If the old symbol has non-default visibility, we ignore the new
1391 definition from a dynamic object. */
1392 if (newdyn
1393 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1394 && !bfd_is_und_section (sec))
1395 {
1396 *skip = TRUE;
1397 /* Make sure this symbol is dynamic. */
1398 h->ref_dynamic = 1;
1399 hi->ref_dynamic = 1;
1400 /* A protected symbol has external availability. Make sure it is
1401 recorded as dynamic.
1402
1403 FIXME: Should we check type and size for protected symbol? */
1404 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1405 return bfd_elf_link_record_dynamic_symbol (info, h);
1406 else
1407 return TRUE;
1408 }
1409 else if (!newdyn
1410 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1411 && h->def_dynamic)
1412 {
1413 /* If the new symbol with non-default visibility comes from a
1414 relocatable file and the old definition comes from a dynamic
1415 object, we remove the old definition. */
1416 if (hi->root.type == bfd_link_hash_indirect)
1417 {
1418 /* Handle the case where the old dynamic definition is
1419 default versioned. We need to copy the symbol info from
1420 the symbol with default version to the normal one if it
1421 was referenced before. */
1422 if (h->ref_regular)
1423 {
1424 hi->root.type = h->root.type;
1425 h->root.type = bfd_link_hash_indirect;
1426 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1427
1428 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1429 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1430 {
1431 /* If the new symbol is hidden or internal, completely undo
1432 any dynamic link state. */
1433 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1434 h->forced_local = 0;
1435 h->ref_dynamic = 0;
1436 }
1437 else
1438 h->ref_dynamic = 1;
1439
1440 h->def_dynamic = 0;
1441 /* FIXME: Should we check type and size for protected symbol? */
1442 h->size = 0;
1443 h->type = 0;
1444
1445 h = hi;
1446 }
1447 else
1448 h = hi;
1449 }
1450
1451 /* If the old symbol was undefined before, then it will still be
1452 on the undefs list. If the new symbol is undefined or
1453 common, we can't make it bfd_link_hash_new here, because new
1454 undefined or common symbols will be added to the undefs list
1455 by _bfd_generic_link_add_one_symbol. Symbols may not be
1456 added twice to the undefs list. Also, if the new symbol is
1457 undefweak then we don't want to lose the strong undef. */
1458 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1459 {
1460 h->root.type = bfd_link_hash_undefined;
1461 h->root.u.undef.abfd = abfd;
1462 }
1463 else
1464 {
1465 h->root.type = bfd_link_hash_new;
1466 h->root.u.undef.abfd = NULL;
1467 }
1468
1469 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1470 {
1471 /* If the new symbol is hidden or internal, completely undo
1472 any dynamic link state. */
1473 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1474 h->forced_local = 0;
1475 h->ref_dynamic = 0;
1476 }
1477 else
1478 h->ref_dynamic = 1;
1479 h->def_dynamic = 0;
1480 /* FIXME: Should we check type and size for protected symbol? */
1481 h->size = 0;
1482 h->type = 0;
1483 return TRUE;
1484 }
1485
1486 /* If a new weak symbol definition comes from a regular file and the
1487 old symbol comes from a dynamic library, we treat the new one as
1488 strong. Similarly, an old weak symbol definition from a regular
1489 file is treated as strong when the new symbol comes from a dynamic
1490 library. Further, an old weak symbol from a dynamic library is
1491 treated as strong if the new symbol is from a dynamic library.
1492 This reflects the way glibc's ld.so works.
1493
1494 Also allow a weak symbol to override a linker script symbol
1495 defined by an early pass over the script. This is done so the
1496 linker knows the symbol is defined in an object file, for the
1497 DEFINED script function.
1498
1499 Do this before setting *type_change_ok or *size_change_ok so that
1500 we warn properly when dynamic library symbols are overridden. */
1501
1502 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1503 newweak = FALSE;
1504 if (olddef && newdyn)
1505 oldweak = FALSE;
1506
1507 /* Allow changes between different types of function symbol. */
1508 if (newfunc && oldfunc)
1509 *type_change_ok = TRUE;
1510
1511 /* It's OK to change the type if either the existing symbol or the
1512 new symbol is weak. A type change is also OK if the old symbol
1513 is undefined and the new symbol is defined. */
1514
1515 if (oldweak
1516 || newweak
1517 || (newdef
1518 && h->root.type == bfd_link_hash_undefined))
1519 *type_change_ok = TRUE;
1520
1521 /* It's OK to change the size if either the existing symbol or the
1522 new symbol is weak, or if the old symbol is undefined. */
1523
1524 if (*type_change_ok
1525 || h->root.type == bfd_link_hash_undefined)
1526 *size_change_ok = TRUE;
1527
1528 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1529 symbol, respectively, appears to be a common symbol in a dynamic
1530 object. If a symbol appears in an uninitialized section, and is
1531 not weak, and is not a function, then it may be a common symbol
1532 which was resolved when the dynamic object was created. We want
1533 to treat such symbols specially, because they raise special
1534 considerations when setting the symbol size: if the symbol
1535 appears as a common symbol in a regular object, and the size in
1536 the regular object is larger, we must make sure that we use the
1537 larger size. This problematic case can always be avoided in C,
1538 but it must be handled correctly when using Fortran shared
1539 libraries.
1540
1541 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1542 likewise for OLDDYNCOMMON and OLDDEF.
1543
1544 Note that this test is just a heuristic, and that it is quite
1545 possible to have an uninitialized symbol in a shared object which
1546 is really a definition, rather than a common symbol. This could
1547 lead to some minor confusion when the symbol really is a common
1548 symbol in some regular object. However, I think it will be
1549 harmless. */
1550
1551 if (newdyn
1552 && newdef
1553 && !newweak
1554 && (sec->flags & SEC_ALLOC) != 0
1555 && (sec->flags & SEC_LOAD) == 0
1556 && sym->st_size > 0
1557 && !newfunc)
1558 newdyncommon = TRUE;
1559 else
1560 newdyncommon = FALSE;
1561
1562 if (olddyn
1563 && olddef
1564 && h->root.type == bfd_link_hash_defined
1565 && h->def_dynamic
1566 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1567 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1568 && h->size > 0
1569 && !oldfunc)
1570 olddyncommon = TRUE;
1571 else
1572 olddyncommon = FALSE;
1573
1574 /* We now know everything about the old and new symbols. We ask the
1575 backend to check if we can merge them. */
1576 if (bed->merge_symbol != NULL)
1577 {
1578 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1579 return FALSE;
1580 sec = *psec;
1581 }
1582
1583 /* There are multiple definitions of a normal symbol. Skip the
1584 default symbol as well as definition from an IR object. */
1585 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1586 && !default_sym && h->def_regular
1587 && !(oldbfd != NULL
1588 && (oldbfd->flags & BFD_PLUGIN) != 0
1589 && (abfd->flags & BFD_PLUGIN) == 0))
1590 {
1591 /* Handle a multiple definition. */
1592 (*info->callbacks->multiple_definition) (info, &h->root,
1593 abfd, sec, *pvalue);
1594 *skip = TRUE;
1595 return TRUE;
1596 }
1597
1598 /* If both the old and the new symbols look like common symbols in a
1599 dynamic object, set the size of the symbol to the larger of the
1600 two. */
1601
1602 if (olddyncommon
1603 && newdyncommon
1604 && sym->st_size != h->size)
1605 {
1606 /* Since we think we have two common symbols, issue a multiple
1607 common warning if desired. Note that we only warn if the
1608 size is different. If the size is the same, we simply let
1609 the old symbol override the new one as normally happens with
1610 symbols defined in dynamic objects. */
1611
1612 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1613 bfd_link_hash_common, sym->st_size);
1614 if (sym->st_size > h->size)
1615 h->size = sym->st_size;
1616
1617 *size_change_ok = TRUE;
1618 }
1619
1620 /* If we are looking at a dynamic object, and we have found a
1621 definition, we need to see if the symbol was already defined by
1622 some other object. If so, we want to use the existing
1623 definition, and we do not want to report a multiple symbol
1624 definition error; we do this by clobbering *PSEC to be
1625 bfd_und_section_ptr.
1626
1627 We treat a common symbol as a definition if the symbol in the
1628 shared library is a function, since common symbols always
1629 represent variables; this can cause confusion in principle, but
1630 any such confusion would seem to indicate an erroneous program or
1631 shared library. We also permit a common symbol in a regular
1632 object to override a weak symbol in a shared object. */
1633
1634 if (newdyn
1635 && newdef
1636 && (olddef
1637 || (h->root.type == bfd_link_hash_common
1638 && (newweak || newfunc))))
1639 {
1640 *override = TRUE;
1641 newdef = FALSE;
1642 newdyncommon = FALSE;
1643
1644 *psec = sec = bfd_und_section_ptr;
1645 *size_change_ok = TRUE;
1646
1647 /* If we get here when the old symbol is a common symbol, then
1648 we are explicitly letting it override a weak symbol or
1649 function in a dynamic object, and we don't want to warn about
1650 a type change. If the old symbol is a defined symbol, a type
1651 change warning may still be appropriate. */
1652
1653 if (h->root.type == bfd_link_hash_common)
1654 *type_change_ok = TRUE;
1655 }
1656
1657 /* Handle the special case of an old common symbol merging with a
1658 new symbol which looks like a common symbol in a shared object.
1659 We change *PSEC and *PVALUE to make the new symbol look like a
1660 common symbol, and let _bfd_generic_link_add_one_symbol do the
1661 right thing. */
1662
1663 if (newdyncommon
1664 && h->root.type == bfd_link_hash_common)
1665 {
1666 *override = TRUE;
1667 newdef = FALSE;
1668 newdyncommon = FALSE;
1669 *pvalue = sym->st_size;
1670 *psec = sec = bed->common_section (oldsec);
1671 *size_change_ok = TRUE;
1672 }
1673
1674 /* Skip weak definitions of symbols that are already defined. */
1675 if (newdef && olddef && newweak)
1676 {
1677 /* Don't skip new non-IR weak syms. */
1678 if (!(oldbfd != NULL
1679 && (oldbfd->flags & BFD_PLUGIN) != 0
1680 && (abfd->flags & BFD_PLUGIN) == 0))
1681 {
1682 newdef = FALSE;
1683 *skip = TRUE;
1684 }
1685
1686 /* Merge st_other. If the symbol already has a dynamic index,
1687 but visibility says it should not be visible, turn it into a
1688 local symbol. */
1689 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1690 if (h->dynindx != -1)
1691 switch (ELF_ST_VISIBILITY (h->other))
1692 {
1693 case STV_INTERNAL:
1694 case STV_HIDDEN:
1695 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1696 break;
1697 }
1698 }
1699
1700 /* If the old symbol is from a dynamic object, and the new symbol is
1701 a definition which is not from a dynamic object, then the new
1702 symbol overrides the old symbol. Symbols from regular files
1703 always take precedence over symbols from dynamic objects, even if
1704 they are defined after the dynamic object in the link.
1705
1706 As above, we again permit a common symbol in a regular object to
1707 override a definition in a shared object if the shared object
1708 symbol is a function or is weak. */
1709
1710 flip = NULL;
1711 if (!newdyn
1712 && (newdef
1713 || (bfd_is_com_section (sec)
1714 && (oldweak || oldfunc)))
1715 && olddyn
1716 && olddef
1717 && h->def_dynamic)
1718 {
1719 /* Change the hash table entry to undefined, and let
1720 _bfd_generic_link_add_one_symbol do the right thing with the
1721 new definition. */
1722
1723 h->root.type = bfd_link_hash_undefined;
1724 h->root.u.undef.abfd = h->root.u.def.section->owner;
1725 *size_change_ok = TRUE;
1726
1727 olddef = FALSE;
1728 olddyncommon = FALSE;
1729
1730 /* We again permit a type change when a common symbol may be
1731 overriding a function. */
1732
1733 if (bfd_is_com_section (sec))
1734 {
1735 if (oldfunc)
1736 {
1737 /* If a common symbol overrides a function, make sure
1738 that it isn't defined dynamically nor has type
1739 function. */
1740 h->def_dynamic = 0;
1741 h->type = STT_NOTYPE;
1742 }
1743 *type_change_ok = TRUE;
1744 }
1745
1746 if (hi->root.type == bfd_link_hash_indirect)
1747 flip = hi;
1748 else
1749 /* This union may have been set to be non-NULL when this symbol
1750 was seen in a dynamic object. We must force the union to be
1751 NULL, so that it is correct for a regular symbol. */
1752 h->verinfo.vertree = NULL;
1753 }
1754
1755 /* Handle the special case of a new common symbol merging with an
1756 old symbol that looks like it might be a common symbol defined in
1757 a shared object. Note that we have already handled the case in
1758 which a new common symbol should simply override the definition
1759 in the shared library. */
1760
1761 if (! newdyn
1762 && bfd_is_com_section (sec)
1763 && olddyncommon)
1764 {
1765 /* It would be best if we could set the hash table entry to a
1766 common symbol, but we don't know what to use for the section
1767 or the alignment. */
1768 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1769 bfd_link_hash_common, sym->st_size);
1770
1771 /* If the presumed common symbol in the dynamic object is
1772 larger, pretend that the new symbol has its size. */
1773
1774 if (h->size > *pvalue)
1775 *pvalue = h->size;
1776
1777 /* We need to remember the alignment required by the symbol
1778 in the dynamic object. */
1779 BFD_ASSERT (pold_alignment);
1780 *pold_alignment = h->root.u.def.section->alignment_power;
1781
1782 olddef = FALSE;
1783 olddyncommon = FALSE;
1784
1785 h->root.type = bfd_link_hash_undefined;
1786 h->root.u.undef.abfd = h->root.u.def.section->owner;
1787
1788 *size_change_ok = TRUE;
1789 *type_change_ok = TRUE;
1790
1791 if (hi->root.type == bfd_link_hash_indirect)
1792 flip = hi;
1793 else
1794 h->verinfo.vertree = NULL;
1795 }
1796
1797 if (flip != NULL)
1798 {
1799 /* Handle the case where we had a versioned symbol in a dynamic
1800 library and now find a definition in a normal object. In this
1801 case, we make the versioned symbol point to the normal one. */
1802 flip->root.type = h->root.type;
1803 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1804 h->root.type = bfd_link_hash_indirect;
1805 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1806 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1807 if (h->def_dynamic)
1808 {
1809 h->def_dynamic = 0;
1810 flip->ref_dynamic = 1;
1811 }
1812 }
1813
1814 return TRUE;
1815 }
1816
1817 /* This function is called to create an indirect symbol from the
1818 default for the symbol with the default version if needed. The
1819 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1820 set DYNSYM if the new indirect symbol is dynamic. */
1821
1822 static bfd_boolean
1823 _bfd_elf_add_default_symbol (bfd *abfd,
1824 struct bfd_link_info *info,
1825 struct elf_link_hash_entry *h,
1826 const char *name,
1827 Elf_Internal_Sym *sym,
1828 asection *sec,
1829 bfd_vma value,
1830 bfd **poldbfd,
1831 bfd_boolean *dynsym)
1832 {
1833 bfd_boolean type_change_ok;
1834 bfd_boolean size_change_ok;
1835 bfd_boolean skip;
1836 char *shortname;
1837 struct elf_link_hash_entry *hi;
1838 struct bfd_link_hash_entry *bh;
1839 const struct elf_backend_data *bed;
1840 bfd_boolean collect;
1841 bfd_boolean dynamic;
1842 bfd_boolean override;
1843 char *p;
1844 size_t len, shortlen;
1845 asection *tmp_sec;
1846 bfd_boolean matched;
1847
1848 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1849 return TRUE;
1850
1851 /* If this symbol has a version, and it is the default version, we
1852 create an indirect symbol from the default name to the fully
1853 decorated name. This will cause external references which do not
1854 specify a version to be bound to this version of the symbol. */
1855 p = strchr (name, ELF_VER_CHR);
1856 if (h->versioned == unknown)
1857 {
1858 if (p == NULL)
1859 {
1860 h->versioned = unversioned;
1861 return TRUE;
1862 }
1863 else
1864 {
1865 if (p[1] != ELF_VER_CHR)
1866 {
1867 h->versioned = versioned_hidden;
1868 return TRUE;
1869 }
1870 else
1871 h->versioned = versioned;
1872 }
1873 }
1874 else
1875 {
1876 /* PR ld/19073: We may see an unversioned definition after the
1877 default version. */
1878 if (p == NULL)
1879 return TRUE;
1880 }
1881
1882 bed = get_elf_backend_data (abfd);
1883 collect = bed->collect;
1884 dynamic = (abfd->flags & DYNAMIC) != 0;
1885
1886 shortlen = p - name;
1887 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1888 if (shortname == NULL)
1889 return FALSE;
1890 memcpy (shortname, name, shortlen);
1891 shortname[shortlen] = '\0';
1892
1893 /* We are going to create a new symbol. Merge it with any existing
1894 symbol with this name. For the purposes of the merge, act as
1895 though we were defining the symbol we just defined, although we
1896 actually going to define an indirect symbol. */
1897 type_change_ok = FALSE;
1898 size_change_ok = FALSE;
1899 matched = TRUE;
1900 tmp_sec = sec;
1901 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1902 &hi, poldbfd, NULL, NULL, &skip, &override,
1903 &type_change_ok, &size_change_ok, &matched))
1904 return FALSE;
1905
1906 if (skip)
1907 goto nondefault;
1908
1909 if (hi->def_regular)
1910 {
1911 /* If the undecorated symbol will have a version added by a
1912 script different to H, then don't indirect to/from the
1913 undecorated symbol. This isn't ideal because we may not yet
1914 have seen symbol versions, if given by a script on the
1915 command line rather than via --version-script. */
1916 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1917 {
1918 bfd_boolean hide;
1919
1920 hi->verinfo.vertree
1921 = bfd_find_version_for_sym (info->version_info,
1922 hi->root.root.string, &hide);
1923 if (hi->verinfo.vertree != NULL && hide)
1924 {
1925 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1926 goto nondefault;
1927 }
1928 }
1929 if (hi->verinfo.vertree != NULL
1930 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1931 goto nondefault;
1932 }
1933
1934 if (! override)
1935 {
1936 /* Add the default symbol if not performing a relocatable link. */
1937 if (! bfd_link_relocatable (info))
1938 {
1939 bh = &hi->root;
1940 if (bh->type == bfd_link_hash_defined
1941 && bh->u.def.section->owner != NULL
1942 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1943 {
1944 /* Mark the previous definition from IR object as
1945 undefined so that the generic linker will override
1946 it. */
1947 bh->type = bfd_link_hash_undefined;
1948 bh->u.undef.abfd = bh->u.def.section->owner;
1949 }
1950 if (! (_bfd_generic_link_add_one_symbol
1951 (info, abfd, shortname, BSF_INDIRECT,
1952 bfd_ind_section_ptr,
1953 0, name, FALSE, collect, &bh)))
1954 return FALSE;
1955 hi = (struct elf_link_hash_entry *) bh;
1956 }
1957 }
1958 else
1959 {
1960 /* In this case the symbol named SHORTNAME is overriding the
1961 indirect symbol we want to add. We were planning on making
1962 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1963 is the name without a version. NAME is the fully versioned
1964 name, and it is the default version.
1965
1966 Overriding means that we already saw a definition for the
1967 symbol SHORTNAME in a regular object, and it is overriding
1968 the symbol defined in the dynamic object.
1969
1970 When this happens, we actually want to change NAME, the
1971 symbol we just added, to refer to SHORTNAME. This will cause
1972 references to NAME in the shared object to become references
1973 to SHORTNAME in the regular object. This is what we expect
1974 when we override a function in a shared object: that the
1975 references in the shared object will be mapped to the
1976 definition in the regular object. */
1977
1978 while (hi->root.type == bfd_link_hash_indirect
1979 || hi->root.type == bfd_link_hash_warning)
1980 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1981
1982 h->root.type = bfd_link_hash_indirect;
1983 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1984 if (h->def_dynamic)
1985 {
1986 h->def_dynamic = 0;
1987 hi->ref_dynamic = 1;
1988 if (hi->ref_regular
1989 || hi->def_regular)
1990 {
1991 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1992 return FALSE;
1993 }
1994 }
1995
1996 /* Now set HI to H, so that the following code will set the
1997 other fields correctly. */
1998 hi = h;
1999 }
2000
2001 /* Check if HI is a warning symbol. */
2002 if (hi->root.type == bfd_link_hash_warning)
2003 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2004
2005 /* If there is a duplicate definition somewhere, then HI may not
2006 point to an indirect symbol. We will have reported an error to
2007 the user in that case. */
2008
2009 if (hi->root.type == bfd_link_hash_indirect)
2010 {
2011 struct elf_link_hash_entry *ht;
2012
2013 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2014 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2015
2016 /* A reference to the SHORTNAME symbol from a dynamic library
2017 will be satisfied by the versioned symbol at runtime. In
2018 effect, we have a reference to the versioned symbol. */
2019 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2020 hi->dynamic_def |= ht->dynamic_def;
2021
2022 /* See if the new flags lead us to realize that the symbol must
2023 be dynamic. */
2024 if (! *dynsym)
2025 {
2026 if (! dynamic)
2027 {
2028 if (! bfd_link_executable (info)
2029 || hi->def_dynamic
2030 || hi->ref_dynamic)
2031 *dynsym = TRUE;
2032 }
2033 else
2034 {
2035 if (hi->ref_regular)
2036 *dynsym = TRUE;
2037 }
2038 }
2039 }
2040
2041 /* We also need to define an indirection from the nondefault version
2042 of the symbol. */
2043
2044 nondefault:
2045 len = strlen (name);
2046 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2047 if (shortname == NULL)
2048 return FALSE;
2049 memcpy (shortname, name, shortlen);
2050 memcpy (shortname + shortlen, p + 1, len - shortlen);
2051
2052 /* Once again, merge with any existing symbol. */
2053 type_change_ok = FALSE;
2054 size_change_ok = FALSE;
2055 tmp_sec = sec;
2056 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2057 &hi, poldbfd, NULL, NULL, &skip, &override,
2058 &type_change_ok, &size_change_ok, &matched))
2059 return FALSE;
2060
2061 if (skip)
2062 return TRUE;
2063
2064 if (override)
2065 {
2066 /* Here SHORTNAME is a versioned name, so we don't expect to see
2067 the type of override we do in the case above unless it is
2068 overridden by a versioned definition. */
2069 if (hi->root.type != bfd_link_hash_defined
2070 && hi->root.type != bfd_link_hash_defweak)
2071 _bfd_error_handler
2072 /* xgettext:c-format */
2073 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2074 abfd, shortname);
2075 }
2076 else
2077 {
2078 bh = &hi->root;
2079 if (! (_bfd_generic_link_add_one_symbol
2080 (info, abfd, shortname, BSF_INDIRECT,
2081 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2082 return FALSE;
2083 hi = (struct elf_link_hash_entry *) bh;
2084
2085 /* If there is a duplicate definition somewhere, then HI may not
2086 point to an indirect symbol. We will have reported an error
2087 to the user in that case. */
2088
2089 if (hi->root.type == bfd_link_hash_indirect)
2090 {
2091 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2092 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2093 hi->dynamic_def |= h->dynamic_def;
2094
2095 /* See if the new flags lead us to realize that the symbol
2096 must be dynamic. */
2097 if (! *dynsym)
2098 {
2099 if (! dynamic)
2100 {
2101 if (! bfd_link_executable (info)
2102 || hi->ref_dynamic)
2103 *dynsym = TRUE;
2104 }
2105 else
2106 {
2107 if (hi->ref_regular)
2108 *dynsym = TRUE;
2109 }
2110 }
2111 }
2112 }
2113
2114 return TRUE;
2115 }
2116 \f
2117 /* This routine is used to export all defined symbols into the dynamic
2118 symbol table. It is called via elf_link_hash_traverse. */
2119
2120 static bfd_boolean
2121 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2122 {
2123 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2124
2125 /* Ignore indirect symbols. These are added by the versioning code. */
2126 if (h->root.type == bfd_link_hash_indirect)
2127 return TRUE;
2128
2129 /* Ignore this if we won't export it. */
2130 if (!eif->info->export_dynamic && !h->dynamic)
2131 return TRUE;
2132
2133 if (h->dynindx == -1
2134 && (h->def_regular || h->ref_regular)
2135 && ! bfd_hide_sym_by_version (eif->info->version_info,
2136 h->root.root.string))
2137 {
2138 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2139 {
2140 eif->failed = TRUE;
2141 return FALSE;
2142 }
2143 }
2144
2145 return TRUE;
2146 }
2147 \f
2148 /* Look through the symbols which are defined in other shared
2149 libraries and referenced here. Update the list of version
2150 dependencies. This will be put into the .gnu.version_r section.
2151 This function is called via elf_link_hash_traverse. */
2152
2153 static bfd_boolean
2154 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2155 void *data)
2156 {
2157 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2158 Elf_Internal_Verneed *t;
2159 Elf_Internal_Vernaux *a;
2160 bfd_size_type amt;
2161
2162 /* We only care about symbols defined in shared objects with version
2163 information. */
2164 if (!h->def_dynamic
2165 || h->def_regular
2166 || h->dynindx == -1
2167 || h->verinfo.verdef == NULL
2168 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2169 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2170 return TRUE;
2171
2172 /* See if we already know about this version. */
2173 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2174 t != NULL;
2175 t = t->vn_nextref)
2176 {
2177 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2178 continue;
2179
2180 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2181 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2182 return TRUE;
2183
2184 break;
2185 }
2186
2187 /* This is a new version. Add it to tree we are building. */
2188
2189 if (t == NULL)
2190 {
2191 amt = sizeof *t;
2192 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2193 if (t == NULL)
2194 {
2195 rinfo->failed = TRUE;
2196 return FALSE;
2197 }
2198
2199 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2200 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2201 elf_tdata (rinfo->info->output_bfd)->verref = t;
2202 }
2203
2204 amt = sizeof *a;
2205 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2206 if (a == NULL)
2207 {
2208 rinfo->failed = TRUE;
2209 return FALSE;
2210 }
2211
2212 /* Note that we are copying a string pointer here, and testing it
2213 above. If bfd_elf_string_from_elf_section is ever changed to
2214 discard the string data when low in memory, this will have to be
2215 fixed. */
2216 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2217
2218 a->vna_flags = h->verinfo.verdef->vd_flags;
2219 a->vna_nextptr = t->vn_auxptr;
2220
2221 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2222 ++rinfo->vers;
2223
2224 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2225
2226 t->vn_auxptr = a;
2227
2228 return TRUE;
2229 }
2230
2231 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2232 hidden. Set *T_P to NULL if there is no match. */
2233
2234 static bfd_boolean
2235 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2236 struct elf_link_hash_entry *h,
2237 const char *version_p,
2238 struct bfd_elf_version_tree **t_p,
2239 bfd_boolean *hide)
2240 {
2241 struct bfd_elf_version_tree *t;
2242
2243 /* Look for the version. If we find it, it is no longer weak. */
2244 for (t = info->version_info; t != NULL; t = t->next)
2245 {
2246 if (strcmp (t->name, version_p) == 0)
2247 {
2248 size_t len;
2249 char *alc;
2250 struct bfd_elf_version_expr *d;
2251
2252 len = version_p - h->root.root.string;
2253 alc = (char *) bfd_malloc (len);
2254 if (alc == NULL)
2255 return FALSE;
2256 memcpy (alc, h->root.root.string, len - 1);
2257 alc[len - 1] = '\0';
2258 if (alc[len - 2] == ELF_VER_CHR)
2259 alc[len - 2] = '\0';
2260
2261 h->verinfo.vertree = t;
2262 t->used = TRUE;
2263 d = NULL;
2264
2265 if (t->globals.list != NULL)
2266 d = (*t->match) (&t->globals, NULL, alc);
2267
2268 /* See if there is anything to force this symbol to
2269 local scope. */
2270 if (d == NULL && t->locals.list != NULL)
2271 {
2272 d = (*t->match) (&t->locals, NULL, alc);
2273 if (d != NULL
2274 && h->dynindx != -1
2275 && ! info->export_dynamic)
2276 *hide = TRUE;
2277 }
2278
2279 free (alc);
2280 break;
2281 }
2282 }
2283
2284 *t_p = t;
2285
2286 return TRUE;
2287 }
2288
2289 /* Return TRUE if the symbol H is hidden by version script. */
2290
2291 bfd_boolean
2292 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h)
2294 {
2295 const char *p;
2296 bfd_boolean hide = FALSE;
2297 const struct elf_backend_data *bed
2298 = get_elf_backend_data (info->output_bfd);
2299
2300 /* Version script only hides symbols defined in regular objects. */
2301 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2302 return TRUE;
2303
2304 p = strchr (h->root.root.string, ELF_VER_CHR);
2305 if (p != NULL && h->verinfo.vertree == NULL)
2306 {
2307 struct bfd_elf_version_tree *t;
2308
2309 ++p;
2310 if (*p == ELF_VER_CHR)
2311 ++p;
2312
2313 if (*p != '\0'
2314 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2315 && hide)
2316 {
2317 if (hide)
2318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2319 return TRUE;
2320 }
2321 }
2322
2323 /* If we don't have a version for this symbol, see if we can find
2324 something. */
2325 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2326 {
2327 h->verinfo.vertree
2328 = bfd_find_version_for_sym (info->version_info,
2329 h->root.root.string, &hide);
2330 if (h->verinfo.vertree != NULL && hide)
2331 {
2332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2333 return TRUE;
2334 }
2335 }
2336
2337 return FALSE;
2338 }
2339
2340 /* Figure out appropriate versions for all the symbols. We may not
2341 have the version number script until we have read all of the input
2342 files, so until that point we don't know which symbols should be
2343 local. This function is called via elf_link_hash_traverse. */
2344
2345 static bfd_boolean
2346 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2347 {
2348 struct elf_info_failed *sinfo;
2349 struct bfd_link_info *info;
2350 const struct elf_backend_data *bed;
2351 struct elf_info_failed eif;
2352 char *p;
2353 bfd_boolean hide;
2354
2355 sinfo = (struct elf_info_failed *) data;
2356 info = sinfo->info;
2357
2358 /* Fix the symbol flags. */
2359 eif.failed = FALSE;
2360 eif.info = info;
2361 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2362 {
2363 if (eif.failed)
2364 sinfo->failed = TRUE;
2365 return FALSE;
2366 }
2367
2368 bed = get_elf_backend_data (info->output_bfd);
2369
2370 /* We only need version numbers for symbols defined in regular
2371 objects. */
2372 if (!h->def_regular)
2373 {
2374 /* Hide symbols defined in discarded input sections. */
2375 if ((h->root.type == bfd_link_hash_defined
2376 || h->root.type == bfd_link_hash_defweak)
2377 && discarded_section (h->root.u.def.section))
2378 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2379 return TRUE;
2380 }
2381
2382 hide = FALSE;
2383 p = strchr (h->root.root.string, ELF_VER_CHR);
2384 if (p != NULL && h->verinfo.vertree == NULL)
2385 {
2386 struct bfd_elf_version_tree *t;
2387
2388 ++p;
2389 if (*p == ELF_VER_CHR)
2390 ++p;
2391
2392 /* If there is no version string, we can just return out. */
2393 if (*p == '\0')
2394 return TRUE;
2395
2396 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2397 {
2398 sinfo->failed = TRUE;
2399 return FALSE;
2400 }
2401
2402 if (hide)
2403 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2404
2405 /* If we are building an application, we need to create a
2406 version node for this version. */
2407 if (t == NULL && bfd_link_executable (info))
2408 {
2409 struct bfd_elf_version_tree **pp;
2410 int version_index;
2411
2412 /* If we aren't going to export this symbol, we don't need
2413 to worry about it. */
2414 if (h->dynindx == -1)
2415 return TRUE;
2416
2417 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2418 sizeof *t);
2419 if (t == NULL)
2420 {
2421 sinfo->failed = TRUE;
2422 return FALSE;
2423 }
2424
2425 t->name = p;
2426 t->name_indx = (unsigned int) -1;
2427 t->used = TRUE;
2428
2429 version_index = 1;
2430 /* Don't count anonymous version tag. */
2431 if (sinfo->info->version_info != NULL
2432 && sinfo->info->version_info->vernum == 0)
2433 version_index = 0;
2434 for (pp = &sinfo->info->version_info;
2435 *pp != NULL;
2436 pp = &(*pp)->next)
2437 ++version_index;
2438 t->vernum = version_index;
2439
2440 *pp = t;
2441
2442 h->verinfo.vertree = t;
2443 }
2444 else if (t == NULL)
2445 {
2446 /* We could not find the version for a symbol when
2447 generating a shared archive. Return an error. */
2448 _bfd_error_handler
2449 /* xgettext:c-format */
2450 (_("%pB: version node not found for symbol %s"),
2451 info->output_bfd, h->root.root.string);
2452 bfd_set_error (bfd_error_bad_value);
2453 sinfo->failed = TRUE;
2454 return FALSE;
2455 }
2456 }
2457
2458 /* If we don't have a version for this symbol, see if we can find
2459 something. */
2460 if (!hide
2461 && h->verinfo.vertree == NULL
2462 && sinfo->info->version_info != NULL)
2463 {
2464 h->verinfo.vertree
2465 = bfd_find_version_for_sym (sinfo->info->version_info,
2466 h->root.root.string, &hide);
2467 if (h->verinfo.vertree != NULL && hide)
2468 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2469 }
2470
2471 return TRUE;
2472 }
2473 \f
2474 /* Read and swap the relocs from the section indicated by SHDR. This
2475 may be either a REL or a RELA section. The relocations are
2476 translated into RELA relocations and stored in INTERNAL_RELOCS,
2477 which should have already been allocated to contain enough space.
2478 The EXTERNAL_RELOCS are a buffer where the external form of the
2479 relocations should be stored.
2480
2481 Returns FALSE if something goes wrong. */
2482
2483 static bfd_boolean
2484 elf_link_read_relocs_from_section (bfd *abfd,
2485 asection *sec,
2486 Elf_Internal_Shdr *shdr,
2487 void *external_relocs,
2488 Elf_Internal_Rela *internal_relocs)
2489 {
2490 const struct elf_backend_data *bed;
2491 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2492 const bfd_byte *erela;
2493 const bfd_byte *erelaend;
2494 Elf_Internal_Rela *irela;
2495 Elf_Internal_Shdr *symtab_hdr;
2496 size_t nsyms;
2497
2498 /* Position ourselves at the start of the section. */
2499 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2500 return FALSE;
2501
2502 /* Read the relocations. */
2503 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2504 return FALSE;
2505
2506 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2507 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2508
2509 bed = get_elf_backend_data (abfd);
2510
2511 /* Convert the external relocations to the internal format. */
2512 if (shdr->sh_entsize == bed->s->sizeof_rel)
2513 swap_in = bed->s->swap_reloc_in;
2514 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2515 swap_in = bed->s->swap_reloca_in;
2516 else
2517 {
2518 bfd_set_error (bfd_error_wrong_format);
2519 return FALSE;
2520 }
2521
2522 erela = (const bfd_byte *) external_relocs;
2523 /* Setting erelaend like this and comparing with <= handles case of
2524 a fuzzed object with sh_size not a multiple of sh_entsize. */
2525 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2526 irela = internal_relocs;
2527 while (erela <= erelaend)
2528 {
2529 bfd_vma r_symndx;
2530
2531 (*swap_in) (abfd, erela, irela);
2532 r_symndx = ELF32_R_SYM (irela->r_info);
2533 if (bed->s->arch_size == 64)
2534 r_symndx >>= 24;
2535 if (nsyms > 0)
2536 {
2537 if ((size_t) r_symndx >= nsyms)
2538 {
2539 _bfd_error_handler
2540 /* xgettext:c-format */
2541 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2542 " for offset %#" PRIx64 " in section `%pA'"),
2543 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2544 (uint64_t) irela->r_offset, sec);
2545 bfd_set_error (bfd_error_bad_value);
2546 return FALSE;
2547 }
2548 }
2549 else if (r_symndx != STN_UNDEF)
2550 {
2551 _bfd_error_handler
2552 /* xgettext:c-format */
2553 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2554 " for offset %#" PRIx64 " in section `%pA'"
2555 " when the object file has no symbol table"),
2556 abfd, (uint64_t) r_symndx,
2557 (uint64_t) irela->r_offset, sec);
2558 bfd_set_error (bfd_error_bad_value);
2559 return FALSE;
2560 }
2561 irela += bed->s->int_rels_per_ext_rel;
2562 erela += shdr->sh_entsize;
2563 }
2564
2565 return TRUE;
2566 }
2567
2568 /* Read and swap the relocs for a section O. They may have been
2569 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2570 not NULL, they are used as buffers to read into. They are known to
2571 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2572 the return value is allocated using either malloc or bfd_alloc,
2573 according to the KEEP_MEMORY argument. If O has two relocation
2574 sections (both REL and RELA relocations), then the REL_HDR
2575 relocations will appear first in INTERNAL_RELOCS, followed by the
2576 RELA_HDR relocations. */
2577
2578 Elf_Internal_Rela *
2579 _bfd_elf_link_read_relocs (bfd *abfd,
2580 asection *o,
2581 void *external_relocs,
2582 Elf_Internal_Rela *internal_relocs,
2583 bfd_boolean keep_memory)
2584 {
2585 void *alloc1 = NULL;
2586 Elf_Internal_Rela *alloc2 = NULL;
2587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2588 struct bfd_elf_section_data *esdo = elf_section_data (o);
2589 Elf_Internal_Rela *internal_rela_relocs;
2590
2591 if (esdo->relocs != NULL)
2592 return esdo->relocs;
2593
2594 if (o->reloc_count == 0)
2595 return NULL;
2596
2597 if (internal_relocs == NULL)
2598 {
2599 bfd_size_type size;
2600
2601 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2602 if (keep_memory)
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2604 else
2605 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2606 if (internal_relocs == NULL)
2607 goto error_return;
2608 }
2609
2610 if (external_relocs == NULL)
2611 {
2612 bfd_size_type size = 0;
2613
2614 if (esdo->rel.hdr)
2615 size += esdo->rel.hdr->sh_size;
2616 if (esdo->rela.hdr)
2617 size += esdo->rela.hdr->sh_size;
2618
2619 alloc1 = bfd_malloc (size);
2620 if (alloc1 == NULL)
2621 goto error_return;
2622 external_relocs = alloc1;
2623 }
2624
2625 internal_rela_relocs = internal_relocs;
2626 if (esdo->rel.hdr)
2627 {
2628 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2629 external_relocs,
2630 internal_relocs))
2631 goto error_return;
2632 external_relocs = (((bfd_byte *) external_relocs)
2633 + esdo->rel.hdr->sh_size);
2634 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2635 * bed->s->int_rels_per_ext_rel);
2636 }
2637
2638 if (esdo->rela.hdr
2639 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2640 external_relocs,
2641 internal_rela_relocs)))
2642 goto error_return;
2643
2644 /* Cache the results for next time, if we can. */
2645 if (keep_memory)
2646 esdo->relocs = internal_relocs;
2647
2648 if (alloc1 != NULL)
2649 free (alloc1);
2650
2651 /* Don't free alloc2, since if it was allocated we are passing it
2652 back (under the name of internal_relocs). */
2653
2654 return internal_relocs;
2655
2656 error_return:
2657 if (alloc1 != NULL)
2658 free (alloc1);
2659 if (alloc2 != NULL)
2660 {
2661 if (keep_memory)
2662 bfd_release (abfd, alloc2);
2663 else
2664 free (alloc2);
2665 }
2666 return NULL;
2667 }
2668
2669 /* Compute the size of, and allocate space for, REL_HDR which is the
2670 section header for a section containing relocations for O. */
2671
2672 static bfd_boolean
2673 _bfd_elf_link_size_reloc_section (bfd *abfd,
2674 struct bfd_elf_section_reloc_data *reldata)
2675 {
2676 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2677
2678 /* That allows us to calculate the size of the section. */
2679 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2680
2681 /* The contents field must last into write_object_contents, so we
2682 allocate it with bfd_alloc rather than malloc. Also since we
2683 cannot be sure that the contents will actually be filled in,
2684 we zero the allocated space. */
2685 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2686 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2687 return FALSE;
2688
2689 if (reldata->hashes == NULL && reldata->count)
2690 {
2691 struct elf_link_hash_entry **p;
2692
2693 p = ((struct elf_link_hash_entry **)
2694 bfd_zmalloc (reldata->count * sizeof (*p)));
2695 if (p == NULL)
2696 return FALSE;
2697
2698 reldata->hashes = p;
2699 }
2700
2701 return TRUE;
2702 }
2703
2704 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2705 originated from the section given by INPUT_REL_HDR) to the
2706 OUTPUT_BFD. */
2707
2708 bfd_boolean
2709 _bfd_elf_link_output_relocs (bfd *output_bfd,
2710 asection *input_section,
2711 Elf_Internal_Shdr *input_rel_hdr,
2712 Elf_Internal_Rela *internal_relocs,
2713 struct elf_link_hash_entry **rel_hash
2714 ATTRIBUTE_UNUSED)
2715 {
2716 Elf_Internal_Rela *irela;
2717 Elf_Internal_Rela *irelaend;
2718 bfd_byte *erel;
2719 struct bfd_elf_section_reloc_data *output_reldata;
2720 asection *output_section;
2721 const struct elf_backend_data *bed;
2722 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2723 struct bfd_elf_section_data *esdo;
2724
2725 output_section = input_section->output_section;
2726
2727 bed = get_elf_backend_data (output_bfd);
2728 esdo = elf_section_data (output_section);
2729 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2730 {
2731 output_reldata = &esdo->rel;
2732 swap_out = bed->s->swap_reloc_out;
2733 }
2734 else if (esdo->rela.hdr
2735 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2736 {
2737 output_reldata = &esdo->rela;
2738 swap_out = bed->s->swap_reloca_out;
2739 }
2740 else
2741 {
2742 _bfd_error_handler
2743 /* xgettext:c-format */
2744 (_("%pB: relocation size mismatch in %pB section %pA"),
2745 output_bfd, input_section->owner, input_section);
2746 bfd_set_error (bfd_error_wrong_format);
2747 return FALSE;
2748 }
2749
2750 erel = output_reldata->hdr->contents;
2751 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2752 irela = internal_relocs;
2753 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2754 * bed->s->int_rels_per_ext_rel);
2755 while (irela < irelaend)
2756 {
2757 (*swap_out) (output_bfd, irela, erel);
2758 irela += bed->s->int_rels_per_ext_rel;
2759 erel += input_rel_hdr->sh_entsize;
2760 }
2761
2762 /* Bump the counter, so that we know where to add the next set of
2763 relocations. */
2764 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2765
2766 return TRUE;
2767 }
2768 \f
2769 /* Make weak undefined symbols in PIE dynamic. */
2770
2771 bfd_boolean
2772 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2773 struct elf_link_hash_entry *h)
2774 {
2775 if (bfd_link_pie (info)
2776 && h->dynindx == -1
2777 && h->root.type == bfd_link_hash_undefweak)
2778 return bfd_elf_link_record_dynamic_symbol (info, h);
2779
2780 return TRUE;
2781 }
2782
2783 /* Fix up the flags for a symbol. This handles various cases which
2784 can only be fixed after all the input files are seen. This is
2785 currently called by both adjust_dynamic_symbol and
2786 assign_sym_version, which is unnecessary but perhaps more robust in
2787 the face of future changes. */
2788
2789 static bfd_boolean
2790 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2791 struct elf_info_failed *eif)
2792 {
2793 const struct elf_backend_data *bed;
2794
2795 /* If this symbol was mentioned in a non-ELF file, try to set
2796 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2797 permit a non-ELF file to correctly refer to a symbol defined in
2798 an ELF dynamic object. */
2799 if (h->non_elf)
2800 {
2801 while (h->root.type == bfd_link_hash_indirect)
2802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2803
2804 if (h->root.type != bfd_link_hash_defined
2805 && h->root.type != bfd_link_hash_defweak)
2806 {
2807 h->ref_regular = 1;
2808 h->ref_regular_nonweak = 1;
2809 }
2810 else
2811 {
2812 if (h->root.u.def.section->owner != NULL
2813 && (bfd_get_flavour (h->root.u.def.section->owner)
2814 == bfd_target_elf_flavour))
2815 {
2816 h->ref_regular = 1;
2817 h->ref_regular_nonweak = 1;
2818 }
2819 else
2820 h->def_regular = 1;
2821 }
2822
2823 if (h->dynindx == -1
2824 && (h->def_dynamic
2825 || h->ref_dynamic))
2826 {
2827 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2828 {
2829 eif->failed = TRUE;
2830 return FALSE;
2831 }
2832 }
2833 }
2834 else
2835 {
2836 /* Unfortunately, NON_ELF is only correct if the symbol
2837 was first seen in a non-ELF file. Fortunately, if the symbol
2838 was first seen in an ELF file, we're probably OK unless the
2839 symbol was defined in a non-ELF file. Catch that case here.
2840 FIXME: We're still in trouble if the symbol was first seen in
2841 a dynamic object, and then later in a non-ELF regular object. */
2842 if ((h->root.type == bfd_link_hash_defined
2843 || h->root.type == bfd_link_hash_defweak)
2844 && !h->def_regular
2845 && (h->root.u.def.section->owner != NULL
2846 ? (bfd_get_flavour (h->root.u.def.section->owner)
2847 != bfd_target_elf_flavour)
2848 : (bfd_is_abs_section (h->root.u.def.section)
2849 && !h->def_dynamic)))
2850 h->def_regular = 1;
2851 }
2852
2853 /* Backend specific symbol fixup. */
2854 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2855 if (bed->elf_backend_fixup_symbol
2856 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2857 return FALSE;
2858
2859 /* If this is a final link, and the symbol was defined as a common
2860 symbol in a regular object file, and there was no definition in
2861 any dynamic object, then the linker will have allocated space for
2862 the symbol in a common section but the DEF_REGULAR
2863 flag will not have been set. */
2864 if (h->root.type == bfd_link_hash_defined
2865 && !h->def_regular
2866 && h->ref_regular
2867 && !h->def_dynamic
2868 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2869 h->def_regular = 1;
2870
2871 /* Symbols defined in discarded sections shouldn't be dynamic. */
2872 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2873 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2874
2875 /* If a weak undefined symbol has non-default visibility, we also
2876 hide it from the dynamic linker. */
2877 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2878 && h->root.type == bfd_link_hash_undefweak)
2879 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2880
2881 /* A hidden versioned symbol in executable should be forced local if
2882 it is is locally defined, not referenced by shared library and not
2883 exported. */
2884 else if (bfd_link_executable (eif->info)
2885 && h->versioned == versioned_hidden
2886 && !eif->info->export_dynamic
2887 && !h->dynamic
2888 && !h->ref_dynamic
2889 && h->def_regular)
2890 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2891
2892 /* If -Bsymbolic was used (which means to bind references to global
2893 symbols to the definition within the shared object), and this
2894 symbol was defined in a regular object, then it actually doesn't
2895 need a PLT entry. Likewise, if the symbol has non-default
2896 visibility. If the symbol has hidden or internal visibility, we
2897 will force it local. */
2898 else if (h->needs_plt
2899 && bfd_link_pic (eif->info)
2900 && is_elf_hash_table (eif->info->hash)
2901 && (SYMBOLIC_BIND (eif->info, h)
2902 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2903 && h->def_regular)
2904 {
2905 bfd_boolean force_local;
2906
2907 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2908 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2909 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2910 }
2911
2912 /* If this is a weak defined symbol in a dynamic object, and we know
2913 the real definition in the dynamic object, copy interesting flags
2914 over to the real definition. */
2915 if (h->is_weakalias)
2916 {
2917 struct elf_link_hash_entry *def = weakdef (h);
2918
2919 /* If the real definition is defined by a regular object file,
2920 don't do anything special. See the longer description in
2921 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2922 bfd_link_hash_defined as it was when put on the alias list
2923 then it must have originally been a versioned symbol (for
2924 which a non-versioned indirect symbol is created) and later
2925 a definition for the non-versioned symbol is found. In that
2926 case the indirection is flipped with the versioned symbol
2927 becoming an indirect pointing at the non-versioned symbol.
2928 Thus, not an alias any more. */
2929 if (def->def_regular
2930 || def->root.type != bfd_link_hash_defined)
2931 {
2932 h = def;
2933 while ((h = h->u.alias) != def)
2934 h->is_weakalias = 0;
2935 }
2936 else
2937 {
2938 while (h->root.type == bfd_link_hash_indirect)
2939 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2940 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2941 || h->root.type == bfd_link_hash_defweak);
2942 BFD_ASSERT (def->def_dynamic);
2943 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2944 }
2945 }
2946
2947 return TRUE;
2948 }
2949
2950 /* Make the backend pick a good value for a dynamic symbol. This is
2951 called via elf_link_hash_traverse, and also calls itself
2952 recursively. */
2953
2954 static bfd_boolean
2955 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2956 {
2957 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2958 struct elf_link_hash_table *htab;
2959 const struct elf_backend_data *bed;
2960
2961 if (! is_elf_hash_table (eif->info->hash))
2962 return FALSE;
2963
2964 /* Ignore indirect symbols. These are added by the versioning code. */
2965 if (h->root.type == bfd_link_hash_indirect)
2966 return TRUE;
2967
2968 /* Fix the symbol flags. */
2969 if (! _bfd_elf_fix_symbol_flags (h, eif))
2970 return FALSE;
2971
2972 htab = elf_hash_table (eif->info);
2973 bed = get_elf_backend_data (htab->dynobj);
2974
2975 if (h->root.type == bfd_link_hash_undefweak)
2976 {
2977 if (eif->info->dynamic_undefined_weak == 0)
2978 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2979 else if (eif->info->dynamic_undefined_weak > 0
2980 && h->ref_regular
2981 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2982 && !bfd_hide_sym_by_version (eif->info->version_info,
2983 h->root.root.string))
2984 {
2985 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2986 {
2987 eif->failed = TRUE;
2988 return FALSE;
2989 }
2990 }
2991 }
2992
2993 /* If this symbol does not require a PLT entry, and it is not
2994 defined by a dynamic object, or is not referenced by a regular
2995 object, ignore it. We do have to handle a weak defined symbol,
2996 even if no regular object refers to it, if we decided to add it
2997 to the dynamic symbol table. FIXME: Do we normally need to worry
2998 about symbols which are defined by one dynamic object and
2999 referenced by another one? */
3000 if (!h->needs_plt
3001 && h->type != STT_GNU_IFUNC
3002 && (h->def_regular
3003 || !h->def_dynamic
3004 || (!h->ref_regular
3005 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3006 {
3007 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3008 return TRUE;
3009 }
3010
3011 /* If we've already adjusted this symbol, don't do it again. This
3012 can happen via a recursive call. */
3013 if (h->dynamic_adjusted)
3014 return TRUE;
3015
3016 /* Don't look at this symbol again. Note that we must set this
3017 after checking the above conditions, because we may look at a
3018 symbol once, decide not to do anything, and then get called
3019 recursively later after REF_REGULAR is set below. */
3020 h->dynamic_adjusted = 1;
3021
3022 /* If this is a weak definition, and we know a real definition, and
3023 the real symbol is not itself defined by a regular object file,
3024 then get a good value for the real definition. We handle the
3025 real symbol first, for the convenience of the backend routine.
3026
3027 Note that there is a confusing case here. If the real definition
3028 is defined by a regular object file, we don't get the real symbol
3029 from the dynamic object, but we do get the weak symbol. If the
3030 processor backend uses a COPY reloc, then if some routine in the
3031 dynamic object changes the real symbol, we will not see that
3032 change in the corresponding weak symbol. This is the way other
3033 ELF linkers work as well, and seems to be a result of the shared
3034 library model.
3035
3036 I will clarify this issue. Most SVR4 shared libraries define the
3037 variable _timezone and define timezone as a weak synonym. The
3038 tzset call changes _timezone. If you write
3039 extern int timezone;
3040 int _timezone = 5;
3041 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3042 you might expect that, since timezone is a synonym for _timezone,
3043 the same number will print both times. However, if the processor
3044 backend uses a COPY reloc, then actually timezone will be copied
3045 into your process image, and, since you define _timezone
3046 yourself, _timezone will not. Thus timezone and _timezone will
3047 wind up at different memory locations. The tzset call will set
3048 _timezone, leaving timezone unchanged. */
3049
3050 if (h->is_weakalias)
3051 {
3052 struct elf_link_hash_entry *def = weakdef (h);
3053
3054 /* If we get to this point, there is an implicit reference to
3055 the alias by a regular object file via the weak symbol H. */
3056 def->ref_regular = 1;
3057
3058 /* Ensure that the backend adjust_dynamic_symbol function sees
3059 the strong alias before H by recursively calling ourselves. */
3060 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3061 return FALSE;
3062 }
3063
3064 /* If a symbol has no type and no size and does not require a PLT
3065 entry, then we are probably about to do the wrong thing here: we
3066 are probably going to create a COPY reloc for an empty object.
3067 This case can arise when a shared object is built with assembly
3068 code, and the assembly code fails to set the symbol type. */
3069 if (h->size == 0
3070 && h->type == STT_NOTYPE
3071 && !h->needs_plt)
3072 _bfd_error_handler
3073 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3074 h->root.root.string);
3075
3076 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3077 {
3078 eif->failed = TRUE;
3079 return FALSE;
3080 }
3081
3082 return TRUE;
3083 }
3084
3085 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3086 DYNBSS. */
3087
3088 bfd_boolean
3089 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3090 struct elf_link_hash_entry *h,
3091 asection *dynbss)
3092 {
3093 unsigned int power_of_two;
3094 bfd_vma mask;
3095 asection *sec = h->root.u.def.section;
3096
3097 /* The section alignment of the definition is the maximum alignment
3098 requirement of symbols defined in the section. Since we don't
3099 know the symbol alignment requirement, we start with the
3100 maximum alignment and check low bits of the symbol address
3101 for the minimum alignment. */
3102 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3103 mask = ((bfd_vma) 1 << power_of_two) - 1;
3104 while ((h->root.u.def.value & mask) != 0)
3105 {
3106 mask >>= 1;
3107 --power_of_two;
3108 }
3109
3110 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3111 dynbss))
3112 {
3113 /* Adjust the section alignment if needed. */
3114 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3115 power_of_two))
3116 return FALSE;
3117 }
3118
3119 /* We make sure that the symbol will be aligned properly. */
3120 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3121
3122 /* Define the symbol as being at this point in DYNBSS. */
3123 h->root.u.def.section = dynbss;
3124 h->root.u.def.value = dynbss->size;
3125
3126 /* Increment the size of DYNBSS to make room for the symbol. */
3127 dynbss->size += h->size;
3128
3129 /* No error if extern_protected_data is true. */
3130 if (h->protected_def
3131 && (!info->extern_protected_data
3132 || (info->extern_protected_data < 0
3133 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3134 info->callbacks->einfo
3135 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3136 h->root.root.string);
3137
3138 return TRUE;
3139 }
3140
3141 /* Adjust all external symbols pointing into SEC_MERGE sections
3142 to reflect the object merging within the sections. */
3143
3144 static bfd_boolean
3145 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3146 {
3147 asection *sec;
3148
3149 if ((h->root.type == bfd_link_hash_defined
3150 || h->root.type == bfd_link_hash_defweak)
3151 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3152 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3153 {
3154 bfd *output_bfd = (bfd *) data;
3155
3156 h->root.u.def.value =
3157 _bfd_merged_section_offset (output_bfd,
3158 &h->root.u.def.section,
3159 elf_section_data (sec)->sec_info,
3160 h->root.u.def.value);
3161 }
3162
3163 return TRUE;
3164 }
3165
3166 /* Returns false if the symbol referred to by H should be considered
3167 to resolve local to the current module, and true if it should be
3168 considered to bind dynamically. */
3169
3170 bfd_boolean
3171 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3172 struct bfd_link_info *info,
3173 bfd_boolean not_local_protected)
3174 {
3175 bfd_boolean binding_stays_local_p;
3176 const struct elf_backend_data *bed;
3177 struct elf_link_hash_table *hash_table;
3178
3179 if (h == NULL)
3180 return FALSE;
3181
3182 while (h->root.type == bfd_link_hash_indirect
3183 || h->root.type == bfd_link_hash_warning)
3184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3185
3186 /* If it was forced local, then clearly it's not dynamic. */
3187 if (h->dynindx == -1)
3188 return FALSE;
3189 if (h->forced_local)
3190 return FALSE;
3191
3192 /* Identify the cases where name binding rules say that a
3193 visible symbol resolves locally. */
3194 binding_stays_local_p = (bfd_link_executable (info)
3195 || SYMBOLIC_BIND (info, h));
3196
3197 switch (ELF_ST_VISIBILITY (h->other))
3198 {
3199 case STV_INTERNAL:
3200 case STV_HIDDEN:
3201 return FALSE;
3202
3203 case STV_PROTECTED:
3204 hash_table = elf_hash_table (info);
3205 if (!is_elf_hash_table (hash_table))
3206 return FALSE;
3207
3208 bed = get_elf_backend_data (hash_table->dynobj);
3209
3210 /* Proper resolution for function pointer equality may require
3211 that these symbols perhaps be resolved dynamically, even though
3212 we should be resolving them to the current module. */
3213 if (!not_local_protected || !bed->is_function_type (h->type))
3214 binding_stays_local_p = TRUE;
3215 break;
3216
3217 default:
3218 break;
3219 }
3220
3221 /* If it isn't defined locally, then clearly it's dynamic. */
3222 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3223 return TRUE;
3224
3225 /* Otherwise, the symbol is dynamic if binding rules don't tell
3226 us that it remains local. */
3227 return !binding_stays_local_p;
3228 }
3229
3230 /* Return true if the symbol referred to by H should be considered
3231 to resolve local to the current module, and false otherwise. Differs
3232 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3233 undefined symbols. The two functions are virtually identical except
3234 for the place where dynindx == -1 is tested. If that test is true,
3235 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3236 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3237 defined symbols.
3238 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3239 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3240 treatment of undefined weak symbols. For those that do not make
3241 undefined weak symbols dynamic, both functions may return false. */
3242
3243 bfd_boolean
3244 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3245 struct bfd_link_info *info,
3246 bfd_boolean local_protected)
3247 {
3248 const struct elf_backend_data *bed;
3249 struct elf_link_hash_table *hash_table;
3250
3251 /* If it's a local sym, of course we resolve locally. */
3252 if (h == NULL)
3253 return TRUE;
3254
3255 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3256 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3257 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3258 return TRUE;
3259
3260 /* Forced local symbols resolve locally. */
3261 if (h->forced_local)
3262 return TRUE;
3263
3264 /* Common symbols that become definitions don't get the DEF_REGULAR
3265 flag set, so test it first, and don't bail out. */
3266 if (ELF_COMMON_DEF_P (h))
3267 /* Do nothing. */;
3268 /* If we don't have a definition in a regular file, then we can't
3269 resolve locally. The sym is either undefined or dynamic. */
3270 else if (!h->def_regular)
3271 return FALSE;
3272
3273 /* Non-dynamic symbols resolve locally. */
3274 if (h->dynindx == -1)
3275 return TRUE;
3276
3277 /* At this point, we know the symbol is defined and dynamic. In an
3278 executable it must resolve locally, likewise when building symbolic
3279 shared libraries. */
3280 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3281 return TRUE;
3282
3283 /* Now deal with defined dynamic symbols in shared libraries. Ones
3284 with default visibility might not resolve locally. */
3285 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3286 return FALSE;
3287
3288 hash_table = elf_hash_table (info);
3289 if (!is_elf_hash_table (hash_table))
3290 return TRUE;
3291
3292 bed = get_elf_backend_data (hash_table->dynobj);
3293
3294 /* If extern_protected_data is false, STV_PROTECTED non-function
3295 symbols are local. */
3296 if ((!info->extern_protected_data
3297 || (info->extern_protected_data < 0
3298 && !bed->extern_protected_data))
3299 && !bed->is_function_type (h->type))
3300 return TRUE;
3301
3302 /* Function pointer equality tests may require that STV_PROTECTED
3303 symbols be treated as dynamic symbols. If the address of a
3304 function not defined in an executable is set to that function's
3305 plt entry in the executable, then the address of the function in
3306 a shared library must also be the plt entry in the executable. */
3307 return local_protected;
3308 }
3309
3310 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3311 aligned. Returns the first TLS output section. */
3312
3313 struct bfd_section *
3314 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3315 {
3316 struct bfd_section *sec, *tls;
3317 unsigned int align = 0;
3318
3319 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3320 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3321 break;
3322 tls = sec;
3323
3324 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3325 if (sec->alignment_power > align)
3326 align = sec->alignment_power;
3327
3328 elf_hash_table (info)->tls_sec = tls;
3329
3330 /* Ensure the alignment of the first section is the largest alignment,
3331 so that the tls segment starts aligned. */
3332 if (tls != NULL)
3333 tls->alignment_power = align;
3334
3335 return tls;
3336 }
3337
3338 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3339 static bfd_boolean
3340 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3341 Elf_Internal_Sym *sym)
3342 {
3343 const struct elf_backend_data *bed;
3344
3345 /* Local symbols do not count, but target specific ones might. */
3346 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3347 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3348 return FALSE;
3349
3350 bed = get_elf_backend_data (abfd);
3351 /* Function symbols do not count. */
3352 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3353 return FALSE;
3354
3355 /* If the section is undefined, then so is the symbol. */
3356 if (sym->st_shndx == SHN_UNDEF)
3357 return FALSE;
3358
3359 /* If the symbol is defined in the common section, then
3360 it is a common definition and so does not count. */
3361 if (bed->common_definition (sym))
3362 return FALSE;
3363
3364 /* If the symbol is in a target specific section then we
3365 must rely upon the backend to tell us what it is. */
3366 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3367 /* FIXME - this function is not coded yet:
3368
3369 return _bfd_is_global_symbol_definition (abfd, sym);
3370
3371 Instead for now assume that the definition is not global,
3372 Even if this is wrong, at least the linker will behave
3373 in the same way that it used to do. */
3374 return FALSE;
3375
3376 return TRUE;
3377 }
3378
3379 /* Search the symbol table of the archive element of the archive ABFD
3380 whose archive map contains a mention of SYMDEF, and determine if
3381 the symbol is defined in this element. */
3382 static bfd_boolean
3383 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3384 {
3385 Elf_Internal_Shdr * hdr;
3386 size_t symcount;
3387 size_t extsymcount;
3388 size_t extsymoff;
3389 Elf_Internal_Sym *isymbuf;
3390 Elf_Internal_Sym *isym;
3391 Elf_Internal_Sym *isymend;
3392 bfd_boolean result;
3393
3394 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3395 if (abfd == NULL)
3396 return FALSE;
3397
3398 if (! bfd_check_format (abfd, bfd_object))
3399 return FALSE;
3400
3401 /* Select the appropriate symbol table. If we don't know if the
3402 object file is an IR object, give linker LTO plugin a chance to
3403 get the correct symbol table. */
3404 if (abfd->plugin_format == bfd_plugin_yes
3405 #if BFD_SUPPORTS_PLUGINS
3406 || (abfd->plugin_format == bfd_plugin_unknown
3407 && bfd_link_plugin_object_p (abfd))
3408 #endif
3409 )
3410 {
3411 /* Use the IR symbol table if the object has been claimed by
3412 plugin. */
3413 abfd = abfd->plugin_dummy_bfd;
3414 hdr = &elf_tdata (abfd)->symtab_hdr;
3415 }
3416 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3417 hdr = &elf_tdata (abfd)->symtab_hdr;
3418 else
3419 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3420
3421 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3422
3423 /* The sh_info field of the symtab header tells us where the
3424 external symbols start. We don't care about the local symbols. */
3425 if (elf_bad_symtab (abfd))
3426 {
3427 extsymcount = symcount;
3428 extsymoff = 0;
3429 }
3430 else
3431 {
3432 extsymcount = symcount - hdr->sh_info;
3433 extsymoff = hdr->sh_info;
3434 }
3435
3436 if (extsymcount == 0)
3437 return FALSE;
3438
3439 /* Read in the symbol table. */
3440 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3441 NULL, NULL, NULL);
3442 if (isymbuf == NULL)
3443 return FALSE;
3444
3445 /* Scan the symbol table looking for SYMDEF. */
3446 result = FALSE;
3447 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3448 {
3449 const char *name;
3450
3451 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 isym->st_name);
3453 if (name == NULL)
3454 break;
3455
3456 if (strcmp (name, symdef->name) == 0)
3457 {
3458 result = is_global_data_symbol_definition (abfd, isym);
3459 break;
3460 }
3461 }
3462
3463 free (isymbuf);
3464
3465 return result;
3466 }
3467 \f
3468 /* Add an entry to the .dynamic table. */
3469
3470 bfd_boolean
3471 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3472 bfd_vma tag,
3473 bfd_vma val)
3474 {
3475 struct elf_link_hash_table *hash_table;
3476 const struct elf_backend_data *bed;
3477 asection *s;
3478 bfd_size_type newsize;
3479 bfd_byte *newcontents;
3480 Elf_Internal_Dyn dyn;
3481
3482 hash_table = elf_hash_table (info);
3483 if (! is_elf_hash_table (hash_table))
3484 return FALSE;
3485
3486 if (tag == DT_RELA || tag == DT_REL)
3487 hash_table->dynamic_relocs = TRUE;
3488
3489 bed = get_elf_backend_data (hash_table->dynobj);
3490 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3491 BFD_ASSERT (s != NULL);
3492
3493 newsize = s->size + bed->s->sizeof_dyn;
3494 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3495 if (newcontents == NULL)
3496 return FALSE;
3497
3498 dyn.d_tag = tag;
3499 dyn.d_un.d_val = val;
3500 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3501
3502 s->size = newsize;
3503 s->contents = newcontents;
3504
3505 return TRUE;
3506 }
3507
3508 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3509 otherwise just check whether one already exists. Returns -1 on error,
3510 1 if a DT_NEEDED tag already exists, and 0 on success. */
3511
3512 static int
3513 elf_add_dt_needed_tag (bfd *abfd,
3514 struct bfd_link_info *info,
3515 const char *soname,
3516 bfd_boolean do_it)
3517 {
3518 struct elf_link_hash_table *hash_table;
3519 size_t strindex;
3520
3521 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3522 return -1;
3523
3524 hash_table = elf_hash_table (info);
3525 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3526 if (strindex == (size_t) -1)
3527 return -1;
3528
3529 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3530 {
3531 asection *sdyn;
3532 const struct elf_backend_data *bed;
3533 bfd_byte *extdyn;
3534
3535 bed = get_elf_backend_data (hash_table->dynobj);
3536 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3537 if (sdyn != NULL)
3538 for (extdyn = sdyn->contents;
3539 extdyn < sdyn->contents + sdyn->size;
3540 extdyn += bed->s->sizeof_dyn)
3541 {
3542 Elf_Internal_Dyn dyn;
3543
3544 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3545 if (dyn.d_tag == DT_NEEDED
3546 && dyn.d_un.d_val == strindex)
3547 {
3548 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3549 return 1;
3550 }
3551 }
3552 }
3553
3554 if (do_it)
3555 {
3556 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3557 return -1;
3558
3559 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3560 return -1;
3561 }
3562 else
3563 /* We were just checking for existence of the tag. */
3564 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3565
3566 return 0;
3567 }
3568
3569 /* Return true if SONAME is on the needed list between NEEDED and STOP
3570 (or the end of list if STOP is NULL), and needed by a library that
3571 will be loaded. */
3572
3573 static bfd_boolean
3574 on_needed_list (const char *soname,
3575 struct bfd_link_needed_list *needed,
3576 struct bfd_link_needed_list *stop)
3577 {
3578 struct bfd_link_needed_list *look;
3579 for (look = needed; look != stop; look = look->next)
3580 if (strcmp (soname, look->name) == 0
3581 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3582 /* If needed by a library that itself is not directly
3583 needed, recursively check whether that library is
3584 indirectly needed. Since we add DT_NEEDED entries to
3585 the end of the list, library dependencies appear after
3586 the library. Therefore search prior to the current
3587 LOOK, preventing possible infinite recursion. */
3588 || on_needed_list (elf_dt_name (look->by), needed, look)))
3589 return TRUE;
3590
3591 return FALSE;
3592 }
3593
3594 /* Sort symbol by value, section, and size. */
3595 static int
3596 elf_sort_symbol (const void *arg1, const void *arg2)
3597 {
3598 const struct elf_link_hash_entry *h1;
3599 const struct elf_link_hash_entry *h2;
3600 bfd_signed_vma vdiff;
3601
3602 h1 = *(const struct elf_link_hash_entry **) arg1;
3603 h2 = *(const struct elf_link_hash_entry **) arg2;
3604 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3605 if (vdiff != 0)
3606 return vdiff > 0 ? 1 : -1;
3607 else
3608 {
3609 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3610 if (sdiff != 0)
3611 return sdiff > 0 ? 1 : -1;
3612 }
3613 vdiff = h1->size - h2->size;
3614 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3615 }
3616
3617 /* This function is used to adjust offsets into .dynstr for
3618 dynamic symbols. This is called via elf_link_hash_traverse. */
3619
3620 static bfd_boolean
3621 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3622 {
3623 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3624
3625 if (h->dynindx != -1)
3626 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3627 return TRUE;
3628 }
3629
3630 /* Assign string offsets in .dynstr, update all structures referencing
3631 them. */
3632
3633 static bfd_boolean
3634 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3635 {
3636 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3637 struct elf_link_local_dynamic_entry *entry;
3638 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3639 bfd *dynobj = hash_table->dynobj;
3640 asection *sdyn;
3641 bfd_size_type size;
3642 const struct elf_backend_data *bed;
3643 bfd_byte *extdyn;
3644
3645 _bfd_elf_strtab_finalize (dynstr);
3646 size = _bfd_elf_strtab_size (dynstr);
3647
3648 bed = get_elf_backend_data (dynobj);
3649 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3650 BFD_ASSERT (sdyn != NULL);
3651
3652 /* Update all .dynamic entries referencing .dynstr strings. */
3653 for (extdyn = sdyn->contents;
3654 extdyn < sdyn->contents + sdyn->size;
3655 extdyn += bed->s->sizeof_dyn)
3656 {
3657 Elf_Internal_Dyn dyn;
3658
3659 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3660 switch (dyn.d_tag)
3661 {
3662 case DT_STRSZ:
3663 dyn.d_un.d_val = size;
3664 break;
3665 case DT_NEEDED:
3666 case DT_SONAME:
3667 case DT_RPATH:
3668 case DT_RUNPATH:
3669 case DT_FILTER:
3670 case DT_AUXILIARY:
3671 case DT_AUDIT:
3672 case DT_DEPAUDIT:
3673 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3674 break;
3675 default:
3676 continue;
3677 }
3678 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3679 }
3680
3681 /* Now update local dynamic symbols. */
3682 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3683 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3684 entry->isym.st_name);
3685
3686 /* And the rest of dynamic symbols. */
3687 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3688
3689 /* Adjust version definitions. */
3690 if (elf_tdata (output_bfd)->cverdefs)
3691 {
3692 asection *s;
3693 bfd_byte *p;
3694 size_t i;
3695 Elf_Internal_Verdef def;
3696 Elf_Internal_Verdaux defaux;
3697
3698 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3699 p = s->contents;
3700 do
3701 {
3702 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3703 &def);
3704 p += sizeof (Elf_External_Verdef);
3705 if (def.vd_aux != sizeof (Elf_External_Verdef))
3706 continue;
3707 for (i = 0; i < def.vd_cnt; ++i)
3708 {
3709 _bfd_elf_swap_verdaux_in (output_bfd,
3710 (Elf_External_Verdaux *) p, &defaux);
3711 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3712 defaux.vda_name);
3713 _bfd_elf_swap_verdaux_out (output_bfd,
3714 &defaux, (Elf_External_Verdaux *) p);
3715 p += sizeof (Elf_External_Verdaux);
3716 }
3717 }
3718 while (def.vd_next);
3719 }
3720
3721 /* Adjust version references. */
3722 if (elf_tdata (output_bfd)->verref)
3723 {
3724 asection *s;
3725 bfd_byte *p;
3726 size_t i;
3727 Elf_Internal_Verneed need;
3728 Elf_Internal_Vernaux needaux;
3729
3730 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3731 p = s->contents;
3732 do
3733 {
3734 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3735 &need);
3736 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3737 _bfd_elf_swap_verneed_out (output_bfd, &need,
3738 (Elf_External_Verneed *) p);
3739 p += sizeof (Elf_External_Verneed);
3740 for (i = 0; i < need.vn_cnt; ++i)
3741 {
3742 _bfd_elf_swap_vernaux_in (output_bfd,
3743 (Elf_External_Vernaux *) p, &needaux);
3744 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3745 needaux.vna_name);
3746 _bfd_elf_swap_vernaux_out (output_bfd,
3747 &needaux,
3748 (Elf_External_Vernaux *) p);
3749 p += sizeof (Elf_External_Vernaux);
3750 }
3751 }
3752 while (need.vn_next);
3753 }
3754
3755 return TRUE;
3756 }
3757 \f
3758 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3759 The default is to only match when the INPUT and OUTPUT are exactly
3760 the same target. */
3761
3762 bfd_boolean
3763 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3764 const bfd_target *output)
3765 {
3766 return input == output;
3767 }
3768
3769 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3770 This version is used when different targets for the same architecture
3771 are virtually identical. */
3772
3773 bfd_boolean
3774 _bfd_elf_relocs_compatible (const bfd_target *input,
3775 const bfd_target *output)
3776 {
3777 const struct elf_backend_data *obed, *ibed;
3778
3779 if (input == output)
3780 return TRUE;
3781
3782 ibed = xvec_get_elf_backend_data (input);
3783 obed = xvec_get_elf_backend_data (output);
3784
3785 if (ibed->arch != obed->arch)
3786 return FALSE;
3787
3788 /* If both backends are using this function, deem them compatible. */
3789 return ibed->relocs_compatible == obed->relocs_compatible;
3790 }
3791
3792 /* Make a special call to the linker "notice" function to tell it that
3793 we are about to handle an as-needed lib, or have finished
3794 processing the lib. */
3795
3796 bfd_boolean
3797 _bfd_elf_notice_as_needed (bfd *ibfd,
3798 struct bfd_link_info *info,
3799 enum notice_asneeded_action act)
3800 {
3801 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3802 }
3803
3804 /* Check relocations an ELF object file. */
3805
3806 bfd_boolean
3807 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3808 {
3809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3810 struct elf_link_hash_table *htab = elf_hash_table (info);
3811
3812 /* If this object is the same format as the output object, and it is
3813 not a shared library, then let the backend look through the
3814 relocs.
3815
3816 This is required to build global offset table entries and to
3817 arrange for dynamic relocs. It is not required for the
3818 particular common case of linking non PIC code, even when linking
3819 against shared libraries, but unfortunately there is no way of
3820 knowing whether an object file has been compiled PIC or not.
3821 Looking through the relocs is not particularly time consuming.
3822 The problem is that we must either (1) keep the relocs in memory,
3823 which causes the linker to require additional runtime memory or
3824 (2) read the relocs twice from the input file, which wastes time.
3825 This would be a good case for using mmap.
3826
3827 I have no idea how to handle linking PIC code into a file of a
3828 different format. It probably can't be done. */
3829 if ((abfd->flags & DYNAMIC) == 0
3830 && is_elf_hash_table (htab)
3831 && bed->check_relocs != NULL
3832 && elf_object_id (abfd) == elf_hash_table_id (htab)
3833 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3834 {
3835 asection *o;
3836
3837 for (o = abfd->sections; o != NULL; o = o->next)
3838 {
3839 Elf_Internal_Rela *internal_relocs;
3840 bfd_boolean ok;
3841
3842 /* Don't check relocations in excluded sections. */
3843 if ((o->flags & SEC_RELOC) == 0
3844 || (o->flags & SEC_EXCLUDE) != 0
3845 || o->reloc_count == 0
3846 || ((info->strip == strip_all || info->strip == strip_debugger)
3847 && (o->flags & SEC_DEBUGGING) != 0)
3848 || bfd_is_abs_section (o->output_section))
3849 continue;
3850
3851 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3852 info->keep_memory);
3853 if (internal_relocs == NULL)
3854 return FALSE;
3855
3856 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3857
3858 if (elf_section_data (o)->relocs != internal_relocs)
3859 free (internal_relocs);
3860
3861 if (! ok)
3862 return FALSE;
3863 }
3864 }
3865
3866 return TRUE;
3867 }
3868
3869 /* Add symbols from an ELF object file to the linker hash table. */
3870
3871 static bfd_boolean
3872 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3873 {
3874 Elf_Internal_Ehdr *ehdr;
3875 Elf_Internal_Shdr *hdr;
3876 size_t symcount;
3877 size_t extsymcount;
3878 size_t extsymoff;
3879 struct elf_link_hash_entry **sym_hash;
3880 bfd_boolean dynamic;
3881 Elf_External_Versym *extversym = NULL;
3882 Elf_External_Versym *extversym_end = NULL;
3883 Elf_External_Versym *ever;
3884 struct elf_link_hash_entry *weaks;
3885 struct elf_link_hash_entry **nondeflt_vers = NULL;
3886 size_t nondeflt_vers_cnt = 0;
3887 Elf_Internal_Sym *isymbuf = NULL;
3888 Elf_Internal_Sym *isym;
3889 Elf_Internal_Sym *isymend;
3890 const struct elf_backend_data *bed;
3891 bfd_boolean add_needed;
3892 struct elf_link_hash_table *htab;
3893 bfd_size_type amt;
3894 void *alloc_mark = NULL;
3895 struct bfd_hash_entry **old_table = NULL;
3896 unsigned int old_size = 0;
3897 unsigned int old_count = 0;
3898 void *old_tab = NULL;
3899 void *old_ent;
3900 struct bfd_link_hash_entry *old_undefs = NULL;
3901 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3902 void *old_strtab = NULL;
3903 size_t tabsize = 0;
3904 asection *s;
3905 bfd_boolean just_syms;
3906
3907 htab = elf_hash_table (info);
3908 bed = get_elf_backend_data (abfd);
3909
3910 if ((abfd->flags & DYNAMIC) == 0)
3911 dynamic = FALSE;
3912 else
3913 {
3914 dynamic = TRUE;
3915
3916 /* You can't use -r against a dynamic object. Also, there's no
3917 hope of using a dynamic object which does not exactly match
3918 the format of the output file. */
3919 if (bfd_link_relocatable (info)
3920 || !is_elf_hash_table (htab)
3921 || info->output_bfd->xvec != abfd->xvec)
3922 {
3923 if (bfd_link_relocatable (info))
3924 bfd_set_error (bfd_error_invalid_operation);
3925 else
3926 bfd_set_error (bfd_error_wrong_format);
3927 goto error_return;
3928 }
3929 }
3930
3931 ehdr = elf_elfheader (abfd);
3932 if (info->warn_alternate_em
3933 && bed->elf_machine_code != ehdr->e_machine
3934 && ((bed->elf_machine_alt1 != 0
3935 && ehdr->e_machine == bed->elf_machine_alt1)
3936 || (bed->elf_machine_alt2 != 0
3937 && ehdr->e_machine == bed->elf_machine_alt2)))
3938 _bfd_error_handler
3939 /* xgettext:c-format */
3940 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3941 ehdr->e_machine, abfd, bed->elf_machine_code);
3942
3943 /* As a GNU extension, any input sections which are named
3944 .gnu.warning.SYMBOL are treated as warning symbols for the given
3945 symbol. This differs from .gnu.warning sections, which generate
3946 warnings when they are included in an output file. */
3947 /* PR 12761: Also generate this warning when building shared libraries. */
3948 for (s = abfd->sections; s != NULL; s = s->next)
3949 {
3950 const char *name;
3951
3952 name = bfd_get_section_name (abfd, s);
3953 if (CONST_STRNEQ (name, ".gnu.warning."))
3954 {
3955 char *msg;
3956 bfd_size_type sz;
3957
3958 name += sizeof ".gnu.warning." - 1;
3959
3960 /* If this is a shared object, then look up the symbol
3961 in the hash table. If it is there, and it is already
3962 been defined, then we will not be using the entry
3963 from this shared object, so we don't need to warn.
3964 FIXME: If we see the definition in a regular object
3965 later on, we will warn, but we shouldn't. The only
3966 fix is to keep track of what warnings we are supposed
3967 to emit, and then handle them all at the end of the
3968 link. */
3969 if (dynamic)
3970 {
3971 struct elf_link_hash_entry *h;
3972
3973 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3974
3975 /* FIXME: What about bfd_link_hash_common? */
3976 if (h != NULL
3977 && (h->root.type == bfd_link_hash_defined
3978 || h->root.type == bfd_link_hash_defweak))
3979 continue;
3980 }
3981
3982 sz = s->size;
3983 msg = (char *) bfd_alloc (abfd, sz + 1);
3984 if (msg == NULL)
3985 goto error_return;
3986
3987 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3988 goto error_return;
3989
3990 msg[sz] = '\0';
3991
3992 if (! (_bfd_generic_link_add_one_symbol
3993 (info, abfd, name, BSF_WARNING, s, 0, msg,
3994 FALSE, bed->collect, NULL)))
3995 goto error_return;
3996
3997 if (bfd_link_executable (info))
3998 {
3999 /* Clobber the section size so that the warning does
4000 not get copied into the output file. */
4001 s->size = 0;
4002
4003 /* Also set SEC_EXCLUDE, so that symbols defined in
4004 the warning section don't get copied to the output. */
4005 s->flags |= SEC_EXCLUDE;
4006 }
4007 }
4008 }
4009
4010 just_syms = ((s = abfd->sections) != NULL
4011 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4012
4013 add_needed = TRUE;
4014 if (! dynamic)
4015 {
4016 /* If we are creating a shared library, create all the dynamic
4017 sections immediately. We need to attach them to something,
4018 so we attach them to this BFD, provided it is the right
4019 format and is not from ld --just-symbols. Always create the
4020 dynamic sections for -E/--dynamic-list. FIXME: If there
4021 are no input BFD's of the same format as the output, we can't
4022 make a shared library. */
4023 if (!just_syms
4024 && (bfd_link_pic (info)
4025 || (!bfd_link_relocatable (info)
4026 && info->nointerp
4027 && (info->export_dynamic || info->dynamic)))
4028 && is_elf_hash_table (htab)
4029 && info->output_bfd->xvec == abfd->xvec
4030 && !htab->dynamic_sections_created)
4031 {
4032 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4033 goto error_return;
4034 }
4035 }
4036 else if (!is_elf_hash_table (htab))
4037 goto error_return;
4038 else
4039 {
4040 const char *soname = NULL;
4041 char *audit = NULL;
4042 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4043 const Elf_Internal_Phdr *phdr;
4044 int ret;
4045
4046 /* ld --just-symbols and dynamic objects don't mix very well.
4047 ld shouldn't allow it. */
4048 if (just_syms)
4049 abort ();
4050
4051 /* If this dynamic lib was specified on the command line with
4052 --as-needed in effect, then we don't want to add a DT_NEEDED
4053 tag unless the lib is actually used. Similary for libs brought
4054 in by another lib's DT_NEEDED. When --no-add-needed is used
4055 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4056 any dynamic library in DT_NEEDED tags in the dynamic lib at
4057 all. */
4058 add_needed = (elf_dyn_lib_class (abfd)
4059 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4060 | DYN_NO_NEEDED)) == 0;
4061
4062 s = bfd_get_section_by_name (abfd, ".dynamic");
4063 if (s != NULL)
4064 {
4065 bfd_byte *dynbuf;
4066 bfd_byte *extdyn;
4067 unsigned int elfsec;
4068 unsigned long shlink;
4069
4070 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4071 {
4072 error_free_dyn:
4073 free (dynbuf);
4074 goto error_return;
4075 }
4076
4077 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4078 if (elfsec == SHN_BAD)
4079 goto error_free_dyn;
4080 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4081
4082 for (extdyn = dynbuf;
4083 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4084 extdyn += bed->s->sizeof_dyn)
4085 {
4086 Elf_Internal_Dyn dyn;
4087
4088 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4089 if (dyn.d_tag == DT_SONAME)
4090 {
4091 unsigned int tagv = dyn.d_un.d_val;
4092 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4093 if (soname == NULL)
4094 goto error_free_dyn;
4095 }
4096 if (dyn.d_tag == DT_NEEDED)
4097 {
4098 struct bfd_link_needed_list *n, **pn;
4099 char *fnm, *anm;
4100 unsigned int tagv = dyn.d_un.d_val;
4101
4102 amt = sizeof (struct bfd_link_needed_list);
4103 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4104 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4105 if (n == NULL || fnm == NULL)
4106 goto error_free_dyn;
4107 amt = strlen (fnm) + 1;
4108 anm = (char *) bfd_alloc (abfd, amt);
4109 if (anm == NULL)
4110 goto error_free_dyn;
4111 memcpy (anm, fnm, amt);
4112 n->name = anm;
4113 n->by = abfd;
4114 n->next = NULL;
4115 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4116 ;
4117 *pn = n;
4118 }
4119 if (dyn.d_tag == DT_RUNPATH)
4120 {
4121 struct bfd_link_needed_list *n, **pn;
4122 char *fnm, *anm;
4123 unsigned int tagv = dyn.d_un.d_val;
4124
4125 amt = sizeof (struct bfd_link_needed_list);
4126 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4127 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4128 if (n == NULL || fnm == NULL)
4129 goto error_free_dyn;
4130 amt = strlen (fnm) + 1;
4131 anm = (char *) bfd_alloc (abfd, amt);
4132 if (anm == NULL)
4133 goto error_free_dyn;
4134 memcpy (anm, fnm, amt);
4135 n->name = anm;
4136 n->by = abfd;
4137 n->next = NULL;
4138 for (pn = & runpath;
4139 *pn != NULL;
4140 pn = &(*pn)->next)
4141 ;
4142 *pn = n;
4143 }
4144 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4145 if (!runpath && dyn.d_tag == DT_RPATH)
4146 {
4147 struct bfd_link_needed_list *n, **pn;
4148 char *fnm, *anm;
4149 unsigned int tagv = dyn.d_un.d_val;
4150
4151 amt = sizeof (struct bfd_link_needed_list);
4152 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4153 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4154 if (n == NULL || fnm == NULL)
4155 goto error_free_dyn;
4156 amt = strlen (fnm) + 1;
4157 anm = (char *) bfd_alloc (abfd, amt);
4158 if (anm == NULL)
4159 goto error_free_dyn;
4160 memcpy (anm, fnm, amt);
4161 n->name = anm;
4162 n->by = abfd;
4163 n->next = NULL;
4164 for (pn = & rpath;
4165 *pn != NULL;
4166 pn = &(*pn)->next)
4167 ;
4168 *pn = n;
4169 }
4170 if (dyn.d_tag == DT_AUDIT)
4171 {
4172 unsigned int tagv = dyn.d_un.d_val;
4173 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4174 }
4175 }
4176
4177 free (dynbuf);
4178 }
4179
4180 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4181 frees all more recently bfd_alloc'd blocks as well. */
4182 if (runpath)
4183 rpath = runpath;
4184
4185 if (rpath)
4186 {
4187 struct bfd_link_needed_list **pn;
4188 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4189 ;
4190 *pn = rpath;
4191 }
4192
4193 /* If we have a PT_GNU_RELRO program header, mark as read-only
4194 all sections contained fully therein. This makes relro
4195 shared library sections appear as they will at run-time. */
4196 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4197 while (phdr-- > elf_tdata (abfd)->phdr)
4198 if (phdr->p_type == PT_GNU_RELRO)
4199 {
4200 for (s = abfd->sections; s != NULL; s = s->next)
4201 if ((s->flags & SEC_ALLOC) != 0
4202 && s->vma >= phdr->p_vaddr
4203 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4204 s->flags |= SEC_READONLY;
4205 break;
4206 }
4207
4208 /* We do not want to include any of the sections in a dynamic
4209 object in the output file. We hack by simply clobbering the
4210 list of sections in the BFD. This could be handled more
4211 cleanly by, say, a new section flag; the existing
4212 SEC_NEVER_LOAD flag is not the one we want, because that one
4213 still implies that the section takes up space in the output
4214 file. */
4215 bfd_section_list_clear (abfd);
4216
4217 /* Find the name to use in a DT_NEEDED entry that refers to this
4218 object. If the object has a DT_SONAME entry, we use it.
4219 Otherwise, if the generic linker stuck something in
4220 elf_dt_name, we use that. Otherwise, we just use the file
4221 name. */
4222 if (soname == NULL || *soname == '\0')
4223 {
4224 soname = elf_dt_name (abfd);
4225 if (soname == NULL || *soname == '\0')
4226 soname = bfd_get_filename (abfd);
4227 }
4228
4229 /* Save the SONAME because sometimes the linker emulation code
4230 will need to know it. */
4231 elf_dt_name (abfd) = soname;
4232
4233 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4234 if (ret < 0)
4235 goto error_return;
4236
4237 /* If we have already included this dynamic object in the
4238 link, just ignore it. There is no reason to include a
4239 particular dynamic object more than once. */
4240 if (ret > 0)
4241 return TRUE;
4242
4243 /* Save the DT_AUDIT entry for the linker emulation code. */
4244 elf_dt_audit (abfd) = audit;
4245 }
4246
4247 /* If this is a dynamic object, we always link against the .dynsym
4248 symbol table, not the .symtab symbol table. The dynamic linker
4249 will only see the .dynsym symbol table, so there is no reason to
4250 look at .symtab for a dynamic object. */
4251
4252 if (! dynamic || elf_dynsymtab (abfd) == 0)
4253 hdr = &elf_tdata (abfd)->symtab_hdr;
4254 else
4255 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4256
4257 symcount = hdr->sh_size / bed->s->sizeof_sym;
4258
4259 /* The sh_info field of the symtab header tells us where the
4260 external symbols start. We don't care about the local symbols at
4261 this point. */
4262 if (elf_bad_symtab (abfd))
4263 {
4264 extsymcount = symcount;
4265 extsymoff = 0;
4266 }
4267 else
4268 {
4269 extsymcount = symcount - hdr->sh_info;
4270 extsymoff = hdr->sh_info;
4271 }
4272
4273 sym_hash = elf_sym_hashes (abfd);
4274 if (extsymcount != 0)
4275 {
4276 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4277 NULL, NULL, NULL);
4278 if (isymbuf == NULL)
4279 goto error_return;
4280
4281 if (sym_hash == NULL)
4282 {
4283 /* We store a pointer to the hash table entry for each
4284 external symbol. */
4285 amt = extsymcount;
4286 amt *= sizeof (struct elf_link_hash_entry *);
4287 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4288 if (sym_hash == NULL)
4289 goto error_free_sym;
4290 elf_sym_hashes (abfd) = sym_hash;
4291 }
4292 }
4293
4294 if (dynamic)
4295 {
4296 /* Read in any version definitions. */
4297 if (!_bfd_elf_slurp_version_tables (abfd,
4298 info->default_imported_symver))
4299 goto error_free_sym;
4300
4301 /* Read in the symbol versions, but don't bother to convert them
4302 to internal format. */
4303 if (elf_dynversym (abfd) != 0)
4304 {
4305 Elf_Internal_Shdr *versymhdr;
4306
4307 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4308 amt = versymhdr->sh_size;
4309 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4310 if (extversym == NULL)
4311 goto error_free_sym;
4312 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4313 || bfd_bread (extversym, amt, abfd) != amt)
4314 goto error_free_vers;
4315 extversym_end = extversym + (amt / sizeof (* extversym));
4316 }
4317 }
4318
4319 /* If we are loading an as-needed shared lib, save the symbol table
4320 state before we start adding symbols. If the lib turns out
4321 to be unneeded, restore the state. */
4322 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4323 {
4324 unsigned int i;
4325 size_t entsize;
4326
4327 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4328 {
4329 struct bfd_hash_entry *p;
4330 struct elf_link_hash_entry *h;
4331
4332 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4333 {
4334 h = (struct elf_link_hash_entry *) p;
4335 entsize += htab->root.table.entsize;
4336 if (h->root.type == bfd_link_hash_warning)
4337 entsize += htab->root.table.entsize;
4338 }
4339 }
4340
4341 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4342 old_tab = bfd_malloc (tabsize + entsize);
4343 if (old_tab == NULL)
4344 goto error_free_vers;
4345
4346 /* Remember the current objalloc pointer, so that all mem for
4347 symbols added can later be reclaimed. */
4348 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4349 if (alloc_mark == NULL)
4350 goto error_free_vers;
4351
4352 /* Make a special call to the linker "notice" function to
4353 tell it that we are about to handle an as-needed lib. */
4354 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4355 goto error_free_vers;
4356
4357 /* Clone the symbol table. Remember some pointers into the
4358 symbol table, and dynamic symbol count. */
4359 old_ent = (char *) old_tab + tabsize;
4360 memcpy (old_tab, htab->root.table.table, tabsize);
4361 old_undefs = htab->root.undefs;
4362 old_undefs_tail = htab->root.undefs_tail;
4363 old_table = htab->root.table.table;
4364 old_size = htab->root.table.size;
4365 old_count = htab->root.table.count;
4366 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4367 if (old_strtab == NULL)
4368 goto error_free_vers;
4369
4370 for (i = 0; i < htab->root.table.size; i++)
4371 {
4372 struct bfd_hash_entry *p;
4373 struct elf_link_hash_entry *h;
4374
4375 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4376 {
4377 memcpy (old_ent, p, htab->root.table.entsize);
4378 old_ent = (char *) old_ent + htab->root.table.entsize;
4379 h = (struct elf_link_hash_entry *) p;
4380 if (h->root.type == bfd_link_hash_warning)
4381 {
4382 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4383 old_ent = (char *) old_ent + htab->root.table.entsize;
4384 }
4385 }
4386 }
4387 }
4388
4389 weaks = NULL;
4390 if (extversym == NULL)
4391 ever = NULL;
4392 else if (extversym + extsymoff < extversym_end)
4393 ever = extversym + extsymoff;
4394 else
4395 {
4396 /* xgettext:c-format */
4397 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4398 abfd, (long) extsymoff,
4399 (long) (extversym_end - extversym) / sizeof (* extversym));
4400 bfd_set_error (bfd_error_bad_value);
4401 goto error_free_vers;
4402 }
4403
4404 if (abfd->lto_slim_object)
4405 {
4406 _bfd_error_handler
4407 (_("%pB: plugin needed to handle lto object"), abfd);
4408 }
4409
4410 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4411 isym < isymend;
4412 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4413 {
4414 int bind;
4415 bfd_vma value;
4416 asection *sec, *new_sec;
4417 flagword flags;
4418 const char *name;
4419 struct elf_link_hash_entry *h;
4420 struct elf_link_hash_entry *hi;
4421 bfd_boolean definition;
4422 bfd_boolean size_change_ok;
4423 bfd_boolean type_change_ok;
4424 bfd_boolean new_weak;
4425 bfd_boolean old_weak;
4426 bfd_boolean override;
4427 bfd_boolean common;
4428 bfd_boolean discarded;
4429 unsigned int old_alignment;
4430 unsigned int shindex;
4431 bfd *old_bfd;
4432 bfd_boolean matched;
4433
4434 override = FALSE;
4435
4436 flags = BSF_NO_FLAGS;
4437 sec = NULL;
4438 value = isym->st_value;
4439 common = bed->common_definition (isym);
4440 if (common && info->inhibit_common_definition)
4441 {
4442 /* Treat common symbol as undefined for --no-define-common. */
4443 isym->st_shndx = SHN_UNDEF;
4444 common = FALSE;
4445 }
4446 discarded = FALSE;
4447
4448 bind = ELF_ST_BIND (isym->st_info);
4449 switch (bind)
4450 {
4451 case STB_LOCAL:
4452 /* This should be impossible, since ELF requires that all
4453 global symbols follow all local symbols, and that sh_info
4454 point to the first global symbol. Unfortunately, Irix 5
4455 screws this up. */
4456 if (elf_bad_symtab (abfd))
4457 continue;
4458
4459 /* If we aren't prepared to handle locals within the globals
4460 then we'll likely segfault on a NULL symbol hash if the
4461 symbol is ever referenced in relocations. */
4462 shindex = elf_elfheader (abfd)->e_shstrndx;
4463 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4464 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4465 " (>= sh_info of %lu)"),
4466 abfd, name, (long) (isym - isymbuf + extsymoff),
4467 (long) extsymoff);
4468
4469 /* Dynamic object relocations are not processed by ld, so
4470 ld won't run into the problem mentioned above. */
4471 if (dynamic)
4472 continue;
4473 bfd_set_error (bfd_error_bad_value);
4474 goto error_free_vers;
4475
4476 case STB_GLOBAL:
4477 if (isym->st_shndx != SHN_UNDEF && !common)
4478 flags = BSF_GLOBAL;
4479 break;
4480
4481 case STB_WEAK:
4482 flags = BSF_WEAK;
4483 break;
4484
4485 case STB_GNU_UNIQUE:
4486 flags = BSF_GNU_UNIQUE;
4487 break;
4488
4489 default:
4490 /* Leave it up to the processor backend. */
4491 break;
4492 }
4493
4494 if (isym->st_shndx == SHN_UNDEF)
4495 sec = bfd_und_section_ptr;
4496 else if (isym->st_shndx == SHN_ABS)
4497 sec = bfd_abs_section_ptr;
4498 else if (isym->st_shndx == SHN_COMMON)
4499 {
4500 sec = bfd_com_section_ptr;
4501 /* What ELF calls the size we call the value. What ELF
4502 calls the value we call the alignment. */
4503 value = isym->st_size;
4504 }
4505 else
4506 {
4507 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4508 if (sec == NULL)
4509 sec = bfd_abs_section_ptr;
4510 else if (discarded_section (sec))
4511 {
4512 /* Symbols from discarded section are undefined. We keep
4513 its visibility. */
4514 sec = bfd_und_section_ptr;
4515 discarded = TRUE;
4516 isym->st_shndx = SHN_UNDEF;
4517 }
4518 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4519 value -= sec->vma;
4520 }
4521
4522 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4523 isym->st_name);
4524 if (name == NULL)
4525 goto error_free_vers;
4526
4527 if (isym->st_shndx == SHN_COMMON
4528 && (abfd->flags & BFD_PLUGIN) != 0)
4529 {
4530 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4531
4532 if (xc == NULL)
4533 {
4534 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4535 | SEC_EXCLUDE);
4536 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4537 if (xc == NULL)
4538 goto error_free_vers;
4539 }
4540 sec = xc;
4541 }
4542 else if (isym->st_shndx == SHN_COMMON
4543 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4544 && !bfd_link_relocatable (info))
4545 {
4546 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4547
4548 if (tcomm == NULL)
4549 {
4550 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4551 | SEC_LINKER_CREATED);
4552 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4553 if (tcomm == NULL)
4554 goto error_free_vers;
4555 }
4556 sec = tcomm;
4557 }
4558 else if (bed->elf_add_symbol_hook)
4559 {
4560 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4561 &sec, &value))
4562 goto error_free_vers;
4563
4564 /* The hook function sets the name to NULL if this symbol
4565 should be skipped for some reason. */
4566 if (name == NULL)
4567 continue;
4568 }
4569
4570 /* Sanity check that all possibilities were handled. */
4571 if (sec == NULL)
4572 abort ();
4573
4574 /* Silently discard TLS symbols from --just-syms. There's
4575 no way to combine a static TLS block with a new TLS block
4576 for this executable. */
4577 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4578 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4579 continue;
4580
4581 if (bfd_is_und_section (sec)
4582 || bfd_is_com_section (sec))
4583 definition = FALSE;
4584 else
4585 definition = TRUE;
4586
4587 size_change_ok = FALSE;
4588 type_change_ok = bed->type_change_ok;
4589 old_weak = FALSE;
4590 matched = FALSE;
4591 old_alignment = 0;
4592 old_bfd = NULL;
4593 new_sec = sec;
4594
4595 if (is_elf_hash_table (htab))
4596 {
4597 Elf_Internal_Versym iver;
4598 unsigned int vernum = 0;
4599 bfd_boolean skip;
4600
4601 if (ever == NULL)
4602 {
4603 if (info->default_imported_symver)
4604 /* Use the default symbol version created earlier. */
4605 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4606 else
4607 iver.vs_vers = 0;
4608 }
4609 else if (ever >= extversym_end)
4610 {
4611 /* xgettext:c-format */
4612 _bfd_error_handler (_("%pB: not enough version information"),
4613 abfd);
4614 bfd_set_error (bfd_error_bad_value);
4615 goto error_free_vers;
4616 }
4617 else
4618 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4619
4620 vernum = iver.vs_vers & VERSYM_VERSION;
4621
4622 /* If this is a hidden symbol, or if it is not version
4623 1, we append the version name to the symbol name.
4624 However, we do not modify a non-hidden absolute symbol
4625 if it is not a function, because it might be the version
4626 symbol itself. FIXME: What if it isn't? */
4627 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4628 || (vernum > 1
4629 && (!bfd_is_abs_section (sec)
4630 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4631 {
4632 const char *verstr;
4633 size_t namelen, verlen, newlen;
4634 char *newname, *p;
4635
4636 if (isym->st_shndx != SHN_UNDEF)
4637 {
4638 if (vernum > elf_tdata (abfd)->cverdefs)
4639 verstr = NULL;
4640 else if (vernum > 1)
4641 verstr =
4642 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4643 else
4644 verstr = "";
4645
4646 if (verstr == NULL)
4647 {
4648 _bfd_error_handler
4649 /* xgettext:c-format */
4650 (_("%pB: %s: invalid version %u (max %d)"),
4651 abfd, name, vernum,
4652 elf_tdata (abfd)->cverdefs);
4653 bfd_set_error (bfd_error_bad_value);
4654 goto error_free_vers;
4655 }
4656 }
4657 else
4658 {
4659 /* We cannot simply test for the number of
4660 entries in the VERNEED section since the
4661 numbers for the needed versions do not start
4662 at 0. */
4663 Elf_Internal_Verneed *t;
4664
4665 verstr = NULL;
4666 for (t = elf_tdata (abfd)->verref;
4667 t != NULL;
4668 t = t->vn_nextref)
4669 {
4670 Elf_Internal_Vernaux *a;
4671
4672 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4673 {
4674 if (a->vna_other == vernum)
4675 {
4676 verstr = a->vna_nodename;
4677 break;
4678 }
4679 }
4680 if (a != NULL)
4681 break;
4682 }
4683 if (verstr == NULL)
4684 {
4685 _bfd_error_handler
4686 /* xgettext:c-format */
4687 (_("%pB: %s: invalid needed version %d"),
4688 abfd, name, vernum);
4689 bfd_set_error (bfd_error_bad_value);
4690 goto error_free_vers;
4691 }
4692 }
4693
4694 namelen = strlen (name);
4695 verlen = strlen (verstr);
4696 newlen = namelen + verlen + 2;
4697 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4698 && isym->st_shndx != SHN_UNDEF)
4699 ++newlen;
4700
4701 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4702 if (newname == NULL)
4703 goto error_free_vers;
4704 memcpy (newname, name, namelen);
4705 p = newname + namelen;
4706 *p++ = ELF_VER_CHR;
4707 /* If this is a defined non-hidden version symbol,
4708 we add another @ to the name. This indicates the
4709 default version of the symbol. */
4710 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4711 && isym->st_shndx != SHN_UNDEF)
4712 *p++ = ELF_VER_CHR;
4713 memcpy (p, verstr, verlen + 1);
4714
4715 name = newname;
4716 }
4717
4718 /* If this symbol has default visibility and the user has
4719 requested we not re-export it, then mark it as hidden. */
4720 if (!bfd_is_und_section (sec)
4721 && !dynamic
4722 && abfd->no_export
4723 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4724 isym->st_other = (STV_HIDDEN
4725 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4726
4727 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4728 sym_hash, &old_bfd, &old_weak,
4729 &old_alignment, &skip, &override,
4730 &type_change_ok, &size_change_ok,
4731 &matched))
4732 goto error_free_vers;
4733
4734 if (skip)
4735 continue;
4736
4737 /* Override a definition only if the new symbol matches the
4738 existing one. */
4739 if (override && matched)
4740 definition = FALSE;
4741
4742 h = *sym_hash;
4743 while (h->root.type == bfd_link_hash_indirect
4744 || h->root.type == bfd_link_hash_warning)
4745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4746
4747 if (elf_tdata (abfd)->verdef != NULL
4748 && vernum > 1
4749 && definition)
4750 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4751 }
4752
4753 if (! (_bfd_generic_link_add_one_symbol
4754 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4755 (struct bfd_link_hash_entry **) sym_hash)))
4756 goto error_free_vers;
4757
4758 h = *sym_hash;
4759 /* We need to make sure that indirect symbol dynamic flags are
4760 updated. */
4761 hi = h;
4762 while (h->root.type == bfd_link_hash_indirect
4763 || h->root.type == bfd_link_hash_warning)
4764 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4765
4766 /* Setting the index to -3 tells elf_link_output_extsym that
4767 this symbol is defined in a discarded section. */
4768 if (discarded)
4769 h->indx = -3;
4770
4771 *sym_hash = h;
4772
4773 new_weak = (flags & BSF_WEAK) != 0;
4774 if (dynamic
4775 && definition
4776 && new_weak
4777 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4778 && is_elf_hash_table (htab)
4779 && h->u.alias == NULL)
4780 {
4781 /* Keep a list of all weak defined non function symbols from
4782 a dynamic object, using the alias field. Later in this
4783 function we will set the alias field to the correct
4784 value. We only put non-function symbols from dynamic
4785 objects on this list, because that happens to be the only
4786 time we need to know the normal symbol corresponding to a
4787 weak symbol, and the information is time consuming to
4788 figure out. If the alias field is not already NULL,
4789 then this symbol was already defined by some previous
4790 dynamic object, and we will be using that previous
4791 definition anyhow. */
4792
4793 h->u.alias = weaks;
4794 weaks = h;
4795 }
4796
4797 /* Set the alignment of a common symbol. */
4798 if ((common || bfd_is_com_section (sec))
4799 && h->root.type == bfd_link_hash_common)
4800 {
4801 unsigned int align;
4802
4803 if (common)
4804 align = bfd_log2 (isym->st_value);
4805 else
4806 {
4807 /* The new symbol is a common symbol in a shared object.
4808 We need to get the alignment from the section. */
4809 align = new_sec->alignment_power;
4810 }
4811 if (align > old_alignment)
4812 h->root.u.c.p->alignment_power = align;
4813 else
4814 h->root.u.c.p->alignment_power = old_alignment;
4815 }
4816
4817 if (is_elf_hash_table (htab))
4818 {
4819 /* Set a flag in the hash table entry indicating the type of
4820 reference or definition we just found. A dynamic symbol
4821 is one which is referenced or defined by both a regular
4822 object and a shared object. */
4823 bfd_boolean dynsym = FALSE;
4824
4825 /* Plugin symbols aren't normal. Don't set def_regular or
4826 ref_regular for them, or make them dynamic. */
4827 if ((abfd->flags & BFD_PLUGIN) != 0)
4828 ;
4829 else if (! dynamic)
4830 {
4831 if (! definition)
4832 {
4833 h->ref_regular = 1;
4834 if (bind != STB_WEAK)
4835 h->ref_regular_nonweak = 1;
4836 }
4837 else
4838 {
4839 h->def_regular = 1;
4840 if (h->def_dynamic)
4841 {
4842 h->def_dynamic = 0;
4843 h->ref_dynamic = 1;
4844 }
4845 }
4846
4847 /* If the indirect symbol has been forced local, don't
4848 make the real symbol dynamic. */
4849 if ((h == hi || !hi->forced_local)
4850 && (bfd_link_dll (info)
4851 || h->def_dynamic
4852 || h->ref_dynamic))
4853 dynsym = TRUE;
4854 }
4855 else
4856 {
4857 if (! definition)
4858 {
4859 h->ref_dynamic = 1;
4860 hi->ref_dynamic = 1;
4861 }
4862 else
4863 {
4864 h->def_dynamic = 1;
4865 hi->def_dynamic = 1;
4866 }
4867
4868 /* If the indirect symbol has been forced local, don't
4869 make the real symbol dynamic. */
4870 if ((h == hi || !hi->forced_local)
4871 && (h->def_regular
4872 || h->ref_regular
4873 || (h->is_weakalias
4874 && weakdef (h)->dynindx != -1)))
4875 dynsym = TRUE;
4876 }
4877
4878 /* Check to see if we need to add an indirect symbol for
4879 the default name. */
4880 if (definition
4881 || (!override && h->root.type == bfd_link_hash_common))
4882 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4883 sec, value, &old_bfd, &dynsym))
4884 goto error_free_vers;
4885
4886 /* Check the alignment when a common symbol is involved. This
4887 can change when a common symbol is overridden by a normal
4888 definition or a common symbol is ignored due to the old
4889 normal definition. We need to make sure the maximum
4890 alignment is maintained. */
4891 if ((old_alignment || common)
4892 && h->root.type != bfd_link_hash_common)
4893 {
4894 unsigned int common_align;
4895 unsigned int normal_align;
4896 unsigned int symbol_align;
4897 bfd *normal_bfd;
4898 bfd *common_bfd;
4899
4900 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4901 || h->root.type == bfd_link_hash_defweak);
4902
4903 symbol_align = ffs (h->root.u.def.value) - 1;
4904 if (h->root.u.def.section->owner != NULL
4905 && (h->root.u.def.section->owner->flags
4906 & (DYNAMIC | BFD_PLUGIN)) == 0)
4907 {
4908 normal_align = h->root.u.def.section->alignment_power;
4909 if (normal_align > symbol_align)
4910 normal_align = symbol_align;
4911 }
4912 else
4913 normal_align = symbol_align;
4914
4915 if (old_alignment)
4916 {
4917 common_align = old_alignment;
4918 common_bfd = old_bfd;
4919 normal_bfd = abfd;
4920 }
4921 else
4922 {
4923 common_align = bfd_log2 (isym->st_value);
4924 common_bfd = abfd;
4925 normal_bfd = old_bfd;
4926 }
4927
4928 if (normal_align < common_align)
4929 {
4930 /* PR binutils/2735 */
4931 if (normal_bfd == NULL)
4932 _bfd_error_handler
4933 /* xgettext:c-format */
4934 (_("warning: alignment %u of common symbol `%s' in %pB is"
4935 " greater than the alignment (%u) of its section %pA"),
4936 1 << common_align, name, common_bfd,
4937 1 << normal_align, h->root.u.def.section);
4938 else
4939 _bfd_error_handler
4940 /* xgettext:c-format */
4941 (_("warning: alignment %u of symbol `%s' in %pB"
4942 " is smaller than %u in %pB"),
4943 1 << normal_align, name, normal_bfd,
4944 1 << common_align, common_bfd);
4945 }
4946 }
4947
4948 /* Remember the symbol size if it isn't undefined. */
4949 if (isym->st_size != 0
4950 && isym->st_shndx != SHN_UNDEF
4951 && (definition || h->size == 0))
4952 {
4953 if (h->size != 0
4954 && h->size != isym->st_size
4955 && ! size_change_ok)
4956 _bfd_error_handler
4957 /* xgettext:c-format */
4958 (_("warning: size of symbol `%s' changed"
4959 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4960 name, (uint64_t) h->size, old_bfd,
4961 (uint64_t) isym->st_size, abfd);
4962
4963 h->size = isym->st_size;
4964 }
4965
4966 /* If this is a common symbol, then we always want H->SIZE
4967 to be the size of the common symbol. The code just above
4968 won't fix the size if a common symbol becomes larger. We
4969 don't warn about a size change here, because that is
4970 covered by --warn-common. Allow changes between different
4971 function types. */
4972 if (h->root.type == bfd_link_hash_common)
4973 h->size = h->root.u.c.size;
4974
4975 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4976 && ((definition && !new_weak)
4977 || (old_weak && h->root.type == bfd_link_hash_common)
4978 || h->type == STT_NOTYPE))
4979 {
4980 unsigned int type = ELF_ST_TYPE (isym->st_info);
4981
4982 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4983 symbol. */
4984 if (type == STT_GNU_IFUNC
4985 && (abfd->flags & DYNAMIC) != 0)
4986 type = STT_FUNC;
4987
4988 if (h->type != type)
4989 {
4990 if (h->type != STT_NOTYPE && ! type_change_ok)
4991 /* xgettext:c-format */
4992 _bfd_error_handler
4993 (_("warning: type of symbol `%s' changed"
4994 " from %d to %d in %pB"),
4995 name, h->type, type, abfd);
4996
4997 h->type = type;
4998 }
4999 }
5000
5001 /* Merge st_other field. */
5002 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5003
5004 /* We don't want to make debug symbol dynamic. */
5005 if (definition
5006 && (sec->flags & SEC_DEBUGGING)
5007 && !bfd_link_relocatable (info))
5008 dynsym = FALSE;
5009
5010 /* Nor should we make plugin symbols dynamic. */
5011 if ((abfd->flags & BFD_PLUGIN) != 0)
5012 dynsym = FALSE;
5013
5014 if (definition)
5015 {
5016 h->target_internal = isym->st_target_internal;
5017 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5018 }
5019
5020 if (definition && !dynamic)
5021 {
5022 char *p = strchr (name, ELF_VER_CHR);
5023 if (p != NULL && p[1] != ELF_VER_CHR)
5024 {
5025 /* Queue non-default versions so that .symver x, x@FOO
5026 aliases can be checked. */
5027 if (!nondeflt_vers)
5028 {
5029 amt = ((isymend - isym + 1)
5030 * sizeof (struct elf_link_hash_entry *));
5031 nondeflt_vers
5032 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5033 if (!nondeflt_vers)
5034 goto error_free_vers;
5035 }
5036 nondeflt_vers[nondeflt_vers_cnt++] = h;
5037 }
5038 }
5039
5040 if (dynsym && h->dynindx == -1)
5041 {
5042 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5043 goto error_free_vers;
5044 if (h->is_weakalias
5045 && weakdef (h)->dynindx == -1)
5046 {
5047 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5048 goto error_free_vers;
5049 }
5050 }
5051 else if (h->dynindx != -1)
5052 /* If the symbol already has a dynamic index, but
5053 visibility says it should not be visible, turn it into
5054 a local symbol. */
5055 switch (ELF_ST_VISIBILITY (h->other))
5056 {
5057 case STV_INTERNAL:
5058 case STV_HIDDEN:
5059 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5060 dynsym = FALSE;
5061 break;
5062 }
5063
5064 /* Don't add DT_NEEDED for references from the dummy bfd nor
5065 for unmatched symbol. */
5066 if (!add_needed
5067 && matched
5068 && definition
5069 && ((dynsym
5070 && h->ref_regular_nonweak
5071 && (old_bfd == NULL
5072 || (old_bfd->flags & BFD_PLUGIN) == 0))
5073 || (h->ref_dynamic_nonweak
5074 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5075 && !on_needed_list (elf_dt_name (abfd),
5076 htab->needed, NULL))))
5077 {
5078 int ret;
5079 const char *soname = elf_dt_name (abfd);
5080
5081 info->callbacks->minfo ("%!", soname, old_bfd,
5082 h->root.root.string);
5083
5084 /* A symbol from a library loaded via DT_NEEDED of some
5085 other library is referenced by a regular object.
5086 Add a DT_NEEDED entry for it. Issue an error if
5087 --no-add-needed is used and the reference was not
5088 a weak one. */
5089 if (old_bfd != NULL
5090 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5091 {
5092 _bfd_error_handler
5093 /* xgettext:c-format */
5094 (_("%pB: undefined reference to symbol '%s'"),
5095 old_bfd, name);
5096 bfd_set_error (bfd_error_missing_dso);
5097 goto error_free_vers;
5098 }
5099
5100 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5101 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5102
5103 add_needed = TRUE;
5104 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5105 if (ret < 0)
5106 goto error_free_vers;
5107
5108 BFD_ASSERT (ret == 0);
5109 }
5110 }
5111 }
5112
5113 if (info->lto_plugin_active
5114 && !bfd_link_relocatable (info)
5115 && (abfd->flags & BFD_PLUGIN) == 0
5116 && !just_syms
5117 && extsymcount)
5118 {
5119 int r_sym_shift;
5120
5121 if (bed->s->arch_size == 32)
5122 r_sym_shift = 8;
5123 else
5124 r_sym_shift = 32;
5125
5126 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5127 referenced in regular objects so that linker plugin will get
5128 the correct symbol resolution. */
5129
5130 sym_hash = elf_sym_hashes (abfd);
5131 for (s = abfd->sections; s != NULL; s = s->next)
5132 {
5133 Elf_Internal_Rela *internal_relocs;
5134 Elf_Internal_Rela *rel, *relend;
5135
5136 /* Don't check relocations in excluded sections. */
5137 if ((s->flags & SEC_RELOC) == 0
5138 || s->reloc_count == 0
5139 || (s->flags & SEC_EXCLUDE) != 0
5140 || ((info->strip == strip_all
5141 || info->strip == strip_debugger)
5142 && (s->flags & SEC_DEBUGGING) != 0))
5143 continue;
5144
5145 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5146 NULL,
5147 info->keep_memory);
5148 if (internal_relocs == NULL)
5149 goto error_free_vers;
5150
5151 rel = internal_relocs;
5152 relend = rel + s->reloc_count;
5153 for ( ; rel < relend; rel++)
5154 {
5155 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5156 struct elf_link_hash_entry *h;
5157
5158 /* Skip local symbols. */
5159 if (r_symndx < extsymoff)
5160 continue;
5161
5162 h = sym_hash[r_symndx - extsymoff];
5163 if (h != NULL)
5164 h->root.non_ir_ref_regular = 1;
5165 }
5166
5167 if (elf_section_data (s)->relocs != internal_relocs)
5168 free (internal_relocs);
5169 }
5170 }
5171
5172 if (extversym != NULL)
5173 {
5174 free (extversym);
5175 extversym = NULL;
5176 }
5177
5178 if (isymbuf != NULL)
5179 {
5180 free (isymbuf);
5181 isymbuf = NULL;
5182 }
5183
5184 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5185 {
5186 unsigned int i;
5187
5188 /* Restore the symbol table. */
5189 old_ent = (char *) old_tab + tabsize;
5190 memset (elf_sym_hashes (abfd), 0,
5191 extsymcount * sizeof (struct elf_link_hash_entry *));
5192 htab->root.table.table = old_table;
5193 htab->root.table.size = old_size;
5194 htab->root.table.count = old_count;
5195 memcpy (htab->root.table.table, old_tab, tabsize);
5196 htab->root.undefs = old_undefs;
5197 htab->root.undefs_tail = old_undefs_tail;
5198 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5199 free (old_strtab);
5200 old_strtab = NULL;
5201 for (i = 0; i < htab->root.table.size; i++)
5202 {
5203 struct bfd_hash_entry *p;
5204 struct elf_link_hash_entry *h;
5205 bfd_size_type size;
5206 unsigned int alignment_power;
5207 unsigned int non_ir_ref_dynamic;
5208
5209 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5210 {
5211 h = (struct elf_link_hash_entry *) p;
5212 if (h->root.type == bfd_link_hash_warning)
5213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5214
5215 /* Preserve the maximum alignment and size for common
5216 symbols even if this dynamic lib isn't on DT_NEEDED
5217 since it can still be loaded at run time by another
5218 dynamic lib. */
5219 if (h->root.type == bfd_link_hash_common)
5220 {
5221 size = h->root.u.c.size;
5222 alignment_power = h->root.u.c.p->alignment_power;
5223 }
5224 else
5225 {
5226 size = 0;
5227 alignment_power = 0;
5228 }
5229 /* Preserve non_ir_ref_dynamic so that this symbol
5230 will be exported when the dynamic lib becomes needed
5231 in the second pass. */
5232 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5233 memcpy (p, old_ent, htab->root.table.entsize);
5234 old_ent = (char *) old_ent + htab->root.table.entsize;
5235 h = (struct elf_link_hash_entry *) p;
5236 if (h->root.type == bfd_link_hash_warning)
5237 {
5238 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5239 old_ent = (char *) old_ent + htab->root.table.entsize;
5240 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5241 }
5242 if (h->root.type == bfd_link_hash_common)
5243 {
5244 if (size > h->root.u.c.size)
5245 h->root.u.c.size = size;
5246 if (alignment_power > h->root.u.c.p->alignment_power)
5247 h->root.u.c.p->alignment_power = alignment_power;
5248 }
5249 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5250 }
5251 }
5252
5253 /* Make a special call to the linker "notice" function to
5254 tell it that symbols added for crefs may need to be removed. */
5255 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5256 goto error_free_vers;
5257
5258 free (old_tab);
5259 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5260 alloc_mark);
5261 if (nondeflt_vers != NULL)
5262 free (nondeflt_vers);
5263 return TRUE;
5264 }
5265
5266 if (old_tab != NULL)
5267 {
5268 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5269 goto error_free_vers;
5270 free (old_tab);
5271 old_tab = NULL;
5272 }
5273
5274 /* Now that all the symbols from this input file are created, if
5275 not performing a relocatable link, handle .symver foo, foo@BAR
5276 such that any relocs against foo become foo@BAR. */
5277 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5278 {
5279 size_t cnt, symidx;
5280
5281 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5282 {
5283 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5284 char *shortname, *p;
5285
5286 p = strchr (h->root.root.string, ELF_VER_CHR);
5287 if (p == NULL
5288 || (h->root.type != bfd_link_hash_defined
5289 && h->root.type != bfd_link_hash_defweak))
5290 continue;
5291
5292 amt = p - h->root.root.string;
5293 shortname = (char *) bfd_malloc (amt + 1);
5294 if (!shortname)
5295 goto error_free_vers;
5296 memcpy (shortname, h->root.root.string, amt);
5297 shortname[amt] = '\0';
5298
5299 hi = (struct elf_link_hash_entry *)
5300 bfd_link_hash_lookup (&htab->root, shortname,
5301 FALSE, FALSE, FALSE);
5302 if (hi != NULL
5303 && hi->root.type == h->root.type
5304 && hi->root.u.def.value == h->root.u.def.value
5305 && hi->root.u.def.section == h->root.u.def.section)
5306 {
5307 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5308 hi->root.type = bfd_link_hash_indirect;
5309 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5310 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5311 sym_hash = elf_sym_hashes (abfd);
5312 if (sym_hash)
5313 for (symidx = 0; symidx < extsymcount; ++symidx)
5314 if (sym_hash[symidx] == hi)
5315 {
5316 sym_hash[symidx] = h;
5317 break;
5318 }
5319 }
5320 free (shortname);
5321 }
5322 free (nondeflt_vers);
5323 nondeflt_vers = NULL;
5324 }
5325
5326 /* Now set the alias field correctly for all the weak defined
5327 symbols we found. The only way to do this is to search all the
5328 symbols. Since we only need the information for non functions in
5329 dynamic objects, that's the only time we actually put anything on
5330 the list WEAKS. We need this information so that if a regular
5331 object refers to a symbol defined weakly in a dynamic object, the
5332 real symbol in the dynamic object is also put in the dynamic
5333 symbols; we also must arrange for both symbols to point to the
5334 same memory location. We could handle the general case of symbol
5335 aliasing, but a general symbol alias can only be generated in
5336 assembler code, handling it correctly would be very time
5337 consuming, and other ELF linkers don't handle general aliasing
5338 either. */
5339 if (weaks != NULL)
5340 {
5341 struct elf_link_hash_entry **hpp;
5342 struct elf_link_hash_entry **hppend;
5343 struct elf_link_hash_entry **sorted_sym_hash;
5344 struct elf_link_hash_entry *h;
5345 size_t sym_count;
5346
5347 /* Since we have to search the whole symbol list for each weak
5348 defined symbol, search time for N weak defined symbols will be
5349 O(N^2). Binary search will cut it down to O(NlogN). */
5350 amt = extsymcount;
5351 amt *= sizeof (struct elf_link_hash_entry *);
5352 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5353 if (sorted_sym_hash == NULL)
5354 goto error_return;
5355 sym_hash = sorted_sym_hash;
5356 hpp = elf_sym_hashes (abfd);
5357 hppend = hpp + extsymcount;
5358 sym_count = 0;
5359 for (; hpp < hppend; hpp++)
5360 {
5361 h = *hpp;
5362 if (h != NULL
5363 && h->root.type == bfd_link_hash_defined
5364 && !bed->is_function_type (h->type))
5365 {
5366 *sym_hash = h;
5367 sym_hash++;
5368 sym_count++;
5369 }
5370 }
5371
5372 qsort (sorted_sym_hash, sym_count,
5373 sizeof (struct elf_link_hash_entry *),
5374 elf_sort_symbol);
5375
5376 while (weaks != NULL)
5377 {
5378 struct elf_link_hash_entry *hlook;
5379 asection *slook;
5380 bfd_vma vlook;
5381 size_t i, j, idx = 0;
5382
5383 hlook = weaks;
5384 weaks = hlook->u.alias;
5385 hlook->u.alias = NULL;
5386
5387 if (hlook->root.type != bfd_link_hash_defined
5388 && hlook->root.type != bfd_link_hash_defweak)
5389 continue;
5390
5391 slook = hlook->root.u.def.section;
5392 vlook = hlook->root.u.def.value;
5393
5394 i = 0;
5395 j = sym_count;
5396 while (i != j)
5397 {
5398 bfd_signed_vma vdiff;
5399 idx = (i + j) / 2;
5400 h = sorted_sym_hash[idx];
5401 vdiff = vlook - h->root.u.def.value;
5402 if (vdiff < 0)
5403 j = idx;
5404 else if (vdiff > 0)
5405 i = idx + 1;
5406 else
5407 {
5408 int sdiff = slook->id - h->root.u.def.section->id;
5409 if (sdiff < 0)
5410 j = idx;
5411 else if (sdiff > 0)
5412 i = idx + 1;
5413 else
5414 break;
5415 }
5416 }
5417
5418 /* We didn't find a value/section match. */
5419 if (i == j)
5420 continue;
5421
5422 /* With multiple aliases, or when the weak symbol is already
5423 strongly defined, we have multiple matching symbols and
5424 the binary search above may land on any of them. Step
5425 one past the matching symbol(s). */
5426 while (++idx != j)
5427 {
5428 h = sorted_sym_hash[idx];
5429 if (h->root.u.def.section != slook
5430 || h->root.u.def.value != vlook)
5431 break;
5432 }
5433
5434 /* Now look back over the aliases. Since we sorted by size
5435 as well as value and section, we'll choose the one with
5436 the largest size. */
5437 while (idx-- != i)
5438 {
5439 h = sorted_sym_hash[idx];
5440
5441 /* Stop if value or section doesn't match. */
5442 if (h->root.u.def.section != slook
5443 || h->root.u.def.value != vlook)
5444 break;
5445 else if (h != hlook)
5446 {
5447 struct elf_link_hash_entry *t;
5448
5449 hlook->u.alias = h;
5450 hlook->is_weakalias = 1;
5451 t = h;
5452 if (t->u.alias != NULL)
5453 while (t->u.alias != h)
5454 t = t->u.alias;
5455 t->u.alias = hlook;
5456
5457 /* If the weak definition is in the list of dynamic
5458 symbols, make sure the real definition is put
5459 there as well. */
5460 if (hlook->dynindx != -1 && h->dynindx == -1)
5461 {
5462 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5463 {
5464 err_free_sym_hash:
5465 free (sorted_sym_hash);
5466 goto error_return;
5467 }
5468 }
5469
5470 /* If the real definition is in the list of dynamic
5471 symbols, make sure the weak definition is put
5472 there as well. If we don't do this, then the
5473 dynamic loader might not merge the entries for the
5474 real definition and the weak definition. */
5475 if (h->dynindx != -1 && hlook->dynindx == -1)
5476 {
5477 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5478 goto err_free_sym_hash;
5479 }
5480 break;
5481 }
5482 }
5483 }
5484
5485 free (sorted_sym_hash);
5486 }
5487
5488 if (bed->check_directives
5489 && !(*bed->check_directives) (abfd, info))
5490 return FALSE;
5491
5492 /* If this is a non-traditional link, try to optimize the handling
5493 of the .stab/.stabstr sections. */
5494 if (! dynamic
5495 && ! info->traditional_format
5496 && is_elf_hash_table (htab)
5497 && (info->strip != strip_all && info->strip != strip_debugger))
5498 {
5499 asection *stabstr;
5500
5501 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5502 if (stabstr != NULL)
5503 {
5504 bfd_size_type string_offset = 0;
5505 asection *stab;
5506
5507 for (stab = abfd->sections; stab; stab = stab->next)
5508 if (CONST_STRNEQ (stab->name, ".stab")
5509 && (!stab->name[5] ||
5510 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5511 && (stab->flags & SEC_MERGE) == 0
5512 && !bfd_is_abs_section (stab->output_section))
5513 {
5514 struct bfd_elf_section_data *secdata;
5515
5516 secdata = elf_section_data (stab);
5517 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5518 stabstr, &secdata->sec_info,
5519 &string_offset))
5520 goto error_return;
5521 if (secdata->sec_info)
5522 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5523 }
5524 }
5525 }
5526
5527 if (is_elf_hash_table (htab) && add_needed)
5528 {
5529 /* Add this bfd to the loaded list. */
5530 struct elf_link_loaded_list *n;
5531
5532 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5533 if (n == NULL)
5534 goto error_return;
5535 n->abfd = abfd;
5536 n->next = htab->loaded;
5537 htab->loaded = n;
5538 }
5539
5540 return TRUE;
5541
5542 error_free_vers:
5543 if (old_tab != NULL)
5544 free (old_tab);
5545 if (old_strtab != NULL)
5546 free (old_strtab);
5547 if (nondeflt_vers != NULL)
5548 free (nondeflt_vers);
5549 if (extversym != NULL)
5550 free (extversym);
5551 error_free_sym:
5552 if (isymbuf != NULL)
5553 free (isymbuf);
5554 error_return:
5555 return FALSE;
5556 }
5557
5558 /* Return the linker hash table entry of a symbol that might be
5559 satisfied by an archive symbol. Return -1 on error. */
5560
5561 struct elf_link_hash_entry *
5562 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5563 struct bfd_link_info *info,
5564 const char *name)
5565 {
5566 struct elf_link_hash_entry *h;
5567 char *p, *copy;
5568 size_t len, first;
5569
5570 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5571 if (h != NULL)
5572 return h;
5573
5574 /* If this is a default version (the name contains @@), look up the
5575 symbol again with only one `@' as well as without the version.
5576 The effect is that references to the symbol with and without the
5577 version will be matched by the default symbol in the archive. */
5578
5579 p = strchr (name, ELF_VER_CHR);
5580 if (p == NULL || p[1] != ELF_VER_CHR)
5581 return h;
5582
5583 /* First check with only one `@'. */
5584 len = strlen (name);
5585 copy = (char *) bfd_alloc (abfd, len);
5586 if (copy == NULL)
5587 return (struct elf_link_hash_entry *) -1;
5588
5589 first = p - name + 1;
5590 memcpy (copy, name, first);
5591 memcpy (copy + first, name + first + 1, len - first);
5592
5593 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5594 if (h == NULL)
5595 {
5596 /* We also need to check references to the symbol without the
5597 version. */
5598 copy[first - 1] = '\0';
5599 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5600 FALSE, FALSE, TRUE);
5601 }
5602
5603 bfd_release (abfd, copy);
5604 return h;
5605 }
5606
5607 /* Add symbols from an ELF archive file to the linker hash table. We
5608 don't use _bfd_generic_link_add_archive_symbols because we need to
5609 handle versioned symbols.
5610
5611 Fortunately, ELF archive handling is simpler than that done by
5612 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5613 oddities. In ELF, if we find a symbol in the archive map, and the
5614 symbol is currently undefined, we know that we must pull in that
5615 object file.
5616
5617 Unfortunately, we do have to make multiple passes over the symbol
5618 table until nothing further is resolved. */
5619
5620 static bfd_boolean
5621 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5622 {
5623 symindex c;
5624 unsigned char *included = NULL;
5625 carsym *symdefs;
5626 bfd_boolean loop;
5627 bfd_size_type amt;
5628 const struct elf_backend_data *bed;
5629 struct elf_link_hash_entry * (*archive_symbol_lookup)
5630 (bfd *, struct bfd_link_info *, const char *);
5631
5632 if (! bfd_has_map (abfd))
5633 {
5634 /* An empty archive is a special case. */
5635 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5636 return TRUE;
5637 bfd_set_error (bfd_error_no_armap);
5638 return FALSE;
5639 }
5640
5641 /* Keep track of all symbols we know to be already defined, and all
5642 files we know to be already included. This is to speed up the
5643 second and subsequent passes. */
5644 c = bfd_ardata (abfd)->symdef_count;
5645 if (c == 0)
5646 return TRUE;
5647 amt = c;
5648 amt *= sizeof (*included);
5649 included = (unsigned char *) bfd_zmalloc (amt);
5650 if (included == NULL)
5651 return FALSE;
5652
5653 symdefs = bfd_ardata (abfd)->symdefs;
5654 bed = get_elf_backend_data (abfd);
5655 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5656
5657 do
5658 {
5659 file_ptr last;
5660 symindex i;
5661 carsym *symdef;
5662 carsym *symdefend;
5663
5664 loop = FALSE;
5665 last = -1;
5666
5667 symdef = symdefs;
5668 symdefend = symdef + c;
5669 for (i = 0; symdef < symdefend; symdef++, i++)
5670 {
5671 struct elf_link_hash_entry *h;
5672 bfd *element;
5673 struct bfd_link_hash_entry *undefs_tail;
5674 symindex mark;
5675
5676 if (included[i])
5677 continue;
5678 if (symdef->file_offset == last)
5679 {
5680 included[i] = TRUE;
5681 continue;
5682 }
5683
5684 h = archive_symbol_lookup (abfd, info, symdef->name);
5685 if (h == (struct elf_link_hash_entry *) -1)
5686 goto error_return;
5687
5688 if (h == NULL)
5689 continue;
5690
5691 if (h->root.type == bfd_link_hash_common)
5692 {
5693 /* We currently have a common symbol. The archive map contains
5694 a reference to this symbol, so we may want to include it. We
5695 only want to include it however, if this archive element
5696 contains a definition of the symbol, not just another common
5697 declaration of it.
5698
5699 Unfortunately some archivers (including GNU ar) will put
5700 declarations of common symbols into their archive maps, as
5701 well as real definitions, so we cannot just go by the archive
5702 map alone. Instead we must read in the element's symbol
5703 table and check that to see what kind of symbol definition
5704 this is. */
5705 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5706 continue;
5707 }
5708 else if (h->root.type != bfd_link_hash_undefined)
5709 {
5710 if (h->root.type != bfd_link_hash_undefweak)
5711 /* Symbol must be defined. Don't check it again. */
5712 included[i] = TRUE;
5713 continue;
5714 }
5715
5716 /* We need to include this archive member. */
5717 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5718 if (element == NULL)
5719 goto error_return;
5720
5721 if (! bfd_check_format (element, bfd_object))
5722 goto error_return;
5723
5724 undefs_tail = info->hash->undefs_tail;
5725
5726 if (!(*info->callbacks
5727 ->add_archive_element) (info, element, symdef->name, &element))
5728 continue;
5729 if (!bfd_link_add_symbols (element, info))
5730 goto error_return;
5731
5732 /* If there are any new undefined symbols, we need to make
5733 another pass through the archive in order to see whether
5734 they can be defined. FIXME: This isn't perfect, because
5735 common symbols wind up on undefs_tail and because an
5736 undefined symbol which is defined later on in this pass
5737 does not require another pass. This isn't a bug, but it
5738 does make the code less efficient than it could be. */
5739 if (undefs_tail != info->hash->undefs_tail)
5740 loop = TRUE;
5741
5742 /* Look backward to mark all symbols from this object file
5743 which we have already seen in this pass. */
5744 mark = i;
5745 do
5746 {
5747 included[mark] = TRUE;
5748 if (mark == 0)
5749 break;
5750 --mark;
5751 }
5752 while (symdefs[mark].file_offset == symdef->file_offset);
5753
5754 /* We mark subsequent symbols from this object file as we go
5755 on through the loop. */
5756 last = symdef->file_offset;
5757 }
5758 }
5759 while (loop);
5760
5761 free (included);
5762
5763 return TRUE;
5764
5765 error_return:
5766 if (included != NULL)
5767 free (included);
5768 return FALSE;
5769 }
5770
5771 /* Given an ELF BFD, add symbols to the global hash table as
5772 appropriate. */
5773
5774 bfd_boolean
5775 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5776 {
5777 switch (bfd_get_format (abfd))
5778 {
5779 case bfd_object:
5780 return elf_link_add_object_symbols (abfd, info);
5781 case bfd_archive:
5782 return elf_link_add_archive_symbols (abfd, info);
5783 default:
5784 bfd_set_error (bfd_error_wrong_format);
5785 return FALSE;
5786 }
5787 }
5788 \f
5789 struct hash_codes_info
5790 {
5791 unsigned long *hashcodes;
5792 bfd_boolean error;
5793 };
5794
5795 /* This function will be called though elf_link_hash_traverse to store
5796 all hash value of the exported symbols in an array. */
5797
5798 static bfd_boolean
5799 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5800 {
5801 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5802 const char *name;
5803 unsigned long ha;
5804 char *alc = NULL;
5805
5806 /* Ignore indirect symbols. These are added by the versioning code. */
5807 if (h->dynindx == -1)
5808 return TRUE;
5809
5810 name = h->root.root.string;
5811 if (h->versioned >= versioned)
5812 {
5813 char *p = strchr (name, ELF_VER_CHR);
5814 if (p != NULL)
5815 {
5816 alc = (char *) bfd_malloc (p - name + 1);
5817 if (alc == NULL)
5818 {
5819 inf->error = TRUE;
5820 return FALSE;
5821 }
5822 memcpy (alc, name, p - name);
5823 alc[p - name] = '\0';
5824 name = alc;
5825 }
5826 }
5827
5828 /* Compute the hash value. */
5829 ha = bfd_elf_hash (name);
5830
5831 /* Store the found hash value in the array given as the argument. */
5832 *(inf->hashcodes)++ = ha;
5833
5834 /* And store it in the struct so that we can put it in the hash table
5835 later. */
5836 h->u.elf_hash_value = ha;
5837
5838 if (alc != NULL)
5839 free (alc);
5840
5841 return TRUE;
5842 }
5843
5844 struct collect_gnu_hash_codes
5845 {
5846 bfd *output_bfd;
5847 const struct elf_backend_data *bed;
5848 unsigned long int nsyms;
5849 unsigned long int maskbits;
5850 unsigned long int *hashcodes;
5851 unsigned long int *hashval;
5852 unsigned long int *indx;
5853 unsigned long int *counts;
5854 bfd_vma *bitmask;
5855 bfd_byte *contents;
5856 bfd_size_type xlat;
5857 long int min_dynindx;
5858 unsigned long int bucketcount;
5859 unsigned long int symindx;
5860 long int local_indx;
5861 long int shift1, shift2;
5862 unsigned long int mask;
5863 bfd_boolean error;
5864 };
5865
5866 /* This function will be called though elf_link_hash_traverse to store
5867 all hash value of the exported symbols in an array. */
5868
5869 static bfd_boolean
5870 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5871 {
5872 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5873 const char *name;
5874 unsigned long ha;
5875 char *alc = NULL;
5876
5877 /* Ignore indirect symbols. These are added by the versioning code. */
5878 if (h->dynindx == -1)
5879 return TRUE;
5880
5881 /* Ignore also local symbols and undefined symbols. */
5882 if (! (*s->bed->elf_hash_symbol) (h))
5883 return TRUE;
5884
5885 name = h->root.root.string;
5886 if (h->versioned >= versioned)
5887 {
5888 char *p = strchr (name, ELF_VER_CHR);
5889 if (p != NULL)
5890 {
5891 alc = (char *) bfd_malloc (p - name + 1);
5892 if (alc == NULL)
5893 {
5894 s->error = TRUE;
5895 return FALSE;
5896 }
5897 memcpy (alc, name, p - name);
5898 alc[p - name] = '\0';
5899 name = alc;
5900 }
5901 }
5902
5903 /* Compute the hash value. */
5904 ha = bfd_elf_gnu_hash (name);
5905
5906 /* Store the found hash value in the array for compute_bucket_count,
5907 and also for .dynsym reordering purposes. */
5908 s->hashcodes[s->nsyms] = ha;
5909 s->hashval[h->dynindx] = ha;
5910 ++s->nsyms;
5911 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5912 s->min_dynindx = h->dynindx;
5913
5914 if (alc != NULL)
5915 free (alc);
5916
5917 return TRUE;
5918 }
5919
5920 /* This function will be called though elf_link_hash_traverse to do
5921 final dynamic symbol renumbering in case of .gnu.hash.
5922 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5923 to the translation table. */
5924
5925 static bfd_boolean
5926 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5927 {
5928 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5929 unsigned long int bucket;
5930 unsigned long int val;
5931
5932 /* Ignore indirect symbols. */
5933 if (h->dynindx == -1)
5934 return TRUE;
5935
5936 /* Ignore also local symbols and undefined symbols. */
5937 if (! (*s->bed->elf_hash_symbol) (h))
5938 {
5939 if (h->dynindx >= s->min_dynindx)
5940 {
5941 if (s->bed->record_xhash_symbol != NULL)
5942 {
5943 (*s->bed->record_xhash_symbol) (h, 0);
5944 s->local_indx++;
5945 }
5946 else
5947 h->dynindx = s->local_indx++;
5948 }
5949 return TRUE;
5950 }
5951
5952 bucket = s->hashval[h->dynindx] % s->bucketcount;
5953 val = (s->hashval[h->dynindx] >> s->shift1)
5954 & ((s->maskbits >> s->shift1) - 1);
5955 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5956 s->bitmask[val]
5957 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5958 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5959 if (s->counts[bucket] == 1)
5960 /* Last element terminates the chain. */
5961 val |= 1;
5962 bfd_put_32 (s->output_bfd, val,
5963 s->contents + (s->indx[bucket] - s->symindx) * 4);
5964 --s->counts[bucket];
5965 if (s->bed->record_xhash_symbol != NULL)
5966 {
5967 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5968
5969 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5970 }
5971 else
5972 h->dynindx = s->indx[bucket]++;
5973 return TRUE;
5974 }
5975
5976 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5977
5978 bfd_boolean
5979 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5980 {
5981 return !(h->forced_local
5982 || h->root.type == bfd_link_hash_undefined
5983 || h->root.type == bfd_link_hash_undefweak
5984 || ((h->root.type == bfd_link_hash_defined
5985 || h->root.type == bfd_link_hash_defweak)
5986 && h->root.u.def.section->output_section == NULL));
5987 }
5988
5989 /* Array used to determine the number of hash table buckets to use
5990 based on the number of symbols there are. If there are fewer than
5991 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5992 fewer than 37 we use 17 buckets, and so forth. We never use more
5993 than 32771 buckets. */
5994
5995 static const size_t elf_buckets[] =
5996 {
5997 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5998 16411, 32771, 0
5999 };
6000
6001 /* Compute bucket count for hashing table. We do not use a static set
6002 of possible tables sizes anymore. Instead we determine for all
6003 possible reasonable sizes of the table the outcome (i.e., the
6004 number of collisions etc) and choose the best solution. The
6005 weighting functions are not too simple to allow the table to grow
6006 without bounds. Instead one of the weighting factors is the size.
6007 Therefore the result is always a good payoff between few collisions
6008 (= short chain lengths) and table size. */
6009 static size_t
6010 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6011 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6012 unsigned long int nsyms,
6013 int gnu_hash)
6014 {
6015 size_t best_size = 0;
6016 unsigned long int i;
6017
6018 /* We have a problem here. The following code to optimize the table
6019 size requires an integer type with more the 32 bits. If
6020 BFD_HOST_U_64_BIT is set we know about such a type. */
6021 #ifdef BFD_HOST_U_64_BIT
6022 if (info->optimize)
6023 {
6024 size_t minsize;
6025 size_t maxsize;
6026 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6027 bfd *dynobj = elf_hash_table (info)->dynobj;
6028 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6029 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6030 unsigned long int *counts;
6031 bfd_size_type amt;
6032 unsigned int no_improvement_count = 0;
6033
6034 /* Possible optimization parameters: if we have NSYMS symbols we say
6035 that the hashing table must at least have NSYMS/4 and at most
6036 2*NSYMS buckets. */
6037 minsize = nsyms / 4;
6038 if (minsize == 0)
6039 minsize = 1;
6040 best_size = maxsize = nsyms * 2;
6041 if (gnu_hash)
6042 {
6043 if (minsize < 2)
6044 minsize = 2;
6045 if ((best_size & 31) == 0)
6046 ++best_size;
6047 }
6048
6049 /* Create array where we count the collisions in. We must use bfd_malloc
6050 since the size could be large. */
6051 amt = maxsize;
6052 amt *= sizeof (unsigned long int);
6053 counts = (unsigned long int *) bfd_malloc (amt);
6054 if (counts == NULL)
6055 return 0;
6056
6057 /* Compute the "optimal" size for the hash table. The criteria is a
6058 minimal chain length. The minor criteria is (of course) the size
6059 of the table. */
6060 for (i = minsize; i < maxsize; ++i)
6061 {
6062 /* Walk through the array of hashcodes and count the collisions. */
6063 BFD_HOST_U_64_BIT max;
6064 unsigned long int j;
6065 unsigned long int fact;
6066
6067 if (gnu_hash && (i & 31) == 0)
6068 continue;
6069
6070 memset (counts, '\0', i * sizeof (unsigned long int));
6071
6072 /* Determine how often each hash bucket is used. */
6073 for (j = 0; j < nsyms; ++j)
6074 ++counts[hashcodes[j] % i];
6075
6076 /* For the weight function we need some information about the
6077 pagesize on the target. This is information need not be 100%
6078 accurate. Since this information is not available (so far) we
6079 define it here to a reasonable default value. If it is crucial
6080 to have a better value some day simply define this value. */
6081 # ifndef BFD_TARGET_PAGESIZE
6082 # define BFD_TARGET_PAGESIZE (4096)
6083 # endif
6084
6085 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6086 and the chains. */
6087 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6088
6089 # if 1
6090 /* Variant 1: optimize for short chains. We add the squares
6091 of all the chain lengths (which favors many small chain
6092 over a few long chains). */
6093 for (j = 0; j < i; ++j)
6094 max += counts[j] * counts[j];
6095
6096 /* This adds penalties for the overall size of the table. */
6097 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6098 max *= fact * fact;
6099 # else
6100 /* Variant 2: Optimize a lot more for small table. Here we
6101 also add squares of the size but we also add penalties for
6102 empty slots (the +1 term). */
6103 for (j = 0; j < i; ++j)
6104 max += (1 + counts[j]) * (1 + counts[j]);
6105
6106 /* The overall size of the table is considered, but not as
6107 strong as in variant 1, where it is squared. */
6108 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6109 max *= fact;
6110 # endif
6111
6112 /* Compare with current best results. */
6113 if (max < best_chlen)
6114 {
6115 best_chlen = max;
6116 best_size = i;
6117 no_improvement_count = 0;
6118 }
6119 /* PR 11843: Avoid futile long searches for the best bucket size
6120 when there are a large number of symbols. */
6121 else if (++no_improvement_count == 100)
6122 break;
6123 }
6124
6125 free (counts);
6126 }
6127 else
6128 #endif /* defined (BFD_HOST_U_64_BIT) */
6129 {
6130 /* This is the fallback solution if no 64bit type is available or if we
6131 are not supposed to spend much time on optimizations. We select the
6132 bucket count using a fixed set of numbers. */
6133 for (i = 0; elf_buckets[i] != 0; i++)
6134 {
6135 best_size = elf_buckets[i];
6136 if (nsyms < elf_buckets[i + 1])
6137 break;
6138 }
6139 if (gnu_hash && best_size < 2)
6140 best_size = 2;
6141 }
6142
6143 return best_size;
6144 }
6145
6146 /* Size any SHT_GROUP section for ld -r. */
6147
6148 bfd_boolean
6149 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6150 {
6151 bfd *ibfd;
6152 asection *s;
6153
6154 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6155 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6156 && (s = ibfd->sections) != NULL
6157 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6158 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6159 return FALSE;
6160 return TRUE;
6161 }
6162
6163 /* Set a default stack segment size. The value in INFO wins. If it
6164 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6165 undefined it is initialized. */
6166
6167 bfd_boolean
6168 bfd_elf_stack_segment_size (bfd *output_bfd,
6169 struct bfd_link_info *info,
6170 const char *legacy_symbol,
6171 bfd_vma default_size)
6172 {
6173 struct elf_link_hash_entry *h = NULL;
6174
6175 /* Look for legacy symbol. */
6176 if (legacy_symbol)
6177 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6178 FALSE, FALSE, FALSE);
6179 if (h && (h->root.type == bfd_link_hash_defined
6180 || h->root.type == bfd_link_hash_defweak)
6181 && h->def_regular
6182 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6183 {
6184 /* The symbol has no type if specified on the command line. */
6185 h->type = STT_OBJECT;
6186 if (info->stacksize)
6187 /* xgettext:c-format */
6188 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6189 output_bfd, legacy_symbol);
6190 else if (h->root.u.def.section != bfd_abs_section_ptr)
6191 /* xgettext:c-format */
6192 _bfd_error_handler (_("%pB: %s not absolute"),
6193 output_bfd, legacy_symbol);
6194 else
6195 info->stacksize = h->root.u.def.value;
6196 }
6197
6198 if (!info->stacksize)
6199 /* If the user didn't set a size, or explicitly inhibit the
6200 size, set it now. */
6201 info->stacksize = default_size;
6202
6203 /* Provide the legacy symbol, if it is referenced. */
6204 if (h && (h->root.type == bfd_link_hash_undefined
6205 || h->root.type == bfd_link_hash_undefweak))
6206 {
6207 struct bfd_link_hash_entry *bh = NULL;
6208
6209 if (!(_bfd_generic_link_add_one_symbol
6210 (info, output_bfd, legacy_symbol,
6211 BSF_GLOBAL, bfd_abs_section_ptr,
6212 info->stacksize >= 0 ? info->stacksize : 0,
6213 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6214 return FALSE;
6215
6216 h = (struct elf_link_hash_entry *) bh;
6217 h->def_regular = 1;
6218 h->type = STT_OBJECT;
6219 }
6220
6221 return TRUE;
6222 }
6223
6224 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6225
6226 struct elf_gc_sweep_symbol_info
6227 {
6228 struct bfd_link_info *info;
6229 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6230 bfd_boolean);
6231 };
6232
6233 static bfd_boolean
6234 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6235 {
6236 if (!h->mark
6237 && (((h->root.type == bfd_link_hash_defined
6238 || h->root.type == bfd_link_hash_defweak)
6239 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6240 && h->root.u.def.section->gc_mark))
6241 || h->root.type == bfd_link_hash_undefined
6242 || h->root.type == bfd_link_hash_undefweak))
6243 {
6244 struct elf_gc_sweep_symbol_info *inf;
6245
6246 inf = (struct elf_gc_sweep_symbol_info *) data;
6247 (*inf->hide_symbol) (inf->info, h, TRUE);
6248 h->def_regular = 0;
6249 h->ref_regular = 0;
6250 h->ref_regular_nonweak = 0;
6251 }
6252
6253 return TRUE;
6254 }
6255
6256 /* Set up the sizes and contents of the ELF dynamic sections. This is
6257 called by the ELF linker emulation before_allocation routine. We
6258 must set the sizes of the sections before the linker sets the
6259 addresses of the various sections. */
6260
6261 bfd_boolean
6262 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6263 const char *soname,
6264 const char *rpath,
6265 const char *filter_shlib,
6266 const char *audit,
6267 const char *depaudit,
6268 const char * const *auxiliary_filters,
6269 struct bfd_link_info *info,
6270 asection **sinterpptr)
6271 {
6272 bfd *dynobj;
6273 const struct elf_backend_data *bed;
6274
6275 *sinterpptr = NULL;
6276
6277 if (!is_elf_hash_table (info->hash))
6278 return TRUE;
6279
6280 dynobj = elf_hash_table (info)->dynobj;
6281
6282 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6283 {
6284 struct bfd_elf_version_tree *verdefs;
6285 struct elf_info_failed asvinfo;
6286 struct bfd_elf_version_tree *t;
6287 struct bfd_elf_version_expr *d;
6288 asection *s;
6289 size_t soname_indx;
6290
6291 /* If we are supposed to export all symbols into the dynamic symbol
6292 table (this is not the normal case), then do so. */
6293 if (info->export_dynamic
6294 || (bfd_link_executable (info) && info->dynamic))
6295 {
6296 struct elf_info_failed eif;
6297
6298 eif.info = info;
6299 eif.failed = FALSE;
6300 elf_link_hash_traverse (elf_hash_table (info),
6301 _bfd_elf_export_symbol,
6302 &eif);
6303 if (eif.failed)
6304 return FALSE;
6305 }
6306
6307 if (soname != NULL)
6308 {
6309 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6310 soname, TRUE);
6311 if (soname_indx == (size_t) -1
6312 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6313 return FALSE;
6314 }
6315 else
6316 soname_indx = (size_t) -1;
6317
6318 /* Make all global versions with definition. */
6319 for (t = info->version_info; t != NULL; t = t->next)
6320 for (d = t->globals.list; d != NULL; d = d->next)
6321 if (!d->symver && d->literal)
6322 {
6323 const char *verstr, *name;
6324 size_t namelen, verlen, newlen;
6325 char *newname, *p, leading_char;
6326 struct elf_link_hash_entry *newh;
6327
6328 leading_char = bfd_get_symbol_leading_char (output_bfd);
6329 name = d->pattern;
6330 namelen = strlen (name) + (leading_char != '\0');
6331 verstr = t->name;
6332 verlen = strlen (verstr);
6333 newlen = namelen + verlen + 3;
6334
6335 newname = (char *) bfd_malloc (newlen);
6336 if (newname == NULL)
6337 return FALSE;
6338 newname[0] = leading_char;
6339 memcpy (newname + (leading_char != '\0'), name, namelen);
6340
6341 /* Check the hidden versioned definition. */
6342 p = newname + namelen;
6343 *p++ = ELF_VER_CHR;
6344 memcpy (p, verstr, verlen + 1);
6345 newh = elf_link_hash_lookup (elf_hash_table (info),
6346 newname, FALSE, FALSE,
6347 FALSE);
6348 if (newh == NULL
6349 || (newh->root.type != bfd_link_hash_defined
6350 && newh->root.type != bfd_link_hash_defweak))
6351 {
6352 /* Check the default versioned definition. */
6353 *p++ = ELF_VER_CHR;
6354 memcpy (p, verstr, verlen + 1);
6355 newh = elf_link_hash_lookup (elf_hash_table (info),
6356 newname, FALSE, FALSE,
6357 FALSE);
6358 }
6359 free (newname);
6360
6361 /* Mark this version if there is a definition and it is
6362 not defined in a shared object. */
6363 if (newh != NULL
6364 && !newh->def_dynamic
6365 && (newh->root.type == bfd_link_hash_defined
6366 || newh->root.type == bfd_link_hash_defweak))
6367 d->symver = 1;
6368 }
6369
6370 /* Attach all the symbols to their version information. */
6371 asvinfo.info = info;
6372 asvinfo.failed = FALSE;
6373
6374 elf_link_hash_traverse (elf_hash_table (info),
6375 _bfd_elf_link_assign_sym_version,
6376 &asvinfo);
6377 if (asvinfo.failed)
6378 return FALSE;
6379
6380 if (!info->allow_undefined_version)
6381 {
6382 /* Check if all global versions have a definition. */
6383 bfd_boolean all_defined = TRUE;
6384 for (t = info->version_info; t != NULL; t = t->next)
6385 for (d = t->globals.list; d != NULL; d = d->next)
6386 if (d->literal && !d->symver && !d->script)
6387 {
6388 _bfd_error_handler
6389 (_("%s: undefined version: %s"),
6390 d->pattern, t->name);
6391 all_defined = FALSE;
6392 }
6393
6394 if (!all_defined)
6395 {
6396 bfd_set_error (bfd_error_bad_value);
6397 return FALSE;
6398 }
6399 }
6400
6401 /* Set up the version definition section. */
6402 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6403 BFD_ASSERT (s != NULL);
6404
6405 /* We may have created additional version definitions if we are
6406 just linking a regular application. */
6407 verdefs = info->version_info;
6408
6409 /* Skip anonymous version tag. */
6410 if (verdefs != NULL && verdefs->vernum == 0)
6411 verdefs = verdefs->next;
6412
6413 if (verdefs == NULL && !info->create_default_symver)
6414 s->flags |= SEC_EXCLUDE;
6415 else
6416 {
6417 unsigned int cdefs;
6418 bfd_size_type size;
6419 bfd_byte *p;
6420 Elf_Internal_Verdef def;
6421 Elf_Internal_Verdaux defaux;
6422 struct bfd_link_hash_entry *bh;
6423 struct elf_link_hash_entry *h;
6424 const char *name;
6425
6426 cdefs = 0;
6427 size = 0;
6428
6429 /* Make space for the base version. */
6430 size += sizeof (Elf_External_Verdef);
6431 size += sizeof (Elf_External_Verdaux);
6432 ++cdefs;
6433
6434 /* Make space for the default version. */
6435 if (info->create_default_symver)
6436 {
6437 size += sizeof (Elf_External_Verdef);
6438 ++cdefs;
6439 }
6440
6441 for (t = verdefs; t != NULL; t = t->next)
6442 {
6443 struct bfd_elf_version_deps *n;
6444
6445 /* Don't emit base version twice. */
6446 if (t->vernum == 0)
6447 continue;
6448
6449 size += sizeof (Elf_External_Verdef);
6450 size += sizeof (Elf_External_Verdaux);
6451 ++cdefs;
6452
6453 for (n = t->deps; n != NULL; n = n->next)
6454 size += sizeof (Elf_External_Verdaux);
6455 }
6456
6457 s->size = size;
6458 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6459 if (s->contents == NULL && s->size != 0)
6460 return FALSE;
6461
6462 /* Fill in the version definition section. */
6463
6464 p = s->contents;
6465
6466 def.vd_version = VER_DEF_CURRENT;
6467 def.vd_flags = VER_FLG_BASE;
6468 def.vd_ndx = 1;
6469 def.vd_cnt = 1;
6470 if (info->create_default_symver)
6471 {
6472 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6473 def.vd_next = sizeof (Elf_External_Verdef);
6474 }
6475 else
6476 {
6477 def.vd_aux = sizeof (Elf_External_Verdef);
6478 def.vd_next = (sizeof (Elf_External_Verdef)
6479 + sizeof (Elf_External_Verdaux));
6480 }
6481
6482 if (soname_indx != (size_t) -1)
6483 {
6484 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6485 soname_indx);
6486 def.vd_hash = bfd_elf_hash (soname);
6487 defaux.vda_name = soname_indx;
6488 name = soname;
6489 }
6490 else
6491 {
6492 size_t indx;
6493
6494 name = lbasename (output_bfd->filename);
6495 def.vd_hash = bfd_elf_hash (name);
6496 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6497 name, FALSE);
6498 if (indx == (size_t) -1)
6499 return FALSE;
6500 defaux.vda_name = indx;
6501 }
6502 defaux.vda_next = 0;
6503
6504 _bfd_elf_swap_verdef_out (output_bfd, &def,
6505 (Elf_External_Verdef *) p);
6506 p += sizeof (Elf_External_Verdef);
6507 if (info->create_default_symver)
6508 {
6509 /* Add a symbol representing this version. */
6510 bh = NULL;
6511 if (! (_bfd_generic_link_add_one_symbol
6512 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6513 0, NULL, FALSE,
6514 get_elf_backend_data (dynobj)->collect, &bh)))
6515 return FALSE;
6516 h = (struct elf_link_hash_entry *) bh;
6517 h->non_elf = 0;
6518 h->def_regular = 1;
6519 h->type = STT_OBJECT;
6520 h->verinfo.vertree = NULL;
6521
6522 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6523 return FALSE;
6524
6525 /* Create a duplicate of the base version with the same
6526 aux block, but different flags. */
6527 def.vd_flags = 0;
6528 def.vd_ndx = 2;
6529 def.vd_aux = sizeof (Elf_External_Verdef);
6530 if (verdefs)
6531 def.vd_next = (sizeof (Elf_External_Verdef)
6532 + sizeof (Elf_External_Verdaux));
6533 else
6534 def.vd_next = 0;
6535 _bfd_elf_swap_verdef_out (output_bfd, &def,
6536 (Elf_External_Verdef *) p);
6537 p += sizeof (Elf_External_Verdef);
6538 }
6539 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6540 (Elf_External_Verdaux *) p);
6541 p += sizeof (Elf_External_Verdaux);
6542
6543 for (t = verdefs; t != NULL; t = t->next)
6544 {
6545 unsigned int cdeps;
6546 struct bfd_elf_version_deps *n;
6547
6548 /* Don't emit the base version twice. */
6549 if (t->vernum == 0)
6550 continue;
6551
6552 cdeps = 0;
6553 for (n = t->deps; n != NULL; n = n->next)
6554 ++cdeps;
6555
6556 /* Add a symbol representing this version. */
6557 bh = NULL;
6558 if (! (_bfd_generic_link_add_one_symbol
6559 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6560 0, NULL, FALSE,
6561 get_elf_backend_data (dynobj)->collect, &bh)))
6562 return FALSE;
6563 h = (struct elf_link_hash_entry *) bh;
6564 h->non_elf = 0;
6565 h->def_regular = 1;
6566 h->type = STT_OBJECT;
6567 h->verinfo.vertree = t;
6568
6569 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6570 return FALSE;
6571
6572 def.vd_version = VER_DEF_CURRENT;
6573 def.vd_flags = 0;
6574 if (t->globals.list == NULL
6575 && t->locals.list == NULL
6576 && ! t->used)
6577 def.vd_flags |= VER_FLG_WEAK;
6578 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6579 def.vd_cnt = cdeps + 1;
6580 def.vd_hash = bfd_elf_hash (t->name);
6581 def.vd_aux = sizeof (Elf_External_Verdef);
6582 def.vd_next = 0;
6583
6584 /* If a basever node is next, it *must* be the last node in
6585 the chain, otherwise Verdef construction breaks. */
6586 if (t->next != NULL && t->next->vernum == 0)
6587 BFD_ASSERT (t->next->next == NULL);
6588
6589 if (t->next != NULL && t->next->vernum != 0)
6590 def.vd_next = (sizeof (Elf_External_Verdef)
6591 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6592
6593 _bfd_elf_swap_verdef_out (output_bfd, &def,
6594 (Elf_External_Verdef *) p);
6595 p += sizeof (Elf_External_Verdef);
6596
6597 defaux.vda_name = h->dynstr_index;
6598 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6599 h->dynstr_index);
6600 defaux.vda_next = 0;
6601 if (t->deps != NULL)
6602 defaux.vda_next = sizeof (Elf_External_Verdaux);
6603 t->name_indx = defaux.vda_name;
6604
6605 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6606 (Elf_External_Verdaux *) p);
6607 p += sizeof (Elf_External_Verdaux);
6608
6609 for (n = t->deps; n != NULL; n = n->next)
6610 {
6611 if (n->version_needed == NULL)
6612 {
6613 /* This can happen if there was an error in the
6614 version script. */
6615 defaux.vda_name = 0;
6616 }
6617 else
6618 {
6619 defaux.vda_name = n->version_needed->name_indx;
6620 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6621 defaux.vda_name);
6622 }
6623 if (n->next == NULL)
6624 defaux.vda_next = 0;
6625 else
6626 defaux.vda_next = sizeof (Elf_External_Verdaux);
6627
6628 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6629 (Elf_External_Verdaux *) p);
6630 p += sizeof (Elf_External_Verdaux);
6631 }
6632 }
6633
6634 elf_tdata (output_bfd)->cverdefs = cdefs;
6635 }
6636 }
6637
6638 bed = get_elf_backend_data (output_bfd);
6639
6640 if (info->gc_sections && bed->can_gc_sections)
6641 {
6642 struct elf_gc_sweep_symbol_info sweep_info;
6643
6644 /* Remove the symbols that were in the swept sections from the
6645 dynamic symbol table. */
6646 sweep_info.info = info;
6647 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6648 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6649 &sweep_info);
6650 }
6651
6652 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6653 {
6654 asection *s;
6655 struct elf_find_verdep_info sinfo;
6656
6657 /* Work out the size of the version reference section. */
6658
6659 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6660 BFD_ASSERT (s != NULL);
6661
6662 sinfo.info = info;
6663 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6664 if (sinfo.vers == 0)
6665 sinfo.vers = 1;
6666 sinfo.failed = FALSE;
6667
6668 elf_link_hash_traverse (elf_hash_table (info),
6669 _bfd_elf_link_find_version_dependencies,
6670 &sinfo);
6671 if (sinfo.failed)
6672 return FALSE;
6673
6674 if (elf_tdata (output_bfd)->verref == NULL)
6675 s->flags |= SEC_EXCLUDE;
6676 else
6677 {
6678 Elf_Internal_Verneed *vn;
6679 unsigned int size;
6680 unsigned int crefs;
6681 bfd_byte *p;
6682
6683 /* Build the version dependency section. */
6684 size = 0;
6685 crefs = 0;
6686 for (vn = elf_tdata (output_bfd)->verref;
6687 vn != NULL;
6688 vn = vn->vn_nextref)
6689 {
6690 Elf_Internal_Vernaux *a;
6691
6692 size += sizeof (Elf_External_Verneed);
6693 ++crefs;
6694 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6695 size += sizeof (Elf_External_Vernaux);
6696 }
6697
6698 s->size = size;
6699 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6700 if (s->contents == NULL)
6701 return FALSE;
6702
6703 p = s->contents;
6704 for (vn = elf_tdata (output_bfd)->verref;
6705 vn != NULL;
6706 vn = vn->vn_nextref)
6707 {
6708 unsigned int caux;
6709 Elf_Internal_Vernaux *a;
6710 size_t indx;
6711
6712 caux = 0;
6713 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6714 ++caux;
6715
6716 vn->vn_version = VER_NEED_CURRENT;
6717 vn->vn_cnt = caux;
6718 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6719 elf_dt_name (vn->vn_bfd) != NULL
6720 ? elf_dt_name (vn->vn_bfd)
6721 : lbasename (vn->vn_bfd->filename),
6722 FALSE);
6723 if (indx == (size_t) -1)
6724 return FALSE;
6725 vn->vn_file = indx;
6726 vn->vn_aux = sizeof (Elf_External_Verneed);
6727 if (vn->vn_nextref == NULL)
6728 vn->vn_next = 0;
6729 else
6730 vn->vn_next = (sizeof (Elf_External_Verneed)
6731 + caux * sizeof (Elf_External_Vernaux));
6732
6733 _bfd_elf_swap_verneed_out (output_bfd, vn,
6734 (Elf_External_Verneed *) p);
6735 p += sizeof (Elf_External_Verneed);
6736
6737 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6738 {
6739 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6740 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6741 a->vna_nodename, FALSE);
6742 if (indx == (size_t) -1)
6743 return FALSE;
6744 a->vna_name = indx;
6745 if (a->vna_nextptr == NULL)
6746 a->vna_next = 0;
6747 else
6748 a->vna_next = sizeof (Elf_External_Vernaux);
6749
6750 _bfd_elf_swap_vernaux_out (output_bfd, a,
6751 (Elf_External_Vernaux *) p);
6752 p += sizeof (Elf_External_Vernaux);
6753 }
6754 }
6755
6756 elf_tdata (output_bfd)->cverrefs = crefs;
6757 }
6758 }
6759
6760 /* Any syms created from now on start with -1 in
6761 got.refcount/offset and plt.refcount/offset. */
6762 elf_hash_table (info)->init_got_refcount
6763 = elf_hash_table (info)->init_got_offset;
6764 elf_hash_table (info)->init_plt_refcount
6765 = elf_hash_table (info)->init_plt_offset;
6766
6767 if (bfd_link_relocatable (info)
6768 && !_bfd_elf_size_group_sections (info))
6769 return FALSE;
6770
6771 /* The backend may have to create some sections regardless of whether
6772 we're dynamic or not. */
6773 if (bed->elf_backend_always_size_sections
6774 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6775 return FALSE;
6776
6777 /* Determine any GNU_STACK segment requirements, after the backend
6778 has had a chance to set a default segment size. */
6779 if (info->execstack)
6780 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6781 else if (info->noexecstack)
6782 elf_stack_flags (output_bfd) = PF_R | PF_W;
6783 else
6784 {
6785 bfd *inputobj;
6786 asection *notesec = NULL;
6787 int exec = 0;
6788
6789 for (inputobj = info->input_bfds;
6790 inputobj;
6791 inputobj = inputobj->link.next)
6792 {
6793 asection *s;
6794
6795 if (inputobj->flags
6796 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6797 continue;
6798 s = inputobj->sections;
6799 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6800 continue;
6801
6802 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6803 if (s)
6804 {
6805 if (s->flags & SEC_CODE)
6806 exec = PF_X;
6807 notesec = s;
6808 }
6809 else if (bed->default_execstack)
6810 exec = PF_X;
6811 }
6812 if (notesec || info->stacksize > 0)
6813 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6814 if (notesec && exec && bfd_link_relocatable (info)
6815 && notesec->output_section != bfd_abs_section_ptr)
6816 notesec->output_section->flags |= SEC_CODE;
6817 }
6818
6819 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6820 {
6821 struct elf_info_failed eif;
6822 struct elf_link_hash_entry *h;
6823 asection *dynstr;
6824 asection *s;
6825
6826 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6827 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6828
6829 if (info->symbolic)
6830 {
6831 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6832 return FALSE;
6833 info->flags |= DF_SYMBOLIC;
6834 }
6835
6836 if (rpath != NULL)
6837 {
6838 size_t indx;
6839 bfd_vma tag;
6840
6841 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6842 TRUE);
6843 if (indx == (size_t) -1)
6844 return FALSE;
6845
6846 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6847 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6848 return FALSE;
6849 }
6850
6851 if (filter_shlib != NULL)
6852 {
6853 size_t indx;
6854
6855 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6856 filter_shlib, TRUE);
6857 if (indx == (size_t) -1
6858 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6859 return FALSE;
6860 }
6861
6862 if (auxiliary_filters != NULL)
6863 {
6864 const char * const *p;
6865
6866 for (p = auxiliary_filters; *p != NULL; p++)
6867 {
6868 size_t indx;
6869
6870 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6871 *p, TRUE);
6872 if (indx == (size_t) -1
6873 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6874 return FALSE;
6875 }
6876 }
6877
6878 if (audit != NULL)
6879 {
6880 size_t indx;
6881
6882 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6883 TRUE);
6884 if (indx == (size_t) -1
6885 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6886 return FALSE;
6887 }
6888
6889 if (depaudit != NULL)
6890 {
6891 size_t indx;
6892
6893 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6894 TRUE);
6895 if (indx == (size_t) -1
6896 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6897 return FALSE;
6898 }
6899
6900 eif.info = info;
6901 eif.failed = FALSE;
6902
6903 /* Find all symbols which were defined in a dynamic object and make
6904 the backend pick a reasonable value for them. */
6905 elf_link_hash_traverse (elf_hash_table (info),
6906 _bfd_elf_adjust_dynamic_symbol,
6907 &eif);
6908 if (eif.failed)
6909 return FALSE;
6910
6911 /* Add some entries to the .dynamic section. We fill in some of the
6912 values later, in bfd_elf_final_link, but we must add the entries
6913 now so that we know the final size of the .dynamic section. */
6914
6915 /* If there are initialization and/or finalization functions to
6916 call then add the corresponding DT_INIT/DT_FINI entries. */
6917 h = (info->init_function
6918 ? elf_link_hash_lookup (elf_hash_table (info),
6919 info->init_function, FALSE,
6920 FALSE, FALSE)
6921 : NULL);
6922 if (h != NULL
6923 && (h->ref_regular
6924 || h->def_regular))
6925 {
6926 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6927 return FALSE;
6928 }
6929 h = (info->fini_function
6930 ? elf_link_hash_lookup (elf_hash_table (info),
6931 info->fini_function, FALSE,
6932 FALSE, FALSE)
6933 : NULL);
6934 if (h != NULL
6935 && (h->ref_regular
6936 || h->def_regular))
6937 {
6938 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6939 return FALSE;
6940 }
6941
6942 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6943 if (s != NULL && s->linker_has_input)
6944 {
6945 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6946 if (! bfd_link_executable (info))
6947 {
6948 bfd *sub;
6949 asection *o;
6950
6951 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6952 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6953 && (o = sub->sections) != NULL
6954 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6955 for (o = sub->sections; o != NULL; o = o->next)
6956 if (elf_section_data (o)->this_hdr.sh_type
6957 == SHT_PREINIT_ARRAY)
6958 {
6959 _bfd_error_handler
6960 (_("%pB: .preinit_array section is not allowed in DSO"),
6961 sub);
6962 break;
6963 }
6964
6965 bfd_set_error (bfd_error_nonrepresentable_section);
6966 return FALSE;
6967 }
6968
6969 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6970 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6971 return FALSE;
6972 }
6973 s = bfd_get_section_by_name (output_bfd, ".init_array");
6974 if (s != NULL && s->linker_has_input)
6975 {
6976 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6977 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6978 return FALSE;
6979 }
6980 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6981 if (s != NULL && s->linker_has_input)
6982 {
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6984 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6985 return FALSE;
6986 }
6987
6988 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6989 /* If .dynstr is excluded from the link, we don't want any of
6990 these tags. Strictly, we should be checking each section
6991 individually; This quick check covers for the case where
6992 someone does a /DISCARD/ : { *(*) }. */
6993 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6994 {
6995 bfd_size_type strsize;
6996
6997 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6998 if ((info->emit_hash
6999 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7000 || (info->emit_gnu_hash
7001 && (bed->record_xhash_symbol == NULL
7002 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7003 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7004 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7005 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7006 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7007 bed->s->sizeof_sym))
7008 return FALSE;
7009 }
7010 }
7011
7012 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7013 return FALSE;
7014
7015 /* The backend must work out the sizes of all the other dynamic
7016 sections. */
7017 if (dynobj != NULL
7018 && bed->elf_backend_size_dynamic_sections != NULL
7019 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7020 return FALSE;
7021
7022 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7023 {
7024 if (elf_tdata (output_bfd)->cverdefs)
7025 {
7026 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7027
7028 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7029 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7030 return FALSE;
7031 }
7032
7033 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7034 {
7035 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7036 return FALSE;
7037 }
7038 else if (info->flags & DF_BIND_NOW)
7039 {
7040 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7041 return FALSE;
7042 }
7043
7044 if (info->flags_1)
7045 {
7046 if (bfd_link_executable (info))
7047 info->flags_1 &= ~ (DF_1_INITFIRST
7048 | DF_1_NODELETE
7049 | DF_1_NOOPEN);
7050 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7051 return FALSE;
7052 }
7053
7054 if (elf_tdata (output_bfd)->cverrefs)
7055 {
7056 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7057
7058 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7059 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7060 return FALSE;
7061 }
7062
7063 if ((elf_tdata (output_bfd)->cverrefs == 0
7064 && elf_tdata (output_bfd)->cverdefs == 0)
7065 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7066 {
7067 asection *s;
7068
7069 s = bfd_get_linker_section (dynobj, ".gnu.version");
7070 s->flags |= SEC_EXCLUDE;
7071 }
7072 }
7073 return TRUE;
7074 }
7075
7076 /* Find the first non-excluded output section. We'll use its
7077 section symbol for some emitted relocs. */
7078 void
7079 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7080 {
7081 asection *s;
7082 asection *found = NULL;
7083
7084 for (s = output_bfd->sections; s != NULL; s = s->next)
7085 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7086 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7087 {
7088 found = s;
7089 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7090 break;
7091 }
7092 elf_hash_table (info)->text_index_section = found;
7093 }
7094
7095 /* Find two non-excluded output sections, one for code, one for data.
7096 We'll use their section symbols for some emitted relocs. */
7097 void
7098 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7099 {
7100 asection *s;
7101 asection *found = NULL;
7102
7103 /* Data first, since setting text_index_section changes
7104 _bfd_elf_omit_section_dynsym_default. */
7105 for (s = output_bfd->sections; s != NULL; s = s->next)
7106 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7107 && !(s->flags & SEC_READONLY)
7108 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7109 {
7110 found = s;
7111 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7112 break;
7113 }
7114 elf_hash_table (info)->data_index_section = found;
7115
7116 for (s = output_bfd->sections; s != NULL; s = s->next)
7117 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7118 && (s->flags & SEC_READONLY)
7119 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7120 {
7121 found = s;
7122 break;
7123 }
7124 elf_hash_table (info)->text_index_section = found;
7125 }
7126
7127 #define GNU_HASH_SECTION_NAME(bed) \
7128 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7129
7130 bfd_boolean
7131 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7132 {
7133 const struct elf_backend_data *bed;
7134 unsigned long section_sym_count;
7135 bfd_size_type dynsymcount = 0;
7136
7137 if (!is_elf_hash_table (info->hash))
7138 return TRUE;
7139
7140 bed = get_elf_backend_data (output_bfd);
7141 (*bed->elf_backend_init_index_section) (output_bfd, info);
7142
7143 /* Assign dynsym indices. In a shared library we generate a section
7144 symbol for each output section, which come first. Next come all
7145 of the back-end allocated local dynamic syms, followed by the rest
7146 of the global symbols.
7147
7148 This is usually not needed for static binaries, however backends
7149 can request to always do it, e.g. the MIPS backend uses dynamic
7150 symbol counts to lay out GOT, which will be produced in the
7151 presence of GOT relocations even in static binaries (holding fixed
7152 data in that case, to satisfy those relocations). */
7153
7154 if (elf_hash_table (info)->dynamic_sections_created
7155 || bed->always_renumber_dynsyms)
7156 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7157 &section_sym_count);
7158
7159 if (elf_hash_table (info)->dynamic_sections_created)
7160 {
7161 bfd *dynobj;
7162 asection *s;
7163 unsigned int dtagcount;
7164
7165 dynobj = elf_hash_table (info)->dynobj;
7166
7167 /* Work out the size of the symbol version section. */
7168 s = bfd_get_linker_section (dynobj, ".gnu.version");
7169 BFD_ASSERT (s != NULL);
7170 if ((s->flags & SEC_EXCLUDE) == 0)
7171 {
7172 s->size = dynsymcount * sizeof (Elf_External_Versym);
7173 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7174 if (s->contents == NULL)
7175 return FALSE;
7176
7177 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7178 return FALSE;
7179 }
7180
7181 /* Set the size of the .dynsym and .hash sections. We counted
7182 the number of dynamic symbols in elf_link_add_object_symbols.
7183 We will build the contents of .dynsym and .hash when we build
7184 the final symbol table, because until then we do not know the
7185 correct value to give the symbols. We built the .dynstr
7186 section as we went along in elf_link_add_object_symbols. */
7187 s = elf_hash_table (info)->dynsym;
7188 BFD_ASSERT (s != NULL);
7189 s->size = dynsymcount * bed->s->sizeof_sym;
7190
7191 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7192 if (s->contents == NULL)
7193 return FALSE;
7194
7195 /* The first entry in .dynsym is a dummy symbol. Clear all the
7196 section syms, in case we don't output them all. */
7197 ++section_sym_count;
7198 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7199
7200 elf_hash_table (info)->bucketcount = 0;
7201
7202 /* Compute the size of the hashing table. As a side effect this
7203 computes the hash values for all the names we export. */
7204 if (info->emit_hash)
7205 {
7206 unsigned long int *hashcodes;
7207 struct hash_codes_info hashinf;
7208 bfd_size_type amt;
7209 unsigned long int nsyms;
7210 size_t bucketcount;
7211 size_t hash_entry_size;
7212
7213 /* Compute the hash values for all exported symbols. At the same
7214 time store the values in an array so that we could use them for
7215 optimizations. */
7216 amt = dynsymcount * sizeof (unsigned long int);
7217 hashcodes = (unsigned long int *) bfd_malloc (amt);
7218 if (hashcodes == NULL)
7219 return FALSE;
7220 hashinf.hashcodes = hashcodes;
7221 hashinf.error = FALSE;
7222
7223 /* Put all hash values in HASHCODES. */
7224 elf_link_hash_traverse (elf_hash_table (info),
7225 elf_collect_hash_codes, &hashinf);
7226 if (hashinf.error)
7227 {
7228 free (hashcodes);
7229 return FALSE;
7230 }
7231
7232 nsyms = hashinf.hashcodes - hashcodes;
7233 bucketcount
7234 = compute_bucket_count (info, hashcodes, nsyms, 0);
7235 free (hashcodes);
7236
7237 if (bucketcount == 0 && nsyms > 0)
7238 return FALSE;
7239
7240 elf_hash_table (info)->bucketcount = bucketcount;
7241
7242 s = bfd_get_linker_section (dynobj, ".hash");
7243 BFD_ASSERT (s != NULL);
7244 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7245 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7246 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7247 if (s->contents == NULL)
7248 return FALSE;
7249
7250 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7251 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7252 s->contents + hash_entry_size);
7253 }
7254
7255 if (info->emit_gnu_hash)
7256 {
7257 size_t i, cnt;
7258 unsigned char *contents;
7259 struct collect_gnu_hash_codes cinfo;
7260 bfd_size_type amt;
7261 size_t bucketcount;
7262
7263 memset (&cinfo, 0, sizeof (cinfo));
7264
7265 /* Compute the hash values for all exported symbols. At the same
7266 time store the values in an array so that we could use them for
7267 optimizations. */
7268 amt = dynsymcount * 2 * sizeof (unsigned long int);
7269 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7270 if (cinfo.hashcodes == NULL)
7271 return FALSE;
7272
7273 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7274 cinfo.min_dynindx = -1;
7275 cinfo.output_bfd = output_bfd;
7276 cinfo.bed = bed;
7277
7278 /* Put all hash values in HASHCODES. */
7279 elf_link_hash_traverse (elf_hash_table (info),
7280 elf_collect_gnu_hash_codes, &cinfo);
7281 if (cinfo.error)
7282 {
7283 free (cinfo.hashcodes);
7284 return FALSE;
7285 }
7286
7287 bucketcount
7288 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7289
7290 if (bucketcount == 0)
7291 {
7292 free (cinfo.hashcodes);
7293 return FALSE;
7294 }
7295
7296 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7297 BFD_ASSERT (s != NULL);
7298
7299 if (cinfo.nsyms == 0)
7300 {
7301 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7302 BFD_ASSERT (cinfo.min_dynindx == -1);
7303 free (cinfo.hashcodes);
7304 s->size = 5 * 4 + bed->s->arch_size / 8;
7305 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7306 if (contents == NULL)
7307 return FALSE;
7308 s->contents = contents;
7309 /* 1 empty bucket. */
7310 bfd_put_32 (output_bfd, 1, contents);
7311 /* SYMIDX above the special symbol 0. */
7312 bfd_put_32 (output_bfd, 1, contents + 4);
7313 /* Just one word for bitmask. */
7314 bfd_put_32 (output_bfd, 1, contents + 8);
7315 /* Only hash fn bloom filter. */
7316 bfd_put_32 (output_bfd, 0, contents + 12);
7317 /* No hashes are valid - empty bitmask. */
7318 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7319 /* No hashes in the only bucket. */
7320 bfd_put_32 (output_bfd, 0,
7321 contents + 16 + bed->s->arch_size / 8);
7322 }
7323 else
7324 {
7325 unsigned long int maskwords, maskbitslog2, x;
7326 BFD_ASSERT (cinfo.min_dynindx != -1);
7327
7328 x = cinfo.nsyms;
7329 maskbitslog2 = 1;
7330 while ((x >>= 1) != 0)
7331 ++maskbitslog2;
7332 if (maskbitslog2 < 3)
7333 maskbitslog2 = 5;
7334 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7335 maskbitslog2 = maskbitslog2 + 3;
7336 else
7337 maskbitslog2 = maskbitslog2 + 2;
7338 if (bed->s->arch_size == 64)
7339 {
7340 if (maskbitslog2 == 5)
7341 maskbitslog2 = 6;
7342 cinfo.shift1 = 6;
7343 }
7344 else
7345 cinfo.shift1 = 5;
7346 cinfo.mask = (1 << cinfo.shift1) - 1;
7347 cinfo.shift2 = maskbitslog2;
7348 cinfo.maskbits = 1 << maskbitslog2;
7349 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7350 amt = bucketcount * sizeof (unsigned long int) * 2;
7351 amt += maskwords * sizeof (bfd_vma);
7352 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7353 if (cinfo.bitmask == NULL)
7354 {
7355 free (cinfo.hashcodes);
7356 return FALSE;
7357 }
7358
7359 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7360 cinfo.indx = cinfo.counts + bucketcount;
7361 cinfo.symindx = dynsymcount - cinfo.nsyms;
7362 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7363
7364 /* Determine how often each hash bucket is used. */
7365 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7366 for (i = 0; i < cinfo.nsyms; ++i)
7367 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7368
7369 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7370 if (cinfo.counts[i] != 0)
7371 {
7372 cinfo.indx[i] = cnt;
7373 cnt += cinfo.counts[i];
7374 }
7375 BFD_ASSERT (cnt == dynsymcount);
7376 cinfo.bucketcount = bucketcount;
7377 cinfo.local_indx = cinfo.min_dynindx;
7378
7379 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7380 s->size += cinfo.maskbits / 8;
7381 if (bed->record_xhash_symbol != NULL)
7382 s->size += cinfo.nsyms * 4;
7383 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7384 if (contents == NULL)
7385 {
7386 free (cinfo.bitmask);
7387 free (cinfo.hashcodes);
7388 return FALSE;
7389 }
7390
7391 s->contents = contents;
7392 bfd_put_32 (output_bfd, bucketcount, contents);
7393 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7394 bfd_put_32 (output_bfd, maskwords, contents + 8);
7395 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7396 contents += 16 + cinfo.maskbits / 8;
7397
7398 for (i = 0; i < bucketcount; ++i)
7399 {
7400 if (cinfo.counts[i] == 0)
7401 bfd_put_32 (output_bfd, 0, contents);
7402 else
7403 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7404 contents += 4;
7405 }
7406
7407 cinfo.contents = contents;
7408
7409 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7410 /* Renumber dynamic symbols, if populating .gnu.hash section.
7411 If using .MIPS.xhash, populate the translation table. */
7412 elf_link_hash_traverse (elf_hash_table (info),
7413 elf_gnu_hash_process_symidx, &cinfo);
7414
7415 contents = s->contents + 16;
7416 for (i = 0; i < maskwords; ++i)
7417 {
7418 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7419 contents);
7420 contents += bed->s->arch_size / 8;
7421 }
7422
7423 free (cinfo.bitmask);
7424 free (cinfo.hashcodes);
7425 }
7426 }
7427
7428 s = bfd_get_linker_section (dynobj, ".dynstr");
7429 BFD_ASSERT (s != NULL);
7430
7431 elf_finalize_dynstr (output_bfd, info);
7432
7433 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7434
7435 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7436 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7437 return FALSE;
7438 }
7439
7440 return TRUE;
7441 }
7442 \f
7443 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7444
7445 static void
7446 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7447 asection *sec)
7448 {
7449 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7450 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7451 }
7452
7453 /* Finish SHF_MERGE section merging. */
7454
7455 bfd_boolean
7456 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7457 {
7458 bfd *ibfd;
7459 asection *sec;
7460
7461 if (!is_elf_hash_table (info->hash))
7462 return FALSE;
7463
7464 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7465 if ((ibfd->flags & DYNAMIC) == 0
7466 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7467 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7468 == get_elf_backend_data (obfd)->s->elfclass))
7469 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7470 if ((sec->flags & SEC_MERGE) != 0
7471 && !bfd_is_abs_section (sec->output_section))
7472 {
7473 struct bfd_elf_section_data *secdata;
7474
7475 secdata = elf_section_data (sec);
7476 if (! _bfd_add_merge_section (obfd,
7477 &elf_hash_table (info)->merge_info,
7478 sec, &secdata->sec_info))
7479 return FALSE;
7480 else if (secdata->sec_info)
7481 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7482 }
7483
7484 if (elf_hash_table (info)->merge_info != NULL)
7485 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7486 merge_sections_remove_hook);
7487 return TRUE;
7488 }
7489
7490 /* Create an entry in an ELF linker hash table. */
7491
7492 struct bfd_hash_entry *
7493 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7494 struct bfd_hash_table *table,
7495 const char *string)
7496 {
7497 /* Allocate the structure if it has not already been allocated by a
7498 subclass. */
7499 if (entry == NULL)
7500 {
7501 entry = (struct bfd_hash_entry *)
7502 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7503 if (entry == NULL)
7504 return entry;
7505 }
7506
7507 /* Call the allocation method of the superclass. */
7508 entry = _bfd_link_hash_newfunc (entry, table, string);
7509 if (entry != NULL)
7510 {
7511 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7512 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7513
7514 /* Set local fields. */
7515 ret->indx = -1;
7516 ret->dynindx = -1;
7517 ret->got = htab->init_got_refcount;
7518 ret->plt = htab->init_plt_refcount;
7519 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7520 - offsetof (struct elf_link_hash_entry, size)));
7521 /* Assume that we have been called by a non-ELF symbol reader.
7522 This flag is then reset by the code which reads an ELF input
7523 file. This ensures that a symbol created by a non-ELF symbol
7524 reader will have the flag set correctly. */
7525 ret->non_elf = 1;
7526 }
7527
7528 return entry;
7529 }
7530
7531 /* Copy data from an indirect symbol to its direct symbol, hiding the
7532 old indirect symbol. Also used for copying flags to a weakdef. */
7533
7534 void
7535 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7536 struct elf_link_hash_entry *dir,
7537 struct elf_link_hash_entry *ind)
7538 {
7539 struct elf_link_hash_table *htab;
7540
7541 /* Copy down any references that we may have already seen to the
7542 symbol which just became indirect. */
7543
7544 if (dir->versioned != versioned_hidden)
7545 dir->ref_dynamic |= ind->ref_dynamic;
7546 dir->ref_regular |= ind->ref_regular;
7547 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7548 dir->non_got_ref |= ind->non_got_ref;
7549 dir->needs_plt |= ind->needs_plt;
7550 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7551
7552 if (ind->root.type != bfd_link_hash_indirect)
7553 return;
7554
7555 /* Copy over the global and procedure linkage table refcount entries.
7556 These may have been already set up by a check_relocs routine. */
7557 htab = elf_hash_table (info);
7558 if (ind->got.refcount > htab->init_got_refcount.refcount)
7559 {
7560 if (dir->got.refcount < 0)
7561 dir->got.refcount = 0;
7562 dir->got.refcount += ind->got.refcount;
7563 ind->got.refcount = htab->init_got_refcount.refcount;
7564 }
7565
7566 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7567 {
7568 if (dir->plt.refcount < 0)
7569 dir->plt.refcount = 0;
7570 dir->plt.refcount += ind->plt.refcount;
7571 ind->plt.refcount = htab->init_plt_refcount.refcount;
7572 }
7573
7574 if (ind->dynindx != -1)
7575 {
7576 if (dir->dynindx != -1)
7577 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7578 dir->dynindx = ind->dynindx;
7579 dir->dynstr_index = ind->dynstr_index;
7580 ind->dynindx = -1;
7581 ind->dynstr_index = 0;
7582 }
7583 }
7584
7585 void
7586 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7587 struct elf_link_hash_entry *h,
7588 bfd_boolean force_local)
7589 {
7590 /* STT_GNU_IFUNC symbol must go through PLT. */
7591 if (h->type != STT_GNU_IFUNC)
7592 {
7593 h->plt = elf_hash_table (info)->init_plt_offset;
7594 h->needs_plt = 0;
7595 }
7596 if (force_local)
7597 {
7598 h->forced_local = 1;
7599 if (h->dynindx != -1)
7600 {
7601 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7602 h->dynstr_index);
7603 h->dynindx = -1;
7604 h->dynstr_index = 0;
7605 }
7606 }
7607 }
7608
7609 /* Hide a symbol. */
7610
7611 void
7612 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7613 struct bfd_link_info *info,
7614 struct bfd_link_hash_entry *h)
7615 {
7616 if (is_elf_hash_table (info->hash))
7617 {
7618 const struct elf_backend_data *bed
7619 = get_elf_backend_data (output_bfd);
7620 struct elf_link_hash_entry *eh
7621 = (struct elf_link_hash_entry *) h;
7622 bed->elf_backend_hide_symbol (info, eh, TRUE);
7623 eh->def_dynamic = 0;
7624 eh->ref_dynamic = 0;
7625 eh->dynamic_def = 0;
7626 }
7627 }
7628
7629 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7630 caller. */
7631
7632 bfd_boolean
7633 _bfd_elf_link_hash_table_init
7634 (struct elf_link_hash_table *table,
7635 bfd *abfd,
7636 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7637 struct bfd_hash_table *,
7638 const char *),
7639 unsigned int entsize,
7640 enum elf_target_id target_id)
7641 {
7642 bfd_boolean ret;
7643 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7644
7645 table->init_got_refcount.refcount = can_refcount - 1;
7646 table->init_plt_refcount.refcount = can_refcount - 1;
7647 table->init_got_offset.offset = -(bfd_vma) 1;
7648 table->init_plt_offset.offset = -(bfd_vma) 1;
7649 /* The first dynamic symbol is a dummy. */
7650 table->dynsymcount = 1;
7651
7652 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7653
7654 table->root.type = bfd_link_elf_hash_table;
7655 table->hash_table_id = target_id;
7656
7657 return ret;
7658 }
7659
7660 /* Create an ELF linker hash table. */
7661
7662 struct bfd_link_hash_table *
7663 _bfd_elf_link_hash_table_create (bfd *abfd)
7664 {
7665 struct elf_link_hash_table *ret;
7666 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7667
7668 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7669 if (ret == NULL)
7670 return NULL;
7671
7672 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7673 sizeof (struct elf_link_hash_entry),
7674 GENERIC_ELF_DATA))
7675 {
7676 free (ret);
7677 return NULL;
7678 }
7679 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7680
7681 return &ret->root;
7682 }
7683
7684 /* Destroy an ELF linker hash table. */
7685
7686 void
7687 _bfd_elf_link_hash_table_free (bfd *obfd)
7688 {
7689 struct elf_link_hash_table *htab;
7690
7691 htab = (struct elf_link_hash_table *) obfd->link.hash;
7692 if (htab->dynstr != NULL)
7693 _bfd_elf_strtab_free (htab->dynstr);
7694 _bfd_merge_sections_free (htab->merge_info);
7695 _bfd_generic_link_hash_table_free (obfd);
7696 }
7697
7698 /* This is a hook for the ELF emulation code in the generic linker to
7699 tell the backend linker what file name to use for the DT_NEEDED
7700 entry for a dynamic object. */
7701
7702 void
7703 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7704 {
7705 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7706 && bfd_get_format (abfd) == bfd_object)
7707 elf_dt_name (abfd) = name;
7708 }
7709
7710 int
7711 bfd_elf_get_dyn_lib_class (bfd *abfd)
7712 {
7713 int lib_class;
7714 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7715 && bfd_get_format (abfd) == bfd_object)
7716 lib_class = elf_dyn_lib_class (abfd);
7717 else
7718 lib_class = 0;
7719 return lib_class;
7720 }
7721
7722 void
7723 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7724 {
7725 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7726 && bfd_get_format (abfd) == bfd_object)
7727 elf_dyn_lib_class (abfd) = lib_class;
7728 }
7729
7730 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7731 the linker ELF emulation code. */
7732
7733 struct bfd_link_needed_list *
7734 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7735 struct bfd_link_info *info)
7736 {
7737 if (! is_elf_hash_table (info->hash))
7738 return NULL;
7739 return elf_hash_table (info)->needed;
7740 }
7741
7742 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7743 hook for the linker ELF emulation code. */
7744
7745 struct bfd_link_needed_list *
7746 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7747 struct bfd_link_info *info)
7748 {
7749 if (! is_elf_hash_table (info->hash))
7750 return NULL;
7751 return elf_hash_table (info)->runpath;
7752 }
7753
7754 /* Get the name actually used for a dynamic object for a link. This
7755 is the SONAME entry if there is one. Otherwise, it is the string
7756 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7757
7758 const char *
7759 bfd_elf_get_dt_soname (bfd *abfd)
7760 {
7761 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7762 && bfd_get_format (abfd) == bfd_object)
7763 return elf_dt_name (abfd);
7764 return NULL;
7765 }
7766
7767 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7768 the ELF linker emulation code. */
7769
7770 bfd_boolean
7771 bfd_elf_get_bfd_needed_list (bfd *abfd,
7772 struct bfd_link_needed_list **pneeded)
7773 {
7774 asection *s;
7775 bfd_byte *dynbuf = NULL;
7776 unsigned int elfsec;
7777 unsigned long shlink;
7778 bfd_byte *extdyn, *extdynend;
7779 size_t extdynsize;
7780 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7781
7782 *pneeded = NULL;
7783
7784 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7785 || bfd_get_format (abfd) != bfd_object)
7786 return TRUE;
7787
7788 s = bfd_get_section_by_name (abfd, ".dynamic");
7789 if (s == NULL || s->size == 0)
7790 return TRUE;
7791
7792 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7793 goto error_return;
7794
7795 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7796 if (elfsec == SHN_BAD)
7797 goto error_return;
7798
7799 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7800
7801 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7802 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7803
7804 extdyn = dynbuf;
7805 extdynend = extdyn + s->size;
7806 for (; extdyn < extdynend; extdyn += extdynsize)
7807 {
7808 Elf_Internal_Dyn dyn;
7809
7810 (*swap_dyn_in) (abfd, extdyn, &dyn);
7811
7812 if (dyn.d_tag == DT_NULL)
7813 break;
7814
7815 if (dyn.d_tag == DT_NEEDED)
7816 {
7817 const char *string;
7818 struct bfd_link_needed_list *l;
7819 unsigned int tagv = dyn.d_un.d_val;
7820 bfd_size_type amt;
7821
7822 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7823 if (string == NULL)
7824 goto error_return;
7825
7826 amt = sizeof *l;
7827 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7828 if (l == NULL)
7829 goto error_return;
7830
7831 l->by = abfd;
7832 l->name = string;
7833 l->next = *pneeded;
7834 *pneeded = l;
7835 }
7836 }
7837
7838 free (dynbuf);
7839
7840 return TRUE;
7841
7842 error_return:
7843 if (dynbuf != NULL)
7844 free (dynbuf);
7845 return FALSE;
7846 }
7847
7848 struct elf_symbuf_symbol
7849 {
7850 unsigned long st_name; /* Symbol name, index in string tbl */
7851 unsigned char st_info; /* Type and binding attributes */
7852 unsigned char st_other; /* Visibilty, and target specific */
7853 };
7854
7855 struct elf_symbuf_head
7856 {
7857 struct elf_symbuf_symbol *ssym;
7858 size_t count;
7859 unsigned int st_shndx;
7860 };
7861
7862 struct elf_symbol
7863 {
7864 union
7865 {
7866 Elf_Internal_Sym *isym;
7867 struct elf_symbuf_symbol *ssym;
7868 } u;
7869 const char *name;
7870 };
7871
7872 /* Sort references to symbols by ascending section number. */
7873
7874 static int
7875 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7876 {
7877 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7878 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7879
7880 return s1->st_shndx - s2->st_shndx;
7881 }
7882
7883 static int
7884 elf_sym_name_compare (const void *arg1, const void *arg2)
7885 {
7886 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7887 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7888 return strcmp (s1->name, s2->name);
7889 }
7890
7891 static struct elf_symbuf_head *
7892 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7893 {
7894 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7895 struct elf_symbuf_symbol *ssym;
7896 struct elf_symbuf_head *ssymbuf, *ssymhead;
7897 size_t i, shndx_count, total_size;
7898
7899 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7900 if (indbuf == NULL)
7901 return NULL;
7902
7903 for (ind = indbuf, i = 0; i < symcount; i++)
7904 if (isymbuf[i].st_shndx != SHN_UNDEF)
7905 *ind++ = &isymbuf[i];
7906 indbufend = ind;
7907
7908 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7909 elf_sort_elf_symbol);
7910
7911 shndx_count = 0;
7912 if (indbufend > indbuf)
7913 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7914 if (ind[0]->st_shndx != ind[1]->st_shndx)
7915 shndx_count++;
7916
7917 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7918 + (indbufend - indbuf) * sizeof (*ssym));
7919 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7920 if (ssymbuf == NULL)
7921 {
7922 free (indbuf);
7923 return NULL;
7924 }
7925
7926 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7927 ssymbuf->ssym = NULL;
7928 ssymbuf->count = shndx_count;
7929 ssymbuf->st_shndx = 0;
7930 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7931 {
7932 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7933 {
7934 ssymhead++;
7935 ssymhead->ssym = ssym;
7936 ssymhead->count = 0;
7937 ssymhead->st_shndx = (*ind)->st_shndx;
7938 }
7939 ssym->st_name = (*ind)->st_name;
7940 ssym->st_info = (*ind)->st_info;
7941 ssym->st_other = (*ind)->st_other;
7942 ssymhead->count++;
7943 }
7944 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7945 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7946 == total_size));
7947
7948 free (indbuf);
7949 return ssymbuf;
7950 }
7951
7952 /* Check if 2 sections define the same set of local and global
7953 symbols. */
7954
7955 static bfd_boolean
7956 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7957 struct bfd_link_info *info)
7958 {
7959 bfd *bfd1, *bfd2;
7960 const struct elf_backend_data *bed1, *bed2;
7961 Elf_Internal_Shdr *hdr1, *hdr2;
7962 size_t symcount1, symcount2;
7963 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7964 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7965 Elf_Internal_Sym *isym, *isymend;
7966 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7967 size_t count1, count2, i;
7968 unsigned int shndx1, shndx2;
7969 bfd_boolean result;
7970
7971 bfd1 = sec1->owner;
7972 bfd2 = sec2->owner;
7973
7974 /* Both sections have to be in ELF. */
7975 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7976 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7977 return FALSE;
7978
7979 if (elf_section_type (sec1) != elf_section_type (sec2))
7980 return FALSE;
7981
7982 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7983 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7984 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7985 return FALSE;
7986
7987 bed1 = get_elf_backend_data (bfd1);
7988 bed2 = get_elf_backend_data (bfd2);
7989 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7990 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7991 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7992 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7993
7994 if (symcount1 == 0 || symcount2 == 0)
7995 return FALSE;
7996
7997 result = FALSE;
7998 isymbuf1 = NULL;
7999 isymbuf2 = NULL;
8000 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8001 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8002
8003 if (ssymbuf1 == NULL)
8004 {
8005 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8006 NULL, NULL, NULL);
8007 if (isymbuf1 == NULL)
8008 goto done;
8009
8010 if (!info->reduce_memory_overheads)
8011 elf_tdata (bfd1)->symbuf = ssymbuf1
8012 = elf_create_symbuf (symcount1, isymbuf1);
8013 }
8014
8015 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8016 {
8017 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8018 NULL, NULL, NULL);
8019 if (isymbuf2 == NULL)
8020 goto done;
8021
8022 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8023 elf_tdata (bfd2)->symbuf = ssymbuf2
8024 = elf_create_symbuf (symcount2, isymbuf2);
8025 }
8026
8027 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8028 {
8029 /* Optimized faster version. */
8030 size_t lo, hi, mid;
8031 struct elf_symbol *symp;
8032 struct elf_symbuf_symbol *ssym, *ssymend;
8033
8034 lo = 0;
8035 hi = ssymbuf1->count;
8036 ssymbuf1++;
8037 count1 = 0;
8038 while (lo < hi)
8039 {
8040 mid = (lo + hi) / 2;
8041 if (shndx1 < ssymbuf1[mid].st_shndx)
8042 hi = mid;
8043 else if (shndx1 > ssymbuf1[mid].st_shndx)
8044 lo = mid + 1;
8045 else
8046 {
8047 count1 = ssymbuf1[mid].count;
8048 ssymbuf1 += mid;
8049 break;
8050 }
8051 }
8052
8053 lo = 0;
8054 hi = ssymbuf2->count;
8055 ssymbuf2++;
8056 count2 = 0;
8057 while (lo < hi)
8058 {
8059 mid = (lo + hi) / 2;
8060 if (shndx2 < ssymbuf2[mid].st_shndx)
8061 hi = mid;
8062 else if (shndx2 > ssymbuf2[mid].st_shndx)
8063 lo = mid + 1;
8064 else
8065 {
8066 count2 = ssymbuf2[mid].count;
8067 ssymbuf2 += mid;
8068 break;
8069 }
8070 }
8071
8072 if (count1 == 0 || count2 == 0 || count1 != count2)
8073 goto done;
8074
8075 symtable1
8076 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8077 symtable2
8078 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8079 if (symtable1 == NULL || symtable2 == NULL)
8080 goto done;
8081
8082 symp = symtable1;
8083 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8084 ssym < ssymend; ssym++, symp++)
8085 {
8086 symp->u.ssym = ssym;
8087 symp->name = bfd_elf_string_from_elf_section (bfd1,
8088 hdr1->sh_link,
8089 ssym->st_name);
8090 }
8091
8092 symp = symtable2;
8093 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8094 ssym < ssymend; ssym++, symp++)
8095 {
8096 symp->u.ssym = ssym;
8097 symp->name = bfd_elf_string_from_elf_section (bfd2,
8098 hdr2->sh_link,
8099 ssym->st_name);
8100 }
8101
8102 /* Sort symbol by name. */
8103 qsort (symtable1, count1, sizeof (struct elf_symbol),
8104 elf_sym_name_compare);
8105 qsort (symtable2, count1, sizeof (struct elf_symbol),
8106 elf_sym_name_compare);
8107
8108 for (i = 0; i < count1; i++)
8109 /* Two symbols must have the same binding, type and name. */
8110 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8111 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8112 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8113 goto done;
8114
8115 result = TRUE;
8116 goto done;
8117 }
8118
8119 symtable1 = (struct elf_symbol *)
8120 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8121 symtable2 = (struct elf_symbol *)
8122 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8123 if (symtable1 == NULL || symtable2 == NULL)
8124 goto done;
8125
8126 /* Count definitions in the section. */
8127 count1 = 0;
8128 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8129 if (isym->st_shndx == shndx1)
8130 symtable1[count1++].u.isym = isym;
8131
8132 count2 = 0;
8133 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8134 if (isym->st_shndx == shndx2)
8135 symtable2[count2++].u.isym = isym;
8136
8137 if (count1 == 0 || count2 == 0 || count1 != count2)
8138 goto done;
8139
8140 for (i = 0; i < count1; i++)
8141 symtable1[i].name
8142 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8143 symtable1[i].u.isym->st_name);
8144
8145 for (i = 0; i < count2; i++)
8146 symtable2[i].name
8147 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8148 symtable2[i].u.isym->st_name);
8149
8150 /* Sort symbol by name. */
8151 qsort (symtable1, count1, sizeof (struct elf_symbol),
8152 elf_sym_name_compare);
8153 qsort (symtable2, count1, sizeof (struct elf_symbol),
8154 elf_sym_name_compare);
8155
8156 for (i = 0; i < count1; i++)
8157 /* Two symbols must have the same binding, type and name. */
8158 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8159 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8160 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8161 goto done;
8162
8163 result = TRUE;
8164
8165 done:
8166 if (symtable1)
8167 free (symtable1);
8168 if (symtable2)
8169 free (symtable2);
8170 if (isymbuf1)
8171 free (isymbuf1);
8172 if (isymbuf2)
8173 free (isymbuf2);
8174
8175 return result;
8176 }
8177
8178 /* Return TRUE if 2 section types are compatible. */
8179
8180 bfd_boolean
8181 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8182 bfd *bbfd, const asection *bsec)
8183 {
8184 if (asec == NULL
8185 || bsec == NULL
8186 || abfd->xvec->flavour != bfd_target_elf_flavour
8187 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8188 return TRUE;
8189
8190 return elf_section_type (asec) == elf_section_type (bsec);
8191 }
8192 \f
8193 /* Final phase of ELF linker. */
8194
8195 /* A structure we use to avoid passing large numbers of arguments. */
8196
8197 struct elf_final_link_info
8198 {
8199 /* General link information. */
8200 struct bfd_link_info *info;
8201 /* Output BFD. */
8202 bfd *output_bfd;
8203 /* Symbol string table. */
8204 struct elf_strtab_hash *symstrtab;
8205 /* .hash section. */
8206 asection *hash_sec;
8207 /* symbol version section (.gnu.version). */
8208 asection *symver_sec;
8209 /* Buffer large enough to hold contents of any section. */
8210 bfd_byte *contents;
8211 /* Buffer large enough to hold external relocs of any section. */
8212 void *external_relocs;
8213 /* Buffer large enough to hold internal relocs of any section. */
8214 Elf_Internal_Rela *internal_relocs;
8215 /* Buffer large enough to hold external local symbols of any input
8216 BFD. */
8217 bfd_byte *external_syms;
8218 /* And a buffer for symbol section indices. */
8219 Elf_External_Sym_Shndx *locsym_shndx;
8220 /* Buffer large enough to hold internal local symbols of any input
8221 BFD. */
8222 Elf_Internal_Sym *internal_syms;
8223 /* Array large enough to hold a symbol index for each local symbol
8224 of any input BFD. */
8225 long *indices;
8226 /* Array large enough to hold a section pointer for each local
8227 symbol of any input BFD. */
8228 asection **sections;
8229 /* Buffer for SHT_SYMTAB_SHNDX section. */
8230 Elf_External_Sym_Shndx *symshndxbuf;
8231 /* Number of STT_FILE syms seen. */
8232 size_t filesym_count;
8233 };
8234
8235 /* This struct is used to pass information to elf_link_output_extsym. */
8236
8237 struct elf_outext_info
8238 {
8239 bfd_boolean failed;
8240 bfd_boolean localsyms;
8241 bfd_boolean file_sym_done;
8242 struct elf_final_link_info *flinfo;
8243 };
8244
8245
8246 /* Support for evaluating a complex relocation.
8247
8248 Complex relocations are generalized, self-describing relocations. The
8249 implementation of them consists of two parts: complex symbols, and the
8250 relocations themselves.
8251
8252 The relocations are use a reserved elf-wide relocation type code (R_RELC
8253 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8254 information (start bit, end bit, word width, etc) into the addend. This
8255 information is extracted from CGEN-generated operand tables within gas.
8256
8257 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8258 internal) representing prefix-notation expressions, including but not
8259 limited to those sorts of expressions normally encoded as addends in the
8260 addend field. The symbol mangling format is:
8261
8262 <node> := <literal>
8263 | <unary-operator> ':' <node>
8264 | <binary-operator> ':' <node> ':' <node>
8265 ;
8266
8267 <literal> := 's' <digits=N> ':' <N character symbol name>
8268 | 'S' <digits=N> ':' <N character section name>
8269 | '#' <hexdigits>
8270 ;
8271
8272 <binary-operator> := as in C
8273 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8274
8275 static void
8276 set_symbol_value (bfd *bfd_with_globals,
8277 Elf_Internal_Sym *isymbuf,
8278 size_t locsymcount,
8279 size_t symidx,
8280 bfd_vma val)
8281 {
8282 struct elf_link_hash_entry **sym_hashes;
8283 struct elf_link_hash_entry *h;
8284 size_t extsymoff = locsymcount;
8285
8286 if (symidx < locsymcount)
8287 {
8288 Elf_Internal_Sym *sym;
8289
8290 sym = isymbuf + symidx;
8291 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8292 {
8293 /* It is a local symbol: move it to the
8294 "absolute" section and give it a value. */
8295 sym->st_shndx = SHN_ABS;
8296 sym->st_value = val;
8297 return;
8298 }
8299 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8300 extsymoff = 0;
8301 }
8302
8303 /* It is a global symbol: set its link type
8304 to "defined" and give it a value. */
8305
8306 sym_hashes = elf_sym_hashes (bfd_with_globals);
8307 h = sym_hashes [symidx - extsymoff];
8308 while (h->root.type == bfd_link_hash_indirect
8309 || h->root.type == bfd_link_hash_warning)
8310 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8311 h->root.type = bfd_link_hash_defined;
8312 h->root.u.def.value = val;
8313 h->root.u.def.section = bfd_abs_section_ptr;
8314 }
8315
8316 static bfd_boolean
8317 resolve_symbol (const char *name,
8318 bfd *input_bfd,
8319 struct elf_final_link_info *flinfo,
8320 bfd_vma *result,
8321 Elf_Internal_Sym *isymbuf,
8322 size_t locsymcount)
8323 {
8324 Elf_Internal_Sym *sym;
8325 struct bfd_link_hash_entry *global_entry;
8326 const char *candidate = NULL;
8327 Elf_Internal_Shdr *symtab_hdr;
8328 size_t i;
8329
8330 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8331
8332 for (i = 0; i < locsymcount; ++ i)
8333 {
8334 sym = isymbuf + i;
8335
8336 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8337 continue;
8338
8339 candidate = bfd_elf_string_from_elf_section (input_bfd,
8340 symtab_hdr->sh_link,
8341 sym->st_name);
8342 #ifdef DEBUG
8343 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8344 name, candidate, (unsigned long) sym->st_value);
8345 #endif
8346 if (candidate && strcmp (candidate, name) == 0)
8347 {
8348 asection *sec = flinfo->sections [i];
8349
8350 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8351 *result += sec->output_offset + sec->output_section->vma;
8352 #ifdef DEBUG
8353 printf ("Found symbol with value %8.8lx\n",
8354 (unsigned long) *result);
8355 #endif
8356 return TRUE;
8357 }
8358 }
8359
8360 /* Hmm, haven't found it yet. perhaps it is a global. */
8361 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8362 FALSE, FALSE, TRUE);
8363 if (!global_entry)
8364 return FALSE;
8365
8366 if (global_entry->type == bfd_link_hash_defined
8367 || global_entry->type == bfd_link_hash_defweak)
8368 {
8369 *result = (global_entry->u.def.value
8370 + global_entry->u.def.section->output_section->vma
8371 + global_entry->u.def.section->output_offset);
8372 #ifdef DEBUG
8373 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8374 global_entry->root.string, (unsigned long) *result);
8375 #endif
8376 return TRUE;
8377 }
8378
8379 return FALSE;
8380 }
8381
8382 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8383 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8384 names like "foo.end" which is the end address of section "foo". */
8385
8386 static bfd_boolean
8387 resolve_section (const char *name,
8388 asection *sections,
8389 bfd_vma *result,
8390 bfd * abfd)
8391 {
8392 asection *curr;
8393 unsigned int len;
8394
8395 for (curr = sections; curr; curr = curr->next)
8396 if (strcmp (curr->name, name) == 0)
8397 {
8398 *result = curr->vma;
8399 return TRUE;
8400 }
8401
8402 /* Hmm. still haven't found it. try pseudo-section names. */
8403 /* FIXME: This could be coded more efficiently... */
8404 for (curr = sections; curr; curr = curr->next)
8405 {
8406 len = strlen (curr->name);
8407 if (len > strlen (name))
8408 continue;
8409
8410 if (strncmp (curr->name, name, len) == 0)
8411 {
8412 if (strncmp (".end", name + len, 4) == 0)
8413 {
8414 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8415 return TRUE;
8416 }
8417
8418 /* Insert more pseudo-section names here, if you like. */
8419 }
8420 }
8421
8422 return FALSE;
8423 }
8424
8425 static void
8426 undefined_reference (const char *reftype, const char *name)
8427 {
8428 /* xgettext:c-format */
8429 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8430 reftype, name);
8431 }
8432
8433 static bfd_boolean
8434 eval_symbol (bfd_vma *result,
8435 const char **symp,
8436 bfd *input_bfd,
8437 struct elf_final_link_info *flinfo,
8438 bfd_vma dot,
8439 Elf_Internal_Sym *isymbuf,
8440 size_t locsymcount,
8441 int signed_p)
8442 {
8443 size_t len;
8444 size_t symlen;
8445 bfd_vma a;
8446 bfd_vma b;
8447 char symbuf[4096];
8448 const char *sym = *symp;
8449 const char *symend;
8450 bfd_boolean symbol_is_section = FALSE;
8451
8452 len = strlen (sym);
8453 symend = sym + len;
8454
8455 if (len < 1 || len > sizeof (symbuf))
8456 {
8457 bfd_set_error (bfd_error_invalid_operation);
8458 return FALSE;
8459 }
8460
8461 switch (* sym)
8462 {
8463 case '.':
8464 *result = dot;
8465 *symp = sym + 1;
8466 return TRUE;
8467
8468 case '#':
8469 ++sym;
8470 *result = strtoul (sym, (char **) symp, 16);
8471 return TRUE;
8472
8473 case 'S':
8474 symbol_is_section = TRUE;
8475 /* Fall through. */
8476 case 's':
8477 ++sym;
8478 symlen = strtol (sym, (char **) symp, 10);
8479 sym = *symp + 1; /* Skip the trailing ':'. */
8480
8481 if (symend < sym || symlen + 1 > sizeof (symbuf))
8482 {
8483 bfd_set_error (bfd_error_invalid_operation);
8484 return FALSE;
8485 }
8486
8487 memcpy (symbuf, sym, symlen);
8488 symbuf[symlen] = '\0';
8489 *symp = sym + symlen;
8490
8491 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8492 the symbol as a section, or vice-versa. so we're pretty liberal in our
8493 interpretation here; section means "try section first", not "must be a
8494 section", and likewise with symbol. */
8495
8496 if (symbol_is_section)
8497 {
8498 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8499 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8500 isymbuf, locsymcount))
8501 {
8502 undefined_reference ("section", symbuf);
8503 return FALSE;
8504 }
8505 }
8506 else
8507 {
8508 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8509 isymbuf, locsymcount)
8510 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8511 result, input_bfd))
8512 {
8513 undefined_reference ("symbol", symbuf);
8514 return FALSE;
8515 }
8516 }
8517
8518 return TRUE;
8519
8520 /* All that remains are operators. */
8521
8522 #define UNARY_OP(op) \
8523 if (strncmp (sym, #op, strlen (#op)) == 0) \
8524 { \
8525 sym += strlen (#op); \
8526 if (*sym == ':') \
8527 ++sym; \
8528 *symp = sym; \
8529 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8530 isymbuf, locsymcount, signed_p)) \
8531 return FALSE; \
8532 if (signed_p) \
8533 *result = op ((bfd_signed_vma) a); \
8534 else \
8535 *result = op a; \
8536 return TRUE; \
8537 }
8538
8539 #define BINARY_OP(op) \
8540 if (strncmp (sym, #op, strlen (#op)) == 0) \
8541 { \
8542 sym += strlen (#op); \
8543 if (*sym == ':') \
8544 ++sym; \
8545 *symp = sym; \
8546 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8547 isymbuf, locsymcount, signed_p)) \
8548 return FALSE; \
8549 ++*symp; \
8550 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8551 isymbuf, locsymcount, signed_p)) \
8552 return FALSE; \
8553 if (signed_p) \
8554 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8555 else \
8556 *result = a op b; \
8557 return TRUE; \
8558 }
8559
8560 default:
8561 UNARY_OP (0-);
8562 BINARY_OP (<<);
8563 BINARY_OP (>>);
8564 BINARY_OP (==);
8565 BINARY_OP (!=);
8566 BINARY_OP (<=);
8567 BINARY_OP (>=);
8568 BINARY_OP (&&);
8569 BINARY_OP (||);
8570 UNARY_OP (~);
8571 UNARY_OP (!);
8572 BINARY_OP (*);
8573 BINARY_OP (/);
8574 BINARY_OP (%);
8575 BINARY_OP (^);
8576 BINARY_OP (|);
8577 BINARY_OP (&);
8578 BINARY_OP (+);
8579 BINARY_OP (-);
8580 BINARY_OP (<);
8581 BINARY_OP (>);
8582 #undef UNARY_OP
8583 #undef BINARY_OP
8584 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8585 bfd_set_error (bfd_error_invalid_operation);
8586 return FALSE;
8587 }
8588 }
8589
8590 static void
8591 put_value (bfd_vma size,
8592 unsigned long chunksz,
8593 bfd *input_bfd,
8594 bfd_vma x,
8595 bfd_byte *location)
8596 {
8597 location += (size - chunksz);
8598
8599 for (; size; size -= chunksz, location -= chunksz)
8600 {
8601 switch (chunksz)
8602 {
8603 case 1:
8604 bfd_put_8 (input_bfd, x, location);
8605 x >>= 8;
8606 break;
8607 case 2:
8608 bfd_put_16 (input_bfd, x, location);
8609 x >>= 16;
8610 break;
8611 case 4:
8612 bfd_put_32 (input_bfd, x, location);
8613 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8614 x >>= 16;
8615 x >>= 16;
8616 break;
8617 #ifdef BFD64
8618 case 8:
8619 bfd_put_64 (input_bfd, x, location);
8620 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8621 x >>= 32;
8622 x >>= 32;
8623 break;
8624 #endif
8625 default:
8626 abort ();
8627 break;
8628 }
8629 }
8630 }
8631
8632 static bfd_vma
8633 get_value (bfd_vma size,
8634 unsigned long chunksz,
8635 bfd *input_bfd,
8636 bfd_byte *location)
8637 {
8638 int shift;
8639 bfd_vma x = 0;
8640
8641 /* Sanity checks. */
8642 BFD_ASSERT (chunksz <= sizeof (x)
8643 && size >= chunksz
8644 && chunksz != 0
8645 && (size % chunksz) == 0
8646 && input_bfd != NULL
8647 && location != NULL);
8648
8649 if (chunksz == sizeof (x))
8650 {
8651 BFD_ASSERT (size == chunksz);
8652
8653 /* Make sure that we do not perform an undefined shift operation.
8654 We know that size == chunksz so there will only be one iteration
8655 of the loop below. */
8656 shift = 0;
8657 }
8658 else
8659 shift = 8 * chunksz;
8660
8661 for (; size; size -= chunksz, location += chunksz)
8662 {
8663 switch (chunksz)
8664 {
8665 case 1:
8666 x = (x << shift) | bfd_get_8 (input_bfd, location);
8667 break;
8668 case 2:
8669 x = (x << shift) | bfd_get_16 (input_bfd, location);
8670 break;
8671 case 4:
8672 x = (x << shift) | bfd_get_32 (input_bfd, location);
8673 break;
8674 #ifdef BFD64
8675 case 8:
8676 x = (x << shift) | bfd_get_64 (input_bfd, location);
8677 break;
8678 #endif
8679 default:
8680 abort ();
8681 }
8682 }
8683 return x;
8684 }
8685
8686 static void
8687 decode_complex_addend (unsigned long *start, /* in bits */
8688 unsigned long *oplen, /* in bits */
8689 unsigned long *len, /* in bits */
8690 unsigned long *wordsz, /* in bytes */
8691 unsigned long *chunksz, /* in bytes */
8692 unsigned long *lsb0_p,
8693 unsigned long *signed_p,
8694 unsigned long *trunc_p,
8695 unsigned long encoded)
8696 {
8697 * start = encoded & 0x3F;
8698 * len = (encoded >> 6) & 0x3F;
8699 * oplen = (encoded >> 12) & 0x3F;
8700 * wordsz = (encoded >> 18) & 0xF;
8701 * chunksz = (encoded >> 22) & 0xF;
8702 * lsb0_p = (encoded >> 27) & 1;
8703 * signed_p = (encoded >> 28) & 1;
8704 * trunc_p = (encoded >> 29) & 1;
8705 }
8706
8707 bfd_reloc_status_type
8708 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8709 asection *input_section ATTRIBUTE_UNUSED,
8710 bfd_byte *contents,
8711 Elf_Internal_Rela *rel,
8712 bfd_vma relocation)
8713 {
8714 bfd_vma shift, x, mask;
8715 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8716 bfd_reloc_status_type r;
8717
8718 /* Perform this reloc, since it is complex.
8719 (this is not to say that it necessarily refers to a complex
8720 symbol; merely that it is a self-describing CGEN based reloc.
8721 i.e. the addend has the complete reloc information (bit start, end,
8722 word size, etc) encoded within it.). */
8723
8724 decode_complex_addend (&start, &oplen, &len, &wordsz,
8725 &chunksz, &lsb0_p, &signed_p,
8726 &trunc_p, rel->r_addend);
8727
8728 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8729
8730 if (lsb0_p)
8731 shift = (start + 1) - len;
8732 else
8733 shift = (8 * wordsz) - (start + len);
8734
8735 x = get_value (wordsz, chunksz, input_bfd,
8736 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8737
8738 #ifdef DEBUG
8739 printf ("Doing complex reloc: "
8740 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8741 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8742 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8743 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8744 oplen, (unsigned long) x, (unsigned long) mask,
8745 (unsigned long) relocation);
8746 #endif
8747
8748 r = bfd_reloc_ok;
8749 if (! trunc_p)
8750 /* Now do an overflow check. */
8751 r = bfd_check_overflow ((signed_p
8752 ? complain_overflow_signed
8753 : complain_overflow_unsigned),
8754 len, 0, (8 * wordsz),
8755 relocation);
8756
8757 /* Do the deed. */
8758 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8759
8760 #ifdef DEBUG
8761 printf (" relocation: %8.8lx\n"
8762 " shifted mask: %8.8lx\n"
8763 " shifted/masked reloc: %8.8lx\n"
8764 " result: %8.8lx\n",
8765 (unsigned long) relocation, (unsigned long) (mask << shift),
8766 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8767 #endif
8768 put_value (wordsz, chunksz, input_bfd, x,
8769 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8770 return r;
8771 }
8772
8773 /* Functions to read r_offset from external (target order) reloc
8774 entry. Faster than bfd_getl32 et al, because we let the compiler
8775 know the value is aligned. */
8776
8777 static bfd_vma
8778 ext32l_r_offset (const void *p)
8779 {
8780 union aligned32
8781 {
8782 uint32_t v;
8783 unsigned char c[4];
8784 };
8785 const union aligned32 *a
8786 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8787
8788 uint32_t aval = ( (uint32_t) a->c[0]
8789 | (uint32_t) a->c[1] << 8
8790 | (uint32_t) a->c[2] << 16
8791 | (uint32_t) a->c[3] << 24);
8792 return aval;
8793 }
8794
8795 static bfd_vma
8796 ext32b_r_offset (const void *p)
8797 {
8798 union aligned32
8799 {
8800 uint32_t v;
8801 unsigned char c[4];
8802 };
8803 const union aligned32 *a
8804 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8805
8806 uint32_t aval = ( (uint32_t) a->c[0] << 24
8807 | (uint32_t) a->c[1] << 16
8808 | (uint32_t) a->c[2] << 8
8809 | (uint32_t) a->c[3]);
8810 return aval;
8811 }
8812
8813 #ifdef BFD_HOST_64_BIT
8814 static bfd_vma
8815 ext64l_r_offset (const void *p)
8816 {
8817 union aligned64
8818 {
8819 uint64_t v;
8820 unsigned char c[8];
8821 };
8822 const union aligned64 *a
8823 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8824
8825 uint64_t aval = ( (uint64_t) a->c[0]
8826 | (uint64_t) a->c[1] << 8
8827 | (uint64_t) a->c[2] << 16
8828 | (uint64_t) a->c[3] << 24
8829 | (uint64_t) a->c[4] << 32
8830 | (uint64_t) a->c[5] << 40
8831 | (uint64_t) a->c[6] << 48
8832 | (uint64_t) a->c[7] << 56);
8833 return aval;
8834 }
8835
8836 static bfd_vma
8837 ext64b_r_offset (const void *p)
8838 {
8839 union aligned64
8840 {
8841 uint64_t v;
8842 unsigned char c[8];
8843 };
8844 const union aligned64 *a
8845 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8846
8847 uint64_t aval = ( (uint64_t) a->c[0] << 56
8848 | (uint64_t) a->c[1] << 48
8849 | (uint64_t) a->c[2] << 40
8850 | (uint64_t) a->c[3] << 32
8851 | (uint64_t) a->c[4] << 24
8852 | (uint64_t) a->c[5] << 16
8853 | (uint64_t) a->c[6] << 8
8854 | (uint64_t) a->c[7]);
8855 return aval;
8856 }
8857 #endif
8858
8859 /* When performing a relocatable link, the input relocations are
8860 preserved. But, if they reference global symbols, the indices
8861 referenced must be updated. Update all the relocations found in
8862 RELDATA. */
8863
8864 static bfd_boolean
8865 elf_link_adjust_relocs (bfd *abfd,
8866 asection *sec,
8867 struct bfd_elf_section_reloc_data *reldata,
8868 bfd_boolean sort,
8869 struct bfd_link_info *info)
8870 {
8871 unsigned int i;
8872 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8873 bfd_byte *erela;
8874 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8875 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8876 bfd_vma r_type_mask;
8877 int r_sym_shift;
8878 unsigned int count = reldata->count;
8879 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8880
8881 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8882 {
8883 swap_in = bed->s->swap_reloc_in;
8884 swap_out = bed->s->swap_reloc_out;
8885 }
8886 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8887 {
8888 swap_in = bed->s->swap_reloca_in;
8889 swap_out = bed->s->swap_reloca_out;
8890 }
8891 else
8892 abort ();
8893
8894 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8895 abort ();
8896
8897 if (bed->s->arch_size == 32)
8898 {
8899 r_type_mask = 0xff;
8900 r_sym_shift = 8;
8901 }
8902 else
8903 {
8904 r_type_mask = 0xffffffff;
8905 r_sym_shift = 32;
8906 }
8907
8908 erela = reldata->hdr->contents;
8909 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8910 {
8911 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8912 unsigned int j;
8913
8914 if (*rel_hash == NULL)
8915 continue;
8916
8917 if ((*rel_hash)->indx == -2
8918 && info->gc_sections
8919 && ! info->gc_keep_exported)
8920 {
8921 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8922 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8923 abfd, sec,
8924 (*rel_hash)->root.root.string);
8925 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8926 abfd, sec);
8927 bfd_set_error (bfd_error_invalid_operation);
8928 return FALSE;
8929 }
8930 BFD_ASSERT ((*rel_hash)->indx >= 0);
8931
8932 (*swap_in) (abfd, erela, irela);
8933 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8934 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8935 | (irela[j].r_info & r_type_mask));
8936 (*swap_out) (abfd, irela, erela);
8937 }
8938
8939 if (bed->elf_backend_update_relocs)
8940 (*bed->elf_backend_update_relocs) (sec, reldata);
8941
8942 if (sort && count != 0)
8943 {
8944 bfd_vma (*ext_r_off) (const void *);
8945 bfd_vma r_off;
8946 size_t elt_size;
8947 bfd_byte *base, *end, *p, *loc;
8948 bfd_byte *buf = NULL;
8949
8950 if (bed->s->arch_size == 32)
8951 {
8952 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8953 ext_r_off = ext32l_r_offset;
8954 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8955 ext_r_off = ext32b_r_offset;
8956 else
8957 abort ();
8958 }
8959 else
8960 {
8961 #ifdef BFD_HOST_64_BIT
8962 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8963 ext_r_off = ext64l_r_offset;
8964 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8965 ext_r_off = ext64b_r_offset;
8966 else
8967 #endif
8968 abort ();
8969 }
8970
8971 /* Must use a stable sort here. A modified insertion sort,
8972 since the relocs are mostly sorted already. */
8973 elt_size = reldata->hdr->sh_entsize;
8974 base = reldata->hdr->contents;
8975 end = base + count * elt_size;
8976 if (elt_size > sizeof (Elf64_External_Rela))
8977 abort ();
8978
8979 /* Ensure the first element is lowest. This acts as a sentinel,
8980 speeding the main loop below. */
8981 r_off = (*ext_r_off) (base);
8982 for (p = loc = base; (p += elt_size) < end; )
8983 {
8984 bfd_vma r_off2 = (*ext_r_off) (p);
8985 if (r_off > r_off2)
8986 {
8987 r_off = r_off2;
8988 loc = p;
8989 }
8990 }
8991 if (loc != base)
8992 {
8993 /* Don't just swap *base and *loc as that changes the order
8994 of the original base[0] and base[1] if they happen to
8995 have the same r_offset. */
8996 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8997 memcpy (onebuf, loc, elt_size);
8998 memmove (base + elt_size, base, loc - base);
8999 memcpy (base, onebuf, elt_size);
9000 }
9001
9002 for (p = base + elt_size; (p += elt_size) < end; )
9003 {
9004 /* base to p is sorted, *p is next to insert. */
9005 r_off = (*ext_r_off) (p);
9006 /* Search the sorted region for location to insert. */
9007 loc = p - elt_size;
9008 while (r_off < (*ext_r_off) (loc))
9009 loc -= elt_size;
9010 loc += elt_size;
9011 if (loc != p)
9012 {
9013 /* Chances are there is a run of relocs to insert here,
9014 from one of more input files. Files are not always
9015 linked in order due to the way elf_link_input_bfd is
9016 called. See pr17666. */
9017 size_t sortlen = p - loc;
9018 bfd_vma r_off2 = (*ext_r_off) (loc);
9019 size_t runlen = elt_size;
9020 size_t buf_size = 96 * 1024;
9021 while (p + runlen < end
9022 && (sortlen <= buf_size
9023 || runlen + elt_size <= buf_size)
9024 && r_off2 > (*ext_r_off) (p + runlen))
9025 runlen += elt_size;
9026 if (buf == NULL)
9027 {
9028 buf = bfd_malloc (buf_size);
9029 if (buf == NULL)
9030 return FALSE;
9031 }
9032 if (runlen < sortlen)
9033 {
9034 memcpy (buf, p, runlen);
9035 memmove (loc + runlen, loc, sortlen);
9036 memcpy (loc, buf, runlen);
9037 }
9038 else
9039 {
9040 memcpy (buf, loc, sortlen);
9041 memmove (loc, p, runlen);
9042 memcpy (loc + runlen, buf, sortlen);
9043 }
9044 p += runlen - elt_size;
9045 }
9046 }
9047 /* Hashes are no longer valid. */
9048 free (reldata->hashes);
9049 reldata->hashes = NULL;
9050 free (buf);
9051 }
9052 return TRUE;
9053 }
9054
9055 struct elf_link_sort_rela
9056 {
9057 union {
9058 bfd_vma offset;
9059 bfd_vma sym_mask;
9060 } u;
9061 enum elf_reloc_type_class type;
9062 /* We use this as an array of size int_rels_per_ext_rel. */
9063 Elf_Internal_Rela rela[1];
9064 };
9065
9066 static int
9067 elf_link_sort_cmp1 (const void *A, const void *B)
9068 {
9069 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9070 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9071 int relativea, relativeb;
9072
9073 relativea = a->type == reloc_class_relative;
9074 relativeb = b->type == reloc_class_relative;
9075
9076 if (relativea < relativeb)
9077 return 1;
9078 if (relativea > relativeb)
9079 return -1;
9080 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9081 return -1;
9082 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9083 return 1;
9084 if (a->rela->r_offset < b->rela->r_offset)
9085 return -1;
9086 if (a->rela->r_offset > b->rela->r_offset)
9087 return 1;
9088 return 0;
9089 }
9090
9091 static int
9092 elf_link_sort_cmp2 (const void *A, const void *B)
9093 {
9094 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9095 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9096
9097 if (a->type < b->type)
9098 return -1;
9099 if (a->type > b->type)
9100 return 1;
9101 if (a->u.offset < b->u.offset)
9102 return -1;
9103 if (a->u.offset > b->u.offset)
9104 return 1;
9105 if (a->rela->r_offset < b->rela->r_offset)
9106 return -1;
9107 if (a->rela->r_offset > b->rela->r_offset)
9108 return 1;
9109 return 0;
9110 }
9111
9112 static size_t
9113 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9114 {
9115 asection *dynamic_relocs;
9116 asection *rela_dyn;
9117 asection *rel_dyn;
9118 bfd_size_type count, size;
9119 size_t i, ret, sort_elt, ext_size;
9120 bfd_byte *sort, *s_non_relative, *p;
9121 struct elf_link_sort_rela *sq;
9122 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9123 int i2e = bed->s->int_rels_per_ext_rel;
9124 unsigned int opb = bfd_octets_per_byte (abfd);
9125 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9126 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9127 struct bfd_link_order *lo;
9128 bfd_vma r_sym_mask;
9129 bfd_boolean use_rela;
9130
9131 /* Find a dynamic reloc section. */
9132 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9133 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9134 if (rela_dyn != NULL && rela_dyn->size > 0
9135 && rel_dyn != NULL && rel_dyn->size > 0)
9136 {
9137 bfd_boolean use_rela_initialised = FALSE;
9138
9139 /* This is just here to stop gcc from complaining.
9140 Its initialization checking code is not perfect. */
9141 use_rela = TRUE;
9142
9143 /* Both sections are present. Examine the sizes
9144 of the indirect sections to help us choose. */
9145 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9146 if (lo->type == bfd_indirect_link_order)
9147 {
9148 asection *o = lo->u.indirect.section;
9149
9150 if ((o->size % bed->s->sizeof_rela) == 0)
9151 {
9152 if ((o->size % bed->s->sizeof_rel) == 0)
9153 /* Section size is divisible by both rel and rela sizes.
9154 It is of no help to us. */
9155 ;
9156 else
9157 {
9158 /* Section size is only divisible by rela. */
9159 if (use_rela_initialised && !use_rela)
9160 {
9161 _bfd_error_handler (_("%pB: unable to sort relocs - "
9162 "they are in more than one size"),
9163 abfd);
9164 bfd_set_error (bfd_error_invalid_operation);
9165 return 0;
9166 }
9167 else
9168 {
9169 use_rela = TRUE;
9170 use_rela_initialised = TRUE;
9171 }
9172 }
9173 }
9174 else if ((o->size % bed->s->sizeof_rel) == 0)
9175 {
9176 /* Section size is only divisible by rel. */
9177 if (use_rela_initialised && use_rela)
9178 {
9179 _bfd_error_handler (_("%pB: unable to sort relocs - "
9180 "they are in more than one size"),
9181 abfd);
9182 bfd_set_error (bfd_error_invalid_operation);
9183 return 0;
9184 }
9185 else
9186 {
9187 use_rela = FALSE;
9188 use_rela_initialised = TRUE;
9189 }
9190 }
9191 else
9192 {
9193 /* The section size is not divisible by either -
9194 something is wrong. */
9195 _bfd_error_handler (_("%pB: unable to sort relocs - "
9196 "they are of an unknown size"), abfd);
9197 bfd_set_error (bfd_error_invalid_operation);
9198 return 0;
9199 }
9200 }
9201
9202 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9203 if (lo->type == bfd_indirect_link_order)
9204 {
9205 asection *o = lo->u.indirect.section;
9206
9207 if ((o->size % bed->s->sizeof_rela) == 0)
9208 {
9209 if ((o->size % bed->s->sizeof_rel) == 0)
9210 /* Section size is divisible by both rel and rela sizes.
9211 It is of no help to us. */
9212 ;
9213 else
9214 {
9215 /* Section size is only divisible by rela. */
9216 if (use_rela_initialised && !use_rela)
9217 {
9218 _bfd_error_handler (_("%pB: unable to sort relocs - "
9219 "they are in more than one size"),
9220 abfd);
9221 bfd_set_error (bfd_error_invalid_operation);
9222 return 0;
9223 }
9224 else
9225 {
9226 use_rela = TRUE;
9227 use_rela_initialised = TRUE;
9228 }
9229 }
9230 }
9231 else if ((o->size % bed->s->sizeof_rel) == 0)
9232 {
9233 /* Section size is only divisible by rel. */
9234 if (use_rela_initialised && use_rela)
9235 {
9236 _bfd_error_handler (_("%pB: unable to sort relocs - "
9237 "they are in more than one size"),
9238 abfd);
9239 bfd_set_error (bfd_error_invalid_operation);
9240 return 0;
9241 }
9242 else
9243 {
9244 use_rela = FALSE;
9245 use_rela_initialised = TRUE;
9246 }
9247 }
9248 else
9249 {
9250 /* The section size is not divisible by either -
9251 something is wrong. */
9252 _bfd_error_handler (_("%pB: unable to sort relocs - "
9253 "they are of an unknown size"), abfd);
9254 bfd_set_error (bfd_error_invalid_operation);
9255 return 0;
9256 }
9257 }
9258
9259 if (! use_rela_initialised)
9260 /* Make a guess. */
9261 use_rela = TRUE;
9262 }
9263 else if (rela_dyn != NULL && rela_dyn->size > 0)
9264 use_rela = TRUE;
9265 else if (rel_dyn != NULL && rel_dyn->size > 0)
9266 use_rela = FALSE;
9267 else
9268 return 0;
9269
9270 if (use_rela)
9271 {
9272 dynamic_relocs = rela_dyn;
9273 ext_size = bed->s->sizeof_rela;
9274 swap_in = bed->s->swap_reloca_in;
9275 swap_out = bed->s->swap_reloca_out;
9276 }
9277 else
9278 {
9279 dynamic_relocs = rel_dyn;
9280 ext_size = bed->s->sizeof_rel;
9281 swap_in = bed->s->swap_reloc_in;
9282 swap_out = bed->s->swap_reloc_out;
9283 }
9284
9285 size = 0;
9286 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9287 if (lo->type == bfd_indirect_link_order)
9288 size += lo->u.indirect.section->size;
9289
9290 if (size != dynamic_relocs->size)
9291 return 0;
9292
9293 sort_elt = (sizeof (struct elf_link_sort_rela)
9294 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9295
9296 count = dynamic_relocs->size / ext_size;
9297 if (count == 0)
9298 return 0;
9299 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9300
9301 if (sort == NULL)
9302 {
9303 (*info->callbacks->warning)
9304 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9305 return 0;
9306 }
9307
9308 if (bed->s->arch_size == 32)
9309 r_sym_mask = ~(bfd_vma) 0xff;
9310 else
9311 r_sym_mask = ~(bfd_vma) 0xffffffff;
9312
9313 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9314 if (lo->type == bfd_indirect_link_order)
9315 {
9316 bfd_byte *erel, *erelend;
9317 asection *o = lo->u.indirect.section;
9318
9319 if (o->contents == NULL && o->size != 0)
9320 {
9321 /* This is a reloc section that is being handled as a normal
9322 section. See bfd_section_from_shdr. We can't combine
9323 relocs in this case. */
9324 free (sort);
9325 return 0;
9326 }
9327 erel = o->contents;
9328 erelend = o->contents + o->size;
9329 p = sort + o->output_offset * opb / ext_size * sort_elt;
9330
9331 while (erel < erelend)
9332 {
9333 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9334
9335 (*swap_in) (abfd, erel, s->rela);
9336 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9337 s->u.sym_mask = r_sym_mask;
9338 p += sort_elt;
9339 erel += ext_size;
9340 }
9341 }
9342
9343 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9344
9345 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9346 {
9347 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9348 if (s->type != reloc_class_relative)
9349 break;
9350 }
9351 ret = i;
9352 s_non_relative = p;
9353
9354 sq = (struct elf_link_sort_rela *) s_non_relative;
9355 for (; i < count; i++, p += sort_elt)
9356 {
9357 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9358 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9359 sq = sp;
9360 sp->u.offset = sq->rela->r_offset;
9361 }
9362
9363 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9364
9365 struct elf_link_hash_table *htab = elf_hash_table (info);
9366 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9367 {
9368 /* We have plt relocs in .rela.dyn. */
9369 sq = (struct elf_link_sort_rela *) sort;
9370 for (i = 0; i < count; i++)
9371 if (sq[count - i - 1].type != reloc_class_plt)
9372 break;
9373 if (i != 0 && htab->srelplt->size == i * ext_size)
9374 {
9375 struct bfd_link_order **plo;
9376 /* Put srelplt link_order last. This is so the output_offset
9377 set in the next loop is correct for DT_JMPREL. */
9378 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9379 if ((*plo)->type == bfd_indirect_link_order
9380 && (*plo)->u.indirect.section == htab->srelplt)
9381 {
9382 lo = *plo;
9383 *plo = lo->next;
9384 }
9385 else
9386 plo = &(*plo)->next;
9387 *plo = lo;
9388 lo->next = NULL;
9389 dynamic_relocs->map_tail.link_order = lo;
9390 }
9391 }
9392
9393 p = sort;
9394 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9395 if (lo->type == bfd_indirect_link_order)
9396 {
9397 bfd_byte *erel, *erelend;
9398 asection *o = lo->u.indirect.section;
9399
9400 erel = o->contents;
9401 erelend = o->contents + o->size;
9402 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9403 while (erel < erelend)
9404 {
9405 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9406 (*swap_out) (abfd, s->rela, erel);
9407 p += sort_elt;
9408 erel += ext_size;
9409 }
9410 }
9411
9412 free (sort);
9413 *psec = dynamic_relocs;
9414 return ret;
9415 }
9416
9417 /* Add a symbol to the output symbol string table. */
9418
9419 static int
9420 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9421 const char *name,
9422 Elf_Internal_Sym *elfsym,
9423 asection *input_sec,
9424 struct elf_link_hash_entry *h)
9425 {
9426 int (*output_symbol_hook)
9427 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9428 struct elf_link_hash_entry *);
9429 struct elf_link_hash_table *hash_table;
9430 const struct elf_backend_data *bed;
9431 bfd_size_type strtabsize;
9432
9433 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9434
9435 bed = get_elf_backend_data (flinfo->output_bfd);
9436 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9437 if (output_symbol_hook != NULL)
9438 {
9439 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9440 if (ret != 1)
9441 return ret;
9442 }
9443
9444 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9445 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9446 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9447 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9448
9449 if (name == NULL
9450 || *name == '\0'
9451 || (input_sec->flags & SEC_EXCLUDE))
9452 elfsym->st_name = (unsigned long) -1;
9453 else
9454 {
9455 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9456 to get the final offset for st_name. */
9457 elfsym->st_name
9458 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9459 name, FALSE);
9460 if (elfsym->st_name == (unsigned long) -1)
9461 return 0;
9462 }
9463
9464 hash_table = elf_hash_table (flinfo->info);
9465 strtabsize = hash_table->strtabsize;
9466 if (strtabsize <= hash_table->strtabcount)
9467 {
9468 strtabsize += strtabsize;
9469 hash_table->strtabsize = strtabsize;
9470 strtabsize *= sizeof (*hash_table->strtab);
9471 hash_table->strtab
9472 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9473 strtabsize);
9474 if (hash_table->strtab == NULL)
9475 return 0;
9476 }
9477 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9478 hash_table->strtab[hash_table->strtabcount].dest_index
9479 = hash_table->strtabcount;
9480 hash_table->strtab[hash_table->strtabcount].destshndx_index
9481 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9482
9483 bfd_get_symcount (flinfo->output_bfd) += 1;
9484 hash_table->strtabcount += 1;
9485
9486 return 1;
9487 }
9488
9489 /* Swap symbols out to the symbol table and flush the output symbols to
9490 the file. */
9491
9492 static bfd_boolean
9493 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9494 {
9495 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9496 bfd_size_type amt;
9497 size_t i;
9498 const struct elf_backend_data *bed;
9499 bfd_byte *symbuf;
9500 Elf_Internal_Shdr *hdr;
9501 file_ptr pos;
9502 bfd_boolean ret;
9503
9504 if (!hash_table->strtabcount)
9505 return TRUE;
9506
9507 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9508
9509 bed = get_elf_backend_data (flinfo->output_bfd);
9510
9511 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9512 symbuf = (bfd_byte *) bfd_malloc (amt);
9513 if (symbuf == NULL)
9514 return FALSE;
9515
9516 if (flinfo->symshndxbuf)
9517 {
9518 amt = sizeof (Elf_External_Sym_Shndx);
9519 amt *= bfd_get_symcount (flinfo->output_bfd);
9520 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9521 if (flinfo->symshndxbuf == NULL)
9522 {
9523 free (symbuf);
9524 return FALSE;
9525 }
9526 }
9527
9528 for (i = 0; i < hash_table->strtabcount; i++)
9529 {
9530 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9531 if (elfsym->sym.st_name == (unsigned long) -1)
9532 elfsym->sym.st_name = 0;
9533 else
9534 elfsym->sym.st_name
9535 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9536 elfsym->sym.st_name);
9537 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9538 ((bfd_byte *) symbuf
9539 + (elfsym->dest_index
9540 * bed->s->sizeof_sym)),
9541 (flinfo->symshndxbuf
9542 + elfsym->destshndx_index));
9543 }
9544
9545 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9546 pos = hdr->sh_offset + hdr->sh_size;
9547 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9548 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9549 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9550 {
9551 hdr->sh_size += amt;
9552 ret = TRUE;
9553 }
9554 else
9555 ret = FALSE;
9556
9557 free (symbuf);
9558
9559 free (hash_table->strtab);
9560 hash_table->strtab = NULL;
9561
9562 return ret;
9563 }
9564
9565 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9566
9567 static bfd_boolean
9568 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9569 {
9570 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9571 && sym->st_shndx < SHN_LORESERVE)
9572 {
9573 /* The gABI doesn't support dynamic symbols in output sections
9574 beyond 64k. */
9575 _bfd_error_handler
9576 /* xgettext:c-format */
9577 (_("%pB: too many sections: %d (>= %d)"),
9578 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9579 bfd_set_error (bfd_error_nonrepresentable_section);
9580 return FALSE;
9581 }
9582 return TRUE;
9583 }
9584
9585 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9586 allowing an unsatisfied unversioned symbol in the DSO to match a
9587 versioned symbol that would normally require an explicit version.
9588 We also handle the case that a DSO references a hidden symbol
9589 which may be satisfied by a versioned symbol in another DSO. */
9590
9591 static bfd_boolean
9592 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9593 const struct elf_backend_data *bed,
9594 struct elf_link_hash_entry *h)
9595 {
9596 bfd *abfd;
9597 struct elf_link_loaded_list *loaded;
9598
9599 if (!is_elf_hash_table (info->hash))
9600 return FALSE;
9601
9602 /* Check indirect symbol. */
9603 while (h->root.type == bfd_link_hash_indirect)
9604 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9605
9606 switch (h->root.type)
9607 {
9608 default:
9609 abfd = NULL;
9610 break;
9611
9612 case bfd_link_hash_undefined:
9613 case bfd_link_hash_undefweak:
9614 abfd = h->root.u.undef.abfd;
9615 if (abfd == NULL
9616 || (abfd->flags & DYNAMIC) == 0
9617 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9618 return FALSE;
9619 break;
9620
9621 case bfd_link_hash_defined:
9622 case bfd_link_hash_defweak:
9623 abfd = h->root.u.def.section->owner;
9624 break;
9625
9626 case bfd_link_hash_common:
9627 abfd = h->root.u.c.p->section->owner;
9628 break;
9629 }
9630 BFD_ASSERT (abfd != NULL);
9631
9632 for (loaded = elf_hash_table (info)->loaded;
9633 loaded != NULL;
9634 loaded = loaded->next)
9635 {
9636 bfd *input;
9637 Elf_Internal_Shdr *hdr;
9638 size_t symcount;
9639 size_t extsymcount;
9640 size_t extsymoff;
9641 Elf_Internal_Shdr *versymhdr;
9642 Elf_Internal_Sym *isym;
9643 Elf_Internal_Sym *isymend;
9644 Elf_Internal_Sym *isymbuf;
9645 Elf_External_Versym *ever;
9646 Elf_External_Versym *extversym;
9647
9648 input = loaded->abfd;
9649
9650 /* We check each DSO for a possible hidden versioned definition. */
9651 if (input == abfd
9652 || (input->flags & DYNAMIC) == 0
9653 || elf_dynversym (input) == 0)
9654 continue;
9655
9656 hdr = &elf_tdata (input)->dynsymtab_hdr;
9657
9658 symcount = hdr->sh_size / bed->s->sizeof_sym;
9659 if (elf_bad_symtab (input))
9660 {
9661 extsymcount = symcount;
9662 extsymoff = 0;
9663 }
9664 else
9665 {
9666 extsymcount = symcount - hdr->sh_info;
9667 extsymoff = hdr->sh_info;
9668 }
9669
9670 if (extsymcount == 0)
9671 continue;
9672
9673 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9674 NULL, NULL, NULL);
9675 if (isymbuf == NULL)
9676 return FALSE;
9677
9678 /* Read in any version definitions. */
9679 versymhdr = &elf_tdata (input)->dynversym_hdr;
9680 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9681 if (extversym == NULL)
9682 goto error_ret;
9683
9684 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9685 || (bfd_bread (extversym, versymhdr->sh_size, input)
9686 != versymhdr->sh_size))
9687 {
9688 free (extversym);
9689 error_ret:
9690 free (isymbuf);
9691 return FALSE;
9692 }
9693
9694 ever = extversym + extsymoff;
9695 isymend = isymbuf + extsymcount;
9696 for (isym = isymbuf; isym < isymend; isym++, ever++)
9697 {
9698 const char *name;
9699 Elf_Internal_Versym iver;
9700 unsigned short version_index;
9701
9702 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9703 || isym->st_shndx == SHN_UNDEF)
9704 continue;
9705
9706 name = bfd_elf_string_from_elf_section (input,
9707 hdr->sh_link,
9708 isym->st_name);
9709 if (strcmp (name, h->root.root.string) != 0)
9710 continue;
9711
9712 _bfd_elf_swap_versym_in (input, ever, &iver);
9713
9714 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9715 && !(h->def_regular
9716 && h->forced_local))
9717 {
9718 /* If we have a non-hidden versioned sym, then it should
9719 have provided a definition for the undefined sym unless
9720 it is defined in a non-shared object and forced local.
9721 */
9722 abort ();
9723 }
9724
9725 version_index = iver.vs_vers & VERSYM_VERSION;
9726 if (version_index == 1 || version_index == 2)
9727 {
9728 /* This is the base or first version. We can use it. */
9729 free (extversym);
9730 free (isymbuf);
9731 return TRUE;
9732 }
9733 }
9734
9735 free (extversym);
9736 free (isymbuf);
9737 }
9738
9739 return FALSE;
9740 }
9741
9742 /* Convert ELF common symbol TYPE. */
9743
9744 static int
9745 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9746 {
9747 /* Commom symbol can only appear in relocatable link. */
9748 if (!bfd_link_relocatable (info))
9749 abort ();
9750 switch (info->elf_stt_common)
9751 {
9752 case unchanged:
9753 break;
9754 case elf_stt_common:
9755 type = STT_COMMON;
9756 break;
9757 case no_elf_stt_common:
9758 type = STT_OBJECT;
9759 break;
9760 }
9761 return type;
9762 }
9763
9764 /* Add an external symbol to the symbol table. This is called from
9765 the hash table traversal routine. When generating a shared object,
9766 we go through the symbol table twice. The first time we output
9767 anything that might have been forced to local scope in a version
9768 script. The second time we output the symbols that are still
9769 global symbols. */
9770
9771 static bfd_boolean
9772 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9773 {
9774 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9775 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9776 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9777 bfd_boolean strip;
9778 Elf_Internal_Sym sym;
9779 asection *input_sec;
9780 const struct elf_backend_data *bed;
9781 long indx;
9782 int ret;
9783 unsigned int type;
9784
9785 if (h->root.type == bfd_link_hash_warning)
9786 {
9787 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9788 if (h->root.type == bfd_link_hash_new)
9789 return TRUE;
9790 }
9791
9792 /* Decide whether to output this symbol in this pass. */
9793 if (eoinfo->localsyms)
9794 {
9795 if (!h->forced_local)
9796 return TRUE;
9797 }
9798 else
9799 {
9800 if (h->forced_local)
9801 return TRUE;
9802 }
9803
9804 bed = get_elf_backend_data (flinfo->output_bfd);
9805
9806 if (h->root.type == bfd_link_hash_undefined)
9807 {
9808 /* If we have an undefined symbol reference here then it must have
9809 come from a shared library that is being linked in. (Undefined
9810 references in regular files have already been handled unless
9811 they are in unreferenced sections which are removed by garbage
9812 collection). */
9813 bfd_boolean ignore_undef = FALSE;
9814
9815 /* Some symbols may be special in that the fact that they're
9816 undefined can be safely ignored - let backend determine that. */
9817 if (bed->elf_backend_ignore_undef_symbol)
9818 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9819
9820 /* If we are reporting errors for this situation then do so now. */
9821 if (!ignore_undef
9822 && h->ref_dynamic_nonweak
9823 && (!h->ref_regular || flinfo->info->gc_sections)
9824 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9825 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9826 (*flinfo->info->callbacks->undefined_symbol)
9827 (flinfo->info, h->root.root.string,
9828 h->ref_regular ? NULL : h->root.u.undef.abfd,
9829 NULL, 0,
9830 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9831
9832 /* Strip a global symbol defined in a discarded section. */
9833 if (h->indx == -3)
9834 return TRUE;
9835 }
9836
9837 /* We should also warn if a forced local symbol is referenced from
9838 shared libraries. */
9839 if (bfd_link_executable (flinfo->info)
9840 && h->forced_local
9841 && h->ref_dynamic
9842 && h->def_regular
9843 && !h->dynamic_def
9844 && h->ref_dynamic_nonweak
9845 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9846 {
9847 bfd *def_bfd;
9848 const char *msg;
9849 struct elf_link_hash_entry *hi = h;
9850
9851 /* Check indirect symbol. */
9852 while (hi->root.type == bfd_link_hash_indirect)
9853 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9854
9855 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9856 /* xgettext:c-format */
9857 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9858 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9859 /* xgettext:c-format */
9860 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9861 else
9862 /* xgettext:c-format */
9863 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9864 def_bfd = flinfo->output_bfd;
9865 if (hi->root.u.def.section != bfd_abs_section_ptr)
9866 def_bfd = hi->root.u.def.section->owner;
9867 _bfd_error_handler (msg, flinfo->output_bfd,
9868 h->root.root.string, def_bfd);
9869 bfd_set_error (bfd_error_bad_value);
9870 eoinfo->failed = TRUE;
9871 return FALSE;
9872 }
9873
9874 /* We don't want to output symbols that have never been mentioned by
9875 a regular file, or that we have been told to strip. However, if
9876 h->indx is set to -2, the symbol is used by a reloc and we must
9877 output it. */
9878 strip = FALSE;
9879 if (h->indx == -2)
9880 ;
9881 else if ((h->def_dynamic
9882 || h->ref_dynamic
9883 || h->root.type == bfd_link_hash_new)
9884 && !h->def_regular
9885 && !h->ref_regular)
9886 strip = TRUE;
9887 else if (flinfo->info->strip == strip_all)
9888 strip = TRUE;
9889 else if (flinfo->info->strip == strip_some
9890 && bfd_hash_lookup (flinfo->info->keep_hash,
9891 h->root.root.string, FALSE, FALSE) == NULL)
9892 strip = TRUE;
9893 else if ((h->root.type == bfd_link_hash_defined
9894 || h->root.type == bfd_link_hash_defweak)
9895 && ((flinfo->info->strip_discarded
9896 && discarded_section (h->root.u.def.section))
9897 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9898 && h->root.u.def.section->owner != NULL
9899 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9900 strip = TRUE;
9901 else if ((h->root.type == bfd_link_hash_undefined
9902 || h->root.type == bfd_link_hash_undefweak)
9903 && h->root.u.undef.abfd != NULL
9904 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9905 strip = TRUE;
9906
9907 type = h->type;
9908
9909 /* If we're stripping it, and it's not a dynamic symbol, there's
9910 nothing else to do. However, if it is a forced local symbol or
9911 an ifunc symbol we need to give the backend finish_dynamic_symbol
9912 function a chance to make it dynamic. */
9913 if (strip
9914 && h->dynindx == -1
9915 && type != STT_GNU_IFUNC
9916 && !h->forced_local)
9917 return TRUE;
9918
9919 sym.st_value = 0;
9920 sym.st_size = h->size;
9921 sym.st_other = h->other;
9922 switch (h->root.type)
9923 {
9924 default:
9925 case bfd_link_hash_new:
9926 case bfd_link_hash_warning:
9927 abort ();
9928 return FALSE;
9929
9930 case bfd_link_hash_undefined:
9931 case bfd_link_hash_undefweak:
9932 input_sec = bfd_und_section_ptr;
9933 sym.st_shndx = SHN_UNDEF;
9934 break;
9935
9936 case bfd_link_hash_defined:
9937 case bfd_link_hash_defweak:
9938 {
9939 input_sec = h->root.u.def.section;
9940 if (input_sec->output_section != NULL)
9941 {
9942 sym.st_shndx =
9943 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9944 input_sec->output_section);
9945 if (sym.st_shndx == SHN_BAD)
9946 {
9947 _bfd_error_handler
9948 /* xgettext:c-format */
9949 (_("%pB: could not find output section %pA for input section %pA"),
9950 flinfo->output_bfd, input_sec->output_section, input_sec);
9951 bfd_set_error (bfd_error_nonrepresentable_section);
9952 eoinfo->failed = TRUE;
9953 return FALSE;
9954 }
9955
9956 /* ELF symbols in relocatable files are section relative,
9957 but in nonrelocatable files they are virtual
9958 addresses. */
9959 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9960 if (!bfd_link_relocatable (flinfo->info))
9961 {
9962 sym.st_value += input_sec->output_section->vma;
9963 if (h->type == STT_TLS)
9964 {
9965 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9966 if (tls_sec != NULL)
9967 sym.st_value -= tls_sec->vma;
9968 }
9969 }
9970 }
9971 else
9972 {
9973 BFD_ASSERT (input_sec->owner == NULL
9974 || (input_sec->owner->flags & DYNAMIC) != 0);
9975 sym.st_shndx = SHN_UNDEF;
9976 input_sec = bfd_und_section_ptr;
9977 }
9978 }
9979 break;
9980
9981 case bfd_link_hash_common:
9982 input_sec = h->root.u.c.p->section;
9983 sym.st_shndx = bed->common_section_index (input_sec);
9984 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9985 break;
9986
9987 case bfd_link_hash_indirect:
9988 /* These symbols are created by symbol versioning. They point
9989 to the decorated version of the name. For example, if the
9990 symbol foo@@GNU_1.2 is the default, which should be used when
9991 foo is used with no version, then we add an indirect symbol
9992 foo which points to foo@@GNU_1.2. We ignore these symbols,
9993 since the indirected symbol is already in the hash table. */
9994 return TRUE;
9995 }
9996
9997 if (type == STT_COMMON || type == STT_OBJECT)
9998 switch (h->root.type)
9999 {
10000 case bfd_link_hash_common:
10001 type = elf_link_convert_common_type (flinfo->info, type);
10002 break;
10003 case bfd_link_hash_defined:
10004 case bfd_link_hash_defweak:
10005 if (bed->common_definition (&sym))
10006 type = elf_link_convert_common_type (flinfo->info, type);
10007 else
10008 type = STT_OBJECT;
10009 break;
10010 case bfd_link_hash_undefined:
10011 case bfd_link_hash_undefweak:
10012 break;
10013 default:
10014 abort ();
10015 }
10016
10017 if (h->forced_local)
10018 {
10019 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10020 /* Turn off visibility on local symbol. */
10021 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10022 }
10023 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10024 else if (h->unique_global && h->def_regular)
10025 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10026 else if (h->root.type == bfd_link_hash_undefweak
10027 || h->root.type == bfd_link_hash_defweak)
10028 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10029 else
10030 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10031 sym.st_target_internal = h->target_internal;
10032
10033 /* Give the processor backend a chance to tweak the symbol value,
10034 and also to finish up anything that needs to be done for this
10035 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10036 forced local syms when non-shared is due to a historical quirk.
10037 STT_GNU_IFUNC symbol must go through PLT. */
10038 if ((h->type == STT_GNU_IFUNC
10039 && h->def_regular
10040 && !bfd_link_relocatable (flinfo->info))
10041 || ((h->dynindx != -1
10042 || h->forced_local)
10043 && ((bfd_link_pic (flinfo->info)
10044 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10045 || h->root.type != bfd_link_hash_undefweak))
10046 || !h->forced_local)
10047 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10048 {
10049 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10050 (flinfo->output_bfd, flinfo->info, h, &sym)))
10051 {
10052 eoinfo->failed = TRUE;
10053 return FALSE;
10054 }
10055 }
10056
10057 /* If we are marking the symbol as undefined, and there are no
10058 non-weak references to this symbol from a regular object, then
10059 mark the symbol as weak undefined; if there are non-weak
10060 references, mark the symbol as strong. We can't do this earlier,
10061 because it might not be marked as undefined until the
10062 finish_dynamic_symbol routine gets through with it. */
10063 if (sym.st_shndx == SHN_UNDEF
10064 && h->ref_regular
10065 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10066 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10067 {
10068 int bindtype;
10069 type = ELF_ST_TYPE (sym.st_info);
10070
10071 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10072 if (type == STT_GNU_IFUNC)
10073 type = STT_FUNC;
10074
10075 if (h->ref_regular_nonweak)
10076 bindtype = STB_GLOBAL;
10077 else
10078 bindtype = STB_WEAK;
10079 sym.st_info = ELF_ST_INFO (bindtype, type);
10080 }
10081
10082 /* If this is a symbol defined in a dynamic library, don't use the
10083 symbol size from the dynamic library. Relinking an executable
10084 against a new library may introduce gratuitous changes in the
10085 executable's symbols if we keep the size. */
10086 if (sym.st_shndx == SHN_UNDEF
10087 && !h->def_regular
10088 && h->def_dynamic)
10089 sym.st_size = 0;
10090
10091 /* If a non-weak symbol with non-default visibility is not defined
10092 locally, it is a fatal error. */
10093 if (!bfd_link_relocatable (flinfo->info)
10094 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10095 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10096 && h->root.type == bfd_link_hash_undefined
10097 && !h->def_regular)
10098 {
10099 const char *msg;
10100
10101 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10102 /* xgettext:c-format */
10103 msg = _("%pB: protected symbol `%s' isn't defined");
10104 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10105 /* xgettext:c-format */
10106 msg = _("%pB: internal symbol `%s' isn't defined");
10107 else
10108 /* xgettext:c-format */
10109 msg = _("%pB: hidden symbol `%s' isn't defined");
10110 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10111 bfd_set_error (bfd_error_bad_value);
10112 eoinfo->failed = TRUE;
10113 return FALSE;
10114 }
10115
10116 /* If this symbol should be put in the .dynsym section, then put it
10117 there now. We already know the symbol index. We also fill in
10118 the entry in the .hash section. */
10119 if (h->dynindx != -1
10120 && elf_hash_table (flinfo->info)->dynamic_sections_created
10121 && elf_hash_table (flinfo->info)->dynsym != NULL
10122 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10123 {
10124 bfd_byte *esym;
10125
10126 /* Since there is no version information in the dynamic string,
10127 if there is no version info in symbol version section, we will
10128 have a run-time problem if not linking executable, referenced
10129 by shared library, or not bound locally. */
10130 if (h->verinfo.verdef == NULL
10131 && (!bfd_link_executable (flinfo->info)
10132 || h->ref_dynamic
10133 || !h->def_regular))
10134 {
10135 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10136
10137 if (p && p [1] != '\0')
10138 {
10139 _bfd_error_handler
10140 /* xgettext:c-format */
10141 (_("%pB: no symbol version section for versioned symbol `%s'"),
10142 flinfo->output_bfd, h->root.root.string);
10143 eoinfo->failed = TRUE;
10144 return FALSE;
10145 }
10146 }
10147
10148 sym.st_name = h->dynstr_index;
10149 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10150 + h->dynindx * bed->s->sizeof_sym);
10151 if (!check_dynsym (flinfo->output_bfd, &sym))
10152 {
10153 eoinfo->failed = TRUE;
10154 return FALSE;
10155 }
10156 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10157
10158 if (flinfo->hash_sec != NULL)
10159 {
10160 size_t hash_entry_size;
10161 bfd_byte *bucketpos;
10162 bfd_vma chain;
10163 size_t bucketcount;
10164 size_t bucket;
10165
10166 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10167 bucket = h->u.elf_hash_value % bucketcount;
10168
10169 hash_entry_size
10170 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10171 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10172 + (bucket + 2) * hash_entry_size);
10173 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10174 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10175 bucketpos);
10176 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10177 ((bfd_byte *) flinfo->hash_sec->contents
10178 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10179 }
10180
10181 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10182 {
10183 Elf_Internal_Versym iversym;
10184 Elf_External_Versym *eversym;
10185
10186 if (!h->def_regular)
10187 {
10188 if (h->verinfo.verdef == NULL
10189 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10190 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10191 iversym.vs_vers = 0;
10192 else
10193 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10194 }
10195 else
10196 {
10197 if (h->verinfo.vertree == NULL)
10198 iversym.vs_vers = 1;
10199 else
10200 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10201 if (flinfo->info->create_default_symver)
10202 iversym.vs_vers++;
10203 }
10204
10205 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10206 defined locally. */
10207 if (h->versioned == versioned_hidden && h->def_regular)
10208 iversym.vs_vers |= VERSYM_HIDDEN;
10209
10210 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10211 eversym += h->dynindx;
10212 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10213 }
10214 }
10215
10216 /* If the symbol is undefined, and we didn't output it to .dynsym,
10217 strip it from .symtab too. Obviously we can't do this for
10218 relocatable output or when needed for --emit-relocs. */
10219 else if (input_sec == bfd_und_section_ptr
10220 && h->indx != -2
10221 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10222 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10223 && !bfd_link_relocatable (flinfo->info))
10224 return TRUE;
10225
10226 /* Also strip others that we couldn't earlier due to dynamic symbol
10227 processing. */
10228 if (strip)
10229 return TRUE;
10230 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10231 return TRUE;
10232
10233 /* Output a FILE symbol so that following locals are not associated
10234 with the wrong input file. We need one for forced local symbols
10235 if we've seen more than one FILE symbol or when we have exactly
10236 one FILE symbol but global symbols are present in a file other
10237 than the one with the FILE symbol. We also need one if linker
10238 defined symbols are present. In practice these conditions are
10239 always met, so just emit the FILE symbol unconditionally. */
10240 if (eoinfo->localsyms
10241 && !eoinfo->file_sym_done
10242 && eoinfo->flinfo->filesym_count != 0)
10243 {
10244 Elf_Internal_Sym fsym;
10245
10246 memset (&fsym, 0, sizeof (fsym));
10247 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10248 fsym.st_shndx = SHN_ABS;
10249 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10250 bfd_und_section_ptr, NULL))
10251 return FALSE;
10252
10253 eoinfo->file_sym_done = TRUE;
10254 }
10255
10256 indx = bfd_get_symcount (flinfo->output_bfd);
10257 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10258 input_sec, h);
10259 if (ret == 0)
10260 {
10261 eoinfo->failed = TRUE;
10262 return FALSE;
10263 }
10264 else if (ret == 1)
10265 h->indx = indx;
10266 else if (h->indx == -2)
10267 abort();
10268
10269 return TRUE;
10270 }
10271
10272 /* Return TRUE if special handling is done for relocs in SEC against
10273 symbols defined in discarded sections. */
10274
10275 static bfd_boolean
10276 elf_section_ignore_discarded_relocs (asection *sec)
10277 {
10278 const struct elf_backend_data *bed;
10279
10280 switch (sec->sec_info_type)
10281 {
10282 case SEC_INFO_TYPE_STABS:
10283 case SEC_INFO_TYPE_EH_FRAME:
10284 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10285 return TRUE;
10286 default:
10287 break;
10288 }
10289
10290 bed = get_elf_backend_data (sec->owner);
10291 if (bed->elf_backend_ignore_discarded_relocs != NULL
10292 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10293 return TRUE;
10294
10295 return FALSE;
10296 }
10297
10298 /* Return a mask saying how ld should treat relocations in SEC against
10299 symbols defined in discarded sections. If this function returns
10300 COMPLAIN set, ld will issue a warning message. If this function
10301 returns PRETEND set, and the discarded section was link-once and the
10302 same size as the kept link-once section, ld will pretend that the
10303 symbol was actually defined in the kept section. Otherwise ld will
10304 zero the reloc (at least that is the intent, but some cooperation by
10305 the target dependent code is needed, particularly for REL targets). */
10306
10307 unsigned int
10308 _bfd_elf_default_action_discarded (asection *sec)
10309 {
10310 if (sec->flags & SEC_DEBUGGING)
10311 return PRETEND;
10312
10313 if (strcmp (".eh_frame", sec->name) == 0)
10314 return 0;
10315
10316 if (strcmp (".gcc_except_table", sec->name) == 0)
10317 return 0;
10318
10319 return COMPLAIN | PRETEND;
10320 }
10321
10322 /* Find a match between a section and a member of a section group. */
10323
10324 static asection *
10325 match_group_member (asection *sec, asection *group,
10326 struct bfd_link_info *info)
10327 {
10328 asection *first = elf_next_in_group (group);
10329 asection *s = first;
10330
10331 while (s != NULL)
10332 {
10333 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10334 return s;
10335
10336 s = elf_next_in_group (s);
10337 if (s == first)
10338 break;
10339 }
10340
10341 return NULL;
10342 }
10343
10344 /* Check if the kept section of a discarded section SEC can be used
10345 to replace it. Return the replacement if it is OK. Otherwise return
10346 NULL. */
10347
10348 asection *
10349 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10350 {
10351 asection *kept;
10352
10353 kept = sec->kept_section;
10354 if (kept != NULL)
10355 {
10356 if ((kept->flags & SEC_GROUP) != 0)
10357 kept = match_group_member (sec, kept, info);
10358 if (kept != NULL
10359 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10360 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10361 kept = NULL;
10362 sec->kept_section = kept;
10363 }
10364 return kept;
10365 }
10366
10367 /* Link an input file into the linker output file. This function
10368 handles all the sections and relocations of the input file at once.
10369 This is so that we only have to read the local symbols once, and
10370 don't have to keep them in memory. */
10371
10372 static bfd_boolean
10373 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10374 {
10375 int (*relocate_section)
10376 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10377 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10378 bfd *output_bfd;
10379 Elf_Internal_Shdr *symtab_hdr;
10380 size_t locsymcount;
10381 size_t extsymoff;
10382 Elf_Internal_Sym *isymbuf;
10383 Elf_Internal_Sym *isym;
10384 Elf_Internal_Sym *isymend;
10385 long *pindex;
10386 asection **ppsection;
10387 asection *o;
10388 const struct elf_backend_data *bed;
10389 struct elf_link_hash_entry **sym_hashes;
10390 bfd_size_type address_size;
10391 bfd_vma r_type_mask;
10392 int r_sym_shift;
10393 bfd_boolean have_file_sym = FALSE;
10394
10395 output_bfd = flinfo->output_bfd;
10396 bed = get_elf_backend_data (output_bfd);
10397 relocate_section = bed->elf_backend_relocate_section;
10398
10399 /* If this is a dynamic object, we don't want to do anything here:
10400 we don't want the local symbols, and we don't want the section
10401 contents. */
10402 if ((input_bfd->flags & DYNAMIC) != 0)
10403 return TRUE;
10404
10405 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10406 if (elf_bad_symtab (input_bfd))
10407 {
10408 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10409 extsymoff = 0;
10410 }
10411 else
10412 {
10413 locsymcount = symtab_hdr->sh_info;
10414 extsymoff = symtab_hdr->sh_info;
10415 }
10416
10417 /* Read the local symbols. */
10418 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10419 if (isymbuf == NULL && locsymcount != 0)
10420 {
10421 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10422 flinfo->internal_syms,
10423 flinfo->external_syms,
10424 flinfo->locsym_shndx);
10425 if (isymbuf == NULL)
10426 return FALSE;
10427 }
10428
10429 /* Find local symbol sections and adjust values of symbols in
10430 SEC_MERGE sections. Write out those local symbols we know are
10431 going into the output file. */
10432 isymend = isymbuf + locsymcount;
10433 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10434 isym < isymend;
10435 isym++, pindex++, ppsection++)
10436 {
10437 asection *isec;
10438 const char *name;
10439 Elf_Internal_Sym osym;
10440 long indx;
10441 int ret;
10442
10443 *pindex = -1;
10444
10445 if (elf_bad_symtab (input_bfd))
10446 {
10447 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10448 {
10449 *ppsection = NULL;
10450 continue;
10451 }
10452 }
10453
10454 if (isym->st_shndx == SHN_UNDEF)
10455 isec = bfd_und_section_ptr;
10456 else if (isym->st_shndx == SHN_ABS)
10457 isec = bfd_abs_section_ptr;
10458 else if (isym->st_shndx == SHN_COMMON)
10459 isec = bfd_com_section_ptr;
10460 else
10461 {
10462 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10463 if (isec == NULL)
10464 {
10465 /* Don't attempt to output symbols with st_shnx in the
10466 reserved range other than SHN_ABS and SHN_COMMON. */
10467 isec = bfd_und_section_ptr;
10468 }
10469 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10470 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10471 isym->st_value =
10472 _bfd_merged_section_offset (output_bfd, &isec,
10473 elf_section_data (isec)->sec_info,
10474 isym->st_value);
10475 }
10476
10477 *ppsection = isec;
10478
10479 /* Don't output the first, undefined, symbol. In fact, don't
10480 output any undefined local symbol. */
10481 if (isec == bfd_und_section_ptr)
10482 continue;
10483
10484 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10485 {
10486 /* We never output section symbols. Instead, we use the
10487 section symbol of the corresponding section in the output
10488 file. */
10489 continue;
10490 }
10491
10492 /* If we are stripping all symbols, we don't want to output this
10493 one. */
10494 if (flinfo->info->strip == strip_all)
10495 continue;
10496
10497 /* If we are discarding all local symbols, we don't want to
10498 output this one. If we are generating a relocatable output
10499 file, then some of the local symbols may be required by
10500 relocs; we output them below as we discover that they are
10501 needed. */
10502 if (flinfo->info->discard == discard_all)
10503 continue;
10504
10505 /* If this symbol is defined in a section which we are
10506 discarding, we don't need to keep it. */
10507 if (isym->st_shndx != SHN_UNDEF
10508 && isym->st_shndx < SHN_LORESERVE
10509 && bfd_section_removed_from_list (output_bfd,
10510 isec->output_section))
10511 continue;
10512
10513 /* Get the name of the symbol. */
10514 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10515 isym->st_name);
10516 if (name == NULL)
10517 return FALSE;
10518
10519 /* See if we are discarding symbols with this name. */
10520 if ((flinfo->info->strip == strip_some
10521 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10522 == NULL))
10523 || (((flinfo->info->discard == discard_sec_merge
10524 && (isec->flags & SEC_MERGE)
10525 && !bfd_link_relocatable (flinfo->info))
10526 || flinfo->info->discard == discard_l)
10527 && bfd_is_local_label_name (input_bfd, name)))
10528 continue;
10529
10530 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10531 {
10532 if (input_bfd->lto_output)
10533 /* -flto puts a temp file name here. This means builds
10534 are not reproducible. Discard the symbol. */
10535 continue;
10536 have_file_sym = TRUE;
10537 flinfo->filesym_count += 1;
10538 }
10539 if (!have_file_sym)
10540 {
10541 /* In the absence of debug info, bfd_find_nearest_line uses
10542 FILE symbols to determine the source file for local
10543 function symbols. Provide a FILE symbol here if input
10544 files lack such, so that their symbols won't be
10545 associated with a previous input file. It's not the
10546 source file, but the best we can do. */
10547 have_file_sym = TRUE;
10548 flinfo->filesym_count += 1;
10549 memset (&osym, 0, sizeof (osym));
10550 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10551 osym.st_shndx = SHN_ABS;
10552 if (!elf_link_output_symstrtab (flinfo,
10553 (input_bfd->lto_output ? NULL
10554 : input_bfd->filename),
10555 &osym, bfd_abs_section_ptr,
10556 NULL))
10557 return FALSE;
10558 }
10559
10560 osym = *isym;
10561
10562 /* Adjust the section index for the output file. */
10563 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10564 isec->output_section);
10565 if (osym.st_shndx == SHN_BAD)
10566 return FALSE;
10567
10568 /* ELF symbols in relocatable files are section relative, but
10569 in executable files they are virtual addresses. Note that
10570 this code assumes that all ELF sections have an associated
10571 BFD section with a reasonable value for output_offset; below
10572 we assume that they also have a reasonable value for
10573 output_section. Any special sections must be set up to meet
10574 these requirements. */
10575 osym.st_value += isec->output_offset;
10576 if (!bfd_link_relocatable (flinfo->info))
10577 {
10578 osym.st_value += isec->output_section->vma;
10579 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10580 {
10581 /* STT_TLS symbols are relative to PT_TLS segment base. */
10582 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10583 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10584 else
10585 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10586 STT_NOTYPE);
10587 }
10588 }
10589
10590 indx = bfd_get_symcount (output_bfd);
10591 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10592 if (ret == 0)
10593 return FALSE;
10594 else if (ret == 1)
10595 *pindex = indx;
10596 }
10597
10598 if (bed->s->arch_size == 32)
10599 {
10600 r_type_mask = 0xff;
10601 r_sym_shift = 8;
10602 address_size = 4;
10603 }
10604 else
10605 {
10606 r_type_mask = 0xffffffff;
10607 r_sym_shift = 32;
10608 address_size = 8;
10609 }
10610
10611 /* Relocate the contents of each section. */
10612 sym_hashes = elf_sym_hashes (input_bfd);
10613 for (o = input_bfd->sections; o != NULL; o = o->next)
10614 {
10615 bfd_byte *contents;
10616
10617 if (! o->linker_mark)
10618 {
10619 /* This section was omitted from the link. */
10620 continue;
10621 }
10622
10623 if (!flinfo->info->resolve_section_groups
10624 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10625 {
10626 /* Deal with the group signature symbol. */
10627 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10628 unsigned long symndx = sec_data->this_hdr.sh_info;
10629 asection *osec = o->output_section;
10630
10631 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10632 if (symndx >= locsymcount
10633 || (elf_bad_symtab (input_bfd)
10634 && flinfo->sections[symndx] == NULL))
10635 {
10636 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10637 while (h->root.type == bfd_link_hash_indirect
10638 || h->root.type == bfd_link_hash_warning)
10639 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10640 /* Arrange for symbol to be output. */
10641 h->indx = -2;
10642 elf_section_data (osec)->this_hdr.sh_info = -2;
10643 }
10644 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10645 {
10646 /* We'll use the output section target_index. */
10647 asection *sec = flinfo->sections[symndx]->output_section;
10648 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10649 }
10650 else
10651 {
10652 if (flinfo->indices[symndx] == -1)
10653 {
10654 /* Otherwise output the local symbol now. */
10655 Elf_Internal_Sym sym = isymbuf[symndx];
10656 asection *sec = flinfo->sections[symndx]->output_section;
10657 const char *name;
10658 long indx;
10659 int ret;
10660
10661 name = bfd_elf_string_from_elf_section (input_bfd,
10662 symtab_hdr->sh_link,
10663 sym.st_name);
10664 if (name == NULL)
10665 return FALSE;
10666
10667 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10668 sec);
10669 if (sym.st_shndx == SHN_BAD)
10670 return FALSE;
10671
10672 sym.st_value += o->output_offset;
10673
10674 indx = bfd_get_symcount (output_bfd);
10675 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10676 NULL);
10677 if (ret == 0)
10678 return FALSE;
10679 else if (ret == 1)
10680 flinfo->indices[symndx] = indx;
10681 else
10682 abort ();
10683 }
10684 elf_section_data (osec)->this_hdr.sh_info
10685 = flinfo->indices[symndx];
10686 }
10687 }
10688
10689 if ((o->flags & SEC_HAS_CONTENTS) == 0
10690 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10691 continue;
10692
10693 if ((o->flags & SEC_LINKER_CREATED) != 0)
10694 {
10695 /* Section was created by _bfd_elf_link_create_dynamic_sections
10696 or somesuch. */
10697 continue;
10698 }
10699
10700 /* Get the contents of the section. They have been cached by a
10701 relaxation routine. Note that o is a section in an input
10702 file, so the contents field will not have been set by any of
10703 the routines which work on output files. */
10704 if (elf_section_data (o)->this_hdr.contents != NULL)
10705 {
10706 contents = elf_section_data (o)->this_hdr.contents;
10707 if (bed->caches_rawsize
10708 && o->rawsize != 0
10709 && o->rawsize < o->size)
10710 {
10711 memcpy (flinfo->contents, contents, o->rawsize);
10712 contents = flinfo->contents;
10713 }
10714 }
10715 else
10716 {
10717 contents = flinfo->contents;
10718 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10719 return FALSE;
10720 }
10721
10722 if ((o->flags & SEC_RELOC) != 0)
10723 {
10724 Elf_Internal_Rela *internal_relocs;
10725 Elf_Internal_Rela *rel, *relend;
10726 int action_discarded;
10727 int ret;
10728
10729 /* Get the swapped relocs. */
10730 internal_relocs
10731 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10732 flinfo->internal_relocs, FALSE);
10733 if (internal_relocs == NULL
10734 && o->reloc_count > 0)
10735 return FALSE;
10736
10737 /* We need to reverse-copy input .ctors/.dtors sections if
10738 they are placed in .init_array/.finit_array for output. */
10739 if (o->size > address_size
10740 && ((strncmp (o->name, ".ctors", 6) == 0
10741 && strcmp (o->output_section->name,
10742 ".init_array") == 0)
10743 || (strncmp (o->name, ".dtors", 6) == 0
10744 && strcmp (o->output_section->name,
10745 ".fini_array") == 0))
10746 && (o->name[6] == 0 || o->name[6] == '.'))
10747 {
10748 if (o->size * bed->s->int_rels_per_ext_rel
10749 != o->reloc_count * address_size)
10750 {
10751 _bfd_error_handler
10752 /* xgettext:c-format */
10753 (_("error: %pB: size of section %pA is not "
10754 "multiple of address size"),
10755 input_bfd, o);
10756 bfd_set_error (bfd_error_bad_value);
10757 return FALSE;
10758 }
10759 o->flags |= SEC_ELF_REVERSE_COPY;
10760 }
10761
10762 action_discarded = -1;
10763 if (!elf_section_ignore_discarded_relocs (o))
10764 action_discarded = (*bed->action_discarded) (o);
10765
10766 /* Run through the relocs evaluating complex reloc symbols and
10767 looking for relocs against symbols from discarded sections
10768 or section symbols from removed link-once sections.
10769 Complain about relocs against discarded sections. Zero
10770 relocs against removed link-once sections. */
10771
10772 rel = internal_relocs;
10773 relend = rel + o->reloc_count;
10774 for ( ; rel < relend; rel++)
10775 {
10776 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10777 unsigned int s_type;
10778 asection **ps, *sec;
10779 struct elf_link_hash_entry *h = NULL;
10780 const char *sym_name;
10781
10782 if (r_symndx == STN_UNDEF)
10783 continue;
10784
10785 if (r_symndx >= locsymcount
10786 || (elf_bad_symtab (input_bfd)
10787 && flinfo->sections[r_symndx] == NULL))
10788 {
10789 h = sym_hashes[r_symndx - extsymoff];
10790
10791 /* Badly formatted input files can contain relocs that
10792 reference non-existant symbols. Check here so that
10793 we do not seg fault. */
10794 if (h == NULL)
10795 {
10796 _bfd_error_handler
10797 /* xgettext:c-format */
10798 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10799 "that references a non-existent global symbol"),
10800 input_bfd, (uint64_t) rel->r_info, o);
10801 bfd_set_error (bfd_error_bad_value);
10802 return FALSE;
10803 }
10804
10805 while (h->root.type == bfd_link_hash_indirect
10806 || h->root.type == bfd_link_hash_warning)
10807 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10808
10809 s_type = h->type;
10810
10811 /* If a plugin symbol is referenced from a non-IR file,
10812 mark the symbol as undefined. Note that the
10813 linker may attach linker created dynamic sections
10814 to the plugin bfd. Symbols defined in linker
10815 created sections are not plugin symbols. */
10816 if ((h->root.non_ir_ref_regular
10817 || h->root.non_ir_ref_dynamic)
10818 && (h->root.type == bfd_link_hash_defined
10819 || h->root.type == bfd_link_hash_defweak)
10820 && (h->root.u.def.section->flags
10821 & SEC_LINKER_CREATED) == 0
10822 && h->root.u.def.section->owner != NULL
10823 && (h->root.u.def.section->owner->flags
10824 & BFD_PLUGIN) != 0)
10825 {
10826 h->root.type = bfd_link_hash_undefined;
10827 h->root.u.undef.abfd = h->root.u.def.section->owner;
10828 }
10829
10830 ps = NULL;
10831 if (h->root.type == bfd_link_hash_defined
10832 || h->root.type == bfd_link_hash_defweak)
10833 ps = &h->root.u.def.section;
10834
10835 sym_name = h->root.root.string;
10836 }
10837 else
10838 {
10839 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10840
10841 s_type = ELF_ST_TYPE (sym->st_info);
10842 ps = &flinfo->sections[r_symndx];
10843 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10844 sym, *ps);
10845 }
10846
10847 if ((s_type == STT_RELC || s_type == STT_SRELC)
10848 && !bfd_link_relocatable (flinfo->info))
10849 {
10850 bfd_vma val;
10851 bfd_vma dot = (rel->r_offset
10852 + o->output_offset + o->output_section->vma);
10853 #ifdef DEBUG
10854 printf ("Encountered a complex symbol!");
10855 printf (" (input_bfd %s, section %s, reloc %ld\n",
10856 input_bfd->filename, o->name,
10857 (long) (rel - internal_relocs));
10858 printf (" symbol: idx %8.8lx, name %s\n",
10859 r_symndx, sym_name);
10860 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10861 (unsigned long) rel->r_info,
10862 (unsigned long) rel->r_offset);
10863 #endif
10864 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10865 isymbuf, locsymcount, s_type == STT_SRELC))
10866 return FALSE;
10867
10868 /* Symbol evaluated OK. Update to absolute value. */
10869 set_symbol_value (input_bfd, isymbuf, locsymcount,
10870 r_symndx, val);
10871 continue;
10872 }
10873
10874 if (action_discarded != -1 && ps != NULL)
10875 {
10876 /* Complain if the definition comes from a
10877 discarded section. */
10878 if ((sec = *ps) != NULL && discarded_section (sec))
10879 {
10880 BFD_ASSERT (r_symndx != STN_UNDEF);
10881 if (action_discarded & COMPLAIN)
10882 (*flinfo->info->callbacks->einfo)
10883 /* xgettext:c-format */
10884 (_("%X`%s' referenced in section `%pA' of %pB: "
10885 "defined in discarded section `%pA' of %pB\n"),
10886 sym_name, o, input_bfd, sec, sec->owner);
10887
10888 /* Try to do the best we can to support buggy old
10889 versions of gcc. Pretend that the symbol is
10890 really defined in the kept linkonce section.
10891 FIXME: This is quite broken. Modifying the
10892 symbol here means we will be changing all later
10893 uses of the symbol, not just in this section. */
10894 if (action_discarded & PRETEND)
10895 {
10896 asection *kept;
10897
10898 kept = _bfd_elf_check_kept_section (sec,
10899 flinfo->info);
10900 if (kept != NULL)
10901 {
10902 *ps = kept;
10903 continue;
10904 }
10905 }
10906 }
10907 }
10908 }
10909
10910 /* Relocate the section by invoking a back end routine.
10911
10912 The back end routine is responsible for adjusting the
10913 section contents as necessary, and (if using Rela relocs
10914 and generating a relocatable output file) adjusting the
10915 reloc addend as necessary.
10916
10917 The back end routine does not have to worry about setting
10918 the reloc address or the reloc symbol index.
10919
10920 The back end routine is given a pointer to the swapped in
10921 internal symbols, and can access the hash table entries
10922 for the external symbols via elf_sym_hashes (input_bfd).
10923
10924 When generating relocatable output, the back end routine
10925 must handle STB_LOCAL/STT_SECTION symbols specially. The
10926 output symbol is going to be a section symbol
10927 corresponding to the output section, which will require
10928 the addend to be adjusted. */
10929
10930 ret = (*relocate_section) (output_bfd, flinfo->info,
10931 input_bfd, o, contents,
10932 internal_relocs,
10933 isymbuf,
10934 flinfo->sections);
10935 if (!ret)
10936 return FALSE;
10937
10938 if (ret == 2
10939 || bfd_link_relocatable (flinfo->info)
10940 || flinfo->info->emitrelocations)
10941 {
10942 Elf_Internal_Rela *irela;
10943 Elf_Internal_Rela *irelaend, *irelamid;
10944 bfd_vma last_offset;
10945 struct elf_link_hash_entry **rel_hash;
10946 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10947 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10948 unsigned int next_erel;
10949 bfd_boolean rela_normal;
10950 struct bfd_elf_section_data *esdi, *esdo;
10951
10952 esdi = elf_section_data (o);
10953 esdo = elf_section_data (o->output_section);
10954 rela_normal = FALSE;
10955
10956 /* Adjust the reloc addresses and symbol indices. */
10957
10958 irela = internal_relocs;
10959 irelaend = irela + o->reloc_count;
10960 rel_hash = esdo->rel.hashes + esdo->rel.count;
10961 /* We start processing the REL relocs, if any. When we reach
10962 IRELAMID in the loop, we switch to the RELA relocs. */
10963 irelamid = irela;
10964 if (esdi->rel.hdr != NULL)
10965 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10966 * bed->s->int_rels_per_ext_rel);
10967 rel_hash_list = rel_hash;
10968 rela_hash_list = NULL;
10969 last_offset = o->output_offset;
10970 if (!bfd_link_relocatable (flinfo->info))
10971 last_offset += o->output_section->vma;
10972 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10973 {
10974 unsigned long r_symndx;
10975 asection *sec;
10976 Elf_Internal_Sym sym;
10977
10978 if (next_erel == bed->s->int_rels_per_ext_rel)
10979 {
10980 rel_hash++;
10981 next_erel = 0;
10982 }
10983
10984 if (irela == irelamid)
10985 {
10986 rel_hash = esdo->rela.hashes + esdo->rela.count;
10987 rela_hash_list = rel_hash;
10988 rela_normal = bed->rela_normal;
10989 }
10990
10991 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10992 flinfo->info, o,
10993 irela->r_offset);
10994 if (irela->r_offset >= (bfd_vma) -2)
10995 {
10996 /* This is a reloc for a deleted entry or somesuch.
10997 Turn it into an R_*_NONE reloc, at the same
10998 offset as the last reloc. elf_eh_frame.c and
10999 bfd_elf_discard_info rely on reloc offsets
11000 being ordered. */
11001 irela->r_offset = last_offset;
11002 irela->r_info = 0;
11003 irela->r_addend = 0;
11004 continue;
11005 }
11006
11007 irela->r_offset += o->output_offset;
11008
11009 /* Relocs in an executable have to be virtual addresses. */
11010 if (!bfd_link_relocatable (flinfo->info))
11011 irela->r_offset += o->output_section->vma;
11012
11013 last_offset = irela->r_offset;
11014
11015 r_symndx = irela->r_info >> r_sym_shift;
11016 if (r_symndx == STN_UNDEF)
11017 continue;
11018
11019 if (r_symndx >= locsymcount
11020 || (elf_bad_symtab (input_bfd)
11021 && flinfo->sections[r_symndx] == NULL))
11022 {
11023 struct elf_link_hash_entry *rh;
11024 unsigned long indx;
11025
11026 /* This is a reloc against a global symbol. We
11027 have not yet output all the local symbols, so
11028 we do not know the symbol index of any global
11029 symbol. We set the rel_hash entry for this
11030 reloc to point to the global hash table entry
11031 for this symbol. The symbol index is then
11032 set at the end of bfd_elf_final_link. */
11033 indx = r_symndx - extsymoff;
11034 rh = elf_sym_hashes (input_bfd)[indx];
11035 while (rh->root.type == bfd_link_hash_indirect
11036 || rh->root.type == bfd_link_hash_warning)
11037 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11038
11039 /* Setting the index to -2 tells
11040 elf_link_output_extsym that this symbol is
11041 used by a reloc. */
11042 BFD_ASSERT (rh->indx < 0);
11043 rh->indx = -2;
11044 *rel_hash = rh;
11045
11046 continue;
11047 }
11048
11049 /* This is a reloc against a local symbol. */
11050
11051 *rel_hash = NULL;
11052 sym = isymbuf[r_symndx];
11053 sec = flinfo->sections[r_symndx];
11054 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11055 {
11056 /* I suppose the backend ought to fill in the
11057 section of any STT_SECTION symbol against a
11058 processor specific section. */
11059 r_symndx = STN_UNDEF;
11060 if (bfd_is_abs_section (sec))
11061 ;
11062 else if (sec == NULL || sec->owner == NULL)
11063 {
11064 bfd_set_error (bfd_error_bad_value);
11065 return FALSE;
11066 }
11067 else
11068 {
11069 asection *osec = sec->output_section;
11070
11071 /* If we have discarded a section, the output
11072 section will be the absolute section. In
11073 case of discarded SEC_MERGE sections, use
11074 the kept section. relocate_section should
11075 have already handled discarded linkonce
11076 sections. */
11077 if (bfd_is_abs_section (osec)
11078 && sec->kept_section != NULL
11079 && sec->kept_section->output_section != NULL)
11080 {
11081 osec = sec->kept_section->output_section;
11082 irela->r_addend -= osec->vma;
11083 }
11084
11085 if (!bfd_is_abs_section (osec))
11086 {
11087 r_symndx = osec->target_index;
11088 if (r_symndx == STN_UNDEF)
11089 {
11090 irela->r_addend += osec->vma;
11091 osec = _bfd_nearby_section (output_bfd, osec,
11092 osec->vma);
11093 irela->r_addend -= osec->vma;
11094 r_symndx = osec->target_index;
11095 }
11096 }
11097 }
11098
11099 /* Adjust the addend according to where the
11100 section winds up in the output section. */
11101 if (rela_normal)
11102 irela->r_addend += sec->output_offset;
11103 }
11104 else
11105 {
11106 if (flinfo->indices[r_symndx] == -1)
11107 {
11108 unsigned long shlink;
11109 const char *name;
11110 asection *osec;
11111 long indx;
11112
11113 if (flinfo->info->strip == strip_all)
11114 {
11115 /* You can't do ld -r -s. */
11116 bfd_set_error (bfd_error_invalid_operation);
11117 return FALSE;
11118 }
11119
11120 /* This symbol was skipped earlier, but
11121 since it is needed by a reloc, we
11122 must output it now. */
11123 shlink = symtab_hdr->sh_link;
11124 name = (bfd_elf_string_from_elf_section
11125 (input_bfd, shlink, sym.st_name));
11126 if (name == NULL)
11127 return FALSE;
11128
11129 osec = sec->output_section;
11130 sym.st_shndx =
11131 _bfd_elf_section_from_bfd_section (output_bfd,
11132 osec);
11133 if (sym.st_shndx == SHN_BAD)
11134 return FALSE;
11135
11136 sym.st_value += sec->output_offset;
11137 if (!bfd_link_relocatable (flinfo->info))
11138 {
11139 sym.st_value += osec->vma;
11140 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11141 {
11142 struct elf_link_hash_table *htab
11143 = elf_hash_table (flinfo->info);
11144
11145 /* STT_TLS symbols are relative to PT_TLS
11146 segment base. */
11147 if (htab->tls_sec != NULL)
11148 sym.st_value -= htab->tls_sec->vma;
11149 else
11150 sym.st_info
11151 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11152 STT_NOTYPE);
11153 }
11154 }
11155
11156 indx = bfd_get_symcount (output_bfd);
11157 ret = elf_link_output_symstrtab (flinfo, name,
11158 &sym, sec,
11159 NULL);
11160 if (ret == 0)
11161 return FALSE;
11162 else if (ret == 1)
11163 flinfo->indices[r_symndx] = indx;
11164 else
11165 abort ();
11166 }
11167
11168 r_symndx = flinfo->indices[r_symndx];
11169 }
11170
11171 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11172 | (irela->r_info & r_type_mask));
11173 }
11174
11175 /* Swap out the relocs. */
11176 input_rel_hdr = esdi->rel.hdr;
11177 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11178 {
11179 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11180 input_rel_hdr,
11181 internal_relocs,
11182 rel_hash_list))
11183 return FALSE;
11184 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11185 * bed->s->int_rels_per_ext_rel);
11186 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11187 }
11188
11189 input_rela_hdr = esdi->rela.hdr;
11190 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11191 {
11192 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11193 input_rela_hdr,
11194 internal_relocs,
11195 rela_hash_list))
11196 return FALSE;
11197 }
11198 }
11199 }
11200
11201 /* Write out the modified section contents. */
11202 if (bed->elf_backend_write_section
11203 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11204 contents))
11205 {
11206 /* Section written out. */
11207 }
11208 else switch (o->sec_info_type)
11209 {
11210 case SEC_INFO_TYPE_STABS:
11211 if (! (_bfd_write_section_stabs
11212 (output_bfd,
11213 &elf_hash_table (flinfo->info)->stab_info,
11214 o, &elf_section_data (o)->sec_info, contents)))
11215 return FALSE;
11216 break;
11217 case SEC_INFO_TYPE_MERGE:
11218 if (! _bfd_write_merged_section (output_bfd, o,
11219 elf_section_data (o)->sec_info))
11220 return FALSE;
11221 break;
11222 case SEC_INFO_TYPE_EH_FRAME:
11223 {
11224 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11225 o, contents))
11226 return FALSE;
11227 }
11228 break;
11229 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11230 {
11231 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11232 flinfo->info,
11233 o, contents))
11234 return FALSE;
11235 }
11236 break;
11237 default:
11238 {
11239 if (! (o->flags & SEC_EXCLUDE))
11240 {
11241 file_ptr offset = (file_ptr) o->output_offset;
11242 bfd_size_type todo = o->size;
11243
11244 offset *= bfd_octets_per_byte (output_bfd);
11245
11246 if ((o->flags & SEC_ELF_REVERSE_COPY))
11247 {
11248 /* Reverse-copy input section to output. */
11249 do
11250 {
11251 todo -= address_size;
11252 if (! bfd_set_section_contents (output_bfd,
11253 o->output_section,
11254 contents + todo,
11255 offset,
11256 address_size))
11257 return FALSE;
11258 if (todo == 0)
11259 break;
11260 offset += address_size;
11261 }
11262 while (1);
11263 }
11264 else if (! bfd_set_section_contents (output_bfd,
11265 o->output_section,
11266 contents,
11267 offset, todo))
11268 return FALSE;
11269 }
11270 }
11271 break;
11272 }
11273 }
11274
11275 return TRUE;
11276 }
11277
11278 /* Generate a reloc when linking an ELF file. This is a reloc
11279 requested by the linker, and does not come from any input file. This
11280 is used to build constructor and destructor tables when linking
11281 with -Ur. */
11282
11283 static bfd_boolean
11284 elf_reloc_link_order (bfd *output_bfd,
11285 struct bfd_link_info *info,
11286 asection *output_section,
11287 struct bfd_link_order *link_order)
11288 {
11289 reloc_howto_type *howto;
11290 long indx;
11291 bfd_vma offset;
11292 bfd_vma addend;
11293 struct bfd_elf_section_reloc_data *reldata;
11294 struct elf_link_hash_entry **rel_hash_ptr;
11295 Elf_Internal_Shdr *rel_hdr;
11296 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11297 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11298 bfd_byte *erel;
11299 unsigned int i;
11300 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11301
11302 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11303 if (howto == NULL)
11304 {
11305 bfd_set_error (bfd_error_bad_value);
11306 return FALSE;
11307 }
11308
11309 addend = link_order->u.reloc.p->addend;
11310
11311 if (esdo->rel.hdr)
11312 reldata = &esdo->rel;
11313 else if (esdo->rela.hdr)
11314 reldata = &esdo->rela;
11315 else
11316 {
11317 reldata = NULL;
11318 BFD_ASSERT (0);
11319 }
11320
11321 /* Figure out the symbol index. */
11322 rel_hash_ptr = reldata->hashes + reldata->count;
11323 if (link_order->type == bfd_section_reloc_link_order)
11324 {
11325 indx = link_order->u.reloc.p->u.section->target_index;
11326 BFD_ASSERT (indx != 0);
11327 *rel_hash_ptr = NULL;
11328 }
11329 else
11330 {
11331 struct elf_link_hash_entry *h;
11332
11333 /* Treat a reloc against a defined symbol as though it were
11334 actually against the section. */
11335 h = ((struct elf_link_hash_entry *)
11336 bfd_wrapped_link_hash_lookup (output_bfd, info,
11337 link_order->u.reloc.p->u.name,
11338 FALSE, FALSE, TRUE));
11339 if (h != NULL
11340 && (h->root.type == bfd_link_hash_defined
11341 || h->root.type == bfd_link_hash_defweak))
11342 {
11343 asection *section;
11344
11345 section = h->root.u.def.section;
11346 indx = section->output_section->target_index;
11347 *rel_hash_ptr = NULL;
11348 /* It seems that we ought to add the symbol value to the
11349 addend here, but in practice it has already been added
11350 because it was passed to constructor_callback. */
11351 addend += section->output_section->vma + section->output_offset;
11352 }
11353 else if (h != NULL)
11354 {
11355 /* Setting the index to -2 tells elf_link_output_extsym that
11356 this symbol is used by a reloc. */
11357 h->indx = -2;
11358 *rel_hash_ptr = h;
11359 indx = 0;
11360 }
11361 else
11362 {
11363 (*info->callbacks->unattached_reloc)
11364 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11365 indx = 0;
11366 }
11367 }
11368
11369 /* If this is an inplace reloc, we must write the addend into the
11370 object file. */
11371 if (howto->partial_inplace && addend != 0)
11372 {
11373 bfd_size_type size;
11374 bfd_reloc_status_type rstat;
11375 bfd_byte *buf;
11376 bfd_boolean ok;
11377 const char *sym_name;
11378
11379 size = (bfd_size_type) bfd_get_reloc_size (howto);
11380 buf = (bfd_byte *) bfd_zmalloc (size);
11381 if (buf == NULL && size != 0)
11382 return FALSE;
11383 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11384 switch (rstat)
11385 {
11386 case bfd_reloc_ok:
11387 break;
11388
11389 default:
11390 case bfd_reloc_outofrange:
11391 abort ();
11392
11393 case bfd_reloc_overflow:
11394 if (link_order->type == bfd_section_reloc_link_order)
11395 sym_name = bfd_section_name (output_bfd,
11396 link_order->u.reloc.p->u.section);
11397 else
11398 sym_name = link_order->u.reloc.p->u.name;
11399 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11400 howto->name, addend, NULL, NULL,
11401 (bfd_vma) 0);
11402 break;
11403 }
11404
11405 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11406 link_order->offset
11407 * bfd_octets_per_byte (output_bfd),
11408 size);
11409 free (buf);
11410 if (! ok)
11411 return FALSE;
11412 }
11413
11414 /* The address of a reloc is relative to the section in a
11415 relocatable file, and is a virtual address in an executable
11416 file. */
11417 offset = link_order->offset;
11418 if (! bfd_link_relocatable (info))
11419 offset += output_section->vma;
11420
11421 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11422 {
11423 irel[i].r_offset = offset;
11424 irel[i].r_info = 0;
11425 irel[i].r_addend = 0;
11426 }
11427 if (bed->s->arch_size == 32)
11428 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11429 else
11430 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11431
11432 rel_hdr = reldata->hdr;
11433 erel = rel_hdr->contents;
11434 if (rel_hdr->sh_type == SHT_REL)
11435 {
11436 erel += reldata->count * bed->s->sizeof_rel;
11437 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11438 }
11439 else
11440 {
11441 irel[0].r_addend = addend;
11442 erel += reldata->count * bed->s->sizeof_rela;
11443 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11444 }
11445
11446 ++reldata->count;
11447
11448 return TRUE;
11449 }
11450
11451
11452 /* Get the output vma of the section pointed to by the sh_link field. */
11453
11454 static bfd_vma
11455 elf_get_linked_section_vma (struct bfd_link_order *p)
11456 {
11457 Elf_Internal_Shdr **elf_shdrp;
11458 asection *s;
11459 int elfsec;
11460
11461 s = p->u.indirect.section;
11462 elf_shdrp = elf_elfsections (s->owner);
11463 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11464 elfsec = elf_shdrp[elfsec]->sh_link;
11465 /* PR 290:
11466 The Intel C compiler generates SHT_IA_64_UNWIND with
11467 SHF_LINK_ORDER. But it doesn't set the sh_link or
11468 sh_info fields. Hence we could get the situation
11469 where elfsec is 0. */
11470 if (elfsec == 0)
11471 {
11472 const struct elf_backend_data *bed
11473 = get_elf_backend_data (s->owner);
11474 if (bed->link_order_error_handler)
11475 bed->link_order_error_handler
11476 /* xgettext:c-format */
11477 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11478 return 0;
11479 }
11480 else
11481 {
11482 s = elf_shdrp[elfsec]->bfd_section;
11483 return s->output_section->vma + s->output_offset;
11484 }
11485 }
11486
11487
11488 /* Compare two sections based on the locations of the sections they are
11489 linked to. Used by elf_fixup_link_order. */
11490
11491 static int
11492 compare_link_order (const void * a, const void * b)
11493 {
11494 bfd_vma apos;
11495 bfd_vma bpos;
11496
11497 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11498 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11499 if (apos < bpos)
11500 return -1;
11501 return apos > bpos;
11502 }
11503
11504
11505 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11506 order as their linked sections. Returns false if this could not be done
11507 because an output section includes both ordered and unordered
11508 sections. Ideally we'd do this in the linker proper. */
11509
11510 static bfd_boolean
11511 elf_fixup_link_order (bfd *abfd, asection *o)
11512 {
11513 int seen_linkorder;
11514 int seen_other;
11515 int n;
11516 struct bfd_link_order *p;
11517 bfd *sub;
11518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11519 unsigned elfsec;
11520 struct bfd_link_order **sections;
11521 asection *s, *other_sec, *linkorder_sec;
11522 bfd_vma offset;
11523
11524 other_sec = NULL;
11525 linkorder_sec = NULL;
11526 seen_other = 0;
11527 seen_linkorder = 0;
11528 for (p = o->map_head.link_order; p != NULL; p = p->next)
11529 {
11530 if (p->type == bfd_indirect_link_order)
11531 {
11532 s = p->u.indirect.section;
11533 sub = s->owner;
11534 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11535 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11536 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11537 && elfsec < elf_numsections (sub)
11538 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11539 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11540 {
11541 seen_linkorder++;
11542 linkorder_sec = s;
11543 }
11544 else
11545 {
11546 seen_other++;
11547 other_sec = s;
11548 }
11549 }
11550 else
11551 seen_other++;
11552
11553 if (seen_other && seen_linkorder)
11554 {
11555 if (other_sec && linkorder_sec)
11556 _bfd_error_handler
11557 /* xgettext:c-format */
11558 (_("%pA has both ordered [`%pA' in %pB] "
11559 "and unordered [`%pA' in %pB] sections"),
11560 o, linkorder_sec, linkorder_sec->owner,
11561 other_sec, other_sec->owner);
11562 else
11563 _bfd_error_handler
11564 (_("%pA has both ordered and unordered sections"), o);
11565 bfd_set_error (bfd_error_bad_value);
11566 return FALSE;
11567 }
11568 }
11569
11570 if (!seen_linkorder)
11571 return TRUE;
11572
11573 sections = (struct bfd_link_order **)
11574 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11575 if (sections == NULL)
11576 return FALSE;
11577 seen_linkorder = 0;
11578
11579 for (p = o->map_head.link_order; p != NULL; p = p->next)
11580 {
11581 sections[seen_linkorder++] = p;
11582 }
11583 /* Sort the input sections in the order of their linked section. */
11584 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11585 compare_link_order);
11586
11587 /* Change the offsets of the sections. */
11588 offset = 0;
11589 for (n = 0; n < seen_linkorder; n++)
11590 {
11591 s = sections[n]->u.indirect.section;
11592 offset &= ~(bfd_vma) 0 << s->alignment_power;
11593 s->output_offset = offset / bfd_octets_per_byte (abfd);
11594 sections[n]->offset = offset;
11595 offset += sections[n]->size;
11596 }
11597
11598 free (sections);
11599 return TRUE;
11600 }
11601
11602 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11603 Returns TRUE upon success, FALSE otherwise. */
11604
11605 static bfd_boolean
11606 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11607 {
11608 bfd_boolean ret = FALSE;
11609 bfd *implib_bfd;
11610 const struct elf_backend_data *bed;
11611 flagword flags;
11612 enum bfd_architecture arch;
11613 unsigned int mach;
11614 asymbol **sympp = NULL;
11615 long symsize;
11616 long symcount;
11617 long src_count;
11618 elf_symbol_type *osymbuf;
11619
11620 implib_bfd = info->out_implib_bfd;
11621 bed = get_elf_backend_data (abfd);
11622
11623 if (!bfd_set_format (implib_bfd, bfd_object))
11624 return FALSE;
11625
11626 /* Use flag from executable but make it a relocatable object. */
11627 flags = bfd_get_file_flags (abfd);
11628 flags &= ~HAS_RELOC;
11629 if (!bfd_set_start_address (implib_bfd, 0)
11630 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11631 return FALSE;
11632
11633 /* Copy architecture of output file to import library file. */
11634 arch = bfd_get_arch (abfd);
11635 mach = bfd_get_mach (abfd);
11636 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11637 && (abfd->target_defaulted
11638 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11639 return FALSE;
11640
11641 /* Get symbol table size. */
11642 symsize = bfd_get_symtab_upper_bound (abfd);
11643 if (symsize < 0)
11644 return FALSE;
11645
11646 /* Read in the symbol table. */
11647 sympp = (asymbol **) xmalloc (symsize);
11648 symcount = bfd_canonicalize_symtab (abfd, sympp);
11649 if (symcount < 0)
11650 goto free_sym_buf;
11651
11652 /* Allow the BFD backend to copy any private header data it
11653 understands from the output BFD to the import library BFD. */
11654 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11655 goto free_sym_buf;
11656
11657 /* Filter symbols to appear in the import library. */
11658 if (bed->elf_backend_filter_implib_symbols)
11659 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11660 symcount);
11661 else
11662 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11663 if (symcount == 0)
11664 {
11665 bfd_set_error (bfd_error_no_symbols);
11666 _bfd_error_handler (_("%pB: no symbol found for import library"),
11667 implib_bfd);
11668 goto free_sym_buf;
11669 }
11670
11671
11672 /* Make symbols absolute. */
11673 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11674 sizeof (*osymbuf));
11675 for (src_count = 0; src_count < symcount; src_count++)
11676 {
11677 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11678 sizeof (*osymbuf));
11679 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11680 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11681 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11682 osymbuf[src_count].internal_elf_sym.st_value =
11683 osymbuf[src_count].symbol.value;
11684 sympp[src_count] = &osymbuf[src_count].symbol;
11685 }
11686
11687 bfd_set_symtab (implib_bfd, sympp, symcount);
11688
11689 /* Allow the BFD backend to copy any private data it understands
11690 from the output BFD to the import library BFD. This is done last
11691 to permit the routine to look at the filtered symbol table. */
11692 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11693 goto free_sym_buf;
11694
11695 if (!bfd_close (implib_bfd))
11696 goto free_sym_buf;
11697
11698 ret = TRUE;
11699
11700 free_sym_buf:
11701 free (sympp);
11702 return ret;
11703 }
11704
11705 static void
11706 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11707 {
11708 asection *o;
11709
11710 if (flinfo->symstrtab != NULL)
11711 _bfd_elf_strtab_free (flinfo->symstrtab);
11712 if (flinfo->contents != NULL)
11713 free (flinfo->contents);
11714 if (flinfo->external_relocs != NULL)
11715 free (flinfo->external_relocs);
11716 if (flinfo->internal_relocs != NULL)
11717 free (flinfo->internal_relocs);
11718 if (flinfo->external_syms != NULL)
11719 free (flinfo->external_syms);
11720 if (flinfo->locsym_shndx != NULL)
11721 free (flinfo->locsym_shndx);
11722 if (flinfo->internal_syms != NULL)
11723 free (flinfo->internal_syms);
11724 if (flinfo->indices != NULL)
11725 free (flinfo->indices);
11726 if (flinfo->sections != NULL)
11727 free (flinfo->sections);
11728 if (flinfo->symshndxbuf != NULL
11729 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11730 free (flinfo->symshndxbuf);
11731 for (o = obfd->sections; o != NULL; o = o->next)
11732 {
11733 struct bfd_elf_section_data *esdo = elf_section_data (o);
11734 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11735 free (esdo->rel.hashes);
11736 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11737 free (esdo->rela.hashes);
11738 }
11739 }
11740
11741 /* Do the final step of an ELF link. */
11742
11743 bfd_boolean
11744 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11745 {
11746 bfd_boolean dynamic;
11747 bfd_boolean emit_relocs;
11748 bfd *dynobj;
11749 struct elf_final_link_info flinfo;
11750 asection *o;
11751 struct bfd_link_order *p;
11752 bfd *sub;
11753 bfd_size_type max_contents_size;
11754 bfd_size_type max_external_reloc_size;
11755 bfd_size_type max_internal_reloc_count;
11756 bfd_size_type max_sym_count;
11757 bfd_size_type max_sym_shndx_count;
11758 Elf_Internal_Sym elfsym;
11759 unsigned int i;
11760 Elf_Internal_Shdr *symtab_hdr;
11761 Elf_Internal_Shdr *symtab_shndx_hdr;
11762 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11763 struct elf_outext_info eoinfo;
11764 bfd_boolean merged;
11765 size_t relativecount = 0;
11766 asection *reldyn = 0;
11767 bfd_size_type amt;
11768 asection *attr_section = NULL;
11769 bfd_vma attr_size = 0;
11770 const char *std_attrs_section;
11771 struct elf_link_hash_table *htab = elf_hash_table (info);
11772
11773 if (!is_elf_hash_table (htab))
11774 return FALSE;
11775
11776 if (bfd_link_pic (info))
11777 abfd->flags |= DYNAMIC;
11778
11779 dynamic = htab->dynamic_sections_created;
11780 dynobj = htab->dynobj;
11781
11782 emit_relocs = (bfd_link_relocatable (info)
11783 || info->emitrelocations);
11784
11785 flinfo.info = info;
11786 flinfo.output_bfd = abfd;
11787 flinfo.symstrtab = _bfd_elf_strtab_init ();
11788 if (flinfo.symstrtab == NULL)
11789 return FALSE;
11790
11791 if (! dynamic)
11792 {
11793 flinfo.hash_sec = NULL;
11794 flinfo.symver_sec = NULL;
11795 }
11796 else
11797 {
11798 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11799 /* Note that dynsym_sec can be NULL (on VMS). */
11800 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11801 /* Note that it is OK if symver_sec is NULL. */
11802 }
11803
11804 flinfo.contents = NULL;
11805 flinfo.external_relocs = NULL;
11806 flinfo.internal_relocs = NULL;
11807 flinfo.external_syms = NULL;
11808 flinfo.locsym_shndx = NULL;
11809 flinfo.internal_syms = NULL;
11810 flinfo.indices = NULL;
11811 flinfo.sections = NULL;
11812 flinfo.symshndxbuf = NULL;
11813 flinfo.filesym_count = 0;
11814
11815 /* The object attributes have been merged. Remove the input
11816 sections from the link, and set the contents of the output
11817 secton. */
11818 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11819 for (o = abfd->sections; o != NULL; o = o->next)
11820 {
11821 bfd_boolean remove_section = FALSE;
11822
11823 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11824 || strcmp (o->name, ".gnu.attributes") == 0)
11825 {
11826 for (p = o->map_head.link_order; p != NULL; p = p->next)
11827 {
11828 asection *input_section;
11829
11830 if (p->type != bfd_indirect_link_order)
11831 continue;
11832 input_section = p->u.indirect.section;
11833 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11834 elf_link_input_bfd ignores this section. */
11835 input_section->flags &= ~SEC_HAS_CONTENTS;
11836 }
11837
11838 attr_size = bfd_elf_obj_attr_size (abfd);
11839 bfd_set_section_size (abfd, o, attr_size);
11840 /* Skip this section later on. */
11841 o->map_head.link_order = NULL;
11842 if (attr_size)
11843 attr_section = o;
11844 else
11845 remove_section = TRUE;
11846 }
11847 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11848 {
11849 /* Remove empty group section from linker output. */
11850 remove_section = TRUE;
11851 }
11852 if (remove_section)
11853 {
11854 o->flags |= SEC_EXCLUDE;
11855 bfd_section_list_remove (abfd, o);
11856 abfd->section_count--;
11857 }
11858 }
11859
11860 /* Count up the number of relocations we will output for each output
11861 section, so that we know the sizes of the reloc sections. We
11862 also figure out some maximum sizes. */
11863 max_contents_size = 0;
11864 max_external_reloc_size = 0;
11865 max_internal_reloc_count = 0;
11866 max_sym_count = 0;
11867 max_sym_shndx_count = 0;
11868 merged = FALSE;
11869 for (o = abfd->sections; o != NULL; o = o->next)
11870 {
11871 struct bfd_elf_section_data *esdo = elf_section_data (o);
11872 o->reloc_count = 0;
11873
11874 for (p = o->map_head.link_order; p != NULL; p = p->next)
11875 {
11876 unsigned int reloc_count = 0;
11877 unsigned int additional_reloc_count = 0;
11878 struct bfd_elf_section_data *esdi = NULL;
11879
11880 if (p->type == bfd_section_reloc_link_order
11881 || p->type == bfd_symbol_reloc_link_order)
11882 reloc_count = 1;
11883 else if (p->type == bfd_indirect_link_order)
11884 {
11885 asection *sec;
11886
11887 sec = p->u.indirect.section;
11888
11889 /* Mark all sections which are to be included in the
11890 link. This will normally be every section. We need
11891 to do this so that we can identify any sections which
11892 the linker has decided to not include. */
11893 sec->linker_mark = TRUE;
11894
11895 if (sec->flags & SEC_MERGE)
11896 merged = TRUE;
11897
11898 if (sec->rawsize > max_contents_size)
11899 max_contents_size = sec->rawsize;
11900 if (sec->size > max_contents_size)
11901 max_contents_size = sec->size;
11902
11903 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11904 && (sec->owner->flags & DYNAMIC) == 0)
11905 {
11906 size_t sym_count;
11907
11908 /* We are interested in just local symbols, not all
11909 symbols. */
11910 if (elf_bad_symtab (sec->owner))
11911 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11912 / bed->s->sizeof_sym);
11913 else
11914 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11915
11916 if (sym_count > max_sym_count)
11917 max_sym_count = sym_count;
11918
11919 if (sym_count > max_sym_shndx_count
11920 && elf_symtab_shndx_list (sec->owner) != NULL)
11921 max_sym_shndx_count = sym_count;
11922
11923 if (esdo->this_hdr.sh_type == SHT_REL
11924 || esdo->this_hdr.sh_type == SHT_RELA)
11925 /* Some backends use reloc_count in relocation sections
11926 to count particular types of relocs. Of course,
11927 reloc sections themselves can't have relocations. */
11928 ;
11929 else if (emit_relocs)
11930 {
11931 reloc_count = sec->reloc_count;
11932 if (bed->elf_backend_count_additional_relocs)
11933 {
11934 int c;
11935 c = (*bed->elf_backend_count_additional_relocs) (sec);
11936 additional_reloc_count += c;
11937 }
11938 }
11939 else if (bed->elf_backend_count_relocs)
11940 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11941
11942 esdi = elf_section_data (sec);
11943
11944 if ((sec->flags & SEC_RELOC) != 0)
11945 {
11946 size_t ext_size = 0;
11947
11948 if (esdi->rel.hdr != NULL)
11949 ext_size = esdi->rel.hdr->sh_size;
11950 if (esdi->rela.hdr != NULL)
11951 ext_size += esdi->rela.hdr->sh_size;
11952
11953 if (ext_size > max_external_reloc_size)
11954 max_external_reloc_size = ext_size;
11955 if (sec->reloc_count > max_internal_reloc_count)
11956 max_internal_reloc_count = sec->reloc_count;
11957 }
11958 }
11959 }
11960
11961 if (reloc_count == 0)
11962 continue;
11963
11964 reloc_count += additional_reloc_count;
11965 o->reloc_count += reloc_count;
11966
11967 if (p->type == bfd_indirect_link_order && emit_relocs)
11968 {
11969 if (esdi->rel.hdr)
11970 {
11971 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11972 esdo->rel.count += additional_reloc_count;
11973 }
11974 if (esdi->rela.hdr)
11975 {
11976 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11977 esdo->rela.count += additional_reloc_count;
11978 }
11979 }
11980 else
11981 {
11982 if (o->use_rela_p)
11983 esdo->rela.count += reloc_count;
11984 else
11985 esdo->rel.count += reloc_count;
11986 }
11987 }
11988
11989 if (o->reloc_count > 0)
11990 o->flags |= SEC_RELOC;
11991 else
11992 {
11993 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11994 set it (this is probably a bug) and if it is set
11995 assign_section_numbers will create a reloc section. */
11996 o->flags &=~ SEC_RELOC;
11997 }
11998
11999 /* If the SEC_ALLOC flag is not set, force the section VMA to
12000 zero. This is done in elf_fake_sections as well, but forcing
12001 the VMA to 0 here will ensure that relocs against these
12002 sections are handled correctly. */
12003 if ((o->flags & SEC_ALLOC) == 0
12004 && ! o->user_set_vma)
12005 o->vma = 0;
12006 }
12007
12008 if (! bfd_link_relocatable (info) && merged)
12009 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12010
12011 /* Figure out the file positions for everything but the symbol table
12012 and the relocs. We set symcount to force assign_section_numbers
12013 to create a symbol table. */
12014 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
12015 BFD_ASSERT (! abfd->output_has_begun);
12016 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12017 goto error_return;
12018
12019 /* Set sizes, and assign file positions for reloc sections. */
12020 for (o = abfd->sections; o != NULL; o = o->next)
12021 {
12022 struct bfd_elf_section_data *esdo = elf_section_data (o);
12023 if ((o->flags & SEC_RELOC) != 0)
12024 {
12025 if (esdo->rel.hdr
12026 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12027 goto error_return;
12028
12029 if (esdo->rela.hdr
12030 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12031 goto error_return;
12032 }
12033
12034 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12035 to count upwards while actually outputting the relocations. */
12036 esdo->rel.count = 0;
12037 esdo->rela.count = 0;
12038
12039 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
12040 {
12041 /* Cache the section contents so that they can be compressed
12042 later. Use bfd_malloc since it will be freed by
12043 bfd_compress_section_contents. */
12044 unsigned char *contents = esdo->this_hdr.contents;
12045 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12046 abort ();
12047 contents
12048 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12049 if (contents == NULL)
12050 goto error_return;
12051 esdo->this_hdr.contents = contents;
12052 }
12053 }
12054
12055 /* We have now assigned file positions for all the sections except
12056 .symtab, .strtab, and non-loaded reloc sections. We start the
12057 .symtab section at the current file position, and write directly
12058 to it. We build the .strtab section in memory. */
12059 bfd_get_symcount (abfd) = 0;
12060 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12061 /* sh_name is set in prep_headers. */
12062 symtab_hdr->sh_type = SHT_SYMTAB;
12063 /* sh_flags, sh_addr and sh_size all start off zero. */
12064 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12065 /* sh_link is set in assign_section_numbers. */
12066 /* sh_info is set below. */
12067 /* sh_offset is set just below. */
12068 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12069
12070 if (max_sym_count < 20)
12071 max_sym_count = 20;
12072 htab->strtabsize = max_sym_count;
12073 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12074 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12075 if (htab->strtab == NULL)
12076 goto error_return;
12077 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12078 flinfo.symshndxbuf
12079 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12080 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12081
12082 if (info->strip != strip_all || emit_relocs)
12083 {
12084 file_ptr off = elf_next_file_pos (abfd);
12085
12086 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12087
12088 /* Note that at this point elf_next_file_pos (abfd) is
12089 incorrect. We do not yet know the size of the .symtab section.
12090 We correct next_file_pos below, after we do know the size. */
12091
12092 /* Start writing out the symbol table. The first symbol is always a
12093 dummy symbol. */
12094 elfsym.st_value = 0;
12095 elfsym.st_size = 0;
12096 elfsym.st_info = 0;
12097 elfsym.st_other = 0;
12098 elfsym.st_shndx = SHN_UNDEF;
12099 elfsym.st_target_internal = 0;
12100 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12101 bfd_und_section_ptr, NULL) != 1)
12102 goto error_return;
12103
12104 /* Output a symbol for each section. We output these even if we are
12105 discarding local symbols, since they are used for relocs. These
12106 symbols have no names. We store the index of each one in the
12107 index field of the section, so that we can find it again when
12108 outputting relocs. */
12109
12110 elfsym.st_size = 0;
12111 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12112 elfsym.st_other = 0;
12113 elfsym.st_value = 0;
12114 elfsym.st_target_internal = 0;
12115 for (i = 1; i < elf_numsections (abfd); i++)
12116 {
12117 o = bfd_section_from_elf_index (abfd, i);
12118 if (o != NULL)
12119 {
12120 o->target_index = bfd_get_symcount (abfd);
12121 elfsym.st_shndx = i;
12122 if (!bfd_link_relocatable (info))
12123 elfsym.st_value = o->vma;
12124 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12125 NULL) != 1)
12126 goto error_return;
12127 }
12128 }
12129 }
12130
12131 /* Allocate some memory to hold information read in from the input
12132 files. */
12133 if (max_contents_size != 0)
12134 {
12135 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12136 if (flinfo.contents == NULL)
12137 goto error_return;
12138 }
12139
12140 if (max_external_reloc_size != 0)
12141 {
12142 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12143 if (flinfo.external_relocs == NULL)
12144 goto error_return;
12145 }
12146
12147 if (max_internal_reloc_count != 0)
12148 {
12149 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12150 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12151 if (flinfo.internal_relocs == NULL)
12152 goto error_return;
12153 }
12154
12155 if (max_sym_count != 0)
12156 {
12157 amt = max_sym_count * bed->s->sizeof_sym;
12158 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12159 if (flinfo.external_syms == NULL)
12160 goto error_return;
12161
12162 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12163 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12164 if (flinfo.internal_syms == NULL)
12165 goto error_return;
12166
12167 amt = max_sym_count * sizeof (long);
12168 flinfo.indices = (long int *) bfd_malloc (amt);
12169 if (flinfo.indices == NULL)
12170 goto error_return;
12171
12172 amt = max_sym_count * sizeof (asection *);
12173 flinfo.sections = (asection **) bfd_malloc (amt);
12174 if (flinfo.sections == NULL)
12175 goto error_return;
12176 }
12177
12178 if (max_sym_shndx_count != 0)
12179 {
12180 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12181 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12182 if (flinfo.locsym_shndx == NULL)
12183 goto error_return;
12184 }
12185
12186 if (htab->tls_sec)
12187 {
12188 bfd_vma base, end = 0;
12189 asection *sec;
12190
12191 for (sec = htab->tls_sec;
12192 sec && (sec->flags & SEC_THREAD_LOCAL);
12193 sec = sec->next)
12194 {
12195 bfd_size_type size = sec->size;
12196
12197 if (size == 0
12198 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12199 {
12200 struct bfd_link_order *ord = sec->map_tail.link_order;
12201
12202 if (ord != NULL)
12203 size = ord->offset + ord->size;
12204 }
12205 end = sec->vma + size;
12206 }
12207 base = htab->tls_sec->vma;
12208 /* Only align end of TLS section if static TLS doesn't have special
12209 alignment requirements. */
12210 if (bed->static_tls_alignment == 1)
12211 end = align_power (end, htab->tls_sec->alignment_power);
12212 htab->tls_size = end - base;
12213 }
12214
12215 /* Reorder SHF_LINK_ORDER sections. */
12216 for (o = abfd->sections; o != NULL; o = o->next)
12217 {
12218 if (!elf_fixup_link_order (abfd, o))
12219 return FALSE;
12220 }
12221
12222 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12223 return FALSE;
12224
12225 /* Since ELF permits relocations to be against local symbols, we
12226 must have the local symbols available when we do the relocations.
12227 Since we would rather only read the local symbols once, and we
12228 would rather not keep them in memory, we handle all the
12229 relocations for a single input file at the same time.
12230
12231 Unfortunately, there is no way to know the total number of local
12232 symbols until we have seen all of them, and the local symbol
12233 indices precede the global symbol indices. This means that when
12234 we are generating relocatable output, and we see a reloc against
12235 a global symbol, we can not know the symbol index until we have
12236 finished examining all the local symbols to see which ones we are
12237 going to output. To deal with this, we keep the relocations in
12238 memory, and don't output them until the end of the link. This is
12239 an unfortunate waste of memory, but I don't see a good way around
12240 it. Fortunately, it only happens when performing a relocatable
12241 link, which is not the common case. FIXME: If keep_memory is set
12242 we could write the relocs out and then read them again; I don't
12243 know how bad the memory loss will be. */
12244
12245 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12246 sub->output_has_begun = FALSE;
12247 for (o = abfd->sections; o != NULL; o = o->next)
12248 {
12249 for (p = o->map_head.link_order; p != NULL; p = p->next)
12250 {
12251 if (p->type == bfd_indirect_link_order
12252 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12253 == bfd_target_elf_flavour)
12254 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12255 {
12256 if (! sub->output_has_begun)
12257 {
12258 if (! elf_link_input_bfd (&flinfo, sub))
12259 goto error_return;
12260 sub->output_has_begun = TRUE;
12261 }
12262 }
12263 else if (p->type == bfd_section_reloc_link_order
12264 || p->type == bfd_symbol_reloc_link_order)
12265 {
12266 if (! elf_reloc_link_order (abfd, info, o, p))
12267 goto error_return;
12268 }
12269 else
12270 {
12271 if (! _bfd_default_link_order (abfd, info, o, p))
12272 {
12273 if (p->type == bfd_indirect_link_order
12274 && (bfd_get_flavour (sub)
12275 == bfd_target_elf_flavour)
12276 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12277 != bed->s->elfclass))
12278 {
12279 const char *iclass, *oclass;
12280
12281 switch (bed->s->elfclass)
12282 {
12283 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12284 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12285 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12286 default: abort ();
12287 }
12288
12289 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12290 {
12291 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12292 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12293 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12294 default: abort ();
12295 }
12296
12297 bfd_set_error (bfd_error_wrong_format);
12298 _bfd_error_handler
12299 /* xgettext:c-format */
12300 (_("%pB: file class %s incompatible with %s"),
12301 sub, iclass, oclass);
12302 }
12303
12304 goto error_return;
12305 }
12306 }
12307 }
12308 }
12309
12310 /* Free symbol buffer if needed. */
12311 if (!info->reduce_memory_overheads)
12312 {
12313 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12314 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12315 && elf_tdata (sub)->symbuf)
12316 {
12317 free (elf_tdata (sub)->symbuf);
12318 elf_tdata (sub)->symbuf = NULL;
12319 }
12320 }
12321
12322 /* Output any global symbols that got converted to local in a
12323 version script or due to symbol visibility. We do this in a
12324 separate step since ELF requires all local symbols to appear
12325 prior to any global symbols. FIXME: We should only do this if
12326 some global symbols were, in fact, converted to become local.
12327 FIXME: Will this work correctly with the Irix 5 linker? */
12328 eoinfo.failed = FALSE;
12329 eoinfo.flinfo = &flinfo;
12330 eoinfo.localsyms = TRUE;
12331 eoinfo.file_sym_done = FALSE;
12332 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12333 if (eoinfo.failed)
12334 return FALSE;
12335
12336 /* If backend needs to output some local symbols not present in the hash
12337 table, do it now. */
12338 if (bed->elf_backend_output_arch_local_syms
12339 && (info->strip != strip_all || emit_relocs))
12340 {
12341 typedef int (*out_sym_func)
12342 (void *, const char *, Elf_Internal_Sym *, asection *,
12343 struct elf_link_hash_entry *);
12344
12345 if (! ((*bed->elf_backend_output_arch_local_syms)
12346 (abfd, info, &flinfo,
12347 (out_sym_func) elf_link_output_symstrtab)))
12348 return FALSE;
12349 }
12350
12351 /* That wrote out all the local symbols. Finish up the symbol table
12352 with the global symbols. Even if we want to strip everything we
12353 can, we still need to deal with those global symbols that got
12354 converted to local in a version script. */
12355
12356 /* The sh_info field records the index of the first non local symbol. */
12357 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12358
12359 if (dynamic
12360 && htab->dynsym != NULL
12361 && htab->dynsym->output_section != bfd_abs_section_ptr)
12362 {
12363 Elf_Internal_Sym sym;
12364 bfd_byte *dynsym = htab->dynsym->contents;
12365
12366 o = htab->dynsym->output_section;
12367 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12368
12369 /* Write out the section symbols for the output sections. */
12370 if (bfd_link_pic (info)
12371 || htab->is_relocatable_executable)
12372 {
12373 asection *s;
12374
12375 sym.st_size = 0;
12376 sym.st_name = 0;
12377 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12378 sym.st_other = 0;
12379 sym.st_target_internal = 0;
12380
12381 for (s = abfd->sections; s != NULL; s = s->next)
12382 {
12383 int indx;
12384 bfd_byte *dest;
12385 long dynindx;
12386
12387 dynindx = elf_section_data (s)->dynindx;
12388 if (dynindx <= 0)
12389 continue;
12390 indx = elf_section_data (s)->this_idx;
12391 BFD_ASSERT (indx > 0);
12392 sym.st_shndx = indx;
12393 if (! check_dynsym (abfd, &sym))
12394 return FALSE;
12395 sym.st_value = s->vma;
12396 dest = dynsym + dynindx * bed->s->sizeof_sym;
12397 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12398 }
12399 }
12400
12401 /* Write out the local dynsyms. */
12402 if (htab->dynlocal)
12403 {
12404 struct elf_link_local_dynamic_entry *e;
12405 for (e = htab->dynlocal; e ; e = e->next)
12406 {
12407 asection *s;
12408 bfd_byte *dest;
12409
12410 /* Copy the internal symbol and turn off visibility.
12411 Note that we saved a word of storage and overwrote
12412 the original st_name with the dynstr_index. */
12413 sym = e->isym;
12414 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12415
12416 s = bfd_section_from_elf_index (e->input_bfd,
12417 e->isym.st_shndx);
12418 if (s != NULL)
12419 {
12420 sym.st_shndx =
12421 elf_section_data (s->output_section)->this_idx;
12422 if (! check_dynsym (abfd, &sym))
12423 return FALSE;
12424 sym.st_value = (s->output_section->vma
12425 + s->output_offset
12426 + e->isym.st_value);
12427 }
12428
12429 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12430 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12431 }
12432 }
12433 }
12434
12435 /* We get the global symbols from the hash table. */
12436 eoinfo.failed = FALSE;
12437 eoinfo.localsyms = FALSE;
12438 eoinfo.flinfo = &flinfo;
12439 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12440 if (eoinfo.failed)
12441 return FALSE;
12442
12443 /* If backend needs to output some symbols not present in the hash
12444 table, do it now. */
12445 if (bed->elf_backend_output_arch_syms
12446 && (info->strip != strip_all || emit_relocs))
12447 {
12448 typedef int (*out_sym_func)
12449 (void *, const char *, Elf_Internal_Sym *, asection *,
12450 struct elf_link_hash_entry *);
12451
12452 if (! ((*bed->elf_backend_output_arch_syms)
12453 (abfd, info, &flinfo,
12454 (out_sym_func) elf_link_output_symstrtab)))
12455 return FALSE;
12456 }
12457
12458 /* Finalize the .strtab section. */
12459 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12460
12461 /* Swap out the .strtab section. */
12462 if (!elf_link_swap_symbols_out (&flinfo))
12463 return FALSE;
12464
12465 /* Now we know the size of the symtab section. */
12466 if (bfd_get_symcount (abfd) > 0)
12467 {
12468 /* Finish up and write out the symbol string table (.strtab)
12469 section. */
12470 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12471 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12472
12473 if (elf_symtab_shndx_list (abfd))
12474 {
12475 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12476
12477 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12478 {
12479 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12480 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12481 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12482 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12483 symtab_shndx_hdr->sh_size = amt;
12484
12485 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12486 off, TRUE);
12487
12488 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12489 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12490 return FALSE;
12491 }
12492 }
12493
12494 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12495 /* sh_name was set in prep_headers. */
12496 symstrtab_hdr->sh_type = SHT_STRTAB;
12497 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12498 symstrtab_hdr->sh_addr = 0;
12499 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12500 symstrtab_hdr->sh_entsize = 0;
12501 symstrtab_hdr->sh_link = 0;
12502 symstrtab_hdr->sh_info = 0;
12503 /* sh_offset is set just below. */
12504 symstrtab_hdr->sh_addralign = 1;
12505
12506 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12507 off, TRUE);
12508 elf_next_file_pos (abfd) = off;
12509
12510 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12511 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12512 return FALSE;
12513 }
12514
12515 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12516 {
12517 _bfd_error_handler (_("%pB: failed to generate import library"),
12518 info->out_implib_bfd);
12519 return FALSE;
12520 }
12521
12522 /* Adjust the relocs to have the correct symbol indices. */
12523 for (o = abfd->sections; o != NULL; o = o->next)
12524 {
12525 struct bfd_elf_section_data *esdo = elf_section_data (o);
12526 bfd_boolean sort;
12527
12528 if ((o->flags & SEC_RELOC) == 0)
12529 continue;
12530
12531 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12532 if (esdo->rel.hdr != NULL
12533 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12534 return FALSE;
12535 if (esdo->rela.hdr != NULL
12536 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12537 return FALSE;
12538
12539 /* Set the reloc_count field to 0 to prevent write_relocs from
12540 trying to swap the relocs out itself. */
12541 o->reloc_count = 0;
12542 }
12543
12544 if (dynamic && info->combreloc && dynobj != NULL)
12545 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12546
12547 /* If we are linking against a dynamic object, or generating a
12548 shared library, finish up the dynamic linking information. */
12549 if (dynamic)
12550 {
12551 bfd_byte *dyncon, *dynconend;
12552
12553 /* Fix up .dynamic entries. */
12554 o = bfd_get_linker_section (dynobj, ".dynamic");
12555 BFD_ASSERT (o != NULL);
12556
12557 dyncon = o->contents;
12558 dynconend = o->contents + o->size;
12559 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12560 {
12561 Elf_Internal_Dyn dyn;
12562 const char *name;
12563 unsigned int type;
12564 bfd_size_type sh_size;
12565 bfd_vma sh_addr;
12566
12567 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12568
12569 switch (dyn.d_tag)
12570 {
12571 default:
12572 continue;
12573 case DT_NULL:
12574 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12575 {
12576 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12577 {
12578 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12579 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12580 default: continue;
12581 }
12582 dyn.d_un.d_val = relativecount;
12583 relativecount = 0;
12584 break;
12585 }
12586 continue;
12587
12588 case DT_INIT:
12589 name = info->init_function;
12590 goto get_sym;
12591 case DT_FINI:
12592 name = info->fini_function;
12593 get_sym:
12594 {
12595 struct elf_link_hash_entry *h;
12596
12597 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12598 if (h != NULL
12599 && (h->root.type == bfd_link_hash_defined
12600 || h->root.type == bfd_link_hash_defweak))
12601 {
12602 dyn.d_un.d_ptr = h->root.u.def.value;
12603 o = h->root.u.def.section;
12604 if (o->output_section != NULL)
12605 dyn.d_un.d_ptr += (o->output_section->vma
12606 + o->output_offset);
12607 else
12608 {
12609 /* The symbol is imported from another shared
12610 library and does not apply to this one. */
12611 dyn.d_un.d_ptr = 0;
12612 }
12613 break;
12614 }
12615 }
12616 continue;
12617
12618 case DT_PREINIT_ARRAYSZ:
12619 name = ".preinit_array";
12620 goto get_out_size;
12621 case DT_INIT_ARRAYSZ:
12622 name = ".init_array";
12623 goto get_out_size;
12624 case DT_FINI_ARRAYSZ:
12625 name = ".fini_array";
12626 get_out_size:
12627 o = bfd_get_section_by_name (abfd, name);
12628 if (o == NULL)
12629 {
12630 _bfd_error_handler
12631 (_("could not find section %s"), name);
12632 goto error_return;
12633 }
12634 if (o->size == 0)
12635 _bfd_error_handler
12636 (_("warning: %s section has zero size"), name);
12637 dyn.d_un.d_val = o->size;
12638 break;
12639
12640 case DT_PREINIT_ARRAY:
12641 name = ".preinit_array";
12642 goto get_out_vma;
12643 case DT_INIT_ARRAY:
12644 name = ".init_array";
12645 goto get_out_vma;
12646 case DT_FINI_ARRAY:
12647 name = ".fini_array";
12648 get_out_vma:
12649 o = bfd_get_section_by_name (abfd, name);
12650 goto do_vma;
12651
12652 case DT_HASH:
12653 name = ".hash";
12654 goto get_vma;
12655 case DT_GNU_HASH:
12656 name = ".gnu.hash";
12657 goto get_vma;
12658 case DT_STRTAB:
12659 name = ".dynstr";
12660 goto get_vma;
12661 case DT_SYMTAB:
12662 name = ".dynsym";
12663 goto get_vma;
12664 case DT_VERDEF:
12665 name = ".gnu.version_d";
12666 goto get_vma;
12667 case DT_VERNEED:
12668 name = ".gnu.version_r";
12669 goto get_vma;
12670 case DT_VERSYM:
12671 name = ".gnu.version";
12672 get_vma:
12673 o = bfd_get_linker_section (dynobj, name);
12674 do_vma:
12675 if (o == NULL || bfd_is_abs_section (o->output_section))
12676 {
12677 _bfd_error_handler
12678 (_("could not find section %s"), name);
12679 goto error_return;
12680 }
12681 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12682 {
12683 _bfd_error_handler
12684 (_("warning: section '%s' is being made into a note"), name);
12685 bfd_set_error (bfd_error_nonrepresentable_section);
12686 goto error_return;
12687 }
12688 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12689 break;
12690
12691 case DT_REL:
12692 case DT_RELA:
12693 case DT_RELSZ:
12694 case DT_RELASZ:
12695 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12696 type = SHT_REL;
12697 else
12698 type = SHT_RELA;
12699 sh_size = 0;
12700 sh_addr = 0;
12701 for (i = 1; i < elf_numsections (abfd); i++)
12702 {
12703 Elf_Internal_Shdr *hdr;
12704
12705 hdr = elf_elfsections (abfd)[i];
12706 if (hdr->sh_type == type
12707 && (hdr->sh_flags & SHF_ALLOC) != 0)
12708 {
12709 sh_size += hdr->sh_size;
12710 if (sh_addr == 0
12711 || sh_addr > hdr->sh_addr)
12712 sh_addr = hdr->sh_addr;
12713 }
12714 }
12715
12716 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12717 {
12718 /* Don't count procedure linkage table relocs in the
12719 overall reloc count. */
12720 sh_size -= htab->srelplt->size;
12721 if (sh_size == 0)
12722 /* If the size is zero, make the address zero too.
12723 This is to avoid a glibc bug. If the backend
12724 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12725 zero, then we'll put DT_RELA at the end of
12726 DT_JMPREL. glibc will interpret the end of
12727 DT_RELA matching the end of DT_JMPREL as the
12728 case where DT_RELA includes DT_JMPREL, and for
12729 LD_BIND_NOW will decide that processing DT_RELA
12730 will process the PLT relocs too. Net result:
12731 No PLT relocs applied. */
12732 sh_addr = 0;
12733
12734 /* If .rela.plt is the first .rela section, exclude
12735 it from DT_RELA. */
12736 else if (sh_addr == (htab->srelplt->output_section->vma
12737 + htab->srelplt->output_offset))
12738 sh_addr += htab->srelplt->size;
12739 }
12740
12741 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12742 dyn.d_un.d_val = sh_size;
12743 else
12744 dyn.d_un.d_ptr = sh_addr;
12745 break;
12746 }
12747 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12748 }
12749 }
12750
12751 /* If we have created any dynamic sections, then output them. */
12752 if (dynobj != NULL)
12753 {
12754 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12755 goto error_return;
12756
12757 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12758 if (((info->warn_shared_textrel && bfd_link_pic (info))
12759 || info->error_textrel)
12760 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12761 {
12762 bfd_byte *dyncon, *dynconend;
12763
12764 dyncon = o->contents;
12765 dynconend = o->contents + o->size;
12766 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12767 {
12768 Elf_Internal_Dyn dyn;
12769
12770 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12771
12772 if (dyn.d_tag == DT_TEXTREL)
12773 {
12774 if (info->error_textrel)
12775 info->callbacks->einfo
12776 (_("%P%X: read-only segment has dynamic relocations\n"));
12777 else
12778 info->callbacks->einfo
12779 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12780 break;
12781 }
12782 }
12783 }
12784
12785 for (o = dynobj->sections; o != NULL; o = o->next)
12786 {
12787 if ((o->flags & SEC_HAS_CONTENTS) == 0
12788 || o->size == 0
12789 || o->output_section == bfd_abs_section_ptr)
12790 continue;
12791 if ((o->flags & SEC_LINKER_CREATED) == 0)
12792 {
12793 /* At this point, we are only interested in sections
12794 created by _bfd_elf_link_create_dynamic_sections. */
12795 continue;
12796 }
12797 if (htab->stab_info.stabstr == o)
12798 continue;
12799 if (htab->eh_info.hdr_sec == o)
12800 continue;
12801 if (strcmp (o->name, ".dynstr") != 0)
12802 {
12803 if (! bfd_set_section_contents (abfd, o->output_section,
12804 o->contents,
12805 (file_ptr) o->output_offset
12806 * bfd_octets_per_byte (abfd),
12807 o->size))
12808 goto error_return;
12809 }
12810 else
12811 {
12812 /* The contents of the .dynstr section are actually in a
12813 stringtab. */
12814 file_ptr off;
12815
12816 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12817 if (bfd_seek (abfd, off, SEEK_SET) != 0
12818 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12819 goto error_return;
12820 }
12821 }
12822 }
12823
12824 if (!info->resolve_section_groups)
12825 {
12826 bfd_boolean failed = FALSE;
12827
12828 BFD_ASSERT (bfd_link_relocatable (info));
12829 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12830 if (failed)
12831 goto error_return;
12832 }
12833
12834 /* If we have optimized stabs strings, output them. */
12835 if (htab->stab_info.stabstr != NULL)
12836 {
12837 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12838 goto error_return;
12839 }
12840
12841 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12842 goto error_return;
12843
12844 elf_final_link_free (abfd, &flinfo);
12845
12846 if (attr_section)
12847 {
12848 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12849 if (contents == NULL)
12850 return FALSE; /* Bail out and fail. */
12851 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12852 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12853 free (contents);
12854 }
12855
12856 return TRUE;
12857
12858 error_return:
12859 elf_final_link_free (abfd, &flinfo);
12860 return FALSE;
12861 }
12862 \f
12863 /* Initialize COOKIE for input bfd ABFD. */
12864
12865 static bfd_boolean
12866 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12867 struct bfd_link_info *info, bfd *abfd)
12868 {
12869 Elf_Internal_Shdr *symtab_hdr;
12870 const struct elf_backend_data *bed;
12871
12872 bed = get_elf_backend_data (abfd);
12873 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12874
12875 cookie->abfd = abfd;
12876 cookie->sym_hashes = elf_sym_hashes (abfd);
12877 cookie->bad_symtab = elf_bad_symtab (abfd);
12878 if (cookie->bad_symtab)
12879 {
12880 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12881 cookie->extsymoff = 0;
12882 }
12883 else
12884 {
12885 cookie->locsymcount = symtab_hdr->sh_info;
12886 cookie->extsymoff = symtab_hdr->sh_info;
12887 }
12888
12889 if (bed->s->arch_size == 32)
12890 cookie->r_sym_shift = 8;
12891 else
12892 cookie->r_sym_shift = 32;
12893
12894 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12895 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12896 {
12897 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12898 cookie->locsymcount, 0,
12899 NULL, NULL, NULL);
12900 if (cookie->locsyms == NULL)
12901 {
12902 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12903 return FALSE;
12904 }
12905 if (info->keep_memory)
12906 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12907 }
12908 return TRUE;
12909 }
12910
12911 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12912
12913 static void
12914 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12915 {
12916 Elf_Internal_Shdr *symtab_hdr;
12917
12918 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12919 if (cookie->locsyms != NULL
12920 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12921 free (cookie->locsyms);
12922 }
12923
12924 /* Initialize the relocation information in COOKIE for input section SEC
12925 of input bfd ABFD. */
12926
12927 static bfd_boolean
12928 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12929 struct bfd_link_info *info, bfd *abfd,
12930 asection *sec)
12931 {
12932 if (sec->reloc_count == 0)
12933 {
12934 cookie->rels = NULL;
12935 cookie->relend = NULL;
12936 }
12937 else
12938 {
12939 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12940 info->keep_memory);
12941 if (cookie->rels == NULL)
12942 return FALSE;
12943 cookie->rel = cookie->rels;
12944 cookie->relend = cookie->rels + sec->reloc_count;
12945 }
12946 cookie->rel = cookie->rels;
12947 return TRUE;
12948 }
12949
12950 /* Free the memory allocated by init_reloc_cookie_rels,
12951 if appropriate. */
12952
12953 static void
12954 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12955 asection *sec)
12956 {
12957 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12958 free (cookie->rels);
12959 }
12960
12961 /* Initialize the whole of COOKIE for input section SEC. */
12962
12963 static bfd_boolean
12964 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12965 struct bfd_link_info *info,
12966 asection *sec)
12967 {
12968 if (!init_reloc_cookie (cookie, info, sec->owner))
12969 goto error1;
12970 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12971 goto error2;
12972 return TRUE;
12973
12974 error2:
12975 fini_reloc_cookie (cookie, sec->owner);
12976 error1:
12977 return FALSE;
12978 }
12979
12980 /* Free the memory allocated by init_reloc_cookie_for_section,
12981 if appropriate. */
12982
12983 static void
12984 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12985 asection *sec)
12986 {
12987 fini_reloc_cookie_rels (cookie, sec);
12988 fini_reloc_cookie (cookie, sec->owner);
12989 }
12990 \f
12991 /* Garbage collect unused sections. */
12992
12993 /* Default gc_mark_hook. */
12994
12995 asection *
12996 _bfd_elf_gc_mark_hook (asection *sec,
12997 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12998 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12999 struct elf_link_hash_entry *h,
13000 Elf_Internal_Sym *sym)
13001 {
13002 if (h != NULL)
13003 {
13004 switch (h->root.type)
13005 {
13006 case bfd_link_hash_defined:
13007 case bfd_link_hash_defweak:
13008 return h->root.u.def.section;
13009
13010 case bfd_link_hash_common:
13011 return h->root.u.c.p->section;
13012
13013 default:
13014 break;
13015 }
13016 }
13017 else
13018 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13019
13020 return NULL;
13021 }
13022
13023 /* Return the debug definition section. */
13024
13025 static asection *
13026 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13027 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13028 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13029 struct elf_link_hash_entry *h,
13030 Elf_Internal_Sym *sym)
13031 {
13032 if (h != NULL)
13033 {
13034 /* Return the global debug definition section. */
13035 if ((h->root.type == bfd_link_hash_defined
13036 || h->root.type == bfd_link_hash_defweak)
13037 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13038 return h->root.u.def.section;
13039 }
13040 else
13041 {
13042 /* Return the local debug definition section. */
13043 asection *isec = bfd_section_from_elf_index (sec->owner,
13044 sym->st_shndx);
13045 if ((isec->flags & SEC_DEBUGGING) != 0)
13046 return isec;
13047 }
13048
13049 return NULL;
13050 }
13051
13052 /* COOKIE->rel describes a relocation against section SEC, which is
13053 a section we've decided to keep. Return the section that contains
13054 the relocation symbol, or NULL if no section contains it. */
13055
13056 asection *
13057 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13058 elf_gc_mark_hook_fn gc_mark_hook,
13059 struct elf_reloc_cookie *cookie,
13060 bfd_boolean *start_stop)
13061 {
13062 unsigned long r_symndx;
13063 struct elf_link_hash_entry *h;
13064
13065 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13066 if (r_symndx == STN_UNDEF)
13067 return NULL;
13068
13069 if (r_symndx >= cookie->locsymcount
13070 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13071 {
13072 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13073 if (h == NULL)
13074 {
13075 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13076 sec->owner);
13077 return NULL;
13078 }
13079 while (h->root.type == bfd_link_hash_indirect
13080 || h->root.type == bfd_link_hash_warning)
13081 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13082 h->mark = 1;
13083 /* If this symbol is weak and there is a non-weak definition, we
13084 keep the non-weak definition because many backends put
13085 dynamic reloc info on the non-weak definition for code
13086 handling copy relocs. */
13087 if (h->is_weakalias)
13088 weakdef (h)->mark = 1;
13089
13090 if (start_stop != NULL)
13091 {
13092 /* To work around a glibc bug, mark XXX input sections
13093 when there is a reference to __start_XXX or __stop_XXX
13094 symbols. */
13095 if (h->start_stop)
13096 {
13097 asection *s = h->u2.start_stop_section;
13098 *start_stop = !s->gc_mark;
13099 return s;
13100 }
13101 }
13102
13103 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13104 }
13105
13106 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13107 &cookie->locsyms[r_symndx]);
13108 }
13109
13110 /* COOKIE->rel describes a relocation against section SEC, which is
13111 a section we've decided to keep. Mark the section that contains
13112 the relocation symbol. */
13113
13114 bfd_boolean
13115 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13116 asection *sec,
13117 elf_gc_mark_hook_fn gc_mark_hook,
13118 struct elf_reloc_cookie *cookie)
13119 {
13120 asection *rsec;
13121 bfd_boolean start_stop = FALSE;
13122
13123 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13124 while (rsec != NULL)
13125 {
13126 if (!rsec->gc_mark)
13127 {
13128 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13129 || (rsec->owner->flags & DYNAMIC) != 0)
13130 rsec->gc_mark = 1;
13131 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13132 return FALSE;
13133 }
13134 if (!start_stop)
13135 break;
13136 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13137 }
13138 return TRUE;
13139 }
13140
13141 /* The mark phase of garbage collection. For a given section, mark
13142 it and any sections in this section's group, and all the sections
13143 which define symbols to which it refers. */
13144
13145 bfd_boolean
13146 _bfd_elf_gc_mark (struct bfd_link_info *info,
13147 asection *sec,
13148 elf_gc_mark_hook_fn gc_mark_hook)
13149 {
13150 bfd_boolean ret;
13151 asection *group_sec, *eh_frame;
13152
13153 sec->gc_mark = 1;
13154
13155 /* Mark all the sections in the group. */
13156 group_sec = elf_section_data (sec)->next_in_group;
13157 if (group_sec && !group_sec->gc_mark)
13158 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13159 return FALSE;
13160
13161 /* Look through the section relocs. */
13162 ret = TRUE;
13163 eh_frame = elf_eh_frame_section (sec->owner);
13164 if ((sec->flags & SEC_RELOC) != 0
13165 && sec->reloc_count > 0
13166 && sec != eh_frame)
13167 {
13168 struct elf_reloc_cookie cookie;
13169
13170 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13171 ret = FALSE;
13172 else
13173 {
13174 for (; cookie.rel < cookie.relend; cookie.rel++)
13175 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13176 {
13177 ret = FALSE;
13178 break;
13179 }
13180 fini_reloc_cookie_for_section (&cookie, sec);
13181 }
13182 }
13183
13184 if (ret && eh_frame && elf_fde_list (sec))
13185 {
13186 struct elf_reloc_cookie cookie;
13187
13188 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13189 ret = FALSE;
13190 else
13191 {
13192 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13193 gc_mark_hook, &cookie))
13194 ret = FALSE;
13195 fini_reloc_cookie_for_section (&cookie, eh_frame);
13196 }
13197 }
13198
13199 eh_frame = elf_section_eh_frame_entry (sec);
13200 if (ret && eh_frame && !eh_frame->gc_mark)
13201 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13202 ret = FALSE;
13203
13204 return ret;
13205 }
13206
13207 /* Scan and mark sections in a special or debug section group. */
13208
13209 static void
13210 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13211 {
13212 /* Point to first section of section group. */
13213 asection *ssec;
13214 /* Used to iterate the section group. */
13215 asection *msec;
13216
13217 bfd_boolean is_special_grp = TRUE;
13218 bfd_boolean is_debug_grp = TRUE;
13219
13220 /* First scan to see if group contains any section other than debug
13221 and special section. */
13222 ssec = msec = elf_next_in_group (grp);
13223 do
13224 {
13225 if ((msec->flags & SEC_DEBUGGING) == 0)
13226 is_debug_grp = FALSE;
13227
13228 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13229 is_special_grp = FALSE;
13230
13231 msec = elf_next_in_group (msec);
13232 }
13233 while (msec != ssec);
13234
13235 /* If this is a pure debug section group or pure special section group,
13236 keep all sections in this group. */
13237 if (is_debug_grp || is_special_grp)
13238 {
13239 do
13240 {
13241 msec->gc_mark = 1;
13242 msec = elf_next_in_group (msec);
13243 }
13244 while (msec != ssec);
13245 }
13246 }
13247
13248 /* Keep debug and special sections. */
13249
13250 bfd_boolean
13251 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13252 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13253 {
13254 bfd *ibfd;
13255
13256 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13257 {
13258 asection *isec;
13259 bfd_boolean some_kept;
13260 bfd_boolean debug_frag_seen;
13261 bfd_boolean has_kept_debug_info;
13262
13263 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13264 continue;
13265 isec = ibfd->sections;
13266 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13267 continue;
13268
13269 /* Ensure all linker created sections are kept,
13270 see if any other section is already marked,
13271 and note if we have any fragmented debug sections. */
13272 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13273 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13274 {
13275 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13276 isec->gc_mark = 1;
13277 else if (isec->gc_mark
13278 && (isec->flags & SEC_ALLOC) != 0
13279 && elf_section_type (isec) != SHT_NOTE)
13280 some_kept = TRUE;
13281
13282 if (!debug_frag_seen
13283 && (isec->flags & SEC_DEBUGGING)
13284 && CONST_STRNEQ (isec->name, ".debug_line."))
13285 debug_frag_seen = TRUE;
13286 }
13287
13288 /* If no non-note alloc section in this file will be kept, then
13289 we can toss out the debug and special sections. */
13290 if (!some_kept)
13291 continue;
13292
13293 /* Keep debug and special sections like .comment when they are
13294 not part of a group. Also keep section groups that contain
13295 just debug sections or special sections. */
13296 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13297 {
13298 if ((isec->flags & SEC_GROUP) != 0)
13299 _bfd_elf_gc_mark_debug_special_section_group (isec);
13300 else if (((isec->flags & SEC_DEBUGGING) != 0
13301 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13302 && elf_next_in_group (isec) == NULL)
13303 isec->gc_mark = 1;
13304 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13305 has_kept_debug_info = TRUE;
13306 }
13307
13308 /* Look for CODE sections which are going to be discarded,
13309 and find and discard any fragmented debug sections which
13310 are associated with that code section. */
13311 if (debug_frag_seen)
13312 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13313 if ((isec->flags & SEC_CODE) != 0
13314 && isec->gc_mark == 0)
13315 {
13316 unsigned int ilen;
13317 asection *dsec;
13318
13319 ilen = strlen (isec->name);
13320
13321 /* Association is determined by the name of the debug
13322 section containing the name of the code section as
13323 a suffix. For example .debug_line.text.foo is a
13324 debug section associated with .text.foo. */
13325 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13326 {
13327 unsigned int dlen;
13328
13329 if (dsec->gc_mark == 0
13330 || (dsec->flags & SEC_DEBUGGING) == 0)
13331 continue;
13332
13333 dlen = strlen (dsec->name);
13334
13335 if (dlen > ilen
13336 && strncmp (dsec->name + (dlen - ilen),
13337 isec->name, ilen) == 0)
13338 dsec->gc_mark = 0;
13339 }
13340 }
13341
13342 /* Mark debug sections referenced by kept debug sections. */
13343 if (has_kept_debug_info)
13344 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13345 if (isec->gc_mark
13346 && (isec->flags & SEC_DEBUGGING) != 0)
13347 if (!_bfd_elf_gc_mark (info, isec,
13348 elf_gc_mark_debug_section))
13349 return FALSE;
13350 }
13351 return TRUE;
13352 }
13353
13354 static bfd_boolean
13355 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13356 {
13357 bfd *sub;
13358 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13359
13360 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13361 {
13362 asection *o;
13363
13364 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13365 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13366 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13367 continue;
13368 o = sub->sections;
13369 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13370 continue;
13371
13372 for (o = sub->sections; o != NULL; o = o->next)
13373 {
13374 /* When any section in a section group is kept, we keep all
13375 sections in the section group. If the first member of
13376 the section group is excluded, we will also exclude the
13377 group section. */
13378 if (o->flags & SEC_GROUP)
13379 {
13380 asection *first = elf_next_in_group (o);
13381 o->gc_mark = first->gc_mark;
13382 }
13383
13384 if (o->gc_mark)
13385 continue;
13386
13387 /* Skip sweeping sections already excluded. */
13388 if (o->flags & SEC_EXCLUDE)
13389 continue;
13390
13391 /* Since this is early in the link process, it is simple
13392 to remove a section from the output. */
13393 o->flags |= SEC_EXCLUDE;
13394
13395 if (info->print_gc_sections && o->size != 0)
13396 /* xgettext:c-format */
13397 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13398 o, sub);
13399 }
13400 }
13401
13402 return TRUE;
13403 }
13404
13405 /* Propagate collected vtable information. This is called through
13406 elf_link_hash_traverse. */
13407
13408 static bfd_boolean
13409 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13410 {
13411 /* Those that are not vtables. */
13412 if (h->start_stop
13413 || h->u2.vtable == NULL
13414 || h->u2.vtable->parent == NULL)
13415 return TRUE;
13416
13417 /* Those vtables that do not have parents, we cannot merge. */
13418 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13419 return TRUE;
13420
13421 /* If we've already been done, exit. */
13422 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13423 return TRUE;
13424
13425 /* Make sure the parent's table is up to date. */
13426 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13427
13428 if (h->u2.vtable->used == NULL)
13429 {
13430 /* None of this table's entries were referenced. Re-use the
13431 parent's table. */
13432 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13433 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13434 }
13435 else
13436 {
13437 size_t n;
13438 bfd_boolean *cu, *pu;
13439
13440 /* Or the parent's entries into ours. */
13441 cu = h->u2.vtable->used;
13442 cu[-1] = TRUE;
13443 pu = h->u2.vtable->parent->u2.vtable->used;
13444 if (pu != NULL)
13445 {
13446 const struct elf_backend_data *bed;
13447 unsigned int log_file_align;
13448
13449 bed = get_elf_backend_data (h->root.u.def.section->owner);
13450 log_file_align = bed->s->log_file_align;
13451 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13452 while (n--)
13453 {
13454 if (*pu)
13455 *cu = TRUE;
13456 pu++;
13457 cu++;
13458 }
13459 }
13460 }
13461
13462 return TRUE;
13463 }
13464
13465 static bfd_boolean
13466 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13467 {
13468 asection *sec;
13469 bfd_vma hstart, hend;
13470 Elf_Internal_Rela *relstart, *relend, *rel;
13471 const struct elf_backend_data *bed;
13472 unsigned int log_file_align;
13473
13474 /* Take care of both those symbols that do not describe vtables as
13475 well as those that are not loaded. */
13476 if (h->start_stop
13477 || h->u2.vtable == NULL
13478 || h->u2.vtable->parent == NULL)
13479 return TRUE;
13480
13481 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13482 || h->root.type == bfd_link_hash_defweak);
13483
13484 sec = h->root.u.def.section;
13485 hstart = h->root.u.def.value;
13486 hend = hstart + h->size;
13487
13488 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13489 if (!relstart)
13490 return *(bfd_boolean *) okp = FALSE;
13491 bed = get_elf_backend_data (sec->owner);
13492 log_file_align = bed->s->log_file_align;
13493
13494 relend = relstart + sec->reloc_count;
13495
13496 for (rel = relstart; rel < relend; ++rel)
13497 if (rel->r_offset >= hstart && rel->r_offset < hend)
13498 {
13499 /* If the entry is in use, do nothing. */
13500 if (h->u2.vtable->used
13501 && (rel->r_offset - hstart) < h->u2.vtable->size)
13502 {
13503 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13504 if (h->u2.vtable->used[entry])
13505 continue;
13506 }
13507 /* Otherwise, kill it. */
13508 rel->r_offset = rel->r_info = rel->r_addend = 0;
13509 }
13510
13511 return TRUE;
13512 }
13513
13514 /* Mark sections containing dynamically referenced symbols. When
13515 building shared libraries, we must assume that any visible symbol is
13516 referenced. */
13517
13518 bfd_boolean
13519 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13520 {
13521 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13522 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13523
13524 if ((h->root.type == bfd_link_hash_defined
13525 || h->root.type == bfd_link_hash_defweak)
13526 && ((h->ref_dynamic && !h->forced_local)
13527 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13528 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13529 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13530 && (!bfd_link_executable (info)
13531 || info->gc_keep_exported
13532 || info->export_dynamic
13533 || (h->dynamic
13534 && d != NULL
13535 && (*d->match) (&d->head, NULL, h->root.root.string)))
13536 && (h->versioned >= versioned
13537 || !bfd_hide_sym_by_version (info->version_info,
13538 h->root.root.string)))))
13539 h->root.u.def.section->flags |= SEC_KEEP;
13540
13541 return TRUE;
13542 }
13543
13544 /* Keep all sections containing symbols undefined on the command-line,
13545 and the section containing the entry symbol. */
13546
13547 void
13548 _bfd_elf_gc_keep (struct bfd_link_info *info)
13549 {
13550 struct bfd_sym_chain *sym;
13551
13552 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13553 {
13554 struct elf_link_hash_entry *h;
13555
13556 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13557 FALSE, FALSE, FALSE);
13558
13559 if (h != NULL
13560 && (h->root.type == bfd_link_hash_defined
13561 || h->root.type == bfd_link_hash_defweak)
13562 && !bfd_is_abs_section (h->root.u.def.section)
13563 && !bfd_is_und_section (h->root.u.def.section))
13564 h->root.u.def.section->flags |= SEC_KEEP;
13565 }
13566 }
13567
13568 bfd_boolean
13569 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13570 struct bfd_link_info *info)
13571 {
13572 bfd *ibfd = info->input_bfds;
13573
13574 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13575 {
13576 asection *sec;
13577 struct elf_reloc_cookie cookie;
13578
13579 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13580 continue;
13581 sec = ibfd->sections;
13582 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13583 continue;
13584
13585 if (!init_reloc_cookie (&cookie, info, ibfd))
13586 return FALSE;
13587
13588 for (sec = ibfd->sections; sec; sec = sec->next)
13589 {
13590 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13591 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13592 {
13593 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13594 fini_reloc_cookie_rels (&cookie, sec);
13595 }
13596 }
13597 }
13598 return TRUE;
13599 }
13600
13601 /* Do mark and sweep of unused sections. */
13602
13603 bfd_boolean
13604 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13605 {
13606 bfd_boolean ok = TRUE;
13607 bfd *sub;
13608 elf_gc_mark_hook_fn gc_mark_hook;
13609 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13610 struct elf_link_hash_table *htab;
13611
13612 if (!bed->can_gc_sections
13613 || !is_elf_hash_table (info->hash))
13614 {
13615 _bfd_error_handler(_("warning: gc-sections option ignored"));
13616 return TRUE;
13617 }
13618
13619 bed->gc_keep (info);
13620 htab = elf_hash_table (info);
13621
13622 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13623 at the .eh_frame section if we can mark the FDEs individually. */
13624 for (sub = info->input_bfds;
13625 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13626 sub = sub->link.next)
13627 {
13628 asection *sec;
13629 struct elf_reloc_cookie cookie;
13630
13631 sec = sub->sections;
13632 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13633 continue;
13634 sec = bfd_get_section_by_name (sub, ".eh_frame");
13635 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13636 {
13637 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13638 if (elf_section_data (sec)->sec_info
13639 && (sec->flags & SEC_LINKER_CREATED) == 0)
13640 elf_eh_frame_section (sub) = sec;
13641 fini_reloc_cookie_for_section (&cookie, sec);
13642 sec = bfd_get_next_section_by_name (NULL, sec);
13643 }
13644 }
13645
13646 /* Apply transitive closure to the vtable entry usage info. */
13647 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13648 if (!ok)
13649 return FALSE;
13650
13651 /* Kill the vtable relocations that were not used. */
13652 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13653 if (!ok)
13654 return FALSE;
13655
13656 /* Mark dynamically referenced symbols. */
13657 if (htab->dynamic_sections_created || info->gc_keep_exported)
13658 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13659
13660 /* Grovel through relocs to find out who stays ... */
13661 gc_mark_hook = bed->gc_mark_hook;
13662 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13663 {
13664 asection *o;
13665
13666 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13667 || elf_object_id (sub) != elf_hash_table_id (htab)
13668 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13669 continue;
13670
13671 o = sub->sections;
13672 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13673 continue;
13674
13675 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13676 Also treat note sections as a root, if the section is not part
13677 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13678 well as FINI_ARRAY sections for ld -r. */
13679 for (o = sub->sections; o != NULL; o = o->next)
13680 if (!o->gc_mark
13681 && (o->flags & SEC_EXCLUDE) == 0
13682 && ((o->flags & SEC_KEEP) != 0
13683 || (bfd_link_relocatable (info)
13684 && ((elf_section_data (o)->this_hdr.sh_type
13685 == SHT_PREINIT_ARRAY)
13686 || (elf_section_data (o)->this_hdr.sh_type
13687 == SHT_INIT_ARRAY)
13688 || (elf_section_data (o)->this_hdr.sh_type
13689 == SHT_FINI_ARRAY)))
13690 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13691 && elf_next_in_group (o) == NULL )))
13692 {
13693 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13694 return FALSE;
13695 }
13696 }
13697
13698 /* Allow the backend to mark additional target specific sections. */
13699 bed->gc_mark_extra_sections (info, gc_mark_hook);
13700
13701 /* ... and mark SEC_EXCLUDE for those that go. */
13702 return elf_gc_sweep (abfd, info);
13703 }
13704 \f
13705 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13706
13707 bfd_boolean
13708 bfd_elf_gc_record_vtinherit (bfd *abfd,
13709 asection *sec,
13710 struct elf_link_hash_entry *h,
13711 bfd_vma offset)
13712 {
13713 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13714 struct elf_link_hash_entry **search, *child;
13715 size_t extsymcount;
13716 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13717
13718 /* The sh_info field of the symtab header tells us where the
13719 external symbols start. We don't care about the local symbols at
13720 this point. */
13721 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13722 if (!elf_bad_symtab (abfd))
13723 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13724
13725 sym_hashes = elf_sym_hashes (abfd);
13726 sym_hashes_end = sym_hashes + extsymcount;
13727
13728 /* Hunt down the child symbol, which is in this section at the same
13729 offset as the relocation. */
13730 for (search = sym_hashes; search != sym_hashes_end; ++search)
13731 {
13732 if ((child = *search) != NULL
13733 && (child->root.type == bfd_link_hash_defined
13734 || child->root.type == bfd_link_hash_defweak)
13735 && child->root.u.def.section == sec
13736 && child->root.u.def.value == offset)
13737 goto win;
13738 }
13739
13740 /* xgettext:c-format */
13741 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13742 abfd, sec, (uint64_t) offset);
13743 bfd_set_error (bfd_error_invalid_operation);
13744 return FALSE;
13745
13746 win:
13747 if (!child->u2.vtable)
13748 {
13749 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13750 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13751 if (!child->u2.vtable)
13752 return FALSE;
13753 }
13754 if (!h)
13755 {
13756 /* This *should* only be the absolute section. It could potentially
13757 be that someone has defined a non-global vtable though, which
13758 would be bad. It isn't worth paging in the local symbols to be
13759 sure though; that case should simply be handled by the assembler. */
13760
13761 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13762 }
13763 else
13764 child->u2.vtable->parent = h;
13765
13766 return TRUE;
13767 }
13768
13769 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13770
13771 bfd_boolean
13772 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13773 struct elf_link_hash_entry *h,
13774 bfd_vma addend)
13775 {
13776 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13777 unsigned int log_file_align = bed->s->log_file_align;
13778
13779 if (!h)
13780 {
13781 /* xgettext:c-format */
13782 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13783 abfd, sec);
13784 bfd_set_error (bfd_error_bad_value);
13785 return FALSE;
13786 }
13787
13788 if (!h->u2.vtable)
13789 {
13790 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13791 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13792 if (!h->u2.vtable)
13793 return FALSE;
13794 }
13795
13796 if (addend >= h->u2.vtable->size)
13797 {
13798 size_t size, bytes, file_align;
13799 bfd_boolean *ptr = h->u2.vtable->used;
13800
13801 /* While the symbol is undefined, we have to be prepared to handle
13802 a zero size. */
13803 file_align = 1 << log_file_align;
13804 if (h->root.type == bfd_link_hash_undefined)
13805 size = addend + file_align;
13806 else
13807 {
13808 size = h->size;
13809 if (addend >= size)
13810 {
13811 /* Oops! We've got a reference past the defined end of
13812 the table. This is probably a bug -- shall we warn? */
13813 size = addend + file_align;
13814 }
13815 }
13816 size = (size + file_align - 1) & -file_align;
13817
13818 /* Allocate one extra entry for use as a "done" flag for the
13819 consolidation pass. */
13820 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13821
13822 if (ptr)
13823 {
13824 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13825
13826 if (ptr != NULL)
13827 {
13828 size_t oldbytes;
13829
13830 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13831 * sizeof (bfd_boolean));
13832 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13833 }
13834 }
13835 else
13836 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13837
13838 if (ptr == NULL)
13839 return FALSE;
13840
13841 /* And arrange for that done flag to be at index -1. */
13842 h->u2.vtable->used = ptr + 1;
13843 h->u2.vtable->size = size;
13844 }
13845
13846 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13847
13848 return TRUE;
13849 }
13850
13851 /* Map an ELF section header flag to its corresponding string. */
13852 typedef struct
13853 {
13854 char *flag_name;
13855 flagword flag_value;
13856 } elf_flags_to_name_table;
13857
13858 static elf_flags_to_name_table elf_flags_to_names [] =
13859 {
13860 { "SHF_WRITE", SHF_WRITE },
13861 { "SHF_ALLOC", SHF_ALLOC },
13862 { "SHF_EXECINSTR", SHF_EXECINSTR },
13863 { "SHF_MERGE", SHF_MERGE },
13864 { "SHF_STRINGS", SHF_STRINGS },
13865 { "SHF_INFO_LINK", SHF_INFO_LINK},
13866 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13867 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13868 { "SHF_GROUP", SHF_GROUP },
13869 { "SHF_TLS", SHF_TLS },
13870 { "SHF_MASKOS", SHF_MASKOS },
13871 { "SHF_EXCLUDE", SHF_EXCLUDE },
13872 };
13873
13874 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13875 bfd_boolean
13876 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13877 struct flag_info *flaginfo,
13878 asection *section)
13879 {
13880 const bfd_vma sh_flags = elf_section_flags (section);
13881
13882 if (!flaginfo->flags_initialized)
13883 {
13884 bfd *obfd = info->output_bfd;
13885 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13886 struct flag_info_list *tf = flaginfo->flag_list;
13887 int with_hex = 0;
13888 int without_hex = 0;
13889
13890 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13891 {
13892 unsigned i;
13893 flagword (*lookup) (char *);
13894
13895 lookup = bed->elf_backend_lookup_section_flags_hook;
13896 if (lookup != NULL)
13897 {
13898 flagword hexval = (*lookup) ((char *) tf->name);
13899
13900 if (hexval != 0)
13901 {
13902 if (tf->with == with_flags)
13903 with_hex |= hexval;
13904 else if (tf->with == without_flags)
13905 without_hex |= hexval;
13906 tf->valid = TRUE;
13907 continue;
13908 }
13909 }
13910 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13911 {
13912 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13913 {
13914 if (tf->with == with_flags)
13915 with_hex |= elf_flags_to_names[i].flag_value;
13916 else if (tf->with == without_flags)
13917 without_hex |= elf_flags_to_names[i].flag_value;
13918 tf->valid = TRUE;
13919 break;
13920 }
13921 }
13922 if (!tf->valid)
13923 {
13924 info->callbacks->einfo
13925 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13926 return FALSE;
13927 }
13928 }
13929 flaginfo->flags_initialized = TRUE;
13930 flaginfo->only_with_flags |= with_hex;
13931 flaginfo->not_with_flags |= without_hex;
13932 }
13933
13934 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13935 return FALSE;
13936
13937 if ((flaginfo->not_with_flags & sh_flags) != 0)
13938 return FALSE;
13939
13940 return TRUE;
13941 }
13942
13943 struct alloc_got_off_arg {
13944 bfd_vma gotoff;
13945 struct bfd_link_info *info;
13946 };
13947
13948 /* We need a special top-level link routine to convert got reference counts
13949 to real got offsets. */
13950
13951 static bfd_boolean
13952 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13953 {
13954 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13955 bfd *obfd = gofarg->info->output_bfd;
13956 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13957
13958 if (h->got.refcount > 0)
13959 {
13960 h->got.offset = gofarg->gotoff;
13961 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13962 }
13963 else
13964 h->got.offset = (bfd_vma) -1;
13965
13966 return TRUE;
13967 }
13968
13969 /* And an accompanying bit to work out final got entry offsets once
13970 we're done. Should be called from final_link. */
13971
13972 bfd_boolean
13973 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13974 struct bfd_link_info *info)
13975 {
13976 bfd *i;
13977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13978 bfd_vma gotoff;
13979 struct alloc_got_off_arg gofarg;
13980
13981 BFD_ASSERT (abfd == info->output_bfd);
13982
13983 if (! is_elf_hash_table (info->hash))
13984 return FALSE;
13985
13986 /* The GOT offset is relative to the .got section, but the GOT header is
13987 put into the .got.plt section, if the backend uses it. */
13988 if (bed->want_got_plt)
13989 gotoff = 0;
13990 else
13991 gotoff = bed->got_header_size;
13992
13993 /* Do the local .got entries first. */
13994 for (i = info->input_bfds; i; i = i->link.next)
13995 {
13996 bfd_signed_vma *local_got;
13997 size_t j, locsymcount;
13998 Elf_Internal_Shdr *symtab_hdr;
13999
14000 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14001 continue;
14002
14003 local_got = elf_local_got_refcounts (i);
14004 if (!local_got)
14005 continue;
14006
14007 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14008 if (elf_bad_symtab (i))
14009 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14010 else
14011 locsymcount = symtab_hdr->sh_info;
14012
14013 for (j = 0; j < locsymcount; ++j)
14014 {
14015 if (local_got[j] > 0)
14016 {
14017 local_got[j] = gotoff;
14018 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14019 }
14020 else
14021 local_got[j] = (bfd_vma) -1;
14022 }
14023 }
14024
14025 /* Then the global .got entries. .plt refcounts are handled by
14026 adjust_dynamic_symbol */
14027 gofarg.gotoff = gotoff;
14028 gofarg.info = info;
14029 elf_link_hash_traverse (elf_hash_table (info),
14030 elf_gc_allocate_got_offsets,
14031 &gofarg);
14032 return TRUE;
14033 }
14034
14035 /* Many folk need no more in the way of final link than this, once
14036 got entry reference counting is enabled. */
14037
14038 bfd_boolean
14039 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14040 {
14041 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14042 return FALSE;
14043
14044 /* Invoke the regular ELF backend linker to do all the work. */
14045 return bfd_elf_final_link (abfd, info);
14046 }
14047
14048 bfd_boolean
14049 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14050 {
14051 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14052
14053 if (rcookie->bad_symtab)
14054 rcookie->rel = rcookie->rels;
14055
14056 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14057 {
14058 unsigned long r_symndx;
14059
14060 if (! rcookie->bad_symtab)
14061 if (rcookie->rel->r_offset > offset)
14062 return FALSE;
14063 if (rcookie->rel->r_offset != offset)
14064 continue;
14065
14066 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14067 if (r_symndx == STN_UNDEF)
14068 return TRUE;
14069
14070 if (r_symndx >= rcookie->locsymcount
14071 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14072 {
14073 struct elf_link_hash_entry *h;
14074
14075 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14076
14077 while (h->root.type == bfd_link_hash_indirect
14078 || h->root.type == bfd_link_hash_warning)
14079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14080
14081 if ((h->root.type == bfd_link_hash_defined
14082 || h->root.type == bfd_link_hash_defweak)
14083 && (h->root.u.def.section->owner != rcookie->abfd
14084 || h->root.u.def.section->kept_section != NULL
14085 || discarded_section (h->root.u.def.section)))
14086 return TRUE;
14087 }
14088 else
14089 {
14090 /* It's not a relocation against a global symbol,
14091 but it could be a relocation against a local
14092 symbol for a discarded section. */
14093 asection *isec;
14094 Elf_Internal_Sym *isym;
14095
14096 /* Need to: get the symbol; get the section. */
14097 isym = &rcookie->locsyms[r_symndx];
14098 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14099 if (isec != NULL
14100 && (isec->kept_section != NULL
14101 || discarded_section (isec)))
14102 return TRUE;
14103 }
14104 return FALSE;
14105 }
14106 return FALSE;
14107 }
14108
14109 /* Discard unneeded references to discarded sections.
14110 Returns -1 on error, 1 if any section's size was changed, 0 if
14111 nothing changed. This function assumes that the relocations are in
14112 sorted order, which is true for all known assemblers. */
14113
14114 int
14115 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14116 {
14117 struct elf_reloc_cookie cookie;
14118 asection *o;
14119 bfd *abfd;
14120 int changed = 0;
14121
14122 if (info->traditional_format
14123 || !is_elf_hash_table (info->hash))
14124 return 0;
14125
14126 o = bfd_get_section_by_name (output_bfd, ".stab");
14127 if (o != NULL)
14128 {
14129 asection *i;
14130
14131 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14132 {
14133 if (i->size == 0
14134 || i->reloc_count == 0
14135 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14136 continue;
14137
14138 abfd = i->owner;
14139 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14140 continue;
14141
14142 if (!init_reloc_cookie_for_section (&cookie, info, i))
14143 return -1;
14144
14145 if (_bfd_discard_section_stabs (abfd, i,
14146 elf_section_data (i)->sec_info,
14147 bfd_elf_reloc_symbol_deleted_p,
14148 &cookie))
14149 changed = 1;
14150
14151 fini_reloc_cookie_for_section (&cookie, i);
14152 }
14153 }
14154
14155 o = NULL;
14156 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14157 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14158 if (o != NULL)
14159 {
14160 asection *i;
14161 int eh_changed = 0;
14162 unsigned int eh_alignment;
14163
14164 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14165 {
14166 if (i->size == 0)
14167 continue;
14168
14169 abfd = i->owner;
14170 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14171 continue;
14172
14173 if (!init_reloc_cookie_for_section (&cookie, info, i))
14174 return -1;
14175
14176 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14177 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14178 bfd_elf_reloc_symbol_deleted_p,
14179 &cookie))
14180 {
14181 eh_changed = 1;
14182 if (i->size != i->rawsize)
14183 changed = 1;
14184 }
14185
14186 fini_reloc_cookie_for_section (&cookie, i);
14187 }
14188
14189 eh_alignment = 1 << o->alignment_power;
14190 /* Skip over zero terminator, and prevent empty sections from
14191 adding alignment padding at the end. */
14192 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14193 if (i->size == 0)
14194 i->flags |= SEC_EXCLUDE;
14195 else if (i->size > 4)
14196 break;
14197 /* The last non-empty eh_frame section doesn't need padding. */
14198 if (i != NULL)
14199 i = i->map_tail.s;
14200 /* Any prior sections must pad the last FDE out to the output
14201 section alignment. Otherwise we might have zero padding
14202 between sections, which would be seen as a terminator. */
14203 for (; i != NULL; i = i->map_tail.s)
14204 if (i->size == 4)
14205 /* All but the last zero terminator should have been removed. */
14206 BFD_FAIL ();
14207 else
14208 {
14209 bfd_size_type size
14210 = (i->size + eh_alignment - 1) & -eh_alignment;
14211 if (i->size != size)
14212 {
14213 i->size = size;
14214 changed = 1;
14215 eh_changed = 1;
14216 }
14217 }
14218 if (eh_changed)
14219 elf_link_hash_traverse (elf_hash_table (info),
14220 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14221 }
14222
14223 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14224 {
14225 const struct elf_backend_data *bed;
14226 asection *s;
14227
14228 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14229 continue;
14230 s = abfd->sections;
14231 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14232 continue;
14233
14234 bed = get_elf_backend_data (abfd);
14235
14236 if (bed->elf_backend_discard_info != NULL)
14237 {
14238 if (!init_reloc_cookie (&cookie, info, abfd))
14239 return -1;
14240
14241 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14242 changed = 1;
14243
14244 fini_reloc_cookie (&cookie, abfd);
14245 }
14246 }
14247
14248 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14249 _bfd_elf_end_eh_frame_parsing (info);
14250
14251 if (info->eh_frame_hdr_type
14252 && !bfd_link_relocatable (info)
14253 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14254 changed = 1;
14255
14256 return changed;
14257 }
14258
14259 bfd_boolean
14260 _bfd_elf_section_already_linked (bfd *abfd,
14261 asection *sec,
14262 struct bfd_link_info *info)
14263 {
14264 flagword flags;
14265 const char *name, *key;
14266 struct bfd_section_already_linked *l;
14267 struct bfd_section_already_linked_hash_entry *already_linked_list;
14268
14269 if (sec->output_section == bfd_abs_section_ptr)
14270 return FALSE;
14271
14272 flags = sec->flags;
14273
14274 /* Return if it isn't a linkonce section. A comdat group section
14275 also has SEC_LINK_ONCE set. */
14276 if ((flags & SEC_LINK_ONCE) == 0)
14277 return FALSE;
14278
14279 /* Don't put group member sections on our list of already linked
14280 sections. They are handled as a group via their group section. */
14281 if (elf_sec_group (sec) != NULL)
14282 return FALSE;
14283
14284 /* For a SHT_GROUP section, use the group signature as the key. */
14285 name = sec->name;
14286 if ((flags & SEC_GROUP) != 0
14287 && elf_next_in_group (sec) != NULL
14288 && elf_group_name (elf_next_in_group (sec)) != NULL)
14289 key = elf_group_name (elf_next_in_group (sec));
14290 else
14291 {
14292 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14293 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14294 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14295 key++;
14296 else
14297 /* Must be a user linkonce section that doesn't follow gcc's
14298 naming convention. In this case we won't be matching
14299 single member groups. */
14300 key = name;
14301 }
14302
14303 already_linked_list = bfd_section_already_linked_table_lookup (key);
14304
14305 for (l = already_linked_list->entry; l != NULL; l = l->next)
14306 {
14307 /* We may have 2 different types of sections on the list: group
14308 sections with a signature of <key> (<key> is some string),
14309 and linkonce sections named .gnu.linkonce.<type>.<key>.
14310 Match like sections. LTO plugin sections are an exception.
14311 They are always named .gnu.linkonce.t.<key> and match either
14312 type of section. */
14313 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14314 && ((flags & SEC_GROUP) != 0
14315 || strcmp (name, l->sec->name) == 0))
14316 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14317 {
14318 /* The section has already been linked. See if we should
14319 issue a warning. */
14320 if (!_bfd_handle_already_linked (sec, l, info))
14321 return FALSE;
14322
14323 if (flags & SEC_GROUP)
14324 {
14325 asection *first = elf_next_in_group (sec);
14326 asection *s = first;
14327
14328 while (s != NULL)
14329 {
14330 s->output_section = bfd_abs_section_ptr;
14331 /* Record which group discards it. */
14332 s->kept_section = l->sec;
14333 s = elf_next_in_group (s);
14334 /* These lists are circular. */
14335 if (s == first)
14336 break;
14337 }
14338 }
14339
14340 return TRUE;
14341 }
14342 }
14343
14344 /* A single member comdat group section may be discarded by a
14345 linkonce section and vice versa. */
14346 if ((flags & SEC_GROUP) != 0)
14347 {
14348 asection *first = elf_next_in_group (sec);
14349
14350 if (first != NULL && elf_next_in_group (first) == first)
14351 /* Check this single member group against linkonce sections. */
14352 for (l = already_linked_list->entry; l != NULL; l = l->next)
14353 if ((l->sec->flags & SEC_GROUP) == 0
14354 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14355 {
14356 first->output_section = bfd_abs_section_ptr;
14357 first->kept_section = l->sec;
14358 sec->output_section = bfd_abs_section_ptr;
14359 break;
14360 }
14361 }
14362 else
14363 /* Check this linkonce section against single member groups. */
14364 for (l = already_linked_list->entry; l != NULL; l = l->next)
14365 if (l->sec->flags & SEC_GROUP)
14366 {
14367 asection *first = elf_next_in_group (l->sec);
14368
14369 if (first != NULL
14370 && elf_next_in_group (first) == first
14371 && bfd_elf_match_symbols_in_sections (first, sec, info))
14372 {
14373 sec->output_section = bfd_abs_section_ptr;
14374 sec->kept_section = first;
14375 break;
14376 }
14377 }
14378
14379 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14380 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14381 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14382 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14383 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14384 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14385 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14386 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14387 The reverse order cannot happen as there is never a bfd with only the
14388 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14389 matter as here were are looking only for cross-bfd sections. */
14390
14391 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14392 for (l = already_linked_list->entry; l != NULL; l = l->next)
14393 if ((l->sec->flags & SEC_GROUP) == 0
14394 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14395 {
14396 if (abfd != l->sec->owner)
14397 sec->output_section = bfd_abs_section_ptr;
14398 break;
14399 }
14400
14401 /* This is the first section with this name. Record it. */
14402 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14403 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14404 return sec->output_section == bfd_abs_section_ptr;
14405 }
14406
14407 bfd_boolean
14408 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14409 {
14410 return sym->st_shndx == SHN_COMMON;
14411 }
14412
14413 unsigned int
14414 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14415 {
14416 return SHN_COMMON;
14417 }
14418
14419 asection *
14420 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14421 {
14422 return bfd_com_section_ptr;
14423 }
14424
14425 bfd_vma
14426 _bfd_elf_default_got_elt_size (bfd *abfd,
14427 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14428 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14429 bfd *ibfd ATTRIBUTE_UNUSED,
14430 unsigned long symndx ATTRIBUTE_UNUSED)
14431 {
14432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14433 return bed->s->arch_size / 8;
14434 }
14435
14436 /* Routines to support the creation of dynamic relocs. */
14437
14438 /* Returns the name of the dynamic reloc section associated with SEC. */
14439
14440 static const char *
14441 get_dynamic_reloc_section_name (bfd * abfd,
14442 asection * sec,
14443 bfd_boolean is_rela)
14444 {
14445 char *name;
14446 const char *old_name = bfd_get_section_name (NULL, sec);
14447 const char *prefix = is_rela ? ".rela" : ".rel";
14448
14449 if (old_name == NULL)
14450 return NULL;
14451
14452 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14453 sprintf (name, "%s%s", prefix, old_name);
14454
14455 return name;
14456 }
14457
14458 /* Returns the dynamic reloc section associated with SEC.
14459 If necessary compute the name of the dynamic reloc section based
14460 on SEC's name (looked up in ABFD's string table) and the setting
14461 of IS_RELA. */
14462
14463 asection *
14464 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14465 asection * sec,
14466 bfd_boolean is_rela)
14467 {
14468 asection * reloc_sec = elf_section_data (sec)->sreloc;
14469
14470 if (reloc_sec == NULL)
14471 {
14472 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14473
14474 if (name != NULL)
14475 {
14476 reloc_sec = bfd_get_linker_section (abfd, name);
14477
14478 if (reloc_sec != NULL)
14479 elf_section_data (sec)->sreloc = reloc_sec;
14480 }
14481 }
14482
14483 return reloc_sec;
14484 }
14485
14486 /* Returns the dynamic reloc section associated with SEC. If the
14487 section does not exist it is created and attached to the DYNOBJ
14488 bfd and stored in the SRELOC field of SEC's elf_section_data
14489 structure.
14490
14491 ALIGNMENT is the alignment for the newly created section and
14492 IS_RELA defines whether the name should be .rela.<SEC's name>
14493 or .rel.<SEC's name>. The section name is looked up in the
14494 string table associated with ABFD. */
14495
14496 asection *
14497 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14498 bfd *dynobj,
14499 unsigned int alignment,
14500 bfd *abfd,
14501 bfd_boolean is_rela)
14502 {
14503 asection * reloc_sec = elf_section_data (sec)->sreloc;
14504
14505 if (reloc_sec == NULL)
14506 {
14507 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14508
14509 if (name == NULL)
14510 return NULL;
14511
14512 reloc_sec = bfd_get_linker_section (dynobj, name);
14513
14514 if (reloc_sec == NULL)
14515 {
14516 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14517 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14518 if ((sec->flags & SEC_ALLOC) != 0)
14519 flags |= SEC_ALLOC | SEC_LOAD;
14520
14521 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14522 if (reloc_sec != NULL)
14523 {
14524 /* _bfd_elf_get_sec_type_attr chooses a section type by
14525 name. Override as it may be wrong, eg. for a user
14526 section named "auto" we'll get ".relauto" which is
14527 seen to be a .rela section. */
14528 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14529 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14530 reloc_sec = NULL;
14531 }
14532 }
14533
14534 elf_section_data (sec)->sreloc = reloc_sec;
14535 }
14536
14537 return reloc_sec;
14538 }
14539
14540 /* Copy the ELF symbol type and other attributes for a linker script
14541 assignment from HSRC to HDEST. Generally this should be treated as
14542 if we found a strong non-dynamic definition for HDEST (except that
14543 ld ignores multiple definition errors). */
14544 void
14545 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14546 struct bfd_link_hash_entry *hdest,
14547 struct bfd_link_hash_entry *hsrc)
14548 {
14549 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14550 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14551 Elf_Internal_Sym isym;
14552
14553 ehdest->type = ehsrc->type;
14554 ehdest->target_internal = ehsrc->target_internal;
14555
14556 isym.st_other = ehsrc->other;
14557 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14558 }
14559
14560 /* Append a RELA relocation REL to section S in BFD. */
14561
14562 void
14563 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14564 {
14565 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14566 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14567 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14568 bed->s->swap_reloca_out (abfd, rel, loc);
14569 }
14570
14571 /* Append a REL relocation REL to section S in BFD. */
14572
14573 void
14574 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14575 {
14576 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14577 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14578 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14579 bed->s->swap_reloc_out (abfd, rel, loc);
14580 }
14581
14582 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14583
14584 struct bfd_link_hash_entry *
14585 bfd_elf_define_start_stop (struct bfd_link_info *info,
14586 const char *symbol, asection *sec)
14587 {
14588 struct elf_link_hash_entry *h;
14589
14590 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14591 FALSE, FALSE, TRUE);
14592 if (h != NULL
14593 && (h->root.type == bfd_link_hash_undefined
14594 || h->root.type == bfd_link_hash_undefweak
14595 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14596 {
14597 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14598 h->root.type = bfd_link_hash_defined;
14599 h->root.u.def.section = sec;
14600 h->root.u.def.value = 0;
14601 h->def_regular = 1;
14602 h->def_dynamic = 0;
14603 h->start_stop = 1;
14604 h->u2.start_stop_section = sec;
14605 if (symbol[0] == '.')
14606 {
14607 /* .startof. and .sizeof. symbols are local. */
14608 const struct elf_backend_data *bed;
14609 bed = get_elf_backend_data (info->output_bfd);
14610 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14611 }
14612 else
14613 {
14614 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14615 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14616 if (was_dynamic)
14617 bfd_elf_link_record_dynamic_symbol (info, h);
14618 }
14619 return &h->root;
14620 }
14621 return NULL;
14622 }
This page took 0.38059 seconds and 4 git commands to generate.