]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 return FALSE;
181 htab->sgotplt = s;
182 }
183
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
196 if (h == NULL)
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202 \f
203 /* Create a strtab to hold the dynamic symbol names. */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 asection *s;
220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
221 if ((ibfd->flags
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242 }
243
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
250
251 bfd_boolean
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 {
254 flagword flags;
255 asection *s;
256 const struct elf_backend_data *bed;
257 struct elf_link_hash_entry *h;
258
259 if (! is_elf_hash_table (info->hash))
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
267
268 abfd = elf_hash_table (info)->dynobj;
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info) && !info->nointerp)
276 {
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
279 if (s == NULL)
280 return FALSE;
281 }
282
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || !bfd_set_section_alignment (s, 1))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
301 return FALSE;
302
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
305 if (s == NULL
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
307 return FALSE;
308 elf_hash_table (info)->dynsym = s;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
312 if (s == NULL)
313 return FALSE;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
316 if (s == NULL
317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
329 return FALSE;
330
331 if (info->emit_hash)
332 {
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
335 if (s == NULL
336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
342 {
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
345 if (s == NULL
346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367 }
368
369 /* Create dynamic sections when linking against a dynamic object. */
370
371 bfd_boolean
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
373 {
374 flagword flags, pltflags;
375 struct elf_link_hash_entry *h;
376 asection *s;
377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378 struct elf_link_hash_table *htab = elf_hash_table (info);
379
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags = bed->dynamic_sec_flags;
383
384 pltflags = flags;
385 if (bed->plt_not_loaded)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
396 if (s == NULL
397 || !bfd_set_section_alignment (s, bed->plt_alignment))
398 return FALSE;
399 htab->splt = s;
400
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
411
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
416 if (s == NULL
417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
418 return FALSE;
419 htab->srelplt = s;
420
421 if (! _bfd_elf_create_got_section (abfd, info))
422 return FALSE;
423
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 SEC_ALLOC | SEC_LINKER_CREATED);
434 if (s == NULL)
435 return FALSE;
436 htab->sdynbss = s;
437
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 flags);
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
461 if (bfd_link_executable (info))
462 {
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return FALSE;
470 htab->srelbss = s;
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
483 }
484 }
485
486 return TRUE;
487 }
488 \f
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
497 bfd_boolean
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
500 {
501 if (h->dynindx == -1)
502 {
503 struct elf_strtab_hash *dynstr;
504 char *p;
505 const char *name;
506 size_t indx;
507
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
513 {
514 case STV_INTERNAL:
515 case STV_HIDDEN:
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
518 {
519 h->forced_local = 1;
520 if (!elf_hash_table (info)->is_relocatable_executable)
521 return TRUE;
522 }
523
524 default:
525 break;
526 }
527
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
530
531 dynstr = elf_hash_table (info)->dynstr;
532 if (dynstr == NULL)
533 {
534 /* Create a strtab to hold the dynamic symbol names. */
535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
536 if (dynstr == NULL)
537 return FALSE;
538 }
539
540 /* We don't put any version information in the dynamic string
541 table. */
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
544 if (p != NULL)
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
550 *p = 0;
551
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553
554 if (p != NULL)
555 *p = ELF_VER_CHR;
556
557 if (indx == (size_t) -1)
558 return FALSE;
559 h->dynstr_index = indx;
560 }
561
562 return TRUE;
563 }
564 \f
565 /* Mark a symbol dynamic. */
566
567 static void
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
571 {
572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
573
574 /* It may be called more than once on the same H. */
575 if(h->dynamic || bfd_link_relocatable (info))
576 return;
577
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
580 || h->type == STT_COMMON
581 || (sym != NULL
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
584 || (d != NULL
585 && h->non_elf
586 && (*d->match) (&d->head, NULL, h->root.root.string)))
587 {
588 h->dynamic = 1;
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 non-IR reference. */
591 h->root.non_ir_ref_dynamic = 1;
592 }
593 }
594
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598 bfd_boolean
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
601 const char *name,
602 bfd_boolean provide,
603 bfd_boolean hidden)
604 {
605 struct elf_link_hash_entry *h, *hv;
606 struct elf_link_hash_table *htab;
607 const struct elf_backend_data *bed;
608
609 if (!is_elf_hash_table (info->hash))
610 return TRUE;
611
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h == NULL)
615 return provide;
616
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
641 switch (h->root.type)
642 {
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h->root.type = bfd_link_hash_new;
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
655 break;
656 case bfd_link_hash_new:
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
673 default:
674 BFD_FAIL ();
675 return FALSE;
676 }
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
683 && h->def_dynamic
684 && !h->def_regular)
685 h->root.type = bfd_link_hash_undefined;
686
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
690 more. */
691 if (h->def_dynamic && !h->def_regular)
692 h->verinfo.verdef = NULL;
693
694 /* Make sure this symbol is not garbage collected. */
695 h->mark = 1;
696
697 h->def_regular = 1;
698
699 if (hidden)
700 {
701 bed = get_elf_backend_data (output_bfd);
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705 }
706
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 and executables. */
709 if (!bfd_link_relocatable (info)
710 && h->dynindx != -1
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713 h->forced_local = 1;
714
715 if ((h->def_dynamic
716 || h->ref_dynamic
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
719 && !h->forced_local
720 && h->dynindx == -1)
721 {
722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 return FALSE;
724
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
728 if (h->is_weakalias)
729 {
730 struct elf_link_hash_entry *def = weakdef (h);
731
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
734 return FALSE;
735 }
736 }
737
738 return TRUE;
739 }
740
741 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
744
745 int
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 bfd *input_bfd,
748 long input_indx)
749 {
750 size_t amt;
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
754 size_t dynstr_index;
755 char *name;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
758
759 if (! is_elf_hash_table (info->hash))
760 return 0;
761
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 return 1;
766
767 amt = sizeof (*entry);
768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
769 if (entry == NULL)
770 return 0;
771
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
774 1, input_indx, &entry->isym, esym, &eshndx))
775 {
776 bfd_release (input_bfd, entry);
777 return 0;
778 }
779
780 if (entry->isym.st_shndx != SHN_UNDEF
781 && entry->isym.st_shndx < SHN_LORESERVE)
782 {
783 asection *s;
784
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
787 {
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
791 return 2;
792 }
793 }
794
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
798
799 dynstr = elf_hash_table (info)->dynstr;
800 if (dynstr == NULL)
801 {
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 if (dynstr == NULL)
805 return 0;
806 }
807
808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
809 if (dynstr_index == (size_t) -1)
810 return 0;
811 entry->isym.st_name = dynstr_index;
812
813 eht = elf_hash_table (info);
814
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
819 eht->dynsymcount++;
820
821 /* Whatever binding the symbol had before, it's now local. */
822 entry->isym.st_info
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824
825 /* The dynindx will be set at the end of size_dynamic_sections. */
826
827 return 1;
828 }
829
830 /* Return the dynindex of a local dynamic symbol. */
831
832 long
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 bfd *input_bfd,
835 long input_indx)
836 {
837 struct elf_link_local_dynamic_entry *e;
838
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 return e->dynindx;
842 return -1;
843 }
844
845 /* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
848
849 static bfd_boolean
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
852 {
853 size_t *count = (size_t *) data;
854
855 if (h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862 }
863
864
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
867
868 static bfd_boolean
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 void *data)
871 {
872 size_t *count = (size_t *) data;
873
874 if (!h->forced_local)
875 return TRUE;
876
877 if (h->dynindx != -1)
878 h->dynindx = ++(*count);
879
880 return TRUE;
881 }
882
883 /* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
885 bfd_boolean
886 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
888 asection *p)
889 {
890 struct elf_link_hash_table *htab;
891 asection *ip;
892
893 switch (elf_section_data (p)->this_hdr.sh_type)
894 {
895 case SHT_PROGBITS:
896 case SHT_NOBITS:
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
899 case SHT_NULL:
900 htab = elf_hash_table (info);
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
903
904 return (htab->dynobj != NULL
905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
906 && ip->output_section == p);
907
908 /* There shouldn't be section relative relocations
909 against any other section. */
910 default:
911 return TRUE;
912 }
913 }
914
915 bfd_boolean
916 _bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
920 {
921 return TRUE;
922 }
923
924 /* Assign dynsym indices. In a shared library we generate a section
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
932
933 static unsigned long
934 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
937 {
938 unsigned long dynsymcount = 0;
939 bfd_boolean do_sec = section_sym_count != NULL;
940
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
943 {
944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
945 asection *p;
946 for (p = output_bfd->sections; p ; p = p->next)
947 if ((p->flags & SEC_EXCLUDE) == 0
948 && (p->flags & SEC_ALLOC) != 0
949 && elf_hash_table (info)->dynamic_relocs
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
957 elf_section_data (p)->dynindx = 0;
958 }
959 if (do_sec)
960 *section_sym_count = dynsymcount;
961
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
983
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
986 }
987
988 /* Merge st_other field. */
989
990 static void
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
994 {
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
1003 if (!dynamic)
1004 {
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1007
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1012 }
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1017 }
1018
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1031
1032 static bfd_boolean
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1048 {
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1058 char *new_version;
1059 bfd_boolean default_sym = *matched;
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
1075
1076 bed = get_elf_backend_data (abfd);
1077
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1080 {
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
1098 }
1099 else
1100 new_version = NULL;
1101
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
1226 {
1227 h->non_elf = 0;
1228 return TRUE;
1229 }
1230
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1241 return TRUE;
1242
1243 olddyn = FALSE;
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1247 {
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1251 }
1252
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1267
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1271
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1287 {
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
1325 }
1326
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
1342 ntbfd = abfd;
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h->root.root.string, tbfd, ntbfd);
1371 else if (tdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1377 else
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
1396 h->ref_dynamic = 1;
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1409 && h->def_dynamic)
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1425
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1428 {
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
1434 }
1435 else
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
1443 h = hi;
1444 }
1445 else
1446 h = hi;
1447 }
1448
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1457 {
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1468 {
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
1474 }
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
1483
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1501 newweak = FALSE;
1502 if (olddef && newdyn)
1503 oldweak = FALSE;
1504
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1508
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1512
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1525
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
1551 && !newweak
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
1555 && !newfunc)
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
1563 && h->def_dynamic
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
1567 && !oldfunc)
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
1580
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1614
1615 *size_change_ok = TRUE;
1616 }
1617
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1631
1632 if (newdyn
1633 && newdef
1634 && (olddef
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
1641
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1644
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1650
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1670 }
1671
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1674 {
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
1697
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1707
1708 flip = NULL;
1709 if (!newdyn
1710 && (newdef
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1713 && olddyn
1714 && olddef
1715 && h->def_dynamic)
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
1743
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1768
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1805 if (h->def_dynamic)
1806 {
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
1809 }
1810 }
1811
1812 return TRUE;
1813 }
1814
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1819
1820 static bfd_boolean
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
1829 bfd_boolean *dynsym)
1830 {
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1841 char *p;
1842 size_t len, shortlen;
1843 asection *tmp_sec;
1844 bfd_boolean matched;
1845
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
1879
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1897 matched = TRUE;
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
1907 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
1932 if (! override)
1933 {
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1936 {
1937 bh = &hi->root;
1938 if (bh->type == bfd_link_hash_defined
1939 && bh->u.def.section->owner != NULL
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 {
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1944 it. */
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1947 }
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982 if (h->def_dynamic)
1983 {
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
1988 {
1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2013
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
2026 if (! bfd_link_executable (info)
2027 || hi->def_dynamic
2028 || hi->ref_dynamic)
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
2033 if (hi->ref_regular)
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
2042 nondefault:
2043 len = strlen (name);
2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 &hi, poldbfd, NULL, NULL, &skip, &override,
2056 &type_change_ok, &size_change_ok, &matched))
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
2069 _bfd_error_handler
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2072 abfd, shortname);
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
2099 if (! bfd_link_executable (info)
2100 || hi->ref_dynamic)
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
2105 if (hi->ref_regular)
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113 }
2114 \f
2115 /* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2117
2118 static bfd_boolean
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2120 {
2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2122
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2125 return TRUE;
2126
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2129 return TRUE;
2130
2131 if (h->dynindx == -1
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
2135 {
2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2137 {
2138 eif->failed = TRUE;
2139 return FALSE;
2140 }
2141 }
2142
2143 return TRUE;
2144 }
2145 \f
2146 /* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2150
2151 static bfd_boolean
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 void *data)
2154 {
2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
2158 size_t amt;
2159
2160 /* We only care about symbols defined in shared objects with version
2161 information. */
2162 if (!h->def_dynamic
2163 || h->def_regular
2164 || h->dynindx == -1
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2168 return TRUE;
2169
2170 /* See if we already know about this version. */
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 t != NULL;
2173 t = t->vn_nextref)
2174 {
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 continue;
2177
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 return TRUE;
2181
2182 break;
2183 }
2184
2185 /* This is a new version. Add it to tree we are building. */
2186
2187 if (t == NULL)
2188 {
2189 amt = sizeof *t;
2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2191 if (t == NULL)
2192 {
2193 rinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
2200 }
2201
2202 amt = sizeof *a;
2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2204 if (a == NULL)
2205 {
2206 rinfo->failed = TRUE;
2207 return FALSE;
2208 }
2209
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2213 fixed. */
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2218
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220 ++rinfo->vers;
2221
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223
2224 t->vn_auxptr = a;
2225
2226 return TRUE;
2227 }
2228
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2231
2232 static bfd_boolean
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2237 bfd_boolean *hide)
2238 {
2239 struct bfd_elf_version_tree *t;
2240
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2243 {
2244 if (strcmp (t->name, version_p) == 0)
2245 {
2246 size_t len;
2247 char *alc;
2248 struct bfd_elf_version_expr *d;
2249
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2252 if (alc == NULL)
2253 return FALSE;
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2258
2259 h->verinfo.vertree = t;
2260 t->used = TRUE;
2261 d = NULL;
2262
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2265
2266 /* See if there is anything to force this symbol to
2267 local scope. */
2268 if (d == NULL && t->locals.list != NULL)
2269 {
2270 d = (*t->match) (&t->locals, NULL, alc);
2271 if (d != NULL
2272 && h->dynindx != -1
2273 && ! info->export_dynamic)
2274 *hide = TRUE;
2275 }
2276
2277 free (alc);
2278 break;
2279 }
2280 }
2281
2282 *t_p = t;
2283
2284 return TRUE;
2285 }
2286
2287 /* Return TRUE if the symbol H is hidden by version script. */
2288
2289 bfd_boolean
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2292 {
2293 const char *p;
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2297
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300 return TRUE;
2301
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2304 {
2305 struct bfd_elf_version_tree *t;
2306
2307 ++p;
2308 if (*p == ELF_VER_CHR)
2309 ++p;
2310
2311 if (*p != '\0'
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 && hide)
2314 {
2315 if (hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 return TRUE;
2318 }
2319 }
2320
2321 /* If we don't have a version for this symbol, see if we can find
2322 something. */
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324 {
2325 h->verinfo.vertree
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2329 {
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 return TRUE;
2332 }
2333 }
2334
2335 return FALSE;
2336 }
2337
2338 /* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2342
2343 static bfd_boolean
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2345 {
2346 struct elf_info_failed *sinfo;
2347 struct bfd_link_info *info;
2348 const struct elf_backend_data *bed;
2349 struct elf_info_failed eif;
2350 char *p;
2351 bfd_boolean hide;
2352
2353 sinfo = (struct elf_info_failed *) data;
2354 info = sinfo->info;
2355
2356 /* Fix the symbol flags. */
2357 eif.failed = FALSE;
2358 eif.info = info;
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360 {
2361 if (eif.failed)
2362 sinfo->failed = TRUE;
2363 return FALSE;
2364 }
2365
2366 bed = get_elf_backend_data (info->output_bfd);
2367
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
2370 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2371 {
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 return TRUE;
2378 }
2379
2380 hide = FALSE;
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2383 {
2384 struct bfd_elf_version_tree *t;
2385
2386 ++p;
2387 if (*p == ELF_VER_CHR)
2388 ++p;
2389
2390 /* If there is no version string, we can just return out. */
2391 if (*p == '\0')
2392 return TRUE;
2393
2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2395 {
2396 sinfo->failed = TRUE;
2397 return FALSE;
2398 }
2399
2400 if (hide)
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
2405 if (t == NULL && bfd_link_executable (info))
2406 {
2407 struct bfd_elf_version_tree **pp;
2408 int version_index;
2409
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2413 return TRUE;
2414
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 sizeof *t);
2417 if (t == NULL)
2418 {
2419 sinfo->failed = TRUE;
2420 return FALSE;
2421 }
2422
2423 t->name = p;
2424 t->name_indx = (unsigned int) -1;
2425 t->used = TRUE;
2426
2427 version_index = 1;
2428 /* Don't count anonymous version tag. */
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
2431 version_index = 0;
2432 for (pp = &sinfo->info->version_info;
2433 *pp != NULL;
2434 pp = &(*pp)->next)
2435 ++version_index;
2436 t->vernum = version_index;
2437
2438 *pp = t;
2439
2440 h->verinfo.vertree = t;
2441 }
2442 else if (t == NULL)
2443 {
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
2446 _bfd_error_handler
2447 /* xgettext:c-format */
2448 (_("%pB: version node not found for symbol %s"),
2449 info->output_bfd, h->root.root.string);
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2452 return FALSE;
2453 }
2454 }
2455
2456 /* If we don't have a version for this symbol, see if we can find
2457 something. */
2458 if (!hide
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
2461 {
2462 h->verinfo.vertree
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2467 }
2468
2469 return TRUE;
2470 }
2471 \f
2472 /* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2478
2479 Returns FALSE if something goes wrong. */
2480
2481 static bfd_boolean
2482 elf_link_read_relocs_from_section (bfd *abfd,
2483 asection *sec,
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
2487 {
2488 const struct elf_backend_data *bed;
2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
2493 Elf_Internal_Shdr *symtab_hdr;
2494 size_t nsyms;
2495
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498 return FALSE;
2499
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502 return FALSE;
2503
2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2506
2507 bed = get_elf_backend_data (abfd);
2508
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2514 else
2515 {
2516 bfd_set_error (bfd_error_wrong_format);
2517 return FALSE;
2518 }
2519
2520 erela = (const bfd_byte *) external_relocs;
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2524 irela = internal_relocs;
2525 while (erela <= erelaend)
2526 {
2527 bfd_vma r_symndx;
2528
2529 (*swap_in) (abfd, erela, irela);
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2532 r_symndx >>= 24;
2533 if (nsyms > 0)
2534 {
2535 if ((size_t) r_symndx >= nsyms)
2536 {
2537 _bfd_error_handler
2538 /* xgettext:c-format */
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 }
2547 else if (r_symndx != STN_UNDEF)
2548 {
2549 _bfd_error_handler
2550 /* xgettext:c-format */
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
2553 " when the object file has no symbol table"),
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
2556 bfd_set_error (bfd_error_bad_value);
2557 return FALSE;
2558 }
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2561 }
2562
2563 return TRUE;
2564 }
2565
2566 /* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
2574 RELA_HDR relocations. */
2575
2576 Elf_Internal_Rela *
2577 _bfd_elf_link_read_relocs (bfd *abfd,
2578 asection *o,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
2582 {
2583 void *alloc1 = NULL;
2584 Elf_Internal_Rela *alloc2 = NULL;
2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
2588
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
2591
2592 if (o->reloc_count == 0)
2593 return NULL;
2594
2595 if (internal_relocs == NULL)
2596 {
2597 bfd_size_type size;
2598
2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2600 if (keep_memory)
2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2602 else
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2604 if (internal_relocs == NULL)
2605 goto error_return;
2606 }
2607
2608 if (external_relocs == NULL)
2609 {
2610 bfd_size_type size = 0;
2611
2612 if (esdo->rel.hdr)
2613 size += esdo->rel.hdr->sh_size;
2614 if (esdo->rela.hdr)
2615 size += esdo->rela.hdr->sh_size;
2616
2617 alloc1 = bfd_malloc (size);
2618 if (alloc1 == NULL)
2619 goto error_return;
2620 external_relocs = alloc1;
2621 }
2622
2623 internal_rela_relocs = internal_relocs;
2624 if (esdo->rel.hdr)
2625 {
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 external_relocs,
2628 internal_relocs))
2629 goto error_return;
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2634 }
2635
2636 if (esdo->rela.hdr
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 external_relocs,
2639 internal_rela_relocs)))
2640 goto error_return;
2641
2642 /* Cache the results for next time, if we can. */
2643 if (keep_memory)
2644 esdo->relocs = internal_relocs;
2645
2646 if (alloc1 != NULL)
2647 free (alloc1);
2648
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2651
2652 return internal_relocs;
2653
2654 error_return:
2655 if (alloc1 != NULL)
2656 free (alloc1);
2657 if (alloc2 != NULL)
2658 {
2659 if (keep_memory)
2660 bfd_release (abfd, alloc2);
2661 else
2662 free (alloc2);
2663 }
2664 return NULL;
2665 }
2666
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2669
2670 static bfd_boolean
2671 _bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
2673 {
2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2675
2676 /* That allows us to calculate the size of the section. */
2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2678
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685 return FALSE;
2686
2687 if (reldata->hashes == NULL && reldata->count)
2688 {
2689 struct elf_link_hash_entry **p;
2690
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
2693 if (p == NULL)
2694 return FALSE;
2695
2696 reldata->hashes = p;
2697 }
2698
2699 return TRUE;
2700 }
2701
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2704 OUTPUT_BFD. */
2705
2706 bfd_boolean
2707 _bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2712 ATTRIBUTE_UNUSED)
2713 {
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2716 bfd_byte *erel;
2717 struct bfd_elf_section_reloc_data *output_reldata;
2718 asection *output_section;
2719 const struct elf_backend_data *bed;
2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2721 struct bfd_elf_section_data *esdo;
2722
2723 output_section = input_section->output_section;
2724
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2728 {
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
2731 }
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734 {
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
2737 }
2738 else
2739 {
2740 _bfd_error_handler
2741 /* xgettext:c-format */
2742 (_("%pB: relocation size mismatch in %pB section %pA"),
2743 output_bfd, input_section->owner, input_section);
2744 bfd_set_error (bfd_error_wrong_format);
2745 return FALSE;
2746 }
2747
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2754 {
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2758 }
2759
2760 /* Bump the counter, so that we know where to add the next set of
2761 relocations. */
2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2763
2764 return TRUE;
2765 }
2766 \f
2767 /* Make weak undefined symbols in PIE dynamic. */
2768
2769 bfd_boolean
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2772 {
2773 if (bfd_link_pie (info)
2774 && h->dynindx == -1
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2777
2778 return TRUE;
2779 }
2780
2781 /* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2786
2787 static bfd_boolean
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
2790 {
2791 const struct elf_backend_data *bed;
2792
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
2797 if (h->non_elf)
2798 {
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
2804 {
2805 h->ref_regular = 1;
2806 h->ref_regular_nonweak = 1;
2807 }
2808 else
2809 {
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
2813 {
2814 h->ref_regular = 1;
2815 h->ref_regular_nonweak = 1;
2816 }
2817 else
2818 h->def_regular = 1;
2819 }
2820
2821 if (h->dynindx == -1
2822 && (h->def_dynamic
2823 || h->ref_dynamic))
2824 {
2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832 else
2833 {
2834 /* Unfortunately, NON_ELF is only correct if the symbol
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
2842 && !h->def_regular
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
2847 && !h->def_dynamic)))
2848 h->def_regular = 1;
2849 }
2850
2851 /* Backend specific symbol fixup. */
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855 return FALSE;
2856
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
2860 the symbol in a common section but the DEF_REGULAR
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
2863 && !h->def_regular
2864 && h->ref_regular
2865 && !h->def_dynamic
2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2867 h->def_regular = 1;
2868
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2881 exported. */
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2885 && !h->dynamic
2886 && !h->ref_dynamic
2887 && h->def_regular)
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
2895 will force it local. */
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 && h->def_regular)
2902 {
2903 bfd_boolean force_local;
2904
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908 }
2909
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
2913 if (h->is_weakalias)
2914 {
2915 struct elf_link_hash_entry *def = weakdef (h);
2916
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
2929 {
2930 h = def;
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2933 }
2934 else
2935 {
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
2940 BFD_ASSERT (def->def_dynamic);
2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2942 }
2943 }
2944
2945 return TRUE;
2946 }
2947
2948 /* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2950 recursively. */
2951
2952 static bfd_boolean
2953 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2954 {
2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2956 struct elf_link_hash_table *htab;
2957 const struct elf_backend_data *bed;
2958
2959 if (! is_elf_hash_table (eif->info->hash))
2960 return FALSE;
2961
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2964 return TRUE;
2965
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 return FALSE;
2969
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2972
2973 if (h->root.type == bfd_link_hash_undefweak)
2974 {
2975 if (eif->info->dynamic_undefined_weak == 0)
2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2977 else if (eif->info->dynamic_undefined_weak > 0
2978 && h->ref_regular
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2982 {
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2984 {
2985 eif->failed = TRUE;
2986 return FALSE;
2987 }
2988 }
2989 }
2990
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
2998 if (!h->needs_plt
2999 && h->type != STT_GNU_IFUNC
3000 && (h->def_regular
3001 || !h->def_dynamic
3002 || (!h->ref_regular
3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3004 {
3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3006 return TRUE;
3007 }
3008
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
3011 if (h->dynamic_adjusted)
3012 return TRUE;
3013
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
3018 h->dynamic_adjusted = 1;
3019
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3024
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3032 library model.
3033
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3038 int _timezone = 5;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3047
3048 if (h->is_weakalias)
3049 {
3050 struct elf_link_hash_entry *def = weakdef (h);
3051
3052 /* If we get to this point, there is an implicit reference to
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
3055
3056 /* Ensure that the backend adjust_dynamic_symbol function sees
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3059 return FALSE;
3060 }
3061
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3067 if (h->size == 0
3068 && h->type == STT_NOTYPE
3069 && !h->needs_plt)
3070 _bfd_error_handler
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3073
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3075 {
3076 eif->failed = TRUE;
3077 return FALSE;
3078 }
3079
3080 return TRUE;
3081 }
3082
3083 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3084 DYNBSS. */
3085
3086 bfd_boolean
3087 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
3089 asection *dynbss)
3090 {
3091 unsigned int power_of_two;
3092 bfd_vma mask;
3093 asection *sec = h->root.u.def.section;
3094
3095 /* The section alignment of the definition is the maximum alignment
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
3100 power_of_two = bfd_section_alignment (sec);
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3103 {
3104 mask >>= 1;
3105 --power_of_two;
3106 }
3107
3108 if (power_of_two > bfd_section_alignment (dynbss))
3109 {
3110 /* Adjust the section alignment if needed. */
3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
3112 return FALSE;
3113 }
3114
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3130 info->callbacks->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h->root.root.string);
3133
3134 return TRUE;
3135 }
3136
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
3140 static bfd_boolean
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3142 {
3143 asection *sec;
3144
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3149 {
3150 bfd *output_bfd = (bfd *) data;
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
3156 h->root.u.def.value);
3157 }
3158
3159 return TRUE;
3160 }
3161
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166 bfd_boolean
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
3169 bfd_boolean not_local_protected)
3170 {
3171 bfd_boolean binding_stays_local_p;
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
3185 if (h->forced_local)
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected || !bed->is_function_type (h->type))
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
3214 break;
3215 }
3216
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3219 return TRUE;
3220
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224 }
3225
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3238
3239 bfd_boolean
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
3243 {
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
3267 return FALSE;
3268
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
3296 return TRUE;
3297
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected;
3304 }
3305
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309 struct bfd_section *
3310 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311 {
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
3326 /* Ensure the alignment of the first section (usually .tdata) is the largest
3327 alignment, so that the tls segment starts aligned. */
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332 }
3333
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335 static bfd_boolean
3336 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338 {
3339 const struct elf_backend_data *bed;
3340
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
3346 bed = get_elf_backend_data (abfd);
3347 /* Function symbols do not count. */
3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed->common_definition (sym))
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373 }
3374
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378 static bfd_boolean
3379 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380 {
3381 Elf_Internal_Shdr * hdr;
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404 #endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462 }
3463 \f
3464 /* Add an entry to the .dynamic table. */
3465
3466 bfd_boolean
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470 {
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
3485 bed = get_elf_backend_data (hash_table->dynobj);
3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3487 BFD_ASSERT (s != NULL);
3488
3489 newsize = s->size + bed->s->sizeof_dyn;
3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3497
3498 s->size = newsize;
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502 }
3503
3504 /* Strip zero-sized dynamic sections. */
3505
3506 bfd_boolean
3507 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3508 {
3509 struct elf_link_hash_table *hash_table;
3510 const struct elf_backend_data *bed;
3511 asection *s, *sdynamic, **pp;
3512 asection *rela_dyn, *rel_dyn;
3513 Elf_Internal_Dyn dyn;
3514 bfd_byte *extdyn, *next;
3515 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3516 bfd_boolean strip_zero_sized;
3517 bfd_boolean strip_zero_sized_plt;
3518
3519 if (bfd_link_relocatable (info))
3520 return TRUE;
3521
3522 hash_table = elf_hash_table (info);
3523 if (!is_elf_hash_table (hash_table))
3524 return FALSE;
3525
3526 if (!hash_table->dynobj)
3527 return TRUE;
3528
3529 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3530 if (!sdynamic)
3531 return TRUE;
3532
3533 bed = get_elf_backend_data (hash_table->dynobj);
3534 swap_dyn_in = bed->s->swap_dyn_in;
3535
3536 strip_zero_sized = FALSE;
3537 strip_zero_sized_plt = FALSE;
3538
3539 /* Strip zero-sized dynamic sections. */
3540 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3541 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3542 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3543 if (s->size == 0
3544 && (s == rela_dyn
3545 || s == rel_dyn
3546 || s == hash_table->srelplt->output_section
3547 || s == hash_table->splt->output_section))
3548 {
3549 *pp = s->next;
3550 info->output_bfd->section_count--;
3551 strip_zero_sized = TRUE;
3552 if (s == rela_dyn)
3553 s = rela_dyn;
3554 if (s == rel_dyn)
3555 s = rel_dyn;
3556 else if (s == hash_table->splt->output_section)
3557 {
3558 s = hash_table->splt;
3559 strip_zero_sized_plt = TRUE;
3560 }
3561 else
3562 s = hash_table->srelplt;
3563 s->flags |= SEC_EXCLUDE;
3564 s->output_section = bfd_abs_section_ptr;
3565 }
3566 else
3567 pp = &s->next;
3568
3569 if (strip_zero_sized_plt)
3570 for (extdyn = sdynamic->contents;
3571 extdyn < sdynamic->contents + sdynamic->size;
3572 extdyn = next)
3573 {
3574 next = extdyn + bed->s->sizeof_dyn;
3575 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3576 switch (dyn.d_tag)
3577 {
3578 default:
3579 break;
3580 case DT_JMPREL:
3581 case DT_PLTRELSZ:
3582 case DT_PLTREL:
3583 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3584 the procedure linkage table (the .plt section) has been
3585 removed. */
3586 memmove (extdyn, next,
3587 sdynamic->size - (next - sdynamic->contents));
3588 next = extdyn;
3589 }
3590 }
3591
3592 if (strip_zero_sized)
3593 {
3594 /* Regenerate program headers. */
3595 elf_seg_map (info->output_bfd) = NULL;
3596 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3597 }
3598
3599 return TRUE;
3600 }
3601
3602 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3603 1 if a DT_NEEDED tag already exists, and 0 on success. */
3604
3605 int
3606 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3607 {
3608 struct elf_link_hash_table *hash_table;
3609 size_t strindex;
3610 const char *soname;
3611
3612 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3613 return -1;
3614
3615 hash_table = elf_hash_table (info);
3616 soname = elf_dt_name (abfd);
3617 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3618 if (strindex == (size_t) -1)
3619 return -1;
3620
3621 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3622 {
3623 asection *sdyn;
3624 const struct elf_backend_data *bed;
3625 bfd_byte *extdyn;
3626
3627 bed = get_elf_backend_data (hash_table->dynobj);
3628 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3629 if (sdyn != NULL)
3630 for (extdyn = sdyn->contents;
3631 extdyn < sdyn->contents + sdyn->size;
3632 extdyn += bed->s->sizeof_dyn)
3633 {
3634 Elf_Internal_Dyn dyn;
3635
3636 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3637 if (dyn.d_tag == DT_NEEDED
3638 && dyn.d_un.d_val == strindex)
3639 {
3640 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3641 return 1;
3642 }
3643 }
3644 }
3645
3646 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3647 return -1;
3648
3649 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3650 return -1;
3651
3652 return 0;
3653 }
3654
3655 /* Return true if SONAME is on the needed list between NEEDED and STOP
3656 (or the end of list if STOP is NULL), and needed by a library that
3657 will be loaded. */
3658
3659 static bfd_boolean
3660 on_needed_list (const char *soname,
3661 struct bfd_link_needed_list *needed,
3662 struct bfd_link_needed_list *stop)
3663 {
3664 struct bfd_link_needed_list *look;
3665 for (look = needed; look != stop; look = look->next)
3666 if (strcmp (soname, look->name) == 0
3667 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3668 /* If needed by a library that itself is not directly
3669 needed, recursively check whether that library is
3670 indirectly needed. Since we add DT_NEEDED entries to
3671 the end of the list, library dependencies appear after
3672 the library. Therefore search prior to the current
3673 LOOK, preventing possible infinite recursion. */
3674 || on_needed_list (elf_dt_name (look->by), needed, look)))
3675 return TRUE;
3676
3677 return FALSE;
3678 }
3679
3680 /* Sort symbol by value, section, size, and type. */
3681 static int
3682 elf_sort_symbol (const void *arg1, const void *arg2)
3683 {
3684 const struct elf_link_hash_entry *h1;
3685 const struct elf_link_hash_entry *h2;
3686 bfd_signed_vma vdiff;
3687 int sdiff;
3688 const char *n1;
3689 const char *n2;
3690
3691 h1 = *(const struct elf_link_hash_entry **) arg1;
3692 h2 = *(const struct elf_link_hash_entry **) arg2;
3693 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3694 if (vdiff != 0)
3695 return vdiff > 0 ? 1 : -1;
3696
3697 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3698 if (sdiff != 0)
3699 return sdiff;
3700
3701 /* Sort so that sized symbols are selected over zero size symbols. */
3702 vdiff = h1->size - h2->size;
3703 if (vdiff != 0)
3704 return vdiff > 0 ? 1 : -1;
3705
3706 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3707 if (h1->type != h2->type)
3708 return h1->type - h2->type;
3709
3710 /* If symbols are properly sized and typed, and multiple strong
3711 aliases are not defined in a shared library by the user we
3712 shouldn't get here. Unfortunately linker script symbols like
3713 __bss_start sometimes match a user symbol defined at the start of
3714 .bss without proper size and type. We'd like to preference the
3715 user symbol over reserved system symbols. Sort on leading
3716 underscores. */
3717 n1 = h1->root.root.string;
3718 n2 = h2->root.root.string;
3719 while (*n1 == *n2)
3720 {
3721 if (*n1 == 0)
3722 break;
3723 ++n1;
3724 ++n2;
3725 }
3726 if (*n1 == '_')
3727 return -1;
3728 if (*n2 == '_')
3729 return 1;
3730
3731 /* Final sort on name selects user symbols like '_u' over reserved
3732 system symbols like '_Z' and also will avoid qsort instability. */
3733 return *n1 - *n2;
3734 }
3735
3736 /* This function is used to adjust offsets into .dynstr for
3737 dynamic symbols. This is called via elf_link_hash_traverse. */
3738
3739 static bfd_boolean
3740 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3741 {
3742 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3743
3744 if (h->dynindx != -1)
3745 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3746 return TRUE;
3747 }
3748
3749 /* Assign string offsets in .dynstr, update all structures referencing
3750 them. */
3751
3752 static bfd_boolean
3753 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3754 {
3755 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3756 struct elf_link_local_dynamic_entry *entry;
3757 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3758 bfd *dynobj = hash_table->dynobj;
3759 asection *sdyn;
3760 bfd_size_type size;
3761 const struct elf_backend_data *bed;
3762 bfd_byte *extdyn;
3763
3764 _bfd_elf_strtab_finalize (dynstr);
3765 size = _bfd_elf_strtab_size (dynstr);
3766
3767 bed = get_elf_backend_data (dynobj);
3768 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3769 BFD_ASSERT (sdyn != NULL);
3770
3771 /* Update all .dynamic entries referencing .dynstr strings. */
3772 for (extdyn = sdyn->contents;
3773 extdyn < sdyn->contents + sdyn->size;
3774 extdyn += bed->s->sizeof_dyn)
3775 {
3776 Elf_Internal_Dyn dyn;
3777
3778 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3779 switch (dyn.d_tag)
3780 {
3781 case DT_STRSZ:
3782 dyn.d_un.d_val = size;
3783 break;
3784 case DT_NEEDED:
3785 case DT_SONAME:
3786 case DT_RPATH:
3787 case DT_RUNPATH:
3788 case DT_FILTER:
3789 case DT_AUXILIARY:
3790 case DT_AUDIT:
3791 case DT_DEPAUDIT:
3792 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3793 break;
3794 default:
3795 continue;
3796 }
3797 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3798 }
3799
3800 /* Now update local dynamic symbols. */
3801 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3802 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3803 entry->isym.st_name);
3804
3805 /* And the rest of dynamic symbols. */
3806 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3807
3808 /* Adjust version definitions. */
3809 if (elf_tdata (output_bfd)->cverdefs)
3810 {
3811 asection *s;
3812 bfd_byte *p;
3813 size_t i;
3814 Elf_Internal_Verdef def;
3815 Elf_Internal_Verdaux defaux;
3816
3817 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3818 p = s->contents;
3819 do
3820 {
3821 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3822 &def);
3823 p += sizeof (Elf_External_Verdef);
3824 if (def.vd_aux != sizeof (Elf_External_Verdef))
3825 continue;
3826 for (i = 0; i < def.vd_cnt; ++i)
3827 {
3828 _bfd_elf_swap_verdaux_in (output_bfd,
3829 (Elf_External_Verdaux *) p, &defaux);
3830 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3831 defaux.vda_name);
3832 _bfd_elf_swap_verdaux_out (output_bfd,
3833 &defaux, (Elf_External_Verdaux *) p);
3834 p += sizeof (Elf_External_Verdaux);
3835 }
3836 }
3837 while (def.vd_next);
3838 }
3839
3840 /* Adjust version references. */
3841 if (elf_tdata (output_bfd)->verref)
3842 {
3843 asection *s;
3844 bfd_byte *p;
3845 size_t i;
3846 Elf_Internal_Verneed need;
3847 Elf_Internal_Vernaux needaux;
3848
3849 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3850 p = s->contents;
3851 do
3852 {
3853 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3854 &need);
3855 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3856 _bfd_elf_swap_verneed_out (output_bfd, &need,
3857 (Elf_External_Verneed *) p);
3858 p += sizeof (Elf_External_Verneed);
3859 for (i = 0; i < need.vn_cnt; ++i)
3860 {
3861 _bfd_elf_swap_vernaux_in (output_bfd,
3862 (Elf_External_Vernaux *) p, &needaux);
3863 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3864 needaux.vna_name);
3865 _bfd_elf_swap_vernaux_out (output_bfd,
3866 &needaux,
3867 (Elf_External_Vernaux *) p);
3868 p += sizeof (Elf_External_Vernaux);
3869 }
3870 }
3871 while (need.vn_next);
3872 }
3873
3874 return TRUE;
3875 }
3876 \f
3877 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3878 The default is to only match when the INPUT and OUTPUT are exactly
3879 the same target. */
3880
3881 bfd_boolean
3882 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3883 const bfd_target *output)
3884 {
3885 return input == output;
3886 }
3887
3888 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3889 This version is used when different targets for the same architecture
3890 are virtually identical. */
3891
3892 bfd_boolean
3893 _bfd_elf_relocs_compatible (const bfd_target *input,
3894 const bfd_target *output)
3895 {
3896 const struct elf_backend_data *obed, *ibed;
3897
3898 if (input == output)
3899 return TRUE;
3900
3901 ibed = xvec_get_elf_backend_data (input);
3902 obed = xvec_get_elf_backend_data (output);
3903
3904 if (ibed->arch != obed->arch)
3905 return FALSE;
3906
3907 /* If both backends are using this function, deem them compatible. */
3908 return ibed->relocs_compatible == obed->relocs_compatible;
3909 }
3910
3911 /* Make a special call to the linker "notice" function to tell it that
3912 we are about to handle an as-needed lib, or have finished
3913 processing the lib. */
3914
3915 bfd_boolean
3916 _bfd_elf_notice_as_needed (bfd *ibfd,
3917 struct bfd_link_info *info,
3918 enum notice_asneeded_action act)
3919 {
3920 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3921 }
3922
3923 /* Check relocations an ELF object file. */
3924
3925 bfd_boolean
3926 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3927 {
3928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3929 struct elf_link_hash_table *htab = elf_hash_table (info);
3930
3931 /* If this object is the same format as the output object, and it is
3932 not a shared library, then let the backend look through the
3933 relocs.
3934
3935 This is required to build global offset table entries and to
3936 arrange for dynamic relocs. It is not required for the
3937 particular common case of linking non PIC code, even when linking
3938 against shared libraries, but unfortunately there is no way of
3939 knowing whether an object file has been compiled PIC or not.
3940 Looking through the relocs is not particularly time consuming.
3941 The problem is that we must either (1) keep the relocs in memory,
3942 which causes the linker to require additional runtime memory or
3943 (2) read the relocs twice from the input file, which wastes time.
3944 This would be a good case for using mmap.
3945
3946 I have no idea how to handle linking PIC code into a file of a
3947 different format. It probably can't be done. */
3948 if ((abfd->flags & DYNAMIC) == 0
3949 && is_elf_hash_table (htab)
3950 && bed->check_relocs != NULL
3951 && elf_object_id (abfd) == elf_hash_table_id (htab)
3952 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3953 {
3954 asection *o;
3955
3956 for (o = abfd->sections; o != NULL; o = o->next)
3957 {
3958 Elf_Internal_Rela *internal_relocs;
3959 bfd_boolean ok;
3960
3961 /* Don't check relocations in excluded sections. */
3962 if ((o->flags & SEC_RELOC) == 0
3963 || (o->flags & SEC_EXCLUDE) != 0
3964 || o->reloc_count == 0
3965 || ((info->strip == strip_all || info->strip == strip_debugger)
3966 && (o->flags & SEC_DEBUGGING) != 0)
3967 || bfd_is_abs_section (o->output_section))
3968 continue;
3969
3970 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3971 info->keep_memory);
3972 if (internal_relocs == NULL)
3973 return FALSE;
3974
3975 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3976
3977 if (elf_section_data (o)->relocs != internal_relocs)
3978 free (internal_relocs);
3979
3980 if (! ok)
3981 return FALSE;
3982 }
3983 }
3984
3985 return TRUE;
3986 }
3987
3988 /* Add symbols from an ELF object file to the linker hash table. */
3989
3990 static bfd_boolean
3991 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3992 {
3993 Elf_Internal_Ehdr *ehdr;
3994 Elf_Internal_Shdr *hdr;
3995 size_t symcount;
3996 size_t extsymcount;
3997 size_t extsymoff;
3998 struct elf_link_hash_entry **sym_hash;
3999 bfd_boolean dynamic;
4000 Elf_External_Versym *extversym = NULL;
4001 Elf_External_Versym *extversym_end = NULL;
4002 Elf_External_Versym *ever;
4003 struct elf_link_hash_entry *weaks;
4004 struct elf_link_hash_entry **nondeflt_vers = NULL;
4005 size_t nondeflt_vers_cnt = 0;
4006 Elf_Internal_Sym *isymbuf = NULL;
4007 Elf_Internal_Sym *isym;
4008 Elf_Internal_Sym *isymend;
4009 const struct elf_backend_data *bed;
4010 bfd_boolean add_needed;
4011 struct elf_link_hash_table *htab;
4012 void *alloc_mark = NULL;
4013 struct bfd_hash_entry **old_table = NULL;
4014 unsigned int old_size = 0;
4015 unsigned int old_count = 0;
4016 void *old_tab = NULL;
4017 void *old_ent;
4018 struct bfd_link_hash_entry *old_undefs = NULL;
4019 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4020 void *old_strtab = NULL;
4021 size_t tabsize = 0;
4022 asection *s;
4023 bfd_boolean just_syms;
4024
4025 htab = elf_hash_table (info);
4026 bed = get_elf_backend_data (abfd);
4027
4028 if ((abfd->flags & DYNAMIC) == 0)
4029 dynamic = FALSE;
4030 else
4031 {
4032 dynamic = TRUE;
4033
4034 /* You can't use -r against a dynamic object. Also, there's no
4035 hope of using a dynamic object which does not exactly match
4036 the format of the output file. */
4037 if (bfd_link_relocatable (info)
4038 || !is_elf_hash_table (htab)
4039 || info->output_bfd->xvec != abfd->xvec)
4040 {
4041 if (bfd_link_relocatable (info))
4042 bfd_set_error (bfd_error_invalid_operation);
4043 else
4044 bfd_set_error (bfd_error_wrong_format);
4045 goto error_return;
4046 }
4047 }
4048
4049 ehdr = elf_elfheader (abfd);
4050 if (info->warn_alternate_em
4051 && bed->elf_machine_code != ehdr->e_machine
4052 && ((bed->elf_machine_alt1 != 0
4053 && ehdr->e_machine == bed->elf_machine_alt1)
4054 || (bed->elf_machine_alt2 != 0
4055 && ehdr->e_machine == bed->elf_machine_alt2)))
4056 _bfd_error_handler
4057 /* xgettext:c-format */
4058 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4059 ehdr->e_machine, abfd, bed->elf_machine_code);
4060
4061 /* As a GNU extension, any input sections which are named
4062 .gnu.warning.SYMBOL are treated as warning symbols for the given
4063 symbol. This differs from .gnu.warning sections, which generate
4064 warnings when they are included in an output file. */
4065 /* PR 12761: Also generate this warning when building shared libraries. */
4066 for (s = abfd->sections; s != NULL; s = s->next)
4067 {
4068 const char *name;
4069
4070 name = bfd_section_name (s);
4071 if (CONST_STRNEQ (name, ".gnu.warning."))
4072 {
4073 char *msg;
4074 bfd_size_type sz;
4075
4076 name += sizeof ".gnu.warning." - 1;
4077
4078 /* If this is a shared object, then look up the symbol
4079 in the hash table. If it is there, and it is already
4080 been defined, then we will not be using the entry
4081 from this shared object, so we don't need to warn.
4082 FIXME: If we see the definition in a regular object
4083 later on, we will warn, but we shouldn't. The only
4084 fix is to keep track of what warnings we are supposed
4085 to emit, and then handle them all at the end of the
4086 link. */
4087 if (dynamic)
4088 {
4089 struct elf_link_hash_entry *h;
4090
4091 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4092
4093 /* FIXME: What about bfd_link_hash_common? */
4094 if (h != NULL
4095 && (h->root.type == bfd_link_hash_defined
4096 || h->root.type == bfd_link_hash_defweak))
4097 continue;
4098 }
4099
4100 sz = s->size;
4101 msg = (char *) bfd_alloc (abfd, sz + 1);
4102 if (msg == NULL)
4103 goto error_return;
4104
4105 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4106 goto error_return;
4107
4108 msg[sz] = '\0';
4109
4110 if (! (_bfd_generic_link_add_one_symbol
4111 (info, abfd, name, BSF_WARNING, s, 0, msg,
4112 FALSE, bed->collect, NULL)))
4113 goto error_return;
4114
4115 if (bfd_link_executable (info))
4116 {
4117 /* Clobber the section size so that the warning does
4118 not get copied into the output file. */
4119 s->size = 0;
4120
4121 /* Also set SEC_EXCLUDE, so that symbols defined in
4122 the warning section don't get copied to the output. */
4123 s->flags |= SEC_EXCLUDE;
4124 }
4125 }
4126 }
4127
4128 just_syms = ((s = abfd->sections) != NULL
4129 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4130
4131 add_needed = TRUE;
4132 if (! dynamic)
4133 {
4134 /* If we are creating a shared library, create all the dynamic
4135 sections immediately. We need to attach them to something,
4136 so we attach them to this BFD, provided it is the right
4137 format and is not from ld --just-symbols. Always create the
4138 dynamic sections for -E/--dynamic-list. FIXME: If there
4139 are no input BFD's of the same format as the output, we can't
4140 make a shared library. */
4141 if (!just_syms
4142 && (bfd_link_pic (info)
4143 || (!bfd_link_relocatable (info)
4144 && info->nointerp
4145 && (info->export_dynamic || info->dynamic)))
4146 && is_elf_hash_table (htab)
4147 && info->output_bfd->xvec == abfd->xvec
4148 && !htab->dynamic_sections_created)
4149 {
4150 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4151 goto error_return;
4152 }
4153 }
4154 else if (!is_elf_hash_table (htab))
4155 goto error_return;
4156 else
4157 {
4158 const char *soname = NULL;
4159 char *audit = NULL;
4160 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4161 const Elf_Internal_Phdr *phdr;
4162 struct elf_link_loaded_list *loaded_lib;
4163
4164 /* ld --just-symbols and dynamic objects don't mix very well.
4165 ld shouldn't allow it. */
4166 if (just_syms)
4167 abort ();
4168
4169 /* If this dynamic lib was specified on the command line with
4170 --as-needed in effect, then we don't want to add a DT_NEEDED
4171 tag unless the lib is actually used. Similary for libs brought
4172 in by another lib's DT_NEEDED. When --no-add-needed is used
4173 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4174 any dynamic library in DT_NEEDED tags in the dynamic lib at
4175 all. */
4176 add_needed = (elf_dyn_lib_class (abfd)
4177 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4178 | DYN_NO_NEEDED)) == 0;
4179
4180 s = bfd_get_section_by_name (abfd, ".dynamic");
4181 if (s != NULL)
4182 {
4183 bfd_byte *dynbuf;
4184 bfd_byte *extdyn;
4185 unsigned int elfsec;
4186 unsigned long shlink;
4187
4188 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4189 {
4190 error_free_dyn:
4191 free (dynbuf);
4192 goto error_return;
4193 }
4194
4195 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4196 if (elfsec == SHN_BAD)
4197 goto error_free_dyn;
4198 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4199
4200 for (extdyn = dynbuf;
4201 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4202 extdyn += bed->s->sizeof_dyn)
4203 {
4204 Elf_Internal_Dyn dyn;
4205
4206 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4207 if (dyn.d_tag == DT_SONAME)
4208 {
4209 unsigned int tagv = dyn.d_un.d_val;
4210 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4211 if (soname == NULL)
4212 goto error_free_dyn;
4213 }
4214 if (dyn.d_tag == DT_NEEDED)
4215 {
4216 struct bfd_link_needed_list *n, **pn;
4217 char *fnm, *anm;
4218 unsigned int tagv = dyn.d_un.d_val;
4219 size_t amt = sizeof (struct bfd_link_needed_list);
4220
4221 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4222 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4223 if (n == NULL || fnm == NULL)
4224 goto error_free_dyn;
4225 amt = strlen (fnm) + 1;
4226 anm = (char *) bfd_alloc (abfd, amt);
4227 if (anm == NULL)
4228 goto error_free_dyn;
4229 memcpy (anm, fnm, amt);
4230 n->name = anm;
4231 n->by = abfd;
4232 n->next = NULL;
4233 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4234 ;
4235 *pn = n;
4236 }
4237 if (dyn.d_tag == DT_RUNPATH)
4238 {
4239 struct bfd_link_needed_list *n, **pn;
4240 char *fnm, *anm;
4241 unsigned int tagv = dyn.d_un.d_val;
4242 size_t amt = sizeof (struct bfd_link_needed_list);
4243
4244 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4245 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4246 if (n == NULL || fnm == NULL)
4247 goto error_free_dyn;
4248 amt = strlen (fnm) + 1;
4249 anm = (char *) bfd_alloc (abfd, amt);
4250 if (anm == NULL)
4251 goto error_free_dyn;
4252 memcpy (anm, fnm, amt);
4253 n->name = anm;
4254 n->by = abfd;
4255 n->next = NULL;
4256 for (pn = & runpath;
4257 *pn != NULL;
4258 pn = &(*pn)->next)
4259 ;
4260 *pn = n;
4261 }
4262 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4263 if (!runpath && dyn.d_tag == DT_RPATH)
4264 {
4265 struct bfd_link_needed_list *n, **pn;
4266 char *fnm, *anm;
4267 unsigned int tagv = dyn.d_un.d_val;
4268 size_t amt = sizeof (struct bfd_link_needed_list);
4269
4270 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4271 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4272 if (n == NULL || fnm == NULL)
4273 goto error_free_dyn;
4274 amt = strlen (fnm) + 1;
4275 anm = (char *) bfd_alloc (abfd, amt);
4276 if (anm == NULL)
4277 goto error_free_dyn;
4278 memcpy (anm, fnm, amt);
4279 n->name = anm;
4280 n->by = abfd;
4281 n->next = NULL;
4282 for (pn = & rpath;
4283 *pn != NULL;
4284 pn = &(*pn)->next)
4285 ;
4286 *pn = n;
4287 }
4288 if (dyn.d_tag == DT_AUDIT)
4289 {
4290 unsigned int tagv = dyn.d_un.d_val;
4291 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4292 }
4293 }
4294
4295 free (dynbuf);
4296 }
4297
4298 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4299 frees all more recently bfd_alloc'd blocks as well. */
4300 if (runpath)
4301 rpath = runpath;
4302
4303 if (rpath)
4304 {
4305 struct bfd_link_needed_list **pn;
4306 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4307 ;
4308 *pn = rpath;
4309 }
4310
4311 /* If we have a PT_GNU_RELRO program header, mark as read-only
4312 all sections contained fully therein. This makes relro
4313 shared library sections appear as they will at run-time. */
4314 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4315 while (phdr-- > elf_tdata (abfd)->phdr)
4316 if (phdr->p_type == PT_GNU_RELRO)
4317 {
4318 for (s = abfd->sections; s != NULL; s = s->next)
4319 {
4320 unsigned int opb = bfd_octets_per_byte (abfd, s);
4321
4322 if ((s->flags & SEC_ALLOC) != 0
4323 && s->vma * opb >= phdr->p_vaddr
4324 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4325 s->flags |= SEC_READONLY;
4326 }
4327 break;
4328 }
4329
4330 /* We do not want to include any of the sections in a dynamic
4331 object in the output file. We hack by simply clobbering the
4332 list of sections in the BFD. This could be handled more
4333 cleanly by, say, a new section flag; the existing
4334 SEC_NEVER_LOAD flag is not the one we want, because that one
4335 still implies that the section takes up space in the output
4336 file. */
4337 bfd_section_list_clear (abfd);
4338
4339 /* Find the name to use in a DT_NEEDED entry that refers to this
4340 object. If the object has a DT_SONAME entry, we use it.
4341 Otherwise, if the generic linker stuck something in
4342 elf_dt_name, we use that. Otherwise, we just use the file
4343 name. */
4344 if (soname == NULL || *soname == '\0')
4345 {
4346 soname = elf_dt_name (abfd);
4347 if (soname == NULL || *soname == '\0')
4348 soname = bfd_get_filename (abfd);
4349 }
4350
4351 /* Save the SONAME because sometimes the linker emulation code
4352 will need to know it. */
4353 elf_dt_name (abfd) = soname;
4354
4355 /* If we have already included this dynamic object in the
4356 link, just ignore it. There is no reason to include a
4357 particular dynamic object more than once. */
4358 for (loaded_lib = htab->dyn_loaded;
4359 loaded_lib != NULL;
4360 loaded_lib = loaded_lib->next)
4361 {
4362 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4363 return TRUE;
4364 }
4365
4366 /* Create dynamic sections for backends that require that be done
4367 before setup_gnu_properties. */
4368 if (add_needed
4369 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4370 return FALSE;
4371
4372 /* Save the DT_AUDIT entry for the linker emulation code. */
4373 elf_dt_audit (abfd) = audit;
4374 }
4375
4376 /* If this is a dynamic object, we always link against the .dynsym
4377 symbol table, not the .symtab symbol table. The dynamic linker
4378 will only see the .dynsym symbol table, so there is no reason to
4379 look at .symtab for a dynamic object. */
4380
4381 if (! dynamic || elf_dynsymtab (abfd) == 0)
4382 hdr = &elf_tdata (abfd)->symtab_hdr;
4383 else
4384 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4385
4386 symcount = hdr->sh_size / bed->s->sizeof_sym;
4387
4388 /* The sh_info field of the symtab header tells us where the
4389 external symbols start. We don't care about the local symbols at
4390 this point. */
4391 if (elf_bad_symtab (abfd))
4392 {
4393 extsymcount = symcount;
4394 extsymoff = 0;
4395 }
4396 else
4397 {
4398 extsymcount = symcount - hdr->sh_info;
4399 extsymoff = hdr->sh_info;
4400 }
4401
4402 sym_hash = elf_sym_hashes (abfd);
4403 if (extsymcount != 0)
4404 {
4405 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4406 NULL, NULL, NULL);
4407 if (isymbuf == NULL)
4408 goto error_return;
4409
4410 if (sym_hash == NULL)
4411 {
4412 /* We store a pointer to the hash table entry for each
4413 external symbol. */
4414 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4415 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4416 if (sym_hash == NULL)
4417 goto error_free_sym;
4418 elf_sym_hashes (abfd) = sym_hash;
4419 }
4420 }
4421
4422 if (dynamic)
4423 {
4424 /* Read in any version definitions. */
4425 if (!_bfd_elf_slurp_version_tables (abfd,
4426 info->default_imported_symver))
4427 goto error_free_sym;
4428
4429 /* Read in the symbol versions, but don't bother to convert them
4430 to internal format. */
4431 if (elf_dynversym (abfd) != 0)
4432 {
4433 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4434 bfd_size_type amt = versymhdr->sh_size;
4435
4436 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4437 goto error_free_sym;
4438 extversym = (Elf_External_Versym *)
4439 _bfd_malloc_and_read (abfd, amt, amt);
4440 if (extversym == NULL)
4441 goto error_free_sym;
4442 extversym_end = extversym + amt / sizeof (*extversym);
4443 }
4444 }
4445
4446 /* If we are loading an as-needed shared lib, save the symbol table
4447 state before we start adding symbols. If the lib turns out
4448 to be unneeded, restore the state. */
4449 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4450 {
4451 unsigned int i;
4452 size_t entsize;
4453
4454 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4455 {
4456 struct bfd_hash_entry *p;
4457 struct elf_link_hash_entry *h;
4458
4459 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4460 {
4461 h = (struct elf_link_hash_entry *) p;
4462 entsize += htab->root.table.entsize;
4463 if (h->root.type == bfd_link_hash_warning)
4464 entsize += htab->root.table.entsize;
4465 }
4466 }
4467
4468 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4469 old_tab = bfd_malloc (tabsize + entsize);
4470 if (old_tab == NULL)
4471 goto error_free_vers;
4472
4473 /* Remember the current objalloc pointer, so that all mem for
4474 symbols added can later be reclaimed. */
4475 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4476 if (alloc_mark == NULL)
4477 goto error_free_vers;
4478
4479 /* Make a special call to the linker "notice" function to
4480 tell it that we are about to handle an as-needed lib. */
4481 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4482 goto error_free_vers;
4483
4484 /* Clone the symbol table. Remember some pointers into the
4485 symbol table, and dynamic symbol count. */
4486 old_ent = (char *) old_tab + tabsize;
4487 memcpy (old_tab, htab->root.table.table, tabsize);
4488 old_undefs = htab->root.undefs;
4489 old_undefs_tail = htab->root.undefs_tail;
4490 old_table = htab->root.table.table;
4491 old_size = htab->root.table.size;
4492 old_count = htab->root.table.count;
4493 old_strtab = NULL;
4494 if (htab->dynstr != NULL)
4495 {
4496 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4497 if (old_strtab == NULL)
4498 goto error_free_vers;
4499 }
4500
4501 for (i = 0; i < htab->root.table.size; i++)
4502 {
4503 struct bfd_hash_entry *p;
4504 struct elf_link_hash_entry *h;
4505
4506 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4507 {
4508 memcpy (old_ent, p, htab->root.table.entsize);
4509 old_ent = (char *) old_ent + htab->root.table.entsize;
4510 h = (struct elf_link_hash_entry *) p;
4511 if (h->root.type == bfd_link_hash_warning)
4512 {
4513 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4514 old_ent = (char *) old_ent + htab->root.table.entsize;
4515 }
4516 }
4517 }
4518 }
4519
4520 weaks = NULL;
4521 if (extversym == NULL)
4522 ever = NULL;
4523 else if (extversym + extsymoff < extversym_end)
4524 ever = extversym + extsymoff;
4525 else
4526 {
4527 /* xgettext:c-format */
4528 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4529 abfd, (long) extsymoff,
4530 (long) (extversym_end - extversym) / sizeof (* extversym));
4531 bfd_set_error (bfd_error_bad_value);
4532 goto error_free_vers;
4533 }
4534
4535 if (!bfd_link_relocatable (info)
4536 && abfd->lto_slim_object)
4537 {
4538 _bfd_error_handler
4539 (_("%pB: plugin needed to handle lto object"), abfd);
4540 }
4541
4542 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4543 isym < isymend;
4544 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4545 {
4546 int bind;
4547 bfd_vma value;
4548 asection *sec, *new_sec;
4549 flagword flags;
4550 const char *name;
4551 struct elf_link_hash_entry *h;
4552 struct elf_link_hash_entry *hi;
4553 bfd_boolean definition;
4554 bfd_boolean size_change_ok;
4555 bfd_boolean type_change_ok;
4556 bfd_boolean new_weak;
4557 bfd_boolean old_weak;
4558 bfd_boolean override;
4559 bfd_boolean common;
4560 bfd_boolean discarded;
4561 unsigned int old_alignment;
4562 unsigned int shindex;
4563 bfd *old_bfd;
4564 bfd_boolean matched;
4565
4566 override = FALSE;
4567
4568 flags = BSF_NO_FLAGS;
4569 sec = NULL;
4570 value = isym->st_value;
4571 common = bed->common_definition (isym);
4572 if (common && info->inhibit_common_definition)
4573 {
4574 /* Treat common symbol as undefined for --no-define-common. */
4575 isym->st_shndx = SHN_UNDEF;
4576 common = FALSE;
4577 }
4578 discarded = FALSE;
4579
4580 bind = ELF_ST_BIND (isym->st_info);
4581 switch (bind)
4582 {
4583 case STB_LOCAL:
4584 /* This should be impossible, since ELF requires that all
4585 global symbols follow all local symbols, and that sh_info
4586 point to the first global symbol. Unfortunately, Irix 5
4587 screws this up. */
4588 if (elf_bad_symtab (abfd))
4589 continue;
4590
4591 /* If we aren't prepared to handle locals within the globals
4592 then we'll likely segfault on a NULL symbol hash if the
4593 symbol is ever referenced in relocations. */
4594 shindex = elf_elfheader (abfd)->e_shstrndx;
4595 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4596 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4597 " (>= sh_info of %lu)"),
4598 abfd, name, (long) (isym - isymbuf + extsymoff),
4599 (long) extsymoff);
4600
4601 /* Dynamic object relocations are not processed by ld, so
4602 ld won't run into the problem mentioned above. */
4603 if (dynamic)
4604 continue;
4605 bfd_set_error (bfd_error_bad_value);
4606 goto error_free_vers;
4607
4608 case STB_GLOBAL:
4609 if (isym->st_shndx != SHN_UNDEF && !common)
4610 flags = BSF_GLOBAL;
4611 break;
4612
4613 case STB_WEAK:
4614 flags = BSF_WEAK;
4615 break;
4616
4617 case STB_GNU_UNIQUE:
4618 flags = BSF_GNU_UNIQUE;
4619 break;
4620
4621 default:
4622 /* Leave it up to the processor backend. */
4623 break;
4624 }
4625
4626 if (isym->st_shndx == SHN_UNDEF)
4627 sec = bfd_und_section_ptr;
4628 else if (isym->st_shndx == SHN_ABS)
4629 sec = bfd_abs_section_ptr;
4630 else if (isym->st_shndx == SHN_COMMON)
4631 {
4632 sec = bfd_com_section_ptr;
4633 /* What ELF calls the size we call the value. What ELF
4634 calls the value we call the alignment. */
4635 value = isym->st_size;
4636 }
4637 else
4638 {
4639 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4640 if (sec == NULL)
4641 sec = bfd_abs_section_ptr;
4642 else if (discarded_section (sec))
4643 {
4644 /* Symbols from discarded section are undefined. We keep
4645 its visibility. */
4646 sec = bfd_und_section_ptr;
4647 discarded = TRUE;
4648 isym->st_shndx = SHN_UNDEF;
4649 }
4650 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4651 value -= sec->vma;
4652 }
4653
4654 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4655 isym->st_name);
4656 if (name == NULL)
4657 goto error_free_vers;
4658
4659 if (isym->st_shndx == SHN_COMMON
4660 && (abfd->flags & BFD_PLUGIN) != 0)
4661 {
4662 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4663
4664 if (xc == NULL)
4665 {
4666 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4667 | SEC_EXCLUDE);
4668 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4669 if (xc == NULL)
4670 goto error_free_vers;
4671 }
4672 sec = xc;
4673 }
4674 else if (isym->st_shndx == SHN_COMMON
4675 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4676 && !bfd_link_relocatable (info))
4677 {
4678 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4679
4680 if (tcomm == NULL)
4681 {
4682 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4683 | SEC_LINKER_CREATED);
4684 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4685 if (tcomm == NULL)
4686 goto error_free_vers;
4687 }
4688 sec = tcomm;
4689 }
4690 else if (bed->elf_add_symbol_hook)
4691 {
4692 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4693 &sec, &value))
4694 goto error_free_vers;
4695
4696 /* The hook function sets the name to NULL if this symbol
4697 should be skipped for some reason. */
4698 if (name == NULL)
4699 continue;
4700 }
4701
4702 /* Sanity check that all possibilities were handled. */
4703 if (sec == NULL)
4704 abort ();
4705
4706 /* Silently discard TLS symbols from --just-syms. There's
4707 no way to combine a static TLS block with a new TLS block
4708 for this executable. */
4709 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4710 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4711 continue;
4712
4713 if (bfd_is_und_section (sec)
4714 || bfd_is_com_section (sec))
4715 definition = FALSE;
4716 else
4717 definition = TRUE;
4718
4719 size_change_ok = FALSE;
4720 type_change_ok = bed->type_change_ok;
4721 old_weak = FALSE;
4722 matched = FALSE;
4723 old_alignment = 0;
4724 old_bfd = NULL;
4725 new_sec = sec;
4726
4727 if (is_elf_hash_table (htab))
4728 {
4729 Elf_Internal_Versym iver;
4730 unsigned int vernum = 0;
4731 bfd_boolean skip;
4732
4733 if (ever == NULL)
4734 {
4735 if (info->default_imported_symver)
4736 /* Use the default symbol version created earlier. */
4737 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4738 else
4739 iver.vs_vers = 0;
4740 }
4741 else if (ever >= extversym_end)
4742 {
4743 /* xgettext:c-format */
4744 _bfd_error_handler (_("%pB: not enough version information"),
4745 abfd);
4746 bfd_set_error (bfd_error_bad_value);
4747 goto error_free_vers;
4748 }
4749 else
4750 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4751
4752 vernum = iver.vs_vers & VERSYM_VERSION;
4753
4754 /* If this is a hidden symbol, or if it is not version
4755 1, we append the version name to the symbol name.
4756 However, we do not modify a non-hidden absolute symbol
4757 if it is not a function, because it might be the version
4758 symbol itself. FIXME: What if it isn't? */
4759 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4760 || (vernum > 1
4761 && (!bfd_is_abs_section (sec)
4762 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4763 {
4764 const char *verstr;
4765 size_t namelen, verlen, newlen;
4766 char *newname, *p;
4767
4768 if (isym->st_shndx != SHN_UNDEF)
4769 {
4770 if (vernum > elf_tdata (abfd)->cverdefs)
4771 verstr = NULL;
4772 else if (vernum > 1)
4773 verstr =
4774 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4775 else
4776 verstr = "";
4777
4778 if (verstr == NULL)
4779 {
4780 _bfd_error_handler
4781 /* xgettext:c-format */
4782 (_("%pB: %s: invalid version %u (max %d)"),
4783 abfd, name, vernum,
4784 elf_tdata (abfd)->cverdefs);
4785 bfd_set_error (bfd_error_bad_value);
4786 goto error_free_vers;
4787 }
4788 }
4789 else
4790 {
4791 /* We cannot simply test for the number of
4792 entries in the VERNEED section since the
4793 numbers for the needed versions do not start
4794 at 0. */
4795 Elf_Internal_Verneed *t;
4796
4797 verstr = NULL;
4798 for (t = elf_tdata (abfd)->verref;
4799 t != NULL;
4800 t = t->vn_nextref)
4801 {
4802 Elf_Internal_Vernaux *a;
4803
4804 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4805 {
4806 if (a->vna_other == vernum)
4807 {
4808 verstr = a->vna_nodename;
4809 break;
4810 }
4811 }
4812 if (a != NULL)
4813 break;
4814 }
4815 if (verstr == NULL)
4816 {
4817 _bfd_error_handler
4818 /* xgettext:c-format */
4819 (_("%pB: %s: invalid needed version %d"),
4820 abfd, name, vernum);
4821 bfd_set_error (bfd_error_bad_value);
4822 goto error_free_vers;
4823 }
4824 }
4825
4826 namelen = strlen (name);
4827 verlen = strlen (verstr);
4828 newlen = namelen + verlen + 2;
4829 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4830 && isym->st_shndx != SHN_UNDEF)
4831 ++newlen;
4832
4833 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4834 if (newname == NULL)
4835 goto error_free_vers;
4836 memcpy (newname, name, namelen);
4837 p = newname + namelen;
4838 *p++ = ELF_VER_CHR;
4839 /* If this is a defined non-hidden version symbol,
4840 we add another @ to the name. This indicates the
4841 default version of the symbol. */
4842 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4843 && isym->st_shndx != SHN_UNDEF)
4844 *p++ = ELF_VER_CHR;
4845 memcpy (p, verstr, verlen + 1);
4846
4847 name = newname;
4848 }
4849
4850 /* If this symbol has default visibility and the user has
4851 requested we not re-export it, then mark it as hidden. */
4852 if (!bfd_is_und_section (sec)
4853 && !dynamic
4854 && abfd->no_export
4855 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4856 isym->st_other = (STV_HIDDEN
4857 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4858
4859 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4860 sym_hash, &old_bfd, &old_weak,
4861 &old_alignment, &skip, &override,
4862 &type_change_ok, &size_change_ok,
4863 &matched))
4864 goto error_free_vers;
4865
4866 if (skip)
4867 continue;
4868
4869 /* Override a definition only if the new symbol matches the
4870 existing one. */
4871 if (override && matched)
4872 definition = FALSE;
4873
4874 h = *sym_hash;
4875 while (h->root.type == bfd_link_hash_indirect
4876 || h->root.type == bfd_link_hash_warning)
4877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4878
4879 if (elf_tdata (abfd)->verdef != NULL
4880 && vernum > 1
4881 && definition)
4882 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4883 }
4884
4885 if (! (_bfd_generic_link_add_one_symbol
4886 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4887 (struct bfd_link_hash_entry **) sym_hash)))
4888 goto error_free_vers;
4889
4890 h = *sym_hash;
4891 /* We need to make sure that indirect symbol dynamic flags are
4892 updated. */
4893 hi = h;
4894 while (h->root.type == bfd_link_hash_indirect
4895 || h->root.type == bfd_link_hash_warning)
4896 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4897
4898 /* Setting the index to -3 tells elf_link_output_extsym that
4899 this symbol is defined in a discarded section. */
4900 if (discarded)
4901 h->indx = -3;
4902
4903 *sym_hash = h;
4904
4905 new_weak = (flags & BSF_WEAK) != 0;
4906 if (dynamic
4907 && definition
4908 && new_weak
4909 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4910 && is_elf_hash_table (htab)
4911 && h->u.alias == NULL)
4912 {
4913 /* Keep a list of all weak defined non function symbols from
4914 a dynamic object, using the alias field. Later in this
4915 function we will set the alias field to the correct
4916 value. We only put non-function symbols from dynamic
4917 objects on this list, because that happens to be the only
4918 time we need to know the normal symbol corresponding to a
4919 weak symbol, and the information is time consuming to
4920 figure out. If the alias field is not already NULL,
4921 then this symbol was already defined by some previous
4922 dynamic object, and we will be using that previous
4923 definition anyhow. */
4924
4925 h->u.alias = weaks;
4926 weaks = h;
4927 }
4928
4929 /* Set the alignment of a common symbol. */
4930 if ((common || bfd_is_com_section (sec))
4931 && h->root.type == bfd_link_hash_common)
4932 {
4933 unsigned int align;
4934
4935 if (common)
4936 align = bfd_log2 (isym->st_value);
4937 else
4938 {
4939 /* The new symbol is a common symbol in a shared object.
4940 We need to get the alignment from the section. */
4941 align = new_sec->alignment_power;
4942 }
4943 if (align > old_alignment)
4944 h->root.u.c.p->alignment_power = align;
4945 else
4946 h->root.u.c.p->alignment_power = old_alignment;
4947 }
4948
4949 if (is_elf_hash_table (htab))
4950 {
4951 /* Set a flag in the hash table entry indicating the type of
4952 reference or definition we just found. A dynamic symbol
4953 is one which is referenced or defined by both a regular
4954 object and a shared object. */
4955 bfd_boolean dynsym = FALSE;
4956
4957 /* Plugin symbols aren't normal. Don't set def_regular or
4958 ref_regular for them, or make them dynamic. */
4959 if ((abfd->flags & BFD_PLUGIN) != 0)
4960 ;
4961 else if (! dynamic)
4962 {
4963 if (! definition)
4964 {
4965 h->ref_regular = 1;
4966 if (bind != STB_WEAK)
4967 h->ref_regular_nonweak = 1;
4968 }
4969 else
4970 {
4971 h->def_regular = 1;
4972 if (h->def_dynamic)
4973 {
4974 h->def_dynamic = 0;
4975 h->ref_dynamic = 1;
4976 }
4977 }
4978
4979 /* If the indirect symbol has been forced local, don't
4980 make the real symbol dynamic. */
4981 if ((h == hi || !hi->forced_local)
4982 && (bfd_link_dll (info)
4983 || h->def_dynamic
4984 || h->ref_dynamic))
4985 dynsym = TRUE;
4986 }
4987 else
4988 {
4989 if (! definition)
4990 {
4991 h->ref_dynamic = 1;
4992 hi->ref_dynamic = 1;
4993 }
4994 else
4995 {
4996 h->def_dynamic = 1;
4997 hi->def_dynamic = 1;
4998 }
4999
5000 /* If the indirect symbol has been forced local, don't
5001 make the real symbol dynamic. */
5002 if ((h == hi || !hi->forced_local)
5003 && (h->def_regular
5004 || h->ref_regular
5005 || (h->is_weakalias
5006 && weakdef (h)->dynindx != -1)))
5007 dynsym = TRUE;
5008 }
5009
5010 /* Check to see if we need to add an indirect symbol for
5011 the default name. */
5012 if (definition
5013 || (!override && h->root.type == bfd_link_hash_common))
5014 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5015 sec, value, &old_bfd, &dynsym))
5016 goto error_free_vers;
5017
5018 /* Check the alignment when a common symbol is involved. This
5019 can change when a common symbol is overridden by a normal
5020 definition or a common symbol is ignored due to the old
5021 normal definition. We need to make sure the maximum
5022 alignment is maintained. */
5023 if ((old_alignment || common)
5024 && h->root.type != bfd_link_hash_common)
5025 {
5026 unsigned int common_align;
5027 unsigned int normal_align;
5028 unsigned int symbol_align;
5029 bfd *normal_bfd;
5030 bfd *common_bfd;
5031
5032 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5033 || h->root.type == bfd_link_hash_defweak);
5034
5035 symbol_align = ffs (h->root.u.def.value) - 1;
5036 if (h->root.u.def.section->owner != NULL
5037 && (h->root.u.def.section->owner->flags
5038 & (DYNAMIC | BFD_PLUGIN)) == 0)
5039 {
5040 normal_align = h->root.u.def.section->alignment_power;
5041 if (normal_align > symbol_align)
5042 normal_align = symbol_align;
5043 }
5044 else
5045 normal_align = symbol_align;
5046
5047 if (old_alignment)
5048 {
5049 common_align = old_alignment;
5050 common_bfd = old_bfd;
5051 normal_bfd = abfd;
5052 }
5053 else
5054 {
5055 common_align = bfd_log2 (isym->st_value);
5056 common_bfd = abfd;
5057 normal_bfd = old_bfd;
5058 }
5059
5060 if (normal_align < common_align)
5061 {
5062 /* PR binutils/2735 */
5063 if (normal_bfd == NULL)
5064 _bfd_error_handler
5065 /* xgettext:c-format */
5066 (_("warning: alignment %u of common symbol `%s' in %pB is"
5067 " greater than the alignment (%u) of its section %pA"),
5068 1 << common_align, name, common_bfd,
5069 1 << normal_align, h->root.u.def.section);
5070 else
5071 _bfd_error_handler
5072 /* xgettext:c-format */
5073 (_("warning: alignment %u of symbol `%s' in %pB"
5074 " is smaller than %u in %pB"),
5075 1 << normal_align, name, normal_bfd,
5076 1 << common_align, common_bfd);
5077 }
5078 }
5079
5080 /* Remember the symbol size if it isn't undefined. */
5081 if (isym->st_size != 0
5082 && isym->st_shndx != SHN_UNDEF
5083 && (definition || h->size == 0))
5084 {
5085 if (h->size != 0
5086 && h->size != isym->st_size
5087 && ! size_change_ok)
5088 _bfd_error_handler
5089 /* xgettext:c-format */
5090 (_("warning: size of symbol `%s' changed"
5091 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5092 name, (uint64_t) h->size, old_bfd,
5093 (uint64_t) isym->st_size, abfd);
5094
5095 h->size = isym->st_size;
5096 }
5097
5098 /* If this is a common symbol, then we always want H->SIZE
5099 to be the size of the common symbol. The code just above
5100 won't fix the size if a common symbol becomes larger. We
5101 don't warn about a size change here, because that is
5102 covered by --warn-common. Allow changes between different
5103 function types. */
5104 if (h->root.type == bfd_link_hash_common)
5105 h->size = h->root.u.c.size;
5106
5107 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5108 && ((definition && !new_weak)
5109 || (old_weak && h->root.type == bfd_link_hash_common)
5110 || h->type == STT_NOTYPE))
5111 {
5112 unsigned int type = ELF_ST_TYPE (isym->st_info);
5113
5114 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5115 symbol. */
5116 if (type == STT_GNU_IFUNC
5117 && (abfd->flags & DYNAMIC) != 0)
5118 type = STT_FUNC;
5119
5120 if (h->type != type)
5121 {
5122 if (h->type != STT_NOTYPE && ! type_change_ok)
5123 /* xgettext:c-format */
5124 _bfd_error_handler
5125 (_("warning: type of symbol `%s' changed"
5126 " from %d to %d in %pB"),
5127 name, h->type, type, abfd);
5128
5129 h->type = type;
5130 }
5131 }
5132
5133 /* Merge st_other field. */
5134 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5135
5136 /* We don't want to make debug symbol dynamic. */
5137 if (definition
5138 && (sec->flags & SEC_DEBUGGING)
5139 && !bfd_link_relocatable (info))
5140 dynsym = FALSE;
5141
5142 /* Nor should we make plugin symbols dynamic. */
5143 if ((abfd->flags & BFD_PLUGIN) != 0)
5144 dynsym = FALSE;
5145
5146 if (definition)
5147 {
5148 h->target_internal = isym->st_target_internal;
5149 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5150 }
5151
5152 if (definition && !dynamic)
5153 {
5154 char *p = strchr (name, ELF_VER_CHR);
5155 if (p != NULL && p[1] != ELF_VER_CHR)
5156 {
5157 /* Queue non-default versions so that .symver x, x@FOO
5158 aliases can be checked. */
5159 if (!nondeflt_vers)
5160 {
5161 size_t amt = ((isymend - isym + 1)
5162 * sizeof (struct elf_link_hash_entry *));
5163 nondeflt_vers
5164 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5165 if (!nondeflt_vers)
5166 goto error_free_vers;
5167 }
5168 nondeflt_vers[nondeflt_vers_cnt++] = h;
5169 }
5170 }
5171
5172 if (dynsym && h->dynindx == -1)
5173 {
5174 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5175 goto error_free_vers;
5176 if (h->is_weakalias
5177 && weakdef (h)->dynindx == -1)
5178 {
5179 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5180 goto error_free_vers;
5181 }
5182 }
5183 else if (h->dynindx != -1)
5184 /* If the symbol already has a dynamic index, but
5185 visibility says it should not be visible, turn it into
5186 a local symbol. */
5187 switch (ELF_ST_VISIBILITY (h->other))
5188 {
5189 case STV_INTERNAL:
5190 case STV_HIDDEN:
5191 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5192 dynsym = FALSE;
5193 break;
5194 }
5195
5196 /* Don't add DT_NEEDED for references from the dummy bfd nor
5197 for unmatched symbol. */
5198 if (!add_needed
5199 && matched
5200 && definition
5201 && ((dynsym
5202 && h->ref_regular_nonweak
5203 && (old_bfd == NULL
5204 || (old_bfd->flags & BFD_PLUGIN) == 0))
5205 || (h->ref_dynamic_nonweak
5206 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5207 && !on_needed_list (elf_dt_name (abfd),
5208 htab->needed, NULL))))
5209 {
5210 const char *soname = elf_dt_name (abfd);
5211
5212 info->callbacks->minfo ("%!", soname, old_bfd,
5213 h->root.root.string);
5214
5215 /* A symbol from a library loaded via DT_NEEDED of some
5216 other library is referenced by a regular object.
5217 Add a DT_NEEDED entry for it. Issue an error if
5218 --no-add-needed is used and the reference was not
5219 a weak one. */
5220 if (old_bfd != NULL
5221 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5222 {
5223 _bfd_error_handler
5224 /* xgettext:c-format */
5225 (_("%pB: undefined reference to symbol '%s'"),
5226 old_bfd, name);
5227 bfd_set_error (bfd_error_missing_dso);
5228 goto error_free_vers;
5229 }
5230
5231 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5232 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5233
5234 /* Create dynamic sections for backends that require
5235 that be done before setup_gnu_properties. */
5236 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5237 return FALSE;
5238 add_needed = TRUE;
5239 }
5240 }
5241 }
5242
5243 if (info->lto_plugin_active
5244 && !bfd_link_relocatable (info)
5245 && (abfd->flags & BFD_PLUGIN) == 0
5246 && !just_syms
5247 && extsymcount)
5248 {
5249 int r_sym_shift;
5250
5251 if (bed->s->arch_size == 32)
5252 r_sym_shift = 8;
5253 else
5254 r_sym_shift = 32;
5255
5256 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5257 referenced in regular objects so that linker plugin will get
5258 the correct symbol resolution. */
5259
5260 sym_hash = elf_sym_hashes (abfd);
5261 for (s = abfd->sections; s != NULL; s = s->next)
5262 {
5263 Elf_Internal_Rela *internal_relocs;
5264 Elf_Internal_Rela *rel, *relend;
5265
5266 /* Don't check relocations in excluded sections. */
5267 if ((s->flags & SEC_RELOC) == 0
5268 || s->reloc_count == 0
5269 || (s->flags & SEC_EXCLUDE) != 0
5270 || ((info->strip == strip_all
5271 || info->strip == strip_debugger)
5272 && (s->flags & SEC_DEBUGGING) != 0))
5273 continue;
5274
5275 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5276 NULL,
5277 info->keep_memory);
5278 if (internal_relocs == NULL)
5279 goto error_free_vers;
5280
5281 rel = internal_relocs;
5282 relend = rel + s->reloc_count;
5283 for ( ; rel < relend; rel++)
5284 {
5285 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5286 struct elf_link_hash_entry *h;
5287
5288 /* Skip local symbols. */
5289 if (r_symndx < extsymoff)
5290 continue;
5291
5292 h = sym_hash[r_symndx - extsymoff];
5293 if (h != NULL)
5294 h->root.non_ir_ref_regular = 1;
5295 }
5296
5297 if (elf_section_data (s)->relocs != internal_relocs)
5298 free (internal_relocs);
5299 }
5300 }
5301
5302 if (extversym != NULL)
5303 {
5304 free (extversym);
5305 extversym = NULL;
5306 }
5307
5308 if (isymbuf != NULL)
5309 {
5310 free (isymbuf);
5311 isymbuf = NULL;
5312 }
5313
5314 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5315 {
5316 unsigned int i;
5317
5318 /* Restore the symbol table. */
5319 old_ent = (char *) old_tab + tabsize;
5320 memset (elf_sym_hashes (abfd), 0,
5321 extsymcount * sizeof (struct elf_link_hash_entry *));
5322 htab->root.table.table = old_table;
5323 htab->root.table.size = old_size;
5324 htab->root.table.count = old_count;
5325 memcpy (htab->root.table.table, old_tab, tabsize);
5326 htab->root.undefs = old_undefs;
5327 htab->root.undefs_tail = old_undefs_tail;
5328 if (htab->dynstr != NULL)
5329 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5330 free (old_strtab);
5331 old_strtab = NULL;
5332 for (i = 0; i < htab->root.table.size; i++)
5333 {
5334 struct bfd_hash_entry *p;
5335 struct elf_link_hash_entry *h;
5336 bfd_size_type size;
5337 unsigned int alignment_power;
5338 unsigned int non_ir_ref_dynamic;
5339
5340 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5341 {
5342 h = (struct elf_link_hash_entry *) p;
5343 if (h->root.type == bfd_link_hash_warning)
5344 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5345
5346 /* Preserve the maximum alignment and size for common
5347 symbols even if this dynamic lib isn't on DT_NEEDED
5348 since it can still be loaded at run time by another
5349 dynamic lib. */
5350 if (h->root.type == bfd_link_hash_common)
5351 {
5352 size = h->root.u.c.size;
5353 alignment_power = h->root.u.c.p->alignment_power;
5354 }
5355 else
5356 {
5357 size = 0;
5358 alignment_power = 0;
5359 }
5360 /* Preserve non_ir_ref_dynamic so that this symbol
5361 will be exported when the dynamic lib becomes needed
5362 in the second pass. */
5363 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5364 memcpy (p, old_ent, htab->root.table.entsize);
5365 old_ent = (char *) old_ent + htab->root.table.entsize;
5366 h = (struct elf_link_hash_entry *) p;
5367 if (h->root.type == bfd_link_hash_warning)
5368 {
5369 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5370 old_ent = (char *) old_ent + htab->root.table.entsize;
5371 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5372 }
5373 if (h->root.type == bfd_link_hash_common)
5374 {
5375 if (size > h->root.u.c.size)
5376 h->root.u.c.size = size;
5377 if (alignment_power > h->root.u.c.p->alignment_power)
5378 h->root.u.c.p->alignment_power = alignment_power;
5379 }
5380 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5381 }
5382 }
5383
5384 /* Make a special call to the linker "notice" function to
5385 tell it that symbols added for crefs may need to be removed. */
5386 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5387 goto error_free_vers;
5388
5389 free (old_tab);
5390 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5391 alloc_mark);
5392 if (nondeflt_vers != NULL)
5393 free (nondeflt_vers);
5394 return TRUE;
5395 }
5396
5397 if (old_tab != NULL)
5398 {
5399 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5400 goto error_free_vers;
5401 free (old_tab);
5402 old_tab = NULL;
5403 }
5404
5405 /* Now that all the symbols from this input file are created, if
5406 not performing a relocatable link, handle .symver foo, foo@BAR
5407 such that any relocs against foo become foo@BAR. */
5408 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5409 {
5410 size_t cnt, symidx;
5411
5412 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5413 {
5414 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5415 char *shortname, *p;
5416 size_t amt;
5417
5418 p = strchr (h->root.root.string, ELF_VER_CHR);
5419 if (p == NULL
5420 || (h->root.type != bfd_link_hash_defined
5421 && h->root.type != bfd_link_hash_defweak))
5422 continue;
5423
5424 amt = p - h->root.root.string;
5425 shortname = (char *) bfd_malloc (amt + 1);
5426 if (!shortname)
5427 goto error_free_vers;
5428 memcpy (shortname, h->root.root.string, amt);
5429 shortname[amt] = '\0';
5430
5431 hi = (struct elf_link_hash_entry *)
5432 bfd_link_hash_lookup (&htab->root, shortname,
5433 FALSE, FALSE, FALSE);
5434 if (hi != NULL
5435 && hi->root.type == h->root.type
5436 && hi->root.u.def.value == h->root.u.def.value
5437 && hi->root.u.def.section == h->root.u.def.section)
5438 {
5439 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5440 hi->root.type = bfd_link_hash_indirect;
5441 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5442 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5443 sym_hash = elf_sym_hashes (abfd);
5444 if (sym_hash)
5445 for (symidx = 0; symidx < extsymcount; ++symidx)
5446 if (sym_hash[symidx] == hi)
5447 {
5448 sym_hash[symidx] = h;
5449 break;
5450 }
5451 }
5452 free (shortname);
5453 }
5454 free (nondeflt_vers);
5455 nondeflt_vers = NULL;
5456 }
5457
5458 /* Now set the alias field correctly for all the weak defined
5459 symbols we found. The only way to do this is to search all the
5460 symbols. Since we only need the information for non functions in
5461 dynamic objects, that's the only time we actually put anything on
5462 the list WEAKS. We need this information so that if a regular
5463 object refers to a symbol defined weakly in a dynamic object, the
5464 real symbol in the dynamic object is also put in the dynamic
5465 symbols; we also must arrange for both symbols to point to the
5466 same memory location. We could handle the general case of symbol
5467 aliasing, but a general symbol alias can only be generated in
5468 assembler code, handling it correctly would be very time
5469 consuming, and other ELF linkers don't handle general aliasing
5470 either. */
5471 if (weaks != NULL)
5472 {
5473 struct elf_link_hash_entry **hpp;
5474 struct elf_link_hash_entry **hppend;
5475 struct elf_link_hash_entry **sorted_sym_hash;
5476 struct elf_link_hash_entry *h;
5477 size_t sym_count, amt;
5478
5479 /* Since we have to search the whole symbol list for each weak
5480 defined symbol, search time for N weak defined symbols will be
5481 O(N^2). Binary search will cut it down to O(NlogN). */
5482 amt = extsymcount * sizeof (*sorted_sym_hash);
5483 sorted_sym_hash = bfd_malloc (amt);
5484 if (sorted_sym_hash == NULL)
5485 goto error_return;
5486 sym_hash = sorted_sym_hash;
5487 hpp = elf_sym_hashes (abfd);
5488 hppend = hpp + extsymcount;
5489 sym_count = 0;
5490 for (; hpp < hppend; hpp++)
5491 {
5492 h = *hpp;
5493 if (h != NULL
5494 && h->root.type == bfd_link_hash_defined
5495 && !bed->is_function_type (h->type))
5496 {
5497 *sym_hash = h;
5498 sym_hash++;
5499 sym_count++;
5500 }
5501 }
5502
5503 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5504 elf_sort_symbol);
5505
5506 while (weaks != NULL)
5507 {
5508 struct elf_link_hash_entry *hlook;
5509 asection *slook;
5510 bfd_vma vlook;
5511 size_t i, j, idx = 0;
5512
5513 hlook = weaks;
5514 weaks = hlook->u.alias;
5515 hlook->u.alias = NULL;
5516
5517 if (hlook->root.type != bfd_link_hash_defined
5518 && hlook->root.type != bfd_link_hash_defweak)
5519 continue;
5520
5521 slook = hlook->root.u.def.section;
5522 vlook = hlook->root.u.def.value;
5523
5524 i = 0;
5525 j = sym_count;
5526 while (i != j)
5527 {
5528 bfd_signed_vma vdiff;
5529 idx = (i + j) / 2;
5530 h = sorted_sym_hash[idx];
5531 vdiff = vlook - h->root.u.def.value;
5532 if (vdiff < 0)
5533 j = idx;
5534 else if (vdiff > 0)
5535 i = idx + 1;
5536 else
5537 {
5538 int sdiff = slook->id - h->root.u.def.section->id;
5539 if (sdiff < 0)
5540 j = idx;
5541 else if (sdiff > 0)
5542 i = idx + 1;
5543 else
5544 break;
5545 }
5546 }
5547
5548 /* We didn't find a value/section match. */
5549 if (i == j)
5550 continue;
5551
5552 /* With multiple aliases, or when the weak symbol is already
5553 strongly defined, we have multiple matching symbols and
5554 the binary search above may land on any of them. Step
5555 one past the matching symbol(s). */
5556 while (++idx != j)
5557 {
5558 h = sorted_sym_hash[idx];
5559 if (h->root.u.def.section != slook
5560 || h->root.u.def.value != vlook)
5561 break;
5562 }
5563
5564 /* Now look back over the aliases. Since we sorted by size
5565 as well as value and section, we'll choose the one with
5566 the largest size. */
5567 while (idx-- != i)
5568 {
5569 h = sorted_sym_hash[idx];
5570
5571 /* Stop if value or section doesn't match. */
5572 if (h->root.u.def.section != slook
5573 || h->root.u.def.value != vlook)
5574 break;
5575 else if (h != hlook)
5576 {
5577 struct elf_link_hash_entry *t;
5578
5579 hlook->u.alias = h;
5580 hlook->is_weakalias = 1;
5581 t = h;
5582 if (t->u.alias != NULL)
5583 while (t->u.alias != h)
5584 t = t->u.alias;
5585 t->u.alias = hlook;
5586
5587 /* If the weak definition is in the list of dynamic
5588 symbols, make sure the real definition is put
5589 there as well. */
5590 if (hlook->dynindx != -1 && h->dynindx == -1)
5591 {
5592 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5593 {
5594 err_free_sym_hash:
5595 free (sorted_sym_hash);
5596 goto error_return;
5597 }
5598 }
5599
5600 /* If the real definition is in the list of dynamic
5601 symbols, make sure the weak definition is put
5602 there as well. If we don't do this, then the
5603 dynamic loader might not merge the entries for the
5604 real definition and the weak definition. */
5605 if (h->dynindx != -1 && hlook->dynindx == -1)
5606 {
5607 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5608 goto err_free_sym_hash;
5609 }
5610 break;
5611 }
5612 }
5613 }
5614
5615 free (sorted_sym_hash);
5616 }
5617
5618 if (bed->check_directives
5619 && !(*bed->check_directives) (abfd, info))
5620 return FALSE;
5621
5622 /* If this is a non-traditional link, try to optimize the handling
5623 of the .stab/.stabstr sections. */
5624 if (! dynamic
5625 && ! info->traditional_format
5626 && is_elf_hash_table (htab)
5627 && (info->strip != strip_all && info->strip != strip_debugger))
5628 {
5629 asection *stabstr;
5630
5631 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5632 if (stabstr != NULL)
5633 {
5634 bfd_size_type string_offset = 0;
5635 asection *stab;
5636
5637 for (stab = abfd->sections; stab; stab = stab->next)
5638 if (CONST_STRNEQ (stab->name, ".stab")
5639 && (!stab->name[5] ||
5640 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5641 && (stab->flags & SEC_MERGE) == 0
5642 && !bfd_is_abs_section (stab->output_section))
5643 {
5644 struct bfd_elf_section_data *secdata;
5645
5646 secdata = elf_section_data (stab);
5647 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5648 stabstr, &secdata->sec_info,
5649 &string_offset))
5650 goto error_return;
5651 if (secdata->sec_info)
5652 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5653 }
5654 }
5655 }
5656
5657 if (dynamic && add_needed)
5658 {
5659 /* Add this bfd to the loaded list. */
5660 struct elf_link_loaded_list *n;
5661
5662 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5663 if (n == NULL)
5664 goto error_return;
5665 n->abfd = abfd;
5666 n->next = htab->dyn_loaded;
5667 htab->dyn_loaded = n;
5668 }
5669 if (dynamic && !add_needed
5670 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5671 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5672
5673 return TRUE;
5674
5675 error_free_vers:
5676 if (old_tab != NULL)
5677 free (old_tab);
5678 if (old_strtab != NULL)
5679 free (old_strtab);
5680 if (nondeflt_vers != NULL)
5681 free (nondeflt_vers);
5682 if (extversym != NULL)
5683 free (extversym);
5684 error_free_sym:
5685 if (isymbuf != NULL)
5686 free (isymbuf);
5687 error_return:
5688 return FALSE;
5689 }
5690
5691 /* Return the linker hash table entry of a symbol that might be
5692 satisfied by an archive symbol. Return -1 on error. */
5693
5694 struct elf_link_hash_entry *
5695 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5696 struct bfd_link_info *info,
5697 const char *name)
5698 {
5699 struct elf_link_hash_entry *h;
5700 char *p, *copy;
5701 size_t len, first;
5702
5703 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5704 if (h != NULL)
5705 return h;
5706
5707 /* If this is a default version (the name contains @@), look up the
5708 symbol again with only one `@' as well as without the version.
5709 The effect is that references to the symbol with and without the
5710 version will be matched by the default symbol in the archive. */
5711
5712 p = strchr (name, ELF_VER_CHR);
5713 if (p == NULL || p[1] != ELF_VER_CHR)
5714 return h;
5715
5716 /* First check with only one `@'. */
5717 len = strlen (name);
5718 copy = (char *) bfd_alloc (abfd, len);
5719 if (copy == NULL)
5720 return (struct elf_link_hash_entry *) -1;
5721
5722 first = p - name + 1;
5723 memcpy (copy, name, first);
5724 memcpy (copy + first, name + first + 1, len - first);
5725
5726 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5727 if (h == NULL)
5728 {
5729 /* We also need to check references to the symbol without the
5730 version. */
5731 copy[first - 1] = '\0';
5732 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5733 FALSE, FALSE, TRUE);
5734 }
5735
5736 bfd_release (abfd, copy);
5737 return h;
5738 }
5739
5740 /* Add symbols from an ELF archive file to the linker hash table. We
5741 don't use _bfd_generic_link_add_archive_symbols because we need to
5742 handle versioned symbols.
5743
5744 Fortunately, ELF archive handling is simpler than that done by
5745 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5746 oddities. In ELF, if we find a symbol in the archive map, and the
5747 symbol is currently undefined, we know that we must pull in that
5748 object file.
5749
5750 Unfortunately, we do have to make multiple passes over the symbol
5751 table until nothing further is resolved. */
5752
5753 static bfd_boolean
5754 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5755 {
5756 symindex c;
5757 unsigned char *included = NULL;
5758 carsym *symdefs;
5759 bfd_boolean loop;
5760 size_t amt;
5761 const struct elf_backend_data *bed;
5762 struct elf_link_hash_entry * (*archive_symbol_lookup)
5763 (bfd *, struct bfd_link_info *, const char *);
5764
5765 if (! bfd_has_map (abfd))
5766 {
5767 /* An empty archive is a special case. */
5768 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5769 return TRUE;
5770 bfd_set_error (bfd_error_no_armap);
5771 return FALSE;
5772 }
5773
5774 /* Keep track of all symbols we know to be already defined, and all
5775 files we know to be already included. This is to speed up the
5776 second and subsequent passes. */
5777 c = bfd_ardata (abfd)->symdef_count;
5778 if (c == 0)
5779 return TRUE;
5780 amt = c * sizeof (*included);
5781 included = (unsigned char *) bfd_zmalloc (amt);
5782 if (included == NULL)
5783 return FALSE;
5784
5785 symdefs = bfd_ardata (abfd)->symdefs;
5786 bed = get_elf_backend_data (abfd);
5787 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5788
5789 do
5790 {
5791 file_ptr last;
5792 symindex i;
5793 carsym *symdef;
5794 carsym *symdefend;
5795
5796 loop = FALSE;
5797 last = -1;
5798
5799 symdef = symdefs;
5800 symdefend = symdef + c;
5801 for (i = 0; symdef < symdefend; symdef++, i++)
5802 {
5803 struct elf_link_hash_entry *h;
5804 bfd *element;
5805 struct bfd_link_hash_entry *undefs_tail;
5806 symindex mark;
5807
5808 if (included[i])
5809 continue;
5810 if (symdef->file_offset == last)
5811 {
5812 included[i] = TRUE;
5813 continue;
5814 }
5815
5816 h = archive_symbol_lookup (abfd, info, symdef->name);
5817 if (h == (struct elf_link_hash_entry *) -1)
5818 goto error_return;
5819
5820 if (h == NULL)
5821 continue;
5822
5823 if (h->root.type == bfd_link_hash_common)
5824 {
5825 /* We currently have a common symbol. The archive map contains
5826 a reference to this symbol, so we may want to include it. We
5827 only want to include it however, if this archive element
5828 contains a definition of the symbol, not just another common
5829 declaration of it.
5830
5831 Unfortunately some archivers (including GNU ar) will put
5832 declarations of common symbols into their archive maps, as
5833 well as real definitions, so we cannot just go by the archive
5834 map alone. Instead we must read in the element's symbol
5835 table and check that to see what kind of symbol definition
5836 this is. */
5837 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5838 continue;
5839 }
5840 else if (h->root.type != bfd_link_hash_undefined)
5841 {
5842 if (h->root.type != bfd_link_hash_undefweak)
5843 /* Symbol must be defined. Don't check it again. */
5844 included[i] = TRUE;
5845 continue;
5846 }
5847
5848 /* We need to include this archive member. */
5849 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5850 if (element == NULL)
5851 goto error_return;
5852
5853 if (! bfd_check_format (element, bfd_object))
5854 goto error_return;
5855
5856 undefs_tail = info->hash->undefs_tail;
5857
5858 if (!(*info->callbacks
5859 ->add_archive_element) (info, element, symdef->name, &element))
5860 continue;
5861 if (!bfd_link_add_symbols (element, info))
5862 goto error_return;
5863
5864 /* If there are any new undefined symbols, we need to make
5865 another pass through the archive in order to see whether
5866 they can be defined. FIXME: This isn't perfect, because
5867 common symbols wind up on undefs_tail and because an
5868 undefined symbol which is defined later on in this pass
5869 does not require another pass. This isn't a bug, but it
5870 does make the code less efficient than it could be. */
5871 if (undefs_tail != info->hash->undefs_tail)
5872 loop = TRUE;
5873
5874 /* Look backward to mark all symbols from this object file
5875 which we have already seen in this pass. */
5876 mark = i;
5877 do
5878 {
5879 included[mark] = TRUE;
5880 if (mark == 0)
5881 break;
5882 --mark;
5883 }
5884 while (symdefs[mark].file_offset == symdef->file_offset);
5885
5886 /* We mark subsequent symbols from this object file as we go
5887 on through the loop. */
5888 last = symdef->file_offset;
5889 }
5890 }
5891 while (loop);
5892
5893 free (included);
5894
5895 return TRUE;
5896
5897 error_return:
5898 if (included != NULL)
5899 free (included);
5900 return FALSE;
5901 }
5902
5903 /* Given an ELF BFD, add symbols to the global hash table as
5904 appropriate. */
5905
5906 bfd_boolean
5907 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5908 {
5909 switch (bfd_get_format (abfd))
5910 {
5911 case bfd_object:
5912 return elf_link_add_object_symbols (abfd, info);
5913 case bfd_archive:
5914 return elf_link_add_archive_symbols (abfd, info);
5915 default:
5916 bfd_set_error (bfd_error_wrong_format);
5917 return FALSE;
5918 }
5919 }
5920 \f
5921 struct hash_codes_info
5922 {
5923 unsigned long *hashcodes;
5924 bfd_boolean error;
5925 };
5926
5927 /* This function will be called though elf_link_hash_traverse to store
5928 all hash value of the exported symbols in an array. */
5929
5930 static bfd_boolean
5931 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5932 {
5933 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5934 const char *name;
5935 unsigned long ha;
5936 char *alc = NULL;
5937
5938 /* Ignore indirect symbols. These are added by the versioning code. */
5939 if (h->dynindx == -1)
5940 return TRUE;
5941
5942 name = h->root.root.string;
5943 if (h->versioned >= versioned)
5944 {
5945 char *p = strchr (name, ELF_VER_CHR);
5946 if (p != NULL)
5947 {
5948 alc = (char *) bfd_malloc (p - name + 1);
5949 if (alc == NULL)
5950 {
5951 inf->error = TRUE;
5952 return FALSE;
5953 }
5954 memcpy (alc, name, p - name);
5955 alc[p - name] = '\0';
5956 name = alc;
5957 }
5958 }
5959
5960 /* Compute the hash value. */
5961 ha = bfd_elf_hash (name);
5962
5963 /* Store the found hash value in the array given as the argument. */
5964 *(inf->hashcodes)++ = ha;
5965
5966 /* And store it in the struct so that we can put it in the hash table
5967 later. */
5968 h->u.elf_hash_value = ha;
5969
5970 if (alc != NULL)
5971 free (alc);
5972
5973 return TRUE;
5974 }
5975
5976 struct collect_gnu_hash_codes
5977 {
5978 bfd *output_bfd;
5979 const struct elf_backend_data *bed;
5980 unsigned long int nsyms;
5981 unsigned long int maskbits;
5982 unsigned long int *hashcodes;
5983 unsigned long int *hashval;
5984 unsigned long int *indx;
5985 unsigned long int *counts;
5986 bfd_vma *bitmask;
5987 bfd_byte *contents;
5988 bfd_size_type xlat;
5989 long int min_dynindx;
5990 unsigned long int bucketcount;
5991 unsigned long int symindx;
5992 long int local_indx;
5993 long int shift1, shift2;
5994 unsigned long int mask;
5995 bfd_boolean error;
5996 };
5997
5998 /* This function will be called though elf_link_hash_traverse to store
5999 all hash value of the exported symbols in an array. */
6000
6001 static bfd_boolean
6002 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6003 {
6004 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6005 const char *name;
6006 unsigned long ha;
6007 char *alc = NULL;
6008
6009 /* Ignore indirect symbols. These are added by the versioning code. */
6010 if (h->dynindx == -1)
6011 return TRUE;
6012
6013 /* Ignore also local symbols and undefined symbols. */
6014 if (! (*s->bed->elf_hash_symbol) (h))
6015 return TRUE;
6016
6017 name = h->root.root.string;
6018 if (h->versioned >= versioned)
6019 {
6020 char *p = strchr (name, ELF_VER_CHR);
6021 if (p != NULL)
6022 {
6023 alc = (char *) bfd_malloc (p - name + 1);
6024 if (alc == NULL)
6025 {
6026 s->error = TRUE;
6027 return FALSE;
6028 }
6029 memcpy (alc, name, p - name);
6030 alc[p - name] = '\0';
6031 name = alc;
6032 }
6033 }
6034
6035 /* Compute the hash value. */
6036 ha = bfd_elf_gnu_hash (name);
6037
6038 /* Store the found hash value in the array for compute_bucket_count,
6039 and also for .dynsym reordering purposes. */
6040 s->hashcodes[s->nsyms] = ha;
6041 s->hashval[h->dynindx] = ha;
6042 ++s->nsyms;
6043 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6044 s->min_dynindx = h->dynindx;
6045
6046 if (alc != NULL)
6047 free (alc);
6048
6049 return TRUE;
6050 }
6051
6052 /* This function will be called though elf_link_hash_traverse to do
6053 final dynamic symbol renumbering in case of .gnu.hash.
6054 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6055 to the translation table. */
6056
6057 static bfd_boolean
6058 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6059 {
6060 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6061 unsigned long int bucket;
6062 unsigned long int val;
6063
6064 /* Ignore indirect symbols. */
6065 if (h->dynindx == -1)
6066 return TRUE;
6067
6068 /* Ignore also local symbols and undefined symbols. */
6069 if (! (*s->bed->elf_hash_symbol) (h))
6070 {
6071 if (h->dynindx >= s->min_dynindx)
6072 {
6073 if (s->bed->record_xhash_symbol != NULL)
6074 {
6075 (*s->bed->record_xhash_symbol) (h, 0);
6076 s->local_indx++;
6077 }
6078 else
6079 h->dynindx = s->local_indx++;
6080 }
6081 return TRUE;
6082 }
6083
6084 bucket = s->hashval[h->dynindx] % s->bucketcount;
6085 val = (s->hashval[h->dynindx] >> s->shift1)
6086 & ((s->maskbits >> s->shift1) - 1);
6087 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6088 s->bitmask[val]
6089 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6090 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6091 if (s->counts[bucket] == 1)
6092 /* Last element terminates the chain. */
6093 val |= 1;
6094 bfd_put_32 (s->output_bfd, val,
6095 s->contents + (s->indx[bucket] - s->symindx) * 4);
6096 --s->counts[bucket];
6097 if (s->bed->record_xhash_symbol != NULL)
6098 {
6099 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6100
6101 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6102 }
6103 else
6104 h->dynindx = s->indx[bucket]++;
6105 return TRUE;
6106 }
6107
6108 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6109
6110 bfd_boolean
6111 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6112 {
6113 return !(h->forced_local
6114 || h->root.type == bfd_link_hash_undefined
6115 || h->root.type == bfd_link_hash_undefweak
6116 || ((h->root.type == bfd_link_hash_defined
6117 || h->root.type == bfd_link_hash_defweak)
6118 && h->root.u.def.section->output_section == NULL));
6119 }
6120
6121 /* Array used to determine the number of hash table buckets to use
6122 based on the number of symbols there are. If there are fewer than
6123 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6124 fewer than 37 we use 17 buckets, and so forth. We never use more
6125 than 32771 buckets. */
6126
6127 static const size_t elf_buckets[] =
6128 {
6129 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6130 16411, 32771, 0
6131 };
6132
6133 /* Compute bucket count for hashing table. We do not use a static set
6134 of possible tables sizes anymore. Instead we determine for all
6135 possible reasonable sizes of the table the outcome (i.e., the
6136 number of collisions etc) and choose the best solution. The
6137 weighting functions are not too simple to allow the table to grow
6138 without bounds. Instead one of the weighting factors is the size.
6139 Therefore the result is always a good payoff between few collisions
6140 (= short chain lengths) and table size. */
6141 static size_t
6142 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6143 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6144 unsigned long int nsyms,
6145 int gnu_hash)
6146 {
6147 size_t best_size = 0;
6148 unsigned long int i;
6149
6150 /* We have a problem here. The following code to optimize the table
6151 size requires an integer type with more the 32 bits. If
6152 BFD_HOST_U_64_BIT is set we know about such a type. */
6153 #ifdef BFD_HOST_U_64_BIT
6154 if (info->optimize)
6155 {
6156 size_t minsize;
6157 size_t maxsize;
6158 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6159 bfd *dynobj = elf_hash_table (info)->dynobj;
6160 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6161 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6162 unsigned long int *counts;
6163 bfd_size_type amt;
6164 unsigned int no_improvement_count = 0;
6165
6166 /* Possible optimization parameters: if we have NSYMS symbols we say
6167 that the hashing table must at least have NSYMS/4 and at most
6168 2*NSYMS buckets. */
6169 minsize = nsyms / 4;
6170 if (minsize == 0)
6171 minsize = 1;
6172 best_size = maxsize = nsyms * 2;
6173 if (gnu_hash)
6174 {
6175 if (minsize < 2)
6176 minsize = 2;
6177 if ((best_size & 31) == 0)
6178 ++best_size;
6179 }
6180
6181 /* Create array where we count the collisions in. We must use bfd_malloc
6182 since the size could be large. */
6183 amt = maxsize;
6184 amt *= sizeof (unsigned long int);
6185 counts = (unsigned long int *) bfd_malloc (amt);
6186 if (counts == NULL)
6187 return 0;
6188
6189 /* Compute the "optimal" size for the hash table. The criteria is a
6190 minimal chain length. The minor criteria is (of course) the size
6191 of the table. */
6192 for (i = minsize; i < maxsize; ++i)
6193 {
6194 /* Walk through the array of hashcodes and count the collisions. */
6195 BFD_HOST_U_64_BIT max;
6196 unsigned long int j;
6197 unsigned long int fact;
6198
6199 if (gnu_hash && (i & 31) == 0)
6200 continue;
6201
6202 memset (counts, '\0', i * sizeof (unsigned long int));
6203
6204 /* Determine how often each hash bucket is used. */
6205 for (j = 0; j < nsyms; ++j)
6206 ++counts[hashcodes[j] % i];
6207
6208 /* For the weight function we need some information about the
6209 pagesize on the target. This is information need not be 100%
6210 accurate. Since this information is not available (so far) we
6211 define it here to a reasonable default value. If it is crucial
6212 to have a better value some day simply define this value. */
6213 # ifndef BFD_TARGET_PAGESIZE
6214 # define BFD_TARGET_PAGESIZE (4096)
6215 # endif
6216
6217 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6218 and the chains. */
6219 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6220
6221 # if 1
6222 /* Variant 1: optimize for short chains. We add the squares
6223 of all the chain lengths (which favors many small chain
6224 over a few long chains). */
6225 for (j = 0; j < i; ++j)
6226 max += counts[j] * counts[j];
6227
6228 /* This adds penalties for the overall size of the table. */
6229 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6230 max *= fact * fact;
6231 # else
6232 /* Variant 2: Optimize a lot more for small table. Here we
6233 also add squares of the size but we also add penalties for
6234 empty slots (the +1 term). */
6235 for (j = 0; j < i; ++j)
6236 max += (1 + counts[j]) * (1 + counts[j]);
6237
6238 /* The overall size of the table is considered, but not as
6239 strong as in variant 1, where it is squared. */
6240 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6241 max *= fact;
6242 # endif
6243
6244 /* Compare with current best results. */
6245 if (max < best_chlen)
6246 {
6247 best_chlen = max;
6248 best_size = i;
6249 no_improvement_count = 0;
6250 }
6251 /* PR 11843: Avoid futile long searches for the best bucket size
6252 when there are a large number of symbols. */
6253 else if (++no_improvement_count == 100)
6254 break;
6255 }
6256
6257 free (counts);
6258 }
6259 else
6260 #endif /* defined (BFD_HOST_U_64_BIT) */
6261 {
6262 /* This is the fallback solution if no 64bit type is available or if we
6263 are not supposed to spend much time on optimizations. We select the
6264 bucket count using a fixed set of numbers. */
6265 for (i = 0; elf_buckets[i] != 0; i++)
6266 {
6267 best_size = elf_buckets[i];
6268 if (nsyms < elf_buckets[i + 1])
6269 break;
6270 }
6271 if (gnu_hash && best_size < 2)
6272 best_size = 2;
6273 }
6274
6275 return best_size;
6276 }
6277
6278 /* Size any SHT_GROUP section for ld -r. */
6279
6280 bfd_boolean
6281 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6282 {
6283 bfd *ibfd;
6284 asection *s;
6285
6286 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6287 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6288 && (s = ibfd->sections) != NULL
6289 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6290 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6291 return FALSE;
6292 return TRUE;
6293 }
6294
6295 /* Set a default stack segment size. The value in INFO wins. If it
6296 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6297 undefined it is initialized. */
6298
6299 bfd_boolean
6300 bfd_elf_stack_segment_size (bfd *output_bfd,
6301 struct bfd_link_info *info,
6302 const char *legacy_symbol,
6303 bfd_vma default_size)
6304 {
6305 struct elf_link_hash_entry *h = NULL;
6306
6307 /* Look for legacy symbol. */
6308 if (legacy_symbol)
6309 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6310 FALSE, FALSE, FALSE);
6311 if (h && (h->root.type == bfd_link_hash_defined
6312 || h->root.type == bfd_link_hash_defweak)
6313 && h->def_regular
6314 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6315 {
6316 /* The symbol has no type if specified on the command line. */
6317 h->type = STT_OBJECT;
6318 if (info->stacksize)
6319 /* xgettext:c-format */
6320 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6321 output_bfd, legacy_symbol);
6322 else if (h->root.u.def.section != bfd_abs_section_ptr)
6323 /* xgettext:c-format */
6324 _bfd_error_handler (_("%pB: %s not absolute"),
6325 output_bfd, legacy_symbol);
6326 else
6327 info->stacksize = h->root.u.def.value;
6328 }
6329
6330 if (!info->stacksize)
6331 /* If the user didn't set a size, or explicitly inhibit the
6332 size, set it now. */
6333 info->stacksize = default_size;
6334
6335 /* Provide the legacy symbol, if it is referenced. */
6336 if (h && (h->root.type == bfd_link_hash_undefined
6337 || h->root.type == bfd_link_hash_undefweak))
6338 {
6339 struct bfd_link_hash_entry *bh = NULL;
6340
6341 if (!(_bfd_generic_link_add_one_symbol
6342 (info, output_bfd, legacy_symbol,
6343 BSF_GLOBAL, bfd_abs_section_ptr,
6344 info->stacksize >= 0 ? info->stacksize : 0,
6345 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6346 return FALSE;
6347
6348 h = (struct elf_link_hash_entry *) bh;
6349 h->def_regular = 1;
6350 h->type = STT_OBJECT;
6351 }
6352
6353 return TRUE;
6354 }
6355
6356 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6357
6358 struct elf_gc_sweep_symbol_info
6359 {
6360 struct bfd_link_info *info;
6361 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6362 bfd_boolean);
6363 };
6364
6365 static bfd_boolean
6366 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6367 {
6368 if (!h->mark
6369 && (((h->root.type == bfd_link_hash_defined
6370 || h->root.type == bfd_link_hash_defweak)
6371 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6372 && h->root.u.def.section->gc_mark))
6373 || h->root.type == bfd_link_hash_undefined
6374 || h->root.type == bfd_link_hash_undefweak))
6375 {
6376 struct elf_gc_sweep_symbol_info *inf;
6377
6378 inf = (struct elf_gc_sweep_symbol_info *) data;
6379 (*inf->hide_symbol) (inf->info, h, TRUE);
6380 h->def_regular = 0;
6381 h->ref_regular = 0;
6382 h->ref_regular_nonweak = 0;
6383 }
6384
6385 return TRUE;
6386 }
6387
6388 /* Set up the sizes and contents of the ELF dynamic sections. This is
6389 called by the ELF linker emulation before_allocation routine. We
6390 must set the sizes of the sections before the linker sets the
6391 addresses of the various sections. */
6392
6393 bfd_boolean
6394 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6395 const char *soname,
6396 const char *rpath,
6397 const char *filter_shlib,
6398 const char *audit,
6399 const char *depaudit,
6400 const char * const *auxiliary_filters,
6401 struct bfd_link_info *info,
6402 asection **sinterpptr)
6403 {
6404 bfd *dynobj;
6405 const struct elf_backend_data *bed;
6406
6407 *sinterpptr = NULL;
6408
6409 if (!is_elf_hash_table (info->hash))
6410 return TRUE;
6411
6412 dynobj = elf_hash_table (info)->dynobj;
6413
6414 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6415 {
6416 struct bfd_elf_version_tree *verdefs;
6417 struct elf_info_failed asvinfo;
6418 struct bfd_elf_version_tree *t;
6419 struct bfd_elf_version_expr *d;
6420 asection *s;
6421 size_t soname_indx;
6422
6423 /* If we are supposed to export all symbols into the dynamic symbol
6424 table (this is not the normal case), then do so. */
6425 if (info->export_dynamic
6426 || (bfd_link_executable (info) && info->dynamic))
6427 {
6428 struct elf_info_failed eif;
6429
6430 eif.info = info;
6431 eif.failed = FALSE;
6432 elf_link_hash_traverse (elf_hash_table (info),
6433 _bfd_elf_export_symbol,
6434 &eif);
6435 if (eif.failed)
6436 return FALSE;
6437 }
6438
6439 if (soname != NULL)
6440 {
6441 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6442 soname, TRUE);
6443 if (soname_indx == (size_t) -1
6444 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6445 return FALSE;
6446 }
6447 else
6448 soname_indx = (size_t) -1;
6449
6450 /* Make all global versions with definition. */
6451 for (t = info->version_info; t != NULL; t = t->next)
6452 for (d = t->globals.list; d != NULL; d = d->next)
6453 if (!d->symver && d->literal)
6454 {
6455 const char *verstr, *name;
6456 size_t namelen, verlen, newlen;
6457 char *newname, *p, leading_char;
6458 struct elf_link_hash_entry *newh;
6459
6460 leading_char = bfd_get_symbol_leading_char (output_bfd);
6461 name = d->pattern;
6462 namelen = strlen (name) + (leading_char != '\0');
6463 verstr = t->name;
6464 verlen = strlen (verstr);
6465 newlen = namelen + verlen + 3;
6466
6467 newname = (char *) bfd_malloc (newlen);
6468 if (newname == NULL)
6469 return FALSE;
6470 newname[0] = leading_char;
6471 memcpy (newname + (leading_char != '\0'), name, namelen);
6472
6473 /* Check the hidden versioned definition. */
6474 p = newname + namelen;
6475 *p++ = ELF_VER_CHR;
6476 memcpy (p, verstr, verlen + 1);
6477 newh = elf_link_hash_lookup (elf_hash_table (info),
6478 newname, FALSE, FALSE,
6479 FALSE);
6480 if (newh == NULL
6481 || (newh->root.type != bfd_link_hash_defined
6482 && newh->root.type != bfd_link_hash_defweak))
6483 {
6484 /* Check the default versioned definition. */
6485 *p++ = ELF_VER_CHR;
6486 memcpy (p, verstr, verlen + 1);
6487 newh = elf_link_hash_lookup (elf_hash_table (info),
6488 newname, FALSE, FALSE,
6489 FALSE);
6490 }
6491 free (newname);
6492
6493 /* Mark this version if there is a definition and it is
6494 not defined in a shared object. */
6495 if (newh != NULL
6496 && !newh->def_dynamic
6497 && (newh->root.type == bfd_link_hash_defined
6498 || newh->root.type == bfd_link_hash_defweak))
6499 d->symver = 1;
6500 }
6501
6502 /* Attach all the symbols to their version information. */
6503 asvinfo.info = info;
6504 asvinfo.failed = FALSE;
6505
6506 elf_link_hash_traverse (elf_hash_table (info),
6507 _bfd_elf_link_assign_sym_version,
6508 &asvinfo);
6509 if (asvinfo.failed)
6510 return FALSE;
6511
6512 if (!info->allow_undefined_version)
6513 {
6514 /* Check if all global versions have a definition. */
6515 bfd_boolean all_defined = TRUE;
6516 for (t = info->version_info; t != NULL; t = t->next)
6517 for (d = t->globals.list; d != NULL; d = d->next)
6518 if (d->literal && !d->symver && !d->script)
6519 {
6520 _bfd_error_handler
6521 (_("%s: undefined version: %s"),
6522 d->pattern, t->name);
6523 all_defined = FALSE;
6524 }
6525
6526 if (!all_defined)
6527 {
6528 bfd_set_error (bfd_error_bad_value);
6529 return FALSE;
6530 }
6531 }
6532
6533 /* Set up the version definition section. */
6534 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6535 BFD_ASSERT (s != NULL);
6536
6537 /* We may have created additional version definitions if we are
6538 just linking a regular application. */
6539 verdefs = info->version_info;
6540
6541 /* Skip anonymous version tag. */
6542 if (verdefs != NULL && verdefs->vernum == 0)
6543 verdefs = verdefs->next;
6544
6545 if (verdefs == NULL && !info->create_default_symver)
6546 s->flags |= SEC_EXCLUDE;
6547 else
6548 {
6549 unsigned int cdefs;
6550 bfd_size_type size;
6551 bfd_byte *p;
6552 Elf_Internal_Verdef def;
6553 Elf_Internal_Verdaux defaux;
6554 struct bfd_link_hash_entry *bh;
6555 struct elf_link_hash_entry *h;
6556 const char *name;
6557
6558 cdefs = 0;
6559 size = 0;
6560
6561 /* Make space for the base version. */
6562 size += sizeof (Elf_External_Verdef);
6563 size += sizeof (Elf_External_Verdaux);
6564 ++cdefs;
6565
6566 /* Make space for the default version. */
6567 if (info->create_default_symver)
6568 {
6569 size += sizeof (Elf_External_Verdef);
6570 ++cdefs;
6571 }
6572
6573 for (t = verdefs; t != NULL; t = t->next)
6574 {
6575 struct bfd_elf_version_deps *n;
6576
6577 /* Don't emit base version twice. */
6578 if (t->vernum == 0)
6579 continue;
6580
6581 size += sizeof (Elf_External_Verdef);
6582 size += sizeof (Elf_External_Verdaux);
6583 ++cdefs;
6584
6585 for (n = t->deps; n != NULL; n = n->next)
6586 size += sizeof (Elf_External_Verdaux);
6587 }
6588
6589 s->size = size;
6590 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6591 if (s->contents == NULL && s->size != 0)
6592 return FALSE;
6593
6594 /* Fill in the version definition section. */
6595
6596 p = s->contents;
6597
6598 def.vd_version = VER_DEF_CURRENT;
6599 def.vd_flags = VER_FLG_BASE;
6600 def.vd_ndx = 1;
6601 def.vd_cnt = 1;
6602 if (info->create_default_symver)
6603 {
6604 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6605 def.vd_next = sizeof (Elf_External_Verdef);
6606 }
6607 else
6608 {
6609 def.vd_aux = sizeof (Elf_External_Verdef);
6610 def.vd_next = (sizeof (Elf_External_Verdef)
6611 + sizeof (Elf_External_Verdaux));
6612 }
6613
6614 if (soname_indx != (size_t) -1)
6615 {
6616 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6617 soname_indx);
6618 def.vd_hash = bfd_elf_hash (soname);
6619 defaux.vda_name = soname_indx;
6620 name = soname;
6621 }
6622 else
6623 {
6624 size_t indx;
6625
6626 name = lbasename (output_bfd->filename);
6627 def.vd_hash = bfd_elf_hash (name);
6628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6629 name, FALSE);
6630 if (indx == (size_t) -1)
6631 return FALSE;
6632 defaux.vda_name = indx;
6633 }
6634 defaux.vda_next = 0;
6635
6636 _bfd_elf_swap_verdef_out (output_bfd, &def,
6637 (Elf_External_Verdef *) p);
6638 p += sizeof (Elf_External_Verdef);
6639 if (info->create_default_symver)
6640 {
6641 /* Add a symbol representing this version. */
6642 bh = NULL;
6643 if (! (_bfd_generic_link_add_one_symbol
6644 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6645 0, NULL, FALSE,
6646 get_elf_backend_data (dynobj)->collect, &bh)))
6647 return FALSE;
6648 h = (struct elf_link_hash_entry *) bh;
6649 h->non_elf = 0;
6650 h->def_regular = 1;
6651 h->type = STT_OBJECT;
6652 h->verinfo.vertree = NULL;
6653
6654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6655 return FALSE;
6656
6657 /* Create a duplicate of the base version with the same
6658 aux block, but different flags. */
6659 def.vd_flags = 0;
6660 def.vd_ndx = 2;
6661 def.vd_aux = sizeof (Elf_External_Verdef);
6662 if (verdefs)
6663 def.vd_next = (sizeof (Elf_External_Verdef)
6664 + sizeof (Elf_External_Verdaux));
6665 else
6666 def.vd_next = 0;
6667 _bfd_elf_swap_verdef_out (output_bfd, &def,
6668 (Elf_External_Verdef *) p);
6669 p += sizeof (Elf_External_Verdef);
6670 }
6671 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6672 (Elf_External_Verdaux *) p);
6673 p += sizeof (Elf_External_Verdaux);
6674
6675 for (t = verdefs; t != NULL; t = t->next)
6676 {
6677 unsigned int cdeps;
6678 struct bfd_elf_version_deps *n;
6679
6680 /* Don't emit the base version twice. */
6681 if (t->vernum == 0)
6682 continue;
6683
6684 cdeps = 0;
6685 for (n = t->deps; n != NULL; n = n->next)
6686 ++cdeps;
6687
6688 /* Add a symbol representing this version. */
6689 bh = NULL;
6690 if (! (_bfd_generic_link_add_one_symbol
6691 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6692 0, NULL, FALSE,
6693 get_elf_backend_data (dynobj)->collect, &bh)))
6694 return FALSE;
6695 h = (struct elf_link_hash_entry *) bh;
6696 h->non_elf = 0;
6697 h->def_regular = 1;
6698 h->type = STT_OBJECT;
6699 h->verinfo.vertree = t;
6700
6701 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6702 return FALSE;
6703
6704 def.vd_version = VER_DEF_CURRENT;
6705 def.vd_flags = 0;
6706 if (t->globals.list == NULL
6707 && t->locals.list == NULL
6708 && ! t->used)
6709 def.vd_flags |= VER_FLG_WEAK;
6710 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6711 def.vd_cnt = cdeps + 1;
6712 def.vd_hash = bfd_elf_hash (t->name);
6713 def.vd_aux = sizeof (Elf_External_Verdef);
6714 def.vd_next = 0;
6715
6716 /* If a basever node is next, it *must* be the last node in
6717 the chain, otherwise Verdef construction breaks. */
6718 if (t->next != NULL && t->next->vernum == 0)
6719 BFD_ASSERT (t->next->next == NULL);
6720
6721 if (t->next != NULL && t->next->vernum != 0)
6722 def.vd_next = (sizeof (Elf_External_Verdef)
6723 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6724
6725 _bfd_elf_swap_verdef_out (output_bfd, &def,
6726 (Elf_External_Verdef *) p);
6727 p += sizeof (Elf_External_Verdef);
6728
6729 defaux.vda_name = h->dynstr_index;
6730 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6731 h->dynstr_index);
6732 defaux.vda_next = 0;
6733 if (t->deps != NULL)
6734 defaux.vda_next = sizeof (Elf_External_Verdaux);
6735 t->name_indx = defaux.vda_name;
6736
6737 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6738 (Elf_External_Verdaux *) p);
6739 p += sizeof (Elf_External_Verdaux);
6740
6741 for (n = t->deps; n != NULL; n = n->next)
6742 {
6743 if (n->version_needed == NULL)
6744 {
6745 /* This can happen if there was an error in the
6746 version script. */
6747 defaux.vda_name = 0;
6748 }
6749 else
6750 {
6751 defaux.vda_name = n->version_needed->name_indx;
6752 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6753 defaux.vda_name);
6754 }
6755 if (n->next == NULL)
6756 defaux.vda_next = 0;
6757 else
6758 defaux.vda_next = sizeof (Elf_External_Verdaux);
6759
6760 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6761 (Elf_External_Verdaux *) p);
6762 p += sizeof (Elf_External_Verdaux);
6763 }
6764 }
6765
6766 elf_tdata (output_bfd)->cverdefs = cdefs;
6767 }
6768 }
6769
6770 bed = get_elf_backend_data (output_bfd);
6771
6772 if (info->gc_sections && bed->can_gc_sections)
6773 {
6774 struct elf_gc_sweep_symbol_info sweep_info;
6775
6776 /* Remove the symbols that were in the swept sections from the
6777 dynamic symbol table. */
6778 sweep_info.info = info;
6779 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6780 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6781 &sweep_info);
6782 }
6783
6784 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6785 {
6786 asection *s;
6787 struct elf_find_verdep_info sinfo;
6788
6789 /* Work out the size of the version reference section. */
6790
6791 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6792 BFD_ASSERT (s != NULL);
6793
6794 sinfo.info = info;
6795 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6796 if (sinfo.vers == 0)
6797 sinfo.vers = 1;
6798 sinfo.failed = FALSE;
6799
6800 elf_link_hash_traverse (elf_hash_table (info),
6801 _bfd_elf_link_find_version_dependencies,
6802 &sinfo);
6803 if (sinfo.failed)
6804 return FALSE;
6805
6806 if (elf_tdata (output_bfd)->verref == NULL)
6807 s->flags |= SEC_EXCLUDE;
6808 else
6809 {
6810 Elf_Internal_Verneed *vn;
6811 unsigned int size;
6812 unsigned int crefs;
6813 bfd_byte *p;
6814
6815 /* Build the version dependency section. */
6816 size = 0;
6817 crefs = 0;
6818 for (vn = elf_tdata (output_bfd)->verref;
6819 vn != NULL;
6820 vn = vn->vn_nextref)
6821 {
6822 Elf_Internal_Vernaux *a;
6823
6824 size += sizeof (Elf_External_Verneed);
6825 ++crefs;
6826 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6827 size += sizeof (Elf_External_Vernaux);
6828 }
6829
6830 s->size = size;
6831 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6832 if (s->contents == NULL)
6833 return FALSE;
6834
6835 p = s->contents;
6836 for (vn = elf_tdata (output_bfd)->verref;
6837 vn != NULL;
6838 vn = vn->vn_nextref)
6839 {
6840 unsigned int caux;
6841 Elf_Internal_Vernaux *a;
6842 size_t indx;
6843
6844 caux = 0;
6845 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6846 ++caux;
6847
6848 vn->vn_version = VER_NEED_CURRENT;
6849 vn->vn_cnt = caux;
6850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6851 elf_dt_name (vn->vn_bfd) != NULL
6852 ? elf_dt_name (vn->vn_bfd)
6853 : lbasename (vn->vn_bfd->filename),
6854 FALSE);
6855 if (indx == (size_t) -1)
6856 return FALSE;
6857 vn->vn_file = indx;
6858 vn->vn_aux = sizeof (Elf_External_Verneed);
6859 if (vn->vn_nextref == NULL)
6860 vn->vn_next = 0;
6861 else
6862 vn->vn_next = (sizeof (Elf_External_Verneed)
6863 + caux * sizeof (Elf_External_Vernaux));
6864
6865 _bfd_elf_swap_verneed_out (output_bfd, vn,
6866 (Elf_External_Verneed *) p);
6867 p += sizeof (Elf_External_Verneed);
6868
6869 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6870 {
6871 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6872 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6873 a->vna_nodename, FALSE);
6874 if (indx == (size_t) -1)
6875 return FALSE;
6876 a->vna_name = indx;
6877 if (a->vna_nextptr == NULL)
6878 a->vna_next = 0;
6879 else
6880 a->vna_next = sizeof (Elf_External_Vernaux);
6881
6882 _bfd_elf_swap_vernaux_out (output_bfd, a,
6883 (Elf_External_Vernaux *) p);
6884 p += sizeof (Elf_External_Vernaux);
6885 }
6886 }
6887
6888 elf_tdata (output_bfd)->cverrefs = crefs;
6889 }
6890 }
6891
6892 /* Any syms created from now on start with -1 in
6893 got.refcount/offset and plt.refcount/offset. */
6894 elf_hash_table (info)->init_got_refcount
6895 = elf_hash_table (info)->init_got_offset;
6896 elf_hash_table (info)->init_plt_refcount
6897 = elf_hash_table (info)->init_plt_offset;
6898
6899 if (bfd_link_relocatable (info)
6900 && !_bfd_elf_size_group_sections (info))
6901 return FALSE;
6902
6903 /* The backend may have to create some sections regardless of whether
6904 we're dynamic or not. */
6905 if (bed->elf_backend_always_size_sections
6906 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6907 return FALSE;
6908
6909 /* Determine any GNU_STACK segment requirements, after the backend
6910 has had a chance to set a default segment size. */
6911 if (info->execstack)
6912 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6913 else if (info->noexecstack)
6914 elf_stack_flags (output_bfd) = PF_R | PF_W;
6915 else
6916 {
6917 bfd *inputobj;
6918 asection *notesec = NULL;
6919 int exec = 0;
6920
6921 for (inputobj = info->input_bfds;
6922 inputobj;
6923 inputobj = inputobj->link.next)
6924 {
6925 asection *s;
6926
6927 if (inputobj->flags
6928 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6929 continue;
6930 s = inputobj->sections;
6931 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6932 continue;
6933
6934 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6935 if (s)
6936 {
6937 if (s->flags & SEC_CODE)
6938 exec = PF_X;
6939 notesec = s;
6940 }
6941 else if (bed->default_execstack)
6942 exec = PF_X;
6943 }
6944 if (notesec || info->stacksize > 0)
6945 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6946 if (notesec && exec && bfd_link_relocatable (info)
6947 && notesec->output_section != bfd_abs_section_ptr)
6948 notesec->output_section->flags |= SEC_CODE;
6949 }
6950
6951 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6952 {
6953 struct elf_info_failed eif;
6954 struct elf_link_hash_entry *h;
6955 asection *dynstr;
6956 asection *s;
6957
6958 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6959 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6960
6961 if (info->symbolic)
6962 {
6963 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6964 return FALSE;
6965 info->flags |= DF_SYMBOLIC;
6966 }
6967
6968 if (rpath != NULL)
6969 {
6970 size_t indx;
6971 bfd_vma tag;
6972
6973 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6974 TRUE);
6975 if (indx == (size_t) -1)
6976 return FALSE;
6977
6978 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6979 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6980 return FALSE;
6981 }
6982
6983 if (filter_shlib != NULL)
6984 {
6985 size_t indx;
6986
6987 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6988 filter_shlib, TRUE);
6989 if (indx == (size_t) -1
6990 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6991 return FALSE;
6992 }
6993
6994 if (auxiliary_filters != NULL)
6995 {
6996 const char * const *p;
6997
6998 for (p = auxiliary_filters; *p != NULL; p++)
6999 {
7000 size_t indx;
7001
7002 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7003 *p, TRUE);
7004 if (indx == (size_t) -1
7005 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7006 return FALSE;
7007 }
7008 }
7009
7010 if (audit != NULL)
7011 {
7012 size_t indx;
7013
7014 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7015 TRUE);
7016 if (indx == (size_t) -1
7017 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7018 return FALSE;
7019 }
7020
7021 if (depaudit != NULL)
7022 {
7023 size_t indx;
7024
7025 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7026 TRUE);
7027 if (indx == (size_t) -1
7028 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7029 return FALSE;
7030 }
7031
7032 eif.info = info;
7033 eif.failed = FALSE;
7034
7035 /* Find all symbols which were defined in a dynamic object and make
7036 the backend pick a reasonable value for them. */
7037 elf_link_hash_traverse (elf_hash_table (info),
7038 _bfd_elf_adjust_dynamic_symbol,
7039 &eif);
7040 if (eif.failed)
7041 return FALSE;
7042
7043 /* Add some entries to the .dynamic section. We fill in some of the
7044 values later, in bfd_elf_final_link, but we must add the entries
7045 now so that we know the final size of the .dynamic section. */
7046
7047 /* If there are initialization and/or finalization functions to
7048 call then add the corresponding DT_INIT/DT_FINI entries. */
7049 h = (info->init_function
7050 ? elf_link_hash_lookup (elf_hash_table (info),
7051 info->init_function, FALSE,
7052 FALSE, FALSE)
7053 : NULL);
7054 if (h != NULL
7055 && (h->ref_regular
7056 || h->def_regular))
7057 {
7058 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7059 return FALSE;
7060 }
7061 h = (info->fini_function
7062 ? elf_link_hash_lookup (elf_hash_table (info),
7063 info->fini_function, FALSE,
7064 FALSE, FALSE)
7065 : NULL);
7066 if (h != NULL
7067 && (h->ref_regular
7068 || h->def_regular))
7069 {
7070 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7071 return FALSE;
7072 }
7073
7074 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7075 if (s != NULL && s->linker_has_input)
7076 {
7077 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7078 if (! bfd_link_executable (info))
7079 {
7080 bfd *sub;
7081 asection *o;
7082
7083 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7084 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7085 && (o = sub->sections) != NULL
7086 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7087 for (o = sub->sections; o != NULL; o = o->next)
7088 if (elf_section_data (o)->this_hdr.sh_type
7089 == SHT_PREINIT_ARRAY)
7090 {
7091 _bfd_error_handler
7092 (_("%pB: .preinit_array section is not allowed in DSO"),
7093 sub);
7094 break;
7095 }
7096
7097 bfd_set_error (bfd_error_nonrepresentable_section);
7098 return FALSE;
7099 }
7100
7101 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7102 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7103 return FALSE;
7104 }
7105 s = bfd_get_section_by_name (output_bfd, ".init_array");
7106 if (s != NULL && s->linker_has_input)
7107 {
7108 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7109 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7110 return FALSE;
7111 }
7112 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7113 if (s != NULL && s->linker_has_input)
7114 {
7115 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7116 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7117 return FALSE;
7118 }
7119
7120 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7121 /* If .dynstr is excluded from the link, we don't want any of
7122 these tags. Strictly, we should be checking each section
7123 individually; This quick check covers for the case where
7124 someone does a /DISCARD/ : { *(*) }. */
7125 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7126 {
7127 bfd_size_type strsize;
7128
7129 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7130 if ((info->emit_hash
7131 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7132 || (info->emit_gnu_hash
7133 && (bed->record_xhash_symbol == NULL
7134 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7135 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7136 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7137 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7138 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7139 bed->s->sizeof_sym))
7140 return FALSE;
7141 }
7142 }
7143
7144 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7145 return FALSE;
7146
7147 /* The backend must work out the sizes of all the other dynamic
7148 sections. */
7149 if (dynobj != NULL
7150 && bed->elf_backend_size_dynamic_sections != NULL
7151 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7152 return FALSE;
7153
7154 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7155 {
7156 if (elf_tdata (output_bfd)->cverdefs)
7157 {
7158 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7159
7160 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7161 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7162 return FALSE;
7163 }
7164
7165 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7166 {
7167 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7168 return FALSE;
7169 }
7170 else if (info->flags & DF_BIND_NOW)
7171 {
7172 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7173 return FALSE;
7174 }
7175
7176 if (info->flags_1)
7177 {
7178 if (bfd_link_executable (info))
7179 info->flags_1 &= ~ (DF_1_INITFIRST
7180 | DF_1_NODELETE
7181 | DF_1_NOOPEN);
7182 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7183 return FALSE;
7184 }
7185
7186 if (elf_tdata (output_bfd)->cverrefs)
7187 {
7188 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7189
7190 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7191 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7192 return FALSE;
7193 }
7194
7195 if ((elf_tdata (output_bfd)->cverrefs == 0
7196 && elf_tdata (output_bfd)->cverdefs == 0)
7197 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7198 {
7199 asection *s;
7200
7201 s = bfd_get_linker_section (dynobj, ".gnu.version");
7202 s->flags |= SEC_EXCLUDE;
7203 }
7204 }
7205 return TRUE;
7206 }
7207
7208 /* Find the first non-excluded output section. We'll use its
7209 section symbol for some emitted relocs. */
7210 void
7211 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7212 {
7213 asection *s;
7214 asection *found = NULL;
7215
7216 for (s = output_bfd->sections; s != NULL; s = s->next)
7217 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7218 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7219 {
7220 found = s;
7221 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7222 break;
7223 }
7224 elf_hash_table (info)->text_index_section = found;
7225 }
7226
7227 /* Find two non-excluded output sections, one for code, one for data.
7228 We'll use their section symbols for some emitted relocs. */
7229 void
7230 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7231 {
7232 asection *s;
7233 asection *found = NULL;
7234
7235 /* Data first, since setting text_index_section changes
7236 _bfd_elf_omit_section_dynsym_default. */
7237 for (s = output_bfd->sections; s != NULL; s = s->next)
7238 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7239 && !(s->flags & SEC_READONLY)
7240 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7241 {
7242 found = s;
7243 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7244 break;
7245 }
7246 elf_hash_table (info)->data_index_section = found;
7247
7248 for (s = output_bfd->sections; s != NULL; s = s->next)
7249 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7250 && (s->flags & SEC_READONLY)
7251 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7252 {
7253 found = s;
7254 break;
7255 }
7256 elf_hash_table (info)->text_index_section = found;
7257 }
7258
7259 #define GNU_HASH_SECTION_NAME(bed) \
7260 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7261
7262 bfd_boolean
7263 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7264 {
7265 const struct elf_backend_data *bed;
7266 unsigned long section_sym_count;
7267 bfd_size_type dynsymcount = 0;
7268
7269 if (!is_elf_hash_table (info->hash))
7270 return TRUE;
7271
7272 bed = get_elf_backend_data (output_bfd);
7273 (*bed->elf_backend_init_index_section) (output_bfd, info);
7274
7275 /* Assign dynsym indices. In a shared library we generate a section
7276 symbol for each output section, which come first. Next come all
7277 of the back-end allocated local dynamic syms, followed by the rest
7278 of the global symbols.
7279
7280 This is usually not needed for static binaries, however backends
7281 can request to always do it, e.g. the MIPS backend uses dynamic
7282 symbol counts to lay out GOT, which will be produced in the
7283 presence of GOT relocations even in static binaries (holding fixed
7284 data in that case, to satisfy those relocations). */
7285
7286 if (elf_hash_table (info)->dynamic_sections_created
7287 || bed->always_renumber_dynsyms)
7288 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7289 &section_sym_count);
7290
7291 if (elf_hash_table (info)->dynamic_sections_created)
7292 {
7293 bfd *dynobj;
7294 asection *s;
7295 unsigned int dtagcount;
7296
7297 dynobj = elf_hash_table (info)->dynobj;
7298
7299 /* Work out the size of the symbol version section. */
7300 s = bfd_get_linker_section (dynobj, ".gnu.version");
7301 BFD_ASSERT (s != NULL);
7302 if ((s->flags & SEC_EXCLUDE) == 0)
7303 {
7304 s->size = dynsymcount * sizeof (Elf_External_Versym);
7305 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7306 if (s->contents == NULL)
7307 return FALSE;
7308
7309 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7310 return FALSE;
7311 }
7312
7313 /* Set the size of the .dynsym and .hash sections. We counted
7314 the number of dynamic symbols in elf_link_add_object_symbols.
7315 We will build the contents of .dynsym and .hash when we build
7316 the final symbol table, because until then we do not know the
7317 correct value to give the symbols. We built the .dynstr
7318 section as we went along in elf_link_add_object_symbols. */
7319 s = elf_hash_table (info)->dynsym;
7320 BFD_ASSERT (s != NULL);
7321 s->size = dynsymcount * bed->s->sizeof_sym;
7322
7323 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7324 if (s->contents == NULL)
7325 return FALSE;
7326
7327 /* The first entry in .dynsym is a dummy symbol. Clear all the
7328 section syms, in case we don't output them all. */
7329 ++section_sym_count;
7330 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7331
7332 elf_hash_table (info)->bucketcount = 0;
7333
7334 /* Compute the size of the hashing table. As a side effect this
7335 computes the hash values for all the names we export. */
7336 if (info->emit_hash)
7337 {
7338 unsigned long int *hashcodes;
7339 struct hash_codes_info hashinf;
7340 bfd_size_type amt;
7341 unsigned long int nsyms;
7342 size_t bucketcount;
7343 size_t hash_entry_size;
7344
7345 /* Compute the hash values for all exported symbols. At the same
7346 time store the values in an array so that we could use them for
7347 optimizations. */
7348 amt = dynsymcount * sizeof (unsigned long int);
7349 hashcodes = (unsigned long int *) bfd_malloc (amt);
7350 if (hashcodes == NULL)
7351 return FALSE;
7352 hashinf.hashcodes = hashcodes;
7353 hashinf.error = FALSE;
7354
7355 /* Put all hash values in HASHCODES. */
7356 elf_link_hash_traverse (elf_hash_table (info),
7357 elf_collect_hash_codes, &hashinf);
7358 if (hashinf.error)
7359 {
7360 free (hashcodes);
7361 return FALSE;
7362 }
7363
7364 nsyms = hashinf.hashcodes - hashcodes;
7365 bucketcount
7366 = compute_bucket_count (info, hashcodes, nsyms, 0);
7367 free (hashcodes);
7368
7369 if (bucketcount == 0 && nsyms > 0)
7370 return FALSE;
7371
7372 elf_hash_table (info)->bucketcount = bucketcount;
7373
7374 s = bfd_get_linker_section (dynobj, ".hash");
7375 BFD_ASSERT (s != NULL);
7376 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7377 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7378 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7379 if (s->contents == NULL)
7380 return FALSE;
7381
7382 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7383 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7384 s->contents + hash_entry_size);
7385 }
7386
7387 if (info->emit_gnu_hash)
7388 {
7389 size_t i, cnt;
7390 unsigned char *contents;
7391 struct collect_gnu_hash_codes cinfo;
7392 bfd_size_type amt;
7393 size_t bucketcount;
7394
7395 memset (&cinfo, 0, sizeof (cinfo));
7396
7397 /* Compute the hash values for all exported symbols. At the same
7398 time store the values in an array so that we could use them for
7399 optimizations. */
7400 amt = dynsymcount * 2 * sizeof (unsigned long int);
7401 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7402 if (cinfo.hashcodes == NULL)
7403 return FALSE;
7404
7405 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7406 cinfo.min_dynindx = -1;
7407 cinfo.output_bfd = output_bfd;
7408 cinfo.bed = bed;
7409
7410 /* Put all hash values in HASHCODES. */
7411 elf_link_hash_traverse (elf_hash_table (info),
7412 elf_collect_gnu_hash_codes, &cinfo);
7413 if (cinfo.error)
7414 {
7415 free (cinfo.hashcodes);
7416 return FALSE;
7417 }
7418
7419 bucketcount
7420 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7421
7422 if (bucketcount == 0)
7423 {
7424 free (cinfo.hashcodes);
7425 return FALSE;
7426 }
7427
7428 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7429 BFD_ASSERT (s != NULL);
7430
7431 if (cinfo.nsyms == 0)
7432 {
7433 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7434 BFD_ASSERT (cinfo.min_dynindx == -1);
7435 free (cinfo.hashcodes);
7436 s->size = 5 * 4 + bed->s->arch_size / 8;
7437 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7438 if (contents == NULL)
7439 return FALSE;
7440 s->contents = contents;
7441 /* 1 empty bucket. */
7442 bfd_put_32 (output_bfd, 1, contents);
7443 /* SYMIDX above the special symbol 0. */
7444 bfd_put_32 (output_bfd, 1, contents + 4);
7445 /* Just one word for bitmask. */
7446 bfd_put_32 (output_bfd, 1, contents + 8);
7447 /* Only hash fn bloom filter. */
7448 bfd_put_32 (output_bfd, 0, contents + 12);
7449 /* No hashes are valid - empty bitmask. */
7450 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7451 /* No hashes in the only bucket. */
7452 bfd_put_32 (output_bfd, 0,
7453 contents + 16 + bed->s->arch_size / 8);
7454 }
7455 else
7456 {
7457 unsigned long int maskwords, maskbitslog2, x;
7458 BFD_ASSERT (cinfo.min_dynindx != -1);
7459
7460 x = cinfo.nsyms;
7461 maskbitslog2 = 1;
7462 while ((x >>= 1) != 0)
7463 ++maskbitslog2;
7464 if (maskbitslog2 < 3)
7465 maskbitslog2 = 5;
7466 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7467 maskbitslog2 = maskbitslog2 + 3;
7468 else
7469 maskbitslog2 = maskbitslog2 + 2;
7470 if (bed->s->arch_size == 64)
7471 {
7472 if (maskbitslog2 == 5)
7473 maskbitslog2 = 6;
7474 cinfo.shift1 = 6;
7475 }
7476 else
7477 cinfo.shift1 = 5;
7478 cinfo.mask = (1 << cinfo.shift1) - 1;
7479 cinfo.shift2 = maskbitslog2;
7480 cinfo.maskbits = 1 << maskbitslog2;
7481 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7482 amt = bucketcount * sizeof (unsigned long int) * 2;
7483 amt += maskwords * sizeof (bfd_vma);
7484 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7485 if (cinfo.bitmask == NULL)
7486 {
7487 free (cinfo.hashcodes);
7488 return FALSE;
7489 }
7490
7491 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7492 cinfo.indx = cinfo.counts + bucketcount;
7493 cinfo.symindx = dynsymcount - cinfo.nsyms;
7494 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7495
7496 /* Determine how often each hash bucket is used. */
7497 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7498 for (i = 0; i < cinfo.nsyms; ++i)
7499 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7500
7501 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7502 if (cinfo.counts[i] != 0)
7503 {
7504 cinfo.indx[i] = cnt;
7505 cnt += cinfo.counts[i];
7506 }
7507 BFD_ASSERT (cnt == dynsymcount);
7508 cinfo.bucketcount = bucketcount;
7509 cinfo.local_indx = cinfo.min_dynindx;
7510
7511 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7512 s->size += cinfo.maskbits / 8;
7513 if (bed->record_xhash_symbol != NULL)
7514 s->size += cinfo.nsyms * 4;
7515 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7516 if (contents == NULL)
7517 {
7518 free (cinfo.bitmask);
7519 free (cinfo.hashcodes);
7520 return FALSE;
7521 }
7522
7523 s->contents = contents;
7524 bfd_put_32 (output_bfd, bucketcount, contents);
7525 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7526 bfd_put_32 (output_bfd, maskwords, contents + 8);
7527 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7528 contents += 16 + cinfo.maskbits / 8;
7529
7530 for (i = 0; i < bucketcount; ++i)
7531 {
7532 if (cinfo.counts[i] == 0)
7533 bfd_put_32 (output_bfd, 0, contents);
7534 else
7535 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7536 contents += 4;
7537 }
7538
7539 cinfo.contents = contents;
7540
7541 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7542 /* Renumber dynamic symbols, if populating .gnu.hash section.
7543 If using .MIPS.xhash, populate the translation table. */
7544 elf_link_hash_traverse (elf_hash_table (info),
7545 elf_gnu_hash_process_symidx, &cinfo);
7546
7547 contents = s->contents + 16;
7548 for (i = 0; i < maskwords; ++i)
7549 {
7550 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7551 contents);
7552 contents += bed->s->arch_size / 8;
7553 }
7554
7555 free (cinfo.bitmask);
7556 free (cinfo.hashcodes);
7557 }
7558 }
7559
7560 s = bfd_get_linker_section (dynobj, ".dynstr");
7561 BFD_ASSERT (s != NULL);
7562
7563 elf_finalize_dynstr (output_bfd, info);
7564
7565 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7566
7567 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7568 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7569 return FALSE;
7570 }
7571
7572 return TRUE;
7573 }
7574 \f
7575 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7576
7577 static void
7578 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7579 asection *sec)
7580 {
7581 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7582 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7583 }
7584
7585 /* Finish SHF_MERGE section merging. */
7586
7587 bfd_boolean
7588 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7589 {
7590 bfd *ibfd;
7591 asection *sec;
7592
7593 if (!is_elf_hash_table (info->hash))
7594 return FALSE;
7595
7596 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7597 if ((ibfd->flags & DYNAMIC) == 0
7598 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7599 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7600 == get_elf_backend_data (obfd)->s->elfclass))
7601 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7602 if ((sec->flags & SEC_MERGE) != 0
7603 && !bfd_is_abs_section (sec->output_section))
7604 {
7605 struct bfd_elf_section_data *secdata;
7606
7607 secdata = elf_section_data (sec);
7608 if (! _bfd_add_merge_section (obfd,
7609 &elf_hash_table (info)->merge_info,
7610 sec, &secdata->sec_info))
7611 return FALSE;
7612 else if (secdata->sec_info)
7613 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7614 }
7615
7616 if (elf_hash_table (info)->merge_info != NULL)
7617 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7618 merge_sections_remove_hook);
7619 return TRUE;
7620 }
7621
7622 /* Create an entry in an ELF linker hash table. */
7623
7624 struct bfd_hash_entry *
7625 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7626 struct bfd_hash_table *table,
7627 const char *string)
7628 {
7629 /* Allocate the structure if it has not already been allocated by a
7630 subclass. */
7631 if (entry == NULL)
7632 {
7633 entry = (struct bfd_hash_entry *)
7634 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7635 if (entry == NULL)
7636 return entry;
7637 }
7638
7639 /* Call the allocation method of the superclass. */
7640 entry = _bfd_link_hash_newfunc (entry, table, string);
7641 if (entry != NULL)
7642 {
7643 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7644 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7645
7646 /* Set local fields. */
7647 ret->indx = -1;
7648 ret->dynindx = -1;
7649 ret->got = htab->init_got_refcount;
7650 ret->plt = htab->init_plt_refcount;
7651 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7652 - offsetof (struct elf_link_hash_entry, size)));
7653 /* Assume that we have been called by a non-ELF symbol reader.
7654 This flag is then reset by the code which reads an ELF input
7655 file. This ensures that a symbol created by a non-ELF symbol
7656 reader will have the flag set correctly. */
7657 ret->non_elf = 1;
7658 }
7659
7660 return entry;
7661 }
7662
7663 /* Copy data from an indirect symbol to its direct symbol, hiding the
7664 old indirect symbol. Also used for copying flags to a weakdef. */
7665
7666 void
7667 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7668 struct elf_link_hash_entry *dir,
7669 struct elf_link_hash_entry *ind)
7670 {
7671 struct elf_link_hash_table *htab;
7672
7673 /* Copy down any references that we may have already seen to the
7674 symbol which just became indirect. */
7675
7676 if (dir->versioned != versioned_hidden)
7677 dir->ref_dynamic |= ind->ref_dynamic;
7678 dir->ref_regular |= ind->ref_regular;
7679 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7680 dir->non_got_ref |= ind->non_got_ref;
7681 dir->needs_plt |= ind->needs_plt;
7682 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7683
7684 if (ind->root.type != bfd_link_hash_indirect)
7685 return;
7686
7687 /* Copy over the global and procedure linkage table refcount entries.
7688 These may have been already set up by a check_relocs routine. */
7689 htab = elf_hash_table (info);
7690 if (ind->got.refcount > htab->init_got_refcount.refcount)
7691 {
7692 if (dir->got.refcount < 0)
7693 dir->got.refcount = 0;
7694 dir->got.refcount += ind->got.refcount;
7695 ind->got.refcount = htab->init_got_refcount.refcount;
7696 }
7697
7698 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7699 {
7700 if (dir->plt.refcount < 0)
7701 dir->plt.refcount = 0;
7702 dir->plt.refcount += ind->plt.refcount;
7703 ind->plt.refcount = htab->init_plt_refcount.refcount;
7704 }
7705
7706 if (ind->dynindx != -1)
7707 {
7708 if (dir->dynindx != -1)
7709 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7710 dir->dynindx = ind->dynindx;
7711 dir->dynstr_index = ind->dynstr_index;
7712 ind->dynindx = -1;
7713 ind->dynstr_index = 0;
7714 }
7715 }
7716
7717 void
7718 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7719 struct elf_link_hash_entry *h,
7720 bfd_boolean force_local)
7721 {
7722 /* STT_GNU_IFUNC symbol must go through PLT. */
7723 if (h->type != STT_GNU_IFUNC)
7724 {
7725 h->plt = elf_hash_table (info)->init_plt_offset;
7726 h->needs_plt = 0;
7727 }
7728 if (force_local)
7729 {
7730 h->forced_local = 1;
7731 if (h->dynindx != -1)
7732 {
7733 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7734 h->dynstr_index);
7735 h->dynindx = -1;
7736 h->dynstr_index = 0;
7737 }
7738 }
7739 }
7740
7741 /* Hide a symbol. */
7742
7743 void
7744 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7745 struct bfd_link_info *info,
7746 struct bfd_link_hash_entry *h)
7747 {
7748 if (is_elf_hash_table (info->hash))
7749 {
7750 const struct elf_backend_data *bed
7751 = get_elf_backend_data (output_bfd);
7752 struct elf_link_hash_entry *eh
7753 = (struct elf_link_hash_entry *) h;
7754 bed->elf_backend_hide_symbol (info, eh, TRUE);
7755 eh->def_dynamic = 0;
7756 eh->ref_dynamic = 0;
7757 eh->dynamic_def = 0;
7758 }
7759 }
7760
7761 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7762 caller. */
7763
7764 bfd_boolean
7765 _bfd_elf_link_hash_table_init
7766 (struct elf_link_hash_table *table,
7767 bfd *abfd,
7768 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7769 struct bfd_hash_table *,
7770 const char *),
7771 unsigned int entsize,
7772 enum elf_target_id target_id)
7773 {
7774 bfd_boolean ret;
7775 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7776
7777 table->init_got_refcount.refcount = can_refcount - 1;
7778 table->init_plt_refcount.refcount = can_refcount - 1;
7779 table->init_got_offset.offset = -(bfd_vma) 1;
7780 table->init_plt_offset.offset = -(bfd_vma) 1;
7781 /* The first dynamic symbol is a dummy. */
7782 table->dynsymcount = 1;
7783
7784 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7785
7786 table->root.type = bfd_link_elf_hash_table;
7787 table->hash_table_id = target_id;
7788
7789 return ret;
7790 }
7791
7792 /* Create an ELF linker hash table. */
7793
7794 struct bfd_link_hash_table *
7795 _bfd_elf_link_hash_table_create (bfd *abfd)
7796 {
7797 struct elf_link_hash_table *ret;
7798 size_t amt = sizeof (struct elf_link_hash_table);
7799
7800 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7801 if (ret == NULL)
7802 return NULL;
7803
7804 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7805 sizeof (struct elf_link_hash_entry),
7806 GENERIC_ELF_DATA))
7807 {
7808 free (ret);
7809 return NULL;
7810 }
7811 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7812
7813 return &ret->root;
7814 }
7815
7816 /* Destroy an ELF linker hash table. */
7817
7818 void
7819 _bfd_elf_link_hash_table_free (bfd *obfd)
7820 {
7821 struct elf_link_hash_table *htab;
7822
7823 htab = (struct elf_link_hash_table *) obfd->link.hash;
7824 if (htab->dynstr != NULL)
7825 _bfd_elf_strtab_free (htab->dynstr);
7826 _bfd_merge_sections_free (htab->merge_info);
7827 _bfd_generic_link_hash_table_free (obfd);
7828 }
7829
7830 /* This is a hook for the ELF emulation code in the generic linker to
7831 tell the backend linker what file name to use for the DT_NEEDED
7832 entry for a dynamic object. */
7833
7834 void
7835 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7836 {
7837 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7838 && bfd_get_format (abfd) == bfd_object)
7839 elf_dt_name (abfd) = name;
7840 }
7841
7842 int
7843 bfd_elf_get_dyn_lib_class (bfd *abfd)
7844 {
7845 int lib_class;
7846 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7847 && bfd_get_format (abfd) == bfd_object)
7848 lib_class = elf_dyn_lib_class (abfd);
7849 else
7850 lib_class = 0;
7851 return lib_class;
7852 }
7853
7854 void
7855 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7856 {
7857 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7858 && bfd_get_format (abfd) == bfd_object)
7859 elf_dyn_lib_class (abfd) = lib_class;
7860 }
7861
7862 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7863 the linker ELF emulation code. */
7864
7865 struct bfd_link_needed_list *
7866 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7867 struct bfd_link_info *info)
7868 {
7869 if (! is_elf_hash_table (info->hash))
7870 return NULL;
7871 return elf_hash_table (info)->needed;
7872 }
7873
7874 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7875 hook for the linker ELF emulation code. */
7876
7877 struct bfd_link_needed_list *
7878 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7879 struct bfd_link_info *info)
7880 {
7881 if (! is_elf_hash_table (info->hash))
7882 return NULL;
7883 return elf_hash_table (info)->runpath;
7884 }
7885
7886 /* Get the name actually used for a dynamic object for a link. This
7887 is the SONAME entry if there is one. Otherwise, it is the string
7888 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7889
7890 const char *
7891 bfd_elf_get_dt_soname (bfd *abfd)
7892 {
7893 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7894 && bfd_get_format (abfd) == bfd_object)
7895 return elf_dt_name (abfd);
7896 return NULL;
7897 }
7898
7899 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7900 the ELF linker emulation code. */
7901
7902 bfd_boolean
7903 bfd_elf_get_bfd_needed_list (bfd *abfd,
7904 struct bfd_link_needed_list **pneeded)
7905 {
7906 asection *s;
7907 bfd_byte *dynbuf = NULL;
7908 unsigned int elfsec;
7909 unsigned long shlink;
7910 bfd_byte *extdyn, *extdynend;
7911 size_t extdynsize;
7912 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7913
7914 *pneeded = NULL;
7915
7916 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7917 || bfd_get_format (abfd) != bfd_object)
7918 return TRUE;
7919
7920 s = bfd_get_section_by_name (abfd, ".dynamic");
7921 if (s == NULL || s->size == 0)
7922 return TRUE;
7923
7924 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7925 goto error_return;
7926
7927 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7928 if (elfsec == SHN_BAD)
7929 goto error_return;
7930
7931 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7932
7933 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7934 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7935
7936 extdyn = dynbuf;
7937 extdynend = extdyn + s->size;
7938 for (; extdyn < extdynend; extdyn += extdynsize)
7939 {
7940 Elf_Internal_Dyn dyn;
7941
7942 (*swap_dyn_in) (abfd, extdyn, &dyn);
7943
7944 if (dyn.d_tag == DT_NULL)
7945 break;
7946
7947 if (dyn.d_tag == DT_NEEDED)
7948 {
7949 const char *string;
7950 struct bfd_link_needed_list *l;
7951 unsigned int tagv = dyn.d_un.d_val;
7952 size_t amt;
7953
7954 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7955 if (string == NULL)
7956 goto error_return;
7957
7958 amt = sizeof *l;
7959 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7960 if (l == NULL)
7961 goto error_return;
7962
7963 l->by = abfd;
7964 l->name = string;
7965 l->next = *pneeded;
7966 *pneeded = l;
7967 }
7968 }
7969
7970 free (dynbuf);
7971
7972 return TRUE;
7973
7974 error_return:
7975 if (dynbuf != NULL)
7976 free (dynbuf);
7977 return FALSE;
7978 }
7979
7980 struct elf_symbuf_symbol
7981 {
7982 unsigned long st_name; /* Symbol name, index in string tbl */
7983 unsigned char st_info; /* Type and binding attributes */
7984 unsigned char st_other; /* Visibilty, and target specific */
7985 };
7986
7987 struct elf_symbuf_head
7988 {
7989 struct elf_symbuf_symbol *ssym;
7990 size_t count;
7991 unsigned int st_shndx;
7992 };
7993
7994 struct elf_symbol
7995 {
7996 union
7997 {
7998 Elf_Internal_Sym *isym;
7999 struct elf_symbuf_symbol *ssym;
8000 void *p;
8001 } u;
8002 const char *name;
8003 };
8004
8005 /* Sort references to symbols by ascending section number. */
8006
8007 static int
8008 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8009 {
8010 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8011 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8012
8013 if (s1->st_shndx != s2->st_shndx)
8014 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8015 /* Final sort by the address of the sym in the symbuf ensures
8016 a stable sort. */
8017 if (s1 != s2)
8018 return s1 > s2 ? 1 : -1;
8019 return 0;
8020 }
8021
8022 static int
8023 elf_sym_name_compare (const void *arg1, const void *arg2)
8024 {
8025 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8026 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8027 int ret = strcmp (s1->name, s2->name);
8028 if (ret != 0)
8029 return ret;
8030 if (s1->u.p != s2->u.p)
8031 return s1->u.p > s2->u.p ? 1 : -1;
8032 return 0;
8033 }
8034
8035 static struct elf_symbuf_head *
8036 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8037 {
8038 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8039 struct elf_symbuf_symbol *ssym;
8040 struct elf_symbuf_head *ssymbuf, *ssymhead;
8041 size_t i, shndx_count, total_size, amt;
8042
8043 amt = symcount * sizeof (*indbuf);
8044 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8045 if (indbuf == NULL)
8046 return NULL;
8047
8048 for (ind = indbuf, i = 0; i < symcount; i++)
8049 if (isymbuf[i].st_shndx != SHN_UNDEF)
8050 *ind++ = &isymbuf[i];
8051 indbufend = ind;
8052
8053 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8054 elf_sort_elf_symbol);
8055
8056 shndx_count = 0;
8057 if (indbufend > indbuf)
8058 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8059 if (ind[0]->st_shndx != ind[1]->st_shndx)
8060 shndx_count++;
8061
8062 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8063 + (indbufend - indbuf) * sizeof (*ssym));
8064 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8065 if (ssymbuf == NULL)
8066 {
8067 free (indbuf);
8068 return NULL;
8069 }
8070
8071 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8072 ssymbuf->ssym = NULL;
8073 ssymbuf->count = shndx_count;
8074 ssymbuf->st_shndx = 0;
8075 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8076 {
8077 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8078 {
8079 ssymhead++;
8080 ssymhead->ssym = ssym;
8081 ssymhead->count = 0;
8082 ssymhead->st_shndx = (*ind)->st_shndx;
8083 }
8084 ssym->st_name = (*ind)->st_name;
8085 ssym->st_info = (*ind)->st_info;
8086 ssym->st_other = (*ind)->st_other;
8087 ssymhead->count++;
8088 }
8089 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8090 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8091 == total_size));
8092
8093 free (indbuf);
8094 return ssymbuf;
8095 }
8096
8097 /* Check if 2 sections define the same set of local and global
8098 symbols. */
8099
8100 static bfd_boolean
8101 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8102 struct bfd_link_info *info)
8103 {
8104 bfd *bfd1, *bfd2;
8105 const struct elf_backend_data *bed1, *bed2;
8106 Elf_Internal_Shdr *hdr1, *hdr2;
8107 size_t symcount1, symcount2;
8108 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8109 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8110 Elf_Internal_Sym *isym, *isymend;
8111 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8112 size_t count1, count2, i;
8113 unsigned int shndx1, shndx2;
8114 bfd_boolean result;
8115
8116 bfd1 = sec1->owner;
8117 bfd2 = sec2->owner;
8118
8119 /* Both sections have to be in ELF. */
8120 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8121 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8122 return FALSE;
8123
8124 if (elf_section_type (sec1) != elf_section_type (sec2))
8125 return FALSE;
8126
8127 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8128 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8129 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8130 return FALSE;
8131
8132 bed1 = get_elf_backend_data (bfd1);
8133 bed2 = get_elf_backend_data (bfd2);
8134 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8135 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8136 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8137 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8138
8139 if (symcount1 == 0 || symcount2 == 0)
8140 return FALSE;
8141
8142 result = FALSE;
8143 isymbuf1 = NULL;
8144 isymbuf2 = NULL;
8145 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8146 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8147
8148 if (ssymbuf1 == NULL)
8149 {
8150 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8151 NULL, NULL, NULL);
8152 if (isymbuf1 == NULL)
8153 goto done;
8154
8155 if (!info->reduce_memory_overheads)
8156 {
8157 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8158 elf_tdata (bfd1)->symbuf = ssymbuf1;
8159 }
8160 }
8161
8162 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8163 {
8164 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8165 NULL, NULL, NULL);
8166 if (isymbuf2 == NULL)
8167 goto done;
8168
8169 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8170 {
8171 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8172 elf_tdata (bfd2)->symbuf = ssymbuf2;
8173 }
8174 }
8175
8176 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8177 {
8178 /* Optimized faster version. */
8179 size_t lo, hi, mid;
8180 struct elf_symbol *symp;
8181 struct elf_symbuf_symbol *ssym, *ssymend;
8182
8183 lo = 0;
8184 hi = ssymbuf1->count;
8185 ssymbuf1++;
8186 count1 = 0;
8187 while (lo < hi)
8188 {
8189 mid = (lo + hi) / 2;
8190 if (shndx1 < ssymbuf1[mid].st_shndx)
8191 hi = mid;
8192 else if (shndx1 > ssymbuf1[mid].st_shndx)
8193 lo = mid + 1;
8194 else
8195 {
8196 count1 = ssymbuf1[mid].count;
8197 ssymbuf1 += mid;
8198 break;
8199 }
8200 }
8201
8202 lo = 0;
8203 hi = ssymbuf2->count;
8204 ssymbuf2++;
8205 count2 = 0;
8206 while (lo < hi)
8207 {
8208 mid = (lo + hi) / 2;
8209 if (shndx2 < ssymbuf2[mid].st_shndx)
8210 hi = mid;
8211 else if (shndx2 > ssymbuf2[mid].st_shndx)
8212 lo = mid + 1;
8213 else
8214 {
8215 count2 = ssymbuf2[mid].count;
8216 ssymbuf2 += mid;
8217 break;
8218 }
8219 }
8220
8221 if (count1 == 0 || count2 == 0 || count1 != count2)
8222 goto done;
8223
8224 symtable1
8225 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8226 symtable2
8227 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8228 if (symtable1 == NULL || symtable2 == NULL)
8229 goto done;
8230
8231 symp = symtable1;
8232 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8233 ssym < ssymend; ssym++, symp++)
8234 {
8235 symp->u.ssym = ssym;
8236 symp->name = bfd_elf_string_from_elf_section (bfd1,
8237 hdr1->sh_link,
8238 ssym->st_name);
8239 }
8240
8241 symp = symtable2;
8242 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8243 ssym < ssymend; ssym++, symp++)
8244 {
8245 symp->u.ssym = ssym;
8246 symp->name = bfd_elf_string_from_elf_section (bfd2,
8247 hdr2->sh_link,
8248 ssym->st_name);
8249 }
8250
8251 /* Sort symbol by name. */
8252 qsort (symtable1, count1, sizeof (struct elf_symbol),
8253 elf_sym_name_compare);
8254 qsort (symtable2, count1, sizeof (struct elf_symbol),
8255 elf_sym_name_compare);
8256
8257 for (i = 0; i < count1; i++)
8258 /* Two symbols must have the same binding, type and name. */
8259 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8260 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8261 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8262 goto done;
8263
8264 result = TRUE;
8265 goto done;
8266 }
8267
8268 symtable1 = (struct elf_symbol *)
8269 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8270 symtable2 = (struct elf_symbol *)
8271 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8272 if (symtable1 == NULL || symtable2 == NULL)
8273 goto done;
8274
8275 /* Count definitions in the section. */
8276 count1 = 0;
8277 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8278 if (isym->st_shndx == shndx1)
8279 symtable1[count1++].u.isym = isym;
8280
8281 count2 = 0;
8282 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8283 if (isym->st_shndx == shndx2)
8284 symtable2[count2++].u.isym = isym;
8285
8286 if (count1 == 0 || count2 == 0 || count1 != count2)
8287 goto done;
8288
8289 for (i = 0; i < count1; i++)
8290 symtable1[i].name
8291 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8292 symtable1[i].u.isym->st_name);
8293
8294 for (i = 0; i < count2; i++)
8295 symtable2[i].name
8296 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8297 symtable2[i].u.isym->st_name);
8298
8299 /* Sort symbol by name. */
8300 qsort (symtable1, count1, sizeof (struct elf_symbol),
8301 elf_sym_name_compare);
8302 qsort (symtable2, count1, sizeof (struct elf_symbol),
8303 elf_sym_name_compare);
8304
8305 for (i = 0; i < count1; i++)
8306 /* Two symbols must have the same binding, type and name. */
8307 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8308 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8309 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8310 goto done;
8311
8312 result = TRUE;
8313
8314 done:
8315 if (symtable1)
8316 free (symtable1);
8317 if (symtable2)
8318 free (symtable2);
8319 if (isymbuf1)
8320 free (isymbuf1);
8321 if (isymbuf2)
8322 free (isymbuf2);
8323
8324 return result;
8325 }
8326
8327 /* Return TRUE if 2 section types are compatible. */
8328
8329 bfd_boolean
8330 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8331 bfd *bbfd, const asection *bsec)
8332 {
8333 if (asec == NULL
8334 || bsec == NULL
8335 || abfd->xvec->flavour != bfd_target_elf_flavour
8336 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8337 return TRUE;
8338
8339 return elf_section_type (asec) == elf_section_type (bsec);
8340 }
8341 \f
8342 /* Final phase of ELF linker. */
8343
8344 /* A structure we use to avoid passing large numbers of arguments. */
8345
8346 struct elf_final_link_info
8347 {
8348 /* General link information. */
8349 struct bfd_link_info *info;
8350 /* Output BFD. */
8351 bfd *output_bfd;
8352 /* Symbol string table. */
8353 struct elf_strtab_hash *symstrtab;
8354 /* .hash section. */
8355 asection *hash_sec;
8356 /* symbol version section (.gnu.version). */
8357 asection *symver_sec;
8358 /* Buffer large enough to hold contents of any section. */
8359 bfd_byte *contents;
8360 /* Buffer large enough to hold external relocs of any section. */
8361 void *external_relocs;
8362 /* Buffer large enough to hold internal relocs of any section. */
8363 Elf_Internal_Rela *internal_relocs;
8364 /* Buffer large enough to hold external local symbols of any input
8365 BFD. */
8366 bfd_byte *external_syms;
8367 /* And a buffer for symbol section indices. */
8368 Elf_External_Sym_Shndx *locsym_shndx;
8369 /* Buffer large enough to hold internal local symbols of any input
8370 BFD. */
8371 Elf_Internal_Sym *internal_syms;
8372 /* Array large enough to hold a symbol index for each local symbol
8373 of any input BFD. */
8374 long *indices;
8375 /* Array large enough to hold a section pointer for each local
8376 symbol of any input BFD. */
8377 asection **sections;
8378 /* Buffer for SHT_SYMTAB_SHNDX section. */
8379 Elf_External_Sym_Shndx *symshndxbuf;
8380 /* Number of STT_FILE syms seen. */
8381 size_t filesym_count;
8382 };
8383
8384 /* This struct is used to pass information to elf_link_output_extsym. */
8385
8386 struct elf_outext_info
8387 {
8388 bfd_boolean failed;
8389 bfd_boolean localsyms;
8390 bfd_boolean file_sym_done;
8391 struct elf_final_link_info *flinfo;
8392 };
8393
8394
8395 /* Support for evaluating a complex relocation.
8396
8397 Complex relocations are generalized, self-describing relocations. The
8398 implementation of them consists of two parts: complex symbols, and the
8399 relocations themselves.
8400
8401 The relocations are use a reserved elf-wide relocation type code (R_RELC
8402 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8403 information (start bit, end bit, word width, etc) into the addend. This
8404 information is extracted from CGEN-generated operand tables within gas.
8405
8406 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8407 internal) representing prefix-notation expressions, including but not
8408 limited to those sorts of expressions normally encoded as addends in the
8409 addend field. The symbol mangling format is:
8410
8411 <node> := <literal>
8412 | <unary-operator> ':' <node>
8413 | <binary-operator> ':' <node> ':' <node>
8414 ;
8415
8416 <literal> := 's' <digits=N> ':' <N character symbol name>
8417 | 'S' <digits=N> ':' <N character section name>
8418 | '#' <hexdigits>
8419 ;
8420
8421 <binary-operator> := as in C
8422 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8423
8424 static void
8425 set_symbol_value (bfd *bfd_with_globals,
8426 Elf_Internal_Sym *isymbuf,
8427 size_t locsymcount,
8428 size_t symidx,
8429 bfd_vma val)
8430 {
8431 struct elf_link_hash_entry **sym_hashes;
8432 struct elf_link_hash_entry *h;
8433 size_t extsymoff = locsymcount;
8434
8435 if (symidx < locsymcount)
8436 {
8437 Elf_Internal_Sym *sym;
8438
8439 sym = isymbuf + symidx;
8440 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8441 {
8442 /* It is a local symbol: move it to the
8443 "absolute" section and give it a value. */
8444 sym->st_shndx = SHN_ABS;
8445 sym->st_value = val;
8446 return;
8447 }
8448 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8449 extsymoff = 0;
8450 }
8451
8452 /* It is a global symbol: set its link type
8453 to "defined" and give it a value. */
8454
8455 sym_hashes = elf_sym_hashes (bfd_with_globals);
8456 h = sym_hashes [symidx - extsymoff];
8457 while (h->root.type == bfd_link_hash_indirect
8458 || h->root.type == bfd_link_hash_warning)
8459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8460 h->root.type = bfd_link_hash_defined;
8461 h->root.u.def.value = val;
8462 h->root.u.def.section = bfd_abs_section_ptr;
8463 }
8464
8465 static bfd_boolean
8466 resolve_symbol (const char *name,
8467 bfd *input_bfd,
8468 struct elf_final_link_info *flinfo,
8469 bfd_vma *result,
8470 Elf_Internal_Sym *isymbuf,
8471 size_t locsymcount)
8472 {
8473 Elf_Internal_Sym *sym;
8474 struct bfd_link_hash_entry *global_entry;
8475 const char *candidate = NULL;
8476 Elf_Internal_Shdr *symtab_hdr;
8477 size_t i;
8478
8479 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8480
8481 for (i = 0; i < locsymcount; ++ i)
8482 {
8483 sym = isymbuf + i;
8484
8485 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8486 continue;
8487
8488 candidate = bfd_elf_string_from_elf_section (input_bfd,
8489 symtab_hdr->sh_link,
8490 sym->st_name);
8491 #ifdef DEBUG
8492 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8493 name, candidate, (unsigned long) sym->st_value);
8494 #endif
8495 if (candidate && strcmp (candidate, name) == 0)
8496 {
8497 asection *sec = flinfo->sections [i];
8498
8499 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8500 *result += sec->output_offset + sec->output_section->vma;
8501 #ifdef DEBUG
8502 printf ("Found symbol with value %8.8lx\n",
8503 (unsigned long) *result);
8504 #endif
8505 return TRUE;
8506 }
8507 }
8508
8509 /* Hmm, haven't found it yet. perhaps it is a global. */
8510 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8511 FALSE, FALSE, TRUE);
8512 if (!global_entry)
8513 return FALSE;
8514
8515 if (global_entry->type == bfd_link_hash_defined
8516 || global_entry->type == bfd_link_hash_defweak)
8517 {
8518 *result = (global_entry->u.def.value
8519 + global_entry->u.def.section->output_section->vma
8520 + global_entry->u.def.section->output_offset);
8521 #ifdef DEBUG
8522 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8523 global_entry->root.string, (unsigned long) *result);
8524 #endif
8525 return TRUE;
8526 }
8527
8528 return FALSE;
8529 }
8530
8531 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8532 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8533 names like "foo.end" which is the end address of section "foo". */
8534
8535 static bfd_boolean
8536 resolve_section (const char *name,
8537 asection *sections,
8538 bfd_vma *result,
8539 bfd * abfd)
8540 {
8541 asection *curr;
8542 unsigned int len;
8543
8544 for (curr = sections; curr; curr = curr->next)
8545 if (strcmp (curr->name, name) == 0)
8546 {
8547 *result = curr->vma;
8548 return TRUE;
8549 }
8550
8551 /* Hmm. still haven't found it. try pseudo-section names. */
8552 /* FIXME: This could be coded more efficiently... */
8553 for (curr = sections; curr; curr = curr->next)
8554 {
8555 len = strlen (curr->name);
8556 if (len > strlen (name))
8557 continue;
8558
8559 if (strncmp (curr->name, name, len) == 0)
8560 {
8561 if (strncmp (".end", name + len, 4) == 0)
8562 {
8563 *result = (curr->vma
8564 + curr->size / bfd_octets_per_byte (abfd, curr));
8565 return TRUE;
8566 }
8567
8568 /* Insert more pseudo-section names here, if you like. */
8569 }
8570 }
8571
8572 return FALSE;
8573 }
8574
8575 static void
8576 undefined_reference (const char *reftype, const char *name)
8577 {
8578 /* xgettext:c-format */
8579 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8580 reftype, name);
8581 }
8582
8583 static bfd_boolean
8584 eval_symbol (bfd_vma *result,
8585 const char **symp,
8586 bfd *input_bfd,
8587 struct elf_final_link_info *flinfo,
8588 bfd_vma dot,
8589 Elf_Internal_Sym *isymbuf,
8590 size_t locsymcount,
8591 int signed_p)
8592 {
8593 size_t len;
8594 size_t symlen;
8595 bfd_vma a;
8596 bfd_vma b;
8597 char symbuf[4096];
8598 const char *sym = *symp;
8599 const char *symend;
8600 bfd_boolean symbol_is_section = FALSE;
8601
8602 len = strlen (sym);
8603 symend = sym + len;
8604
8605 if (len < 1 || len > sizeof (symbuf))
8606 {
8607 bfd_set_error (bfd_error_invalid_operation);
8608 return FALSE;
8609 }
8610
8611 switch (* sym)
8612 {
8613 case '.':
8614 *result = dot;
8615 *symp = sym + 1;
8616 return TRUE;
8617
8618 case '#':
8619 ++sym;
8620 *result = strtoul (sym, (char **) symp, 16);
8621 return TRUE;
8622
8623 case 'S':
8624 symbol_is_section = TRUE;
8625 /* Fall through. */
8626 case 's':
8627 ++sym;
8628 symlen = strtol (sym, (char **) symp, 10);
8629 sym = *symp + 1; /* Skip the trailing ':'. */
8630
8631 if (symend < sym || symlen + 1 > sizeof (symbuf))
8632 {
8633 bfd_set_error (bfd_error_invalid_operation);
8634 return FALSE;
8635 }
8636
8637 memcpy (symbuf, sym, symlen);
8638 symbuf[symlen] = '\0';
8639 *symp = sym + symlen;
8640
8641 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8642 the symbol as a section, or vice-versa. so we're pretty liberal in our
8643 interpretation here; section means "try section first", not "must be a
8644 section", and likewise with symbol. */
8645
8646 if (symbol_is_section)
8647 {
8648 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8649 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8650 isymbuf, locsymcount))
8651 {
8652 undefined_reference ("section", symbuf);
8653 return FALSE;
8654 }
8655 }
8656 else
8657 {
8658 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8659 isymbuf, locsymcount)
8660 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8661 result, input_bfd))
8662 {
8663 undefined_reference ("symbol", symbuf);
8664 return FALSE;
8665 }
8666 }
8667
8668 return TRUE;
8669
8670 /* All that remains are operators. */
8671
8672 #define UNARY_OP(op) \
8673 if (strncmp (sym, #op, strlen (#op)) == 0) \
8674 { \
8675 sym += strlen (#op); \
8676 if (*sym == ':') \
8677 ++sym; \
8678 *symp = sym; \
8679 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8680 isymbuf, locsymcount, signed_p)) \
8681 return FALSE; \
8682 if (signed_p) \
8683 *result = op ((bfd_signed_vma) a); \
8684 else \
8685 *result = op a; \
8686 return TRUE; \
8687 }
8688
8689 #define BINARY_OP(op) \
8690 if (strncmp (sym, #op, strlen (#op)) == 0) \
8691 { \
8692 sym += strlen (#op); \
8693 if (*sym == ':') \
8694 ++sym; \
8695 *symp = sym; \
8696 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8697 isymbuf, locsymcount, signed_p)) \
8698 return FALSE; \
8699 ++*symp; \
8700 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8701 isymbuf, locsymcount, signed_p)) \
8702 return FALSE; \
8703 if (signed_p) \
8704 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8705 else \
8706 *result = a op b; \
8707 return TRUE; \
8708 }
8709
8710 default:
8711 UNARY_OP (0-);
8712 BINARY_OP (<<);
8713 BINARY_OP (>>);
8714 BINARY_OP (==);
8715 BINARY_OP (!=);
8716 BINARY_OP (<=);
8717 BINARY_OP (>=);
8718 BINARY_OP (&&);
8719 BINARY_OP (||);
8720 UNARY_OP (~);
8721 UNARY_OP (!);
8722 BINARY_OP (*);
8723 BINARY_OP (/);
8724 BINARY_OP (%);
8725 BINARY_OP (^);
8726 BINARY_OP (|);
8727 BINARY_OP (&);
8728 BINARY_OP (+);
8729 BINARY_OP (-);
8730 BINARY_OP (<);
8731 BINARY_OP (>);
8732 #undef UNARY_OP
8733 #undef BINARY_OP
8734 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8735 bfd_set_error (bfd_error_invalid_operation);
8736 return FALSE;
8737 }
8738 }
8739
8740 static void
8741 put_value (bfd_vma size,
8742 unsigned long chunksz,
8743 bfd *input_bfd,
8744 bfd_vma x,
8745 bfd_byte *location)
8746 {
8747 location += (size - chunksz);
8748
8749 for (; size; size -= chunksz, location -= chunksz)
8750 {
8751 switch (chunksz)
8752 {
8753 case 1:
8754 bfd_put_8 (input_bfd, x, location);
8755 x >>= 8;
8756 break;
8757 case 2:
8758 bfd_put_16 (input_bfd, x, location);
8759 x >>= 16;
8760 break;
8761 case 4:
8762 bfd_put_32 (input_bfd, x, location);
8763 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8764 x >>= 16;
8765 x >>= 16;
8766 break;
8767 #ifdef BFD64
8768 case 8:
8769 bfd_put_64 (input_bfd, x, location);
8770 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8771 x >>= 32;
8772 x >>= 32;
8773 break;
8774 #endif
8775 default:
8776 abort ();
8777 break;
8778 }
8779 }
8780 }
8781
8782 static bfd_vma
8783 get_value (bfd_vma size,
8784 unsigned long chunksz,
8785 bfd *input_bfd,
8786 bfd_byte *location)
8787 {
8788 int shift;
8789 bfd_vma x = 0;
8790
8791 /* Sanity checks. */
8792 BFD_ASSERT (chunksz <= sizeof (x)
8793 && size >= chunksz
8794 && chunksz != 0
8795 && (size % chunksz) == 0
8796 && input_bfd != NULL
8797 && location != NULL);
8798
8799 if (chunksz == sizeof (x))
8800 {
8801 BFD_ASSERT (size == chunksz);
8802
8803 /* Make sure that we do not perform an undefined shift operation.
8804 We know that size == chunksz so there will only be one iteration
8805 of the loop below. */
8806 shift = 0;
8807 }
8808 else
8809 shift = 8 * chunksz;
8810
8811 for (; size; size -= chunksz, location += chunksz)
8812 {
8813 switch (chunksz)
8814 {
8815 case 1:
8816 x = (x << shift) | bfd_get_8 (input_bfd, location);
8817 break;
8818 case 2:
8819 x = (x << shift) | bfd_get_16 (input_bfd, location);
8820 break;
8821 case 4:
8822 x = (x << shift) | bfd_get_32 (input_bfd, location);
8823 break;
8824 #ifdef BFD64
8825 case 8:
8826 x = (x << shift) | bfd_get_64 (input_bfd, location);
8827 break;
8828 #endif
8829 default:
8830 abort ();
8831 }
8832 }
8833 return x;
8834 }
8835
8836 static void
8837 decode_complex_addend (unsigned long *start, /* in bits */
8838 unsigned long *oplen, /* in bits */
8839 unsigned long *len, /* in bits */
8840 unsigned long *wordsz, /* in bytes */
8841 unsigned long *chunksz, /* in bytes */
8842 unsigned long *lsb0_p,
8843 unsigned long *signed_p,
8844 unsigned long *trunc_p,
8845 unsigned long encoded)
8846 {
8847 * start = encoded & 0x3F;
8848 * len = (encoded >> 6) & 0x3F;
8849 * oplen = (encoded >> 12) & 0x3F;
8850 * wordsz = (encoded >> 18) & 0xF;
8851 * chunksz = (encoded >> 22) & 0xF;
8852 * lsb0_p = (encoded >> 27) & 1;
8853 * signed_p = (encoded >> 28) & 1;
8854 * trunc_p = (encoded >> 29) & 1;
8855 }
8856
8857 bfd_reloc_status_type
8858 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8859 asection *input_section,
8860 bfd_byte *contents,
8861 Elf_Internal_Rela *rel,
8862 bfd_vma relocation)
8863 {
8864 bfd_vma shift, x, mask;
8865 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8866 bfd_reloc_status_type r;
8867 bfd_size_type octets;
8868
8869 /* Perform this reloc, since it is complex.
8870 (this is not to say that it necessarily refers to a complex
8871 symbol; merely that it is a self-describing CGEN based reloc.
8872 i.e. the addend has the complete reloc information (bit start, end,
8873 word size, etc) encoded within it.). */
8874
8875 decode_complex_addend (&start, &oplen, &len, &wordsz,
8876 &chunksz, &lsb0_p, &signed_p,
8877 &trunc_p, rel->r_addend);
8878
8879 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8880
8881 if (lsb0_p)
8882 shift = (start + 1) - len;
8883 else
8884 shift = (8 * wordsz) - (start + len);
8885
8886 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8887 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8888
8889 #ifdef DEBUG
8890 printf ("Doing complex reloc: "
8891 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8892 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8893 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8894 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8895 oplen, (unsigned long) x, (unsigned long) mask,
8896 (unsigned long) relocation);
8897 #endif
8898
8899 r = bfd_reloc_ok;
8900 if (! trunc_p)
8901 /* Now do an overflow check. */
8902 r = bfd_check_overflow ((signed_p
8903 ? complain_overflow_signed
8904 : complain_overflow_unsigned),
8905 len, 0, (8 * wordsz),
8906 relocation);
8907
8908 /* Do the deed. */
8909 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8910
8911 #ifdef DEBUG
8912 printf (" relocation: %8.8lx\n"
8913 " shifted mask: %8.8lx\n"
8914 " shifted/masked reloc: %8.8lx\n"
8915 " result: %8.8lx\n",
8916 (unsigned long) relocation, (unsigned long) (mask << shift),
8917 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8918 #endif
8919 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8920 return r;
8921 }
8922
8923 /* Functions to read r_offset from external (target order) reloc
8924 entry. Faster than bfd_getl32 et al, because we let the compiler
8925 know the value is aligned. */
8926
8927 static bfd_vma
8928 ext32l_r_offset (const void *p)
8929 {
8930 union aligned32
8931 {
8932 uint32_t v;
8933 unsigned char c[4];
8934 };
8935 const union aligned32 *a
8936 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8937
8938 uint32_t aval = ( (uint32_t) a->c[0]
8939 | (uint32_t) a->c[1] << 8
8940 | (uint32_t) a->c[2] << 16
8941 | (uint32_t) a->c[3] << 24);
8942 return aval;
8943 }
8944
8945 static bfd_vma
8946 ext32b_r_offset (const void *p)
8947 {
8948 union aligned32
8949 {
8950 uint32_t v;
8951 unsigned char c[4];
8952 };
8953 const union aligned32 *a
8954 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8955
8956 uint32_t aval = ( (uint32_t) a->c[0] << 24
8957 | (uint32_t) a->c[1] << 16
8958 | (uint32_t) a->c[2] << 8
8959 | (uint32_t) a->c[3]);
8960 return aval;
8961 }
8962
8963 #ifdef BFD_HOST_64_BIT
8964 static bfd_vma
8965 ext64l_r_offset (const void *p)
8966 {
8967 union aligned64
8968 {
8969 uint64_t v;
8970 unsigned char c[8];
8971 };
8972 const union aligned64 *a
8973 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8974
8975 uint64_t aval = ( (uint64_t) a->c[0]
8976 | (uint64_t) a->c[1] << 8
8977 | (uint64_t) a->c[2] << 16
8978 | (uint64_t) a->c[3] << 24
8979 | (uint64_t) a->c[4] << 32
8980 | (uint64_t) a->c[5] << 40
8981 | (uint64_t) a->c[6] << 48
8982 | (uint64_t) a->c[7] << 56);
8983 return aval;
8984 }
8985
8986 static bfd_vma
8987 ext64b_r_offset (const void *p)
8988 {
8989 union aligned64
8990 {
8991 uint64_t v;
8992 unsigned char c[8];
8993 };
8994 const union aligned64 *a
8995 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8996
8997 uint64_t aval = ( (uint64_t) a->c[0] << 56
8998 | (uint64_t) a->c[1] << 48
8999 | (uint64_t) a->c[2] << 40
9000 | (uint64_t) a->c[3] << 32
9001 | (uint64_t) a->c[4] << 24
9002 | (uint64_t) a->c[5] << 16
9003 | (uint64_t) a->c[6] << 8
9004 | (uint64_t) a->c[7]);
9005 return aval;
9006 }
9007 #endif
9008
9009 /* When performing a relocatable link, the input relocations are
9010 preserved. But, if they reference global symbols, the indices
9011 referenced must be updated. Update all the relocations found in
9012 RELDATA. */
9013
9014 static bfd_boolean
9015 elf_link_adjust_relocs (bfd *abfd,
9016 asection *sec,
9017 struct bfd_elf_section_reloc_data *reldata,
9018 bfd_boolean sort,
9019 struct bfd_link_info *info)
9020 {
9021 unsigned int i;
9022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9023 bfd_byte *erela;
9024 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9025 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9026 bfd_vma r_type_mask;
9027 int r_sym_shift;
9028 unsigned int count = reldata->count;
9029 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9030
9031 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9032 {
9033 swap_in = bed->s->swap_reloc_in;
9034 swap_out = bed->s->swap_reloc_out;
9035 }
9036 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9037 {
9038 swap_in = bed->s->swap_reloca_in;
9039 swap_out = bed->s->swap_reloca_out;
9040 }
9041 else
9042 abort ();
9043
9044 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9045 abort ();
9046
9047 if (bed->s->arch_size == 32)
9048 {
9049 r_type_mask = 0xff;
9050 r_sym_shift = 8;
9051 }
9052 else
9053 {
9054 r_type_mask = 0xffffffff;
9055 r_sym_shift = 32;
9056 }
9057
9058 erela = reldata->hdr->contents;
9059 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9060 {
9061 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9062 unsigned int j;
9063
9064 if (*rel_hash == NULL)
9065 continue;
9066
9067 if ((*rel_hash)->indx == -2
9068 && info->gc_sections
9069 && ! info->gc_keep_exported)
9070 {
9071 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9072 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9073 abfd, sec,
9074 (*rel_hash)->root.root.string);
9075 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9076 abfd, sec);
9077 bfd_set_error (bfd_error_invalid_operation);
9078 return FALSE;
9079 }
9080 BFD_ASSERT ((*rel_hash)->indx >= 0);
9081
9082 (*swap_in) (abfd, erela, irela);
9083 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9084 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9085 | (irela[j].r_info & r_type_mask));
9086 (*swap_out) (abfd, irela, erela);
9087 }
9088
9089 if (bed->elf_backend_update_relocs)
9090 (*bed->elf_backend_update_relocs) (sec, reldata);
9091
9092 if (sort && count != 0)
9093 {
9094 bfd_vma (*ext_r_off) (const void *);
9095 bfd_vma r_off;
9096 size_t elt_size;
9097 bfd_byte *base, *end, *p, *loc;
9098 bfd_byte *buf = NULL;
9099
9100 if (bed->s->arch_size == 32)
9101 {
9102 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9103 ext_r_off = ext32l_r_offset;
9104 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9105 ext_r_off = ext32b_r_offset;
9106 else
9107 abort ();
9108 }
9109 else
9110 {
9111 #ifdef BFD_HOST_64_BIT
9112 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9113 ext_r_off = ext64l_r_offset;
9114 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9115 ext_r_off = ext64b_r_offset;
9116 else
9117 #endif
9118 abort ();
9119 }
9120
9121 /* Must use a stable sort here. A modified insertion sort,
9122 since the relocs are mostly sorted already. */
9123 elt_size = reldata->hdr->sh_entsize;
9124 base = reldata->hdr->contents;
9125 end = base + count * elt_size;
9126 if (elt_size > sizeof (Elf64_External_Rela))
9127 abort ();
9128
9129 /* Ensure the first element is lowest. This acts as a sentinel,
9130 speeding the main loop below. */
9131 r_off = (*ext_r_off) (base);
9132 for (p = loc = base; (p += elt_size) < end; )
9133 {
9134 bfd_vma r_off2 = (*ext_r_off) (p);
9135 if (r_off > r_off2)
9136 {
9137 r_off = r_off2;
9138 loc = p;
9139 }
9140 }
9141 if (loc != base)
9142 {
9143 /* Don't just swap *base and *loc as that changes the order
9144 of the original base[0] and base[1] if they happen to
9145 have the same r_offset. */
9146 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9147 memcpy (onebuf, loc, elt_size);
9148 memmove (base + elt_size, base, loc - base);
9149 memcpy (base, onebuf, elt_size);
9150 }
9151
9152 for (p = base + elt_size; (p += elt_size) < end; )
9153 {
9154 /* base to p is sorted, *p is next to insert. */
9155 r_off = (*ext_r_off) (p);
9156 /* Search the sorted region for location to insert. */
9157 loc = p - elt_size;
9158 while (r_off < (*ext_r_off) (loc))
9159 loc -= elt_size;
9160 loc += elt_size;
9161 if (loc != p)
9162 {
9163 /* Chances are there is a run of relocs to insert here,
9164 from one of more input files. Files are not always
9165 linked in order due to the way elf_link_input_bfd is
9166 called. See pr17666. */
9167 size_t sortlen = p - loc;
9168 bfd_vma r_off2 = (*ext_r_off) (loc);
9169 size_t runlen = elt_size;
9170 size_t buf_size = 96 * 1024;
9171 while (p + runlen < end
9172 && (sortlen <= buf_size
9173 || runlen + elt_size <= buf_size)
9174 && r_off2 > (*ext_r_off) (p + runlen))
9175 runlen += elt_size;
9176 if (buf == NULL)
9177 {
9178 buf = bfd_malloc (buf_size);
9179 if (buf == NULL)
9180 return FALSE;
9181 }
9182 if (runlen < sortlen)
9183 {
9184 memcpy (buf, p, runlen);
9185 memmove (loc + runlen, loc, sortlen);
9186 memcpy (loc, buf, runlen);
9187 }
9188 else
9189 {
9190 memcpy (buf, loc, sortlen);
9191 memmove (loc, p, runlen);
9192 memcpy (loc + runlen, buf, sortlen);
9193 }
9194 p += runlen - elt_size;
9195 }
9196 }
9197 /* Hashes are no longer valid. */
9198 free (reldata->hashes);
9199 reldata->hashes = NULL;
9200 free (buf);
9201 }
9202 return TRUE;
9203 }
9204
9205 struct elf_link_sort_rela
9206 {
9207 union {
9208 bfd_vma offset;
9209 bfd_vma sym_mask;
9210 } u;
9211 enum elf_reloc_type_class type;
9212 /* We use this as an array of size int_rels_per_ext_rel. */
9213 Elf_Internal_Rela rela[1];
9214 };
9215
9216 /* qsort stability here and for cmp2 is only an issue if multiple
9217 dynamic relocations are emitted at the same address. But targets
9218 that apply a series of dynamic relocations each operating on the
9219 result of the prior relocation can't use -z combreloc as
9220 implemented anyway. Such schemes tend to be broken by sorting on
9221 symbol index. That leaves dynamic NONE relocs as the only other
9222 case where ld might emit multiple relocs at the same address, and
9223 those are only emitted due to target bugs. */
9224
9225 static int
9226 elf_link_sort_cmp1 (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 int relativea, relativeb;
9231
9232 relativea = a->type == reloc_class_relative;
9233 relativeb = b->type == reloc_class_relative;
9234
9235 if (relativea < relativeb)
9236 return 1;
9237 if (relativea > relativeb)
9238 return -1;
9239 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9240 return -1;
9241 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9242 return 1;
9243 if (a->rela->r_offset < b->rela->r_offset)
9244 return -1;
9245 if (a->rela->r_offset > b->rela->r_offset)
9246 return 1;
9247 return 0;
9248 }
9249
9250 static int
9251 elf_link_sort_cmp2 (const void *A, const void *B)
9252 {
9253 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9254 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9255
9256 if (a->type < b->type)
9257 return -1;
9258 if (a->type > b->type)
9259 return 1;
9260 if (a->u.offset < b->u.offset)
9261 return -1;
9262 if (a->u.offset > b->u.offset)
9263 return 1;
9264 if (a->rela->r_offset < b->rela->r_offset)
9265 return -1;
9266 if (a->rela->r_offset > b->rela->r_offset)
9267 return 1;
9268 return 0;
9269 }
9270
9271 static size_t
9272 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9273 {
9274 asection *dynamic_relocs;
9275 asection *rela_dyn;
9276 asection *rel_dyn;
9277 bfd_size_type count, size;
9278 size_t i, ret, sort_elt, ext_size;
9279 bfd_byte *sort, *s_non_relative, *p;
9280 struct elf_link_sort_rela *sq;
9281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9282 int i2e = bed->s->int_rels_per_ext_rel;
9283 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9284 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9285 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9286 struct bfd_link_order *lo;
9287 bfd_vma r_sym_mask;
9288 bfd_boolean use_rela;
9289
9290 /* Find a dynamic reloc section. */
9291 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9292 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9293 if (rela_dyn != NULL && rela_dyn->size > 0
9294 && rel_dyn != NULL && rel_dyn->size > 0)
9295 {
9296 bfd_boolean use_rela_initialised = FALSE;
9297
9298 /* This is just here to stop gcc from complaining.
9299 Its initialization checking code is not perfect. */
9300 use_rela = TRUE;
9301
9302 /* Both sections are present. Examine the sizes
9303 of the indirect sections to help us choose. */
9304 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9305 if (lo->type == bfd_indirect_link_order)
9306 {
9307 asection *o = lo->u.indirect.section;
9308
9309 if ((o->size % bed->s->sizeof_rela) == 0)
9310 {
9311 if ((o->size % bed->s->sizeof_rel) == 0)
9312 /* Section size is divisible by both rel and rela sizes.
9313 It is of no help to us. */
9314 ;
9315 else
9316 {
9317 /* Section size is only divisible by rela. */
9318 if (use_rela_initialised && !use_rela)
9319 {
9320 _bfd_error_handler (_("%pB: unable to sort relocs - "
9321 "they are in more than one size"),
9322 abfd);
9323 bfd_set_error (bfd_error_invalid_operation);
9324 return 0;
9325 }
9326 else
9327 {
9328 use_rela = TRUE;
9329 use_rela_initialised = TRUE;
9330 }
9331 }
9332 }
9333 else if ((o->size % bed->s->sizeof_rel) == 0)
9334 {
9335 /* Section size is only divisible by rel. */
9336 if (use_rela_initialised && use_rela)
9337 {
9338 _bfd_error_handler (_("%pB: unable to sort relocs - "
9339 "they are in more than one size"),
9340 abfd);
9341 bfd_set_error (bfd_error_invalid_operation);
9342 return 0;
9343 }
9344 else
9345 {
9346 use_rela = FALSE;
9347 use_rela_initialised = TRUE;
9348 }
9349 }
9350 else
9351 {
9352 /* The section size is not divisible by either -
9353 something is wrong. */
9354 _bfd_error_handler (_("%pB: unable to sort relocs - "
9355 "they are of an unknown size"), abfd);
9356 bfd_set_error (bfd_error_invalid_operation);
9357 return 0;
9358 }
9359 }
9360
9361 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9362 if (lo->type == bfd_indirect_link_order)
9363 {
9364 asection *o = lo->u.indirect.section;
9365
9366 if ((o->size % bed->s->sizeof_rela) == 0)
9367 {
9368 if ((o->size % bed->s->sizeof_rel) == 0)
9369 /* Section size is divisible by both rel and rela sizes.
9370 It is of no help to us. */
9371 ;
9372 else
9373 {
9374 /* Section size is only divisible by rela. */
9375 if (use_rela_initialised && !use_rela)
9376 {
9377 _bfd_error_handler (_("%pB: unable to sort relocs - "
9378 "they are in more than one size"),
9379 abfd);
9380 bfd_set_error (bfd_error_invalid_operation);
9381 return 0;
9382 }
9383 else
9384 {
9385 use_rela = TRUE;
9386 use_rela_initialised = TRUE;
9387 }
9388 }
9389 }
9390 else if ((o->size % bed->s->sizeof_rel) == 0)
9391 {
9392 /* Section size is only divisible by rel. */
9393 if (use_rela_initialised && use_rela)
9394 {
9395 _bfd_error_handler (_("%pB: unable to sort relocs - "
9396 "they are in more than one size"),
9397 abfd);
9398 bfd_set_error (bfd_error_invalid_operation);
9399 return 0;
9400 }
9401 else
9402 {
9403 use_rela = FALSE;
9404 use_rela_initialised = TRUE;
9405 }
9406 }
9407 else
9408 {
9409 /* The section size is not divisible by either -
9410 something is wrong. */
9411 _bfd_error_handler (_("%pB: unable to sort relocs - "
9412 "they are of an unknown size"), abfd);
9413 bfd_set_error (bfd_error_invalid_operation);
9414 return 0;
9415 }
9416 }
9417
9418 if (! use_rela_initialised)
9419 /* Make a guess. */
9420 use_rela = TRUE;
9421 }
9422 else if (rela_dyn != NULL && rela_dyn->size > 0)
9423 use_rela = TRUE;
9424 else if (rel_dyn != NULL && rel_dyn->size > 0)
9425 use_rela = FALSE;
9426 else
9427 return 0;
9428
9429 if (use_rela)
9430 {
9431 dynamic_relocs = rela_dyn;
9432 ext_size = bed->s->sizeof_rela;
9433 swap_in = bed->s->swap_reloca_in;
9434 swap_out = bed->s->swap_reloca_out;
9435 }
9436 else
9437 {
9438 dynamic_relocs = rel_dyn;
9439 ext_size = bed->s->sizeof_rel;
9440 swap_in = bed->s->swap_reloc_in;
9441 swap_out = bed->s->swap_reloc_out;
9442 }
9443
9444 size = 0;
9445 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9446 if (lo->type == bfd_indirect_link_order)
9447 size += lo->u.indirect.section->size;
9448
9449 if (size != dynamic_relocs->size)
9450 return 0;
9451
9452 sort_elt = (sizeof (struct elf_link_sort_rela)
9453 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9454
9455 count = dynamic_relocs->size / ext_size;
9456 if (count == 0)
9457 return 0;
9458 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9459
9460 if (sort == NULL)
9461 {
9462 (*info->callbacks->warning)
9463 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9464 return 0;
9465 }
9466
9467 if (bed->s->arch_size == 32)
9468 r_sym_mask = ~(bfd_vma) 0xff;
9469 else
9470 r_sym_mask = ~(bfd_vma) 0xffffffff;
9471
9472 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9473 if (lo->type == bfd_indirect_link_order)
9474 {
9475 bfd_byte *erel, *erelend;
9476 asection *o = lo->u.indirect.section;
9477
9478 if (o->contents == NULL && o->size != 0)
9479 {
9480 /* This is a reloc section that is being handled as a normal
9481 section. See bfd_section_from_shdr. We can't combine
9482 relocs in this case. */
9483 free (sort);
9484 return 0;
9485 }
9486 erel = o->contents;
9487 erelend = o->contents + o->size;
9488 p = sort + o->output_offset * opb / ext_size * sort_elt;
9489
9490 while (erel < erelend)
9491 {
9492 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9493
9494 (*swap_in) (abfd, erel, s->rela);
9495 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9496 s->u.sym_mask = r_sym_mask;
9497 p += sort_elt;
9498 erel += ext_size;
9499 }
9500 }
9501
9502 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9503
9504 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9505 {
9506 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9507 if (s->type != reloc_class_relative)
9508 break;
9509 }
9510 ret = i;
9511 s_non_relative = p;
9512
9513 sq = (struct elf_link_sort_rela *) s_non_relative;
9514 for (; i < count; i++, p += sort_elt)
9515 {
9516 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9517 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9518 sq = sp;
9519 sp->u.offset = sq->rela->r_offset;
9520 }
9521
9522 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9523
9524 struct elf_link_hash_table *htab = elf_hash_table (info);
9525 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9526 {
9527 /* We have plt relocs in .rela.dyn. */
9528 sq = (struct elf_link_sort_rela *) sort;
9529 for (i = 0; i < count; i++)
9530 if (sq[count - i - 1].type != reloc_class_plt)
9531 break;
9532 if (i != 0 && htab->srelplt->size == i * ext_size)
9533 {
9534 struct bfd_link_order **plo;
9535 /* Put srelplt link_order last. This is so the output_offset
9536 set in the next loop is correct for DT_JMPREL. */
9537 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9538 if ((*plo)->type == bfd_indirect_link_order
9539 && (*plo)->u.indirect.section == htab->srelplt)
9540 {
9541 lo = *plo;
9542 *plo = lo->next;
9543 }
9544 else
9545 plo = &(*plo)->next;
9546 *plo = lo;
9547 lo->next = NULL;
9548 dynamic_relocs->map_tail.link_order = lo;
9549 }
9550 }
9551
9552 p = sort;
9553 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9554 if (lo->type == bfd_indirect_link_order)
9555 {
9556 bfd_byte *erel, *erelend;
9557 asection *o = lo->u.indirect.section;
9558
9559 erel = o->contents;
9560 erelend = o->contents + o->size;
9561 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9562 while (erel < erelend)
9563 {
9564 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9565 (*swap_out) (abfd, s->rela, erel);
9566 p += sort_elt;
9567 erel += ext_size;
9568 }
9569 }
9570
9571 free (sort);
9572 *psec = dynamic_relocs;
9573 return ret;
9574 }
9575
9576 /* Add a symbol to the output symbol string table. */
9577
9578 static int
9579 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9580 const char *name,
9581 Elf_Internal_Sym *elfsym,
9582 asection *input_sec,
9583 struct elf_link_hash_entry *h)
9584 {
9585 int (*output_symbol_hook)
9586 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9587 struct elf_link_hash_entry *);
9588 struct elf_link_hash_table *hash_table;
9589 const struct elf_backend_data *bed;
9590 bfd_size_type strtabsize;
9591
9592 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9593
9594 bed = get_elf_backend_data (flinfo->output_bfd);
9595 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9596 if (output_symbol_hook != NULL)
9597 {
9598 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9599 if (ret != 1)
9600 return ret;
9601 }
9602
9603 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9604 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9605 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9606 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9607
9608 if (name == NULL
9609 || *name == '\0'
9610 || (input_sec->flags & SEC_EXCLUDE))
9611 elfsym->st_name = (unsigned long) -1;
9612 else
9613 {
9614 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9615 to get the final offset for st_name. */
9616 elfsym->st_name
9617 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9618 name, FALSE);
9619 if (elfsym->st_name == (unsigned long) -1)
9620 return 0;
9621 }
9622
9623 hash_table = elf_hash_table (flinfo->info);
9624 strtabsize = hash_table->strtabsize;
9625 if (strtabsize <= hash_table->strtabcount)
9626 {
9627 strtabsize += strtabsize;
9628 hash_table->strtabsize = strtabsize;
9629 strtabsize *= sizeof (*hash_table->strtab);
9630 hash_table->strtab
9631 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9632 strtabsize);
9633 if (hash_table->strtab == NULL)
9634 return 0;
9635 }
9636 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9637 hash_table->strtab[hash_table->strtabcount].dest_index
9638 = hash_table->strtabcount;
9639 hash_table->strtab[hash_table->strtabcount].destshndx_index
9640 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9641
9642 flinfo->output_bfd->symcount += 1;
9643 hash_table->strtabcount += 1;
9644
9645 return 1;
9646 }
9647
9648 /* Swap symbols out to the symbol table and flush the output symbols to
9649 the file. */
9650
9651 static bfd_boolean
9652 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9653 {
9654 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9655 size_t amt;
9656 size_t i;
9657 const struct elf_backend_data *bed;
9658 bfd_byte *symbuf;
9659 Elf_Internal_Shdr *hdr;
9660 file_ptr pos;
9661 bfd_boolean ret;
9662
9663 if (!hash_table->strtabcount)
9664 return TRUE;
9665
9666 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9667
9668 bed = get_elf_backend_data (flinfo->output_bfd);
9669
9670 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9671 symbuf = (bfd_byte *) bfd_malloc (amt);
9672 if (symbuf == NULL)
9673 return FALSE;
9674
9675 if (flinfo->symshndxbuf)
9676 {
9677 amt = sizeof (Elf_External_Sym_Shndx);
9678 amt *= bfd_get_symcount (flinfo->output_bfd);
9679 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9680 if (flinfo->symshndxbuf == NULL)
9681 {
9682 free (symbuf);
9683 return FALSE;
9684 }
9685 }
9686
9687 for (i = 0; i < hash_table->strtabcount; i++)
9688 {
9689 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9690 if (elfsym->sym.st_name == (unsigned long) -1)
9691 elfsym->sym.st_name = 0;
9692 else
9693 elfsym->sym.st_name
9694 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9695 elfsym->sym.st_name);
9696 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9697 ((bfd_byte *) symbuf
9698 + (elfsym->dest_index
9699 * bed->s->sizeof_sym)),
9700 (flinfo->symshndxbuf
9701 + elfsym->destshndx_index));
9702 }
9703
9704 /* Allow the linker to examine the strtab and symtab now they are
9705 populated. */
9706
9707 if (flinfo->info->callbacks->examine_strtab)
9708 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9709 hash_table->strtabcount,
9710 flinfo->symstrtab);
9711
9712 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9713 pos = hdr->sh_offset + hdr->sh_size;
9714 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9715 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9716 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9717 {
9718 hdr->sh_size += amt;
9719 ret = TRUE;
9720 }
9721 else
9722 ret = FALSE;
9723
9724 free (symbuf);
9725
9726 free (hash_table->strtab);
9727 hash_table->strtab = NULL;
9728
9729 return ret;
9730 }
9731
9732 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9733
9734 static bfd_boolean
9735 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9736 {
9737 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9738 && sym->st_shndx < SHN_LORESERVE)
9739 {
9740 /* The gABI doesn't support dynamic symbols in output sections
9741 beyond 64k. */
9742 _bfd_error_handler
9743 /* xgettext:c-format */
9744 (_("%pB: too many sections: %d (>= %d)"),
9745 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9746 bfd_set_error (bfd_error_nonrepresentable_section);
9747 return FALSE;
9748 }
9749 return TRUE;
9750 }
9751
9752 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9753 allowing an unsatisfied unversioned symbol in the DSO to match a
9754 versioned symbol that would normally require an explicit version.
9755 We also handle the case that a DSO references a hidden symbol
9756 which may be satisfied by a versioned symbol in another DSO. */
9757
9758 static bfd_boolean
9759 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9760 const struct elf_backend_data *bed,
9761 struct elf_link_hash_entry *h)
9762 {
9763 bfd *abfd;
9764 struct elf_link_loaded_list *loaded;
9765
9766 if (!is_elf_hash_table (info->hash))
9767 return FALSE;
9768
9769 /* Check indirect symbol. */
9770 while (h->root.type == bfd_link_hash_indirect)
9771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9772
9773 switch (h->root.type)
9774 {
9775 default:
9776 abfd = NULL;
9777 break;
9778
9779 case bfd_link_hash_undefined:
9780 case bfd_link_hash_undefweak:
9781 abfd = h->root.u.undef.abfd;
9782 if (abfd == NULL
9783 || (abfd->flags & DYNAMIC) == 0
9784 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9785 return FALSE;
9786 break;
9787
9788 case bfd_link_hash_defined:
9789 case bfd_link_hash_defweak:
9790 abfd = h->root.u.def.section->owner;
9791 break;
9792
9793 case bfd_link_hash_common:
9794 abfd = h->root.u.c.p->section->owner;
9795 break;
9796 }
9797 BFD_ASSERT (abfd != NULL);
9798
9799 for (loaded = elf_hash_table (info)->dyn_loaded;
9800 loaded != NULL;
9801 loaded = loaded->next)
9802 {
9803 bfd *input;
9804 Elf_Internal_Shdr *hdr;
9805 size_t symcount;
9806 size_t extsymcount;
9807 size_t extsymoff;
9808 Elf_Internal_Shdr *versymhdr;
9809 Elf_Internal_Sym *isym;
9810 Elf_Internal_Sym *isymend;
9811 Elf_Internal_Sym *isymbuf;
9812 Elf_External_Versym *ever;
9813 Elf_External_Versym *extversym;
9814
9815 input = loaded->abfd;
9816
9817 /* We check each DSO for a possible hidden versioned definition. */
9818 if (input == abfd
9819 || elf_dynversym (input) == 0)
9820 continue;
9821
9822 hdr = &elf_tdata (input)->dynsymtab_hdr;
9823
9824 symcount = hdr->sh_size / bed->s->sizeof_sym;
9825 if (elf_bad_symtab (input))
9826 {
9827 extsymcount = symcount;
9828 extsymoff = 0;
9829 }
9830 else
9831 {
9832 extsymcount = symcount - hdr->sh_info;
9833 extsymoff = hdr->sh_info;
9834 }
9835
9836 if (extsymcount == 0)
9837 continue;
9838
9839 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9840 NULL, NULL, NULL);
9841 if (isymbuf == NULL)
9842 return FALSE;
9843
9844 /* Read in any version definitions. */
9845 versymhdr = &elf_tdata (input)->dynversym_hdr;
9846 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9847 || (extversym = (Elf_External_Versym *)
9848 _bfd_malloc_and_read (input, versymhdr->sh_size,
9849 versymhdr->sh_size)) == NULL)
9850 {
9851 free (isymbuf);
9852 return FALSE;
9853 }
9854
9855 ever = extversym + extsymoff;
9856 isymend = isymbuf + extsymcount;
9857 for (isym = isymbuf; isym < isymend; isym++, ever++)
9858 {
9859 const char *name;
9860 Elf_Internal_Versym iver;
9861 unsigned short version_index;
9862
9863 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9864 || isym->st_shndx == SHN_UNDEF)
9865 continue;
9866
9867 name = bfd_elf_string_from_elf_section (input,
9868 hdr->sh_link,
9869 isym->st_name);
9870 if (strcmp (name, h->root.root.string) != 0)
9871 continue;
9872
9873 _bfd_elf_swap_versym_in (input, ever, &iver);
9874
9875 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9876 && !(h->def_regular
9877 && h->forced_local))
9878 {
9879 /* If we have a non-hidden versioned sym, then it should
9880 have provided a definition for the undefined sym unless
9881 it is defined in a non-shared object and forced local.
9882 */
9883 abort ();
9884 }
9885
9886 version_index = iver.vs_vers & VERSYM_VERSION;
9887 if (version_index == 1 || version_index == 2)
9888 {
9889 /* This is the base or first version. We can use it. */
9890 free (extversym);
9891 free (isymbuf);
9892 return TRUE;
9893 }
9894 }
9895
9896 free (extversym);
9897 free (isymbuf);
9898 }
9899
9900 return FALSE;
9901 }
9902
9903 /* Convert ELF common symbol TYPE. */
9904
9905 static int
9906 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9907 {
9908 /* Commom symbol can only appear in relocatable link. */
9909 if (!bfd_link_relocatable (info))
9910 abort ();
9911 switch (info->elf_stt_common)
9912 {
9913 case unchanged:
9914 break;
9915 case elf_stt_common:
9916 type = STT_COMMON;
9917 break;
9918 case no_elf_stt_common:
9919 type = STT_OBJECT;
9920 break;
9921 }
9922 return type;
9923 }
9924
9925 /* Add an external symbol to the symbol table. This is called from
9926 the hash table traversal routine. When generating a shared object,
9927 we go through the symbol table twice. The first time we output
9928 anything that might have been forced to local scope in a version
9929 script. The second time we output the symbols that are still
9930 global symbols. */
9931
9932 static bfd_boolean
9933 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9934 {
9935 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9936 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9937 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9938 bfd_boolean strip;
9939 Elf_Internal_Sym sym;
9940 asection *input_sec;
9941 const struct elf_backend_data *bed;
9942 long indx;
9943 int ret;
9944 unsigned int type;
9945
9946 if (h->root.type == bfd_link_hash_warning)
9947 {
9948 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9949 if (h->root.type == bfd_link_hash_new)
9950 return TRUE;
9951 }
9952
9953 /* Decide whether to output this symbol in this pass. */
9954 if (eoinfo->localsyms)
9955 {
9956 if (!h->forced_local)
9957 return TRUE;
9958 }
9959 else
9960 {
9961 if (h->forced_local)
9962 return TRUE;
9963 }
9964
9965 bed = get_elf_backend_data (flinfo->output_bfd);
9966
9967 if (h->root.type == bfd_link_hash_undefined)
9968 {
9969 /* If we have an undefined symbol reference here then it must have
9970 come from a shared library that is being linked in. (Undefined
9971 references in regular files have already been handled unless
9972 they are in unreferenced sections which are removed by garbage
9973 collection). */
9974 bfd_boolean ignore_undef = FALSE;
9975
9976 /* Some symbols may be special in that the fact that they're
9977 undefined can be safely ignored - let backend determine that. */
9978 if (bed->elf_backend_ignore_undef_symbol)
9979 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9980
9981 /* If we are reporting errors for this situation then do so now. */
9982 if (!ignore_undef
9983 && h->ref_dynamic_nonweak
9984 && (!h->ref_regular || flinfo->info->gc_sections)
9985 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9986 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9987 {
9988 flinfo->info->callbacks->undefined_symbol
9989 (flinfo->info, h->root.root.string,
9990 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
9991 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
9992 && !flinfo->info->warn_unresolved_syms);
9993 }
9994
9995 /* Strip a global symbol defined in a discarded section. */
9996 if (h->indx == -3)
9997 return TRUE;
9998 }
9999
10000 /* We should also warn if a forced local symbol is referenced from
10001 shared libraries. */
10002 if (bfd_link_executable (flinfo->info)
10003 && h->forced_local
10004 && h->ref_dynamic
10005 && h->def_regular
10006 && !h->dynamic_def
10007 && h->ref_dynamic_nonweak
10008 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10009 {
10010 bfd *def_bfd;
10011 const char *msg;
10012 struct elf_link_hash_entry *hi = h;
10013
10014 /* Check indirect symbol. */
10015 while (hi->root.type == bfd_link_hash_indirect)
10016 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10017
10018 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10019 /* xgettext:c-format */
10020 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10021 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10022 /* xgettext:c-format */
10023 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10024 else
10025 /* xgettext:c-format */
10026 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10027 def_bfd = flinfo->output_bfd;
10028 if (hi->root.u.def.section != bfd_abs_section_ptr)
10029 def_bfd = hi->root.u.def.section->owner;
10030 _bfd_error_handler (msg, flinfo->output_bfd,
10031 h->root.root.string, def_bfd);
10032 bfd_set_error (bfd_error_bad_value);
10033 eoinfo->failed = TRUE;
10034 return FALSE;
10035 }
10036
10037 /* We don't want to output symbols that have never been mentioned by
10038 a regular file, or that we have been told to strip. However, if
10039 h->indx is set to -2, the symbol is used by a reloc and we must
10040 output it. */
10041 strip = FALSE;
10042 if (h->indx == -2)
10043 ;
10044 else if ((h->def_dynamic
10045 || h->ref_dynamic
10046 || h->root.type == bfd_link_hash_new)
10047 && !h->def_regular
10048 && !h->ref_regular)
10049 strip = TRUE;
10050 else if (flinfo->info->strip == strip_all)
10051 strip = TRUE;
10052 else if (flinfo->info->strip == strip_some
10053 && bfd_hash_lookup (flinfo->info->keep_hash,
10054 h->root.root.string, FALSE, FALSE) == NULL)
10055 strip = TRUE;
10056 else if ((h->root.type == bfd_link_hash_defined
10057 || h->root.type == bfd_link_hash_defweak)
10058 && ((flinfo->info->strip_discarded
10059 && discarded_section (h->root.u.def.section))
10060 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10061 && h->root.u.def.section->owner != NULL
10062 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10063 strip = TRUE;
10064 else if ((h->root.type == bfd_link_hash_undefined
10065 || h->root.type == bfd_link_hash_undefweak)
10066 && h->root.u.undef.abfd != NULL
10067 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10068 strip = TRUE;
10069
10070 type = h->type;
10071
10072 /* If we're stripping it, and it's not a dynamic symbol, there's
10073 nothing else to do. However, if it is a forced local symbol or
10074 an ifunc symbol we need to give the backend finish_dynamic_symbol
10075 function a chance to make it dynamic. */
10076 if (strip
10077 && h->dynindx == -1
10078 && type != STT_GNU_IFUNC
10079 && !h->forced_local)
10080 return TRUE;
10081
10082 sym.st_value = 0;
10083 sym.st_size = h->size;
10084 sym.st_other = h->other;
10085 switch (h->root.type)
10086 {
10087 default:
10088 case bfd_link_hash_new:
10089 case bfd_link_hash_warning:
10090 abort ();
10091 return FALSE;
10092
10093 case bfd_link_hash_undefined:
10094 case bfd_link_hash_undefweak:
10095 input_sec = bfd_und_section_ptr;
10096 sym.st_shndx = SHN_UNDEF;
10097 break;
10098
10099 case bfd_link_hash_defined:
10100 case bfd_link_hash_defweak:
10101 {
10102 input_sec = h->root.u.def.section;
10103 if (input_sec->output_section != NULL)
10104 {
10105 sym.st_shndx =
10106 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10107 input_sec->output_section);
10108 if (sym.st_shndx == SHN_BAD)
10109 {
10110 _bfd_error_handler
10111 /* xgettext:c-format */
10112 (_("%pB: could not find output section %pA for input section %pA"),
10113 flinfo->output_bfd, input_sec->output_section, input_sec);
10114 bfd_set_error (bfd_error_nonrepresentable_section);
10115 eoinfo->failed = TRUE;
10116 return FALSE;
10117 }
10118
10119 /* ELF symbols in relocatable files are section relative,
10120 but in nonrelocatable files they are virtual
10121 addresses. */
10122 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10123 if (!bfd_link_relocatable (flinfo->info))
10124 {
10125 sym.st_value += input_sec->output_section->vma;
10126 if (h->type == STT_TLS)
10127 {
10128 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10129 if (tls_sec != NULL)
10130 sym.st_value -= tls_sec->vma;
10131 }
10132 }
10133 }
10134 else
10135 {
10136 BFD_ASSERT (input_sec->owner == NULL
10137 || (input_sec->owner->flags & DYNAMIC) != 0);
10138 sym.st_shndx = SHN_UNDEF;
10139 input_sec = bfd_und_section_ptr;
10140 }
10141 }
10142 break;
10143
10144 case bfd_link_hash_common:
10145 input_sec = h->root.u.c.p->section;
10146 sym.st_shndx = bed->common_section_index (input_sec);
10147 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10148 break;
10149
10150 case bfd_link_hash_indirect:
10151 /* These symbols are created by symbol versioning. They point
10152 to the decorated version of the name. For example, if the
10153 symbol foo@@GNU_1.2 is the default, which should be used when
10154 foo is used with no version, then we add an indirect symbol
10155 foo which points to foo@@GNU_1.2. We ignore these symbols,
10156 since the indirected symbol is already in the hash table. */
10157 return TRUE;
10158 }
10159
10160 if (type == STT_COMMON || type == STT_OBJECT)
10161 switch (h->root.type)
10162 {
10163 case bfd_link_hash_common:
10164 type = elf_link_convert_common_type (flinfo->info, type);
10165 break;
10166 case bfd_link_hash_defined:
10167 case bfd_link_hash_defweak:
10168 if (bed->common_definition (&sym))
10169 type = elf_link_convert_common_type (flinfo->info, type);
10170 else
10171 type = STT_OBJECT;
10172 break;
10173 case bfd_link_hash_undefined:
10174 case bfd_link_hash_undefweak:
10175 break;
10176 default:
10177 abort ();
10178 }
10179
10180 if (h->forced_local)
10181 {
10182 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10183 /* Turn off visibility on local symbol. */
10184 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10185 }
10186 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10187 else if (h->unique_global && h->def_regular)
10188 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10189 else if (h->root.type == bfd_link_hash_undefweak
10190 || h->root.type == bfd_link_hash_defweak)
10191 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10192 else
10193 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10194 sym.st_target_internal = h->target_internal;
10195
10196 /* Give the processor backend a chance to tweak the symbol value,
10197 and also to finish up anything that needs to be done for this
10198 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10199 forced local syms when non-shared is due to a historical quirk.
10200 STT_GNU_IFUNC symbol must go through PLT. */
10201 if ((h->type == STT_GNU_IFUNC
10202 && h->def_regular
10203 && !bfd_link_relocatable (flinfo->info))
10204 || ((h->dynindx != -1
10205 || h->forced_local)
10206 && ((bfd_link_pic (flinfo->info)
10207 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10208 || h->root.type != bfd_link_hash_undefweak))
10209 || !h->forced_local)
10210 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10211 {
10212 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10213 (flinfo->output_bfd, flinfo->info, h, &sym)))
10214 {
10215 eoinfo->failed = TRUE;
10216 return FALSE;
10217 }
10218 }
10219
10220 /* If we are marking the symbol as undefined, and there are no
10221 non-weak references to this symbol from a regular object, then
10222 mark the symbol as weak undefined; if there are non-weak
10223 references, mark the symbol as strong. We can't do this earlier,
10224 because it might not be marked as undefined until the
10225 finish_dynamic_symbol routine gets through with it. */
10226 if (sym.st_shndx == SHN_UNDEF
10227 && h->ref_regular
10228 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10229 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10230 {
10231 int bindtype;
10232 type = ELF_ST_TYPE (sym.st_info);
10233
10234 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10235 if (type == STT_GNU_IFUNC)
10236 type = STT_FUNC;
10237
10238 if (h->ref_regular_nonweak)
10239 bindtype = STB_GLOBAL;
10240 else
10241 bindtype = STB_WEAK;
10242 sym.st_info = ELF_ST_INFO (bindtype, type);
10243 }
10244
10245 /* If this is a symbol defined in a dynamic library, don't use the
10246 symbol size from the dynamic library. Relinking an executable
10247 against a new library may introduce gratuitous changes in the
10248 executable's symbols if we keep the size. */
10249 if (sym.st_shndx == SHN_UNDEF
10250 && !h->def_regular
10251 && h->def_dynamic)
10252 sym.st_size = 0;
10253
10254 /* If a non-weak symbol with non-default visibility is not defined
10255 locally, it is a fatal error. */
10256 if (!bfd_link_relocatable (flinfo->info)
10257 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10258 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10259 && h->root.type == bfd_link_hash_undefined
10260 && !h->def_regular)
10261 {
10262 const char *msg;
10263
10264 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10265 /* xgettext:c-format */
10266 msg = _("%pB: protected symbol `%s' isn't defined");
10267 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10268 /* xgettext:c-format */
10269 msg = _("%pB: internal symbol `%s' isn't defined");
10270 else
10271 /* xgettext:c-format */
10272 msg = _("%pB: hidden symbol `%s' isn't defined");
10273 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10274 bfd_set_error (bfd_error_bad_value);
10275 eoinfo->failed = TRUE;
10276 return FALSE;
10277 }
10278
10279 /* If this symbol should be put in the .dynsym section, then put it
10280 there now. We already know the symbol index. We also fill in
10281 the entry in the .hash section. */
10282 if (h->dynindx != -1
10283 && elf_hash_table (flinfo->info)->dynamic_sections_created
10284 && elf_hash_table (flinfo->info)->dynsym != NULL
10285 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10286 {
10287 bfd_byte *esym;
10288
10289 /* Since there is no version information in the dynamic string,
10290 if there is no version info in symbol version section, we will
10291 have a run-time problem if not linking executable, referenced
10292 by shared library, or not bound locally. */
10293 if (h->verinfo.verdef == NULL
10294 && (!bfd_link_executable (flinfo->info)
10295 || h->ref_dynamic
10296 || !h->def_regular))
10297 {
10298 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10299
10300 if (p && p [1] != '\0')
10301 {
10302 _bfd_error_handler
10303 /* xgettext:c-format */
10304 (_("%pB: no symbol version section for versioned symbol `%s'"),
10305 flinfo->output_bfd, h->root.root.string);
10306 eoinfo->failed = TRUE;
10307 return FALSE;
10308 }
10309 }
10310
10311 sym.st_name = h->dynstr_index;
10312 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10313 + h->dynindx * bed->s->sizeof_sym);
10314 if (!check_dynsym (flinfo->output_bfd, &sym))
10315 {
10316 eoinfo->failed = TRUE;
10317 return FALSE;
10318 }
10319 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10320
10321 if (flinfo->hash_sec != NULL)
10322 {
10323 size_t hash_entry_size;
10324 bfd_byte *bucketpos;
10325 bfd_vma chain;
10326 size_t bucketcount;
10327 size_t bucket;
10328
10329 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10330 bucket = h->u.elf_hash_value % bucketcount;
10331
10332 hash_entry_size
10333 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10334 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10335 + (bucket + 2) * hash_entry_size);
10336 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10337 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10338 bucketpos);
10339 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10340 ((bfd_byte *) flinfo->hash_sec->contents
10341 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10342 }
10343
10344 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10345 {
10346 Elf_Internal_Versym iversym;
10347 Elf_External_Versym *eversym;
10348
10349 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10350 {
10351 if (h->verinfo.verdef == NULL
10352 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10353 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10354 iversym.vs_vers = 0;
10355 else
10356 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10357 }
10358 else
10359 {
10360 if (h->verinfo.vertree == NULL)
10361 iversym.vs_vers = 1;
10362 else
10363 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10364 if (flinfo->info->create_default_symver)
10365 iversym.vs_vers++;
10366 }
10367
10368 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10369 defined locally. */
10370 if (h->versioned == versioned_hidden && h->def_regular)
10371 iversym.vs_vers |= VERSYM_HIDDEN;
10372
10373 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10374 eversym += h->dynindx;
10375 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10376 }
10377 }
10378
10379 /* If the symbol is undefined, and we didn't output it to .dynsym,
10380 strip it from .symtab too. Obviously we can't do this for
10381 relocatable output or when needed for --emit-relocs. */
10382 else if (input_sec == bfd_und_section_ptr
10383 && h->indx != -2
10384 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10385 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10386 && !bfd_link_relocatable (flinfo->info))
10387 return TRUE;
10388
10389 /* Also strip others that we couldn't earlier due to dynamic symbol
10390 processing. */
10391 if (strip)
10392 return TRUE;
10393 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10394 return TRUE;
10395
10396 /* Output a FILE symbol so that following locals are not associated
10397 with the wrong input file. We need one for forced local symbols
10398 if we've seen more than one FILE symbol or when we have exactly
10399 one FILE symbol but global symbols are present in a file other
10400 than the one with the FILE symbol. We also need one if linker
10401 defined symbols are present. In practice these conditions are
10402 always met, so just emit the FILE symbol unconditionally. */
10403 if (eoinfo->localsyms
10404 && !eoinfo->file_sym_done
10405 && eoinfo->flinfo->filesym_count != 0)
10406 {
10407 Elf_Internal_Sym fsym;
10408
10409 memset (&fsym, 0, sizeof (fsym));
10410 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10411 fsym.st_shndx = SHN_ABS;
10412 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10413 bfd_und_section_ptr, NULL))
10414 return FALSE;
10415
10416 eoinfo->file_sym_done = TRUE;
10417 }
10418
10419 indx = bfd_get_symcount (flinfo->output_bfd);
10420 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10421 input_sec, h);
10422 if (ret == 0)
10423 {
10424 eoinfo->failed = TRUE;
10425 return FALSE;
10426 }
10427 else if (ret == 1)
10428 h->indx = indx;
10429 else if (h->indx == -2)
10430 abort();
10431
10432 return TRUE;
10433 }
10434
10435 /* Return TRUE if special handling is done for relocs in SEC against
10436 symbols defined in discarded sections. */
10437
10438 static bfd_boolean
10439 elf_section_ignore_discarded_relocs (asection *sec)
10440 {
10441 const struct elf_backend_data *bed;
10442
10443 switch (sec->sec_info_type)
10444 {
10445 case SEC_INFO_TYPE_STABS:
10446 case SEC_INFO_TYPE_EH_FRAME:
10447 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10448 return TRUE;
10449 default:
10450 break;
10451 }
10452
10453 bed = get_elf_backend_data (sec->owner);
10454 if (bed->elf_backend_ignore_discarded_relocs != NULL
10455 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10456 return TRUE;
10457
10458 return FALSE;
10459 }
10460
10461 /* Return a mask saying how ld should treat relocations in SEC against
10462 symbols defined in discarded sections. If this function returns
10463 COMPLAIN set, ld will issue a warning message. If this function
10464 returns PRETEND set, and the discarded section was link-once and the
10465 same size as the kept link-once section, ld will pretend that the
10466 symbol was actually defined in the kept section. Otherwise ld will
10467 zero the reloc (at least that is the intent, but some cooperation by
10468 the target dependent code is needed, particularly for REL targets). */
10469
10470 unsigned int
10471 _bfd_elf_default_action_discarded (asection *sec)
10472 {
10473 if (sec->flags & SEC_DEBUGGING)
10474 return PRETEND;
10475
10476 if (strcmp (".eh_frame", sec->name) == 0)
10477 return 0;
10478
10479 if (strcmp (".gcc_except_table", sec->name) == 0)
10480 return 0;
10481
10482 return COMPLAIN | PRETEND;
10483 }
10484
10485 /* Find a match between a section and a member of a section group. */
10486
10487 static asection *
10488 match_group_member (asection *sec, asection *group,
10489 struct bfd_link_info *info)
10490 {
10491 asection *first = elf_next_in_group (group);
10492 asection *s = first;
10493
10494 while (s != NULL)
10495 {
10496 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10497 return s;
10498
10499 s = elf_next_in_group (s);
10500 if (s == first)
10501 break;
10502 }
10503
10504 return NULL;
10505 }
10506
10507 /* Check if the kept section of a discarded section SEC can be used
10508 to replace it. Return the replacement if it is OK. Otherwise return
10509 NULL. */
10510
10511 asection *
10512 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10513 {
10514 asection *kept;
10515
10516 kept = sec->kept_section;
10517 if (kept != NULL)
10518 {
10519 if ((kept->flags & SEC_GROUP) != 0)
10520 kept = match_group_member (sec, kept, info);
10521 if (kept != NULL
10522 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10523 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10524 kept = NULL;
10525 sec->kept_section = kept;
10526 }
10527 return kept;
10528 }
10529
10530 /* Link an input file into the linker output file. This function
10531 handles all the sections and relocations of the input file at once.
10532 This is so that we only have to read the local symbols once, and
10533 don't have to keep them in memory. */
10534
10535 static bfd_boolean
10536 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10537 {
10538 int (*relocate_section)
10539 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10540 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10541 bfd *output_bfd;
10542 Elf_Internal_Shdr *symtab_hdr;
10543 size_t locsymcount;
10544 size_t extsymoff;
10545 Elf_Internal_Sym *isymbuf;
10546 Elf_Internal_Sym *isym;
10547 Elf_Internal_Sym *isymend;
10548 long *pindex;
10549 asection **ppsection;
10550 asection *o;
10551 const struct elf_backend_data *bed;
10552 struct elf_link_hash_entry **sym_hashes;
10553 bfd_size_type address_size;
10554 bfd_vma r_type_mask;
10555 int r_sym_shift;
10556 bfd_boolean have_file_sym = FALSE;
10557
10558 output_bfd = flinfo->output_bfd;
10559 bed = get_elf_backend_data (output_bfd);
10560 relocate_section = bed->elf_backend_relocate_section;
10561
10562 /* If this is a dynamic object, we don't want to do anything here:
10563 we don't want the local symbols, and we don't want the section
10564 contents. */
10565 if ((input_bfd->flags & DYNAMIC) != 0)
10566 return TRUE;
10567
10568 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10569 if (elf_bad_symtab (input_bfd))
10570 {
10571 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10572 extsymoff = 0;
10573 }
10574 else
10575 {
10576 locsymcount = symtab_hdr->sh_info;
10577 extsymoff = symtab_hdr->sh_info;
10578 }
10579
10580 /* Read the local symbols. */
10581 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10582 if (isymbuf == NULL && locsymcount != 0)
10583 {
10584 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10585 flinfo->internal_syms,
10586 flinfo->external_syms,
10587 flinfo->locsym_shndx);
10588 if (isymbuf == NULL)
10589 return FALSE;
10590 }
10591
10592 /* Find local symbol sections and adjust values of symbols in
10593 SEC_MERGE sections. Write out those local symbols we know are
10594 going into the output file. */
10595 isymend = isymbuf + locsymcount;
10596 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10597 isym < isymend;
10598 isym++, pindex++, ppsection++)
10599 {
10600 asection *isec;
10601 const char *name;
10602 Elf_Internal_Sym osym;
10603 long indx;
10604 int ret;
10605
10606 *pindex = -1;
10607
10608 if (elf_bad_symtab (input_bfd))
10609 {
10610 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10611 {
10612 *ppsection = NULL;
10613 continue;
10614 }
10615 }
10616
10617 if (isym->st_shndx == SHN_UNDEF)
10618 isec = bfd_und_section_ptr;
10619 else if (isym->st_shndx == SHN_ABS)
10620 isec = bfd_abs_section_ptr;
10621 else if (isym->st_shndx == SHN_COMMON)
10622 isec = bfd_com_section_ptr;
10623 else
10624 {
10625 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10626 if (isec == NULL)
10627 {
10628 /* Don't attempt to output symbols with st_shnx in the
10629 reserved range other than SHN_ABS and SHN_COMMON. */
10630 isec = bfd_und_section_ptr;
10631 }
10632 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10633 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10634 isym->st_value =
10635 _bfd_merged_section_offset (output_bfd, &isec,
10636 elf_section_data (isec)->sec_info,
10637 isym->st_value);
10638 }
10639
10640 *ppsection = isec;
10641
10642 /* Don't output the first, undefined, symbol. In fact, don't
10643 output any undefined local symbol. */
10644 if (isec == bfd_und_section_ptr)
10645 continue;
10646
10647 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10648 {
10649 /* We never output section symbols. Instead, we use the
10650 section symbol of the corresponding section in the output
10651 file. */
10652 continue;
10653 }
10654
10655 /* If we are stripping all symbols, we don't want to output this
10656 one. */
10657 if (flinfo->info->strip == strip_all)
10658 continue;
10659
10660 /* If we are discarding all local symbols, we don't want to
10661 output this one. If we are generating a relocatable output
10662 file, then some of the local symbols may be required by
10663 relocs; we output them below as we discover that they are
10664 needed. */
10665 if (flinfo->info->discard == discard_all)
10666 continue;
10667
10668 /* If this symbol is defined in a section which we are
10669 discarding, we don't need to keep it. */
10670 if (isym->st_shndx != SHN_UNDEF
10671 && isym->st_shndx < SHN_LORESERVE
10672 && isec->output_section == NULL
10673 && flinfo->info->non_contiguous_regions
10674 && flinfo->info->non_contiguous_regions_warnings)
10675 {
10676 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10677 "discards section `%s' from '%s'\n"),
10678 isec->name, isec->owner->filename);
10679 continue;
10680 }
10681
10682 if (isym->st_shndx != SHN_UNDEF
10683 && isym->st_shndx < SHN_LORESERVE
10684 && bfd_section_removed_from_list (output_bfd,
10685 isec->output_section))
10686 continue;
10687
10688 /* Get the name of the symbol. */
10689 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10690 isym->st_name);
10691 if (name == NULL)
10692 return FALSE;
10693
10694 /* See if we are discarding symbols with this name. */
10695 if ((flinfo->info->strip == strip_some
10696 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10697 == NULL))
10698 || (((flinfo->info->discard == discard_sec_merge
10699 && (isec->flags & SEC_MERGE)
10700 && !bfd_link_relocatable (flinfo->info))
10701 || flinfo->info->discard == discard_l)
10702 && bfd_is_local_label_name (input_bfd, name)))
10703 continue;
10704
10705 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10706 {
10707 if (input_bfd->lto_output)
10708 /* -flto puts a temp file name here. This means builds
10709 are not reproducible. Discard the symbol. */
10710 continue;
10711 have_file_sym = TRUE;
10712 flinfo->filesym_count += 1;
10713 }
10714 if (!have_file_sym)
10715 {
10716 /* In the absence of debug info, bfd_find_nearest_line uses
10717 FILE symbols to determine the source file for local
10718 function symbols. Provide a FILE symbol here if input
10719 files lack such, so that their symbols won't be
10720 associated with a previous input file. It's not the
10721 source file, but the best we can do. */
10722 have_file_sym = TRUE;
10723 flinfo->filesym_count += 1;
10724 memset (&osym, 0, sizeof (osym));
10725 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10726 osym.st_shndx = SHN_ABS;
10727 if (!elf_link_output_symstrtab (flinfo,
10728 (input_bfd->lto_output ? NULL
10729 : input_bfd->filename),
10730 &osym, bfd_abs_section_ptr,
10731 NULL))
10732 return FALSE;
10733 }
10734
10735 osym = *isym;
10736
10737 /* Adjust the section index for the output file. */
10738 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10739 isec->output_section);
10740 if (osym.st_shndx == SHN_BAD)
10741 return FALSE;
10742
10743 /* ELF symbols in relocatable files are section relative, but
10744 in executable files they are virtual addresses. Note that
10745 this code assumes that all ELF sections have an associated
10746 BFD section with a reasonable value for output_offset; below
10747 we assume that they also have a reasonable value for
10748 output_section. Any special sections must be set up to meet
10749 these requirements. */
10750 osym.st_value += isec->output_offset;
10751 if (!bfd_link_relocatable (flinfo->info))
10752 {
10753 osym.st_value += isec->output_section->vma;
10754 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10755 {
10756 /* STT_TLS symbols are relative to PT_TLS segment base. */
10757 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10758 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10759 else
10760 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10761 STT_NOTYPE);
10762 }
10763 }
10764
10765 indx = bfd_get_symcount (output_bfd);
10766 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10767 if (ret == 0)
10768 return FALSE;
10769 else if (ret == 1)
10770 *pindex = indx;
10771 }
10772
10773 if (bed->s->arch_size == 32)
10774 {
10775 r_type_mask = 0xff;
10776 r_sym_shift = 8;
10777 address_size = 4;
10778 }
10779 else
10780 {
10781 r_type_mask = 0xffffffff;
10782 r_sym_shift = 32;
10783 address_size = 8;
10784 }
10785
10786 /* Relocate the contents of each section. */
10787 sym_hashes = elf_sym_hashes (input_bfd);
10788 for (o = input_bfd->sections; o != NULL; o = o->next)
10789 {
10790 bfd_byte *contents;
10791
10792 if (! o->linker_mark)
10793 {
10794 /* This section was omitted from the link. */
10795 continue;
10796 }
10797
10798 if (!flinfo->info->resolve_section_groups
10799 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10800 {
10801 /* Deal with the group signature symbol. */
10802 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10803 unsigned long symndx = sec_data->this_hdr.sh_info;
10804 asection *osec = o->output_section;
10805
10806 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10807 if (symndx >= locsymcount
10808 || (elf_bad_symtab (input_bfd)
10809 && flinfo->sections[symndx] == NULL))
10810 {
10811 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10812 while (h->root.type == bfd_link_hash_indirect
10813 || h->root.type == bfd_link_hash_warning)
10814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10815 /* Arrange for symbol to be output. */
10816 h->indx = -2;
10817 elf_section_data (osec)->this_hdr.sh_info = -2;
10818 }
10819 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10820 {
10821 /* We'll use the output section target_index. */
10822 asection *sec = flinfo->sections[symndx]->output_section;
10823 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10824 }
10825 else
10826 {
10827 if (flinfo->indices[symndx] == -1)
10828 {
10829 /* Otherwise output the local symbol now. */
10830 Elf_Internal_Sym sym = isymbuf[symndx];
10831 asection *sec = flinfo->sections[symndx]->output_section;
10832 const char *name;
10833 long indx;
10834 int ret;
10835
10836 name = bfd_elf_string_from_elf_section (input_bfd,
10837 symtab_hdr->sh_link,
10838 sym.st_name);
10839 if (name == NULL)
10840 return FALSE;
10841
10842 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10843 sec);
10844 if (sym.st_shndx == SHN_BAD)
10845 return FALSE;
10846
10847 sym.st_value += o->output_offset;
10848
10849 indx = bfd_get_symcount (output_bfd);
10850 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10851 NULL);
10852 if (ret == 0)
10853 return FALSE;
10854 else if (ret == 1)
10855 flinfo->indices[symndx] = indx;
10856 else
10857 abort ();
10858 }
10859 elf_section_data (osec)->this_hdr.sh_info
10860 = flinfo->indices[symndx];
10861 }
10862 }
10863
10864 if ((o->flags & SEC_HAS_CONTENTS) == 0
10865 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10866 continue;
10867
10868 if ((o->flags & SEC_LINKER_CREATED) != 0)
10869 {
10870 /* Section was created by _bfd_elf_link_create_dynamic_sections
10871 or somesuch. */
10872 continue;
10873 }
10874
10875 /* Get the contents of the section. They have been cached by a
10876 relaxation routine. Note that o is a section in an input
10877 file, so the contents field will not have been set by any of
10878 the routines which work on output files. */
10879 if (elf_section_data (o)->this_hdr.contents != NULL)
10880 {
10881 contents = elf_section_data (o)->this_hdr.contents;
10882 if (bed->caches_rawsize
10883 && o->rawsize != 0
10884 && o->rawsize < o->size)
10885 {
10886 memcpy (flinfo->contents, contents, o->rawsize);
10887 contents = flinfo->contents;
10888 }
10889 }
10890 else
10891 {
10892 contents = flinfo->contents;
10893 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10894 return FALSE;
10895 }
10896
10897 if ((o->flags & SEC_RELOC) != 0)
10898 {
10899 Elf_Internal_Rela *internal_relocs;
10900 Elf_Internal_Rela *rel, *relend;
10901 int action_discarded;
10902 int ret;
10903
10904 /* Get the swapped relocs. */
10905 internal_relocs
10906 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10907 flinfo->internal_relocs, FALSE);
10908 if (internal_relocs == NULL
10909 && o->reloc_count > 0)
10910 return FALSE;
10911
10912 /* We need to reverse-copy input .ctors/.dtors sections if
10913 they are placed in .init_array/.finit_array for output. */
10914 if (o->size > address_size
10915 && ((strncmp (o->name, ".ctors", 6) == 0
10916 && strcmp (o->output_section->name,
10917 ".init_array") == 0)
10918 || (strncmp (o->name, ".dtors", 6) == 0
10919 && strcmp (o->output_section->name,
10920 ".fini_array") == 0))
10921 && (o->name[6] == 0 || o->name[6] == '.'))
10922 {
10923 if (o->size * bed->s->int_rels_per_ext_rel
10924 != o->reloc_count * address_size)
10925 {
10926 _bfd_error_handler
10927 /* xgettext:c-format */
10928 (_("error: %pB: size of section %pA is not "
10929 "multiple of address size"),
10930 input_bfd, o);
10931 bfd_set_error (bfd_error_bad_value);
10932 return FALSE;
10933 }
10934 o->flags |= SEC_ELF_REVERSE_COPY;
10935 }
10936
10937 action_discarded = -1;
10938 if (!elf_section_ignore_discarded_relocs (o))
10939 action_discarded = (*bed->action_discarded) (o);
10940
10941 /* Run through the relocs evaluating complex reloc symbols and
10942 looking for relocs against symbols from discarded sections
10943 or section symbols from removed link-once sections.
10944 Complain about relocs against discarded sections. Zero
10945 relocs against removed link-once sections. */
10946
10947 rel = internal_relocs;
10948 relend = rel + o->reloc_count;
10949 for ( ; rel < relend; rel++)
10950 {
10951 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10952 unsigned int s_type;
10953 asection **ps, *sec;
10954 struct elf_link_hash_entry *h = NULL;
10955 const char *sym_name;
10956
10957 if (r_symndx == STN_UNDEF)
10958 continue;
10959
10960 if (r_symndx >= locsymcount
10961 || (elf_bad_symtab (input_bfd)
10962 && flinfo->sections[r_symndx] == NULL))
10963 {
10964 h = sym_hashes[r_symndx - extsymoff];
10965
10966 /* Badly formatted input files can contain relocs that
10967 reference non-existant symbols. Check here so that
10968 we do not seg fault. */
10969 if (h == NULL)
10970 {
10971 _bfd_error_handler
10972 /* xgettext:c-format */
10973 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10974 "that references a non-existent global symbol"),
10975 input_bfd, (uint64_t) rel->r_info, o);
10976 bfd_set_error (bfd_error_bad_value);
10977 return FALSE;
10978 }
10979
10980 while (h->root.type == bfd_link_hash_indirect
10981 || h->root.type == bfd_link_hash_warning)
10982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10983
10984 s_type = h->type;
10985
10986 /* If a plugin symbol is referenced from a non-IR file,
10987 mark the symbol as undefined. Note that the
10988 linker may attach linker created dynamic sections
10989 to the plugin bfd. Symbols defined in linker
10990 created sections are not plugin symbols. */
10991 if ((h->root.non_ir_ref_regular
10992 || h->root.non_ir_ref_dynamic)
10993 && (h->root.type == bfd_link_hash_defined
10994 || h->root.type == bfd_link_hash_defweak)
10995 && (h->root.u.def.section->flags
10996 & SEC_LINKER_CREATED) == 0
10997 && h->root.u.def.section->owner != NULL
10998 && (h->root.u.def.section->owner->flags
10999 & BFD_PLUGIN) != 0)
11000 {
11001 h->root.type = bfd_link_hash_undefined;
11002 h->root.u.undef.abfd = h->root.u.def.section->owner;
11003 }
11004
11005 ps = NULL;
11006 if (h->root.type == bfd_link_hash_defined
11007 || h->root.type == bfd_link_hash_defweak)
11008 ps = &h->root.u.def.section;
11009
11010 sym_name = h->root.root.string;
11011 }
11012 else
11013 {
11014 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11015
11016 s_type = ELF_ST_TYPE (sym->st_info);
11017 ps = &flinfo->sections[r_symndx];
11018 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11019 sym, *ps);
11020 }
11021
11022 if ((s_type == STT_RELC || s_type == STT_SRELC)
11023 && !bfd_link_relocatable (flinfo->info))
11024 {
11025 bfd_vma val;
11026 bfd_vma dot = (rel->r_offset
11027 + o->output_offset + o->output_section->vma);
11028 #ifdef DEBUG
11029 printf ("Encountered a complex symbol!");
11030 printf (" (input_bfd %s, section %s, reloc %ld\n",
11031 input_bfd->filename, o->name,
11032 (long) (rel - internal_relocs));
11033 printf (" symbol: idx %8.8lx, name %s\n",
11034 r_symndx, sym_name);
11035 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11036 (unsigned long) rel->r_info,
11037 (unsigned long) rel->r_offset);
11038 #endif
11039 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11040 isymbuf, locsymcount, s_type == STT_SRELC))
11041 return FALSE;
11042
11043 /* Symbol evaluated OK. Update to absolute value. */
11044 set_symbol_value (input_bfd, isymbuf, locsymcount,
11045 r_symndx, val);
11046 continue;
11047 }
11048
11049 if (action_discarded != -1 && ps != NULL)
11050 {
11051 /* Complain if the definition comes from a
11052 discarded section. */
11053 if ((sec = *ps) != NULL && discarded_section (sec))
11054 {
11055 BFD_ASSERT (r_symndx != STN_UNDEF);
11056 if (action_discarded & COMPLAIN)
11057 (*flinfo->info->callbacks->einfo)
11058 /* xgettext:c-format */
11059 (_("%X`%s' referenced in section `%pA' of %pB: "
11060 "defined in discarded section `%pA' of %pB\n"),
11061 sym_name, o, input_bfd, sec, sec->owner);
11062
11063 /* Try to do the best we can to support buggy old
11064 versions of gcc. Pretend that the symbol is
11065 really defined in the kept linkonce section.
11066 FIXME: This is quite broken. Modifying the
11067 symbol here means we will be changing all later
11068 uses of the symbol, not just in this section. */
11069 if (action_discarded & PRETEND)
11070 {
11071 asection *kept;
11072
11073 kept = _bfd_elf_check_kept_section (sec,
11074 flinfo->info);
11075 if (kept != NULL)
11076 {
11077 *ps = kept;
11078 continue;
11079 }
11080 }
11081 }
11082 }
11083 }
11084
11085 /* Relocate the section by invoking a back end routine.
11086
11087 The back end routine is responsible for adjusting the
11088 section contents as necessary, and (if using Rela relocs
11089 and generating a relocatable output file) adjusting the
11090 reloc addend as necessary.
11091
11092 The back end routine does not have to worry about setting
11093 the reloc address or the reloc symbol index.
11094
11095 The back end routine is given a pointer to the swapped in
11096 internal symbols, and can access the hash table entries
11097 for the external symbols via elf_sym_hashes (input_bfd).
11098
11099 When generating relocatable output, the back end routine
11100 must handle STB_LOCAL/STT_SECTION symbols specially. The
11101 output symbol is going to be a section symbol
11102 corresponding to the output section, which will require
11103 the addend to be adjusted. */
11104
11105 ret = (*relocate_section) (output_bfd, flinfo->info,
11106 input_bfd, o, contents,
11107 internal_relocs,
11108 isymbuf,
11109 flinfo->sections);
11110 if (!ret)
11111 return FALSE;
11112
11113 if (ret == 2
11114 || bfd_link_relocatable (flinfo->info)
11115 || flinfo->info->emitrelocations)
11116 {
11117 Elf_Internal_Rela *irela;
11118 Elf_Internal_Rela *irelaend, *irelamid;
11119 bfd_vma last_offset;
11120 struct elf_link_hash_entry **rel_hash;
11121 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11122 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11123 unsigned int next_erel;
11124 bfd_boolean rela_normal;
11125 struct bfd_elf_section_data *esdi, *esdo;
11126
11127 esdi = elf_section_data (o);
11128 esdo = elf_section_data (o->output_section);
11129 rela_normal = FALSE;
11130
11131 /* Adjust the reloc addresses and symbol indices. */
11132
11133 irela = internal_relocs;
11134 irelaend = irela + o->reloc_count;
11135 rel_hash = esdo->rel.hashes + esdo->rel.count;
11136 /* We start processing the REL relocs, if any. When we reach
11137 IRELAMID in the loop, we switch to the RELA relocs. */
11138 irelamid = irela;
11139 if (esdi->rel.hdr != NULL)
11140 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11141 * bed->s->int_rels_per_ext_rel);
11142 rel_hash_list = rel_hash;
11143 rela_hash_list = NULL;
11144 last_offset = o->output_offset;
11145 if (!bfd_link_relocatable (flinfo->info))
11146 last_offset += o->output_section->vma;
11147 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11148 {
11149 unsigned long r_symndx;
11150 asection *sec;
11151 Elf_Internal_Sym sym;
11152
11153 if (next_erel == bed->s->int_rels_per_ext_rel)
11154 {
11155 rel_hash++;
11156 next_erel = 0;
11157 }
11158
11159 if (irela == irelamid)
11160 {
11161 rel_hash = esdo->rela.hashes + esdo->rela.count;
11162 rela_hash_list = rel_hash;
11163 rela_normal = bed->rela_normal;
11164 }
11165
11166 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11167 flinfo->info, o,
11168 irela->r_offset);
11169 if (irela->r_offset >= (bfd_vma) -2)
11170 {
11171 /* This is a reloc for a deleted entry or somesuch.
11172 Turn it into an R_*_NONE reloc, at the same
11173 offset as the last reloc. elf_eh_frame.c and
11174 bfd_elf_discard_info rely on reloc offsets
11175 being ordered. */
11176 irela->r_offset = last_offset;
11177 irela->r_info = 0;
11178 irela->r_addend = 0;
11179 continue;
11180 }
11181
11182 irela->r_offset += o->output_offset;
11183
11184 /* Relocs in an executable have to be virtual addresses. */
11185 if (!bfd_link_relocatable (flinfo->info))
11186 irela->r_offset += o->output_section->vma;
11187
11188 last_offset = irela->r_offset;
11189
11190 r_symndx = irela->r_info >> r_sym_shift;
11191 if (r_symndx == STN_UNDEF)
11192 continue;
11193
11194 if (r_symndx >= locsymcount
11195 || (elf_bad_symtab (input_bfd)
11196 && flinfo->sections[r_symndx] == NULL))
11197 {
11198 struct elf_link_hash_entry *rh;
11199 unsigned long indx;
11200
11201 /* This is a reloc against a global symbol. We
11202 have not yet output all the local symbols, so
11203 we do not know the symbol index of any global
11204 symbol. We set the rel_hash entry for this
11205 reloc to point to the global hash table entry
11206 for this symbol. The symbol index is then
11207 set at the end of bfd_elf_final_link. */
11208 indx = r_symndx - extsymoff;
11209 rh = elf_sym_hashes (input_bfd)[indx];
11210 while (rh->root.type == bfd_link_hash_indirect
11211 || rh->root.type == bfd_link_hash_warning)
11212 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11213
11214 /* Setting the index to -2 tells
11215 elf_link_output_extsym that this symbol is
11216 used by a reloc. */
11217 BFD_ASSERT (rh->indx < 0);
11218 rh->indx = -2;
11219 *rel_hash = rh;
11220
11221 continue;
11222 }
11223
11224 /* This is a reloc against a local symbol. */
11225
11226 *rel_hash = NULL;
11227 sym = isymbuf[r_symndx];
11228 sec = flinfo->sections[r_symndx];
11229 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11230 {
11231 /* I suppose the backend ought to fill in the
11232 section of any STT_SECTION symbol against a
11233 processor specific section. */
11234 r_symndx = STN_UNDEF;
11235 if (bfd_is_abs_section (sec))
11236 ;
11237 else if (sec == NULL || sec->owner == NULL)
11238 {
11239 bfd_set_error (bfd_error_bad_value);
11240 return FALSE;
11241 }
11242 else
11243 {
11244 asection *osec = sec->output_section;
11245
11246 /* If we have discarded a section, the output
11247 section will be the absolute section. In
11248 case of discarded SEC_MERGE sections, use
11249 the kept section. relocate_section should
11250 have already handled discarded linkonce
11251 sections. */
11252 if (bfd_is_abs_section (osec)
11253 && sec->kept_section != NULL
11254 && sec->kept_section->output_section != NULL)
11255 {
11256 osec = sec->kept_section->output_section;
11257 irela->r_addend -= osec->vma;
11258 }
11259
11260 if (!bfd_is_abs_section (osec))
11261 {
11262 r_symndx = osec->target_index;
11263 if (r_symndx == STN_UNDEF)
11264 {
11265 irela->r_addend += osec->vma;
11266 osec = _bfd_nearby_section (output_bfd, osec,
11267 osec->vma);
11268 irela->r_addend -= osec->vma;
11269 r_symndx = osec->target_index;
11270 }
11271 }
11272 }
11273
11274 /* Adjust the addend according to where the
11275 section winds up in the output section. */
11276 if (rela_normal)
11277 irela->r_addend += sec->output_offset;
11278 }
11279 else
11280 {
11281 if (flinfo->indices[r_symndx] == -1)
11282 {
11283 unsigned long shlink;
11284 const char *name;
11285 asection *osec;
11286 long indx;
11287
11288 if (flinfo->info->strip == strip_all)
11289 {
11290 /* You can't do ld -r -s. */
11291 bfd_set_error (bfd_error_invalid_operation);
11292 return FALSE;
11293 }
11294
11295 /* This symbol was skipped earlier, but
11296 since it is needed by a reloc, we
11297 must output it now. */
11298 shlink = symtab_hdr->sh_link;
11299 name = (bfd_elf_string_from_elf_section
11300 (input_bfd, shlink, sym.st_name));
11301 if (name == NULL)
11302 return FALSE;
11303
11304 osec = sec->output_section;
11305 sym.st_shndx =
11306 _bfd_elf_section_from_bfd_section (output_bfd,
11307 osec);
11308 if (sym.st_shndx == SHN_BAD)
11309 return FALSE;
11310
11311 sym.st_value += sec->output_offset;
11312 if (!bfd_link_relocatable (flinfo->info))
11313 {
11314 sym.st_value += osec->vma;
11315 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11316 {
11317 struct elf_link_hash_table *htab
11318 = elf_hash_table (flinfo->info);
11319
11320 /* STT_TLS symbols are relative to PT_TLS
11321 segment base. */
11322 if (htab->tls_sec != NULL)
11323 sym.st_value -= htab->tls_sec->vma;
11324 else
11325 sym.st_info
11326 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11327 STT_NOTYPE);
11328 }
11329 }
11330
11331 indx = bfd_get_symcount (output_bfd);
11332 ret = elf_link_output_symstrtab (flinfo, name,
11333 &sym, sec,
11334 NULL);
11335 if (ret == 0)
11336 return FALSE;
11337 else if (ret == 1)
11338 flinfo->indices[r_symndx] = indx;
11339 else
11340 abort ();
11341 }
11342
11343 r_symndx = flinfo->indices[r_symndx];
11344 }
11345
11346 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11347 | (irela->r_info & r_type_mask));
11348 }
11349
11350 /* Swap out the relocs. */
11351 input_rel_hdr = esdi->rel.hdr;
11352 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11353 {
11354 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11355 input_rel_hdr,
11356 internal_relocs,
11357 rel_hash_list))
11358 return FALSE;
11359 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11360 * bed->s->int_rels_per_ext_rel);
11361 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11362 }
11363
11364 input_rela_hdr = esdi->rela.hdr;
11365 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11366 {
11367 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11368 input_rela_hdr,
11369 internal_relocs,
11370 rela_hash_list))
11371 return FALSE;
11372 }
11373 }
11374 }
11375
11376 /* Write out the modified section contents. */
11377 if (bed->elf_backend_write_section
11378 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11379 contents))
11380 {
11381 /* Section written out. */
11382 }
11383 else switch (o->sec_info_type)
11384 {
11385 case SEC_INFO_TYPE_STABS:
11386 if (! (_bfd_write_section_stabs
11387 (output_bfd,
11388 &elf_hash_table (flinfo->info)->stab_info,
11389 o, &elf_section_data (o)->sec_info, contents)))
11390 return FALSE;
11391 break;
11392 case SEC_INFO_TYPE_MERGE:
11393 if (! _bfd_write_merged_section (output_bfd, o,
11394 elf_section_data (o)->sec_info))
11395 return FALSE;
11396 break;
11397 case SEC_INFO_TYPE_EH_FRAME:
11398 {
11399 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11400 o, contents))
11401 return FALSE;
11402 }
11403 break;
11404 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11405 {
11406 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11407 flinfo->info,
11408 o, contents))
11409 return FALSE;
11410 }
11411 break;
11412 default:
11413 {
11414 if (! (o->flags & SEC_EXCLUDE))
11415 {
11416 file_ptr offset = (file_ptr) o->output_offset;
11417 bfd_size_type todo = o->size;
11418
11419 offset *= bfd_octets_per_byte (output_bfd, o);
11420
11421 if ((o->flags & SEC_ELF_REVERSE_COPY))
11422 {
11423 /* Reverse-copy input section to output. */
11424 do
11425 {
11426 todo -= address_size;
11427 if (! bfd_set_section_contents (output_bfd,
11428 o->output_section,
11429 contents + todo,
11430 offset,
11431 address_size))
11432 return FALSE;
11433 if (todo == 0)
11434 break;
11435 offset += address_size;
11436 }
11437 while (1);
11438 }
11439 else if (! bfd_set_section_contents (output_bfd,
11440 o->output_section,
11441 contents,
11442 offset, todo))
11443 return FALSE;
11444 }
11445 }
11446 break;
11447 }
11448 }
11449
11450 return TRUE;
11451 }
11452
11453 /* Generate a reloc when linking an ELF file. This is a reloc
11454 requested by the linker, and does not come from any input file. This
11455 is used to build constructor and destructor tables when linking
11456 with -Ur. */
11457
11458 static bfd_boolean
11459 elf_reloc_link_order (bfd *output_bfd,
11460 struct bfd_link_info *info,
11461 asection *output_section,
11462 struct bfd_link_order *link_order)
11463 {
11464 reloc_howto_type *howto;
11465 long indx;
11466 bfd_vma offset;
11467 bfd_vma addend;
11468 struct bfd_elf_section_reloc_data *reldata;
11469 struct elf_link_hash_entry **rel_hash_ptr;
11470 Elf_Internal_Shdr *rel_hdr;
11471 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11472 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11473 bfd_byte *erel;
11474 unsigned int i;
11475 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11476
11477 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11478 if (howto == NULL)
11479 {
11480 bfd_set_error (bfd_error_bad_value);
11481 return FALSE;
11482 }
11483
11484 addend = link_order->u.reloc.p->addend;
11485
11486 if (esdo->rel.hdr)
11487 reldata = &esdo->rel;
11488 else if (esdo->rela.hdr)
11489 reldata = &esdo->rela;
11490 else
11491 {
11492 reldata = NULL;
11493 BFD_ASSERT (0);
11494 }
11495
11496 /* Figure out the symbol index. */
11497 rel_hash_ptr = reldata->hashes + reldata->count;
11498 if (link_order->type == bfd_section_reloc_link_order)
11499 {
11500 indx = link_order->u.reloc.p->u.section->target_index;
11501 BFD_ASSERT (indx != 0);
11502 *rel_hash_ptr = NULL;
11503 }
11504 else
11505 {
11506 struct elf_link_hash_entry *h;
11507
11508 /* Treat a reloc against a defined symbol as though it were
11509 actually against the section. */
11510 h = ((struct elf_link_hash_entry *)
11511 bfd_wrapped_link_hash_lookup (output_bfd, info,
11512 link_order->u.reloc.p->u.name,
11513 FALSE, FALSE, TRUE));
11514 if (h != NULL
11515 && (h->root.type == bfd_link_hash_defined
11516 || h->root.type == bfd_link_hash_defweak))
11517 {
11518 asection *section;
11519
11520 section = h->root.u.def.section;
11521 indx = section->output_section->target_index;
11522 *rel_hash_ptr = NULL;
11523 /* It seems that we ought to add the symbol value to the
11524 addend here, but in practice it has already been added
11525 because it was passed to constructor_callback. */
11526 addend += section->output_section->vma + section->output_offset;
11527 }
11528 else if (h != NULL)
11529 {
11530 /* Setting the index to -2 tells elf_link_output_extsym that
11531 this symbol is used by a reloc. */
11532 h->indx = -2;
11533 *rel_hash_ptr = h;
11534 indx = 0;
11535 }
11536 else
11537 {
11538 (*info->callbacks->unattached_reloc)
11539 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11540 indx = 0;
11541 }
11542 }
11543
11544 /* If this is an inplace reloc, we must write the addend into the
11545 object file. */
11546 if (howto->partial_inplace && addend != 0)
11547 {
11548 bfd_size_type size;
11549 bfd_reloc_status_type rstat;
11550 bfd_byte *buf;
11551 bfd_boolean ok;
11552 const char *sym_name;
11553 bfd_size_type octets;
11554
11555 size = (bfd_size_type) bfd_get_reloc_size (howto);
11556 buf = (bfd_byte *) bfd_zmalloc (size);
11557 if (buf == NULL && size != 0)
11558 return FALSE;
11559 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11560 switch (rstat)
11561 {
11562 case bfd_reloc_ok:
11563 break;
11564
11565 default:
11566 case bfd_reloc_outofrange:
11567 abort ();
11568
11569 case bfd_reloc_overflow:
11570 if (link_order->type == bfd_section_reloc_link_order)
11571 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11572 else
11573 sym_name = link_order->u.reloc.p->u.name;
11574 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11575 howto->name, addend, NULL, NULL,
11576 (bfd_vma) 0);
11577 break;
11578 }
11579
11580 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11581 output_section);
11582 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11583 octets, size);
11584 free (buf);
11585 if (! ok)
11586 return FALSE;
11587 }
11588
11589 /* The address of a reloc is relative to the section in a
11590 relocatable file, and is a virtual address in an executable
11591 file. */
11592 offset = link_order->offset;
11593 if (! bfd_link_relocatable (info))
11594 offset += output_section->vma;
11595
11596 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11597 {
11598 irel[i].r_offset = offset;
11599 irel[i].r_info = 0;
11600 irel[i].r_addend = 0;
11601 }
11602 if (bed->s->arch_size == 32)
11603 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11604 else
11605 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11606
11607 rel_hdr = reldata->hdr;
11608 erel = rel_hdr->contents;
11609 if (rel_hdr->sh_type == SHT_REL)
11610 {
11611 erel += reldata->count * bed->s->sizeof_rel;
11612 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11613 }
11614 else
11615 {
11616 irel[0].r_addend = addend;
11617 erel += reldata->count * bed->s->sizeof_rela;
11618 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11619 }
11620
11621 ++reldata->count;
11622
11623 return TRUE;
11624 }
11625
11626
11627 /* Compare two sections based on the locations of the sections they are
11628 linked to. Used by elf_fixup_link_order. */
11629
11630 static int
11631 compare_link_order (const void *a, const void *b)
11632 {
11633 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11634 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11635 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11636 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11637 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11638 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11639
11640 if (apos < bpos)
11641 return -1;
11642 if (apos > bpos)
11643 return 1;
11644
11645 /* The only way we should get matching LMAs is when the first of two
11646 sections has zero size. */
11647 if (asec->size < bsec->size)
11648 return -1;
11649 if (asec->size > bsec->size)
11650 return 1;
11651
11652 /* If they are both zero size then they almost certainly have the same
11653 VMA and thus are not ordered with respect to each other. Test VMA
11654 anyway, and fall back to id to make the result reproducible across
11655 qsort implementations. */
11656 apos = asec->output_section->vma + asec->output_offset;
11657 bpos = bsec->output_section->vma + bsec->output_offset;
11658 if (apos < bpos)
11659 return -1;
11660 if (apos > bpos)
11661 return 1;
11662
11663 return asec->id - bsec->id;
11664 }
11665
11666
11667 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11668 order as their linked sections. Returns false if this could not be done
11669 because an output section includes both ordered and unordered
11670 sections. Ideally we'd do this in the linker proper. */
11671
11672 static bfd_boolean
11673 elf_fixup_link_order (bfd *abfd, asection *o)
11674 {
11675 size_t seen_linkorder;
11676 size_t seen_other;
11677 size_t n;
11678 struct bfd_link_order *p;
11679 bfd *sub;
11680 struct bfd_link_order **sections;
11681 asection *other_sec, *linkorder_sec;
11682 bfd_vma offset; /* Octets. */
11683
11684 other_sec = NULL;
11685 linkorder_sec = NULL;
11686 seen_other = 0;
11687 seen_linkorder = 0;
11688 for (p = o->map_head.link_order; p != NULL; p = p->next)
11689 {
11690 if (p->type == bfd_indirect_link_order)
11691 {
11692 asection *s = p->u.indirect.section;
11693 sub = s->owner;
11694 if ((s->flags & SEC_LINKER_CREATED) == 0
11695 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11696 && elf_section_data (s) != NULL
11697 && elf_linked_to_section (s) != NULL)
11698 {
11699 seen_linkorder++;
11700 linkorder_sec = s;
11701 }
11702 else
11703 {
11704 seen_other++;
11705 other_sec = s;
11706 }
11707 }
11708 else
11709 seen_other++;
11710
11711 if (seen_other && seen_linkorder)
11712 {
11713 if (other_sec && linkorder_sec)
11714 _bfd_error_handler
11715 /* xgettext:c-format */
11716 (_("%pA has both ordered [`%pA' in %pB] "
11717 "and unordered [`%pA' in %pB] sections"),
11718 o, linkorder_sec, linkorder_sec->owner,
11719 other_sec, other_sec->owner);
11720 else
11721 _bfd_error_handler
11722 (_("%pA has both ordered and unordered sections"), o);
11723 bfd_set_error (bfd_error_bad_value);
11724 return FALSE;
11725 }
11726 }
11727
11728 if (!seen_linkorder)
11729 return TRUE;
11730
11731 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11732 if (sections == NULL)
11733 return FALSE;
11734
11735 seen_linkorder = 0;
11736 for (p = o->map_head.link_order; p != NULL; p = p->next)
11737 sections[seen_linkorder++] = p;
11738
11739 /* Sort the input sections in the order of their linked section. */
11740 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11741
11742 /* Change the offsets of the sections. */
11743 offset = 0;
11744 for (n = 0; n < seen_linkorder; n++)
11745 {
11746 bfd_vma mask;
11747 asection *s = sections[n]->u.indirect.section;
11748 unsigned int opb = bfd_octets_per_byte (abfd, s);
11749
11750 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11751 offset = (offset + ~mask) & mask;
11752 sections[n]->offset = s->output_offset = offset / opb;
11753 offset += sections[n]->size;
11754 }
11755
11756 free (sections);
11757 return TRUE;
11758 }
11759
11760 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11761 Returns TRUE upon success, FALSE otherwise. */
11762
11763 static bfd_boolean
11764 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11765 {
11766 bfd_boolean ret = FALSE;
11767 bfd *implib_bfd;
11768 const struct elf_backend_data *bed;
11769 flagword flags;
11770 enum bfd_architecture arch;
11771 unsigned int mach;
11772 asymbol **sympp = NULL;
11773 long symsize;
11774 long symcount;
11775 long src_count;
11776 elf_symbol_type *osymbuf;
11777 size_t amt;
11778
11779 implib_bfd = info->out_implib_bfd;
11780 bed = get_elf_backend_data (abfd);
11781
11782 if (!bfd_set_format (implib_bfd, bfd_object))
11783 return FALSE;
11784
11785 /* Use flag from executable but make it a relocatable object. */
11786 flags = bfd_get_file_flags (abfd);
11787 flags &= ~HAS_RELOC;
11788 if (!bfd_set_start_address (implib_bfd, 0)
11789 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11790 return FALSE;
11791
11792 /* Copy architecture of output file to import library file. */
11793 arch = bfd_get_arch (abfd);
11794 mach = bfd_get_mach (abfd);
11795 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11796 && (abfd->target_defaulted
11797 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11798 return FALSE;
11799
11800 /* Get symbol table size. */
11801 symsize = bfd_get_symtab_upper_bound (abfd);
11802 if (symsize < 0)
11803 return FALSE;
11804
11805 /* Read in the symbol table. */
11806 sympp = (asymbol **) bfd_malloc (symsize);
11807 if (sympp == NULL)
11808 return FALSE;
11809
11810 symcount = bfd_canonicalize_symtab (abfd, sympp);
11811 if (symcount < 0)
11812 goto free_sym_buf;
11813
11814 /* Allow the BFD backend to copy any private header data it
11815 understands from the output BFD to the import library BFD. */
11816 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11817 goto free_sym_buf;
11818
11819 /* Filter symbols to appear in the import library. */
11820 if (bed->elf_backend_filter_implib_symbols)
11821 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11822 symcount);
11823 else
11824 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11825 if (symcount == 0)
11826 {
11827 bfd_set_error (bfd_error_no_symbols);
11828 _bfd_error_handler (_("%pB: no symbol found for import library"),
11829 implib_bfd);
11830 goto free_sym_buf;
11831 }
11832
11833
11834 /* Make symbols absolute. */
11835 amt = symcount * sizeof (*osymbuf);
11836 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
11837 if (osymbuf == NULL)
11838 goto free_sym_buf;
11839
11840 for (src_count = 0; src_count < symcount; src_count++)
11841 {
11842 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11843 sizeof (*osymbuf));
11844 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11845 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11846 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11847 osymbuf[src_count].internal_elf_sym.st_value =
11848 osymbuf[src_count].symbol.value;
11849 sympp[src_count] = &osymbuf[src_count].symbol;
11850 }
11851
11852 bfd_set_symtab (implib_bfd, sympp, symcount);
11853
11854 /* Allow the BFD backend to copy any private data it understands
11855 from the output BFD to the import library BFD. This is done last
11856 to permit the routine to look at the filtered symbol table. */
11857 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11858 goto free_sym_buf;
11859
11860 if (!bfd_close (implib_bfd))
11861 goto free_sym_buf;
11862
11863 ret = TRUE;
11864
11865 free_sym_buf:
11866 free (sympp);
11867 return ret;
11868 }
11869
11870 static void
11871 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11872 {
11873 asection *o;
11874
11875 if (flinfo->symstrtab != NULL)
11876 _bfd_elf_strtab_free (flinfo->symstrtab);
11877 if (flinfo->contents != NULL)
11878 free (flinfo->contents);
11879 if (flinfo->external_relocs != NULL)
11880 free (flinfo->external_relocs);
11881 if (flinfo->internal_relocs != NULL)
11882 free (flinfo->internal_relocs);
11883 if (flinfo->external_syms != NULL)
11884 free (flinfo->external_syms);
11885 if (flinfo->locsym_shndx != NULL)
11886 free (flinfo->locsym_shndx);
11887 if (flinfo->internal_syms != NULL)
11888 free (flinfo->internal_syms);
11889 if (flinfo->indices != NULL)
11890 free (flinfo->indices);
11891 if (flinfo->sections != NULL)
11892 free (flinfo->sections);
11893 if (flinfo->symshndxbuf != NULL
11894 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11895 free (flinfo->symshndxbuf);
11896 for (o = obfd->sections; o != NULL; o = o->next)
11897 {
11898 struct bfd_elf_section_data *esdo = elf_section_data (o);
11899 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11900 free (esdo->rel.hashes);
11901 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11902 free (esdo->rela.hashes);
11903 }
11904 }
11905
11906 /* Do the final step of an ELF link. */
11907
11908 bfd_boolean
11909 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11910 {
11911 bfd_boolean dynamic;
11912 bfd_boolean emit_relocs;
11913 bfd *dynobj;
11914 struct elf_final_link_info flinfo;
11915 asection *o;
11916 struct bfd_link_order *p;
11917 bfd *sub;
11918 bfd_size_type max_contents_size;
11919 bfd_size_type max_external_reloc_size;
11920 bfd_size_type max_internal_reloc_count;
11921 bfd_size_type max_sym_count;
11922 bfd_size_type max_sym_shndx_count;
11923 Elf_Internal_Sym elfsym;
11924 unsigned int i;
11925 Elf_Internal_Shdr *symtab_hdr;
11926 Elf_Internal_Shdr *symtab_shndx_hdr;
11927 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11928 struct elf_outext_info eoinfo;
11929 bfd_boolean merged;
11930 size_t relativecount = 0;
11931 asection *reldyn = 0;
11932 bfd_size_type amt;
11933 asection *attr_section = NULL;
11934 bfd_vma attr_size = 0;
11935 const char *std_attrs_section;
11936 struct elf_link_hash_table *htab = elf_hash_table (info);
11937 bfd_boolean sections_removed;
11938
11939 if (!is_elf_hash_table (htab))
11940 return FALSE;
11941
11942 if (bfd_link_pic (info))
11943 abfd->flags |= DYNAMIC;
11944
11945 dynamic = htab->dynamic_sections_created;
11946 dynobj = htab->dynobj;
11947
11948 emit_relocs = (bfd_link_relocatable (info)
11949 || info->emitrelocations);
11950
11951 flinfo.info = info;
11952 flinfo.output_bfd = abfd;
11953 flinfo.symstrtab = _bfd_elf_strtab_init ();
11954 if (flinfo.symstrtab == NULL)
11955 return FALSE;
11956
11957 if (! dynamic)
11958 {
11959 flinfo.hash_sec = NULL;
11960 flinfo.symver_sec = NULL;
11961 }
11962 else
11963 {
11964 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11965 /* Note that dynsym_sec can be NULL (on VMS). */
11966 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11967 /* Note that it is OK if symver_sec is NULL. */
11968 }
11969
11970 flinfo.contents = NULL;
11971 flinfo.external_relocs = NULL;
11972 flinfo.internal_relocs = NULL;
11973 flinfo.external_syms = NULL;
11974 flinfo.locsym_shndx = NULL;
11975 flinfo.internal_syms = NULL;
11976 flinfo.indices = NULL;
11977 flinfo.sections = NULL;
11978 flinfo.symshndxbuf = NULL;
11979 flinfo.filesym_count = 0;
11980
11981 /* The object attributes have been merged. Remove the input
11982 sections from the link, and set the contents of the output
11983 section. */
11984 sections_removed = FALSE;
11985 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11986 for (o = abfd->sections; o != NULL; o = o->next)
11987 {
11988 bfd_boolean remove_section = FALSE;
11989
11990 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11991 || strcmp (o->name, ".gnu.attributes") == 0)
11992 {
11993 for (p = o->map_head.link_order; p != NULL; p = p->next)
11994 {
11995 asection *input_section;
11996
11997 if (p->type != bfd_indirect_link_order)
11998 continue;
11999 input_section = p->u.indirect.section;
12000 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12001 elf_link_input_bfd ignores this section. */
12002 input_section->flags &= ~SEC_HAS_CONTENTS;
12003 }
12004
12005 attr_size = bfd_elf_obj_attr_size (abfd);
12006 bfd_set_section_size (o, attr_size);
12007 /* Skip this section later on. */
12008 o->map_head.link_order = NULL;
12009 if (attr_size)
12010 attr_section = o;
12011 else
12012 remove_section = TRUE;
12013 }
12014 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12015 {
12016 /* Remove empty group section from linker output. */
12017 remove_section = TRUE;
12018 }
12019 if (remove_section)
12020 {
12021 o->flags |= SEC_EXCLUDE;
12022 bfd_section_list_remove (abfd, o);
12023 abfd->section_count--;
12024 sections_removed = TRUE;
12025 }
12026 }
12027 if (sections_removed)
12028 _bfd_fix_excluded_sec_syms (abfd, info);
12029
12030 /* Count up the number of relocations we will output for each output
12031 section, so that we know the sizes of the reloc sections. We
12032 also figure out some maximum sizes. */
12033 max_contents_size = 0;
12034 max_external_reloc_size = 0;
12035 max_internal_reloc_count = 0;
12036 max_sym_count = 0;
12037 max_sym_shndx_count = 0;
12038 merged = FALSE;
12039 for (o = abfd->sections; o != NULL; o = o->next)
12040 {
12041 struct bfd_elf_section_data *esdo = elf_section_data (o);
12042 o->reloc_count = 0;
12043
12044 for (p = o->map_head.link_order; p != NULL; p = p->next)
12045 {
12046 unsigned int reloc_count = 0;
12047 unsigned int additional_reloc_count = 0;
12048 struct bfd_elf_section_data *esdi = NULL;
12049
12050 if (p->type == bfd_section_reloc_link_order
12051 || p->type == bfd_symbol_reloc_link_order)
12052 reloc_count = 1;
12053 else if (p->type == bfd_indirect_link_order)
12054 {
12055 asection *sec;
12056
12057 sec = p->u.indirect.section;
12058
12059 /* Mark all sections which are to be included in the
12060 link. This will normally be every section. We need
12061 to do this so that we can identify any sections which
12062 the linker has decided to not include. */
12063 sec->linker_mark = TRUE;
12064
12065 if (sec->flags & SEC_MERGE)
12066 merged = TRUE;
12067
12068 if (sec->rawsize > max_contents_size)
12069 max_contents_size = sec->rawsize;
12070 if (sec->size > max_contents_size)
12071 max_contents_size = sec->size;
12072
12073 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12074 && (sec->owner->flags & DYNAMIC) == 0)
12075 {
12076 size_t sym_count;
12077
12078 /* We are interested in just local symbols, not all
12079 symbols. */
12080 if (elf_bad_symtab (sec->owner))
12081 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12082 / bed->s->sizeof_sym);
12083 else
12084 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12085
12086 if (sym_count > max_sym_count)
12087 max_sym_count = sym_count;
12088
12089 if (sym_count > max_sym_shndx_count
12090 && elf_symtab_shndx_list (sec->owner) != NULL)
12091 max_sym_shndx_count = sym_count;
12092
12093 if (esdo->this_hdr.sh_type == SHT_REL
12094 || esdo->this_hdr.sh_type == SHT_RELA)
12095 /* Some backends use reloc_count in relocation sections
12096 to count particular types of relocs. Of course,
12097 reloc sections themselves can't have relocations. */
12098 ;
12099 else if (emit_relocs)
12100 {
12101 reloc_count = sec->reloc_count;
12102 if (bed->elf_backend_count_additional_relocs)
12103 {
12104 int c;
12105 c = (*bed->elf_backend_count_additional_relocs) (sec);
12106 additional_reloc_count += c;
12107 }
12108 }
12109 else if (bed->elf_backend_count_relocs)
12110 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12111
12112 esdi = elf_section_data (sec);
12113
12114 if ((sec->flags & SEC_RELOC) != 0)
12115 {
12116 size_t ext_size = 0;
12117
12118 if (esdi->rel.hdr != NULL)
12119 ext_size = esdi->rel.hdr->sh_size;
12120 if (esdi->rela.hdr != NULL)
12121 ext_size += esdi->rela.hdr->sh_size;
12122
12123 if (ext_size > max_external_reloc_size)
12124 max_external_reloc_size = ext_size;
12125 if (sec->reloc_count > max_internal_reloc_count)
12126 max_internal_reloc_count = sec->reloc_count;
12127 }
12128 }
12129 }
12130
12131 if (reloc_count == 0)
12132 continue;
12133
12134 reloc_count += additional_reloc_count;
12135 o->reloc_count += reloc_count;
12136
12137 if (p->type == bfd_indirect_link_order && emit_relocs)
12138 {
12139 if (esdi->rel.hdr)
12140 {
12141 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12142 esdo->rel.count += additional_reloc_count;
12143 }
12144 if (esdi->rela.hdr)
12145 {
12146 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12147 esdo->rela.count += additional_reloc_count;
12148 }
12149 }
12150 else
12151 {
12152 if (o->use_rela_p)
12153 esdo->rela.count += reloc_count;
12154 else
12155 esdo->rel.count += reloc_count;
12156 }
12157 }
12158
12159 if (o->reloc_count > 0)
12160 o->flags |= SEC_RELOC;
12161 else
12162 {
12163 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12164 set it (this is probably a bug) and if it is set
12165 assign_section_numbers will create a reloc section. */
12166 o->flags &=~ SEC_RELOC;
12167 }
12168
12169 /* If the SEC_ALLOC flag is not set, force the section VMA to
12170 zero. This is done in elf_fake_sections as well, but forcing
12171 the VMA to 0 here will ensure that relocs against these
12172 sections are handled correctly. */
12173 if ((o->flags & SEC_ALLOC) == 0
12174 && ! o->user_set_vma)
12175 o->vma = 0;
12176 }
12177
12178 if (! bfd_link_relocatable (info) && merged)
12179 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12180
12181 /* Figure out the file positions for everything but the symbol table
12182 and the relocs. We set symcount to force assign_section_numbers
12183 to create a symbol table. */
12184 abfd->symcount = info->strip != strip_all || emit_relocs;
12185 BFD_ASSERT (! abfd->output_has_begun);
12186 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12187 goto error_return;
12188
12189 /* Set sizes, and assign file positions for reloc sections. */
12190 for (o = abfd->sections; o != NULL; o = o->next)
12191 {
12192 struct bfd_elf_section_data *esdo = elf_section_data (o);
12193 if ((o->flags & SEC_RELOC) != 0)
12194 {
12195 if (esdo->rel.hdr
12196 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12197 goto error_return;
12198
12199 if (esdo->rela.hdr
12200 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12201 goto error_return;
12202 }
12203
12204 /* _bfd_elf_compute_section_file_positions makes temporary use
12205 of target_index. Reset it. */
12206 o->target_index = 0;
12207
12208 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12209 to count upwards while actually outputting the relocations. */
12210 esdo->rel.count = 0;
12211 esdo->rela.count = 0;
12212
12213 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12214 && !bfd_section_is_ctf (o))
12215 {
12216 /* Cache the section contents so that they can be compressed
12217 later. Use bfd_malloc since it will be freed by
12218 bfd_compress_section_contents. */
12219 unsigned char *contents = esdo->this_hdr.contents;
12220 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12221 abort ();
12222 contents
12223 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12224 if (contents == NULL)
12225 goto error_return;
12226 esdo->this_hdr.contents = contents;
12227 }
12228 }
12229
12230 /* We have now assigned file positions for all the sections except .symtab,
12231 .strtab, and non-loaded reloc and compressed debugging sections. We start
12232 the .symtab section at the current file position, and write directly to it.
12233 We build the .strtab section in memory. */
12234 abfd->symcount = 0;
12235 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12236 /* sh_name is set in prep_headers. */
12237 symtab_hdr->sh_type = SHT_SYMTAB;
12238 /* sh_flags, sh_addr and sh_size all start off zero. */
12239 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12240 /* sh_link is set in assign_section_numbers. */
12241 /* sh_info is set below. */
12242 /* sh_offset is set just below. */
12243 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12244
12245 if (max_sym_count < 20)
12246 max_sym_count = 20;
12247 htab->strtabsize = max_sym_count;
12248 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12249 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12250 if (htab->strtab == NULL)
12251 goto error_return;
12252 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12253 flinfo.symshndxbuf
12254 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12255 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12256
12257 if (info->strip != strip_all || emit_relocs)
12258 {
12259 file_ptr off = elf_next_file_pos (abfd);
12260
12261 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12262
12263 /* Note that at this point elf_next_file_pos (abfd) is
12264 incorrect. We do not yet know the size of the .symtab section.
12265 We correct next_file_pos below, after we do know the size. */
12266
12267 /* Start writing out the symbol table. The first symbol is always a
12268 dummy symbol. */
12269 elfsym.st_value = 0;
12270 elfsym.st_size = 0;
12271 elfsym.st_info = 0;
12272 elfsym.st_other = 0;
12273 elfsym.st_shndx = SHN_UNDEF;
12274 elfsym.st_target_internal = 0;
12275 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12276 bfd_und_section_ptr, NULL) != 1)
12277 goto error_return;
12278
12279 /* Output a symbol for each section. We output these even if we are
12280 discarding local symbols, since they are used for relocs. These
12281 symbols have no names. We store the index of each one in the
12282 index field of the section, so that we can find it again when
12283 outputting relocs. */
12284
12285 elfsym.st_size = 0;
12286 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12287 elfsym.st_other = 0;
12288 elfsym.st_value = 0;
12289 elfsym.st_target_internal = 0;
12290 for (i = 1; i < elf_numsections (abfd); i++)
12291 {
12292 o = bfd_section_from_elf_index (abfd, i);
12293 if (o != NULL)
12294 {
12295 o->target_index = bfd_get_symcount (abfd);
12296 elfsym.st_shndx = i;
12297 if (!bfd_link_relocatable (info))
12298 elfsym.st_value = o->vma;
12299 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12300 NULL) != 1)
12301 goto error_return;
12302 }
12303 }
12304 }
12305
12306 /* Allocate some memory to hold information read in from the input
12307 files. */
12308 if (max_contents_size != 0)
12309 {
12310 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12311 if (flinfo.contents == NULL)
12312 goto error_return;
12313 }
12314
12315 if (max_external_reloc_size != 0)
12316 {
12317 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12318 if (flinfo.external_relocs == NULL)
12319 goto error_return;
12320 }
12321
12322 if (max_internal_reloc_count != 0)
12323 {
12324 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12325 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12326 if (flinfo.internal_relocs == NULL)
12327 goto error_return;
12328 }
12329
12330 if (max_sym_count != 0)
12331 {
12332 amt = max_sym_count * bed->s->sizeof_sym;
12333 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12334 if (flinfo.external_syms == NULL)
12335 goto error_return;
12336
12337 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12338 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12339 if (flinfo.internal_syms == NULL)
12340 goto error_return;
12341
12342 amt = max_sym_count * sizeof (long);
12343 flinfo.indices = (long int *) bfd_malloc (amt);
12344 if (flinfo.indices == NULL)
12345 goto error_return;
12346
12347 amt = max_sym_count * sizeof (asection *);
12348 flinfo.sections = (asection **) bfd_malloc (amt);
12349 if (flinfo.sections == NULL)
12350 goto error_return;
12351 }
12352
12353 if (max_sym_shndx_count != 0)
12354 {
12355 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12356 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12357 if (flinfo.locsym_shndx == NULL)
12358 goto error_return;
12359 }
12360
12361 if (htab->tls_sec)
12362 {
12363 bfd_vma base, end = 0; /* Both bytes. */
12364 asection *sec;
12365
12366 for (sec = htab->tls_sec;
12367 sec && (sec->flags & SEC_THREAD_LOCAL);
12368 sec = sec->next)
12369 {
12370 bfd_size_type size = sec->size;
12371 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12372
12373 if (size == 0
12374 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12375 {
12376 struct bfd_link_order *ord = sec->map_tail.link_order;
12377
12378 if (ord != NULL)
12379 size = ord->offset * opb + ord->size;
12380 }
12381 end = sec->vma + size / opb;
12382 }
12383 base = htab->tls_sec->vma;
12384 /* Only align end of TLS section if static TLS doesn't have special
12385 alignment requirements. */
12386 if (bed->static_tls_alignment == 1)
12387 end = align_power (end, htab->tls_sec->alignment_power);
12388 htab->tls_size = end - base;
12389 }
12390
12391 /* Reorder SHF_LINK_ORDER sections. */
12392 for (o = abfd->sections; o != NULL; o = o->next)
12393 {
12394 if (!elf_fixup_link_order (abfd, o))
12395 return FALSE;
12396 }
12397
12398 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12399 return FALSE;
12400
12401 /* Since ELF permits relocations to be against local symbols, we
12402 must have the local symbols available when we do the relocations.
12403 Since we would rather only read the local symbols once, and we
12404 would rather not keep them in memory, we handle all the
12405 relocations for a single input file at the same time.
12406
12407 Unfortunately, there is no way to know the total number of local
12408 symbols until we have seen all of them, and the local symbol
12409 indices precede the global symbol indices. This means that when
12410 we are generating relocatable output, and we see a reloc against
12411 a global symbol, we can not know the symbol index until we have
12412 finished examining all the local symbols to see which ones we are
12413 going to output. To deal with this, we keep the relocations in
12414 memory, and don't output them until the end of the link. This is
12415 an unfortunate waste of memory, but I don't see a good way around
12416 it. Fortunately, it only happens when performing a relocatable
12417 link, which is not the common case. FIXME: If keep_memory is set
12418 we could write the relocs out and then read them again; I don't
12419 know how bad the memory loss will be. */
12420
12421 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12422 sub->output_has_begun = FALSE;
12423 for (o = abfd->sections; o != NULL; o = o->next)
12424 {
12425 for (p = o->map_head.link_order; p != NULL; p = p->next)
12426 {
12427 if (p->type == bfd_indirect_link_order
12428 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12429 == bfd_target_elf_flavour)
12430 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12431 {
12432 if (! sub->output_has_begun)
12433 {
12434 if (! elf_link_input_bfd (&flinfo, sub))
12435 goto error_return;
12436 sub->output_has_begun = TRUE;
12437 }
12438 }
12439 else if (p->type == bfd_section_reloc_link_order
12440 || p->type == bfd_symbol_reloc_link_order)
12441 {
12442 if (! elf_reloc_link_order (abfd, info, o, p))
12443 goto error_return;
12444 }
12445 else
12446 {
12447 if (! _bfd_default_link_order (abfd, info, o, p))
12448 {
12449 if (p->type == bfd_indirect_link_order
12450 && (bfd_get_flavour (sub)
12451 == bfd_target_elf_flavour)
12452 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12453 != bed->s->elfclass))
12454 {
12455 const char *iclass, *oclass;
12456
12457 switch (bed->s->elfclass)
12458 {
12459 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12460 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12461 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12462 default: abort ();
12463 }
12464
12465 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12466 {
12467 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12468 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12469 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12470 default: abort ();
12471 }
12472
12473 bfd_set_error (bfd_error_wrong_format);
12474 _bfd_error_handler
12475 /* xgettext:c-format */
12476 (_("%pB: file class %s incompatible with %s"),
12477 sub, iclass, oclass);
12478 }
12479
12480 goto error_return;
12481 }
12482 }
12483 }
12484 }
12485
12486 /* Free symbol buffer if needed. */
12487 if (!info->reduce_memory_overheads)
12488 {
12489 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12490 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12491 && elf_tdata (sub)->symbuf)
12492 {
12493 free (elf_tdata (sub)->symbuf);
12494 elf_tdata (sub)->symbuf = NULL;
12495 }
12496 }
12497
12498 /* Output any global symbols that got converted to local in a
12499 version script or due to symbol visibility. We do this in a
12500 separate step since ELF requires all local symbols to appear
12501 prior to any global symbols. FIXME: We should only do this if
12502 some global symbols were, in fact, converted to become local.
12503 FIXME: Will this work correctly with the Irix 5 linker? */
12504 eoinfo.failed = FALSE;
12505 eoinfo.flinfo = &flinfo;
12506 eoinfo.localsyms = TRUE;
12507 eoinfo.file_sym_done = FALSE;
12508 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12509 if (eoinfo.failed)
12510 return FALSE;
12511
12512 /* If backend needs to output some local symbols not present in the hash
12513 table, do it now. */
12514 if (bed->elf_backend_output_arch_local_syms
12515 && (info->strip != strip_all || emit_relocs))
12516 {
12517 typedef int (*out_sym_func)
12518 (void *, const char *, Elf_Internal_Sym *, asection *,
12519 struct elf_link_hash_entry *);
12520
12521 if (! ((*bed->elf_backend_output_arch_local_syms)
12522 (abfd, info, &flinfo,
12523 (out_sym_func) elf_link_output_symstrtab)))
12524 return FALSE;
12525 }
12526
12527 /* That wrote out all the local symbols. Finish up the symbol table
12528 with the global symbols. Even if we want to strip everything we
12529 can, we still need to deal with those global symbols that got
12530 converted to local in a version script. */
12531
12532 /* The sh_info field records the index of the first non local symbol. */
12533 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12534
12535 if (dynamic
12536 && htab->dynsym != NULL
12537 && htab->dynsym->output_section != bfd_abs_section_ptr)
12538 {
12539 Elf_Internal_Sym sym;
12540 bfd_byte *dynsym = htab->dynsym->contents;
12541
12542 o = htab->dynsym->output_section;
12543 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12544
12545 /* Write out the section symbols for the output sections. */
12546 if (bfd_link_pic (info)
12547 || htab->is_relocatable_executable)
12548 {
12549 asection *s;
12550
12551 sym.st_size = 0;
12552 sym.st_name = 0;
12553 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12554 sym.st_other = 0;
12555 sym.st_target_internal = 0;
12556
12557 for (s = abfd->sections; s != NULL; s = s->next)
12558 {
12559 int indx;
12560 bfd_byte *dest;
12561 long dynindx;
12562
12563 dynindx = elf_section_data (s)->dynindx;
12564 if (dynindx <= 0)
12565 continue;
12566 indx = elf_section_data (s)->this_idx;
12567 BFD_ASSERT (indx > 0);
12568 sym.st_shndx = indx;
12569 if (! check_dynsym (abfd, &sym))
12570 return FALSE;
12571 sym.st_value = s->vma;
12572 dest = dynsym + dynindx * bed->s->sizeof_sym;
12573 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12574 }
12575 }
12576
12577 /* Write out the local dynsyms. */
12578 if (htab->dynlocal)
12579 {
12580 struct elf_link_local_dynamic_entry *e;
12581 for (e = htab->dynlocal; e ; e = e->next)
12582 {
12583 asection *s;
12584 bfd_byte *dest;
12585
12586 /* Copy the internal symbol and turn off visibility.
12587 Note that we saved a word of storage and overwrote
12588 the original st_name with the dynstr_index. */
12589 sym = e->isym;
12590 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12591
12592 s = bfd_section_from_elf_index (e->input_bfd,
12593 e->isym.st_shndx);
12594 if (s != NULL)
12595 {
12596 sym.st_shndx =
12597 elf_section_data (s->output_section)->this_idx;
12598 if (! check_dynsym (abfd, &sym))
12599 return FALSE;
12600 sym.st_value = (s->output_section->vma
12601 + s->output_offset
12602 + e->isym.st_value);
12603 }
12604
12605 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12606 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12607 }
12608 }
12609 }
12610
12611 /* We get the global symbols from the hash table. */
12612 eoinfo.failed = FALSE;
12613 eoinfo.localsyms = FALSE;
12614 eoinfo.flinfo = &flinfo;
12615 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12616 if (eoinfo.failed)
12617 return FALSE;
12618
12619 /* If backend needs to output some symbols not present in the hash
12620 table, do it now. */
12621 if (bed->elf_backend_output_arch_syms
12622 && (info->strip != strip_all || emit_relocs))
12623 {
12624 typedef int (*out_sym_func)
12625 (void *, const char *, Elf_Internal_Sym *, asection *,
12626 struct elf_link_hash_entry *);
12627
12628 if (! ((*bed->elf_backend_output_arch_syms)
12629 (abfd, info, &flinfo,
12630 (out_sym_func) elf_link_output_symstrtab)))
12631 return FALSE;
12632 }
12633
12634 /* Finalize the .strtab section. */
12635 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12636
12637 /* Swap out the .strtab section. */
12638 if (!elf_link_swap_symbols_out (&flinfo))
12639 return FALSE;
12640
12641 /* Now we know the size of the symtab section. */
12642 if (bfd_get_symcount (abfd) > 0)
12643 {
12644 /* Finish up and write out the symbol string table (.strtab)
12645 section. */
12646 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12647 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12648
12649 if (elf_symtab_shndx_list (abfd))
12650 {
12651 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12652
12653 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12654 {
12655 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12656 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12657 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12658 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12659 symtab_shndx_hdr->sh_size = amt;
12660
12661 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12662 off, TRUE);
12663
12664 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12665 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12666 return FALSE;
12667 }
12668 }
12669
12670 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12671 /* sh_name was set in prep_headers. */
12672 symstrtab_hdr->sh_type = SHT_STRTAB;
12673 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12674 symstrtab_hdr->sh_addr = 0;
12675 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12676 symstrtab_hdr->sh_entsize = 0;
12677 symstrtab_hdr->sh_link = 0;
12678 symstrtab_hdr->sh_info = 0;
12679 /* sh_offset is set just below. */
12680 symstrtab_hdr->sh_addralign = 1;
12681
12682 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12683 off, TRUE);
12684 elf_next_file_pos (abfd) = off;
12685
12686 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12687 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12688 return FALSE;
12689 }
12690
12691 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12692 {
12693 _bfd_error_handler (_("%pB: failed to generate import library"),
12694 info->out_implib_bfd);
12695 return FALSE;
12696 }
12697
12698 /* Adjust the relocs to have the correct symbol indices. */
12699 for (o = abfd->sections; o != NULL; o = o->next)
12700 {
12701 struct bfd_elf_section_data *esdo = elf_section_data (o);
12702 bfd_boolean sort;
12703
12704 if ((o->flags & SEC_RELOC) == 0)
12705 continue;
12706
12707 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12708 if (esdo->rel.hdr != NULL
12709 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12710 return FALSE;
12711 if (esdo->rela.hdr != NULL
12712 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12713 return FALSE;
12714
12715 /* Set the reloc_count field to 0 to prevent write_relocs from
12716 trying to swap the relocs out itself. */
12717 o->reloc_count = 0;
12718 }
12719
12720 if (dynamic && info->combreloc && dynobj != NULL)
12721 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12722
12723 /* If we are linking against a dynamic object, or generating a
12724 shared library, finish up the dynamic linking information. */
12725 if (dynamic)
12726 {
12727 bfd_byte *dyncon, *dynconend;
12728
12729 /* Fix up .dynamic entries. */
12730 o = bfd_get_linker_section (dynobj, ".dynamic");
12731 BFD_ASSERT (o != NULL);
12732
12733 dyncon = o->contents;
12734 dynconend = o->contents + o->size;
12735 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12736 {
12737 Elf_Internal_Dyn dyn;
12738 const char *name;
12739 unsigned int type;
12740 bfd_size_type sh_size;
12741 bfd_vma sh_addr;
12742
12743 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12744
12745 switch (dyn.d_tag)
12746 {
12747 default:
12748 continue;
12749 case DT_NULL:
12750 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12751 {
12752 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12753 {
12754 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12755 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12756 default: continue;
12757 }
12758 dyn.d_un.d_val = relativecount;
12759 relativecount = 0;
12760 break;
12761 }
12762 continue;
12763
12764 case DT_INIT:
12765 name = info->init_function;
12766 goto get_sym;
12767 case DT_FINI:
12768 name = info->fini_function;
12769 get_sym:
12770 {
12771 struct elf_link_hash_entry *h;
12772
12773 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12774 if (h != NULL
12775 && (h->root.type == bfd_link_hash_defined
12776 || h->root.type == bfd_link_hash_defweak))
12777 {
12778 dyn.d_un.d_ptr = h->root.u.def.value;
12779 o = h->root.u.def.section;
12780 if (o->output_section != NULL)
12781 dyn.d_un.d_ptr += (o->output_section->vma
12782 + o->output_offset);
12783 else
12784 {
12785 /* The symbol is imported from another shared
12786 library and does not apply to this one. */
12787 dyn.d_un.d_ptr = 0;
12788 }
12789 break;
12790 }
12791 }
12792 continue;
12793
12794 case DT_PREINIT_ARRAYSZ:
12795 name = ".preinit_array";
12796 goto get_out_size;
12797 case DT_INIT_ARRAYSZ:
12798 name = ".init_array";
12799 goto get_out_size;
12800 case DT_FINI_ARRAYSZ:
12801 name = ".fini_array";
12802 get_out_size:
12803 o = bfd_get_section_by_name (abfd, name);
12804 if (o == NULL)
12805 {
12806 _bfd_error_handler
12807 (_("could not find section %s"), name);
12808 goto error_return;
12809 }
12810 if (o->size == 0)
12811 _bfd_error_handler
12812 (_("warning: %s section has zero size"), name);
12813 dyn.d_un.d_val = o->size;
12814 break;
12815
12816 case DT_PREINIT_ARRAY:
12817 name = ".preinit_array";
12818 goto get_out_vma;
12819 case DT_INIT_ARRAY:
12820 name = ".init_array";
12821 goto get_out_vma;
12822 case DT_FINI_ARRAY:
12823 name = ".fini_array";
12824 get_out_vma:
12825 o = bfd_get_section_by_name (abfd, name);
12826 goto do_vma;
12827
12828 case DT_HASH:
12829 name = ".hash";
12830 goto get_vma;
12831 case DT_GNU_HASH:
12832 name = ".gnu.hash";
12833 goto get_vma;
12834 case DT_STRTAB:
12835 name = ".dynstr";
12836 goto get_vma;
12837 case DT_SYMTAB:
12838 name = ".dynsym";
12839 goto get_vma;
12840 case DT_VERDEF:
12841 name = ".gnu.version_d";
12842 goto get_vma;
12843 case DT_VERNEED:
12844 name = ".gnu.version_r";
12845 goto get_vma;
12846 case DT_VERSYM:
12847 name = ".gnu.version";
12848 get_vma:
12849 o = bfd_get_linker_section (dynobj, name);
12850 do_vma:
12851 if (o == NULL || bfd_is_abs_section (o->output_section))
12852 {
12853 _bfd_error_handler
12854 (_("could not find section %s"), name);
12855 goto error_return;
12856 }
12857 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12858 {
12859 _bfd_error_handler
12860 (_("warning: section '%s' is being made into a note"), name);
12861 bfd_set_error (bfd_error_nonrepresentable_section);
12862 goto error_return;
12863 }
12864 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12865 break;
12866
12867 case DT_REL:
12868 case DT_RELA:
12869 case DT_RELSZ:
12870 case DT_RELASZ:
12871 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12872 type = SHT_REL;
12873 else
12874 type = SHT_RELA;
12875 sh_size = 0;
12876 sh_addr = 0;
12877 for (i = 1; i < elf_numsections (abfd); i++)
12878 {
12879 Elf_Internal_Shdr *hdr;
12880
12881 hdr = elf_elfsections (abfd)[i];
12882 if (hdr->sh_type == type
12883 && (hdr->sh_flags & SHF_ALLOC) != 0)
12884 {
12885 sh_size += hdr->sh_size;
12886 if (sh_addr == 0
12887 || sh_addr > hdr->sh_addr)
12888 sh_addr = hdr->sh_addr;
12889 }
12890 }
12891
12892 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12893 {
12894 unsigned int opb = bfd_octets_per_byte (abfd, o);
12895
12896 /* Don't count procedure linkage table relocs in the
12897 overall reloc count. */
12898 sh_size -= htab->srelplt->size;
12899 if (sh_size == 0)
12900 /* If the size is zero, make the address zero too.
12901 This is to avoid a glibc bug. If the backend
12902 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12903 zero, then we'll put DT_RELA at the end of
12904 DT_JMPREL. glibc will interpret the end of
12905 DT_RELA matching the end of DT_JMPREL as the
12906 case where DT_RELA includes DT_JMPREL, and for
12907 LD_BIND_NOW will decide that processing DT_RELA
12908 will process the PLT relocs too. Net result:
12909 No PLT relocs applied. */
12910 sh_addr = 0;
12911
12912 /* If .rela.plt is the first .rela section, exclude
12913 it from DT_RELA. */
12914 else if (sh_addr == (htab->srelplt->output_section->vma
12915 + htab->srelplt->output_offset) * opb)
12916 sh_addr += htab->srelplt->size;
12917 }
12918
12919 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12920 dyn.d_un.d_val = sh_size;
12921 else
12922 dyn.d_un.d_ptr = sh_addr;
12923 break;
12924 }
12925 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12926 }
12927 }
12928
12929 /* If we have created any dynamic sections, then output them. */
12930 if (dynobj != NULL)
12931 {
12932 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12933 goto error_return;
12934
12935 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12936 if (((info->warn_shared_textrel && bfd_link_pic (info))
12937 || info->error_textrel)
12938 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12939 {
12940 bfd_byte *dyncon, *dynconend;
12941
12942 dyncon = o->contents;
12943 dynconend = o->contents + o->size;
12944 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12945 {
12946 Elf_Internal_Dyn dyn;
12947
12948 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12949
12950 if (dyn.d_tag == DT_TEXTREL)
12951 {
12952 if (info->error_textrel)
12953 info->callbacks->einfo
12954 (_("%P%X: read-only segment has dynamic relocations\n"));
12955 else
12956 info->callbacks->einfo
12957 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12958 break;
12959 }
12960 }
12961 }
12962
12963 for (o = dynobj->sections; o != NULL; o = o->next)
12964 {
12965 if ((o->flags & SEC_HAS_CONTENTS) == 0
12966 || o->size == 0
12967 || o->output_section == bfd_abs_section_ptr)
12968 continue;
12969 if ((o->flags & SEC_LINKER_CREATED) == 0)
12970 {
12971 /* At this point, we are only interested in sections
12972 created by _bfd_elf_link_create_dynamic_sections. */
12973 continue;
12974 }
12975 if (htab->stab_info.stabstr == o)
12976 continue;
12977 if (htab->eh_info.hdr_sec == o)
12978 continue;
12979 if (strcmp (o->name, ".dynstr") != 0)
12980 {
12981 bfd_size_type octets = ((file_ptr) o->output_offset
12982 * bfd_octets_per_byte (abfd, o));
12983 if (!bfd_set_section_contents (abfd, o->output_section,
12984 o->contents, octets, o->size))
12985 goto error_return;
12986 }
12987 else
12988 {
12989 /* The contents of the .dynstr section are actually in a
12990 stringtab. */
12991 file_ptr off;
12992
12993 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12994 if (bfd_seek (abfd, off, SEEK_SET) != 0
12995 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12996 goto error_return;
12997 }
12998 }
12999 }
13000
13001 if (!info->resolve_section_groups)
13002 {
13003 bfd_boolean failed = FALSE;
13004
13005 BFD_ASSERT (bfd_link_relocatable (info));
13006 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13007 if (failed)
13008 goto error_return;
13009 }
13010
13011 /* If we have optimized stabs strings, output them. */
13012 if (htab->stab_info.stabstr != NULL)
13013 {
13014 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13015 goto error_return;
13016 }
13017
13018 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13019 goto error_return;
13020
13021 if (info->callbacks->emit_ctf)
13022 info->callbacks->emit_ctf ();
13023
13024 elf_final_link_free (abfd, &flinfo);
13025
13026 if (attr_section)
13027 {
13028 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13029 if (contents == NULL)
13030 return FALSE; /* Bail out and fail. */
13031 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13032 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13033 free (contents);
13034 }
13035
13036 return TRUE;
13037
13038 error_return:
13039 elf_final_link_free (abfd, &flinfo);
13040 return FALSE;
13041 }
13042 \f
13043 /* Initialize COOKIE for input bfd ABFD. */
13044
13045 static bfd_boolean
13046 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13047 struct bfd_link_info *info, bfd *abfd)
13048 {
13049 Elf_Internal_Shdr *symtab_hdr;
13050 const struct elf_backend_data *bed;
13051
13052 bed = get_elf_backend_data (abfd);
13053 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13054
13055 cookie->abfd = abfd;
13056 cookie->sym_hashes = elf_sym_hashes (abfd);
13057 cookie->bad_symtab = elf_bad_symtab (abfd);
13058 if (cookie->bad_symtab)
13059 {
13060 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13061 cookie->extsymoff = 0;
13062 }
13063 else
13064 {
13065 cookie->locsymcount = symtab_hdr->sh_info;
13066 cookie->extsymoff = symtab_hdr->sh_info;
13067 }
13068
13069 if (bed->s->arch_size == 32)
13070 cookie->r_sym_shift = 8;
13071 else
13072 cookie->r_sym_shift = 32;
13073
13074 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13075 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13076 {
13077 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13078 cookie->locsymcount, 0,
13079 NULL, NULL, NULL);
13080 if (cookie->locsyms == NULL)
13081 {
13082 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13083 return FALSE;
13084 }
13085 if (info->keep_memory)
13086 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13087 }
13088 return TRUE;
13089 }
13090
13091 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13092
13093 static void
13094 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13095 {
13096 Elf_Internal_Shdr *symtab_hdr;
13097
13098 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13099 if (cookie->locsyms != NULL
13100 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13101 free (cookie->locsyms);
13102 }
13103
13104 /* Initialize the relocation information in COOKIE for input section SEC
13105 of input bfd ABFD. */
13106
13107 static bfd_boolean
13108 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13109 struct bfd_link_info *info, bfd *abfd,
13110 asection *sec)
13111 {
13112 if (sec->reloc_count == 0)
13113 {
13114 cookie->rels = NULL;
13115 cookie->relend = NULL;
13116 }
13117 else
13118 {
13119 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13120 info->keep_memory);
13121 if (cookie->rels == NULL)
13122 return FALSE;
13123 cookie->rel = cookie->rels;
13124 cookie->relend = cookie->rels + sec->reloc_count;
13125 }
13126 cookie->rel = cookie->rels;
13127 return TRUE;
13128 }
13129
13130 /* Free the memory allocated by init_reloc_cookie_rels,
13131 if appropriate. */
13132
13133 static void
13134 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13135 asection *sec)
13136 {
13137 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13138 free (cookie->rels);
13139 }
13140
13141 /* Initialize the whole of COOKIE for input section SEC. */
13142
13143 static bfd_boolean
13144 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13145 struct bfd_link_info *info,
13146 asection *sec)
13147 {
13148 if (!init_reloc_cookie (cookie, info, sec->owner))
13149 goto error1;
13150 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13151 goto error2;
13152 return TRUE;
13153
13154 error2:
13155 fini_reloc_cookie (cookie, sec->owner);
13156 error1:
13157 return FALSE;
13158 }
13159
13160 /* Free the memory allocated by init_reloc_cookie_for_section,
13161 if appropriate. */
13162
13163 static void
13164 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13165 asection *sec)
13166 {
13167 fini_reloc_cookie_rels (cookie, sec);
13168 fini_reloc_cookie (cookie, sec->owner);
13169 }
13170 \f
13171 /* Garbage collect unused sections. */
13172
13173 /* Default gc_mark_hook. */
13174
13175 asection *
13176 _bfd_elf_gc_mark_hook (asection *sec,
13177 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13178 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13179 struct elf_link_hash_entry *h,
13180 Elf_Internal_Sym *sym)
13181 {
13182 if (h != NULL)
13183 {
13184 switch (h->root.type)
13185 {
13186 case bfd_link_hash_defined:
13187 case bfd_link_hash_defweak:
13188 return h->root.u.def.section;
13189
13190 case bfd_link_hash_common:
13191 return h->root.u.c.p->section;
13192
13193 default:
13194 break;
13195 }
13196 }
13197 else
13198 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13199
13200 return NULL;
13201 }
13202
13203 /* Return the debug definition section. */
13204
13205 static asection *
13206 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13207 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13208 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13209 struct elf_link_hash_entry *h,
13210 Elf_Internal_Sym *sym)
13211 {
13212 if (h != NULL)
13213 {
13214 /* Return the global debug definition section. */
13215 if ((h->root.type == bfd_link_hash_defined
13216 || h->root.type == bfd_link_hash_defweak)
13217 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13218 return h->root.u.def.section;
13219 }
13220 else
13221 {
13222 /* Return the local debug definition section. */
13223 asection *isec = bfd_section_from_elf_index (sec->owner,
13224 sym->st_shndx);
13225 if ((isec->flags & SEC_DEBUGGING) != 0)
13226 return isec;
13227 }
13228
13229 return NULL;
13230 }
13231
13232 /* COOKIE->rel describes a relocation against section SEC, which is
13233 a section we've decided to keep. Return the section that contains
13234 the relocation symbol, or NULL if no section contains it. */
13235
13236 asection *
13237 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13238 elf_gc_mark_hook_fn gc_mark_hook,
13239 struct elf_reloc_cookie *cookie,
13240 bfd_boolean *start_stop)
13241 {
13242 unsigned long r_symndx;
13243 struct elf_link_hash_entry *h, *hw;
13244
13245 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13246 if (r_symndx == STN_UNDEF)
13247 return NULL;
13248
13249 if (r_symndx >= cookie->locsymcount
13250 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13251 {
13252 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13253 if (h == NULL)
13254 {
13255 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13256 sec->owner);
13257 return NULL;
13258 }
13259 while (h->root.type == bfd_link_hash_indirect
13260 || h->root.type == bfd_link_hash_warning)
13261 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13262 h->mark = 1;
13263 /* Keep all aliases of the symbol too. If an object symbol
13264 needs to be copied into .dynbss then all of its aliases
13265 should be present as dynamic symbols, not just the one used
13266 on the copy relocation. */
13267 hw = h;
13268 while (hw->is_weakalias)
13269 {
13270 hw = hw->u.alias;
13271 hw->mark = 1;
13272 }
13273
13274 if (start_stop != NULL)
13275 {
13276 /* To work around a glibc bug, mark XXX input sections
13277 when there is a reference to __start_XXX or __stop_XXX
13278 symbols. */
13279 if (h->start_stop)
13280 {
13281 asection *s = h->u2.start_stop_section;
13282 *start_stop = !s->gc_mark;
13283 return s;
13284 }
13285 }
13286
13287 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13288 }
13289
13290 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13291 &cookie->locsyms[r_symndx]);
13292 }
13293
13294 /* COOKIE->rel describes a relocation against section SEC, which is
13295 a section we've decided to keep. Mark the section that contains
13296 the relocation symbol. */
13297
13298 bfd_boolean
13299 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13300 asection *sec,
13301 elf_gc_mark_hook_fn gc_mark_hook,
13302 struct elf_reloc_cookie *cookie)
13303 {
13304 asection *rsec;
13305 bfd_boolean start_stop = FALSE;
13306
13307 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13308 while (rsec != NULL)
13309 {
13310 if (!rsec->gc_mark)
13311 {
13312 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13313 || (rsec->owner->flags & DYNAMIC) != 0)
13314 rsec->gc_mark = 1;
13315 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13316 return FALSE;
13317 }
13318 if (!start_stop)
13319 break;
13320 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13321 }
13322 return TRUE;
13323 }
13324
13325 /* The mark phase of garbage collection. For a given section, mark
13326 it and any sections in this section's group, and all the sections
13327 which define symbols to which it refers. */
13328
13329 bfd_boolean
13330 _bfd_elf_gc_mark (struct bfd_link_info *info,
13331 asection *sec,
13332 elf_gc_mark_hook_fn gc_mark_hook)
13333 {
13334 bfd_boolean ret;
13335 asection *group_sec, *eh_frame;
13336
13337 sec->gc_mark = 1;
13338
13339 /* Mark all the sections in the group. */
13340 group_sec = elf_section_data (sec)->next_in_group;
13341 if (group_sec && !group_sec->gc_mark)
13342 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13343 return FALSE;
13344
13345 /* Look through the section relocs. */
13346 ret = TRUE;
13347 eh_frame = elf_eh_frame_section (sec->owner);
13348 if ((sec->flags & SEC_RELOC) != 0
13349 && sec->reloc_count > 0
13350 && sec != eh_frame)
13351 {
13352 struct elf_reloc_cookie cookie;
13353
13354 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13355 ret = FALSE;
13356 else
13357 {
13358 for (; cookie.rel < cookie.relend; cookie.rel++)
13359 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13360 {
13361 ret = FALSE;
13362 break;
13363 }
13364 fini_reloc_cookie_for_section (&cookie, sec);
13365 }
13366 }
13367
13368 if (ret && eh_frame && elf_fde_list (sec))
13369 {
13370 struct elf_reloc_cookie cookie;
13371
13372 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13373 ret = FALSE;
13374 else
13375 {
13376 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13377 gc_mark_hook, &cookie))
13378 ret = FALSE;
13379 fini_reloc_cookie_for_section (&cookie, eh_frame);
13380 }
13381 }
13382
13383 eh_frame = elf_section_eh_frame_entry (sec);
13384 if (ret && eh_frame && !eh_frame->gc_mark)
13385 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13386 ret = FALSE;
13387
13388 return ret;
13389 }
13390
13391 /* Scan and mark sections in a special or debug section group. */
13392
13393 static void
13394 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13395 {
13396 /* Point to first section of section group. */
13397 asection *ssec;
13398 /* Used to iterate the section group. */
13399 asection *msec;
13400
13401 bfd_boolean is_special_grp = TRUE;
13402 bfd_boolean is_debug_grp = TRUE;
13403
13404 /* First scan to see if group contains any section other than debug
13405 and special section. */
13406 ssec = msec = elf_next_in_group (grp);
13407 do
13408 {
13409 if ((msec->flags & SEC_DEBUGGING) == 0)
13410 is_debug_grp = FALSE;
13411
13412 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13413 is_special_grp = FALSE;
13414
13415 msec = elf_next_in_group (msec);
13416 }
13417 while (msec != ssec);
13418
13419 /* If this is a pure debug section group or pure special section group,
13420 keep all sections in this group. */
13421 if (is_debug_grp || is_special_grp)
13422 {
13423 do
13424 {
13425 msec->gc_mark = 1;
13426 msec = elf_next_in_group (msec);
13427 }
13428 while (msec != ssec);
13429 }
13430 }
13431
13432 /* Keep debug and special sections. */
13433
13434 bfd_boolean
13435 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13436 elf_gc_mark_hook_fn mark_hook)
13437 {
13438 bfd *ibfd;
13439
13440 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13441 {
13442 asection *isec;
13443 bfd_boolean some_kept;
13444 bfd_boolean debug_frag_seen;
13445 bfd_boolean has_kept_debug_info;
13446
13447 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13448 continue;
13449 isec = ibfd->sections;
13450 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13451 continue;
13452
13453 /* Ensure all linker created sections are kept,
13454 see if any other section is already marked,
13455 and note if we have any fragmented debug sections. */
13456 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13457 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13458 {
13459 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13460 isec->gc_mark = 1;
13461 else if (isec->gc_mark
13462 && (isec->flags & SEC_ALLOC) != 0
13463 && elf_section_type (isec) != SHT_NOTE)
13464 some_kept = TRUE;
13465 else
13466 {
13467 /* Since all sections, except for backend specific ones,
13468 have been garbage collected, call mark_hook on this
13469 section if any of its linked-to sections is marked. */
13470 asection *linked_to_sec = elf_linked_to_section (isec);
13471 for (; linked_to_sec != NULL;
13472 linked_to_sec = elf_linked_to_section (linked_to_sec))
13473 if (linked_to_sec->gc_mark)
13474 {
13475 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13476 return FALSE;
13477 break;
13478 }
13479 }
13480
13481 if (!debug_frag_seen
13482 && (isec->flags & SEC_DEBUGGING)
13483 && CONST_STRNEQ (isec->name, ".debug_line."))
13484 debug_frag_seen = TRUE;
13485 else if (strcmp (bfd_section_name (isec),
13486 "__patchable_function_entries") == 0
13487 && elf_linked_to_section (isec) == NULL)
13488 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13489 "need linked-to section "
13490 "for --gc-sections\n"),
13491 isec->owner, isec);
13492 }
13493
13494 /* If no non-note alloc section in this file will be kept, then
13495 we can toss out the debug and special sections. */
13496 if (!some_kept)
13497 continue;
13498
13499 /* Keep debug and special sections like .comment when they are
13500 not part of a group. Also keep section groups that contain
13501 just debug sections or special sections. NB: Sections with
13502 linked-to section has been handled above. */
13503 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13504 {
13505 if ((isec->flags & SEC_GROUP) != 0)
13506 _bfd_elf_gc_mark_debug_special_section_group (isec);
13507 else if (((isec->flags & SEC_DEBUGGING) != 0
13508 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13509 && elf_next_in_group (isec) == NULL
13510 && elf_linked_to_section (isec) == NULL)
13511 isec->gc_mark = 1;
13512 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13513 has_kept_debug_info = TRUE;
13514 }
13515
13516 /* Look for CODE sections which are going to be discarded,
13517 and find and discard any fragmented debug sections which
13518 are associated with that code section. */
13519 if (debug_frag_seen)
13520 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13521 if ((isec->flags & SEC_CODE) != 0
13522 && isec->gc_mark == 0)
13523 {
13524 unsigned int ilen;
13525 asection *dsec;
13526
13527 ilen = strlen (isec->name);
13528
13529 /* Association is determined by the name of the debug
13530 section containing the name of the code section as
13531 a suffix. For example .debug_line.text.foo is a
13532 debug section associated with .text.foo. */
13533 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13534 {
13535 unsigned int dlen;
13536
13537 if (dsec->gc_mark == 0
13538 || (dsec->flags & SEC_DEBUGGING) == 0)
13539 continue;
13540
13541 dlen = strlen (dsec->name);
13542
13543 if (dlen > ilen
13544 && strncmp (dsec->name + (dlen - ilen),
13545 isec->name, ilen) == 0)
13546 dsec->gc_mark = 0;
13547 }
13548 }
13549
13550 /* Mark debug sections referenced by kept debug sections. */
13551 if (has_kept_debug_info)
13552 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13553 if (isec->gc_mark
13554 && (isec->flags & SEC_DEBUGGING) != 0)
13555 if (!_bfd_elf_gc_mark (info, isec,
13556 elf_gc_mark_debug_section))
13557 return FALSE;
13558 }
13559 return TRUE;
13560 }
13561
13562 static bfd_boolean
13563 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13564 {
13565 bfd *sub;
13566 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13567
13568 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13569 {
13570 asection *o;
13571
13572 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13573 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13574 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13575 continue;
13576 o = sub->sections;
13577 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13578 continue;
13579
13580 for (o = sub->sections; o != NULL; o = o->next)
13581 {
13582 /* When any section in a section group is kept, we keep all
13583 sections in the section group. If the first member of
13584 the section group is excluded, we will also exclude the
13585 group section. */
13586 if (o->flags & SEC_GROUP)
13587 {
13588 asection *first = elf_next_in_group (o);
13589 o->gc_mark = first->gc_mark;
13590 }
13591
13592 if (o->gc_mark)
13593 continue;
13594
13595 /* Skip sweeping sections already excluded. */
13596 if (o->flags & SEC_EXCLUDE)
13597 continue;
13598
13599 /* Since this is early in the link process, it is simple
13600 to remove a section from the output. */
13601 o->flags |= SEC_EXCLUDE;
13602
13603 if (info->print_gc_sections && o->size != 0)
13604 /* xgettext:c-format */
13605 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13606 o, sub);
13607 }
13608 }
13609
13610 return TRUE;
13611 }
13612
13613 /* Propagate collected vtable information. This is called through
13614 elf_link_hash_traverse. */
13615
13616 static bfd_boolean
13617 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13618 {
13619 /* Those that are not vtables. */
13620 if (h->start_stop
13621 || h->u2.vtable == NULL
13622 || h->u2.vtable->parent == NULL)
13623 return TRUE;
13624
13625 /* Those vtables that do not have parents, we cannot merge. */
13626 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13627 return TRUE;
13628
13629 /* If we've already been done, exit. */
13630 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13631 return TRUE;
13632
13633 /* Make sure the parent's table is up to date. */
13634 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13635
13636 if (h->u2.vtable->used == NULL)
13637 {
13638 /* None of this table's entries were referenced. Re-use the
13639 parent's table. */
13640 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13641 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13642 }
13643 else
13644 {
13645 size_t n;
13646 bfd_boolean *cu, *pu;
13647
13648 /* Or the parent's entries into ours. */
13649 cu = h->u2.vtable->used;
13650 cu[-1] = TRUE;
13651 pu = h->u2.vtable->parent->u2.vtable->used;
13652 if (pu != NULL)
13653 {
13654 const struct elf_backend_data *bed;
13655 unsigned int log_file_align;
13656
13657 bed = get_elf_backend_data (h->root.u.def.section->owner);
13658 log_file_align = bed->s->log_file_align;
13659 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13660 while (n--)
13661 {
13662 if (*pu)
13663 *cu = TRUE;
13664 pu++;
13665 cu++;
13666 }
13667 }
13668 }
13669
13670 return TRUE;
13671 }
13672
13673 static bfd_boolean
13674 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13675 {
13676 asection *sec;
13677 bfd_vma hstart, hend;
13678 Elf_Internal_Rela *relstart, *relend, *rel;
13679 const struct elf_backend_data *bed;
13680 unsigned int log_file_align;
13681
13682 /* Take care of both those symbols that do not describe vtables as
13683 well as those that are not loaded. */
13684 if (h->start_stop
13685 || h->u2.vtable == NULL
13686 || h->u2.vtable->parent == NULL)
13687 return TRUE;
13688
13689 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13690 || h->root.type == bfd_link_hash_defweak);
13691
13692 sec = h->root.u.def.section;
13693 hstart = h->root.u.def.value;
13694 hend = hstart + h->size;
13695
13696 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13697 if (!relstart)
13698 return *(bfd_boolean *) okp = FALSE;
13699 bed = get_elf_backend_data (sec->owner);
13700 log_file_align = bed->s->log_file_align;
13701
13702 relend = relstart + sec->reloc_count;
13703
13704 for (rel = relstart; rel < relend; ++rel)
13705 if (rel->r_offset >= hstart && rel->r_offset < hend)
13706 {
13707 /* If the entry is in use, do nothing. */
13708 if (h->u2.vtable->used
13709 && (rel->r_offset - hstart) < h->u2.vtable->size)
13710 {
13711 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13712 if (h->u2.vtable->used[entry])
13713 continue;
13714 }
13715 /* Otherwise, kill it. */
13716 rel->r_offset = rel->r_info = rel->r_addend = 0;
13717 }
13718
13719 return TRUE;
13720 }
13721
13722 /* Mark sections containing dynamically referenced symbols. When
13723 building shared libraries, we must assume that any visible symbol is
13724 referenced. */
13725
13726 bfd_boolean
13727 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13728 {
13729 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13730 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13731
13732 if ((h->root.type == bfd_link_hash_defined
13733 || h->root.type == bfd_link_hash_defweak)
13734 && ((h->ref_dynamic && !h->forced_local)
13735 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13736 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13737 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13738 && (!bfd_link_executable (info)
13739 || info->gc_keep_exported
13740 || info->export_dynamic
13741 || (h->dynamic
13742 && d != NULL
13743 && (*d->match) (&d->head, NULL, h->root.root.string)))
13744 && (h->versioned >= versioned
13745 || !bfd_hide_sym_by_version (info->version_info,
13746 h->root.root.string)))))
13747 h->root.u.def.section->flags |= SEC_KEEP;
13748
13749 return TRUE;
13750 }
13751
13752 /* Keep all sections containing symbols undefined on the command-line,
13753 and the section containing the entry symbol. */
13754
13755 void
13756 _bfd_elf_gc_keep (struct bfd_link_info *info)
13757 {
13758 struct bfd_sym_chain *sym;
13759
13760 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13761 {
13762 struct elf_link_hash_entry *h;
13763
13764 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13765 FALSE, FALSE, FALSE);
13766
13767 if (h != NULL
13768 && (h->root.type == bfd_link_hash_defined
13769 || h->root.type == bfd_link_hash_defweak)
13770 && !bfd_is_abs_section (h->root.u.def.section)
13771 && !bfd_is_und_section (h->root.u.def.section))
13772 h->root.u.def.section->flags |= SEC_KEEP;
13773 }
13774 }
13775
13776 bfd_boolean
13777 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13778 struct bfd_link_info *info)
13779 {
13780 bfd *ibfd = info->input_bfds;
13781
13782 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13783 {
13784 asection *sec;
13785 struct elf_reloc_cookie cookie;
13786
13787 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13788 continue;
13789 sec = ibfd->sections;
13790 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13791 continue;
13792
13793 if (!init_reloc_cookie (&cookie, info, ibfd))
13794 return FALSE;
13795
13796 for (sec = ibfd->sections; sec; sec = sec->next)
13797 {
13798 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13799 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13800 {
13801 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13802 fini_reloc_cookie_rels (&cookie, sec);
13803 }
13804 }
13805 }
13806 return TRUE;
13807 }
13808
13809 /* Do mark and sweep of unused sections. */
13810
13811 bfd_boolean
13812 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13813 {
13814 bfd_boolean ok = TRUE;
13815 bfd *sub;
13816 elf_gc_mark_hook_fn gc_mark_hook;
13817 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13818 struct elf_link_hash_table *htab;
13819
13820 if (!bed->can_gc_sections
13821 || !is_elf_hash_table (info->hash))
13822 {
13823 _bfd_error_handler(_("warning: gc-sections option ignored"));
13824 return TRUE;
13825 }
13826
13827 bed->gc_keep (info);
13828 htab = elf_hash_table (info);
13829
13830 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13831 at the .eh_frame section if we can mark the FDEs individually. */
13832 for (sub = info->input_bfds;
13833 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13834 sub = sub->link.next)
13835 {
13836 asection *sec;
13837 struct elf_reloc_cookie cookie;
13838
13839 sec = sub->sections;
13840 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13841 continue;
13842 sec = bfd_get_section_by_name (sub, ".eh_frame");
13843 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13844 {
13845 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13846 if (elf_section_data (sec)->sec_info
13847 && (sec->flags & SEC_LINKER_CREATED) == 0)
13848 elf_eh_frame_section (sub) = sec;
13849 fini_reloc_cookie_for_section (&cookie, sec);
13850 sec = bfd_get_next_section_by_name (NULL, sec);
13851 }
13852 }
13853
13854 /* Apply transitive closure to the vtable entry usage info. */
13855 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13856 if (!ok)
13857 return FALSE;
13858
13859 /* Kill the vtable relocations that were not used. */
13860 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13861 if (!ok)
13862 return FALSE;
13863
13864 /* Mark dynamically referenced symbols. */
13865 if (htab->dynamic_sections_created || info->gc_keep_exported)
13866 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13867
13868 /* Grovel through relocs to find out who stays ... */
13869 gc_mark_hook = bed->gc_mark_hook;
13870 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13871 {
13872 asection *o;
13873
13874 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13875 || elf_object_id (sub) != elf_hash_table_id (htab)
13876 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13877 continue;
13878
13879 o = sub->sections;
13880 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13881 continue;
13882
13883 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13884 Also treat note sections as a root, if the section is not part
13885 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13886 well as FINI_ARRAY sections for ld -r. */
13887 for (o = sub->sections; o != NULL; o = o->next)
13888 if (!o->gc_mark
13889 && (o->flags & SEC_EXCLUDE) == 0
13890 && ((o->flags & SEC_KEEP) != 0
13891 || (bfd_link_relocatable (info)
13892 && ((elf_section_data (o)->this_hdr.sh_type
13893 == SHT_PREINIT_ARRAY)
13894 || (elf_section_data (o)->this_hdr.sh_type
13895 == SHT_INIT_ARRAY)
13896 || (elf_section_data (o)->this_hdr.sh_type
13897 == SHT_FINI_ARRAY)))
13898 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13899 && elf_next_in_group (o) == NULL )))
13900 {
13901 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13902 return FALSE;
13903 }
13904 }
13905
13906 /* Allow the backend to mark additional target specific sections. */
13907 bed->gc_mark_extra_sections (info, gc_mark_hook);
13908
13909 /* ... and mark SEC_EXCLUDE for those that go. */
13910 return elf_gc_sweep (abfd, info);
13911 }
13912 \f
13913 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13914
13915 bfd_boolean
13916 bfd_elf_gc_record_vtinherit (bfd *abfd,
13917 asection *sec,
13918 struct elf_link_hash_entry *h,
13919 bfd_vma offset)
13920 {
13921 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13922 struct elf_link_hash_entry **search, *child;
13923 size_t extsymcount;
13924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13925
13926 /* The sh_info field of the symtab header tells us where the
13927 external symbols start. We don't care about the local symbols at
13928 this point. */
13929 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13930 if (!elf_bad_symtab (abfd))
13931 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13932
13933 sym_hashes = elf_sym_hashes (abfd);
13934 sym_hashes_end = sym_hashes + extsymcount;
13935
13936 /* Hunt down the child symbol, which is in this section at the same
13937 offset as the relocation. */
13938 for (search = sym_hashes; search != sym_hashes_end; ++search)
13939 {
13940 if ((child = *search) != NULL
13941 && (child->root.type == bfd_link_hash_defined
13942 || child->root.type == bfd_link_hash_defweak)
13943 && child->root.u.def.section == sec
13944 && child->root.u.def.value == offset)
13945 goto win;
13946 }
13947
13948 /* xgettext:c-format */
13949 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13950 abfd, sec, (uint64_t) offset);
13951 bfd_set_error (bfd_error_invalid_operation);
13952 return FALSE;
13953
13954 win:
13955 if (!child->u2.vtable)
13956 {
13957 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13958 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13959 if (!child->u2.vtable)
13960 return FALSE;
13961 }
13962 if (!h)
13963 {
13964 /* This *should* only be the absolute section. It could potentially
13965 be that someone has defined a non-global vtable though, which
13966 would be bad. It isn't worth paging in the local symbols to be
13967 sure though; that case should simply be handled by the assembler. */
13968
13969 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13970 }
13971 else
13972 child->u2.vtable->parent = h;
13973
13974 return TRUE;
13975 }
13976
13977 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13978
13979 bfd_boolean
13980 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13981 struct elf_link_hash_entry *h,
13982 bfd_vma addend)
13983 {
13984 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13985 unsigned int log_file_align = bed->s->log_file_align;
13986
13987 if (!h)
13988 {
13989 /* xgettext:c-format */
13990 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13991 abfd, sec);
13992 bfd_set_error (bfd_error_bad_value);
13993 return FALSE;
13994 }
13995
13996 if (!h->u2.vtable)
13997 {
13998 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13999 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14000 if (!h->u2.vtable)
14001 return FALSE;
14002 }
14003
14004 if (addend >= h->u2.vtable->size)
14005 {
14006 size_t size, bytes, file_align;
14007 bfd_boolean *ptr = h->u2.vtable->used;
14008
14009 /* While the symbol is undefined, we have to be prepared to handle
14010 a zero size. */
14011 file_align = 1 << log_file_align;
14012 if (h->root.type == bfd_link_hash_undefined)
14013 size = addend + file_align;
14014 else
14015 {
14016 size = h->size;
14017 if (addend >= size)
14018 {
14019 /* Oops! We've got a reference past the defined end of
14020 the table. This is probably a bug -- shall we warn? */
14021 size = addend + file_align;
14022 }
14023 }
14024 size = (size + file_align - 1) & -file_align;
14025
14026 /* Allocate one extra entry for use as a "done" flag for the
14027 consolidation pass. */
14028 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14029
14030 if (ptr)
14031 {
14032 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14033
14034 if (ptr != NULL)
14035 {
14036 size_t oldbytes;
14037
14038 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14039 * sizeof (bfd_boolean));
14040 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14041 }
14042 }
14043 else
14044 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14045
14046 if (ptr == NULL)
14047 return FALSE;
14048
14049 /* And arrange for that done flag to be at index -1. */
14050 h->u2.vtable->used = ptr + 1;
14051 h->u2.vtable->size = size;
14052 }
14053
14054 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14055
14056 return TRUE;
14057 }
14058
14059 /* Map an ELF section header flag to its corresponding string. */
14060 typedef struct
14061 {
14062 char *flag_name;
14063 flagword flag_value;
14064 } elf_flags_to_name_table;
14065
14066 static elf_flags_to_name_table elf_flags_to_names [] =
14067 {
14068 { "SHF_WRITE", SHF_WRITE },
14069 { "SHF_ALLOC", SHF_ALLOC },
14070 { "SHF_EXECINSTR", SHF_EXECINSTR },
14071 { "SHF_MERGE", SHF_MERGE },
14072 { "SHF_STRINGS", SHF_STRINGS },
14073 { "SHF_INFO_LINK", SHF_INFO_LINK},
14074 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14075 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14076 { "SHF_GROUP", SHF_GROUP },
14077 { "SHF_TLS", SHF_TLS },
14078 { "SHF_MASKOS", SHF_MASKOS },
14079 { "SHF_EXCLUDE", SHF_EXCLUDE },
14080 };
14081
14082 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14083 bfd_boolean
14084 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14085 struct flag_info *flaginfo,
14086 asection *section)
14087 {
14088 const bfd_vma sh_flags = elf_section_flags (section);
14089
14090 if (!flaginfo->flags_initialized)
14091 {
14092 bfd *obfd = info->output_bfd;
14093 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14094 struct flag_info_list *tf = flaginfo->flag_list;
14095 int with_hex = 0;
14096 int without_hex = 0;
14097
14098 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14099 {
14100 unsigned i;
14101 flagword (*lookup) (char *);
14102
14103 lookup = bed->elf_backend_lookup_section_flags_hook;
14104 if (lookup != NULL)
14105 {
14106 flagword hexval = (*lookup) ((char *) tf->name);
14107
14108 if (hexval != 0)
14109 {
14110 if (tf->with == with_flags)
14111 with_hex |= hexval;
14112 else if (tf->with == without_flags)
14113 without_hex |= hexval;
14114 tf->valid = TRUE;
14115 continue;
14116 }
14117 }
14118 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14119 {
14120 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14121 {
14122 if (tf->with == with_flags)
14123 with_hex |= elf_flags_to_names[i].flag_value;
14124 else if (tf->with == without_flags)
14125 without_hex |= elf_flags_to_names[i].flag_value;
14126 tf->valid = TRUE;
14127 break;
14128 }
14129 }
14130 if (!tf->valid)
14131 {
14132 info->callbacks->einfo
14133 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14134 return FALSE;
14135 }
14136 }
14137 flaginfo->flags_initialized = TRUE;
14138 flaginfo->only_with_flags |= with_hex;
14139 flaginfo->not_with_flags |= without_hex;
14140 }
14141
14142 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14143 return FALSE;
14144
14145 if ((flaginfo->not_with_flags & sh_flags) != 0)
14146 return FALSE;
14147
14148 return TRUE;
14149 }
14150
14151 struct alloc_got_off_arg {
14152 bfd_vma gotoff;
14153 struct bfd_link_info *info;
14154 };
14155
14156 /* We need a special top-level link routine to convert got reference counts
14157 to real got offsets. */
14158
14159 static bfd_boolean
14160 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14161 {
14162 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14163 bfd *obfd = gofarg->info->output_bfd;
14164 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14165
14166 if (h->got.refcount > 0)
14167 {
14168 h->got.offset = gofarg->gotoff;
14169 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14170 }
14171 else
14172 h->got.offset = (bfd_vma) -1;
14173
14174 return TRUE;
14175 }
14176
14177 /* And an accompanying bit to work out final got entry offsets once
14178 we're done. Should be called from final_link. */
14179
14180 bfd_boolean
14181 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14182 struct bfd_link_info *info)
14183 {
14184 bfd *i;
14185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14186 bfd_vma gotoff;
14187 struct alloc_got_off_arg gofarg;
14188
14189 BFD_ASSERT (abfd == info->output_bfd);
14190
14191 if (! is_elf_hash_table (info->hash))
14192 return FALSE;
14193
14194 /* The GOT offset is relative to the .got section, but the GOT header is
14195 put into the .got.plt section, if the backend uses it. */
14196 if (bed->want_got_plt)
14197 gotoff = 0;
14198 else
14199 gotoff = bed->got_header_size;
14200
14201 /* Do the local .got entries first. */
14202 for (i = info->input_bfds; i; i = i->link.next)
14203 {
14204 bfd_signed_vma *local_got;
14205 size_t j, locsymcount;
14206 Elf_Internal_Shdr *symtab_hdr;
14207
14208 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14209 continue;
14210
14211 local_got = elf_local_got_refcounts (i);
14212 if (!local_got)
14213 continue;
14214
14215 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14216 if (elf_bad_symtab (i))
14217 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14218 else
14219 locsymcount = symtab_hdr->sh_info;
14220
14221 for (j = 0; j < locsymcount; ++j)
14222 {
14223 if (local_got[j] > 0)
14224 {
14225 local_got[j] = gotoff;
14226 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14227 }
14228 else
14229 local_got[j] = (bfd_vma) -1;
14230 }
14231 }
14232
14233 /* Then the global .got entries. .plt refcounts are handled by
14234 adjust_dynamic_symbol */
14235 gofarg.gotoff = gotoff;
14236 gofarg.info = info;
14237 elf_link_hash_traverse (elf_hash_table (info),
14238 elf_gc_allocate_got_offsets,
14239 &gofarg);
14240 return TRUE;
14241 }
14242
14243 /* Many folk need no more in the way of final link than this, once
14244 got entry reference counting is enabled. */
14245
14246 bfd_boolean
14247 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14248 {
14249 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14250 return FALSE;
14251
14252 /* Invoke the regular ELF backend linker to do all the work. */
14253 return bfd_elf_final_link (abfd, info);
14254 }
14255
14256 bfd_boolean
14257 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14258 {
14259 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14260
14261 if (rcookie->bad_symtab)
14262 rcookie->rel = rcookie->rels;
14263
14264 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14265 {
14266 unsigned long r_symndx;
14267
14268 if (! rcookie->bad_symtab)
14269 if (rcookie->rel->r_offset > offset)
14270 return FALSE;
14271 if (rcookie->rel->r_offset != offset)
14272 continue;
14273
14274 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14275 if (r_symndx == STN_UNDEF)
14276 return TRUE;
14277
14278 if (r_symndx >= rcookie->locsymcount
14279 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14280 {
14281 struct elf_link_hash_entry *h;
14282
14283 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14284
14285 while (h->root.type == bfd_link_hash_indirect
14286 || h->root.type == bfd_link_hash_warning)
14287 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14288
14289 if ((h->root.type == bfd_link_hash_defined
14290 || h->root.type == bfd_link_hash_defweak)
14291 && (h->root.u.def.section->owner != rcookie->abfd
14292 || h->root.u.def.section->kept_section != NULL
14293 || discarded_section (h->root.u.def.section)))
14294 return TRUE;
14295 }
14296 else
14297 {
14298 /* It's not a relocation against a global symbol,
14299 but it could be a relocation against a local
14300 symbol for a discarded section. */
14301 asection *isec;
14302 Elf_Internal_Sym *isym;
14303
14304 /* Need to: get the symbol; get the section. */
14305 isym = &rcookie->locsyms[r_symndx];
14306 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14307 if (isec != NULL
14308 && (isec->kept_section != NULL
14309 || discarded_section (isec)))
14310 return TRUE;
14311 }
14312 return FALSE;
14313 }
14314 return FALSE;
14315 }
14316
14317 /* Discard unneeded references to discarded sections.
14318 Returns -1 on error, 1 if any section's size was changed, 0 if
14319 nothing changed. This function assumes that the relocations are in
14320 sorted order, which is true for all known assemblers. */
14321
14322 int
14323 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14324 {
14325 struct elf_reloc_cookie cookie;
14326 asection *o;
14327 bfd *abfd;
14328 int changed = 0;
14329
14330 if (info->traditional_format
14331 || !is_elf_hash_table (info->hash))
14332 return 0;
14333
14334 o = bfd_get_section_by_name (output_bfd, ".stab");
14335 if (o != NULL)
14336 {
14337 asection *i;
14338
14339 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14340 {
14341 if (i->size == 0
14342 || i->reloc_count == 0
14343 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14344 continue;
14345
14346 abfd = i->owner;
14347 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14348 continue;
14349
14350 if (!init_reloc_cookie_for_section (&cookie, info, i))
14351 return -1;
14352
14353 if (_bfd_discard_section_stabs (abfd, i,
14354 elf_section_data (i)->sec_info,
14355 bfd_elf_reloc_symbol_deleted_p,
14356 &cookie))
14357 changed = 1;
14358
14359 fini_reloc_cookie_for_section (&cookie, i);
14360 }
14361 }
14362
14363 o = NULL;
14364 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14365 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14366 if (o != NULL)
14367 {
14368 asection *i;
14369 int eh_changed = 0;
14370 unsigned int eh_alignment; /* Octets. */
14371
14372 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14373 {
14374 if (i->size == 0)
14375 continue;
14376
14377 abfd = i->owner;
14378 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14379 continue;
14380
14381 if (!init_reloc_cookie_for_section (&cookie, info, i))
14382 return -1;
14383
14384 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14385 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14386 bfd_elf_reloc_symbol_deleted_p,
14387 &cookie))
14388 {
14389 eh_changed = 1;
14390 if (i->size != i->rawsize)
14391 changed = 1;
14392 }
14393
14394 fini_reloc_cookie_for_section (&cookie, i);
14395 }
14396
14397 eh_alignment = ((1 << o->alignment_power)
14398 * bfd_octets_per_byte (output_bfd, o));
14399 /* Skip over zero terminator, and prevent empty sections from
14400 adding alignment padding at the end. */
14401 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14402 if (i->size == 0)
14403 i->flags |= SEC_EXCLUDE;
14404 else if (i->size > 4)
14405 break;
14406 /* The last non-empty eh_frame section doesn't need padding. */
14407 if (i != NULL)
14408 i = i->map_tail.s;
14409 /* Any prior sections must pad the last FDE out to the output
14410 section alignment. Otherwise we might have zero padding
14411 between sections, which would be seen as a terminator. */
14412 for (; i != NULL; i = i->map_tail.s)
14413 if (i->size == 4)
14414 /* All but the last zero terminator should have been removed. */
14415 BFD_FAIL ();
14416 else
14417 {
14418 bfd_size_type size
14419 = (i->size + eh_alignment - 1) & -eh_alignment;
14420 if (i->size != size)
14421 {
14422 i->size = size;
14423 changed = 1;
14424 eh_changed = 1;
14425 }
14426 }
14427 if (eh_changed)
14428 elf_link_hash_traverse (elf_hash_table (info),
14429 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14430 }
14431
14432 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14433 {
14434 const struct elf_backend_data *bed;
14435 asection *s;
14436
14437 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14438 continue;
14439 s = abfd->sections;
14440 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14441 continue;
14442
14443 bed = get_elf_backend_data (abfd);
14444
14445 if (bed->elf_backend_discard_info != NULL)
14446 {
14447 if (!init_reloc_cookie (&cookie, info, abfd))
14448 return -1;
14449
14450 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14451 changed = 1;
14452
14453 fini_reloc_cookie (&cookie, abfd);
14454 }
14455 }
14456
14457 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14458 _bfd_elf_end_eh_frame_parsing (info);
14459
14460 if (info->eh_frame_hdr_type
14461 && !bfd_link_relocatable (info)
14462 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14463 changed = 1;
14464
14465 return changed;
14466 }
14467
14468 bfd_boolean
14469 _bfd_elf_section_already_linked (bfd *abfd,
14470 asection *sec,
14471 struct bfd_link_info *info)
14472 {
14473 flagword flags;
14474 const char *name, *key;
14475 struct bfd_section_already_linked *l;
14476 struct bfd_section_already_linked_hash_entry *already_linked_list;
14477
14478 if (sec->output_section == bfd_abs_section_ptr)
14479 return FALSE;
14480
14481 flags = sec->flags;
14482
14483 /* Return if it isn't a linkonce section. A comdat group section
14484 also has SEC_LINK_ONCE set. */
14485 if ((flags & SEC_LINK_ONCE) == 0)
14486 return FALSE;
14487
14488 /* Don't put group member sections on our list of already linked
14489 sections. They are handled as a group via their group section. */
14490 if (elf_sec_group (sec) != NULL)
14491 return FALSE;
14492
14493 /* For a SHT_GROUP section, use the group signature as the key. */
14494 name = sec->name;
14495 if ((flags & SEC_GROUP) != 0
14496 && elf_next_in_group (sec) != NULL
14497 && elf_group_name (elf_next_in_group (sec)) != NULL)
14498 key = elf_group_name (elf_next_in_group (sec));
14499 else
14500 {
14501 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14502 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14503 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14504 key++;
14505 else
14506 /* Must be a user linkonce section that doesn't follow gcc's
14507 naming convention. In this case we won't be matching
14508 single member groups. */
14509 key = name;
14510 }
14511
14512 already_linked_list = bfd_section_already_linked_table_lookup (key);
14513
14514 for (l = already_linked_list->entry; l != NULL; l = l->next)
14515 {
14516 /* We may have 2 different types of sections on the list: group
14517 sections with a signature of <key> (<key> is some string),
14518 and linkonce sections named .gnu.linkonce.<type>.<key>.
14519 Match like sections. LTO plugin sections are an exception.
14520 They are always named .gnu.linkonce.t.<key> and match either
14521 type of section. */
14522 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14523 && ((flags & SEC_GROUP) != 0
14524 || strcmp (name, l->sec->name) == 0))
14525 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14526 {
14527 /* The section has already been linked. See if we should
14528 issue a warning. */
14529 if (!_bfd_handle_already_linked (sec, l, info))
14530 return FALSE;
14531
14532 if (flags & SEC_GROUP)
14533 {
14534 asection *first = elf_next_in_group (sec);
14535 asection *s = first;
14536
14537 while (s != NULL)
14538 {
14539 s->output_section = bfd_abs_section_ptr;
14540 /* Record which group discards it. */
14541 s->kept_section = l->sec;
14542 s = elf_next_in_group (s);
14543 /* These lists are circular. */
14544 if (s == first)
14545 break;
14546 }
14547 }
14548
14549 return TRUE;
14550 }
14551 }
14552
14553 /* A single member comdat group section may be discarded by a
14554 linkonce section and vice versa. */
14555 if ((flags & SEC_GROUP) != 0)
14556 {
14557 asection *first = elf_next_in_group (sec);
14558
14559 if (first != NULL && elf_next_in_group (first) == first)
14560 /* Check this single member group against linkonce sections. */
14561 for (l = already_linked_list->entry; l != NULL; l = l->next)
14562 if ((l->sec->flags & SEC_GROUP) == 0
14563 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14564 {
14565 first->output_section = bfd_abs_section_ptr;
14566 first->kept_section = l->sec;
14567 sec->output_section = bfd_abs_section_ptr;
14568 break;
14569 }
14570 }
14571 else
14572 /* Check this linkonce section against single member groups. */
14573 for (l = already_linked_list->entry; l != NULL; l = l->next)
14574 if (l->sec->flags & SEC_GROUP)
14575 {
14576 asection *first = elf_next_in_group (l->sec);
14577
14578 if (first != NULL
14579 && elf_next_in_group (first) == first
14580 && bfd_elf_match_symbols_in_sections (first, sec, info))
14581 {
14582 sec->output_section = bfd_abs_section_ptr;
14583 sec->kept_section = first;
14584 break;
14585 }
14586 }
14587
14588 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14589 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14590 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14591 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14592 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14593 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14594 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14595 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14596 The reverse order cannot happen as there is never a bfd with only the
14597 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14598 matter as here were are looking only for cross-bfd sections. */
14599
14600 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14601 for (l = already_linked_list->entry; l != NULL; l = l->next)
14602 if ((l->sec->flags & SEC_GROUP) == 0
14603 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14604 {
14605 if (abfd != l->sec->owner)
14606 sec->output_section = bfd_abs_section_ptr;
14607 break;
14608 }
14609
14610 /* This is the first section with this name. Record it. */
14611 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14612 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14613 return sec->output_section == bfd_abs_section_ptr;
14614 }
14615
14616 bfd_boolean
14617 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14618 {
14619 return sym->st_shndx == SHN_COMMON;
14620 }
14621
14622 unsigned int
14623 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14624 {
14625 return SHN_COMMON;
14626 }
14627
14628 asection *
14629 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14630 {
14631 return bfd_com_section_ptr;
14632 }
14633
14634 bfd_vma
14635 _bfd_elf_default_got_elt_size (bfd *abfd,
14636 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14637 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14638 bfd *ibfd ATTRIBUTE_UNUSED,
14639 unsigned long symndx ATTRIBUTE_UNUSED)
14640 {
14641 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14642 return bed->s->arch_size / 8;
14643 }
14644
14645 /* Routines to support the creation of dynamic relocs. */
14646
14647 /* Returns the name of the dynamic reloc section associated with SEC. */
14648
14649 static const char *
14650 get_dynamic_reloc_section_name (bfd * abfd,
14651 asection * sec,
14652 bfd_boolean is_rela)
14653 {
14654 char *name;
14655 const char *old_name = bfd_section_name (sec);
14656 const char *prefix = is_rela ? ".rela" : ".rel";
14657
14658 if (old_name == NULL)
14659 return NULL;
14660
14661 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14662 sprintf (name, "%s%s", prefix, old_name);
14663
14664 return name;
14665 }
14666
14667 /* Returns the dynamic reloc section associated with SEC.
14668 If necessary compute the name of the dynamic reloc section based
14669 on SEC's name (looked up in ABFD's string table) and the setting
14670 of IS_RELA. */
14671
14672 asection *
14673 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14674 asection * sec,
14675 bfd_boolean is_rela)
14676 {
14677 asection * reloc_sec = elf_section_data (sec)->sreloc;
14678
14679 if (reloc_sec == NULL)
14680 {
14681 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14682
14683 if (name != NULL)
14684 {
14685 reloc_sec = bfd_get_linker_section (abfd, name);
14686
14687 if (reloc_sec != NULL)
14688 elf_section_data (sec)->sreloc = reloc_sec;
14689 }
14690 }
14691
14692 return reloc_sec;
14693 }
14694
14695 /* Returns the dynamic reloc section associated with SEC. If the
14696 section does not exist it is created and attached to the DYNOBJ
14697 bfd and stored in the SRELOC field of SEC's elf_section_data
14698 structure.
14699
14700 ALIGNMENT is the alignment for the newly created section and
14701 IS_RELA defines whether the name should be .rela.<SEC's name>
14702 or .rel.<SEC's name>. The section name is looked up in the
14703 string table associated with ABFD. */
14704
14705 asection *
14706 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14707 bfd *dynobj,
14708 unsigned int alignment,
14709 bfd *abfd,
14710 bfd_boolean is_rela)
14711 {
14712 asection * reloc_sec = elf_section_data (sec)->sreloc;
14713
14714 if (reloc_sec == NULL)
14715 {
14716 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14717
14718 if (name == NULL)
14719 return NULL;
14720
14721 reloc_sec = bfd_get_linker_section (dynobj, name);
14722
14723 if (reloc_sec == NULL)
14724 {
14725 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14726 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14727 if ((sec->flags & SEC_ALLOC) != 0)
14728 flags |= SEC_ALLOC | SEC_LOAD;
14729
14730 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14731 if (reloc_sec != NULL)
14732 {
14733 /* _bfd_elf_get_sec_type_attr chooses a section type by
14734 name. Override as it may be wrong, eg. for a user
14735 section named "auto" we'll get ".relauto" which is
14736 seen to be a .rela section. */
14737 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14738 if (!bfd_set_section_alignment (reloc_sec, alignment))
14739 reloc_sec = NULL;
14740 }
14741 }
14742
14743 elf_section_data (sec)->sreloc = reloc_sec;
14744 }
14745
14746 return reloc_sec;
14747 }
14748
14749 /* Copy the ELF symbol type and other attributes for a linker script
14750 assignment from HSRC to HDEST. Generally this should be treated as
14751 if we found a strong non-dynamic definition for HDEST (except that
14752 ld ignores multiple definition errors). */
14753 void
14754 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14755 struct bfd_link_hash_entry *hdest,
14756 struct bfd_link_hash_entry *hsrc)
14757 {
14758 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14759 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14760 Elf_Internal_Sym isym;
14761
14762 ehdest->type = ehsrc->type;
14763 ehdest->target_internal = ehsrc->target_internal;
14764
14765 isym.st_other = ehsrc->other;
14766 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14767 }
14768
14769 /* Append a RELA relocation REL to section S in BFD. */
14770
14771 void
14772 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14773 {
14774 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14775 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14776 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14777 bed->s->swap_reloca_out (abfd, rel, loc);
14778 }
14779
14780 /* Append a REL relocation REL to section S in BFD. */
14781
14782 void
14783 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14784 {
14785 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14786 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14787 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14788 bed->s->swap_reloc_out (abfd, rel, loc);
14789 }
14790
14791 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14792
14793 struct bfd_link_hash_entry *
14794 bfd_elf_define_start_stop (struct bfd_link_info *info,
14795 const char *symbol, asection *sec)
14796 {
14797 struct elf_link_hash_entry *h;
14798
14799 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14800 FALSE, FALSE, TRUE);
14801 if (h != NULL
14802 && (h->root.type == bfd_link_hash_undefined
14803 || h->root.type == bfd_link_hash_undefweak
14804 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14805 {
14806 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14807 h->root.type = bfd_link_hash_defined;
14808 h->root.u.def.section = sec;
14809 h->root.u.def.value = 0;
14810 h->def_regular = 1;
14811 h->def_dynamic = 0;
14812 h->start_stop = 1;
14813 h->u2.start_stop_section = sec;
14814 if (symbol[0] == '.')
14815 {
14816 /* .startof. and .sizeof. symbols are local. */
14817 const struct elf_backend_data *bed;
14818 bed = get_elf_backend_data (info->output_bfd);
14819 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14820 }
14821 else
14822 {
14823 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14824 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14825 if (was_dynamic)
14826 bfd_elf_link_record_dynamic_symbol (info, h);
14827 }
14828 return &h->root;
14829 }
14830 return NULL;
14831 }