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