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