Extend pdp11-aout symbol table format and code for .stab symbols.
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 return FALSE;
181 htab->sgotplt = s;
182 }
183
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
196 if (h == NULL)
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202 \f
203 /* Create a strtab to hold the dynamic symbol names. */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 asection *s;
220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
221 if ((ibfd->flags
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242 }
243
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
250
251 bfd_boolean
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 {
254 flagword flags;
255 asection *s;
256 const struct elf_backend_data *bed;
257 struct elf_link_hash_entry *h;
258
259 if (! is_elf_hash_table (info->hash))
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
267
268 abfd = elf_hash_table (info)->dynobj;
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info) && !info->nointerp)
276 {
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
279 if (s == NULL)
280 return FALSE;
281 }
282
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || !bfd_set_section_alignment (s, 1))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
301 return FALSE;
302
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
305 if (s == NULL
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
307 return FALSE;
308 elf_hash_table (info)->dynsym = s;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
312 if (s == NULL)
313 return FALSE;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
316 if (s == NULL
317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
329 return FALSE;
330
331 if (info->emit_hash)
332 {
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
335 if (s == NULL
336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
342 {
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
345 if (s == NULL
346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367 }
368
369 /* Create dynamic sections when linking against a dynamic object. */
370
371 bfd_boolean
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
373 {
374 flagword flags, pltflags;
375 struct elf_link_hash_entry *h;
376 asection *s;
377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378 struct elf_link_hash_table *htab = elf_hash_table (info);
379
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags = bed->dynamic_sec_flags;
383
384 pltflags = flags;
385 if (bed->plt_not_loaded)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
396 if (s == NULL
397 || !bfd_set_section_alignment (s, bed->plt_alignment))
398 return FALSE;
399 htab->splt = s;
400
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
411
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
416 if (s == NULL
417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
418 return FALSE;
419 htab->srelplt = s;
420
421 if (! _bfd_elf_create_got_section (abfd, info))
422 return FALSE;
423
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 SEC_ALLOC | SEC_LINKER_CREATED);
434 if (s == NULL)
435 return FALSE;
436 htab->sdynbss = s;
437
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 flags);
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
461 if (bfd_link_executable (info))
462 {
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return FALSE;
470 htab->srelbss = s;
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
483 }
484 }
485
486 return TRUE;
487 }
488 \f
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
497 bfd_boolean
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
500 {
501 if (h->dynindx == -1)
502 {
503 struct elf_strtab_hash *dynstr;
504 char *p;
505 const char *name;
506 size_t indx;
507
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
513 {
514 case STV_INTERNAL:
515 case STV_HIDDEN:
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
518 {
519 h->forced_local = 1;
520 if (!elf_hash_table (info)->is_relocatable_executable)
521 return TRUE;
522 }
523
524 default:
525 break;
526 }
527
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
530
531 dynstr = elf_hash_table (info)->dynstr;
532 if (dynstr == NULL)
533 {
534 /* Create a strtab to hold the dynamic symbol names. */
535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
536 if (dynstr == NULL)
537 return FALSE;
538 }
539
540 /* We don't put any version information in the dynamic string
541 table. */
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
544 if (p != NULL)
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
550 *p = 0;
551
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553
554 if (p != NULL)
555 *p = ELF_VER_CHR;
556
557 if (indx == (size_t) -1)
558 return FALSE;
559 h->dynstr_index = indx;
560 }
561
562 return TRUE;
563 }
564 \f
565 /* Mark a symbol dynamic. */
566
567 static void
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
571 {
572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
573
574 /* It may be called more than once on the same H. */
575 if(h->dynamic || bfd_link_relocatable (info))
576 return;
577
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
580 || h->type == STT_COMMON
581 || (sym != NULL
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
584 || (d != NULL
585 && h->non_elf
586 && (*d->match) (&d->head, NULL, h->root.root.string)))
587 {
588 h->dynamic = 1;
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 non-IR reference. */
591 h->root.non_ir_ref_dynamic = 1;
592 }
593 }
594
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598 bfd_boolean
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
601 const char *name,
602 bfd_boolean provide,
603 bfd_boolean hidden)
604 {
605 struct elf_link_hash_entry *h, *hv;
606 struct elf_link_hash_table *htab;
607 const struct elf_backend_data *bed;
608
609 if (!is_elf_hash_table (info->hash))
610 return TRUE;
611
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h == NULL)
615 return provide;
616
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
641 switch (h->root.type)
642 {
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h->root.type = bfd_link_hash_new;
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
655 break;
656 case bfd_link_hash_new:
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
673 default:
674 BFD_FAIL ();
675 return FALSE;
676 }
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
683 && h->def_dynamic
684 && !h->def_regular)
685 h->root.type = bfd_link_hash_undefined;
686
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
690 more. */
691 if (h->def_dynamic && !h->def_regular)
692 h->verinfo.verdef = NULL;
693
694 /* Make sure this symbol is not garbage collected. */
695 h->mark = 1;
696
697 h->def_regular = 1;
698
699 if (hidden)
700 {
701 bed = get_elf_backend_data (output_bfd);
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705 }
706
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 and executables. */
709 if (!bfd_link_relocatable (info)
710 && h->dynindx != -1
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713 h->forced_local = 1;
714
715 if ((h->def_dynamic
716 || h->ref_dynamic
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
719 && !h->forced_local
720 && h->dynindx == -1)
721 {
722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 return FALSE;
724
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
728 if (h->is_weakalias)
729 {
730 struct elf_link_hash_entry *def = weakdef (h);
731
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
734 return FALSE;
735 }
736 }
737
738 return TRUE;
739 }
740
741 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
744
745 int
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 bfd *input_bfd,
748 long input_indx)
749 {
750 size_t amt;
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
754 size_t dynstr_index;
755 char *name;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
758
759 if (! is_elf_hash_table (info->hash))
760 return 0;
761
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 return 1;
766
767 amt = sizeof (*entry);
768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
769 if (entry == NULL)
770 return 0;
771
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
774 1, input_indx, &entry->isym, esym, &eshndx))
775 {
776 bfd_release (input_bfd, entry);
777 return 0;
778 }
779
780 if (entry->isym.st_shndx != SHN_UNDEF
781 && entry->isym.st_shndx < SHN_LORESERVE)
782 {
783 asection *s;
784
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
787 {
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
791 return 2;
792 }
793 }
794
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
798
799 dynstr = elf_hash_table (info)->dynstr;
800 if (dynstr == NULL)
801 {
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 if (dynstr == NULL)
805 return 0;
806 }
807
808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
809 if (dynstr_index == (size_t) -1)
810 return 0;
811 entry->isym.st_name = dynstr_index;
812
813 eht = elf_hash_table (info);
814
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
819 eht->dynsymcount++;
820
821 /* Whatever binding the symbol had before, it's now local. */
822 entry->isym.st_info
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824
825 /* The dynindx will be set at the end of size_dynamic_sections. */
826
827 return 1;
828 }
829
830 /* Return the dynindex of a local dynamic symbol. */
831
832 long
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 bfd *input_bfd,
835 long input_indx)
836 {
837 struct elf_link_local_dynamic_entry *e;
838
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 return e->dynindx;
842 return -1;
843 }
844
845 /* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
848
849 static bfd_boolean
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
852 {
853 size_t *count = (size_t *) data;
854
855 if (h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862 }
863
864
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
867
868 static bfd_boolean
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 void *data)
871 {
872 size_t *count = (size_t *) data;
873
874 if (!h->forced_local)
875 return TRUE;
876
877 if (h->dynindx != -1)
878 h->dynindx = ++(*count);
879
880 return TRUE;
881 }
882
883 /* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
885 bfd_boolean
886 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
888 asection *p)
889 {
890 struct elf_link_hash_table *htab;
891 asection *ip;
892
893 switch (elf_section_data (p)->this_hdr.sh_type)
894 {
895 case SHT_PROGBITS:
896 case SHT_NOBITS:
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
899 case SHT_NULL:
900 htab = elf_hash_table (info);
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
903
904 return (htab->dynobj != NULL
905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
906 && ip->output_section == p);
907
908 /* There shouldn't be section relative relocations
909 against any other section. */
910 default:
911 return TRUE;
912 }
913 }
914
915 bfd_boolean
916 _bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
920 {
921 return TRUE;
922 }
923
924 /* Assign dynsym indices. In a shared library we generate a section
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
932
933 static unsigned long
934 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
937 {
938 unsigned long dynsymcount = 0;
939 bfd_boolean do_sec = section_sym_count != NULL;
940
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
943 {
944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
945 asection *p;
946 for (p = output_bfd->sections; p ; p = p->next)
947 if ((p->flags & SEC_EXCLUDE) == 0
948 && (p->flags & SEC_ALLOC) != 0
949 && elf_hash_table (info)->dynamic_relocs
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
957 elf_section_data (p)->dynindx = 0;
958 }
959 if (do_sec)
960 *section_sym_count = dynsymcount;
961
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
983
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
986 }
987
988 /* Merge st_other field. */
989
990 static void
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
994 {
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
1003 if (!dynamic)
1004 {
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1007
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1012 }
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1017 }
1018
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1031
1032 static bfd_boolean
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1048 {
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1058 char *new_version;
1059 bfd_boolean default_sym = *matched;
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
1075
1076 bed = get_elf_backend_data (abfd);
1077
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1080 {
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
1098 }
1099 else
1100 new_version = NULL;
1101
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
1226 {
1227 h->non_elf = 0;
1228 return TRUE;
1229 }
1230
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1241 return TRUE;
1242
1243 olddyn = FALSE;
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1247 {
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1251 }
1252
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1267
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1271
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1287 {
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
1325 }
1326
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
1342 ntbfd = abfd;
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h->root.root.string, tbfd, ntbfd);
1371 else if (tdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1377 else
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
1396 h->ref_dynamic = 1;
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1409 && h->def_dynamic)
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1425
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1428 {
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
1434 }
1435 else
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
1443 h = hi;
1444 }
1445 else
1446 h = hi;
1447 }
1448
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1457 {
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1468 {
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
1474 }
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
1483
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1501 newweak = FALSE;
1502 if (olddef && newdyn)
1503 oldweak = FALSE;
1504
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1508
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1512
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1525
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
1551 && !newweak
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
1555 && !newfunc)
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
1563 && h->def_dynamic
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
1567 && !oldfunc)
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
1580
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1614
1615 *size_change_ok = TRUE;
1616 }
1617
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1631
1632 if (newdyn
1633 && newdef
1634 && (olddef
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
1641
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1644
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1650
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1670 }
1671
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1674 {
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
1697
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1707
1708 flip = NULL;
1709 if (!newdyn
1710 && (newdef
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1713 && olddyn
1714 && olddef
1715 && h->def_dynamic)
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
1743
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1768
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1805 if (h->def_dynamic)
1806 {
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
1809 }
1810 }
1811
1812 return TRUE;
1813 }
1814
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1819
1820 static bfd_boolean
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
1829 bfd_boolean *dynsym)
1830 {
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1841 char *p;
1842 size_t len, shortlen;
1843 asection *tmp_sec;
1844 bfd_boolean matched;
1845
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
1879
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1897 matched = TRUE;
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
1907 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
1932 if (! override)
1933 {
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1936 {
1937 bh = &hi->root;
1938 if (bh->type == bfd_link_hash_defined
1939 && bh->u.def.section->owner != NULL
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 {
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1944 it. */
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1947 }
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982 if (h->def_dynamic)
1983 {
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
1988 {
1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2013
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
2026 if (! bfd_link_executable (info)
2027 || hi->def_dynamic
2028 || hi->ref_dynamic)
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
2033 if (hi->ref_regular)
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
2042 nondefault:
2043 len = strlen (name);
2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 &hi, poldbfd, NULL, NULL, &skip, &override,
2056 &type_change_ok, &size_change_ok, &matched))
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
2069 _bfd_error_handler
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2072 abfd, shortname);
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
2099 if (! bfd_link_executable (info)
2100 || hi->ref_dynamic)
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
2105 if (hi->ref_regular)
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113 }
2114 \f
2115 /* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2117
2118 static bfd_boolean
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2120 {
2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2122
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2125 return TRUE;
2126
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2129 return TRUE;
2130
2131 if (h->dynindx == -1
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
2135 {
2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2137 {
2138 eif->failed = TRUE;
2139 return FALSE;
2140 }
2141 }
2142
2143 return TRUE;
2144 }
2145 \f
2146 /* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2150
2151 static bfd_boolean
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 void *data)
2154 {
2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
2158 size_t amt;
2159
2160 /* We only care about symbols defined in shared objects with version
2161 information. */
2162 if (!h->def_dynamic
2163 || h->def_regular
2164 || h->dynindx == -1
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2168 return TRUE;
2169
2170 /* See if we already know about this version. */
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 t != NULL;
2173 t = t->vn_nextref)
2174 {
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 continue;
2177
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 return TRUE;
2181
2182 break;
2183 }
2184
2185 /* This is a new version. Add it to tree we are building. */
2186
2187 if (t == NULL)
2188 {
2189 amt = sizeof *t;
2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2191 if (t == NULL)
2192 {
2193 rinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
2200 }
2201
2202 amt = sizeof *a;
2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2204 if (a == NULL)
2205 {
2206 rinfo->failed = TRUE;
2207 return FALSE;
2208 }
2209
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2213 fixed. */
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2218
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220 ++rinfo->vers;
2221
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223
2224 t->vn_auxptr = a;
2225
2226 return TRUE;
2227 }
2228
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2231
2232 static bfd_boolean
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2237 bfd_boolean *hide)
2238 {
2239 struct bfd_elf_version_tree *t;
2240
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2243 {
2244 if (strcmp (t->name, version_p) == 0)
2245 {
2246 size_t len;
2247 char *alc;
2248 struct bfd_elf_version_expr *d;
2249
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2252 if (alc == NULL)
2253 return FALSE;
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2258
2259 h->verinfo.vertree = t;
2260 t->used = TRUE;
2261 d = NULL;
2262
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2265
2266 /* See if there is anything to force this symbol to
2267 local scope. */
2268 if (d == NULL && t->locals.list != NULL)
2269 {
2270 d = (*t->match) (&t->locals, NULL, alc);
2271 if (d != NULL
2272 && h->dynindx != -1
2273 && ! info->export_dynamic)
2274 *hide = TRUE;
2275 }
2276
2277 free (alc);
2278 break;
2279 }
2280 }
2281
2282 *t_p = t;
2283
2284 return TRUE;
2285 }
2286
2287 /* Return TRUE if the symbol H is hidden by version script. */
2288
2289 bfd_boolean
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2292 {
2293 const char *p;
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2297
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300 return TRUE;
2301
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2304 {
2305 struct bfd_elf_version_tree *t;
2306
2307 ++p;
2308 if (*p == ELF_VER_CHR)
2309 ++p;
2310
2311 if (*p != '\0'
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 && hide)
2314 {
2315 if (hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 return TRUE;
2318 }
2319 }
2320
2321 /* If we don't have a version for this symbol, see if we can find
2322 something. */
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324 {
2325 h->verinfo.vertree
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2329 {
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 return TRUE;
2332 }
2333 }
2334
2335 return FALSE;
2336 }
2337
2338 /* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2342
2343 static bfd_boolean
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2345 {
2346 struct elf_info_failed *sinfo;
2347 struct bfd_link_info *info;
2348 const struct elf_backend_data *bed;
2349 struct elf_info_failed eif;
2350 char *p;
2351 bfd_boolean hide;
2352
2353 sinfo = (struct elf_info_failed *) data;
2354 info = sinfo->info;
2355
2356 /* Fix the symbol flags. */
2357 eif.failed = FALSE;
2358 eif.info = info;
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360 {
2361 if (eif.failed)
2362 sinfo->failed = TRUE;
2363 return FALSE;
2364 }
2365
2366 bed = get_elf_backend_data (info->output_bfd);
2367
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
2370 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2371 {
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 return TRUE;
2378 }
2379
2380 hide = FALSE;
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2383 {
2384 struct bfd_elf_version_tree *t;
2385
2386 ++p;
2387 if (*p == ELF_VER_CHR)
2388 ++p;
2389
2390 /* If there is no version string, we can just return out. */
2391 if (*p == '\0')
2392 return TRUE;
2393
2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2395 {
2396 sinfo->failed = TRUE;
2397 return FALSE;
2398 }
2399
2400 if (hide)
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
2405 if (t == NULL && bfd_link_executable (info))
2406 {
2407 struct bfd_elf_version_tree **pp;
2408 int version_index;
2409
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2413 return TRUE;
2414
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 sizeof *t);
2417 if (t == NULL)
2418 {
2419 sinfo->failed = TRUE;
2420 return FALSE;
2421 }
2422
2423 t->name = p;
2424 t->name_indx = (unsigned int) -1;
2425 t->used = TRUE;
2426
2427 version_index = 1;
2428 /* Don't count anonymous version tag. */
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
2431 version_index = 0;
2432 for (pp = &sinfo->info->version_info;
2433 *pp != NULL;
2434 pp = &(*pp)->next)
2435 ++version_index;
2436 t->vernum = version_index;
2437
2438 *pp = t;
2439
2440 h->verinfo.vertree = t;
2441 }
2442 else if (t == NULL)
2443 {
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
2446 _bfd_error_handler
2447 /* xgettext:c-format */
2448 (_("%pB: version node not found for symbol %s"),
2449 info->output_bfd, h->root.root.string);
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2452 return FALSE;
2453 }
2454 }
2455
2456 /* If we don't have a version for this symbol, see if we can find
2457 something. */
2458 if (!hide
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
2461 {
2462 h->verinfo.vertree
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2467 }
2468
2469 return TRUE;
2470 }
2471 \f
2472 /* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2478
2479 Returns FALSE if something goes wrong. */
2480
2481 static bfd_boolean
2482 elf_link_read_relocs_from_section (bfd *abfd,
2483 asection *sec,
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
2487 {
2488 const struct elf_backend_data *bed;
2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
2493 Elf_Internal_Shdr *symtab_hdr;
2494 size_t nsyms;
2495
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498 return FALSE;
2499
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502 return FALSE;
2503
2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2506
2507 bed = get_elf_backend_data (abfd);
2508
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2514 else
2515 {
2516 bfd_set_error (bfd_error_wrong_format);
2517 return FALSE;
2518 }
2519
2520 erela = (const bfd_byte *) external_relocs;
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2524 irela = internal_relocs;
2525 while (erela <= erelaend)
2526 {
2527 bfd_vma r_symndx;
2528
2529 (*swap_in) (abfd, erela, irela);
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2532 r_symndx >>= 24;
2533 if (nsyms > 0)
2534 {
2535 if ((size_t) r_symndx >= nsyms)
2536 {
2537 _bfd_error_handler
2538 /* xgettext:c-format */
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 }
2547 else if (r_symndx != STN_UNDEF)
2548 {
2549 _bfd_error_handler
2550 /* xgettext:c-format */
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
2553 " when the object file has no symbol table"),
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
2556 bfd_set_error (bfd_error_bad_value);
2557 return FALSE;
2558 }
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2561 }
2562
2563 return TRUE;
2564 }
2565
2566 /* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
2574 RELA_HDR relocations. */
2575
2576 Elf_Internal_Rela *
2577 _bfd_elf_link_read_relocs (bfd *abfd,
2578 asection *o,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
2582 {
2583 void *alloc1 = NULL;
2584 Elf_Internal_Rela *alloc2 = NULL;
2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
2588
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
2591
2592 if (o->reloc_count == 0)
2593 return NULL;
2594
2595 if (internal_relocs == NULL)
2596 {
2597 bfd_size_type size;
2598
2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2600 if (keep_memory)
2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2602 else
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2604 if (internal_relocs == NULL)
2605 goto error_return;
2606 }
2607
2608 if (external_relocs == NULL)
2609 {
2610 bfd_size_type size = 0;
2611
2612 if (esdo->rel.hdr)
2613 size += esdo->rel.hdr->sh_size;
2614 if (esdo->rela.hdr)
2615 size += esdo->rela.hdr->sh_size;
2616
2617 alloc1 = bfd_malloc (size);
2618 if (alloc1 == NULL)
2619 goto error_return;
2620 external_relocs = alloc1;
2621 }
2622
2623 internal_rela_relocs = internal_relocs;
2624 if (esdo->rel.hdr)
2625 {
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 external_relocs,
2628 internal_relocs))
2629 goto error_return;
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2634 }
2635
2636 if (esdo->rela.hdr
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 external_relocs,
2639 internal_rela_relocs)))
2640 goto error_return;
2641
2642 /* Cache the results for next time, if we can. */
2643 if (keep_memory)
2644 esdo->relocs = internal_relocs;
2645
2646 free (alloc1);
2647
2648 /* Don't free alloc2, since if it was allocated we are passing it
2649 back (under the name of internal_relocs). */
2650
2651 return internal_relocs;
2652
2653 error_return:
2654 free (alloc1);
2655 if (alloc2 != NULL)
2656 {
2657 if (keep_memory)
2658 bfd_release (abfd, alloc2);
2659 else
2660 free (alloc2);
2661 }
2662 return NULL;
2663 }
2664
2665 /* Compute the size of, and allocate space for, REL_HDR which is the
2666 section header for a section containing relocations for O. */
2667
2668 static bfd_boolean
2669 _bfd_elf_link_size_reloc_section (bfd *abfd,
2670 struct bfd_elf_section_reloc_data *reldata)
2671 {
2672 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2673
2674 /* That allows us to calculate the size of the section. */
2675 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2676
2677 /* The contents field must last into write_object_contents, so we
2678 allocate it with bfd_alloc rather than malloc. Also since we
2679 cannot be sure that the contents will actually be filled in,
2680 we zero the allocated space. */
2681 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2682 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2683 return FALSE;
2684
2685 if (reldata->hashes == NULL && reldata->count)
2686 {
2687 struct elf_link_hash_entry **p;
2688
2689 p = ((struct elf_link_hash_entry **)
2690 bfd_zmalloc (reldata->count * sizeof (*p)));
2691 if (p == NULL)
2692 return FALSE;
2693
2694 reldata->hashes = p;
2695 }
2696
2697 return TRUE;
2698 }
2699
2700 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2701 originated from the section given by INPUT_REL_HDR) to the
2702 OUTPUT_BFD. */
2703
2704 bfd_boolean
2705 _bfd_elf_link_output_relocs (bfd *output_bfd,
2706 asection *input_section,
2707 Elf_Internal_Shdr *input_rel_hdr,
2708 Elf_Internal_Rela *internal_relocs,
2709 struct elf_link_hash_entry **rel_hash
2710 ATTRIBUTE_UNUSED)
2711 {
2712 Elf_Internal_Rela *irela;
2713 Elf_Internal_Rela *irelaend;
2714 bfd_byte *erel;
2715 struct bfd_elf_section_reloc_data *output_reldata;
2716 asection *output_section;
2717 const struct elf_backend_data *bed;
2718 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2719 struct bfd_elf_section_data *esdo;
2720
2721 output_section = input_section->output_section;
2722
2723 bed = get_elf_backend_data (output_bfd);
2724 esdo = elf_section_data (output_section);
2725 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2726 {
2727 output_reldata = &esdo->rel;
2728 swap_out = bed->s->swap_reloc_out;
2729 }
2730 else if (esdo->rela.hdr
2731 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2732 {
2733 output_reldata = &esdo->rela;
2734 swap_out = bed->s->swap_reloca_out;
2735 }
2736 else
2737 {
2738 _bfd_error_handler
2739 /* xgettext:c-format */
2740 (_("%pB: relocation size mismatch in %pB section %pA"),
2741 output_bfd, input_section->owner, input_section);
2742 bfd_set_error (bfd_error_wrong_format);
2743 return FALSE;
2744 }
2745
2746 erel = output_reldata->hdr->contents;
2747 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2748 irela = internal_relocs;
2749 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2750 * bed->s->int_rels_per_ext_rel);
2751 while (irela < irelaend)
2752 {
2753 (*swap_out) (output_bfd, irela, erel);
2754 irela += bed->s->int_rels_per_ext_rel;
2755 erel += input_rel_hdr->sh_entsize;
2756 }
2757
2758 /* Bump the counter, so that we know where to add the next set of
2759 relocations. */
2760 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2761
2762 return TRUE;
2763 }
2764 \f
2765 /* Make weak undefined symbols in PIE dynamic. */
2766
2767 bfd_boolean
2768 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2769 struct elf_link_hash_entry *h)
2770 {
2771 if (bfd_link_pie (info)
2772 && h->dynindx == -1
2773 && h->root.type == bfd_link_hash_undefweak)
2774 return bfd_elf_link_record_dynamic_symbol (info, h);
2775
2776 return TRUE;
2777 }
2778
2779 /* Fix up the flags for a symbol. This handles various cases which
2780 can only be fixed after all the input files are seen. This is
2781 currently called by both adjust_dynamic_symbol and
2782 assign_sym_version, which is unnecessary but perhaps more robust in
2783 the face of future changes. */
2784
2785 static bfd_boolean
2786 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2787 struct elf_info_failed *eif)
2788 {
2789 const struct elf_backend_data *bed;
2790
2791 /* If this symbol was mentioned in a non-ELF file, try to set
2792 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2793 permit a non-ELF file to correctly refer to a symbol defined in
2794 an ELF dynamic object. */
2795 if (h->non_elf)
2796 {
2797 while (h->root.type == bfd_link_hash_indirect)
2798 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2799
2800 if (h->root.type != bfd_link_hash_defined
2801 && h->root.type != bfd_link_hash_defweak)
2802 {
2803 h->ref_regular = 1;
2804 h->ref_regular_nonweak = 1;
2805 }
2806 else
2807 {
2808 if (h->root.u.def.section->owner != NULL
2809 && (bfd_get_flavour (h->root.u.def.section->owner)
2810 == bfd_target_elf_flavour))
2811 {
2812 h->ref_regular = 1;
2813 h->ref_regular_nonweak = 1;
2814 }
2815 else
2816 h->def_regular = 1;
2817 }
2818
2819 if (h->dynindx == -1
2820 && (h->def_dynamic
2821 || h->ref_dynamic))
2822 {
2823 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2824 {
2825 eif->failed = TRUE;
2826 return FALSE;
2827 }
2828 }
2829 }
2830 else
2831 {
2832 /* Unfortunately, NON_ELF is only correct if the symbol
2833 was first seen in a non-ELF file. Fortunately, if the symbol
2834 was first seen in an ELF file, we're probably OK unless the
2835 symbol was defined in a non-ELF file. Catch that case here.
2836 FIXME: We're still in trouble if the symbol was first seen in
2837 a dynamic object, and then later in a non-ELF regular object. */
2838 if ((h->root.type == bfd_link_hash_defined
2839 || h->root.type == bfd_link_hash_defweak)
2840 && !h->def_regular
2841 && (h->root.u.def.section->owner != NULL
2842 ? (bfd_get_flavour (h->root.u.def.section->owner)
2843 != bfd_target_elf_flavour)
2844 : (bfd_is_abs_section (h->root.u.def.section)
2845 && !h->def_dynamic)))
2846 h->def_regular = 1;
2847 }
2848
2849 /* Backend specific symbol fixup. */
2850 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2851 if (bed->elf_backend_fixup_symbol
2852 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2853 return FALSE;
2854
2855 /* If this is a final link, and the symbol was defined as a common
2856 symbol in a regular object file, and there was no definition in
2857 any dynamic object, then the linker will have allocated space for
2858 the symbol in a common section but the DEF_REGULAR
2859 flag will not have been set. */
2860 if (h->root.type == bfd_link_hash_defined
2861 && !h->def_regular
2862 && h->ref_regular
2863 && !h->def_dynamic
2864 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2865 h->def_regular = 1;
2866
2867 /* Symbols defined in discarded sections shouldn't be dynamic. */
2868 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870
2871 /* If a weak undefined symbol has non-default visibility, we also
2872 hide it from the dynamic linker. */
2873 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2874 && h->root.type == bfd_link_hash_undefweak)
2875 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2876
2877 /* A hidden versioned symbol in executable should be forced local if
2878 it is is locally defined, not referenced by shared library and not
2879 exported. */
2880 else if (bfd_link_executable (eif->info)
2881 && h->versioned == versioned_hidden
2882 && !eif->info->export_dynamic
2883 && !h->dynamic
2884 && !h->ref_dynamic
2885 && h->def_regular)
2886 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2887
2888 /* If -Bsymbolic was used (which means to bind references to global
2889 symbols to the definition within the shared object), and this
2890 symbol was defined in a regular object, then it actually doesn't
2891 need a PLT entry. Likewise, if the symbol has non-default
2892 visibility. If the symbol has hidden or internal visibility, we
2893 will force it local. */
2894 else if (h->needs_plt
2895 && bfd_link_pic (eif->info)
2896 && is_elf_hash_table (eif->info->hash)
2897 && (SYMBOLIC_BIND (eif->info, h)
2898 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2899 && h->def_regular)
2900 {
2901 bfd_boolean force_local;
2902
2903 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2904 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2905 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2906 }
2907
2908 /* If this is a weak defined symbol in a dynamic object, and we know
2909 the real definition in the dynamic object, copy interesting flags
2910 over to the real definition. */
2911 if (h->is_weakalias)
2912 {
2913 struct elf_link_hash_entry *def = weakdef (h);
2914
2915 /* If the real definition is defined by a regular object file,
2916 don't do anything special. See the longer description in
2917 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2918 bfd_link_hash_defined as it was when put on the alias list
2919 then it must have originally been a versioned symbol (for
2920 which a non-versioned indirect symbol is created) and later
2921 a definition for the non-versioned symbol is found. In that
2922 case the indirection is flipped with the versioned symbol
2923 becoming an indirect pointing at the non-versioned symbol.
2924 Thus, not an alias any more. */
2925 if (def->def_regular
2926 || def->root.type != bfd_link_hash_defined)
2927 {
2928 h = def;
2929 while ((h = h->u.alias) != def)
2930 h->is_weakalias = 0;
2931 }
2932 else
2933 {
2934 while (h->root.type == bfd_link_hash_indirect)
2935 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2936 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2937 || h->root.type == bfd_link_hash_defweak);
2938 BFD_ASSERT (def->def_dynamic);
2939 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2940 }
2941 }
2942
2943 return TRUE;
2944 }
2945
2946 /* Make the backend pick a good value for a dynamic symbol. This is
2947 called via elf_link_hash_traverse, and also calls itself
2948 recursively. */
2949
2950 static bfd_boolean
2951 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2952 {
2953 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2954 struct elf_link_hash_table *htab;
2955 const struct elf_backend_data *bed;
2956
2957 if (! is_elf_hash_table (eif->info->hash))
2958 return FALSE;
2959
2960 /* Ignore indirect symbols. These are added by the versioning code. */
2961 if (h->root.type == bfd_link_hash_indirect)
2962 return TRUE;
2963
2964 /* Fix the symbol flags. */
2965 if (! _bfd_elf_fix_symbol_flags (h, eif))
2966 return FALSE;
2967
2968 htab = elf_hash_table (eif->info);
2969 bed = get_elf_backend_data (htab->dynobj);
2970
2971 if (h->root.type == bfd_link_hash_undefweak)
2972 {
2973 if (eif->info->dynamic_undefined_weak == 0)
2974 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2975 else if (eif->info->dynamic_undefined_weak > 0
2976 && h->ref_regular
2977 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2978 && !bfd_hide_sym_by_version (eif->info->version_info,
2979 h->root.root.string))
2980 {
2981 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2982 {
2983 eif->failed = TRUE;
2984 return FALSE;
2985 }
2986 }
2987 }
2988
2989 /* If this symbol does not require a PLT entry, and it is not
2990 defined by a dynamic object, or is not referenced by a regular
2991 object, ignore it. We do have to handle a weak defined symbol,
2992 even if no regular object refers to it, if we decided to add it
2993 to the dynamic symbol table. FIXME: Do we normally need to worry
2994 about symbols which are defined by one dynamic object and
2995 referenced by another one? */
2996 if (!h->needs_plt
2997 && h->type != STT_GNU_IFUNC
2998 && (h->def_regular
2999 || !h->def_dynamic
3000 || (!h->ref_regular
3001 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3002 {
3003 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3004 return TRUE;
3005 }
3006
3007 /* If we've already adjusted this symbol, don't do it again. This
3008 can happen via a recursive call. */
3009 if (h->dynamic_adjusted)
3010 return TRUE;
3011
3012 /* Don't look at this symbol again. Note that we must set this
3013 after checking the above conditions, because we may look at a
3014 symbol once, decide not to do anything, and then get called
3015 recursively later after REF_REGULAR is set below. */
3016 h->dynamic_adjusted = 1;
3017
3018 /* If this is a weak definition, and we know a real definition, and
3019 the real symbol is not itself defined by a regular object file,
3020 then get a good value for the real definition. We handle the
3021 real symbol first, for the convenience of the backend routine.
3022
3023 Note that there is a confusing case here. If the real definition
3024 is defined by a regular object file, we don't get the real symbol
3025 from the dynamic object, but we do get the weak symbol. If the
3026 processor backend uses a COPY reloc, then if some routine in the
3027 dynamic object changes the real symbol, we will not see that
3028 change in the corresponding weak symbol. This is the way other
3029 ELF linkers work as well, and seems to be a result of the shared
3030 library model.
3031
3032 I will clarify this issue. Most SVR4 shared libraries define the
3033 variable _timezone and define timezone as a weak synonym. The
3034 tzset call changes _timezone. If you write
3035 extern int timezone;
3036 int _timezone = 5;
3037 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3038 you might expect that, since timezone is a synonym for _timezone,
3039 the same number will print both times. However, if the processor
3040 backend uses a COPY reloc, then actually timezone will be copied
3041 into your process image, and, since you define _timezone
3042 yourself, _timezone will not. Thus timezone and _timezone will
3043 wind up at different memory locations. The tzset call will set
3044 _timezone, leaving timezone unchanged. */
3045
3046 if (h->is_weakalias)
3047 {
3048 struct elf_link_hash_entry *def = weakdef (h);
3049
3050 /* If we get to this point, there is an implicit reference to
3051 the alias by a regular object file via the weak symbol H. */
3052 def->ref_regular = 1;
3053
3054 /* Ensure that the backend adjust_dynamic_symbol function sees
3055 the strong alias before H by recursively calling ourselves. */
3056 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3057 return FALSE;
3058 }
3059
3060 /* If a symbol has no type and no size and does not require a PLT
3061 entry, then we are probably about to do the wrong thing here: we
3062 are probably going to create a COPY reloc for an empty object.
3063 This case can arise when a shared object is built with assembly
3064 code, and the assembly code fails to set the symbol type. */
3065 if (h->size == 0
3066 && h->type == STT_NOTYPE
3067 && !h->needs_plt)
3068 _bfd_error_handler
3069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3070 h->root.root.string);
3071
3072 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3073 {
3074 eif->failed = TRUE;
3075 return FALSE;
3076 }
3077
3078 return TRUE;
3079 }
3080
3081 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3082 DYNBSS. */
3083
3084 bfd_boolean
3085 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3086 struct elf_link_hash_entry *h,
3087 asection *dynbss)
3088 {
3089 unsigned int power_of_two;
3090 bfd_vma mask;
3091 asection *sec = h->root.u.def.section;
3092
3093 /* The section alignment of the definition is the maximum alignment
3094 requirement of symbols defined in the section. Since we don't
3095 know the symbol alignment requirement, we start with the
3096 maximum alignment and check low bits of the symbol address
3097 for the minimum alignment. */
3098 power_of_two = bfd_section_alignment (sec);
3099 mask = ((bfd_vma) 1 << power_of_two) - 1;
3100 while ((h->root.u.def.value & mask) != 0)
3101 {
3102 mask >>= 1;
3103 --power_of_two;
3104 }
3105
3106 if (power_of_two > bfd_section_alignment (dynbss))
3107 {
3108 /* Adjust the section alignment if needed. */
3109 if (!bfd_set_section_alignment (dynbss, power_of_two))
3110 return FALSE;
3111 }
3112
3113 /* We make sure that the symbol will be aligned properly. */
3114 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3115
3116 /* Define the symbol as being at this point in DYNBSS. */
3117 h->root.u.def.section = dynbss;
3118 h->root.u.def.value = dynbss->size;
3119
3120 /* Increment the size of DYNBSS to make room for the symbol. */
3121 dynbss->size += h->size;
3122
3123 /* No error if extern_protected_data is true. */
3124 if (h->protected_def
3125 && (!info->extern_protected_data
3126 || (info->extern_protected_data < 0
3127 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3128 info->callbacks->einfo
3129 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3130 h->root.root.string);
3131
3132 return TRUE;
3133 }
3134
3135 /* Adjust all external symbols pointing into SEC_MERGE sections
3136 to reflect the object merging within the sections. */
3137
3138 static bfd_boolean
3139 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3140 {
3141 asection *sec;
3142
3143 if ((h->root.type == bfd_link_hash_defined
3144 || h->root.type == bfd_link_hash_defweak)
3145 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3146 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3147 {
3148 bfd *output_bfd = (bfd *) data;
3149
3150 h->root.u.def.value =
3151 _bfd_merged_section_offset (output_bfd,
3152 &h->root.u.def.section,
3153 elf_section_data (sec)->sec_info,
3154 h->root.u.def.value);
3155 }
3156
3157 return TRUE;
3158 }
3159
3160 /* Returns false if the symbol referred to by H should be considered
3161 to resolve local to the current module, and true if it should be
3162 considered to bind dynamically. */
3163
3164 bfd_boolean
3165 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3166 struct bfd_link_info *info,
3167 bfd_boolean not_local_protected)
3168 {
3169 bfd_boolean binding_stays_local_p;
3170 const struct elf_backend_data *bed;
3171 struct elf_link_hash_table *hash_table;
3172
3173 if (h == NULL)
3174 return FALSE;
3175
3176 while (h->root.type == bfd_link_hash_indirect
3177 || h->root.type == bfd_link_hash_warning)
3178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3179
3180 /* If it was forced local, then clearly it's not dynamic. */
3181 if (h->dynindx == -1)
3182 return FALSE;
3183 if (h->forced_local)
3184 return FALSE;
3185
3186 /* Identify the cases where name binding rules say that a
3187 visible symbol resolves locally. */
3188 binding_stays_local_p = (bfd_link_executable (info)
3189 || SYMBOLIC_BIND (info, h));
3190
3191 switch (ELF_ST_VISIBILITY (h->other))
3192 {
3193 case STV_INTERNAL:
3194 case STV_HIDDEN:
3195 return FALSE;
3196
3197 case STV_PROTECTED:
3198 hash_table = elf_hash_table (info);
3199 if (!is_elf_hash_table (hash_table))
3200 return FALSE;
3201
3202 bed = get_elf_backend_data (hash_table->dynobj);
3203
3204 /* Proper resolution for function pointer equality may require
3205 that these symbols perhaps be resolved dynamically, even though
3206 we should be resolving them to the current module. */
3207 if (!not_local_protected || !bed->is_function_type (h->type))
3208 binding_stays_local_p = TRUE;
3209 break;
3210
3211 default:
3212 break;
3213 }
3214
3215 /* If it isn't defined locally, then clearly it's dynamic. */
3216 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3217 return TRUE;
3218
3219 /* Otherwise, the symbol is dynamic if binding rules don't tell
3220 us that it remains local. */
3221 return !binding_stays_local_p;
3222 }
3223
3224 /* Return true if the symbol referred to by H should be considered
3225 to resolve local to the current module, and false otherwise. Differs
3226 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3227 undefined symbols. The two functions are virtually identical except
3228 for the place where dynindx == -1 is tested. If that test is true,
3229 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3230 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3231 defined symbols.
3232 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3233 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3234 treatment of undefined weak symbols. For those that do not make
3235 undefined weak symbols dynamic, both functions may return false. */
3236
3237 bfd_boolean
3238 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3239 struct bfd_link_info *info,
3240 bfd_boolean local_protected)
3241 {
3242 const struct elf_backend_data *bed;
3243 struct elf_link_hash_table *hash_table;
3244
3245 /* If it's a local sym, of course we resolve locally. */
3246 if (h == NULL)
3247 return TRUE;
3248
3249 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3250 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3251 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3252 return TRUE;
3253
3254 /* Forced local symbols resolve locally. */
3255 if (h->forced_local)
3256 return TRUE;
3257
3258 /* Common symbols that become definitions don't get the DEF_REGULAR
3259 flag set, so test it first, and don't bail out. */
3260 if (ELF_COMMON_DEF_P (h))
3261 /* Do nothing. */;
3262 /* If we don't have a definition in a regular file, then we can't
3263 resolve locally. The sym is either undefined or dynamic. */
3264 else if (!h->def_regular)
3265 return FALSE;
3266
3267 /* Non-dynamic symbols resolve locally. */
3268 if (h->dynindx == -1)
3269 return TRUE;
3270
3271 /* At this point, we know the symbol is defined and dynamic. In an
3272 executable it must resolve locally, likewise when building symbolic
3273 shared libraries. */
3274 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3275 return TRUE;
3276
3277 /* Now deal with defined dynamic symbols in shared libraries. Ones
3278 with default visibility might not resolve locally. */
3279 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3280 return FALSE;
3281
3282 hash_table = elf_hash_table (info);
3283 if (!is_elf_hash_table (hash_table))
3284 return TRUE;
3285
3286 bed = get_elf_backend_data (hash_table->dynobj);
3287
3288 /* If extern_protected_data is false, STV_PROTECTED non-function
3289 symbols are local. */
3290 if ((!info->extern_protected_data
3291 || (info->extern_protected_data < 0
3292 && !bed->extern_protected_data))
3293 && !bed->is_function_type (h->type))
3294 return TRUE;
3295
3296 /* Function pointer equality tests may require that STV_PROTECTED
3297 symbols be treated as dynamic symbols. If the address of a
3298 function not defined in an executable is set to that function's
3299 plt entry in the executable, then the address of the function in
3300 a shared library must also be the plt entry in the executable. */
3301 return local_protected;
3302 }
3303
3304 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3305 aligned. Returns the first TLS output section. */
3306
3307 struct bfd_section *
3308 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3309 {
3310 struct bfd_section *sec, *tls;
3311 unsigned int align = 0;
3312
3313 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3314 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3315 break;
3316 tls = sec;
3317
3318 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3319 if (sec->alignment_power > align)
3320 align = sec->alignment_power;
3321
3322 elf_hash_table (info)->tls_sec = tls;
3323
3324 /* Ensure the alignment of the first section (usually .tdata) is the largest
3325 alignment, so that the tls segment starts aligned. */
3326 if (tls != NULL)
3327 tls->alignment_power = align;
3328
3329 return tls;
3330 }
3331
3332 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3333 static bfd_boolean
3334 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3335 Elf_Internal_Sym *sym)
3336 {
3337 const struct elf_backend_data *bed;
3338
3339 /* Local symbols do not count, but target specific ones might. */
3340 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3341 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3342 return FALSE;
3343
3344 bed = get_elf_backend_data (abfd);
3345 /* Function symbols do not count. */
3346 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3347 return FALSE;
3348
3349 /* If the section is undefined, then so is the symbol. */
3350 if (sym->st_shndx == SHN_UNDEF)
3351 return FALSE;
3352
3353 /* If the symbol is defined in the common section, then
3354 it is a common definition and so does not count. */
3355 if (bed->common_definition (sym))
3356 return FALSE;
3357
3358 /* If the symbol is in a target specific section then we
3359 must rely upon the backend to tell us what it is. */
3360 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3361 /* FIXME - this function is not coded yet:
3362
3363 return _bfd_is_global_symbol_definition (abfd, sym);
3364
3365 Instead for now assume that the definition is not global,
3366 Even if this is wrong, at least the linker will behave
3367 in the same way that it used to do. */
3368 return FALSE;
3369
3370 return TRUE;
3371 }
3372
3373 /* Search the symbol table of the archive element of the archive ABFD
3374 whose archive map contains a mention of SYMDEF, and determine if
3375 the symbol is defined in this element. */
3376 static bfd_boolean
3377 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3378 {
3379 Elf_Internal_Shdr * hdr;
3380 size_t symcount;
3381 size_t extsymcount;
3382 size_t extsymoff;
3383 Elf_Internal_Sym *isymbuf;
3384 Elf_Internal_Sym *isym;
3385 Elf_Internal_Sym *isymend;
3386 bfd_boolean result;
3387
3388 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3389 if (abfd == NULL)
3390 return FALSE;
3391
3392 if (! bfd_check_format (abfd, bfd_object))
3393 return FALSE;
3394
3395 /* Select the appropriate symbol table. If we don't know if the
3396 object file is an IR object, give linker LTO plugin a chance to
3397 get the correct symbol table. */
3398 if (abfd->plugin_format == bfd_plugin_yes
3399 #if BFD_SUPPORTS_PLUGINS
3400 || (abfd->plugin_format == bfd_plugin_unknown
3401 && bfd_link_plugin_object_p (abfd))
3402 #endif
3403 )
3404 {
3405 /* Use the IR symbol table if the object has been claimed by
3406 plugin. */
3407 abfd = abfd->plugin_dummy_bfd;
3408 hdr = &elf_tdata (abfd)->symtab_hdr;
3409 }
3410 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3411 hdr = &elf_tdata (abfd)->symtab_hdr;
3412 else
3413 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3414
3415 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3416
3417 /* The sh_info field of the symtab header tells us where the
3418 external symbols start. We don't care about the local symbols. */
3419 if (elf_bad_symtab (abfd))
3420 {
3421 extsymcount = symcount;
3422 extsymoff = 0;
3423 }
3424 else
3425 {
3426 extsymcount = symcount - hdr->sh_info;
3427 extsymoff = hdr->sh_info;
3428 }
3429
3430 if (extsymcount == 0)
3431 return FALSE;
3432
3433 /* Read in the symbol table. */
3434 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3435 NULL, NULL, NULL);
3436 if (isymbuf == NULL)
3437 return FALSE;
3438
3439 /* Scan the symbol table looking for SYMDEF. */
3440 result = FALSE;
3441 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3442 {
3443 const char *name;
3444
3445 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3446 isym->st_name);
3447 if (name == NULL)
3448 break;
3449
3450 if (strcmp (name, symdef->name) == 0)
3451 {
3452 result = is_global_data_symbol_definition (abfd, isym);
3453 break;
3454 }
3455 }
3456
3457 free (isymbuf);
3458
3459 return result;
3460 }
3461 \f
3462 /* Add an entry to the .dynamic table. */
3463
3464 bfd_boolean
3465 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3466 bfd_vma tag,
3467 bfd_vma val)
3468 {
3469 struct elf_link_hash_table *hash_table;
3470 const struct elf_backend_data *bed;
3471 asection *s;
3472 bfd_size_type newsize;
3473 bfd_byte *newcontents;
3474 Elf_Internal_Dyn dyn;
3475
3476 hash_table = elf_hash_table (info);
3477 if (! is_elf_hash_table (hash_table))
3478 return FALSE;
3479
3480 if (tag == DT_RELA || tag == DT_REL)
3481 hash_table->dynamic_relocs = TRUE;
3482
3483 bed = get_elf_backend_data (hash_table->dynobj);
3484 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3485 BFD_ASSERT (s != NULL);
3486
3487 newsize = s->size + bed->s->sizeof_dyn;
3488 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3489 if (newcontents == NULL)
3490 return FALSE;
3491
3492 dyn.d_tag = tag;
3493 dyn.d_un.d_val = val;
3494 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3495
3496 s->size = newsize;
3497 s->contents = newcontents;
3498
3499 return TRUE;
3500 }
3501
3502 /* Strip zero-sized dynamic sections. */
3503
3504 bfd_boolean
3505 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3506 {
3507 struct elf_link_hash_table *hash_table;
3508 const struct elf_backend_data *bed;
3509 asection *s, *sdynamic, **pp;
3510 asection *rela_dyn, *rel_dyn;
3511 Elf_Internal_Dyn dyn;
3512 bfd_byte *extdyn, *next;
3513 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3514 bfd_boolean strip_zero_sized;
3515 bfd_boolean strip_zero_sized_plt;
3516
3517 if (bfd_link_relocatable (info))
3518 return TRUE;
3519
3520 hash_table = elf_hash_table (info);
3521 if (!is_elf_hash_table (hash_table))
3522 return FALSE;
3523
3524 if (!hash_table->dynobj)
3525 return TRUE;
3526
3527 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3528 if (!sdynamic)
3529 return TRUE;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 swap_dyn_in = bed->s->swap_dyn_in;
3533
3534 strip_zero_sized = FALSE;
3535 strip_zero_sized_plt = FALSE;
3536
3537 /* Strip zero-sized dynamic sections. */
3538 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3539 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3540 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3541 if (s->size == 0
3542 && (s == rela_dyn
3543 || s == rel_dyn
3544 || s == hash_table->srelplt->output_section
3545 || s == hash_table->splt->output_section))
3546 {
3547 *pp = s->next;
3548 info->output_bfd->section_count--;
3549 strip_zero_sized = TRUE;
3550 if (s == rela_dyn)
3551 s = rela_dyn;
3552 if (s == rel_dyn)
3553 s = rel_dyn;
3554 else if (s == hash_table->splt->output_section)
3555 {
3556 s = hash_table->splt;
3557 strip_zero_sized_plt = TRUE;
3558 }
3559 else
3560 s = hash_table->srelplt;
3561 s->flags |= SEC_EXCLUDE;
3562 s->output_section = bfd_abs_section_ptr;
3563 }
3564 else
3565 pp = &s->next;
3566
3567 if (strip_zero_sized_plt)
3568 for (extdyn = sdynamic->contents;
3569 extdyn < sdynamic->contents + sdynamic->size;
3570 extdyn = next)
3571 {
3572 next = extdyn + bed->s->sizeof_dyn;
3573 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3574 switch (dyn.d_tag)
3575 {
3576 default:
3577 break;
3578 case DT_JMPREL:
3579 case DT_PLTRELSZ:
3580 case DT_PLTREL:
3581 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3582 the procedure linkage table (the .plt section) has been
3583 removed. */
3584 memmove (extdyn, next,
3585 sdynamic->size - (next - sdynamic->contents));
3586 next = extdyn;
3587 }
3588 }
3589
3590 if (strip_zero_sized)
3591 {
3592 /* Regenerate program headers. */
3593 elf_seg_map (info->output_bfd) = NULL;
3594 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3595 }
3596
3597 return TRUE;
3598 }
3599
3600 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3601 1 if a DT_NEEDED tag already exists, and 0 on success. */
3602
3603 int
3604 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3605 {
3606 struct elf_link_hash_table *hash_table;
3607 size_t strindex;
3608 const char *soname;
3609
3610 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3611 return -1;
3612
3613 hash_table = elf_hash_table (info);
3614 soname = elf_dt_name (abfd);
3615 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3616 if (strindex == (size_t) -1)
3617 return -1;
3618
3619 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3620 {
3621 asection *sdyn;
3622 const struct elf_backend_data *bed;
3623 bfd_byte *extdyn;
3624
3625 bed = get_elf_backend_data (hash_table->dynobj);
3626 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3627 if (sdyn != NULL)
3628 for (extdyn = sdyn->contents;
3629 extdyn < sdyn->contents + sdyn->size;
3630 extdyn += bed->s->sizeof_dyn)
3631 {
3632 Elf_Internal_Dyn dyn;
3633
3634 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3635 if (dyn.d_tag == DT_NEEDED
3636 && dyn.d_un.d_val == strindex)
3637 {
3638 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3639 return 1;
3640 }
3641 }
3642 }
3643
3644 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3645 return -1;
3646
3647 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3648 return -1;
3649
3650 return 0;
3651 }
3652
3653 /* Return true if SONAME is on the needed list between NEEDED and STOP
3654 (or the end of list if STOP is NULL), and needed by a library that
3655 will be loaded. */
3656
3657 static bfd_boolean
3658 on_needed_list (const char *soname,
3659 struct bfd_link_needed_list *needed,
3660 struct bfd_link_needed_list *stop)
3661 {
3662 struct bfd_link_needed_list *look;
3663 for (look = needed; look != stop; look = look->next)
3664 if (strcmp (soname, look->name) == 0
3665 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3666 /* If needed by a library that itself is not directly
3667 needed, recursively check whether that library is
3668 indirectly needed. Since we add DT_NEEDED entries to
3669 the end of the list, library dependencies appear after
3670 the library. Therefore search prior to the current
3671 LOOK, preventing possible infinite recursion. */
3672 || on_needed_list (elf_dt_name (look->by), needed, look)))
3673 return TRUE;
3674
3675 return FALSE;
3676 }
3677
3678 /* Sort symbol by value, section, size, and type. */
3679 static int
3680 elf_sort_symbol (const void *arg1, const void *arg2)
3681 {
3682 const struct elf_link_hash_entry *h1;
3683 const struct elf_link_hash_entry *h2;
3684 bfd_signed_vma vdiff;
3685 int sdiff;
3686 const char *n1;
3687 const char *n2;
3688
3689 h1 = *(const struct elf_link_hash_entry **) arg1;
3690 h2 = *(const struct elf_link_hash_entry **) arg2;
3691 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3692 if (vdiff != 0)
3693 return vdiff > 0 ? 1 : -1;
3694
3695 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3696 if (sdiff != 0)
3697 return sdiff;
3698
3699 /* Sort so that sized symbols are selected over zero size symbols. */
3700 vdiff = h1->size - h2->size;
3701 if (vdiff != 0)
3702 return vdiff > 0 ? 1 : -1;
3703
3704 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3705 if (h1->type != h2->type)
3706 return h1->type - h2->type;
3707
3708 /* If symbols are properly sized and typed, and multiple strong
3709 aliases are not defined in a shared library by the user we
3710 shouldn't get here. Unfortunately linker script symbols like
3711 __bss_start sometimes match a user symbol defined at the start of
3712 .bss without proper size and type. We'd like to preference the
3713 user symbol over reserved system symbols. Sort on leading
3714 underscores. */
3715 n1 = h1->root.root.string;
3716 n2 = h2->root.root.string;
3717 while (*n1 == *n2)
3718 {
3719 if (*n1 == 0)
3720 break;
3721 ++n1;
3722 ++n2;
3723 }
3724 if (*n1 == '_')
3725 return -1;
3726 if (*n2 == '_')
3727 return 1;
3728
3729 /* Final sort on name selects user symbols like '_u' over reserved
3730 system symbols like '_Z' and also will avoid qsort instability. */
3731 return *n1 - *n2;
3732 }
3733
3734 /* This function is used to adjust offsets into .dynstr for
3735 dynamic symbols. This is called via elf_link_hash_traverse. */
3736
3737 static bfd_boolean
3738 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3739 {
3740 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3741
3742 if (h->dynindx != -1)
3743 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3744 return TRUE;
3745 }
3746
3747 /* Assign string offsets in .dynstr, update all structures referencing
3748 them. */
3749
3750 static bfd_boolean
3751 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3752 {
3753 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3754 struct elf_link_local_dynamic_entry *entry;
3755 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3756 bfd *dynobj = hash_table->dynobj;
3757 asection *sdyn;
3758 bfd_size_type size;
3759 const struct elf_backend_data *bed;
3760 bfd_byte *extdyn;
3761
3762 _bfd_elf_strtab_finalize (dynstr);
3763 size = _bfd_elf_strtab_size (dynstr);
3764
3765 bed = get_elf_backend_data (dynobj);
3766 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3767 BFD_ASSERT (sdyn != NULL);
3768
3769 /* Update all .dynamic entries referencing .dynstr strings. */
3770 for (extdyn = sdyn->contents;
3771 extdyn < sdyn->contents + sdyn->size;
3772 extdyn += bed->s->sizeof_dyn)
3773 {
3774 Elf_Internal_Dyn dyn;
3775
3776 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3777 switch (dyn.d_tag)
3778 {
3779 case DT_STRSZ:
3780 dyn.d_un.d_val = size;
3781 break;
3782 case DT_NEEDED:
3783 case DT_SONAME:
3784 case DT_RPATH:
3785 case DT_RUNPATH:
3786 case DT_FILTER:
3787 case DT_AUXILIARY:
3788 case DT_AUDIT:
3789 case DT_DEPAUDIT:
3790 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3791 break;
3792 default:
3793 continue;
3794 }
3795 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3796 }
3797
3798 /* Now update local dynamic symbols. */
3799 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3800 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3801 entry->isym.st_name);
3802
3803 /* And the rest of dynamic symbols. */
3804 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3805
3806 /* Adjust version definitions. */
3807 if (elf_tdata (output_bfd)->cverdefs)
3808 {
3809 asection *s;
3810 bfd_byte *p;
3811 size_t i;
3812 Elf_Internal_Verdef def;
3813 Elf_Internal_Verdaux defaux;
3814
3815 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3816 p = s->contents;
3817 do
3818 {
3819 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3820 &def);
3821 p += sizeof (Elf_External_Verdef);
3822 if (def.vd_aux != sizeof (Elf_External_Verdef))
3823 continue;
3824 for (i = 0; i < def.vd_cnt; ++i)
3825 {
3826 _bfd_elf_swap_verdaux_in (output_bfd,
3827 (Elf_External_Verdaux *) p, &defaux);
3828 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3829 defaux.vda_name);
3830 _bfd_elf_swap_verdaux_out (output_bfd,
3831 &defaux, (Elf_External_Verdaux *) p);
3832 p += sizeof (Elf_External_Verdaux);
3833 }
3834 }
3835 while (def.vd_next);
3836 }
3837
3838 /* Adjust version references. */
3839 if (elf_tdata (output_bfd)->verref)
3840 {
3841 asection *s;
3842 bfd_byte *p;
3843 size_t i;
3844 Elf_Internal_Verneed need;
3845 Elf_Internal_Vernaux needaux;
3846
3847 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3848 p = s->contents;
3849 do
3850 {
3851 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3852 &need);
3853 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3854 _bfd_elf_swap_verneed_out (output_bfd, &need,
3855 (Elf_External_Verneed *) p);
3856 p += sizeof (Elf_External_Verneed);
3857 for (i = 0; i < need.vn_cnt; ++i)
3858 {
3859 _bfd_elf_swap_vernaux_in (output_bfd,
3860 (Elf_External_Vernaux *) p, &needaux);
3861 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3862 needaux.vna_name);
3863 _bfd_elf_swap_vernaux_out (output_bfd,
3864 &needaux,
3865 (Elf_External_Vernaux *) p);
3866 p += sizeof (Elf_External_Vernaux);
3867 }
3868 }
3869 while (need.vn_next);
3870 }
3871
3872 return TRUE;
3873 }
3874 \f
3875 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3876 The default is to only match when the INPUT and OUTPUT are exactly
3877 the same target. */
3878
3879 bfd_boolean
3880 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3881 const bfd_target *output)
3882 {
3883 return input == output;
3884 }
3885
3886 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3887 This version is used when different targets for the same architecture
3888 are virtually identical. */
3889
3890 bfd_boolean
3891 _bfd_elf_relocs_compatible (const bfd_target *input,
3892 const bfd_target *output)
3893 {
3894 const struct elf_backend_data *obed, *ibed;
3895
3896 if (input == output)
3897 return TRUE;
3898
3899 ibed = xvec_get_elf_backend_data (input);
3900 obed = xvec_get_elf_backend_data (output);
3901
3902 if (ibed->arch != obed->arch)
3903 return FALSE;
3904
3905 /* If both backends are using this function, deem them compatible. */
3906 return ibed->relocs_compatible == obed->relocs_compatible;
3907 }
3908
3909 /* Make a special call to the linker "notice" function to tell it that
3910 we are about to handle an as-needed lib, or have finished
3911 processing the lib. */
3912
3913 bfd_boolean
3914 _bfd_elf_notice_as_needed (bfd *ibfd,
3915 struct bfd_link_info *info,
3916 enum notice_asneeded_action act)
3917 {
3918 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3919 }
3920
3921 /* Check relocations an ELF object file. */
3922
3923 bfd_boolean
3924 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3925 {
3926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3927 struct elf_link_hash_table *htab = elf_hash_table (info);
3928
3929 /* If this object is the same format as the output object, and it is
3930 not a shared library, then let the backend look through the
3931 relocs.
3932
3933 This is required to build global offset table entries and to
3934 arrange for dynamic relocs. It is not required for the
3935 particular common case of linking non PIC code, even when linking
3936 against shared libraries, but unfortunately there is no way of
3937 knowing whether an object file has been compiled PIC or not.
3938 Looking through the relocs is not particularly time consuming.
3939 The problem is that we must either (1) keep the relocs in memory,
3940 which causes the linker to require additional runtime memory or
3941 (2) read the relocs twice from the input file, which wastes time.
3942 This would be a good case for using mmap.
3943
3944 I have no idea how to handle linking PIC code into a file of a
3945 different format. It probably can't be done. */
3946 if ((abfd->flags & DYNAMIC) == 0
3947 && is_elf_hash_table (htab)
3948 && bed->check_relocs != NULL
3949 && elf_object_id (abfd) == elf_hash_table_id (htab)
3950 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3951 {
3952 asection *o;
3953
3954 for (o = abfd->sections; o != NULL; o = o->next)
3955 {
3956 Elf_Internal_Rela *internal_relocs;
3957 bfd_boolean ok;
3958
3959 /* Don't check relocations in excluded sections. Don't do
3960 anything special with non-loaded, non-alloced sections.
3961 In particular, any relocs in such sections should not
3962 affect GOT and PLT reference counting (ie. we don't
3963 allow them to create GOT or PLT entries), there's no
3964 possibility or desire to optimize TLS relocs, and
3965 there's not much point in propagating relocs to shared
3966 libs that the dynamic linker won't relocate. */
3967 if ((o->flags & SEC_ALLOC) == 0
3968 || (o->flags & SEC_RELOC) == 0
3969 || (o->flags & SEC_EXCLUDE) != 0
3970 || o->reloc_count == 0
3971 || ((info->strip == strip_all || info->strip == strip_debugger)
3972 && (o->flags & SEC_DEBUGGING) != 0)
3973 || bfd_is_abs_section (o->output_section))
3974 continue;
3975
3976 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3977 info->keep_memory);
3978 if (internal_relocs == NULL)
3979 return FALSE;
3980
3981 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3982
3983 if (elf_section_data (o)->relocs != internal_relocs)
3984 free (internal_relocs);
3985
3986 if (! ok)
3987 return FALSE;
3988 }
3989 }
3990
3991 return TRUE;
3992 }
3993
3994 /* Add symbols from an ELF object file to the linker hash table. */
3995
3996 static bfd_boolean
3997 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3998 {
3999 Elf_Internal_Ehdr *ehdr;
4000 Elf_Internal_Shdr *hdr;
4001 size_t symcount;
4002 size_t extsymcount;
4003 size_t extsymoff;
4004 struct elf_link_hash_entry **sym_hash;
4005 bfd_boolean dynamic;
4006 Elf_External_Versym *extversym = NULL;
4007 Elf_External_Versym *extversym_end = NULL;
4008 Elf_External_Versym *ever;
4009 struct elf_link_hash_entry *weaks;
4010 struct elf_link_hash_entry **nondeflt_vers = NULL;
4011 size_t nondeflt_vers_cnt = 0;
4012 Elf_Internal_Sym *isymbuf = NULL;
4013 Elf_Internal_Sym *isym;
4014 Elf_Internal_Sym *isymend;
4015 const struct elf_backend_data *bed;
4016 bfd_boolean add_needed;
4017 struct elf_link_hash_table *htab;
4018 void *alloc_mark = NULL;
4019 struct bfd_hash_entry **old_table = NULL;
4020 unsigned int old_size = 0;
4021 unsigned int old_count = 0;
4022 void *old_tab = NULL;
4023 void *old_ent;
4024 struct bfd_link_hash_entry *old_undefs = NULL;
4025 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4026 void *old_strtab = NULL;
4027 size_t tabsize = 0;
4028 asection *s;
4029 bfd_boolean just_syms;
4030
4031 htab = elf_hash_table (info);
4032 bed = get_elf_backend_data (abfd);
4033
4034 if ((abfd->flags & DYNAMIC) == 0)
4035 dynamic = FALSE;
4036 else
4037 {
4038 dynamic = TRUE;
4039
4040 /* You can't use -r against a dynamic object. Also, there's no
4041 hope of using a dynamic object which does not exactly match
4042 the format of the output file. */
4043 if (bfd_link_relocatable (info)
4044 || !is_elf_hash_table (htab)
4045 || info->output_bfd->xvec != abfd->xvec)
4046 {
4047 if (bfd_link_relocatable (info))
4048 bfd_set_error (bfd_error_invalid_operation);
4049 else
4050 bfd_set_error (bfd_error_wrong_format);
4051 goto error_return;
4052 }
4053 }
4054
4055 ehdr = elf_elfheader (abfd);
4056 if (info->warn_alternate_em
4057 && bed->elf_machine_code != ehdr->e_machine
4058 && ((bed->elf_machine_alt1 != 0
4059 && ehdr->e_machine == bed->elf_machine_alt1)
4060 || (bed->elf_machine_alt2 != 0
4061 && ehdr->e_machine == bed->elf_machine_alt2)))
4062 _bfd_error_handler
4063 /* xgettext:c-format */
4064 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4065 ehdr->e_machine, abfd, bed->elf_machine_code);
4066
4067 /* As a GNU extension, any input sections which are named
4068 .gnu.warning.SYMBOL are treated as warning symbols for the given
4069 symbol. This differs from .gnu.warning sections, which generate
4070 warnings when they are included in an output file. */
4071 /* PR 12761: Also generate this warning when building shared libraries. */
4072 for (s = abfd->sections; s != NULL; s = s->next)
4073 {
4074 const char *name;
4075
4076 name = bfd_section_name (s);
4077 if (CONST_STRNEQ (name, ".gnu.warning."))
4078 {
4079 char *msg;
4080 bfd_size_type sz;
4081
4082 name += sizeof ".gnu.warning." - 1;
4083
4084 /* If this is a shared object, then look up the symbol
4085 in the hash table. If it is there, and it is already
4086 been defined, then we will not be using the entry
4087 from this shared object, so we don't need to warn.
4088 FIXME: If we see the definition in a regular object
4089 later on, we will warn, but we shouldn't. The only
4090 fix is to keep track of what warnings we are supposed
4091 to emit, and then handle them all at the end of the
4092 link. */
4093 if (dynamic)
4094 {
4095 struct elf_link_hash_entry *h;
4096
4097 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4098
4099 /* FIXME: What about bfd_link_hash_common? */
4100 if (h != NULL
4101 && (h->root.type == bfd_link_hash_defined
4102 || h->root.type == bfd_link_hash_defweak))
4103 continue;
4104 }
4105
4106 sz = s->size;
4107 msg = (char *) bfd_alloc (abfd, sz + 1);
4108 if (msg == NULL)
4109 goto error_return;
4110
4111 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4112 goto error_return;
4113
4114 msg[sz] = '\0';
4115
4116 if (! (_bfd_generic_link_add_one_symbol
4117 (info, abfd, name, BSF_WARNING, s, 0, msg,
4118 FALSE, bed->collect, NULL)))
4119 goto error_return;
4120
4121 if (bfd_link_executable (info))
4122 {
4123 /* Clobber the section size so that the warning does
4124 not get copied into the output file. */
4125 s->size = 0;
4126
4127 /* Also set SEC_EXCLUDE, so that symbols defined in
4128 the warning section don't get copied to the output. */
4129 s->flags |= SEC_EXCLUDE;
4130 }
4131 }
4132 }
4133
4134 just_syms = ((s = abfd->sections) != NULL
4135 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4136
4137 add_needed = TRUE;
4138 if (! dynamic)
4139 {
4140 /* If we are creating a shared library, create all the dynamic
4141 sections immediately. We need to attach them to something,
4142 so we attach them to this BFD, provided it is the right
4143 format and is not from ld --just-symbols. Always create the
4144 dynamic sections for -E/--dynamic-list. FIXME: If there
4145 are no input BFD's of the same format as the output, we can't
4146 make a shared library. */
4147 if (!just_syms
4148 && (bfd_link_pic (info)
4149 || (!bfd_link_relocatable (info)
4150 && info->nointerp
4151 && (info->export_dynamic || info->dynamic)))
4152 && is_elf_hash_table (htab)
4153 && info->output_bfd->xvec == abfd->xvec
4154 && !htab->dynamic_sections_created)
4155 {
4156 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4157 goto error_return;
4158 }
4159 }
4160 else if (!is_elf_hash_table (htab))
4161 goto error_return;
4162 else
4163 {
4164 const char *soname = NULL;
4165 char *audit = NULL;
4166 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4167 const Elf_Internal_Phdr *phdr;
4168 struct elf_link_loaded_list *loaded_lib;
4169
4170 /* ld --just-symbols and dynamic objects don't mix very well.
4171 ld shouldn't allow it. */
4172 if (just_syms)
4173 abort ();
4174
4175 /* If this dynamic lib was specified on the command line with
4176 --as-needed in effect, then we don't want to add a DT_NEEDED
4177 tag unless the lib is actually used. Similary for libs brought
4178 in by another lib's DT_NEEDED. When --no-add-needed is used
4179 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4180 any dynamic library in DT_NEEDED tags in the dynamic lib at
4181 all. */
4182 add_needed = (elf_dyn_lib_class (abfd)
4183 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4184 | DYN_NO_NEEDED)) == 0;
4185
4186 s = bfd_get_section_by_name (abfd, ".dynamic");
4187 if (s != NULL)
4188 {
4189 bfd_byte *dynbuf;
4190 bfd_byte *extdyn;
4191 unsigned int elfsec;
4192 unsigned long shlink;
4193
4194 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4195 {
4196 error_free_dyn:
4197 free (dynbuf);
4198 goto error_return;
4199 }
4200
4201 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4202 if (elfsec == SHN_BAD)
4203 goto error_free_dyn;
4204 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4205
4206 for (extdyn = dynbuf;
4207 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4208 extdyn += bed->s->sizeof_dyn)
4209 {
4210 Elf_Internal_Dyn dyn;
4211
4212 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4213 if (dyn.d_tag == DT_SONAME)
4214 {
4215 unsigned int tagv = dyn.d_un.d_val;
4216 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4217 if (soname == NULL)
4218 goto error_free_dyn;
4219 }
4220 if (dyn.d_tag == DT_NEEDED)
4221 {
4222 struct bfd_link_needed_list *n, **pn;
4223 char *fnm, *anm;
4224 unsigned int tagv = dyn.d_un.d_val;
4225 size_t amt = sizeof (struct bfd_link_needed_list);
4226
4227 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4228 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4229 if (n == NULL || fnm == NULL)
4230 goto error_free_dyn;
4231 amt = strlen (fnm) + 1;
4232 anm = (char *) bfd_alloc (abfd, amt);
4233 if (anm == NULL)
4234 goto error_free_dyn;
4235 memcpy (anm, fnm, amt);
4236 n->name = anm;
4237 n->by = abfd;
4238 n->next = NULL;
4239 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4240 ;
4241 *pn = n;
4242 }
4243 if (dyn.d_tag == DT_RUNPATH)
4244 {
4245 struct bfd_link_needed_list *n, **pn;
4246 char *fnm, *anm;
4247 unsigned int tagv = dyn.d_un.d_val;
4248 size_t amt = sizeof (struct bfd_link_needed_list);
4249
4250 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4251 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4252 if (n == NULL || fnm == NULL)
4253 goto error_free_dyn;
4254 amt = strlen (fnm) + 1;
4255 anm = (char *) bfd_alloc (abfd, amt);
4256 if (anm == NULL)
4257 goto error_free_dyn;
4258 memcpy (anm, fnm, amt);
4259 n->name = anm;
4260 n->by = abfd;
4261 n->next = NULL;
4262 for (pn = & runpath;
4263 *pn != NULL;
4264 pn = &(*pn)->next)
4265 ;
4266 *pn = n;
4267 }
4268 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4269 if (!runpath && dyn.d_tag == DT_RPATH)
4270 {
4271 struct bfd_link_needed_list *n, **pn;
4272 char *fnm, *anm;
4273 unsigned int tagv = dyn.d_un.d_val;
4274 size_t amt = sizeof (struct bfd_link_needed_list);
4275
4276 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4277 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4278 if (n == NULL || fnm == NULL)
4279 goto error_free_dyn;
4280 amt = strlen (fnm) + 1;
4281 anm = (char *) bfd_alloc (abfd, amt);
4282 if (anm == NULL)
4283 goto error_free_dyn;
4284 memcpy (anm, fnm, amt);
4285 n->name = anm;
4286 n->by = abfd;
4287 n->next = NULL;
4288 for (pn = & rpath;
4289 *pn != NULL;
4290 pn = &(*pn)->next)
4291 ;
4292 *pn = n;
4293 }
4294 if (dyn.d_tag == DT_AUDIT)
4295 {
4296 unsigned int tagv = dyn.d_un.d_val;
4297 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4298 }
4299 }
4300
4301 free (dynbuf);
4302 }
4303
4304 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4305 frees all more recently bfd_alloc'd blocks as well. */
4306 if (runpath)
4307 rpath = runpath;
4308
4309 if (rpath)
4310 {
4311 struct bfd_link_needed_list **pn;
4312 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4313 ;
4314 *pn = rpath;
4315 }
4316
4317 /* If we have a PT_GNU_RELRO program header, mark as read-only
4318 all sections contained fully therein. This makes relro
4319 shared library sections appear as they will at run-time. */
4320 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4321 while (phdr-- > elf_tdata (abfd)->phdr)
4322 if (phdr->p_type == PT_GNU_RELRO)
4323 {
4324 for (s = abfd->sections; s != NULL; s = s->next)
4325 {
4326 unsigned int opb = bfd_octets_per_byte (abfd, s);
4327
4328 if ((s->flags & SEC_ALLOC) != 0
4329 && s->vma * opb >= phdr->p_vaddr
4330 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4331 s->flags |= SEC_READONLY;
4332 }
4333 break;
4334 }
4335
4336 /* We do not want to include any of the sections in a dynamic
4337 object in the output file. We hack by simply clobbering the
4338 list of sections in the BFD. This could be handled more
4339 cleanly by, say, a new section flag; the existing
4340 SEC_NEVER_LOAD flag is not the one we want, because that one
4341 still implies that the section takes up space in the output
4342 file. */
4343 bfd_section_list_clear (abfd);
4344
4345 /* Find the name to use in a DT_NEEDED entry that refers to this
4346 object. If the object has a DT_SONAME entry, we use it.
4347 Otherwise, if the generic linker stuck something in
4348 elf_dt_name, we use that. Otherwise, we just use the file
4349 name. */
4350 if (soname == NULL || *soname == '\0')
4351 {
4352 soname = elf_dt_name (abfd);
4353 if (soname == NULL || *soname == '\0')
4354 soname = bfd_get_filename (abfd);
4355 }
4356
4357 /* Save the SONAME because sometimes the linker emulation code
4358 will need to know it. */
4359 elf_dt_name (abfd) = soname;
4360
4361 /* If we have already included this dynamic object in the
4362 link, just ignore it. There is no reason to include a
4363 particular dynamic object more than once. */
4364 for (loaded_lib = htab->dyn_loaded;
4365 loaded_lib != NULL;
4366 loaded_lib = loaded_lib->next)
4367 {
4368 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4369 return TRUE;
4370 }
4371
4372 /* Create dynamic sections for backends that require that be done
4373 before setup_gnu_properties. */
4374 if (add_needed
4375 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4376 return FALSE;
4377
4378 /* Save the DT_AUDIT entry for the linker emulation code. */
4379 elf_dt_audit (abfd) = audit;
4380 }
4381
4382 /* If this is a dynamic object, we always link against the .dynsym
4383 symbol table, not the .symtab symbol table. The dynamic linker
4384 will only see the .dynsym symbol table, so there is no reason to
4385 look at .symtab for a dynamic object. */
4386
4387 if (! dynamic || elf_dynsymtab (abfd) == 0)
4388 hdr = &elf_tdata (abfd)->symtab_hdr;
4389 else
4390 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4391
4392 symcount = hdr->sh_size / bed->s->sizeof_sym;
4393
4394 /* The sh_info field of the symtab header tells us where the
4395 external symbols start. We don't care about the local symbols at
4396 this point. */
4397 if (elf_bad_symtab (abfd))
4398 {
4399 extsymcount = symcount;
4400 extsymoff = 0;
4401 }
4402 else
4403 {
4404 extsymcount = symcount - hdr->sh_info;
4405 extsymoff = hdr->sh_info;
4406 }
4407
4408 sym_hash = elf_sym_hashes (abfd);
4409 if (extsymcount != 0)
4410 {
4411 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4412 NULL, NULL, NULL);
4413 if (isymbuf == NULL)
4414 goto error_return;
4415
4416 if (sym_hash == NULL)
4417 {
4418 /* We store a pointer to the hash table entry for each
4419 external symbol. */
4420 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4421 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4422 if (sym_hash == NULL)
4423 goto error_free_sym;
4424 elf_sym_hashes (abfd) = sym_hash;
4425 }
4426 }
4427
4428 if (dynamic)
4429 {
4430 /* Read in any version definitions. */
4431 if (!_bfd_elf_slurp_version_tables (abfd,
4432 info->default_imported_symver))
4433 goto error_free_sym;
4434
4435 /* Read in the symbol versions, but don't bother to convert them
4436 to internal format. */
4437 if (elf_dynversym (abfd) != 0)
4438 {
4439 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4440 bfd_size_type amt = versymhdr->sh_size;
4441
4442 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4443 goto error_free_sym;
4444 extversym = (Elf_External_Versym *)
4445 _bfd_malloc_and_read (abfd, amt, amt);
4446 if (extversym == NULL)
4447 goto error_free_sym;
4448 extversym_end = extversym + amt / sizeof (*extversym);
4449 }
4450 }
4451
4452 /* If we are loading an as-needed shared lib, save the symbol table
4453 state before we start adding symbols. If the lib turns out
4454 to be unneeded, restore the state. */
4455 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4456 {
4457 unsigned int i;
4458 size_t entsize;
4459
4460 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4461 {
4462 struct bfd_hash_entry *p;
4463 struct elf_link_hash_entry *h;
4464
4465 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4466 {
4467 h = (struct elf_link_hash_entry *) p;
4468 entsize += htab->root.table.entsize;
4469 if (h->root.type == bfd_link_hash_warning)
4470 entsize += htab->root.table.entsize;
4471 }
4472 }
4473
4474 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4475 old_tab = bfd_malloc (tabsize + entsize);
4476 if (old_tab == NULL)
4477 goto error_free_vers;
4478
4479 /* Remember the current objalloc pointer, so that all mem for
4480 symbols added can later be reclaimed. */
4481 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4482 if (alloc_mark == NULL)
4483 goto error_free_vers;
4484
4485 /* Make a special call to the linker "notice" function to
4486 tell it that we are about to handle an as-needed lib. */
4487 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4488 goto error_free_vers;
4489
4490 /* Clone the symbol table. Remember some pointers into the
4491 symbol table, and dynamic symbol count. */
4492 old_ent = (char *) old_tab + tabsize;
4493 memcpy (old_tab, htab->root.table.table, tabsize);
4494 old_undefs = htab->root.undefs;
4495 old_undefs_tail = htab->root.undefs_tail;
4496 old_table = htab->root.table.table;
4497 old_size = htab->root.table.size;
4498 old_count = htab->root.table.count;
4499 old_strtab = NULL;
4500 if (htab->dynstr != NULL)
4501 {
4502 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4503 if (old_strtab == NULL)
4504 goto error_free_vers;
4505 }
4506
4507 for (i = 0; i < htab->root.table.size; i++)
4508 {
4509 struct bfd_hash_entry *p;
4510 struct elf_link_hash_entry *h;
4511
4512 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4513 {
4514 memcpy (old_ent, p, htab->root.table.entsize);
4515 old_ent = (char *) old_ent + htab->root.table.entsize;
4516 h = (struct elf_link_hash_entry *) p;
4517 if (h->root.type == bfd_link_hash_warning)
4518 {
4519 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4520 old_ent = (char *) old_ent + htab->root.table.entsize;
4521 }
4522 }
4523 }
4524 }
4525
4526 weaks = NULL;
4527 if (extversym == NULL)
4528 ever = NULL;
4529 else if (extversym + extsymoff < extversym_end)
4530 ever = extversym + extsymoff;
4531 else
4532 {
4533 /* xgettext:c-format */
4534 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4535 abfd, (long) extsymoff,
4536 (long) (extversym_end - extversym) / sizeof (* extversym));
4537 bfd_set_error (bfd_error_bad_value);
4538 goto error_free_vers;
4539 }
4540
4541 if (!bfd_link_relocatable (info)
4542 && abfd->lto_slim_object)
4543 {
4544 _bfd_error_handler
4545 (_("%pB: plugin needed to handle lto object"), abfd);
4546 }
4547
4548 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4549 isym < isymend;
4550 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4551 {
4552 int bind;
4553 bfd_vma value;
4554 asection *sec, *new_sec;
4555 flagword flags;
4556 const char *name;
4557 struct elf_link_hash_entry *h;
4558 struct elf_link_hash_entry *hi;
4559 bfd_boolean definition;
4560 bfd_boolean size_change_ok;
4561 bfd_boolean type_change_ok;
4562 bfd_boolean new_weak;
4563 bfd_boolean old_weak;
4564 bfd_boolean override;
4565 bfd_boolean common;
4566 bfd_boolean discarded;
4567 unsigned int old_alignment;
4568 unsigned int shindex;
4569 bfd *old_bfd;
4570 bfd_boolean matched;
4571
4572 override = FALSE;
4573
4574 flags = BSF_NO_FLAGS;
4575 sec = NULL;
4576 value = isym->st_value;
4577 common = bed->common_definition (isym);
4578 if (common && info->inhibit_common_definition)
4579 {
4580 /* Treat common symbol as undefined for --no-define-common. */
4581 isym->st_shndx = SHN_UNDEF;
4582 common = FALSE;
4583 }
4584 discarded = FALSE;
4585
4586 bind = ELF_ST_BIND (isym->st_info);
4587 switch (bind)
4588 {
4589 case STB_LOCAL:
4590 /* This should be impossible, since ELF requires that all
4591 global symbols follow all local symbols, and that sh_info
4592 point to the first global symbol. Unfortunately, Irix 5
4593 screws this up. */
4594 if (elf_bad_symtab (abfd))
4595 continue;
4596
4597 /* If we aren't prepared to handle locals within the globals
4598 then we'll likely segfault on a NULL symbol hash if the
4599 symbol is ever referenced in relocations. */
4600 shindex = elf_elfheader (abfd)->e_shstrndx;
4601 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4602 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4603 " (>= sh_info of %lu)"),
4604 abfd, name, (long) (isym - isymbuf + extsymoff),
4605 (long) extsymoff);
4606
4607 /* Dynamic object relocations are not processed by ld, so
4608 ld won't run into the problem mentioned above. */
4609 if (dynamic)
4610 continue;
4611 bfd_set_error (bfd_error_bad_value);
4612 goto error_free_vers;
4613
4614 case STB_GLOBAL:
4615 if (isym->st_shndx != SHN_UNDEF && !common)
4616 flags = BSF_GLOBAL;
4617 break;
4618
4619 case STB_WEAK:
4620 flags = BSF_WEAK;
4621 break;
4622
4623 case STB_GNU_UNIQUE:
4624 flags = BSF_GNU_UNIQUE;
4625 break;
4626
4627 default:
4628 /* Leave it up to the processor backend. */
4629 break;
4630 }
4631
4632 if (isym->st_shndx == SHN_UNDEF)
4633 sec = bfd_und_section_ptr;
4634 else if (isym->st_shndx == SHN_ABS)
4635 sec = bfd_abs_section_ptr;
4636 else if (isym->st_shndx == SHN_COMMON)
4637 {
4638 sec = bfd_com_section_ptr;
4639 /* What ELF calls the size we call the value. What ELF
4640 calls the value we call the alignment. */
4641 value = isym->st_size;
4642 }
4643 else
4644 {
4645 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4646 if (sec == NULL)
4647 sec = bfd_abs_section_ptr;
4648 else if (discarded_section (sec))
4649 {
4650 /* Symbols from discarded section are undefined. We keep
4651 its visibility. */
4652 sec = bfd_und_section_ptr;
4653 discarded = TRUE;
4654 isym->st_shndx = SHN_UNDEF;
4655 }
4656 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4657 value -= sec->vma;
4658 }
4659
4660 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4661 isym->st_name);
4662 if (name == NULL)
4663 goto error_free_vers;
4664
4665 if (isym->st_shndx == SHN_COMMON
4666 && (abfd->flags & BFD_PLUGIN) != 0)
4667 {
4668 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4669
4670 if (xc == NULL)
4671 {
4672 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4673 | SEC_EXCLUDE);
4674 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4675 if (xc == NULL)
4676 goto error_free_vers;
4677 }
4678 sec = xc;
4679 }
4680 else if (isym->st_shndx == SHN_COMMON
4681 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4682 && !bfd_link_relocatable (info))
4683 {
4684 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4685
4686 if (tcomm == NULL)
4687 {
4688 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4689 | SEC_LINKER_CREATED);
4690 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4691 if (tcomm == NULL)
4692 goto error_free_vers;
4693 }
4694 sec = tcomm;
4695 }
4696 else if (bed->elf_add_symbol_hook)
4697 {
4698 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4699 &sec, &value))
4700 goto error_free_vers;
4701
4702 /* The hook function sets the name to NULL if this symbol
4703 should be skipped for some reason. */
4704 if (name == NULL)
4705 continue;
4706 }
4707
4708 /* Sanity check that all possibilities were handled. */
4709 if (sec == NULL)
4710 abort ();
4711
4712 /* Silently discard TLS symbols from --just-syms. There's
4713 no way to combine a static TLS block with a new TLS block
4714 for this executable. */
4715 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4716 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4717 continue;
4718
4719 if (bfd_is_und_section (sec)
4720 || bfd_is_com_section (sec))
4721 definition = FALSE;
4722 else
4723 definition = TRUE;
4724
4725 size_change_ok = FALSE;
4726 type_change_ok = bed->type_change_ok;
4727 old_weak = FALSE;
4728 matched = FALSE;
4729 old_alignment = 0;
4730 old_bfd = NULL;
4731 new_sec = sec;
4732
4733 if (is_elf_hash_table (htab))
4734 {
4735 Elf_Internal_Versym iver;
4736 unsigned int vernum = 0;
4737 bfd_boolean skip;
4738
4739 if (ever == NULL)
4740 {
4741 if (info->default_imported_symver)
4742 /* Use the default symbol version created earlier. */
4743 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4744 else
4745 iver.vs_vers = 0;
4746 }
4747 else if (ever >= extversym_end)
4748 {
4749 /* xgettext:c-format */
4750 _bfd_error_handler (_("%pB: not enough version information"),
4751 abfd);
4752 bfd_set_error (bfd_error_bad_value);
4753 goto error_free_vers;
4754 }
4755 else
4756 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4757
4758 vernum = iver.vs_vers & VERSYM_VERSION;
4759
4760 /* If this is a hidden symbol, or if it is not version
4761 1, we append the version name to the symbol name.
4762 However, we do not modify a non-hidden absolute symbol
4763 if it is not a function, because it might be the version
4764 symbol itself. FIXME: What if it isn't? */
4765 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4766 || (vernum > 1
4767 && (!bfd_is_abs_section (sec)
4768 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4769 {
4770 const char *verstr;
4771 size_t namelen, verlen, newlen;
4772 char *newname, *p;
4773
4774 if (isym->st_shndx != SHN_UNDEF)
4775 {
4776 if (vernum > elf_tdata (abfd)->cverdefs)
4777 verstr = NULL;
4778 else if (vernum > 1)
4779 verstr =
4780 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4781 else
4782 verstr = "";
4783
4784 if (verstr == NULL)
4785 {
4786 _bfd_error_handler
4787 /* xgettext:c-format */
4788 (_("%pB: %s: invalid version %u (max %d)"),
4789 abfd, name, vernum,
4790 elf_tdata (abfd)->cverdefs);
4791 bfd_set_error (bfd_error_bad_value);
4792 goto error_free_vers;
4793 }
4794 }
4795 else
4796 {
4797 /* We cannot simply test for the number of
4798 entries in the VERNEED section since the
4799 numbers for the needed versions do not start
4800 at 0. */
4801 Elf_Internal_Verneed *t;
4802
4803 verstr = NULL;
4804 for (t = elf_tdata (abfd)->verref;
4805 t != NULL;
4806 t = t->vn_nextref)
4807 {
4808 Elf_Internal_Vernaux *a;
4809
4810 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4811 {
4812 if (a->vna_other == vernum)
4813 {
4814 verstr = a->vna_nodename;
4815 break;
4816 }
4817 }
4818 if (a != NULL)
4819 break;
4820 }
4821 if (verstr == NULL)
4822 {
4823 _bfd_error_handler
4824 /* xgettext:c-format */
4825 (_("%pB: %s: invalid needed version %d"),
4826 abfd, name, vernum);
4827 bfd_set_error (bfd_error_bad_value);
4828 goto error_free_vers;
4829 }
4830 }
4831
4832 namelen = strlen (name);
4833 verlen = strlen (verstr);
4834 newlen = namelen + verlen + 2;
4835 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4836 && isym->st_shndx != SHN_UNDEF)
4837 ++newlen;
4838
4839 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4840 if (newname == NULL)
4841 goto error_free_vers;
4842 memcpy (newname, name, namelen);
4843 p = newname + namelen;
4844 *p++ = ELF_VER_CHR;
4845 /* If this is a defined non-hidden version symbol,
4846 we add another @ to the name. This indicates the
4847 default version of the symbol. */
4848 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4849 && isym->st_shndx != SHN_UNDEF)
4850 *p++ = ELF_VER_CHR;
4851 memcpy (p, verstr, verlen + 1);
4852
4853 name = newname;
4854 }
4855
4856 /* If this symbol has default visibility and the user has
4857 requested we not re-export it, then mark it as hidden. */
4858 if (!bfd_is_und_section (sec)
4859 && !dynamic
4860 && abfd->no_export
4861 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4862 isym->st_other = (STV_HIDDEN
4863 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4864
4865 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4866 sym_hash, &old_bfd, &old_weak,
4867 &old_alignment, &skip, &override,
4868 &type_change_ok, &size_change_ok,
4869 &matched))
4870 goto error_free_vers;
4871
4872 if (skip)
4873 continue;
4874
4875 /* Override a definition only if the new symbol matches the
4876 existing one. */
4877 if (override && matched)
4878 definition = FALSE;
4879
4880 h = *sym_hash;
4881 while (h->root.type == bfd_link_hash_indirect
4882 || h->root.type == bfd_link_hash_warning)
4883 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4884
4885 if (elf_tdata (abfd)->verdef != NULL
4886 && vernum > 1
4887 && definition)
4888 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4889 }
4890
4891 if (! (_bfd_generic_link_add_one_symbol
4892 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4893 (struct bfd_link_hash_entry **) sym_hash)))
4894 goto error_free_vers;
4895
4896 h = *sym_hash;
4897 /* We need to make sure that indirect symbol dynamic flags are
4898 updated. */
4899 hi = h;
4900 while (h->root.type == bfd_link_hash_indirect
4901 || h->root.type == bfd_link_hash_warning)
4902 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4903
4904 /* Setting the index to -3 tells elf_link_output_extsym that
4905 this symbol is defined in a discarded section. */
4906 if (discarded)
4907 h->indx = -3;
4908
4909 *sym_hash = h;
4910
4911 new_weak = (flags & BSF_WEAK) != 0;
4912 if (dynamic
4913 && definition
4914 && new_weak
4915 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4916 && is_elf_hash_table (htab)
4917 && h->u.alias == NULL)
4918 {
4919 /* Keep a list of all weak defined non function symbols from
4920 a dynamic object, using the alias field. Later in this
4921 function we will set the alias field to the correct
4922 value. We only put non-function symbols from dynamic
4923 objects on this list, because that happens to be the only
4924 time we need to know the normal symbol corresponding to a
4925 weak symbol, and the information is time consuming to
4926 figure out. If the alias field is not already NULL,
4927 then this symbol was already defined by some previous
4928 dynamic object, and we will be using that previous
4929 definition anyhow. */
4930
4931 h->u.alias = weaks;
4932 weaks = h;
4933 }
4934
4935 /* Set the alignment of a common symbol. */
4936 if ((common || bfd_is_com_section (sec))
4937 && h->root.type == bfd_link_hash_common)
4938 {
4939 unsigned int align;
4940
4941 if (common)
4942 align = bfd_log2 (isym->st_value);
4943 else
4944 {
4945 /* The new symbol is a common symbol in a shared object.
4946 We need to get the alignment from the section. */
4947 align = new_sec->alignment_power;
4948 }
4949 if (align > old_alignment)
4950 h->root.u.c.p->alignment_power = align;
4951 else
4952 h->root.u.c.p->alignment_power = old_alignment;
4953 }
4954
4955 if (is_elf_hash_table (htab))
4956 {
4957 /* Set a flag in the hash table entry indicating the type of
4958 reference or definition we just found. A dynamic symbol
4959 is one which is referenced or defined by both a regular
4960 object and a shared object. */
4961 bfd_boolean dynsym = FALSE;
4962
4963 /* Plugin symbols aren't normal. Don't set def_regular or
4964 ref_regular for them, or make them dynamic. */
4965 if ((abfd->flags & BFD_PLUGIN) != 0)
4966 ;
4967 else if (! dynamic)
4968 {
4969 if (! definition)
4970 {
4971 h->ref_regular = 1;
4972 if (bind != STB_WEAK)
4973 h->ref_regular_nonweak = 1;
4974 }
4975 else
4976 {
4977 h->def_regular = 1;
4978 if (h->def_dynamic)
4979 {
4980 h->def_dynamic = 0;
4981 h->ref_dynamic = 1;
4982 }
4983 }
4984
4985 /* If the indirect symbol has been forced local, don't
4986 make the real symbol dynamic. */
4987 if ((h == hi || !hi->forced_local)
4988 && (bfd_link_dll (info)
4989 || h->def_dynamic
4990 || h->ref_dynamic))
4991 dynsym = TRUE;
4992 }
4993 else
4994 {
4995 if (! definition)
4996 {
4997 h->ref_dynamic = 1;
4998 hi->ref_dynamic = 1;
4999 }
5000 else
5001 {
5002 h->def_dynamic = 1;
5003 hi->def_dynamic = 1;
5004 }
5005
5006 /* If the indirect symbol has been forced local, don't
5007 make the real symbol dynamic. */
5008 if ((h == hi || !hi->forced_local)
5009 && (h->def_regular
5010 || h->ref_regular
5011 || (h->is_weakalias
5012 && weakdef (h)->dynindx != -1)))
5013 dynsym = TRUE;
5014 }
5015
5016 /* Check to see if we need to add an indirect symbol for
5017 the default name. */
5018 if (definition
5019 || (!override && h->root.type == bfd_link_hash_common))
5020 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5021 sec, value, &old_bfd, &dynsym))
5022 goto error_free_vers;
5023
5024 /* Check the alignment when a common symbol is involved. This
5025 can change when a common symbol is overridden by a normal
5026 definition or a common symbol is ignored due to the old
5027 normal definition. We need to make sure the maximum
5028 alignment is maintained. */
5029 if ((old_alignment || common)
5030 && h->root.type != bfd_link_hash_common)
5031 {
5032 unsigned int common_align;
5033 unsigned int normal_align;
5034 unsigned int symbol_align;
5035 bfd *normal_bfd;
5036 bfd *common_bfd;
5037
5038 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5039 || h->root.type == bfd_link_hash_defweak);
5040
5041 symbol_align = ffs (h->root.u.def.value) - 1;
5042 if (h->root.u.def.section->owner != NULL
5043 && (h->root.u.def.section->owner->flags
5044 & (DYNAMIC | BFD_PLUGIN)) == 0)
5045 {
5046 normal_align = h->root.u.def.section->alignment_power;
5047 if (normal_align > symbol_align)
5048 normal_align = symbol_align;
5049 }
5050 else
5051 normal_align = symbol_align;
5052
5053 if (old_alignment)
5054 {
5055 common_align = old_alignment;
5056 common_bfd = old_bfd;
5057 normal_bfd = abfd;
5058 }
5059 else
5060 {
5061 common_align = bfd_log2 (isym->st_value);
5062 common_bfd = abfd;
5063 normal_bfd = old_bfd;
5064 }
5065
5066 if (normal_align < common_align)
5067 {
5068 /* PR binutils/2735 */
5069 if (normal_bfd == NULL)
5070 _bfd_error_handler
5071 /* xgettext:c-format */
5072 (_("warning: alignment %u of common symbol `%s' in %pB is"
5073 " greater than the alignment (%u) of its section %pA"),
5074 1 << common_align, name, common_bfd,
5075 1 << normal_align, h->root.u.def.section);
5076 else
5077 _bfd_error_handler
5078 /* xgettext:c-format */
5079 (_("warning: alignment %u of symbol `%s' in %pB"
5080 " is smaller than %u in %pB"),
5081 1 << normal_align, name, normal_bfd,
5082 1 << common_align, common_bfd);
5083 }
5084 }
5085
5086 /* Remember the symbol size if it isn't undefined. */
5087 if (isym->st_size != 0
5088 && isym->st_shndx != SHN_UNDEF
5089 && (definition || h->size == 0))
5090 {
5091 if (h->size != 0
5092 && h->size != isym->st_size
5093 && ! size_change_ok)
5094 _bfd_error_handler
5095 /* xgettext:c-format */
5096 (_("warning: size of symbol `%s' changed"
5097 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5098 name, (uint64_t) h->size, old_bfd,
5099 (uint64_t) isym->st_size, abfd);
5100
5101 h->size = isym->st_size;
5102 }
5103
5104 /* If this is a common symbol, then we always want H->SIZE
5105 to be the size of the common symbol. The code just above
5106 won't fix the size if a common symbol becomes larger. We
5107 don't warn about a size change here, because that is
5108 covered by --warn-common. Allow changes between different
5109 function types. */
5110 if (h->root.type == bfd_link_hash_common)
5111 h->size = h->root.u.c.size;
5112
5113 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5114 && ((definition && !new_weak)
5115 || (old_weak && h->root.type == bfd_link_hash_common)
5116 || h->type == STT_NOTYPE))
5117 {
5118 unsigned int type = ELF_ST_TYPE (isym->st_info);
5119
5120 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5121 symbol. */
5122 if (type == STT_GNU_IFUNC
5123 && (abfd->flags & DYNAMIC) != 0)
5124 type = STT_FUNC;
5125
5126 if (h->type != type)
5127 {
5128 if (h->type != STT_NOTYPE && ! type_change_ok)
5129 /* xgettext:c-format */
5130 _bfd_error_handler
5131 (_("warning: type of symbol `%s' changed"
5132 " from %d to %d in %pB"),
5133 name, h->type, type, abfd);
5134
5135 h->type = type;
5136 }
5137 }
5138
5139 /* Merge st_other field. */
5140 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5141
5142 /* We don't want to make debug symbol dynamic. */
5143 if (definition
5144 && (sec->flags & SEC_DEBUGGING)
5145 && !bfd_link_relocatable (info))
5146 dynsym = FALSE;
5147
5148 /* Nor should we make plugin symbols dynamic. */
5149 if ((abfd->flags & BFD_PLUGIN) != 0)
5150 dynsym = FALSE;
5151
5152 if (definition)
5153 {
5154 h->target_internal = isym->st_target_internal;
5155 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5156 }
5157
5158 if (definition && !dynamic)
5159 {
5160 char *p = strchr (name, ELF_VER_CHR);
5161 if (p != NULL && p[1] != ELF_VER_CHR)
5162 {
5163 /* Queue non-default versions so that .symver x, x@FOO
5164 aliases can be checked. */
5165 if (!nondeflt_vers)
5166 {
5167 size_t amt = ((isymend - isym + 1)
5168 * sizeof (struct elf_link_hash_entry *));
5169 nondeflt_vers
5170 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5171 if (!nondeflt_vers)
5172 goto error_free_vers;
5173 }
5174 nondeflt_vers[nondeflt_vers_cnt++] = h;
5175 }
5176 }
5177
5178 if (dynsym && h->dynindx == -1)
5179 {
5180 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5181 goto error_free_vers;
5182 if (h->is_weakalias
5183 && weakdef (h)->dynindx == -1)
5184 {
5185 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5186 goto error_free_vers;
5187 }
5188 }
5189 else if (h->dynindx != -1)
5190 /* If the symbol already has a dynamic index, but
5191 visibility says it should not be visible, turn it into
5192 a local symbol. */
5193 switch (ELF_ST_VISIBILITY (h->other))
5194 {
5195 case STV_INTERNAL:
5196 case STV_HIDDEN:
5197 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5198 dynsym = FALSE;
5199 break;
5200 }
5201
5202 /* Don't add DT_NEEDED for references from the dummy bfd nor
5203 for unmatched symbol. */
5204 if (!add_needed
5205 && matched
5206 && definition
5207 && ((dynsym
5208 && h->ref_regular_nonweak
5209 && (old_bfd == NULL
5210 || (old_bfd->flags & BFD_PLUGIN) == 0))
5211 || (h->ref_dynamic_nonweak
5212 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5213 && !on_needed_list (elf_dt_name (abfd),
5214 htab->needed, NULL))))
5215 {
5216 const char *soname = elf_dt_name (abfd);
5217
5218 info->callbacks->minfo ("%!", soname, old_bfd,
5219 h->root.root.string);
5220
5221 /* A symbol from a library loaded via DT_NEEDED of some
5222 other library is referenced by a regular object.
5223 Add a DT_NEEDED entry for it. Issue an error if
5224 --no-add-needed is used and the reference was not
5225 a weak one. */
5226 if (old_bfd != NULL
5227 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5228 {
5229 _bfd_error_handler
5230 /* xgettext:c-format */
5231 (_("%pB: undefined reference to symbol '%s'"),
5232 old_bfd, name);
5233 bfd_set_error (bfd_error_missing_dso);
5234 goto error_free_vers;
5235 }
5236
5237 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5238 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5239
5240 /* Create dynamic sections for backends that require
5241 that be done before setup_gnu_properties. */
5242 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5243 return FALSE;
5244 add_needed = TRUE;
5245 }
5246 }
5247 }
5248
5249 if (info->lto_plugin_active
5250 && !bfd_link_relocatable (info)
5251 && (abfd->flags & BFD_PLUGIN) == 0
5252 && !just_syms
5253 && extsymcount)
5254 {
5255 int r_sym_shift;
5256
5257 if (bed->s->arch_size == 32)
5258 r_sym_shift = 8;
5259 else
5260 r_sym_shift = 32;
5261
5262 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5263 referenced in regular objects so that linker plugin will get
5264 the correct symbol resolution. */
5265
5266 sym_hash = elf_sym_hashes (abfd);
5267 for (s = abfd->sections; s != NULL; s = s->next)
5268 {
5269 Elf_Internal_Rela *internal_relocs;
5270 Elf_Internal_Rela *rel, *relend;
5271
5272 /* Don't check relocations in excluded sections. */
5273 if ((s->flags & SEC_RELOC) == 0
5274 || s->reloc_count == 0
5275 || (s->flags & SEC_EXCLUDE) != 0
5276 || ((info->strip == strip_all
5277 || info->strip == strip_debugger)
5278 && (s->flags & SEC_DEBUGGING) != 0))
5279 continue;
5280
5281 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5282 NULL,
5283 info->keep_memory);
5284 if (internal_relocs == NULL)
5285 goto error_free_vers;
5286
5287 rel = internal_relocs;
5288 relend = rel + s->reloc_count;
5289 for ( ; rel < relend; rel++)
5290 {
5291 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5292 struct elf_link_hash_entry *h;
5293
5294 /* Skip local symbols. */
5295 if (r_symndx < extsymoff)
5296 continue;
5297
5298 h = sym_hash[r_symndx - extsymoff];
5299 if (h != NULL)
5300 h->root.non_ir_ref_regular = 1;
5301 }
5302
5303 if (elf_section_data (s)->relocs != internal_relocs)
5304 free (internal_relocs);
5305 }
5306 }
5307
5308 free (extversym);
5309 extversym = NULL;
5310 free (isymbuf);
5311 isymbuf = NULL;
5312
5313 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5314 {
5315 unsigned int i;
5316
5317 /* Restore the symbol table. */
5318 old_ent = (char *) old_tab + tabsize;
5319 memset (elf_sym_hashes (abfd), 0,
5320 extsymcount * sizeof (struct elf_link_hash_entry *));
5321 htab->root.table.table = old_table;
5322 htab->root.table.size = old_size;
5323 htab->root.table.count = old_count;
5324 memcpy (htab->root.table.table, old_tab, tabsize);
5325 htab->root.undefs = old_undefs;
5326 htab->root.undefs_tail = old_undefs_tail;
5327 if (htab->dynstr != NULL)
5328 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5329 free (old_strtab);
5330 old_strtab = NULL;
5331 for (i = 0; i < htab->root.table.size; i++)
5332 {
5333 struct bfd_hash_entry *p;
5334 struct elf_link_hash_entry *h;
5335 bfd_size_type size;
5336 unsigned int alignment_power;
5337 unsigned int non_ir_ref_dynamic;
5338
5339 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5340 {
5341 h = (struct elf_link_hash_entry *) p;
5342 if (h->root.type == bfd_link_hash_warning)
5343 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5344
5345 /* Preserve the maximum alignment and size for common
5346 symbols even if this dynamic lib isn't on DT_NEEDED
5347 since it can still be loaded at run time by another
5348 dynamic lib. */
5349 if (h->root.type == bfd_link_hash_common)
5350 {
5351 size = h->root.u.c.size;
5352 alignment_power = h->root.u.c.p->alignment_power;
5353 }
5354 else
5355 {
5356 size = 0;
5357 alignment_power = 0;
5358 }
5359 /* Preserve non_ir_ref_dynamic so that this symbol
5360 will be exported when the dynamic lib becomes needed
5361 in the second pass. */
5362 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5363 memcpy (p, old_ent, htab->root.table.entsize);
5364 old_ent = (char *) old_ent + htab->root.table.entsize;
5365 h = (struct elf_link_hash_entry *) p;
5366 if (h->root.type == bfd_link_hash_warning)
5367 {
5368 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5369 old_ent = (char *) old_ent + htab->root.table.entsize;
5370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5371 }
5372 if (h->root.type == bfd_link_hash_common)
5373 {
5374 if (size > h->root.u.c.size)
5375 h->root.u.c.size = size;
5376 if (alignment_power > h->root.u.c.p->alignment_power)
5377 h->root.u.c.p->alignment_power = alignment_power;
5378 }
5379 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5380 }
5381 }
5382
5383 /* Make a special call to the linker "notice" function to
5384 tell it that symbols added for crefs may need to be removed. */
5385 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5386 goto error_free_vers;
5387
5388 free (old_tab);
5389 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5390 alloc_mark);
5391 free (nondeflt_vers);
5392 return TRUE;
5393 }
5394
5395 if (old_tab != NULL)
5396 {
5397 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5398 goto error_free_vers;
5399 free (old_tab);
5400 old_tab = NULL;
5401 }
5402
5403 /* Now that all the symbols from this input file are created, if
5404 not performing a relocatable link, handle .symver foo, foo@BAR
5405 such that any relocs against foo become foo@BAR. */
5406 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5407 {
5408 size_t cnt, symidx;
5409
5410 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5411 {
5412 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5413 char *shortname, *p;
5414 size_t amt;
5415
5416 p = strchr (h->root.root.string, ELF_VER_CHR);
5417 if (p == NULL
5418 || (h->root.type != bfd_link_hash_defined
5419 && h->root.type != bfd_link_hash_defweak))
5420 continue;
5421
5422 amt = p - h->root.root.string;
5423 shortname = (char *) bfd_malloc (amt + 1);
5424 if (!shortname)
5425 goto error_free_vers;
5426 memcpy (shortname, h->root.root.string, amt);
5427 shortname[amt] = '\0';
5428
5429 hi = (struct elf_link_hash_entry *)
5430 bfd_link_hash_lookup (&htab->root, shortname,
5431 FALSE, FALSE, FALSE);
5432 if (hi != NULL
5433 && hi->root.type == h->root.type
5434 && hi->root.u.def.value == h->root.u.def.value
5435 && hi->root.u.def.section == h->root.u.def.section)
5436 {
5437 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5438 hi->root.type = bfd_link_hash_indirect;
5439 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5440 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5441 sym_hash = elf_sym_hashes (abfd);
5442 if (sym_hash)
5443 for (symidx = 0; symidx < extsymcount; ++symidx)
5444 if (sym_hash[symidx] == hi)
5445 {
5446 sym_hash[symidx] = h;
5447 break;
5448 }
5449 }
5450 free (shortname);
5451 }
5452 free (nondeflt_vers);
5453 nondeflt_vers = NULL;
5454 }
5455
5456 /* Now set the alias field correctly for all the weak defined
5457 symbols we found. The only way to do this is to search all the
5458 symbols. Since we only need the information for non functions in
5459 dynamic objects, that's the only time we actually put anything on
5460 the list WEAKS. We need this information so that if a regular
5461 object refers to a symbol defined weakly in a dynamic object, the
5462 real symbol in the dynamic object is also put in the dynamic
5463 symbols; we also must arrange for both symbols to point to the
5464 same memory location. We could handle the general case of symbol
5465 aliasing, but a general symbol alias can only be generated in
5466 assembler code, handling it correctly would be very time
5467 consuming, and other ELF linkers don't handle general aliasing
5468 either. */
5469 if (weaks != NULL)
5470 {
5471 struct elf_link_hash_entry **hpp;
5472 struct elf_link_hash_entry **hppend;
5473 struct elf_link_hash_entry **sorted_sym_hash;
5474 struct elf_link_hash_entry *h;
5475 size_t sym_count, amt;
5476
5477 /* Since we have to search the whole symbol list for each weak
5478 defined symbol, search time for N weak defined symbols will be
5479 O(N^2). Binary search will cut it down to O(NlogN). */
5480 amt = extsymcount * sizeof (*sorted_sym_hash);
5481 sorted_sym_hash = bfd_malloc (amt);
5482 if (sorted_sym_hash == NULL)
5483 goto error_return;
5484 sym_hash = sorted_sym_hash;
5485 hpp = elf_sym_hashes (abfd);
5486 hppend = hpp + extsymcount;
5487 sym_count = 0;
5488 for (; hpp < hppend; hpp++)
5489 {
5490 h = *hpp;
5491 if (h != NULL
5492 && h->root.type == bfd_link_hash_defined
5493 && !bed->is_function_type (h->type))
5494 {
5495 *sym_hash = h;
5496 sym_hash++;
5497 sym_count++;
5498 }
5499 }
5500
5501 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5502 elf_sort_symbol);
5503
5504 while (weaks != NULL)
5505 {
5506 struct elf_link_hash_entry *hlook;
5507 asection *slook;
5508 bfd_vma vlook;
5509 size_t i, j, idx = 0;
5510
5511 hlook = weaks;
5512 weaks = hlook->u.alias;
5513 hlook->u.alias = NULL;
5514
5515 if (hlook->root.type != bfd_link_hash_defined
5516 && hlook->root.type != bfd_link_hash_defweak)
5517 continue;
5518
5519 slook = hlook->root.u.def.section;
5520 vlook = hlook->root.u.def.value;
5521
5522 i = 0;
5523 j = sym_count;
5524 while (i != j)
5525 {
5526 bfd_signed_vma vdiff;
5527 idx = (i + j) / 2;
5528 h = sorted_sym_hash[idx];
5529 vdiff = vlook - h->root.u.def.value;
5530 if (vdiff < 0)
5531 j = idx;
5532 else if (vdiff > 0)
5533 i = idx + 1;
5534 else
5535 {
5536 int sdiff = slook->id - h->root.u.def.section->id;
5537 if (sdiff < 0)
5538 j = idx;
5539 else if (sdiff > 0)
5540 i = idx + 1;
5541 else
5542 break;
5543 }
5544 }
5545
5546 /* We didn't find a value/section match. */
5547 if (i == j)
5548 continue;
5549
5550 /* With multiple aliases, or when the weak symbol is already
5551 strongly defined, we have multiple matching symbols and
5552 the binary search above may land on any of them. Step
5553 one past the matching symbol(s). */
5554 while (++idx != j)
5555 {
5556 h = sorted_sym_hash[idx];
5557 if (h->root.u.def.section != slook
5558 || h->root.u.def.value != vlook)
5559 break;
5560 }
5561
5562 /* Now look back over the aliases. Since we sorted by size
5563 as well as value and section, we'll choose the one with
5564 the largest size. */
5565 while (idx-- != i)
5566 {
5567 h = sorted_sym_hash[idx];
5568
5569 /* Stop if value or section doesn't match. */
5570 if (h->root.u.def.section != slook
5571 || h->root.u.def.value != vlook)
5572 break;
5573 else if (h != hlook)
5574 {
5575 struct elf_link_hash_entry *t;
5576
5577 hlook->u.alias = h;
5578 hlook->is_weakalias = 1;
5579 t = h;
5580 if (t->u.alias != NULL)
5581 while (t->u.alias != h)
5582 t = t->u.alias;
5583 t->u.alias = hlook;
5584
5585 /* If the weak definition is in the list of dynamic
5586 symbols, make sure the real definition is put
5587 there as well. */
5588 if (hlook->dynindx != -1 && h->dynindx == -1)
5589 {
5590 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5591 {
5592 err_free_sym_hash:
5593 free (sorted_sym_hash);
5594 goto error_return;
5595 }
5596 }
5597
5598 /* If the real definition is in the list of dynamic
5599 symbols, make sure the weak definition is put
5600 there as well. If we don't do this, then the
5601 dynamic loader might not merge the entries for the
5602 real definition and the weak definition. */
5603 if (h->dynindx != -1 && hlook->dynindx == -1)
5604 {
5605 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5606 goto err_free_sym_hash;
5607 }
5608 break;
5609 }
5610 }
5611 }
5612
5613 free (sorted_sym_hash);
5614 }
5615
5616 if (bed->check_directives
5617 && !(*bed->check_directives) (abfd, info))
5618 return FALSE;
5619
5620 /* If this is a non-traditional link, try to optimize the handling
5621 of the .stab/.stabstr sections. */
5622 if (! dynamic
5623 && ! info->traditional_format
5624 && is_elf_hash_table (htab)
5625 && (info->strip != strip_all && info->strip != strip_debugger))
5626 {
5627 asection *stabstr;
5628
5629 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5630 if (stabstr != NULL)
5631 {
5632 bfd_size_type string_offset = 0;
5633 asection *stab;
5634
5635 for (stab = abfd->sections; stab; stab = stab->next)
5636 if (CONST_STRNEQ (stab->name, ".stab")
5637 && (!stab->name[5] ||
5638 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5639 && (stab->flags & SEC_MERGE) == 0
5640 && !bfd_is_abs_section (stab->output_section))
5641 {
5642 struct bfd_elf_section_data *secdata;
5643
5644 secdata = elf_section_data (stab);
5645 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5646 stabstr, &secdata->sec_info,
5647 &string_offset))
5648 goto error_return;
5649 if (secdata->sec_info)
5650 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5651 }
5652 }
5653 }
5654
5655 if (dynamic && add_needed)
5656 {
5657 /* Add this bfd to the loaded list. */
5658 struct elf_link_loaded_list *n;
5659
5660 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5661 if (n == NULL)
5662 goto error_return;
5663 n->abfd = abfd;
5664 n->next = htab->dyn_loaded;
5665 htab->dyn_loaded = n;
5666 }
5667 if (dynamic && !add_needed
5668 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5669 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5670
5671 return TRUE;
5672
5673 error_free_vers:
5674 free (old_tab);
5675 free (old_strtab);
5676 free (nondeflt_vers);
5677 free (extversym);
5678 error_free_sym:
5679 free (isymbuf);
5680 error_return:
5681 return FALSE;
5682 }
5683
5684 /* Return the linker hash table entry of a symbol that might be
5685 satisfied by an archive symbol. Return -1 on error. */
5686
5687 struct elf_link_hash_entry *
5688 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5689 struct bfd_link_info *info,
5690 const char *name)
5691 {
5692 struct elf_link_hash_entry *h;
5693 char *p, *copy;
5694 size_t len, first;
5695
5696 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5697 if (h != NULL)
5698 return h;
5699
5700 /* If this is a default version (the name contains @@), look up the
5701 symbol again with only one `@' as well as without the version.
5702 The effect is that references to the symbol with and without the
5703 version will be matched by the default symbol in the archive. */
5704
5705 p = strchr (name, ELF_VER_CHR);
5706 if (p == NULL || p[1] != ELF_VER_CHR)
5707 return h;
5708
5709 /* First check with only one `@'. */
5710 len = strlen (name);
5711 copy = (char *) bfd_alloc (abfd, len);
5712 if (copy == NULL)
5713 return (struct elf_link_hash_entry *) -1;
5714
5715 first = p - name + 1;
5716 memcpy (copy, name, first);
5717 memcpy (copy + first, name + first + 1, len - first);
5718
5719 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5720 if (h == NULL)
5721 {
5722 /* We also need to check references to the symbol without the
5723 version. */
5724 copy[first - 1] = '\0';
5725 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5726 FALSE, FALSE, TRUE);
5727 }
5728
5729 bfd_release (abfd, copy);
5730 return h;
5731 }
5732
5733 /* Add symbols from an ELF archive file to the linker hash table. We
5734 don't use _bfd_generic_link_add_archive_symbols because we need to
5735 handle versioned symbols.
5736
5737 Fortunately, ELF archive handling is simpler than that done by
5738 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5739 oddities. In ELF, if we find a symbol in the archive map, and the
5740 symbol is currently undefined, we know that we must pull in that
5741 object file.
5742
5743 Unfortunately, we do have to make multiple passes over the symbol
5744 table until nothing further is resolved. */
5745
5746 static bfd_boolean
5747 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5748 {
5749 symindex c;
5750 unsigned char *included = NULL;
5751 carsym *symdefs;
5752 bfd_boolean loop;
5753 size_t amt;
5754 const struct elf_backend_data *bed;
5755 struct elf_link_hash_entry * (*archive_symbol_lookup)
5756 (bfd *, struct bfd_link_info *, const char *);
5757
5758 if (! bfd_has_map (abfd))
5759 {
5760 /* An empty archive is a special case. */
5761 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5762 return TRUE;
5763 bfd_set_error (bfd_error_no_armap);
5764 return FALSE;
5765 }
5766
5767 /* Keep track of all symbols we know to be already defined, and all
5768 files we know to be already included. This is to speed up the
5769 second and subsequent passes. */
5770 c = bfd_ardata (abfd)->symdef_count;
5771 if (c == 0)
5772 return TRUE;
5773 amt = c * sizeof (*included);
5774 included = (unsigned char *) bfd_zmalloc (amt);
5775 if (included == NULL)
5776 return FALSE;
5777
5778 symdefs = bfd_ardata (abfd)->symdefs;
5779 bed = get_elf_backend_data (abfd);
5780 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5781
5782 do
5783 {
5784 file_ptr last;
5785 symindex i;
5786 carsym *symdef;
5787 carsym *symdefend;
5788
5789 loop = FALSE;
5790 last = -1;
5791
5792 symdef = symdefs;
5793 symdefend = symdef + c;
5794 for (i = 0; symdef < symdefend; symdef++, i++)
5795 {
5796 struct elf_link_hash_entry *h;
5797 bfd *element;
5798 struct bfd_link_hash_entry *undefs_tail;
5799 symindex mark;
5800
5801 if (included[i])
5802 continue;
5803 if (symdef->file_offset == last)
5804 {
5805 included[i] = TRUE;
5806 continue;
5807 }
5808
5809 h = archive_symbol_lookup (abfd, info, symdef->name);
5810 if (h == (struct elf_link_hash_entry *) -1)
5811 goto error_return;
5812
5813 if (h == NULL)
5814 continue;
5815
5816 if (h->root.type == bfd_link_hash_common)
5817 {
5818 /* We currently have a common symbol. The archive map contains
5819 a reference to this symbol, so we may want to include it. We
5820 only want to include it however, if this archive element
5821 contains a definition of the symbol, not just another common
5822 declaration of it.
5823
5824 Unfortunately some archivers (including GNU ar) will put
5825 declarations of common symbols into their archive maps, as
5826 well as real definitions, so we cannot just go by the archive
5827 map alone. Instead we must read in the element's symbol
5828 table and check that to see what kind of symbol definition
5829 this is. */
5830 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5831 continue;
5832 }
5833 else if (h->root.type != bfd_link_hash_undefined)
5834 {
5835 if (h->root.type != bfd_link_hash_undefweak)
5836 /* Symbol must be defined. Don't check it again. */
5837 included[i] = TRUE;
5838 continue;
5839 }
5840
5841 /* We need to include this archive member. */
5842 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5843 if (element == NULL)
5844 goto error_return;
5845
5846 if (! bfd_check_format (element, bfd_object))
5847 goto error_return;
5848
5849 undefs_tail = info->hash->undefs_tail;
5850
5851 if (!(*info->callbacks
5852 ->add_archive_element) (info, element, symdef->name, &element))
5853 continue;
5854 if (!bfd_link_add_symbols (element, info))
5855 goto error_return;
5856
5857 /* If there are any new undefined symbols, we need to make
5858 another pass through the archive in order to see whether
5859 they can be defined. FIXME: This isn't perfect, because
5860 common symbols wind up on undefs_tail and because an
5861 undefined symbol which is defined later on in this pass
5862 does not require another pass. This isn't a bug, but it
5863 does make the code less efficient than it could be. */
5864 if (undefs_tail != info->hash->undefs_tail)
5865 loop = TRUE;
5866
5867 /* Look backward to mark all symbols from this object file
5868 which we have already seen in this pass. */
5869 mark = i;
5870 do
5871 {
5872 included[mark] = TRUE;
5873 if (mark == 0)
5874 break;
5875 --mark;
5876 }
5877 while (symdefs[mark].file_offset == symdef->file_offset);
5878
5879 /* We mark subsequent symbols from this object file as we go
5880 on through the loop. */
5881 last = symdef->file_offset;
5882 }
5883 }
5884 while (loop);
5885
5886 free (included);
5887 return TRUE;
5888
5889 error_return:
5890 free (included);
5891 return FALSE;
5892 }
5893
5894 /* Given an ELF BFD, add symbols to the global hash table as
5895 appropriate. */
5896
5897 bfd_boolean
5898 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5899 {
5900 switch (bfd_get_format (abfd))
5901 {
5902 case bfd_object:
5903 return elf_link_add_object_symbols (abfd, info);
5904 case bfd_archive:
5905 return elf_link_add_archive_symbols (abfd, info);
5906 default:
5907 bfd_set_error (bfd_error_wrong_format);
5908 return FALSE;
5909 }
5910 }
5911 \f
5912 struct hash_codes_info
5913 {
5914 unsigned long *hashcodes;
5915 bfd_boolean error;
5916 };
5917
5918 /* This function will be called though elf_link_hash_traverse to store
5919 all hash value of the exported symbols in an array. */
5920
5921 static bfd_boolean
5922 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5923 {
5924 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5925 const char *name;
5926 unsigned long ha;
5927 char *alc = NULL;
5928
5929 /* Ignore indirect symbols. These are added by the versioning code. */
5930 if (h->dynindx == -1)
5931 return TRUE;
5932
5933 name = h->root.root.string;
5934 if (h->versioned >= versioned)
5935 {
5936 char *p = strchr (name, ELF_VER_CHR);
5937 if (p != NULL)
5938 {
5939 alc = (char *) bfd_malloc (p - name + 1);
5940 if (alc == NULL)
5941 {
5942 inf->error = TRUE;
5943 return FALSE;
5944 }
5945 memcpy (alc, name, p - name);
5946 alc[p - name] = '\0';
5947 name = alc;
5948 }
5949 }
5950
5951 /* Compute the hash value. */
5952 ha = bfd_elf_hash (name);
5953
5954 /* Store the found hash value in the array given as the argument. */
5955 *(inf->hashcodes)++ = ha;
5956
5957 /* And store it in the struct so that we can put it in the hash table
5958 later. */
5959 h->u.elf_hash_value = ha;
5960
5961 free (alc);
5962 return TRUE;
5963 }
5964
5965 struct collect_gnu_hash_codes
5966 {
5967 bfd *output_bfd;
5968 const struct elf_backend_data *bed;
5969 unsigned long int nsyms;
5970 unsigned long int maskbits;
5971 unsigned long int *hashcodes;
5972 unsigned long int *hashval;
5973 unsigned long int *indx;
5974 unsigned long int *counts;
5975 bfd_vma *bitmask;
5976 bfd_byte *contents;
5977 bfd_size_type xlat;
5978 long int min_dynindx;
5979 unsigned long int bucketcount;
5980 unsigned long int symindx;
5981 long int local_indx;
5982 long int shift1, shift2;
5983 unsigned long int mask;
5984 bfd_boolean error;
5985 };
5986
5987 /* This function will be called though elf_link_hash_traverse to store
5988 all hash value of the exported symbols in an array. */
5989
5990 static bfd_boolean
5991 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5992 {
5993 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5994 const char *name;
5995 unsigned long ha;
5996 char *alc = NULL;
5997
5998 /* Ignore indirect symbols. These are added by the versioning code. */
5999 if (h->dynindx == -1)
6000 return TRUE;
6001
6002 /* Ignore also local symbols and undefined symbols. */
6003 if (! (*s->bed->elf_hash_symbol) (h))
6004 return TRUE;
6005
6006 name = h->root.root.string;
6007 if (h->versioned >= versioned)
6008 {
6009 char *p = strchr (name, ELF_VER_CHR);
6010 if (p != NULL)
6011 {
6012 alc = (char *) bfd_malloc (p - name + 1);
6013 if (alc == NULL)
6014 {
6015 s->error = TRUE;
6016 return FALSE;
6017 }
6018 memcpy (alc, name, p - name);
6019 alc[p - name] = '\0';
6020 name = alc;
6021 }
6022 }
6023
6024 /* Compute the hash value. */
6025 ha = bfd_elf_gnu_hash (name);
6026
6027 /* Store the found hash value in the array for compute_bucket_count,
6028 and also for .dynsym reordering purposes. */
6029 s->hashcodes[s->nsyms] = ha;
6030 s->hashval[h->dynindx] = ha;
6031 ++s->nsyms;
6032 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6033 s->min_dynindx = h->dynindx;
6034
6035 free (alc);
6036 return TRUE;
6037 }
6038
6039 /* This function will be called though elf_link_hash_traverse to do
6040 final dynamic symbol renumbering in case of .gnu.hash.
6041 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6042 to the translation table. */
6043
6044 static bfd_boolean
6045 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6046 {
6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6048 unsigned long int bucket;
6049 unsigned long int val;
6050
6051 /* Ignore indirect symbols. */
6052 if (h->dynindx == -1)
6053 return TRUE;
6054
6055 /* Ignore also local symbols and undefined symbols. */
6056 if (! (*s->bed->elf_hash_symbol) (h))
6057 {
6058 if (h->dynindx >= s->min_dynindx)
6059 {
6060 if (s->bed->record_xhash_symbol != NULL)
6061 {
6062 (*s->bed->record_xhash_symbol) (h, 0);
6063 s->local_indx++;
6064 }
6065 else
6066 h->dynindx = s->local_indx++;
6067 }
6068 return TRUE;
6069 }
6070
6071 bucket = s->hashval[h->dynindx] % s->bucketcount;
6072 val = (s->hashval[h->dynindx] >> s->shift1)
6073 & ((s->maskbits >> s->shift1) - 1);
6074 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6075 s->bitmask[val]
6076 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6077 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6078 if (s->counts[bucket] == 1)
6079 /* Last element terminates the chain. */
6080 val |= 1;
6081 bfd_put_32 (s->output_bfd, val,
6082 s->contents + (s->indx[bucket] - s->symindx) * 4);
6083 --s->counts[bucket];
6084 if (s->bed->record_xhash_symbol != NULL)
6085 {
6086 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6087
6088 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6089 }
6090 else
6091 h->dynindx = s->indx[bucket]++;
6092 return TRUE;
6093 }
6094
6095 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6096
6097 bfd_boolean
6098 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6099 {
6100 return !(h->forced_local
6101 || h->root.type == bfd_link_hash_undefined
6102 || h->root.type == bfd_link_hash_undefweak
6103 || ((h->root.type == bfd_link_hash_defined
6104 || h->root.type == bfd_link_hash_defweak)
6105 && h->root.u.def.section->output_section == NULL));
6106 }
6107
6108 /* Array used to determine the number of hash table buckets to use
6109 based on the number of symbols there are. If there are fewer than
6110 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6111 fewer than 37 we use 17 buckets, and so forth. We never use more
6112 than 32771 buckets. */
6113
6114 static const size_t elf_buckets[] =
6115 {
6116 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6117 16411, 32771, 0
6118 };
6119
6120 /* Compute bucket count for hashing table. We do not use a static set
6121 of possible tables sizes anymore. Instead we determine for all
6122 possible reasonable sizes of the table the outcome (i.e., the
6123 number of collisions etc) and choose the best solution. The
6124 weighting functions are not too simple to allow the table to grow
6125 without bounds. Instead one of the weighting factors is the size.
6126 Therefore the result is always a good payoff between few collisions
6127 (= short chain lengths) and table size. */
6128 static size_t
6129 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6130 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6131 unsigned long int nsyms,
6132 int gnu_hash)
6133 {
6134 size_t best_size = 0;
6135 unsigned long int i;
6136
6137 /* We have a problem here. The following code to optimize the table
6138 size requires an integer type with more the 32 bits. If
6139 BFD_HOST_U_64_BIT is set we know about such a type. */
6140 #ifdef BFD_HOST_U_64_BIT
6141 if (info->optimize)
6142 {
6143 size_t minsize;
6144 size_t maxsize;
6145 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6146 bfd *dynobj = elf_hash_table (info)->dynobj;
6147 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6148 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6149 unsigned long int *counts;
6150 bfd_size_type amt;
6151 unsigned int no_improvement_count = 0;
6152
6153 /* Possible optimization parameters: if we have NSYMS symbols we say
6154 that the hashing table must at least have NSYMS/4 and at most
6155 2*NSYMS buckets. */
6156 minsize = nsyms / 4;
6157 if (minsize == 0)
6158 minsize = 1;
6159 best_size = maxsize = nsyms * 2;
6160 if (gnu_hash)
6161 {
6162 if (minsize < 2)
6163 minsize = 2;
6164 if ((best_size & 31) == 0)
6165 ++best_size;
6166 }
6167
6168 /* Create array where we count the collisions in. We must use bfd_malloc
6169 since the size could be large. */
6170 amt = maxsize;
6171 amt *= sizeof (unsigned long int);
6172 counts = (unsigned long int *) bfd_malloc (amt);
6173 if (counts == NULL)
6174 return 0;
6175
6176 /* Compute the "optimal" size for the hash table. The criteria is a
6177 minimal chain length. The minor criteria is (of course) the size
6178 of the table. */
6179 for (i = minsize; i < maxsize; ++i)
6180 {
6181 /* Walk through the array of hashcodes and count the collisions. */
6182 BFD_HOST_U_64_BIT max;
6183 unsigned long int j;
6184 unsigned long int fact;
6185
6186 if (gnu_hash && (i & 31) == 0)
6187 continue;
6188
6189 memset (counts, '\0', i * sizeof (unsigned long int));
6190
6191 /* Determine how often each hash bucket is used. */
6192 for (j = 0; j < nsyms; ++j)
6193 ++counts[hashcodes[j] % i];
6194
6195 /* For the weight function we need some information about the
6196 pagesize on the target. This is information need not be 100%
6197 accurate. Since this information is not available (so far) we
6198 define it here to a reasonable default value. If it is crucial
6199 to have a better value some day simply define this value. */
6200 # ifndef BFD_TARGET_PAGESIZE
6201 # define BFD_TARGET_PAGESIZE (4096)
6202 # endif
6203
6204 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6205 and the chains. */
6206 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6207
6208 # if 1
6209 /* Variant 1: optimize for short chains. We add the squares
6210 of all the chain lengths (which favors many small chain
6211 over a few long chains). */
6212 for (j = 0; j < i; ++j)
6213 max += counts[j] * counts[j];
6214
6215 /* This adds penalties for the overall size of the table. */
6216 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6217 max *= fact * fact;
6218 # else
6219 /* Variant 2: Optimize a lot more for small table. Here we
6220 also add squares of the size but we also add penalties for
6221 empty slots (the +1 term). */
6222 for (j = 0; j < i; ++j)
6223 max += (1 + counts[j]) * (1 + counts[j]);
6224
6225 /* The overall size of the table is considered, but not as
6226 strong as in variant 1, where it is squared. */
6227 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6228 max *= fact;
6229 # endif
6230
6231 /* Compare with current best results. */
6232 if (max < best_chlen)
6233 {
6234 best_chlen = max;
6235 best_size = i;
6236 no_improvement_count = 0;
6237 }
6238 /* PR 11843: Avoid futile long searches for the best bucket size
6239 when there are a large number of symbols. */
6240 else if (++no_improvement_count == 100)
6241 break;
6242 }
6243
6244 free (counts);
6245 }
6246 else
6247 #endif /* defined (BFD_HOST_U_64_BIT) */
6248 {
6249 /* This is the fallback solution if no 64bit type is available or if we
6250 are not supposed to spend much time on optimizations. We select the
6251 bucket count using a fixed set of numbers. */
6252 for (i = 0; elf_buckets[i] != 0; i++)
6253 {
6254 best_size = elf_buckets[i];
6255 if (nsyms < elf_buckets[i + 1])
6256 break;
6257 }
6258 if (gnu_hash && best_size < 2)
6259 best_size = 2;
6260 }
6261
6262 return best_size;
6263 }
6264
6265 /* Size any SHT_GROUP section for ld -r. */
6266
6267 bfd_boolean
6268 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6269 {
6270 bfd *ibfd;
6271 asection *s;
6272
6273 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6274 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6275 && (s = ibfd->sections) != NULL
6276 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6277 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6278 return FALSE;
6279 return TRUE;
6280 }
6281
6282 /* Set a default stack segment size. The value in INFO wins. If it
6283 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6284 undefined it is initialized. */
6285
6286 bfd_boolean
6287 bfd_elf_stack_segment_size (bfd *output_bfd,
6288 struct bfd_link_info *info,
6289 const char *legacy_symbol,
6290 bfd_vma default_size)
6291 {
6292 struct elf_link_hash_entry *h = NULL;
6293
6294 /* Look for legacy symbol. */
6295 if (legacy_symbol)
6296 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6297 FALSE, FALSE, FALSE);
6298 if (h && (h->root.type == bfd_link_hash_defined
6299 || h->root.type == bfd_link_hash_defweak)
6300 && h->def_regular
6301 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6302 {
6303 /* The symbol has no type if specified on the command line. */
6304 h->type = STT_OBJECT;
6305 if (info->stacksize)
6306 /* xgettext:c-format */
6307 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6308 output_bfd, legacy_symbol);
6309 else if (h->root.u.def.section != bfd_abs_section_ptr)
6310 /* xgettext:c-format */
6311 _bfd_error_handler (_("%pB: %s not absolute"),
6312 output_bfd, legacy_symbol);
6313 else
6314 info->stacksize = h->root.u.def.value;
6315 }
6316
6317 if (!info->stacksize)
6318 /* If the user didn't set a size, or explicitly inhibit the
6319 size, set it now. */
6320 info->stacksize = default_size;
6321
6322 /* Provide the legacy symbol, if it is referenced. */
6323 if (h && (h->root.type == bfd_link_hash_undefined
6324 || h->root.type == bfd_link_hash_undefweak))
6325 {
6326 struct bfd_link_hash_entry *bh = NULL;
6327
6328 if (!(_bfd_generic_link_add_one_symbol
6329 (info, output_bfd, legacy_symbol,
6330 BSF_GLOBAL, bfd_abs_section_ptr,
6331 info->stacksize >= 0 ? info->stacksize : 0,
6332 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6333 return FALSE;
6334
6335 h = (struct elf_link_hash_entry *) bh;
6336 h->def_regular = 1;
6337 h->type = STT_OBJECT;
6338 }
6339
6340 return TRUE;
6341 }
6342
6343 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6344
6345 struct elf_gc_sweep_symbol_info
6346 {
6347 struct bfd_link_info *info;
6348 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6349 bfd_boolean);
6350 };
6351
6352 static bfd_boolean
6353 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6354 {
6355 if (!h->mark
6356 && (((h->root.type == bfd_link_hash_defined
6357 || h->root.type == bfd_link_hash_defweak)
6358 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6359 && h->root.u.def.section->gc_mark))
6360 || h->root.type == bfd_link_hash_undefined
6361 || h->root.type == bfd_link_hash_undefweak))
6362 {
6363 struct elf_gc_sweep_symbol_info *inf;
6364
6365 inf = (struct elf_gc_sweep_symbol_info *) data;
6366 (*inf->hide_symbol) (inf->info, h, TRUE);
6367 h->def_regular = 0;
6368 h->ref_regular = 0;
6369 h->ref_regular_nonweak = 0;
6370 }
6371
6372 return TRUE;
6373 }
6374
6375 /* Set up the sizes and contents of the ELF dynamic sections. This is
6376 called by the ELF linker emulation before_allocation routine. We
6377 must set the sizes of the sections before the linker sets the
6378 addresses of the various sections. */
6379
6380 bfd_boolean
6381 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6382 const char *soname,
6383 const char *rpath,
6384 const char *filter_shlib,
6385 const char *audit,
6386 const char *depaudit,
6387 const char * const *auxiliary_filters,
6388 struct bfd_link_info *info,
6389 asection **sinterpptr)
6390 {
6391 bfd *dynobj;
6392 const struct elf_backend_data *bed;
6393
6394 *sinterpptr = NULL;
6395
6396 if (!is_elf_hash_table (info->hash))
6397 return TRUE;
6398
6399 dynobj = elf_hash_table (info)->dynobj;
6400
6401 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6402 {
6403 struct bfd_elf_version_tree *verdefs;
6404 struct elf_info_failed asvinfo;
6405 struct bfd_elf_version_tree *t;
6406 struct bfd_elf_version_expr *d;
6407 asection *s;
6408 size_t soname_indx;
6409
6410 /* If we are supposed to export all symbols into the dynamic symbol
6411 table (this is not the normal case), then do so. */
6412 if (info->export_dynamic
6413 || (bfd_link_executable (info) && info->dynamic))
6414 {
6415 struct elf_info_failed eif;
6416
6417 eif.info = info;
6418 eif.failed = FALSE;
6419 elf_link_hash_traverse (elf_hash_table (info),
6420 _bfd_elf_export_symbol,
6421 &eif);
6422 if (eif.failed)
6423 return FALSE;
6424 }
6425
6426 if (soname != NULL)
6427 {
6428 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6429 soname, TRUE);
6430 if (soname_indx == (size_t) -1
6431 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6432 return FALSE;
6433 }
6434 else
6435 soname_indx = (size_t) -1;
6436
6437 /* Make all global versions with definition. */
6438 for (t = info->version_info; t != NULL; t = t->next)
6439 for (d = t->globals.list; d != NULL; d = d->next)
6440 if (!d->symver && d->literal)
6441 {
6442 const char *verstr, *name;
6443 size_t namelen, verlen, newlen;
6444 char *newname, *p, leading_char;
6445 struct elf_link_hash_entry *newh;
6446
6447 leading_char = bfd_get_symbol_leading_char (output_bfd);
6448 name = d->pattern;
6449 namelen = strlen (name) + (leading_char != '\0');
6450 verstr = t->name;
6451 verlen = strlen (verstr);
6452 newlen = namelen + verlen + 3;
6453
6454 newname = (char *) bfd_malloc (newlen);
6455 if (newname == NULL)
6456 return FALSE;
6457 newname[0] = leading_char;
6458 memcpy (newname + (leading_char != '\0'), name, namelen);
6459
6460 /* Check the hidden versioned definition. */
6461 p = newname + namelen;
6462 *p++ = ELF_VER_CHR;
6463 memcpy (p, verstr, verlen + 1);
6464 newh = elf_link_hash_lookup (elf_hash_table (info),
6465 newname, FALSE, FALSE,
6466 FALSE);
6467 if (newh == NULL
6468 || (newh->root.type != bfd_link_hash_defined
6469 && newh->root.type != bfd_link_hash_defweak))
6470 {
6471 /* Check the default versioned definition. */
6472 *p++ = ELF_VER_CHR;
6473 memcpy (p, verstr, verlen + 1);
6474 newh = elf_link_hash_lookup (elf_hash_table (info),
6475 newname, FALSE, FALSE,
6476 FALSE);
6477 }
6478 free (newname);
6479
6480 /* Mark this version if there is a definition and it is
6481 not defined in a shared object. */
6482 if (newh != NULL
6483 && !newh->def_dynamic
6484 && (newh->root.type == bfd_link_hash_defined
6485 || newh->root.type == bfd_link_hash_defweak))
6486 d->symver = 1;
6487 }
6488
6489 /* Attach all the symbols to their version information. */
6490 asvinfo.info = info;
6491 asvinfo.failed = FALSE;
6492
6493 elf_link_hash_traverse (elf_hash_table (info),
6494 _bfd_elf_link_assign_sym_version,
6495 &asvinfo);
6496 if (asvinfo.failed)
6497 return FALSE;
6498
6499 if (!info->allow_undefined_version)
6500 {
6501 /* Check if all global versions have a definition. */
6502 bfd_boolean all_defined = TRUE;
6503 for (t = info->version_info; t != NULL; t = t->next)
6504 for (d = t->globals.list; d != NULL; d = d->next)
6505 if (d->literal && !d->symver && !d->script)
6506 {
6507 _bfd_error_handler
6508 (_("%s: undefined version: %s"),
6509 d->pattern, t->name);
6510 all_defined = FALSE;
6511 }
6512
6513 if (!all_defined)
6514 {
6515 bfd_set_error (bfd_error_bad_value);
6516 return FALSE;
6517 }
6518 }
6519
6520 /* Set up the version definition section. */
6521 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6522 BFD_ASSERT (s != NULL);
6523
6524 /* We may have created additional version definitions if we are
6525 just linking a regular application. */
6526 verdefs = info->version_info;
6527
6528 /* Skip anonymous version tag. */
6529 if (verdefs != NULL && verdefs->vernum == 0)
6530 verdefs = verdefs->next;
6531
6532 if (verdefs == NULL && !info->create_default_symver)
6533 s->flags |= SEC_EXCLUDE;
6534 else
6535 {
6536 unsigned int cdefs;
6537 bfd_size_type size;
6538 bfd_byte *p;
6539 Elf_Internal_Verdef def;
6540 Elf_Internal_Verdaux defaux;
6541 struct bfd_link_hash_entry *bh;
6542 struct elf_link_hash_entry *h;
6543 const char *name;
6544
6545 cdefs = 0;
6546 size = 0;
6547
6548 /* Make space for the base version. */
6549 size += sizeof (Elf_External_Verdef);
6550 size += sizeof (Elf_External_Verdaux);
6551 ++cdefs;
6552
6553 /* Make space for the default version. */
6554 if (info->create_default_symver)
6555 {
6556 size += sizeof (Elf_External_Verdef);
6557 ++cdefs;
6558 }
6559
6560 for (t = verdefs; t != NULL; t = t->next)
6561 {
6562 struct bfd_elf_version_deps *n;
6563
6564 /* Don't emit base version twice. */
6565 if (t->vernum == 0)
6566 continue;
6567
6568 size += sizeof (Elf_External_Verdef);
6569 size += sizeof (Elf_External_Verdaux);
6570 ++cdefs;
6571
6572 for (n = t->deps; n != NULL; n = n->next)
6573 size += sizeof (Elf_External_Verdaux);
6574 }
6575
6576 s->size = size;
6577 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6578 if (s->contents == NULL && s->size != 0)
6579 return FALSE;
6580
6581 /* Fill in the version definition section. */
6582
6583 p = s->contents;
6584
6585 def.vd_version = VER_DEF_CURRENT;
6586 def.vd_flags = VER_FLG_BASE;
6587 def.vd_ndx = 1;
6588 def.vd_cnt = 1;
6589 if (info->create_default_symver)
6590 {
6591 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6592 def.vd_next = sizeof (Elf_External_Verdef);
6593 }
6594 else
6595 {
6596 def.vd_aux = sizeof (Elf_External_Verdef);
6597 def.vd_next = (sizeof (Elf_External_Verdef)
6598 + sizeof (Elf_External_Verdaux));
6599 }
6600
6601 if (soname_indx != (size_t) -1)
6602 {
6603 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6604 soname_indx);
6605 def.vd_hash = bfd_elf_hash (soname);
6606 defaux.vda_name = soname_indx;
6607 name = soname;
6608 }
6609 else
6610 {
6611 size_t indx;
6612
6613 name = lbasename (bfd_get_filename (output_bfd));
6614 def.vd_hash = bfd_elf_hash (name);
6615 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6616 name, FALSE);
6617 if (indx == (size_t) -1)
6618 return FALSE;
6619 defaux.vda_name = indx;
6620 }
6621 defaux.vda_next = 0;
6622
6623 _bfd_elf_swap_verdef_out (output_bfd, &def,
6624 (Elf_External_Verdef *) p);
6625 p += sizeof (Elf_External_Verdef);
6626 if (info->create_default_symver)
6627 {
6628 /* Add a symbol representing this version. */
6629 bh = NULL;
6630 if (! (_bfd_generic_link_add_one_symbol
6631 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6632 0, NULL, FALSE,
6633 get_elf_backend_data (dynobj)->collect, &bh)))
6634 return FALSE;
6635 h = (struct elf_link_hash_entry *) bh;
6636 h->non_elf = 0;
6637 h->def_regular = 1;
6638 h->type = STT_OBJECT;
6639 h->verinfo.vertree = NULL;
6640
6641 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6642 return FALSE;
6643
6644 /* Create a duplicate of the base version with the same
6645 aux block, but different flags. */
6646 def.vd_flags = 0;
6647 def.vd_ndx = 2;
6648 def.vd_aux = sizeof (Elf_External_Verdef);
6649 if (verdefs)
6650 def.vd_next = (sizeof (Elf_External_Verdef)
6651 + sizeof (Elf_External_Verdaux));
6652 else
6653 def.vd_next = 0;
6654 _bfd_elf_swap_verdef_out (output_bfd, &def,
6655 (Elf_External_Verdef *) p);
6656 p += sizeof (Elf_External_Verdef);
6657 }
6658 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6659 (Elf_External_Verdaux *) p);
6660 p += sizeof (Elf_External_Verdaux);
6661
6662 for (t = verdefs; t != NULL; t = t->next)
6663 {
6664 unsigned int cdeps;
6665 struct bfd_elf_version_deps *n;
6666
6667 /* Don't emit the base version twice. */
6668 if (t->vernum == 0)
6669 continue;
6670
6671 cdeps = 0;
6672 for (n = t->deps; n != NULL; n = n->next)
6673 ++cdeps;
6674
6675 /* Add a symbol representing this version. */
6676 bh = NULL;
6677 if (! (_bfd_generic_link_add_one_symbol
6678 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6679 0, NULL, FALSE,
6680 get_elf_backend_data (dynobj)->collect, &bh)))
6681 return FALSE;
6682 h = (struct elf_link_hash_entry *) bh;
6683 h->non_elf = 0;
6684 h->def_regular = 1;
6685 h->type = STT_OBJECT;
6686 h->verinfo.vertree = t;
6687
6688 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6689 return FALSE;
6690
6691 def.vd_version = VER_DEF_CURRENT;
6692 def.vd_flags = 0;
6693 if (t->globals.list == NULL
6694 && t->locals.list == NULL
6695 && ! t->used)
6696 def.vd_flags |= VER_FLG_WEAK;
6697 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6698 def.vd_cnt = cdeps + 1;
6699 def.vd_hash = bfd_elf_hash (t->name);
6700 def.vd_aux = sizeof (Elf_External_Verdef);
6701 def.vd_next = 0;
6702
6703 /* If a basever node is next, it *must* be the last node in
6704 the chain, otherwise Verdef construction breaks. */
6705 if (t->next != NULL && t->next->vernum == 0)
6706 BFD_ASSERT (t->next->next == NULL);
6707
6708 if (t->next != NULL && t->next->vernum != 0)
6709 def.vd_next = (sizeof (Elf_External_Verdef)
6710 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6711
6712 _bfd_elf_swap_verdef_out (output_bfd, &def,
6713 (Elf_External_Verdef *) p);
6714 p += sizeof (Elf_External_Verdef);
6715
6716 defaux.vda_name = h->dynstr_index;
6717 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6718 h->dynstr_index);
6719 defaux.vda_next = 0;
6720 if (t->deps != NULL)
6721 defaux.vda_next = sizeof (Elf_External_Verdaux);
6722 t->name_indx = defaux.vda_name;
6723
6724 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6725 (Elf_External_Verdaux *) p);
6726 p += sizeof (Elf_External_Verdaux);
6727
6728 for (n = t->deps; n != NULL; n = n->next)
6729 {
6730 if (n->version_needed == NULL)
6731 {
6732 /* This can happen if there was an error in the
6733 version script. */
6734 defaux.vda_name = 0;
6735 }
6736 else
6737 {
6738 defaux.vda_name = n->version_needed->name_indx;
6739 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6740 defaux.vda_name);
6741 }
6742 if (n->next == NULL)
6743 defaux.vda_next = 0;
6744 else
6745 defaux.vda_next = sizeof (Elf_External_Verdaux);
6746
6747 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6748 (Elf_External_Verdaux *) p);
6749 p += sizeof (Elf_External_Verdaux);
6750 }
6751 }
6752
6753 elf_tdata (output_bfd)->cverdefs = cdefs;
6754 }
6755 }
6756
6757 bed = get_elf_backend_data (output_bfd);
6758
6759 if (info->gc_sections && bed->can_gc_sections)
6760 {
6761 struct elf_gc_sweep_symbol_info sweep_info;
6762
6763 /* Remove the symbols that were in the swept sections from the
6764 dynamic symbol table. */
6765 sweep_info.info = info;
6766 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6767 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6768 &sweep_info);
6769 }
6770
6771 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6772 {
6773 asection *s;
6774 struct elf_find_verdep_info sinfo;
6775
6776 /* Work out the size of the version reference section. */
6777
6778 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6779 BFD_ASSERT (s != NULL);
6780
6781 sinfo.info = info;
6782 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6783 if (sinfo.vers == 0)
6784 sinfo.vers = 1;
6785 sinfo.failed = FALSE;
6786
6787 elf_link_hash_traverse (elf_hash_table (info),
6788 _bfd_elf_link_find_version_dependencies,
6789 &sinfo);
6790 if (sinfo.failed)
6791 return FALSE;
6792
6793 if (elf_tdata (output_bfd)->verref == NULL)
6794 s->flags |= SEC_EXCLUDE;
6795 else
6796 {
6797 Elf_Internal_Verneed *vn;
6798 unsigned int size;
6799 unsigned int crefs;
6800 bfd_byte *p;
6801
6802 /* Build the version dependency section. */
6803 size = 0;
6804 crefs = 0;
6805 for (vn = elf_tdata (output_bfd)->verref;
6806 vn != NULL;
6807 vn = vn->vn_nextref)
6808 {
6809 Elf_Internal_Vernaux *a;
6810
6811 size += sizeof (Elf_External_Verneed);
6812 ++crefs;
6813 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6814 size += sizeof (Elf_External_Vernaux);
6815 }
6816
6817 s->size = size;
6818 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6819 if (s->contents == NULL)
6820 return FALSE;
6821
6822 p = s->contents;
6823 for (vn = elf_tdata (output_bfd)->verref;
6824 vn != NULL;
6825 vn = vn->vn_nextref)
6826 {
6827 unsigned int caux;
6828 Elf_Internal_Vernaux *a;
6829 size_t indx;
6830
6831 caux = 0;
6832 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6833 ++caux;
6834
6835 vn->vn_version = VER_NEED_CURRENT;
6836 vn->vn_cnt = caux;
6837 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6838 elf_dt_name (vn->vn_bfd) != NULL
6839 ? elf_dt_name (vn->vn_bfd)
6840 : lbasename (bfd_get_filename
6841 (vn->vn_bfd)),
6842 FALSE);
6843 if (indx == (size_t) -1)
6844 return FALSE;
6845 vn->vn_file = indx;
6846 vn->vn_aux = sizeof (Elf_External_Verneed);
6847 if (vn->vn_nextref == NULL)
6848 vn->vn_next = 0;
6849 else
6850 vn->vn_next = (sizeof (Elf_External_Verneed)
6851 + caux * sizeof (Elf_External_Vernaux));
6852
6853 _bfd_elf_swap_verneed_out (output_bfd, vn,
6854 (Elf_External_Verneed *) p);
6855 p += sizeof (Elf_External_Verneed);
6856
6857 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6858 {
6859 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6860 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6861 a->vna_nodename, FALSE);
6862 if (indx == (size_t) -1)
6863 return FALSE;
6864 a->vna_name = indx;
6865 if (a->vna_nextptr == NULL)
6866 a->vna_next = 0;
6867 else
6868 a->vna_next = sizeof (Elf_External_Vernaux);
6869
6870 _bfd_elf_swap_vernaux_out (output_bfd, a,
6871 (Elf_External_Vernaux *) p);
6872 p += sizeof (Elf_External_Vernaux);
6873 }
6874 }
6875
6876 elf_tdata (output_bfd)->cverrefs = crefs;
6877 }
6878 }
6879
6880 /* Any syms created from now on start with -1 in
6881 got.refcount/offset and plt.refcount/offset. */
6882 elf_hash_table (info)->init_got_refcount
6883 = elf_hash_table (info)->init_got_offset;
6884 elf_hash_table (info)->init_plt_refcount
6885 = elf_hash_table (info)->init_plt_offset;
6886
6887 if (bfd_link_relocatable (info)
6888 && !_bfd_elf_size_group_sections (info))
6889 return FALSE;
6890
6891 /* The backend may have to create some sections regardless of whether
6892 we're dynamic or not. */
6893 if (bed->elf_backend_always_size_sections
6894 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6895 return FALSE;
6896
6897 /* Determine any GNU_STACK segment requirements, after the backend
6898 has had a chance to set a default segment size. */
6899 if (info->execstack)
6900 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6901 else if (info->noexecstack)
6902 elf_stack_flags (output_bfd) = PF_R | PF_W;
6903 else
6904 {
6905 bfd *inputobj;
6906 asection *notesec = NULL;
6907 int exec = 0;
6908
6909 for (inputobj = info->input_bfds;
6910 inputobj;
6911 inputobj = inputobj->link.next)
6912 {
6913 asection *s;
6914
6915 if (inputobj->flags
6916 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6917 continue;
6918 s = inputobj->sections;
6919 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6920 continue;
6921
6922 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6923 if (s)
6924 {
6925 if (s->flags & SEC_CODE)
6926 exec = PF_X;
6927 notesec = s;
6928 }
6929 else if (bed->default_execstack)
6930 exec = PF_X;
6931 }
6932 if (notesec || info->stacksize > 0)
6933 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6934 if (notesec && exec && bfd_link_relocatable (info)
6935 && notesec->output_section != bfd_abs_section_ptr)
6936 notesec->output_section->flags |= SEC_CODE;
6937 }
6938
6939 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6940 {
6941 struct elf_info_failed eif;
6942 struct elf_link_hash_entry *h;
6943 asection *dynstr;
6944 asection *s;
6945
6946 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6947 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6948
6949 if (info->symbolic)
6950 {
6951 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6952 return FALSE;
6953 info->flags |= DF_SYMBOLIC;
6954 }
6955
6956 if (rpath != NULL)
6957 {
6958 size_t indx;
6959 bfd_vma tag;
6960
6961 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6962 TRUE);
6963 if (indx == (size_t) -1)
6964 return FALSE;
6965
6966 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6967 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6968 return FALSE;
6969 }
6970
6971 if (filter_shlib != NULL)
6972 {
6973 size_t indx;
6974
6975 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6976 filter_shlib, TRUE);
6977 if (indx == (size_t) -1
6978 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6979 return FALSE;
6980 }
6981
6982 if (auxiliary_filters != NULL)
6983 {
6984 const char * const *p;
6985
6986 for (p = auxiliary_filters; *p != NULL; p++)
6987 {
6988 size_t indx;
6989
6990 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6991 *p, TRUE);
6992 if (indx == (size_t) -1
6993 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6994 return FALSE;
6995 }
6996 }
6997
6998 if (audit != NULL)
6999 {
7000 size_t indx;
7001
7002 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7003 TRUE);
7004 if (indx == (size_t) -1
7005 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7006 return FALSE;
7007 }
7008
7009 if (depaudit != NULL)
7010 {
7011 size_t indx;
7012
7013 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7014 TRUE);
7015 if (indx == (size_t) -1
7016 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7017 return FALSE;
7018 }
7019
7020 eif.info = info;
7021 eif.failed = FALSE;
7022
7023 /* Find all symbols which were defined in a dynamic object and make
7024 the backend pick a reasonable value for them. */
7025 elf_link_hash_traverse (elf_hash_table (info),
7026 _bfd_elf_adjust_dynamic_symbol,
7027 &eif);
7028 if (eif.failed)
7029 return FALSE;
7030
7031 /* Add some entries to the .dynamic section. We fill in some of the
7032 values later, in bfd_elf_final_link, but we must add the entries
7033 now so that we know the final size of the .dynamic section. */
7034
7035 /* If there are initialization and/or finalization functions to
7036 call then add the corresponding DT_INIT/DT_FINI entries. */
7037 h = (info->init_function
7038 ? elf_link_hash_lookup (elf_hash_table (info),
7039 info->init_function, FALSE,
7040 FALSE, FALSE)
7041 : NULL);
7042 if (h != NULL
7043 && (h->ref_regular
7044 || h->def_regular))
7045 {
7046 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7047 return FALSE;
7048 }
7049 h = (info->fini_function
7050 ? elf_link_hash_lookup (elf_hash_table (info),
7051 info->fini_function, FALSE,
7052 FALSE, FALSE)
7053 : NULL);
7054 if (h != NULL
7055 && (h->ref_regular
7056 || h->def_regular))
7057 {
7058 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7059 return FALSE;
7060 }
7061
7062 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7063 if (s != NULL && s->linker_has_input)
7064 {
7065 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7066 if (! bfd_link_executable (info))
7067 {
7068 bfd *sub;
7069 asection *o;
7070
7071 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7072 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7073 && (o = sub->sections) != NULL
7074 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7075 for (o = sub->sections; o != NULL; o = o->next)
7076 if (elf_section_data (o)->this_hdr.sh_type
7077 == SHT_PREINIT_ARRAY)
7078 {
7079 _bfd_error_handler
7080 (_("%pB: .preinit_array section is not allowed in DSO"),
7081 sub);
7082 break;
7083 }
7084
7085 bfd_set_error (bfd_error_nonrepresentable_section);
7086 return FALSE;
7087 }
7088
7089 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7090 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7091 return FALSE;
7092 }
7093 s = bfd_get_section_by_name (output_bfd, ".init_array");
7094 if (s != NULL && s->linker_has_input)
7095 {
7096 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7097 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7098 return FALSE;
7099 }
7100 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7101 if (s != NULL && s->linker_has_input)
7102 {
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7104 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7105 return FALSE;
7106 }
7107
7108 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7109 /* If .dynstr is excluded from the link, we don't want any of
7110 these tags. Strictly, we should be checking each section
7111 individually; This quick check covers for the case where
7112 someone does a /DISCARD/ : { *(*) }. */
7113 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7114 {
7115 bfd_size_type strsize;
7116
7117 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7118 if ((info->emit_hash
7119 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7120 || (info->emit_gnu_hash
7121 && (bed->record_xhash_symbol == NULL
7122 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7123 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7124 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7125 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7126 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7127 bed->s->sizeof_sym))
7128 return FALSE;
7129 }
7130 }
7131
7132 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7133 return FALSE;
7134
7135 /* The backend must work out the sizes of all the other dynamic
7136 sections. */
7137 if (dynobj != NULL
7138 && bed->elf_backend_size_dynamic_sections != NULL
7139 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7140 return FALSE;
7141
7142 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7143 {
7144 if (elf_tdata (output_bfd)->cverdefs)
7145 {
7146 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7147
7148 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7149 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7150 return FALSE;
7151 }
7152
7153 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7156 return FALSE;
7157 }
7158 else if (info->flags & DF_BIND_NOW)
7159 {
7160 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7161 return FALSE;
7162 }
7163
7164 if (info->flags_1)
7165 {
7166 if (bfd_link_executable (info))
7167 info->flags_1 &= ~ (DF_1_INITFIRST
7168 | DF_1_NODELETE
7169 | DF_1_NOOPEN);
7170 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7171 return FALSE;
7172 }
7173
7174 if (elf_tdata (output_bfd)->cverrefs)
7175 {
7176 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7177
7178 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7180 return FALSE;
7181 }
7182
7183 if ((elf_tdata (output_bfd)->cverrefs == 0
7184 && elf_tdata (output_bfd)->cverdefs == 0)
7185 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7186 {
7187 asection *s;
7188
7189 s = bfd_get_linker_section (dynobj, ".gnu.version");
7190 s->flags |= SEC_EXCLUDE;
7191 }
7192 }
7193 return TRUE;
7194 }
7195
7196 /* Find the first non-excluded output section. We'll use its
7197 section symbol for some emitted relocs. */
7198 void
7199 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7200 {
7201 asection *s;
7202 asection *found = NULL;
7203
7204 for (s = output_bfd->sections; s != NULL; s = s->next)
7205 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7206 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7207 {
7208 found = s;
7209 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7210 break;
7211 }
7212 elf_hash_table (info)->text_index_section = found;
7213 }
7214
7215 /* Find two non-excluded output sections, one for code, one for data.
7216 We'll use their section symbols for some emitted relocs. */
7217 void
7218 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7219 {
7220 asection *s;
7221 asection *found = NULL;
7222
7223 /* Data first, since setting text_index_section changes
7224 _bfd_elf_omit_section_dynsym_default. */
7225 for (s = output_bfd->sections; s != NULL; s = s->next)
7226 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7227 && !(s->flags & SEC_READONLY)
7228 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7229 {
7230 found = s;
7231 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7232 break;
7233 }
7234 elf_hash_table (info)->data_index_section = found;
7235
7236 for (s = output_bfd->sections; s != NULL; s = s->next)
7237 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7238 && (s->flags & SEC_READONLY)
7239 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7240 {
7241 found = s;
7242 break;
7243 }
7244 elf_hash_table (info)->text_index_section = found;
7245 }
7246
7247 #define GNU_HASH_SECTION_NAME(bed) \
7248 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7249
7250 bfd_boolean
7251 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7252 {
7253 const struct elf_backend_data *bed;
7254 unsigned long section_sym_count;
7255 bfd_size_type dynsymcount = 0;
7256
7257 if (!is_elf_hash_table (info->hash))
7258 return TRUE;
7259
7260 bed = get_elf_backend_data (output_bfd);
7261 (*bed->elf_backend_init_index_section) (output_bfd, info);
7262
7263 /* Assign dynsym indices. In a shared library we generate a section
7264 symbol for each output section, which come first. Next come all
7265 of the back-end allocated local dynamic syms, followed by the rest
7266 of the global symbols.
7267
7268 This is usually not needed for static binaries, however backends
7269 can request to always do it, e.g. the MIPS backend uses dynamic
7270 symbol counts to lay out GOT, which will be produced in the
7271 presence of GOT relocations even in static binaries (holding fixed
7272 data in that case, to satisfy those relocations). */
7273
7274 if (elf_hash_table (info)->dynamic_sections_created
7275 || bed->always_renumber_dynsyms)
7276 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7277 &section_sym_count);
7278
7279 if (elf_hash_table (info)->dynamic_sections_created)
7280 {
7281 bfd *dynobj;
7282 asection *s;
7283 unsigned int dtagcount;
7284
7285 dynobj = elf_hash_table (info)->dynobj;
7286
7287 /* Work out the size of the symbol version section. */
7288 s = bfd_get_linker_section (dynobj, ".gnu.version");
7289 BFD_ASSERT (s != NULL);
7290 if ((s->flags & SEC_EXCLUDE) == 0)
7291 {
7292 s->size = dynsymcount * sizeof (Elf_External_Versym);
7293 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7294 if (s->contents == NULL)
7295 return FALSE;
7296
7297 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7298 return FALSE;
7299 }
7300
7301 /* Set the size of the .dynsym and .hash sections. We counted
7302 the number of dynamic symbols in elf_link_add_object_symbols.
7303 We will build the contents of .dynsym and .hash when we build
7304 the final symbol table, because until then we do not know the
7305 correct value to give the symbols. We built the .dynstr
7306 section as we went along in elf_link_add_object_symbols. */
7307 s = elf_hash_table (info)->dynsym;
7308 BFD_ASSERT (s != NULL);
7309 s->size = dynsymcount * bed->s->sizeof_sym;
7310
7311 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7312 if (s->contents == NULL)
7313 return FALSE;
7314
7315 /* The first entry in .dynsym is a dummy symbol. Clear all the
7316 section syms, in case we don't output them all. */
7317 ++section_sym_count;
7318 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7319
7320 elf_hash_table (info)->bucketcount = 0;
7321
7322 /* Compute the size of the hashing table. As a side effect this
7323 computes the hash values for all the names we export. */
7324 if (info->emit_hash)
7325 {
7326 unsigned long int *hashcodes;
7327 struct hash_codes_info hashinf;
7328 bfd_size_type amt;
7329 unsigned long int nsyms;
7330 size_t bucketcount;
7331 size_t hash_entry_size;
7332
7333 /* Compute the hash values for all exported symbols. At the same
7334 time store the values in an array so that we could use them for
7335 optimizations. */
7336 amt = dynsymcount * sizeof (unsigned long int);
7337 hashcodes = (unsigned long int *) bfd_malloc (amt);
7338 if (hashcodes == NULL)
7339 return FALSE;
7340 hashinf.hashcodes = hashcodes;
7341 hashinf.error = FALSE;
7342
7343 /* Put all hash values in HASHCODES. */
7344 elf_link_hash_traverse (elf_hash_table (info),
7345 elf_collect_hash_codes, &hashinf);
7346 if (hashinf.error)
7347 {
7348 free (hashcodes);
7349 return FALSE;
7350 }
7351
7352 nsyms = hashinf.hashcodes - hashcodes;
7353 bucketcount
7354 = compute_bucket_count (info, hashcodes, nsyms, 0);
7355 free (hashcodes);
7356
7357 if (bucketcount == 0 && nsyms > 0)
7358 return FALSE;
7359
7360 elf_hash_table (info)->bucketcount = bucketcount;
7361
7362 s = bfd_get_linker_section (dynobj, ".hash");
7363 BFD_ASSERT (s != NULL);
7364 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7365 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7366 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7367 if (s->contents == NULL)
7368 return FALSE;
7369
7370 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7371 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7372 s->contents + hash_entry_size);
7373 }
7374
7375 if (info->emit_gnu_hash)
7376 {
7377 size_t i, cnt;
7378 unsigned char *contents;
7379 struct collect_gnu_hash_codes cinfo;
7380 bfd_size_type amt;
7381 size_t bucketcount;
7382
7383 memset (&cinfo, 0, sizeof (cinfo));
7384
7385 /* Compute the hash values for all exported symbols. At the same
7386 time store the values in an array so that we could use them for
7387 optimizations. */
7388 amt = dynsymcount * 2 * sizeof (unsigned long int);
7389 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7390 if (cinfo.hashcodes == NULL)
7391 return FALSE;
7392
7393 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7394 cinfo.min_dynindx = -1;
7395 cinfo.output_bfd = output_bfd;
7396 cinfo.bed = bed;
7397
7398 /* Put all hash values in HASHCODES. */
7399 elf_link_hash_traverse (elf_hash_table (info),
7400 elf_collect_gnu_hash_codes, &cinfo);
7401 if (cinfo.error)
7402 {
7403 free (cinfo.hashcodes);
7404 return FALSE;
7405 }
7406
7407 bucketcount
7408 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7409
7410 if (bucketcount == 0)
7411 {
7412 free (cinfo.hashcodes);
7413 return FALSE;
7414 }
7415
7416 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7417 BFD_ASSERT (s != NULL);
7418
7419 if (cinfo.nsyms == 0)
7420 {
7421 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7422 BFD_ASSERT (cinfo.min_dynindx == -1);
7423 free (cinfo.hashcodes);
7424 s->size = 5 * 4 + bed->s->arch_size / 8;
7425 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7426 if (contents == NULL)
7427 return FALSE;
7428 s->contents = contents;
7429 /* 1 empty bucket. */
7430 bfd_put_32 (output_bfd, 1, contents);
7431 /* SYMIDX above the special symbol 0. */
7432 bfd_put_32 (output_bfd, 1, contents + 4);
7433 /* Just one word for bitmask. */
7434 bfd_put_32 (output_bfd, 1, contents + 8);
7435 /* Only hash fn bloom filter. */
7436 bfd_put_32 (output_bfd, 0, contents + 12);
7437 /* No hashes are valid - empty bitmask. */
7438 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7439 /* No hashes in the only bucket. */
7440 bfd_put_32 (output_bfd, 0,
7441 contents + 16 + bed->s->arch_size / 8);
7442 }
7443 else
7444 {
7445 unsigned long int maskwords, maskbitslog2, x;
7446 BFD_ASSERT (cinfo.min_dynindx != -1);
7447
7448 x = cinfo.nsyms;
7449 maskbitslog2 = 1;
7450 while ((x >>= 1) != 0)
7451 ++maskbitslog2;
7452 if (maskbitslog2 < 3)
7453 maskbitslog2 = 5;
7454 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7455 maskbitslog2 = maskbitslog2 + 3;
7456 else
7457 maskbitslog2 = maskbitslog2 + 2;
7458 if (bed->s->arch_size == 64)
7459 {
7460 if (maskbitslog2 == 5)
7461 maskbitslog2 = 6;
7462 cinfo.shift1 = 6;
7463 }
7464 else
7465 cinfo.shift1 = 5;
7466 cinfo.mask = (1 << cinfo.shift1) - 1;
7467 cinfo.shift2 = maskbitslog2;
7468 cinfo.maskbits = 1 << maskbitslog2;
7469 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7470 amt = bucketcount * sizeof (unsigned long int) * 2;
7471 amt += maskwords * sizeof (bfd_vma);
7472 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7473 if (cinfo.bitmask == NULL)
7474 {
7475 free (cinfo.hashcodes);
7476 return FALSE;
7477 }
7478
7479 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7480 cinfo.indx = cinfo.counts + bucketcount;
7481 cinfo.symindx = dynsymcount - cinfo.nsyms;
7482 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7483
7484 /* Determine how often each hash bucket is used. */
7485 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7486 for (i = 0; i < cinfo.nsyms; ++i)
7487 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7488
7489 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7490 if (cinfo.counts[i] != 0)
7491 {
7492 cinfo.indx[i] = cnt;
7493 cnt += cinfo.counts[i];
7494 }
7495 BFD_ASSERT (cnt == dynsymcount);
7496 cinfo.bucketcount = bucketcount;
7497 cinfo.local_indx = cinfo.min_dynindx;
7498
7499 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7500 s->size += cinfo.maskbits / 8;
7501 if (bed->record_xhash_symbol != NULL)
7502 s->size += cinfo.nsyms * 4;
7503 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7504 if (contents == NULL)
7505 {
7506 free (cinfo.bitmask);
7507 free (cinfo.hashcodes);
7508 return FALSE;
7509 }
7510
7511 s->contents = contents;
7512 bfd_put_32 (output_bfd, bucketcount, contents);
7513 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7514 bfd_put_32 (output_bfd, maskwords, contents + 8);
7515 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7516 contents += 16 + cinfo.maskbits / 8;
7517
7518 for (i = 0; i < bucketcount; ++i)
7519 {
7520 if (cinfo.counts[i] == 0)
7521 bfd_put_32 (output_bfd, 0, contents);
7522 else
7523 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7524 contents += 4;
7525 }
7526
7527 cinfo.contents = contents;
7528
7529 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7530 /* Renumber dynamic symbols, if populating .gnu.hash section.
7531 If using .MIPS.xhash, populate the translation table. */
7532 elf_link_hash_traverse (elf_hash_table (info),
7533 elf_gnu_hash_process_symidx, &cinfo);
7534
7535 contents = s->contents + 16;
7536 for (i = 0; i < maskwords; ++i)
7537 {
7538 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7539 contents);
7540 contents += bed->s->arch_size / 8;
7541 }
7542
7543 free (cinfo.bitmask);
7544 free (cinfo.hashcodes);
7545 }
7546 }
7547
7548 s = bfd_get_linker_section (dynobj, ".dynstr");
7549 BFD_ASSERT (s != NULL);
7550
7551 elf_finalize_dynstr (output_bfd, info);
7552
7553 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7554
7555 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7556 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7557 return FALSE;
7558 }
7559
7560 return TRUE;
7561 }
7562 \f
7563 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7564
7565 static void
7566 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7567 asection *sec)
7568 {
7569 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7570 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7571 }
7572
7573 /* Finish SHF_MERGE section merging. */
7574
7575 bfd_boolean
7576 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7577 {
7578 bfd *ibfd;
7579 asection *sec;
7580
7581 if (!is_elf_hash_table (info->hash))
7582 return FALSE;
7583
7584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7585 if ((ibfd->flags & DYNAMIC) == 0
7586 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7587 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7588 == get_elf_backend_data (obfd)->s->elfclass))
7589 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7590 if ((sec->flags & SEC_MERGE) != 0
7591 && !bfd_is_abs_section (sec->output_section))
7592 {
7593 struct bfd_elf_section_data *secdata;
7594
7595 secdata = elf_section_data (sec);
7596 if (! _bfd_add_merge_section (obfd,
7597 &elf_hash_table (info)->merge_info,
7598 sec, &secdata->sec_info))
7599 return FALSE;
7600 else if (secdata->sec_info)
7601 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7602 }
7603
7604 if (elf_hash_table (info)->merge_info != NULL)
7605 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7606 merge_sections_remove_hook);
7607 return TRUE;
7608 }
7609
7610 /* Create an entry in an ELF linker hash table. */
7611
7612 struct bfd_hash_entry *
7613 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7614 struct bfd_hash_table *table,
7615 const char *string)
7616 {
7617 /* Allocate the structure if it has not already been allocated by a
7618 subclass. */
7619 if (entry == NULL)
7620 {
7621 entry = (struct bfd_hash_entry *)
7622 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7623 if (entry == NULL)
7624 return entry;
7625 }
7626
7627 /* Call the allocation method of the superclass. */
7628 entry = _bfd_link_hash_newfunc (entry, table, string);
7629 if (entry != NULL)
7630 {
7631 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7632 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7633
7634 /* Set local fields. */
7635 ret->indx = -1;
7636 ret->dynindx = -1;
7637 ret->got = htab->init_got_refcount;
7638 ret->plt = htab->init_plt_refcount;
7639 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7640 - offsetof (struct elf_link_hash_entry, size)));
7641 /* Assume that we have been called by a non-ELF symbol reader.
7642 This flag is then reset by the code which reads an ELF input
7643 file. This ensures that a symbol created by a non-ELF symbol
7644 reader will have the flag set correctly. */
7645 ret->non_elf = 1;
7646 }
7647
7648 return entry;
7649 }
7650
7651 /* Copy data from an indirect symbol to its direct symbol, hiding the
7652 old indirect symbol. Also used for copying flags to a weakdef. */
7653
7654 void
7655 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7656 struct elf_link_hash_entry *dir,
7657 struct elf_link_hash_entry *ind)
7658 {
7659 struct elf_link_hash_table *htab;
7660
7661 if (ind->dyn_relocs != NULL)
7662 {
7663 if (dir->dyn_relocs != NULL)
7664 {
7665 struct elf_dyn_relocs **pp;
7666 struct elf_dyn_relocs *p;
7667
7668 /* Add reloc counts against the indirect sym to the direct sym
7669 list. Merge any entries against the same section. */
7670 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7671 {
7672 struct elf_dyn_relocs *q;
7673
7674 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7675 if (q->sec == p->sec)
7676 {
7677 q->pc_count += p->pc_count;
7678 q->count += p->count;
7679 *pp = p->next;
7680 break;
7681 }
7682 if (q == NULL)
7683 pp = &p->next;
7684 }
7685 *pp = dir->dyn_relocs;
7686 }
7687
7688 dir->dyn_relocs = ind->dyn_relocs;
7689 ind->dyn_relocs = NULL;
7690 }
7691
7692 /* Copy down any references that we may have already seen to the
7693 symbol which just became indirect. */
7694
7695 if (dir->versioned != versioned_hidden)
7696 dir->ref_dynamic |= ind->ref_dynamic;
7697 dir->ref_regular |= ind->ref_regular;
7698 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7699 dir->non_got_ref |= ind->non_got_ref;
7700 dir->needs_plt |= ind->needs_plt;
7701 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7702
7703 if (ind->root.type != bfd_link_hash_indirect)
7704 return;
7705
7706 /* Copy over the global and procedure linkage table refcount entries.
7707 These may have been already set up by a check_relocs routine. */
7708 htab = elf_hash_table (info);
7709 if (ind->got.refcount > htab->init_got_refcount.refcount)
7710 {
7711 if (dir->got.refcount < 0)
7712 dir->got.refcount = 0;
7713 dir->got.refcount += ind->got.refcount;
7714 ind->got.refcount = htab->init_got_refcount.refcount;
7715 }
7716
7717 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7718 {
7719 if (dir->plt.refcount < 0)
7720 dir->plt.refcount = 0;
7721 dir->plt.refcount += ind->plt.refcount;
7722 ind->plt.refcount = htab->init_plt_refcount.refcount;
7723 }
7724
7725 if (ind->dynindx != -1)
7726 {
7727 if (dir->dynindx != -1)
7728 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7729 dir->dynindx = ind->dynindx;
7730 dir->dynstr_index = ind->dynstr_index;
7731 ind->dynindx = -1;
7732 ind->dynstr_index = 0;
7733 }
7734 }
7735
7736 void
7737 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7738 struct elf_link_hash_entry *h,
7739 bfd_boolean force_local)
7740 {
7741 /* STT_GNU_IFUNC symbol must go through PLT. */
7742 if (h->type != STT_GNU_IFUNC)
7743 {
7744 h->plt = elf_hash_table (info)->init_plt_offset;
7745 h->needs_plt = 0;
7746 }
7747 if (force_local)
7748 {
7749 h->forced_local = 1;
7750 if (h->dynindx != -1)
7751 {
7752 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7753 h->dynstr_index);
7754 h->dynindx = -1;
7755 h->dynstr_index = 0;
7756 }
7757 }
7758 }
7759
7760 /* Hide a symbol. */
7761
7762 void
7763 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7764 struct bfd_link_info *info,
7765 struct bfd_link_hash_entry *h)
7766 {
7767 if (is_elf_hash_table (info->hash))
7768 {
7769 const struct elf_backend_data *bed
7770 = get_elf_backend_data (output_bfd);
7771 struct elf_link_hash_entry *eh
7772 = (struct elf_link_hash_entry *) h;
7773 bed->elf_backend_hide_symbol (info, eh, TRUE);
7774 eh->def_dynamic = 0;
7775 eh->ref_dynamic = 0;
7776 eh->dynamic_def = 0;
7777 }
7778 }
7779
7780 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7781 caller. */
7782
7783 bfd_boolean
7784 _bfd_elf_link_hash_table_init
7785 (struct elf_link_hash_table *table,
7786 bfd *abfd,
7787 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7788 struct bfd_hash_table *,
7789 const char *),
7790 unsigned int entsize,
7791 enum elf_target_id target_id)
7792 {
7793 bfd_boolean ret;
7794 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7795
7796 table->init_got_refcount.refcount = can_refcount - 1;
7797 table->init_plt_refcount.refcount = can_refcount - 1;
7798 table->init_got_offset.offset = -(bfd_vma) 1;
7799 table->init_plt_offset.offset = -(bfd_vma) 1;
7800 /* The first dynamic symbol is a dummy. */
7801 table->dynsymcount = 1;
7802
7803 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7804
7805 table->root.type = bfd_link_elf_hash_table;
7806 table->hash_table_id = target_id;
7807
7808 return ret;
7809 }
7810
7811 /* Create an ELF linker hash table. */
7812
7813 struct bfd_link_hash_table *
7814 _bfd_elf_link_hash_table_create (bfd *abfd)
7815 {
7816 struct elf_link_hash_table *ret;
7817 size_t amt = sizeof (struct elf_link_hash_table);
7818
7819 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7820 if (ret == NULL)
7821 return NULL;
7822
7823 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7824 sizeof (struct elf_link_hash_entry),
7825 GENERIC_ELF_DATA))
7826 {
7827 free (ret);
7828 return NULL;
7829 }
7830 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7831
7832 return &ret->root;
7833 }
7834
7835 /* Destroy an ELF linker hash table. */
7836
7837 void
7838 _bfd_elf_link_hash_table_free (bfd *obfd)
7839 {
7840 struct elf_link_hash_table *htab;
7841
7842 htab = (struct elf_link_hash_table *) obfd->link.hash;
7843 if (htab->dynstr != NULL)
7844 _bfd_elf_strtab_free (htab->dynstr);
7845 _bfd_merge_sections_free (htab->merge_info);
7846 _bfd_generic_link_hash_table_free (obfd);
7847 }
7848
7849 /* This is a hook for the ELF emulation code in the generic linker to
7850 tell the backend linker what file name to use for the DT_NEEDED
7851 entry for a dynamic object. */
7852
7853 void
7854 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7855 {
7856 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7857 && bfd_get_format (abfd) == bfd_object)
7858 elf_dt_name (abfd) = name;
7859 }
7860
7861 int
7862 bfd_elf_get_dyn_lib_class (bfd *abfd)
7863 {
7864 int lib_class;
7865 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7866 && bfd_get_format (abfd) == bfd_object)
7867 lib_class = elf_dyn_lib_class (abfd);
7868 else
7869 lib_class = 0;
7870 return lib_class;
7871 }
7872
7873 void
7874 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7875 {
7876 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7877 && bfd_get_format (abfd) == bfd_object)
7878 elf_dyn_lib_class (abfd) = lib_class;
7879 }
7880
7881 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7882 the linker ELF emulation code. */
7883
7884 struct bfd_link_needed_list *
7885 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7886 struct bfd_link_info *info)
7887 {
7888 if (! is_elf_hash_table (info->hash))
7889 return NULL;
7890 return elf_hash_table (info)->needed;
7891 }
7892
7893 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7894 hook for the linker ELF emulation code. */
7895
7896 struct bfd_link_needed_list *
7897 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7898 struct bfd_link_info *info)
7899 {
7900 if (! is_elf_hash_table (info->hash))
7901 return NULL;
7902 return elf_hash_table (info)->runpath;
7903 }
7904
7905 /* Get the name actually used for a dynamic object for a link. This
7906 is the SONAME entry if there is one. Otherwise, it is the string
7907 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7908
7909 const char *
7910 bfd_elf_get_dt_soname (bfd *abfd)
7911 {
7912 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7913 && bfd_get_format (abfd) == bfd_object)
7914 return elf_dt_name (abfd);
7915 return NULL;
7916 }
7917
7918 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7919 the ELF linker emulation code. */
7920
7921 bfd_boolean
7922 bfd_elf_get_bfd_needed_list (bfd *abfd,
7923 struct bfd_link_needed_list **pneeded)
7924 {
7925 asection *s;
7926 bfd_byte *dynbuf = NULL;
7927 unsigned int elfsec;
7928 unsigned long shlink;
7929 bfd_byte *extdyn, *extdynend;
7930 size_t extdynsize;
7931 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7932
7933 *pneeded = NULL;
7934
7935 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7936 || bfd_get_format (abfd) != bfd_object)
7937 return TRUE;
7938
7939 s = bfd_get_section_by_name (abfd, ".dynamic");
7940 if (s == NULL || s->size == 0)
7941 return TRUE;
7942
7943 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7944 goto error_return;
7945
7946 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7947 if (elfsec == SHN_BAD)
7948 goto error_return;
7949
7950 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7951
7952 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7953 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7954
7955 extdyn = dynbuf;
7956 extdynend = extdyn + s->size;
7957 for (; extdyn < extdynend; extdyn += extdynsize)
7958 {
7959 Elf_Internal_Dyn dyn;
7960
7961 (*swap_dyn_in) (abfd, extdyn, &dyn);
7962
7963 if (dyn.d_tag == DT_NULL)
7964 break;
7965
7966 if (dyn.d_tag == DT_NEEDED)
7967 {
7968 const char *string;
7969 struct bfd_link_needed_list *l;
7970 unsigned int tagv = dyn.d_un.d_val;
7971 size_t amt;
7972
7973 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7974 if (string == NULL)
7975 goto error_return;
7976
7977 amt = sizeof *l;
7978 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7979 if (l == NULL)
7980 goto error_return;
7981
7982 l->by = abfd;
7983 l->name = string;
7984 l->next = *pneeded;
7985 *pneeded = l;
7986 }
7987 }
7988
7989 free (dynbuf);
7990
7991 return TRUE;
7992
7993 error_return:
7994 free (dynbuf);
7995 return FALSE;
7996 }
7997
7998 struct elf_symbuf_symbol
7999 {
8000 unsigned long st_name; /* Symbol name, index in string tbl */
8001 unsigned char st_info; /* Type and binding attributes */
8002 unsigned char st_other; /* Visibilty, and target specific */
8003 };
8004
8005 struct elf_symbuf_head
8006 {
8007 struct elf_symbuf_symbol *ssym;
8008 size_t count;
8009 unsigned int st_shndx;
8010 };
8011
8012 struct elf_symbol
8013 {
8014 union
8015 {
8016 Elf_Internal_Sym *isym;
8017 struct elf_symbuf_symbol *ssym;
8018 void *p;
8019 } u;
8020 const char *name;
8021 };
8022
8023 /* Sort references to symbols by ascending section number. */
8024
8025 static int
8026 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8027 {
8028 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8029 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8030
8031 if (s1->st_shndx != s2->st_shndx)
8032 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8033 /* Final sort by the address of the sym in the symbuf ensures
8034 a stable sort. */
8035 if (s1 != s2)
8036 return s1 > s2 ? 1 : -1;
8037 return 0;
8038 }
8039
8040 static int
8041 elf_sym_name_compare (const void *arg1, const void *arg2)
8042 {
8043 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8044 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8045 int ret = strcmp (s1->name, s2->name);
8046 if (ret != 0)
8047 return ret;
8048 if (s1->u.p != s2->u.p)
8049 return s1->u.p > s2->u.p ? 1 : -1;
8050 return 0;
8051 }
8052
8053 static struct elf_symbuf_head *
8054 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8055 {
8056 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8057 struct elf_symbuf_symbol *ssym;
8058 struct elf_symbuf_head *ssymbuf, *ssymhead;
8059 size_t i, shndx_count, total_size, amt;
8060
8061 amt = symcount * sizeof (*indbuf);
8062 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8063 if (indbuf == NULL)
8064 return NULL;
8065
8066 for (ind = indbuf, i = 0; i < symcount; i++)
8067 if (isymbuf[i].st_shndx != SHN_UNDEF)
8068 *ind++ = &isymbuf[i];
8069 indbufend = ind;
8070
8071 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8072 elf_sort_elf_symbol);
8073
8074 shndx_count = 0;
8075 if (indbufend > indbuf)
8076 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8077 if (ind[0]->st_shndx != ind[1]->st_shndx)
8078 shndx_count++;
8079
8080 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8081 + (indbufend - indbuf) * sizeof (*ssym));
8082 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8083 if (ssymbuf == NULL)
8084 {
8085 free (indbuf);
8086 return NULL;
8087 }
8088
8089 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8090 ssymbuf->ssym = NULL;
8091 ssymbuf->count = shndx_count;
8092 ssymbuf->st_shndx = 0;
8093 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8094 {
8095 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8096 {
8097 ssymhead++;
8098 ssymhead->ssym = ssym;
8099 ssymhead->count = 0;
8100 ssymhead->st_shndx = (*ind)->st_shndx;
8101 }
8102 ssym->st_name = (*ind)->st_name;
8103 ssym->st_info = (*ind)->st_info;
8104 ssym->st_other = (*ind)->st_other;
8105 ssymhead->count++;
8106 }
8107 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8108 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8109 == total_size));
8110
8111 free (indbuf);
8112 return ssymbuf;
8113 }
8114
8115 /* Check if 2 sections define the same set of local and global
8116 symbols. */
8117
8118 static bfd_boolean
8119 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8120 struct bfd_link_info *info)
8121 {
8122 bfd *bfd1, *bfd2;
8123 const struct elf_backend_data *bed1, *bed2;
8124 Elf_Internal_Shdr *hdr1, *hdr2;
8125 size_t symcount1, symcount2;
8126 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8127 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8128 Elf_Internal_Sym *isym, *isymend;
8129 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8130 size_t count1, count2, i;
8131 unsigned int shndx1, shndx2;
8132 bfd_boolean result;
8133
8134 bfd1 = sec1->owner;
8135 bfd2 = sec2->owner;
8136
8137 /* Both sections have to be in ELF. */
8138 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8139 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8140 return FALSE;
8141
8142 if (elf_section_type (sec1) != elf_section_type (sec2))
8143 return FALSE;
8144
8145 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8146 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8147 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8148 return FALSE;
8149
8150 bed1 = get_elf_backend_data (bfd1);
8151 bed2 = get_elf_backend_data (bfd2);
8152 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8153 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8154 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8155 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8156
8157 if (symcount1 == 0 || symcount2 == 0)
8158 return FALSE;
8159
8160 result = FALSE;
8161 isymbuf1 = NULL;
8162 isymbuf2 = NULL;
8163 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8164 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8165
8166 if (ssymbuf1 == NULL)
8167 {
8168 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8169 NULL, NULL, NULL);
8170 if (isymbuf1 == NULL)
8171 goto done;
8172
8173 if (!info->reduce_memory_overheads)
8174 {
8175 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8176 elf_tdata (bfd1)->symbuf = ssymbuf1;
8177 }
8178 }
8179
8180 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8181 {
8182 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8183 NULL, NULL, NULL);
8184 if (isymbuf2 == NULL)
8185 goto done;
8186
8187 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8188 {
8189 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8190 elf_tdata (bfd2)->symbuf = ssymbuf2;
8191 }
8192 }
8193
8194 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8195 {
8196 /* Optimized faster version. */
8197 size_t lo, hi, mid;
8198 struct elf_symbol *symp;
8199 struct elf_symbuf_symbol *ssym, *ssymend;
8200
8201 lo = 0;
8202 hi = ssymbuf1->count;
8203 ssymbuf1++;
8204 count1 = 0;
8205 while (lo < hi)
8206 {
8207 mid = (lo + hi) / 2;
8208 if (shndx1 < ssymbuf1[mid].st_shndx)
8209 hi = mid;
8210 else if (shndx1 > ssymbuf1[mid].st_shndx)
8211 lo = mid + 1;
8212 else
8213 {
8214 count1 = ssymbuf1[mid].count;
8215 ssymbuf1 += mid;
8216 break;
8217 }
8218 }
8219
8220 lo = 0;
8221 hi = ssymbuf2->count;
8222 ssymbuf2++;
8223 count2 = 0;
8224 while (lo < hi)
8225 {
8226 mid = (lo + hi) / 2;
8227 if (shndx2 < ssymbuf2[mid].st_shndx)
8228 hi = mid;
8229 else if (shndx2 > ssymbuf2[mid].st_shndx)
8230 lo = mid + 1;
8231 else
8232 {
8233 count2 = ssymbuf2[mid].count;
8234 ssymbuf2 += mid;
8235 break;
8236 }
8237 }
8238
8239 if (count1 == 0 || count2 == 0 || count1 != count2)
8240 goto done;
8241
8242 symtable1
8243 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8244 symtable2
8245 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8246 if (symtable1 == NULL || symtable2 == NULL)
8247 goto done;
8248
8249 symp = symtable1;
8250 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8251 ssym < ssymend; ssym++, symp++)
8252 {
8253 symp->u.ssym = ssym;
8254 symp->name = bfd_elf_string_from_elf_section (bfd1,
8255 hdr1->sh_link,
8256 ssym->st_name);
8257 }
8258
8259 symp = symtable2;
8260 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8261 ssym < ssymend; ssym++, symp++)
8262 {
8263 symp->u.ssym = ssym;
8264 symp->name = bfd_elf_string_from_elf_section (bfd2,
8265 hdr2->sh_link,
8266 ssym->st_name);
8267 }
8268
8269 /* Sort symbol by name. */
8270 qsort (symtable1, count1, sizeof (struct elf_symbol),
8271 elf_sym_name_compare);
8272 qsort (symtable2, count1, sizeof (struct elf_symbol),
8273 elf_sym_name_compare);
8274
8275 for (i = 0; i < count1; i++)
8276 /* Two symbols must have the same binding, type and name. */
8277 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8278 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8279 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8280 goto done;
8281
8282 result = TRUE;
8283 goto done;
8284 }
8285
8286 symtable1 = (struct elf_symbol *)
8287 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8288 symtable2 = (struct elf_symbol *)
8289 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8290 if (symtable1 == NULL || symtable2 == NULL)
8291 goto done;
8292
8293 /* Count definitions in the section. */
8294 count1 = 0;
8295 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8296 if (isym->st_shndx == shndx1)
8297 symtable1[count1++].u.isym = isym;
8298
8299 count2 = 0;
8300 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8301 if (isym->st_shndx == shndx2)
8302 symtable2[count2++].u.isym = isym;
8303
8304 if (count1 == 0 || count2 == 0 || count1 != count2)
8305 goto done;
8306
8307 for (i = 0; i < count1; i++)
8308 symtable1[i].name
8309 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8310 symtable1[i].u.isym->st_name);
8311
8312 for (i = 0; i < count2; i++)
8313 symtable2[i].name
8314 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8315 symtable2[i].u.isym->st_name);
8316
8317 /* Sort symbol by name. */
8318 qsort (symtable1, count1, sizeof (struct elf_symbol),
8319 elf_sym_name_compare);
8320 qsort (symtable2, count1, sizeof (struct elf_symbol),
8321 elf_sym_name_compare);
8322
8323 for (i = 0; i < count1; i++)
8324 /* Two symbols must have the same binding, type and name. */
8325 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8326 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8327 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8328 goto done;
8329
8330 result = TRUE;
8331
8332 done:
8333 free (symtable1);
8334 free (symtable2);
8335 free (isymbuf1);
8336 free (isymbuf2);
8337
8338 return result;
8339 }
8340
8341 /* Return TRUE if 2 section types are compatible. */
8342
8343 bfd_boolean
8344 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8345 bfd *bbfd, const asection *bsec)
8346 {
8347 if (asec == NULL
8348 || bsec == NULL
8349 || abfd->xvec->flavour != bfd_target_elf_flavour
8350 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8351 return TRUE;
8352
8353 return elf_section_type (asec) == elf_section_type (bsec);
8354 }
8355 \f
8356 /* Final phase of ELF linker. */
8357
8358 /* A structure we use to avoid passing large numbers of arguments. */
8359
8360 struct elf_final_link_info
8361 {
8362 /* General link information. */
8363 struct bfd_link_info *info;
8364 /* Output BFD. */
8365 bfd *output_bfd;
8366 /* Symbol string table. */
8367 struct elf_strtab_hash *symstrtab;
8368 /* .hash section. */
8369 asection *hash_sec;
8370 /* symbol version section (.gnu.version). */
8371 asection *symver_sec;
8372 /* Buffer large enough to hold contents of any section. */
8373 bfd_byte *contents;
8374 /* Buffer large enough to hold external relocs of any section. */
8375 void *external_relocs;
8376 /* Buffer large enough to hold internal relocs of any section. */
8377 Elf_Internal_Rela *internal_relocs;
8378 /* Buffer large enough to hold external local symbols of any input
8379 BFD. */
8380 bfd_byte *external_syms;
8381 /* And a buffer for symbol section indices. */
8382 Elf_External_Sym_Shndx *locsym_shndx;
8383 /* Buffer large enough to hold internal local symbols of any input
8384 BFD. */
8385 Elf_Internal_Sym *internal_syms;
8386 /* Array large enough to hold a symbol index for each local symbol
8387 of any input BFD. */
8388 long *indices;
8389 /* Array large enough to hold a section pointer for each local
8390 symbol of any input BFD. */
8391 asection **sections;
8392 /* Buffer for SHT_SYMTAB_SHNDX section. */
8393 Elf_External_Sym_Shndx *symshndxbuf;
8394 /* Number of STT_FILE syms seen. */
8395 size_t filesym_count;
8396 };
8397
8398 /* This struct is used to pass information to elf_link_output_extsym. */
8399
8400 struct elf_outext_info
8401 {
8402 bfd_boolean failed;
8403 bfd_boolean localsyms;
8404 bfd_boolean file_sym_done;
8405 struct elf_final_link_info *flinfo;
8406 };
8407
8408
8409 /* Support for evaluating a complex relocation.
8410
8411 Complex relocations are generalized, self-describing relocations. The
8412 implementation of them consists of two parts: complex symbols, and the
8413 relocations themselves.
8414
8415 The relocations are use a reserved elf-wide relocation type code (R_RELC
8416 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8417 information (start bit, end bit, word width, etc) into the addend. This
8418 information is extracted from CGEN-generated operand tables within gas.
8419
8420 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8421 internal) representing prefix-notation expressions, including but not
8422 limited to those sorts of expressions normally encoded as addends in the
8423 addend field. The symbol mangling format is:
8424
8425 <node> := <literal>
8426 | <unary-operator> ':' <node>
8427 | <binary-operator> ':' <node> ':' <node>
8428 ;
8429
8430 <literal> := 's' <digits=N> ':' <N character symbol name>
8431 | 'S' <digits=N> ':' <N character section name>
8432 | '#' <hexdigits>
8433 ;
8434
8435 <binary-operator> := as in C
8436 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8437
8438 static void
8439 set_symbol_value (bfd *bfd_with_globals,
8440 Elf_Internal_Sym *isymbuf,
8441 size_t locsymcount,
8442 size_t symidx,
8443 bfd_vma val)
8444 {
8445 struct elf_link_hash_entry **sym_hashes;
8446 struct elf_link_hash_entry *h;
8447 size_t extsymoff = locsymcount;
8448
8449 if (symidx < locsymcount)
8450 {
8451 Elf_Internal_Sym *sym;
8452
8453 sym = isymbuf + symidx;
8454 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8455 {
8456 /* It is a local symbol: move it to the
8457 "absolute" section and give it a value. */
8458 sym->st_shndx = SHN_ABS;
8459 sym->st_value = val;
8460 return;
8461 }
8462 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8463 extsymoff = 0;
8464 }
8465
8466 /* It is a global symbol: set its link type
8467 to "defined" and give it a value. */
8468
8469 sym_hashes = elf_sym_hashes (bfd_with_globals);
8470 h = sym_hashes [symidx - extsymoff];
8471 while (h->root.type == bfd_link_hash_indirect
8472 || h->root.type == bfd_link_hash_warning)
8473 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8474 h->root.type = bfd_link_hash_defined;
8475 h->root.u.def.value = val;
8476 h->root.u.def.section = bfd_abs_section_ptr;
8477 }
8478
8479 static bfd_boolean
8480 resolve_symbol (const char *name,
8481 bfd *input_bfd,
8482 struct elf_final_link_info *flinfo,
8483 bfd_vma *result,
8484 Elf_Internal_Sym *isymbuf,
8485 size_t locsymcount)
8486 {
8487 Elf_Internal_Sym *sym;
8488 struct bfd_link_hash_entry *global_entry;
8489 const char *candidate = NULL;
8490 Elf_Internal_Shdr *symtab_hdr;
8491 size_t i;
8492
8493 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8494
8495 for (i = 0; i < locsymcount; ++ i)
8496 {
8497 sym = isymbuf + i;
8498
8499 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8500 continue;
8501
8502 candidate = bfd_elf_string_from_elf_section (input_bfd,
8503 symtab_hdr->sh_link,
8504 sym->st_name);
8505 #ifdef DEBUG
8506 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8507 name, candidate, (unsigned long) sym->st_value);
8508 #endif
8509 if (candidate && strcmp (candidate, name) == 0)
8510 {
8511 asection *sec = flinfo->sections [i];
8512
8513 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8514 *result += sec->output_offset + sec->output_section->vma;
8515 #ifdef DEBUG
8516 printf ("Found symbol with value %8.8lx\n",
8517 (unsigned long) *result);
8518 #endif
8519 return TRUE;
8520 }
8521 }
8522
8523 /* Hmm, haven't found it yet. perhaps it is a global. */
8524 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8525 FALSE, FALSE, TRUE);
8526 if (!global_entry)
8527 return FALSE;
8528
8529 if (global_entry->type == bfd_link_hash_defined
8530 || global_entry->type == bfd_link_hash_defweak)
8531 {
8532 *result = (global_entry->u.def.value
8533 + global_entry->u.def.section->output_section->vma
8534 + global_entry->u.def.section->output_offset);
8535 #ifdef DEBUG
8536 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8537 global_entry->root.string, (unsigned long) *result);
8538 #endif
8539 return TRUE;
8540 }
8541
8542 return FALSE;
8543 }
8544
8545 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8546 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8547 names like "foo.end" which is the end address of section "foo". */
8548
8549 static bfd_boolean
8550 resolve_section (const char *name,
8551 asection *sections,
8552 bfd_vma *result,
8553 bfd * abfd)
8554 {
8555 asection *curr;
8556 unsigned int len;
8557
8558 for (curr = sections; curr; curr = curr->next)
8559 if (strcmp (curr->name, name) == 0)
8560 {
8561 *result = curr->vma;
8562 return TRUE;
8563 }
8564
8565 /* Hmm. still haven't found it. try pseudo-section names. */
8566 /* FIXME: This could be coded more efficiently... */
8567 for (curr = sections; curr; curr = curr->next)
8568 {
8569 len = strlen (curr->name);
8570 if (len > strlen (name))
8571 continue;
8572
8573 if (strncmp (curr->name, name, len) == 0)
8574 {
8575 if (strncmp (".end", name + len, 4) == 0)
8576 {
8577 *result = (curr->vma
8578 + curr->size / bfd_octets_per_byte (abfd, curr));
8579 return TRUE;
8580 }
8581
8582 /* Insert more pseudo-section names here, if you like. */
8583 }
8584 }
8585
8586 return FALSE;
8587 }
8588
8589 static void
8590 undefined_reference (const char *reftype, const char *name)
8591 {
8592 /* xgettext:c-format */
8593 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8594 reftype, name);
8595 }
8596
8597 static bfd_boolean
8598 eval_symbol (bfd_vma *result,
8599 const char **symp,
8600 bfd *input_bfd,
8601 struct elf_final_link_info *flinfo,
8602 bfd_vma dot,
8603 Elf_Internal_Sym *isymbuf,
8604 size_t locsymcount,
8605 int signed_p)
8606 {
8607 size_t len;
8608 size_t symlen;
8609 bfd_vma a;
8610 bfd_vma b;
8611 char symbuf[4096];
8612 const char *sym = *symp;
8613 const char *symend;
8614 bfd_boolean symbol_is_section = FALSE;
8615
8616 len = strlen (sym);
8617 symend = sym + len;
8618
8619 if (len < 1 || len > sizeof (symbuf))
8620 {
8621 bfd_set_error (bfd_error_invalid_operation);
8622 return FALSE;
8623 }
8624
8625 switch (* sym)
8626 {
8627 case '.':
8628 *result = dot;
8629 *symp = sym + 1;
8630 return TRUE;
8631
8632 case '#':
8633 ++sym;
8634 *result = strtoul (sym, (char **) symp, 16);
8635 return TRUE;
8636
8637 case 'S':
8638 symbol_is_section = TRUE;
8639 /* Fall through. */
8640 case 's':
8641 ++sym;
8642 symlen = strtol (sym, (char **) symp, 10);
8643 sym = *symp + 1; /* Skip the trailing ':'. */
8644
8645 if (symend < sym || symlen + 1 > sizeof (symbuf))
8646 {
8647 bfd_set_error (bfd_error_invalid_operation);
8648 return FALSE;
8649 }
8650
8651 memcpy (symbuf, sym, symlen);
8652 symbuf[symlen] = '\0';
8653 *symp = sym + symlen;
8654
8655 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8656 the symbol as a section, or vice-versa. so we're pretty liberal in our
8657 interpretation here; section means "try section first", not "must be a
8658 section", and likewise with symbol. */
8659
8660 if (symbol_is_section)
8661 {
8662 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8663 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8664 isymbuf, locsymcount))
8665 {
8666 undefined_reference ("section", symbuf);
8667 return FALSE;
8668 }
8669 }
8670 else
8671 {
8672 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8673 isymbuf, locsymcount)
8674 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8675 result, input_bfd))
8676 {
8677 undefined_reference ("symbol", symbuf);
8678 return FALSE;
8679 }
8680 }
8681
8682 return TRUE;
8683
8684 /* All that remains are operators. */
8685
8686 #define UNARY_OP(op) \
8687 if (strncmp (sym, #op, strlen (#op)) == 0) \
8688 { \
8689 sym += strlen (#op); \
8690 if (*sym == ':') \
8691 ++sym; \
8692 *symp = sym; \
8693 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8694 isymbuf, locsymcount, signed_p)) \
8695 return FALSE; \
8696 if (signed_p) \
8697 *result = op ((bfd_signed_vma) a); \
8698 else \
8699 *result = op a; \
8700 return TRUE; \
8701 }
8702
8703 #define BINARY_OP(op) \
8704 if (strncmp (sym, #op, strlen (#op)) == 0) \
8705 { \
8706 sym += strlen (#op); \
8707 if (*sym == ':') \
8708 ++sym; \
8709 *symp = sym; \
8710 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8711 isymbuf, locsymcount, signed_p)) \
8712 return FALSE; \
8713 ++*symp; \
8714 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8715 isymbuf, locsymcount, signed_p)) \
8716 return FALSE; \
8717 if (signed_p) \
8718 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8719 else \
8720 *result = a op b; \
8721 return TRUE; \
8722 }
8723
8724 default:
8725 UNARY_OP (0-);
8726 BINARY_OP (<<);
8727 BINARY_OP (>>);
8728 BINARY_OP (==);
8729 BINARY_OP (!=);
8730 BINARY_OP (<=);
8731 BINARY_OP (>=);
8732 BINARY_OP (&&);
8733 BINARY_OP (||);
8734 UNARY_OP (~);
8735 UNARY_OP (!);
8736 BINARY_OP (*);
8737 BINARY_OP (/);
8738 BINARY_OP (%);
8739 BINARY_OP (^);
8740 BINARY_OP (|);
8741 BINARY_OP (&);
8742 BINARY_OP (+);
8743 BINARY_OP (-);
8744 BINARY_OP (<);
8745 BINARY_OP (>);
8746 #undef UNARY_OP
8747 #undef BINARY_OP
8748 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8749 bfd_set_error (bfd_error_invalid_operation);
8750 return FALSE;
8751 }
8752 }
8753
8754 static void
8755 put_value (bfd_vma size,
8756 unsigned long chunksz,
8757 bfd *input_bfd,
8758 bfd_vma x,
8759 bfd_byte *location)
8760 {
8761 location += (size - chunksz);
8762
8763 for (; size; size -= chunksz, location -= chunksz)
8764 {
8765 switch (chunksz)
8766 {
8767 case 1:
8768 bfd_put_8 (input_bfd, x, location);
8769 x >>= 8;
8770 break;
8771 case 2:
8772 bfd_put_16 (input_bfd, x, location);
8773 x >>= 16;
8774 break;
8775 case 4:
8776 bfd_put_32 (input_bfd, x, location);
8777 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8778 x >>= 16;
8779 x >>= 16;
8780 break;
8781 #ifdef BFD64
8782 case 8:
8783 bfd_put_64 (input_bfd, x, location);
8784 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8785 x >>= 32;
8786 x >>= 32;
8787 break;
8788 #endif
8789 default:
8790 abort ();
8791 break;
8792 }
8793 }
8794 }
8795
8796 static bfd_vma
8797 get_value (bfd_vma size,
8798 unsigned long chunksz,
8799 bfd *input_bfd,
8800 bfd_byte *location)
8801 {
8802 int shift;
8803 bfd_vma x = 0;
8804
8805 /* Sanity checks. */
8806 BFD_ASSERT (chunksz <= sizeof (x)
8807 && size >= chunksz
8808 && chunksz != 0
8809 && (size % chunksz) == 0
8810 && input_bfd != NULL
8811 && location != NULL);
8812
8813 if (chunksz == sizeof (x))
8814 {
8815 BFD_ASSERT (size == chunksz);
8816
8817 /* Make sure that we do not perform an undefined shift operation.
8818 We know that size == chunksz so there will only be one iteration
8819 of the loop below. */
8820 shift = 0;
8821 }
8822 else
8823 shift = 8 * chunksz;
8824
8825 for (; size; size -= chunksz, location += chunksz)
8826 {
8827 switch (chunksz)
8828 {
8829 case 1:
8830 x = (x << shift) | bfd_get_8 (input_bfd, location);
8831 break;
8832 case 2:
8833 x = (x << shift) | bfd_get_16 (input_bfd, location);
8834 break;
8835 case 4:
8836 x = (x << shift) | bfd_get_32 (input_bfd, location);
8837 break;
8838 #ifdef BFD64
8839 case 8:
8840 x = (x << shift) | bfd_get_64 (input_bfd, location);
8841 break;
8842 #endif
8843 default:
8844 abort ();
8845 }
8846 }
8847 return x;
8848 }
8849
8850 static void
8851 decode_complex_addend (unsigned long *start, /* in bits */
8852 unsigned long *oplen, /* in bits */
8853 unsigned long *len, /* in bits */
8854 unsigned long *wordsz, /* in bytes */
8855 unsigned long *chunksz, /* in bytes */
8856 unsigned long *lsb0_p,
8857 unsigned long *signed_p,
8858 unsigned long *trunc_p,
8859 unsigned long encoded)
8860 {
8861 * start = encoded & 0x3F;
8862 * len = (encoded >> 6) & 0x3F;
8863 * oplen = (encoded >> 12) & 0x3F;
8864 * wordsz = (encoded >> 18) & 0xF;
8865 * chunksz = (encoded >> 22) & 0xF;
8866 * lsb0_p = (encoded >> 27) & 1;
8867 * signed_p = (encoded >> 28) & 1;
8868 * trunc_p = (encoded >> 29) & 1;
8869 }
8870
8871 bfd_reloc_status_type
8872 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8873 asection *input_section,
8874 bfd_byte *contents,
8875 Elf_Internal_Rela *rel,
8876 bfd_vma relocation)
8877 {
8878 bfd_vma shift, x, mask;
8879 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8880 bfd_reloc_status_type r;
8881 bfd_size_type octets;
8882
8883 /* Perform this reloc, since it is complex.
8884 (this is not to say that it necessarily refers to a complex
8885 symbol; merely that it is a self-describing CGEN based reloc.
8886 i.e. the addend has the complete reloc information (bit start, end,
8887 word size, etc) encoded within it.). */
8888
8889 decode_complex_addend (&start, &oplen, &len, &wordsz,
8890 &chunksz, &lsb0_p, &signed_p,
8891 &trunc_p, rel->r_addend);
8892
8893 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8894
8895 if (lsb0_p)
8896 shift = (start + 1) - len;
8897 else
8898 shift = (8 * wordsz) - (start + len);
8899
8900 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8901 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8902
8903 #ifdef DEBUG
8904 printf ("Doing complex reloc: "
8905 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8906 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8907 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8908 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8909 oplen, (unsigned long) x, (unsigned long) mask,
8910 (unsigned long) relocation);
8911 #endif
8912
8913 r = bfd_reloc_ok;
8914 if (! trunc_p)
8915 /* Now do an overflow check. */
8916 r = bfd_check_overflow ((signed_p
8917 ? complain_overflow_signed
8918 : complain_overflow_unsigned),
8919 len, 0, (8 * wordsz),
8920 relocation);
8921
8922 /* Do the deed. */
8923 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8924
8925 #ifdef DEBUG
8926 printf (" relocation: %8.8lx\n"
8927 " shifted mask: %8.8lx\n"
8928 " shifted/masked reloc: %8.8lx\n"
8929 " result: %8.8lx\n",
8930 (unsigned long) relocation, (unsigned long) (mask << shift),
8931 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8932 #endif
8933 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8934 return r;
8935 }
8936
8937 /* Functions to read r_offset from external (target order) reloc
8938 entry. Faster than bfd_getl32 et al, because we let the compiler
8939 know the value is aligned. */
8940
8941 static bfd_vma
8942 ext32l_r_offset (const void *p)
8943 {
8944 union aligned32
8945 {
8946 uint32_t v;
8947 unsigned char c[4];
8948 };
8949 const union aligned32 *a
8950 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8951
8952 uint32_t aval = ( (uint32_t) a->c[0]
8953 | (uint32_t) a->c[1] << 8
8954 | (uint32_t) a->c[2] << 16
8955 | (uint32_t) a->c[3] << 24);
8956 return aval;
8957 }
8958
8959 static bfd_vma
8960 ext32b_r_offset (const void *p)
8961 {
8962 union aligned32
8963 {
8964 uint32_t v;
8965 unsigned char c[4];
8966 };
8967 const union aligned32 *a
8968 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8969
8970 uint32_t aval = ( (uint32_t) a->c[0] << 24
8971 | (uint32_t) a->c[1] << 16
8972 | (uint32_t) a->c[2] << 8
8973 | (uint32_t) a->c[3]);
8974 return aval;
8975 }
8976
8977 #ifdef BFD_HOST_64_BIT
8978 static bfd_vma
8979 ext64l_r_offset (const void *p)
8980 {
8981 union aligned64
8982 {
8983 uint64_t v;
8984 unsigned char c[8];
8985 };
8986 const union aligned64 *a
8987 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8988
8989 uint64_t aval = ( (uint64_t) a->c[0]
8990 | (uint64_t) a->c[1] << 8
8991 | (uint64_t) a->c[2] << 16
8992 | (uint64_t) a->c[3] << 24
8993 | (uint64_t) a->c[4] << 32
8994 | (uint64_t) a->c[5] << 40
8995 | (uint64_t) a->c[6] << 48
8996 | (uint64_t) a->c[7] << 56);
8997 return aval;
8998 }
8999
9000 static bfd_vma
9001 ext64b_r_offset (const void *p)
9002 {
9003 union aligned64
9004 {
9005 uint64_t v;
9006 unsigned char c[8];
9007 };
9008 const union aligned64 *a
9009 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9010
9011 uint64_t aval = ( (uint64_t) a->c[0] << 56
9012 | (uint64_t) a->c[1] << 48
9013 | (uint64_t) a->c[2] << 40
9014 | (uint64_t) a->c[3] << 32
9015 | (uint64_t) a->c[4] << 24
9016 | (uint64_t) a->c[5] << 16
9017 | (uint64_t) a->c[6] << 8
9018 | (uint64_t) a->c[7]);
9019 return aval;
9020 }
9021 #endif
9022
9023 /* When performing a relocatable link, the input relocations are
9024 preserved. But, if they reference global symbols, the indices
9025 referenced must be updated. Update all the relocations found in
9026 RELDATA. */
9027
9028 static bfd_boolean
9029 elf_link_adjust_relocs (bfd *abfd,
9030 asection *sec,
9031 struct bfd_elf_section_reloc_data *reldata,
9032 bfd_boolean sort,
9033 struct bfd_link_info *info)
9034 {
9035 unsigned int i;
9036 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9037 bfd_byte *erela;
9038 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9039 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9040 bfd_vma r_type_mask;
9041 int r_sym_shift;
9042 unsigned int count = reldata->count;
9043 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9044
9045 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9046 {
9047 swap_in = bed->s->swap_reloc_in;
9048 swap_out = bed->s->swap_reloc_out;
9049 }
9050 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9051 {
9052 swap_in = bed->s->swap_reloca_in;
9053 swap_out = bed->s->swap_reloca_out;
9054 }
9055 else
9056 abort ();
9057
9058 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9059 abort ();
9060
9061 if (bed->s->arch_size == 32)
9062 {
9063 r_type_mask = 0xff;
9064 r_sym_shift = 8;
9065 }
9066 else
9067 {
9068 r_type_mask = 0xffffffff;
9069 r_sym_shift = 32;
9070 }
9071
9072 erela = reldata->hdr->contents;
9073 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9074 {
9075 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9076 unsigned int j;
9077
9078 if (*rel_hash == NULL)
9079 continue;
9080
9081 if ((*rel_hash)->indx == -2
9082 && info->gc_sections
9083 && ! info->gc_keep_exported)
9084 {
9085 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9086 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9087 abfd, sec,
9088 (*rel_hash)->root.root.string);
9089 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9090 abfd, sec);
9091 bfd_set_error (bfd_error_invalid_operation);
9092 return FALSE;
9093 }
9094 BFD_ASSERT ((*rel_hash)->indx >= 0);
9095
9096 (*swap_in) (abfd, erela, irela);
9097 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9098 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9099 | (irela[j].r_info & r_type_mask));
9100 (*swap_out) (abfd, irela, erela);
9101 }
9102
9103 if (bed->elf_backend_update_relocs)
9104 (*bed->elf_backend_update_relocs) (sec, reldata);
9105
9106 if (sort && count != 0)
9107 {
9108 bfd_vma (*ext_r_off) (const void *);
9109 bfd_vma r_off;
9110 size_t elt_size;
9111 bfd_byte *base, *end, *p, *loc;
9112 bfd_byte *buf = NULL;
9113
9114 if (bed->s->arch_size == 32)
9115 {
9116 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9117 ext_r_off = ext32l_r_offset;
9118 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9119 ext_r_off = ext32b_r_offset;
9120 else
9121 abort ();
9122 }
9123 else
9124 {
9125 #ifdef BFD_HOST_64_BIT
9126 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9127 ext_r_off = ext64l_r_offset;
9128 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9129 ext_r_off = ext64b_r_offset;
9130 else
9131 #endif
9132 abort ();
9133 }
9134
9135 /* Must use a stable sort here. A modified insertion sort,
9136 since the relocs are mostly sorted already. */
9137 elt_size = reldata->hdr->sh_entsize;
9138 base = reldata->hdr->contents;
9139 end = base + count * elt_size;
9140 if (elt_size > sizeof (Elf64_External_Rela))
9141 abort ();
9142
9143 /* Ensure the first element is lowest. This acts as a sentinel,
9144 speeding the main loop below. */
9145 r_off = (*ext_r_off) (base);
9146 for (p = loc = base; (p += elt_size) < end; )
9147 {
9148 bfd_vma r_off2 = (*ext_r_off) (p);
9149 if (r_off > r_off2)
9150 {
9151 r_off = r_off2;
9152 loc = p;
9153 }
9154 }
9155 if (loc != base)
9156 {
9157 /* Don't just swap *base and *loc as that changes the order
9158 of the original base[0] and base[1] if they happen to
9159 have the same r_offset. */
9160 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9161 memcpy (onebuf, loc, elt_size);
9162 memmove (base + elt_size, base, loc - base);
9163 memcpy (base, onebuf, elt_size);
9164 }
9165
9166 for (p = base + elt_size; (p += elt_size) < end; )
9167 {
9168 /* base to p is sorted, *p is next to insert. */
9169 r_off = (*ext_r_off) (p);
9170 /* Search the sorted region for location to insert. */
9171 loc = p - elt_size;
9172 while (r_off < (*ext_r_off) (loc))
9173 loc -= elt_size;
9174 loc += elt_size;
9175 if (loc != p)
9176 {
9177 /* Chances are there is a run of relocs to insert here,
9178 from one of more input files. Files are not always
9179 linked in order due to the way elf_link_input_bfd is
9180 called. See pr17666. */
9181 size_t sortlen = p - loc;
9182 bfd_vma r_off2 = (*ext_r_off) (loc);
9183 size_t runlen = elt_size;
9184 size_t buf_size = 96 * 1024;
9185 while (p + runlen < end
9186 && (sortlen <= buf_size
9187 || runlen + elt_size <= buf_size)
9188 && r_off2 > (*ext_r_off) (p + runlen))
9189 runlen += elt_size;
9190 if (buf == NULL)
9191 {
9192 buf = bfd_malloc (buf_size);
9193 if (buf == NULL)
9194 return FALSE;
9195 }
9196 if (runlen < sortlen)
9197 {
9198 memcpy (buf, p, runlen);
9199 memmove (loc + runlen, loc, sortlen);
9200 memcpy (loc, buf, runlen);
9201 }
9202 else
9203 {
9204 memcpy (buf, loc, sortlen);
9205 memmove (loc, p, runlen);
9206 memcpy (loc + runlen, buf, sortlen);
9207 }
9208 p += runlen - elt_size;
9209 }
9210 }
9211 /* Hashes are no longer valid. */
9212 free (reldata->hashes);
9213 reldata->hashes = NULL;
9214 free (buf);
9215 }
9216 return TRUE;
9217 }
9218
9219 struct elf_link_sort_rela
9220 {
9221 union {
9222 bfd_vma offset;
9223 bfd_vma sym_mask;
9224 } u;
9225 enum elf_reloc_type_class type;
9226 /* We use this as an array of size int_rels_per_ext_rel. */
9227 Elf_Internal_Rela rela[1];
9228 };
9229
9230 /* qsort stability here and for cmp2 is only an issue if multiple
9231 dynamic relocations are emitted at the same address. But targets
9232 that apply a series of dynamic relocations each operating on the
9233 result of the prior relocation can't use -z combreloc as
9234 implemented anyway. Such schemes tend to be broken by sorting on
9235 symbol index. That leaves dynamic NONE relocs as the only other
9236 case where ld might emit multiple relocs at the same address, and
9237 those are only emitted due to target bugs. */
9238
9239 static int
9240 elf_link_sort_cmp1 (const void *A, const void *B)
9241 {
9242 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9243 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9244 int relativea, relativeb;
9245
9246 relativea = a->type == reloc_class_relative;
9247 relativeb = b->type == reloc_class_relative;
9248
9249 if (relativea < relativeb)
9250 return 1;
9251 if (relativea > relativeb)
9252 return -1;
9253 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9254 return -1;
9255 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9256 return 1;
9257 if (a->rela->r_offset < b->rela->r_offset)
9258 return -1;
9259 if (a->rela->r_offset > b->rela->r_offset)
9260 return 1;
9261 return 0;
9262 }
9263
9264 static int
9265 elf_link_sort_cmp2 (const void *A, const void *B)
9266 {
9267 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9268 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9269
9270 if (a->type < b->type)
9271 return -1;
9272 if (a->type > b->type)
9273 return 1;
9274 if (a->u.offset < b->u.offset)
9275 return -1;
9276 if (a->u.offset > b->u.offset)
9277 return 1;
9278 if (a->rela->r_offset < b->rela->r_offset)
9279 return -1;
9280 if (a->rela->r_offset > b->rela->r_offset)
9281 return 1;
9282 return 0;
9283 }
9284
9285 static size_t
9286 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9287 {
9288 asection *dynamic_relocs;
9289 asection *rela_dyn;
9290 asection *rel_dyn;
9291 bfd_size_type count, size;
9292 size_t i, ret, sort_elt, ext_size;
9293 bfd_byte *sort, *s_non_relative, *p;
9294 struct elf_link_sort_rela *sq;
9295 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9296 int i2e = bed->s->int_rels_per_ext_rel;
9297 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9298 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9299 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9300 struct bfd_link_order *lo;
9301 bfd_vma r_sym_mask;
9302 bfd_boolean use_rela;
9303
9304 /* Find a dynamic reloc section. */
9305 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9306 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9307 if (rela_dyn != NULL && rela_dyn->size > 0
9308 && rel_dyn != NULL && rel_dyn->size > 0)
9309 {
9310 bfd_boolean use_rela_initialised = FALSE;
9311
9312 /* This is just here to stop gcc from complaining.
9313 Its initialization checking code is not perfect. */
9314 use_rela = TRUE;
9315
9316 /* Both sections are present. Examine the sizes
9317 of the indirect sections to help us choose. */
9318 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9319 if (lo->type == bfd_indirect_link_order)
9320 {
9321 asection *o = lo->u.indirect.section;
9322
9323 if ((o->size % bed->s->sizeof_rela) == 0)
9324 {
9325 if ((o->size % bed->s->sizeof_rel) == 0)
9326 /* Section size is divisible by both rel and rela sizes.
9327 It is of no help to us. */
9328 ;
9329 else
9330 {
9331 /* Section size is only divisible by rela. */
9332 if (use_rela_initialised && !use_rela)
9333 {
9334 _bfd_error_handler (_("%pB: unable to sort relocs - "
9335 "they are in more than one size"),
9336 abfd);
9337 bfd_set_error (bfd_error_invalid_operation);
9338 return 0;
9339 }
9340 else
9341 {
9342 use_rela = TRUE;
9343 use_rela_initialised = TRUE;
9344 }
9345 }
9346 }
9347 else if ((o->size % bed->s->sizeof_rel) == 0)
9348 {
9349 /* Section size is only divisible by rel. */
9350 if (use_rela_initialised && use_rela)
9351 {
9352 _bfd_error_handler (_("%pB: unable to sort relocs - "
9353 "they are in more than one size"),
9354 abfd);
9355 bfd_set_error (bfd_error_invalid_operation);
9356 return 0;
9357 }
9358 else
9359 {
9360 use_rela = FALSE;
9361 use_rela_initialised = TRUE;
9362 }
9363 }
9364 else
9365 {
9366 /* The section size is not divisible by either -
9367 something is wrong. */
9368 _bfd_error_handler (_("%pB: unable to sort relocs - "
9369 "they are of an unknown size"), abfd);
9370 bfd_set_error (bfd_error_invalid_operation);
9371 return 0;
9372 }
9373 }
9374
9375 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9376 if (lo->type == bfd_indirect_link_order)
9377 {
9378 asection *o = lo->u.indirect.section;
9379
9380 if ((o->size % bed->s->sizeof_rela) == 0)
9381 {
9382 if ((o->size % bed->s->sizeof_rel) == 0)
9383 /* Section size is divisible by both rel and rela sizes.
9384 It is of no help to us. */
9385 ;
9386 else
9387 {
9388 /* Section size is only divisible by rela. */
9389 if (use_rela_initialised && !use_rela)
9390 {
9391 _bfd_error_handler (_("%pB: unable to sort relocs - "
9392 "they are in more than one size"),
9393 abfd);
9394 bfd_set_error (bfd_error_invalid_operation);
9395 return 0;
9396 }
9397 else
9398 {
9399 use_rela = TRUE;
9400 use_rela_initialised = TRUE;
9401 }
9402 }
9403 }
9404 else if ((o->size % bed->s->sizeof_rel) == 0)
9405 {
9406 /* Section size is only divisible by rel. */
9407 if (use_rela_initialised && use_rela)
9408 {
9409 _bfd_error_handler (_("%pB: unable to sort relocs - "
9410 "they are in more than one size"),
9411 abfd);
9412 bfd_set_error (bfd_error_invalid_operation);
9413 return 0;
9414 }
9415 else
9416 {
9417 use_rela = FALSE;
9418 use_rela_initialised = TRUE;
9419 }
9420 }
9421 else
9422 {
9423 /* The section size is not divisible by either -
9424 something is wrong. */
9425 _bfd_error_handler (_("%pB: unable to sort relocs - "
9426 "they are of an unknown size"), abfd);
9427 bfd_set_error (bfd_error_invalid_operation);
9428 return 0;
9429 }
9430 }
9431
9432 if (! use_rela_initialised)
9433 /* Make a guess. */
9434 use_rela = TRUE;
9435 }
9436 else if (rela_dyn != NULL && rela_dyn->size > 0)
9437 use_rela = TRUE;
9438 else if (rel_dyn != NULL && rel_dyn->size > 0)
9439 use_rela = FALSE;
9440 else
9441 return 0;
9442
9443 if (use_rela)
9444 {
9445 dynamic_relocs = rela_dyn;
9446 ext_size = bed->s->sizeof_rela;
9447 swap_in = bed->s->swap_reloca_in;
9448 swap_out = bed->s->swap_reloca_out;
9449 }
9450 else
9451 {
9452 dynamic_relocs = rel_dyn;
9453 ext_size = bed->s->sizeof_rel;
9454 swap_in = bed->s->swap_reloc_in;
9455 swap_out = bed->s->swap_reloc_out;
9456 }
9457
9458 size = 0;
9459 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9460 if (lo->type == bfd_indirect_link_order)
9461 size += lo->u.indirect.section->size;
9462
9463 if (size != dynamic_relocs->size)
9464 return 0;
9465
9466 sort_elt = (sizeof (struct elf_link_sort_rela)
9467 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9468
9469 count = dynamic_relocs->size / ext_size;
9470 if (count == 0)
9471 return 0;
9472 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9473
9474 if (sort == NULL)
9475 {
9476 (*info->callbacks->warning)
9477 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9478 return 0;
9479 }
9480
9481 if (bed->s->arch_size == 32)
9482 r_sym_mask = ~(bfd_vma) 0xff;
9483 else
9484 r_sym_mask = ~(bfd_vma) 0xffffffff;
9485
9486 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9487 if (lo->type == bfd_indirect_link_order)
9488 {
9489 bfd_byte *erel, *erelend;
9490 asection *o = lo->u.indirect.section;
9491
9492 if (o->contents == NULL && o->size != 0)
9493 {
9494 /* This is a reloc section that is being handled as a normal
9495 section. See bfd_section_from_shdr. We can't combine
9496 relocs in this case. */
9497 free (sort);
9498 return 0;
9499 }
9500 erel = o->contents;
9501 erelend = o->contents + o->size;
9502 p = sort + o->output_offset * opb / ext_size * sort_elt;
9503
9504 while (erel < erelend)
9505 {
9506 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9507
9508 (*swap_in) (abfd, erel, s->rela);
9509 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9510 s->u.sym_mask = r_sym_mask;
9511 p += sort_elt;
9512 erel += ext_size;
9513 }
9514 }
9515
9516 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9517
9518 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9519 {
9520 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9521 if (s->type != reloc_class_relative)
9522 break;
9523 }
9524 ret = i;
9525 s_non_relative = p;
9526
9527 sq = (struct elf_link_sort_rela *) s_non_relative;
9528 for (; i < count; i++, p += sort_elt)
9529 {
9530 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9531 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9532 sq = sp;
9533 sp->u.offset = sq->rela->r_offset;
9534 }
9535
9536 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9537
9538 struct elf_link_hash_table *htab = elf_hash_table (info);
9539 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9540 {
9541 /* We have plt relocs in .rela.dyn. */
9542 sq = (struct elf_link_sort_rela *) sort;
9543 for (i = 0; i < count; i++)
9544 if (sq[count - i - 1].type != reloc_class_plt)
9545 break;
9546 if (i != 0 && htab->srelplt->size == i * ext_size)
9547 {
9548 struct bfd_link_order **plo;
9549 /* Put srelplt link_order last. This is so the output_offset
9550 set in the next loop is correct for DT_JMPREL. */
9551 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9552 if ((*plo)->type == bfd_indirect_link_order
9553 && (*plo)->u.indirect.section == htab->srelplt)
9554 {
9555 lo = *plo;
9556 *plo = lo->next;
9557 }
9558 else
9559 plo = &(*plo)->next;
9560 *plo = lo;
9561 lo->next = NULL;
9562 dynamic_relocs->map_tail.link_order = lo;
9563 }
9564 }
9565
9566 p = sort;
9567 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9568 if (lo->type == bfd_indirect_link_order)
9569 {
9570 bfd_byte *erel, *erelend;
9571 asection *o = lo->u.indirect.section;
9572
9573 erel = o->contents;
9574 erelend = o->contents + o->size;
9575 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9576 while (erel < erelend)
9577 {
9578 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9579 (*swap_out) (abfd, s->rela, erel);
9580 p += sort_elt;
9581 erel += ext_size;
9582 }
9583 }
9584
9585 free (sort);
9586 *psec = dynamic_relocs;
9587 return ret;
9588 }
9589
9590 /* Add a symbol to the output symbol string table. */
9591
9592 static int
9593 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9594 const char *name,
9595 Elf_Internal_Sym *elfsym,
9596 asection *input_sec,
9597 struct elf_link_hash_entry *h)
9598 {
9599 int (*output_symbol_hook)
9600 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9601 struct elf_link_hash_entry *);
9602 struct elf_link_hash_table *hash_table;
9603 const struct elf_backend_data *bed;
9604 bfd_size_type strtabsize;
9605
9606 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9607
9608 bed = get_elf_backend_data (flinfo->output_bfd);
9609 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9610 if (output_symbol_hook != NULL)
9611 {
9612 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9613 if (ret != 1)
9614 return ret;
9615 }
9616
9617 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9618 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9619 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9620 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9621
9622 if (name == NULL
9623 || *name == '\0'
9624 || (input_sec->flags & SEC_EXCLUDE))
9625 elfsym->st_name = (unsigned long) -1;
9626 else
9627 {
9628 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9629 to get the final offset for st_name. */
9630 elfsym->st_name
9631 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9632 name, FALSE);
9633 if (elfsym->st_name == (unsigned long) -1)
9634 return 0;
9635 }
9636
9637 hash_table = elf_hash_table (flinfo->info);
9638 strtabsize = hash_table->strtabsize;
9639 if (strtabsize <= hash_table->strtabcount)
9640 {
9641 strtabsize += strtabsize;
9642 hash_table->strtabsize = strtabsize;
9643 strtabsize *= sizeof (*hash_table->strtab);
9644 hash_table->strtab
9645 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9646 strtabsize);
9647 if (hash_table->strtab == NULL)
9648 return 0;
9649 }
9650 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9651 hash_table->strtab[hash_table->strtabcount].dest_index
9652 = hash_table->strtabcount;
9653 hash_table->strtab[hash_table->strtabcount].destshndx_index
9654 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9655
9656 flinfo->output_bfd->symcount += 1;
9657 hash_table->strtabcount += 1;
9658
9659 return 1;
9660 }
9661
9662 /* Swap symbols out to the symbol table and flush the output symbols to
9663 the file. */
9664
9665 static bfd_boolean
9666 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9667 {
9668 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9669 size_t amt;
9670 size_t i;
9671 const struct elf_backend_data *bed;
9672 bfd_byte *symbuf;
9673 Elf_Internal_Shdr *hdr;
9674 file_ptr pos;
9675 bfd_boolean ret;
9676
9677 if (!hash_table->strtabcount)
9678 return TRUE;
9679
9680 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9681
9682 bed = get_elf_backend_data (flinfo->output_bfd);
9683
9684 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9685 symbuf = (bfd_byte *) bfd_malloc (amt);
9686 if (symbuf == NULL)
9687 return FALSE;
9688
9689 if (flinfo->symshndxbuf)
9690 {
9691 amt = sizeof (Elf_External_Sym_Shndx);
9692 amt *= bfd_get_symcount (flinfo->output_bfd);
9693 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9694 if (flinfo->symshndxbuf == NULL)
9695 {
9696 free (symbuf);
9697 return FALSE;
9698 }
9699 }
9700
9701 for (i = 0; i < hash_table->strtabcount; i++)
9702 {
9703 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9704 if (elfsym->sym.st_name == (unsigned long) -1)
9705 elfsym->sym.st_name = 0;
9706 else
9707 elfsym->sym.st_name
9708 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9709 elfsym->sym.st_name);
9710 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9711 ((bfd_byte *) symbuf
9712 + (elfsym->dest_index
9713 * bed->s->sizeof_sym)),
9714 (flinfo->symshndxbuf
9715 + elfsym->destshndx_index));
9716 }
9717
9718 /* Allow the linker to examine the strtab and symtab now they are
9719 populated. */
9720
9721 if (flinfo->info->callbacks->examine_strtab)
9722 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9723 hash_table->strtabcount,
9724 flinfo->symstrtab);
9725
9726 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9727 pos = hdr->sh_offset + hdr->sh_size;
9728 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9729 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9730 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9731 {
9732 hdr->sh_size += amt;
9733 ret = TRUE;
9734 }
9735 else
9736 ret = FALSE;
9737
9738 free (symbuf);
9739
9740 free (hash_table->strtab);
9741 hash_table->strtab = NULL;
9742
9743 return ret;
9744 }
9745
9746 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9747
9748 static bfd_boolean
9749 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9750 {
9751 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9752 && sym->st_shndx < SHN_LORESERVE)
9753 {
9754 /* The gABI doesn't support dynamic symbols in output sections
9755 beyond 64k. */
9756 _bfd_error_handler
9757 /* xgettext:c-format */
9758 (_("%pB: too many sections: %d (>= %d)"),
9759 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9760 bfd_set_error (bfd_error_nonrepresentable_section);
9761 return FALSE;
9762 }
9763 return TRUE;
9764 }
9765
9766 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9767 allowing an unsatisfied unversioned symbol in the DSO to match a
9768 versioned symbol that would normally require an explicit version.
9769 We also handle the case that a DSO references a hidden symbol
9770 which may be satisfied by a versioned symbol in another DSO. */
9771
9772 static bfd_boolean
9773 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9774 const struct elf_backend_data *bed,
9775 struct elf_link_hash_entry *h)
9776 {
9777 bfd *abfd;
9778 struct elf_link_loaded_list *loaded;
9779
9780 if (!is_elf_hash_table (info->hash))
9781 return FALSE;
9782
9783 /* Check indirect symbol. */
9784 while (h->root.type == bfd_link_hash_indirect)
9785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9786
9787 switch (h->root.type)
9788 {
9789 default:
9790 abfd = NULL;
9791 break;
9792
9793 case bfd_link_hash_undefined:
9794 case bfd_link_hash_undefweak:
9795 abfd = h->root.u.undef.abfd;
9796 if (abfd == NULL
9797 || (abfd->flags & DYNAMIC) == 0
9798 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9799 return FALSE;
9800 break;
9801
9802 case bfd_link_hash_defined:
9803 case bfd_link_hash_defweak:
9804 abfd = h->root.u.def.section->owner;
9805 break;
9806
9807 case bfd_link_hash_common:
9808 abfd = h->root.u.c.p->section->owner;
9809 break;
9810 }
9811 BFD_ASSERT (abfd != NULL);
9812
9813 for (loaded = elf_hash_table (info)->dyn_loaded;
9814 loaded != NULL;
9815 loaded = loaded->next)
9816 {
9817 bfd *input;
9818 Elf_Internal_Shdr *hdr;
9819 size_t symcount;
9820 size_t extsymcount;
9821 size_t extsymoff;
9822 Elf_Internal_Shdr *versymhdr;
9823 Elf_Internal_Sym *isym;
9824 Elf_Internal_Sym *isymend;
9825 Elf_Internal_Sym *isymbuf;
9826 Elf_External_Versym *ever;
9827 Elf_External_Versym *extversym;
9828
9829 input = loaded->abfd;
9830
9831 /* We check each DSO for a possible hidden versioned definition. */
9832 if (input == abfd
9833 || elf_dynversym (input) == 0)
9834 continue;
9835
9836 hdr = &elf_tdata (input)->dynsymtab_hdr;
9837
9838 symcount = hdr->sh_size / bed->s->sizeof_sym;
9839 if (elf_bad_symtab (input))
9840 {
9841 extsymcount = symcount;
9842 extsymoff = 0;
9843 }
9844 else
9845 {
9846 extsymcount = symcount - hdr->sh_info;
9847 extsymoff = hdr->sh_info;
9848 }
9849
9850 if (extsymcount == 0)
9851 continue;
9852
9853 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9854 NULL, NULL, NULL);
9855 if (isymbuf == NULL)
9856 return FALSE;
9857
9858 /* Read in any version definitions. */
9859 versymhdr = &elf_tdata (input)->dynversym_hdr;
9860 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9861 || (extversym = (Elf_External_Versym *)
9862 _bfd_malloc_and_read (input, versymhdr->sh_size,
9863 versymhdr->sh_size)) == NULL)
9864 {
9865 free (isymbuf);
9866 return FALSE;
9867 }
9868
9869 ever = extversym + extsymoff;
9870 isymend = isymbuf + extsymcount;
9871 for (isym = isymbuf; isym < isymend; isym++, ever++)
9872 {
9873 const char *name;
9874 Elf_Internal_Versym iver;
9875 unsigned short version_index;
9876
9877 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9878 || isym->st_shndx == SHN_UNDEF)
9879 continue;
9880
9881 name = bfd_elf_string_from_elf_section (input,
9882 hdr->sh_link,
9883 isym->st_name);
9884 if (strcmp (name, h->root.root.string) != 0)
9885 continue;
9886
9887 _bfd_elf_swap_versym_in (input, ever, &iver);
9888
9889 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9890 && !(h->def_regular
9891 && h->forced_local))
9892 {
9893 /* If we have a non-hidden versioned sym, then it should
9894 have provided a definition for the undefined sym unless
9895 it is defined in a non-shared object and forced local.
9896 */
9897 abort ();
9898 }
9899
9900 version_index = iver.vs_vers & VERSYM_VERSION;
9901 if (version_index == 1 || version_index == 2)
9902 {
9903 /* This is the base or first version. We can use it. */
9904 free (extversym);
9905 free (isymbuf);
9906 return TRUE;
9907 }
9908 }
9909
9910 free (extversym);
9911 free (isymbuf);
9912 }
9913
9914 return FALSE;
9915 }
9916
9917 /* Convert ELF common symbol TYPE. */
9918
9919 static int
9920 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9921 {
9922 /* Commom symbol can only appear in relocatable link. */
9923 if (!bfd_link_relocatable (info))
9924 abort ();
9925 switch (info->elf_stt_common)
9926 {
9927 case unchanged:
9928 break;
9929 case elf_stt_common:
9930 type = STT_COMMON;
9931 break;
9932 case no_elf_stt_common:
9933 type = STT_OBJECT;
9934 break;
9935 }
9936 return type;
9937 }
9938
9939 /* Add an external symbol to the symbol table. This is called from
9940 the hash table traversal routine. When generating a shared object,
9941 we go through the symbol table twice. The first time we output
9942 anything that might have been forced to local scope in a version
9943 script. The second time we output the symbols that are still
9944 global symbols. */
9945
9946 static bfd_boolean
9947 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9948 {
9949 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9950 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9951 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9952 bfd_boolean strip;
9953 Elf_Internal_Sym sym;
9954 asection *input_sec;
9955 const struct elf_backend_data *bed;
9956 long indx;
9957 int ret;
9958 unsigned int type;
9959
9960 if (h->root.type == bfd_link_hash_warning)
9961 {
9962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9963 if (h->root.type == bfd_link_hash_new)
9964 return TRUE;
9965 }
9966
9967 /* Decide whether to output this symbol in this pass. */
9968 if (eoinfo->localsyms)
9969 {
9970 if (!h->forced_local)
9971 return TRUE;
9972 }
9973 else
9974 {
9975 if (h->forced_local)
9976 return TRUE;
9977 }
9978
9979 bed = get_elf_backend_data (flinfo->output_bfd);
9980
9981 if (h->root.type == bfd_link_hash_undefined)
9982 {
9983 /* If we have an undefined symbol reference here then it must have
9984 come from a shared library that is being linked in. (Undefined
9985 references in regular files have already been handled unless
9986 they are in unreferenced sections which are removed by garbage
9987 collection). */
9988 bfd_boolean ignore_undef = FALSE;
9989
9990 /* Some symbols may be special in that the fact that they're
9991 undefined can be safely ignored - let backend determine that. */
9992 if (bed->elf_backend_ignore_undef_symbol)
9993 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9994
9995 /* If we are reporting errors for this situation then do so now. */
9996 if (!ignore_undef
9997 && h->ref_dynamic_nonweak
9998 && (!h->ref_regular || flinfo->info->gc_sections)
9999 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10000 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10001 {
10002 flinfo->info->callbacks->undefined_symbol
10003 (flinfo->info, h->root.root.string,
10004 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10005 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10006 && !flinfo->info->warn_unresolved_syms);
10007 }
10008
10009 /* Strip a global symbol defined in a discarded section. */
10010 if (h->indx == -3)
10011 return TRUE;
10012 }
10013
10014 /* We should also warn if a forced local symbol is referenced from
10015 shared libraries. */
10016 if (bfd_link_executable (flinfo->info)
10017 && h->forced_local
10018 && h->ref_dynamic
10019 && h->def_regular
10020 && !h->dynamic_def
10021 && h->ref_dynamic_nonweak
10022 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10023 {
10024 bfd *def_bfd;
10025 const char *msg;
10026 struct elf_link_hash_entry *hi = h;
10027
10028 /* Check indirect symbol. */
10029 while (hi->root.type == bfd_link_hash_indirect)
10030 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10031
10032 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10033 /* xgettext:c-format */
10034 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10035 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10036 /* xgettext:c-format */
10037 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10038 else
10039 /* xgettext:c-format */
10040 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10041 def_bfd = flinfo->output_bfd;
10042 if (hi->root.u.def.section != bfd_abs_section_ptr)
10043 def_bfd = hi->root.u.def.section->owner;
10044 _bfd_error_handler (msg, flinfo->output_bfd,
10045 h->root.root.string, def_bfd);
10046 bfd_set_error (bfd_error_bad_value);
10047 eoinfo->failed = TRUE;
10048 return FALSE;
10049 }
10050
10051 /* We don't want to output symbols that have never been mentioned by
10052 a regular file, or that we have been told to strip. However, if
10053 h->indx is set to -2, the symbol is used by a reloc and we must
10054 output it. */
10055 strip = FALSE;
10056 if (h->indx == -2)
10057 ;
10058 else if ((h->def_dynamic
10059 || h->ref_dynamic
10060 || h->root.type == bfd_link_hash_new)
10061 && !h->def_regular
10062 && !h->ref_regular)
10063 strip = TRUE;
10064 else if (flinfo->info->strip == strip_all)
10065 strip = TRUE;
10066 else if (flinfo->info->strip == strip_some
10067 && bfd_hash_lookup (flinfo->info->keep_hash,
10068 h->root.root.string, FALSE, FALSE) == NULL)
10069 strip = TRUE;
10070 else if ((h->root.type == bfd_link_hash_defined
10071 || h->root.type == bfd_link_hash_defweak)
10072 && ((flinfo->info->strip_discarded
10073 && discarded_section (h->root.u.def.section))
10074 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10075 && h->root.u.def.section->owner != NULL
10076 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10077 strip = TRUE;
10078 else if ((h->root.type == bfd_link_hash_undefined
10079 || h->root.type == bfd_link_hash_undefweak)
10080 && h->root.u.undef.abfd != NULL
10081 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10082 strip = TRUE;
10083
10084 type = h->type;
10085
10086 /* If we're stripping it, and it's not a dynamic symbol, there's
10087 nothing else to do. However, if it is a forced local symbol or
10088 an ifunc symbol we need to give the backend finish_dynamic_symbol
10089 function a chance to make it dynamic. */
10090 if (strip
10091 && h->dynindx == -1
10092 && type != STT_GNU_IFUNC
10093 && !h->forced_local)
10094 return TRUE;
10095
10096 sym.st_value = 0;
10097 sym.st_size = h->size;
10098 sym.st_other = h->other;
10099 switch (h->root.type)
10100 {
10101 default:
10102 case bfd_link_hash_new:
10103 case bfd_link_hash_warning:
10104 abort ();
10105 return FALSE;
10106
10107 case bfd_link_hash_undefined:
10108 case bfd_link_hash_undefweak:
10109 input_sec = bfd_und_section_ptr;
10110 sym.st_shndx = SHN_UNDEF;
10111 break;
10112
10113 case bfd_link_hash_defined:
10114 case bfd_link_hash_defweak:
10115 {
10116 input_sec = h->root.u.def.section;
10117 if (input_sec->output_section != NULL)
10118 {
10119 sym.st_shndx =
10120 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10121 input_sec->output_section);
10122 if (sym.st_shndx == SHN_BAD)
10123 {
10124 _bfd_error_handler
10125 /* xgettext:c-format */
10126 (_("%pB: could not find output section %pA for input section %pA"),
10127 flinfo->output_bfd, input_sec->output_section, input_sec);
10128 bfd_set_error (bfd_error_nonrepresentable_section);
10129 eoinfo->failed = TRUE;
10130 return FALSE;
10131 }
10132
10133 /* ELF symbols in relocatable files are section relative,
10134 but in nonrelocatable files they are virtual
10135 addresses. */
10136 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10137 if (!bfd_link_relocatable (flinfo->info))
10138 {
10139 sym.st_value += input_sec->output_section->vma;
10140 if (h->type == STT_TLS)
10141 {
10142 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10143 if (tls_sec != NULL)
10144 sym.st_value -= tls_sec->vma;
10145 }
10146 }
10147 }
10148 else
10149 {
10150 BFD_ASSERT (input_sec->owner == NULL
10151 || (input_sec->owner->flags & DYNAMIC) != 0);
10152 sym.st_shndx = SHN_UNDEF;
10153 input_sec = bfd_und_section_ptr;
10154 }
10155 }
10156 break;
10157
10158 case bfd_link_hash_common:
10159 input_sec = h->root.u.c.p->section;
10160 sym.st_shndx = bed->common_section_index (input_sec);
10161 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10162 break;
10163
10164 case bfd_link_hash_indirect:
10165 /* These symbols are created by symbol versioning. They point
10166 to the decorated version of the name. For example, if the
10167 symbol foo@@GNU_1.2 is the default, which should be used when
10168 foo is used with no version, then we add an indirect symbol
10169 foo which points to foo@@GNU_1.2. We ignore these symbols,
10170 since the indirected symbol is already in the hash table. */
10171 return TRUE;
10172 }
10173
10174 if (type == STT_COMMON || type == STT_OBJECT)
10175 switch (h->root.type)
10176 {
10177 case bfd_link_hash_common:
10178 type = elf_link_convert_common_type (flinfo->info, type);
10179 break;
10180 case bfd_link_hash_defined:
10181 case bfd_link_hash_defweak:
10182 if (bed->common_definition (&sym))
10183 type = elf_link_convert_common_type (flinfo->info, type);
10184 else
10185 type = STT_OBJECT;
10186 break;
10187 case bfd_link_hash_undefined:
10188 case bfd_link_hash_undefweak:
10189 break;
10190 default:
10191 abort ();
10192 }
10193
10194 if (h->forced_local)
10195 {
10196 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10197 /* Turn off visibility on local symbol. */
10198 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10199 }
10200 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10201 else if (h->unique_global && h->def_regular)
10202 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10203 else if (h->root.type == bfd_link_hash_undefweak
10204 || h->root.type == bfd_link_hash_defweak)
10205 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10206 else
10207 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10208 sym.st_target_internal = h->target_internal;
10209
10210 /* Give the processor backend a chance to tweak the symbol value,
10211 and also to finish up anything that needs to be done for this
10212 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10213 forced local syms when non-shared is due to a historical quirk.
10214 STT_GNU_IFUNC symbol must go through PLT. */
10215 if ((h->type == STT_GNU_IFUNC
10216 && h->def_regular
10217 && !bfd_link_relocatable (flinfo->info))
10218 || ((h->dynindx != -1
10219 || h->forced_local)
10220 && ((bfd_link_pic (flinfo->info)
10221 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10222 || h->root.type != bfd_link_hash_undefweak))
10223 || !h->forced_local)
10224 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10225 {
10226 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10227 (flinfo->output_bfd, flinfo->info, h, &sym)))
10228 {
10229 eoinfo->failed = TRUE;
10230 return FALSE;
10231 }
10232 }
10233
10234 /* If we are marking the symbol as undefined, and there are no
10235 non-weak references to this symbol from a regular object, then
10236 mark the symbol as weak undefined; if there are non-weak
10237 references, mark the symbol as strong. We can't do this earlier,
10238 because it might not be marked as undefined until the
10239 finish_dynamic_symbol routine gets through with it. */
10240 if (sym.st_shndx == SHN_UNDEF
10241 && h->ref_regular
10242 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10243 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10244 {
10245 int bindtype;
10246 type = ELF_ST_TYPE (sym.st_info);
10247
10248 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10249 if (type == STT_GNU_IFUNC)
10250 type = STT_FUNC;
10251
10252 if (h->ref_regular_nonweak)
10253 bindtype = STB_GLOBAL;
10254 else
10255 bindtype = STB_WEAK;
10256 sym.st_info = ELF_ST_INFO (bindtype, type);
10257 }
10258
10259 /* If this is a symbol defined in a dynamic library, don't use the
10260 symbol size from the dynamic library. Relinking an executable
10261 against a new library may introduce gratuitous changes in the
10262 executable's symbols if we keep the size. */
10263 if (sym.st_shndx == SHN_UNDEF
10264 && !h->def_regular
10265 && h->def_dynamic)
10266 sym.st_size = 0;
10267
10268 /* If a non-weak symbol with non-default visibility is not defined
10269 locally, it is a fatal error. */
10270 if (!bfd_link_relocatable (flinfo->info)
10271 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10272 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10273 && h->root.type == bfd_link_hash_undefined
10274 && !h->def_regular)
10275 {
10276 const char *msg;
10277
10278 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10279 /* xgettext:c-format */
10280 msg = _("%pB: protected symbol `%s' isn't defined");
10281 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10282 /* xgettext:c-format */
10283 msg = _("%pB: internal symbol `%s' isn't defined");
10284 else
10285 /* xgettext:c-format */
10286 msg = _("%pB: hidden symbol `%s' isn't defined");
10287 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10288 bfd_set_error (bfd_error_bad_value);
10289 eoinfo->failed = TRUE;
10290 return FALSE;
10291 }
10292
10293 /* If this symbol should be put in the .dynsym section, then put it
10294 there now. We already know the symbol index. We also fill in
10295 the entry in the .hash section. */
10296 if (h->dynindx != -1
10297 && elf_hash_table (flinfo->info)->dynamic_sections_created
10298 && elf_hash_table (flinfo->info)->dynsym != NULL
10299 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10300 {
10301 bfd_byte *esym;
10302
10303 /* Since there is no version information in the dynamic string,
10304 if there is no version info in symbol version section, we will
10305 have a run-time problem if not linking executable, referenced
10306 by shared library, or not bound locally. */
10307 if (h->verinfo.verdef == NULL
10308 && (!bfd_link_executable (flinfo->info)
10309 || h->ref_dynamic
10310 || !h->def_regular))
10311 {
10312 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10313
10314 if (p && p [1] != '\0')
10315 {
10316 _bfd_error_handler
10317 /* xgettext:c-format */
10318 (_("%pB: no symbol version section for versioned symbol `%s'"),
10319 flinfo->output_bfd, h->root.root.string);
10320 eoinfo->failed = TRUE;
10321 return FALSE;
10322 }
10323 }
10324
10325 sym.st_name = h->dynstr_index;
10326 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10327 + h->dynindx * bed->s->sizeof_sym);
10328 if (!check_dynsym (flinfo->output_bfd, &sym))
10329 {
10330 eoinfo->failed = TRUE;
10331 return FALSE;
10332 }
10333 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10334
10335 if (flinfo->hash_sec != NULL)
10336 {
10337 size_t hash_entry_size;
10338 bfd_byte *bucketpos;
10339 bfd_vma chain;
10340 size_t bucketcount;
10341 size_t bucket;
10342
10343 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10344 bucket = h->u.elf_hash_value % bucketcount;
10345
10346 hash_entry_size
10347 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10348 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10349 + (bucket + 2) * hash_entry_size);
10350 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10351 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10352 bucketpos);
10353 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10354 ((bfd_byte *) flinfo->hash_sec->contents
10355 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10356 }
10357
10358 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10359 {
10360 Elf_Internal_Versym iversym;
10361 Elf_External_Versym *eversym;
10362
10363 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10364 {
10365 if (h->verinfo.verdef == NULL
10366 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10367 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10368 iversym.vs_vers = 0;
10369 else
10370 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10371 }
10372 else
10373 {
10374 if (h->verinfo.vertree == NULL)
10375 iversym.vs_vers = 1;
10376 else
10377 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10378 if (flinfo->info->create_default_symver)
10379 iversym.vs_vers++;
10380 }
10381
10382 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10383 defined locally. */
10384 if (h->versioned == versioned_hidden && h->def_regular)
10385 iversym.vs_vers |= VERSYM_HIDDEN;
10386
10387 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10388 eversym += h->dynindx;
10389 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10390 }
10391 }
10392
10393 /* If the symbol is undefined, and we didn't output it to .dynsym,
10394 strip it from .symtab too. Obviously we can't do this for
10395 relocatable output or when needed for --emit-relocs. */
10396 else if (input_sec == bfd_und_section_ptr
10397 && h->indx != -2
10398 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10399 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10400 && !bfd_link_relocatable (flinfo->info))
10401 return TRUE;
10402
10403 /* Also strip others that we couldn't earlier due to dynamic symbol
10404 processing. */
10405 if (strip)
10406 return TRUE;
10407 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10408 return TRUE;
10409
10410 /* Output a FILE symbol so that following locals are not associated
10411 with the wrong input file. We need one for forced local symbols
10412 if we've seen more than one FILE symbol or when we have exactly
10413 one FILE symbol but global symbols are present in a file other
10414 than the one with the FILE symbol. We also need one if linker
10415 defined symbols are present. In practice these conditions are
10416 always met, so just emit the FILE symbol unconditionally. */
10417 if (eoinfo->localsyms
10418 && !eoinfo->file_sym_done
10419 && eoinfo->flinfo->filesym_count != 0)
10420 {
10421 Elf_Internal_Sym fsym;
10422
10423 memset (&fsym, 0, sizeof (fsym));
10424 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10425 fsym.st_shndx = SHN_ABS;
10426 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10427 bfd_und_section_ptr, NULL))
10428 return FALSE;
10429
10430 eoinfo->file_sym_done = TRUE;
10431 }
10432
10433 indx = bfd_get_symcount (flinfo->output_bfd);
10434 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10435 input_sec, h);
10436 if (ret == 0)
10437 {
10438 eoinfo->failed = TRUE;
10439 return FALSE;
10440 }
10441 else if (ret == 1)
10442 h->indx = indx;
10443 else if (h->indx == -2)
10444 abort();
10445
10446 return TRUE;
10447 }
10448
10449 /* Return TRUE if special handling is done for relocs in SEC against
10450 symbols defined in discarded sections. */
10451
10452 static bfd_boolean
10453 elf_section_ignore_discarded_relocs (asection *sec)
10454 {
10455 const struct elf_backend_data *bed;
10456
10457 switch (sec->sec_info_type)
10458 {
10459 case SEC_INFO_TYPE_STABS:
10460 case SEC_INFO_TYPE_EH_FRAME:
10461 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10462 return TRUE;
10463 default:
10464 break;
10465 }
10466
10467 bed = get_elf_backend_data (sec->owner);
10468 if (bed->elf_backend_ignore_discarded_relocs != NULL
10469 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10470 return TRUE;
10471
10472 return FALSE;
10473 }
10474
10475 /* Return a mask saying how ld should treat relocations in SEC against
10476 symbols defined in discarded sections. If this function returns
10477 COMPLAIN set, ld will issue a warning message. If this function
10478 returns PRETEND set, and the discarded section was link-once and the
10479 same size as the kept link-once section, ld will pretend that the
10480 symbol was actually defined in the kept section. Otherwise ld will
10481 zero the reloc (at least that is the intent, but some cooperation by
10482 the target dependent code is needed, particularly for REL targets). */
10483
10484 unsigned int
10485 _bfd_elf_default_action_discarded (asection *sec)
10486 {
10487 if (sec->flags & SEC_DEBUGGING)
10488 return PRETEND;
10489
10490 if (strcmp (".eh_frame", sec->name) == 0)
10491 return 0;
10492
10493 if (strcmp (".gcc_except_table", sec->name) == 0)
10494 return 0;
10495
10496 return COMPLAIN | PRETEND;
10497 }
10498
10499 /* Find a match between a section and a member of a section group. */
10500
10501 static asection *
10502 match_group_member (asection *sec, asection *group,
10503 struct bfd_link_info *info)
10504 {
10505 asection *first = elf_next_in_group (group);
10506 asection *s = first;
10507
10508 while (s != NULL)
10509 {
10510 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10511 return s;
10512
10513 s = elf_next_in_group (s);
10514 if (s == first)
10515 break;
10516 }
10517
10518 return NULL;
10519 }
10520
10521 /* Check if the kept section of a discarded section SEC can be used
10522 to replace it. Return the replacement if it is OK. Otherwise return
10523 NULL. */
10524
10525 asection *
10526 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10527 {
10528 asection *kept;
10529
10530 kept = sec->kept_section;
10531 if (kept != NULL)
10532 {
10533 if ((kept->flags & SEC_GROUP) != 0)
10534 kept = match_group_member (sec, kept, info);
10535 if (kept != NULL
10536 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10537 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10538 kept = NULL;
10539 sec->kept_section = kept;
10540 }
10541 return kept;
10542 }
10543
10544 /* Link an input file into the linker output file. This function
10545 handles all the sections and relocations of the input file at once.
10546 This is so that we only have to read the local symbols once, and
10547 don't have to keep them in memory. */
10548
10549 static bfd_boolean
10550 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10551 {
10552 int (*relocate_section)
10553 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10554 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10555 bfd *output_bfd;
10556 Elf_Internal_Shdr *symtab_hdr;
10557 size_t locsymcount;
10558 size_t extsymoff;
10559 Elf_Internal_Sym *isymbuf;
10560 Elf_Internal_Sym *isym;
10561 Elf_Internal_Sym *isymend;
10562 long *pindex;
10563 asection **ppsection;
10564 asection *o;
10565 const struct elf_backend_data *bed;
10566 struct elf_link_hash_entry **sym_hashes;
10567 bfd_size_type address_size;
10568 bfd_vma r_type_mask;
10569 int r_sym_shift;
10570 bfd_boolean have_file_sym = FALSE;
10571
10572 output_bfd = flinfo->output_bfd;
10573 bed = get_elf_backend_data (output_bfd);
10574 relocate_section = bed->elf_backend_relocate_section;
10575
10576 /* If this is a dynamic object, we don't want to do anything here:
10577 we don't want the local symbols, and we don't want the section
10578 contents. */
10579 if ((input_bfd->flags & DYNAMIC) != 0)
10580 return TRUE;
10581
10582 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10583 if (elf_bad_symtab (input_bfd))
10584 {
10585 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10586 extsymoff = 0;
10587 }
10588 else
10589 {
10590 locsymcount = symtab_hdr->sh_info;
10591 extsymoff = symtab_hdr->sh_info;
10592 }
10593
10594 /* Read the local symbols. */
10595 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10596 if (isymbuf == NULL && locsymcount != 0)
10597 {
10598 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10599 flinfo->internal_syms,
10600 flinfo->external_syms,
10601 flinfo->locsym_shndx);
10602 if (isymbuf == NULL)
10603 return FALSE;
10604 }
10605
10606 /* Find local symbol sections and adjust values of symbols in
10607 SEC_MERGE sections. Write out those local symbols we know are
10608 going into the output file. */
10609 isymend = isymbuf + locsymcount;
10610 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10611 isym < isymend;
10612 isym++, pindex++, ppsection++)
10613 {
10614 asection *isec;
10615 const char *name;
10616 Elf_Internal_Sym osym;
10617 long indx;
10618 int ret;
10619
10620 *pindex = -1;
10621
10622 if (elf_bad_symtab (input_bfd))
10623 {
10624 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10625 {
10626 *ppsection = NULL;
10627 continue;
10628 }
10629 }
10630
10631 if (isym->st_shndx == SHN_UNDEF)
10632 isec = bfd_und_section_ptr;
10633 else if (isym->st_shndx == SHN_ABS)
10634 isec = bfd_abs_section_ptr;
10635 else if (isym->st_shndx == SHN_COMMON)
10636 isec = bfd_com_section_ptr;
10637 else
10638 {
10639 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10640 if (isec == NULL)
10641 {
10642 /* Don't attempt to output symbols with st_shnx in the
10643 reserved range other than SHN_ABS and SHN_COMMON. */
10644 isec = bfd_und_section_ptr;
10645 }
10646 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10647 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10648 isym->st_value =
10649 _bfd_merged_section_offset (output_bfd, &isec,
10650 elf_section_data (isec)->sec_info,
10651 isym->st_value);
10652 }
10653
10654 *ppsection = isec;
10655
10656 /* Don't output the first, undefined, symbol. In fact, don't
10657 output any undefined local symbol. */
10658 if (isec == bfd_und_section_ptr)
10659 continue;
10660
10661 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10662 {
10663 /* We never output section symbols. Instead, we use the
10664 section symbol of the corresponding section in the output
10665 file. */
10666 continue;
10667 }
10668
10669 /* If we are stripping all symbols, we don't want to output this
10670 one. */
10671 if (flinfo->info->strip == strip_all)
10672 continue;
10673
10674 /* If we are discarding all local symbols, we don't want to
10675 output this one. If we are generating a relocatable output
10676 file, then some of the local symbols may be required by
10677 relocs; we output them below as we discover that they are
10678 needed. */
10679 if (flinfo->info->discard == discard_all)
10680 continue;
10681
10682 /* If this symbol is defined in a section which we are
10683 discarding, we don't need to keep it. */
10684 if (isym->st_shndx != SHN_UNDEF
10685 && isym->st_shndx < SHN_LORESERVE
10686 && isec->output_section == NULL
10687 && flinfo->info->non_contiguous_regions
10688 && flinfo->info->non_contiguous_regions_warnings)
10689 {
10690 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10691 "discards section `%s' from '%s'\n"),
10692 isec->name, bfd_get_filename (isec->owner));
10693 continue;
10694 }
10695
10696 if (isym->st_shndx != SHN_UNDEF
10697 && isym->st_shndx < SHN_LORESERVE
10698 && bfd_section_removed_from_list (output_bfd,
10699 isec->output_section))
10700 continue;
10701
10702 /* Get the name of the symbol. */
10703 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10704 isym->st_name);
10705 if (name == NULL)
10706 return FALSE;
10707
10708 /* See if we are discarding symbols with this name. */
10709 if ((flinfo->info->strip == strip_some
10710 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10711 == NULL))
10712 || (((flinfo->info->discard == discard_sec_merge
10713 && (isec->flags & SEC_MERGE)
10714 && !bfd_link_relocatable (flinfo->info))
10715 || flinfo->info->discard == discard_l)
10716 && bfd_is_local_label_name (input_bfd, name)))
10717 continue;
10718
10719 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10720 {
10721 if (input_bfd->lto_output)
10722 /* -flto puts a temp file name here. This means builds
10723 are not reproducible. Discard the symbol. */
10724 continue;
10725 have_file_sym = TRUE;
10726 flinfo->filesym_count += 1;
10727 }
10728 if (!have_file_sym)
10729 {
10730 /* In the absence of debug info, bfd_find_nearest_line uses
10731 FILE symbols to determine the source file for local
10732 function symbols. Provide a FILE symbol here if input
10733 files lack such, so that their symbols won't be
10734 associated with a previous input file. It's not the
10735 source file, but the best we can do. */
10736 have_file_sym = TRUE;
10737 flinfo->filesym_count += 1;
10738 memset (&osym, 0, sizeof (osym));
10739 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10740 osym.st_shndx = SHN_ABS;
10741 if (!elf_link_output_symstrtab (flinfo,
10742 (input_bfd->lto_output ? NULL
10743 : bfd_get_filename (input_bfd)),
10744 &osym, bfd_abs_section_ptr,
10745 NULL))
10746 return FALSE;
10747 }
10748
10749 osym = *isym;
10750
10751 /* Adjust the section index for the output file. */
10752 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10753 isec->output_section);
10754 if (osym.st_shndx == SHN_BAD)
10755 return FALSE;
10756
10757 /* ELF symbols in relocatable files are section relative, but
10758 in executable files they are virtual addresses. Note that
10759 this code assumes that all ELF sections have an associated
10760 BFD section with a reasonable value for output_offset; below
10761 we assume that they also have a reasonable value for
10762 output_section. Any special sections must be set up to meet
10763 these requirements. */
10764 osym.st_value += isec->output_offset;
10765 if (!bfd_link_relocatable (flinfo->info))
10766 {
10767 osym.st_value += isec->output_section->vma;
10768 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10769 {
10770 /* STT_TLS symbols are relative to PT_TLS segment base. */
10771 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10772 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10773 else
10774 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10775 STT_NOTYPE);
10776 }
10777 }
10778
10779 indx = bfd_get_symcount (output_bfd);
10780 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10781 if (ret == 0)
10782 return FALSE;
10783 else if (ret == 1)
10784 *pindex = indx;
10785 }
10786
10787 if (bed->s->arch_size == 32)
10788 {
10789 r_type_mask = 0xff;
10790 r_sym_shift = 8;
10791 address_size = 4;
10792 }
10793 else
10794 {
10795 r_type_mask = 0xffffffff;
10796 r_sym_shift = 32;
10797 address_size = 8;
10798 }
10799
10800 /* Relocate the contents of each section. */
10801 sym_hashes = elf_sym_hashes (input_bfd);
10802 for (o = input_bfd->sections; o != NULL; o = o->next)
10803 {
10804 bfd_byte *contents;
10805
10806 if (! o->linker_mark)
10807 {
10808 /* This section was omitted from the link. */
10809 continue;
10810 }
10811
10812 if (!flinfo->info->resolve_section_groups
10813 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10814 {
10815 /* Deal with the group signature symbol. */
10816 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10817 unsigned long symndx = sec_data->this_hdr.sh_info;
10818 asection *osec = o->output_section;
10819
10820 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10821 if (symndx >= locsymcount
10822 || (elf_bad_symtab (input_bfd)
10823 && flinfo->sections[symndx] == NULL))
10824 {
10825 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10826 while (h->root.type == bfd_link_hash_indirect
10827 || h->root.type == bfd_link_hash_warning)
10828 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10829 /* Arrange for symbol to be output. */
10830 h->indx = -2;
10831 elf_section_data (osec)->this_hdr.sh_info = -2;
10832 }
10833 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10834 {
10835 /* We'll use the output section target_index. */
10836 asection *sec = flinfo->sections[symndx]->output_section;
10837 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10838 }
10839 else
10840 {
10841 if (flinfo->indices[symndx] == -1)
10842 {
10843 /* Otherwise output the local symbol now. */
10844 Elf_Internal_Sym sym = isymbuf[symndx];
10845 asection *sec = flinfo->sections[symndx]->output_section;
10846 const char *name;
10847 long indx;
10848 int ret;
10849
10850 name = bfd_elf_string_from_elf_section (input_bfd,
10851 symtab_hdr->sh_link,
10852 sym.st_name);
10853 if (name == NULL)
10854 return FALSE;
10855
10856 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10857 sec);
10858 if (sym.st_shndx == SHN_BAD)
10859 return FALSE;
10860
10861 sym.st_value += o->output_offset;
10862
10863 indx = bfd_get_symcount (output_bfd);
10864 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10865 NULL);
10866 if (ret == 0)
10867 return FALSE;
10868 else if (ret == 1)
10869 flinfo->indices[symndx] = indx;
10870 else
10871 abort ();
10872 }
10873 elf_section_data (osec)->this_hdr.sh_info
10874 = flinfo->indices[symndx];
10875 }
10876 }
10877
10878 if ((o->flags & SEC_HAS_CONTENTS) == 0
10879 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10880 continue;
10881
10882 if ((o->flags & SEC_LINKER_CREATED) != 0)
10883 {
10884 /* Section was created by _bfd_elf_link_create_dynamic_sections
10885 or somesuch. */
10886 continue;
10887 }
10888
10889 /* Get the contents of the section. They have been cached by a
10890 relaxation routine. Note that o is a section in an input
10891 file, so the contents field will not have been set by any of
10892 the routines which work on output files. */
10893 if (elf_section_data (o)->this_hdr.contents != NULL)
10894 {
10895 contents = elf_section_data (o)->this_hdr.contents;
10896 if (bed->caches_rawsize
10897 && o->rawsize != 0
10898 && o->rawsize < o->size)
10899 {
10900 memcpy (flinfo->contents, contents, o->rawsize);
10901 contents = flinfo->contents;
10902 }
10903 }
10904 else
10905 {
10906 contents = flinfo->contents;
10907 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10908 return FALSE;
10909 }
10910
10911 if ((o->flags & SEC_RELOC) != 0)
10912 {
10913 Elf_Internal_Rela *internal_relocs;
10914 Elf_Internal_Rela *rel, *relend;
10915 int action_discarded;
10916 int ret;
10917
10918 /* Get the swapped relocs. */
10919 internal_relocs
10920 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10921 flinfo->internal_relocs, FALSE);
10922 if (internal_relocs == NULL
10923 && o->reloc_count > 0)
10924 return FALSE;
10925
10926 /* We need to reverse-copy input .ctors/.dtors sections if
10927 they are placed in .init_array/.finit_array for output. */
10928 if (o->size > address_size
10929 && ((strncmp (o->name, ".ctors", 6) == 0
10930 && strcmp (o->output_section->name,
10931 ".init_array") == 0)
10932 || (strncmp (o->name, ".dtors", 6) == 0
10933 && strcmp (o->output_section->name,
10934 ".fini_array") == 0))
10935 && (o->name[6] == 0 || o->name[6] == '.'))
10936 {
10937 if (o->size * bed->s->int_rels_per_ext_rel
10938 != o->reloc_count * address_size)
10939 {
10940 _bfd_error_handler
10941 /* xgettext:c-format */
10942 (_("error: %pB: size of section %pA is not "
10943 "multiple of address size"),
10944 input_bfd, o);
10945 bfd_set_error (bfd_error_bad_value);
10946 return FALSE;
10947 }
10948 o->flags |= SEC_ELF_REVERSE_COPY;
10949 }
10950
10951 action_discarded = -1;
10952 if (!elf_section_ignore_discarded_relocs (o))
10953 action_discarded = (*bed->action_discarded) (o);
10954
10955 /* Run through the relocs evaluating complex reloc symbols and
10956 looking for relocs against symbols from discarded sections
10957 or section symbols from removed link-once sections.
10958 Complain about relocs against discarded sections. Zero
10959 relocs against removed link-once sections. */
10960
10961 rel = internal_relocs;
10962 relend = rel + o->reloc_count;
10963 for ( ; rel < relend; rel++)
10964 {
10965 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10966 unsigned int s_type;
10967 asection **ps, *sec;
10968 struct elf_link_hash_entry *h = NULL;
10969 const char *sym_name;
10970
10971 if (r_symndx == STN_UNDEF)
10972 continue;
10973
10974 if (r_symndx >= locsymcount
10975 || (elf_bad_symtab (input_bfd)
10976 && flinfo->sections[r_symndx] == NULL))
10977 {
10978 h = sym_hashes[r_symndx - extsymoff];
10979
10980 /* Badly formatted input files can contain relocs that
10981 reference non-existant symbols. Check here so that
10982 we do not seg fault. */
10983 if (h == NULL)
10984 {
10985 _bfd_error_handler
10986 /* xgettext:c-format */
10987 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10988 "that references a non-existent global symbol"),
10989 input_bfd, (uint64_t) rel->r_info, o);
10990 bfd_set_error (bfd_error_bad_value);
10991 return FALSE;
10992 }
10993
10994 while (h->root.type == bfd_link_hash_indirect
10995 || h->root.type == bfd_link_hash_warning)
10996 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10997
10998 s_type = h->type;
10999
11000 /* If a plugin symbol is referenced from a non-IR file,
11001 mark the symbol as undefined. Note that the
11002 linker may attach linker created dynamic sections
11003 to the plugin bfd. Symbols defined in linker
11004 created sections are not plugin symbols. */
11005 if ((h->root.non_ir_ref_regular
11006 || h->root.non_ir_ref_dynamic)
11007 && (h->root.type == bfd_link_hash_defined
11008 || h->root.type == bfd_link_hash_defweak)
11009 && (h->root.u.def.section->flags
11010 & SEC_LINKER_CREATED) == 0
11011 && h->root.u.def.section->owner != NULL
11012 && (h->root.u.def.section->owner->flags
11013 & BFD_PLUGIN) != 0)
11014 {
11015 h->root.type = bfd_link_hash_undefined;
11016 h->root.u.undef.abfd = h->root.u.def.section->owner;
11017 }
11018
11019 ps = NULL;
11020 if (h->root.type == bfd_link_hash_defined
11021 || h->root.type == bfd_link_hash_defweak)
11022 ps = &h->root.u.def.section;
11023
11024 sym_name = h->root.root.string;
11025 }
11026 else
11027 {
11028 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11029
11030 s_type = ELF_ST_TYPE (sym->st_info);
11031 ps = &flinfo->sections[r_symndx];
11032 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11033 sym, *ps);
11034 }
11035
11036 if ((s_type == STT_RELC || s_type == STT_SRELC)
11037 && !bfd_link_relocatable (flinfo->info))
11038 {
11039 bfd_vma val;
11040 bfd_vma dot = (rel->r_offset
11041 + o->output_offset + o->output_section->vma);
11042 #ifdef DEBUG
11043 printf ("Encountered a complex symbol!");
11044 printf (" (input_bfd %s, section %s, reloc %ld\n",
11045 bfd_get_filename (input_bfd), o->name,
11046 (long) (rel - internal_relocs));
11047 printf (" symbol: idx %8.8lx, name %s\n",
11048 r_symndx, sym_name);
11049 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11050 (unsigned long) rel->r_info,
11051 (unsigned long) rel->r_offset);
11052 #endif
11053 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11054 isymbuf, locsymcount, s_type == STT_SRELC))
11055 return FALSE;
11056
11057 /* Symbol evaluated OK. Update to absolute value. */
11058 set_symbol_value (input_bfd, isymbuf, locsymcount,
11059 r_symndx, val);
11060 continue;
11061 }
11062
11063 if (action_discarded != -1 && ps != NULL)
11064 {
11065 /* Complain if the definition comes from a
11066 discarded section. */
11067 if ((sec = *ps) != NULL && discarded_section (sec))
11068 {
11069 BFD_ASSERT (r_symndx != STN_UNDEF);
11070 if (action_discarded & COMPLAIN)
11071 (*flinfo->info->callbacks->einfo)
11072 /* xgettext:c-format */
11073 (_("%X`%s' referenced in section `%pA' of %pB: "
11074 "defined in discarded section `%pA' of %pB\n"),
11075 sym_name, o, input_bfd, sec, sec->owner);
11076
11077 /* Try to do the best we can to support buggy old
11078 versions of gcc. Pretend that the symbol is
11079 really defined in the kept linkonce section.
11080 FIXME: This is quite broken. Modifying the
11081 symbol here means we will be changing all later
11082 uses of the symbol, not just in this section. */
11083 if (action_discarded & PRETEND)
11084 {
11085 asection *kept;
11086
11087 kept = _bfd_elf_check_kept_section (sec,
11088 flinfo->info);
11089 if (kept != NULL)
11090 {
11091 *ps = kept;
11092 continue;
11093 }
11094 }
11095 }
11096 }
11097 }
11098
11099 /* Relocate the section by invoking a back end routine.
11100
11101 The back end routine is responsible for adjusting the
11102 section contents as necessary, and (if using Rela relocs
11103 and generating a relocatable output file) adjusting the
11104 reloc addend as necessary.
11105
11106 The back end routine does not have to worry about setting
11107 the reloc address or the reloc symbol index.
11108
11109 The back end routine is given a pointer to the swapped in
11110 internal symbols, and can access the hash table entries
11111 for the external symbols via elf_sym_hashes (input_bfd).
11112
11113 When generating relocatable output, the back end routine
11114 must handle STB_LOCAL/STT_SECTION symbols specially. The
11115 output symbol is going to be a section symbol
11116 corresponding to the output section, which will require
11117 the addend to be adjusted. */
11118
11119 ret = (*relocate_section) (output_bfd, flinfo->info,
11120 input_bfd, o, contents,
11121 internal_relocs,
11122 isymbuf,
11123 flinfo->sections);
11124 if (!ret)
11125 return FALSE;
11126
11127 if (ret == 2
11128 || bfd_link_relocatable (flinfo->info)
11129 || flinfo->info->emitrelocations)
11130 {
11131 Elf_Internal_Rela *irela;
11132 Elf_Internal_Rela *irelaend, *irelamid;
11133 bfd_vma last_offset;
11134 struct elf_link_hash_entry **rel_hash;
11135 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11136 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11137 unsigned int next_erel;
11138 bfd_boolean rela_normal;
11139 struct bfd_elf_section_data *esdi, *esdo;
11140
11141 esdi = elf_section_data (o);
11142 esdo = elf_section_data (o->output_section);
11143 rela_normal = FALSE;
11144
11145 /* Adjust the reloc addresses and symbol indices. */
11146
11147 irela = internal_relocs;
11148 irelaend = irela + o->reloc_count;
11149 rel_hash = esdo->rel.hashes + esdo->rel.count;
11150 /* We start processing the REL relocs, if any. When we reach
11151 IRELAMID in the loop, we switch to the RELA relocs. */
11152 irelamid = irela;
11153 if (esdi->rel.hdr != NULL)
11154 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11155 * bed->s->int_rels_per_ext_rel);
11156 rel_hash_list = rel_hash;
11157 rela_hash_list = NULL;
11158 last_offset = o->output_offset;
11159 if (!bfd_link_relocatable (flinfo->info))
11160 last_offset += o->output_section->vma;
11161 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11162 {
11163 unsigned long r_symndx;
11164 asection *sec;
11165 Elf_Internal_Sym sym;
11166
11167 if (next_erel == bed->s->int_rels_per_ext_rel)
11168 {
11169 rel_hash++;
11170 next_erel = 0;
11171 }
11172
11173 if (irela == irelamid)
11174 {
11175 rel_hash = esdo->rela.hashes + esdo->rela.count;
11176 rela_hash_list = rel_hash;
11177 rela_normal = bed->rela_normal;
11178 }
11179
11180 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11181 flinfo->info, o,
11182 irela->r_offset);
11183 if (irela->r_offset >= (bfd_vma) -2)
11184 {
11185 /* This is a reloc for a deleted entry or somesuch.
11186 Turn it into an R_*_NONE reloc, at the same
11187 offset as the last reloc. elf_eh_frame.c and
11188 bfd_elf_discard_info rely on reloc offsets
11189 being ordered. */
11190 irela->r_offset = last_offset;
11191 irela->r_info = 0;
11192 irela->r_addend = 0;
11193 continue;
11194 }
11195
11196 irela->r_offset += o->output_offset;
11197
11198 /* Relocs in an executable have to be virtual addresses. */
11199 if (!bfd_link_relocatable (flinfo->info))
11200 irela->r_offset += o->output_section->vma;
11201
11202 last_offset = irela->r_offset;
11203
11204 r_symndx = irela->r_info >> r_sym_shift;
11205 if (r_symndx == STN_UNDEF)
11206 continue;
11207
11208 if (r_symndx >= locsymcount
11209 || (elf_bad_symtab (input_bfd)
11210 && flinfo->sections[r_symndx] == NULL))
11211 {
11212 struct elf_link_hash_entry *rh;
11213 unsigned long indx;
11214
11215 /* This is a reloc against a global symbol. We
11216 have not yet output all the local symbols, so
11217 we do not know the symbol index of any global
11218 symbol. We set the rel_hash entry for this
11219 reloc to point to the global hash table entry
11220 for this symbol. The symbol index is then
11221 set at the end of bfd_elf_final_link. */
11222 indx = r_symndx - extsymoff;
11223 rh = elf_sym_hashes (input_bfd)[indx];
11224 while (rh->root.type == bfd_link_hash_indirect
11225 || rh->root.type == bfd_link_hash_warning)
11226 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11227
11228 /* Setting the index to -2 tells
11229 elf_link_output_extsym that this symbol is
11230 used by a reloc. */
11231 BFD_ASSERT (rh->indx < 0);
11232 rh->indx = -2;
11233 *rel_hash = rh;
11234
11235 continue;
11236 }
11237
11238 /* This is a reloc against a local symbol. */
11239
11240 *rel_hash = NULL;
11241 sym = isymbuf[r_symndx];
11242 sec = flinfo->sections[r_symndx];
11243 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11244 {
11245 /* I suppose the backend ought to fill in the
11246 section of any STT_SECTION symbol against a
11247 processor specific section. */
11248 r_symndx = STN_UNDEF;
11249 if (bfd_is_abs_section (sec))
11250 ;
11251 else if (sec == NULL || sec->owner == NULL)
11252 {
11253 bfd_set_error (bfd_error_bad_value);
11254 return FALSE;
11255 }
11256 else
11257 {
11258 asection *osec = sec->output_section;
11259
11260 /* If we have discarded a section, the output
11261 section will be the absolute section. In
11262 case of discarded SEC_MERGE sections, use
11263 the kept section. relocate_section should
11264 have already handled discarded linkonce
11265 sections. */
11266 if (bfd_is_abs_section (osec)
11267 && sec->kept_section != NULL
11268 && sec->kept_section->output_section != NULL)
11269 {
11270 osec = sec->kept_section->output_section;
11271 irela->r_addend -= osec->vma;
11272 }
11273
11274 if (!bfd_is_abs_section (osec))
11275 {
11276 r_symndx = osec->target_index;
11277 if (r_symndx == STN_UNDEF)
11278 {
11279 irela->r_addend += osec->vma;
11280 osec = _bfd_nearby_section (output_bfd, osec,
11281 osec->vma);
11282 irela->r_addend -= osec->vma;
11283 r_symndx = osec->target_index;
11284 }
11285 }
11286 }
11287
11288 /* Adjust the addend according to where the
11289 section winds up in the output section. */
11290 if (rela_normal)
11291 irela->r_addend += sec->output_offset;
11292 }
11293 else
11294 {
11295 if (flinfo->indices[r_symndx] == -1)
11296 {
11297 unsigned long shlink;
11298 const char *name;
11299 asection *osec;
11300 long indx;
11301
11302 if (flinfo->info->strip == strip_all)
11303 {
11304 /* You can't do ld -r -s. */
11305 bfd_set_error (bfd_error_invalid_operation);
11306 return FALSE;
11307 }
11308
11309 /* This symbol was skipped earlier, but
11310 since it is needed by a reloc, we
11311 must output it now. */
11312 shlink = symtab_hdr->sh_link;
11313 name = (bfd_elf_string_from_elf_section
11314 (input_bfd, shlink, sym.st_name));
11315 if (name == NULL)
11316 return FALSE;
11317
11318 osec = sec->output_section;
11319 sym.st_shndx =
11320 _bfd_elf_section_from_bfd_section (output_bfd,
11321 osec);
11322 if (sym.st_shndx == SHN_BAD)
11323 return FALSE;
11324
11325 sym.st_value += sec->output_offset;
11326 if (!bfd_link_relocatable (flinfo->info))
11327 {
11328 sym.st_value += osec->vma;
11329 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11330 {
11331 struct elf_link_hash_table *htab
11332 = elf_hash_table (flinfo->info);
11333
11334 /* STT_TLS symbols are relative to PT_TLS
11335 segment base. */
11336 if (htab->tls_sec != NULL)
11337 sym.st_value -= htab->tls_sec->vma;
11338 else
11339 sym.st_info
11340 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11341 STT_NOTYPE);
11342 }
11343 }
11344
11345 indx = bfd_get_symcount (output_bfd);
11346 ret = elf_link_output_symstrtab (flinfo, name,
11347 &sym, sec,
11348 NULL);
11349 if (ret == 0)
11350 return FALSE;
11351 else if (ret == 1)
11352 flinfo->indices[r_symndx] = indx;
11353 else
11354 abort ();
11355 }
11356
11357 r_symndx = flinfo->indices[r_symndx];
11358 }
11359
11360 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11361 | (irela->r_info & r_type_mask));
11362 }
11363
11364 /* Swap out the relocs. */
11365 input_rel_hdr = esdi->rel.hdr;
11366 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11367 {
11368 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11369 input_rel_hdr,
11370 internal_relocs,
11371 rel_hash_list))
11372 return FALSE;
11373 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11374 * bed->s->int_rels_per_ext_rel);
11375 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11376 }
11377
11378 input_rela_hdr = esdi->rela.hdr;
11379 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11380 {
11381 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11382 input_rela_hdr,
11383 internal_relocs,
11384 rela_hash_list))
11385 return FALSE;
11386 }
11387 }
11388 }
11389
11390 /* Write out the modified section contents. */
11391 if (bed->elf_backend_write_section
11392 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11393 contents))
11394 {
11395 /* Section written out. */
11396 }
11397 else switch (o->sec_info_type)
11398 {
11399 case SEC_INFO_TYPE_STABS:
11400 if (! (_bfd_write_section_stabs
11401 (output_bfd,
11402 &elf_hash_table (flinfo->info)->stab_info,
11403 o, &elf_section_data (o)->sec_info, contents)))
11404 return FALSE;
11405 break;
11406 case SEC_INFO_TYPE_MERGE:
11407 if (! _bfd_write_merged_section (output_bfd, o,
11408 elf_section_data (o)->sec_info))
11409 return FALSE;
11410 break;
11411 case SEC_INFO_TYPE_EH_FRAME:
11412 {
11413 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11414 o, contents))
11415 return FALSE;
11416 }
11417 break;
11418 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11419 {
11420 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11421 flinfo->info,
11422 o, contents))
11423 return FALSE;
11424 }
11425 break;
11426 default:
11427 {
11428 if (! (o->flags & SEC_EXCLUDE))
11429 {
11430 file_ptr offset = (file_ptr) o->output_offset;
11431 bfd_size_type todo = o->size;
11432
11433 offset *= bfd_octets_per_byte (output_bfd, o);
11434
11435 if ((o->flags & SEC_ELF_REVERSE_COPY))
11436 {
11437 /* Reverse-copy input section to output. */
11438 do
11439 {
11440 todo -= address_size;
11441 if (! bfd_set_section_contents (output_bfd,
11442 o->output_section,
11443 contents + todo,
11444 offset,
11445 address_size))
11446 return FALSE;
11447 if (todo == 0)
11448 break;
11449 offset += address_size;
11450 }
11451 while (1);
11452 }
11453 else if (! bfd_set_section_contents (output_bfd,
11454 o->output_section,
11455 contents,
11456 offset, todo))
11457 return FALSE;
11458 }
11459 }
11460 break;
11461 }
11462 }
11463
11464 return TRUE;
11465 }
11466
11467 /* Generate a reloc when linking an ELF file. This is a reloc
11468 requested by the linker, and does not come from any input file. This
11469 is used to build constructor and destructor tables when linking
11470 with -Ur. */
11471
11472 static bfd_boolean
11473 elf_reloc_link_order (bfd *output_bfd,
11474 struct bfd_link_info *info,
11475 asection *output_section,
11476 struct bfd_link_order *link_order)
11477 {
11478 reloc_howto_type *howto;
11479 long indx;
11480 bfd_vma offset;
11481 bfd_vma addend;
11482 struct bfd_elf_section_reloc_data *reldata;
11483 struct elf_link_hash_entry **rel_hash_ptr;
11484 Elf_Internal_Shdr *rel_hdr;
11485 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11486 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11487 bfd_byte *erel;
11488 unsigned int i;
11489 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11490
11491 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11492 if (howto == NULL)
11493 {
11494 bfd_set_error (bfd_error_bad_value);
11495 return FALSE;
11496 }
11497
11498 addend = link_order->u.reloc.p->addend;
11499
11500 if (esdo->rel.hdr)
11501 reldata = &esdo->rel;
11502 else if (esdo->rela.hdr)
11503 reldata = &esdo->rela;
11504 else
11505 {
11506 reldata = NULL;
11507 BFD_ASSERT (0);
11508 }
11509
11510 /* Figure out the symbol index. */
11511 rel_hash_ptr = reldata->hashes + reldata->count;
11512 if (link_order->type == bfd_section_reloc_link_order)
11513 {
11514 indx = link_order->u.reloc.p->u.section->target_index;
11515 BFD_ASSERT (indx != 0);
11516 *rel_hash_ptr = NULL;
11517 }
11518 else
11519 {
11520 struct elf_link_hash_entry *h;
11521
11522 /* Treat a reloc against a defined symbol as though it were
11523 actually against the section. */
11524 h = ((struct elf_link_hash_entry *)
11525 bfd_wrapped_link_hash_lookup (output_bfd, info,
11526 link_order->u.reloc.p->u.name,
11527 FALSE, FALSE, TRUE));
11528 if (h != NULL
11529 && (h->root.type == bfd_link_hash_defined
11530 || h->root.type == bfd_link_hash_defweak))
11531 {
11532 asection *section;
11533
11534 section = h->root.u.def.section;
11535 indx = section->output_section->target_index;
11536 *rel_hash_ptr = NULL;
11537 /* It seems that we ought to add the symbol value to the
11538 addend here, but in practice it has already been added
11539 because it was passed to constructor_callback. */
11540 addend += section->output_section->vma + section->output_offset;
11541 }
11542 else if (h != NULL)
11543 {
11544 /* Setting the index to -2 tells elf_link_output_extsym that
11545 this symbol is used by a reloc. */
11546 h->indx = -2;
11547 *rel_hash_ptr = h;
11548 indx = 0;
11549 }
11550 else
11551 {
11552 (*info->callbacks->unattached_reloc)
11553 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11554 indx = 0;
11555 }
11556 }
11557
11558 /* If this is an inplace reloc, we must write the addend into the
11559 object file. */
11560 if (howto->partial_inplace && addend != 0)
11561 {
11562 bfd_size_type size;
11563 bfd_reloc_status_type rstat;
11564 bfd_byte *buf;
11565 bfd_boolean ok;
11566 const char *sym_name;
11567 bfd_size_type octets;
11568
11569 size = (bfd_size_type) bfd_get_reloc_size (howto);
11570 buf = (bfd_byte *) bfd_zmalloc (size);
11571 if (buf == NULL && size != 0)
11572 return FALSE;
11573 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11574 switch (rstat)
11575 {
11576 case bfd_reloc_ok:
11577 break;
11578
11579 default:
11580 case bfd_reloc_outofrange:
11581 abort ();
11582
11583 case bfd_reloc_overflow:
11584 if (link_order->type == bfd_section_reloc_link_order)
11585 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11586 else
11587 sym_name = link_order->u.reloc.p->u.name;
11588 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11589 howto->name, addend, NULL, NULL,
11590 (bfd_vma) 0);
11591 break;
11592 }
11593
11594 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11595 output_section);
11596 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11597 octets, size);
11598 free (buf);
11599 if (! ok)
11600 return FALSE;
11601 }
11602
11603 /* The address of a reloc is relative to the section in a
11604 relocatable file, and is a virtual address in an executable
11605 file. */
11606 offset = link_order->offset;
11607 if (! bfd_link_relocatable (info))
11608 offset += output_section->vma;
11609
11610 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11611 {
11612 irel[i].r_offset = offset;
11613 irel[i].r_info = 0;
11614 irel[i].r_addend = 0;
11615 }
11616 if (bed->s->arch_size == 32)
11617 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11618 else
11619 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11620
11621 rel_hdr = reldata->hdr;
11622 erel = rel_hdr->contents;
11623 if (rel_hdr->sh_type == SHT_REL)
11624 {
11625 erel += reldata->count * bed->s->sizeof_rel;
11626 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11627 }
11628 else
11629 {
11630 irel[0].r_addend = addend;
11631 erel += reldata->count * bed->s->sizeof_rela;
11632 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11633 }
11634
11635 ++reldata->count;
11636
11637 return TRUE;
11638 }
11639
11640
11641 /* Compare two sections based on the locations of the sections they are
11642 linked to. Used by elf_fixup_link_order. */
11643
11644 static int
11645 compare_link_order (const void *a, const void *b)
11646 {
11647 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11648 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11649 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11650 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11651 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11652 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11653
11654 if (apos < bpos)
11655 return -1;
11656 if (apos > bpos)
11657 return 1;
11658
11659 /* The only way we should get matching LMAs is when the first of two
11660 sections has zero size. */
11661 if (asec->size < bsec->size)
11662 return -1;
11663 if (asec->size > bsec->size)
11664 return 1;
11665
11666 /* If they are both zero size then they almost certainly have the same
11667 VMA and thus are not ordered with respect to each other. Test VMA
11668 anyway, and fall back to id to make the result reproducible across
11669 qsort implementations. */
11670 apos = asec->output_section->vma + asec->output_offset;
11671 bpos = bsec->output_section->vma + bsec->output_offset;
11672 if (apos < bpos)
11673 return -1;
11674 if (apos > bpos)
11675 return 1;
11676
11677 return asec->id - bsec->id;
11678 }
11679
11680
11681 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11682 order as their linked sections. Returns false if this could not be done
11683 because an output section includes both ordered and unordered
11684 sections. Ideally we'd do this in the linker proper. */
11685
11686 static bfd_boolean
11687 elf_fixup_link_order (bfd *abfd, asection *o)
11688 {
11689 size_t seen_linkorder;
11690 size_t seen_other;
11691 size_t n;
11692 struct bfd_link_order *p;
11693 bfd *sub;
11694 struct bfd_link_order **sections;
11695 asection *other_sec, *linkorder_sec;
11696 bfd_vma offset; /* Octets. */
11697
11698 other_sec = NULL;
11699 linkorder_sec = NULL;
11700 seen_other = 0;
11701 seen_linkorder = 0;
11702 for (p = o->map_head.link_order; p != NULL; p = p->next)
11703 {
11704 if (p->type == bfd_indirect_link_order)
11705 {
11706 asection *s = p->u.indirect.section;
11707 sub = s->owner;
11708 if ((s->flags & SEC_LINKER_CREATED) == 0
11709 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11710 && elf_section_data (s) != NULL
11711 && elf_linked_to_section (s) != NULL)
11712 {
11713 seen_linkorder++;
11714 linkorder_sec = s;
11715 }
11716 else
11717 {
11718 seen_other++;
11719 other_sec = s;
11720 }
11721 }
11722 else
11723 seen_other++;
11724
11725 if (seen_other && seen_linkorder)
11726 {
11727 if (other_sec && linkorder_sec)
11728 _bfd_error_handler
11729 /* xgettext:c-format */
11730 (_("%pA has both ordered [`%pA' in %pB] "
11731 "and unordered [`%pA' in %pB] sections"),
11732 o, linkorder_sec, linkorder_sec->owner,
11733 other_sec, other_sec->owner);
11734 else
11735 _bfd_error_handler
11736 (_("%pA has both ordered and unordered sections"), o);
11737 bfd_set_error (bfd_error_bad_value);
11738 return FALSE;
11739 }
11740 }
11741
11742 if (!seen_linkorder)
11743 return TRUE;
11744
11745 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11746 if (sections == NULL)
11747 return FALSE;
11748
11749 seen_linkorder = 0;
11750 for (p = o->map_head.link_order; p != NULL; p = p->next)
11751 sections[seen_linkorder++] = p;
11752
11753 /* Sort the input sections in the order of their linked section. */
11754 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11755
11756 /* Change the offsets of the sections. */
11757 offset = 0;
11758 for (n = 0; n < seen_linkorder; n++)
11759 {
11760 bfd_vma mask;
11761 asection *s = sections[n]->u.indirect.section;
11762 unsigned int opb = bfd_octets_per_byte (abfd, s);
11763
11764 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11765 offset = (offset + ~mask) & mask;
11766 sections[n]->offset = s->output_offset = offset / opb;
11767 offset += sections[n]->size;
11768 }
11769
11770 free (sections);
11771 return TRUE;
11772 }
11773
11774 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11775 Returns TRUE upon success, FALSE otherwise. */
11776
11777 static bfd_boolean
11778 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11779 {
11780 bfd_boolean ret = FALSE;
11781 bfd *implib_bfd;
11782 const struct elf_backend_data *bed;
11783 flagword flags;
11784 enum bfd_architecture arch;
11785 unsigned int mach;
11786 asymbol **sympp = NULL;
11787 long symsize;
11788 long symcount;
11789 long src_count;
11790 elf_symbol_type *osymbuf;
11791 size_t amt;
11792
11793 implib_bfd = info->out_implib_bfd;
11794 bed = get_elf_backend_data (abfd);
11795
11796 if (!bfd_set_format (implib_bfd, bfd_object))
11797 return FALSE;
11798
11799 /* Use flag from executable but make it a relocatable object. */
11800 flags = bfd_get_file_flags (abfd);
11801 flags &= ~HAS_RELOC;
11802 if (!bfd_set_start_address (implib_bfd, 0)
11803 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11804 return FALSE;
11805
11806 /* Copy architecture of output file to import library file. */
11807 arch = bfd_get_arch (abfd);
11808 mach = bfd_get_mach (abfd);
11809 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11810 && (abfd->target_defaulted
11811 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11812 return FALSE;
11813
11814 /* Get symbol table size. */
11815 symsize = bfd_get_symtab_upper_bound (abfd);
11816 if (symsize < 0)
11817 return FALSE;
11818
11819 /* Read in the symbol table. */
11820 sympp = (asymbol **) bfd_malloc (symsize);
11821 if (sympp == NULL)
11822 return FALSE;
11823
11824 symcount = bfd_canonicalize_symtab (abfd, sympp);
11825 if (symcount < 0)
11826 goto free_sym_buf;
11827
11828 /* Allow the BFD backend to copy any private header data it
11829 understands from the output BFD to the import library BFD. */
11830 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11831 goto free_sym_buf;
11832
11833 /* Filter symbols to appear in the import library. */
11834 if (bed->elf_backend_filter_implib_symbols)
11835 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11836 symcount);
11837 else
11838 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11839 if (symcount == 0)
11840 {
11841 bfd_set_error (bfd_error_no_symbols);
11842 _bfd_error_handler (_("%pB: no symbol found for import library"),
11843 implib_bfd);
11844 goto free_sym_buf;
11845 }
11846
11847
11848 /* Make symbols absolute. */
11849 amt = symcount * sizeof (*osymbuf);
11850 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11851 if (osymbuf == NULL)
11852 goto free_sym_buf;
11853
11854 for (src_count = 0; src_count < symcount; src_count++)
11855 {
11856 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11857 sizeof (*osymbuf));
11858 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11859 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11860 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11861 osymbuf[src_count].internal_elf_sym.st_value =
11862 osymbuf[src_count].symbol.value;
11863 sympp[src_count] = &osymbuf[src_count].symbol;
11864 }
11865
11866 bfd_set_symtab (implib_bfd, sympp, symcount);
11867
11868 /* Allow the BFD backend to copy any private data it understands
11869 from the output BFD to the import library BFD. This is done last
11870 to permit the routine to look at the filtered symbol table. */
11871 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11872 goto free_sym_buf;
11873
11874 if (!bfd_close (implib_bfd))
11875 goto free_sym_buf;
11876
11877 ret = TRUE;
11878
11879 free_sym_buf:
11880 free (sympp);
11881 return ret;
11882 }
11883
11884 static void
11885 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11886 {
11887 asection *o;
11888
11889 if (flinfo->symstrtab != NULL)
11890 _bfd_elf_strtab_free (flinfo->symstrtab);
11891 free (flinfo->contents);
11892 free (flinfo->external_relocs);
11893 free (flinfo->internal_relocs);
11894 free (flinfo->external_syms);
11895 free (flinfo->locsym_shndx);
11896 free (flinfo->internal_syms);
11897 free (flinfo->indices);
11898 free (flinfo->sections);
11899 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11900 free (flinfo->symshndxbuf);
11901 for (o = obfd->sections; o != NULL; o = o->next)
11902 {
11903 struct bfd_elf_section_data *esdo = elf_section_data (o);
11904 free (esdo->rel.hashes);
11905 free (esdo->rela.hashes);
11906 }
11907 }
11908
11909 /* Do the final step of an ELF link. */
11910
11911 bfd_boolean
11912 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11913 {
11914 bfd_boolean dynamic;
11915 bfd_boolean emit_relocs;
11916 bfd *dynobj;
11917 struct elf_final_link_info flinfo;
11918 asection *o;
11919 struct bfd_link_order *p;
11920 bfd *sub;
11921 bfd_size_type max_contents_size;
11922 bfd_size_type max_external_reloc_size;
11923 bfd_size_type max_internal_reloc_count;
11924 bfd_size_type max_sym_count;
11925 bfd_size_type max_sym_shndx_count;
11926 Elf_Internal_Sym elfsym;
11927 unsigned int i;
11928 Elf_Internal_Shdr *symtab_hdr;
11929 Elf_Internal_Shdr *symtab_shndx_hdr;
11930 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11931 struct elf_outext_info eoinfo;
11932 bfd_boolean merged;
11933 size_t relativecount = 0;
11934 asection *reldyn = 0;
11935 bfd_size_type amt;
11936 asection *attr_section = NULL;
11937 bfd_vma attr_size = 0;
11938 const char *std_attrs_section;
11939 struct elf_link_hash_table *htab = elf_hash_table (info);
11940 bfd_boolean sections_removed;
11941
11942 if (!is_elf_hash_table (htab))
11943 return FALSE;
11944
11945 if (bfd_link_pic (info))
11946 abfd->flags |= DYNAMIC;
11947
11948 dynamic = htab->dynamic_sections_created;
11949 dynobj = htab->dynobj;
11950
11951 emit_relocs = (bfd_link_relocatable (info)
11952 || info->emitrelocations);
11953
11954 flinfo.info = info;
11955 flinfo.output_bfd = abfd;
11956 flinfo.symstrtab = _bfd_elf_strtab_init ();
11957 if (flinfo.symstrtab == NULL)
11958 return FALSE;
11959
11960 if (! dynamic)
11961 {
11962 flinfo.hash_sec = NULL;
11963 flinfo.symver_sec = NULL;
11964 }
11965 else
11966 {
11967 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11968 /* Note that dynsym_sec can be NULL (on VMS). */
11969 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11970 /* Note that it is OK if symver_sec is NULL. */
11971 }
11972
11973 flinfo.contents = NULL;
11974 flinfo.external_relocs = NULL;
11975 flinfo.internal_relocs = NULL;
11976 flinfo.external_syms = NULL;
11977 flinfo.locsym_shndx = NULL;
11978 flinfo.internal_syms = NULL;
11979 flinfo.indices = NULL;
11980 flinfo.sections = NULL;
11981 flinfo.symshndxbuf = NULL;
11982 flinfo.filesym_count = 0;
11983
11984 /* The object attributes have been merged. Remove the input
11985 sections from the link, and set the contents of the output
11986 section. */
11987 sections_removed = FALSE;
11988 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11989 for (o = abfd->sections; o != NULL; o = o->next)
11990 {
11991 bfd_boolean remove_section = FALSE;
11992
11993 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11994 || strcmp (o->name, ".gnu.attributes") == 0)
11995 {
11996 for (p = o->map_head.link_order; p != NULL; p = p->next)
11997 {
11998 asection *input_section;
11999
12000 if (p->type != bfd_indirect_link_order)
12001 continue;
12002 input_section = p->u.indirect.section;
12003 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12004 elf_link_input_bfd ignores this section. */
12005 input_section->flags &= ~SEC_HAS_CONTENTS;
12006 }
12007
12008 attr_size = bfd_elf_obj_attr_size (abfd);
12009 bfd_set_section_size (o, attr_size);
12010 /* Skip this section later on. */
12011 o->map_head.link_order = NULL;
12012 if (attr_size)
12013 attr_section = o;
12014 else
12015 remove_section = TRUE;
12016 }
12017 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12018 {
12019 /* Remove empty group section from linker output. */
12020 remove_section = TRUE;
12021 }
12022 if (remove_section)
12023 {
12024 o->flags |= SEC_EXCLUDE;
12025 bfd_section_list_remove (abfd, o);
12026 abfd->section_count--;
12027 sections_removed = TRUE;
12028 }
12029 }
12030 if (sections_removed)
12031 _bfd_fix_excluded_sec_syms (abfd, info);
12032
12033 /* Count up the number of relocations we will output for each output
12034 section, so that we know the sizes of the reloc sections. We
12035 also figure out some maximum sizes. */
12036 max_contents_size = 0;
12037 max_external_reloc_size = 0;
12038 max_internal_reloc_count = 0;
12039 max_sym_count = 0;
12040 max_sym_shndx_count = 0;
12041 merged = FALSE;
12042 for (o = abfd->sections; o != NULL; o = o->next)
12043 {
12044 struct bfd_elf_section_data *esdo = elf_section_data (o);
12045 o->reloc_count = 0;
12046
12047 for (p = o->map_head.link_order; p != NULL; p = p->next)
12048 {
12049 unsigned int reloc_count = 0;
12050 unsigned int additional_reloc_count = 0;
12051 struct bfd_elf_section_data *esdi = NULL;
12052
12053 if (p->type == bfd_section_reloc_link_order
12054 || p->type == bfd_symbol_reloc_link_order)
12055 reloc_count = 1;
12056 else if (p->type == bfd_indirect_link_order)
12057 {
12058 asection *sec;
12059
12060 sec = p->u.indirect.section;
12061
12062 /* Mark all sections which are to be included in the
12063 link. This will normally be every section. We need
12064 to do this so that we can identify any sections which
12065 the linker has decided to not include. */
12066 sec->linker_mark = TRUE;
12067
12068 if (sec->flags & SEC_MERGE)
12069 merged = TRUE;
12070
12071 if (sec->rawsize > max_contents_size)
12072 max_contents_size = sec->rawsize;
12073 if (sec->size > max_contents_size)
12074 max_contents_size = sec->size;
12075
12076 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12077 && (sec->owner->flags & DYNAMIC) == 0)
12078 {
12079 size_t sym_count;
12080
12081 /* We are interested in just local symbols, not all
12082 symbols. */
12083 if (elf_bad_symtab (sec->owner))
12084 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12085 / bed->s->sizeof_sym);
12086 else
12087 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12088
12089 if (sym_count > max_sym_count)
12090 max_sym_count = sym_count;
12091
12092 if (sym_count > max_sym_shndx_count
12093 && elf_symtab_shndx_list (sec->owner) != NULL)
12094 max_sym_shndx_count = sym_count;
12095
12096 if (esdo->this_hdr.sh_type == SHT_REL
12097 || esdo->this_hdr.sh_type == SHT_RELA)
12098 /* Some backends use reloc_count in relocation sections
12099 to count particular types of relocs. Of course,
12100 reloc sections themselves can't have relocations. */
12101 ;
12102 else if (emit_relocs)
12103 {
12104 reloc_count = sec->reloc_count;
12105 if (bed->elf_backend_count_additional_relocs)
12106 {
12107 int c;
12108 c = (*bed->elf_backend_count_additional_relocs) (sec);
12109 additional_reloc_count += c;
12110 }
12111 }
12112 else if (bed->elf_backend_count_relocs)
12113 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12114
12115 esdi = elf_section_data (sec);
12116
12117 if ((sec->flags & SEC_RELOC) != 0)
12118 {
12119 size_t ext_size = 0;
12120
12121 if (esdi->rel.hdr != NULL)
12122 ext_size = esdi->rel.hdr->sh_size;
12123 if (esdi->rela.hdr != NULL)
12124 ext_size += esdi->rela.hdr->sh_size;
12125
12126 if (ext_size > max_external_reloc_size)
12127 max_external_reloc_size = ext_size;
12128 if (sec->reloc_count > max_internal_reloc_count)
12129 max_internal_reloc_count = sec->reloc_count;
12130 }
12131 }
12132 }
12133
12134 if (reloc_count == 0)
12135 continue;
12136
12137 reloc_count += additional_reloc_count;
12138 o->reloc_count += reloc_count;
12139
12140 if (p->type == bfd_indirect_link_order && emit_relocs)
12141 {
12142 if (esdi->rel.hdr)
12143 {
12144 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12145 esdo->rel.count += additional_reloc_count;
12146 }
12147 if (esdi->rela.hdr)
12148 {
12149 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12150 esdo->rela.count += additional_reloc_count;
12151 }
12152 }
12153 else
12154 {
12155 if (o->use_rela_p)
12156 esdo->rela.count += reloc_count;
12157 else
12158 esdo->rel.count += reloc_count;
12159 }
12160 }
12161
12162 if (o->reloc_count > 0)
12163 o->flags |= SEC_RELOC;
12164 else
12165 {
12166 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12167 set it (this is probably a bug) and if it is set
12168 assign_section_numbers will create a reloc section. */
12169 o->flags &=~ SEC_RELOC;
12170 }
12171
12172 /* If the SEC_ALLOC flag is not set, force the section VMA to
12173 zero. This is done in elf_fake_sections as well, but forcing
12174 the VMA to 0 here will ensure that relocs against these
12175 sections are handled correctly. */
12176 if ((o->flags & SEC_ALLOC) == 0
12177 && ! o->user_set_vma)
12178 o->vma = 0;
12179 }
12180
12181 if (! bfd_link_relocatable (info) && merged)
12182 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12183
12184 /* Figure out the file positions for everything but the symbol table
12185 and the relocs. We set symcount to force assign_section_numbers
12186 to create a symbol table. */
12187 abfd->symcount = info->strip != strip_all || emit_relocs;
12188 BFD_ASSERT (! abfd->output_has_begun);
12189 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12190 goto error_return;
12191
12192 /* Set sizes, and assign file positions for reloc sections. */
12193 for (o = abfd->sections; o != NULL; o = o->next)
12194 {
12195 struct bfd_elf_section_data *esdo = elf_section_data (o);
12196 if ((o->flags & SEC_RELOC) != 0)
12197 {
12198 if (esdo->rel.hdr
12199 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12200 goto error_return;
12201
12202 if (esdo->rela.hdr
12203 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12204 goto error_return;
12205 }
12206
12207 /* _bfd_elf_compute_section_file_positions makes temporary use
12208 of target_index. Reset it. */
12209 o->target_index = 0;
12210
12211 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12212 to count upwards while actually outputting the relocations. */
12213 esdo->rel.count = 0;
12214 esdo->rela.count = 0;
12215
12216 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12217 && !bfd_section_is_ctf (o))
12218 {
12219 /* Cache the section contents so that they can be compressed
12220 later. Use bfd_malloc since it will be freed by
12221 bfd_compress_section_contents. */
12222 unsigned char *contents = esdo->this_hdr.contents;
12223 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12224 abort ();
12225 contents
12226 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12227 if (contents == NULL)
12228 goto error_return;
12229 esdo->this_hdr.contents = contents;
12230 }
12231 }
12232
12233 /* We have now assigned file positions for all the sections except .symtab,
12234 .strtab, and non-loaded reloc and compressed debugging sections. We start
12235 the .symtab section at the current file position, and write directly to it.
12236 We build the .strtab section in memory. */
12237 abfd->symcount = 0;
12238 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12239 /* sh_name is set in prep_headers. */
12240 symtab_hdr->sh_type = SHT_SYMTAB;
12241 /* sh_flags, sh_addr and sh_size all start off zero. */
12242 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12243 /* sh_link is set in assign_section_numbers. */
12244 /* sh_info is set below. */
12245 /* sh_offset is set just below. */
12246 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12247
12248 if (max_sym_count < 20)
12249 max_sym_count = 20;
12250 htab->strtabsize = max_sym_count;
12251 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12252 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12253 if (htab->strtab == NULL)
12254 goto error_return;
12255 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12256 flinfo.symshndxbuf
12257 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12258 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12259
12260 if (info->strip != strip_all || emit_relocs)
12261 {
12262 file_ptr off = elf_next_file_pos (abfd);
12263
12264 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12265
12266 /* Note that at this point elf_next_file_pos (abfd) is
12267 incorrect. We do not yet know the size of the .symtab section.
12268 We correct next_file_pos below, after we do know the size. */
12269
12270 /* Start writing out the symbol table. The first symbol is always a
12271 dummy symbol. */
12272 elfsym.st_value = 0;
12273 elfsym.st_size = 0;
12274 elfsym.st_info = 0;
12275 elfsym.st_other = 0;
12276 elfsym.st_shndx = SHN_UNDEF;
12277 elfsym.st_target_internal = 0;
12278 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12279 bfd_und_section_ptr, NULL) != 1)
12280 goto error_return;
12281
12282 /* Output a symbol for each section. We output these even if we are
12283 discarding local symbols, since they are used for relocs. These
12284 symbols have no names. We store the index of each one in the
12285 index field of the section, so that we can find it again when
12286 outputting relocs. */
12287
12288 elfsym.st_size = 0;
12289 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12290 elfsym.st_other = 0;
12291 elfsym.st_value = 0;
12292 elfsym.st_target_internal = 0;
12293 for (i = 1; i < elf_numsections (abfd); i++)
12294 {
12295 o = bfd_section_from_elf_index (abfd, i);
12296 if (o != NULL)
12297 {
12298 o->target_index = bfd_get_symcount (abfd);
12299 elfsym.st_shndx = i;
12300 if (!bfd_link_relocatable (info))
12301 elfsym.st_value = o->vma;
12302 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12303 NULL) != 1)
12304 goto error_return;
12305 }
12306 }
12307 }
12308
12309 /* Allocate some memory to hold information read in from the input
12310 files. */
12311 if (max_contents_size != 0)
12312 {
12313 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12314 if (flinfo.contents == NULL)
12315 goto error_return;
12316 }
12317
12318 if (max_external_reloc_size != 0)
12319 {
12320 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12321 if (flinfo.external_relocs == NULL)
12322 goto error_return;
12323 }
12324
12325 if (max_internal_reloc_count != 0)
12326 {
12327 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12328 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12329 if (flinfo.internal_relocs == NULL)
12330 goto error_return;
12331 }
12332
12333 if (max_sym_count != 0)
12334 {
12335 amt = max_sym_count * bed->s->sizeof_sym;
12336 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12337 if (flinfo.external_syms == NULL)
12338 goto error_return;
12339
12340 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12341 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12342 if (flinfo.internal_syms == NULL)
12343 goto error_return;
12344
12345 amt = max_sym_count * sizeof (long);
12346 flinfo.indices = (long int *) bfd_malloc (amt);
12347 if (flinfo.indices == NULL)
12348 goto error_return;
12349
12350 amt = max_sym_count * sizeof (asection *);
12351 flinfo.sections = (asection **) bfd_malloc (amt);
12352 if (flinfo.sections == NULL)
12353 goto error_return;
12354 }
12355
12356 if (max_sym_shndx_count != 0)
12357 {
12358 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12359 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12360 if (flinfo.locsym_shndx == NULL)
12361 goto error_return;
12362 }
12363
12364 if (htab->tls_sec)
12365 {
12366 bfd_vma base, end = 0; /* Both bytes. */
12367 asection *sec;
12368
12369 for (sec = htab->tls_sec;
12370 sec && (sec->flags & SEC_THREAD_LOCAL);
12371 sec = sec->next)
12372 {
12373 bfd_size_type size = sec->size;
12374 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12375
12376 if (size == 0
12377 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12378 {
12379 struct bfd_link_order *ord = sec->map_tail.link_order;
12380
12381 if (ord != NULL)
12382 size = ord->offset * opb + ord->size;
12383 }
12384 end = sec->vma + size / opb;
12385 }
12386 base = htab->tls_sec->vma;
12387 /* Only align end of TLS section if static TLS doesn't have special
12388 alignment requirements. */
12389 if (bed->static_tls_alignment == 1)
12390 end = align_power (end, htab->tls_sec->alignment_power);
12391 htab->tls_size = end - base;
12392 }
12393
12394 /* Reorder SHF_LINK_ORDER sections. */
12395 for (o = abfd->sections; o != NULL; o = o->next)
12396 {
12397 if (!elf_fixup_link_order (abfd, o))
12398 return FALSE;
12399 }
12400
12401 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12402 return FALSE;
12403
12404 /* Since ELF permits relocations to be against local symbols, we
12405 must have the local symbols available when we do the relocations.
12406 Since we would rather only read the local symbols once, and we
12407 would rather not keep them in memory, we handle all the
12408 relocations for a single input file at the same time.
12409
12410 Unfortunately, there is no way to know the total number of local
12411 symbols until we have seen all of them, and the local symbol
12412 indices precede the global symbol indices. This means that when
12413 we are generating relocatable output, and we see a reloc against
12414 a global symbol, we can not know the symbol index until we have
12415 finished examining all the local symbols to see which ones we are
12416 going to output. To deal with this, we keep the relocations in
12417 memory, and don't output them until the end of the link. This is
12418 an unfortunate waste of memory, but I don't see a good way around
12419 it. Fortunately, it only happens when performing a relocatable
12420 link, which is not the common case. FIXME: If keep_memory is set
12421 we could write the relocs out and then read them again; I don't
12422 know how bad the memory loss will be. */
12423
12424 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12425 sub->output_has_begun = FALSE;
12426 for (o = abfd->sections; o != NULL; o = o->next)
12427 {
12428 for (p = o->map_head.link_order; p != NULL; p = p->next)
12429 {
12430 if (p->type == bfd_indirect_link_order
12431 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12432 == bfd_target_elf_flavour)
12433 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12434 {
12435 if (! sub->output_has_begun)
12436 {
12437 if (! elf_link_input_bfd (&flinfo, sub))
12438 goto error_return;
12439 sub->output_has_begun = TRUE;
12440 }
12441 }
12442 else if (p->type == bfd_section_reloc_link_order
12443 || p->type == bfd_symbol_reloc_link_order)
12444 {
12445 if (! elf_reloc_link_order (abfd, info, o, p))
12446 goto error_return;
12447 }
12448 else
12449 {
12450 if (! _bfd_default_link_order (abfd, info, o, p))
12451 {
12452 if (p->type == bfd_indirect_link_order
12453 && (bfd_get_flavour (sub)
12454 == bfd_target_elf_flavour)
12455 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12456 != bed->s->elfclass))
12457 {
12458 const char *iclass, *oclass;
12459
12460 switch (bed->s->elfclass)
12461 {
12462 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12463 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12464 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12465 default: abort ();
12466 }
12467
12468 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12469 {
12470 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12471 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12472 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12473 default: abort ();
12474 }
12475
12476 bfd_set_error (bfd_error_wrong_format);
12477 _bfd_error_handler
12478 /* xgettext:c-format */
12479 (_("%pB: file class %s incompatible with %s"),
12480 sub, iclass, oclass);
12481 }
12482
12483 goto error_return;
12484 }
12485 }
12486 }
12487 }
12488
12489 /* Free symbol buffer if needed. */
12490 if (!info->reduce_memory_overheads)
12491 {
12492 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12493 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12494 {
12495 free (elf_tdata (sub)->symbuf);
12496 elf_tdata (sub)->symbuf = NULL;
12497 }
12498 }
12499
12500 /* Output any global symbols that got converted to local in a
12501 version script or due to symbol visibility. We do this in a
12502 separate step since ELF requires all local symbols to appear
12503 prior to any global symbols. FIXME: We should only do this if
12504 some global symbols were, in fact, converted to become local.
12505 FIXME: Will this work correctly with the Irix 5 linker? */
12506 eoinfo.failed = FALSE;
12507 eoinfo.flinfo = &flinfo;
12508 eoinfo.localsyms = TRUE;
12509 eoinfo.file_sym_done = FALSE;
12510 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12511 if (eoinfo.failed)
12512 return FALSE;
12513
12514 /* If backend needs to output some local symbols not present in the hash
12515 table, do it now. */
12516 if (bed->elf_backend_output_arch_local_syms
12517 && (info->strip != strip_all || emit_relocs))
12518 {
12519 typedef int (*out_sym_func)
12520 (void *, const char *, Elf_Internal_Sym *, asection *,
12521 struct elf_link_hash_entry *);
12522
12523 if (! ((*bed->elf_backend_output_arch_local_syms)
12524 (abfd, info, &flinfo,
12525 (out_sym_func) elf_link_output_symstrtab)))
12526 return FALSE;
12527 }
12528
12529 /* That wrote out all the local symbols. Finish up the symbol table
12530 with the global symbols. Even if we want to strip everything we
12531 can, we still need to deal with those global symbols that got
12532 converted to local in a version script. */
12533
12534 /* The sh_info field records the index of the first non local symbol. */
12535 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12536
12537 if (dynamic
12538 && htab->dynsym != NULL
12539 && htab->dynsym->output_section != bfd_abs_section_ptr)
12540 {
12541 Elf_Internal_Sym sym;
12542 bfd_byte *dynsym = htab->dynsym->contents;
12543
12544 o = htab->dynsym->output_section;
12545 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12546
12547 /* Write out the section symbols for the output sections. */
12548 if (bfd_link_pic (info)
12549 || htab->is_relocatable_executable)
12550 {
12551 asection *s;
12552
12553 sym.st_size = 0;
12554 sym.st_name = 0;
12555 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12556 sym.st_other = 0;
12557 sym.st_target_internal = 0;
12558
12559 for (s = abfd->sections; s != NULL; s = s->next)
12560 {
12561 int indx;
12562 bfd_byte *dest;
12563 long dynindx;
12564
12565 dynindx = elf_section_data (s)->dynindx;
12566 if (dynindx <= 0)
12567 continue;
12568 indx = elf_section_data (s)->this_idx;
12569 BFD_ASSERT (indx > 0);
12570 sym.st_shndx = indx;
12571 if (! check_dynsym (abfd, &sym))
12572 return FALSE;
12573 sym.st_value = s->vma;
12574 dest = dynsym + dynindx * bed->s->sizeof_sym;
12575 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12576 }
12577 }
12578
12579 /* Write out the local dynsyms. */
12580 if (htab->dynlocal)
12581 {
12582 struct elf_link_local_dynamic_entry *e;
12583 for (e = htab->dynlocal; e ; e = e->next)
12584 {
12585 asection *s;
12586 bfd_byte *dest;
12587
12588 /* Copy the internal symbol and turn off visibility.
12589 Note that we saved a word of storage and overwrote
12590 the original st_name with the dynstr_index. */
12591 sym = e->isym;
12592 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12593
12594 s = bfd_section_from_elf_index (e->input_bfd,
12595 e->isym.st_shndx);
12596 if (s != NULL)
12597 {
12598 sym.st_shndx =
12599 elf_section_data (s->output_section)->this_idx;
12600 if (! check_dynsym (abfd, &sym))
12601 return FALSE;
12602 sym.st_value = (s->output_section->vma
12603 + s->output_offset
12604 + e->isym.st_value);
12605 }
12606
12607 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12608 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12609 }
12610 }
12611 }
12612
12613 /* We get the global symbols from the hash table. */
12614 eoinfo.failed = FALSE;
12615 eoinfo.localsyms = FALSE;
12616 eoinfo.flinfo = &flinfo;
12617 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12618 if (eoinfo.failed)
12619 return FALSE;
12620
12621 /* If backend needs to output some symbols not present in the hash
12622 table, do it now. */
12623 if (bed->elf_backend_output_arch_syms
12624 && (info->strip != strip_all || emit_relocs))
12625 {
12626 typedef int (*out_sym_func)
12627 (void *, const char *, Elf_Internal_Sym *, asection *,
12628 struct elf_link_hash_entry *);
12629
12630 if (! ((*bed->elf_backend_output_arch_syms)
12631 (abfd, info, &flinfo,
12632 (out_sym_func) elf_link_output_symstrtab)))
12633 return FALSE;
12634 }
12635
12636 /* Finalize the .strtab section. */
12637 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12638
12639 /* Swap out the .strtab section. */
12640 if (!elf_link_swap_symbols_out (&flinfo))
12641 return FALSE;
12642
12643 /* Now we know the size of the symtab section. */
12644 if (bfd_get_symcount (abfd) > 0)
12645 {
12646 /* Finish up and write out the symbol string table (.strtab)
12647 section. */
12648 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12649 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12650
12651 if (elf_symtab_shndx_list (abfd))
12652 {
12653 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12654
12655 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12656 {
12657 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12658 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12659 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12660 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12661 symtab_shndx_hdr->sh_size = amt;
12662
12663 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12664 off, TRUE);
12665
12666 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12667 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12668 return FALSE;
12669 }
12670 }
12671
12672 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12673 /* sh_name was set in prep_headers. */
12674 symstrtab_hdr->sh_type = SHT_STRTAB;
12675 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12676 symstrtab_hdr->sh_addr = 0;
12677 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12678 symstrtab_hdr->sh_entsize = 0;
12679 symstrtab_hdr->sh_link = 0;
12680 symstrtab_hdr->sh_info = 0;
12681 /* sh_offset is set just below. */
12682 symstrtab_hdr->sh_addralign = 1;
12683
12684 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12685 off, TRUE);
12686 elf_next_file_pos (abfd) = off;
12687
12688 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12689 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12690 return FALSE;
12691 }
12692
12693 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12694 {
12695 _bfd_error_handler (_("%pB: failed to generate import library"),
12696 info->out_implib_bfd);
12697 return FALSE;
12698 }
12699
12700 /* Adjust the relocs to have the correct symbol indices. */
12701 for (o = abfd->sections; o != NULL; o = o->next)
12702 {
12703 struct bfd_elf_section_data *esdo = elf_section_data (o);
12704 bfd_boolean sort;
12705
12706 if ((o->flags & SEC_RELOC) == 0)
12707 continue;
12708
12709 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12710 if (esdo->rel.hdr != NULL
12711 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12712 return FALSE;
12713 if (esdo->rela.hdr != NULL
12714 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12715 return FALSE;
12716
12717 /* Set the reloc_count field to 0 to prevent write_relocs from
12718 trying to swap the relocs out itself. */
12719 o->reloc_count = 0;
12720 }
12721
12722 if (dynamic && info->combreloc && dynobj != NULL)
12723 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12724
12725 /* If we are linking against a dynamic object, or generating a
12726 shared library, finish up the dynamic linking information. */
12727 if (dynamic)
12728 {
12729 bfd_byte *dyncon, *dynconend;
12730
12731 /* Fix up .dynamic entries. */
12732 o = bfd_get_linker_section (dynobj, ".dynamic");
12733 BFD_ASSERT (o != NULL);
12734
12735 dyncon = o->contents;
12736 dynconend = o->contents + o->size;
12737 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12738 {
12739 Elf_Internal_Dyn dyn;
12740 const char *name;
12741 unsigned int type;
12742 bfd_size_type sh_size;
12743 bfd_vma sh_addr;
12744
12745 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12746
12747 switch (dyn.d_tag)
12748 {
12749 default:
12750 continue;
12751 case DT_NULL:
12752 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12753 {
12754 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12755 {
12756 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12757 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12758 default: continue;
12759 }
12760 dyn.d_un.d_val = relativecount;
12761 relativecount = 0;
12762 break;
12763 }
12764 continue;
12765
12766 case DT_INIT:
12767 name = info->init_function;
12768 goto get_sym;
12769 case DT_FINI:
12770 name = info->fini_function;
12771 get_sym:
12772 {
12773 struct elf_link_hash_entry *h;
12774
12775 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12776 if (h != NULL
12777 && (h->root.type == bfd_link_hash_defined
12778 || h->root.type == bfd_link_hash_defweak))
12779 {
12780 dyn.d_un.d_ptr = h->root.u.def.value;
12781 o = h->root.u.def.section;
12782 if (o->output_section != NULL)
12783 dyn.d_un.d_ptr += (o->output_section->vma
12784 + o->output_offset);
12785 else
12786 {
12787 /* The symbol is imported from another shared
12788 library and does not apply to this one. */
12789 dyn.d_un.d_ptr = 0;
12790 }
12791 break;
12792 }
12793 }
12794 continue;
12795
12796 case DT_PREINIT_ARRAYSZ:
12797 name = ".preinit_array";
12798 goto get_out_size;
12799 case DT_INIT_ARRAYSZ:
12800 name = ".init_array";
12801 goto get_out_size;
12802 case DT_FINI_ARRAYSZ:
12803 name = ".fini_array";
12804 get_out_size:
12805 o = bfd_get_section_by_name (abfd, name);
12806 if (o == NULL)
12807 {
12808 _bfd_error_handler
12809 (_("could not find section %s"), name);
12810 goto error_return;
12811 }
12812 if (o->size == 0)
12813 _bfd_error_handler
12814 (_("warning: %s section has zero size"), name);
12815 dyn.d_un.d_val = o->size;
12816 break;
12817
12818 case DT_PREINIT_ARRAY:
12819 name = ".preinit_array";
12820 goto get_out_vma;
12821 case DT_INIT_ARRAY:
12822 name = ".init_array";
12823 goto get_out_vma;
12824 case DT_FINI_ARRAY:
12825 name = ".fini_array";
12826 get_out_vma:
12827 o = bfd_get_section_by_name (abfd, name);
12828 goto do_vma;
12829
12830 case DT_HASH:
12831 name = ".hash";
12832 goto get_vma;
12833 case DT_GNU_HASH:
12834 name = ".gnu.hash";
12835 goto get_vma;
12836 case DT_STRTAB:
12837 name = ".dynstr";
12838 goto get_vma;
12839 case DT_SYMTAB:
12840 name = ".dynsym";
12841 goto get_vma;
12842 case DT_VERDEF:
12843 name = ".gnu.version_d";
12844 goto get_vma;
12845 case DT_VERNEED:
12846 name = ".gnu.version_r";
12847 goto get_vma;
12848 case DT_VERSYM:
12849 name = ".gnu.version";
12850 get_vma:
12851 o = bfd_get_linker_section (dynobj, name);
12852 do_vma:
12853 if (o == NULL || bfd_is_abs_section (o->output_section))
12854 {
12855 _bfd_error_handler
12856 (_("could not find section %s"), name);
12857 goto error_return;
12858 }
12859 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12860 {
12861 _bfd_error_handler
12862 (_("warning: section '%s' is being made into a note"), name);
12863 bfd_set_error (bfd_error_nonrepresentable_section);
12864 goto error_return;
12865 }
12866 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12867 break;
12868
12869 case DT_REL:
12870 case DT_RELA:
12871 case DT_RELSZ:
12872 case DT_RELASZ:
12873 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12874 type = SHT_REL;
12875 else
12876 type = SHT_RELA;
12877 sh_size = 0;
12878 sh_addr = 0;
12879 for (i = 1; i < elf_numsections (abfd); i++)
12880 {
12881 Elf_Internal_Shdr *hdr;
12882
12883 hdr = elf_elfsections (abfd)[i];
12884 if (hdr->sh_type == type
12885 && (hdr->sh_flags & SHF_ALLOC) != 0)
12886 {
12887 sh_size += hdr->sh_size;
12888 if (sh_addr == 0
12889 || sh_addr > hdr->sh_addr)
12890 sh_addr = hdr->sh_addr;
12891 }
12892 }
12893
12894 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12895 {
12896 unsigned int opb = bfd_octets_per_byte (abfd, o);
12897
12898 /* Don't count procedure linkage table relocs in the
12899 overall reloc count. */
12900 sh_size -= htab->srelplt->size;
12901 if (sh_size == 0)
12902 /* If the size is zero, make the address zero too.
12903 This is to avoid a glibc bug. If the backend
12904 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12905 zero, then we'll put DT_RELA at the end of
12906 DT_JMPREL. glibc will interpret the end of
12907 DT_RELA matching the end of DT_JMPREL as the
12908 case where DT_RELA includes DT_JMPREL, and for
12909 LD_BIND_NOW will decide that processing DT_RELA
12910 will process the PLT relocs too. Net result:
12911 No PLT relocs applied. */
12912 sh_addr = 0;
12913
12914 /* If .rela.plt is the first .rela section, exclude
12915 it from DT_RELA. */
12916 else if (sh_addr == (htab->srelplt->output_section->vma
12917 + htab->srelplt->output_offset) * opb)
12918 sh_addr += htab->srelplt->size;
12919 }
12920
12921 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12922 dyn.d_un.d_val = sh_size;
12923 else
12924 dyn.d_un.d_ptr = sh_addr;
12925 break;
12926 }
12927 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12928 }
12929 }
12930
12931 /* If we have created any dynamic sections, then output them. */
12932 if (dynobj != NULL)
12933 {
12934 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12935 goto error_return;
12936
12937 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12938 if (bfd_link_textrel_check (info)
12939 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12940 {
12941 bfd_byte *dyncon, *dynconend;
12942
12943 dyncon = o->contents;
12944 dynconend = o->contents + o->size;
12945 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12946 {
12947 Elf_Internal_Dyn dyn;
12948
12949 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12950
12951 if (dyn.d_tag == DT_TEXTREL)
12952 {
12953 if (info->textrel_check == textrel_check_error)
12954 info->callbacks->einfo
12955 (_("%P%X: read-only segment has dynamic relocations\n"));
12956 else if (bfd_link_dll (info))
12957 info->callbacks->einfo
12958 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
12959 else
12960 info->callbacks->einfo
12961 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
12962 break;
12963 }
12964 }
12965 }
12966
12967 for (o = dynobj->sections; o != NULL; o = o->next)
12968 {
12969 if ((o->flags & SEC_HAS_CONTENTS) == 0
12970 || o->size == 0
12971 || o->output_section == bfd_abs_section_ptr)
12972 continue;
12973 if ((o->flags & SEC_LINKER_CREATED) == 0)
12974 {
12975 /* At this point, we are only interested in sections
12976 created by _bfd_elf_link_create_dynamic_sections. */
12977 continue;
12978 }
12979 if (htab->stab_info.stabstr == o)
12980 continue;
12981 if (htab->eh_info.hdr_sec == o)
12982 continue;
12983 if (strcmp (o->name, ".dynstr") != 0)
12984 {
12985 bfd_size_type octets = ((file_ptr) o->output_offset
12986 * bfd_octets_per_byte (abfd, o));
12987 if (!bfd_set_section_contents (abfd, o->output_section,
12988 o->contents, octets, o->size))
12989 goto error_return;
12990 }
12991 else
12992 {
12993 /* The contents of the .dynstr section are actually in a
12994 stringtab. */
12995 file_ptr off;
12996
12997 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12998 if (bfd_seek (abfd, off, SEEK_SET) != 0
12999 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13000 goto error_return;
13001 }
13002 }
13003 }
13004
13005 if (!info->resolve_section_groups)
13006 {
13007 bfd_boolean failed = FALSE;
13008
13009 BFD_ASSERT (bfd_link_relocatable (info));
13010 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13011 if (failed)
13012 goto error_return;
13013 }
13014
13015 /* If we have optimized stabs strings, output them. */
13016 if (htab->stab_info.stabstr != NULL)
13017 {
13018 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13019 goto error_return;
13020 }
13021
13022 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13023 goto error_return;
13024
13025 if (info->callbacks->emit_ctf)
13026 info->callbacks->emit_ctf ();
13027
13028 elf_final_link_free (abfd, &flinfo);
13029
13030 if (attr_section)
13031 {
13032 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13033 if (contents == NULL)
13034 return FALSE; /* Bail out and fail. */
13035 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13036 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13037 free (contents);
13038 }
13039
13040 return TRUE;
13041
13042 error_return:
13043 elf_final_link_free (abfd, &flinfo);
13044 return FALSE;
13045 }
13046 \f
13047 /* Initialize COOKIE for input bfd ABFD. */
13048
13049 static bfd_boolean
13050 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13051 struct bfd_link_info *info, bfd *abfd)
13052 {
13053 Elf_Internal_Shdr *symtab_hdr;
13054 const struct elf_backend_data *bed;
13055
13056 bed = get_elf_backend_data (abfd);
13057 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13058
13059 cookie->abfd = abfd;
13060 cookie->sym_hashes = elf_sym_hashes (abfd);
13061 cookie->bad_symtab = elf_bad_symtab (abfd);
13062 if (cookie->bad_symtab)
13063 {
13064 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13065 cookie->extsymoff = 0;
13066 }
13067 else
13068 {
13069 cookie->locsymcount = symtab_hdr->sh_info;
13070 cookie->extsymoff = symtab_hdr->sh_info;
13071 }
13072
13073 if (bed->s->arch_size == 32)
13074 cookie->r_sym_shift = 8;
13075 else
13076 cookie->r_sym_shift = 32;
13077
13078 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13079 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13080 {
13081 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13082 cookie->locsymcount, 0,
13083 NULL, NULL, NULL);
13084 if (cookie->locsyms == NULL)
13085 {
13086 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13087 return FALSE;
13088 }
13089 if (info->keep_memory)
13090 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13091 }
13092 return TRUE;
13093 }
13094
13095 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13096
13097 static void
13098 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13099 {
13100 Elf_Internal_Shdr *symtab_hdr;
13101
13102 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13103 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13104 free (cookie->locsyms);
13105 }
13106
13107 /* Initialize the relocation information in COOKIE for input section SEC
13108 of input bfd ABFD. */
13109
13110 static bfd_boolean
13111 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13112 struct bfd_link_info *info, bfd *abfd,
13113 asection *sec)
13114 {
13115 if (sec->reloc_count == 0)
13116 {
13117 cookie->rels = NULL;
13118 cookie->relend = NULL;
13119 }
13120 else
13121 {
13122 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13123 info->keep_memory);
13124 if (cookie->rels == NULL)
13125 return FALSE;
13126 cookie->rel = cookie->rels;
13127 cookie->relend = cookie->rels + sec->reloc_count;
13128 }
13129 cookie->rel = cookie->rels;
13130 return TRUE;
13131 }
13132
13133 /* Free the memory allocated by init_reloc_cookie_rels,
13134 if appropriate. */
13135
13136 static void
13137 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13138 asection *sec)
13139 {
13140 if (elf_section_data (sec)->relocs != cookie->rels)
13141 free (cookie->rels);
13142 }
13143
13144 /* Initialize the whole of COOKIE for input section SEC. */
13145
13146 static bfd_boolean
13147 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13148 struct bfd_link_info *info,
13149 asection *sec)
13150 {
13151 if (!init_reloc_cookie (cookie, info, sec->owner))
13152 goto error1;
13153 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13154 goto error2;
13155 return TRUE;
13156
13157 error2:
13158 fini_reloc_cookie (cookie, sec->owner);
13159 error1:
13160 return FALSE;
13161 }
13162
13163 /* Free the memory allocated by init_reloc_cookie_for_section,
13164 if appropriate. */
13165
13166 static void
13167 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13168 asection *sec)
13169 {
13170 fini_reloc_cookie_rels (cookie, sec);
13171 fini_reloc_cookie (cookie, sec->owner);
13172 }
13173 \f
13174 /* Garbage collect unused sections. */
13175
13176 /* Default gc_mark_hook. */
13177
13178 asection *
13179 _bfd_elf_gc_mark_hook (asection *sec,
13180 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13181 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13182 struct elf_link_hash_entry *h,
13183 Elf_Internal_Sym *sym)
13184 {
13185 if (h != NULL)
13186 {
13187 switch (h->root.type)
13188 {
13189 case bfd_link_hash_defined:
13190 case bfd_link_hash_defweak:
13191 return h->root.u.def.section;
13192
13193 case bfd_link_hash_common:
13194 return h->root.u.c.p->section;
13195
13196 default:
13197 break;
13198 }
13199 }
13200 else
13201 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13202
13203 return NULL;
13204 }
13205
13206 /* Return the debug definition section. */
13207
13208 static asection *
13209 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13210 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13211 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13212 struct elf_link_hash_entry *h,
13213 Elf_Internal_Sym *sym)
13214 {
13215 if (h != NULL)
13216 {
13217 /* Return the global debug definition section. */
13218 if ((h->root.type == bfd_link_hash_defined
13219 || h->root.type == bfd_link_hash_defweak)
13220 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13221 return h->root.u.def.section;
13222 }
13223 else
13224 {
13225 /* Return the local debug definition section. */
13226 asection *isec = bfd_section_from_elf_index (sec->owner,
13227 sym->st_shndx);
13228 if ((isec->flags & SEC_DEBUGGING) != 0)
13229 return isec;
13230 }
13231
13232 return NULL;
13233 }
13234
13235 /* COOKIE->rel describes a relocation against section SEC, which is
13236 a section we've decided to keep. Return the section that contains
13237 the relocation symbol, or NULL if no section contains it. */
13238
13239 asection *
13240 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13241 elf_gc_mark_hook_fn gc_mark_hook,
13242 struct elf_reloc_cookie *cookie,
13243 bfd_boolean *start_stop)
13244 {
13245 unsigned long r_symndx;
13246 struct elf_link_hash_entry *h, *hw;
13247
13248 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13249 if (r_symndx == STN_UNDEF)
13250 return NULL;
13251
13252 if (r_symndx >= cookie->locsymcount
13253 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13254 {
13255 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13256 if (h == NULL)
13257 {
13258 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13259 sec->owner);
13260 return NULL;
13261 }
13262 while (h->root.type == bfd_link_hash_indirect
13263 || h->root.type == bfd_link_hash_warning)
13264 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13265 h->mark = 1;
13266 /* Keep all aliases of the symbol too. If an object symbol
13267 needs to be copied into .dynbss then all of its aliases
13268 should be present as dynamic symbols, not just the one used
13269 on the copy relocation. */
13270 hw = h;
13271 while (hw->is_weakalias)
13272 {
13273 hw = hw->u.alias;
13274 hw->mark = 1;
13275 }
13276
13277 if (start_stop != NULL)
13278 {
13279 /* To work around a glibc bug, mark XXX input sections
13280 when there is a reference to __start_XXX or __stop_XXX
13281 symbols. */
13282 if (h->start_stop)
13283 {
13284 asection *s = h->u2.start_stop_section;
13285 *start_stop = !s->gc_mark;
13286 return s;
13287 }
13288 }
13289
13290 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13291 }
13292
13293 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13294 &cookie->locsyms[r_symndx]);
13295 }
13296
13297 /* COOKIE->rel describes a relocation against section SEC, which is
13298 a section we've decided to keep. Mark the section that contains
13299 the relocation symbol. */
13300
13301 bfd_boolean
13302 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13303 asection *sec,
13304 elf_gc_mark_hook_fn gc_mark_hook,
13305 struct elf_reloc_cookie *cookie)
13306 {
13307 asection *rsec;
13308 bfd_boolean start_stop = FALSE;
13309
13310 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13311 while (rsec != NULL)
13312 {
13313 if (!rsec->gc_mark)
13314 {
13315 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13316 || (rsec->owner->flags & DYNAMIC) != 0)
13317 rsec->gc_mark = 1;
13318 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13319 return FALSE;
13320 }
13321 if (!start_stop)
13322 break;
13323 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13324 }
13325 return TRUE;
13326 }
13327
13328 /* The mark phase of garbage collection. For a given section, mark
13329 it and any sections in this section's group, and all the sections
13330 which define symbols to which it refers. */
13331
13332 bfd_boolean
13333 _bfd_elf_gc_mark (struct bfd_link_info *info,
13334 asection *sec,
13335 elf_gc_mark_hook_fn gc_mark_hook)
13336 {
13337 bfd_boolean ret;
13338 asection *group_sec, *eh_frame;
13339
13340 sec->gc_mark = 1;
13341
13342 /* Mark all the sections in the group. */
13343 group_sec = elf_section_data (sec)->next_in_group;
13344 if (group_sec && !group_sec->gc_mark)
13345 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13346 return FALSE;
13347
13348 /* Look through the section relocs. */
13349 ret = TRUE;
13350 eh_frame = elf_eh_frame_section (sec->owner);
13351 if ((sec->flags & SEC_RELOC) != 0
13352 && sec->reloc_count > 0
13353 && sec != eh_frame)
13354 {
13355 struct elf_reloc_cookie cookie;
13356
13357 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13358 ret = FALSE;
13359 else
13360 {
13361 for (; cookie.rel < cookie.relend; cookie.rel++)
13362 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13363 {
13364 ret = FALSE;
13365 break;
13366 }
13367 fini_reloc_cookie_for_section (&cookie, sec);
13368 }
13369 }
13370
13371 if (ret && eh_frame && elf_fde_list (sec))
13372 {
13373 struct elf_reloc_cookie cookie;
13374
13375 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13376 ret = FALSE;
13377 else
13378 {
13379 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13380 gc_mark_hook, &cookie))
13381 ret = FALSE;
13382 fini_reloc_cookie_for_section (&cookie, eh_frame);
13383 }
13384 }
13385
13386 eh_frame = elf_section_eh_frame_entry (sec);
13387 if (ret && eh_frame && !eh_frame->gc_mark)
13388 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13389 ret = FALSE;
13390
13391 return ret;
13392 }
13393
13394 /* Scan and mark sections in a special or debug section group. */
13395
13396 static void
13397 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13398 {
13399 /* Point to first section of section group. */
13400 asection *ssec;
13401 /* Used to iterate the section group. */
13402 asection *msec;
13403
13404 bfd_boolean is_special_grp = TRUE;
13405 bfd_boolean is_debug_grp = TRUE;
13406
13407 /* First scan to see if group contains any section other than debug
13408 and special section. */
13409 ssec = msec = elf_next_in_group (grp);
13410 do
13411 {
13412 if ((msec->flags & SEC_DEBUGGING) == 0)
13413 is_debug_grp = FALSE;
13414
13415 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13416 is_special_grp = FALSE;
13417
13418 msec = elf_next_in_group (msec);
13419 }
13420 while (msec != ssec);
13421
13422 /* If this is a pure debug section group or pure special section group,
13423 keep all sections in this group. */
13424 if (is_debug_grp || is_special_grp)
13425 {
13426 do
13427 {
13428 msec->gc_mark = 1;
13429 msec = elf_next_in_group (msec);
13430 }
13431 while (msec != ssec);
13432 }
13433 }
13434
13435 /* Keep debug and special sections. */
13436
13437 bfd_boolean
13438 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13439 elf_gc_mark_hook_fn mark_hook)
13440 {
13441 bfd *ibfd;
13442
13443 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13444 {
13445 asection *isec;
13446 bfd_boolean some_kept;
13447 bfd_boolean debug_frag_seen;
13448 bfd_boolean has_kept_debug_info;
13449
13450 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13451 continue;
13452 isec = ibfd->sections;
13453 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13454 continue;
13455
13456 /* Ensure all linker created sections are kept,
13457 see if any other section is already marked,
13458 and note if we have any fragmented debug sections. */
13459 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13460 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13461 {
13462 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13463 isec->gc_mark = 1;
13464 else if (isec->gc_mark
13465 && (isec->flags & SEC_ALLOC) != 0
13466 && elf_section_type (isec) != SHT_NOTE)
13467 some_kept = TRUE;
13468 else
13469 {
13470 /* Since all sections, except for backend specific ones,
13471 have been garbage collected, call mark_hook on this
13472 section if any of its linked-to sections is marked. */
13473 asection *linked_to_sec = elf_linked_to_section (isec);
13474 for (; linked_to_sec != NULL;
13475 linked_to_sec = elf_linked_to_section (linked_to_sec))
13476 if (linked_to_sec->gc_mark)
13477 {
13478 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13479 return FALSE;
13480 break;
13481 }
13482 }
13483
13484 if (!debug_frag_seen
13485 && (isec->flags & SEC_DEBUGGING)
13486 && CONST_STRNEQ (isec->name, ".debug_line."))
13487 debug_frag_seen = TRUE;
13488 else if (strcmp (bfd_section_name (isec),
13489 "__patchable_function_entries") == 0
13490 && elf_linked_to_section (isec) == NULL)
13491 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13492 "need linked-to section "
13493 "for --gc-sections\n"),
13494 isec->owner, isec);
13495 }
13496
13497 /* If no non-note alloc section in this file will be kept, then
13498 we can toss out the debug and special sections. */
13499 if (!some_kept)
13500 continue;
13501
13502 /* Keep debug and special sections like .comment when they are
13503 not part of a group. Also keep section groups that contain
13504 just debug sections or special sections. NB: Sections with
13505 linked-to section has been handled above. */
13506 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13507 {
13508 if ((isec->flags & SEC_GROUP) != 0)
13509 _bfd_elf_gc_mark_debug_special_section_group (isec);
13510 else if (((isec->flags & SEC_DEBUGGING) != 0
13511 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13512 && elf_next_in_group (isec) == NULL
13513 && elf_linked_to_section (isec) == NULL)
13514 isec->gc_mark = 1;
13515 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13516 has_kept_debug_info = TRUE;
13517 }
13518
13519 /* Look for CODE sections which are going to be discarded,
13520 and find and discard any fragmented debug sections which
13521 are associated with that code section. */
13522 if (debug_frag_seen)
13523 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13524 if ((isec->flags & SEC_CODE) != 0
13525 && isec->gc_mark == 0)
13526 {
13527 unsigned int ilen;
13528 asection *dsec;
13529
13530 ilen = strlen (isec->name);
13531
13532 /* Association is determined by the name of the debug
13533 section containing the name of the code section as
13534 a suffix. For example .debug_line.text.foo is a
13535 debug section associated with .text.foo. */
13536 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13537 {
13538 unsigned int dlen;
13539
13540 if (dsec->gc_mark == 0
13541 || (dsec->flags & SEC_DEBUGGING) == 0)
13542 continue;
13543
13544 dlen = strlen (dsec->name);
13545
13546 if (dlen > ilen
13547 && strncmp (dsec->name + (dlen - ilen),
13548 isec->name, ilen) == 0)
13549 dsec->gc_mark = 0;
13550 }
13551 }
13552
13553 /* Mark debug sections referenced by kept debug sections. */
13554 if (has_kept_debug_info)
13555 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13556 if (isec->gc_mark
13557 && (isec->flags & SEC_DEBUGGING) != 0)
13558 if (!_bfd_elf_gc_mark (info, isec,
13559 elf_gc_mark_debug_section))
13560 return FALSE;
13561 }
13562 return TRUE;
13563 }
13564
13565 static bfd_boolean
13566 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13567 {
13568 bfd *sub;
13569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13570
13571 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13572 {
13573 asection *o;
13574
13575 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13576 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13577 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13578 continue;
13579 o = sub->sections;
13580 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13581 continue;
13582
13583 for (o = sub->sections; o != NULL; o = o->next)
13584 {
13585 /* When any section in a section group is kept, we keep all
13586 sections in the section group. If the first member of
13587 the section group is excluded, we will also exclude the
13588 group section. */
13589 if (o->flags & SEC_GROUP)
13590 {
13591 asection *first = elf_next_in_group (o);
13592 o->gc_mark = first->gc_mark;
13593 }
13594
13595 if (o->gc_mark)
13596 continue;
13597
13598 /* Skip sweeping sections already excluded. */
13599 if (o->flags & SEC_EXCLUDE)
13600 continue;
13601
13602 /* Since this is early in the link process, it is simple
13603 to remove a section from the output. */
13604 o->flags |= SEC_EXCLUDE;
13605
13606 if (info->print_gc_sections && o->size != 0)
13607 /* xgettext:c-format */
13608 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13609 o, sub);
13610 }
13611 }
13612
13613 return TRUE;
13614 }
13615
13616 /* Propagate collected vtable information. This is called through
13617 elf_link_hash_traverse. */
13618
13619 static bfd_boolean
13620 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13621 {
13622 /* Those that are not vtables. */
13623 if (h->start_stop
13624 || h->u2.vtable == NULL
13625 || h->u2.vtable->parent == NULL)
13626 return TRUE;
13627
13628 /* Those vtables that do not have parents, we cannot merge. */
13629 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13630 return TRUE;
13631
13632 /* If we've already been done, exit. */
13633 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13634 return TRUE;
13635
13636 /* Make sure the parent's table is up to date. */
13637 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13638
13639 if (h->u2.vtable->used == NULL)
13640 {
13641 /* None of this table's entries were referenced. Re-use the
13642 parent's table. */
13643 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13644 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13645 }
13646 else
13647 {
13648 size_t n;
13649 bfd_boolean *cu, *pu;
13650
13651 /* Or the parent's entries into ours. */
13652 cu = h->u2.vtable->used;
13653 cu[-1] = TRUE;
13654 pu = h->u2.vtable->parent->u2.vtable->used;
13655 if (pu != NULL)
13656 {
13657 const struct elf_backend_data *bed;
13658 unsigned int log_file_align;
13659
13660 bed = get_elf_backend_data (h->root.u.def.section->owner);
13661 log_file_align = bed->s->log_file_align;
13662 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13663 while (n--)
13664 {
13665 if (*pu)
13666 *cu = TRUE;
13667 pu++;
13668 cu++;
13669 }
13670 }
13671 }
13672
13673 return TRUE;
13674 }
13675
13676 static bfd_boolean
13677 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13678 {
13679 asection *sec;
13680 bfd_vma hstart, hend;
13681 Elf_Internal_Rela *relstart, *relend, *rel;
13682 const struct elf_backend_data *bed;
13683 unsigned int log_file_align;
13684
13685 /* Take care of both those symbols that do not describe vtables as
13686 well as those that are not loaded. */
13687 if (h->start_stop
13688 || h->u2.vtable == NULL
13689 || h->u2.vtable->parent == NULL)
13690 return TRUE;
13691
13692 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13693 || h->root.type == bfd_link_hash_defweak);
13694
13695 sec = h->root.u.def.section;
13696 hstart = h->root.u.def.value;
13697 hend = hstart + h->size;
13698
13699 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13700 if (!relstart)
13701 return *(bfd_boolean *) okp = FALSE;
13702 bed = get_elf_backend_data (sec->owner);
13703 log_file_align = bed->s->log_file_align;
13704
13705 relend = relstart + sec->reloc_count;
13706
13707 for (rel = relstart; rel < relend; ++rel)
13708 if (rel->r_offset >= hstart && rel->r_offset < hend)
13709 {
13710 /* If the entry is in use, do nothing. */
13711 if (h->u2.vtable->used
13712 && (rel->r_offset - hstart) < h->u2.vtable->size)
13713 {
13714 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13715 if (h->u2.vtable->used[entry])
13716 continue;
13717 }
13718 /* Otherwise, kill it. */
13719 rel->r_offset = rel->r_info = rel->r_addend = 0;
13720 }
13721
13722 return TRUE;
13723 }
13724
13725 /* Mark sections containing dynamically referenced symbols. When
13726 building shared libraries, we must assume that any visible symbol is
13727 referenced. */
13728
13729 bfd_boolean
13730 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13731 {
13732 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13733 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13734
13735 if ((h->root.type == bfd_link_hash_defined
13736 || h->root.type == bfd_link_hash_defweak)
13737 && ((h->ref_dynamic && !h->forced_local)
13738 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13739 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13740 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13741 && (!bfd_link_executable (info)
13742 || info->gc_keep_exported
13743 || info->export_dynamic
13744 || (h->dynamic
13745 && d != NULL
13746 && (*d->match) (&d->head, NULL, h->root.root.string)))
13747 && (h->versioned >= versioned
13748 || !bfd_hide_sym_by_version (info->version_info,
13749 h->root.root.string)))))
13750 h->root.u.def.section->flags |= SEC_KEEP;
13751
13752 return TRUE;
13753 }
13754
13755 /* Keep all sections containing symbols undefined on the command-line,
13756 and the section containing the entry symbol. */
13757
13758 void
13759 _bfd_elf_gc_keep (struct bfd_link_info *info)
13760 {
13761 struct bfd_sym_chain *sym;
13762
13763 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13764 {
13765 struct elf_link_hash_entry *h;
13766
13767 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13768 FALSE, FALSE, FALSE);
13769
13770 if (h != NULL
13771 && (h->root.type == bfd_link_hash_defined
13772 || h->root.type == bfd_link_hash_defweak)
13773 && !bfd_is_abs_section (h->root.u.def.section)
13774 && !bfd_is_und_section (h->root.u.def.section))
13775 h->root.u.def.section->flags |= SEC_KEEP;
13776 }
13777 }
13778
13779 bfd_boolean
13780 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13781 struct bfd_link_info *info)
13782 {
13783 bfd *ibfd = info->input_bfds;
13784
13785 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13786 {
13787 asection *sec;
13788 struct elf_reloc_cookie cookie;
13789
13790 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13791 continue;
13792 sec = ibfd->sections;
13793 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13794 continue;
13795
13796 if (!init_reloc_cookie (&cookie, info, ibfd))
13797 return FALSE;
13798
13799 for (sec = ibfd->sections; sec; sec = sec->next)
13800 {
13801 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13802 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13803 {
13804 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13805 fini_reloc_cookie_rels (&cookie, sec);
13806 }
13807 }
13808 }
13809 return TRUE;
13810 }
13811
13812 /* Do mark and sweep of unused sections. */
13813
13814 bfd_boolean
13815 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13816 {
13817 bfd_boolean ok = TRUE;
13818 bfd *sub;
13819 elf_gc_mark_hook_fn gc_mark_hook;
13820 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13821 struct elf_link_hash_table *htab;
13822
13823 if (!bed->can_gc_sections
13824 || !is_elf_hash_table (info->hash))
13825 {
13826 _bfd_error_handler(_("warning: gc-sections option ignored"));
13827 return TRUE;
13828 }
13829
13830 bed->gc_keep (info);
13831 htab = elf_hash_table (info);
13832
13833 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13834 at the .eh_frame section if we can mark the FDEs individually. */
13835 for (sub = info->input_bfds;
13836 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13837 sub = sub->link.next)
13838 {
13839 asection *sec;
13840 struct elf_reloc_cookie cookie;
13841
13842 sec = sub->sections;
13843 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13844 continue;
13845 sec = bfd_get_section_by_name (sub, ".eh_frame");
13846 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13847 {
13848 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13849 if (elf_section_data (sec)->sec_info
13850 && (sec->flags & SEC_LINKER_CREATED) == 0)
13851 elf_eh_frame_section (sub) = sec;
13852 fini_reloc_cookie_for_section (&cookie, sec);
13853 sec = bfd_get_next_section_by_name (NULL, sec);
13854 }
13855 }
13856
13857 /* Apply transitive closure to the vtable entry usage info. */
13858 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13859 if (!ok)
13860 return FALSE;
13861
13862 /* Kill the vtable relocations that were not used. */
13863 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13864 if (!ok)
13865 return FALSE;
13866
13867 /* Mark dynamically referenced symbols. */
13868 if (htab->dynamic_sections_created || info->gc_keep_exported)
13869 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13870
13871 /* Grovel through relocs to find out who stays ... */
13872 gc_mark_hook = bed->gc_mark_hook;
13873 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13874 {
13875 asection *o;
13876
13877 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13878 || elf_object_id (sub) != elf_hash_table_id (htab)
13879 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13880 continue;
13881
13882 o = sub->sections;
13883 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13884 continue;
13885
13886 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13887 Also treat note sections as a root, if the section is not part
13888 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13889 well as FINI_ARRAY sections for ld -r. */
13890 for (o = sub->sections; o != NULL; o = o->next)
13891 if (!o->gc_mark
13892 && (o->flags & SEC_EXCLUDE) == 0
13893 && ((o->flags & SEC_KEEP) != 0
13894 || (bfd_link_relocatable (info)
13895 && ((elf_section_data (o)->this_hdr.sh_type
13896 == SHT_PREINIT_ARRAY)
13897 || (elf_section_data (o)->this_hdr.sh_type
13898 == SHT_INIT_ARRAY)
13899 || (elf_section_data (o)->this_hdr.sh_type
13900 == SHT_FINI_ARRAY)))
13901 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13902 && elf_next_in_group (o) == NULL )))
13903 {
13904 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13905 return FALSE;
13906 }
13907 }
13908
13909 /* Allow the backend to mark additional target specific sections. */
13910 bed->gc_mark_extra_sections (info, gc_mark_hook);
13911
13912 /* ... and mark SEC_EXCLUDE for those that go. */
13913 return elf_gc_sweep (abfd, info);
13914 }
13915 \f
13916 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13917
13918 bfd_boolean
13919 bfd_elf_gc_record_vtinherit (bfd *abfd,
13920 asection *sec,
13921 struct elf_link_hash_entry *h,
13922 bfd_vma offset)
13923 {
13924 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13925 struct elf_link_hash_entry **search, *child;
13926 size_t extsymcount;
13927 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13928
13929 /* The sh_info field of the symtab header tells us where the
13930 external symbols start. We don't care about the local symbols at
13931 this point. */
13932 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13933 if (!elf_bad_symtab (abfd))
13934 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13935
13936 sym_hashes = elf_sym_hashes (abfd);
13937 sym_hashes_end = sym_hashes + extsymcount;
13938
13939 /* Hunt down the child symbol, which is in this section at the same
13940 offset as the relocation. */
13941 for (search = sym_hashes; search != sym_hashes_end; ++search)
13942 {
13943 if ((child = *search) != NULL
13944 && (child->root.type == bfd_link_hash_defined
13945 || child->root.type == bfd_link_hash_defweak)
13946 && child->root.u.def.section == sec
13947 && child->root.u.def.value == offset)
13948 goto win;
13949 }
13950
13951 /* xgettext:c-format */
13952 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13953 abfd, sec, (uint64_t) offset);
13954 bfd_set_error (bfd_error_invalid_operation);
13955 return FALSE;
13956
13957 win:
13958 if (!child->u2.vtable)
13959 {
13960 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13961 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13962 if (!child->u2.vtable)
13963 return FALSE;
13964 }
13965 if (!h)
13966 {
13967 /* This *should* only be the absolute section. It could potentially
13968 be that someone has defined a non-global vtable though, which
13969 would be bad. It isn't worth paging in the local symbols to be
13970 sure though; that case should simply be handled by the assembler. */
13971
13972 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13973 }
13974 else
13975 child->u2.vtable->parent = h;
13976
13977 return TRUE;
13978 }
13979
13980 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13981
13982 bfd_boolean
13983 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13984 struct elf_link_hash_entry *h,
13985 bfd_vma addend)
13986 {
13987 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13988 unsigned int log_file_align = bed->s->log_file_align;
13989
13990 if (!h)
13991 {
13992 /* xgettext:c-format */
13993 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13994 abfd, sec);
13995 bfd_set_error (bfd_error_bad_value);
13996 return FALSE;
13997 }
13998
13999 if (!h->u2.vtable)
14000 {
14001 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14002 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14003 if (!h->u2.vtable)
14004 return FALSE;
14005 }
14006
14007 if (addend >= h->u2.vtable->size)
14008 {
14009 size_t size, bytes, file_align;
14010 bfd_boolean *ptr = h->u2.vtable->used;
14011
14012 /* While the symbol is undefined, we have to be prepared to handle
14013 a zero size. */
14014 file_align = 1 << log_file_align;
14015 if (h->root.type == bfd_link_hash_undefined)
14016 size = addend + file_align;
14017 else
14018 {
14019 size = h->size;
14020 if (addend >= size)
14021 {
14022 /* Oops! We've got a reference past the defined end of
14023 the table. This is probably a bug -- shall we warn? */
14024 size = addend + file_align;
14025 }
14026 }
14027 size = (size + file_align - 1) & -file_align;
14028
14029 /* Allocate one extra entry for use as a "done" flag for the
14030 consolidation pass. */
14031 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14032
14033 if (ptr)
14034 {
14035 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14036
14037 if (ptr != NULL)
14038 {
14039 size_t oldbytes;
14040
14041 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14042 * sizeof (bfd_boolean));
14043 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14044 }
14045 }
14046 else
14047 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14048
14049 if (ptr == NULL)
14050 return FALSE;
14051
14052 /* And arrange for that done flag to be at index -1. */
14053 h->u2.vtable->used = ptr + 1;
14054 h->u2.vtable->size = size;
14055 }
14056
14057 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14058
14059 return TRUE;
14060 }
14061
14062 /* Map an ELF section header flag to its corresponding string. */
14063 typedef struct
14064 {
14065 char *flag_name;
14066 flagword flag_value;
14067 } elf_flags_to_name_table;
14068
14069 static elf_flags_to_name_table elf_flags_to_names [] =
14070 {
14071 { "SHF_WRITE", SHF_WRITE },
14072 { "SHF_ALLOC", SHF_ALLOC },
14073 { "SHF_EXECINSTR", SHF_EXECINSTR },
14074 { "SHF_MERGE", SHF_MERGE },
14075 { "SHF_STRINGS", SHF_STRINGS },
14076 { "SHF_INFO_LINK", SHF_INFO_LINK},
14077 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14078 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14079 { "SHF_GROUP", SHF_GROUP },
14080 { "SHF_TLS", SHF_TLS },
14081 { "SHF_MASKOS", SHF_MASKOS },
14082 { "SHF_EXCLUDE", SHF_EXCLUDE },
14083 };
14084
14085 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14086 bfd_boolean
14087 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14088 struct flag_info *flaginfo,
14089 asection *section)
14090 {
14091 const bfd_vma sh_flags = elf_section_flags (section);
14092
14093 if (!flaginfo->flags_initialized)
14094 {
14095 bfd *obfd = info->output_bfd;
14096 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14097 struct flag_info_list *tf = flaginfo->flag_list;
14098 int with_hex = 0;
14099 int without_hex = 0;
14100
14101 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14102 {
14103 unsigned i;
14104 flagword (*lookup) (char *);
14105
14106 lookup = bed->elf_backend_lookup_section_flags_hook;
14107 if (lookup != NULL)
14108 {
14109 flagword hexval = (*lookup) ((char *) tf->name);
14110
14111 if (hexval != 0)
14112 {
14113 if (tf->with == with_flags)
14114 with_hex |= hexval;
14115 else if (tf->with == without_flags)
14116 without_hex |= hexval;
14117 tf->valid = TRUE;
14118 continue;
14119 }
14120 }
14121 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14122 {
14123 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14124 {
14125 if (tf->with == with_flags)
14126 with_hex |= elf_flags_to_names[i].flag_value;
14127 else if (tf->with == without_flags)
14128 without_hex |= elf_flags_to_names[i].flag_value;
14129 tf->valid = TRUE;
14130 break;
14131 }
14132 }
14133 if (!tf->valid)
14134 {
14135 info->callbacks->einfo
14136 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14137 return FALSE;
14138 }
14139 }
14140 flaginfo->flags_initialized = TRUE;
14141 flaginfo->only_with_flags |= with_hex;
14142 flaginfo->not_with_flags |= without_hex;
14143 }
14144
14145 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14146 return FALSE;
14147
14148 if ((flaginfo->not_with_flags & sh_flags) != 0)
14149 return FALSE;
14150
14151 return TRUE;
14152 }
14153
14154 struct alloc_got_off_arg {
14155 bfd_vma gotoff;
14156 struct bfd_link_info *info;
14157 };
14158
14159 /* We need a special top-level link routine to convert got reference counts
14160 to real got offsets. */
14161
14162 static bfd_boolean
14163 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14164 {
14165 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14166 bfd *obfd = gofarg->info->output_bfd;
14167 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14168
14169 if (h->got.refcount > 0)
14170 {
14171 h->got.offset = gofarg->gotoff;
14172 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14173 }
14174 else
14175 h->got.offset = (bfd_vma) -1;
14176
14177 return TRUE;
14178 }
14179
14180 /* And an accompanying bit to work out final got entry offsets once
14181 we're done. Should be called from final_link. */
14182
14183 bfd_boolean
14184 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14185 struct bfd_link_info *info)
14186 {
14187 bfd *i;
14188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14189 bfd_vma gotoff;
14190 struct alloc_got_off_arg gofarg;
14191
14192 BFD_ASSERT (abfd == info->output_bfd);
14193
14194 if (! is_elf_hash_table (info->hash))
14195 return FALSE;
14196
14197 /* The GOT offset is relative to the .got section, but the GOT header is
14198 put into the .got.plt section, if the backend uses it. */
14199 if (bed->want_got_plt)
14200 gotoff = 0;
14201 else
14202 gotoff = bed->got_header_size;
14203
14204 /* Do the local .got entries first. */
14205 for (i = info->input_bfds; i; i = i->link.next)
14206 {
14207 bfd_signed_vma *local_got;
14208 size_t j, locsymcount;
14209 Elf_Internal_Shdr *symtab_hdr;
14210
14211 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14212 continue;
14213
14214 local_got = elf_local_got_refcounts (i);
14215 if (!local_got)
14216 continue;
14217
14218 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14219 if (elf_bad_symtab (i))
14220 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14221 else
14222 locsymcount = symtab_hdr->sh_info;
14223
14224 for (j = 0; j < locsymcount; ++j)
14225 {
14226 if (local_got[j] > 0)
14227 {
14228 local_got[j] = gotoff;
14229 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14230 }
14231 else
14232 local_got[j] = (bfd_vma) -1;
14233 }
14234 }
14235
14236 /* Then the global .got entries. .plt refcounts are handled by
14237 adjust_dynamic_symbol */
14238 gofarg.gotoff = gotoff;
14239 gofarg.info = info;
14240 elf_link_hash_traverse (elf_hash_table (info),
14241 elf_gc_allocate_got_offsets,
14242 &gofarg);
14243 return TRUE;
14244 }
14245
14246 /* Many folk need no more in the way of final link than this, once
14247 got entry reference counting is enabled. */
14248
14249 bfd_boolean
14250 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14251 {
14252 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14253 return FALSE;
14254
14255 /* Invoke the regular ELF backend linker to do all the work. */
14256 return bfd_elf_final_link (abfd, info);
14257 }
14258
14259 bfd_boolean
14260 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14261 {
14262 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14263
14264 if (rcookie->bad_symtab)
14265 rcookie->rel = rcookie->rels;
14266
14267 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14268 {
14269 unsigned long r_symndx;
14270
14271 if (! rcookie->bad_symtab)
14272 if (rcookie->rel->r_offset > offset)
14273 return FALSE;
14274 if (rcookie->rel->r_offset != offset)
14275 continue;
14276
14277 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14278 if (r_symndx == STN_UNDEF)
14279 return TRUE;
14280
14281 if (r_symndx >= rcookie->locsymcount
14282 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14283 {
14284 struct elf_link_hash_entry *h;
14285
14286 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14287
14288 while (h->root.type == bfd_link_hash_indirect
14289 || h->root.type == bfd_link_hash_warning)
14290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14291
14292 if ((h->root.type == bfd_link_hash_defined
14293 || h->root.type == bfd_link_hash_defweak)
14294 && (h->root.u.def.section->owner != rcookie->abfd
14295 || h->root.u.def.section->kept_section != NULL
14296 || discarded_section (h->root.u.def.section)))
14297 return TRUE;
14298 }
14299 else
14300 {
14301 /* It's not a relocation against a global symbol,
14302 but it could be a relocation against a local
14303 symbol for a discarded section. */
14304 asection *isec;
14305 Elf_Internal_Sym *isym;
14306
14307 /* Need to: get the symbol; get the section. */
14308 isym = &rcookie->locsyms[r_symndx];
14309 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14310 if (isec != NULL
14311 && (isec->kept_section != NULL
14312 || discarded_section (isec)))
14313 return TRUE;
14314 }
14315 return FALSE;
14316 }
14317 return FALSE;
14318 }
14319
14320 /* Discard unneeded references to discarded sections.
14321 Returns -1 on error, 1 if any section's size was changed, 0 if
14322 nothing changed. This function assumes that the relocations are in
14323 sorted order, which is true for all known assemblers. */
14324
14325 int
14326 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14327 {
14328 struct elf_reloc_cookie cookie;
14329 asection *o;
14330 bfd *abfd;
14331 int changed = 0;
14332
14333 if (info->traditional_format
14334 || !is_elf_hash_table (info->hash))
14335 return 0;
14336
14337 o = bfd_get_section_by_name (output_bfd, ".stab");
14338 if (o != NULL)
14339 {
14340 asection *i;
14341
14342 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14343 {
14344 if (i->size == 0
14345 || i->reloc_count == 0
14346 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14347 continue;
14348
14349 abfd = i->owner;
14350 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14351 continue;
14352
14353 if (!init_reloc_cookie_for_section (&cookie, info, i))
14354 return -1;
14355
14356 if (_bfd_discard_section_stabs (abfd, i,
14357 elf_section_data (i)->sec_info,
14358 bfd_elf_reloc_symbol_deleted_p,
14359 &cookie))
14360 changed = 1;
14361
14362 fini_reloc_cookie_for_section (&cookie, i);
14363 }
14364 }
14365
14366 o = NULL;
14367 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14368 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14369 if (o != NULL)
14370 {
14371 asection *i;
14372 int eh_changed = 0;
14373 unsigned int eh_alignment; /* Octets. */
14374
14375 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14376 {
14377 if (i->size == 0)
14378 continue;
14379
14380 abfd = i->owner;
14381 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14382 continue;
14383
14384 if (!init_reloc_cookie_for_section (&cookie, info, i))
14385 return -1;
14386
14387 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14388 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14389 bfd_elf_reloc_symbol_deleted_p,
14390 &cookie))
14391 {
14392 eh_changed = 1;
14393 if (i->size != i->rawsize)
14394 changed = 1;
14395 }
14396
14397 fini_reloc_cookie_for_section (&cookie, i);
14398 }
14399
14400 eh_alignment = ((1 << o->alignment_power)
14401 * bfd_octets_per_byte (output_bfd, o));
14402 /* Skip over zero terminator, and prevent empty sections from
14403 adding alignment padding at the end. */
14404 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14405 if (i->size == 0)
14406 i->flags |= SEC_EXCLUDE;
14407 else if (i->size > 4)
14408 break;
14409 /* The last non-empty eh_frame section doesn't need padding. */
14410 if (i != NULL)
14411 i = i->map_tail.s;
14412 /* Any prior sections must pad the last FDE out to the output
14413 section alignment. Otherwise we might have zero padding
14414 between sections, which would be seen as a terminator. */
14415 for (; i != NULL; i = i->map_tail.s)
14416 if (i->size == 4)
14417 /* All but the last zero terminator should have been removed. */
14418 BFD_FAIL ();
14419 else
14420 {
14421 bfd_size_type size
14422 = (i->size + eh_alignment - 1) & -eh_alignment;
14423 if (i->size != size)
14424 {
14425 i->size = size;
14426 changed = 1;
14427 eh_changed = 1;
14428 }
14429 }
14430 if (eh_changed)
14431 elf_link_hash_traverse (elf_hash_table (info),
14432 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14433 }
14434
14435 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14436 {
14437 const struct elf_backend_data *bed;
14438 asection *s;
14439
14440 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14441 continue;
14442 s = abfd->sections;
14443 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14444 continue;
14445
14446 bed = get_elf_backend_data (abfd);
14447
14448 if (bed->elf_backend_discard_info != NULL)
14449 {
14450 if (!init_reloc_cookie (&cookie, info, abfd))
14451 return -1;
14452
14453 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14454 changed = 1;
14455
14456 fini_reloc_cookie (&cookie, abfd);
14457 }
14458 }
14459
14460 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14461 _bfd_elf_end_eh_frame_parsing (info);
14462
14463 if (info->eh_frame_hdr_type
14464 && !bfd_link_relocatable (info)
14465 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14466 changed = 1;
14467
14468 return changed;
14469 }
14470
14471 bfd_boolean
14472 _bfd_elf_section_already_linked (bfd *abfd,
14473 asection *sec,
14474 struct bfd_link_info *info)
14475 {
14476 flagword flags;
14477 const char *name, *key;
14478 struct bfd_section_already_linked *l;
14479 struct bfd_section_already_linked_hash_entry *already_linked_list;
14480
14481 if (sec->output_section == bfd_abs_section_ptr)
14482 return FALSE;
14483
14484 flags = sec->flags;
14485
14486 /* Return if it isn't a linkonce section. A comdat group section
14487 also has SEC_LINK_ONCE set. */
14488 if ((flags & SEC_LINK_ONCE) == 0)
14489 return FALSE;
14490
14491 /* Don't put group member sections on our list of already linked
14492 sections. They are handled as a group via their group section. */
14493 if (elf_sec_group (sec) != NULL)
14494 return FALSE;
14495
14496 /* For a SHT_GROUP section, use the group signature as the key. */
14497 name = sec->name;
14498 if ((flags & SEC_GROUP) != 0
14499 && elf_next_in_group (sec) != NULL
14500 && elf_group_name (elf_next_in_group (sec)) != NULL)
14501 key = elf_group_name (elf_next_in_group (sec));
14502 else
14503 {
14504 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14505 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14506 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14507 key++;
14508 else
14509 /* Must be a user linkonce section that doesn't follow gcc's
14510 naming convention. In this case we won't be matching
14511 single member groups. */
14512 key = name;
14513 }
14514
14515 already_linked_list = bfd_section_already_linked_table_lookup (key);
14516
14517 for (l = already_linked_list->entry; l != NULL; l = l->next)
14518 {
14519 /* We may have 2 different types of sections on the list: group
14520 sections with a signature of <key> (<key> is some string),
14521 and linkonce sections named .gnu.linkonce.<type>.<key>.
14522 Match like sections. LTO plugin sections are an exception.
14523 They are always named .gnu.linkonce.t.<key> and match either
14524 type of section. */
14525 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14526 && ((flags & SEC_GROUP) != 0
14527 || strcmp (name, l->sec->name) == 0))
14528 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14529 {
14530 /* The section has already been linked. See if we should
14531 issue a warning. */
14532 if (!_bfd_handle_already_linked (sec, l, info))
14533 return FALSE;
14534
14535 if (flags & SEC_GROUP)
14536 {
14537 asection *first = elf_next_in_group (sec);
14538 asection *s = first;
14539
14540 while (s != NULL)
14541 {
14542 s->output_section = bfd_abs_section_ptr;
14543 /* Record which group discards it. */
14544 s->kept_section = l->sec;
14545 s = elf_next_in_group (s);
14546 /* These lists are circular. */
14547 if (s == first)
14548 break;
14549 }
14550 }
14551
14552 return TRUE;
14553 }
14554 }
14555
14556 /* A single member comdat group section may be discarded by a
14557 linkonce section and vice versa. */
14558 if ((flags & SEC_GROUP) != 0)
14559 {
14560 asection *first = elf_next_in_group (sec);
14561
14562 if (first != NULL && elf_next_in_group (first) == first)
14563 /* Check this single member group against linkonce sections. */
14564 for (l = already_linked_list->entry; l != NULL; l = l->next)
14565 if ((l->sec->flags & SEC_GROUP) == 0
14566 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14567 {
14568 first->output_section = bfd_abs_section_ptr;
14569 first->kept_section = l->sec;
14570 sec->output_section = bfd_abs_section_ptr;
14571 break;
14572 }
14573 }
14574 else
14575 /* Check this linkonce section against single member groups. */
14576 for (l = already_linked_list->entry; l != NULL; l = l->next)
14577 if (l->sec->flags & SEC_GROUP)
14578 {
14579 asection *first = elf_next_in_group (l->sec);
14580
14581 if (first != NULL
14582 && elf_next_in_group (first) == first
14583 && bfd_elf_match_symbols_in_sections (first, sec, info))
14584 {
14585 sec->output_section = bfd_abs_section_ptr;
14586 sec->kept_section = first;
14587 break;
14588 }
14589 }
14590
14591 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14592 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14593 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14594 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14595 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14596 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14597 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14598 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14599 The reverse order cannot happen as there is never a bfd with only the
14600 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14601 matter as here were are looking only for cross-bfd sections. */
14602
14603 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14604 for (l = already_linked_list->entry; l != NULL; l = l->next)
14605 if ((l->sec->flags & SEC_GROUP) == 0
14606 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14607 {
14608 if (abfd != l->sec->owner)
14609 sec->output_section = bfd_abs_section_ptr;
14610 break;
14611 }
14612
14613 /* This is the first section with this name. Record it. */
14614 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14615 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14616 return sec->output_section == bfd_abs_section_ptr;
14617 }
14618
14619 bfd_boolean
14620 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14621 {
14622 return sym->st_shndx == SHN_COMMON;
14623 }
14624
14625 unsigned int
14626 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14627 {
14628 return SHN_COMMON;
14629 }
14630
14631 asection *
14632 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14633 {
14634 return bfd_com_section_ptr;
14635 }
14636
14637 bfd_vma
14638 _bfd_elf_default_got_elt_size (bfd *abfd,
14639 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14640 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14641 bfd *ibfd ATTRIBUTE_UNUSED,
14642 unsigned long symndx ATTRIBUTE_UNUSED)
14643 {
14644 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14645 return bed->s->arch_size / 8;
14646 }
14647
14648 /* Routines to support the creation of dynamic relocs. */
14649
14650 /* Returns the name of the dynamic reloc section associated with SEC. */
14651
14652 static const char *
14653 get_dynamic_reloc_section_name (bfd * abfd,
14654 asection * sec,
14655 bfd_boolean is_rela)
14656 {
14657 char *name;
14658 const char *old_name = bfd_section_name (sec);
14659 const char *prefix = is_rela ? ".rela" : ".rel";
14660
14661 if (old_name == NULL)
14662 return NULL;
14663
14664 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14665 sprintf (name, "%s%s", prefix, old_name);
14666
14667 return name;
14668 }
14669
14670 /* Returns the dynamic reloc section associated with SEC.
14671 If necessary compute the name of the dynamic reloc section based
14672 on SEC's name (looked up in ABFD's string table) and the setting
14673 of IS_RELA. */
14674
14675 asection *
14676 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14677 asection * sec,
14678 bfd_boolean is_rela)
14679 {
14680 asection * reloc_sec = elf_section_data (sec)->sreloc;
14681
14682 if (reloc_sec == NULL)
14683 {
14684 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14685
14686 if (name != NULL)
14687 {
14688 reloc_sec = bfd_get_linker_section (abfd, name);
14689
14690 if (reloc_sec != NULL)
14691 elf_section_data (sec)->sreloc = reloc_sec;
14692 }
14693 }
14694
14695 return reloc_sec;
14696 }
14697
14698 /* Returns the dynamic reloc section associated with SEC. If the
14699 section does not exist it is created and attached to the DYNOBJ
14700 bfd and stored in the SRELOC field of SEC's elf_section_data
14701 structure.
14702
14703 ALIGNMENT is the alignment for the newly created section and
14704 IS_RELA defines whether the name should be .rela.<SEC's name>
14705 or .rel.<SEC's name>. The section name is looked up in the
14706 string table associated with ABFD. */
14707
14708 asection *
14709 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14710 bfd *dynobj,
14711 unsigned int alignment,
14712 bfd *abfd,
14713 bfd_boolean is_rela)
14714 {
14715 asection * reloc_sec = elf_section_data (sec)->sreloc;
14716
14717 if (reloc_sec == NULL)
14718 {
14719 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14720
14721 if (name == NULL)
14722 return NULL;
14723
14724 reloc_sec = bfd_get_linker_section (dynobj, name);
14725
14726 if (reloc_sec == NULL)
14727 {
14728 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14729 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14730 if ((sec->flags & SEC_ALLOC) != 0)
14731 flags |= SEC_ALLOC | SEC_LOAD;
14732
14733 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14734 if (reloc_sec != NULL)
14735 {
14736 /* _bfd_elf_get_sec_type_attr chooses a section type by
14737 name. Override as it may be wrong, eg. for a user
14738 section named "auto" we'll get ".relauto" which is
14739 seen to be a .rela section. */
14740 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14741 if (!bfd_set_section_alignment (reloc_sec, alignment))
14742 reloc_sec = NULL;
14743 }
14744 }
14745
14746 elf_section_data (sec)->sreloc = reloc_sec;
14747 }
14748
14749 return reloc_sec;
14750 }
14751
14752 /* Copy the ELF symbol type and other attributes for a linker script
14753 assignment from HSRC to HDEST. Generally this should be treated as
14754 if we found a strong non-dynamic definition for HDEST (except that
14755 ld ignores multiple definition errors). */
14756 void
14757 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14758 struct bfd_link_hash_entry *hdest,
14759 struct bfd_link_hash_entry *hsrc)
14760 {
14761 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14762 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14763 Elf_Internal_Sym isym;
14764
14765 ehdest->type = ehsrc->type;
14766 ehdest->target_internal = ehsrc->target_internal;
14767
14768 isym.st_other = ehsrc->other;
14769 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14770 }
14771
14772 /* Append a RELA relocation REL to section S in BFD. */
14773
14774 void
14775 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14776 {
14777 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14778 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14779 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14780 bed->s->swap_reloca_out (abfd, rel, loc);
14781 }
14782
14783 /* Append a REL relocation REL to section S in BFD. */
14784
14785 void
14786 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14787 {
14788 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14789 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14790 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14791 bed->s->swap_reloc_out (abfd, rel, loc);
14792 }
14793
14794 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14795
14796 struct bfd_link_hash_entry *
14797 bfd_elf_define_start_stop (struct bfd_link_info *info,
14798 const char *symbol, asection *sec)
14799 {
14800 struct elf_link_hash_entry *h;
14801
14802 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14803 FALSE, FALSE, TRUE);
14804 if (h != NULL
14805 && (h->root.type == bfd_link_hash_undefined
14806 || h->root.type == bfd_link_hash_undefweak
14807 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14808 {
14809 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14810 h->root.type = bfd_link_hash_defined;
14811 h->root.u.def.section = sec;
14812 h->root.u.def.value = 0;
14813 h->def_regular = 1;
14814 h->def_dynamic = 0;
14815 h->start_stop = 1;
14816 h->u2.start_stop_section = sec;
14817 if (symbol[0] == '.')
14818 {
14819 /* .startof. and .sizeof. symbols are local. */
14820 const struct elf_backend_data *bed;
14821 bed = get_elf_backend_data (info->output_bfd);
14822 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14823 }
14824 else
14825 {
14826 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14827 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14828 if (was_dynamic)
14829 bfd_elf_link_record_dynamic_symbol (info, h);
14830 }
14831 return &h->root;
14832 }
14833 return NULL;
14834 }
14835
14836 /* Find dynamic relocs for H that apply to read-only sections. */
14837
14838 asection *
14839 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
14840 {
14841 struct elf_dyn_relocs *p;
14842
14843 for (p = h->dyn_relocs; p != NULL; p = p->next)
14844 {
14845 asection *s = p->sec->output_section;
14846
14847 if (s != NULL && (s->flags & SEC_READONLY) != 0)
14848 return p->sec;
14849 }
14850 return NULL;
14851 }
14852
14853 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
14854 read-only sections. */
14855
14856 bfd_boolean
14857 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
14858 {
14859 asection *sec;
14860
14861 if (h->root.type == bfd_link_hash_indirect)
14862 return TRUE;
14863
14864 sec = _bfd_elf_readonly_dynrelocs (h);
14865 if (sec != NULL)
14866 {
14867 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14868
14869 info->flags |= DF_TEXTREL;
14870 /* xgettext:c-format */
14871 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
14872 "in read-only section `%pA'\n"),
14873 sec->owner, h->root.root.string, sec);
14874
14875 if (bfd_link_textrel_check (info))
14876 /* xgettext:c-format */
14877 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
14878 "in read-only section `%pA'\n"),
14879 sec->owner, h->root.root.string, sec);
14880
14881 /* Not an error, just cut short the traversal. */
14882 return FALSE;
14883 }
14884 return TRUE;
14885 }