Indent labels
[deliverable/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 if (alloc1 != NULL)
2647 free (alloc1);
2648
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2651
2652 return internal_relocs;
2653
2654 error_return:
2655 if (alloc1 != NULL)
2656 free (alloc1);
2657 if (alloc2 != NULL)
2658 {
2659 if (keep_memory)
2660 bfd_release (abfd, alloc2);
2661 else
2662 free (alloc2);
2663 }
2664 return NULL;
2665 }
2666
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2669
2670 static bfd_boolean
2671 _bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
2673 {
2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2675
2676 /* That allows us to calculate the size of the section. */
2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2678
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685 return FALSE;
2686
2687 if (reldata->hashes == NULL && reldata->count)
2688 {
2689 struct elf_link_hash_entry **p;
2690
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
2693 if (p == NULL)
2694 return FALSE;
2695
2696 reldata->hashes = p;
2697 }
2698
2699 return TRUE;
2700 }
2701
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2704 OUTPUT_BFD. */
2705
2706 bfd_boolean
2707 _bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2712 ATTRIBUTE_UNUSED)
2713 {
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2716 bfd_byte *erel;
2717 struct bfd_elf_section_reloc_data *output_reldata;
2718 asection *output_section;
2719 const struct elf_backend_data *bed;
2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2721 struct bfd_elf_section_data *esdo;
2722
2723 output_section = input_section->output_section;
2724
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2728 {
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
2731 }
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734 {
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
2737 }
2738 else
2739 {
2740 _bfd_error_handler
2741 /* xgettext:c-format */
2742 (_("%pB: relocation size mismatch in %pB section %pA"),
2743 output_bfd, input_section->owner, input_section);
2744 bfd_set_error (bfd_error_wrong_format);
2745 return FALSE;
2746 }
2747
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2754 {
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2758 }
2759
2760 /* Bump the counter, so that we know where to add the next set of
2761 relocations. */
2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2763
2764 return TRUE;
2765 }
2766 \f
2767 /* Make weak undefined symbols in PIE dynamic. */
2768
2769 bfd_boolean
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2772 {
2773 if (bfd_link_pie (info)
2774 && h->dynindx == -1
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2777
2778 return TRUE;
2779 }
2780
2781 /* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2786
2787 static bfd_boolean
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
2790 {
2791 const struct elf_backend_data *bed;
2792
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
2797 if (h->non_elf)
2798 {
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
2804 {
2805 h->ref_regular = 1;
2806 h->ref_regular_nonweak = 1;
2807 }
2808 else
2809 {
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
2813 {
2814 h->ref_regular = 1;
2815 h->ref_regular_nonweak = 1;
2816 }
2817 else
2818 h->def_regular = 1;
2819 }
2820
2821 if (h->dynindx == -1
2822 && (h->def_dynamic
2823 || h->ref_dynamic))
2824 {
2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832 else
2833 {
2834 /* Unfortunately, NON_ELF is only correct if the symbol
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
2842 && !h->def_regular
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
2847 && !h->def_dynamic)))
2848 h->def_regular = 1;
2849 }
2850
2851 /* Backend specific symbol fixup. */
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855 return FALSE;
2856
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
2860 the symbol in a common section but the DEF_REGULAR
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
2863 && !h->def_regular
2864 && h->ref_regular
2865 && !h->def_dynamic
2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2867 h->def_regular = 1;
2868
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2881 exported. */
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2885 && !h->dynamic
2886 && !h->ref_dynamic
2887 && h->def_regular)
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
2895 will force it local. */
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 && h->def_regular)
2902 {
2903 bfd_boolean force_local;
2904
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908 }
2909
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
2913 if (h->is_weakalias)
2914 {
2915 struct elf_link_hash_entry *def = weakdef (h);
2916
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
2929 {
2930 h = def;
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2933 }
2934 else
2935 {
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
2940 BFD_ASSERT (def->def_dynamic);
2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2942 }
2943 }
2944
2945 return TRUE;
2946 }
2947
2948 /* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2950 recursively. */
2951
2952 static bfd_boolean
2953 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2954 {
2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2956 struct elf_link_hash_table *htab;
2957 const struct elf_backend_data *bed;
2958
2959 if (! is_elf_hash_table (eif->info->hash))
2960 return FALSE;
2961
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2964 return TRUE;
2965
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 return FALSE;
2969
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2972
2973 if (h->root.type == bfd_link_hash_undefweak)
2974 {
2975 if (eif->info->dynamic_undefined_weak == 0)
2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2977 else if (eif->info->dynamic_undefined_weak > 0
2978 && h->ref_regular
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2982 {
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2984 {
2985 eif->failed = TRUE;
2986 return FALSE;
2987 }
2988 }
2989 }
2990
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
2998 if (!h->needs_plt
2999 && h->type != STT_GNU_IFUNC
3000 && (h->def_regular
3001 || !h->def_dynamic
3002 || (!h->ref_regular
3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3004 {
3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3006 return TRUE;
3007 }
3008
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
3011 if (h->dynamic_adjusted)
3012 return TRUE;
3013
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
3018 h->dynamic_adjusted = 1;
3019
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3024
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3032 library model.
3033
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3038 int _timezone = 5;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3047
3048 if (h->is_weakalias)
3049 {
3050 struct elf_link_hash_entry *def = weakdef (h);
3051
3052 /* If we get to this point, there is an implicit reference to
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
3055
3056 /* Ensure that the backend adjust_dynamic_symbol function sees
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3059 return FALSE;
3060 }
3061
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3067 if (h->size == 0
3068 && h->type == STT_NOTYPE
3069 && !h->needs_plt)
3070 _bfd_error_handler
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3073
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3075 {
3076 eif->failed = TRUE;
3077 return FALSE;
3078 }
3079
3080 return TRUE;
3081 }
3082
3083 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3084 DYNBSS. */
3085
3086 bfd_boolean
3087 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
3089 asection *dynbss)
3090 {
3091 unsigned int power_of_two;
3092 bfd_vma mask;
3093 asection *sec = h->root.u.def.section;
3094
3095 /* The section alignment of the definition is the maximum alignment
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
3100 power_of_two = bfd_section_alignment (sec);
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3103 {
3104 mask >>= 1;
3105 --power_of_two;
3106 }
3107
3108 if (power_of_two > bfd_section_alignment (dynbss))
3109 {
3110 /* Adjust the section alignment if needed. */
3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
3112 return FALSE;
3113 }
3114
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3130 info->callbacks->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h->root.root.string);
3133
3134 return TRUE;
3135 }
3136
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
3140 static bfd_boolean
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3142 {
3143 asection *sec;
3144
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3149 {
3150 bfd *output_bfd = (bfd *) data;
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
3156 h->root.u.def.value);
3157 }
3158
3159 return TRUE;
3160 }
3161
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166 bfd_boolean
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
3169 bfd_boolean not_local_protected)
3170 {
3171 bfd_boolean binding_stays_local_p;
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
3185 if (h->forced_local)
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected || !bed->is_function_type (h->type))
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
3214 break;
3215 }
3216
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3219 return TRUE;
3220
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224 }
3225
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3238
3239 bfd_boolean
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
3243 {
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
3267 return FALSE;
3268
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
3296 return TRUE;
3297
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected;
3304 }
3305
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309 struct bfd_section *
3310 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311 {
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332 }
3333
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335 static bfd_boolean
3336 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338 {
3339 const struct elf_backend_data *bed;
3340
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
3346 bed = get_elf_backend_data (abfd);
3347 /* Function symbols do not count. */
3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed->common_definition (sym))
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373 }
3374
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378 static bfd_boolean
3379 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380 {
3381 Elf_Internal_Shdr * hdr;
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404 #endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462 }
3463 \f
3464 /* Add an entry to the .dynamic table. */
3465
3466 bfd_boolean
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470 {
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
3485 bed = get_elf_backend_data (hash_table->dynobj);
3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3487 BFD_ASSERT (s != NULL);
3488
3489 newsize = s->size + bed->s->sizeof_dyn;
3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3497
3498 s->size = newsize;
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502 }
3503
3504 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3505 1 if a DT_NEEDED tag already exists, and 0 on success. */
3506
3507 int
3508 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3509 {
3510 struct elf_link_hash_table *hash_table;
3511 size_t strindex;
3512 const char *soname;
3513
3514 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3515 return -1;
3516
3517 hash_table = elf_hash_table (info);
3518 soname = elf_dt_name (abfd);
3519 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3520 if (strindex == (size_t) -1)
3521 return -1;
3522
3523 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3524 {
3525 asection *sdyn;
3526 const struct elf_backend_data *bed;
3527 bfd_byte *extdyn;
3528
3529 bed = get_elf_backend_data (hash_table->dynobj);
3530 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3531 if (sdyn != NULL)
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3535 {
3536 Elf_Internal_Dyn dyn;
3537
3538 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3539 if (dyn.d_tag == DT_NEEDED
3540 && dyn.d_un.d_val == strindex)
3541 {
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3543 return 1;
3544 }
3545 }
3546 }
3547
3548 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3549 return -1;
3550
3551 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3552 return -1;
3553
3554 return 0;
3555 }
3556
3557 /* Return true if SONAME is on the needed list between NEEDED and STOP
3558 (or the end of list if STOP is NULL), and needed by a library that
3559 will be loaded. */
3560
3561 static bfd_boolean
3562 on_needed_list (const char *soname,
3563 struct bfd_link_needed_list *needed,
3564 struct bfd_link_needed_list *stop)
3565 {
3566 struct bfd_link_needed_list *look;
3567 for (look = needed; look != stop; look = look->next)
3568 if (strcmp (soname, look->name) == 0
3569 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3570 /* If needed by a library that itself is not directly
3571 needed, recursively check whether that library is
3572 indirectly needed. Since we add DT_NEEDED entries to
3573 the end of the list, library dependencies appear after
3574 the library. Therefore search prior to the current
3575 LOOK, preventing possible infinite recursion. */
3576 || on_needed_list (elf_dt_name (look->by), needed, look)))
3577 return TRUE;
3578
3579 return FALSE;
3580 }
3581
3582 /* Sort symbol by value, section, size, and type. */
3583 static int
3584 elf_sort_symbol (const void *arg1, const void *arg2)
3585 {
3586 const struct elf_link_hash_entry *h1;
3587 const struct elf_link_hash_entry *h2;
3588 bfd_signed_vma vdiff;
3589 int sdiff;
3590 const char *n1;
3591 const char *n2;
3592
3593 h1 = *(const struct elf_link_hash_entry **) arg1;
3594 h2 = *(const struct elf_link_hash_entry **) arg2;
3595 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3596 if (vdiff != 0)
3597 return vdiff > 0 ? 1 : -1;
3598
3599 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3600 if (sdiff != 0)
3601 return sdiff;
3602
3603 /* Sort so that sized symbols are selected over zero size symbols. */
3604 vdiff = h1->size - h2->size;
3605 if (vdiff != 0)
3606 return vdiff > 0 ? 1 : -1;
3607
3608 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3609 if (h1->type != h2->type)
3610 return h1->type - h2->type;
3611
3612 /* If symbols are properly sized and typed, and multiple strong
3613 aliases are not defined in a shared library by the user we
3614 shouldn't get here. Unfortunately linker script symbols like
3615 __bss_start sometimes match a user symbol defined at the start of
3616 .bss without proper size and type. We'd like to preference the
3617 user symbol over reserved system symbols. Sort on leading
3618 underscores. */
3619 n1 = h1->root.root.string;
3620 n2 = h2->root.root.string;
3621 while (*n1 == *n2)
3622 {
3623 if (*n1 == 0)
3624 break;
3625 ++n1;
3626 ++n2;
3627 }
3628 if (*n1 == '_')
3629 return -1;
3630 if (*n2 == '_')
3631 return 1;
3632
3633 /* Final sort on name selects user symbols like '_u' over reserved
3634 system symbols like '_Z' and also will avoid qsort instability. */
3635 return *n1 - *n2;
3636 }
3637
3638 /* This function is used to adjust offsets into .dynstr for
3639 dynamic symbols. This is called via elf_link_hash_traverse. */
3640
3641 static bfd_boolean
3642 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3643 {
3644 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3645
3646 if (h->dynindx != -1)
3647 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3648 return TRUE;
3649 }
3650
3651 /* Assign string offsets in .dynstr, update all structures referencing
3652 them. */
3653
3654 static bfd_boolean
3655 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3656 {
3657 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3658 struct elf_link_local_dynamic_entry *entry;
3659 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3660 bfd *dynobj = hash_table->dynobj;
3661 asection *sdyn;
3662 bfd_size_type size;
3663 const struct elf_backend_data *bed;
3664 bfd_byte *extdyn;
3665
3666 _bfd_elf_strtab_finalize (dynstr);
3667 size = _bfd_elf_strtab_size (dynstr);
3668
3669 bed = get_elf_backend_data (dynobj);
3670 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3671 BFD_ASSERT (sdyn != NULL);
3672
3673 /* Update all .dynamic entries referencing .dynstr strings. */
3674 for (extdyn = sdyn->contents;
3675 extdyn < sdyn->contents + sdyn->size;
3676 extdyn += bed->s->sizeof_dyn)
3677 {
3678 Elf_Internal_Dyn dyn;
3679
3680 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3681 switch (dyn.d_tag)
3682 {
3683 case DT_STRSZ:
3684 dyn.d_un.d_val = size;
3685 break;
3686 case DT_NEEDED:
3687 case DT_SONAME:
3688 case DT_RPATH:
3689 case DT_RUNPATH:
3690 case DT_FILTER:
3691 case DT_AUXILIARY:
3692 case DT_AUDIT:
3693 case DT_DEPAUDIT:
3694 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3695 break;
3696 default:
3697 continue;
3698 }
3699 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3700 }
3701
3702 /* Now update local dynamic symbols. */
3703 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3704 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3705 entry->isym.st_name);
3706
3707 /* And the rest of dynamic symbols. */
3708 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3709
3710 /* Adjust version definitions. */
3711 if (elf_tdata (output_bfd)->cverdefs)
3712 {
3713 asection *s;
3714 bfd_byte *p;
3715 size_t i;
3716 Elf_Internal_Verdef def;
3717 Elf_Internal_Verdaux defaux;
3718
3719 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3720 p = s->contents;
3721 do
3722 {
3723 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3724 &def);
3725 p += sizeof (Elf_External_Verdef);
3726 if (def.vd_aux != sizeof (Elf_External_Verdef))
3727 continue;
3728 for (i = 0; i < def.vd_cnt; ++i)
3729 {
3730 _bfd_elf_swap_verdaux_in (output_bfd,
3731 (Elf_External_Verdaux *) p, &defaux);
3732 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3733 defaux.vda_name);
3734 _bfd_elf_swap_verdaux_out (output_bfd,
3735 &defaux, (Elf_External_Verdaux *) p);
3736 p += sizeof (Elf_External_Verdaux);
3737 }
3738 }
3739 while (def.vd_next);
3740 }
3741
3742 /* Adjust version references. */
3743 if (elf_tdata (output_bfd)->verref)
3744 {
3745 asection *s;
3746 bfd_byte *p;
3747 size_t i;
3748 Elf_Internal_Verneed need;
3749 Elf_Internal_Vernaux needaux;
3750
3751 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3752 p = s->contents;
3753 do
3754 {
3755 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3756 &need);
3757 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3758 _bfd_elf_swap_verneed_out (output_bfd, &need,
3759 (Elf_External_Verneed *) p);
3760 p += sizeof (Elf_External_Verneed);
3761 for (i = 0; i < need.vn_cnt; ++i)
3762 {
3763 _bfd_elf_swap_vernaux_in (output_bfd,
3764 (Elf_External_Vernaux *) p, &needaux);
3765 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3766 needaux.vna_name);
3767 _bfd_elf_swap_vernaux_out (output_bfd,
3768 &needaux,
3769 (Elf_External_Vernaux *) p);
3770 p += sizeof (Elf_External_Vernaux);
3771 }
3772 }
3773 while (need.vn_next);
3774 }
3775
3776 return TRUE;
3777 }
3778 \f
3779 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3780 The default is to only match when the INPUT and OUTPUT are exactly
3781 the same target. */
3782
3783 bfd_boolean
3784 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3785 const bfd_target *output)
3786 {
3787 return input == output;
3788 }
3789
3790 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3791 This version is used when different targets for the same architecture
3792 are virtually identical. */
3793
3794 bfd_boolean
3795 _bfd_elf_relocs_compatible (const bfd_target *input,
3796 const bfd_target *output)
3797 {
3798 const struct elf_backend_data *obed, *ibed;
3799
3800 if (input == output)
3801 return TRUE;
3802
3803 ibed = xvec_get_elf_backend_data (input);
3804 obed = xvec_get_elf_backend_data (output);
3805
3806 if (ibed->arch != obed->arch)
3807 return FALSE;
3808
3809 /* If both backends are using this function, deem them compatible. */
3810 return ibed->relocs_compatible == obed->relocs_compatible;
3811 }
3812
3813 /* Make a special call to the linker "notice" function to tell it that
3814 we are about to handle an as-needed lib, or have finished
3815 processing the lib. */
3816
3817 bfd_boolean
3818 _bfd_elf_notice_as_needed (bfd *ibfd,
3819 struct bfd_link_info *info,
3820 enum notice_asneeded_action act)
3821 {
3822 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3823 }
3824
3825 /* Check relocations an ELF object file. */
3826
3827 bfd_boolean
3828 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3829 {
3830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3831 struct elf_link_hash_table *htab = elf_hash_table (info);
3832
3833 /* If this object is the same format as the output object, and it is
3834 not a shared library, then let the backend look through the
3835 relocs.
3836
3837 This is required to build global offset table entries and to
3838 arrange for dynamic relocs. It is not required for the
3839 particular common case of linking non PIC code, even when linking
3840 against shared libraries, but unfortunately there is no way of
3841 knowing whether an object file has been compiled PIC or not.
3842 Looking through the relocs is not particularly time consuming.
3843 The problem is that we must either (1) keep the relocs in memory,
3844 which causes the linker to require additional runtime memory or
3845 (2) read the relocs twice from the input file, which wastes time.
3846 This would be a good case for using mmap.
3847
3848 I have no idea how to handle linking PIC code into a file of a
3849 different format. It probably can't be done. */
3850 if ((abfd->flags & DYNAMIC) == 0
3851 && is_elf_hash_table (htab)
3852 && bed->check_relocs != NULL
3853 && elf_object_id (abfd) == elf_hash_table_id (htab)
3854 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3855 {
3856 asection *o;
3857
3858 for (o = abfd->sections; o != NULL; o = o->next)
3859 {
3860 Elf_Internal_Rela *internal_relocs;
3861 bfd_boolean ok;
3862
3863 /* Don't check relocations in excluded sections. */
3864 if ((o->flags & SEC_RELOC) == 0
3865 || (o->flags & SEC_EXCLUDE) != 0
3866 || o->reloc_count == 0
3867 || ((info->strip == strip_all || info->strip == strip_debugger)
3868 && (o->flags & SEC_DEBUGGING) != 0)
3869 || bfd_is_abs_section (o->output_section))
3870 continue;
3871
3872 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3873 info->keep_memory);
3874 if (internal_relocs == NULL)
3875 return FALSE;
3876
3877 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3878
3879 if (elf_section_data (o)->relocs != internal_relocs)
3880 free (internal_relocs);
3881
3882 if (! ok)
3883 return FALSE;
3884 }
3885 }
3886
3887 return TRUE;
3888 }
3889
3890 /* Add symbols from an ELF object file to the linker hash table. */
3891
3892 static bfd_boolean
3893 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3894 {
3895 Elf_Internal_Ehdr *ehdr;
3896 Elf_Internal_Shdr *hdr;
3897 size_t symcount;
3898 size_t extsymcount;
3899 size_t extsymoff;
3900 struct elf_link_hash_entry **sym_hash;
3901 bfd_boolean dynamic;
3902 Elf_External_Versym *extversym = NULL;
3903 Elf_External_Versym *extversym_end = NULL;
3904 Elf_External_Versym *ever;
3905 struct elf_link_hash_entry *weaks;
3906 struct elf_link_hash_entry **nondeflt_vers = NULL;
3907 size_t nondeflt_vers_cnt = 0;
3908 Elf_Internal_Sym *isymbuf = NULL;
3909 Elf_Internal_Sym *isym;
3910 Elf_Internal_Sym *isymend;
3911 const struct elf_backend_data *bed;
3912 bfd_boolean add_needed;
3913 struct elf_link_hash_table *htab;
3914 void *alloc_mark = NULL;
3915 struct bfd_hash_entry **old_table = NULL;
3916 unsigned int old_size = 0;
3917 unsigned int old_count = 0;
3918 void *old_tab = NULL;
3919 void *old_ent;
3920 struct bfd_link_hash_entry *old_undefs = NULL;
3921 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3922 void *old_strtab = NULL;
3923 size_t tabsize = 0;
3924 asection *s;
3925 bfd_boolean just_syms;
3926
3927 htab = elf_hash_table (info);
3928 bed = get_elf_backend_data (abfd);
3929
3930 if ((abfd->flags & DYNAMIC) == 0)
3931 dynamic = FALSE;
3932 else
3933 {
3934 dynamic = TRUE;
3935
3936 /* You can't use -r against a dynamic object. Also, there's no
3937 hope of using a dynamic object which does not exactly match
3938 the format of the output file. */
3939 if (bfd_link_relocatable (info)
3940 || !is_elf_hash_table (htab)
3941 || info->output_bfd->xvec != abfd->xvec)
3942 {
3943 if (bfd_link_relocatable (info))
3944 bfd_set_error (bfd_error_invalid_operation);
3945 else
3946 bfd_set_error (bfd_error_wrong_format);
3947 goto error_return;
3948 }
3949 }
3950
3951 ehdr = elf_elfheader (abfd);
3952 if (info->warn_alternate_em
3953 && bed->elf_machine_code != ehdr->e_machine
3954 && ((bed->elf_machine_alt1 != 0
3955 && ehdr->e_machine == bed->elf_machine_alt1)
3956 || (bed->elf_machine_alt2 != 0
3957 && ehdr->e_machine == bed->elf_machine_alt2)))
3958 _bfd_error_handler
3959 /* xgettext:c-format */
3960 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3961 ehdr->e_machine, abfd, bed->elf_machine_code);
3962
3963 /* As a GNU extension, any input sections which are named
3964 .gnu.warning.SYMBOL are treated as warning symbols for the given
3965 symbol. This differs from .gnu.warning sections, which generate
3966 warnings when they are included in an output file. */
3967 /* PR 12761: Also generate this warning when building shared libraries. */
3968 for (s = abfd->sections; s != NULL; s = s->next)
3969 {
3970 const char *name;
3971
3972 name = bfd_section_name (s);
3973 if (CONST_STRNEQ (name, ".gnu.warning."))
3974 {
3975 char *msg;
3976 bfd_size_type sz;
3977
3978 name += sizeof ".gnu.warning." - 1;
3979
3980 /* If this is a shared object, then look up the symbol
3981 in the hash table. If it is there, and it is already
3982 been defined, then we will not be using the entry
3983 from this shared object, so we don't need to warn.
3984 FIXME: If we see the definition in a regular object
3985 later on, we will warn, but we shouldn't. The only
3986 fix is to keep track of what warnings we are supposed
3987 to emit, and then handle them all at the end of the
3988 link. */
3989 if (dynamic)
3990 {
3991 struct elf_link_hash_entry *h;
3992
3993 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3994
3995 /* FIXME: What about bfd_link_hash_common? */
3996 if (h != NULL
3997 && (h->root.type == bfd_link_hash_defined
3998 || h->root.type == bfd_link_hash_defweak))
3999 continue;
4000 }
4001
4002 sz = s->size;
4003 msg = (char *) bfd_alloc (abfd, sz + 1);
4004 if (msg == NULL)
4005 goto error_return;
4006
4007 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4008 goto error_return;
4009
4010 msg[sz] = '\0';
4011
4012 if (! (_bfd_generic_link_add_one_symbol
4013 (info, abfd, name, BSF_WARNING, s, 0, msg,
4014 FALSE, bed->collect, NULL)))
4015 goto error_return;
4016
4017 if (bfd_link_executable (info))
4018 {
4019 /* Clobber the section size so that the warning does
4020 not get copied into the output file. */
4021 s->size = 0;
4022
4023 /* Also set SEC_EXCLUDE, so that symbols defined in
4024 the warning section don't get copied to the output. */
4025 s->flags |= SEC_EXCLUDE;
4026 }
4027 }
4028 }
4029
4030 just_syms = ((s = abfd->sections) != NULL
4031 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4032
4033 add_needed = TRUE;
4034 if (! dynamic)
4035 {
4036 /* If we are creating a shared library, create all the dynamic
4037 sections immediately. We need to attach them to something,
4038 so we attach them to this BFD, provided it is the right
4039 format and is not from ld --just-symbols. Always create the
4040 dynamic sections for -E/--dynamic-list. FIXME: If there
4041 are no input BFD's of the same format as the output, we can't
4042 make a shared library. */
4043 if (!just_syms
4044 && (bfd_link_pic (info)
4045 || (!bfd_link_relocatable (info)
4046 && info->nointerp
4047 && (info->export_dynamic || info->dynamic)))
4048 && is_elf_hash_table (htab)
4049 && info->output_bfd->xvec == abfd->xvec
4050 && !htab->dynamic_sections_created)
4051 {
4052 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4053 goto error_return;
4054 }
4055 }
4056 else if (!is_elf_hash_table (htab))
4057 goto error_return;
4058 else
4059 {
4060 const char *soname = NULL;
4061 char *audit = NULL;
4062 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4063 const Elf_Internal_Phdr *phdr;
4064 struct elf_link_loaded_list *loaded_lib;
4065
4066 /* ld --just-symbols and dynamic objects don't mix very well.
4067 ld shouldn't allow it. */
4068 if (just_syms)
4069 abort ();
4070
4071 /* If this dynamic lib was specified on the command line with
4072 --as-needed in effect, then we don't want to add a DT_NEEDED
4073 tag unless the lib is actually used. Similary for libs brought
4074 in by another lib's DT_NEEDED. When --no-add-needed is used
4075 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4076 any dynamic library in DT_NEEDED tags in the dynamic lib at
4077 all. */
4078 add_needed = (elf_dyn_lib_class (abfd)
4079 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4080 | DYN_NO_NEEDED)) == 0;
4081
4082 s = bfd_get_section_by_name (abfd, ".dynamic");
4083 if (s != NULL)
4084 {
4085 bfd_byte *dynbuf;
4086 bfd_byte *extdyn;
4087 unsigned int elfsec;
4088 unsigned long shlink;
4089
4090 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4091 {
4092 error_free_dyn:
4093 free (dynbuf);
4094 goto error_return;
4095 }
4096
4097 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4098 if (elfsec == SHN_BAD)
4099 goto error_free_dyn;
4100 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4101
4102 for (extdyn = dynbuf;
4103 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4104 extdyn += bed->s->sizeof_dyn)
4105 {
4106 Elf_Internal_Dyn dyn;
4107
4108 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4109 if (dyn.d_tag == DT_SONAME)
4110 {
4111 unsigned int tagv = dyn.d_un.d_val;
4112 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4113 if (soname == NULL)
4114 goto error_free_dyn;
4115 }
4116 if (dyn.d_tag == DT_NEEDED)
4117 {
4118 struct bfd_link_needed_list *n, **pn;
4119 char *fnm, *anm;
4120 unsigned int tagv = dyn.d_un.d_val;
4121 size_t amt = sizeof (struct bfd_link_needed_list);
4122
4123 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4124 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4125 if (n == NULL || fnm == NULL)
4126 goto error_free_dyn;
4127 amt = strlen (fnm) + 1;
4128 anm = (char *) bfd_alloc (abfd, amt);
4129 if (anm == NULL)
4130 goto error_free_dyn;
4131 memcpy (anm, fnm, amt);
4132 n->name = anm;
4133 n->by = abfd;
4134 n->next = NULL;
4135 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4136 ;
4137 *pn = n;
4138 }
4139 if (dyn.d_tag == DT_RUNPATH)
4140 {
4141 struct bfd_link_needed_list *n, **pn;
4142 char *fnm, *anm;
4143 unsigned int tagv = dyn.d_un.d_val;
4144 size_t amt = sizeof (struct bfd_link_needed_list);
4145
4146 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4147 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4148 if (n == NULL || fnm == NULL)
4149 goto error_free_dyn;
4150 amt = strlen (fnm) + 1;
4151 anm = (char *) bfd_alloc (abfd, amt);
4152 if (anm == NULL)
4153 goto error_free_dyn;
4154 memcpy (anm, fnm, amt);
4155 n->name = anm;
4156 n->by = abfd;
4157 n->next = NULL;
4158 for (pn = & runpath;
4159 *pn != NULL;
4160 pn = &(*pn)->next)
4161 ;
4162 *pn = n;
4163 }
4164 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4165 if (!runpath && dyn.d_tag == DT_RPATH)
4166 {
4167 struct bfd_link_needed_list *n, **pn;
4168 char *fnm, *anm;
4169 unsigned int tagv = dyn.d_un.d_val;
4170 size_t amt = sizeof (struct bfd_link_needed_list);
4171
4172 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4173 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4174 if (n == NULL || fnm == NULL)
4175 goto error_free_dyn;
4176 amt = strlen (fnm) + 1;
4177 anm = (char *) bfd_alloc (abfd, amt);
4178 if (anm == NULL)
4179 goto error_free_dyn;
4180 memcpy (anm, fnm, amt);
4181 n->name = anm;
4182 n->by = abfd;
4183 n->next = NULL;
4184 for (pn = & rpath;
4185 *pn != NULL;
4186 pn = &(*pn)->next)
4187 ;
4188 *pn = n;
4189 }
4190 if (dyn.d_tag == DT_AUDIT)
4191 {
4192 unsigned int tagv = dyn.d_un.d_val;
4193 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4194 }
4195 }
4196
4197 free (dynbuf);
4198 }
4199
4200 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4201 frees all more recently bfd_alloc'd blocks as well. */
4202 if (runpath)
4203 rpath = runpath;
4204
4205 if (rpath)
4206 {
4207 struct bfd_link_needed_list **pn;
4208 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4209 ;
4210 *pn = rpath;
4211 }
4212
4213 /* If we have a PT_GNU_RELRO program header, mark as read-only
4214 all sections contained fully therein. This makes relro
4215 shared library sections appear as they will at run-time. */
4216 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4217 while (phdr-- > elf_tdata (abfd)->phdr)
4218 if (phdr->p_type == PT_GNU_RELRO)
4219 {
4220 for (s = abfd->sections; s != NULL; s = s->next)
4221 if ((s->flags & SEC_ALLOC) != 0
4222 && s->vma >= phdr->p_vaddr
4223 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4224 s->flags |= SEC_READONLY;
4225 break;
4226 }
4227
4228 /* We do not want to include any of the sections in a dynamic
4229 object in the output file. We hack by simply clobbering the
4230 list of sections in the BFD. This could be handled more
4231 cleanly by, say, a new section flag; the existing
4232 SEC_NEVER_LOAD flag is not the one we want, because that one
4233 still implies that the section takes up space in the output
4234 file. */
4235 bfd_section_list_clear (abfd);
4236
4237 /* Find the name to use in a DT_NEEDED entry that refers to this
4238 object. If the object has a DT_SONAME entry, we use it.
4239 Otherwise, if the generic linker stuck something in
4240 elf_dt_name, we use that. Otherwise, we just use the file
4241 name. */
4242 if (soname == NULL || *soname == '\0')
4243 {
4244 soname = elf_dt_name (abfd);
4245 if (soname == NULL || *soname == '\0')
4246 soname = bfd_get_filename (abfd);
4247 }
4248
4249 /* Save the SONAME because sometimes the linker emulation code
4250 will need to know it. */
4251 elf_dt_name (abfd) = soname;
4252
4253 /* If we have already included this dynamic object in the
4254 link, just ignore it. There is no reason to include a
4255 particular dynamic object more than once. */
4256 for (loaded_lib = htab->dyn_loaded;
4257 loaded_lib != NULL;
4258 loaded_lib = loaded_lib->next)
4259 {
4260 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4261 return TRUE;
4262 }
4263
4264 /* Create dynamic sections for backends that require that be done
4265 before setup_gnu_properties. */
4266 if (add_needed
4267 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4268 return FALSE;
4269
4270 /* Save the DT_AUDIT entry for the linker emulation code. */
4271 elf_dt_audit (abfd) = audit;
4272 }
4273
4274 /* If this is a dynamic object, we always link against the .dynsym
4275 symbol table, not the .symtab symbol table. The dynamic linker
4276 will only see the .dynsym symbol table, so there is no reason to
4277 look at .symtab for a dynamic object. */
4278
4279 if (! dynamic || elf_dynsymtab (abfd) == 0)
4280 hdr = &elf_tdata (abfd)->symtab_hdr;
4281 else
4282 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4283
4284 symcount = hdr->sh_size / bed->s->sizeof_sym;
4285
4286 /* The sh_info field of the symtab header tells us where the
4287 external symbols start. We don't care about the local symbols at
4288 this point. */
4289 if (elf_bad_symtab (abfd))
4290 {
4291 extsymcount = symcount;
4292 extsymoff = 0;
4293 }
4294 else
4295 {
4296 extsymcount = symcount - hdr->sh_info;
4297 extsymoff = hdr->sh_info;
4298 }
4299
4300 sym_hash = elf_sym_hashes (abfd);
4301 if (extsymcount != 0)
4302 {
4303 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4304 NULL, NULL, NULL);
4305 if (isymbuf == NULL)
4306 goto error_return;
4307
4308 if (sym_hash == NULL)
4309 {
4310 /* We store a pointer to the hash table entry for each
4311 external symbol. */
4312 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4313 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4314 if (sym_hash == NULL)
4315 goto error_free_sym;
4316 elf_sym_hashes (abfd) = sym_hash;
4317 }
4318 }
4319
4320 if (dynamic)
4321 {
4322 /* Read in any version definitions. */
4323 if (!_bfd_elf_slurp_version_tables (abfd,
4324 info->default_imported_symver))
4325 goto error_free_sym;
4326
4327 /* Read in the symbol versions, but don't bother to convert them
4328 to internal format. */
4329 if (elf_dynversym (abfd) != 0)
4330 {
4331 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4332 bfd_size_type amt = versymhdr->sh_size;
4333
4334 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4335 goto error_free_sym;
4336 extversym = (Elf_External_Versym *)
4337 _bfd_malloc_and_read (abfd, amt, amt);
4338 if (extversym == NULL)
4339 goto error_free_sym;
4340 extversym_end = extversym + amt / sizeof (*extversym);
4341 }
4342 }
4343
4344 /* If we are loading an as-needed shared lib, save the symbol table
4345 state before we start adding symbols. If the lib turns out
4346 to be unneeded, restore the state. */
4347 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4348 {
4349 unsigned int i;
4350 size_t entsize;
4351
4352 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4353 {
4354 struct bfd_hash_entry *p;
4355 struct elf_link_hash_entry *h;
4356
4357 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4358 {
4359 h = (struct elf_link_hash_entry *) p;
4360 entsize += htab->root.table.entsize;
4361 if (h->root.type == bfd_link_hash_warning)
4362 entsize += htab->root.table.entsize;
4363 }
4364 }
4365
4366 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4367 old_tab = bfd_malloc (tabsize + entsize);
4368 if (old_tab == NULL)
4369 goto error_free_vers;
4370
4371 /* Remember the current objalloc pointer, so that all mem for
4372 symbols added can later be reclaimed. */
4373 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4374 if (alloc_mark == NULL)
4375 goto error_free_vers;
4376
4377 /* Make a special call to the linker "notice" function to
4378 tell it that we are about to handle an as-needed lib. */
4379 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4380 goto error_free_vers;
4381
4382 /* Clone the symbol table. Remember some pointers into the
4383 symbol table, and dynamic symbol count. */
4384 old_ent = (char *) old_tab + tabsize;
4385 memcpy (old_tab, htab->root.table.table, tabsize);
4386 old_undefs = htab->root.undefs;
4387 old_undefs_tail = htab->root.undefs_tail;
4388 old_table = htab->root.table.table;
4389 old_size = htab->root.table.size;
4390 old_count = htab->root.table.count;
4391 old_strtab = NULL;
4392 if (htab->dynstr != NULL)
4393 {
4394 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4395 if (old_strtab == NULL)
4396 goto error_free_vers;
4397 }
4398
4399 for (i = 0; i < htab->root.table.size; i++)
4400 {
4401 struct bfd_hash_entry *p;
4402 struct elf_link_hash_entry *h;
4403
4404 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4405 {
4406 memcpy (old_ent, p, htab->root.table.entsize);
4407 old_ent = (char *) old_ent + htab->root.table.entsize;
4408 h = (struct elf_link_hash_entry *) p;
4409 if (h->root.type == bfd_link_hash_warning)
4410 {
4411 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4412 old_ent = (char *) old_ent + htab->root.table.entsize;
4413 }
4414 }
4415 }
4416 }
4417
4418 weaks = NULL;
4419 if (extversym == NULL)
4420 ever = NULL;
4421 else if (extversym + extsymoff < extversym_end)
4422 ever = extversym + extsymoff;
4423 else
4424 {
4425 /* xgettext:c-format */
4426 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4427 abfd, (long) extsymoff,
4428 (long) (extversym_end - extversym) / sizeof (* extversym));
4429 bfd_set_error (bfd_error_bad_value);
4430 goto error_free_vers;
4431 }
4432
4433 if (!bfd_link_relocatable (info)
4434 && abfd->lto_slim_object)
4435 {
4436 _bfd_error_handler
4437 (_("%pB: plugin needed to handle lto object"), abfd);
4438 }
4439
4440 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4441 isym < isymend;
4442 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4443 {
4444 int bind;
4445 bfd_vma value;
4446 asection *sec, *new_sec;
4447 flagword flags;
4448 const char *name;
4449 struct elf_link_hash_entry *h;
4450 struct elf_link_hash_entry *hi;
4451 bfd_boolean definition;
4452 bfd_boolean size_change_ok;
4453 bfd_boolean type_change_ok;
4454 bfd_boolean new_weak;
4455 bfd_boolean old_weak;
4456 bfd_boolean override;
4457 bfd_boolean common;
4458 bfd_boolean discarded;
4459 unsigned int old_alignment;
4460 unsigned int shindex;
4461 bfd *old_bfd;
4462 bfd_boolean matched;
4463
4464 override = FALSE;
4465
4466 flags = BSF_NO_FLAGS;
4467 sec = NULL;
4468 value = isym->st_value;
4469 common = bed->common_definition (isym);
4470 if (common && info->inhibit_common_definition)
4471 {
4472 /* Treat common symbol as undefined for --no-define-common. */
4473 isym->st_shndx = SHN_UNDEF;
4474 common = FALSE;
4475 }
4476 discarded = FALSE;
4477
4478 bind = ELF_ST_BIND (isym->st_info);
4479 switch (bind)
4480 {
4481 case STB_LOCAL:
4482 /* This should be impossible, since ELF requires that all
4483 global symbols follow all local symbols, and that sh_info
4484 point to the first global symbol. Unfortunately, Irix 5
4485 screws this up. */
4486 if (elf_bad_symtab (abfd))
4487 continue;
4488
4489 /* If we aren't prepared to handle locals within the globals
4490 then we'll likely segfault on a NULL symbol hash if the
4491 symbol is ever referenced in relocations. */
4492 shindex = elf_elfheader (abfd)->e_shstrndx;
4493 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4494 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4495 " (>= sh_info of %lu)"),
4496 abfd, name, (long) (isym - isymbuf + extsymoff),
4497 (long) extsymoff);
4498
4499 /* Dynamic object relocations are not processed by ld, so
4500 ld won't run into the problem mentioned above. */
4501 if (dynamic)
4502 continue;
4503 bfd_set_error (bfd_error_bad_value);
4504 goto error_free_vers;
4505
4506 case STB_GLOBAL:
4507 if (isym->st_shndx != SHN_UNDEF && !common)
4508 flags = BSF_GLOBAL;
4509 break;
4510
4511 case STB_WEAK:
4512 flags = BSF_WEAK;
4513 break;
4514
4515 case STB_GNU_UNIQUE:
4516 flags = BSF_GNU_UNIQUE;
4517 break;
4518
4519 default:
4520 /* Leave it up to the processor backend. */
4521 break;
4522 }
4523
4524 if (isym->st_shndx == SHN_UNDEF)
4525 sec = bfd_und_section_ptr;
4526 else if (isym->st_shndx == SHN_ABS)
4527 sec = bfd_abs_section_ptr;
4528 else if (isym->st_shndx == SHN_COMMON)
4529 {
4530 sec = bfd_com_section_ptr;
4531 /* What ELF calls the size we call the value. What ELF
4532 calls the value we call the alignment. */
4533 value = isym->st_size;
4534 }
4535 else
4536 {
4537 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4538 if (sec == NULL)
4539 sec = bfd_abs_section_ptr;
4540 else if (discarded_section (sec))
4541 {
4542 /* Symbols from discarded section are undefined. We keep
4543 its visibility. */
4544 sec = bfd_und_section_ptr;
4545 discarded = TRUE;
4546 isym->st_shndx = SHN_UNDEF;
4547 }
4548 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4549 value -= sec->vma;
4550 }
4551
4552 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4553 isym->st_name);
4554 if (name == NULL)
4555 goto error_free_vers;
4556
4557 if (isym->st_shndx == SHN_COMMON
4558 && (abfd->flags & BFD_PLUGIN) != 0)
4559 {
4560 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4561
4562 if (xc == NULL)
4563 {
4564 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4565 | SEC_EXCLUDE);
4566 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4567 if (xc == NULL)
4568 goto error_free_vers;
4569 }
4570 sec = xc;
4571 }
4572 else if (isym->st_shndx == SHN_COMMON
4573 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4574 && !bfd_link_relocatable (info))
4575 {
4576 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4577
4578 if (tcomm == NULL)
4579 {
4580 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4581 | SEC_LINKER_CREATED);
4582 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4583 if (tcomm == NULL)
4584 goto error_free_vers;
4585 }
4586 sec = tcomm;
4587 }
4588 else if (bed->elf_add_symbol_hook)
4589 {
4590 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4591 &sec, &value))
4592 goto error_free_vers;
4593
4594 /* The hook function sets the name to NULL if this symbol
4595 should be skipped for some reason. */
4596 if (name == NULL)
4597 continue;
4598 }
4599
4600 /* Sanity check that all possibilities were handled. */
4601 if (sec == NULL)
4602 abort ();
4603
4604 /* Silently discard TLS symbols from --just-syms. There's
4605 no way to combine a static TLS block with a new TLS block
4606 for this executable. */
4607 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4608 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4609 continue;
4610
4611 if (bfd_is_und_section (sec)
4612 || bfd_is_com_section (sec))
4613 definition = FALSE;
4614 else
4615 definition = TRUE;
4616
4617 size_change_ok = FALSE;
4618 type_change_ok = bed->type_change_ok;
4619 old_weak = FALSE;
4620 matched = FALSE;
4621 old_alignment = 0;
4622 old_bfd = NULL;
4623 new_sec = sec;
4624
4625 if (is_elf_hash_table (htab))
4626 {
4627 Elf_Internal_Versym iver;
4628 unsigned int vernum = 0;
4629 bfd_boolean skip;
4630
4631 if (ever == NULL)
4632 {
4633 if (info->default_imported_symver)
4634 /* Use the default symbol version created earlier. */
4635 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4636 else
4637 iver.vs_vers = 0;
4638 }
4639 else if (ever >= extversym_end)
4640 {
4641 /* xgettext:c-format */
4642 _bfd_error_handler (_("%pB: not enough version information"),
4643 abfd);
4644 bfd_set_error (bfd_error_bad_value);
4645 goto error_free_vers;
4646 }
4647 else
4648 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4649
4650 vernum = iver.vs_vers & VERSYM_VERSION;
4651
4652 /* If this is a hidden symbol, or if it is not version
4653 1, we append the version name to the symbol name.
4654 However, we do not modify a non-hidden absolute symbol
4655 if it is not a function, because it might be the version
4656 symbol itself. FIXME: What if it isn't? */
4657 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4658 || (vernum > 1
4659 && (!bfd_is_abs_section (sec)
4660 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4661 {
4662 const char *verstr;
4663 size_t namelen, verlen, newlen;
4664 char *newname, *p;
4665
4666 if (isym->st_shndx != SHN_UNDEF)
4667 {
4668 if (vernum > elf_tdata (abfd)->cverdefs)
4669 verstr = NULL;
4670 else if (vernum > 1)
4671 verstr =
4672 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4673 else
4674 verstr = "";
4675
4676 if (verstr == NULL)
4677 {
4678 _bfd_error_handler
4679 /* xgettext:c-format */
4680 (_("%pB: %s: invalid version %u (max %d)"),
4681 abfd, name, vernum,
4682 elf_tdata (abfd)->cverdefs);
4683 bfd_set_error (bfd_error_bad_value);
4684 goto error_free_vers;
4685 }
4686 }
4687 else
4688 {
4689 /* We cannot simply test for the number of
4690 entries in the VERNEED section since the
4691 numbers for the needed versions do not start
4692 at 0. */
4693 Elf_Internal_Verneed *t;
4694
4695 verstr = NULL;
4696 for (t = elf_tdata (abfd)->verref;
4697 t != NULL;
4698 t = t->vn_nextref)
4699 {
4700 Elf_Internal_Vernaux *a;
4701
4702 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4703 {
4704 if (a->vna_other == vernum)
4705 {
4706 verstr = a->vna_nodename;
4707 break;
4708 }
4709 }
4710 if (a != NULL)
4711 break;
4712 }
4713 if (verstr == NULL)
4714 {
4715 _bfd_error_handler
4716 /* xgettext:c-format */
4717 (_("%pB: %s: invalid needed version %d"),
4718 abfd, name, vernum);
4719 bfd_set_error (bfd_error_bad_value);
4720 goto error_free_vers;
4721 }
4722 }
4723
4724 namelen = strlen (name);
4725 verlen = strlen (verstr);
4726 newlen = namelen + verlen + 2;
4727 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4728 && isym->st_shndx != SHN_UNDEF)
4729 ++newlen;
4730
4731 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4732 if (newname == NULL)
4733 goto error_free_vers;
4734 memcpy (newname, name, namelen);
4735 p = newname + namelen;
4736 *p++ = ELF_VER_CHR;
4737 /* If this is a defined non-hidden version symbol,
4738 we add another @ to the name. This indicates the
4739 default version of the symbol. */
4740 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4741 && isym->st_shndx != SHN_UNDEF)
4742 *p++ = ELF_VER_CHR;
4743 memcpy (p, verstr, verlen + 1);
4744
4745 name = newname;
4746 }
4747
4748 /* If this symbol has default visibility and the user has
4749 requested we not re-export it, then mark it as hidden. */
4750 if (!bfd_is_und_section (sec)
4751 && !dynamic
4752 && abfd->no_export
4753 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4754 isym->st_other = (STV_HIDDEN
4755 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4756
4757 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4758 sym_hash, &old_bfd, &old_weak,
4759 &old_alignment, &skip, &override,
4760 &type_change_ok, &size_change_ok,
4761 &matched))
4762 goto error_free_vers;
4763
4764 if (skip)
4765 continue;
4766
4767 /* Override a definition only if the new symbol matches the
4768 existing one. */
4769 if (override && matched)
4770 definition = FALSE;
4771
4772 h = *sym_hash;
4773 while (h->root.type == bfd_link_hash_indirect
4774 || h->root.type == bfd_link_hash_warning)
4775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4776
4777 if (elf_tdata (abfd)->verdef != NULL
4778 && vernum > 1
4779 && definition)
4780 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4781 }
4782
4783 if (! (_bfd_generic_link_add_one_symbol
4784 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4785 (struct bfd_link_hash_entry **) sym_hash)))
4786 goto error_free_vers;
4787
4788 h = *sym_hash;
4789 /* We need to make sure that indirect symbol dynamic flags are
4790 updated. */
4791 hi = h;
4792 while (h->root.type == bfd_link_hash_indirect
4793 || h->root.type == bfd_link_hash_warning)
4794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4795
4796 /* Setting the index to -3 tells elf_link_output_extsym that
4797 this symbol is defined in a discarded section. */
4798 if (discarded)
4799 h->indx = -3;
4800
4801 *sym_hash = h;
4802
4803 new_weak = (flags & BSF_WEAK) != 0;
4804 if (dynamic
4805 && definition
4806 && new_weak
4807 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4808 && is_elf_hash_table (htab)
4809 && h->u.alias == NULL)
4810 {
4811 /* Keep a list of all weak defined non function symbols from
4812 a dynamic object, using the alias field. Later in this
4813 function we will set the alias field to the correct
4814 value. We only put non-function symbols from dynamic
4815 objects on this list, because that happens to be the only
4816 time we need to know the normal symbol corresponding to a
4817 weak symbol, and the information is time consuming to
4818 figure out. If the alias field is not already NULL,
4819 then this symbol was already defined by some previous
4820 dynamic object, and we will be using that previous
4821 definition anyhow. */
4822
4823 h->u.alias = weaks;
4824 weaks = h;
4825 }
4826
4827 /* Set the alignment of a common symbol. */
4828 if ((common || bfd_is_com_section (sec))
4829 && h->root.type == bfd_link_hash_common)
4830 {
4831 unsigned int align;
4832
4833 if (common)
4834 align = bfd_log2 (isym->st_value);
4835 else
4836 {
4837 /* The new symbol is a common symbol in a shared object.
4838 We need to get the alignment from the section. */
4839 align = new_sec->alignment_power;
4840 }
4841 if (align > old_alignment)
4842 h->root.u.c.p->alignment_power = align;
4843 else
4844 h->root.u.c.p->alignment_power = old_alignment;
4845 }
4846
4847 if (is_elf_hash_table (htab))
4848 {
4849 /* Set a flag in the hash table entry indicating the type of
4850 reference or definition we just found. A dynamic symbol
4851 is one which is referenced or defined by both a regular
4852 object and a shared object. */
4853 bfd_boolean dynsym = FALSE;
4854
4855 /* Plugin symbols aren't normal. Don't set def_regular or
4856 ref_regular for them, or make them dynamic. */
4857 if ((abfd->flags & BFD_PLUGIN) != 0)
4858 ;
4859 else if (! dynamic)
4860 {
4861 if (! definition)
4862 {
4863 h->ref_regular = 1;
4864 if (bind != STB_WEAK)
4865 h->ref_regular_nonweak = 1;
4866 }
4867 else
4868 {
4869 h->def_regular = 1;
4870 if (h->def_dynamic)
4871 {
4872 h->def_dynamic = 0;
4873 h->ref_dynamic = 1;
4874 }
4875 }
4876
4877 /* If the indirect symbol has been forced local, don't
4878 make the real symbol dynamic. */
4879 if ((h == hi || !hi->forced_local)
4880 && (bfd_link_dll (info)
4881 || h->def_dynamic
4882 || h->ref_dynamic))
4883 dynsym = TRUE;
4884 }
4885 else
4886 {
4887 if (! definition)
4888 {
4889 h->ref_dynamic = 1;
4890 hi->ref_dynamic = 1;
4891 }
4892 else
4893 {
4894 h->def_dynamic = 1;
4895 hi->def_dynamic = 1;
4896 }
4897
4898 /* If the indirect symbol has been forced local, don't
4899 make the real symbol dynamic. */
4900 if ((h == hi || !hi->forced_local)
4901 && (h->def_regular
4902 || h->ref_regular
4903 || (h->is_weakalias
4904 && weakdef (h)->dynindx != -1)))
4905 dynsym = TRUE;
4906 }
4907
4908 /* Check to see if we need to add an indirect symbol for
4909 the default name. */
4910 if (definition
4911 || (!override && h->root.type == bfd_link_hash_common))
4912 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4913 sec, value, &old_bfd, &dynsym))
4914 goto error_free_vers;
4915
4916 /* Check the alignment when a common symbol is involved. This
4917 can change when a common symbol is overridden by a normal
4918 definition or a common symbol is ignored due to the old
4919 normal definition. We need to make sure the maximum
4920 alignment is maintained. */
4921 if ((old_alignment || common)
4922 && h->root.type != bfd_link_hash_common)
4923 {
4924 unsigned int common_align;
4925 unsigned int normal_align;
4926 unsigned int symbol_align;
4927 bfd *normal_bfd;
4928 bfd *common_bfd;
4929
4930 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4931 || h->root.type == bfd_link_hash_defweak);
4932
4933 symbol_align = ffs (h->root.u.def.value) - 1;
4934 if (h->root.u.def.section->owner != NULL
4935 && (h->root.u.def.section->owner->flags
4936 & (DYNAMIC | BFD_PLUGIN)) == 0)
4937 {
4938 normal_align = h->root.u.def.section->alignment_power;
4939 if (normal_align > symbol_align)
4940 normal_align = symbol_align;
4941 }
4942 else
4943 normal_align = symbol_align;
4944
4945 if (old_alignment)
4946 {
4947 common_align = old_alignment;
4948 common_bfd = old_bfd;
4949 normal_bfd = abfd;
4950 }
4951 else
4952 {
4953 common_align = bfd_log2 (isym->st_value);
4954 common_bfd = abfd;
4955 normal_bfd = old_bfd;
4956 }
4957
4958 if (normal_align < common_align)
4959 {
4960 /* PR binutils/2735 */
4961 if (normal_bfd == NULL)
4962 _bfd_error_handler
4963 /* xgettext:c-format */
4964 (_("warning: alignment %u of common symbol `%s' in %pB is"
4965 " greater than the alignment (%u) of its section %pA"),
4966 1 << common_align, name, common_bfd,
4967 1 << normal_align, h->root.u.def.section);
4968 else
4969 _bfd_error_handler
4970 /* xgettext:c-format */
4971 (_("warning: alignment %u of symbol `%s' in %pB"
4972 " is smaller than %u in %pB"),
4973 1 << normal_align, name, normal_bfd,
4974 1 << common_align, common_bfd);
4975 }
4976 }
4977
4978 /* Remember the symbol size if it isn't undefined. */
4979 if (isym->st_size != 0
4980 && isym->st_shndx != SHN_UNDEF
4981 && (definition || h->size == 0))
4982 {
4983 if (h->size != 0
4984 && h->size != isym->st_size
4985 && ! size_change_ok)
4986 _bfd_error_handler
4987 /* xgettext:c-format */
4988 (_("warning: size of symbol `%s' changed"
4989 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4990 name, (uint64_t) h->size, old_bfd,
4991 (uint64_t) isym->st_size, abfd);
4992
4993 h->size = isym->st_size;
4994 }
4995
4996 /* If this is a common symbol, then we always want H->SIZE
4997 to be the size of the common symbol. The code just above
4998 won't fix the size if a common symbol becomes larger. We
4999 don't warn about a size change here, because that is
5000 covered by --warn-common. Allow changes between different
5001 function types. */
5002 if (h->root.type == bfd_link_hash_common)
5003 h->size = h->root.u.c.size;
5004
5005 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5006 && ((definition && !new_weak)
5007 || (old_weak && h->root.type == bfd_link_hash_common)
5008 || h->type == STT_NOTYPE))
5009 {
5010 unsigned int type = ELF_ST_TYPE (isym->st_info);
5011
5012 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5013 symbol. */
5014 if (type == STT_GNU_IFUNC
5015 && (abfd->flags & DYNAMIC) != 0)
5016 type = STT_FUNC;
5017
5018 if (h->type != type)
5019 {
5020 if (h->type != STT_NOTYPE && ! type_change_ok)
5021 /* xgettext:c-format */
5022 _bfd_error_handler
5023 (_("warning: type of symbol `%s' changed"
5024 " from %d to %d in %pB"),
5025 name, h->type, type, abfd);
5026
5027 h->type = type;
5028 }
5029 }
5030
5031 /* Merge st_other field. */
5032 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5033
5034 /* We don't want to make debug symbol dynamic. */
5035 if (definition
5036 && (sec->flags & SEC_DEBUGGING)
5037 && !bfd_link_relocatable (info))
5038 dynsym = FALSE;
5039
5040 /* Nor should we make plugin symbols dynamic. */
5041 if ((abfd->flags & BFD_PLUGIN) != 0)
5042 dynsym = FALSE;
5043
5044 if (definition)
5045 {
5046 h->target_internal = isym->st_target_internal;
5047 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5048 }
5049
5050 if (definition && !dynamic)
5051 {
5052 char *p = strchr (name, ELF_VER_CHR);
5053 if (p != NULL && p[1] != ELF_VER_CHR)
5054 {
5055 /* Queue non-default versions so that .symver x, x@FOO
5056 aliases can be checked. */
5057 if (!nondeflt_vers)
5058 {
5059 size_t amt = ((isymend - isym + 1)
5060 * sizeof (struct elf_link_hash_entry *));
5061 nondeflt_vers
5062 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5063 if (!nondeflt_vers)
5064 goto error_free_vers;
5065 }
5066 nondeflt_vers[nondeflt_vers_cnt++] = h;
5067 }
5068 }
5069
5070 if (dynsym && h->dynindx == -1)
5071 {
5072 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5073 goto error_free_vers;
5074 if (h->is_weakalias
5075 && weakdef (h)->dynindx == -1)
5076 {
5077 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5078 goto error_free_vers;
5079 }
5080 }
5081 else if (h->dynindx != -1)
5082 /* If the symbol already has a dynamic index, but
5083 visibility says it should not be visible, turn it into
5084 a local symbol. */
5085 switch (ELF_ST_VISIBILITY (h->other))
5086 {
5087 case STV_INTERNAL:
5088 case STV_HIDDEN:
5089 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5090 dynsym = FALSE;
5091 break;
5092 }
5093
5094 /* Don't add DT_NEEDED for references from the dummy bfd nor
5095 for unmatched symbol. */
5096 if (!add_needed
5097 && matched
5098 && definition
5099 && ((dynsym
5100 && h->ref_regular_nonweak
5101 && (old_bfd == NULL
5102 || (old_bfd->flags & BFD_PLUGIN) == 0))
5103 || (h->ref_dynamic_nonweak
5104 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5105 && !on_needed_list (elf_dt_name (abfd),
5106 htab->needed, NULL))))
5107 {
5108 const char *soname = elf_dt_name (abfd);
5109
5110 info->callbacks->minfo ("%!", soname, old_bfd,
5111 h->root.root.string);
5112
5113 /* A symbol from a library loaded via DT_NEEDED of some
5114 other library is referenced by a regular object.
5115 Add a DT_NEEDED entry for it. Issue an error if
5116 --no-add-needed is used and the reference was not
5117 a weak one. */
5118 if (old_bfd != NULL
5119 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5120 {
5121 _bfd_error_handler
5122 /* xgettext:c-format */
5123 (_("%pB: undefined reference to symbol '%s'"),
5124 old_bfd, name);
5125 bfd_set_error (bfd_error_missing_dso);
5126 goto error_free_vers;
5127 }
5128
5129 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5130 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5131
5132 /* Create dynamic sections for backends that require
5133 that be done before setup_gnu_properties. */
5134 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5135 return FALSE;
5136 add_needed = TRUE;
5137 }
5138 }
5139 }
5140
5141 if (info->lto_plugin_active
5142 && !bfd_link_relocatable (info)
5143 && (abfd->flags & BFD_PLUGIN) == 0
5144 && !just_syms
5145 && extsymcount)
5146 {
5147 int r_sym_shift;
5148
5149 if (bed->s->arch_size == 32)
5150 r_sym_shift = 8;
5151 else
5152 r_sym_shift = 32;
5153
5154 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5155 referenced in regular objects so that linker plugin will get
5156 the correct symbol resolution. */
5157
5158 sym_hash = elf_sym_hashes (abfd);
5159 for (s = abfd->sections; s != NULL; s = s->next)
5160 {
5161 Elf_Internal_Rela *internal_relocs;
5162 Elf_Internal_Rela *rel, *relend;
5163
5164 /* Don't check relocations in excluded sections. */
5165 if ((s->flags & SEC_RELOC) == 0
5166 || s->reloc_count == 0
5167 || (s->flags & SEC_EXCLUDE) != 0
5168 || ((info->strip == strip_all
5169 || info->strip == strip_debugger)
5170 && (s->flags & SEC_DEBUGGING) != 0))
5171 continue;
5172
5173 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5174 NULL,
5175 info->keep_memory);
5176 if (internal_relocs == NULL)
5177 goto error_free_vers;
5178
5179 rel = internal_relocs;
5180 relend = rel + s->reloc_count;
5181 for ( ; rel < relend; rel++)
5182 {
5183 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5184 struct elf_link_hash_entry *h;
5185
5186 /* Skip local symbols. */
5187 if (r_symndx < extsymoff)
5188 continue;
5189
5190 h = sym_hash[r_symndx - extsymoff];
5191 if (h != NULL)
5192 h->root.non_ir_ref_regular = 1;
5193 }
5194
5195 if (elf_section_data (s)->relocs != internal_relocs)
5196 free (internal_relocs);
5197 }
5198 }
5199
5200 if (extversym != NULL)
5201 {
5202 free (extversym);
5203 extversym = NULL;
5204 }
5205
5206 if (isymbuf != NULL)
5207 {
5208 free (isymbuf);
5209 isymbuf = NULL;
5210 }
5211
5212 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5213 {
5214 unsigned int i;
5215
5216 /* Restore the symbol table. */
5217 old_ent = (char *) old_tab + tabsize;
5218 memset (elf_sym_hashes (abfd), 0,
5219 extsymcount * sizeof (struct elf_link_hash_entry *));
5220 htab->root.table.table = old_table;
5221 htab->root.table.size = old_size;
5222 htab->root.table.count = old_count;
5223 memcpy (htab->root.table.table, old_tab, tabsize);
5224 htab->root.undefs = old_undefs;
5225 htab->root.undefs_tail = old_undefs_tail;
5226 if (htab->dynstr != NULL)
5227 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5228 free (old_strtab);
5229 old_strtab = NULL;
5230 for (i = 0; i < htab->root.table.size; i++)
5231 {
5232 struct bfd_hash_entry *p;
5233 struct elf_link_hash_entry *h;
5234 bfd_size_type size;
5235 unsigned int alignment_power;
5236 unsigned int non_ir_ref_dynamic;
5237
5238 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5239 {
5240 h = (struct elf_link_hash_entry *) p;
5241 if (h->root.type == bfd_link_hash_warning)
5242 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5243
5244 /* Preserve the maximum alignment and size for common
5245 symbols even if this dynamic lib isn't on DT_NEEDED
5246 since it can still be loaded at run time by another
5247 dynamic lib. */
5248 if (h->root.type == bfd_link_hash_common)
5249 {
5250 size = h->root.u.c.size;
5251 alignment_power = h->root.u.c.p->alignment_power;
5252 }
5253 else
5254 {
5255 size = 0;
5256 alignment_power = 0;
5257 }
5258 /* Preserve non_ir_ref_dynamic so that this symbol
5259 will be exported when the dynamic lib becomes needed
5260 in the second pass. */
5261 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5262 memcpy (p, old_ent, htab->root.table.entsize);
5263 old_ent = (char *) old_ent + htab->root.table.entsize;
5264 h = (struct elf_link_hash_entry *) p;
5265 if (h->root.type == bfd_link_hash_warning)
5266 {
5267 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5268 old_ent = (char *) old_ent + htab->root.table.entsize;
5269 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5270 }
5271 if (h->root.type == bfd_link_hash_common)
5272 {
5273 if (size > h->root.u.c.size)
5274 h->root.u.c.size = size;
5275 if (alignment_power > h->root.u.c.p->alignment_power)
5276 h->root.u.c.p->alignment_power = alignment_power;
5277 }
5278 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5279 }
5280 }
5281
5282 /* Make a special call to the linker "notice" function to
5283 tell it that symbols added for crefs may need to be removed. */
5284 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5285 goto error_free_vers;
5286
5287 free (old_tab);
5288 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5289 alloc_mark);
5290 if (nondeflt_vers != NULL)
5291 free (nondeflt_vers);
5292 return TRUE;
5293 }
5294
5295 if (old_tab != NULL)
5296 {
5297 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5298 goto error_free_vers;
5299 free (old_tab);
5300 old_tab = NULL;
5301 }
5302
5303 /* Now that all the symbols from this input file are created, if
5304 not performing a relocatable link, handle .symver foo, foo@BAR
5305 such that any relocs against foo become foo@BAR. */
5306 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5307 {
5308 size_t cnt, symidx;
5309
5310 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5311 {
5312 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5313 char *shortname, *p;
5314 size_t amt;
5315
5316 p = strchr (h->root.root.string, ELF_VER_CHR);
5317 if (p == NULL
5318 || (h->root.type != bfd_link_hash_defined
5319 && h->root.type != bfd_link_hash_defweak))
5320 continue;
5321
5322 amt = p - h->root.root.string;
5323 shortname = (char *) bfd_malloc (amt + 1);
5324 if (!shortname)
5325 goto error_free_vers;
5326 memcpy (shortname, h->root.root.string, amt);
5327 shortname[amt] = '\0';
5328
5329 hi = (struct elf_link_hash_entry *)
5330 bfd_link_hash_lookup (&htab->root, shortname,
5331 FALSE, FALSE, FALSE);
5332 if (hi != NULL
5333 && hi->root.type == h->root.type
5334 && hi->root.u.def.value == h->root.u.def.value
5335 && hi->root.u.def.section == h->root.u.def.section)
5336 {
5337 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5338 hi->root.type = bfd_link_hash_indirect;
5339 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5340 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5341 sym_hash = elf_sym_hashes (abfd);
5342 if (sym_hash)
5343 for (symidx = 0; symidx < extsymcount; ++symidx)
5344 if (sym_hash[symidx] == hi)
5345 {
5346 sym_hash[symidx] = h;
5347 break;
5348 }
5349 }
5350 free (shortname);
5351 }
5352 free (nondeflt_vers);
5353 nondeflt_vers = NULL;
5354 }
5355
5356 /* Now set the alias field correctly for all the weak defined
5357 symbols we found. The only way to do this is to search all the
5358 symbols. Since we only need the information for non functions in
5359 dynamic objects, that's the only time we actually put anything on
5360 the list WEAKS. We need this information so that if a regular
5361 object refers to a symbol defined weakly in a dynamic object, the
5362 real symbol in the dynamic object is also put in the dynamic
5363 symbols; we also must arrange for both symbols to point to the
5364 same memory location. We could handle the general case of symbol
5365 aliasing, but a general symbol alias can only be generated in
5366 assembler code, handling it correctly would be very time
5367 consuming, and other ELF linkers don't handle general aliasing
5368 either. */
5369 if (weaks != NULL)
5370 {
5371 struct elf_link_hash_entry **hpp;
5372 struct elf_link_hash_entry **hppend;
5373 struct elf_link_hash_entry **sorted_sym_hash;
5374 struct elf_link_hash_entry *h;
5375 size_t sym_count, amt;
5376
5377 /* Since we have to search the whole symbol list for each weak
5378 defined symbol, search time for N weak defined symbols will be
5379 O(N^2). Binary search will cut it down to O(NlogN). */
5380 amt = extsymcount * sizeof (*sorted_sym_hash);
5381 sorted_sym_hash = bfd_malloc (amt);
5382 if (sorted_sym_hash == NULL)
5383 goto error_return;
5384 sym_hash = sorted_sym_hash;
5385 hpp = elf_sym_hashes (abfd);
5386 hppend = hpp + extsymcount;
5387 sym_count = 0;
5388 for (; hpp < hppend; hpp++)
5389 {
5390 h = *hpp;
5391 if (h != NULL
5392 && h->root.type == bfd_link_hash_defined
5393 && !bed->is_function_type (h->type))
5394 {
5395 *sym_hash = h;
5396 sym_hash++;
5397 sym_count++;
5398 }
5399 }
5400
5401 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5402 elf_sort_symbol);
5403
5404 while (weaks != NULL)
5405 {
5406 struct elf_link_hash_entry *hlook;
5407 asection *slook;
5408 bfd_vma vlook;
5409 size_t i, j, idx = 0;
5410
5411 hlook = weaks;
5412 weaks = hlook->u.alias;
5413 hlook->u.alias = NULL;
5414
5415 if (hlook->root.type != bfd_link_hash_defined
5416 && hlook->root.type != bfd_link_hash_defweak)
5417 continue;
5418
5419 slook = hlook->root.u.def.section;
5420 vlook = hlook->root.u.def.value;
5421
5422 i = 0;
5423 j = sym_count;
5424 while (i != j)
5425 {
5426 bfd_signed_vma vdiff;
5427 idx = (i + j) / 2;
5428 h = sorted_sym_hash[idx];
5429 vdiff = vlook - h->root.u.def.value;
5430 if (vdiff < 0)
5431 j = idx;
5432 else if (vdiff > 0)
5433 i = idx + 1;
5434 else
5435 {
5436 int sdiff = slook->id - h->root.u.def.section->id;
5437 if (sdiff < 0)
5438 j = idx;
5439 else if (sdiff > 0)
5440 i = idx + 1;
5441 else
5442 break;
5443 }
5444 }
5445
5446 /* We didn't find a value/section match. */
5447 if (i == j)
5448 continue;
5449
5450 /* With multiple aliases, or when the weak symbol is already
5451 strongly defined, we have multiple matching symbols and
5452 the binary search above may land on any of them. Step
5453 one past the matching symbol(s). */
5454 while (++idx != j)
5455 {
5456 h = sorted_sym_hash[idx];
5457 if (h->root.u.def.section != slook
5458 || h->root.u.def.value != vlook)
5459 break;
5460 }
5461
5462 /* Now look back over the aliases. Since we sorted by size
5463 as well as value and section, we'll choose the one with
5464 the largest size. */
5465 while (idx-- != i)
5466 {
5467 h = sorted_sym_hash[idx];
5468
5469 /* Stop if value or section doesn't match. */
5470 if (h->root.u.def.section != slook
5471 || h->root.u.def.value != vlook)
5472 break;
5473 else if (h != hlook)
5474 {
5475 struct elf_link_hash_entry *t;
5476
5477 hlook->u.alias = h;
5478 hlook->is_weakalias = 1;
5479 t = h;
5480 if (t->u.alias != NULL)
5481 while (t->u.alias != h)
5482 t = t->u.alias;
5483 t->u.alias = hlook;
5484
5485 /* If the weak definition is in the list of dynamic
5486 symbols, make sure the real definition is put
5487 there as well. */
5488 if (hlook->dynindx != -1 && h->dynindx == -1)
5489 {
5490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5491 {
5492 err_free_sym_hash:
5493 free (sorted_sym_hash);
5494 goto error_return;
5495 }
5496 }
5497
5498 /* If the real definition is in the list of dynamic
5499 symbols, make sure the weak definition is put
5500 there as well. If we don't do this, then the
5501 dynamic loader might not merge the entries for the
5502 real definition and the weak definition. */
5503 if (h->dynindx != -1 && hlook->dynindx == -1)
5504 {
5505 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5506 goto err_free_sym_hash;
5507 }
5508 break;
5509 }
5510 }
5511 }
5512
5513 free (sorted_sym_hash);
5514 }
5515
5516 if (bed->check_directives
5517 && !(*bed->check_directives) (abfd, info))
5518 return FALSE;
5519
5520 /* If this is a non-traditional link, try to optimize the handling
5521 of the .stab/.stabstr sections. */
5522 if (! dynamic
5523 && ! info->traditional_format
5524 && is_elf_hash_table (htab)
5525 && (info->strip != strip_all && info->strip != strip_debugger))
5526 {
5527 asection *stabstr;
5528
5529 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5530 if (stabstr != NULL)
5531 {
5532 bfd_size_type string_offset = 0;
5533 asection *stab;
5534
5535 for (stab = abfd->sections; stab; stab = stab->next)
5536 if (CONST_STRNEQ (stab->name, ".stab")
5537 && (!stab->name[5] ||
5538 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5539 && (stab->flags & SEC_MERGE) == 0
5540 && !bfd_is_abs_section (stab->output_section))
5541 {
5542 struct bfd_elf_section_data *secdata;
5543
5544 secdata = elf_section_data (stab);
5545 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5546 stabstr, &secdata->sec_info,
5547 &string_offset))
5548 goto error_return;
5549 if (secdata->sec_info)
5550 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5551 }
5552 }
5553 }
5554
5555 if (dynamic && add_needed)
5556 {
5557 /* Add this bfd to the loaded list. */
5558 struct elf_link_loaded_list *n;
5559
5560 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5561 if (n == NULL)
5562 goto error_return;
5563 n->abfd = abfd;
5564 n->next = htab->dyn_loaded;
5565 htab->dyn_loaded = n;
5566 }
5567 if (dynamic && !add_needed
5568 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5569 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5570
5571 return TRUE;
5572
5573 error_free_vers:
5574 if (old_tab != NULL)
5575 free (old_tab);
5576 if (old_strtab != NULL)
5577 free (old_strtab);
5578 if (nondeflt_vers != NULL)
5579 free (nondeflt_vers);
5580 if (extversym != NULL)
5581 free (extversym);
5582 error_free_sym:
5583 if (isymbuf != NULL)
5584 free (isymbuf);
5585 error_return:
5586 return FALSE;
5587 }
5588
5589 /* Return the linker hash table entry of a symbol that might be
5590 satisfied by an archive symbol. Return -1 on error. */
5591
5592 struct elf_link_hash_entry *
5593 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5594 struct bfd_link_info *info,
5595 const char *name)
5596 {
5597 struct elf_link_hash_entry *h;
5598 char *p, *copy;
5599 size_t len, first;
5600
5601 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5602 if (h != NULL)
5603 return h;
5604
5605 /* If this is a default version (the name contains @@), look up the
5606 symbol again with only one `@' as well as without the version.
5607 The effect is that references to the symbol with and without the
5608 version will be matched by the default symbol in the archive. */
5609
5610 p = strchr (name, ELF_VER_CHR);
5611 if (p == NULL || p[1] != ELF_VER_CHR)
5612 return h;
5613
5614 /* First check with only one `@'. */
5615 len = strlen (name);
5616 copy = (char *) bfd_alloc (abfd, len);
5617 if (copy == NULL)
5618 return (struct elf_link_hash_entry *) -1;
5619
5620 first = p - name + 1;
5621 memcpy (copy, name, first);
5622 memcpy (copy + first, name + first + 1, len - first);
5623
5624 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5625 if (h == NULL)
5626 {
5627 /* We also need to check references to the symbol without the
5628 version. */
5629 copy[first - 1] = '\0';
5630 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5631 FALSE, FALSE, TRUE);
5632 }
5633
5634 bfd_release (abfd, copy);
5635 return h;
5636 }
5637
5638 /* Add symbols from an ELF archive file to the linker hash table. We
5639 don't use _bfd_generic_link_add_archive_symbols because we need to
5640 handle versioned symbols.
5641
5642 Fortunately, ELF archive handling is simpler than that done by
5643 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5644 oddities. In ELF, if we find a symbol in the archive map, and the
5645 symbol is currently undefined, we know that we must pull in that
5646 object file.
5647
5648 Unfortunately, we do have to make multiple passes over the symbol
5649 table until nothing further is resolved. */
5650
5651 static bfd_boolean
5652 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5653 {
5654 symindex c;
5655 unsigned char *included = NULL;
5656 carsym *symdefs;
5657 bfd_boolean loop;
5658 size_t amt;
5659 const struct elf_backend_data *bed;
5660 struct elf_link_hash_entry * (*archive_symbol_lookup)
5661 (bfd *, struct bfd_link_info *, const char *);
5662
5663 if (! bfd_has_map (abfd))
5664 {
5665 /* An empty archive is a special case. */
5666 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5667 return TRUE;
5668 bfd_set_error (bfd_error_no_armap);
5669 return FALSE;
5670 }
5671
5672 /* Keep track of all symbols we know to be already defined, and all
5673 files we know to be already included. This is to speed up the
5674 second and subsequent passes. */
5675 c = bfd_ardata (abfd)->symdef_count;
5676 if (c == 0)
5677 return TRUE;
5678 amt = c * sizeof (*included);
5679 included = (unsigned char *) bfd_zmalloc (amt);
5680 if (included == NULL)
5681 return FALSE;
5682
5683 symdefs = bfd_ardata (abfd)->symdefs;
5684 bed = get_elf_backend_data (abfd);
5685 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5686
5687 do
5688 {
5689 file_ptr last;
5690 symindex i;
5691 carsym *symdef;
5692 carsym *symdefend;
5693
5694 loop = FALSE;
5695 last = -1;
5696
5697 symdef = symdefs;
5698 symdefend = symdef + c;
5699 for (i = 0; symdef < symdefend; symdef++, i++)
5700 {
5701 struct elf_link_hash_entry *h;
5702 bfd *element;
5703 struct bfd_link_hash_entry *undefs_tail;
5704 symindex mark;
5705
5706 if (included[i])
5707 continue;
5708 if (symdef->file_offset == last)
5709 {
5710 included[i] = TRUE;
5711 continue;
5712 }
5713
5714 h = archive_symbol_lookup (abfd, info, symdef->name);
5715 if (h == (struct elf_link_hash_entry *) -1)
5716 goto error_return;
5717
5718 if (h == NULL)
5719 continue;
5720
5721 if (h->root.type == bfd_link_hash_common)
5722 {
5723 /* We currently have a common symbol. The archive map contains
5724 a reference to this symbol, so we may want to include it. We
5725 only want to include it however, if this archive element
5726 contains a definition of the symbol, not just another common
5727 declaration of it.
5728
5729 Unfortunately some archivers (including GNU ar) will put
5730 declarations of common symbols into their archive maps, as
5731 well as real definitions, so we cannot just go by the archive
5732 map alone. Instead we must read in the element's symbol
5733 table and check that to see what kind of symbol definition
5734 this is. */
5735 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5736 continue;
5737 }
5738 else if (h->root.type != bfd_link_hash_undefined)
5739 {
5740 if (h->root.type != bfd_link_hash_undefweak)
5741 /* Symbol must be defined. Don't check it again. */
5742 included[i] = TRUE;
5743 continue;
5744 }
5745
5746 /* We need to include this archive member. */
5747 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5748 if (element == NULL)
5749 goto error_return;
5750
5751 if (! bfd_check_format (element, bfd_object))
5752 goto error_return;
5753
5754 undefs_tail = info->hash->undefs_tail;
5755
5756 if (!(*info->callbacks
5757 ->add_archive_element) (info, element, symdef->name, &element))
5758 continue;
5759 if (!bfd_link_add_symbols (element, info))
5760 goto error_return;
5761
5762 /* If there are any new undefined symbols, we need to make
5763 another pass through the archive in order to see whether
5764 they can be defined. FIXME: This isn't perfect, because
5765 common symbols wind up on undefs_tail and because an
5766 undefined symbol which is defined later on in this pass
5767 does not require another pass. This isn't a bug, but it
5768 does make the code less efficient than it could be. */
5769 if (undefs_tail != info->hash->undefs_tail)
5770 loop = TRUE;
5771
5772 /* Look backward to mark all symbols from this object file
5773 which we have already seen in this pass. */
5774 mark = i;
5775 do
5776 {
5777 included[mark] = TRUE;
5778 if (mark == 0)
5779 break;
5780 --mark;
5781 }
5782 while (symdefs[mark].file_offset == symdef->file_offset);
5783
5784 /* We mark subsequent symbols from this object file as we go
5785 on through the loop. */
5786 last = symdef->file_offset;
5787 }
5788 }
5789 while (loop);
5790
5791 free (included);
5792
5793 return TRUE;
5794
5795 error_return:
5796 if (included != NULL)
5797 free (included);
5798 return FALSE;
5799 }
5800
5801 /* Given an ELF BFD, add symbols to the global hash table as
5802 appropriate. */
5803
5804 bfd_boolean
5805 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5806 {
5807 switch (bfd_get_format (abfd))
5808 {
5809 case bfd_object:
5810 return elf_link_add_object_symbols (abfd, info);
5811 case bfd_archive:
5812 return elf_link_add_archive_symbols (abfd, info);
5813 default:
5814 bfd_set_error (bfd_error_wrong_format);
5815 return FALSE;
5816 }
5817 }
5818 \f
5819 struct hash_codes_info
5820 {
5821 unsigned long *hashcodes;
5822 bfd_boolean error;
5823 };
5824
5825 /* This function will be called though elf_link_hash_traverse to store
5826 all hash value of the exported symbols in an array. */
5827
5828 static bfd_boolean
5829 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5830 {
5831 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5832 const char *name;
5833 unsigned long ha;
5834 char *alc = NULL;
5835
5836 /* Ignore indirect symbols. These are added by the versioning code. */
5837 if (h->dynindx == -1)
5838 return TRUE;
5839
5840 name = h->root.root.string;
5841 if (h->versioned >= versioned)
5842 {
5843 char *p = strchr (name, ELF_VER_CHR);
5844 if (p != NULL)
5845 {
5846 alc = (char *) bfd_malloc (p - name + 1);
5847 if (alc == NULL)
5848 {
5849 inf->error = TRUE;
5850 return FALSE;
5851 }
5852 memcpy (alc, name, p - name);
5853 alc[p - name] = '\0';
5854 name = alc;
5855 }
5856 }
5857
5858 /* Compute the hash value. */
5859 ha = bfd_elf_hash (name);
5860
5861 /* Store the found hash value in the array given as the argument. */
5862 *(inf->hashcodes)++ = ha;
5863
5864 /* And store it in the struct so that we can put it in the hash table
5865 later. */
5866 h->u.elf_hash_value = ha;
5867
5868 if (alc != NULL)
5869 free (alc);
5870
5871 return TRUE;
5872 }
5873
5874 struct collect_gnu_hash_codes
5875 {
5876 bfd *output_bfd;
5877 const struct elf_backend_data *bed;
5878 unsigned long int nsyms;
5879 unsigned long int maskbits;
5880 unsigned long int *hashcodes;
5881 unsigned long int *hashval;
5882 unsigned long int *indx;
5883 unsigned long int *counts;
5884 bfd_vma *bitmask;
5885 bfd_byte *contents;
5886 bfd_size_type xlat;
5887 long int min_dynindx;
5888 unsigned long int bucketcount;
5889 unsigned long int symindx;
5890 long int local_indx;
5891 long int shift1, shift2;
5892 unsigned long int mask;
5893 bfd_boolean error;
5894 };
5895
5896 /* This function will be called though elf_link_hash_traverse to store
5897 all hash value of the exported symbols in an array. */
5898
5899 static bfd_boolean
5900 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5901 {
5902 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5903 const char *name;
5904 unsigned long ha;
5905 char *alc = NULL;
5906
5907 /* Ignore indirect symbols. These are added by the versioning code. */
5908 if (h->dynindx == -1)
5909 return TRUE;
5910
5911 /* Ignore also local symbols and undefined symbols. */
5912 if (! (*s->bed->elf_hash_symbol) (h))
5913 return TRUE;
5914
5915 name = h->root.root.string;
5916 if (h->versioned >= versioned)
5917 {
5918 char *p = strchr (name, ELF_VER_CHR);
5919 if (p != NULL)
5920 {
5921 alc = (char *) bfd_malloc (p - name + 1);
5922 if (alc == NULL)
5923 {
5924 s->error = TRUE;
5925 return FALSE;
5926 }
5927 memcpy (alc, name, p - name);
5928 alc[p - name] = '\0';
5929 name = alc;
5930 }
5931 }
5932
5933 /* Compute the hash value. */
5934 ha = bfd_elf_gnu_hash (name);
5935
5936 /* Store the found hash value in the array for compute_bucket_count,
5937 and also for .dynsym reordering purposes. */
5938 s->hashcodes[s->nsyms] = ha;
5939 s->hashval[h->dynindx] = ha;
5940 ++s->nsyms;
5941 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5942 s->min_dynindx = h->dynindx;
5943
5944 if (alc != NULL)
5945 free (alc);
5946
5947 return TRUE;
5948 }
5949
5950 /* This function will be called though elf_link_hash_traverse to do
5951 final dynamic symbol renumbering in case of .gnu.hash.
5952 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5953 to the translation table. */
5954
5955 static bfd_boolean
5956 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5957 {
5958 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5959 unsigned long int bucket;
5960 unsigned long int val;
5961
5962 /* Ignore indirect symbols. */
5963 if (h->dynindx == -1)
5964 return TRUE;
5965
5966 /* Ignore also local symbols and undefined symbols. */
5967 if (! (*s->bed->elf_hash_symbol) (h))
5968 {
5969 if (h->dynindx >= s->min_dynindx)
5970 {
5971 if (s->bed->record_xhash_symbol != NULL)
5972 {
5973 (*s->bed->record_xhash_symbol) (h, 0);
5974 s->local_indx++;
5975 }
5976 else
5977 h->dynindx = s->local_indx++;
5978 }
5979 return TRUE;
5980 }
5981
5982 bucket = s->hashval[h->dynindx] % s->bucketcount;
5983 val = (s->hashval[h->dynindx] >> s->shift1)
5984 & ((s->maskbits >> s->shift1) - 1);
5985 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5986 s->bitmask[val]
5987 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5988 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5989 if (s->counts[bucket] == 1)
5990 /* Last element terminates the chain. */
5991 val |= 1;
5992 bfd_put_32 (s->output_bfd, val,
5993 s->contents + (s->indx[bucket] - s->symindx) * 4);
5994 --s->counts[bucket];
5995 if (s->bed->record_xhash_symbol != NULL)
5996 {
5997 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5998
5999 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6000 }
6001 else
6002 h->dynindx = s->indx[bucket]++;
6003 return TRUE;
6004 }
6005
6006 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6007
6008 bfd_boolean
6009 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6010 {
6011 return !(h->forced_local
6012 || h->root.type == bfd_link_hash_undefined
6013 || h->root.type == bfd_link_hash_undefweak
6014 || ((h->root.type == bfd_link_hash_defined
6015 || h->root.type == bfd_link_hash_defweak)
6016 && h->root.u.def.section->output_section == NULL));
6017 }
6018
6019 /* Array used to determine the number of hash table buckets to use
6020 based on the number of symbols there are. If there are fewer than
6021 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6022 fewer than 37 we use 17 buckets, and so forth. We never use more
6023 than 32771 buckets. */
6024
6025 static const size_t elf_buckets[] =
6026 {
6027 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6028 16411, 32771, 0
6029 };
6030
6031 /* Compute bucket count for hashing table. We do not use a static set
6032 of possible tables sizes anymore. Instead we determine for all
6033 possible reasonable sizes of the table the outcome (i.e., the
6034 number of collisions etc) and choose the best solution. The
6035 weighting functions are not too simple to allow the table to grow
6036 without bounds. Instead one of the weighting factors is the size.
6037 Therefore the result is always a good payoff between few collisions
6038 (= short chain lengths) and table size. */
6039 static size_t
6040 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6041 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6042 unsigned long int nsyms,
6043 int gnu_hash)
6044 {
6045 size_t best_size = 0;
6046 unsigned long int i;
6047
6048 /* We have a problem here. The following code to optimize the table
6049 size requires an integer type with more the 32 bits. If
6050 BFD_HOST_U_64_BIT is set we know about such a type. */
6051 #ifdef BFD_HOST_U_64_BIT
6052 if (info->optimize)
6053 {
6054 size_t minsize;
6055 size_t maxsize;
6056 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6057 bfd *dynobj = elf_hash_table (info)->dynobj;
6058 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6059 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6060 unsigned long int *counts;
6061 bfd_size_type amt;
6062 unsigned int no_improvement_count = 0;
6063
6064 /* Possible optimization parameters: if we have NSYMS symbols we say
6065 that the hashing table must at least have NSYMS/4 and at most
6066 2*NSYMS buckets. */
6067 minsize = nsyms / 4;
6068 if (minsize == 0)
6069 minsize = 1;
6070 best_size = maxsize = nsyms * 2;
6071 if (gnu_hash)
6072 {
6073 if (minsize < 2)
6074 minsize = 2;
6075 if ((best_size & 31) == 0)
6076 ++best_size;
6077 }
6078
6079 /* Create array where we count the collisions in. We must use bfd_malloc
6080 since the size could be large. */
6081 amt = maxsize;
6082 amt *= sizeof (unsigned long int);
6083 counts = (unsigned long int *) bfd_malloc (amt);
6084 if (counts == NULL)
6085 return 0;
6086
6087 /* Compute the "optimal" size for the hash table. The criteria is a
6088 minimal chain length. The minor criteria is (of course) the size
6089 of the table. */
6090 for (i = minsize; i < maxsize; ++i)
6091 {
6092 /* Walk through the array of hashcodes and count the collisions. */
6093 BFD_HOST_U_64_BIT max;
6094 unsigned long int j;
6095 unsigned long int fact;
6096
6097 if (gnu_hash && (i & 31) == 0)
6098 continue;
6099
6100 memset (counts, '\0', i * sizeof (unsigned long int));
6101
6102 /* Determine how often each hash bucket is used. */
6103 for (j = 0; j < nsyms; ++j)
6104 ++counts[hashcodes[j] % i];
6105
6106 /* For the weight function we need some information about the
6107 pagesize on the target. This is information need not be 100%
6108 accurate. Since this information is not available (so far) we
6109 define it here to a reasonable default value. If it is crucial
6110 to have a better value some day simply define this value. */
6111 # ifndef BFD_TARGET_PAGESIZE
6112 # define BFD_TARGET_PAGESIZE (4096)
6113 # endif
6114
6115 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6116 and the chains. */
6117 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6118
6119 # if 1
6120 /* Variant 1: optimize for short chains. We add the squares
6121 of all the chain lengths (which favors many small chain
6122 over a few long chains). */
6123 for (j = 0; j < i; ++j)
6124 max += counts[j] * counts[j];
6125
6126 /* This adds penalties for the overall size of the table. */
6127 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6128 max *= fact * fact;
6129 # else
6130 /* Variant 2: Optimize a lot more for small table. Here we
6131 also add squares of the size but we also add penalties for
6132 empty slots (the +1 term). */
6133 for (j = 0; j < i; ++j)
6134 max += (1 + counts[j]) * (1 + counts[j]);
6135
6136 /* The overall size of the table is considered, but not as
6137 strong as in variant 1, where it is squared. */
6138 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6139 max *= fact;
6140 # endif
6141
6142 /* Compare with current best results. */
6143 if (max < best_chlen)
6144 {
6145 best_chlen = max;
6146 best_size = i;
6147 no_improvement_count = 0;
6148 }
6149 /* PR 11843: Avoid futile long searches for the best bucket size
6150 when there are a large number of symbols. */
6151 else if (++no_improvement_count == 100)
6152 break;
6153 }
6154
6155 free (counts);
6156 }
6157 else
6158 #endif /* defined (BFD_HOST_U_64_BIT) */
6159 {
6160 /* This is the fallback solution if no 64bit type is available or if we
6161 are not supposed to spend much time on optimizations. We select the
6162 bucket count using a fixed set of numbers. */
6163 for (i = 0; elf_buckets[i] != 0; i++)
6164 {
6165 best_size = elf_buckets[i];
6166 if (nsyms < elf_buckets[i + 1])
6167 break;
6168 }
6169 if (gnu_hash && best_size < 2)
6170 best_size = 2;
6171 }
6172
6173 return best_size;
6174 }
6175
6176 /* Size any SHT_GROUP section for ld -r. */
6177
6178 bfd_boolean
6179 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6180 {
6181 bfd *ibfd;
6182 asection *s;
6183
6184 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6185 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6186 && (s = ibfd->sections) != NULL
6187 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6188 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6189 return FALSE;
6190 return TRUE;
6191 }
6192
6193 /* Set a default stack segment size. The value in INFO wins. If it
6194 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6195 undefined it is initialized. */
6196
6197 bfd_boolean
6198 bfd_elf_stack_segment_size (bfd *output_bfd,
6199 struct bfd_link_info *info,
6200 const char *legacy_symbol,
6201 bfd_vma default_size)
6202 {
6203 struct elf_link_hash_entry *h = NULL;
6204
6205 /* Look for legacy symbol. */
6206 if (legacy_symbol)
6207 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6208 FALSE, FALSE, FALSE);
6209 if (h && (h->root.type == bfd_link_hash_defined
6210 || h->root.type == bfd_link_hash_defweak)
6211 && h->def_regular
6212 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6213 {
6214 /* The symbol has no type if specified on the command line. */
6215 h->type = STT_OBJECT;
6216 if (info->stacksize)
6217 /* xgettext:c-format */
6218 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6219 output_bfd, legacy_symbol);
6220 else if (h->root.u.def.section != bfd_abs_section_ptr)
6221 /* xgettext:c-format */
6222 _bfd_error_handler (_("%pB: %s not absolute"),
6223 output_bfd, legacy_symbol);
6224 else
6225 info->stacksize = h->root.u.def.value;
6226 }
6227
6228 if (!info->stacksize)
6229 /* If the user didn't set a size, or explicitly inhibit the
6230 size, set it now. */
6231 info->stacksize = default_size;
6232
6233 /* Provide the legacy symbol, if it is referenced. */
6234 if (h && (h->root.type == bfd_link_hash_undefined
6235 || h->root.type == bfd_link_hash_undefweak))
6236 {
6237 struct bfd_link_hash_entry *bh = NULL;
6238
6239 if (!(_bfd_generic_link_add_one_symbol
6240 (info, output_bfd, legacy_symbol,
6241 BSF_GLOBAL, bfd_abs_section_ptr,
6242 info->stacksize >= 0 ? info->stacksize : 0,
6243 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6244 return FALSE;
6245
6246 h = (struct elf_link_hash_entry *) bh;
6247 h->def_regular = 1;
6248 h->type = STT_OBJECT;
6249 }
6250
6251 return TRUE;
6252 }
6253
6254 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6255
6256 struct elf_gc_sweep_symbol_info
6257 {
6258 struct bfd_link_info *info;
6259 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6260 bfd_boolean);
6261 };
6262
6263 static bfd_boolean
6264 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6265 {
6266 if (!h->mark
6267 && (((h->root.type == bfd_link_hash_defined
6268 || h->root.type == bfd_link_hash_defweak)
6269 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6270 && h->root.u.def.section->gc_mark))
6271 || h->root.type == bfd_link_hash_undefined
6272 || h->root.type == bfd_link_hash_undefweak))
6273 {
6274 struct elf_gc_sweep_symbol_info *inf;
6275
6276 inf = (struct elf_gc_sweep_symbol_info *) data;
6277 (*inf->hide_symbol) (inf->info, h, TRUE);
6278 h->def_regular = 0;
6279 h->ref_regular = 0;
6280 h->ref_regular_nonweak = 0;
6281 }
6282
6283 return TRUE;
6284 }
6285
6286 /* Set up the sizes and contents of the ELF dynamic sections. This is
6287 called by the ELF linker emulation before_allocation routine. We
6288 must set the sizes of the sections before the linker sets the
6289 addresses of the various sections. */
6290
6291 bfd_boolean
6292 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6293 const char *soname,
6294 const char *rpath,
6295 const char *filter_shlib,
6296 const char *audit,
6297 const char *depaudit,
6298 const char * const *auxiliary_filters,
6299 struct bfd_link_info *info,
6300 asection **sinterpptr)
6301 {
6302 bfd *dynobj;
6303 const struct elf_backend_data *bed;
6304
6305 *sinterpptr = NULL;
6306
6307 if (!is_elf_hash_table (info->hash))
6308 return TRUE;
6309
6310 dynobj = elf_hash_table (info)->dynobj;
6311
6312 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6313 {
6314 struct bfd_elf_version_tree *verdefs;
6315 struct elf_info_failed asvinfo;
6316 struct bfd_elf_version_tree *t;
6317 struct bfd_elf_version_expr *d;
6318 asection *s;
6319 size_t soname_indx;
6320
6321 /* If we are supposed to export all symbols into the dynamic symbol
6322 table (this is not the normal case), then do so. */
6323 if (info->export_dynamic
6324 || (bfd_link_executable (info) && info->dynamic))
6325 {
6326 struct elf_info_failed eif;
6327
6328 eif.info = info;
6329 eif.failed = FALSE;
6330 elf_link_hash_traverse (elf_hash_table (info),
6331 _bfd_elf_export_symbol,
6332 &eif);
6333 if (eif.failed)
6334 return FALSE;
6335 }
6336
6337 if (soname != NULL)
6338 {
6339 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6340 soname, TRUE);
6341 if (soname_indx == (size_t) -1
6342 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6343 return FALSE;
6344 }
6345 else
6346 soname_indx = (size_t) -1;
6347
6348 /* Make all global versions with definition. */
6349 for (t = info->version_info; t != NULL; t = t->next)
6350 for (d = t->globals.list; d != NULL; d = d->next)
6351 if (!d->symver && d->literal)
6352 {
6353 const char *verstr, *name;
6354 size_t namelen, verlen, newlen;
6355 char *newname, *p, leading_char;
6356 struct elf_link_hash_entry *newh;
6357
6358 leading_char = bfd_get_symbol_leading_char (output_bfd);
6359 name = d->pattern;
6360 namelen = strlen (name) + (leading_char != '\0');
6361 verstr = t->name;
6362 verlen = strlen (verstr);
6363 newlen = namelen + verlen + 3;
6364
6365 newname = (char *) bfd_malloc (newlen);
6366 if (newname == NULL)
6367 return FALSE;
6368 newname[0] = leading_char;
6369 memcpy (newname + (leading_char != '\0'), name, namelen);
6370
6371 /* Check the hidden versioned definition. */
6372 p = newname + namelen;
6373 *p++ = ELF_VER_CHR;
6374 memcpy (p, verstr, verlen + 1);
6375 newh = elf_link_hash_lookup (elf_hash_table (info),
6376 newname, FALSE, FALSE,
6377 FALSE);
6378 if (newh == NULL
6379 || (newh->root.type != bfd_link_hash_defined
6380 && newh->root.type != bfd_link_hash_defweak))
6381 {
6382 /* Check the default versioned definition. */
6383 *p++ = ELF_VER_CHR;
6384 memcpy (p, verstr, verlen + 1);
6385 newh = elf_link_hash_lookup (elf_hash_table (info),
6386 newname, FALSE, FALSE,
6387 FALSE);
6388 }
6389 free (newname);
6390
6391 /* Mark this version if there is a definition and it is
6392 not defined in a shared object. */
6393 if (newh != NULL
6394 && !newh->def_dynamic
6395 && (newh->root.type == bfd_link_hash_defined
6396 || newh->root.type == bfd_link_hash_defweak))
6397 d->symver = 1;
6398 }
6399
6400 /* Attach all the symbols to their version information. */
6401 asvinfo.info = info;
6402 asvinfo.failed = FALSE;
6403
6404 elf_link_hash_traverse (elf_hash_table (info),
6405 _bfd_elf_link_assign_sym_version,
6406 &asvinfo);
6407 if (asvinfo.failed)
6408 return FALSE;
6409
6410 if (!info->allow_undefined_version)
6411 {
6412 /* Check if all global versions have a definition. */
6413 bfd_boolean all_defined = TRUE;
6414 for (t = info->version_info; t != NULL; t = t->next)
6415 for (d = t->globals.list; d != NULL; d = d->next)
6416 if (d->literal && !d->symver && !d->script)
6417 {
6418 _bfd_error_handler
6419 (_("%s: undefined version: %s"),
6420 d->pattern, t->name);
6421 all_defined = FALSE;
6422 }
6423
6424 if (!all_defined)
6425 {
6426 bfd_set_error (bfd_error_bad_value);
6427 return FALSE;
6428 }
6429 }
6430
6431 /* Set up the version definition section. */
6432 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6433 BFD_ASSERT (s != NULL);
6434
6435 /* We may have created additional version definitions if we are
6436 just linking a regular application. */
6437 verdefs = info->version_info;
6438
6439 /* Skip anonymous version tag. */
6440 if (verdefs != NULL && verdefs->vernum == 0)
6441 verdefs = verdefs->next;
6442
6443 if (verdefs == NULL && !info->create_default_symver)
6444 s->flags |= SEC_EXCLUDE;
6445 else
6446 {
6447 unsigned int cdefs;
6448 bfd_size_type size;
6449 bfd_byte *p;
6450 Elf_Internal_Verdef def;
6451 Elf_Internal_Verdaux defaux;
6452 struct bfd_link_hash_entry *bh;
6453 struct elf_link_hash_entry *h;
6454 const char *name;
6455
6456 cdefs = 0;
6457 size = 0;
6458
6459 /* Make space for the base version. */
6460 size += sizeof (Elf_External_Verdef);
6461 size += sizeof (Elf_External_Verdaux);
6462 ++cdefs;
6463
6464 /* Make space for the default version. */
6465 if (info->create_default_symver)
6466 {
6467 size += sizeof (Elf_External_Verdef);
6468 ++cdefs;
6469 }
6470
6471 for (t = verdefs; t != NULL; t = t->next)
6472 {
6473 struct bfd_elf_version_deps *n;
6474
6475 /* Don't emit base version twice. */
6476 if (t->vernum == 0)
6477 continue;
6478
6479 size += sizeof (Elf_External_Verdef);
6480 size += sizeof (Elf_External_Verdaux);
6481 ++cdefs;
6482
6483 for (n = t->deps; n != NULL; n = n->next)
6484 size += sizeof (Elf_External_Verdaux);
6485 }
6486
6487 s->size = size;
6488 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6489 if (s->contents == NULL && s->size != 0)
6490 return FALSE;
6491
6492 /* Fill in the version definition section. */
6493
6494 p = s->contents;
6495
6496 def.vd_version = VER_DEF_CURRENT;
6497 def.vd_flags = VER_FLG_BASE;
6498 def.vd_ndx = 1;
6499 def.vd_cnt = 1;
6500 if (info->create_default_symver)
6501 {
6502 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6503 def.vd_next = sizeof (Elf_External_Verdef);
6504 }
6505 else
6506 {
6507 def.vd_aux = sizeof (Elf_External_Verdef);
6508 def.vd_next = (sizeof (Elf_External_Verdef)
6509 + sizeof (Elf_External_Verdaux));
6510 }
6511
6512 if (soname_indx != (size_t) -1)
6513 {
6514 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6515 soname_indx);
6516 def.vd_hash = bfd_elf_hash (soname);
6517 defaux.vda_name = soname_indx;
6518 name = soname;
6519 }
6520 else
6521 {
6522 size_t indx;
6523
6524 name = lbasename (output_bfd->filename);
6525 def.vd_hash = bfd_elf_hash (name);
6526 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6527 name, FALSE);
6528 if (indx == (size_t) -1)
6529 return FALSE;
6530 defaux.vda_name = indx;
6531 }
6532 defaux.vda_next = 0;
6533
6534 _bfd_elf_swap_verdef_out (output_bfd, &def,
6535 (Elf_External_Verdef *) p);
6536 p += sizeof (Elf_External_Verdef);
6537 if (info->create_default_symver)
6538 {
6539 /* Add a symbol representing this version. */
6540 bh = NULL;
6541 if (! (_bfd_generic_link_add_one_symbol
6542 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6543 0, NULL, FALSE,
6544 get_elf_backend_data (dynobj)->collect, &bh)))
6545 return FALSE;
6546 h = (struct elf_link_hash_entry *) bh;
6547 h->non_elf = 0;
6548 h->def_regular = 1;
6549 h->type = STT_OBJECT;
6550 h->verinfo.vertree = NULL;
6551
6552 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6553 return FALSE;
6554
6555 /* Create a duplicate of the base version with the same
6556 aux block, but different flags. */
6557 def.vd_flags = 0;
6558 def.vd_ndx = 2;
6559 def.vd_aux = sizeof (Elf_External_Verdef);
6560 if (verdefs)
6561 def.vd_next = (sizeof (Elf_External_Verdef)
6562 + sizeof (Elf_External_Verdaux));
6563 else
6564 def.vd_next = 0;
6565 _bfd_elf_swap_verdef_out (output_bfd, &def,
6566 (Elf_External_Verdef *) p);
6567 p += sizeof (Elf_External_Verdef);
6568 }
6569 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6570 (Elf_External_Verdaux *) p);
6571 p += sizeof (Elf_External_Verdaux);
6572
6573 for (t = verdefs; t != NULL; t = t->next)
6574 {
6575 unsigned int cdeps;
6576 struct bfd_elf_version_deps *n;
6577
6578 /* Don't emit the base version twice. */
6579 if (t->vernum == 0)
6580 continue;
6581
6582 cdeps = 0;
6583 for (n = t->deps; n != NULL; n = n->next)
6584 ++cdeps;
6585
6586 /* Add a symbol representing this version. */
6587 bh = NULL;
6588 if (! (_bfd_generic_link_add_one_symbol
6589 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6590 0, NULL, FALSE,
6591 get_elf_backend_data (dynobj)->collect, &bh)))
6592 return FALSE;
6593 h = (struct elf_link_hash_entry *) bh;
6594 h->non_elf = 0;
6595 h->def_regular = 1;
6596 h->type = STT_OBJECT;
6597 h->verinfo.vertree = t;
6598
6599 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6600 return FALSE;
6601
6602 def.vd_version = VER_DEF_CURRENT;
6603 def.vd_flags = 0;
6604 if (t->globals.list == NULL
6605 && t->locals.list == NULL
6606 && ! t->used)
6607 def.vd_flags |= VER_FLG_WEAK;
6608 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6609 def.vd_cnt = cdeps + 1;
6610 def.vd_hash = bfd_elf_hash (t->name);
6611 def.vd_aux = sizeof (Elf_External_Verdef);
6612 def.vd_next = 0;
6613
6614 /* If a basever node is next, it *must* be the last node in
6615 the chain, otherwise Verdef construction breaks. */
6616 if (t->next != NULL && t->next->vernum == 0)
6617 BFD_ASSERT (t->next->next == NULL);
6618
6619 if (t->next != NULL && t->next->vernum != 0)
6620 def.vd_next = (sizeof (Elf_External_Verdef)
6621 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6622
6623 _bfd_elf_swap_verdef_out (output_bfd, &def,
6624 (Elf_External_Verdef *) p);
6625 p += sizeof (Elf_External_Verdef);
6626
6627 defaux.vda_name = h->dynstr_index;
6628 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6629 h->dynstr_index);
6630 defaux.vda_next = 0;
6631 if (t->deps != NULL)
6632 defaux.vda_next = sizeof (Elf_External_Verdaux);
6633 t->name_indx = defaux.vda_name;
6634
6635 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6636 (Elf_External_Verdaux *) p);
6637 p += sizeof (Elf_External_Verdaux);
6638
6639 for (n = t->deps; n != NULL; n = n->next)
6640 {
6641 if (n->version_needed == NULL)
6642 {
6643 /* This can happen if there was an error in the
6644 version script. */
6645 defaux.vda_name = 0;
6646 }
6647 else
6648 {
6649 defaux.vda_name = n->version_needed->name_indx;
6650 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6651 defaux.vda_name);
6652 }
6653 if (n->next == NULL)
6654 defaux.vda_next = 0;
6655 else
6656 defaux.vda_next = sizeof (Elf_External_Verdaux);
6657
6658 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6659 (Elf_External_Verdaux *) p);
6660 p += sizeof (Elf_External_Verdaux);
6661 }
6662 }
6663
6664 elf_tdata (output_bfd)->cverdefs = cdefs;
6665 }
6666 }
6667
6668 bed = get_elf_backend_data (output_bfd);
6669
6670 if (info->gc_sections && bed->can_gc_sections)
6671 {
6672 struct elf_gc_sweep_symbol_info sweep_info;
6673
6674 /* Remove the symbols that were in the swept sections from the
6675 dynamic symbol table. */
6676 sweep_info.info = info;
6677 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6678 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6679 &sweep_info);
6680 }
6681
6682 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6683 {
6684 asection *s;
6685 struct elf_find_verdep_info sinfo;
6686
6687 /* Work out the size of the version reference section. */
6688
6689 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6690 BFD_ASSERT (s != NULL);
6691
6692 sinfo.info = info;
6693 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6694 if (sinfo.vers == 0)
6695 sinfo.vers = 1;
6696 sinfo.failed = FALSE;
6697
6698 elf_link_hash_traverse (elf_hash_table (info),
6699 _bfd_elf_link_find_version_dependencies,
6700 &sinfo);
6701 if (sinfo.failed)
6702 return FALSE;
6703
6704 if (elf_tdata (output_bfd)->verref == NULL)
6705 s->flags |= SEC_EXCLUDE;
6706 else
6707 {
6708 Elf_Internal_Verneed *vn;
6709 unsigned int size;
6710 unsigned int crefs;
6711 bfd_byte *p;
6712
6713 /* Build the version dependency section. */
6714 size = 0;
6715 crefs = 0;
6716 for (vn = elf_tdata (output_bfd)->verref;
6717 vn != NULL;
6718 vn = vn->vn_nextref)
6719 {
6720 Elf_Internal_Vernaux *a;
6721
6722 size += sizeof (Elf_External_Verneed);
6723 ++crefs;
6724 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6725 size += sizeof (Elf_External_Vernaux);
6726 }
6727
6728 s->size = size;
6729 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6730 if (s->contents == NULL)
6731 return FALSE;
6732
6733 p = s->contents;
6734 for (vn = elf_tdata (output_bfd)->verref;
6735 vn != NULL;
6736 vn = vn->vn_nextref)
6737 {
6738 unsigned int caux;
6739 Elf_Internal_Vernaux *a;
6740 size_t indx;
6741
6742 caux = 0;
6743 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6744 ++caux;
6745
6746 vn->vn_version = VER_NEED_CURRENT;
6747 vn->vn_cnt = caux;
6748 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6749 elf_dt_name (vn->vn_bfd) != NULL
6750 ? elf_dt_name (vn->vn_bfd)
6751 : lbasename (vn->vn_bfd->filename),
6752 FALSE);
6753 if (indx == (size_t) -1)
6754 return FALSE;
6755 vn->vn_file = indx;
6756 vn->vn_aux = sizeof (Elf_External_Verneed);
6757 if (vn->vn_nextref == NULL)
6758 vn->vn_next = 0;
6759 else
6760 vn->vn_next = (sizeof (Elf_External_Verneed)
6761 + caux * sizeof (Elf_External_Vernaux));
6762
6763 _bfd_elf_swap_verneed_out (output_bfd, vn,
6764 (Elf_External_Verneed *) p);
6765 p += sizeof (Elf_External_Verneed);
6766
6767 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6768 {
6769 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6770 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6771 a->vna_nodename, FALSE);
6772 if (indx == (size_t) -1)
6773 return FALSE;
6774 a->vna_name = indx;
6775 if (a->vna_nextptr == NULL)
6776 a->vna_next = 0;
6777 else
6778 a->vna_next = sizeof (Elf_External_Vernaux);
6779
6780 _bfd_elf_swap_vernaux_out (output_bfd, a,
6781 (Elf_External_Vernaux *) p);
6782 p += sizeof (Elf_External_Vernaux);
6783 }
6784 }
6785
6786 elf_tdata (output_bfd)->cverrefs = crefs;
6787 }
6788 }
6789
6790 /* Any syms created from now on start with -1 in
6791 got.refcount/offset and plt.refcount/offset. */
6792 elf_hash_table (info)->init_got_refcount
6793 = elf_hash_table (info)->init_got_offset;
6794 elf_hash_table (info)->init_plt_refcount
6795 = elf_hash_table (info)->init_plt_offset;
6796
6797 if (bfd_link_relocatable (info)
6798 && !_bfd_elf_size_group_sections (info))
6799 return FALSE;
6800
6801 /* The backend may have to create some sections regardless of whether
6802 we're dynamic or not. */
6803 if (bed->elf_backend_always_size_sections
6804 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6805 return FALSE;
6806
6807 /* Determine any GNU_STACK segment requirements, after the backend
6808 has had a chance to set a default segment size. */
6809 if (info->execstack)
6810 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6811 else if (info->noexecstack)
6812 elf_stack_flags (output_bfd) = PF_R | PF_W;
6813 else
6814 {
6815 bfd *inputobj;
6816 asection *notesec = NULL;
6817 int exec = 0;
6818
6819 for (inputobj = info->input_bfds;
6820 inputobj;
6821 inputobj = inputobj->link.next)
6822 {
6823 asection *s;
6824
6825 if (inputobj->flags
6826 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6827 continue;
6828 s = inputobj->sections;
6829 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6830 continue;
6831
6832 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6833 if (s)
6834 {
6835 if (s->flags & SEC_CODE)
6836 exec = PF_X;
6837 notesec = s;
6838 }
6839 else if (bed->default_execstack)
6840 exec = PF_X;
6841 }
6842 if (notesec || info->stacksize > 0)
6843 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6844 if (notesec && exec && bfd_link_relocatable (info)
6845 && notesec->output_section != bfd_abs_section_ptr)
6846 notesec->output_section->flags |= SEC_CODE;
6847 }
6848
6849 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6850 {
6851 struct elf_info_failed eif;
6852 struct elf_link_hash_entry *h;
6853 asection *dynstr;
6854 asection *s;
6855
6856 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6857 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6858
6859 if (info->symbolic)
6860 {
6861 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6862 return FALSE;
6863 info->flags |= DF_SYMBOLIC;
6864 }
6865
6866 if (rpath != NULL)
6867 {
6868 size_t indx;
6869 bfd_vma tag;
6870
6871 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6872 TRUE);
6873 if (indx == (size_t) -1)
6874 return FALSE;
6875
6876 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6877 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6878 return FALSE;
6879 }
6880
6881 if (filter_shlib != NULL)
6882 {
6883 size_t indx;
6884
6885 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6886 filter_shlib, TRUE);
6887 if (indx == (size_t) -1
6888 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6889 return FALSE;
6890 }
6891
6892 if (auxiliary_filters != NULL)
6893 {
6894 const char * const *p;
6895
6896 for (p = auxiliary_filters; *p != NULL; p++)
6897 {
6898 size_t indx;
6899
6900 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6901 *p, TRUE);
6902 if (indx == (size_t) -1
6903 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6904 return FALSE;
6905 }
6906 }
6907
6908 if (audit != NULL)
6909 {
6910 size_t indx;
6911
6912 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6913 TRUE);
6914 if (indx == (size_t) -1
6915 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6916 return FALSE;
6917 }
6918
6919 if (depaudit != NULL)
6920 {
6921 size_t indx;
6922
6923 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6924 TRUE);
6925 if (indx == (size_t) -1
6926 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6927 return FALSE;
6928 }
6929
6930 eif.info = info;
6931 eif.failed = FALSE;
6932
6933 /* Find all symbols which were defined in a dynamic object and make
6934 the backend pick a reasonable value for them. */
6935 elf_link_hash_traverse (elf_hash_table (info),
6936 _bfd_elf_adjust_dynamic_symbol,
6937 &eif);
6938 if (eif.failed)
6939 return FALSE;
6940
6941 /* Add some entries to the .dynamic section. We fill in some of the
6942 values later, in bfd_elf_final_link, but we must add the entries
6943 now so that we know the final size of the .dynamic section. */
6944
6945 /* If there are initialization and/or finalization functions to
6946 call then add the corresponding DT_INIT/DT_FINI entries. */
6947 h = (info->init_function
6948 ? elf_link_hash_lookup (elf_hash_table (info),
6949 info->init_function, FALSE,
6950 FALSE, FALSE)
6951 : NULL);
6952 if (h != NULL
6953 && (h->ref_regular
6954 || h->def_regular))
6955 {
6956 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6957 return FALSE;
6958 }
6959 h = (info->fini_function
6960 ? elf_link_hash_lookup (elf_hash_table (info),
6961 info->fini_function, FALSE,
6962 FALSE, FALSE)
6963 : NULL);
6964 if (h != NULL
6965 && (h->ref_regular
6966 || h->def_regular))
6967 {
6968 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6969 return FALSE;
6970 }
6971
6972 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6973 if (s != NULL && s->linker_has_input)
6974 {
6975 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6976 if (! bfd_link_executable (info))
6977 {
6978 bfd *sub;
6979 asection *o;
6980
6981 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6982 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6983 && (o = sub->sections) != NULL
6984 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6985 for (o = sub->sections; o != NULL; o = o->next)
6986 if (elf_section_data (o)->this_hdr.sh_type
6987 == SHT_PREINIT_ARRAY)
6988 {
6989 _bfd_error_handler
6990 (_("%pB: .preinit_array section is not allowed in DSO"),
6991 sub);
6992 break;
6993 }
6994
6995 bfd_set_error (bfd_error_nonrepresentable_section);
6996 return FALSE;
6997 }
6998
6999 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7000 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7001 return FALSE;
7002 }
7003 s = bfd_get_section_by_name (output_bfd, ".init_array");
7004 if (s != NULL && s->linker_has_input)
7005 {
7006 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7007 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7008 return FALSE;
7009 }
7010 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7011 if (s != NULL && s->linker_has_input)
7012 {
7013 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7014 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7015 return FALSE;
7016 }
7017
7018 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7019 /* If .dynstr is excluded from the link, we don't want any of
7020 these tags. Strictly, we should be checking each section
7021 individually; This quick check covers for the case where
7022 someone does a /DISCARD/ : { *(*) }. */
7023 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7024 {
7025 bfd_size_type strsize;
7026
7027 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7028 if ((info->emit_hash
7029 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7030 || (info->emit_gnu_hash
7031 && (bed->record_xhash_symbol == NULL
7032 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7033 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7034 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7035 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7036 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7037 bed->s->sizeof_sym))
7038 return FALSE;
7039 }
7040 }
7041
7042 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7043 return FALSE;
7044
7045 /* The backend must work out the sizes of all the other dynamic
7046 sections. */
7047 if (dynobj != NULL
7048 && bed->elf_backend_size_dynamic_sections != NULL
7049 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7050 return FALSE;
7051
7052 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7053 {
7054 if (elf_tdata (output_bfd)->cverdefs)
7055 {
7056 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7057
7058 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7059 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7060 return FALSE;
7061 }
7062
7063 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7064 {
7065 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7066 return FALSE;
7067 }
7068 else if (info->flags & DF_BIND_NOW)
7069 {
7070 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7071 return FALSE;
7072 }
7073
7074 if (info->flags_1)
7075 {
7076 if (bfd_link_executable (info))
7077 info->flags_1 &= ~ (DF_1_INITFIRST
7078 | DF_1_NODELETE
7079 | DF_1_NOOPEN);
7080 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7081 return FALSE;
7082 }
7083
7084 if (elf_tdata (output_bfd)->cverrefs)
7085 {
7086 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7087
7088 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7089 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7090 return FALSE;
7091 }
7092
7093 if ((elf_tdata (output_bfd)->cverrefs == 0
7094 && elf_tdata (output_bfd)->cverdefs == 0)
7095 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7096 {
7097 asection *s;
7098
7099 s = bfd_get_linker_section (dynobj, ".gnu.version");
7100 s->flags |= SEC_EXCLUDE;
7101 }
7102 }
7103 return TRUE;
7104 }
7105
7106 /* Find the first non-excluded output section. We'll use its
7107 section symbol for some emitted relocs. */
7108 void
7109 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7110 {
7111 asection *s;
7112 asection *found = NULL;
7113
7114 for (s = output_bfd->sections; s != NULL; s = s->next)
7115 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7116 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7117 {
7118 found = s;
7119 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7120 break;
7121 }
7122 elf_hash_table (info)->text_index_section = found;
7123 }
7124
7125 /* Find two non-excluded output sections, one for code, one for data.
7126 We'll use their section symbols for some emitted relocs. */
7127 void
7128 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7129 {
7130 asection *s;
7131 asection *found = NULL;
7132
7133 /* Data first, since setting text_index_section changes
7134 _bfd_elf_omit_section_dynsym_default. */
7135 for (s = output_bfd->sections; s != NULL; s = s->next)
7136 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7137 && !(s->flags & SEC_READONLY)
7138 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7139 {
7140 found = s;
7141 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7142 break;
7143 }
7144 elf_hash_table (info)->data_index_section = found;
7145
7146 for (s = output_bfd->sections; s != NULL; s = s->next)
7147 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7148 && (s->flags & SEC_READONLY)
7149 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7150 {
7151 found = s;
7152 break;
7153 }
7154 elf_hash_table (info)->text_index_section = found;
7155 }
7156
7157 #define GNU_HASH_SECTION_NAME(bed) \
7158 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7159
7160 bfd_boolean
7161 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7162 {
7163 const struct elf_backend_data *bed;
7164 unsigned long section_sym_count;
7165 bfd_size_type dynsymcount = 0;
7166
7167 if (!is_elf_hash_table (info->hash))
7168 return TRUE;
7169
7170 bed = get_elf_backend_data (output_bfd);
7171 (*bed->elf_backend_init_index_section) (output_bfd, info);
7172
7173 /* Assign dynsym indices. In a shared library we generate a section
7174 symbol for each output section, which come first. Next come all
7175 of the back-end allocated local dynamic syms, followed by the rest
7176 of the global symbols.
7177
7178 This is usually not needed for static binaries, however backends
7179 can request to always do it, e.g. the MIPS backend uses dynamic
7180 symbol counts to lay out GOT, which will be produced in the
7181 presence of GOT relocations even in static binaries (holding fixed
7182 data in that case, to satisfy those relocations). */
7183
7184 if (elf_hash_table (info)->dynamic_sections_created
7185 || bed->always_renumber_dynsyms)
7186 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7187 &section_sym_count);
7188
7189 if (elf_hash_table (info)->dynamic_sections_created)
7190 {
7191 bfd *dynobj;
7192 asection *s;
7193 unsigned int dtagcount;
7194
7195 dynobj = elf_hash_table (info)->dynobj;
7196
7197 /* Work out the size of the symbol version section. */
7198 s = bfd_get_linker_section (dynobj, ".gnu.version");
7199 BFD_ASSERT (s != NULL);
7200 if ((s->flags & SEC_EXCLUDE) == 0)
7201 {
7202 s->size = dynsymcount * sizeof (Elf_External_Versym);
7203 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7204 if (s->contents == NULL)
7205 return FALSE;
7206
7207 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7208 return FALSE;
7209 }
7210
7211 /* Set the size of the .dynsym and .hash sections. We counted
7212 the number of dynamic symbols in elf_link_add_object_symbols.
7213 We will build the contents of .dynsym and .hash when we build
7214 the final symbol table, because until then we do not know the
7215 correct value to give the symbols. We built the .dynstr
7216 section as we went along in elf_link_add_object_symbols. */
7217 s = elf_hash_table (info)->dynsym;
7218 BFD_ASSERT (s != NULL);
7219 s->size = dynsymcount * bed->s->sizeof_sym;
7220
7221 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7222 if (s->contents == NULL)
7223 return FALSE;
7224
7225 /* The first entry in .dynsym is a dummy symbol. Clear all the
7226 section syms, in case we don't output them all. */
7227 ++section_sym_count;
7228 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7229
7230 elf_hash_table (info)->bucketcount = 0;
7231
7232 /* Compute the size of the hashing table. As a side effect this
7233 computes the hash values for all the names we export. */
7234 if (info->emit_hash)
7235 {
7236 unsigned long int *hashcodes;
7237 struct hash_codes_info hashinf;
7238 bfd_size_type amt;
7239 unsigned long int nsyms;
7240 size_t bucketcount;
7241 size_t hash_entry_size;
7242
7243 /* Compute the hash values for all exported symbols. At the same
7244 time store the values in an array so that we could use them for
7245 optimizations. */
7246 amt = dynsymcount * sizeof (unsigned long int);
7247 hashcodes = (unsigned long int *) bfd_malloc (amt);
7248 if (hashcodes == NULL)
7249 return FALSE;
7250 hashinf.hashcodes = hashcodes;
7251 hashinf.error = FALSE;
7252
7253 /* Put all hash values in HASHCODES. */
7254 elf_link_hash_traverse (elf_hash_table (info),
7255 elf_collect_hash_codes, &hashinf);
7256 if (hashinf.error)
7257 {
7258 free (hashcodes);
7259 return FALSE;
7260 }
7261
7262 nsyms = hashinf.hashcodes - hashcodes;
7263 bucketcount
7264 = compute_bucket_count (info, hashcodes, nsyms, 0);
7265 free (hashcodes);
7266
7267 if (bucketcount == 0 && nsyms > 0)
7268 return FALSE;
7269
7270 elf_hash_table (info)->bucketcount = bucketcount;
7271
7272 s = bfd_get_linker_section (dynobj, ".hash");
7273 BFD_ASSERT (s != NULL);
7274 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7275 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7276 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7277 if (s->contents == NULL)
7278 return FALSE;
7279
7280 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7281 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7282 s->contents + hash_entry_size);
7283 }
7284
7285 if (info->emit_gnu_hash)
7286 {
7287 size_t i, cnt;
7288 unsigned char *contents;
7289 struct collect_gnu_hash_codes cinfo;
7290 bfd_size_type amt;
7291 size_t bucketcount;
7292
7293 memset (&cinfo, 0, sizeof (cinfo));
7294
7295 /* Compute the hash values for all exported symbols. At the same
7296 time store the values in an array so that we could use them for
7297 optimizations. */
7298 amt = dynsymcount * 2 * sizeof (unsigned long int);
7299 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7300 if (cinfo.hashcodes == NULL)
7301 return FALSE;
7302
7303 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7304 cinfo.min_dynindx = -1;
7305 cinfo.output_bfd = output_bfd;
7306 cinfo.bed = bed;
7307
7308 /* Put all hash values in HASHCODES. */
7309 elf_link_hash_traverse (elf_hash_table (info),
7310 elf_collect_gnu_hash_codes, &cinfo);
7311 if (cinfo.error)
7312 {
7313 free (cinfo.hashcodes);
7314 return FALSE;
7315 }
7316
7317 bucketcount
7318 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7319
7320 if (bucketcount == 0)
7321 {
7322 free (cinfo.hashcodes);
7323 return FALSE;
7324 }
7325
7326 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7327 BFD_ASSERT (s != NULL);
7328
7329 if (cinfo.nsyms == 0)
7330 {
7331 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7332 BFD_ASSERT (cinfo.min_dynindx == -1);
7333 free (cinfo.hashcodes);
7334 s->size = 5 * 4 + bed->s->arch_size / 8;
7335 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7336 if (contents == NULL)
7337 return FALSE;
7338 s->contents = contents;
7339 /* 1 empty bucket. */
7340 bfd_put_32 (output_bfd, 1, contents);
7341 /* SYMIDX above the special symbol 0. */
7342 bfd_put_32 (output_bfd, 1, contents + 4);
7343 /* Just one word for bitmask. */
7344 bfd_put_32 (output_bfd, 1, contents + 8);
7345 /* Only hash fn bloom filter. */
7346 bfd_put_32 (output_bfd, 0, contents + 12);
7347 /* No hashes are valid - empty bitmask. */
7348 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7349 /* No hashes in the only bucket. */
7350 bfd_put_32 (output_bfd, 0,
7351 contents + 16 + bed->s->arch_size / 8);
7352 }
7353 else
7354 {
7355 unsigned long int maskwords, maskbitslog2, x;
7356 BFD_ASSERT (cinfo.min_dynindx != -1);
7357
7358 x = cinfo.nsyms;
7359 maskbitslog2 = 1;
7360 while ((x >>= 1) != 0)
7361 ++maskbitslog2;
7362 if (maskbitslog2 < 3)
7363 maskbitslog2 = 5;
7364 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7365 maskbitslog2 = maskbitslog2 + 3;
7366 else
7367 maskbitslog2 = maskbitslog2 + 2;
7368 if (bed->s->arch_size == 64)
7369 {
7370 if (maskbitslog2 == 5)
7371 maskbitslog2 = 6;
7372 cinfo.shift1 = 6;
7373 }
7374 else
7375 cinfo.shift1 = 5;
7376 cinfo.mask = (1 << cinfo.shift1) - 1;
7377 cinfo.shift2 = maskbitslog2;
7378 cinfo.maskbits = 1 << maskbitslog2;
7379 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7380 amt = bucketcount * sizeof (unsigned long int) * 2;
7381 amt += maskwords * sizeof (bfd_vma);
7382 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7383 if (cinfo.bitmask == NULL)
7384 {
7385 free (cinfo.hashcodes);
7386 return FALSE;
7387 }
7388
7389 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7390 cinfo.indx = cinfo.counts + bucketcount;
7391 cinfo.symindx = dynsymcount - cinfo.nsyms;
7392 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7393
7394 /* Determine how often each hash bucket is used. */
7395 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7396 for (i = 0; i < cinfo.nsyms; ++i)
7397 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7398
7399 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7400 if (cinfo.counts[i] != 0)
7401 {
7402 cinfo.indx[i] = cnt;
7403 cnt += cinfo.counts[i];
7404 }
7405 BFD_ASSERT (cnt == dynsymcount);
7406 cinfo.bucketcount = bucketcount;
7407 cinfo.local_indx = cinfo.min_dynindx;
7408
7409 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7410 s->size += cinfo.maskbits / 8;
7411 if (bed->record_xhash_symbol != NULL)
7412 s->size += cinfo.nsyms * 4;
7413 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7414 if (contents == NULL)
7415 {
7416 free (cinfo.bitmask);
7417 free (cinfo.hashcodes);
7418 return FALSE;
7419 }
7420
7421 s->contents = contents;
7422 bfd_put_32 (output_bfd, bucketcount, contents);
7423 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7424 bfd_put_32 (output_bfd, maskwords, contents + 8);
7425 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7426 contents += 16 + cinfo.maskbits / 8;
7427
7428 for (i = 0; i < bucketcount; ++i)
7429 {
7430 if (cinfo.counts[i] == 0)
7431 bfd_put_32 (output_bfd, 0, contents);
7432 else
7433 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7434 contents += 4;
7435 }
7436
7437 cinfo.contents = contents;
7438
7439 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7440 /* Renumber dynamic symbols, if populating .gnu.hash section.
7441 If using .MIPS.xhash, populate the translation table. */
7442 elf_link_hash_traverse (elf_hash_table (info),
7443 elf_gnu_hash_process_symidx, &cinfo);
7444
7445 contents = s->contents + 16;
7446 for (i = 0; i < maskwords; ++i)
7447 {
7448 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7449 contents);
7450 contents += bed->s->arch_size / 8;
7451 }
7452
7453 free (cinfo.bitmask);
7454 free (cinfo.hashcodes);
7455 }
7456 }
7457
7458 s = bfd_get_linker_section (dynobj, ".dynstr");
7459 BFD_ASSERT (s != NULL);
7460
7461 elf_finalize_dynstr (output_bfd, info);
7462
7463 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7464
7465 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7466 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7467 return FALSE;
7468 }
7469
7470 return TRUE;
7471 }
7472 \f
7473 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7474
7475 static void
7476 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7477 asection *sec)
7478 {
7479 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7480 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7481 }
7482
7483 /* Finish SHF_MERGE section merging. */
7484
7485 bfd_boolean
7486 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7487 {
7488 bfd *ibfd;
7489 asection *sec;
7490
7491 if (!is_elf_hash_table (info->hash))
7492 return FALSE;
7493
7494 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7495 if ((ibfd->flags & DYNAMIC) == 0
7496 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7497 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7498 == get_elf_backend_data (obfd)->s->elfclass))
7499 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7500 if ((sec->flags & SEC_MERGE) != 0
7501 && !bfd_is_abs_section (sec->output_section))
7502 {
7503 struct bfd_elf_section_data *secdata;
7504
7505 secdata = elf_section_data (sec);
7506 if (! _bfd_add_merge_section (obfd,
7507 &elf_hash_table (info)->merge_info,
7508 sec, &secdata->sec_info))
7509 return FALSE;
7510 else if (secdata->sec_info)
7511 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7512 }
7513
7514 if (elf_hash_table (info)->merge_info != NULL)
7515 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7516 merge_sections_remove_hook);
7517 return TRUE;
7518 }
7519
7520 /* Create an entry in an ELF linker hash table. */
7521
7522 struct bfd_hash_entry *
7523 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7524 struct bfd_hash_table *table,
7525 const char *string)
7526 {
7527 /* Allocate the structure if it has not already been allocated by a
7528 subclass. */
7529 if (entry == NULL)
7530 {
7531 entry = (struct bfd_hash_entry *)
7532 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7533 if (entry == NULL)
7534 return entry;
7535 }
7536
7537 /* Call the allocation method of the superclass. */
7538 entry = _bfd_link_hash_newfunc (entry, table, string);
7539 if (entry != NULL)
7540 {
7541 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7542 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7543
7544 /* Set local fields. */
7545 ret->indx = -1;
7546 ret->dynindx = -1;
7547 ret->got = htab->init_got_refcount;
7548 ret->plt = htab->init_plt_refcount;
7549 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7550 - offsetof (struct elf_link_hash_entry, size)));
7551 /* Assume that we have been called by a non-ELF symbol reader.
7552 This flag is then reset by the code which reads an ELF input
7553 file. This ensures that a symbol created by a non-ELF symbol
7554 reader will have the flag set correctly. */
7555 ret->non_elf = 1;
7556 }
7557
7558 return entry;
7559 }
7560
7561 /* Copy data from an indirect symbol to its direct symbol, hiding the
7562 old indirect symbol. Also used for copying flags to a weakdef. */
7563
7564 void
7565 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7566 struct elf_link_hash_entry *dir,
7567 struct elf_link_hash_entry *ind)
7568 {
7569 struct elf_link_hash_table *htab;
7570
7571 /* Copy down any references that we may have already seen to the
7572 symbol which just became indirect. */
7573
7574 if (dir->versioned != versioned_hidden)
7575 dir->ref_dynamic |= ind->ref_dynamic;
7576 dir->ref_regular |= ind->ref_regular;
7577 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7578 dir->non_got_ref |= ind->non_got_ref;
7579 dir->needs_plt |= ind->needs_plt;
7580 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7581
7582 if (ind->root.type != bfd_link_hash_indirect)
7583 return;
7584
7585 /* Copy over the global and procedure linkage table refcount entries.
7586 These may have been already set up by a check_relocs routine. */
7587 htab = elf_hash_table (info);
7588 if (ind->got.refcount > htab->init_got_refcount.refcount)
7589 {
7590 if (dir->got.refcount < 0)
7591 dir->got.refcount = 0;
7592 dir->got.refcount += ind->got.refcount;
7593 ind->got.refcount = htab->init_got_refcount.refcount;
7594 }
7595
7596 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7597 {
7598 if (dir->plt.refcount < 0)
7599 dir->plt.refcount = 0;
7600 dir->plt.refcount += ind->plt.refcount;
7601 ind->plt.refcount = htab->init_plt_refcount.refcount;
7602 }
7603
7604 if (ind->dynindx != -1)
7605 {
7606 if (dir->dynindx != -1)
7607 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7608 dir->dynindx = ind->dynindx;
7609 dir->dynstr_index = ind->dynstr_index;
7610 ind->dynindx = -1;
7611 ind->dynstr_index = 0;
7612 }
7613 }
7614
7615 void
7616 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7617 struct elf_link_hash_entry *h,
7618 bfd_boolean force_local)
7619 {
7620 /* STT_GNU_IFUNC symbol must go through PLT. */
7621 if (h->type != STT_GNU_IFUNC)
7622 {
7623 h->plt = elf_hash_table (info)->init_plt_offset;
7624 h->needs_plt = 0;
7625 }
7626 if (force_local)
7627 {
7628 h->forced_local = 1;
7629 if (h->dynindx != -1)
7630 {
7631 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7632 h->dynstr_index);
7633 h->dynindx = -1;
7634 h->dynstr_index = 0;
7635 }
7636 }
7637 }
7638
7639 /* Hide a symbol. */
7640
7641 void
7642 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7643 struct bfd_link_info *info,
7644 struct bfd_link_hash_entry *h)
7645 {
7646 if (is_elf_hash_table (info->hash))
7647 {
7648 const struct elf_backend_data *bed
7649 = get_elf_backend_data (output_bfd);
7650 struct elf_link_hash_entry *eh
7651 = (struct elf_link_hash_entry *) h;
7652 bed->elf_backend_hide_symbol (info, eh, TRUE);
7653 eh->def_dynamic = 0;
7654 eh->ref_dynamic = 0;
7655 eh->dynamic_def = 0;
7656 }
7657 }
7658
7659 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7660 caller. */
7661
7662 bfd_boolean
7663 _bfd_elf_link_hash_table_init
7664 (struct elf_link_hash_table *table,
7665 bfd *abfd,
7666 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7667 struct bfd_hash_table *,
7668 const char *),
7669 unsigned int entsize,
7670 enum elf_target_id target_id)
7671 {
7672 bfd_boolean ret;
7673 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7674
7675 table->init_got_refcount.refcount = can_refcount - 1;
7676 table->init_plt_refcount.refcount = can_refcount - 1;
7677 table->init_got_offset.offset = -(bfd_vma) 1;
7678 table->init_plt_offset.offset = -(bfd_vma) 1;
7679 /* The first dynamic symbol is a dummy. */
7680 table->dynsymcount = 1;
7681
7682 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7683
7684 table->root.type = bfd_link_elf_hash_table;
7685 table->hash_table_id = target_id;
7686
7687 return ret;
7688 }
7689
7690 /* Create an ELF linker hash table. */
7691
7692 struct bfd_link_hash_table *
7693 _bfd_elf_link_hash_table_create (bfd *abfd)
7694 {
7695 struct elf_link_hash_table *ret;
7696 size_t amt = sizeof (struct elf_link_hash_table);
7697
7698 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7699 if (ret == NULL)
7700 return NULL;
7701
7702 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7703 sizeof (struct elf_link_hash_entry),
7704 GENERIC_ELF_DATA))
7705 {
7706 free (ret);
7707 return NULL;
7708 }
7709 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7710
7711 return &ret->root;
7712 }
7713
7714 /* Destroy an ELF linker hash table. */
7715
7716 void
7717 _bfd_elf_link_hash_table_free (bfd *obfd)
7718 {
7719 struct elf_link_hash_table *htab;
7720
7721 htab = (struct elf_link_hash_table *) obfd->link.hash;
7722 if (htab->dynstr != NULL)
7723 _bfd_elf_strtab_free (htab->dynstr);
7724 _bfd_merge_sections_free (htab->merge_info);
7725 _bfd_generic_link_hash_table_free (obfd);
7726 }
7727
7728 /* This is a hook for the ELF emulation code in the generic linker to
7729 tell the backend linker what file name to use for the DT_NEEDED
7730 entry for a dynamic object. */
7731
7732 void
7733 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7734 {
7735 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7736 && bfd_get_format (abfd) == bfd_object)
7737 elf_dt_name (abfd) = name;
7738 }
7739
7740 int
7741 bfd_elf_get_dyn_lib_class (bfd *abfd)
7742 {
7743 int lib_class;
7744 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7745 && bfd_get_format (abfd) == bfd_object)
7746 lib_class = elf_dyn_lib_class (abfd);
7747 else
7748 lib_class = 0;
7749 return lib_class;
7750 }
7751
7752 void
7753 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7754 {
7755 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7756 && bfd_get_format (abfd) == bfd_object)
7757 elf_dyn_lib_class (abfd) = lib_class;
7758 }
7759
7760 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7761 the linker ELF emulation code. */
7762
7763 struct bfd_link_needed_list *
7764 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7765 struct bfd_link_info *info)
7766 {
7767 if (! is_elf_hash_table (info->hash))
7768 return NULL;
7769 return elf_hash_table (info)->needed;
7770 }
7771
7772 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7773 hook for the linker ELF emulation code. */
7774
7775 struct bfd_link_needed_list *
7776 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7777 struct bfd_link_info *info)
7778 {
7779 if (! is_elf_hash_table (info->hash))
7780 return NULL;
7781 return elf_hash_table (info)->runpath;
7782 }
7783
7784 /* Get the name actually used for a dynamic object for a link. This
7785 is the SONAME entry if there is one. Otherwise, it is the string
7786 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7787
7788 const char *
7789 bfd_elf_get_dt_soname (bfd *abfd)
7790 {
7791 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7792 && bfd_get_format (abfd) == bfd_object)
7793 return elf_dt_name (abfd);
7794 return NULL;
7795 }
7796
7797 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7798 the ELF linker emulation code. */
7799
7800 bfd_boolean
7801 bfd_elf_get_bfd_needed_list (bfd *abfd,
7802 struct bfd_link_needed_list **pneeded)
7803 {
7804 asection *s;
7805 bfd_byte *dynbuf = NULL;
7806 unsigned int elfsec;
7807 unsigned long shlink;
7808 bfd_byte *extdyn, *extdynend;
7809 size_t extdynsize;
7810 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7811
7812 *pneeded = NULL;
7813
7814 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7815 || bfd_get_format (abfd) != bfd_object)
7816 return TRUE;
7817
7818 s = bfd_get_section_by_name (abfd, ".dynamic");
7819 if (s == NULL || s->size == 0)
7820 return TRUE;
7821
7822 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7823 goto error_return;
7824
7825 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7826 if (elfsec == SHN_BAD)
7827 goto error_return;
7828
7829 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7830
7831 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7832 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7833
7834 extdyn = dynbuf;
7835 extdynend = extdyn + s->size;
7836 for (; extdyn < extdynend; extdyn += extdynsize)
7837 {
7838 Elf_Internal_Dyn dyn;
7839
7840 (*swap_dyn_in) (abfd, extdyn, &dyn);
7841
7842 if (dyn.d_tag == DT_NULL)
7843 break;
7844
7845 if (dyn.d_tag == DT_NEEDED)
7846 {
7847 const char *string;
7848 struct bfd_link_needed_list *l;
7849 unsigned int tagv = dyn.d_un.d_val;
7850 size_t amt;
7851
7852 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7853 if (string == NULL)
7854 goto error_return;
7855
7856 amt = sizeof *l;
7857 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7858 if (l == NULL)
7859 goto error_return;
7860
7861 l->by = abfd;
7862 l->name = string;
7863 l->next = *pneeded;
7864 *pneeded = l;
7865 }
7866 }
7867
7868 free (dynbuf);
7869
7870 return TRUE;
7871
7872 error_return:
7873 if (dynbuf != NULL)
7874 free (dynbuf);
7875 return FALSE;
7876 }
7877
7878 struct elf_symbuf_symbol
7879 {
7880 unsigned long st_name; /* Symbol name, index in string tbl */
7881 unsigned char st_info; /* Type and binding attributes */
7882 unsigned char st_other; /* Visibilty, and target specific */
7883 };
7884
7885 struct elf_symbuf_head
7886 {
7887 struct elf_symbuf_symbol *ssym;
7888 size_t count;
7889 unsigned int st_shndx;
7890 };
7891
7892 struct elf_symbol
7893 {
7894 union
7895 {
7896 Elf_Internal_Sym *isym;
7897 struct elf_symbuf_symbol *ssym;
7898 void *p;
7899 } u;
7900 const char *name;
7901 };
7902
7903 /* Sort references to symbols by ascending section number. */
7904
7905 static int
7906 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7907 {
7908 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7909 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7910
7911 if (s1->st_shndx != s2->st_shndx)
7912 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7913 /* Final sort by the address of the sym in the symbuf ensures
7914 a stable sort. */
7915 if (s1 != s2)
7916 return s1 > s2 ? 1 : -1;
7917 return 0;
7918 }
7919
7920 static int
7921 elf_sym_name_compare (const void *arg1, const void *arg2)
7922 {
7923 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7924 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7925 int ret = strcmp (s1->name, s2->name);
7926 if (ret != 0)
7927 return ret;
7928 if (s1->u.p != s2->u.p)
7929 return s1->u.p > s2->u.p ? 1 : -1;
7930 return 0;
7931 }
7932
7933 static struct elf_symbuf_head *
7934 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7935 {
7936 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7937 struct elf_symbuf_symbol *ssym;
7938 struct elf_symbuf_head *ssymbuf, *ssymhead;
7939 size_t i, shndx_count, total_size, amt;
7940
7941 amt = symcount * sizeof (*indbuf);
7942 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
7943 if (indbuf == NULL)
7944 return NULL;
7945
7946 for (ind = indbuf, i = 0; i < symcount; i++)
7947 if (isymbuf[i].st_shndx != SHN_UNDEF)
7948 *ind++ = &isymbuf[i];
7949 indbufend = ind;
7950
7951 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7952 elf_sort_elf_symbol);
7953
7954 shndx_count = 0;
7955 if (indbufend > indbuf)
7956 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7957 if (ind[0]->st_shndx != ind[1]->st_shndx)
7958 shndx_count++;
7959
7960 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7961 + (indbufend - indbuf) * sizeof (*ssym));
7962 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7963 if (ssymbuf == NULL)
7964 {
7965 free (indbuf);
7966 return NULL;
7967 }
7968
7969 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7970 ssymbuf->ssym = NULL;
7971 ssymbuf->count = shndx_count;
7972 ssymbuf->st_shndx = 0;
7973 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7974 {
7975 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7976 {
7977 ssymhead++;
7978 ssymhead->ssym = ssym;
7979 ssymhead->count = 0;
7980 ssymhead->st_shndx = (*ind)->st_shndx;
7981 }
7982 ssym->st_name = (*ind)->st_name;
7983 ssym->st_info = (*ind)->st_info;
7984 ssym->st_other = (*ind)->st_other;
7985 ssymhead->count++;
7986 }
7987 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7988 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7989 == total_size));
7990
7991 free (indbuf);
7992 return ssymbuf;
7993 }
7994
7995 /* Check if 2 sections define the same set of local and global
7996 symbols. */
7997
7998 static bfd_boolean
7999 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8000 struct bfd_link_info *info)
8001 {
8002 bfd *bfd1, *bfd2;
8003 const struct elf_backend_data *bed1, *bed2;
8004 Elf_Internal_Shdr *hdr1, *hdr2;
8005 size_t symcount1, symcount2;
8006 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8007 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8008 Elf_Internal_Sym *isym, *isymend;
8009 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8010 size_t count1, count2, i;
8011 unsigned int shndx1, shndx2;
8012 bfd_boolean result;
8013
8014 bfd1 = sec1->owner;
8015 bfd2 = sec2->owner;
8016
8017 /* Both sections have to be in ELF. */
8018 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8019 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8020 return FALSE;
8021
8022 if (elf_section_type (sec1) != elf_section_type (sec2))
8023 return FALSE;
8024
8025 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8026 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8027 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8028 return FALSE;
8029
8030 bed1 = get_elf_backend_data (bfd1);
8031 bed2 = get_elf_backend_data (bfd2);
8032 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8033 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8034 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8035 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8036
8037 if (symcount1 == 0 || symcount2 == 0)
8038 return FALSE;
8039
8040 result = FALSE;
8041 isymbuf1 = NULL;
8042 isymbuf2 = NULL;
8043 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8044 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8045
8046 if (ssymbuf1 == NULL)
8047 {
8048 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8049 NULL, NULL, NULL);
8050 if (isymbuf1 == NULL)
8051 goto done;
8052
8053 if (!info->reduce_memory_overheads)
8054 {
8055 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8056 elf_tdata (bfd1)->symbuf = ssymbuf1;
8057 }
8058 }
8059
8060 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8061 {
8062 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8063 NULL, NULL, NULL);
8064 if (isymbuf2 == NULL)
8065 goto done;
8066
8067 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8068 {
8069 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8070 elf_tdata (bfd2)->symbuf = ssymbuf2;
8071 }
8072 }
8073
8074 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8075 {
8076 /* Optimized faster version. */
8077 size_t lo, hi, mid;
8078 struct elf_symbol *symp;
8079 struct elf_symbuf_symbol *ssym, *ssymend;
8080
8081 lo = 0;
8082 hi = ssymbuf1->count;
8083 ssymbuf1++;
8084 count1 = 0;
8085 while (lo < hi)
8086 {
8087 mid = (lo + hi) / 2;
8088 if (shndx1 < ssymbuf1[mid].st_shndx)
8089 hi = mid;
8090 else if (shndx1 > ssymbuf1[mid].st_shndx)
8091 lo = mid + 1;
8092 else
8093 {
8094 count1 = ssymbuf1[mid].count;
8095 ssymbuf1 += mid;
8096 break;
8097 }
8098 }
8099
8100 lo = 0;
8101 hi = ssymbuf2->count;
8102 ssymbuf2++;
8103 count2 = 0;
8104 while (lo < hi)
8105 {
8106 mid = (lo + hi) / 2;
8107 if (shndx2 < ssymbuf2[mid].st_shndx)
8108 hi = mid;
8109 else if (shndx2 > ssymbuf2[mid].st_shndx)
8110 lo = mid + 1;
8111 else
8112 {
8113 count2 = ssymbuf2[mid].count;
8114 ssymbuf2 += mid;
8115 break;
8116 }
8117 }
8118
8119 if (count1 == 0 || count2 == 0 || count1 != count2)
8120 goto done;
8121
8122 symtable1
8123 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8124 symtable2
8125 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8126 if (symtable1 == NULL || symtable2 == NULL)
8127 goto done;
8128
8129 symp = symtable1;
8130 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8131 ssym < ssymend; ssym++, symp++)
8132 {
8133 symp->u.ssym = ssym;
8134 symp->name = bfd_elf_string_from_elf_section (bfd1,
8135 hdr1->sh_link,
8136 ssym->st_name);
8137 }
8138
8139 symp = symtable2;
8140 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8141 ssym < ssymend; ssym++, symp++)
8142 {
8143 symp->u.ssym = ssym;
8144 symp->name = bfd_elf_string_from_elf_section (bfd2,
8145 hdr2->sh_link,
8146 ssym->st_name);
8147 }
8148
8149 /* Sort symbol by name. */
8150 qsort (symtable1, count1, sizeof (struct elf_symbol),
8151 elf_sym_name_compare);
8152 qsort (symtable2, count1, sizeof (struct elf_symbol),
8153 elf_sym_name_compare);
8154
8155 for (i = 0; i < count1; i++)
8156 /* Two symbols must have the same binding, type and name. */
8157 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8158 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8159 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8160 goto done;
8161
8162 result = TRUE;
8163 goto done;
8164 }
8165
8166 symtable1 = (struct elf_symbol *)
8167 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8168 symtable2 = (struct elf_symbol *)
8169 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8170 if (symtable1 == NULL || symtable2 == NULL)
8171 goto done;
8172
8173 /* Count definitions in the section. */
8174 count1 = 0;
8175 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8176 if (isym->st_shndx == shndx1)
8177 symtable1[count1++].u.isym = isym;
8178
8179 count2 = 0;
8180 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8181 if (isym->st_shndx == shndx2)
8182 symtable2[count2++].u.isym = isym;
8183
8184 if (count1 == 0 || count2 == 0 || count1 != count2)
8185 goto done;
8186
8187 for (i = 0; i < count1; i++)
8188 symtable1[i].name
8189 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8190 symtable1[i].u.isym->st_name);
8191
8192 for (i = 0; i < count2; i++)
8193 symtable2[i].name
8194 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8195 symtable2[i].u.isym->st_name);
8196
8197 /* Sort symbol by name. */
8198 qsort (symtable1, count1, sizeof (struct elf_symbol),
8199 elf_sym_name_compare);
8200 qsort (symtable2, count1, sizeof (struct elf_symbol),
8201 elf_sym_name_compare);
8202
8203 for (i = 0; i < count1; i++)
8204 /* Two symbols must have the same binding, type and name. */
8205 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8206 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8207 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8208 goto done;
8209
8210 result = TRUE;
8211
8212 done:
8213 if (symtable1)
8214 free (symtable1);
8215 if (symtable2)
8216 free (symtable2);
8217 if (isymbuf1)
8218 free (isymbuf1);
8219 if (isymbuf2)
8220 free (isymbuf2);
8221
8222 return result;
8223 }
8224
8225 /* Return TRUE if 2 section types are compatible. */
8226
8227 bfd_boolean
8228 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8229 bfd *bbfd, const asection *bsec)
8230 {
8231 if (asec == NULL
8232 || bsec == NULL
8233 || abfd->xvec->flavour != bfd_target_elf_flavour
8234 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8235 return TRUE;
8236
8237 return elf_section_type (asec) == elf_section_type (bsec);
8238 }
8239 \f
8240 /* Final phase of ELF linker. */
8241
8242 /* A structure we use to avoid passing large numbers of arguments. */
8243
8244 struct elf_final_link_info
8245 {
8246 /* General link information. */
8247 struct bfd_link_info *info;
8248 /* Output BFD. */
8249 bfd *output_bfd;
8250 /* Symbol string table. */
8251 struct elf_strtab_hash *symstrtab;
8252 /* .hash section. */
8253 asection *hash_sec;
8254 /* symbol version section (.gnu.version). */
8255 asection *symver_sec;
8256 /* Buffer large enough to hold contents of any section. */
8257 bfd_byte *contents;
8258 /* Buffer large enough to hold external relocs of any section. */
8259 void *external_relocs;
8260 /* Buffer large enough to hold internal relocs of any section. */
8261 Elf_Internal_Rela *internal_relocs;
8262 /* Buffer large enough to hold external local symbols of any input
8263 BFD. */
8264 bfd_byte *external_syms;
8265 /* And a buffer for symbol section indices. */
8266 Elf_External_Sym_Shndx *locsym_shndx;
8267 /* Buffer large enough to hold internal local symbols of any input
8268 BFD. */
8269 Elf_Internal_Sym *internal_syms;
8270 /* Array large enough to hold a symbol index for each local symbol
8271 of any input BFD. */
8272 long *indices;
8273 /* Array large enough to hold a section pointer for each local
8274 symbol of any input BFD. */
8275 asection **sections;
8276 /* Buffer for SHT_SYMTAB_SHNDX section. */
8277 Elf_External_Sym_Shndx *symshndxbuf;
8278 /* Number of STT_FILE syms seen. */
8279 size_t filesym_count;
8280 };
8281
8282 /* This struct is used to pass information to elf_link_output_extsym. */
8283
8284 struct elf_outext_info
8285 {
8286 bfd_boolean failed;
8287 bfd_boolean localsyms;
8288 bfd_boolean file_sym_done;
8289 struct elf_final_link_info *flinfo;
8290 };
8291
8292
8293 /* Support for evaluating a complex relocation.
8294
8295 Complex relocations are generalized, self-describing relocations. The
8296 implementation of them consists of two parts: complex symbols, and the
8297 relocations themselves.
8298
8299 The relocations are use a reserved elf-wide relocation type code (R_RELC
8300 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8301 information (start bit, end bit, word width, etc) into the addend. This
8302 information is extracted from CGEN-generated operand tables within gas.
8303
8304 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8305 internal) representing prefix-notation expressions, including but not
8306 limited to those sorts of expressions normally encoded as addends in the
8307 addend field. The symbol mangling format is:
8308
8309 <node> := <literal>
8310 | <unary-operator> ':' <node>
8311 | <binary-operator> ':' <node> ':' <node>
8312 ;
8313
8314 <literal> := 's' <digits=N> ':' <N character symbol name>
8315 | 'S' <digits=N> ':' <N character section name>
8316 | '#' <hexdigits>
8317 ;
8318
8319 <binary-operator> := as in C
8320 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8321
8322 static void
8323 set_symbol_value (bfd *bfd_with_globals,
8324 Elf_Internal_Sym *isymbuf,
8325 size_t locsymcount,
8326 size_t symidx,
8327 bfd_vma val)
8328 {
8329 struct elf_link_hash_entry **sym_hashes;
8330 struct elf_link_hash_entry *h;
8331 size_t extsymoff = locsymcount;
8332
8333 if (symidx < locsymcount)
8334 {
8335 Elf_Internal_Sym *sym;
8336
8337 sym = isymbuf + symidx;
8338 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8339 {
8340 /* It is a local symbol: move it to the
8341 "absolute" section and give it a value. */
8342 sym->st_shndx = SHN_ABS;
8343 sym->st_value = val;
8344 return;
8345 }
8346 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8347 extsymoff = 0;
8348 }
8349
8350 /* It is a global symbol: set its link type
8351 to "defined" and give it a value. */
8352
8353 sym_hashes = elf_sym_hashes (bfd_with_globals);
8354 h = sym_hashes [symidx - extsymoff];
8355 while (h->root.type == bfd_link_hash_indirect
8356 || h->root.type == bfd_link_hash_warning)
8357 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8358 h->root.type = bfd_link_hash_defined;
8359 h->root.u.def.value = val;
8360 h->root.u.def.section = bfd_abs_section_ptr;
8361 }
8362
8363 static bfd_boolean
8364 resolve_symbol (const char *name,
8365 bfd *input_bfd,
8366 struct elf_final_link_info *flinfo,
8367 bfd_vma *result,
8368 Elf_Internal_Sym *isymbuf,
8369 size_t locsymcount)
8370 {
8371 Elf_Internal_Sym *sym;
8372 struct bfd_link_hash_entry *global_entry;
8373 const char *candidate = NULL;
8374 Elf_Internal_Shdr *symtab_hdr;
8375 size_t i;
8376
8377 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8378
8379 for (i = 0; i < locsymcount; ++ i)
8380 {
8381 sym = isymbuf + i;
8382
8383 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8384 continue;
8385
8386 candidate = bfd_elf_string_from_elf_section (input_bfd,
8387 symtab_hdr->sh_link,
8388 sym->st_name);
8389 #ifdef DEBUG
8390 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8391 name, candidate, (unsigned long) sym->st_value);
8392 #endif
8393 if (candidate && strcmp (candidate, name) == 0)
8394 {
8395 asection *sec = flinfo->sections [i];
8396
8397 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8398 *result += sec->output_offset + sec->output_section->vma;
8399 #ifdef DEBUG
8400 printf ("Found symbol with value %8.8lx\n",
8401 (unsigned long) *result);
8402 #endif
8403 return TRUE;
8404 }
8405 }
8406
8407 /* Hmm, haven't found it yet. perhaps it is a global. */
8408 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8409 FALSE, FALSE, TRUE);
8410 if (!global_entry)
8411 return FALSE;
8412
8413 if (global_entry->type == bfd_link_hash_defined
8414 || global_entry->type == bfd_link_hash_defweak)
8415 {
8416 *result = (global_entry->u.def.value
8417 + global_entry->u.def.section->output_section->vma
8418 + global_entry->u.def.section->output_offset);
8419 #ifdef DEBUG
8420 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8421 global_entry->root.string, (unsigned long) *result);
8422 #endif
8423 return TRUE;
8424 }
8425
8426 return FALSE;
8427 }
8428
8429 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8430 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8431 names like "foo.end" which is the end address of section "foo". */
8432
8433 static bfd_boolean
8434 resolve_section (const char *name,
8435 asection *sections,
8436 bfd_vma *result,
8437 bfd * abfd)
8438 {
8439 asection *curr;
8440 unsigned int len;
8441
8442 for (curr = sections; curr; curr = curr->next)
8443 if (strcmp (curr->name, name) == 0)
8444 {
8445 *result = curr->vma;
8446 return TRUE;
8447 }
8448
8449 /* Hmm. still haven't found it. try pseudo-section names. */
8450 /* FIXME: This could be coded more efficiently... */
8451 for (curr = sections; curr; curr = curr->next)
8452 {
8453 len = strlen (curr->name);
8454 if (len > strlen (name))
8455 continue;
8456
8457 if (strncmp (curr->name, name, len) == 0)
8458 {
8459 if (strncmp (".end", name + len, 4) == 0)
8460 {
8461 *result = (curr->vma
8462 + curr->size / bfd_octets_per_byte (abfd, curr));
8463 return TRUE;
8464 }
8465
8466 /* Insert more pseudo-section names here, if you like. */
8467 }
8468 }
8469
8470 return FALSE;
8471 }
8472
8473 static void
8474 undefined_reference (const char *reftype, const char *name)
8475 {
8476 /* xgettext:c-format */
8477 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8478 reftype, name);
8479 }
8480
8481 static bfd_boolean
8482 eval_symbol (bfd_vma *result,
8483 const char **symp,
8484 bfd *input_bfd,
8485 struct elf_final_link_info *flinfo,
8486 bfd_vma dot,
8487 Elf_Internal_Sym *isymbuf,
8488 size_t locsymcount,
8489 int signed_p)
8490 {
8491 size_t len;
8492 size_t symlen;
8493 bfd_vma a;
8494 bfd_vma b;
8495 char symbuf[4096];
8496 const char *sym = *symp;
8497 const char *symend;
8498 bfd_boolean symbol_is_section = FALSE;
8499
8500 len = strlen (sym);
8501 symend = sym + len;
8502
8503 if (len < 1 || len > sizeof (symbuf))
8504 {
8505 bfd_set_error (bfd_error_invalid_operation);
8506 return FALSE;
8507 }
8508
8509 switch (* sym)
8510 {
8511 case '.':
8512 *result = dot;
8513 *symp = sym + 1;
8514 return TRUE;
8515
8516 case '#':
8517 ++sym;
8518 *result = strtoul (sym, (char **) symp, 16);
8519 return TRUE;
8520
8521 case 'S':
8522 symbol_is_section = TRUE;
8523 /* Fall through. */
8524 case 's':
8525 ++sym;
8526 symlen = strtol (sym, (char **) symp, 10);
8527 sym = *symp + 1; /* Skip the trailing ':'. */
8528
8529 if (symend < sym || symlen + 1 > sizeof (symbuf))
8530 {
8531 bfd_set_error (bfd_error_invalid_operation);
8532 return FALSE;
8533 }
8534
8535 memcpy (symbuf, sym, symlen);
8536 symbuf[symlen] = '\0';
8537 *symp = sym + symlen;
8538
8539 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8540 the symbol as a section, or vice-versa. so we're pretty liberal in our
8541 interpretation here; section means "try section first", not "must be a
8542 section", and likewise with symbol. */
8543
8544 if (symbol_is_section)
8545 {
8546 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8547 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8548 isymbuf, locsymcount))
8549 {
8550 undefined_reference ("section", symbuf);
8551 return FALSE;
8552 }
8553 }
8554 else
8555 {
8556 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8557 isymbuf, locsymcount)
8558 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8559 result, input_bfd))
8560 {
8561 undefined_reference ("symbol", symbuf);
8562 return FALSE;
8563 }
8564 }
8565
8566 return TRUE;
8567
8568 /* All that remains are operators. */
8569
8570 #define UNARY_OP(op) \
8571 if (strncmp (sym, #op, strlen (#op)) == 0) \
8572 { \
8573 sym += strlen (#op); \
8574 if (*sym == ':') \
8575 ++sym; \
8576 *symp = sym; \
8577 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8578 isymbuf, locsymcount, signed_p)) \
8579 return FALSE; \
8580 if (signed_p) \
8581 *result = op ((bfd_signed_vma) a); \
8582 else \
8583 *result = op a; \
8584 return TRUE; \
8585 }
8586
8587 #define BINARY_OP(op) \
8588 if (strncmp (sym, #op, strlen (#op)) == 0) \
8589 { \
8590 sym += strlen (#op); \
8591 if (*sym == ':') \
8592 ++sym; \
8593 *symp = sym; \
8594 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8595 isymbuf, locsymcount, signed_p)) \
8596 return FALSE; \
8597 ++*symp; \
8598 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8599 isymbuf, locsymcount, signed_p)) \
8600 return FALSE; \
8601 if (signed_p) \
8602 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8603 else \
8604 *result = a op b; \
8605 return TRUE; \
8606 }
8607
8608 default:
8609 UNARY_OP (0-);
8610 BINARY_OP (<<);
8611 BINARY_OP (>>);
8612 BINARY_OP (==);
8613 BINARY_OP (!=);
8614 BINARY_OP (<=);
8615 BINARY_OP (>=);
8616 BINARY_OP (&&);
8617 BINARY_OP (||);
8618 UNARY_OP (~);
8619 UNARY_OP (!);
8620 BINARY_OP (*);
8621 BINARY_OP (/);
8622 BINARY_OP (%);
8623 BINARY_OP (^);
8624 BINARY_OP (|);
8625 BINARY_OP (&);
8626 BINARY_OP (+);
8627 BINARY_OP (-);
8628 BINARY_OP (<);
8629 BINARY_OP (>);
8630 #undef UNARY_OP
8631 #undef BINARY_OP
8632 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8633 bfd_set_error (bfd_error_invalid_operation);
8634 return FALSE;
8635 }
8636 }
8637
8638 static void
8639 put_value (bfd_vma size,
8640 unsigned long chunksz,
8641 bfd *input_bfd,
8642 bfd_vma x,
8643 bfd_byte *location)
8644 {
8645 location += (size - chunksz);
8646
8647 for (; size; size -= chunksz, location -= chunksz)
8648 {
8649 switch (chunksz)
8650 {
8651 case 1:
8652 bfd_put_8 (input_bfd, x, location);
8653 x >>= 8;
8654 break;
8655 case 2:
8656 bfd_put_16 (input_bfd, x, location);
8657 x >>= 16;
8658 break;
8659 case 4:
8660 bfd_put_32 (input_bfd, x, location);
8661 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8662 x >>= 16;
8663 x >>= 16;
8664 break;
8665 #ifdef BFD64
8666 case 8:
8667 bfd_put_64 (input_bfd, x, location);
8668 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8669 x >>= 32;
8670 x >>= 32;
8671 break;
8672 #endif
8673 default:
8674 abort ();
8675 break;
8676 }
8677 }
8678 }
8679
8680 static bfd_vma
8681 get_value (bfd_vma size,
8682 unsigned long chunksz,
8683 bfd *input_bfd,
8684 bfd_byte *location)
8685 {
8686 int shift;
8687 bfd_vma x = 0;
8688
8689 /* Sanity checks. */
8690 BFD_ASSERT (chunksz <= sizeof (x)
8691 && size >= chunksz
8692 && chunksz != 0
8693 && (size % chunksz) == 0
8694 && input_bfd != NULL
8695 && location != NULL);
8696
8697 if (chunksz == sizeof (x))
8698 {
8699 BFD_ASSERT (size == chunksz);
8700
8701 /* Make sure that we do not perform an undefined shift operation.
8702 We know that size == chunksz so there will only be one iteration
8703 of the loop below. */
8704 shift = 0;
8705 }
8706 else
8707 shift = 8 * chunksz;
8708
8709 for (; size; size -= chunksz, location += chunksz)
8710 {
8711 switch (chunksz)
8712 {
8713 case 1:
8714 x = (x << shift) | bfd_get_8 (input_bfd, location);
8715 break;
8716 case 2:
8717 x = (x << shift) | bfd_get_16 (input_bfd, location);
8718 break;
8719 case 4:
8720 x = (x << shift) | bfd_get_32 (input_bfd, location);
8721 break;
8722 #ifdef BFD64
8723 case 8:
8724 x = (x << shift) | bfd_get_64 (input_bfd, location);
8725 break;
8726 #endif
8727 default:
8728 abort ();
8729 }
8730 }
8731 return x;
8732 }
8733
8734 static void
8735 decode_complex_addend (unsigned long *start, /* in bits */
8736 unsigned long *oplen, /* in bits */
8737 unsigned long *len, /* in bits */
8738 unsigned long *wordsz, /* in bytes */
8739 unsigned long *chunksz, /* in bytes */
8740 unsigned long *lsb0_p,
8741 unsigned long *signed_p,
8742 unsigned long *trunc_p,
8743 unsigned long encoded)
8744 {
8745 * start = encoded & 0x3F;
8746 * len = (encoded >> 6) & 0x3F;
8747 * oplen = (encoded >> 12) & 0x3F;
8748 * wordsz = (encoded >> 18) & 0xF;
8749 * chunksz = (encoded >> 22) & 0xF;
8750 * lsb0_p = (encoded >> 27) & 1;
8751 * signed_p = (encoded >> 28) & 1;
8752 * trunc_p = (encoded >> 29) & 1;
8753 }
8754
8755 bfd_reloc_status_type
8756 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8757 asection *input_section,
8758 bfd_byte *contents,
8759 Elf_Internal_Rela *rel,
8760 bfd_vma relocation)
8761 {
8762 bfd_vma shift, x, mask;
8763 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8764 bfd_reloc_status_type r;
8765 bfd_size_type octets;
8766
8767 /* Perform this reloc, since it is complex.
8768 (this is not to say that it necessarily refers to a complex
8769 symbol; merely that it is a self-describing CGEN based reloc.
8770 i.e. the addend has the complete reloc information (bit start, end,
8771 word size, etc) encoded within it.). */
8772
8773 decode_complex_addend (&start, &oplen, &len, &wordsz,
8774 &chunksz, &lsb0_p, &signed_p,
8775 &trunc_p, rel->r_addend);
8776
8777 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8778
8779 if (lsb0_p)
8780 shift = (start + 1) - len;
8781 else
8782 shift = (8 * wordsz) - (start + len);
8783
8784 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8785 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8786
8787 #ifdef DEBUG
8788 printf ("Doing complex reloc: "
8789 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8790 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8791 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8792 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8793 oplen, (unsigned long) x, (unsigned long) mask,
8794 (unsigned long) relocation);
8795 #endif
8796
8797 r = bfd_reloc_ok;
8798 if (! trunc_p)
8799 /* Now do an overflow check. */
8800 r = bfd_check_overflow ((signed_p
8801 ? complain_overflow_signed
8802 : complain_overflow_unsigned),
8803 len, 0, (8 * wordsz),
8804 relocation);
8805
8806 /* Do the deed. */
8807 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8808
8809 #ifdef DEBUG
8810 printf (" relocation: %8.8lx\n"
8811 " shifted mask: %8.8lx\n"
8812 " shifted/masked reloc: %8.8lx\n"
8813 " result: %8.8lx\n",
8814 (unsigned long) relocation, (unsigned long) (mask << shift),
8815 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8816 #endif
8817 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8818 return r;
8819 }
8820
8821 /* Functions to read r_offset from external (target order) reloc
8822 entry. Faster than bfd_getl32 et al, because we let the compiler
8823 know the value is aligned. */
8824
8825 static bfd_vma
8826 ext32l_r_offset (const void *p)
8827 {
8828 union aligned32
8829 {
8830 uint32_t v;
8831 unsigned char c[4];
8832 };
8833 const union aligned32 *a
8834 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8835
8836 uint32_t aval = ( (uint32_t) a->c[0]
8837 | (uint32_t) a->c[1] << 8
8838 | (uint32_t) a->c[2] << 16
8839 | (uint32_t) a->c[3] << 24);
8840 return aval;
8841 }
8842
8843 static bfd_vma
8844 ext32b_r_offset (const void *p)
8845 {
8846 union aligned32
8847 {
8848 uint32_t v;
8849 unsigned char c[4];
8850 };
8851 const union aligned32 *a
8852 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8853
8854 uint32_t aval = ( (uint32_t) a->c[0] << 24
8855 | (uint32_t) a->c[1] << 16
8856 | (uint32_t) a->c[2] << 8
8857 | (uint32_t) a->c[3]);
8858 return aval;
8859 }
8860
8861 #ifdef BFD_HOST_64_BIT
8862 static bfd_vma
8863 ext64l_r_offset (const void *p)
8864 {
8865 union aligned64
8866 {
8867 uint64_t v;
8868 unsigned char c[8];
8869 };
8870 const union aligned64 *a
8871 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8872
8873 uint64_t aval = ( (uint64_t) a->c[0]
8874 | (uint64_t) a->c[1] << 8
8875 | (uint64_t) a->c[2] << 16
8876 | (uint64_t) a->c[3] << 24
8877 | (uint64_t) a->c[4] << 32
8878 | (uint64_t) a->c[5] << 40
8879 | (uint64_t) a->c[6] << 48
8880 | (uint64_t) a->c[7] << 56);
8881 return aval;
8882 }
8883
8884 static bfd_vma
8885 ext64b_r_offset (const void *p)
8886 {
8887 union aligned64
8888 {
8889 uint64_t v;
8890 unsigned char c[8];
8891 };
8892 const union aligned64 *a
8893 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8894
8895 uint64_t aval = ( (uint64_t) a->c[0] << 56
8896 | (uint64_t) a->c[1] << 48
8897 | (uint64_t) a->c[2] << 40
8898 | (uint64_t) a->c[3] << 32
8899 | (uint64_t) a->c[4] << 24
8900 | (uint64_t) a->c[5] << 16
8901 | (uint64_t) a->c[6] << 8
8902 | (uint64_t) a->c[7]);
8903 return aval;
8904 }
8905 #endif
8906
8907 /* When performing a relocatable link, the input relocations are
8908 preserved. But, if they reference global symbols, the indices
8909 referenced must be updated. Update all the relocations found in
8910 RELDATA. */
8911
8912 static bfd_boolean
8913 elf_link_adjust_relocs (bfd *abfd,
8914 asection *sec,
8915 struct bfd_elf_section_reloc_data *reldata,
8916 bfd_boolean sort,
8917 struct bfd_link_info *info)
8918 {
8919 unsigned int i;
8920 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8921 bfd_byte *erela;
8922 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8923 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8924 bfd_vma r_type_mask;
8925 int r_sym_shift;
8926 unsigned int count = reldata->count;
8927 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8928
8929 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8930 {
8931 swap_in = bed->s->swap_reloc_in;
8932 swap_out = bed->s->swap_reloc_out;
8933 }
8934 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8935 {
8936 swap_in = bed->s->swap_reloca_in;
8937 swap_out = bed->s->swap_reloca_out;
8938 }
8939 else
8940 abort ();
8941
8942 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8943 abort ();
8944
8945 if (bed->s->arch_size == 32)
8946 {
8947 r_type_mask = 0xff;
8948 r_sym_shift = 8;
8949 }
8950 else
8951 {
8952 r_type_mask = 0xffffffff;
8953 r_sym_shift = 32;
8954 }
8955
8956 erela = reldata->hdr->contents;
8957 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8958 {
8959 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8960 unsigned int j;
8961
8962 if (*rel_hash == NULL)
8963 continue;
8964
8965 if ((*rel_hash)->indx == -2
8966 && info->gc_sections
8967 && ! info->gc_keep_exported)
8968 {
8969 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8970 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8971 abfd, sec,
8972 (*rel_hash)->root.root.string);
8973 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8974 abfd, sec);
8975 bfd_set_error (bfd_error_invalid_operation);
8976 return FALSE;
8977 }
8978 BFD_ASSERT ((*rel_hash)->indx >= 0);
8979
8980 (*swap_in) (abfd, erela, irela);
8981 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8982 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8983 | (irela[j].r_info & r_type_mask));
8984 (*swap_out) (abfd, irela, erela);
8985 }
8986
8987 if (bed->elf_backend_update_relocs)
8988 (*bed->elf_backend_update_relocs) (sec, reldata);
8989
8990 if (sort && count != 0)
8991 {
8992 bfd_vma (*ext_r_off) (const void *);
8993 bfd_vma r_off;
8994 size_t elt_size;
8995 bfd_byte *base, *end, *p, *loc;
8996 bfd_byte *buf = NULL;
8997
8998 if (bed->s->arch_size == 32)
8999 {
9000 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9001 ext_r_off = ext32l_r_offset;
9002 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9003 ext_r_off = ext32b_r_offset;
9004 else
9005 abort ();
9006 }
9007 else
9008 {
9009 #ifdef BFD_HOST_64_BIT
9010 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9011 ext_r_off = ext64l_r_offset;
9012 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9013 ext_r_off = ext64b_r_offset;
9014 else
9015 #endif
9016 abort ();
9017 }
9018
9019 /* Must use a stable sort here. A modified insertion sort,
9020 since the relocs are mostly sorted already. */
9021 elt_size = reldata->hdr->sh_entsize;
9022 base = reldata->hdr->contents;
9023 end = base + count * elt_size;
9024 if (elt_size > sizeof (Elf64_External_Rela))
9025 abort ();
9026
9027 /* Ensure the first element is lowest. This acts as a sentinel,
9028 speeding the main loop below. */
9029 r_off = (*ext_r_off) (base);
9030 for (p = loc = base; (p += elt_size) < end; )
9031 {
9032 bfd_vma r_off2 = (*ext_r_off) (p);
9033 if (r_off > r_off2)
9034 {
9035 r_off = r_off2;
9036 loc = p;
9037 }
9038 }
9039 if (loc != base)
9040 {
9041 /* Don't just swap *base and *loc as that changes the order
9042 of the original base[0] and base[1] if they happen to
9043 have the same r_offset. */
9044 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9045 memcpy (onebuf, loc, elt_size);
9046 memmove (base + elt_size, base, loc - base);
9047 memcpy (base, onebuf, elt_size);
9048 }
9049
9050 for (p = base + elt_size; (p += elt_size) < end; )
9051 {
9052 /* base to p is sorted, *p is next to insert. */
9053 r_off = (*ext_r_off) (p);
9054 /* Search the sorted region for location to insert. */
9055 loc = p - elt_size;
9056 while (r_off < (*ext_r_off) (loc))
9057 loc -= elt_size;
9058 loc += elt_size;
9059 if (loc != p)
9060 {
9061 /* Chances are there is a run of relocs to insert here,
9062 from one of more input files. Files are not always
9063 linked in order due to the way elf_link_input_bfd is
9064 called. See pr17666. */
9065 size_t sortlen = p - loc;
9066 bfd_vma r_off2 = (*ext_r_off) (loc);
9067 size_t runlen = elt_size;
9068 size_t buf_size = 96 * 1024;
9069 while (p + runlen < end
9070 && (sortlen <= buf_size
9071 || runlen + elt_size <= buf_size)
9072 && r_off2 > (*ext_r_off) (p + runlen))
9073 runlen += elt_size;
9074 if (buf == NULL)
9075 {
9076 buf = bfd_malloc (buf_size);
9077 if (buf == NULL)
9078 return FALSE;
9079 }
9080 if (runlen < sortlen)
9081 {
9082 memcpy (buf, p, runlen);
9083 memmove (loc + runlen, loc, sortlen);
9084 memcpy (loc, buf, runlen);
9085 }
9086 else
9087 {
9088 memcpy (buf, loc, sortlen);
9089 memmove (loc, p, runlen);
9090 memcpy (loc + runlen, buf, sortlen);
9091 }
9092 p += runlen - elt_size;
9093 }
9094 }
9095 /* Hashes are no longer valid. */
9096 free (reldata->hashes);
9097 reldata->hashes = NULL;
9098 free (buf);
9099 }
9100 return TRUE;
9101 }
9102
9103 struct elf_link_sort_rela
9104 {
9105 union {
9106 bfd_vma offset;
9107 bfd_vma sym_mask;
9108 } u;
9109 enum elf_reloc_type_class type;
9110 /* We use this as an array of size int_rels_per_ext_rel. */
9111 Elf_Internal_Rela rela[1];
9112 };
9113
9114 /* qsort stability here and for cmp2 is only an issue if multiple
9115 dynamic relocations are emitted at the same address. But targets
9116 that apply a series of dynamic relocations each operating on the
9117 result of the prior relocation can't use -z combreloc as
9118 implemented anyway. Such schemes tend to be broken by sorting on
9119 symbol index. That leaves dynamic NONE relocs as the only other
9120 case where ld might emit multiple relocs at the same address, and
9121 those are only emitted due to target bugs. */
9122
9123 static int
9124 elf_link_sort_cmp1 (const void *A, const void *B)
9125 {
9126 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9127 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9128 int relativea, relativeb;
9129
9130 relativea = a->type == reloc_class_relative;
9131 relativeb = b->type == reloc_class_relative;
9132
9133 if (relativea < relativeb)
9134 return 1;
9135 if (relativea > relativeb)
9136 return -1;
9137 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9138 return -1;
9139 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9140 return 1;
9141 if (a->rela->r_offset < b->rela->r_offset)
9142 return -1;
9143 if (a->rela->r_offset > b->rela->r_offset)
9144 return 1;
9145 return 0;
9146 }
9147
9148 static int
9149 elf_link_sort_cmp2 (const void *A, const void *B)
9150 {
9151 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9152 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9153
9154 if (a->type < b->type)
9155 return -1;
9156 if (a->type > b->type)
9157 return 1;
9158 if (a->u.offset < b->u.offset)
9159 return -1;
9160 if (a->u.offset > b->u.offset)
9161 return 1;
9162 if (a->rela->r_offset < b->rela->r_offset)
9163 return -1;
9164 if (a->rela->r_offset > b->rela->r_offset)
9165 return 1;
9166 return 0;
9167 }
9168
9169 static size_t
9170 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9171 {
9172 asection *dynamic_relocs;
9173 asection *rela_dyn;
9174 asection *rel_dyn;
9175 bfd_size_type count, size;
9176 size_t i, ret, sort_elt, ext_size;
9177 bfd_byte *sort, *s_non_relative, *p;
9178 struct elf_link_sort_rela *sq;
9179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9180 int i2e = bed->s->int_rels_per_ext_rel;
9181 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9182 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9183 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9184 struct bfd_link_order *lo;
9185 bfd_vma r_sym_mask;
9186 bfd_boolean use_rela;
9187
9188 /* Find a dynamic reloc section. */
9189 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9190 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9191 if (rela_dyn != NULL && rela_dyn->size > 0
9192 && rel_dyn != NULL && rel_dyn->size > 0)
9193 {
9194 bfd_boolean use_rela_initialised = FALSE;
9195
9196 /* This is just here to stop gcc from complaining.
9197 Its initialization checking code is not perfect. */
9198 use_rela = TRUE;
9199
9200 /* Both sections are present. Examine the sizes
9201 of the indirect sections to help us choose. */
9202 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9203 if (lo->type == bfd_indirect_link_order)
9204 {
9205 asection *o = lo->u.indirect.section;
9206
9207 if ((o->size % bed->s->sizeof_rela) == 0)
9208 {
9209 if ((o->size % bed->s->sizeof_rel) == 0)
9210 /* Section size is divisible by both rel and rela sizes.
9211 It is of no help to us. */
9212 ;
9213 else
9214 {
9215 /* Section size is only divisible by rela. */
9216 if (use_rela_initialised && !use_rela)
9217 {
9218 _bfd_error_handler (_("%pB: unable to sort relocs - "
9219 "they are in more than one size"),
9220 abfd);
9221 bfd_set_error (bfd_error_invalid_operation);
9222 return 0;
9223 }
9224 else
9225 {
9226 use_rela = TRUE;
9227 use_rela_initialised = TRUE;
9228 }
9229 }
9230 }
9231 else if ((o->size % bed->s->sizeof_rel) == 0)
9232 {
9233 /* Section size is only divisible by rel. */
9234 if (use_rela_initialised && use_rela)
9235 {
9236 _bfd_error_handler (_("%pB: unable to sort relocs - "
9237 "they are in more than one size"),
9238 abfd);
9239 bfd_set_error (bfd_error_invalid_operation);
9240 return 0;
9241 }
9242 else
9243 {
9244 use_rela = FALSE;
9245 use_rela_initialised = TRUE;
9246 }
9247 }
9248 else
9249 {
9250 /* The section size is not divisible by either -
9251 something is wrong. */
9252 _bfd_error_handler (_("%pB: unable to sort relocs - "
9253 "they are of an unknown size"), abfd);
9254 bfd_set_error (bfd_error_invalid_operation);
9255 return 0;
9256 }
9257 }
9258
9259 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9260 if (lo->type == bfd_indirect_link_order)
9261 {
9262 asection *o = lo->u.indirect.section;
9263
9264 if ((o->size % bed->s->sizeof_rela) == 0)
9265 {
9266 if ((o->size % bed->s->sizeof_rel) == 0)
9267 /* Section size is divisible by both rel and rela sizes.
9268 It is of no help to us. */
9269 ;
9270 else
9271 {
9272 /* Section size is only divisible by rela. */
9273 if (use_rela_initialised && !use_rela)
9274 {
9275 _bfd_error_handler (_("%pB: unable to sort relocs - "
9276 "they are in more than one size"),
9277 abfd);
9278 bfd_set_error (bfd_error_invalid_operation);
9279 return 0;
9280 }
9281 else
9282 {
9283 use_rela = TRUE;
9284 use_rela_initialised = TRUE;
9285 }
9286 }
9287 }
9288 else if ((o->size % bed->s->sizeof_rel) == 0)
9289 {
9290 /* Section size is only divisible by rel. */
9291 if (use_rela_initialised && use_rela)
9292 {
9293 _bfd_error_handler (_("%pB: unable to sort relocs - "
9294 "they are in more than one size"),
9295 abfd);
9296 bfd_set_error (bfd_error_invalid_operation);
9297 return 0;
9298 }
9299 else
9300 {
9301 use_rela = FALSE;
9302 use_rela_initialised = TRUE;
9303 }
9304 }
9305 else
9306 {
9307 /* The section size is not divisible by either -
9308 something is wrong. */
9309 _bfd_error_handler (_("%pB: unable to sort relocs - "
9310 "they are of an unknown size"), abfd);
9311 bfd_set_error (bfd_error_invalid_operation);
9312 return 0;
9313 }
9314 }
9315
9316 if (! use_rela_initialised)
9317 /* Make a guess. */
9318 use_rela = TRUE;
9319 }
9320 else if (rela_dyn != NULL && rela_dyn->size > 0)
9321 use_rela = TRUE;
9322 else if (rel_dyn != NULL && rel_dyn->size > 0)
9323 use_rela = FALSE;
9324 else
9325 return 0;
9326
9327 if (use_rela)
9328 {
9329 dynamic_relocs = rela_dyn;
9330 ext_size = bed->s->sizeof_rela;
9331 swap_in = bed->s->swap_reloca_in;
9332 swap_out = bed->s->swap_reloca_out;
9333 }
9334 else
9335 {
9336 dynamic_relocs = rel_dyn;
9337 ext_size = bed->s->sizeof_rel;
9338 swap_in = bed->s->swap_reloc_in;
9339 swap_out = bed->s->swap_reloc_out;
9340 }
9341
9342 size = 0;
9343 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9344 if (lo->type == bfd_indirect_link_order)
9345 size += lo->u.indirect.section->size;
9346
9347 if (size != dynamic_relocs->size)
9348 return 0;
9349
9350 sort_elt = (sizeof (struct elf_link_sort_rela)
9351 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9352
9353 count = dynamic_relocs->size / ext_size;
9354 if (count == 0)
9355 return 0;
9356 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9357
9358 if (sort == NULL)
9359 {
9360 (*info->callbacks->warning)
9361 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9362 return 0;
9363 }
9364
9365 if (bed->s->arch_size == 32)
9366 r_sym_mask = ~(bfd_vma) 0xff;
9367 else
9368 r_sym_mask = ~(bfd_vma) 0xffffffff;
9369
9370 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9371 if (lo->type == bfd_indirect_link_order)
9372 {
9373 bfd_byte *erel, *erelend;
9374 asection *o = lo->u.indirect.section;
9375
9376 if (o->contents == NULL && o->size != 0)
9377 {
9378 /* This is a reloc section that is being handled as a normal
9379 section. See bfd_section_from_shdr. We can't combine
9380 relocs in this case. */
9381 free (sort);
9382 return 0;
9383 }
9384 erel = o->contents;
9385 erelend = o->contents + o->size;
9386 p = sort + o->output_offset * opb / ext_size * sort_elt;
9387
9388 while (erel < erelend)
9389 {
9390 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9391
9392 (*swap_in) (abfd, erel, s->rela);
9393 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9394 s->u.sym_mask = r_sym_mask;
9395 p += sort_elt;
9396 erel += ext_size;
9397 }
9398 }
9399
9400 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9401
9402 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9403 {
9404 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9405 if (s->type != reloc_class_relative)
9406 break;
9407 }
9408 ret = i;
9409 s_non_relative = p;
9410
9411 sq = (struct elf_link_sort_rela *) s_non_relative;
9412 for (; i < count; i++, p += sort_elt)
9413 {
9414 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9415 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9416 sq = sp;
9417 sp->u.offset = sq->rela->r_offset;
9418 }
9419
9420 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9421
9422 struct elf_link_hash_table *htab = elf_hash_table (info);
9423 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9424 {
9425 /* We have plt relocs in .rela.dyn. */
9426 sq = (struct elf_link_sort_rela *) sort;
9427 for (i = 0; i < count; i++)
9428 if (sq[count - i - 1].type != reloc_class_plt)
9429 break;
9430 if (i != 0 && htab->srelplt->size == i * ext_size)
9431 {
9432 struct bfd_link_order **plo;
9433 /* Put srelplt link_order last. This is so the output_offset
9434 set in the next loop is correct for DT_JMPREL. */
9435 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9436 if ((*plo)->type == bfd_indirect_link_order
9437 && (*plo)->u.indirect.section == htab->srelplt)
9438 {
9439 lo = *plo;
9440 *plo = lo->next;
9441 }
9442 else
9443 plo = &(*plo)->next;
9444 *plo = lo;
9445 lo->next = NULL;
9446 dynamic_relocs->map_tail.link_order = lo;
9447 }
9448 }
9449
9450 p = sort;
9451 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9452 if (lo->type == bfd_indirect_link_order)
9453 {
9454 bfd_byte *erel, *erelend;
9455 asection *o = lo->u.indirect.section;
9456
9457 erel = o->contents;
9458 erelend = o->contents + o->size;
9459 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9460 while (erel < erelend)
9461 {
9462 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9463 (*swap_out) (abfd, s->rela, erel);
9464 p += sort_elt;
9465 erel += ext_size;
9466 }
9467 }
9468
9469 free (sort);
9470 *psec = dynamic_relocs;
9471 return ret;
9472 }
9473
9474 /* Add a symbol to the output symbol string table. */
9475
9476 static int
9477 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9478 const char *name,
9479 Elf_Internal_Sym *elfsym,
9480 asection *input_sec,
9481 struct elf_link_hash_entry *h)
9482 {
9483 int (*output_symbol_hook)
9484 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9485 struct elf_link_hash_entry *);
9486 struct elf_link_hash_table *hash_table;
9487 const struct elf_backend_data *bed;
9488 bfd_size_type strtabsize;
9489
9490 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9491
9492 bed = get_elf_backend_data (flinfo->output_bfd);
9493 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9494 if (output_symbol_hook != NULL)
9495 {
9496 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9497 if (ret != 1)
9498 return ret;
9499 }
9500
9501 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9502 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9503 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9504 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9505
9506 if (name == NULL
9507 || *name == '\0'
9508 || (input_sec->flags & SEC_EXCLUDE))
9509 elfsym->st_name = (unsigned long) -1;
9510 else
9511 {
9512 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9513 to get the final offset for st_name. */
9514 elfsym->st_name
9515 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9516 name, FALSE);
9517 if (elfsym->st_name == (unsigned long) -1)
9518 return 0;
9519 }
9520
9521 hash_table = elf_hash_table (flinfo->info);
9522 strtabsize = hash_table->strtabsize;
9523 if (strtabsize <= hash_table->strtabcount)
9524 {
9525 strtabsize += strtabsize;
9526 hash_table->strtabsize = strtabsize;
9527 strtabsize *= sizeof (*hash_table->strtab);
9528 hash_table->strtab
9529 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9530 strtabsize);
9531 if (hash_table->strtab == NULL)
9532 return 0;
9533 }
9534 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9535 hash_table->strtab[hash_table->strtabcount].dest_index
9536 = hash_table->strtabcount;
9537 hash_table->strtab[hash_table->strtabcount].destshndx_index
9538 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9539
9540 flinfo->output_bfd->symcount += 1;
9541 hash_table->strtabcount += 1;
9542
9543 return 1;
9544 }
9545
9546 /* Swap symbols out to the symbol table and flush the output symbols to
9547 the file. */
9548
9549 static bfd_boolean
9550 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9551 {
9552 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9553 size_t amt;
9554 size_t i;
9555 const struct elf_backend_data *bed;
9556 bfd_byte *symbuf;
9557 Elf_Internal_Shdr *hdr;
9558 file_ptr pos;
9559 bfd_boolean ret;
9560
9561 if (!hash_table->strtabcount)
9562 return TRUE;
9563
9564 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9565
9566 bed = get_elf_backend_data (flinfo->output_bfd);
9567
9568 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9569 symbuf = (bfd_byte *) bfd_malloc (amt);
9570 if (symbuf == NULL)
9571 return FALSE;
9572
9573 if (flinfo->symshndxbuf)
9574 {
9575 amt = sizeof (Elf_External_Sym_Shndx);
9576 amt *= bfd_get_symcount (flinfo->output_bfd);
9577 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9578 if (flinfo->symshndxbuf == NULL)
9579 {
9580 free (symbuf);
9581 return FALSE;
9582 }
9583 }
9584
9585 for (i = 0; i < hash_table->strtabcount; i++)
9586 {
9587 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9588 if (elfsym->sym.st_name == (unsigned long) -1)
9589 elfsym->sym.st_name = 0;
9590 else
9591 elfsym->sym.st_name
9592 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9593 elfsym->sym.st_name);
9594 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9595 ((bfd_byte *) symbuf
9596 + (elfsym->dest_index
9597 * bed->s->sizeof_sym)),
9598 (flinfo->symshndxbuf
9599 + elfsym->destshndx_index));
9600 }
9601
9602 /* Allow the linker to examine the strtab and symtab now they are
9603 populated. */
9604
9605 if (flinfo->info->callbacks->examine_strtab)
9606 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9607 hash_table->strtabcount,
9608 flinfo->symstrtab);
9609
9610 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9611 pos = hdr->sh_offset + hdr->sh_size;
9612 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9613 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9614 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9615 {
9616 hdr->sh_size += amt;
9617 ret = TRUE;
9618 }
9619 else
9620 ret = FALSE;
9621
9622 free (symbuf);
9623
9624 free (hash_table->strtab);
9625 hash_table->strtab = NULL;
9626
9627 return ret;
9628 }
9629
9630 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9631
9632 static bfd_boolean
9633 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9634 {
9635 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9636 && sym->st_shndx < SHN_LORESERVE)
9637 {
9638 /* The gABI doesn't support dynamic symbols in output sections
9639 beyond 64k. */
9640 _bfd_error_handler
9641 /* xgettext:c-format */
9642 (_("%pB: too many sections: %d (>= %d)"),
9643 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9644 bfd_set_error (bfd_error_nonrepresentable_section);
9645 return FALSE;
9646 }
9647 return TRUE;
9648 }
9649
9650 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9651 allowing an unsatisfied unversioned symbol in the DSO to match a
9652 versioned symbol that would normally require an explicit version.
9653 We also handle the case that a DSO references a hidden symbol
9654 which may be satisfied by a versioned symbol in another DSO. */
9655
9656 static bfd_boolean
9657 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9658 const struct elf_backend_data *bed,
9659 struct elf_link_hash_entry *h)
9660 {
9661 bfd *abfd;
9662 struct elf_link_loaded_list *loaded;
9663
9664 if (!is_elf_hash_table (info->hash))
9665 return FALSE;
9666
9667 /* Check indirect symbol. */
9668 while (h->root.type == bfd_link_hash_indirect)
9669 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9670
9671 switch (h->root.type)
9672 {
9673 default:
9674 abfd = NULL;
9675 break;
9676
9677 case bfd_link_hash_undefined:
9678 case bfd_link_hash_undefweak:
9679 abfd = h->root.u.undef.abfd;
9680 if (abfd == NULL
9681 || (abfd->flags & DYNAMIC) == 0
9682 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9683 return FALSE;
9684 break;
9685
9686 case bfd_link_hash_defined:
9687 case bfd_link_hash_defweak:
9688 abfd = h->root.u.def.section->owner;
9689 break;
9690
9691 case bfd_link_hash_common:
9692 abfd = h->root.u.c.p->section->owner;
9693 break;
9694 }
9695 BFD_ASSERT (abfd != NULL);
9696
9697 for (loaded = elf_hash_table (info)->dyn_loaded;
9698 loaded != NULL;
9699 loaded = loaded->next)
9700 {
9701 bfd *input;
9702 Elf_Internal_Shdr *hdr;
9703 size_t symcount;
9704 size_t extsymcount;
9705 size_t extsymoff;
9706 Elf_Internal_Shdr *versymhdr;
9707 Elf_Internal_Sym *isym;
9708 Elf_Internal_Sym *isymend;
9709 Elf_Internal_Sym *isymbuf;
9710 Elf_External_Versym *ever;
9711 Elf_External_Versym *extversym;
9712
9713 input = loaded->abfd;
9714
9715 /* We check each DSO for a possible hidden versioned definition. */
9716 if (input == abfd
9717 || elf_dynversym (input) == 0)
9718 continue;
9719
9720 hdr = &elf_tdata (input)->dynsymtab_hdr;
9721
9722 symcount = hdr->sh_size / bed->s->sizeof_sym;
9723 if (elf_bad_symtab (input))
9724 {
9725 extsymcount = symcount;
9726 extsymoff = 0;
9727 }
9728 else
9729 {
9730 extsymcount = symcount - hdr->sh_info;
9731 extsymoff = hdr->sh_info;
9732 }
9733
9734 if (extsymcount == 0)
9735 continue;
9736
9737 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9738 NULL, NULL, NULL);
9739 if (isymbuf == NULL)
9740 return FALSE;
9741
9742 /* Read in any version definitions. */
9743 versymhdr = &elf_tdata (input)->dynversym_hdr;
9744 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9745 || (extversym = (Elf_External_Versym *)
9746 _bfd_malloc_and_read (input, versymhdr->sh_size,
9747 versymhdr->sh_size)) == NULL)
9748 {
9749 free (isymbuf);
9750 return FALSE;
9751 }
9752
9753 ever = extversym + extsymoff;
9754 isymend = isymbuf + extsymcount;
9755 for (isym = isymbuf; isym < isymend; isym++, ever++)
9756 {
9757 const char *name;
9758 Elf_Internal_Versym iver;
9759 unsigned short version_index;
9760
9761 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9762 || isym->st_shndx == SHN_UNDEF)
9763 continue;
9764
9765 name = bfd_elf_string_from_elf_section (input,
9766 hdr->sh_link,
9767 isym->st_name);
9768 if (strcmp (name, h->root.root.string) != 0)
9769 continue;
9770
9771 _bfd_elf_swap_versym_in (input, ever, &iver);
9772
9773 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9774 && !(h->def_regular
9775 && h->forced_local))
9776 {
9777 /* If we have a non-hidden versioned sym, then it should
9778 have provided a definition for the undefined sym unless
9779 it is defined in a non-shared object and forced local.
9780 */
9781 abort ();
9782 }
9783
9784 version_index = iver.vs_vers & VERSYM_VERSION;
9785 if (version_index == 1 || version_index == 2)
9786 {
9787 /* This is the base or first version. We can use it. */
9788 free (extversym);
9789 free (isymbuf);
9790 return TRUE;
9791 }
9792 }
9793
9794 free (extversym);
9795 free (isymbuf);
9796 }
9797
9798 return FALSE;
9799 }
9800
9801 /* Convert ELF common symbol TYPE. */
9802
9803 static int
9804 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9805 {
9806 /* Commom symbol can only appear in relocatable link. */
9807 if (!bfd_link_relocatable (info))
9808 abort ();
9809 switch (info->elf_stt_common)
9810 {
9811 case unchanged:
9812 break;
9813 case elf_stt_common:
9814 type = STT_COMMON;
9815 break;
9816 case no_elf_stt_common:
9817 type = STT_OBJECT;
9818 break;
9819 }
9820 return type;
9821 }
9822
9823 /* Add an external symbol to the symbol table. This is called from
9824 the hash table traversal routine. When generating a shared object,
9825 we go through the symbol table twice. The first time we output
9826 anything that might have been forced to local scope in a version
9827 script. The second time we output the symbols that are still
9828 global symbols. */
9829
9830 static bfd_boolean
9831 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9832 {
9833 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9834 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9835 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9836 bfd_boolean strip;
9837 Elf_Internal_Sym sym;
9838 asection *input_sec;
9839 const struct elf_backend_data *bed;
9840 long indx;
9841 int ret;
9842 unsigned int type;
9843
9844 if (h->root.type == bfd_link_hash_warning)
9845 {
9846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9847 if (h->root.type == bfd_link_hash_new)
9848 return TRUE;
9849 }
9850
9851 /* Decide whether to output this symbol in this pass. */
9852 if (eoinfo->localsyms)
9853 {
9854 if (!h->forced_local)
9855 return TRUE;
9856 }
9857 else
9858 {
9859 if (h->forced_local)
9860 return TRUE;
9861 }
9862
9863 bed = get_elf_backend_data (flinfo->output_bfd);
9864
9865 if (h->root.type == bfd_link_hash_undefined)
9866 {
9867 /* If we have an undefined symbol reference here then it must have
9868 come from a shared library that is being linked in. (Undefined
9869 references in regular files have already been handled unless
9870 they are in unreferenced sections which are removed by garbage
9871 collection). */
9872 bfd_boolean ignore_undef = FALSE;
9873
9874 /* Some symbols may be special in that the fact that they're
9875 undefined can be safely ignored - let backend determine that. */
9876 if (bed->elf_backend_ignore_undef_symbol)
9877 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9878
9879 /* If we are reporting errors for this situation then do so now. */
9880 if (!ignore_undef
9881 && h->ref_dynamic_nonweak
9882 && (!h->ref_regular || flinfo->info->gc_sections)
9883 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9884 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9885 (*flinfo->info->callbacks->undefined_symbol)
9886 (flinfo->info, h->root.root.string,
9887 h->ref_regular ? NULL : h->root.u.undef.abfd,
9888 NULL, 0,
9889 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9890
9891 /* Strip a global symbol defined in a discarded section. */
9892 if (h->indx == -3)
9893 return TRUE;
9894 }
9895
9896 /* We should also warn if a forced local symbol is referenced from
9897 shared libraries. */
9898 if (bfd_link_executable (flinfo->info)
9899 && h->forced_local
9900 && h->ref_dynamic
9901 && h->def_regular
9902 && !h->dynamic_def
9903 && h->ref_dynamic_nonweak
9904 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9905 {
9906 bfd *def_bfd;
9907 const char *msg;
9908 struct elf_link_hash_entry *hi = h;
9909
9910 /* Check indirect symbol. */
9911 while (hi->root.type == bfd_link_hash_indirect)
9912 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9913
9914 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9915 /* xgettext:c-format */
9916 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9917 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9918 /* xgettext:c-format */
9919 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9920 else
9921 /* xgettext:c-format */
9922 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9923 def_bfd = flinfo->output_bfd;
9924 if (hi->root.u.def.section != bfd_abs_section_ptr)
9925 def_bfd = hi->root.u.def.section->owner;
9926 _bfd_error_handler (msg, flinfo->output_bfd,
9927 h->root.root.string, def_bfd);
9928 bfd_set_error (bfd_error_bad_value);
9929 eoinfo->failed = TRUE;
9930 return FALSE;
9931 }
9932
9933 /* We don't want to output symbols that have never been mentioned by
9934 a regular file, or that we have been told to strip. However, if
9935 h->indx is set to -2, the symbol is used by a reloc and we must
9936 output it. */
9937 strip = FALSE;
9938 if (h->indx == -2)
9939 ;
9940 else if ((h->def_dynamic
9941 || h->ref_dynamic
9942 || h->root.type == bfd_link_hash_new)
9943 && !h->def_regular
9944 && !h->ref_regular)
9945 strip = TRUE;
9946 else if (flinfo->info->strip == strip_all)
9947 strip = TRUE;
9948 else if (flinfo->info->strip == strip_some
9949 && bfd_hash_lookup (flinfo->info->keep_hash,
9950 h->root.root.string, FALSE, FALSE) == NULL)
9951 strip = TRUE;
9952 else if ((h->root.type == bfd_link_hash_defined
9953 || h->root.type == bfd_link_hash_defweak)
9954 && ((flinfo->info->strip_discarded
9955 && discarded_section (h->root.u.def.section))
9956 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9957 && h->root.u.def.section->owner != NULL
9958 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9959 strip = TRUE;
9960 else if ((h->root.type == bfd_link_hash_undefined
9961 || h->root.type == bfd_link_hash_undefweak)
9962 && h->root.u.undef.abfd != NULL
9963 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9964 strip = TRUE;
9965
9966 type = h->type;
9967
9968 /* If we're stripping it, and it's not a dynamic symbol, there's
9969 nothing else to do. However, if it is a forced local symbol or
9970 an ifunc symbol we need to give the backend finish_dynamic_symbol
9971 function a chance to make it dynamic. */
9972 if (strip
9973 && h->dynindx == -1
9974 && type != STT_GNU_IFUNC
9975 && !h->forced_local)
9976 return TRUE;
9977
9978 sym.st_value = 0;
9979 sym.st_size = h->size;
9980 sym.st_other = h->other;
9981 switch (h->root.type)
9982 {
9983 default:
9984 case bfd_link_hash_new:
9985 case bfd_link_hash_warning:
9986 abort ();
9987 return FALSE;
9988
9989 case bfd_link_hash_undefined:
9990 case bfd_link_hash_undefweak:
9991 input_sec = bfd_und_section_ptr;
9992 sym.st_shndx = SHN_UNDEF;
9993 break;
9994
9995 case bfd_link_hash_defined:
9996 case bfd_link_hash_defweak:
9997 {
9998 input_sec = h->root.u.def.section;
9999 if (input_sec->output_section != NULL)
10000 {
10001 sym.st_shndx =
10002 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10003 input_sec->output_section);
10004 if (sym.st_shndx == SHN_BAD)
10005 {
10006 _bfd_error_handler
10007 /* xgettext:c-format */
10008 (_("%pB: could not find output section %pA for input section %pA"),
10009 flinfo->output_bfd, input_sec->output_section, input_sec);
10010 bfd_set_error (bfd_error_nonrepresentable_section);
10011 eoinfo->failed = TRUE;
10012 return FALSE;
10013 }
10014
10015 /* ELF symbols in relocatable files are section relative,
10016 but in nonrelocatable files they are virtual
10017 addresses. */
10018 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10019 if (!bfd_link_relocatable (flinfo->info))
10020 {
10021 sym.st_value += input_sec->output_section->vma;
10022 if (h->type == STT_TLS)
10023 {
10024 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10025 if (tls_sec != NULL)
10026 sym.st_value -= tls_sec->vma;
10027 }
10028 }
10029 }
10030 else
10031 {
10032 BFD_ASSERT (input_sec->owner == NULL
10033 || (input_sec->owner->flags & DYNAMIC) != 0);
10034 sym.st_shndx = SHN_UNDEF;
10035 input_sec = bfd_und_section_ptr;
10036 }
10037 }
10038 break;
10039
10040 case bfd_link_hash_common:
10041 input_sec = h->root.u.c.p->section;
10042 sym.st_shndx = bed->common_section_index (input_sec);
10043 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10044 break;
10045
10046 case bfd_link_hash_indirect:
10047 /* These symbols are created by symbol versioning. They point
10048 to the decorated version of the name. For example, if the
10049 symbol foo@@GNU_1.2 is the default, which should be used when
10050 foo is used with no version, then we add an indirect symbol
10051 foo which points to foo@@GNU_1.2. We ignore these symbols,
10052 since the indirected symbol is already in the hash table. */
10053 return TRUE;
10054 }
10055
10056 if (type == STT_COMMON || type == STT_OBJECT)
10057 switch (h->root.type)
10058 {
10059 case bfd_link_hash_common:
10060 type = elf_link_convert_common_type (flinfo->info, type);
10061 break;
10062 case bfd_link_hash_defined:
10063 case bfd_link_hash_defweak:
10064 if (bed->common_definition (&sym))
10065 type = elf_link_convert_common_type (flinfo->info, type);
10066 else
10067 type = STT_OBJECT;
10068 break;
10069 case bfd_link_hash_undefined:
10070 case bfd_link_hash_undefweak:
10071 break;
10072 default:
10073 abort ();
10074 }
10075
10076 if (h->forced_local)
10077 {
10078 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10079 /* Turn off visibility on local symbol. */
10080 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10081 }
10082 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10083 else if (h->unique_global && h->def_regular)
10084 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10085 else if (h->root.type == bfd_link_hash_undefweak
10086 || h->root.type == bfd_link_hash_defweak)
10087 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10088 else
10089 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10090 sym.st_target_internal = h->target_internal;
10091
10092 /* Give the processor backend a chance to tweak the symbol value,
10093 and also to finish up anything that needs to be done for this
10094 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10095 forced local syms when non-shared is due to a historical quirk.
10096 STT_GNU_IFUNC symbol must go through PLT. */
10097 if ((h->type == STT_GNU_IFUNC
10098 && h->def_regular
10099 && !bfd_link_relocatable (flinfo->info))
10100 || ((h->dynindx != -1
10101 || h->forced_local)
10102 && ((bfd_link_pic (flinfo->info)
10103 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10104 || h->root.type != bfd_link_hash_undefweak))
10105 || !h->forced_local)
10106 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10107 {
10108 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10109 (flinfo->output_bfd, flinfo->info, h, &sym)))
10110 {
10111 eoinfo->failed = TRUE;
10112 return FALSE;
10113 }
10114 }
10115
10116 /* If we are marking the symbol as undefined, and there are no
10117 non-weak references to this symbol from a regular object, then
10118 mark the symbol as weak undefined; if there are non-weak
10119 references, mark the symbol as strong. We can't do this earlier,
10120 because it might not be marked as undefined until the
10121 finish_dynamic_symbol routine gets through with it. */
10122 if (sym.st_shndx == SHN_UNDEF
10123 && h->ref_regular
10124 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10125 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10126 {
10127 int bindtype;
10128 type = ELF_ST_TYPE (sym.st_info);
10129
10130 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10131 if (type == STT_GNU_IFUNC)
10132 type = STT_FUNC;
10133
10134 if (h->ref_regular_nonweak)
10135 bindtype = STB_GLOBAL;
10136 else
10137 bindtype = STB_WEAK;
10138 sym.st_info = ELF_ST_INFO (bindtype, type);
10139 }
10140
10141 /* If this is a symbol defined in a dynamic library, don't use the
10142 symbol size from the dynamic library. Relinking an executable
10143 against a new library may introduce gratuitous changes in the
10144 executable's symbols if we keep the size. */
10145 if (sym.st_shndx == SHN_UNDEF
10146 && !h->def_regular
10147 && h->def_dynamic)
10148 sym.st_size = 0;
10149
10150 /* If a non-weak symbol with non-default visibility is not defined
10151 locally, it is a fatal error. */
10152 if (!bfd_link_relocatable (flinfo->info)
10153 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10154 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10155 && h->root.type == bfd_link_hash_undefined
10156 && !h->def_regular)
10157 {
10158 const char *msg;
10159
10160 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10161 /* xgettext:c-format */
10162 msg = _("%pB: protected symbol `%s' isn't defined");
10163 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10164 /* xgettext:c-format */
10165 msg = _("%pB: internal symbol `%s' isn't defined");
10166 else
10167 /* xgettext:c-format */
10168 msg = _("%pB: hidden symbol `%s' isn't defined");
10169 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10170 bfd_set_error (bfd_error_bad_value);
10171 eoinfo->failed = TRUE;
10172 return FALSE;
10173 }
10174
10175 /* If this symbol should be put in the .dynsym section, then put it
10176 there now. We already know the symbol index. We also fill in
10177 the entry in the .hash section. */
10178 if (h->dynindx != -1
10179 && elf_hash_table (flinfo->info)->dynamic_sections_created
10180 && elf_hash_table (flinfo->info)->dynsym != NULL
10181 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10182 {
10183 bfd_byte *esym;
10184
10185 /* Since there is no version information in the dynamic string,
10186 if there is no version info in symbol version section, we will
10187 have a run-time problem if not linking executable, referenced
10188 by shared library, or not bound locally. */
10189 if (h->verinfo.verdef == NULL
10190 && (!bfd_link_executable (flinfo->info)
10191 || h->ref_dynamic
10192 || !h->def_regular))
10193 {
10194 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10195
10196 if (p && p [1] != '\0')
10197 {
10198 _bfd_error_handler
10199 /* xgettext:c-format */
10200 (_("%pB: no symbol version section for versioned symbol `%s'"),
10201 flinfo->output_bfd, h->root.root.string);
10202 eoinfo->failed = TRUE;
10203 return FALSE;
10204 }
10205 }
10206
10207 sym.st_name = h->dynstr_index;
10208 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10209 + h->dynindx * bed->s->sizeof_sym);
10210 if (!check_dynsym (flinfo->output_bfd, &sym))
10211 {
10212 eoinfo->failed = TRUE;
10213 return FALSE;
10214 }
10215 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10216
10217 if (flinfo->hash_sec != NULL)
10218 {
10219 size_t hash_entry_size;
10220 bfd_byte *bucketpos;
10221 bfd_vma chain;
10222 size_t bucketcount;
10223 size_t bucket;
10224
10225 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10226 bucket = h->u.elf_hash_value % bucketcount;
10227
10228 hash_entry_size
10229 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10230 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10231 + (bucket + 2) * hash_entry_size);
10232 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10233 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10234 bucketpos);
10235 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10236 ((bfd_byte *) flinfo->hash_sec->contents
10237 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10238 }
10239
10240 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10241 {
10242 Elf_Internal_Versym iversym;
10243 Elf_External_Versym *eversym;
10244
10245 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10246 {
10247 if (h->verinfo.verdef == NULL
10248 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10249 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10250 iversym.vs_vers = 0;
10251 else
10252 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10253 }
10254 else
10255 {
10256 if (h->verinfo.vertree == NULL)
10257 iversym.vs_vers = 1;
10258 else
10259 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10260 if (flinfo->info->create_default_symver)
10261 iversym.vs_vers++;
10262 }
10263
10264 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10265 defined locally. */
10266 if (h->versioned == versioned_hidden && h->def_regular)
10267 iversym.vs_vers |= VERSYM_HIDDEN;
10268
10269 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10270 eversym += h->dynindx;
10271 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10272 }
10273 }
10274
10275 /* If the symbol is undefined, and we didn't output it to .dynsym,
10276 strip it from .symtab too. Obviously we can't do this for
10277 relocatable output or when needed for --emit-relocs. */
10278 else if (input_sec == bfd_und_section_ptr
10279 && h->indx != -2
10280 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10281 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10282 && !bfd_link_relocatable (flinfo->info))
10283 return TRUE;
10284
10285 /* Also strip others that we couldn't earlier due to dynamic symbol
10286 processing. */
10287 if (strip)
10288 return TRUE;
10289 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10290 return TRUE;
10291
10292 /* Output a FILE symbol so that following locals are not associated
10293 with the wrong input file. We need one for forced local symbols
10294 if we've seen more than one FILE symbol or when we have exactly
10295 one FILE symbol but global symbols are present in a file other
10296 than the one with the FILE symbol. We also need one if linker
10297 defined symbols are present. In practice these conditions are
10298 always met, so just emit the FILE symbol unconditionally. */
10299 if (eoinfo->localsyms
10300 && !eoinfo->file_sym_done
10301 && eoinfo->flinfo->filesym_count != 0)
10302 {
10303 Elf_Internal_Sym fsym;
10304
10305 memset (&fsym, 0, sizeof (fsym));
10306 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10307 fsym.st_shndx = SHN_ABS;
10308 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10309 bfd_und_section_ptr, NULL))
10310 return FALSE;
10311
10312 eoinfo->file_sym_done = TRUE;
10313 }
10314
10315 indx = bfd_get_symcount (flinfo->output_bfd);
10316 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10317 input_sec, h);
10318 if (ret == 0)
10319 {
10320 eoinfo->failed = TRUE;
10321 return FALSE;
10322 }
10323 else if (ret == 1)
10324 h->indx = indx;
10325 else if (h->indx == -2)
10326 abort();
10327
10328 return TRUE;
10329 }
10330
10331 /* Return TRUE if special handling is done for relocs in SEC against
10332 symbols defined in discarded sections. */
10333
10334 static bfd_boolean
10335 elf_section_ignore_discarded_relocs (asection *sec)
10336 {
10337 const struct elf_backend_data *bed;
10338
10339 switch (sec->sec_info_type)
10340 {
10341 case SEC_INFO_TYPE_STABS:
10342 case SEC_INFO_TYPE_EH_FRAME:
10343 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10344 return TRUE;
10345 default:
10346 break;
10347 }
10348
10349 bed = get_elf_backend_data (sec->owner);
10350 if (bed->elf_backend_ignore_discarded_relocs != NULL
10351 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10352 return TRUE;
10353
10354 return FALSE;
10355 }
10356
10357 /* Return a mask saying how ld should treat relocations in SEC against
10358 symbols defined in discarded sections. If this function returns
10359 COMPLAIN set, ld will issue a warning message. If this function
10360 returns PRETEND set, and the discarded section was link-once and the
10361 same size as the kept link-once section, ld will pretend that the
10362 symbol was actually defined in the kept section. Otherwise ld will
10363 zero the reloc (at least that is the intent, but some cooperation by
10364 the target dependent code is needed, particularly for REL targets). */
10365
10366 unsigned int
10367 _bfd_elf_default_action_discarded (asection *sec)
10368 {
10369 if (sec->flags & SEC_DEBUGGING)
10370 return PRETEND;
10371
10372 if (strcmp (".eh_frame", sec->name) == 0)
10373 return 0;
10374
10375 if (strcmp (".gcc_except_table", sec->name) == 0)
10376 return 0;
10377
10378 return COMPLAIN | PRETEND;
10379 }
10380
10381 /* Find a match between a section and a member of a section group. */
10382
10383 static asection *
10384 match_group_member (asection *sec, asection *group,
10385 struct bfd_link_info *info)
10386 {
10387 asection *first = elf_next_in_group (group);
10388 asection *s = first;
10389
10390 while (s != NULL)
10391 {
10392 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10393 return s;
10394
10395 s = elf_next_in_group (s);
10396 if (s == first)
10397 break;
10398 }
10399
10400 return NULL;
10401 }
10402
10403 /* Check if the kept section of a discarded section SEC can be used
10404 to replace it. Return the replacement if it is OK. Otherwise return
10405 NULL. */
10406
10407 asection *
10408 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10409 {
10410 asection *kept;
10411
10412 kept = sec->kept_section;
10413 if (kept != NULL)
10414 {
10415 if ((kept->flags & SEC_GROUP) != 0)
10416 kept = match_group_member (sec, kept, info);
10417 if (kept != NULL
10418 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10419 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10420 kept = NULL;
10421 sec->kept_section = kept;
10422 }
10423 return kept;
10424 }
10425
10426 /* Link an input file into the linker output file. This function
10427 handles all the sections and relocations of the input file at once.
10428 This is so that we only have to read the local symbols once, and
10429 don't have to keep them in memory. */
10430
10431 static bfd_boolean
10432 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10433 {
10434 int (*relocate_section)
10435 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10436 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10437 bfd *output_bfd;
10438 Elf_Internal_Shdr *symtab_hdr;
10439 size_t locsymcount;
10440 size_t extsymoff;
10441 Elf_Internal_Sym *isymbuf;
10442 Elf_Internal_Sym *isym;
10443 Elf_Internal_Sym *isymend;
10444 long *pindex;
10445 asection **ppsection;
10446 asection *o;
10447 const struct elf_backend_data *bed;
10448 struct elf_link_hash_entry **sym_hashes;
10449 bfd_size_type address_size;
10450 bfd_vma r_type_mask;
10451 int r_sym_shift;
10452 bfd_boolean have_file_sym = FALSE;
10453
10454 output_bfd = flinfo->output_bfd;
10455 bed = get_elf_backend_data (output_bfd);
10456 relocate_section = bed->elf_backend_relocate_section;
10457
10458 /* If this is a dynamic object, we don't want to do anything here:
10459 we don't want the local symbols, and we don't want the section
10460 contents. */
10461 if ((input_bfd->flags & DYNAMIC) != 0)
10462 return TRUE;
10463
10464 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10465 if (elf_bad_symtab (input_bfd))
10466 {
10467 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10468 extsymoff = 0;
10469 }
10470 else
10471 {
10472 locsymcount = symtab_hdr->sh_info;
10473 extsymoff = symtab_hdr->sh_info;
10474 }
10475
10476 /* Read the local symbols. */
10477 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10478 if (isymbuf == NULL && locsymcount != 0)
10479 {
10480 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10481 flinfo->internal_syms,
10482 flinfo->external_syms,
10483 flinfo->locsym_shndx);
10484 if (isymbuf == NULL)
10485 return FALSE;
10486 }
10487
10488 /* Find local symbol sections and adjust values of symbols in
10489 SEC_MERGE sections. Write out those local symbols we know are
10490 going into the output file. */
10491 isymend = isymbuf + locsymcount;
10492 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10493 isym < isymend;
10494 isym++, pindex++, ppsection++)
10495 {
10496 asection *isec;
10497 const char *name;
10498 Elf_Internal_Sym osym;
10499 long indx;
10500 int ret;
10501
10502 *pindex = -1;
10503
10504 if (elf_bad_symtab (input_bfd))
10505 {
10506 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10507 {
10508 *ppsection = NULL;
10509 continue;
10510 }
10511 }
10512
10513 if (isym->st_shndx == SHN_UNDEF)
10514 isec = bfd_und_section_ptr;
10515 else if (isym->st_shndx == SHN_ABS)
10516 isec = bfd_abs_section_ptr;
10517 else if (isym->st_shndx == SHN_COMMON)
10518 isec = bfd_com_section_ptr;
10519 else
10520 {
10521 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10522 if (isec == NULL)
10523 {
10524 /* Don't attempt to output symbols with st_shnx in the
10525 reserved range other than SHN_ABS and SHN_COMMON. */
10526 isec = bfd_und_section_ptr;
10527 }
10528 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10529 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10530 isym->st_value =
10531 _bfd_merged_section_offset (output_bfd, &isec,
10532 elf_section_data (isec)->sec_info,
10533 isym->st_value);
10534 }
10535
10536 *ppsection = isec;
10537
10538 /* Don't output the first, undefined, symbol. In fact, don't
10539 output any undefined local symbol. */
10540 if (isec == bfd_und_section_ptr)
10541 continue;
10542
10543 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10544 {
10545 /* We never output section symbols. Instead, we use the
10546 section symbol of the corresponding section in the output
10547 file. */
10548 continue;
10549 }
10550
10551 /* If we are stripping all symbols, we don't want to output this
10552 one. */
10553 if (flinfo->info->strip == strip_all)
10554 continue;
10555
10556 /* If we are discarding all local symbols, we don't want to
10557 output this one. If we are generating a relocatable output
10558 file, then some of the local symbols may be required by
10559 relocs; we output them below as we discover that they are
10560 needed. */
10561 if (flinfo->info->discard == discard_all)
10562 continue;
10563
10564 /* If this symbol is defined in a section which we are
10565 discarding, we don't need to keep it. */
10566 if (isym->st_shndx != SHN_UNDEF
10567 && isym->st_shndx < SHN_LORESERVE
10568 && bfd_section_removed_from_list (output_bfd,
10569 isec->output_section))
10570 continue;
10571
10572 /* Get the name of the symbol. */
10573 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10574 isym->st_name);
10575 if (name == NULL)
10576 return FALSE;
10577
10578 /* See if we are discarding symbols with this name. */
10579 if ((flinfo->info->strip == strip_some
10580 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10581 == NULL))
10582 || (((flinfo->info->discard == discard_sec_merge
10583 && (isec->flags & SEC_MERGE)
10584 && !bfd_link_relocatable (flinfo->info))
10585 || flinfo->info->discard == discard_l)
10586 && bfd_is_local_label_name (input_bfd, name)))
10587 continue;
10588
10589 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10590 {
10591 if (input_bfd->lto_output)
10592 /* -flto puts a temp file name here. This means builds
10593 are not reproducible. Discard the symbol. */
10594 continue;
10595 have_file_sym = TRUE;
10596 flinfo->filesym_count += 1;
10597 }
10598 if (!have_file_sym)
10599 {
10600 /* In the absence of debug info, bfd_find_nearest_line uses
10601 FILE symbols to determine the source file for local
10602 function symbols. Provide a FILE symbol here if input
10603 files lack such, so that their symbols won't be
10604 associated with a previous input file. It's not the
10605 source file, but the best we can do. */
10606 have_file_sym = TRUE;
10607 flinfo->filesym_count += 1;
10608 memset (&osym, 0, sizeof (osym));
10609 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10610 osym.st_shndx = SHN_ABS;
10611 if (!elf_link_output_symstrtab (flinfo,
10612 (input_bfd->lto_output ? NULL
10613 : input_bfd->filename),
10614 &osym, bfd_abs_section_ptr,
10615 NULL))
10616 return FALSE;
10617 }
10618
10619 osym = *isym;
10620
10621 /* Adjust the section index for the output file. */
10622 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10623 isec->output_section);
10624 if (osym.st_shndx == SHN_BAD)
10625 return FALSE;
10626
10627 /* ELF symbols in relocatable files are section relative, but
10628 in executable files they are virtual addresses. Note that
10629 this code assumes that all ELF sections have an associated
10630 BFD section with a reasonable value for output_offset; below
10631 we assume that they also have a reasonable value for
10632 output_section. Any special sections must be set up to meet
10633 these requirements. */
10634 osym.st_value += isec->output_offset;
10635 if (!bfd_link_relocatable (flinfo->info))
10636 {
10637 osym.st_value += isec->output_section->vma;
10638 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10639 {
10640 /* STT_TLS symbols are relative to PT_TLS segment base. */
10641 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10642 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10643 else
10644 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10645 STT_NOTYPE);
10646 }
10647 }
10648
10649 indx = bfd_get_symcount (output_bfd);
10650 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10651 if (ret == 0)
10652 return FALSE;
10653 else if (ret == 1)
10654 *pindex = indx;
10655 }
10656
10657 if (bed->s->arch_size == 32)
10658 {
10659 r_type_mask = 0xff;
10660 r_sym_shift = 8;
10661 address_size = 4;
10662 }
10663 else
10664 {
10665 r_type_mask = 0xffffffff;
10666 r_sym_shift = 32;
10667 address_size = 8;
10668 }
10669
10670 /* Relocate the contents of each section. */
10671 sym_hashes = elf_sym_hashes (input_bfd);
10672 for (o = input_bfd->sections; o != NULL; o = o->next)
10673 {
10674 bfd_byte *contents;
10675
10676 if (! o->linker_mark)
10677 {
10678 /* This section was omitted from the link. */
10679 continue;
10680 }
10681
10682 if (!flinfo->info->resolve_section_groups
10683 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10684 {
10685 /* Deal with the group signature symbol. */
10686 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10687 unsigned long symndx = sec_data->this_hdr.sh_info;
10688 asection *osec = o->output_section;
10689
10690 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10691 if (symndx >= locsymcount
10692 || (elf_bad_symtab (input_bfd)
10693 && flinfo->sections[symndx] == NULL))
10694 {
10695 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10696 while (h->root.type == bfd_link_hash_indirect
10697 || h->root.type == bfd_link_hash_warning)
10698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10699 /* Arrange for symbol to be output. */
10700 h->indx = -2;
10701 elf_section_data (osec)->this_hdr.sh_info = -2;
10702 }
10703 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10704 {
10705 /* We'll use the output section target_index. */
10706 asection *sec = flinfo->sections[symndx]->output_section;
10707 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10708 }
10709 else
10710 {
10711 if (flinfo->indices[symndx] == -1)
10712 {
10713 /* Otherwise output the local symbol now. */
10714 Elf_Internal_Sym sym = isymbuf[symndx];
10715 asection *sec = flinfo->sections[symndx]->output_section;
10716 const char *name;
10717 long indx;
10718 int ret;
10719
10720 name = bfd_elf_string_from_elf_section (input_bfd,
10721 symtab_hdr->sh_link,
10722 sym.st_name);
10723 if (name == NULL)
10724 return FALSE;
10725
10726 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10727 sec);
10728 if (sym.st_shndx == SHN_BAD)
10729 return FALSE;
10730
10731 sym.st_value += o->output_offset;
10732
10733 indx = bfd_get_symcount (output_bfd);
10734 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10735 NULL);
10736 if (ret == 0)
10737 return FALSE;
10738 else if (ret == 1)
10739 flinfo->indices[symndx] = indx;
10740 else
10741 abort ();
10742 }
10743 elf_section_data (osec)->this_hdr.sh_info
10744 = flinfo->indices[symndx];
10745 }
10746 }
10747
10748 if ((o->flags & SEC_HAS_CONTENTS) == 0
10749 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10750 continue;
10751
10752 if ((o->flags & SEC_LINKER_CREATED) != 0)
10753 {
10754 /* Section was created by _bfd_elf_link_create_dynamic_sections
10755 or somesuch. */
10756 continue;
10757 }
10758
10759 /* Get the contents of the section. They have been cached by a
10760 relaxation routine. Note that o is a section in an input
10761 file, so the contents field will not have been set by any of
10762 the routines which work on output files. */
10763 if (elf_section_data (o)->this_hdr.contents != NULL)
10764 {
10765 contents = elf_section_data (o)->this_hdr.contents;
10766 if (bed->caches_rawsize
10767 && o->rawsize != 0
10768 && o->rawsize < o->size)
10769 {
10770 memcpy (flinfo->contents, contents, o->rawsize);
10771 contents = flinfo->contents;
10772 }
10773 }
10774 else
10775 {
10776 contents = flinfo->contents;
10777 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10778 return FALSE;
10779 }
10780
10781 if ((o->flags & SEC_RELOC) != 0)
10782 {
10783 Elf_Internal_Rela *internal_relocs;
10784 Elf_Internal_Rela *rel, *relend;
10785 int action_discarded;
10786 int ret;
10787
10788 /* Get the swapped relocs. */
10789 internal_relocs
10790 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10791 flinfo->internal_relocs, FALSE);
10792 if (internal_relocs == NULL
10793 && o->reloc_count > 0)
10794 return FALSE;
10795
10796 /* We need to reverse-copy input .ctors/.dtors sections if
10797 they are placed in .init_array/.finit_array for output. */
10798 if (o->size > address_size
10799 && ((strncmp (o->name, ".ctors", 6) == 0
10800 && strcmp (o->output_section->name,
10801 ".init_array") == 0)
10802 || (strncmp (o->name, ".dtors", 6) == 0
10803 && strcmp (o->output_section->name,
10804 ".fini_array") == 0))
10805 && (o->name[6] == 0 || o->name[6] == '.'))
10806 {
10807 if (o->size * bed->s->int_rels_per_ext_rel
10808 != o->reloc_count * address_size)
10809 {
10810 _bfd_error_handler
10811 /* xgettext:c-format */
10812 (_("error: %pB: size of section %pA is not "
10813 "multiple of address size"),
10814 input_bfd, o);
10815 bfd_set_error (bfd_error_bad_value);
10816 return FALSE;
10817 }
10818 o->flags |= SEC_ELF_REVERSE_COPY;
10819 }
10820
10821 action_discarded = -1;
10822 if (!elf_section_ignore_discarded_relocs (o))
10823 action_discarded = (*bed->action_discarded) (o);
10824
10825 /* Run through the relocs evaluating complex reloc symbols and
10826 looking for relocs against symbols from discarded sections
10827 or section symbols from removed link-once sections.
10828 Complain about relocs against discarded sections. Zero
10829 relocs against removed link-once sections. */
10830
10831 rel = internal_relocs;
10832 relend = rel + o->reloc_count;
10833 for ( ; rel < relend; rel++)
10834 {
10835 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10836 unsigned int s_type;
10837 asection **ps, *sec;
10838 struct elf_link_hash_entry *h = NULL;
10839 const char *sym_name;
10840
10841 if (r_symndx == STN_UNDEF)
10842 continue;
10843
10844 if (r_symndx >= locsymcount
10845 || (elf_bad_symtab (input_bfd)
10846 && flinfo->sections[r_symndx] == NULL))
10847 {
10848 h = sym_hashes[r_symndx - extsymoff];
10849
10850 /* Badly formatted input files can contain relocs that
10851 reference non-existant symbols. Check here so that
10852 we do not seg fault. */
10853 if (h == NULL)
10854 {
10855 _bfd_error_handler
10856 /* xgettext:c-format */
10857 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10858 "that references a non-existent global symbol"),
10859 input_bfd, (uint64_t) rel->r_info, o);
10860 bfd_set_error (bfd_error_bad_value);
10861 return FALSE;
10862 }
10863
10864 while (h->root.type == bfd_link_hash_indirect
10865 || h->root.type == bfd_link_hash_warning)
10866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10867
10868 s_type = h->type;
10869
10870 /* If a plugin symbol is referenced from a non-IR file,
10871 mark the symbol as undefined. Note that the
10872 linker may attach linker created dynamic sections
10873 to the plugin bfd. Symbols defined in linker
10874 created sections are not plugin symbols. */
10875 if ((h->root.non_ir_ref_regular
10876 || h->root.non_ir_ref_dynamic)
10877 && (h->root.type == bfd_link_hash_defined
10878 || h->root.type == bfd_link_hash_defweak)
10879 && (h->root.u.def.section->flags
10880 & SEC_LINKER_CREATED) == 0
10881 && h->root.u.def.section->owner != NULL
10882 && (h->root.u.def.section->owner->flags
10883 & BFD_PLUGIN) != 0)
10884 {
10885 h->root.type = bfd_link_hash_undefined;
10886 h->root.u.undef.abfd = h->root.u.def.section->owner;
10887 }
10888
10889 ps = NULL;
10890 if (h->root.type == bfd_link_hash_defined
10891 || h->root.type == bfd_link_hash_defweak)
10892 ps = &h->root.u.def.section;
10893
10894 sym_name = h->root.root.string;
10895 }
10896 else
10897 {
10898 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10899
10900 s_type = ELF_ST_TYPE (sym->st_info);
10901 ps = &flinfo->sections[r_symndx];
10902 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10903 sym, *ps);
10904 }
10905
10906 if ((s_type == STT_RELC || s_type == STT_SRELC)
10907 && !bfd_link_relocatable (flinfo->info))
10908 {
10909 bfd_vma val;
10910 bfd_vma dot = (rel->r_offset
10911 + o->output_offset + o->output_section->vma);
10912 #ifdef DEBUG
10913 printf ("Encountered a complex symbol!");
10914 printf (" (input_bfd %s, section %s, reloc %ld\n",
10915 input_bfd->filename, o->name,
10916 (long) (rel - internal_relocs));
10917 printf (" symbol: idx %8.8lx, name %s\n",
10918 r_symndx, sym_name);
10919 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10920 (unsigned long) rel->r_info,
10921 (unsigned long) rel->r_offset);
10922 #endif
10923 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10924 isymbuf, locsymcount, s_type == STT_SRELC))
10925 return FALSE;
10926
10927 /* Symbol evaluated OK. Update to absolute value. */
10928 set_symbol_value (input_bfd, isymbuf, locsymcount,
10929 r_symndx, val);
10930 continue;
10931 }
10932
10933 if (action_discarded != -1 && ps != NULL)
10934 {
10935 /* Complain if the definition comes from a
10936 discarded section. */
10937 if ((sec = *ps) != NULL && discarded_section (sec))
10938 {
10939 BFD_ASSERT (r_symndx != STN_UNDEF);
10940 if (action_discarded & COMPLAIN)
10941 (*flinfo->info->callbacks->einfo)
10942 /* xgettext:c-format */
10943 (_("%X`%s' referenced in section `%pA' of %pB: "
10944 "defined in discarded section `%pA' of %pB\n"),
10945 sym_name, o, input_bfd, sec, sec->owner);
10946
10947 /* Try to do the best we can to support buggy old
10948 versions of gcc. Pretend that the symbol is
10949 really defined in the kept linkonce section.
10950 FIXME: This is quite broken. Modifying the
10951 symbol here means we will be changing all later
10952 uses of the symbol, not just in this section. */
10953 if (action_discarded & PRETEND)
10954 {
10955 asection *kept;
10956
10957 kept = _bfd_elf_check_kept_section (sec,
10958 flinfo->info);
10959 if (kept != NULL)
10960 {
10961 *ps = kept;
10962 continue;
10963 }
10964 }
10965 }
10966 }
10967 }
10968
10969 /* Relocate the section by invoking a back end routine.
10970
10971 The back end routine is responsible for adjusting the
10972 section contents as necessary, and (if using Rela relocs
10973 and generating a relocatable output file) adjusting the
10974 reloc addend as necessary.
10975
10976 The back end routine does not have to worry about setting
10977 the reloc address or the reloc symbol index.
10978
10979 The back end routine is given a pointer to the swapped in
10980 internal symbols, and can access the hash table entries
10981 for the external symbols via elf_sym_hashes (input_bfd).
10982
10983 When generating relocatable output, the back end routine
10984 must handle STB_LOCAL/STT_SECTION symbols specially. The
10985 output symbol is going to be a section symbol
10986 corresponding to the output section, which will require
10987 the addend to be adjusted. */
10988
10989 ret = (*relocate_section) (output_bfd, flinfo->info,
10990 input_bfd, o, contents,
10991 internal_relocs,
10992 isymbuf,
10993 flinfo->sections);
10994 if (!ret)
10995 return FALSE;
10996
10997 if (ret == 2
10998 || bfd_link_relocatable (flinfo->info)
10999 || flinfo->info->emitrelocations)
11000 {
11001 Elf_Internal_Rela *irela;
11002 Elf_Internal_Rela *irelaend, *irelamid;
11003 bfd_vma last_offset;
11004 struct elf_link_hash_entry **rel_hash;
11005 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11006 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11007 unsigned int next_erel;
11008 bfd_boolean rela_normal;
11009 struct bfd_elf_section_data *esdi, *esdo;
11010
11011 esdi = elf_section_data (o);
11012 esdo = elf_section_data (o->output_section);
11013 rela_normal = FALSE;
11014
11015 /* Adjust the reloc addresses and symbol indices. */
11016
11017 irela = internal_relocs;
11018 irelaend = irela + o->reloc_count;
11019 rel_hash = esdo->rel.hashes + esdo->rel.count;
11020 /* We start processing the REL relocs, if any. When we reach
11021 IRELAMID in the loop, we switch to the RELA relocs. */
11022 irelamid = irela;
11023 if (esdi->rel.hdr != NULL)
11024 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11025 * bed->s->int_rels_per_ext_rel);
11026 rel_hash_list = rel_hash;
11027 rela_hash_list = NULL;
11028 last_offset = o->output_offset;
11029 if (!bfd_link_relocatable (flinfo->info))
11030 last_offset += o->output_section->vma;
11031 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11032 {
11033 unsigned long r_symndx;
11034 asection *sec;
11035 Elf_Internal_Sym sym;
11036
11037 if (next_erel == bed->s->int_rels_per_ext_rel)
11038 {
11039 rel_hash++;
11040 next_erel = 0;
11041 }
11042
11043 if (irela == irelamid)
11044 {
11045 rel_hash = esdo->rela.hashes + esdo->rela.count;
11046 rela_hash_list = rel_hash;
11047 rela_normal = bed->rela_normal;
11048 }
11049
11050 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11051 flinfo->info, o,
11052 irela->r_offset);
11053 if (irela->r_offset >= (bfd_vma) -2)
11054 {
11055 /* This is a reloc for a deleted entry or somesuch.
11056 Turn it into an R_*_NONE reloc, at the same
11057 offset as the last reloc. elf_eh_frame.c and
11058 bfd_elf_discard_info rely on reloc offsets
11059 being ordered. */
11060 irela->r_offset = last_offset;
11061 irela->r_info = 0;
11062 irela->r_addend = 0;
11063 continue;
11064 }
11065
11066 irela->r_offset += o->output_offset;
11067
11068 /* Relocs in an executable have to be virtual addresses. */
11069 if (!bfd_link_relocatable (flinfo->info))
11070 irela->r_offset += o->output_section->vma;
11071
11072 last_offset = irela->r_offset;
11073
11074 r_symndx = irela->r_info >> r_sym_shift;
11075 if (r_symndx == STN_UNDEF)
11076 continue;
11077
11078 if (r_symndx >= locsymcount
11079 || (elf_bad_symtab (input_bfd)
11080 && flinfo->sections[r_symndx] == NULL))
11081 {
11082 struct elf_link_hash_entry *rh;
11083 unsigned long indx;
11084
11085 /* This is a reloc against a global symbol. We
11086 have not yet output all the local symbols, so
11087 we do not know the symbol index of any global
11088 symbol. We set the rel_hash entry for this
11089 reloc to point to the global hash table entry
11090 for this symbol. The symbol index is then
11091 set at the end of bfd_elf_final_link. */
11092 indx = r_symndx - extsymoff;
11093 rh = elf_sym_hashes (input_bfd)[indx];
11094 while (rh->root.type == bfd_link_hash_indirect
11095 || rh->root.type == bfd_link_hash_warning)
11096 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11097
11098 /* Setting the index to -2 tells
11099 elf_link_output_extsym that this symbol is
11100 used by a reloc. */
11101 BFD_ASSERT (rh->indx < 0);
11102 rh->indx = -2;
11103 *rel_hash = rh;
11104
11105 continue;
11106 }
11107
11108 /* This is a reloc against a local symbol. */
11109
11110 *rel_hash = NULL;
11111 sym = isymbuf[r_symndx];
11112 sec = flinfo->sections[r_symndx];
11113 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11114 {
11115 /* I suppose the backend ought to fill in the
11116 section of any STT_SECTION symbol against a
11117 processor specific section. */
11118 r_symndx = STN_UNDEF;
11119 if (bfd_is_abs_section (sec))
11120 ;
11121 else if (sec == NULL || sec->owner == NULL)
11122 {
11123 bfd_set_error (bfd_error_bad_value);
11124 return FALSE;
11125 }
11126 else
11127 {
11128 asection *osec = sec->output_section;
11129
11130 /* If we have discarded a section, the output
11131 section will be the absolute section. In
11132 case of discarded SEC_MERGE sections, use
11133 the kept section. relocate_section should
11134 have already handled discarded linkonce
11135 sections. */
11136 if (bfd_is_abs_section (osec)
11137 && sec->kept_section != NULL
11138 && sec->kept_section->output_section != NULL)
11139 {
11140 osec = sec->kept_section->output_section;
11141 irela->r_addend -= osec->vma;
11142 }
11143
11144 if (!bfd_is_abs_section (osec))
11145 {
11146 r_symndx = osec->target_index;
11147 if (r_symndx == STN_UNDEF)
11148 {
11149 irela->r_addend += osec->vma;
11150 osec = _bfd_nearby_section (output_bfd, osec,
11151 osec->vma);
11152 irela->r_addend -= osec->vma;
11153 r_symndx = osec->target_index;
11154 }
11155 }
11156 }
11157
11158 /* Adjust the addend according to where the
11159 section winds up in the output section. */
11160 if (rela_normal)
11161 irela->r_addend += sec->output_offset;
11162 }
11163 else
11164 {
11165 if (flinfo->indices[r_symndx] == -1)
11166 {
11167 unsigned long shlink;
11168 const char *name;
11169 asection *osec;
11170 long indx;
11171
11172 if (flinfo->info->strip == strip_all)
11173 {
11174 /* You can't do ld -r -s. */
11175 bfd_set_error (bfd_error_invalid_operation);
11176 return FALSE;
11177 }
11178
11179 /* This symbol was skipped earlier, but
11180 since it is needed by a reloc, we
11181 must output it now. */
11182 shlink = symtab_hdr->sh_link;
11183 name = (bfd_elf_string_from_elf_section
11184 (input_bfd, shlink, sym.st_name));
11185 if (name == NULL)
11186 return FALSE;
11187
11188 osec = sec->output_section;
11189 sym.st_shndx =
11190 _bfd_elf_section_from_bfd_section (output_bfd,
11191 osec);
11192 if (sym.st_shndx == SHN_BAD)
11193 return FALSE;
11194
11195 sym.st_value += sec->output_offset;
11196 if (!bfd_link_relocatable (flinfo->info))
11197 {
11198 sym.st_value += osec->vma;
11199 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11200 {
11201 struct elf_link_hash_table *htab
11202 = elf_hash_table (flinfo->info);
11203
11204 /* STT_TLS symbols are relative to PT_TLS
11205 segment base. */
11206 if (htab->tls_sec != NULL)
11207 sym.st_value -= htab->tls_sec->vma;
11208 else
11209 sym.st_info
11210 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11211 STT_NOTYPE);
11212 }
11213 }
11214
11215 indx = bfd_get_symcount (output_bfd);
11216 ret = elf_link_output_symstrtab (flinfo, name,
11217 &sym, sec,
11218 NULL);
11219 if (ret == 0)
11220 return FALSE;
11221 else if (ret == 1)
11222 flinfo->indices[r_symndx] = indx;
11223 else
11224 abort ();
11225 }
11226
11227 r_symndx = flinfo->indices[r_symndx];
11228 }
11229
11230 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11231 | (irela->r_info & r_type_mask));
11232 }
11233
11234 /* Swap out the relocs. */
11235 input_rel_hdr = esdi->rel.hdr;
11236 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11237 {
11238 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11239 input_rel_hdr,
11240 internal_relocs,
11241 rel_hash_list))
11242 return FALSE;
11243 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11244 * bed->s->int_rels_per_ext_rel);
11245 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11246 }
11247
11248 input_rela_hdr = esdi->rela.hdr;
11249 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11250 {
11251 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11252 input_rela_hdr,
11253 internal_relocs,
11254 rela_hash_list))
11255 return FALSE;
11256 }
11257 }
11258 }
11259
11260 /* Write out the modified section contents. */
11261 if (bed->elf_backend_write_section
11262 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11263 contents))
11264 {
11265 /* Section written out. */
11266 }
11267 else switch (o->sec_info_type)
11268 {
11269 case SEC_INFO_TYPE_STABS:
11270 if (! (_bfd_write_section_stabs
11271 (output_bfd,
11272 &elf_hash_table (flinfo->info)->stab_info,
11273 o, &elf_section_data (o)->sec_info, contents)))
11274 return FALSE;
11275 break;
11276 case SEC_INFO_TYPE_MERGE:
11277 if (! _bfd_write_merged_section (output_bfd, o,
11278 elf_section_data (o)->sec_info))
11279 return FALSE;
11280 break;
11281 case SEC_INFO_TYPE_EH_FRAME:
11282 {
11283 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11284 o, contents))
11285 return FALSE;
11286 }
11287 break;
11288 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11289 {
11290 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11291 flinfo->info,
11292 o, contents))
11293 return FALSE;
11294 }
11295 break;
11296 default:
11297 {
11298 if (! (o->flags & SEC_EXCLUDE))
11299 {
11300 file_ptr offset = (file_ptr) o->output_offset;
11301 bfd_size_type todo = o->size;
11302
11303 offset *= bfd_octets_per_byte (output_bfd, o);
11304
11305 if ((o->flags & SEC_ELF_REVERSE_COPY))
11306 {
11307 /* Reverse-copy input section to output. */
11308 do
11309 {
11310 todo -= address_size;
11311 if (! bfd_set_section_contents (output_bfd,
11312 o->output_section,
11313 contents + todo,
11314 offset,
11315 address_size))
11316 return FALSE;
11317 if (todo == 0)
11318 break;
11319 offset += address_size;
11320 }
11321 while (1);
11322 }
11323 else if (! bfd_set_section_contents (output_bfd,
11324 o->output_section,
11325 contents,
11326 offset, todo))
11327 return FALSE;
11328 }
11329 }
11330 break;
11331 }
11332 }
11333
11334 return TRUE;
11335 }
11336
11337 /* Generate a reloc when linking an ELF file. This is a reloc
11338 requested by the linker, and does not come from any input file. This
11339 is used to build constructor and destructor tables when linking
11340 with -Ur. */
11341
11342 static bfd_boolean
11343 elf_reloc_link_order (bfd *output_bfd,
11344 struct bfd_link_info *info,
11345 asection *output_section,
11346 struct bfd_link_order *link_order)
11347 {
11348 reloc_howto_type *howto;
11349 long indx;
11350 bfd_vma offset;
11351 bfd_vma addend;
11352 struct bfd_elf_section_reloc_data *reldata;
11353 struct elf_link_hash_entry **rel_hash_ptr;
11354 Elf_Internal_Shdr *rel_hdr;
11355 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11356 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11357 bfd_byte *erel;
11358 unsigned int i;
11359 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11360
11361 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11362 if (howto == NULL)
11363 {
11364 bfd_set_error (bfd_error_bad_value);
11365 return FALSE;
11366 }
11367
11368 addend = link_order->u.reloc.p->addend;
11369
11370 if (esdo->rel.hdr)
11371 reldata = &esdo->rel;
11372 else if (esdo->rela.hdr)
11373 reldata = &esdo->rela;
11374 else
11375 {
11376 reldata = NULL;
11377 BFD_ASSERT (0);
11378 }
11379
11380 /* Figure out the symbol index. */
11381 rel_hash_ptr = reldata->hashes + reldata->count;
11382 if (link_order->type == bfd_section_reloc_link_order)
11383 {
11384 indx = link_order->u.reloc.p->u.section->target_index;
11385 BFD_ASSERT (indx != 0);
11386 *rel_hash_ptr = NULL;
11387 }
11388 else
11389 {
11390 struct elf_link_hash_entry *h;
11391
11392 /* Treat a reloc against a defined symbol as though it were
11393 actually against the section. */
11394 h = ((struct elf_link_hash_entry *)
11395 bfd_wrapped_link_hash_lookup (output_bfd, info,
11396 link_order->u.reloc.p->u.name,
11397 FALSE, FALSE, TRUE));
11398 if (h != NULL
11399 && (h->root.type == bfd_link_hash_defined
11400 || h->root.type == bfd_link_hash_defweak))
11401 {
11402 asection *section;
11403
11404 section = h->root.u.def.section;
11405 indx = section->output_section->target_index;
11406 *rel_hash_ptr = NULL;
11407 /* It seems that we ought to add the symbol value to the
11408 addend here, but in practice it has already been added
11409 because it was passed to constructor_callback. */
11410 addend += section->output_section->vma + section->output_offset;
11411 }
11412 else if (h != NULL)
11413 {
11414 /* Setting the index to -2 tells elf_link_output_extsym that
11415 this symbol is used by a reloc. */
11416 h->indx = -2;
11417 *rel_hash_ptr = h;
11418 indx = 0;
11419 }
11420 else
11421 {
11422 (*info->callbacks->unattached_reloc)
11423 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11424 indx = 0;
11425 }
11426 }
11427
11428 /* If this is an inplace reloc, we must write the addend into the
11429 object file. */
11430 if (howto->partial_inplace && addend != 0)
11431 {
11432 bfd_size_type size;
11433 bfd_reloc_status_type rstat;
11434 bfd_byte *buf;
11435 bfd_boolean ok;
11436 const char *sym_name;
11437 bfd_size_type octets;
11438
11439 size = (bfd_size_type) bfd_get_reloc_size (howto);
11440 buf = (bfd_byte *) bfd_zmalloc (size);
11441 if (buf == NULL && size != 0)
11442 return FALSE;
11443 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11444 switch (rstat)
11445 {
11446 case bfd_reloc_ok:
11447 break;
11448
11449 default:
11450 case bfd_reloc_outofrange:
11451 abort ();
11452
11453 case bfd_reloc_overflow:
11454 if (link_order->type == bfd_section_reloc_link_order)
11455 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11456 else
11457 sym_name = link_order->u.reloc.p->u.name;
11458 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11459 howto->name, addend, NULL, NULL,
11460 (bfd_vma) 0);
11461 break;
11462 }
11463
11464 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11465 output_section);
11466 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11467 octets, size);
11468 free (buf);
11469 if (! ok)
11470 return FALSE;
11471 }
11472
11473 /* The address of a reloc is relative to the section in a
11474 relocatable file, and is a virtual address in an executable
11475 file. */
11476 offset = link_order->offset;
11477 if (! bfd_link_relocatable (info))
11478 offset += output_section->vma;
11479
11480 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11481 {
11482 irel[i].r_offset = offset;
11483 irel[i].r_info = 0;
11484 irel[i].r_addend = 0;
11485 }
11486 if (bed->s->arch_size == 32)
11487 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11488 else
11489 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11490
11491 rel_hdr = reldata->hdr;
11492 erel = rel_hdr->contents;
11493 if (rel_hdr->sh_type == SHT_REL)
11494 {
11495 erel += reldata->count * bed->s->sizeof_rel;
11496 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11497 }
11498 else
11499 {
11500 irel[0].r_addend = addend;
11501 erel += reldata->count * bed->s->sizeof_rela;
11502 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11503 }
11504
11505 ++reldata->count;
11506
11507 return TRUE;
11508 }
11509
11510
11511 /* Compare two sections based on the locations of the sections they are
11512 linked to. Used by elf_fixup_link_order. */
11513
11514 static int
11515 compare_link_order (const void *a, const void *b)
11516 {
11517 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11518 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11519 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11520 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11521 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11522 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11523
11524 if (apos < bpos)
11525 return -1;
11526 if (apos > bpos)
11527 return 1;
11528
11529 /* The only way we should get matching LMAs is when the first of two
11530 sections has zero size. */
11531 if (asec->size < bsec->size)
11532 return -1;
11533 if (asec->size > bsec->size)
11534 return 1;
11535
11536 /* If they are both zero size then they almost certainly have the same
11537 VMA and thus are not ordered with respect to each other. Test VMA
11538 anyway, and fall back to id to make the result reproducible across
11539 qsort implementations. */
11540 apos = asec->output_section->vma + asec->output_offset;
11541 bpos = bsec->output_section->vma + bsec->output_offset;
11542 if (apos < bpos)
11543 return -1;
11544 if (apos > bpos)
11545 return 1;
11546
11547 return asec->id - bsec->id;
11548 }
11549
11550
11551 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11552 order as their linked sections. Returns false if this could not be done
11553 because an output section includes both ordered and unordered
11554 sections. Ideally we'd do this in the linker proper. */
11555
11556 static bfd_boolean
11557 elf_fixup_link_order (bfd *abfd, asection *o)
11558 {
11559 size_t seen_linkorder;
11560 size_t seen_other;
11561 size_t n;
11562 struct bfd_link_order *p;
11563 bfd *sub;
11564 struct bfd_link_order **sections;
11565 asection *s, *other_sec, *linkorder_sec;
11566 bfd_vma offset;
11567
11568 other_sec = NULL;
11569 linkorder_sec = NULL;
11570 seen_other = 0;
11571 seen_linkorder = 0;
11572 for (p = o->map_head.link_order; p != NULL; p = p->next)
11573 {
11574 if (p->type == bfd_indirect_link_order)
11575 {
11576 s = p->u.indirect.section;
11577 sub = s->owner;
11578 if ((s->flags & SEC_LINKER_CREATED) == 0
11579 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11580 && elf_section_data (s) != NULL
11581 && elf_linked_to_section (s) != NULL)
11582 {
11583 seen_linkorder++;
11584 linkorder_sec = s;
11585 }
11586 else
11587 {
11588 seen_other++;
11589 other_sec = s;
11590 }
11591 }
11592 else
11593 seen_other++;
11594
11595 if (seen_other && seen_linkorder)
11596 {
11597 if (other_sec && linkorder_sec)
11598 _bfd_error_handler
11599 /* xgettext:c-format */
11600 (_("%pA has both ordered [`%pA' in %pB] "
11601 "and unordered [`%pA' in %pB] sections"),
11602 o, linkorder_sec, linkorder_sec->owner,
11603 other_sec, other_sec->owner);
11604 else
11605 _bfd_error_handler
11606 (_("%pA has both ordered and unordered sections"), o);
11607 bfd_set_error (bfd_error_bad_value);
11608 return FALSE;
11609 }
11610 }
11611
11612 if (!seen_linkorder)
11613 return TRUE;
11614
11615 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11616 if (sections == NULL)
11617 return FALSE;
11618
11619 seen_linkorder = 0;
11620 for (p = o->map_head.link_order; p != NULL; p = p->next)
11621 sections[seen_linkorder++] = p;
11622
11623 /* Sort the input sections in the order of their linked section. */
11624 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11625
11626 /* Change the offsets of the sections. */
11627 offset = 0;
11628 for (n = 0; n < seen_linkorder; n++)
11629 {
11630 bfd_vma mask;
11631 s = sections[n]->u.indirect.section;
11632 mask = ~(bfd_vma) 0 << s->alignment_power;
11633 offset = (offset + ~mask) & mask;
11634 s->output_offset = offset / bfd_octets_per_byte (abfd, s);
11635 sections[n]->offset = offset;
11636 offset += sections[n]->size;
11637 }
11638
11639 free (sections);
11640 return TRUE;
11641 }
11642
11643 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11644 Returns TRUE upon success, FALSE otherwise. */
11645
11646 static bfd_boolean
11647 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11648 {
11649 bfd_boolean ret = FALSE;
11650 bfd *implib_bfd;
11651 const struct elf_backend_data *bed;
11652 flagword flags;
11653 enum bfd_architecture arch;
11654 unsigned int mach;
11655 asymbol **sympp = NULL;
11656 long symsize;
11657 long symcount;
11658 long src_count;
11659 elf_symbol_type *osymbuf;
11660 size_t amt;
11661
11662 implib_bfd = info->out_implib_bfd;
11663 bed = get_elf_backend_data (abfd);
11664
11665 if (!bfd_set_format (implib_bfd, bfd_object))
11666 return FALSE;
11667
11668 /* Use flag from executable but make it a relocatable object. */
11669 flags = bfd_get_file_flags (abfd);
11670 flags &= ~HAS_RELOC;
11671 if (!bfd_set_start_address (implib_bfd, 0)
11672 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11673 return FALSE;
11674
11675 /* Copy architecture of output file to import library file. */
11676 arch = bfd_get_arch (abfd);
11677 mach = bfd_get_mach (abfd);
11678 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11679 && (abfd->target_defaulted
11680 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11681 return FALSE;
11682
11683 /* Get symbol table size. */
11684 symsize = bfd_get_symtab_upper_bound (abfd);
11685 if (symsize < 0)
11686 return FALSE;
11687
11688 /* Read in the symbol table. */
11689 sympp = (asymbol **) bfd_malloc (symsize);
11690 if (sympp == NULL)
11691 return FALSE;
11692
11693 symcount = bfd_canonicalize_symtab (abfd, sympp);
11694 if (symcount < 0)
11695 goto free_sym_buf;
11696
11697 /* Allow the BFD backend to copy any private header data it
11698 understands from the output BFD to the import library BFD. */
11699 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11700 goto free_sym_buf;
11701
11702 /* Filter symbols to appear in the import library. */
11703 if (bed->elf_backend_filter_implib_symbols)
11704 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11705 symcount);
11706 else
11707 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11708 if (symcount == 0)
11709 {
11710 bfd_set_error (bfd_error_no_symbols);
11711 _bfd_error_handler (_("%pB: no symbol found for import library"),
11712 implib_bfd);
11713 goto free_sym_buf;
11714 }
11715
11716
11717 /* Make symbols absolute. */
11718 amt = symcount * sizeof (*osymbuf);
11719 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11720 if (osymbuf == NULL)
11721 goto free_sym_buf;
11722
11723 for (src_count = 0; src_count < symcount; src_count++)
11724 {
11725 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11726 sizeof (*osymbuf));
11727 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11728 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11729 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11730 osymbuf[src_count].internal_elf_sym.st_value =
11731 osymbuf[src_count].symbol.value;
11732 sympp[src_count] = &osymbuf[src_count].symbol;
11733 }
11734
11735 bfd_set_symtab (implib_bfd, sympp, symcount);
11736
11737 /* Allow the BFD backend to copy any private data it understands
11738 from the output BFD to the import library BFD. This is done last
11739 to permit the routine to look at the filtered symbol table. */
11740 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11741 goto free_sym_buf;
11742
11743 if (!bfd_close (implib_bfd))
11744 goto free_sym_buf;
11745
11746 ret = TRUE;
11747
11748 free_sym_buf:
11749 free (sympp);
11750 return ret;
11751 }
11752
11753 static void
11754 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11755 {
11756 asection *o;
11757
11758 if (flinfo->symstrtab != NULL)
11759 _bfd_elf_strtab_free (flinfo->symstrtab);
11760 if (flinfo->contents != NULL)
11761 free (flinfo->contents);
11762 if (flinfo->external_relocs != NULL)
11763 free (flinfo->external_relocs);
11764 if (flinfo->internal_relocs != NULL)
11765 free (flinfo->internal_relocs);
11766 if (flinfo->external_syms != NULL)
11767 free (flinfo->external_syms);
11768 if (flinfo->locsym_shndx != NULL)
11769 free (flinfo->locsym_shndx);
11770 if (flinfo->internal_syms != NULL)
11771 free (flinfo->internal_syms);
11772 if (flinfo->indices != NULL)
11773 free (flinfo->indices);
11774 if (flinfo->sections != NULL)
11775 free (flinfo->sections);
11776 if (flinfo->symshndxbuf != NULL
11777 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11778 free (flinfo->symshndxbuf);
11779 for (o = obfd->sections; o != NULL; o = o->next)
11780 {
11781 struct bfd_elf_section_data *esdo = elf_section_data (o);
11782 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11783 free (esdo->rel.hashes);
11784 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11785 free (esdo->rela.hashes);
11786 }
11787 }
11788
11789 /* Do the final step of an ELF link. */
11790
11791 bfd_boolean
11792 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11793 {
11794 bfd_boolean dynamic;
11795 bfd_boolean emit_relocs;
11796 bfd *dynobj;
11797 struct elf_final_link_info flinfo;
11798 asection *o;
11799 struct bfd_link_order *p;
11800 bfd *sub;
11801 bfd_size_type max_contents_size;
11802 bfd_size_type max_external_reloc_size;
11803 bfd_size_type max_internal_reloc_count;
11804 bfd_size_type max_sym_count;
11805 bfd_size_type max_sym_shndx_count;
11806 Elf_Internal_Sym elfsym;
11807 unsigned int i;
11808 Elf_Internal_Shdr *symtab_hdr;
11809 Elf_Internal_Shdr *symtab_shndx_hdr;
11810 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11811 struct elf_outext_info eoinfo;
11812 bfd_boolean merged;
11813 size_t relativecount = 0;
11814 asection *reldyn = 0;
11815 bfd_size_type amt;
11816 asection *attr_section = NULL;
11817 bfd_vma attr_size = 0;
11818 const char *std_attrs_section;
11819 struct elf_link_hash_table *htab = elf_hash_table (info);
11820 bfd_boolean sections_removed;
11821
11822 if (!is_elf_hash_table (htab))
11823 return FALSE;
11824
11825 if (bfd_link_pic (info))
11826 abfd->flags |= DYNAMIC;
11827
11828 dynamic = htab->dynamic_sections_created;
11829 dynobj = htab->dynobj;
11830
11831 emit_relocs = (bfd_link_relocatable (info)
11832 || info->emitrelocations);
11833
11834 flinfo.info = info;
11835 flinfo.output_bfd = abfd;
11836 flinfo.symstrtab = _bfd_elf_strtab_init ();
11837 if (flinfo.symstrtab == NULL)
11838 return FALSE;
11839
11840 if (! dynamic)
11841 {
11842 flinfo.hash_sec = NULL;
11843 flinfo.symver_sec = NULL;
11844 }
11845 else
11846 {
11847 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11848 /* Note that dynsym_sec can be NULL (on VMS). */
11849 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11850 /* Note that it is OK if symver_sec is NULL. */
11851 }
11852
11853 flinfo.contents = NULL;
11854 flinfo.external_relocs = NULL;
11855 flinfo.internal_relocs = NULL;
11856 flinfo.external_syms = NULL;
11857 flinfo.locsym_shndx = NULL;
11858 flinfo.internal_syms = NULL;
11859 flinfo.indices = NULL;
11860 flinfo.sections = NULL;
11861 flinfo.symshndxbuf = NULL;
11862 flinfo.filesym_count = 0;
11863
11864 /* The object attributes have been merged. Remove the input
11865 sections from the link, and set the contents of the output
11866 section. */
11867 sections_removed = FALSE;
11868 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11869 for (o = abfd->sections; o != NULL; o = o->next)
11870 {
11871 bfd_boolean remove_section = FALSE;
11872
11873 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11874 || strcmp (o->name, ".gnu.attributes") == 0)
11875 {
11876 for (p = o->map_head.link_order; p != NULL; p = p->next)
11877 {
11878 asection *input_section;
11879
11880 if (p->type != bfd_indirect_link_order)
11881 continue;
11882 input_section = p->u.indirect.section;
11883 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11884 elf_link_input_bfd ignores this section. */
11885 input_section->flags &= ~SEC_HAS_CONTENTS;
11886 }
11887
11888 attr_size = bfd_elf_obj_attr_size (abfd);
11889 bfd_set_section_size (o, attr_size);
11890 /* Skip this section later on. */
11891 o->map_head.link_order = NULL;
11892 if (attr_size)
11893 attr_section = o;
11894 else
11895 remove_section = TRUE;
11896 }
11897 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11898 {
11899 /* Remove empty group section from linker output. */
11900 remove_section = TRUE;
11901 }
11902 if (remove_section)
11903 {
11904 o->flags |= SEC_EXCLUDE;
11905 bfd_section_list_remove (abfd, o);
11906 abfd->section_count--;
11907 sections_removed = TRUE;
11908 }
11909 }
11910 if (sections_removed)
11911 _bfd_fix_excluded_sec_syms (abfd, info);
11912
11913 /* Count up the number of relocations we will output for each output
11914 section, so that we know the sizes of the reloc sections. We
11915 also figure out some maximum sizes. */
11916 max_contents_size = 0;
11917 max_external_reloc_size = 0;
11918 max_internal_reloc_count = 0;
11919 max_sym_count = 0;
11920 max_sym_shndx_count = 0;
11921 merged = FALSE;
11922 for (o = abfd->sections; o != NULL; o = o->next)
11923 {
11924 struct bfd_elf_section_data *esdo = elf_section_data (o);
11925 o->reloc_count = 0;
11926
11927 for (p = o->map_head.link_order; p != NULL; p = p->next)
11928 {
11929 unsigned int reloc_count = 0;
11930 unsigned int additional_reloc_count = 0;
11931 struct bfd_elf_section_data *esdi = NULL;
11932
11933 if (p->type == bfd_section_reloc_link_order
11934 || p->type == bfd_symbol_reloc_link_order)
11935 reloc_count = 1;
11936 else if (p->type == bfd_indirect_link_order)
11937 {
11938 asection *sec;
11939
11940 sec = p->u.indirect.section;
11941
11942 /* Mark all sections which are to be included in the
11943 link. This will normally be every section. We need
11944 to do this so that we can identify any sections which
11945 the linker has decided to not include. */
11946 sec->linker_mark = TRUE;
11947
11948 if (sec->flags & SEC_MERGE)
11949 merged = TRUE;
11950
11951 if (sec->rawsize > max_contents_size)
11952 max_contents_size = sec->rawsize;
11953 if (sec->size > max_contents_size)
11954 max_contents_size = sec->size;
11955
11956 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11957 && (sec->owner->flags & DYNAMIC) == 0)
11958 {
11959 size_t sym_count;
11960
11961 /* We are interested in just local symbols, not all
11962 symbols. */
11963 if (elf_bad_symtab (sec->owner))
11964 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11965 / bed->s->sizeof_sym);
11966 else
11967 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11968
11969 if (sym_count > max_sym_count)
11970 max_sym_count = sym_count;
11971
11972 if (sym_count > max_sym_shndx_count
11973 && elf_symtab_shndx_list (sec->owner) != NULL)
11974 max_sym_shndx_count = sym_count;
11975
11976 if (esdo->this_hdr.sh_type == SHT_REL
11977 || esdo->this_hdr.sh_type == SHT_RELA)
11978 /* Some backends use reloc_count in relocation sections
11979 to count particular types of relocs. Of course,
11980 reloc sections themselves can't have relocations. */
11981 ;
11982 else if (emit_relocs)
11983 {
11984 reloc_count = sec->reloc_count;
11985 if (bed->elf_backend_count_additional_relocs)
11986 {
11987 int c;
11988 c = (*bed->elf_backend_count_additional_relocs) (sec);
11989 additional_reloc_count += c;
11990 }
11991 }
11992 else if (bed->elf_backend_count_relocs)
11993 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11994
11995 esdi = elf_section_data (sec);
11996
11997 if ((sec->flags & SEC_RELOC) != 0)
11998 {
11999 size_t ext_size = 0;
12000
12001 if (esdi->rel.hdr != NULL)
12002 ext_size = esdi->rel.hdr->sh_size;
12003 if (esdi->rela.hdr != NULL)
12004 ext_size += esdi->rela.hdr->sh_size;
12005
12006 if (ext_size > max_external_reloc_size)
12007 max_external_reloc_size = ext_size;
12008 if (sec->reloc_count > max_internal_reloc_count)
12009 max_internal_reloc_count = sec->reloc_count;
12010 }
12011 }
12012 }
12013
12014 if (reloc_count == 0)
12015 continue;
12016
12017 reloc_count += additional_reloc_count;
12018 o->reloc_count += reloc_count;
12019
12020 if (p->type == bfd_indirect_link_order && emit_relocs)
12021 {
12022 if (esdi->rel.hdr)
12023 {
12024 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12025 esdo->rel.count += additional_reloc_count;
12026 }
12027 if (esdi->rela.hdr)
12028 {
12029 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12030 esdo->rela.count += additional_reloc_count;
12031 }
12032 }
12033 else
12034 {
12035 if (o->use_rela_p)
12036 esdo->rela.count += reloc_count;
12037 else
12038 esdo->rel.count += reloc_count;
12039 }
12040 }
12041
12042 if (o->reloc_count > 0)
12043 o->flags |= SEC_RELOC;
12044 else
12045 {
12046 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12047 set it (this is probably a bug) and if it is set
12048 assign_section_numbers will create a reloc section. */
12049 o->flags &=~ SEC_RELOC;
12050 }
12051
12052 /* If the SEC_ALLOC flag is not set, force the section VMA to
12053 zero. This is done in elf_fake_sections as well, but forcing
12054 the VMA to 0 here will ensure that relocs against these
12055 sections are handled correctly. */
12056 if ((o->flags & SEC_ALLOC) == 0
12057 && ! o->user_set_vma)
12058 o->vma = 0;
12059 }
12060
12061 if (! bfd_link_relocatable (info) && merged)
12062 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12063
12064 /* Figure out the file positions for everything but the symbol table
12065 and the relocs. We set symcount to force assign_section_numbers
12066 to create a symbol table. */
12067 abfd->symcount = info->strip != strip_all || emit_relocs;
12068 BFD_ASSERT (! abfd->output_has_begun);
12069 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12070 goto error_return;
12071
12072 /* Set sizes, and assign file positions for reloc sections. */
12073 for (o = abfd->sections; o != NULL; o = o->next)
12074 {
12075 struct bfd_elf_section_data *esdo = elf_section_data (o);
12076 if ((o->flags & SEC_RELOC) != 0)
12077 {
12078 if (esdo->rel.hdr
12079 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12080 goto error_return;
12081
12082 if (esdo->rela.hdr
12083 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12084 goto error_return;
12085 }
12086
12087 /* _bfd_elf_compute_section_file_positions makes temporary use
12088 of target_index. Reset it. */
12089 o->target_index = 0;
12090
12091 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12092 to count upwards while actually outputting the relocations. */
12093 esdo->rel.count = 0;
12094 esdo->rela.count = 0;
12095
12096 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12097 && !bfd_section_is_ctf (o))
12098 {
12099 /* Cache the section contents so that they can be compressed
12100 later. Use bfd_malloc since it will be freed by
12101 bfd_compress_section_contents. */
12102 unsigned char *contents = esdo->this_hdr.contents;
12103 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12104 abort ();
12105 contents
12106 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12107 if (contents == NULL)
12108 goto error_return;
12109 esdo->this_hdr.contents = contents;
12110 }
12111 }
12112
12113 /* We have now assigned file positions for all the sections except .symtab,
12114 .strtab, and non-loaded reloc and compressed debugging sections. We start
12115 the .symtab section at the current file position, and write directly to it.
12116 We build the .strtab section in memory. */
12117 abfd->symcount = 0;
12118 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12119 /* sh_name is set in prep_headers. */
12120 symtab_hdr->sh_type = SHT_SYMTAB;
12121 /* sh_flags, sh_addr and sh_size all start off zero. */
12122 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12123 /* sh_link is set in assign_section_numbers. */
12124 /* sh_info is set below. */
12125 /* sh_offset is set just below. */
12126 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12127
12128 if (max_sym_count < 20)
12129 max_sym_count = 20;
12130 htab->strtabsize = max_sym_count;
12131 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12132 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12133 if (htab->strtab == NULL)
12134 goto error_return;
12135 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12136 flinfo.symshndxbuf
12137 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12138 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12139
12140 if (info->strip != strip_all || emit_relocs)
12141 {
12142 file_ptr off = elf_next_file_pos (abfd);
12143
12144 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12145
12146 /* Note that at this point elf_next_file_pos (abfd) is
12147 incorrect. We do not yet know the size of the .symtab section.
12148 We correct next_file_pos below, after we do know the size. */
12149
12150 /* Start writing out the symbol table. The first symbol is always a
12151 dummy symbol. */
12152 elfsym.st_value = 0;
12153 elfsym.st_size = 0;
12154 elfsym.st_info = 0;
12155 elfsym.st_other = 0;
12156 elfsym.st_shndx = SHN_UNDEF;
12157 elfsym.st_target_internal = 0;
12158 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12159 bfd_und_section_ptr, NULL) != 1)
12160 goto error_return;
12161
12162 /* Output a symbol for each section. We output these even if we are
12163 discarding local symbols, since they are used for relocs. These
12164 symbols have no names. We store the index of each one in the
12165 index field of the section, so that we can find it again when
12166 outputting relocs. */
12167
12168 elfsym.st_size = 0;
12169 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12170 elfsym.st_other = 0;
12171 elfsym.st_value = 0;
12172 elfsym.st_target_internal = 0;
12173 for (i = 1; i < elf_numsections (abfd); i++)
12174 {
12175 o = bfd_section_from_elf_index (abfd, i);
12176 if (o != NULL)
12177 {
12178 o->target_index = bfd_get_symcount (abfd);
12179 elfsym.st_shndx = i;
12180 if (!bfd_link_relocatable (info))
12181 elfsym.st_value = o->vma;
12182 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12183 NULL) != 1)
12184 goto error_return;
12185 }
12186 }
12187 }
12188
12189 /* Allocate some memory to hold information read in from the input
12190 files. */
12191 if (max_contents_size != 0)
12192 {
12193 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12194 if (flinfo.contents == NULL)
12195 goto error_return;
12196 }
12197
12198 if (max_external_reloc_size != 0)
12199 {
12200 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12201 if (flinfo.external_relocs == NULL)
12202 goto error_return;
12203 }
12204
12205 if (max_internal_reloc_count != 0)
12206 {
12207 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12208 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12209 if (flinfo.internal_relocs == NULL)
12210 goto error_return;
12211 }
12212
12213 if (max_sym_count != 0)
12214 {
12215 amt = max_sym_count * bed->s->sizeof_sym;
12216 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12217 if (flinfo.external_syms == NULL)
12218 goto error_return;
12219
12220 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12221 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12222 if (flinfo.internal_syms == NULL)
12223 goto error_return;
12224
12225 amt = max_sym_count * sizeof (long);
12226 flinfo.indices = (long int *) bfd_malloc (amt);
12227 if (flinfo.indices == NULL)
12228 goto error_return;
12229
12230 amt = max_sym_count * sizeof (asection *);
12231 flinfo.sections = (asection **) bfd_malloc (amt);
12232 if (flinfo.sections == NULL)
12233 goto error_return;
12234 }
12235
12236 if (max_sym_shndx_count != 0)
12237 {
12238 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12239 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12240 if (flinfo.locsym_shndx == NULL)
12241 goto error_return;
12242 }
12243
12244 if (htab->tls_sec)
12245 {
12246 bfd_vma base, end = 0;
12247 asection *sec;
12248
12249 for (sec = htab->tls_sec;
12250 sec && (sec->flags & SEC_THREAD_LOCAL);
12251 sec = sec->next)
12252 {
12253 bfd_size_type size = sec->size;
12254
12255 if (size == 0
12256 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12257 {
12258 struct bfd_link_order *ord = sec->map_tail.link_order;
12259
12260 if (ord != NULL)
12261 size = ord->offset + ord->size;
12262 }
12263 end = sec->vma + size;
12264 }
12265 base = htab->tls_sec->vma;
12266 /* Only align end of TLS section if static TLS doesn't have special
12267 alignment requirements. */
12268 if (bed->static_tls_alignment == 1)
12269 end = align_power (end, htab->tls_sec->alignment_power);
12270 htab->tls_size = end - base;
12271 }
12272
12273 /* Reorder SHF_LINK_ORDER sections. */
12274 for (o = abfd->sections; o != NULL; o = o->next)
12275 {
12276 if (!elf_fixup_link_order (abfd, o))
12277 return FALSE;
12278 }
12279
12280 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12281 return FALSE;
12282
12283 /* Since ELF permits relocations to be against local symbols, we
12284 must have the local symbols available when we do the relocations.
12285 Since we would rather only read the local symbols once, and we
12286 would rather not keep them in memory, we handle all the
12287 relocations for a single input file at the same time.
12288
12289 Unfortunately, there is no way to know the total number of local
12290 symbols until we have seen all of them, and the local symbol
12291 indices precede the global symbol indices. This means that when
12292 we are generating relocatable output, and we see a reloc against
12293 a global symbol, we can not know the symbol index until we have
12294 finished examining all the local symbols to see which ones we are
12295 going to output. To deal with this, we keep the relocations in
12296 memory, and don't output them until the end of the link. This is
12297 an unfortunate waste of memory, but I don't see a good way around
12298 it. Fortunately, it only happens when performing a relocatable
12299 link, which is not the common case. FIXME: If keep_memory is set
12300 we could write the relocs out and then read them again; I don't
12301 know how bad the memory loss will be. */
12302
12303 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12304 sub->output_has_begun = FALSE;
12305 for (o = abfd->sections; o != NULL; o = o->next)
12306 {
12307 for (p = o->map_head.link_order; p != NULL; p = p->next)
12308 {
12309 if (p->type == bfd_indirect_link_order
12310 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12311 == bfd_target_elf_flavour)
12312 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12313 {
12314 if (! sub->output_has_begun)
12315 {
12316 if (! elf_link_input_bfd (&flinfo, sub))
12317 goto error_return;
12318 sub->output_has_begun = TRUE;
12319 }
12320 }
12321 else if (p->type == bfd_section_reloc_link_order
12322 || p->type == bfd_symbol_reloc_link_order)
12323 {
12324 if (! elf_reloc_link_order (abfd, info, o, p))
12325 goto error_return;
12326 }
12327 else
12328 {
12329 if (! _bfd_default_link_order (abfd, info, o, p))
12330 {
12331 if (p->type == bfd_indirect_link_order
12332 && (bfd_get_flavour (sub)
12333 == bfd_target_elf_flavour)
12334 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12335 != bed->s->elfclass))
12336 {
12337 const char *iclass, *oclass;
12338
12339 switch (bed->s->elfclass)
12340 {
12341 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12342 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12343 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12344 default: abort ();
12345 }
12346
12347 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12348 {
12349 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12350 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12351 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12352 default: abort ();
12353 }
12354
12355 bfd_set_error (bfd_error_wrong_format);
12356 _bfd_error_handler
12357 /* xgettext:c-format */
12358 (_("%pB: file class %s incompatible with %s"),
12359 sub, iclass, oclass);
12360 }
12361
12362 goto error_return;
12363 }
12364 }
12365 }
12366 }
12367
12368 /* Free symbol buffer if needed. */
12369 if (!info->reduce_memory_overheads)
12370 {
12371 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12372 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12373 && elf_tdata (sub)->symbuf)
12374 {
12375 free (elf_tdata (sub)->symbuf);
12376 elf_tdata (sub)->symbuf = NULL;
12377 }
12378 }
12379
12380 /* Output any global symbols that got converted to local in a
12381 version script or due to symbol visibility. We do this in a
12382 separate step since ELF requires all local symbols to appear
12383 prior to any global symbols. FIXME: We should only do this if
12384 some global symbols were, in fact, converted to become local.
12385 FIXME: Will this work correctly with the Irix 5 linker? */
12386 eoinfo.failed = FALSE;
12387 eoinfo.flinfo = &flinfo;
12388 eoinfo.localsyms = TRUE;
12389 eoinfo.file_sym_done = FALSE;
12390 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12391 if (eoinfo.failed)
12392 return FALSE;
12393
12394 /* If backend needs to output some local symbols not present in the hash
12395 table, do it now. */
12396 if (bed->elf_backend_output_arch_local_syms
12397 && (info->strip != strip_all || emit_relocs))
12398 {
12399 typedef int (*out_sym_func)
12400 (void *, const char *, Elf_Internal_Sym *, asection *,
12401 struct elf_link_hash_entry *);
12402
12403 if (! ((*bed->elf_backend_output_arch_local_syms)
12404 (abfd, info, &flinfo,
12405 (out_sym_func) elf_link_output_symstrtab)))
12406 return FALSE;
12407 }
12408
12409 /* That wrote out all the local symbols. Finish up the symbol table
12410 with the global symbols. Even if we want to strip everything we
12411 can, we still need to deal with those global symbols that got
12412 converted to local in a version script. */
12413
12414 /* The sh_info field records the index of the first non local symbol. */
12415 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12416
12417 if (dynamic
12418 && htab->dynsym != NULL
12419 && htab->dynsym->output_section != bfd_abs_section_ptr)
12420 {
12421 Elf_Internal_Sym sym;
12422 bfd_byte *dynsym = htab->dynsym->contents;
12423
12424 o = htab->dynsym->output_section;
12425 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12426
12427 /* Write out the section symbols for the output sections. */
12428 if (bfd_link_pic (info)
12429 || htab->is_relocatable_executable)
12430 {
12431 asection *s;
12432
12433 sym.st_size = 0;
12434 sym.st_name = 0;
12435 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12436 sym.st_other = 0;
12437 sym.st_target_internal = 0;
12438
12439 for (s = abfd->sections; s != NULL; s = s->next)
12440 {
12441 int indx;
12442 bfd_byte *dest;
12443 long dynindx;
12444
12445 dynindx = elf_section_data (s)->dynindx;
12446 if (dynindx <= 0)
12447 continue;
12448 indx = elf_section_data (s)->this_idx;
12449 BFD_ASSERT (indx > 0);
12450 sym.st_shndx = indx;
12451 if (! check_dynsym (abfd, &sym))
12452 return FALSE;
12453 sym.st_value = s->vma;
12454 dest = dynsym + dynindx * bed->s->sizeof_sym;
12455 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12456 }
12457 }
12458
12459 /* Write out the local dynsyms. */
12460 if (htab->dynlocal)
12461 {
12462 struct elf_link_local_dynamic_entry *e;
12463 for (e = htab->dynlocal; e ; e = e->next)
12464 {
12465 asection *s;
12466 bfd_byte *dest;
12467
12468 /* Copy the internal symbol and turn off visibility.
12469 Note that we saved a word of storage and overwrote
12470 the original st_name with the dynstr_index. */
12471 sym = e->isym;
12472 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12473
12474 s = bfd_section_from_elf_index (e->input_bfd,
12475 e->isym.st_shndx);
12476 if (s != NULL)
12477 {
12478 sym.st_shndx =
12479 elf_section_data (s->output_section)->this_idx;
12480 if (! check_dynsym (abfd, &sym))
12481 return FALSE;
12482 sym.st_value = (s->output_section->vma
12483 + s->output_offset
12484 + e->isym.st_value);
12485 }
12486
12487 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12488 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12489 }
12490 }
12491 }
12492
12493 /* We get the global symbols from the hash table. */
12494 eoinfo.failed = FALSE;
12495 eoinfo.localsyms = FALSE;
12496 eoinfo.flinfo = &flinfo;
12497 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12498 if (eoinfo.failed)
12499 return FALSE;
12500
12501 /* If backend needs to output some symbols not present in the hash
12502 table, do it now. */
12503 if (bed->elf_backend_output_arch_syms
12504 && (info->strip != strip_all || emit_relocs))
12505 {
12506 typedef int (*out_sym_func)
12507 (void *, const char *, Elf_Internal_Sym *, asection *,
12508 struct elf_link_hash_entry *);
12509
12510 if (! ((*bed->elf_backend_output_arch_syms)
12511 (abfd, info, &flinfo,
12512 (out_sym_func) elf_link_output_symstrtab)))
12513 return FALSE;
12514 }
12515
12516 /* Finalize the .strtab section. */
12517 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12518
12519 /* Swap out the .strtab section. */
12520 if (!elf_link_swap_symbols_out (&flinfo))
12521 return FALSE;
12522
12523 /* Now we know the size of the symtab section. */
12524 if (bfd_get_symcount (abfd) > 0)
12525 {
12526 /* Finish up and write out the symbol string table (.strtab)
12527 section. */
12528 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12529 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12530
12531 if (elf_symtab_shndx_list (abfd))
12532 {
12533 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12534
12535 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12536 {
12537 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12538 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12539 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12540 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12541 symtab_shndx_hdr->sh_size = amt;
12542
12543 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12544 off, TRUE);
12545
12546 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12547 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12548 return FALSE;
12549 }
12550 }
12551
12552 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12553 /* sh_name was set in prep_headers. */
12554 symstrtab_hdr->sh_type = SHT_STRTAB;
12555 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12556 symstrtab_hdr->sh_addr = 0;
12557 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12558 symstrtab_hdr->sh_entsize = 0;
12559 symstrtab_hdr->sh_link = 0;
12560 symstrtab_hdr->sh_info = 0;
12561 /* sh_offset is set just below. */
12562 symstrtab_hdr->sh_addralign = 1;
12563
12564 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12565 off, TRUE);
12566 elf_next_file_pos (abfd) = off;
12567
12568 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12569 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12570 return FALSE;
12571 }
12572
12573 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12574 {
12575 _bfd_error_handler (_("%pB: failed to generate import library"),
12576 info->out_implib_bfd);
12577 return FALSE;
12578 }
12579
12580 /* Adjust the relocs to have the correct symbol indices. */
12581 for (o = abfd->sections; o != NULL; o = o->next)
12582 {
12583 struct bfd_elf_section_data *esdo = elf_section_data (o);
12584 bfd_boolean sort;
12585
12586 if ((o->flags & SEC_RELOC) == 0)
12587 continue;
12588
12589 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12590 if (esdo->rel.hdr != NULL
12591 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12592 return FALSE;
12593 if (esdo->rela.hdr != NULL
12594 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12595 return FALSE;
12596
12597 /* Set the reloc_count field to 0 to prevent write_relocs from
12598 trying to swap the relocs out itself. */
12599 o->reloc_count = 0;
12600 }
12601
12602 if (dynamic && info->combreloc && dynobj != NULL)
12603 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12604
12605 /* If we are linking against a dynamic object, or generating a
12606 shared library, finish up the dynamic linking information. */
12607 if (dynamic)
12608 {
12609 bfd_byte *dyncon, *dynconend;
12610
12611 /* Fix up .dynamic entries. */
12612 o = bfd_get_linker_section (dynobj, ".dynamic");
12613 BFD_ASSERT (o != NULL);
12614
12615 dyncon = o->contents;
12616 dynconend = o->contents + o->size;
12617 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12618 {
12619 Elf_Internal_Dyn dyn;
12620 const char *name;
12621 unsigned int type;
12622 bfd_size_type sh_size;
12623 bfd_vma sh_addr;
12624
12625 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12626
12627 switch (dyn.d_tag)
12628 {
12629 default:
12630 continue;
12631 case DT_NULL:
12632 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12633 {
12634 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12635 {
12636 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12637 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12638 default: continue;
12639 }
12640 dyn.d_un.d_val = relativecount;
12641 relativecount = 0;
12642 break;
12643 }
12644 continue;
12645
12646 case DT_INIT:
12647 name = info->init_function;
12648 goto get_sym;
12649 case DT_FINI:
12650 name = info->fini_function;
12651 get_sym:
12652 {
12653 struct elf_link_hash_entry *h;
12654
12655 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12656 if (h != NULL
12657 && (h->root.type == bfd_link_hash_defined
12658 || h->root.type == bfd_link_hash_defweak))
12659 {
12660 dyn.d_un.d_ptr = h->root.u.def.value;
12661 o = h->root.u.def.section;
12662 if (o->output_section != NULL)
12663 dyn.d_un.d_ptr += (o->output_section->vma
12664 + o->output_offset);
12665 else
12666 {
12667 /* The symbol is imported from another shared
12668 library and does not apply to this one. */
12669 dyn.d_un.d_ptr = 0;
12670 }
12671 break;
12672 }
12673 }
12674 continue;
12675
12676 case DT_PREINIT_ARRAYSZ:
12677 name = ".preinit_array";
12678 goto get_out_size;
12679 case DT_INIT_ARRAYSZ:
12680 name = ".init_array";
12681 goto get_out_size;
12682 case DT_FINI_ARRAYSZ:
12683 name = ".fini_array";
12684 get_out_size:
12685 o = bfd_get_section_by_name (abfd, name);
12686 if (o == NULL)
12687 {
12688 _bfd_error_handler
12689 (_("could not find section %s"), name);
12690 goto error_return;
12691 }
12692 if (o->size == 0)
12693 _bfd_error_handler
12694 (_("warning: %s section has zero size"), name);
12695 dyn.d_un.d_val = o->size;
12696 break;
12697
12698 case DT_PREINIT_ARRAY:
12699 name = ".preinit_array";
12700 goto get_out_vma;
12701 case DT_INIT_ARRAY:
12702 name = ".init_array";
12703 goto get_out_vma;
12704 case DT_FINI_ARRAY:
12705 name = ".fini_array";
12706 get_out_vma:
12707 o = bfd_get_section_by_name (abfd, name);
12708 goto do_vma;
12709
12710 case DT_HASH:
12711 name = ".hash";
12712 goto get_vma;
12713 case DT_GNU_HASH:
12714 name = ".gnu.hash";
12715 goto get_vma;
12716 case DT_STRTAB:
12717 name = ".dynstr";
12718 goto get_vma;
12719 case DT_SYMTAB:
12720 name = ".dynsym";
12721 goto get_vma;
12722 case DT_VERDEF:
12723 name = ".gnu.version_d";
12724 goto get_vma;
12725 case DT_VERNEED:
12726 name = ".gnu.version_r";
12727 goto get_vma;
12728 case DT_VERSYM:
12729 name = ".gnu.version";
12730 get_vma:
12731 o = bfd_get_linker_section (dynobj, name);
12732 do_vma:
12733 if (o == NULL || bfd_is_abs_section (o->output_section))
12734 {
12735 _bfd_error_handler
12736 (_("could not find section %s"), name);
12737 goto error_return;
12738 }
12739 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12740 {
12741 _bfd_error_handler
12742 (_("warning: section '%s' is being made into a note"), name);
12743 bfd_set_error (bfd_error_nonrepresentable_section);
12744 goto error_return;
12745 }
12746 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12747 break;
12748
12749 case DT_REL:
12750 case DT_RELA:
12751 case DT_RELSZ:
12752 case DT_RELASZ:
12753 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12754 type = SHT_REL;
12755 else
12756 type = SHT_RELA;
12757 sh_size = 0;
12758 sh_addr = 0;
12759 for (i = 1; i < elf_numsections (abfd); i++)
12760 {
12761 Elf_Internal_Shdr *hdr;
12762
12763 hdr = elf_elfsections (abfd)[i];
12764 if (hdr->sh_type == type
12765 && (hdr->sh_flags & SHF_ALLOC) != 0)
12766 {
12767 sh_size += hdr->sh_size;
12768 if (sh_addr == 0
12769 || sh_addr > hdr->sh_addr)
12770 sh_addr = hdr->sh_addr;
12771 }
12772 }
12773
12774 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12775 {
12776 /* Don't count procedure linkage table relocs in the
12777 overall reloc count. */
12778 sh_size -= htab->srelplt->size;
12779 if (sh_size == 0)
12780 /* If the size is zero, make the address zero too.
12781 This is to avoid a glibc bug. If the backend
12782 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12783 zero, then we'll put DT_RELA at the end of
12784 DT_JMPREL. glibc will interpret the end of
12785 DT_RELA matching the end of DT_JMPREL as the
12786 case where DT_RELA includes DT_JMPREL, and for
12787 LD_BIND_NOW will decide that processing DT_RELA
12788 will process the PLT relocs too. Net result:
12789 No PLT relocs applied. */
12790 sh_addr = 0;
12791
12792 /* If .rela.plt is the first .rela section, exclude
12793 it from DT_RELA. */
12794 else if (sh_addr == (htab->srelplt->output_section->vma
12795 + htab->srelplt->output_offset))
12796 sh_addr += htab->srelplt->size;
12797 }
12798
12799 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12800 dyn.d_un.d_val = sh_size;
12801 else
12802 dyn.d_un.d_ptr = sh_addr;
12803 break;
12804 }
12805 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12806 }
12807 }
12808
12809 /* If we have created any dynamic sections, then output them. */
12810 if (dynobj != NULL)
12811 {
12812 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12813 goto error_return;
12814
12815 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12816 if (((info->warn_shared_textrel && bfd_link_pic (info))
12817 || info->error_textrel)
12818 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12819 {
12820 bfd_byte *dyncon, *dynconend;
12821
12822 dyncon = o->contents;
12823 dynconend = o->contents + o->size;
12824 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12825 {
12826 Elf_Internal_Dyn dyn;
12827
12828 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12829
12830 if (dyn.d_tag == DT_TEXTREL)
12831 {
12832 if (info->error_textrel)
12833 info->callbacks->einfo
12834 (_("%P%X: read-only segment has dynamic relocations\n"));
12835 else
12836 info->callbacks->einfo
12837 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12838 break;
12839 }
12840 }
12841 }
12842
12843 for (o = dynobj->sections; o != NULL; o = o->next)
12844 {
12845 if ((o->flags & SEC_HAS_CONTENTS) == 0
12846 || o->size == 0
12847 || o->output_section == bfd_abs_section_ptr)
12848 continue;
12849 if ((o->flags & SEC_LINKER_CREATED) == 0)
12850 {
12851 /* At this point, we are only interested in sections
12852 created by _bfd_elf_link_create_dynamic_sections. */
12853 continue;
12854 }
12855 if (htab->stab_info.stabstr == o)
12856 continue;
12857 if (htab->eh_info.hdr_sec == o)
12858 continue;
12859 if (strcmp (o->name, ".dynstr") != 0)
12860 {
12861 bfd_size_type octets = ((file_ptr) o->output_offset
12862 * bfd_octets_per_byte (abfd, o));
12863 if (!bfd_set_section_contents (abfd, o->output_section,
12864 o->contents, octets, o->size))
12865 goto error_return;
12866 }
12867 else
12868 {
12869 /* The contents of the .dynstr section are actually in a
12870 stringtab. */
12871 file_ptr off;
12872
12873 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12874 if (bfd_seek (abfd, off, SEEK_SET) != 0
12875 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12876 goto error_return;
12877 }
12878 }
12879 }
12880
12881 if (!info->resolve_section_groups)
12882 {
12883 bfd_boolean failed = FALSE;
12884
12885 BFD_ASSERT (bfd_link_relocatable (info));
12886 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12887 if (failed)
12888 goto error_return;
12889 }
12890
12891 /* If we have optimized stabs strings, output them. */
12892 if (htab->stab_info.stabstr != NULL)
12893 {
12894 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12895 goto error_return;
12896 }
12897
12898 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12899 goto error_return;
12900
12901 if (info->callbacks->emit_ctf)
12902 info->callbacks->emit_ctf ();
12903
12904 elf_final_link_free (abfd, &flinfo);
12905
12906 if (attr_section)
12907 {
12908 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12909 if (contents == NULL)
12910 return FALSE; /* Bail out and fail. */
12911 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12912 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12913 free (contents);
12914 }
12915
12916 return TRUE;
12917
12918 error_return:
12919 elf_final_link_free (abfd, &flinfo);
12920 return FALSE;
12921 }
12922 \f
12923 /* Initialize COOKIE for input bfd ABFD. */
12924
12925 static bfd_boolean
12926 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12927 struct bfd_link_info *info, bfd *abfd)
12928 {
12929 Elf_Internal_Shdr *symtab_hdr;
12930 const struct elf_backend_data *bed;
12931
12932 bed = get_elf_backend_data (abfd);
12933 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12934
12935 cookie->abfd = abfd;
12936 cookie->sym_hashes = elf_sym_hashes (abfd);
12937 cookie->bad_symtab = elf_bad_symtab (abfd);
12938 if (cookie->bad_symtab)
12939 {
12940 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12941 cookie->extsymoff = 0;
12942 }
12943 else
12944 {
12945 cookie->locsymcount = symtab_hdr->sh_info;
12946 cookie->extsymoff = symtab_hdr->sh_info;
12947 }
12948
12949 if (bed->s->arch_size == 32)
12950 cookie->r_sym_shift = 8;
12951 else
12952 cookie->r_sym_shift = 32;
12953
12954 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12955 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12956 {
12957 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12958 cookie->locsymcount, 0,
12959 NULL, NULL, NULL);
12960 if (cookie->locsyms == NULL)
12961 {
12962 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12963 return FALSE;
12964 }
12965 if (info->keep_memory)
12966 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12967 }
12968 return TRUE;
12969 }
12970
12971 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12972
12973 static void
12974 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12975 {
12976 Elf_Internal_Shdr *symtab_hdr;
12977
12978 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12979 if (cookie->locsyms != NULL
12980 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12981 free (cookie->locsyms);
12982 }
12983
12984 /* Initialize the relocation information in COOKIE for input section SEC
12985 of input bfd ABFD. */
12986
12987 static bfd_boolean
12988 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12989 struct bfd_link_info *info, bfd *abfd,
12990 asection *sec)
12991 {
12992 if (sec->reloc_count == 0)
12993 {
12994 cookie->rels = NULL;
12995 cookie->relend = NULL;
12996 }
12997 else
12998 {
12999 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13000 info->keep_memory);
13001 if (cookie->rels == NULL)
13002 return FALSE;
13003 cookie->rel = cookie->rels;
13004 cookie->relend = cookie->rels + sec->reloc_count;
13005 }
13006 cookie->rel = cookie->rels;
13007 return TRUE;
13008 }
13009
13010 /* Free the memory allocated by init_reloc_cookie_rels,
13011 if appropriate. */
13012
13013 static void
13014 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13015 asection *sec)
13016 {
13017 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13018 free (cookie->rels);
13019 }
13020
13021 /* Initialize the whole of COOKIE for input section SEC. */
13022
13023 static bfd_boolean
13024 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13025 struct bfd_link_info *info,
13026 asection *sec)
13027 {
13028 if (!init_reloc_cookie (cookie, info, sec->owner))
13029 goto error1;
13030 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13031 goto error2;
13032 return TRUE;
13033
13034 error2:
13035 fini_reloc_cookie (cookie, sec->owner);
13036 error1:
13037 return FALSE;
13038 }
13039
13040 /* Free the memory allocated by init_reloc_cookie_for_section,
13041 if appropriate. */
13042
13043 static void
13044 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13045 asection *sec)
13046 {
13047 fini_reloc_cookie_rels (cookie, sec);
13048 fini_reloc_cookie (cookie, sec->owner);
13049 }
13050 \f
13051 /* Garbage collect unused sections. */
13052
13053 /* Default gc_mark_hook. */
13054
13055 asection *
13056 _bfd_elf_gc_mark_hook (asection *sec,
13057 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13058 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13059 struct elf_link_hash_entry *h,
13060 Elf_Internal_Sym *sym)
13061 {
13062 if (h != NULL)
13063 {
13064 switch (h->root.type)
13065 {
13066 case bfd_link_hash_defined:
13067 case bfd_link_hash_defweak:
13068 return h->root.u.def.section;
13069
13070 case bfd_link_hash_common:
13071 return h->root.u.c.p->section;
13072
13073 default:
13074 break;
13075 }
13076 }
13077 else
13078 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13079
13080 return NULL;
13081 }
13082
13083 /* Return the debug definition section. */
13084
13085 static asection *
13086 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13087 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13088 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13089 struct elf_link_hash_entry *h,
13090 Elf_Internal_Sym *sym)
13091 {
13092 if (h != NULL)
13093 {
13094 /* Return the global debug definition section. */
13095 if ((h->root.type == bfd_link_hash_defined
13096 || h->root.type == bfd_link_hash_defweak)
13097 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13098 return h->root.u.def.section;
13099 }
13100 else
13101 {
13102 /* Return the local debug definition section. */
13103 asection *isec = bfd_section_from_elf_index (sec->owner,
13104 sym->st_shndx);
13105 if ((isec->flags & SEC_DEBUGGING) != 0)
13106 return isec;
13107 }
13108
13109 return NULL;
13110 }
13111
13112 /* COOKIE->rel describes a relocation against section SEC, which is
13113 a section we've decided to keep. Return the section that contains
13114 the relocation symbol, or NULL if no section contains it. */
13115
13116 asection *
13117 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13118 elf_gc_mark_hook_fn gc_mark_hook,
13119 struct elf_reloc_cookie *cookie,
13120 bfd_boolean *start_stop)
13121 {
13122 unsigned long r_symndx;
13123 struct elf_link_hash_entry *h, *hw;
13124
13125 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13126 if (r_symndx == STN_UNDEF)
13127 return NULL;
13128
13129 if (r_symndx >= cookie->locsymcount
13130 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13131 {
13132 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13133 if (h == NULL)
13134 {
13135 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13136 sec->owner);
13137 return NULL;
13138 }
13139 while (h->root.type == bfd_link_hash_indirect
13140 || h->root.type == bfd_link_hash_warning)
13141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13142 h->mark = 1;
13143 /* Keep all aliases of the symbol too. If an object symbol
13144 needs to be copied into .dynbss then all of its aliases
13145 should be present as dynamic symbols, not just the one used
13146 on the copy relocation. */
13147 hw = h;
13148 while (hw->is_weakalias)
13149 {
13150 hw = hw->u.alias;
13151 hw->mark = 1;
13152 }
13153
13154 if (start_stop != NULL)
13155 {
13156 /* To work around a glibc bug, mark XXX input sections
13157 when there is a reference to __start_XXX or __stop_XXX
13158 symbols. */
13159 if (h->start_stop)
13160 {
13161 asection *s = h->u2.start_stop_section;
13162 *start_stop = !s->gc_mark;
13163 return s;
13164 }
13165 }
13166
13167 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13168 }
13169
13170 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13171 &cookie->locsyms[r_symndx]);
13172 }
13173
13174 /* COOKIE->rel describes a relocation against section SEC, which is
13175 a section we've decided to keep. Mark the section that contains
13176 the relocation symbol. */
13177
13178 bfd_boolean
13179 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13180 asection *sec,
13181 elf_gc_mark_hook_fn gc_mark_hook,
13182 struct elf_reloc_cookie *cookie)
13183 {
13184 asection *rsec;
13185 bfd_boolean start_stop = FALSE;
13186
13187 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13188 while (rsec != NULL)
13189 {
13190 if (!rsec->gc_mark)
13191 {
13192 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13193 || (rsec->owner->flags & DYNAMIC) != 0)
13194 rsec->gc_mark = 1;
13195 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13196 return FALSE;
13197 }
13198 if (!start_stop)
13199 break;
13200 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13201 }
13202 return TRUE;
13203 }
13204
13205 /* The mark phase of garbage collection. For a given section, mark
13206 it and any sections in this section's group, and all the sections
13207 which define symbols to which it refers. */
13208
13209 bfd_boolean
13210 _bfd_elf_gc_mark (struct bfd_link_info *info,
13211 asection *sec,
13212 elf_gc_mark_hook_fn gc_mark_hook)
13213 {
13214 bfd_boolean ret;
13215 asection *group_sec, *eh_frame;
13216
13217 sec->gc_mark = 1;
13218
13219 /* Mark all the sections in the group. */
13220 group_sec = elf_section_data (sec)->next_in_group;
13221 if (group_sec && !group_sec->gc_mark)
13222 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13223 return FALSE;
13224
13225 /* Look through the section relocs. */
13226 ret = TRUE;
13227 eh_frame = elf_eh_frame_section (sec->owner);
13228 if ((sec->flags & SEC_RELOC) != 0
13229 && sec->reloc_count > 0
13230 && sec != eh_frame)
13231 {
13232 struct elf_reloc_cookie cookie;
13233
13234 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13235 ret = FALSE;
13236 else
13237 {
13238 for (; cookie.rel < cookie.relend; cookie.rel++)
13239 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13240 {
13241 ret = FALSE;
13242 break;
13243 }
13244 fini_reloc_cookie_for_section (&cookie, sec);
13245 }
13246 }
13247
13248 if (ret && eh_frame && elf_fde_list (sec))
13249 {
13250 struct elf_reloc_cookie cookie;
13251
13252 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13253 ret = FALSE;
13254 else
13255 {
13256 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13257 gc_mark_hook, &cookie))
13258 ret = FALSE;
13259 fini_reloc_cookie_for_section (&cookie, eh_frame);
13260 }
13261 }
13262
13263 eh_frame = elf_section_eh_frame_entry (sec);
13264 if (ret && eh_frame && !eh_frame->gc_mark)
13265 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13266 ret = FALSE;
13267
13268 return ret;
13269 }
13270
13271 /* Scan and mark sections in a special or debug section group. */
13272
13273 static void
13274 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13275 {
13276 /* Point to first section of section group. */
13277 asection *ssec;
13278 /* Used to iterate the section group. */
13279 asection *msec;
13280
13281 bfd_boolean is_special_grp = TRUE;
13282 bfd_boolean is_debug_grp = TRUE;
13283
13284 /* First scan to see if group contains any section other than debug
13285 and special section. */
13286 ssec = msec = elf_next_in_group (grp);
13287 do
13288 {
13289 if ((msec->flags & SEC_DEBUGGING) == 0)
13290 is_debug_grp = FALSE;
13291
13292 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13293 is_special_grp = FALSE;
13294
13295 msec = elf_next_in_group (msec);
13296 }
13297 while (msec != ssec);
13298
13299 /* If this is a pure debug section group or pure special section group,
13300 keep all sections in this group. */
13301 if (is_debug_grp || is_special_grp)
13302 {
13303 do
13304 {
13305 msec->gc_mark = 1;
13306 msec = elf_next_in_group (msec);
13307 }
13308 while (msec != ssec);
13309 }
13310 }
13311
13312 /* Keep debug and special sections. */
13313
13314 bfd_boolean
13315 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13316 elf_gc_mark_hook_fn mark_hook)
13317 {
13318 bfd *ibfd;
13319
13320 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13321 {
13322 asection *isec;
13323 bfd_boolean some_kept;
13324 bfd_boolean debug_frag_seen;
13325 bfd_boolean has_kept_debug_info;
13326
13327 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13328 continue;
13329 isec = ibfd->sections;
13330 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13331 continue;
13332
13333 /* Ensure all linker created sections are kept,
13334 see if any other section is already marked,
13335 and note if we have any fragmented debug sections. */
13336 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13337 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13338 {
13339 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13340 isec->gc_mark = 1;
13341 else if (isec->gc_mark
13342 && (isec->flags & SEC_ALLOC) != 0
13343 && elf_section_type (isec) != SHT_NOTE)
13344 some_kept = TRUE;
13345 else
13346 {
13347 /* Since all sections, except for backend specific ones,
13348 have been garbage collected, call mark_hook on this
13349 section if any of its linked-to sections is marked. */
13350 asection *linked_to_sec = elf_linked_to_section (isec);
13351 for (; linked_to_sec != NULL;
13352 linked_to_sec = elf_linked_to_section (linked_to_sec))
13353 if (linked_to_sec->gc_mark)
13354 {
13355 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13356 return FALSE;
13357 break;
13358 }
13359 }
13360
13361 if (!debug_frag_seen
13362 && (isec->flags & SEC_DEBUGGING)
13363 && CONST_STRNEQ (isec->name, ".debug_line."))
13364 debug_frag_seen = TRUE;
13365 else if (strcmp (bfd_section_name (isec),
13366 "__patchable_function_entries") == 0
13367 && elf_linked_to_section (isec) == NULL)
13368 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13369 "need linked-to section "
13370 "for --gc-sections\n"),
13371 isec->owner, isec);
13372 }
13373
13374 /* If no non-note alloc section in this file will be kept, then
13375 we can toss out the debug and special sections. */
13376 if (!some_kept)
13377 continue;
13378
13379 /* Keep debug and special sections like .comment when they are
13380 not part of a group. Also keep section groups that contain
13381 just debug sections or special sections. NB: Sections with
13382 linked-to section has been handled above. */
13383 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13384 {
13385 if ((isec->flags & SEC_GROUP) != 0)
13386 _bfd_elf_gc_mark_debug_special_section_group (isec);
13387 else if (((isec->flags & SEC_DEBUGGING) != 0
13388 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13389 && elf_next_in_group (isec) == NULL
13390 && elf_linked_to_section (isec) == NULL)
13391 isec->gc_mark = 1;
13392 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13393 has_kept_debug_info = TRUE;
13394 }
13395
13396 /* Look for CODE sections which are going to be discarded,
13397 and find and discard any fragmented debug sections which
13398 are associated with that code section. */
13399 if (debug_frag_seen)
13400 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13401 if ((isec->flags & SEC_CODE) != 0
13402 && isec->gc_mark == 0)
13403 {
13404 unsigned int ilen;
13405 asection *dsec;
13406
13407 ilen = strlen (isec->name);
13408
13409 /* Association is determined by the name of the debug
13410 section containing the name of the code section as
13411 a suffix. For example .debug_line.text.foo is a
13412 debug section associated with .text.foo. */
13413 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13414 {
13415 unsigned int dlen;
13416
13417 if (dsec->gc_mark == 0
13418 || (dsec->flags & SEC_DEBUGGING) == 0)
13419 continue;
13420
13421 dlen = strlen (dsec->name);
13422
13423 if (dlen > ilen
13424 && strncmp (dsec->name + (dlen - ilen),
13425 isec->name, ilen) == 0)
13426 dsec->gc_mark = 0;
13427 }
13428 }
13429
13430 /* Mark debug sections referenced by kept debug sections. */
13431 if (has_kept_debug_info)
13432 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13433 if (isec->gc_mark
13434 && (isec->flags & SEC_DEBUGGING) != 0)
13435 if (!_bfd_elf_gc_mark (info, isec,
13436 elf_gc_mark_debug_section))
13437 return FALSE;
13438 }
13439 return TRUE;
13440 }
13441
13442 static bfd_boolean
13443 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13444 {
13445 bfd *sub;
13446 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13447
13448 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13449 {
13450 asection *o;
13451
13452 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13453 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13454 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13455 continue;
13456 o = sub->sections;
13457 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13458 continue;
13459
13460 for (o = sub->sections; o != NULL; o = o->next)
13461 {
13462 /* When any section in a section group is kept, we keep all
13463 sections in the section group. If the first member of
13464 the section group is excluded, we will also exclude the
13465 group section. */
13466 if (o->flags & SEC_GROUP)
13467 {
13468 asection *first = elf_next_in_group (o);
13469 o->gc_mark = first->gc_mark;
13470 }
13471
13472 if (o->gc_mark)
13473 continue;
13474
13475 /* Skip sweeping sections already excluded. */
13476 if (o->flags & SEC_EXCLUDE)
13477 continue;
13478
13479 /* Since this is early in the link process, it is simple
13480 to remove a section from the output. */
13481 o->flags |= SEC_EXCLUDE;
13482
13483 if (info->print_gc_sections && o->size != 0)
13484 /* xgettext:c-format */
13485 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13486 o, sub);
13487 }
13488 }
13489
13490 return TRUE;
13491 }
13492
13493 /* Propagate collected vtable information. This is called through
13494 elf_link_hash_traverse. */
13495
13496 static bfd_boolean
13497 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13498 {
13499 /* Those that are not vtables. */
13500 if (h->start_stop
13501 || h->u2.vtable == NULL
13502 || h->u2.vtable->parent == NULL)
13503 return TRUE;
13504
13505 /* Those vtables that do not have parents, we cannot merge. */
13506 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13507 return TRUE;
13508
13509 /* If we've already been done, exit. */
13510 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13511 return TRUE;
13512
13513 /* Make sure the parent's table is up to date. */
13514 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13515
13516 if (h->u2.vtable->used == NULL)
13517 {
13518 /* None of this table's entries were referenced. Re-use the
13519 parent's table. */
13520 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13521 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13522 }
13523 else
13524 {
13525 size_t n;
13526 bfd_boolean *cu, *pu;
13527
13528 /* Or the parent's entries into ours. */
13529 cu = h->u2.vtable->used;
13530 cu[-1] = TRUE;
13531 pu = h->u2.vtable->parent->u2.vtable->used;
13532 if (pu != NULL)
13533 {
13534 const struct elf_backend_data *bed;
13535 unsigned int log_file_align;
13536
13537 bed = get_elf_backend_data (h->root.u.def.section->owner);
13538 log_file_align = bed->s->log_file_align;
13539 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13540 while (n--)
13541 {
13542 if (*pu)
13543 *cu = TRUE;
13544 pu++;
13545 cu++;
13546 }
13547 }
13548 }
13549
13550 return TRUE;
13551 }
13552
13553 static bfd_boolean
13554 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13555 {
13556 asection *sec;
13557 bfd_vma hstart, hend;
13558 Elf_Internal_Rela *relstart, *relend, *rel;
13559 const struct elf_backend_data *bed;
13560 unsigned int log_file_align;
13561
13562 /* Take care of both those symbols that do not describe vtables as
13563 well as those that are not loaded. */
13564 if (h->start_stop
13565 || h->u2.vtable == NULL
13566 || h->u2.vtable->parent == NULL)
13567 return TRUE;
13568
13569 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13570 || h->root.type == bfd_link_hash_defweak);
13571
13572 sec = h->root.u.def.section;
13573 hstart = h->root.u.def.value;
13574 hend = hstart + h->size;
13575
13576 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13577 if (!relstart)
13578 return *(bfd_boolean *) okp = FALSE;
13579 bed = get_elf_backend_data (sec->owner);
13580 log_file_align = bed->s->log_file_align;
13581
13582 relend = relstart + sec->reloc_count;
13583
13584 for (rel = relstart; rel < relend; ++rel)
13585 if (rel->r_offset >= hstart && rel->r_offset < hend)
13586 {
13587 /* If the entry is in use, do nothing. */
13588 if (h->u2.vtable->used
13589 && (rel->r_offset - hstart) < h->u2.vtable->size)
13590 {
13591 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13592 if (h->u2.vtable->used[entry])
13593 continue;
13594 }
13595 /* Otherwise, kill it. */
13596 rel->r_offset = rel->r_info = rel->r_addend = 0;
13597 }
13598
13599 return TRUE;
13600 }
13601
13602 /* Mark sections containing dynamically referenced symbols. When
13603 building shared libraries, we must assume that any visible symbol is
13604 referenced. */
13605
13606 bfd_boolean
13607 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13608 {
13609 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13610 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13611
13612 if ((h->root.type == bfd_link_hash_defined
13613 || h->root.type == bfd_link_hash_defweak)
13614 && ((h->ref_dynamic && !h->forced_local)
13615 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13616 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13617 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13618 && (!bfd_link_executable (info)
13619 || info->gc_keep_exported
13620 || info->export_dynamic
13621 || (h->dynamic
13622 && d != NULL
13623 && (*d->match) (&d->head, NULL, h->root.root.string)))
13624 && (h->versioned >= versioned
13625 || !bfd_hide_sym_by_version (info->version_info,
13626 h->root.root.string)))))
13627 h->root.u.def.section->flags |= SEC_KEEP;
13628
13629 return TRUE;
13630 }
13631
13632 /* Keep all sections containing symbols undefined on the command-line,
13633 and the section containing the entry symbol. */
13634
13635 void
13636 _bfd_elf_gc_keep (struct bfd_link_info *info)
13637 {
13638 struct bfd_sym_chain *sym;
13639
13640 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13641 {
13642 struct elf_link_hash_entry *h;
13643
13644 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13645 FALSE, FALSE, FALSE);
13646
13647 if (h != NULL
13648 && (h->root.type == bfd_link_hash_defined
13649 || h->root.type == bfd_link_hash_defweak)
13650 && !bfd_is_abs_section (h->root.u.def.section)
13651 && !bfd_is_und_section (h->root.u.def.section))
13652 h->root.u.def.section->flags |= SEC_KEEP;
13653 }
13654 }
13655
13656 bfd_boolean
13657 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13658 struct bfd_link_info *info)
13659 {
13660 bfd *ibfd = info->input_bfds;
13661
13662 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13663 {
13664 asection *sec;
13665 struct elf_reloc_cookie cookie;
13666
13667 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13668 continue;
13669 sec = ibfd->sections;
13670 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13671 continue;
13672
13673 if (!init_reloc_cookie (&cookie, info, ibfd))
13674 return FALSE;
13675
13676 for (sec = ibfd->sections; sec; sec = sec->next)
13677 {
13678 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13679 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13680 {
13681 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13682 fini_reloc_cookie_rels (&cookie, sec);
13683 }
13684 }
13685 }
13686 return TRUE;
13687 }
13688
13689 /* Do mark and sweep of unused sections. */
13690
13691 bfd_boolean
13692 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13693 {
13694 bfd_boolean ok = TRUE;
13695 bfd *sub;
13696 elf_gc_mark_hook_fn gc_mark_hook;
13697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13698 struct elf_link_hash_table *htab;
13699
13700 if (!bed->can_gc_sections
13701 || !is_elf_hash_table (info->hash))
13702 {
13703 _bfd_error_handler(_("warning: gc-sections option ignored"));
13704 return TRUE;
13705 }
13706
13707 bed->gc_keep (info);
13708 htab = elf_hash_table (info);
13709
13710 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13711 at the .eh_frame section if we can mark the FDEs individually. */
13712 for (sub = info->input_bfds;
13713 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13714 sub = sub->link.next)
13715 {
13716 asection *sec;
13717 struct elf_reloc_cookie cookie;
13718
13719 sec = sub->sections;
13720 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13721 continue;
13722 sec = bfd_get_section_by_name (sub, ".eh_frame");
13723 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13724 {
13725 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13726 if (elf_section_data (sec)->sec_info
13727 && (sec->flags & SEC_LINKER_CREATED) == 0)
13728 elf_eh_frame_section (sub) = sec;
13729 fini_reloc_cookie_for_section (&cookie, sec);
13730 sec = bfd_get_next_section_by_name (NULL, sec);
13731 }
13732 }
13733
13734 /* Apply transitive closure to the vtable entry usage info. */
13735 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13736 if (!ok)
13737 return FALSE;
13738
13739 /* Kill the vtable relocations that were not used. */
13740 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13741 if (!ok)
13742 return FALSE;
13743
13744 /* Mark dynamically referenced symbols. */
13745 if (htab->dynamic_sections_created || info->gc_keep_exported)
13746 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13747
13748 /* Grovel through relocs to find out who stays ... */
13749 gc_mark_hook = bed->gc_mark_hook;
13750 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13751 {
13752 asection *o;
13753
13754 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13755 || elf_object_id (sub) != elf_hash_table_id (htab)
13756 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13757 continue;
13758
13759 o = sub->sections;
13760 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13761 continue;
13762
13763 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13764 Also treat note sections as a root, if the section is not part
13765 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13766 well as FINI_ARRAY sections for ld -r. */
13767 for (o = sub->sections; o != NULL; o = o->next)
13768 if (!o->gc_mark
13769 && (o->flags & SEC_EXCLUDE) == 0
13770 && ((o->flags & SEC_KEEP) != 0
13771 || (bfd_link_relocatable (info)
13772 && ((elf_section_data (o)->this_hdr.sh_type
13773 == SHT_PREINIT_ARRAY)
13774 || (elf_section_data (o)->this_hdr.sh_type
13775 == SHT_INIT_ARRAY)
13776 || (elf_section_data (o)->this_hdr.sh_type
13777 == SHT_FINI_ARRAY)))
13778 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13779 && elf_next_in_group (o) == NULL )))
13780 {
13781 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13782 return FALSE;
13783 }
13784 }
13785
13786 /* Allow the backend to mark additional target specific sections. */
13787 bed->gc_mark_extra_sections (info, gc_mark_hook);
13788
13789 /* ... and mark SEC_EXCLUDE for those that go. */
13790 return elf_gc_sweep (abfd, info);
13791 }
13792 \f
13793 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13794
13795 bfd_boolean
13796 bfd_elf_gc_record_vtinherit (bfd *abfd,
13797 asection *sec,
13798 struct elf_link_hash_entry *h,
13799 bfd_vma offset)
13800 {
13801 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13802 struct elf_link_hash_entry **search, *child;
13803 size_t extsymcount;
13804 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13805
13806 /* The sh_info field of the symtab header tells us where the
13807 external symbols start. We don't care about the local symbols at
13808 this point. */
13809 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13810 if (!elf_bad_symtab (abfd))
13811 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13812
13813 sym_hashes = elf_sym_hashes (abfd);
13814 sym_hashes_end = sym_hashes + extsymcount;
13815
13816 /* Hunt down the child symbol, which is in this section at the same
13817 offset as the relocation. */
13818 for (search = sym_hashes; search != sym_hashes_end; ++search)
13819 {
13820 if ((child = *search) != NULL
13821 && (child->root.type == bfd_link_hash_defined
13822 || child->root.type == bfd_link_hash_defweak)
13823 && child->root.u.def.section == sec
13824 && child->root.u.def.value == offset)
13825 goto win;
13826 }
13827
13828 /* xgettext:c-format */
13829 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13830 abfd, sec, (uint64_t) offset);
13831 bfd_set_error (bfd_error_invalid_operation);
13832 return FALSE;
13833
13834 win:
13835 if (!child->u2.vtable)
13836 {
13837 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13838 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13839 if (!child->u2.vtable)
13840 return FALSE;
13841 }
13842 if (!h)
13843 {
13844 /* This *should* only be the absolute section. It could potentially
13845 be that someone has defined a non-global vtable though, which
13846 would be bad. It isn't worth paging in the local symbols to be
13847 sure though; that case should simply be handled by the assembler. */
13848
13849 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13850 }
13851 else
13852 child->u2.vtable->parent = h;
13853
13854 return TRUE;
13855 }
13856
13857 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13858
13859 bfd_boolean
13860 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13861 struct elf_link_hash_entry *h,
13862 bfd_vma addend)
13863 {
13864 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13865 unsigned int log_file_align = bed->s->log_file_align;
13866
13867 if (!h)
13868 {
13869 /* xgettext:c-format */
13870 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13871 abfd, sec);
13872 bfd_set_error (bfd_error_bad_value);
13873 return FALSE;
13874 }
13875
13876 if (!h->u2.vtable)
13877 {
13878 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13879 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13880 if (!h->u2.vtable)
13881 return FALSE;
13882 }
13883
13884 if (addend >= h->u2.vtable->size)
13885 {
13886 size_t size, bytes, file_align;
13887 bfd_boolean *ptr = h->u2.vtable->used;
13888
13889 /* While the symbol is undefined, we have to be prepared to handle
13890 a zero size. */
13891 file_align = 1 << log_file_align;
13892 if (h->root.type == bfd_link_hash_undefined)
13893 size = addend + file_align;
13894 else
13895 {
13896 size = h->size;
13897 if (addend >= size)
13898 {
13899 /* Oops! We've got a reference past the defined end of
13900 the table. This is probably a bug -- shall we warn? */
13901 size = addend + file_align;
13902 }
13903 }
13904 size = (size + file_align - 1) & -file_align;
13905
13906 /* Allocate one extra entry for use as a "done" flag for the
13907 consolidation pass. */
13908 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13909
13910 if (ptr)
13911 {
13912 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13913
13914 if (ptr != NULL)
13915 {
13916 size_t oldbytes;
13917
13918 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13919 * sizeof (bfd_boolean));
13920 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13921 }
13922 }
13923 else
13924 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13925
13926 if (ptr == NULL)
13927 return FALSE;
13928
13929 /* And arrange for that done flag to be at index -1. */
13930 h->u2.vtable->used = ptr + 1;
13931 h->u2.vtable->size = size;
13932 }
13933
13934 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13935
13936 return TRUE;
13937 }
13938
13939 /* Map an ELF section header flag to its corresponding string. */
13940 typedef struct
13941 {
13942 char *flag_name;
13943 flagword flag_value;
13944 } elf_flags_to_name_table;
13945
13946 static elf_flags_to_name_table elf_flags_to_names [] =
13947 {
13948 { "SHF_WRITE", SHF_WRITE },
13949 { "SHF_ALLOC", SHF_ALLOC },
13950 { "SHF_EXECINSTR", SHF_EXECINSTR },
13951 { "SHF_MERGE", SHF_MERGE },
13952 { "SHF_STRINGS", SHF_STRINGS },
13953 { "SHF_INFO_LINK", SHF_INFO_LINK},
13954 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13955 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13956 { "SHF_GROUP", SHF_GROUP },
13957 { "SHF_TLS", SHF_TLS },
13958 { "SHF_MASKOS", SHF_MASKOS },
13959 { "SHF_EXCLUDE", SHF_EXCLUDE },
13960 };
13961
13962 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13963 bfd_boolean
13964 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13965 struct flag_info *flaginfo,
13966 asection *section)
13967 {
13968 const bfd_vma sh_flags = elf_section_flags (section);
13969
13970 if (!flaginfo->flags_initialized)
13971 {
13972 bfd *obfd = info->output_bfd;
13973 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13974 struct flag_info_list *tf = flaginfo->flag_list;
13975 int with_hex = 0;
13976 int without_hex = 0;
13977
13978 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13979 {
13980 unsigned i;
13981 flagword (*lookup) (char *);
13982
13983 lookup = bed->elf_backend_lookup_section_flags_hook;
13984 if (lookup != NULL)
13985 {
13986 flagword hexval = (*lookup) ((char *) tf->name);
13987
13988 if (hexval != 0)
13989 {
13990 if (tf->with == with_flags)
13991 with_hex |= hexval;
13992 else if (tf->with == without_flags)
13993 without_hex |= hexval;
13994 tf->valid = TRUE;
13995 continue;
13996 }
13997 }
13998 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13999 {
14000 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14001 {
14002 if (tf->with == with_flags)
14003 with_hex |= elf_flags_to_names[i].flag_value;
14004 else if (tf->with == without_flags)
14005 without_hex |= elf_flags_to_names[i].flag_value;
14006 tf->valid = TRUE;
14007 break;
14008 }
14009 }
14010 if (!tf->valid)
14011 {
14012 info->callbacks->einfo
14013 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14014 return FALSE;
14015 }
14016 }
14017 flaginfo->flags_initialized = TRUE;
14018 flaginfo->only_with_flags |= with_hex;
14019 flaginfo->not_with_flags |= without_hex;
14020 }
14021
14022 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14023 return FALSE;
14024
14025 if ((flaginfo->not_with_flags & sh_flags) != 0)
14026 return FALSE;
14027
14028 return TRUE;
14029 }
14030
14031 struct alloc_got_off_arg {
14032 bfd_vma gotoff;
14033 struct bfd_link_info *info;
14034 };
14035
14036 /* We need a special top-level link routine to convert got reference counts
14037 to real got offsets. */
14038
14039 static bfd_boolean
14040 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14041 {
14042 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14043 bfd *obfd = gofarg->info->output_bfd;
14044 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14045
14046 if (h->got.refcount > 0)
14047 {
14048 h->got.offset = gofarg->gotoff;
14049 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14050 }
14051 else
14052 h->got.offset = (bfd_vma) -1;
14053
14054 return TRUE;
14055 }
14056
14057 /* And an accompanying bit to work out final got entry offsets once
14058 we're done. Should be called from final_link. */
14059
14060 bfd_boolean
14061 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14062 struct bfd_link_info *info)
14063 {
14064 bfd *i;
14065 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14066 bfd_vma gotoff;
14067 struct alloc_got_off_arg gofarg;
14068
14069 BFD_ASSERT (abfd == info->output_bfd);
14070
14071 if (! is_elf_hash_table (info->hash))
14072 return FALSE;
14073
14074 /* The GOT offset is relative to the .got section, but the GOT header is
14075 put into the .got.plt section, if the backend uses it. */
14076 if (bed->want_got_plt)
14077 gotoff = 0;
14078 else
14079 gotoff = bed->got_header_size;
14080
14081 /* Do the local .got entries first. */
14082 for (i = info->input_bfds; i; i = i->link.next)
14083 {
14084 bfd_signed_vma *local_got;
14085 size_t j, locsymcount;
14086 Elf_Internal_Shdr *symtab_hdr;
14087
14088 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14089 continue;
14090
14091 local_got = elf_local_got_refcounts (i);
14092 if (!local_got)
14093 continue;
14094
14095 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14096 if (elf_bad_symtab (i))
14097 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14098 else
14099 locsymcount = symtab_hdr->sh_info;
14100
14101 for (j = 0; j < locsymcount; ++j)
14102 {
14103 if (local_got[j] > 0)
14104 {
14105 local_got[j] = gotoff;
14106 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14107 }
14108 else
14109 local_got[j] = (bfd_vma) -1;
14110 }
14111 }
14112
14113 /* Then the global .got entries. .plt refcounts are handled by
14114 adjust_dynamic_symbol */
14115 gofarg.gotoff = gotoff;
14116 gofarg.info = info;
14117 elf_link_hash_traverse (elf_hash_table (info),
14118 elf_gc_allocate_got_offsets,
14119 &gofarg);
14120 return TRUE;
14121 }
14122
14123 /* Many folk need no more in the way of final link than this, once
14124 got entry reference counting is enabled. */
14125
14126 bfd_boolean
14127 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14128 {
14129 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14130 return FALSE;
14131
14132 /* Invoke the regular ELF backend linker to do all the work. */
14133 return bfd_elf_final_link (abfd, info);
14134 }
14135
14136 bfd_boolean
14137 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14138 {
14139 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14140
14141 if (rcookie->bad_symtab)
14142 rcookie->rel = rcookie->rels;
14143
14144 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14145 {
14146 unsigned long r_symndx;
14147
14148 if (! rcookie->bad_symtab)
14149 if (rcookie->rel->r_offset > offset)
14150 return FALSE;
14151 if (rcookie->rel->r_offset != offset)
14152 continue;
14153
14154 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14155 if (r_symndx == STN_UNDEF)
14156 return TRUE;
14157
14158 if (r_symndx >= rcookie->locsymcount
14159 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14160 {
14161 struct elf_link_hash_entry *h;
14162
14163 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14164
14165 while (h->root.type == bfd_link_hash_indirect
14166 || h->root.type == bfd_link_hash_warning)
14167 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14168
14169 if ((h->root.type == bfd_link_hash_defined
14170 || h->root.type == bfd_link_hash_defweak)
14171 && (h->root.u.def.section->owner != rcookie->abfd
14172 || h->root.u.def.section->kept_section != NULL
14173 || discarded_section (h->root.u.def.section)))
14174 return TRUE;
14175 }
14176 else
14177 {
14178 /* It's not a relocation against a global symbol,
14179 but it could be a relocation against a local
14180 symbol for a discarded section. */
14181 asection *isec;
14182 Elf_Internal_Sym *isym;
14183
14184 /* Need to: get the symbol; get the section. */
14185 isym = &rcookie->locsyms[r_symndx];
14186 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14187 if (isec != NULL
14188 && (isec->kept_section != NULL
14189 || discarded_section (isec)))
14190 return TRUE;
14191 }
14192 return FALSE;
14193 }
14194 return FALSE;
14195 }
14196
14197 /* Discard unneeded references to discarded sections.
14198 Returns -1 on error, 1 if any section's size was changed, 0 if
14199 nothing changed. This function assumes that the relocations are in
14200 sorted order, which is true for all known assemblers. */
14201
14202 int
14203 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14204 {
14205 struct elf_reloc_cookie cookie;
14206 asection *o;
14207 bfd *abfd;
14208 int changed = 0;
14209
14210 if (info->traditional_format
14211 || !is_elf_hash_table (info->hash))
14212 return 0;
14213
14214 o = bfd_get_section_by_name (output_bfd, ".stab");
14215 if (o != NULL)
14216 {
14217 asection *i;
14218
14219 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14220 {
14221 if (i->size == 0
14222 || i->reloc_count == 0
14223 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14224 continue;
14225
14226 abfd = i->owner;
14227 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14228 continue;
14229
14230 if (!init_reloc_cookie_for_section (&cookie, info, i))
14231 return -1;
14232
14233 if (_bfd_discard_section_stabs (abfd, i,
14234 elf_section_data (i)->sec_info,
14235 bfd_elf_reloc_symbol_deleted_p,
14236 &cookie))
14237 changed = 1;
14238
14239 fini_reloc_cookie_for_section (&cookie, i);
14240 }
14241 }
14242
14243 o = NULL;
14244 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14245 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14246 if (o != NULL)
14247 {
14248 asection *i;
14249 int eh_changed = 0;
14250 unsigned int eh_alignment;
14251
14252 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14253 {
14254 if (i->size == 0)
14255 continue;
14256
14257 abfd = i->owner;
14258 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14259 continue;
14260
14261 if (!init_reloc_cookie_for_section (&cookie, info, i))
14262 return -1;
14263
14264 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14265 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14266 bfd_elf_reloc_symbol_deleted_p,
14267 &cookie))
14268 {
14269 eh_changed = 1;
14270 if (i->size != i->rawsize)
14271 changed = 1;
14272 }
14273
14274 fini_reloc_cookie_for_section (&cookie, i);
14275 }
14276
14277 eh_alignment = 1 << o->alignment_power;
14278 /* Skip over zero terminator, and prevent empty sections from
14279 adding alignment padding at the end. */
14280 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14281 if (i->size == 0)
14282 i->flags |= SEC_EXCLUDE;
14283 else if (i->size > 4)
14284 break;
14285 /* The last non-empty eh_frame section doesn't need padding. */
14286 if (i != NULL)
14287 i = i->map_tail.s;
14288 /* Any prior sections must pad the last FDE out to the output
14289 section alignment. Otherwise we might have zero padding
14290 between sections, which would be seen as a terminator. */
14291 for (; i != NULL; i = i->map_tail.s)
14292 if (i->size == 4)
14293 /* All but the last zero terminator should have been removed. */
14294 BFD_FAIL ();
14295 else
14296 {
14297 bfd_size_type size
14298 = (i->size + eh_alignment - 1) & -eh_alignment;
14299 if (i->size != size)
14300 {
14301 i->size = size;
14302 changed = 1;
14303 eh_changed = 1;
14304 }
14305 }
14306 if (eh_changed)
14307 elf_link_hash_traverse (elf_hash_table (info),
14308 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14309 }
14310
14311 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14312 {
14313 const struct elf_backend_data *bed;
14314 asection *s;
14315
14316 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14317 continue;
14318 s = abfd->sections;
14319 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14320 continue;
14321
14322 bed = get_elf_backend_data (abfd);
14323
14324 if (bed->elf_backend_discard_info != NULL)
14325 {
14326 if (!init_reloc_cookie (&cookie, info, abfd))
14327 return -1;
14328
14329 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14330 changed = 1;
14331
14332 fini_reloc_cookie (&cookie, abfd);
14333 }
14334 }
14335
14336 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14337 _bfd_elf_end_eh_frame_parsing (info);
14338
14339 if (info->eh_frame_hdr_type
14340 && !bfd_link_relocatable (info)
14341 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14342 changed = 1;
14343
14344 return changed;
14345 }
14346
14347 bfd_boolean
14348 _bfd_elf_section_already_linked (bfd *abfd,
14349 asection *sec,
14350 struct bfd_link_info *info)
14351 {
14352 flagword flags;
14353 const char *name, *key;
14354 struct bfd_section_already_linked *l;
14355 struct bfd_section_already_linked_hash_entry *already_linked_list;
14356
14357 if (sec->output_section == bfd_abs_section_ptr)
14358 return FALSE;
14359
14360 flags = sec->flags;
14361
14362 /* Return if it isn't a linkonce section. A comdat group section
14363 also has SEC_LINK_ONCE set. */
14364 if ((flags & SEC_LINK_ONCE) == 0)
14365 return FALSE;
14366
14367 /* Don't put group member sections on our list of already linked
14368 sections. They are handled as a group via their group section. */
14369 if (elf_sec_group (sec) != NULL)
14370 return FALSE;
14371
14372 /* For a SHT_GROUP section, use the group signature as the key. */
14373 name = sec->name;
14374 if ((flags & SEC_GROUP) != 0
14375 && elf_next_in_group (sec) != NULL
14376 && elf_group_name (elf_next_in_group (sec)) != NULL)
14377 key = elf_group_name (elf_next_in_group (sec));
14378 else
14379 {
14380 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14381 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14382 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14383 key++;
14384 else
14385 /* Must be a user linkonce section that doesn't follow gcc's
14386 naming convention. In this case we won't be matching
14387 single member groups. */
14388 key = name;
14389 }
14390
14391 already_linked_list = bfd_section_already_linked_table_lookup (key);
14392
14393 for (l = already_linked_list->entry; l != NULL; l = l->next)
14394 {
14395 /* We may have 2 different types of sections on the list: group
14396 sections with a signature of <key> (<key> is some string),
14397 and linkonce sections named .gnu.linkonce.<type>.<key>.
14398 Match like sections. LTO plugin sections are an exception.
14399 They are always named .gnu.linkonce.t.<key> and match either
14400 type of section. */
14401 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14402 && ((flags & SEC_GROUP) != 0
14403 || strcmp (name, l->sec->name) == 0))
14404 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14405 {
14406 /* The section has already been linked. See if we should
14407 issue a warning. */
14408 if (!_bfd_handle_already_linked (sec, l, info))
14409 return FALSE;
14410
14411 if (flags & SEC_GROUP)
14412 {
14413 asection *first = elf_next_in_group (sec);
14414 asection *s = first;
14415
14416 while (s != NULL)
14417 {
14418 s->output_section = bfd_abs_section_ptr;
14419 /* Record which group discards it. */
14420 s->kept_section = l->sec;
14421 s = elf_next_in_group (s);
14422 /* These lists are circular. */
14423 if (s == first)
14424 break;
14425 }
14426 }
14427
14428 return TRUE;
14429 }
14430 }
14431
14432 /* A single member comdat group section may be discarded by a
14433 linkonce section and vice versa. */
14434 if ((flags & SEC_GROUP) != 0)
14435 {
14436 asection *first = elf_next_in_group (sec);
14437
14438 if (first != NULL && elf_next_in_group (first) == first)
14439 /* Check this single member group against linkonce sections. */
14440 for (l = already_linked_list->entry; l != NULL; l = l->next)
14441 if ((l->sec->flags & SEC_GROUP) == 0
14442 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14443 {
14444 first->output_section = bfd_abs_section_ptr;
14445 first->kept_section = l->sec;
14446 sec->output_section = bfd_abs_section_ptr;
14447 break;
14448 }
14449 }
14450 else
14451 /* Check this linkonce section against single member groups. */
14452 for (l = already_linked_list->entry; l != NULL; l = l->next)
14453 if (l->sec->flags & SEC_GROUP)
14454 {
14455 asection *first = elf_next_in_group (l->sec);
14456
14457 if (first != NULL
14458 && elf_next_in_group (first) == first
14459 && bfd_elf_match_symbols_in_sections (first, sec, info))
14460 {
14461 sec->output_section = bfd_abs_section_ptr;
14462 sec->kept_section = first;
14463 break;
14464 }
14465 }
14466
14467 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14468 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14469 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14470 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14471 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14472 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14473 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14474 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14475 The reverse order cannot happen as there is never a bfd with only the
14476 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14477 matter as here were are looking only for cross-bfd sections. */
14478
14479 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14480 for (l = already_linked_list->entry; l != NULL; l = l->next)
14481 if ((l->sec->flags & SEC_GROUP) == 0
14482 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14483 {
14484 if (abfd != l->sec->owner)
14485 sec->output_section = bfd_abs_section_ptr;
14486 break;
14487 }
14488
14489 /* This is the first section with this name. Record it. */
14490 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14491 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14492 return sec->output_section == bfd_abs_section_ptr;
14493 }
14494
14495 bfd_boolean
14496 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14497 {
14498 return sym->st_shndx == SHN_COMMON;
14499 }
14500
14501 unsigned int
14502 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14503 {
14504 return SHN_COMMON;
14505 }
14506
14507 asection *
14508 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14509 {
14510 return bfd_com_section_ptr;
14511 }
14512
14513 bfd_vma
14514 _bfd_elf_default_got_elt_size (bfd *abfd,
14515 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14516 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14517 bfd *ibfd ATTRIBUTE_UNUSED,
14518 unsigned long symndx ATTRIBUTE_UNUSED)
14519 {
14520 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14521 return bed->s->arch_size / 8;
14522 }
14523
14524 /* Routines to support the creation of dynamic relocs. */
14525
14526 /* Returns the name of the dynamic reloc section associated with SEC. */
14527
14528 static const char *
14529 get_dynamic_reloc_section_name (bfd * abfd,
14530 asection * sec,
14531 bfd_boolean is_rela)
14532 {
14533 char *name;
14534 const char *old_name = bfd_section_name (sec);
14535 const char *prefix = is_rela ? ".rela" : ".rel";
14536
14537 if (old_name == NULL)
14538 return NULL;
14539
14540 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14541 sprintf (name, "%s%s", prefix, old_name);
14542
14543 return name;
14544 }
14545
14546 /* Returns the dynamic reloc section associated with SEC.
14547 If necessary compute the name of the dynamic reloc section based
14548 on SEC's name (looked up in ABFD's string table) and the setting
14549 of IS_RELA. */
14550
14551 asection *
14552 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14553 asection * sec,
14554 bfd_boolean is_rela)
14555 {
14556 asection * reloc_sec = elf_section_data (sec)->sreloc;
14557
14558 if (reloc_sec == NULL)
14559 {
14560 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14561
14562 if (name != NULL)
14563 {
14564 reloc_sec = bfd_get_linker_section (abfd, name);
14565
14566 if (reloc_sec != NULL)
14567 elf_section_data (sec)->sreloc = reloc_sec;
14568 }
14569 }
14570
14571 return reloc_sec;
14572 }
14573
14574 /* Returns the dynamic reloc section associated with SEC. If the
14575 section does not exist it is created and attached to the DYNOBJ
14576 bfd and stored in the SRELOC field of SEC's elf_section_data
14577 structure.
14578
14579 ALIGNMENT is the alignment for the newly created section and
14580 IS_RELA defines whether the name should be .rela.<SEC's name>
14581 or .rel.<SEC's name>. The section name is looked up in the
14582 string table associated with ABFD. */
14583
14584 asection *
14585 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14586 bfd *dynobj,
14587 unsigned int alignment,
14588 bfd *abfd,
14589 bfd_boolean is_rela)
14590 {
14591 asection * reloc_sec = elf_section_data (sec)->sreloc;
14592
14593 if (reloc_sec == NULL)
14594 {
14595 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14596
14597 if (name == NULL)
14598 return NULL;
14599
14600 reloc_sec = bfd_get_linker_section (dynobj, name);
14601
14602 if (reloc_sec == NULL)
14603 {
14604 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14605 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14606 if ((sec->flags & SEC_ALLOC) != 0)
14607 flags |= SEC_ALLOC | SEC_LOAD;
14608
14609 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14610 if (reloc_sec != NULL)
14611 {
14612 /* _bfd_elf_get_sec_type_attr chooses a section type by
14613 name. Override as it may be wrong, eg. for a user
14614 section named "auto" we'll get ".relauto" which is
14615 seen to be a .rela section. */
14616 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14617 if (!bfd_set_section_alignment (reloc_sec, alignment))
14618 reloc_sec = NULL;
14619 }
14620 }
14621
14622 elf_section_data (sec)->sreloc = reloc_sec;
14623 }
14624
14625 return reloc_sec;
14626 }
14627
14628 /* Copy the ELF symbol type and other attributes for a linker script
14629 assignment from HSRC to HDEST. Generally this should be treated as
14630 if we found a strong non-dynamic definition for HDEST (except that
14631 ld ignores multiple definition errors). */
14632 void
14633 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14634 struct bfd_link_hash_entry *hdest,
14635 struct bfd_link_hash_entry *hsrc)
14636 {
14637 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14638 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14639 Elf_Internal_Sym isym;
14640
14641 ehdest->type = ehsrc->type;
14642 ehdest->target_internal = ehsrc->target_internal;
14643
14644 isym.st_other = ehsrc->other;
14645 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14646 }
14647
14648 /* Append a RELA relocation REL to section S in BFD. */
14649
14650 void
14651 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14652 {
14653 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14654 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14655 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14656 bed->s->swap_reloca_out (abfd, rel, loc);
14657 }
14658
14659 /* Append a REL relocation REL to section S in BFD. */
14660
14661 void
14662 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14663 {
14664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14665 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14666 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14667 bed->s->swap_reloc_out (abfd, rel, loc);
14668 }
14669
14670 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14671
14672 struct bfd_link_hash_entry *
14673 bfd_elf_define_start_stop (struct bfd_link_info *info,
14674 const char *symbol, asection *sec)
14675 {
14676 struct elf_link_hash_entry *h;
14677
14678 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14679 FALSE, FALSE, TRUE);
14680 if (h != NULL
14681 && (h->root.type == bfd_link_hash_undefined
14682 || h->root.type == bfd_link_hash_undefweak
14683 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14684 {
14685 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14686 h->root.type = bfd_link_hash_defined;
14687 h->root.u.def.section = sec;
14688 h->root.u.def.value = 0;
14689 h->def_regular = 1;
14690 h->def_dynamic = 0;
14691 h->start_stop = 1;
14692 h->u2.start_stop_section = sec;
14693 if (symbol[0] == '.')
14694 {
14695 /* .startof. and .sizeof. symbols are local. */
14696 const struct elf_backend_data *bed;
14697 bed = get_elf_backend_data (info->output_bfd);
14698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14699 }
14700 else
14701 {
14702 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14704 if (was_dynamic)
14705 bfd_elf_link_record_dynamic_symbol (info, h);
14706 }
14707 return &h->root;
14708 }
14709 return NULL;
14710 }
This page took 0.495482 seconds and 5 git commands to generate.